xfs_bmap.c 174 KB

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