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