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