2 * Coda: an Experimental Distributed File System
5 * Copyright (c) 1987-1998 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation, and
13 * that credit is given to Carnegie Mellon University in all documents
14 * and publicity pertaining to direct or indirect use of this code or its
17 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
18 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
19 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
20 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
22 * ANY DERIVATIVE WORK.
24 * Carnegie Mellon encourages users of this software to return any
25 * improvements or extensions that they make, and to grant Carnegie
26 * Mellon the rights to redistribute these changes without encumbrance.
28 * @(#) src/sys/cfs/coda_vfsops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
32 * Mach Operating System
33 * Copyright (c) 1989 Carnegie-Mellon University
34 * All rights reserved. The CMU software License Agreement specifies
35 * the terms and conditions for use and redistribution.
39 * This code was written for the Coda filesystem at Carnegie Mellon
40 * University. Contributers include David Steere, James Kistler, and
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
58 #include <fs/coda/coda.h>
59 #include <fs/coda/cnode.h>
60 #include <fs/coda/coda_vfsops.h>
61 #include <fs/coda/coda_venus.h>
62 #include <fs/coda/coda_subr.h>
63 #include <fs/coda/coda_opstats.h>
65 MALLOC_DEFINE(M_CODA, "coda", "Various Coda Structures");
68 int coda_vfsop_print_entry = 0;
70 if (coda_vfsop_print_entry) \
71 myprintf(("Entered %s\n", __func__)); \
74 struct vnode *coda_ctlvp;
77 * Structure to keep statistics of internally generated/satisfied calls.
79 static struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
81 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
82 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
83 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
84 #define MARK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
87 coda_vfsopstats_init(void)
91 for (i=0; i<CODA_VFSOPS_SIZE;i++) {
92 coda_vfsopstats[i].opcode = i;
93 coda_vfsopstats[i].entries = 0;
94 coda_vfsopstats[i].sat_intrn = 0;
95 coda_vfsopstats[i].unsat_intrn = 0;
96 coda_vfsopstats[i].gen_intrn = 0;
101 static const char *coda_opts[] = { "from", NULL };
105 * Set up mount info record and attach it to vfs struct.
109 coda_mount(struct mount *vfsp)
114 struct coda_mntinfo *mi;
115 struct vnode *rootvp;
116 CodaFid rootfid = INVAL_FID;
117 CodaFid ctlfid = CTL_FID;
119 struct nameidata ndp;
123 if (vfs_filteropt(vfsp->mnt_optnew, coda_opts))
125 from = vfs_getopts(vfsp->mnt_optnew, "from", &error);
128 coda_vfsopstats_init();
129 coda_vnodeopstats_init();
130 MARK_ENTRY(CODA_MOUNT_STATS);
131 if (CODA_MOUNTED(vfsp)) {
132 MARK_INT_FAIL(CODA_MOUNT_STATS);
137 * Validate mount device. Similar to getmdev().
139 NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, from, curthread);
143 MARK_INT_FAIL(CODA_MOUNT_STATS);
146 if (dvp->v_type != VCHR) {
147 MARK_INT_FAIL(CODA_MOUNT_STATS);
149 NDFREE(&ndp, NDF_ONLY_PNBUF);
154 NDFREE(&ndp, NDF_ONLY_PNBUF);
157 * Initialize the mount record and link it to the vfs struct.
159 mi = dev2coda_mntinfo(dev);
161 MARK_INT_FAIL(CODA_MOUNT_STATS);
162 printf("Coda mount: %s is not a cfs device\n", from);
165 if (!VC_OPEN(&mi->mi_vcomm)) {
166 MARK_INT_FAIL(CODA_MOUNT_STATS);
171 * No initialization (here) of mi_vcomm!
174 vfs_getnewfsid (vfsp);
176 mi->mi_started = 0; /* XXX See coda_root() */
179 * Make a root vnode to placate the Vnode interface, but don't
180 * actually make the CODA_ROOT call to venus until the first call to
181 * coda_root in case a server is down while venus is starting.
183 cp = make_coda_node(&rootfid, vfsp, VDIR);
185 rootvp->v_vflag |= VV_ROOT;
186 cp = make_coda_node(&ctlfid, vfsp, VREG);
187 coda_ctlvp = CTOV(cp);
190 * Add vfs and rootvp to chain of vfs hanging off mntinfo.
193 mi->mi_rootvp = rootvp;
194 vfs_mountedfrom(vfsp, from);
197 * Error is currently guaranteed to be zero, but in case some code
200 CODADEBUG(1, myprintf(("coda_mount returned %d\n", error)););
202 MARK_INT_FAIL(CODA_MOUNT_STATS);
204 MARK_INT_SAT(CODA_MOUNT_STATS);
209 coda_unmount(struct mount *vfsp, int mntflags)
211 struct coda_mntinfo *mi = vftomi(vfsp);
212 int active, error = 0;
215 MARK_ENTRY(CODA_UMOUNT_STATS);
216 if (!CODA_MOUNTED(vfsp)) {
217 MARK_INT_FAIL(CODA_UMOUNT_STATS);
220 if (mi->mi_vfsp == vfsp) {
222 * We found the victim.
224 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
225 return (EBUSY); /* Venus is still running */
227 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp,
228 VTOC(mi->mi_rootvp));
230 vrele(mi->mi_rootvp);
231 mi->mi_rootvp = NULL;
234 active = coda_kill(vfsp, NOT_DOWNCALL);
235 error = vflush(mi->mi_vfsp, 0, FORCECLOSE, curthread);
237 printf("coda_unmount: active = %d, vflush active %d\n",
242 * I'm going to take this out to allow lookups to go through.
243 * I'm not sure it's important anyway. -- DCS 2/2/94
245 /* vfsp->VFS_DATA = NULL; */
248 * No more vfsp's to hold onto.
253 MARK_INT_FAIL(CODA_UMOUNT_STATS);
255 MARK_INT_SAT(CODA_UMOUNT_STATS);
265 coda_root(struct mount *vfsp, int flags, struct vnode **vpp)
267 struct coda_mntinfo *mi = vftomi(vfsp);
272 static const CodaFid invalfid = INVAL_FID;
277 MARK_ENTRY(CODA_ROOT_STATS);
278 if (vfsp == mi->mi_vfsp) {
280 * Cache the root across calls. We only need to pass the
281 * request on to Venus if the root vnode is the dummy we
282 * installed in coda_mount() with all c_fid members zeroed.
284 * XXX In addition, we assume that the first call to
285 * coda_root() is from vfs_mount() (before the call to
286 * checkdirs()) and return the dummy root node to avoid a
287 * deadlock. This bug is fixed in the Coda CVS repository
288 * but not in any released versions as of 6 Mar 2003.
290 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid,
291 sizeof(CodaFid)) != 0 || mi->mi_started == 0) {
295 *vpp = mi->mi_rootvp;
299 * On Mach, this is vref. On FreeBSD, vref +
303 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
304 MARK_INT_SAT(CODA_ROOT_STATS);
309 error = venus_root(vftomi(vfsp), td->td_ucred, p, &VFid);
312 * Save the new rootfid in the cnode, and rehash the cnode
313 * into the cnode hash with the new fid key.
315 coda_unsave(VTOC(mi->mi_rootvp));
316 VTOC(mi->mi_rootvp)->c_fid = VFid;
317 coda_save(VTOC(mi->mi_rootvp));
318 *vpp = mi->mi_rootvp;
320 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
321 MARK_INT_SAT(CODA_ROOT_STATS);
322 } else if (error == ENODEV || error == EINTR) {
326 * If Venus fails to respond to the CODA_ROOT call, coda_call
327 * returns ENODEV. Return the uninitialized root vnode to
328 * allow vfs operations such as unmount to continue. Without
329 * this hack, there is no way to do an unmount if Venus dies
330 * before a successful CODA_ROOT call is done. All vnode
331 * operations will fail.
333 *vpp = mi->mi_rootvp;
335 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
336 MARK_INT_FAIL(CODA_ROOT_STATS);
339 CODADEBUG(CODA_ROOT, myprintf(("error %d in CODA_ROOT\n",
341 MARK_INT_FAIL(CODA_ROOT_STATS);
347 * Get filesystem statistics.
350 coda_statfs(struct mount *vfsp, struct statfs *sbp)
354 MARK_ENTRY(CODA_STATFS_STATS);
355 if (!CODA_MOUNTED(vfsp)) {
356 MARK_INT_FAIL(CODA_STATFS_STATS);
361 * XXX - what to do about f_flags, others? --bnoble
363 * We just make up free space counts that are sufficiently large.
366 sbp->f_bsize = 8192; /* XXX */
367 sbp->f_iosize = 8192; /* XXX */
368 #define CODA_SFS_SIZ 0x8AB75D
369 sbp->f_blocks = CODA_SFS_SIZ;
370 sbp->f_bfree = CODA_SFS_SIZ;
371 sbp->f_bavail = CODA_SFS_SIZ;
372 sbp->f_files = CODA_SFS_SIZ;
373 sbp->f_ffree = CODA_SFS_SIZ;
374 MARK_INT_SAT(CODA_STATFS_STATS);
379 * Flush any pending I/O.
382 coda_sync(struct mount *vfsp, int waitfor)
386 MARK_ENTRY(CODA_SYNC_STATS);
387 MARK_INT_SAT(CODA_SYNC_STATS);
392 * fhtovp is now what vget used to be in 4.3-derived systems. For some silly
393 * reason, vget is now keyed by a 32 bit ino_t, rather than a type-specific
396 * XXX: coda_fhtovp is currently not hooked up, so no NFS export for Coda.
397 * We leave it here in the hopes that someone will find it someday and hook
398 * it up. Among other things, it will need some reworking to match the
399 * vfs_fhtovp_t prototype.
402 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
403 struct vnode **vpp, int *exflagsp, struct ucred **creadanonp)
405 struct cfid *cfid = (struct cfid *)fhp;
406 struct cnode *cp = NULL;
408 struct thread *td = curthread; /* XXX -mach */
409 struct proc *p = td->td_proc;
414 MARK_ENTRY(CODA_VGET_STATS);
417 * Check for vget of control object.
419 if (IS_CTL_FID(&cfid->cfid_fid)) {
422 MARK_INT_SAT(CODA_VGET_STATS);
425 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, td->td_ucred, p,
428 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error)););
431 CODADEBUG(CODA_VGET, myprintf(("vget: %s type %d result "
432 "%d\n", coda_f2s(&VFid), vtype, error)););
433 cp = make_coda_node(&VFid, vfsp, vtype);
439 struct vfsops coda_vfsops = {
440 .vfs_mount = coda_mount,
441 .vfs_root = coda_root,
442 .vfs_statfs = coda_statfs,
443 .vfs_sync = coda_sync,
444 .vfs_unmount = coda_unmount,
446 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);