xfs_bmap.c 167 KB

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