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