xfs_bmap.c 170 KB

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