xfs_bmap.c 197 KB

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