xfs_bmap.c 194 KB

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