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