xfs_da_btree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2000,2002,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_DA_BTREE_H__
  20. #define __XFS_DA_BTREE_H__
  21. struct xfs_bmap_free;
  22. struct xfs_inode;
  23. struct xfs_trans;
  24. struct zone;
  25. /*========================================================================
  26. * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
  27. *========================================================================*/
  28. /*
  29. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  30. *
  31. * It is used to manage a doubly linked list of all blocks at the same
  32. * level in the Btree, and to identify which type of block this is.
  33. */
  34. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  35. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  36. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  37. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  38. typedef struct xfs_da_blkinfo {
  39. __be32 forw; /* previous block in list */
  40. __be32 back; /* following block in list */
  41. __be16 magic; /* validity check on block */
  42. __be16 pad; /* unused */
  43. } xfs_da_blkinfo_t;
  44. /*
  45. * CRC enabled directory structure types
  46. *
  47. * The headers change size for the additional verification information, but
  48. * otherwise the tree layouts and contents are unchanged. Hence the da btree
  49. * code can use the struct xfs_da_blkinfo for manipulating the tree links and
  50. * magic numbers without modification for both v2 and v3 nodes.
  51. */
  52. #define XFS_DA3_NODE_MAGIC 0x3ebe /* magic number: non-leaf blocks */
  53. #define XFS_ATTR3_LEAF_MAGIC 0x3bee /* magic number: attribute leaf blks */
  54. #define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
  55. #define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
  56. struct xfs_da3_blkinfo {
  57. /*
  58. * the node link manipulation code relies on the fact that the first
  59. * element of this structure is the struct xfs_da_blkinfo so it can
  60. * ignore the differences in the rest of the structures.
  61. */
  62. struct xfs_da_blkinfo hdr;
  63. __be32 crc; /* CRC of block */
  64. __be64 blkno; /* first block of the buffer */
  65. __be64 lsn; /* sequence number of last write */
  66. uuid_t uuid; /* filesystem we belong to */
  67. __be64 owner; /* inode that owns the block */
  68. };
  69. /*
  70. * This is the structure of the root and intermediate nodes in the Btree.
  71. * The leaf nodes are defined above.
  72. *
  73. * Entries are not packed.
  74. *
  75. * Since we have duplicate keys, use a binary search but always follow
  76. * all match in the block, not just the first match found.
  77. */
  78. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  79. typedef struct xfs_da_node_hdr {
  80. struct xfs_da_blkinfo info; /* block type, links, etc. */
  81. __be16 __count; /* count of active entries */
  82. __be16 __level; /* level above leaves (leaf == 0) */
  83. } xfs_da_node_hdr_t;
  84. struct xfs_da3_node_hdr {
  85. struct xfs_da3_blkinfo info; /* block type, links, etc. */
  86. __be16 __count; /* count of active entries */
  87. __be16 __level; /* level above leaves (leaf == 0) */
  88. __be32 __pad32;
  89. };
  90. #define XFS_DA3_NODE_CRC_OFF (offsetof(struct xfs_da3_node_hdr, info.crc))
  91. typedef struct xfs_da_node_entry {
  92. __be32 hashval; /* hash value for this descendant */
  93. __be32 before; /* Btree block before this key */
  94. } xfs_da_node_entry_t;
  95. typedef struct xfs_da_intnode {
  96. struct xfs_da_node_hdr hdr;
  97. struct xfs_da_node_entry __btree[];
  98. } xfs_da_intnode_t;
  99. struct xfs_da3_intnode {
  100. struct xfs_da3_node_hdr hdr;
  101. struct xfs_da_node_entry __btree[];
  102. };
  103. /*
  104. * In-core version of the node header to abstract the differences in the v2 and
  105. * v3 disk format of the headers. Callers need to convert to/from disk format as
  106. * appropriate.
  107. */
  108. struct xfs_da3_icnode_hdr {
  109. __uint32_t forw;
  110. __uint32_t back;
  111. __uint16_t magic;
  112. __uint16_t count;
  113. __uint16_t level;
  114. };
  115. extern void xfs_da3_node_hdr_from_disk(struct xfs_da3_icnode_hdr *to,
  116. struct xfs_da_intnode *from);
  117. extern void xfs_da3_node_hdr_to_disk(struct xfs_da_intnode *to,
  118. struct xfs_da3_icnode_hdr *from);
  119. static inline int
  120. xfs_da3_node_hdr_size(struct xfs_da_intnode *dap)
  121. {
  122. if (dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC))
  123. return sizeof(struct xfs_da3_node_hdr);
  124. return sizeof(struct xfs_da_node_hdr);
  125. }
  126. static inline struct xfs_da_node_entry *
  127. xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
  128. {
  129. if (dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
  130. struct xfs_da3_intnode *dap3 = (struct xfs_da3_intnode *)dap;
  131. return dap3->__btree;
  132. }
  133. return dap->__btree;
  134. }
  135. extern void xfs_da3_intnode_from_disk(struct xfs_da3_icnode_hdr *to,
  136. struct xfs_da_intnode *from);
  137. extern void xfs_da3_intnode_to_disk(struct xfs_da_intnode *to,
  138. struct xfs_da3_icnode_hdr *from);
  139. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  140. /*========================================================================
  141. * Btree searching and modification structure definitions.
  142. *========================================================================*/
  143. /*
  144. * Search comparison results
  145. */
  146. enum xfs_dacmp {
  147. XFS_CMP_DIFFERENT, /* names are completely different */
  148. XFS_CMP_EXACT, /* names are exactly the same */
  149. XFS_CMP_CASE /* names are same but differ in case */
  150. };
  151. /*
  152. * Structure to ease passing around component names.
  153. */
  154. typedef struct xfs_da_args {
  155. const __uint8_t *name; /* string (maybe not NULL terminated) */
  156. int namelen; /* length of string (maybe no NULL) */
  157. __uint8_t *value; /* set of bytes (maybe contain NULLs) */
  158. int valuelen; /* length of value */
  159. int flags; /* argument flags (eg: ATTR_NOCREATE) */
  160. xfs_dahash_t hashval; /* hash value of name */
  161. xfs_ino_t inumber; /* input/output inode number */
  162. struct xfs_inode *dp; /* directory inode to manipulate */
  163. xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
  164. struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
  165. struct xfs_trans *trans; /* current trans (changes over time) */
  166. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  167. int whichfork; /* data or attribute fork */
  168. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  169. int index; /* index of attr of interest in blk */
  170. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  171. int rmtblkcnt; /* remote attr value block count */
  172. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  173. int index2; /* index of 2nd attr in blk */
  174. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  175. int rmtblkcnt2; /* remote attr value block count */
  176. int op_flags; /* operation flags */
  177. enum xfs_dacmp cmpresult; /* name compare result for lookups */
  178. } xfs_da_args_t;
  179. /*
  180. * Operation flags:
  181. */
  182. #define XFS_DA_OP_JUSTCHECK 0x0001 /* check for ok with no space */
  183. #define XFS_DA_OP_RENAME 0x0002 /* this is an atomic rename op */
  184. #define XFS_DA_OP_ADDNAME 0x0004 /* this is an add operation */
  185. #define XFS_DA_OP_OKNOENT 0x0008 /* lookup/add op, ENOENT ok, else die */
  186. #define XFS_DA_OP_CILOOKUP 0x0010 /* lookup to return CI name if found */
  187. #define XFS_DA_OP_FLAGS \
  188. { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \
  189. { XFS_DA_OP_RENAME, "RENAME" }, \
  190. { XFS_DA_OP_ADDNAME, "ADDNAME" }, \
  191. { XFS_DA_OP_OKNOENT, "OKNOENT" }, \
  192. { XFS_DA_OP_CILOOKUP, "CILOOKUP" }
  193. /*
  194. * Storage for holding state during Btree searches and split/join ops.
  195. *
  196. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  197. * fanout to the Btree, we can support over 900 million directory blocks,
  198. * which is slightly more than enough.
  199. */
  200. typedef struct xfs_da_state_blk {
  201. struct xfs_buf *bp; /* buffer containing block */
  202. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  203. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  204. int index; /* relevant index into block */
  205. xfs_dahash_t hashval; /* last hash value in block */
  206. int magic; /* blk's magic number, ie: blk type */
  207. } xfs_da_state_blk_t;
  208. typedef struct xfs_da_state_path {
  209. int active; /* number of active levels */
  210. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  211. } xfs_da_state_path_t;
  212. typedef struct xfs_da_state {
  213. xfs_da_args_t *args; /* filename arguments */
  214. struct xfs_mount *mp; /* filesystem mount point */
  215. unsigned int blocksize; /* logical block size */
  216. unsigned int node_ents; /* how many entries in danode */
  217. xfs_da_state_path_t path; /* search/split paths */
  218. xfs_da_state_path_t altpath; /* alternate path for join */
  219. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  220. unsigned char extravalid; /* T/F: extrablk is in use */
  221. unsigned char extraafter; /* T/F: extrablk is after new */
  222. xfs_da_state_blk_t extrablk; /* for double-splits on leaves */
  223. /* for dirv2 extrablk is data */
  224. } xfs_da_state_t;
  225. /*
  226. * Utility macros to aid in logging changed structure fields.
  227. */
  228. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  229. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  230. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  231. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  232. /*
  233. * Name ops for directory and/or attr name operations
  234. */
  235. struct xfs_nameops {
  236. xfs_dahash_t (*hashname)(struct xfs_name *);
  237. enum xfs_dacmp (*compname)(struct xfs_da_args *,
  238. const unsigned char *, int);
  239. };
  240. /*========================================================================
  241. * Function prototypes.
  242. *========================================================================*/
  243. /*
  244. * Routines used for growing the Btree.
  245. */
  246. int xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno,
  247. int level, struct xfs_buf **bpp, int whichfork);
  248. int xfs_da3_split(xfs_da_state_t *state);
  249. /*
  250. * Routines used for shrinking the Btree.
  251. */
  252. int xfs_da3_join(xfs_da_state_t *state);
  253. void xfs_da3_fixhashpath(struct xfs_da_state *state,
  254. struct xfs_da_state_path *path_to_to_fix);
  255. /*
  256. * Routines used for finding things in the Btree.
  257. */
  258. int xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result);
  259. int xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  260. int forward, int release, int *result);
  261. /*
  262. * Utility routines.
  263. */
  264. int xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  265. xfs_da_state_blk_t *new_blk);
  266. int xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
  267. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  268. struct xfs_buf **bpp, int which_fork);
  269. extern const struct xfs_buf_ops xfs_da3_node_buf_ops;
  270. /*
  271. * Utility routines.
  272. */
  273. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  274. int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
  275. int count);
  276. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  277. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  278. struct xfs_buf **bp, int whichfork);
  279. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  280. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  281. struct xfs_buf **bpp, int whichfork,
  282. const struct xfs_buf_ops *ops);
  283. xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  284. xfs_dablk_t bno, xfs_daddr_t mapped_bno,
  285. int whichfork, const struct xfs_buf_ops *ops);
  286. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  287. struct xfs_buf *dead_buf);
  288. uint xfs_da_hashname(const __uint8_t *name_string, int name_length);
  289. enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
  290. const unsigned char *name, int len);
  291. xfs_da_state_t *xfs_da_state_alloc(void);
  292. void xfs_da_state_free(xfs_da_state_t *state);
  293. extern struct kmem_zone *xfs_da_state_zone;
  294. extern const struct xfs_nameops xfs_default_nameops;
  295. #endif /* __XFS_DA_BTREE_H__ */