xfs_attr_leaf.h 12 KB

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