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