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 10 struct xfs_inode; 11 struct xfs_trans; 12 struct zone; 13 struct xfs_dir_ops; 14 15 /* 16 * Directory/attribute geometry information. There will be one of these for each 17 * data fork type, and it will be passed around via the xfs_da_args. Global 18 * structures will be attached to the xfs_mount. 19 */ 20 struct xfs_da_geometry { 21 int blksize; /* da block size in bytes */ 22 int fsbcount; /* da block size in filesystem blocks */ 23 uint8_t fsblog; /* log2 of _filesystem_ block size */ 24 uint8_t blklog; /* log2 of da block size */ 25 uint node_ents; /* # of entries in a danode */ 26 int magicpct; /* 37% of block size in bytes */ 27 xfs_dablk_t datablk; /* blockno of dir data v2 */ 28 xfs_dablk_t leafblk; /* blockno of leaf data v2 */ 29 xfs_dablk_t freeblk; /* blockno of free data v2 */ 30 }; 31 32 /*======================================================================== 33 * Btree searching and modification structure definitions. 34 *========================================================================*/ 35 36 /* 37 * Search comparison results 38 */ 39 enum xfs_dacmp { 40 XFS_CMP_DIFFERENT, /* names are completely different */ 41 XFS_CMP_EXACT, /* names are exactly the same */ 42 XFS_CMP_CASE /* names are same but differ in case */ 43 }; 44 45 /* 46 * Structure to ease passing around component names. 47 */ 48 typedef struct xfs_da_args { 49 struct xfs_da_geometry *geo; /* da block geometry */ 50 const uint8_t *name; /* string (maybe not NULL terminated) */ 51 int namelen; /* length of string (maybe no NULL) */ 52 uint8_t filetype; /* filetype of inode for directories */ 53 uint8_t *value; /* set of bytes (maybe contain NULLs) */ 54 int valuelen; /* length of value */ 55 int flags; /* argument flags (eg: ATTR_NOCREATE) */ 56 xfs_dahash_t hashval; /* hash value of name */ 57 xfs_ino_t inumber; /* input/output inode number */ 58 struct xfs_inode *dp; /* directory inode to manipulate */ 59 struct xfs_trans *trans; /* current trans (changes over time) */ 60 xfs_extlen_t total; /* total blocks needed, for 1st bmap */ 61 int whichfork; /* data or attribute fork */ 62 xfs_dablk_t blkno; /* blkno of attr leaf of interest */ 63 int index; /* index of attr of interest in blk */ 64 xfs_dablk_t rmtblkno; /* remote attr value starting blkno */ 65 int rmtblkcnt; /* remote attr value block count */ 66 int rmtvaluelen; /* remote attr value length in bytes */ 67 xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */ 68 int index2; /* index of 2nd attr in blk */ 69 xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */ 70 int rmtblkcnt2; /* remote attr value block count */ 71 int rmtvaluelen2; /* remote attr value length in bytes */ 72 int op_flags; /* operation flags */ 73 enum xfs_dacmp cmpresult; /* name compare result for lookups */ 74 } xfs_da_args_t; 75 76 /* 77 * Operation flags: 78 */ 79 #define XFS_DA_OP_JUSTCHECK 0x0001 /* check for ok with no space */ 80 #define XFS_DA_OP_RENAME 0x0002 /* this is an atomic rename op */ 81 #define XFS_DA_OP_ADDNAME 0x0004 /* this is an add operation */ 82 #define XFS_DA_OP_OKNOENT 0x0008 /* lookup/add op, ENOENT ok, else die */ 83 #define XFS_DA_OP_CILOOKUP 0x0010 /* lookup to return CI name if found */ 84 #define XFS_DA_OP_ALLOCVAL 0x0020 /* lookup to alloc buffer if found */ 85 #define XFS_DA_OP_INCOMPLETE 0x0040 /* lookup INCOMPLETE attr keys */ 86 87 #define XFS_DA_OP_FLAGS \ 88 { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \ 89 { XFS_DA_OP_RENAME, "RENAME" }, \ 90 { XFS_DA_OP_ADDNAME, "ADDNAME" }, \ 91 { XFS_DA_OP_OKNOENT, "OKNOENT" }, \ 92 { XFS_DA_OP_CILOOKUP, "CILOOKUP" }, \ 93 { XFS_DA_OP_ALLOCVAL, "ALLOCVAL" }, \ 94 { XFS_DA_OP_INCOMPLETE, "INCOMPLETE" } 95 96 /* 97 * Storage for holding state during Btree searches and split/join ops. 98 * 99 * Only need space for 5 intermediate nodes. With a minimum of 62-way 100 * fanout to the Btree, we can support over 900 million directory blocks, 101 * which is slightly more than enough. 102 */ 103 typedef struct xfs_da_state_blk { 104 struct xfs_buf *bp; /* buffer containing block */ 105 xfs_dablk_t blkno; /* filesystem blkno of buffer */ 106 xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */ 107 int index; /* relevant index into block */ 108 xfs_dahash_t hashval; /* last hash value in block */ 109 int magic; /* blk's magic number, ie: blk type */ 110 } xfs_da_state_blk_t; 111 112 typedef struct xfs_da_state_path { 113 int active; /* number of active levels */ 114 xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH]; 115 } xfs_da_state_path_t; 116 117 typedef struct xfs_da_state { 118 xfs_da_args_t *args; /* filename arguments */ 119 struct xfs_mount *mp; /* filesystem mount point */ 120 xfs_da_state_path_t path; /* search/split paths */ 121 xfs_da_state_path_t altpath; /* alternate path for join */ 122 unsigned char inleaf; /* insert into 1->lf, 0->splf */ 123 unsigned char extravalid; /* T/F: extrablk is in use */ 124 unsigned char extraafter; /* T/F: extrablk is after new */ 125 xfs_da_state_blk_t extrablk; /* for double-splits on leaves */ 126 /* for dirv2 extrablk is data */ 127 } xfs_da_state_t; 128 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 142 /* 143 * Utility macros to aid in logging changed structure fields. 144 */ 145 #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE)) 146 #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \ 147 (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \ 148 (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1) 149 150 /* 151 * Name ops for directory and/or attr name operations 152 */ 153 struct xfs_nameops { 154 xfs_dahash_t (*hashname)(struct xfs_name *); 155 enum xfs_dacmp (*compname)(struct xfs_da_args *, 156 const unsigned char *, int); 157 }; 158 159 160 /*======================================================================== 161 * Function prototypes. 162 *========================================================================*/ 163 164 /* 165 * Routines used for growing the Btree. 166 */ 167 int xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno, 168 int level, struct xfs_buf **bpp, int whichfork); 169 int xfs_da3_split(xfs_da_state_t *state); 170 171 /* 172 * Routines used for shrinking the Btree. 173 */ 174 int xfs_da3_join(xfs_da_state_t *state); 175 void xfs_da3_fixhashpath(struct xfs_da_state *state, 176 struct xfs_da_state_path *path_to_to_fix); 177 178 /* 179 * Routines used for finding things in the Btree. 180 */ 181 int xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result); 182 int xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path, 183 int forward, int release, int *result); 184 /* 185 * Utility routines. 186 */ 187 int xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk, 188 xfs_da_state_blk_t *new_blk); 189 int xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp, 190 xfs_dablk_t bno, xfs_daddr_t mappedbno, 191 struct xfs_buf **bpp, int which_fork); 192 193 /* 194 * Utility routines. 195 */ 196 int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno); 197 int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno, 198 int count); 199 int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp, 200 xfs_dablk_t bno, xfs_daddr_t mappedbno, 201 struct xfs_buf **bp, int whichfork); 202 int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp, 203 xfs_dablk_t bno, xfs_daddr_t mappedbno, 204 struct xfs_buf **bpp, int whichfork, 205 const struct xfs_buf_ops *ops); 206 int xfs_da_reada_buf(struct xfs_inode *dp, xfs_dablk_t bno, 207 xfs_daddr_t mapped_bno, int whichfork, 208 const struct xfs_buf_ops *ops); 209 int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno, 210 struct xfs_buf *dead_buf); 211 212 uint xfs_da_hashname(const uint8_t *name_string, int name_length); 213 enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args, 214 const unsigned char *name, int len); 215 216 217 xfs_da_state_t *xfs_da_state_alloc(void); 218 void xfs_da_state_free(xfs_da_state_t *state); 219 220 extern struct kmem_zone *xfs_da_state_zone; 221 extern const struct xfs_nameops xfs_default_nameops; 222 223 #endif /* __XFS_DA_BTREE_H__ */ 224