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