xref: /netbsd/sys/coda/coda_vfsops.c (revision bf9ec67e)
1 /*	$NetBSD: coda_vfsops.c,v 1.16 2002/03/27 05:10:41 phil Exp $	*/
2 
3 /*
4  *
5  *             Coda: an Experimental Distributed File System
6  *                              Release 3.1
7  *
8  *           Copyright (c) 1987-1998 Carnegie Mellon University
9  *                          All Rights Reserved
10  *
11  * Permission  to  use, copy, modify and distribute this software and its
12  * documentation is hereby granted,  provided  that  both  the  copyright
13  * notice  and  this  permission  notice  appear  in  all  copies  of the
14  * software, derivative works or  modified  versions,  and  any  portions
15  * thereof, and that both notices appear in supporting documentation, and
16  * that credit is given to Carnegie Mellon University  in  all  documents
17  * and publicity pertaining to direct or indirect use of this code or its
18  * derivatives.
19  *
20  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
21  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
22  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
23  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
24  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
25  * ANY DERIVATIVE WORK.
26  *
27  * Carnegie  Mellon  encourages  users  of  this  software  to return any
28  * improvements or extensions that  they  make,  and  to  grant  Carnegie
29  * Mellon the rights to redistribute these changes without encumbrance.
30  *
31  * 	@(#) cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
32  */
33 
34 /*
35  * Mach Operating System
36  * Copyright (c) 1989 Carnegie-Mellon University
37  * All rights reserved.  The CMU software License Agreement specifies
38  * the terms and conditions for use and redistribution.
39  */
40 
41 /*
42  * This code was written for the Coda file system at Carnegie Mellon
43  * University.  Contributers include David Steere, James Kistler, and
44  * M. Satyanarayanan.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: coda_vfsops.c,v 1.16 2002/03/27 05:10:41 phil Exp $");
49 
50 #ifdef	_LKM
51 #define	NVCODA 4
52 #else
53 #include <vcoda.h>
54 #endif
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/malloc.h>
59 #include <sys/conf.h>
60 #include <sys/namei.h>
61 #include <sys/mount.h>
62 #include <sys/proc.h>
63 #include <sys/select.h>
64 
65 #include <coda/coda.h>
66 #include <coda/cnode.h>
67 #include <coda/coda_vfsops.h>
68 #include <coda/coda_venus.h>
69 #include <coda/coda_subr.h>
70 #include <coda/coda_opstats.h>
71 /* for VN_RDEV */
72 #include <miscfs/specfs/specdev.h>
73 
74 int codadebug = 0;
75 
76 int coda_vfsop_print_entry = 0;
77 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__))
78 
79 struct vnode *coda_ctlvp;
80 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
81 
82 /* structure to keep statistics of internally generated/satisfied calls */
83 
84 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
85 
86 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
87 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
88 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
89 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
90 
91 extern int coda_nc_initialized;     /* Set if cache has been initialized */
92 extern int vc_nb_open __P((dev_t, int, int, struct proc *));
93 extern struct cdevsw cdevsw[];    /* For sanity check in coda_mount */
94 extern const struct vnodeopv_desc coda_vnodeop_opv_desc;
95 
96 const struct vnodeopv_desc * const coda_vnodeopv_descs[] = {
97 	&coda_vnodeop_opv_desc,
98 	NULL,
99 };
100 
101 struct vfsops coda_vfsops = {
102     MOUNT_CODA,
103     coda_mount,
104     coda_start,
105     coda_unmount,
106     coda_root,
107     coda_quotactl,
108     coda_nb_statfs,
109     coda_sync,
110     coda_vget,
111     (int (*) (struct mount *, struct fid *, struct vnode ** ))
112 	eopnotsupp,
113     (int (*) (struct vnode *, struct fid *)) eopnotsupp,
114     coda_init,
115     NULL,
116     coda_done,
117     coda_sysctl,
118     (int (*)(void)) eopnotsupp,
119     (int (*)(struct mount *, struct mbuf *, int *, struct ucred **))
120 	eopnotsupp,
121     coda_vnodeopv_descs,
122     0
123 };
124 
125 int
126 coda_vfsopstats_init(void)
127 {
128 	int i;
129 
130 	for (i=0;i<CODA_VFSOPS_SIZE;i++) {
131 		coda_vfsopstats[i].opcode = i;
132 		coda_vfsopstats[i].entries = 0;
133 		coda_vfsopstats[i].sat_intrn = 0;
134 		coda_vfsopstats[i].unsat_intrn = 0;
135 		coda_vfsopstats[i].gen_intrn = 0;
136 	}
137 
138 	return 0;
139 }
140 
141 /*
142  * cfs mount vfsop
143  * Set up mount info record and attach it to vfs struct.
144  */
145 /*ARGSUSED*/
146 int
147 coda_mount(vfsp, path, data, ndp, p)
148     struct mount *vfsp;		/* Allocated and initialized by mount(2) */
149     const char *path;		/* path covered: ignored by the fs-layer */
150     void *data;			/* Need to define a data type for this in netbsd? */
151     struct nameidata *ndp;	/* Clobber this to lookup the device name */
152     struct proc *p;		/* The ever-famous proc pointer */
153 {
154     struct vnode *dvp;
155     struct cnode *cp;
156     dev_t dev;
157     struct coda_mntinfo *mi;
158     struct vnode *rootvp;
159     ViceFid rootfid;
160     ViceFid ctlfid;
161     int error;
162 
163     ENTRY;
164 
165     coda_vfsopstats_init();
166     coda_vnodeopstats_init();
167 
168     MARK_ENTRY(CODA_MOUNT_STATS);
169     if (CODA_MOUNTED(vfsp)) {
170 	MARK_INT_FAIL(CODA_MOUNT_STATS);
171 	return(EBUSY);
172     }
173 
174     /* Validate mount device.  Similar to getmdev(). */
175 
176     NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p);
177     error = namei(ndp);
178     dvp = ndp->ni_vp;
179 
180     if (error) {
181 	MARK_INT_FAIL(CODA_MOUNT_STATS);
182 	return (error);
183     }
184     if (dvp->v_type != VCHR) {
185 	MARK_INT_FAIL(CODA_MOUNT_STATS);
186 	vrele(dvp);
187 	return(ENXIO);
188     }
189     dev = dvp->v_specinfo->si_rdev;
190     vrele(dvp);
191     if (major(dev) >= nchrdev || major(dev) < 0) {
192 	MARK_INT_FAIL(CODA_MOUNT_STATS);
193 	return(ENXIO);
194     }
195 
196     /*
197      * See if the device table matches our expectations.
198      */
199     if (cdevsw[major(dev)].d_open != vc_nb_open)
200     {
201 	MARK_INT_FAIL(CODA_MOUNT_STATS);
202 	return(ENXIO);
203     }
204 
205     if (minor(dev) >= NVCODA || minor(dev) < 0) {
206 	MARK_INT_FAIL(CODA_MOUNT_STATS);
207 	return(ENXIO);
208     }
209 
210     /*
211      * Initialize the mount record and link it to the vfs struct
212      */
213     mi = &coda_mnttbl[minor(dev)];
214 
215     if (!VC_OPEN(&mi->mi_vcomm)) {
216 	MARK_INT_FAIL(CODA_MOUNT_STATS);
217 	return(ENODEV);
218     }
219 
220     /* No initialization (here) of mi_vcomm! */
221     vfsp->mnt_data = (qaddr_t)mi;
222     vfsp->mnt_stat.f_fsid.val[0] = 0;
223     vfsp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_CODA);
224     mi->mi_vfsp = vfsp;
225 
226     /*
227      * Make a root vnode to placate the Vnode interface, but don't
228      * actually make the CODA_ROOT call to venus until the first call
229      * to coda_root in case a server is down while venus is starting.
230      */
231     rootfid.Volume = 0;
232     rootfid.Vnode = 0;
233     rootfid.Unique = 0;
234     cp = make_coda_node(&rootfid, vfsp, VDIR);
235     rootvp = CTOV(cp);
236     rootvp->v_flag |= VROOT;
237 
238     ctlfid.Volume = CTL_VOL;
239     ctlfid.Vnode = CTL_VNO;
240     ctlfid.Unique = CTL_UNI;
241 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
242     The above code seems to cause a loop in the cnode links.
243     I don't totally understand when it happens, it is caught
244     when closing down the system.
245  */
246     cp = make_coda_node(&ctlfid, 0, VCHR);
247 
248     coda_ctlvp = CTOV(cp);
249 
250     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
251     mi->mi_vfsp = vfsp;
252     mi->mi_rootvp = rootvp;
253 
254     /* set filesystem block size */
255     vfsp->mnt_stat.f_bsize = 8192;	    /* XXX -JJK */
256 
257     /* error is currently guaranteed to be zero, but in case some
258        code changes... */
259     CODADEBUG(1,
260 	     myprintf(("coda_mount returned %d\n",error)););
261     if (error)
262 	MARK_INT_FAIL(CODA_MOUNT_STATS);
263     else
264 	MARK_INT_SAT(CODA_MOUNT_STATS);
265 
266     return(error);
267 }
268 
269 int
270 coda_start(vfsp, flags, p)
271     struct mount *vfsp;
272     int flags;
273     struct proc *p;
274 {
275     ENTRY;
276     return (0);
277 }
278 
279 int
280 coda_unmount(vfsp, mntflags, p)
281     struct mount *vfsp;
282     int mntflags;
283     struct proc *p;
284 {
285     struct coda_mntinfo *mi = vftomi(vfsp);
286     int active, error = 0;
287 
288     ENTRY;
289     MARK_ENTRY(CODA_UMOUNT_STATS);
290     if (!CODA_MOUNTED(vfsp)) {
291 	MARK_INT_FAIL(CODA_UMOUNT_STATS);
292 	return(EINVAL);
293     }
294 
295     if (mi->mi_vfsp == vfsp) {	/* We found the victim */
296 	if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
297 	    return (EBUSY); 	/* Venus is still running */
298 
299 #ifdef	DEBUG
300 	printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
301 #endif
302 	vrele(mi->mi_rootvp);
303 
304 	active = coda_kill(vfsp, NOT_DOWNCALL);
305 	mi->mi_rootvp->v_flag &= ~VROOT;
306 	error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
307 	printf("coda_unmount: active = %d, vflush active %d\n", active, error);
308 	error = 0;
309 
310 	/* I'm going to take this out to allow lookups to go through. I'm
311 	 * not sure it's important anyway. -- DCS 2/2/94
312 	 */
313 	/* vfsp->VFS_DATA = NULL; */
314 
315 	/* No more vfsp's to hold onto */
316 	mi->mi_vfsp = NULL;
317 	mi->mi_rootvp = NULL;
318 
319 	if (error)
320 	    MARK_INT_FAIL(CODA_UMOUNT_STATS);
321 	else
322 	    MARK_INT_SAT(CODA_UMOUNT_STATS);
323 
324 	return(error);
325     }
326     return (EINVAL);
327 }
328 
329 /*
330  * find root of cfs
331  */
332 int
333 coda_root(vfsp, vpp)
334 	struct mount *vfsp;
335 	struct vnode **vpp;
336 {
337     struct coda_mntinfo *mi = vftomi(vfsp);
338     struct vnode **result;
339     int error;
340     struct proc *p = curproc;    /* XXX - bnoble */
341     ViceFid VFid;
342 
343     ENTRY;
344     MARK_ENTRY(CODA_ROOT_STATS);
345     result = NULL;
346 
347     if (vfsp == mi->mi_vfsp) {
348 	if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
349 	    (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
350 	    (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
351 	    { /* Found valid root. */
352 		*vpp = mi->mi_rootvp;
353 		/* On Mach, this is vref.  On NetBSD, VOP_LOCK */
354 		vref(*vpp);
355 		vn_lock(*vpp, LK_EXCLUSIVE);
356 		MARK_INT_SAT(CODA_ROOT_STATS);
357 		return(0);
358 	    }
359     }
360 
361     error = venus_root(vftomi(vfsp), p->p_cred->pc_ucred, p, &VFid);
362 
363     if (!error) {
364 	/*
365 	 * Save the new rootfid in the cnode, and rehash the cnode into the
366 	 * cnode hash with the new fid key.
367 	 */
368 	coda_unsave(VTOC(mi->mi_rootvp));
369 	VTOC(mi->mi_rootvp)->c_fid = VFid;
370 	coda_save(VTOC(mi->mi_rootvp));
371 
372 	*vpp = mi->mi_rootvp;
373 	vref(*vpp);
374 	vn_lock(*vpp, LK_EXCLUSIVE);
375 	MARK_INT_SAT(CODA_ROOT_STATS);
376 	goto exit;
377     } else if (error == ENODEV || error == EINTR) {
378 	/* Gross hack here! */
379 	/*
380 	 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
381 	 * ENODEV. Return the uninitialized root vnode to allow vfs
382 	 * operations such as unmount to continue. Without this hack,
383 	 * there is no way to do an unmount if Venus dies before a
384 	 * successful CODA_ROOT call is done. All vnode operations
385 	 * will fail.
386 	 */
387 	*vpp = mi->mi_rootvp;
388 	vref(*vpp);
389 	vn_lock(*vpp, LK_EXCLUSIVE);
390 	MARK_INT_FAIL(CODA_ROOT_STATS);
391 	error = 0;
392 	goto exit;
393     } else {
394 	CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
395 	MARK_INT_FAIL(CODA_ROOT_STATS);
396 
397 	goto exit;
398     }
399  exit:
400     return(error);
401 }
402 
403 int
404 coda_quotactl(vfsp, cmd, uid, arg, p)
405     struct mount *vfsp;
406     int cmd;
407     uid_t uid;
408     caddr_t arg;
409     struct proc *p;
410 {
411     ENTRY;
412     return (EOPNOTSUPP);
413 }
414 
415 /*
416  * Get file system statistics.
417  */
418 int
419 coda_nb_statfs(vfsp, sbp, p)
420     struct mount *vfsp;
421     struct statfs *sbp;
422     struct proc *p;
423 {
424     struct coda_statfs fsstat;
425     int error;
426 
427     ENTRY;
428     MARK_ENTRY(CODA_STATFS_STATS);
429     if (!CODA_MOUNTED(vfsp)) {
430 /*	MARK_INT_FAIL(CODA_STATFS_STATS); */
431 	return(EINVAL);
432     }
433 
434     memset(sbp, 0, sizeof(struct statfs));
435     /* XXX - what to do about f_flags, others? --bnoble */
436     /* Below This is what AFS does
437     	#define NB_SFS_SIZ 0x895440
438      */
439     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
440 
441     error = venus_statfs(vftomi(vfsp), p->p_cred->pc_ucred, p, &fsstat);
442 
443     if (!error) {
444 	sbp->f_type = 0;
445 	sbp->f_bsize = 8192; /* XXX */
446 	sbp->f_iosize = 8192; /* XXX */
447 	sbp->f_blocks = fsstat.f_blocks;
448 	sbp->f_bfree  = fsstat.f_bfree;
449 	sbp->f_bavail = fsstat.f_bavail;
450 	sbp->f_files  = fsstat.f_files;
451 	sbp->f_ffree  = fsstat.f_ffree;
452         bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid),
453 	      (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
454         strncpy(sbp->f_fstypename, MOUNT_CODA, MFSNAMELEN-1);
455         strcpy(sbp->f_mntonname, "/coda");
456         strcpy(sbp->f_mntfromname, "CODA");
457     }
458 
459     MARK_INT_SAT(CODA_STATFS_STATS);
460     return(error);
461 }
462 
463 /*
464  * Flush any pending I/O.
465  */
466 int
467 coda_sync(vfsp, waitfor, cred, p)
468     struct mount *vfsp;
469     int    waitfor;
470     struct ucred *cred;
471     struct proc *p;
472 {
473     ENTRY;
474     MARK_ENTRY(CODA_SYNC_STATS);
475     MARK_INT_SAT(CODA_SYNC_STATS);
476     return(0);
477 }
478 
479 int
480 coda_vget(vfsp, ino, vpp)
481     struct mount *vfsp;
482     ino_t ino;
483     struct vnode **vpp;
484 {
485     ENTRY;
486     return (EOPNOTSUPP);
487 }
488 
489 /*
490  * fhtovp is now what vget used to be in 4.3-derived systems.  For
491  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
492  * a type-specific fid.
493  */
494 int
495 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
496     struct mount *vfsp;
497     struct fid *fhp;
498     struct mbuf *nam;
499     struct vnode **vpp;
500     int *exflagsp;
501     struct ucred **creadanonp;
502 {
503     struct cfid *cfid = (struct cfid *)fhp;
504     struct cnode *cp = 0;
505     int error;
506     struct proc *p = curproc; /* XXX -mach */
507     ViceFid VFid;
508     int vtype;
509 
510     ENTRY;
511 
512     MARK_ENTRY(CODA_VGET_STATS);
513     /* Check for vget of control object. */
514     if (IS_CTL_FID(&cfid->cfid_fid)) {
515 	*vpp = coda_ctlvp;
516 	vref(coda_ctlvp);
517 	MARK_INT_SAT(CODA_VGET_STATS);
518 	return(0);
519     }
520 
521     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype);
522 
523     if (error) {
524 	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
525 	    *vpp = (struct vnode *)0;
526     } else {
527 	CODADEBUG(CODA_VGET,
528 		 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
529 			VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
530 
531 	cp = make_coda_node(&VFid, vfsp, vtype);
532 	*vpp = CTOV(cp);
533     }
534     return(error);
535 }
536 
537 int
538 coda_vptofh(vnp, fidp)
539     struct vnode *vnp;
540     struct fid   *fidp;
541 {
542     ENTRY;
543     return (EOPNOTSUPP);
544 }
545 
546 void
547 coda_init(void)
548 {
549     ENTRY;
550 }
551 
552 void
553 coda_done(void)
554 {
555     ENTRY;
556 }
557 
558 int
559 coda_sysctl(name, namelen, oldp, oldlp, newp, newl, p)
560 	int *name;
561 	u_int namelen;
562 	void *oldp;
563 	size_t *oldlp;
564 	void *newp;
565 	size_t newl;
566 	struct proc *p;
567 {
568 
569 	/* all sysctl names at this level are terminal */
570 	if (namelen != 1)
571 		return (ENOTDIR);		/* overloaded */
572 
573 	switch (name[0]) {
574 /*
575 	case FFS_CLUSTERREAD:
576 		return (sysctl_int(oldp, oldlp, newp, newl, &doclusterread));
577  */
578 	default:
579 		return (EOPNOTSUPP);
580 	}
581 	/* NOTREACHED */
582 }
583 
584 /*
585  * To allow for greater ease of use, some vnodes may be orphaned when
586  * Venus dies.  Certain operations should still be allowed to go
587  * through, but without propagating ophan-ness.  So this function will
588  * get a new vnode for the file from the current run of Venus.  */
589 
590 int
591 getNewVnode(vpp)
592      struct vnode **vpp;
593 {
594     struct cfid cfid;
595     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
596 
597     ENTRY;
598 
599     cfid.cfid_len = (short)sizeof(ViceFid);
600     cfid.cfid_fid = VTOC(*vpp)->c_fid;	/* Structure assignment. */
601     /* XXX ? */
602 
603     /* We're guessing that if set, the 1st element on the list is a
604      * valid vnode to use. If not, return ENODEV as venus is dead.
605      */
606     if (mi->mi_vfsp == NULL)
607 	return ENODEV;
608 
609     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
610 		      NULL, NULL);
611 }
612 
613 #include <ufs/ufs/quota.h>
614 #include <ufs/ufs/ufsmount.h>
615 /* get the mount structure corresponding to a given device.  Assume
616  * device corresponds to a UFS. Return NULL if no device is found.
617  */
618 struct mount *devtomp(dev)
619     dev_t dev;
620 {
621     struct mount *mp, *nmp;
622 
623     for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) {
624 	nmp = mp->mnt_list.cqe_next;
625 	if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) &&
626 	    ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) {
627 	    /* mount corresponds to UFS and the device matches one we want */
628 	    return(mp);
629 	}
630     }
631     /* mount structure wasn't found */
632     return(NULL);
633 }
634