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/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
57 #include <fs/coda/coda.h>
58 #include <fs/coda/cnode.h>
59 #include <fs/coda/coda_vfsops.h>
60 #include <fs/coda/coda_venus.h>
61 #include <fs/coda/coda_subr.h>
62 #include <fs/coda/coda_opstats.h>
64 MALLOC_DEFINE(M_CODA, "coda", "Various Coda Structures");
67 int coda_vfsop_print_entry = 0;
69 if (coda_vfsop_print_entry) \
70 myprintf(("Entered %s\n", __func__)); \
73 struct vnode *coda_ctlvp;
76 * Structure to keep statistics of internally generated/satisfied calls.
78 static struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE];
80 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++)
81 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++)
82 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++)
83 #define MARK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++)
86 coda_vfsopstats_init(void)
90 for (i=0; i<CODA_VFSOPS_SIZE;i++) {
91 coda_vfsopstats[i].opcode = i;
92 coda_vfsopstats[i].entries = 0;
93 coda_vfsopstats[i].sat_intrn = 0;
94 coda_vfsopstats[i].unsat_intrn = 0;
95 coda_vfsopstats[i].gen_intrn = 0;
100 static const char *coda_opts[] = { "from", NULL };
104 * Set up mount info record and attach it to vfs struct.
108 coda_mount(struct mount *vfsp, struct thread *td)
113 struct coda_mntinfo *mi;
114 struct vnode *rootvp;
115 CodaFid rootfid = INVAL_FID;
116 CodaFid ctlfid = CTL_FID;
118 struct nameidata ndp;
122 if (vfs_filteropt(vfsp->mnt_optnew, coda_opts))
124 from = vfs_getopts(vfsp->mnt_optnew, "from", &error);
127 coda_vfsopstats_init();
128 coda_vnodeopstats_init();
129 MARK_ENTRY(CODA_MOUNT_STATS);
130 if (CODA_MOUNTED(vfsp)) {
131 MARK_INT_FAIL(CODA_MOUNT_STATS);
136 * Validate mount device. Similar to getmdev().
138 NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, from, td);
142 MARK_INT_FAIL(CODA_MOUNT_STATS);
145 if (dvp->v_type != VCHR) {
146 MARK_INT_FAIL(CODA_MOUNT_STATS);
148 NDFREE(&ndp, NDF_ONLY_PNBUF);
153 NDFREE(&ndp, NDF_ONLY_PNBUF);
156 * Initialize the mount record and link it to the vfs struct.
158 mi = dev2coda_mntinfo(dev);
160 MARK_INT_FAIL(CODA_MOUNT_STATS);
161 printf("Coda mount: %s is not a cfs device\n", from);
164 if (!VC_OPEN(&mi->mi_vcomm)) {
165 MARK_INT_FAIL(CODA_MOUNT_STATS);
170 * No initialization (here) of mi_vcomm!
173 vfs_getnewfsid (vfsp);
175 mi->mi_started = 0; /* XXX See coda_root() */
178 * Make a root vnode to placate the Vnode interface, but don't
179 * actually make the CODA_ROOT call to venus until the first call to
180 * coda_root in case a server is down while venus is starting.
182 cp = make_coda_node(&rootfid, vfsp, VDIR);
184 rootvp->v_vflag |= VV_ROOT;
185 cp = make_coda_node(&ctlfid, vfsp, VREG);
186 coda_ctlvp = CTOV(cp);
189 * Add vfs and rootvp to chain of vfs hanging off mntinfo.
192 mi->mi_rootvp = rootvp;
193 vfs_mountedfrom(vfsp, from);
196 * Error is currently guaranteed to be zero, but in case some code
199 CODADEBUG(1, myprintf(("coda_mount returned %d\n", error)););
201 MARK_INT_FAIL(CODA_MOUNT_STATS);
203 MARK_INT_SAT(CODA_MOUNT_STATS);
208 coda_unmount(struct mount *vfsp, int mntflags, struct thread *td)
210 struct coda_mntinfo *mi = vftomi(vfsp);
211 int active, error = 0;
214 MARK_ENTRY(CODA_UMOUNT_STATS);
215 if (!CODA_MOUNTED(vfsp)) {
216 MARK_INT_FAIL(CODA_UMOUNT_STATS);
219 if (mi->mi_vfsp == vfsp) {
221 * We found the victim.
223 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp)))
224 return (EBUSY); /* Venus is still running */
226 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp,
227 VTOC(mi->mi_rootvp));
229 vrele(mi->mi_rootvp);
230 mi->mi_rootvp = NULL;
233 active = coda_kill(vfsp, NOT_DOWNCALL);
234 error = vflush(mi->mi_vfsp, 0, FORCECLOSE, td);
236 printf("coda_unmount: active = %d, vflush active %d\n",
241 * I'm going to take this out to allow lookups to go through.
242 * I'm not sure it's important anyway. -- DCS 2/2/94
244 /* vfsp->VFS_DATA = NULL; */
247 * No more vfsp's to hold onto.
252 MARK_INT_FAIL(CODA_UMOUNT_STATS);
254 MARK_INT_SAT(CODA_UMOUNT_STATS);
264 coda_root(struct mount *vfsp, int flags, struct vnode **vpp,
267 struct coda_mntinfo *mi = vftomi(vfsp);
268 struct vnode **result;
270 struct proc *p = td->td_proc;
272 static const CodaFid invalfid = INVAL_FID;
275 MARK_ENTRY(CODA_ROOT_STATS);
277 if (vfsp == mi->mi_vfsp) {
279 * Cache the root across calls. We only need to pass the
280 * request on to Venus if the root vnode is the dummy we
281 * installed in coda_mount() with all c_fid members zeroed.
283 * XXX In addition, we assume that the first call to
284 * coda_root() is from vfs_mount() (before the call to
285 * checkdirs()) and return the dummy root node to avoid a
286 * deadlock. This bug is fixed in the Coda CVS repository
287 * but not in any released versions as of 6 Mar 2003.
289 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid,
290 sizeof(CodaFid)) != 0 || mi->mi_started == 0) {
294 *vpp = mi->mi_rootvp;
298 * On Mach, this is vref. On FreeBSD, vref +
302 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
303 MARK_INT_SAT(CODA_ROOT_STATS);
308 error = venus_root(vftomi(vfsp), td->td_ucred, p, &VFid);
311 * Save the new rootfid in the cnode, and rehash the cnode
312 * into the cnode hash with the new fid key.
314 coda_unsave(VTOC(mi->mi_rootvp));
315 VTOC(mi->mi_rootvp)->c_fid = VFid;
316 coda_save(VTOC(mi->mi_rootvp));
317 *vpp = mi->mi_rootvp;
319 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
320 MARK_INT_SAT(CODA_ROOT_STATS);
321 } else if (error == ENODEV || error == EINTR) {
325 * If Venus fails to respond to the CODA_ROOT call, coda_call
326 * returns ENODEV. Return the uninitialized root vnode to
327 * allow vfs operations such as unmount to continue. Without
328 * this hack, there is no way to do an unmount if Venus dies
329 * before a successful CODA_ROOT call is done. All vnode
330 * operations will fail.
332 *vpp = mi->mi_rootvp;
334 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
335 MARK_INT_FAIL(CODA_ROOT_STATS);
338 CODADEBUG(CODA_ROOT, myprintf(("error %d in CODA_ROOT\n",
340 MARK_INT_FAIL(CODA_ROOT_STATS);
346 * Get filesystem statistics.
349 coda_statfs(struct mount *vfsp, struct statfs *sbp, struct thread *td)
353 MARK_ENTRY(CODA_STATFS_STATS);
354 if (!CODA_MOUNTED(vfsp)) {
355 MARK_INT_FAIL(CODA_STATFS_STATS);
360 * XXX - what to do about f_flags, others? --bnoble
362 * We just make up free space counts that are sufficiently large.
365 sbp->f_bsize = 8192; /* XXX */
366 sbp->f_iosize = 8192; /* XXX */
367 #define CODA_SFS_SIZ 0x8AB75D
368 sbp->f_blocks = CODA_SFS_SIZ;
369 sbp->f_bfree = CODA_SFS_SIZ;
370 sbp->f_bavail = CODA_SFS_SIZ;
371 sbp->f_files = CODA_SFS_SIZ;
372 sbp->f_ffree = CODA_SFS_SIZ;
373 MARK_INT_SAT(CODA_STATFS_STATS);
378 * Flush any pending I/O.
381 coda_sync(struct mount *vfsp, int waitfor, struct thread *td)
385 MARK_ENTRY(CODA_SYNC_STATS);
386 MARK_INT_SAT(CODA_SYNC_STATS);
391 * fhtovp is now what vget used to be in 4.3-derived systems. For some silly
392 * reason, vget is now keyed by a 32 bit ino_t, rather than a type-specific
395 * XXX: coda_fhtovp is currently not hooked up, so no NFS export for Coda.
396 * We leave it here in the hopes that someone will find it someday and hook
397 * it up. Among other things, it will need some reworking to match the
398 * vfs_fhtovp_t prototype.
401 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam,
402 struct vnode **vpp, int *exflagsp, struct ucred **creadanonp)
404 struct cfid *cfid = (struct cfid *)fhp;
405 struct cnode *cp = NULL;
407 struct thread *td = curthread; /* XXX -mach */
408 struct proc *p = td->td_proc;
413 MARK_ENTRY(CODA_VGET_STATS);
416 * Check for vget of control object.
418 if (IS_CTL_FID(&cfid->cfid_fid)) {
421 MARK_INT_SAT(CODA_VGET_STATS);
424 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, td->td_ucred, p,
427 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error)););
430 CODADEBUG(CODA_VGET, myprintf(("vget: %s type %d result "
431 "%d\n", coda_f2s(&VFid), vtype, error)););
432 cp = make_coda_node(&VFid, vfsp, vtype);
438 struct vfsops coda_vfsops = {
439 .vfs_mount = coda_mount,
440 .vfs_root = coda_root,
441 .vfs_statfs = coda_statfs,
442 .vfs_sync = coda_sync,
443 .vfs_unmount = coda_unmount,
445 VFS_SET(coda_vfsops, coda, VFCF_NETWORK);