xfs_da_btree.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2000, 2002 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_DA_BTREE_H__
  33. #define __XFS_DA_BTREE_H__
  34. struct xfs_buf;
  35. struct xfs_bmap_free;
  36. struct xfs_inode;
  37. struct xfs_mount;
  38. struct xfs_trans;
  39. struct zone;
  40. /*========================================================================
  41. * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
  42. *========================================================================*/
  43. /*
  44. * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
  45. *
  46. * Is is used to manage a doubly linked list of all blocks at the same
  47. * level in the Btree, and to identify which type of block this is.
  48. */
  49. #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
  50. #define XFS_DIR_LEAF_MAGIC 0xfeeb /* magic number: directory leaf blks */
  51. #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
  52. #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
  53. #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
  54. #define XFS_DIRX_LEAF_MAGIC(mp) \
  55. (XFS_DIR_IS_V1(mp) ? XFS_DIR_LEAF_MAGIC : XFS_DIR2_LEAFN_MAGIC)
  56. typedef struct xfs_da_blkinfo {
  57. xfs_dablk_t forw; /* previous block in list */
  58. xfs_dablk_t back; /* following block in list */
  59. __uint16_t magic; /* validity check on block */
  60. __uint16_t pad; /* unused */
  61. } xfs_da_blkinfo_t;
  62. /*
  63. * This is the structure of the root and intermediate nodes in the Btree.
  64. * The leaf nodes are defined above.
  65. *
  66. * Entries are not packed.
  67. *
  68. * Since we have duplicate keys, use a binary search but always follow
  69. * all match in the block, not just the first match found.
  70. */
  71. #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
  72. typedef struct xfs_da_intnode {
  73. struct xfs_da_node_hdr { /* constant-structure header block */
  74. xfs_da_blkinfo_t info; /* block type, links, etc. */
  75. __uint16_t count; /* count of active entries */
  76. __uint16_t level; /* level above leaves (leaf == 0) */
  77. } hdr;
  78. struct xfs_da_node_entry {
  79. xfs_dahash_t hashval; /* hash value for this descendant */
  80. xfs_dablk_t before; /* Btree block before this key */
  81. } btree[1]; /* variable sized array of keys */
  82. } xfs_da_intnode_t;
  83. typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
  84. typedef struct xfs_da_node_entry xfs_da_node_entry_t;
  85. #define XFS_DA_MAXHASH ((xfs_dahash_t)-1) /* largest valid hash value */
  86. /*
  87. * Macros used by directory code to interface to the filesystem.
  88. */
  89. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_LBSIZE)
  90. int xfs_lbsize(struct xfs_mount *mp);
  91. #define XFS_LBSIZE(mp) xfs_lbsize(mp)
  92. #else
  93. #define XFS_LBSIZE(mp) ((mp)->m_sb.sb_blocksize)
  94. #endif
  95. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_LBLOG)
  96. int xfs_lblog(struct xfs_mount *mp);
  97. #define XFS_LBLOG(mp) xfs_lblog(mp)
  98. #else
  99. #define XFS_LBLOG(mp) ((mp)->m_sb.sb_blocklog)
  100. #endif
  101. /*
  102. * Macros used by directory code to interface to the kernel
  103. */
  104. /*
  105. * Macros used to manipulate directory off_t's
  106. */
  107. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_MAKE_BNOENTRY)
  108. __uint32_t xfs_da_make_bnoentry(struct xfs_mount *mp, xfs_dablk_t bno,
  109. int entry);
  110. #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
  111. xfs_da_make_bnoentry(mp,bno,entry)
  112. #else
  113. #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
  114. (((bno) << (mp)->m_dircook_elog) | (entry))
  115. #endif
  116. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_MAKE_COOKIE)
  117. xfs_off_t xfs_da_make_cookie(struct xfs_mount *mp, xfs_dablk_t bno, int entry,
  118. xfs_dahash_t hash);
  119. #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
  120. xfs_da_make_cookie(mp,bno,entry,hash)
  121. #else
  122. #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
  123. (((xfs_off_t)XFS_DA_MAKE_BNOENTRY(mp, bno, entry) << 32) | (hash))
  124. #endif
  125. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_HASH)
  126. xfs_dahash_t xfs_da_cookie_hash(struct xfs_mount *mp, xfs_off_t cookie);
  127. #define XFS_DA_COOKIE_HASH(mp,cookie) xfs_da_cookie_hash(mp,cookie)
  128. #else
  129. #define XFS_DA_COOKIE_HASH(mp,cookie) ((xfs_dahash_t)(cookie))
  130. #endif
  131. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_BNO)
  132. xfs_dablk_t xfs_da_cookie_bno(struct xfs_mount *mp, xfs_off_t cookie);
  133. #define XFS_DA_COOKIE_BNO(mp,cookie) xfs_da_cookie_bno(mp,cookie)
  134. #else
  135. #define XFS_DA_COOKIE_BNO(mp,cookie) \
  136. (((xfs_off_t)(cookie) >> 31) == -1LL ? \
  137. (xfs_dablk_t)0 : \
  138. (xfs_dablk_t)((xfs_off_t)(cookie) >> ((mp)->m_dircook_elog + 32)))
  139. #endif
  140. #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_ENTRY)
  141. int xfs_da_cookie_entry(struct xfs_mount *mp, xfs_off_t cookie);
  142. #define XFS_DA_COOKIE_ENTRY(mp,cookie) xfs_da_cookie_entry(mp,cookie)
  143. #else
  144. #define XFS_DA_COOKIE_ENTRY(mp,cookie) \
  145. (((xfs_off_t)(cookie) >> 31) == -1LL ? \
  146. (xfs_dablk_t)0 : \
  147. (xfs_dablk_t)(((xfs_off_t)(cookie) >> 32) & \
  148. ((1 << (mp)->m_dircook_elog) - 1)))
  149. #endif
  150. /*========================================================================
  151. * Btree searching and modification structure definitions.
  152. *========================================================================*/
  153. /*
  154. * Structure to ease passing around component names.
  155. */
  156. typedef struct xfs_da_args {
  157. uchar_t *name; /* string (maybe not NULL terminated) */
  158. int namelen; /* length of string (maybe no NULL) */
  159. uchar_t *value; /* set of bytes (maybe contain NULLs) */
  160. int valuelen; /* length of value */
  161. int flags; /* argument flags (eg: ATTR_NOCREATE) */
  162. xfs_dahash_t hashval; /* hash value of name */
  163. xfs_ino_t inumber; /* input/output inode number */
  164. struct xfs_inode *dp; /* directory inode to manipulate */
  165. xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
  166. struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
  167. struct xfs_trans *trans; /* current trans (changes over time) */
  168. xfs_extlen_t total; /* total blocks needed, for 1st bmap */
  169. int whichfork; /* data or attribute fork */
  170. xfs_dablk_t blkno; /* blkno of attr leaf of interest */
  171. int index; /* index of attr of interest in blk */
  172. xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
  173. int rmtblkcnt; /* remote attr value block count */
  174. xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
  175. int index2; /* index of 2nd attr in blk */
  176. xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
  177. int rmtblkcnt2; /* remote attr value block count */
  178. unsigned char justcheck; /* T/F: check for ok with no space */
  179. unsigned char rename; /* T/F: this is an atomic rename op */
  180. unsigned char addname; /* T/F: this is an add operation */
  181. unsigned char oknoent; /* T/F: ok to return ENOENT, else die */
  182. } xfs_da_args_t;
  183. /*
  184. * Structure to describe buffer(s) for a block.
  185. * This is needed in the directory version 2 format case, when
  186. * multiple non-contiguous fsblocks might be needed to cover one
  187. * logical directory block.
  188. * If the buffer count is 1 then the data pointer points to the
  189. * same place as the b_addr field for the buffer, else to kmem_alloced memory.
  190. */
  191. typedef struct xfs_dabuf {
  192. int nbuf; /* number of buffer pointers present */
  193. short dirty; /* data needs to be copied back */
  194. short bbcount; /* how large is data in bbs */
  195. void *data; /* pointer for buffers' data */
  196. #ifdef XFS_DABUF_DEBUG
  197. inst_t *ra; /* return address of caller to make */
  198. struct xfs_dabuf *next; /* next in global chain */
  199. struct xfs_dabuf *prev; /* previous in global chain */
  200. struct xfs_buftarg *target; /* device for buffer */
  201. xfs_daddr_t blkno; /* daddr first in bps[0] */
  202. #endif
  203. struct xfs_buf *bps[1]; /* actually nbuf of these */
  204. } xfs_dabuf_t;
  205. #define XFS_DA_BUF_SIZE(n) \
  206. (sizeof(xfs_dabuf_t) + sizeof(struct xfs_buf *) * ((n) - 1))
  207. #ifdef XFS_DABUF_DEBUG
  208. extern xfs_dabuf_t *xfs_dabuf_global_list;
  209. #endif
  210. /*
  211. * Storage for holding state during Btree searches and split/join ops.
  212. *
  213. * Only need space for 5 intermediate nodes. With a minimum of 62-way
  214. * fanout to the Btree, we can support over 900 million directory blocks,
  215. * which is slightly more than enough.
  216. */
  217. typedef struct xfs_da_state_blk {
  218. xfs_dabuf_t *bp; /* buffer containing block */
  219. xfs_dablk_t blkno; /* filesystem blkno of buffer */
  220. xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
  221. int index; /* relevant index into block */
  222. xfs_dahash_t hashval; /* last hash value in block */
  223. int magic; /* blk's magic number, ie: blk type */
  224. } xfs_da_state_blk_t;
  225. typedef struct xfs_da_state_path {
  226. int active; /* number of active levels */
  227. xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
  228. } xfs_da_state_path_t;
  229. typedef struct xfs_da_state {
  230. xfs_da_args_t *args; /* filename arguments */
  231. struct xfs_mount *mp; /* filesystem mount point */
  232. unsigned int blocksize; /* logical block size */
  233. unsigned int node_ents; /* how many entries in danode */
  234. xfs_da_state_path_t path; /* search/split paths */
  235. xfs_da_state_path_t altpath; /* alternate path for join */
  236. unsigned char inleaf; /* insert into 1->lf, 0->splf */
  237. unsigned char extravalid; /* T/F: extrablk is in use */
  238. unsigned char extraafter; /* T/F: extrablk is after new */
  239. xfs_da_state_blk_t extrablk; /* for double-splits on leafs */
  240. /* for dirv2 extrablk is data */
  241. } xfs_da_state_t;
  242. /*
  243. * Utility macros to aid in logging changed structure fields.
  244. */
  245. #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
  246. #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
  247. (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
  248. (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
  249. #ifdef __KERNEL__
  250. /*========================================================================
  251. * Function prototypes for the kernel.
  252. *========================================================================*/
  253. /*
  254. * Routines used for growing the Btree.
  255. */
  256. int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
  257. xfs_dabuf_t **bpp, int whichfork);
  258. int xfs_da_split(xfs_da_state_t *state);
  259. /*
  260. * Routines used for shrinking the Btree.
  261. */
  262. int xfs_da_join(xfs_da_state_t *state);
  263. void xfs_da_fixhashpath(xfs_da_state_t *state,
  264. xfs_da_state_path_t *path_to_to_fix);
  265. /*
  266. * Routines used for finding things in the Btree.
  267. */
  268. int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
  269. int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  270. int forward, int release, int *result);
  271. /*
  272. * Utility routines.
  273. */
  274. int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  275. xfs_da_state_blk_t *new_blk);
  276. /*
  277. * Utility routines.
  278. */
  279. int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
  280. int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  281. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  282. xfs_dabuf_t **bp, int whichfork);
  283. int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  284. xfs_dablk_t bno, xfs_daddr_t mappedbno,
  285. xfs_dabuf_t **bpp, int whichfork);
  286. xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
  287. xfs_dablk_t bno, int whichfork);
  288. int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  289. xfs_dabuf_t *dead_buf);
  290. uint xfs_da_hashname(uchar_t *name_string, int name_length);
  291. uint xfs_da_log2_roundup(uint i);
  292. xfs_da_state_t *xfs_da_state_alloc(void);
  293. void xfs_da_state_free(xfs_da_state_t *state);
  294. void xfs_da_buf_done(xfs_dabuf_t *dabuf);
  295. void xfs_da_log_buf(struct xfs_trans *tp, xfs_dabuf_t *dabuf, uint first,
  296. uint last);
  297. void xfs_da_brelse(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
  298. void xfs_da_binval(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
  299. xfs_daddr_t xfs_da_blkno(xfs_dabuf_t *dabuf);
  300. extern struct kmem_zone *xfs_da_state_zone;
  301. #endif /* __KERNEL__ */
  302. #endif /* __XFS_DA_BTREE_H__ */