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