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 https://opensource.org/licenses/CDDL-1.0. 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, 2018 by Delphix. All rights reserved. 24 */ 25 26 /* Portions Copyright 2010 Robert Milkowski */ 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/sysmacros.h> 31 #include <sys/kmem.h> 32 #include <sys/pathname.h> 33 #include <sys/vnode.h> 34 #include <sys/vfs.h> 35 #include <sys/mntent.h> 36 #include <sys/cmn_err.h> 37 #include <sys/zfs_znode.h> 38 #include <sys/zfs_vnops.h> 39 #include <sys/zfs_dir.h> 40 #include <sys/zil.h> 41 #include <sys/fs/zfs.h> 42 #include <sys/dmu.h> 43 #include <sys/dsl_prop.h> 44 #include <sys/dsl_dataset.h> 45 #include <sys/dsl_deleg.h> 46 #include <sys/spa.h> 47 #include <sys/zap.h> 48 #include <sys/sa.h> 49 #include <sys/sa_impl.h> 50 #include <sys/policy.h> 51 #include <sys/atomic.h> 52 #include <sys/zfs_ioctl.h> 53 #include <sys/zfs_ctldir.h> 54 #include <sys/zfs_fuid.h> 55 #include <sys/zfs_quota.h> 56 #include <sys/sunddi.h> 57 #include <sys/dmu_objset.h> 58 #include <sys/dsl_dir.h> 59 #include <sys/objlist.h> 60 #include <sys/zfeature.h> 61 #include <sys/zpl.h> 62 #include <linux/vfs_compat.h> 63 #include <linux/fs.h> 64 #include "zfs_comutil.h" 65 66 enum { 67 TOKEN_RO, 68 TOKEN_RW, 69 TOKEN_SETUID, 70 TOKEN_NOSETUID, 71 TOKEN_EXEC, 72 TOKEN_NOEXEC, 73 TOKEN_DEVICES, 74 TOKEN_NODEVICES, 75 TOKEN_DIRXATTR, 76 TOKEN_SAXATTR, 77 TOKEN_XATTR, 78 TOKEN_NOXATTR, 79 TOKEN_ATIME, 80 TOKEN_NOATIME, 81 TOKEN_RELATIME, 82 TOKEN_NORELATIME, 83 TOKEN_NBMAND, 84 TOKEN_NONBMAND, 85 TOKEN_MNTPOINT, 86 TOKEN_LAST, 87 }; 88 89 static const match_table_t zpl_tokens = { 90 { TOKEN_RO, MNTOPT_RO }, 91 { TOKEN_RW, MNTOPT_RW }, 92 { TOKEN_SETUID, MNTOPT_SETUID }, 93 { TOKEN_NOSETUID, MNTOPT_NOSETUID }, 94 { TOKEN_EXEC, MNTOPT_EXEC }, 95 { TOKEN_NOEXEC, MNTOPT_NOEXEC }, 96 { TOKEN_DEVICES, MNTOPT_DEVICES }, 97 { TOKEN_NODEVICES, MNTOPT_NODEVICES }, 98 { TOKEN_DIRXATTR, MNTOPT_DIRXATTR }, 99 { TOKEN_SAXATTR, MNTOPT_SAXATTR }, 100 { TOKEN_XATTR, MNTOPT_XATTR }, 101 { TOKEN_NOXATTR, MNTOPT_NOXATTR }, 102 { TOKEN_ATIME, MNTOPT_ATIME }, 103 { TOKEN_NOATIME, MNTOPT_NOATIME }, 104 { TOKEN_RELATIME, MNTOPT_RELATIME }, 105 { TOKEN_NORELATIME, MNTOPT_NORELATIME }, 106 { TOKEN_NBMAND, MNTOPT_NBMAND }, 107 { TOKEN_NONBMAND, MNTOPT_NONBMAND }, 108 { TOKEN_MNTPOINT, MNTOPT_MNTPOINT "=%s" }, 109 { TOKEN_LAST, NULL }, 110 }; 111 112 static void 113 zfsvfs_vfs_free(vfs_t *vfsp) 114 { 115 if (vfsp != NULL) { 116 if (vfsp->vfs_mntpoint != NULL) 117 kmem_strfree(vfsp->vfs_mntpoint); 118 119 kmem_free(vfsp, sizeof (vfs_t)); 120 } 121 } 122 123 static int 124 zfsvfs_parse_option(char *option, int token, substring_t *args, vfs_t *vfsp) 125 { 126 switch (token) { 127 case TOKEN_RO: 128 vfsp->vfs_readonly = B_TRUE; 129 vfsp->vfs_do_readonly = B_TRUE; 130 break; 131 case TOKEN_RW: 132 vfsp->vfs_readonly = B_FALSE; 133 vfsp->vfs_do_readonly = B_TRUE; 134 break; 135 case TOKEN_SETUID: 136 vfsp->vfs_setuid = B_TRUE; 137 vfsp->vfs_do_setuid = B_TRUE; 138 break; 139 case TOKEN_NOSETUID: 140 vfsp->vfs_setuid = B_FALSE; 141 vfsp->vfs_do_setuid = B_TRUE; 142 break; 143 case TOKEN_EXEC: 144 vfsp->vfs_exec = B_TRUE; 145 vfsp->vfs_do_exec = B_TRUE; 146 break; 147 case TOKEN_NOEXEC: 148 vfsp->vfs_exec = B_FALSE; 149 vfsp->vfs_do_exec = B_TRUE; 150 break; 151 case TOKEN_DEVICES: 152 vfsp->vfs_devices = B_TRUE; 153 vfsp->vfs_do_devices = B_TRUE; 154 break; 155 case TOKEN_NODEVICES: 156 vfsp->vfs_devices = B_FALSE; 157 vfsp->vfs_do_devices = B_TRUE; 158 break; 159 case TOKEN_DIRXATTR: 160 vfsp->vfs_xattr = ZFS_XATTR_DIR; 161 vfsp->vfs_do_xattr = B_TRUE; 162 break; 163 case TOKEN_SAXATTR: 164 vfsp->vfs_xattr = ZFS_XATTR_SA; 165 vfsp->vfs_do_xattr = B_TRUE; 166 break; 167 case TOKEN_XATTR: 168 vfsp->vfs_xattr = ZFS_XATTR_SA; 169 vfsp->vfs_do_xattr = B_TRUE; 170 break; 171 case TOKEN_NOXATTR: 172 vfsp->vfs_xattr = ZFS_XATTR_OFF; 173 vfsp->vfs_do_xattr = B_TRUE; 174 break; 175 case TOKEN_ATIME: 176 vfsp->vfs_atime = B_TRUE; 177 vfsp->vfs_do_atime = B_TRUE; 178 break; 179 case TOKEN_NOATIME: 180 vfsp->vfs_atime = B_FALSE; 181 vfsp->vfs_do_atime = B_TRUE; 182 break; 183 case TOKEN_RELATIME: 184 vfsp->vfs_relatime = B_TRUE; 185 vfsp->vfs_do_relatime = B_TRUE; 186 break; 187 case TOKEN_NORELATIME: 188 vfsp->vfs_relatime = B_FALSE; 189 vfsp->vfs_do_relatime = B_TRUE; 190 break; 191 case TOKEN_NBMAND: 192 vfsp->vfs_nbmand = B_TRUE; 193 vfsp->vfs_do_nbmand = B_TRUE; 194 break; 195 case TOKEN_NONBMAND: 196 vfsp->vfs_nbmand = B_FALSE; 197 vfsp->vfs_do_nbmand = B_TRUE; 198 break; 199 case TOKEN_MNTPOINT: 200 vfsp->vfs_mntpoint = match_strdup(&args[0]); 201 if (vfsp->vfs_mntpoint == NULL) 202 return (SET_ERROR(ENOMEM)); 203 204 break; 205 default: 206 break; 207 } 208 209 return (0); 210 } 211 212 /* 213 * Parse the raw mntopts and return a vfs_t describing the options. 214 */ 215 static int 216 zfsvfs_parse_options(char *mntopts, vfs_t **vfsp) 217 { 218 vfs_t *tmp_vfsp; 219 int error; 220 221 tmp_vfsp = kmem_zalloc(sizeof (vfs_t), KM_SLEEP); 222 223 if (mntopts != NULL) { 224 substring_t args[MAX_OPT_ARGS]; 225 char *tmp_mntopts, *p, *t; 226 int token; 227 228 tmp_mntopts = t = kmem_strdup(mntopts); 229 if (tmp_mntopts == NULL) 230 return (SET_ERROR(ENOMEM)); 231 232 while ((p = strsep(&t, ",")) != NULL) { 233 if (!*p) 234 continue; 235 236 args[0].to = args[0].from = NULL; 237 token = match_token(p, zpl_tokens, args); 238 error = zfsvfs_parse_option(p, token, args, tmp_vfsp); 239 if (error) { 240 kmem_strfree(tmp_mntopts); 241 zfsvfs_vfs_free(tmp_vfsp); 242 return (error); 243 } 244 } 245 246 kmem_strfree(tmp_mntopts); 247 } 248 249 *vfsp = tmp_vfsp; 250 251 return (0); 252 } 253 254 boolean_t 255 zfs_is_readonly(zfsvfs_t *zfsvfs) 256 { 257 return (!!(zfsvfs->z_sb->s_flags & SB_RDONLY)); 258 } 259 260 int 261 zfs_sync(struct super_block *sb, int wait, cred_t *cr) 262 { 263 (void) cr; 264 zfsvfs_t *zfsvfs = sb->s_fs_info; 265 266 /* 267 * Semantically, the only requirement is that the sync be initiated. 268 * The DMU syncs out txgs frequently, so there's nothing to do. 269 */ 270 if (!wait) 271 return (0); 272 273 if (zfsvfs != NULL) { 274 /* 275 * Sync a specific filesystem. 276 */ 277 dsl_pool_t *dp; 278 int error; 279 280 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) 281 return (error); 282 dp = dmu_objset_pool(zfsvfs->z_os); 283 284 /* 285 * If the system is shutting down, then skip any 286 * filesystems which may exist on a suspended pool. 287 */ 288 if (spa_suspended(dp->dp_spa)) { 289 zfs_exit(zfsvfs, FTAG); 290 return (0); 291 } 292 293 if (zfsvfs->z_log != NULL) 294 zil_commit(zfsvfs->z_log, 0); 295 296 zfs_exit(zfsvfs, FTAG); 297 } else { 298 /* 299 * Sync all ZFS filesystems. This is what happens when you 300 * run sync(1). Unlike other filesystems, ZFS honors the 301 * request by waiting for all pools to commit all dirty data. 302 */ 303 spa_sync_allpools(); 304 } 305 306 return (0); 307 } 308 309 static void 310 atime_changed_cb(void *arg, uint64_t newval) 311 { 312 zfsvfs_t *zfsvfs = arg; 313 struct super_block *sb = zfsvfs->z_sb; 314 315 if (sb == NULL) 316 return; 317 /* 318 * Update SB_NOATIME bit in VFS super block. Since atime update is 319 * determined by atime_needs_update(), atime_needs_update() needs to 320 * return false if atime is turned off, and not unconditionally return 321 * false if atime is turned on. 322 */ 323 if (newval) 324 sb->s_flags &= ~SB_NOATIME; 325 else 326 sb->s_flags |= SB_NOATIME; 327 } 328 329 static void 330 relatime_changed_cb(void *arg, uint64_t newval) 331 { 332 ((zfsvfs_t *)arg)->z_relatime = newval; 333 } 334 335 static void 336 xattr_changed_cb(void *arg, uint64_t newval) 337 { 338 zfsvfs_t *zfsvfs = arg; 339 340 if (newval == ZFS_XATTR_OFF) { 341 zfsvfs->z_flags &= ~ZSB_XATTR; 342 } else { 343 zfsvfs->z_flags |= ZSB_XATTR; 344 345 if (newval == ZFS_XATTR_SA) 346 zfsvfs->z_xattr_sa = B_TRUE; 347 else 348 zfsvfs->z_xattr_sa = B_FALSE; 349 } 350 } 351 352 static void 353 acltype_changed_cb(void *arg, uint64_t newval) 354 { 355 zfsvfs_t *zfsvfs = arg; 356 357 switch (newval) { 358 case ZFS_ACLTYPE_NFSV4: 359 case ZFS_ACLTYPE_OFF: 360 zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF; 361 zfsvfs->z_sb->s_flags &= ~SB_POSIXACL; 362 break; 363 case ZFS_ACLTYPE_POSIX: 364 #ifdef CONFIG_FS_POSIX_ACL 365 zfsvfs->z_acl_type = ZFS_ACLTYPE_POSIX; 366 zfsvfs->z_sb->s_flags |= SB_POSIXACL; 367 #else 368 zfsvfs->z_acl_type = ZFS_ACLTYPE_OFF; 369 zfsvfs->z_sb->s_flags &= ~SB_POSIXACL; 370 #endif /* CONFIG_FS_POSIX_ACL */ 371 break; 372 default: 373 break; 374 } 375 } 376 377 static void 378 blksz_changed_cb(void *arg, uint64_t newval) 379 { 380 zfsvfs_t *zfsvfs = arg; 381 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os))); 382 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE); 383 ASSERT(ISP2(newval)); 384 385 zfsvfs->z_max_blksz = newval; 386 } 387 388 static void 389 readonly_changed_cb(void *arg, uint64_t newval) 390 { 391 zfsvfs_t *zfsvfs = arg; 392 struct super_block *sb = zfsvfs->z_sb; 393 394 if (sb == NULL) 395 return; 396 397 if (newval) 398 sb->s_flags |= SB_RDONLY; 399 else 400 sb->s_flags &= ~SB_RDONLY; 401 } 402 403 static void 404 devices_changed_cb(void *arg, uint64_t newval) 405 { 406 } 407 408 static void 409 setuid_changed_cb(void *arg, uint64_t newval) 410 { 411 } 412 413 static void 414 exec_changed_cb(void *arg, uint64_t newval) 415 { 416 } 417 418 static void 419 nbmand_changed_cb(void *arg, uint64_t newval) 420 { 421 zfsvfs_t *zfsvfs = arg; 422 struct super_block *sb = zfsvfs->z_sb; 423 424 if (sb == NULL) 425 return; 426 427 if (newval == TRUE) 428 sb->s_flags |= SB_MANDLOCK; 429 else 430 sb->s_flags &= ~SB_MANDLOCK; 431 } 432 433 static void 434 snapdir_changed_cb(void *arg, uint64_t newval) 435 { 436 ((zfsvfs_t *)arg)->z_show_ctldir = newval; 437 } 438 439 static void 440 acl_mode_changed_cb(void *arg, uint64_t newval) 441 { 442 zfsvfs_t *zfsvfs = arg; 443 444 zfsvfs->z_acl_mode = newval; 445 } 446 447 static void 448 acl_inherit_changed_cb(void *arg, uint64_t newval) 449 { 450 ((zfsvfs_t *)arg)->z_acl_inherit = newval; 451 } 452 453 static void 454 longname_changed_cb(void *arg, uint64_t newval) 455 { 456 ((zfsvfs_t *)arg)->z_longname = newval; 457 } 458 459 static int 460 zfs_register_callbacks(vfs_t *vfsp) 461 { 462 struct dsl_dataset *ds = NULL; 463 objset_t *os = NULL; 464 zfsvfs_t *zfsvfs = NULL; 465 int error = 0; 466 467 ASSERT(vfsp); 468 zfsvfs = vfsp->vfs_data; 469 ASSERT(zfsvfs); 470 os = zfsvfs->z_os; 471 472 /* 473 * The act of registering our callbacks will destroy any mount 474 * options we may have. In order to enable temporary overrides 475 * of mount options, we stash away the current values and 476 * restore them after we register the callbacks. 477 */ 478 if (zfs_is_readonly(zfsvfs) || !spa_writeable(dmu_objset_spa(os))) { 479 vfsp->vfs_do_readonly = B_TRUE; 480 vfsp->vfs_readonly = B_TRUE; 481 } 482 483 /* 484 * Register property callbacks. 485 * 486 * It would probably be fine to just check for i/o error from 487 * the first prop_register(), but I guess I like to go 488 * overboard... 489 */ 490 ds = dmu_objset_ds(os); 491 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 492 error = dsl_prop_register(ds, 493 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs); 494 error = error ? error : dsl_prop_register(ds, 495 zfs_prop_to_name(ZFS_PROP_RELATIME), relatime_changed_cb, zfsvfs); 496 error = error ? error : dsl_prop_register(ds, 497 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs); 498 error = error ? error : dsl_prop_register(ds, 499 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs); 500 error = error ? error : dsl_prop_register(ds, 501 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs); 502 error = error ? error : dsl_prop_register(ds, 503 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs); 504 error = error ? error : dsl_prop_register(ds, 505 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs); 506 error = error ? error : dsl_prop_register(ds, 507 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs); 508 error = error ? error : dsl_prop_register(ds, 509 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs); 510 error = error ? error : dsl_prop_register(ds, 511 zfs_prop_to_name(ZFS_PROP_ACLTYPE), acltype_changed_cb, zfsvfs); 512 error = error ? error : dsl_prop_register(ds, 513 zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs); 514 error = error ? error : dsl_prop_register(ds, 515 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb, 516 zfsvfs); 517 error = error ? error : dsl_prop_register(ds, 518 zfs_prop_to_name(ZFS_PROP_NBMAND), nbmand_changed_cb, zfsvfs); 519 error = error ? error : dsl_prop_register(ds, 520 zfs_prop_to_name(ZFS_PROP_LONGNAME), longname_changed_cb, zfsvfs); 521 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 522 if (error) 523 goto unregister; 524 525 /* 526 * Invoke our callbacks to restore temporary mount options. 527 */ 528 if (vfsp->vfs_do_readonly) 529 readonly_changed_cb(zfsvfs, vfsp->vfs_readonly); 530 if (vfsp->vfs_do_setuid) 531 setuid_changed_cb(zfsvfs, vfsp->vfs_setuid); 532 if (vfsp->vfs_do_exec) 533 exec_changed_cb(zfsvfs, vfsp->vfs_exec); 534 if (vfsp->vfs_do_devices) 535 devices_changed_cb(zfsvfs, vfsp->vfs_devices); 536 if (vfsp->vfs_do_xattr) 537 xattr_changed_cb(zfsvfs, vfsp->vfs_xattr); 538 if (vfsp->vfs_do_atime) 539 atime_changed_cb(zfsvfs, vfsp->vfs_atime); 540 if (vfsp->vfs_do_relatime) 541 relatime_changed_cb(zfsvfs, vfsp->vfs_relatime); 542 if (vfsp->vfs_do_nbmand) 543 nbmand_changed_cb(zfsvfs, vfsp->vfs_nbmand); 544 545 return (0); 546 547 unregister: 548 dsl_prop_unregister_all(ds, zfsvfs); 549 return (error); 550 } 551 552 /* 553 * Takes a dataset, a property, a value and that value's setpoint as 554 * found in the ZAP. Checks if the property has been changed in the vfs. 555 * If so, val and setpoint will be overwritten with updated content. 556 * Otherwise, they are left unchanged. 557 */ 558 int 559 zfs_get_temporary_prop(dsl_dataset_t *ds, zfs_prop_t zfs_prop, uint64_t *val, 560 char *setpoint) 561 { 562 int error; 563 zfsvfs_t *zfvp; 564 vfs_t *vfsp; 565 objset_t *os; 566 uint64_t tmp = *val; 567 568 error = dmu_objset_from_ds(ds, &os); 569 if (error != 0) 570 return (error); 571 572 if (dmu_objset_type(os) != DMU_OST_ZFS) 573 return (EINVAL); 574 575 mutex_enter(&os->os_user_ptr_lock); 576 zfvp = dmu_objset_get_user(os); 577 mutex_exit(&os->os_user_ptr_lock); 578 if (zfvp == NULL) 579 return (ESRCH); 580 581 vfsp = zfvp->z_vfs; 582 583 switch (zfs_prop) { 584 case ZFS_PROP_ATIME: 585 if (vfsp->vfs_do_atime) 586 tmp = vfsp->vfs_atime; 587 break; 588 case ZFS_PROP_RELATIME: 589 if (vfsp->vfs_do_relatime) 590 tmp = vfsp->vfs_relatime; 591 break; 592 case ZFS_PROP_DEVICES: 593 if (vfsp->vfs_do_devices) 594 tmp = vfsp->vfs_devices; 595 break; 596 case ZFS_PROP_EXEC: 597 if (vfsp->vfs_do_exec) 598 tmp = vfsp->vfs_exec; 599 break; 600 case ZFS_PROP_SETUID: 601 if (vfsp->vfs_do_setuid) 602 tmp = vfsp->vfs_setuid; 603 break; 604 case ZFS_PROP_READONLY: 605 if (vfsp->vfs_do_readonly) 606 tmp = vfsp->vfs_readonly; 607 break; 608 case ZFS_PROP_XATTR: 609 if (vfsp->vfs_do_xattr) 610 tmp = vfsp->vfs_xattr; 611 break; 612 case ZFS_PROP_NBMAND: 613 if (vfsp->vfs_do_nbmand) 614 tmp = vfsp->vfs_nbmand; 615 break; 616 default: 617 return (ENOENT); 618 } 619 620 if (tmp != *val) { 621 if (setpoint) 622 (void) strcpy(setpoint, "temporary"); 623 *val = tmp; 624 } 625 return (0); 626 } 627 628 /* 629 * Associate this zfsvfs with the given objset, which must be owned. 630 * This will cache a bunch of on-disk state from the objset in the 631 * zfsvfs. 632 */ 633 static int 634 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os) 635 { 636 int error; 637 uint64_t val; 638 639 zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE; 640 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 641 zfsvfs->z_os = os; 642 643 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version); 644 if (error != 0) 645 return (error); 646 if (zfsvfs->z_version > 647 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) { 648 (void) printk("Can't mount a version %lld file system " 649 "on a version %lld pool\n. Pool must be upgraded to mount " 650 "this file system.\n", (u_longlong_t)zfsvfs->z_version, 651 (u_longlong_t)spa_version(dmu_objset_spa(os))); 652 return (SET_ERROR(ENOTSUP)); 653 } 654 error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val); 655 if (error != 0) 656 return (error); 657 zfsvfs->z_norm = (int)val; 658 659 error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val); 660 if (error != 0) 661 return (error); 662 zfsvfs->z_utf8 = (val != 0); 663 664 error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val); 665 if (error != 0) 666 return (error); 667 zfsvfs->z_case = (uint_t)val; 668 669 if ((error = zfs_get_zplprop(os, ZFS_PROP_ACLTYPE, &val)) != 0) 670 return (error); 671 zfsvfs->z_acl_type = (uint_t)val; 672 673 /* 674 * Fold case on file systems that are always or sometimes case 675 * insensitive. 676 */ 677 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 678 zfsvfs->z_case == ZFS_CASE_MIXED) 679 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 680 681 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 682 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os); 683 684 uint64_t sa_obj = 0; 685 if (zfsvfs->z_use_sa) { 686 /* should either have both of these objects or none */ 687 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, 688 &sa_obj); 689 if (error != 0) 690 return (error); 691 692 error = zfs_get_zplprop(os, ZFS_PROP_XATTR, &val); 693 if ((error == 0) && (val == ZFS_XATTR_SA)) 694 zfsvfs->z_xattr_sa = B_TRUE; 695 } 696 697 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 698 &zfsvfs->z_root); 699 if (error != 0) 700 return (error); 701 ASSERT(zfsvfs->z_root != 0); 702 703 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 704 &zfsvfs->z_unlinkedobj); 705 if (error != 0) 706 return (error); 707 708 error = zap_lookup(os, MASTER_NODE_OBJ, 709 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA], 710 8, 1, &zfsvfs->z_userquota_obj); 711 if (error == ENOENT) 712 zfsvfs->z_userquota_obj = 0; 713 else if (error != 0) 714 return (error); 715 716 error = zap_lookup(os, MASTER_NODE_OBJ, 717 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA], 718 8, 1, &zfsvfs->z_groupquota_obj); 719 if (error == ENOENT) 720 zfsvfs->z_groupquota_obj = 0; 721 else if (error != 0) 722 return (error); 723 724 error = zap_lookup(os, MASTER_NODE_OBJ, 725 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA], 726 8, 1, &zfsvfs->z_projectquota_obj); 727 if (error == ENOENT) 728 zfsvfs->z_projectquota_obj = 0; 729 else if (error != 0) 730 return (error); 731 732 error = zap_lookup(os, MASTER_NODE_OBJ, 733 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA], 734 8, 1, &zfsvfs->z_userobjquota_obj); 735 if (error == ENOENT) 736 zfsvfs->z_userobjquota_obj = 0; 737 else if (error != 0) 738 return (error); 739 740 error = zap_lookup(os, MASTER_NODE_OBJ, 741 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA], 742 8, 1, &zfsvfs->z_groupobjquota_obj); 743 if (error == ENOENT) 744 zfsvfs->z_groupobjquota_obj = 0; 745 else if (error != 0) 746 return (error); 747 748 error = zap_lookup(os, MASTER_NODE_OBJ, 749 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTOBJQUOTA], 750 8, 1, &zfsvfs->z_projectobjquota_obj); 751 if (error == ENOENT) 752 zfsvfs->z_projectobjquota_obj = 0; 753 else if (error != 0) 754 return (error); 755 756 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1, 757 &zfsvfs->z_fuid_obj); 758 if (error == ENOENT) 759 zfsvfs->z_fuid_obj = 0; 760 else if (error != 0) 761 return (error); 762 763 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1, 764 &zfsvfs->z_shares_dir); 765 if (error == ENOENT) 766 zfsvfs->z_shares_dir = 0; 767 else if (error != 0) 768 return (error); 769 770 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 771 &zfsvfs->z_attr_table); 772 if (error != 0) 773 return (error); 774 775 if (zfsvfs->z_version >= ZPL_VERSION_SA) 776 sa_register_update_callback(os, zfs_sa_upgrade); 777 778 return (0); 779 } 780 781 int 782 zfsvfs_create(const char *osname, boolean_t readonly, zfsvfs_t **zfvp) 783 { 784 objset_t *os; 785 zfsvfs_t *zfsvfs; 786 int error; 787 boolean_t ro = (readonly || (strchr(osname, '@') != NULL)); 788 789 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 790 791 error = dmu_objset_own(osname, DMU_OST_ZFS, ro, B_TRUE, zfsvfs, &os); 792 if (error != 0) { 793 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 794 return (error); 795 } 796 797 error = zfsvfs_create_impl(zfvp, zfsvfs, os); 798 799 return (error); 800 } 801 802 803 /* 804 * Note: zfsvfs is assumed to be malloc'd, and will be freed by this function 805 * on a failure. Do not pass in a statically allocated zfsvfs. 806 */ 807 int 808 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os) 809 { 810 int error; 811 812 zfsvfs->z_vfs = NULL; 813 zfsvfs->z_sb = NULL; 814 zfsvfs->z_parent = zfsvfs; 815 816 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 817 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL); 818 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 819 offsetof(znode_t, z_link_node)); 820 ZFS_TEARDOWN_INIT(zfsvfs); 821 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL); 822 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL); 823 824 int size = MIN(1 << (highbit64(zfs_object_mutex_size) - 1), 825 ZFS_OBJ_MTX_MAX); 826 zfsvfs->z_hold_size = size; 827 zfsvfs->z_hold_trees = vmem_zalloc(sizeof (avl_tree_t) * size, 828 KM_SLEEP); 829 zfsvfs->z_hold_locks = vmem_zalloc(sizeof (kmutex_t) * size, KM_SLEEP); 830 for (int i = 0; i != size; i++) { 831 avl_create(&zfsvfs->z_hold_trees[i], zfs_znode_hold_compare, 832 sizeof (znode_hold_t), offsetof(znode_hold_t, zh_node)); 833 mutex_init(&zfsvfs->z_hold_locks[i], NULL, MUTEX_DEFAULT, NULL); 834 } 835 836 error = zfsvfs_init(zfsvfs, os); 837 if (error != 0) { 838 dmu_objset_disown(os, B_TRUE, zfsvfs); 839 *zfvp = NULL; 840 zfsvfs_free(zfsvfs); 841 return (error); 842 } 843 844 zfsvfs->z_drain_task = TASKQID_INVALID; 845 zfsvfs->z_draining = B_FALSE; 846 zfsvfs->z_drain_cancel = B_TRUE; 847 848 *zfvp = zfsvfs; 849 return (0); 850 } 851 852 static int 853 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting) 854 { 855 int error; 856 boolean_t readonly = zfs_is_readonly(zfsvfs); 857 858 error = zfs_register_callbacks(zfsvfs->z_vfs); 859 if (error) 860 return (error); 861 862 /* 863 * If we are not mounting (ie: online recv), then we don't 864 * have to worry about replaying the log as we blocked all 865 * operations out since we closed the ZIL. 866 */ 867 if (mounting) { 868 ASSERT3P(zfsvfs->z_kstat.dk_kstats, ==, NULL); 869 error = dataset_kstats_create(&zfsvfs->z_kstat, zfsvfs->z_os); 870 if (error) 871 return (error); 872 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data, 873 &zfsvfs->z_kstat.dk_zil_sums); 874 875 /* 876 * During replay we remove the read only flag to 877 * allow replays to succeed. 878 */ 879 if (readonly != 0) { 880 readonly_changed_cb(zfsvfs, B_FALSE); 881 } else { 882 zap_stats_t zs; 883 if (zap_get_stats(zfsvfs->z_os, zfsvfs->z_unlinkedobj, 884 &zs) == 0) { 885 dataset_kstats_update_nunlinks_kstat( 886 &zfsvfs->z_kstat, zs.zs_num_entries); 887 dprintf_ds(zfsvfs->z_os->os_dsl_dataset, 888 "num_entries in unlinked set: %llu", 889 zs.zs_num_entries); 890 } 891 zfs_unlinked_drain(zfsvfs); 892 dsl_dir_t *dd = zfsvfs->z_os->os_dsl_dataset->ds_dir; 893 dd->dd_activity_cancelled = B_FALSE; 894 } 895 896 /* 897 * Parse and replay the intent log. 898 * 899 * Because of ziltest, this must be done after 900 * zfs_unlinked_drain(). (Further note: ziltest 901 * doesn't use readonly mounts, where 902 * zfs_unlinked_drain() isn't called.) This is because 903 * ziltest causes spa_sync() to think it's committed, 904 * but actually it is not, so the intent log contains 905 * many txg's worth of changes. 906 * 907 * In particular, if object N is in the unlinked set in 908 * the last txg to actually sync, then it could be 909 * actually freed in a later txg and then reallocated 910 * in a yet later txg. This would write a "create 911 * object N" record to the intent log. Normally, this 912 * would be fine because the spa_sync() would have 913 * written out the fact that object N is free, before 914 * we could write the "create object N" intent log 915 * record. 916 * 917 * But when we are in ziltest mode, we advance the "open 918 * txg" without actually spa_sync()-ing the changes to 919 * disk. So we would see that object N is still 920 * allocated and in the unlinked set, and there is an 921 * intent log record saying to allocate it. 922 */ 923 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) { 924 if (zil_replay_disable) { 925 zil_destroy(zfsvfs->z_log, B_FALSE); 926 } else { 927 zfsvfs->z_replay = B_TRUE; 928 zil_replay(zfsvfs->z_os, zfsvfs, 929 zfs_replay_vector); 930 zfsvfs->z_replay = B_FALSE; 931 } 932 } 933 934 /* restore readonly bit */ 935 if (readonly != 0) 936 readonly_changed_cb(zfsvfs, B_TRUE); 937 } else { 938 ASSERT3P(zfsvfs->z_kstat.dk_kstats, !=, NULL); 939 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data, 940 &zfsvfs->z_kstat.dk_zil_sums); 941 } 942 943 /* 944 * Set the objset user_ptr to track its zfsvfs. 945 */ 946 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 947 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 948 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 949 950 return (0); 951 } 952 953 void 954 zfsvfs_free(zfsvfs_t *zfsvfs) 955 { 956 int i, size = zfsvfs->z_hold_size; 957 958 zfs_fuid_destroy(zfsvfs); 959 960 mutex_destroy(&zfsvfs->z_znodes_lock); 961 mutex_destroy(&zfsvfs->z_lock); 962 list_destroy(&zfsvfs->z_all_znodes); 963 ZFS_TEARDOWN_DESTROY(zfsvfs); 964 rw_destroy(&zfsvfs->z_teardown_inactive_lock); 965 rw_destroy(&zfsvfs->z_fuid_lock); 966 for (i = 0; i != size; i++) { 967 avl_destroy(&zfsvfs->z_hold_trees[i]); 968 mutex_destroy(&zfsvfs->z_hold_locks[i]); 969 } 970 vmem_free(zfsvfs->z_hold_trees, sizeof (avl_tree_t) * size); 971 vmem_free(zfsvfs->z_hold_locks, sizeof (kmutex_t) * size); 972 zfsvfs_vfs_free(zfsvfs->z_vfs); 973 dataset_kstats_destroy(&zfsvfs->z_kstat); 974 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 975 } 976 977 static void 978 zfs_set_fuid_feature(zfsvfs_t *zfsvfs) 979 { 980 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 981 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os); 982 } 983 984 static void 985 zfs_unregister_callbacks(zfsvfs_t *zfsvfs) 986 { 987 objset_t *os = zfsvfs->z_os; 988 989 if (!dmu_objset_is_snapshot(os)) 990 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs); 991 } 992 993 #ifdef HAVE_MLSLABEL 994 /* 995 * Check that the hex label string is appropriate for the dataset being 996 * mounted into the global_zone proper. 997 * 998 * Return an error if the hex label string is not default or 999 * admin_low/admin_high. For admin_low labels, the corresponding 1000 * dataset must be readonly. 1001 */ 1002 int 1003 zfs_check_global_label(const char *dsname, const char *hexsl) 1004 { 1005 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0) 1006 return (0); 1007 if (strcasecmp(hexsl, ADMIN_HIGH) == 0) 1008 return (0); 1009 if (strcasecmp(hexsl, ADMIN_LOW) == 0) { 1010 /* must be readonly */ 1011 uint64_t rdonly; 1012 1013 if (dsl_prop_get_integer(dsname, 1014 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL)) 1015 return (SET_ERROR(EACCES)); 1016 return (rdonly ? 0 : SET_ERROR(EACCES)); 1017 } 1018 return (SET_ERROR(EACCES)); 1019 } 1020 #endif /* HAVE_MLSLABEL */ 1021 1022 static int 1023 zfs_statfs_project(zfsvfs_t *zfsvfs, znode_t *zp, struct kstatfs *statp, 1024 uint32_t bshift) 1025 { 1026 char buf[20 + DMU_OBJACCT_PREFIX_LEN]; 1027 uint64_t offset = DMU_OBJACCT_PREFIX_LEN; 1028 uint64_t quota; 1029 uint64_t used; 1030 int err; 1031 1032 strlcpy(buf, DMU_OBJACCT_PREFIX, DMU_OBJACCT_PREFIX_LEN + 1); 1033 err = zfs_id_to_fuidstr(zfsvfs, NULL, zp->z_projid, buf + offset, 1034 sizeof (buf) - offset, B_FALSE); 1035 if (err) 1036 return (err); 1037 1038 if (zfsvfs->z_projectquota_obj == 0) 1039 goto objs; 1040 1041 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectquota_obj, 1042 buf + offset, 8, 1, "a); 1043 if (err == ENOENT) 1044 goto objs; 1045 else if (err) 1046 return (err); 1047 1048 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT, 1049 buf + offset, 8, 1, &used); 1050 if (unlikely(err == ENOENT)) { 1051 uint32_t blksize; 1052 u_longlong_t nblocks; 1053 1054 /* 1055 * Quota accounting is async, so it is possible race case. 1056 * There is at least one object with the given project ID. 1057 */ 1058 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); 1059 if (unlikely(zp->z_blksz == 0)) 1060 blksize = zfsvfs->z_max_blksz; 1061 1062 used = blksize * nblocks; 1063 } else if (err) { 1064 return (err); 1065 } 1066 1067 statp->f_blocks = quota >> bshift; 1068 statp->f_bfree = (quota > used) ? ((quota - used) >> bshift) : 0; 1069 statp->f_bavail = statp->f_bfree; 1070 1071 objs: 1072 if (zfsvfs->z_projectobjquota_obj == 0) 1073 return (0); 1074 1075 err = zap_lookup(zfsvfs->z_os, zfsvfs->z_projectobjquota_obj, 1076 buf + offset, 8, 1, "a); 1077 if (err == ENOENT) 1078 return (0); 1079 else if (err) 1080 return (err); 1081 1082 err = zap_lookup(zfsvfs->z_os, DMU_PROJECTUSED_OBJECT, 1083 buf, 8, 1, &used); 1084 if (unlikely(err == ENOENT)) { 1085 /* 1086 * Quota accounting is async, so it is possible race case. 1087 * There is at least one object with the given project ID. 1088 */ 1089 used = 1; 1090 } else if (err) { 1091 return (err); 1092 } 1093 1094 statp->f_files = quota; 1095 statp->f_ffree = (quota > used) ? (quota - used) : 0; 1096 1097 return (0); 1098 } 1099 1100 int 1101 zfs_statvfs(struct inode *ip, struct kstatfs *statp) 1102 { 1103 zfsvfs_t *zfsvfs = ITOZSB(ip); 1104 uint64_t refdbytes, availbytes, usedobjs, availobjs; 1105 int err = 0; 1106 1107 if ((err = zfs_enter(zfsvfs, FTAG)) != 0) 1108 return (err); 1109 1110 dmu_objset_space(zfsvfs->z_os, 1111 &refdbytes, &availbytes, &usedobjs, &availobjs); 1112 1113 uint64_t fsid = dmu_objset_fsid_guid(zfsvfs->z_os); 1114 /* 1115 * The underlying storage pool actually uses multiple block 1116 * size. Under Solaris frsize (fragment size) is reported as 1117 * the smallest block size we support, and bsize (block size) 1118 * as the filesystem's maximum block size. Unfortunately, 1119 * under Linux the fragment size and block size are often used 1120 * interchangeably. Thus we are forced to report both of them 1121 * as the filesystem's maximum block size. 1122 */ 1123 statp->f_frsize = zfsvfs->z_max_blksz; 1124 statp->f_bsize = zfsvfs->z_max_blksz; 1125 uint32_t bshift = fls(statp->f_bsize) - 1; 1126 1127 /* 1128 * The following report "total" blocks of various kinds in 1129 * the file system, but reported in terms of f_bsize - the 1130 * "preferred" size. 1131 */ 1132 1133 /* Round up so we never have a filesystem using 0 blocks. */ 1134 refdbytes = P2ROUNDUP(refdbytes, statp->f_bsize); 1135 statp->f_blocks = (refdbytes + availbytes) >> bshift; 1136 statp->f_bfree = availbytes >> bshift; 1137 statp->f_bavail = statp->f_bfree; /* no root reservation */ 1138 1139 /* 1140 * statvfs() should really be called statufs(), because it assumes 1141 * static metadata. ZFS doesn't preallocate files, so the best 1142 * we can do is report the max that could possibly fit in f_files, 1143 * and that minus the number actually used in f_ffree. 1144 * For f_ffree, report the smaller of the number of objects available 1145 * and the number of blocks (each object will take at least a block). 1146 */ 1147 statp->f_ffree = MIN(availobjs, availbytes >> DNODE_SHIFT); 1148 statp->f_files = statp->f_ffree + usedobjs; 1149 statp->f_fsid.val[0] = (uint32_t)fsid; 1150 statp->f_fsid.val[1] = (uint32_t)(fsid >> 32); 1151 statp->f_type = ZFS_SUPER_MAGIC; 1152 statp->f_namelen = 1153 zfsvfs->z_longname ? (ZAP_MAXNAMELEN_NEW - 1) : (MAXNAMELEN - 1); 1154 1155 /* 1156 * We have all of 40 characters to stuff a string here. 1157 * Is there anything useful we could/should provide? 1158 */ 1159 memset(statp->f_spare, 0, sizeof (statp->f_spare)); 1160 1161 if (dmu_objset_projectquota_enabled(zfsvfs->z_os) && 1162 dmu_objset_projectquota_present(zfsvfs->z_os)) { 1163 znode_t *zp = ITOZ(ip); 1164 1165 if (zp->z_pflags & ZFS_PROJINHERIT && zp->z_projid && 1166 zpl_is_valid_projid(zp->z_projid)) 1167 err = zfs_statfs_project(zfsvfs, zp, statp, bshift); 1168 } 1169 1170 zfs_exit(zfsvfs, FTAG); 1171 return (err); 1172 } 1173 1174 static int 1175 zfs_root(zfsvfs_t *zfsvfs, struct inode **ipp) 1176 { 1177 znode_t *rootzp; 1178 int error; 1179 1180 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) 1181 return (error); 1182 1183 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 1184 if (error == 0) 1185 *ipp = ZTOI(rootzp); 1186 1187 zfs_exit(zfsvfs, FTAG); 1188 return (error); 1189 } 1190 1191 /* 1192 * The ARC has requested that the filesystem drop entries from the dentry 1193 * and inode caches. This can occur when the ARC needs to free meta data 1194 * blocks but can't because they are all pinned by entries in these caches. 1195 */ 1196 #if defined(HAVE_SUPER_BLOCK_S_SHRINK) 1197 #define S_SHRINK(sb) (&(sb)->s_shrink) 1198 #elif defined(HAVE_SUPER_BLOCK_S_SHRINK_PTR) 1199 #define S_SHRINK(sb) ((sb)->s_shrink) 1200 #endif 1201 1202 int 1203 zfs_prune(struct super_block *sb, unsigned long nr_to_scan, int *objects) 1204 { 1205 zfsvfs_t *zfsvfs = sb->s_fs_info; 1206 int error = 0; 1207 struct shrinker *shrinker = S_SHRINK(sb); 1208 struct shrink_control sc = { 1209 .nr_to_scan = nr_to_scan, 1210 .gfp_mask = GFP_KERNEL, 1211 }; 1212 1213 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) 1214 return (error); 1215 1216 #ifdef SHRINKER_NUMA_AWARE 1217 if (shrinker->flags & SHRINKER_NUMA_AWARE) { 1218 long tc = 1; 1219 for_each_online_node(sc.nid) { 1220 long c = shrinker->count_objects(shrinker, &sc); 1221 if (c == 0 || c == SHRINK_EMPTY) 1222 continue; 1223 tc += c; 1224 } 1225 *objects = 0; 1226 for_each_online_node(sc.nid) { 1227 long c = shrinker->count_objects(shrinker, &sc); 1228 if (c == 0 || c == SHRINK_EMPTY) 1229 continue; 1230 if (c > tc) 1231 tc = c; 1232 sc.nr_to_scan = mult_frac(nr_to_scan, c, tc) + 1; 1233 *objects += (*shrinker->scan_objects)(shrinker, &sc); 1234 } 1235 } else { 1236 *objects = (*shrinker->scan_objects)(shrinker, &sc); 1237 } 1238 #else 1239 *objects = (*shrinker->scan_objects)(shrinker, &sc); 1240 #endif 1241 1242 zfs_exit(zfsvfs, FTAG); 1243 1244 dprintf_ds(zfsvfs->z_os->os_dsl_dataset, 1245 "pruning, nr_to_scan=%lu objects=%d error=%d\n", 1246 nr_to_scan, *objects, error); 1247 1248 return (error); 1249 } 1250 1251 /* 1252 * Teardown the zfsvfs_t. 1253 * 1254 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock' 1255 * and 'z_teardown_inactive_lock' held. 1256 */ 1257 static int 1258 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting) 1259 { 1260 znode_t *zp; 1261 1262 zfs_unlinked_drain_stop_wait(zfsvfs); 1263 1264 /* 1265 * If someone has not already unmounted this file system, 1266 * drain the zrele_taskq to ensure all active references to the 1267 * zfsvfs_t have been handled only then can it be safely destroyed. 1268 */ 1269 if (zfsvfs->z_os) { 1270 /* 1271 * If we're unmounting we have to wait for the list to 1272 * drain completely. 1273 * 1274 * If we're not unmounting there's no guarantee the list 1275 * will drain completely, but iputs run from the taskq 1276 * may add the parents of dir-based xattrs to the taskq 1277 * so we want to wait for these. 1278 * 1279 * We can safely check z_all_znodes for being empty because the 1280 * VFS has already blocked operations which add to it. 1281 */ 1282 int round = 0; 1283 while (!list_is_empty(&zfsvfs->z_all_znodes)) { 1284 taskq_wait_outstanding(dsl_pool_zrele_taskq( 1285 dmu_objset_pool(zfsvfs->z_os)), 0); 1286 if (++round > 1 && !unmounting) 1287 break; 1288 } 1289 } 1290 1291 ZFS_TEARDOWN_ENTER_WRITE(zfsvfs, FTAG); 1292 1293 if (!unmounting) { 1294 /* 1295 * We purge the parent filesystem's super block as the 1296 * parent filesystem and all of its snapshots have their 1297 * inode's super block set to the parent's filesystem's 1298 * super block. Note, 'z_parent' is self referential 1299 * for non-snapshots. 1300 */ 1301 shrink_dcache_sb(zfsvfs->z_parent->z_sb); 1302 } 1303 1304 /* 1305 * Close the zil. NB: Can't close the zil while zfs_inactive 1306 * threads are blocked as zil_close can call zfs_inactive. 1307 */ 1308 if (zfsvfs->z_log) { 1309 zil_close(zfsvfs->z_log); 1310 zfsvfs->z_log = NULL; 1311 } 1312 1313 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER); 1314 1315 /* 1316 * If we are not unmounting (ie: online recv) and someone already 1317 * unmounted this file system while we were doing the switcheroo, 1318 * or a reopen of z_os failed then just bail out now. 1319 */ 1320 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) { 1321 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1322 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG); 1323 return (SET_ERROR(EIO)); 1324 } 1325 1326 /* 1327 * At this point there are no VFS ops active, and any new VFS ops 1328 * will fail with EIO since we have z_teardown_lock for writer (only 1329 * relevant for forced unmount). 1330 * 1331 * Release all holds on dbufs. We also grab an extra reference to all 1332 * the remaining inodes so that the kernel does not attempt to free 1333 * any inodes of a suspended fs. This can cause deadlocks since the 1334 * zfs_resume_fs() process may involve starting threads, which might 1335 * attempt to free unreferenced inodes to free up memory for the new 1336 * thread. 1337 */ 1338 if (!unmounting) { 1339 mutex_enter(&zfsvfs->z_znodes_lock); 1340 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL; 1341 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 1342 if (zp->z_sa_hdl) 1343 zfs_znode_dmu_fini(zp); 1344 if (igrab(ZTOI(zp)) != NULL) 1345 zp->z_suspended = B_TRUE; 1346 1347 } 1348 mutex_exit(&zfsvfs->z_znodes_lock); 1349 } 1350 1351 /* 1352 * If we are unmounting, set the unmounted flag and let new VFS ops 1353 * unblock. zfs_inactive will have the unmounted behavior, and all 1354 * other VFS ops will fail with EIO. 1355 */ 1356 if (unmounting) { 1357 zfsvfs->z_unmounted = B_TRUE; 1358 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1359 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG); 1360 } 1361 1362 /* 1363 * z_os will be NULL if there was an error in attempting to reopen 1364 * zfsvfs, so just return as the properties had already been 1365 * 1366 * unregistered and cached data had been evicted before. 1367 */ 1368 if (zfsvfs->z_os == NULL) 1369 return (0); 1370 1371 /* 1372 * Unregister properties. 1373 */ 1374 zfs_unregister_callbacks(zfsvfs); 1375 1376 /* 1377 * Evict cached data. We must write out any dirty data before 1378 * disowning the dataset. 1379 */ 1380 objset_t *os = zfsvfs->z_os; 1381 boolean_t os_dirty = B_FALSE; 1382 for (int t = 0; t < TXG_SIZE; t++) { 1383 if (dmu_objset_is_dirty(os, t)) { 1384 os_dirty = B_TRUE; 1385 break; 1386 } 1387 } 1388 if (!zfs_is_readonly(zfsvfs) && os_dirty) { 1389 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 1390 } 1391 dmu_objset_evict_dbufs(zfsvfs->z_os); 1392 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 1393 dsl_dir_cancel_waiters(dd); 1394 1395 return (0); 1396 } 1397 1398 static atomic_long_t zfs_bdi_seq = ATOMIC_LONG_INIT(0); 1399 1400 int 1401 zfs_domount(struct super_block *sb, zfs_mnt_t *zm, int silent) 1402 { 1403 const char *osname = zm->mnt_osname; 1404 struct inode *root_inode = NULL; 1405 uint64_t recordsize; 1406 int error = 0; 1407 zfsvfs_t *zfsvfs = NULL; 1408 vfs_t *vfs = NULL; 1409 int canwrite; 1410 int dataset_visible_zone; 1411 1412 ASSERT(zm); 1413 ASSERT(osname); 1414 1415 dataset_visible_zone = zone_dataset_visible(osname, &canwrite); 1416 1417 /* 1418 * Refuse to mount a filesystem if we are in a namespace and the 1419 * dataset is not visible or writable in that namespace. 1420 */ 1421 if (!INGLOBALZONE(curproc) && 1422 (!dataset_visible_zone || !canwrite)) { 1423 return (SET_ERROR(EPERM)); 1424 } 1425 1426 error = zfsvfs_parse_options(zm->mnt_data, &vfs); 1427 if (error) 1428 return (error); 1429 1430 /* 1431 * If a non-writable filesystem is being mounted without the 1432 * read-only flag, pretend it was set, as done for snapshots. 1433 */ 1434 if (!canwrite) 1435 vfs->vfs_readonly = B_TRUE; 1436 1437 error = zfsvfs_create(osname, vfs->vfs_readonly, &zfsvfs); 1438 if (error) { 1439 zfsvfs_vfs_free(vfs); 1440 goto out; 1441 } 1442 1443 if ((error = dsl_prop_get_integer(osname, "recordsize", 1444 &recordsize, NULL))) { 1445 zfsvfs_vfs_free(vfs); 1446 goto out; 1447 } 1448 1449 vfs->vfs_data = zfsvfs; 1450 zfsvfs->z_vfs = vfs; 1451 zfsvfs->z_sb = sb; 1452 sb->s_fs_info = zfsvfs; 1453 sb->s_magic = ZFS_SUPER_MAGIC; 1454 sb->s_maxbytes = MAX_LFS_FILESIZE; 1455 sb->s_time_gran = 1; 1456 sb->s_blocksize = recordsize; 1457 sb->s_blocksize_bits = ilog2(recordsize); 1458 1459 error = -super_setup_bdi_name(sb, "%.28s-%ld", "zfs", 1460 atomic_long_inc_return(&zfs_bdi_seq)); 1461 if (error) 1462 goto out; 1463 1464 sb->s_bdi->ra_pages = 0; 1465 1466 /* Set callback operations for the file system. */ 1467 sb->s_op = &zpl_super_operations; 1468 sb->s_xattr = zpl_xattr_handlers; 1469 sb->s_export_op = &zpl_export_operations; 1470 1471 /* Set features for file system. */ 1472 zfs_set_fuid_feature(zfsvfs); 1473 1474 if (dmu_objset_is_snapshot(zfsvfs->z_os)) { 1475 uint64_t pval; 1476 1477 atime_changed_cb(zfsvfs, B_FALSE); 1478 readonly_changed_cb(zfsvfs, B_TRUE); 1479 if ((error = dsl_prop_get_integer(osname, 1480 "xattr", &pval, NULL))) 1481 goto out; 1482 xattr_changed_cb(zfsvfs, pval); 1483 if ((error = dsl_prop_get_integer(osname, 1484 "acltype", &pval, NULL))) 1485 goto out; 1486 acltype_changed_cb(zfsvfs, pval); 1487 zfsvfs->z_issnap = B_TRUE; 1488 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED; 1489 zfsvfs->z_snap_defer_time = jiffies; 1490 1491 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 1492 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 1493 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 1494 } else { 1495 if ((error = zfsvfs_setup(zfsvfs, B_TRUE))) 1496 goto out; 1497 } 1498 1499 /* Allocate a root inode for the filesystem. */ 1500 error = zfs_root(zfsvfs, &root_inode); 1501 if (error) { 1502 (void) zfs_umount(sb); 1503 zfsvfs = NULL; /* avoid double-free; first in zfs_umount */ 1504 goto out; 1505 } 1506 1507 /* Allocate a root dentry for the filesystem */ 1508 sb->s_root = d_make_root(root_inode); 1509 if (sb->s_root == NULL) { 1510 (void) zfs_umount(sb); 1511 zfsvfs = NULL; /* avoid double-free; first in zfs_umount */ 1512 error = SET_ERROR(ENOMEM); 1513 goto out; 1514 } 1515 1516 if (!zfsvfs->z_issnap) 1517 zfsctl_create(zfsvfs); 1518 1519 zfsvfs->z_arc_prune = arc_add_prune_callback(zpl_prune_sb, sb); 1520 out: 1521 if (error) { 1522 if (zfsvfs != NULL) { 1523 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs); 1524 zfsvfs_free(zfsvfs); 1525 } 1526 /* 1527 * make sure we don't have dangling sb->s_fs_info which 1528 * zfs_preumount will use. 1529 */ 1530 sb->s_fs_info = NULL; 1531 } 1532 1533 return (error); 1534 } 1535 1536 /* 1537 * Called when an unmount is requested and certain sanity checks have 1538 * already passed. At this point no dentries or inodes have been reclaimed 1539 * from their respective caches. We drop the extra reference on the .zfs 1540 * control directory to allow everything to be reclaimed. All snapshots 1541 * must already have been unmounted to reach this point. 1542 */ 1543 void 1544 zfs_preumount(struct super_block *sb) 1545 { 1546 zfsvfs_t *zfsvfs = sb->s_fs_info; 1547 1548 /* zfsvfs is NULL when zfs_domount fails during mount */ 1549 if (zfsvfs) { 1550 zfs_unlinked_drain_stop_wait(zfsvfs); 1551 zfsctl_destroy(sb->s_fs_info); 1552 /* 1553 * Wait for zrele_async before entering evict_inodes in 1554 * generic_shutdown_super. The reason we must finish before 1555 * evict_inodes is when lazytime is on, or when zfs_purgedir 1556 * calls zfs_zget, zrele would bump i_count from 0 to 1. This 1557 * would race with the i_count check in evict_inodes. This means 1558 * it could destroy the inode while we are still using it. 1559 * 1560 * We wait for two passes. xattr directories in the first pass 1561 * may add xattr entries in zfs_purgedir, so in the second pass 1562 * we wait for them. We don't use taskq_wait here because it is 1563 * a pool wide taskq. Other mounted filesystems can constantly 1564 * do zrele_async and there's no guarantee when taskq will be 1565 * empty. 1566 */ 1567 taskq_wait_outstanding(dsl_pool_zrele_taskq( 1568 dmu_objset_pool(zfsvfs->z_os)), 0); 1569 taskq_wait_outstanding(dsl_pool_zrele_taskq( 1570 dmu_objset_pool(zfsvfs->z_os)), 0); 1571 } 1572 } 1573 1574 /* 1575 * Called once all other unmount released tear down has occurred. 1576 * It is our responsibility to release any remaining infrastructure. 1577 */ 1578 int 1579 zfs_umount(struct super_block *sb) 1580 { 1581 zfsvfs_t *zfsvfs = sb->s_fs_info; 1582 objset_t *os; 1583 1584 if (zfsvfs->z_arc_prune != NULL) 1585 arc_remove_prune_callback(zfsvfs->z_arc_prune); 1586 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0); 1587 os = zfsvfs->z_os; 1588 1589 /* 1590 * z_os will be NULL if there was an error in 1591 * attempting to reopen zfsvfs. 1592 */ 1593 if (os != NULL) { 1594 /* 1595 * Unset the objset user_ptr. 1596 */ 1597 mutex_enter(&os->os_user_ptr_lock); 1598 dmu_objset_set_user(os, NULL); 1599 mutex_exit(&os->os_user_ptr_lock); 1600 1601 /* 1602 * Finally release the objset 1603 */ 1604 dmu_objset_disown(os, B_TRUE, zfsvfs); 1605 } 1606 1607 zfsvfs_free(zfsvfs); 1608 sb->s_fs_info = NULL; 1609 return (0); 1610 } 1611 1612 int 1613 zfs_remount(struct super_block *sb, int *flags, zfs_mnt_t *zm) 1614 { 1615 zfsvfs_t *zfsvfs = sb->s_fs_info; 1616 vfs_t *vfsp; 1617 boolean_t issnap = dmu_objset_is_snapshot(zfsvfs->z_os); 1618 int error; 1619 1620 if ((issnap || !spa_writeable(dmu_objset_spa(zfsvfs->z_os))) && 1621 !(*flags & SB_RDONLY)) { 1622 *flags |= SB_RDONLY; 1623 return (EROFS); 1624 } 1625 1626 error = zfsvfs_parse_options(zm->mnt_data, &vfsp); 1627 if (error) 1628 return (error); 1629 1630 if (!zfs_is_readonly(zfsvfs) && (*flags & SB_RDONLY)) 1631 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 1632 1633 zfs_unregister_callbacks(zfsvfs); 1634 zfsvfs_vfs_free(zfsvfs->z_vfs); 1635 1636 vfsp->vfs_data = zfsvfs; 1637 zfsvfs->z_vfs = vfsp; 1638 if (!issnap) 1639 (void) zfs_register_callbacks(vfsp); 1640 1641 return (error); 1642 } 1643 1644 int 1645 zfs_vget(struct super_block *sb, struct inode **ipp, fid_t *fidp) 1646 { 1647 zfsvfs_t *zfsvfs = sb->s_fs_info; 1648 znode_t *zp; 1649 uint64_t object = 0; 1650 uint64_t fid_gen = 0; 1651 uint64_t gen_mask; 1652 uint64_t zp_gen; 1653 int i, err; 1654 1655 *ipp = NULL; 1656 1657 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 1658 zfid_short_t *zfid = (zfid_short_t *)fidp; 1659 1660 for (i = 0; i < sizeof (zfid->zf_object); i++) 1661 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 1662 1663 for (i = 0; i < sizeof (zfid->zf_gen); i++) 1664 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 1665 } else { 1666 return (SET_ERROR(EINVAL)); 1667 } 1668 1669 /* LONG_FID_LEN means snapdirs */ 1670 if (fidp->fid_len == LONG_FID_LEN) { 1671 zfid_long_t *zlfid = (zfid_long_t *)fidp; 1672 uint64_t objsetid = 0; 1673 uint64_t setgen = 0; 1674 1675 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 1676 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 1677 1678 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 1679 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 1680 1681 if (objsetid != ZFSCTL_INO_SNAPDIRS - object) { 1682 dprintf("snapdir fid: objsetid (%llu) != " 1683 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n", 1684 objsetid, ZFSCTL_INO_SNAPDIRS, object); 1685 1686 return (SET_ERROR(EINVAL)); 1687 } 1688 1689 if (fid_gen > 1 || setgen != 0) { 1690 dprintf("snapdir fid: fid_gen (%llu) and setgen " 1691 "(%llu)\n", fid_gen, setgen); 1692 return (SET_ERROR(EINVAL)); 1693 } 1694 1695 return (zfsctl_snapdir_vget(sb, objsetid, fid_gen, ipp)); 1696 } 1697 1698 if ((err = zfs_enter(zfsvfs, FTAG)) != 0) 1699 return (err); 1700 /* A zero fid_gen means we are in the .zfs control directories */ 1701 if (fid_gen == 0 && 1702 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) { 1703 *ipp = zfsvfs->z_ctldir; 1704 ASSERT(*ipp != NULL); 1705 1706 if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) { 1707 return (SET_ERROR(ENOENT)); 1708 } 1709 1710 if (object == ZFSCTL_INO_SNAPDIR) { 1711 VERIFY(zfsctl_root_lookup(*ipp, "snapshot", ipp, 1712 0, kcred, NULL, NULL) == 0); 1713 } else { 1714 /* 1715 * Must have an existing ref, so igrab() 1716 * cannot return NULL 1717 */ 1718 VERIFY3P(igrab(*ipp), !=, NULL); 1719 } 1720 zfs_exit(zfsvfs, FTAG); 1721 return (0); 1722 } 1723 1724 gen_mask = -1ULL >> (64 - 8 * i); 1725 1726 dprintf("getting %llu [%llu mask %llx]\n", object, fid_gen, gen_mask); 1727 if ((err = zfs_zget(zfsvfs, object, &zp))) { 1728 zfs_exit(zfsvfs, FTAG); 1729 return (err); 1730 } 1731 1732 /* Don't export xattr stuff */ 1733 if (zp->z_pflags & ZFS_XATTR) { 1734 zrele(zp); 1735 zfs_exit(zfsvfs, FTAG); 1736 return (SET_ERROR(ENOENT)); 1737 } 1738 1739 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen, 1740 sizeof (uint64_t)); 1741 zp_gen = zp_gen & gen_mask; 1742 if (zp_gen == 0) 1743 zp_gen = 1; 1744 if ((fid_gen == 0) && (zfsvfs->z_root == object)) 1745 fid_gen = zp_gen; 1746 if (zp->z_unlinked || zp_gen != fid_gen) { 1747 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen, 1748 fid_gen); 1749 zrele(zp); 1750 zfs_exit(zfsvfs, FTAG); 1751 return (SET_ERROR(ENOENT)); 1752 } 1753 1754 *ipp = ZTOI(zp); 1755 if (*ipp) 1756 zfs_znode_update_vfs(ITOZ(*ipp)); 1757 1758 zfs_exit(zfsvfs, FTAG); 1759 return (0); 1760 } 1761 1762 /* 1763 * Block out VFS ops and close zfsvfs_t 1764 * 1765 * Note, if successful, then we return with the 'z_teardown_lock' and 1766 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying 1767 * dataset and objset intact so that they can be atomically handed off during 1768 * a subsequent rollback or recv operation and the resume thereafter. 1769 */ 1770 int 1771 zfs_suspend_fs(zfsvfs_t *zfsvfs) 1772 { 1773 int error; 1774 1775 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0) 1776 return (error); 1777 1778 return (0); 1779 } 1780 1781 /* 1782 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset 1783 * is an invariant across any of the operations that can be performed while the 1784 * filesystem was suspended. Whether it succeeded or failed, the preconditions 1785 * are the same: the relevant objset and associated dataset are owned by 1786 * zfsvfs, held, and long held on entry. 1787 */ 1788 int 1789 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds) 1790 { 1791 int err, err2; 1792 znode_t *zp; 1793 1794 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs)); 1795 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 1796 1797 /* 1798 * We already own this, so just update the objset_t, as the one we 1799 * had before may have been evicted. 1800 */ 1801 objset_t *os; 1802 VERIFY3P(ds->ds_owner, ==, zfsvfs); 1803 VERIFY(dsl_dataset_long_held(ds)); 1804 dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds)); 1805 dsl_pool_config_enter(dp, FTAG); 1806 VERIFY0(dmu_objset_from_ds(ds, &os)); 1807 dsl_pool_config_exit(dp, FTAG); 1808 1809 err = zfsvfs_init(zfsvfs, os); 1810 if (err != 0) 1811 goto bail; 1812 1813 ds->ds_dir->dd_activity_cancelled = B_FALSE; 1814 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0); 1815 1816 zfs_set_fuid_feature(zfsvfs); 1817 zfsvfs->z_rollback_time = jiffies; 1818 1819 /* 1820 * Attempt to re-establish all the active inodes with their 1821 * dbufs. If a zfs_rezget() fails, then we unhash the inode 1822 * and mark it stale. This prevents a collision if a new 1823 * inode/object is created which must use the same inode 1824 * number. The stale inode will be be released when the 1825 * VFS prunes the dentry holding the remaining references 1826 * on the stale inode. 1827 */ 1828 mutex_enter(&zfsvfs->z_znodes_lock); 1829 for (zp = list_head(&zfsvfs->z_all_znodes); zp; 1830 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 1831 err2 = zfs_rezget(zp); 1832 if (err2) { 1833 zpl_d_drop_aliases(ZTOI(zp)); 1834 remove_inode_hash(ZTOI(zp)); 1835 } 1836 1837 /* see comment in zfs_suspend_fs() */ 1838 if (zp->z_suspended) { 1839 zfs_zrele_async(zp); 1840 zp->z_suspended = B_FALSE; 1841 } 1842 } 1843 mutex_exit(&zfsvfs->z_znodes_lock); 1844 1845 if (!zfs_is_readonly(zfsvfs) && !zfsvfs->z_unmounted) { 1846 /* 1847 * zfs_suspend_fs() could have interrupted freeing 1848 * of dnodes. We need to restart this freeing so 1849 * that we don't "leak" the space. 1850 */ 1851 zfs_unlinked_drain(zfsvfs); 1852 } 1853 1854 /* 1855 * Most of the time zfs_suspend_fs is used for changing the contents 1856 * of the underlying dataset. ZFS rollback and receive operations 1857 * might create files for which negative dentries are present in 1858 * the cache. Since walking the dcache would require a lot of GPL-only 1859 * code duplication, it's much easier on these rather rare occasions 1860 * just to flush the whole dcache for the given dataset/filesystem. 1861 */ 1862 shrink_dcache_sb(zfsvfs->z_sb); 1863 1864 bail: 1865 if (err != 0) 1866 zfsvfs->z_unmounted = B_TRUE; 1867 1868 /* release the VFS ops */ 1869 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1870 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG); 1871 1872 if (err != 0) { 1873 /* 1874 * Since we couldn't setup the sa framework, try to force 1875 * unmount this file system. 1876 */ 1877 if (zfsvfs->z_os) 1878 (void) zfs_umount(zfsvfs->z_sb); 1879 } 1880 return (err); 1881 } 1882 1883 /* 1884 * Release VOPs and unmount a suspended filesystem. 1885 */ 1886 int 1887 zfs_end_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds) 1888 { 1889 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs)); 1890 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 1891 1892 /* 1893 * We already own this, so just hold and rele it to update the 1894 * objset_t, as the one we had before may have been evicted. 1895 */ 1896 objset_t *os; 1897 VERIFY3P(ds->ds_owner, ==, zfsvfs); 1898 VERIFY(dsl_dataset_long_held(ds)); 1899 dsl_pool_t *dp = spa_get_dsl(dsl_dataset_get_spa(ds)); 1900 dsl_pool_config_enter(dp, FTAG); 1901 VERIFY0(dmu_objset_from_ds(ds, &os)); 1902 dsl_pool_config_exit(dp, FTAG); 1903 zfsvfs->z_os = os; 1904 1905 /* release the VOPs */ 1906 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1907 ZFS_TEARDOWN_EXIT(zfsvfs, FTAG); 1908 1909 /* 1910 * Try to force unmount this file system. 1911 */ 1912 (void) zfs_umount(zfsvfs->z_sb); 1913 zfsvfs->z_unmounted = B_TRUE; 1914 return (0); 1915 } 1916 1917 /* 1918 * Automounted snapshots rely on periodic revalidation 1919 * to defer snapshots from being automatically unmounted. 1920 */ 1921 1922 inline void 1923 zfs_exit_fs(zfsvfs_t *zfsvfs) 1924 { 1925 if (!zfsvfs->z_issnap) 1926 return; 1927 1928 if (time_after(jiffies, zfsvfs->z_snap_defer_time + 1929 MAX(zfs_expire_snapshot * HZ / 2, HZ))) { 1930 zfsvfs->z_snap_defer_time = jiffies; 1931 zfsctl_snapshot_unmount_delay(zfsvfs->z_os->os_spa, 1932 dmu_objset_id(zfsvfs->z_os), 1933 zfs_expire_snapshot); 1934 } 1935 } 1936 1937 int 1938 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers) 1939 { 1940 int error; 1941 objset_t *os = zfsvfs->z_os; 1942 dmu_tx_t *tx; 1943 1944 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION) 1945 return (SET_ERROR(EINVAL)); 1946 1947 if (newvers < zfsvfs->z_version) 1948 return (SET_ERROR(EINVAL)); 1949 1950 if (zfs_spa_version_map(newvers) > 1951 spa_version(dmu_objset_spa(zfsvfs->z_os))) 1952 return (SET_ERROR(ENOTSUP)); 1953 1954 tx = dmu_tx_create(os); 1955 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR); 1956 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) { 1957 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE, 1958 ZFS_SA_ATTRS); 1959 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1960 } 1961 error = dmu_tx_assign(tx, TXG_WAIT); 1962 if (error) { 1963 dmu_tx_abort(tx); 1964 return (error); 1965 } 1966 1967 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1968 8, 1, &newvers, tx); 1969 1970 if (error) { 1971 dmu_tx_commit(tx); 1972 return (error); 1973 } 1974 1975 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) { 1976 uint64_t sa_obj; 1977 1978 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=, 1979 SPA_VERSION_SA); 1980 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 1981 DMU_OT_NONE, 0, tx); 1982 1983 error = zap_add(os, MASTER_NODE_OBJ, 1984 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 1985 ASSERT0(error); 1986 1987 VERIFY(0 == sa_set_sa_object(os, sa_obj)); 1988 sa_register_update_callback(os, zfs_sa_upgrade); 1989 } 1990 1991 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx, 1992 "from %llu to %llu", zfsvfs->z_version, newvers); 1993 1994 dmu_tx_commit(tx); 1995 1996 zfsvfs->z_version = newvers; 1997 os->os_version = newvers; 1998 1999 zfs_set_fuid_feature(zfsvfs); 2000 2001 return (0); 2002 } 2003 2004 /* 2005 * Return true if the corresponding vfs's unmounted flag is set. 2006 * Otherwise return false. 2007 * If this function returns true we know VFS unmount has been initiated. 2008 */ 2009 boolean_t 2010 zfs_get_vfs_flag_unmounted(objset_t *os) 2011 { 2012 zfsvfs_t *zfvp; 2013 boolean_t unmounted = B_FALSE; 2014 2015 ASSERT(dmu_objset_type(os) == DMU_OST_ZFS); 2016 2017 mutex_enter(&os->os_user_ptr_lock); 2018 zfvp = dmu_objset_get_user(os); 2019 if (zfvp != NULL && zfvp->z_unmounted) 2020 unmounted = B_TRUE; 2021 mutex_exit(&os->os_user_ptr_lock); 2022 2023 return (unmounted); 2024 } 2025 2026 void 2027 zfsvfs_update_fromname(const char *oldname, const char *newname) 2028 { 2029 /* 2030 * We don't need to do anything here, the devname is always current by 2031 * virtue of zfsvfs->z_sb->s_op->show_devname. 2032 */ 2033 (void) oldname, (void) newname; 2034 } 2035 2036 void 2037 zfs_init(void) 2038 { 2039 zfsctl_init(); 2040 zfs_znode_init(); 2041 dmu_objset_register_type(DMU_OST_ZFS, zpl_get_file_info); 2042 register_filesystem(&zpl_fs_type); 2043 } 2044 2045 void 2046 zfs_fini(void) 2047 { 2048 /* 2049 * we don't use outstanding because zpl_posix_acl_free might add more. 2050 */ 2051 taskq_wait(system_delay_taskq); 2052 taskq_wait(system_taskq); 2053 unregister_filesystem(&zpl_fs_type); 2054 zfs_znode_fini(); 2055 zfsctl_fini(); 2056 } 2057 2058 #if defined(_KERNEL) 2059 EXPORT_SYMBOL(zfs_suspend_fs); 2060 EXPORT_SYMBOL(zfs_resume_fs); 2061 EXPORT_SYMBOL(zfs_set_version); 2062 EXPORT_SYMBOL(zfsvfs_create); 2063 EXPORT_SYMBOL(zfsvfs_free); 2064 EXPORT_SYMBOL(zfs_is_readonly); 2065 EXPORT_SYMBOL(zfs_domount); 2066 EXPORT_SYMBOL(zfs_preumount); 2067 EXPORT_SYMBOL(zfs_umount); 2068 EXPORT_SYMBOL(zfs_remount); 2069 EXPORT_SYMBOL(zfs_statvfs); 2070 EXPORT_SYMBOL(zfs_vget); 2071 EXPORT_SYMBOL(zfs_prune); 2072 #endif 2073