1 /* $NetBSD: coda_vfsops.c,v 1.44 2005/07/02 07:05:27 blymn 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.44 2005/07/02 07:05:27 blymn 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 vfs_stdextattrctl, 125 coda_vnodeopv_descs, 126 0 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(vfsp, path, data, ndp, p) 154 struct mount *vfsp; /* Allocated and initialized by mount(2) */ 155 const char *path; /* path covered: ignored by the fs-layer */ 156 void *data; /* Need to define a data type for this in netbsd? */ 157 struct nameidata *ndp; /* Clobber this to lookup the device name */ 158 struct proc *p; /* The ever-famous proc pointer */ 159 { 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 0; 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 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, p); 186 error = namei(ndp); 187 dvp = ndp->ni_vp; 188 189 if (error) { 190 MARK_INT_FAIL(CODA_MOUNT_STATS); 191 return (error); 192 } 193 if (dvp->v_type != VCHR) { 194 MARK_INT_FAIL(CODA_MOUNT_STATS); 195 vrele(dvp); 196 return(ENXIO); 197 } 198 dev = dvp->v_specinfo->si_rdev; 199 vrele(dvp); 200 cdev = cdevsw_lookup(dev); 201 if (cdev == NULL) { 202 MARK_INT_FAIL(CODA_MOUNT_STATS); 203 return(ENXIO); 204 } 205 206 /* 207 * See if the device table matches our expectations. 208 */ 209 if (cdev != &vcoda_cdevsw) 210 { 211 MARK_INT_FAIL(CODA_MOUNT_STATS); 212 return(ENXIO); 213 } 214 215 if (minor(dev) >= NVCODA || minor(dev) < 0) { 216 MARK_INT_FAIL(CODA_MOUNT_STATS); 217 return(ENXIO); 218 } 219 220 /* 221 * Initialize the mount record and link it to the vfs struct 222 */ 223 mi = &coda_mnttbl[minor(dev)]; 224 225 if (!VC_OPEN(&mi->mi_vcomm)) { 226 MARK_INT_FAIL(CODA_MOUNT_STATS); 227 return(ENODEV); 228 } 229 230 /* No initialization (here) of mi_vcomm! */ 231 vfsp->mnt_data = mi; 232 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 233 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 234 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 235 vfsp->mnt_stat.f_namemax = MAXNAMLEN; 236 mi->mi_vfsp = vfsp; 237 238 /* 239 * Make a root vnode to placate the Vnode interface, but don't 240 * actually make the CODA_ROOT call to venus until the first call 241 * to coda_root in case a server is down while venus is starting. 242 */ 243 cp = make_coda_node(&rootfid, vfsp, VDIR); 244 rtvp = CTOV(cp); 245 rtvp->v_flag |= VROOT; 246 247 /* cp = make_coda_node(&ctlfid, vfsp, VCHR); 248 The above code seems to cause a loop in the cnode links. 249 I don't totally understand when it happens, it is caught 250 when closing down the system. 251 */ 252 cp = make_coda_node(&ctlfid, 0, VCHR); 253 254 coda_ctlvp = CTOV(cp); 255 256 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 257 mi->mi_vfsp = vfsp; 258 mi->mi_rootvp = rtvp; 259 260 /* set filesystem block size */ 261 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 262 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 263 264 /* error is currently guaranteed to be zero, but in case some 265 code changes... */ 266 CODADEBUG(1, 267 myprintf(("coda_mount returned %d\n",error));); 268 if (error) 269 MARK_INT_FAIL(CODA_MOUNT_STATS); 270 else 271 MARK_INT_SAT(CODA_MOUNT_STATS); 272 273 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, vfsp, 274 p); 275 } 276 277 int 278 coda_start(vfsp, flags, p) 279 struct mount *vfsp; 280 int flags; 281 struct proc *p; 282 { 283 ENTRY; 284 vftomi(vfsp)->mi_started = 1; 285 return (0); 286 } 287 288 int 289 coda_unmount(vfsp, mntflags, p) 290 struct mount *vfsp; 291 int mntflags; 292 struct proc *p; 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_flag &= ~VROOT; 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(vfsp, vpp) 345 struct mount *vfsp; 346 struct vnode **vpp; 347 { 348 struct coda_mntinfo *mi = vftomi(vfsp); 349 int error; 350 struct proc *p = curproc; /* XXX - bnoble */ 351 CodaFid VFid; 352 static const CodaFid invalfid = INVAL_FID; 353 354 ENTRY; 355 MARK_ENTRY(CODA_ROOT_STATS); 356 357 if (vfsp == mi->mi_vfsp) { 358 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid))) 359 { /* Found valid root. */ 360 *vpp = mi->mi_rootvp; 361 /* On Mach, this is vref. On NetBSD, VOP_LOCK */ 362 vref(*vpp); 363 vn_lock(*vpp, LK_EXCLUSIVE); 364 MARK_INT_SAT(CODA_ROOT_STATS); 365 return(0); 366 } 367 } 368 369 error = venus_root(vftomi(vfsp), p->p_cred->pc_ucred, p, &VFid); 370 371 if (!error) { 372 /* 373 * Save the new rootfid in the cnode, and rehash the cnode into the 374 * cnode hash with the new fid key. 375 */ 376 coda_unsave(VTOC(mi->mi_rootvp)); 377 VTOC(mi->mi_rootvp)->c_fid = VFid; 378 coda_save(VTOC(mi->mi_rootvp)); 379 380 *vpp = mi->mi_rootvp; 381 vref(*vpp); 382 vn_lock(*vpp, LK_EXCLUSIVE); 383 MARK_INT_SAT(CODA_ROOT_STATS); 384 goto exit; 385 } else if (error == ENODEV || error == EINTR) { 386 /* Gross hack here! */ 387 /* 388 * If Venus fails to respond to the CODA_ROOT call, coda_call returns 389 * ENODEV. Return the uninitialized root vnode to allow vfs 390 * operations such as unmount to continue. Without this hack, 391 * there is no way to do an unmount if Venus dies before a 392 * successful CODA_ROOT call is done. All vnode operations 393 * will fail. 394 */ 395 *vpp = mi->mi_rootvp; 396 vref(*vpp); 397 vn_lock(*vpp, LK_EXCLUSIVE); 398 MARK_INT_FAIL(CODA_ROOT_STATS); 399 error = 0; 400 goto exit; 401 } else { 402 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); ); 403 MARK_INT_FAIL(CODA_ROOT_STATS); 404 405 goto exit; 406 } 407 exit: 408 return(error); 409 } 410 411 int 412 coda_quotactl(vfsp, cmd, uid, arg, p) 413 struct mount *vfsp; 414 int cmd; 415 uid_t uid; 416 void *arg; 417 struct proc *p; 418 { 419 ENTRY; 420 return (EOPNOTSUPP); 421 } 422 423 /* 424 * Get file system statistics. 425 */ 426 int 427 coda_nb_statvfs(vfsp, sbp, p) 428 struct mount *vfsp; 429 struct statvfs *sbp; 430 struct proc *p; 431 { 432 struct coda_statfs fsstat; 433 int error; 434 435 ENTRY; 436 MARK_ENTRY(CODA_STATFS_STATS); 437 if (!CODA_MOUNTED(vfsp)) { 438 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 439 return(EINVAL); 440 } 441 442 /* XXX - what to do about f_flags, others? --bnoble */ 443 /* Below This is what AFS does 444 #define NB_SFS_SIZ 0x895440 445 */ 446 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 447 448 error = venus_statfs(vftomi(vfsp), p->p_cred->pc_ucred, p, &fsstat); 449 450 if (!error) { 451 sbp->f_bsize = 8192; /* XXX */ 452 sbp->f_frsize = 8192; /* XXX */ 453 sbp->f_iosize = 8192; /* XXX */ 454 sbp->f_blocks = fsstat.f_blocks; 455 sbp->f_bfree = fsstat.f_bfree; 456 sbp->f_bavail = fsstat.f_bavail; 457 sbp->f_bresvd = 0; 458 sbp->f_files = fsstat.f_files; 459 sbp->f_ffree = fsstat.f_ffree; 460 sbp->f_favail = fsstat.f_ffree; 461 sbp->f_fresvd = 0; 462 copy_statvfs_info(sbp, vfsp); 463 } 464 465 MARK_INT_SAT(CODA_STATFS_STATS); 466 return(error); 467 } 468 469 /* 470 * Flush any pending I/O. 471 */ 472 int 473 coda_sync(vfsp, waitfor, cred, p) 474 struct mount *vfsp; 475 int waitfor; 476 struct ucred *cred; 477 struct proc *p; 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(vfsp, ino, vpp) 487 struct mount *vfsp; 488 ino_t ino; 489 struct vnode **vpp; 490 { 491 ENTRY; 492 return (EOPNOTSUPP); 493 } 494 495 /* 496 * fhtovp is now what vget used to be in 4.3-derived systems. For 497 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 498 * a type-specific fid. 499 */ 500 int 501 coda_fhtovp(vfsp, fhp, nam, vpp, exflagsp, creadanonp) 502 struct mount *vfsp; 503 struct fid *fhp; 504 struct mbuf *nam; 505 struct vnode **vpp; 506 int *exflagsp; 507 struct ucred **creadanonp; 508 { 509 struct cfid *cfid = (struct cfid *)fhp; 510 struct cnode *cp = 0; 511 int error; 512 struct proc *p = curproc; /* XXX -mach */ 513 CodaFid VFid; 514 int vtype; 515 516 ENTRY; 517 518 MARK_ENTRY(CODA_VGET_STATS); 519 /* Check for vget of control object. */ 520 if (IS_CTL_FID(&cfid->cfid_fid)) { 521 *vpp = coda_ctlvp; 522 vref(coda_ctlvp); 523 MARK_INT_SAT(CODA_VGET_STATS); 524 return(0); 525 } 526 527 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, p->p_cred->pc_ucred, p, &VFid, &vtype); 528 529 if (error) { 530 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 531 *vpp = (struct vnode *)0; 532 } else { 533 CODADEBUG(CODA_VGET, 534 myprintf(("vget: %s type %d result %d\n", 535 coda_f2s(&VFid), vtype, error)); ) 536 537 cp = make_coda_node(&VFid, vfsp, vtype); 538 *vpp = CTOV(cp); 539 } 540 return(error); 541 } 542 543 int 544 coda_vptofh(vnp, fidp) 545 struct vnode *vnp; 546 struct fid *fidp; 547 { 548 ENTRY; 549 return (EOPNOTSUPP); 550 } 551 552 void 553 coda_init(void) 554 { 555 ENTRY; 556 } 557 558 void 559 coda_done(void) 560 { 561 ENTRY; 562 } 563 564 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 565 { 566 sysctl_createv(clog, 0, NULL, NULL, 567 CTLFLAG_PERMANENT, 568 CTLTYPE_NODE, "vfs", NULL, 569 NULL, 0, NULL, 0, 570 CTL_VFS, CTL_EOL); 571 sysctl_createv(clog, 0, NULL, NULL, 572 CTLFLAG_PERMANENT, 573 CTLTYPE_NODE, "coda", 574 SYSCTL_DESCR("code vfs options"), 575 NULL, 0, NULL, 0, 576 CTL_VFS, 18, CTL_EOL); 577 /* 578 * XXX the "18" above could be dynamic, thereby eliminating 579 * one more instance of the "number to vfs" mapping problem, 580 * but "18" is the order as taken from sys/mount.h 581 */ 582 583 /* 584 sysctl_createv(clog, 0, NULL, NULL, 585 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 586 CTLTYPE_INT, "clusterread", 587 SYSCTL_DESCR( anyone? ), 588 NULL, 0, &doclusterread, 0, 589 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 590 */ 591 } 592 593 /* 594 * To allow for greater ease of use, some vnodes may be orphaned when 595 * Venus dies. Certain operations should still be allowed to go 596 * through, but without propagating orphan-ness. So this function will 597 * get a new vnode for the file from the current run of Venus. 598 */ 599 600 int 601 getNewVnode(vpp) 602 struct vnode **vpp; 603 { 604 struct cfid cfid; 605 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 606 607 ENTRY; 608 609 cfid.cfid_len = (short)sizeof(CodaFid); 610 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 611 /* XXX ? */ 612 613 /* We're guessing that if set, the 1st element on the list is a 614 * valid vnode to use. If not, return ENODEV as venus is dead. 615 */ 616 if (mi->mi_vfsp == NULL) 617 return ENODEV; 618 619 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 620 NULL, NULL); 621 } 622 623 #include <ufs/ufs/quota.h> 624 #include <ufs/ufs/ufsmount.h> 625 /* get the mount structure corresponding to a given device. Assume 626 * device corresponds to a UFS. Return NULL if no device is found. 627 */ 628 struct mount *devtomp(dev) 629 dev_t dev; 630 { 631 struct mount *mp, *nmp; 632 633 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 634 nmp = mp->mnt_list.cqe_next; 635 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 636 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 637 /* mount corresponds to UFS and the device matches one we want */ 638 return(mp); 639 } 640 } 641 /* mount structure wasn't found */ 642 return(NULL); 643 } 644