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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/sysmacros.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.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>
41 #include <sys/fs/zfs.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_deleg.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/spa_boot.h>
60 #include <sys/objlist.h>
62 #include <linux/vfs_compat.h>
63 #include "zfs_comutil.h"
88 static const match_table_t zpl_tokens
= {
89 { TOKEN_RO
, MNTOPT_RO
},
90 { TOKEN_RW
, MNTOPT_RW
},
91 { TOKEN_SETUID
, MNTOPT_SETUID
},
92 { TOKEN_NOSETUID
, MNTOPT_NOSETUID
},
93 { TOKEN_EXEC
, MNTOPT_EXEC
},
94 { TOKEN_NOEXEC
, MNTOPT_NOEXEC
},
95 { TOKEN_DEVICES
, MNTOPT_DEVICES
},
96 { TOKEN_NODEVICES
, MNTOPT_NODEVICES
},
97 { TOKEN_DIRXATTR
, MNTOPT_DIRXATTR
},
98 { TOKEN_SAXATTR
, MNTOPT_SAXATTR
},
99 { TOKEN_XATTR
, MNTOPT_XATTR
},
100 { TOKEN_NOXATTR
, MNTOPT_NOXATTR
},
101 { TOKEN_ATIME
, MNTOPT_ATIME
},
102 { TOKEN_NOATIME
, MNTOPT_NOATIME
},
103 { TOKEN_RELATIME
, MNTOPT_RELATIME
},
104 { TOKEN_NORELATIME
, MNTOPT_NORELATIME
},
105 { TOKEN_NBMAND
, MNTOPT_NBMAND
},
106 { TOKEN_NONBMAND
, MNTOPT_NONBMAND
},
107 { TOKEN_MNTPOINT
, MNTOPT_MNTPOINT
"=%s" },
108 { TOKEN_LAST
, NULL
},
112 zfsvfs_vfs_free(vfs_t
*vfsp
)
115 if (vfsp
->vfs_mntpoint
!= NULL
)
116 kmem_strfree(vfsp
->vfs_mntpoint
);
118 kmem_free(vfsp
, sizeof (vfs_t
));
123 zfsvfs_parse_option(char *option
, int token
, substring_t
*args
, vfs_t
*vfsp
)
127 vfsp
->vfs_readonly
= B_TRUE
;
128 vfsp
->vfs_do_readonly
= B_TRUE
;
131 vfsp
->vfs_readonly
= B_FALSE
;
132 vfsp
->vfs_do_readonly
= B_TRUE
;
135 vfsp
->vfs_setuid
= B_TRUE
;
136 vfsp
->vfs_do_setuid
= B_TRUE
;
139 vfsp
->vfs_setuid
= B_FALSE
;
140 vfsp
->vfs_do_setuid
= B_TRUE
;
143 vfsp
->vfs_exec
= B_TRUE
;
144 vfsp
->vfs_do_exec
= B_TRUE
;
147 vfsp
->vfs_exec
= B_FALSE
;
148 vfsp
->vfs_do_exec
= B_TRUE
;
151 vfsp
->vfs_devices
= B_TRUE
;
152 vfsp
->vfs_do_devices
= B_TRUE
;
154 case TOKEN_NODEVICES
:
155 vfsp
->vfs_devices
= B_FALSE
;
156 vfsp
->vfs_do_devices
= B_TRUE
;
159 vfsp
->vfs_xattr
= ZFS_XATTR_DIR
;
160 vfsp
->vfs_do_xattr
= B_TRUE
;
163 vfsp
->vfs_xattr
= ZFS_XATTR_SA
;
164 vfsp
->vfs_do_xattr
= B_TRUE
;
167 vfsp
->vfs_xattr
= ZFS_XATTR_DIR
;
168 vfsp
->vfs_do_xattr
= B_TRUE
;
171 vfsp
->vfs_xattr
= ZFS_XATTR_OFF
;
172 vfsp
->vfs_do_xattr
= B_TRUE
;
175 vfsp
->vfs_atime
= B_TRUE
;
176 vfsp
->vfs_do_atime
= B_TRUE
;
179 vfsp
->vfs_atime
= B_FALSE
;
180 vfsp
->vfs_do_atime
= B_TRUE
;
183 vfsp
->vfs_relatime
= B_TRUE
;
184 vfsp
->vfs_do_relatime
= B_TRUE
;
186 case TOKEN_NORELATIME
:
187 vfsp
->vfs_relatime
= B_FALSE
;
188 vfsp
->vfs_do_relatime
= B_TRUE
;
191 vfsp
->vfs_nbmand
= B_TRUE
;
192 vfsp
->vfs_do_nbmand
= B_TRUE
;
195 vfsp
->vfs_nbmand
= B_FALSE
;
196 vfsp
->vfs_do_nbmand
= B_TRUE
;
199 vfsp
->vfs_mntpoint
= match_strdup(&args
[0]);
200 if (vfsp
->vfs_mntpoint
== NULL
)
201 return (SET_ERROR(ENOMEM
));
212 * Parse the raw mntopts and return a vfs_t describing the options.
215 zfsvfs_parse_options(char *mntopts
, vfs_t
**vfsp
)
220 tmp_vfsp
= kmem_zalloc(sizeof (vfs_t
), KM_SLEEP
);
222 if (mntopts
!= NULL
) {
223 substring_t args
[MAX_OPT_ARGS
];
224 char *tmp_mntopts
, *p
, *t
;
227 tmp_mntopts
= t
= kmem_strdup(mntopts
);
228 if (tmp_mntopts
== NULL
)
229 return (SET_ERROR(ENOMEM
));
231 while ((p
= strsep(&t
, ",")) != NULL
) {
235 args
[0].to
= args
[0].from
= NULL
;
236 token
= match_token(p
, zpl_tokens
, args
);
237 error
= zfsvfs_parse_option(p
, token
, args
, tmp_vfsp
);
239 kmem_strfree(tmp_mntopts
);
240 zfsvfs_vfs_free(tmp_vfsp
);
245 kmem_strfree(tmp_mntopts
);
254 zfs_is_readonly(zfsvfs_t
*zfsvfs
)
256 return (!!(zfsvfs
->z_sb
->s_flags
& SB_RDONLY
));
260 zfs_sync(struct super_block
*sb
, int wait
, cred_t
*cr
)
263 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
266 * Semantically, the only requirement is that the sync be initiated.
267 * The DMU syncs out txgs frequently, so there's nothing to do.
272 if (zfsvfs
!= NULL
) {
274 * Sync a specific filesystem.
279 dp
= dmu_objset_pool(zfsvfs
->z_os
);
282 * If the system is shutting down, then skip any
283 * filesystems which may exist on a suspended pool.
285 if (spa_suspended(dp
->dp_spa
)) {
290 if (zfsvfs
->z_log
!= NULL
)
291 zil_commit(zfsvfs
->z_log
, 0);
296 * Sync all ZFS filesystems. This is what happens when you
297 * run sync(1). Unlike other filesystems, ZFS honors the
298 * request by waiting for all pools to commit all dirty data.
307 atime_changed_cb(void *arg
, uint64_t newval
)
309 zfsvfs_t
*zfsvfs
= arg
;
310 struct super_block
*sb
= zfsvfs
->z_sb
;
315 * Update SB_NOATIME bit in VFS super block. Since atime update is
316 * determined by atime_needs_update(), atime_needs_update() needs to
317 * return false if atime is turned off, and not unconditionally return
318 * false if atime is turned on.
321 sb
->s_flags
&= ~SB_NOATIME
;
323 sb
->s_flags
|= SB_NOATIME
;
327 relatime_changed_cb(void *arg
, uint64_t newval
)
329 ((zfsvfs_t
*)arg
)->z_relatime
= newval
;
333 xattr_changed_cb(void *arg
, uint64_t newval
)
335 zfsvfs_t
*zfsvfs
= arg
;
337 if (newval
== ZFS_XATTR_OFF
) {
338 zfsvfs
->z_flags
&= ~ZSB_XATTR
;
340 zfsvfs
->z_flags
|= ZSB_XATTR
;
342 if (newval
== ZFS_XATTR_SA
)
343 zfsvfs
->z_xattr_sa
= B_TRUE
;
345 zfsvfs
->z_xattr_sa
= B_FALSE
;
350 acltype_changed_cb(void *arg
, uint64_t newval
)
352 zfsvfs_t
*zfsvfs
= arg
;
355 case ZFS_ACLTYPE_NFSV4
:
356 case ZFS_ACLTYPE_OFF
:
357 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_OFF
;
358 zfsvfs
->z_sb
->s_flags
&= ~SB_POSIXACL
;
360 case ZFS_ACLTYPE_POSIX
:
361 #ifdef CONFIG_FS_POSIX_ACL
362 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_POSIX
;
363 zfsvfs
->z_sb
->s_flags
|= SB_POSIXACL
;
365 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_OFF
;
366 zfsvfs
->z_sb
->s_flags
&= ~SB_POSIXACL
;
367 #endif /* CONFIG_FS_POSIX_ACL */
375 blksz_changed_cb(void *arg
, uint64_t newval
)
377 zfsvfs_t
*zfsvfs
= arg
;
378 ASSERT3U(newval
, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs
->z_os
)));
379 ASSERT3U(newval
, >=, SPA_MINBLOCKSIZE
);
380 ASSERT(ISP2(newval
));
382 zfsvfs
->z_max_blksz
= newval
;
386 readonly_changed_cb(void *arg
, uint64_t newval
)
388 zfsvfs_t
*zfsvfs
= arg
;
389 struct super_block
*sb
= zfsvfs
->z_sb
;
395 sb
->s_flags
|= SB_RDONLY
;
397 sb
->s_flags
&= ~SB_RDONLY
;
401 devices_changed_cb(void *arg
, uint64_t newval
)
406 setuid_changed_cb(void *arg
, uint64_t newval
)
411 exec_changed_cb(void *arg
, uint64_t newval
)
416 nbmand_changed_cb(void *arg
, uint64_t newval
)
418 zfsvfs_t
*zfsvfs
= arg
;
419 struct super_block
*sb
= zfsvfs
->z_sb
;
425 sb
->s_flags
|= SB_MANDLOCK
;
427 sb
->s_flags
&= ~SB_MANDLOCK
;
431 snapdir_changed_cb(void *arg
, uint64_t newval
)
433 ((zfsvfs_t
*)arg
)->z_show_ctldir
= newval
;
437 acl_mode_changed_cb(void *arg
, uint64_t newval
)
439 zfsvfs_t
*zfsvfs
= arg
;
441 zfsvfs
->z_acl_mode
= newval
;
445 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
447 ((zfsvfs_t
*)arg
)->z_acl_inherit
= newval
;
451 zfs_register_callbacks(vfs_t
*vfsp
)
453 struct dsl_dataset
*ds
= NULL
;
455 zfsvfs_t
*zfsvfs
= NULL
;
459 zfsvfs
= vfsp
->vfs_data
;
464 * The act of registering our callbacks will destroy any mount
465 * options we may have. In order to enable temporary overrides
466 * of mount options, we stash away the current values and
467 * restore them after we register the callbacks.
469 if (zfs_is_readonly(zfsvfs
) || !spa_writeable(dmu_objset_spa(os
))) {
470 vfsp
->vfs_do_readonly
= B_TRUE
;
471 vfsp
->vfs_readonly
= B_TRUE
;
475 * Register property callbacks.
477 * It would probably be fine to just check for i/o error from
478 * the first prop_register(), but I guess I like to go
481 ds
= dmu_objset_ds(os
);
482 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
483 error
= dsl_prop_register(ds
,
484 zfs_prop_to_name(ZFS_PROP_ATIME
), atime_changed_cb
, zfsvfs
);
485 error
= error
? error
: dsl_prop_register(ds
,
486 zfs_prop_to_name(ZFS_PROP_RELATIME
), relatime_changed_cb
, zfsvfs
);
487 error
= error
? error
: dsl_prop_register(ds
,
488 zfs_prop_to_name(ZFS_PROP_XATTR
), xattr_changed_cb
, zfsvfs
);
489 error
= error
? error
: dsl_prop_register(ds
,
490 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), blksz_changed_cb
, zfsvfs
);
491 error
= error
? error
: dsl_prop_register(ds
,
492 zfs_prop_to_name(ZFS_PROP_READONLY
), readonly_changed_cb
, zfsvfs
);
493 error
= error
? error
: dsl_prop_register(ds
,
494 zfs_prop_to_name(ZFS_PROP_DEVICES
), devices_changed_cb
, zfsvfs
);
495 error
= error
? error
: dsl_prop_register(ds
,
496 zfs_prop_to_name(ZFS_PROP_SETUID
), setuid_changed_cb
, zfsvfs
);
497 error
= error
? error
: dsl_prop_register(ds
,
498 zfs_prop_to_name(ZFS_PROP_EXEC
), exec_changed_cb
, zfsvfs
);
499 error
= error
? error
: dsl_prop_register(ds
,
500 zfs_prop_to_name(ZFS_PROP_SNAPDIR
), snapdir_changed_cb
, zfsvfs
);
501 error
= error
? error
: dsl_prop_register(ds
,
502 zfs_prop_to_name(ZFS_PROP_ACLTYPE
), acltype_changed_cb
, zfsvfs
);
503 error
= error
? error
: dsl_prop_register(ds
,
504 zfs_prop_to_name(ZFS_PROP_ACLMODE
), acl_mode_changed_cb
, zfsvfs
);
505 error
= error
? error
: dsl_prop_register(ds
,
506 zfs_prop_to_name(ZFS_PROP_ACLINHERIT
), acl_inherit_changed_cb
,
508 error
= error
? error
: dsl_prop_register(ds
,
509 zfs_prop_to_name(ZFS_PROP_NBMAND
), nbmand_changed_cb
, zfsvfs
);
510 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
515 * Invoke our callbacks to restore temporary mount options.
517 if (vfsp
->vfs_do_readonly
)
518 readonly_changed_cb(zfsvfs
, vfsp
->vfs_readonly
);
519 if (vfsp
->vfs_do_setuid
)
520 setuid_changed_cb(zfsvfs
, vfsp
->vfs_setuid
);
521 if (vfsp
->vfs_do_exec
)
522 exec_changed_cb(zfsvfs
, vfsp
->vfs_exec
);
523 if (vfsp
->vfs_do_devices
)
524 devices_changed_cb(zfsvfs
, vfsp
->vfs_devices
);
525 if (vfsp
->vfs_do_xattr
)
526 xattr_changed_cb(zfsvfs
, vfsp
->vfs_xattr
);
527 if (vfsp
->vfs_do_atime
)
528 atime_changed_cb(zfsvfs
, vfsp
->vfs_atime
);
529 if (vfsp
->vfs_do_relatime
)
530 relatime_changed_cb(zfsvfs
, vfsp
->vfs_relatime
);
531 if (vfsp
->vfs_do_nbmand
)
532 nbmand_changed_cb(zfsvfs
, vfsp
->vfs_nbmand
);
537 dsl_prop_unregister_all(ds
, zfsvfs
);
542 * Takes a dataset, a property, a value and that value's setpoint as
543 * found in the ZAP. Checks if the property has been changed in the vfs.
544 * If so, val and setpoint will be overwritten with updated content.
545 * Otherwise, they are left unchanged.
548 zfs_get_temporary_prop(dsl_dataset_t
*ds
, zfs_prop_t zfs_prop
, uint64_t *val
,
557 error
= dmu_objset_from_ds(ds
, &os
);
561 if (dmu_objset_type(os
) != DMU_OST_ZFS
)
564 mutex_enter(&os
->os_user_ptr_lock
);
565 zfvp
= dmu_objset_get_user(os
);
566 mutex_exit(&os
->os_user_ptr_lock
);
574 if (vfsp
->vfs_do_atime
)
575 tmp
= vfsp
->vfs_atime
;
577 case ZFS_PROP_RELATIME
:
578 if (vfsp
->vfs_do_relatime
)
579 tmp
= vfsp
->vfs_relatime
;
581 case ZFS_PROP_DEVICES
:
582 if (vfsp
->vfs_do_devices
)
583 tmp
= vfsp
->vfs_devices
;
586 if (vfsp
->vfs_do_exec
)
587 tmp
= vfsp
->vfs_exec
;
589 case ZFS_PROP_SETUID
:
590 if (vfsp
->vfs_do_setuid
)
591 tmp
= vfsp
->vfs_setuid
;
593 case ZFS_PROP_READONLY
:
594 if (vfsp
->vfs_do_readonly
)
595 tmp
= vfsp
->vfs_readonly
;
598 if (vfsp
->vfs_do_xattr
)
599 tmp
= vfsp
->vfs_xattr
;
601 case ZFS_PROP_NBMAND
:
602 if (vfsp
->vfs_do_nbmand
)
603 tmp
= vfsp
->vfs_nbmand
;
610 (void) strcpy(setpoint
, "temporary");
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
622 zfsvfs_init(zfsvfs_t
*zfsvfs
, objset_t
*os
)
627 zfsvfs
->z_max_blksz
= SPA_OLD_MAXBLOCKSIZE
;
628 zfsvfs
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
631 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zfsvfs
->z_version
);
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
));
642 error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &val
);
645 zfsvfs
->z_norm
= (int)val
;
647 error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &val
);
650 zfsvfs
->z_utf8
= (val
!= 0);
652 error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &val
);
655 zfsvfs
->z_case
= (uint_t
)val
;
657 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_ACLTYPE
, &val
)) != 0)
659 zfsvfs
->z_acl_type
= (uint_t
)val
;
662 * Fold case on file systems that are always or sometimes case
665 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
666 zfsvfs
->z_case
== ZFS_CASE_MIXED
)
667 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
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
);
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,
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
;
685 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
689 ASSERT(zfsvfs
->z_root
!= 0);
691 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
692 &zfsvfs
->z_unlinkedobj
);
696 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
697 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
698 8, 1, &zfsvfs
->z_userquota_obj
);
700 zfsvfs
->z_userquota_obj
= 0;
704 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
705 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
706 8, 1, &zfsvfs
->z_groupquota_obj
);
708 zfsvfs
->z_groupquota_obj
= 0;
712 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
713 zfs_userquota_prop_prefixes
[ZFS_PROP_PROJECTQUOTA
],
714 8, 1, &zfsvfs
->z_projectquota_obj
);
716 zfsvfs
->z_projectquota_obj
= 0;
720 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
721 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
],
722 8, 1, &zfsvfs
->z_userobjquota_obj
);
724 zfsvfs
->z_userobjquota_obj
= 0;
728 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
729 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
],
730 8, 1, &zfsvfs
->z_groupobjquota_obj
);
732 zfsvfs
->z_groupobjquota_obj
= 0;
736 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
737 zfs_userquota_prop_prefixes
[ZFS_PROP_PROJECTOBJQUOTA
],
738 8, 1, &zfsvfs
->z_projectobjquota_obj
);
740 zfsvfs
->z_projectobjquota_obj
= 0;
744 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
745 &zfsvfs
->z_fuid_obj
);
747 zfsvfs
->z_fuid_obj
= 0;
751 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
752 &zfsvfs
->z_shares_dir
);
754 zfsvfs
->z_shares_dir
= 0;
758 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
759 &zfsvfs
->z_attr_table
);
763 if (zfsvfs
->z_version
>= ZPL_VERSION_SA
)
764 sa_register_update_callback(os
, zfs_sa_upgrade
);
770 zfsvfs_create(const char *osname
, boolean_t readonly
, zfsvfs_t
**zfvp
)
775 boolean_t ro
= (readonly
|| (strchr(osname
, '@') != NULL
));
777 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
779 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, ro
, B_TRUE
, zfsvfs
, &os
);
781 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
785 error
= zfsvfs_create_impl(zfvp
, zfsvfs
, os
);
787 dmu_objset_disown(os
, B_TRUE
, zfsvfs
);
794 * Note: zfsvfs is assumed to be malloc'd, and will be freed by this function
795 * on a failure. Do not pass in a statically allocated zfsvfs.
798 zfsvfs_create_impl(zfsvfs_t
**zfvp
, zfsvfs_t
*zfsvfs
, objset_t
*os
)
802 zfsvfs
->z_vfs
= NULL
;
804 zfsvfs
->z_parent
= zfsvfs
;
806 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
807 mutex_init(&zfsvfs
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
808 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
809 offsetof(znode_t
, z_link_node
));
810 ZFS_TEARDOWN_INIT(zfsvfs
);
811 rw_init(&zfsvfs
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
812 rw_init(&zfsvfs
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
814 int size
= MIN(1 << (highbit64(zfs_object_mutex_size
) - 1),
816 zfsvfs
->z_hold_size
= size
;
817 zfsvfs
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
,
819 zfsvfs
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
820 for (int i
= 0; i
!= size
; i
++) {
821 avl_create(&zfsvfs
->z_hold_trees
[i
], zfs_znode_hold_compare
,
822 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
823 mutex_init(&zfsvfs
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
826 error
= zfsvfs_init(zfsvfs
, os
);
833 zfsvfs
->z_drain_task
= TASKQID_INVALID
;
834 zfsvfs
->z_draining
= B_FALSE
;
835 zfsvfs
->z_drain_cancel
= B_TRUE
;
842 zfsvfs_setup(zfsvfs_t
*zfsvfs
, boolean_t mounting
)
845 boolean_t readonly
= zfs_is_readonly(zfsvfs
);
847 error
= zfs_register_callbacks(zfsvfs
->z_vfs
);
851 zfsvfs
->z_log
= zil_open(zfsvfs
->z_os
, zfs_get_data
);
854 * If we are not mounting (ie: online recv), then we don't
855 * have to worry about replaying the log as we blocked all
856 * operations out since we closed the ZIL.
859 ASSERT3P(zfsvfs
->z_kstat
.dk_kstats
, ==, NULL
);
860 dataset_kstats_create(&zfsvfs
->z_kstat
, zfsvfs
->z_os
);
863 * During replay we remove the read only flag to
864 * allow replays to succeed.
867 readonly_changed_cb(zfsvfs
, B_FALSE
);
870 if (zap_get_stats(zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
,
872 dataset_kstats_update_nunlinks_kstat(
873 &zfsvfs
->z_kstat
, zs
.zs_num_entries
);
874 dprintf_ds(zfsvfs
->z_os
->os_dsl_dataset
,
875 "num_entries in unlinked set: %llu",
878 zfs_unlinked_drain(zfsvfs
);
879 dsl_dir_t
*dd
= zfsvfs
->z_os
->os_dsl_dataset
->ds_dir
;
880 dd
->dd_activity_cancelled
= B_FALSE
;
884 * Parse and replay the intent log.
886 * Because of ziltest, this must be done after
887 * zfs_unlinked_drain(). (Further note: ziltest
888 * doesn't use readonly mounts, where
889 * zfs_unlinked_drain() isn't called.) This is because
890 * ziltest causes spa_sync() to think it's committed,
891 * but actually it is not, so the intent log contains
892 * many txg's worth of changes.
894 * In particular, if object N is in the unlinked set in
895 * the last txg to actually sync, then it could be
896 * actually freed in a later txg and then reallocated
897 * in a yet later txg. This would write a "create
898 * object N" record to the intent log. Normally, this
899 * would be fine because the spa_sync() would have
900 * written out the fact that object N is free, before
901 * we could write the "create object N" intent log
904 * But when we are in ziltest mode, we advance the "open
905 * txg" without actually spa_sync()-ing the changes to
906 * disk. So we would see that object N is still
907 * allocated and in the unlinked set, and there is an
908 * intent log record saying to allocate it.
910 if (spa_writeable(dmu_objset_spa(zfsvfs
->z_os
))) {
911 if (zil_replay_disable
) {
912 zil_destroy(zfsvfs
->z_log
, B_FALSE
);
914 zfsvfs
->z_replay
= B_TRUE
;
915 zil_replay(zfsvfs
->z_os
, zfsvfs
,
917 zfsvfs
->z_replay
= B_FALSE
;
921 /* restore readonly bit */
923 readonly_changed_cb(zfsvfs
, B_TRUE
);
927 * Set the objset user_ptr to track its zfsvfs.
929 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
930 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
931 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
937 zfsvfs_free(zfsvfs_t
*zfsvfs
)
939 int i
, size
= zfsvfs
->z_hold_size
;
941 zfs_fuid_destroy(zfsvfs
);
943 mutex_destroy(&zfsvfs
->z_znodes_lock
);
944 mutex_destroy(&zfsvfs
->z_lock
);
945 list_destroy(&zfsvfs
->z_all_znodes
);
946 ZFS_TEARDOWN_DESTROY(zfsvfs
);
947 rw_destroy(&zfsvfs
->z_teardown_inactive_lock
);
948 rw_destroy(&zfsvfs
->z_fuid_lock
);
949 for (i
= 0; i
!= size
; i
++) {
950 avl_destroy(&zfsvfs
->z_hold_trees
[i
]);
951 mutex_destroy(&zfsvfs
->z_hold_locks
[i
]);
953 vmem_free(zfsvfs
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
954 vmem_free(zfsvfs
->z_hold_locks
, sizeof (kmutex_t
) * size
);
955 zfsvfs_vfs_free(zfsvfs
->z_vfs
);
956 dataset_kstats_destroy(&zfsvfs
->z_kstat
);
957 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
961 zfs_set_fuid_feature(zfsvfs_t
*zfsvfs
)
963 zfsvfs
->z_use_fuids
= USE_FUIDS(zfsvfs
->z_version
, zfsvfs
->z_os
);
964 zfsvfs
->z_use_sa
= USE_SA(zfsvfs
->z_version
, zfsvfs
->z_os
);
968 zfs_unregister_callbacks(zfsvfs_t
*zfsvfs
)
970 objset_t
*os
= zfsvfs
->z_os
;
972 if (!dmu_objset_is_snapshot(os
))
973 dsl_prop_unregister_all(dmu_objset_ds(os
), zfsvfs
);
978 * Check that the hex label string is appropriate for the dataset being
979 * mounted into the global_zone proper.
981 * Return an error if the hex label string is not default or
982 * admin_low/admin_high. For admin_low labels, the corresponding
983 * dataset must be readonly.
986 zfs_check_global_label(const char *dsname
, const char *hexsl
)
988 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
990 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
992 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
993 /* must be readonly */
996 if (dsl_prop_get_integer(dsname
,
997 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
998 return (SET_ERROR(EACCES
));
999 return (rdonly
? 0 : SET_ERROR(EACCES
));
1001 return (SET_ERROR(EACCES
));
1003 #endif /* HAVE_MLSLABEL */
1006 zfs_statfs_project(zfsvfs_t
*zfsvfs
, znode_t
*zp
, struct kstatfs
*statp
,
1009 char buf
[20 + DMU_OBJACCT_PREFIX_LEN
];
1010 uint64_t offset
= DMU_OBJACCT_PREFIX_LEN
;
1015 strlcpy(buf
, DMU_OBJACCT_PREFIX
, DMU_OBJACCT_PREFIX_LEN
+ 1);
1016 err
= zfs_id_to_fuidstr(zfsvfs
, NULL
, zp
->z_projid
, buf
+ offset
,
1017 sizeof (buf
) - offset
, B_FALSE
);
1021 if (zfsvfs
->z_projectquota_obj
== 0)
1024 err
= zap_lookup(zfsvfs
->z_os
, zfsvfs
->z_projectquota_obj
,
1025 buf
+ offset
, 8, 1, "a
);
1031 err
= zap_lookup(zfsvfs
->z_os
, DMU_PROJECTUSED_OBJECT
,
1032 buf
+ offset
, 8, 1, &used
);
1033 if (unlikely(err
== ENOENT
)) {
1035 u_longlong_t nblocks
;
1038 * Quota accounting is async, so it is possible race case.
1039 * There is at least one object with the given project ID.
1041 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
1042 if (unlikely(zp
->z_blksz
== 0))
1043 blksize
= zfsvfs
->z_max_blksz
;
1045 used
= blksize
* nblocks
;
1050 statp
->f_blocks
= quota
>> bshift
;
1051 statp
->f_bfree
= (quota
> used
) ? ((quota
- used
) >> bshift
) : 0;
1052 statp
->f_bavail
= statp
->f_bfree
;
1055 if (zfsvfs
->z_projectobjquota_obj
== 0)
1058 err
= zap_lookup(zfsvfs
->z_os
, zfsvfs
->z_projectobjquota_obj
,
1059 buf
+ offset
, 8, 1, "a
);
1065 err
= zap_lookup(zfsvfs
->z_os
, DMU_PROJECTUSED_OBJECT
,
1067 if (unlikely(err
== ENOENT
)) {
1069 * Quota accounting is async, so it is possible race case.
1070 * There is at least one object with the given project ID.
1077 statp
->f_files
= quota
;
1078 statp
->f_ffree
= (quota
> used
) ? (quota
- used
) : 0;
1084 zfs_statvfs(struct inode
*ip
, struct kstatfs
*statp
)
1086 zfsvfs_t
*zfsvfs
= ITOZSB(ip
);
1087 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
1092 dmu_objset_space(zfsvfs
->z_os
,
1093 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1095 uint64_t fsid
= dmu_objset_fsid_guid(zfsvfs
->z_os
);
1097 * The underlying storage pool actually uses multiple block
1098 * size. Under Solaris frsize (fragment size) is reported as
1099 * the smallest block size we support, and bsize (block size)
1100 * as the filesystem's maximum block size. Unfortunately,
1101 * under Linux the fragment size and block size are often used
1102 * interchangeably. Thus we are forced to report both of them
1103 * as the filesystem's maximum block size.
1105 statp
->f_frsize
= zfsvfs
->z_max_blksz
;
1106 statp
->f_bsize
= zfsvfs
->z_max_blksz
;
1107 uint32_t bshift
= fls(statp
->f_bsize
) - 1;
1110 * The following report "total" blocks of various kinds in
1111 * the file system, but reported in terms of f_bsize - the
1115 /* Round up so we never have a filesystem using 0 blocks. */
1116 refdbytes
= P2ROUNDUP(refdbytes
, statp
->f_bsize
);
1117 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
1118 statp
->f_bfree
= availbytes
>> bshift
;
1119 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1122 * statvfs() should really be called statufs(), because it assumes
1123 * static metadata. ZFS doesn't preallocate files, so the best
1124 * we can do is report the max that could possibly fit in f_files,
1125 * and that minus the number actually used in f_ffree.
1126 * For f_ffree, report the smaller of the number of objects available
1127 * and the number of blocks (each object will take at least a block).
1129 statp
->f_ffree
= MIN(availobjs
, availbytes
>> DNODE_SHIFT
);
1130 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1131 statp
->f_fsid
.val
[0] = (uint32_t)fsid
;
1132 statp
->f_fsid
.val
[1] = (uint32_t)(fsid
>> 32);
1133 statp
->f_type
= ZFS_SUPER_MAGIC
;
1134 statp
->f_namelen
= MAXNAMELEN
- 1;
1137 * We have all of 40 characters to stuff a string here.
1138 * Is there anything useful we could/should provide?
1140 memset(statp
->f_spare
, 0, sizeof (statp
->f_spare
));
1142 if (dmu_objset_projectquota_enabled(zfsvfs
->z_os
) &&
1143 dmu_objset_projectquota_present(zfsvfs
->z_os
)) {
1144 znode_t
*zp
= ITOZ(ip
);
1146 if (zp
->z_pflags
& ZFS_PROJINHERIT
&& zp
->z_projid
&&
1147 zpl_is_valid_projid(zp
->z_projid
))
1148 err
= zfs_statfs_project(zfsvfs
, zp
, statp
, bshift
);
1156 zfs_root(zfsvfs_t
*zfsvfs
, struct inode
**ipp
)
1163 error
= zfs_zget(zfsvfs
, zfsvfs
->z_root
, &rootzp
);
1165 *ipp
= ZTOI(rootzp
);
1172 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1173 * To accommodate this we must improvise and manually walk the list of znodes
1174 * attempting to prune dentries in order to be able to drop the inodes.
1176 * To avoid scanning the same znodes multiple times they are always rotated
1177 * to the end of the z_all_znodes list. New znodes are inserted at the
1178 * end of the list so we're always scanning the oldest znodes first.
1181 zfs_prune_aliases(zfsvfs_t
*zfsvfs
, unsigned long nr_to_scan
)
1183 znode_t
**zp_array
, *zp
;
1184 int max_array
= MIN(nr_to_scan
, PAGE_SIZE
* 8 / sizeof (znode_t
*));
1188 zp_array
= kmem_zalloc(max_array
* sizeof (znode_t
*), KM_SLEEP
);
1190 mutex_enter(&zfsvfs
->z_znodes_lock
);
1191 while ((zp
= list_head(&zfsvfs
->z_all_znodes
)) != NULL
) {
1193 if ((i
++ > nr_to_scan
) || (j
>= max_array
))
1196 ASSERT(list_link_active(&zp
->z_link_node
));
1197 list_remove(&zfsvfs
->z_all_znodes
, zp
);
1198 list_insert_tail(&zfsvfs
->z_all_znodes
, zp
);
1200 /* Skip active znodes and .zfs entries */
1201 if (MUTEX_HELD(&zp
->z_lock
) || zp
->z_is_ctldir
)
1204 if (igrab(ZTOI(zp
)) == NULL
)
1210 mutex_exit(&zfsvfs
->z_znodes_lock
);
1212 for (i
= 0; i
< j
; i
++) {
1215 ASSERT3P(zp
, !=, NULL
);
1216 d_prune_aliases(ZTOI(zp
));
1218 if (atomic_read(&ZTOI(zp
)->i_count
) == 1)
1224 kmem_free(zp_array
, max_array
* sizeof (znode_t
*));
1230 * The ARC has requested that the filesystem drop entries from the dentry
1231 * and inode caches. This can occur when the ARC needs to free meta data
1232 * blocks but can't because they are all pinned by entries in these caches.
1235 zfs_prune(struct super_block
*sb
, unsigned long nr_to_scan
, int *objects
)
1237 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1239 struct shrinker
*shrinker
= &sb
->s_shrink
;
1240 struct shrink_control sc
= {
1241 .nr_to_scan
= nr_to_scan
,
1242 .gfp_mask
= GFP_KERNEL
,
1247 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1248 defined(SHRINK_CONTROL_HAS_NID) && \
1249 defined(SHRINKER_NUMA_AWARE)
1250 if (sb
->s_shrink
.flags
& SHRINKER_NUMA_AWARE
) {
1252 for_each_online_node(sc
.nid
) {
1253 *objects
+= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1255 * reset sc.nr_to_scan, modified by
1256 * scan_objects == super_cache_scan
1258 sc
.nr_to_scan
= nr_to_scan
;
1261 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1264 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1265 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1266 #elif defined(HAVE_SINGLE_SHRINKER_CALLBACK)
1267 *objects
= (*shrinker
->shrink
)(shrinker
, &sc
);
1268 #elif defined(HAVE_D_PRUNE_ALIASES)
1269 #define D_PRUNE_ALIASES_IS_DEFAULT
1270 *objects
= zfs_prune_aliases(zfsvfs
, nr_to_scan
);
1272 #error "No available dentry and inode cache pruning mechanism."
1275 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1276 #undef D_PRUNE_ALIASES_IS_DEFAULT
1278 * Fall back to zfs_prune_aliases if the kernel's per-superblock
1279 * shrinker couldn't free anything, possibly due to the inodes being
1280 * allocated in a different memcg.
1283 *objects
= zfs_prune_aliases(zfsvfs
, nr_to_scan
);
1288 dprintf_ds(zfsvfs
->z_os
->os_dsl_dataset
,
1289 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1290 nr_to_scan
, *objects
, error
);
1296 * Teardown the zfsvfs_t.
1298 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1299 * and 'z_teardown_inactive_lock' held.
1302 zfsvfs_teardown(zfsvfs_t
*zfsvfs
, boolean_t unmounting
)
1306 zfs_unlinked_drain_stop_wait(zfsvfs
);
1309 * If someone has not already unmounted this file system,
1310 * drain the zrele_taskq to ensure all active references to the
1311 * zfsvfs_t have been handled only then can it be safely destroyed.
1315 * If we're unmounting we have to wait for the list to
1318 * If we're not unmounting there's no guarantee the list
1319 * will drain completely, but iputs run from the taskq
1320 * may add the parents of dir-based xattrs to the taskq
1321 * so we want to wait for these.
1323 * We can safely read z_nr_znodes without locking because the
1324 * VFS has already blocked operations which add to the
1325 * z_all_znodes list and thus increment z_nr_znodes.
1328 while (zfsvfs
->z_nr_znodes
> 0) {
1329 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1330 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1331 if (++round
> 1 && !unmounting
)
1336 ZFS_TEARDOWN_ENTER_WRITE(zfsvfs
, FTAG
);
1340 * We purge the parent filesystem's super block as the
1341 * parent filesystem and all of its snapshots have their
1342 * inode's super block set to the parent's filesystem's
1343 * super block. Note, 'z_parent' is self referential
1344 * for non-snapshots.
1346 shrink_dcache_sb(zfsvfs
->z_parent
->z_sb
);
1350 * Close the zil. NB: Can't close the zil while zfs_inactive
1351 * threads are blocked as zil_close can call zfs_inactive.
1353 if (zfsvfs
->z_log
) {
1354 zil_close(zfsvfs
->z_log
);
1355 zfsvfs
->z_log
= NULL
;
1358 rw_enter(&zfsvfs
->z_teardown_inactive_lock
, RW_WRITER
);
1361 * If we are not unmounting (ie: online recv) and someone already
1362 * unmounted this file system while we were doing the switcheroo,
1363 * or a reopen of z_os failed then just bail out now.
1365 if (!unmounting
&& (zfsvfs
->z_unmounted
|| zfsvfs
->z_os
== NULL
)) {
1366 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1367 ZFS_TEARDOWN_EXIT(zfsvfs
, FTAG
);
1368 return (SET_ERROR(EIO
));
1372 * At this point there are no VFS ops active, and any new VFS ops
1373 * will fail with EIO since we have z_teardown_lock for writer (only
1374 * relevant for forced unmount).
1376 * Release all holds on dbufs. We also grab an extra reference to all
1377 * the remaining inodes so that the kernel does not attempt to free
1378 * any inodes of a suspended fs. This can cause deadlocks since the
1379 * zfs_resume_fs() process may involve starting threads, which might
1380 * attempt to free unreferenced inodes to free up memory for the new
1384 mutex_enter(&zfsvfs
->z_znodes_lock
);
1385 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
!= NULL
;
1386 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
)) {
1388 zfs_znode_dmu_fini(zp
);
1389 if (igrab(ZTOI(zp
)) != NULL
)
1390 zp
->z_suspended
= B_TRUE
;
1393 mutex_exit(&zfsvfs
->z_znodes_lock
);
1397 * If we are unmounting, set the unmounted flag and let new VFS ops
1398 * unblock. zfs_inactive will have the unmounted behavior, and all
1399 * other VFS ops will fail with EIO.
1402 zfsvfs
->z_unmounted
= B_TRUE
;
1403 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1404 ZFS_TEARDOWN_EXIT(zfsvfs
, FTAG
);
1408 * z_os will be NULL if there was an error in attempting to reopen
1409 * zfsvfs, so just return as the properties had already been
1411 * unregistered and cached data had been evicted before.
1413 if (zfsvfs
->z_os
== NULL
)
1417 * Unregister properties.
1419 zfs_unregister_callbacks(zfsvfs
);
1422 * Evict cached data. We must write out any dirty data before
1423 * disowning the dataset.
1425 objset_t
*os
= zfsvfs
->z_os
;
1426 boolean_t os_dirty
= B_FALSE
;
1427 for (int t
= 0; t
< TXG_SIZE
; t
++) {
1428 if (dmu_objset_is_dirty(os
, t
)) {
1433 if (!zfs_is_readonly(zfsvfs
) && os_dirty
) {
1434 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), 0);
1436 dmu_objset_evict_dbufs(zfsvfs
->z_os
);
1437 dsl_dir_t
*dd
= os
->os_dsl_dataset
->ds_dir
;
1438 dsl_dir_cancel_waiters(dd
);
1443 #if defined(HAVE_SUPER_SETUP_BDI_NAME)
1444 atomic_long_t zfs_bdi_seq
= ATOMIC_LONG_INIT(0);
1448 zfs_domount(struct super_block
*sb
, zfs_mnt_t
*zm
, int silent
)
1450 const char *osname
= zm
->mnt_osname
;
1451 struct inode
*root_inode
= NULL
;
1452 uint64_t recordsize
;
1454 zfsvfs_t
*zfsvfs
= NULL
;
1460 error
= zfsvfs_parse_options(zm
->mnt_data
, &vfs
);
1464 error
= zfsvfs_create(osname
, vfs
->vfs_readonly
, &zfsvfs
);
1466 zfsvfs_vfs_free(vfs
);
1470 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1471 &recordsize
, NULL
))) {
1472 zfsvfs_vfs_free(vfs
);
1476 vfs
->vfs_data
= zfsvfs
;
1477 zfsvfs
->z_vfs
= vfs
;
1479 sb
->s_fs_info
= zfsvfs
;
1480 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1481 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1482 sb
->s_time_gran
= 1;
1483 sb
->s_blocksize
= recordsize
;
1484 sb
->s_blocksize_bits
= ilog2(recordsize
);
1486 error
= -zpl_bdi_setup(sb
, "zfs");
1490 sb
->s_bdi
->ra_pages
= 0;
1492 /* Set callback operations for the file system. */
1493 sb
->s_op
= &zpl_super_operations
;
1494 sb
->s_xattr
= zpl_xattr_handlers
;
1495 sb
->s_export_op
= &zpl_export_operations
;
1496 sb
->s_d_op
= &zpl_dentry_operations
;
1498 /* Set features for file system. */
1499 zfs_set_fuid_feature(zfsvfs
);
1501 if (dmu_objset_is_snapshot(zfsvfs
->z_os
)) {
1504 atime_changed_cb(zfsvfs
, B_FALSE
);
1505 readonly_changed_cb(zfsvfs
, B_TRUE
);
1506 if ((error
= dsl_prop_get_integer(osname
,
1507 "xattr", &pval
, NULL
)))
1509 xattr_changed_cb(zfsvfs
, pval
);
1510 if ((error
= dsl_prop_get_integer(osname
,
1511 "acltype", &pval
, NULL
)))
1513 acltype_changed_cb(zfsvfs
, pval
);
1514 zfsvfs
->z_issnap
= B_TRUE
;
1515 zfsvfs
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1516 zfsvfs
->z_snap_defer_time
= jiffies
;
1518 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
1519 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
1520 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
1522 if ((error
= zfsvfs_setup(zfsvfs
, B_TRUE
)))
1526 /* Allocate a root inode for the filesystem. */
1527 error
= zfs_root(zfsvfs
, &root_inode
);
1529 (void) zfs_umount(sb
);
1533 /* Allocate a root dentry for the filesystem */
1534 sb
->s_root
= d_make_root(root_inode
);
1535 if (sb
->s_root
== NULL
) {
1536 (void) zfs_umount(sb
);
1537 error
= SET_ERROR(ENOMEM
);
1541 if (!zfsvfs
->z_issnap
)
1542 zfsctl_create(zfsvfs
);
1544 zfsvfs
->z_arc_prune
= arc_add_prune_callback(zpl_prune_sb
, sb
);
1547 if (zfsvfs
!= NULL
) {
1548 dmu_objset_disown(zfsvfs
->z_os
, B_TRUE
, zfsvfs
);
1549 zfsvfs_free(zfsvfs
);
1552 * make sure we don't have dangling sb->s_fs_info which
1553 * zfs_preumount will use.
1555 sb
->s_fs_info
= NULL
;
1562 * Called when an unmount is requested and certain sanity checks have
1563 * already passed. At this point no dentries or inodes have been reclaimed
1564 * from their respective caches. We drop the extra reference on the .zfs
1565 * control directory to allow everything to be reclaimed. All snapshots
1566 * must already have been unmounted to reach this point.
1569 zfs_preumount(struct super_block
*sb
)
1571 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1573 /* zfsvfs is NULL when zfs_domount fails during mount */
1575 zfs_unlinked_drain_stop_wait(zfsvfs
);
1576 zfsctl_destroy(sb
->s_fs_info
);
1578 * Wait for zrele_async before entering evict_inodes in
1579 * generic_shutdown_super. The reason we must finish before
1580 * evict_inodes is when lazytime is on, or when zfs_purgedir
1581 * calls zfs_zget, zrele would bump i_count from 0 to 1. This
1582 * would race with the i_count check in evict_inodes. This means
1583 * it could destroy the inode while we are still using it.
1585 * We wait for two passes. xattr directories in the first pass
1586 * may add xattr entries in zfs_purgedir, so in the second pass
1587 * we wait for them. We don't use taskq_wait here because it is
1588 * a pool wide taskq. Other mounted filesystems can constantly
1589 * do zrele_async and there's no guarantee when taskq will be
1592 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1593 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1594 taskq_wait_outstanding(dsl_pool_zrele_taskq(
1595 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1600 * Called once all other unmount released tear down has occurred.
1601 * It is our responsibility to release any remaining infrastructure.
1604 zfs_umount(struct super_block
*sb
)
1606 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1609 if (zfsvfs
->z_arc_prune
!= NULL
)
1610 arc_remove_prune_callback(zfsvfs
->z_arc_prune
);
1611 VERIFY(zfsvfs_teardown(zfsvfs
, B_TRUE
) == 0);
1613 zpl_bdi_destroy(sb
);
1616 * z_os will be NULL if there was an error in
1617 * attempting to reopen zfsvfs.
1621 * Unset the objset user_ptr.
1623 mutex_enter(&os
->os_user_ptr_lock
);
1624 dmu_objset_set_user(os
, NULL
);
1625 mutex_exit(&os
->os_user_ptr_lock
);
1628 * Finally release the objset
1630 dmu_objset_disown(os
, B_TRUE
, zfsvfs
);
1633 zfsvfs_free(zfsvfs
);
1638 zfs_remount(struct super_block
*sb
, int *flags
, zfs_mnt_t
*zm
)
1640 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1642 boolean_t issnap
= dmu_objset_is_snapshot(zfsvfs
->z_os
);
1645 if ((issnap
|| !spa_writeable(dmu_objset_spa(zfsvfs
->z_os
))) &&
1646 !(*flags
& SB_RDONLY
)) {
1647 *flags
|= SB_RDONLY
;
1651 error
= zfsvfs_parse_options(zm
->mnt_data
, &vfsp
);
1655 if (!zfs_is_readonly(zfsvfs
) && (*flags
& SB_RDONLY
))
1656 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), 0);
1658 zfs_unregister_callbacks(zfsvfs
);
1659 zfsvfs_vfs_free(zfsvfs
->z_vfs
);
1661 vfsp
->vfs_data
= zfsvfs
;
1662 zfsvfs
->z_vfs
= vfsp
;
1664 (void) zfs_register_callbacks(vfsp
);
1670 zfs_vget(struct super_block
*sb
, struct inode
**ipp
, fid_t
*fidp
)
1672 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1674 uint64_t object
= 0;
1675 uint64_t fid_gen
= 0;
1682 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1683 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1685 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1686 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1688 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1689 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1691 return (SET_ERROR(EINVAL
));
1694 /* LONG_FID_LEN means snapdirs */
1695 if (fidp
->fid_len
== LONG_FID_LEN
) {
1696 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1697 uint64_t objsetid
= 0;
1698 uint64_t setgen
= 0;
1700 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1701 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1703 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1704 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1706 if (objsetid
!= ZFSCTL_INO_SNAPDIRS
- object
) {
1707 dprintf("snapdir fid: objsetid (%llu) != "
1708 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
1709 objsetid
, ZFSCTL_INO_SNAPDIRS
, object
);
1711 return (SET_ERROR(EINVAL
));
1714 if (fid_gen
> 1 || setgen
!= 0) {
1715 dprintf("snapdir fid: fid_gen (%llu) and setgen "
1716 "(%llu)\n", fid_gen
, setgen
);
1717 return (SET_ERROR(EINVAL
));
1720 return (zfsctl_snapdir_vget(sb
, objsetid
, fid_gen
, ipp
));
1724 /* A zero fid_gen means we are in the .zfs control directories */
1726 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1727 *ipp
= zfsvfs
->z_ctldir
;
1728 ASSERT(*ipp
!= NULL
);
1729 if (object
== ZFSCTL_INO_SNAPDIR
) {
1730 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
,
1731 0, kcred
, NULL
, NULL
) == 0);
1734 * Must have an existing ref, so igrab()
1735 * cannot return NULL
1737 VERIFY3P(igrab(*ipp
), !=, NULL
);
1743 gen_mask
= -1ULL >> (64 - 8 * i
);
1745 dprintf("getting %llu [%llu mask %llx]\n", object
, fid_gen
, gen_mask
);
1746 if ((err
= zfs_zget(zfsvfs
, object
, &zp
))) {
1751 /* Don't export xattr stuff */
1752 if (zp
->z_pflags
& ZFS_XATTR
) {
1755 return (SET_ERROR(ENOENT
));
1758 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
), &zp_gen
,
1760 zp_gen
= zp_gen
& gen_mask
;
1763 if ((fid_gen
== 0) && (zfsvfs
->z_root
== object
))
1765 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1766 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen
,
1770 return (SET_ERROR(ENOENT
));
1775 zfs_znode_update_vfs(ITOZ(*ipp
));
1782 * Block out VFS ops and close zfsvfs_t
1784 * Note, if successful, then we return with the 'z_teardown_lock' and
1785 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1786 * dataset and objset intact so that they can be atomically handed off during
1787 * a subsequent rollback or recv operation and the resume thereafter.
1790 zfs_suspend_fs(zfsvfs_t
*zfsvfs
)
1794 if ((error
= zfsvfs_teardown(zfsvfs
, B_FALSE
)) != 0)
1801 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
1802 * is an invariant across any of the operations that can be performed while the
1803 * filesystem was suspended. Whether it succeeded or failed, the preconditions
1804 * are the same: the relevant objset and associated dataset are owned by
1805 * zfsvfs, held, and long held on entry.
1808 zfs_resume_fs(zfsvfs_t
*zfsvfs
, dsl_dataset_t
*ds
)
1813 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs
));
1814 ASSERT(RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
));
1817 * We already own this, so just update the objset_t, as the one we
1818 * had before may have been evicted.
1821 VERIFY3P(ds
->ds_owner
, ==, zfsvfs
);
1822 VERIFY(dsl_dataset_long_held(ds
));
1823 dsl_pool_t
*dp
= spa_get_dsl(dsl_dataset_get_spa(ds
));
1824 dsl_pool_config_enter(dp
, FTAG
);
1825 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1826 dsl_pool_config_exit(dp
, FTAG
);
1828 err
= zfsvfs_init(zfsvfs
, os
);
1832 ds
->ds_dir
->dd_activity_cancelled
= B_FALSE
;
1833 VERIFY(zfsvfs_setup(zfsvfs
, B_FALSE
) == 0);
1835 zfs_set_fuid_feature(zfsvfs
);
1836 zfsvfs
->z_rollback_time
= jiffies
;
1839 * Attempt to re-establish all the active inodes with their
1840 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1841 * and mark it stale. This prevents a collision if a new
1842 * inode/object is created which must use the same inode
1843 * number. The stale inode will be be released when the
1844 * VFS prunes the dentry holding the remaining references
1845 * on the stale inode.
1847 mutex_enter(&zfsvfs
->z_znodes_lock
);
1848 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
;
1849 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
)) {
1850 err2
= zfs_rezget(zp
);
1852 remove_inode_hash(ZTOI(zp
));
1853 zp
->z_is_stale
= B_TRUE
;
1856 /* see comment in zfs_suspend_fs() */
1857 if (zp
->z_suspended
) {
1858 zfs_zrele_async(zp
);
1859 zp
->z_suspended
= B_FALSE
;
1862 mutex_exit(&zfsvfs
->z_znodes_lock
);
1864 if (!zfs_is_readonly(zfsvfs
) && !zfsvfs
->z_unmounted
) {
1866 * zfs_suspend_fs() could have interrupted freeing
1867 * of dnodes. We need to restart this freeing so
1868 * that we don't "leak" the space.
1870 zfs_unlinked_drain(zfsvfs
);
1874 * Most of the time zfs_suspend_fs is used for changing the contents
1875 * of the underlying dataset. ZFS rollback and receive operations
1876 * might create files for which negative dentries are present in
1877 * the cache. Since walking the dcache would require a lot of GPL-only
1878 * code duplication, it's much easier on these rather rare occasions
1879 * just to flush the whole dcache for the given dataset/filesystem.
1881 shrink_dcache_sb(zfsvfs
->z_sb
);
1885 zfsvfs
->z_unmounted
= B_TRUE
;
1887 /* release the VFS ops */
1888 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1889 ZFS_TEARDOWN_EXIT(zfsvfs
, FTAG
);
1893 * Since we couldn't setup the sa framework, try to force
1894 * unmount this file system.
1897 (void) zfs_umount(zfsvfs
->z_sb
);
1903 * Release VOPs and unmount a suspended filesystem.
1906 zfs_end_fs(zfsvfs_t
*zfsvfs
, dsl_dataset_t
*ds
)
1908 ASSERT(ZFS_TEARDOWN_WRITE_HELD(zfsvfs
));
1909 ASSERT(RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
));
1912 * We already own this, so just hold and rele it to update the
1913 * objset_t, as the one we had before may have been evicted.
1916 VERIFY3P(ds
->ds_owner
, ==, zfsvfs
);
1917 VERIFY(dsl_dataset_long_held(ds
));
1918 dsl_pool_t
*dp
= spa_get_dsl(dsl_dataset_get_spa(ds
));
1919 dsl_pool_config_enter(dp
, FTAG
);
1920 VERIFY0(dmu_objset_from_ds(ds
, &os
));
1921 dsl_pool_config_exit(dp
, FTAG
);
1924 /* release the VOPs */
1925 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1926 ZFS_TEARDOWN_EXIT(zfsvfs
, FTAG
);
1929 * Try to force unmount this file system.
1931 (void) zfs_umount(zfsvfs
->z_sb
);
1932 zfsvfs
->z_unmounted
= B_TRUE
;
1937 * Automounted snapshots rely on periodic revalidation
1938 * to defer snapshots from being automatically unmounted.
1942 zfs_exit_fs(zfsvfs_t
*zfsvfs
)
1944 if (!zfsvfs
->z_issnap
)
1947 if (time_after(jiffies
, zfsvfs
->z_snap_defer_time
+
1948 MAX(zfs_expire_snapshot
* HZ
/ 2, HZ
))) {
1949 zfsvfs
->z_snap_defer_time
= jiffies
;
1950 zfsctl_snapshot_unmount_delay(zfsvfs
->z_os
->os_spa
,
1951 dmu_objset_id(zfsvfs
->z_os
),
1952 zfs_expire_snapshot
);
1957 zfs_set_version(zfsvfs_t
*zfsvfs
, uint64_t newvers
)
1960 objset_t
*os
= zfsvfs
->z_os
;
1963 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1964 return (SET_ERROR(EINVAL
));
1966 if (newvers
< zfsvfs
->z_version
)
1967 return (SET_ERROR(EINVAL
));
1969 if (zfs_spa_version_map(newvers
) >
1970 spa_version(dmu_objset_spa(zfsvfs
->z_os
)))
1971 return (SET_ERROR(ENOTSUP
));
1973 tx
= dmu_tx_create(os
);
1974 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1975 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
1976 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1978 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1980 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1986 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1987 8, 1, &newvers
, tx
);
1994 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
1997 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs
->z_os
)), >=,
1999 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
2000 DMU_OT_NONE
, 0, tx
);
2002 error
= zap_add(os
, MASTER_NODE_OBJ
,
2003 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
2006 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
2007 sa_register_update_callback(os
, zfs_sa_upgrade
);
2010 spa_history_log_internal_ds(dmu_objset_ds(os
), "upgrade", tx
,
2011 "from %llu to %llu", zfsvfs
->z_version
, newvers
);
2015 zfsvfs
->z_version
= newvers
;
2016 os
->os_version
= newvers
;
2018 zfs_set_fuid_feature(zfsvfs
);
2024 * Read a property stored within the master node.
2027 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
2029 uint64_t *cached_copy
= NULL
;
2032 * Figure out where in the objset_t the cached copy would live, if it
2033 * is available for the requested property.
2037 case ZFS_PROP_VERSION
:
2038 cached_copy
= &os
->os_version
;
2040 case ZFS_PROP_NORMALIZE
:
2041 cached_copy
= &os
->os_normalization
;
2043 case ZFS_PROP_UTF8ONLY
:
2044 cached_copy
= &os
->os_utf8only
;
2047 cached_copy
= &os
->os_casesensitivity
;
2053 if (cached_copy
!= NULL
&& *cached_copy
!= OBJSET_PROP_UNINITIALIZED
) {
2054 *value
= *cached_copy
;
2059 * If the property wasn't cached, look up the file system's value for
2060 * the property. For the version property, we look up a slightly
2065 if (prop
== ZFS_PROP_VERSION
)
2066 pname
= ZPL_VERSION_STR
;
2068 pname
= zfs_prop_to_name(prop
);
2071 ASSERT3U(os
->os_phys
->os_type
, ==, DMU_OST_ZFS
);
2072 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
2075 if (error
== ENOENT
) {
2076 /* No value set, use the default value */
2078 case ZFS_PROP_VERSION
:
2079 *value
= ZPL_VERSION
;
2081 case ZFS_PROP_NORMALIZE
:
2082 case ZFS_PROP_UTF8ONLY
:
2086 *value
= ZFS_CASE_SENSITIVE
;
2088 case ZFS_PROP_ACLTYPE
:
2089 *value
= ZFS_ACLTYPE_OFF
;
2098 * If one of the methods for getting the property value above worked,
2099 * copy it into the objset_t's cache.
2101 if (error
== 0 && cached_copy
!= NULL
) {
2102 *cached_copy
= *value
;
2109 * Return true if the corresponding vfs's unmounted flag is set.
2110 * Otherwise return false.
2111 * If this function returns true we know VFS unmount has been initiated.
2114 zfs_get_vfs_flag_unmounted(objset_t
*os
)
2117 boolean_t unmounted
= B_FALSE
;
2119 ASSERT(dmu_objset_type(os
) == DMU_OST_ZFS
);
2121 mutex_enter(&os
->os_user_ptr_lock
);
2122 zfvp
= dmu_objset_get_user(os
);
2123 if (zfvp
!= NULL
&& zfvp
->z_unmounted
)
2125 mutex_exit(&os
->os_user_ptr_lock
);
2131 zfsvfs_update_fromname(const char *oldname
, const char *newname
)
2134 * We don't need to do anything here, the devname is always current by
2135 * virtue of zfsvfs->z_sb->s_op->show_devname.
2137 (void) oldname
, (void) newname
;
2145 dmu_objset_register_type(DMU_OST_ZFS
, zpl_get_file_info
);
2146 register_filesystem(&zpl_fs_type
);
2153 * we don't use outstanding because zpl_posix_acl_free might add more.
2155 taskq_wait(system_delay_taskq
);
2156 taskq_wait(system_taskq
);
2157 unregister_filesystem(&zpl_fs_type
);
2162 #if defined(_KERNEL)
2163 EXPORT_SYMBOL(zfs_suspend_fs
);
2164 EXPORT_SYMBOL(zfs_resume_fs
);
2165 EXPORT_SYMBOL(zfs_set_version
);
2166 EXPORT_SYMBOL(zfsvfs_create
);
2167 EXPORT_SYMBOL(zfsvfs_free
);
2168 EXPORT_SYMBOL(zfs_is_readonly
);
2169 EXPORT_SYMBOL(zfs_domount
);
2170 EXPORT_SYMBOL(zfs_preumount
);
2171 EXPORT_SYMBOL(zfs_umount
);
2172 EXPORT_SYMBOL(zfs_remount
);
2173 EXPORT_SYMBOL(zfs_statvfs
);
2174 EXPORT_SYMBOL(zfs_vget
);
2175 EXPORT_SYMBOL(zfs_prune
);