1 /* $NetBSD: coda_vfsops.c,v 1.51 2006/10/12 01:30:47 christos 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.51 2006/10/12 01:30:47 christos 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 coda_mount, 107 coda_start, 108 coda_unmount, 109 coda_root, 110 coda_quotactl, 111 coda_nb_statvfs, 112 coda_sync, 113 coda_vget, 114 NULL, /* vfs_fhtovp */ 115 NULL, /* vfs_vptofh */ 116 coda_init, 117 NULL, /* vfs_reinit */ 118 coda_done, 119 (int (*)(void)) eopnotsupp, 120 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 121 vfs_stdextattrctl, 122 coda_vnodeopv_descs, 123 0, /* vfs_refcount */ 124 { NULL, NULL }, /* vfs_list */ 125 }; 126 127 VFS_ATTACH(coda_vfsops); 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(struct mount *vfsp, /* Allocated and initialized by mount(2) */ 152 const char *path __unused, /* path covered: ignored by the fs-layer */ 153 void *data, /* Need to define a data type for this in netbsd? */ 154 struct nameidata *ndp, /* Clobber this to lookup the device name */ 155 struct lwp *l) /* The ever-famous lwp pointer */ 156 { 157 struct vnode *dvp; 158 struct cnode *cp; 159 dev_t dev; 160 struct coda_mntinfo *mi; 161 struct vnode *rtvp; 162 const struct cdevsw *cdev; 163 CodaFid rootfid = INVAL_FID; 164 CodaFid ctlfid = CTL_FID; 165 int error; 166 167 if (vfsp->mnt_flag & MNT_GETARGS) 168 return 0; 169 ENTRY; 170 171 coda_vfsopstats_init(); 172 coda_vnodeopstats_init(); 173 174 MARK_ENTRY(CODA_MOUNT_STATS); 175 if (CODA_MOUNTED(vfsp)) { 176 MARK_INT_FAIL(CODA_MOUNT_STATS); 177 return(EBUSY); 178 } 179 180 /* Validate mount device. Similar to getmdev(). */ 181 182 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, data, l); 183 error = namei(ndp); 184 dvp = ndp->ni_vp; 185 186 if (error) { 187 MARK_INT_FAIL(CODA_MOUNT_STATS); 188 return (error); 189 } 190 if (dvp->v_type != VCHR) { 191 MARK_INT_FAIL(CODA_MOUNT_STATS); 192 vrele(dvp); 193 return(ENXIO); 194 } 195 dev = dvp->v_specinfo->si_rdev; 196 vrele(dvp); 197 cdev = cdevsw_lookup(dev); 198 if (cdev == NULL) { 199 MARK_INT_FAIL(CODA_MOUNT_STATS); 200 return(ENXIO); 201 } 202 203 /* 204 * See if the device table matches our expectations. 205 */ 206 if (cdev != &vcoda_cdevsw) 207 { 208 MARK_INT_FAIL(CODA_MOUNT_STATS); 209 return(ENXIO); 210 } 211 212 if (minor(dev) >= NVCODA || minor(dev) < 0) { 213 MARK_INT_FAIL(CODA_MOUNT_STATS); 214 return(ENXIO); 215 } 216 217 /* 218 * Initialize the mount record and link it to the vfs struct 219 */ 220 mi = &coda_mnttbl[minor(dev)]; 221 222 if (!VC_OPEN(&mi->mi_vcomm)) { 223 MARK_INT_FAIL(CODA_MOUNT_STATS); 224 return(ENODEV); 225 } 226 227 /* No initialization (here) of mi_vcomm! */ 228 vfsp->mnt_data = mi; 229 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = 0; 230 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CODA); 231 vfsp->mnt_stat.f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 232 vfsp->mnt_stat.f_namemax = MAXNAMLEN; 233 mi->mi_vfsp = vfsp; 234 235 /* 236 * Make a root vnode to placate the Vnode interface, but don't 237 * actually make the CODA_ROOT call to venus until the first call 238 * to coda_root in case a server is down while venus is starting. 239 */ 240 cp = make_coda_node(&rootfid, vfsp, VDIR); 241 rtvp = CTOV(cp); 242 rtvp->v_flag |= VROOT; 243 244 /* cp = make_coda_node(&ctlfid, vfsp, VCHR); 245 The above code seems to cause a loop in the cnode links. 246 I don't totally understand when it happens, it is caught 247 when closing down the system. 248 */ 249 cp = make_coda_node(&ctlfid, 0, VCHR); 250 251 coda_ctlvp = CTOV(cp); 252 253 /* Add vfs and rootvp to chain of vfs hanging off mntinfo */ 254 mi->mi_vfsp = vfsp; 255 mi->mi_rootvp = rtvp; 256 257 /* set filesystem block size */ 258 vfsp->mnt_stat.f_bsize = 8192; /* XXX -JJK */ 259 vfsp->mnt_stat.f_frsize = 8192; /* XXX -JJK */ 260 261 /* error is currently guaranteed to be zero, but in case some 262 code changes... */ 263 CODADEBUG(1, 264 myprintf(("coda_mount returned %d\n",error));); 265 if (error) 266 MARK_INT_FAIL(CODA_MOUNT_STATS); 267 else 268 MARK_INT_SAT(CODA_MOUNT_STATS); 269 270 return set_statvfs_info("/coda", UIO_SYSSPACE, "CODA", UIO_SYSSPACE, vfsp, 271 l); 272 } 273 274 int 275 coda_start(struct mount *vfsp, int flags __unused, struct lwp *l __unused) 276 { 277 ENTRY; 278 vftomi(vfsp)->mi_started = 1; 279 return (0); 280 } 281 282 int 283 coda_unmount(struct mount *vfsp, int mntflags __unused, struct lwp *l __unused) 284 { 285 struct coda_mntinfo *mi = vftomi(vfsp); 286 int active, error = 0; 287 288 ENTRY; 289 MARK_ENTRY(CODA_UMOUNT_STATS); 290 if (!CODA_MOUNTED(vfsp)) { 291 MARK_INT_FAIL(CODA_UMOUNT_STATS); 292 return(EINVAL); 293 } 294 295 if (mi->mi_vfsp == vfsp) { /* We found the victim */ 296 if (!IS_UNMOUNTING(VTOC(mi->mi_rootvp))) 297 return (EBUSY); /* Venus is still running */ 298 299 #ifdef DEBUG 300 printf("coda_unmount: ROOT: vp %p, cp %p\n", mi->mi_rootvp, VTOC(mi->mi_rootvp)); 301 #endif 302 mi->mi_started = 0; 303 304 vrele(mi->mi_rootvp); 305 306 active = coda_kill(vfsp, NOT_DOWNCALL); 307 mi->mi_rootvp->v_flag &= ~VROOT; 308 error = vflush(mi->mi_vfsp, NULLVP, FORCECLOSE); 309 printf("coda_unmount: active = %d, vflush active %d\n", active, error); 310 error = 0; 311 312 /* I'm going to take this out to allow lookups to go through. I'm 313 * not sure it's important anyway. -- DCS 2/2/94 314 */ 315 /* vfsp->VFS_DATA = NULL; */ 316 317 /* No more vfsp's to hold onto */ 318 mi->mi_vfsp = NULL; 319 mi->mi_rootvp = NULL; 320 321 if (error) 322 MARK_INT_FAIL(CODA_UMOUNT_STATS); 323 else 324 MARK_INT_SAT(CODA_UMOUNT_STATS); 325 326 return(error); 327 } 328 return (EINVAL); 329 } 330 331 /* 332 * find root of cfs 333 */ 334 int 335 coda_root(struct mount *vfsp, struct vnode **vpp) 336 { 337 struct coda_mntinfo *mi = vftomi(vfsp); 338 int error; 339 struct lwp *l = curlwp; /* XXX - bnoble */ 340 CodaFid VFid; 341 static const CodaFid invalfid = INVAL_FID; 342 343 ENTRY; 344 MARK_ENTRY(CODA_ROOT_STATS); 345 346 if (vfsp == mi->mi_vfsp) { 347 if (memcmp(&VTOC(mi->mi_rootvp)->c_fid, &invalfid, sizeof(CodaFid))) 348 { /* Found valid root. */ 349 *vpp = mi->mi_rootvp; 350 /* On Mach, this is vref. On NetBSD, VOP_LOCK */ 351 vref(*vpp); 352 vn_lock(*vpp, LK_EXCLUSIVE); 353 MARK_INT_SAT(CODA_ROOT_STATS); 354 return(0); 355 } 356 } 357 358 error = venus_root(vftomi(vfsp), l->l_cred, l->l_proc, &VFid); 359 360 if (!error) { 361 /* 362 * Save the new rootfid in the cnode, and rehash the cnode into the 363 * cnode hash with the new fid key. 364 */ 365 coda_unsave(VTOC(mi->mi_rootvp)); 366 VTOC(mi->mi_rootvp)->c_fid = VFid; 367 coda_save(VTOC(mi->mi_rootvp)); 368 369 *vpp = mi->mi_rootvp; 370 vref(*vpp); 371 vn_lock(*vpp, LK_EXCLUSIVE); 372 MARK_INT_SAT(CODA_ROOT_STATS); 373 goto exit; 374 } else if (error == ENODEV || error == EINTR) { 375 /* Gross hack here! */ 376 /* 377 * If Venus fails to respond to the CODA_ROOT call, coda_call returns 378 * ENODEV. Return the uninitialized root vnode to allow vfs 379 * operations such as unmount to continue. Without this hack, 380 * there is no way to do an unmount if Venus dies before a 381 * successful CODA_ROOT call is done. All vnode operations 382 * will fail. 383 */ 384 *vpp = mi->mi_rootvp; 385 vref(*vpp); 386 vn_lock(*vpp, LK_EXCLUSIVE); 387 MARK_INT_FAIL(CODA_ROOT_STATS); 388 error = 0; 389 goto exit; 390 } else { 391 CODADEBUG( CODA_ROOT, myprintf(("error %d in CODA_ROOT\n", error)); ); 392 MARK_INT_FAIL(CODA_ROOT_STATS); 393 394 goto exit; 395 } 396 exit: 397 return(error); 398 } 399 400 int 401 coda_quotactl(struct mount *vfsp __unused, int cmd __unused, uid_t uid __unused, 402 void *arg __unused, struct lwp *l __unused) 403 { 404 ENTRY; 405 return (EOPNOTSUPP); 406 } 407 408 /* 409 * Get file system statistics. 410 */ 411 int 412 coda_nb_statvfs(struct mount *vfsp, struct statvfs *sbp, struct lwp *l) 413 { 414 struct coda_statfs fsstat; 415 int error; 416 417 ENTRY; 418 MARK_ENTRY(CODA_STATFS_STATS); 419 if (!CODA_MOUNTED(vfsp)) { 420 /* MARK_INT_FAIL(CODA_STATFS_STATS); */ 421 return(EINVAL); 422 } 423 424 /* XXX - what to do about f_flags, others? --bnoble */ 425 /* Below This is what AFS does 426 #define NB_SFS_SIZ 0x895440 427 */ 428 /* Note: Normal fs's have a bsize of 0x400 == 1024 */ 429 430 error = venus_statfs(vftomi(vfsp), l->l_cred, l, &fsstat); 431 432 if (!error) { 433 sbp->f_bsize = 8192; /* XXX */ 434 sbp->f_frsize = 8192; /* XXX */ 435 sbp->f_iosize = 8192; /* XXX */ 436 sbp->f_blocks = fsstat.f_blocks; 437 sbp->f_bfree = fsstat.f_bfree; 438 sbp->f_bavail = fsstat.f_bavail; 439 sbp->f_bresvd = 0; 440 sbp->f_files = fsstat.f_files; 441 sbp->f_ffree = fsstat.f_ffree; 442 sbp->f_favail = fsstat.f_ffree; 443 sbp->f_fresvd = 0; 444 copy_statvfs_info(sbp, vfsp); 445 } 446 447 MARK_INT_SAT(CODA_STATFS_STATS); 448 return(error); 449 } 450 451 /* 452 * Flush any pending I/O. 453 */ 454 int 455 coda_sync(struct mount *vfsp __unused, int waitfor __unused, 456 kauth_cred_t cred __unused, struct lwp *l __unused) 457 { 458 ENTRY; 459 MARK_ENTRY(CODA_SYNC_STATS); 460 MARK_INT_SAT(CODA_SYNC_STATS); 461 return(0); 462 } 463 464 int 465 coda_vget(struct mount *vfsp __unused, ino_t ino __unused, 466 struct vnode **vpp __unused) 467 { 468 ENTRY; 469 return (EOPNOTSUPP); 470 } 471 472 /* 473 * fhtovp is now what vget used to be in 4.3-derived systems. For 474 * some silly reason, vget is now keyed by a 32 bit ino_t, rather than 475 * a type-specific fid. 476 */ 477 int 478 coda_fhtovp(struct mount *vfsp, struct fid *fhp, struct mbuf *nam __unused, 479 struct vnode **vpp, int *exflagsp __unused, 480 kauth_cred_t *creadanonp __unused) 481 { 482 struct cfid *cfid = (struct cfid *)fhp; 483 struct cnode *cp = 0; 484 int error; 485 struct lwp *l = curlwp; /* XXX -mach */ 486 CodaFid VFid; 487 int vtype; 488 489 ENTRY; 490 491 MARK_ENTRY(CODA_VGET_STATS); 492 /* Check for vget of control object. */ 493 if (IS_CTL_FID(&cfid->cfid_fid)) { 494 *vpp = coda_ctlvp; 495 vref(coda_ctlvp); 496 MARK_INT_SAT(CODA_VGET_STATS); 497 return(0); 498 } 499 500 error = venus_fhtovp(vftomi(vfsp), &cfid->cfid_fid, l->l_cred, l->l_proc, &VFid, &vtype); 501 502 if (error) { 503 CODADEBUG(CODA_VGET, myprintf(("vget error %d\n",error));) 504 *vpp = (struct vnode *)0; 505 } else { 506 CODADEBUG(CODA_VGET, 507 myprintf(("vget: %s type %d result %d\n", 508 coda_f2s(&VFid), vtype, error)); ) 509 510 cp = make_coda_node(&VFid, vfsp, vtype); 511 *vpp = CTOV(cp); 512 } 513 return(error); 514 } 515 516 int 517 coda_vptofh(struct vnode *vnp __unused, struct fid *fidp __unused) 518 { 519 ENTRY; 520 return (EOPNOTSUPP); 521 } 522 523 void 524 coda_init(void) 525 { 526 ENTRY; 527 } 528 529 void 530 coda_done(void) 531 { 532 ENTRY; 533 } 534 535 SYSCTL_SETUP(sysctl_vfs_coda_setup, "sysctl vfs.coda subtree setup") 536 { 537 sysctl_createv(clog, 0, NULL, NULL, 538 CTLFLAG_PERMANENT, 539 CTLTYPE_NODE, "vfs", NULL, 540 NULL, 0, NULL, 0, 541 CTL_VFS, CTL_EOL); 542 sysctl_createv(clog, 0, NULL, NULL, 543 CTLFLAG_PERMANENT, 544 CTLTYPE_NODE, "coda", 545 SYSCTL_DESCR("code vfs options"), 546 NULL, 0, NULL, 0, 547 CTL_VFS, 18, CTL_EOL); 548 /* 549 * XXX the "18" above could be dynamic, thereby eliminating 550 * one more instance of the "number to vfs" mapping problem, 551 * but "18" is the order as taken from sys/mount.h 552 */ 553 554 /* 555 sysctl_createv(clog, 0, NULL, NULL, 556 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 557 CTLTYPE_INT, "clusterread", 558 SYSCTL_DESCR( anyone? ), 559 NULL, 0, &doclusterread, 0, 560 CTL_VFS, 18, FFS_CLUSTERREAD, CTL_EOL); 561 */ 562 } 563 564 /* 565 * To allow for greater ease of use, some vnodes may be orphaned when 566 * Venus dies. Certain operations should still be allowed to go 567 * through, but without propagating orphan-ness. So this function will 568 * get a new vnode for the file from the current run of Venus. 569 */ 570 571 int 572 getNewVnode(struct vnode **vpp) 573 { 574 struct cfid cfid; 575 struct coda_mntinfo *mi = vftomi((*vpp)->v_mount); 576 577 ENTRY; 578 579 cfid.cfid_len = (short)sizeof(CodaFid); 580 cfid.cfid_fid = VTOC(*vpp)->c_fid; /* Structure assignment. */ 581 /* XXX ? */ 582 583 /* We're guessing that if set, the 1st element on the list is a 584 * valid vnode to use. If not, return ENODEV as venus is dead. 585 */ 586 if (mi->mi_vfsp == NULL) 587 return ENODEV; 588 589 return coda_fhtovp(mi->mi_vfsp, (struct fid*)&cfid, NULL, vpp, 590 NULL, NULL); 591 } 592 593 #include <ufs/ufs/quota.h> 594 #include <ufs/ufs/ufsmount.h> 595 /* get the mount structure corresponding to a given device. Assume 596 * device corresponds to a UFS. Return NULL if no device is found. 597 */ 598 struct mount *devtomp(dev_t dev) 599 { 600 struct mount *mp, *nmp; 601 602 for (mp = mountlist.cqh_first; mp != (void*)&mountlist; mp = nmp) { 603 nmp = mp->mnt_list.cqe_next; 604 if ((!strcmp(mp->mnt_op->vfs_name, MOUNT_UFS)) && 605 ((VFSTOUFS(mp))->um_dev == (dev_t) dev)) { 606 /* mount corresponds to UFS and the device matches one we want */ 607 return(mp); 608 } 609 } 610 /* mount structure wasn't found */ 611 return(NULL); 612 } 613