xfs_bmap.c 188 KB

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