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