PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xfs/xfs_btree.h

https://github.com/dmitriy103/bravo_kernel-2.6.35
C Header | 467 lines | 286 code | 57 blank | 124 comment | 5 complexity | 34c015e60b1fa2768ff1c2205dd1f446 MD5 | raw file
  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. int (*kill_root)(struct xfs_btree_cur *cur, struct xfs_buf *bp,
  139. int level, union xfs_btree_ptr *newroot);
  140. /* block allocation / freeing */
  141. int (*alloc_block)(struct xfs_btree_cur *cur,
  142. union xfs_btree_ptr *start_bno,
  143. union xfs_btree_ptr *new_bno,
  144. int length, int *stat);
  145. int (*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
  146. /* update last record information */
  147. void (*update_lastrec)(struct xfs_btree_cur *cur,
  148. struct xfs_btree_block *block,
  149. union xfs_btree_rec *rec,
  150. int ptr, int reason);
  151. /* records in block/level */
  152. int (*get_minrecs)(struct xfs_btree_cur *cur, int level);
  153. int (*get_maxrecs)(struct xfs_btree_cur *cur, int level);
  154. /* records on disk. Matter for the root in inode case. */
  155. int (*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
  156. /* init values of btree structures */
  157. void (*init_key_from_rec)(union xfs_btree_key *key,
  158. union xfs_btree_rec *rec);
  159. void (*init_rec_from_key)(union xfs_btree_key *key,
  160. union xfs_btree_rec *rec);
  161. void (*init_rec_from_cur)(struct xfs_btree_cur *cur,
  162. union xfs_btree_rec *rec);
  163. void (*init_ptr_from_cur)(struct xfs_btree_cur *cur,
  164. union xfs_btree_ptr *ptr);
  165. /* difference between key value and cursor value */
  166. __int64_t (*key_diff)(struct xfs_btree_cur *cur,
  167. union xfs_btree_key *key);
  168. #ifdef DEBUG
  169. /* check that k1 is lower than k2 */
  170. int (*keys_inorder)(struct xfs_btree_cur *cur,
  171. union xfs_btree_key *k1,
  172. union xfs_btree_key *k2);
  173. /* check that r1 is lower than r2 */
  174. int (*recs_inorder)(struct xfs_btree_cur *cur,
  175. union xfs_btree_rec *r1,
  176. union xfs_btree_rec *r2);
  177. #endif
  178. /* btree tracing */
  179. #ifdef XFS_BTREE_TRACE
  180. void (*trace_enter)(struct xfs_btree_cur *, const char *,
  181. char *, int, int, __psunsigned_t,
  182. __psunsigned_t, __psunsigned_t,
  183. __psunsigned_t, __psunsigned_t,
  184. __psunsigned_t, __psunsigned_t,
  185. __psunsigned_t, __psunsigned_t,
  186. __psunsigned_t, __psunsigned_t);
  187. void (*trace_cursor)(struct xfs_btree_cur *, __uint32_t *,
  188. __uint64_t *, __uint64_t *);
  189. void (*trace_key)(struct xfs_btree_cur *,
  190. union xfs_btree_key *, __uint64_t *,
  191. __uint64_t *);
  192. void (*trace_record)(struct xfs_btree_cur *,
  193. union xfs_btree_rec *, __uint64_t *,
  194. __uint64_t *, __uint64_t *);
  195. #endif
  196. };
  197. /*
  198. * Reasons for the update_lastrec method to be called.
  199. */
  200. #define LASTREC_UPDATE 0
  201. #define LASTREC_INSREC 1
  202. #define LASTREC_DELREC 2
  203. /*
  204. * Btree cursor structure.
  205. * This collects all information needed by the btree code in one place.
  206. */
  207. typedef struct xfs_btree_cur
  208. {
  209. struct xfs_trans *bc_tp; /* transaction we're in, if any */
  210. struct xfs_mount *bc_mp; /* file system mount struct */
  211. const struct xfs_btree_ops *bc_ops;
  212. uint bc_flags; /* btree features - below */
  213. union {
  214. xfs_alloc_rec_incore_t a;
  215. xfs_bmbt_irec_t b;
  216. xfs_inobt_rec_incore_t i;
  217. } bc_rec; /* current insert/search record value */
  218. struct xfs_buf *bc_bufs[XFS_BTREE_MAXLEVELS]; /* buf ptr per level */
  219. int bc_ptrs[XFS_BTREE_MAXLEVELS]; /* key/record # */
  220. __uint8_t bc_ra[XFS_BTREE_MAXLEVELS]; /* readahead bits */
  221. #define XFS_BTCUR_LEFTRA 1 /* left sibling has been read-ahead */
  222. #define XFS_BTCUR_RIGHTRA 2 /* right sibling has been read-ahead */
  223. __uint8_t bc_nlevels; /* number of levels in the tree */
  224. __uint8_t bc_blocklog; /* log2(blocksize) of btree blocks */
  225. xfs_btnum_t bc_btnum; /* identifies which btree type */
  226. union {
  227. struct { /* needed for BNO, CNT, INO */
  228. struct xfs_buf *agbp; /* agf/agi buffer pointer */
  229. xfs_agnumber_t agno; /* ag number */
  230. } a;
  231. struct { /* needed for BMAP */
  232. struct xfs_inode *ip; /* pointer to our inode */
  233. struct xfs_bmap_free *flist; /* list to free after */
  234. xfs_fsblock_t firstblock; /* 1st blk allocated */
  235. int allocated; /* count of alloced */
  236. short forksize; /* fork's inode space */
  237. char whichfork; /* data or attr fork */
  238. char flags; /* flags */
  239. #define XFS_BTCUR_BPRV_WASDEL 1 /* was delayed */
  240. } b;
  241. } bc_private; /* per-btree type data */
  242. } xfs_btree_cur_t;
  243. /* cursor flags */
  244. #define XFS_BTREE_LONG_PTRS (1<<0) /* pointers are 64bits long */
  245. #define XFS_BTREE_ROOT_IN_INODE (1<<1) /* root may be variable size */
  246. #define XFS_BTREE_LASTREC_UPDATE (1<<2) /* track last rec externally */
  247. #define XFS_BTREE_NOERROR 0
  248. #define XFS_BTREE_ERROR 1
  249. /*
  250. * Convert from buffer to btree block header.
  251. */
  252. #define XFS_BUF_TO_BLOCK(bp) ((struct xfs_btree_block *)XFS_BUF_PTR(bp))
  253. /*
  254. * Check that block header is ok.
  255. */
  256. int
  257. xfs_btree_check_block(
  258. struct xfs_btree_cur *cur, /* btree cursor */
  259. struct xfs_btree_block *block, /* generic btree block pointer */
  260. int level, /* level of the btree block */
  261. struct xfs_buf *bp); /* buffer containing block, if any */
  262. /*
  263. * Check that (long) pointer is ok.
  264. */
  265. int /* error (0 or EFSCORRUPTED) */
  266. xfs_btree_check_lptr(
  267. struct xfs_btree_cur *cur, /* btree cursor */
  268. xfs_dfsbno_t ptr, /* btree block disk address */
  269. int level); /* btree block level */
  270. /*
  271. * Delete the btree cursor.
  272. */
  273. void
  274. xfs_btree_del_cursor(
  275. xfs_btree_cur_t *cur, /* btree cursor */
  276. int error); /* del because of error */
  277. /*
  278. * Duplicate the btree cursor.
  279. * Allocate a new one, copy the record, re-get the buffers.
  280. */
  281. int /* error */
  282. xfs_btree_dup_cursor(
  283. xfs_btree_cur_t *cur, /* input cursor */
  284. xfs_btree_cur_t **ncur);/* output cursor */
  285. /*
  286. * Get a buffer for the block, return it with no data read.
  287. * Long-form addressing.
  288. */
  289. struct xfs_buf * /* buffer for fsbno */
  290. xfs_btree_get_bufl(
  291. struct xfs_mount *mp, /* file system mount point */
  292. struct xfs_trans *tp, /* transaction pointer */
  293. xfs_fsblock_t fsbno, /* file system block number */
  294. uint lock); /* lock flags for get_buf */
  295. /*
  296. * Get a buffer for the block, return it with no data read.
  297. * Short-form addressing.
  298. */
  299. struct xfs_buf * /* buffer for agno/agbno */
  300. xfs_btree_get_bufs(
  301. struct xfs_mount *mp, /* file system mount point */
  302. struct xfs_trans *tp, /* transaction pointer */
  303. xfs_agnumber_t agno, /* allocation group number */
  304. xfs_agblock_t agbno, /* allocation group block number */
  305. uint lock); /* lock flags for get_buf */
  306. /*
  307. * Check for the cursor referring to the last block at the given level.
  308. */
  309. int /* 1=is last block, 0=not last block */
  310. xfs_btree_islastblock(
  311. xfs_btree_cur_t *cur, /* btree cursor */
  312. int level); /* level to check */
  313. /*
  314. * Compute first and last byte offsets for the fields given.
  315. * Interprets the offsets table, which contains struct field offsets.
  316. */
  317. void
  318. xfs_btree_offsets(
  319. __int64_t fields, /* bitmask of fields */
  320. const short *offsets,/* table of field offsets */
  321. int nbits, /* number of bits to inspect */
  322. int *first, /* output: first byte offset */
  323. int *last); /* output: last byte offset */
  324. /*
  325. * Get a buffer for the block, return it read in.
  326. * Long-form addressing.
  327. */
  328. int /* error */
  329. xfs_btree_read_bufl(
  330. struct xfs_mount *mp, /* file system mount point */
  331. struct xfs_trans *tp, /* transaction pointer */
  332. xfs_fsblock_t fsbno, /* file system block number */
  333. uint lock, /* lock flags for read_buf */
  334. struct xfs_buf **bpp, /* buffer for fsbno */
  335. int refval);/* ref count value for buffer */
  336. /*
  337. * Read-ahead the block, don't wait for it, don't return a buffer.
  338. * Long-form addressing.
  339. */
  340. void /* error */
  341. xfs_btree_reada_bufl(
  342. struct xfs_mount *mp, /* file system mount point */
  343. xfs_fsblock_t fsbno, /* file system block number */
  344. xfs_extlen_t count); /* count of filesystem blocks */
  345. /*
  346. * Read-ahead the block, don't wait for it, don't return a buffer.
  347. * Short-form addressing.
  348. */
  349. void /* error */
  350. xfs_btree_reada_bufs(
  351. struct xfs_mount *mp, /* file system mount point */
  352. xfs_agnumber_t agno, /* allocation group number */
  353. xfs_agblock_t agbno, /* allocation group block number */
  354. xfs_extlen_t count); /* count of filesystem blocks */
  355. /*
  356. * Set the buffer for level "lev" in the cursor to bp, releasing
  357. * any previous buffer.
  358. */
  359. void
  360. xfs_btree_setbuf(
  361. xfs_btree_cur_t *cur, /* btree cursor */
  362. int lev, /* level in btree */
  363. struct xfs_buf *bp); /* new buffer to set */
  364. /*
  365. * Common btree core entry points.
  366. */
  367. int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
  368. int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
  369. int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
  370. int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
  371. int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
  372. int xfs_btree_insert(struct xfs_btree_cur *, int *);
  373. int xfs_btree_delete(struct xfs_btree_cur *, int *);
  374. int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
  375. /*
  376. * Internal btree helpers also used by xfs_bmap.c.
  377. */
  378. void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
  379. void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
  380. /*
  381. * Helpers.
  382. */
  383. static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
  384. {
  385. return be16_to_cpu(block->bb_numrecs);
  386. }
  387. static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
  388. __uint16_t numrecs)
  389. {
  390. block->bb_numrecs = cpu_to_be16(numrecs);
  391. }
  392. static inline int xfs_btree_get_level(struct xfs_btree_block *block)
  393. {
  394. return be16_to_cpu(block->bb_level);
  395. }
  396. /*
  397. * Min and max functions for extlen, agblock, fileoff, and filblks types.
  398. */
  399. #define XFS_EXTLEN_MIN(a,b) min_t(xfs_extlen_t, (a), (b))
  400. #define XFS_EXTLEN_MAX(a,b) max_t(xfs_extlen_t, (a), (b))
  401. #define XFS_AGBLOCK_MIN(a,b) min_t(xfs_agblock_t, (a), (b))
  402. #define XFS_AGBLOCK_MAX(a,b) max_t(xfs_agblock_t, (a), (b))
  403. #define XFS_FILEOFF_MIN(a,b) min_t(xfs_fileoff_t, (a), (b))
  404. #define XFS_FILEOFF_MAX(a,b) max_t(xfs_fileoff_t, (a), (b))
  405. #define XFS_FILBLKS_MIN(a,b) min_t(xfs_filblks_t, (a), (b))
  406. #define XFS_FILBLKS_MAX(a,b) max_t(xfs_filblks_t, (a), (b))
  407. #define XFS_FSB_SANITY_CHECK(mp,fsb) \
  408. (XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
  409. XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
  410. #endif /* __XFS_BTREE_H__ */