xfs_bmap.c 194 KB

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