xfs_attr_leaf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2000,2002-2003,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_ATTR_LEAF_H__
  19. #define __XFS_ATTR_LEAF_H__
  20. /*
  21. * Attribute storage layout, internal structure, access macros, etc.
  22. *
  23. * Attribute lists are structured around Btrees where all the data
  24. * elements are in the leaf nodes. Attribute names are hashed into an int,
  25. * then that int is used as the index into the Btree. Since the hashval
  26. * of an attribute name 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 attrlist;
  30. struct attrlist_cursor_kern;
  31. struct attrnames;
  32. struct xfs_dabuf;
  33. struct xfs_da_args;
  34. struct xfs_da_state;
  35. struct xfs_da_state_blk;
  36. struct xfs_inode;
  37. struct xfs_trans;
  38. /*========================================================================
  39. * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
  40. *========================================================================*/
  41. /*
  42. * This is the structure of the leaf nodes in the Btree.
  43. *
  44. * Struct leaf_entry's are packed from the top. Name/values grow from the
  45. * bottom but are not packed. The freemap contains run-length-encoded entries
  46. * for the free bytes after the leaf_entry's, but only the N largest such,
  47. * smaller runs are dropped. When the freemap doesn't show enough space
  48. * for an allocation, we compact the name/value area and try again. If we
  49. * still don't have enough space, then we have to split the block. The
  50. * name/value structs (both local and remote versions) must be 32bit aligned.
  51. *
  52. * Since we have duplicate hash keys, for each key that matches, compare
  53. * the actual name string. The root and intermediate node search always
  54. * takes the first-in-the-block key match found, so we should only have
  55. * to work "forw"ard. If none matches, continue with the "forw"ard leaf
  56. * nodes until the hash key changes or the attribute name is found.
  57. *
  58. * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
  59. * the leaf_entry. The namespaces are independent only because we also look
  60. * at the namespace bit when we are looking for a matching attribute name.
  61. *
  62. * We also store a "incomplete" bit in the leaf_entry. It shows that an
  63. * attribute is in the middle of being created and should not be shown to
  64. * the user if we crash during the time that the bit is set. We clear the
  65. * bit when we have finished setting up the attribute. We do this because
  66. * we cannot create some large attributes inside a single transaction, and we
  67. * need some indication that we weren't finished if we crash in the middle.
  68. */
  69. #define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
  70. typedef struct xfs_attr_leafblock {
  71. struct xfs_attr_leaf_hdr { /* constant-structure header block */
  72. xfs_da_blkinfo_t info; /* block type, links, etc. */
  73. __uint16_t count; /* count of active leaf_entry's */
  74. __uint16_t usedbytes; /* num bytes of names/values stored */
  75. __uint16_t firstused; /* first used byte in name area */
  76. __uint8_t holes; /* != 0 if blk needs compaction */
  77. __uint8_t pad1;
  78. struct xfs_attr_leaf_map { /* RLE map of free bytes */
  79. __uint16_t base; /* base of free region */
  80. __uint16_t size; /* length of free region */
  81. } freemap[XFS_ATTR_LEAF_MAPSIZE]; /* N largest free regions */
  82. } hdr;
  83. struct xfs_attr_leaf_entry { /* sorted on key, not name */
  84. xfs_dahash_t hashval; /* hash value of name */
  85. __uint16_t nameidx; /* index into buffer of name/value */
  86. __uint8_t flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  87. __uint8_t pad2; /* unused pad byte */
  88. } entries[1]; /* variable sized array */
  89. struct xfs_attr_leaf_name_local {
  90. __uint16_t valuelen; /* number of bytes in value */
  91. __uint8_t namelen; /* length of name bytes */
  92. __uint8_t nameval[1]; /* name/value bytes */
  93. } namelist; /* grows from bottom of buf */
  94. struct xfs_attr_leaf_name_remote {
  95. xfs_dablk_t valueblk; /* block number of value bytes */
  96. __uint32_t valuelen; /* number of bytes in value */
  97. __uint8_t namelen; /* length of name bytes */
  98. __uint8_t name[1]; /* name bytes */
  99. } valuelist; /* grows from bottom of buf */
  100. } xfs_attr_leafblock_t;
  101. typedef struct xfs_attr_leaf_hdr xfs_attr_leaf_hdr_t;
  102. typedef struct xfs_attr_leaf_map xfs_attr_leaf_map_t;
  103. typedef struct xfs_attr_leaf_entry xfs_attr_leaf_entry_t;
  104. typedef struct xfs_attr_leaf_name_local xfs_attr_leaf_name_local_t;
  105. typedef struct xfs_attr_leaf_name_remote xfs_attr_leaf_name_remote_t;
  106. /*
  107. * Flags used in the leaf_entry[i].flags field.
  108. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  109. * on the system call, they are "or"ed together for various operations.
  110. */
  111. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  112. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  113. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  114. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  115. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  116. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  117. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  118. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  119. /*
  120. * Alignment for namelist and valuelist entries (since they are mixed
  121. * there can be only one alignment value)
  122. */
  123. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  124. /*
  125. * Cast typed pointers for "local" and "remote" name/value structs.
  126. */
  127. #define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx) \
  128. xfs_attr_leaf_name_remote(leafp,idx)
  129. static inline xfs_attr_leaf_name_remote_t *
  130. xfs_attr_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  131. {
  132. return (xfs_attr_leaf_name_remote_t *) &((char *)
  133. (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)];
  134. }
  135. #define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx) \
  136. xfs_attr_leaf_name_local(leafp,idx)
  137. static inline xfs_attr_leaf_name_local_t *
  138. xfs_attr_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  139. {
  140. return (xfs_attr_leaf_name_local_t *) &((char *)
  141. (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)];
  142. }
  143. #define XFS_ATTR_LEAF_NAME(leafp,idx) xfs_attr_leaf_name(leafp,idx)
  144. static inline char *xfs_attr_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  145. {
  146. return (&((char *)
  147. (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)]);
  148. }
  149. /*
  150. * Calculate total bytes used (including trailing pad for alignment) for
  151. * a "local" name/value structure, a "remote" name/value structure, and
  152. * a pointer which might be either.
  153. */
  154. #define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen) \
  155. xfs_attr_leaf_entsize_remote(nlen)
  156. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  157. {
  158. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  159. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  160. }
  161. #define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen) \
  162. xfs_attr_leaf_entsize_local(nlen,vlen)
  163. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  164. {
  165. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  166. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  167. }
  168. #define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize) \
  169. xfs_attr_leaf_entsize_local_max(bsize)
  170. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  171. {
  172. return (((bsize) >> 1) + ((bsize) >> 2));
  173. }
  174. /*========================================================================
  175. * Structure used to pass context around among the routines.
  176. *========================================================================*/
  177. typedef struct xfs_attr_list_context {
  178. struct xfs_inode *dp; /* inode */
  179. struct attrlist_cursor_kern *cursor;/* position in list */
  180. struct attrlist *alist; /* output buffer */
  181. int count; /* num used entries */
  182. int dupcnt; /* count dup hashvals seen */
  183. int bufsize;/* total buffer size */
  184. int firstu; /* first used byte in buffer */
  185. int flags; /* from VOP call */
  186. int resynch;/* T/F: resynch with cursor */
  187. } xfs_attr_list_context_t;
  188. /*
  189. * Used to keep a list of "remote value" extents when unlinking an inode.
  190. */
  191. typedef struct xfs_attr_inactive_list {
  192. xfs_dablk_t valueblk; /* block number of value bytes */
  193. int valuelen; /* number of bytes in value */
  194. } xfs_attr_inactive_list_t;
  195. /*========================================================================
  196. * Function prototypes for the kernel.
  197. *========================================================================*/
  198. /*
  199. * Internal routines when attribute fork size < XFS_LITINO(mp).
  200. */
  201. void xfs_attr_shortform_create(struct xfs_da_args *args);
  202. void xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
  203. int xfs_attr_shortform_lookup(struct xfs_da_args *args);
  204. int xfs_attr_shortform_getvalue(struct xfs_da_args *args);
  205. int xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
  206. int xfs_attr_shortform_remove(struct xfs_da_args *args);
  207. int xfs_attr_shortform_list(struct xfs_attr_list_context *context);
  208. int xfs_attr_shortform_allfit(struct xfs_dabuf *bp, struct xfs_inode *dp);
  209. int xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
  210. /*
  211. * Internal routines when attribute fork size == XFS_LBSIZE(mp).
  212. */
  213. int xfs_attr_leaf_to_node(struct xfs_da_args *args);
  214. int xfs_attr_leaf_to_shortform(struct xfs_dabuf *bp,
  215. struct xfs_da_args *args, int forkoff);
  216. int xfs_attr_leaf_clearflag(struct xfs_da_args *args);
  217. int xfs_attr_leaf_setflag(struct xfs_da_args *args);
  218. int xfs_attr_leaf_flipflags(xfs_da_args_t *args);
  219. /*
  220. * Routines used for growing the Btree.
  221. */
  222. int xfs_attr_leaf_split(struct xfs_da_state *state,
  223. struct xfs_da_state_blk *oldblk,
  224. struct xfs_da_state_blk *newblk);
  225. int xfs_attr_leaf_lookup_int(struct xfs_dabuf *leaf,
  226. struct xfs_da_args *args);
  227. int xfs_attr_leaf_getvalue(struct xfs_dabuf *bp, struct xfs_da_args *args);
  228. int xfs_attr_leaf_add(struct xfs_dabuf *leaf_buffer,
  229. struct xfs_da_args *args);
  230. int xfs_attr_leaf_remove(struct xfs_dabuf *leaf_buffer,
  231. struct xfs_da_args *args);
  232. int xfs_attr_leaf_list_int(struct xfs_dabuf *bp,
  233. struct xfs_attr_list_context *context);
  234. /*
  235. * Routines used for shrinking the Btree.
  236. */
  237. int xfs_attr_leaf_toosmall(struct xfs_da_state *state, int *retval);
  238. void xfs_attr_leaf_unbalance(struct xfs_da_state *state,
  239. struct xfs_da_state_blk *drop_blk,
  240. struct xfs_da_state_blk *save_blk);
  241. int xfs_attr_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
  242. /*
  243. * Utility routines.
  244. */
  245. xfs_dahash_t xfs_attr_leaf_lasthash(struct xfs_dabuf *bp, int *count);
  246. int xfs_attr_leaf_order(struct xfs_dabuf *leaf1_bp,
  247. struct xfs_dabuf *leaf2_bp);
  248. int xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
  249. int *local);
  250. int xfs_attr_rolltrans(struct xfs_trans **transp, struct xfs_inode *dp);
  251. #endif /* __XFS_ATTR_LEAF_H__ */