xfs_bmap.c 171 KB

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