1 /* $NetBSD: coda_vfsops.c,v 1.91 2024/05/17 23:57:46 thorpej 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.91 2024/05/17 23:57:46 thorpej Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/sysctl.h> 53 #include <sys/conf.h> 54 #include <sys/namei.h> 55 #include <sys/dirent.h> 56 #include <sys/mount.h> 57 #include <sys/proc.h> 58 #include <sys/select.h> 59 #include <sys/kauth.h> 60 #include <sys/module.h> 61 62 #include <coda/coda.h> 63 #include <coda/cnode.h> 64 #include <coda/coda_vfsops.h> 65 #include <coda/coda_venus.h> 66 #include <coda/coda_subr.h> 67 #include <coda/coda_opstats.h> 68 /* for VN_RDEV */ 69 #include <miscfs/specfs/specdev.h> 70 #include <miscfs/genfs/genfs.h> 71 72 MODULE(MODULE_CLASS_VFS, coda, "vcoda"); 73 74 #define ENTRY if(coda_vfsop_print_entry) myprintf(("Entered %s\n",__func__)) 75 76 extern struct vnode *coda_ctlvp; 77 extern struct coda_mntinfo coda_mnttbl[NVCODA]; /* indexed by minor device number */ 78 79 /* structure to keep statistics of internally generated/satisfied calls */ 80 81 struct coda_op_stats coda_vfsopstats[CODA_VFSOPS_SIZE]; 82 83 #define MARK_ENTRY(op) (coda_vfsopstats[op].entries++) 84 #define MARK_INT_SAT(op) (coda_vfsopstats[op].sat_intrn++) 85 #define MARK_INT_FAIL(op) (coda_vfsopstats[op].unsat_intrn++) 86 #define MRAK_INT_GEN(op) (coda_vfsopstats[op].gen_intrn++) 87 88 extern const struct cdevsw vcoda_cdevsw; 89 extern const struct vnodeopv_desc coda_vnodeop_opv_desc; 90 91 const struct vnodeopv_desc * const coda_vnodeopv_descs[] = { 92 &coda_vnodeop_opv_desc, 93 NULL, 94 }; 95 96 struct vfsops coda_vfsops = { 97 .vfs_name = MOUNT_CODA, 98 .vfs_min_mount_data = 256, 99 /* This is the pathname, unlike every other fs */ 100 .vfs_mount = coda_mount, 101 .vfs_start = coda_start, 102 .vfs_unmount = coda_unmount, 103 .vfs_root = coda_root, 104 .vfs_quotactl = (void *)eopnotsupp, 105 .vfs_statvfs = coda_nb_statvfs, 106 .vfs_sync = coda_sync, 107 .vfs_vget = coda_vget, 108 .vfs_loadvnode = coda_loadvnode, 109 .vfs_fhtovp = (void *)eopnotsupp, 110 .vfs_vptofh = (void *)eopnotsupp, 111 .vfs_init = coda_init, 112 .vfs_done = coda_done, 113 .vfs_mountroot = (void *)eopnotsupp, 114 .vfs_snapshot = (void *)eopnotsupp, 115 .vfs_extattrctl = vfs_stdextattrctl, 116 .vfs_suspendctl = genfs_suspendctl, 117 .vfs_renamelock_enter = genfs_renamelock_enter, 118 .vfs_renamelock_exit = genfs_renamelock_exit, 119 .vfs_fsync = (void *)eopnotsupp, 120 .vfs_opv_descs = coda_vnodeopv_descs 121 }; 122 123 static int 124 coda_modcmd(modcmd_t cmd, void *arg) 125 { 126 127 switch (cmd) { 128 case MODULE_CMD_INIT: 129 return vfs_attach(&coda_vfsops); 130 case MODULE_CMD_FINI: 131 return vfs_detach(&coda_vfsops); 132 default: 133 return ENOTTY; 134 } 135 } 136 137 int 138 coda_vfsopstats_init(void) 139 { 140 int i; 141 142 for (i=0;i<CODA_VFSOPS_SIZE;i++) { 143 coda_vfsopstats[i].opcode = i; 144 coda_vfsopstats[i].entries = 0; 145 coda_vfsopstats[i].sat_intrn = 0; 146 coda_vfsopstats[i].unsat_intrn = 0; 147 coda_vfsopstats[i].gen_intrn = 0; 148 } 149 150 return 0; 151 } 152 153 /* 154 * cfs mount vfsop 155 * Set up mount info record and attach it to vfs struct. 156 */ 157 /*ARGSUSED*/ 158 int 159 coda_mount(struct mount *vfsp, /* Allocated and initialized by mount(2) */ 160 const char *path, /* path covered: ignored by the fs-layer */ 161 void *data, /* Need to define a data type for this in netbsd? */ 162 size_t *data_len) 163 { 164 struct lwp *l = curlwp; 165 struct vnode *dvp; 166 struct cnode *cp; 167 dev_t dev; 168 struct coda_mntinfo *mi; 169 struct vnode *rtvp; 170 const struct cdevsw *cdev; 171 CodaFid rootfid = INVAL_FID; 172 CodaFid ctlfid = CTL_FID; 173 int error; 174 175 if (data == NULL) 176 return EINVAL; 177 if (vfsp->mnt_flag & MNT_GETARGS) 178 return EINVAL; 179 ENTRY; 180 181 coda_vfsopstats_init(); 182 coda_vnodeopstats_init(); 183 184 MARK_ENTRY(CODA_MOUNT_STATS); 185 if (CODA_MOUNTED(vfsp)) { 186 MARK_INT_FAIL(CODA_MOUNT_STATS); 187 return(EBUSY); 188 } 189 190 /* Validate mount device. Similar to getmdev(). */ 191 192 /* 193 * XXX: coda passes the mount device as the entire mount args, 194 * All other fs pass a structure contining a pointer. 195 * In order to get sys_mount() to do the copyin() we've set a 196 * fixed default size for the filename buffer. 197 */ 198 /* Ensure that namei() doesn't run off the filename buffer */ 199 if (*data_len < 1 || *data_len > PATH_MAX || 200 strnlen(data, *data_len) >= *data_len) { 201 MARK_INT_FAIL(CODA_MOUNT_STATS); 202 return EINVAL; 203 } 204 error = namei_simple_kernel((char *)data, NSM_FOLLOW_NOEMULROOT, 205 &dvp); 206 207 if (error) { 208 MARK_INT_FAIL(CODA_MOUNT_STATS); 209 return (error); 210 } 211 if (dvp->v_type != VCHR) { 212 MARK_INT_FAIL(CODA_MOUNT_STATS); 213 vrele(dvp); 214 return(ENXIO); 215 } 216 dev = dvp->v_rdev; 217 vrele(dvp); 218 cdev = cdevsw_lookup(dev); 219 if (cdev == NULL) { 220 MARK_INT_FAIL(CODA_MOUNT_STATS); 221 return(ENXIO); 222 } 223 224 /* 225 * See if the device table matches our expectations. 226 */ 227 if (cdev != &vcoda_cdevsw) 228 { 229 MARK_INT_FAIL(CODA_MOUNT_STATS); 230 return(ENXIO); 231 } 232 233 if (minor(dev) >= NVCODA) { 234 MARK_INT_FAIL(CODA_MOUNT_STATS); 235 return(ENXIO); 236 } 237 238 /* 239 * Initialize the mount record and link it to the vfs struct 240 */ 241 mi = &coda_mnttbl[minor(dev)]; 242 243 if (!VC_OPEN(&mi->mi_vcomm)) { 244 MARK_INT_FAIL(CODA_MOUNT_STATS); 245 return(ENODEV); 246 } 247 248 /* No initialization (here) of mi_vcomm! */ 249 vfsp->mnt_data = mi; 250 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 251 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 252 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 253 vfsp->mnt_stat.f_namemax = CODA_MAXNAMLEN; 254 mi->mi_vfsp = vfsp; 255 256 /* 257 * Make a root vnode to placate the Vnode interface, but don't 258 * actually make the CODA_ROOT call to venus until the first call 259 * to coda_root in case a server is down while venus is starting. 260 */ 261 cp = make_coda_node(&rootfid, vfsp, VDIR); 262 rtvp = CTOV(cp); 263 rtvp->v_vflag |= VV_ROOT; 264 265 cp = make_coda_node(&ctlfid, vfsp, VCHR); 266 267 coda_ctlvp = CTOV(cp); 268 269 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 270 mi->mi_vfsp = vfsp; 271 mi->mi_rootvp = rtvp; 272 273 /* set filesystem block size */ 274 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 275 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 276 277 /* error is currently guaranteed to be zero, but in case some 278 code changes... */ 279 CODADEBUG(1, 280 myprintf(("coda_mount returned %d\n",error));); 281 if (error) 282 MARK_INT_FAIL(CODA_MOUNT_STATS); 283 else 284 MARK_INT_SAT(CODA_MOUNT_STATS); 285 286 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, 287 vfsp->mnt_op->vfs_name, vfsp, l); 288 } 289 290 int 291 coda_start(struct mount *vfsp, int flags) 292 { 293 ENTRY; 294 vftomi(vfsp)->mi_started = 1; 295 return (0); 296 } 297 298 int 299 coda_unmount(struct mount *vfsp, int mntflags) 300 { 301 struct coda_mntinfo *mi = vftomi(vfsp); 302 int active, error = 0; 303 304 ENTRY; 305 MARK_ENTRY(CODA_UMOUNT_STATS); 306 if (!CODA_MOUNTED(vfsp)) { 307 MARK_INT_FAIL(CODA_UMOUNT_STATS); 308 return(EINVAL); 309 } 310 311 if (mi->mi_vfsp == vfsp) { /* We found the victim */ 312 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp))) 313 return (EBUSY); /* Venus is still running */ 314 315 #ifdef DEBUG 316 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp)); 317 #endif 318 mi->mi_started = 0; 319 320 vrele(mi->mi_rootvp); 321 vrele(coda_ctlvp); 322 323 active = coda_kill(vfsp, NOT_DOWNCALL); 324 mi->mi_rootvp->v_vflag &= ~VV_ROOT; 325 error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE); 326 printf("coda_unmount: active = %d, vflush active %d\n", active, error); 327 error = 0; 328 329 /* I'm going to take this out to allow lookups to go through. I'm 330 * not sure it's important anyway. -- DCS 2/2/94 331 */ 332 /* vfsp->VFS_DATA = NULL; */ 333 334 /* No more vfsp's to hold onto */ 335 mi->mi_vfsp = NULL; 336 mi->mi_rootvp = NULL; 337 338 if (error) 339 MARK_INT_FAIL(CODA_UMOUNT_STATS); 340 else 341 MARK_INT_SAT(CODA_UMOUNT_STATS); 342 343 return(error); 344 } 345 return (EINVAL); 346 } 347 348 /* 349 * find root of cfs 350 */ 351 int 352 coda_root(struct mount *vfsp, int lktype, struct vnode **vpp) 353 { 354 struct coda_mntinfo *mi = vftomi(vfsp); 355 int error; 356 struct lwp *l = curlwp; /* XXX - bnoble */ 357 CodaFid VFid; 358 static const CodaFid invalfid = INVAL_FID; 359 360 ENTRY; 361 MARK_ENTRY(CODA_ROOT_STATS); 362 363 if (vfsp == mi->mi_vfsp) { 364 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid))) 365 { /* Found valid root. */ 366 *vpp = mi->mi_rootvp; 367 /* On Mach, this is vref. On NetBSD, VOP_LOCK */ 368 vref(*vpp); 369 vn_lock(*vpp, lktype); 370 MARK_INT_SAT(CODA_ROOT_STATS); 371 return(0); 372 } 373 } 374 375 error = venus_root(vftomi(vfsp), l->l_cred, l->l_proc, &VFid); 376 377 if (!error) { 378 struct cnode *cp = VTOC(mi->mi_rootvp); 379 380 /* 381 * Save the new rootfid in the cnode, and rekey the cnode 382 * with the new fid key. 383 */ 384 error = vcache_rekey_enter(vfsp, mi->mi_rootvp, 385 &invalfid, sizeof(CodaFid), &VFid, sizeof(CodaFid)); 386 if (error) 387 goto exit; 388 cp->c_fid = VFid; 389 vcache_rekey_exit(vfsp, mi->mi_rootvp, 390 &invalfid, sizeof(CodaFid), &cp->c_fid, sizeof(CodaFid)); 391 392 *vpp = mi->mi_rootvp; 393 vref(*vpp); 394 vn_lock(*vpp, lktype); 395 MARK_INT_SAT(CODA_ROOT_STATS); 396 goto exit; 397 } else if (error == ENODEV || error == EINTR) { 398 /* Gross hack here! */ 399 /* 400 * If Venus fails to respond to the CODA_ROOT call, coda_call returns 401 * ENODEV. Return the uninitialized root vnode to allow vfs 402 * operations such as unmount to continue. Without this hack, 403 * there is no way to do an unmount if Venus dies before a 404 * successful CODA_ROOT call is done. All vnode operations 405 * will fail. 406 */ 407 *vpp = mi->mi_rootvp; 408 vref(*vpp); 409 vn_lock(*vpp, lktype); 410 MARK_INT_FAIL(CODA_ROOT_STATS); 411 error = 0; 412 goto exit; 413 } else { 414 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); ); 415 MARK_INT_FAIL(CODA_ROOT_STATS); 416 417 goto exit; 418 } 419 exit: 420 return(error); 421 } 422 423 /* 424 * Get file system statistics. 425 */ 426 int 427 coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp) 428 { 429 struct lwp *l = curlwp; 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), l->l_cred, l, &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(struct mount *vfsp, int waitfor, 472 kauth_cred_t cred) 473 { 474 ENTRY; 475 MARK_ENTRY(CODA_SYNC_STATS); 476 MARK_INT_SAT(CODA_SYNC_STATS); 477 return(0); 478 } 479 480 int 481 coda_vget(struct mount *vfsp, ino_t ino, int lktype, 482 struct vnode **vpp) 483 { 484 ENTRY; 485 return (EOPNOTSUPP); 486 } 487 488 int 489 coda_loadvnode(struct mount *mp, struct vnode *vp, 490 const void *key, size_t key_len, const void **new_key) 491 { 492 CodaFid fid; 493 struct cnode *cp; 494 extern int (**coda_vnodeop_p)(void *); 495 496 KASSERT(key_len == sizeof(CodaFid)); 497 memcpy(&fid, key, key_len); 498 499 cp = kmem_zalloc(sizeof(*cp), KM_SLEEP); 500 mutex_init(&cp->c_lock, MUTEX_DEFAULT, IPL_NONE); 501 cp->c_fid = fid; 502 cp->c_vnode = vp; 503 vp->v_op = coda_vnodeop_p; 504 vp->v_tag = VT_CODA; 505 vp->v_type = VNON; 506 vp->v_data = cp; 507 508 *new_key = &cp->c_fid; 509 510 return 0; 511 } 512 513 /* 514 * fhtovp is now what vget used to be in 4.3-derived systems. For 515 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 516 * a type-specific fid. 517 */ 518 int 519 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam, 520 struct vnode **vpp, int *exflagsp, 521 kauth_cred_t *creadanonp, int lktype) 522 { 523 struct cfid *cfid = (struct cfid *)fhp; 524 struct cnode *cp = 0; 525 int error; 526 struct lwp *l = curlwp; /* XXX -mach */ 527 CodaFid VFid; 528 int vtype; 529 530 ENTRY; 531 532 MARK_ENTRY(CODA_VGET_STATS); 533 /* Check for vget of control object. */ 534 if (IS_CTL_FID(&cfid->cfid_fid)) { 535 *vpp = coda_ctlvp; 536 vref(coda_ctlvp); 537 MARK_INT_SAT(CODA_VGET_STATS); 538 return(0); 539 } 540 541 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype); 542 543 if (error) { 544 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 545 *vpp = (struct vnode *)0; 546 } else { 547 CODADEBUG(CODA_VGET, 548 myprintf(("vget: %s type %d result %d\n", 549 coda_f2s(&VFid), vtype, error)); ) 550 551 cp = make_coda_node(&VFid, vfsp, vtype); 552 *vpp = CTOV(cp); 553 } 554 return(error); 555 } 556 557 int 558 coda_vptofh(struct vnode *vnp, struct fid *fidp) 559 { 560 ENTRY; 561 return (EOPNOTSUPP); 562 } 563 564 void 565 coda_init(void) 566 { 567 ENTRY; 568 } 569 570 void 571 coda_done(void) 572 { 573 ENTRY; 574 } 575 576 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 577 { 578 579 sysctl_createv(clog, 0, NULL, NULL, 580 CTLFLAG_PERMANENT, 581 CTLTYPE_NODE, "coda", 582 SYSCTL_DESCR("code vfs options"), 583 NULL, 0, NULL, 0, 584 CTL_VFS, 18, CTL_EOL); 585 /* 586 * XXX the "18" above could be dynamic, thereby eliminating 587 * one more instance of the "number to vfs" mapping problem, 588 * but "18" is the order as taken from sys/mount.h 589 */ 590 591 /* 592 sysctl_createv(clog, 0, NULL, NULL, 593 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 594 CTLTYPE_INT, "clusterread", 595 SYSCTL_DESCR( anyone? ), 596 NULL, 0, &doclusterread, 0, 597 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 598 */ 599 } 600 601 /* 602 * To allow for greater ease of use, some vnodes may be orphaned when 603 * Venus dies. Certain operations should still be allowed to go 604 * through, but without propagating orphan-ness. So this function will 605 * get a new vnode for the file from the current run of Venus. 606 */ 607 608 int 609 getNewVnode(struct vnode **vpp) 610 { 611 struct cfid cfid; 612 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 613 614 ENTRY; 615 616 cfid.cfid_len = (short)sizeof(CodaFid); 617 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 618 /* XXX ? */ 619 620 /* We're guessing that if set, the 1st element on the list is a 621 * valid vnode to use. If not, return ENODEV as venus is dead. 622 */ 623 if (mi->mi_vfsp == NULL) 624 return ENODEV; 625 626 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 627 NULL, NULL, LK_EXCLUSIVE); 628 } 629 630 /* Get the mount structure corresponding to a given device. 631 * Return NULL if no device is found or the device is not mounted. 632 */ 633 struct mount *devtomp(dev_t dev) 634 { 635 struct mount *mp; 636 struct vnode *vp; 637 638 if (spec_node_lookup_by_dev(VBLK, dev, VDEAD_NOWAIT, &vp) == 0) { 639 mp = spec_node_getmountedfs(vp); 640 vrele(vp); 641 } else { 642 mp = NULL; 643 } 644 645 return mp; 646 } 647