xref: /freebsd/sys/fs/unionfs/union.h (revision 0fac350c54d0a72f5341e15021efcde63eb58a4b)
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 
38 #ifdef _KERNEL
39 
40 /* copy method of attr from lower to upper */
41 typedef enum _unionfs_copymode {
42 	UNIONFS_TRADITIONAL = 0,
43 	UNIONFS_TRANSPARENT,
44 	UNIONFS_MASQUERADE
45 } unionfs_copymode;
46 
47 /* whiteout policy of upper layer */
48 typedef enum _unionfs_whitemode {
49        UNIONFS_WHITE_ALWAYS = 0,
50        UNIONFS_WHITE_WHENNEEDED
51 } unionfs_whitemode;
52 
53 struct unionfs_mount {
54 	struct vnode   *um_lowervp;	/* VREFed once */
55 	struct vnode   *um_uppervp;	/* VREFed once */
56 	struct vnode   *um_rootvp;	/* ROOT vnode */
57 	struct mount_upper_node	um_lower_link;	/* node in lower FS list of uppers */
58 	struct mount_upper_node	um_upper_link;	/* node in upper FS list of uppers */
59 	unionfs_copymode um_copymode;
60 	unionfs_whitemode um_whitemode;
61 	uid_t		um_uid;
62 	gid_t		um_gid;
63 	u_short		um_udir;
64 	u_short		um_ufile;
65 };
66 
67 /* unionfs status list */
68 struct unionfs_node_status {
69 	LIST_ENTRY(unionfs_node_status) uns_list;	/* Status list */
70 	pid_t		uns_pid;		/* current process id */
71 	int		uns_node_flag;		/* uns flag */
72 	int		uns_lower_opencnt;	/* open count of lower */
73 	int		uns_upper_opencnt;	/* open count of upper */
74 	int		uns_lower_openmode;	/* open mode of lower */
75 	int		uns_readdir_status;	/* read status of readdir */
76 };
77 
78 /* union node status flags */
79 #define	UNS_OPENL_4_READDIR	0x01	/* open lower layer for readdir */
80 
81 /* A cache of vnode references */
82 struct unionfs_node {
83 	struct vnode   *un_lowervp;		/* lower side vnode */
84 	struct vnode   *un_uppervp;		/* upper side vnode */
85 	struct vnode   *un_dvp;			/* parent unionfs vnode */
86 	struct vnode   *un_vnode;		/* Back pointer */
87 	LIST_HEAD(, unionfs_node_status) un_unshead;
88 						/* unionfs status head */
89 	LIST_HEAD(unionfs_node_hashhead, unionfs_node) *un_hashtbl;
90 						/* dir vnode hash table */
91 	union {
92 		LIST_ENTRY(unionfs_node) un_hash; /* hash list entry */
93 		STAILQ_ENTRY(unionfs_node) un_rele; /* deferred release list */
94 	};
95 
96 	char           *un_path;		/* path */
97 	int		un_pathlen;		/* strlen of path */
98 	int		un_flag;		/* unionfs node flag */
99 };
100 
101 /*
102  * unionfs node flags
103  * It needs the vnode with exclusive lock, when changing the un_flag variable.
104  */
105 #define UNIONFS_OPENEXTL	0x01	/* openextattr (lower) */
106 #define UNIONFS_OPENEXTU	0x02	/* openextattr (upper) */
107 
108 extern struct vop_vector unionfs_vnodeops;
109 
110 static inline struct unionfs_node *
111 unionfs_check_vnode(struct vnode *vp, const char *file __unused,
112     int line __unused)
113 {
114 	/*
115 	 * unionfs_lock() needs the NULL check here, as it explicitly
116 	 * handles the case in which the vnode has been vgonel()'ed.
117 	 */
118 	KASSERT(vp->v_op == &unionfs_vnodeops || vp->v_data == NULL,
119 	    ("%s:%d: non-unionfs vnode %p", file, line, vp));
120 	return ((struct unionfs_node *)vp->v_data);
121 }
122 
123 #define	MOUNTTOUNIONFSMOUNT(mp) ((struct unionfs_mount *)((mp)->mnt_data))
124 #define	VTOUNIONFS(vp) unionfs_check_vnode(vp, __FILE__, __LINE__)
125 #define	UNIONFSTOV(xp) ((xp)->un_vnode)
126 
127 int	unionfs_init(struct vfsconf *);
128 int	unionfs_uninit(struct vfsconf *);
129 int	unionfs_nodeget(struct mount *, struct vnode *, struct vnode *,
130 	    struct vnode *, struct vnode **, struct componentname *);
131 void	unionfs_noderem(struct vnode *);
132 void	unionfs_get_node_status(struct unionfs_node *, struct thread *,
133 	    struct unionfs_node_status **);
134 void	unionfs_tryrem_node_status(struct unionfs_node *,
135 	    struct unionfs_node_status *);
136 int	unionfs_check_rmdir(struct vnode *, struct ucred *, struct thread *td);
137 int	unionfs_copyfile(struct unionfs_node *, int, struct ucred *,
138 	    struct thread *);
139 void	unionfs_create_uppervattr_core(struct unionfs_mount *, struct vattr *,
140 	    struct vattr *, struct thread *);
141 int	unionfs_create_uppervattr(struct unionfs_mount *, struct vnode *,
142 	    struct vattr *, struct ucred *, struct thread *);
143 int	unionfs_mkshadowdir(struct unionfs_mount *, struct vnode *,
144 	    struct unionfs_node *, struct componentname *, struct thread *);
145 int	unionfs_mkwhiteout(struct vnode *, struct componentname *,
146 	    struct thread *, char *, int);
147 int	unionfs_relookup(struct vnode *, struct vnode **,
148 	    struct componentname *, struct componentname *, struct thread *,
149 	    char *, int, u_long);
150 int	unionfs_relookup_for_create(struct vnode *, struct componentname *,
151 	    struct thread *);
152 int	unionfs_relookup_for_delete(struct vnode *, struct componentname *,
153 	    struct thread *);
154 int	unionfs_relookup_for_rename(struct vnode *, struct componentname *,
155 	    struct thread *);
156 
157 #define	UNIONFSVPTOLOWERVP(vp) (VTOUNIONFS(vp)->un_lowervp)
158 #define	UNIONFSVPTOUPPERVP(vp) (VTOUNIONFS(vp)->un_uppervp)
159 
160 #ifdef MALLOC_DECLARE
161 MALLOC_DECLARE(M_UNIONFSNODE);
162 MALLOC_DECLARE(M_UNIONFSPATH);
163 #endif
164 
165 #ifdef UNIONFS_DEBUG
166 #define UNIONFSDEBUG(format, args...) printf(format ,## args)
167 #else
168 #define UNIONFSDEBUG(format, args...)
169 #endif				/* UNIONFS_DEBUG */
170 
171 #endif				/* _KERNEL */
172