xfs_bmap.c 197 KB

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