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