xfs_rtalloc.c 34 KB

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