xref: /freebsd/sys/fs/nullfs/null_vfsops.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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 int	nullfs_fhtovp(struct mount *mp, struct fid *fidp,
62 				   struct vnode **vpp);
63 static int	nullfs_checkexp(struct mount *mp, struct sockaddr *nam,
64 				    int *extflagsp, struct ucred **credanonp);
65 static int	nullfs_mount(struct mount *mp, char *path, caddr_t data,
66 				  struct nameidata *ndp, struct thread *td);
67 static int	nullfs_quotactl(struct mount *mp, int cmd, uid_t uid,
68 				     caddr_t arg, struct thread *td);
69 static int	nullfs_root(struct mount *mp, struct vnode **vpp);
70 static int	nullfs_start(struct mount *mp, int flags, struct thread *td);
71 static int	nullfs_statfs(struct mount *mp, struct statfs *sbp,
72 				   struct thread *td);
73 static int	nullfs_sync(struct mount *mp, int waitfor,
74 				 struct ucred *cred, struct thread *td);
75 static int	nullfs_unmount(struct mount *mp, int mntflags, struct thread *td);
76 static int	nullfs_vget(struct mount *mp, ino_t ino, int flags,
77 				 struct vnode **vpp);
78 static int	nullfs_vptofh(struct vnode *vp, struct fid *fhp);
79 static int	nullfs_extattrctl(struct mount *mp, int cmd,
80 				       struct vnode *filename_vp,
81 				       int namespace, const char *attrname,
82 				       struct thread *td);
83 
84 /*
85  * Mount null layer
86  */
87 static int
88 nullfs_mount(mp, path, data, ndp, td)
89 	struct mount *mp;
90 	char *path;
91 	caddr_t data;
92 	struct nameidata *ndp;
93 	struct thread *td;
94 {
95 	int error = 0;
96 	struct null_args args;
97 	struct vnode *lowerrootvp, *vp;
98 	struct vnode *nullm_rootvp;
99 	struct null_mount *xmp;
100 	size_t size;
101 	int isvnunlocked = 0;
102 
103 	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
104 
105 	/*
106 	 * Update is a no-op
107 	 */
108 	if (mp->mnt_flag & MNT_UPDATE) {
109 		return (EOPNOTSUPP);
110 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, td);*/
111 	}
112 
113 	/*
114 	 * Get argument
115 	 */
116 	error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
117 	if (error)
118 		return (error);
119 
120 	/*
121 	 * Unlock lower node to avoid deadlock.
122 	 * (XXX) VOP_ISLOCKED is needed?
123 	 */
124 	if ((mp->mnt_vnodecovered->v_op == null_vnodeop_p) &&
125 		VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) {
126 		VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
127 		isvnunlocked = 1;
128 	}
129 	/*
130 	 * Find lower node
131 	 */
132 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
133 		UIO_USERSPACE, args.target, td);
134 	error = namei(ndp);
135 	/*
136 	 * Re-lock vnode.
137 	 */
138 	if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered, NULL))
139 		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY, td);
140 
141 	if (error)
142 		return (error);
143 	NDFREE(ndp, NDF_ONLY_PNBUF);
144 
145 	/*
146 	 * Sanity check on lower vnode
147 	 */
148 	lowerrootvp = ndp->ni_vp;
149 
150 	vrele(ndp->ni_dvp);
151 	ndp->ni_dvp = NULLVP;
152 
153 	/*
154 	 * Check multi null mount to avoid `lock against myself' panic.
155 	 */
156 	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
157 		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
158 		vput(lowerrootvp);
159 		return (EDEADLK);
160 	}
161 
162 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
163 				M_NULLFSMNT, M_WAITOK);	/* XXX */
164 
165 	/*
166 	 * Save reference to underlying FS
167 	 */
168 	xmp->nullm_vfs = lowerrootvp->v_mount;
169 
170 	/*
171 	 * Save reference.  Each mount also holds
172 	 * a reference on the root vnode.
173 	 */
174 	error = null_node_create(mp, lowerrootvp, &vp);
175 	/*
176 	 * Unlock the node (either the lower or the alias)
177 	 */
178 	VOP_UNLOCK(vp, 0, td);
179 	/*
180 	 * Make sure the node alias worked
181 	 */
182 	if (error) {
183 		vrele(lowerrootvp);
184 		free(xmp, M_NULLFSMNT);	/* XXX */
185 		return (error);
186 	}
187 
188 	/*
189 	 * Keep a held reference to the root vnode.
190 	 * It is vrele'd in nullfs_unmount.
191 	 */
192 	nullm_rootvp = vp;
193 	nullm_rootvp->v_flag |= VROOT;
194 	xmp->nullm_rootvp = nullm_rootvp;
195 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
196 		mp->mnt_flag |= MNT_LOCAL;
197 	mp->mnt_data = (qaddr_t) xmp;
198 	vfs_getnewfsid(mp);
199 
200 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname,
201 	    MNAMELEN - 1, &size);
202 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
203 	(void)nullfs_statfs(mp, &mp->mnt_stat, td);
204 	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
205 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
206 	return (0);
207 }
208 
209 /*
210  * VFS start.  Nothing needed here - the start routine
211  * on the underlying filesystem will have been called
212  * when that filesystem was mounted.
213  */
214 static int
215 nullfs_start(mp, flags, td)
216 	struct mount *mp;
217 	int flags;
218 	struct thread *td;
219 {
220 	return (0);
221 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, td); */
222 }
223 
224 /*
225  * Free reference to null layer
226  */
227 static int
228 nullfs_unmount(mp, mntflags, td)
229 	struct mount *mp;
230 	int mntflags;
231 	struct thread *td;
232 {
233 	void *mntdata;
234 	int error;
235 	int flags = 0;
236 
237 	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
238 
239 	if (mntflags & MNT_FORCE)
240 		flags |= FORCECLOSE;
241 
242 	/* There is 1 extra root vnode reference (nullm_rootvp). */
243 	error = vflush(mp, 1, flags);
244 	if (error)
245 		return (error);
246 
247 	/*
248 	 * Finally, throw away the null_mount structure
249 	 */
250 	mntdata = mp->mnt_data;
251 	mp->mnt_data = 0;
252 	free(mntdata, M_NULLFSMNT);
253 	return 0;
254 }
255 
256 static int
257 nullfs_root(mp, vpp)
258 	struct mount *mp;
259 	struct vnode **vpp;
260 {
261 	struct thread *td = curthread;	/* XXX */
262 	struct vnode *vp;
263 
264 	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
265 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
266 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
267 
268 	/*
269 	 * Return locked reference to root.
270 	 */
271 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
272 	VREF(vp);
273 
274 #ifdef NULLFS_DEBUG
275 	if (VOP_ISLOCKED(vp, NULL)) {
276 		Debugger("root vnode is locked.\n");
277 		vrele(vp);
278 		return (EDEADLK);
279 	}
280 #endif
281 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
282 	*vpp = vp;
283 	return 0;
284 }
285 
286 static int
287 nullfs_quotactl(mp, cmd, uid, arg, td)
288 	struct mount *mp;
289 	int cmd;
290 	uid_t uid;
291 	caddr_t arg;
292 	struct thread *td;
293 {
294 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td);
295 }
296 
297 static int
298 nullfs_statfs(mp, sbp, td)
299 	struct mount *mp;
300 	struct statfs *sbp;
301 	struct thread *td;
302 {
303 	int error;
304 	struct statfs mstat;
305 
306 	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
307 	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
308 	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
309 
310 	bzero(&mstat, sizeof(mstat));
311 
312 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td);
313 	if (error)
314 		return (error);
315 
316 	/* now copy across the "interesting" information and fake the rest */
317 	sbp->f_type = mstat.f_type;
318 	sbp->f_flags = mstat.f_flags;
319 	sbp->f_bsize = mstat.f_bsize;
320 	sbp->f_iosize = mstat.f_iosize;
321 	sbp->f_blocks = mstat.f_blocks;
322 	sbp->f_bfree = mstat.f_bfree;
323 	sbp->f_bavail = mstat.f_bavail;
324 	sbp->f_files = mstat.f_files;
325 	sbp->f_ffree = mstat.f_ffree;
326 	if (sbp != &mp->mnt_stat) {
327 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
328 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
329 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
330 	}
331 	return (0);
332 }
333 
334 static int
335 nullfs_sync(mp, waitfor, cred, td)
336 	struct mount *mp;
337 	int waitfor;
338 	struct ucred *cred;
339 	struct thread *td;
340 {
341 	/*
342 	 * XXX - Assumes no data cached at null layer.
343 	 */
344 	return (0);
345 }
346 
347 static int
348 nullfs_vget(mp, ino, flags, vpp)
349 	struct mount *mp;
350 	ino_t ino;
351 	int flags;
352 	struct vnode **vpp;
353 {
354 	int error;
355 	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
356 	if (error)
357 		return (error);
358 
359 	return (null_node_create(mp, *vpp, vpp));
360 }
361 
362 static int
363 nullfs_fhtovp(mp, fidp, vpp)
364 	struct mount *mp;
365 	struct fid *fidp;
366 	struct vnode **vpp;
367 {
368 	int error;
369 	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp);
370 	if (error)
371 		return (error);
372 
373 	return (null_node_create(mp, *vpp, vpp));
374 }
375 
376 static int
377 nullfs_checkexp(mp, nam, extflagsp, credanonp)
378 	struct mount *mp;
379 	struct sockaddr *nam;
380 	int *extflagsp;
381 	struct ucred **credanonp;
382 {
383 
384 	return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam,
385 		extflagsp, credanonp);
386 }
387 
388 static int
389 nullfs_vptofh(vp, fhp)
390 	struct vnode *vp;
391 	struct fid *fhp;
392 {
393 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
394 }
395 
396 static int
397 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td)
398 	struct mount *mp;
399 	int cmd;
400 	struct vnode *filename_vp;
401 	int namespace;
402 	const char *attrname;
403 	struct thread *td;
404 {
405 	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
406 	    namespace, attrname, td);
407 }
408 
409 
410 static struct vfsops null_vfsops = {
411 	nullfs_mount,
412 	nullfs_start,
413 	nullfs_unmount,
414 	nullfs_root,
415 	nullfs_quotactl,
416 	nullfs_statfs,
417 	nullfs_sync,
418 	nullfs_vget,
419 	nullfs_fhtovp,
420 	nullfs_checkexp,
421 	nullfs_vptofh,
422 	nullfs_init,
423 	nullfs_uninit,
424 	nullfs_extattrctl,
425 };
426 
427 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
428