xfs_btree.h 16 KB

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