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