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