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.15 2004/05/26 19:11:08 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
48 #include "use_vcoda.h"
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>
64 #include "coda_vfsops.h"
65 #include "coda_venus.h"
66 #include "coda_subr.h"
67 #include "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 (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(struct mount *vfsp, /* Allocated and initialized by mount(2) */
113 char *path, /* path covered: ignored by the fs-layer */
114 caddr_t data, /* Need to define a data type for this in
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, NAMEI_LOOKUP, CNP_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);
158 #if 0 /* YYY huh? what paranoia is this? */
160 * See if the device table matches our expectations.
162 if (devsw(dev)->d_open != vc_nb_open)
164 MARK_INT_FAIL(CODA_MOUNT_STATS);
169 if (uminor(udev) >= NVCODA || uminor(udev) < 0) {
170 MARK_INT_FAIL(CODA_MOUNT_STATS);
175 * Initialize the mount record and link it to the vfs struct
177 mi = &coda_mnttbl[uminor(udev)];
179 if (!VC_OPEN(&mi->mi_vcomm)) {
180 MARK_INT_FAIL(CODA_MOUNT_STATS);
184 /* No initialization (here) of mi_vcomm! */
185 vfsp->mnt_data = (qaddr_t)mi;
186 vfs_getnewfsid (vfsp);
191 * Make a root vnode to placate the Vnode interface, but don't
192 * actually make the CODA_ROOT call to venus until the first call
193 * to coda_root in case a server is down while venus is starting.
198 cp = make_coda_node(&rootfid, vfsp, VDIR);
200 rootvp->v_flag |= VROOT;
202 ctlfid.Volume = CTL_VOL;
203 ctlfid.Vnode = CTL_VNO;
204 ctlfid.Unique = CTL_UNI;
205 /* cp = make_coda_node(&ctlfid, vfsp, VCHR);
206 The above code seems to cause a loop in the cnode links.
207 I don't totally understand when it happens, it is caught
208 when closing down the system.
210 cp = make_coda_node(&ctlfid, 0, VCHR);
212 coda_ctlvp = CTOV(cp);
214 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */
216 mi->mi_rootvp = rootvp;
218 /* set filesystem block size */
219 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */
221 /* Set f_iosize. XXX -- inamura@isl.ntt.co.jp.
222 For vnode_pager_haspage() references. The value should be obtained
223 from underlying UFS. */
224 /* Checked UFS. iosize is set as 8192 */
225 vfsp->mnt_stat.f_iosize = 8192;
227 /* error is currently guaranteed to be zero, but in case some
230 myprintf(("coda_mount returned %d\n",error)););
232 MARK_INT_FAIL(CODA_MOUNT_STATS);
234 MARK_INT_SAT(CODA_MOUNT_STATS);
240 coda_unmount(struct mount *vfsp, int mntflags, struct thread *td)
242 struct coda_mntinfo *mi = vftomi(vfsp);
243 int active, error = 0;
246 MARK_ENTRY(CODA_UMOUNT_STATS);
247 if (!CODA_MOUNTED(vfsp)) {
248 MARK_INT_FAIL(CODA_UMOUNT_STATS);
252 if (mi->mi_vfsp == vfsp) { /* We found the victim */
253 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
254 return (EBUSY); /* Venus is still running */
257 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp));
259 vrele(mi->mi_rootvp);
261 active = coda_kill(vfsp, NOT_DOWNCALL);
262 mi->mi_rootvp->v_flag &= ~VROOT;
263 error = vflush(mi->mi_vfsp, 0, FORCECLOSE);
264 printf("coda_unmount: active = %d, vflush active %d\n", active, error);
266 /* I'm going to take this out to allow lookups to go through. I'm
267 * not sure it's important anyway. -- DCS 2/2/94
269 /* vfsp->VFS_DATA = NULL; */
271 /* No more vfsp's to hold onto */
273 mi->mi_rootvp = NULL;
276 MARK_INT_FAIL(CODA_UMOUNT_STATS);
278 MARK_INT_SAT(CODA_UMOUNT_STATS);
289 coda_root(struct mount *vfsp, struct vnode **vpp)
291 struct coda_mntinfo *mi = vftomi(vfsp);
292 struct vnode **result;
294 struct thread *td = curthread; /* XXX - bnoble */
298 KKASSERT(td->td_proc);
299 cred = td->td_proc->p_ucred;
302 MARK_ENTRY(CODA_ROOT_STATS);
305 if (vfsp == mi->mi_vfsp) {
306 if ((VTOC(mi->mi_rootvp)->c_fid.Volume != 0) ||
307 (VTOC(mi->mi_rootvp)->c_fid.Vnode != 0) ||
308 (VTOC(mi->mi_rootvp)->c_fid.Unique != 0))
309 { /* Found valid root. */
310 *vpp = mi->mi_rootvp;
311 /* On Mach, this is vref. On NetBSD, VOP_LOCK */
314 vn_lock(*vpp, NULL, LK_EXCLUSIVE, td);
316 vget(*vpp, NULL, LK_EXCLUSIVE, td);
318 MARK_INT_SAT(CODA_ROOT_STATS);
323 error = venus_root(vftomi(vfsp), cred, td, &VFid);
327 * Save the new rootfid in the cnode, and rehash the cnode into the
328 * cnode hash with the new fid key.
330 coda_unsave(VTOC(mi->mi_rootvp));
331 VTOC(mi->mi_rootvp)->c_fid = VFid;
332 coda_save(VTOC(mi->mi_rootvp));
334 *vpp = mi->mi_rootvp;
337 vn_lock(*vpp, NULL, LK_EXCLUSIVE, td);
339 vget(*vpp, NULL, LK_EXCLUSIVE, td);
342 MARK_INT_SAT(CODA_ROOT_STATS);
344 } else if (error == ENODEV || error == EINTR) {
345 /* Gross hack here! */
347 * If Venus fails to respond to the CODA_ROOT call, coda_call returns
348 * ENODEV. Return the uninitialized root vnode to allow vfs
349 * operations such as unmount to continue. Without this hack,
350 * there is no way to do an unmount if Venus dies before a
351 * successful CODA_ROOT call is done. All vnode operations
354 *vpp = mi->mi_rootvp;
357 vn_lock(*vpp, NULL, LK_EXCLUSIVE, td);
359 vget(*vpp, NULL, LK_EXCLUSIVE, td);
362 MARK_INT_FAIL(CODA_ROOT_STATS);
366 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); );
367 MARK_INT_FAIL(CODA_ROOT_STATS);
377 * Get file system statistics.
380 coda_nb_statfs(struct mount *vfsp, struct statfs *sbp, struct thread *td)
383 /* MARK_ENTRY(CODA_STATFS_STATS); */
384 if (!CODA_MOUNTED(vfsp)) {
385 /* MARK_INT_FAIL(CODA_STATFS_STATS);*/
389 bzero(sbp, sizeof(struct statfs));
390 /* XXX - what to do about f_flags, others? --bnoble */
391 /* Below This is what AFS does
392 #define NB_SFS_SIZ 0x895440
394 /* Note: Normal fs's have a bsize of 0x400 == 1024 */
395 sbp->f_type = vfsp->mnt_vfc->vfc_typenum;
396 sbp->f_bsize = 8192; /* XXX */
397 sbp->f_iosize = 8192; /* XXX */
398 #define NB_SFS_SIZ 0x8AB75D
399 sbp->f_blocks = NB_SFS_SIZ;
400 sbp->f_bfree = NB_SFS_SIZ;
401 sbp->f_bavail = NB_SFS_SIZ;
402 sbp->f_files = NB_SFS_SIZ;
403 sbp->f_ffree = NB_SFS_SIZ;
404 bcopy((caddr_t)&(vfsp->mnt_stat.f_fsid), (caddr_t)&(sbp->f_fsid), sizeof (fsid_t));
405 snprintf(sbp->f_mntonname, sizeof(sbp->f_mntonname), "/coda");
406 snprintf(sbp->f_mntfromname, sizeof(sbp->f_mntfromname), "CODA");
407 /* MARK_INT_SAT(CODA_STATFS_STATS); */
412 * Flush any pending I/O.
415 coda_sync(struct mount *vfsp, int waitfor, struct thread *td)
418 MARK_ENTRY(CODA_SYNC_STATS);
419 MARK_INT_SAT(CODA_SYNC_STATS);
424 * fhtovp is now what vget used to be in 4.3-derived systems. For
425 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than
426 * a type-specific fid.
429 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
430 struct vnode **vpp, int *exflagsp, struct ucred **creadanonp)
432 struct cfid *cfid = (struct cfid *)fhp;
433 struct cnode *cp = 0;
435 struct thread *td = curthread; /* XXX -mach */
440 KKASSERT(td->td_proc);
441 cred = td->td_proc->p_ucred;
445 MARK_ENTRY(CODA_VGET_STATS);
446 /* Check for vget of control object. */
447 if (IS_CTL_FID(&cfid->cfid_fid)) {
450 MARK_INT_SAT(CODA_VGET_STATS);
454 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, cred, td, &VFid, &vtype);
457 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));)
458 *vpp = (struct vnode *)0;
461 myprintf(("vget: vol %lx vno %lx uni %lx type %d result %d\n",
462 VFid.Volume, VFid.Vnode, VFid.Unique, vtype, error)); )
464 cp = make_coda_node(&VFid, vfsp, vtype);
471 * To allow for greater ease of use, some vnodes may be orphaned when
472 * Venus dies. Certain operations should still be allowed to go
473 * through, but without propagating orphan-ness. So this function will
474 * get a new vnode for the file from the current run of Venus.
478 getNewVnode(struct vnode **vpp)
481 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount);
485 cfid.cfid_len = (short)sizeof(ViceFid);
486 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */
489 /* We're guessing that if set, the 1st element on the list is a
490 * valid vnode to use. If not, return ENODEV as venus is dead.
492 if (mi->mi_vfsp == NULL)
495 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp,
499 #include <vfs/ufs/quota.h>
500 #include <vfs/ufs/ufsmount.h>
501 /* get the mount structure corresponding to a given device. Assume
502 * device corresponds to a UFS. Return NULL if no device is found.
504 struct mount *devtomp(dev_t dev)
508 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
509 if (((VFSTOUFS(mp))->um_dev == dev)) {
510 /* mount corresponds to UFS and the device matches one we want */
514 /* mount structure wasn't found */
518 struct vfsops coda_vfsops = {
535 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);