1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/sysmacros.h> 30 #include <sys/kmem.h> 31 #include <sys/pathname.h> 32 #include <sys/vnode.h> 33 #include <sys/vfs.h> 34 #include <sys/vfs_opreg.h> 35 #include <sys/mntent.h> 36 #include <sys/mount.h> 37 #include <sys/cmn_err.h> 38 #include <sys/zfs_znode.h> 39 #include <sys/zfs_dir.h> 40 #include <sys/zil.h> 41 #include <sys/fs/zfs.h> 42 #include <sys/dmu.h> 43 #include <sys/dsl_prop.h> 44 #include <sys/dsl_dataset.h> 45 #include <sys/dsl_deleg.h> 46 #include <sys/spa.h> 47 #include <sys/zap.h> 48 #include <sys/varargs.h> 49 #include <sys/policy.h> 50 #include <sys/atomic.h> 51 #include <sys/mkdev.h> 52 #include <sys/modctl.h> 53 #include <sys/zfs_ioctl.h> 54 #include <sys/zfs_ctldir.h> 55 #include <sys/zfs_fuid.h> 56 #include <sys/sunddi.h> 57 #include <sys/dnlc.h> 58 #include <sys/dmu_objset.h> 59 #include <sys/spa_boot.h> 60 61 #ifdef __NetBSD__ 62 /* include ddi_name_to_major function is there better place for it ?*/ 63 #include <sys/ddi.h> 64 #include <sys/systm.h> 65 #endif 66 67 int zfsfstype; 68 vfsops_t *zfs_vfsops = NULL; 69 static major_t zfs_major; 70 static minor_t zfs_minor; 71 static kmutex_t zfs_dev_mtx; 72 73 int zfs_debug_level; 74 kmutex_t zfs_debug_mtx; 75 76 /* XXX NetBSD static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);*/ 77 static int zfs_mount(vfs_t *vfsp, const char *path, void *data, size_t *data_len); 78 static int zfs_umount(vfs_t *vfsp, int fflag); 79 static int zfs_root(vfs_t *vfsp, vnode_t **vpp); 80 static int zfs_statvfs(vfs_t *vfsp, struct statvfs *statp); 81 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp); 82 static int zfs_vget(vfs_t *vfsp, ino_t ino, vnode_t **vpp); 83 static int zfs_start(vfs_t *vfsp, int flags); 84 static void zfs_freevfs(vfs_t *vfsp); 85 86 void zfs_init(void); 87 void zfs_fini(void); 88 89 90 extern const struct vnodeopv_desc zfs_vnodeop_opv_desc; 91 92 static const struct vnodeopv_desc * const zfs_vnodeop_descs[] = { 93 &zfs_vnodeop_opv_desc, 94 NULL, 95 }; 96 97 static struct vfsops zfs_vfsops_template = { 98 .vfs_name = MOUNT_ZFS, 99 .vfs_min_mount_data = sizeof(struct zfs_args), 100 .vfs_opv_descs = zfs_vnodeop_descs, 101 .vfs_mount = zfs_mount, 102 .vfs_unmount = zfs_umount, 103 .vfs_root = zfs_root, 104 .vfs_statvfs = zfs_statvfs, 105 .vfs_sync = zfs_sync, 106 .vfs_vget = zfs_vget, 107 .vfs_fhtovp = zfs_fhtovp, 108 .vfs_init = zfs_init, 109 .vfs_done = zfs_fini, 110 .vfs_start = zfs_start, 111 .vfs_renamelock_enter = (void*)nullop, 112 .vfs_renamelock_exit = (void*)nullop, 113 .vfs_reinit = (void *)nullop, 114 .vfs_vptofh = (void *)eopnotsupp, 115 .vfs_fhtovp = (void *)eopnotsupp, 116 .vfs_quotactl = (void *)eopnotsupp, 117 .vfs_extattrctl = (void *)eopnotsupp, 118 .vfs_snapshot = (void *)eopnotsupp, 119 .vfs_fsync = (void *)eopnotsupp, 120 }; 121 122 /* 123 * We need to keep a count of active fs's. 124 * This is necessary to prevent our module 125 * from being unloaded after a umount -f 126 */ 127 static uint32_t zfs_active_fs_count = 0; 128 129 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL }; 130 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL }; 131 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL }; 132 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL }; 133 134 /* 135 * MO_DEFAULT is not used since the default value is determined 136 * by the equivalent property. 137 */ 138 static mntopt_t mntopts[] = { 139 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL }, 140 { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL }, 141 { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL }, 142 { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL } 143 }; 144 145 static mntopts_t zfs_mntopts = { 146 sizeof (mntopts) / sizeof (mntopt_t), 147 mntopts 148 }; 149 150 /*ARGSUSED*/ 151 int 152 zfs_sync(vfs_t *vfsp, int flag, cred_t *cr) 153 { 154 zfsvfs_t *zfsvfs = vfsp->vfs_data; 155 znode_t *zp; 156 vnode_t *vp; 157 struct vnode_iterator *marker; 158 dmu_tx_t *tx; 159 int error; 160 161 162 error = 0; 163 164 /* 165 * Data integrity is job one. We don't want a compromised kernel 166 * writing to the storage pool, so we never sync during panic. 167 */ 168 if (panicstr) 169 return (0); 170 171 /* 172 * On NetBSD, we need to push out atime updates. Solaris does 173 * this during VOP_INACTIVE, but that does not work well with the 174 * BSD VFS, so we do it in batch here. 175 */ 176 vfs_vnode_iterator_init(vfsp, &marker); 177 while (vfs_vnode_iterator_next(marker, &vp)) { 178 error = vn_lock(vp, LK_EXCLUSIVE); 179 if (error) { 180 vrele(vp); 181 continue; 182 } 183 /* 184 * Skip the vnode/inode if inaccessible, or if the 185 * atime is clean. 186 */ 187 zp = VTOZ(vp); 188 if (zp == NULL || vp->v_type == VNON || 189 zp->z_atime_dirty == 0 || zp->z_unlinked) { 190 vput(vp); 191 continue; 192 } 193 tx = dmu_tx_create(zfsvfs->z_os); 194 dmu_tx_hold_bonus(tx, zp->z_id); 195 error = dmu_tx_assign(tx, TXG_WAIT); 196 if (error) { 197 dmu_tx_abort(tx); 198 } else { 199 dmu_buf_will_dirty(zp->z_dbuf, tx); 200 mutex_enter(&zp->z_lock); 201 zp->z_atime_dirty = 0; 202 mutex_exit(&zp->z_lock); 203 dmu_tx_commit(tx); 204 } 205 vput(vp); 206 } 207 vfs_vnode_iterator_destroy(marker); 208 209 /* 210 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS 211 * to sync metadata, which they would otherwise cache indefinitely. 212 * Semantically, the only requirement is that the sync be initiated. 213 * The DMU syncs out txgs frequently, so there's nothing to do. 214 */ 215 if ((flag & MNT_LAZY) != 0) 216 return (0); 217 218 if (vfsp != NULL) { 219 /* 220 * Sync a specific filesystem. 221 */ 222 zfsvfs_t *zfsvfs = vfsp->vfs_data; 223 dsl_pool_t *dp; 224 225 ZFS_ENTER(zfsvfs); 226 dp = dmu_objset_pool(zfsvfs->z_os); 227 228 /* 229 * If the system is shutting down, then skip any 230 * filesystems which may exist on a suspended pool. 231 */ 232 if (sys_shutdown && spa_suspended(dp->dp_spa)) { 233 ZFS_EXIT(zfsvfs); 234 return (0); 235 } 236 237 if (zfsvfs->z_log != NULL) 238 zil_commit(zfsvfs->z_log, UINT64_MAX, 0); 239 else 240 txg_wait_synced(dp, 0); 241 ZFS_EXIT(zfsvfs); 242 } else { 243 /* 244 * Sync all ZFS filesystems. This is what happens when you 245 * run sync(1M). Unlike other filesystems, ZFS honors the 246 * request by waiting for all pools to commit all dirty data. 247 */ 248 spa_sync_allpools(); 249 } 250 251 return (0); 252 } 253 254 static int 255 zfs_create_unique_device(dev_t *dev) 256 { 257 major_t new_major; 258 259 do { 260 ASSERT3U(zfs_minor, <=, MAXMIN); 261 minor_t start = zfs_minor; 262 do { 263 mutex_enter(&zfs_dev_mtx); 264 if (zfs_minor >= MAXMIN) { 265 /* 266 * If we're still using the real major 267 * keep out of /dev/zfs and /dev/zvol minor 268 * number space. If we're using a getudev()'ed 269 * major number, we can use all of its minors. 270 */ 271 if (zfs_major == ddi_name_to_major(ZFS_DRIVER)) 272 zfs_minor = ZFS_MIN_MINOR; 273 else 274 zfs_minor = 0; 275 } else { 276 zfs_minor++; 277 } 278 *dev = makedevice(zfs_major, zfs_minor); 279 mutex_exit(&zfs_dev_mtx); 280 } while (vfs_devismounted(*dev) && zfs_minor != start); 281 break; 282 #ifndef __NetBSD__ 283 if (zfs_minor == start) { 284 /* 285 * We are using all ~262,000 minor numbers for the 286 * current major number. Create a new major number. 287 */ 288 if ((new_major = getudev()) == (major_t)-1) { 289 cmn_err(CE_WARN, 290 "zfs_mount: Can't get unique major " 291 "device number."); 292 return (-1); 293 } 294 mutex_enter(&zfs_dev_mtx); 295 zfs_major = new_major; 296 zfs_minor = 0; 297 298 mutex_exit(&zfs_dev_mtx); 299 } else { 300 break; 301 } 302 /* CONSTANTCONDITION */ 303 #endif 304 } while (1); 305 306 return (0); 307 } 308 309 static void 310 atime_changed_cb(void *arg, uint64_t newval) 311 { 312 zfsvfs_t *zfsvfs = arg; 313 314 if (newval == TRUE) { 315 zfsvfs->z_atime = TRUE; 316 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME); 317 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0); 318 } else { 319 zfsvfs->z_atime = FALSE; 320 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME); 321 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0); 322 } 323 } 324 325 static void 326 xattr_changed_cb(void *arg, uint64_t newval) 327 { 328 zfsvfs_t *zfsvfs = arg; 329 330 if (newval == TRUE) { 331 /* XXX locking on vfs_flag? */ 332 #ifdef TODO 333 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR; 334 #endif 335 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR); 336 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0); 337 } else { 338 /* XXX locking on vfs_flag? */ 339 #ifdef TODO 340 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR; 341 #endif 342 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR); 343 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0); 344 } 345 } 346 347 static void 348 blksz_changed_cb(void *arg, uint64_t newval) 349 { 350 zfsvfs_t *zfsvfs = arg; 351 352 if (newval < SPA_MINBLOCKSIZE || 353 newval > SPA_MAXBLOCKSIZE || !ISP2(newval)) 354 newval = SPA_MAXBLOCKSIZE; 355 356 zfsvfs->z_max_blksz = newval; 357 zfsvfs->z_vfs->vfs_bsize = newval; 358 } 359 360 static void 361 readonly_changed_cb(void *arg, uint64_t newval) 362 { 363 zfsvfs_t *zfsvfs = arg; 364 365 if (newval) { 366 /* XXX locking on vfs_flag? */ 367 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY; 368 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW); 369 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0); 370 } else { 371 /* XXX locking on vfs_flag? */ 372 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 373 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO); 374 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0); 375 } 376 } 377 378 static void 379 devices_changed_cb(void *arg, uint64_t newval) 380 { 381 zfsvfs_t *zfsvfs = arg; 382 383 if (newval == FALSE) { 384 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES; 385 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES); 386 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0); 387 } else { 388 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES; 389 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES); 390 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0); 391 } 392 } 393 394 static void 395 setuid_changed_cb(void *arg, uint64_t newval) 396 { 397 zfsvfs_t *zfsvfs = arg; 398 399 if (newval == FALSE) { 400 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID; 401 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID); 402 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0); 403 } else { 404 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID; 405 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID); 406 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0); 407 } 408 } 409 410 static void 411 exec_changed_cb(void *arg, uint64_t newval) 412 { 413 zfsvfs_t *zfsvfs = arg; 414 415 if (newval == FALSE) { 416 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC; 417 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC); 418 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0); 419 } else { 420 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC; 421 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC); 422 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0); 423 } 424 } 425 426 /* 427 * The nbmand mount option can be changed at mount time. 428 * We can't allow it to be toggled on live file systems or incorrect 429 * behavior may be seen from cifs clients 430 * 431 * This property isn't registered via dsl_prop_register(), but this callback 432 * will be called when a file system is first mounted 433 */ 434 static void 435 nbmand_changed_cb(void *arg, uint64_t newval) 436 { 437 zfsvfs_t *zfsvfs = arg; 438 if (newval == FALSE) { 439 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND); 440 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0); 441 } else { 442 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND); 443 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0); 444 } 445 } 446 447 static void 448 snapdir_changed_cb(void *arg, uint64_t newval) 449 { 450 zfsvfs_t *zfsvfs = arg; 451 452 zfsvfs->z_show_ctldir = newval; 453 } 454 455 static void 456 vscan_changed_cb(void *arg, uint64_t newval) 457 { 458 zfsvfs_t *zfsvfs = arg; 459 460 zfsvfs->z_vscan = newval; 461 } 462 463 static void 464 acl_mode_changed_cb(void *arg, uint64_t newval) 465 { 466 zfsvfs_t *zfsvfs = arg; 467 468 zfsvfs->z_acl_mode = newval; 469 } 470 471 static void 472 acl_inherit_changed_cb(void *arg, uint64_t newval) 473 { 474 zfsvfs_t *zfsvfs = arg; 475 476 zfsvfs->z_acl_inherit = newval; 477 } 478 479 static int 480 zfs_register_callbacks(vfs_t *vfsp) 481 { 482 struct dsl_dataset *ds = NULL; 483 objset_t *os = NULL; 484 zfsvfs_t *zfsvfs = NULL; 485 uint64_t nbmand; 486 int readonly, do_readonly = B_FALSE; 487 int setuid, do_setuid = B_FALSE; 488 int exec, do_exec = B_FALSE; 489 int devices, do_devices = B_FALSE; 490 int xattr, do_xattr = B_FALSE; 491 int atime, do_atime = B_FALSE; 492 int error = 0; 493 494 ASSERT(vfsp); 495 zfsvfs = vfsp->vfs_data; 496 ASSERT(zfsvfs); 497 os = zfsvfs->z_os; 498 499 /* 500 * The act of registering our callbacks will destroy any mount 501 * options we may have. In order to enable temporary overrides 502 * of mount options, we stash away the current values and 503 * restore them after we register the callbacks. 504 */ 505 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) { 506 readonly = B_TRUE; 507 do_readonly = B_TRUE; 508 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 509 readonly = B_FALSE; 510 do_readonly = B_TRUE; 511 } 512 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 513 devices = B_FALSE; 514 setuid = B_FALSE; 515 do_devices = B_TRUE; 516 do_setuid = B_TRUE; 517 } else { 518 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) { 519 devices = B_FALSE; 520 do_devices = B_TRUE; 521 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) { 522 devices = B_TRUE; 523 do_devices = B_TRUE; 524 } 525 526 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) { 527 setuid = B_FALSE; 528 do_setuid = B_TRUE; 529 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) { 530 setuid = B_TRUE; 531 do_setuid = B_TRUE; 532 } 533 } 534 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) { 535 exec = B_FALSE; 536 do_exec = B_TRUE; 537 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) { 538 exec = B_TRUE; 539 do_exec = B_TRUE; 540 } 541 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) { 542 xattr = B_FALSE; 543 do_xattr = B_TRUE; 544 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) { 545 xattr = B_TRUE; 546 do_xattr = B_TRUE; 547 } 548 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) { 549 atime = B_FALSE; 550 do_atime = B_TRUE; 551 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) { 552 atime = B_TRUE; 553 do_atime = B_TRUE; 554 } 555 556 /* 557 * nbmand is a special property. It can only be changed at 558 * mount time. 559 * 560 * This is weird, but it is documented to only be changeable 561 * at mount time. 562 */ 563 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) { 564 nbmand = B_FALSE; 565 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) { 566 nbmand = B_TRUE; 567 } else { 568 char osname[MAXNAMELEN]; 569 570 dmu_objset_name(os, osname); 571 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand, 572 NULL)) { 573 return (error); 574 } 575 } 576 577 /* 578 * Register property callbacks. 579 * 580 * It would probably be fine to just check for i/o error from 581 * the first prop_register(), but I guess I like to go 582 * overboard... 583 */ 584 ds = dmu_objset_ds(os); 585 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs); 586 error = error ? error : dsl_prop_register(ds, 587 "xattr", xattr_changed_cb, zfsvfs); 588 error = error ? error : dsl_prop_register(ds, 589 "recordsize", blksz_changed_cb, zfsvfs); 590 error = error ? error : dsl_prop_register(ds, 591 "readonly", readonly_changed_cb, zfsvfs); 592 error = error ? error : dsl_prop_register(ds, 593 "devices", devices_changed_cb, zfsvfs); 594 error = error ? error : dsl_prop_register(ds, 595 "setuid", setuid_changed_cb, zfsvfs); 596 error = error ? error : dsl_prop_register(ds, 597 "exec", exec_changed_cb, zfsvfs); 598 error = error ? error : dsl_prop_register(ds, 599 "snapdir", snapdir_changed_cb, zfsvfs); 600 error = error ? error : dsl_prop_register(ds, 601 "aclmode", acl_mode_changed_cb, zfsvfs); 602 error = error ? error : dsl_prop_register(ds, 603 "aclinherit", acl_inherit_changed_cb, zfsvfs); 604 error = error ? error : dsl_prop_register(ds, 605 "vscan", vscan_changed_cb, zfsvfs); 606 if (error) 607 goto unregister; 608 609 /* 610 * Invoke our callbacks to restore temporary mount options. 611 */ 612 if (do_readonly) 613 readonly_changed_cb(zfsvfs, readonly); 614 if (do_setuid) 615 setuid_changed_cb(zfsvfs, setuid); 616 if (do_exec) 617 exec_changed_cb(zfsvfs, exec); 618 if (do_devices) 619 devices_changed_cb(zfsvfs, devices); 620 if (do_xattr) 621 xattr_changed_cb(zfsvfs, xattr); 622 if (do_atime) 623 atime_changed_cb(zfsvfs, atime); 624 625 nbmand_changed_cb(zfsvfs, nbmand); 626 627 return (0); 628 629 unregister: 630 /* 631 * We may attempt to unregister some callbacks that are not 632 * registered, but this is OK; it will simply return ENOMSG, 633 * which we will ignore. 634 */ 635 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs); 636 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs); 637 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs); 638 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs); 639 (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs); 640 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs); 641 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs); 642 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs); 643 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs); 644 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb, 645 zfsvfs); 646 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs); 647 return (error); 648 649 } 650 651 static void 652 uidacct(objset_t *os, boolean_t isgroup, uint64_t fuid, 653 int64_t delta, dmu_tx_t *tx) 654 { 655 uint64_t used = 0; 656 char buf[32]; 657 int err; 658 uint64_t obj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT; 659 660 if (delta == 0) 661 return; 662 663 (void) snprintf(buf, sizeof (buf), "%llx", (longlong_t)fuid); 664 err = zap_lookup(os, obj, buf, 8, 1, &used); 665 ASSERT(err == 0 || err == ENOENT); 666 /* no underflow/overflow */ 667 ASSERT(delta > 0 || used >= -delta); 668 ASSERT(delta < 0 || used + delta > used); 669 used += delta; 670 if (used == 0) 671 err = zap_remove(os, obj, buf, tx); 672 else 673 err = zap_update(os, obj, buf, 8, 1, &used, tx); 674 ASSERT(err == 0); 675 } 676 677 static int 678 zfs_space_delta_cb(dmu_object_type_t bonustype, void *bonus, 679 uint64_t *userp, uint64_t *groupp) 680 { 681 znode_phys_t *znp = bonus; 682 683 if (bonustype != DMU_OT_ZNODE) 684 return (ENOENT); 685 686 *userp = znp->zp_uid; 687 *groupp = znp->zp_gid; 688 return (0); 689 } 690 691 static void 692 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr, 693 char *domainbuf, int buflen, uid_t *ridp) 694 { 695 uint64_t fuid; 696 const char *domain; 697 698 fuid = strtonum(fuidstr, NULL); 699 700 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid)); 701 if (domain) 702 (void) strlcpy(domainbuf, domain, buflen); 703 else 704 domainbuf[0] = '\0'; 705 *ridp = FUID_RID(fuid); 706 } 707 708 static uint64_t 709 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type) 710 { 711 switch (type) { 712 case ZFS_PROP_USERUSED: 713 return (DMU_USERUSED_OBJECT); 714 case ZFS_PROP_GROUPUSED: 715 return (DMU_GROUPUSED_OBJECT); 716 case ZFS_PROP_USERQUOTA: 717 return (zfsvfs->z_userquota_obj); 718 case ZFS_PROP_GROUPQUOTA: 719 return (zfsvfs->z_groupquota_obj); 720 } 721 return (0); 722 } 723 724 int 725 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 726 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep) 727 { 728 int error; 729 zap_cursor_t zc; 730 zap_attribute_t za; 731 zfs_useracct_t *buf = vbuf; 732 uint64_t obj; 733 734 if (!dmu_objset_userspace_present(zfsvfs->z_os)) 735 return (ENOTSUP); 736 737 obj = zfs_userquota_prop_to_obj(zfsvfs, type); 738 if (obj == 0) { 739 *bufsizep = 0; 740 return (0); 741 } 742 743 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep); 744 (error = zap_cursor_retrieve(&zc, &za)) == 0; 745 zap_cursor_advance(&zc)) { 746 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) > 747 *bufsizep) 748 break; 749 750 fuidstr_to_sid(zfsvfs, za.za_name, 751 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid); 752 753 buf->zu_space = za.za_first_integer; 754 buf++; 755 } 756 if (error == ENOENT) 757 error = 0; 758 759 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep); 760 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf; 761 *cookiep = zap_cursor_serialize(&zc); 762 zap_cursor_fini(&zc); 763 return (error); 764 } 765 766 /* 767 * buf must be big enough (eg, 32 bytes) 768 */ 769 static int 770 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid, 771 char *buf, size_t buflen, boolean_t addok) 772 { 773 uint64_t fuid; 774 int domainid = 0; 775 776 if (domain && domain[0]) { 777 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok); 778 if (domainid == -1) 779 return (ENOENT); 780 } 781 fuid = FUID_ENCODE(domainid, rid); 782 (void) snprintf(buf, buflen, "%llx", (longlong_t)fuid); 783 return (0); 784 } 785 786 int 787 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 788 const char *domain, uint64_t rid, uint64_t *valp) 789 { 790 char buf[32]; 791 int err; 792 uint64_t obj; 793 794 *valp = 0; 795 796 if (!dmu_objset_userspace_present(zfsvfs->z_os)) 797 return (ENOTSUP); 798 799 obj = zfs_userquota_prop_to_obj(zfsvfs, type); 800 if (obj == 0) 801 return (0); 802 803 err = id_to_fuidstr(zfsvfs, domain, rid, buf, sizeof(buf), FALSE); 804 if (err) 805 return (err); 806 807 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp); 808 if (err == ENOENT) 809 err = 0; 810 return (err); 811 } 812 813 int 814 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 815 const char *domain, uint64_t rid, uint64_t quota) 816 { 817 char buf[32]; 818 int err; 819 dmu_tx_t *tx; 820 uint64_t *objp; 821 boolean_t fuid_dirtied; 822 823 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA) 824 return (EINVAL); 825 826 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE) 827 return (ENOTSUP); 828 829 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj : 830 &zfsvfs->z_groupquota_obj; 831 832 err = id_to_fuidstr(zfsvfs, domain, rid, buf, sizeof(buf), B_TRUE); 833 if (err) 834 return (err); 835 fuid_dirtied = zfsvfs->z_fuid_dirty; 836 837 tx = dmu_tx_create(zfsvfs->z_os); 838 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL); 839 if (*objp == 0) { 840 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE, 841 zfs_userquota_prop_prefixes[type]); 842 } 843 if (fuid_dirtied) 844 zfs_fuid_txhold(zfsvfs, tx); 845 err = dmu_tx_assign(tx, TXG_WAIT); 846 if (err) { 847 dmu_tx_abort(tx); 848 return (err); 849 } 850 851 mutex_enter(&zfsvfs->z_lock); 852 if (*objp == 0) { 853 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA, 854 DMU_OT_NONE, 0, tx); 855 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 856 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx)); 857 } 858 mutex_exit(&zfsvfs->z_lock); 859 860 if (quota == 0) { 861 err = zap_remove(zfsvfs->z_os, *objp, buf, tx); 862 if (err == ENOENT) 863 err = 0; 864 } else { 865 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx); 866 } 867 ASSERT(err == 0); 868 if (fuid_dirtied) 869 zfs_fuid_sync(zfsvfs, tx); 870 dmu_tx_commit(tx); 871 return (err); 872 } 873 874 boolean_t 875 zfs_usergroup_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid) 876 { 877 char buf[32]; 878 uint64_t used, quota, usedobj, quotaobj; 879 int err; 880 881 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT; 882 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj; 883 884 if (quotaobj == 0 || zfsvfs->z_replay) 885 return (B_FALSE); 886 887 (void) snprintf(buf, sizeof(buf), "%llx", (longlong_t)fuid); 888 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a); 889 if (err != 0) 890 return (B_FALSE); 891 892 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used); 893 if (err != 0) 894 return (B_FALSE); 895 return (used >= quota); 896 } 897 898 int 899 zfsvfs_create(const char *osname, zfsvfs_t **zfvp) 900 { 901 objset_t *os; 902 zfsvfs_t *zfsvfs; 903 uint64_t zval; 904 int i, error; 905 906 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 907 908 /* 909 * We claim to always be readonly so we can open snapshots; 910 * other ZPL code will prevent us from writing to snapshots. 911 */ 912 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os); 913 if (error) { 914 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 915 return (error); 916 } 917 918 /* 919 * Initialize the zfs-specific filesystem structure. 920 * Should probably make this a kmem cache, shuffle fields, 921 * and just bzero up to z_hold_mtx[]. 922 */ 923 zfsvfs->z_vfs = NULL; 924 zfsvfs->z_parent = zfsvfs; 925 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 926 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 927 zfsvfs->z_os = os; 928 929 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version); 930 if (error) { 931 goto out; 932 } else if (zfsvfs->z_version > ZPL_VERSION) { 933 (void) printf("Mismatched versions: File system " 934 "is version %llu on-disk format, which is " 935 "incompatible with this software version %lld!", 936 (u_longlong_t)zfsvfs->z_version, ZPL_VERSION); 937 error = ENOTSUP; 938 goto out; 939 } 940 941 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0) 942 goto out; 943 zfsvfs->z_norm = (int)zval; 944 945 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0) 946 goto out; 947 zfsvfs->z_utf8 = (zval != 0); 948 949 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0) 950 goto out; 951 zfsvfs->z_case = (uint_t)zval; 952 953 /* 954 * Fold case on file systems that are always or sometimes case 955 * insensitive. 956 */ 957 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 958 zfsvfs->z_case == ZFS_CASE_MIXED) 959 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 960 961 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 962 963 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 964 &zfsvfs->z_root); 965 if (error) 966 goto out; 967 ASSERT(zfsvfs->z_root != 0); 968 969 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 970 &zfsvfs->z_unlinkedobj); 971 if (error) 972 goto out; 973 974 error = zap_lookup(os, MASTER_NODE_OBJ, 975 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA], 976 8, 1, &zfsvfs->z_userquota_obj); 977 if (error && error != ENOENT) 978 goto out; 979 980 error = zap_lookup(os, MASTER_NODE_OBJ, 981 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA], 982 8, 1, &zfsvfs->z_groupquota_obj); 983 if (error && error != ENOENT) 984 goto out; 985 986 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1, 987 &zfsvfs->z_fuid_obj); 988 if (error && error != ENOENT) 989 goto out; 990 991 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1, 992 &zfsvfs->z_shares_dir); 993 if (error && error != ENOENT) 994 goto out; 995 996 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 997 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL); 998 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 999 offsetof(znode_t, z_link_node)); 1000 rrw_init(&zfsvfs->z_teardown_lock); 1001 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL); 1002 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL); 1003 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1004 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 1005 1006 *zfvp = zfsvfs; 1007 return (0); 1008 1009 out: 1010 dmu_objset_disown(os, zfsvfs); 1011 *zfvp = NULL; 1012 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 1013 return (error); 1014 } 1015 1016 static int 1017 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting) 1018 { 1019 int error; 1020 1021 error = zfs_register_callbacks(zfsvfs->z_vfs); 1022 if (error) 1023 return (error); 1024 1025 /* 1026 * Set the objset user_ptr to track its zfsvfs. 1027 */ 1028 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 1029 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 1030 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 1031 1032 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data); 1033 if (zil_disable) { 1034 zil_destroy(zfsvfs->z_log, B_FALSE); 1035 zfsvfs->z_log = NULL; 1036 } 1037 1038 /* 1039 * If we are not mounting (ie: online recv), then we don't 1040 * have to worry about replaying the log as we blocked all 1041 * operations out since we closed the ZIL. 1042 */ 1043 if (mounting) { 1044 boolean_t readonly; 1045 1046 /* 1047 * During replay we remove the read only flag to 1048 * allow replays to succeed. 1049 */ 1050 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY; 1051 if (readonly != 0) 1052 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 1053 else 1054 zfs_unlinked_drain(zfsvfs); 1055 1056 if (zfsvfs->z_log) { 1057 /* 1058 * Parse and replay the intent log. 1059 * 1060 * Because of ziltest, this must be done after 1061 * zfs_unlinked_drain(). (Further note: ziltest 1062 * doesn't use readonly mounts, where 1063 * zfs_unlinked_drain() isn't called.) This is because 1064 * ziltest causes spa_sync() to think it's committed, 1065 * but actually it is not, so the intent log contains 1066 * many txg's worth of changes. 1067 * 1068 * In particular, if object N is in the unlinked set in 1069 * the last txg to actually sync, then it could be 1070 * actually freed in a later txg and then reallocated 1071 * in a yet later txg. This would write a "create 1072 * object N" record to the intent log. Normally, this 1073 * would be fine because the spa_sync() would have 1074 * written out the fact that object N is free, before 1075 * we could write the "create object N" intent log 1076 * record. 1077 * 1078 * But when we are in ziltest mode, we advance the "open 1079 * txg" without actually spa_sync()-ing the changes to 1080 * disk. So we would see that object N is still 1081 * allocated and in the unlinked set, and there is an 1082 * intent log record saying to allocate it. 1083 */ 1084 zfsvfs->z_replay = B_TRUE; 1085 zil_replay(zfsvfs->z_os, zfsvfs, zfs_replay_vector); 1086 zfsvfs->z_replay = B_FALSE; 1087 } 1088 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */ 1089 } 1090 1091 return (0); 1092 } 1093 1094 void 1095 zfsvfs_free(zfsvfs_t *zfsvfs) 1096 { 1097 int i; 1098 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */ 1099 1100 /* 1101 * This is a barrier to prevent the filesystem from going away in 1102 * zfs_znode_move() until we can safely ensure that the filesystem is 1103 * not unmounted. We consider the filesystem valid before the barrier 1104 * and invalid after the barrier. 1105 */ 1106 rw_enter(&zfsvfs_lock, RW_READER); 1107 rw_exit(&zfsvfs_lock); 1108 1109 zfs_fuid_destroy(zfsvfs); 1110 mutex_destroy(&zfsvfs->z_znodes_lock); 1111 mutex_destroy(&zfsvfs->z_lock); 1112 list_destroy(&zfsvfs->z_all_znodes); 1113 rrw_destroy(&zfsvfs->z_teardown_lock); 1114 rw_destroy(&zfsvfs->z_teardown_inactive_lock); 1115 rw_destroy(&zfsvfs->z_fuid_lock); 1116 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1117 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 1118 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 1119 } 1120 1121 static void 1122 zfs_set_fuid_feature(zfsvfs_t *zfsvfs) 1123 { 1124 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 1125 if (zfsvfs->z_use_fuids && zfsvfs->z_vfs) { 1126 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR); 1127 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS); 1128 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS); 1129 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE); 1130 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER); 1131 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE); 1132 } 1133 } 1134 1135 static int 1136 zfs_domount(vfs_t *vfsp, char *osname) 1137 { 1138 dev_t mount_dev; 1139 uint64_t recordsize, fsid_guid; 1140 int error = 0; 1141 zfsvfs_t *zfsvfs; 1142 1143 ASSERT(vfsp); 1144 ASSERT(osname); 1145 1146 error = zfsvfs_create(osname, &zfsvfs); 1147 if (error) 1148 return (error); 1149 zfsvfs->z_vfs = vfsp; 1150 zfsvfs->z_parent = zfsvfs; 1151 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 1152 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 1153 1154 /* Initialize the generic filesystem structure. */ 1155 vfsp->vfs_data = NULL; 1156 1157 if (zfs_create_unique_device(&mount_dev) == -1) { 1158 error = ENODEV; 1159 goto out; 1160 } 1161 ASSERT(vfs_devismounted(mount_dev) == 0); 1162 1163 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize, 1164 NULL)) 1165 goto out; 1166 1167 vfsp->vfs_bsize = DEV_BSIZE; 1168 vfsp->vfs_flag |= VFS_NOTRUNC; 1169 vfsp->vfs_data = zfsvfs; 1170 1171 /* 1172 * The fsid is 64 bits, composed of an 8-bit fs type, which 1173 * separates our fsid from any other filesystem types, and a 1174 * 56-bit objset unique ID. The objset unique ID is unique to 1175 * all objsets open on this system, provided by unique_create(). 1176 * The 8-bit fs type must be put in the low bits of fsid[1] 1177 * because that's where other Solaris filesystems put it. 1178 */ 1179 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os); 1180 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0); 1181 vfsp->mnt_stat.f_fsidx.__fsid_val[0] = fsid_guid; 1182 vfsp->mnt_stat.f_fsidx.__fsid_val[1] = ((fsid_guid>>32) << 8) | 1183 zfsfstype & 0xFF; 1184 1185 dprintf("zfs_domount vrele after vfsp->vfs_count %d\n", vfsp->vfs_count); 1186 /* 1187 * Set features for file system. 1188 */ 1189 zfs_set_fuid_feature(zfsvfs); 1190 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 1191 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 1192 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 1193 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE); 1194 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) { 1195 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 1196 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 1197 } 1198 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED); 1199 1200 if (dmu_objset_is_snapshot(zfsvfs->z_os)) { 1201 uint64_t pval; 1202 1203 atime_changed_cb(zfsvfs, B_FALSE); 1204 readonly_changed_cb(zfsvfs, B_TRUE); 1205 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL)) 1206 goto out; 1207 xattr_changed_cb(zfsvfs, pval); 1208 zfsvfs->z_issnap = B_TRUE; 1209 1210 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 1211 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 1212 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 1213 } else { 1214 error = zfsvfs_setup(zfsvfs, B_TRUE); 1215 } 1216 1217 dprintf("zfs_vfsops.c zfs_domount called\n"); 1218 dprintf("vfsp->vfs_count %d\n", vfsp->vfs_count); 1219 1220 if (!zfsvfs->z_issnap) 1221 zfsctl_create(zfsvfs); 1222 out: 1223 if (error) { 1224 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 1225 zfsvfs_free(zfsvfs); 1226 } else { 1227 atomic_add_32(&zfs_active_fs_count, 1); 1228 } 1229 return (error); 1230 } 1231 1232 void 1233 zfs_unregister_callbacks(zfsvfs_t *zfsvfs) 1234 { 1235 objset_t *os = zfsvfs->z_os; 1236 struct dsl_dataset *ds; 1237 1238 /* 1239 * Unregister properties. 1240 */ 1241 if (!dmu_objset_is_snapshot(os)) { 1242 ds = dmu_objset_ds(os); 1243 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb, 1244 zfsvfs) == 0); 1245 1246 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb, 1247 zfsvfs) == 0); 1248 1249 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, 1250 zfsvfs) == 0); 1251 1252 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb, 1253 zfsvfs) == 0); 1254 1255 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb, 1256 zfsvfs) == 0); 1257 1258 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb, 1259 zfsvfs) == 0); 1260 1261 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb, 1262 zfsvfs) == 0); 1263 1264 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, 1265 zfsvfs) == 0); 1266 1267 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, 1268 zfsvfs) == 0); 1269 1270 VERIFY(dsl_prop_unregister(ds, "aclinherit", 1271 acl_inherit_changed_cb, zfsvfs) == 0); 1272 1273 VERIFY(dsl_prop_unregister(ds, "vscan", 1274 vscan_changed_cb, zfsvfs) == 0); 1275 } 1276 } 1277 1278 /* 1279 * Convert a decimal digit string to a uint64_t integer. 1280 */ 1281 static int 1282 str_to_uint64(char *str, uint64_t *objnum) 1283 { 1284 uint64_t num = 0; 1285 1286 while (*str) { 1287 if (*str < '0' || *str > '9') 1288 return (EINVAL); 1289 1290 num = num*10 + *str++ - '0'; 1291 } 1292 1293 *objnum = num; 1294 return (0); 1295 } 1296 1297 /* 1298 * The boot path passed from the boot loader is in the form of 1299 * "rootpool-name/root-filesystem-object-number'. Convert this 1300 * string to a dataset name: "rootpool-name/root-filesystem-name". 1301 */ 1302 static int 1303 zfs_parse_bootfs(char *bpath, char *outpath) 1304 { 1305 char *slashp; 1306 uint64_t objnum; 1307 int error; 1308 1309 if (*bpath == 0 || *bpath == '/') 1310 return (EINVAL); 1311 1312 (void) strcpy(outpath, bpath); 1313 1314 slashp = strchr(bpath, '/'); 1315 1316 /* if no '/', just return the pool name */ 1317 if (slashp == NULL) { 1318 return (0); 1319 } 1320 1321 /* if not a number, just return the root dataset name */ 1322 if (str_to_uint64(slashp+1, &objnum)) { 1323 return (0); 1324 } 1325 1326 *slashp = '\0'; 1327 error = dsl_dsobj_to_dsname(bpath, objnum, outpath); 1328 *slashp = '/'; 1329 1330 return (error); 1331 } 1332 1333 1334 /* 1335 * zfs_check_global_label: 1336 * Check that the hex label string is appropriate for the dataset 1337 * being mounted into the global_zone proper. 1338 * 1339 * Return an error if the hex label string is not default or 1340 * admin_low/admin_high. For admin_low labels, the corresponding 1341 * dataset must be readonly. 1342 */ 1343 int 1344 zfs_check_global_label(const char *dsname, const char *hexsl) 1345 { 1346 #ifdef PORT_SOLARIS 1347 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0) 1348 return (0); 1349 if (strcasecmp(hexsl, ADMIN_HIGH) == 0) 1350 return (0); 1351 if (strcasecmp(hexsl, ADMIN_LOW) == 0) { 1352 /* must be readonly */ 1353 uint64_t rdonly; 1354 1355 if (dsl_prop_get_integer(dsname, 1356 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL)) 1357 return (EACCES); 1358 return (rdonly ? 0 : EACCES); 1359 } 1360 return (EACCES); 1361 #else 1362 return 0; 1363 #endif 1364 } 1365 1366 /* 1367 * zfs_mount_label_policy: 1368 * Determine whether the mount is allowed according to MAC check. 1369 * by comparing (where appropriate) label of the dataset against 1370 * the label of the zone being mounted into. If the dataset has 1371 * no label, create one. 1372 * 1373 * Returns: 1374 * 0 : access allowed 1375 * >0 : error code, such as EACCES 1376 */ 1377 static int 1378 zfs_mount_label_policy(vfs_t *vfsp, char *osname) 1379 { 1380 #ifdef PORT_SOLARIS 1381 int error, retv; 1382 zone_t *mntzone = NULL; 1383 ts_label_t *mnt_tsl; 1384 bslabel_t *mnt_sl; 1385 bslabel_t ds_sl; 1386 char ds_hexsl[MAXNAMELEN]; 1387 1388 retv = EACCES; /* assume the worst */ 1389 1390 /* 1391 * Start by getting the dataset label if it exists. 1392 */ 1393 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 1394 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 1395 if (error) 1396 return (EACCES); 1397 1398 /* 1399 * If labeling is NOT enabled, then disallow the mount of datasets 1400 * which have a non-default label already. No other label checks 1401 * are needed. 1402 */ 1403 if (!is_system_labeled()) { 1404 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) 1405 return (0); 1406 return (EACCES); 1407 } 1408 1409 /* 1410 * Get the label of the mountpoint. If mounting into the global 1411 * zone (i.e. mountpoint is not within an active zone and the 1412 * zoned property is off), the label must be default or 1413 * admin_low/admin_high only; no other checks are needed. 1414 */ 1415 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE); 1416 if (mntzone->zone_id == GLOBAL_ZONEID) { 1417 uint64_t zoned; 1418 1419 zone_rele(mntzone); 1420 1421 if (dsl_prop_get_integer(osname, 1422 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 1423 return (EACCES); 1424 if (!zoned) 1425 return (zfs_check_global_label(osname, ds_hexsl)); 1426 else 1427 /* 1428 * This is the case of a zone dataset being mounted 1429 * initially, before the zone has been fully created; 1430 * allow this mount into global zone. 1431 */ 1432 return (0); 1433 } 1434 1435 mnt_tsl = mntzone->zone_slabel; 1436 ASSERT(mnt_tsl != NULL); 1437 label_hold(mnt_tsl); 1438 mnt_sl = label2bslabel(mnt_tsl); 1439 1440 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) { 1441 /* 1442 * The dataset doesn't have a real label, so fabricate one. 1443 */ 1444 char *str = NULL; 1445 1446 if (l_to_str_internal(mnt_sl, &str) == 0 && 1447 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 1448 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0) 1449 retv = 0; 1450 if (str != NULL) 1451 kmem_free(str, strlen(str) + 1); 1452 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) { 1453 /* 1454 * Now compare labels to complete the MAC check. If the 1455 * labels are equal then allow access. If the mountpoint 1456 * label dominates the dataset label, allow readonly access. 1457 * Otherwise, access is denied. 1458 */ 1459 if (blequal(mnt_sl, &ds_sl)) 1460 retv = 0; 1461 else if (bldominates(mnt_sl, &ds_sl)) { 1462 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0); 1463 retv = 0; 1464 } 1465 } 1466 1467 label_rele(mnt_tsl); 1468 zone_rele(mntzone); 1469 return (retv); 1470 #else /* PORT_SOLARIS */ 1471 return (0); 1472 #endif 1473 } 1474 1475 #ifndef __NetBSD__ 1476 static int 1477 zfs_mountroot(vfs_t *vfsp, enum whymountroot why) 1478 { 1479 int error = 0; 1480 static int zfsrootdone = 0; 1481 zfsvfs_t *zfsvfs = NULL; 1482 znode_t *zp = NULL; 1483 vnode_t *vp = NULL; 1484 char *zfs_bootfs; 1485 char *zfs_devid; 1486 1487 ASSERT(vfsp); 1488 1489 /* 1490 * The filesystem that we mount as root is defined in the 1491 * boot property "zfs-bootfs" with a format of 1492 * "poolname/root-dataset-objnum". 1493 */ 1494 if (why == ROOT_INIT) { 1495 if (zfsrootdone++) 1496 return (EBUSY); 1497 /* 1498 * the process of doing a spa_load will require the 1499 * clock to be set before we could (for example) do 1500 * something better by looking at the timestamp on 1501 * an uberblock, so just set it to -1. 1502 */ 1503 clkset(-1); 1504 1505 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) { 1506 cmn_err(CE_NOTE, "spa_get_bootfs: can not get " 1507 "bootfs name"); 1508 return (EINVAL); 1509 } 1510 zfs_devid = spa_get_bootprop("diskdevid"); 1511 error = spa_import_rootpool(rootfs.bo_name, zfs_devid); 1512 if (zfs_devid) 1513 spa_free_bootprop(zfs_devid); 1514 if (error) { 1515 spa_free_bootprop(zfs_bootfs); 1516 cmn_err(CE_NOTE, "spa_import_rootpool: error %d", 1517 error); 1518 return (error); 1519 } 1520 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) { 1521 spa_free_bootprop(zfs_bootfs); 1522 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d", 1523 error); 1524 return (error); 1525 } 1526 1527 spa_free_bootprop(zfs_bootfs); 1528 1529 if (error = vfs_lock(vfsp)) 1530 return (error); 1531 1532 if (error = zfs_domount(vfsp, rootfs.bo_name)) { 1533 cmn_err(CE_NOTE, "zfs_domount: error %d", error); 1534 goto out; 1535 } 1536 1537 zfsvfs = (zfsvfs_t *)vfsp->vfs_data; 1538 ASSERT(zfsvfs); 1539 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) { 1540 cmn_err(CE_NOTE, "zfs_zget: error %d", error); 1541 goto out; 1542 } 1543 1544 vp = ZTOV(zp); 1545 mutex_enter(&vp->v_lock); 1546 vp->v_flag |= VROOT; 1547 mutex_exit(&vp->v_lock); 1548 rootvp = vp; 1549 1550 /* 1551 * Leave rootvp held. The root file system is never unmounted. 1552 */ 1553 1554 vfs_add((struct vnode *)0, vfsp, 1555 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0); 1556 out: 1557 vfs_unlock(vfsp); 1558 return (error); 1559 } else if (why == ROOT_REMOUNT) { 1560 readonly_changed_cb(vfsp->vfs_data, B_FALSE); 1561 vfsp->vfs_flag |= VFS_REMOUNT; 1562 1563 /* refresh mount options */ 1564 zfs_unregister_callbacks(vfsp->vfs_data); 1565 return (zfs_register_callbacks(vfsp)); 1566 1567 } else if (why == ROOT_UNMOUNT) { 1568 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data); 1569 (void) zfs_sync(vfsp, 0, 0); 1570 return (0); 1571 } 1572 1573 /* 1574 * if "why" is equal to anything else other than ROOT_INIT, 1575 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it. 1576 */ 1577 return (ENOTSUP); 1578 } 1579 #endif /*__NetBSD__ */ 1580 1581 /*ARGSUSED*/ 1582 static int 1583 zfs_mount(vfs_t *vfsp, const char *path, void *data, size_t *data_len) 1584 { 1585 char *osname; 1586 pathname_t spn; 1587 vnode_t *mvp = vfsp->mnt_vnodecovered; 1588 struct mounta *uap = data; 1589 int error = 0; 1590 int canwrite; 1591 cred_t *cr; 1592 1593 crget(cr); 1594 dprintf("zfs_vfsops.c zfs_mount called\n"); 1595 dprintf("vfsp->vfs_count %d\n", vfsp->vfs_count); 1596 if (mvp->v_type != VDIR) 1597 return (ENOTDIR); 1598 1599 if (uap == NULL) 1600 return (EINVAL); 1601 1602 mutex_enter(mvp->v_interlock); 1603 if ((uap->flags & MS_REMOUNT) == 0 && 1604 (uap->flags & MS_OVERLAY) == 0 && 1605 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) { 1606 mutex_exit(mvp->v_interlock); 1607 return (EBUSY); 1608 } 1609 mutex_exit(mvp->v_interlock); 1610 1611 /* 1612 * ZFS does not support passing unparsed data in via MS_DATA. 1613 * Users should use the MS_OPTIONSTR interface; this means 1614 * that all option parsing is already done and the options struct 1615 * can be interrogated. 1616 */ 1617 if ((uap->flags & MS_DATA) && uap->datalen > 0) 1618 return (EINVAL); 1619 1620 osname = PNBUF_GET(); 1621 1622 strlcpy(osname, uap->fspec, strlen(uap->fspec) + 1); 1623 1624 /* 1625 * Check for mount privilege? 1626 * 1627 * If we don't have privilege then see if 1628 * we have local permission to allow it 1629 */ 1630 error = secpolicy_fs_mount(cr, mvp, vfsp); 1631 if (error) { 1632 error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr); 1633 if (error == 0) { 1634 vattr_t vattr; 1635 1636 /* 1637 * Make sure user is the owner of the mount point 1638 * or has sufficient privileges. 1639 */ 1640 1641 vattr.va_mask = AT_UID; 1642 1643 if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) { 1644 goto out; 1645 } 1646 1647 if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 && 1648 VOP_ACCESS(mvp, VWRITE, cr) != 0) { 1649 error = EPERM; 1650 goto out; 1651 } 1652 1653 /* XXX NetBSD secpolicy_fs_mount_clearopts(cr, vfsp);*/ 1654 } else { 1655 goto out; 1656 } 1657 } 1658 1659 /* 1660 * Refuse to mount a filesystem if we are in a local zone and the 1661 * dataset is not visible. 1662 */ 1663 if (!INGLOBALZONE(curproc) && 1664 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) { 1665 error = EPERM; 1666 goto out; 1667 } 1668 1669 error = zfs_mount_label_policy(vfsp, osname); 1670 if (error) 1671 goto out; 1672 1673 /* 1674 * When doing a remount, we simply refresh our temporary properties 1675 * according to those options set in the current VFS options. 1676 */ 1677 if (uap->flags & MS_REMOUNT) { 1678 /* refresh mount options */ 1679 zfs_unregister_callbacks(vfsp->vfs_data); 1680 error = zfs_register_callbacks(vfsp); 1681 goto out; 1682 } 1683 1684 /* Mark ZFS as MP SAFE */ 1685 vfsp->mnt_iflag |= IMNT_MPSAFE; 1686 1687 error = zfs_domount(vfsp, osname); 1688 1689 vfs_getnewfsid(vfsp); 1690 1691 /* setup zfs mount info */ 1692 strlcpy(vfsp->mnt_stat.f_mntfromname, osname, 1693 sizeof(vfsp->mnt_stat.f_mntfromname)); 1694 set_statvfs_info(path, UIO_USERSPACE, vfsp->mnt_stat.f_mntfromname, 1695 UIO_SYSSPACE, vfsp->mnt_op->vfs_name, vfsp, curlwp); 1696 1697 /* 1698 * Add an extra VFS_HOLD on our parent vfs so that it can't 1699 * disappear due to a forced unmount. 1700 */ 1701 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap) 1702 VFS_HOLD(mvp->v_vfsp); 1703 1704 out: 1705 PNBUF_PUT(osname); 1706 return (error); 1707 } 1708 1709 static int 1710 zfs_statvfs(vfs_t *vfsp, struct statvfs *statp) 1711 { 1712 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1713 dev_t dev; 1714 uint64_t refdbytes, availbytes, usedobjs, availobjs; 1715 1716 ZFS_ENTER(zfsvfs); 1717 1718 dmu_objset_space(zfsvfs->z_os, 1719 &refdbytes, &availbytes, &usedobjs, &availobjs); 1720 1721 /* 1722 * The underlying storage pool actually uses multiple block sizes. 1723 * We report the fragsize as the smallest block size we support, 1724 * and we report our blocksize as the filesystem's maximum blocksize. 1725 */ 1726 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT; 1727 statp->f_bsize = zfsvfs->z_max_blksz; 1728 1729 /* 1730 * The following report "total" blocks of various kinds in the 1731 * file system, but reported in terms of f_frsize - the 1732 * "fragment" size. 1733 */ 1734 1735 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT; 1736 statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT; 1737 statp->f_bavail = statp->f_bfree; /* no root reservation */ 1738 1739 /* 1740 * statvfs() should really be called statufs(), because it assumes 1741 * static metadata. ZFS doesn't preallocate files, so the best 1742 * we can do is report the max that could possibly fit in f_files, 1743 * and that minus the number actually used in f_ffree. 1744 * For f_ffree, report the smaller of the number of object available 1745 * and the number of blocks (each object will take at least a block). 1746 */ 1747 statp->f_ffree = MIN(availobjs, statp->f_bfree); 1748 statp->f_favail = statp->f_ffree; /* no "root reservation" */ 1749 statp->f_files = statp->f_ffree + usedobjs; 1750 1751 statp->f_fsid = vfsp->mnt_stat.f_fsidx.__fsid_val[0]; 1752 1753 /* 1754 * We're a zfs filesystem. 1755 */ 1756 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename)); 1757 (void) strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname, 1758 sizeof(statp->f_mntfromname)); 1759 (void) strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname, 1760 sizeof(statp->f_mntonname)); 1761 1762 statp->f_namemax = ZFS_MAXNAMELEN; 1763 1764 /* 1765 * We have all of 32 characters to stuff a string here. 1766 * Is there anything useful we could/should provide? 1767 */ 1768 #ifndef __NetBSD__ 1769 bzero(statp->f_fstr, sizeof (statp->f_fstr)); 1770 #endif 1771 ZFS_EXIT(zfsvfs); 1772 return (0); 1773 } 1774 1775 static int 1776 zfs_root(vfs_t *vfsp, vnode_t **vpp) 1777 { 1778 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1779 znode_t *rootzp; 1780 int error; 1781 1782 ZFS_ENTER(zfsvfs); 1783 dprintf("zfs_root called\n"); 1784 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 1785 if (error == 0) 1786 *vpp = ZTOV(rootzp); 1787 dprintf("vpp -> %d, error %d -- %p\n", (*vpp)->v_type, error, *vpp); 1788 ZFS_EXIT(zfsvfs); 1789 if (error == 0) 1790 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 1791 KASSERT((error != 0) || (*vpp != NULL)); 1792 KASSERT((error != 0) || (VOP_ISLOCKED(*vpp) == LK_EXCLUSIVE)); 1793 return (error); 1794 } 1795 1796 /* 1797 * Teardown the zfsvfs::z_os. 1798 * 1799 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock' 1800 * and 'z_teardown_inactive_lock' held. 1801 */ 1802 static int 1803 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting) 1804 { 1805 znode_t *zp; 1806 1807 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG); 1808 1809 if (!unmounting) { 1810 /* 1811 * We purge the parent filesystem's vfsp as the parent 1812 * filesystem and all of its snapshots have their vnode's 1813 * v_vfsp set to the parent's filesystem's vfsp. Note, 1814 * 'z_parent' is self referential for non-snapshots. 1815 */ 1816 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1817 } 1818 1819 /* 1820 * Close the zil. NB: Can't close the zil while zfs_inactive 1821 * threads are blocked as zil_close can call zfs_inactive. 1822 */ 1823 if (zfsvfs->z_log) { 1824 zil_close(zfsvfs->z_log); 1825 zfsvfs->z_log = NULL; 1826 } 1827 1828 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER); 1829 1830 /* 1831 * If we are not unmounting (ie: online recv) and someone already 1832 * unmounted this file system while we were doing the switcheroo, 1833 * or a reopen of z_os failed then just bail out now. 1834 */ 1835 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) { 1836 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1837 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1838 return (EIO); 1839 } 1840 1841 /* 1842 * At this point there are no vops active, and any new vops will 1843 * fail with EIO since we have z_teardown_lock for writer (only 1844 * relavent for forced unmount). 1845 * 1846 * Release all holds on dbufs. 1847 */ 1848 mutex_enter(&zfsvfs->z_znodes_lock); 1849 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL; 1850 zp = list_next(&zfsvfs->z_all_znodes, zp)) 1851 if (zp->z_dbuf) { 1852 ASSERT(ZTOV(zp)->v_count > 0); 1853 zfs_znode_dmu_fini(zp); 1854 } 1855 mutex_exit(&zfsvfs->z_znodes_lock); 1856 1857 /* 1858 * If we are unmounting, set the unmounted flag and let new vops 1859 * unblock. zfs_inactive will have the unmounted behavior, and all 1860 * other vops will fail with EIO. 1861 */ 1862 if (unmounting) { 1863 zfsvfs->z_unmounted = B_TRUE; 1864 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1865 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1866 } 1867 1868 /* 1869 * z_os will be NULL if there was an error in attempting to reopen 1870 * zfsvfs, so just return as the properties had already been 1871 * unregistered and cached data had been evicted before. 1872 */ 1873 if (zfsvfs->z_os == NULL) 1874 return (0); 1875 1876 /* 1877 * Unregister properties. 1878 */ 1879 zfs_unregister_callbacks(zfsvfs); 1880 1881 /* 1882 * Evict cached data 1883 */ 1884 if (dmu_objset_evict_dbufs(zfsvfs->z_os)) { 1885 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 1886 (void) dmu_objset_evict_dbufs(zfsvfs->z_os); 1887 } 1888 1889 return (0); 1890 } 1891 1892 /*ARGSUSED*/ 1893 static int 1894 zfs_umount(vfs_t *vfsp, int fflag) 1895 { 1896 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1897 objset_t *os; 1898 int ret, flags = 0; 1899 cred_t *cr; 1900 1901 vnode_t *vpp; 1902 int counter; 1903 1904 counter = 0; 1905 1906 dprintf("ZFS_UMOUNT called\n"); 1907 1908 /*TAILQ_FOREACH(vpp, &vfsp->mnt_vnodelist, v_mntvnodes) { 1909 printf("vnode list vnode number %d -- vnode address %p\n", counter, vpp); 1910 vprint("ZFS vfsp vnode list", vpp); 1911 counter++; 1912 } */ 1913 1914 crget(cr); 1915 #ifdef TODO 1916 ret = secpolicy_fs_unmount(cr, vfsp); 1917 if (ret) { 1918 ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource), 1919 ZFS_DELEG_PERM_MOUNT, cr); 1920 if (ret) 1921 return (ret); 1922 } 1923 #endif 1924 /* 1925 * We purge the parent filesystem's vfsp as the parent filesystem 1926 * and all of its snapshots have their vnode's v_vfsp set to the 1927 * parent's filesystem's vfsp. Note, 'z_parent' is self 1928 * referential for non-snapshots. 1929 */ 1930 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1931 1932 /* 1933 * Unmount any snapshots mounted under .zfs before unmounting the 1934 * dataset itself. 1935 */ 1936 if (zfsvfs->z_ctldir != NULL && 1937 (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) { 1938 return (ret); 1939 } 1940 1941 #if 0 1942 if (!(fflag & MS_FORCE)) { 1943 /* 1944 * Check the number of active vnodes in the file system. 1945 * Our count is maintained in the vfs structure, but the 1946 * number is off by 1 to indicate a hold on the vfs 1947 * structure itself. 1948 * 1949 * The '.zfs' directory maintains a reference of its 1950 * own, and any active references underneath are 1951 * reflected in the vnode count. 1952 */ 1953 if (zfsvfs->z_ctldir == NULL) { 1954 if (vfsp->vfs_count > 1){ 1955 return (EBUSY); 1956 } 1957 } else { 1958 if (vfsp->vfs_count > 2 || 1959 zfsvfs->z_ctldir->v_count > 1) { 1960 return (EBUSY); 1961 } 1962 } 1963 } 1964 #endif 1965 ret = vflush(vfsp, NULL, (ISSET(fflag, MS_FORCE)? FORCECLOSE : 0)); 1966 if (ret != 0) 1967 return ret; 1968 vfsp->vfs_flag |= VFS_UNMOUNTED; 1969 1970 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0); 1971 os = zfsvfs->z_os; 1972 1973 /* 1974 * z_os will be NULL if there was an error in 1975 * attempting to reopen zfsvfs. 1976 */ 1977 if (os != NULL) { 1978 /* 1979 * Unset the objset user_ptr. 1980 */ 1981 mutex_enter(&os->os_user_ptr_lock); 1982 dmu_objset_set_user(os, NULL); 1983 mutex_exit(&os->os_user_ptr_lock); 1984 1985 /* 1986 * Finally release the objset 1987 */ 1988 dmu_objset_disown(os, zfsvfs); 1989 } 1990 1991 /* 1992 * We can now safely destroy the '.zfs' directory node. 1993 */ 1994 if (zfsvfs->z_ctldir != NULL) 1995 zfsctl_destroy(zfsvfs); 1996 1997 return (0); 1998 } 1999 2000 static int 2001 zfs_vget(vfs_t *vfsp, ino_t ino, vnode_t **vpp) 2002 { 2003 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2004 znode_t *zp; 2005 int err; 2006 2007 dprintf("zfs_vget called\n"); 2008 dprintf("vfsp->vfs_count %d\n", vfsp->vfs_count); 2009 2010 ZFS_ENTER(zfsvfs); 2011 err = zfs_zget(zfsvfs, ino, &zp); 2012 if (err == 0 && zp->z_unlinked) { 2013 VN_RELE(ZTOV(zp)); 2014 err = EINVAL; 2015 } 2016 if (err != 0) 2017 *vpp = NULL; 2018 else { 2019 *vpp = ZTOV(zp); 2020 /* XXX NetBSD how to get flags for vn_lock ? */ 2021 vn_lock(*vpp, 0); 2022 } 2023 ZFS_EXIT(zfsvfs); 2024 return (err); 2025 } 2026 2027 static int 2028 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp) 2029 { 2030 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2031 znode_t *zp; 2032 uint64_t object = 0; 2033 uint64_t fid_gen = 0; 2034 uint64_t gen_mask; 2035 uint64_t zp_gen; 2036 int i, err; 2037 2038 *vpp = NULL; 2039 2040 dprintf("zfs_fhtovp called\n"); 2041 dprintf("vfsp->vfs_count %d\n", vfsp->vfs_count); 2042 2043 ZFS_ENTER(zfsvfs); 2044 2045 if (fidp->fid_len == LONG_FID_LEN) { 2046 zfid_long_t *zlfid = (zfid_long_t *)fidp; 2047 uint64_t objsetid = 0; 2048 uint64_t setgen = 0; 2049 2050 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 2051 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 2052 2053 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 2054 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 2055 2056 ZFS_EXIT(zfsvfs); 2057 2058 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs); 2059 if (err) 2060 return (EINVAL); 2061 ZFS_ENTER(zfsvfs); 2062 } 2063 2064 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 2065 zfid_short_t *zfid = (zfid_short_t *)fidp; 2066 2067 for (i = 0; i < sizeof (zfid->zf_object); i++) 2068 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 2069 2070 for (i = 0; i < sizeof (zfid->zf_gen); i++) 2071 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 2072 } else { 2073 ZFS_EXIT(zfsvfs); 2074 return (EINVAL); 2075 } 2076 2077 /* A zero fid_gen means we are in the .zfs control directories */ 2078 if (fid_gen == 0 && 2079 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) { 2080 *vpp = zfsvfs->z_ctldir; 2081 ASSERT(*vpp != NULL); 2082 if (object == ZFSCTL_INO_SNAPDIR) { 2083 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL, 2084 0, NULL, NULL, NULL, NULL, NULL) == 0); 2085 } else { 2086 VN_HOLD(*vpp); 2087 } 2088 ZFS_EXIT(zfsvfs); 2089 /* XXX: LK_RETRY? */ 2090 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 2091 return (0); 2092 } 2093 2094 gen_mask = -1ULL >> (64 - 8 * i); 2095 2096 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask); 2097 if (err = zfs_zget(zfsvfs, object, &zp)) { 2098 ZFS_EXIT(zfsvfs); 2099 return (err); 2100 } 2101 zp_gen = zp->z_phys->zp_gen & gen_mask; 2102 if (zp_gen == 0) 2103 zp_gen = 1; 2104 if (zp->z_unlinked || zp_gen != fid_gen) { 2105 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen); 2106 VN_RELE(ZTOV(zp)); 2107 ZFS_EXIT(zfsvfs); 2108 return (EINVAL); 2109 } 2110 2111 *vpp = ZTOV(zp); 2112 /* XXX: LK_RETRY? */ 2113 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY); 2114 ZFS_EXIT(zfsvfs); 2115 return (0); 2116 } 2117 2118 /* 2119 * Block out VOPs and close zfsvfs_t::z_os 2120 * 2121 * Note, if successful, then we return with the 'z_teardown_lock' and 2122 * 'z_teardown_inactive_lock' write held. 2123 */ 2124 int 2125 zfs_suspend_fs(zfsvfs_t *zfsvfs) 2126 { 2127 int error; 2128 2129 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0) 2130 return (error); 2131 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 2132 2133 return (0); 2134 } 2135 2136 /* 2137 * Reopen zfsvfs_t::z_os and release VOPs. 2138 */ 2139 int 2140 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname) 2141 { 2142 int err; 2143 2144 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock)); 2145 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 2146 2147 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs, 2148 &zfsvfs->z_os); 2149 if (err) { 2150 zfsvfs->z_os = NULL; 2151 } else { 2152 znode_t *zp; 2153 2154 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0); 2155 2156 /* 2157 * Attempt to re-establish all the active znodes with 2158 * their dbufs. If a zfs_rezget() fails, then we'll let 2159 * any potential callers discover that via ZFS_ENTER_VERIFY_VP 2160 * when they try to use their znode. 2161 */ 2162 mutex_enter(&zfsvfs->z_znodes_lock); 2163 for (zp = list_head(&zfsvfs->z_all_znodes); zp; 2164 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 2165 (void) zfs_rezget(zp); 2166 } 2167 mutex_exit(&zfsvfs->z_znodes_lock); 2168 2169 } 2170 2171 /* release the VOPs */ 2172 rw_exit(&zfsvfs->z_teardown_inactive_lock); 2173 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 2174 2175 if (err) { 2176 /* 2177 * Since we couldn't reopen zfsvfs::z_os, force 2178 * unmount this file system. 2179 */ 2180 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) 2181 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curlwp); 2182 } 2183 return (err); 2184 } 2185 2186 static void 2187 zfs_freevfs(vfs_t *vfsp) 2188 { 2189 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2190 2191 /* 2192 * If this is a snapshot, we have an extra VFS_HOLD on our parent 2193 * from zfs_mount(). Release it here. 2194 */ 2195 if (zfsvfs->z_issnap) 2196 VFS_RELE(zfsvfs->z_parent->z_vfs); 2197 2198 zfsvfs_free(zfsvfs); 2199 2200 atomic_add_32(&zfs_active_fs_count, -1); 2201 } 2202 2203 /* 2204 * VFS_INIT() initialization. Note that there is no VFS_FINI(), 2205 * so we can't safely do any non-idempotent initialization here. 2206 * Leave that to zfs_init() and zfs_fini(), which are called 2207 * from the module's _init() and _fini() entry points. 2208 */ 2209 /*ARGSUSED*/ 2210 int 2211 zfs_vfsinit(int fstype, char *name) 2212 { 2213 int error; 2214 2215 zfsfstype = fstype; 2216 2217 /* 2218 * Setup vfsops and vnodeops tables. 2219 */ 2220 error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops); 2221 2222 error = zfs_create_op_tables(); 2223 if (error) { 2224 zfs_remove_op_tables(); 2225 cmn_err(CE_WARN, "zfs: bad vnode ops template"); 2226 vfs_freevfsops_by_type(zfsfstype); 2227 return (error); 2228 } 2229 2230 mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 2231 mutex_init(&zfs_debug_mtx, NULL, MUTEX_DEFAULT, NULL); 2232 2233 /* 2234 * Unique major number for all zfs mounts. 2235 * If we run out of 32-bit minors, we'll getudev() another major. 2236 */ 2237 zfs_major = ddi_name_to_major(ZFS_DRIVER); 2238 zfs_minor = ZFS_MIN_MINOR; 2239 2240 return (0); 2241 } 2242 2243 int 2244 zfs_vfsfini(void) 2245 { 2246 int err; 2247 2248 err = vfs_detach(&zfs_vfsops_template); 2249 if (err != 0) 2250 return err; 2251 2252 mutex_destroy(&zfs_debug_mtx); 2253 mutex_destroy(&zfs_dev_mtx); 2254 2255 return 0; 2256 } 2257 2258 void 2259 zfs_init(void) 2260 { 2261 /* 2262 * Initialize .zfs directory structures 2263 */ 2264 zfsctl_init(); 2265 2266 /* 2267 * Initialize znode cache, vnode ops, etc... 2268 */ 2269 zfs_znode_init(); 2270 2271 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb); 2272 } 2273 2274 void 2275 zfs_fini(void) 2276 { 2277 zfsctl_fini(); 2278 zfs_znode_fini(); 2279 } 2280 2281 int 2282 zfs_busy(void) 2283 { 2284 return (zfs_active_fs_count != 0); 2285 } 2286 2287 int 2288 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers) 2289 { 2290 int error; 2291 objset_t *os = zfsvfs->z_os; 2292 dmu_tx_t *tx; 2293 2294 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION) 2295 return (EINVAL); 2296 2297 if (newvers < zfsvfs->z_version) 2298 return (EINVAL); 2299 2300 tx = dmu_tx_create(os); 2301 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR); 2302 error = dmu_tx_assign(tx, TXG_WAIT); 2303 if (error) { 2304 dmu_tx_abort(tx); 2305 return (error); 2306 } 2307 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 2308 8, 1, &newvers, tx); 2309 2310 if (error) { 2311 dmu_tx_commit(tx); 2312 return (error); 2313 } 2314 2315 spa_history_internal_log(LOG_DS_UPGRADE, 2316 dmu_objset_spa(os), tx, CRED(), 2317 "oldver=%llu newver=%llu dataset = %llu", 2318 zfsvfs->z_version, newvers, dmu_objset_id(os)); 2319 2320 dmu_tx_commit(tx); 2321 2322 zfsvfs->z_version = newvers; 2323 2324 if (zfsvfs->z_version >= ZPL_VERSION_FUID) 2325 zfs_set_fuid_feature(zfsvfs); 2326 2327 return (0); 2328 } 2329 2330 /* 2331 * Read a property stored within the master node. 2332 */ 2333 int 2334 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value) 2335 { 2336 const char *pname; 2337 int error = ENOENT; 2338 2339 /* 2340 * Look up the file system's value for the property. For the 2341 * version property, we look up a slightly different string. 2342 */ 2343 if (prop == ZFS_PROP_VERSION) 2344 pname = ZPL_VERSION_STR; 2345 else 2346 pname = zfs_prop_to_name(prop); 2347 2348 if (os != NULL) 2349 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value); 2350 2351 if (error == ENOENT) { 2352 /* No value set, use the default value */ 2353 switch (prop) { 2354 case ZFS_PROP_VERSION: 2355 *value = ZPL_VERSION; 2356 break; 2357 case ZFS_PROP_NORMALIZE: 2358 case ZFS_PROP_UTF8ONLY: 2359 *value = 0; 2360 break; 2361 case ZFS_PROP_CASE: 2362 *value = ZFS_CASE_SENSITIVE; 2363 break; 2364 default: 2365 return (error); 2366 } 2367 error = 0; 2368 } 2369 return (error); 2370 } 2371 2372 static int 2373 zfs_start(vfs_t *vfsp, int flags) 2374 { 2375 2376 return (0); 2377 } 2378 2379 2380 #ifdef TODO 2381 static vfsdef_t vfw = { 2382 VFSDEF_VERSION, 2383 MNTTYPE_ZFS, 2384 zfs_vfsinit, 2385 VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS| 2386 VSW_XID, 2387 &zfs_mntopts 2388 }; 2389 2390 struct modlfs zfs_modlfs = { 2391 &mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw 2392 }; 2393 #endif 2394