xfs_da_btree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2000,2002,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_DA_BTREE_H__
  19. #define __XFS_DA_BTREE_H__
  20. struct xfs_buf;
  21. struct xfs_bmap_free;
  22. struct xfs_inode;
  23. struct xfs_mount;
  24. struct xfs_trans;
  25. struct zone;
  26. /*========================================================================
  27. * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
  28. *========================================================================*/
  29. /*
  30. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  31. *
  32. * Is is used to manage a doubly linked list of all blocks at the same
  33. * level in the Btree, and to identify which type of block this is.
  34. */
  35. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  36. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  37. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  38. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  39. typedef struct xfs_da_blkinfo {
  40. __be32 forw; /* previous block in list */
  41. __be32 back; /* following block in list */
  42. __be16 magic; /* validity check on block */
  43. __be16 pad; /* unused */
  44. } xfs_da_blkinfo_t;
  45. /*
  46. * This is the structure of the root and intermediate nodes in the Btree.
  47. * The leaf nodes are defined above.
  48. *
  49. * Entries are not packed.
  50. *
  51. * Since we have duplicate keys, use a binary search but always follow
  52. * all match in the block, not just the first match found.
  53. */
  54. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  55. typedef struct xfs_da_intnode {
  56. struct xfs_da_node_hdr { /* constant-structure header block */
  57. xfs_da_blkinfo_t info; /* block type, links, etc. */
  58. __be16 count; /* count of active entries */
  59. __be16 level; /* level above leaves (leaf == 0) */
  60. } hdr;
  61. struct xfs_da_node_entry {
  62. __be32 hashval; /* hash value for this descendant */
  63. __be32 before; /* Btree block before this key */
  64. } btree[1]; /* variable sized array of keys */
  65. } xfs_da_intnode_t;
  66. typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
  67. typedef struct xfs_da_node_entry xfs_da_node_entry_t;
  68. #define XFS_DA_MAXHASH ((xfs_dahash_t)-1) /* largest valid hash value */
  69. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  70. #define XFS_LBLOG(mp) (mp)->m_sb.sb_blocklog
  71. #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
  72. (((bno) << (mp)->m_dircook_elog) | (entry))
  73. #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
  74. (((xfs_off_t)XFS_DA_MAKE_BNOENTRY(mp, bno, entry) << 32) | (hash))
  75. #define XFS_DA_COOKIE_HASH(mp,cookie) ((xfs_dahash_t)cookie)
  76. #define XFS_DA_COOKIE_BNO(mp,cookie) \
  77. ((((xfs_off_t)(cookie) >> 31) == -1LL ? \
  78. (xfs_dablk_t)0 : \
  79. (xfs_dablk_t)((xfs_off_t)(cookie) >> \
  80. ((mp)->m_dircook_elog + 32))))
  81. #define XFS_DA_COOKIE_ENTRY(mp,cookie) \
  82. ((((xfs_off_t)(cookie) >> 31) == -1LL ? \
  83. (xfs_dablk_t)0 : \
  84. (xfs_dablk_t)(((xfs_off_t)(cookie) >> 32) & \
  85. ((1 << (mp)->m_dircook_elog) - 1))))
  86. /*========================================================================
  87. * Btree searching and modification structure definitions.
  88. *========================================================================*/
  89. /*
  90. * Search comparison results
  91. */
  92. enum xfs_dacmp {
  93. XFS_CMP_DIFFERENT, /* names are completely different */
  94. XFS_CMP_EXACT, /* names are exactly the same */
  95. XFS_CMP_CASE /* names are same but differ in case */
  96. };
  97. /*
  98. * Structure to ease passing around component names.
  99. */
  100. typedef struct xfs_da_args {
  101. const uchar_t *name; /* string (maybe not NULL terminated) */
  102. int namelen; /* length of string (maybe no NULL) */
  103. uchar_t *value; /* set of bytes (maybe contain NULLs) */
  104. int valuelen; /* length of value */
  105. int flags; /* argument flags (eg: ATTR_NOCREATE) */
  106. xfs_dahash_t hashval; /* hash value of name */
  107. xfs_ino_t inumber; /* input/output inode number */
  108. struct xfs_inode *dp; /* directory inode to manipulate */
  109. xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
  110. struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
  111. struct xfs_trans *trans; /* current trans (changes over time) */
  112. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  113. int whichfork; /* data or attribute fork */
  114. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  115. int index; /* index of attr of interest in blk */
  116. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  117. int rmtblkcnt; /* remote attr value block count */
  118. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  119. int index2; /* index of 2nd attr in blk */
  120. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  121. int rmtblkcnt2; /* remote attr value block count */
  122. unsigned char justcheck; /* T/F: check for ok with no space */
  123. unsigned char rename; /* T/F: this is an atomic rename op */
  124. unsigned char addname; /* T/F: this is an add operation */
  125. unsigned char oknoent; /* T/F: ok to return ENOENT, else die */
  126. enum xfs_dacmp cmpresult; /* name compare result for lookups */
  127. } xfs_da_args_t;
  128. /*
  129. * Structure to describe buffer(s) for a block.
  130. * This is needed in the directory version 2 format case, when
  131. * multiple non-contiguous fsblocks might be needed to cover one
  132. * logical directory block.
  133. * If the buffer count is 1 then the data pointer points to the
  134. * same place as the b_addr field for the buffer, else to kmem_alloced memory.
  135. */
  136. typedef struct xfs_dabuf {
  137. int nbuf; /* number of buffer pointers present */
  138. short dirty; /* data needs to be copied back */
  139. short bbcount; /* how large is data in bbs */
  140. void *data; /* pointer for buffers' data */
  141. #ifdef XFS_DABUF_DEBUG
  142. inst_t *ra; /* return address of caller to make */
  143. struct xfs_dabuf *next; /* next in global chain */
  144. struct xfs_dabuf *prev; /* previous in global chain */
  145. struct xfs_buftarg *target; /* device for buffer */
  146. xfs_daddr_t blkno; /* daddr first in bps[0] */
  147. #endif
  148. struct xfs_buf *bps[1]; /* actually nbuf of these */
  149. } xfs_dabuf_t;
  150. #define XFS_DA_BUF_SIZE(n) \
  151. (sizeof(xfs_dabuf_t) + sizeof(struct xfs_buf *) * ((n) - 1))
  152. #ifdef XFS_DABUF_DEBUG
  153. extern xfs_dabuf_t *xfs_dabuf_global_list;
  154. #endif
  155. /*
  156. * Storage for holding state during Btree searches and split/join ops.
  157. *
  158. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  159. * fanout to the Btree, we can support over 900 million directory blocks,
  160. * which is slightly more than enough.
  161. */
  162. typedef struct xfs_da_state_blk {
  163. xfs_dabuf_t *bp; /* buffer containing block */
  164. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  165. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  166. int index; /* relevant index into block */
  167. xfs_dahash_t hashval; /* last hash value in block */
  168. int magic; /* blk's magic number, ie: blk type */
  169. } xfs_da_state_blk_t;
  170. typedef struct xfs_da_state_path {
  171. int active; /* number of active levels */
  172. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  173. } xfs_da_state_path_t;
  174. typedef struct xfs_da_state {
  175. xfs_da_args_t *args; /* filename arguments */
  176. struct xfs_mount *mp; /* filesystem mount point */
  177. unsigned int blocksize; /* logical block size */
  178. unsigned int node_ents; /* how many entries in danode */
  179. xfs_da_state_path_t path; /* search/split paths */
  180. xfs_da_state_path_t altpath; /* alternate path for join */
  181. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  182. unsigned char extravalid; /* T/F: extrablk is in use */
  183. unsigned char extraafter; /* T/F: extrablk is after new */
  184. xfs_da_state_blk_t extrablk; /* for double-splits on leafs */
  185. /* for dirv2 extrablk is data */
  186. } xfs_da_state_t;
  187. /*
  188. * Utility macros to aid in logging changed structure fields.
  189. */
  190. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  191. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  192. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  193. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  194. /*
  195. * Name ops for directory and/or attr name operations
  196. */
  197. struct xfs_nameops {
  198. xfs_dahash_t (*hashname)(struct xfs_name *);
  199. enum xfs_dacmp (*compname)(struct xfs_da_args *, const char *, int);
  200. };
  201. #ifdef __KERNEL__
  202. /*========================================================================
  203. * Function prototypes for the kernel.
  204. *========================================================================*/
  205. /*
  206. * Routines used for growing the Btree.
  207. */
  208. int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
  209. xfs_dabuf_t **bpp, int whichfork);
  210. int xfs_da_split(xfs_da_state_t *state);
  211. /*
  212. * Routines used for shrinking the Btree.
  213. */
  214. int xfs_da_join(xfs_da_state_t *state);
  215. void xfs_da_fixhashpath(xfs_da_state_t *state,
  216. xfs_da_state_path_t *path_to_to_fix);
  217. /*
  218. * Routines used for finding things in the Btree.
  219. */
  220. int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
  221. int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  222. int forward, int release, int *result);
  223. /*
  224. * Utility routines.
  225. */
  226. int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  227. xfs_da_state_blk_t *new_blk);
  228. /*
  229. * Utility routines.
  230. */
  231. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  232. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  233. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  234. xfs_dabuf_t **bp, int whichfork);
  235. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  236. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  237. xfs_dabuf_t **bpp, int whichfork);
  238. xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  239. xfs_dablk_t bno, int whichfork);
  240. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  241. xfs_dabuf_t *dead_buf);
  242. uint xfs_da_hashname(const uchar_t *name_string, int name_length);
  243. enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
  244. const char *name, int len);
  245. xfs_da_state_t *xfs_da_state_alloc(void);
  246. void xfs_da_state_free(xfs_da_state_t *state);
  247. void xfs_da_buf_done(xfs_dabuf_t *dabuf);
  248. void xfs_da_log_buf(struct xfs_trans *tp, xfs_dabuf_t *dabuf, uint first,
  249. uint last);
  250. void xfs_da_brelse(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
  251. void xfs_da_binval(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
  252. xfs_daddr_t xfs_da_blkno(xfs_dabuf_t *dabuf);
  253. extern struct kmem_zone *xfs_da_state_zone;
  254. extern struct kmem_zone *xfs_dabuf_zone;
  255. #endif /* __KERNEL__ */
  256. #endif /* __XFS_DA_BTREE_H__ */