1 /* $NetBSD: coda_vfsops.c,v 1.69 2009/06/29 05:08:15 dholland 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.69 2009/06/29 05:08:15 dholland Exp $"); 49 50 #ifndef _KERNEL_OPT 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 vnode *dvp; 179 struct cnode *cp; 180 dev_t dev; 181 struct coda_mntinfo *mi; 182 struct vnode *rtvp; 183 const struct cdevsw *cdev; 184 CodaFid rootfid = INVAL_FID; 185 CodaFid ctlfid = CTL_FID; 186 int error; 187 188 if (vfsp->mnt_flag & MNT_GETARGS) 189 return EINVAL; 190 ENTRY; 191 192 coda_vfsopstats_init(); 193 coda_vnodeopstats_init(); 194 195 MARK_ENTRY(CODA_MOUNT_STATS); 196 if (CODA_MOUNTED(vfsp)) { 197 MARK_INT_FAIL(CODA_MOUNT_STATS); 198 return(EBUSY); 199 } 200 201 /* Validate mount device. Similar to getmdev(). */ 202 203 /* 204 * XXX: coda passes the mount device as the entire mount args, 205 * All other fs pass a structure contining a pointer. 206 * In order to get sys_mount() to do the copyin() we've set a 207 * fixed default size for the filename buffer. 208 */ 209 /* Ensure that namei() doesn't run off the filename buffer */ 210 ((char *)data)[*data_len - 1] = 0; 211 error = namei_simple_kernel((char *)data, NSM_FOLLOW_NOEMULROOT, 212 &dvp); 213 214 if (error) { 215 MARK_INT_FAIL(CODA_MOUNT_STATS); 216 return (error); 217 } 218 if (dvp->v_type != VCHR) { 219 MARK_INT_FAIL(CODA_MOUNT_STATS); 220 vrele(dvp); 221 return(ENXIO); 222 } 223 dev = dvp->v_rdev; 224 vrele(dvp); 225 cdev = cdevsw_lookup(dev); 226 if (cdev == NULL) { 227 MARK_INT_FAIL(CODA_MOUNT_STATS); 228 return(ENXIO); 229 } 230 231 /* 232 * See if the device table matches our expectations. 233 */ 234 if (cdev != &vcoda_cdevsw) 235 { 236 MARK_INT_FAIL(CODA_MOUNT_STATS); 237 return(ENXIO); 238 } 239 240 if (minor(dev) >= NVCODA) { 241 MARK_INT_FAIL(CODA_MOUNT_STATS); 242 return(ENXIO); 243 } 244 245 /* 246 * Initialize the mount record and link it to the vfs struct 247 */ 248 mi = &coda_mnttbl[minor(dev)]; 249 250 if (!VC_OPEN(&mi->mi_vcomm)) { 251 MARK_INT_FAIL(CODA_MOUNT_STATS); 252 return(ENODEV); 253 } 254 255 /* No initialization (here) of mi_vcomm! */ 256 vfsp->mnt_data = mi; 257 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 258 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 259 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 260 vfsp->mnt_stat.f_namemax = MAXNAMLEN; 261 mi->mi_vfsp = vfsp; 262 263 /* 264 * Make a root vnode to placate the Vnode interface, but don't 265 * actually make the CODA_ROOT call to venus until the first call 266 * to coda_root in case a server is down while venus is starting. 267 */ 268 cp = make_coda_node(&rootfid, vfsp, VDIR); 269 rtvp = CTOV(cp); 270 rtvp->v_vflag |= VV_ROOT; 271 272 /* cp = make_coda_node(&ctlfid, vfsp, VCHR); 273 The above code seems to cause a loop in the cnode links. 274 I don't totally understand when it happens, it is caught 275 when closing down the system. 276 */ 277 cp = make_coda_node(&ctlfid, 0, VCHR); 278 279 coda_ctlvp = CTOV(cp); 280 281 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 282 mi->mi_vfsp = vfsp; 283 mi->mi_rootvp = rtvp; 284 285 /* set filesystem block size */ 286 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 287 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 288 289 /* error is currently guaranteed to be zero, but in case some 290 code changes... */ 291 CODADEBUG(1, 292 myprintf(("coda_mount returned %d\n",error));); 293 if (error) 294 MARK_INT_FAIL(CODA_MOUNT_STATS); 295 else 296 MARK_INT_SAT(CODA_MOUNT_STATS); 297 298 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, 299 vfsp->mnt_op->vfs_name, vfsp, l); 300 } 301 302 int 303 coda_start(struct mount *vfsp, int flags) 304 { 305 ENTRY; 306 vftomi(vfsp)->mi_started = 1; 307 return (0); 308 } 309 310 int 311 coda_unmount(struct mount *vfsp, int mntflags) 312 { 313 struct coda_mntinfo *mi = vftomi(vfsp); 314 int active, error = 0; 315 316 ENTRY; 317 MARK_ENTRY(CODA_UMOUNT_STATS); 318 if (!CODA_MOUNTED(vfsp)) { 319 MARK_INT_FAIL(CODA_UMOUNT_STATS); 320 return(EINVAL); 321 } 322 323 if (mi->mi_vfsp == vfsp) { /* We found the victim */ 324 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp))) 325 return (EBUSY); /* Venus is still running */ 326 327 #ifdef DEBUG 328 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp)); 329 #endif 330 mi->mi_started = 0; 331 332 vrele(mi->mi_rootvp); 333 334 active = coda_kill(vfsp, NOT_DOWNCALL); 335 mi->mi_rootvp->v_vflag &= ~VV_ROOT; 336 error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE); 337 printf("coda_unmount: active = %d, vflush active %d\n", active, error); 338 error = 0; 339 340 /* I'm going to take this out to allow lookups to go through. I'm 341 * not sure it's important anyway. -- DCS 2/2/94 342 */ 343 /* vfsp->VFS_DATA = NULL; */ 344 345 /* No more vfsp's to hold onto */ 346 mi->mi_vfsp = NULL; 347 mi->mi_rootvp = NULL; 348 349 if (error) 350 MARK_INT_FAIL(CODA_UMOUNT_STATS); 351 else 352 MARK_INT_SAT(CODA_UMOUNT_STATS); 353 354 return(error); 355 } 356 return (EINVAL); 357 } 358 359 /* 360 * find root of cfs 361 */ 362 int 363 coda_root(struct mount *vfsp, struct vnode **vpp) 364 { 365 struct coda_mntinfo *mi = vftomi(vfsp); 366 int error; 367 struct lwp *l = curlwp; /* XXX - bnoble */ 368 CodaFid VFid; 369 static const CodaFid invalfid = INVAL_FID; 370 371 ENTRY; 372 MARK_ENTRY(CODA_ROOT_STATS); 373 374 if (vfsp == mi->mi_vfsp) { 375 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid))) 376 { /* Found valid root. */ 377 *vpp = mi->mi_rootvp; 378 /* On Mach, this is vref. On NetBSD, VOP_LOCK */ 379 vref(*vpp); 380 vn_lock(*vpp, LK_EXCLUSIVE); 381 MARK_INT_SAT(CODA_ROOT_STATS); 382 return(0); 383 } 384 } 385 386 error = venus_root(vftomi(vfsp), l->l_cred, l->l_proc, &VFid); 387 388 if (!error) { 389 /* 390 * Save the new rootfid in the cnode, and rehash the cnode into the 391 * cnode hash with the new fid key. 392 */ 393 coda_unsave(VTOC(mi->mi_rootvp)); 394 VTOC(mi->mi_rootvp)->c_fid = VFid; 395 coda_save(VTOC(mi->mi_rootvp)); 396 397 *vpp = mi->mi_rootvp; 398 vref(*vpp); 399 vn_lock(*vpp, LK_EXCLUSIVE); 400 MARK_INT_SAT(CODA_ROOT_STATS); 401 goto exit; 402 } else if (error == ENODEV || error == EINTR) { 403 /* Gross hack here! */ 404 /* 405 * If Venus fails to respond to the CODA_ROOT call, coda_call returns 406 * ENODEV. Return the uninitialized root vnode to allow vfs 407 * operations such as unmount to continue. Without this hack, 408 * there is no way to do an unmount if Venus dies before a 409 * successful CODA_ROOT call is done. All vnode operations 410 * will fail. 411 */ 412 *vpp = mi->mi_rootvp; 413 vref(*vpp); 414 vn_lock(*vpp, LK_EXCLUSIVE); 415 MARK_INT_FAIL(CODA_ROOT_STATS); 416 error = 0; 417 goto exit; 418 } else { 419 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); ); 420 MARK_INT_FAIL(CODA_ROOT_STATS); 421 422 goto exit; 423 } 424 exit: 425 return(error); 426 } 427 428 /* 429 * Get file system statistics. 430 */ 431 int 432 coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp) 433 { 434 struct lwp *l = curlwp; 435 struct coda_statfs fsstat; 436 int error; 437 438 ENTRY; 439 MARK_ENTRY(CODA_STATFS_STATS); 440 if (!CODA_MOUNTED(vfsp)) { 441 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 442 return(EINVAL); 443 } 444 445 /* XXX - what to do about f_flags, others? --bnoble */ 446 /* Below This is what AFS does 447 #define NB_SFS_SIZ 0x895440 448 */ 449 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 450 451 error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat); 452 453 if (!error) { 454 sbp->f_bsize = 8192; /* XXX */ 455 sbp->f_frsize = 8192; /* XXX */ 456 sbp->f_iosize = 8192; /* XXX */ 457 sbp->f_blocks = fsstat.f_blocks; 458 sbp->f_bfree = fsstat.f_bfree; 459 sbp->f_bavail = fsstat.f_bavail; 460 sbp->f_bresvd = 0; 461 sbp->f_files = fsstat.f_files; 462 sbp->f_ffree = fsstat.f_ffree; 463 sbp->f_favail = fsstat.f_ffree; 464 sbp->f_fresvd = 0; 465 copy_statvfs_info(sbp, vfsp); 466 } 467 468 MARK_INT_SAT(CODA_STATFS_STATS); 469 return(error); 470 } 471 472 /* 473 * Flush any pending I/O. 474 */ 475 int 476 coda_sync(struct mount *vfsp, int waitfor, 477 kauth_cred_t cred) 478 { 479 ENTRY; 480 MARK_ENTRY(CODA_SYNC_STATS); 481 MARK_INT_SAT(CODA_SYNC_STATS); 482 return(0); 483 } 484 485 int 486 coda_vget(struct mount *vfsp, ino_t ino, 487 struct vnode **vpp) 488 { 489 ENTRY; 490 return (EOPNOTSUPP); 491 } 492 493 /* 494 * fhtovp is now what vget used to be in 4.3-derived systems. For 495 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 496 * a type-specific fid. 497 */ 498 int 499 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam, 500 struct vnode **vpp, int *exflagsp, 501 kauth_cred_t *creadanonp) 502 { 503 struct cfid *cfid = (struct cfid *)fhp; 504 struct cnode *cp = 0; 505 int error; 506 struct lwp *l = curlwp; /* XXX -mach */ 507 CodaFid VFid; 508 int vtype; 509 510 ENTRY; 511 512 MARK_ENTRY(CODA_VGET_STATS); 513 /* Check for vget of control object. */ 514 if (IS_CTL_FID(&cfid->cfid_fid)) { 515 *vpp = coda_ctlvp; 516 vref(coda_ctlvp); 517 MARK_INT_SAT(CODA_VGET_STATS); 518 return(0); 519 } 520 521 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype); 522 523 if (error) { 524 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 525 *vpp = (struct vnode *)0; 526 } else { 527 CODADEBUG(CODA_VGET, 528 myprintf(("vget: %s type %d result %d\n", 529 coda_f2s(&VFid), vtype, error)); ) 530 531 cp = make_coda_node(&VFid, vfsp, vtype); 532 *vpp = CTOV(cp); 533 } 534 return(error); 535 } 536 537 int 538 coda_vptofh(struct vnode *vnp, struct fid *fidp) 539 { 540 ENTRY; 541 return (EOPNOTSUPP); 542 } 543 544 void 545 coda_init(void) 546 { 547 ENTRY; 548 } 549 550 void 551 coda_done(void) 552 { 553 ENTRY; 554 } 555 556 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 557 { 558 sysctl_createv(clog, 0, NULL, NULL, 559 CTLFLAG_PERMANENT, 560 CTLTYPE_NODE, "vfs", NULL, 561 NULL, 0, NULL, 0, 562 CTL_VFS, CTL_EOL); 563 sysctl_createv(clog, 0, NULL, NULL, 564 CTLFLAG_PERMANENT, 565 CTLTYPE_NODE, "coda", 566 SYSCTL_DESCR("code vfs options"), 567 NULL, 0, NULL, 0, 568 CTL_VFS, 18, CTL_EOL); 569 /* 570 * XXX the "18" above could be dynamic, thereby eliminating 571 * one more instance of the "number to vfs" mapping problem, 572 * but "18" is the order as taken from sys/mount.h 573 */ 574 575 /* 576 sysctl_createv(clog, 0, NULL, NULL, 577 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 578 CTLTYPE_INT, "clusterread", 579 SYSCTL_DESCR( anyone? ), 580 NULL, 0, &doclusterread, 0, 581 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 582 */ 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 orphan-ness. So this function will 589 * get a new vnode for the file from the current run of Venus. 590 */ 591 592 int 593 getNewVnode(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(CodaFid); 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_t dev) 620 { 621 struct mount *mp, *nmp; 622 623 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 624 nmp = mp->mnt_list.cqe_next; 625 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 626 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 627 /* mount corresponds to UFS and the device matches one we want */ 628 return(mp); 629 } 630 } 631 /* mount structure wasn't found */ 632 return(NULL); 633 } 634