1 /* $NetBSD: coda_vfsops.c,v 1.40 2004/10/15 09:09:09 skrll 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.40 2004/10/15 09:09:09 skrll 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 67 #include <coda/coda.h> 68 #include <coda/cnode.h> 69 #include <coda/coda_vfsops.h> 70 #include <coda/coda_venus.h> 71 #include <coda/coda_subr.h> 72 #include <coda/coda_opstats.h> 73 /* for VN_RDEV */ 74 #include <miscfs/specfs/specdev.h> 75 76 MALLOC_DEFINE(M_CODA, "coda", "Coda file system structures and tables"); 77 78 int codadebug = 0; 79 80 int coda_vfsop_print_entry = 0; 81 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__)) 82 83 struct vnode *coda_ctlvp; 84 struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */ 85 86 /* structure to keep statistics of internally generated/satisfied calls */ 87 88 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE]; 89 90 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++) 91 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++) 92 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++) 93 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++) 94 95 extern const struct cdevsw vcoda_cdevsw; 96 extern const struct vnodeopv_desc coda_vnodeop_opv_desc; 97 98 const struct vnodeopv_desc * const coda_vnodeopv_descs[] = { 99 &coda_vnodeop_opv_desc, 100 NULL, 101 }; 102 103 struct vfsops coda_vfsops = { 104 MOUNT_CODA, 105 coda_mount, 106 coda_start, 107 coda_unmount, 108 coda_root, 109 coda_quotactl, 110 coda_nb_statvfs, 111 coda_sync, 112 coda_vget, 113 (int (*) (struct mount *, struct fid *, struct vnode ** )) 114 eopnotsupp, 115 (int (*) (struct vnode *, struct fid *)) eopnotsupp, 116 coda_init, 117 NULL, 118 coda_done, 119 NULL, 120 (int (*)(void)) eopnotsupp, 121 (int (*)(struct mount *, struct mbuf *, int *, struct ucred **)) 122 eopnotsupp, 123 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 124 coda_vnodeopv_descs, 125 0 126 }; 127 128 129 int 130 coda_vfsopstats_init(void) 131 { 132 int i; 133 134 for (i=0;i<CODA_VFSOPS_SIZE;i++) { 135 coda_vfsopstats[i].opcode = i; 136 coda_vfsopstats[i].entries = 0; 137 coda_vfsopstats[i].sat_intrn = 0; 138 coda_vfsopstats[i].unsat_intrn = 0; 139 coda_vfsopstats[i].gen_intrn = 0; 140 } 141 142 return 0; 143 } 144 145 /* 146 * cfs mount vfsop 147 * Set up mount info record and attach it to vfs struct. 148 */ 149 /*ARGSUSED*/ 150 int 151 coda_mount(vfsp, path, data, ndp, p) 152 struct mount *vfsp; /* Allocated and initialized by mount(2) */ 153 const char *path; /* path covered: ignored by the fs-layer */ 154 void *data; /* Need to define a data type for this in netbsd? */ 155 struct nameidata *ndp; /* Clobber this to lookup the device name */ 156 struct proc *p; /* The ever-famous proc pointer */ 157 { 158 struct vnode *dvp; 159 struct cnode *cp; 160 dev_t dev; 161 struct coda_mntinfo *mi; 162 struct vnode *rootvp; 163 const struct cdevsw *cdev; 164 CodaFid rootfid = INVAL_FID; 165 CodaFid ctlfid = CTL_FID; 166 int error; 167 168 if (vfsp->mnt_flag & MNT_GETARGS) 169 return 0; 170 ENTRY; 171 172 coda_vfsopstats_init(); 173 coda_vnodeopstats_init(); 174 175 MARK_ENTRY(CODA_MOUNT_STATS); 176 if (CODA_MOUNTED(vfsp)) { 177 MARK_INT_FAIL(CODA_MOUNT_STATS); 178 return(EBUSY); 179 } 180 181 /* Validate mount device. Similar to getmdev(). */ 182 183 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p); 184 error = namei(ndp); 185 dvp = ndp->ni_vp; 186 187 if (error) { 188 MARK_INT_FAIL(CODA_MOUNT_STATS); 189 return (error); 190 } 191 if (dvp->v_type != VCHR) { 192 MARK_INT_FAIL(CODA_MOUNT_STATS); 193 vrele(dvp); 194 return(ENXIO); 195 } 196 dev = dvp->v_specinfo->si_rdev; 197 vrele(dvp); 198 cdev = cdevsw_lookup(dev); 199 if (cdev == NULL) { 200 MARK_INT_FAIL(CODA_MOUNT_STATS); 201 return(ENXIO); 202 } 203 204 /* 205 * See if the device table matches our expectations. 206 */ 207 if (cdev != &vcoda_cdevsw) 208 { 209 MARK_INT_FAIL(CODA_MOUNT_STATS); 210 return(ENXIO); 211 } 212 213 if (minor(dev) >= NVCODA || minor(dev) < 0) { 214 MARK_INT_FAIL(CODA_MOUNT_STATS); 215 return(ENXIO); 216 } 217 218 /* 219 * Initialize the mount record and link it to the vfs struct 220 */ 221 mi = &coda_mnttbl[minor(dev)]; 222 223 if (!VC_OPEN(&mi->mi_vcomm)) { 224 MARK_INT_FAIL(CODA_MOUNT_STATS); 225 return(ENODEV); 226 } 227 228 /* No initialization (here) of mi_vcomm! */ 229 vfsp->mnt_data = mi; 230 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 231 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 232 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 233 vfsp->mnt_stat.f_namemax = MAXNAMLEN; 234 mi->mi_vfsp = vfsp; 235 236 /* 237 * Make a root vnode to placate the Vnode interface, but don't 238 * actually make the CODA_ROOT call to venus until the first call 239 * to coda_root in case a server is down while venus is starting. 240 */ 241 cp = make_coda_node(&rootfid, vfsp, VDIR); 242 rootvp = CTOV(cp); 243 rootvp->v_flag |= VROOT; 244 245 /* cp = make_coda_node(&ctlfid, vfsp, VCHR); 246 The above code seems to cause a loop in the cnode links. 247 I don't totally understand when it happens, it is caught 248 when closing down the system. 249 */ 250 cp = make_coda_node(&ctlfid, 0, VCHR); 251 252 coda_ctlvp = CTOV(cp); 253 254 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 255 mi->mi_vfsp = vfsp; 256 mi->mi_rootvp = rootvp; 257 258 /* set filesystem block size */ 259 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 260 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 261 262 /* error is currently guaranteed to be zero, but in case some 263 code changes... */ 264 CODADEBUG(1, 265 myprintf(("coda_mount returned %d\n",error));); 266 if (error) 267 MARK_INT_FAIL(CODA_MOUNT_STATS); 268 else 269 MARK_INT_SAT(CODA_MOUNT_STATS); 270 271 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, vfsp, 272 p); 273 } 274 275 int 276 coda_start(vfsp, flags, p) 277 struct mount *vfsp; 278 int flags; 279 struct proc *p; 280 { 281 ENTRY; 282 vftomi(vfsp)->mi_started = 1; 283 return (0); 284 } 285 286 int 287 coda_unmount(vfsp, mntflags, p) 288 struct mount *vfsp; 289 int mntflags; 290 struct proc *p; 291 { 292 struct coda_mntinfo *mi = vftomi(vfsp); 293 int active, error = 0; 294 295 ENTRY; 296 MARK_ENTRY(CODA_UMOUNT_STATS); 297 if (!CODA_MOUNTED(vfsp)) { 298 MARK_INT_FAIL(CODA_UMOUNT_STATS); 299 return(EINVAL); 300 } 301 302 if (mi->mi_vfsp == vfsp) { /* We found the victim */ 303 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp))) 304 return (EBUSY); /* Venus is still running */ 305 306 #ifdef DEBUG 307 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp)); 308 #endif 309 mi->mi_started = 0; 310 311 vrele(mi->mi_rootvp); 312 313 active = coda_kill(vfsp, NOT_DOWNCALL); 314 mi->mi_rootvp->v_flag &= ~VROOT; 315 error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE); 316 printf("coda_unmount: active = %d, vflush active %d\n", active, error); 317 error = 0; 318 319 /* I'm going to take this out to allow lookups to go through. I'm 320 * not sure it's important anyway. -- DCS 2/2/94 321 */ 322 /* vfsp->VFS_DATA = NULL; */ 323 324 /* No more vfsp's to hold onto */ 325 mi->mi_vfsp = NULL; 326 mi->mi_rootvp = NULL; 327 328 if (error) 329 MARK_INT_FAIL(CODA_UMOUNT_STATS); 330 else 331 MARK_INT_SAT(CODA_UMOUNT_STATS); 332 333 return(error); 334 } 335 return (EINVAL); 336 } 337 338 /* 339 * find root of cfs 340 */ 341 int 342 coda_root(vfsp, vpp) 343 struct mount *vfsp; 344 struct vnode **vpp; 345 { 346 struct coda_mntinfo *mi = vftomi(vfsp); 347 int error; 348 struct proc *p = curproc; /* 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), p->p_cred->pc_ucred, p, &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 int 410 coda_quotactl(vfsp, cmd, uid, arg, p) 411 struct mount *vfsp; 412 int cmd; 413 uid_t uid; 414 void *arg; 415 struct proc *p; 416 { 417 ENTRY; 418 return (EOPNOTSUPP); 419 } 420 421 /* 422 * Get file system statistics. 423 */ 424 int 425 coda_nb_statvfs(vfsp, sbp, p) 426 struct mount *vfsp; 427 struct statvfs *sbp; 428 struct proc *p; 429 { 430 struct coda_statfs fsstat; 431 int error; 432 433 ENTRY; 434 MARK_ENTRY(CODA_STATFS_STATS); 435 if (!CODA_MOUNTED(vfsp)) { 436 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 437 return(EINVAL); 438 } 439 440 /* XXX - what to do about f_flags, others? --bnoble */ 441 /* Below This is what AFS does 442 #define NB_SFS_SIZ 0x895440 443 */ 444 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 445 446 error = venus_statfs(vftomi(vfsp), p->p_cred->pc_ucred, p, &fsstat); 447 448 if (!error) { 449 sbp->f_bsize = 8192; /* XXX */ 450 sbp->f_frsize = 8192; /* XXX */ 451 sbp->f_iosize = 8192; /* XXX */ 452 sbp->f_blocks = fsstat.f_blocks; 453 sbp->f_bfree = fsstat.f_bfree; 454 sbp->f_bavail = fsstat.f_bavail; 455 sbp->f_bresvd = 0; 456 sbp->f_files = fsstat.f_files; 457 sbp->f_ffree = fsstat.f_ffree; 458 sbp->f_favail = fsstat.f_ffree; 459 sbp->f_fresvd = 0; 460 copy_statvfs_info(sbp, vfsp); 461 } 462 463 MARK_INT_SAT(CODA_STATFS_STATS); 464 return(error); 465 } 466 467 /* 468 * Flush any pending I/O. 469 */ 470 int 471 coda_sync(vfsp, waitfor, cred, p) 472 struct mount *vfsp; 473 int waitfor; 474 struct ucred *cred; 475 struct proc *p; 476 { 477 ENTRY; 478 MARK_ENTRY(CODA_SYNC_STATS); 479 MARK_INT_SAT(CODA_SYNC_STATS); 480 return(0); 481 } 482 483 int 484 coda_vget(vfsp, ino, vpp) 485 struct mount *vfsp; 486 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(vfsp, fhp, nam, vpp, exflagsp, creadanonp) 500 struct mount *vfsp; 501 struct fid *fhp; 502 struct mbuf *nam; 503 struct vnode **vpp; 504 int *exflagsp; 505 struct ucred **creadanonp; 506 { 507 struct cfid *cfid = (struct cfid *)fhp; 508 struct cnode *cp = 0; 509 int error; 510 struct proc *p = curproc; /* 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, p->p_cred->pc_ucred, p, &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(vnp, fidp) 543 struct vnode *vnp; 544 struct fid *fidp; 545 { 546 ENTRY; 547 return (EOPNOTSUPP); 548 } 549 550 void 551 coda_init(void) 552 { 553 ENTRY; 554 } 555 556 void 557 coda_done(void) 558 { 559 ENTRY; 560 } 561 562 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 563 { 564 sysctl_createv(clog, 0, NULL, NULL, 565 CTLFLAG_PERMANENT, 566 CTLTYPE_NODE, "vfs", NULL, 567 NULL, 0, NULL, 0, 568 CTL_VFS, CTL_EOL); 569 sysctl_createv(clog, 0, NULL, NULL, 570 CTLFLAG_PERMANENT, 571 CTLTYPE_NODE, "coda", 572 SYSCTL_DESCR("code vfs options"), 573 NULL, 0, NULL, 0, 574 CTL_VFS, 18, CTL_EOL); 575 /* 576 * XXX the "18" above could be dynamic, thereby eliminating 577 * one more instance of the "number to vfs" mapping problem, 578 * but "18" is the order as taken from sys/mount.h 579 */ 580 581 /* 582 sysctl_createv(clog, 0, NULL, NULL, 583 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 584 CTLTYPE_INT, "clusterread", 585 SYSCTL_DESCR( anyone? ), 586 NULL, 0, &doclusterread, 0, 587 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 588 */ 589 } 590 591 /* 592 * To allow for greater ease of use, some vnodes may be orphaned when 593 * Venus dies. Certain operations should still be allowed to go 594 * through, but without propagating orphan-ness. So this function will 595 * get a new vnode for the file from the current run of Venus. 596 */ 597 598 int 599 getNewVnode(vpp) 600 struct vnode **vpp; 601 { 602 struct cfid cfid; 603 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 604 605 ENTRY; 606 607 cfid.cfid_len = (short)sizeof(CodaFid); 608 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 609 /* XXX ? */ 610 611 /* We're guessing that if set, the 1st element on the list is a 612 * valid vnode to use. If not, return ENODEV as venus is dead. 613 */ 614 if (mi->mi_vfsp == NULL) 615 return ENODEV; 616 617 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 618 NULL, NULL); 619 } 620 621 #include <ufs/ufs/quota.h> 622 #include <ufs/ufs/ufsmount.h> 623 /* get the mount structure corresponding to a given device. Assume 624 * device corresponds to a UFS. Return NULL if no device is found. 625 */ 626 struct mount *devtomp(dev) 627 dev_t dev; 628 { 629 struct mount *mp, *nmp; 630 631 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 632 nmp = mp->mnt_list.cqe_next; 633 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 634 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 635 /* mount corresponds to UFS and the device matches one we want */ 636 return(mp); 637 } 638 } 639 /* mount structure wasn't found */ 640 return(NULL); 641 } 642