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