xfs_bmap.c 164 KB

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