xfs_rtalloc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.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_bmap.h"
  16. #include "xfs_bmap_btree.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_trans_space.h"
  19. #include "xfs_icache.h"
  20. #include "xfs_rtalloc.h"
  21. #include "xfs_sb.h"
  22. /*
  23. * Read and return the summary information for a given extent size,
  24. * bitmap block combination.
  25. * Keeps track of a current summary block, so we don't keep reading
  26. * it from the buffer cache.
  27. */
  28. static int
  29. xfs_rtget_summary(
  30. xfs_mount_t *mp, /* file system mount structure */
  31. xfs_trans_t *tp, /* transaction pointer */
  32. int log, /* log2 of extent size */
  33. xfs_rtblock_t bbno, /* bitmap block number */
  34. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  35. xfs_fsblock_t *rsb, /* in/out: summary block number */
  36. xfs_suminfo_t *sum) /* out: summary info for this block */
  37. {
  38. return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
  39. }
  40. /*
  41. * Return whether there are any free extents in the size range given
  42. * by low and high, for the bitmap block bbno.
  43. */
  44. STATIC int /* error */
  45. xfs_rtany_summary(
  46. xfs_mount_t *mp, /* file system mount structure */
  47. xfs_trans_t *tp, /* transaction pointer */
  48. int low, /* low log2 extent size */
  49. int high, /* high log2 extent size */
  50. xfs_rtblock_t bbno, /* bitmap block number */
  51. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  52. xfs_fsblock_t *rsb, /* in/out: summary block number */
  53. int *stat) /* out: any good extents here? */
  54. {
  55. int error; /* error value */
  56. int log; /* loop counter, log2 of ext. size */
  57. xfs_suminfo_t sum; /* summary data */
  58. /* There are no extents at levels < m_rsum_cache[bbno]. */
  59. if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
  60. low = mp->m_rsum_cache[bbno];
  61. /*
  62. * Loop over logs of extent sizes.
  63. */
  64. for (log = low; log <= high; log++) {
  65. /*
  66. * Get one summary datum.
  67. */
  68. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  69. if (error) {
  70. return error;
  71. }
  72. /*
  73. * If there are any, return success.
  74. */
  75. if (sum) {
  76. *stat = 1;
  77. goto out;
  78. }
  79. }
  80. /*
  81. * Found nothing, return failure.
  82. */
  83. *stat = 0;
  84. out:
  85. /* There were no extents at levels < log. */
  86. if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
  87. mp->m_rsum_cache[bbno] = log;
  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 /* error */
  95. xfs_rtcopy_summary(
  96. xfs_mount_t *omp, /* old file system mount point */
  97. xfs_mount_t *nmp, /* new file system mount point */
  98. xfs_trans_t *tp) /* transaction pointer */
  99. {
  100. xfs_rtblock_t bbno; /* bitmap block number */
  101. xfs_buf_t *bp; /* summary buffer */
  102. int error; /* error return value */
  103. int log; /* summary level number (log length) */
  104. xfs_suminfo_t sum; /* summary data */
  105. xfs_fsblock_t sumbno; /* summary block number */
  106. bp = NULL;
  107. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  108. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  109. (xfs_srtblock_t)bbno >= 0;
  110. bbno--) {
  111. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  112. &sumbno, &sum);
  113. if (error)
  114. return error;
  115. if (sum == 0)
  116. continue;
  117. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  118. &bp, &sumbno);
  119. if (error)
  120. return error;
  121. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  122. &bp, &sumbno);
  123. if (error)
  124. return error;
  125. ASSERT(sum > 0);
  126. }
  127. }
  128. return 0;
  129. }
  130. /*
  131. * Mark an extent specified by start and len allocated.
  132. * Updates all the summary information as well as the bitmap.
  133. */
  134. STATIC int /* error */
  135. xfs_rtallocate_range(
  136. xfs_mount_t *mp, /* file system mount point */
  137. xfs_trans_t *tp, /* transaction pointer */
  138. xfs_rtblock_t start, /* start block to allocate */
  139. xfs_extlen_t len, /* length to allocate */
  140. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  141. xfs_fsblock_t *rsb) /* in/out: summary block number */
  142. {
  143. xfs_rtblock_t end; /* end of the allocated extent */
  144. int error; /* error value */
  145. xfs_rtblock_t postblock = 0; /* first block allocated > end */
  146. xfs_rtblock_t preblock = 0; /* first block allocated < start */
  147. end = start + len - 1;
  148. /*
  149. * Assume we're allocating out of the middle of a free extent.
  150. * We need to find the beginning and end of the extent so we can
  151. * properly update the summary.
  152. */
  153. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  154. if (error) {
  155. return error;
  156. }
  157. /*
  158. * Find the next allocated block (end of free extent).
  159. */
  160. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  161. &postblock);
  162. if (error) {
  163. return error;
  164. }
  165. /*
  166. * Decrement the summary information corresponding to the entire
  167. * (old) free extent.
  168. */
  169. error = xfs_rtmodify_summary(mp, tp,
  170. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  171. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  172. if (error) {
  173. return error;
  174. }
  175. /*
  176. * If there are blocks not being allocated at the front of the
  177. * old extent, add summary data for them to be free.
  178. */
  179. if (preblock < start) {
  180. error = xfs_rtmodify_summary(mp, tp,
  181. XFS_RTBLOCKLOG(start - preblock),
  182. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  183. if (error) {
  184. return error;
  185. }
  186. }
  187. /*
  188. * If there are blocks not being allocated at the end of the
  189. * old extent, add summary data for them to be free.
  190. */
  191. if (postblock > end) {
  192. error = xfs_rtmodify_summary(mp, tp,
  193. XFS_RTBLOCKLOG(postblock - end),
  194. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  195. if (error) {
  196. return error;
  197. }
  198. }
  199. /*
  200. * Modify the bitmap to mark this extent allocated.
  201. */
  202. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  203. return error;
  204. }
  205. /*
  206. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  207. * bitmap block bbno. If we don't get maxlen then use prod to trim
  208. * the length, if given. Returns error; returns starting block in *rtblock.
  209. * The lengths are all in rtextents.
  210. */
  211. STATIC int /* error */
  212. xfs_rtallocate_extent_block(
  213. xfs_mount_t *mp, /* file system mount point */
  214. xfs_trans_t *tp, /* transaction pointer */
  215. xfs_rtblock_t bbno, /* bitmap block number */
  216. xfs_extlen_t minlen, /* minimum length to allocate */
  217. xfs_extlen_t maxlen, /* maximum length to allocate */
  218. xfs_extlen_t *len, /* out: actual length allocated */
  219. xfs_rtblock_t *nextp, /* out: next block to try */
  220. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  221. xfs_fsblock_t *rsb, /* in/out: summary block number */
  222. xfs_extlen_t prod, /* extent product factor */
  223. xfs_rtblock_t *rtblock) /* out: start block allocated */
  224. {
  225. xfs_rtblock_t besti; /* best rtblock found so far */
  226. xfs_rtblock_t bestlen; /* best length found so far */
  227. xfs_rtblock_t end; /* last rtblock in chunk */
  228. int error; /* error value */
  229. xfs_rtblock_t i; /* current rtblock trying */
  230. xfs_rtblock_t next; /* next rtblock to try */
  231. int stat; /* status from internal calls */
  232. /*
  233. * Loop over all the extents starting in this bitmap block,
  234. * looking for one that's long enough.
  235. */
  236. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  237. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  238. i <= end;
  239. i++) {
  240. /* Make sure we don't scan off the end of the rt volume. */
  241. maxlen = min(mp->m_sb.sb_rextents, i + maxlen) - i;
  242. /*
  243. * See if there's a free extent of maxlen starting at i.
  244. * If it's not so then next will contain the first non-free.
  245. */
  246. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  247. if (error) {
  248. return error;
  249. }
  250. if (stat) {
  251. /*
  252. * i for maxlen is all free, allocate and return that.
  253. */
  254. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  255. rsb);
  256. if (error) {
  257. return error;
  258. }
  259. *len = maxlen;
  260. *rtblock = i;
  261. return 0;
  262. }
  263. /*
  264. * In the case where we have a variable-sized allocation
  265. * request, figure out how big this free piece is,
  266. * and if it's big enough for the minimum, and the best
  267. * so far, remember it.
  268. */
  269. if (minlen < maxlen) {
  270. xfs_rtblock_t thislen; /* this extent size */
  271. thislen = next - i;
  272. if (thislen >= minlen && thislen > bestlen) {
  273. besti = i;
  274. bestlen = thislen;
  275. }
  276. }
  277. /*
  278. * If not done yet, find the start of the next free space.
  279. */
  280. if (next < end) {
  281. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  282. if (error) {
  283. return error;
  284. }
  285. } else
  286. break;
  287. }
  288. /*
  289. * Searched the whole thing & didn't find a maxlen free extent.
  290. */
  291. if (minlen < maxlen && besti != -1) {
  292. xfs_extlen_t p; /* amount to trim length by */
  293. /*
  294. * If size should be a multiple of prod, make that so.
  295. */
  296. if (prod > 1) {
  297. div_u64_rem(bestlen, prod, &p);
  298. if (p)
  299. bestlen -= p;
  300. }
  301. /*
  302. * Allocate besti for bestlen & return that.
  303. */
  304. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  305. if (error) {
  306. return error;
  307. }
  308. *len = bestlen;
  309. *rtblock = besti;
  310. return 0;
  311. }
  312. /*
  313. * Allocation failed. Set *nextp to the next block to try.
  314. */
  315. *nextp = next;
  316. *rtblock = NULLRTBLOCK;
  317. return 0;
  318. }
  319. /*
  320. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  321. * bno. If we don't get maxlen then use prod to trim the length, if given.
  322. * Returns error; returns starting block in *rtblock.
  323. * The lengths are all in rtextents.
  324. */
  325. STATIC int /* error */
  326. xfs_rtallocate_extent_exact(
  327. xfs_mount_t *mp, /* file system mount point */
  328. xfs_trans_t *tp, /* transaction pointer */
  329. xfs_rtblock_t bno, /* starting block number to allocate */
  330. xfs_extlen_t minlen, /* minimum length to allocate */
  331. xfs_extlen_t maxlen, /* maximum length to allocate */
  332. xfs_extlen_t *len, /* out: actual length allocated */
  333. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  334. xfs_fsblock_t *rsb, /* in/out: summary block number */
  335. xfs_extlen_t prod, /* extent product factor */
  336. xfs_rtblock_t *rtblock) /* out: start block allocated */
  337. {
  338. int error; /* error value */
  339. xfs_extlen_t i; /* extent length trimmed due to prod */
  340. int isfree; /* extent is free */
  341. xfs_rtblock_t next; /* next block to try (dummy) */
  342. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  343. /*
  344. * Check if the range in question (for maxlen) is free.
  345. */
  346. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  347. if (error) {
  348. return error;
  349. }
  350. if (isfree) {
  351. /*
  352. * If it is, allocate it and return success.
  353. */
  354. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  355. if (error) {
  356. return error;
  357. }
  358. *len = maxlen;
  359. *rtblock = bno;
  360. return 0;
  361. }
  362. /*
  363. * If not, allocate what there is, if it's at least minlen.
  364. */
  365. maxlen = next - bno;
  366. if (maxlen < minlen) {
  367. /*
  368. * Failed, return failure status.
  369. */
  370. *rtblock = NULLRTBLOCK;
  371. return 0;
  372. }
  373. /*
  374. * Trim off tail of extent, if prod is specified.
  375. */
  376. if (prod > 1 && (i = maxlen % prod)) {
  377. maxlen -= i;
  378. if (maxlen < minlen) {
  379. /*
  380. * Now we can't do it, return failure status.
  381. */
  382. *rtblock = NULLRTBLOCK;
  383. return 0;
  384. }
  385. }
  386. /*
  387. * Allocate what we can and return it.
  388. */
  389. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  390. if (error) {
  391. return error;
  392. }
  393. *len = maxlen;
  394. *rtblock = bno;
  395. return 0;
  396. }
  397. /*
  398. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  399. * to bno as possible. If we don't get maxlen then use prod to trim
  400. * the length, if given. The lengths are all in rtextents.
  401. */
  402. STATIC int /* error */
  403. xfs_rtallocate_extent_near(
  404. xfs_mount_t *mp, /* file system mount point */
  405. xfs_trans_t *tp, /* transaction pointer */
  406. xfs_rtblock_t bno, /* starting block number to allocate */
  407. xfs_extlen_t minlen, /* minimum length to allocate */
  408. xfs_extlen_t maxlen, /* maximum length to allocate */
  409. xfs_extlen_t *len, /* out: actual length allocated */
  410. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  411. xfs_fsblock_t *rsb, /* in/out: summary block number */
  412. xfs_extlen_t prod, /* extent product factor */
  413. xfs_rtblock_t *rtblock) /* out: start block allocated */
  414. {
  415. int any; /* any useful extents from summary */
  416. xfs_rtblock_t bbno; /* bitmap block number */
  417. int error; /* error value */
  418. int i; /* bitmap block offset (loop control) */
  419. int j; /* secondary loop control */
  420. int log2len; /* log2 of minlen */
  421. xfs_rtblock_t n; /* next block to try */
  422. xfs_rtblock_t r; /* result block */
  423. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  424. /*
  425. * If the block number given is off the end, silently set it to
  426. * the last block.
  427. */
  428. if (bno >= mp->m_sb.sb_rextents)
  429. bno = mp->m_sb.sb_rextents - 1;
  430. /* Make sure we don't run off the end of the rt volume. */
  431. maxlen = min(mp->m_sb.sb_rextents, bno + maxlen) - bno;
  432. if (maxlen < minlen) {
  433. *rtblock = NULLRTBLOCK;
  434. return 0;
  435. }
  436. /*
  437. * Try the exact allocation first.
  438. */
  439. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  440. rbpp, rsb, prod, &r);
  441. if (error) {
  442. return error;
  443. }
  444. /*
  445. * If the exact allocation worked, return that.
  446. */
  447. if (r != NULLRTBLOCK) {
  448. *rtblock = r;
  449. return 0;
  450. }
  451. bbno = XFS_BITTOBLOCK(mp, bno);
  452. i = 0;
  453. ASSERT(minlen != 0);
  454. log2len = xfs_highbit32(minlen);
  455. /*
  456. * Loop over all bitmap blocks (bbno + i is current block).
  457. */
  458. for (;;) {
  459. /*
  460. * Get summary information of extents of all useful levels
  461. * starting in this bitmap block.
  462. */
  463. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  464. bbno + i, rbpp, rsb, &any);
  465. if (error) {
  466. return error;
  467. }
  468. /*
  469. * If there are any useful extents starting here, try
  470. * allocating one.
  471. */
  472. if (any) {
  473. /*
  474. * On the positive side of the starting location.
  475. */
  476. if (i >= 0) {
  477. /*
  478. * Try to allocate an extent starting in
  479. * this block.
  480. */
  481. error = xfs_rtallocate_extent_block(mp, tp,
  482. bbno + i, minlen, maxlen, len, &n, rbpp,
  483. rsb, prod, &r);
  484. if (error) {
  485. return error;
  486. }
  487. /*
  488. * If it worked, return it.
  489. */
  490. if (r != NULLRTBLOCK) {
  491. *rtblock = r;
  492. return 0;
  493. }
  494. }
  495. /*
  496. * On the negative side of the starting location.
  497. */
  498. else { /* i < 0 */
  499. /*
  500. * Loop backwards through the bitmap blocks from
  501. * the starting point-1 up to where we are now.
  502. * There should be an extent which ends in this
  503. * bitmap block and is long enough.
  504. */
  505. for (j = -1; j > i; j--) {
  506. /*
  507. * Grab the summary information for
  508. * this bitmap block.
  509. */
  510. error = xfs_rtany_summary(mp, tp,
  511. log2len, mp->m_rsumlevels - 1,
  512. bbno + j, rbpp, rsb, &any);
  513. if (error) {
  514. return error;
  515. }
  516. /*
  517. * If there's no extent given in the
  518. * summary that means the extent we
  519. * found must carry over from an
  520. * earlier block. If there is an
  521. * extent given, we've already tried
  522. * that allocation, don't do it again.
  523. */
  524. if (any)
  525. continue;
  526. error = xfs_rtallocate_extent_block(mp,
  527. tp, bbno + j, minlen, maxlen,
  528. len, &n, rbpp, rsb, prod, &r);
  529. if (error) {
  530. return error;
  531. }
  532. /*
  533. * If it works, return the extent.
  534. */
  535. if (r != NULLRTBLOCK) {
  536. *rtblock = r;
  537. return 0;
  538. }
  539. }
  540. /*
  541. * There weren't intervening bitmap blocks
  542. * with a long enough extent, or the
  543. * allocation didn't work for some reason
  544. * (i.e. it's a little * too short).
  545. * Try to allocate from the summary block
  546. * that we found.
  547. */
  548. error = xfs_rtallocate_extent_block(mp, tp,
  549. bbno + i, minlen, maxlen, len, &n, rbpp,
  550. rsb, prod, &r);
  551. if (error) {
  552. return error;
  553. }
  554. /*
  555. * If it works, return the extent.
  556. */
  557. if (r != NULLRTBLOCK) {
  558. *rtblock = r;
  559. return 0;
  560. }
  561. }
  562. }
  563. /*
  564. * Loop control. If we were on the positive side, and there's
  565. * still more blocks on the negative side, go there.
  566. */
  567. if (i > 0 && (int)bbno - i >= 0)
  568. i = -i;
  569. /*
  570. * If positive, and no more negative, but there are more
  571. * positive, go there.
  572. */
  573. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  574. i++;
  575. /*
  576. * If negative or 0 (just started), and there are positive
  577. * blocks to go, go there. The 0 case moves to block 1.
  578. */
  579. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  580. i = 1 - i;
  581. /*
  582. * If negative or 0 and there are more negative blocks,
  583. * go there.
  584. */
  585. else if (i <= 0 && (int)bbno + i > 0)
  586. i--;
  587. /*
  588. * Must be done. Return failure.
  589. */
  590. else
  591. break;
  592. }
  593. *rtblock = NULLRTBLOCK;
  594. return 0;
  595. }
  596. /*
  597. * Allocate an extent of length minlen<=len<=maxlen, with no position
  598. * specified. If we don't get maxlen then use prod to trim
  599. * the length, if given. The lengths are all in rtextents.
  600. */
  601. STATIC int /* error */
  602. xfs_rtallocate_extent_size(
  603. xfs_mount_t *mp, /* file system mount point */
  604. xfs_trans_t *tp, /* transaction pointer */
  605. xfs_extlen_t minlen, /* minimum length to allocate */
  606. xfs_extlen_t maxlen, /* maximum length to allocate */
  607. xfs_extlen_t *len, /* out: actual length allocated */
  608. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  609. xfs_fsblock_t *rsb, /* in/out: summary block number */
  610. xfs_extlen_t prod, /* extent product factor */
  611. xfs_rtblock_t *rtblock) /* out: start block allocated */
  612. {
  613. int error; /* error value */
  614. int i; /* bitmap block number */
  615. int l; /* level number (loop control) */
  616. xfs_rtblock_t n; /* next block to be tried */
  617. xfs_rtblock_t r; /* result block number */
  618. xfs_suminfo_t sum; /* summary information for extents */
  619. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  620. ASSERT(maxlen != 0);
  621. /*
  622. * Loop over all the levels starting with maxlen.
  623. * At each level, look at all the bitmap blocks, to see if there
  624. * are extents starting there that are long enough (>= maxlen).
  625. * Note, only on the initial level can the allocation fail if
  626. * the summary says there's an extent.
  627. */
  628. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  629. /*
  630. * Loop over all the bitmap blocks.
  631. */
  632. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  633. /*
  634. * Get the summary for this level/block.
  635. */
  636. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  637. &sum);
  638. if (error) {
  639. return error;
  640. }
  641. /*
  642. * Nothing there, on to the next block.
  643. */
  644. if (!sum)
  645. continue;
  646. /*
  647. * Try allocating the extent.
  648. */
  649. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  650. maxlen, len, &n, rbpp, rsb, prod, &r);
  651. if (error) {
  652. return error;
  653. }
  654. /*
  655. * If it worked, return that.
  656. */
  657. if (r != NULLRTBLOCK) {
  658. *rtblock = r;
  659. return 0;
  660. }
  661. /*
  662. * If the "next block to try" returned from the
  663. * allocator is beyond the next bitmap block,
  664. * skip to that bitmap block.
  665. */
  666. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  667. i = XFS_BITTOBLOCK(mp, n) - 1;
  668. }
  669. }
  670. /*
  671. * Didn't find any maxlen blocks. Try smaller ones, unless
  672. * we're asking for a fixed size extent.
  673. */
  674. if (minlen > --maxlen) {
  675. *rtblock = NULLRTBLOCK;
  676. return 0;
  677. }
  678. ASSERT(minlen != 0);
  679. ASSERT(maxlen != 0);
  680. /*
  681. * Loop over sizes, from maxlen down to minlen.
  682. * This time, when we do the allocations, allow smaller ones
  683. * to succeed.
  684. */
  685. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  686. /*
  687. * Loop over all the bitmap blocks, try an allocation
  688. * starting in that block.
  689. */
  690. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  691. /*
  692. * Get the summary information for this level/block.
  693. */
  694. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  695. &sum);
  696. if (error) {
  697. return error;
  698. }
  699. /*
  700. * If nothing there, go on to next.
  701. */
  702. if (!sum)
  703. continue;
  704. /*
  705. * Try the allocation. Make sure the specified
  706. * minlen/maxlen are in the possible range for
  707. * this summary level.
  708. */
  709. error = xfs_rtallocate_extent_block(mp, tp, i,
  710. XFS_RTMAX(minlen, 1 << l),
  711. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  712. len, &n, rbpp, rsb, prod, &r);
  713. if (error) {
  714. return error;
  715. }
  716. /*
  717. * If it worked, return that extent.
  718. */
  719. if (r != NULLRTBLOCK) {
  720. *rtblock = r;
  721. return 0;
  722. }
  723. /*
  724. * If the "next block to try" returned from the
  725. * allocator is beyond the next bitmap block,
  726. * skip to that bitmap block.
  727. */
  728. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  729. i = XFS_BITTOBLOCK(mp, n) - 1;
  730. }
  731. }
  732. /*
  733. * Got nothing, return failure.
  734. */
  735. *rtblock = NULLRTBLOCK;
  736. return 0;
  737. }
  738. /*
  739. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  740. */
  741. STATIC int
  742. xfs_growfs_rt_alloc(
  743. struct xfs_mount *mp, /* file system mount point */
  744. xfs_extlen_t oblocks, /* old count of blocks */
  745. xfs_extlen_t nblocks, /* new count of blocks */
  746. struct xfs_inode *ip) /* inode (bitmap/summary) */
  747. {
  748. xfs_fileoff_t bno; /* block number in file */
  749. struct xfs_buf *bp; /* temporary buffer for zeroing */
  750. xfs_daddr_t d; /* disk block address */
  751. int error; /* error return value */
  752. xfs_fsblock_t fsbno; /* filesystem block for bno */
  753. struct xfs_bmbt_irec map; /* block map output */
  754. int nmap; /* number of block maps */
  755. int resblks; /* space reservation */
  756. enum xfs_blft buf_type;
  757. struct xfs_trans *tp;
  758. if (ip == mp->m_rsumip)
  759. buf_type = XFS_BLFT_RTSUMMARY_BUF;
  760. else
  761. buf_type = XFS_BLFT_RTBITMAP_BUF;
  762. /*
  763. * Allocate space to the file, as necessary.
  764. */
  765. while (oblocks < nblocks) {
  766. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  767. /*
  768. * Reserve space & log for one extent added to the file.
  769. */
  770. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
  771. 0, 0, &tp);
  772. if (error)
  773. return error;
  774. /*
  775. * Lock the inode.
  776. */
  777. xfs_ilock(ip, XFS_ILOCK_EXCL);
  778. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  779. /*
  780. * Allocate blocks to the bitmap file.
  781. */
  782. nmap = 1;
  783. error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
  784. XFS_BMAPI_METADATA, 0, &map, &nmap);
  785. if (!error && nmap < 1)
  786. error = -ENOSPC;
  787. if (error)
  788. goto out_trans_cancel;
  789. /*
  790. * Free any blocks freed up in the transaction, then commit.
  791. */
  792. error = xfs_trans_commit(tp);
  793. if (error)
  794. return error;
  795. /*
  796. * Now we need to clear the allocated blocks.
  797. * Do this one block per transaction, to keep it simple.
  798. */
  799. for (bno = map.br_startoff, fsbno = map.br_startblock;
  800. bno < map.br_startoff + map.br_blockcount;
  801. bno++, fsbno++) {
  802. /*
  803. * Reserve log for one block zeroing.
  804. */
  805. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
  806. 0, 0, 0, &tp);
  807. if (error)
  808. return error;
  809. /*
  810. * Lock the bitmap inode.
  811. */
  812. xfs_ilock(ip, XFS_ILOCK_EXCL);
  813. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  814. /*
  815. * Get a buffer for the block.
  816. */
  817. d = XFS_FSB_TO_DADDR(mp, fsbno);
  818. error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  819. mp->m_bsize, 0, &bp);
  820. if (error)
  821. goto out_trans_cancel;
  822. xfs_trans_buf_set_type(tp, bp, buf_type);
  823. bp->b_ops = &xfs_rtbuf_ops;
  824. memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
  825. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  826. /*
  827. * Commit the transaction.
  828. */
  829. error = xfs_trans_commit(tp);
  830. if (error)
  831. return error;
  832. }
  833. /*
  834. * Go on to the next extent, if any.
  835. */
  836. oblocks = map.br_startoff + map.br_blockcount;
  837. }
  838. return 0;
  839. out_trans_cancel:
  840. xfs_trans_cancel(tp);
  841. return error;
  842. }
  843. static void
  844. xfs_alloc_rsum_cache(
  845. xfs_mount_t *mp, /* file system mount structure */
  846. xfs_extlen_t rbmblocks) /* number of rt bitmap blocks */
  847. {
  848. /*
  849. * The rsum cache is initialized to all zeroes, which is trivially a
  850. * lower bound on the minimum level with any free extents. We can
  851. * continue without the cache if it couldn't be allocated.
  852. */
  853. mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
  854. if (!mp->m_rsum_cache)
  855. xfs_warn(mp, "could not allocate realtime summary cache");
  856. }
  857. /*
  858. * Visible (exported) functions.
  859. */
  860. /*
  861. * Grow the realtime area of the filesystem.
  862. */
  863. int
  864. xfs_growfs_rt(
  865. xfs_mount_t *mp, /* mount point for filesystem */
  866. xfs_growfs_rt_t *in) /* growfs rt input struct */
  867. {
  868. xfs_rtblock_t bmbno; /* bitmap block number */
  869. xfs_buf_t *bp; /* temporary buffer */
  870. int error; /* error return value */
  871. xfs_mount_t *nmp; /* new (fake) mount structure */
  872. xfs_rfsblock_t nrblocks; /* new number of realtime blocks */
  873. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  874. xfs_rtblock_t nrextents; /* new number of realtime extents */
  875. uint8_t nrextslog; /* new log2 of sb_rextents */
  876. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  877. uint nrsumlevels; /* new rt summary levels */
  878. uint nrsumsize; /* new size of rt summary, bytes */
  879. xfs_sb_t *nsbp; /* new superblock */
  880. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  881. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  882. xfs_sb_t *sbp; /* old superblock */
  883. xfs_fsblock_t sumbno; /* summary block number */
  884. uint8_t *rsum_cache; /* old summary cache */
  885. sbp = &mp->m_sb;
  886. /*
  887. * Initial error checking.
  888. */
  889. if (!capable(CAP_SYS_ADMIN))
  890. return -EPERM;
  891. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  892. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  893. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  894. return -EINVAL;
  895. if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
  896. return error;
  897. /*
  898. * Read in the last block of the device, make sure it exists.
  899. */
  900. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  901. XFS_FSB_TO_BB(mp, nrblocks - 1),
  902. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  903. if (error)
  904. return error;
  905. xfs_buf_relse(bp);
  906. /*
  907. * Calculate new parameters. These are the final values to be reached.
  908. */
  909. nrextents = nrblocks;
  910. do_div(nrextents, in->extsize);
  911. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  912. nrextslog = xfs_highbit32(nrextents);
  913. nrsumlevels = nrextslog + 1;
  914. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  915. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  916. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  917. /*
  918. * New summary size can't be more than half the size of
  919. * the log. This prevents us from getting a log overflow,
  920. * since we'll log basically the whole summary file at once.
  921. */
  922. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  923. return -EINVAL;
  924. /*
  925. * Get the old block counts for bitmap and summary inodes.
  926. * These can't change since other growfs callers are locked out.
  927. */
  928. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  929. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  930. /*
  931. * Allocate space to the bitmap and summary files, as necessary.
  932. */
  933. error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
  934. if (error)
  935. return error;
  936. error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
  937. if (error)
  938. return error;
  939. rsum_cache = mp->m_rsum_cache;
  940. if (nrbmblocks != sbp->sb_rbmblocks)
  941. xfs_alloc_rsum_cache(mp, nrbmblocks);
  942. /*
  943. * Allocate a new (fake) mount/sb.
  944. */
  945. nmp = kmem_alloc(sizeof(*nmp), 0);
  946. /*
  947. * Loop over the bitmap blocks.
  948. * We will do everything one bitmap block at a time.
  949. * Skip the current block if it is exactly full.
  950. * This also deals with the case where there were no rtextents before.
  951. */
  952. for (bmbno = sbp->sb_rbmblocks -
  953. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  954. bmbno < nrbmblocks;
  955. bmbno++) {
  956. xfs_trans_t *tp;
  957. *nmp = *mp;
  958. nsbp = &nmp->m_sb;
  959. /*
  960. * Calculate new sb and mount fields for this round.
  961. */
  962. nsbp->sb_rextsize = in->extsize;
  963. nsbp->sb_rbmblocks = bmbno + 1;
  964. nsbp->sb_rblocks =
  965. XFS_RTMIN(nrblocks,
  966. nsbp->sb_rbmblocks * NBBY *
  967. nsbp->sb_blocksize * nsbp->sb_rextsize);
  968. nsbp->sb_rextents = nsbp->sb_rblocks;
  969. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  970. ASSERT(nsbp->sb_rextents != 0);
  971. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  972. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  973. nrsumsize =
  974. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  975. nsbp->sb_rbmblocks;
  976. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  977. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  978. /*
  979. * Start a transaction, get the log reservation.
  980. */
  981. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
  982. &tp);
  983. if (error)
  984. break;
  985. /*
  986. * Lock out other callers by grabbing the bitmap inode lock.
  987. */
  988. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
  989. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  990. /*
  991. * Update the bitmap inode's size ondisk and incore. We need
  992. * to update the incore size so that inode inactivation won't
  993. * punch what it thinks are "posteof" blocks.
  994. */
  995. mp->m_rbmip->i_d.di_size =
  996. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  997. i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_d.di_size);
  998. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  999. /*
  1000. * Get the summary inode into the transaction.
  1001. */
  1002. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
  1003. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  1004. /*
  1005. * Update the summary inode's size. We need to update the
  1006. * incore size so that inode inactivation won't punch what it
  1007. * thinks are "posteof" blocks.
  1008. */
  1009. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  1010. i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_d.di_size);
  1011. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  1012. /*
  1013. * Copy summary data from old to new sizes.
  1014. * Do this when the real size (not block-aligned) changes.
  1015. */
  1016. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1017. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1018. error = xfs_rtcopy_summary(mp, nmp, tp);
  1019. if (error)
  1020. goto error_cancel;
  1021. }
  1022. /*
  1023. * Update superblock fields.
  1024. */
  1025. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1026. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1027. nsbp->sb_rextsize - sbp->sb_rextsize);
  1028. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1029. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1030. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1031. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1032. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1033. nsbp->sb_rblocks - sbp->sb_rblocks);
  1034. if (nsbp->sb_rextents != sbp->sb_rextents)
  1035. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1036. nsbp->sb_rextents - sbp->sb_rextents);
  1037. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1038. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1039. nsbp->sb_rextslog - sbp->sb_rextslog);
  1040. /*
  1041. * Free new extent.
  1042. */
  1043. bp = NULL;
  1044. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1045. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1046. if (error) {
  1047. error_cancel:
  1048. xfs_trans_cancel(tp);
  1049. break;
  1050. }
  1051. /*
  1052. * Mark more blocks free in the superblock.
  1053. */
  1054. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1055. nsbp->sb_rextents - sbp->sb_rextents);
  1056. /*
  1057. * Update mp values into the real mp structure.
  1058. */
  1059. mp->m_rsumlevels = nrsumlevels;
  1060. mp->m_rsumsize = nrsumsize;
  1061. error = xfs_trans_commit(tp);
  1062. if (error)
  1063. break;
  1064. }
  1065. if (error)
  1066. goto out_free;
  1067. /* Update secondary superblocks now the physical grow has completed */
  1068. error = xfs_update_secondary_sbs(mp);
  1069. out_free:
  1070. /*
  1071. * Free the fake mp structure.
  1072. */
  1073. kmem_free(nmp);
  1074. /*
  1075. * If we had to allocate a new rsum_cache, we either need to free the
  1076. * old one (if we succeeded) or free the new one and restore the old one
  1077. * (if there was an error).
  1078. */
  1079. if (rsum_cache != mp->m_rsum_cache) {
  1080. if (error) {
  1081. kmem_free(mp->m_rsum_cache);
  1082. mp->m_rsum_cache = rsum_cache;
  1083. } else {
  1084. kmem_free(rsum_cache);
  1085. }
  1086. }
  1087. return error;
  1088. }
  1089. /*
  1090. * Allocate an extent in the realtime subvolume, with the usual allocation
  1091. * parameters. The length units are all in realtime extents, as is the
  1092. * result block number.
  1093. */
  1094. int /* error */
  1095. xfs_rtallocate_extent(
  1096. xfs_trans_t *tp, /* transaction pointer */
  1097. xfs_rtblock_t bno, /* starting block number to allocate */
  1098. xfs_extlen_t minlen, /* minimum length to allocate */
  1099. xfs_extlen_t maxlen, /* maximum length to allocate */
  1100. xfs_extlen_t *len, /* out: actual length allocated */
  1101. int wasdel, /* was a delayed allocation extent */
  1102. xfs_extlen_t prod, /* extent product factor */
  1103. xfs_rtblock_t *rtblock) /* out: start block allocated */
  1104. {
  1105. xfs_mount_t *mp = tp->t_mountp;
  1106. int error; /* error value */
  1107. xfs_rtblock_t r; /* result allocated block */
  1108. xfs_fsblock_t sb; /* summary file block number */
  1109. xfs_buf_t *sumbp; /* summary file block buffer */
  1110. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1111. ASSERT(minlen > 0 && minlen <= maxlen);
  1112. /*
  1113. * If prod is set then figure out what to do to minlen and maxlen.
  1114. */
  1115. if (prod > 1) {
  1116. xfs_extlen_t i;
  1117. if ((i = maxlen % prod))
  1118. maxlen -= i;
  1119. if ((i = minlen % prod))
  1120. minlen += prod - i;
  1121. if (maxlen < minlen) {
  1122. *rtblock = NULLRTBLOCK;
  1123. return 0;
  1124. }
  1125. }
  1126. retry:
  1127. sumbp = NULL;
  1128. if (bno == 0) {
  1129. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  1130. &sumbp, &sb, prod, &r);
  1131. } else {
  1132. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  1133. len, &sumbp, &sb, prod, &r);
  1134. }
  1135. if (error)
  1136. return error;
  1137. /*
  1138. * If it worked, update the superblock.
  1139. */
  1140. if (r != NULLRTBLOCK) {
  1141. long slen = (long)*len;
  1142. ASSERT(*len >= minlen && *len <= maxlen);
  1143. if (wasdel)
  1144. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  1145. else
  1146. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  1147. } else if (prod > 1) {
  1148. prod = 1;
  1149. goto retry;
  1150. }
  1151. *rtblock = r;
  1152. return 0;
  1153. }
  1154. /*
  1155. * Initialize realtime fields in the mount structure.
  1156. */
  1157. int /* error */
  1158. xfs_rtmount_init(
  1159. struct xfs_mount *mp) /* file system mount structure */
  1160. {
  1161. struct xfs_buf *bp; /* buffer for last block of subvolume */
  1162. struct xfs_sb *sbp; /* filesystem superblock copy in mount */
  1163. xfs_daddr_t d; /* address of last block of subvolume */
  1164. int error;
  1165. sbp = &mp->m_sb;
  1166. if (sbp->sb_rblocks == 0)
  1167. return 0;
  1168. if (mp->m_rtdev_targp == NULL) {
  1169. xfs_warn(mp,
  1170. "Filesystem has a realtime volume, use rtdev=device option");
  1171. return -ENODEV;
  1172. }
  1173. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  1174. mp->m_rsumsize =
  1175. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  1176. sbp->sb_rbmblocks;
  1177. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  1178. mp->m_rbmip = mp->m_rsumip = NULL;
  1179. /*
  1180. * Check that the realtime section is an ok size.
  1181. */
  1182. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  1183. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  1184. xfs_warn(mp, "realtime mount -- %llu != %llu",
  1185. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  1186. (unsigned long long) mp->m_sb.sb_rblocks);
  1187. return -EFBIG;
  1188. }
  1189. error = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1190. d - XFS_FSB_TO_BB(mp, 1),
  1191. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  1192. if (error) {
  1193. xfs_warn(mp, "realtime device size check failed");
  1194. return error;
  1195. }
  1196. xfs_buf_relse(bp);
  1197. return 0;
  1198. }
  1199. /*
  1200. * Get the bitmap and summary inodes and the summary cache into the mount
  1201. * structure at mount time.
  1202. */
  1203. int /* error */
  1204. xfs_rtmount_inodes(
  1205. xfs_mount_t *mp) /* file system mount structure */
  1206. {
  1207. int error; /* error return value */
  1208. xfs_sb_t *sbp;
  1209. sbp = &mp->m_sb;
  1210. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  1211. if (error)
  1212. return error;
  1213. ASSERT(mp->m_rbmip != NULL);
  1214. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  1215. if (error) {
  1216. xfs_irele(mp->m_rbmip);
  1217. return error;
  1218. }
  1219. ASSERT(mp->m_rsumip != NULL);
  1220. xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
  1221. return 0;
  1222. }
  1223. void
  1224. xfs_rtunmount_inodes(
  1225. struct xfs_mount *mp)
  1226. {
  1227. kmem_free(mp->m_rsum_cache);
  1228. if (mp->m_rbmip)
  1229. xfs_irele(mp->m_rbmip);
  1230. if (mp->m_rsumip)
  1231. xfs_irele(mp->m_rsumip);
  1232. }
  1233. /*
  1234. * Pick an extent for allocation at the start of a new realtime file.
  1235. * Use the sequence number stored in the atime field of the bitmap inode.
  1236. * Translate this to a fraction of the rtextents, and return the product
  1237. * of rtextents and the fraction.
  1238. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  1239. */
  1240. int /* error */
  1241. xfs_rtpick_extent(
  1242. xfs_mount_t *mp, /* file system mount point */
  1243. xfs_trans_t *tp, /* transaction pointer */
  1244. xfs_extlen_t len, /* allocation length (rtextents) */
  1245. xfs_rtblock_t *pick) /* result rt extent */
  1246. {
  1247. xfs_rtblock_t b; /* result block */
  1248. int log2; /* log of sequence number */
  1249. uint64_t resid; /* residual after log removed */
  1250. uint64_t seq; /* sequence number of file creation */
  1251. uint64_t *seqp; /* pointer to seqno in inode */
  1252. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  1253. seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
  1254. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  1255. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  1256. *seqp = 0;
  1257. }
  1258. seq = *seqp;
  1259. if ((log2 = xfs_highbit64(seq)) == -1)
  1260. b = 0;
  1261. else {
  1262. resid = seq - (1ULL << log2);
  1263. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  1264. (log2 + 1);
  1265. if (b >= mp->m_sb.sb_rextents)
  1266. div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
  1267. if (b + len > mp->m_sb.sb_rextents)
  1268. b = mp->m_sb.sb_rextents - len;
  1269. }
  1270. *seqp = seq + 1;
  1271. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1272. *pick = b;
  1273. return 0;
  1274. }