xref: /netbsd-src/sys/miscfs/genfs/layer_vfsops.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: layer_vfsops.c,v 1.33 2010/07/02 03:16:01 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 National Aeronautics & Space Administration
5  * All rights reserved.
6  *
7  * This software was written by William Studenmund of the
8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the National Aeronautics & Space Administration
19  *    nor the names of its contributors may be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1992, 1993, 1995
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software donated to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
68  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
69  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
70  */
71 
72 /*
73  * Generic layer VFS operations.
74  */
75 
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: layer_vfsops.c,v 1.33 2010/07/02 03:16:01 rmind Exp $");
78 
79 #include <sys/param.h>
80 #include <sys/sysctl.h>
81 #include <sys/systm.h>
82 #include <sys/vnode.h>
83 #include <sys/mount.h>
84 #include <sys/namei.h>
85 #include <sys/malloc.h>
86 #include <sys/kauth.h>
87 #include <sys/module.h>
88 
89 #include <miscfs/genfs/layer.h>
90 #include <miscfs/genfs/layer_extern.h>
91 
92 MODULE(MODULE_CLASS_MISC, layerfs, NULL);
93 
94 static int
95 layerfs_modcmd(modcmd_t cmd, void *arg)
96 {
97 
98 	switch (cmd) {
99 	case MODULE_CMD_INIT:
100 		return 0;
101 	case MODULE_CMD_FINI:
102 		return 0;
103 	default:
104 		return ENOTTY;
105 	}
106 	return 0;
107 }
108 
109 /*
110  * VFS start.  Nothing needed here - the start routine on the underlying
111  * filesystem will have been called when that filesystem was mounted.
112  */
113 int
114 layerfs_start(struct mount *mp, int flags)
115 {
116 
117 #ifdef notyet
118 	return VFS_START(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, flags);
119 #else
120 	return 0;
121 #endif
122 }
123 
124 int
125 layerfs_root(struct mount *mp, struct vnode **vpp)
126 {
127 	struct vnode *vp;
128 
129 	vp = MOUNTTOLAYERMOUNT(mp)->layerm_rootvp;
130 	if (vp == NULL) {
131 		*vpp = NULL;
132 		return EINVAL;
133 	}
134 	/*
135 	 * Return root vnode with locked and with a reference held.
136 	 */
137 	vref(vp);
138 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
139 	*vpp = vp;
140 	return 0;
141 }
142 
143 int
144 layerfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
145 {
146 
147 	return VFS_QUOTACTL(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, cmd, uid, arg);
148 }
149 
150 int
151 layerfs_statvfs(struct mount *mp, struct statvfs *sbp)
152 {
153 	struct statvfs *sbuf;
154 	int error;
155 
156 	sbuf = kmem_zalloc(sizeof(*sbuf), KM_SLEEP);
157 	if (sbuf == NULL) {
158 		return ENOMEM;
159 	}
160 	error = VFS_STATVFS(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, sbuf);
161 	if (error) {
162 		goto done;
163 	}
164 	/* Copy across the relevant data and fake the rest. */
165 	sbp->f_flag = sbuf->f_flag;
166 	sbp->f_bsize = sbuf->f_bsize;
167 	sbp->f_frsize = sbuf->f_frsize;
168 	sbp->f_iosize = sbuf->f_iosize;
169 	sbp->f_blocks = sbuf->f_blocks;
170 	sbp->f_bfree = sbuf->f_bfree;
171 	sbp->f_bavail = sbuf->f_bavail;
172 	sbp->f_bresvd = sbuf->f_bresvd;
173 	sbp->f_files = sbuf->f_files;
174 	sbp->f_ffree = sbuf->f_ffree;
175 	sbp->f_favail = sbuf->f_favail;
176 	sbp->f_fresvd = sbuf->f_fresvd;
177 	sbp->f_namemax = sbuf->f_namemax;
178 	copy_statvfs_info(sbp, mp);
179 done:
180 	kmem_free(sbuf, sizeof(*sbuf));
181 	return error;
182 }
183 
184 int
185 layerfs_sync(struct mount *mp, int waitfor,
186     kauth_cred_t cred)
187 {
188 
189 	/*
190 	 * XXX - Assumes no data cached at layer.
191 	 */
192 	return 0;
193 }
194 
195 int
196 layerfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
197 {
198 	struct vnode *vp;
199 	int error;
200 
201 	error = VFS_VGET(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, ino, &vp);
202 	if (error) {
203 		*vpp = NULL;
204 		return error;
205 	}
206 	error = layer_node_create(mp, vp, vpp);
207 	if (error) {
208 		vput(vp);
209 		*vpp = NULL;
210 		return error;
211 	}
212 	return 0;
213 }
214 
215 int
216 layerfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
217 {
218 	struct vnode *vp;
219 	int error;
220 
221 	error = VFS_FHTOVP(MOUNTTOLAYERMOUNT(mp)->layerm_vfs, fidp, &vp);
222 	if (error) {
223 		return error;
224 	}
225 	error = layer_node_create(mp, vp, vpp);
226 	if (error) {
227 		vput(vp);
228 		*vpp = NULL;
229 		return (error);
230 	}
231 	return 0;
232 }
233 
234 int
235 layerfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
236 {
237 
238 	return VFS_VPTOFH(LAYERVPTOLOWERVP(vp), fhp, fh_size);
239 }
240 
241 /*
242  * layerfs_snapshot - handle a snapshot through a layered file system
243  *
244  * At present, we do NOT support snapshotting through a layered file
245  * system as the ffs implementation changes v_vnlock of the snapshot
246  * vnodes to point to one common lock. As there is no way for us to
247  * absolutely pass this change up the stack, a layered file system
248  * would end up referencing the wrong lock.
249  *
250  * This routine serves as a central resource for this behavior; all
251  * layered file systems don't need to worry about the above. Also, if
252  * things get fixed, all layers get the benefit.
253  */
254 int
255 layerfs_snapshot(struct mount *mp, struct vnode *vp,
256     struct timespec *ts)
257 {
258 
259 	return EOPNOTSUPP;
260 }
261 
262 SYSCTL_SETUP(sysctl_vfs_layerfs_setup, "sysctl vfs.layerfs subtree setup")
263 {
264 	const struct sysctlnode *layerfs_node = NULL;
265 
266 	sysctl_createv(clog, 0, NULL, NULL,
267 		       CTLFLAG_PERMANENT,
268 		       CTLTYPE_NODE, "vfs", NULL,
269 		       NULL, 0, NULL, 0,
270 		       CTL_VFS, CTL_EOL);
271 	sysctl_createv(clog, 0, NULL, &layerfs_node,
272 		       CTLFLAG_PERMANENT,
273 		       CTLTYPE_NODE, "layerfs",
274 		       SYSCTL_DESCR("Generic layered file system"),
275 		       NULL, 0, NULL, 0,
276 		       CTL_VFS, CTL_CREATE);
277 
278 #ifdef LAYERFS_DIAGNOSTIC
279 	sysctl_createv(clog, 0, &layerfs_node, NULL,
280 	               CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
281 	               CTLTYPE_INT,
282 	               "debug",
283 	               SYSCTL_DESCR("Verbose debugging messages"),
284 	               NULL, 0, &layerfs_debug, 0,
285 	               CTL_CREATE, CTL_EOL);
286 #endif
287 
288 	/*
289 	 * other subtrees should really be aliases to this, but since
290 	 * they can't tell if layerfs has been instantiated yet, they
291 	 * can't do that...not easily.  not yet.  :-)
292 	 */
293 }
294 
295 int
296 layerfs_renamelock_enter(struct mount *mp)
297 {
298 
299 	return VFS_RENAMELOCK_ENTER(MOUNTTOLAYERMOUNT(mp)->layerm_vfs);
300 }
301 
302 void
303 layerfs_renamelock_exit(struct mount *mp)
304 {
305 
306 	VFS_RENAMELOCK_EXIT(MOUNTTOLAYERMOUNT(mp)->layerm_vfs);
307 }
308