xfs_bmap.c 154 KB

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