3 * Coda: an Experimental Distributed File System
6 * Copyright (c) 1987-1998 Carnegie Mellon University
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation, and
14 * that credit is given to Carnegie Mellon University in all documents
15 * and publicity pertaining to direct or indirect use of this code or its
18 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
19 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
20 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
21 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
22 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
23 * ANY DERIVATIVE WORK.
25 * Carnegie Mellon encourages users of this software to return any
26 * improvements or extensions that they make, and to grant Carnegie
27 * Mellon the rights to redistribute these changes without encumbrance.
29 * @(#) src/sys/cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30 * $FreeBSD: src/sys/coda/coda_vfsops.c,v 1.24.2.1 2001/07/26 20:36:45 iedowse Exp $
31 * $DragonFly: src/sys/vfs/coda/Attic/coda_vfsops.c,v 1.4 2003/06/25 03:55:44 dillon Exp $
36 * Mach Operating System
37 * Copyright (c) 1989 Carnegie-Mellon University
38 * All rights reserved. The CMU software License Agreement specifies
39 * the terms and conditions for use and redistribution.
43 * This code was written for the Coda file system at Carnegie Mellon
44 * University. Contributers include David Steere, James Kistler, and
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
54 #include <sys/malloc.h>
56 #include <sys/namei.h>
57 #include <sys/mount.h>
58 #include <sys/select.h>
60 #include <vm/vm_zone.h>
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>
69 MALLOC_DEFINE(M_CODA, "CODA storage", "Various Coda Structures");
72 int coda_vfsop_print_entry = 0;
73 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
75 struct vnode *coda_ctlvp;
76 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */
78 /* structure to keep statistics of internally generated/satisfied calls */
80 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
82 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
83 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
84 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
85 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
87 extern int coda_nc_initialized; /* Set if cache has been initialized */
88 extern int vc_nb_open __P((dev_t, int, int, d_thread_t *));
91 coda_vfsopstats_init(void)
95 for (i=0;i<CODA_VFSOPS_SIZE;i++) {
96 coda_vfsopstats[i].opcode = i;
97 coda_vfsopstats[i].entries = 0;
98 coda_vfsopstats[i].sat_intrn = 0;
99 coda_vfsopstats[i].unsat_intrn = 0;
100 coda_vfsopstats[i].gen_intrn = 0;
108 * Set up mount info record and attach it to vfs struct.
112 coda_mount(vfsp, path, data, ndp, td)
113 struct mount *vfsp; /* Allocated and initialized by mount(2) */
114 char *path; /* path covered: ignored by the fs-layer */
115 caddr_t data; /* Need to define a data type for this in netbsd? */
116 struct nameidata *ndp; /* Clobber this to lookup the device name */
117 struct thread *td; /* The ever-famous proc pointer */
122 struct coda_mntinfo *mi;
123 struct vnode *rootvp;
130 coda_vfsopstats_init();
131 coda_vnodeopstats_init();
133 MARK_ENTRY(CODA_MOUNT_STATS);
134 if (CODA_MOUNTED(vfsp)) {
135 MARK_INT_FAIL(CODA_MOUNT_STATS);
139 /* Validate mount device. Similar to getmdev(). */
140 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, td);
145 MARK_INT_FAIL(CODA_MOUNT_STATS);
148 if (dvp->v_type != VCHR) {
149 MARK_INT_FAIL(CODA_MOUNT_STATS);
151 NDFREE(ndp, NDF_ONLY_PNBUF);
156 NDFREE(ndp, NDF_ONLY_PNBUF);
159 * See if the device table matches our expectations.
161 if (devsw(dev)->d_open != vc_nb_open)
163 MARK_INT_FAIL(CODA_MOUNT_STATS);
167 if (minor(dev) >= NVCODA || minor(dev) < 0) {
168 MARK_INT_FAIL(CODA_MOUNT_STATS);
173 * Initialize the mount record and link it to the vfs struct
175 mi = &coda_mnttbl[minor(dev)];
177 if (!VC_OPEN(&mi->mi_vcomm)) {
178 MARK_INT_FAIL(CODA_MOUNT_STATS);
182 /* No initialization (here) of mi_vcomm! */
183 vfsp->mnt_data = (qaddr_t)mi;
184 vfs_getnewfsid (vfsp);
189 * Make a root vnode to placate the Vnode interface, but don't
190 * actually make the CODA_ROOT call to venus until the first call
191 * to coda_root in case a server is down while venus is starting.
196 cp = make_coda_node(&rootfid, vfsp, VDIR);
198 rootvp->v_flag |= VROOT;
200 ctlfid.Volume = CTL_VOL;
201 ctlfid.Vnode = CTL_VNO;
202 ctlfid.Unique = CTL_UNI;
203 /* cp = make_coda_node(&ctlfid, vfsp, VCHR);
204 The above code seems to cause a loop in the cnode links.
205 I don't totally understand when it happens, it is caught
206 when closing down the system.
208 cp = make_coda_node(&ctlfid, 0, VCHR);
210 coda_ctlvp = CTOV(cp);
212 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
214 mi->mi_rootvp = rootvp;
216 /* set filesystem block size */
217 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */
219 /* Set f_iosize. XXX -- inamura@isl.ntt.co.jp.
220 For vnode_pager_haspage() references. The value should be obtained
221 from underlying UFS. */
222 /* Checked UFS. iosize is set as 8192 */
223 vfsp->mnt_stat.f_iosize = 8192;
225 /* error is currently guaranteed to be zero, but in case some
228 myprintf(("coda_mount returned %d\n",error)););
230 MARK_INT_FAIL(CODA_MOUNT_STATS);
232 MARK_INT_SAT(CODA_MOUNT_STATS);
238 coda_unmount(vfsp, mntflags, td)
243 struct coda_mntinfo *mi = vftomi(vfsp);
244 int active, error = 0;
247 MARK_ENTRY(CODA_UMOUNT_STATS);
248 if (!CODA_MOUNTED(vfsp)) {
249 MARK_INT_FAIL(CODA_UMOUNT_STATS);
253 if (mi->mi_vfsp == vfsp) { /* We found the victim */
254 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
255 return (EBUSY); /* Venus is still running */
258 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
260 vrele(mi->mi_rootvp);
262 active = coda_kill(vfsp, NOT_DOWNCALL);
263 mi->mi_rootvp->v_flag &= ~VROOT;
264 error = vflush(mi->mi_vfsp, 0, FORCECLOSE);
265 printf("coda_unmount: active = %d, vflush active %d\n", active, error);
267 /* I'm going to take this out to allow lookups to go through. I'm
268 * not sure it's important anyway. -- DCS 2/2/94
270 /* vfsp->VFS_DATA = NULL; */
272 /* No more vfsp's to hold onto */
274 mi->mi_rootvp = NULL;
277 MARK_INT_FAIL(CODA_UMOUNT_STATS);
279 MARK_INT_SAT(CODA_UMOUNT_STATS);
294 struct coda_mntinfo *mi = vftomi(vfsp);
295 struct vnode **result;
297 struct thread *td = curthread; /* XXX - bnoble */
301 KKASSERT(td->td_proc);
302 cred = td->td_proc->p_ucred;
305 MARK_ENTRY(CODA_ROOT_STATS);
308 if (vfsp == mi->mi_vfsp) {
309 if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
310 (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
311 (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
312 { /* Found valid root. */
313 *vpp = mi->mi_rootvp;
314 /* On Mach, this is vref. On NetBSD, VOP_LOCK */
317 vn_lock(*vpp, LK_EXCLUSIVE, td);
319 vget(*vpp, LK_EXCLUSIVE, td);
321 MARK_INT_SAT(CODA_ROOT_STATS);
326 error = venus_root(vftomi(vfsp), cred, td, &VFid);
330 * Save the new rootfid in the cnode, and rehash the cnode into the
331 * cnode hash with the new fid key.
333 coda_unsave(VTOC(mi->mi_rootvp));
334 VTOC(mi->mi_rootvp)->c_fid = VFid;
335 coda_save(VTOC(mi->mi_rootvp));
337 *vpp = mi->mi_rootvp;
340 vn_lock(*vpp, LK_EXCLUSIVE, td);
342 vget(*vpp, LK_EXCLUSIVE, td);
345 MARK_INT_SAT(CODA_ROOT_STATS);
347 } else if (error == ENODEV || error == EINTR) {
348 /* Gross hack here! */
350 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
351 * ENODEV. Return the uninitialized root vnode to allow vfs
352 * operations such as unmount to continue. Without this hack,
353 * there is no way to do an unmount if Venus dies before a
354 * successful CODA_ROOT call is done. All vnode operations
357 *vpp = mi->mi_rootvp;
360 vn_lock(*vpp, LK_EXCLUSIVE, td);
362 vget(*vpp, LK_EXCLUSIVE, td);
365 MARK_INT_FAIL(CODA_ROOT_STATS);
369 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
370 MARK_INT_FAIL(CODA_ROOT_STATS);
380 * Get file system statistics.
383 coda_nb_statfs(vfsp, sbp, td)
384 register struct mount *vfsp;
389 /* MARK_ENTRY(CODA_STATFS_STATS); */
390 if (!CODA_MOUNTED(vfsp)) {
391 /* MARK_INT_FAIL(CODA_STATFS_STATS);*/
395 bzero(sbp, sizeof(struct statfs));
396 /* XXX - what to do about f_flags, others? --bnoble */
397 /* Below This is what AFS does
398 #define NB_SFS_SIZ 0x895440
400 /* Note: Normal fs's have a bsize of 0x400 == 1024 */
401 sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
402 sbp->f_bsize = 8192; /* XXX */
403 sbp->f_iosize = 8192; /* XXX */
404 #define NB_SFS_SIZ 0x8AB75D
405 sbp->f_blocks = NB_SFS_SIZ;
406 sbp->f_bfree = NB_SFS_SIZ;
407 sbp->f_bavail = NB_SFS_SIZ;
408 sbp->f_files = NB_SFS_SIZ;
409 sbp->f_ffree = NB_SFS_SIZ;
410 bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
411 snprintf(sbp->f_mntonname, sizeof(sbp->f_mntonname), "/coda");
412 snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
413 /* MARK_INT_SAT(CODA_STATFS_STATS); */
418 * Flush any pending I/O.
421 coda_sync(vfsp, waitfor, cred, td)
428 MARK_ENTRY(CODA_SYNC_STATS);
429 MARK_INT_SAT(CODA_SYNC_STATS);
434 * fhtovp is now what vget used to be in 4.3-derived systems. For
435 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
436 * a type-specific fid.
439 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp)
440 register struct mount *vfsp;
445 struct ucred **creadanonp;
447 struct cfid *cfid = (struct cfid *)fhp;
448 struct cnode *cp = 0;
450 struct thread *td = curthread; /* XXX -mach */
455 KKASSERT(td->td_proc);
456 cred = td->td_proc->p_ucred;
460 MARK_ENTRY(CODA_VGET_STATS);
461 /* Check for vget of control object. */
462 if (IS_CTL_FID(&cfid->cfid_fid)) {
465 MARK_INT_SAT(CODA_VGET_STATS);
469 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, cred, td, &VFid, &vtype);
472 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
473 *vpp = (struct vnode *)0;
476 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
477 VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
479 cp = make_coda_node(&VFid, vfsp, vtype);
486 * To allow for greater ease of use, some vnodes may be orphaned when
487 * Venus dies. Certain operations should still be allowed to go
488 * through, but without propagating ophan-ness. So this function will
489 * get a new vnode for the file from the current run of Venus. */
496 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
500 cfid.cfid_len = (short)sizeof(ViceFid);
501 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */
504 /* We're guessing that if set, the 1st element on the list is a
505 * valid vnode to use. If not, return ENODEV as venus is dead.
507 if (mi->mi_vfsp == NULL)
510 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
514 #include <ufs/ufs/quota.h>
515 #include <ufs/ufs/ufsmount.h>
516 /* get the mount structure corresponding to a given device. Assume
517 * device corresponds to a UFS. Return NULL if no device is found.
519 struct mount *devtomp(dev)
524 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
525 if (((VFSTOUFS(mp))->um_dev == dev)) {
526 /* mount corresponds to UFS and the device matches one we want */
530 /* mount structure wasn't found */
534 struct vfsops coda_vfsops = {
551 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);