xfs_bmap.c 171 KB

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