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_ctldir.h>
60 #include <sys/zfs_fuid.h>
61 #include <sys/bootconf.h>
62 #include <sys/sunddi.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa_boot.h>
68 #include "zfs_comutil.h"
73 zfs_sync(struct super_block
*sb
, int wait
, cred_t
*cr
)
75 zfs_sb_t
*zsb
= sb
->s_fs_info
;
78 * Data integrity is job one. We don't want a compromised kernel
79 * writing to the storage pool, so we never sync during panic.
81 if (unlikely(oops_in_progress
))
85 * Semantically, the only requirement is that the sync be initiated.
86 * The DMU syncs out txgs frequently, so there's nothing to do.
93 * Sync a specific filesystem.
98 dp
= dmu_objset_pool(zsb
->z_os
);
101 * If the system is shutting down, then skip any
102 * filesystems which may exist on a suspended pool.
104 if (spa_suspended(dp
->dp_spa
)) {
109 if (zsb
->z_log
!= NULL
)
110 zil_commit(zsb
->z_log
, 0);
115 * Sync all ZFS filesystems. This is what happens when you
116 * run sync(1M). Unlike other filesystems, ZFS honors the
117 * request by waiting for all pools to commit all dirty data.
124 EXPORT_SYMBOL(zfs_sync
);
127 zfs_is_readonly(zfs_sb_t
*zsb
)
129 return (!!(zsb
->z_sb
->s_flags
& MS_RDONLY
));
131 EXPORT_SYMBOL(zfs_is_readonly
);
134 atime_changed_cb(void *arg
, uint64_t newval
)
136 ((zfs_sb_t
*)arg
)->z_atime
= newval
;
140 xattr_changed_cb(void *arg
, uint64_t newval
)
144 if (newval
== ZFS_XATTR_OFF
) {
145 zsb
->z_flags
&= ~ZSB_XATTR
;
147 zsb
->z_flags
|= ZSB_XATTR
;
149 if (newval
== ZFS_XATTR_SA
)
150 zsb
->z_xattr_sa
= B_TRUE
;
152 zsb
->z_xattr_sa
= B_FALSE
;
157 blksz_changed_cb(void *arg
, uint64_t newval
)
161 if (newval
< SPA_MINBLOCKSIZE
||
162 newval
> SPA_MAXBLOCKSIZE
|| !ISP2(newval
))
163 newval
= SPA_MAXBLOCKSIZE
;
165 zsb
->z_max_blksz
= newval
;
169 readonly_changed_cb(void *arg
, uint64_t newval
)
172 struct super_block
*sb
= zsb
->z_sb
;
178 sb
->s_flags
|= MS_RDONLY
;
180 sb
->s_flags
&= ~MS_RDONLY
;
184 devices_changed_cb(void *arg
, uint64_t newval
)
189 setuid_changed_cb(void *arg
, uint64_t newval
)
194 exec_changed_cb(void *arg
, uint64_t newval
)
199 nbmand_changed_cb(void *arg
, uint64_t newval
)
202 struct super_block
*sb
= zsb
->z_sb
;
208 sb
->s_flags
|= MS_MANDLOCK
;
210 sb
->s_flags
&= ~MS_MANDLOCK
;
214 snapdir_changed_cb(void *arg
, uint64_t newval
)
216 ((zfs_sb_t
*)arg
)->z_show_ctldir
= newval
;
220 vscan_changed_cb(void *arg
, uint64_t newval
)
222 ((zfs_sb_t
*)arg
)->z_vscan
= newval
;
226 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
228 ((zfs_sb_t
*)arg
)->z_acl_inherit
= newval
;
232 zfs_register_callbacks(zfs_sb_t
*zsb
)
234 struct dsl_dataset
*ds
= NULL
;
235 objset_t
*os
= zsb
->z_os
;
238 if (zfs_is_readonly(zsb
) || !spa_writeable(dmu_objset_spa(os
)))
239 readonly_changed_cb(zsb
, B_TRUE
);
242 * Register property callbacks.
244 * It would probably be fine to just check for i/o error from
245 * the first prop_register(), but I guess I like to go
248 ds
= dmu_objset_ds(os
);
249 error
= dsl_prop_register(ds
,
250 "atime", atime_changed_cb
, zsb
);
251 error
= error
? error
: dsl_prop_register(ds
,
252 "xattr", xattr_changed_cb
, zsb
);
253 error
= error
? error
: dsl_prop_register(ds
,
254 "recordsize", blksz_changed_cb
, zsb
);
255 error
= error
? error
: dsl_prop_register(ds
,
256 "readonly", readonly_changed_cb
, zsb
);
257 error
= error
? error
: dsl_prop_register(ds
,
258 "devices", devices_changed_cb
, zsb
);
259 error
= error
? error
: dsl_prop_register(ds
,
260 "setuid", setuid_changed_cb
, zsb
);
261 error
= error
? error
: dsl_prop_register(ds
,
262 "exec", exec_changed_cb
, zsb
);
263 error
= error
? error
: dsl_prop_register(ds
,
264 "snapdir", snapdir_changed_cb
, zsb
);
265 error
= error
? error
: dsl_prop_register(ds
,
266 "aclinherit", acl_inherit_changed_cb
, zsb
);
267 error
= error
? error
: dsl_prop_register(ds
,
268 "vscan", vscan_changed_cb
, zsb
);
269 error
= error
? error
: dsl_prop_register(ds
,
270 "nbmand", nbmand_changed_cb
, zsb
);
278 * We may attempt to unregister some callbacks that are not
279 * registered, but this is OK; it will simply return ENOMSG,
280 * which we will ignore.
282 (void) dsl_prop_unregister(ds
, "atime", atime_changed_cb
, zsb
);
283 (void) dsl_prop_unregister(ds
, "xattr", xattr_changed_cb
, zsb
);
284 (void) dsl_prop_unregister(ds
, "recordsize", blksz_changed_cb
, zsb
);
285 (void) dsl_prop_unregister(ds
, "readonly", readonly_changed_cb
, zsb
);
286 (void) dsl_prop_unregister(ds
, "devices", devices_changed_cb
, zsb
);
287 (void) dsl_prop_unregister(ds
, "setuid", setuid_changed_cb
, zsb
);
288 (void) dsl_prop_unregister(ds
, "exec", exec_changed_cb
, zsb
);
289 (void) dsl_prop_unregister(ds
, "snapdir", snapdir_changed_cb
, zsb
);
290 (void) dsl_prop_unregister(ds
, "aclinherit", acl_inherit_changed_cb
,
292 (void) dsl_prop_unregister(ds
, "vscan", vscan_changed_cb
, zsb
);
293 (void) dsl_prop_unregister(ds
, "nbmand", nbmand_changed_cb
, zsb
);
297 EXPORT_SYMBOL(zfs_register_callbacks
);
300 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
301 uint64_t *userp
, uint64_t *groupp
)
303 znode_phys_t
*znp
= data
;
307 * Is it a valid type of object to track?
309 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
313 * If we have a NULL data pointer
314 * then assume the id's aren't changing and
315 * return EEXIST to the dmu to let it know to
321 if (bonustype
== DMU_OT_ZNODE
) {
322 *userp
= znp
->zp_uid
;
323 *groupp
= znp
->zp_gid
;
327 ASSERT(bonustype
== DMU_OT_SA
);
328 hdrsize
= sa_hdrsize(data
);
331 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
333 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
337 * This should only happen for newly created
338 * files that haven't had the znode data filled
349 fuidstr_to_sid(zfs_sb_t
*zsb
, const char *fuidstr
,
350 char *domainbuf
, int buflen
, uid_t
*ridp
)
355 fuid
= strtonum(fuidstr
, NULL
);
357 domain
= zfs_fuid_find_by_idx(zsb
, FUID_INDEX(fuid
));
359 (void) strlcpy(domainbuf
, domain
, buflen
);
362 *ridp
= FUID_RID(fuid
);
366 zfs_userquota_prop_to_obj(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
)
369 case ZFS_PROP_USERUSED
:
370 return (DMU_USERUSED_OBJECT
);
371 case ZFS_PROP_GROUPUSED
:
372 return (DMU_GROUPUSED_OBJECT
);
373 case ZFS_PROP_USERQUOTA
:
374 return (zsb
->z_userquota_obj
);
375 case ZFS_PROP_GROUPQUOTA
:
376 return (zsb
->z_groupquota_obj
);
384 zfs_userspace_many(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
385 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
390 zfs_useracct_t
*buf
= vbuf
;
393 if (!dmu_objset_userspace_present(zsb
->z_os
))
396 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
402 for (zap_cursor_init_serialized(&zc
, zsb
->z_os
, obj
, *cookiep
);
403 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
404 zap_cursor_advance(&zc
)) {
405 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
409 fuidstr_to_sid(zsb
, za
.za_name
,
410 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
412 buf
->zu_space
= za
.za_first_integer
;
418 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
419 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
420 *cookiep
= zap_cursor_serialize(&zc
);
421 zap_cursor_fini(&zc
);
424 EXPORT_SYMBOL(zfs_userspace_many
);
427 * buf must be big enough (eg, 32 bytes)
430 id_to_fuidstr(zfs_sb_t
*zsb
, const char *domain
, uid_t rid
,
431 char *buf
, boolean_t addok
)
436 if (domain
&& domain
[0]) {
437 domainid
= zfs_fuid_find_by_domain(zsb
, domain
, NULL
, addok
);
441 fuid
= FUID_ENCODE(domainid
, rid
);
442 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
447 zfs_userspace_one(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
448 const char *domain
, uint64_t rid
, uint64_t *valp
)
456 if (!dmu_objset_userspace_present(zsb
->z_os
))
459 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
463 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_FALSE
);
467 err
= zap_lookup(zsb
->z_os
, obj
, buf
, 8, 1, valp
);
472 EXPORT_SYMBOL(zfs_userspace_one
);
475 zfs_set_userquota(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
476 const char *domain
, uint64_t rid
, uint64_t quota
)
482 boolean_t fuid_dirtied
;
484 if (type
!= ZFS_PROP_USERQUOTA
&& type
!= ZFS_PROP_GROUPQUOTA
)
487 if (zsb
->z_version
< ZPL_VERSION_USERSPACE
)
490 objp
= (type
== ZFS_PROP_USERQUOTA
) ? &zsb
->z_userquota_obj
:
491 &zsb
->z_groupquota_obj
;
493 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_TRUE
);
496 fuid_dirtied
= zsb
->z_fuid_dirty
;
498 tx
= dmu_tx_create(zsb
->z_os
);
499 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
501 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
502 zfs_userquota_prop_prefixes
[type
]);
505 zfs_fuid_txhold(zsb
, tx
);
506 err
= dmu_tx_assign(tx
, TXG_WAIT
);
512 mutex_enter(&zsb
->z_lock
);
514 *objp
= zap_create(zsb
->z_os
, DMU_OT_USERGROUP_QUOTA
,
516 VERIFY(0 == zap_add(zsb
->z_os
, MASTER_NODE_OBJ
,
517 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
519 mutex_exit(&zsb
->z_lock
);
522 err
= zap_remove(zsb
->z_os
, *objp
, buf
, tx
);
526 err
= zap_update(zsb
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
530 zfs_fuid_sync(zsb
, tx
);
534 EXPORT_SYMBOL(zfs_set_userquota
);
537 zfs_fuid_overquota(zfs_sb_t
*zsb
, boolean_t isgroup
, uint64_t fuid
)
540 uint64_t used
, quota
, usedobj
, quotaobj
;
543 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
544 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
546 if (quotaobj
== 0 || zsb
->z_replay
)
549 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
550 err
= zap_lookup(zsb
->z_os
, quotaobj
, buf
, 8, 1, "a
);
554 err
= zap_lookup(zsb
->z_os
, usedobj
, buf
, 8, 1, &used
);
557 return (used
>= quota
);
559 EXPORT_SYMBOL(zfs_fuid_overquota
);
562 zfs_owner_overquota(zfs_sb_t
*zsb
, znode_t
*zp
, boolean_t isgroup
)
567 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
569 fuid
= isgroup
? zp
->z_gid
: zp
->z_uid
;
571 if (quotaobj
== 0 || zsb
->z_replay
)
574 return (zfs_fuid_overquota(zsb
, isgroup
, fuid
));
576 EXPORT_SYMBOL(zfs_owner_overquota
);
579 zfs_sb_create(const char *osname
, zfs_sb_t
**zsbp
)
587 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
590 * We claim to always be readonly so we can open snapshots;
591 * other ZPL code will prevent us from writing to snapshots.
593 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zsb
, &os
);
595 kmem_free(zsb
, sizeof (zfs_sb_t
));
600 * Initialize the zfs-specific filesystem structure.
601 * Should probably make this a kmem cache, shuffle fields,
602 * and just bzero up to z_hold_mtx[].
606 zsb
->z_max_blksz
= SPA_MAXBLOCKSIZE
;
607 zsb
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
610 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zsb
->z_version
);
613 } else if (zsb
->z_version
>
614 zfs_zpl_version_map(spa_version(dmu_objset_spa(os
)))) {
615 (void) printk("Can't mount a version %lld file system "
616 "on a version %lld pool\n. Pool must be upgraded to mount "
617 "this file system.", (u_longlong_t
)zsb
->z_version
,
618 (u_longlong_t
)spa_version(dmu_objset_spa(os
)));
622 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &zval
)) != 0)
624 zsb
->z_norm
= (int)zval
;
626 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &zval
)) != 0)
628 zsb
->z_utf8
= (zval
!= 0);
630 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &zval
)) != 0)
632 zsb
->z_case
= (uint_t
)zval
;
635 * Fold case on file systems that are always or sometimes case
638 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
639 zsb
->z_case
== ZFS_CASE_MIXED
)
640 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
642 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
643 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
646 /* should either have both of these objects or none */
647 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
652 error
= zfs_get_zplprop(os
, ZFS_PROP_XATTR
, &zval
);
653 if ((error
== 0) && (zval
== ZFS_XATTR_SA
))
654 zsb
->z_xattr_sa
= B_TRUE
;
657 * Pre SA versions file systems should never touch
658 * either the attribute registration or layout objects.
663 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
668 if (zsb
->z_version
>= ZPL_VERSION_SA
)
669 sa_register_update_callback(os
, zfs_sa_upgrade
);
671 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
675 ASSERT(zsb
->z_root
!= 0);
677 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
678 &zsb
->z_unlinkedobj
);
682 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
683 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
684 8, 1, &zsb
->z_userquota_obj
);
685 if (error
&& error
!= ENOENT
)
688 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
689 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
690 8, 1, &zsb
->z_groupquota_obj
);
691 if (error
&& error
!= ENOENT
)
694 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
696 if (error
&& error
!= ENOENT
)
699 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
701 if (error
&& error
!= ENOENT
)
704 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
705 mutex_init(&zsb
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
706 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
707 offsetof(znode_t
, z_link_node
));
708 rrw_init(&zsb
->z_teardown_lock
);
709 rw_init(&zsb
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
710 rw_init(&zsb
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
711 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
712 mutex_init(&zsb
->z_hold_mtx
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
714 avl_create(&zsb
->z_ctldir_snaps
, snapentry_compare
,
715 sizeof (zfs_snapentry_t
), offsetof(zfs_snapentry_t
, se_node
));
716 mutex_init(&zsb
->z_ctldir_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
722 dmu_objset_disown(os
, zsb
);
724 kmem_free(zsb
, sizeof (zfs_sb_t
));
727 EXPORT_SYMBOL(zfs_sb_create
);
730 zfs_sb_setup(zfs_sb_t
*zsb
, boolean_t mounting
)
734 error
= zfs_register_callbacks(zsb
);
739 * Set the objset user_ptr to track its zsb.
741 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
742 dmu_objset_set_user(zsb
->z_os
, zsb
);
743 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
745 zsb
->z_log
= zil_open(zsb
->z_os
, zfs_get_data
);
748 * If we are not mounting (ie: online recv), then we don't
749 * have to worry about replaying the log as we blocked all
750 * operations out since we closed the ZIL.
756 * During replay we remove the read only flag to
757 * allow replays to succeed.
759 readonly
= zfs_is_readonly(zsb
);
761 readonly_changed_cb(zsb
, B_FALSE
);
763 zfs_unlinked_drain(zsb
);
766 * Parse and replay the intent log.
768 * Because of ziltest, this must be done after
769 * zfs_unlinked_drain(). (Further note: ziltest
770 * doesn't use readonly mounts, where
771 * zfs_unlinked_drain() isn't called.) This is because
772 * ziltest causes spa_sync() to think it's committed,
773 * but actually it is not, so the intent log contains
774 * many txg's worth of changes.
776 * In particular, if object N is in the unlinked set in
777 * the last txg to actually sync, then it could be
778 * actually freed in a later txg and then reallocated
779 * in a yet later txg. This would write a "create
780 * object N" record to the intent log. Normally, this
781 * would be fine because the spa_sync() would have
782 * written out the fact that object N is free, before
783 * we could write the "create object N" intent log
786 * But when we are in ziltest mode, we advance the "open
787 * txg" without actually spa_sync()-ing the changes to
788 * disk. So we would see that object N is still
789 * allocated and in the unlinked set, and there is an
790 * intent log record saying to allocate it.
792 if (spa_writeable(dmu_objset_spa(zsb
->z_os
))) {
793 if (zil_replay_disable
) {
794 zil_destroy(zsb
->z_log
, B_FALSE
);
796 zsb
->z_replay
= B_TRUE
;
797 zil_replay(zsb
->z_os
, zsb
,
799 zsb
->z_replay
= B_FALSE
;
803 /* restore readonly bit */
805 readonly_changed_cb(zsb
, B_TRUE
);
810 EXPORT_SYMBOL(zfs_sb_setup
);
813 zfs_sb_free(zfs_sb_t
*zsb
)
817 zfs_fuid_destroy(zsb
);
819 mutex_destroy(&zsb
->z_znodes_lock
);
820 mutex_destroy(&zsb
->z_lock
);
821 list_destroy(&zsb
->z_all_znodes
);
822 rrw_destroy(&zsb
->z_teardown_lock
);
823 rw_destroy(&zsb
->z_teardown_inactive_lock
);
824 rw_destroy(&zsb
->z_fuid_lock
);
825 for (i
= 0; i
!= ZFS_OBJ_MTX_SZ
; i
++)
826 mutex_destroy(&zsb
->z_hold_mtx
[i
]);
827 mutex_destroy(&zsb
->z_ctldir_lock
);
828 avl_destroy(&zsb
->z_ctldir_snaps
);
829 kmem_free(zsb
, sizeof (zfs_sb_t
));
831 EXPORT_SYMBOL(zfs_sb_free
);
834 zfs_set_fuid_feature(zfs_sb_t
*zsb
)
836 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
837 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
841 zfs_unregister_callbacks(zfs_sb_t
*zsb
)
843 objset_t
*os
= zsb
->z_os
;
844 struct dsl_dataset
*ds
;
847 * Unregister properties.
849 if (!dmu_objset_is_snapshot(os
)) {
850 ds
= dmu_objset_ds(os
);
851 VERIFY(dsl_prop_unregister(ds
, "atime", atime_changed_cb
,
854 VERIFY(dsl_prop_unregister(ds
, "xattr", xattr_changed_cb
,
857 VERIFY(dsl_prop_unregister(ds
, "recordsize", blksz_changed_cb
,
860 VERIFY(dsl_prop_unregister(ds
, "readonly", readonly_changed_cb
,
863 VERIFY(dsl_prop_unregister(ds
, "devices", devices_changed_cb
,
866 VERIFY(dsl_prop_unregister(ds
, "setuid", setuid_changed_cb
,
869 VERIFY(dsl_prop_unregister(ds
, "exec", exec_changed_cb
,
872 VERIFY(dsl_prop_unregister(ds
, "snapdir", snapdir_changed_cb
,
875 VERIFY(dsl_prop_unregister(ds
, "aclinherit",
876 acl_inherit_changed_cb
, zsb
) == 0);
878 VERIFY(dsl_prop_unregister(ds
, "vscan",
879 vscan_changed_cb
, zsb
) == 0);
881 VERIFY(dsl_prop_unregister(ds
, "nbmand",
882 nbmand_changed_cb
, zsb
) == 0);
885 EXPORT_SYMBOL(zfs_unregister_callbacks
);
889 * zfs_check_global_label:
890 * Check that the hex label string is appropriate for the dataset
891 * being mounted into the global_zone proper.
893 * Return an error if the hex label string is not default or
894 * admin_low/admin_high. For admin_low labels, the corresponding
895 * dataset must be readonly.
898 zfs_check_global_label(const char *dsname
, const char *hexsl
)
900 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
902 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
904 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
905 /* must be readonly */
908 if (dsl_prop_get_integer(dsname
,
909 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
911 return (rdonly
? 0 : EACCES
);
915 EXPORT_SYMBOL(zfs_check_global_label
);
916 #endif /* HAVE_MLSLABEL */
919 zfs_statvfs(struct dentry
*dentry
, struct kstatfs
*statp
)
921 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
922 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
927 dmu_objset_space(zsb
->z_os
,
928 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
931 * The underlying storage pool actually uses multiple block
932 * size. Under Solaris frsize (fragment size) is reported as
933 * the smallest block size we support, and bsize (block size)
934 * as the filesystem's maximum block size. Unfortunately,
935 * under Linux the fragment size and block size are often used
936 * interchangeably. Thus we are forced to report both of them
937 * as the filesystem's maximum block size.
939 statp
->f_frsize
= zsb
->z_max_blksz
;
940 statp
->f_bsize
= zsb
->z_max_blksz
;
941 bshift
= fls(statp
->f_bsize
) - 1;
944 * The following report "total" blocks of various kinds in
945 * the file system, but reported in terms of f_bsize - the
949 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
950 statp
->f_bfree
= availbytes
>> bshift
;
951 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
954 * statvfs() should really be called statufs(), because it assumes
955 * static metadata. ZFS doesn't preallocate files, so the best
956 * we can do is report the max that could possibly fit in f_files,
957 * and that minus the number actually used in f_ffree.
958 * For f_ffree, report the smaller of the number of object available
959 * and the number of blocks (each object will take at least a block).
961 statp
->f_ffree
= MIN(availobjs
, availbytes
>> DNODE_SHIFT
);
962 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
963 statp
->f_fsid
.val
[0] = dentry
->d_sb
->s_dev
;
964 statp
->f_fsid
.val
[1] = 0;
965 statp
->f_type
= ZFS_SUPER_MAGIC
;
966 statp
->f_namelen
= ZFS_MAXNAMELEN
;
969 * We have all of 40 characters to stuff a string here.
970 * Is there anything useful we could/should provide?
972 bzero(statp
->f_spare
, sizeof (statp
->f_spare
));
977 EXPORT_SYMBOL(zfs_statvfs
);
980 zfs_root(zfs_sb_t
*zsb
, struct inode
**ipp
)
987 error
= zfs_zget(zsb
, zsb
->z_root
, &rootzp
);
994 EXPORT_SYMBOL(zfs_root
);
998 zfs_sb_prune(struct super_block
*sb
, unsigned long nr_to_scan
, int *objects
)
1000 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1001 struct shrinker
*shrinker
= &sb
->s_shrink
;
1002 struct shrink_control sc
= {
1003 .nr_to_scan
= nr_to_scan
,
1004 .gfp_mask
= GFP_KERNEL
,
1008 *objects
= (*shrinker
->shrink
)(shrinker
, &sc
);
1013 EXPORT_SYMBOL(zfs_sb_prune
);
1014 #endif /* HAVE_SHRINK */
1017 * Teardown the zfs_sb_t::z_os.
1019 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1020 * and 'z_teardown_inactive_lock' held.
1023 zfs_sb_teardown(zfs_sb_t
*zsb
, boolean_t unmounting
)
1027 rrw_enter(&zsb
->z_teardown_lock
, RW_WRITER
, FTAG
);
1031 * We purge the parent filesystem's super block as the
1032 * parent filesystem and all of its snapshots have their
1033 * inode's super block set to the parent's filesystem's
1034 * super block. Note, 'z_parent' is self referential
1035 * for non-snapshots.
1037 shrink_dcache_sb(zsb
->z_parent
->z_sb
);
1038 (void) spl_invalidate_inodes(zsb
->z_parent
->z_sb
, 0);
1042 * Drain the iput_taskq to ensure all active references to the
1043 * zfs_sb_t have been handled only then can it be safely destroyed.
1045 taskq_wait(dsl_pool_iput_taskq(dmu_objset_pool(zsb
->z_os
)));
1048 * Close the zil. NB: Can't close the zil while zfs_inactive
1049 * threads are blocked as zil_close can call zfs_inactive.
1052 zil_close(zsb
->z_log
);
1056 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_WRITER
);
1059 * If we are not unmounting (ie: online recv) and someone already
1060 * unmounted this file system while we were doing the switcheroo,
1061 * or a reopen of z_os failed then just bail out now.
1063 if (!unmounting
&& (zsb
->z_unmounted
|| zsb
->z_os
== NULL
)) {
1064 rw_exit(&zsb
->z_teardown_inactive_lock
);
1065 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1070 * At this point there are no vops active, and any new vops will
1071 * fail with EIO since we have z_teardown_lock for writer (only
1072 * relavent for forced unmount).
1074 * Release all holds on dbufs.
1076 mutex_enter(&zsb
->z_znodes_lock
);
1077 for (zp
= list_head(&zsb
->z_all_znodes
); zp
!= NULL
;
1078 zp
= list_next(&zsb
->z_all_znodes
, zp
))
1080 ASSERT(atomic_read(&ZTOI(zp
)->i_count
) > 0);
1081 zfs_znode_dmu_fini(zp
);
1083 mutex_exit(&zsb
->z_znodes_lock
);
1086 * If we are unmounting, set the unmounted flag and let new vops
1087 * unblock. zfs_inactive will have the unmounted behavior, and all
1088 * other vops will fail with EIO.
1091 zsb
->z_unmounted
= B_TRUE
;
1092 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1093 rw_exit(&zsb
->z_teardown_inactive_lock
);
1097 * z_os will be NULL if there was an error in attempting to reopen
1098 * zsb, so just return as the properties had already been
1100 * unregistered and cached data had been evicted before.
1102 if (zsb
->z_os
== NULL
)
1106 * Unregister properties.
1108 zfs_unregister_callbacks(zsb
);
1113 if (dmu_objset_is_dirty_anywhere(zsb
->z_os
))
1114 if (!zfs_is_readonly(zsb
))
1115 txg_wait_synced(dmu_objset_pool(zsb
->z_os
), 0);
1116 (void) dmu_objset_evict_dbufs(zsb
->z_os
);
1120 EXPORT_SYMBOL(zfs_sb_teardown
);
1122 #if defined(HAVE_BDI) && !defined(HAVE_BDI_SETUP_AND_REGISTER)
1123 atomic_long_t zfs_bdi_seq
= ATOMIC_LONG_INIT(0);
1124 #endif /* HAVE_BDI && !HAVE_BDI_SETUP_AND_REGISTER */
1127 zfs_domount(struct super_block
*sb
, void *data
, int silent
)
1129 zpl_mount_data_t
*zmd
= data
;
1130 const char *osname
= zmd
->z_osname
;
1132 struct inode
*root_inode
;
1133 uint64_t recordsize
;
1136 error
= zfs_sb_create(osname
, &zsb
);
1140 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1141 &recordsize
, NULL
)))
1145 sb
->s_fs_info
= zsb
;
1146 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1147 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1148 sb
->s_time_gran
= 1;
1149 sb
->s_blocksize
= recordsize
;
1150 sb
->s_blocksize_bits
= ilog2(recordsize
);
1154 * 2.6.32 API change,
1155 * Added backing_device_info (BDI) per super block interfaces. A BDI
1156 * must be configured when using a non-device backed filesystem for
1157 * proper writeback. This is not required for older pdflush kernels.
1159 * NOTE: Linux read-ahead is disabled in favor of zfs read-ahead.
1161 zsb
->z_bdi
.ra_pages
= 0;
1162 sb
->s_bdi
= &zsb
->z_bdi
;
1164 error
= -bdi_setup_and_register(&zsb
->z_bdi
, "zfs", BDI_CAP_MAP_COPY
);
1167 #endif /* HAVE_BDI */
1169 /* Set callback operations for the file system. */
1170 sb
->s_op
= &zpl_super_operations
;
1171 sb
->s_xattr
= zpl_xattr_handlers
;
1172 sb
->s_export_op
= &zpl_export_operations
;
1174 /* Set features for file system. */
1175 zfs_set_fuid_feature(zsb
);
1177 if (dmu_objset_is_snapshot(zsb
->z_os
)) {
1180 atime_changed_cb(zsb
, B_FALSE
);
1181 readonly_changed_cb(zsb
, B_TRUE
);
1182 if ((error
= dsl_prop_get_integer(osname
,"xattr",&pval
,NULL
)))
1184 xattr_changed_cb(zsb
, pval
);
1185 zsb
->z_issnap
= B_TRUE
;
1186 zsb
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1188 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
1189 dmu_objset_set_user(zsb
->z_os
, zsb
);
1190 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
1192 error
= zfs_sb_setup(zsb
, B_TRUE
);
1195 /* Allocate a root inode for the filesystem. */
1196 error
= zfs_root(zsb
, &root_inode
);
1198 (void) zfs_umount(sb
);
1202 /* Allocate a root dentry for the filesystem */
1203 sb
->s_root
= d_alloc_root(root_inode
);
1204 if (sb
->s_root
== NULL
) {
1205 (void) zfs_umount(sb
);
1214 dmu_objset_disown(zsb
->z_os
, zsb
);
1220 EXPORT_SYMBOL(zfs_domount
);
1223 * Called when an unmount is requested and certain sanity checks have
1224 * already passed. At this point no dentries or inodes have been reclaimed
1225 * from their respective caches. We drop the extra reference on the .zfs
1226 * control directory to allow everything to be reclaimed. All snapshots
1227 * must already have been unmounted to reach this point.
1230 zfs_preumount(struct super_block
*sb
)
1232 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1234 if (zsb
->z_ctldir
!= NULL
)
1235 zfsctl_destroy(zsb
);
1237 EXPORT_SYMBOL(zfs_preumount
);
1240 * Called once all other unmount released tear down has occurred.
1241 * It is our responsibility to release any remaining infrastructure.
1245 zfs_umount(struct super_block
*sb
)
1247 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1250 VERIFY(zfs_sb_teardown(zsb
, B_TRUE
) == 0);
1254 bdi_destroy(sb
->s_bdi
);
1255 #endif /* HAVE_BDI */
1258 * z_os will be NULL if there was an error in
1259 * attempting to reopen zsb.
1263 * Unset the objset user_ptr.
1265 mutex_enter(&os
->os_user_ptr_lock
);
1266 dmu_objset_set_user(os
, NULL
);
1267 mutex_exit(&os
->os_user_ptr_lock
);
1270 * Finally release the objset
1272 dmu_objset_disown(os
, zsb
);
1278 EXPORT_SYMBOL(zfs_umount
);
1281 zfs_remount(struct super_block
*sb
, int *flags
, char *data
)
1284 * All namespace flags (MNT_*) and super block flags (MS_*) will
1285 * be handled by the Linux VFS. Only handle custom options here.
1289 EXPORT_SYMBOL(zfs_remount
);
1292 zfs_vget(struct super_block
*sb
, struct inode
**ipp
, fid_t
*fidp
)
1294 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1296 uint64_t object
= 0;
1297 uint64_t fid_gen
= 0;
1306 if (fidp
->fid_len
== LONG_FID_LEN
) {
1307 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1308 uint64_t objsetid
= 0;
1309 uint64_t setgen
= 0;
1311 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1312 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1314 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1315 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1319 err
= zfsctl_lookup_objset(sb
, objsetid
, &zsb
);
1326 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1327 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1329 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1330 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1332 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1333 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1339 /* A zero fid_gen means we are in the .zfs control directories */
1341 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1342 *ipp
= zsb
->z_ctldir
;
1343 ASSERT(*ipp
!= NULL
);
1344 if (object
== ZFSCTL_INO_SNAPDIR
) {
1345 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
,
1346 0, kcred
, NULL
, NULL
) == 0);
1354 gen_mask
= -1ULL >> (64 - 8 * i
);
1356 dprintf("getting %llu [%u mask %llx]\n", object
, fid_gen
, gen_mask
);
1357 if ((err
= zfs_zget(zsb
, object
, &zp
))) {
1361 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
), &zp_gen
,
1363 zp_gen
= zp_gen
& gen_mask
;
1366 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1367 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen
, fid_gen
);
1375 zfs_inode_update(ITOZ(*ipp
));
1380 EXPORT_SYMBOL(zfs_vget
);
1383 * Block out VOPs and close zfs_sb_t::z_os
1385 * Note, if successful, then we return with the 'z_teardown_lock' and
1386 * 'z_teardown_inactive_lock' write held.
1389 zfs_suspend_fs(zfs_sb_t
*zsb
)
1393 if ((error
= zfs_sb_teardown(zsb
, B_FALSE
)) != 0)
1395 dmu_objset_disown(zsb
->z_os
, zsb
);
1399 EXPORT_SYMBOL(zfs_suspend_fs
);
1402 * Reopen zfs_sb_t::z_os and release VOPs.
1405 zfs_resume_fs(zfs_sb_t
*zsb
, const char *osname
)
1409 ASSERT(RRW_WRITE_HELD(&zsb
->z_teardown_lock
));
1410 ASSERT(RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
));
1412 err
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_FALSE
, zsb
, &zsb
->z_os
);
1417 uint64_t sa_obj
= 0;
1419 err2
= zap_lookup(zsb
->z_os
, MASTER_NODE_OBJ
,
1420 ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1422 if ((err
|| err2
) && zsb
->z_version
>= ZPL_VERSION_SA
)
1426 if ((err
= sa_setup(zsb
->z_os
, sa_obj
,
1427 zfs_attr_table
, ZPL_END
, &zsb
->z_attr_table
)) != 0)
1430 VERIFY(zfs_sb_setup(zsb
, B_FALSE
) == 0);
1433 * Attempt to re-establish all the active znodes with
1434 * their dbufs. If a zfs_rezget() fails, then we'll let
1435 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
1436 * when they try to use their znode.
1438 mutex_enter(&zsb
->z_znodes_lock
);
1439 for (zp
= list_head(&zsb
->z_all_znodes
); zp
;
1440 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1441 (void) zfs_rezget(zp
);
1443 mutex_exit(&zsb
->z_znodes_lock
);
1448 /* release the VOPs */
1449 rw_exit(&zsb
->z_teardown_inactive_lock
);
1450 rrw_exit(&zsb
->z_teardown_lock
, FTAG
);
1454 * Since we couldn't reopen zfs_sb_t::z_os, force
1455 * unmount this file system.
1457 (void) zfs_umount(zsb
->z_sb
);
1461 EXPORT_SYMBOL(zfs_resume_fs
);
1464 zfs_set_version(zfs_sb_t
*zsb
, uint64_t newvers
)
1467 objset_t
*os
= zsb
->z_os
;
1470 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1473 if (newvers
< zsb
->z_version
)
1476 if (zfs_spa_version_map(newvers
) >
1477 spa_version(dmu_objset_spa(zsb
->z_os
)))
1480 tx
= dmu_tx_create(os
);
1481 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1482 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1483 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1485 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1487 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1493 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1494 8, 1, &newvers
, tx
);
1501 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1504 ASSERT3U(spa_version(dmu_objset_spa(zsb
->z_os
)), >=,
1506 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1507 DMU_OT_NONE
, 0, tx
);
1509 error
= zap_add(os
, MASTER_NODE_OBJ
,
1510 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1511 ASSERT3U(error
, ==, 0);
1513 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
1514 sa_register_update_callback(os
, zfs_sa_upgrade
);
1517 spa_history_log_internal(LOG_DS_UPGRADE
,
1518 dmu_objset_spa(os
), tx
, "oldver=%llu newver=%llu dataset = %llu",
1519 zsb
->z_version
, newvers
, dmu_objset_id(os
));
1523 zsb
->z_version
= newvers
;
1525 if (zsb
->z_version
>= ZPL_VERSION_FUID
)
1526 zfs_set_fuid_feature(zsb
);
1530 EXPORT_SYMBOL(zfs_set_version
);
1533 * Read a property stored within the master node.
1536 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
1542 * Look up the file system's value for the property. For the
1543 * version property, we look up a slightly different string.
1545 if (prop
== ZFS_PROP_VERSION
)
1546 pname
= ZPL_VERSION_STR
;
1548 pname
= zfs_prop_to_name(prop
);
1551 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
1553 if (error
== ENOENT
) {
1554 /* No value set, use the default value */
1556 case ZFS_PROP_VERSION
:
1557 *value
= ZPL_VERSION
;
1559 case ZFS_PROP_NORMALIZE
:
1560 case ZFS_PROP_UTF8ONLY
:
1564 *value
= ZFS_CASE_SENSITIVE
;
1573 EXPORT_SYMBOL(zfs_get_zplprop
);
1580 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
1581 register_filesystem(&zpl_fs_type
);
1582 (void) arc_add_prune_callback(zpl_prune_sbs
, NULL
);
1588 unregister_filesystem(&zpl_fs_type
);