xfs_bmap.c 171 KB

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