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 mount *um_lowermp; /* MNT_REFed lower mount object */
55 struct mount *um_uppermp; /* MNT_REFed upper mount object */
56 struct vnode *um_lowervp; /* VREFed once */
57 struct vnode *um_uppervp; /* VREFed once */
58 struct vnode *um_rootvp; /* ROOT vnode */
59 struct mount_upper_node um_lower_link; /* node in lower FS list of uppers */
60 struct mount_upper_node um_upper_link; /* node in upper FS list of uppers */
61 unionfs_copymode um_copymode;
62 unionfs_whitemode um_whitemode;
63 uid_t um_uid;
64 gid_t um_gid;
65 u_short um_udir;
66 u_short um_ufile;
67 };
68
69 /* unionfs status list */
70 struct unionfs_node_status {
71 LIST_ENTRY(unionfs_node_status) uns_list; /* Status list */
72 pid_t uns_pid; /* current process id */
73 int uns_node_flag; /* uns flag */
74 int uns_lower_opencnt; /* open count of lower */
75 int uns_upper_opencnt; /* open count of upper */
76 int uns_lower_openmode; /* open mode of lower */
77 int uns_readdir_status; /* read status of readdir */
78 };
79
80 /* union node status flags */
81 #define UNS_OPENL_4_READDIR 0x01 /* open lower layer for readdir */
82
83 /* A cache of vnode references */
84 struct unionfs_node {
85 struct vnode *un_lowervp; /* lower side vnode */
86 struct vnode *un_uppervp; /* upper side vnode */
87 struct vnode *un_dvp; /* parent unionfs vnode */
88 struct vnode *un_vnode; /* Back pointer */
89 LIST_HEAD(, unionfs_node_status) un_unshead;
90 /* unionfs status head */
91 LIST_HEAD(unionfs_node_hashhead, unionfs_node) *un_hashtbl;
92 /* dir vnode hash table */
93 union {
94 LIST_ENTRY(unionfs_node) un_hash; /* hash list entry */
95 STAILQ_ENTRY(unionfs_node) un_rele; /* deferred release list */
96 };
97
98 char *un_path; /* path */
99 int un_pathlen; /* strlen of path */
100
101 /*
102 * unionfs node flags
103 * Changing these flags requires the vnode to be locked exclusive.
104 */
105 #define UNIONFS_OPENEXTL 0x01 /* openextattr (lower) */
106 #define UNIONFS_OPENEXTU 0x02 /* openextattr (upper) */
107 #define UNIONFS_COPY_IN_PROGRESS 0x04 /* copy/dir shadow in progres */
108 #define UNIONFS_LOOKUP_IN_PROGRESS 0x08
109 unsigned int un_flag; /* unionfs node flag */
110 };
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 struct unionfs_node_status * unionfs_find_node_status(struct unionfs_node *,
137 struct thread *td);
138 void unionfs_get_node_status(struct unionfs_node *, struct thread *,
139 struct unionfs_node_status **);
140 void unionfs_tryrem_node_status(struct unionfs_node *,
141 struct unionfs_node_status *);
142 int unionfs_check_rmdir(struct vnode *, struct ucred *, struct thread *td);
143 int unionfs_copyfile(struct vnode *, int, struct ucred *,
144 struct thread *);
145 void unionfs_create_uppervattr_core(struct unionfs_mount *, struct vattr *,
146 struct vattr *, struct thread *);
147 int unionfs_create_uppervattr(struct unionfs_mount *, struct vnode *,
148 struct vattr *, struct ucred *, struct thread *);
149 int unionfs_mkshadowdir(struct vnode *, struct vnode *,
150 struct componentname *, struct thread *);
151 int unionfs_mkwhiteout(struct vnode *, struct vnode *,
152 struct componentname *, struct thread *, char *, int);
153 int unionfs_relookup(struct vnode *, struct vnode **,
154 struct componentname *, struct componentname *, struct thread *,
155 char *, int, u_long);
156 void unionfs_forward_vop_start_pair(struct vnode *, int *,
157 struct vnode *, int *);
158 bool unionfs_forward_vop_finish_pair(struct vnode *, struct vnode *, int,
159 struct vnode *, struct vnode *, int);
160 int unionfs_set_in_progress_flag(struct vnode *, unsigned int);
161 void unionfs_clear_in_progress_flag(struct vnode *, unsigned int);
162
163 static inline void
unionfs_forward_vop_start(struct vnode * basevp,int * lkflags)164 unionfs_forward_vop_start(struct vnode *basevp, int *lkflags)
165 {
166 unionfs_forward_vop_start_pair(basevp, lkflags, NULL, NULL);
167 }
168
169 static inline bool
unionfs_forward_vop_finish(struct vnode * unionvp,struct vnode * basevp,int lkflags)170 unionfs_forward_vop_finish(struct vnode *unionvp, struct vnode *basevp,
171 int lkflags)
172 {
173 return (unionfs_forward_vop_finish_pair(unionvp, basevp, lkflags,
174 NULL, NULL, 0));
175 }
176
177 #define UNIONFSVPTOLOWERVP(vp) (VTOUNIONFS(vp)->un_lowervp)
178 #define UNIONFSVPTOUPPERVP(vp) (VTOUNIONFS(vp)->un_uppervp)
179
180 #ifdef MALLOC_DECLARE
181 MALLOC_DECLARE(M_UNIONFSNODE);
182 MALLOC_DECLARE(M_UNIONFSPATH);
183 #endif
184
185 #ifdef UNIONFS_DEBUG
186 #define UNIONFSDEBUG(format, args...) printf(format ,## args)
187 #else
188 #define UNIONFSDEBUG(format, args...)
189 #endif /* UNIONFS_DEBUG */
190
191 #endif /* _KERNEL */
192