xfs_btree.h 14 KB

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