xfs_btree.h 15 KB

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