xfs_btree.h 16 KB

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