xfs_btree.h 14 KB

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