xfs_btree.h 16 KB

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