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