null_vfsops.c revision 1.23 1 /* $NetBSD: null_vfsops.c,v 1.23 1999/02/26 23:44:45 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 vnode **));
70 int nullfs_checkexp __P((struct mount *, struct mbuf *, int *,
71 struct ucred **));
72 int nullfs_vptofh __P((struct vnode *, struct fid *));
73 int nullfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
74 struct proc *));
75 /*
76 * Mount null layer
77 */
78 int
79 nullfs_mount(mp, path, data, ndp, p)
80 struct mount *mp;
81 const char *path;
82 void *data;
83 struct nameidata *ndp;
84 struct proc *p;
85 {
86 int error = 0;
87 struct null_args args;
88 struct vnode *lowerrootvp, *vp;
89 struct vnode *nullm_rootvp;
90 struct null_mount *xmp;
91 size_t size;
92
93 #ifdef NULLFS_DIAGNOSTIC
94 printf("nullfs_mount(mp = %p)\n", mp);
95 #endif
96
97 /*
98 * Update is a no-op
99 */
100 if (mp->mnt_flag & MNT_UPDATE) {
101 return (EOPNOTSUPP);
102 /* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
103 }
104
105 /*
106 * Get argument
107 */
108 error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
109 if (error)
110 return (error);
111
112 /*
113 * Find lower node
114 */
115 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
116 UIO_USERSPACE, args.target, p);
117 if ((error = namei(ndp)) != 0)
118 return (error);
119
120 /*
121 * Sanity check on lower vnode
122 */
123 lowerrootvp = ndp->ni_vp;
124
125 vrele(ndp->ni_dvp);
126 ndp->ni_dvp = NULL;
127
128 xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
129 M_UFSMNT, M_WAITOK); /* XXX */
130
131 /*
132 * Save reference to underlying FS
133 */
134 xmp->nullm_vfs = lowerrootvp->v_mount;
135
136 /*
137 * Save reference. Each mount also holds
138 * a reference on the root vnode.
139 */
140 error = null_node_create(mp, lowerrootvp, &vp, 1);
141 /*
142 * Make sure the node alias worked
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, vpp)
349 struct mount *mp;
350 struct fid *fidp;
351 struct vnode **vpp;
352 {
353
354 return (EOPNOTSUPP);
355 }
356
357 int
358 nullfs_checkexp(mp, nam, exflagsp, credanonp)
359 struct mount *mp;
360 struct mbuf *nam;
361 int *exflagsp;
362 struct ucred**credanonp;
363 {
364
365 return (EOPNOTSUPP);
366 }
367
368 int
369 nullfs_vptofh(vp, fhp)
370 struct vnode *vp;
371 struct fid *fhp;
372 {
373
374 return (EOPNOTSUPP);
375 }
376
377 int
378 nullfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
379 int *name;
380 u_int namelen;
381 void *oldp;
382 size_t *oldlenp;
383 void *newp;
384 size_t newlen;
385 struct proc *p;
386 {
387 return (EOPNOTSUPP);
388 }
389
390 extern struct vnodeopv_desc null_vnodeop_opv_desc;
391
392 struct vnodeopv_desc *nullfs_vnodeopv_descs[] = {
393 &null_vnodeop_opv_desc,
394 NULL,
395 };
396
397 struct vfsops nullfs_vfsops = {
398 MOUNT_NULL,
399 nullfs_mount,
400 nullfs_start,
401 nullfs_unmount,
402 nullfs_root,
403 nullfs_quotactl,
404 nullfs_statfs,
405 nullfs_sync,
406 nullfs_vget,
407 nullfs_fhtovp,
408 nullfs_vptofh,
409 nullfs_init,
410 nullfs_sysctl,
411 NULL, /* vfs_mountroot */
412 nullfs_checkexp,
413 nullfs_vnodeopv_descs,
414 };
415