xfs_btree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /*
  2. * Copyright (c) 2000-2002,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_error.h"
  40. /*
  41. * Cursor allocation zone.
  42. */
  43. kmem_zone_t *xfs_btree_cur_zone;
  44. /*
  45. * Btree magic numbers.
  46. */
  47. const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
  48. XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
  49. };
  50. /*
  51. * External routines.
  52. */
  53. #ifdef DEBUG
  54. /*
  55. * Debug routine: check that keys are in the right order.
  56. */
  57. void
  58. xfs_btree_check_key(
  59. xfs_btnum_t btnum, /* btree identifier */
  60. void *ak1, /* pointer to left (lower) key */
  61. void *ak2) /* pointer to right (higher) key */
  62. {
  63. switch (btnum) {
  64. case XFS_BTNUM_BNO: {
  65. xfs_alloc_key_t *k1;
  66. xfs_alloc_key_t *k2;
  67. k1 = ak1;
  68. k2 = ak2;
  69. ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
  70. break;
  71. }
  72. case XFS_BTNUM_CNT: {
  73. xfs_alloc_key_t *k1;
  74. xfs_alloc_key_t *k2;
  75. k1 = ak1;
  76. k2 = ak2;
  77. ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
  78. (k1->ar_blockcount == k2->ar_blockcount &&
  79. be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
  80. break;
  81. }
  82. case XFS_BTNUM_BMAP: {
  83. xfs_bmbt_key_t *k1;
  84. xfs_bmbt_key_t *k2;
  85. k1 = ak1;
  86. k2 = ak2;
  87. ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff));
  88. break;
  89. }
  90. case XFS_BTNUM_INO: {
  91. xfs_inobt_key_t *k1;
  92. xfs_inobt_key_t *k2;
  93. k1 = ak1;
  94. k2 = ak2;
  95. ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino));
  96. break;
  97. }
  98. default:
  99. ASSERT(0);
  100. }
  101. }
  102. /*
  103. * Debug routine: check that records are in the right order.
  104. */
  105. void
  106. xfs_btree_check_rec(
  107. xfs_btnum_t btnum, /* btree identifier */
  108. void *ar1, /* pointer to left (lower) record */
  109. void *ar2) /* pointer to right (higher) record */
  110. {
  111. switch (btnum) {
  112. case XFS_BTNUM_BNO: {
  113. xfs_alloc_rec_t *r1;
  114. xfs_alloc_rec_t *r2;
  115. r1 = ar1;
  116. r2 = ar2;
  117. ASSERT(be32_to_cpu(r1->ar_startblock) +
  118. be32_to_cpu(r1->ar_blockcount) <=
  119. be32_to_cpu(r2->ar_startblock));
  120. break;
  121. }
  122. case XFS_BTNUM_CNT: {
  123. xfs_alloc_rec_t *r1;
  124. xfs_alloc_rec_t *r2;
  125. r1 = ar1;
  126. r2 = ar2;
  127. ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
  128. (r1->ar_blockcount == r2->ar_blockcount &&
  129. be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
  130. break;
  131. }
  132. case XFS_BTNUM_BMAP: {
  133. xfs_bmbt_rec_t *r1;
  134. xfs_bmbt_rec_t *r2;
  135. r1 = ar1;
  136. r2 = ar2;
  137. ASSERT(xfs_bmbt_disk_get_startoff(r1) +
  138. xfs_bmbt_disk_get_blockcount(r1) <=
  139. xfs_bmbt_disk_get_startoff(r2));
  140. break;
  141. }
  142. case XFS_BTNUM_INO: {
  143. xfs_inobt_rec_t *r1;
  144. xfs_inobt_rec_t *r2;
  145. r1 = ar1;
  146. r2 = ar2;
  147. ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <=
  148. be32_to_cpu(r2->ir_startino));
  149. break;
  150. }
  151. default:
  152. ASSERT(0);
  153. }
  154. }
  155. #endif /* DEBUG */
  156. int /* error (0 or EFSCORRUPTED) */
  157. xfs_btree_check_lblock(
  158. struct xfs_btree_cur *cur, /* btree cursor */
  159. struct xfs_btree_lblock *block, /* btree long form block pointer */
  160. int level, /* level of the btree block */
  161. struct xfs_buf *bp) /* buffer for block, if any */
  162. {
  163. int lblock_ok; /* block passes checks */
  164. struct xfs_mount *mp; /* file system mount point */
  165. mp = cur->bc_mp;
  166. lblock_ok =
  167. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  168. be16_to_cpu(block->bb_level) == level &&
  169. be16_to_cpu(block->bb_numrecs) <=
  170. cur->bc_ops->get_maxrecs(cur, level) &&
  171. block->bb_leftsib &&
  172. (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
  173. XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
  174. block->bb_rightsib &&
  175. (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
  176. XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
  177. if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
  178. XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  179. XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  180. if (bp)
  181. xfs_buftrace("LBTREE ERROR", bp);
  182. XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
  183. mp);
  184. return XFS_ERROR(EFSCORRUPTED);
  185. }
  186. return 0;
  187. }
  188. int /* error (0 or EFSCORRUPTED) */
  189. xfs_btree_check_sblock(
  190. struct xfs_btree_cur *cur, /* btree cursor */
  191. struct xfs_btree_sblock *block, /* btree short form block pointer */
  192. int level, /* level of the btree block */
  193. struct xfs_buf *bp) /* buffer containing block */
  194. {
  195. struct xfs_buf *agbp; /* buffer for ag. freespace struct */
  196. struct xfs_agf *agf; /* ag. freespace structure */
  197. xfs_agblock_t agflen; /* native ag. freespace length */
  198. int sblock_ok; /* block passes checks */
  199. agbp = cur->bc_private.a.agbp;
  200. agf = XFS_BUF_TO_AGF(agbp);
  201. agflen = be32_to_cpu(agf->agf_length);
  202. sblock_ok =
  203. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  204. be16_to_cpu(block->bb_level) == level &&
  205. be16_to_cpu(block->bb_numrecs) <=
  206. cur->bc_ops->get_maxrecs(cur, level) &&
  207. (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
  208. be32_to_cpu(block->bb_leftsib) < agflen) &&
  209. block->bb_leftsib &&
  210. (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
  211. be32_to_cpu(block->bb_rightsib) < agflen) &&
  212. block->bb_rightsib;
  213. if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
  214. XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  215. XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  216. if (bp)
  217. xfs_buftrace("SBTREE ERROR", bp);
  218. XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
  219. cur->bc_mp);
  220. return XFS_ERROR(EFSCORRUPTED);
  221. }
  222. return 0;
  223. }
  224. /*
  225. * Debug routine: check that block header is ok.
  226. */
  227. int
  228. xfs_btree_check_block(
  229. struct xfs_btree_cur *cur, /* btree cursor */
  230. struct xfs_btree_block *block, /* generic btree block pointer */
  231. int level, /* level of the btree block */
  232. struct xfs_buf *bp) /* buffer containing block, if any */
  233. {
  234. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  235. return xfs_btree_check_lblock(cur,
  236. (struct xfs_btree_lblock *)block, level, bp);
  237. } else {
  238. return xfs_btree_check_sblock(cur,
  239. (struct xfs_btree_sblock *)block, level, bp);
  240. }
  241. }
  242. /*
  243. * Check that (long) pointer is ok.
  244. */
  245. int /* error (0 or EFSCORRUPTED) */
  246. xfs_btree_check_lptr(
  247. struct xfs_btree_cur *cur, /* btree cursor */
  248. xfs_dfsbno_t bno, /* btree block disk address */
  249. int level) /* btree block level */
  250. {
  251. XFS_WANT_CORRUPTED_RETURN(
  252. level > 0 &&
  253. bno != NULLDFSBNO &&
  254. XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
  255. return 0;
  256. }
  257. /*
  258. * Check that (short) pointer is ok.
  259. */
  260. int /* error (0 or EFSCORRUPTED) */
  261. xfs_btree_check_sptr(
  262. struct xfs_btree_cur *cur, /* btree cursor */
  263. xfs_agblock_t bno, /* btree block disk address */
  264. int level) /* btree block level */
  265. {
  266. xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
  267. XFS_WANT_CORRUPTED_RETURN(
  268. level > 0 &&
  269. bno != NULLAGBLOCK &&
  270. bno != 0 &&
  271. bno < agblocks);
  272. return 0;
  273. }
  274. /*
  275. * Check that block ptr is ok.
  276. */
  277. int /* error (0 or EFSCORRUPTED) */
  278. xfs_btree_check_ptr(
  279. struct xfs_btree_cur *cur, /* btree cursor */
  280. union xfs_btree_ptr *ptr, /* btree block disk address */
  281. int index, /* offset from ptr to check */
  282. int level) /* btree block level */
  283. {
  284. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  285. return xfs_btree_check_lptr(cur,
  286. be64_to_cpu((&ptr->l)[index]), level);
  287. } else {
  288. return xfs_btree_check_sptr(cur,
  289. be32_to_cpu((&ptr->s)[index]), level);
  290. }
  291. }
  292. /*
  293. * Delete the btree cursor.
  294. */
  295. void
  296. xfs_btree_del_cursor(
  297. xfs_btree_cur_t *cur, /* btree cursor */
  298. int error) /* del because of error */
  299. {
  300. int i; /* btree level */
  301. /*
  302. * Clear the buffer pointers, and release the buffers.
  303. * If we're doing this in the face of an error, we
  304. * need to make sure to inspect all of the entries
  305. * in the bc_bufs array for buffers to be unlocked.
  306. * This is because some of the btree code works from
  307. * level n down to 0, and if we get an error along
  308. * the way we won't have initialized all the entries
  309. * down to 0.
  310. */
  311. for (i = 0; i < cur->bc_nlevels; i++) {
  312. if (cur->bc_bufs[i])
  313. xfs_btree_setbuf(cur, i, NULL);
  314. else if (!error)
  315. break;
  316. }
  317. /*
  318. * Can't free a bmap cursor without having dealt with the
  319. * allocated indirect blocks' accounting.
  320. */
  321. ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  322. cur->bc_private.b.allocated == 0);
  323. /*
  324. * Free the cursor.
  325. */
  326. kmem_zone_free(xfs_btree_cur_zone, cur);
  327. }
  328. /*
  329. * Duplicate the btree cursor.
  330. * Allocate a new one, copy the record, re-get the buffers.
  331. */
  332. int /* error */
  333. xfs_btree_dup_cursor(
  334. xfs_btree_cur_t *cur, /* input cursor */
  335. xfs_btree_cur_t **ncur) /* output cursor */
  336. {
  337. xfs_buf_t *bp; /* btree block's buffer pointer */
  338. int error; /* error return value */
  339. int i; /* level number of btree block */
  340. xfs_mount_t *mp; /* mount structure for filesystem */
  341. xfs_btree_cur_t *new; /* new cursor value */
  342. xfs_trans_t *tp; /* transaction pointer, can be NULL */
  343. tp = cur->bc_tp;
  344. mp = cur->bc_mp;
  345. /*
  346. * Allocate a new cursor like the old one.
  347. */
  348. new = cur->bc_ops->dup_cursor(cur);
  349. /*
  350. * Copy the record currently in the cursor.
  351. */
  352. new->bc_rec = cur->bc_rec;
  353. /*
  354. * For each level current, re-get the buffer and copy the ptr value.
  355. */
  356. for (i = 0; i < new->bc_nlevels; i++) {
  357. new->bc_ptrs[i] = cur->bc_ptrs[i];
  358. new->bc_ra[i] = cur->bc_ra[i];
  359. if ((bp = cur->bc_bufs[i])) {
  360. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  361. XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
  362. xfs_btree_del_cursor(new, error);
  363. *ncur = NULL;
  364. return error;
  365. }
  366. new->bc_bufs[i] = bp;
  367. ASSERT(bp);
  368. ASSERT(!XFS_BUF_GETERROR(bp));
  369. } else
  370. new->bc_bufs[i] = NULL;
  371. }
  372. *ncur = new;
  373. return 0;
  374. }
  375. /*
  376. * Get a the root block which is stored in the inode.
  377. *
  378. * For now this btree implementation assumes the btree root is always
  379. * stored in the if_broot field of an inode fork.
  380. */
  381. STATIC struct xfs_btree_block *
  382. xfs_btree_get_iroot(
  383. struct xfs_btree_cur *cur)
  384. {
  385. struct xfs_ifork *ifp;
  386. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  387. return (struct xfs_btree_block *)ifp->if_broot;
  388. }
  389. /*
  390. * Retrieve the block pointer from the cursor at the given level.
  391. * This may be an inode btree root or from a buffer.
  392. */
  393. STATIC struct xfs_btree_block * /* generic btree block pointer */
  394. xfs_btree_get_block(
  395. struct xfs_btree_cur *cur, /* btree cursor */
  396. int level, /* level in btree */
  397. struct xfs_buf **bpp) /* buffer containing the block */
  398. {
  399. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  400. (level == cur->bc_nlevels - 1)) {
  401. *bpp = NULL;
  402. return xfs_btree_get_iroot(cur);
  403. }
  404. *bpp = cur->bc_bufs[level];
  405. return XFS_BUF_TO_BLOCK(*bpp);
  406. }
  407. /*
  408. * Get a buffer for the block, return it with no data read.
  409. * Long-form addressing.
  410. */
  411. xfs_buf_t * /* buffer for fsbno */
  412. xfs_btree_get_bufl(
  413. xfs_mount_t *mp, /* file system mount point */
  414. xfs_trans_t *tp, /* transaction pointer */
  415. xfs_fsblock_t fsbno, /* file system block number */
  416. uint lock) /* lock flags for get_buf */
  417. {
  418. xfs_buf_t *bp; /* buffer pointer (return value) */
  419. xfs_daddr_t d; /* real disk block address */
  420. ASSERT(fsbno != NULLFSBLOCK);
  421. d = XFS_FSB_TO_DADDR(mp, fsbno);
  422. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  423. ASSERT(bp);
  424. ASSERT(!XFS_BUF_GETERROR(bp));
  425. return bp;
  426. }
  427. /*
  428. * Get a buffer for the block, return it with no data read.
  429. * Short-form addressing.
  430. */
  431. xfs_buf_t * /* buffer for agno/agbno */
  432. xfs_btree_get_bufs(
  433. xfs_mount_t *mp, /* file system mount point */
  434. xfs_trans_t *tp, /* transaction pointer */
  435. xfs_agnumber_t agno, /* allocation group number */
  436. xfs_agblock_t agbno, /* allocation group block number */
  437. uint lock) /* lock flags for get_buf */
  438. {
  439. xfs_buf_t *bp; /* buffer pointer (return value) */
  440. xfs_daddr_t d; /* real disk block address */
  441. ASSERT(agno != NULLAGNUMBER);
  442. ASSERT(agbno != NULLAGBLOCK);
  443. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  444. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  445. ASSERT(bp);
  446. ASSERT(!XFS_BUF_GETERROR(bp));
  447. return bp;
  448. }
  449. /*
  450. * Check for the cursor referring to the last block at the given level.
  451. */
  452. int /* 1=is last block, 0=not last block */
  453. xfs_btree_islastblock(
  454. xfs_btree_cur_t *cur, /* btree cursor */
  455. int level) /* level to check */
  456. {
  457. xfs_btree_block_t *block; /* generic btree block pointer */
  458. xfs_buf_t *bp; /* buffer containing block */
  459. block = xfs_btree_get_block(cur, level, &bp);
  460. xfs_btree_check_block(cur, block, level, bp);
  461. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  462. return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
  463. else
  464. return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
  465. }
  466. /*
  467. * Change the cursor to point to the first record at the given level.
  468. * Other levels are unaffected.
  469. */
  470. int /* success=1, failure=0 */
  471. xfs_btree_firstrec(
  472. xfs_btree_cur_t *cur, /* btree cursor */
  473. int level) /* level to change */
  474. {
  475. xfs_btree_block_t *block; /* generic btree block pointer */
  476. xfs_buf_t *bp; /* buffer containing block */
  477. /*
  478. * Get the block pointer for this level.
  479. */
  480. block = xfs_btree_get_block(cur, level, &bp);
  481. xfs_btree_check_block(cur, block, level, bp);
  482. /*
  483. * It's empty, there is no such record.
  484. */
  485. if (!block->bb_numrecs)
  486. return 0;
  487. /*
  488. * Set the ptr value to 1, that's the first record/key.
  489. */
  490. cur->bc_ptrs[level] = 1;
  491. return 1;
  492. }
  493. /*
  494. * Change the cursor to point to the last record in the current block
  495. * at the given level. Other levels are unaffected.
  496. */
  497. int /* success=1, failure=0 */
  498. xfs_btree_lastrec(
  499. xfs_btree_cur_t *cur, /* btree cursor */
  500. int level) /* level to change */
  501. {
  502. xfs_btree_block_t *block; /* generic btree block pointer */
  503. xfs_buf_t *bp; /* buffer containing block */
  504. /*
  505. * Get the block pointer for this level.
  506. */
  507. block = xfs_btree_get_block(cur, level, &bp);
  508. xfs_btree_check_block(cur, block, level, bp);
  509. /*
  510. * It's empty, there is no such record.
  511. */
  512. if (!block->bb_numrecs)
  513. return 0;
  514. /*
  515. * Set the ptr value to numrecs, that's the last record/key.
  516. */
  517. cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
  518. return 1;
  519. }
  520. /*
  521. * Compute first and last byte offsets for the fields given.
  522. * Interprets the offsets table, which contains struct field offsets.
  523. */
  524. void
  525. xfs_btree_offsets(
  526. __int64_t fields, /* bitmask of fields */
  527. const short *offsets, /* table of field offsets */
  528. int nbits, /* number of bits to inspect */
  529. int *first, /* output: first byte offset */
  530. int *last) /* output: last byte offset */
  531. {
  532. int i; /* current bit number */
  533. __int64_t imask; /* mask for current bit number */
  534. ASSERT(fields != 0);
  535. /*
  536. * Find the lowest bit, so the first byte offset.
  537. */
  538. for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  539. if (imask & fields) {
  540. *first = offsets[i];
  541. break;
  542. }
  543. }
  544. /*
  545. * Find the highest bit, so the last byte offset.
  546. */
  547. for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  548. if (imask & fields) {
  549. *last = offsets[i + 1] - 1;
  550. break;
  551. }
  552. }
  553. }
  554. /*
  555. * Get a buffer for the block, return it read in.
  556. * Long-form addressing.
  557. */
  558. int /* error */
  559. xfs_btree_read_bufl(
  560. xfs_mount_t *mp, /* file system mount point */
  561. xfs_trans_t *tp, /* transaction pointer */
  562. xfs_fsblock_t fsbno, /* file system block number */
  563. uint lock, /* lock flags for read_buf */
  564. xfs_buf_t **bpp, /* buffer for fsbno */
  565. int refval) /* ref count value for buffer */
  566. {
  567. xfs_buf_t *bp; /* return value */
  568. xfs_daddr_t d; /* real disk block address */
  569. int error;
  570. ASSERT(fsbno != NULLFSBLOCK);
  571. d = XFS_FSB_TO_DADDR(mp, fsbno);
  572. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  573. mp->m_bsize, lock, &bp))) {
  574. return error;
  575. }
  576. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  577. if (bp != NULL) {
  578. XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  579. }
  580. *bpp = bp;
  581. return 0;
  582. }
  583. /*
  584. * Get a buffer for the block, return it read in.
  585. * Short-form addressing.
  586. */
  587. int /* error */
  588. xfs_btree_read_bufs(
  589. xfs_mount_t *mp, /* file system mount point */
  590. xfs_trans_t *tp, /* transaction pointer */
  591. xfs_agnumber_t agno, /* allocation group number */
  592. xfs_agblock_t agbno, /* allocation group block number */
  593. uint lock, /* lock flags for read_buf */
  594. xfs_buf_t **bpp, /* buffer for agno/agbno */
  595. int refval) /* ref count value for buffer */
  596. {
  597. xfs_buf_t *bp; /* return value */
  598. xfs_daddr_t d; /* real disk block address */
  599. int error;
  600. ASSERT(agno != NULLAGNUMBER);
  601. ASSERT(agbno != NULLAGBLOCK);
  602. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  603. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  604. mp->m_bsize, lock, &bp))) {
  605. return error;
  606. }
  607. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  608. if (bp != NULL) {
  609. switch (refval) {
  610. case XFS_ALLOC_BTREE_REF:
  611. XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  612. break;
  613. case XFS_INO_BTREE_REF:
  614. XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
  615. break;
  616. }
  617. }
  618. *bpp = bp;
  619. return 0;
  620. }
  621. /*
  622. * Read-ahead the block, don't wait for it, don't return a buffer.
  623. * Long-form addressing.
  624. */
  625. /* ARGSUSED */
  626. void
  627. xfs_btree_reada_bufl(
  628. xfs_mount_t *mp, /* file system mount point */
  629. xfs_fsblock_t fsbno, /* file system block number */
  630. xfs_extlen_t count) /* count of filesystem blocks */
  631. {
  632. xfs_daddr_t d;
  633. ASSERT(fsbno != NULLFSBLOCK);
  634. d = XFS_FSB_TO_DADDR(mp, fsbno);
  635. xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  636. }
  637. /*
  638. * Read-ahead the block, don't wait for it, don't return a buffer.
  639. * Short-form addressing.
  640. */
  641. /* ARGSUSED */
  642. void
  643. xfs_btree_reada_bufs(
  644. xfs_mount_t *mp, /* file system mount point */
  645. xfs_agnumber_t agno, /* allocation group number */
  646. xfs_agblock_t agbno, /* allocation group block number */
  647. xfs_extlen_t count) /* count of filesystem blocks */
  648. {
  649. xfs_daddr_t d;
  650. ASSERT(agno != NULLAGNUMBER);
  651. ASSERT(agbno != NULLAGBLOCK);
  652. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  653. xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  654. }
  655. STATIC int
  656. xfs_btree_readahead_lblock(
  657. struct xfs_btree_cur *cur,
  658. int lr,
  659. struct xfs_btree_block *block)
  660. {
  661. int rval = 0;
  662. xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
  663. xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
  664. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
  665. xfs_btree_reada_bufl(cur->bc_mp, left, 1);
  666. rval++;
  667. }
  668. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
  669. xfs_btree_reada_bufl(cur->bc_mp, right, 1);
  670. rval++;
  671. }
  672. return rval;
  673. }
  674. STATIC int
  675. xfs_btree_readahead_sblock(
  676. struct xfs_btree_cur *cur,
  677. int lr,
  678. struct xfs_btree_block *block)
  679. {
  680. int rval = 0;
  681. xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
  682. xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
  683. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
  684. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  685. left, 1);
  686. rval++;
  687. }
  688. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
  689. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  690. right, 1);
  691. rval++;
  692. }
  693. return rval;
  694. }
  695. /*
  696. * Read-ahead btree blocks, at the given level.
  697. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  698. */
  699. int
  700. xfs_btree_readahead(
  701. struct xfs_btree_cur *cur, /* btree cursor */
  702. int lev, /* level in btree */
  703. int lr) /* left/right bits */
  704. {
  705. struct xfs_btree_block *block;
  706. /*
  707. * No readahead needed if we are at the root level and the
  708. * btree root is stored in the inode.
  709. */
  710. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  711. (lev == cur->bc_nlevels - 1))
  712. return 0;
  713. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  714. return 0;
  715. cur->bc_ra[lev] |= lr;
  716. block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
  717. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  718. return xfs_btree_readahead_lblock(cur, lr, block);
  719. return xfs_btree_readahead_sblock(cur, lr, block);
  720. }
  721. /*
  722. * Set the buffer for level "lev" in the cursor to bp, releasing
  723. * any previous buffer.
  724. */
  725. void
  726. xfs_btree_setbuf(
  727. xfs_btree_cur_t *cur, /* btree cursor */
  728. int lev, /* level in btree */
  729. xfs_buf_t *bp) /* new buffer to set */
  730. {
  731. xfs_btree_block_t *b; /* btree block */
  732. xfs_buf_t *obp; /* old buffer pointer */
  733. obp = cur->bc_bufs[lev];
  734. if (obp)
  735. xfs_trans_brelse(cur->bc_tp, obp);
  736. cur->bc_bufs[lev] = bp;
  737. cur->bc_ra[lev] = 0;
  738. if (!bp)
  739. return;
  740. b = XFS_BUF_TO_BLOCK(bp);
  741. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  742. if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
  743. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  744. if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
  745. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  746. } else {
  747. if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
  748. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  749. if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
  750. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  751. }
  752. }