xfs_dir_leaf.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_DIR_LEAF_H__
  19. #define __XFS_DIR_LEAF_H__
  20. /*
  21. * Directory layout, internal structure, access macros, etc.
  22. *
  23. * Large directories are structured around Btrees where all the data
  24. * elements are in the leaf nodes. Filenames are hashed into an int,
  25. * then that int is used as the index into the Btree. Since the hashval
  26. * of a filename may not be unique, we may have duplicate keys. The
  27. * internal links in the Btree are logical block offsets into the file.
  28. */
  29. struct uio;
  30. struct xfs_bmap_free;
  31. struct xfs_dabuf;
  32. struct xfs_da_args;
  33. struct xfs_da_state;
  34. struct xfs_da_state_blk;
  35. struct xfs_dir_put_args;
  36. struct xfs_inode;
  37. struct xfs_mount;
  38. struct xfs_trans;
  39. /*========================================================================
  40. * Directory Structure when equal to XFS_LBSIZE(mp) bytes.
  41. *========================================================================*/
  42. /*
  43. * This is the structure of the leaf nodes in the Btree.
  44. *
  45. * Struct leaf_entry's are packed from the top. Names grow from the bottom
  46. * but are not packed. The freemap contains run-length-encoded entries
  47. * for the free bytes after the leaf_entry's, but only the N largest such,
  48. * smaller runs are dropped. When the freemap doesn't show enough space
  49. * for an allocation, we compact the namelist area and try again. If we
  50. * still don't have enough space, then we have to split the block.
  51. *
  52. * Since we have duplicate hash keys, for each key that matches, compare
  53. * the actual string. The root and intermediate node search always takes
  54. * the first-in-the-block key match found, so we should only have to work
  55. * "forw"ard. If none matches, continue with the "forw"ard leaf nodes
  56. * until the hash key changes or the filename is found.
  57. *
  58. * The parent directory and the self-pointer are explicitly represented
  59. * (ie: there are entries for "." and "..").
  60. *
  61. * Note that the count being a __uint16_t limits us to something like a
  62. * blocksize of 1.3MB in the face of worst case (short) filenames.
  63. */
  64. #define XFS_DIR_LEAF_MAPSIZE 3 /* how many freespace slots */
  65. typedef struct xfs_dir_leaf_map { /* RLE map of free bytes */
  66. __be16 base; /* base of free region */
  67. __be16 size; /* run length of free region */
  68. } xfs_dir_leaf_map_t;
  69. typedef struct xfs_dir_leaf_hdr { /* constant-structure header block */
  70. xfs_da_blkinfo_t info; /* block type, links, etc. */
  71. __be16 count; /* count of active leaf_entry's */
  72. __be16 namebytes; /* num bytes of name strings stored */
  73. __be16 firstused; /* first used byte in name area */
  74. __u8 holes; /* != 0 if blk needs compaction */
  75. __u8 pad1;
  76. xfs_dir_leaf_map_t freemap[XFS_DIR_LEAF_MAPSIZE];
  77. } xfs_dir_leaf_hdr_t;
  78. typedef struct xfs_dir_leaf_entry { /* sorted on key, not name */
  79. xfs_dahash_t hashval; /* hash value of name */
  80. __uint16_t nameidx; /* index into buffer of name */
  81. __uint8_t namelen; /* length of name string */
  82. __uint8_t pad2;
  83. } xfs_dir_leaf_entry_t;
  84. typedef struct xfs_dir_leaf_name {
  85. xfs_dir_ino_t inumber; /* inode number for this key */
  86. __uint8_t name[1]; /* name string itself */
  87. } xfs_dir_leaf_name_t;
  88. typedef struct xfs_dir_leafblock {
  89. xfs_dir_leaf_hdr_t hdr; /* constant-structure header block */
  90. xfs_dir_leaf_entry_t entries[1]; /* var sized array */
  91. xfs_dir_leaf_name_t namelist[1]; /* grows from bottom of buf */
  92. } xfs_dir_leafblock_t;
  93. /*
  94. * Length of name for which a 512-byte block filesystem
  95. * can get a double split.
  96. */
  97. #define XFS_DIR_LEAF_CAN_DOUBLE_SPLIT_LEN \
  98. (512 - (uint)sizeof(xfs_dir_leaf_hdr_t) - \
  99. (uint)sizeof(xfs_dir_leaf_entry_t) * 2 - \
  100. (uint)sizeof(xfs_dir_leaf_name_t) * 2 - (MAXNAMELEN - 2) + 1 + 1)
  101. typedef int (*xfs_dir_put_t)(struct xfs_dir_put_args *pa);
  102. typedef union {
  103. xfs_off_t o; /* offset (cookie) */
  104. /*
  105. * Watch the order here (endian-ness dependent).
  106. */
  107. struct {
  108. #ifndef XFS_NATIVE_HOST
  109. xfs_dahash_t h; /* hash value */
  110. __uint32_t be; /* block and entry */
  111. #else
  112. __uint32_t be; /* block and entry */
  113. xfs_dahash_t h; /* hash value */
  114. #endif /* XFS_NATIVE_HOST */
  115. } s;
  116. } xfs_dircook_t;
  117. #define XFS_PUT_COOKIE(c,mp,bno,entry,hash) \
  118. ((c).s.be = XFS_DA_MAKE_BNOENTRY(mp, bno, entry), (c).s.h = (hash))
  119. typedef struct xfs_dir_put_args {
  120. xfs_dircook_t cook; /* cookie of (next) entry */
  121. xfs_intino_t ino; /* inode number */
  122. struct xfs_dirent *dbp; /* buffer pointer */
  123. char *name; /* directory entry name */
  124. int namelen; /* length of name */
  125. int done; /* output: set if value was stored */
  126. xfs_dir_put_t put; /* put function ptr (i/o) */
  127. struct uio *uio; /* uio control structure */
  128. } xfs_dir_put_args_t;
  129. #define XFS_DIR_LEAF_ENTSIZE_BYNAME(len) \
  130. xfs_dir_leaf_entsize_byname(len)
  131. static inline int xfs_dir_leaf_entsize_byname(int len)
  132. {
  133. return (uint)sizeof(xfs_dir_leaf_name_t)-1 + len;
  134. }
  135. #define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry) \
  136. xfs_dir_leaf_entsize_byentry(entry)
  137. static inline int xfs_dir_leaf_entsize_byentry(xfs_dir_leaf_entry_t *entry)
  138. {
  139. return (uint)sizeof(xfs_dir_leaf_name_t)-1 + (entry)->namelen;
  140. }
  141. #define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset) \
  142. xfs_dir_leaf_namestruct(leafp,offset)
  143. static inline xfs_dir_leaf_name_t *
  144. xfs_dir_leaf_namestruct(xfs_dir_leafblock_t *leafp, int offset)
  145. {
  146. return (xfs_dir_leaf_name_t *)&((char *)(leafp))[offset];
  147. }
  148. /*========================================================================
  149. * Function prototypes for the kernel.
  150. *========================================================================*/
  151. /*
  152. * Internal routines when dirsize < XFS_LITINO(mp).
  153. */
  154. int xfs_dir_shortform_create(struct xfs_da_args *args, xfs_ino_t parent);
  155. int xfs_dir_shortform_addname(struct xfs_da_args *args);
  156. int xfs_dir_shortform_lookup(struct xfs_da_args *args);
  157. int xfs_dir_shortform_to_leaf(struct xfs_da_args *args);
  158. int xfs_dir_shortform_removename(struct xfs_da_args *args);
  159. int xfs_dir_shortform_getdents(struct xfs_inode *dp, struct uio *uio, int *eofp,
  160. struct xfs_dirent *dbp, xfs_dir_put_t put);
  161. int xfs_dir_shortform_replace(struct xfs_da_args *args);
  162. /*
  163. * Internal routines when dirsize == XFS_LBSIZE(mp).
  164. */
  165. int xfs_dir_leaf_to_node(struct xfs_da_args *args);
  166. int xfs_dir_leaf_to_shortform(struct xfs_da_args *args);
  167. /*
  168. * Routines used for growing the Btree.
  169. */
  170. int xfs_dir_leaf_split(struct xfs_da_state *state,
  171. struct xfs_da_state_blk *oldblk,
  172. struct xfs_da_state_blk *newblk);
  173. int xfs_dir_leaf_add(struct xfs_dabuf *leaf_buffer,
  174. struct xfs_da_args *args, int insertion_index);
  175. int xfs_dir_leaf_addname(struct xfs_da_args *args);
  176. int xfs_dir_leaf_lookup_int(struct xfs_dabuf *leaf_buffer,
  177. struct xfs_da_args *args,
  178. int *index_found_at);
  179. int xfs_dir_leaf_remove(struct xfs_trans *trans,
  180. struct xfs_dabuf *leaf_buffer,
  181. int index_to_remove);
  182. int xfs_dir_leaf_getdents_int(struct xfs_dabuf *bp, struct xfs_inode *dp,
  183. xfs_dablk_t bno, struct uio *uio,
  184. int *eobp, struct xfs_dirent *dbp,
  185. xfs_dir_put_t put, xfs_daddr_t nextda);
  186. /*
  187. * Routines used for shrinking the Btree.
  188. */
  189. int xfs_dir_leaf_toosmall(struct xfs_da_state *state, int *retval);
  190. void xfs_dir_leaf_unbalance(struct xfs_da_state *state,
  191. struct xfs_da_state_blk *drop_blk,
  192. struct xfs_da_state_blk *save_blk);
  193. /*
  194. * Utility routines.
  195. */
  196. uint xfs_dir_leaf_lasthash(struct xfs_dabuf *bp, int *count);
  197. int xfs_dir_leaf_order(struct xfs_dabuf *leaf1_bp,
  198. struct xfs_dabuf *leaf2_bp);
  199. int xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa);
  200. int xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa);
  201. int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
  202. /*
  203. * Global data.
  204. */
  205. extern xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
  206. #endif /* __XFS_DIR_LEAF_H__ */