xref: /freebsd-14-stable/sys/fs/unionfs/union.h (revision 6d118b95861220fdbbe82e933429091f7c0501eb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994 The Regents of the University of California.
5  * Copyright (c) 1994 Jan-Simon Pendry.
6  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
7  * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
8  * All rights reserved.
9  *
10  * This code is derived from software donated to Berkeley by
11  * Jan-Simon Pendry.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)union.h	8.9 (Berkeley) 12/10/94
38  */
39 
40 #ifdef _KERNEL
41 
42 /* copy method of attr from lower to upper */
43 typedef enum _unionfs_copymode {
44 	UNIONFS_TRADITIONAL = 0,
45 	UNIONFS_TRANSPARENT,
46 	UNIONFS_MASQUERADE
47 } unionfs_copymode;
48 
49 /* whiteout policy of upper layer */
50 typedef enum _unionfs_whitemode {
51        UNIONFS_WHITE_ALWAYS = 0,
52        UNIONFS_WHITE_WHENNEEDED
53 } unionfs_whitemode;
54 
55 struct unionfs_mount {
56 	struct mount   *um_lowermp;     /* MNT_REFed lower mount object */
57 	struct mount   *um_uppermp;     /* MNT_REFed upper mount object */
58 	struct vnode   *um_lowervp;	/* VREFed once */
59 	struct vnode   *um_uppervp;	/* VREFed once */
60 	struct vnode   *um_rootvp;	/* ROOT vnode */
61 	struct mount_upper_node	um_lower_link;	/* node in lower FS list of uppers */
62 	struct mount_upper_node	um_upper_link;	/* node in upper FS list of uppers */
63 	unionfs_copymode um_copymode;
64 	unionfs_whitemode um_whitemode;
65 	uid_t		um_uid;
66 	gid_t		um_gid;
67 	u_short		um_udir;
68 	u_short		um_ufile;
69 };
70 
71 /* unionfs status list */
72 struct unionfs_node_status {
73 	LIST_ENTRY(unionfs_node_status) uns_list;	/* Status list */
74 	pid_t		uns_pid;		/* current process id */
75 	int		uns_node_flag;		/* uns flag */
76 	int		uns_lower_opencnt;	/* open count of lower */
77 	int		uns_upper_opencnt;	/* open count of upper */
78 	int		uns_lower_openmode;	/* open mode of lower */
79 	int		uns_readdir_status;	/* read status of readdir */
80 };
81 
82 /* union node status flags */
83 #define	UNS_OPENL_4_READDIR	0x01	/* open lower layer for readdir */
84 
85 /* A cache of vnode references */
86 struct unionfs_node {
87 	struct vnode   *un_lowervp;		/* lower side vnode */
88 	struct vnode   *un_uppervp;		/* upper side vnode */
89 	struct vnode   *un_dvp;			/* parent unionfs vnode */
90 	struct vnode   *un_vnode;		/* Back pointer */
91 	LIST_HEAD(, unionfs_node_status) un_unshead;
92 						/* unionfs status head */
93 	LIST_HEAD(unionfs_node_hashhead, unionfs_node) *un_hashtbl;
94 						/* dir vnode hash table */
95 	union {
96 		LIST_ENTRY(unionfs_node) un_hash; /* hash list entry */
97 		STAILQ_ENTRY(unionfs_node) un_rele; /* deferred release list */
98 	};
99 
100 	char           *un_path;		/* path */
101 	int		un_pathlen;		/* strlen of path */
102 	int		un_flag;		/* unionfs node flag */
103 };
104 
105 /*
106  * unionfs node flags
107  * It needs the vnode with exclusive lock, when changing the un_flag variable.
108  */
109 #define UNIONFS_OPENEXTL	0x01	/* openextattr (lower) */
110 #define UNIONFS_OPENEXTU	0x02	/* openextattr (upper) */
111 
112 extern struct vop_vector unionfs_vnodeops;
113 
114 static inline struct unionfs_node *
unionfs_check_vnode(struct vnode * vp,const char * file __unused,int line __unused)115 unionfs_check_vnode(struct vnode *vp, const char *file __unused,
116     int line __unused)
117 {
118 	/*
119 	 * unionfs_lock() needs the NULL check here, as it explicitly
120 	 * handles the case in which the vnode has been vgonel()'ed.
121 	 */
122 	KASSERT(vp->v_op == &unionfs_vnodeops || vp->v_data == NULL,
123 	    ("%s:%d: non-unionfs vnode %p", file, line, vp));
124 	return ((struct unionfs_node *)vp->v_data);
125 }
126 
127 #define	MOUNTTOUNIONFSMOUNT(mp) ((struct unionfs_mount *)((mp)->mnt_data))
128 #define	VTOUNIONFS(vp) unionfs_check_vnode(vp, __FILE__, __LINE__)
129 #define	UNIONFSTOV(xp) ((xp)->un_vnode)
130 
131 int	unionfs_init(struct vfsconf *);
132 int	unionfs_uninit(struct vfsconf *);
133 int	unionfs_nodeget(struct mount *, struct vnode *, struct vnode *,
134 	    struct vnode *, struct vnode **, struct componentname *);
135 void	unionfs_noderem(struct vnode *);
136 void	unionfs_get_node_status(struct unionfs_node *, struct thread *,
137 	    struct unionfs_node_status **);
138 void	unionfs_tryrem_node_status(struct unionfs_node *,
139 	    struct unionfs_node_status *);
140 int	unionfs_check_rmdir(struct vnode *, struct ucred *, struct thread *td);
141 int	unionfs_copyfile(struct unionfs_node *, int, struct ucred *,
142 	    struct thread *);
143 void	unionfs_create_uppervattr_core(struct unionfs_mount *, struct vattr *,
144 	    struct vattr *, struct thread *);
145 int	unionfs_create_uppervattr(struct unionfs_mount *, struct vnode *,
146 	    struct vattr *, struct ucred *, struct thread *);
147 int	unionfs_mkshadowdir(struct unionfs_mount *, struct vnode *,
148 	    struct unionfs_node *, struct componentname *, struct thread *);
149 int	unionfs_mkwhiteout(struct vnode *, struct vnode *,
150 	    struct componentname *, struct thread *, char *, int);
151 int	unionfs_relookup(struct vnode *, struct vnode **,
152 	    struct componentname *, struct componentname *, struct thread *,
153 	    char *, int, u_long);
154 int	unionfs_relookup_for_create(struct vnode *, struct componentname *,
155 	    struct thread *);
156 int	unionfs_relookup_for_delete(struct vnode *, struct componentname *,
157 	    struct thread *);
158 int	unionfs_relookup_for_rename(struct vnode *, struct componentname *,
159 	    struct thread *);
160 void	unionfs_forward_vop_start_pair(struct vnode *, int *,
161 	    struct vnode *, int *);
162 bool	unionfs_forward_vop_finish_pair(struct vnode *, struct vnode *, int,
163 	    struct vnode *, struct vnode *, int);
164 
165 static inline void
unionfs_forward_vop_start(struct vnode * basevp,int * lkflags)166 unionfs_forward_vop_start(struct vnode *basevp, int *lkflags)
167 {
168 	unionfs_forward_vop_start_pair(basevp, lkflags, NULL, NULL);
169 }
170 
171 static inline bool
unionfs_forward_vop_finish(struct vnode * unionvp,struct vnode * basevp,int lkflags)172 unionfs_forward_vop_finish(struct vnode *unionvp, struct vnode *basevp,
173     int lkflags)
174 {
175 	return (unionfs_forward_vop_finish_pair(unionvp, basevp, lkflags,
176 	    NULL, NULL, 0));
177 }
178 
179 #define	UNIONFSVPTOLOWERVP(vp) (VTOUNIONFS(vp)->un_lowervp)
180 #define	UNIONFSVPTOUPPERVP(vp) (VTOUNIONFS(vp)->un_uppervp)
181 
182 #ifdef MALLOC_DECLARE
183 MALLOC_DECLARE(M_UNIONFSNODE);
184 MALLOC_DECLARE(M_UNIONFSPATH);
185 #endif
186 
187 #ifdef UNIONFS_DEBUG
188 #define UNIONFSDEBUG(format, args...) printf(format ,## args)
189 #else
190 #define UNIONFSDEBUG(format, args...)
191 #endif				/* UNIONFS_DEBUG */
192 
193 #endif				/* _KERNEL */
194