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