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