xref: /netbsd-src/sys/coda/coda_vfsops.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: coda_vfsops.c,v 1.12 2001/07/18 16:12:31 thorpej 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 #ifdef	_LKM
48 #define	NVCODA 4
49 #else
50 #include <vcoda.h>
51 #endif
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/conf.h>
57 #include <sys/namei.h>
58 #include <sys/mount.h>
59 #include <sys/proc.h>
60 #include <sys/select.h>
61 
62 #include <coda/coda.h>
63 #include <coda/cnode.h>
64 #include <coda/coda_vfsops.h>
65 #include <coda/coda_venus.h>
66 #include <coda/coda_subr.h>
67 #include <coda/coda_opstats.h>
68 /* for VN_RDEV */
69 #include <miscfs/specfs/specdev.h>
70 
71 int codadebug = 0;
72 
73 int coda_vfsop_print_entry = 0;
74 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
75 
76 struct vnode *coda_ctlvp;
77 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
78 
79 /* structure to keep statistics of internally generated/satisfied calls */
80 
81 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
82 
83 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
84 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
85 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
86 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
87 
88 extern int coda_nc_initialized;     /* Set if cache has been initialized */
89 extern int vc_nb_open __P((dev_t, int, int, struct proc *));
90 extern struct cdevsw cdevsw[];    /* For sanity check in coda_mount */
91 extern const struct vnodeopv_desc coda_vnodeop_opv_desc;
92 
93 const struct vnodeopv_desc * const coda_vnodeopv_descs[] = {
94 	&coda_vnodeop_opv_desc,
95 	NULL,
96 };
97 
98 struct vfsops coda_vfsops = {
99     MOUNT_CODA,
100     coda_mount,
101     coda_start,
102     coda_unmount,
103     coda_root,
104     coda_quotactl,
105     coda_nb_statfs,
106     coda_sync,
107     coda_vget,
108     (int (*) (struct mount *, struct fid *, struct vnode ** ))
109 	eopnotsupp,
110     (int (*) (struct vnode *, struct fid *)) eopnotsupp,
111     coda_init,
112 #ifdef __NetBSD__
113     coda_done,
114 #endif
115     coda_sysctl,
116     (int (*)(void)) eopnotsupp,
117     (int (*)(struct mount *, struct mbuf *, int *, struct ucred **))
118 	eopnotsupp,
119     coda_vnodeopv_descs,
120     0
121 };
122 
123 int
124 coda_vfsopstats_init(void)
125 {
126 	int i;
127 
128 	for (i=0;i<CODA_VFSOPS_SIZE;i++) {
129 		coda_vfsopstats[i].opcode = i;
130 		coda_vfsopstats[i].entries = 0;
131 		coda_vfsopstats[i].sat_intrn = 0;
132 		coda_vfsopstats[i].unsat_intrn = 0;
133 		coda_vfsopstats[i].gen_intrn = 0;
134 	}
135 
136 	return 0;
137 }
138 
139 /*
140  * cfs mount vfsop
141  * Set up mount info record and attach it to vfs struct.
142  */
143 /*ARGSUSED*/
144 int
145 coda_mount(vfsp, path, data, ndp, p)
146     struct mount *vfsp;		/* Allocated and initialized by mount(2) */
147     const char *path;		/* path covered: ignored by the fs-layer */
148     void *data;			/* Need to define a data type for this in netbsd? */
149     struct nameidata *ndp;	/* Clobber this to lookup the device name */
150     struct proc *p;		/* The ever-famous proc pointer */
151 {
152     struct vnode *dvp;
153     struct cnode *cp;
154     dev_t dev;
155     struct coda_mntinfo *mi;
156     struct vnode *rootvp;
157     ViceFid rootfid;
158     ViceFid ctlfid;
159     int error;
160 
161     ENTRY;
162 
163     coda_vfsopstats_init();
164     coda_vnodeopstats_init();
165 
166     MARK_ENTRY(CODA_MOUNT_STATS);
167     if (CODA_MOUNTED(vfsp)) {
168 	MARK_INT_FAIL(CODA_MOUNT_STATS);
169 	return(EBUSY);
170     }
171 
172     /* Validate mount device.  Similar to getmdev(). */
173 
174     NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p);
175     error = namei(ndp);
176     dvp = ndp->ni_vp;
177 
178     if (error) {
179 	MARK_INT_FAIL(CODA_MOUNT_STATS);
180 	return (error);
181     }
182     if (dvp->v_type != VCHR) {
183 	MARK_INT_FAIL(CODA_MOUNT_STATS);
184 	vrele(dvp);
185 	return(ENXIO);
186     }
187     dev = dvp->v_specinfo->si_rdev;
188     vrele(dvp);
189     if (major(dev) >= nchrdev || major(dev) < 0) {
190 	MARK_INT_FAIL(CODA_MOUNT_STATS);
191 	return(ENXIO);
192     }
193 
194     /*
195      * See if the device table matches our expectations.
196      */
197     if (cdevsw[major(dev)].d_open != vc_nb_open)
198     {
199 	MARK_INT_FAIL(CODA_MOUNT_STATS);
200 	return(ENXIO);
201     }
202 
203     if (minor(dev) >= NVCODA || minor(dev) < 0) {
204 	MARK_INT_FAIL(CODA_MOUNT_STATS);
205 	return(ENXIO);
206     }
207 
208     /*
209      * Initialize the mount record and link it to the vfs struct
210      */
211     mi = &coda_mnttbl[minor(dev)];
212 
213     if (!VC_OPEN(&mi->mi_vcomm)) {
214 	MARK_INT_FAIL(CODA_MOUNT_STATS);
215 	return(ENODEV);
216     }
217 
218     /* No initialization (here) of mi_vcomm! */
219     vfsp->mnt_data = (qaddr_t)mi;
220     vfsp->mnt_stat.f_fsid.val[0] = 0;
221     vfsp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_CODA);
222     mi->mi_vfsp = vfsp;
223 
224     /*
225      * Make a root vnode to placate the Vnode interface, but don't
226      * actually make the CODA_ROOT call to venus until the first call
227      * to coda_root in case a server is down while venus is starting.
228      */
229     rootfid.Volume = 0;
230     rootfid.Vnode = 0;
231     rootfid.Unique = 0;
232     cp = make_coda_node(&rootfid, vfsp, VDIR);
233     rootvp = CTOV(cp);
234     rootvp->v_flag |= VROOT;
235 
236     ctlfid.Volume = CTL_VOL;
237     ctlfid.Vnode = CTL_VNO;
238     ctlfid.Unique = CTL_UNI;
239 /*  cp = make_coda_node(&ctlfid, vfsp, VCHR);
240     The above code seems to cause a loop in the cnode links.
241     I don't totally understand when it happens, it is caught
242     when closing down the system.
243  */
244     cp = make_coda_node(&ctlfid, 0, VCHR);
245 
246     coda_ctlvp = CTOV(cp);
247 
248     /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
249     mi->mi_vfsp = vfsp;
250     mi->mi_rootvp = rootvp;
251 
252     /* set filesystem block size */
253     vfsp->mnt_stat.f_bsize = 8192;	    /* XXX -JJK */
254 
255     /* error is currently guaranteed to be zero, but in case some
256        code changes... */
257     CODADEBUG(1,
258 	     myprintf(("coda_mount returned %d\n",error)););
259     if (error)
260 	MARK_INT_FAIL(CODA_MOUNT_STATS);
261     else
262 	MARK_INT_SAT(CODA_MOUNT_STATS);
263 
264     return(error);
265 }
266 
267 int
268 coda_start(vfsp, flags, p)
269     struct mount *vfsp;
270     int flags;
271     struct proc *p;
272 {
273     ENTRY;
274     return (0);
275 }
276 
277 int
278 coda_unmount(vfsp, mntflags, p)
279     struct mount *vfsp;
280     int mntflags;
281     struct proc *p;
282 {
283     struct coda_mntinfo *mi = vftomi(vfsp);
284     int active, error = 0;
285 
286     ENTRY;
287     MARK_ENTRY(CODA_UMOUNT_STATS);
288     if (!CODA_MOUNTED(vfsp)) {
289 	MARK_INT_FAIL(CODA_UMOUNT_STATS);
290 	return(EINVAL);
291     }
292 
293     if (mi->mi_vfsp == vfsp) {	/* We found the victim */
294 	if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
295 	    return (EBUSY); 	/* Venus is still running */
296 
297 #ifdef	DEBUG
298 	printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
299 #endif
300 	vrele(mi->mi_rootvp);
301 
302 	active = coda_kill(vfsp, NOT_DOWNCALL);
303 	mi->mi_rootvp->v_flag &= ~VROOT;
304 	error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE);
305 	printf("coda_unmount: active = %d, vflush active %d\n", active, error);
306 	error = 0;
307 
308 	/* I'm going to take this out to allow lookups to go through. I'm
309 	 * not sure it's important anyway. -- DCS 2/2/94
310 	 */
311 	/* vfsp->VFS_DATA = NULL; */
312 
313 	/* No more vfsp's to hold onto */
314 	mi->mi_vfsp = NULL;
315 	mi->mi_rootvp = NULL;
316 
317 	if (error)
318 	    MARK_INT_FAIL(CODA_UMOUNT_STATS);
319 	else
320 	    MARK_INT_SAT(CODA_UMOUNT_STATS);
321 
322 	return(error);
323     }
324     return (EINVAL);
325 }
326 
327 /*
328  * find root of cfs
329  */
330 int
331 coda_root(vfsp, vpp)
332 	struct mount *vfsp;
333 	struct vnode **vpp;
334 {
335     struct coda_mntinfo *mi = vftomi(vfsp);
336     struct vnode **result;
337     int error;
338     struct proc *p = curproc;    /* XXX - bnoble */
339     ViceFid VFid;
340 
341     ENTRY;
342     MARK_ENTRY(CODA_ROOT_STATS);
343     result = NULL;
344 
345     if (vfsp == mi->mi_vfsp) {
346 	if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
347 	    (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
348 	    (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
349 	    { /* Found valid root. */
350 		*vpp = mi->mi_rootvp;
351 		/* On Mach, this is vref.  On NetBSD, VOP_LOCK */
352 		vref(*vpp);
353 		vn_lock(*vpp, LK_EXCLUSIVE);
354 		MARK_INT_SAT(CODA_ROOT_STATS);
355 		return(0);
356 	    }
357     }
358 
359     error = venus_root(vftomi(vfsp), p->p_cred->pc_ucred, p, &VFid);
360 
361     if (!error) {
362 	/*
363 	 * Save the new rootfid in the cnode, and rehash the cnode into the
364 	 * cnode hash with the new fid key.
365 	 */
366 	coda_unsave(VTOC(mi->mi_rootvp));
367 	VTOC(mi->mi_rootvp)->c_fid = VFid;
368 	coda_save(VTOC(mi->mi_rootvp));
369 
370 	*vpp = mi->mi_rootvp;
371 	vref(*vpp);
372 	vn_lock(*vpp, LK_EXCLUSIVE);
373 	MARK_INT_SAT(CODA_ROOT_STATS);
374 	goto exit;
375     } else if (error == ENODEV || error == EINTR) {
376 	/* Gross hack here! */
377 	/*
378 	 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
379 	 * ENODEV. Return the uninitialized root vnode to allow vfs
380 	 * operations such as unmount to continue. Without this hack,
381 	 * there is no way to do an unmount if Venus dies before a
382 	 * successful CODA_ROOT call is done. All vnode operations
383 	 * will fail.
384 	 */
385 	*vpp = mi->mi_rootvp;
386 	vref(*vpp);
387 	vn_lock(*vpp, LK_EXCLUSIVE);
388 	MARK_INT_FAIL(CODA_ROOT_STATS);
389 	error = 0;
390 	goto exit;
391     } else {
392 	CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
393 	MARK_INT_FAIL(CODA_ROOT_STATS);
394 
395 	goto exit;
396     }
397  exit:
398     return(error);
399 }
400 
401 int
402 coda_quotactl(vfsp, cmd, uid, arg, p)
403     struct mount *vfsp;
404     int cmd;
405     uid_t uid;
406     caddr_t arg;
407     struct proc *p;
408 {
409     ENTRY;
410     return (EOPNOTSUPP);
411 }
412 
413 /*
414  * Get file system statistics.
415  */
416 int
417 coda_nb_statfs(vfsp, sbp, p)
418     struct mount *vfsp;
419     struct statfs *sbp;
420     struct proc *p;
421 {
422     ENTRY;
423 /*  MARK_ENTRY(CODA_STATFS_STATS); */
424     if (!CODA_MOUNTED(vfsp)) {
425 /*	MARK_INT_FAIL(CODA_STATFS_STATS);*/
426 	return(EINVAL);
427     }
428 
429     memset(sbp, 0, sizeof(struct statfs));
430     /* XXX - what to do about f_flags, others? --bnoble */
431     /* Below This is what AFS does
432     	#define NB_SFS_SIZ 0x895440
433      */
434     /* Note: Normal fs's have a bsize of 0x400 == 1024 */
435     sbp->f_type = 0;
436     sbp->f_bsize = 8192; /* XXX */
437     sbp->f_iosize = 8192; /* XXX */
438 #define NB_SFS_SIZ 0x8AB75D
439     sbp->f_blocks = NB_SFS_SIZ;
440     sbp->f_bfree = NB_SFS_SIZ;
441     sbp->f_bavail = NB_SFS_SIZ;
442     sbp->f_files = NB_SFS_SIZ;
443     sbp->f_ffree = NB_SFS_SIZ;
444     bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
445     strncpy(sbp->f_fstypename, MOUNT_CODA, MFSNAMELEN-1);
446     strcpy(sbp->f_mntonname, "/coda");
447     strcpy(sbp->f_mntfromname, "CODA");
448 /*  MARK_INT_SAT(CODA_STATFS_STATS); */
449     return(0);
450 }
451 
452 /*
453  * Flush any pending I/O.
454  */
455 int
456 coda_sync(vfsp, waitfor, cred, p)
457     struct mount *vfsp;
458     int    waitfor;
459     struct ucred *cred;
460     struct proc *p;
461 {
462     ENTRY;
463     MARK_ENTRY(CODA_SYNC_STATS);
464     MARK_INT_SAT(CODA_SYNC_STATS);
465     return(0);
466 }
467 
468 int
469 coda_vget(vfsp, ino, vpp)
470     struct mount *vfsp;
471     ino_t ino;
472     struct vnode **vpp;
473 {
474     ENTRY;
475     return (EOPNOTSUPP);
476 }
477 
478 /*
479  * fhtovp is now what vget used to be in 4.3-derived systems.  For
480  * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
481  * a type-specific fid.
482  */
483 int
484 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
485     struct mount *vfsp;
486     struct fid *fhp;
487     struct mbuf *nam;
488     struct vnode **vpp;
489     int *exflagsp;
490     struct ucred **creadanonp;
491 {
492     struct cfid *cfid = (struct cfid *)fhp;
493     struct cnode *cp = 0;
494     int error;
495     struct proc *p = curproc; /* XXX -mach */
496     ViceFid VFid;
497     int vtype;
498 
499     ENTRY;
500 
501     MARK_ENTRY(CODA_VGET_STATS);
502     /* Check for vget of control object. */
503     if (IS_CTL_FID(&cfid->cfid_fid)) {
504 	*vpp = coda_ctlvp;
505 	vref(coda_ctlvp);
506 	MARK_INT_SAT(CODA_VGET_STATS);
507 	return(0);
508     }
509 
510     error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype);
511 
512     if (error) {
513 	CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
514 	    *vpp = (struct vnode *)0;
515     } else {
516 	CODADEBUG(CODA_VGET,
517 		 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
518 			VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
519 
520 	cp = make_coda_node(&VFid, vfsp, vtype);
521 	*vpp = CTOV(cp);
522     }
523     return(error);
524 }
525 
526 int
527 coda_vptofh(vnp, fidp)
528     struct vnode *vnp;
529     struct fid   *fidp;
530 {
531     ENTRY;
532     return (EOPNOTSUPP);
533 }
534 
535 void
536 coda_init(void)
537 {
538     ENTRY;
539 }
540 
541 #ifdef __NetBSD__
542 void
543 coda_done(void)
544 {
545     ENTRY;
546 }
547 #endif
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