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