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