xfs_bmap.c 190 KB

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