xfs_bmap.c 170 KB

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