xfs_alloc.c 89 KB

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