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) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2010 Robert Milkowski */
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
33 #include <sys/pathname.h>
34 #include <sys/vnode.h>
36 #include <sys/vfs_opreg.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include "fs/fs_subr.h"
41 #include <sys/zfs_znode.h>
42 #include <sys/zfs_vnops.h>
43 #include <sys/zfs_dir.h>
45 #include <sys/fs/zfs.h>
47 #include <sys/dsl_prop.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_deleg.h>
53 #include <sys/sa_impl.h>
54 #include <sys/varargs.h>
55 #include <sys/policy.h>
56 #include <sys/atomic.h>
57 #include <sys/mkdev.h>
58 #include <sys/modctl.h>
59 #include <sys/refstr.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_fuid.h>
63 #include <sys/bootconf.h>
64 #include <sys/sunddi.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/spa_boot.h>
69 #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 relatime_changed_cb(void *arg
, uint64_t newval
)
142 ((zfs_sb_t
*)arg
)->z_relatime
= newval
;
146 xattr_changed_cb(void *arg
, uint64_t newval
)
150 if (newval
== ZFS_XATTR_OFF
) {
151 zsb
->z_flags
&= ~ZSB_XATTR
;
153 zsb
->z_flags
|= ZSB_XATTR
;
155 if (newval
== ZFS_XATTR_SA
)
156 zsb
->z_xattr_sa
= B_TRUE
;
158 zsb
->z_xattr_sa
= B_FALSE
;
163 acltype_changed_cb(void *arg
, uint64_t newval
)
168 case ZFS_ACLTYPE_OFF
:
169 zsb
->z_acl_type
= ZFS_ACLTYPE_OFF
;
170 zsb
->z_sb
->s_flags
&= ~MS_POSIXACL
;
172 case ZFS_ACLTYPE_POSIXACL
:
173 #ifdef CONFIG_FS_POSIX_ACL
174 zsb
->z_acl_type
= ZFS_ACLTYPE_POSIXACL
;
175 zsb
->z_sb
->s_flags
|= MS_POSIXACL
;
177 zsb
->z_acl_type
= ZFS_ACLTYPE_OFF
;
178 zsb
->z_sb
->s_flags
&= ~MS_POSIXACL
;
179 #endif /* CONFIG_FS_POSIX_ACL */
187 blksz_changed_cb(void *arg
, uint64_t newval
)
190 ASSERT3U(newval
, <=, spa_maxblocksize(dmu_objset_spa(zsb
->z_os
)));
191 ASSERT3U(newval
, >=, SPA_MINBLOCKSIZE
);
192 ASSERT(ISP2(newval
));
194 zsb
->z_max_blksz
= newval
;
198 readonly_changed_cb(void *arg
, uint64_t newval
)
201 struct super_block
*sb
= zsb
->z_sb
;
207 sb
->s_flags
|= MS_RDONLY
;
209 sb
->s_flags
&= ~MS_RDONLY
;
213 devices_changed_cb(void *arg
, uint64_t newval
)
218 setuid_changed_cb(void *arg
, uint64_t newval
)
223 exec_changed_cb(void *arg
, uint64_t newval
)
228 nbmand_changed_cb(void *arg
, uint64_t newval
)
231 struct super_block
*sb
= zsb
->z_sb
;
237 sb
->s_flags
|= MS_MANDLOCK
;
239 sb
->s_flags
&= ~MS_MANDLOCK
;
243 snapdir_changed_cb(void *arg
, uint64_t newval
)
245 ((zfs_sb_t
*)arg
)->z_show_ctldir
= newval
;
249 vscan_changed_cb(void *arg
, uint64_t newval
)
251 ((zfs_sb_t
*)arg
)->z_vscan
= newval
;
255 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
257 ((zfs_sb_t
*)arg
)->z_acl_inherit
= newval
;
261 zfs_register_callbacks(zfs_sb_t
*zsb
)
263 struct dsl_dataset
*ds
= NULL
;
264 objset_t
*os
= zsb
->z_os
;
265 zfs_mntopts_t
*zmo
= zsb
->z_mntopts
;
272 * The act of registering our callbacks will destroy any mount
273 * options we may have. In order to enable temporary overrides
274 * of mount options, we stash away the current values and
275 * restore them after we register the callbacks.
277 if (zfs_is_readonly(zsb
) || !spa_writeable(dmu_objset_spa(os
))) {
278 zmo
->z_do_readonly
= B_TRUE
;
279 zmo
->z_readonly
= B_TRUE
;
283 * Register property callbacks.
285 * It would probably be fine to just check for i/o error from
286 * the first prop_register(), but I guess I like to go
289 ds
= dmu_objset_ds(os
);
290 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
291 error
= dsl_prop_register(ds
,
292 zfs_prop_to_name(ZFS_PROP_ATIME
), atime_changed_cb
, zsb
);
293 error
= error
? error
: dsl_prop_register(ds
,
294 zfs_prop_to_name(ZFS_PROP_RELATIME
), relatime_changed_cb
, zsb
);
295 error
= error
? error
: dsl_prop_register(ds
,
296 zfs_prop_to_name(ZFS_PROP_XATTR
), xattr_changed_cb
, zsb
);
297 error
= error
? error
: dsl_prop_register(ds
,
298 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), blksz_changed_cb
, zsb
);
299 error
= error
? error
: dsl_prop_register(ds
,
300 zfs_prop_to_name(ZFS_PROP_READONLY
), readonly_changed_cb
, zsb
);
301 error
= error
? error
: dsl_prop_register(ds
,
302 zfs_prop_to_name(ZFS_PROP_DEVICES
), devices_changed_cb
, zsb
);
303 error
= error
? error
: dsl_prop_register(ds
,
304 zfs_prop_to_name(ZFS_PROP_SETUID
), setuid_changed_cb
, zsb
);
305 error
= error
? error
: dsl_prop_register(ds
,
306 zfs_prop_to_name(ZFS_PROP_EXEC
), exec_changed_cb
, zsb
);
307 error
= error
? error
: dsl_prop_register(ds
,
308 zfs_prop_to_name(ZFS_PROP_SNAPDIR
), snapdir_changed_cb
, zsb
);
309 error
= error
? error
: dsl_prop_register(ds
,
310 zfs_prop_to_name(ZFS_PROP_ACLTYPE
), acltype_changed_cb
, zsb
);
311 error
= error
? error
: dsl_prop_register(ds
,
312 zfs_prop_to_name(ZFS_PROP_ACLINHERIT
), acl_inherit_changed_cb
, zsb
);
313 error
= error
? error
: dsl_prop_register(ds
,
314 zfs_prop_to_name(ZFS_PROP_VSCAN
), vscan_changed_cb
, zsb
);
315 error
= error
? error
: dsl_prop_register(ds
,
316 zfs_prop_to_name(ZFS_PROP_NBMAND
), nbmand_changed_cb
, zsb
);
317 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
322 * Invoke our callbacks to restore temporary mount options.
324 if (zmo
->z_do_readonly
)
325 readonly_changed_cb(zsb
, zmo
->z_readonly
);
326 if (zmo
->z_do_setuid
)
327 setuid_changed_cb(zsb
, zmo
->z_setuid
);
329 exec_changed_cb(zsb
, zmo
->z_exec
);
330 if (zmo
->z_do_devices
)
331 devices_changed_cb(zsb
, zmo
->z_devices
);
333 xattr_changed_cb(zsb
, zmo
->z_xattr
);
335 atime_changed_cb(zsb
, zmo
->z_atime
);
336 if (zmo
->z_do_relatime
)
337 relatime_changed_cb(zsb
, zmo
->z_relatime
);
338 if (zmo
->z_do_nbmand
)
339 nbmand_changed_cb(zsb
, zmo
->z_nbmand
);
345 * We may attempt to unregister some callbacks that are not
346 * registered, but this is OK; it will simply return ENOMSG,
347 * which we will ignore.
349 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_ATIME
),
350 atime_changed_cb
, zsb
);
351 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_RELATIME
),
352 relatime_changed_cb
, zsb
);
353 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_XATTR
),
354 xattr_changed_cb
, zsb
);
355 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
356 blksz_changed_cb
, zsb
);
357 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_READONLY
),
358 readonly_changed_cb
, zsb
);
359 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_DEVICES
),
360 devices_changed_cb
, zsb
);
361 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_SETUID
),
362 setuid_changed_cb
, zsb
);
363 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_EXEC
),
364 exec_changed_cb
, zsb
);
365 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_SNAPDIR
),
366 snapdir_changed_cb
, zsb
);
367 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_ACLTYPE
),
368 acltype_changed_cb
, zsb
);
369 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_ACLINHERIT
),
370 acl_inherit_changed_cb
, zsb
);
371 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_VSCAN
),
372 vscan_changed_cb
, zsb
);
373 (void) dsl_prop_unregister(ds
, zfs_prop_to_name(ZFS_PROP_NBMAND
),
374 nbmand_changed_cb
, zsb
);
378 EXPORT_SYMBOL(zfs_register_callbacks
);
381 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
382 uint64_t *userp
, uint64_t *groupp
)
385 * Is it a valid type of object to track?
387 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
388 return (SET_ERROR(ENOENT
));
391 * If we have a NULL data pointer
392 * then assume the id's aren't changing and
393 * return EEXIST to the dmu to let it know to
397 return (SET_ERROR(EEXIST
));
399 if (bonustype
== DMU_OT_ZNODE
) {
400 znode_phys_t
*znp
= data
;
401 *userp
= znp
->zp_uid
;
402 *groupp
= znp
->zp_gid
;
405 sa_hdr_phys_t
*sap
= data
;
406 sa_hdr_phys_t sa
= *sap
;
407 boolean_t swap
= B_FALSE
;
409 ASSERT(bonustype
== DMU_OT_SA
);
411 if (sa
.sa_magic
== 0) {
413 * This should only happen for newly created
414 * files that haven't had the znode data filled
421 if (sa
.sa_magic
== BSWAP_32(SA_MAGIC
)) {
422 sa
.sa_magic
= SA_MAGIC
;
423 sa
.sa_layout_info
= BSWAP_16(sa
.sa_layout_info
);
426 VERIFY3U(sa
.sa_magic
, ==, SA_MAGIC
);
429 hdrsize
= sa_hdrsize(&sa
);
430 VERIFY3U(hdrsize
, >=, sizeof (sa_hdr_phys_t
));
431 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
433 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
436 *userp
= BSWAP_64(*userp
);
437 *groupp
= BSWAP_64(*groupp
);
444 fuidstr_to_sid(zfs_sb_t
*zsb
, const char *fuidstr
,
445 char *domainbuf
, int buflen
, uid_t
*ridp
)
450 fuid
= strtonum(fuidstr
, NULL
);
452 domain
= zfs_fuid_find_by_idx(zsb
, FUID_INDEX(fuid
));
454 (void) strlcpy(domainbuf
, domain
, buflen
);
457 *ridp
= FUID_RID(fuid
);
461 zfs_userquota_prop_to_obj(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
)
464 case ZFS_PROP_USERUSED
:
465 return (DMU_USERUSED_OBJECT
);
466 case ZFS_PROP_GROUPUSED
:
467 return (DMU_GROUPUSED_OBJECT
);
468 case ZFS_PROP_USERQUOTA
:
469 return (zsb
->z_userquota_obj
);
470 case ZFS_PROP_GROUPQUOTA
:
471 return (zsb
->z_groupquota_obj
);
473 return (SET_ERROR(ENOTSUP
));
479 zfs_userspace_many(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
480 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
485 zfs_useracct_t
*buf
= vbuf
;
488 if (!dmu_objset_userspace_present(zsb
->z_os
))
489 return (SET_ERROR(ENOTSUP
));
491 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
497 for (zap_cursor_init_serialized(&zc
, zsb
->z_os
, obj
, *cookiep
);
498 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
499 zap_cursor_advance(&zc
)) {
500 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
504 fuidstr_to_sid(zsb
, za
.za_name
,
505 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
507 buf
->zu_space
= za
.za_first_integer
;
513 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
514 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
515 *cookiep
= zap_cursor_serialize(&zc
);
516 zap_cursor_fini(&zc
);
519 EXPORT_SYMBOL(zfs_userspace_many
);
522 * buf must be big enough (eg, 32 bytes)
525 id_to_fuidstr(zfs_sb_t
*zsb
, const char *domain
, uid_t rid
,
526 char *buf
, boolean_t addok
)
531 if (domain
&& domain
[0]) {
532 domainid
= zfs_fuid_find_by_domain(zsb
, domain
, NULL
, addok
);
534 return (SET_ERROR(ENOENT
));
536 fuid
= FUID_ENCODE(domainid
, rid
);
537 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
542 zfs_userspace_one(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
543 const char *domain
, uint64_t rid
, uint64_t *valp
)
551 if (!dmu_objset_userspace_present(zsb
->z_os
))
552 return (SET_ERROR(ENOTSUP
));
554 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
558 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_FALSE
);
562 err
= zap_lookup(zsb
->z_os
, obj
, buf
, 8, 1, valp
);
567 EXPORT_SYMBOL(zfs_userspace_one
);
570 zfs_set_userquota(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
571 const char *domain
, uint64_t rid
, uint64_t quota
)
577 boolean_t fuid_dirtied
;
579 if (type
!= ZFS_PROP_USERQUOTA
&& type
!= ZFS_PROP_GROUPQUOTA
)
580 return (SET_ERROR(EINVAL
));
582 if (zsb
->z_version
< ZPL_VERSION_USERSPACE
)
583 return (SET_ERROR(ENOTSUP
));
585 objp
= (type
== ZFS_PROP_USERQUOTA
) ? &zsb
->z_userquota_obj
:
586 &zsb
->z_groupquota_obj
;
588 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_TRUE
);
591 fuid_dirtied
= zsb
->z_fuid_dirty
;
593 tx
= dmu_tx_create(zsb
->z_os
);
594 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
596 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
597 zfs_userquota_prop_prefixes
[type
]);
600 zfs_fuid_txhold(zsb
, tx
);
601 err
= dmu_tx_assign(tx
, TXG_WAIT
);
607 mutex_enter(&zsb
->z_lock
);
609 *objp
= zap_create(zsb
->z_os
, DMU_OT_USERGROUP_QUOTA
,
611 VERIFY(0 == zap_add(zsb
->z_os
, MASTER_NODE_OBJ
,
612 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
614 mutex_exit(&zsb
->z_lock
);
617 err
= zap_remove(zsb
->z_os
, *objp
, buf
, tx
);
621 err
= zap_update(zsb
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
625 zfs_fuid_sync(zsb
, tx
);
629 EXPORT_SYMBOL(zfs_set_userquota
);
632 zfs_fuid_overquota(zfs_sb_t
*zsb
, boolean_t isgroup
, uint64_t fuid
)
635 uint64_t used
, quota
, usedobj
, quotaobj
;
638 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
639 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
641 if (quotaobj
== 0 || zsb
->z_replay
)
644 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
645 err
= zap_lookup(zsb
->z_os
, quotaobj
, buf
, 8, 1, "a
);
649 err
= zap_lookup(zsb
->z_os
, usedobj
, buf
, 8, 1, &used
);
652 return (used
>= quota
);
654 EXPORT_SYMBOL(zfs_fuid_overquota
);
657 zfs_owner_overquota(zfs_sb_t
*zsb
, znode_t
*zp
, boolean_t isgroup
)
662 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
664 fuid
= isgroup
? zp
->z_gid
: zp
->z_uid
;
666 if (quotaobj
== 0 || zsb
->z_replay
)
669 return (zfs_fuid_overquota(zsb
, isgroup
, fuid
));
671 EXPORT_SYMBOL(zfs_owner_overquota
);
674 zfs_mntopts_alloc(void)
676 return (kmem_zalloc(sizeof (zfs_mntopts_t
), KM_SLEEP
));
680 zfs_mntopts_free(zfs_mntopts_t
*zmo
)
683 strfree(zmo
->z_osname
);
686 strfree(zmo
->z_mntpoint
);
688 kmem_free(zmo
, sizeof (zfs_mntopts_t
));
692 zfs_sb_create(const char *osname
, zfs_mntopts_t
*zmo
, zfs_sb_t
**zsbp
)
700 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
703 * We claim to always be readonly so we can open snapshots;
704 * other ZPL code will prevent us from writing to snapshots.
706 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zsb
, &os
);
708 kmem_free(zsb
, sizeof (zfs_sb_t
));
713 * Optional temporary mount options, free'd in zfs_sb_free().
715 zsb
->z_mntopts
= (zmo
? zmo
: zfs_mntopts_alloc());
718 * Initialize the zfs-specific filesystem structure.
719 * Should probably make this a kmem cache, shuffle fields.
723 zsb
->z_max_blksz
= SPA_OLD_MAXBLOCKSIZE
;
724 zsb
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
727 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zsb
->z_version
);
730 } else if (zsb
->z_version
> ZPL_VERSION
) {
731 error
= SET_ERROR(ENOTSUP
);
734 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &zval
)) != 0)
736 zsb
->z_norm
= (int)zval
;
738 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &zval
)) != 0)
740 zsb
->z_utf8
= (zval
!= 0);
742 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &zval
)) != 0)
744 zsb
->z_case
= (uint_t
)zval
;
746 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_ACLTYPE
, &zval
)) != 0)
748 zsb
->z_acl_type
= (uint_t
)zval
;
751 * Fold case on file systems that are always or sometimes case
754 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
755 zsb
->z_case
== ZFS_CASE_MIXED
)
756 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
758 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
759 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
762 /* should either have both of these objects or none */
763 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
768 error
= zfs_get_zplprop(os
, ZFS_PROP_XATTR
, &zval
);
769 if ((error
== 0) && (zval
== ZFS_XATTR_SA
))
770 zsb
->z_xattr_sa
= B_TRUE
;
773 * Pre SA versions file systems should never touch
774 * either the attribute registration or layout objects.
779 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
784 if (zsb
->z_version
>= ZPL_VERSION_SA
)
785 sa_register_update_callback(os
, zfs_sa_upgrade
);
787 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
791 ASSERT(zsb
->z_root
!= 0);
793 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
794 &zsb
->z_unlinkedobj
);
798 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
799 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
800 8, 1, &zsb
->z_userquota_obj
);
801 if (error
&& error
!= ENOENT
)
804 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
805 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
806 8, 1, &zsb
->z_groupquota_obj
);
807 if (error
&& error
!= ENOENT
)
810 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
812 if (error
&& error
!= ENOENT
)
815 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
817 if (error
&& error
!= ENOENT
)
820 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
821 mutex_init(&zsb
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
822 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
823 offsetof(znode_t
, z_link_node
));
824 rrm_init(&zsb
->z_teardown_lock
, B_FALSE
);
825 rw_init(&zsb
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
826 rw_init(&zsb
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
828 size
= MIN(1 << (highbit64(zfs_object_mutex_size
)-1), ZFS_OBJ_MTX_MAX
);
829 zsb
->z_hold_size
= size
;
830 zsb
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
, KM_SLEEP
);
831 zsb
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
832 for (i
= 0; i
!= size
; i
++) {
833 avl_create(&zsb
->z_hold_trees
[i
], zfs_znode_hold_compare
,
834 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
835 mutex_init(&zsb
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
842 dmu_objset_disown(os
, zsb
);
845 kmem_free(zsb
, sizeof (zfs_sb_t
));
848 EXPORT_SYMBOL(zfs_sb_create
);
851 zfs_sb_setup(zfs_sb_t
*zsb
, boolean_t mounting
)
855 error
= zfs_register_callbacks(zsb
);
860 * Set the objset user_ptr to track its zsb.
862 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
863 dmu_objset_set_user(zsb
->z_os
, zsb
);
864 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
866 zsb
->z_log
= zil_open(zsb
->z_os
, zfs_get_data
);
869 * If we are not mounting (ie: online recv), then we don't
870 * have to worry about replaying the log as we blocked all
871 * operations out since we closed the ZIL.
877 * During replay we remove the read only flag to
878 * allow replays to succeed.
880 readonly
= zfs_is_readonly(zsb
);
882 readonly_changed_cb(zsb
, B_FALSE
);
884 zfs_unlinked_drain(zsb
);
887 * Parse and replay the intent log.
889 * Because of ziltest, this must be done after
890 * zfs_unlinked_drain(). (Further note: ziltest
891 * doesn't use readonly mounts, where
892 * zfs_unlinked_drain() isn't called.) This is because
893 * ziltest causes spa_sync() to think it's committed,
894 * but actually it is not, so the intent log contains
895 * many txg's worth of changes.
897 * In particular, if object N is in the unlinked set in
898 * the last txg to actually sync, then it could be
899 * actually freed in a later txg and then reallocated
900 * in a yet later txg. This would write a "create
901 * object N" record to the intent log. Normally, this
902 * would be fine because the spa_sync() would have
903 * written out the fact that object N is free, before
904 * we could write the "create object N" intent log
907 * But when we are in ziltest mode, we advance the "open
908 * txg" without actually spa_sync()-ing the changes to
909 * disk. So we would see that object N is still
910 * allocated and in the unlinked set, and there is an
911 * intent log record saying to allocate it.
913 if (spa_writeable(dmu_objset_spa(zsb
->z_os
))) {
914 if (zil_replay_disable
) {
915 zil_destroy(zsb
->z_log
, B_FALSE
);
917 zsb
->z_replay
= B_TRUE
;
918 zil_replay(zsb
->z_os
, zsb
,
920 zsb
->z_replay
= B_FALSE
;
924 /* restore readonly bit */
926 readonly_changed_cb(zsb
, B_TRUE
);
931 EXPORT_SYMBOL(zfs_sb_setup
);
934 zfs_sb_free(zfs_sb_t
*zsb
)
936 int i
, size
= zsb
->z_hold_size
;
938 zfs_fuid_destroy(zsb
);
940 mutex_destroy(&zsb
->z_znodes_lock
);
941 mutex_destroy(&zsb
->z_lock
);
942 list_destroy(&zsb
->z_all_znodes
);
943 rrm_destroy(&zsb
->z_teardown_lock
);
944 rw_destroy(&zsb
->z_teardown_inactive_lock
);
945 rw_destroy(&zsb
->z_fuid_lock
);
946 for (i
= 0; i
!= size
; i
++) {
947 avl_destroy(&zsb
->z_hold_trees
[i
]);
948 mutex_destroy(&zsb
->z_hold_locks
[i
]);
950 vmem_free(zsb
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
951 vmem_free(zsb
->z_hold_locks
, sizeof (kmutex_t
) * size
);
952 zfs_mntopts_free(zsb
->z_mntopts
);
953 kmem_free(zsb
, sizeof (zfs_sb_t
));
955 EXPORT_SYMBOL(zfs_sb_free
);
958 zfs_set_fuid_feature(zfs_sb_t
*zsb
)
960 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
961 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
965 zfs_unregister_callbacks(zfs_sb_t
*zsb
)
967 objset_t
*os
= zsb
->z_os
;
968 struct dsl_dataset
*ds
;
971 * Unregister properties.
973 if (!dmu_objset_is_snapshot(os
)) {
974 ds
= dmu_objset_ds(os
);
975 VERIFY(dsl_prop_unregister(ds
, "atime", atime_changed_cb
,
978 VERIFY(dsl_prop_unregister(ds
, "relatime", relatime_changed_cb
,
981 VERIFY(dsl_prop_unregister(ds
, "xattr", xattr_changed_cb
,
984 VERIFY(dsl_prop_unregister(ds
, "recordsize", blksz_changed_cb
,
987 VERIFY(dsl_prop_unregister(ds
, "readonly", readonly_changed_cb
,
990 VERIFY(dsl_prop_unregister(ds
, "devices", devices_changed_cb
,
993 VERIFY(dsl_prop_unregister(ds
, "setuid", setuid_changed_cb
,
996 VERIFY(dsl_prop_unregister(ds
, "exec", exec_changed_cb
,
999 VERIFY(dsl_prop_unregister(ds
, "snapdir", snapdir_changed_cb
,
1002 VERIFY(dsl_prop_unregister(ds
, "acltype", acltype_changed_cb
,
1005 VERIFY(dsl_prop_unregister(ds
, "aclinherit",
1006 acl_inherit_changed_cb
, zsb
) == 0);
1008 VERIFY(dsl_prop_unregister(ds
, "vscan",
1009 vscan_changed_cb
, zsb
) == 0);
1011 VERIFY(dsl_prop_unregister(ds
, "nbmand",
1012 nbmand_changed_cb
, zsb
) == 0);
1015 EXPORT_SYMBOL(zfs_unregister_callbacks
);
1017 #ifdef HAVE_MLSLABEL
1019 * Check that the hex label string is appropriate for the dataset being
1020 * mounted into the global_zone proper.
1022 * Return an error if the hex label string is not default or
1023 * admin_low/admin_high. For admin_low labels, the corresponding
1024 * dataset must be readonly.
1027 zfs_check_global_label(const char *dsname
, const char *hexsl
)
1029 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
1031 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
1033 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
1034 /* must be readonly */
1037 if (dsl_prop_get_integer(dsname
,
1038 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
1039 return (SET_ERROR(EACCES
));
1040 return (rdonly
? 0 : EACCES
);
1042 return (SET_ERROR(EACCES
));
1044 EXPORT_SYMBOL(zfs_check_global_label
);
1045 #endif /* HAVE_MLSLABEL */
1048 zfs_statvfs(struct dentry
*dentry
, struct kstatfs
*statp
)
1050 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
1051 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
1057 dmu_objset_space(zsb
->z_os
,
1058 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1060 fsid
= dmu_objset_fsid_guid(zsb
->z_os
);
1062 * The underlying storage pool actually uses multiple block
1063 * size. Under Solaris frsize (fragment size) is reported as
1064 * the smallest block size we support, and bsize (block size)
1065 * as the filesystem's maximum block size. Unfortunately,
1066 * under Linux the fragment size and block size are often used
1067 * interchangeably. Thus we are forced to report both of them
1068 * as the filesystem's maximum block size.
1070 statp
->f_frsize
= zsb
->z_max_blksz
;
1071 statp
->f_bsize
= zsb
->z_max_blksz
;
1072 bshift
= fls(statp
->f_bsize
) - 1;
1075 * The following report "total" blocks of various kinds in
1076 * the file system, but reported in terms of f_bsize - the
1080 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
1081 statp
->f_bfree
= availbytes
>> bshift
;
1082 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1085 * statvfs() should really be called statufs(), because it assumes
1086 * static metadata. ZFS doesn't preallocate files, so the best
1087 * we can do is report the max that could possibly fit in f_files,
1088 * and that minus the number actually used in f_ffree.
1089 * For f_ffree, report the smaller of the number of object available
1090 * and the number of blocks (each object will take at least a block).
1092 statp
->f_ffree
= MIN(availobjs
, availbytes
>> DNODE_SHIFT
);
1093 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1094 statp
->f_fsid
.val
[0] = (uint32_t)fsid
;
1095 statp
->f_fsid
.val
[1] = (uint32_t)(fsid
>> 32);
1096 statp
->f_type
= ZFS_SUPER_MAGIC
;
1097 statp
->f_namelen
= ZFS_MAXNAMELEN
;
1100 * We have all of 40 characters to stuff a string here.
1101 * Is there anything useful we could/should provide?
1103 bzero(statp
->f_spare
, sizeof (statp
->f_spare
));
1108 EXPORT_SYMBOL(zfs_statvfs
);
1111 zfs_root(zfs_sb_t
*zsb
, struct inode
**ipp
)
1118 error
= zfs_zget(zsb
, zsb
->z_root
, &rootzp
);
1120 *ipp
= ZTOI(rootzp
);
1125 EXPORT_SYMBOL(zfs_root
);
1127 #ifdef HAVE_D_PRUNE_ALIASES
1129 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1130 * To accommodate this we must improvise and manually walk the list of znodes
1131 * attempting to prune dentries in order to be able to drop the inodes.
1133 * To avoid scanning the same znodes multiple times they are always rotated
1134 * to the end of the z_all_znodes list. New znodes are inserted at the
1135 * end of the list so we're always scanning the oldest znodes first.
1138 zfs_sb_prune_aliases(zfs_sb_t
*zsb
, unsigned long nr_to_scan
)
1140 znode_t
**zp_array
, *zp
;
1141 int max_array
= MIN(nr_to_scan
, PAGE_SIZE
* 8 / sizeof (znode_t
*));
1145 zp_array
= kmem_zalloc(max_array
* sizeof (znode_t
*), KM_SLEEP
);
1147 mutex_enter(&zsb
->z_znodes_lock
);
1148 while ((zp
= list_head(&zsb
->z_all_znodes
)) != NULL
) {
1150 if ((i
++ > nr_to_scan
) || (j
>= max_array
))
1153 ASSERT(list_link_active(&zp
->z_link_node
));
1154 list_remove(&zsb
->z_all_znodes
, zp
);
1155 list_insert_tail(&zsb
->z_all_znodes
, zp
);
1157 /* Skip active znodes and .zfs entries */
1158 if (MUTEX_HELD(&zp
->z_lock
) || zp
->z_is_ctldir
)
1161 if (igrab(ZTOI(zp
)) == NULL
)
1167 mutex_exit(&zsb
->z_znodes_lock
);
1169 for (i
= 0; i
< j
; i
++) {
1172 ASSERT3P(zp
, !=, NULL
);
1173 d_prune_aliases(ZTOI(zp
));
1175 if (atomic_read(&ZTOI(zp
)->i_count
) == 1)
1181 kmem_free(zp_array
, max_array
* sizeof (znode_t
*));
1185 #endif /* HAVE_D_PRUNE_ALIASES */
1188 * The ARC has requested that the filesystem drop entries from the dentry
1189 * and inode caches. This can occur when the ARC needs to free meta data
1190 * blocks but can't because they are all pinned by entries in these caches.
1193 zfs_sb_prune(struct super_block
*sb
, unsigned long nr_to_scan
, int *objects
)
1195 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1197 #if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1198 struct shrinker
*shrinker
= &sb
->s_shrink
;
1199 struct shrink_control sc
= {
1200 .nr_to_scan
= nr_to_scan
,
1201 .gfp_mask
= GFP_KERNEL
,
1207 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1208 defined(SHRINK_CONTROL_HAS_NID) && \
1209 defined(SHRINKER_NUMA_AWARE)
1210 if (sb
->s_shrink
.flags
& SHRINKER_NUMA_AWARE
) {
1212 for_each_online_node(sc
.nid
)
1213 *objects
+= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1215 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1218 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1219 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1220 #elif defined(HAVE_SHRINK)
1221 *objects
= (*shrinker
->shrink
)(shrinker
, &sc
);
1222 #elif defined(HAVE_D_PRUNE_ALIASES)
1223 #define D_PRUNE_ALIASES_IS_DEFAULT
1224 *objects
= zfs_sb_prune_aliases(zsb
, nr_to_scan
);
1226 #error "No available dentry and inode cache pruning mechanism."
1229 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1230 #undef D_PRUNE_ALIASES_IS_DEFAULT
1232 * Fall back to zfs_sb_prune_aliases if the kernel's per-superblock
1233 * shrinker couldn't free anything, possibly due to the inodes being
1234 * allocated in a different memcg.
1237 *objects
= zfs_sb_prune_aliases(zsb
, nr_to_scan
);
1242 dprintf_ds(zsb
->z_os
->os_dsl_dataset
,
1243 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1244 nr_to_scan
, *objects
, error
);
1248 EXPORT_SYMBOL(zfs_sb_prune
);
1251 * Teardown the zfs_sb_t.
1253 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
1254 * and 'z_teardown_inactive_lock' held.
1257 zfs_sb_teardown(zfs_sb_t
*zsb
, boolean_t unmounting
)
1262 * If someone has not already unmounted this file system,
1263 * drain the iput_taskq to ensure all active references to the
1264 * zfs_sb_t have been handled only then can it be safely destroyed.
1268 * If we're unmounting we have to wait for the list to
1271 * If we're not unmounting there's no guarantee the list
1272 * will drain completely, but iputs run from the taskq
1273 * may add the parents of dir-based xattrs to the taskq
1274 * so we want to wait for these.
1276 * We can safely read z_nr_znodes without locking because the
1277 * VFS has already blocked operations which add to the
1278 * z_all_znodes list and thus increment z_nr_znodes.
1281 while (zsb
->z_nr_znodes
> 0) {
1282 taskq_wait_outstanding(dsl_pool_iput_taskq(
1283 dmu_objset_pool(zsb
->z_os
)), 0);
1284 if (++round
> 1 && !unmounting
)
1289 rrm_enter(&zsb
->z_teardown_lock
, RW_WRITER
, FTAG
);
1293 * We purge the parent filesystem's super block as the
1294 * parent filesystem and all of its snapshots have their
1295 * inode's super block set to the parent's filesystem's
1296 * super block. Note, 'z_parent' is self referential
1297 * for non-snapshots.
1299 shrink_dcache_sb(zsb
->z_parent
->z_sb
);
1303 * Close the zil. NB: Can't close the zil while zfs_inactive
1304 * threads are blocked as zil_close can call zfs_inactive.
1307 zil_close(zsb
->z_log
);
1311 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_WRITER
);
1314 * If we are not unmounting (ie: online recv) and someone already
1315 * unmounted this file system while we were doing the switcheroo,
1316 * or a reopen of z_os failed then just bail out now.
1318 if (!unmounting
&& (zsb
->z_unmounted
|| zsb
->z_os
== NULL
)) {
1319 rw_exit(&zsb
->z_teardown_inactive_lock
);
1320 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1321 return (SET_ERROR(EIO
));
1325 * At this point there are no VFS ops active, and any new VFS ops
1326 * will fail with EIO since we have z_teardown_lock for writer (only
1327 * relevant for forced unmount).
1329 * Release all holds on dbufs.
1332 mutex_enter(&zsb
->z_znodes_lock
);
1333 for (zp
= list_head(&zsb
->z_all_znodes
); zp
!= NULL
;
1334 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1336 zfs_znode_dmu_fini(zp
);
1338 mutex_exit(&zsb
->z_znodes_lock
);
1342 * If we are unmounting, set the unmounted flag and let new VFS ops
1343 * unblock. zfs_inactive will have the unmounted behavior, and all
1344 * other VFS ops will fail with EIO.
1347 zsb
->z_unmounted
= B_TRUE
;
1348 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1349 rw_exit(&zsb
->z_teardown_inactive_lock
);
1353 * z_os will be NULL if there was an error in attempting to reopen
1354 * zsb, so just return as the properties had already been
1356 * unregistered and cached data had been evicted before.
1358 if (zsb
->z_os
== NULL
)
1362 * Unregister properties.
1364 zfs_unregister_callbacks(zsb
);
1369 if (dsl_dataset_is_dirty(dmu_objset_ds(zsb
->z_os
)) &&
1370 !zfs_is_readonly(zsb
))
1371 txg_wait_synced(dmu_objset_pool(zsb
->z_os
), 0);
1372 dmu_objset_evict_dbufs(zsb
->z_os
);
1376 EXPORT_SYMBOL(zfs_sb_teardown
);
1378 #if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1379 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
1380 atomic_long_t zfs_bdi_seq
= ATOMIC_LONG_INIT(0);
1384 zfs_domount(struct super_block
*sb
, zfs_mntopts_t
*zmo
, int silent
)
1386 const char *osname
= zmo
->z_osname
;
1388 struct inode
*root_inode
;
1389 uint64_t recordsize
;
1392 error
= zfs_sb_create(osname
, zmo
, &zsb
);
1396 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1397 &recordsize
, NULL
)))
1401 sb
->s_fs_info
= zsb
;
1402 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1403 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1404 sb
->s_time_gran
= 1;
1405 sb
->s_blocksize
= recordsize
;
1406 sb
->s_blocksize_bits
= ilog2(recordsize
);
1407 zsb
->z_bdi
.ra_pages
= 0;
1408 sb
->s_bdi
= &zsb
->z_bdi
;
1410 error
= -zpl_bdi_setup_and_register(&zsb
->z_bdi
, "zfs");
1414 /* Set callback operations for the file system. */
1415 sb
->s_op
= &zpl_super_operations
;
1416 sb
->s_xattr
= zpl_xattr_handlers
;
1417 sb
->s_export_op
= &zpl_export_operations
;
1419 sb
->s_d_op
= &zpl_dentry_operations
;
1420 #endif /* HAVE_S_D_OP */
1422 /* Set features for file system. */
1423 zfs_set_fuid_feature(zsb
);
1425 if (dmu_objset_is_snapshot(zsb
->z_os
)) {
1428 atime_changed_cb(zsb
, B_FALSE
);
1429 readonly_changed_cb(zsb
, B_TRUE
);
1430 if ((error
= dsl_prop_get_integer(osname
,
1431 "xattr", &pval
, NULL
)))
1433 xattr_changed_cb(zsb
, pval
);
1434 if ((error
= dsl_prop_get_integer(osname
,
1435 "acltype", &pval
, NULL
)))
1437 acltype_changed_cb(zsb
, pval
);
1438 zsb
->z_issnap
= B_TRUE
;
1439 zsb
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1440 zsb
->z_snap_defer_time
= jiffies
;
1442 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
1443 dmu_objset_set_user(zsb
->z_os
, zsb
);
1444 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
1446 error
= zfs_sb_setup(zsb
, B_TRUE
);
1449 /* Allocate a root inode for the filesystem. */
1450 error
= zfs_root(zsb
, &root_inode
);
1452 (void) zfs_umount(sb
);
1456 /* Allocate a root dentry for the filesystem */
1457 sb
->s_root
= d_make_root(root_inode
);
1458 if (sb
->s_root
== NULL
) {
1459 (void) zfs_umount(sb
);
1460 error
= SET_ERROR(ENOMEM
);
1467 zsb
->z_arc_prune
= arc_add_prune_callback(zpl_prune_sb
, sb
);
1470 dmu_objset_disown(zsb
->z_os
, zsb
);
1476 EXPORT_SYMBOL(zfs_domount
);
1479 * Called when an unmount is requested and certain sanity checks have
1480 * already passed. At this point no dentries or inodes have been reclaimed
1481 * from their respective caches. We drop the extra reference on the .zfs
1482 * control directory to allow everything to be reclaimed. All snapshots
1483 * must already have been unmounted to reach this point.
1486 zfs_preumount(struct super_block
*sb
)
1488 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1491 zfsctl_destroy(sb
->s_fs_info
);
1493 EXPORT_SYMBOL(zfs_preumount
);
1496 * Called once all other unmount released tear down has occurred.
1497 * It is our responsibility to release any remaining infrastructure.
1501 zfs_umount(struct super_block
*sb
)
1503 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1506 arc_remove_prune_callback(zsb
->z_arc_prune
);
1507 VERIFY(zfs_sb_teardown(zsb
, B_TRUE
) == 0);
1509 bdi_destroy(sb
->s_bdi
);
1512 * z_os will be NULL if there was an error in
1513 * attempting to reopen zsb.
1517 * Unset the objset user_ptr.
1519 mutex_enter(&os
->os_user_ptr_lock
);
1520 dmu_objset_set_user(os
, NULL
);
1521 mutex_exit(&os
->os_user_ptr_lock
);
1524 * Finally release the objset
1526 dmu_objset_disown(os
, zsb
);
1532 EXPORT_SYMBOL(zfs_umount
);
1535 zfs_remount(struct super_block
*sb
, int *flags
, zfs_mntopts_t
*zmo
)
1537 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1540 zfs_unregister_callbacks(zsb
);
1541 error
= zfs_register_callbacks(zsb
);
1545 EXPORT_SYMBOL(zfs_remount
);
1548 zfs_vget(struct super_block
*sb
, struct inode
**ipp
, fid_t
*fidp
)
1550 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1552 uint64_t object
= 0;
1553 uint64_t fid_gen
= 0;
1562 if (fidp
->fid_len
== LONG_FID_LEN
) {
1563 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1564 uint64_t objsetid
= 0;
1565 uint64_t setgen
= 0;
1567 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1568 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1570 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1571 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1575 err
= zfsctl_lookup_objset(sb
, objsetid
, &zsb
);
1577 return (SET_ERROR(EINVAL
));
1582 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1583 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1585 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1586 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1588 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1589 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1592 return (SET_ERROR(EINVAL
));
1595 /* A zero fid_gen means we are in the .zfs control directories */
1597 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1598 *ipp
= zsb
->z_ctldir
;
1599 ASSERT(*ipp
!= NULL
);
1600 if (object
== ZFSCTL_INO_SNAPDIR
) {
1601 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
,
1602 0, kcred
, NULL
, NULL
) == 0);
1610 gen_mask
= -1ULL >> (64 - 8 * i
);
1612 dprintf("getting %llu [%llu mask %llx]\n", object
, fid_gen
, gen_mask
);
1613 if ((err
= zfs_zget(zsb
, object
, &zp
))) {
1618 /* Don't export xattr stuff */
1619 if (zp
->z_pflags
& ZFS_XATTR
) {
1622 return (SET_ERROR(ENOENT
));
1625 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
), &zp_gen
,
1627 zp_gen
= zp_gen
& gen_mask
;
1630 if ((fid_gen
== 0) && (zsb
->z_root
== object
))
1632 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1633 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen
,
1637 return (SET_ERROR(ENOENT
));
1642 zfs_inode_update(ITOZ(*ipp
));
1647 EXPORT_SYMBOL(zfs_vget
);
1650 * Block out VFS ops and close zfs_sb_t
1652 * Note, if successful, then we return with the 'z_teardown_lock' and
1653 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1654 * dataset and objset intact so that they can be atomically handed off during
1655 * a subsequent rollback or recv operation and the resume thereafter.
1658 zfs_suspend_fs(zfs_sb_t
*zsb
)
1662 if ((error
= zfs_sb_teardown(zsb
, B_FALSE
)) != 0)
1667 EXPORT_SYMBOL(zfs_suspend_fs
);
1670 * Reopen zfs_sb_t and release VFS ops.
1673 zfs_resume_fs(zfs_sb_t
*zsb
, const char *osname
)
1677 uint64_t sa_obj
= 0;
1679 ASSERT(RRM_WRITE_HELD(&zsb
->z_teardown_lock
));
1680 ASSERT(RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
));
1683 * We already own this, so just hold and rele it to update the
1684 * objset_t, as the one we had before may have been evicted.
1686 VERIFY0(dmu_objset_hold(osname
, zsb
, &zsb
->z_os
));
1687 VERIFY3P(zsb
->z_os
->os_dsl_dataset
->ds_owner
, ==, zsb
);
1688 VERIFY(dsl_dataset_long_held(zsb
->z_os
->os_dsl_dataset
));
1689 dmu_objset_rele(zsb
->z_os
, zsb
);
1692 * Make sure version hasn't changed
1695 err
= zfs_get_zplprop(zsb
->z_os
, ZFS_PROP_VERSION
,
1701 err
= zap_lookup(zsb
->z_os
, MASTER_NODE_OBJ
,
1702 ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1704 if (err
&& zsb
->z_version
>= ZPL_VERSION_SA
)
1707 if ((err
= sa_setup(zsb
->z_os
, sa_obj
,
1708 zfs_attr_table
, ZPL_END
, &zsb
->z_attr_table
)) != 0)
1711 if (zsb
->z_version
>= ZPL_VERSION_SA
)
1712 sa_register_update_callback(zsb
->z_os
,
1715 VERIFY(zfs_sb_setup(zsb
, B_FALSE
) == 0);
1717 zfs_set_fuid_feature(zsb
);
1718 zsb
->z_rollback_time
= jiffies
;
1721 * Attempt to re-establish all the active inodes with their
1722 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1723 * and mark it stale. This prevents a collision if a new
1724 * inode/object is created which must use the same inode
1725 * number. The stale inode will be be released when the
1726 * VFS prunes the dentry holding the remaining references
1727 * on the stale inode.
1729 mutex_enter(&zsb
->z_znodes_lock
);
1730 for (zp
= list_head(&zsb
->z_all_znodes
); zp
;
1731 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1732 err2
= zfs_rezget(zp
);
1734 remove_inode_hash(ZTOI(zp
));
1735 zp
->z_is_stale
= B_TRUE
;
1738 mutex_exit(&zsb
->z_znodes_lock
);
1741 /* release the VFS ops */
1742 rw_exit(&zsb
->z_teardown_inactive_lock
);
1743 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1747 * Since we couldn't setup the sa framework, try to force
1748 * unmount this file system.
1751 (void) zfs_umount(zsb
->z_sb
);
1755 EXPORT_SYMBOL(zfs_resume_fs
);
1758 zfs_set_version(zfs_sb_t
*zsb
, uint64_t newvers
)
1761 objset_t
*os
= zsb
->z_os
;
1764 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1765 return (SET_ERROR(EINVAL
));
1767 if (newvers
< zsb
->z_version
)
1768 return (SET_ERROR(EINVAL
));
1770 if (zfs_spa_version_map(newvers
) >
1771 spa_version(dmu_objset_spa(zsb
->z_os
)))
1772 return (SET_ERROR(ENOTSUP
));
1774 tx
= dmu_tx_create(os
);
1775 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1776 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1777 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1779 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1781 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1787 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1788 8, 1, &newvers
, tx
);
1795 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1798 ASSERT3U(spa_version(dmu_objset_spa(zsb
->z_os
)), >=,
1800 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1801 DMU_OT_NONE
, 0, tx
);
1803 error
= zap_add(os
, MASTER_NODE_OBJ
,
1804 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1807 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
1808 sa_register_update_callback(os
, zfs_sa_upgrade
);
1811 spa_history_log_internal_ds(dmu_objset_ds(os
), "upgrade", tx
,
1812 "from %llu to %llu", zsb
->z_version
, newvers
);
1816 zsb
->z_version
= newvers
;
1818 zfs_set_fuid_feature(zsb
);
1822 EXPORT_SYMBOL(zfs_set_version
);
1825 * Read a property stored within the master node.
1828 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
1831 int error
= SET_ERROR(ENOENT
);
1834 * Look up the file system's value for the property. For the
1835 * version property, we look up a slightly different string.
1837 if (prop
== ZFS_PROP_VERSION
)
1838 pname
= ZPL_VERSION_STR
;
1840 pname
= zfs_prop_to_name(prop
);
1843 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
1845 if (error
== ENOENT
) {
1846 /* No value set, use the default value */
1848 case ZFS_PROP_VERSION
:
1849 *value
= ZPL_VERSION
;
1851 case ZFS_PROP_NORMALIZE
:
1852 case ZFS_PROP_UTF8ONLY
:
1856 *value
= ZFS_CASE_SENSITIVE
;
1858 case ZFS_PROP_ACLTYPE
:
1859 *value
= ZFS_ACLTYPE_OFF
;
1868 EXPORT_SYMBOL(zfs_get_zplprop
);
1875 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
1876 register_filesystem(&zpl_fs_type
);
1882 taskq_wait_outstanding(system_taskq
, 0);
1883 unregister_filesystem(&zpl_fs_type
);