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