xfs_alloc.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_rmap.h"
  19. #include "xfs_alloc_btree.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_extent_busy.h"
  22. #include "xfs_errortag.h"
  23. #include "xfs_error.h"
  24. #include "xfs_cksum.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_trans.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_log.h"
  29. #include "xfs_ag_resv.h"
  30. #include "xfs_bmap.h"
  31. extern kmem_zone_t *xfs_bmap_free_item_zone;
  32. struct workqueue_struct *xfs_alloc_wq;
  33. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  34. #define XFSA_FIXUP_BNO_OK 1
  35. #define XFSA_FIXUP_CNT_OK 2
  36. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  37. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  38. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  39. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  40. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  41. /*
  42. * Size of the AGFL. For CRC-enabled filesystes we steal a couple of slots in
  43. * the beginning of the block for a proper header with the location information
  44. * and CRC.
  45. */
  46. unsigned int
  47. xfs_agfl_size(
  48. struct xfs_mount *mp)
  49. {
  50. unsigned int size = mp->m_sb.sb_sectsize;
  51. if (xfs_sb_version_hascrc(&mp->m_sb))
  52. size -= sizeof(struct xfs_agfl);
  53. return size / sizeof(xfs_agblock_t);
  54. }
  55. unsigned int
  56. xfs_refc_block(
  57. struct xfs_mount *mp)
  58. {
  59. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  60. return XFS_RMAP_BLOCK(mp) + 1;
  61. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  62. return XFS_FIBT_BLOCK(mp) + 1;
  63. return XFS_IBT_BLOCK(mp) + 1;
  64. }
  65. xfs_extlen_t
  66. xfs_prealloc_blocks(
  67. struct xfs_mount *mp)
  68. {
  69. if (xfs_sb_version_hasreflink(&mp->m_sb))
  70. return xfs_refc_block(mp) + 1;
  71. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  72. return XFS_RMAP_BLOCK(mp) + 1;
  73. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  74. return XFS_FIBT_BLOCK(mp) + 1;
  75. return XFS_IBT_BLOCK(mp) + 1;
  76. }
  77. /*
  78. * In order to avoid ENOSPC-related deadlock caused by out-of-order locking of
  79. * AGF buffer (PV 947395), we place constraints on the relationship among
  80. * actual allocations for data blocks, freelist blocks, and potential file data
  81. * bmap btree blocks. However, these restrictions may result in no actual space
  82. * allocated for a delayed extent, for example, a data block in a certain AG is
  83. * allocated but there is no additional block for the additional bmap btree
  84. * block due to a split of the bmap btree of the file. The result of this may
  85. * lead to an infinite loop when the file gets flushed to disk and all delayed
  86. * extents need to be actually allocated. To get around this, we explicitly set
  87. * aside a few blocks which will not be reserved in delayed allocation.
  88. *
  89. * We need to reserve 4 fsbs _per AG_ for the freelist and 4 more to handle a
  90. * potential split of the file's bmap btree.
  91. */
  92. unsigned int
  93. xfs_alloc_set_aside(
  94. struct xfs_mount *mp)
  95. {
  96. return mp->m_sb.sb_agcount * (XFS_ALLOC_AGFL_RESERVE + 4);
  97. }
  98. /*
  99. * When deciding how much space to allocate out of an AG, we limit the
  100. * allocation maximum size to the size the AG. However, we cannot use all the
  101. * blocks in the AG - some are permanently used by metadata. These
  102. * blocks are generally:
  103. * - the AG superblock, AGF, AGI and AGFL
  104. * - the AGF (bno and cnt) and AGI btree root blocks, and optionally
  105. * the AGI free inode and rmap btree root blocks.
  106. * - blocks on the AGFL according to xfs_alloc_set_aside() limits
  107. * - the rmapbt root block
  108. *
  109. * The AG headers are sector sized, so the amount of space they take up is
  110. * dependent on filesystem geometry. The others are all single blocks.
  111. */
  112. unsigned int
  113. xfs_alloc_ag_max_usable(
  114. struct xfs_mount *mp)
  115. {
  116. unsigned int blocks;
  117. blocks = XFS_BB_TO_FSB(mp, XFS_FSS_TO_BB(mp, 4)); /* ag headers */
  118. blocks += XFS_ALLOC_AGFL_RESERVE;
  119. blocks += 3; /* AGF, AGI btree root blocks */
  120. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  121. blocks++; /* finobt root block */
  122. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  123. blocks++; /* rmap root block */
  124. if (xfs_sb_version_hasreflink(&mp->m_sb))
  125. blocks++; /* refcount root block */
  126. return mp->m_sb.sb_agblocks - blocks;
  127. }
  128. /*
  129. * Lookup the record equal to [bno, len] in the btree given by cur.
  130. */
  131. STATIC int /* error */
  132. xfs_alloc_lookup_eq(
  133. struct xfs_btree_cur *cur, /* btree cursor */
  134. xfs_agblock_t bno, /* starting block of extent */
  135. xfs_extlen_t len, /* length of extent */
  136. int *stat) /* success/failure */
  137. {
  138. cur->bc_rec.a.ar_startblock = bno;
  139. cur->bc_rec.a.ar_blockcount = len;
  140. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  141. }
  142. /*
  143. * Lookup the first record greater than or equal to [bno, len]
  144. * in the btree given by cur.
  145. */
  146. int /* error */
  147. xfs_alloc_lookup_ge(
  148. struct xfs_btree_cur *cur, /* btree cursor */
  149. xfs_agblock_t bno, /* starting block of extent */
  150. xfs_extlen_t len, /* length of extent */
  151. int *stat) /* success/failure */
  152. {
  153. cur->bc_rec.a.ar_startblock = bno;
  154. cur->bc_rec.a.ar_blockcount = len;
  155. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  156. }
  157. /*
  158. * Lookup the first record less than or equal to [bno, len]
  159. * in the btree given by cur.
  160. */
  161. int /* error */
  162. xfs_alloc_lookup_le(
  163. struct xfs_btree_cur *cur, /* btree cursor */
  164. xfs_agblock_t bno, /* starting block of extent */
  165. xfs_extlen_t len, /* length of extent */
  166. int *stat) /* success/failure */
  167. {
  168. cur->bc_rec.a.ar_startblock = bno;
  169. cur->bc_rec.a.ar_blockcount = len;
  170. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  171. }
  172. /*
  173. * Update the record referred to by cur to the value given
  174. * by [bno, len].
  175. * This either works (return 0) or gets an EFSCORRUPTED error.
  176. */
  177. STATIC int /* error */
  178. xfs_alloc_update(
  179. struct xfs_btree_cur *cur, /* btree cursor */
  180. xfs_agblock_t bno, /* starting block of extent */
  181. xfs_extlen_t len) /* length of extent */
  182. {
  183. union xfs_btree_rec rec;
  184. rec.alloc.ar_startblock = cpu_to_be32(bno);
  185. rec.alloc.ar_blockcount = cpu_to_be32(len);
  186. return xfs_btree_update(cur, &rec);
  187. }
  188. /*
  189. * Get the data from the pointed-to record.
  190. */
  191. int /* error */
  192. xfs_alloc_get_rec(
  193. struct xfs_btree_cur *cur, /* btree cursor */
  194. xfs_agblock_t *bno, /* output: starting block of extent */
  195. xfs_extlen_t *len, /* output: length of extent */
  196. int *stat) /* output: success/failure */
  197. {
  198. struct xfs_mount *mp = cur->bc_mp;
  199. xfs_agnumber_t agno = cur->bc_private.a.agno;
  200. union xfs_btree_rec *rec;
  201. int error;
  202. error = xfs_btree_get_rec(cur, &rec, stat);
  203. if (error || !(*stat))
  204. return error;
  205. *bno = be32_to_cpu(rec->alloc.ar_startblock);
  206. *len = be32_to_cpu(rec->alloc.ar_blockcount);
  207. if (*len == 0)
  208. goto out_bad_rec;
  209. /* check for valid extent range, including overflow */
  210. if (!xfs_verify_agbno(mp, agno, *bno))
  211. goto out_bad_rec;
  212. if (*bno > *bno + *len)
  213. goto out_bad_rec;
  214. if (!xfs_verify_agbno(mp, agno, *bno + *len - 1))
  215. goto out_bad_rec;
  216. return 0;
  217. out_bad_rec:
  218. xfs_warn(mp,
  219. "%s Freespace BTree record corruption in AG %d detected!",
  220. cur->bc_btnum == XFS_BTNUM_BNO ? "Block" : "Size", agno);
  221. xfs_warn(mp,
  222. "start block 0x%x block count 0x%x", *bno, *len);
  223. return -EFSCORRUPTED;
  224. }
  225. /*
  226. * Compute aligned version of the found extent.
  227. * Takes alignment and min length into account.
  228. */
  229. STATIC bool
  230. xfs_alloc_compute_aligned(
  231. xfs_alloc_arg_t *args, /* allocation argument structure */
  232. xfs_agblock_t foundbno, /* starting block in found extent */
  233. xfs_extlen_t foundlen, /* length in found extent */
  234. xfs_agblock_t *resbno, /* result block number */
  235. xfs_extlen_t *reslen, /* result length */
  236. unsigned *busy_gen)
  237. {
  238. xfs_agblock_t bno = foundbno;
  239. xfs_extlen_t len = foundlen;
  240. xfs_extlen_t diff;
  241. bool busy;
  242. /* Trim busy sections out of found extent */
  243. busy = xfs_extent_busy_trim(args, &bno, &len, busy_gen);
  244. /*
  245. * If we have a largish extent that happens to start before min_agbno,
  246. * see if we can shift it into range...
  247. */
  248. if (bno < args->min_agbno && bno + len > args->min_agbno) {
  249. diff = args->min_agbno - bno;
  250. if (len > diff) {
  251. bno += diff;
  252. len -= diff;
  253. }
  254. }
  255. if (args->alignment > 1 && len >= args->minlen) {
  256. xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
  257. diff = aligned_bno - bno;
  258. *resbno = aligned_bno;
  259. *reslen = diff >= len ? 0 : len - diff;
  260. } else {
  261. *resbno = bno;
  262. *reslen = len;
  263. }
  264. return busy;
  265. }
  266. /*
  267. * Compute best start block and diff for "near" allocations.
  268. * freelen >= wantlen already checked by caller.
  269. */
  270. STATIC xfs_extlen_t /* difference value (absolute) */
  271. xfs_alloc_compute_diff(
  272. xfs_agblock_t wantbno, /* target starting block */
  273. xfs_extlen_t wantlen, /* target length */
  274. xfs_extlen_t alignment, /* target alignment */
  275. int datatype, /* are we allocating data? */
  276. xfs_agblock_t freebno, /* freespace's starting block */
  277. xfs_extlen_t freelen, /* freespace's length */
  278. xfs_agblock_t *newbnop) /* result: best start block from free */
  279. {
  280. xfs_agblock_t freeend; /* end of freespace extent */
  281. xfs_agblock_t newbno1; /* return block number */
  282. xfs_agblock_t newbno2; /* other new block number */
  283. xfs_extlen_t newlen1=0; /* length with newbno1 */
  284. xfs_extlen_t newlen2=0; /* length with newbno2 */
  285. xfs_agblock_t wantend; /* end of target extent */
  286. bool userdata = xfs_alloc_is_userdata(datatype);
  287. ASSERT(freelen >= wantlen);
  288. freeend = freebno + freelen;
  289. wantend = wantbno + wantlen;
  290. /*
  291. * We want to allocate from the start of a free extent if it is past
  292. * the desired block or if we are allocating user data and the free
  293. * extent is before desired block. The second case is there to allow
  294. * for contiguous allocation from the remaining free space if the file
  295. * grows in the short term.
  296. */
  297. if (freebno >= wantbno || (userdata && freeend < wantend)) {
  298. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  299. newbno1 = NULLAGBLOCK;
  300. } else if (freeend >= wantend && alignment > 1) {
  301. newbno1 = roundup(wantbno, alignment);
  302. newbno2 = newbno1 - alignment;
  303. if (newbno1 >= freeend)
  304. newbno1 = NULLAGBLOCK;
  305. else
  306. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  307. if (newbno2 < freebno)
  308. newbno2 = NULLAGBLOCK;
  309. else
  310. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  311. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  312. if (newlen1 < newlen2 ||
  313. (newlen1 == newlen2 &&
  314. XFS_ABSDIFF(newbno1, wantbno) >
  315. XFS_ABSDIFF(newbno2, wantbno)))
  316. newbno1 = newbno2;
  317. } else if (newbno2 != NULLAGBLOCK)
  318. newbno1 = newbno2;
  319. } else if (freeend >= wantend) {
  320. newbno1 = wantbno;
  321. } else if (alignment > 1) {
  322. newbno1 = roundup(freeend - wantlen, alignment);
  323. if (newbno1 > freeend - wantlen &&
  324. newbno1 - alignment >= freebno)
  325. newbno1 -= alignment;
  326. else if (newbno1 >= freeend)
  327. newbno1 = NULLAGBLOCK;
  328. } else
  329. newbno1 = freeend - wantlen;
  330. *newbnop = newbno1;
  331. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  332. }
  333. /*
  334. * Fix up the length, based on mod and prod.
  335. * len should be k * prod + mod for some k.
  336. * If len is too small it is returned unchanged.
  337. * If len hits maxlen it is left alone.
  338. */
  339. STATIC void
  340. xfs_alloc_fix_len(
  341. xfs_alloc_arg_t *args) /* allocation argument structure */
  342. {
  343. xfs_extlen_t k;
  344. xfs_extlen_t rlen;
  345. ASSERT(args->mod < args->prod);
  346. rlen = args->len;
  347. ASSERT(rlen >= args->minlen);
  348. ASSERT(rlen <= args->maxlen);
  349. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  350. (args->mod == 0 && rlen < args->prod))
  351. return;
  352. k = rlen % args->prod;
  353. if (k == args->mod)
  354. return;
  355. if (k > args->mod)
  356. rlen = rlen - (k - args->mod);
  357. else
  358. rlen = rlen - args->prod + (args->mod - k);
  359. /* casts to (int) catch length underflows */
  360. if ((int)rlen < (int)args->minlen)
  361. return;
  362. ASSERT(rlen >= args->minlen && rlen <= args->maxlen);
  363. ASSERT(rlen % args->prod == args->mod);
  364. ASSERT(args->pag->pagf_freeblks + args->pag->pagf_flcount >=
  365. rlen + args->minleft);
  366. args->len = rlen;
  367. }
  368. /*
  369. * Update the two btrees, logically removing from freespace the extent
  370. * starting at rbno, rlen blocks. The extent is contained within the
  371. * actual (current) free extent fbno for flen blocks.
  372. * Flags are passed in indicating whether the cursors are set to the
  373. * relevant records.
  374. */
  375. STATIC int /* error code */
  376. xfs_alloc_fixup_trees(
  377. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  378. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  379. xfs_agblock_t fbno, /* starting block of free extent */
  380. xfs_extlen_t flen, /* length of free extent */
  381. xfs_agblock_t rbno, /* starting block of returned extent */
  382. xfs_extlen_t rlen, /* length of returned extent */
  383. int flags) /* flags, XFSA_FIXUP_... */
  384. {
  385. int error; /* error code */
  386. int i; /* operation results */
  387. xfs_agblock_t nfbno1; /* first new free startblock */
  388. xfs_agblock_t nfbno2; /* second new free startblock */
  389. xfs_extlen_t nflen1=0; /* first new free length */
  390. xfs_extlen_t nflen2=0; /* second new free length */
  391. struct xfs_mount *mp;
  392. mp = cnt_cur->bc_mp;
  393. /*
  394. * Look up the record in the by-size tree if necessary.
  395. */
  396. if (flags & XFSA_FIXUP_CNT_OK) {
  397. #ifdef DEBUG
  398. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  399. return error;
  400. XFS_WANT_CORRUPTED_RETURN(mp,
  401. i == 1 && nfbno1 == fbno && nflen1 == flen);
  402. #endif
  403. } else {
  404. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  405. return error;
  406. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  407. }
  408. /*
  409. * Look up the record in the by-block tree if necessary.
  410. */
  411. if (flags & XFSA_FIXUP_BNO_OK) {
  412. #ifdef DEBUG
  413. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  414. return error;
  415. XFS_WANT_CORRUPTED_RETURN(mp,
  416. i == 1 && nfbno1 == fbno && nflen1 == flen);
  417. #endif
  418. } else {
  419. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  420. return error;
  421. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  422. }
  423. #ifdef DEBUG
  424. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  425. struct xfs_btree_block *bnoblock;
  426. struct xfs_btree_block *cntblock;
  427. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
  428. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
  429. XFS_WANT_CORRUPTED_RETURN(mp,
  430. bnoblock->bb_numrecs == cntblock->bb_numrecs);
  431. }
  432. #endif
  433. /*
  434. * Deal with all four cases: the allocated record is contained
  435. * within the freespace record, so we can have new freespace
  436. * at either (or both) end, or no freespace remaining.
  437. */
  438. if (rbno == fbno && rlen == flen)
  439. nfbno1 = nfbno2 = NULLAGBLOCK;
  440. else if (rbno == fbno) {
  441. nfbno1 = rbno + rlen;
  442. nflen1 = flen - rlen;
  443. nfbno2 = NULLAGBLOCK;
  444. } else if (rbno + rlen == fbno + flen) {
  445. nfbno1 = fbno;
  446. nflen1 = flen - rlen;
  447. nfbno2 = NULLAGBLOCK;
  448. } else {
  449. nfbno1 = fbno;
  450. nflen1 = rbno - fbno;
  451. nfbno2 = rbno + rlen;
  452. nflen2 = (fbno + flen) - nfbno2;
  453. }
  454. /*
  455. * Delete the entry from the by-size btree.
  456. */
  457. if ((error = xfs_btree_delete(cnt_cur, &i)))
  458. return error;
  459. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  460. /*
  461. * Add new by-size btree entry(s).
  462. */
  463. if (nfbno1 != NULLAGBLOCK) {
  464. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  465. return error;
  466. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  467. if ((error = xfs_btree_insert(cnt_cur, &i)))
  468. return error;
  469. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  470. }
  471. if (nfbno2 != NULLAGBLOCK) {
  472. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  473. return error;
  474. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  475. if ((error = xfs_btree_insert(cnt_cur, &i)))
  476. return error;
  477. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  478. }
  479. /*
  480. * Fix up the by-block btree entry(s).
  481. */
  482. if (nfbno1 == NULLAGBLOCK) {
  483. /*
  484. * No remaining freespace, just delete the by-block tree entry.
  485. */
  486. if ((error = xfs_btree_delete(bno_cur, &i)))
  487. return error;
  488. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  489. } else {
  490. /*
  491. * Update the by-block entry to start later|be shorter.
  492. */
  493. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  494. return error;
  495. }
  496. if (nfbno2 != NULLAGBLOCK) {
  497. /*
  498. * 2 resulting free entries, need to add one.
  499. */
  500. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  501. return error;
  502. XFS_WANT_CORRUPTED_RETURN(mp, i == 0);
  503. if ((error = xfs_btree_insert(bno_cur, &i)))
  504. return error;
  505. XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
  506. }
  507. return 0;
  508. }
  509. static xfs_failaddr_t
  510. xfs_agfl_verify(
  511. struct xfs_buf *bp)
  512. {
  513. struct xfs_mount *mp = bp->b_target->bt_mount;
  514. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  515. int i;
  516. /*
  517. * There is no verification of non-crc AGFLs because mkfs does not
  518. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  519. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  520. * can't verify just those entries are valid.
  521. */
  522. if (!xfs_sb_version_hascrc(&mp->m_sb))
  523. return NULL;
  524. if (!uuid_equal(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid))
  525. return __this_address;
  526. if (be32_to_cpu(agfl->agfl_magicnum) != XFS_AGFL_MAGIC)
  527. return __this_address;
  528. /*
  529. * during growfs operations, the perag is not fully initialised,
  530. * so we can't use it for any useful checking. growfs ensures we can't
  531. * use it by using uncached buffers that don't have the perag attached
  532. * so we can detect and avoid this problem.
  533. */
  534. if (bp->b_pag && be32_to_cpu(agfl->agfl_seqno) != bp->b_pag->pag_agno)
  535. return __this_address;
  536. for (i = 0; i < xfs_agfl_size(mp); i++) {
  537. if (be32_to_cpu(agfl->agfl_bno[i]) != NULLAGBLOCK &&
  538. be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
  539. return __this_address;
  540. }
  541. if (!xfs_log_check_lsn(mp, be64_to_cpu(XFS_BUF_TO_AGFL(bp)->agfl_lsn)))
  542. return __this_address;
  543. return NULL;
  544. }
  545. static void
  546. xfs_agfl_read_verify(
  547. struct xfs_buf *bp)
  548. {
  549. struct xfs_mount *mp = bp->b_target->bt_mount;
  550. xfs_failaddr_t fa;
  551. /*
  552. * There is no verification of non-crc AGFLs because mkfs does not
  553. * initialise the AGFL to zero or NULL. Hence the only valid part of the
  554. * AGFL is what the AGF says is active. We can't get to the AGF, so we
  555. * can't verify just those entries are valid.
  556. */
  557. if (!xfs_sb_version_hascrc(&mp->m_sb))
  558. return;
  559. if (!xfs_buf_verify_cksum(bp, XFS_AGFL_CRC_OFF))
  560. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  561. else {
  562. fa = xfs_agfl_verify(bp);
  563. if (fa)
  564. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  565. }
  566. }
  567. static void
  568. xfs_agfl_write_verify(
  569. struct xfs_buf *bp)
  570. {
  571. struct xfs_mount *mp = bp->b_target->bt_mount;
  572. struct xfs_buf_log_item *bip = bp->b_log_item;
  573. xfs_failaddr_t fa;
  574. /* no verification of non-crc AGFLs */
  575. if (!xfs_sb_version_hascrc(&mp->m_sb))
  576. return;
  577. fa = xfs_agfl_verify(bp);
  578. if (fa) {
  579. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  580. return;
  581. }
  582. if (bip)
  583. XFS_BUF_TO_AGFL(bp)->agfl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  584. xfs_buf_update_cksum(bp, XFS_AGFL_CRC_OFF);
  585. }
  586. const struct xfs_buf_ops xfs_agfl_buf_ops = {
  587. .name = "xfs_agfl",
  588. .verify_read = xfs_agfl_read_verify,
  589. .verify_write = xfs_agfl_write_verify,
  590. .verify_struct = xfs_agfl_verify,
  591. };
  592. /*
  593. * Read in the allocation group free block array.
  594. */
  595. int /* error */
  596. xfs_alloc_read_agfl(
  597. xfs_mount_t *mp, /* mount point structure */
  598. xfs_trans_t *tp, /* transaction pointer */
  599. xfs_agnumber_t agno, /* allocation group number */
  600. xfs_buf_t **bpp) /* buffer for the ag free block array */
  601. {
  602. xfs_buf_t *bp; /* return value */
  603. int error;
  604. ASSERT(agno != NULLAGNUMBER);
  605. error = xfs_trans_read_buf(
  606. mp, tp, mp->m_ddev_targp,
  607. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  608. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
  609. if (error)
  610. return error;
  611. xfs_buf_set_ref(bp, XFS_AGFL_REF);
  612. *bpp = bp;
  613. return 0;
  614. }
  615. STATIC int
  616. xfs_alloc_update_counters(
  617. struct xfs_trans *tp,
  618. struct xfs_perag *pag,
  619. struct xfs_buf *agbp,
  620. long len)
  621. {
  622. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  623. pag->pagf_freeblks += len;
  624. be32_add_cpu(&agf->agf_freeblks, len);
  625. xfs_trans_agblocks_delta(tp, len);
  626. if (unlikely(be32_to_cpu(agf->agf_freeblks) >
  627. be32_to_cpu(agf->agf_length)))
  628. return -EFSCORRUPTED;
  629. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  630. return 0;
  631. }
  632. /*
  633. * Allocation group level functions.
  634. */
  635. /*
  636. * Allocate a variable extent in the allocation group agno.
  637. * Type and bno are used to determine where in the allocation group the
  638. * extent will start.
  639. * Extent's length (returned in *len) will be between minlen and maxlen,
  640. * and of the form k * prod + mod unless there's nothing that large.
  641. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  642. */
  643. STATIC int /* error */
  644. xfs_alloc_ag_vextent(
  645. xfs_alloc_arg_t *args) /* argument structure for allocation */
  646. {
  647. int error=0;
  648. ASSERT(args->minlen > 0);
  649. ASSERT(args->maxlen > 0);
  650. ASSERT(args->minlen <= args->maxlen);
  651. ASSERT(args->mod < args->prod);
  652. ASSERT(args->alignment > 0);
  653. /*
  654. * Branch to correct routine based on the type.
  655. */
  656. args->wasfromfl = 0;
  657. switch (args->type) {
  658. case XFS_ALLOCTYPE_THIS_AG:
  659. error = xfs_alloc_ag_vextent_size(args);
  660. break;
  661. case XFS_ALLOCTYPE_NEAR_BNO:
  662. error = xfs_alloc_ag_vextent_near(args);
  663. break;
  664. case XFS_ALLOCTYPE_THIS_BNO:
  665. error = xfs_alloc_ag_vextent_exact(args);
  666. break;
  667. default:
  668. ASSERT(0);
  669. /* NOTREACHED */
  670. }
  671. if (error || args->agbno == NULLAGBLOCK)
  672. return error;
  673. ASSERT(args->len >= args->minlen);
  674. ASSERT(args->len <= args->maxlen);
  675. ASSERT(!args->wasfromfl || args->resv != XFS_AG_RESV_AGFL);
  676. ASSERT(args->agbno % args->alignment == 0);
  677. /* if not file data, insert new block into the reverse map btree */
  678. if (!xfs_rmap_should_skip_owner_update(&args->oinfo)) {
  679. error = xfs_rmap_alloc(args->tp, args->agbp, args->agno,
  680. args->agbno, args->len, &args->oinfo);
  681. if (error)
  682. return error;
  683. }
  684. if (!args->wasfromfl) {
  685. error = xfs_alloc_update_counters(args->tp, args->pag,
  686. args->agbp,
  687. -((long)(args->len)));
  688. if (error)
  689. return error;
  690. ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
  691. args->agbno, args->len));
  692. }
  693. xfs_ag_resv_alloc_extent(args->pag, args->resv, args);
  694. XFS_STATS_INC(args->mp, xs_allocx);
  695. XFS_STATS_ADD(args->mp, xs_allocb, args->len);
  696. return error;
  697. }
  698. /*
  699. * Allocate a variable extent at exactly agno/bno.
  700. * Extent's length (returned in *len) will be between minlen and maxlen,
  701. * and of the form k * prod + mod unless there's nothing that large.
  702. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  703. */
  704. STATIC int /* error */
  705. xfs_alloc_ag_vextent_exact(
  706. xfs_alloc_arg_t *args) /* allocation argument structure */
  707. {
  708. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  709. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  710. int error;
  711. xfs_agblock_t fbno; /* start block of found extent */
  712. xfs_extlen_t flen; /* length of found extent */
  713. xfs_agblock_t tbno; /* start block of busy extent */
  714. xfs_extlen_t tlen; /* length of busy extent */
  715. xfs_agblock_t tend; /* end block of busy extent */
  716. int i; /* success/failure of operation */
  717. unsigned busy_gen;
  718. ASSERT(args->alignment == 1);
  719. /*
  720. * Allocate/initialize a cursor for the by-number freespace btree.
  721. */
  722. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  723. args->agno, XFS_BTNUM_BNO);
  724. /*
  725. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  726. * Look for the closest free block <= bno, it must contain bno
  727. * if any free block does.
  728. */
  729. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  730. if (error)
  731. goto error0;
  732. if (!i)
  733. goto not_found;
  734. /*
  735. * Grab the freespace record.
  736. */
  737. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  738. if (error)
  739. goto error0;
  740. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  741. ASSERT(fbno <= args->agbno);
  742. /*
  743. * Check for overlapping busy extents.
  744. */
  745. tbno = fbno;
  746. tlen = flen;
  747. xfs_extent_busy_trim(args, &tbno, &tlen, &busy_gen);
  748. /*
  749. * Give up if the start of the extent is busy, or the freespace isn't
  750. * long enough for the minimum request.
  751. */
  752. if (tbno > args->agbno)
  753. goto not_found;
  754. if (tlen < args->minlen)
  755. goto not_found;
  756. tend = tbno + tlen;
  757. if (tend < args->agbno + args->minlen)
  758. goto not_found;
  759. /*
  760. * End of extent will be smaller of the freespace end and the
  761. * maximal requested end.
  762. *
  763. * Fix the length according to mod and prod if given.
  764. */
  765. args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
  766. - args->agbno;
  767. xfs_alloc_fix_len(args);
  768. ASSERT(args->agbno + args->len <= tend);
  769. /*
  770. * We are allocating agbno for args->len
  771. * Allocate/initialize a cursor for the by-size btree.
  772. */
  773. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  774. args->agno, XFS_BTNUM_CNT);
  775. ASSERT(args->agbno + args->len <=
  776. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  777. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  778. args->len, XFSA_FIXUP_BNO_OK);
  779. if (error) {
  780. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  781. goto error0;
  782. }
  783. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  784. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  785. args->wasfromfl = 0;
  786. trace_xfs_alloc_exact_done(args);
  787. return 0;
  788. not_found:
  789. /* Didn't find it, return null. */
  790. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  791. args->agbno = NULLAGBLOCK;
  792. trace_xfs_alloc_exact_notfound(args);
  793. return 0;
  794. error0:
  795. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  796. trace_xfs_alloc_exact_error(args);
  797. return error;
  798. }
  799. /*
  800. * Search the btree in a given direction via the search cursor and compare
  801. * the records found against the good extent we've already found.
  802. */
  803. STATIC int
  804. xfs_alloc_find_best_extent(
  805. struct xfs_alloc_arg *args, /* allocation argument structure */
  806. struct xfs_btree_cur **gcur, /* good cursor */
  807. struct xfs_btree_cur **scur, /* searching cursor */
  808. xfs_agblock_t gdiff, /* difference for search comparison */
  809. xfs_agblock_t *sbno, /* extent found by search */
  810. xfs_extlen_t *slen, /* extent length */
  811. xfs_agblock_t *sbnoa, /* aligned extent found by search */
  812. xfs_extlen_t *slena, /* aligned extent length */
  813. int dir) /* 0 = search right, 1 = search left */
  814. {
  815. xfs_agblock_t new;
  816. xfs_agblock_t sdiff;
  817. int error;
  818. int i;
  819. unsigned busy_gen;
  820. /* The good extent is perfect, no need to search. */
  821. if (!gdiff)
  822. goto out_use_good;
  823. /*
  824. * Look until we find a better one, run out of space or run off the end.
  825. */
  826. do {
  827. error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
  828. if (error)
  829. goto error0;
  830. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  831. xfs_alloc_compute_aligned(args, *sbno, *slen,
  832. sbnoa, slena, &busy_gen);
  833. /*
  834. * The good extent is closer than this one.
  835. */
  836. if (!dir) {
  837. if (*sbnoa > args->max_agbno)
  838. goto out_use_good;
  839. if (*sbnoa >= args->agbno + gdiff)
  840. goto out_use_good;
  841. } else {
  842. if (*sbnoa < args->min_agbno)
  843. goto out_use_good;
  844. if (*sbnoa <= args->agbno - gdiff)
  845. goto out_use_good;
  846. }
  847. /*
  848. * Same distance, compare length and pick the best.
  849. */
  850. if (*slena >= args->minlen) {
  851. args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
  852. xfs_alloc_fix_len(args);
  853. sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  854. args->alignment,
  855. args->datatype, *sbnoa,
  856. *slena, &new);
  857. /*
  858. * Choose closer size and invalidate other cursor.
  859. */
  860. if (sdiff < gdiff)
  861. goto out_use_search;
  862. goto out_use_good;
  863. }
  864. if (!dir)
  865. error = xfs_btree_increment(*scur, 0, &i);
  866. else
  867. error = xfs_btree_decrement(*scur, 0, &i);
  868. if (error)
  869. goto error0;
  870. } while (i);
  871. out_use_good:
  872. xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
  873. *scur = NULL;
  874. return 0;
  875. out_use_search:
  876. xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
  877. *gcur = NULL;
  878. return 0;
  879. error0:
  880. /* caller invalidates cursors */
  881. return error;
  882. }
  883. /*
  884. * Allocate a variable extent near bno in the allocation group agno.
  885. * Extent's length (returned in len) will be between minlen and maxlen,
  886. * and of the form k * prod + mod unless there's nothing that large.
  887. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  888. */
  889. STATIC int /* error */
  890. xfs_alloc_ag_vextent_near(
  891. xfs_alloc_arg_t *args) /* allocation argument structure */
  892. {
  893. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  894. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  895. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  896. xfs_agblock_t gtbno; /* start bno of right side entry */
  897. xfs_agblock_t gtbnoa; /* aligned ... */
  898. xfs_extlen_t gtdiff; /* difference to right side entry */
  899. xfs_extlen_t gtlen; /* length of right side entry */
  900. xfs_extlen_t gtlena; /* aligned ... */
  901. xfs_agblock_t gtnew; /* useful start bno of right side */
  902. int error; /* error code */
  903. int i; /* result code, temporary */
  904. int j; /* result code, temporary */
  905. xfs_agblock_t ltbno; /* start bno of left side entry */
  906. xfs_agblock_t ltbnoa; /* aligned ... */
  907. xfs_extlen_t ltdiff; /* difference to left side entry */
  908. xfs_extlen_t ltlen; /* length of left side entry */
  909. xfs_extlen_t ltlena; /* aligned ... */
  910. xfs_agblock_t ltnew; /* useful start bno of left side */
  911. xfs_extlen_t rlen; /* length of returned extent */
  912. bool busy;
  913. unsigned busy_gen;
  914. #ifdef DEBUG
  915. /*
  916. * Randomly don't execute the first algorithm.
  917. */
  918. int dofirst; /* set to do first algorithm */
  919. dofirst = prandom_u32() & 1;
  920. #endif
  921. /* handle unitialized agbno range so caller doesn't have to */
  922. if (!args->min_agbno && !args->max_agbno)
  923. args->max_agbno = args->mp->m_sb.sb_agblocks - 1;
  924. ASSERT(args->min_agbno <= args->max_agbno);
  925. /* clamp agbno to the range if it's outside */
  926. if (args->agbno < args->min_agbno)
  927. args->agbno = args->min_agbno;
  928. if (args->agbno > args->max_agbno)
  929. args->agbno = args->max_agbno;
  930. restart:
  931. bno_cur_lt = NULL;
  932. bno_cur_gt = NULL;
  933. ltlen = 0;
  934. gtlena = 0;
  935. ltlena = 0;
  936. busy = false;
  937. /*
  938. * Get a cursor for the by-size btree.
  939. */
  940. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  941. args->agno, XFS_BTNUM_CNT);
  942. /*
  943. * See if there are any free extents as big as maxlen.
  944. */
  945. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  946. goto error0;
  947. /*
  948. * If none, then pick up the last entry in the tree unless the
  949. * tree is empty.
  950. */
  951. if (!i) {
  952. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  953. &ltlen, &i)))
  954. goto error0;
  955. if (i == 0 || ltlen == 0) {
  956. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  957. trace_xfs_alloc_near_noentry(args);
  958. return 0;
  959. }
  960. ASSERT(i == 1);
  961. }
  962. args->wasfromfl = 0;
  963. /*
  964. * First algorithm.
  965. * If the requested extent is large wrt the freespaces available
  966. * in this a.g., then the cursor will be pointing to a btree entry
  967. * near the right edge of the tree. If it's in the last btree leaf
  968. * block, then we just examine all the entries in that block
  969. * that are big enough, and pick the best one.
  970. * This is written as a while loop so we can break out of it,
  971. * but we never loop back to the top.
  972. */
  973. while (xfs_btree_islastblock(cnt_cur, 0)) {
  974. xfs_extlen_t bdiff;
  975. int besti=0;
  976. xfs_extlen_t blen=0;
  977. xfs_agblock_t bnew=0;
  978. #ifdef DEBUG
  979. if (dofirst)
  980. break;
  981. #endif
  982. /*
  983. * Start from the entry that lookup found, sequence through
  984. * all larger free blocks. If we're actually pointing at a
  985. * record smaller than maxlen, go to the start of this block,
  986. * and skip all those smaller than minlen.
  987. */
  988. if (ltlen || args->alignment > 1) {
  989. cnt_cur->bc_ptrs[0] = 1;
  990. do {
  991. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  992. &ltlen, &i)))
  993. goto error0;
  994. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  995. if (ltlen >= args->minlen)
  996. break;
  997. if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
  998. goto error0;
  999. } while (i);
  1000. ASSERT(ltlen >= args->minlen);
  1001. if (!i)
  1002. break;
  1003. }
  1004. i = cnt_cur->bc_ptrs[0];
  1005. for (j = 1, blen = 0, bdiff = 0;
  1006. !error && j && (blen < args->maxlen || bdiff > 0);
  1007. error = xfs_btree_increment(cnt_cur, 0, &j)) {
  1008. /*
  1009. * For each entry, decide if it's better than
  1010. * the previous best entry.
  1011. */
  1012. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1013. goto error0;
  1014. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1015. busy = xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1016. &ltbnoa, &ltlena, &busy_gen);
  1017. if (ltlena < args->minlen)
  1018. continue;
  1019. if (ltbnoa < args->min_agbno || ltbnoa > args->max_agbno)
  1020. continue;
  1021. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1022. xfs_alloc_fix_len(args);
  1023. ASSERT(args->len >= args->minlen);
  1024. if (args->len < blen)
  1025. continue;
  1026. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1027. args->alignment, args->datatype, ltbnoa,
  1028. ltlena, &ltnew);
  1029. if (ltnew != NULLAGBLOCK &&
  1030. (args->len > blen || ltdiff < bdiff)) {
  1031. bdiff = ltdiff;
  1032. bnew = ltnew;
  1033. blen = args->len;
  1034. besti = cnt_cur->bc_ptrs[0];
  1035. }
  1036. }
  1037. /*
  1038. * It didn't work. We COULD be in a case where
  1039. * there's a good record somewhere, so try again.
  1040. */
  1041. if (blen == 0)
  1042. break;
  1043. /*
  1044. * Point at the best entry, and retrieve it again.
  1045. */
  1046. cnt_cur->bc_ptrs[0] = besti;
  1047. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  1048. goto error0;
  1049. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1050. ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1051. args->len = blen;
  1052. /*
  1053. * We are allocating starting at bnew for blen blocks.
  1054. */
  1055. args->agbno = bnew;
  1056. ASSERT(bnew >= ltbno);
  1057. ASSERT(bnew + blen <= ltbno + ltlen);
  1058. /*
  1059. * Set up a cursor for the by-bno tree.
  1060. */
  1061. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
  1062. args->agbp, args->agno, XFS_BTNUM_BNO);
  1063. /*
  1064. * Fix up the btree entries.
  1065. */
  1066. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  1067. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  1068. goto error0;
  1069. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1070. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1071. trace_xfs_alloc_near_first(args);
  1072. return 0;
  1073. }
  1074. /*
  1075. * Second algorithm.
  1076. * Search in the by-bno tree to the left and to the right
  1077. * simultaneously, until in each case we find a space big enough,
  1078. * or run into the edge of the tree. When we run into the edge,
  1079. * we deallocate that cursor.
  1080. * If both searches succeed, we compare the two spaces and pick
  1081. * the better one.
  1082. * With alignment, it's possible for both to fail; the upper
  1083. * level algorithm that picks allocation groups for allocations
  1084. * is not supposed to do this.
  1085. */
  1086. /*
  1087. * Allocate and initialize the cursor for the leftward search.
  1088. */
  1089. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1090. args->agno, XFS_BTNUM_BNO);
  1091. /*
  1092. * Lookup <= bno to find the leftward search's starting point.
  1093. */
  1094. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  1095. goto error0;
  1096. if (!i) {
  1097. /*
  1098. * Didn't find anything; use this cursor for the rightward
  1099. * search.
  1100. */
  1101. bno_cur_gt = bno_cur_lt;
  1102. bno_cur_lt = NULL;
  1103. }
  1104. /*
  1105. * Found something. Duplicate the cursor for the rightward search.
  1106. */
  1107. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  1108. goto error0;
  1109. /*
  1110. * Increment the cursor, so we will point at the entry just right
  1111. * of the leftward entry if any, or to the leftmost entry.
  1112. */
  1113. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1114. goto error0;
  1115. if (!i) {
  1116. /*
  1117. * It failed, there are no rightward entries.
  1118. */
  1119. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  1120. bno_cur_gt = NULL;
  1121. }
  1122. /*
  1123. * Loop going left with the leftward cursor, right with the
  1124. * rightward cursor, until either both directions give up or
  1125. * we find an entry at least as big as minlen.
  1126. */
  1127. do {
  1128. if (bno_cur_lt) {
  1129. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  1130. goto error0;
  1131. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1132. busy |= xfs_alloc_compute_aligned(args, ltbno, ltlen,
  1133. &ltbnoa, &ltlena, &busy_gen);
  1134. if (ltlena >= args->minlen && ltbnoa >= args->min_agbno)
  1135. break;
  1136. if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
  1137. goto error0;
  1138. if (!i || ltbnoa < args->min_agbno) {
  1139. xfs_btree_del_cursor(bno_cur_lt,
  1140. XFS_BTREE_NOERROR);
  1141. bno_cur_lt = NULL;
  1142. }
  1143. }
  1144. if (bno_cur_gt) {
  1145. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  1146. goto error0;
  1147. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1148. busy |= xfs_alloc_compute_aligned(args, gtbno, gtlen,
  1149. &gtbnoa, &gtlena, &busy_gen);
  1150. if (gtlena >= args->minlen && gtbnoa <= args->max_agbno)
  1151. break;
  1152. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  1153. goto error0;
  1154. if (!i || gtbnoa > args->max_agbno) {
  1155. xfs_btree_del_cursor(bno_cur_gt,
  1156. XFS_BTREE_NOERROR);
  1157. bno_cur_gt = NULL;
  1158. }
  1159. }
  1160. } while (bno_cur_lt || bno_cur_gt);
  1161. /*
  1162. * Got both cursors still active, need to find better entry.
  1163. */
  1164. if (bno_cur_lt && bno_cur_gt) {
  1165. if (ltlena >= args->minlen) {
  1166. /*
  1167. * Left side is good, look for a right side entry.
  1168. */
  1169. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1170. xfs_alloc_fix_len(args);
  1171. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1172. args->alignment, args->datatype, ltbnoa,
  1173. ltlena, &ltnew);
  1174. error = xfs_alloc_find_best_extent(args,
  1175. &bno_cur_lt, &bno_cur_gt,
  1176. ltdiff, &gtbno, &gtlen,
  1177. &gtbnoa, &gtlena,
  1178. 0 /* search right */);
  1179. } else {
  1180. ASSERT(gtlena >= args->minlen);
  1181. /*
  1182. * Right side is good, look for a left side entry.
  1183. */
  1184. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  1185. xfs_alloc_fix_len(args);
  1186. gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1187. args->alignment, args->datatype, gtbnoa,
  1188. gtlena, &gtnew);
  1189. error = xfs_alloc_find_best_extent(args,
  1190. &bno_cur_gt, &bno_cur_lt,
  1191. gtdiff, &ltbno, &ltlen,
  1192. &ltbnoa, &ltlena,
  1193. 1 /* search left */);
  1194. }
  1195. if (error)
  1196. goto error0;
  1197. }
  1198. /*
  1199. * If we couldn't get anything, give up.
  1200. */
  1201. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1202. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1203. if (busy) {
  1204. trace_xfs_alloc_near_busy(args);
  1205. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1206. goto restart;
  1207. }
  1208. trace_xfs_alloc_size_neither(args);
  1209. args->agbno = NULLAGBLOCK;
  1210. return 0;
  1211. }
  1212. /*
  1213. * At this point we have selected a freespace entry, either to the
  1214. * left or to the right. If it's on the right, copy all the
  1215. * useful variables to the "left" set so we only have one
  1216. * copy of this code.
  1217. */
  1218. if (bno_cur_gt) {
  1219. bno_cur_lt = bno_cur_gt;
  1220. bno_cur_gt = NULL;
  1221. ltbno = gtbno;
  1222. ltbnoa = gtbnoa;
  1223. ltlen = gtlen;
  1224. ltlena = gtlena;
  1225. j = 1;
  1226. } else
  1227. j = 0;
  1228. /*
  1229. * Fix up the length and compute the useful address.
  1230. */
  1231. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1232. xfs_alloc_fix_len(args);
  1233. rlen = args->len;
  1234. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
  1235. args->datatype, ltbnoa, ltlena, &ltnew);
  1236. ASSERT(ltnew >= ltbno);
  1237. ASSERT(ltnew + rlen <= ltbnoa + ltlena);
  1238. ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1239. ASSERT(ltnew >= args->min_agbno && ltnew <= args->max_agbno);
  1240. args->agbno = ltnew;
  1241. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1242. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1243. goto error0;
  1244. if (j)
  1245. trace_xfs_alloc_near_greater(args);
  1246. else
  1247. trace_xfs_alloc_near_lesser(args);
  1248. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1249. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1250. return 0;
  1251. error0:
  1252. trace_xfs_alloc_near_error(args);
  1253. if (cnt_cur != NULL)
  1254. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1255. if (bno_cur_lt != NULL)
  1256. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1257. if (bno_cur_gt != NULL)
  1258. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1259. return error;
  1260. }
  1261. /*
  1262. * Allocate a variable extent anywhere in the allocation group agno.
  1263. * Extent's length (returned in len) will be between minlen and maxlen,
  1264. * and of the form k * prod + mod unless there's nothing that large.
  1265. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1266. */
  1267. STATIC int /* error */
  1268. xfs_alloc_ag_vextent_size(
  1269. xfs_alloc_arg_t *args) /* allocation argument structure */
  1270. {
  1271. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1272. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1273. int error; /* error result */
  1274. xfs_agblock_t fbno; /* start of found freespace */
  1275. xfs_extlen_t flen; /* length of found freespace */
  1276. int i; /* temp status variable */
  1277. xfs_agblock_t rbno; /* returned block number */
  1278. xfs_extlen_t rlen; /* length of returned extent */
  1279. bool busy;
  1280. unsigned busy_gen;
  1281. restart:
  1282. /*
  1283. * Allocate and initialize a cursor for the by-size btree.
  1284. */
  1285. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1286. args->agno, XFS_BTNUM_CNT);
  1287. bno_cur = NULL;
  1288. busy = false;
  1289. /*
  1290. * Look for an entry >= maxlen+alignment-1 blocks.
  1291. */
  1292. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1293. args->maxlen + args->alignment - 1, &i)))
  1294. goto error0;
  1295. /*
  1296. * If none then we have to settle for a smaller extent. In the case that
  1297. * there are no large extents, this will return the last entry in the
  1298. * tree unless the tree is empty. In the case that there are only busy
  1299. * large extents, this will return the largest small extent unless there
  1300. * are no smaller extents available.
  1301. */
  1302. if (!i) {
  1303. error = xfs_alloc_ag_vextent_small(args, cnt_cur,
  1304. &fbno, &flen, &i);
  1305. if (error)
  1306. goto error0;
  1307. if (i == 0 || flen == 0) {
  1308. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1309. trace_xfs_alloc_size_noentry(args);
  1310. return 0;
  1311. }
  1312. ASSERT(i == 1);
  1313. busy = xfs_alloc_compute_aligned(args, fbno, flen, &rbno,
  1314. &rlen, &busy_gen);
  1315. } else {
  1316. /*
  1317. * Search for a non-busy extent that is large enough.
  1318. */
  1319. for (;;) {
  1320. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
  1321. if (error)
  1322. goto error0;
  1323. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1324. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1325. &rbno, &rlen, &busy_gen);
  1326. if (rlen >= args->maxlen)
  1327. break;
  1328. error = xfs_btree_increment(cnt_cur, 0, &i);
  1329. if (error)
  1330. goto error0;
  1331. if (i == 0) {
  1332. /*
  1333. * Our only valid extents must have been busy.
  1334. * Make it unbusy by forcing the log out and
  1335. * retrying.
  1336. */
  1337. xfs_btree_del_cursor(cnt_cur,
  1338. XFS_BTREE_NOERROR);
  1339. trace_xfs_alloc_size_busy(args);
  1340. xfs_extent_busy_flush(args->mp,
  1341. args->pag, busy_gen);
  1342. goto restart;
  1343. }
  1344. }
  1345. }
  1346. /*
  1347. * In the first case above, we got the last entry in the
  1348. * by-size btree. Now we check to see if the space hits maxlen
  1349. * once aligned; if not, we search left for something better.
  1350. * This can't happen in the second case above.
  1351. */
  1352. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1353. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1354. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1355. if (rlen < args->maxlen) {
  1356. xfs_agblock_t bestfbno;
  1357. xfs_extlen_t bestflen;
  1358. xfs_agblock_t bestrbno;
  1359. xfs_extlen_t bestrlen;
  1360. bestrlen = rlen;
  1361. bestrbno = rbno;
  1362. bestflen = flen;
  1363. bestfbno = fbno;
  1364. for (;;) {
  1365. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1366. goto error0;
  1367. if (i == 0)
  1368. break;
  1369. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1370. &i)))
  1371. goto error0;
  1372. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1373. if (flen < bestrlen)
  1374. break;
  1375. busy = xfs_alloc_compute_aligned(args, fbno, flen,
  1376. &rbno, &rlen, &busy_gen);
  1377. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1378. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen == 0 ||
  1379. (rlen <= flen && rbno + rlen <= fbno + flen),
  1380. error0);
  1381. if (rlen > bestrlen) {
  1382. bestrlen = rlen;
  1383. bestrbno = rbno;
  1384. bestflen = flen;
  1385. bestfbno = fbno;
  1386. if (rlen == args->maxlen)
  1387. break;
  1388. }
  1389. }
  1390. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1391. &i)))
  1392. goto error0;
  1393. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1394. rlen = bestrlen;
  1395. rbno = bestrbno;
  1396. flen = bestflen;
  1397. fbno = bestfbno;
  1398. }
  1399. args->wasfromfl = 0;
  1400. /*
  1401. * Fix up the length.
  1402. */
  1403. args->len = rlen;
  1404. if (rlen < args->minlen) {
  1405. if (busy) {
  1406. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1407. trace_xfs_alloc_size_busy(args);
  1408. xfs_extent_busy_flush(args->mp, args->pag, busy_gen);
  1409. goto restart;
  1410. }
  1411. goto out_nominleft;
  1412. }
  1413. xfs_alloc_fix_len(args);
  1414. rlen = args->len;
  1415. XFS_WANT_CORRUPTED_GOTO(args->mp, rlen <= flen, error0);
  1416. /*
  1417. * Allocate and initialize a cursor for the by-block tree.
  1418. */
  1419. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1420. args->agno, XFS_BTNUM_BNO);
  1421. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1422. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1423. goto error0;
  1424. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1425. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1426. cnt_cur = bno_cur = NULL;
  1427. args->len = rlen;
  1428. args->agbno = rbno;
  1429. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1430. args->agbno + args->len <=
  1431. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1432. error0);
  1433. trace_xfs_alloc_size_done(args);
  1434. return 0;
  1435. error0:
  1436. trace_xfs_alloc_size_error(args);
  1437. if (cnt_cur)
  1438. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1439. if (bno_cur)
  1440. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1441. return error;
  1442. out_nominleft:
  1443. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1444. trace_xfs_alloc_size_nominleft(args);
  1445. args->agbno = NULLAGBLOCK;
  1446. return 0;
  1447. }
  1448. /*
  1449. * Deal with the case where only small freespaces remain.
  1450. * Either return the contents of the last freespace record,
  1451. * or allocate space from the freelist if there is nothing in the tree.
  1452. */
  1453. STATIC int /* error */
  1454. xfs_alloc_ag_vextent_small(
  1455. xfs_alloc_arg_t *args, /* allocation argument structure */
  1456. xfs_btree_cur_t *ccur, /* by-size cursor */
  1457. xfs_agblock_t *fbnop, /* result block number */
  1458. xfs_extlen_t *flenp, /* result length */
  1459. int *stat) /* status: 0-freelist, 1-normal/none */
  1460. {
  1461. struct xfs_owner_info oinfo;
  1462. int error;
  1463. xfs_agblock_t fbno;
  1464. xfs_extlen_t flen;
  1465. int i;
  1466. if ((error = xfs_btree_decrement(ccur, 0, &i)))
  1467. goto error0;
  1468. if (i) {
  1469. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1470. goto error0;
  1471. XFS_WANT_CORRUPTED_GOTO(args->mp, i == 1, error0);
  1472. }
  1473. /*
  1474. * Nothing in the btree, try the freelist. Make sure
  1475. * to respect minleft even when pulling from the
  1476. * freelist.
  1477. */
  1478. else if (args->minlen == 1 && args->alignment == 1 &&
  1479. args->resv != XFS_AG_RESV_AGFL &&
  1480. (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
  1481. > args->minleft)) {
  1482. error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
  1483. if (error)
  1484. goto error0;
  1485. if (fbno != NULLAGBLOCK) {
  1486. xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
  1487. xfs_alloc_allow_busy_reuse(args->datatype));
  1488. if (xfs_alloc_is_userdata(args->datatype)) {
  1489. xfs_buf_t *bp;
  1490. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1491. args->agno, fbno, 0);
  1492. if (!bp) {
  1493. error = -EFSCORRUPTED;
  1494. goto error0;
  1495. }
  1496. xfs_trans_binval(args->tp, bp);
  1497. }
  1498. args->len = 1;
  1499. args->agbno = fbno;
  1500. XFS_WANT_CORRUPTED_GOTO(args->mp,
  1501. args->agbno + args->len <=
  1502. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1503. error0);
  1504. args->wasfromfl = 1;
  1505. trace_xfs_alloc_small_freelist(args);
  1506. /*
  1507. * If we're feeding an AGFL block to something that
  1508. * doesn't live in the free space, we need to clear
  1509. * out the OWN_AG rmap.
  1510. */
  1511. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  1512. error = xfs_rmap_free(args->tp, args->agbp, args->agno,
  1513. fbno, 1, &oinfo);
  1514. if (error)
  1515. goto error0;
  1516. *stat = 0;
  1517. return 0;
  1518. }
  1519. /*
  1520. * Nothing in the freelist.
  1521. */
  1522. else
  1523. flen = 0;
  1524. }
  1525. /*
  1526. * Can't allocate from the freelist for some reason.
  1527. */
  1528. else {
  1529. fbno = NULLAGBLOCK;
  1530. flen = 0;
  1531. }
  1532. /*
  1533. * Can't do the allocation, give up.
  1534. */
  1535. if (flen < args->minlen) {
  1536. args->agbno = NULLAGBLOCK;
  1537. trace_xfs_alloc_small_notenough(args);
  1538. flen = 0;
  1539. }
  1540. *fbnop = fbno;
  1541. *flenp = flen;
  1542. *stat = 1;
  1543. trace_xfs_alloc_small_done(args);
  1544. return 0;
  1545. error0:
  1546. trace_xfs_alloc_small_error(args);
  1547. return error;
  1548. }
  1549. /*
  1550. * Free the extent starting at agno/bno for length.
  1551. */
  1552. STATIC int
  1553. xfs_free_ag_extent(
  1554. xfs_trans_t *tp,
  1555. xfs_buf_t *agbp,
  1556. xfs_agnumber_t agno,
  1557. xfs_agblock_t bno,
  1558. xfs_extlen_t len,
  1559. struct xfs_owner_info *oinfo,
  1560. enum xfs_ag_resv_type type)
  1561. {
  1562. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1563. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1564. int error; /* error return value */
  1565. xfs_agblock_t gtbno; /* start of right neighbor block */
  1566. xfs_extlen_t gtlen; /* length of right neighbor block */
  1567. int haveleft; /* have a left neighbor block */
  1568. int haveright; /* have a right neighbor block */
  1569. int i; /* temp, result code */
  1570. xfs_agblock_t ltbno; /* start of left neighbor block */
  1571. xfs_extlen_t ltlen; /* length of left neighbor block */
  1572. xfs_mount_t *mp; /* mount point struct for filesystem */
  1573. xfs_agblock_t nbno; /* new starting block of freespace */
  1574. xfs_extlen_t nlen; /* new length of freespace */
  1575. xfs_perag_t *pag; /* per allocation group data */
  1576. bno_cur = cnt_cur = NULL;
  1577. mp = tp->t_mountp;
  1578. if (!xfs_rmap_should_skip_owner_update(oinfo)) {
  1579. error = xfs_rmap_free(tp, agbp, agno, bno, len, oinfo);
  1580. if (error)
  1581. goto error0;
  1582. }
  1583. /*
  1584. * Allocate and initialize a cursor for the by-block btree.
  1585. */
  1586. bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
  1587. /*
  1588. * Look for a neighboring block on the left (lower block numbers)
  1589. * that is contiguous with this space.
  1590. */
  1591. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1592. goto error0;
  1593. if (haveleft) {
  1594. /*
  1595. * There is a block to our left.
  1596. */
  1597. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1598. goto error0;
  1599. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1600. /*
  1601. * It's not contiguous, though.
  1602. */
  1603. if (ltbno + ltlen < bno)
  1604. haveleft = 0;
  1605. else {
  1606. /*
  1607. * If this failure happens the request to free this
  1608. * space was invalid, it's (partly) already free.
  1609. * Very bad.
  1610. */
  1611. XFS_WANT_CORRUPTED_GOTO(mp,
  1612. ltbno + ltlen <= bno, error0);
  1613. }
  1614. }
  1615. /*
  1616. * Look for a neighboring block on the right (higher block numbers)
  1617. * that is contiguous with this space.
  1618. */
  1619. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1620. goto error0;
  1621. if (haveright) {
  1622. /*
  1623. * There is a block to our right.
  1624. */
  1625. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1626. goto error0;
  1627. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1628. /*
  1629. * It's not contiguous, though.
  1630. */
  1631. if (bno + len < gtbno)
  1632. haveright = 0;
  1633. else {
  1634. /*
  1635. * If this failure happens the request to free this
  1636. * space was invalid, it's (partly) already free.
  1637. * Very bad.
  1638. */
  1639. XFS_WANT_CORRUPTED_GOTO(mp, gtbno >= bno + len, error0);
  1640. }
  1641. }
  1642. /*
  1643. * Now allocate and initialize a cursor for the by-size tree.
  1644. */
  1645. cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
  1646. /*
  1647. * Have both left and right contiguous neighbors.
  1648. * Merge all three into a single free block.
  1649. */
  1650. if (haveleft && haveright) {
  1651. /*
  1652. * Delete the old by-size entry on the left.
  1653. */
  1654. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1655. goto error0;
  1656. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1657. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1658. goto error0;
  1659. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1660. /*
  1661. * Delete the old by-size entry on the right.
  1662. */
  1663. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1664. goto error0;
  1665. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1666. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1667. goto error0;
  1668. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1669. /*
  1670. * Delete the old by-block entry for the right block.
  1671. */
  1672. if ((error = xfs_btree_delete(bno_cur, &i)))
  1673. goto error0;
  1674. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1675. /*
  1676. * Move the by-block cursor back to the left neighbor.
  1677. */
  1678. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1679. goto error0;
  1680. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1681. #ifdef DEBUG
  1682. /*
  1683. * Check that this is the right record: delete didn't
  1684. * mangle the cursor.
  1685. */
  1686. {
  1687. xfs_agblock_t xxbno;
  1688. xfs_extlen_t xxlen;
  1689. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1690. &i)))
  1691. goto error0;
  1692. XFS_WANT_CORRUPTED_GOTO(mp,
  1693. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1694. error0);
  1695. }
  1696. #endif
  1697. /*
  1698. * Update remaining by-block entry to the new, joined block.
  1699. */
  1700. nbno = ltbno;
  1701. nlen = len + ltlen + gtlen;
  1702. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1703. goto error0;
  1704. }
  1705. /*
  1706. * Have only a left contiguous neighbor.
  1707. * Merge it together with the new freespace.
  1708. */
  1709. else if (haveleft) {
  1710. /*
  1711. * Delete the old by-size entry on the left.
  1712. */
  1713. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1714. goto error0;
  1715. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1716. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1717. goto error0;
  1718. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1719. /*
  1720. * Back up the by-block cursor to the left neighbor, and
  1721. * update its length.
  1722. */
  1723. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1724. goto error0;
  1725. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1726. nbno = ltbno;
  1727. nlen = len + ltlen;
  1728. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1729. goto error0;
  1730. }
  1731. /*
  1732. * Have only a right contiguous neighbor.
  1733. * Merge it together with the new freespace.
  1734. */
  1735. else if (haveright) {
  1736. /*
  1737. * Delete the old by-size entry on the right.
  1738. */
  1739. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1740. goto error0;
  1741. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1742. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1743. goto error0;
  1744. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1745. /*
  1746. * Update the starting block and length of the right
  1747. * neighbor in the by-block tree.
  1748. */
  1749. nbno = bno;
  1750. nlen = len + gtlen;
  1751. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1752. goto error0;
  1753. }
  1754. /*
  1755. * No contiguous neighbors.
  1756. * Insert the new freespace into the by-block tree.
  1757. */
  1758. else {
  1759. nbno = bno;
  1760. nlen = len;
  1761. if ((error = xfs_btree_insert(bno_cur, &i)))
  1762. goto error0;
  1763. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1764. }
  1765. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1766. bno_cur = NULL;
  1767. /*
  1768. * In all cases we need to insert the new freespace in the by-size tree.
  1769. */
  1770. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1771. goto error0;
  1772. XFS_WANT_CORRUPTED_GOTO(mp, i == 0, error0);
  1773. if ((error = xfs_btree_insert(cnt_cur, &i)))
  1774. goto error0;
  1775. XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
  1776. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1777. cnt_cur = NULL;
  1778. /*
  1779. * Update the freespace totals in the ag and superblock.
  1780. */
  1781. pag = xfs_perag_get(mp, agno);
  1782. error = xfs_alloc_update_counters(tp, pag, agbp, len);
  1783. xfs_ag_resv_free_extent(pag, type, tp, len);
  1784. xfs_perag_put(pag);
  1785. if (error)
  1786. goto error0;
  1787. XFS_STATS_INC(mp, xs_freex);
  1788. XFS_STATS_ADD(mp, xs_freeb, len);
  1789. trace_xfs_free_extent(mp, agno, bno, len, type, haveleft, haveright);
  1790. return 0;
  1791. error0:
  1792. trace_xfs_free_extent(mp, agno, bno, len, type, -1, -1);
  1793. if (bno_cur)
  1794. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1795. if (cnt_cur)
  1796. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1797. return error;
  1798. }
  1799. /*
  1800. * Visible (exported) allocation/free functions.
  1801. * Some of these are used just by xfs_alloc_btree.c and this file.
  1802. */
  1803. /*
  1804. * Compute and fill in value of m_ag_maxlevels.
  1805. */
  1806. void
  1807. xfs_alloc_compute_maxlevels(
  1808. xfs_mount_t *mp) /* file system mount structure */
  1809. {
  1810. mp->m_ag_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
  1811. (mp->m_sb.sb_agblocks + 1) / 2);
  1812. }
  1813. /*
  1814. * Find the length of the longest extent in an AG. The 'need' parameter
  1815. * specifies how much space we're going to need for the AGFL and the
  1816. * 'reserved' parameter tells us how many blocks in this AG are reserved for
  1817. * other callers.
  1818. */
  1819. xfs_extlen_t
  1820. xfs_alloc_longest_free_extent(
  1821. struct xfs_perag *pag,
  1822. xfs_extlen_t need,
  1823. xfs_extlen_t reserved)
  1824. {
  1825. xfs_extlen_t delta = 0;
  1826. /*
  1827. * If the AGFL needs a recharge, we'll have to subtract that from the
  1828. * longest extent.
  1829. */
  1830. if (need > pag->pagf_flcount)
  1831. delta = need - pag->pagf_flcount;
  1832. /*
  1833. * If we cannot maintain others' reservations with space from the
  1834. * not-longest freesp extents, we'll have to subtract /that/ from
  1835. * the longest extent too.
  1836. */
  1837. if (pag->pagf_freeblks - pag->pagf_longest < reserved)
  1838. delta += reserved - (pag->pagf_freeblks - pag->pagf_longest);
  1839. /*
  1840. * If the longest extent is long enough to satisfy all the
  1841. * reservations and AGFL rules in place, we can return this extent.
  1842. */
  1843. if (pag->pagf_longest > delta)
  1844. return pag->pagf_longest - delta;
  1845. /* Otherwise, let the caller try for 1 block if there's space. */
  1846. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  1847. }
  1848. unsigned int
  1849. xfs_alloc_min_freelist(
  1850. struct xfs_mount *mp,
  1851. struct xfs_perag *pag)
  1852. {
  1853. unsigned int min_free;
  1854. /* space needed by-bno freespace btree */
  1855. min_free = min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_BNOi] + 1,
  1856. mp->m_ag_maxlevels);
  1857. /* space needed by-size freespace btree */
  1858. min_free += min_t(unsigned int, pag->pagf_levels[XFS_BTNUM_CNTi] + 1,
  1859. mp->m_ag_maxlevels);
  1860. /* space needed reverse mapping used space btree */
  1861. if (xfs_sb_version_hasrmapbt(&mp->m_sb))
  1862. min_free += min_t(unsigned int,
  1863. pag->pagf_levels[XFS_BTNUM_RMAPi] + 1,
  1864. mp->m_rmap_maxlevels);
  1865. return min_free;
  1866. }
  1867. /*
  1868. * Check if the operation we are fixing up the freelist for should go ahead or
  1869. * not. If we are freeing blocks, we always allow it, otherwise the allocation
  1870. * is dependent on whether the size and shape of free space available will
  1871. * permit the requested allocation to take place.
  1872. */
  1873. static bool
  1874. xfs_alloc_space_available(
  1875. struct xfs_alloc_arg *args,
  1876. xfs_extlen_t min_free,
  1877. int flags)
  1878. {
  1879. struct xfs_perag *pag = args->pag;
  1880. xfs_extlen_t alloc_len, longest;
  1881. xfs_extlen_t reservation; /* blocks that are still reserved */
  1882. int available;
  1883. if (flags & XFS_ALLOC_FLAG_FREEING)
  1884. return true;
  1885. reservation = xfs_ag_resv_needed(pag, args->resv);
  1886. /* do we have enough contiguous free space for the allocation? */
  1887. alloc_len = args->minlen + (args->alignment - 1) + args->minalignslop;
  1888. longest = xfs_alloc_longest_free_extent(pag, min_free, reservation);
  1889. if (longest < alloc_len)
  1890. return false;
  1891. /* do we have enough free space remaining for the allocation? */
  1892. available = (int)(pag->pagf_freeblks + pag->pagf_flcount -
  1893. reservation - min_free - args->minleft);
  1894. if (available < (int)max(args->total, alloc_len))
  1895. return false;
  1896. /*
  1897. * Clamp maxlen to the amount of free space available for the actual
  1898. * extent allocation.
  1899. */
  1900. if (available < (int)args->maxlen && !(flags & XFS_ALLOC_FLAG_CHECK)) {
  1901. args->maxlen = available;
  1902. ASSERT(args->maxlen > 0);
  1903. ASSERT(args->maxlen >= args->minlen);
  1904. }
  1905. return true;
  1906. }
  1907. int
  1908. xfs_free_agfl_block(
  1909. struct xfs_trans *tp,
  1910. xfs_agnumber_t agno,
  1911. xfs_agblock_t agbno,
  1912. struct xfs_buf *agbp,
  1913. struct xfs_owner_info *oinfo)
  1914. {
  1915. int error;
  1916. struct xfs_buf *bp;
  1917. error = xfs_free_ag_extent(tp, agbp, agno, agbno, 1, oinfo,
  1918. XFS_AG_RESV_AGFL);
  1919. if (error)
  1920. return error;
  1921. bp = xfs_btree_get_bufs(tp->t_mountp, tp, agno, agbno, 0);
  1922. if (!bp)
  1923. return -EFSCORRUPTED;
  1924. xfs_trans_binval(tp, bp);
  1925. return 0;
  1926. }
  1927. /*
  1928. * Check the agfl fields of the agf for inconsistency or corruption. The purpose
  1929. * is to detect an agfl header padding mismatch between current and early v5
  1930. * kernels. This problem manifests as a 1-slot size difference between the
  1931. * on-disk flcount and the active [first, last] range of a wrapped agfl. This
  1932. * may also catch variants of agfl count corruption unrelated to padding. Either
  1933. * way, we'll reset the agfl and warn the user.
  1934. *
  1935. * Return true if a reset is required before the agfl can be used, false
  1936. * otherwise.
  1937. */
  1938. static bool
  1939. xfs_agfl_needs_reset(
  1940. struct xfs_mount *mp,
  1941. struct xfs_agf *agf)
  1942. {
  1943. uint32_t f = be32_to_cpu(agf->agf_flfirst);
  1944. uint32_t l = be32_to_cpu(agf->agf_fllast);
  1945. uint32_t c = be32_to_cpu(agf->agf_flcount);
  1946. int agfl_size = xfs_agfl_size(mp);
  1947. int active;
  1948. /* no agfl header on v4 supers */
  1949. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1950. return false;
  1951. /*
  1952. * The agf read verifier catches severe corruption of these fields.
  1953. * Repeat some sanity checks to cover a packed -> unpacked mismatch if
  1954. * the verifier allows it.
  1955. */
  1956. if (f >= agfl_size || l >= agfl_size)
  1957. return true;
  1958. if (c > agfl_size)
  1959. return true;
  1960. /*
  1961. * Check consistency between the on-disk count and the active range. An
  1962. * agfl padding mismatch manifests as an inconsistent flcount.
  1963. */
  1964. if (c && l >= f)
  1965. active = l - f + 1;
  1966. else if (c)
  1967. active = agfl_size - f + l + 1;
  1968. else
  1969. active = 0;
  1970. return active != c;
  1971. }
  1972. /*
  1973. * Reset the agfl to an empty state. Ignore/drop any existing blocks since the
  1974. * agfl content cannot be trusted. Warn the user that a repair is required to
  1975. * recover leaked blocks.
  1976. *
  1977. * The purpose of this mechanism is to handle filesystems affected by the agfl
  1978. * header padding mismatch problem. A reset keeps the filesystem online with a
  1979. * relatively minor free space accounting inconsistency rather than suffer the
  1980. * inevitable crash from use of an invalid agfl block.
  1981. */
  1982. static void
  1983. xfs_agfl_reset(
  1984. struct xfs_trans *tp,
  1985. struct xfs_buf *agbp,
  1986. struct xfs_perag *pag)
  1987. {
  1988. struct xfs_mount *mp = tp->t_mountp;
  1989. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  1990. ASSERT(pag->pagf_agflreset);
  1991. trace_xfs_agfl_reset(mp, agf, 0, _RET_IP_);
  1992. xfs_warn(mp,
  1993. "WARNING: Reset corrupted AGFL on AG %u. %d blocks leaked. "
  1994. "Please unmount and run xfs_repair.",
  1995. pag->pag_agno, pag->pagf_flcount);
  1996. agf->agf_flfirst = 0;
  1997. agf->agf_fllast = cpu_to_be32(xfs_agfl_size(mp) - 1);
  1998. agf->agf_flcount = 0;
  1999. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FLFIRST | XFS_AGF_FLLAST |
  2000. XFS_AGF_FLCOUNT);
  2001. pag->pagf_flcount = 0;
  2002. pag->pagf_agflreset = false;
  2003. }
  2004. /*
  2005. * Defer an AGFL block free. This is effectively equivalent to
  2006. * xfs_bmap_add_free() with some special handling particular to AGFL blocks.
  2007. *
  2008. * Deferring AGFL frees helps prevent log reservation overruns due to too many
  2009. * allocation operations in a transaction. AGFL frees are prone to this problem
  2010. * because for one they are always freed one at a time. Further, an immediate
  2011. * AGFL block free can cause a btree join and require another block free before
  2012. * the real allocation can proceed. Deferring the free disconnects freeing up
  2013. * the AGFL slot from freeing the block.
  2014. */
  2015. STATIC void
  2016. xfs_defer_agfl_block(
  2017. struct xfs_trans *tp,
  2018. xfs_agnumber_t agno,
  2019. xfs_fsblock_t agbno,
  2020. struct xfs_owner_info *oinfo)
  2021. {
  2022. struct xfs_mount *mp = tp->t_mountp;
  2023. struct xfs_extent_free_item *new; /* new element */
  2024. ASSERT(xfs_bmap_free_item_zone != NULL);
  2025. ASSERT(oinfo != NULL);
  2026. new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
  2027. new->xefi_startblock = XFS_AGB_TO_FSB(mp, agno, agbno);
  2028. new->xefi_blockcount = 1;
  2029. new->xefi_oinfo = *oinfo;
  2030. new->xefi_skip_discard = false;
  2031. trace_xfs_agfl_free_defer(mp, agno, 0, agbno, 1);
  2032. xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_AGFL_FREE, &new->xefi_list);
  2033. }
  2034. /*
  2035. * Decide whether to use this allocation group for this allocation.
  2036. * If so, fix up the btree freelist's size.
  2037. */
  2038. int /* error */
  2039. xfs_alloc_fix_freelist(
  2040. struct xfs_alloc_arg *args, /* allocation argument structure */
  2041. int flags) /* XFS_ALLOC_FLAG_... */
  2042. {
  2043. struct xfs_mount *mp = args->mp;
  2044. struct xfs_perag *pag = args->pag;
  2045. struct xfs_trans *tp = args->tp;
  2046. struct xfs_buf *agbp = NULL;
  2047. struct xfs_buf *agflbp = NULL;
  2048. struct xfs_alloc_arg targs; /* local allocation arguments */
  2049. xfs_agblock_t bno; /* freelist block */
  2050. xfs_extlen_t need; /* total blocks needed in freelist */
  2051. int error = 0;
  2052. if (!pag->pagf_init) {
  2053. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2054. if (error)
  2055. goto out_no_agbp;
  2056. if (!pag->pagf_init) {
  2057. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2058. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2059. goto out_agbp_relse;
  2060. }
  2061. }
  2062. /*
  2063. * If this is a metadata preferred pag and we are user data then try
  2064. * somewhere else if we are not being asked to try harder at this
  2065. * point
  2066. */
  2067. if (pag->pagf_metadata && xfs_alloc_is_userdata(args->datatype) &&
  2068. (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  2069. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2070. goto out_agbp_relse;
  2071. }
  2072. need = xfs_alloc_min_freelist(mp, pag);
  2073. if (!xfs_alloc_space_available(args, need, flags |
  2074. XFS_ALLOC_FLAG_CHECK))
  2075. goto out_agbp_relse;
  2076. /*
  2077. * Get the a.g. freespace buffer.
  2078. * Can fail if we're not blocking on locks, and it's held.
  2079. */
  2080. if (!agbp) {
  2081. error = xfs_alloc_read_agf(mp, tp, args->agno, flags, &agbp);
  2082. if (error)
  2083. goto out_no_agbp;
  2084. if (!agbp) {
  2085. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  2086. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  2087. goto out_no_agbp;
  2088. }
  2089. }
  2090. /* reset a padding mismatched agfl before final free space check */
  2091. if (pag->pagf_agflreset)
  2092. xfs_agfl_reset(tp, agbp, pag);
  2093. /* If there isn't enough total space or single-extent, reject it. */
  2094. need = xfs_alloc_min_freelist(mp, pag);
  2095. if (!xfs_alloc_space_available(args, need, flags))
  2096. goto out_agbp_relse;
  2097. /*
  2098. * Make the freelist shorter if it's too long.
  2099. *
  2100. * Note that from this point onwards, we will always release the agf and
  2101. * agfl buffers on error. This handles the case where we error out and
  2102. * the buffers are clean or may not have been joined to the transaction
  2103. * and hence need to be released manually. If they have been joined to
  2104. * the transaction, then xfs_trans_brelse() will handle them
  2105. * appropriately based on the recursion count and dirty state of the
  2106. * buffer.
  2107. *
  2108. * XXX (dgc): When we have lots of free space, does this buy us
  2109. * anything other than extra overhead when we need to put more blocks
  2110. * back on the free list? Maybe we should only do this when space is
  2111. * getting low or the AGFL is more than half full?
  2112. *
  2113. * The NOSHRINK flag prevents the AGFL from being shrunk if it's too
  2114. * big; the NORMAP flag prevents AGFL expand/shrink operations from
  2115. * updating the rmapbt. Both flags are used in xfs_repair while we're
  2116. * rebuilding the rmapbt, and neither are used by the kernel. They're
  2117. * both required to ensure that rmaps are correctly recorded for the
  2118. * regenerated AGFL, bnobt, and cntbt. See repair/phase5.c and
  2119. * repair/rmap.c in xfsprogs for details.
  2120. */
  2121. memset(&targs, 0, sizeof(targs));
  2122. if (flags & XFS_ALLOC_FLAG_NORMAP)
  2123. xfs_rmap_skip_owner_update(&targs.oinfo);
  2124. else
  2125. xfs_rmap_ag_owner(&targs.oinfo, XFS_RMAP_OWN_AG);
  2126. while (!(flags & XFS_ALLOC_FLAG_NOSHRINK) && pag->pagf_flcount > need) {
  2127. error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
  2128. if (error)
  2129. goto out_agbp_relse;
  2130. /* defer agfl frees */
  2131. xfs_defer_agfl_block(tp, args->agno, bno, &targs.oinfo);
  2132. }
  2133. targs.tp = tp;
  2134. targs.mp = mp;
  2135. targs.agbp = agbp;
  2136. targs.agno = args->agno;
  2137. targs.alignment = targs.minlen = targs.prod = 1;
  2138. targs.type = XFS_ALLOCTYPE_THIS_AG;
  2139. targs.pag = pag;
  2140. error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp);
  2141. if (error)
  2142. goto out_agbp_relse;
  2143. /* Make the freelist longer if it's too short. */
  2144. while (pag->pagf_flcount < need) {
  2145. targs.agbno = 0;
  2146. targs.maxlen = need - pag->pagf_flcount;
  2147. targs.resv = XFS_AG_RESV_AGFL;
  2148. /* Allocate as many blocks as possible at once. */
  2149. error = xfs_alloc_ag_vextent(&targs);
  2150. if (error)
  2151. goto out_agflbp_relse;
  2152. /*
  2153. * Stop if we run out. Won't happen if callers are obeying
  2154. * the restrictions correctly. Can happen for free calls
  2155. * on a completely full ag.
  2156. */
  2157. if (targs.agbno == NULLAGBLOCK) {
  2158. if (flags & XFS_ALLOC_FLAG_FREEING)
  2159. break;
  2160. goto out_agflbp_relse;
  2161. }
  2162. /*
  2163. * Put each allocated block on the list.
  2164. */
  2165. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  2166. error = xfs_alloc_put_freelist(tp, agbp,
  2167. agflbp, bno, 0);
  2168. if (error)
  2169. goto out_agflbp_relse;
  2170. }
  2171. }
  2172. xfs_trans_brelse(tp, agflbp);
  2173. args->agbp = agbp;
  2174. return 0;
  2175. out_agflbp_relse:
  2176. xfs_trans_brelse(tp, agflbp);
  2177. out_agbp_relse:
  2178. if (agbp)
  2179. xfs_trans_brelse(tp, agbp);
  2180. out_no_agbp:
  2181. args->agbp = NULL;
  2182. return error;
  2183. }
  2184. /*
  2185. * Get a block from the freelist.
  2186. * Returns with the buffer for the block gotten.
  2187. */
  2188. int /* error */
  2189. xfs_alloc_get_freelist(
  2190. xfs_trans_t *tp, /* transaction pointer */
  2191. xfs_buf_t *agbp, /* buffer containing the agf structure */
  2192. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  2193. int btreeblk) /* destination is a AGF btree */
  2194. {
  2195. xfs_agf_t *agf; /* a.g. freespace structure */
  2196. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  2197. xfs_agblock_t bno; /* block number returned */
  2198. __be32 *agfl_bno;
  2199. int error;
  2200. int logflags;
  2201. xfs_mount_t *mp = tp->t_mountp;
  2202. xfs_perag_t *pag; /* per allocation group data */
  2203. /*
  2204. * Freelist is empty, give up.
  2205. */
  2206. agf = XFS_BUF_TO_AGF(agbp);
  2207. if (!agf->agf_flcount) {
  2208. *bnop = NULLAGBLOCK;
  2209. return 0;
  2210. }
  2211. /*
  2212. * Read the array of free blocks.
  2213. */
  2214. error = xfs_alloc_read_agfl(mp, tp, be32_to_cpu(agf->agf_seqno),
  2215. &agflbp);
  2216. if (error)
  2217. return error;
  2218. /*
  2219. * Get the block number and update the data structures.
  2220. */
  2221. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2222. bno = be32_to_cpu(agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  2223. be32_add_cpu(&agf->agf_flfirst, 1);
  2224. xfs_trans_brelse(tp, agflbp);
  2225. if (be32_to_cpu(agf->agf_flfirst) == xfs_agfl_size(mp))
  2226. agf->agf_flfirst = 0;
  2227. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2228. ASSERT(!pag->pagf_agflreset);
  2229. be32_add_cpu(&agf->agf_flcount, -1);
  2230. xfs_trans_agflist_delta(tp, -1);
  2231. pag->pagf_flcount--;
  2232. xfs_perag_put(pag);
  2233. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  2234. if (btreeblk) {
  2235. be32_add_cpu(&agf->agf_btreeblks, 1);
  2236. pag->pagf_btreeblks++;
  2237. logflags |= XFS_AGF_BTREEBLKS;
  2238. }
  2239. xfs_alloc_log_agf(tp, agbp, logflags);
  2240. *bnop = bno;
  2241. return 0;
  2242. }
  2243. /*
  2244. * Log the given fields from the agf structure.
  2245. */
  2246. void
  2247. xfs_alloc_log_agf(
  2248. xfs_trans_t *tp, /* transaction pointer */
  2249. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  2250. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  2251. {
  2252. int first; /* first byte offset */
  2253. int last; /* last byte offset */
  2254. static const short offsets[] = {
  2255. offsetof(xfs_agf_t, agf_magicnum),
  2256. offsetof(xfs_agf_t, agf_versionnum),
  2257. offsetof(xfs_agf_t, agf_seqno),
  2258. offsetof(xfs_agf_t, agf_length),
  2259. offsetof(xfs_agf_t, agf_roots[0]),
  2260. offsetof(xfs_agf_t, agf_levels[0]),
  2261. offsetof(xfs_agf_t, agf_flfirst),
  2262. offsetof(xfs_agf_t, agf_fllast),
  2263. offsetof(xfs_agf_t, agf_flcount),
  2264. offsetof(xfs_agf_t, agf_freeblks),
  2265. offsetof(xfs_agf_t, agf_longest),
  2266. offsetof(xfs_agf_t, agf_btreeblks),
  2267. offsetof(xfs_agf_t, agf_uuid),
  2268. offsetof(xfs_agf_t, agf_rmap_blocks),
  2269. offsetof(xfs_agf_t, agf_refcount_blocks),
  2270. offsetof(xfs_agf_t, agf_refcount_root),
  2271. offsetof(xfs_agf_t, agf_refcount_level),
  2272. /* needed so that we don't log the whole rest of the structure: */
  2273. offsetof(xfs_agf_t, agf_spare64),
  2274. sizeof(xfs_agf_t)
  2275. };
  2276. trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
  2277. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGF_BUF);
  2278. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  2279. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  2280. }
  2281. /*
  2282. * Interface for inode allocation to force the pag data to be initialized.
  2283. */
  2284. int /* error */
  2285. xfs_alloc_pagf_init(
  2286. xfs_mount_t *mp, /* file system mount structure */
  2287. xfs_trans_t *tp, /* transaction pointer */
  2288. xfs_agnumber_t agno, /* allocation group number */
  2289. int flags) /* XFS_ALLOC_FLAGS_... */
  2290. {
  2291. xfs_buf_t *bp;
  2292. int error;
  2293. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  2294. return error;
  2295. if (bp)
  2296. xfs_trans_brelse(tp, bp);
  2297. return 0;
  2298. }
  2299. /*
  2300. * Put the block on the freelist for the allocation group.
  2301. */
  2302. int /* error */
  2303. xfs_alloc_put_freelist(
  2304. xfs_trans_t *tp, /* transaction pointer */
  2305. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  2306. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  2307. xfs_agblock_t bno, /* block being freed */
  2308. int btreeblk) /* block came from a AGF btree */
  2309. {
  2310. xfs_agf_t *agf; /* a.g. freespace structure */
  2311. __be32 *blockp;/* pointer to array entry */
  2312. int error;
  2313. int logflags;
  2314. xfs_mount_t *mp; /* mount structure */
  2315. xfs_perag_t *pag; /* per allocation group data */
  2316. __be32 *agfl_bno;
  2317. int startoff;
  2318. agf = XFS_BUF_TO_AGF(agbp);
  2319. mp = tp->t_mountp;
  2320. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  2321. be32_to_cpu(agf->agf_seqno), &agflbp)))
  2322. return error;
  2323. be32_add_cpu(&agf->agf_fllast, 1);
  2324. if (be32_to_cpu(agf->agf_fllast) == xfs_agfl_size(mp))
  2325. agf->agf_fllast = 0;
  2326. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  2327. ASSERT(!pag->pagf_agflreset);
  2328. be32_add_cpu(&agf->agf_flcount, 1);
  2329. xfs_trans_agflist_delta(tp, 1);
  2330. pag->pagf_flcount++;
  2331. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  2332. if (btreeblk) {
  2333. be32_add_cpu(&agf->agf_btreeblks, -1);
  2334. pag->pagf_btreeblks--;
  2335. logflags |= XFS_AGF_BTREEBLKS;
  2336. }
  2337. xfs_perag_put(pag);
  2338. xfs_alloc_log_agf(tp, agbp, logflags);
  2339. ASSERT(be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp));
  2340. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2341. blockp = &agfl_bno[be32_to_cpu(agf->agf_fllast)];
  2342. *blockp = cpu_to_be32(bno);
  2343. startoff = (char *)blockp - (char *)agflbp->b_addr;
  2344. xfs_alloc_log_agf(tp, agbp, logflags);
  2345. xfs_trans_buf_set_type(tp, agflbp, XFS_BLFT_AGFL_BUF);
  2346. xfs_trans_log_buf(tp, agflbp, startoff,
  2347. startoff + sizeof(xfs_agblock_t) - 1);
  2348. return 0;
  2349. }
  2350. static xfs_failaddr_t
  2351. xfs_agf_verify(
  2352. struct xfs_buf *bp)
  2353. {
  2354. struct xfs_mount *mp = bp->b_target->bt_mount;
  2355. struct xfs_agf *agf = XFS_BUF_TO_AGF(bp);
  2356. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2357. if (!uuid_equal(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid))
  2358. return __this_address;
  2359. if (!xfs_log_check_lsn(mp,
  2360. be64_to_cpu(XFS_BUF_TO_AGF(bp)->agf_lsn)))
  2361. return __this_address;
  2362. }
  2363. if (!(agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
  2364. XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
  2365. be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
  2366. be32_to_cpu(agf->agf_flfirst) < xfs_agfl_size(mp) &&
  2367. be32_to_cpu(agf->agf_fllast) < xfs_agfl_size(mp) &&
  2368. be32_to_cpu(agf->agf_flcount) <= xfs_agfl_size(mp)))
  2369. return __this_address;
  2370. if (be32_to_cpu(agf->agf_length) > mp->m_sb.sb_dblocks)
  2371. return __this_address;
  2372. if (be32_to_cpu(agf->agf_freeblks) < be32_to_cpu(agf->agf_longest) ||
  2373. be32_to_cpu(agf->agf_freeblks) > be32_to_cpu(agf->agf_length))
  2374. return __this_address;
  2375. if (be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) < 1 ||
  2376. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) < 1 ||
  2377. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]) > XFS_BTREE_MAXLEVELS ||
  2378. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]) > XFS_BTREE_MAXLEVELS)
  2379. return __this_address;
  2380. if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
  2381. (be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) < 1 ||
  2382. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]) > XFS_BTREE_MAXLEVELS))
  2383. return __this_address;
  2384. if (xfs_sb_version_hasrmapbt(&mp->m_sb) &&
  2385. be32_to_cpu(agf->agf_rmap_blocks) > be32_to_cpu(agf->agf_length))
  2386. return __this_address;
  2387. /*
  2388. * during growfs operations, the perag is not fully initialised,
  2389. * so we can't use it for any useful checking. growfs ensures we can't
  2390. * use it by using uncached buffers that don't have the perag attached
  2391. * so we can detect and avoid this problem.
  2392. */
  2393. if (bp->b_pag && be32_to_cpu(agf->agf_seqno) != bp->b_pag->pag_agno)
  2394. return __this_address;
  2395. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  2396. be32_to_cpu(agf->agf_btreeblks) > be32_to_cpu(agf->agf_length))
  2397. return __this_address;
  2398. if (xfs_sb_version_hasreflink(&mp->m_sb) &&
  2399. be32_to_cpu(agf->agf_refcount_blocks) >
  2400. be32_to_cpu(agf->agf_length))
  2401. return __this_address;
  2402. if (xfs_sb_version_hasreflink(&mp->m_sb) &&
  2403. (be32_to_cpu(agf->agf_refcount_level) < 1 ||
  2404. be32_to_cpu(agf->agf_refcount_level) > XFS_BTREE_MAXLEVELS))
  2405. return __this_address;
  2406. return NULL;
  2407. }
  2408. static void
  2409. xfs_agf_read_verify(
  2410. struct xfs_buf *bp)
  2411. {
  2412. struct xfs_mount *mp = bp->b_target->bt_mount;
  2413. xfs_failaddr_t fa;
  2414. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  2415. !xfs_buf_verify_cksum(bp, XFS_AGF_CRC_OFF))
  2416. xfs_verifier_error(bp, -EFSBADCRC, __this_address);
  2417. else {
  2418. fa = xfs_agf_verify(bp);
  2419. if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_ALLOC_READ_AGF))
  2420. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2421. }
  2422. }
  2423. static void
  2424. xfs_agf_write_verify(
  2425. struct xfs_buf *bp)
  2426. {
  2427. struct xfs_mount *mp = bp->b_target->bt_mount;
  2428. struct xfs_buf_log_item *bip = bp->b_log_item;
  2429. xfs_failaddr_t fa;
  2430. fa = xfs_agf_verify(bp);
  2431. if (fa) {
  2432. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  2433. return;
  2434. }
  2435. if (!xfs_sb_version_hascrc(&mp->m_sb))
  2436. return;
  2437. if (bip)
  2438. XFS_BUF_TO_AGF(bp)->agf_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  2439. xfs_buf_update_cksum(bp, XFS_AGF_CRC_OFF);
  2440. }
  2441. const struct xfs_buf_ops xfs_agf_buf_ops = {
  2442. .name = "xfs_agf",
  2443. .verify_read = xfs_agf_read_verify,
  2444. .verify_write = xfs_agf_write_verify,
  2445. .verify_struct = xfs_agf_verify,
  2446. };
  2447. /*
  2448. * Read in the allocation group header (free/alloc section).
  2449. */
  2450. int /* error */
  2451. xfs_read_agf(
  2452. struct xfs_mount *mp, /* mount point structure */
  2453. struct xfs_trans *tp, /* transaction pointer */
  2454. xfs_agnumber_t agno, /* allocation group number */
  2455. int flags, /* XFS_BUF_ */
  2456. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2457. {
  2458. int error;
  2459. trace_xfs_read_agf(mp, agno);
  2460. ASSERT(agno != NULLAGNUMBER);
  2461. error = xfs_trans_read_buf(
  2462. mp, tp, mp->m_ddev_targp,
  2463. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2464. XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
  2465. if (error)
  2466. return error;
  2467. if (!*bpp)
  2468. return 0;
  2469. ASSERT(!(*bpp)->b_error);
  2470. xfs_buf_set_ref(*bpp, XFS_AGF_REF);
  2471. return 0;
  2472. }
  2473. /*
  2474. * Read in the allocation group header (free/alloc section).
  2475. */
  2476. int /* error */
  2477. xfs_alloc_read_agf(
  2478. struct xfs_mount *mp, /* mount point structure */
  2479. struct xfs_trans *tp, /* transaction pointer */
  2480. xfs_agnumber_t agno, /* allocation group number */
  2481. int flags, /* XFS_ALLOC_FLAG_... */
  2482. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2483. {
  2484. struct xfs_agf *agf; /* ag freelist header */
  2485. struct xfs_perag *pag; /* per allocation group data */
  2486. int error;
  2487. trace_xfs_alloc_read_agf(mp, agno);
  2488. ASSERT(agno != NULLAGNUMBER);
  2489. error = xfs_read_agf(mp, tp, agno,
  2490. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  2491. bpp);
  2492. if (error)
  2493. return error;
  2494. if (!*bpp)
  2495. return 0;
  2496. ASSERT(!(*bpp)->b_error);
  2497. agf = XFS_BUF_TO_AGF(*bpp);
  2498. pag = xfs_perag_get(mp, agno);
  2499. if (!pag->pagf_init) {
  2500. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  2501. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  2502. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  2503. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  2504. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2505. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
  2506. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2507. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
  2508. pag->pagf_levels[XFS_BTNUM_RMAPi] =
  2509. be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
  2510. pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
  2511. pag->pagf_init = 1;
  2512. pag->pagf_agflreset = xfs_agfl_needs_reset(mp, agf);
  2513. }
  2514. #ifdef DEBUG
  2515. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2516. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  2517. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  2518. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  2519. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  2520. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2521. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
  2522. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2523. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
  2524. }
  2525. #endif
  2526. xfs_perag_put(pag);
  2527. return 0;
  2528. }
  2529. /*
  2530. * Allocate an extent (variable-size).
  2531. * Depending on the allocation type, we either look in a single allocation
  2532. * group or loop over the allocation groups to find the result.
  2533. */
  2534. int /* error */
  2535. xfs_alloc_vextent(
  2536. struct xfs_alloc_arg *args) /* allocation argument structure */
  2537. {
  2538. xfs_agblock_t agsize; /* allocation group size */
  2539. int error;
  2540. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2541. struct xfs_mount *mp; /* mount structure pointer */
  2542. xfs_agnumber_t sagno; /* starting allocation group number */
  2543. xfs_alloctype_t type; /* input allocation type */
  2544. int bump_rotor = 0;
  2545. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2546. mp = args->mp;
  2547. type = args->otype = args->type;
  2548. args->agbno = NULLAGBLOCK;
  2549. /*
  2550. * Just fix this up, for the case where the last a.g. is shorter
  2551. * (or there's only one a.g.) and the caller couldn't easily figure
  2552. * that out (xfs_bmap_alloc).
  2553. */
  2554. agsize = mp->m_sb.sb_agblocks;
  2555. if (args->maxlen > agsize)
  2556. args->maxlen = agsize;
  2557. if (args->alignment == 0)
  2558. args->alignment = 1;
  2559. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2560. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2561. ASSERT(args->minlen <= args->maxlen);
  2562. ASSERT(args->minlen <= agsize);
  2563. ASSERT(args->mod < args->prod);
  2564. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2565. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2566. args->minlen > args->maxlen || args->minlen > agsize ||
  2567. args->mod >= args->prod) {
  2568. args->fsbno = NULLFSBLOCK;
  2569. trace_xfs_alloc_vextent_badargs(args);
  2570. return 0;
  2571. }
  2572. switch (type) {
  2573. case XFS_ALLOCTYPE_THIS_AG:
  2574. case XFS_ALLOCTYPE_NEAR_BNO:
  2575. case XFS_ALLOCTYPE_THIS_BNO:
  2576. /*
  2577. * These three force us into a single a.g.
  2578. */
  2579. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2580. args->pag = xfs_perag_get(mp, args->agno);
  2581. error = xfs_alloc_fix_freelist(args, 0);
  2582. if (error) {
  2583. trace_xfs_alloc_vextent_nofix(args);
  2584. goto error0;
  2585. }
  2586. if (!args->agbp) {
  2587. trace_xfs_alloc_vextent_noagbp(args);
  2588. break;
  2589. }
  2590. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2591. if ((error = xfs_alloc_ag_vextent(args)))
  2592. goto error0;
  2593. break;
  2594. case XFS_ALLOCTYPE_START_BNO:
  2595. /*
  2596. * Try near allocation first, then anywhere-in-ag after
  2597. * the first a.g. fails.
  2598. */
  2599. if ((args->datatype & XFS_ALLOC_INITIAL_USER_DATA) &&
  2600. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2601. args->fsbno = XFS_AGB_TO_FSB(mp,
  2602. ((mp->m_agfrotor / rotorstep) %
  2603. mp->m_sb.sb_agcount), 0);
  2604. bump_rotor = 1;
  2605. }
  2606. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2607. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2608. /* FALLTHROUGH */
  2609. case XFS_ALLOCTYPE_FIRST_AG:
  2610. /*
  2611. * Rotate through the allocation groups looking for a winner.
  2612. */
  2613. if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2614. /*
  2615. * Start with allocation group given by bno.
  2616. */
  2617. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2618. args->type = XFS_ALLOCTYPE_THIS_AG;
  2619. sagno = 0;
  2620. flags = 0;
  2621. } else {
  2622. /*
  2623. * Start with the given allocation group.
  2624. */
  2625. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2626. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2627. }
  2628. /*
  2629. * Loop over allocation groups twice; first time with
  2630. * trylock set, second time without.
  2631. */
  2632. for (;;) {
  2633. args->pag = xfs_perag_get(mp, args->agno);
  2634. error = xfs_alloc_fix_freelist(args, flags);
  2635. if (error) {
  2636. trace_xfs_alloc_vextent_nofix(args);
  2637. goto error0;
  2638. }
  2639. /*
  2640. * If we get a buffer back then the allocation will fly.
  2641. */
  2642. if (args->agbp) {
  2643. if ((error = xfs_alloc_ag_vextent(args)))
  2644. goto error0;
  2645. break;
  2646. }
  2647. trace_xfs_alloc_vextent_loopfailed(args);
  2648. /*
  2649. * Didn't work, figure out the next iteration.
  2650. */
  2651. if (args->agno == sagno &&
  2652. type == XFS_ALLOCTYPE_START_BNO)
  2653. args->type = XFS_ALLOCTYPE_THIS_AG;
  2654. /*
  2655. * For the first allocation, we can try any AG to get
  2656. * space. However, if we already have allocated a
  2657. * block, we don't want to try AGs whose number is below
  2658. * sagno. Otherwise, we may end up with out-of-order
  2659. * locking of AGF, which might cause deadlock.
  2660. */
  2661. if (++(args->agno) == mp->m_sb.sb_agcount) {
  2662. if (args->tp->t_firstblock != NULLFSBLOCK)
  2663. args->agno = sagno;
  2664. else
  2665. args->agno = 0;
  2666. }
  2667. /*
  2668. * Reached the starting a.g., must either be done
  2669. * or switch to non-trylock mode.
  2670. */
  2671. if (args->agno == sagno) {
  2672. if (flags == 0) {
  2673. args->agbno = NULLAGBLOCK;
  2674. trace_xfs_alloc_vextent_allfailed(args);
  2675. break;
  2676. }
  2677. flags = 0;
  2678. if (type == XFS_ALLOCTYPE_START_BNO) {
  2679. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2680. args->fsbno);
  2681. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2682. }
  2683. }
  2684. xfs_perag_put(args->pag);
  2685. }
  2686. if (bump_rotor) {
  2687. if (args->agno == sagno)
  2688. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2689. (mp->m_sb.sb_agcount * rotorstep);
  2690. else
  2691. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2692. (mp->m_sb.sb_agcount * rotorstep);
  2693. }
  2694. break;
  2695. default:
  2696. ASSERT(0);
  2697. /* NOTREACHED */
  2698. }
  2699. if (args->agbno == NULLAGBLOCK)
  2700. args->fsbno = NULLFSBLOCK;
  2701. else {
  2702. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2703. #ifdef DEBUG
  2704. ASSERT(args->len >= args->minlen);
  2705. ASSERT(args->len <= args->maxlen);
  2706. ASSERT(args->agbno % args->alignment == 0);
  2707. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2708. args->len);
  2709. #endif
  2710. /* Zero the extent if we were asked to do so */
  2711. if (args->datatype & XFS_ALLOC_USERDATA_ZERO) {
  2712. error = xfs_zero_extent(args->ip, args->fsbno, args->len);
  2713. if (error)
  2714. goto error0;
  2715. }
  2716. }
  2717. xfs_perag_put(args->pag);
  2718. return 0;
  2719. error0:
  2720. xfs_perag_put(args->pag);
  2721. return error;
  2722. }
  2723. /* Ensure that the freelist is at full capacity. */
  2724. int
  2725. xfs_free_extent_fix_freelist(
  2726. struct xfs_trans *tp,
  2727. xfs_agnumber_t agno,
  2728. struct xfs_buf **agbp)
  2729. {
  2730. struct xfs_alloc_arg args;
  2731. int error;
  2732. memset(&args, 0, sizeof(struct xfs_alloc_arg));
  2733. args.tp = tp;
  2734. args.mp = tp->t_mountp;
  2735. args.agno = agno;
  2736. /*
  2737. * validate that the block number is legal - the enables us to detect
  2738. * and handle a silent filesystem corruption rather than crashing.
  2739. */
  2740. if (args.agno >= args.mp->m_sb.sb_agcount)
  2741. return -EFSCORRUPTED;
  2742. args.pag = xfs_perag_get(args.mp, args.agno);
  2743. ASSERT(args.pag);
  2744. error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
  2745. if (error)
  2746. goto out;
  2747. *agbp = args.agbp;
  2748. out:
  2749. xfs_perag_put(args.pag);
  2750. return error;
  2751. }
  2752. /*
  2753. * Free an extent.
  2754. * Just break up the extent address and hand off to xfs_free_ag_extent
  2755. * after fixing up the freelist.
  2756. */
  2757. int /* error */
  2758. __xfs_free_extent(
  2759. struct xfs_trans *tp, /* transaction pointer */
  2760. xfs_fsblock_t bno, /* starting block number of extent */
  2761. xfs_extlen_t len, /* length of extent */
  2762. struct xfs_owner_info *oinfo, /* extent owner */
  2763. enum xfs_ag_resv_type type, /* block reservation type */
  2764. bool skip_discard)
  2765. {
  2766. struct xfs_mount *mp = tp->t_mountp;
  2767. struct xfs_buf *agbp;
  2768. xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, bno);
  2769. xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, bno);
  2770. int error;
  2771. unsigned int busy_flags = 0;
  2772. ASSERT(len != 0);
  2773. ASSERT(type != XFS_AG_RESV_AGFL);
  2774. if (XFS_TEST_ERROR(false, mp,
  2775. XFS_ERRTAG_FREE_EXTENT))
  2776. return -EIO;
  2777. error = xfs_free_extent_fix_freelist(tp, agno, &agbp);
  2778. if (error)
  2779. return error;
  2780. XFS_WANT_CORRUPTED_GOTO(mp, agbno < mp->m_sb.sb_agblocks, err);
  2781. /* validate the extent size is legal now we have the agf locked */
  2782. XFS_WANT_CORRUPTED_GOTO(mp,
  2783. agbno + len <= be32_to_cpu(XFS_BUF_TO_AGF(agbp)->agf_length),
  2784. err);
  2785. error = xfs_free_ag_extent(tp, agbp, agno, agbno, len, oinfo, type);
  2786. if (error)
  2787. goto err;
  2788. if (skip_discard)
  2789. busy_flags |= XFS_EXTENT_BUSY_SKIP_DISCARD;
  2790. xfs_extent_busy_insert(tp, agno, agbno, len, busy_flags);
  2791. return 0;
  2792. err:
  2793. xfs_trans_brelse(tp, agbp);
  2794. return error;
  2795. }
  2796. struct xfs_alloc_query_range_info {
  2797. xfs_alloc_query_range_fn fn;
  2798. void *priv;
  2799. };
  2800. /* Format btree record and pass to our callback. */
  2801. STATIC int
  2802. xfs_alloc_query_range_helper(
  2803. struct xfs_btree_cur *cur,
  2804. union xfs_btree_rec *rec,
  2805. void *priv)
  2806. {
  2807. struct xfs_alloc_query_range_info *query = priv;
  2808. struct xfs_alloc_rec_incore irec;
  2809. irec.ar_startblock = be32_to_cpu(rec->alloc.ar_startblock);
  2810. irec.ar_blockcount = be32_to_cpu(rec->alloc.ar_blockcount);
  2811. return query->fn(cur, &irec, query->priv);
  2812. }
  2813. /* Find all free space within a given range of blocks. */
  2814. int
  2815. xfs_alloc_query_range(
  2816. struct xfs_btree_cur *cur,
  2817. struct xfs_alloc_rec_incore *low_rec,
  2818. struct xfs_alloc_rec_incore *high_rec,
  2819. xfs_alloc_query_range_fn fn,
  2820. void *priv)
  2821. {
  2822. union xfs_btree_irec low_brec;
  2823. union xfs_btree_irec high_brec;
  2824. struct xfs_alloc_query_range_info query;
  2825. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2826. low_brec.a = *low_rec;
  2827. high_brec.a = *high_rec;
  2828. query.priv = priv;
  2829. query.fn = fn;
  2830. return xfs_btree_query_range(cur, &low_brec, &high_brec,
  2831. xfs_alloc_query_range_helper, &query);
  2832. }
  2833. /* Find all free space records. */
  2834. int
  2835. xfs_alloc_query_all(
  2836. struct xfs_btree_cur *cur,
  2837. xfs_alloc_query_range_fn fn,
  2838. void *priv)
  2839. {
  2840. struct xfs_alloc_query_range_info query;
  2841. ASSERT(cur->bc_btnum == XFS_BTNUM_BNO);
  2842. query.priv = priv;
  2843. query.fn = fn;
  2844. return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
  2845. }
  2846. /* Is there a record covering a given extent? */
  2847. int
  2848. xfs_alloc_has_record(
  2849. struct xfs_btree_cur *cur,
  2850. xfs_agblock_t bno,
  2851. xfs_extlen_t len,
  2852. bool *exists)
  2853. {
  2854. union xfs_btree_irec low;
  2855. union xfs_btree_irec high;
  2856. memset(&low, 0, sizeof(low));
  2857. low.a.ar_startblock = bno;
  2858. memset(&high, 0xFF, sizeof(high));
  2859. high.a.ar_startblock = bno + len - 1;
  2860. return xfs_btree_has_record(cur, &low, &high, exists);
  2861. }
  2862. /*
  2863. * Walk all the blocks in the AGFL. The @walk_fn can return any negative
  2864. * error code or XFS_BTREE_QUERY_RANGE_ABORT.
  2865. */
  2866. int
  2867. xfs_agfl_walk(
  2868. struct xfs_mount *mp,
  2869. struct xfs_agf *agf,
  2870. struct xfs_buf *agflbp,
  2871. xfs_agfl_walk_fn walk_fn,
  2872. void *priv)
  2873. {
  2874. __be32 *agfl_bno;
  2875. unsigned int i;
  2876. int error;
  2877. agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agflbp);
  2878. i = be32_to_cpu(agf->agf_flfirst);
  2879. /* Nothing to walk in an empty AGFL. */
  2880. if (agf->agf_flcount == cpu_to_be32(0))
  2881. return 0;
  2882. /* Otherwise, walk from first to last, wrapping as needed. */
  2883. for (;;) {
  2884. error = walk_fn(mp, be32_to_cpu(agfl_bno[i]), priv);
  2885. if (error)
  2886. return error;
  2887. if (i == be32_to_cpu(agf->agf_fllast))
  2888. break;
  2889. if (++i == xfs_agfl_size(mp))
  2890. i = 0;
  2891. }
  2892. return 0;
  2893. }