4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 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
);
344 dsl_prop_unregister_all(ds
, zsb
);
347 EXPORT_SYMBOL(zfs_register_callbacks
);
350 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
351 uint64_t *userp
, uint64_t *groupp
)
354 * Is it a valid type of object to track?
356 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
357 return (SET_ERROR(ENOENT
));
360 * If we have a NULL data pointer
361 * then assume the id's aren't changing and
362 * return EEXIST to the dmu to let it know to
366 return (SET_ERROR(EEXIST
));
368 if (bonustype
== DMU_OT_ZNODE
) {
369 znode_phys_t
*znp
= data
;
370 *userp
= znp
->zp_uid
;
371 *groupp
= znp
->zp_gid
;
374 sa_hdr_phys_t
*sap
= data
;
375 sa_hdr_phys_t sa
= *sap
;
376 boolean_t swap
= B_FALSE
;
378 ASSERT(bonustype
== DMU_OT_SA
);
380 if (sa
.sa_magic
== 0) {
382 * This should only happen for newly created
383 * files that haven't had the znode data filled
390 if (sa
.sa_magic
== BSWAP_32(SA_MAGIC
)) {
391 sa
.sa_magic
= SA_MAGIC
;
392 sa
.sa_layout_info
= BSWAP_16(sa
.sa_layout_info
);
395 VERIFY3U(sa
.sa_magic
, ==, SA_MAGIC
);
398 hdrsize
= sa_hdrsize(&sa
);
399 VERIFY3U(hdrsize
, >=, sizeof (sa_hdr_phys_t
));
400 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
402 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
405 *userp
= BSWAP_64(*userp
);
406 *groupp
= BSWAP_64(*groupp
);
413 fuidstr_to_sid(zfs_sb_t
*zsb
, const char *fuidstr
,
414 char *domainbuf
, int buflen
, uid_t
*ridp
)
419 fuid
= strtonum(fuidstr
, NULL
);
421 domain
= zfs_fuid_find_by_idx(zsb
, FUID_INDEX(fuid
));
423 (void) strlcpy(domainbuf
, domain
, buflen
);
426 *ridp
= FUID_RID(fuid
);
430 zfs_userquota_prop_to_obj(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
)
433 case ZFS_PROP_USERUSED
:
434 case ZFS_PROP_USEROBJUSED
:
435 return (DMU_USERUSED_OBJECT
);
436 case ZFS_PROP_GROUPUSED
:
437 case ZFS_PROP_GROUPOBJUSED
:
438 return (DMU_GROUPUSED_OBJECT
);
439 case ZFS_PROP_USERQUOTA
:
440 return (zsb
->z_userquota_obj
);
441 case ZFS_PROP_GROUPQUOTA
:
442 return (zsb
->z_groupquota_obj
);
443 case ZFS_PROP_USEROBJQUOTA
:
444 return (zsb
->z_userobjquota_obj
);
445 case ZFS_PROP_GROUPOBJQUOTA
:
446 return (zsb
->z_groupobjquota_obj
);
448 return (ZFS_NO_OBJECT
);
453 zfs_userspace_many(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
454 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
459 zfs_useracct_t
*buf
= vbuf
;
463 if (!dmu_objset_userspace_present(zsb
->z_os
))
464 return (SET_ERROR(ENOTSUP
));
466 if ((type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
||
467 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
) &&
468 !dmu_objset_userobjspace_present(zsb
->z_os
))
469 return (SET_ERROR(ENOTSUP
));
471 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
472 if (obj
== ZFS_NO_OBJECT
) {
477 if (type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
)
478 offset
= DMU_OBJACCT_PREFIX_LEN
;
480 for (zap_cursor_init_serialized(&zc
, zsb
->z_os
, obj
, *cookiep
);
481 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
482 zap_cursor_advance(&zc
)) {
483 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
488 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
489 * when dealing with block quota and vice versa.
491 if ((offset
> 0) != (strncmp(za
.za_name
, DMU_OBJACCT_PREFIX
,
492 DMU_OBJACCT_PREFIX_LEN
) == 0))
495 fuidstr_to_sid(zsb
, za
.za_name
+ offset
,
496 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
498 buf
->zu_space
= za
.za_first_integer
;
504 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
505 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
506 *cookiep
= zap_cursor_serialize(&zc
);
507 zap_cursor_fini(&zc
);
510 EXPORT_SYMBOL(zfs_userspace_many
);
513 * buf must be big enough (eg, 32 bytes)
516 id_to_fuidstr(zfs_sb_t
*zsb
, const char *domain
, uid_t rid
,
517 char *buf
, boolean_t addok
)
522 if (domain
&& domain
[0]) {
523 domainid
= zfs_fuid_find_by_domain(zsb
, domain
, NULL
, addok
);
525 return (SET_ERROR(ENOENT
));
527 fuid
= FUID_ENCODE(domainid
, rid
);
528 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
533 zfs_userspace_one(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
534 const char *domain
, uint64_t rid
, uint64_t *valp
)
536 char buf
[20 + DMU_OBJACCT_PREFIX_LEN
];
543 if (!dmu_objset_userspace_present(zsb
->z_os
))
544 return (SET_ERROR(ENOTSUP
));
546 if ((type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
||
547 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
) &&
548 !dmu_objset_userobjspace_present(zsb
->z_os
))
549 return (SET_ERROR(ENOTSUP
));
551 obj
= zfs_userquota_prop_to_obj(zsb
, type
);
552 if (obj
== ZFS_NO_OBJECT
)
555 if (type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
) {
556 strlcpy(buf
, DMU_OBJACCT_PREFIX
, DMU_OBJACCT_PREFIX_LEN
);
557 offset
= DMU_OBJACCT_PREFIX_LEN
;
560 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
+ offset
, B_FALSE
);
564 err
= zap_lookup(zsb
->z_os
, obj
, buf
, 8, 1, valp
);
569 EXPORT_SYMBOL(zfs_userspace_one
);
572 zfs_set_userquota(zfs_sb_t
*zsb
, zfs_userquota_prop_t type
,
573 const char *domain
, uint64_t rid
, uint64_t quota
)
579 boolean_t fuid_dirtied
;
581 if (zsb
->z_version
< ZPL_VERSION_USERSPACE
)
582 return (SET_ERROR(ENOTSUP
));
585 case ZFS_PROP_USERQUOTA
:
586 objp
= &zsb
->z_userquota_obj
;
588 case ZFS_PROP_GROUPQUOTA
:
589 objp
= &zsb
->z_groupquota_obj
;
591 case ZFS_PROP_USEROBJQUOTA
:
592 objp
= &zsb
->z_userobjquota_obj
;
594 case ZFS_PROP_GROUPOBJQUOTA
:
595 objp
= &zsb
->z_groupobjquota_obj
;
598 return (SET_ERROR(EINVAL
));
601 err
= id_to_fuidstr(zsb
, domain
, rid
, buf
, B_TRUE
);
604 fuid_dirtied
= zsb
->z_fuid_dirty
;
606 tx
= dmu_tx_create(zsb
->z_os
);
607 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
609 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
610 zfs_userquota_prop_prefixes
[type
]);
613 zfs_fuid_txhold(zsb
, tx
);
614 err
= dmu_tx_assign(tx
, TXG_WAIT
);
620 mutex_enter(&zsb
->z_lock
);
622 *objp
= zap_create(zsb
->z_os
, DMU_OT_USERGROUP_QUOTA
,
624 VERIFY(0 == zap_add(zsb
->z_os
, MASTER_NODE_OBJ
,
625 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
627 mutex_exit(&zsb
->z_lock
);
630 err
= zap_remove(zsb
->z_os
, *objp
, buf
, tx
);
634 err
= zap_update(zsb
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
638 zfs_fuid_sync(zsb
, tx
);
642 EXPORT_SYMBOL(zfs_set_userquota
);
645 zfs_fuid_overobjquota(zfs_sb_t
*zsb
, boolean_t isgroup
, uint64_t fuid
)
647 char buf
[20 + DMU_OBJACCT_PREFIX_LEN
];
648 uint64_t used
, quota
, usedobj
, quotaobj
;
651 if (!dmu_objset_userobjspace_present(zsb
->z_os
)) {
652 if (dmu_objset_userobjspace_upgradable(zsb
->z_os
))
653 dmu_objset_userobjspace_upgrade(zsb
->z_os
);
657 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
658 quotaobj
= isgroup
? zsb
->z_groupobjquota_obj
: zsb
->z_userobjquota_obj
;
659 if (quotaobj
== 0 || zsb
->z_replay
)
662 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
663 err
= zap_lookup(zsb
->z_os
, quotaobj
, buf
, 8, 1, "a
);
667 (void) sprintf(buf
, DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)fuid
);
668 err
= zap_lookup(zsb
->z_os
, usedobj
, buf
, 8, 1, &used
);
671 return (used
>= quota
);
675 zfs_fuid_overquota(zfs_sb_t
*zsb
, boolean_t isgroup
, uint64_t fuid
)
678 uint64_t used
, quota
, usedobj
, quotaobj
;
681 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
682 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
684 if (quotaobj
== 0 || zsb
->z_replay
)
687 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
688 err
= zap_lookup(zsb
->z_os
, quotaobj
, buf
, 8, 1, "a
);
692 err
= zap_lookup(zsb
->z_os
, usedobj
, buf
, 8, 1, &used
);
695 return (used
>= quota
);
697 EXPORT_SYMBOL(zfs_fuid_overquota
);
700 zfs_owner_overquota(zfs_sb_t
*zsb
, znode_t
*zp
, boolean_t isgroup
)
704 struct inode
*ip
= ZTOI(zp
);
706 quotaobj
= isgroup
? zsb
->z_groupquota_obj
: zsb
->z_userquota_obj
;
708 fuid
= isgroup
? KGID_TO_SGID(ip
->i_gid
) : KUID_TO_SUID(ip
->i_uid
);
710 if (quotaobj
== 0 || zsb
->z_replay
)
713 return (zfs_fuid_overquota(zsb
, isgroup
, fuid
));
715 EXPORT_SYMBOL(zfs_owner_overquota
);
718 zfs_mntopts_alloc(void)
720 return (kmem_zalloc(sizeof (zfs_mntopts_t
), KM_SLEEP
));
724 zfs_mntopts_free(zfs_mntopts_t
*zmo
)
727 strfree(zmo
->z_osname
);
730 strfree(zmo
->z_mntpoint
);
732 kmem_free(zmo
, sizeof (zfs_mntopts_t
));
736 zfs_sb_create(const char *osname
, zfs_mntopts_t
*zmo
, zfs_sb_t
**zsbp
)
744 zsb
= kmem_zalloc(sizeof (zfs_sb_t
), KM_SLEEP
);
747 * Optional temporary mount options, free'd in zfs_sb_free().
749 zsb
->z_mntopts
= (zmo
? zmo
: zfs_mntopts_alloc());
752 * We claim to always be readonly so we can open snapshots;
753 * other ZPL code will prevent us from writing to snapshots.
755 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zsb
, &os
);
760 * Initialize the zfs-specific filesystem structure.
761 * Should probably make this a kmem cache, shuffle fields.
765 zsb
->z_max_blksz
= SPA_OLD_MAXBLOCKSIZE
;
766 zsb
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
769 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zsb
->z_version
);
772 } else if (zsb
->z_version
> ZPL_VERSION
) {
773 error
= SET_ERROR(ENOTSUP
);
776 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &zval
)) != 0)
778 zsb
->z_norm
= (int)zval
;
780 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &zval
)) != 0)
782 zsb
->z_utf8
= (zval
!= 0);
784 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &zval
)) != 0)
786 zsb
->z_case
= (uint_t
)zval
;
788 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_ACLTYPE
, &zval
)) != 0)
790 zsb
->z_acl_type
= (uint_t
)zval
;
793 * Fold case on file systems that are always or sometimes case
796 if (zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
797 zsb
->z_case
== ZFS_CASE_MIXED
)
798 zsb
->z_norm
|= U8_TEXTPREP_TOUPPER
;
800 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
801 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
804 /* should either have both of these objects or none */
805 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
810 error
= zfs_get_zplprop(os
, ZFS_PROP_XATTR
, &zval
);
811 if ((error
== 0) && (zval
== ZFS_XATTR_SA
))
812 zsb
->z_xattr_sa
= B_TRUE
;
815 * Pre SA versions file systems should never touch
816 * either the attribute registration or layout objects.
821 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
826 if (zsb
->z_version
>= ZPL_VERSION_SA
)
827 sa_register_update_callback(os
, zfs_sa_upgrade
);
829 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
833 ASSERT(zsb
->z_root
!= 0);
835 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
836 &zsb
->z_unlinkedobj
);
840 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
841 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
842 8, 1, &zsb
->z_userquota_obj
);
843 if (error
&& error
!= ENOENT
)
846 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
847 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
848 8, 1, &zsb
->z_groupquota_obj
);
849 if (error
&& error
!= ENOENT
)
852 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
853 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
],
854 8, 1, &zsb
->z_userobjquota_obj
);
855 if (error
&& error
!= ENOENT
)
858 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
859 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
],
860 8, 1, &zsb
->z_groupobjquota_obj
);
861 if (error
&& error
!= ENOENT
)
864 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
866 if (error
&& error
!= ENOENT
)
869 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
871 if (error
&& error
!= ENOENT
)
874 mutex_init(&zsb
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
875 mutex_init(&zsb
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
876 list_create(&zsb
->z_all_znodes
, sizeof (znode_t
),
877 offsetof(znode_t
, z_link_node
));
878 rrm_init(&zsb
->z_teardown_lock
, B_FALSE
);
879 rw_init(&zsb
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
880 rw_init(&zsb
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
882 size
= MIN(1 << (highbit64(zfs_object_mutex_size
)-1), ZFS_OBJ_MTX_MAX
);
883 zsb
->z_hold_size
= size
;
884 zsb
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
, KM_SLEEP
);
885 zsb
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
886 for (i
= 0; i
!= size
; i
++) {
887 avl_create(&zsb
->z_hold_trees
[i
], zfs_znode_hold_compare
,
888 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
889 mutex_init(&zsb
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
896 dmu_objset_disown(os
, zsb
);
899 zfs_mntopts_free(zsb
->z_mntopts
);
900 kmem_free(zsb
, sizeof (zfs_sb_t
));
903 EXPORT_SYMBOL(zfs_sb_create
);
906 zfs_sb_setup(zfs_sb_t
*zsb
, boolean_t mounting
)
910 error
= zfs_register_callbacks(zsb
);
914 zsb
->z_log
= zil_open(zsb
->z_os
, zfs_get_data
);
917 * If we are not mounting (ie: online recv), then we don't
918 * have to worry about replaying the log as we blocked all
919 * operations out since we closed the ZIL.
925 * During replay we remove the read only flag to
926 * allow replays to succeed.
928 readonly
= zfs_is_readonly(zsb
);
930 readonly_changed_cb(zsb
, B_FALSE
);
932 zfs_unlinked_drain(zsb
);
935 * Parse and replay the intent log.
937 * Because of ziltest, this must be done after
938 * zfs_unlinked_drain(). (Further note: ziltest
939 * doesn't use readonly mounts, where
940 * zfs_unlinked_drain() isn't called.) This is because
941 * ziltest causes spa_sync() to think it's committed,
942 * but actually it is not, so the intent log contains
943 * many txg's worth of changes.
945 * In particular, if object N is in the unlinked set in
946 * the last txg to actually sync, then it could be
947 * actually freed in a later txg and then reallocated
948 * in a yet later txg. This would write a "create
949 * object N" record to the intent log. Normally, this
950 * would be fine because the spa_sync() would have
951 * written out the fact that object N is free, before
952 * we could write the "create object N" intent log
955 * But when we are in ziltest mode, we advance the "open
956 * txg" without actually spa_sync()-ing the changes to
957 * disk. So we would see that object N is still
958 * allocated and in the unlinked set, and there is an
959 * intent log record saying to allocate it.
961 if (spa_writeable(dmu_objset_spa(zsb
->z_os
))) {
962 if (zil_replay_disable
) {
963 zil_destroy(zsb
->z_log
, B_FALSE
);
965 zsb
->z_replay
= B_TRUE
;
966 zil_replay(zsb
->z_os
, zsb
,
968 zsb
->z_replay
= B_FALSE
;
972 /* restore readonly bit */
974 readonly_changed_cb(zsb
, B_TRUE
);
978 * Set the objset user_ptr to track its zsb.
980 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
981 dmu_objset_set_user(zsb
->z_os
, zsb
);
982 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
986 EXPORT_SYMBOL(zfs_sb_setup
);
989 zfs_sb_free(zfs_sb_t
*zsb
)
991 int i
, size
= zsb
->z_hold_size
;
993 zfs_fuid_destroy(zsb
);
995 mutex_destroy(&zsb
->z_znodes_lock
);
996 mutex_destroy(&zsb
->z_lock
);
997 list_destroy(&zsb
->z_all_znodes
);
998 rrm_destroy(&zsb
->z_teardown_lock
);
999 rw_destroy(&zsb
->z_teardown_inactive_lock
);
1000 rw_destroy(&zsb
->z_fuid_lock
);
1001 for (i
= 0; i
!= size
; i
++) {
1002 avl_destroy(&zsb
->z_hold_trees
[i
]);
1003 mutex_destroy(&zsb
->z_hold_locks
[i
]);
1005 vmem_free(zsb
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
1006 vmem_free(zsb
->z_hold_locks
, sizeof (kmutex_t
) * size
);
1007 zfs_mntopts_free(zsb
->z_mntopts
);
1008 kmem_free(zsb
, sizeof (zfs_sb_t
));
1010 EXPORT_SYMBOL(zfs_sb_free
);
1013 zfs_set_fuid_feature(zfs_sb_t
*zsb
)
1015 zsb
->z_use_fuids
= USE_FUIDS(zsb
->z_version
, zsb
->z_os
);
1016 zsb
->z_use_sa
= USE_SA(zsb
->z_version
, zsb
->z_os
);
1020 zfs_unregister_callbacks(zfs_sb_t
*zsb
)
1022 objset_t
*os
= zsb
->z_os
;
1024 if (!dmu_objset_is_snapshot(os
))
1025 dsl_prop_unregister_all(dmu_objset_ds(os
), zsb
);
1027 EXPORT_SYMBOL(zfs_unregister_callbacks
);
1029 #ifdef HAVE_MLSLABEL
1031 * Check that the hex label string is appropriate for the dataset being
1032 * mounted into the global_zone proper.
1034 * Return an error if the hex label string is not default or
1035 * admin_low/admin_high. For admin_low labels, the corresponding
1036 * dataset must be readonly.
1039 zfs_check_global_label(const char *dsname
, const char *hexsl
)
1041 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
1043 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
1045 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
1046 /* must be readonly */
1049 if (dsl_prop_get_integer(dsname
,
1050 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
1051 return (SET_ERROR(EACCES
));
1052 return (rdonly
? 0 : EACCES
);
1054 return (SET_ERROR(EACCES
));
1056 EXPORT_SYMBOL(zfs_check_global_label
);
1057 #endif /* HAVE_MLSLABEL */
1060 zfs_statvfs(struct dentry
*dentry
, struct kstatfs
*statp
)
1062 zfs_sb_t
*zsb
= dentry
->d_sb
->s_fs_info
;
1063 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
1069 dmu_objset_space(zsb
->z_os
,
1070 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1072 fsid
= dmu_objset_fsid_guid(zsb
->z_os
);
1074 * The underlying storage pool actually uses multiple block
1075 * size. Under Solaris frsize (fragment size) is reported as
1076 * the smallest block size we support, and bsize (block size)
1077 * as the filesystem's maximum block size. Unfortunately,
1078 * under Linux the fragment size and block size are often used
1079 * interchangeably. Thus we are forced to report both of them
1080 * as the filesystem's maximum block size.
1082 statp
->f_frsize
= zsb
->z_max_blksz
;
1083 statp
->f_bsize
= zsb
->z_max_blksz
;
1084 bshift
= fls(statp
->f_bsize
) - 1;
1087 * The following report "total" blocks of various kinds in
1088 * the file system, but reported in terms of f_bsize - the
1092 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
1093 statp
->f_bfree
= availbytes
>> bshift
;
1094 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1097 * statvfs() should really be called statufs(), because it assumes
1098 * static metadata. ZFS doesn't preallocate files, so the best
1099 * we can do is report the max that could possibly fit in f_files,
1100 * and that minus the number actually used in f_ffree.
1101 * For f_ffree, report the smaller of the number of object available
1102 * and the number of blocks (each object will take at least a block).
1104 statp
->f_ffree
= MIN(availobjs
, availbytes
>> DNODE_SHIFT
);
1105 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1106 statp
->f_fsid
.val
[0] = (uint32_t)fsid
;
1107 statp
->f_fsid
.val
[1] = (uint32_t)(fsid
>> 32);
1108 statp
->f_type
= ZFS_SUPER_MAGIC
;
1109 statp
->f_namelen
= MAXNAMELEN
- 1;
1112 * We have all of 40 characters to stuff a string here.
1113 * Is there anything useful we could/should provide?
1115 bzero(statp
->f_spare
, sizeof (statp
->f_spare
));
1120 EXPORT_SYMBOL(zfs_statvfs
);
1123 zfs_root(zfs_sb_t
*zsb
, struct inode
**ipp
)
1130 error
= zfs_zget(zsb
, zsb
->z_root
, &rootzp
);
1132 *ipp
= ZTOI(rootzp
);
1137 EXPORT_SYMBOL(zfs_root
);
1139 #ifdef HAVE_D_PRUNE_ALIASES
1141 * Linux kernels older than 3.1 do not support a per-filesystem shrinker.
1142 * To accommodate this we must improvise and manually walk the list of znodes
1143 * attempting to prune dentries in order to be able to drop the inodes.
1145 * To avoid scanning the same znodes multiple times they are always rotated
1146 * to the end of the z_all_znodes list. New znodes are inserted at the
1147 * end of the list so we're always scanning the oldest znodes first.
1150 zfs_sb_prune_aliases(zfs_sb_t
*zsb
, unsigned long nr_to_scan
)
1152 znode_t
**zp_array
, *zp
;
1153 int max_array
= MIN(nr_to_scan
, PAGE_SIZE
* 8 / sizeof (znode_t
*));
1157 zp_array
= kmem_zalloc(max_array
* sizeof (znode_t
*), KM_SLEEP
);
1159 mutex_enter(&zsb
->z_znodes_lock
);
1160 while ((zp
= list_head(&zsb
->z_all_znodes
)) != NULL
) {
1162 if ((i
++ > nr_to_scan
) || (j
>= max_array
))
1165 ASSERT(list_link_active(&zp
->z_link_node
));
1166 list_remove(&zsb
->z_all_znodes
, zp
);
1167 list_insert_tail(&zsb
->z_all_znodes
, zp
);
1169 /* Skip active znodes and .zfs entries */
1170 if (MUTEX_HELD(&zp
->z_lock
) || zp
->z_is_ctldir
)
1173 if (igrab(ZTOI(zp
)) == NULL
)
1179 mutex_exit(&zsb
->z_znodes_lock
);
1181 for (i
= 0; i
< j
; i
++) {
1184 ASSERT3P(zp
, !=, NULL
);
1185 d_prune_aliases(ZTOI(zp
));
1187 if (atomic_read(&ZTOI(zp
)->i_count
) == 1)
1193 kmem_free(zp_array
, max_array
* sizeof (znode_t
*));
1197 #endif /* HAVE_D_PRUNE_ALIASES */
1200 * The ARC has requested that the filesystem drop entries from the dentry
1201 * and inode caches. This can occur when the ARC needs to free meta data
1202 * blocks but can't because they are all pinned by entries in these caches.
1205 zfs_sb_prune(struct super_block
*sb
, unsigned long nr_to_scan
, int *objects
)
1207 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1209 #if defined(HAVE_SHRINK) || defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1210 struct shrinker
*shrinker
= &sb
->s_shrink
;
1211 struct shrink_control sc
= {
1212 .nr_to_scan
= nr_to_scan
,
1213 .gfp_mask
= GFP_KERNEL
,
1219 #if defined(HAVE_SPLIT_SHRINKER_CALLBACK) && \
1220 defined(SHRINK_CONTROL_HAS_NID) && \
1221 defined(SHRINKER_NUMA_AWARE)
1222 if (sb
->s_shrink
.flags
& SHRINKER_NUMA_AWARE
) {
1224 for_each_online_node(sc
.nid
) {
1225 *objects
+= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1228 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1231 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1232 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1233 #elif defined(HAVE_SHRINK)
1234 *objects
= (*shrinker
->shrink
)(shrinker
, &sc
);
1235 #elif defined(HAVE_D_PRUNE_ALIASES)
1236 #define D_PRUNE_ALIASES_IS_DEFAULT
1237 *objects
= zfs_sb_prune_aliases(zsb
, nr_to_scan
);
1239 #error "No available dentry and inode cache pruning mechanism."
1242 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1243 #undef D_PRUNE_ALIASES_IS_DEFAULT
1245 * Fall back to zfs_sb_prune_aliases if the kernel's per-superblock
1246 * shrinker couldn't free anything, possibly due to the inodes being
1247 * allocated in a different memcg.
1250 *objects
= zfs_sb_prune_aliases(zsb
, nr_to_scan
);
1255 dprintf_ds(zsb
->z_os
->os_dsl_dataset
,
1256 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1257 nr_to_scan
, *objects
, error
);
1261 EXPORT_SYMBOL(zfs_sb_prune
);
1264 * Teardown the zfs_sb_t.
1266 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1267 * and 'z_teardown_inactive_lock' held.
1270 zfs_sb_teardown(zfs_sb_t
*zsb
, boolean_t unmounting
)
1275 * If someone has not already unmounted this file system,
1276 * drain the iput_taskq to ensure all active references to the
1277 * zfs_sb_t have been handled only then can it be safely destroyed.
1281 * If we're unmounting we have to wait for the list to
1284 * If we're not unmounting there's no guarantee the list
1285 * will drain completely, but iputs run from the taskq
1286 * may add the parents of dir-based xattrs to the taskq
1287 * so we want to wait for these.
1289 * We can safely read z_nr_znodes without locking because the
1290 * VFS has already blocked operations which add to the
1291 * z_all_znodes list and thus increment z_nr_znodes.
1294 while (zsb
->z_nr_znodes
> 0) {
1295 taskq_wait_outstanding(dsl_pool_iput_taskq(
1296 dmu_objset_pool(zsb
->z_os
)), 0);
1297 if (++round
> 1 && !unmounting
)
1302 rrm_enter(&zsb
->z_teardown_lock
, RW_WRITER
, FTAG
);
1306 * We purge the parent filesystem's super block as the
1307 * parent filesystem and all of its snapshots have their
1308 * inode's super block set to the parent's filesystem's
1309 * super block. Note, 'z_parent' is self referential
1310 * for non-snapshots.
1312 shrink_dcache_sb(zsb
->z_parent
->z_sb
);
1316 * Close the zil. NB: Can't close the zil while zfs_inactive
1317 * threads are blocked as zil_close can call zfs_inactive.
1320 zil_close(zsb
->z_log
);
1324 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_WRITER
);
1327 * If we are not unmounting (ie: online recv) and someone already
1328 * unmounted this file system while we were doing the switcheroo,
1329 * or a reopen of z_os failed then just bail out now.
1331 if (!unmounting
&& (zsb
->z_unmounted
|| zsb
->z_os
== NULL
)) {
1332 rw_exit(&zsb
->z_teardown_inactive_lock
);
1333 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1334 return (SET_ERROR(EIO
));
1338 * At this point there are no VFS ops active, and any new VFS ops
1339 * will fail with EIO since we have z_teardown_lock for writer (only
1340 * relevant for forced unmount).
1342 * Release all holds on dbufs.
1345 mutex_enter(&zsb
->z_znodes_lock
);
1346 for (zp
= list_head(&zsb
->z_all_znodes
); zp
!= NULL
;
1347 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1349 zfs_znode_dmu_fini(zp
);
1351 mutex_exit(&zsb
->z_znodes_lock
);
1355 * If we are unmounting, set the unmounted flag and let new VFS ops
1356 * unblock. zfs_inactive will have the unmounted behavior, and all
1357 * other VFS ops will fail with EIO.
1360 zsb
->z_unmounted
= B_TRUE
;
1361 rw_exit(&zsb
->z_teardown_inactive_lock
);
1362 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1366 * z_os will be NULL if there was an error in attempting to reopen
1367 * zsb, so just return as the properties had already been
1369 * unregistered and cached data had been evicted before.
1371 if (zsb
->z_os
== NULL
)
1375 * Unregister properties.
1377 zfs_unregister_callbacks(zsb
);
1382 if (dsl_dataset_is_dirty(dmu_objset_ds(zsb
->z_os
)) &&
1383 !zfs_is_readonly(zsb
))
1384 txg_wait_synced(dmu_objset_pool(zsb
->z_os
), 0);
1385 dmu_objset_evict_dbufs(zsb
->z_os
);
1389 EXPORT_SYMBOL(zfs_sb_teardown
);
1391 #if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1392 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
1393 atomic_long_t zfs_bdi_seq
= ATOMIC_LONG_INIT(0);
1397 zfs_domount(struct super_block
*sb
, zfs_mntopts_t
*zmo
, int silent
)
1399 const char *osname
= zmo
->z_osname
;
1401 struct inode
*root_inode
;
1402 uint64_t recordsize
;
1405 error
= zfs_sb_create(osname
, zmo
, &zsb
);
1409 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1410 &recordsize
, NULL
)))
1414 sb
->s_fs_info
= zsb
;
1415 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1416 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1417 sb
->s_time_gran
= 1;
1418 sb
->s_blocksize
= recordsize
;
1419 sb
->s_blocksize_bits
= ilog2(recordsize
);
1420 zsb
->z_bdi
.ra_pages
= 0;
1421 sb
->s_bdi
= &zsb
->z_bdi
;
1423 error
= -zpl_bdi_setup_and_register(&zsb
->z_bdi
, "zfs");
1427 /* Set callback operations for the file system. */
1428 sb
->s_op
= &zpl_super_operations
;
1429 sb
->s_xattr
= zpl_xattr_handlers
;
1430 sb
->s_export_op
= &zpl_export_operations
;
1432 sb
->s_d_op
= &zpl_dentry_operations
;
1433 #endif /* HAVE_S_D_OP */
1435 /* Set features for file system. */
1436 zfs_set_fuid_feature(zsb
);
1438 if (dmu_objset_is_snapshot(zsb
->z_os
)) {
1441 atime_changed_cb(zsb
, B_FALSE
);
1442 readonly_changed_cb(zsb
, B_TRUE
);
1443 if ((error
= dsl_prop_get_integer(osname
,
1444 "xattr", &pval
, NULL
)))
1446 xattr_changed_cb(zsb
, pval
);
1447 if ((error
= dsl_prop_get_integer(osname
,
1448 "acltype", &pval
, NULL
)))
1450 acltype_changed_cb(zsb
, pval
);
1451 zsb
->z_issnap
= B_TRUE
;
1452 zsb
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1453 zsb
->z_snap_defer_time
= jiffies
;
1455 mutex_enter(&zsb
->z_os
->os_user_ptr_lock
);
1456 dmu_objset_set_user(zsb
->z_os
, zsb
);
1457 mutex_exit(&zsb
->z_os
->os_user_ptr_lock
);
1459 if ((error
= zfs_sb_setup(zsb
, B_TRUE
)))
1463 /* Allocate a root inode for the filesystem. */
1464 error
= zfs_root(zsb
, &root_inode
);
1466 (void) zfs_umount(sb
);
1470 /* Allocate a root dentry for the filesystem */
1471 sb
->s_root
= d_make_root(root_inode
);
1472 if (sb
->s_root
== NULL
) {
1473 (void) zfs_umount(sb
);
1474 error
= SET_ERROR(ENOMEM
);
1481 zsb
->z_arc_prune
= arc_add_prune_callback(zpl_prune_sb
, sb
);
1484 dmu_objset_disown(zsb
->z_os
, zsb
);
1487 * make sure we don't have dangling sb->s_fs_info which
1488 * zfs_preumount will use.
1490 sb
->s_fs_info
= NULL
;
1495 EXPORT_SYMBOL(zfs_domount
);
1498 * Called when an unmount is requested and certain sanity checks have
1499 * already passed. At this point no dentries or inodes have been reclaimed
1500 * from their respective caches. We drop the extra reference on the .zfs
1501 * control directory to allow everything to be reclaimed. All snapshots
1502 * must already have been unmounted to reach this point.
1505 zfs_preumount(struct super_block
*sb
)
1507 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1509 /* zsb is NULL when zfs_domount fails during mount */
1511 zfsctl_destroy(sb
->s_fs_info
);
1513 * Wait for iput_async before entering evict_inodes in
1514 * generic_shutdown_super. The reason we must finish before
1515 * evict_inodes is when lazytime is on, or when zfs_purgedir
1516 * calls zfs_zget, iput would bump i_count from 0 to 1. This
1517 * would race with the i_count check in evict_inodes. This means
1518 * it could destroy the inode while we are still using it.
1520 * We wait for two passes. xattr directories in the first pass
1521 * may add xattr entries in zfs_purgedir, so in the second pass
1522 * we wait for them. We don't use taskq_wait here because it is
1523 * a pool wide taskq. Other mounted filesystems can constantly
1524 * do iput_async and there's no guarantee when taskq will be
1527 taskq_wait_outstanding(dsl_pool_iput_taskq(
1528 dmu_objset_pool(zsb
->z_os
)), 0);
1529 taskq_wait_outstanding(dsl_pool_iput_taskq(
1530 dmu_objset_pool(zsb
->z_os
)), 0);
1533 EXPORT_SYMBOL(zfs_preumount
);
1536 * Called once all other unmount released tear down has occurred.
1537 * It is our responsibility to release any remaining infrastructure.
1541 zfs_umount(struct super_block
*sb
)
1543 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1546 arc_remove_prune_callback(zsb
->z_arc_prune
);
1547 VERIFY(zfs_sb_teardown(zsb
, B_TRUE
) == 0);
1549 bdi_destroy(sb
->s_bdi
);
1552 * z_os will be NULL if there was an error in
1553 * attempting to reopen zsb.
1557 * Unset the objset user_ptr.
1559 mutex_enter(&os
->os_user_ptr_lock
);
1560 dmu_objset_set_user(os
, NULL
);
1561 mutex_exit(&os
->os_user_ptr_lock
);
1564 * Finally release the objset
1566 dmu_objset_disown(os
, zsb
);
1572 EXPORT_SYMBOL(zfs_umount
);
1575 zfs_remount(struct super_block
*sb
, int *flags
, zfs_mntopts_t
*zmo
)
1577 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1580 zfs_unregister_callbacks(zsb
);
1581 error
= zfs_register_callbacks(zsb
);
1585 EXPORT_SYMBOL(zfs_remount
);
1588 zfs_vget(struct super_block
*sb
, struct inode
**ipp
, fid_t
*fidp
)
1590 zfs_sb_t
*zsb
= sb
->s_fs_info
;
1592 uint64_t object
= 0;
1593 uint64_t fid_gen
= 0;
1600 if (fidp
->fid_len
== SHORT_FID_LEN
|| fidp
->fid_len
== LONG_FID_LEN
) {
1601 zfid_short_t
*zfid
= (zfid_short_t
*)fidp
;
1603 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
1604 object
|= ((uint64_t)zfid
->zf_object
[i
]) << (8 * i
);
1606 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
1607 fid_gen
|= ((uint64_t)zfid
->zf_gen
[i
]) << (8 * i
);
1609 return (SET_ERROR(EINVAL
));
1612 /* LONG_FID_LEN means snapdirs */
1613 if (fidp
->fid_len
== LONG_FID_LEN
) {
1614 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1615 uint64_t objsetid
= 0;
1616 uint64_t setgen
= 0;
1618 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1619 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1621 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1622 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1624 if (objsetid
!= ZFSCTL_INO_SNAPDIRS
- object
) {
1625 dprintf("snapdir fid: objsetid (%llu) != "
1626 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
1627 objsetid
, ZFSCTL_INO_SNAPDIRS
, object
);
1629 return (SET_ERROR(EINVAL
));
1632 if (fid_gen
> 1 || setgen
!= 0) {
1633 dprintf("snapdir fid: fid_gen (%llu) and setgen "
1634 "(%llu)\n", fid_gen
, setgen
);
1635 return (SET_ERROR(EINVAL
));
1638 return (zfsctl_snapdir_vget(sb
, objsetid
, fid_gen
, ipp
));
1642 /* A zero fid_gen means we are in the .zfs control directories */
1644 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1645 *ipp
= zsb
->z_ctldir
;
1646 ASSERT(*ipp
!= NULL
);
1647 if (object
== ZFSCTL_INO_SNAPDIR
) {
1648 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
,
1649 0, kcred
, NULL
, NULL
) == 0);
1657 gen_mask
= -1ULL >> (64 - 8 * i
);
1659 dprintf("getting %llu [%llu mask %llx]\n", object
, fid_gen
, gen_mask
);
1660 if ((err
= zfs_zget(zsb
, object
, &zp
))) {
1665 /* Don't export xattr stuff */
1666 if (zp
->z_pflags
& ZFS_XATTR
) {
1669 return (SET_ERROR(ENOENT
));
1672 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
), &zp_gen
,
1674 zp_gen
= zp_gen
& gen_mask
;
1677 if ((fid_gen
== 0) && (zsb
->z_root
== object
))
1679 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1680 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen
,
1684 return (SET_ERROR(ENOENT
));
1689 zfs_inode_update(ITOZ(*ipp
));
1694 EXPORT_SYMBOL(zfs_vget
);
1697 * Block out VFS ops and close zfs_sb_t
1699 * Note, if successful, then we return with the 'z_teardown_lock' and
1700 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1701 * dataset and objset intact so that they can be atomically handed off during
1702 * a subsequent rollback or recv operation and the resume thereafter.
1705 zfs_suspend_fs(zfs_sb_t
*zsb
)
1709 if ((error
= zfs_sb_teardown(zsb
, B_FALSE
)) != 0)
1714 EXPORT_SYMBOL(zfs_suspend_fs
);
1717 * Reopen zfs_sb_t and release VFS ops.
1720 zfs_resume_fs(zfs_sb_t
*zsb
, dsl_dataset_t
*ds
)
1724 uint64_t sa_obj
= 0;
1726 ASSERT(RRM_WRITE_HELD(&zsb
->z_teardown_lock
));
1727 ASSERT(RW_WRITE_HELD(&zsb
->z_teardown_inactive_lock
));
1730 * We already own this, so just update the objset_t, as the one we
1731 * had before may have been evicted.
1733 VERIFY3P(ds
->ds_owner
, ==, zsb
);
1734 VERIFY(dsl_dataset_long_held(ds
));
1735 VERIFY0(dmu_objset_from_ds(ds
, &zsb
->z_os
));
1738 * Make sure version hasn't changed
1741 err
= zfs_get_zplprop(zsb
->z_os
, ZFS_PROP_VERSION
,
1747 err
= zap_lookup(zsb
->z_os
, MASTER_NODE_OBJ
,
1748 ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1750 if (err
&& zsb
->z_version
>= ZPL_VERSION_SA
)
1753 if ((err
= sa_setup(zsb
->z_os
, sa_obj
,
1754 zfs_attr_table
, ZPL_END
, &zsb
->z_attr_table
)) != 0)
1757 if (zsb
->z_version
>= ZPL_VERSION_SA
)
1758 sa_register_update_callback(zsb
->z_os
,
1761 VERIFY(zfs_sb_setup(zsb
, B_FALSE
) == 0);
1763 zfs_set_fuid_feature(zsb
);
1764 zsb
->z_rollback_time
= jiffies
;
1767 * Attempt to re-establish all the active inodes with their
1768 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1769 * and mark it stale. This prevents a collision if a new
1770 * inode/object is created which must use the same inode
1771 * number. The stale inode will be be released when the
1772 * VFS prunes the dentry holding the remaining references
1773 * on the stale inode.
1775 mutex_enter(&zsb
->z_znodes_lock
);
1776 for (zp
= list_head(&zsb
->z_all_znodes
); zp
;
1777 zp
= list_next(&zsb
->z_all_znodes
, zp
)) {
1778 err2
= zfs_rezget(zp
);
1780 remove_inode_hash(ZTOI(zp
));
1781 zp
->z_is_stale
= B_TRUE
;
1784 mutex_exit(&zsb
->z_znodes_lock
);
1787 /* release the VFS ops */
1788 rw_exit(&zsb
->z_teardown_inactive_lock
);
1789 rrm_exit(&zsb
->z_teardown_lock
, FTAG
);
1793 * Since we couldn't setup the sa framework, try to force
1794 * unmount this file system.
1797 (void) zfs_umount(zsb
->z_sb
);
1801 EXPORT_SYMBOL(zfs_resume_fs
);
1804 zfs_set_version(zfs_sb_t
*zsb
, uint64_t newvers
)
1807 objset_t
*os
= zsb
->z_os
;
1810 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1811 return (SET_ERROR(EINVAL
));
1813 if (newvers
< zsb
->z_version
)
1814 return (SET_ERROR(EINVAL
));
1816 if (zfs_spa_version_map(newvers
) >
1817 spa_version(dmu_objset_spa(zsb
->z_os
)))
1818 return (SET_ERROR(ENOTSUP
));
1820 tx
= dmu_tx_create(os
);
1821 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1822 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1823 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1825 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1827 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1833 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1834 8, 1, &newvers
, tx
);
1841 if (newvers
>= ZPL_VERSION_SA
&& !zsb
->z_use_sa
) {
1844 ASSERT3U(spa_version(dmu_objset_spa(zsb
->z_os
)), >=,
1846 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1847 DMU_OT_NONE
, 0, tx
);
1849 error
= zap_add(os
, MASTER_NODE_OBJ
,
1850 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1853 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
1854 sa_register_update_callback(os
, zfs_sa_upgrade
);
1857 spa_history_log_internal_ds(dmu_objset_ds(os
), "upgrade", tx
,
1858 "from %llu to %llu", zsb
->z_version
, newvers
);
1862 zsb
->z_version
= newvers
;
1864 zfs_set_fuid_feature(zsb
);
1868 EXPORT_SYMBOL(zfs_set_version
);
1871 * Read a property stored within the master node.
1874 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
1877 int error
= SET_ERROR(ENOENT
);
1880 * Look up the file system's value for the property. For the
1881 * version property, we look up a slightly different string.
1883 if (prop
== ZFS_PROP_VERSION
)
1884 pname
= ZPL_VERSION_STR
;
1886 pname
= zfs_prop_to_name(prop
);
1889 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
1891 if (error
== ENOENT
) {
1892 /* No value set, use the default value */
1894 case ZFS_PROP_VERSION
:
1895 *value
= ZPL_VERSION
;
1897 case ZFS_PROP_NORMALIZE
:
1898 case ZFS_PROP_UTF8ONLY
:
1902 *value
= ZFS_CASE_SENSITIVE
;
1904 case ZFS_PROP_ACLTYPE
:
1905 *value
= ZFS_ACLTYPE_OFF
;
1914 EXPORT_SYMBOL(zfs_get_zplprop
);
1917 * Return true if the coresponding vfs's unmounted flag is set.
1918 * Otherwise return false.
1919 * If this function returns true we know VFS unmount has been initiated.
1922 zfs_get_vfs_flag_unmounted(objset_t
*os
)
1925 boolean_t unmounted
= B_FALSE
;
1927 ASSERT(dmu_objset_type(os
) == DMU_OST_ZFS
);
1929 mutex_enter(&os
->os_user_ptr_lock
);
1930 zfvp
= dmu_objset_get_user(os
);
1931 if (zfvp
!= NULL
&& zfvp
->z_unmounted
)
1933 mutex_exit(&os
->os_user_ptr_lock
);
1943 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
1944 register_filesystem(&zpl_fs_type
);
1951 * we don't use outstanding because zpl_posix_acl_free might add more.
1953 taskq_wait(system_delay_taskq
);
1954 taskq_wait(system_taskq
);
1955 unregister_filesystem(&zpl_fs_type
);