xfs_btree.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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_btree_trace.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_error.h"
  41. /*
  42. * Cursor allocation zone.
  43. */
  44. kmem_zone_t *xfs_btree_cur_zone;
  45. /*
  46. * Btree magic numbers.
  47. */
  48. const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
  49. XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
  50. };
  51. /*
  52. * External routines.
  53. */
  54. #ifdef DEBUG
  55. /*
  56. * Debug routine: check that keys are in the right order.
  57. */
  58. void
  59. xfs_btree_check_key(
  60. xfs_btnum_t btnum, /* btree identifier */
  61. void *ak1, /* pointer to left (lower) key */
  62. void *ak2) /* pointer to right (higher) key */
  63. {
  64. switch (btnum) {
  65. case XFS_BTNUM_BNO: {
  66. xfs_alloc_key_t *k1;
  67. xfs_alloc_key_t *k2;
  68. k1 = ak1;
  69. k2 = ak2;
  70. ASSERT(be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock));
  71. break;
  72. }
  73. case XFS_BTNUM_CNT: {
  74. xfs_alloc_key_t *k1;
  75. xfs_alloc_key_t *k2;
  76. k1 = ak1;
  77. k2 = ak2;
  78. ASSERT(be32_to_cpu(k1->ar_blockcount) < be32_to_cpu(k2->ar_blockcount) ||
  79. (k1->ar_blockcount == k2->ar_blockcount &&
  80. be32_to_cpu(k1->ar_startblock) < be32_to_cpu(k2->ar_startblock)));
  81. break;
  82. }
  83. case XFS_BTNUM_BMAP: {
  84. xfs_bmbt_key_t *k1;
  85. xfs_bmbt_key_t *k2;
  86. k1 = ak1;
  87. k2 = ak2;
  88. ASSERT(be64_to_cpu(k1->br_startoff) < be64_to_cpu(k2->br_startoff));
  89. break;
  90. }
  91. case XFS_BTNUM_INO: {
  92. xfs_inobt_key_t *k1;
  93. xfs_inobt_key_t *k2;
  94. k1 = ak1;
  95. k2 = ak2;
  96. ASSERT(be32_to_cpu(k1->ir_startino) < be32_to_cpu(k2->ir_startino));
  97. break;
  98. }
  99. default:
  100. ASSERT(0);
  101. }
  102. }
  103. /*
  104. * Debug routine: check that records are in the right order.
  105. */
  106. void
  107. xfs_btree_check_rec(
  108. xfs_btnum_t btnum, /* btree identifier */
  109. void *ar1, /* pointer to left (lower) record */
  110. void *ar2) /* pointer to right (higher) record */
  111. {
  112. switch (btnum) {
  113. case XFS_BTNUM_BNO: {
  114. xfs_alloc_rec_t *r1;
  115. xfs_alloc_rec_t *r2;
  116. r1 = ar1;
  117. r2 = ar2;
  118. ASSERT(be32_to_cpu(r1->ar_startblock) +
  119. be32_to_cpu(r1->ar_blockcount) <=
  120. be32_to_cpu(r2->ar_startblock));
  121. break;
  122. }
  123. case XFS_BTNUM_CNT: {
  124. xfs_alloc_rec_t *r1;
  125. xfs_alloc_rec_t *r2;
  126. r1 = ar1;
  127. r2 = ar2;
  128. ASSERT(be32_to_cpu(r1->ar_blockcount) < be32_to_cpu(r2->ar_blockcount) ||
  129. (r1->ar_blockcount == r2->ar_blockcount &&
  130. be32_to_cpu(r1->ar_startblock) < be32_to_cpu(r2->ar_startblock)));
  131. break;
  132. }
  133. case XFS_BTNUM_BMAP: {
  134. xfs_bmbt_rec_t *r1;
  135. xfs_bmbt_rec_t *r2;
  136. r1 = ar1;
  137. r2 = ar2;
  138. ASSERT(xfs_bmbt_disk_get_startoff(r1) +
  139. xfs_bmbt_disk_get_blockcount(r1) <=
  140. xfs_bmbt_disk_get_startoff(r2));
  141. break;
  142. }
  143. case XFS_BTNUM_INO: {
  144. xfs_inobt_rec_t *r1;
  145. xfs_inobt_rec_t *r2;
  146. r1 = ar1;
  147. r2 = ar2;
  148. ASSERT(be32_to_cpu(r1->ir_startino) + XFS_INODES_PER_CHUNK <=
  149. be32_to_cpu(r2->ir_startino));
  150. break;
  151. }
  152. default:
  153. ASSERT(0);
  154. }
  155. }
  156. #endif /* DEBUG */
  157. int /* error (0 or EFSCORRUPTED) */
  158. xfs_btree_check_lblock(
  159. struct xfs_btree_cur *cur, /* btree cursor */
  160. struct xfs_btree_lblock *block, /* btree long form block pointer */
  161. int level, /* level of the btree block */
  162. struct xfs_buf *bp) /* buffer for block, if any */
  163. {
  164. int lblock_ok; /* block passes checks */
  165. struct xfs_mount *mp; /* file system mount point */
  166. mp = cur->bc_mp;
  167. lblock_ok =
  168. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  169. be16_to_cpu(block->bb_level) == level &&
  170. be16_to_cpu(block->bb_numrecs) <=
  171. cur->bc_ops->get_maxrecs(cur, level) &&
  172. block->bb_leftsib &&
  173. (be64_to_cpu(block->bb_leftsib) == NULLDFSBNO ||
  174. XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_leftsib))) &&
  175. block->bb_rightsib &&
  176. (be64_to_cpu(block->bb_rightsib) == NULLDFSBNO ||
  177. XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_rightsib)));
  178. if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
  179. XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  180. XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  181. if (bp)
  182. xfs_buftrace("LBTREE ERROR", bp);
  183. XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
  184. mp);
  185. return XFS_ERROR(EFSCORRUPTED);
  186. }
  187. return 0;
  188. }
  189. int /* error (0 or EFSCORRUPTED) */
  190. xfs_btree_check_sblock(
  191. struct xfs_btree_cur *cur, /* btree cursor */
  192. struct xfs_btree_sblock *block, /* btree short form block pointer */
  193. int level, /* level of the btree block */
  194. struct xfs_buf *bp) /* buffer containing block */
  195. {
  196. struct xfs_buf *agbp; /* buffer for ag. freespace struct */
  197. struct xfs_agf *agf; /* ag. freespace structure */
  198. xfs_agblock_t agflen; /* native ag. freespace length */
  199. int sblock_ok; /* block passes checks */
  200. agbp = cur->bc_private.a.agbp;
  201. agf = XFS_BUF_TO_AGF(agbp);
  202. agflen = be32_to_cpu(agf->agf_length);
  203. sblock_ok =
  204. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  205. be16_to_cpu(block->bb_level) == level &&
  206. be16_to_cpu(block->bb_numrecs) <=
  207. cur->bc_ops->get_maxrecs(cur, level) &&
  208. (be32_to_cpu(block->bb_leftsib) == NULLAGBLOCK ||
  209. be32_to_cpu(block->bb_leftsib) < agflen) &&
  210. block->bb_leftsib &&
  211. (be32_to_cpu(block->bb_rightsib) == NULLAGBLOCK ||
  212. be32_to_cpu(block->bb_rightsib) < agflen) &&
  213. block->bb_rightsib;
  214. if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
  215. XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  216. XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  217. if (bp)
  218. xfs_buftrace("SBTREE ERROR", bp);
  219. XFS_ERROR_REPORT("xfs_btree_check_sblock", XFS_ERRLEVEL_LOW,
  220. cur->bc_mp);
  221. return XFS_ERROR(EFSCORRUPTED);
  222. }
  223. return 0;
  224. }
  225. /*
  226. * Debug routine: check that block header is ok.
  227. */
  228. int
  229. xfs_btree_check_block(
  230. struct xfs_btree_cur *cur, /* btree cursor */
  231. struct xfs_btree_block *block, /* generic btree block pointer */
  232. int level, /* level of the btree block */
  233. struct xfs_buf *bp) /* buffer containing block, if any */
  234. {
  235. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  236. return xfs_btree_check_lblock(cur,
  237. (struct xfs_btree_lblock *)block, level, bp);
  238. } else {
  239. return xfs_btree_check_sblock(cur,
  240. (struct xfs_btree_sblock *)block, level, bp);
  241. }
  242. }
  243. /*
  244. * Check that (long) pointer is ok.
  245. */
  246. int /* error (0 or EFSCORRUPTED) */
  247. xfs_btree_check_lptr(
  248. struct xfs_btree_cur *cur, /* btree cursor */
  249. xfs_dfsbno_t bno, /* btree block disk address */
  250. int level) /* btree block level */
  251. {
  252. XFS_WANT_CORRUPTED_RETURN(
  253. level > 0 &&
  254. bno != NULLDFSBNO &&
  255. XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
  256. return 0;
  257. }
  258. /*
  259. * Check that (short) pointer is ok.
  260. */
  261. int /* error (0 or EFSCORRUPTED) */
  262. xfs_btree_check_sptr(
  263. struct xfs_btree_cur *cur, /* btree cursor */
  264. xfs_agblock_t bno, /* btree block disk address */
  265. int level) /* btree block level */
  266. {
  267. xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
  268. XFS_WANT_CORRUPTED_RETURN(
  269. level > 0 &&
  270. bno != NULLAGBLOCK &&
  271. bno != 0 &&
  272. bno < agblocks);
  273. return 0;
  274. }
  275. /*
  276. * Check that block ptr is ok.
  277. */
  278. int /* error (0 or EFSCORRUPTED) */
  279. xfs_btree_check_ptr(
  280. struct xfs_btree_cur *cur, /* btree cursor */
  281. union xfs_btree_ptr *ptr, /* btree block disk address */
  282. int index, /* offset from ptr to check */
  283. int level) /* btree block level */
  284. {
  285. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  286. return xfs_btree_check_lptr(cur,
  287. be64_to_cpu((&ptr->l)[index]), level);
  288. } else {
  289. return xfs_btree_check_sptr(cur,
  290. be32_to_cpu((&ptr->s)[index]), level);
  291. }
  292. }
  293. /*
  294. * Delete the btree cursor.
  295. */
  296. void
  297. xfs_btree_del_cursor(
  298. xfs_btree_cur_t *cur, /* btree cursor */
  299. int error) /* del because of error */
  300. {
  301. int i; /* btree level */
  302. /*
  303. * Clear the buffer pointers, and release the buffers.
  304. * If we're doing this in the face of an error, we
  305. * need to make sure to inspect all of the entries
  306. * in the bc_bufs array for buffers to be unlocked.
  307. * This is because some of the btree code works from
  308. * level n down to 0, and if we get an error along
  309. * the way we won't have initialized all the entries
  310. * down to 0.
  311. */
  312. for (i = 0; i < cur->bc_nlevels; i++) {
  313. if (cur->bc_bufs[i])
  314. xfs_btree_setbuf(cur, i, NULL);
  315. else if (!error)
  316. break;
  317. }
  318. /*
  319. * Can't free a bmap cursor without having dealt with the
  320. * allocated indirect blocks' accounting.
  321. */
  322. ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  323. cur->bc_private.b.allocated == 0);
  324. /*
  325. * Free the cursor.
  326. */
  327. kmem_zone_free(xfs_btree_cur_zone, cur);
  328. }
  329. /*
  330. * Duplicate the btree cursor.
  331. * Allocate a new one, copy the record, re-get the buffers.
  332. */
  333. int /* error */
  334. xfs_btree_dup_cursor(
  335. xfs_btree_cur_t *cur, /* input cursor */
  336. xfs_btree_cur_t **ncur) /* output cursor */
  337. {
  338. xfs_buf_t *bp; /* btree block's buffer pointer */
  339. int error; /* error return value */
  340. int i; /* level number of btree block */
  341. xfs_mount_t *mp; /* mount structure for filesystem */
  342. xfs_btree_cur_t *new; /* new cursor value */
  343. xfs_trans_t *tp; /* transaction pointer, can be NULL */
  344. tp = cur->bc_tp;
  345. mp = cur->bc_mp;
  346. /*
  347. * Allocate a new cursor like the old one.
  348. */
  349. new = cur->bc_ops->dup_cursor(cur);
  350. /*
  351. * Copy the record currently in the cursor.
  352. */
  353. new->bc_rec = cur->bc_rec;
  354. /*
  355. * For each level current, re-get the buffer and copy the ptr value.
  356. */
  357. for (i = 0; i < new->bc_nlevels; i++) {
  358. new->bc_ptrs[i] = cur->bc_ptrs[i];
  359. new->bc_ra[i] = cur->bc_ra[i];
  360. if ((bp = cur->bc_bufs[i])) {
  361. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  362. XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
  363. xfs_btree_del_cursor(new, error);
  364. *ncur = NULL;
  365. return error;
  366. }
  367. new->bc_bufs[i] = bp;
  368. ASSERT(bp);
  369. ASSERT(!XFS_BUF_GETERROR(bp));
  370. } else
  371. new->bc_bufs[i] = NULL;
  372. }
  373. *ncur = new;
  374. return 0;
  375. }
  376. /*
  377. * XFS btree block layout and addressing:
  378. *
  379. * There are two types of blocks in the btree: leaf and non-leaf blocks.
  380. *
  381. * The leaf record start with a header then followed by records containing
  382. * the values. A non-leaf block also starts with the same header, and
  383. * then first contains lookup keys followed by an equal number of pointers
  384. * to the btree blocks at the previous level.
  385. *
  386. * +--------+-------+-------+-------+-------+-------+-------+
  387. * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
  388. * +--------+-------+-------+-------+-------+-------+-------+
  389. *
  390. * +--------+-------+-------+-------+-------+-------+-------+
  391. * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
  392. * +--------+-------+-------+-------+-------+-------+-------+
  393. *
  394. * The header is called struct xfs_btree_block for reasons better left unknown
  395. * and comes in different versions for short (32bit) and long (64bit) block
  396. * pointers. The record and key structures are defined by the btree instances
  397. * and opaque to the btree core. The block pointers are simple disk endian
  398. * integers, available in a short (32bit) and long (64bit) variant.
  399. *
  400. * The helpers below calculate the offset of a given record, key or pointer
  401. * into a btree block (xfs_btree_*_offset) or return a pointer to the given
  402. * record, key or pointer (xfs_btree_*_addr). Note that all addressing
  403. * inside the btree block is done using indices starting at one, not zero!
  404. */
  405. /*
  406. * Return size of the btree block header for this btree instance.
  407. */
  408. static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
  409. {
  410. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  411. sizeof(struct xfs_btree_lblock) :
  412. sizeof(struct xfs_btree_sblock);
  413. }
  414. /*
  415. * Return size of btree block pointers for this btree instance.
  416. */
  417. static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
  418. {
  419. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  420. sizeof(__be64) : sizeof(__be32);
  421. }
  422. /*
  423. * Calculate offset of the n-th record in a btree block.
  424. */
  425. STATIC size_t
  426. xfs_btree_rec_offset(
  427. struct xfs_btree_cur *cur,
  428. int n)
  429. {
  430. return xfs_btree_block_len(cur) +
  431. (n - 1) * cur->bc_ops->rec_len;
  432. }
  433. /*
  434. * Calculate offset of the n-th key in a btree block.
  435. */
  436. STATIC size_t
  437. xfs_btree_key_offset(
  438. struct xfs_btree_cur *cur,
  439. int n)
  440. {
  441. return xfs_btree_block_len(cur) +
  442. (n - 1) * cur->bc_ops->key_len;
  443. }
  444. /*
  445. * Calculate offset of the n-th block pointer in a btree block.
  446. */
  447. STATIC size_t
  448. xfs_btree_ptr_offset(
  449. struct xfs_btree_cur *cur,
  450. int n,
  451. int level)
  452. {
  453. return xfs_btree_block_len(cur) +
  454. cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
  455. (n - 1) * xfs_btree_ptr_len(cur);
  456. }
  457. /*
  458. * Return a pointer to the n-th record in the btree block.
  459. */
  460. STATIC union xfs_btree_rec *
  461. xfs_btree_rec_addr(
  462. struct xfs_btree_cur *cur,
  463. int n,
  464. struct xfs_btree_block *block)
  465. {
  466. return (union xfs_btree_rec *)
  467. ((char *)block + xfs_btree_rec_offset(cur, n));
  468. }
  469. /*
  470. * Return a pointer to the n-th key in the btree block.
  471. */
  472. STATIC union xfs_btree_key *
  473. xfs_btree_key_addr(
  474. struct xfs_btree_cur *cur,
  475. int n,
  476. struct xfs_btree_block *block)
  477. {
  478. return (union xfs_btree_key *)
  479. ((char *)block + xfs_btree_key_offset(cur, n));
  480. }
  481. /*
  482. * Return a pointer to the n-th block pointer in the btree block.
  483. */
  484. STATIC union xfs_btree_ptr *
  485. xfs_btree_ptr_addr(
  486. struct xfs_btree_cur *cur,
  487. int n,
  488. struct xfs_btree_block *block)
  489. {
  490. int level = xfs_btree_get_level(block);
  491. ASSERT(block->bb_level != 0);
  492. return (union xfs_btree_ptr *)
  493. ((char *)block + xfs_btree_ptr_offset(cur, n, level));
  494. }
  495. /*
  496. * Get a the root block which is stored in the inode.
  497. *
  498. * For now this btree implementation assumes the btree root is always
  499. * stored in the if_broot field of an inode fork.
  500. */
  501. STATIC struct xfs_btree_block *
  502. xfs_btree_get_iroot(
  503. struct xfs_btree_cur *cur)
  504. {
  505. struct xfs_ifork *ifp;
  506. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  507. return (struct xfs_btree_block *)ifp->if_broot;
  508. }
  509. /*
  510. * Retrieve the block pointer from the cursor at the given level.
  511. * This may be an inode btree root or from a buffer.
  512. */
  513. STATIC struct xfs_btree_block * /* generic btree block pointer */
  514. xfs_btree_get_block(
  515. struct xfs_btree_cur *cur, /* btree cursor */
  516. int level, /* level in btree */
  517. struct xfs_buf **bpp) /* buffer containing the block */
  518. {
  519. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  520. (level == cur->bc_nlevels - 1)) {
  521. *bpp = NULL;
  522. return xfs_btree_get_iroot(cur);
  523. }
  524. *bpp = cur->bc_bufs[level];
  525. return XFS_BUF_TO_BLOCK(*bpp);
  526. }
  527. /*
  528. * Get a buffer for the block, return it with no data read.
  529. * Long-form addressing.
  530. */
  531. xfs_buf_t * /* buffer for fsbno */
  532. xfs_btree_get_bufl(
  533. xfs_mount_t *mp, /* file system mount point */
  534. xfs_trans_t *tp, /* transaction pointer */
  535. xfs_fsblock_t fsbno, /* file system block number */
  536. uint lock) /* lock flags for get_buf */
  537. {
  538. xfs_buf_t *bp; /* buffer pointer (return value) */
  539. xfs_daddr_t d; /* real disk block address */
  540. ASSERT(fsbno != NULLFSBLOCK);
  541. d = XFS_FSB_TO_DADDR(mp, fsbno);
  542. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  543. ASSERT(bp);
  544. ASSERT(!XFS_BUF_GETERROR(bp));
  545. return bp;
  546. }
  547. /*
  548. * Get a buffer for the block, return it with no data read.
  549. * Short-form addressing.
  550. */
  551. xfs_buf_t * /* buffer for agno/agbno */
  552. xfs_btree_get_bufs(
  553. xfs_mount_t *mp, /* file system mount point */
  554. xfs_trans_t *tp, /* transaction pointer */
  555. xfs_agnumber_t agno, /* allocation group number */
  556. xfs_agblock_t agbno, /* allocation group block number */
  557. uint lock) /* lock flags for get_buf */
  558. {
  559. xfs_buf_t *bp; /* buffer pointer (return value) */
  560. xfs_daddr_t d; /* real disk block address */
  561. ASSERT(agno != NULLAGNUMBER);
  562. ASSERT(agbno != NULLAGBLOCK);
  563. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  564. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  565. ASSERT(bp);
  566. ASSERT(!XFS_BUF_GETERROR(bp));
  567. return bp;
  568. }
  569. /*
  570. * Check for the cursor referring to the last block at the given level.
  571. */
  572. int /* 1=is last block, 0=not last block */
  573. xfs_btree_islastblock(
  574. xfs_btree_cur_t *cur, /* btree cursor */
  575. int level) /* level to check */
  576. {
  577. xfs_btree_block_t *block; /* generic btree block pointer */
  578. xfs_buf_t *bp; /* buffer containing block */
  579. block = xfs_btree_get_block(cur, level, &bp);
  580. xfs_btree_check_block(cur, block, level, bp);
  581. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  582. return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
  583. else
  584. return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
  585. }
  586. /*
  587. * Change the cursor to point to the first record at the given level.
  588. * Other levels are unaffected.
  589. */
  590. int /* success=1, failure=0 */
  591. xfs_btree_firstrec(
  592. xfs_btree_cur_t *cur, /* btree cursor */
  593. int level) /* level to change */
  594. {
  595. xfs_btree_block_t *block; /* generic btree block pointer */
  596. xfs_buf_t *bp; /* buffer containing block */
  597. /*
  598. * Get the block pointer for this level.
  599. */
  600. block = xfs_btree_get_block(cur, level, &bp);
  601. xfs_btree_check_block(cur, block, level, bp);
  602. /*
  603. * It's empty, there is no such record.
  604. */
  605. if (!block->bb_numrecs)
  606. return 0;
  607. /*
  608. * Set the ptr value to 1, that's the first record/key.
  609. */
  610. cur->bc_ptrs[level] = 1;
  611. return 1;
  612. }
  613. /*
  614. * Change the cursor to point to the last record in the current block
  615. * at the given level. Other levels are unaffected.
  616. */
  617. int /* success=1, failure=0 */
  618. xfs_btree_lastrec(
  619. xfs_btree_cur_t *cur, /* btree cursor */
  620. int level) /* level to change */
  621. {
  622. xfs_btree_block_t *block; /* generic btree block pointer */
  623. xfs_buf_t *bp; /* buffer containing block */
  624. /*
  625. * Get the block pointer for this level.
  626. */
  627. block = xfs_btree_get_block(cur, level, &bp);
  628. xfs_btree_check_block(cur, block, level, bp);
  629. /*
  630. * It's empty, there is no such record.
  631. */
  632. if (!block->bb_numrecs)
  633. return 0;
  634. /*
  635. * Set the ptr value to numrecs, that's the last record/key.
  636. */
  637. cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
  638. return 1;
  639. }
  640. /*
  641. * Compute first and last byte offsets for the fields given.
  642. * Interprets the offsets table, which contains struct field offsets.
  643. */
  644. void
  645. xfs_btree_offsets(
  646. __int64_t fields, /* bitmask of fields */
  647. const short *offsets, /* table of field offsets */
  648. int nbits, /* number of bits to inspect */
  649. int *first, /* output: first byte offset */
  650. int *last) /* output: last byte offset */
  651. {
  652. int i; /* current bit number */
  653. __int64_t imask; /* mask for current bit number */
  654. ASSERT(fields != 0);
  655. /*
  656. * Find the lowest bit, so the first byte offset.
  657. */
  658. for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  659. if (imask & fields) {
  660. *first = offsets[i];
  661. break;
  662. }
  663. }
  664. /*
  665. * Find the highest bit, so the last byte offset.
  666. */
  667. for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  668. if (imask & fields) {
  669. *last = offsets[i + 1] - 1;
  670. break;
  671. }
  672. }
  673. }
  674. /*
  675. * Get a buffer for the block, return it read in.
  676. * Long-form addressing.
  677. */
  678. int /* error */
  679. xfs_btree_read_bufl(
  680. xfs_mount_t *mp, /* file system mount point */
  681. xfs_trans_t *tp, /* transaction pointer */
  682. xfs_fsblock_t fsbno, /* file system block number */
  683. uint lock, /* lock flags for read_buf */
  684. xfs_buf_t **bpp, /* buffer for fsbno */
  685. int refval) /* ref count value for buffer */
  686. {
  687. xfs_buf_t *bp; /* return value */
  688. xfs_daddr_t d; /* real disk block address */
  689. int error;
  690. ASSERT(fsbno != NULLFSBLOCK);
  691. d = XFS_FSB_TO_DADDR(mp, fsbno);
  692. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  693. mp->m_bsize, lock, &bp))) {
  694. return error;
  695. }
  696. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  697. if (bp != NULL) {
  698. XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  699. }
  700. *bpp = bp;
  701. return 0;
  702. }
  703. /*
  704. * Get a buffer for the block, return it read in.
  705. * Short-form addressing.
  706. */
  707. int /* error */
  708. xfs_btree_read_bufs(
  709. xfs_mount_t *mp, /* file system mount point */
  710. xfs_trans_t *tp, /* transaction pointer */
  711. xfs_agnumber_t agno, /* allocation group number */
  712. xfs_agblock_t agbno, /* allocation group block number */
  713. uint lock, /* lock flags for read_buf */
  714. xfs_buf_t **bpp, /* buffer for agno/agbno */
  715. int refval) /* ref count value for buffer */
  716. {
  717. xfs_buf_t *bp; /* return value */
  718. xfs_daddr_t d; /* real disk block address */
  719. int error;
  720. ASSERT(agno != NULLAGNUMBER);
  721. ASSERT(agbno != NULLAGBLOCK);
  722. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  723. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  724. mp->m_bsize, lock, &bp))) {
  725. return error;
  726. }
  727. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  728. if (bp != NULL) {
  729. switch (refval) {
  730. case XFS_ALLOC_BTREE_REF:
  731. XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  732. break;
  733. case XFS_INO_BTREE_REF:
  734. XFS_BUF_SET_VTYPE_REF(bp, B_FS_INOMAP, refval);
  735. break;
  736. }
  737. }
  738. *bpp = bp;
  739. return 0;
  740. }
  741. /*
  742. * Read-ahead the block, don't wait for it, don't return a buffer.
  743. * Long-form addressing.
  744. */
  745. /* ARGSUSED */
  746. void
  747. xfs_btree_reada_bufl(
  748. xfs_mount_t *mp, /* file system mount point */
  749. xfs_fsblock_t fsbno, /* file system block number */
  750. xfs_extlen_t count) /* count of filesystem blocks */
  751. {
  752. xfs_daddr_t d;
  753. ASSERT(fsbno != NULLFSBLOCK);
  754. d = XFS_FSB_TO_DADDR(mp, fsbno);
  755. xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  756. }
  757. /*
  758. * Read-ahead the block, don't wait for it, don't return a buffer.
  759. * Short-form addressing.
  760. */
  761. /* ARGSUSED */
  762. void
  763. xfs_btree_reada_bufs(
  764. xfs_mount_t *mp, /* file system mount point */
  765. xfs_agnumber_t agno, /* allocation group number */
  766. xfs_agblock_t agbno, /* allocation group block number */
  767. xfs_extlen_t count) /* count of filesystem blocks */
  768. {
  769. xfs_daddr_t d;
  770. ASSERT(agno != NULLAGNUMBER);
  771. ASSERT(agbno != NULLAGBLOCK);
  772. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  773. xfs_baread(mp->m_ddev_targp, d, mp->m_bsize * count);
  774. }
  775. STATIC int
  776. xfs_btree_readahead_lblock(
  777. struct xfs_btree_cur *cur,
  778. int lr,
  779. struct xfs_btree_block *block)
  780. {
  781. int rval = 0;
  782. xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
  783. xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
  784. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
  785. xfs_btree_reada_bufl(cur->bc_mp, left, 1);
  786. rval++;
  787. }
  788. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
  789. xfs_btree_reada_bufl(cur->bc_mp, right, 1);
  790. rval++;
  791. }
  792. return rval;
  793. }
  794. STATIC int
  795. xfs_btree_readahead_sblock(
  796. struct xfs_btree_cur *cur,
  797. int lr,
  798. struct xfs_btree_block *block)
  799. {
  800. int rval = 0;
  801. xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
  802. xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
  803. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
  804. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  805. left, 1);
  806. rval++;
  807. }
  808. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
  809. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  810. right, 1);
  811. rval++;
  812. }
  813. return rval;
  814. }
  815. /*
  816. * Read-ahead btree blocks, at the given level.
  817. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  818. */
  819. int
  820. xfs_btree_readahead(
  821. struct xfs_btree_cur *cur, /* btree cursor */
  822. int lev, /* level in btree */
  823. int lr) /* left/right bits */
  824. {
  825. struct xfs_btree_block *block;
  826. /*
  827. * No readahead needed if we are at the root level and the
  828. * btree root is stored in the inode.
  829. */
  830. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  831. (lev == cur->bc_nlevels - 1))
  832. return 0;
  833. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  834. return 0;
  835. cur->bc_ra[lev] |= lr;
  836. block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
  837. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  838. return xfs_btree_readahead_lblock(cur, lr, block);
  839. return xfs_btree_readahead_sblock(cur, lr, block);
  840. }
  841. /*
  842. * Set the buffer for level "lev" in the cursor to bp, releasing
  843. * any previous buffer.
  844. */
  845. void
  846. xfs_btree_setbuf(
  847. xfs_btree_cur_t *cur, /* btree cursor */
  848. int lev, /* level in btree */
  849. xfs_buf_t *bp) /* new buffer to set */
  850. {
  851. xfs_btree_block_t *b; /* btree block */
  852. xfs_buf_t *obp; /* old buffer pointer */
  853. obp = cur->bc_bufs[lev];
  854. if (obp)
  855. xfs_trans_brelse(cur->bc_tp, obp);
  856. cur->bc_bufs[lev] = bp;
  857. cur->bc_ra[lev] = 0;
  858. if (!bp)
  859. return;
  860. b = XFS_BUF_TO_BLOCK(bp);
  861. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  862. if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
  863. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  864. if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
  865. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  866. } else {
  867. if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
  868. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  869. if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
  870. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  871. }
  872. }
  873. STATIC int
  874. xfs_btree_ptr_is_null(
  875. struct xfs_btree_cur *cur,
  876. union xfs_btree_ptr *ptr)
  877. {
  878. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  879. return be64_to_cpu(ptr->l) == NULLFSBLOCK;
  880. else
  881. return be32_to_cpu(ptr->s) == NULLAGBLOCK;
  882. }
  883. /*
  884. * Get/set/init sibling pointers
  885. */
  886. STATIC void
  887. xfs_btree_get_sibling(
  888. struct xfs_btree_cur *cur,
  889. struct xfs_btree_block *block,
  890. union xfs_btree_ptr *ptr,
  891. int lr)
  892. {
  893. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  894. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  895. if (lr == XFS_BB_RIGHTSIB)
  896. ptr->l = block->bb_u.l.bb_rightsib;
  897. else
  898. ptr->l = block->bb_u.l.bb_leftsib;
  899. } else {
  900. if (lr == XFS_BB_RIGHTSIB)
  901. ptr->s = block->bb_u.s.bb_rightsib;
  902. else
  903. ptr->s = block->bb_u.s.bb_leftsib;
  904. }
  905. }
  906. STATIC xfs_daddr_t
  907. xfs_btree_ptr_to_daddr(
  908. struct xfs_btree_cur *cur,
  909. union xfs_btree_ptr *ptr)
  910. {
  911. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  912. ASSERT(be64_to_cpu(ptr->l) != NULLFSBLOCK);
  913. return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
  914. } else {
  915. ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
  916. ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
  917. return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
  918. be32_to_cpu(ptr->s));
  919. }
  920. }
  921. STATIC void
  922. xfs_btree_set_refs(
  923. struct xfs_btree_cur *cur,
  924. struct xfs_buf *bp)
  925. {
  926. switch (cur->bc_btnum) {
  927. case XFS_BTNUM_BNO:
  928. case XFS_BTNUM_CNT:
  929. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
  930. break;
  931. case XFS_BTNUM_INO:
  932. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
  933. break;
  934. case XFS_BTNUM_BMAP:
  935. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
  936. break;
  937. default:
  938. ASSERT(0);
  939. }
  940. }
  941. /*
  942. * Read in the buffer at the given ptr and return the buffer and
  943. * the block pointer within the buffer.
  944. */
  945. STATIC int
  946. xfs_btree_read_buf_block(
  947. struct xfs_btree_cur *cur,
  948. union xfs_btree_ptr *ptr,
  949. int level,
  950. int flags,
  951. struct xfs_btree_block **block,
  952. struct xfs_buf **bpp)
  953. {
  954. struct xfs_mount *mp = cur->bc_mp;
  955. xfs_daddr_t d;
  956. int error;
  957. /* need to sort out how callers deal with failures first */
  958. ASSERT(!(flags & XFS_BUF_TRYLOCK));
  959. d = xfs_btree_ptr_to_daddr(cur, ptr);
  960. error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
  961. mp->m_bsize, flags, bpp);
  962. if (error)
  963. return error;
  964. ASSERT(*bpp != NULL);
  965. ASSERT(!XFS_BUF_GETERROR(*bpp));
  966. xfs_btree_set_refs(cur, *bpp);
  967. *block = XFS_BUF_TO_BLOCK(*bpp);
  968. error = xfs_btree_check_block(cur, *block, level, *bpp);
  969. if (error)
  970. xfs_trans_brelse(cur->bc_tp, *bpp);
  971. return error;
  972. }
  973. /*
  974. * Increment cursor by one record at the level.
  975. * For nonzero levels the leaf-ward information is untouched.
  976. */
  977. int /* error */
  978. xfs_btree_increment(
  979. struct xfs_btree_cur *cur,
  980. int level,
  981. int *stat) /* success/failure */
  982. {
  983. struct xfs_btree_block *block;
  984. union xfs_btree_ptr ptr;
  985. struct xfs_buf *bp;
  986. int error; /* error return value */
  987. int lev;
  988. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  989. XFS_BTREE_TRACE_ARGI(cur, level);
  990. ASSERT(level < cur->bc_nlevels);
  991. /* Read-ahead to the right at this level. */
  992. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  993. /* Get a pointer to the btree block. */
  994. block = xfs_btree_get_block(cur, level, &bp);
  995. #ifdef DEBUG
  996. error = xfs_btree_check_block(cur, block, level, bp);
  997. if (error)
  998. goto error0;
  999. #endif
  1000. /* We're done if we remain in the block after the increment. */
  1001. if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
  1002. goto out1;
  1003. /* Fail if we just went off the right edge of the tree. */
  1004. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1005. if (xfs_btree_ptr_is_null(cur, &ptr))
  1006. goto out0;
  1007. XFS_BTREE_STATS_INC(cur, increment);
  1008. /*
  1009. * March up the tree incrementing pointers.
  1010. * Stop when we don't go off the right edge of a block.
  1011. */
  1012. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1013. block = xfs_btree_get_block(cur, lev, &bp);
  1014. #ifdef DEBUG
  1015. error = xfs_btree_check_block(cur, block, lev, bp);
  1016. if (error)
  1017. goto error0;
  1018. #endif
  1019. if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
  1020. break;
  1021. /* Read-ahead the right block for the next loop. */
  1022. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1023. }
  1024. /*
  1025. * If we went off the root then we are either seriously
  1026. * confused or have the tree root in an inode.
  1027. */
  1028. if (lev == cur->bc_nlevels) {
  1029. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1030. goto out0;
  1031. ASSERT(0);
  1032. error = EFSCORRUPTED;
  1033. goto error0;
  1034. }
  1035. ASSERT(lev < cur->bc_nlevels);
  1036. /*
  1037. * Now walk back down the tree, fixing up the cursor's buffer
  1038. * pointers and key numbers.
  1039. */
  1040. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1041. union xfs_btree_ptr *ptrp;
  1042. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1043. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1044. 0, &block, &bp);
  1045. if (error)
  1046. goto error0;
  1047. xfs_btree_setbuf(cur, lev, bp);
  1048. cur->bc_ptrs[lev] = 1;
  1049. }
  1050. out1:
  1051. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1052. *stat = 1;
  1053. return 0;
  1054. out0:
  1055. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1056. *stat = 0;
  1057. return 0;
  1058. error0:
  1059. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1060. return error;
  1061. }
  1062. /*
  1063. * Decrement cursor by one record at the level.
  1064. * For nonzero levels the leaf-ward information is untouched.
  1065. */
  1066. int /* error */
  1067. xfs_btree_decrement(
  1068. struct xfs_btree_cur *cur,
  1069. int level,
  1070. int *stat) /* success/failure */
  1071. {
  1072. struct xfs_btree_block *block;
  1073. xfs_buf_t *bp;
  1074. int error; /* error return value */
  1075. int lev;
  1076. union xfs_btree_ptr ptr;
  1077. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1078. XFS_BTREE_TRACE_ARGI(cur, level);
  1079. ASSERT(level < cur->bc_nlevels);
  1080. /* Read-ahead to the left at this level. */
  1081. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1082. /* We're done if we remain in the block after the decrement. */
  1083. if (--cur->bc_ptrs[level] > 0)
  1084. goto out1;
  1085. /* Get a pointer to the btree block. */
  1086. block = xfs_btree_get_block(cur, level, &bp);
  1087. #ifdef DEBUG
  1088. error = xfs_btree_check_block(cur, block, level, bp);
  1089. if (error)
  1090. goto error0;
  1091. #endif
  1092. /* Fail if we just went off the left edge of the tree. */
  1093. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  1094. if (xfs_btree_ptr_is_null(cur, &ptr))
  1095. goto out0;
  1096. XFS_BTREE_STATS_INC(cur, decrement);
  1097. /*
  1098. * March up the tree decrementing pointers.
  1099. * Stop when we don't go off the left edge of a block.
  1100. */
  1101. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1102. if (--cur->bc_ptrs[lev] > 0)
  1103. break;
  1104. /* Read-ahead the left block for the next loop. */
  1105. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1106. }
  1107. /*
  1108. * If we went off the root then we are seriously confused.
  1109. * or the root of the tree is in an inode.
  1110. */
  1111. if (lev == cur->bc_nlevels) {
  1112. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1113. goto out0;
  1114. ASSERT(0);
  1115. error = EFSCORRUPTED;
  1116. goto error0;
  1117. }
  1118. ASSERT(lev < cur->bc_nlevels);
  1119. /*
  1120. * Now walk back down the tree, fixing up the cursor's buffer
  1121. * pointers and key numbers.
  1122. */
  1123. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1124. union xfs_btree_ptr *ptrp;
  1125. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1126. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1127. 0, &block, &bp);
  1128. if (error)
  1129. goto error0;
  1130. xfs_btree_setbuf(cur, lev, bp);
  1131. cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
  1132. }
  1133. out1:
  1134. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1135. *stat = 1;
  1136. return 0;
  1137. out0:
  1138. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1139. *stat = 0;
  1140. return 0;
  1141. error0:
  1142. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1143. return error;
  1144. }