xfs_da_btree.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #ifndef __XFS_DA_BTREE_H__
  8. #define __XFS_DA_BTREE_H__
  9. struct xfs_inode;
  10. struct xfs_trans;
  11. /*
  12. * Directory/attribute geometry information. There will be one of these for each
  13. * data fork type, and it will be passed around via the xfs_da_args. Global
  14. * structures will be attached to the xfs_mount.
  15. */
  16. struct xfs_da_geometry {
  17. unsigned int blksize; /* da block size in bytes */
  18. unsigned int fsbcount; /* da block size in filesystem blocks */
  19. uint8_t fsblog; /* log2 of _filesystem_ block size */
  20. uint8_t blklog; /* log2 of da block size */
  21. unsigned int node_hdr_size; /* danode header size in bytes */
  22. unsigned int node_ents; /* # of entries in a danode */
  23. unsigned int magicpct; /* 37% of block size in bytes */
  24. xfs_dablk_t datablk; /* blockno of dir data v2 */
  25. unsigned int leaf_hdr_size; /* dir2 leaf header size */
  26. unsigned int leaf_max_ents; /* # of entries in dir2 leaf */
  27. xfs_dablk_t leafblk; /* blockno of leaf data v2 */
  28. unsigned int free_hdr_size; /* dir2 free header size */
  29. unsigned int free_max_bests; /* # of bests entries in dir2 free */
  30. xfs_dablk_t freeblk; /* blockno of free data v2 */
  31. xfs_extnum_t max_extents; /* Max. extents in corresponding fork */
  32. xfs_dir2_data_aoff_t data_first_offset;
  33. size_t data_entry_offset;
  34. };
  35. /*========================================================================
  36. * Btree searching and modification structure definitions.
  37. *========================================================================*/
  38. /*
  39. * Search comparison results
  40. */
  41. enum xfs_dacmp {
  42. XFS_CMP_DIFFERENT, /* names are completely different */
  43. XFS_CMP_EXACT, /* names are exactly the same */
  44. XFS_CMP_CASE /* names are same but differ in case */
  45. };
  46. /*
  47. * Structure to ease passing around component names.
  48. */
  49. typedef struct xfs_da_args {
  50. struct xfs_da_geometry *geo; /* da block geometry */
  51. const uint8_t *name; /* string (maybe not NULL terminated) */
  52. int namelen; /* length of string (maybe no NULL) */
  53. uint8_t filetype; /* filetype of inode for directories */
  54. void *value; /* set of bytes (maybe contain NULLs) */
  55. int valuelen; /* length of value */
  56. unsigned int attr_filter; /* XFS_ATTR_{ROOT,SECURE,INCOMPLETE} */
  57. unsigned int attr_flags; /* XATTR_{CREATE,REPLACE} */
  58. xfs_dahash_t hashval; /* hash value of name */
  59. xfs_ino_t inumber; /* input/output inode number */
  60. struct xfs_inode *dp; /* directory inode to manipulate */
  61. struct xfs_trans *trans; /* current trans (changes over time) */
  62. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  63. int whichfork; /* data or attribute fork */
  64. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  65. int index; /* index of attr of interest in blk */
  66. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  67. int rmtblkcnt; /* remote attr value block count */
  68. int rmtvaluelen; /* remote attr value length in bytes */
  69. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  70. int index2; /* index of 2nd attr in blk */
  71. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  72. int rmtblkcnt2; /* remote attr value block count */
  73. int rmtvaluelen2; /* remote attr value length in bytes */
  74. uint32_t op_flags; /* operation flags */
  75. enum xfs_dacmp cmpresult; /* name compare result for lookups */
  76. } xfs_da_args_t;
  77. /*
  78. * Operation flags:
  79. */
  80. #define XFS_DA_OP_JUSTCHECK (1u << 0) /* check for ok with no space */
  81. #define XFS_DA_OP_REPLACE (1u << 1) /* this is an atomic replace op */
  82. #define XFS_DA_OP_ADDNAME (1u << 2) /* this is an add operation */
  83. #define XFS_DA_OP_OKNOENT (1u << 3) /* lookup op, ENOENT ok, else die */
  84. #define XFS_DA_OP_CILOOKUP (1u << 4) /* lookup returns CI name if found */
  85. #define XFS_DA_OP_NOTIME (1u << 5) /* don't update inode timestamps */
  86. #define XFS_DA_OP_REMOVE (1u << 6) /* this is a remove operation */
  87. #define XFS_DA_OP_RECOVERY (1u << 7) /* Log recovery operation */
  88. #define XFS_DA_OP_LOGGED (1u << 8) /* Use intent items to track op */
  89. #define XFS_DA_OP_FLAGS \
  90. { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \
  91. { XFS_DA_OP_REPLACE, "REPLACE" }, \
  92. { XFS_DA_OP_ADDNAME, "ADDNAME" }, \
  93. { XFS_DA_OP_OKNOENT, "OKNOENT" }, \
  94. { XFS_DA_OP_CILOOKUP, "CILOOKUP" }, \
  95. { XFS_DA_OP_NOTIME, "NOTIME" }, \
  96. { XFS_DA_OP_REMOVE, "REMOVE" }, \
  97. { XFS_DA_OP_RECOVERY, "RECOVERY" }, \
  98. { XFS_DA_OP_LOGGED, "LOGGED" }
  99. /*
  100. * Storage for holding state during Btree searches and split/join ops.
  101. *
  102. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  103. * fanout to the Btree, we can support over 900 million directory blocks,
  104. * which is slightly more than enough.
  105. */
  106. typedef struct xfs_da_state_blk {
  107. struct xfs_buf *bp; /* buffer containing block */
  108. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  109. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  110. int index; /* relevant index into block */
  111. xfs_dahash_t hashval; /* last hash value in block */
  112. int magic; /* blk's magic number, ie: blk type */
  113. } xfs_da_state_blk_t;
  114. typedef struct xfs_da_state_path {
  115. int active; /* number of active levels */
  116. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  117. } xfs_da_state_path_t;
  118. typedef struct xfs_da_state {
  119. xfs_da_args_t *args; /* filename arguments */
  120. struct xfs_mount *mp; /* filesystem mount point */
  121. xfs_da_state_path_t path; /* search/split paths */
  122. xfs_da_state_path_t altpath; /* alternate path for join */
  123. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  124. unsigned char extravalid; /* T/F: extrablk is in use */
  125. unsigned char extraafter; /* T/F: extrablk is after new */
  126. xfs_da_state_blk_t extrablk; /* for double-splits on leaves */
  127. /* for dirv2 extrablk is data */
  128. } xfs_da_state_t;
  129. /*
  130. * In-core version of the node header to abstract the differences in the v2 and
  131. * v3 disk format of the headers. Callers need to convert to/from disk format as
  132. * appropriate.
  133. */
  134. struct xfs_da3_icnode_hdr {
  135. uint32_t forw;
  136. uint32_t back;
  137. uint16_t magic;
  138. uint16_t count;
  139. uint16_t level;
  140. /*
  141. * Pointer to the on-disk format entries, which are behind the
  142. * variable size (v4 vs v5) header in the on-disk block.
  143. */
  144. struct xfs_da_node_entry *btree;
  145. };
  146. /*
  147. * Utility macros to aid in logging changed structure fields.
  148. */
  149. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  150. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  151. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  152. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  153. /*========================================================================
  154. * Function prototypes.
  155. *========================================================================*/
  156. /*
  157. * Routines used for growing the Btree.
  158. */
  159. int xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno,
  160. int level, struct xfs_buf **bpp, int whichfork);
  161. int xfs_da3_split(xfs_da_state_t *state);
  162. /*
  163. * Routines used for shrinking the Btree.
  164. */
  165. int xfs_da3_join(xfs_da_state_t *state);
  166. void xfs_da3_fixhashpath(struct xfs_da_state *state,
  167. struct xfs_da_state_path *path_to_to_fix);
  168. /*
  169. * Routines used for finding things in the Btree.
  170. */
  171. int xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result);
  172. int xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  173. int forward, int release, int *result);
  174. /*
  175. * Utility routines.
  176. */
  177. int xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  178. xfs_da_state_blk_t *new_blk);
  179. int xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
  180. xfs_dablk_t bno, struct xfs_buf **bpp, int whichfork);
  181. int xfs_da3_node_read_mapped(struct xfs_trans *tp, struct xfs_inode *dp,
  182. xfs_daddr_t mappedbno, struct xfs_buf **bpp,
  183. int whichfork);
  184. /*
  185. * Utility routines.
  186. */
  187. #define XFS_DABUF_MAP_HOLE_OK (1u << 0)
  188. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  189. int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
  190. int count);
  191. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  192. xfs_dablk_t bno, struct xfs_buf **bp, int whichfork);
  193. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  194. xfs_dablk_t bno, unsigned int flags, struct xfs_buf **bpp,
  195. int whichfork, const struct xfs_buf_ops *ops);
  196. int xfs_da_reada_buf(struct xfs_inode *dp, xfs_dablk_t bno,
  197. unsigned int flags, int whichfork,
  198. const struct xfs_buf_ops *ops);
  199. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  200. struct xfs_buf *dead_buf);
  201. uint xfs_da_hashname(const uint8_t *name_string, int name_length);
  202. enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
  203. const unsigned char *name, int len);
  204. struct xfs_da_state *xfs_da_state_alloc(struct xfs_da_args *args);
  205. void xfs_da_state_free(xfs_da_state_t *state);
  206. void xfs_da_state_reset(struct xfs_da_state *state, struct xfs_da_args *args);
  207. void xfs_da3_node_hdr_from_disk(struct xfs_mount *mp,
  208. struct xfs_da3_icnode_hdr *to, struct xfs_da_intnode *from);
  209. void xfs_da3_node_hdr_to_disk(struct xfs_mount *mp,
  210. struct xfs_da_intnode *to, struct xfs_da3_icnode_hdr *from);
  211. extern struct kmem_cache *xfs_da_state_cache;
  212. #endif /* __XFS_DA_BTREE_H__ */