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