xref: /netbsd/sys/fs/unionfs/unionfs.h (revision 6550d01e)
1 /*-
2  * Copyright (c) 1994 The Regents of the University of California.
3  * Copyright (c) 1994 Jan-Simon Pendry.
4  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
5  * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
6  * All rights reserved.
7  *
8  * This code is derived from software donated to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)union.h	8.9 (Berkeley) 12/10/94
36  * $FreeBSD: src/sys/fs/unionfs/union.h,v 1.36 2007/10/14 13:55:38 daichi Exp $
37  */
38 
39 #ifndef _MISCFS_UNION_H_
40 #define _MISCFS_UNION_H_
41 
42 #define	UNIONFS_DEBUG
43 
44 struct union_args {
45 	char		*target;	/* Target of loopback  */
46 	int		mntflags;	/* Options on the mount */
47 };
48 #define	unionfs_args	union_args
49 
50 #define UNMNT_ABOVE	0x0001		/* Target appears below mount point */
51 #define UNMNT_BELOW	0x0002		/* Target appears below mount point */
52 #define UNMNT_REPLACE	0x0003		/* Target replaces mount point */
53 #define UNMNT_OPMASK	0x0003
54 
55 #define UNMNT_BITS "\177\20" \
56     "b\00above\0b\01below\0b\02replace\0"
57 
58 #ifdef _KERNEL
59 
60 /* copy method of attr from lower to upper */
61 typedef enum _unionfs_copymode {
62 	UNIONFS_TRADITIONAL = 0,
63 	UNIONFS_TRANSPARENT,
64 	UNIONFS_MASQUERADE
65 } unionfs_copymode;
66 
67 /* whiteout policy of upper layer */
68 typedef enum _unionfs_whitemode {
69        UNIONFS_WHITE_ALWAYS = 0,
70        UNIONFS_WHITE_WHENNEEDED
71 } unionfs_whitemode;
72 
73 struct unionfs_mount {
74 	struct vnode   *um_lowervp;	/* VREFed once */
75 	struct vnode   *um_uppervp;	/* VREFed once */
76 	struct vnode   *um_rootvp;	/* ROOT vnode */
77 	kmutex_t	um_lock;
78 	unionfs_copymode um_copymode;
79 	unionfs_whitemode um_whitemode;
80 	uid_t		um_uid;
81 	gid_t		um_gid;
82 	int		um_op;		/* Operation mode */
83 	u_short		um_udir;
84 	u_short		um_ufile;
85 };
86 
87 /* unionfs status list */
88 struct unionfs_node_status {
89 	LIST_ENTRY(unionfs_node_status) uns_list;	/* Status list */
90 	pid_t		uns_pid;		/* current process id */
91 	lwpid_t		uns_lid;		/* current thread id */
92 	int		uns_node_flag;		/* uns flag */
93 	int		uns_lower_opencnt;	/* open count of lower */
94 	int		uns_upper_opencnt;	/* open count of upper */
95 	int		uns_lower_openmode;	/* open mode of lower */
96 	int		uns_readdir_status;	/* read status of readdir */
97 };
98 
99 /* union node status flags */
100 #define	UNS_OPENL_4_READDIR	0x01	/* open lower layer for readdir */
101 
102 /* A cache of vnode references */
103 struct unionfs_node {
104 	struct vnode   *un_lowervp;		/* lower side vnode */
105 	struct vnode   *un_uppervp;		/* upper side vnode */
106 	struct vnode   *un_dvp;			/* parent unionfs vnode */
107 	struct vnode   *un_vnode;		/* Back pointer */
108 	LIST_HEAD(, unionfs_node_status) un_unshead;  /* unionfs status head */
109 	char           *un_path;		/* path */
110 	int		un_flag;		/* unionfs node flag */
111 };
112 
113 /*
114  * unionfs node flags
115  * It needs the vnode with exclusive lock, when changing the un_flag variable.
116  */
117 #define UNIONFS_OPENEXTL	0x01	/* openextattr (lower) */
118 #define UNIONFS_OPENEXTU	0x02	/* openextattr (upper) */
119 
120 #define	MOUNTTOUNIONFSMOUNT(mp) ((struct unionfs_mount *)((mp)->mnt_data))
121 #define	VTOUNIONFS(vp) ((struct unionfs_node *)(vp)->v_data)
122 #define	UNIONFSTOV(xp) ((xp)->un_vnode)
123 
124 int unionfs_nodeget(struct mount *mp, struct vnode *uppervp, struct vnode *lowervp, struct vnode *dvp, struct vnode **vpp, struct componentname *cnp);
125 void unionfs_noderem(struct vnode *vp);
126 void unionfs_get_node_status(struct unionfs_node *unp, struct unionfs_node_status **unspp);
127 void unionfs_tryrem_node_status(struct unionfs_node *unp, struct unionfs_node_status *unsp);
128 
129 int unionfs_check_rmdir(struct vnode *vp, kauth_cred_t cred);
130 int unionfs_copyfile(struct unionfs_node *unp, int docopy, kauth_cred_t cred);
131 void unionfs_create_uppervattr_core(struct unionfs_mount *ump, struct vattr *lva, struct vattr *uva);
132 int unionfs_create_uppervattr(struct unionfs_mount *ump, struct vnode *lvp, struct vattr *uva, kauth_cred_t cred);
133 int unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *duvp, struct unionfs_node *unp, struct componentname *cnp);
134 int unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp, const char *path);
135 int unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp);
136 int unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp);
137 int unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp);
138 
139 #ifdef DIAGNOSTIC
140 struct vnode   *unionfs_checklowervp(struct vnode *vp, const char *fil, int lno);
141 struct vnode   *unionfs_checkuppervp(struct vnode *vp, const char *fil, int lno);
142 #define	UNIONFSVPTOLOWERVP(vp) unionfs_checklowervp((vp), __FILE__, __LINE__)
143 #define	UNIONFSVPTOUPPERVP(vp) unionfs_checkuppervp((vp), __FILE__, __LINE__)
144 #else
145 #define	UNIONFSVPTOLOWERVP(vp) (VTOUNIONFS(vp)->un_lowervp)
146 #define	UNIONFSVPTOUPPERVP(vp) (VTOUNIONFS(vp)->un_uppervp)
147 #endif
148 
149 extern int (**unionfs_vnodeop_p)(void *);
150 
151 #ifdef MALLOC_DECLARE
152 MALLOC_DECLARE(M_UNIONFSPATH);
153 #endif
154 
155 #ifdef UNIONFS_DEBUG
156 #define UNIONFSDEBUG(format, args...) printf(format ,## args)
157 #else
158 #define UNIONFSDEBUG(format, args...)
159 #endif				/* UNIONFS_DEBUG */
160 
161 #endif /* _KERNEL */
162 #endif /* _MISCFS_UNION_H_ */
163