xfs_bmap.c 189 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_alloc_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dir2_sf.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_dmapi.h"
  38. #include "xfs_mount.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_dir2_data.h"
  42. #include "xfs_dir2_leaf.h"
  43. #include "xfs_dir2_block.h"
  44. #include "xfs_inode_item.h"
  45. #include "xfs_extfree_item.h"
  46. #include "xfs_alloc.h"
  47. #include "xfs_bmap.h"
  48. #include "xfs_rtalloc.h"
  49. #include "xfs_error.h"
  50. #include "xfs_attr_leaf.h"
  51. #include "xfs_rw.h"
  52. #include "xfs_quota.h"
  53. #include "xfs_trans_space.h"
  54. #include "xfs_buf_item.h"
  55. #include "xfs_filestream.h"
  56. #include "xfs_vnodeops.h"
  57. #include "xfs_trace.h"
  58. #ifdef DEBUG
  59. STATIC void
  60. xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
  61. #endif
  62. kmem_zone_t *xfs_bmap_free_item_zone;
  63. /*
  64. * Prototypes for internal bmap routines.
  65. */
  66. /*
  67. * Called from xfs_bmap_add_attrfork to handle extents format files.
  68. */
  69. STATIC int /* error */
  70. xfs_bmap_add_attrfork_extents(
  71. xfs_trans_t *tp, /* transaction pointer */
  72. xfs_inode_t *ip, /* incore inode pointer */
  73. xfs_fsblock_t *firstblock, /* first block allocated */
  74. xfs_bmap_free_t *flist, /* blocks to free at commit */
  75. int *flags); /* inode logging flags */
  76. /*
  77. * Called from xfs_bmap_add_attrfork to handle local format files.
  78. */
  79. STATIC int /* error */
  80. xfs_bmap_add_attrfork_local(
  81. xfs_trans_t *tp, /* transaction pointer */
  82. xfs_inode_t *ip, /* incore inode pointer */
  83. xfs_fsblock_t *firstblock, /* first block allocated */
  84. xfs_bmap_free_t *flist, /* blocks to free at commit */
  85. int *flags); /* inode logging flags */
  86. /*
  87. * Called by xfs_bmapi to update file extent records and the btree
  88. * after allocating space (or doing a delayed allocation).
  89. */
  90. STATIC int /* error */
  91. xfs_bmap_add_extent(
  92. xfs_inode_t *ip, /* incore inode pointer */
  93. xfs_extnum_t idx, /* extent number to update/insert */
  94. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  95. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  96. xfs_fsblock_t *first, /* pointer to firstblock variable */
  97. xfs_bmap_free_t *flist, /* list of extents to be freed */
  98. int *logflagsp, /* inode logging flags */
  99. xfs_extdelta_t *delta, /* Change made to incore extents */
  100. int whichfork, /* data or attr fork */
  101. int rsvd); /* OK to allocate reserved blocks */
  102. /*
  103. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  104. * allocation to a real allocation.
  105. */
  106. STATIC int /* error */
  107. xfs_bmap_add_extent_delay_real(
  108. xfs_inode_t *ip, /* incore inode pointer */
  109. xfs_extnum_t idx, /* extent number to update/insert */
  110. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  111. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  112. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  113. xfs_fsblock_t *first, /* pointer to firstblock variable */
  114. xfs_bmap_free_t *flist, /* list of extents to be freed */
  115. int *logflagsp, /* inode logging flags */
  116. xfs_extdelta_t *delta, /* Change made to incore extents */
  117. int rsvd); /* OK to allocate reserved blocks */
  118. /*
  119. * Called by xfs_bmap_add_extent to handle cases converting a hole
  120. * to a delayed allocation.
  121. */
  122. STATIC int /* error */
  123. xfs_bmap_add_extent_hole_delay(
  124. xfs_inode_t *ip, /* incore inode pointer */
  125. xfs_extnum_t idx, /* extent number to update/insert */
  126. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  127. int *logflagsp,/* inode logging flags */
  128. xfs_extdelta_t *delta, /* Change made to incore extents */
  129. int rsvd); /* OK to allocate reserved blocks */
  130. /*
  131. * Called by xfs_bmap_add_extent to handle cases converting a hole
  132. * to a real allocation.
  133. */
  134. STATIC int /* error */
  135. xfs_bmap_add_extent_hole_real(
  136. xfs_inode_t *ip, /* incore inode pointer */
  137. xfs_extnum_t idx, /* extent number to update/insert */
  138. xfs_btree_cur_t *cur, /* if null, not a btree */
  139. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  140. int *logflagsp, /* inode logging flags */
  141. xfs_extdelta_t *delta, /* Change made to incore extents */
  142. int whichfork); /* data or attr fork */
  143. /*
  144. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  145. * allocation to a real allocation or vice versa.
  146. */
  147. STATIC int /* error */
  148. xfs_bmap_add_extent_unwritten_real(
  149. xfs_inode_t *ip, /* incore inode pointer */
  150. xfs_extnum_t idx, /* extent number to update/insert */
  151. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  152. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  153. int *logflagsp, /* inode logging flags */
  154. xfs_extdelta_t *delta); /* Change made to incore extents */
  155. /*
  156. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  157. * It figures out where to ask the underlying allocator to put the new extent.
  158. */
  159. STATIC int /* error */
  160. xfs_bmap_alloc(
  161. xfs_bmalloca_t *ap); /* bmap alloc argument struct */
  162. /*
  163. * Transform a btree format file with only one leaf node, where the
  164. * extents list will fit in the inode, into an extents format file.
  165. * Since the file extents are already in-core, all we have to do is
  166. * give up the space for the btree root and pitch the leaf block.
  167. */
  168. STATIC int /* error */
  169. xfs_bmap_btree_to_extents(
  170. xfs_trans_t *tp, /* transaction pointer */
  171. xfs_inode_t *ip, /* incore inode pointer */
  172. xfs_btree_cur_t *cur, /* btree cursor */
  173. int *logflagsp, /* inode logging flags */
  174. int whichfork); /* data or attr fork */
  175. /*
  176. * Called by xfs_bmapi to update file extent records and the btree
  177. * after removing space (or undoing a delayed allocation).
  178. */
  179. STATIC int /* error */
  180. xfs_bmap_del_extent(
  181. xfs_inode_t *ip, /* incore inode pointer */
  182. xfs_trans_t *tp, /* current trans pointer */
  183. xfs_extnum_t idx, /* extent number to update/insert */
  184. xfs_bmap_free_t *flist, /* list of extents to be freed */
  185. xfs_btree_cur_t *cur, /* if null, not a btree */
  186. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  187. int *logflagsp,/* inode logging flags */
  188. xfs_extdelta_t *delta, /* Change made to incore extents */
  189. int whichfork, /* data or attr fork */
  190. int rsvd); /* OK to allocate reserved blocks */
  191. /*
  192. * Remove the entry "free" from the free item list. Prev points to the
  193. * previous entry, unless "free" is the head of the list.
  194. */
  195. STATIC void
  196. xfs_bmap_del_free(
  197. xfs_bmap_free_t *flist, /* free item list header */
  198. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  199. xfs_bmap_free_item_t *free); /* list item to be freed */
  200. /*
  201. * Convert an extents-format file into a btree-format file.
  202. * The new file will have a root block (in the inode) and a single child block.
  203. */
  204. STATIC int /* error */
  205. xfs_bmap_extents_to_btree(
  206. xfs_trans_t *tp, /* transaction pointer */
  207. xfs_inode_t *ip, /* incore inode pointer */
  208. xfs_fsblock_t *firstblock, /* first-block-allocated */
  209. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  210. xfs_btree_cur_t **curp, /* cursor returned to caller */
  211. int wasdel, /* converting a delayed alloc */
  212. int *logflagsp, /* inode logging flags */
  213. int whichfork); /* data or attr fork */
  214. /*
  215. * Convert a local file to an extents file.
  216. * This code is sort of bogus, since the file data needs to get
  217. * logged so it won't be lost. The bmap-level manipulations are ok, though.
  218. */
  219. STATIC int /* error */
  220. xfs_bmap_local_to_extents(
  221. xfs_trans_t *tp, /* transaction pointer */
  222. xfs_inode_t *ip, /* incore inode pointer */
  223. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  224. xfs_extlen_t total, /* total blocks needed by transaction */
  225. int *logflagsp, /* inode logging flags */
  226. int whichfork); /* data or attr fork */
  227. /*
  228. * Search the extents list for the inode, for the extent containing bno.
  229. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  230. * *eofp will be set, and *prevp will contain the last entry (null if none).
  231. * Else, *lastxp will be set to the index of the found
  232. * entry; *gotp will contain the entry.
  233. */
  234. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  235. xfs_bmap_search_extents(
  236. xfs_inode_t *ip, /* incore inode pointer */
  237. xfs_fileoff_t bno, /* block number searched for */
  238. int whichfork, /* data or attr fork */
  239. int *eofp, /* out: end of file found */
  240. xfs_extnum_t *lastxp, /* out: last extent index */
  241. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  242. xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
  243. /*
  244. * Check the last inode extent to determine whether this allocation will result
  245. * in blocks being allocated at the end of the file. When we allocate new data
  246. * blocks at the end of the file which do not start at the previous data block,
  247. * we will try to align the new blocks at stripe unit boundaries.
  248. */
  249. STATIC int /* error */
  250. xfs_bmap_isaeof(
  251. xfs_inode_t *ip, /* incore inode pointer */
  252. xfs_fileoff_t off, /* file offset in fsblocks */
  253. int whichfork, /* data or attribute fork */
  254. char *aeof); /* return value */
  255. /*
  256. * Compute the worst-case number of indirect blocks that will be used
  257. * for ip's delayed extent of length "len".
  258. */
  259. STATIC xfs_filblks_t
  260. xfs_bmap_worst_indlen(
  261. xfs_inode_t *ip, /* incore inode pointer */
  262. xfs_filblks_t len); /* delayed extent length */
  263. #ifdef DEBUG
  264. /*
  265. * Perform various validation checks on the values being returned
  266. * from xfs_bmapi().
  267. */
  268. STATIC void
  269. xfs_bmap_validate_ret(
  270. xfs_fileoff_t bno,
  271. xfs_filblks_t len,
  272. int flags,
  273. xfs_bmbt_irec_t *mval,
  274. int nmap,
  275. int ret_nmap);
  276. #else
  277. #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
  278. #endif /* DEBUG */
  279. STATIC int
  280. xfs_bmap_count_tree(
  281. xfs_mount_t *mp,
  282. xfs_trans_t *tp,
  283. xfs_ifork_t *ifp,
  284. xfs_fsblock_t blockno,
  285. int levelin,
  286. int *count);
  287. STATIC void
  288. xfs_bmap_count_leaves(
  289. xfs_ifork_t *ifp,
  290. xfs_extnum_t idx,
  291. int numrecs,
  292. int *count);
  293. STATIC void
  294. xfs_bmap_disk_count_leaves(
  295. struct xfs_mount *mp,
  296. struct xfs_btree_block *block,
  297. int numrecs,
  298. int *count);
  299. /*
  300. * Bmap internal routines.
  301. */
  302. STATIC int /* error */
  303. xfs_bmbt_lookup_eq(
  304. struct xfs_btree_cur *cur,
  305. xfs_fileoff_t off,
  306. xfs_fsblock_t bno,
  307. xfs_filblks_t len,
  308. int *stat) /* success/failure */
  309. {
  310. cur->bc_rec.b.br_startoff = off;
  311. cur->bc_rec.b.br_startblock = bno;
  312. cur->bc_rec.b.br_blockcount = len;
  313. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  314. }
  315. STATIC int /* error */
  316. xfs_bmbt_lookup_ge(
  317. struct xfs_btree_cur *cur,
  318. xfs_fileoff_t off,
  319. xfs_fsblock_t bno,
  320. xfs_filblks_t len,
  321. int *stat) /* success/failure */
  322. {
  323. cur->bc_rec.b.br_startoff = off;
  324. cur->bc_rec.b.br_startblock = bno;
  325. cur->bc_rec.b.br_blockcount = len;
  326. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  327. }
  328. /*
  329. * Update the record referred to by cur to the value given
  330. * by [off, bno, len, state].
  331. * This either works (return 0) or gets an EFSCORRUPTED error.
  332. */
  333. STATIC int
  334. xfs_bmbt_update(
  335. struct xfs_btree_cur *cur,
  336. xfs_fileoff_t off,
  337. xfs_fsblock_t bno,
  338. xfs_filblks_t len,
  339. xfs_exntst_t state)
  340. {
  341. union xfs_btree_rec rec;
  342. xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
  343. return xfs_btree_update(cur, &rec);
  344. }
  345. /*
  346. * Called from xfs_bmap_add_attrfork to handle btree format files.
  347. */
  348. STATIC int /* error */
  349. xfs_bmap_add_attrfork_btree(
  350. xfs_trans_t *tp, /* transaction pointer */
  351. xfs_inode_t *ip, /* incore inode pointer */
  352. xfs_fsblock_t *firstblock, /* first block allocated */
  353. xfs_bmap_free_t *flist, /* blocks to free at commit */
  354. int *flags) /* inode logging flags */
  355. {
  356. xfs_btree_cur_t *cur; /* btree cursor */
  357. int error; /* error return value */
  358. xfs_mount_t *mp; /* file system mount struct */
  359. int stat; /* newroot status */
  360. mp = ip->i_mount;
  361. if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
  362. *flags |= XFS_ILOG_DBROOT;
  363. else {
  364. cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
  365. cur->bc_private.b.flist = flist;
  366. cur->bc_private.b.firstblock = *firstblock;
  367. if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
  368. goto error0;
  369. /* must be at least one entry */
  370. XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
  371. if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
  372. goto error0;
  373. if (stat == 0) {
  374. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  375. return XFS_ERROR(ENOSPC);
  376. }
  377. *firstblock = cur->bc_private.b.firstblock;
  378. cur->bc_private.b.allocated = 0;
  379. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  380. }
  381. return 0;
  382. error0:
  383. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  384. return error;
  385. }
  386. /*
  387. * Called from xfs_bmap_add_attrfork to handle extents format files.
  388. */
  389. STATIC int /* error */
  390. xfs_bmap_add_attrfork_extents(
  391. xfs_trans_t *tp, /* transaction pointer */
  392. xfs_inode_t *ip, /* incore inode pointer */
  393. xfs_fsblock_t *firstblock, /* first block allocated */
  394. xfs_bmap_free_t *flist, /* blocks to free at commit */
  395. int *flags) /* inode logging flags */
  396. {
  397. xfs_btree_cur_t *cur; /* bmap btree cursor */
  398. int error; /* error return value */
  399. if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
  400. return 0;
  401. cur = NULL;
  402. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
  403. flags, XFS_DATA_FORK);
  404. if (cur) {
  405. cur->bc_private.b.allocated = 0;
  406. xfs_btree_del_cursor(cur,
  407. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  408. }
  409. return error;
  410. }
  411. /*
  412. * Called from xfs_bmap_add_attrfork to handle local format files.
  413. */
  414. STATIC int /* error */
  415. xfs_bmap_add_attrfork_local(
  416. xfs_trans_t *tp, /* transaction pointer */
  417. xfs_inode_t *ip, /* incore inode pointer */
  418. xfs_fsblock_t *firstblock, /* first block allocated */
  419. xfs_bmap_free_t *flist, /* blocks to free at commit */
  420. int *flags) /* inode logging flags */
  421. {
  422. xfs_da_args_t dargs; /* args for dir/attr code */
  423. int error; /* error return value */
  424. xfs_mount_t *mp; /* mount structure pointer */
  425. if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
  426. return 0;
  427. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  428. mp = ip->i_mount;
  429. memset(&dargs, 0, sizeof(dargs));
  430. dargs.dp = ip;
  431. dargs.firstblock = firstblock;
  432. dargs.flist = flist;
  433. dargs.total = mp->m_dirblkfsbs;
  434. dargs.whichfork = XFS_DATA_FORK;
  435. dargs.trans = tp;
  436. error = xfs_dir2_sf_to_block(&dargs);
  437. } else
  438. error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
  439. XFS_DATA_FORK);
  440. return error;
  441. }
  442. /*
  443. * Called by xfs_bmapi to update file extent records and the btree
  444. * after allocating space (or doing a delayed allocation).
  445. */
  446. STATIC int /* error */
  447. xfs_bmap_add_extent(
  448. xfs_inode_t *ip, /* incore inode pointer */
  449. xfs_extnum_t idx, /* extent number to update/insert */
  450. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  451. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  452. xfs_fsblock_t *first, /* pointer to firstblock variable */
  453. xfs_bmap_free_t *flist, /* list of extents to be freed */
  454. int *logflagsp, /* inode logging flags */
  455. xfs_extdelta_t *delta, /* Change made to incore extents */
  456. int whichfork, /* data or attr fork */
  457. int rsvd) /* OK to use reserved data blocks */
  458. {
  459. xfs_btree_cur_t *cur; /* btree cursor or null */
  460. xfs_filblks_t da_new; /* new count del alloc blocks used */
  461. xfs_filblks_t da_old; /* old count del alloc blocks used */
  462. int error; /* error return value */
  463. xfs_ifork_t *ifp; /* inode fork ptr */
  464. int logflags; /* returned value */
  465. xfs_extnum_t nextents; /* number of extents in file now */
  466. XFS_STATS_INC(xs_add_exlist);
  467. cur = *curp;
  468. ifp = XFS_IFORK_PTR(ip, whichfork);
  469. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  470. ASSERT(idx <= nextents);
  471. da_old = da_new = 0;
  472. error = 0;
  473. /*
  474. * This is the first extent added to a new/empty file.
  475. * Special case this one, so other routines get to assume there are
  476. * already extents in the list.
  477. */
  478. if (nextents == 0) {
  479. xfs_iext_insert(ip, 0, 1, new,
  480. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  481. ASSERT(cur == NULL);
  482. ifp->if_lastex = 0;
  483. if (!isnullstartblock(new->br_startblock)) {
  484. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  485. logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  486. } else
  487. logflags = 0;
  488. /* DELTA: single new extent */
  489. if (delta) {
  490. if (delta->xed_startoff > new->br_startoff)
  491. delta->xed_startoff = new->br_startoff;
  492. if (delta->xed_blockcount <
  493. new->br_startoff + new->br_blockcount)
  494. delta->xed_blockcount = new->br_startoff +
  495. new->br_blockcount;
  496. }
  497. }
  498. /*
  499. * Any kind of new delayed allocation goes here.
  500. */
  501. else if (isnullstartblock(new->br_startblock)) {
  502. if (cur)
  503. ASSERT((cur->bc_private.b.flags &
  504. XFS_BTCUR_BPRV_WASDEL) == 0);
  505. if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
  506. &logflags, delta, rsvd)))
  507. goto done;
  508. }
  509. /*
  510. * Real allocation off the end of the file.
  511. */
  512. else if (idx == nextents) {
  513. if (cur)
  514. ASSERT((cur->bc_private.b.flags &
  515. XFS_BTCUR_BPRV_WASDEL) == 0);
  516. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
  517. &logflags, delta, whichfork)))
  518. goto done;
  519. } else {
  520. xfs_bmbt_irec_t prev; /* old extent at offset idx */
  521. /*
  522. * Get the record referred to by idx.
  523. */
  524. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
  525. /*
  526. * If it's a real allocation record, and the new allocation ends
  527. * after the start of the referred to record, then we're filling
  528. * in a delayed or unwritten allocation with a real one, or
  529. * converting real back to unwritten.
  530. */
  531. if (!isnullstartblock(new->br_startblock) &&
  532. new->br_startoff + new->br_blockcount > prev.br_startoff) {
  533. if (prev.br_state != XFS_EXT_UNWRITTEN &&
  534. isnullstartblock(prev.br_startblock)) {
  535. da_old = startblockval(prev.br_startblock);
  536. if (cur)
  537. ASSERT(cur->bc_private.b.flags &
  538. XFS_BTCUR_BPRV_WASDEL);
  539. if ((error = xfs_bmap_add_extent_delay_real(ip,
  540. idx, &cur, new, &da_new, first, flist,
  541. &logflags, delta, rsvd)))
  542. goto done;
  543. } else if (new->br_state == XFS_EXT_NORM) {
  544. ASSERT(new->br_state == XFS_EXT_NORM);
  545. if ((error = xfs_bmap_add_extent_unwritten_real(
  546. ip, idx, &cur, new, &logflags, delta)))
  547. goto done;
  548. } else {
  549. ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
  550. if ((error = xfs_bmap_add_extent_unwritten_real(
  551. ip, idx, &cur, new, &logflags, delta)))
  552. goto done;
  553. }
  554. ASSERT(*curp == cur || *curp == NULL);
  555. }
  556. /*
  557. * Otherwise we're filling in a hole with an allocation.
  558. */
  559. else {
  560. if (cur)
  561. ASSERT((cur->bc_private.b.flags &
  562. XFS_BTCUR_BPRV_WASDEL) == 0);
  563. if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
  564. new, &logflags, delta, whichfork)))
  565. goto done;
  566. }
  567. }
  568. ASSERT(*curp == cur || *curp == NULL);
  569. /*
  570. * Convert to a btree if necessary.
  571. */
  572. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  573. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  574. int tmp_logflags; /* partial log flag return val */
  575. ASSERT(cur == NULL);
  576. error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
  577. flist, &cur, da_old > 0, &tmp_logflags, whichfork);
  578. logflags |= tmp_logflags;
  579. if (error)
  580. goto done;
  581. }
  582. /*
  583. * Adjust for changes in reserved delayed indirect blocks.
  584. * Nothing to do for disk quotas here.
  585. */
  586. if (da_old || da_new) {
  587. xfs_filblks_t nblks;
  588. nblks = da_new;
  589. if (cur)
  590. nblks += cur->bc_private.b.allocated;
  591. ASSERT(nblks <= da_old);
  592. if (nblks < da_old)
  593. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  594. (int64_t)(da_old - nblks), rsvd);
  595. }
  596. /*
  597. * Clear out the allocated field, done with it now in any case.
  598. */
  599. if (cur) {
  600. cur->bc_private.b.allocated = 0;
  601. *curp = cur;
  602. }
  603. done:
  604. #ifdef DEBUG
  605. if (!error)
  606. xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
  607. #endif
  608. *logflagsp = logflags;
  609. return error;
  610. }
  611. /*
  612. * Called by xfs_bmap_add_extent to handle cases converting a delayed
  613. * allocation to a real allocation.
  614. */
  615. STATIC int /* error */
  616. xfs_bmap_add_extent_delay_real(
  617. xfs_inode_t *ip, /* incore inode pointer */
  618. xfs_extnum_t idx, /* extent number to update/insert */
  619. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  620. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  621. xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
  622. xfs_fsblock_t *first, /* pointer to firstblock variable */
  623. xfs_bmap_free_t *flist, /* list of extents to be freed */
  624. int *logflagsp, /* inode logging flags */
  625. xfs_extdelta_t *delta, /* Change made to incore extents */
  626. int rsvd) /* OK to use reserved data block allocation */
  627. {
  628. xfs_btree_cur_t *cur; /* btree cursor */
  629. int diff; /* temp value */
  630. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  631. int error; /* error return value */
  632. int i; /* temp state */
  633. xfs_ifork_t *ifp; /* inode fork pointer */
  634. xfs_fileoff_t new_endoff; /* end offset of new entry */
  635. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  636. /* left is 0, right is 1, prev is 2 */
  637. int rval=0; /* return value (logging flags) */
  638. int state = 0;/* state bits, accessed thru macros */
  639. xfs_filblks_t temp=0; /* value for dnew calculations */
  640. xfs_filblks_t temp2=0;/* value for dnew calculations */
  641. int tmp_rval; /* partial logging flags */
  642. #define LEFT r[0]
  643. #define RIGHT r[1]
  644. #define PREV r[2]
  645. /*
  646. * Set up a bunch of variables to make the tests simpler.
  647. */
  648. cur = *curp;
  649. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  650. ep = xfs_iext_get_ext(ifp, idx);
  651. xfs_bmbt_get_all(ep, &PREV);
  652. new_endoff = new->br_startoff + new->br_blockcount;
  653. ASSERT(PREV.br_startoff <= new->br_startoff);
  654. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  655. /*
  656. * Set flags determining what part of the previous delayed allocation
  657. * extent is being replaced by a real allocation.
  658. */
  659. if (PREV.br_startoff == new->br_startoff)
  660. state |= BMAP_LEFT_FILLING;
  661. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  662. state |= BMAP_RIGHT_FILLING;
  663. /*
  664. * Check and set flags if this segment has a left neighbor.
  665. * Don't set contiguous if the combined extent would be too large.
  666. */
  667. if (idx > 0) {
  668. state |= BMAP_LEFT_VALID;
  669. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
  670. if (isnullstartblock(LEFT.br_startblock))
  671. state |= BMAP_LEFT_DELAY;
  672. }
  673. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  674. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  675. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  676. LEFT.br_state == new->br_state &&
  677. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  678. state |= BMAP_LEFT_CONTIG;
  679. /*
  680. * Check and set flags if this segment has a right neighbor.
  681. * Don't set contiguous if the combined extent would be too large.
  682. * Also check for all-three-contiguous being too large.
  683. */
  684. if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
  685. state |= BMAP_RIGHT_VALID;
  686. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
  687. if (isnullstartblock(RIGHT.br_startblock))
  688. state |= BMAP_RIGHT_DELAY;
  689. }
  690. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  691. new_endoff == RIGHT.br_startoff &&
  692. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  693. new->br_state == RIGHT.br_state &&
  694. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  695. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  696. BMAP_RIGHT_FILLING)) !=
  697. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  698. BMAP_RIGHT_FILLING) ||
  699. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  700. <= MAXEXTLEN))
  701. state |= BMAP_RIGHT_CONTIG;
  702. error = 0;
  703. /*
  704. * Switch out based on the FILLING and CONTIG state bits.
  705. */
  706. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  707. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  708. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  709. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  710. /*
  711. * Filling in all of a previously delayed allocation extent.
  712. * The left and right neighbors are both contiguous with new.
  713. */
  714. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  715. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  716. LEFT.br_blockcount + PREV.br_blockcount +
  717. RIGHT.br_blockcount);
  718. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  719. xfs_iext_remove(ip, idx, 2, state);
  720. ip->i_df.if_lastex = idx - 1;
  721. ip->i_d.di_nextents--;
  722. if (cur == NULL)
  723. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  724. else {
  725. rval = XFS_ILOG_CORE;
  726. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  727. RIGHT.br_startblock,
  728. RIGHT.br_blockcount, &i)))
  729. goto done;
  730. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  731. if ((error = xfs_btree_delete(cur, &i)))
  732. goto done;
  733. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  734. if ((error = xfs_btree_decrement(cur, 0, &i)))
  735. goto done;
  736. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  737. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  738. LEFT.br_startblock,
  739. LEFT.br_blockcount +
  740. PREV.br_blockcount +
  741. RIGHT.br_blockcount, LEFT.br_state)))
  742. goto done;
  743. }
  744. *dnew = 0;
  745. /* DELTA: Three in-core extents are replaced by one. */
  746. temp = LEFT.br_startoff;
  747. temp2 = LEFT.br_blockcount +
  748. PREV.br_blockcount +
  749. RIGHT.br_blockcount;
  750. break;
  751. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  752. /*
  753. * Filling in all of a previously delayed allocation extent.
  754. * The left neighbor is contiguous, the right is not.
  755. */
  756. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  757. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  758. LEFT.br_blockcount + PREV.br_blockcount);
  759. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  760. ip->i_df.if_lastex = idx - 1;
  761. xfs_iext_remove(ip, idx, 1, state);
  762. if (cur == NULL)
  763. rval = XFS_ILOG_DEXT;
  764. else {
  765. rval = 0;
  766. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  767. LEFT.br_startblock, LEFT.br_blockcount,
  768. &i)))
  769. goto done;
  770. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  771. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  772. LEFT.br_startblock,
  773. LEFT.br_blockcount +
  774. PREV.br_blockcount, LEFT.br_state)))
  775. goto done;
  776. }
  777. *dnew = 0;
  778. /* DELTA: Two in-core extents are replaced by one. */
  779. temp = LEFT.br_startoff;
  780. temp2 = LEFT.br_blockcount +
  781. PREV.br_blockcount;
  782. break;
  783. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  784. /*
  785. * Filling in all of a previously delayed allocation extent.
  786. * The right neighbor is contiguous, the left is not.
  787. */
  788. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  789. xfs_bmbt_set_startblock(ep, new->br_startblock);
  790. xfs_bmbt_set_blockcount(ep,
  791. PREV.br_blockcount + RIGHT.br_blockcount);
  792. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  793. ip->i_df.if_lastex = idx;
  794. xfs_iext_remove(ip, idx + 1, 1, state);
  795. if (cur == NULL)
  796. rval = XFS_ILOG_DEXT;
  797. else {
  798. rval = 0;
  799. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  800. RIGHT.br_startblock,
  801. RIGHT.br_blockcount, &i)))
  802. goto done;
  803. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  804. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  805. new->br_startblock,
  806. PREV.br_blockcount +
  807. RIGHT.br_blockcount, PREV.br_state)))
  808. goto done;
  809. }
  810. *dnew = 0;
  811. /* DELTA: Two in-core extents are replaced by one. */
  812. temp = PREV.br_startoff;
  813. temp2 = PREV.br_blockcount +
  814. RIGHT.br_blockcount;
  815. break;
  816. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  817. /*
  818. * Filling in all of a previously delayed allocation extent.
  819. * Neither the left nor right neighbors are contiguous with
  820. * the new one.
  821. */
  822. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  823. xfs_bmbt_set_startblock(ep, new->br_startblock);
  824. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  825. ip->i_df.if_lastex = idx;
  826. ip->i_d.di_nextents++;
  827. if (cur == NULL)
  828. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  829. else {
  830. rval = XFS_ILOG_CORE;
  831. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  832. new->br_startblock, new->br_blockcount,
  833. &i)))
  834. goto done;
  835. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  836. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  837. if ((error = xfs_btree_insert(cur, &i)))
  838. goto done;
  839. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  840. }
  841. *dnew = 0;
  842. /* DELTA: The in-core extent described by new changed type. */
  843. temp = new->br_startoff;
  844. temp2 = new->br_blockcount;
  845. break;
  846. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  847. /*
  848. * Filling in the first part of a previous delayed allocation.
  849. * The left neighbor is contiguous.
  850. */
  851. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  852. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  853. LEFT.br_blockcount + new->br_blockcount);
  854. xfs_bmbt_set_startoff(ep,
  855. PREV.br_startoff + new->br_blockcount);
  856. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  857. temp = PREV.br_blockcount - new->br_blockcount;
  858. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  859. xfs_bmbt_set_blockcount(ep, temp);
  860. ip->i_df.if_lastex = idx - 1;
  861. if (cur == NULL)
  862. rval = XFS_ILOG_DEXT;
  863. else {
  864. rval = 0;
  865. if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
  866. LEFT.br_startblock, LEFT.br_blockcount,
  867. &i)))
  868. goto done;
  869. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  870. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  871. LEFT.br_startblock,
  872. LEFT.br_blockcount +
  873. new->br_blockcount,
  874. LEFT.br_state)))
  875. goto done;
  876. }
  877. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  878. startblockval(PREV.br_startblock));
  879. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  880. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  881. *dnew = temp;
  882. /* DELTA: The boundary between two in-core extents moved. */
  883. temp = LEFT.br_startoff;
  884. temp2 = LEFT.br_blockcount +
  885. PREV.br_blockcount;
  886. break;
  887. case BMAP_LEFT_FILLING:
  888. /*
  889. * Filling in the first part of a previous delayed allocation.
  890. * The left neighbor is not contiguous.
  891. */
  892. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  893. xfs_bmbt_set_startoff(ep, new_endoff);
  894. temp = PREV.br_blockcount - new->br_blockcount;
  895. xfs_bmbt_set_blockcount(ep, temp);
  896. xfs_iext_insert(ip, idx, 1, new, state);
  897. ip->i_df.if_lastex = idx;
  898. ip->i_d.di_nextents++;
  899. if (cur == NULL)
  900. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  901. else {
  902. rval = XFS_ILOG_CORE;
  903. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  904. new->br_startblock, new->br_blockcount,
  905. &i)))
  906. goto done;
  907. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  908. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  909. if ((error = xfs_btree_insert(cur, &i)))
  910. goto done;
  911. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  912. }
  913. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  914. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  915. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  916. first, flist, &cur, 1, &tmp_rval,
  917. XFS_DATA_FORK);
  918. rval |= tmp_rval;
  919. if (error)
  920. goto done;
  921. }
  922. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  923. startblockval(PREV.br_startblock) -
  924. (cur ? cur->bc_private.b.allocated : 0));
  925. ep = xfs_iext_get_ext(ifp, idx + 1);
  926. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  927. trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
  928. *dnew = temp;
  929. /* DELTA: One in-core extent is split in two. */
  930. temp = PREV.br_startoff;
  931. temp2 = PREV.br_blockcount;
  932. break;
  933. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  934. /*
  935. * Filling in the last part of a previous delayed allocation.
  936. * The right neighbor is contiguous with the new allocation.
  937. */
  938. temp = PREV.br_blockcount - new->br_blockcount;
  939. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  940. trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
  941. xfs_bmbt_set_blockcount(ep, temp);
  942. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
  943. new->br_startoff, new->br_startblock,
  944. new->br_blockcount + RIGHT.br_blockcount,
  945. RIGHT.br_state);
  946. trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
  947. ip->i_df.if_lastex = idx + 1;
  948. if (cur == NULL)
  949. rval = XFS_ILOG_DEXT;
  950. else {
  951. rval = 0;
  952. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  953. RIGHT.br_startblock,
  954. RIGHT.br_blockcount, &i)))
  955. goto done;
  956. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  957. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  958. new->br_startblock,
  959. new->br_blockcount +
  960. RIGHT.br_blockcount,
  961. RIGHT.br_state)))
  962. goto done;
  963. }
  964. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  965. startblockval(PREV.br_startblock));
  966. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  967. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  968. *dnew = temp;
  969. /* DELTA: The boundary between two in-core extents moved. */
  970. temp = PREV.br_startoff;
  971. temp2 = PREV.br_blockcount +
  972. RIGHT.br_blockcount;
  973. break;
  974. case BMAP_RIGHT_FILLING:
  975. /*
  976. * Filling in the last part of a previous delayed allocation.
  977. * The right neighbor is not contiguous.
  978. */
  979. temp = PREV.br_blockcount - new->br_blockcount;
  980. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  981. xfs_bmbt_set_blockcount(ep, temp);
  982. xfs_iext_insert(ip, idx + 1, 1, new, state);
  983. ip->i_df.if_lastex = idx + 1;
  984. ip->i_d.di_nextents++;
  985. if (cur == NULL)
  986. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  987. else {
  988. rval = XFS_ILOG_CORE;
  989. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  990. new->br_startblock, new->br_blockcount,
  991. &i)))
  992. goto done;
  993. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  994. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  995. if ((error = xfs_btree_insert(cur, &i)))
  996. goto done;
  997. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  998. }
  999. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1000. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1001. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1002. first, flist, &cur, 1, &tmp_rval,
  1003. XFS_DATA_FORK);
  1004. rval |= tmp_rval;
  1005. if (error)
  1006. goto done;
  1007. }
  1008. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  1009. startblockval(PREV.br_startblock) -
  1010. (cur ? cur->bc_private.b.allocated : 0));
  1011. ep = xfs_iext_get_ext(ifp, idx);
  1012. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  1013. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1014. *dnew = temp;
  1015. /* DELTA: One in-core extent is split in two. */
  1016. temp = PREV.br_startoff;
  1017. temp2 = PREV.br_blockcount;
  1018. break;
  1019. case 0:
  1020. /*
  1021. * Filling in the middle part of a previous delayed allocation.
  1022. * Contiguity is impossible here.
  1023. * This case is avoided almost all the time.
  1024. */
  1025. temp = new->br_startoff - PREV.br_startoff;
  1026. trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
  1027. xfs_bmbt_set_blockcount(ep, temp);
  1028. r[0] = *new;
  1029. r[1].br_state = PREV.br_state;
  1030. r[1].br_startblock = 0;
  1031. r[1].br_startoff = new_endoff;
  1032. temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1033. r[1].br_blockcount = temp2;
  1034. xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
  1035. ip->i_df.if_lastex = idx + 1;
  1036. ip->i_d.di_nextents++;
  1037. if (cur == NULL)
  1038. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1039. else {
  1040. rval = XFS_ILOG_CORE;
  1041. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1042. new->br_startblock, new->br_blockcount,
  1043. &i)))
  1044. goto done;
  1045. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1046. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1047. if ((error = xfs_btree_insert(cur, &i)))
  1048. goto done;
  1049. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1050. }
  1051. if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
  1052. ip->i_d.di_nextents > ip->i_df.if_ext_max) {
  1053. error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
  1054. first, flist, &cur, 1, &tmp_rval,
  1055. XFS_DATA_FORK);
  1056. rval |= tmp_rval;
  1057. if (error)
  1058. goto done;
  1059. }
  1060. temp = xfs_bmap_worst_indlen(ip, temp);
  1061. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  1062. diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
  1063. (cur ? cur->bc_private.b.allocated : 0));
  1064. if (diff > 0 &&
  1065. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
  1066. /*
  1067. * Ick gross gag me with a spoon.
  1068. */
  1069. ASSERT(0); /* want to see if this ever happens! */
  1070. while (diff > 0) {
  1071. if (temp) {
  1072. temp--;
  1073. diff--;
  1074. if (!diff ||
  1075. !xfs_mod_incore_sb(ip->i_mount,
  1076. XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
  1077. break;
  1078. }
  1079. if (temp2) {
  1080. temp2--;
  1081. diff--;
  1082. if (!diff ||
  1083. !xfs_mod_incore_sb(ip->i_mount,
  1084. XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. ep = xfs_iext_get_ext(ifp, idx);
  1090. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  1091. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1092. trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
  1093. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
  1094. nullstartblock((int)temp2));
  1095. trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
  1096. *dnew = temp + temp2;
  1097. /* DELTA: One in-core extent is split in three. */
  1098. temp = PREV.br_startoff;
  1099. temp2 = PREV.br_blockcount;
  1100. break;
  1101. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1102. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1103. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  1104. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1105. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1106. case BMAP_LEFT_CONTIG:
  1107. case BMAP_RIGHT_CONTIG:
  1108. /*
  1109. * These cases are all impossible.
  1110. */
  1111. ASSERT(0);
  1112. }
  1113. *curp = cur;
  1114. if (delta) {
  1115. temp2 += temp;
  1116. if (delta->xed_startoff > temp)
  1117. delta->xed_startoff = temp;
  1118. if (delta->xed_blockcount < temp2)
  1119. delta->xed_blockcount = temp2;
  1120. }
  1121. done:
  1122. *logflagsp = rval;
  1123. return error;
  1124. #undef LEFT
  1125. #undef RIGHT
  1126. #undef PREV
  1127. }
  1128. /*
  1129. * Called by xfs_bmap_add_extent to handle cases converting an unwritten
  1130. * allocation to a real allocation or vice versa.
  1131. */
  1132. STATIC int /* error */
  1133. xfs_bmap_add_extent_unwritten_real(
  1134. xfs_inode_t *ip, /* incore inode pointer */
  1135. xfs_extnum_t idx, /* extent number to update/insert */
  1136. xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
  1137. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1138. int *logflagsp, /* inode logging flags */
  1139. xfs_extdelta_t *delta) /* Change made to incore extents */
  1140. {
  1141. xfs_btree_cur_t *cur; /* btree cursor */
  1142. xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
  1143. int error; /* error return value */
  1144. int i; /* temp state */
  1145. xfs_ifork_t *ifp; /* inode fork pointer */
  1146. xfs_fileoff_t new_endoff; /* end offset of new entry */
  1147. xfs_exntst_t newext; /* new extent state */
  1148. xfs_exntst_t oldext; /* old extent state */
  1149. xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
  1150. /* left is 0, right is 1, prev is 2 */
  1151. int rval=0; /* return value (logging flags) */
  1152. int state = 0;/* state bits, accessed thru macros */
  1153. xfs_filblks_t temp=0;
  1154. xfs_filblks_t temp2=0;
  1155. #define LEFT r[0]
  1156. #define RIGHT r[1]
  1157. #define PREV r[2]
  1158. /*
  1159. * Set up a bunch of variables to make the tests simpler.
  1160. */
  1161. error = 0;
  1162. cur = *curp;
  1163. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1164. ep = xfs_iext_get_ext(ifp, idx);
  1165. xfs_bmbt_get_all(ep, &PREV);
  1166. newext = new->br_state;
  1167. oldext = (newext == XFS_EXT_UNWRITTEN) ?
  1168. XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
  1169. ASSERT(PREV.br_state == oldext);
  1170. new_endoff = new->br_startoff + new->br_blockcount;
  1171. ASSERT(PREV.br_startoff <= new->br_startoff);
  1172. ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
  1173. /*
  1174. * Set flags determining what part of the previous oldext allocation
  1175. * extent is being replaced by a newext allocation.
  1176. */
  1177. if (PREV.br_startoff == new->br_startoff)
  1178. state |= BMAP_LEFT_FILLING;
  1179. if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
  1180. state |= BMAP_RIGHT_FILLING;
  1181. /*
  1182. * Check and set flags if this segment has a left neighbor.
  1183. * Don't set contiguous if the combined extent would be too large.
  1184. */
  1185. if (idx > 0) {
  1186. state |= BMAP_LEFT_VALID;
  1187. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
  1188. if (isnullstartblock(LEFT.br_startblock))
  1189. state |= BMAP_LEFT_DELAY;
  1190. }
  1191. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  1192. LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
  1193. LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
  1194. LEFT.br_state == newext &&
  1195. LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1196. state |= BMAP_LEFT_CONTIG;
  1197. /*
  1198. * Check and set flags if this segment has a right neighbor.
  1199. * Don't set contiguous if the combined extent would be too large.
  1200. * Also check for all-three-contiguous being too large.
  1201. */
  1202. if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
  1203. state |= BMAP_RIGHT_VALID;
  1204. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
  1205. if (isnullstartblock(RIGHT.br_startblock))
  1206. state |= BMAP_RIGHT_DELAY;
  1207. }
  1208. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  1209. new_endoff == RIGHT.br_startoff &&
  1210. new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
  1211. newext == RIGHT.br_state &&
  1212. new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
  1213. ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1214. BMAP_RIGHT_FILLING)) !=
  1215. (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
  1216. BMAP_RIGHT_FILLING) ||
  1217. LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
  1218. <= MAXEXTLEN))
  1219. state |= BMAP_RIGHT_CONTIG;
  1220. /*
  1221. * Switch out based on the FILLING and CONTIG state bits.
  1222. */
  1223. switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1224. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
  1225. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
  1226. BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1227. /*
  1228. * Setting all of a previous oldext extent to newext.
  1229. * The left and right neighbors are both contiguous with new.
  1230. */
  1231. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1232. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1233. LEFT.br_blockcount + PREV.br_blockcount +
  1234. RIGHT.br_blockcount);
  1235. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1236. xfs_iext_remove(ip, idx, 2, state);
  1237. ip->i_df.if_lastex = idx - 1;
  1238. ip->i_d.di_nextents -= 2;
  1239. if (cur == NULL)
  1240. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1241. else {
  1242. rval = XFS_ILOG_CORE;
  1243. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1244. RIGHT.br_startblock,
  1245. RIGHT.br_blockcount, &i)))
  1246. goto done;
  1247. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1248. if ((error = xfs_btree_delete(cur, &i)))
  1249. goto done;
  1250. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1251. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1252. goto done;
  1253. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1254. if ((error = xfs_btree_delete(cur, &i)))
  1255. goto done;
  1256. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1257. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1258. goto done;
  1259. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1260. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1261. LEFT.br_startblock,
  1262. LEFT.br_blockcount + PREV.br_blockcount +
  1263. RIGHT.br_blockcount, LEFT.br_state)))
  1264. goto done;
  1265. }
  1266. /* DELTA: Three in-core extents are replaced by one. */
  1267. temp = LEFT.br_startoff;
  1268. temp2 = LEFT.br_blockcount +
  1269. PREV.br_blockcount +
  1270. RIGHT.br_blockcount;
  1271. break;
  1272. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1273. /*
  1274. * Setting all of a previous oldext extent to newext.
  1275. * The left neighbor is contiguous, the right is not.
  1276. */
  1277. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1278. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1279. LEFT.br_blockcount + PREV.br_blockcount);
  1280. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1281. ip->i_df.if_lastex = idx - 1;
  1282. xfs_iext_remove(ip, idx, 1, state);
  1283. ip->i_d.di_nextents--;
  1284. if (cur == NULL)
  1285. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1286. else {
  1287. rval = XFS_ILOG_CORE;
  1288. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1289. PREV.br_startblock, PREV.br_blockcount,
  1290. &i)))
  1291. goto done;
  1292. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1293. if ((error = xfs_btree_delete(cur, &i)))
  1294. goto done;
  1295. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1296. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1297. goto done;
  1298. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1299. if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
  1300. LEFT.br_startblock,
  1301. LEFT.br_blockcount + PREV.br_blockcount,
  1302. LEFT.br_state)))
  1303. goto done;
  1304. }
  1305. /* DELTA: Two in-core extents are replaced by one. */
  1306. temp = LEFT.br_startoff;
  1307. temp2 = LEFT.br_blockcount +
  1308. PREV.br_blockcount;
  1309. break;
  1310. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1311. /*
  1312. * Setting all of a previous oldext extent to newext.
  1313. * The right neighbor is contiguous, the left is not.
  1314. */
  1315. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1316. xfs_bmbt_set_blockcount(ep,
  1317. PREV.br_blockcount + RIGHT.br_blockcount);
  1318. xfs_bmbt_set_state(ep, newext);
  1319. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1320. ip->i_df.if_lastex = idx;
  1321. xfs_iext_remove(ip, idx + 1, 1, state);
  1322. ip->i_d.di_nextents--;
  1323. if (cur == NULL)
  1324. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1325. else {
  1326. rval = XFS_ILOG_CORE;
  1327. if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
  1328. RIGHT.br_startblock,
  1329. RIGHT.br_blockcount, &i)))
  1330. goto done;
  1331. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1332. if ((error = xfs_btree_delete(cur, &i)))
  1333. goto done;
  1334. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1335. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1336. goto done;
  1337. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1338. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1339. new->br_startblock,
  1340. new->br_blockcount + RIGHT.br_blockcount,
  1341. newext)))
  1342. goto done;
  1343. }
  1344. /* DELTA: Two in-core extents are replaced by one. */
  1345. temp = PREV.br_startoff;
  1346. temp2 = PREV.br_blockcount +
  1347. RIGHT.br_blockcount;
  1348. break;
  1349. case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
  1350. /*
  1351. * Setting all of a previous oldext extent to newext.
  1352. * Neither the left nor right neighbors are contiguous with
  1353. * the new one.
  1354. */
  1355. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1356. xfs_bmbt_set_state(ep, newext);
  1357. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1358. ip->i_df.if_lastex = idx;
  1359. if (cur == NULL)
  1360. rval = XFS_ILOG_DEXT;
  1361. else {
  1362. rval = 0;
  1363. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1364. new->br_startblock, new->br_blockcount,
  1365. &i)))
  1366. goto done;
  1367. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1368. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1369. new->br_startblock, new->br_blockcount,
  1370. newext)))
  1371. goto done;
  1372. }
  1373. /* DELTA: The in-core extent described by new changed type. */
  1374. temp = new->br_startoff;
  1375. temp2 = new->br_blockcount;
  1376. break;
  1377. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
  1378. /*
  1379. * Setting the first part of a previous oldext extent to newext.
  1380. * The left neighbor is contiguous.
  1381. */
  1382. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1383. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1384. LEFT.br_blockcount + new->br_blockcount);
  1385. xfs_bmbt_set_startoff(ep,
  1386. PREV.br_startoff + new->br_blockcount);
  1387. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1388. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1389. xfs_bmbt_set_startblock(ep,
  1390. new->br_startblock + new->br_blockcount);
  1391. xfs_bmbt_set_blockcount(ep,
  1392. PREV.br_blockcount - new->br_blockcount);
  1393. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1394. ip->i_df.if_lastex = idx - 1;
  1395. if (cur == NULL)
  1396. rval = XFS_ILOG_DEXT;
  1397. else {
  1398. rval = 0;
  1399. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1400. PREV.br_startblock, PREV.br_blockcount,
  1401. &i)))
  1402. goto done;
  1403. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1404. if ((error = xfs_bmbt_update(cur,
  1405. PREV.br_startoff + new->br_blockcount,
  1406. PREV.br_startblock + new->br_blockcount,
  1407. PREV.br_blockcount - new->br_blockcount,
  1408. oldext)))
  1409. goto done;
  1410. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1411. goto done;
  1412. if (xfs_bmbt_update(cur, LEFT.br_startoff,
  1413. LEFT.br_startblock,
  1414. LEFT.br_blockcount + new->br_blockcount,
  1415. LEFT.br_state))
  1416. goto done;
  1417. }
  1418. /* DELTA: The boundary between two in-core extents moved. */
  1419. temp = LEFT.br_startoff;
  1420. temp2 = LEFT.br_blockcount +
  1421. PREV.br_blockcount;
  1422. break;
  1423. case BMAP_LEFT_FILLING:
  1424. /*
  1425. * Setting the first part of a previous oldext extent to newext.
  1426. * The left neighbor is not contiguous.
  1427. */
  1428. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1429. ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
  1430. xfs_bmbt_set_startoff(ep, new_endoff);
  1431. xfs_bmbt_set_blockcount(ep,
  1432. PREV.br_blockcount - new->br_blockcount);
  1433. xfs_bmbt_set_startblock(ep,
  1434. new->br_startblock + new->br_blockcount);
  1435. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1436. xfs_iext_insert(ip, idx, 1, new, state);
  1437. ip->i_df.if_lastex = idx;
  1438. ip->i_d.di_nextents++;
  1439. if (cur == NULL)
  1440. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1441. else {
  1442. rval = XFS_ILOG_CORE;
  1443. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1444. PREV.br_startblock, PREV.br_blockcount,
  1445. &i)))
  1446. goto done;
  1447. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1448. if ((error = xfs_bmbt_update(cur,
  1449. PREV.br_startoff + new->br_blockcount,
  1450. PREV.br_startblock + new->br_blockcount,
  1451. PREV.br_blockcount - new->br_blockcount,
  1452. oldext)))
  1453. goto done;
  1454. cur->bc_rec.b = *new;
  1455. if ((error = xfs_btree_insert(cur, &i)))
  1456. goto done;
  1457. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1458. }
  1459. /* DELTA: One in-core extent is split in two. */
  1460. temp = PREV.br_startoff;
  1461. temp2 = PREV.br_blockcount;
  1462. break;
  1463. case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
  1464. /*
  1465. * Setting the last part of a previous oldext extent to newext.
  1466. * The right neighbor is contiguous with the new allocation.
  1467. */
  1468. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1469. trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
  1470. xfs_bmbt_set_blockcount(ep,
  1471. PREV.br_blockcount - new->br_blockcount);
  1472. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1473. xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
  1474. new->br_startoff, new->br_startblock,
  1475. new->br_blockcount + RIGHT.br_blockcount, newext);
  1476. trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
  1477. ip->i_df.if_lastex = idx + 1;
  1478. if (cur == NULL)
  1479. rval = XFS_ILOG_DEXT;
  1480. else {
  1481. rval = 0;
  1482. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1483. PREV.br_startblock,
  1484. PREV.br_blockcount, &i)))
  1485. goto done;
  1486. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1487. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1488. PREV.br_startblock,
  1489. PREV.br_blockcount - new->br_blockcount,
  1490. oldext)))
  1491. goto done;
  1492. if ((error = xfs_btree_increment(cur, 0, &i)))
  1493. goto done;
  1494. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1495. new->br_startblock,
  1496. new->br_blockcount + RIGHT.br_blockcount,
  1497. newext)))
  1498. goto done;
  1499. }
  1500. /* DELTA: The boundary between two in-core extents moved. */
  1501. temp = PREV.br_startoff;
  1502. temp2 = PREV.br_blockcount +
  1503. RIGHT.br_blockcount;
  1504. break;
  1505. case BMAP_RIGHT_FILLING:
  1506. /*
  1507. * Setting the last part of a previous oldext extent to newext.
  1508. * The right neighbor is not contiguous.
  1509. */
  1510. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1511. xfs_bmbt_set_blockcount(ep,
  1512. PREV.br_blockcount - new->br_blockcount);
  1513. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1514. xfs_iext_insert(ip, idx + 1, 1, new, state);
  1515. ip->i_df.if_lastex = idx + 1;
  1516. ip->i_d.di_nextents++;
  1517. if (cur == NULL)
  1518. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1519. else {
  1520. rval = XFS_ILOG_CORE;
  1521. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1522. PREV.br_startblock, PREV.br_blockcount,
  1523. &i)))
  1524. goto done;
  1525. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1526. if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
  1527. PREV.br_startblock,
  1528. PREV.br_blockcount - new->br_blockcount,
  1529. oldext)))
  1530. goto done;
  1531. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1532. new->br_startblock, new->br_blockcount,
  1533. &i)))
  1534. goto done;
  1535. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1536. cur->bc_rec.b.br_state = XFS_EXT_NORM;
  1537. if ((error = xfs_btree_insert(cur, &i)))
  1538. goto done;
  1539. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1540. }
  1541. /* DELTA: One in-core extent is split in two. */
  1542. temp = PREV.br_startoff;
  1543. temp2 = PREV.br_blockcount;
  1544. break;
  1545. case 0:
  1546. /*
  1547. * Setting the middle part of a previous oldext extent to
  1548. * newext. Contiguity is impossible here.
  1549. * One extent becomes three extents.
  1550. */
  1551. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1552. xfs_bmbt_set_blockcount(ep,
  1553. new->br_startoff - PREV.br_startoff);
  1554. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1555. r[0] = *new;
  1556. r[1].br_startoff = new_endoff;
  1557. r[1].br_blockcount =
  1558. PREV.br_startoff + PREV.br_blockcount - new_endoff;
  1559. r[1].br_startblock = new->br_startblock + new->br_blockcount;
  1560. r[1].br_state = oldext;
  1561. xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
  1562. ip->i_df.if_lastex = idx + 1;
  1563. ip->i_d.di_nextents += 2;
  1564. if (cur == NULL)
  1565. rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
  1566. else {
  1567. rval = XFS_ILOG_CORE;
  1568. if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
  1569. PREV.br_startblock, PREV.br_blockcount,
  1570. &i)))
  1571. goto done;
  1572. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1573. /* new right extent - oldext */
  1574. if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
  1575. r[1].br_startblock, r[1].br_blockcount,
  1576. r[1].br_state)))
  1577. goto done;
  1578. /* new left extent - oldext */
  1579. cur->bc_rec.b = PREV;
  1580. cur->bc_rec.b.br_blockcount =
  1581. new->br_startoff - PREV.br_startoff;
  1582. if ((error = xfs_btree_insert(cur, &i)))
  1583. goto done;
  1584. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1585. /*
  1586. * Reset the cursor to the position of the new extent
  1587. * we are about to insert as we can't trust it after
  1588. * the previous insert.
  1589. */
  1590. if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
  1591. new->br_startblock, new->br_blockcount,
  1592. &i)))
  1593. goto done;
  1594. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1595. /* new middle extent - newext */
  1596. cur->bc_rec.b.br_state = new->br_state;
  1597. if ((error = xfs_btree_insert(cur, &i)))
  1598. goto done;
  1599. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1600. }
  1601. /* DELTA: One in-core extent is split in three. */
  1602. temp = PREV.br_startoff;
  1603. temp2 = PREV.br_blockcount;
  1604. break;
  1605. case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1606. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1607. case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
  1608. case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
  1609. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1610. case BMAP_LEFT_CONTIG:
  1611. case BMAP_RIGHT_CONTIG:
  1612. /*
  1613. * These cases are all impossible.
  1614. */
  1615. ASSERT(0);
  1616. }
  1617. *curp = cur;
  1618. if (delta) {
  1619. temp2 += temp;
  1620. if (delta->xed_startoff > temp)
  1621. delta->xed_startoff = temp;
  1622. if (delta->xed_blockcount < temp2)
  1623. delta->xed_blockcount = temp2;
  1624. }
  1625. done:
  1626. *logflagsp = rval;
  1627. return error;
  1628. #undef LEFT
  1629. #undef RIGHT
  1630. #undef PREV
  1631. }
  1632. /*
  1633. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1634. * to a delayed allocation.
  1635. */
  1636. /*ARGSUSED*/
  1637. STATIC int /* error */
  1638. xfs_bmap_add_extent_hole_delay(
  1639. xfs_inode_t *ip, /* incore inode pointer */
  1640. xfs_extnum_t idx, /* extent number to update/insert */
  1641. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1642. int *logflagsp, /* inode logging flags */
  1643. xfs_extdelta_t *delta, /* Change made to incore extents */
  1644. int rsvd) /* OK to allocate reserved blocks */
  1645. {
  1646. xfs_bmbt_rec_host_t *ep; /* extent record for idx */
  1647. xfs_ifork_t *ifp; /* inode fork pointer */
  1648. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1649. xfs_filblks_t newlen=0; /* new indirect size */
  1650. xfs_filblks_t oldlen=0; /* old indirect size */
  1651. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1652. int state; /* state bits, accessed thru macros */
  1653. xfs_filblks_t temp=0; /* temp for indirect calculations */
  1654. xfs_filblks_t temp2=0;
  1655. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  1656. ep = xfs_iext_get_ext(ifp, idx);
  1657. state = 0;
  1658. ASSERT(isnullstartblock(new->br_startblock));
  1659. /*
  1660. * Check and set flags if this segment has a left neighbor
  1661. */
  1662. if (idx > 0) {
  1663. state |= BMAP_LEFT_VALID;
  1664. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
  1665. if (isnullstartblock(left.br_startblock))
  1666. state |= BMAP_LEFT_DELAY;
  1667. }
  1668. /*
  1669. * Check and set flags if the current (right) segment exists.
  1670. * If it doesn't exist, we're converting the hole at end-of-file.
  1671. */
  1672. if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
  1673. state |= BMAP_RIGHT_VALID;
  1674. xfs_bmbt_get_all(ep, &right);
  1675. if (isnullstartblock(right.br_startblock))
  1676. state |= BMAP_RIGHT_DELAY;
  1677. }
  1678. /*
  1679. * Set contiguity flags on the left and right neighbors.
  1680. * Don't let extents get too large, even if the pieces are contiguous.
  1681. */
  1682. if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
  1683. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1684. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1685. state |= BMAP_LEFT_CONTIG;
  1686. if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
  1687. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1688. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1689. (!(state & BMAP_LEFT_CONTIG) ||
  1690. (left.br_blockcount + new->br_blockcount +
  1691. right.br_blockcount <= MAXEXTLEN)))
  1692. state |= BMAP_RIGHT_CONTIG;
  1693. /*
  1694. * Switch out based on the contiguity flags.
  1695. */
  1696. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  1697. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1698. /*
  1699. * New allocation is contiguous with delayed allocations
  1700. * on the left and on the right.
  1701. * Merge all three into a single extent record.
  1702. */
  1703. temp = left.br_blockcount + new->br_blockcount +
  1704. right.br_blockcount;
  1705. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1706. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
  1707. oldlen = startblockval(left.br_startblock) +
  1708. startblockval(new->br_startblock) +
  1709. startblockval(right.br_startblock);
  1710. newlen = xfs_bmap_worst_indlen(ip, temp);
  1711. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
  1712. nullstartblock((int)newlen));
  1713. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1714. xfs_iext_remove(ip, idx, 1, state);
  1715. ip->i_df.if_lastex = idx - 1;
  1716. /* DELTA: Two in-core extents were replaced by one. */
  1717. temp2 = temp;
  1718. temp = left.br_startoff;
  1719. break;
  1720. case BMAP_LEFT_CONTIG:
  1721. /*
  1722. * New allocation is contiguous with a delayed allocation
  1723. * on the left.
  1724. * Merge the new allocation with the left neighbor.
  1725. */
  1726. temp = left.br_blockcount + new->br_blockcount;
  1727. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1728. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
  1729. oldlen = startblockval(left.br_startblock) +
  1730. startblockval(new->br_startblock);
  1731. newlen = xfs_bmap_worst_indlen(ip, temp);
  1732. xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
  1733. nullstartblock((int)newlen));
  1734. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1735. ip->i_df.if_lastex = idx - 1;
  1736. /* DELTA: One in-core extent grew into a hole. */
  1737. temp2 = temp;
  1738. temp = left.br_startoff;
  1739. break;
  1740. case BMAP_RIGHT_CONTIG:
  1741. /*
  1742. * New allocation is contiguous with a delayed allocation
  1743. * on the right.
  1744. * Merge the new allocation with the right neighbor.
  1745. */
  1746. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1747. temp = new->br_blockcount + right.br_blockcount;
  1748. oldlen = startblockval(new->br_startblock) +
  1749. startblockval(right.br_startblock);
  1750. newlen = xfs_bmap_worst_indlen(ip, temp);
  1751. xfs_bmbt_set_allf(ep, new->br_startoff,
  1752. nullstartblock((int)newlen), temp, right.br_state);
  1753. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1754. ip->i_df.if_lastex = idx;
  1755. /* DELTA: One in-core extent grew into a hole. */
  1756. temp2 = temp;
  1757. temp = new->br_startoff;
  1758. break;
  1759. case 0:
  1760. /*
  1761. * New allocation is not contiguous with another
  1762. * delayed allocation.
  1763. * Insert a new entry.
  1764. */
  1765. oldlen = newlen = 0;
  1766. xfs_iext_insert(ip, idx, 1, new, state);
  1767. ip->i_df.if_lastex = idx;
  1768. /* DELTA: A new in-core extent was added in a hole. */
  1769. temp2 = new->br_blockcount;
  1770. temp = new->br_startoff;
  1771. break;
  1772. }
  1773. if (oldlen != newlen) {
  1774. ASSERT(oldlen > newlen);
  1775. xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
  1776. (int64_t)(oldlen - newlen), rsvd);
  1777. /*
  1778. * Nothing to do for disk quota accounting here.
  1779. */
  1780. }
  1781. if (delta) {
  1782. temp2 += temp;
  1783. if (delta->xed_startoff > temp)
  1784. delta->xed_startoff = temp;
  1785. if (delta->xed_blockcount < temp2)
  1786. delta->xed_blockcount = temp2;
  1787. }
  1788. *logflagsp = 0;
  1789. return 0;
  1790. }
  1791. /*
  1792. * Called by xfs_bmap_add_extent to handle cases converting a hole
  1793. * to a real allocation.
  1794. */
  1795. STATIC int /* error */
  1796. xfs_bmap_add_extent_hole_real(
  1797. xfs_inode_t *ip, /* incore inode pointer */
  1798. xfs_extnum_t idx, /* extent number to update/insert */
  1799. xfs_btree_cur_t *cur, /* if null, not a btree */
  1800. xfs_bmbt_irec_t *new, /* new data to add to file extents */
  1801. int *logflagsp, /* inode logging flags */
  1802. xfs_extdelta_t *delta, /* Change made to incore extents */
  1803. int whichfork) /* data or attr fork */
  1804. {
  1805. xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
  1806. int error; /* error return value */
  1807. int i; /* temp state */
  1808. xfs_ifork_t *ifp; /* inode fork pointer */
  1809. xfs_bmbt_irec_t left; /* left neighbor extent entry */
  1810. xfs_bmbt_irec_t right; /* right neighbor extent entry */
  1811. int rval=0; /* return value (logging flags) */
  1812. int state; /* state bits, accessed thru macros */
  1813. xfs_filblks_t temp=0;
  1814. xfs_filblks_t temp2=0;
  1815. ifp = XFS_IFORK_PTR(ip, whichfork);
  1816. ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
  1817. ep = xfs_iext_get_ext(ifp, idx);
  1818. state = 0;
  1819. if (whichfork == XFS_ATTR_FORK)
  1820. state |= BMAP_ATTRFORK;
  1821. /*
  1822. * Check and set flags if this segment has a left neighbor.
  1823. */
  1824. if (idx > 0) {
  1825. state |= BMAP_LEFT_VALID;
  1826. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
  1827. if (isnullstartblock(left.br_startblock))
  1828. state |= BMAP_LEFT_DELAY;
  1829. }
  1830. /*
  1831. * Check and set flags if this segment has a current value.
  1832. * Not true if we're inserting into the "hole" at eof.
  1833. */
  1834. if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
  1835. state |= BMAP_RIGHT_VALID;
  1836. xfs_bmbt_get_all(ep, &right);
  1837. if (isnullstartblock(right.br_startblock))
  1838. state |= BMAP_RIGHT_DELAY;
  1839. }
  1840. /*
  1841. * We're inserting a real allocation between "left" and "right".
  1842. * Set the contiguity flags. Don't let extents get too large.
  1843. */
  1844. if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
  1845. left.br_startoff + left.br_blockcount == new->br_startoff &&
  1846. left.br_startblock + left.br_blockcount == new->br_startblock &&
  1847. left.br_state == new->br_state &&
  1848. left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
  1849. state |= BMAP_LEFT_CONTIG;
  1850. if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
  1851. new->br_startoff + new->br_blockcount == right.br_startoff &&
  1852. new->br_startblock + new->br_blockcount == right.br_startblock &&
  1853. new->br_state == right.br_state &&
  1854. new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
  1855. (!(state & BMAP_LEFT_CONTIG) ||
  1856. left.br_blockcount + new->br_blockcount +
  1857. right.br_blockcount <= MAXEXTLEN))
  1858. state |= BMAP_RIGHT_CONTIG;
  1859. error = 0;
  1860. /*
  1861. * Select which case we're in here, and implement it.
  1862. */
  1863. switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
  1864. case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
  1865. /*
  1866. * New allocation is contiguous with real allocations on the
  1867. * left and on the right.
  1868. * Merge all three into a single extent record.
  1869. */
  1870. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1871. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1872. left.br_blockcount + new->br_blockcount +
  1873. right.br_blockcount);
  1874. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1875. xfs_iext_remove(ip, idx, 1, state);
  1876. ifp->if_lastex = idx - 1;
  1877. XFS_IFORK_NEXT_SET(ip, whichfork,
  1878. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  1879. if (cur == NULL) {
  1880. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  1881. } else {
  1882. rval = XFS_ILOG_CORE;
  1883. if ((error = xfs_bmbt_lookup_eq(cur,
  1884. right.br_startoff,
  1885. right.br_startblock,
  1886. right.br_blockcount, &i)))
  1887. goto done;
  1888. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1889. if ((error = xfs_btree_delete(cur, &i)))
  1890. goto done;
  1891. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1892. if ((error = xfs_btree_decrement(cur, 0, &i)))
  1893. goto done;
  1894. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1895. if ((error = xfs_bmbt_update(cur, left.br_startoff,
  1896. left.br_startblock,
  1897. left.br_blockcount +
  1898. new->br_blockcount +
  1899. right.br_blockcount,
  1900. left.br_state)))
  1901. goto done;
  1902. }
  1903. /* DELTA: Two in-core extents were replaced by one. */
  1904. temp = left.br_startoff;
  1905. temp2 = left.br_blockcount +
  1906. new->br_blockcount +
  1907. right.br_blockcount;
  1908. break;
  1909. case BMAP_LEFT_CONTIG:
  1910. /*
  1911. * New allocation is contiguous with a real allocation
  1912. * on the left.
  1913. * Merge the new allocation with the left neighbor.
  1914. */
  1915. trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
  1916. xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
  1917. left.br_blockcount + new->br_blockcount);
  1918. trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
  1919. ifp->if_lastex = idx - 1;
  1920. if (cur == NULL) {
  1921. rval = xfs_ilog_fext(whichfork);
  1922. } else {
  1923. rval = 0;
  1924. if ((error = xfs_bmbt_lookup_eq(cur,
  1925. left.br_startoff,
  1926. left.br_startblock,
  1927. left.br_blockcount, &i)))
  1928. goto done;
  1929. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1930. if ((error = xfs_bmbt_update(cur, left.br_startoff,
  1931. left.br_startblock,
  1932. left.br_blockcount +
  1933. new->br_blockcount,
  1934. left.br_state)))
  1935. goto done;
  1936. }
  1937. /* DELTA: One in-core extent grew. */
  1938. temp = left.br_startoff;
  1939. temp2 = left.br_blockcount +
  1940. new->br_blockcount;
  1941. break;
  1942. case BMAP_RIGHT_CONTIG:
  1943. /*
  1944. * New allocation is contiguous with a real allocation
  1945. * on the right.
  1946. * Merge the new allocation with the right neighbor.
  1947. */
  1948. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  1949. xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
  1950. new->br_blockcount + right.br_blockcount,
  1951. right.br_state);
  1952. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  1953. ifp->if_lastex = idx;
  1954. if (cur == NULL) {
  1955. rval = xfs_ilog_fext(whichfork);
  1956. } else {
  1957. rval = 0;
  1958. if ((error = xfs_bmbt_lookup_eq(cur,
  1959. right.br_startoff,
  1960. right.br_startblock,
  1961. right.br_blockcount, &i)))
  1962. goto done;
  1963. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  1964. if ((error = xfs_bmbt_update(cur, new->br_startoff,
  1965. new->br_startblock,
  1966. new->br_blockcount +
  1967. right.br_blockcount,
  1968. right.br_state)))
  1969. goto done;
  1970. }
  1971. /* DELTA: One in-core extent grew. */
  1972. temp = new->br_startoff;
  1973. temp2 = new->br_blockcount +
  1974. right.br_blockcount;
  1975. break;
  1976. case 0:
  1977. /*
  1978. * New allocation is not contiguous with another
  1979. * real allocation.
  1980. * Insert a new entry.
  1981. */
  1982. xfs_iext_insert(ip, idx, 1, new, state);
  1983. ifp->if_lastex = idx;
  1984. XFS_IFORK_NEXT_SET(ip, whichfork,
  1985. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  1986. if (cur == NULL) {
  1987. rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  1988. } else {
  1989. rval = XFS_ILOG_CORE;
  1990. if ((error = xfs_bmbt_lookup_eq(cur,
  1991. new->br_startoff,
  1992. new->br_startblock,
  1993. new->br_blockcount, &i)))
  1994. goto done;
  1995. XFS_WANT_CORRUPTED_GOTO(i == 0, done);
  1996. cur->bc_rec.b.br_state = new->br_state;
  1997. if ((error = xfs_btree_insert(cur, &i)))
  1998. goto done;
  1999. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2000. }
  2001. /* DELTA: A new extent was added in a hole. */
  2002. temp = new->br_startoff;
  2003. temp2 = new->br_blockcount;
  2004. break;
  2005. }
  2006. if (delta) {
  2007. temp2 += temp;
  2008. if (delta->xed_startoff > temp)
  2009. delta->xed_startoff = temp;
  2010. if (delta->xed_blockcount < temp2)
  2011. delta->xed_blockcount = temp2;
  2012. }
  2013. done:
  2014. *logflagsp = rval;
  2015. return error;
  2016. }
  2017. /*
  2018. * Adjust the size of the new extent based on di_extsize and rt extsize.
  2019. */
  2020. STATIC int
  2021. xfs_bmap_extsize_align(
  2022. xfs_mount_t *mp,
  2023. xfs_bmbt_irec_t *gotp, /* next extent pointer */
  2024. xfs_bmbt_irec_t *prevp, /* previous extent pointer */
  2025. xfs_extlen_t extsz, /* align to this extent size */
  2026. int rt, /* is this a realtime inode? */
  2027. int eof, /* is extent at end-of-file? */
  2028. int delay, /* creating delalloc extent? */
  2029. int convert, /* overwriting unwritten extent? */
  2030. xfs_fileoff_t *offp, /* in/out: aligned offset */
  2031. xfs_extlen_t *lenp) /* in/out: aligned length */
  2032. {
  2033. xfs_fileoff_t orig_off; /* original offset */
  2034. xfs_extlen_t orig_alen; /* original length */
  2035. xfs_fileoff_t orig_end; /* original off+len */
  2036. xfs_fileoff_t nexto; /* next file offset */
  2037. xfs_fileoff_t prevo; /* previous file offset */
  2038. xfs_fileoff_t align_off; /* temp for offset */
  2039. xfs_extlen_t align_alen; /* temp for length */
  2040. xfs_extlen_t temp; /* temp for calculations */
  2041. if (convert)
  2042. return 0;
  2043. orig_off = align_off = *offp;
  2044. orig_alen = align_alen = *lenp;
  2045. orig_end = orig_off + orig_alen;
  2046. /*
  2047. * If this request overlaps an existing extent, then don't
  2048. * attempt to perform any additional alignment.
  2049. */
  2050. if (!delay && !eof &&
  2051. (orig_off >= gotp->br_startoff) &&
  2052. (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
  2053. return 0;
  2054. }
  2055. /*
  2056. * If the file offset is unaligned vs. the extent size
  2057. * we need to align it. This will be possible unless
  2058. * the file was previously written with a kernel that didn't
  2059. * perform this alignment, or if a truncate shot us in the
  2060. * foot.
  2061. */
  2062. temp = do_mod(orig_off, extsz);
  2063. if (temp) {
  2064. align_alen += temp;
  2065. align_off -= temp;
  2066. }
  2067. /*
  2068. * Same adjustment for the end of the requested area.
  2069. */
  2070. if ((temp = (align_alen % extsz))) {
  2071. align_alen += extsz - temp;
  2072. }
  2073. /*
  2074. * If the previous block overlaps with this proposed allocation
  2075. * then move the start forward without adjusting the length.
  2076. */
  2077. if (prevp->br_startoff != NULLFILEOFF) {
  2078. if (prevp->br_startblock == HOLESTARTBLOCK)
  2079. prevo = prevp->br_startoff;
  2080. else
  2081. prevo = prevp->br_startoff + prevp->br_blockcount;
  2082. } else
  2083. prevo = 0;
  2084. if (align_off != orig_off && align_off < prevo)
  2085. align_off = prevo;
  2086. /*
  2087. * If the next block overlaps with this proposed allocation
  2088. * then move the start back without adjusting the length,
  2089. * but not before offset 0.
  2090. * This may of course make the start overlap previous block,
  2091. * and if we hit the offset 0 limit then the next block
  2092. * can still overlap too.
  2093. */
  2094. if (!eof && gotp->br_startoff != NULLFILEOFF) {
  2095. if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
  2096. (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
  2097. nexto = gotp->br_startoff + gotp->br_blockcount;
  2098. else
  2099. nexto = gotp->br_startoff;
  2100. } else
  2101. nexto = NULLFILEOFF;
  2102. if (!eof &&
  2103. align_off + align_alen != orig_end &&
  2104. align_off + align_alen > nexto)
  2105. align_off = nexto > align_alen ? nexto - align_alen : 0;
  2106. /*
  2107. * If we're now overlapping the next or previous extent that
  2108. * means we can't fit an extsz piece in this hole. Just move
  2109. * the start forward to the first valid spot and set
  2110. * the length so we hit the end.
  2111. */
  2112. if (align_off != orig_off && align_off < prevo)
  2113. align_off = prevo;
  2114. if (align_off + align_alen != orig_end &&
  2115. align_off + align_alen > nexto &&
  2116. nexto != NULLFILEOFF) {
  2117. ASSERT(nexto > prevo);
  2118. align_alen = nexto - align_off;
  2119. }
  2120. /*
  2121. * If realtime, and the result isn't a multiple of the realtime
  2122. * extent size we need to remove blocks until it is.
  2123. */
  2124. if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
  2125. /*
  2126. * We're not covering the original request, or
  2127. * we won't be able to once we fix the length.
  2128. */
  2129. if (orig_off < align_off ||
  2130. orig_end > align_off + align_alen ||
  2131. align_alen - temp < orig_alen)
  2132. return XFS_ERROR(EINVAL);
  2133. /*
  2134. * Try to fix it by moving the start up.
  2135. */
  2136. if (align_off + temp <= orig_off) {
  2137. align_alen -= temp;
  2138. align_off += temp;
  2139. }
  2140. /*
  2141. * Try to fix it by moving the end in.
  2142. */
  2143. else if (align_off + align_alen - temp >= orig_end)
  2144. align_alen -= temp;
  2145. /*
  2146. * Set the start to the minimum then trim the length.
  2147. */
  2148. else {
  2149. align_alen -= orig_off - align_off;
  2150. align_off = orig_off;
  2151. align_alen -= align_alen % mp->m_sb.sb_rextsize;
  2152. }
  2153. /*
  2154. * Result doesn't cover the request, fail it.
  2155. */
  2156. if (orig_off < align_off || orig_end > align_off + align_alen)
  2157. return XFS_ERROR(EINVAL);
  2158. } else {
  2159. ASSERT(orig_off >= align_off);
  2160. ASSERT(orig_end <= align_off + align_alen);
  2161. }
  2162. #ifdef DEBUG
  2163. if (!eof && gotp->br_startoff != NULLFILEOFF)
  2164. ASSERT(align_off + align_alen <= gotp->br_startoff);
  2165. if (prevp->br_startoff != NULLFILEOFF)
  2166. ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
  2167. #endif
  2168. *lenp = align_alen;
  2169. *offp = align_off;
  2170. return 0;
  2171. }
  2172. #define XFS_ALLOC_GAP_UNITS 4
  2173. STATIC void
  2174. xfs_bmap_adjacent(
  2175. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2176. {
  2177. xfs_fsblock_t adjust; /* adjustment to block numbers */
  2178. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2179. xfs_mount_t *mp; /* mount point structure */
  2180. int nullfb; /* true if ap->firstblock isn't set */
  2181. int rt; /* true if inode is realtime */
  2182. #define ISVALID(x,y) \
  2183. (rt ? \
  2184. (x) < mp->m_sb.sb_rblocks : \
  2185. XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
  2186. XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
  2187. XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
  2188. mp = ap->ip->i_mount;
  2189. nullfb = ap->firstblock == NULLFSBLOCK;
  2190. rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
  2191. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2192. /*
  2193. * If allocating at eof, and there's a previous real block,
  2194. * try to use its last block as our starting point.
  2195. */
  2196. if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
  2197. !isnullstartblock(ap->prevp->br_startblock) &&
  2198. ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
  2199. ap->prevp->br_startblock)) {
  2200. ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
  2201. /*
  2202. * Adjust for the gap between prevp and us.
  2203. */
  2204. adjust = ap->off -
  2205. (ap->prevp->br_startoff + ap->prevp->br_blockcount);
  2206. if (adjust &&
  2207. ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
  2208. ap->rval += adjust;
  2209. }
  2210. /*
  2211. * If not at eof, then compare the two neighbor blocks.
  2212. * Figure out whether either one gives us a good starting point,
  2213. * and pick the better one.
  2214. */
  2215. else if (!ap->eof) {
  2216. xfs_fsblock_t gotbno; /* right side block number */
  2217. xfs_fsblock_t gotdiff=0; /* right side difference */
  2218. xfs_fsblock_t prevbno; /* left side block number */
  2219. xfs_fsblock_t prevdiff=0; /* left side difference */
  2220. /*
  2221. * If there's a previous (left) block, select a requested
  2222. * start block based on it.
  2223. */
  2224. if (ap->prevp->br_startoff != NULLFILEOFF &&
  2225. !isnullstartblock(ap->prevp->br_startblock) &&
  2226. (prevbno = ap->prevp->br_startblock +
  2227. ap->prevp->br_blockcount) &&
  2228. ISVALID(prevbno, ap->prevp->br_startblock)) {
  2229. /*
  2230. * Calculate gap to end of previous block.
  2231. */
  2232. adjust = prevdiff = ap->off -
  2233. (ap->prevp->br_startoff +
  2234. ap->prevp->br_blockcount);
  2235. /*
  2236. * Figure the startblock based on the previous block's
  2237. * end and the gap size.
  2238. * Heuristic!
  2239. * If the gap is large relative to the piece we're
  2240. * allocating, or using it gives us an invalid block
  2241. * number, then just use the end of the previous block.
  2242. */
  2243. if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2244. ISVALID(prevbno + prevdiff,
  2245. ap->prevp->br_startblock))
  2246. prevbno += adjust;
  2247. else
  2248. prevdiff += adjust;
  2249. /*
  2250. * If the firstblock forbids it, can't use it,
  2251. * must use default.
  2252. */
  2253. if (!rt && !nullfb &&
  2254. XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
  2255. prevbno = NULLFSBLOCK;
  2256. }
  2257. /*
  2258. * No previous block or can't follow it, just default.
  2259. */
  2260. else
  2261. prevbno = NULLFSBLOCK;
  2262. /*
  2263. * If there's a following (right) block, select a requested
  2264. * start block based on it.
  2265. */
  2266. if (!isnullstartblock(ap->gotp->br_startblock)) {
  2267. /*
  2268. * Calculate gap to start of next block.
  2269. */
  2270. adjust = gotdiff = ap->gotp->br_startoff - ap->off;
  2271. /*
  2272. * Figure the startblock based on the next block's
  2273. * start and the gap size.
  2274. */
  2275. gotbno = ap->gotp->br_startblock;
  2276. /*
  2277. * Heuristic!
  2278. * If the gap is large relative to the piece we're
  2279. * allocating, or using it gives us an invalid block
  2280. * number, then just use the start of the next block
  2281. * offset by our length.
  2282. */
  2283. if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
  2284. ISVALID(gotbno - gotdiff, gotbno))
  2285. gotbno -= adjust;
  2286. else if (ISVALID(gotbno - ap->alen, gotbno)) {
  2287. gotbno -= ap->alen;
  2288. gotdiff += adjust - ap->alen;
  2289. } else
  2290. gotdiff += adjust;
  2291. /*
  2292. * If the firstblock forbids it, can't use it,
  2293. * must use default.
  2294. */
  2295. if (!rt && !nullfb &&
  2296. XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
  2297. gotbno = NULLFSBLOCK;
  2298. }
  2299. /*
  2300. * No next block, just default.
  2301. */
  2302. else
  2303. gotbno = NULLFSBLOCK;
  2304. /*
  2305. * If both valid, pick the better one, else the only good
  2306. * one, else ap->rval is already set (to 0 or the inode block).
  2307. */
  2308. if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
  2309. ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
  2310. else if (prevbno != NULLFSBLOCK)
  2311. ap->rval = prevbno;
  2312. else if (gotbno != NULLFSBLOCK)
  2313. ap->rval = gotbno;
  2314. }
  2315. #undef ISVALID
  2316. }
  2317. STATIC int
  2318. xfs_bmap_rtalloc(
  2319. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2320. {
  2321. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2322. int error; /* error return value */
  2323. xfs_mount_t *mp; /* mount point structure */
  2324. xfs_extlen_t prod = 0; /* product factor for allocators */
  2325. xfs_extlen_t ralen = 0; /* realtime allocation length */
  2326. xfs_extlen_t align; /* minimum allocation alignment */
  2327. xfs_rtblock_t rtb;
  2328. mp = ap->ip->i_mount;
  2329. align = xfs_get_extsz_hint(ap->ip);
  2330. prod = align / mp->m_sb.sb_rextsize;
  2331. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2332. align, 1, ap->eof, 0,
  2333. ap->conv, &ap->off, &ap->alen);
  2334. if (error)
  2335. return error;
  2336. ASSERT(ap->alen);
  2337. ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
  2338. /*
  2339. * If the offset & length are not perfectly aligned
  2340. * then kill prod, it will just get us in trouble.
  2341. */
  2342. if (do_mod(ap->off, align) || ap->alen % align)
  2343. prod = 1;
  2344. /*
  2345. * Set ralen to be the actual requested length in rtextents.
  2346. */
  2347. ralen = ap->alen / mp->m_sb.sb_rextsize;
  2348. /*
  2349. * If the old value was close enough to MAXEXTLEN that
  2350. * we rounded up to it, cut it back so it's valid again.
  2351. * Note that if it's a really large request (bigger than
  2352. * MAXEXTLEN), we don't hear about that number, and can't
  2353. * adjust the starting point to match it.
  2354. */
  2355. if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
  2356. ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
  2357. /*
  2358. * If it's an allocation to an empty file at offset 0,
  2359. * pick an extent that will space things out in the rt area.
  2360. */
  2361. if (ap->eof && ap->off == 0) {
  2362. xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
  2363. error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
  2364. if (error)
  2365. return error;
  2366. ap->rval = rtx * mp->m_sb.sb_rextsize;
  2367. } else {
  2368. ap->rval = 0;
  2369. }
  2370. xfs_bmap_adjacent(ap);
  2371. /*
  2372. * Realtime allocation, done through xfs_rtallocate_extent.
  2373. */
  2374. atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
  2375. do_div(ap->rval, mp->m_sb.sb_rextsize);
  2376. rtb = ap->rval;
  2377. ap->alen = ralen;
  2378. if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
  2379. &ralen, atype, ap->wasdel, prod, &rtb)))
  2380. return error;
  2381. if (rtb == NULLFSBLOCK && prod > 1 &&
  2382. (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
  2383. ap->alen, &ralen, atype,
  2384. ap->wasdel, 1, &rtb)))
  2385. return error;
  2386. ap->rval = rtb;
  2387. if (ap->rval != NULLFSBLOCK) {
  2388. ap->rval *= mp->m_sb.sb_rextsize;
  2389. ralen *= mp->m_sb.sb_rextsize;
  2390. ap->alen = ralen;
  2391. ap->ip->i_d.di_nblocks += ralen;
  2392. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2393. if (ap->wasdel)
  2394. ap->ip->i_delayed_blks -= ralen;
  2395. /*
  2396. * Adjust the disk quota also. This was reserved
  2397. * earlier.
  2398. */
  2399. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  2400. ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
  2401. XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
  2402. } else {
  2403. ap->alen = 0;
  2404. }
  2405. return 0;
  2406. }
  2407. STATIC int
  2408. xfs_bmap_btalloc(
  2409. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2410. {
  2411. xfs_mount_t *mp; /* mount point structure */
  2412. xfs_alloctype_t atype = 0; /* type for allocation routines */
  2413. xfs_extlen_t align; /* minimum allocation alignment */
  2414. xfs_agnumber_t ag;
  2415. xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
  2416. xfs_agnumber_t startag;
  2417. xfs_alloc_arg_t args;
  2418. xfs_extlen_t blen;
  2419. xfs_extlen_t nextminlen = 0;
  2420. xfs_perag_t *pag;
  2421. int nullfb; /* true if ap->firstblock isn't set */
  2422. int isaligned;
  2423. int notinit;
  2424. int tryagain;
  2425. int error;
  2426. mp = ap->ip->i_mount;
  2427. align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
  2428. if (unlikely(align)) {
  2429. error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
  2430. align, 0, ap->eof, 0, ap->conv,
  2431. &ap->off, &ap->alen);
  2432. ASSERT(!error);
  2433. ASSERT(ap->alen);
  2434. }
  2435. nullfb = ap->firstblock == NULLFSBLOCK;
  2436. fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
  2437. if (nullfb) {
  2438. if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
  2439. ag = xfs_filestream_lookup_ag(ap->ip);
  2440. ag = (ag != NULLAGNUMBER) ? ag : 0;
  2441. ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
  2442. } else {
  2443. ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
  2444. }
  2445. } else
  2446. ap->rval = ap->firstblock;
  2447. xfs_bmap_adjacent(ap);
  2448. /*
  2449. * If allowed, use ap->rval; otherwise must use firstblock since
  2450. * it's in the right allocation group.
  2451. */
  2452. if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
  2453. ;
  2454. else
  2455. ap->rval = ap->firstblock;
  2456. /*
  2457. * Normal allocation, done through xfs_alloc_vextent.
  2458. */
  2459. tryagain = isaligned = 0;
  2460. args.tp = ap->tp;
  2461. args.mp = mp;
  2462. args.fsbno = ap->rval;
  2463. args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
  2464. args.firstblock = ap->firstblock;
  2465. blen = 0;
  2466. if (nullfb) {
  2467. if (ap->userdata && xfs_inode_is_filestream(ap->ip))
  2468. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2469. else
  2470. args.type = XFS_ALLOCTYPE_START_BNO;
  2471. args.total = ap->total;
  2472. /*
  2473. * Search for an allocation group with a single extent
  2474. * large enough for the request.
  2475. *
  2476. * If one isn't found, then adjust the minimum allocation
  2477. * size to the largest space found.
  2478. */
  2479. startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
  2480. if (startag == NULLAGNUMBER)
  2481. startag = ag = 0;
  2482. notinit = 0;
  2483. down_read(&mp->m_peraglock);
  2484. while (blen < ap->alen) {
  2485. pag = &mp->m_perag[ag];
  2486. if (!pag->pagf_init &&
  2487. (error = xfs_alloc_pagf_init(mp, args.tp,
  2488. ag, XFS_ALLOC_FLAG_TRYLOCK))) {
  2489. up_read(&mp->m_peraglock);
  2490. return error;
  2491. }
  2492. /*
  2493. * See xfs_alloc_fix_freelist...
  2494. */
  2495. if (pag->pagf_init) {
  2496. xfs_extlen_t longest;
  2497. longest = xfs_alloc_longest_free_extent(mp, pag);
  2498. if (blen < longest)
  2499. blen = longest;
  2500. } else
  2501. notinit = 1;
  2502. if (xfs_inode_is_filestream(ap->ip)) {
  2503. if (blen >= ap->alen)
  2504. break;
  2505. if (ap->userdata) {
  2506. /*
  2507. * If startag is an invalid AG, we've
  2508. * come here once before and
  2509. * xfs_filestream_new_ag picked the
  2510. * best currently available.
  2511. *
  2512. * Don't continue looping, since we
  2513. * could loop forever.
  2514. */
  2515. if (startag == NULLAGNUMBER)
  2516. break;
  2517. error = xfs_filestream_new_ag(ap, &ag);
  2518. if (error) {
  2519. up_read(&mp->m_peraglock);
  2520. return error;
  2521. }
  2522. /* loop again to set 'blen'*/
  2523. startag = NULLAGNUMBER;
  2524. continue;
  2525. }
  2526. }
  2527. if (++ag == mp->m_sb.sb_agcount)
  2528. ag = 0;
  2529. if (ag == startag)
  2530. break;
  2531. }
  2532. up_read(&mp->m_peraglock);
  2533. /*
  2534. * Since the above loop did a BUF_TRYLOCK, it is
  2535. * possible that there is space for this request.
  2536. */
  2537. if (notinit || blen < ap->minlen)
  2538. args.minlen = ap->minlen;
  2539. /*
  2540. * If the best seen length is less than the request
  2541. * length, use the best as the minimum.
  2542. */
  2543. else if (blen < ap->alen)
  2544. args.minlen = blen;
  2545. /*
  2546. * Otherwise we've seen an extent as big as alen,
  2547. * use that as the minimum.
  2548. */
  2549. else
  2550. args.minlen = ap->alen;
  2551. /*
  2552. * set the failure fallback case to look in the selected
  2553. * AG as the stream may have moved.
  2554. */
  2555. if (xfs_inode_is_filestream(ap->ip))
  2556. ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
  2557. } else if (ap->low) {
  2558. if (xfs_inode_is_filestream(ap->ip))
  2559. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2560. else
  2561. args.type = XFS_ALLOCTYPE_START_BNO;
  2562. args.total = args.minlen = ap->minlen;
  2563. } else {
  2564. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  2565. args.total = ap->total;
  2566. args.minlen = ap->minlen;
  2567. }
  2568. /* apply extent size hints if obtained earlier */
  2569. if (unlikely(align)) {
  2570. args.prod = align;
  2571. if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
  2572. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2573. } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
  2574. args.prod = 1;
  2575. args.mod = 0;
  2576. } else {
  2577. args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
  2578. if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
  2579. args.mod = (xfs_extlen_t)(args.prod - args.mod);
  2580. }
  2581. /*
  2582. * If we are not low on available data blocks, and the
  2583. * underlying logical volume manager is a stripe, and
  2584. * the file offset is zero then try to allocate data
  2585. * blocks on stripe unit boundary.
  2586. * NOTE: ap->aeof is only set if the allocation length
  2587. * is >= the stripe unit and the allocation offset is
  2588. * at the end of file.
  2589. */
  2590. if (!ap->low && ap->aeof) {
  2591. if (!ap->off) {
  2592. args.alignment = mp->m_dalign;
  2593. atype = args.type;
  2594. isaligned = 1;
  2595. /*
  2596. * Adjust for alignment
  2597. */
  2598. if (blen > args.alignment && blen <= ap->alen)
  2599. args.minlen = blen - args.alignment;
  2600. args.minalignslop = 0;
  2601. } else {
  2602. /*
  2603. * First try an exact bno allocation.
  2604. * If it fails then do a near or start bno
  2605. * allocation with alignment turned on.
  2606. */
  2607. atype = args.type;
  2608. tryagain = 1;
  2609. args.type = XFS_ALLOCTYPE_THIS_BNO;
  2610. args.alignment = 1;
  2611. /*
  2612. * Compute the minlen+alignment for the
  2613. * next case. Set slop so that the value
  2614. * of minlen+alignment+slop doesn't go up
  2615. * between the calls.
  2616. */
  2617. if (blen > mp->m_dalign && blen <= ap->alen)
  2618. nextminlen = blen - mp->m_dalign;
  2619. else
  2620. nextminlen = args.minlen;
  2621. if (nextminlen + mp->m_dalign > args.minlen + 1)
  2622. args.minalignslop =
  2623. nextminlen + mp->m_dalign -
  2624. args.minlen - 1;
  2625. else
  2626. args.minalignslop = 0;
  2627. }
  2628. } else {
  2629. args.alignment = 1;
  2630. args.minalignslop = 0;
  2631. }
  2632. args.minleft = ap->minleft;
  2633. args.wasdel = ap->wasdel;
  2634. args.isfl = 0;
  2635. args.userdata = ap->userdata;
  2636. if ((error = xfs_alloc_vextent(&args)))
  2637. return error;
  2638. if (tryagain && args.fsbno == NULLFSBLOCK) {
  2639. /*
  2640. * Exact allocation failed. Now try with alignment
  2641. * turned on.
  2642. */
  2643. args.type = atype;
  2644. args.fsbno = ap->rval;
  2645. args.alignment = mp->m_dalign;
  2646. args.minlen = nextminlen;
  2647. args.minalignslop = 0;
  2648. isaligned = 1;
  2649. if ((error = xfs_alloc_vextent(&args)))
  2650. return error;
  2651. }
  2652. if (isaligned && args.fsbno == NULLFSBLOCK) {
  2653. /*
  2654. * allocation failed, so turn off alignment and
  2655. * try again.
  2656. */
  2657. args.type = atype;
  2658. args.fsbno = ap->rval;
  2659. args.alignment = 0;
  2660. if ((error = xfs_alloc_vextent(&args)))
  2661. return error;
  2662. }
  2663. if (args.fsbno == NULLFSBLOCK && nullfb &&
  2664. args.minlen > ap->minlen) {
  2665. args.minlen = ap->minlen;
  2666. args.type = XFS_ALLOCTYPE_START_BNO;
  2667. args.fsbno = ap->rval;
  2668. if ((error = xfs_alloc_vextent(&args)))
  2669. return error;
  2670. }
  2671. if (args.fsbno == NULLFSBLOCK && nullfb) {
  2672. args.fsbno = 0;
  2673. args.type = XFS_ALLOCTYPE_FIRST_AG;
  2674. args.total = ap->minlen;
  2675. args.minleft = 0;
  2676. if ((error = xfs_alloc_vextent(&args)))
  2677. return error;
  2678. ap->low = 1;
  2679. }
  2680. if (args.fsbno != NULLFSBLOCK) {
  2681. ap->firstblock = ap->rval = args.fsbno;
  2682. ASSERT(nullfb || fb_agno == args.agno ||
  2683. (ap->low && fb_agno < args.agno));
  2684. ap->alen = args.len;
  2685. ap->ip->i_d.di_nblocks += args.len;
  2686. xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
  2687. if (ap->wasdel)
  2688. ap->ip->i_delayed_blks -= args.len;
  2689. /*
  2690. * Adjust the disk quota also. This was reserved
  2691. * earlier.
  2692. */
  2693. xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
  2694. ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
  2695. XFS_TRANS_DQ_BCOUNT,
  2696. (long) args.len);
  2697. } else {
  2698. ap->rval = NULLFSBLOCK;
  2699. ap->alen = 0;
  2700. }
  2701. return 0;
  2702. }
  2703. /*
  2704. * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
  2705. * It figures out where to ask the underlying allocator to put the new extent.
  2706. */
  2707. STATIC int
  2708. xfs_bmap_alloc(
  2709. xfs_bmalloca_t *ap) /* bmap alloc argument struct */
  2710. {
  2711. if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
  2712. return xfs_bmap_rtalloc(ap);
  2713. return xfs_bmap_btalloc(ap);
  2714. }
  2715. /*
  2716. * Transform a btree format file with only one leaf node, where the
  2717. * extents list will fit in the inode, into an extents format file.
  2718. * Since the file extents are already in-core, all we have to do is
  2719. * give up the space for the btree root and pitch the leaf block.
  2720. */
  2721. STATIC int /* error */
  2722. xfs_bmap_btree_to_extents(
  2723. xfs_trans_t *tp, /* transaction pointer */
  2724. xfs_inode_t *ip, /* incore inode pointer */
  2725. xfs_btree_cur_t *cur, /* btree cursor */
  2726. int *logflagsp, /* inode logging flags */
  2727. int whichfork) /* data or attr fork */
  2728. {
  2729. /* REFERENCED */
  2730. struct xfs_btree_block *cblock;/* child btree block */
  2731. xfs_fsblock_t cbno; /* child block number */
  2732. xfs_buf_t *cbp; /* child block's buffer */
  2733. int error; /* error return value */
  2734. xfs_ifork_t *ifp; /* inode fork data */
  2735. xfs_mount_t *mp; /* mount point structure */
  2736. __be64 *pp; /* ptr to block address */
  2737. struct xfs_btree_block *rblock;/* root btree block */
  2738. mp = ip->i_mount;
  2739. ifp = XFS_IFORK_PTR(ip, whichfork);
  2740. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2741. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  2742. rblock = ifp->if_broot;
  2743. ASSERT(be16_to_cpu(rblock->bb_level) == 1);
  2744. ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
  2745. ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
  2746. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
  2747. cbno = be64_to_cpu(*pp);
  2748. *logflagsp = 0;
  2749. #ifdef DEBUG
  2750. if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
  2751. return error;
  2752. #endif
  2753. if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
  2754. XFS_BMAP_BTREE_REF)))
  2755. return error;
  2756. cblock = XFS_BUF_TO_BLOCK(cbp);
  2757. if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
  2758. return error;
  2759. xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
  2760. ip->i_d.di_nblocks--;
  2761. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
  2762. xfs_trans_binval(tp, cbp);
  2763. if (cur->bc_bufs[0] == cbp)
  2764. cur->bc_bufs[0] = NULL;
  2765. xfs_iroot_realloc(ip, -1, whichfork);
  2766. ASSERT(ifp->if_broot == NULL);
  2767. ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
  2768. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  2769. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
  2770. return 0;
  2771. }
  2772. /*
  2773. * Called by xfs_bmapi to update file extent records and the btree
  2774. * after removing space (or undoing a delayed allocation).
  2775. */
  2776. STATIC int /* error */
  2777. xfs_bmap_del_extent(
  2778. xfs_inode_t *ip, /* incore inode pointer */
  2779. xfs_trans_t *tp, /* current transaction pointer */
  2780. xfs_extnum_t idx, /* extent number to update/delete */
  2781. xfs_bmap_free_t *flist, /* list of extents to be freed */
  2782. xfs_btree_cur_t *cur, /* if null, not a btree */
  2783. xfs_bmbt_irec_t *del, /* data to remove from extents */
  2784. int *logflagsp, /* inode logging flags */
  2785. xfs_extdelta_t *delta, /* Change made to incore extents */
  2786. int whichfork, /* data or attr fork */
  2787. int rsvd) /* OK to allocate reserved blocks */
  2788. {
  2789. xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
  2790. xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
  2791. xfs_fsblock_t del_endblock=0; /* first block past del */
  2792. xfs_fileoff_t del_endoff; /* first offset past del */
  2793. int delay; /* current block is delayed allocated */
  2794. int do_fx; /* free extent at end of routine */
  2795. xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
  2796. int error; /* error return value */
  2797. int flags; /* inode logging flags */
  2798. xfs_bmbt_irec_t got; /* current extent entry */
  2799. xfs_fileoff_t got_endoff; /* first offset past got */
  2800. int i; /* temp state */
  2801. xfs_ifork_t *ifp; /* inode fork pointer */
  2802. xfs_mount_t *mp; /* mount structure */
  2803. xfs_filblks_t nblks; /* quota/sb block count */
  2804. xfs_bmbt_irec_t new; /* new record to be inserted */
  2805. /* REFERENCED */
  2806. uint qfield; /* quota field to update */
  2807. xfs_filblks_t temp; /* for indirect length calculations */
  2808. xfs_filblks_t temp2; /* for indirect length calculations */
  2809. int state = 0;
  2810. XFS_STATS_INC(xs_del_exlist);
  2811. if (whichfork == XFS_ATTR_FORK)
  2812. state |= BMAP_ATTRFORK;
  2813. mp = ip->i_mount;
  2814. ifp = XFS_IFORK_PTR(ip, whichfork);
  2815. ASSERT((idx >= 0) && (idx < ifp->if_bytes /
  2816. (uint)sizeof(xfs_bmbt_rec_t)));
  2817. ASSERT(del->br_blockcount > 0);
  2818. ep = xfs_iext_get_ext(ifp, idx);
  2819. xfs_bmbt_get_all(ep, &got);
  2820. ASSERT(got.br_startoff <= del->br_startoff);
  2821. del_endoff = del->br_startoff + del->br_blockcount;
  2822. got_endoff = got.br_startoff + got.br_blockcount;
  2823. ASSERT(got_endoff >= del_endoff);
  2824. delay = isnullstartblock(got.br_startblock);
  2825. ASSERT(isnullstartblock(del->br_startblock) == delay);
  2826. flags = 0;
  2827. qfield = 0;
  2828. error = 0;
  2829. /*
  2830. * If deleting a real allocation, must free up the disk space.
  2831. */
  2832. if (!delay) {
  2833. flags = XFS_ILOG_CORE;
  2834. /*
  2835. * Realtime allocation. Free it and record di_nblocks update.
  2836. */
  2837. if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
  2838. xfs_fsblock_t bno;
  2839. xfs_filblks_t len;
  2840. ASSERT(do_mod(del->br_blockcount,
  2841. mp->m_sb.sb_rextsize) == 0);
  2842. ASSERT(do_mod(del->br_startblock,
  2843. mp->m_sb.sb_rextsize) == 0);
  2844. bno = del->br_startblock;
  2845. len = del->br_blockcount;
  2846. do_div(bno, mp->m_sb.sb_rextsize);
  2847. do_div(len, mp->m_sb.sb_rextsize);
  2848. if ((error = xfs_rtfree_extent(ip->i_transp, bno,
  2849. (xfs_extlen_t)len)))
  2850. goto done;
  2851. do_fx = 0;
  2852. nblks = len * mp->m_sb.sb_rextsize;
  2853. qfield = XFS_TRANS_DQ_RTBCOUNT;
  2854. }
  2855. /*
  2856. * Ordinary allocation.
  2857. */
  2858. else {
  2859. do_fx = 1;
  2860. nblks = del->br_blockcount;
  2861. qfield = XFS_TRANS_DQ_BCOUNT;
  2862. }
  2863. /*
  2864. * Set up del_endblock and cur for later.
  2865. */
  2866. del_endblock = del->br_startblock + del->br_blockcount;
  2867. if (cur) {
  2868. if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
  2869. got.br_startblock, got.br_blockcount,
  2870. &i)))
  2871. goto done;
  2872. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2873. }
  2874. da_old = da_new = 0;
  2875. } else {
  2876. da_old = startblockval(got.br_startblock);
  2877. da_new = 0;
  2878. nblks = 0;
  2879. do_fx = 0;
  2880. }
  2881. /*
  2882. * Set flag value to use in switch statement.
  2883. * Left-contig is 2, right-contig is 1.
  2884. */
  2885. switch (((got.br_startoff == del->br_startoff) << 1) |
  2886. (got_endoff == del_endoff)) {
  2887. case 3:
  2888. /*
  2889. * Matches the whole extent. Delete the entry.
  2890. */
  2891. xfs_iext_remove(ip, idx, 1,
  2892. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
  2893. ifp->if_lastex = idx;
  2894. if (delay)
  2895. break;
  2896. XFS_IFORK_NEXT_SET(ip, whichfork,
  2897. XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
  2898. flags |= XFS_ILOG_CORE;
  2899. if (!cur) {
  2900. flags |= xfs_ilog_fext(whichfork);
  2901. break;
  2902. }
  2903. if ((error = xfs_btree_delete(cur, &i)))
  2904. goto done;
  2905. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  2906. break;
  2907. case 2:
  2908. /*
  2909. * Deleting the first part of the extent.
  2910. */
  2911. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2912. xfs_bmbt_set_startoff(ep, del_endoff);
  2913. temp = got.br_blockcount - del->br_blockcount;
  2914. xfs_bmbt_set_blockcount(ep, temp);
  2915. ifp->if_lastex = idx;
  2916. if (delay) {
  2917. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2918. da_old);
  2919. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2920. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2921. da_new = temp;
  2922. break;
  2923. }
  2924. xfs_bmbt_set_startblock(ep, del_endblock);
  2925. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2926. if (!cur) {
  2927. flags |= xfs_ilog_fext(whichfork);
  2928. break;
  2929. }
  2930. if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
  2931. got.br_blockcount - del->br_blockcount,
  2932. got.br_state)))
  2933. goto done;
  2934. break;
  2935. case 1:
  2936. /*
  2937. * Deleting the last part of the extent.
  2938. */
  2939. temp = got.br_blockcount - del->br_blockcount;
  2940. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2941. xfs_bmbt_set_blockcount(ep, temp);
  2942. ifp->if_lastex = idx;
  2943. if (delay) {
  2944. temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
  2945. da_old);
  2946. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  2947. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2948. da_new = temp;
  2949. break;
  2950. }
  2951. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  2952. if (!cur) {
  2953. flags |= xfs_ilog_fext(whichfork);
  2954. break;
  2955. }
  2956. if ((error = xfs_bmbt_update(cur, got.br_startoff,
  2957. got.br_startblock,
  2958. got.br_blockcount - del->br_blockcount,
  2959. got.br_state)))
  2960. goto done;
  2961. break;
  2962. case 0:
  2963. /*
  2964. * Deleting the middle of the extent.
  2965. */
  2966. temp = del->br_startoff - got.br_startoff;
  2967. trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
  2968. xfs_bmbt_set_blockcount(ep, temp);
  2969. new.br_startoff = del_endoff;
  2970. temp2 = got_endoff - del_endoff;
  2971. new.br_blockcount = temp2;
  2972. new.br_state = got.br_state;
  2973. if (!delay) {
  2974. new.br_startblock = del_endblock;
  2975. flags |= XFS_ILOG_CORE;
  2976. if (cur) {
  2977. if ((error = xfs_bmbt_update(cur,
  2978. got.br_startoff,
  2979. got.br_startblock, temp,
  2980. got.br_state)))
  2981. goto done;
  2982. if ((error = xfs_btree_increment(cur, 0, &i)))
  2983. goto done;
  2984. cur->bc_rec.b = new;
  2985. error = xfs_btree_insert(cur, &i);
  2986. if (error && error != ENOSPC)
  2987. goto done;
  2988. /*
  2989. * If get no-space back from btree insert,
  2990. * it tried a split, and we have a zero
  2991. * block reservation.
  2992. * Fix up our state and return the error.
  2993. */
  2994. if (error == ENOSPC) {
  2995. /*
  2996. * Reset the cursor, don't trust
  2997. * it after any insert operation.
  2998. */
  2999. if ((error = xfs_bmbt_lookup_eq(cur,
  3000. got.br_startoff,
  3001. got.br_startblock,
  3002. temp, &i)))
  3003. goto done;
  3004. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3005. /*
  3006. * Update the btree record back
  3007. * to the original value.
  3008. */
  3009. if ((error = xfs_bmbt_update(cur,
  3010. got.br_startoff,
  3011. got.br_startblock,
  3012. got.br_blockcount,
  3013. got.br_state)))
  3014. goto done;
  3015. /*
  3016. * Reset the extent record back
  3017. * to the original value.
  3018. */
  3019. xfs_bmbt_set_blockcount(ep,
  3020. got.br_blockcount);
  3021. flags = 0;
  3022. error = XFS_ERROR(ENOSPC);
  3023. goto done;
  3024. }
  3025. XFS_WANT_CORRUPTED_GOTO(i == 1, done);
  3026. } else
  3027. flags |= xfs_ilog_fext(whichfork);
  3028. XFS_IFORK_NEXT_SET(ip, whichfork,
  3029. XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
  3030. } else {
  3031. ASSERT(whichfork == XFS_DATA_FORK);
  3032. temp = xfs_bmap_worst_indlen(ip, temp);
  3033. xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
  3034. temp2 = xfs_bmap_worst_indlen(ip, temp2);
  3035. new.br_startblock = nullstartblock((int)temp2);
  3036. da_new = temp + temp2;
  3037. while (da_new > da_old) {
  3038. if (temp) {
  3039. temp--;
  3040. da_new--;
  3041. xfs_bmbt_set_startblock(ep,
  3042. nullstartblock((int)temp));
  3043. }
  3044. if (da_new == da_old)
  3045. break;
  3046. if (temp2) {
  3047. temp2--;
  3048. da_new--;
  3049. new.br_startblock =
  3050. nullstartblock((int)temp2);
  3051. }
  3052. }
  3053. }
  3054. trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
  3055. xfs_iext_insert(ip, idx + 1, 1, &new, state);
  3056. ifp->if_lastex = idx + 1;
  3057. break;
  3058. }
  3059. /*
  3060. * If we need to, add to list of extents to delete.
  3061. */
  3062. if (do_fx)
  3063. xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
  3064. mp);
  3065. /*
  3066. * Adjust inode # blocks in the file.
  3067. */
  3068. if (nblks)
  3069. ip->i_d.di_nblocks -= nblks;
  3070. /*
  3071. * Adjust quota data.
  3072. */
  3073. if (qfield)
  3074. xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
  3075. /*
  3076. * Account for change in delayed indirect blocks.
  3077. * Nothing to do for disk quota accounting here.
  3078. */
  3079. ASSERT(da_old >= da_new);
  3080. if (da_old > da_new)
  3081. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
  3082. rsvd);
  3083. if (delta) {
  3084. /* DELTA: report the original extent. */
  3085. if (delta->xed_startoff > got.br_startoff)
  3086. delta->xed_startoff = got.br_startoff;
  3087. if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
  3088. delta->xed_blockcount = got.br_startoff +
  3089. got.br_blockcount;
  3090. }
  3091. done:
  3092. *logflagsp = flags;
  3093. return error;
  3094. }
  3095. /*
  3096. * Remove the entry "free" from the free item list. Prev points to the
  3097. * previous entry, unless "free" is the head of the list.
  3098. */
  3099. STATIC void
  3100. xfs_bmap_del_free(
  3101. xfs_bmap_free_t *flist, /* free item list header */
  3102. xfs_bmap_free_item_t *prev, /* previous item on list, if any */
  3103. xfs_bmap_free_item_t *free) /* list item to be freed */
  3104. {
  3105. if (prev)
  3106. prev->xbfi_next = free->xbfi_next;
  3107. else
  3108. flist->xbf_first = free->xbfi_next;
  3109. flist->xbf_count--;
  3110. kmem_zone_free(xfs_bmap_free_item_zone, free);
  3111. }
  3112. /*
  3113. * Convert an extents-format file into a btree-format file.
  3114. * The new file will have a root block (in the inode) and a single child block.
  3115. */
  3116. STATIC int /* error */
  3117. xfs_bmap_extents_to_btree(
  3118. xfs_trans_t *tp, /* transaction pointer */
  3119. xfs_inode_t *ip, /* incore inode pointer */
  3120. xfs_fsblock_t *firstblock, /* first-block-allocated */
  3121. xfs_bmap_free_t *flist, /* blocks freed in xaction */
  3122. xfs_btree_cur_t **curp, /* cursor returned to caller */
  3123. int wasdel, /* converting a delayed alloc */
  3124. int *logflagsp, /* inode logging flags */
  3125. int whichfork) /* data or attr fork */
  3126. {
  3127. struct xfs_btree_block *ablock; /* allocated (child) bt block */
  3128. xfs_buf_t *abp; /* buffer for ablock */
  3129. xfs_alloc_arg_t args; /* allocation arguments */
  3130. xfs_bmbt_rec_t *arp; /* child record pointer */
  3131. struct xfs_btree_block *block; /* btree root block */
  3132. xfs_btree_cur_t *cur; /* bmap btree cursor */
  3133. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3134. int error; /* error return value */
  3135. xfs_extnum_t i, cnt; /* extent record index */
  3136. xfs_ifork_t *ifp; /* inode fork pointer */
  3137. xfs_bmbt_key_t *kp; /* root block key pointer */
  3138. xfs_mount_t *mp; /* mount structure */
  3139. xfs_extnum_t nextents; /* number of file extents */
  3140. xfs_bmbt_ptr_t *pp; /* root block address pointer */
  3141. ifp = XFS_IFORK_PTR(ip, whichfork);
  3142. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
  3143. ASSERT(ifp->if_ext_max ==
  3144. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  3145. /*
  3146. * Make space in the inode incore.
  3147. */
  3148. xfs_iroot_realloc(ip, 1, whichfork);
  3149. ifp->if_flags |= XFS_IFBROOT;
  3150. /*
  3151. * Fill in the root.
  3152. */
  3153. block = ifp->if_broot;
  3154. block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3155. block->bb_level = cpu_to_be16(1);
  3156. block->bb_numrecs = cpu_to_be16(1);
  3157. block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3158. block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3159. /*
  3160. * Need a cursor. Can't allocate until bb_level is filled in.
  3161. */
  3162. mp = ip->i_mount;
  3163. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  3164. cur->bc_private.b.firstblock = *firstblock;
  3165. cur->bc_private.b.flist = flist;
  3166. cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
  3167. /*
  3168. * Convert to a btree with two levels, one record in root.
  3169. */
  3170. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
  3171. args.tp = tp;
  3172. args.mp = mp;
  3173. args.firstblock = *firstblock;
  3174. if (*firstblock == NULLFSBLOCK) {
  3175. args.type = XFS_ALLOCTYPE_START_BNO;
  3176. args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
  3177. } else if (flist->xbf_low) {
  3178. args.type = XFS_ALLOCTYPE_START_BNO;
  3179. args.fsbno = *firstblock;
  3180. } else {
  3181. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3182. args.fsbno = *firstblock;
  3183. }
  3184. args.minlen = args.maxlen = args.prod = 1;
  3185. args.total = args.minleft = args.alignment = args.mod = args.isfl =
  3186. args.minalignslop = 0;
  3187. args.wasdel = wasdel;
  3188. *logflagsp = 0;
  3189. if ((error = xfs_alloc_vextent(&args))) {
  3190. xfs_iroot_realloc(ip, -1, whichfork);
  3191. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  3192. return error;
  3193. }
  3194. /*
  3195. * Allocation can't fail, the space was reserved.
  3196. */
  3197. ASSERT(args.fsbno != NULLFSBLOCK);
  3198. ASSERT(*firstblock == NULLFSBLOCK ||
  3199. args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
  3200. (flist->xbf_low &&
  3201. args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
  3202. *firstblock = cur->bc_private.b.firstblock = args.fsbno;
  3203. cur->bc_private.b.allocated++;
  3204. ip->i_d.di_nblocks++;
  3205. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
  3206. abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
  3207. /*
  3208. * Fill in the child block.
  3209. */
  3210. ablock = XFS_BUF_TO_BLOCK(abp);
  3211. ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
  3212. ablock->bb_level = 0;
  3213. ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  3214. ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  3215. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3216. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3217. for (cnt = i = 0; i < nextents; i++) {
  3218. ep = xfs_iext_get_ext(ifp, i);
  3219. if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
  3220. arp->l0 = cpu_to_be64(ep->l0);
  3221. arp->l1 = cpu_to_be64(ep->l1);
  3222. arp++; cnt++;
  3223. }
  3224. }
  3225. ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
  3226. xfs_btree_set_numrecs(ablock, cnt);
  3227. /*
  3228. * Fill in the root key and pointer.
  3229. */
  3230. kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
  3231. arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
  3232. kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
  3233. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
  3234. be16_to_cpu(block->bb_level)));
  3235. *pp = cpu_to_be64(args.fsbno);
  3236. /*
  3237. * Do all this logging at the end so that
  3238. * the root is at the right level.
  3239. */
  3240. xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
  3241. xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
  3242. ASSERT(*curp == NULL);
  3243. *curp = cur;
  3244. *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
  3245. return 0;
  3246. }
  3247. /*
  3248. * Calculate the default attribute fork offset for newly created inodes.
  3249. */
  3250. uint
  3251. xfs_default_attroffset(
  3252. struct xfs_inode *ip)
  3253. {
  3254. struct xfs_mount *mp = ip->i_mount;
  3255. uint offset;
  3256. if (mp->m_sb.sb_inodesize == 256) {
  3257. offset = XFS_LITINO(mp) -
  3258. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3259. } else {
  3260. offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  3261. }
  3262. ASSERT(offset < XFS_LITINO(mp));
  3263. return offset;
  3264. }
  3265. /*
  3266. * Helper routine to reset inode di_forkoff field when switching
  3267. * attribute fork from local to extent format - we reset it where
  3268. * possible to make space available for inline data fork extents.
  3269. */
  3270. STATIC void
  3271. xfs_bmap_forkoff_reset(
  3272. xfs_mount_t *mp,
  3273. xfs_inode_t *ip,
  3274. int whichfork)
  3275. {
  3276. if (whichfork == XFS_ATTR_FORK &&
  3277. ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
  3278. ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
  3279. ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
  3280. uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
  3281. if (dfl_forkoff > ip->i_d.di_forkoff) {
  3282. ip->i_d.di_forkoff = dfl_forkoff;
  3283. ip->i_df.if_ext_max =
  3284. XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
  3285. ip->i_afp->if_ext_max =
  3286. XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
  3287. }
  3288. }
  3289. }
  3290. /*
  3291. * Convert a local file to an extents file.
  3292. * This code is out of bounds for data forks of regular files,
  3293. * since the file data needs to get logged so things will stay consistent.
  3294. * (The bmap-level manipulations are ok, though).
  3295. */
  3296. STATIC int /* error */
  3297. xfs_bmap_local_to_extents(
  3298. xfs_trans_t *tp, /* transaction pointer */
  3299. xfs_inode_t *ip, /* incore inode pointer */
  3300. xfs_fsblock_t *firstblock, /* first block allocated in xaction */
  3301. xfs_extlen_t total, /* total blocks needed by transaction */
  3302. int *logflagsp, /* inode logging flags */
  3303. int whichfork) /* data or attr fork */
  3304. {
  3305. int error; /* error return value */
  3306. int flags; /* logging flags returned */
  3307. xfs_ifork_t *ifp; /* inode fork pointer */
  3308. /*
  3309. * We don't want to deal with the case of keeping inode data inline yet.
  3310. * So sending the data fork of a regular inode is invalid.
  3311. */
  3312. ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
  3313. whichfork == XFS_DATA_FORK));
  3314. ifp = XFS_IFORK_PTR(ip, whichfork);
  3315. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3316. flags = 0;
  3317. error = 0;
  3318. if (ifp->if_bytes) {
  3319. xfs_alloc_arg_t args; /* allocation arguments */
  3320. xfs_buf_t *bp; /* buffer for extent block */
  3321. xfs_bmbt_rec_host_t *ep;/* extent record pointer */
  3322. args.tp = tp;
  3323. args.mp = ip->i_mount;
  3324. args.firstblock = *firstblock;
  3325. ASSERT((ifp->if_flags &
  3326. (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
  3327. /*
  3328. * Allocate a block. We know we need only one, since the
  3329. * file currently fits in an inode.
  3330. */
  3331. if (*firstblock == NULLFSBLOCK) {
  3332. args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
  3333. args.type = XFS_ALLOCTYPE_START_BNO;
  3334. } else {
  3335. args.fsbno = *firstblock;
  3336. args.type = XFS_ALLOCTYPE_NEAR_BNO;
  3337. }
  3338. args.total = total;
  3339. args.mod = args.minleft = args.alignment = args.wasdel =
  3340. args.isfl = args.minalignslop = 0;
  3341. args.minlen = args.maxlen = args.prod = 1;
  3342. if ((error = xfs_alloc_vextent(&args)))
  3343. goto done;
  3344. /*
  3345. * Can't fail, the space was reserved.
  3346. */
  3347. ASSERT(args.fsbno != NULLFSBLOCK);
  3348. ASSERT(args.len == 1);
  3349. *firstblock = args.fsbno;
  3350. bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
  3351. memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
  3352. ifp->if_bytes);
  3353. xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
  3354. xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
  3355. xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
  3356. xfs_iext_add(ifp, 0, 1);
  3357. ep = xfs_iext_get_ext(ifp, 0);
  3358. xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
  3359. trace_xfs_bmap_post_update(ip, 0,
  3360. whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
  3361. _THIS_IP_);
  3362. XFS_IFORK_NEXT_SET(ip, whichfork, 1);
  3363. ip->i_d.di_nblocks = 1;
  3364. xfs_trans_mod_dquot_byino(tp, ip,
  3365. XFS_TRANS_DQ_BCOUNT, 1L);
  3366. flags |= xfs_ilog_fext(whichfork);
  3367. } else {
  3368. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
  3369. xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
  3370. }
  3371. ifp->if_flags &= ~XFS_IFINLINE;
  3372. ifp->if_flags |= XFS_IFEXTENTS;
  3373. XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
  3374. flags |= XFS_ILOG_CORE;
  3375. done:
  3376. *logflagsp = flags;
  3377. return error;
  3378. }
  3379. /*
  3380. * Search the extent records for the entry containing block bno.
  3381. * If bno lies in a hole, point to the next entry. If bno lies
  3382. * past eof, *eofp will be set, and *prevp will contain the last
  3383. * entry (null if none). Else, *lastxp will be set to the index
  3384. * of the found entry; *gotp will contain the entry.
  3385. */
  3386. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3387. xfs_bmap_search_multi_extents(
  3388. xfs_ifork_t *ifp, /* inode fork pointer */
  3389. xfs_fileoff_t bno, /* block number searched for */
  3390. int *eofp, /* out: end of file found */
  3391. xfs_extnum_t *lastxp, /* out: last extent index */
  3392. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3393. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3394. {
  3395. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3396. xfs_extnum_t lastx; /* last extent index */
  3397. /*
  3398. * Initialize the extent entry structure to catch access to
  3399. * uninitialized br_startblock field.
  3400. */
  3401. gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
  3402. gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
  3403. gotp->br_state = XFS_EXT_INVALID;
  3404. #if XFS_BIG_BLKNOS
  3405. gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
  3406. #else
  3407. gotp->br_startblock = 0xffffa5a5;
  3408. #endif
  3409. prevp->br_startoff = NULLFILEOFF;
  3410. ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
  3411. if (lastx > 0) {
  3412. xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
  3413. }
  3414. if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
  3415. xfs_bmbt_get_all(ep, gotp);
  3416. *eofp = 0;
  3417. } else {
  3418. if (lastx > 0) {
  3419. *gotp = *prevp;
  3420. }
  3421. *eofp = 1;
  3422. ep = NULL;
  3423. }
  3424. *lastxp = lastx;
  3425. return ep;
  3426. }
  3427. /*
  3428. * Search the extents list for the inode, for the extent containing bno.
  3429. * If bno lies in a hole, point to the next entry. If bno lies past eof,
  3430. * *eofp will be set, and *prevp will contain the last entry (null if none).
  3431. * Else, *lastxp will be set to the index of the found
  3432. * entry; *gotp will contain the entry.
  3433. */
  3434. STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
  3435. xfs_bmap_search_extents(
  3436. xfs_inode_t *ip, /* incore inode pointer */
  3437. xfs_fileoff_t bno, /* block number searched for */
  3438. int fork, /* data or attr fork */
  3439. int *eofp, /* out: end of file found */
  3440. xfs_extnum_t *lastxp, /* out: last extent index */
  3441. xfs_bmbt_irec_t *gotp, /* out: extent entry found */
  3442. xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
  3443. {
  3444. xfs_ifork_t *ifp; /* inode fork pointer */
  3445. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3446. XFS_STATS_INC(xs_look_exlist);
  3447. ifp = XFS_IFORK_PTR(ip, fork);
  3448. ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
  3449. if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
  3450. !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
  3451. xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
  3452. "Access to block zero in inode %llu "
  3453. "start_block: %llx start_off: %llx "
  3454. "blkcnt: %llx extent-state: %x lastx: %x\n",
  3455. (unsigned long long)ip->i_ino,
  3456. (unsigned long long)gotp->br_startblock,
  3457. (unsigned long long)gotp->br_startoff,
  3458. (unsigned long long)gotp->br_blockcount,
  3459. gotp->br_state, *lastxp);
  3460. *lastxp = NULLEXTNUM;
  3461. *eofp = 1;
  3462. return NULL;
  3463. }
  3464. return ep;
  3465. }
  3466. /*
  3467. * Compute the worst-case number of indirect blocks that will be used
  3468. * for ip's delayed extent of length "len".
  3469. */
  3470. STATIC xfs_filblks_t
  3471. xfs_bmap_worst_indlen(
  3472. xfs_inode_t *ip, /* incore inode pointer */
  3473. xfs_filblks_t len) /* delayed extent length */
  3474. {
  3475. int level; /* btree level number */
  3476. int maxrecs; /* maximum record count at this level */
  3477. xfs_mount_t *mp; /* mount structure */
  3478. xfs_filblks_t rval; /* return value */
  3479. mp = ip->i_mount;
  3480. maxrecs = mp->m_bmap_dmxr[0];
  3481. for (level = 0, rval = 0;
  3482. level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
  3483. level++) {
  3484. len += maxrecs - 1;
  3485. do_div(len, maxrecs);
  3486. rval += len;
  3487. if (len == 1)
  3488. return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
  3489. level - 1;
  3490. if (level == 0)
  3491. maxrecs = mp->m_bmap_dmxr[1];
  3492. }
  3493. return rval;
  3494. }
  3495. /*
  3496. * Convert inode from non-attributed to attributed.
  3497. * Must not be in a transaction, ip must not be locked.
  3498. */
  3499. int /* error code */
  3500. xfs_bmap_add_attrfork(
  3501. xfs_inode_t *ip, /* incore inode pointer */
  3502. int size, /* space new attribute needs */
  3503. int rsvd) /* xact may use reserved blks */
  3504. {
  3505. xfs_fsblock_t firstblock; /* 1st block/ag allocated */
  3506. xfs_bmap_free_t flist; /* freed extent records */
  3507. xfs_mount_t *mp; /* mount structure */
  3508. xfs_trans_t *tp; /* transaction pointer */
  3509. int blks; /* space reservation */
  3510. int version = 1; /* superblock attr version */
  3511. int committed; /* xaction was committed */
  3512. int logflags; /* logging flags */
  3513. int error; /* error return value */
  3514. ASSERT(XFS_IFORK_Q(ip) == 0);
  3515. ASSERT(ip->i_df.if_ext_max ==
  3516. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3517. mp = ip->i_mount;
  3518. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  3519. tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
  3520. blks = XFS_ADDAFORK_SPACE_RES(mp);
  3521. if (rsvd)
  3522. tp->t_flags |= XFS_TRANS_RESERVE;
  3523. if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
  3524. XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
  3525. goto error0;
  3526. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3527. error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
  3528. XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  3529. XFS_QMOPT_RES_REGBLKS);
  3530. if (error) {
  3531. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3532. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
  3533. return error;
  3534. }
  3535. if (XFS_IFORK_Q(ip))
  3536. goto error1;
  3537. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
  3538. /*
  3539. * For inodes coming from pre-6.2 filesystems.
  3540. */
  3541. ASSERT(ip->i_d.di_aformat == 0);
  3542. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  3543. }
  3544. ASSERT(ip->i_d.di_anextents == 0);
  3545. IHOLD(ip);
  3546. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3547. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3548. switch (ip->i_d.di_format) {
  3549. case XFS_DINODE_FMT_DEV:
  3550. ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  3551. break;
  3552. case XFS_DINODE_FMT_UUID:
  3553. ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
  3554. break;
  3555. case XFS_DINODE_FMT_LOCAL:
  3556. case XFS_DINODE_FMT_EXTENTS:
  3557. case XFS_DINODE_FMT_BTREE:
  3558. ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
  3559. if (!ip->i_d.di_forkoff)
  3560. ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
  3561. else if (mp->m_flags & XFS_MOUNT_ATTR2)
  3562. version = 2;
  3563. break;
  3564. default:
  3565. ASSERT(0);
  3566. error = XFS_ERROR(EINVAL);
  3567. goto error1;
  3568. }
  3569. ip->i_df.if_ext_max =
  3570. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3571. ASSERT(ip->i_afp == NULL);
  3572. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  3573. ip->i_afp->if_ext_max =
  3574. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  3575. ip->i_afp->if_flags = XFS_IFEXTENTS;
  3576. logflags = 0;
  3577. xfs_bmap_init(&flist, &firstblock);
  3578. switch (ip->i_d.di_format) {
  3579. case XFS_DINODE_FMT_LOCAL:
  3580. error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
  3581. &logflags);
  3582. break;
  3583. case XFS_DINODE_FMT_EXTENTS:
  3584. error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
  3585. &flist, &logflags);
  3586. break;
  3587. case XFS_DINODE_FMT_BTREE:
  3588. error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
  3589. &logflags);
  3590. break;
  3591. default:
  3592. error = 0;
  3593. break;
  3594. }
  3595. if (logflags)
  3596. xfs_trans_log_inode(tp, ip, logflags);
  3597. if (error)
  3598. goto error2;
  3599. if (!xfs_sb_version_hasattr(&mp->m_sb) ||
  3600. (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
  3601. __int64_t sbfields = 0;
  3602. spin_lock(&mp->m_sb_lock);
  3603. if (!xfs_sb_version_hasattr(&mp->m_sb)) {
  3604. xfs_sb_version_addattr(&mp->m_sb);
  3605. sbfields |= XFS_SB_VERSIONNUM;
  3606. }
  3607. if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
  3608. xfs_sb_version_addattr2(&mp->m_sb);
  3609. sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  3610. }
  3611. if (sbfields) {
  3612. spin_unlock(&mp->m_sb_lock);
  3613. xfs_mod_sb(tp, sbfields);
  3614. } else
  3615. spin_unlock(&mp->m_sb_lock);
  3616. }
  3617. if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
  3618. goto error2;
  3619. error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
  3620. ASSERT(ip->i_df.if_ext_max ==
  3621. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3622. return error;
  3623. error2:
  3624. xfs_bmap_cancel(&flist);
  3625. error1:
  3626. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3627. error0:
  3628. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  3629. ASSERT(ip->i_df.if_ext_max ==
  3630. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
  3631. return error;
  3632. }
  3633. /*
  3634. * Add the extent to the list of extents to be free at transaction end.
  3635. * The list is maintained sorted (by block number).
  3636. */
  3637. /* ARGSUSED */
  3638. void
  3639. xfs_bmap_add_free(
  3640. xfs_fsblock_t bno, /* fs block number of extent */
  3641. xfs_filblks_t len, /* length of extent */
  3642. xfs_bmap_free_t *flist, /* list of extents */
  3643. xfs_mount_t *mp) /* mount point structure */
  3644. {
  3645. xfs_bmap_free_item_t *cur; /* current (next) element */
  3646. xfs_bmap_free_item_t *new; /* new element */
  3647. xfs_bmap_free_item_t *prev; /* previous element */
  3648. #ifdef DEBUG
  3649. xfs_agnumber_t agno;
  3650. xfs_agblock_t agbno;
  3651. ASSERT(bno != NULLFSBLOCK);
  3652. ASSERT(len > 0);
  3653. ASSERT(len <= MAXEXTLEN);
  3654. ASSERT(!isnullstartblock(bno));
  3655. agno = XFS_FSB_TO_AGNO(mp, bno);
  3656. agbno = XFS_FSB_TO_AGBNO(mp, bno);
  3657. ASSERT(agno < mp->m_sb.sb_agcount);
  3658. ASSERT(agbno < mp->m_sb.sb_agblocks);
  3659. ASSERT(len < mp->m_sb.sb_agblocks);
  3660. ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
  3661. #endif
  3662. ASSERT(xfs_bmap_free_item_zone != NULL);
  3663. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  3664. new->xbfi_startblock = bno;
  3665. new->xbfi_blockcount = (xfs_extlen_t)len;
  3666. for (prev = NULL, cur = flist->xbf_first;
  3667. cur != NULL;
  3668. prev = cur, cur = cur->xbfi_next) {
  3669. if (cur->xbfi_startblock >= bno)
  3670. break;
  3671. }
  3672. if (prev)
  3673. prev->xbfi_next = new;
  3674. else
  3675. flist->xbf_first = new;
  3676. new->xbfi_next = cur;
  3677. flist->xbf_count++;
  3678. }
  3679. /*
  3680. * Compute and fill in the value of the maximum depth of a bmap btree
  3681. * in this filesystem. Done once, during mount.
  3682. */
  3683. void
  3684. xfs_bmap_compute_maxlevels(
  3685. xfs_mount_t *mp, /* file system mount structure */
  3686. int whichfork) /* data or attr fork */
  3687. {
  3688. int level; /* btree level */
  3689. uint maxblocks; /* max blocks at this level */
  3690. uint maxleafents; /* max leaf entries possible */
  3691. int maxrootrecs; /* max records in root block */
  3692. int minleafrecs; /* min records in leaf block */
  3693. int minnoderecs; /* min records in node block */
  3694. int sz; /* root block size */
  3695. /*
  3696. * The maximum number of extents in a file, hence the maximum
  3697. * number of leaf entries, is controlled by the type of di_nextents
  3698. * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
  3699. * (a signed 16-bit number, xfs_aextnum_t).
  3700. *
  3701. * Note that we can no longer assume that if we are in ATTR1 that
  3702. * the fork offset of all the inodes will be
  3703. * (xfs_default_attroffset(ip) >> 3) because we could have mounted
  3704. * with ATTR2 and then mounted back with ATTR1, keeping the
  3705. * di_forkoff's fixed but probably at various positions. Therefore,
  3706. * for both ATTR1 and ATTR2 we have to assume the worst case scenario
  3707. * of a minimum size available.
  3708. */
  3709. if (whichfork == XFS_DATA_FORK) {
  3710. maxleafents = MAXEXTNUM;
  3711. sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  3712. } else {
  3713. maxleafents = MAXAEXTNUM;
  3714. sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
  3715. }
  3716. maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
  3717. minleafrecs = mp->m_bmap_dmnr[0];
  3718. minnoderecs = mp->m_bmap_dmnr[1];
  3719. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  3720. for (level = 1; maxblocks > 1; level++) {
  3721. if (maxblocks <= maxrootrecs)
  3722. maxblocks = 1;
  3723. else
  3724. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  3725. }
  3726. mp->m_bm_maxlevels[whichfork] = level;
  3727. }
  3728. /*
  3729. * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
  3730. * caller. Frees all the extents that need freeing, which must be done
  3731. * last due to locking considerations. We never free any extents in
  3732. * the first transaction. This is to allow the caller to make the first
  3733. * transaction a synchronous one so that the pointers to the data being
  3734. * broken in this transaction will be permanent before the data is actually
  3735. * freed. This is necessary to prevent blocks from being reallocated
  3736. * and written to before the free and reallocation are actually permanent.
  3737. * We do not just make the first transaction synchronous here, because
  3738. * there are more efficient ways to gain the same protection in some cases
  3739. * (see the file truncation code).
  3740. *
  3741. * Return 1 if the given transaction was committed and a new one
  3742. * started, and 0 otherwise in the committed parameter.
  3743. */
  3744. /*ARGSUSED*/
  3745. int /* error */
  3746. xfs_bmap_finish(
  3747. xfs_trans_t **tp, /* transaction pointer addr */
  3748. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  3749. int *committed) /* xact committed or not */
  3750. {
  3751. xfs_efd_log_item_t *efd; /* extent free data */
  3752. xfs_efi_log_item_t *efi; /* extent free intention */
  3753. int error; /* error return value */
  3754. xfs_bmap_free_item_t *free; /* free extent item */
  3755. unsigned int logres; /* new log reservation */
  3756. unsigned int logcount; /* new log count */
  3757. xfs_mount_t *mp; /* filesystem mount structure */
  3758. xfs_bmap_free_item_t *next; /* next item on free list */
  3759. xfs_trans_t *ntp; /* new transaction pointer */
  3760. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  3761. if (flist->xbf_count == 0) {
  3762. *committed = 0;
  3763. return 0;
  3764. }
  3765. ntp = *tp;
  3766. efi = xfs_trans_get_efi(ntp, flist->xbf_count);
  3767. for (free = flist->xbf_first; free; free = free->xbfi_next)
  3768. xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
  3769. free->xbfi_blockcount);
  3770. logres = ntp->t_log_res;
  3771. logcount = ntp->t_log_count;
  3772. ntp = xfs_trans_dup(*tp);
  3773. error = xfs_trans_commit(*tp, 0);
  3774. *tp = ntp;
  3775. *committed = 1;
  3776. /*
  3777. * We have a new transaction, so we should return committed=1,
  3778. * even though we're returning an error.
  3779. */
  3780. if (error)
  3781. return error;
  3782. /*
  3783. * transaction commit worked ok so we can drop the extra ticket
  3784. * reference that we gained in xfs_trans_dup()
  3785. */
  3786. xfs_log_ticket_put(ntp->t_ticket);
  3787. if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
  3788. logcount)))
  3789. return error;
  3790. efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
  3791. for (free = flist->xbf_first; free != NULL; free = next) {
  3792. next = free->xbfi_next;
  3793. if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
  3794. free->xbfi_blockcount))) {
  3795. /*
  3796. * The bmap free list will be cleaned up at a
  3797. * higher level. The EFI will be canceled when
  3798. * this transaction is aborted.
  3799. * Need to force shutdown here to make sure it
  3800. * happens, since this transaction may not be
  3801. * dirty yet.
  3802. */
  3803. mp = ntp->t_mountp;
  3804. if (!XFS_FORCED_SHUTDOWN(mp))
  3805. xfs_force_shutdown(mp,
  3806. (error == EFSCORRUPTED) ?
  3807. SHUTDOWN_CORRUPT_INCORE :
  3808. SHUTDOWN_META_IO_ERROR);
  3809. return error;
  3810. }
  3811. xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
  3812. free->xbfi_blockcount);
  3813. xfs_bmap_del_free(flist, NULL, free);
  3814. }
  3815. return 0;
  3816. }
  3817. /*
  3818. * Free up any items left in the list.
  3819. */
  3820. void
  3821. xfs_bmap_cancel(
  3822. xfs_bmap_free_t *flist) /* list of bmap_free_items */
  3823. {
  3824. xfs_bmap_free_item_t *free; /* free list item */
  3825. xfs_bmap_free_item_t *next;
  3826. if (flist->xbf_count == 0)
  3827. return;
  3828. ASSERT(flist->xbf_first != NULL);
  3829. for (free = flist->xbf_first; free; free = next) {
  3830. next = free->xbfi_next;
  3831. xfs_bmap_del_free(flist, NULL, free);
  3832. }
  3833. ASSERT(flist->xbf_count == 0);
  3834. }
  3835. /*
  3836. * Returns the file-relative block number of the first unused block(s)
  3837. * in the file with at least "len" logically contiguous blocks free.
  3838. * This is the lowest-address hole if the file has holes, else the first block
  3839. * past the end of file.
  3840. * Return 0 if the file is currently local (in-inode).
  3841. */
  3842. int /* error */
  3843. xfs_bmap_first_unused(
  3844. xfs_trans_t *tp, /* transaction pointer */
  3845. xfs_inode_t *ip, /* incore inode */
  3846. xfs_extlen_t len, /* size of hole to find */
  3847. xfs_fileoff_t *first_unused, /* unused block */
  3848. int whichfork) /* data or attr fork */
  3849. {
  3850. int error; /* error return value */
  3851. int idx; /* extent record index */
  3852. xfs_ifork_t *ifp; /* inode fork pointer */
  3853. xfs_fileoff_t lastaddr; /* last block number seen */
  3854. xfs_fileoff_t lowest; /* lowest useful block */
  3855. xfs_fileoff_t max; /* starting useful block */
  3856. xfs_fileoff_t off; /* offset for this block */
  3857. xfs_extnum_t nextents; /* number of extent entries */
  3858. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
  3859. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
  3860. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
  3861. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3862. *first_unused = 0;
  3863. return 0;
  3864. }
  3865. ifp = XFS_IFORK_PTR(ip, whichfork);
  3866. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3867. (error = xfs_iread_extents(tp, ip, whichfork)))
  3868. return error;
  3869. lowest = *first_unused;
  3870. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3871. for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
  3872. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
  3873. off = xfs_bmbt_get_startoff(ep);
  3874. /*
  3875. * See if the hole before this extent will work.
  3876. */
  3877. if (off >= lowest + len && off - max >= len) {
  3878. *first_unused = max;
  3879. return 0;
  3880. }
  3881. lastaddr = off + xfs_bmbt_get_blockcount(ep);
  3882. max = XFS_FILEOFF_MAX(lastaddr, lowest);
  3883. }
  3884. *first_unused = max;
  3885. return 0;
  3886. }
  3887. /*
  3888. * Returns the file-relative block number of the last block + 1 before
  3889. * last_block (input value) in the file.
  3890. * This is not based on i_size, it is based on the extent records.
  3891. * Returns 0 for local files, as they do not have extent records.
  3892. */
  3893. int /* error */
  3894. xfs_bmap_last_before(
  3895. xfs_trans_t *tp, /* transaction pointer */
  3896. xfs_inode_t *ip, /* incore inode */
  3897. xfs_fileoff_t *last_block, /* last block */
  3898. int whichfork) /* data or attr fork */
  3899. {
  3900. xfs_fileoff_t bno; /* input file offset */
  3901. int eof; /* hit end of file */
  3902. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  3903. int error; /* error return value */
  3904. xfs_bmbt_irec_t got; /* current extent value */
  3905. xfs_ifork_t *ifp; /* inode fork pointer */
  3906. xfs_extnum_t lastx; /* last extent used */
  3907. xfs_bmbt_irec_t prev; /* previous extent value */
  3908. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3909. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3910. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  3911. return XFS_ERROR(EIO);
  3912. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3913. *last_block = 0;
  3914. return 0;
  3915. }
  3916. ifp = XFS_IFORK_PTR(ip, whichfork);
  3917. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3918. (error = xfs_iread_extents(tp, ip, whichfork)))
  3919. return error;
  3920. bno = *last_block - 1;
  3921. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  3922. &prev);
  3923. if (eof || xfs_bmbt_get_startoff(ep) > bno) {
  3924. if (prev.br_startoff == NULLFILEOFF)
  3925. *last_block = 0;
  3926. else
  3927. *last_block = prev.br_startoff + prev.br_blockcount;
  3928. }
  3929. /*
  3930. * Otherwise *last_block is already the right answer.
  3931. */
  3932. return 0;
  3933. }
  3934. /*
  3935. * Returns the file-relative block number of the first block past eof in
  3936. * the file. This is not based on i_size, it is based on the extent records.
  3937. * Returns 0 for local files, as they do not have extent records.
  3938. */
  3939. int /* error */
  3940. xfs_bmap_last_offset(
  3941. xfs_trans_t *tp, /* transaction pointer */
  3942. xfs_inode_t *ip, /* incore inode */
  3943. xfs_fileoff_t *last_block, /* last block */
  3944. int whichfork) /* data or attr fork */
  3945. {
  3946. xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
  3947. int error; /* error return value */
  3948. xfs_ifork_t *ifp; /* inode fork pointer */
  3949. xfs_extnum_t nextents; /* number of extent entries */
  3950. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  3951. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  3952. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
  3953. return XFS_ERROR(EIO);
  3954. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  3955. *last_block = 0;
  3956. return 0;
  3957. }
  3958. ifp = XFS_IFORK_PTR(ip, whichfork);
  3959. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  3960. (error = xfs_iread_extents(tp, ip, whichfork)))
  3961. return error;
  3962. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3963. if (!nextents) {
  3964. *last_block = 0;
  3965. return 0;
  3966. }
  3967. ep = xfs_iext_get_ext(ifp, nextents - 1);
  3968. *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
  3969. return 0;
  3970. }
  3971. /*
  3972. * Returns whether the selected fork of the inode has exactly one
  3973. * block or not. For the data fork we check this matches di_size,
  3974. * implying the file's range is 0..bsize-1.
  3975. */
  3976. int /* 1=>1 block, 0=>otherwise */
  3977. xfs_bmap_one_block(
  3978. xfs_inode_t *ip, /* incore inode */
  3979. int whichfork) /* data or attr fork */
  3980. {
  3981. xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
  3982. xfs_ifork_t *ifp; /* inode fork pointer */
  3983. int rval; /* return value */
  3984. xfs_bmbt_irec_t s; /* internal version of extent */
  3985. #ifndef DEBUG
  3986. if (whichfork == XFS_DATA_FORK) {
  3987. return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
  3988. (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
  3989. (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
  3990. }
  3991. #endif /* !DEBUG */
  3992. if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
  3993. return 0;
  3994. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  3995. return 0;
  3996. ifp = XFS_IFORK_PTR(ip, whichfork);
  3997. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3998. ep = xfs_iext_get_ext(ifp, 0);
  3999. xfs_bmbt_get_all(ep, &s);
  4000. rval = s.br_startoff == 0 && s.br_blockcount == 1;
  4001. if (rval && whichfork == XFS_DATA_FORK)
  4002. ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
  4003. return rval;
  4004. }
  4005. STATIC int
  4006. xfs_bmap_sanity_check(
  4007. struct xfs_mount *mp,
  4008. struct xfs_buf *bp,
  4009. int level)
  4010. {
  4011. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  4012. if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
  4013. be16_to_cpu(block->bb_level) != level ||
  4014. be16_to_cpu(block->bb_numrecs) == 0 ||
  4015. be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
  4016. return 0;
  4017. return 1;
  4018. }
  4019. /*
  4020. * Read in the extents to if_extents.
  4021. * All inode fields are set up by caller, we just traverse the btree
  4022. * and copy the records in. If the file system cannot contain unwritten
  4023. * extents, the records are checked for no "state" flags.
  4024. */
  4025. int /* error */
  4026. xfs_bmap_read_extents(
  4027. xfs_trans_t *tp, /* transaction pointer */
  4028. xfs_inode_t *ip, /* incore inode */
  4029. int whichfork) /* data or attr fork */
  4030. {
  4031. struct xfs_btree_block *block; /* current btree block */
  4032. xfs_fsblock_t bno; /* block # of "block" */
  4033. xfs_buf_t *bp; /* buffer for "block" */
  4034. int error; /* error return value */
  4035. xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
  4036. xfs_extnum_t i, j; /* index into the extents list */
  4037. xfs_ifork_t *ifp; /* fork structure */
  4038. int level; /* btree level, for checking */
  4039. xfs_mount_t *mp; /* file system mount structure */
  4040. __be64 *pp; /* pointer to block address */
  4041. /* REFERENCED */
  4042. xfs_extnum_t room; /* number of entries there's room for */
  4043. bno = NULLFSBLOCK;
  4044. mp = ip->i_mount;
  4045. ifp = XFS_IFORK_PTR(ip, whichfork);
  4046. exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
  4047. XFS_EXTFMT_INODE(ip);
  4048. block = ifp->if_broot;
  4049. /*
  4050. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  4051. */
  4052. level = be16_to_cpu(block->bb_level);
  4053. ASSERT(level > 0);
  4054. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  4055. bno = be64_to_cpu(*pp);
  4056. ASSERT(bno != NULLDFSBNO);
  4057. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  4058. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  4059. /*
  4060. * Go down the tree until leaf level is reached, following the first
  4061. * pointer (leftmost) at each level.
  4062. */
  4063. while (level-- > 0) {
  4064. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4065. XFS_BMAP_BTREE_REF)))
  4066. return error;
  4067. block = XFS_BUF_TO_BLOCK(bp);
  4068. XFS_WANT_CORRUPTED_GOTO(
  4069. xfs_bmap_sanity_check(mp, bp, level),
  4070. error0);
  4071. if (level == 0)
  4072. break;
  4073. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  4074. bno = be64_to_cpu(*pp);
  4075. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  4076. xfs_trans_brelse(tp, bp);
  4077. }
  4078. /*
  4079. * Here with bp and block set to the leftmost leaf node in the tree.
  4080. */
  4081. room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4082. i = 0;
  4083. /*
  4084. * Loop over all leaf nodes. Copy information to the extent records.
  4085. */
  4086. for (;;) {
  4087. xfs_bmbt_rec_t *frp;
  4088. xfs_fsblock_t nextbno;
  4089. xfs_extnum_t num_recs;
  4090. xfs_extnum_t start;
  4091. num_recs = xfs_btree_get_numrecs(block);
  4092. if (unlikely(i + num_recs > room)) {
  4093. ASSERT(i + num_recs <= room);
  4094. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  4095. "corrupt dinode %Lu, (btree extents).",
  4096. (unsigned long long) ip->i_ino);
  4097. XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
  4098. XFS_ERRLEVEL_LOW,
  4099. ip->i_mount);
  4100. goto error0;
  4101. }
  4102. XFS_WANT_CORRUPTED_GOTO(
  4103. xfs_bmap_sanity_check(mp, bp, 0),
  4104. error0);
  4105. /*
  4106. * Read-ahead the next leaf block, if any.
  4107. */
  4108. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  4109. if (nextbno != NULLFSBLOCK)
  4110. xfs_btree_reada_bufl(mp, nextbno, 1);
  4111. /*
  4112. * Copy records into the extent records.
  4113. */
  4114. frp = XFS_BMBT_REC_ADDR(mp, block, 1);
  4115. start = i;
  4116. for (j = 0; j < num_recs; j++, i++, frp++) {
  4117. xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
  4118. trp->l0 = be64_to_cpu(frp->l0);
  4119. trp->l1 = be64_to_cpu(frp->l1);
  4120. }
  4121. if (exntf == XFS_EXTFMT_NOSTATE) {
  4122. /*
  4123. * Check all attribute bmap btree records and
  4124. * any "older" data bmap btree records for a
  4125. * set bit in the "extent flag" position.
  4126. */
  4127. if (unlikely(xfs_check_nostate_extents(ifp,
  4128. start, num_recs))) {
  4129. XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
  4130. XFS_ERRLEVEL_LOW,
  4131. ip->i_mount);
  4132. goto error0;
  4133. }
  4134. }
  4135. xfs_trans_brelse(tp, bp);
  4136. bno = nextbno;
  4137. /*
  4138. * If we've reached the end, stop.
  4139. */
  4140. if (bno == NULLFSBLOCK)
  4141. break;
  4142. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  4143. XFS_BMAP_BTREE_REF)))
  4144. return error;
  4145. block = XFS_BUF_TO_BLOCK(bp);
  4146. }
  4147. ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4148. ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
  4149. XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
  4150. return 0;
  4151. error0:
  4152. xfs_trans_brelse(tp, bp);
  4153. return XFS_ERROR(EFSCORRUPTED);
  4154. }
  4155. #ifdef DEBUG
  4156. /*
  4157. * Add bmap trace insert entries for all the contents of the extent records.
  4158. */
  4159. void
  4160. xfs_bmap_trace_exlist(
  4161. xfs_inode_t *ip, /* incore inode pointer */
  4162. xfs_extnum_t cnt, /* count of entries in the list */
  4163. int whichfork, /* data or attr fork */
  4164. unsigned long caller_ip)
  4165. {
  4166. xfs_extnum_t idx; /* extent record index */
  4167. xfs_ifork_t *ifp; /* inode fork pointer */
  4168. int state = 0;
  4169. if (whichfork == XFS_ATTR_FORK)
  4170. state |= BMAP_ATTRFORK;
  4171. ifp = XFS_IFORK_PTR(ip, whichfork);
  4172. ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
  4173. for (idx = 0; idx < cnt; idx++)
  4174. trace_xfs_extlist(ip, idx, whichfork, caller_ip);
  4175. }
  4176. /*
  4177. * Validate that the bmbt_irecs being returned from bmapi are valid
  4178. * given the callers original parameters. Specifically check the
  4179. * ranges of the returned irecs to ensure that they only extent beyond
  4180. * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
  4181. */
  4182. STATIC void
  4183. xfs_bmap_validate_ret(
  4184. xfs_fileoff_t bno,
  4185. xfs_filblks_t len,
  4186. int flags,
  4187. xfs_bmbt_irec_t *mval,
  4188. int nmap,
  4189. int ret_nmap)
  4190. {
  4191. int i; /* index to map values */
  4192. ASSERT(ret_nmap <= nmap);
  4193. for (i = 0; i < ret_nmap; i++) {
  4194. ASSERT(mval[i].br_blockcount > 0);
  4195. if (!(flags & XFS_BMAPI_ENTIRE)) {
  4196. ASSERT(mval[i].br_startoff >= bno);
  4197. ASSERT(mval[i].br_blockcount <= len);
  4198. ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
  4199. bno + len);
  4200. } else {
  4201. ASSERT(mval[i].br_startoff < bno + len);
  4202. ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
  4203. bno);
  4204. }
  4205. ASSERT(i == 0 ||
  4206. mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
  4207. mval[i].br_startoff);
  4208. if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
  4209. ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
  4210. mval[i].br_startblock != HOLESTARTBLOCK);
  4211. ASSERT(mval[i].br_state == XFS_EXT_NORM ||
  4212. mval[i].br_state == XFS_EXT_UNWRITTEN);
  4213. }
  4214. }
  4215. #endif /* DEBUG */
  4216. /*
  4217. * Map file blocks to filesystem blocks.
  4218. * File range is given by the bno/len pair.
  4219. * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
  4220. * into a hole or past eof.
  4221. * Only allocates blocks from a single allocation group,
  4222. * to avoid locking problems.
  4223. * The returned value in "firstblock" from the first call in a transaction
  4224. * must be remembered and presented to subsequent calls in "firstblock".
  4225. * An upper bound for the number of blocks to be allocated is supplied to
  4226. * the first call in "total"; if no allocation group has that many free
  4227. * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
  4228. */
  4229. int /* error */
  4230. xfs_bmapi(
  4231. xfs_trans_t *tp, /* transaction pointer */
  4232. xfs_inode_t *ip, /* incore inode */
  4233. xfs_fileoff_t bno, /* starting file offs. mapped */
  4234. xfs_filblks_t len, /* length to map in file */
  4235. int flags, /* XFS_BMAPI_... */
  4236. xfs_fsblock_t *firstblock, /* first allocated block
  4237. controls a.g. for allocs */
  4238. xfs_extlen_t total, /* total blocks needed */
  4239. xfs_bmbt_irec_t *mval, /* output: map values */
  4240. int *nmap, /* i/o: mval size/count */
  4241. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4242. xfs_extdelta_t *delta) /* o: change made to incore extents */
  4243. {
  4244. xfs_fsblock_t abno; /* allocated block number */
  4245. xfs_extlen_t alen; /* allocated extent length */
  4246. xfs_fileoff_t aoff; /* allocated file offset */
  4247. xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
  4248. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4249. xfs_fileoff_t end; /* end of mapped file region */
  4250. int eof; /* we've hit the end of extents */
  4251. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4252. int error; /* error return */
  4253. xfs_bmbt_irec_t got; /* current file extent record */
  4254. xfs_ifork_t *ifp; /* inode fork pointer */
  4255. xfs_extlen_t indlen; /* indirect blocks length */
  4256. xfs_extnum_t lastx; /* last useful extent number */
  4257. int logflags; /* flags for transaction logging */
  4258. xfs_extlen_t minleft; /* min blocks left after allocation */
  4259. xfs_extlen_t minlen; /* min allocation size */
  4260. xfs_mount_t *mp; /* xfs mount structure */
  4261. int n; /* current extent index */
  4262. int nallocs; /* number of extents alloc'd */
  4263. xfs_extnum_t nextents; /* number of extents in file */
  4264. xfs_fileoff_t obno; /* old block number (offset) */
  4265. xfs_bmbt_irec_t prev; /* previous file extent record */
  4266. int tmp_logflags; /* temp flags holder */
  4267. int whichfork; /* data or attr fork */
  4268. char inhole; /* current location is hole in file */
  4269. char wasdelay; /* old extent was delayed */
  4270. char wr; /* this is a write request */
  4271. char rt; /* this is a realtime file */
  4272. #ifdef DEBUG
  4273. xfs_fileoff_t orig_bno; /* original block number value */
  4274. int orig_flags; /* original flags arg value */
  4275. xfs_filblks_t orig_len; /* original value of len arg */
  4276. xfs_bmbt_irec_t *orig_mval; /* original value of mval */
  4277. int orig_nmap; /* original value of *nmap */
  4278. orig_bno = bno;
  4279. orig_len = len;
  4280. orig_flags = flags;
  4281. orig_mval = mval;
  4282. orig_nmap = *nmap;
  4283. #endif
  4284. ASSERT(*nmap >= 1);
  4285. ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
  4286. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4287. XFS_ATTR_FORK : XFS_DATA_FORK;
  4288. mp = ip->i_mount;
  4289. if (unlikely(XFS_TEST_ERROR(
  4290. (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4291. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4292. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
  4293. mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
  4294. XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
  4295. return XFS_ERROR(EFSCORRUPTED);
  4296. }
  4297. if (XFS_FORCED_SHUTDOWN(mp))
  4298. return XFS_ERROR(EIO);
  4299. rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4300. ifp = XFS_IFORK_PTR(ip, whichfork);
  4301. ASSERT(ifp->if_ext_max ==
  4302. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4303. if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
  4304. XFS_STATS_INC(xs_blk_mapw);
  4305. else
  4306. XFS_STATS_INC(xs_blk_mapr);
  4307. /*
  4308. * IGSTATE flag is used to combine extents which
  4309. * differ only due to the state of the extents.
  4310. * This technique is used from xfs_getbmap()
  4311. * when the caller does not wish to see the
  4312. * separation (which is the default).
  4313. *
  4314. * This technique is also used when writing a
  4315. * buffer which has been partially written,
  4316. * (usually by being flushed during a chunkread),
  4317. * to ensure one write takes place. This also
  4318. * prevents a change in the xfs inode extents at
  4319. * this time, intentionally. This change occurs
  4320. * on completion of the write operation, in
  4321. * xfs_strat_comp(), where the xfs_bmapi() call
  4322. * is transactioned, and the extents combined.
  4323. */
  4324. if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
  4325. wr = 0; /* no allocations are allowed */
  4326. ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
  4327. logflags = 0;
  4328. nallocs = 0;
  4329. cur = NULL;
  4330. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  4331. ASSERT(wr && tp);
  4332. if ((error = xfs_bmap_local_to_extents(tp, ip,
  4333. firstblock, total, &logflags, whichfork)))
  4334. goto error0;
  4335. }
  4336. if (wr && *firstblock == NULLFSBLOCK) {
  4337. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
  4338. minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
  4339. else
  4340. minleft = 1;
  4341. } else
  4342. minleft = 0;
  4343. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4344. (error = xfs_iread_extents(tp, ip, whichfork)))
  4345. goto error0;
  4346. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4347. &prev);
  4348. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4349. n = 0;
  4350. end = bno + len;
  4351. obno = bno;
  4352. bma.ip = NULL;
  4353. if (delta) {
  4354. delta->xed_startoff = NULLFILEOFF;
  4355. delta->xed_blockcount = 0;
  4356. }
  4357. while (bno < end && n < *nmap) {
  4358. /*
  4359. * Reading past eof, act as though there's a hole
  4360. * up to end.
  4361. */
  4362. if (eof && !wr)
  4363. got.br_startoff = end;
  4364. inhole = eof || got.br_startoff > bno;
  4365. wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
  4366. isnullstartblock(got.br_startblock);
  4367. /*
  4368. * First, deal with the hole before the allocated space
  4369. * that we found, if any.
  4370. */
  4371. if (wr && (inhole || wasdelay)) {
  4372. /*
  4373. * For the wasdelay case, we could also just
  4374. * allocate the stuff asked for in this bmap call
  4375. * but that wouldn't be as good.
  4376. */
  4377. if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
  4378. alen = (xfs_extlen_t)got.br_blockcount;
  4379. aoff = got.br_startoff;
  4380. if (lastx != NULLEXTNUM && lastx) {
  4381. ep = xfs_iext_get_ext(ifp, lastx - 1);
  4382. xfs_bmbt_get_all(ep, &prev);
  4383. }
  4384. } else if (wasdelay) {
  4385. alen = (xfs_extlen_t)
  4386. XFS_FILBLKS_MIN(len,
  4387. (got.br_startoff +
  4388. got.br_blockcount) - bno);
  4389. aoff = bno;
  4390. } else {
  4391. alen = (xfs_extlen_t)
  4392. XFS_FILBLKS_MIN(len, MAXEXTLEN);
  4393. if (!eof)
  4394. alen = (xfs_extlen_t)
  4395. XFS_FILBLKS_MIN(alen,
  4396. got.br_startoff - bno);
  4397. aoff = bno;
  4398. }
  4399. minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
  4400. if (flags & XFS_BMAPI_DELAY) {
  4401. xfs_extlen_t extsz;
  4402. /* Figure out the extent size, adjust alen */
  4403. extsz = xfs_get_extsz_hint(ip);
  4404. if (extsz) {
  4405. error = xfs_bmap_extsize_align(mp,
  4406. &got, &prev, extsz,
  4407. rt, eof,
  4408. flags&XFS_BMAPI_DELAY,
  4409. flags&XFS_BMAPI_CONVERT,
  4410. &aoff, &alen);
  4411. ASSERT(!error);
  4412. }
  4413. if (rt)
  4414. extsz = alen / mp->m_sb.sb_rextsize;
  4415. /*
  4416. * Make a transaction-less quota reservation for
  4417. * delayed allocation blocks. This number gets
  4418. * adjusted later. We return if we haven't
  4419. * allocated blocks already inside this loop.
  4420. */
  4421. error = xfs_trans_reserve_quota_nblks(
  4422. NULL, ip, (long)alen, 0,
  4423. rt ? XFS_QMOPT_RES_RTBLKS :
  4424. XFS_QMOPT_RES_REGBLKS);
  4425. if (error) {
  4426. if (n == 0) {
  4427. *nmap = 0;
  4428. ASSERT(cur == NULL);
  4429. return error;
  4430. }
  4431. break;
  4432. }
  4433. /*
  4434. * Split changing sb for alen and indlen since
  4435. * they could be coming from different places.
  4436. */
  4437. indlen = (xfs_extlen_t)
  4438. xfs_bmap_worst_indlen(ip, alen);
  4439. ASSERT(indlen > 0);
  4440. if (rt) {
  4441. error = xfs_mod_incore_sb(mp,
  4442. XFS_SBS_FREXTENTS,
  4443. -((int64_t)extsz), (flags &
  4444. XFS_BMAPI_RSVBLOCKS));
  4445. } else {
  4446. error = xfs_mod_incore_sb(mp,
  4447. XFS_SBS_FDBLOCKS,
  4448. -((int64_t)alen), (flags &
  4449. XFS_BMAPI_RSVBLOCKS));
  4450. }
  4451. if (!error) {
  4452. error = xfs_mod_incore_sb(mp,
  4453. XFS_SBS_FDBLOCKS,
  4454. -((int64_t)indlen), (flags &
  4455. XFS_BMAPI_RSVBLOCKS));
  4456. if (error && rt)
  4457. xfs_mod_incore_sb(mp,
  4458. XFS_SBS_FREXTENTS,
  4459. (int64_t)extsz, (flags &
  4460. XFS_BMAPI_RSVBLOCKS));
  4461. else if (error)
  4462. xfs_mod_incore_sb(mp,
  4463. XFS_SBS_FDBLOCKS,
  4464. (int64_t)alen, (flags &
  4465. XFS_BMAPI_RSVBLOCKS));
  4466. }
  4467. if (error) {
  4468. if (XFS_IS_QUOTA_ON(mp))
  4469. /* unreserve the blocks now */
  4470. (void)
  4471. xfs_trans_unreserve_quota_nblks(
  4472. NULL, ip,
  4473. (long)alen, 0, rt ?
  4474. XFS_QMOPT_RES_RTBLKS :
  4475. XFS_QMOPT_RES_REGBLKS);
  4476. break;
  4477. }
  4478. ip->i_delayed_blks += alen;
  4479. abno = nullstartblock(indlen);
  4480. } else {
  4481. /*
  4482. * If first time, allocate and fill in
  4483. * once-only bma fields.
  4484. */
  4485. if (bma.ip == NULL) {
  4486. bma.tp = tp;
  4487. bma.ip = ip;
  4488. bma.prevp = &prev;
  4489. bma.gotp = &got;
  4490. bma.total = total;
  4491. bma.userdata = 0;
  4492. }
  4493. /* Indicate if this is the first user data
  4494. * in the file, or just any user data.
  4495. */
  4496. if (!(flags & XFS_BMAPI_METADATA)) {
  4497. bma.userdata = (aoff == 0) ?
  4498. XFS_ALLOC_INITIAL_USER_DATA :
  4499. XFS_ALLOC_USERDATA;
  4500. }
  4501. /*
  4502. * Fill in changeable bma fields.
  4503. */
  4504. bma.eof = eof;
  4505. bma.firstblock = *firstblock;
  4506. bma.alen = alen;
  4507. bma.off = aoff;
  4508. bma.conv = !!(flags & XFS_BMAPI_CONVERT);
  4509. bma.wasdel = wasdelay;
  4510. bma.minlen = minlen;
  4511. bma.low = flist->xbf_low;
  4512. bma.minleft = minleft;
  4513. /*
  4514. * Only want to do the alignment at the
  4515. * eof if it is userdata and allocation length
  4516. * is larger than a stripe unit.
  4517. */
  4518. if (mp->m_dalign && alen >= mp->m_dalign &&
  4519. (!(flags & XFS_BMAPI_METADATA)) &&
  4520. (whichfork == XFS_DATA_FORK)) {
  4521. if ((error = xfs_bmap_isaeof(ip, aoff,
  4522. whichfork, &bma.aeof)))
  4523. goto error0;
  4524. } else
  4525. bma.aeof = 0;
  4526. /*
  4527. * Call allocator.
  4528. */
  4529. if ((error = xfs_bmap_alloc(&bma)))
  4530. goto error0;
  4531. /*
  4532. * Copy out result fields.
  4533. */
  4534. abno = bma.rval;
  4535. if ((flist->xbf_low = bma.low))
  4536. minleft = 0;
  4537. alen = bma.alen;
  4538. aoff = bma.off;
  4539. ASSERT(*firstblock == NULLFSBLOCK ||
  4540. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4541. XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
  4542. (flist->xbf_low &&
  4543. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4544. XFS_FSB_TO_AGNO(mp, bma.firstblock)));
  4545. *firstblock = bma.firstblock;
  4546. if (cur)
  4547. cur->bc_private.b.firstblock =
  4548. *firstblock;
  4549. if (abno == NULLFSBLOCK)
  4550. break;
  4551. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4552. cur = xfs_bmbt_init_cursor(mp, tp,
  4553. ip, whichfork);
  4554. cur->bc_private.b.firstblock =
  4555. *firstblock;
  4556. cur->bc_private.b.flist = flist;
  4557. }
  4558. /*
  4559. * Bump the number of extents we've allocated
  4560. * in this call.
  4561. */
  4562. nallocs++;
  4563. }
  4564. if (cur)
  4565. cur->bc_private.b.flags =
  4566. wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
  4567. got.br_startoff = aoff;
  4568. got.br_startblock = abno;
  4569. got.br_blockcount = alen;
  4570. got.br_state = XFS_EXT_NORM; /* assume normal */
  4571. /*
  4572. * Determine state of extent, and the filesystem.
  4573. * A wasdelay extent has been initialized, so
  4574. * shouldn't be flagged as unwritten.
  4575. */
  4576. if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  4577. if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
  4578. got.br_state = XFS_EXT_UNWRITTEN;
  4579. }
  4580. error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
  4581. firstblock, flist, &tmp_logflags, delta,
  4582. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4583. logflags |= tmp_logflags;
  4584. if (error)
  4585. goto error0;
  4586. lastx = ifp->if_lastex;
  4587. ep = xfs_iext_get_ext(ifp, lastx);
  4588. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4589. xfs_bmbt_get_all(ep, &got);
  4590. ASSERT(got.br_startoff <= aoff);
  4591. ASSERT(got.br_startoff + got.br_blockcount >=
  4592. aoff + alen);
  4593. #ifdef DEBUG
  4594. if (flags & XFS_BMAPI_DELAY) {
  4595. ASSERT(isnullstartblock(got.br_startblock));
  4596. ASSERT(startblockval(got.br_startblock) > 0);
  4597. }
  4598. ASSERT(got.br_state == XFS_EXT_NORM ||
  4599. got.br_state == XFS_EXT_UNWRITTEN);
  4600. #endif
  4601. /*
  4602. * Fall down into the found allocated space case.
  4603. */
  4604. } else if (inhole) {
  4605. /*
  4606. * Reading in a hole.
  4607. */
  4608. mval->br_startoff = bno;
  4609. mval->br_startblock = HOLESTARTBLOCK;
  4610. mval->br_blockcount =
  4611. XFS_FILBLKS_MIN(len, got.br_startoff - bno);
  4612. mval->br_state = XFS_EXT_NORM;
  4613. bno += mval->br_blockcount;
  4614. len -= mval->br_blockcount;
  4615. mval++;
  4616. n++;
  4617. continue;
  4618. }
  4619. /*
  4620. * Then deal with the allocated space we found.
  4621. */
  4622. ASSERT(ep != NULL);
  4623. if (!(flags & XFS_BMAPI_ENTIRE) &&
  4624. (got.br_startoff + got.br_blockcount > obno)) {
  4625. if (obno > bno)
  4626. bno = obno;
  4627. ASSERT((bno >= obno) || (n == 0));
  4628. ASSERT(bno < end);
  4629. mval->br_startoff = bno;
  4630. if (isnullstartblock(got.br_startblock)) {
  4631. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4632. mval->br_startblock = DELAYSTARTBLOCK;
  4633. } else
  4634. mval->br_startblock =
  4635. got.br_startblock +
  4636. (bno - got.br_startoff);
  4637. /*
  4638. * Return the minimum of what we got and what we
  4639. * asked for for the length. We can use the len
  4640. * variable here because it is modified below
  4641. * and we could have been there before coming
  4642. * here if the first part of the allocation
  4643. * didn't overlap what was asked for.
  4644. */
  4645. mval->br_blockcount =
  4646. XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
  4647. (bno - got.br_startoff));
  4648. mval->br_state = got.br_state;
  4649. ASSERT(mval->br_blockcount <= len);
  4650. } else {
  4651. *mval = got;
  4652. if (isnullstartblock(mval->br_startblock)) {
  4653. ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
  4654. mval->br_startblock = DELAYSTARTBLOCK;
  4655. }
  4656. }
  4657. /*
  4658. * Check if writing previously allocated but
  4659. * unwritten extents.
  4660. */
  4661. if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
  4662. ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
  4663. /*
  4664. * Modify (by adding) the state flag, if writing.
  4665. */
  4666. ASSERT(mval->br_blockcount <= len);
  4667. if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
  4668. cur = xfs_bmbt_init_cursor(mp,
  4669. tp, ip, whichfork);
  4670. cur->bc_private.b.firstblock =
  4671. *firstblock;
  4672. cur->bc_private.b.flist = flist;
  4673. }
  4674. mval->br_state = XFS_EXT_NORM;
  4675. error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
  4676. firstblock, flist, &tmp_logflags, delta,
  4677. whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
  4678. logflags |= tmp_logflags;
  4679. if (error)
  4680. goto error0;
  4681. lastx = ifp->if_lastex;
  4682. ep = xfs_iext_get_ext(ifp, lastx);
  4683. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4684. xfs_bmbt_get_all(ep, &got);
  4685. /*
  4686. * We may have combined previously unwritten
  4687. * space with written space, so generate
  4688. * another request.
  4689. */
  4690. if (mval->br_blockcount < len)
  4691. continue;
  4692. }
  4693. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4694. ((mval->br_startoff + mval->br_blockcount) <= end));
  4695. ASSERT((flags & XFS_BMAPI_ENTIRE) ||
  4696. (mval->br_blockcount <= len) ||
  4697. (mval->br_startoff < obno));
  4698. bno = mval->br_startoff + mval->br_blockcount;
  4699. len = end - bno;
  4700. if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
  4701. ASSERT(mval->br_startblock == mval[-1].br_startblock);
  4702. ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
  4703. ASSERT(mval->br_state == mval[-1].br_state);
  4704. mval[-1].br_blockcount = mval->br_blockcount;
  4705. mval[-1].br_state = mval->br_state;
  4706. } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
  4707. mval[-1].br_startblock != DELAYSTARTBLOCK &&
  4708. mval[-1].br_startblock != HOLESTARTBLOCK &&
  4709. mval->br_startblock ==
  4710. mval[-1].br_startblock + mval[-1].br_blockcount &&
  4711. ((flags & XFS_BMAPI_IGSTATE) ||
  4712. mval[-1].br_state == mval->br_state)) {
  4713. ASSERT(mval->br_startoff ==
  4714. mval[-1].br_startoff + mval[-1].br_blockcount);
  4715. mval[-1].br_blockcount += mval->br_blockcount;
  4716. } else if (n > 0 &&
  4717. mval->br_startblock == DELAYSTARTBLOCK &&
  4718. mval[-1].br_startblock == DELAYSTARTBLOCK &&
  4719. mval->br_startoff ==
  4720. mval[-1].br_startoff + mval[-1].br_blockcount) {
  4721. mval[-1].br_blockcount += mval->br_blockcount;
  4722. mval[-1].br_state = mval->br_state;
  4723. } else if (!((n == 0) &&
  4724. ((mval->br_startoff + mval->br_blockcount) <=
  4725. obno))) {
  4726. mval++;
  4727. n++;
  4728. }
  4729. /*
  4730. * If we're done, stop now. Stop when we've allocated
  4731. * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
  4732. * the transaction may get too big.
  4733. */
  4734. if (bno >= end || n >= *nmap || nallocs >= *nmap)
  4735. break;
  4736. /*
  4737. * Else go on to the next record.
  4738. */
  4739. ep = xfs_iext_get_ext(ifp, ++lastx);
  4740. prev = got;
  4741. if (lastx >= nextents)
  4742. eof = 1;
  4743. else
  4744. xfs_bmbt_get_all(ep, &got);
  4745. }
  4746. ifp->if_lastex = lastx;
  4747. *nmap = n;
  4748. /*
  4749. * Transform from btree to extents, give it cur.
  4750. */
  4751. if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  4752. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  4753. ASSERT(wr && cur);
  4754. error = xfs_bmap_btree_to_extents(tp, ip, cur,
  4755. &tmp_logflags, whichfork);
  4756. logflags |= tmp_logflags;
  4757. if (error)
  4758. goto error0;
  4759. }
  4760. ASSERT(ifp->if_ext_max ==
  4761. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4762. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
  4763. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
  4764. error = 0;
  4765. if (delta && delta->xed_startoff != NULLFILEOFF) {
  4766. /* A change was actually made.
  4767. * Note that delta->xed_blockount is an offset at this
  4768. * point and needs to be converted to a block count.
  4769. */
  4770. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  4771. delta->xed_blockcount -= delta->xed_startoff;
  4772. }
  4773. error0:
  4774. /*
  4775. * Log everything. Do this after conversion, there's no point in
  4776. * logging the extent records if we've converted to btree format.
  4777. */
  4778. if ((logflags & xfs_ilog_fext(whichfork)) &&
  4779. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  4780. logflags &= ~xfs_ilog_fext(whichfork);
  4781. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  4782. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  4783. logflags &= ~xfs_ilog_fbroot(whichfork);
  4784. /*
  4785. * Log whatever the flags say, even if error. Otherwise we might miss
  4786. * detecting a case where the data is changed, there's an error,
  4787. * and it's not logged so we don't shutdown when we should.
  4788. */
  4789. if (logflags) {
  4790. ASSERT(tp && wr);
  4791. xfs_trans_log_inode(tp, ip, logflags);
  4792. }
  4793. if (cur) {
  4794. if (!error) {
  4795. ASSERT(*firstblock == NULLFSBLOCK ||
  4796. XFS_FSB_TO_AGNO(mp, *firstblock) ==
  4797. XFS_FSB_TO_AGNO(mp,
  4798. cur->bc_private.b.firstblock) ||
  4799. (flist->xbf_low &&
  4800. XFS_FSB_TO_AGNO(mp, *firstblock) <
  4801. XFS_FSB_TO_AGNO(mp,
  4802. cur->bc_private.b.firstblock)));
  4803. *firstblock = cur->bc_private.b.firstblock;
  4804. }
  4805. xfs_btree_del_cursor(cur,
  4806. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  4807. }
  4808. if (!error)
  4809. xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
  4810. orig_nmap, *nmap);
  4811. return error;
  4812. }
  4813. /*
  4814. * Map file blocks to filesystem blocks, simple version.
  4815. * One block (extent) only, read-only.
  4816. * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
  4817. * For the other flag values, the effect is as if XFS_BMAPI_METADATA
  4818. * was set and all the others were clear.
  4819. */
  4820. int /* error */
  4821. xfs_bmapi_single(
  4822. xfs_trans_t *tp, /* transaction pointer */
  4823. xfs_inode_t *ip, /* incore inode */
  4824. int whichfork, /* data or attr fork */
  4825. xfs_fsblock_t *fsb, /* output: mapped block */
  4826. xfs_fileoff_t bno) /* starting file offs. mapped */
  4827. {
  4828. int eof; /* we've hit the end of extents */
  4829. int error; /* error return */
  4830. xfs_bmbt_irec_t got; /* current file extent record */
  4831. xfs_ifork_t *ifp; /* inode fork pointer */
  4832. xfs_extnum_t lastx; /* last useful extent number */
  4833. xfs_bmbt_irec_t prev; /* previous file extent record */
  4834. ifp = XFS_IFORK_PTR(ip, whichfork);
  4835. if (unlikely(
  4836. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  4837. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
  4838. XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
  4839. ip->i_mount);
  4840. return XFS_ERROR(EFSCORRUPTED);
  4841. }
  4842. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  4843. return XFS_ERROR(EIO);
  4844. XFS_STATS_INC(xs_blk_mapr);
  4845. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4846. (error = xfs_iread_extents(tp, ip, whichfork)))
  4847. return error;
  4848. (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4849. &prev);
  4850. /*
  4851. * Reading past eof, act as though there's a hole
  4852. * up to end.
  4853. */
  4854. if (eof || got.br_startoff > bno) {
  4855. *fsb = NULLFSBLOCK;
  4856. return 0;
  4857. }
  4858. ASSERT(!isnullstartblock(got.br_startblock));
  4859. ASSERT(bno < got.br_startoff + got.br_blockcount);
  4860. *fsb = got.br_startblock + (bno - got.br_startoff);
  4861. ifp->if_lastex = lastx;
  4862. return 0;
  4863. }
  4864. /*
  4865. * Unmap (remove) blocks from a file.
  4866. * If nexts is nonzero then the number of extents to remove is limited to
  4867. * that value. If not all extents in the block range can be removed then
  4868. * *done is set.
  4869. */
  4870. int /* error */
  4871. xfs_bunmapi(
  4872. xfs_trans_t *tp, /* transaction pointer */
  4873. struct xfs_inode *ip, /* incore inode */
  4874. xfs_fileoff_t bno, /* starting offset to unmap */
  4875. xfs_filblks_t len, /* length to unmap in file */
  4876. int flags, /* misc flags */
  4877. xfs_extnum_t nexts, /* number of extents max */
  4878. xfs_fsblock_t *firstblock, /* first allocated block
  4879. controls a.g. for allocs */
  4880. xfs_bmap_free_t *flist, /* i/o: list extents to free */
  4881. xfs_extdelta_t *delta, /* o: change made to incore
  4882. extents */
  4883. int *done) /* set if not done yet */
  4884. {
  4885. xfs_btree_cur_t *cur; /* bmap btree cursor */
  4886. xfs_bmbt_irec_t del; /* extent being deleted */
  4887. int eof; /* is deleting at eof */
  4888. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  4889. int error; /* error return value */
  4890. xfs_extnum_t extno; /* extent number in list */
  4891. xfs_bmbt_irec_t got; /* current extent record */
  4892. xfs_ifork_t *ifp; /* inode fork pointer */
  4893. int isrt; /* freeing in rt area */
  4894. xfs_extnum_t lastx; /* last extent index used */
  4895. int logflags; /* transaction logging flags */
  4896. xfs_extlen_t mod; /* rt extent offset */
  4897. xfs_mount_t *mp; /* mount structure */
  4898. xfs_extnum_t nextents; /* number of file extents */
  4899. xfs_bmbt_irec_t prev; /* previous extent record */
  4900. xfs_fileoff_t start; /* first file offset deleted */
  4901. int tmp_logflags; /* partial logging flags */
  4902. int wasdel; /* was a delayed alloc extent */
  4903. int whichfork; /* data or attribute fork */
  4904. int rsvd; /* OK to allocate reserved blocks */
  4905. xfs_fsblock_t sum;
  4906. trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
  4907. whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
  4908. XFS_ATTR_FORK : XFS_DATA_FORK;
  4909. ifp = XFS_IFORK_PTR(ip, whichfork);
  4910. if (unlikely(
  4911. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
  4912. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  4913. XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
  4914. ip->i_mount);
  4915. return XFS_ERROR(EFSCORRUPTED);
  4916. }
  4917. mp = ip->i_mount;
  4918. if (XFS_FORCED_SHUTDOWN(mp))
  4919. return XFS_ERROR(EIO);
  4920. rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
  4921. ASSERT(len > 0);
  4922. ASSERT(nexts >= 0);
  4923. ASSERT(ifp->if_ext_max ==
  4924. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  4925. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  4926. (error = xfs_iread_extents(tp, ip, whichfork)))
  4927. return error;
  4928. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4929. if (nextents == 0) {
  4930. *done = 1;
  4931. return 0;
  4932. }
  4933. XFS_STATS_INC(xs_blk_unmap);
  4934. isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
  4935. start = bno;
  4936. bno = start + len - 1;
  4937. ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
  4938. &prev);
  4939. if (delta) {
  4940. delta->xed_startoff = NULLFILEOFF;
  4941. delta->xed_blockcount = 0;
  4942. }
  4943. /*
  4944. * Check to see if the given block number is past the end of the
  4945. * file, back up to the last block if so...
  4946. */
  4947. if (eof) {
  4948. ep = xfs_iext_get_ext(ifp, --lastx);
  4949. xfs_bmbt_get_all(ep, &got);
  4950. bno = got.br_startoff + got.br_blockcount - 1;
  4951. }
  4952. logflags = 0;
  4953. if (ifp->if_flags & XFS_IFBROOT) {
  4954. ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
  4955. cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
  4956. cur->bc_private.b.firstblock = *firstblock;
  4957. cur->bc_private.b.flist = flist;
  4958. cur->bc_private.b.flags = 0;
  4959. } else
  4960. cur = NULL;
  4961. extno = 0;
  4962. while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
  4963. (nexts == 0 || extno < nexts)) {
  4964. /*
  4965. * Is the found extent after a hole in which bno lives?
  4966. * Just back up to the previous extent, if so.
  4967. */
  4968. if (got.br_startoff > bno) {
  4969. if (--lastx < 0)
  4970. break;
  4971. ep = xfs_iext_get_ext(ifp, lastx);
  4972. xfs_bmbt_get_all(ep, &got);
  4973. }
  4974. /*
  4975. * Is the last block of this extent before the range
  4976. * we're supposed to delete? If so, we're done.
  4977. */
  4978. bno = XFS_FILEOFF_MIN(bno,
  4979. got.br_startoff + got.br_blockcount - 1);
  4980. if (bno < start)
  4981. break;
  4982. /*
  4983. * Then deal with the (possibly delayed) allocated space
  4984. * we found.
  4985. */
  4986. ASSERT(ep != NULL);
  4987. del = got;
  4988. wasdel = isnullstartblock(del.br_startblock);
  4989. if (got.br_startoff < start) {
  4990. del.br_startoff = start;
  4991. del.br_blockcount -= start - got.br_startoff;
  4992. if (!wasdel)
  4993. del.br_startblock += start - got.br_startoff;
  4994. }
  4995. if (del.br_startoff + del.br_blockcount > bno + 1)
  4996. del.br_blockcount = bno + 1 - del.br_startoff;
  4997. sum = del.br_startblock + del.br_blockcount;
  4998. if (isrt &&
  4999. (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
  5000. /*
  5001. * Realtime extent not lined up at the end.
  5002. * The extent could have been split into written
  5003. * and unwritten pieces, or we could just be
  5004. * unmapping part of it. But we can't really
  5005. * get rid of part of a realtime extent.
  5006. */
  5007. if (del.br_state == XFS_EXT_UNWRITTEN ||
  5008. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5009. /*
  5010. * This piece is unwritten, or we're not
  5011. * using unwritten extents. Skip over it.
  5012. */
  5013. ASSERT(bno >= mod);
  5014. bno -= mod > del.br_blockcount ?
  5015. del.br_blockcount : mod;
  5016. if (bno < got.br_startoff) {
  5017. if (--lastx >= 0)
  5018. xfs_bmbt_get_all(xfs_iext_get_ext(
  5019. ifp, lastx), &got);
  5020. }
  5021. continue;
  5022. }
  5023. /*
  5024. * It's written, turn it unwritten.
  5025. * This is better than zeroing it.
  5026. */
  5027. ASSERT(del.br_state == XFS_EXT_NORM);
  5028. ASSERT(xfs_trans_get_block_res(tp) > 0);
  5029. /*
  5030. * If this spans a realtime extent boundary,
  5031. * chop it back to the start of the one we end at.
  5032. */
  5033. if (del.br_blockcount > mod) {
  5034. del.br_startoff += del.br_blockcount - mod;
  5035. del.br_startblock += del.br_blockcount - mod;
  5036. del.br_blockcount = mod;
  5037. }
  5038. del.br_state = XFS_EXT_UNWRITTEN;
  5039. error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
  5040. firstblock, flist, &logflags, delta,
  5041. XFS_DATA_FORK, 0);
  5042. if (error)
  5043. goto error0;
  5044. goto nodelete;
  5045. }
  5046. if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
  5047. /*
  5048. * Realtime extent is lined up at the end but not
  5049. * at the front. We'll get rid of full extents if
  5050. * we can.
  5051. */
  5052. mod = mp->m_sb.sb_rextsize - mod;
  5053. if (del.br_blockcount > mod) {
  5054. del.br_blockcount -= mod;
  5055. del.br_startoff += mod;
  5056. del.br_startblock += mod;
  5057. } else if ((del.br_startoff == start &&
  5058. (del.br_state == XFS_EXT_UNWRITTEN ||
  5059. xfs_trans_get_block_res(tp) == 0)) ||
  5060. !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  5061. /*
  5062. * Can't make it unwritten. There isn't
  5063. * a full extent here so just skip it.
  5064. */
  5065. ASSERT(bno >= del.br_blockcount);
  5066. bno -= del.br_blockcount;
  5067. if (bno < got.br_startoff) {
  5068. if (--lastx >= 0)
  5069. xfs_bmbt_get_all(--ep, &got);
  5070. }
  5071. continue;
  5072. } else if (del.br_state == XFS_EXT_UNWRITTEN) {
  5073. /*
  5074. * This one is already unwritten.
  5075. * It must have a written left neighbor.
  5076. * Unwrite the killed part of that one and
  5077. * try again.
  5078. */
  5079. ASSERT(lastx > 0);
  5080. xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
  5081. lastx - 1), &prev);
  5082. ASSERT(prev.br_state == XFS_EXT_NORM);
  5083. ASSERT(!isnullstartblock(prev.br_startblock));
  5084. ASSERT(del.br_startblock ==
  5085. prev.br_startblock + prev.br_blockcount);
  5086. if (prev.br_startoff < start) {
  5087. mod = start - prev.br_startoff;
  5088. prev.br_blockcount -= mod;
  5089. prev.br_startblock += mod;
  5090. prev.br_startoff = start;
  5091. }
  5092. prev.br_state = XFS_EXT_UNWRITTEN;
  5093. error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
  5094. &prev, firstblock, flist, &logflags,
  5095. delta, XFS_DATA_FORK, 0);
  5096. if (error)
  5097. goto error0;
  5098. goto nodelete;
  5099. } else {
  5100. ASSERT(del.br_state == XFS_EXT_NORM);
  5101. del.br_state = XFS_EXT_UNWRITTEN;
  5102. error = xfs_bmap_add_extent(ip, lastx, &cur,
  5103. &del, firstblock, flist, &logflags,
  5104. delta, XFS_DATA_FORK, 0);
  5105. if (error)
  5106. goto error0;
  5107. goto nodelete;
  5108. }
  5109. }
  5110. if (wasdel) {
  5111. ASSERT(startblockval(del.br_startblock) > 0);
  5112. /* Update realtime/data freespace, unreserve quota */
  5113. if (isrt) {
  5114. xfs_filblks_t rtexts;
  5115. rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
  5116. do_div(rtexts, mp->m_sb.sb_rextsize);
  5117. xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
  5118. (int64_t)rtexts, rsvd);
  5119. (void)xfs_trans_reserve_quota_nblks(NULL,
  5120. ip, -((long)del.br_blockcount), 0,
  5121. XFS_QMOPT_RES_RTBLKS);
  5122. } else {
  5123. xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
  5124. (int64_t)del.br_blockcount, rsvd);
  5125. (void)xfs_trans_reserve_quota_nblks(NULL,
  5126. ip, -((long)del.br_blockcount), 0,
  5127. XFS_QMOPT_RES_REGBLKS);
  5128. }
  5129. ip->i_delayed_blks -= del.br_blockcount;
  5130. if (cur)
  5131. cur->bc_private.b.flags |=
  5132. XFS_BTCUR_BPRV_WASDEL;
  5133. } else if (cur)
  5134. cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
  5135. /*
  5136. * If it's the case where the directory code is running
  5137. * with no block reservation, and the deleted block is in
  5138. * the middle of its extent, and the resulting insert
  5139. * of an extent would cause transformation to btree format,
  5140. * then reject it. The calling code will then swap
  5141. * blocks around instead.
  5142. * We have to do this now, rather than waiting for the
  5143. * conversion to btree format, since the transaction
  5144. * will be dirty.
  5145. */
  5146. if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
  5147. XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5148. XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
  5149. del.br_startoff > got.br_startoff &&
  5150. del.br_startoff + del.br_blockcount <
  5151. got.br_startoff + got.br_blockcount) {
  5152. error = XFS_ERROR(ENOSPC);
  5153. goto error0;
  5154. }
  5155. error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
  5156. &tmp_logflags, delta, whichfork, rsvd);
  5157. logflags |= tmp_logflags;
  5158. if (error)
  5159. goto error0;
  5160. bno = del.br_startoff - 1;
  5161. nodelete:
  5162. lastx = ifp->if_lastex;
  5163. /*
  5164. * If not done go on to the next (previous) record.
  5165. * Reset ep in case the extents array was re-alloced.
  5166. */
  5167. ep = xfs_iext_get_ext(ifp, lastx);
  5168. if (bno != (xfs_fileoff_t)-1 && bno >= start) {
  5169. if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
  5170. xfs_bmbt_get_startoff(ep) > bno) {
  5171. if (--lastx >= 0)
  5172. ep = xfs_iext_get_ext(ifp, lastx);
  5173. }
  5174. if (lastx >= 0)
  5175. xfs_bmbt_get_all(ep, &got);
  5176. extno++;
  5177. }
  5178. }
  5179. ifp->if_lastex = lastx;
  5180. *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
  5181. ASSERT(ifp->if_ext_max ==
  5182. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5183. /*
  5184. * Convert to a btree if necessary.
  5185. */
  5186. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
  5187. XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
  5188. ASSERT(cur == NULL);
  5189. error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
  5190. &cur, 0, &tmp_logflags, whichfork);
  5191. logflags |= tmp_logflags;
  5192. if (error)
  5193. goto error0;
  5194. }
  5195. /*
  5196. * transform from btree to extents, give it cur
  5197. */
  5198. else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
  5199. XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
  5200. ASSERT(cur != NULL);
  5201. error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
  5202. whichfork);
  5203. logflags |= tmp_logflags;
  5204. if (error)
  5205. goto error0;
  5206. }
  5207. /*
  5208. * transform from extents to local?
  5209. */
  5210. ASSERT(ifp->if_ext_max ==
  5211. XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
  5212. error = 0;
  5213. if (delta && delta->xed_startoff != NULLFILEOFF) {
  5214. /* A change was actually made.
  5215. * Note that delta->xed_blockount is an offset at this
  5216. * point and needs to be converted to a block count.
  5217. */
  5218. ASSERT(delta->xed_blockcount > delta->xed_startoff);
  5219. delta->xed_blockcount -= delta->xed_startoff;
  5220. }
  5221. error0:
  5222. /*
  5223. * Log everything. Do this after conversion, there's no point in
  5224. * logging the extent records if we've converted to btree format.
  5225. */
  5226. if ((logflags & xfs_ilog_fext(whichfork)) &&
  5227. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
  5228. logflags &= ~xfs_ilog_fext(whichfork);
  5229. else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
  5230. XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
  5231. logflags &= ~xfs_ilog_fbroot(whichfork);
  5232. /*
  5233. * Log inode even in the error case, if the transaction
  5234. * is dirty we'll need to shut down the filesystem.
  5235. */
  5236. if (logflags)
  5237. xfs_trans_log_inode(tp, ip, logflags);
  5238. if (cur) {
  5239. if (!error) {
  5240. *firstblock = cur->bc_private.b.firstblock;
  5241. cur->bc_private.b.allocated = 0;
  5242. }
  5243. xfs_btree_del_cursor(cur,
  5244. error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  5245. }
  5246. return error;
  5247. }
  5248. /*
  5249. * returns 1 for success, 0 if we failed to map the extent.
  5250. */
  5251. STATIC int
  5252. xfs_getbmapx_fix_eof_hole(
  5253. xfs_inode_t *ip, /* xfs incore inode pointer */
  5254. struct getbmapx *out, /* output structure */
  5255. int prealloced, /* this is a file with
  5256. * preallocated data space */
  5257. __int64_t end, /* last block requested */
  5258. xfs_fsblock_t startblock)
  5259. {
  5260. __int64_t fixlen;
  5261. xfs_mount_t *mp; /* file system mount point */
  5262. xfs_ifork_t *ifp; /* inode fork pointer */
  5263. xfs_extnum_t lastx; /* last extent pointer */
  5264. xfs_fileoff_t fileblock;
  5265. if (startblock == HOLESTARTBLOCK) {
  5266. mp = ip->i_mount;
  5267. out->bmv_block = -1;
  5268. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
  5269. fixlen -= out->bmv_offset;
  5270. if (prealloced && out->bmv_offset + out->bmv_length == end) {
  5271. /* Came to hole at EOF. Trim it. */
  5272. if (fixlen <= 0)
  5273. return 0;
  5274. out->bmv_length = fixlen;
  5275. }
  5276. } else {
  5277. if (startblock == DELAYSTARTBLOCK)
  5278. out->bmv_block = -2;
  5279. else
  5280. out->bmv_block = xfs_fsb_to_db(ip, startblock);
  5281. fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
  5282. ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
  5283. if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
  5284. (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
  5285. out->bmv_oflags |= BMV_OF_LAST;
  5286. }
  5287. return 1;
  5288. }
  5289. /*
  5290. * Get inode's extents as described in bmv, and format for output.
  5291. * Calls formatter to fill the user's buffer until all extents
  5292. * are mapped, until the passed-in bmv->bmv_count slots have
  5293. * been filled, or until the formatter short-circuits the loop,
  5294. * if it is tracking filled-in extents on its own.
  5295. */
  5296. int /* error code */
  5297. xfs_getbmap(
  5298. xfs_inode_t *ip,
  5299. struct getbmapx *bmv, /* user bmap structure */
  5300. xfs_bmap_format_t formatter, /* format to user */
  5301. void *arg) /* formatter arg */
  5302. {
  5303. __int64_t bmvend; /* last block requested */
  5304. int error = 0; /* return value */
  5305. __int64_t fixlen; /* length for -1 case */
  5306. int i; /* extent number */
  5307. int lock; /* lock state */
  5308. xfs_bmbt_irec_t *map; /* buffer for user's data */
  5309. xfs_mount_t *mp; /* file system mount point */
  5310. int nex; /* # of user extents can do */
  5311. int nexleft; /* # of user extents left */
  5312. int subnex; /* # of bmapi's can do */
  5313. int nmap; /* number of map entries */
  5314. struct getbmapx *out; /* output structure */
  5315. int whichfork; /* data or attr fork */
  5316. int prealloced; /* this is a file with
  5317. * preallocated data space */
  5318. int iflags; /* interface flags */
  5319. int bmapi_flags; /* flags for xfs_bmapi */
  5320. int cur_ext = 0;
  5321. mp = ip->i_mount;
  5322. iflags = bmv->bmv_iflags;
  5323. whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
  5324. if (whichfork == XFS_ATTR_FORK) {
  5325. if (XFS_IFORK_Q(ip)) {
  5326. if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
  5327. ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
  5328. ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
  5329. return XFS_ERROR(EINVAL);
  5330. } else if (unlikely(
  5331. ip->i_d.di_aformat != 0 &&
  5332. ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
  5333. XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
  5334. ip->i_mount);
  5335. return XFS_ERROR(EFSCORRUPTED);
  5336. }
  5337. prealloced = 0;
  5338. fixlen = 1LL << 32;
  5339. } else {
  5340. /*
  5341. * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
  5342. * not generate a DMAPI read event. Otherwise, if the
  5343. * DM_EVENT_READ bit is set for the file, generate a read
  5344. * event in order that the DMAPI application may do its thing
  5345. * before we return the extents. Usually this means restoring
  5346. * user file data to regions of the file that look like holes.
  5347. *
  5348. * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
  5349. * BMV_IF_NO_DMAPI_READ so that read events are generated.
  5350. * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
  5351. * could misinterpret holes in a DMAPI file as true holes,
  5352. * when in fact they may represent offline user data.
  5353. */
  5354. if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
  5355. !(iflags & BMV_IF_NO_DMAPI_READ)) {
  5356. error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
  5357. 0, 0, 0, NULL);
  5358. if (error)
  5359. return XFS_ERROR(error);
  5360. }
  5361. if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
  5362. ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
  5363. ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
  5364. return XFS_ERROR(EINVAL);
  5365. if (xfs_get_extsz_hint(ip) ||
  5366. ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
  5367. prealloced = 1;
  5368. fixlen = XFS_MAXIOFFSET(mp);
  5369. } else {
  5370. prealloced = 0;
  5371. fixlen = ip->i_size;
  5372. }
  5373. }
  5374. if (bmv->bmv_length == -1) {
  5375. fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
  5376. bmv->bmv_length =
  5377. max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
  5378. } else if (bmv->bmv_length == 0) {
  5379. bmv->bmv_entries = 0;
  5380. return 0;
  5381. } else if (bmv->bmv_length < 0) {
  5382. return XFS_ERROR(EINVAL);
  5383. }
  5384. nex = bmv->bmv_count - 1;
  5385. if (nex <= 0)
  5386. return XFS_ERROR(EINVAL);
  5387. bmvend = bmv->bmv_offset + bmv->bmv_length;
  5388. if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
  5389. return XFS_ERROR(ENOMEM);
  5390. out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
  5391. if (!out)
  5392. return XFS_ERROR(ENOMEM);
  5393. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  5394. if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
  5395. if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
  5396. error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
  5397. if (error)
  5398. goto out_unlock_iolock;
  5399. }
  5400. ASSERT(ip->i_delayed_blks == 0);
  5401. }
  5402. lock = xfs_ilock_map_shared(ip);
  5403. /*
  5404. * Don't let nex be bigger than the number of extents
  5405. * we can have assuming alternating holes and real extents.
  5406. */
  5407. if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
  5408. nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
  5409. bmapi_flags = xfs_bmapi_aflag(whichfork);
  5410. if (!(iflags & BMV_IF_PREALLOC))
  5411. bmapi_flags |= XFS_BMAPI_IGSTATE;
  5412. /*
  5413. * Allocate enough space to handle "subnex" maps at a time.
  5414. */
  5415. error = ENOMEM;
  5416. subnex = 16;
  5417. map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
  5418. if (!map)
  5419. goto out_unlock_ilock;
  5420. bmv->bmv_entries = 0;
  5421. if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
  5422. (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
  5423. error = 0;
  5424. goto out_free_map;
  5425. }
  5426. nexleft = nex;
  5427. do {
  5428. nmap = (nexleft > subnex) ? subnex : nexleft;
  5429. error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
  5430. XFS_BB_TO_FSB(mp, bmv->bmv_length),
  5431. bmapi_flags, NULL, 0, map, &nmap,
  5432. NULL, NULL);
  5433. if (error)
  5434. goto out_free_map;
  5435. ASSERT(nmap <= subnex);
  5436. for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
  5437. out[cur_ext].bmv_oflags = 0;
  5438. if (map[i].br_state == XFS_EXT_UNWRITTEN)
  5439. out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
  5440. else if (map[i].br_startblock == DELAYSTARTBLOCK)
  5441. out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
  5442. out[cur_ext].bmv_offset =
  5443. XFS_FSB_TO_BB(mp, map[i].br_startoff);
  5444. out[cur_ext].bmv_length =
  5445. XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  5446. out[cur_ext].bmv_unused1 = 0;
  5447. out[cur_ext].bmv_unused2 = 0;
  5448. ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
  5449. (map[i].br_startblock != DELAYSTARTBLOCK));
  5450. if (map[i].br_startblock == HOLESTARTBLOCK &&
  5451. whichfork == XFS_ATTR_FORK) {
  5452. /* came to the end of attribute fork */
  5453. out[cur_ext].bmv_oflags |= BMV_OF_LAST;
  5454. goto out_free_map;
  5455. }
  5456. if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
  5457. prealloced, bmvend,
  5458. map[i].br_startblock))
  5459. goto out_free_map;
  5460. nexleft--;
  5461. bmv->bmv_offset =
  5462. out[cur_ext].bmv_offset +
  5463. out[cur_ext].bmv_length;
  5464. bmv->bmv_length =
  5465. max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
  5466. bmv->bmv_entries++;
  5467. cur_ext++;
  5468. }
  5469. } while (nmap && nexleft && bmv->bmv_length);
  5470. out_free_map:
  5471. kmem_free(map);
  5472. out_unlock_ilock:
  5473. xfs_iunlock_map_shared(ip, lock);
  5474. out_unlock_iolock:
  5475. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  5476. for (i = 0; i < cur_ext; i++) {
  5477. int full = 0; /* user array is full */
  5478. /* format results & advance arg */
  5479. error = formatter(&arg, &out[i], &full);
  5480. if (error || full)
  5481. break;
  5482. }
  5483. kmem_free(out);
  5484. return error;
  5485. }
  5486. /*
  5487. * Check the last inode extent to determine whether this allocation will result
  5488. * in blocks being allocated at the end of the file. When we allocate new data
  5489. * blocks at the end of the file which do not start at the previous data block,
  5490. * we will try to align the new blocks at stripe unit boundaries.
  5491. */
  5492. STATIC int /* error */
  5493. xfs_bmap_isaeof(
  5494. xfs_inode_t *ip, /* incore inode pointer */
  5495. xfs_fileoff_t off, /* file offset in fsblocks */
  5496. int whichfork, /* data or attribute fork */
  5497. char *aeof) /* return value */
  5498. {
  5499. int error; /* error return value */
  5500. xfs_ifork_t *ifp; /* inode fork pointer */
  5501. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5502. xfs_extnum_t nextents; /* number of file extents */
  5503. xfs_bmbt_irec_t s; /* expanded extent record */
  5504. ASSERT(whichfork == XFS_DATA_FORK);
  5505. ifp = XFS_IFORK_PTR(ip, whichfork);
  5506. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5507. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5508. return error;
  5509. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5510. if (nextents == 0) {
  5511. *aeof = 1;
  5512. return 0;
  5513. }
  5514. /*
  5515. * Go to the last extent
  5516. */
  5517. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5518. xfs_bmbt_get_all(lastrec, &s);
  5519. /*
  5520. * Check we are allocating in the last extent (for delayed allocations)
  5521. * or past the last extent for non-delayed allocations.
  5522. */
  5523. *aeof = (off >= s.br_startoff &&
  5524. off < s.br_startoff + s.br_blockcount &&
  5525. isnullstartblock(s.br_startblock)) ||
  5526. off >= s.br_startoff + s.br_blockcount;
  5527. return 0;
  5528. }
  5529. /*
  5530. * Check if the endoff is outside the last extent. If so the caller will grow
  5531. * the allocation to a stripe unit boundary.
  5532. */
  5533. int /* error */
  5534. xfs_bmap_eof(
  5535. xfs_inode_t *ip, /* incore inode pointer */
  5536. xfs_fileoff_t endoff, /* file offset in fsblocks */
  5537. int whichfork, /* data or attribute fork */
  5538. int *eof) /* result value */
  5539. {
  5540. xfs_fsblock_t blockcount; /* extent block count */
  5541. int error; /* error return value */
  5542. xfs_ifork_t *ifp; /* inode fork pointer */
  5543. xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
  5544. xfs_extnum_t nextents; /* number of file extents */
  5545. xfs_fileoff_t startoff; /* extent starting file offset */
  5546. ASSERT(whichfork == XFS_DATA_FORK);
  5547. ifp = XFS_IFORK_PTR(ip, whichfork);
  5548. if (!(ifp->if_flags & XFS_IFEXTENTS) &&
  5549. (error = xfs_iread_extents(NULL, ip, whichfork)))
  5550. return error;
  5551. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  5552. if (nextents == 0) {
  5553. *eof = 1;
  5554. return 0;
  5555. }
  5556. /*
  5557. * Go to the last extent
  5558. */
  5559. lastrec = xfs_iext_get_ext(ifp, nextents - 1);
  5560. startoff = xfs_bmbt_get_startoff(lastrec);
  5561. blockcount = xfs_bmbt_get_blockcount(lastrec);
  5562. *eof = endoff >= startoff + blockcount;
  5563. return 0;
  5564. }
  5565. #ifdef DEBUG
  5566. STATIC
  5567. xfs_buf_t *
  5568. xfs_bmap_get_bp(
  5569. xfs_btree_cur_t *cur,
  5570. xfs_fsblock_t bno)
  5571. {
  5572. int i;
  5573. xfs_buf_t *bp;
  5574. if (!cur)
  5575. return(NULL);
  5576. bp = NULL;
  5577. for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
  5578. bp = cur->bc_bufs[i];
  5579. if (!bp) break;
  5580. if (XFS_BUF_ADDR(bp) == bno)
  5581. break; /* Found it */
  5582. }
  5583. if (i == XFS_BTREE_MAXLEVELS)
  5584. bp = NULL;
  5585. if (!bp) { /* Chase down all the log items to see if the bp is there */
  5586. xfs_log_item_chunk_t *licp;
  5587. xfs_trans_t *tp;
  5588. tp = cur->bc_tp;
  5589. licp = &tp->t_items;
  5590. while (!bp && licp != NULL) {
  5591. if (xfs_lic_are_all_free(licp)) {
  5592. licp = licp->lic_next;
  5593. continue;
  5594. }
  5595. for (i = 0; i < licp->lic_unused; i++) {
  5596. xfs_log_item_desc_t *lidp;
  5597. xfs_log_item_t *lip;
  5598. xfs_buf_log_item_t *bip;
  5599. xfs_buf_t *lbp;
  5600. if (xfs_lic_isfree(licp, i)) {
  5601. continue;
  5602. }
  5603. lidp = xfs_lic_slot(licp, i);
  5604. lip = lidp->lid_item;
  5605. if (lip->li_type != XFS_LI_BUF)
  5606. continue;
  5607. bip = (xfs_buf_log_item_t *)lip;
  5608. lbp = bip->bli_buf;
  5609. if (XFS_BUF_ADDR(lbp) == bno) {
  5610. bp = lbp;
  5611. break; /* Found it */
  5612. }
  5613. }
  5614. licp = licp->lic_next;
  5615. }
  5616. }
  5617. return(bp);
  5618. }
  5619. STATIC void
  5620. xfs_check_block(
  5621. struct xfs_btree_block *block,
  5622. xfs_mount_t *mp,
  5623. int root,
  5624. short sz)
  5625. {
  5626. int i, j, dmxr;
  5627. __be64 *pp, *thispa; /* pointer to block address */
  5628. xfs_bmbt_key_t *prevp, *keyp;
  5629. ASSERT(be16_to_cpu(block->bb_level) > 0);
  5630. prevp = NULL;
  5631. for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
  5632. dmxr = mp->m_bmap_dmxr[0];
  5633. keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
  5634. if (prevp) {
  5635. ASSERT(be64_to_cpu(prevp->br_startoff) <
  5636. be64_to_cpu(keyp->br_startoff));
  5637. }
  5638. prevp = keyp;
  5639. /*
  5640. * Compare the block numbers to see if there are dups.
  5641. */
  5642. if (root)
  5643. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
  5644. else
  5645. pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
  5646. for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
  5647. if (root)
  5648. thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
  5649. else
  5650. thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
  5651. if (*thispa == *pp) {
  5652. cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
  5653. __func__, j, i,
  5654. (unsigned long long)be64_to_cpu(*thispa));
  5655. panic("%s: ptrs are equal in node\n",
  5656. __func__);
  5657. }
  5658. }
  5659. }
  5660. }
  5661. /*
  5662. * Check that the extents for the inode ip are in the right order in all
  5663. * btree leaves.
  5664. */
  5665. STATIC void
  5666. xfs_bmap_check_leaf_extents(
  5667. xfs_btree_cur_t *cur, /* btree cursor or null */
  5668. xfs_inode_t *ip, /* incore inode pointer */
  5669. int whichfork) /* data or attr fork */
  5670. {
  5671. struct xfs_btree_block *block; /* current btree block */
  5672. xfs_fsblock_t bno; /* block # of "block" */
  5673. xfs_buf_t *bp; /* buffer for "block" */
  5674. int error; /* error return value */
  5675. xfs_extnum_t i=0, j; /* index into the extents list */
  5676. xfs_ifork_t *ifp; /* fork structure */
  5677. int level; /* btree level, for checking */
  5678. xfs_mount_t *mp; /* file system mount structure */
  5679. __be64 *pp; /* pointer to block address */
  5680. xfs_bmbt_rec_t *ep; /* pointer to current extent */
  5681. xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
  5682. xfs_bmbt_rec_t *nextp; /* pointer to next extent */
  5683. int bp_release = 0;
  5684. if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
  5685. return;
  5686. }
  5687. bno = NULLFSBLOCK;
  5688. mp = ip->i_mount;
  5689. ifp = XFS_IFORK_PTR(ip, whichfork);
  5690. block = ifp->if_broot;
  5691. /*
  5692. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5693. */
  5694. level = be16_to_cpu(block->bb_level);
  5695. ASSERT(level > 0);
  5696. xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
  5697. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5698. bno = be64_to_cpu(*pp);
  5699. ASSERT(bno != NULLDFSBNO);
  5700. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5701. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5702. /*
  5703. * Go down the tree until leaf level is reached, following the first
  5704. * pointer (leftmost) at each level.
  5705. */
  5706. while (level-- > 0) {
  5707. /* See if buf is in cur first */
  5708. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5709. if (bp) {
  5710. bp_release = 0;
  5711. } else {
  5712. bp_release = 1;
  5713. }
  5714. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5715. XFS_BMAP_BTREE_REF)))
  5716. goto error_norelse;
  5717. block = XFS_BUF_TO_BLOCK(bp);
  5718. XFS_WANT_CORRUPTED_GOTO(
  5719. xfs_bmap_sanity_check(mp, bp, level),
  5720. error0);
  5721. if (level == 0)
  5722. break;
  5723. /*
  5724. * Check this block for basic sanity (increasing keys and
  5725. * no duplicate blocks).
  5726. */
  5727. xfs_check_block(block, mp, 0, 0);
  5728. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5729. bno = be64_to_cpu(*pp);
  5730. XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
  5731. if (bp_release) {
  5732. bp_release = 0;
  5733. xfs_trans_brelse(NULL, bp);
  5734. }
  5735. }
  5736. /*
  5737. * Here with bp and block set to the leftmost leaf node in the tree.
  5738. */
  5739. i = 0;
  5740. /*
  5741. * Loop over all leaf nodes checking that all extents are in the right order.
  5742. */
  5743. for (;;) {
  5744. xfs_fsblock_t nextbno;
  5745. xfs_extnum_t num_recs;
  5746. num_recs = xfs_btree_get_numrecs(block);
  5747. /*
  5748. * Read-ahead the next leaf block, if any.
  5749. */
  5750. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5751. /*
  5752. * Check all the extents to make sure they are OK.
  5753. * If we had a previous block, the last entry should
  5754. * conform with the first entry in this one.
  5755. */
  5756. ep = XFS_BMBT_REC_ADDR(mp, block, 1);
  5757. if (i) {
  5758. ASSERT(xfs_bmbt_disk_get_startoff(&last) +
  5759. xfs_bmbt_disk_get_blockcount(&last) <=
  5760. xfs_bmbt_disk_get_startoff(ep));
  5761. }
  5762. for (j = 1; j < num_recs; j++) {
  5763. nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
  5764. ASSERT(xfs_bmbt_disk_get_startoff(ep) +
  5765. xfs_bmbt_disk_get_blockcount(ep) <=
  5766. xfs_bmbt_disk_get_startoff(nextp));
  5767. ep = nextp;
  5768. }
  5769. last = *ep;
  5770. i += num_recs;
  5771. if (bp_release) {
  5772. bp_release = 0;
  5773. xfs_trans_brelse(NULL, bp);
  5774. }
  5775. bno = nextbno;
  5776. /*
  5777. * If we've reached the end, stop.
  5778. */
  5779. if (bno == NULLFSBLOCK)
  5780. break;
  5781. bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
  5782. if (bp) {
  5783. bp_release = 0;
  5784. } else {
  5785. bp_release = 1;
  5786. }
  5787. if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
  5788. XFS_BMAP_BTREE_REF)))
  5789. goto error_norelse;
  5790. block = XFS_BUF_TO_BLOCK(bp);
  5791. }
  5792. if (bp_release) {
  5793. bp_release = 0;
  5794. xfs_trans_brelse(NULL, bp);
  5795. }
  5796. return;
  5797. error0:
  5798. cmn_err(CE_WARN, "%s: at error0", __func__);
  5799. if (bp_release)
  5800. xfs_trans_brelse(NULL, bp);
  5801. error_norelse:
  5802. cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
  5803. __func__, i);
  5804. panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
  5805. return;
  5806. }
  5807. #endif
  5808. /*
  5809. * Count fsblocks of the given fork.
  5810. */
  5811. int /* error */
  5812. xfs_bmap_count_blocks(
  5813. xfs_trans_t *tp, /* transaction pointer */
  5814. xfs_inode_t *ip, /* incore inode */
  5815. int whichfork, /* data or attr fork */
  5816. int *count) /* out: count of blocks */
  5817. {
  5818. struct xfs_btree_block *block; /* current btree block */
  5819. xfs_fsblock_t bno; /* block # of "block" */
  5820. xfs_ifork_t *ifp; /* fork structure */
  5821. int level; /* btree level, for checking */
  5822. xfs_mount_t *mp; /* file system mount structure */
  5823. __be64 *pp; /* pointer to block address */
  5824. bno = NULLFSBLOCK;
  5825. mp = ip->i_mount;
  5826. ifp = XFS_IFORK_PTR(ip, whichfork);
  5827. if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
  5828. xfs_bmap_count_leaves(ifp, 0,
  5829. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
  5830. count);
  5831. return 0;
  5832. }
  5833. /*
  5834. * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
  5835. */
  5836. block = ifp->if_broot;
  5837. level = be16_to_cpu(block->bb_level);
  5838. ASSERT(level > 0);
  5839. pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
  5840. bno = be64_to_cpu(*pp);
  5841. ASSERT(bno != NULLDFSBNO);
  5842. ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
  5843. ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
  5844. if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
  5845. XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
  5846. mp);
  5847. return XFS_ERROR(EFSCORRUPTED);
  5848. }
  5849. return 0;
  5850. }
  5851. /*
  5852. * Recursively walks each level of a btree
  5853. * to count total fsblocks is use.
  5854. */
  5855. STATIC int /* error */
  5856. xfs_bmap_count_tree(
  5857. xfs_mount_t *mp, /* file system mount point */
  5858. xfs_trans_t *tp, /* transaction pointer */
  5859. xfs_ifork_t *ifp, /* inode fork pointer */
  5860. xfs_fsblock_t blockno, /* file system block number */
  5861. int levelin, /* level in btree */
  5862. int *count) /* Count of blocks */
  5863. {
  5864. int error;
  5865. xfs_buf_t *bp, *nbp;
  5866. int level = levelin;
  5867. __be64 *pp;
  5868. xfs_fsblock_t bno = blockno;
  5869. xfs_fsblock_t nextbno;
  5870. struct xfs_btree_block *block, *nextblock;
  5871. int numrecs;
  5872. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
  5873. return error;
  5874. *count += 1;
  5875. block = XFS_BUF_TO_BLOCK(bp);
  5876. if (--level) {
  5877. /* Not at node above leaves, count this level of nodes */
  5878. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5879. while (nextbno != NULLFSBLOCK) {
  5880. if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
  5881. 0, &nbp, XFS_BMAP_BTREE_REF)))
  5882. return error;
  5883. *count += 1;
  5884. nextblock = XFS_BUF_TO_BLOCK(nbp);
  5885. nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
  5886. xfs_trans_brelse(tp, nbp);
  5887. }
  5888. /* Dive to the next level */
  5889. pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
  5890. bno = be64_to_cpu(*pp);
  5891. if (unlikely((error =
  5892. xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
  5893. xfs_trans_brelse(tp, bp);
  5894. XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
  5895. XFS_ERRLEVEL_LOW, mp);
  5896. return XFS_ERROR(EFSCORRUPTED);
  5897. }
  5898. xfs_trans_brelse(tp, bp);
  5899. } else {
  5900. /* count all level 1 nodes and their leaves */
  5901. for (;;) {
  5902. nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
  5903. numrecs = be16_to_cpu(block->bb_numrecs);
  5904. xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
  5905. xfs_trans_brelse(tp, bp);
  5906. if (nextbno == NULLFSBLOCK)
  5907. break;
  5908. bno = nextbno;
  5909. if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
  5910. XFS_BMAP_BTREE_REF)))
  5911. return error;
  5912. *count += 1;
  5913. block = XFS_BUF_TO_BLOCK(bp);
  5914. }
  5915. }
  5916. return 0;
  5917. }
  5918. /*
  5919. * Count leaf blocks given a range of extent records.
  5920. */
  5921. STATIC void
  5922. xfs_bmap_count_leaves(
  5923. xfs_ifork_t *ifp,
  5924. xfs_extnum_t idx,
  5925. int numrecs,
  5926. int *count)
  5927. {
  5928. int b;
  5929. for (b = 0; b < numrecs; b++) {
  5930. xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
  5931. *count += xfs_bmbt_get_blockcount(frp);
  5932. }
  5933. }
  5934. /*
  5935. * Count leaf blocks given a range of extent records originally
  5936. * in btree format.
  5937. */
  5938. STATIC void
  5939. xfs_bmap_disk_count_leaves(
  5940. struct xfs_mount *mp,
  5941. struct xfs_btree_block *block,
  5942. int numrecs,
  5943. int *count)
  5944. {
  5945. int b;
  5946. xfs_bmbt_rec_t *frp;
  5947. for (b = 1; b <= numrecs; b++) {
  5948. frp = XFS_BMBT_REC_ADDR(mp, block, b);
  5949. *count += xfs_bmbt_disk_get_blockcount(frp);
  5950. }
  5951. }