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.
25 /* Portions Copyright 2010 Robert Milkowski */
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
32 #include <sys/pathname.h>
33 #include <sys/vnode.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/mntent.h>
37 #include <sys/mount.h>
38 #include <sys/cmn_err.h>
39 #include "fs/fs_subr.h"
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_vnops.h>
42 #include <sys/zfs_dir.h>
44 #include <sys/fs/zfs.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_deleg.h>
52 #include <sys/varargs.h>
53 #include <sys/policy.h>
54 #include <sys/atomic.h>
55 #include <sys/mkdev.h>
56 #include <sys/modctl.h>
57 #include <sys/refstr.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/zfs_fuid.h>
60 #include <sys/bootconf.h>
61 #include <sys/sunddi.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/spa_boot.h>
67 #include "zfs_comutil.h"
72 zfs_sync(zfs_sb_t
*zsb
, short flag
, cred_t
*cr
)
75 * Data integrity is job one. We don't want a compromised kernel
76 * writing to the storage pool, so we never sync during panic.
78 if (unlikely(oops_in_progress
))
83 * Sync a specific filesystem.
88 dp
= dmu_objset_pool(zsb
->z_os
);
92 * If the system is shutting down, then skip any
93 * filesystems which may exist on a suspended pool.
95 * XXX: This can be implemented using the Linux reboot
96 * notifiers: {un}register_reboot_notifier().
98 if (sys_shutdown
&& spa_suspended(dp
->dp_spa
)) {
102 #endif /* HAVE_SHUTDOWN */
104 if (zsb
->z_log
!= NULL
)
105 zil_commit(zsb
->z_log
, 0);
110 * Sync all ZFS filesystems. This is what happens when you
111 * run sync(1M). Unlike other filesystems, ZFS honors the
112 * request by waiting for all pools to commit all dirty data.
119 EXPORT_SYMBOL(zfs_sync
);
122 atime_changed_cb(void *arg
, uint64_t newval
)
125 struct super_block
*sb
= zsb
->z_sb
;
126 struct vfsmount
*vfs
= zsb
->z_vfs
;
128 if (newval
== TRUE
) {
129 vfs
->mnt_flags
&= ~MNT_NOATIME
;
130 sb
->s_flags
&= ~MS_NOATIME
;
133 vfs
->mnt_flags
|= MNT_NOATIME
;
134 sb
->s_flags
|= MS_NOATIME
;
135 zsb
->z_atime
= FALSE
;
140 xattr_changed_cb(void *arg
, uint64_t newval
)
144 if (newval
== TRUE
) {
145 zsb
->z_flags
|= ZSB_XATTR_USER
;
147 zsb
->z_flags
&= ~ZSB_XATTR_USER
;
152 blksz_changed_cb(void *arg
, uint64_t newval
)
156 if (newval
< SPA_MINBLOCKSIZE
||
157 newval
> SPA_MAXBLOCKSIZE
|| !ISP2(newval
))
158 newval
= SPA_MAXBLOCKSIZE
;
160 zsb
->z_max_blksz
= newval
;
164 readonly_changed_cb(void *arg
, uint64_t newval
)
167 struct super_block
*sb
= zsb
->z_sb
;
168 struct vfsmount
*vfs
= zsb
->z_vfs
;
171 vfs
->mnt_flags
|= MNT_READONLY
;
172 sb
->s_flags
|= MS_RDONLY
;
174 vfs
->mnt_flags
&= ~MNT_READONLY
;
175 sb
->s_flags
&= ~MS_RDONLY
;
180 devices_changed_cb(void *arg
, uint64_t newval
)
183 struct super_block
*sb
= zsb
->z_sb
;
184 struct vfsmount
*vfs
= zsb
->z_vfs
;
186 if (newval
== FALSE
) {
187 vfs
->mnt_flags
|= MNT_NODEV
;
188 sb
->s_flags
|= MS_NODEV
;
190 vfs
->mnt_flags
&= ~MNT_NODEV
;
191 sb
->s_flags
&= ~MS_NODEV
;
196 setuid_changed_cb(void *arg
, uint64_t newval
)
199 struct super_block
*sb
= zsb
->z_sb
;
200 struct vfsmount
*vfs
= zsb
->z_vfs
;
202 if (newval
== FALSE
) {
203 vfs
->mnt_flags
|= MNT_NOSUID
;
204 sb
->s_flags
|= MS_NOSUID
;
206 vfs
->mnt_flags
&= ~MNT_NOSUID
;
207 sb
->s_flags
&= ~MS_NOSUID
;
212 exec_changed_cb(void *arg
, uint64_t newval
)
215 struct super_block
*sb
= zsb
->z_sb
;
216 struct vfsmount
*vfs
= zsb
->z_vfs
;
218 if (newval
== FALSE
) {
219 vfs
->mnt_flags
|= MNT_NOEXEC
;
220 sb
->s_flags
|= MS_NOEXEC
;
222 vfs
->mnt_flags
&= ~MNT_NOEXEC
;
223 sb
->s_flags
&= ~MS_NOEXEC
;
228 * The nbmand mount option can be changed at mount time.
229 * We can't allow it to be toggled on live file systems or incorrect
230 * behavior may be seen from cifs clients
232 * This property isn't registered via dsl_prop_register(), but this callback
233 * will be called when a file system is first mounted
236 nbmand_changed_cb(void *arg
, uint64_t newval
)
239 struct super_block
*sb
= zsb
->z_sb
;
241 if (newval
== TRUE
) {
242 sb
->s_flags
|= MS_MANDLOCK
;
244 sb
->s_flags
&= ~MS_MANDLOCK
;
249 snapdir_changed_cb(void *arg
, uint64_t newval
)
251 ((zfs_sb_t
*)arg
)->z_show_ctldir
= newval
;
255 vscan_changed_cb(void *arg
, uint64_t newval
)
257 ((zfs_sb_t
*)arg
)->z_vscan
= newval
;
261 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
263 ((zfs_sb_t
*)arg
)->z_acl_inherit
= newval
;
267 zfs_register_callbacks(zfs_sb_t
*zsb
)
269 struct vfsmount
*vfsp
= zsb
->z_vfs
;
270 struct dsl_dataset
*ds
= NULL
;
271 objset_t
*os
= zsb
->z_os
;
273 boolean_t readonly
= B_FALSE
;
274 boolean_t setuid
= B_TRUE
;
275 boolean_t exec
= B_TRUE
;
276 boolean_t devices
= B_TRUE
;
277 boolean_t xattr
= B_TRUE
;
278 boolean_t atime
= B_TRUE
;
279 char osname
[MAXNAMELEN
];
283 * While Linux allows multiple vfs mounts per super block we have
284 * limited it artificially to one in zfs_fill_super. Thus it is
285 * safe for us to modify the vfs mount fails through the callbacks.
287 if ((vfsp
->mnt_flags
& MNT_READONLY
) ||
288 !spa_writeable(dmu_objset_spa(os
)))
291 if (vfsp
->mnt_flags
& MNT_NOSUID
) {
295 if (vfsp
->mnt_flags
& MNT_NODEV
)
299 if (vfsp
->mnt_flags
& MNT_NOEXEC
)
302 if (vfsp
->mnt_flags
& MNT_NOATIME
)
306 * nbmand is a special property which may only be changed at
307 * mount time. Unfortunately, Linux does not have a VFS mount
308 * flag instead this is a super block flag. So setting this
309 * option at mount time will have to wait until we can parse
310 * the mount option string. For now we rely on the nbmand
311 * value stored with the object set. Additional mount option
312 * string to be handled:
314 * case: sensitive|insensitive|mixed
318 dmu_objset_name(os
, osname
);
319 if ((error
= dsl_prop_get_integer(osname
, "nbmand", &nbmand
, NULL
)))
323 * Register property callbacks.
325 * It would probably be fine to just check for i/o error from
326 * the first prop_register(), but I guess I like to go
329 ds
= dmu_objset_ds(os
);
330 error
= dsl_prop_register(ds
,
331 "atime", atime_changed_cb
, zsb
);
332 error
= error
? error
: dsl_prop_register(ds
,
333 "xattr", xattr_changed_cb
, zsb
);
334 error
= error
? error
: dsl_prop_register(ds
,
335 "recordsize", blksz_changed_cb
, zsb
);
336 error
= error
? error
: dsl_prop_register(ds
,
337 "readonly", readonly_changed_cb
, zsb
);
338 error
= error
? error
: dsl_prop_register(ds
,
339 "devices", devices_changed_cb
, zsb
);
340 error
= error
? error
: dsl_prop_register(ds
,
341 "setuid", setuid_changed_cb
, zsb
);
342 error
= error
? error
: dsl_prop_register(ds
,
343 "exec", exec_changed_cb
, zsb
);
344 error
= error
? error
: dsl_prop_register(ds
,
345 "snapdir", snapdir_changed_cb
, zsb
);
346 error
= error
? error
: dsl_prop_register(ds
,
347 "aclinherit", acl_inherit_changed_cb
, zsb
);
348 error
= error
? error
: dsl_prop_register(ds
,
349 "vscan", vscan_changed_cb
, zsb
);
354 * Invoke our callbacks to set required flags.
356 readonly_changed_cb(zsb
, readonly
);
357 setuid_changed_cb(zsb
, setuid
);
358 exec_changed_cb(zsb
, exec
);
359 devices_changed_cb(zsb
, devices
);
360 xattr_changed_cb(zsb
, xattr
);
361 atime_changed_cb(zsb
, atime
);
362 nbmand_changed_cb(zsb
, nbmand
);
368 * We may attempt to unregister some callbacks that are not
369 * registered, but this is OK; it will simply return ENOMSG,
370 * which we will ignore.
372 (void) dsl_prop_unregister(ds
, "atime", atime_changed_cb
, zsb
);
373 (void) dsl_prop_unregister(ds
, "xattr", xattr_changed_cb
, zsb
);
374 (void) dsl_prop_unregister(ds
, "recordsize", blksz_changed_cb
, zsb
);
375 (void) dsl_prop_unregister(ds
, "readonly", readonly_changed_cb
, zsb
);
376 (void) dsl_prop_unregister(ds
, "devices", devices_changed_cb
, zsb
);
377 (void) dsl_prop_unregister(ds
, "setuid", setuid_changed_cb
, zsb
);
378 (void) dsl_prop_unregister(ds
, "exec", exec_changed_cb
, zsb
);
379 (void) dsl_prop_unregister(ds
, "snapdir", snapdir_changed_cb
, zsb
);
380 (void) dsl_prop_unregister(ds
, "aclinherit", acl_inherit_changed_cb
,
382 (void) dsl_prop_unregister(ds
, "vscan", vscan_changed_cb
, zsb
);
386 EXPORT_SYMBOL(zfs_register_callbacks
);
389 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
390 uint64_t *userp
, uint64_t *groupp
)
392 znode_phys_t
*znp
= data
;
396 * Is it a valid type of object to track?
398 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
402 * If we have a NULL data pointer
403 * then assume the id's aren't changing and
404 * return EEXIST to the dmu to let it know to
410 if (bonustype
== DMU_OT_ZNODE
) {
411 *userp
= znp
->zp_uid
;
412 *groupp
= znp
->zp_gid
;
416 ASSERT(bonustype
== DMU_OT_SA
);
417 hdrsize
= sa_hdrsize(data
);
420 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
422 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
426 * This should only happen for newly created
427 * files that haven't had the znode data filled
438 fuidstr_to_sid(zfs_sb_t
*zsb
, const char *fuidstr
,
439 char *domainbuf
, int buflen
, uid_t
*ridp
)
444 fuid
= strtonum(fuidstr
, NULL
);
446 domain
= zfs_fuid_find_by_idx(zsb
, FUID_INDEX(fuid
));
448 (void) strlcpy(domainbuf
, domain
, buflen
);
451 *ridp
= FUID_RID(fuid
);
455 zfs_userquota_prop_to_obj(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
)
458 case ZFS_PROP_USERUSED
:
459 return (DMU_USERUSED_OBJECT
);
460 case ZFS_PROP_GROUPUSED
:
461 return (DMU_GROUPUSED_OBJECT
);
462 case ZFS_PROP_USERQUOTA
:
463 return (zsb
->z_userquota_obj
);
464 case ZFS_PROP_GROUPQUOTA
:
465 return (zsb
->z_groupquota_obj
);
473 zfs_userspace_many(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
474 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
479 zfs_useracct_t
*buf
= vbuf
;
482 if (!dmu_objset_userspace_present(zsb
->z_os
))
485 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
491 for (zap_cursor_init_serialized(&zc
, zsb
->z_os
, obj
, *cookiep
);
492 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
493 zap_cursor_advance(&zc
)) {
494 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
498 fuidstr_to_sid(zsb
, za
.za_name
,
499 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
501 buf
->zu_space
= za
.za_first_integer
;
507 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
508 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
509 *cookiep
= zap_cursor_serialize(&zc
);
510 zap_cursor_fini(&zc
);
513 EXPORT_SYMBOL(zfs_userspace_many
);
516 * buf must be big enough (eg, 32 bytes)
519 id_to_fuidstr(zfs_sb_t
*zsb
, const char *domain
, uid_t rid
,
520 char *buf
, boolean_t addok
)
525 if (domain
&& domain
[0]) {
526 domainid
= zfs_fuid_find_by_domain(zsb
, domain
, NULL
, addok
);
530 fuid
= FUID_ENCODE(domainid
, rid
);
531 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
536 zfs_userspace_one(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
537 const char *domain
, uint64_t rid
, uint64_t *valp
)
545 if (!dmu_objset_userspace_present(zsb
->z_os
))
548 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
552 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_FALSE
);
556 err
= zap_lookup(zsb
->z_os
, obj
, buf
, 8, 1, valp
);
561 EXPORT_SYMBOL(zfs_userspace_one
);
564 zfs_set_userquota(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
565 const char *domain
, uint64_t rid
, uint64_t quota
)
571 boolean_t fuid_dirtied
;
573 if (type
!= ZFS_PROP_USERQUOTA
&& type
!= ZFS_PROP_GROUPQUOTA
)
576 if (zsb
->z_version
< ZPL_VERSION_USERSPACE
)
579 objp
= (type
== ZFS_PROP_USERQUOTA
) ? &zsb
->z_userquota_obj
:
580 &zsb
->z_groupquota_obj
;
582 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_TRUE
);
585 fuid_dirtied
= zsb
->z_fuid_dirty
;
587 tx
= dmu_tx_create(zsb
->z_os
);
588 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
590 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
591 zfs_userquota_prop_prefixes
[type
]);
594 zfs_fuid_txhold(zsb
, tx
);
595 err
= dmu_tx_assign(tx
, TXG_WAIT
);
601 mutex_enter(&zsb
->z_lock
);
603 *objp
= zap_create(zsb
->z_os
, DMU_OT_USERGROUP_QUOTA
,
605 VERIFY(0 == zap_add(zsb
->z_os
, MASTER_NODE_OBJ
,
606 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
608 mutex_exit(&zsb
->z_lock
);
611 err
= zap_remove(zsb
->z_os
, *objp
, buf
, tx
);
615 err
= zap_update(zsb
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
619 zfs_fuid_sync(zsb
, tx
);
623 EXPORT_SYMBOL(zfs_set_userquota
);
626 zfs_fuid_overquota(zfs_sb_t
*zsb
, boolean_t isgroup
, uint64_t fuid
)
629 uint64_t used
, quota
, usedobj
, quotaobj
;
632 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
633 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
635 if (quotaobj
== 0 || zsb
->z_replay
)
638 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
639 err
= zap_lookup(zsb
->z_os
, quotaobj
, buf
, 8, 1, "a
);
643 err
= zap_lookup(zsb
->z_os
, usedobj
, buf
, 8, 1, &used
);
646 return (used
>= quota
);
648 EXPORT_SYMBOL(zfs_fuid_overquota
);
651 zfs_owner_overquota(zfs_sb_t
*zsb
, znode_t
*zp
, boolean_t isgroup
)
656 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
658 fuid
= isgroup
? zp
->z_gid
: zp
->z_uid
;
660 if (quotaobj
== 0 || zsb
->z_replay
)
663 return (zfs_fuid_overquota(zsb
, isgroup
, fuid
));
665 EXPORT_SYMBOL(zfs_owner_overquota
);
668 zfs_sb_create(const char *osname
, zfs_sb_t
**zsbp
)
676 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
679 * We claim to always be readonly so we can open snapshots;
680 * other ZPL code will prevent us from writing to snapshots.
682 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zsb
, &os
);
684 kmem_free(zsb
, sizeof (zfs_sb_t
));
689 * Initialize the zfs-specific filesystem structure.
690 * Should probably make this a kmem cache, shuffle fields,
691 * and just bzero up to z_hold_mtx[].
695 zsb
->z_max_blksz
= SPA_MAXBLOCKSIZE
;
696 zsb
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
699 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zsb
->z_version
);
702 } else if (zsb
->z_version
>
703 zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)))) {
704 (void) printk("Can't mount a version %lld file system "
705 "on a version %lld pool\n. Pool must be upgraded to mount "
706 "this file system.", (u_longlong_t
)zsb
->z_version
,
707 (u_longlong_t
)spa_version(dmu_objset_spa(os
)));
711 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &zval
)) != 0)
713 zsb
->z_norm
= (int)zval
;
715 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &zval
)) != 0)
717 zsb
->z_utf8
= (zval
!= 0);
719 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &zval
)) != 0)
721 zsb
->z_case
= (uint_t
)zval
;
724 * Fold case on file systems that are always or sometimes case
727 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
728 zsb
->z_case
== ZFS_CASE_MIXED
)
729 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
731 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
732 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
735 /* should either have both of these objects or none */
736 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
742 * Pre SA versions file systems should never touch
743 * either the attribute registration or layout objects.
748 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
753 if (zsb
->z_version
>= ZPL_VERSION_SA
)
754 sa_register_update_callback(os
, zfs_sa_upgrade
);
756 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
760 ASSERT(zsb
->z_root
!= 0);
762 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
763 &zsb
->z_unlinkedobj
);
767 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
768 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
769 8, 1, &zsb
->z_userquota_obj
);
770 if (error
&& error
!= ENOENT
)
773 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
774 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
775 8, 1, &zsb
->z_groupquota_obj
);
776 if (error
&& error
!= ENOENT
)
779 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
781 if (error
&& error
!= ENOENT
)
784 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
786 if (error
&& error
!= ENOENT
)
789 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
790 mutex_init(&zsb
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
791 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
792 offsetof(znode_t
, z_link_node
));
793 rrw_init(&zsb
->z_teardown_lock
);
794 rw_init(&zsb
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
795 rw_init(&zsb
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
796 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
797 mutex_init(&zsb
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
803 dmu_objset_disown(os
, zsb
);
805 kmem_free(zsb
, sizeof (zfs_sb_t
));
810 zfs_sb_setup(zfs_sb_t
*zsb
, boolean_t mounting
)
814 error
= zfs_register_callbacks(zsb
);
819 * Set the objset user_ptr to track its zsb.
821 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
822 dmu_objset_set_user(zsb
->z_os
, zsb
);
823 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
825 zsb
->z_log
= zil_open(zsb
->z_os
, zfs_get_data
);
828 * If we are not mounting (ie: online recv), then we don't
829 * have to worry about replaying the log as we blocked all
830 * operations out since we closed the ZIL.
836 * During replay we remove the read only flag to
837 * allow replays to succeed.
839 readonly
= zsb
->z_vfs
->mnt_flags
& MNT_READONLY
;
841 zsb
->z_vfs
->mnt_flags
&= ~MNT_READONLY
;
843 zfs_unlinked_drain(zsb
);
846 * Parse and replay the intent log.
848 * Because of ziltest, this must be done after
849 * zfs_unlinked_drain(). (Further note: ziltest
850 * doesn't use readonly mounts, where
851 * zfs_unlinked_drain() isn't called.) This is because
852 * ziltest causes spa_sync() to think it's committed,
853 * but actually it is not, so the intent log contains
854 * many txg's worth of changes.
856 * In particular, if object N is in the unlinked set in
857 * the last txg to actually sync, then it could be
858 * actually freed in a later txg and then reallocated
859 * in a yet later txg. This would write a "create
860 * object N" record to the intent log. Normally, this
861 * would be fine because the spa_sync() would have
862 * written out the fact that object N is free, before
863 * we could write the "create object N" intent log
866 * But when we are in ziltest mode, we advance the "open
867 * txg" without actually spa_sync()-ing the changes to
868 * disk. So we would see that object N is still
869 * allocated and in the unlinked set, and there is an
870 * intent log record saying to allocate it.
872 if (spa_writeable(dmu_objset_spa(zsb
->z_os
))) {
873 if (zil_replay_disable
) {
874 zil_destroy(zsb
->z_log
, B_FALSE
);
876 zsb
->z_replay
= B_TRUE
;
877 zil_replay(zsb
->z_os
, zsb
,
879 zsb
->z_replay
= B_FALSE
;
882 zsb
->z_vfs
->mnt_flags
|= readonly
; /* restore readonly bit */
889 zfs_sb_free(zfs_sb_t
*zsb
)
893 zfs_fuid_destroy(zsb
);
895 mutex_destroy(&zsb
->z_znodes_lock
);
896 mutex_destroy(&zsb
->z_lock
);
897 list_destroy(&zsb
->z_all_znodes
);
898 rrw_destroy(&zsb
->z_teardown_lock
);
899 rw_destroy(&zsb
->z_teardown_inactive_lock
);
900 rw_destroy(&zsb
->z_fuid_lock
);
901 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
902 mutex_destroy(&zsb
->z_hold_mtx
[i
]);
903 kmem_free(zsb
, sizeof (zfs_sb_t
));
907 zfs_set_fuid_feature(zfs_sb_t
*zsb
)
909 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
910 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
914 zfs_unregister_callbacks(zfs_sb_t
*zsb
)
916 objset_t
*os
= zsb
->z_os
;
917 struct dsl_dataset
*ds
;
920 * Unregister properties.
922 if (!dmu_objset_is_snapshot(os
)) {
923 ds
= dmu_objset_ds(os
);
924 VERIFY(dsl_prop_unregister(ds
, "atime", atime_changed_cb
,
927 VERIFY(dsl_prop_unregister(ds
, "xattr", xattr_changed_cb
,
930 VERIFY(dsl_prop_unregister(ds
, "recordsize", blksz_changed_cb
,
933 VERIFY(dsl_prop_unregister(ds
, "readonly", readonly_changed_cb
,
936 VERIFY(dsl_prop_unregister(ds
, "devices", devices_changed_cb
,
939 VERIFY(dsl_prop_unregister(ds
, "setuid", setuid_changed_cb
,
942 VERIFY(dsl_prop_unregister(ds
, "exec", exec_changed_cb
,
945 VERIFY(dsl_prop_unregister(ds
, "snapdir", snapdir_changed_cb
,
948 VERIFY(dsl_prop_unregister(ds
, "aclinherit",
949 acl_inherit_changed_cb
, zsb
) == 0);
951 VERIFY(dsl_prop_unregister(ds
, "vscan",
952 vscan_changed_cb
, zsb
) == 0);
955 EXPORT_SYMBOL(zfs_unregister_callbacks
);
959 * zfs_check_global_label:
960 * Check that the hex label string is appropriate for the dataset
961 * being mounted into the global_zone proper.
963 * Return an error if the hex label string is not default or
964 * admin_low/admin_high. For admin_low labels, the corresponding
965 * dataset must be readonly.
968 zfs_check_global_label(const char *dsname
, const char *hexsl
)
970 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
972 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
974 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
975 /* must be readonly */
978 if (dsl_prop_get_integer(dsname
,
979 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
981 return (rdonly
? 0 : EACCES
);
985 #endif /* HAVE_MLSLABEL */
988 zfs_statvfs(struct dentry
*dentry
, struct kstatfs
*statp
)
990 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
991 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
996 dmu_objset_space(zsb
->z_os
,
997 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1000 * The underlying storage pool actually uses multiple block sizes.
1001 * We report the fragsize as the smallest block size we support,
1002 * and we report our blocksize as the filesystem's maximum blocksize.
1004 statp
->f_frsize
= 1UL << SPA_MINBLOCKSHIFT
;
1005 statp
->f_bsize
= zsb
->z_max_blksz
;
1006 bshift
= fls(statp
->f_bsize
) - 1;
1009 * The following report "total" blocks of various kinds in
1010 * the file system, but reported in terms of f_bsize - the
1014 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
1015 statp
->f_bfree
= availbytes
>> bshift
;
1016 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1019 * statvfs() should really be called statufs(), because it assumes
1020 * static metadata. ZFS doesn't preallocate files, so the best
1021 * we can do is report the max that could possibly fit in f_files,
1022 * and that minus the number actually used in f_ffree.
1023 * For f_ffree, report the smaller of the number of object available
1024 * and the number of blocks (each object will take at least a block).
1026 statp
->f_ffree
= MIN(availobjs
, statp
->f_bfree
);
1027 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1028 statp
->f_fsid
.val
[0] = 0; /* XXX: Map up some unique ID */
1029 statp
->f_fsid
.val
[1] = 0;
1030 statp
->f_type
= ZFS_SUPER_MAGIC
;
1031 statp
->f_namelen
= ZFS_MAXNAMELEN
;
1034 * We have all of 40 characters to stuff a string here.
1035 * Is there anything useful we could/should provide?
1037 bzero(statp
->f_spare
, sizeof (statp
->f_spare
));
1042 EXPORT_SYMBOL(zfs_statvfs
);
1045 zfs_root(zfs_sb_t
*zsb
, struct inode
**ipp
)
1052 error
= zfs_zget(zsb
, zsb
->z_root
, &rootzp
);
1054 *ipp
= ZTOI(rootzp
);
1059 EXPORT_SYMBOL(zfs_root
);
1062 * Teardown the zfs_sb_t::z_os.
1064 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1065 * and 'z_teardown_inactive_lock' held.
1068 zfsvfs_teardown(zfs_sb_t
*zsb
, boolean_t unmounting
)
1072 rrw_enter(&zsb
->z_teardown_lock
, RW_WRITER
, FTAG
);
1076 * We purge the parent filesystem's super block as the
1077 * parent filesystem and all of its snapshots have their
1078 * inode's super block set to the parent's filesystem's
1079 * super block. Note, 'z_parent' is self referential
1080 * for non-snapshots.
1082 shrink_dcache_sb(zsb
->z_parent
->z_sb
);
1083 invalidate_inodes(zsb
->z_parent
->z_sb
);
1087 * Close the zil. NB: Can't close the zil while zfs_inactive
1088 * threads are blocked as zil_close can call zfs_inactive.
1091 zil_close(zsb
->z_log
);
1095 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_WRITER
);
1098 * If we are not unmounting (ie: online recv) and someone already
1099 * unmounted this file system while we were doing the switcheroo,
1100 * or a reopen of z_os failed then just bail out now.
1102 if (!unmounting
&& (zsb
->z_unmounted
|| zsb
->z_os
== NULL
)) {
1103 rw_exit(&zsb
->z_teardown_inactive_lock
);
1104 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1109 * At this point there are no vops active, and any new vops will
1110 * fail with EIO since we have z_teardown_lock for writer (only
1111 * relavent for forced unmount).
1113 * Release all holds on dbufs.
1115 mutex_enter(&zsb
->z_znodes_lock
);
1116 for (zp
= list_head(&zsb
->z_all_znodes
); zp
!= NULL
;
1117 zp
= list_next(&zsb
->z_all_znodes
, zp
))
1119 ASSERT(atomic_read(&ZTOI(zp
)->i_count
) > 0);
1120 zfs_znode_dmu_fini(zp
);
1122 mutex_exit(&zsb
->z_znodes_lock
);
1125 * If we are unmounting, set the unmounted flag and let new vops
1126 * unblock. zfs_inactive will have the unmounted behavior, and all
1127 * other vops will fail with EIO.
1130 zsb
->z_unmounted
= B_TRUE
;
1131 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1132 rw_exit(&zsb
->z_teardown_inactive_lock
);
1136 * z_os will be NULL if there was an error in attempting to reopen
1137 * zsb, so just return as the properties had already been
1139 * unregistered and cached data had been evicted before.
1141 if (zsb
->z_os
== NULL
)
1145 * Unregister properties.
1147 zfs_unregister_callbacks(zsb
);
1152 if (dmu_objset_is_dirty_anywhere(zsb
->z_os
))
1153 if (!(zsb
->z_vfs
->mnt_flags
& MNT_READONLY
))
1154 txg_wait_synced(dmu_objset_pool(zsb
->z_os
), 0);
1155 (void) dmu_objset_evict_dbufs(zsb
->z_os
);
1161 zfs_domount(struct super_block
*sb
, void *data
, int silent
)
1163 zpl_mount_data_t
*zmd
= data
;
1164 const char *osname
= zmd
->z_osname
;
1166 struct inode
*root_inode
;
1167 uint64_t recordsize
;
1171 * Linux allows multiple vfs mounts per super block. However, the
1172 * zfs_sb_t only contains a pointer for a single vfs mount. This
1173 * back reference in the long term could be extended to a list of
1174 * vfs mounts if a hook were added to the kernel to notify us when
1175 * a vfsmount is destroyed. Until then we must limit the number
1176 * of mounts per super block to one.
1178 if (atomic_read(&sb
->s_active
) > 1)
1181 error
= zfs_sb_create(osname
, &zsb
);
1185 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1186 &recordsize
, NULL
)))
1190 zsb
->z_vfs
= zmd
->z_vfs
;
1191 sb
->s_fs_info
= zsb
;
1192 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1193 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1194 sb
->s_time_gran
= 1;
1195 sb
->s_blocksize
= recordsize
;
1196 sb
->s_blocksize_bits
= ilog2(recordsize
);
1198 /* Set callback operations for the file system. */
1199 sb
->s_op
= &zpl_super_operations
;
1200 sb
->s_xattr
= zpl_xattr_handlers
;
1202 sb
->s_export_op
= &zpl_export_operations
;
1203 #endif /* HAVE_EXPORTS */
1205 /* Set features for file system. */
1206 zfs_set_fuid_feature(zsb
);
1208 if (dmu_objset_is_snapshot(zsb
->z_os
)) {
1211 atime_changed_cb(zsb
, B_FALSE
);
1212 readonly_changed_cb(zsb
, B_TRUE
);
1213 if ((error
= dsl_prop_get_integer(osname
,"xattr",&pval
,NULL
)))
1215 xattr_changed_cb(zsb
, pval
);
1216 zsb
->z_issnap
= B_TRUE
;
1217 zsb
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1219 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
1220 dmu_objset_set_user(zsb
->z_os
, zsb
);
1221 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
1223 error
= zfs_sb_setup(zsb
, B_TRUE
);
1224 #ifdef HAVE_SNAPSHOT
1225 (void) zfs_snap_create(zsb
);
1226 #endif /* HAVE_SNAPSHOT */
1229 /* Allocate a root inode for the filesystem. */
1230 error
= zfs_root(zsb
, &root_inode
);
1232 (void) zfs_umount(sb
);
1236 /* Allocate a root dentry for the filesystem */
1237 sb
->s_root
= d_alloc_root(root_inode
);
1238 if (sb
->s_root
== NULL
) {
1239 (void) zfs_umount(sb
);
1245 dmu_objset_disown(zsb
->z_os
, zsb
);
1251 EXPORT_SYMBOL(zfs_domount
);
1255 zfs_umount(struct super_block
*sb
)
1257 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1260 VERIFY(zfsvfs_teardown(zsb
, B_TRUE
) == 0);
1264 * z_os will be NULL if there was an error in
1265 * attempting to reopen zsb.
1269 * Unset the objset user_ptr.
1271 mutex_enter(&os
->os_user_ptr_lock
);
1272 dmu_objset_set_user(os
, NULL
);
1273 mutex_exit(&os
->os_user_ptr_lock
);
1276 * Finally release the objset
1278 dmu_objset_disown(os
, zsb
);
1284 EXPORT_SYMBOL(zfs_umount
);
1287 zfs_vget(struct vfsmount
*vfsp
, struct inode
**ipp
, fid_t
*fidp
)
1289 zfs_sb_t
*zsb
= VTOZSB(vfsp
);
1291 uint64_t object
= 0;
1292 uint64_t fid_gen
= 0;
1301 if (fidp
->fid_len
== LONG_FID_LEN
) {
1302 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1303 uint64_t objsetid
= 0;
1304 uint64_t setgen
= 0;
1306 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1307 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1309 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1310 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1314 #ifdef HAVE_SNAPSHOT
1315 err
= zfsctl_lookup_objset(vfsp
, objsetid
, &zsb
);
1318 #endif /* HAVE_SNAPSHOT */
1322 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1323 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1325 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1326 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1328 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1329 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1335 #ifdef HAVE_SNAPSHOT
1336 /* A zero fid_gen means we are in the .zfs control directories */
1338 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1339 *ipp
= zsb
->z_ctldir
;
1340 ASSERT(*ipp
!= NULL
);
1341 if (object
== ZFSCTL_INO_SNAPDIR
) {
1342 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
, NULL
,
1343 0, NULL
, NULL
, NULL
, NULL
, NULL
) == 0);
1350 #endif /* HAVE_SNAPSHOT */
1352 gen_mask
= -1ULL >> (64 - 8 * i
);
1354 dprintf("getting %llu [%u mask %llx]\n", object
, fid_gen
, gen_mask
);
1355 if ((err
= zfs_zget(zsb
, object
, &zp
))) {
1359 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
), &zp_gen
,
1361 zp_gen
= zp_gen
& gen_mask
;
1364 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1365 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen
, fid_gen
);
1373 zfs_inode_update(ITOZ(*ipp
));
1378 EXPORT_SYMBOL(zfs_vget
);
1381 * Block out VOPs and close zfs_sb_t::z_os
1383 * Note, if successful, then we return with the 'z_teardown_lock' and
1384 * 'z_teardown_inactive_lock' write held.
1387 zfs_suspend_fs(zfs_sb_t
*zsb
)
1391 if ((error
= zfsvfs_teardown(zsb
, B_FALSE
)) != 0)
1393 dmu_objset_disown(zsb
->z_os
, zsb
);
1397 EXPORT_SYMBOL(zfs_suspend_fs
);
1400 * Reopen zfs_sb_t::z_os and release VOPs.
1403 zfs_resume_fs(zfs_sb_t
*zsb
, const char *osname
)
1407 ASSERT(RRW_WRITE_HELD(&zsb
->z_teardown_lock
));
1408 ASSERT(RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
));
1410 err
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_FALSE
, zsb
, &zsb
->z_os
);
1415 uint64_t sa_obj
= 0;
1417 err2
= zap_lookup(zsb
->z_os
, MASTER_NODE_OBJ
,
1418 ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1420 if ((err
|| err2
) && zsb
->z_version
>= ZPL_VERSION_SA
)
1424 if ((err
= sa_setup(zsb
->z_os
, sa_obj
,
1425 zfs_attr_table
, ZPL_END
, &zsb
->z_attr_table
)) != 0)
1428 VERIFY(zfs_sb_setup(zsb
, B_FALSE
) == 0);
1431 * Attempt to re-establish all the active znodes with
1432 * their dbufs. If a zfs_rezget() fails, then we'll let
1433 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1434 * when they try to use their znode.
1436 mutex_enter(&zsb
->z_znodes_lock
);
1437 for (zp
= list_head(&zsb
->z_all_znodes
); zp
;
1438 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1439 (void) zfs_rezget(zp
);
1441 mutex_exit(&zsb
->z_znodes_lock
);
1446 /* release the VOPs */
1447 rw_exit(&zsb
->z_teardown_inactive_lock
);
1448 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1452 * Since we couldn't reopen zfs_sb_t::z_os, force
1453 * unmount this file system.
1455 (void) zfs_umount(zsb
->z_sb
);
1459 EXPORT_SYMBOL(zfs_resume_fs
);
1462 zfs_set_version(zfs_sb_t
*zsb
, uint64_t newvers
)
1465 objset_t
*os
= zsb
->z_os
;
1468 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1471 if (newvers
< zsb
->z_version
)
1474 if (zfs_spa_version_map(newvers
) >
1475 spa_version(dmu_objset_spa(zsb
->z_os
)))
1478 tx
= dmu_tx_create(os
);
1479 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1480 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1481 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1483 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1485 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1491 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1492 8, 1, &newvers
, tx
);
1499 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1502 ASSERT3U(spa_version(dmu_objset_spa(zsb
->z_os
)), >=,
1504 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1505 DMU_OT_NONE
, 0, tx
);
1507 error
= zap_add(os
, MASTER_NODE_OBJ
,
1508 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1509 ASSERT3U(error
, ==, 0);
1511 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
1512 sa_register_update_callback(os
, zfs_sa_upgrade
);
1515 spa_history_log_internal(LOG_DS_UPGRADE
,
1516 dmu_objset_spa(os
), tx
, "oldver=%llu newver=%llu dataset = %llu",
1517 zsb
->z_version
, newvers
, dmu_objset_id(os
));
1521 zsb
->z_version
= newvers
;
1523 if (zsb
->z_version
>= ZPL_VERSION_FUID
)
1524 zfs_set_fuid_feature(zsb
);
1528 EXPORT_SYMBOL(zfs_set_version
);
1531 * Read a property stored within the master node.
1534 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
1540 * Look up the file system's value for the property. For the
1541 * version property, we look up a slightly different string.
1543 if (prop
== ZFS_PROP_VERSION
)
1544 pname
= ZPL_VERSION_STR
;
1546 pname
= zfs_prop_to_name(prop
);
1549 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
1551 if (error
== ENOENT
) {
1552 /* No value set, use the default value */
1554 case ZFS_PROP_VERSION
:
1555 *value
= ZPL_VERSION
;
1557 case ZFS_PROP_NORMALIZE
:
1558 case ZFS_PROP_UTF8ONLY
:
1562 *value
= ZFS_CASE_SENSITIVE
;
1576 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
1577 register_filesystem(&zpl_fs_type
);
1583 unregister_filesystem(&zpl_fs_type
);