xfs_bmap.c 175 KB

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