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