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