xfs_btree.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2000-2001,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. #ifndef __XFS_BTREE_H__
  19. #define __XFS_BTREE_H__
  20. struct xfs_buf;
  21. struct xfs_bmap_free;
  22. struct xfs_inode;
  23. struct xfs_mount;
  24. struct xfs_trans;
  25. /*
  26. * This nonsense is to make -wlint happy.
  27. */
  28. #define XFS_LOOKUP_EQ ((xfs_lookup_t)XFS_LOOKUP_EQi)
  29. #define XFS_LOOKUP_LE ((xfs_lookup_t)XFS_LOOKUP_LEi)
  30. #define XFS_LOOKUP_GE ((xfs_lookup_t)XFS_LOOKUP_GEi)
  31. #define XFS_BTNUM_BNO ((xfs_btnum_t)XFS_BTNUM_BNOi)
  32. #define XFS_BTNUM_CNT ((xfs_btnum_t)XFS_BTNUM_CNTi)
  33. #define XFS_BTNUM_BMAP ((xfs_btnum_t)XFS_BTNUM_BMAPi)
  34. #define XFS_BTNUM_INO ((xfs_btnum_t)XFS_BTNUM_INOi)
  35. /*
  36. * Short form header: space allocation btrees.
  37. */
  38. typedef struct xfs_btree_sblock {
  39. __be32 bb_magic; /* magic number for block type */
  40. __be16 bb_level; /* 0 is a leaf */
  41. __be16 bb_numrecs; /* current # of data records */
  42. __be32 bb_leftsib; /* left sibling block or NULLAGBLOCK */
  43. __be32 bb_rightsib; /* right sibling block or NULLAGBLOCK */
  44. } xfs_btree_sblock_t;
  45. /*
  46. * Long form header: bmap btrees.
  47. */
  48. typedef struct xfs_btree_lblock {
  49. __be32 bb_magic; /* magic number for block type */
  50. __be16 bb_level; /* 0 is a leaf */
  51. __be16 bb_numrecs; /* current # of data records */
  52. __be64 bb_leftsib; /* left sibling block or NULLDFSBNO */
  53. __be64 bb_rightsib; /* right sibling block or NULLDFSBNO */
  54. } xfs_btree_lblock_t;
  55. /*
  56. * Combined header and structure, used by common code.
  57. */
  58. typedef struct xfs_btree_hdr
  59. {
  60. __be32 bb_magic; /* magic number for block type */
  61. __be16 bb_level; /* 0 is a leaf */
  62. __be16 bb_numrecs; /* current # of data records */
  63. } xfs_btree_hdr_t;
  64. typedef struct xfs_btree_block {
  65. xfs_btree_hdr_t bb_h; /* header */
  66. union {
  67. struct {
  68. __be32 bb_leftsib;
  69. __be32 bb_rightsib;
  70. } s; /* short form pointers */
  71. struct {
  72. __be64 bb_leftsib;
  73. __be64 bb_rightsib;
  74. } l; /* long form pointers */
  75. } bb_u; /* rest */
  76. } xfs_btree_block_t;
  77. /*
  78. * For logging record fields.
  79. */
  80. #define XFS_BB_MAGIC 0x01
  81. #define XFS_BB_LEVEL 0x02
  82. #define XFS_BB_NUMRECS 0x04
  83. #define XFS_BB_LEFTSIB 0x08
  84. #define XFS_BB_RIGHTSIB 0x10
  85. #define XFS_BB_NUM_BITS 5
  86. #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
  87. /*
  88. * Boolean to select which form of xfs_btree_block_t.bb_u to use.
  89. */
  90. #define XFS_BTREE_LONG_PTRS(btnum) ((btnum) == XFS_BTNUM_BMAP)
  91. /*
  92. * Magic numbers for btree blocks.
  93. */
  94. extern const __uint32_t xfs_magics[];
  95. /*
  96. * Maximum and minimum records in a btree block.
  97. * Given block size, type prefix, and leaf flag (0 or 1).
  98. * The divisor below is equivalent to lf ? (e1) : (e2) but that produces
  99. * compiler warnings.
  100. */
  101. #define XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) \
  102. ((int)(((bsz) - (uint)sizeof(t ## _block_t)) / \
  103. (((lf) * (uint)sizeof(t ## _rec_t)) + \
  104. ((1 - (lf)) * \
  105. ((uint)sizeof(t ## _key_t) + (uint)sizeof(t ## _ptr_t))))))
  106. #define XFS_BTREE_BLOCK_MINRECS(bsz,t,lf) \
  107. (XFS_BTREE_BLOCK_MAXRECS(bsz,t,lf) / 2)
  108. /*
  109. * Record, key, and pointer address calculation macros.
  110. * Given block size, type prefix, block pointer, and index of requested entry
  111. * (first entry numbered 1).
  112. */
  113. #define XFS_BTREE_REC_ADDR(t,bb,i) \
  114. ((t ## _rec_t *)((char *)(bb) + sizeof(t ## _block_t) + \
  115. ((i) - 1) * sizeof(t ## _rec_t)))
  116. #define XFS_BTREE_KEY_ADDR(t,bb,i) \
  117. ((t ## _key_t *)((char *)(bb) + sizeof(t ## _block_t) + \
  118. ((i) - 1) * sizeof(t ## _key_t)))
  119. #define XFS_BTREE_PTR_ADDR(t,bb,i,mxr) \
  120. ((t ## _ptr_t *)((char *)(bb) + sizeof(t ## _block_t) + \
  121. (mxr) * sizeof(t ## _key_t) + ((i) - 1) * sizeof(t ## _ptr_t)))
  122. #define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
  123. /*
  124. * Btree cursor structure.
  125. * This collects all information needed by the btree code in one place.
  126. */
  127. typedef struct xfs_btree_cur
  128. {
  129. struct xfs_trans *bc_tp; /* transaction we're in, if any */
  130. struct xfs_mount *bc_mp; /* file system mount struct */
  131. union {
  132. xfs_alloc_rec_incore_t a;
  133. xfs_bmbt_irec_t b;
  134. xfs_inobt_rec_incore_t i;
  135. } bc_rec; /* current insert/search record value */
  136. struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
  137. int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
  138. __uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
  139. #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
  140. #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
  141. __uint8_t bc_nlevels; /* number of levels in the tree */
  142. __uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
  143. xfs_btnum_t bc_btnum; /* identifies which btree type */
  144. union {
  145. struct { /* needed for BNO, CNT */
  146. struct xfs_buf *agbp; /* agf buffer pointer */
  147. xfs_agnumber_t agno; /* ag number */
  148. } a;
  149. struct { /* needed for BMAP */
  150. struct xfs_inode *ip; /* pointer to our inode */
  151. struct xfs_bmap_free *flist; /* list to free after */
  152. xfs_fsblock_t firstblock; /* 1st blk allocated */
  153. int allocated; /* count of alloced */
  154. short forksize; /* fork's inode space */
  155. char whichfork; /* data or attr fork */
  156. char flags; /* flags */
  157. #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
  158. } b;
  159. struct { /* needed for INO */
  160. struct xfs_buf *agbp; /* agi buffer pointer */
  161. xfs_agnumber_t agno; /* ag number */
  162. } i;
  163. } bc_private; /* per-btree type data */
  164. } xfs_btree_cur_t;
  165. #define XFS_BTREE_NOERROR 0
  166. #define XFS_BTREE_ERROR 1
  167. /*
  168. * Convert from buffer to btree block header.
  169. */
  170. #define XFS_BUF_TO_BLOCK(bp) ((xfs_btree_block_t *)XFS_BUF_PTR(bp))
  171. #define XFS_BUF_TO_LBLOCK(bp) ((xfs_btree_lblock_t *)XFS_BUF_PTR(bp))
  172. #define XFS_BUF_TO_SBLOCK(bp) ((xfs_btree_sblock_t *)XFS_BUF_PTR(bp))
  173. #ifdef __KERNEL__
  174. #ifdef DEBUG
  175. /*
  176. * Debug routine: check that block header is ok.
  177. */
  178. void
  179. xfs_btree_check_block(
  180. xfs_btree_cur_t *cur, /* btree cursor */
  181. xfs_btree_block_t *block, /* generic btree block pointer */
  182. int level, /* level of the btree block */
  183. struct xfs_buf *bp); /* buffer containing block, if any */
  184. /*
  185. * Debug routine: check that keys are in the right order.
  186. */
  187. void
  188. xfs_btree_check_key(
  189. xfs_btnum_t btnum, /* btree identifier */
  190. void *ak1, /* pointer to left (lower) key */
  191. void *ak2); /* pointer to right (higher) key */
  192. /*
  193. * Debug routine: check that records are in the right order.
  194. */
  195. void
  196. xfs_btree_check_rec(
  197. xfs_btnum_t btnum, /* btree identifier */
  198. void *ar1, /* pointer to left (lower) record */
  199. void *ar2); /* pointer to right (higher) record */
  200. #else
  201. #define xfs_btree_check_block(a,b,c,d)
  202. #define xfs_btree_check_key(a,b,c)
  203. #define xfs_btree_check_rec(a,b,c)
  204. #endif /* DEBUG */
  205. /*
  206. * Checking routine: check that long form block header is ok.
  207. */
  208. int /* error (0 or EFSCORRUPTED) */
  209. xfs_btree_check_lblock(
  210. xfs_btree_cur_t *cur, /* btree cursor */
  211. xfs_btree_lblock_t *block, /* btree long form block pointer */
  212. int level, /* level of the btree block */
  213. struct xfs_buf *bp); /* buffer containing block, if any */
  214. /*
  215. * Checking routine: check that (long) pointer is ok.
  216. */
  217. int /* error (0 or EFSCORRUPTED) */
  218. xfs_btree_check_lptr(
  219. xfs_btree_cur_t *cur, /* btree cursor */
  220. xfs_dfsbno_t ptr, /* btree block disk address */
  221. int level); /* btree block level */
  222. #define xfs_btree_check_lptr_disk(cur, ptr, level) \
  223. xfs_btree_check_lptr(cur, be64_to_cpu(ptr), level)
  224. /*
  225. * Checking routine: check that short form block header is ok.
  226. */
  227. int /* error (0 or EFSCORRUPTED) */
  228. xfs_btree_check_sblock(
  229. xfs_btree_cur_t *cur, /* btree cursor */
  230. xfs_btree_sblock_t *block, /* btree short form block pointer */
  231. int level, /* level of the btree block */
  232. struct xfs_buf *bp); /* buffer containing block */
  233. /*
  234. * Checking routine: check that (short) pointer is ok.
  235. */
  236. int /* error (0 or EFSCORRUPTED) */
  237. xfs_btree_check_sptr(
  238. xfs_btree_cur_t *cur, /* btree cursor */
  239. xfs_agblock_t ptr, /* btree block disk address */
  240. int level); /* btree block level */
  241. /*
  242. * Delete the btree cursor.
  243. */
  244. void
  245. xfs_btree_del_cursor(
  246. xfs_btree_cur_t *cur, /* btree cursor */
  247. int error); /* del because of error */
  248. /*
  249. * Duplicate the btree cursor.
  250. * Allocate a new one, copy the record, re-get the buffers.
  251. */
  252. int /* error */
  253. xfs_btree_dup_cursor(
  254. xfs_btree_cur_t *cur, /* input cursor */
  255. xfs_btree_cur_t **ncur);/* output cursor */
  256. /*
  257. * Change the cursor to point to the first record in the current block
  258. * at the given level. Other levels are unaffected.
  259. */
  260. int /* success=1, failure=0 */
  261. xfs_btree_firstrec(
  262. xfs_btree_cur_t *cur, /* btree cursor */
  263. int level); /* level to change */
  264. /*
  265. * Get a buffer for the block, return it with no data read.
  266. * Long-form addressing.
  267. */
  268. struct xfs_buf * /* buffer for fsbno */
  269. xfs_btree_get_bufl(
  270. struct xfs_mount *mp, /* file system mount point */
  271. struct xfs_trans *tp, /* transaction pointer */
  272. xfs_fsblock_t fsbno, /* file system block number */
  273. uint lock); /* lock flags for get_buf */
  274. /*
  275. * Get a buffer for the block, return it with no data read.
  276. * Short-form addressing.
  277. */
  278. struct xfs_buf * /* buffer for agno/agbno */
  279. xfs_btree_get_bufs(
  280. struct xfs_mount *mp, /* file system mount point */
  281. struct xfs_trans *tp, /* transaction pointer */
  282. xfs_agnumber_t agno, /* allocation group number */
  283. xfs_agblock_t agbno, /* allocation group block number */
  284. uint lock); /* lock flags for get_buf */
  285. /*
  286. * Allocate a new btree cursor.
  287. * The cursor is either for allocation (A) or bmap (B).
  288. */
  289. xfs_btree_cur_t * /* new btree cursor */
  290. xfs_btree_init_cursor(
  291. struct xfs_mount *mp, /* file system mount point */
  292. struct xfs_trans *tp, /* transaction pointer */
  293. struct xfs_buf *agbp, /* (A only) buffer for agf structure */
  294. xfs_agnumber_t agno, /* (A only) allocation group number */
  295. xfs_btnum_t btnum, /* btree identifier */
  296. struct xfs_inode *ip, /* (B only) inode owning the btree */
  297. int whichfork); /* (B only) data/attr fork */
  298. /*
  299. * Check for the cursor referring to the last block at the given level.
  300. */
  301. int /* 1=is last block, 0=not last block */
  302. xfs_btree_islastblock(
  303. xfs_btree_cur_t *cur, /* btree cursor */
  304. int level); /* level to check */
  305. /*
  306. * Change the cursor to point to the last record in the current block
  307. * at the given level. Other levels are unaffected.
  308. */
  309. int /* success=1, failure=0 */
  310. xfs_btree_lastrec(
  311. xfs_btree_cur_t *cur, /* btree cursor */
  312. int level); /* level to change */
  313. /*
  314. * Compute first and last byte offsets for the fields given.
  315. * Interprets the offsets table, which contains struct field offsets.
  316. */
  317. void
  318. xfs_btree_offsets(
  319. __int64_t fields, /* bitmask of fields */
  320. const short *offsets,/* table of field offsets */
  321. int nbits, /* number of bits to inspect */
  322. int *first, /* output: first byte offset */
  323. int *last); /* output: last byte offset */
  324. /*
  325. * Get a buffer for the block, return it read in.
  326. * Long-form addressing.
  327. */
  328. int /* error */
  329. xfs_btree_read_bufl(
  330. struct xfs_mount *mp, /* file system mount point */
  331. struct xfs_trans *tp, /* transaction pointer */
  332. xfs_fsblock_t fsbno, /* file system block number */
  333. uint lock, /* lock flags for read_buf */
  334. struct xfs_buf **bpp, /* buffer for fsbno */
  335. int refval);/* ref count value for buffer */
  336. /*
  337. * Get a buffer for the block, return it read in.
  338. * Short-form addressing.
  339. */
  340. int /* error */
  341. xfs_btree_read_bufs(
  342. struct xfs_mount *mp, /* file system mount point */
  343. struct xfs_trans *tp, /* transaction pointer */
  344. xfs_agnumber_t agno, /* allocation group number */
  345. xfs_agblock_t agbno, /* allocation group block number */
  346. uint lock, /* lock flags for read_buf */
  347. struct xfs_buf **bpp, /* buffer for agno/agbno */
  348. int refval);/* ref count value for buffer */
  349. /*
  350. * Read-ahead the block, don't wait for it, don't return a buffer.
  351. * Long-form addressing.
  352. */
  353. void /* error */
  354. xfs_btree_reada_bufl(
  355. struct xfs_mount *mp, /* file system mount point */
  356. xfs_fsblock_t fsbno, /* file system block number */
  357. xfs_extlen_t count); /* count of filesystem blocks */
  358. /*
  359. * Read-ahead the block, don't wait for it, don't return a buffer.
  360. * Short-form addressing.
  361. */
  362. void /* error */
  363. xfs_btree_reada_bufs(
  364. struct xfs_mount *mp, /* file system mount point */
  365. xfs_agnumber_t agno, /* allocation group number */
  366. xfs_agblock_t agbno, /* allocation group block number */
  367. xfs_extlen_t count); /* count of filesystem blocks */
  368. /*
  369. * Read-ahead btree blocks, at the given level.
  370. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  371. */
  372. int /* readahead block count */
  373. xfs_btree_readahead_core(
  374. xfs_btree_cur_t *cur, /* btree cursor */
  375. int lev, /* level in btree */
  376. int lr); /* left/right bits */
  377. static inline int /* readahead block count */
  378. xfs_btree_readahead(
  379. xfs_btree_cur_t *cur, /* btree cursor */
  380. int lev, /* level in btree */
  381. int lr) /* left/right bits */
  382. {
  383. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  384. return 0;
  385. return xfs_btree_readahead_core(cur, lev, lr);
  386. }
  387. /*
  388. * Set the buffer for level "lev" in the cursor to bp, releasing
  389. * any previous buffer.
  390. */
  391. void
  392. xfs_btree_setbuf(
  393. xfs_btree_cur_t *cur, /* btree cursor */
  394. int lev, /* level in btree */
  395. struct xfs_buf *bp); /* new buffer to set */
  396. #endif /* __KERNEL__ */
  397. /*
  398. * Min and max functions for extlen, agblock, fileoff, and filblks types.
  399. */
  400. #define XFS_EXTLEN_MIN(a,b) \
  401. ((xfs_extlen_t)(a) < (xfs_extlen_t)(b) ? \
  402. (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
  403. #define XFS_EXTLEN_MAX(a,b) \
  404. ((xfs_extlen_t)(a) > (xfs_extlen_t)(b) ? \
  405. (xfs_extlen_t)(a) : (xfs_extlen_t)(b))
  406. #define XFS_AGBLOCK_MIN(a,b) \
  407. ((xfs_agblock_t)(a) < (xfs_agblock_t)(b) ? \
  408. (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
  409. #define XFS_AGBLOCK_MAX(a,b) \
  410. ((xfs_agblock_t)(a) > (xfs_agblock_t)(b) ? \
  411. (xfs_agblock_t)(a) : (xfs_agblock_t)(b))
  412. #define XFS_FILEOFF_MIN(a,b) \
  413. ((xfs_fileoff_t)(a) < (xfs_fileoff_t)(b) ? \
  414. (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
  415. #define XFS_FILEOFF_MAX(a,b) \
  416. ((xfs_fileoff_t)(a) > (xfs_fileoff_t)(b) ? \
  417. (xfs_fileoff_t)(a) : (xfs_fileoff_t)(b))
  418. #define XFS_FILBLKS_MIN(a,b) \
  419. ((xfs_filblks_t)(a) < (xfs_filblks_t)(b) ? \
  420. (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
  421. #define XFS_FILBLKS_MAX(a,b) \
  422. ((xfs_filblks_t)(a) > (xfs_filblks_t)(b) ? \
  423. (xfs_filblks_t)(a) : (xfs_filblks_t)(b))
  424. #define XFS_FSB_SANITY_CHECK(mp,fsb) \
  425. (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
  426. XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
  427. #endif /* __XFS_BTREE_H__ */