PageRenderTime 72ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/linux-2.6.21.x/fs/xfs/xfs_rtalloc.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 2333 lines | 1400 code | 61 blank | 872 comment | 287 complexity | 7b9a9073d812fe56530bee49e9893a77 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_bmap.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_fsops.h"
  43. #include "xfs_error.h"
  44. #include "xfs_rw.h"
  45. #include "xfs_inode_item.h"
  46. #include "xfs_trans_space.h"
  47. /*
  48. * Prototypes for internal functions.
  49. */
  50. STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  51. xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
  52. STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
  53. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
  54. STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  55. xfs_extlen_t, int, xfs_rtblock_t *, int *);
  56. STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  57. xfs_rtblock_t, xfs_rtblock_t *);
  58. STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  59. xfs_rtblock_t, xfs_rtblock_t *);
  60. STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
  61. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
  62. STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  63. xfs_extlen_t, int);
  64. STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  65. xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
  66. /*
  67. * Internal functions.
  68. */
  69. /*
  70. * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
  71. */
  72. STATIC int
  73. xfs_lowbit32(
  74. __uint32_t v)
  75. {
  76. if (v)
  77. return ffs(v) - 1;
  78. return -1;
  79. }
  80. /*
  81. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  82. */
  83. STATIC int /* error */
  84. xfs_growfs_rt_alloc(
  85. xfs_mount_t *mp, /* file system mount point */
  86. xfs_extlen_t oblocks, /* old count of blocks */
  87. xfs_extlen_t nblocks, /* new count of blocks */
  88. xfs_ino_t ino) /* inode number (bitmap/summary) */
  89. {
  90. xfs_fileoff_t bno; /* block number in file */
  91. xfs_buf_t *bp; /* temporary buffer for zeroing */
  92. int cancelflags; /* flags for xfs_trans_cancel */
  93. int committed; /* transaction committed flag */
  94. xfs_daddr_t d; /* disk block address */
  95. int error; /* error return value */
  96. xfs_fsblock_t firstblock; /* first block allocated in xaction */
  97. xfs_bmap_free_t flist; /* list of freed blocks */
  98. xfs_fsblock_t fsbno; /* filesystem block for bno */
  99. xfs_inode_t *ip; /* pointer to incore inode */
  100. xfs_bmbt_irec_t map; /* block map output */
  101. int nmap; /* number of block maps */
  102. int resblks; /* space reservation */
  103. xfs_trans_t *tp; /* transaction pointer */
  104. /*
  105. * Allocate space to the file, as necessary.
  106. */
  107. while (oblocks < nblocks) {
  108. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  109. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  110. cancelflags = 0;
  111. /*
  112. * Reserve space & log for one extent added to the file.
  113. */
  114. if ((error = xfs_trans_reserve(tp, resblks,
  115. XFS_GROWRTALLOC_LOG_RES(mp), 0,
  116. XFS_TRANS_PERM_LOG_RES,
  117. XFS_DEFAULT_PERM_LOG_COUNT)))
  118. goto error_exit;
  119. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  120. /*
  121. * Lock the inode.
  122. */
  123. if ((error = xfs_trans_iget(mp, tp, ino, 0,
  124. XFS_ILOCK_EXCL, &ip)))
  125. goto error_exit;
  126. XFS_BMAP_INIT(&flist, &firstblock);
  127. /*
  128. * Allocate blocks to the bitmap file.
  129. */
  130. nmap = 1;
  131. cancelflags |= XFS_TRANS_ABORT;
  132. error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
  133. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
  134. resblks, &map, &nmap, &flist, NULL);
  135. if (!error && nmap < 1)
  136. error = XFS_ERROR(ENOSPC);
  137. if (error)
  138. goto error_exit;
  139. /*
  140. * Free any blocks freed up in the transaction, then commit.
  141. */
  142. error = xfs_bmap_finish(&tp, &flist, &committed);
  143. if (error)
  144. goto error_exit;
  145. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  146. /*
  147. * Now we need to clear the allocated blocks.
  148. * Do this one block per transaction, to keep it simple.
  149. */
  150. cancelflags = 0;
  151. for (bno = map.br_startoff, fsbno = map.br_startblock;
  152. bno < map.br_startoff + map.br_blockcount;
  153. bno++, fsbno++) {
  154. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  155. /*
  156. * Reserve log for one block zeroing.
  157. */
  158. if ((error = xfs_trans_reserve(tp, 0,
  159. XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
  160. goto error_exit;
  161. /*
  162. * Lock the bitmap inode.
  163. */
  164. if ((error = xfs_trans_iget(mp, tp, ino, 0,
  165. XFS_ILOCK_EXCL, &ip)))
  166. goto error_exit;
  167. /*
  168. * Get a buffer for the block.
  169. */
  170. d = XFS_FSB_TO_DADDR(mp, fsbno);
  171. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  172. mp->m_bsize, 0);
  173. if (bp == NULL) {
  174. error = XFS_ERROR(EIO);
  175. goto error_exit;
  176. }
  177. memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
  178. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  179. /*
  180. * Commit the transaction.
  181. */
  182. xfs_trans_commit(tp, 0, NULL);
  183. }
  184. /*
  185. * Go on to the next extent, if any.
  186. */
  187. oblocks = map.br_startoff + map.br_blockcount;
  188. }
  189. return 0;
  190. error_exit:
  191. xfs_trans_cancel(tp, cancelflags);
  192. return error;
  193. }
  194. /*
  195. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  196. * bitmap block bbno. If we don't get maxlen then use prod to trim
  197. * the length, if given. Returns error; returns starting block in *rtblock.
  198. * The lengths are all in rtextents.
  199. */
  200. STATIC int /* error */
  201. xfs_rtallocate_extent_block(
  202. xfs_mount_t *mp, /* file system mount point */
  203. xfs_trans_t *tp, /* transaction pointer */
  204. xfs_rtblock_t bbno, /* bitmap block number */
  205. xfs_extlen_t minlen, /* minimum length to allocate */
  206. xfs_extlen_t maxlen, /* maximum length to allocate */
  207. xfs_extlen_t *len, /* out: actual length allocated */
  208. xfs_rtblock_t *nextp, /* out: next block to try */
  209. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  210. xfs_fsblock_t *rsb, /* in/out: summary block number */
  211. xfs_extlen_t prod, /* extent product factor */
  212. xfs_rtblock_t *rtblock) /* out: start block allocated */
  213. {
  214. xfs_rtblock_t besti; /* best rtblock found so far */
  215. xfs_rtblock_t bestlen; /* best length found so far */
  216. xfs_rtblock_t end; /* last rtblock in chunk */
  217. int error; /* error value */
  218. xfs_rtblock_t i; /* current rtblock trying */
  219. xfs_rtblock_t next; /* next rtblock to try */
  220. int stat; /* status from internal calls */
  221. /*
  222. * Loop over all the extents starting in this bitmap block,
  223. * looking for one that's long enough.
  224. */
  225. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  226. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  227. i <= end;
  228. i++) {
  229. /*
  230. * See if there's a free extent of maxlen starting at i.
  231. * If it's not so then next will contain the first non-free.
  232. */
  233. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  234. if (error) {
  235. return error;
  236. }
  237. if (stat) {
  238. /*
  239. * i for maxlen is all free, allocate and return that.
  240. */
  241. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  242. rsb);
  243. if (error) {
  244. return error;
  245. }
  246. *len = maxlen;
  247. *rtblock = i;
  248. return 0;
  249. }
  250. /*
  251. * In the case where we have a variable-sized allocation
  252. * request, figure out how big this free piece is,
  253. * and if it's big enough for the minimum, and the best
  254. * so far, remember it.
  255. */
  256. if (minlen < maxlen) {
  257. xfs_rtblock_t thislen; /* this extent size */
  258. thislen = next - i;
  259. if (thislen >= minlen && thislen > bestlen) {
  260. besti = i;
  261. bestlen = thislen;
  262. }
  263. }
  264. /*
  265. * If not done yet, find the start of the next free space.
  266. */
  267. if (next < end) {
  268. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  269. if (error) {
  270. return error;
  271. }
  272. } else
  273. break;
  274. }
  275. /*
  276. * Searched the whole thing & didn't find a maxlen free extent.
  277. */
  278. if (minlen < maxlen && besti != -1) {
  279. xfs_extlen_t p; /* amount to trim length by */
  280. /*
  281. * If size should be a multiple of prod, make that so.
  282. */
  283. if (prod > 1 && (p = do_mod(bestlen, prod)))
  284. bestlen -= p;
  285. /*
  286. * Allocate besti for bestlen & return that.
  287. */
  288. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  289. if (error) {
  290. return error;
  291. }
  292. *len = bestlen;
  293. *rtblock = besti;
  294. return 0;
  295. }
  296. /*
  297. * Allocation failed. Set *nextp to the next block to try.
  298. */
  299. *nextp = next;
  300. *rtblock = NULLRTBLOCK;
  301. return 0;
  302. }
  303. /*
  304. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  305. * bno. If we don't get maxlen then use prod to trim the length, if given.
  306. * Returns error; returns starting block in *rtblock.
  307. * The lengths are all in rtextents.
  308. */
  309. STATIC int /* error */
  310. xfs_rtallocate_extent_exact(
  311. xfs_mount_t *mp, /* file system mount point */
  312. xfs_trans_t *tp, /* transaction pointer */
  313. xfs_rtblock_t bno, /* starting block number to allocate */
  314. xfs_extlen_t minlen, /* minimum length to allocate */
  315. xfs_extlen_t maxlen, /* maximum length to allocate */
  316. xfs_extlen_t *len, /* out: actual length allocated */
  317. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  318. xfs_fsblock_t *rsb, /* in/out: summary block number */
  319. xfs_extlen_t prod, /* extent product factor */
  320. xfs_rtblock_t *rtblock) /* out: start block allocated */
  321. {
  322. int error; /* error value */
  323. xfs_extlen_t i; /* extent length trimmed due to prod */
  324. int isfree; /* extent is free */
  325. xfs_rtblock_t next; /* next block to try (dummy) */
  326. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  327. /*
  328. * Check if the range in question (for maxlen) is free.
  329. */
  330. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  331. if (error) {
  332. return error;
  333. }
  334. if (isfree) {
  335. /*
  336. * If it is, allocate it and return success.
  337. */
  338. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  339. if (error) {
  340. return error;
  341. }
  342. *len = maxlen;
  343. *rtblock = bno;
  344. return 0;
  345. }
  346. /*
  347. * If not, allocate what there is, if it's at least minlen.
  348. */
  349. maxlen = next - bno;
  350. if (maxlen < minlen) {
  351. /*
  352. * Failed, return failure status.
  353. */
  354. *rtblock = NULLRTBLOCK;
  355. return 0;
  356. }
  357. /*
  358. * Trim off tail of extent, if prod is specified.
  359. */
  360. if (prod > 1 && (i = maxlen % prod)) {
  361. maxlen -= i;
  362. if (maxlen < minlen) {
  363. /*
  364. * Now we can't do it, return failure status.
  365. */
  366. *rtblock = NULLRTBLOCK;
  367. return 0;
  368. }
  369. }
  370. /*
  371. * Allocate what we can and return it.
  372. */
  373. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  374. if (error) {
  375. return error;
  376. }
  377. *len = maxlen;
  378. *rtblock = bno;
  379. return 0;
  380. }
  381. /*
  382. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  383. * to bno as possible. If we don't get maxlen then use prod to trim
  384. * the length, if given. The lengths are all in rtextents.
  385. */
  386. STATIC int /* error */
  387. xfs_rtallocate_extent_near(
  388. xfs_mount_t *mp, /* file system mount point */
  389. xfs_trans_t *tp, /* transaction pointer */
  390. xfs_rtblock_t bno, /* starting block number to allocate */
  391. xfs_extlen_t minlen, /* minimum length to allocate */
  392. xfs_extlen_t maxlen, /* maximum length to allocate */
  393. xfs_extlen_t *len, /* out: actual length allocated */
  394. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  395. xfs_fsblock_t *rsb, /* in/out: summary block number */
  396. xfs_extlen_t prod, /* extent product factor */
  397. xfs_rtblock_t *rtblock) /* out: start block allocated */
  398. {
  399. int any; /* any useful extents from summary */
  400. xfs_rtblock_t bbno; /* bitmap block number */
  401. int error; /* error value */
  402. int i; /* bitmap block offset (loop control) */
  403. int j; /* secondary loop control */
  404. int log2len; /* log2 of minlen */
  405. xfs_rtblock_t n; /* next block to try */
  406. xfs_rtblock_t r; /* result block */
  407. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  408. /*
  409. * If the block number given is off the end, silently set it to
  410. * the last block.
  411. */
  412. if (bno >= mp->m_sb.sb_rextents)
  413. bno = mp->m_sb.sb_rextents - 1;
  414. /*
  415. * Try the exact allocation first.
  416. */
  417. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  418. rbpp, rsb, prod, &r);
  419. if (error) {
  420. return error;
  421. }
  422. /*
  423. * If the exact allocation worked, return that.
  424. */
  425. if (r != NULLRTBLOCK) {
  426. *rtblock = r;
  427. return 0;
  428. }
  429. bbno = XFS_BITTOBLOCK(mp, bno);
  430. i = 0;
  431. log2len = xfs_highbit32(minlen);
  432. /*
  433. * Loop over all bitmap blocks (bbno + i is current block).
  434. */
  435. for (;;) {
  436. /*
  437. * Get summary information of extents of all useful levels
  438. * starting in this bitmap block.
  439. */
  440. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  441. bbno + i, rbpp, rsb, &any);
  442. if (error) {
  443. return error;
  444. }
  445. /*
  446. * If there are any useful extents starting here, try
  447. * allocating one.
  448. */
  449. if (any) {
  450. /*
  451. * On the positive side of the starting location.
  452. */
  453. if (i >= 0) {
  454. /*
  455. * Try to allocate an extent starting in
  456. * this block.
  457. */
  458. error = xfs_rtallocate_extent_block(mp, tp,
  459. bbno + i, minlen, maxlen, len, &n, rbpp,
  460. rsb, prod, &r);
  461. if (error) {
  462. return error;
  463. }
  464. /*
  465. * If it worked, return it.
  466. */
  467. if (r != NULLRTBLOCK) {
  468. *rtblock = r;
  469. return 0;
  470. }
  471. }
  472. /*
  473. * On the negative side of the starting location.
  474. */
  475. else { /* i < 0 */
  476. /*
  477. * Loop backwards through the bitmap blocks from
  478. * the starting point-1 up to where we are now.
  479. * There should be an extent which ends in this
  480. * bitmap block and is long enough.
  481. */
  482. for (j = -1; j > i; j--) {
  483. /*
  484. * Grab the summary information for
  485. * this bitmap block.
  486. */
  487. error = xfs_rtany_summary(mp, tp,
  488. log2len, mp->m_rsumlevels - 1,
  489. bbno + j, rbpp, rsb, &any);
  490. if (error) {
  491. return error;
  492. }
  493. /*
  494. * If there's no extent given in the
  495. * summary that means the extent we
  496. * found must carry over from an
  497. * earlier block. If there is an
  498. * extent given, we've already tried
  499. * that allocation, don't do it again.
  500. */
  501. if (any)
  502. continue;
  503. error = xfs_rtallocate_extent_block(mp,
  504. tp, bbno + j, minlen, maxlen,
  505. len, &n, rbpp, rsb, prod, &r);
  506. if (error) {
  507. return error;
  508. }
  509. /*
  510. * If it works, return the extent.
  511. */
  512. if (r != NULLRTBLOCK) {
  513. *rtblock = r;
  514. return 0;
  515. }
  516. }
  517. /*
  518. * There weren't intervening bitmap blocks
  519. * with a long enough extent, or the
  520. * allocation didn't work for some reason
  521. * (i.e. it's a little * too short).
  522. * Try to allocate from the summary block
  523. * that we found.
  524. */
  525. error = xfs_rtallocate_extent_block(mp, tp,
  526. bbno + i, minlen, maxlen, len, &n, rbpp,
  527. rsb, prod, &r);
  528. if (error) {
  529. return error;
  530. }
  531. /*
  532. * If it works, return the extent.
  533. */
  534. if (r != NULLRTBLOCK) {
  535. *rtblock = r;
  536. return 0;
  537. }
  538. }
  539. }
  540. /*
  541. * Loop control. If we were on the positive side, and there's
  542. * still more blocks on the negative side, go there.
  543. */
  544. if (i > 0 && (int)bbno - i >= 0)
  545. i = -i;
  546. /*
  547. * If positive, and no more negative, but there are more
  548. * positive, go there.
  549. */
  550. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  551. i++;
  552. /*
  553. * If negative or 0 (just started), and there are positive
  554. * blocks to go, go there. The 0 case moves to block 1.
  555. */
  556. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  557. i = 1 - i;
  558. /*
  559. * If negative or 0 and there are more negative blocks,
  560. * go there.
  561. */
  562. else if (i <= 0 && (int)bbno + i > 0)
  563. i--;
  564. /*
  565. * Must be done. Return failure.
  566. */
  567. else
  568. break;
  569. }
  570. *rtblock = NULLRTBLOCK;
  571. return 0;
  572. }
  573. /*
  574. * Allocate an extent of length minlen<=len<=maxlen, with no position
  575. * specified. If we don't get maxlen then use prod to trim
  576. * the length, if given. The lengths are all in rtextents.
  577. */
  578. STATIC int /* error */
  579. xfs_rtallocate_extent_size(
  580. xfs_mount_t *mp, /* file system mount point */
  581. xfs_trans_t *tp, /* transaction pointer */
  582. xfs_extlen_t minlen, /* minimum length to allocate */
  583. xfs_extlen_t maxlen, /* maximum length to allocate */
  584. xfs_extlen_t *len, /* out: actual length allocated */
  585. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  586. xfs_fsblock_t *rsb, /* in/out: summary block number */
  587. xfs_extlen_t prod, /* extent product factor */
  588. xfs_rtblock_t *rtblock) /* out: start block allocated */
  589. {
  590. int error; /* error value */
  591. int i; /* bitmap block number */
  592. int l; /* level number (loop control) */
  593. xfs_rtblock_t n; /* next block to be tried */
  594. xfs_rtblock_t r; /* result block number */
  595. xfs_suminfo_t sum; /* summary information for extents */
  596. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  597. /*
  598. * Loop over all the levels starting with maxlen.
  599. * At each level, look at all the bitmap blocks, to see if there
  600. * are extents starting there that are long enough (>= maxlen).
  601. * Note, only on the initial level can the allocation fail if
  602. * the summary says there's an extent.
  603. */
  604. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  605. /*
  606. * Loop over all the bitmap blocks.
  607. */
  608. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  609. /*
  610. * Get the summary for this level/block.
  611. */
  612. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  613. &sum);
  614. if (error) {
  615. return error;
  616. }
  617. /*
  618. * Nothing there, on to the next block.
  619. */
  620. if (!sum)
  621. continue;
  622. /*
  623. * Try allocating the extent.
  624. */
  625. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  626. maxlen, len, &n, rbpp, rsb, prod, &r);
  627. if (error) {
  628. return error;
  629. }
  630. /*
  631. * If it worked, return that.
  632. */
  633. if (r != NULLRTBLOCK) {
  634. *rtblock = r;
  635. return 0;
  636. }
  637. /*
  638. * If the "next block to try" returned from the
  639. * allocator is beyond the next bitmap block,
  640. * skip to that bitmap block.
  641. */
  642. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  643. i = XFS_BITTOBLOCK(mp, n) - 1;
  644. }
  645. }
  646. /*
  647. * Didn't find any maxlen blocks. Try smaller ones, unless
  648. * we're asking for a fixed size extent.
  649. */
  650. if (minlen > --maxlen) {
  651. *rtblock = NULLRTBLOCK;
  652. return 0;
  653. }
  654. /*
  655. * Loop over sizes, from maxlen down to minlen.
  656. * This time, when we do the allocations, allow smaller ones
  657. * to succeed.
  658. */
  659. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  660. /*
  661. * Loop over all the bitmap blocks, try an allocation
  662. * starting in that block.
  663. */
  664. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  665. /*
  666. * Get the summary information for this level/block.
  667. */
  668. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  669. &sum);
  670. if (error) {
  671. return error;
  672. }
  673. /*
  674. * If nothing there, go on to next.
  675. */
  676. if (!sum)
  677. continue;
  678. /*
  679. * Try the allocation. Make sure the specified
  680. * minlen/maxlen are in the possible range for
  681. * this summary level.
  682. */
  683. error = xfs_rtallocate_extent_block(mp, tp, i,
  684. XFS_RTMAX(minlen, 1 << l),
  685. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  686. len, &n, rbpp, rsb, prod, &r);
  687. if (error) {
  688. return error;
  689. }
  690. /*
  691. * If it worked, return that extent.
  692. */
  693. if (r != NULLRTBLOCK) {
  694. *rtblock = r;
  695. return 0;
  696. }
  697. /*
  698. * If the "next block to try" returned from the
  699. * allocator is beyond the next bitmap block,
  700. * skip to that bitmap block.
  701. */
  702. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  703. i = XFS_BITTOBLOCK(mp, n) - 1;
  704. }
  705. }
  706. /*
  707. * Got nothing, return failure.
  708. */
  709. *rtblock = NULLRTBLOCK;
  710. return 0;
  711. }
  712. /*
  713. * Mark an extent specified by start and len allocated.
  714. * Updates all the summary information as well as the bitmap.
  715. */
  716. STATIC int /* error */
  717. xfs_rtallocate_range(
  718. xfs_mount_t *mp, /* file system mount point */
  719. xfs_trans_t *tp, /* transaction pointer */
  720. xfs_rtblock_t start, /* start block to allocate */
  721. xfs_extlen_t len, /* length to allocate */
  722. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  723. xfs_fsblock_t *rsb) /* in/out: summary block number */
  724. {
  725. xfs_rtblock_t end; /* end of the allocated extent */
  726. int error; /* error value */
  727. xfs_rtblock_t postblock; /* first block allocated > end */
  728. xfs_rtblock_t preblock; /* first block allocated < start */
  729. end = start + len - 1;
  730. /*
  731. * Assume we're allocating out of the middle of a free extent.
  732. * We need to find the beginning and end of the extent so we can
  733. * properly update the summary.
  734. */
  735. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  736. if (error) {
  737. return error;
  738. }
  739. /*
  740. * Find the next allocated block (end of free extent).
  741. */
  742. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  743. &postblock);
  744. if (error) {
  745. return error;
  746. }
  747. /*
  748. * Decrement the summary information corresponding to the entire
  749. * (old) free extent.
  750. */
  751. error = xfs_rtmodify_summary(mp, tp,
  752. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  753. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  754. if (error) {
  755. return error;
  756. }
  757. /*
  758. * If there are blocks not being allocated at the front of the
  759. * old extent, add summary data for them to be free.
  760. */
  761. if (preblock < start) {
  762. error = xfs_rtmodify_summary(mp, tp,
  763. XFS_RTBLOCKLOG(start - preblock),
  764. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  765. if (error) {
  766. return error;
  767. }
  768. }
  769. /*
  770. * If there are blocks not being allocated at the end of the
  771. * old extent, add summary data for them to be free.
  772. */
  773. if (postblock > end) {
  774. error = xfs_rtmodify_summary(mp, tp,
  775. XFS_RTBLOCKLOG(postblock - end),
  776. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  777. if (error) {
  778. return error;
  779. }
  780. }
  781. /*
  782. * Modify the bitmap to mark this extent allocated.
  783. */
  784. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  785. return error;
  786. }
  787. /*
  788. * Return whether there are any free extents in the size range given
  789. * by low and high, for the bitmap block bbno.
  790. */
  791. STATIC int /* error */
  792. xfs_rtany_summary(
  793. xfs_mount_t *mp, /* file system mount structure */
  794. xfs_trans_t *tp, /* transaction pointer */
  795. int low, /* low log2 extent size */
  796. int high, /* high log2 extent size */
  797. xfs_rtblock_t bbno, /* bitmap block number */
  798. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  799. xfs_fsblock_t *rsb, /* in/out: summary block number */
  800. int *stat) /* out: any good extents here? */
  801. {
  802. int error; /* error value */
  803. int log; /* loop counter, log2 of ext. size */
  804. xfs_suminfo_t sum; /* summary data */
  805. /*
  806. * Loop over logs of extent sizes. Order is irrelevant.
  807. */
  808. for (log = low; log <= high; log++) {
  809. /*
  810. * Get one summary datum.
  811. */
  812. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  813. if (error) {
  814. return error;
  815. }
  816. /*
  817. * If there are any, return success.
  818. */
  819. if (sum) {
  820. *stat = 1;
  821. return 0;
  822. }
  823. }
  824. /*
  825. * Found nothing, return failure.
  826. */
  827. *stat = 0;
  828. return 0;
  829. }
  830. /*
  831. * Get a buffer for the bitmap or summary file block specified.
  832. * The buffer is returned read and locked.
  833. */
  834. STATIC int /* error */
  835. xfs_rtbuf_get(
  836. xfs_mount_t *mp, /* file system mount structure */
  837. xfs_trans_t *tp, /* transaction pointer */
  838. xfs_rtblock_t block, /* block number in bitmap or summary */
  839. int issum, /* is summary not bitmap */
  840. xfs_buf_t **bpp) /* output: buffer for the block */
  841. {
  842. xfs_buf_t *bp; /* block buffer, result */
  843. xfs_daddr_t d; /* disk addr of block */
  844. int error; /* error value */
  845. xfs_fsblock_t fsb; /* fs block number for block */
  846. xfs_inode_t *ip; /* bitmap or summary inode */
  847. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  848. /*
  849. * Map from the file offset (block) and inode number to the
  850. * file system block.
  851. */
  852. error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
  853. if (error) {
  854. return error;
  855. }
  856. ASSERT(fsb != NULLFSBLOCK);
  857. /*
  858. * Convert to disk address for buffer cache.
  859. */
  860. d = XFS_FSB_TO_DADDR(mp, fsb);
  861. /*
  862. * Read the buffer.
  863. */
  864. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  865. mp->m_bsize, 0, &bp);
  866. if (error) {
  867. return error;
  868. }
  869. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  870. *bpp = bp;
  871. return 0;
  872. }
  873. #ifdef DEBUG
  874. /*
  875. * Check that the given extent (block range) is allocated already.
  876. */
  877. STATIC int /* error */
  878. xfs_rtcheck_alloc_range(
  879. xfs_mount_t *mp, /* file system mount point */
  880. xfs_trans_t *tp, /* transaction pointer */
  881. xfs_rtblock_t bno, /* starting block number of extent */
  882. xfs_extlen_t len, /* length of extent */
  883. int *stat) /* out: 1 for allocated, 0 for not */
  884. {
  885. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  886. return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
  887. }
  888. #endif
  889. /*
  890. * Check that the given range is either all allocated (val = 0) or
  891. * all free (val = 1).
  892. */
  893. STATIC int /* error */
  894. xfs_rtcheck_range(
  895. xfs_mount_t *mp, /* file system mount point */
  896. xfs_trans_t *tp, /* transaction pointer */
  897. xfs_rtblock_t start, /* starting block number of extent */
  898. xfs_extlen_t len, /* length of extent */
  899. int val, /* 1 for free, 0 for allocated */
  900. xfs_rtblock_t *new, /* out: first block not matching */
  901. int *stat) /* out: 1 for matches, 0 for not */
  902. {
  903. xfs_rtword_t *b; /* current word in buffer */
  904. int bit; /* bit number in the word */
  905. xfs_rtblock_t block; /* bitmap block number */
  906. xfs_buf_t *bp; /* buf for the block */
  907. xfs_rtword_t *bufp; /* starting word in buffer */
  908. int error; /* error value */
  909. xfs_rtblock_t i; /* current bit number rel. to start */
  910. xfs_rtblock_t lastbit; /* last useful bit in word */
  911. xfs_rtword_t mask; /* mask of relevant bits for value */
  912. xfs_rtword_t wdiff; /* difference from wanted value */
  913. int word; /* word number in the buffer */
  914. /*
  915. * Compute starting bitmap block number
  916. */
  917. block = XFS_BITTOBLOCK(mp, start);
  918. /*
  919. * Read the bitmap block.
  920. */
  921. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  922. if (error) {
  923. return error;
  924. }
  925. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  926. /*
  927. * Compute the starting word's address, and starting bit.
  928. */
  929. word = XFS_BITTOWORD(mp, start);
  930. b = &bufp[word];
  931. bit = (int)(start & (XFS_NBWORD - 1));
  932. /*
  933. * 0 (allocated) => all zero's; 1 (free) => all one's.
  934. */
  935. val = -val;
  936. /*
  937. * If not starting on a word boundary, deal with the first
  938. * (partial) word.
  939. */
  940. if (bit) {
  941. /*
  942. * Compute first bit not examined.
  943. */
  944. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  945. /*
  946. * Mask of relevant bits.
  947. */
  948. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  949. /*
  950. * Compute difference between actual and desired value.
  951. */
  952. if ((wdiff = (*b ^ val) & mask)) {
  953. /*
  954. * Different, compute first wrong bit and return.
  955. */
  956. xfs_trans_brelse(tp, bp);
  957. i = XFS_RTLOBIT(wdiff) - bit;
  958. *new = start + i;
  959. *stat = 0;
  960. return 0;
  961. }
  962. i = lastbit - bit;
  963. /*
  964. * Go on to next block if that's where the next word is
  965. * and we need the next word.
  966. */
  967. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  968. /*
  969. * If done with this block, get the next one.
  970. */
  971. xfs_trans_brelse(tp, bp);
  972. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  973. if (error) {
  974. return error;
  975. }
  976. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  977. word = 0;
  978. } else {
  979. /*
  980. * Go on to the next word in the buffer.
  981. */
  982. b++;
  983. }
  984. } else {
  985. /*
  986. * Starting on a word boundary, no partial word.
  987. */
  988. i = 0;
  989. }
  990. /*
  991. * Loop over whole words in buffers. When we use up one buffer
  992. * we move on to the next one.
  993. */
  994. while (len - i >= XFS_NBWORD) {
  995. /*
  996. * Compute difference between actual and desired value.
  997. */
  998. if ((wdiff = *b ^ val)) {
  999. /*
  1000. * Different, compute first wrong bit and return.
  1001. */
  1002. xfs_trans_brelse(tp, bp);
  1003. i += XFS_RTLOBIT(wdiff);
  1004. *new = start + i;
  1005. *stat = 0;
  1006. return 0;
  1007. }
  1008. i += XFS_NBWORD;
  1009. /*
  1010. * Go on to next block if that's where the next word is
  1011. * and we need the next word.
  1012. */
  1013. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1014. /*
  1015. * If done with this block, get the next one.
  1016. */
  1017. xfs_trans_brelse(tp, bp);
  1018. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1019. if (error) {
  1020. return error;
  1021. }
  1022. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1023. word = 0;
  1024. } else {
  1025. /*
  1026. * Go on to the next word in the buffer.
  1027. */
  1028. b++;
  1029. }
  1030. }
  1031. /*
  1032. * If not ending on a word boundary, deal with the last
  1033. * (partial) word.
  1034. */
  1035. if ((lastbit = len - i)) {
  1036. /*
  1037. * Mask of relevant bits.
  1038. */
  1039. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1040. /*
  1041. * Compute difference between actual and desired value.
  1042. */
  1043. if ((wdiff = (*b ^ val) & mask)) {
  1044. /*
  1045. * Different, compute first wrong bit and return.
  1046. */
  1047. xfs_trans_brelse(tp, bp);
  1048. i += XFS_RTLOBIT(wdiff);
  1049. *new = start + i;
  1050. *stat = 0;
  1051. return 0;
  1052. } else
  1053. i = len;
  1054. }
  1055. /*
  1056. * Successful, return.
  1057. */
  1058. xfs_trans_brelse(tp, bp);
  1059. *new = start + i;
  1060. *stat = 1;
  1061. return 0;
  1062. }
  1063. /*
  1064. * Copy and transform the summary file, given the old and new
  1065. * parameters in the mount structures.
  1066. */
  1067. STATIC int /* error */
  1068. xfs_rtcopy_summary(
  1069. xfs_mount_t *omp, /* old file system mount point */
  1070. xfs_mount_t *nmp, /* new file system mount point */
  1071. xfs_trans_t *tp) /* transaction pointer */
  1072. {
  1073. xfs_rtblock_t bbno; /* bitmap block number */
  1074. xfs_buf_t *bp; /* summary buffer */
  1075. int error; /* error return value */
  1076. int log; /* summary level number (log length) */
  1077. xfs_suminfo_t sum; /* summary data */
  1078. xfs_fsblock_t sumbno; /* summary block number */
  1079. bp = NULL;
  1080. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  1081. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  1082. (xfs_srtblock_t)bbno >= 0;
  1083. bbno--) {
  1084. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  1085. &sumbno, &sum);
  1086. if (error)
  1087. return error;
  1088. if (sum == 0)
  1089. continue;
  1090. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  1091. &bp, &sumbno);
  1092. if (error)
  1093. return error;
  1094. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  1095. &bp, &sumbno);
  1096. if (error)
  1097. return error;
  1098. ASSERT(sum > 0);
  1099. }
  1100. }
  1101. return 0;
  1102. }
  1103. /*
  1104. * Searching backward from start to limit, find the first block whose
  1105. * allocated/free state is different from start's.
  1106. */
  1107. STATIC int /* error */
  1108. xfs_rtfind_back(
  1109. xfs_mount_t *mp, /* file system mount point */
  1110. xfs_trans_t *tp, /* transaction pointer */
  1111. xfs_rtblock_t start, /* starting block to look at */
  1112. xfs_rtblock_t limit, /* last block to look at */
  1113. xfs_rtblock_t *rtblock) /* out: start block found */
  1114. {
  1115. xfs_rtword_t *b; /* current word in buffer */
  1116. int bit; /* bit number in the word */
  1117. xfs_rtblock_t block; /* bitmap block number */
  1118. xfs_buf_t *bp; /* buf for the block */
  1119. xfs_rtword_t *bufp; /* starting word in buffer */
  1120. int error; /* error value */
  1121. xfs_rtblock_t firstbit; /* first useful bit in the word */
  1122. xfs_rtblock_t i; /* current bit number rel. to start */
  1123. xfs_rtblock_t len; /* length of inspected area */
  1124. xfs_rtword_t mask; /* mask of relevant bits for value */
  1125. xfs_rtword_t want; /* mask for "good" values */
  1126. xfs_rtword_t wdiff; /* difference from wanted value */
  1127. int word; /* word number in the buffer */
  1128. /*
  1129. * Compute and read in starting bitmap block for starting block.
  1130. */
  1131. block = XFS_BITTOBLOCK(mp, start);
  1132. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1133. if (error) {
  1134. return error;
  1135. }
  1136. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1137. /*
  1138. * Get the first word's index & point to it.
  1139. */
  1140. word = XFS_BITTOWORD(mp, start);
  1141. b = &bufp[word];
  1142. bit = (int)(start & (XFS_NBWORD - 1));
  1143. len = start - limit + 1;
  1144. /*
  1145. * Compute match value, based on the bit at start: if 1 (free)
  1146. * then all-ones, else all-zeroes.
  1147. */
  1148. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1149. /*
  1150. * If the starting position is not word-aligned, deal with the
  1151. * partial word.
  1152. */
  1153. if (bit < XFS_NBWORD - 1) {
  1154. /*
  1155. * Calculate first (leftmost) bit number to look at,
  1156. * and mask for all the relevant bits in this word.
  1157. */
  1158. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  1159. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  1160. firstbit;
  1161. /*
  1162. * Calculate the difference between the value there
  1163. * and what we're looking for.
  1164. */
  1165. if ((wdiff = (*b ^ want) & mask)) {
  1166. /*
  1167. * Different. Mark where we are and return.
  1168. */
  1169. xfs_trans_brelse(tp, bp);
  1170. i = bit - XFS_RTHIBIT(wdiff);
  1171. *rtblock = start - i + 1;
  1172. return 0;
  1173. }
  1174. i = bit - firstbit + 1;
  1175. /*
  1176. * Go on to previous block if that's where the previous word is
  1177. * and we need the previous word.
  1178. */
  1179. if (--word == -1 && i < len) {
  1180. /*
  1181. * If done with this block, get the previous one.
  1182. */
  1183. xfs_trans_brelse(tp, bp);
  1184. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1185. if (error) {
  1186. return error;
  1187. }
  1188. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1189. word = XFS_BLOCKWMASK(mp);
  1190. b = &bufp[word];
  1191. } else {
  1192. /*
  1193. * Go on to the previous word in the buffer.
  1194. */
  1195. b--;
  1196. }
  1197. } else {
  1198. /*
  1199. * Starting on a word boundary, no partial word.
  1200. */
  1201. i = 0;
  1202. }
  1203. /*
  1204. * Loop over whole words in buffers. When we use up one buffer
  1205. * we move on to the previous one.
  1206. */
  1207. while (len - i >= XFS_NBWORD) {
  1208. /*
  1209. * Compute difference between actual and desired value.
  1210. */
  1211. if ((wdiff = *b ^ want)) {
  1212. /*
  1213. * Different, mark where we are and return.
  1214. */
  1215. xfs_trans_brelse(tp, bp);
  1216. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1217. *rtblock = start - i + 1;
  1218. return 0;
  1219. }
  1220. i += XFS_NBWORD;
  1221. /*
  1222. * Go on to previous block if that's where the previous word is
  1223. * and we need the previous word.
  1224. */
  1225. if (--word == -1 && i < len) {
  1226. /*
  1227. * If done with this block, get the previous one.
  1228. */
  1229. xfs_trans_brelse(tp, bp);
  1230. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1231. if (error) {
  1232. return error;
  1233. }
  1234. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1235. word = XFS_BLOCKWMASK(mp);
  1236. b = &bufp[word];
  1237. } else {
  1238. /*
  1239. * Go on to the previous word in the buffer.
  1240. */
  1241. b--;
  1242. }
  1243. }
  1244. /*
  1245. * If not ending on a word boundary, deal with the last
  1246. * (partial) word.
  1247. */
  1248. if (len - i) {
  1249. /*
  1250. * Calculate first (leftmost) bit number to look at,
  1251. * and mask for all the relevant bits in this word.
  1252. */
  1253. firstbit = XFS_NBWORD - (len - i);
  1254. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  1255. /*
  1256. * Compute difference between actual and desired value.
  1257. */
  1258. if ((wdiff = (*b ^ want) & mask)) {
  1259. /*
  1260. * Different, mark where we are and return.
  1261. */
  1262. xfs_trans_brelse(tp, bp);
  1263. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1264. *rtblock = start - i + 1;
  1265. return 0;
  1266. } else
  1267. i = len;
  1268. }
  1269. /*
  1270. * No match, return that we scanned the whole area.
  1271. */
  1272. xfs_trans_brelse(tp, bp);
  1273. *rtblock = start - i + 1;
  1274. return 0;
  1275. }
  1276. /*
  1277. * Searching forward from start to limit, find the first block whose
  1278. * allocated/free state is different from start's.
  1279. */
  1280. STATIC int /* error */
  1281. xfs_rtfind_forw(
  1282. xfs_mount_t *mp, /* file system mount point */
  1283. xfs_trans_t *tp, /* transaction pointer */
  1284. xfs_rtblock_t start, /* starting block to look at */
  1285. xfs_rtblock_t limit, /* last block to look at */
  1286. xfs_rtblock_t *rtblock) /* out: start block found */
  1287. {
  1288. xfs_rtword_t *b; /* current word in buffer */
  1289. int bit; /* bit number in the word */
  1290. xfs_rtblock_t block; /* bitmap block number */
  1291. xfs_buf_t *bp; /* buf for the block */
  1292. xfs_rtword_t *bufp; /* starting word in buffer */
  1293. int error; /* error value */
  1294. xfs_rtblock_t i; /* current bit number rel. to start */
  1295. xfs_rtblock_t lastbit; /* last useful bit in the word */
  1296. xfs_rtblock_t len; /* length of inspected area */
  1297. xfs_rtword_t mask; /* mask of relevant bits for value */
  1298. xfs_rtword_t want; /* mask for "good" values */
  1299. xfs_rtword_t wdiff; /* difference from wanted value */
  1300. int word; /* word number in the buffer */
  1301. /*
  1302. * Compute and read in starting bitmap block for starting block.
  1303. */
  1304. block = XFS_BITTOBLOCK(mp, start);
  1305. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1306. if (error) {
  1307. return error;
  1308. }
  1309. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1310. /*
  1311. * Get the first word's index & point to it.
  1312. */
  1313. word = XFS_BITTOWORD(mp, start);
  1314. b = &bufp[word];
  1315. bit = (int)(start & (XFS_NBWORD - 1));
  1316. len = limit - start + 1;
  1317. /*
  1318. * Compute match value, based on the bit at start: if 1 (free)
  1319. * then all-ones, else all-zeroes.
  1320. */
  1321. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1322. /*
  1323. * If the starting position is not word-aligned, deal with the
  1324. * partial word.
  1325. */
  1326. if (bit) {
  1327. /*
  1328. * Calculate last (rightmost) bit number to look at,
  1329. * and mask for all the relevant bits in this word.
  1330. */
  1331. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1332. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1333. /*
  1334. * Calculate the difference between the value there
  1335. * and what we're looking for.
  1336. */
  1337. if ((wdiff = (*b ^ want) & mask)) {
  1338. /*
  1339. * Different. Mark where we are and return.
  1340. */
  1341. xfs_trans_brelse(tp, bp);
  1342. i = XFS_RTLOBIT(wdiff) - bit;
  1343. *rtblock = start + i - 1;
  1344. return 0;
  1345. }
  1346. i = lastbit - bit;
  1347. /*
  1348. * Go on to next block if that's where the next word is
  1349. * and we need the next word.
  1350. */
  1351. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1352. /*
  1353. * If done with this block, get the previous one.
  1354. */
  1355. xfs_trans_brelse(tp, bp);
  1356. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1357. if (error) {
  1358. return error;
  1359. }
  1360. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1361. word = 0;
  1362. } else {
  1363. /*
  1364. * Go on to the previous word in the buffer.
  1365. */
  1366. b++;
  1367. }
  1368. } else {
  1369. /*
  1370. * Starting on a word boundary, no partial word.
  1371. */
  1372. i = 0;
  1373. }
  1374. /*
  1375. * Loop over whole words in buffers. When we use up one buffer
  1376. * we move on to the next one.
  1377. */
  1378. while (len - i >= XFS_NBWORD) {
  1379. /*
  1380. * Compute difference between actual and desired value.
  1381. */
  1382. if ((wdiff = *b ^ want)) {
  1383. /*
  1384. * Different, mark where we are and return.
  1385. */
  1386. xfs_trans_brelse(tp, bp);
  1387. i += XFS_RTLOBIT(wdiff);
  1388. *rtblock = start + i - 1;
  1389. return 0;
  1390. }
  1391. i += XFS_NBWORD;
  1392. /*
  1393. * Go on to next block if that's where the next word is
  1394. * and we need the next word.
  1395. */
  1396. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1397. /*
  1398. * If done with this block, get the next one.
  1399. */
  1400. xfs_trans_brelse(tp, bp);
  1401. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1402. if (error) {
  1403. return error;
  1404. }
  1405. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1406. word = 0;
  1407. } else {
  1408. /*
  1409. * Go on to the next word in the buffer.
  1410. */
  1411. b++;
  1412. }
  1413. }
  1414. /*
  1415. * If not ending on a word boundary, deal with the last
  1416. * (partial) word.
  1417. */
  1418. if ((lastbit = len - i)) {
  1419. /*
  1420. * Calculate mask for all the relevant bits in this word.
  1421. */
  1422. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1423. /*
  1424. * Compute difference between actual and desired value.
  1425. */
  1426. if ((wdiff = (*b ^ want) & mask)) {
  1427. /*
  1428. * Different, mark where we are and return.
  1429. */
  1430. xfs_trans_brelse(tp, bp);
  1431. i += XFS_RTLOBIT(wdiff);
  1432. *rtblock = start + i - 1;
  1433. return 0;
  1434. } else
  1435. i = len;
  1436. }
  1437. /*
  1438. * No match, return that we scanned the whole area.
  1439. */
  1440. xfs_trans_brelse(tp, bp);
  1441. *rtblock = start + i - 1;
  1442. return 0;
  1443. }
  1444. /*
  1445. * Mark an extent specified by start and len freed.
  1446. * Updates all the summary information as well as the bitmap.
  1447. */
  1448. STATIC int /* error */
  1449. xfs_rtfree_range(
  1450. xfs_mount_t *mp, /* file system mount point */
  1451. xfs_trans_t *tp, /* transaction pointer */
  1452. xfs_rtblock_t start, /* starting block to free */
  1453. xfs_extlen_t len, /* length to free */
  1454. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1455. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1456. {
  1457. xfs_rtblock_t end; /* end of the freed extent */
  1458. int error; /* error value */
  1459. xfs_rtblock_t postblock; /* first block freed > end */
  1460. xfs_rtblock_t preblock; /* first block freed < start */
  1461. end = start + len - 1;
  1462. /*
  1463. * Modify the bitmap to mark this extent freed.
  1464. */
  1465. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  1466. if (error) {
  1467. return error;
  1468. }
  1469. /*
  1470. * Assume we're freeing out of the middle of an allocated extent.
  1471. * We need to find the beginning and end of the extent so we can
  1472. * properly update the summary.
  1473. */
  1474. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  1475. if (error) {
  1476. return error;
  1477. }
  1478. /*
  1479. * Find the next allocated block (end of allocated extent).
  1480. */
  1481. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  1482. &postblock);
  1483. /*
  1484. * If there are blocks not being freed at the front of the
  1485. * old extent, add summary data for them to be allocated.
  1486. */
  1487. if (preblock < start) {
  1488. error = xfs_rtmodify_summary(mp, tp,
  1489. XFS_RTBLOCKLOG(start - preblock),
  1490. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  1491. if (error) {
  1492. return error;
  1493. }
  1494. }
  1495. /*
  1496. * If there are blocks not being freed at the end of the
  1497. * old extent, add summary data for them to be allocated.
  1498. */
  1499. if (postblock > end) {
  1500. error = xfs_rtmodify_summary(mp, tp,
  1501. XFS_RTBLOCKLOG(postblock - end),
  1502. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  1503. if (error) {
  1504. return error;
  1505. }
  1506. }
  1507. /*
  1508. * Increment the summary information corresponding to the entire
  1509. * (new) free extent.
  1510. */
  1511. error = xfs_rtmodify_summary(mp, tp,
  1512. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  1513. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  1514. return error;
  1515. }
  1516. /*
  1517. * Read and return the summary information for a given extent size,
  1518. * bitmap block combination.
  1519. * Keeps track of a current summary block, so we don't keep reading
  1520. * it from the buffer cache.
  1521. */
  1522. STATIC int /* error */
  1523. xfs_rtget_summary(
  1524. xfs_mount_t *mp, /* file system mount structure */
  1525. xfs_trans_t *tp, /* transaction pointer */
  1526. int log, /* log2 of extent size */
  1527. xfs_rtblock_t bbno, /* bitmap block number */
  1528. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1529. xfs_fsblock_t *rsb, /* in/out: summary block number */
  1530. xfs_suminfo_t *sum) /* out: summary info for this block */
  1531. {
  1532. xfs_buf_t *bp; /* buffer for summary block */
  1533. int error; /* error value */
  1534. xfs_fsblock_t sb; /* summary fsblock */
  1535. int so; /* index into the summary file */
  1536. xfs_suminfo_t *sp; /* pointer to returned data */
  1537. /*
  1538. * Compute entry number in the summary file.
  1539. */
  1540. so = XFS_SUMOFFS(mp, log, bbno);
  1541. /*
  1542. * Compute the block number in the summary file.
  1543. */
  1544. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1545. /*
  1546. * If we have an old buffer, and the block number matches, use that.
  1547. */
  1548. if (rbpp && *rbpp && *rsb == sb)
  1549. bp = *rbpp;
  1550. /*
  1551. * Otherwise we have to get the buffer.
  1552. */
  1553. else {
  1554. /*
  1555. * If there was an old one, get rid of it first.
  1556. */
  1557. if (rbpp && *rbpp)
  1558. xfs_trans_brelse(tp, *rbpp);
  1559. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1560. if (error) {
  1561. return error;
  1562. }
  1563. /*
  1564. * Remember this buffer and block for the next call.
  1565. */
  1566. if (rbpp) {
  1567. *rbpp = bp;
  1568. *rsb = sb;
  1569. }
  1570. }
  1571. /*
  1572. * Point to the summary information & copy it out.
  1573. */
  1574. sp = XFS_SUMPTR(mp, bp, so);
  1575. *sum = *sp;
  1576. /*
  1577. * Drop the buffer if we're not asked to remember it.
  1578. */
  1579. if (!rbpp)
  1580. xfs_trans_brelse(tp, bp);
  1581. return 0;
  1582. }
  1583. /*
  1584. * Set the given range of bitmap bits to the given value.
  1585. * Do whatever I/O and logging is required.
  1586. */
  1587. STATIC int /* error */
  1588. xfs_rtmodify_range(
  1589. xfs_mount_t *mp, /* file system mount point */
  1590. xfs_trans_t *tp, /* transaction pointer */
  1591. xfs_rtblock_t start, /* starting block to modify */
  1592. xfs_extlen_t len, /* length of extent to modify */
  1593. int val) /* 1 for free, 0 for allocated */
  1594. {
  1595. xfs_rtword_t *b; /* current word in buffer */
  1596. int bit; /* bit number in the word */
  1597. xfs_rtblock_t block; /* bitmap block number */
  1598. xfs_buf_t *bp; /* buf for the block */
  1599. xfs_rtword_t *bufp; /* starting word in buffer */
  1600. int error; /* error value */
  1601. xfs_rtword_t *first; /* first used word in the buffer */
  1602. int i; /* current bit number rel. to start */
  1603. int lastbit; /* last useful bit in word */
  1604. xfs_rtword_t mask; /* mask o frelevant bits for value */
  1605. int word; /* word number in the buffer */
  1606. /*
  1607. * Compute starting bitmap block number.
  1608. */
  1609. block = XFS_BITTOBLOCK(mp, start);
  1610. /*
  1611. * Read the bitmap block, and point to its data.
  1612. */
  1613. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1614. if (error) {
  1615. return error;
  1616. }
  1617. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1618. /*
  1619. * Compute the starting word's address, and starting bit.
  1620. */
  1621. word = XFS_BITTOWORD(mp, start);
  1622. first = b = &bufp[word];
  1623. bit = (int)(start & (XFS_NBWORD - 1));
  1624. /*
  1625. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  1626. */
  1627. val = -val;
  1628. /*
  1629. * If not starting on a word boundary, deal with the first
  1630. * (partial) word.
  1631. */
  1632. if (bit) {
  1633. /*
  1634. * Compute first bit not changed and mask of relevant bits.
  1635. */
  1636. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1637. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1638. /*
  1639. * Set/clear the active bits.
  1640. */
  1641. if (val)
  1642. *b |= mask;
  1643. else
  1644. *b &= ~mask;
  1645. i = lastbit - bit;
  1646. /*
  1647. * Go on to the next block if that's where the next word is
  1648. * and we need the next word.
  1649. */
  1650. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1651. /*
  1652. * Log the changed part of this block.
  1653. * Get the next one.
  1654. */
  1655. xfs_trans_log_buf(tp, bp,
  1656. (uint)((char *)first - (char *)bufp),
  1657. (uint)((char *)b - (char *)bufp));
  1658. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1659. if (error) {
  1660. return error;
  1661. }
  1662. first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1663. word = 0;
  1664. } else {
  1665. /*
  1666. * Go on to the next word in the buffer
  1667. */
  1668. b++;
  1669. }
  1670. } else {
  1671. /*
  1672. * Starting on a word boundary, no partial word.
  1673. */
  1674. i = 0;
  1675. }
  1676. /*
  1677. * Loop over whole words in buffers. When we use up one buffer
  1678. * we move on to the next one.
  1679. */
  1680. while (len - i >= XFS_NBWORD) {
  1681. /*
  1682. * Set the word value correctly.
  1683. */
  1684. *b = val;
  1685. i += XFS_NBWORD;
  1686. /*
  1687. * Go on to the next block if that's where the next word is
  1688. * and we need the next word.
  1689. */
  1690. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1691. /*
  1692. * Log the changed part of this block.
  1693. * Get the next one.
  1694. */
  1695. xfs_trans_log_buf(tp, bp,
  1696. (uint)((char *)first - (char *)bufp),
  1697. (uint)((char *)b - (char *)bufp));
  1698. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1699. if (error) {
  1700. return error;
  1701. }
  1702. first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1703. word = 0;
  1704. } else {
  1705. /*
  1706. * Go on to the next word in the buffer
  1707. */
  1708. b++;
  1709. }
  1710. }
  1711. /*
  1712. * If not ending on a word boundary, deal with the last
  1713. * (partial) word.
  1714. */
  1715. if ((lastbit = len - i)) {
  1716. /*
  1717. * Compute a mask of relevant bits.
  1718. */
  1719. bit = 0;
  1720. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1721. /*
  1722. * Set/clear the active bits.
  1723. */
  1724. if (val)
  1725. *b |= mask;
  1726. else
  1727. *b &= ~mask;
  1728. b++;
  1729. }
  1730. /*
  1731. * Log any remaining changed bytes.
  1732. */
  1733. if (b > first)
  1734. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  1735. (uint)((char *)b - (char *)bufp - 1));
  1736. return 0;
  1737. }
  1738. /*
  1739. * Read and modify the summary information for a given extent size,
  1740. * bitmap block combination.
  1741. * Keeps track of a current summary block, so we don't keep reading
  1742. * it from the buffer cache.
  1743. */
  1744. STATIC int /* error */
  1745. xfs_rtmodify_summary(
  1746. xfs_mount_t *mp, /* file system mount point */
  1747. xfs_trans_t *tp, /* transaction pointer */
  1748. int log, /* log2 of extent size */
  1749. xfs_rtblock_t bbno, /* bitmap block number */
  1750. int delta, /* change to make to summary info */
  1751. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1752. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1753. {
  1754. xfs_buf_t *bp; /* buffer for the summary block */
  1755. int error; /* error value */
  1756. xfs_fsblock_t sb; /* summary fsblock */
  1757. int so; /* index into the summary file */
  1758. xfs_suminfo_t *sp; /* pointer to returned data */
  1759. /*
  1760. * Compute entry number in the summary file.
  1761. */
  1762. so = XFS_SUMOFFS(mp, log, bbno);
  1763. /*
  1764. * Compute the block number in the summary file.
  1765. */
  1766. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1767. /*
  1768. * If we have an old buffer, and the block number matches, use that.
  1769. */
  1770. if (rbpp && *rbpp && *rsb == sb)
  1771. bp = *rbpp;
  1772. /*
  1773. * Otherwise we have to get the buffer.
  1774. */
  1775. else {
  1776. /*
  1777. * If there was an old one, get rid of it first.
  1778. */
  1779. if (rbpp && *rbpp)
  1780. xfs_trans_brelse(tp, *rbpp);
  1781. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1782. if (error) {
  1783. return error;
  1784. }
  1785. /*
  1786. * Remember this buffer and block for the next call.
  1787. */
  1788. if (rbpp) {
  1789. *rbpp = bp;
  1790. *rsb = sb;
  1791. }
  1792. }
  1793. /*
  1794. * Point to the summary information, modify and log it.
  1795. */
  1796. sp = XFS_SUMPTR(mp, bp, so);
  1797. *sp += delta;
  1798. xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)XFS_BUF_PTR(bp)),
  1799. (uint)((char *)sp - (char *)XFS_BUF_PTR(bp) + sizeof(*sp) - 1));
  1800. return 0;
  1801. }
  1802. /*
  1803. * Visible (exported) functions.
  1804. */
  1805. /*
  1806. * Grow the realtime area of the filesystem.
  1807. */
  1808. int
  1809. xfs_growfs_rt(
  1810. xfs_mount_t *mp, /* mount point for filesystem */
  1811. xfs_growfs_rt_t *in) /* growfs rt input struct */
  1812. {
  1813. xfs_rtblock_t bmbno; /* bitmap block number */
  1814. xfs_buf_t *bp; /* temporary buffer */
  1815. int cancelflags; /* flags for xfs_trans_cancel */
  1816. int error; /* error return value */
  1817. xfs_inode_t *ip; /* bitmap inode, used as lock */
  1818. xfs_mount_t *nmp; /* new (fake) mount structure */
  1819. xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
  1820. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  1821. xfs_drtbno_t nrextents; /* new number of realtime extents */
  1822. uint8_t nrextslog; /* new log2 of sb_rextents */
  1823. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  1824. uint nrsumlevels; /* new rt summary levels */
  1825. uint nrsumsize; /* new size of rt summary, bytes */
  1826. xfs_sb_t *nsbp; /* new superblock */
  1827. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  1828. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  1829. xfs_sb_t *sbp; /* old superblock */
  1830. xfs_fsblock_t sumbno; /* summary block number */
  1831. xfs_trans_t *tp; /* transaction pointer */
  1832. sbp = &mp->m_sb;
  1833. /*
  1834. * Initial error checking.
  1835. */
  1836. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  1837. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  1838. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  1839. return XFS_ERROR(EINVAL);
  1840. /*
  1841. * Read in the last block of the device, make sure it exists.
  1842. */
  1843. error = xfs_read_buf(mp, mp->m_rtdev_targp,
  1844. XFS_FSB_TO_BB(mp, in->newblocks - 1),
  1845. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  1846. if (error)
  1847. return error;
  1848. ASSERT(bp);
  1849. xfs_buf_relse(bp);
  1850. /*
  1851. * Calculate new parameters. These are the final values to be reached.
  1852. */
  1853. nrextents = nrblocks;
  1854. do_div(nrextents, in->extsize);
  1855. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  1856. nrextslog = xfs_highbit32(nrextents);
  1857. nrsumlevels = nrextslog + 1;
  1858. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  1859. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1860. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1861. /*
  1862. * New summary size can't be more than half the size of
  1863. * the log. This prevents us from getting a log overflow,
  1864. * since we'll log basically the whole summary file at once.
  1865. */
  1866. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  1867. return XFS_ERROR(EINVAL);
  1868. /*
  1869. * Get the old block counts for bitmap and summary inodes.
  1870. * These can't change since other growfs callers are locked out.
  1871. */
  1872. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  1873. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  1874. /*
  1875. * Allocate space to the bitmap and summary files, as necessary.
  1876. */
  1877. if ((error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks,
  1878. mp->m_sb.sb_rbmino)))
  1879. return error;
  1880. if ((error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks,
  1881. mp->m_sb.sb_rsumino)))
  1882. return error;
  1883. /*
  1884. * Allocate a new (fake) mount/sb.
  1885. */
  1886. nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
  1887. /*
  1888. * Loop over the bitmap blocks.
  1889. * We will do everything one bitmap block at a time.
  1890. * Skip the current block if it is exactly full.
  1891. * This also deals with the case where there were no rtextents before.
  1892. */
  1893. for (bmbno = sbp->sb_rbmblocks -
  1894. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  1895. bmbno < nrbmblocks;
  1896. bmbno++) {
  1897. *nmp = *mp;
  1898. nsbp = &nmp->m_sb;
  1899. /*
  1900. * Calculate new sb and mount fields for this round.
  1901. */
  1902. nsbp->sb_rextsize = in->extsize;
  1903. nsbp->sb_rbmblocks = bmbno + 1;
  1904. nsbp->sb_rblocks =
  1905. XFS_RTMIN(nrblocks,
  1906. nsbp->sb_rbmblocks * NBBY *
  1907. nsbp->sb_blocksize * nsbp->sb_rextsize);
  1908. nsbp->sb_rextents = nsbp->sb_rblocks;
  1909. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  1910. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  1911. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  1912. nrsumsize =
  1913. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  1914. nsbp->sb_rbmblocks;
  1915. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1916. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1917. /*
  1918. * Start a transaction, get the log reservation.
  1919. */
  1920. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
  1921. cancelflags = 0;
  1922. if ((error = xfs_trans_reserve(tp, 0,
  1923. XFS_GROWRTFREE_LOG_RES(nmp), 0, 0, 0)))
  1924. break;
  1925. /*
  1926. * Lock out other callers by grabbing the bitmap inode lock.
  1927. */
  1928. if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
  1929. XFS_ILOCK_EXCL, &ip)))
  1930. break;
  1931. ASSERT(ip == mp->m_rbmip);
  1932. /*
  1933. * Update the bitmap inode's size.
  1934. */
  1935. mp->m_rbmip->i_d.di_size =
  1936. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  1937. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1938. cancelflags |= XFS_TRANS_ABORT;
  1939. /*
  1940. * Get the summary inode into the transaction.
  1941. */
  1942. if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rsumino, 0,
  1943. XFS_ILOCK_EXCL, &ip)))
  1944. break;
  1945. ASSERT(ip == mp->m_rsumip);
  1946. /*
  1947. * Update the summary inode's size.
  1948. */
  1949. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  1950. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  1951. /*
  1952. * Copy summary data from old to new sizes.
  1953. * Do this when the real size (not block-aligned) changes.
  1954. */
  1955. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1956. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1957. error = xfs_rtcopy_summary(mp, nmp, tp);
  1958. if (error)
  1959. break;
  1960. }
  1961. /*
  1962. * Update superblock fields.
  1963. */
  1964. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1965. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1966. nsbp->sb_rextsize - sbp->sb_rextsize);
  1967. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1968. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1969. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1970. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1971. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1972. nsbp->sb_rblocks - sbp->sb_rblocks);
  1973. if (nsbp->sb_rextents != sbp->sb_rextents)
  1974. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1975. nsbp->sb_rextents - sbp->sb_rextents);
  1976. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1977. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1978. nsbp->sb_rextslog - sbp->sb_rextslog);
  1979. /*
  1980. * Free new extent.
  1981. */
  1982. bp = NULL;
  1983. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1984. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1985. if (error)
  1986. break;
  1987. /*
  1988. * Mark more blocks free in the superblock.
  1989. */
  1990. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1991. nsbp->sb_rextents - sbp->sb_rextents);
  1992. /*
  1993. * Update mp values into the real mp structure.
  1994. */
  1995. mp->m_rsumlevels = nrsumlevels;
  1996. mp->m_rsumsize = nrsumsize;
  1997. /*
  1998. * Commit the transaction.
  1999. */
  2000. xfs_trans_commit(tp, 0, NULL);
  2001. }
  2002. if (error)
  2003. xfs_trans_cancel(tp, cancelflags);
  2004. /*
  2005. * Free the fake mp structure.
  2006. */
  2007. kmem_free(nmp, sizeof(*nmp));
  2008. return error;
  2009. }
  2010. /*
  2011. * Allocate an extent in the realtime subvolume, with the usual allocation
  2012. * parameters. The length units are all in realtime extents, as is the
  2013. * result block number.
  2014. */
  2015. int /* error */
  2016. xfs_rtallocate_extent(
  2017. xfs_trans_t *tp, /* transaction pointer */
  2018. xfs_rtblock_t bno, /* starting block number to allocate */
  2019. xfs_extlen_t minlen, /* minimum length to allocate */
  2020. xfs_extlen_t maxlen, /* maximum length to allocate */
  2021. xfs_extlen_t *len, /* out: actual length allocated */
  2022. xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
  2023. int wasdel, /* was a delayed allocation extent */
  2024. xfs_extlen_t prod, /* extent product factor */
  2025. xfs_rtblock_t *rtblock) /* out: start block allocated */
  2026. {
  2027. int error; /* error value */
  2028. xfs_inode_t *ip; /* inode for bitmap file */
  2029. xfs_mount_t *mp; /* file system mount structure */
  2030. xfs_rtblock_t r; /* result allocated block */
  2031. xfs_fsblock_t sb; /* summary file block number */
  2032. xfs_buf_t *sumbp; /* summary file block buffer */
  2033. ASSERT(minlen > 0 && minlen <= maxlen);
  2034. mp = tp->t_mountp;
  2035. /*
  2036. * If prod is set then figure out what to do to minlen and maxlen.
  2037. */
  2038. if (prod > 1) {
  2039. xfs_extlen_t i;
  2040. if ((i = maxlen % prod))
  2041. maxlen -= i;
  2042. if ((i = minlen % prod))
  2043. minlen += prod - i;
  2044. if (maxlen < minlen) {
  2045. *rtblock = NULLRTBLOCK;
  2046. return 0;
  2047. }
  2048. }
  2049. /*
  2050. * Lock out other callers by grabbing the bitmap inode lock.
  2051. */
  2052. if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
  2053. XFS_ILOCK_EXCL, &ip)))
  2054. return error;
  2055. sumbp = NULL;
  2056. /*
  2057. * Allocate by size, or near another block, or exactly at some block.
  2058. */
  2059. switch (type) {
  2060. case XFS_ALLOCTYPE_ANY_AG:
  2061. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  2062. &sumbp, &sb, prod, &r);
  2063. break;
  2064. case XFS_ALLOCTYPE_NEAR_BNO:
  2065. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  2066. len, &sumbp, &sb, prod, &r);
  2067. break;
  2068. case XFS_ALLOCTYPE_THIS_BNO:
  2069. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
  2070. len, &sumbp, &sb, prod, &r);
  2071. break;
  2072. default:
  2073. ASSERT(0);
  2074. }
  2075. if (error) {
  2076. return error;
  2077. }
  2078. /*
  2079. * If it worked, update the superblock.
  2080. */
  2081. if (r != NULLRTBLOCK) {
  2082. long slen = (long)*len;
  2083. ASSERT(*len >= minlen && *len <= maxlen);
  2084. if (wasdel)
  2085. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  2086. else
  2087. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  2088. }
  2089. *rtblock = r;
  2090. return 0;
  2091. }
  2092. /*
  2093. * Free an extent in the realtime subvolume. Length is expressed in
  2094. * realtime extents, as is the block number.
  2095. */
  2096. int /* error */
  2097. xfs_rtfree_extent(
  2098. xfs_trans_t *tp, /* transaction pointer */
  2099. xfs_rtblock_t bno, /* starting block number to free */
  2100. xfs_extlen_t len) /* length of extent freed */
  2101. {
  2102. int error; /* error value */
  2103. xfs_inode_t *ip; /* bitmap file inode */
  2104. xfs_mount_t *mp; /* file system mount structure */
  2105. xfs_fsblock_t sb; /* summary file block number */
  2106. xfs_buf_t *sumbp; /* summary file block buffer */
  2107. mp = tp->t_mountp;
  2108. /*
  2109. * Synchronize by locking the bitmap inode.
  2110. */
  2111. if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
  2112. XFS_ILOCK_EXCL, &ip)))
  2113. return error;
  2114. #if defined(__KERNEL__) && defined(DEBUG)
  2115. /*
  2116. * Check to see that this whole range is currently allocated.
  2117. */
  2118. {
  2119. int stat; /* result from checking range */
  2120. error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
  2121. if (error) {
  2122. return error;
  2123. }
  2124. ASSERT(stat);
  2125. }
  2126. #endif
  2127. sumbp = NULL;
  2128. /*
  2129. * Free the range of realtime blocks.
  2130. */
  2131. error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
  2132. if (error) {
  2133. return error;
  2134. }
  2135. /*
  2136. * Mark more blocks free in the superblock.
  2137. */
  2138. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
  2139. /*
  2140. * If we've now freed all the blocks, reset the file sequence
  2141. * number to 0.
  2142. */
  2143. if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
  2144. mp->m_sb.sb_rextents) {
  2145. if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
  2146. ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2147. *(__uint64_t *)&ip->i_d.di_atime = 0;
  2148. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2149. }
  2150. return 0;
  2151. }
  2152. /*
  2153. * Initialize realtime fields in the mount structure.
  2154. */
  2155. int /* error */
  2156. xfs_rtmount_init(
  2157. xfs_mount_t *mp) /* file system mount structure */
  2158. {
  2159. xfs_buf_t *bp; /* buffer for last block of subvolume */
  2160. xfs_daddr_t d; /* address of last block of subvolume */
  2161. int error; /* error return value */
  2162. xfs_sb_t *sbp; /* filesystem superblock copy in mount */
  2163. sbp = &mp->m_sb;
  2164. if (sbp->sb_rblocks == 0)
  2165. return 0;
  2166. if (mp->m_rtdev_targp == NULL) {
  2167. cmn_err(CE_WARN,
  2168. "XFS: This filesystem has a realtime volume, use rtdev=device option");
  2169. return XFS_ERROR(ENODEV);
  2170. }
  2171. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  2172. mp->m_rsumsize =
  2173. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  2174. sbp->sb_rbmblocks;
  2175. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  2176. mp->m_rbmip = mp->m_rsumip = NULL;
  2177. /*
  2178. * Check that the realtime section is an ok size.
  2179. */
  2180. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  2181. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  2182. cmn_err(CE_WARN, "XFS: realtime mount -- %llu != %llu",
  2183. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  2184. (unsigned long long) mp->m_sb.sb_rblocks);
  2185. return XFS_ERROR(E2BIG);
  2186. }
  2187. error = xfs_read_buf(mp, mp->m_rtdev_targp,
  2188. d - XFS_FSB_TO_BB(mp, 1),
  2189. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  2190. if (error) {
  2191. cmn_err(CE_WARN,
  2192. "XFS: realtime mount -- xfs_read_buf failed, returned %d", error);
  2193. if (error == ENOSPC)
  2194. return XFS_ERROR(E2BIG);
  2195. return error;
  2196. }
  2197. xfs_buf_relse(bp);
  2198. return 0;
  2199. }
  2200. /*
  2201. * Get the bitmap and summary inodes into the mount structure
  2202. * at mount time.
  2203. */
  2204. int /* error */
  2205. xfs_rtmount_inodes(
  2206. xfs_mount_t *mp) /* file system mount structure */
  2207. {
  2208. int error; /* error return value */
  2209. xfs_sb_t *sbp;
  2210. sbp = &mp->m_sb;
  2211. if (sbp->sb_rbmino == NULLFSINO)
  2212. return 0;
  2213. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip, 0);
  2214. if (error)
  2215. return error;
  2216. ASSERT(mp->m_rbmip != NULL);
  2217. ASSERT(sbp->sb_rsumino != NULLFSINO);
  2218. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip, 0);
  2219. if (error) {
  2220. VN_RELE(XFS_ITOV(mp->m_rbmip));
  2221. return error;
  2222. }
  2223. ASSERT(mp->m_rsumip != NULL);
  2224. return 0;
  2225. }
  2226. /*
  2227. * Pick an extent for allocation at the start of a new realtime file.
  2228. * Use the sequence number stored in the atime field of the bitmap inode.
  2229. * Translate this to a fraction of the rtextents, and return the product
  2230. * of rtextents and the fraction.
  2231. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  2232. */
  2233. int /* error */
  2234. xfs_rtpick_extent(
  2235. xfs_mount_t *mp, /* file system mount point */
  2236. xfs_trans_t *tp, /* transaction pointer */
  2237. xfs_extlen_t len, /* allocation length (rtextents) */
  2238. xfs_rtblock_t *pick) /* result rt extent */
  2239. {
  2240. xfs_rtblock_t b; /* result block */
  2241. int error; /* error return value */
  2242. xfs_inode_t *ip; /* bitmap incore inode */
  2243. int log2; /* log of sequence number */
  2244. __uint64_t resid; /* residual after log removed */
  2245. __uint64_t seq; /* sequence number of file creation */
  2246. __uint64_t *seqp; /* pointer to seqno in inode */
  2247. if ((error = xfs_trans_iget(mp, tp, mp->m_sb.sb_rbmino, 0,
  2248. XFS_ILOCK_EXCL, &ip)))
  2249. return error;
  2250. ASSERT(ip == mp->m_rbmip);
  2251. seqp = (__uint64_t *)&ip->i_d.di_atime;
  2252. if (!(ip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  2253. ip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2254. *seqp = 0;
  2255. }
  2256. seq = *seqp;
  2257. if ((log2 = xfs_highbit64(seq)) == -1)
  2258. b = 0;
  2259. else {
  2260. resid = seq - (1ULL << log2);
  2261. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  2262. (log2 + 1);
  2263. if (b >= mp->m_sb.sb_rextents)
  2264. b = do_mod(b, mp->m_sb.sb_rextents);
  2265. if (b + len > mp->m_sb.sb_rextents)
  2266. b = mp->m_sb.sb_rextents - len;
  2267. }
  2268. *seqp = seq + 1;
  2269. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2270. *pick = b;
  2271. return 0;
  2272. }