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