xref: /netbsd-src/sys/miscfs/nullfs/null_vfsops.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: null_vfsops.c,v 1.21 1999/01/13 01:51:37 wrstuden Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
39  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
40  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
41  */
42 
43 /*
44  * Null Layer
45  * (See null_vnops.c for a description of what this does.)
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/time.h>
51 #include <sys/proc.h>
52 #include <sys/types.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/malloc.h>
57 #include <miscfs/nullfs/null.h>
58 
59 int	nullfs_mount __P((struct mount *, const char *, void *,
60 			  struct nameidata *, struct proc *));
61 int	nullfs_start __P((struct mount *, int, struct proc *));
62 int	nullfs_unmount __P((struct mount *, int, struct proc *));
63 int	nullfs_root __P((struct mount *, struct vnode **));
64 int	nullfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
65 			     struct proc *));
66 int	nullfs_statfs __P((struct mount *, struct statfs *, struct proc *));
67 int	nullfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
68 int	nullfs_vget __P((struct mount *, ino_t, struct vnode **));
69 int	nullfs_fhtovp __P((struct mount *, struct fid *, struct mbuf *,
70 			   struct vnode **, int *, struct ucred **));
71 int	nullfs_vptofh __P((struct vnode *, struct fid *));
72 int	nullfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
73 			   struct proc *));
74 /*
75  * Mount null layer
76  */
77 int
78 nullfs_mount(mp, path, data, ndp, p)
79 	struct mount *mp;
80 	const char *path;
81 	void *data;
82 	struct nameidata *ndp;
83 	struct proc *p;
84 {
85 	int error = 0;
86 	struct null_args args;
87 	struct vnode *lowerrootvp, *vp;
88 	struct vnode *nullm_rootvp;
89 	struct null_mount *xmp;
90 	size_t size;
91 
92 #ifdef NULLFS_DIAGNOSTIC
93 	printf("nullfs_mount(mp = %p)\n", mp);
94 #endif
95 
96 	/*
97 	 * Update is a no-op
98 	 */
99 	if (mp->mnt_flag & MNT_UPDATE) {
100 		return (EOPNOTSUPP);
101 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
102 	}
103 
104 	/*
105 	 * Get argument
106 	 */
107 	error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
108 	if (error)
109 		return (error);
110 
111 	/*
112 	 * Find lower node
113 	 */
114 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
115 		UIO_USERSPACE, args.target, p);
116 	if ((error = namei(ndp)) != 0)
117 		return (error);
118 
119 	/*
120 	 * Sanity check on lower vnode
121 	 */
122 	lowerrootvp = ndp->ni_vp;
123 
124 	vrele(ndp->ni_dvp);
125 	ndp->ni_dvp = NULL;
126 
127 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
128 				M_UFSMNT, M_WAITOK);	/* XXX */
129 
130 	/*
131 	 * Save reference to underlying FS
132 	 */
133 	xmp->nullm_vfs = lowerrootvp->v_mount;
134 
135 	/*
136 	 * Save reference.  Each mount also holds
137 	 * a reference on the root vnode.
138 	 */
139 	error = null_node_create(mp, lowerrootvp, &vp, 1);
140 	/*
141 	 * Make sure the node alias worked
142 	 *
143 	 */
144 	if (error) {
145 		vrele(lowerrootvp);
146 		free(xmp, M_UFSMNT);	/* XXX */
147 		return (error);
148 	}
149 	/*
150 	 * Unlock the node (either the lower or the alias)
151 	 */
152 	VOP_UNLOCK(vp, 0);
153 
154 	/*
155 	 * Keep a held reference to the root vnode.
156 	 * It is vrele'd in nullfs_unmount.
157 	 */
158 	nullm_rootvp = vp;
159 	nullm_rootvp->v_flag |= VROOT;
160 	xmp->nullm_rootvp = nullm_rootvp;
161 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
162 		mp->mnt_flag |= MNT_LOCAL;
163 	mp->mnt_data = (qaddr_t) xmp;
164 	vfs_getnewfsid(mp, MOUNT_NULL);
165 
166 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
167 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
168 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
169 	    &size);
170 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
171 #ifdef NULLFS_DIAGNOSTIC
172 	printf("nullfs_mount: lower %s, alias at %s\n",
173 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
174 #endif
175 	return (0);
176 }
177 
178 /*
179  * VFS start.  Nothing needed here - the start routine
180  * on the underlying filesystem will have been called
181  * when that filesystem was mounted.
182  */
183 int
184 nullfs_start(mp, flags, p)
185 	struct mount *mp;
186 	int flags;
187 	struct proc *p;
188 {
189 
190 	return (0);
191 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
192 }
193 
194 /*
195  * Free reference to null layer
196  */
197 int
198 nullfs_unmount(mp, mntflags, p)
199 	struct mount *mp;
200 	int mntflags;
201 	struct proc *p;
202 {
203 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
204 	int error;
205 	int flags = 0;
206 
207 #ifdef NULLFS_DIAGNOSTIC
208 	printf("nullfs_unmount(mp = %p)\n", mp);
209 #endif
210 
211 	if (mntflags & MNT_FORCE)
212 		flags |= FORCECLOSE;
213 
214 	/*
215 	 * Clear out buffer cache.  I don't think we
216 	 * ever get anything cached at this level at the
217 	 * moment, but who knows...
218 	 */
219 #if 0
220 	mntflushbuf(mp, 0);
221 	if (mntinvalbuf(mp, 1))
222 		return (EBUSY);
223 #endif
224 	if (nullm_rootvp->v_usecount > 1)
225 		return (EBUSY);
226 	if ((error = vflush(mp, nullm_rootvp, flags)) != 0)
227 		return (error);
228 
229 #ifdef NULLFS_DIAGNOSTIC
230 	vprint("alias root of lower", nullm_rootvp);
231 #endif
232 	/*
233 	 * Release reference on underlying root vnode
234 	 */
235 	vrele(nullm_rootvp);
236 	/*
237 	 * And blow it away for future re-use
238 	 */
239 	vgone(nullm_rootvp);
240 	/*
241 	 * Finally, throw away the null_mount structure
242 	 */
243 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
244 	mp->mnt_data = 0;
245 	return 0;
246 }
247 
248 int
249 nullfs_root(mp, vpp)
250 	struct mount *mp;
251 	struct vnode **vpp;
252 {
253 	struct vnode *vp;
254 
255 #ifdef NULLFS_DIAGNOSTIC
256 	printf("nullfs_root(mp = %p, vp = %p->%p)\n", mp,
257 	    MOUNTTONULLMOUNT(mp)->nullm_rootvp,
258 	    NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
259 #endif
260 
261 	/*
262 	 * Return locked reference to root.
263 	 */
264 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
265 	VREF(vp);
266 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
267 	*vpp = vp;
268 	return 0;
269 }
270 
271 int
272 nullfs_quotactl(mp, cmd, uid, arg, p)
273 	struct mount *mp;
274 	int cmd;
275 	uid_t uid;
276 	caddr_t arg;
277 	struct proc *p;
278 {
279 
280 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
281 }
282 
283 int
284 nullfs_statfs(mp, sbp, p)
285 	struct mount *mp;
286 	struct statfs *sbp;
287 	struct proc *p;
288 {
289 	int error;
290 	struct statfs mstat;
291 
292 #ifdef NULLFS_DIAGNOSTIC
293 	printf("nullfs_statfs(mp = %p, vp = %p->%p)\n", mp,
294 	    MOUNTTONULLMOUNT(mp)->nullm_rootvp,
295 	    NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
296 #endif
297 
298 	memset(&mstat, 0, sizeof(mstat));
299 
300 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
301 	if (error)
302 		return (error);
303 
304 	/* now copy across the "interesting" information and fake the rest */
305 	sbp->f_type = mstat.f_type;
306 	sbp->f_flags = mstat.f_flags;
307 	sbp->f_bsize = mstat.f_bsize;
308 	sbp->f_iosize = mstat.f_iosize;
309 	sbp->f_blocks = mstat.f_blocks;
310 	sbp->f_bfree = mstat.f_bfree;
311 	sbp->f_bavail = mstat.f_bavail;
312 	sbp->f_files = mstat.f_files;
313 	sbp->f_ffree = mstat.f_ffree;
314 	if (sbp != &mp->mnt_stat) {
315 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
316 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
317 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
318 	}
319 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
320 	return (0);
321 }
322 
323 int
324 nullfs_sync(mp, waitfor, cred, p)
325 	struct mount *mp;
326 	int waitfor;
327 	struct ucred *cred;
328 	struct proc *p;
329 {
330 
331 	/*
332 	 * XXX - Assumes no data cached at null layer.
333 	 */
334 	return (0);
335 }
336 
337 int
338 nullfs_vget(mp, ino, vpp)
339 	struct mount *mp;
340 	ino_t ino;
341 	struct vnode **vpp;
342 {
343 
344 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
345 }
346 
347 int
348 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
349 	struct mount *mp;
350 	struct fid *fidp;
351 	struct mbuf *nam;
352 	struct vnode **vpp;
353 	int *exflagsp;
354 	struct ucred**credanonp;
355 {
356 
357 	return (EOPNOTSUPP);
358 }
359 
360 int
361 nullfs_vptofh(vp, fhp)
362 	struct vnode *vp;
363 	struct fid *fhp;
364 {
365 
366 	return (EOPNOTSUPP);
367 }
368 
369 int
370 nullfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
371 	int *name;
372 	u_int namelen;
373 	void *oldp;
374 	size_t *oldlenp;
375 	void *newp;
376 	size_t newlen;
377 	struct proc *p;
378 {
379 	return (EOPNOTSUPP);
380 }
381 
382 extern struct vnodeopv_desc null_vnodeop_opv_desc;
383 
384 struct vnodeopv_desc *nullfs_vnodeopv_descs[] = {
385 	&null_vnodeop_opv_desc,
386 	NULL,
387 };
388 
389 struct vfsops nullfs_vfsops = {
390 	MOUNT_NULL,
391 	nullfs_mount,
392 	nullfs_start,
393 	nullfs_unmount,
394 	nullfs_root,
395 	nullfs_quotactl,
396 	nullfs_statfs,
397 	nullfs_sync,
398 	nullfs_vget,
399 	nullfs_fhtovp,
400 	nullfs_vptofh,
401 	nullfs_init,
402 	nullfs_sysctl,
403 	NULL,				/* vfs_mountroot */
404 	nullfs_vnodeopv_descs,
405 };
406