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