xfs_bmap.c 198 KB

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