xfs_bmap.c 182 KB

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