xfs_btree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. * Generic btree header.
  38. *
  39. * This is a combination of the actual format used on disk for short and long
  40. * format btrees. The first three fields are shared by both format, but
  41. * the pointers are different and should be used with care.
  42. *
  43. * To get the size of the actual short or long form headers please use
  44. * the size macros below. Never use sizeof(xfs_btree_block).
  45. */
  46. struct xfs_btree_block {
  47. __be32 bb_magic; /* magic number for block type */
  48. __be16 bb_level; /* 0 is a leaf */
  49. __be16 bb_numrecs; /* current # of data records */
  50. union {
  51. struct {
  52. __be32 bb_leftsib;
  53. __be32 bb_rightsib;
  54. } s; /* short form pointers */
  55. struct {
  56. __be64 bb_leftsib;
  57. __be64 bb_rightsib;
  58. } l; /* long form pointers */
  59. } bb_u; /* rest */
  60. };
  61. #define XFS_BTREE_SBLOCK_LEN 16 /* size of a short form block */
  62. #define XFS_BTREE_LBLOCK_LEN 24 /* size of a long form block */
  63. /*
  64. * Generic key, ptr and record wrapper structures.
  65. *
  66. * These are disk format structures, and are converted where necessary
  67. * by the btree specific code that needs to interpret them.
  68. */
  69. union xfs_btree_ptr {
  70. __be32 s; /* short form ptr */
  71. __be64 l; /* long form ptr */
  72. };
  73. union xfs_btree_key {
  74. xfs_bmbt_key_t bmbt;
  75. xfs_bmdr_key_t bmbr; /* bmbt root block */
  76. xfs_alloc_key_t alloc;
  77. xfs_inobt_key_t inobt;
  78. };
  79. union xfs_btree_rec {
  80. xfs_bmbt_rec_t bmbt;
  81. xfs_bmdr_rec_t bmbr; /* bmbt root block */
  82. xfs_alloc_rec_t alloc;
  83. xfs_inobt_rec_t inobt;
  84. };
  85. /*
  86. * For logging record fields.
  87. */
  88. #define XFS_BB_MAGIC 0x01
  89. #define XFS_BB_LEVEL 0x02
  90. #define XFS_BB_NUMRECS 0x04
  91. #define XFS_BB_LEFTSIB 0x08
  92. #define XFS_BB_RIGHTSIB 0x10
  93. #define XFS_BB_NUM_BITS 5
  94. #define XFS_BB_ALL_BITS ((1 << XFS_BB_NUM_BITS) - 1)
  95. /*
  96. * Magic numbers for btree blocks.
  97. */
  98. extern const __uint32_t xfs_magics[];
  99. /*
  100. * Generic stats interface
  101. */
  102. #define __XFS_BTREE_STATS_INC(type, stat) \
  103. XFS_STATS_INC(xs_ ## type ## _2_ ## stat)
  104. #define XFS_BTREE_STATS_INC(cur, stat) \
  105. do { \
  106. switch (cur->bc_btnum) { \
  107. case XFS_BTNUM_BNO: __XFS_BTREE_STATS_INC(abtb, stat); break; \
  108. case XFS_BTNUM_CNT: __XFS_BTREE_STATS_INC(abtc, stat); break; \
  109. case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break; \
  110. case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break; \
  111. case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
  112. } \
  113. } while (0)
  114. #define __XFS_BTREE_STATS_ADD(type, stat, val) \
  115. XFS_STATS_ADD(xs_ ## type ## _2_ ## stat, val)
  116. #define XFS_BTREE_STATS_ADD(cur, stat, val) \
  117. do { \
  118. switch (cur->bc_btnum) { \
  119. case XFS_BTNUM_BNO: __XFS_BTREE_STATS_ADD(abtb, stat, val); break; \
  120. case XFS_BTNUM_CNT: __XFS_BTREE_STATS_ADD(abtc, stat, val); break; \
  121. case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
  122. case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
  123. case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
  124. } \
  125. } while (0)
  126. #define XFS_BTREE_MAXLEVELS 8 /* max of all btrees */
  127. struct xfs_btree_ops {
  128. /* size of the key and record structures */
  129. size_t key_len;
  130. size_t rec_len;
  131. /* cursor operations */
  132. struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
  133. void (*update_cursor)(struct xfs_btree_cur *src,
  134. struct xfs_btree_cur *dst);
  135. /* update btree root pointer */
  136. void (*set_root)(struct xfs_btree_cur *cur,
  137. union xfs_btree_ptr *nptr, int level_change);
  138. /* block allocation / freeing */
  139. int (*alloc_block)(struct xfs_btree_cur *cur,
  140. union xfs_btree_ptr *start_bno,
  141. union xfs_btree_ptr *new_bno,
  142. int length, int *stat);
  143. int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
  144. /* update last record information */
  145. void (*update_lastrec)(struct xfs_btree_cur *cur,
  146. struct xfs_btree_block *block,
  147. union xfs_btree_rec *rec,
  148. int ptr, int reason);
  149. /* records in block/level */
  150. int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
  151. int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
  152. /* records on disk. Matter for the root in inode case. */
  153. int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
  154. /* init values of btree structures */
  155. void (*init_key_from_rec)(union xfs_btree_key *key,
  156. union xfs_btree_rec *rec);
  157. void (*init_rec_from_key)(union xfs_btree_key *key,
  158. union xfs_btree_rec *rec);
  159. void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
  160. union xfs_btree_rec *rec);
  161. void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
  162. union xfs_btree_ptr *ptr);
  163. /* difference between key value and cursor value */
  164. __int64_t (*key_diff)(struct xfs_btree_cur *cur,
  165. union xfs_btree_key *key);
  166. const struct xfs_buf_ops *buf_ops;
  167. #ifdef DEBUG
  168. /* check that k1 is lower than k2 */
  169. int (*keys_inorder)(struct xfs_btree_cur *cur,
  170. union xfs_btree_key *k1,
  171. union xfs_btree_key *k2);
  172. /* check that r1 is lower than r2 */
  173. int (*recs_inorder)(struct xfs_btree_cur *cur,
  174. union xfs_btree_rec *r1,
  175. union xfs_btree_rec *r2);
  176. #endif
  177. };
  178. /*
  179. * Reasons for the update_lastrec method to be called.
  180. */
  181. #define LASTREC_UPDATE 0
  182. #define LASTREC_INSREC 1
  183. #define LASTREC_DELREC 2
  184. /*
  185. * Btree cursor structure.
  186. * This collects all information needed by the btree code in one place.
  187. */
  188. typedef struct xfs_btree_cur
  189. {
  190. struct xfs_trans *bc_tp; /* transaction we're in, if any */
  191. struct xfs_mount *bc_mp; /* file system mount struct */
  192. const struct xfs_btree_ops *bc_ops;
  193. uint bc_flags; /* btree features - below */
  194. union {
  195. xfs_alloc_rec_incore_t a;
  196. xfs_bmbt_irec_t b;
  197. xfs_inobt_rec_incore_t i;
  198. } bc_rec; /* current insert/search record value */
  199. struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
  200. int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
  201. __uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
  202. #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
  203. #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
  204. __uint8_t bc_nlevels; /* number of levels in the tree */
  205. __uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
  206. xfs_btnum_t bc_btnum; /* identifies which btree type */
  207. union {
  208. struct { /* needed for BNO, CNT, INO */
  209. struct xfs_buf *agbp; /* agf/agi buffer pointer */
  210. xfs_agnumber_t agno; /* ag number */
  211. } a;
  212. struct { /* needed for BMAP */
  213. struct xfs_inode *ip; /* pointer to our inode */
  214. struct xfs_bmap_free *flist; /* list to free after */
  215. xfs_fsblock_t firstblock; /* 1st blk allocated */
  216. int allocated; /* count of alloced */
  217. short forksize; /* fork's inode space */
  218. char whichfork; /* data or attr fork */
  219. char flags; /* flags */
  220. #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
  221. } b;
  222. } bc_private; /* per-btree type data */
  223. } xfs_btree_cur_t;
  224. /* cursor flags */
  225. #define XFS_BTREE_LONG_PTRS (1<<0) /* pointers are 64bits long */
  226. #define XFS_BTREE_ROOT_IN_INODE (1<<1) /* root may be variable size */
  227. #define XFS_BTREE_LASTREC_UPDATE (1<<2) /* track last rec externally */
  228. #define XFS_BTREE_NOERROR 0
  229. #define XFS_BTREE_ERROR 1
  230. /*
  231. * Convert from buffer to btree block header.
  232. */
  233. #define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)((bp)->b_addr))
  234. /*
  235. * Check that block header is ok.
  236. */
  237. int
  238. xfs_btree_check_block(
  239. struct xfs_btree_cur *cur, /* btree cursor */
  240. struct xfs_btree_block *block, /* generic btree block pointer */
  241. int level, /* level of the btree block */
  242. struct xfs_buf *bp); /* buffer containing block, if any */
  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 ptr, /* btree block disk address */
  250. int level); /* btree block level */
  251. /*
  252. * Delete the btree cursor.
  253. */
  254. void
  255. xfs_btree_del_cursor(
  256. xfs_btree_cur_t *cur, /* btree cursor */
  257. int error); /* del because of error */
  258. /*
  259. * Duplicate the btree cursor.
  260. * Allocate a new one, copy the record, re-get the buffers.
  261. */
  262. int /* error */
  263. xfs_btree_dup_cursor(
  264. xfs_btree_cur_t *cur, /* input cursor */
  265. xfs_btree_cur_t **ncur);/* output cursor */
  266. /*
  267. * Get a buffer for the block, return it with no data read.
  268. * Long-form addressing.
  269. */
  270. struct xfs_buf * /* buffer for fsbno */
  271. xfs_btree_get_bufl(
  272. struct xfs_mount *mp, /* file system mount point */
  273. struct xfs_trans *tp, /* transaction pointer */
  274. xfs_fsblock_t fsbno, /* file system block number */
  275. uint lock); /* lock flags for get_buf */
  276. /*
  277. * Get a buffer for the block, return it with no data read.
  278. * Short-form addressing.
  279. */
  280. struct xfs_buf * /* buffer for agno/agbno */
  281. xfs_btree_get_bufs(
  282. struct xfs_mount *mp, /* file system mount point */
  283. struct xfs_trans *tp, /* transaction pointer */
  284. xfs_agnumber_t agno, /* allocation group number */
  285. xfs_agblock_t agbno, /* allocation group block number */
  286. uint lock); /* lock flags for get_buf */
  287. /*
  288. * Check for the cursor referring to the last block at the given level.
  289. */
  290. int /* 1=is last block, 0=not last block */
  291. xfs_btree_islastblock(
  292. xfs_btree_cur_t *cur, /* btree cursor */
  293. int level); /* level to check */
  294. /*
  295. * Compute first and last byte offsets for the fields given.
  296. * Interprets the offsets table, which contains struct field offsets.
  297. */
  298. void
  299. xfs_btree_offsets(
  300. __int64_t fields, /* bitmask of fields */
  301. const short *offsets,/* table of field offsets */
  302. int nbits, /* number of bits to inspect */
  303. int *first, /* output: first byte offset */
  304. int *last); /* output: last byte offset */
  305. /*
  306. * Get a buffer for the block, return it read in.
  307. * Long-form addressing.
  308. */
  309. int /* error */
  310. xfs_btree_read_bufl(
  311. struct xfs_mount *mp, /* file system mount point */
  312. struct xfs_trans *tp, /* transaction pointer */
  313. xfs_fsblock_t fsbno, /* file system block number */
  314. uint lock, /* lock flags for read_buf */
  315. struct xfs_buf **bpp, /* buffer for fsbno */
  316. int refval, /* ref count value for buffer */
  317. const struct xfs_buf_ops *ops);
  318. /*
  319. * Read-ahead the block, don't wait for it, don't return a buffer.
  320. * Long-form addressing.
  321. */
  322. void /* error */
  323. xfs_btree_reada_bufl(
  324. struct xfs_mount *mp, /* file system mount point */
  325. xfs_fsblock_t fsbno, /* file system block number */
  326. xfs_extlen_t count, /* count of filesystem blocks */
  327. const struct xfs_buf_ops *ops);
  328. /*
  329. * Read-ahead the block, don't wait for it, don't return a buffer.
  330. * Short-form addressing.
  331. */
  332. void /* error */
  333. xfs_btree_reada_bufs(
  334. struct xfs_mount *mp, /* file system mount point */
  335. xfs_agnumber_t agno, /* allocation group number */
  336. xfs_agblock_t agbno, /* allocation group block number */
  337. xfs_extlen_t count, /* count of filesystem blocks */
  338. const struct xfs_buf_ops *ops);
  339. /*
  340. * Initialise a new btree block header
  341. */
  342. void
  343. xfs_btree_init_block(
  344. struct xfs_mount *mp,
  345. struct xfs_buf *bp,
  346. __u32 magic,
  347. __u16 level,
  348. __u16 numrecs,
  349. unsigned int flags);
  350. /*
  351. * Common btree core entry points.
  352. */
  353. int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
  354. int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
  355. int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
  356. int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
  357. int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
  358. int xfs_btree_insert(struct xfs_btree_cur *, int *);
  359. int xfs_btree_delete(struct xfs_btree_cur *, int *);
  360. int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
  361. /*
  362. * Internal btree helpers also used by xfs_bmap.c.
  363. */
  364. void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
  365. void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
  366. /*
  367. * Helpers.
  368. */
  369. static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
  370. {
  371. return be16_to_cpu(block->bb_numrecs);
  372. }
  373. static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
  374. __uint16_t numrecs)
  375. {
  376. block->bb_numrecs = cpu_to_be16(numrecs);
  377. }
  378. static inline int xfs_btree_get_level(struct xfs_btree_block *block)
  379. {
  380. return be16_to_cpu(block->bb_level);
  381. }
  382. /*
  383. * Min and max functions for extlen, agblock, fileoff, and filblks types.
  384. */
  385. #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
  386. #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
  387. #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
  388. #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
  389. #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
  390. #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
  391. #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
  392. #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
  393. #define XFS_FSB_SANITY_CHECK(mp,fsb) \
  394. (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
  395. XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
  396. /*
  397. * Trace hooks. Currently not implemented as they need to be ported
  398. * over to the generic tracing functionality, which is some effort.
  399. *
  400. * i,j = integer (32 bit)
  401. * b = btree block buffer (xfs_buf_t)
  402. * p = btree ptr
  403. * r = btree record
  404. * k = btree key
  405. */
  406. #define XFS_BTREE_TRACE_ARGBI(c, b, i)
  407. #define XFS_BTREE_TRACE_ARGBII(c, b, i, j)
  408. #define XFS_BTREE_TRACE_ARGI(c, i)
  409. #define XFS_BTREE_TRACE_ARGIPK(c, i, p, s)
  410. #define XFS_BTREE_TRACE_ARGIPR(c, i, p, r)
  411. #define XFS_BTREE_TRACE_ARGIK(c, i, k)
  412. #define XFS_BTREE_TRACE_ARGR(c, r)
  413. #define XFS_BTREE_TRACE_CURSOR(c, t)
  414. #endif /* __XFS_BTREE_H__ */