xfs_da_btree.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_bmap_free;
  21. struct xfs_inode;
  22. struct xfs_mount;
  23. struct xfs_trans;
  24. struct zone;
  25. /*========================================================================
  26. * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
  27. *========================================================================*/
  28. /*
  29. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  30. *
  31. * It is used to manage a doubly linked list of all blocks at the same
  32. * level in the Btree, and to identify which type of block this is.
  33. */
  34. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  35. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  36. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  37. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  38. typedef struct xfs_da_blkinfo {
  39. __be32 forw; /* previous block in list */
  40. __be32 back; /* following block in list */
  41. __be16 magic; /* validity check on block */
  42. __be16 pad; /* unused */
  43. } xfs_da_blkinfo_t;
  44. /*
  45. * This is the structure of the root and intermediate nodes in the Btree.
  46. * The leaf nodes are defined above.
  47. *
  48. * Entries are not packed.
  49. *
  50. * Since we have duplicate keys, use a binary search but always follow
  51. * all match in the block, not just the first match found.
  52. */
  53. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  54. typedef struct xfs_da_intnode {
  55. struct xfs_da_node_hdr { /* constant-structure header block */
  56. xfs_da_blkinfo_t info; /* block type, links, etc. */
  57. __be16 count; /* count of active entries */
  58. __be16 level; /* level above leaves (leaf == 0) */
  59. } hdr;
  60. struct xfs_da_node_entry {
  61. __be32 hashval; /* hash value for this descendant */
  62. __be32 before; /* Btree block before this key */
  63. } btree[1]; /* variable sized array of keys */
  64. } xfs_da_intnode_t;
  65. typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
  66. typedef struct xfs_da_node_entry xfs_da_node_entry_t;
  67. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  68. /*========================================================================
  69. * Btree searching and modification structure definitions.
  70. *========================================================================*/
  71. /*
  72. * Search comparison results
  73. */
  74. enum xfs_dacmp {
  75. XFS_CMP_DIFFERENT, /* names are completely different */
  76. XFS_CMP_EXACT, /* names are exactly the same */
  77. XFS_CMP_CASE /* names are same but differ in case */
  78. };
  79. /*
  80. * Structure to ease passing around component names.
  81. */
  82. typedef struct xfs_da_args {
  83. const __uint8_t *name; /* string (maybe not NULL terminated) */
  84. int namelen; /* length of string (maybe no NULL) */
  85. __uint8_t *value; /* set of bytes (maybe contain NULLs) */
  86. int valuelen; /* length of value */
  87. int flags; /* argument flags (eg: ATTR_NOCREATE) */
  88. xfs_dahash_t hashval; /* hash value of name */
  89. xfs_ino_t inumber; /* input/output inode number */
  90. struct xfs_inode *dp; /* directory inode to manipulate */
  91. xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
  92. struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
  93. struct xfs_trans *trans; /* current trans (changes over time) */
  94. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  95. int whichfork; /* data or attribute fork */
  96. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  97. int index; /* index of attr of interest in blk */
  98. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  99. int rmtblkcnt; /* remote attr value block count */
  100. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  101. int index2; /* index of 2nd attr in blk */
  102. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  103. int rmtblkcnt2; /* remote attr value block count */
  104. int op_flags; /* operation flags */
  105. enum xfs_dacmp cmpresult; /* name compare result for lookups */
  106. } xfs_da_args_t;
  107. /*
  108. * Operation flags:
  109. */
  110. #define XFS_DA_OP_JUSTCHECK 0x0001 /* check for ok with no space */
  111. #define XFS_DA_OP_RENAME 0x0002 /* this is an atomic rename op */
  112. #define XFS_DA_OP_ADDNAME 0x0004 /* this is an add operation */
  113. #define XFS_DA_OP_OKNOENT 0x0008 /* lookup/add op, ENOENT ok, else die */
  114. #define XFS_DA_OP_CILOOKUP 0x0010 /* lookup to return CI name if found */
  115. #define XFS_DA_OP_FLAGS \
  116. { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \
  117. { XFS_DA_OP_RENAME, "RENAME" }, \
  118. { XFS_DA_OP_ADDNAME, "ADDNAME" }, \
  119. { XFS_DA_OP_OKNOENT, "OKNOENT" }, \
  120. { XFS_DA_OP_CILOOKUP, "CILOOKUP" }
  121. /*
  122. * Storage for holding state during Btree searches and split/join ops.
  123. *
  124. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  125. * fanout to the Btree, we can support over 900 million directory blocks,
  126. * which is slightly more than enough.
  127. */
  128. typedef struct xfs_da_state_blk {
  129. struct xfs_buf *bp; /* buffer containing block */
  130. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  131. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  132. int index; /* relevant index into block */
  133. xfs_dahash_t hashval; /* last hash value in block */
  134. int magic; /* blk's magic number, ie: blk type */
  135. } xfs_da_state_blk_t;
  136. typedef struct xfs_da_state_path {
  137. int active; /* number of active levels */
  138. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  139. } xfs_da_state_path_t;
  140. typedef struct xfs_da_state {
  141. xfs_da_args_t *args; /* filename arguments */
  142. struct xfs_mount *mp; /* filesystem mount point */
  143. unsigned int blocksize; /* logical block size */
  144. unsigned int node_ents; /* how many entries in danode */
  145. xfs_da_state_path_t path; /* search/split paths */
  146. xfs_da_state_path_t altpath; /* alternate path for join */
  147. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  148. unsigned char extravalid; /* T/F: extrablk is in use */
  149. unsigned char extraafter; /* T/F: extrablk is after new */
  150. xfs_da_state_blk_t extrablk; /* for double-splits on leaves */
  151. /* for dirv2 extrablk is data */
  152. } xfs_da_state_t;
  153. /*
  154. * Utility macros to aid in logging changed structure fields.
  155. */
  156. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  157. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  158. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  159. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  160. /*
  161. * Name ops for directory and/or attr name operations
  162. */
  163. struct xfs_nameops {
  164. xfs_dahash_t (*hashname)(struct xfs_name *);
  165. enum xfs_dacmp (*compname)(struct xfs_da_args *,
  166. const unsigned char *, int);
  167. };
  168. /*========================================================================
  169. * Function prototypes.
  170. *========================================================================*/
  171. /*
  172. * Routines used for growing the Btree.
  173. */
  174. int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
  175. struct xfs_buf **bpp, int whichfork);
  176. int xfs_da_split(xfs_da_state_t *state);
  177. /*
  178. * Routines used for shrinking the Btree.
  179. */
  180. int xfs_da_join(xfs_da_state_t *state);
  181. void xfs_da_fixhashpath(xfs_da_state_t *state,
  182. xfs_da_state_path_t *path_to_to_fix);
  183. /*
  184. * Routines used for finding things in the Btree.
  185. */
  186. int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
  187. int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  188. int forward, int release, int *result);
  189. /*
  190. * Utility routines.
  191. */
  192. int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  193. xfs_da_state_blk_t *new_blk);
  194. int xfs_da_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
  195. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  196. struct xfs_buf **bpp, int which_fork);
  197. /*
  198. * Utility routines.
  199. */
  200. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  201. int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
  202. int count);
  203. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  204. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  205. struct xfs_buf **bp, int whichfork);
  206. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  207. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  208. struct xfs_buf **bpp, int whichfork,
  209. const struct xfs_buf_ops *ops);
  210. xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  211. xfs_dablk_t bno, xfs_daddr_t mapped_bno,
  212. int whichfork, const struct xfs_buf_ops *ops);
  213. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  214. struct xfs_buf *dead_buf);
  215. uint xfs_da_hashname(const __uint8_t *name_string, int name_length);
  216. enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
  217. const unsigned char *name, int len);
  218. xfs_da_state_t *xfs_da_state_alloc(void);
  219. void xfs_da_state_free(xfs_da_state_t *state);
  220. extern struct kmem_zone *xfs_da_state_zone;
  221. extern const struct xfs_nameops xfs_default_nameops;
  222. #endif /* __XFS_DA_BTREE_H__ */