xfs_bmap.c 167 KB

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