xfs_da_btree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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(bool v3)
  121. {
  122. if (v3)
  123. return sizeof(struct xfs_da3_node_hdr);
  124. return sizeof(struct xfs_da_node_hdr);
  125. }
  126. static inline int
  127. xfs_da3_node_hdr_size(struct xfs_da_intnode *dap)
  128. {
  129. bool v3 = dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC);
  130. return __xfs_da3_node_hdr_size(v3);
  131. }
  132. static inline struct xfs_da_node_entry *
  133. xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
  134. {
  135. if (dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
  136. struct xfs_da3_intnode *dap3 = (struct xfs_da3_intnode *)dap;
  137. return dap3->__btree;
  138. }
  139. return dap->__btree;
  140. }
  141. extern void xfs_da3_intnode_from_disk(struct xfs_da3_icnode_hdr *to,
  142. struct xfs_da_intnode *from);
  143. extern void xfs_da3_intnode_to_disk(struct xfs_da_intnode *to,
  144. struct xfs_da3_icnode_hdr *from);
  145. #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
  146. /*========================================================================
  147. * Btree searching and modification structure definitions.
  148. *========================================================================*/
  149. /*
  150. * Search comparison results
  151. */
  152. enum xfs_dacmp {
  153. XFS_CMP_DIFFERENT, /* names are completely different */
  154. XFS_CMP_EXACT, /* names are exactly the same */
  155. XFS_CMP_CASE /* names are same but differ in case */
  156. };
  157. /*
  158. * Structure to ease passing around component names.
  159. */
  160. typedef struct xfs_da_args {
  161. const __uint8_t *name; /* string (maybe not NULL terminated) */
  162. int namelen; /* length of string (maybe no NULL) */
  163. __uint8_t filetype; /* filetype of inode for directories */
  164. __uint8_t *value; /* set of bytes (maybe contain NULLs) */
  165. int valuelen; /* length of value */
  166. int flags; /* argument flags (eg: ATTR_NOCREATE) */
  167. xfs_dahash_t hashval; /* hash value of name */
  168. xfs_ino_t inumber; /* input/output inode number */
  169. struct xfs_inode *dp; /* directory inode to manipulate */
  170. xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
  171. struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
  172. struct xfs_trans *trans; /* current trans (changes over time) */
  173. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  174. int whichfork; /* data or attribute fork */
  175. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  176. int index; /* index of attr of interest in blk */
  177. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  178. int rmtblkcnt; /* remote attr value block count */
  179. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  180. int index2; /* index of 2nd attr in blk */
  181. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  182. int rmtblkcnt2; /* remote attr value block count */
  183. int op_flags; /* operation flags */
  184. enum xfs_dacmp cmpresult; /* name compare result for lookups */
  185. } xfs_da_args_t;
  186. /*
  187. * Operation flags:
  188. */
  189. #define XFS_DA_OP_JUSTCHECK 0x0001 /* check for ok with no space */
  190. #define XFS_DA_OP_RENAME 0x0002 /* this is an atomic rename op */
  191. #define XFS_DA_OP_ADDNAME 0x0004 /* this is an add operation */
  192. #define XFS_DA_OP_OKNOENT 0x0008 /* lookup/add op, ENOENT ok, else die */
  193. #define XFS_DA_OP_CILOOKUP 0x0010 /* lookup to return CI name if found */
  194. #define XFS_DA_OP_FLAGS \
  195. { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \
  196. { XFS_DA_OP_RENAME, "RENAME" }, \
  197. { XFS_DA_OP_ADDNAME, "ADDNAME" }, \
  198. { XFS_DA_OP_OKNOENT, "OKNOENT" }, \
  199. { XFS_DA_OP_CILOOKUP, "CILOOKUP" }
  200. /*
  201. * Storage for holding state during Btree searches and split/join ops.
  202. *
  203. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  204. * fanout to the Btree, we can support over 900 million directory blocks,
  205. * which is slightly more than enough.
  206. */
  207. typedef struct xfs_da_state_blk {
  208. struct xfs_buf *bp; /* buffer containing block */
  209. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  210. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  211. int index; /* relevant index into block */
  212. xfs_dahash_t hashval; /* last hash value in block */
  213. int magic; /* blk's magic number, ie: blk type */
  214. } xfs_da_state_blk_t;
  215. typedef struct xfs_da_state_path {
  216. int active; /* number of active levels */
  217. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  218. } xfs_da_state_path_t;
  219. typedef struct xfs_da_state {
  220. xfs_da_args_t *args; /* filename arguments */
  221. struct xfs_mount *mp; /* filesystem mount point */
  222. unsigned int blocksize; /* logical block size */
  223. unsigned int node_ents; /* how many entries in danode */
  224. xfs_da_state_path_t path; /* search/split paths */
  225. xfs_da_state_path_t altpath; /* alternate path for join */
  226. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  227. unsigned char extravalid; /* T/F: extrablk is in use */
  228. unsigned char extraafter; /* T/F: extrablk is after new */
  229. xfs_da_state_blk_t extrablk; /* for double-splits on leaves */
  230. /* for dirv2 extrablk is data */
  231. } xfs_da_state_t;
  232. /*
  233. * Utility macros to aid in logging changed structure fields.
  234. */
  235. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  236. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  237. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  238. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  239. /*
  240. * Name ops for directory and/or attr name operations
  241. */
  242. struct xfs_nameops {
  243. xfs_dahash_t (*hashname)(struct xfs_name *);
  244. enum xfs_dacmp (*compname)(struct xfs_da_args *,
  245. const unsigned char *, int);
  246. };
  247. /*========================================================================
  248. * Function prototypes.
  249. *========================================================================*/
  250. /*
  251. * Routines used for growing the Btree.
  252. */
  253. int xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno,
  254. int level, struct xfs_buf **bpp, int whichfork);
  255. int xfs_da3_split(xfs_da_state_t *state);
  256. /*
  257. * Routines used for shrinking the Btree.
  258. */
  259. int xfs_da3_join(xfs_da_state_t *state);
  260. void xfs_da3_fixhashpath(struct xfs_da_state *state,
  261. struct xfs_da_state_path *path_to_to_fix);
  262. /*
  263. * Routines used for finding things in the Btree.
  264. */
  265. int xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result);
  266. int xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  267. int forward, int release, int *result);
  268. /*
  269. * Utility routines.
  270. */
  271. int xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  272. xfs_da_state_blk_t *new_blk);
  273. int xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
  274. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  275. struct xfs_buf **bpp, int which_fork);
  276. extern const struct xfs_buf_ops xfs_da3_node_buf_ops;
  277. /*
  278. * Utility routines.
  279. */
  280. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  281. int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
  282. int count);
  283. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  284. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  285. struct xfs_buf **bp, int whichfork);
  286. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  287. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  288. struct xfs_buf **bpp, int whichfork,
  289. const struct xfs_buf_ops *ops);
  290. xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  291. xfs_dablk_t bno, xfs_daddr_t mapped_bno,
  292. int whichfork, const struct xfs_buf_ops *ops);
  293. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  294. struct xfs_buf *dead_buf);
  295. uint xfs_da_hashname(const __uint8_t *name_string, int name_length);
  296. enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
  297. const unsigned char *name, int len);
  298. xfs_da_state_t *xfs_da_state_alloc(void);
  299. void xfs_da_state_free(xfs_da_state_t *state);
  300. extern struct kmem_zone *xfs_da_state_zone;
  301. extern const struct xfs_nameops xfs_default_nameops;
  302. #endif /* __XFS_DA_BTREE_H__ */