xref: /netbsd-src/sys/coda/coda_vfsops.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: coda_vfsops.c,v 1.15 2001/11/23 17:42:48 perry 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.15 2001/11/23 17:42:48 perry 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     ENTRY;
425 /*  MARK_ENTRY(CODA_STATFS_STATS); */
426     if (!CODA_MOUNTED(vfsp)) {
427 /*	MARK_INT_FAIL(CODA_STATFS_STATS);*/
428 	return(EINVAL);
429     }
430 
431     memset(sbp, 0, sizeof(struct statfs));
432     /* XXX - what to do about f_flags, others? --bnoble */
433     /* Below This is what AFS does
434     	#define NB_SFS_SIZ 0x895440
435      */
436     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
437     sbp->f_type = 0;
438     sbp->f_bsize = 8192; /* XXX */
439     sbp->f_iosize = 8192; /* XXX */
440 #define NB_SFS_SIZ 0x8AB75D
441     sbp->f_blocks = NB_SFS_SIZ;
442     sbp->f_bfree = NB_SFS_SIZ;
443     sbp->f_bavail = NB_SFS_SIZ;
444     sbp->f_files = NB_SFS_SIZ;
445     sbp->f_ffree = NB_SFS_SIZ;
446     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
447     strncpy(sbp->f_fstypename, MOUNT_CODA, MFSNAMELEN-1);
448     strcpy(sbp->f_mntonname, "/coda");
449     strcpy(sbp->f_mntfromname, "CODA");
450 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
451     return(0);
452 }
453 
454 /*
455  * Flush any pending I/O.
456  */
457 int
458 coda_sync(vfsp, waitfor, cred, p)
459     struct mount *vfsp;
460     int    waitfor;
461     struct ucred *cred;
462     struct proc *p;
463 {
464     ENTRY;
465     MARK_ENTRY(CODA_SYNC_STATS);
466     MARK_INT_SAT(CODA_SYNC_STATS);
467     return(0);
468 }
469 
470 int
471 coda_vget(vfsp, ino, vpp)
472     struct mount *vfsp;
473     ino_t ino;
474     struct vnode **vpp;
475 {
476     ENTRY;
477     return (EOPNOTSUPP);
478 }
479 
480 /*
481  * fhtovp is now what vget used to be in 4.3-derived systems.  For
482  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
483  * a type-specific fid.
484  */
485 int
486 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
487     struct mount *vfsp;
488     struct fid *fhp;
489     struct mbuf *nam;
490     struct vnode **vpp;
491     int *exflagsp;
492     struct ucred **creadanonp;
493 {
494     struct cfid *cfid = (struct cfid *)fhp;
495     struct cnode *cp = 0;
496     int error;
497     struct proc *p = curproc; /* XXX -mach */
498     ViceFid VFid;
499     int vtype;
500 
501     ENTRY;
502 
503     MARK_ENTRY(CODA_VGET_STATS);
504     /* Check for vget of control object. */
505     if (IS_CTL_FID(&cfid->cfid_fid)) {
506 	*vpp = coda_ctlvp;
507 	vref(coda_ctlvp);
508 	MARK_INT_SAT(CODA_VGET_STATS);
509 	return(0);
510     }
511 
512     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype);
513 
514     if (error) {
515 	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
516 	    *vpp = (struct vnode *)0;
517     } else {
518 	CODADEBUG(CODA_VGET,
519 		 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
520 			VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
521 
522 	cp = make_coda_node(&VFid, vfsp, vtype);
523 	*vpp = CTOV(cp);
524     }
525     return(error);
526 }
527 
528 int
529 coda_vptofh(vnp, fidp)
530     struct vnode *vnp;
531     struct fid   *fidp;
532 {
533     ENTRY;
534     return (EOPNOTSUPP);
535 }
536 
537 void
538 coda_init(void)
539 {
540     ENTRY;
541 }
542 
543 void
544 coda_done(void)
545 {
546     ENTRY;
547 }
548 
549 int
550 coda_sysctl(name, namelen, oldp, oldlp, newp, newl, p)
551 	int *name;
552 	u_int namelen;
553 	void *oldp;
554 	size_t *oldlp;
555 	void *newp;
556 	size_t newl;
557 	struct proc *p;
558 {
559 
560 	/* all sysctl names at this level are terminal */
561 	if (namelen != 1)
562 		return (ENOTDIR);		/* overloaded */
563 
564 	switch (name[0]) {
565 /*
566 	case FFS_CLUSTERREAD:
567 		return (sysctl_int(oldp, oldlp, newp, newl, &doclusterread));
568  */
569 	default:
570 		return (EOPNOTSUPP);
571 	}
572 	/* NOTREACHED */
573 }
574 
575 /*
576  * To allow for greater ease of use, some vnodes may be orphaned when
577  * Venus dies.  Certain operations should still be allowed to go
578  * through, but without propagating ophan-ness.  So this function will
579  * get a new vnode for the file from the current run of Venus.  */
580 
581 int
582 getNewVnode(vpp)
583      struct vnode **vpp;
584 {
585     struct cfid cfid;
586     struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
587 
588     ENTRY;
589 
590     cfid.cfid_len = (short)sizeof(ViceFid);
591     cfid.cfid_fid = VTOC(*vpp)->c_fid;	/* Structure assignment. */
592     /* XXX ? */
593 
594     /* We're guessing that if set, the 1st element on the list is a
595      * valid vnode to use. If not, return ENODEV as venus is dead.
596      */
597     if (mi->mi_vfsp == NULL)
598 	return ENODEV;
599 
600     return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
601 		      NULL, NULL);
602 }
603 
604 #include <ufs/ufs/quota.h>
605 #include <ufs/ufs/ufsmount.h>
606 /* get the mount structure corresponding to a given device.  Assume
607  * device corresponds to a UFS. Return NULL if no device is found.
608  */
609 struct mount *devtomp(dev)
610     dev_t dev;
611 {
612     struct mount *mp, *nmp;
613 
614     for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) {
615 	nmp = mp->mnt_list.cqe_next;
616 	if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) &&
617 	    ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) {
618 	    /* mount corresponds to UFS and the device matches one we want */
619 	    return(mp);
620 	}
621     }
622     /* mount structure wasn't found */
623     return(NULL);
624 }
625