xfs_bmap.c 196 KB

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