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