xfs_bmap.c 178 KB

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