xfs_bmap.c 182 KB

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