xfs_bmap.c 180 KB

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