xfs_bmap.c 184 KB

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