xref: /freebsd/sys/fs/nullfs/null_vfsops.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
37  *
38  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
39  * $FreeBSD$
40  */
41 
42 /*
43  * Null Layer
44  * (See null_vnops.c for a description of what this does.)
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/proc.h>
55 #include <sys/vnode.h>
56 
57 #include <fs/nullfs/null.h>
58 
59 static MALLOC_DEFINE(M_NULLFSMNT, "NULLFS mount", "NULLFS mount structure");
60 
61 static vfs_fhtovp_t	nullfs_fhtovp;
62 static vfs_checkexp_t	nullfs_checkexp;
63 static vfs_nmount_t	nullfs_mount;
64 static vfs_quotactl_t	nullfs_quotactl;
65 static vfs_root_t	nullfs_root;
66 static vfs_start_t	nullfs_start;
67 static vfs_sync_t	nullfs_sync;
68 static vfs_statfs_t	nullfs_statfs;
69 static vfs_unmount_t	nullfs_unmount;
70 static vfs_vget_t	nullfs_vget;
71 static vfs_vptofh_t	nullfs_vptofh;
72 static vfs_extattrctl_t	nullfs_extattrctl;
73 
74 /*
75  * Mount null layer
76  */
77 static int
78 nullfs_mount(mp, ndp, td)
79 	struct mount *mp;
80 	struct nameidata *ndp;
81 	struct thread *td;
82 {
83 	int error = 0;
84 	struct vnode *lowerrootvp, *vp;
85 	struct vnode *nullm_rootvp;
86 	struct null_mount *xmp;
87 	char *target;
88 	size_t size;
89 	int isvnunlocked = 0, len;
90 
91 	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
92 
93 	/*
94 	 * Update is a no-op
95 	 */
96 	if (mp->mnt_flag & MNT_UPDATE) {
97 		return (EOPNOTSUPP);
98 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, td);*/
99 	}
100 
101 	/*
102 	 * Get argument
103 	 */
104 	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
105 	if (error || target[len - 1] != '\0')
106 		return (EINVAL);
107 
108 	/*
109 	 * Unlock lower node to avoid deadlock.
110 	 * (XXX) VOP_ISLOCKED is needed?
111 	 */
112 	if ((mp->mnt_vnodecovered->v_op == null_vnodeop_p) &&
113 		VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) {
114 		VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
115 		isvnunlocked = 1;
116 	}
117 	/*
118 	 * Find lower node
119 	 */
120 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
121 		UIO_SYSSPACE, target, td);
122 	error = namei(ndp);
123 	/*
124 	 * Re-lock vnode.
125 	 */
126 	if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered, NULL))
127 		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY, td);
128 
129 	if (error)
130 		return (error);
131 	NDFREE(ndp, NDF_ONLY_PNBUF);
132 
133 	/*
134 	 * Sanity check on lower vnode
135 	 */
136 	lowerrootvp = ndp->ni_vp;
137 
138 	vrele(ndp->ni_dvp);
139 	ndp->ni_dvp = NULLVP;
140 
141 	/*
142 	 * Check multi null mount to avoid `lock against myself' panic.
143 	 */
144 	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
145 		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
146 		vput(lowerrootvp);
147 		return (EDEADLK);
148 	}
149 
150 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
151 				M_NULLFSMNT, M_WAITOK);	/* XXX */
152 
153 	/*
154 	 * Save reference to underlying FS
155 	 */
156 	xmp->nullm_vfs = lowerrootvp->v_mount;
157 
158 	/*
159 	 * Save reference.  Each mount also holds
160 	 * a reference on the root vnode.
161 	 */
162 	error = null_nodeget(mp, lowerrootvp, &vp);
163 	/*
164 	 * Make sure the node alias worked
165 	 */
166 	if (error) {
167 		VOP_UNLOCK(vp, 0, td);
168 		vrele(lowerrootvp);
169 		free(xmp, M_NULLFSMNT);	/* XXX */
170 		return (error);
171 	}
172 
173 	/*
174 	 * Keep a held reference to the root vnode.
175 	 * It is vrele'd in nullfs_unmount.
176 	 */
177 	nullm_rootvp = vp;
178 	nullm_rootvp->v_vflag |= VV_ROOT;
179 	xmp->nullm_rootvp = nullm_rootvp;
180 
181 	/*
182 	 * Unlock the node (either the lower or the alias)
183 	 */
184 	VOP_UNLOCK(vp, 0, td);
185 
186 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
187 		mp->mnt_flag |= MNT_LOCAL;
188 	mp->mnt_data = (qaddr_t) xmp;
189 	vfs_getnewfsid(mp);
190 
191 	(void) copystr(target, mp->mnt_stat.f_mntfromname,
192 	    MNAMELEN - 1, &size);
193 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
194 	(void)nullfs_statfs(mp, &mp->mnt_stat, td);
195 	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
196 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
197 	return (0);
198 }
199 
200 /*
201  * VFS start.  Nothing needed here - the start routine
202  * on the underlying filesystem will have been called
203  * when that filesystem was mounted.
204  */
205 static int
206 nullfs_start(mp, flags, td)
207 	struct mount *mp;
208 	int flags;
209 	struct thread *td;
210 {
211 	return (0);
212 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, td); */
213 }
214 
215 /*
216  * Free reference to null layer
217  */
218 static int
219 nullfs_unmount(mp, mntflags, td)
220 	struct mount *mp;
221 	int mntflags;
222 	struct thread *td;
223 {
224 	void *mntdata;
225 	int error;
226 	int flags = 0;
227 
228 	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
229 
230 	if (mntflags & MNT_FORCE)
231 		flags |= FORCECLOSE;
232 
233 	/* There is 1 extra root vnode reference (nullm_rootvp). */
234 	error = vflush(mp, 1, flags);
235 	if (error)
236 		return (error);
237 
238 	/*
239 	 * Finally, throw away the null_mount structure
240 	 */
241 	mntdata = mp->mnt_data;
242 	mp->mnt_data = 0;
243 	free(mntdata, M_NULLFSMNT);
244 	return 0;
245 }
246 
247 static int
248 nullfs_root(mp, vpp)
249 	struct mount *mp;
250 	struct vnode **vpp;
251 {
252 	struct thread *td = curthread;	/* XXX */
253 	struct vnode *vp;
254 
255 	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
256 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
257 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
258 
259 	/*
260 	 * Return locked reference to root.
261 	 */
262 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
263 	VREF(vp);
264 
265 #ifdef NULLFS_DEBUG
266 	if (VOP_ISLOCKED(vp, NULL)) {
267 		Debugger("root vnode is locked.\n");
268 		vrele(vp);
269 		return (EDEADLK);
270 	}
271 #endif
272 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
273 	*vpp = vp;
274 	return 0;
275 }
276 
277 static int
278 nullfs_quotactl(mp, cmd, uid, arg, td)
279 	struct mount *mp;
280 	int cmd;
281 	uid_t uid;
282 	caddr_t arg;
283 	struct thread *td;
284 {
285 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td);
286 }
287 
288 static int
289 nullfs_statfs(mp, sbp, td)
290 	struct mount *mp;
291 	struct statfs *sbp;
292 	struct thread *td;
293 {
294 	int error;
295 	struct statfs mstat;
296 
297 	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
298 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
299 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
300 
301 	bzero(&mstat, sizeof(mstat));
302 
303 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td);
304 	if (error)
305 		return (error);
306 
307 	/* now copy across the "interesting" information and fake the rest */
308 	sbp->f_type = mstat.f_type;
309 	sbp->f_flags = mstat.f_flags;
310 	sbp->f_bsize = mstat.f_bsize;
311 	sbp->f_iosize = mstat.f_iosize;
312 	sbp->f_blocks = mstat.f_blocks;
313 	sbp->f_bfree = mstat.f_bfree;
314 	sbp->f_bavail = mstat.f_bavail;
315 	sbp->f_files = mstat.f_files;
316 	sbp->f_ffree = mstat.f_ffree;
317 	if (sbp != &mp->mnt_stat) {
318 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
319 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
320 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
321 	}
322 	return (0);
323 }
324 
325 static int
326 nullfs_sync(mp, waitfor, cred, td)
327 	struct mount *mp;
328 	int waitfor;
329 	struct ucred *cred;
330 	struct thread *td;
331 {
332 	/*
333 	 * XXX - Assumes no data cached at null layer.
334 	 */
335 	return (0);
336 }
337 
338 static int
339 nullfs_vget(mp, ino, flags, vpp)
340 	struct mount *mp;
341 	ino_t ino;
342 	int flags;
343 	struct vnode **vpp;
344 {
345 	int error;
346 	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
347 	if (error)
348 		return (error);
349 
350 	return (null_nodeget(mp, *vpp, vpp));
351 }
352 
353 static int
354 nullfs_fhtovp(mp, fidp, vpp)
355 	struct mount *mp;
356 	struct fid *fidp;
357 	struct vnode **vpp;
358 {
359 	int error;
360 	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp);
361 	if (error)
362 		return (error);
363 
364 	return (null_nodeget(mp, *vpp, vpp));
365 }
366 
367 static int
368 nullfs_checkexp(mp, nam, extflagsp, credanonp)
369 	struct mount *mp;
370 	struct sockaddr *nam;
371 	int *extflagsp;
372 	struct ucred **credanonp;
373 {
374 
375 	return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam,
376 		extflagsp, credanonp);
377 }
378 
379 static int
380 nullfs_vptofh(vp, fhp)
381 	struct vnode *vp;
382 	struct fid *fhp;
383 {
384 	struct vnode *lvp;
385 
386 	lvp = NULLVPTOLOWERVP(vp);
387 	return VFS_VPTOFH(lvp, fhp);
388 }
389 
390 static int
391 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td)
392 	struct mount *mp;
393 	int cmd;
394 	struct vnode *filename_vp;
395 	int namespace;
396 	const char *attrname;
397 	struct thread *td;
398 {
399 	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
400 	    namespace, attrname, td);
401 }
402 
403 
404 static struct vfsops null_vfsops = {
405 	.vfs_checkexp =		nullfs_checkexp,
406 	.vfs_extattrctl =	nullfs_extattrctl,
407 	.vfs_fhtovp =		nullfs_fhtovp,
408 	.vfs_init =		nullfs_init,
409 	.vfs_nmount =		nullfs_mount,
410 	.vfs_quotactl =		nullfs_quotactl,
411 	.vfs_root =		nullfs_root,
412 	.vfs_start =		nullfs_start,
413 	.vfs_statfs =		nullfs_statfs,
414 	.vfs_sync =		nullfs_sync,
415 	.vfs_uninit =		nullfs_uninit,
416 	.vfs_unmount =		nullfs_unmount,
417 	.vfs_vget =		nullfs_vget,
418 	.vfs_vptofh =		nullfs_vptofh,
419 };
420 
421 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
422