PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_rtalloc.c

https://bitbucket.org/evzijst/gittest
C | 2469 lines | 1495 code | 69 blank | 905 comment | 293 complexity | d804284312fe0d3edb8a8c4c1967d33c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0

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

  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. /*
  33. * Free realtime space allocation for XFS.
  34. */
  35. #include "xfs.h"
  36. #include "xfs_macros.h"
  37. #include "xfs_types.h"
  38. #include "xfs_inum.h"
  39. #include "xfs_log.h"
  40. #include "xfs_trans.h"
  41. #include "xfs_sb.h"
  42. #include "xfs_ag.h"
  43. #include "xfs_dir.h"
  44. #include "xfs_dir2.h"
  45. #include "xfs_dmapi.h"
  46. #include "xfs_mount.h"
  47. #include "xfs_alloc_btree.h"
  48. #include "xfs_bmap_btree.h"
  49. #include "xfs_ialloc_btree.h"
  50. #include "xfs_btree.h"
  51. #include "xfs_ialloc.h"
  52. #include "xfs_attr_sf.h"
  53. #include "xfs_dir_sf.h"
  54. #include "xfs_dir2_sf.h"
  55. #include "xfs_dinode.h"
  56. #include "xfs_inode.h"
  57. #include "xfs_alloc.h"
  58. #include "xfs_bmap.h"
  59. #include "xfs_bit.h"
  60. #include "xfs_rtalloc.h"
  61. #include "xfs_fsops.h"
  62. #include "xfs_error.h"
  63. #include "xfs_rw.h"
  64. #include "xfs_inode_item.h"
  65. #include "xfs_trans_space.h"
  66. /*
  67. * Prototypes for internal functions.
  68. */
  69. STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  70. xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
  71. STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
  72. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
  73. STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  74. xfs_extlen_t, int, xfs_rtblock_t *, int *);
  75. STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  76. xfs_rtblock_t, xfs_rtblock_t *);
  77. STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  78. xfs_rtblock_t, xfs_rtblock_t *);
  79. STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
  80. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
  81. STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  82. xfs_extlen_t, int);
  83. STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  84. xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
  85. /*
  86. * Internal functions.
  87. */
  88. /*
  89. * xfs_lowbit32: get low bit set out of 32-bit argument, -1 if none set.
  90. */
  91. STATIC int
  92. xfs_lowbit32(
  93. __uint32_t v)
  94. {
  95. if (v)
  96. return ffs(v) - 1;
  97. return -1;
  98. }
  99. /*
  100. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  101. */
  102. STATIC int /* error */
  103. xfs_growfs_rt_alloc(
  104. xfs_mount_t *mp, /* file system mount point */
  105. xfs_extlen_t oblocks, /* old count of blocks */
  106. xfs_extlen_t nblocks, /* new count of blocks */
  107. xfs_ino_t ino) /* inode number (bitmap/summary) */
  108. {
  109. xfs_fileoff_t bno; /* block number in file */
  110. xfs_buf_t *bp; /* temporary buffer for zeroing */
  111. int cancelflags; /* flags for xfs_trans_cancel */
  112. int committed; /* transaction committed flag */
  113. xfs_daddr_t d; /* disk block address */
  114. int error; /* error return value */
  115. xfs_fsblock_t firstblock; /* first block allocated in xaction */
  116. xfs_bmap_free_t flist; /* list of freed blocks */
  117. xfs_fsblock_t fsbno; /* filesystem block for bno */
  118. xfs_inode_t *ip; /* pointer to incore inode */
  119. xfs_bmbt_irec_t map; /* block map output */
  120. int nmap; /* number of block maps */
  121. int resblks; /* space reservation */
  122. xfs_trans_t *tp; /* transaction pointer */
  123. /*
  124. * Allocate space to the file, as necessary.
  125. */
  126. while (oblocks < nblocks) {
  127. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  128. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  129. cancelflags = 0;
  130. /*
  131. * Reserve space & log for one extent added to the file.
  132. */
  133. if ((error = xfs_trans_reserve(tp, resblks,
  134. XFS_GROWRTALLOC_LOG_RES(mp), 0,
  135. XFS_TRANS_PERM_LOG_RES,
  136. XFS_DEFAULT_PERM_LOG_COUNT)))
  137. goto error_exit;
  138. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  139. /*
  140. * Lock the inode.
  141. */
  142. if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL, &ip)))
  143. goto error_exit;
  144. XFS_BMAP_INIT(&flist, &firstblock);
  145. /*
  146. * Allocate blocks to the bitmap file.
  147. */
  148. nmap = 1;
  149. cancelflags |= XFS_TRANS_ABORT;
  150. error = xfs_bmapi(tp, ip, oblocks, nblocks - oblocks,
  151. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA, &firstblock,
  152. resblks, &map, &nmap, &flist);
  153. if (!error && nmap < 1)
  154. error = XFS_ERROR(ENOSPC);
  155. if (error)
  156. goto error_exit;
  157. /*
  158. * Free any blocks freed up in the transaction, then commit.
  159. */
  160. error = xfs_bmap_finish(&tp, &flist, firstblock, &committed);
  161. if (error)
  162. goto error_exit;
  163. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  164. /*
  165. * Now we need to clear the allocated blocks.
  166. * Do this one block per transaction, to keep it simple.
  167. */
  168. cancelflags = 0;
  169. for (bno = map.br_startoff, fsbno = map.br_startblock;
  170. bno < map.br_startoff + map.br_blockcount;
  171. bno++, fsbno++) {
  172. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  173. /*
  174. * Reserve log for one block zeroing.
  175. */
  176. if ((error = xfs_trans_reserve(tp, 0,
  177. XFS_GROWRTZERO_LOG_RES(mp), 0, 0, 0)))
  178. goto error_exit;
  179. /*
  180. * Lock the bitmap inode.
  181. */
  182. if ((error = xfs_trans_iget(mp, tp, ino, 0, XFS_ILOCK_EXCL,
  183. &ip)))
  184. goto error_exit;
  185. /*
  186. * Get a buffer for the block.
  187. */
  188. d = XFS_FSB_TO_DADDR(mp, fsbno);
  189. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  190. mp->m_bsize, 0);
  191. if (bp == NULL) {
  192. error = XFS_ERROR(EIO);
  193. goto error_exit;
  194. }
  195. memset(XFS_BUF_PTR(bp), 0, mp->m_sb.sb_blocksize);
  196. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  197. /*
  198. * Commit the transaction.
  199. */
  200. xfs_trans_commit(tp, 0, NULL);
  201. }
  202. /*
  203. * Go on to the next extent, if any.
  204. */
  205. oblocks = map.br_startoff + map.br_blockcount;
  206. }
  207. return 0;
  208. error_exit:
  209. xfs_trans_cancel(tp, cancelflags);
  210. return error;
  211. }
  212. /*
  213. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  214. * bitmap block bbno. If we don't get maxlen then use prod to trim
  215. * the length, if given. Returns error; returns starting block in *rtblock.
  216. * The lengths are all in rtextents.
  217. */
  218. STATIC int /* error */
  219. xfs_rtallocate_extent_block(
  220. xfs_mount_t *mp, /* file system mount point */
  221. xfs_trans_t *tp, /* transaction pointer */
  222. xfs_rtblock_t bbno, /* bitmap block number */
  223. xfs_extlen_t minlen, /* minimum length to allocate */
  224. xfs_extlen_t maxlen, /* maximum length to allocate */
  225. xfs_extlen_t *len, /* out: actual length allocated */
  226. xfs_rtblock_t *nextp, /* out: next block to try */
  227. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  228. xfs_fsblock_t *rsb, /* in/out: summary block number */
  229. xfs_extlen_t prod, /* extent product factor */
  230. xfs_rtblock_t *rtblock) /* out: start block allocated */
  231. {
  232. xfs_rtblock_t besti; /* best rtblock found so far */
  233. xfs_rtblock_t bestlen; /* best length found so far */
  234. xfs_rtblock_t end; /* last rtblock in chunk */
  235. int error; /* error value */
  236. xfs_rtblock_t i; /* current rtblock trying */
  237. xfs_rtblock_t next; /* next rtblock to try */
  238. int stat; /* status from internal calls */
  239. /*
  240. * Loop over all the extents starting in this bitmap block,
  241. * looking for one that's long enough.
  242. */
  243. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  244. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  245. i <= end;
  246. i++) {
  247. /*
  248. * See if there's a free extent of maxlen starting at i.
  249. * If it's not so then next will contain the first non-free.
  250. */
  251. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  252. if (error) {
  253. return error;
  254. }
  255. if (stat) {
  256. /*
  257. * i for maxlen is all free, allocate and return that.
  258. */
  259. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  260. rsb);
  261. if (error) {
  262. return error;
  263. }
  264. *len = maxlen;
  265. *rtblock = i;
  266. return 0;
  267. }
  268. /*
  269. * In the case where we have a variable-sized allocation
  270. * request, figure out how big this free piece is,
  271. * and if it's big enough for the minimum, and the best
  272. * so far, remember it.
  273. */
  274. if (minlen < maxlen) {
  275. xfs_rtblock_t thislen; /* this extent size */
  276. thislen = next - i;
  277. if (thislen >= minlen && thislen > bestlen) {
  278. besti = i;
  279. bestlen = thislen;
  280. }
  281. }
  282. /*
  283. * If not done yet, find the start of the next free space.
  284. */
  285. if (next < end) {
  286. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  287. if (error) {
  288. return error;
  289. }
  290. } else
  291. break;
  292. }
  293. /*
  294. * Searched the whole thing & didn't find a maxlen free extent.
  295. */
  296. if (minlen < maxlen && besti != -1) {
  297. xfs_extlen_t p; /* amount to trim length by */
  298. /*
  299. * If size should be a multiple of prod, make that so.
  300. */
  301. if (prod > 1 && (p = do_mod(bestlen, prod)))
  302. bestlen -= p;
  303. /*
  304. * Allocate besti for bestlen & return that.
  305. */
  306. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  307. if (error) {
  308. return error;
  309. }
  310. *len = bestlen;
  311. *rtblock = besti;
  312. return 0;
  313. }
  314. /*
  315. * Allocation failed. Set *nextp to the next block to try.
  316. */
  317. *nextp = next;
  318. *rtblock = NULLRTBLOCK;
  319. return 0;
  320. }
  321. /*
  322. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  323. * bno. If we don't get maxlen then use prod to trim the length, if given.
  324. * Returns error; returns starting block in *rtblock.
  325. * The lengths are all in rtextents.
  326. */
  327. STATIC int /* error */
  328. xfs_rtallocate_extent_exact(
  329. xfs_mount_t *mp, /* file system mount point */
  330. xfs_trans_t *tp, /* transaction pointer */
  331. xfs_rtblock_t bno, /* starting block number to allocate */
  332. xfs_extlen_t minlen, /* minimum length to allocate */
  333. xfs_extlen_t maxlen, /* maximum length to allocate */
  334. xfs_extlen_t *len, /* out: actual length allocated */
  335. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  336. xfs_fsblock_t *rsb, /* in/out: summary block number */
  337. xfs_extlen_t prod, /* extent product factor */
  338. xfs_rtblock_t *rtblock) /* out: start block allocated */
  339. {
  340. int error; /* error value */
  341. xfs_extlen_t i; /* extent length trimmed due to prod */
  342. int isfree; /* extent is free */
  343. xfs_rtblock_t next; /* next block to try (dummy) */
  344. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  345. /*
  346. * Check if the range in question (for maxlen) is free.
  347. */
  348. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  349. if (error) {
  350. return error;
  351. }
  352. if (isfree) {
  353. /*
  354. * If it is, allocate it and return success.
  355. */
  356. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  357. if (error) {
  358. return error;
  359. }
  360. *len = maxlen;
  361. *rtblock = bno;
  362. return 0;
  363. }
  364. /*
  365. * If not, allocate what there is, if it's at least minlen.
  366. */
  367. maxlen = next - bno;
  368. if (maxlen < minlen) {
  369. /*
  370. * Failed, return failure status.
  371. */
  372. *rtblock = NULLRTBLOCK;
  373. return 0;
  374. }
  375. /*
  376. * Trim off tail of extent, if prod is specified.
  377. */
  378. if (prod > 1 && (i = maxlen % prod)) {
  379. maxlen -= i;
  380. if (maxlen < minlen) {
  381. /*
  382. * Now we can't do it, return failure status.
  383. */
  384. *rtblock = NULLRTBLOCK;
  385. return 0;
  386. }
  387. }
  388. /*
  389. * Allocate what we can and return it.
  390. */
  391. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  392. if (error) {
  393. return error;
  394. }
  395. *len = maxlen;
  396. *rtblock = bno;
  397. return 0;
  398. }
  399. /*
  400. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  401. * to bno as possible. If we don't get maxlen then use prod to trim
  402. * the length, if given. The lengths are all in rtextents.
  403. */
  404. STATIC int /* error */
  405. xfs_rtallocate_extent_near(
  406. xfs_mount_t *mp, /* file system mount point */
  407. xfs_trans_t *tp, /* transaction pointer */
  408. xfs_rtblock_t bno, /* starting block number to allocate */
  409. xfs_extlen_t minlen, /* minimum length to allocate */
  410. xfs_extlen_t maxlen, /* maximum length to allocate */
  411. xfs_extlen_t *len, /* out: actual length allocated */
  412. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  413. xfs_fsblock_t *rsb, /* in/out: summary block number */
  414. xfs_extlen_t prod, /* extent product factor */
  415. xfs_rtblock_t *rtblock) /* out: start block allocated */
  416. {
  417. int any; /* any useful extents from summary */
  418. xfs_rtblock_t bbno; /* bitmap block number */
  419. int error; /* error value */
  420. int i; /* bitmap block offset (loop control) */
  421. int j; /* secondary loop control */
  422. int log2len; /* log2 of minlen */
  423. xfs_rtblock_t n; /* next block to try */
  424. xfs_rtblock_t r; /* result block */
  425. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  426. /*
  427. * If the block number given is off the end, silently set it to
  428. * the last block.
  429. */
  430. if (bno >= mp->m_sb.sb_rextents)
  431. bno = mp->m_sb.sb_rextents - 1;
  432. /*
  433. * Try the exact allocation first.
  434. */
  435. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  436. rbpp, rsb, prod, &r);
  437. if (error) {
  438. return error;
  439. }
  440. /*
  441. * If the exact allocation worked, return that.
  442. */
  443. if (r != NULLRTBLOCK) {
  444. *rtblock = r;
  445. return 0;
  446. }
  447. bbno = XFS_BITTOBLOCK(mp, bno);
  448. i = 0;
  449. log2len = xfs_highbit32(minlen);
  450. /*
  451. * Loop over all bitmap blocks (bbno + i is current block).
  452. */
  453. for (;;) {
  454. /*
  455. * Get summary information of extents of all useful levels
  456. * starting in this bitmap block.
  457. */
  458. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  459. bbno + i, rbpp, rsb, &any);
  460. if (error) {
  461. return error;
  462. }
  463. /*
  464. * If there are any useful extents starting here, try
  465. * allocating one.
  466. */
  467. if (any) {
  468. /*
  469. * On the positive side of the starting location.
  470. */
  471. if (i >= 0) {
  472. /*
  473. * Try to allocate an extent starting in
  474. * this block.
  475. */
  476. error = xfs_rtallocate_extent_block(mp, tp,
  477. bbno + i, minlen, maxlen, len, &n, rbpp,
  478. rsb, prod, &r);
  479. if (error) {
  480. return error;
  481. }
  482. /*
  483. * If it worked, return it.
  484. */
  485. if (r != NULLRTBLOCK) {
  486. *rtblock = r;
  487. return 0;
  488. }
  489. }
  490. /*
  491. * On the negative side of the starting location.
  492. */
  493. else { /* i < 0 */
  494. /*
  495. * Loop backwards through the bitmap blocks from
  496. * the starting point-1 up to where we are now.
  497. * There should be an extent which ends in this
  498. * bitmap block and is long enough.
  499. */
  500. for (j = -1; j > i; j--) {
  501. /*
  502. * Grab the summary information for
  503. * this bitmap block.
  504. */
  505. error = xfs_rtany_summary(mp, tp,
  506. log2len, mp->m_rsumlevels - 1,
  507. bbno + j, rbpp, rsb, &any);
  508. if (error) {
  509. return error;
  510. }
  511. /*
  512. * If there's no extent given in the
  513. * summary that means the extent we
  514. * found must carry over from an
  515. * earlier block. If there is an
  516. * extent given, we've already tried
  517. * that allocation, don't do it again.
  518. */
  519. if (any)
  520. continue;
  521. error = xfs_rtallocate_extent_block(mp,
  522. tp, bbno + j, minlen, maxlen,
  523. len, &n, rbpp, rsb, prod, &r);
  524. if (error) {
  525. return error;
  526. }
  527. /*
  528. * If it works, return the extent.
  529. */
  530. if (r != NULLRTBLOCK) {
  531. *rtblock = r;
  532. return 0;
  533. }
  534. }
  535. /*
  536. * There weren't intervening bitmap blocks
  537. * with a long enough extent, or the
  538. * allocation didn't work for some reason
  539. * (i.e. it's a little * too short).
  540. * Try to allocate from the summary block
  541. * that we found.
  542. */
  543. error = xfs_rtallocate_extent_block(mp, tp,
  544. bbno + i, minlen, maxlen, len, &n, rbpp,
  545. rsb, prod, &r);
  546. if (error) {
  547. return error;
  548. }
  549. /*
  550. * If it works, return the extent.
  551. */
  552. if (r != NULLRTBLOCK) {
  553. *rtblock = r;
  554. return 0;
  555. }
  556. }
  557. }
  558. /*
  559. * Loop control. If we were on the positive side, and there's
  560. * still more blocks on the negative side, go there.
  561. */
  562. if (i > 0 && (int)bbno - i >= 0)
  563. i = -i;
  564. /*
  565. * If positive, and no more negative, but there are more
  566. * positive, go there.
  567. */
  568. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  569. i++;
  570. /*
  571. * If negative or 0 (just started), and there are positive
  572. * blocks to go, go there. The 0 case moves to block 1.
  573. */
  574. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  575. i = 1 - i;
  576. /*
  577. * If negative or 0 and there are more negative blocks,
  578. * go there.
  579. */
  580. else if (i <= 0 && (int)bbno + i > 0)
  581. i--;
  582. /*
  583. * Must be done. Return failure.
  584. */
  585. else
  586. break;
  587. }
  588. *rtblock = NULLRTBLOCK;
  589. return 0;
  590. }
  591. /*
  592. * Allocate an extent of length minlen<=len<=maxlen, with no position
  593. * specified. If we don't get maxlen then use prod to trim
  594. * the length, if given. The lengths are all in rtextents.
  595. */
  596. STATIC int /* error */
  597. xfs_rtallocate_extent_size(
  598. xfs_mount_t *mp, /* file system mount point */
  599. xfs_trans_t *tp, /* transaction pointer */
  600. xfs_extlen_t minlen, /* minimum length to allocate */
  601. xfs_extlen_t maxlen, /* maximum length to allocate */
  602. xfs_extlen_t *len, /* out: actual length allocated */
  603. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  604. xfs_fsblock_t *rsb, /* in/out: summary block number */
  605. xfs_extlen_t prod, /* extent product factor */
  606. xfs_rtblock_t *rtblock) /* out: start block allocated */
  607. {
  608. int error; /* error value */
  609. int i; /* bitmap block number */
  610. int l; /* level number (loop control) */
  611. xfs_rtblock_t n; /* next block to be tried */
  612. xfs_rtblock_t r; /* result block number */
  613. xfs_suminfo_t sum; /* summary information for extents */
  614. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  615. /*
  616. * Loop over all the levels starting with maxlen.
  617. * At each level, look at all the bitmap blocks, to see if there
  618. * are extents starting there that are long enough (>= maxlen).
  619. * Note, only on the initial level can the allocation fail if
  620. * the summary says there's an extent.
  621. */
  622. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  623. /*
  624. * Loop over all the bitmap blocks.
  625. */
  626. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  627. /*
  628. * Get the summary for this level/block.
  629. */
  630. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  631. &sum);
  632. if (error) {
  633. return error;
  634. }
  635. /*
  636. * Nothing there, on to the next block.
  637. */
  638. if (!sum)
  639. continue;
  640. /*
  641. * Try allocating the extent.
  642. */
  643. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  644. maxlen, len, &n, rbpp, rsb, prod, &r);
  645. if (error) {
  646. return error;
  647. }
  648. /*
  649. * If it worked, return that.
  650. */
  651. if (r != NULLRTBLOCK) {
  652. *rtblock = r;
  653. return 0;
  654. }
  655. /*
  656. * If the "next block to try" returned from the
  657. * allocator is beyond the next bitmap block,
  658. * skip to that bitmap block.
  659. */
  660. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  661. i = XFS_BITTOBLOCK(mp, n) - 1;
  662. }
  663. }
  664. /*
  665. * Didn't find any maxlen blocks. Try smaller ones, unless
  666. * we're asking for a fixed size extent.
  667. */
  668. if (minlen > --maxlen) {
  669. *rtblock = NULLRTBLOCK;
  670. return 0;
  671. }
  672. /*
  673. * Loop over sizes, from maxlen down to minlen.
  674. * This time, when we do the allocations, allow smaller ones
  675. * to succeed.
  676. */
  677. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  678. /*
  679. * Loop over all the bitmap blocks, try an allocation
  680. * starting in that block.
  681. */
  682. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  683. /*
  684. * Get the summary information for this level/block.
  685. */
  686. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  687. &sum);
  688. if (error) {
  689. return error;
  690. }
  691. /*
  692. * If nothing there, go on to next.
  693. */
  694. if (!sum)
  695. continue;
  696. /*
  697. * Try the allocation. Make sure the specified
  698. * minlen/maxlen are in the possible range for
  699. * this summary level.
  700. */
  701. error = xfs_rtallocate_extent_block(mp, tp, i,
  702. XFS_RTMAX(minlen, 1 << l),
  703. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  704. len, &n, rbpp, rsb, prod, &r);
  705. if (error) {
  706. return error;
  707. }
  708. /*
  709. * If it worked, return that extent.
  710. */
  711. if (r != NULLRTBLOCK) {
  712. *rtblock = r;
  713. return 0;
  714. }
  715. /*
  716. * If the "next block to try" returned from the
  717. * allocator is beyond the next bitmap block,
  718. * skip to that bitmap block.
  719. */
  720. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  721. i = XFS_BITTOBLOCK(mp, n) - 1;
  722. }
  723. }
  724. /*
  725. * Got nothing, return failure.
  726. */
  727. *rtblock = NULLRTBLOCK;
  728. return 0;
  729. }
  730. /*
  731. * Mark an extent specified by start and len allocated.
  732. * Updates all the summary information as well as the bitmap.
  733. */
  734. STATIC int /* error */
  735. xfs_rtallocate_range(
  736. xfs_mount_t *mp, /* file system mount point */
  737. xfs_trans_t *tp, /* transaction pointer */
  738. xfs_rtblock_t start, /* start block to allocate */
  739. xfs_extlen_t len, /* length to allocate */
  740. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  741. xfs_fsblock_t *rsb) /* in/out: summary block number */
  742. {
  743. xfs_rtblock_t end; /* end of the allocated extent */
  744. int error; /* error value */
  745. xfs_rtblock_t postblock; /* first block allocated > end */
  746. xfs_rtblock_t preblock; /* first block allocated < start */
  747. end = start + len - 1;
  748. /*
  749. * Assume we're allocating out of the middle of a free extent.
  750. * We need to find the beginning and end of the extent so we can
  751. * properly update the summary.
  752. */
  753. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  754. if (error) {
  755. return error;
  756. }
  757. /*
  758. * Find the next allocated block (end of free extent).
  759. */
  760. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  761. &postblock);
  762. if (error) {
  763. return error;
  764. }
  765. /*
  766. * Decrement the summary information corresponding to the entire
  767. * (old) free extent.
  768. */
  769. error = xfs_rtmodify_summary(mp, tp,
  770. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  771. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  772. if (error) {
  773. return error;
  774. }
  775. /*
  776. * If there are blocks not being allocated at the front of the
  777. * old extent, add summary data for them to be free.
  778. */
  779. if (preblock < start) {
  780. error = xfs_rtmodify_summary(mp, tp,
  781. XFS_RTBLOCKLOG(start - preblock),
  782. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  783. if (error) {
  784. return error;
  785. }
  786. }
  787. /*
  788. * If there are blocks not being allocated at the end of the
  789. * old extent, add summary data for them to be free.
  790. */
  791. if (postblock > end) {
  792. error = xfs_rtmodify_summary(mp, tp,
  793. XFS_RTBLOCKLOG(postblock - end),
  794. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  795. if (error) {
  796. return error;
  797. }
  798. }
  799. /*
  800. * Modify the bitmap to mark this extent allocated.
  801. */
  802. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  803. return error;
  804. }
  805. /*
  806. * Return whether there are any free extents in the size range given
  807. * by low and high, for the bitmap block bbno.
  808. */
  809. STATIC int /* error */
  810. xfs_rtany_summary(
  811. xfs_mount_t *mp, /* file system mount structure */
  812. xfs_trans_t *tp, /* transaction pointer */
  813. int low, /* low log2 extent size */
  814. int high, /* high log2 extent size */
  815. xfs_rtblock_t bbno, /* bitmap block number */
  816. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  817. xfs_fsblock_t *rsb, /* in/out: summary block number */
  818. int *stat) /* out: any good extents here? */
  819. {
  820. int error; /* error value */
  821. int log; /* loop counter, log2 of ext. size */
  822. xfs_suminfo_t sum; /* summary data */
  823. /*
  824. * Loop over logs of extent sizes. Order is irrelevant.
  825. */
  826. for (log = low; log <= high; log++) {
  827. /*
  828. * Get one summary datum.
  829. */
  830. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  831. if (error) {
  832. return error;
  833. }
  834. /*
  835. * If there are any, return success.
  836. */
  837. if (sum) {
  838. *stat = 1;
  839. return 0;
  840. }
  841. }
  842. /*
  843. * Found nothing, return failure.
  844. */
  845. *stat = 0;
  846. return 0;
  847. }
  848. /*
  849. * Get a buffer for the bitmap or summary file block specified.
  850. * The buffer is returned read and locked.
  851. */
  852. STATIC int /* error */
  853. xfs_rtbuf_get(
  854. xfs_mount_t *mp, /* file system mount structure */
  855. xfs_trans_t *tp, /* transaction pointer */
  856. xfs_rtblock_t block, /* block number in bitmap or summary */
  857. int issum, /* is summary not bitmap */
  858. xfs_buf_t **bpp) /* output: buffer for the block */
  859. {
  860. xfs_buf_t *bp; /* block buffer, result */
  861. xfs_daddr_t d; /* disk addr of block */
  862. int error; /* error value */
  863. xfs_fsblock_t fsb; /* fs block number for block */
  864. xfs_inode_t *ip; /* bitmap or summary inode */
  865. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  866. /*
  867. * Map from the file offset (block) and inode number to the
  868. * file system block.
  869. */
  870. error = xfs_bmapi_single(tp, ip, XFS_DATA_FORK, &fsb, block);
  871. if (error) {
  872. return error;
  873. }
  874. ASSERT(fsb != NULLFSBLOCK);
  875. /*
  876. * Convert to disk address for buffer cache.
  877. */
  878. d = XFS_FSB_TO_DADDR(mp, fsb);
  879. /*
  880. * Read the buffer.
  881. */
  882. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  883. mp->m_bsize, 0, &bp);
  884. if (error) {
  885. return error;
  886. }
  887. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  888. *bpp = bp;
  889. return 0;
  890. }
  891. #ifdef DEBUG
  892. /*
  893. * Check that the given extent (block range) is allocated already.
  894. */
  895. STATIC int /* error */
  896. xfs_rtcheck_alloc_range(
  897. xfs_mount_t *mp, /* file system mount point */
  898. xfs_trans_t *tp, /* transaction pointer */
  899. xfs_rtblock_t bno, /* starting block number of extent */
  900. xfs_extlen_t len, /* length of extent */
  901. int *stat) /* out: 1 for allocated, 0 for not */
  902. {
  903. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  904. return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
  905. }
  906. #endif
  907. #ifdef DEBUG
  908. /*
  909. * Check whether the given block in the bitmap has the given value.
  910. */
  911. STATIC int /* 1 for matches, 0 for not */
  912. xfs_rtcheck_bit(
  913. xfs_mount_t *mp, /* file system mount structure */
  914. xfs_trans_t *tp, /* transaction pointer */
  915. xfs_rtblock_t start, /* bit (block) to check */
  916. int val) /* 1 for free, 0 for allocated */
  917. {
  918. int bit; /* bit number in the word */
  919. xfs_rtblock_t block; /* bitmap block number */
  920. xfs_buf_t *bp; /* buf for the block */
  921. xfs_rtword_t *bufp; /* pointer into the buffer */
  922. /* REFERENCED */
  923. int error; /* error value */
  924. xfs_rtword_t wdiff; /* difference between bit & expected */
  925. int word; /* word number in the buffer */
  926. xfs_rtword_t wval; /* word value from buffer */
  927. block = XFS_BITTOBLOCK(mp, start);
  928. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  929. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  930. word = XFS_BITTOWORD(mp, start);
  931. bit = (int)(start & (XFS_NBWORD - 1));
  932. wval = bufp[word];
  933. xfs_trans_brelse(tp, bp);
  934. wdiff = (wval ^ -val) & ((xfs_rtword_t)1 << bit);
  935. return !wdiff;
  936. }
  937. #endif /* DEBUG */
  938. #if 0
  939. /*
  940. * Check that the given extent (block range) is free already.
  941. */
  942. STATIC int /* error */
  943. xfs_rtcheck_free_range(
  944. xfs_mount_t *mp, /* file system mount point */
  945. xfs_trans_t *tp, /* transaction pointer */
  946. xfs_rtblock_t bno, /* starting block number of extent */
  947. xfs_extlen_t len, /* length of extent */
  948. int *stat) /* out: 1 for free, 0 for not */
  949. {
  950. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  951. return xfs_rtcheck_range(mp, tp, bno, len, 1, &new, stat);
  952. }
  953. #endif
  954. /*
  955. * Check that the given range is either all allocated (val = 0) or
  956. * all free (val = 1).
  957. */
  958. STATIC int /* error */
  959. xfs_rtcheck_range(
  960. xfs_mount_t *mp, /* file system mount point */
  961. xfs_trans_t *tp, /* transaction pointer */
  962. xfs_rtblock_t start, /* starting block number of extent */
  963. xfs_extlen_t len, /* length of extent */
  964. int val, /* 1 for free, 0 for allocated */
  965. xfs_rtblock_t *new, /* out: first block not matching */
  966. int *stat) /* out: 1 for matches, 0 for not */
  967. {
  968. xfs_rtword_t *b; /* current word in buffer */
  969. int bit; /* bit number in the word */
  970. xfs_rtblock_t block; /* bitmap block number */
  971. xfs_buf_t *bp; /* buf for the block */
  972. xfs_rtword_t *bufp; /* starting word in buffer */
  973. int error; /* error value */
  974. xfs_rtblock_t i; /* current bit number rel. to start */
  975. xfs_rtblock_t lastbit; /* last useful bit in word */
  976. xfs_rtword_t mask; /* mask of relevant bits for value */
  977. xfs_rtword_t wdiff; /* difference from wanted value */
  978. int word; /* word number in the buffer */
  979. /*
  980. * Compute starting bitmap block number
  981. */
  982. block = XFS_BITTOBLOCK(mp, start);
  983. /*
  984. * Read the bitmap block.
  985. */
  986. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  987. if (error) {
  988. return error;
  989. }
  990. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  991. /*
  992. * Compute the starting word's address, and starting bit.
  993. */
  994. word = XFS_BITTOWORD(mp, start);
  995. b = &bufp[word];
  996. bit = (int)(start & (XFS_NBWORD - 1));
  997. /*
  998. * 0 (allocated) => all zero's; 1 (free) => all one's.
  999. */
  1000. val = -val;
  1001. /*
  1002. * If not starting on a word boundary, deal with the first
  1003. * (partial) word.
  1004. */
  1005. if (bit) {
  1006. /*
  1007. * Compute first bit not examined.
  1008. */
  1009. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1010. /*
  1011. * Mask of relevant bits.
  1012. */
  1013. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1014. /*
  1015. * Compute difference between actual and desired value.
  1016. */
  1017. if ((wdiff = (*b ^ val) & mask)) {
  1018. /*
  1019. * Different, compute first wrong bit and return.
  1020. */
  1021. xfs_trans_brelse(tp, bp);
  1022. i = XFS_RTLOBIT(wdiff) - bit;
  1023. *new = start + i;
  1024. *stat = 0;
  1025. return 0;
  1026. }
  1027. i = lastbit - bit;
  1028. /*
  1029. * Go on to next block if that's where the next word is
  1030. * and we need the next word.
  1031. */
  1032. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1033. /*
  1034. * If done with this block, get the next one.
  1035. */
  1036. xfs_trans_brelse(tp, bp);
  1037. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1038. if (error) {
  1039. return error;
  1040. }
  1041. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1042. word = 0;
  1043. } else {
  1044. /*
  1045. * Go on to the next word in the buffer.
  1046. */
  1047. b++;
  1048. }
  1049. } else {
  1050. /*
  1051. * Starting on a word boundary, no partial word.
  1052. */
  1053. i = 0;
  1054. }
  1055. /*
  1056. * Loop over whole words in buffers. When we use up one buffer
  1057. * we move on to the next one.
  1058. */
  1059. while (len - i >= XFS_NBWORD) {
  1060. /*
  1061. * Compute difference between actual and desired value.
  1062. */
  1063. if ((wdiff = *b ^ val)) {
  1064. /*
  1065. * Different, compute first wrong bit and return.
  1066. */
  1067. xfs_trans_brelse(tp, bp);
  1068. i += XFS_RTLOBIT(wdiff);
  1069. *new = start + i;
  1070. *stat = 0;
  1071. return 0;
  1072. }
  1073. i += XFS_NBWORD;
  1074. /*
  1075. * Go on to next block if that's where the next word is
  1076. * and we need the next word.
  1077. */
  1078. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1079. /*
  1080. * If done with this block, get the next one.
  1081. */
  1082. xfs_trans_brelse(tp, bp);
  1083. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1084. if (error) {
  1085. return error;
  1086. }
  1087. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1088. word = 0;
  1089. } else {
  1090. /*
  1091. * Go on to the next word in the buffer.
  1092. */
  1093. b++;
  1094. }
  1095. }
  1096. /*
  1097. * If not ending on a word boundary, deal with the last
  1098. * (partial) word.
  1099. */
  1100. if ((lastbit = len - i)) {
  1101. /*
  1102. * Mask of relevant bits.
  1103. */
  1104. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1105. /*
  1106. * Compute difference between actual and desired value.
  1107. */
  1108. if ((wdiff = (*b ^ val) & mask)) {
  1109. /*
  1110. * Different, compute first wrong bit and return.
  1111. */
  1112. xfs_trans_brelse(tp, bp);
  1113. i += XFS_RTLOBIT(wdiff);
  1114. *new = start + i;
  1115. *stat = 0;
  1116. return 0;
  1117. } else
  1118. i = len;
  1119. }
  1120. /*
  1121. * Successful, return.
  1122. */
  1123. xfs_trans_brelse(tp, bp);
  1124. *new = start + i;
  1125. *stat = 1;
  1126. return 0;
  1127. }
  1128. /*
  1129. * Copy and transform the summary file, given the old and new
  1130. * parameters in the mount structures.
  1131. */
  1132. STATIC int /* error */
  1133. xfs_rtcopy_summary(
  1134. xfs_mount_t *omp, /* old file system mount point */
  1135. xfs_mount_t *nmp, /* new file system mount point */
  1136. xfs_trans_t *tp) /* transaction pointer */
  1137. {
  1138. xfs_rtblock_t bbno; /* bitmap block number */
  1139. xfs_buf_t *bp; /* summary buffer */
  1140. int error; /* error return value */
  1141. int log; /* summary level number (log length) */
  1142. xfs_suminfo_t sum; /* summary data */
  1143. xfs_fsblock_t sumbno; /* summary block number */
  1144. bp = NULL;
  1145. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  1146. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  1147. (xfs_srtblock_t)bbno >= 0;
  1148. bbno--) {
  1149. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  1150. &sumbno, &sum);
  1151. if (error)
  1152. return error;
  1153. if (sum == 0)
  1154. continue;
  1155. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  1156. &bp, &sumbno);
  1157. if (error)
  1158. return error;
  1159. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  1160. &bp, &sumbno);
  1161. if (error)
  1162. return error;
  1163. ASSERT(sum > 0);
  1164. }
  1165. }
  1166. return 0;
  1167. }
  1168. /*
  1169. * Searching backward from start to limit, find the first block whose
  1170. * allocated/free state is different from start's.
  1171. */
  1172. STATIC int /* error */
  1173. xfs_rtfind_back(
  1174. xfs_mount_t *mp, /* file system mount point */
  1175. xfs_trans_t *tp, /* transaction pointer */
  1176. xfs_rtblock_t start, /* starting block to look at */
  1177. xfs_rtblock_t limit, /* last block to look at */
  1178. xfs_rtblock_t *rtblock) /* out: start block found */
  1179. {
  1180. xfs_rtword_t *b; /* current word in buffer */
  1181. int bit; /* bit number in the word */
  1182. xfs_rtblock_t block; /* bitmap block number */
  1183. xfs_buf_t *bp; /* buf for the block */
  1184. xfs_rtword_t *bufp; /* starting word in buffer */
  1185. int error; /* error value */
  1186. xfs_rtblock_t firstbit; /* first useful bit in the word */
  1187. xfs_rtblock_t i; /* current bit number rel. to start */
  1188. xfs_rtblock_t len; /* length of inspected area */
  1189. xfs_rtword_t mask; /* mask of relevant bits for value */
  1190. xfs_rtword_t want; /* mask for "good" values */
  1191. xfs_rtword_t wdiff; /* difference from wanted value */
  1192. int word; /* word number in the buffer */
  1193. /*
  1194. * Compute and read in starting bitmap block for starting block.
  1195. */
  1196. block = XFS_BITTOBLOCK(mp, start);
  1197. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1198. if (error) {
  1199. return error;
  1200. }
  1201. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1202. /*
  1203. * Get the first word's index & point to it.
  1204. */
  1205. word = XFS_BITTOWORD(mp, start);
  1206. b = &bufp[word];
  1207. bit = (int)(start & (XFS_NBWORD - 1));
  1208. len = start - limit + 1;
  1209. /*
  1210. * Compute match value, based on the bit at start: if 1 (free)
  1211. * then all-ones, else all-zeroes.
  1212. */
  1213. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1214. /*
  1215. * If the starting position is not word-aligned, deal with the
  1216. * partial word.
  1217. */
  1218. if (bit < XFS_NBWORD - 1) {
  1219. /*
  1220. * Calculate first (leftmost) bit number to look at,
  1221. * and mask for all the relevant bits in this word.
  1222. */
  1223. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  1224. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  1225. firstbit;
  1226. /*
  1227. * Calculate the difference between the value there
  1228. * and what we're looking for.
  1229. */
  1230. if ((wdiff = (*b ^ want) & mask)) {
  1231. /*
  1232. * Different. Mark where we are and return.
  1233. */
  1234. xfs_trans_brelse(tp, bp);
  1235. i = bit - XFS_RTHIBIT(wdiff);
  1236. *rtblock = start - i + 1;
  1237. return 0;
  1238. }
  1239. i = bit - firstbit + 1;
  1240. /*
  1241. * Go on to previous block if that's where the previous word is
  1242. * and we need the previous word.
  1243. */
  1244. if (--word == -1 && i < len) {
  1245. /*
  1246. * If done with this block, get the previous one.
  1247. */
  1248. xfs_trans_brelse(tp, bp);
  1249. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1250. if (error) {
  1251. return error;
  1252. }
  1253. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1254. word = XFS_BLOCKWMASK(mp);
  1255. b = &bufp[word];
  1256. } else {
  1257. /*
  1258. * Go on to the previous word in the buffer.
  1259. */
  1260. b--;
  1261. }
  1262. } else {
  1263. /*
  1264. * Starting on a word boundary, no partial word.
  1265. */
  1266. i = 0;
  1267. }
  1268. /*
  1269. * Loop over whole words in buffers. When we use up one buffer
  1270. * we move on to the previous one.
  1271. */
  1272. while (len - i >= XFS_NBWORD) {
  1273. /*
  1274. * Compute difference between actual and desired value.
  1275. */
  1276. if ((wdiff = *b ^ want)) {
  1277. /*
  1278. * Different, mark where we are and return.
  1279. */
  1280. xfs_trans_brelse(tp, bp);
  1281. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1282. *rtblock = start - i + 1;
  1283. return 0;
  1284. }
  1285. i += XFS_NBWORD;
  1286. /*
  1287. * Go on to previous block if that's where the previous word is
  1288. * and we need the previous word.
  1289. */
  1290. if (--word == -1 && i < len) {
  1291. /*
  1292. * If done with this block, get the previous one.
  1293. */
  1294. xfs_trans_brelse(tp, bp);
  1295. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1296. if (error) {
  1297. return error;
  1298. }
  1299. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1300. word = XFS_BLOCKWMASK(mp);
  1301. b = &bufp[word];
  1302. } else {
  1303. /*
  1304. * Go on to the previous word in the buffer.
  1305. */
  1306. b--;
  1307. }
  1308. }
  1309. /*
  1310. * If not ending on a word boundary, deal with the last
  1311. * (partial) word.
  1312. */
  1313. if (len - i) {
  1314. /*
  1315. * Calculate first (leftmost) bit number to look at,
  1316. * and mask for all the relevant bits in this word.
  1317. */
  1318. firstbit = XFS_NBWORD - (len - i);
  1319. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  1320. /*
  1321. * Compute difference between actual and desired value.
  1322. */
  1323. if ((wdiff = (*b ^ want) & mask)) {
  1324. /*
  1325. * Different, mark where we are and return.
  1326. */
  1327. xfs_trans_brelse(tp, bp);
  1328. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1329. *rtblock = start - i + 1;
  1330. return 0;
  1331. } else
  1332. i = len;
  1333. }
  1334. /*
  1335. * No match, return that we scanned the whole area.
  1336. */
  1337. xfs_trans_brelse(tp, bp);
  1338. *rtblock = start - i + 1;
  1339. return 0;
  1340. }
  1341. /*
  1342. * Searching forward from start to limit, find the first block whose
  1343. * allocated/free state is different from start's.
  1344. */
  1345. STATIC int /* error */
  1346. xfs_rtfind_forw(
  1347. xfs_mount_t *mp, /* file system mount point */
  1348. xfs_trans_t *tp, /* transaction pointer */
  1349. xfs_rtblock_t start, /* starting block to look at */
  1350. xfs_rtblock_t limit, /* last block to look at */
  1351. xfs_rtblock_t *rtblock) /* out: start block found */
  1352. {
  1353. xfs_rtword_t *b; /* current word in buffer */
  1354. int bit; /* bit number in the word */
  1355. xfs_rtblock_t block; /* bitmap block number */
  1356. xfs_buf_t *bp; /* buf for the block */
  1357. xfs_rtword_t *bufp; /* starting word in buffer */
  1358. int error; /* error value */
  1359. xfs_rtblock_t i; /* current bit number rel. to start */
  1360. xfs_rtblock_t lastbit; /* last useful bit in the word */
  1361. xfs_rtblock_t len; /* length of inspected area */
  1362. xfs_rtword_t mask; /* mask of relevant bits for value */
  1363. xfs_rtword_t want; /* mask for "good" values */
  1364. xfs_rtword_t wdiff; /* difference from wanted value */
  1365. int word; /* word number in the buffer */
  1366. /*
  1367. * Compute and read in starting bitmap block for starting block.
  1368. */
  1369. block = XFS_BITTOBLOCK(mp, start);
  1370. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1371. if (error) {
  1372. return error;
  1373. }
  1374. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1375. /*
  1376. * Get the first word's index & point to it.
  1377. */
  1378. word = XFS_BITTOWORD(mp, start);
  1379. b = &bufp[word];
  1380. bit = (int)(start & (XFS_NBWORD - 1));
  1381. len = limit - start + 1;
  1382. /*
  1383. * Compute match value, based on the bit at start: if 1 (free)
  1384. * then all-ones, else all-zeroes.
  1385. */
  1386. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1387. /*
  1388. * If the starting position is not word-aligned, deal with the
  1389. * partial word.
  1390. */
  1391. if (bit) {
  1392. /*
  1393. * Calculate last (rightmost) bit number to look at,
  1394. * and mask for all the relevant bits in this word.
  1395. */
  1396. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1397. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1398. /*
  1399. * Calculate the difference between the value there
  1400. * and what we're looking for.
  1401. */
  1402. if ((wdiff = (*b ^ want) & mask)) {
  1403. /*
  1404. * Different. Mark where we are and return.
  1405. */
  1406. xfs_trans_brelse(tp, bp);
  1407. i = XFS_RTLOBIT(wdiff) - bit;
  1408. *rtblock = start + i - 1;
  1409. return 0;
  1410. }
  1411. i = lastbit - bit;
  1412. /*
  1413. * Go on to next block if that's where the next word is
  1414. * and we need the next word.
  1415. */
  1416. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1417. /*
  1418. * If done with this block, get the previous one.
  1419. */
  1420. xfs_trans_brelse(tp, bp);
  1421. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1422. if (error) {
  1423. return error;
  1424. }
  1425. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1426. word = 0;
  1427. } else {
  1428. /*
  1429. * Go on to the previous word in the buffer.
  1430. */
  1431. b++;
  1432. }
  1433. } else {
  1434. /*
  1435. * Starting on a word boundary, no partial word.
  1436. */
  1437. i = 0;
  1438. }
  1439. /*
  1440. * Loop over whole words in buffers. When we use up one buffer
  1441. * we move on to the next one.
  1442. */
  1443. while (len - i >= XFS_NBWORD) {
  1444. /*
  1445. * Compute difference between actual and desired value.
  1446. */
  1447. if ((wdiff = *b ^ want)) {
  1448. /*
  1449. * Different, mark where we are and return.
  1450. */
  1451. xfs_trans_brelse(tp, bp);
  1452. i += XFS_RTLOBIT(wdiff);
  1453. *rtblock = start + i - 1;
  1454. return 0;
  1455. }
  1456. i += XFS_NBWORD;
  1457. /*
  1458. * Go on to next block if that's where the next word is
  1459. * and we need the next word.
  1460. */
  1461. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1462. /*
  1463. * If done with this block, get the next one.
  1464. */
  1465. xfs_trans_brelse(tp, bp);
  1466. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1467. if (error) {
  1468. return error;
  1469. }
  1470. b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1471. word = 0;
  1472. } else {
  1473. /*
  1474. * Go on to the next word in the buffer.
  1475. */
  1476. b++;
  1477. }
  1478. }
  1479. /*
  1480. * If not ending on a word boundary, deal with the last
  1481. * (partial) word.
  1482. */
  1483. if ((lastbit = len - i)) {
  1484. /*
  1485. * Calculate mask for all the relevant bits in this word.
  1486. */
  1487. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1488. /*
  1489. * Compute difference between actual and desired value.
  1490. */
  1491. if ((wdiff = (*b ^ want) & mask)) {
  1492. /*
  1493. * Different, mark where we are and return.
  1494. */
  1495. xfs_trans_brelse(tp, bp);
  1496. i += XFS_RTLOBIT(wdiff);
  1497. *rtblock = start + i - 1;
  1498. return 0;
  1499. } else
  1500. i = len;
  1501. }
  1502. /*
  1503. * No match, return that we scanned the whole area.
  1504. */
  1505. xfs_trans_brelse(tp, bp);
  1506. *rtblock = start + i - 1;
  1507. return 0;
  1508. }
  1509. /*
  1510. * Mark an extent specified by start and len freed.
  1511. * Updates all the summary information as well as the bitmap.
  1512. */
  1513. STATIC int /* error */
  1514. xfs_rtfree_range(
  1515. xfs_mount_t *mp, /* file system mount point */
  1516. xfs_trans_t *tp, /* transaction pointer */
  1517. xfs_rtblock_t start, /* starting block to free */
  1518. xfs_extlen_t len, /* length to free */
  1519. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1520. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1521. {
  1522. xfs_rtblock_t end; /* end of the freed extent */
  1523. int error; /* error value */
  1524. xfs_rtblock_t postblock; /* first block freed > end */
  1525. xfs_rtblock_t preblock; /* first block freed < start */
  1526. end = start + len - 1;
  1527. /*
  1528. * Modify the bitmap to mark this extent freed.
  1529. */
  1530. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  1531. if (error) {
  1532. return error;
  1533. }
  1534. /*
  1535. * Assume we're freeing out of the middle of an allocated extent.
  1536. * We need to find the beginning and end of the extent so we can
  1537. * properly update the summary.
  1538. */
  1539. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  1540. if (error) {
  1541. return error;
  1542. }
  1543. /*
  1544. * Find the next allocated block (end of allocated extent).
  1545. */
  1546. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  1547. &postblock);
  1548. /*
  1549. * If there are blocks not being freed at the front of the
  1550. * old extent, add summary data for them to be allocated.
  1551. */
  1552. if (preblock < start) {
  1553. error = xfs_rtmodify_summary(mp, tp,
  1554. XFS_RTBLOCKLOG(start - preblock),
  1555. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  1556. if (error) {
  1557. return error;
  1558. }
  1559. }
  1560. /*
  1561. * If there are blocks not being freed at the end of the
  1562. * old extent, add summary data for them to be allocated.
  1563. */
  1564. if (postblock > end) {
  1565. error = xfs_rtmodify_summary(mp, tp,
  1566. XFS_RTBLOCKLOG(postblock - end),
  1567. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  1568. if (error) {
  1569. return error;
  1570. }
  1571. }
  1572. /*
  1573. * Increment the summary information corresponding to the entire
  1574. * (new) free extent.
  1575. */
  1576. error = xfs_rtmodify_summary(mp, tp,
  1577. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  1578. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  1579. return error;
  1580. }
  1581. /*
  1582. * Read and return the summary information for a given extent size,
  1583. * bitmap block combination.
  1584. * Keeps track of a current summary block, so we don't keep reading
  1585. * it from the buffer cache.
  1586. */
  1587. STATIC int /* error */
  1588. xfs_rtget_summary(
  1589. xfs_mount_t *mp, /* file system mount structure */
  1590. xfs_trans_t *tp, /* transaction pointer */
  1591. int log, /* log2 of extent size */
  1592. xfs_rtblock_t bbno, /* bitmap block number */
  1593. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1594. xfs_fsblock_t *rsb, /* in/out: summary block number */
  1595. xfs_suminfo_t *sum) /* out: summary info for this block */
  1596. {
  1597. xfs_buf_t *bp; /* buffer for summary block */
  1598. int error; /* error value */
  1599. xfs_fsblock_t sb; /* summary fsblock */
  1600. int so; /* index into the summary file */
  1601. xfs_suminfo_t *sp; /* pointer to returned data */
  1602. /*
  1603. * Compute entry number in the summary file.
  1604. */
  1605. so = XFS_SUMOFFS(mp, log, bbno);
  1606. /*
  1607. * Compute the block number in the summary file.
  1608. */
  1609. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1610. /*
  1611. * If we have an old buffer, and the block number matches, use that.
  1612. */
  1613. if (rbpp && *rbpp && *rsb == sb)
  1614. bp = *rbpp;
  1615. /*
  1616. * Otherwise we have to get the buffer.
  1617. */
  1618. else {
  1619. /*
  1620. * If there was an old one, get rid of it first.
  1621. */
  1622. if (rbpp && *rbpp)
  1623. xfs_trans_brelse(tp, *rbpp);
  1624. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1625. if (error) {
  1626. return error;
  1627. }
  1628. /*
  1629. * Remember this buffer and block for the next call.
  1630. */
  1631. if (rbpp) {
  1632. *rbpp = bp;
  1633. *rsb = sb;
  1634. }
  1635. }
  1636. /*
  1637. * Point to the summary information & copy it out.
  1638. */
  1639. sp = XFS_SUMPTR(mp, bp, so);
  1640. *sum = *sp;
  1641. /*
  1642. * Drop the buffer if we're not asked to remember it.
  1643. */
  1644. if (!rbpp)
  1645. xfs_trans_brelse(tp, bp);
  1646. return 0;
  1647. }
  1648. /*
  1649. * Set the given range of bitmap bits to the given value.
  1650. * Do whatever I/O and logging is required.
  1651. */
  1652. STATIC int /* error */
  1653. xfs_rtmodify_range(
  1654. xfs_mount_t *mp, /* file system mount point */
  1655. xfs_trans_t *tp, /* transaction pointer */
  1656. xfs_rtblock_t start, /* starting block to modify */
  1657. xfs_extlen_t len, /* length of extent to modify */
  1658. int val) /* 1 for free, 0 for allocated */
  1659. {
  1660. xfs_rtword_t *b; /* current word in buffer */
  1661. int bit; /* bit number in the word */
  1662. xfs_rtblock_t block; /* bitmap block number */
  1663. xfs_buf_t *bp; /* buf for the block */
  1664. xfs_rtword_t *bufp; /* starting word in buffer */
  1665. int error; /* error value */
  1666. xfs_rtword_t *first; /* first used word in the buffer */
  1667. int i; /* current bit number rel. to start */
  1668. int lastbit; /* last useful bit in word */
  1669. xfs_rtword_t mask; /* mask o frelevant bits for value */
  1670. int word; /* word number in the buffer */
  1671. /*
  1672. * Compute starting bitmap block number.
  1673. */
  1674. block = XFS_BITTOBLOCK(mp, start);
  1675. /*
  1676. * Read the bitmap block, and point to its data.
  1677. */
  1678. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1679. if (error) {
  1680. return error;
  1681. }
  1682. bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1683. /*
  1684. * Compute the starting word's address, and starting bit.
  1685. */
  1686. word = XFS_BITTOWORD(mp, start);
  1687. first = b = &bufp[word];
  1688. bit = (int)(start & (XFS_NBWORD - 1));
  1689. /*
  1690. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  1691. */
  1692. val = -val;
  1693. /*
  1694. * If not starting on a word boundary, deal with the first
  1695. * (partial) word.
  1696. */
  1697. if (bit) {
  1698. /*
  1699. * Compute first bit not changed and mask of relevant bits.
  1700. */
  1701. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1702. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1703. /*
  1704. * Set/clear the active bits.
  1705. */
  1706. if (val)
  1707. *b |= mask;
  1708. else
  1709. *b &= ~mask;
  1710. i = lastbit - bit;
  1711. /*
  1712. * Go on to the next block if that's where the next word is
  1713. * and we need the next word.
  1714. */
  1715. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1716. /*
  1717. * Log the changed part of this block.
  1718. * Get the next one.
  1719. */
  1720. xfs_trans_log_buf(tp, bp,
  1721. (uint)((char *)first - (char *)bufp),
  1722. (uint)((char *)b - (char *)bufp));
  1723. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1724. if (error) {
  1725. return error;
  1726. }
  1727. first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1728. word = 0;
  1729. } else {
  1730. /*
  1731. * Go on to the next word in the buffer
  1732. */
  1733. b++;
  1734. }
  1735. } else {
  1736. /*
  1737. * Starting on a word boundary, no partial word.
  1738. */
  1739. i = 0;
  1740. }
  1741. /*
  1742. * Loop over whole words in buffers. When we use up one buffer
  1743. * we move on to the next one.
  1744. */
  1745. while (len - i >= XFS_NBWORD) {
  1746. /*
  1747. * Set the word value correctly.
  1748. */
  1749. *b = val;
  1750. i += XFS_NBWORD;
  1751. /*
  1752. * Go on to the next block if that's where the next word is
  1753. * and we need the next word.
  1754. */
  1755. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1756. /*
  1757. * Log the changed part of this block.
  1758. * Get the next one.
  1759. */
  1760. xfs_trans_log_buf(tp, bp,
  1761. (uint)((char *)first - (char *)bufp),
  1762. (uint)((char *)b - (char *)bufp));
  1763. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1764. if (error) {
  1765. return error;
  1766. }
  1767. first = b = bufp = (xfs_rtword_t *)XFS_BUF_PTR(bp);
  1768. word = 0;
  1769. } else {
  1770. /*
  1771. * Go on to the next word in the buffer
  1772. */
  1773. b++;
  1774. }
  1775. }
  1776. /*
  1777. * If not ending on a word boundary, deal with the last
  1778. * (partial) word.
  1779. */
  1780. if ((lastbit = len - i)) {
  1781. /*
  1782. * Compute a mask of relevant bits.
  1783. */
  1784. bit = 0;
  1785. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1786. /*
  1787. * Set/clear the active bits.
  1788. */
  1789. if (val)
  1790. *b |= mask;
  1791. else
  1792. *b &= ~mask;
  1793. b++;
  1794. }
  1795. /*
  1796. * Log any remaining changed bytes.
  1797. */
  1798. if (b > first)
  1799. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  1800. (uint)((char *)b - (char *)bufp - 1));
  1801. return 0;
  1802. }
  1803. /*
  1804. * Read and modify the summary information for a given extent size,
  1805. * bitmap block combination.
  1806. * Keeps track of a current summary block, so we don't keep reading
  1807. * it from the buffer cache.
  1808. */
  1809. STATIC int /* error */
  1810. xfs_rtmodify_summary(
  1811. xfs_mount_t *mp, /* file system mount point */
  1812. xfs_trans_t *tp, /* transaction pointer */
  1813. int log, /* log2 of extent size */
  1814. xfs_rtblock_t bbno, /* bitmap block number */
  1815. int delta, /* change to make to summary info */
  1816. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1817. xfs_fsblock_t *rsb)

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