1 /* $NetBSD: coda_vfsops.c,v 1.59 2007/10/10 20:42:21 ad 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.59 2007/10/10 20:42:21 ad 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 coda_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 struct lwp *l) /* The ever-famous lwp pointer */ 158 { 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, struct lwp *l) 285 { 286 ENTRY; 287 vftomi(vfsp)->mi_started = 1; 288 return (0); 289 } 290 291 int 292 coda_unmount(struct mount *vfsp, int mntflags, struct lwp *l) 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 int 410 coda_quotactl(struct mount *vfsp, int cmd, uid_t uid, 411 void *arg, struct lwp *l) 412 { 413 ENTRY; 414 return (EOPNOTSUPP); 415 } 416 417 /* 418 * Get file system statistics. 419 */ 420 int 421 coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp, struct lwp *l) 422 { 423 struct coda_statfs fsstat; 424 int error; 425 426 ENTRY; 427 MARK_ENTRY(CODA_STATFS_STATS); 428 if (!CODA_MOUNTED(vfsp)) { 429 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 430 return(EINVAL); 431 } 432 433 /* XXX - what to do about f_flags, others? --bnoble */ 434 /* Below This is what AFS does 435 #define NB_SFS_SIZ 0x895440 436 */ 437 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 438 439 error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat); 440 441 if (!error) { 442 sbp->f_bsize = 8192; /* XXX */ 443 sbp->f_frsize = 8192; /* XXX */ 444 sbp->f_iosize = 8192; /* XXX */ 445 sbp->f_blocks = fsstat.f_blocks; 446 sbp->f_bfree = fsstat.f_bfree; 447 sbp->f_bavail = fsstat.f_bavail; 448 sbp->f_bresvd = 0; 449 sbp->f_files = fsstat.f_files; 450 sbp->f_ffree = fsstat.f_ffree; 451 sbp->f_favail = fsstat.f_ffree; 452 sbp->f_fresvd = 0; 453 copy_statvfs_info(sbp, vfsp); 454 } 455 456 MARK_INT_SAT(CODA_STATFS_STATS); 457 return(error); 458 } 459 460 /* 461 * Flush any pending I/O. 462 */ 463 int 464 coda_sync(struct mount *vfsp, int waitfor, 465 kauth_cred_t cred, struct lwp *l) 466 { 467 ENTRY; 468 MARK_ENTRY(CODA_SYNC_STATS); 469 MARK_INT_SAT(CODA_SYNC_STATS); 470 return(0); 471 } 472 473 int 474 coda_vget(struct mount *vfsp, ino_t ino, 475 struct vnode **vpp) 476 { 477 ENTRY; 478 return (EOPNOTSUPP); 479 } 480 481 /* 482 * fhtovp is now what vget used to be in 4.3-derived systems. For 483 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 484 * a type-specific fid. 485 */ 486 int 487 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam, 488 struct vnode **vpp, int *exflagsp, 489 kauth_cred_t *creadanonp) 490 { 491 struct cfid *cfid = (struct cfid *)fhp; 492 struct cnode *cp = 0; 493 int error; 494 struct lwp *l = curlwp; /* XXX -mach */ 495 CodaFid VFid; 496 int vtype; 497 498 ENTRY; 499 500 MARK_ENTRY(CODA_VGET_STATS); 501 /* Check for vget of control object. */ 502 if (IS_CTL_FID(&cfid->cfid_fid)) { 503 *vpp = coda_ctlvp; 504 vref(coda_ctlvp); 505 MARK_INT_SAT(CODA_VGET_STATS); 506 return(0); 507 } 508 509 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype); 510 511 if (error) { 512 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 513 *vpp = (struct vnode *)0; 514 } else { 515 CODADEBUG(CODA_VGET, 516 myprintf(("vget: %s type %d result %d\n", 517 coda_f2s(&VFid), vtype, error)); ) 518 519 cp = make_coda_node(&VFid, vfsp, vtype); 520 *vpp = CTOV(cp); 521 } 522 return(error); 523 } 524 525 int 526 coda_vptofh(struct vnode *vnp, struct fid *fidp) 527 { 528 ENTRY; 529 return (EOPNOTSUPP); 530 } 531 532 void 533 coda_init(void) 534 { 535 ENTRY; 536 } 537 538 void 539 coda_done(void) 540 { 541 ENTRY; 542 } 543 544 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 545 { 546 sysctl_createv(clog, 0, NULL, NULL, 547 CTLFLAG_PERMANENT, 548 CTLTYPE_NODE, "vfs", NULL, 549 NULL, 0, NULL, 0, 550 CTL_VFS, CTL_EOL); 551 sysctl_createv(clog, 0, NULL, NULL, 552 CTLFLAG_PERMANENT, 553 CTLTYPE_NODE, "coda", 554 SYSCTL_DESCR("code vfs options"), 555 NULL, 0, NULL, 0, 556 CTL_VFS, 18, CTL_EOL); 557 /* 558 * XXX the "18" above could be dynamic, thereby eliminating 559 * one more instance of the "number to vfs" mapping problem, 560 * but "18" is the order as taken from sys/mount.h 561 */ 562 563 /* 564 sysctl_createv(clog, 0, NULL, NULL, 565 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 566 CTLTYPE_INT, "clusterread", 567 SYSCTL_DESCR( anyone? ), 568 NULL, 0, &doclusterread, 0, 569 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 570 */ 571 } 572 573 /* 574 * To allow for greater ease of use, some vnodes may be orphaned when 575 * Venus dies. Certain operations should still be allowed to go 576 * through, but without propagating orphan-ness. So this function will 577 * get a new vnode for the file from the current run of Venus. 578 */ 579 580 int 581 getNewVnode(struct vnode **vpp) 582 { 583 struct cfid cfid; 584 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 585 586 ENTRY; 587 588 cfid.cfid_len = (short)sizeof(CodaFid); 589 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 590 /* XXX ? */ 591 592 /* We're guessing that if set, the 1st element on the list is a 593 * valid vnode to use. If not, return ENODEV as venus is dead. 594 */ 595 if (mi->mi_vfsp == NULL) 596 return ENODEV; 597 598 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 599 NULL, NULL); 600 } 601 602 #include <ufs/ufs/quota.h> 603 #include <ufs/ufs/ufsmount.h> 604 /* get the mount structure corresponding to a given device. Assume 605 * device corresponds to a UFS. Return NULL if no device is found. 606 */ 607 struct mount *devtomp(dev_t dev) 608 { 609 struct mount *mp, *nmp; 610 611 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 612 nmp = mp->mnt_list.cqe_next; 613 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 614 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 615 /* mount corresponds to UFS and the device matches one we want */ 616 return(mp); 617 } 618 } 619 /* mount structure wasn't found */ 620 return(NULL); 621 } 622