xfs_bmap.c 192 KB

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