1 /* $NetBSD: coda_vfsops.c,v 1.62 2007/12/08 19:29:37 pooka 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.62 2007/12/08 19:29:37 pooka 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 (void *)eopnotsupp, /* vfs_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 { 158 struct lwp *l = curlwp; 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 default size for the filename buffer. 190 */ 191 /* Ensure that namei() doesn't run off the filename buffer */ 192 ((char *)data)[*data_len - 1] = 0; 193 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, data); 194 error = namei(&nd); 195 dvp = nd.ni_vp; 196 197 if (error) { 198 MARK_INT_FAIL(CODA_MOUNT_STATS); 199 return (error); 200 } 201 if (dvp->v_type != VCHR) { 202 MARK_INT_FAIL(CODA_MOUNT_STATS); 203 vrele(dvp); 204 return(ENXIO); 205 } 206 dev = dvp->v_specinfo->si_rdev; 207 vrele(dvp); 208 cdev = cdevsw_lookup(dev); 209 if (cdev == NULL) { 210 MARK_INT_FAIL(CODA_MOUNT_STATS); 211 return(ENXIO); 212 } 213 214 /* 215 * See if the device table matches our expectations. 216 */ 217 if (cdev != &vcoda_cdevsw) 218 { 219 MARK_INT_FAIL(CODA_MOUNT_STATS); 220 return(ENXIO); 221 } 222 223 if (minor(dev) >= NVCODA || minor(dev) < 0) { 224 MARK_INT_FAIL(CODA_MOUNT_STATS); 225 return(ENXIO); 226 } 227 228 /* 229 * Initialize the mount record and link it to the vfs struct 230 */ 231 mi = &coda_mnttbl[minor(dev)]; 232 233 if (!VC_OPEN(&mi->mi_vcomm)) { 234 MARK_INT_FAIL(CODA_MOUNT_STATS); 235 return(ENODEV); 236 } 237 238 /* No initialization (here) of mi_vcomm! */ 239 vfsp->mnt_data = mi; 240 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 241 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 242 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 243 vfsp->mnt_stat.f_namemax = MAXNAMLEN; 244 mi->mi_vfsp = vfsp; 245 246 /* 247 * Make a root vnode to placate the Vnode interface, but don't 248 * actually make the CODA_ROOT call to venus until the first call 249 * to coda_root in case a server is down while venus is starting. 250 */ 251 cp = make_coda_node(&rootfid, vfsp, VDIR); 252 rtvp = CTOV(cp); 253 rtvp->v_vflag |= VV_ROOT; 254 255 /* cp = make_coda_node(&ctlfid, vfsp, VCHR); 256 The above code seems to cause a loop in the cnode links. 257 I don't totally understand when it happens, it is caught 258 when closing down the system. 259 */ 260 cp = make_coda_node(&ctlfid, 0, VCHR); 261 262 coda_ctlvp = CTOV(cp); 263 264 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 265 mi->mi_vfsp = vfsp; 266 mi->mi_rootvp = rtvp; 267 268 /* set filesystem block size */ 269 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 270 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 271 272 /* error is currently guaranteed to be zero, but in case some 273 code changes... */ 274 CODADEBUG(1, 275 myprintf(("coda_mount returned %d\n",error));); 276 if (error) 277 MARK_INT_FAIL(CODA_MOUNT_STATS); 278 else 279 MARK_INT_SAT(CODA_MOUNT_STATS); 280 281 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, 282 vfsp->mnt_op->vfs_name, vfsp, l); 283 } 284 285 int 286 coda_start(struct mount *vfsp, int flags) 287 { 288 ENTRY; 289 vftomi(vfsp)->mi_started = 1; 290 return (0); 291 } 292 293 int 294 coda_unmount(struct mount *vfsp, int mntflags) 295 { 296 struct coda_mntinfo *mi = vftomi(vfsp); 297 int active, error = 0; 298 299 ENTRY; 300 MARK_ENTRY(CODA_UMOUNT_STATS); 301 if (!CODA_MOUNTED(vfsp)) { 302 MARK_INT_FAIL(CODA_UMOUNT_STATS); 303 return(EINVAL); 304 } 305 306 if (mi->mi_vfsp == vfsp) { /* We found the victim */ 307 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp))) 308 return (EBUSY); /* Venus is still running */ 309 310 #ifdef DEBUG 311 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp)); 312 #endif 313 mi->mi_started = 0; 314 315 vrele(mi->mi_rootvp); 316 317 active = coda_kill(vfsp, NOT_DOWNCALL); 318 mi->mi_rootvp->v_vflag &= ~VV_ROOT; 319 error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE); 320 printf("coda_unmount: active = %d, vflush active %d\n", active, error); 321 error = 0; 322 323 /* I'm going to take this out to allow lookups to go through. I'm 324 * not sure it's important anyway. -- DCS 2/2/94 325 */ 326 /* vfsp->VFS_DATA = NULL; */ 327 328 /* No more vfsp's to hold onto */ 329 mi->mi_vfsp = NULL; 330 mi->mi_rootvp = NULL; 331 332 if (error) 333 MARK_INT_FAIL(CODA_UMOUNT_STATS); 334 else 335 MARK_INT_SAT(CODA_UMOUNT_STATS); 336 337 return(error); 338 } 339 return (EINVAL); 340 } 341 342 /* 343 * find root of cfs 344 */ 345 int 346 coda_root(struct mount *vfsp, struct vnode **vpp) 347 { 348 struct coda_mntinfo *mi = vftomi(vfsp); 349 int error; 350 struct lwp *l = curlwp; /* 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), l->l_cred, l->l_proc, &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 /* 412 * Get file system statistics. 413 */ 414 int 415 coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp) 416 { 417 struct lwp *l = curlwp; 418 struct coda_statfs fsstat; 419 int error; 420 421 ENTRY; 422 MARK_ENTRY(CODA_STATFS_STATS); 423 if (!CODA_MOUNTED(vfsp)) { 424 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 425 return(EINVAL); 426 } 427 428 /* XXX - what to do about f_flags, others? --bnoble */ 429 /* Below This is what AFS does 430 #define NB_SFS_SIZ 0x895440 431 */ 432 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 433 434 error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat); 435 436 if (!error) { 437 sbp->f_bsize = 8192; /* XXX */ 438 sbp->f_frsize = 8192; /* XXX */ 439 sbp->f_iosize = 8192; /* XXX */ 440 sbp->f_blocks = fsstat.f_blocks; 441 sbp->f_bfree = fsstat.f_bfree; 442 sbp->f_bavail = fsstat.f_bavail; 443 sbp->f_bresvd = 0; 444 sbp->f_files = fsstat.f_files; 445 sbp->f_ffree = fsstat.f_ffree; 446 sbp->f_favail = fsstat.f_ffree; 447 sbp->f_fresvd = 0; 448 copy_statvfs_info(sbp, vfsp); 449 } 450 451 MARK_INT_SAT(CODA_STATFS_STATS); 452 return(error); 453 } 454 455 /* 456 * Flush any pending I/O. 457 */ 458 int 459 coda_sync(struct mount *vfsp, int waitfor, 460 kauth_cred_t cred) 461 { 462 ENTRY; 463 MARK_ENTRY(CODA_SYNC_STATS); 464 MARK_INT_SAT(CODA_SYNC_STATS); 465 return(0); 466 } 467 468 int 469 coda_vget(struct mount *vfsp, ino_t ino, 470 struct vnode **vpp) 471 { 472 ENTRY; 473 return (EOPNOTSUPP); 474 } 475 476 /* 477 * fhtovp is now what vget used to be in 4.3-derived systems. For 478 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 479 * a type-specific fid. 480 */ 481 int 482 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam, 483 struct vnode **vpp, int *exflagsp, 484 kauth_cred_t *creadanonp) 485 { 486 struct cfid *cfid = (struct cfid *)fhp; 487 struct cnode *cp = 0; 488 int error; 489 struct lwp *l = curlwp; /* XXX -mach */ 490 CodaFid VFid; 491 int vtype; 492 493 ENTRY; 494 495 MARK_ENTRY(CODA_VGET_STATS); 496 /* Check for vget of control object. */ 497 if (IS_CTL_FID(&cfid->cfid_fid)) { 498 *vpp = coda_ctlvp; 499 vref(coda_ctlvp); 500 MARK_INT_SAT(CODA_VGET_STATS); 501 return(0); 502 } 503 504 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype); 505 506 if (error) { 507 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 508 *vpp = (struct vnode *)0; 509 } else { 510 CODADEBUG(CODA_VGET, 511 myprintf(("vget: %s type %d result %d\n", 512 coda_f2s(&VFid), vtype, error)); ) 513 514 cp = make_coda_node(&VFid, vfsp, vtype); 515 *vpp = CTOV(cp); 516 } 517 return(error); 518 } 519 520 int 521 coda_vptofh(struct vnode *vnp, struct fid *fidp) 522 { 523 ENTRY; 524 return (EOPNOTSUPP); 525 } 526 527 void 528 coda_init(void) 529 { 530 ENTRY; 531 } 532 533 void 534 coda_done(void) 535 { 536 ENTRY; 537 } 538 539 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 540 { 541 sysctl_createv(clog, 0, NULL, NULL, 542 CTLFLAG_PERMANENT, 543 CTLTYPE_NODE, "vfs", NULL, 544 NULL, 0, NULL, 0, 545 CTL_VFS, CTL_EOL); 546 sysctl_createv(clog, 0, NULL, NULL, 547 CTLFLAG_PERMANENT, 548 CTLTYPE_NODE, "coda", 549 SYSCTL_DESCR("code vfs options"), 550 NULL, 0, NULL, 0, 551 CTL_VFS, 18, CTL_EOL); 552 /* 553 * XXX the "18" above could be dynamic, thereby eliminating 554 * one more instance of the "number to vfs" mapping problem, 555 * but "18" is the order as taken from sys/mount.h 556 */ 557 558 /* 559 sysctl_createv(clog, 0, NULL, NULL, 560 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 561 CTLTYPE_INT, "clusterread", 562 SYSCTL_DESCR( anyone? ), 563 NULL, 0, &doclusterread, 0, 564 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 565 */ 566 } 567 568 /* 569 * To allow for greater ease of use, some vnodes may be orphaned when 570 * Venus dies. Certain operations should still be allowed to go 571 * through, but without propagating orphan-ness. So this function will 572 * get a new vnode for the file from the current run of Venus. 573 */ 574 575 int 576 getNewVnode(struct vnode **vpp) 577 { 578 struct cfid cfid; 579 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 580 581 ENTRY; 582 583 cfid.cfid_len = (short)sizeof(CodaFid); 584 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 585 /* XXX ? */ 586 587 /* We're guessing that if set, the 1st element on the list is a 588 * valid vnode to use. If not, return ENODEV as venus is dead. 589 */ 590 if (mi->mi_vfsp == NULL) 591 return ENODEV; 592 593 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 594 NULL, NULL); 595 } 596 597 #include <ufs/ufs/quota.h> 598 #include <ufs/ufs/ufsmount.h> 599 /* get the mount structure corresponding to a given device. Assume 600 * device corresponds to a UFS. Return NULL if no device is found. 601 */ 602 struct mount *devtomp(dev_t dev) 603 { 604 struct mount *mp, *nmp; 605 606 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 607 nmp = mp->mnt_list.cqe_next; 608 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 609 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 610 /* mount corresponds to UFS and the device matches one we want */ 611 return(mp); 612 } 613 } 614 /* mount structure wasn't found */ 615 return(NULL); 616 } 617