PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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

Large files files are truncated, but you can click here to view the full file

  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 */

Large files files are truncated, but you can click here to view the full file