xfs_rtalloc.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs_platform.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_alloc.h"
  16. #include "xfs_bmap.h"
  17. #include "xfs_bmap_btree.h"
  18. #include "xfs_bmap_util.h"
  19. #include "xfs_trans.h"
  20. #include "xfs_trans_space.h"
  21. #include "xfs_icache.h"
  22. #include "xfs_rtalloc.h"
  23. #include "xfs_sb.h"
  24. #include "xfs_rtbitmap.h"
  25. #include "xfs_rtrmap_btree.h"
  26. #include "xfs_quota.h"
  27. #include "xfs_log_priv.h"
  28. #include "xfs_health.h"
  29. #include "xfs_da_format.h"
  30. #include "xfs_metafile.h"
  31. #include "xfs_rtgroup.h"
  32. #include "xfs_error.h"
  33. #include "xfs_trace.h"
  34. #include "xfs_rtrefcount_btree.h"
  35. #include "xfs_reflink.h"
  36. #include "xfs_zone_alloc.h"
  37. /*
  38. * Return whether there are any free extents in the size range given
  39. * by low and high, for the bitmap block bbno.
  40. */
  41. STATIC int
  42. xfs_rtany_summary(
  43. struct xfs_rtalloc_args *args,
  44. int low, /* low log2 extent size */
  45. int high, /* high log2 extent size */
  46. xfs_fileoff_t bbno, /* bitmap block number */
  47. int *maxlog) /* out: max log2 extent size free */
  48. {
  49. uint8_t *rsum_cache = args->rtg->rtg_rsum_cache;
  50. int error;
  51. int log; /* loop counter, log2 of ext. size */
  52. xfs_suminfo_t sum; /* summary data */
  53. /* There are no extents at levels >= rsum_cache[bbno]. */
  54. if (rsum_cache) {
  55. high = min(high, rsum_cache[bbno] - 1);
  56. if (low > high) {
  57. *maxlog = -1;
  58. return 0;
  59. }
  60. }
  61. /*
  62. * Loop over logs of extent sizes.
  63. */
  64. for (log = high; log >= low; log--) {
  65. /*
  66. * Get one summary datum.
  67. */
  68. error = xfs_rtget_summary(args, log, bbno, &sum);
  69. if (error) {
  70. return error;
  71. }
  72. /*
  73. * If there are any, return success.
  74. */
  75. if (sum) {
  76. *maxlog = log;
  77. goto out;
  78. }
  79. }
  80. /*
  81. * Found nothing, return failure.
  82. */
  83. *maxlog = -1;
  84. out:
  85. /* There were no extents at levels > log. */
  86. if (rsum_cache && log + 1 < rsum_cache[bbno])
  87. rsum_cache[bbno] = log + 1;
  88. return 0;
  89. }
  90. /*
  91. * Copy and transform the summary file, given the old and new
  92. * parameters in the mount structures.
  93. */
  94. STATIC int
  95. xfs_rtcopy_summary(
  96. struct xfs_rtalloc_args *oargs,
  97. struct xfs_rtalloc_args *nargs)
  98. {
  99. xfs_fileoff_t bbno; /* bitmap block number */
  100. int error;
  101. int log; /* summary level number (log length) */
  102. xfs_suminfo_t sum; /* summary data */
  103. for (log = oargs->mp->m_rsumlevels - 1; log >= 0; log--) {
  104. for (bbno = oargs->mp->m_sb.sb_rbmblocks - 1;
  105. (xfs_srtblock_t)bbno >= 0;
  106. bbno--) {
  107. error = xfs_rtget_summary(oargs, log, bbno, &sum);
  108. if (error)
  109. goto out;
  110. if (XFS_IS_CORRUPT(oargs->mp, sum < 0)) {
  111. error = -EFSCORRUPTED;
  112. goto out;
  113. }
  114. if (sum == 0)
  115. continue;
  116. error = xfs_rtmodify_summary(oargs, log, bbno, -sum);
  117. if (error)
  118. goto out;
  119. error = xfs_rtmodify_summary(nargs, log, bbno, sum);
  120. if (error)
  121. goto out;
  122. }
  123. }
  124. error = 0;
  125. out:
  126. xfs_rtbuf_cache_relse(oargs);
  127. return error;
  128. }
  129. /*
  130. * Mark an extent specified by start and len allocated.
  131. * Updates all the summary information as well as the bitmap.
  132. */
  133. STATIC int
  134. xfs_rtallocate_range(
  135. struct xfs_rtalloc_args *args,
  136. xfs_rtxnum_t start, /* start rtext to allocate */
  137. xfs_rtxlen_t len) /* in/out: summary block number */
  138. {
  139. struct xfs_mount *mp = args->mp;
  140. xfs_rtxnum_t end; /* end of the allocated rtext */
  141. int error;
  142. xfs_rtxnum_t postblock = 0; /* first rtext allocated > end */
  143. xfs_rtxnum_t preblock = 0; /* first rtext allocated < start */
  144. end = start + len - 1;
  145. /*
  146. * Assume we're allocating out of the middle of a free extent.
  147. * We need to find the beginning and end of the extent so we can
  148. * properly update the summary.
  149. */
  150. error = xfs_rtfind_back(args, start, &preblock);
  151. if (error)
  152. return error;
  153. /*
  154. * Find the next allocated block (end of free extent).
  155. */
  156. error = xfs_rtfind_forw(args, end, args->rtg->rtg_extents - 1,
  157. &postblock);
  158. if (error)
  159. return error;
  160. /*
  161. * Decrement the summary information corresponding to the entire
  162. * (old) free extent.
  163. */
  164. error = xfs_rtmodify_summary(args,
  165. xfs_highbit64(postblock + 1 - preblock),
  166. xfs_rtx_to_rbmblock(mp, preblock), -1);
  167. if (error)
  168. return error;
  169. /*
  170. * If there are blocks not being allocated at the front of the
  171. * old extent, add summary data for them to be free.
  172. */
  173. if (preblock < start) {
  174. error = xfs_rtmodify_summary(args,
  175. xfs_highbit64(start - preblock),
  176. xfs_rtx_to_rbmblock(mp, preblock), 1);
  177. if (error)
  178. return error;
  179. }
  180. /*
  181. * If there are blocks not being allocated at the end of the
  182. * old extent, add summary data for them to be free.
  183. */
  184. if (postblock > end) {
  185. error = xfs_rtmodify_summary(args,
  186. xfs_highbit64(postblock - end),
  187. xfs_rtx_to_rbmblock(mp, end + 1), 1);
  188. if (error)
  189. return error;
  190. }
  191. /*
  192. * Modify the bitmap to mark this extent allocated.
  193. */
  194. return xfs_rtmodify_range(args, start, len, 0);
  195. }
  196. /* Reduce @rtxlen until it is a multiple of @prod. */
  197. static inline xfs_rtxlen_t
  198. xfs_rtalloc_align_len(
  199. xfs_rtxlen_t rtxlen,
  200. xfs_rtxlen_t prod)
  201. {
  202. if (unlikely(prod > 1))
  203. return rounddown(rtxlen, prod);
  204. return rtxlen;
  205. }
  206. /*
  207. * Make sure we don't run off the end of the rt volume. Be careful that
  208. * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
  209. */
  210. static inline xfs_rtxlen_t
  211. xfs_rtallocate_clamp_len(
  212. struct xfs_rtgroup *rtg,
  213. xfs_rtxnum_t startrtx,
  214. xfs_rtxlen_t rtxlen,
  215. xfs_rtxlen_t prod)
  216. {
  217. xfs_rtxlen_t ret;
  218. ret = min(rtg->rtg_extents, startrtx + rtxlen) - startrtx;
  219. return xfs_rtalloc_align_len(ret, prod);
  220. }
  221. /*
  222. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  223. * bitmap block bbno. If we don't get maxlen then use prod to trim
  224. * the length, if given. Returns error; returns starting block in *rtx.
  225. * The lengths are all in rtextents.
  226. */
  227. STATIC int
  228. xfs_rtallocate_extent_block(
  229. struct xfs_rtalloc_args *args,
  230. xfs_fileoff_t bbno, /* bitmap block number */
  231. xfs_rtxlen_t minlen, /* minimum length to allocate */
  232. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  233. xfs_rtxlen_t *len, /* out: actual length allocated */
  234. xfs_rtxnum_t *nextp, /* out: next rtext to try */
  235. xfs_rtxlen_t prod, /* extent product factor */
  236. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  237. {
  238. struct xfs_mount *mp = args->mp;
  239. xfs_rtxnum_t besti = -1; /* best rtext found so far */
  240. xfs_rtxnum_t end; /* last rtext in chunk */
  241. xfs_rtxnum_t i; /* current rtext trying */
  242. xfs_rtxnum_t next; /* next rtext to try */
  243. xfs_rtxlen_t scanlen; /* number of free rtx to look for */
  244. xfs_rtxlen_t bestlen = 0; /* best length found so far */
  245. int stat; /* status from internal calls */
  246. int error;
  247. /*
  248. * Loop over all the extents starting in this bitmap block up to the
  249. * end of the rt volume, looking for one that's long enough.
  250. */
  251. end = min(args->rtg->rtg_extents, xfs_rbmblock_to_rtx(mp, bbno + 1)) -
  252. 1;
  253. for (i = xfs_rbmblock_to_rtx(mp, bbno); i <= end; i++) {
  254. /* Make sure we don't scan off the end of the rt volume. */
  255. scanlen = xfs_rtallocate_clamp_len(args->rtg, i, maxlen, prod);
  256. if (scanlen < minlen)
  257. break;
  258. /*
  259. * See if there's a free extent of scanlen starting at i.
  260. * If it's not so then next will contain the first non-free.
  261. */
  262. error = xfs_rtcheck_range(args, i, scanlen, 1, &next, &stat);
  263. if (error)
  264. return error;
  265. if (stat) {
  266. /*
  267. * i to scanlen is all free, allocate and return that.
  268. */
  269. *len = scanlen;
  270. *rtx = i;
  271. return 0;
  272. }
  273. /*
  274. * In the case where we have a variable-sized allocation
  275. * request, figure out how big this free piece is,
  276. * and if it's big enough for the minimum, and the best
  277. * so far, remember it.
  278. */
  279. if (minlen < maxlen) {
  280. xfs_rtxnum_t thislen; /* this extent size */
  281. thislen = next - i;
  282. if (thislen >= minlen && thislen > bestlen) {
  283. besti = i;
  284. bestlen = thislen;
  285. }
  286. }
  287. /*
  288. * If not done yet, find the start of the next free space.
  289. */
  290. if (next >= end)
  291. break;
  292. error = xfs_rtfind_forw(args, next, end, &i);
  293. if (error)
  294. return error;
  295. }
  296. /* Searched the whole thing & didn't find a maxlen free extent. */
  297. if (besti == -1)
  298. goto nospace;
  299. /*
  300. * Ensure bestlen is a multiple of prod, but don't return a too-short
  301. * extent.
  302. */
  303. bestlen = xfs_rtalloc_align_len(bestlen, prod);
  304. if (bestlen < minlen)
  305. goto nospace;
  306. /*
  307. * Pick besti for bestlen & return that.
  308. */
  309. *len = bestlen;
  310. *rtx = besti;
  311. return 0;
  312. nospace:
  313. /* Allocation failed. Set *nextp to the next block to try. */
  314. *nextp = next;
  315. return -ENOSPC;
  316. }
  317. /*
  318. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  319. * bno. If we don't get maxlen then use prod to trim the length, if given.
  320. * Returns error; returns starting block in *rtx.
  321. * The lengths are all in rtextents.
  322. */
  323. STATIC int
  324. xfs_rtallocate_extent_exact(
  325. struct xfs_rtalloc_args *args,
  326. xfs_rtxnum_t start, /* starting rtext number to allocate */
  327. xfs_rtxlen_t minlen, /* minimum length to allocate */
  328. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  329. xfs_rtxlen_t *len, /* out: actual length allocated */
  330. xfs_rtxlen_t prod, /* extent product factor */
  331. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  332. {
  333. xfs_rtxnum_t next; /* next rtext to try (dummy) */
  334. xfs_rtxlen_t alloclen; /* candidate length */
  335. xfs_rtxlen_t scanlen; /* number of free rtx to look for */
  336. int isfree; /* extent is free */
  337. int error;
  338. ASSERT(minlen % prod == 0);
  339. ASSERT(maxlen % prod == 0);
  340. /* Make sure we don't run off the end of the rt volume. */
  341. scanlen = xfs_rtallocate_clamp_len(args->rtg, start, maxlen, prod);
  342. if (scanlen < minlen)
  343. return -ENOSPC;
  344. /* Check if the range in question (for scanlen) is free. */
  345. error = xfs_rtcheck_range(args, start, scanlen, 1, &next, &isfree);
  346. if (error)
  347. return error;
  348. if (isfree) {
  349. /* start to scanlen is all free; allocate it. */
  350. *len = scanlen;
  351. *rtx = start;
  352. return 0;
  353. }
  354. /*
  355. * If not, allocate what there is, if it's at least minlen.
  356. */
  357. alloclen = next - start;
  358. if (alloclen < minlen)
  359. return -ENOSPC;
  360. /* Ensure alloclen is a multiple of prod. */
  361. alloclen = xfs_rtalloc_align_len(alloclen, prod);
  362. if (alloclen < minlen)
  363. return -ENOSPC;
  364. *len = alloclen;
  365. *rtx = start;
  366. return 0;
  367. }
  368. /*
  369. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  370. * to start as possible. If we don't get maxlen then use prod to trim
  371. * the length, if given. The lengths are all in rtextents.
  372. */
  373. STATIC int
  374. xfs_rtallocate_extent_near(
  375. struct xfs_rtalloc_args *args,
  376. xfs_rtxnum_t start, /* starting rtext number to allocate */
  377. xfs_rtxlen_t minlen, /* minimum length to allocate */
  378. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  379. xfs_rtxlen_t *len, /* out: actual length allocated */
  380. xfs_rtxlen_t prod, /* extent product factor */
  381. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  382. {
  383. struct xfs_mount *mp = args->mp;
  384. int maxlog; /* max useful extent from summary */
  385. xfs_fileoff_t bbno; /* bitmap block number */
  386. int error;
  387. int i; /* bitmap block offset (loop control) */
  388. int j; /* secondary loop control */
  389. int log2len; /* log2 of minlen */
  390. xfs_rtxnum_t n; /* next rtext to try */
  391. ASSERT(minlen % prod == 0);
  392. ASSERT(maxlen % prod == 0);
  393. /*
  394. * If the block number given is off the end, silently set it to the last
  395. * block.
  396. */
  397. start = min(start, args->rtg->rtg_extents - 1);
  398. /*
  399. * Try the exact allocation first.
  400. */
  401. error = xfs_rtallocate_extent_exact(args, start, minlen, maxlen, len,
  402. prod, rtx);
  403. if (error != -ENOSPC)
  404. return error;
  405. bbno = xfs_rtx_to_rbmblock(mp, start);
  406. i = 0;
  407. j = -1;
  408. ASSERT(minlen != 0);
  409. log2len = xfs_highbit32(minlen);
  410. /*
  411. * Loop over all bitmap blocks (bbno + i is current block).
  412. */
  413. for (;;) {
  414. /*
  415. * Get summary information of extents of all useful levels
  416. * starting in this bitmap block.
  417. */
  418. error = xfs_rtany_summary(args, log2len, mp->m_rsumlevels - 1,
  419. bbno + i, &maxlog);
  420. if (error)
  421. return error;
  422. /*
  423. * If there are any useful extents starting here, try
  424. * allocating one.
  425. */
  426. if (maxlog >= 0) {
  427. xfs_extlen_t maxavail =
  428. min_t(xfs_rtblock_t, maxlen,
  429. (1ULL << (maxlog + 1)) - 1);
  430. /*
  431. * On the positive side of the starting location.
  432. */
  433. if (i >= 0) {
  434. /*
  435. * Try to allocate an extent starting in
  436. * this block.
  437. */
  438. error = xfs_rtallocate_extent_block(args,
  439. bbno + i, minlen, maxavail, len,
  440. &n, prod, rtx);
  441. if (error != -ENOSPC)
  442. return error;
  443. }
  444. /*
  445. * On the negative side of the starting location.
  446. */
  447. else { /* i < 0 */
  448. int maxblocks;
  449. /*
  450. * Loop backwards to find the end of the extent
  451. * we found in the realtime summary.
  452. *
  453. * maxblocks is the maximum possible number of
  454. * bitmap blocks from the start of the extent
  455. * to the end of the extent.
  456. */
  457. if (maxlog == 0)
  458. maxblocks = 0;
  459. else if (maxlog < mp->m_blkbit_log)
  460. maxblocks = 1;
  461. else
  462. maxblocks = 2 << (maxlog - mp->m_blkbit_log);
  463. /*
  464. * We need to check bbno + i + maxblocks down to
  465. * bbno + i. We already checked bbno down to
  466. * bbno + j + 1, so we don't need to check those
  467. * again.
  468. */
  469. j = min(i + maxblocks, j);
  470. for (; j >= i; j--) {
  471. error = xfs_rtallocate_extent_block(args,
  472. bbno + j, minlen,
  473. maxavail, len, &n, prod,
  474. rtx);
  475. if (error != -ENOSPC)
  476. return error;
  477. }
  478. }
  479. }
  480. /*
  481. * Loop control. If we were on the positive side, and there's
  482. * still more blocks on the negative side, go there.
  483. */
  484. if (i > 0 && (int)bbno - i >= 0)
  485. i = -i;
  486. /*
  487. * If positive, and no more negative, but there are more
  488. * positive, go there.
  489. */
  490. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  491. i++;
  492. /*
  493. * If negative or 0 (just started), and there are positive
  494. * blocks to go, go there. The 0 case moves to block 1.
  495. */
  496. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  497. i = 1 - i;
  498. /*
  499. * If negative or 0 and there are more negative blocks,
  500. * go there.
  501. */
  502. else if (i <= 0 && (int)bbno + i > 0)
  503. i--;
  504. /*
  505. * Must be done. Return failure.
  506. */
  507. else
  508. break;
  509. }
  510. return -ENOSPC;
  511. }
  512. static int
  513. xfs_rtalloc_sumlevel(
  514. struct xfs_rtalloc_args *args,
  515. int l, /* level number */
  516. xfs_rtxlen_t minlen, /* minimum length to allocate */
  517. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  518. xfs_rtxlen_t prod, /* extent product factor */
  519. xfs_rtxlen_t *len, /* out: actual length allocated */
  520. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  521. {
  522. xfs_fileoff_t i; /* bitmap block number */
  523. int error;
  524. for (i = 0; i < args->mp->m_sb.sb_rbmblocks; i++) {
  525. xfs_suminfo_t sum; /* summary information for extents */
  526. xfs_rtxnum_t n; /* next rtext to be tried */
  527. error = xfs_rtget_summary(args, l, i, &sum);
  528. if (error)
  529. return error;
  530. /*
  531. * Nothing there, on to the next block.
  532. */
  533. if (!sum)
  534. continue;
  535. /*
  536. * Try allocating the extent.
  537. */
  538. error = xfs_rtallocate_extent_block(args, i, minlen, maxlen,
  539. len, &n, prod, rtx);
  540. if (error != -ENOSPC)
  541. return error;
  542. /*
  543. * If the "next block to try" returned from the allocator is
  544. * beyond the next bitmap block, skip to that bitmap block.
  545. */
  546. if (xfs_rtx_to_rbmblock(args->mp, n) > i + 1)
  547. i = xfs_rtx_to_rbmblock(args->mp, n) - 1;
  548. }
  549. return -ENOSPC;
  550. }
  551. /*
  552. * Allocate an extent of length minlen<=len<=maxlen, with no position
  553. * specified. If we don't get maxlen then use prod to trim
  554. * the length, if given. The lengths are all in rtextents.
  555. */
  556. static int
  557. xfs_rtallocate_extent_size(
  558. struct xfs_rtalloc_args *args,
  559. xfs_rtxlen_t minlen, /* minimum length to allocate */
  560. xfs_rtxlen_t maxlen, /* maximum length to allocate */
  561. xfs_rtxlen_t *len, /* out: actual length allocated */
  562. xfs_rtxlen_t prod, /* extent product factor */
  563. xfs_rtxnum_t *rtx) /* out: start rtext allocated */
  564. {
  565. int error;
  566. int l; /* level number (loop control) */
  567. ASSERT(minlen % prod == 0);
  568. ASSERT(maxlen % prod == 0);
  569. ASSERT(maxlen != 0);
  570. /*
  571. * Loop over all the levels starting with maxlen.
  572. *
  573. * At each level, look at all the bitmap blocks, to see if there are
  574. * extents starting there that are long enough (>= maxlen).
  575. *
  576. * Note, only on the initial level can the allocation fail if the
  577. * summary says there's an extent.
  578. */
  579. for (l = xfs_highbit32(maxlen); l < args->mp->m_rsumlevels; l++) {
  580. error = xfs_rtalloc_sumlevel(args, l, minlen, maxlen, prod, len,
  581. rtx);
  582. if (error != -ENOSPC)
  583. return error;
  584. }
  585. /*
  586. * Didn't find any maxlen blocks. Try smaller ones, unless we are
  587. * looking for a fixed size extent.
  588. */
  589. if (minlen > --maxlen)
  590. return -ENOSPC;
  591. ASSERT(minlen != 0);
  592. ASSERT(maxlen != 0);
  593. /*
  594. * Loop over sizes, from maxlen down to minlen.
  595. *
  596. * This time, when we do the allocations, allow smaller ones to succeed,
  597. * but make sure the specified minlen/maxlen are in the possible range
  598. * for this summary level.
  599. */
  600. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  601. error = xfs_rtalloc_sumlevel(args, l,
  602. max_t(xfs_rtxlen_t, minlen, 1 << l),
  603. min_t(xfs_rtxlen_t, maxlen, (1 << (l + 1)) - 1),
  604. prod, len, rtx);
  605. if (error != -ENOSPC)
  606. return error;
  607. }
  608. return -ENOSPC;
  609. }
  610. static void
  611. xfs_rtunmount_rtg(
  612. struct xfs_rtgroup *rtg)
  613. {
  614. int i;
  615. for (i = 0; i < XFS_RTGI_MAX; i++)
  616. xfs_rtginode_irele(&rtg->rtg_inodes[i]);
  617. if (!xfs_has_zoned(rtg_mount(rtg)))
  618. kvfree(rtg->rtg_rsum_cache);
  619. }
  620. static int
  621. xfs_alloc_rsum_cache(
  622. struct xfs_rtgroup *rtg,
  623. xfs_extlen_t rbmblocks)
  624. {
  625. /*
  626. * The rsum cache is initialized to the maximum value, which is
  627. * trivially an upper bound on the maximum level with any free extents.
  628. */
  629. rtg->rtg_rsum_cache = kvmalloc(rbmblocks, GFP_KERNEL);
  630. if (!rtg->rtg_rsum_cache)
  631. return -ENOMEM;
  632. memset(rtg->rtg_rsum_cache, -1, rbmblocks);
  633. return 0;
  634. }
  635. /*
  636. * If we changed the rt extent size (meaning there was no rt volume previously)
  637. * and the root directory had EXTSZINHERIT and RTINHERIT set, it's possible
  638. * that the extent size hint on the root directory is no longer congruent with
  639. * the new rt extent size. Log the rootdir inode to fix this.
  640. */
  641. static int
  642. xfs_growfs_rt_fixup_extsize(
  643. struct xfs_mount *mp)
  644. {
  645. struct xfs_inode *ip = mp->m_rootip;
  646. struct xfs_trans *tp;
  647. int error = 0;
  648. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  649. if (!(ip->i_diflags & XFS_DIFLAG_RTINHERIT) ||
  650. !(ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT))
  651. goto out_iolock;
  652. error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_ichange, 0, 0, false,
  653. &tp);
  654. if (error)
  655. goto out_iolock;
  656. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  657. error = xfs_trans_commit(tp);
  658. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  659. out_iolock:
  660. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  661. return error;
  662. }
  663. /* Ensure that the rtgroup metadata inode is loaded, creating it if neeeded. */
  664. static int
  665. xfs_rtginode_ensure(
  666. struct xfs_rtgroup *rtg,
  667. enum xfs_rtg_inodes type)
  668. {
  669. struct xfs_trans *tp;
  670. int error;
  671. if (rtg->rtg_inodes[type])
  672. return 0;
  673. tp = xfs_trans_alloc_empty(rtg_mount(rtg));
  674. error = xfs_rtginode_load(rtg, type, tp);
  675. xfs_trans_cancel(tp);
  676. if (error != -ENOENT)
  677. return 0;
  678. return xfs_rtginode_create(rtg, type, true);
  679. }
  680. static struct xfs_mount *
  681. xfs_growfs_rt_alloc_fake_mount(
  682. const struct xfs_mount *mp,
  683. xfs_rfsblock_t rblocks,
  684. xfs_agblock_t rextsize)
  685. {
  686. struct xfs_mount *nmp;
  687. nmp = kmemdup(mp, sizeof(*mp), GFP_KERNEL);
  688. if (!nmp)
  689. return NULL;
  690. xfs_mount_sb_set_rextsize(nmp, &nmp->m_sb, rextsize);
  691. nmp->m_sb.sb_rblocks = rblocks;
  692. nmp->m_sb.sb_rextents = xfs_blen_to_rtbxlen(nmp, nmp->m_sb.sb_rblocks);
  693. nmp->m_sb.sb_rbmblocks = xfs_rtbitmap_blockcount(nmp);
  694. nmp->m_sb.sb_rextslog = xfs_compute_rextslog(nmp->m_sb.sb_rextents);
  695. if (xfs_has_rtgroups(nmp))
  696. nmp->m_sb.sb_rgcount = howmany_64(nmp->m_sb.sb_rextents,
  697. nmp->m_sb.sb_rgextents);
  698. else
  699. nmp->m_sb.sb_rgcount = 1;
  700. nmp->m_rsumblocks = xfs_rtsummary_blockcount(nmp, &nmp->m_rsumlevels);
  701. if (rblocks > 0)
  702. nmp->m_features |= XFS_FEAT_REALTIME;
  703. /* recompute growfsrt reservation from new rsumsize */
  704. xfs_trans_resv_calc(nmp, &nmp->m_resv);
  705. return nmp;
  706. }
  707. /* Free all the new space and return the number of extents actually freed. */
  708. static int
  709. xfs_growfs_rt_free_new(
  710. struct xfs_rtgroup *rtg,
  711. struct xfs_rtalloc_args *nargs,
  712. xfs_rtbxlen_t *freed_rtx)
  713. {
  714. struct xfs_mount *mp = rtg_mount(rtg);
  715. xfs_rgnumber_t rgno = rtg_rgno(rtg);
  716. xfs_rtxnum_t start_rtx = 0, end_rtx;
  717. if (rgno < mp->m_sb.sb_rgcount)
  718. start_rtx = xfs_rtgroup_extents(mp, rgno);
  719. end_rtx = xfs_rtgroup_extents(nargs->mp, rgno);
  720. /*
  721. * Compute the first new extent that we want to free, being careful to
  722. * skip past a realtime superblock at the start of the realtime volume.
  723. */
  724. if (xfs_has_rtsb(nargs->mp) && rgno == 0 && start_rtx == 0)
  725. start_rtx++;
  726. *freed_rtx = end_rtx - start_rtx;
  727. return xfs_rtfree_range(nargs, start_rtx, *freed_rtx);
  728. }
  729. static xfs_rfsblock_t
  730. xfs_growfs_rt_nrblocks(
  731. struct xfs_rtgroup *rtg,
  732. xfs_rfsblock_t nrblocks,
  733. xfs_agblock_t rextsize,
  734. xfs_fileoff_t bmbno)
  735. {
  736. struct xfs_mount *mp = rtg_mount(rtg);
  737. xfs_rfsblock_t step;
  738. step = (bmbno + 1) * mp->m_rtx_per_rbmblock * rextsize;
  739. if (xfs_has_rtgroups(mp)) {
  740. xfs_rfsblock_t rgblocks = mp->m_sb.sb_rgextents * rextsize;
  741. step = min(rgblocks, step) + rgblocks * rtg_rgno(rtg);
  742. }
  743. return min(nrblocks, step);
  744. }
  745. /*
  746. * If the post-grow filesystem will have an rtsb; we're initializing the first
  747. * rtgroup; and the filesystem didn't have a realtime section, write the rtsb
  748. * now, and attach the rtsb buffer to the real mount.
  749. */
  750. static int
  751. xfs_growfs_rt_init_rtsb(
  752. const struct xfs_rtalloc_args *nargs,
  753. const struct xfs_rtgroup *rtg,
  754. const struct xfs_rtalloc_args *args)
  755. {
  756. struct xfs_mount *mp = args->mp;
  757. struct xfs_buf *rtsb_bp;
  758. int error;
  759. if (!xfs_has_rtsb(nargs->mp))
  760. return 0;
  761. if (rtg_rgno(rtg) > 0)
  762. return 0;
  763. if (mp->m_sb.sb_rblocks)
  764. return 0;
  765. error = xfs_buf_get_uncached(mp->m_rtdev_targp, XFS_FSB_TO_BB(mp, 1),
  766. &rtsb_bp);
  767. if (error)
  768. return error;
  769. rtsb_bp->b_maps[0].bm_bn = XFS_RTSB_DADDR;
  770. rtsb_bp->b_ops = &xfs_rtsb_buf_ops;
  771. xfs_update_rtsb(rtsb_bp, mp->m_sb_bp);
  772. mp->m_rtsb_bp = rtsb_bp;
  773. error = xfs_bwrite(rtsb_bp);
  774. xfs_buf_unlock(rtsb_bp);
  775. if (error)
  776. return error;
  777. /* Initialize the rtrmap to reflect the rtsb. */
  778. if (rtg_rmap(args->rtg) != NULL)
  779. error = xfs_rtrmapbt_init_rtsb(nargs->mp, args->rtg, args->tp);
  780. return error;
  781. }
  782. static void
  783. xfs_growfs_rt_sb_fields(
  784. struct xfs_trans *tp,
  785. const struct xfs_mount *nmp)
  786. {
  787. struct xfs_mount *mp = tp->t_mountp;
  788. if (nmp->m_sb.sb_rextsize != mp->m_sb.sb_rextsize)
  789. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  790. nmp->m_sb.sb_rextsize - mp->m_sb.sb_rextsize);
  791. if (nmp->m_sb.sb_rbmblocks != mp->m_sb.sb_rbmblocks)
  792. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  793. nmp->m_sb.sb_rbmblocks - mp->m_sb.sb_rbmblocks);
  794. if (nmp->m_sb.sb_rblocks != mp->m_sb.sb_rblocks)
  795. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  796. nmp->m_sb.sb_rblocks - mp->m_sb.sb_rblocks);
  797. if (nmp->m_sb.sb_rextents != mp->m_sb.sb_rextents)
  798. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  799. nmp->m_sb.sb_rextents - mp->m_sb.sb_rextents);
  800. if (nmp->m_sb.sb_rextslog != mp->m_sb.sb_rextslog)
  801. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  802. nmp->m_sb.sb_rextslog - mp->m_sb.sb_rextslog);
  803. if (nmp->m_sb.sb_rgcount != mp->m_sb.sb_rgcount)
  804. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RGCOUNT,
  805. nmp->m_sb.sb_rgcount - mp->m_sb.sb_rgcount);
  806. }
  807. static int
  808. xfs_growfs_rt_zoned(
  809. struct xfs_rtgroup *rtg,
  810. xfs_rfsblock_t nrblocks)
  811. {
  812. struct xfs_mount *mp = rtg_mount(rtg);
  813. struct xfs_mount *nmp;
  814. struct xfs_trans *tp;
  815. xfs_rtbxlen_t freed_rtx;
  816. int error;
  817. /*
  818. * Calculate new sb and mount fields for this round. Also ensure the
  819. * rtg_extents value is uptodate as the rtbitmap code relies on it.
  820. */
  821. nmp = xfs_growfs_rt_alloc_fake_mount(mp, nrblocks,
  822. mp->m_sb.sb_rextsize);
  823. if (!nmp)
  824. return -ENOMEM;
  825. freed_rtx = nmp->m_sb.sb_rextents - mp->m_sb.sb_rextents;
  826. xfs_rtgroup_calc_geometry(nmp, rtg, rtg_rgno(rtg),
  827. nmp->m_sb.sb_rgcount, nmp->m_sb.sb_rextents);
  828. error = xfs_trans_alloc(mp, &M_RES(nmp)->tr_growrtfree, 0, 0, 0, &tp);
  829. if (error)
  830. goto out_free;
  831. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP);
  832. xfs_rtgroup_trans_join(tp, rtg, XFS_RTGLOCK_RMAP);
  833. xfs_growfs_rt_sb_fields(tp, nmp);
  834. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, freed_rtx);
  835. error = xfs_trans_commit(tp);
  836. if (error)
  837. goto out_free;
  838. /*
  839. * Ensure the mount RT feature flag is now set, and compute new
  840. * maxlevels for rt btrees.
  841. */
  842. mp->m_features |= XFS_FEAT_REALTIME;
  843. xfs_rtrmapbt_compute_maxlevels(mp);
  844. xfs_rtrefcountbt_compute_maxlevels(mp);
  845. xfs_zoned_add_available(mp, freed_rtx);
  846. out_free:
  847. kfree(nmp);
  848. return error;
  849. }
  850. static int
  851. xfs_growfs_rt_bmblock(
  852. struct xfs_rtgroup *rtg,
  853. xfs_rfsblock_t nrblocks,
  854. xfs_agblock_t rextsize,
  855. xfs_fileoff_t bmbno)
  856. {
  857. struct xfs_mount *mp = rtg_mount(rtg);
  858. struct xfs_inode *rbmip = rtg_bitmap(rtg);
  859. struct xfs_inode *rsumip = rtg_summary(rtg);
  860. struct xfs_rtalloc_args args = {
  861. .mp = mp,
  862. .rtg = rtg,
  863. };
  864. struct xfs_rtalloc_args nargs = {
  865. .rtg = rtg,
  866. };
  867. struct xfs_mount *nmp;
  868. xfs_rtbxlen_t freed_rtx;
  869. int error;
  870. /*
  871. * Calculate new sb and mount fields for this round. Also ensure the
  872. * rtg_extents value is uptodate as the rtbitmap code relies on it.
  873. */
  874. nmp = nargs.mp = xfs_growfs_rt_alloc_fake_mount(mp,
  875. xfs_growfs_rt_nrblocks(rtg, nrblocks, rextsize, bmbno),
  876. rextsize);
  877. if (!nmp)
  878. return -ENOMEM;
  879. xfs_rtgroup_calc_geometry(nmp, rtg, rtg_rgno(rtg),
  880. nmp->m_sb.sb_rgcount, nmp->m_sb.sb_rextents);
  881. /*
  882. * Recompute the growfsrt reservation from the new rsumsize, so that the
  883. * transaction below use the new, potentially larger value.
  884. * */
  885. xfs_trans_resv_calc(nmp, &nmp->m_resv);
  886. error = xfs_trans_alloc(mp, &M_RES(nmp)->tr_growrtfree, 0, 0, 0,
  887. &args.tp);
  888. if (error)
  889. goto out_free;
  890. nargs.tp = args.tp;
  891. xfs_rtgroup_lock(args.rtg, XFS_RTGLOCK_BITMAP | XFS_RTGLOCK_RMAP);
  892. xfs_rtgroup_trans_join(args.tp, args.rtg,
  893. XFS_RTGLOCK_BITMAP | XFS_RTGLOCK_RMAP);
  894. /*
  895. * Update the bitmap inode's size ondisk and incore. We need to update
  896. * the incore size so that inode inactivation won't punch what it thinks
  897. * are "posteof" blocks.
  898. */
  899. rbmip->i_disk_size = nmp->m_sb.sb_rbmblocks * nmp->m_sb.sb_blocksize;
  900. i_size_write(VFS_I(rbmip), rbmip->i_disk_size);
  901. xfs_trans_log_inode(args.tp, rbmip, XFS_ILOG_CORE);
  902. /*
  903. * Update the summary inode's size. We need to update the incore size
  904. * so that inode inactivation won't punch what it thinks are "posteof"
  905. * blocks.
  906. */
  907. rsumip->i_disk_size = nmp->m_rsumblocks * nmp->m_sb.sb_blocksize;
  908. i_size_write(VFS_I(rsumip), rsumip->i_disk_size);
  909. xfs_trans_log_inode(args.tp, rsumip, XFS_ILOG_CORE);
  910. /*
  911. * Copy summary data from old to new sizes when the real size (not
  912. * block-aligned) changes.
  913. */
  914. if (mp->m_sb.sb_rbmblocks != nmp->m_sb.sb_rbmblocks ||
  915. mp->m_rsumlevels != nmp->m_rsumlevels) {
  916. error = xfs_rtcopy_summary(&args, &nargs);
  917. if (error)
  918. goto out_cancel;
  919. }
  920. error = xfs_growfs_rt_init_rtsb(&nargs, rtg, &args);
  921. if (error)
  922. goto out_cancel;
  923. /*
  924. * Update superblock fields.
  925. */
  926. xfs_growfs_rt_sb_fields(args.tp, nmp);
  927. /*
  928. * Free the new extent.
  929. */
  930. error = xfs_growfs_rt_free_new(rtg, &nargs, &freed_rtx);
  931. xfs_rtbuf_cache_relse(&nargs);
  932. if (error)
  933. goto out_cancel;
  934. /*
  935. * Mark more blocks free in the superblock.
  936. */
  937. xfs_trans_mod_sb(args.tp, XFS_TRANS_SB_FREXTENTS, freed_rtx);
  938. /*
  939. * Update the calculated values in the real mount structure.
  940. */
  941. mp->m_rsumlevels = nmp->m_rsumlevels;
  942. mp->m_rsumblocks = nmp->m_rsumblocks;
  943. /*
  944. * Recompute the growfsrt reservation from the new rsumsize.
  945. */
  946. xfs_trans_resv_calc(mp, &mp->m_resv);
  947. /*
  948. * Sync sb counters now to reflect the updated values. Lazy counters are
  949. * not always updated and in order to avoid inconsistencies between
  950. * frextents and rtextents, it is better to sync the counters.
  951. */
  952. if (xfs_has_lazysbcount(mp))
  953. xfs_log_sb(args.tp);
  954. error = xfs_trans_commit(args.tp);
  955. if (error)
  956. goto out_free;
  957. /*
  958. * Ensure the mount RT feature flag is now set, and compute new
  959. * maxlevels for rt btrees.
  960. */
  961. mp->m_features |= XFS_FEAT_REALTIME;
  962. xfs_rtrmapbt_compute_maxlevels(mp);
  963. xfs_rtrefcountbt_compute_maxlevels(mp);
  964. kfree(nmp);
  965. return 0;
  966. out_cancel:
  967. xfs_trans_cancel(args.tp);
  968. out_free:
  969. kfree(nmp);
  970. return error;
  971. }
  972. static xfs_rtxnum_t
  973. xfs_last_rtgroup_extents(
  974. struct xfs_mount *mp)
  975. {
  976. return mp->m_sb.sb_rextents -
  977. ((xfs_rtxnum_t)(mp->m_sb.sb_rgcount - 1) *
  978. mp->m_sb.sb_rgextents);
  979. }
  980. /*
  981. * This will return the bitmap block number (indexed at 0) that will be
  982. * extended/modified. There are 2 cases here:
  983. * 1. The size of the rtg is such that it is a multiple of
  984. * xfs_rtbitmap_rtx_per_rbmblock() i.e, an integral number of bitmap blocks
  985. * are completely filled up. In this case, we should return
  986. * 1 + (the last used bitmap block number).
  987. * 2. The size of the rtg is not an multiple of xfs_rtbitmap_rtx_per_rbmblock().
  988. * Here we will return the block number of last used block number. In this
  989. * case, we will modify the last used bitmap block to extend the size of the
  990. * rtgroup.
  991. *
  992. * This also deals with the case where there were no rtextents before.
  993. */
  994. static xfs_fileoff_t
  995. xfs_last_rt_bmblock_to_extend(
  996. struct xfs_rtgroup *rtg)
  997. {
  998. struct xfs_mount *mp = rtg_mount(rtg);
  999. xfs_rgnumber_t rgno = rtg_rgno(rtg);
  1000. xfs_fileoff_t bmbno = 0;
  1001. unsigned int mod = 0;
  1002. ASSERT(!mp->m_sb.sb_rgcount || rgno >= mp->m_sb.sb_rgcount - 1);
  1003. if (mp->m_sb.sb_rgcount && rgno == mp->m_sb.sb_rgcount - 1) {
  1004. xfs_rtxnum_t nrext = xfs_last_rtgroup_extents(mp);
  1005. /* Also fill up the previous block if not entirely full. */
  1006. /* We are doing a -1 to convert it to a 0 based index */
  1007. bmbno = xfs_rtbitmap_blockcount_len(mp, nrext) - 1;
  1008. div_u64_rem(nrext, xfs_rtbitmap_rtx_per_rbmblock(mp), &mod);
  1009. /*
  1010. * mod = 0 means that all the current blocks are full. So
  1011. * return the next block number to be used for the rtgroup
  1012. * growth.
  1013. */
  1014. if (mod == 0)
  1015. bmbno++;
  1016. }
  1017. return bmbno;
  1018. }
  1019. /*
  1020. * Allocate space to the bitmap and summary files, as necessary.
  1021. */
  1022. static int
  1023. xfs_growfs_rt_alloc_blocks(
  1024. struct xfs_rtgroup *rtg,
  1025. xfs_rfsblock_t nrblocks,
  1026. xfs_agblock_t rextsize,
  1027. xfs_extlen_t *nrbmblocks)
  1028. {
  1029. struct xfs_mount *mp = rtg_mount(rtg);
  1030. struct xfs_inode *rbmip = rtg_bitmap(rtg);
  1031. struct xfs_inode *rsumip = rtg_summary(rtg);
  1032. xfs_extlen_t orbmblocks = 0;
  1033. xfs_extlen_t orsumblocks = 0;
  1034. struct xfs_mount *nmp;
  1035. int error = 0;
  1036. nmp = xfs_growfs_rt_alloc_fake_mount(mp, nrblocks, rextsize);
  1037. if (!nmp)
  1038. return -ENOMEM;
  1039. *nrbmblocks = nmp->m_sb.sb_rbmblocks;
  1040. if (xfs_has_rtgroups(mp)) {
  1041. /*
  1042. * For file systems with the rtgroups feature, the RT bitmap and
  1043. * summary are always fully allocated, which means that we never
  1044. * need to grow the existing files.
  1045. *
  1046. * But we have to be careful to only fill the bitmap until the
  1047. * end of the actually used range.
  1048. */
  1049. if (rtg_rgno(rtg) == nmp->m_sb.sb_rgcount - 1)
  1050. *nrbmblocks = xfs_rtbitmap_blockcount_len(nmp,
  1051. xfs_last_rtgroup_extents(nmp));
  1052. if (mp->m_sb.sb_rgcount &&
  1053. rtg_rgno(rtg) == mp->m_sb.sb_rgcount - 1)
  1054. goto out_free;
  1055. } else {
  1056. /*
  1057. * Get the old block counts for bitmap and summary inodes.
  1058. * These can't change since other growfs callers are locked out.
  1059. */
  1060. orbmblocks = XFS_B_TO_FSB(mp, rbmip->i_disk_size);
  1061. orsumblocks = XFS_B_TO_FSB(mp, rsumip->i_disk_size);
  1062. }
  1063. error = xfs_rtfile_initialize_blocks(rtg, XFS_RTGI_BITMAP, orbmblocks,
  1064. nmp->m_sb.sb_rbmblocks, NULL);
  1065. if (error)
  1066. goto out_free;
  1067. error = xfs_rtfile_initialize_blocks(rtg, XFS_RTGI_SUMMARY, orsumblocks,
  1068. nmp->m_rsumblocks, NULL);
  1069. out_free:
  1070. kfree(nmp);
  1071. return error;
  1072. }
  1073. static int
  1074. xfs_growfs_rtg(
  1075. struct xfs_mount *mp,
  1076. xfs_rgnumber_t rgno,
  1077. xfs_rfsblock_t nrblocks,
  1078. xfs_agblock_t rextsize)
  1079. {
  1080. uint8_t *old_rsum_cache = NULL;
  1081. xfs_extlen_t bmblocks;
  1082. xfs_fileoff_t bmbno;
  1083. struct xfs_rtgroup *rtg;
  1084. unsigned int i;
  1085. int error;
  1086. rtg = xfs_rtgroup_grab(mp, rgno);
  1087. if (!rtg)
  1088. return -EINVAL;
  1089. for (i = 0; i < XFS_RTGI_MAX; i++) {
  1090. error = xfs_rtginode_ensure(rtg, i);
  1091. if (error)
  1092. goto out_rele;
  1093. }
  1094. if (xfs_has_zoned(mp)) {
  1095. error = xfs_growfs_rt_zoned(rtg, nrblocks);
  1096. goto out_rele;
  1097. }
  1098. error = xfs_growfs_rt_alloc_blocks(rtg, nrblocks, rextsize, &bmblocks);
  1099. if (error)
  1100. goto out_rele;
  1101. if (bmblocks != rtg_mount(rtg)->m_sb.sb_rbmblocks) {
  1102. old_rsum_cache = rtg->rtg_rsum_cache;
  1103. error = xfs_alloc_rsum_cache(rtg, bmblocks);
  1104. if (error)
  1105. goto out_rele;
  1106. }
  1107. for (bmbno = xfs_last_rt_bmblock_to_extend(rtg); bmbno < bmblocks;
  1108. bmbno++) {
  1109. error = xfs_growfs_rt_bmblock(rtg, nrblocks, rextsize, bmbno);
  1110. if (error)
  1111. goto out_error;
  1112. }
  1113. kvfree(old_rsum_cache);
  1114. goto out_rele;
  1115. out_error:
  1116. /*
  1117. * Reset rtg_extents to the old value if adding more blocks failed.
  1118. */
  1119. xfs_rtgroup_calc_geometry(mp, rtg, rtg_rgno(rtg), mp->m_sb.sb_rgcount,
  1120. mp->m_sb.sb_rextents);
  1121. if (old_rsum_cache) {
  1122. kvfree(rtg->rtg_rsum_cache);
  1123. rtg->rtg_rsum_cache = old_rsum_cache;
  1124. }
  1125. out_rele:
  1126. xfs_rtgroup_rele(rtg);
  1127. return error;
  1128. }
  1129. int
  1130. xfs_growfs_check_rtgeom(
  1131. const struct xfs_mount *mp,
  1132. xfs_rfsblock_t dblocks,
  1133. xfs_rfsblock_t rblocks,
  1134. xfs_extlen_t rextsize)
  1135. {
  1136. xfs_extlen_t min_logfsbs;
  1137. struct xfs_mount *nmp;
  1138. nmp = xfs_growfs_rt_alloc_fake_mount(mp, rblocks, rextsize);
  1139. if (!nmp)
  1140. return -ENOMEM;
  1141. nmp->m_sb.sb_dblocks = dblocks;
  1142. xfs_rtrmapbt_compute_maxlevels(nmp);
  1143. xfs_rtrefcountbt_compute_maxlevels(nmp);
  1144. xfs_trans_resv_calc(nmp, M_RES(nmp));
  1145. /*
  1146. * New summary size can't be more than half the size of the log. This
  1147. * prevents us from getting a log overflow, since we'll log basically
  1148. * the whole summary file at once.
  1149. */
  1150. min_logfsbs = min_t(xfs_extlen_t, xfs_log_calc_minimum_size(nmp),
  1151. nmp->m_rsumblocks * 2);
  1152. trace_xfs_growfs_check_rtgeom(mp, min_logfsbs);
  1153. if (min_logfsbs > mp->m_sb.sb_logblocks)
  1154. goto out_inval;
  1155. if (xfs_has_zoned(mp)) {
  1156. uint32_t gblocks = mp->m_groups[XG_TYPE_RTG].blocks;
  1157. uint32_t rem;
  1158. if (rextsize != 1)
  1159. goto out_inval;
  1160. div_u64_rem(nmp->m_sb.sb_rblocks, gblocks, &rem);
  1161. if (rem) {
  1162. xfs_warn(mp,
  1163. "new RT volume size (%lld) not aligned to RT group size (%d)",
  1164. nmp->m_sb.sb_rblocks, gblocks);
  1165. goto out_inval;
  1166. }
  1167. }
  1168. kfree(nmp);
  1169. return 0;
  1170. out_inval:
  1171. kfree(nmp);
  1172. return -EINVAL;
  1173. }
  1174. /*
  1175. * Compute the new number of rt groups and ensure that /rtgroups exists.
  1176. *
  1177. * Changing the rtgroup size is not allowed (even if the rt volume hasn't yet
  1178. * been initialized) because the userspace ABI doesn't support it.
  1179. */
  1180. static int
  1181. xfs_growfs_rt_prep_groups(
  1182. struct xfs_mount *mp,
  1183. xfs_rfsblock_t rblocks,
  1184. xfs_extlen_t rextsize,
  1185. xfs_rgnumber_t *new_rgcount)
  1186. {
  1187. int error;
  1188. *new_rgcount = howmany_64(rblocks, mp->m_sb.sb_rgextents * rextsize);
  1189. if (*new_rgcount > XFS_MAX_RGNUMBER)
  1190. return -EINVAL;
  1191. /* Make sure the /rtgroups dir has been created */
  1192. if (!mp->m_rtdirip) {
  1193. struct xfs_trans *tp;
  1194. tp = xfs_trans_alloc_empty(mp);
  1195. error = xfs_rtginode_load_parent(tp);
  1196. xfs_trans_cancel(tp);
  1197. if (error == -ENOENT)
  1198. error = xfs_rtginode_mkdir_parent(mp);
  1199. if (error)
  1200. return error;
  1201. }
  1202. return 0;
  1203. }
  1204. static bool
  1205. xfs_grow_last_rtg(
  1206. struct xfs_mount *mp)
  1207. {
  1208. if (!xfs_has_rtgroups(mp))
  1209. return true;
  1210. if (mp->m_sb.sb_rgcount == 0)
  1211. return false;
  1212. return xfs_rtgroup_extents(mp, mp->m_sb.sb_rgcount - 1) <
  1213. mp->m_sb.sb_rgextents;
  1214. }
  1215. /*
  1216. * Read in the last block of the RT device to make sure it is accessible.
  1217. */
  1218. static int
  1219. xfs_rt_check_size(
  1220. struct xfs_mount *mp,
  1221. xfs_rfsblock_t last_block)
  1222. {
  1223. xfs_daddr_t daddr = XFS_FSB_TO_BB(mp, last_block);
  1224. struct xfs_buf *bp;
  1225. int error;
  1226. if (XFS_BB_TO_FSB(mp, daddr) != last_block) {
  1227. xfs_warn(mp, "RT device size overflow: %llu != %llu",
  1228. XFS_BB_TO_FSB(mp, daddr), last_block);
  1229. return -EFBIG;
  1230. }
  1231. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1232. XFS_FSB_TO_BB(mp, mp->m_sb.sb_rtstart) + daddr,
  1233. XFS_FSB_TO_BB(mp, 1), &bp, NULL);
  1234. if (error)
  1235. xfs_warn(mp, "cannot read last RT device sector (%lld)",
  1236. last_block);
  1237. else
  1238. xfs_buf_relse(bp);
  1239. return error;
  1240. }
  1241. /*
  1242. * Grow the realtime area of the filesystem.
  1243. */
  1244. int
  1245. xfs_growfs_rt(
  1246. struct xfs_mount *mp,
  1247. struct xfs_growfs_rt *in)
  1248. {
  1249. xfs_rgnumber_t old_rgcount = mp->m_sb.sb_rgcount;
  1250. xfs_rgnumber_t new_rgcount = 1;
  1251. xfs_rgnumber_t rgno;
  1252. xfs_agblock_t old_rextsize = mp->m_sb.sb_rextsize;
  1253. int error;
  1254. if (!capable(CAP_SYS_ADMIN))
  1255. return -EPERM;
  1256. /* Needs to have been mounted with an rt device. */
  1257. if (!XFS_IS_REALTIME_MOUNT(mp))
  1258. return -EINVAL;
  1259. if (!mutex_trylock(&mp->m_growlock))
  1260. return -EWOULDBLOCK;
  1261. /* Shrink not supported. */
  1262. error = -EINVAL;
  1263. if (in->newblocks <= mp->m_sb.sb_rblocks)
  1264. goto out_unlock;
  1265. /* Can only change rt extent size when adding rt volume. */
  1266. if (mp->m_sb.sb_rblocks > 0 && in->extsize != mp->m_sb.sb_rextsize)
  1267. goto out_unlock;
  1268. /* Range check the extent size. */
  1269. if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
  1270. XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
  1271. goto out_unlock;
  1272. /* Check for features supported only on rtgroups filesystems. */
  1273. error = -EOPNOTSUPP;
  1274. if (!xfs_has_rtgroups(mp)) {
  1275. if (xfs_has_rmapbt(mp))
  1276. goto out_unlock;
  1277. if (xfs_has_quota(mp))
  1278. goto out_unlock;
  1279. if (xfs_has_reflink(mp))
  1280. goto out_unlock;
  1281. } else if (xfs_has_reflink(mp) &&
  1282. !xfs_reflink_supports_rextsize(mp, in->extsize))
  1283. goto out_unlock;
  1284. error = xfs_sb_validate_fsb_count(&mp->m_sb, in->newblocks);
  1285. if (error)
  1286. goto out_unlock;
  1287. error = xfs_rt_check_size(mp, in->newblocks - 1);
  1288. if (error)
  1289. goto out_unlock;
  1290. /*
  1291. * Calculate new parameters. These are the final values to be reached.
  1292. */
  1293. error = -EINVAL;
  1294. if (in->newblocks < in->extsize)
  1295. goto out_unlock;
  1296. /* Make sure the new fs size won't cause problems with the log. */
  1297. error = xfs_growfs_check_rtgeom(mp, mp->m_sb.sb_dblocks, in->newblocks,
  1298. in->extsize);
  1299. if (error)
  1300. goto out_unlock;
  1301. if (xfs_has_rtgroups(mp)) {
  1302. error = xfs_growfs_rt_prep_groups(mp, in->newblocks,
  1303. in->extsize, &new_rgcount);
  1304. if (error)
  1305. goto out_unlock;
  1306. }
  1307. if (xfs_grow_last_rtg(mp)) {
  1308. error = xfs_growfs_rtg(mp, old_rgcount - 1, in->newblocks,
  1309. in->extsize);
  1310. if (error)
  1311. goto out_unlock;
  1312. }
  1313. for (rgno = old_rgcount; rgno < new_rgcount; rgno++) {
  1314. xfs_rtbxlen_t rextents = div_u64(in->newblocks, in->extsize);
  1315. error = xfs_rtgroup_alloc(mp, rgno, new_rgcount, rextents);
  1316. if (error)
  1317. goto out_unlock;
  1318. error = xfs_growfs_rtg(mp, rgno, in->newblocks, in->extsize);
  1319. if (error) {
  1320. struct xfs_rtgroup *rtg;
  1321. rtg = xfs_rtgroup_grab(mp, rgno);
  1322. if (!WARN_ON_ONCE(!rtg)) {
  1323. xfs_rtunmount_rtg(rtg);
  1324. xfs_rtgroup_rele(rtg);
  1325. xfs_rtgroup_free(mp, rgno);
  1326. }
  1327. break;
  1328. }
  1329. }
  1330. if (!error && old_rextsize != in->extsize)
  1331. error = xfs_growfs_rt_fixup_extsize(mp);
  1332. /*
  1333. * Update secondary superblocks now the physical grow has completed.
  1334. *
  1335. * Also do this in case of an error as we might have already
  1336. * successfully updated one or more RTGs and incremented sb_rgcount.
  1337. */
  1338. if (!xfs_is_shutdown(mp)) {
  1339. int error2 = xfs_update_secondary_sbs(mp);
  1340. if (!error)
  1341. error = error2;
  1342. /* Reset the rt metadata btree space reservations. */
  1343. error2 = xfs_metafile_resv_init(mp);
  1344. if (error2 && error2 != -ENOSPC)
  1345. error = error2;
  1346. }
  1347. out_unlock:
  1348. mutex_unlock(&mp->m_growlock);
  1349. return error;
  1350. }
  1351. /* Read the realtime superblock and attach it to the mount. */
  1352. int
  1353. xfs_rtmount_readsb(
  1354. struct xfs_mount *mp)
  1355. {
  1356. struct xfs_buf *bp;
  1357. int error;
  1358. if (!xfs_has_rtsb(mp))
  1359. return 0;
  1360. if (mp->m_sb.sb_rblocks == 0)
  1361. return 0;
  1362. if (mp->m_rtdev_targp == NULL) {
  1363. xfs_warn(mp,
  1364. "Filesystem has a realtime volume, use rtdev=device option");
  1365. return -ENODEV;
  1366. }
  1367. /* m_blkbb_log is not set up yet */
  1368. error = xfs_buf_read_uncached(mp->m_rtdev_targp, XFS_RTSB_DADDR,
  1369. mp->m_sb.sb_blocksize >> BBSHIFT, &bp,
  1370. &xfs_rtsb_buf_ops);
  1371. if (error) {
  1372. xfs_warn(mp, "rt sb validate failed with error %d.", error);
  1373. /* bad CRC means corrupted metadata */
  1374. if (error == -EFSBADCRC)
  1375. error = -EFSCORRUPTED;
  1376. return error;
  1377. }
  1378. mp->m_rtsb_bp = bp;
  1379. xfs_buf_unlock(bp);
  1380. return 0;
  1381. }
  1382. /* Detach the realtime superblock from the mount and free it. */
  1383. void
  1384. xfs_rtmount_freesb(
  1385. struct xfs_mount *mp)
  1386. {
  1387. struct xfs_buf *bp = mp->m_rtsb_bp;
  1388. if (!bp)
  1389. return;
  1390. xfs_buf_lock(bp);
  1391. mp->m_rtsb_bp = NULL;
  1392. xfs_buf_relse(bp);
  1393. }
  1394. /*
  1395. * Initialize realtime fields in the mount structure.
  1396. */
  1397. int /* error */
  1398. xfs_rtmount_init(
  1399. struct xfs_mount *mp) /* file system mount structure */
  1400. {
  1401. if (mp->m_sb.sb_rblocks == 0)
  1402. return 0;
  1403. if (mp->m_rtdev_targp == NULL) {
  1404. xfs_warn(mp,
  1405. "Filesystem has a realtime volume, use rtdev=device option");
  1406. return -ENODEV;
  1407. }
  1408. mp->m_rsumblocks = xfs_rtsummary_blockcount(mp, &mp->m_rsumlevels);
  1409. return xfs_rt_check_size(mp, mp->m_sb.sb_rblocks - 1);
  1410. }
  1411. static int
  1412. xfs_rtalloc_count_frextent(
  1413. struct xfs_rtgroup *rtg,
  1414. struct xfs_trans *tp,
  1415. const struct xfs_rtalloc_rec *rec,
  1416. void *priv)
  1417. {
  1418. uint64_t *valp = priv;
  1419. *valp += rec->ar_extcount;
  1420. return 0;
  1421. }
  1422. /*
  1423. * Reinitialize the number of free realtime extents from the realtime bitmap.
  1424. * Callers must ensure that there is no other activity in the filesystem.
  1425. */
  1426. int
  1427. xfs_rtalloc_reinit_frextents(
  1428. struct xfs_mount *mp)
  1429. {
  1430. uint64_t val = 0;
  1431. int error;
  1432. struct xfs_rtgroup *rtg = NULL;
  1433. while ((rtg = xfs_rtgroup_next(mp, rtg))) {
  1434. xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
  1435. error = xfs_rtalloc_query_all(rtg, NULL,
  1436. xfs_rtalloc_count_frextent, &val);
  1437. xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
  1438. if (error) {
  1439. xfs_rtgroup_rele(rtg);
  1440. return error;
  1441. }
  1442. }
  1443. spin_lock(&mp->m_sb_lock);
  1444. mp->m_sb.sb_frextents = val;
  1445. spin_unlock(&mp->m_sb_lock);
  1446. xfs_set_freecounter(mp, XC_FREE_RTEXTENTS, mp->m_sb.sb_frextents);
  1447. return 0;
  1448. }
  1449. /*
  1450. * Read in the bmbt of an rt metadata inode so that we never have to load them
  1451. * at runtime. This enables the use of shared ILOCKs for rtbitmap scans. Use
  1452. * an empty transaction to avoid deadlocking on loops in the bmbt.
  1453. */
  1454. static inline int
  1455. xfs_rtmount_iread_extents(
  1456. struct xfs_trans *tp,
  1457. struct xfs_inode *ip)
  1458. {
  1459. int error;
  1460. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1461. error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
  1462. if (error)
  1463. goto out_unlock;
  1464. if (xfs_inode_has_attr_fork(ip)) {
  1465. error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
  1466. if (error)
  1467. goto out_unlock;
  1468. }
  1469. out_unlock:
  1470. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1471. return error;
  1472. }
  1473. static int
  1474. xfs_rtmount_rtg(
  1475. struct xfs_mount *mp,
  1476. struct xfs_trans *tp,
  1477. struct xfs_rtgroup *rtg)
  1478. {
  1479. int error, i;
  1480. for (i = 0; i < XFS_RTGI_MAX; i++) {
  1481. error = xfs_rtginode_load(rtg, i, tp);
  1482. if (error)
  1483. return error;
  1484. if (rtg->rtg_inodes[i]) {
  1485. error = xfs_rtmount_iread_extents(tp,
  1486. rtg->rtg_inodes[i]);
  1487. if (error)
  1488. return error;
  1489. }
  1490. }
  1491. if (xfs_has_zoned(mp))
  1492. return 0;
  1493. return xfs_alloc_rsum_cache(rtg, mp->m_sb.sb_rbmblocks);
  1494. }
  1495. /*
  1496. * Get the bitmap and summary inodes and the summary cache into the mount
  1497. * structure at mount time.
  1498. */
  1499. int
  1500. xfs_rtmount_inodes(
  1501. struct xfs_mount *mp)
  1502. {
  1503. struct xfs_trans *tp;
  1504. struct xfs_rtgroup *rtg = NULL;
  1505. int error;
  1506. tp = xfs_trans_alloc_empty(mp);
  1507. if (xfs_has_rtgroups(mp) && mp->m_sb.sb_rgcount > 0) {
  1508. error = xfs_rtginode_load_parent(tp);
  1509. if (error)
  1510. goto out_cancel;
  1511. }
  1512. while ((rtg = xfs_rtgroup_next(mp, rtg))) {
  1513. error = xfs_rtmount_rtg(mp, tp, rtg);
  1514. if (error) {
  1515. xfs_rtgroup_rele(rtg);
  1516. xfs_rtunmount_inodes(mp);
  1517. break;
  1518. }
  1519. }
  1520. out_cancel:
  1521. xfs_trans_cancel(tp);
  1522. return error;
  1523. }
  1524. void
  1525. xfs_rtunmount_inodes(
  1526. struct xfs_mount *mp)
  1527. {
  1528. struct xfs_rtgroup *rtg = NULL;
  1529. while ((rtg = xfs_rtgroup_next(mp, rtg)))
  1530. xfs_rtunmount_rtg(rtg);
  1531. xfs_rtginode_irele(&mp->m_rtdirip);
  1532. }
  1533. /*
  1534. * Pick an extent for allocation at the start of a new realtime file.
  1535. * Use the sequence number stored in the atime field of the bitmap inode.
  1536. * Translate this to a fraction of the rtextents, and return the product
  1537. * of rtextents and the fraction.
  1538. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  1539. */
  1540. static xfs_rtxnum_t
  1541. xfs_rtpick_extent(
  1542. struct xfs_rtgroup *rtg,
  1543. struct xfs_trans *tp,
  1544. xfs_rtxlen_t len) /* allocation length (rtextents) */
  1545. {
  1546. struct xfs_mount *mp = rtg_mount(rtg);
  1547. struct xfs_inode *rbmip = rtg_bitmap(rtg);
  1548. xfs_rtxnum_t b = 0; /* result rtext */
  1549. int log2; /* log of sequence number */
  1550. uint64_t resid; /* residual after log removed */
  1551. uint64_t seq; /* sequence number of file creation */
  1552. struct timespec64 ts; /* timespec in inode */
  1553. xfs_assert_ilocked(rbmip, XFS_ILOCK_EXCL);
  1554. ts = inode_get_atime(VFS_I(rbmip));
  1555. if (!(rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
  1556. rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
  1557. seq = 0;
  1558. } else {
  1559. seq = ts.tv_sec;
  1560. }
  1561. log2 = xfs_highbit64(seq);
  1562. if (log2 != -1) {
  1563. resid = seq - (1ULL << log2);
  1564. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  1565. (log2 + 1);
  1566. if (b >= mp->m_sb.sb_rextents)
  1567. div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
  1568. if (b + len > mp->m_sb.sb_rextents)
  1569. b = mp->m_sb.sb_rextents - len;
  1570. }
  1571. ts.tv_sec = seq + 1;
  1572. inode_set_atime_to_ts(VFS_I(rbmip), ts);
  1573. xfs_trans_log_inode(tp, rbmip, XFS_ILOG_CORE);
  1574. return b;
  1575. }
  1576. static void
  1577. xfs_rtalloc_align_minmax(
  1578. xfs_rtxlen_t *raminlen,
  1579. xfs_rtxlen_t *ramaxlen,
  1580. xfs_rtxlen_t *prod)
  1581. {
  1582. xfs_rtxlen_t newmaxlen = *ramaxlen;
  1583. xfs_rtxlen_t newminlen = *raminlen;
  1584. xfs_rtxlen_t slack;
  1585. slack = newmaxlen % *prod;
  1586. if (slack)
  1587. newmaxlen -= slack;
  1588. slack = newminlen % *prod;
  1589. if (slack)
  1590. newminlen += *prod - slack;
  1591. /*
  1592. * If adjusting for extent size hint alignment produces an invalid
  1593. * min/max len combination, go ahead without it.
  1594. */
  1595. if (newmaxlen < newminlen) {
  1596. *prod = 1;
  1597. return;
  1598. }
  1599. *ramaxlen = newmaxlen;
  1600. *raminlen = newminlen;
  1601. }
  1602. /* Given a free extent, find any part of it that isn't busy, if possible. */
  1603. STATIC bool
  1604. xfs_rtalloc_check_busy(
  1605. struct xfs_rtalloc_args *args,
  1606. xfs_rtxnum_t start,
  1607. xfs_rtxlen_t minlen_rtx,
  1608. xfs_rtxlen_t maxlen_rtx,
  1609. xfs_rtxlen_t len_rtx,
  1610. xfs_rtxlen_t prod,
  1611. xfs_rtxnum_t rtx,
  1612. xfs_rtxlen_t *reslen,
  1613. xfs_rtxnum_t *resrtx,
  1614. unsigned *busy_gen)
  1615. {
  1616. struct xfs_rtgroup *rtg = args->rtg;
  1617. struct xfs_mount *mp = rtg_mount(rtg);
  1618. xfs_agblock_t rgbno = xfs_rtx_to_rgbno(rtg, rtx);
  1619. xfs_rgblock_t min_rgbno = xfs_rtx_to_rgbno(rtg, start);
  1620. xfs_extlen_t minlen = xfs_rtxlen_to_extlen(mp, minlen_rtx);
  1621. xfs_extlen_t len = xfs_rtxlen_to_extlen(mp, len_rtx);
  1622. xfs_extlen_t diff;
  1623. bool busy;
  1624. busy = xfs_extent_busy_trim(rtg_group(rtg), minlen,
  1625. xfs_rtxlen_to_extlen(mp, maxlen_rtx), &rgbno, &len,
  1626. busy_gen);
  1627. /*
  1628. * If we have a largish extent that happens to start before min_rgbno,
  1629. * see if we can shift it into range...
  1630. */
  1631. if (rgbno < min_rgbno && rgbno + len > min_rgbno) {
  1632. diff = min_rgbno - rgbno;
  1633. if (len > diff) {
  1634. rgbno += diff;
  1635. len -= diff;
  1636. }
  1637. }
  1638. if (prod > 1 && len >= minlen) {
  1639. xfs_rgblock_t aligned_rgbno = roundup(rgbno, prod);
  1640. diff = aligned_rgbno - rgbno;
  1641. *resrtx = xfs_rgbno_to_rtx(mp, aligned_rgbno);
  1642. *reslen = xfs_extlen_to_rtxlen(mp,
  1643. diff >= len ? 0 : len - diff);
  1644. } else {
  1645. *resrtx = xfs_rgbno_to_rtx(mp, rgbno);
  1646. *reslen = xfs_extlen_to_rtxlen(mp, len);
  1647. }
  1648. return busy;
  1649. }
  1650. /*
  1651. * Adjust the given free extent so that it isn't busy, or flush the log and
  1652. * wait for the space to become unbusy. Only needed for rtgroups.
  1653. */
  1654. STATIC int
  1655. xfs_rtallocate_adjust_for_busy(
  1656. struct xfs_rtalloc_args *args,
  1657. xfs_rtxnum_t start,
  1658. xfs_rtxlen_t minlen,
  1659. xfs_rtxlen_t maxlen,
  1660. xfs_rtxlen_t *len,
  1661. xfs_rtxlen_t prod,
  1662. xfs_rtxnum_t *rtx)
  1663. {
  1664. xfs_rtxnum_t resrtx;
  1665. xfs_rtxlen_t reslen;
  1666. unsigned busy_gen;
  1667. bool busy;
  1668. int error;
  1669. again:
  1670. busy = xfs_rtalloc_check_busy(args, start, minlen, maxlen, *len, prod,
  1671. *rtx, &reslen, &resrtx, &busy_gen);
  1672. if (!busy)
  1673. return 0;
  1674. if (reslen < minlen || (start != 0 && resrtx != *rtx)) {
  1675. /*
  1676. * Enough of the extent was busy that we cannot satisfy the
  1677. * allocation, or this is a near allocation and the start of
  1678. * the extent is busy. Flush the log and wait for the busy
  1679. * situation to resolve.
  1680. */
  1681. trace_xfs_rtalloc_extent_busy(args->rtg, start, minlen, maxlen,
  1682. *len, prod, *rtx, busy_gen);
  1683. error = xfs_extent_busy_flush(args->tp, rtg_group(args->rtg),
  1684. busy_gen, 0);
  1685. if (error)
  1686. return error;
  1687. goto again;
  1688. }
  1689. /* Some of the free space wasn't busy, hand that back to the caller. */
  1690. trace_xfs_rtalloc_extent_busy_trim(args->rtg, *rtx, *len, resrtx,
  1691. reslen);
  1692. *len = reslen;
  1693. *rtx = resrtx;
  1694. return 0;
  1695. }
  1696. static int
  1697. xfs_rtallocate_rtg(
  1698. struct xfs_trans *tp,
  1699. xfs_rgnumber_t rgno,
  1700. xfs_rtblock_t bno_hint,
  1701. xfs_rtxlen_t minlen,
  1702. xfs_rtxlen_t maxlen,
  1703. xfs_rtxlen_t prod,
  1704. bool wasdel,
  1705. bool initial_user_data,
  1706. bool *rtlocked,
  1707. xfs_rtblock_t *bno,
  1708. xfs_extlen_t *blen)
  1709. {
  1710. struct xfs_rtalloc_args args = {
  1711. .mp = tp->t_mountp,
  1712. .tp = tp,
  1713. };
  1714. xfs_rtxnum_t start = 0;
  1715. xfs_rtxnum_t rtx;
  1716. xfs_rtxlen_t len = 0;
  1717. int error = 0;
  1718. args.rtg = xfs_rtgroup_grab(args.mp, rgno);
  1719. if (!args.rtg)
  1720. return -ENOSPC;
  1721. /*
  1722. * We need to lock out modifications to both the RT bitmap and summary
  1723. * inodes for finding free space in xfs_rtallocate_extent_{near,size}
  1724. * and join the bitmap and summary inodes for the actual allocation
  1725. * down in xfs_rtallocate_range.
  1726. *
  1727. * For RTG-enabled file system we don't want to join the inodes to the
  1728. * transaction until we are committed to allocate to allocate from this
  1729. * RTG so that only one inode of each type is locked at a time.
  1730. *
  1731. * But for pre-RTG file systems we need to already to join the bitmap
  1732. * inode to the transaction for xfs_rtpick_extent, which bumps the
  1733. * sequence number in it, so we'll have to join the inode to the
  1734. * transaction early here.
  1735. *
  1736. * This is all a bit messy, but at least the mess is contained in
  1737. * this function.
  1738. */
  1739. if (!*rtlocked) {
  1740. xfs_rtgroup_lock(args.rtg, XFS_RTGLOCK_BITMAP);
  1741. if (!xfs_has_rtgroups(args.mp))
  1742. xfs_rtgroup_trans_join(tp, args.rtg,
  1743. XFS_RTGLOCK_BITMAP);
  1744. *rtlocked = true;
  1745. }
  1746. /*
  1747. * For an allocation to an empty file at offset 0, pick an extent that
  1748. * will space things out in the rt area.
  1749. */
  1750. if (bno_hint != NULLFSBLOCK)
  1751. start = xfs_rtb_to_rtx(args.mp, bno_hint);
  1752. else if (!xfs_has_rtgroups(args.mp) && initial_user_data)
  1753. start = xfs_rtpick_extent(args.rtg, tp, maxlen);
  1754. if (start) {
  1755. error = xfs_rtallocate_extent_near(&args, start, minlen, maxlen,
  1756. &len, prod, &rtx);
  1757. /*
  1758. * If we can't allocate near a specific rt extent, try again
  1759. * without locality criteria.
  1760. */
  1761. if (error == -ENOSPC) {
  1762. xfs_rtbuf_cache_relse(&args);
  1763. error = 0;
  1764. }
  1765. }
  1766. if (!error) {
  1767. error = xfs_rtallocate_extent_size(&args, minlen, maxlen, &len,
  1768. prod, &rtx);
  1769. }
  1770. if (error) {
  1771. if (xfs_has_rtgroups(args.mp))
  1772. goto out_unlock;
  1773. goto out_release;
  1774. }
  1775. if (xfs_has_rtgroups(args.mp)) {
  1776. error = xfs_rtallocate_adjust_for_busy(&args, start, minlen,
  1777. maxlen, &len, prod, &rtx);
  1778. if (error)
  1779. goto out_unlock;
  1780. xfs_rtgroup_trans_join(tp, args.rtg, XFS_RTGLOCK_BITMAP);
  1781. }
  1782. error = xfs_rtallocate_range(&args, rtx, len);
  1783. if (error)
  1784. goto out_release;
  1785. xfs_trans_mod_sb(tp, wasdel ?
  1786. XFS_TRANS_SB_RES_FREXTENTS : XFS_TRANS_SB_FREXTENTS,
  1787. -(long)len);
  1788. *bno = xfs_rtx_to_rtb(args.rtg, rtx);
  1789. *blen = xfs_rtxlen_to_extlen(args.mp, len);
  1790. out_release:
  1791. xfs_rtgroup_rele(args.rtg);
  1792. xfs_rtbuf_cache_relse(&args);
  1793. return error;
  1794. out_unlock:
  1795. xfs_rtgroup_unlock(args.rtg, XFS_RTGLOCK_BITMAP);
  1796. *rtlocked = false;
  1797. goto out_release;
  1798. }
  1799. int
  1800. xfs_rtallocate_rtgs(
  1801. struct xfs_trans *tp,
  1802. xfs_fsblock_t bno_hint,
  1803. xfs_rtxlen_t minlen,
  1804. xfs_rtxlen_t maxlen,
  1805. xfs_rtxlen_t prod,
  1806. bool wasdel,
  1807. bool initial_user_data,
  1808. xfs_rtblock_t *bno,
  1809. xfs_extlen_t *blen)
  1810. {
  1811. struct xfs_mount *mp = tp->t_mountp;
  1812. xfs_rgnumber_t start_rgno, rgno;
  1813. int error;
  1814. /*
  1815. * For now this just blindly iterates over the RTGs for an initial
  1816. * allocation. We could try to keep an in-memory rtg_longest member
  1817. * to avoid the locking when just looking for big enough free space,
  1818. * but for now this keeps things simple.
  1819. */
  1820. if (bno_hint != NULLFSBLOCK)
  1821. start_rgno = xfs_rtb_to_rgno(mp, bno_hint);
  1822. else
  1823. start_rgno = (atomic_inc_return(&mp->m_rtgrotor) - 1) %
  1824. mp->m_sb.sb_rgcount;
  1825. rgno = start_rgno;
  1826. do {
  1827. bool rtlocked = false;
  1828. error = xfs_rtallocate_rtg(tp, rgno, bno_hint, minlen, maxlen,
  1829. prod, wasdel, initial_user_data, &rtlocked,
  1830. bno, blen);
  1831. if (error != -ENOSPC)
  1832. return error;
  1833. ASSERT(!rtlocked);
  1834. if (++rgno == mp->m_sb.sb_rgcount)
  1835. rgno = 0;
  1836. bno_hint = NULLFSBLOCK;
  1837. } while (rgno != start_rgno);
  1838. return -ENOSPC;
  1839. }
  1840. static int
  1841. xfs_rtallocate_align(
  1842. struct xfs_bmalloca *ap,
  1843. xfs_rtxlen_t *ralen,
  1844. xfs_rtxlen_t *raminlen,
  1845. xfs_rtxlen_t *prod,
  1846. bool *noalign)
  1847. {
  1848. struct xfs_mount *mp = ap->ip->i_mount;
  1849. xfs_fileoff_t orig_offset = ap->offset;
  1850. xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
  1851. xfs_extlen_t align; /* minimum allocation alignment */
  1852. xfs_extlen_t mod; /* product factor for allocators */
  1853. int error;
  1854. if (*noalign) {
  1855. align = mp->m_sb.sb_rextsize;
  1856. } else {
  1857. if (ap->flags & XFS_BMAPI_COWFORK)
  1858. align = xfs_get_cowextsz_hint(ap->ip);
  1859. else
  1860. align = xfs_get_extsz_hint(ap->ip);
  1861. if (!align)
  1862. align = 1;
  1863. if (align == mp->m_sb.sb_rextsize)
  1864. *noalign = true;
  1865. }
  1866. error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 1,
  1867. ap->eof, 0, ap->conv, &ap->offset, &ap->length);
  1868. if (error)
  1869. return error;
  1870. ASSERT(ap->length);
  1871. ASSERT(xfs_extlen_to_rtxmod(mp, ap->length) == 0);
  1872. /*
  1873. * If we shifted the file offset downward to satisfy an extent size
  1874. * hint, increase minlen by that amount so that the allocator won't
  1875. * give us an allocation that's too short to cover at least one of the
  1876. * blocks that the caller asked for.
  1877. */
  1878. if (ap->offset != orig_offset)
  1879. minlen += orig_offset - ap->offset;
  1880. /*
  1881. * Set ralen to be the actual requested length in rtextents.
  1882. *
  1883. * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
  1884. * we rounded up to it, cut it back so it's valid again.
  1885. * Note that if it's a really large request (bigger than
  1886. * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
  1887. * adjust the starting point to match it.
  1888. */
  1889. *ralen = xfs_extlen_to_rtxlen(mp, min(ap->length, XFS_MAX_BMBT_EXTLEN));
  1890. *raminlen = max_t(xfs_rtxlen_t, 1, xfs_extlen_to_rtxlen(mp, minlen));
  1891. ASSERT(*raminlen > 0);
  1892. ASSERT(*raminlen <= *ralen);
  1893. /*
  1894. * Only bother calculating a real prod factor if offset & length are
  1895. * perfectly aligned, otherwise it will just get us in trouble.
  1896. */
  1897. div_u64_rem(ap->offset, align, &mod);
  1898. if (mod || ap->length % align)
  1899. *prod = 1;
  1900. else
  1901. *prod = xfs_extlen_to_rtxlen(mp, align);
  1902. if (*prod > 1)
  1903. xfs_rtalloc_align_minmax(raminlen, ralen, prod);
  1904. return 0;
  1905. }
  1906. int
  1907. xfs_bmap_rtalloc(
  1908. struct xfs_bmalloca *ap)
  1909. {
  1910. xfs_fileoff_t orig_offset = ap->offset;
  1911. xfs_rtxlen_t prod = 0; /* product factor for allocators */
  1912. xfs_rtxlen_t ralen = 0; /* realtime allocation length */
  1913. xfs_rtblock_t bno_hint = NULLRTBLOCK;
  1914. xfs_extlen_t orig_length = ap->length;
  1915. xfs_rtxlen_t raminlen;
  1916. bool rtlocked = false;
  1917. bool noalign = false;
  1918. bool initial_user_data =
  1919. ap->datatype & XFS_ALLOC_INITIAL_USER_DATA;
  1920. int error;
  1921. ASSERT(!xfs_has_zoned(ap->tp->t_mountp));
  1922. retry:
  1923. error = xfs_rtallocate_align(ap, &ralen, &raminlen, &prod, &noalign);
  1924. if (error)
  1925. return error;
  1926. if (xfs_bmap_adjacent(ap))
  1927. bno_hint = ap->blkno;
  1928. if (xfs_has_rtgroups(ap->ip->i_mount)) {
  1929. error = xfs_rtallocate_rtgs(ap->tp, bno_hint, raminlen, ralen,
  1930. prod, ap->wasdel, initial_user_data,
  1931. &ap->blkno, &ap->length);
  1932. } else {
  1933. error = xfs_rtallocate_rtg(ap->tp, 0, bno_hint, raminlen, ralen,
  1934. prod, ap->wasdel, initial_user_data,
  1935. &rtlocked, &ap->blkno, &ap->length);
  1936. }
  1937. if (error == -ENOSPC) {
  1938. if (!noalign) {
  1939. /*
  1940. * We previously enlarged the request length to try to
  1941. * satisfy an extent size hint. The allocator didn't
  1942. * return anything, so reset the parameters to the
  1943. * original values and try again without alignment
  1944. * criteria.
  1945. */
  1946. ap->offset = orig_offset;
  1947. ap->length = orig_length;
  1948. noalign = true;
  1949. goto retry;
  1950. }
  1951. ap->blkno = NULLFSBLOCK;
  1952. ap->length = 0;
  1953. return 0;
  1954. }
  1955. if (error)
  1956. return error;
  1957. xfs_bmap_alloc_account(ap);
  1958. return 0;
  1959. }