xfs_attr_leaf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2000,2002-2003,2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef __XFS_ATTR_LEAF_H__
  20. #define __XFS_ATTR_LEAF_H__
  21. /*
  22. * Attribute storage layout, internal structure, access macros, etc.
  23. *
  24. * Attribute lists are structured around Btrees where all the data
  25. * elements are in the leaf nodes. Attribute names are hashed into an int,
  26. * then that int is used as the index into the Btree. Since the hashval
  27. * of an attribute name may not be unique, we may have duplicate keys. The
  28. * internal links in the Btree are logical block offsets into the file.
  29. */
  30. struct attrlist;
  31. struct attrlist_cursor_kern;
  32. struct xfs_attr_list_context;
  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 an "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_leaf_map { /* RLE map of free bytes */
  71. __be16 base; /* base of free region */
  72. __be16 size; /* length of free region */
  73. } xfs_attr_leaf_map_t;
  74. typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
  75. xfs_da_blkinfo_t info; /* block type, links, etc. */
  76. __be16 count; /* count of active leaf_entry's */
  77. __be16 usedbytes; /* num bytes of names/values stored */
  78. __be16 firstused; /* first used byte in name area */
  79. __u8 holes; /* != 0 if blk needs compaction */
  80. __u8 pad1;
  81. xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
  82. /* N largest free regions */
  83. } xfs_attr_leaf_hdr_t;
  84. typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
  85. __be32 hashval; /* hash value of name */
  86. __be16 nameidx; /* index into buffer of name/value */
  87. __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
  88. __u8 pad2; /* unused pad byte */
  89. } xfs_attr_leaf_entry_t;
  90. typedef struct xfs_attr_leaf_name_local {
  91. __be16 valuelen; /* number of bytes in value */
  92. __u8 namelen; /* length of name bytes */
  93. __u8 nameval[1]; /* name/value bytes */
  94. } xfs_attr_leaf_name_local_t;
  95. typedef struct xfs_attr_leaf_name_remote {
  96. __be32 valueblk; /* block number of value bytes */
  97. __be32 valuelen; /* number of bytes in value */
  98. __u8 namelen; /* length of name bytes */
  99. __u8 name[1]; /* name bytes */
  100. } xfs_attr_leaf_name_remote_t;
  101. typedef struct xfs_attr_leafblock {
  102. xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
  103. xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
  104. xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
  105. xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
  106. } xfs_attr_leafblock_t;
  107. /*
  108. * CRC enabled leaf structures. Called "version 3" structures to match the
  109. * version number of the directory and dablk structures for this feature, and
  110. * attr2 is already taken by the variable inode attribute fork size feature.
  111. */
  112. struct xfs_attr3_leaf_hdr {
  113. struct xfs_da3_blkinfo info;
  114. __be16 count;
  115. __be16 usedbytes;
  116. __be16 firstused;
  117. __u8 holes;
  118. __u8 pad1;
  119. struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
  120. __be32 pad2; /* 64 bit alignment */
  121. };
  122. #define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
  123. struct xfs_attr3_leafblock {
  124. struct xfs_attr3_leaf_hdr hdr;
  125. struct xfs_attr_leaf_entry entries[1];
  126. /*
  127. * The rest of the block contains the following structures after the
  128. * leaf entries, growing from the bottom up. The variables are never
  129. * referenced, the locations accessed purely from helper functions.
  130. *
  131. * struct xfs_attr_leaf_name_local
  132. * struct xfs_attr_leaf_name_remote
  133. */
  134. };
  135. /*
  136. * incore, neutral version of the attribute leaf header
  137. */
  138. struct xfs_attr3_icleaf_hdr {
  139. __uint32_t forw;
  140. __uint32_t back;
  141. __uint16_t magic;
  142. __uint16_t count;
  143. __uint16_t usedbytes;
  144. __uint16_t firstused;
  145. __u8 holes;
  146. struct {
  147. __uint16_t base;
  148. __uint16_t size;
  149. } freemap[XFS_ATTR_LEAF_MAPSIZE];
  150. };
  151. /*
  152. * Flags used in the leaf_entry[i].flags field.
  153. * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
  154. * on the system call, they are "or"ed together for various operations.
  155. */
  156. #define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
  157. #define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
  158. #define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
  159. #define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
  160. #define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
  161. #define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
  162. #define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
  163. #define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
  164. /*
  165. * Conversion macros for converting namespace bits from argument flags
  166. * to ondisk flags.
  167. */
  168. #define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
  169. #define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
  170. #define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
  171. #define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
  172. #define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
  173. ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
  174. #define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
  175. ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
  176. /*
  177. * Alignment for namelist and valuelist entries (since they are mixed
  178. * there can be only one alignment value)
  179. */
  180. #define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
  181. static inline int
  182. xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
  183. {
  184. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  185. return sizeof(struct xfs_attr3_leaf_hdr);
  186. return sizeof(struct xfs_attr_leaf_hdr);
  187. }
  188. static inline struct xfs_attr_leaf_entry *
  189. xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
  190. {
  191. if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
  192. return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
  193. return &leafp->entries[0];
  194. }
  195. /*
  196. * Cast typed pointers for "local" and "remote" name/value structs.
  197. */
  198. static inline char *
  199. xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
  200. {
  201. struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
  202. return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
  203. }
  204. static inline xfs_attr_leaf_name_remote_t *
  205. xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
  206. {
  207. return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
  208. }
  209. static inline xfs_attr_leaf_name_local_t *
  210. xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
  211. {
  212. return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
  213. }
  214. /*
  215. * Calculate total bytes used (including trailing pad for alignment) for
  216. * a "local" name/value structure, a "remote" name/value structure, and
  217. * a pointer which might be either.
  218. */
  219. static inline int xfs_attr_leaf_entsize_remote(int nlen)
  220. {
  221. return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
  222. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  223. }
  224. static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
  225. {
  226. return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
  227. XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
  228. }
  229. static inline int xfs_attr_leaf_entsize_local_max(int bsize)
  230. {
  231. return (((bsize) >> 1) + ((bsize) >> 2));
  232. }
  233. /*
  234. * Used to keep a list of "remote value" extents when unlinking an inode.
  235. */
  236. typedef struct xfs_attr_inactive_list {
  237. xfs_dablk_t valueblk; /* block number of value bytes */
  238. int valuelen; /* number of bytes in value */
  239. } xfs_attr_inactive_list_t;
  240. /*========================================================================
  241. * Function prototypes for the kernel.
  242. *========================================================================*/
  243. /*
  244. * Internal routines when attribute fork size < XFS_LITINO(mp).
  245. */
  246. void xfs_attr_shortform_create(struct xfs_da_args *args);
  247. void xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
  248. int xfs_attr_shortform_lookup(struct xfs_da_args *args);
  249. int xfs_attr_shortform_getvalue(struct xfs_da_args *args);
  250. int xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
  251. int xfs_attr_shortform_remove(struct xfs_da_args *args);
  252. int xfs_attr_shortform_list(struct xfs_attr_list_context *context);
  253. int xfs_attr_shortform_allfit(struct xfs_buf *bp, struct xfs_inode *dp);
  254. int xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
  255. /*
  256. * Internal routines when attribute fork size == XFS_LBSIZE(mp).
  257. */
  258. int xfs_attr3_leaf_to_node(struct xfs_da_args *args);
  259. int xfs_attr3_leaf_to_shortform(struct xfs_buf *bp,
  260. struct xfs_da_args *args, int forkoff);
  261. int xfs_attr3_leaf_clearflag(struct xfs_da_args *args);
  262. int xfs_attr3_leaf_setflag(struct xfs_da_args *args);
  263. int xfs_attr3_leaf_flipflags(struct xfs_da_args *args);
  264. /*
  265. * Routines used for growing the Btree.
  266. */
  267. int xfs_attr3_leaf_split(struct xfs_da_state *state,
  268. struct xfs_da_state_blk *oldblk,
  269. struct xfs_da_state_blk *newblk);
  270. int xfs_attr3_leaf_lookup_int(struct xfs_buf *leaf,
  271. struct xfs_da_args *args);
  272. int xfs_attr3_leaf_getvalue(struct xfs_buf *bp, struct xfs_da_args *args);
  273. int xfs_attr3_leaf_add(struct xfs_buf *leaf_buffer,
  274. struct xfs_da_args *args);
  275. int xfs_attr3_leaf_remove(struct xfs_buf *leaf_buffer,
  276. struct xfs_da_args *args);
  277. int xfs_attr3_leaf_list_int(struct xfs_buf *bp,
  278. struct xfs_attr_list_context *context);
  279. /*
  280. * Routines used for shrinking the Btree.
  281. */
  282. int xfs_attr3_leaf_toosmall(struct xfs_da_state *state, int *retval);
  283. void xfs_attr3_leaf_unbalance(struct xfs_da_state *state,
  284. struct xfs_da_state_blk *drop_blk,
  285. struct xfs_da_state_blk *save_blk);
  286. int xfs_attr3_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
  287. /*
  288. * Utility routines.
  289. */
  290. xfs_dahash_t xfs_attr_leaf_lasthash(struct xfs_buf *bp, int *count);
  291. int xfs_attr_leaf_order(struct xfs_buf *leaf1_bp,
  292. struct xfs_buf *leaf2_bp);
  293. int xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
  294. int *local);
  295. int xfs_attr3_leaf_read(struct xfs_trans *tp, struct xfs_inode *dp,
  296. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  297. struct xfs_buf **bpp);
  298. void xfs_attr3_leaf_hdr_from_disk(struct xfs_attr3_icleaf_hdr *to,
  299. struct xfs_attr_leafblock *from);
  300. extern const struct xfs_buf_ops xfs_attr3_leaf_buf_ops;
  301. #endif /* __XFS_ATTR_LEAF_H__ */