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