xfs_dir_leaf.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #ifndef __XFS_DIR_LEAF_H__
  33. #define __XFS_DIR_LEAF_H__
  34. /*
  35. * Directory layout, internal structure, access macros, etc.
  36. *
  37. * Large directories are structured around Btrees where all the data
  38. * elements are in the leaf nodes. Filenames are hashed into an int,
  39. * then that int is used as the index into the Btree. Since the hashval
  40. * of a filename may not be unique, we may have duplicate keys. The
  41. * internal links in the Btree are logical block offsets into the file.
  42. */
  43. struct uio;
  44. struct xfs_bmap_free;
  45. struct xfs_dabuf;
  46. struct xfs_da_args;
  47. struct xfs_da_state;
  48. struct xfs_da_state_blk;
  49. struct xfs_dir_put_args;
  50. struct xfs_inode;
  51. struct xfs_mount;
  52. struct xfs_trans;
  53. /*========================================================================
  54. * Directory Structure when equal to XFS_LBSIZE(mp) bytes.
  55. *========================================================================*/
  56. /*
  57. * This is the structure of the leaf nodes in the Btree.
  58. *
  59. * Struct leaf_entry's are packed from the top. Names grow from the bottom
  60. * but are not packed. The freemap contains run-length-encoded entries
  61. * for the free bytes after the leaf_entry's, but only the N largest such,
  62. * smaller runs are dropped. When the freemap doesn't show enough space
  63. * for an allocation, we compact the namelist area and try again. If we
  64. * still don't have enough space, then we have to split the block.
  65. *
  66. * Since we have duplicate hash keys, for each key that matches, compare
  67. * the actual string. The root and intermediate node search always takes
  68. * the first-in-the-block key match found, so we should only have to work
  69. * "forw"ard. If none matches, continue with the "forw"ard leaf nodes
  70. * until the hash key changes or the filename is found.
  71. *
  72. * The parent directory and the self-pointer are explicitly represented
  73. * (ie: there are entries for "." and "..").
  74. *
  75. * Note that the count being a __uint16_t limits us to something like a
  76. * blocksize of 1.3MB in the face of worst case (short) filenames.
  77. */
  78. #define XFS_DIR_LEAF_MAPSIZE 3 /* how many freespace slots */
  79. typedef struct xfs_dir_leafblock {
  80. struct xfs_dir_leaf_hdr { /* constant-structure header block */
  81. xfs_da_blkinfo_t info; /* block type, links, etc. */
  82. __uint16_t count; /* count of active leaf_entry's */
  83. __uint16_t namebytes; /* num bytes of name strings stored */
  84. __uint16_t firstused; /* first used byte in name area */
  85. __uint8_t holes; /* != 0 if blk needs compaction */
  86. __uint8_t pad1;
  87. struct xfs_dir_leaf_map {/* RLE map of free bytes */
  88. __uint16_t base; /* base of free region */
  89. __uint16_t size; /* run length of free region */
  90. } freemap[XFS_DIR_LEAF_MAPSIZE]; /* N largest free regions */
  91. } hdr;
  92. struct xfs_dir_leaf_entry { /* sorted on key, not name */
  93. xfs_dahash_t hashval; /* hash value of name */
  94. __uint16_t nameidx; /* index into buffer of name */
  95. __uint8_t namelen; /* length of name string */
  96. __uint8_t pad2;
  97. } entries[1]; /* var sized array */
  98. struct xfs_dir_leaf_name {
  99. xfs_dir_ino_t inumber; /* inode number for this key */
  100. __uint8_t name[1]; /* name string itself */
  101. } namelist[1]; /* grows from bottom of buf */
  102. } xfs_dir_leafblock_t;
  103. typedef struct xfs_dir_leaf_hdr xfs_dir_leaf_hdr_t;
  104. typedef struct xfs_dir_leaf_map xfs_dir_leaf_map_t;
  105. typedef struct xfs_dir_leaf_entry xfs_dir_leaf_entry_t;
  106. typedef struct xfs_dir_leaf_name xfs_dir_leaf_name_t;
  107. /*
  108. * Length of name for which a 512-byte block filesystem
  109. * can get a double split.
  110. */
  111. #define XFS_DIR_LEAF_CAN_DOUBLE_SPLIT_LEN \
  112. (512 - (uint)sizeof(xfs_dir_leaf_hdr_t) - \
  113. (uint)sizeof(xfs_dir_leaf_entry_t) * 2 - \
  114. (uint)sizeof(xfs_dir_leaf_name_t) * 2 - (MAXNAMELEN - 2) + 1 + 1)
  115. typedef int (*xfs_dir_put_t)(struct xfs_dir_put_args *pa);
  116. typedef union {
  117. xfs_off_t o; /* offset (cookie) */
  118. /*
  119. * Watch the order here (endian-ness dependent).
  120. */
  121. struct {
  122. #ifndef XFS_NATIVE_HOST
  123. xfs_dahash_t h; /* hash value */
  124. __uint32_t be; /* block and entry */
  125. #else
  126. __uint32_t be; /* block and entry */
  127. xfs_dahash_t h; /* hash value */
  128. #endif /* XFS_NATIVE_HOST */
  129. } s;
  130. } xfs_dircook_t;
  131. #define XFS_PUT_COOKIE(c,mp,bno,entry,hash) \
  132. ((c).s.be = XFS_DA_MAKE_BNOENTRY(mp, bno, entry), (c).s.h = (hash))
  133. typedef struct xfs_dir_put_args
  134. {
  135. xfs_dircook_t cook; /* cookie of (next) entry */
  136. xfs_intino_t ino; /* inode number */
  137. struct xfs_dirent *dbp; /* buffer pointer */
  138. char *name; /* directory entry name */
  139. int namelen; /* length of name */
  140. int done; /* output: set if value was stored */
  141. xfs_dir_put_t put; /* put function ptr (i/o) */
  142. struct uio *uio; /* uio control structure */
  143. } xfs_dir_put_args_t;
  144. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR_LEAF_ENTSIZE_BYNAME)
  145. int xfs_dir_leaf_entsize_byname(int len);
  146. #define XFS_DIR_LEAF_ENTSIZE_BYNAME(len) xfs_dir_leaf_entsize_byname(len)
  147. #else
  148. #define XFS_DIR_LEAF_ENTSIZE_BYNAME(len) /* space a name will use */ \
  149. ((uint)sizeof(xfs_dir_leaf_name_t)-1 + len)
  150. #endif
  151. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR_LEAF_ENTSIZE_BYENTRY)
  152. int xfs_dir_leaf_entsize_byentry(xfs_dir_leaf_entry_t *entry);
  153. #define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry) \
  154. xfs_dir_leaf_entsize_byentry(entry)
  155. #else
  156. #define XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry) /* space an entry will use */ \
  157. ((uint)sizeof(xfs_dir_leaf_name_t)-1 + (entry)->namelen)
  158. #endif
  159. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DIR_LEAF_NAMESTRUCT)
  160. xfs_dir_leaf_name_t *
  161. xfs_dir_leaf_namestruct(xfs_dir_leafblock_t *leafp, int offset);
  162. #define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset) \
  163. xfs_dir_leaf_namestruct(leafp,offset)
  164. #else
  165. #define XFS_DIR_LEAF_NAMESTRUCT(leafp,offset) /* point to name struct */ \
  166. ((xfs_dir_leaf_name_t *)&((char *)(leafp))[offset])
  167. #endif
  168. /*========================================================================
  169. * Function prototypes for the kernel.
  170. *========================================================================*/
  171. /*
  172. * Internal routines when dirsize < XFS_LITINO(mp).
  173. */
  174. int xfs_dir_shortform_create(struct xfs_da_args *args, xfs_ino_t parent);
  175. int xfs_dir_shortform_addname(struct xfs_da_args *args);
  176. int xfs_dir_shortform_lookup(struct xfs_da_args *args);
  177. int xfs_dir_shortform_to_leaf(struct xfs_da_args *args);
  178. int xfs_dir_shortform_removename(struct xfs_da_args *args);
  179. int xfs_dir_shortform_getdents(struct xfs_inode *dp, struct uio *uio, int *eofp,
  180. struct xfs_dirent *dbp, xfs_dir_put_t put);
  181. int xfs_dir_shortform_replace(struct xfs_da_args *args);
  182. /*
  183. * Internal routines when dirsize == XFS_LBSIZE(mp).
  184. */
  185. int xfs_dir_leaf_to_node(struct xfs_da_args *args);
  186. int xfs_dir_leaf_to_shortform(struct xfs_da_args *args);
  187. /*
  188. * Routines used for growing the Btree.
  189. */
  190. int xfs_dir_leaf_split(struct xfs_da_state *state,
  191. struct xfs_da_state_blk *oldblk,
  192. struct xfs_da_state_blk *newblk);
  193. int xfs_dir_leaf_add(struct xfs_dabuf *leaf_buffer,
  194. struct xfs_da_args *args, int insertion_index);
  195. int xfs_dir_leaf_addname(struct xfs_da_args *args);
  196. int xfs_dir_leaf_lookup_int(struct xfs_dabuf *leaf_buffer,
  197. struct xfs_da_args *args,
  198. int *index_found_at);
  199. int xfs_dir_leaf_remove(struct xfs_trans *trans,
  200. struct xfs_dabuf *leaf_buffer,
  201. int index_to_remove);
  202. int xfs_dir_leaf_getdents_int(struct xfs_dabuf *bp, struct xfs_inode *dp,
  203. xfs_dablk_t bno, struct uio *uio,
  204. int *eobp, struct xfs_dirent *dbp,
  205. xfs_dir_put_t put, xfs_daddr_t nextda);
  206. /*
  207. * Routines used for shrinking the Btree.
  208. */
  209. int xfs_dir_leaf_toosmall(struct xfs_da_state *state, int *retval);
  210. void xfs_dir_leaf_unbalance(struct xfs_da_state *state,
  211. struct xfs_da_state_blk *drop_blk,
  212. struct xfs_da_state_blk *save_blk);
  213. /*
  214. * Utility routines.
  215. */
  216. uint xfs_dir_leaf_lasthash(struct xfs_dabuf *bp, int *count);
  217. int xfs_dir_leaf_order(struct xfs_dabuf *leaf1_bp,
  218. struct xfs_dabuf *leaf2_bp);
  219. int xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa);
  220. int xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa);
  221. int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
  222. /*
  223. * Global data.
  224. */
  225. extern xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
  226. #endif /* __XFS_DIR_LEAF_H__ */