xfs_bmap.c 178 KB

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