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 zfsvfs_t
*zfsvfs
= 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(zfsvfs
->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 (zfsvfs
->z_log
!= NULL
)
110 zil_commit(zfsvfs
->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.
126 zfs_is_readonly(zfsvfs_t
*zfsvfs
)
128 return (!!(zfsvfs
->z_sb
->s_flags
& MS_RDONLY
));
132 atime_changed_cb(void *arg
, uint64_t newval
)
134 ((zfsvfs_t
*)arg
)->z_atime
= newval
;
138 relatime_changed_cb(void *arg
, uint64_t newval
)
140 ((zfsvfs_t
*)arg
)->z_relatime
= newval
;
144 xattr_changed_cb(void *arg
, uint64_t newval
)
146 zfsvfs_t
*zfsvfs
= arg
;
148 if (newval
== ZFS_XATTR_OFF
) {
149 zfsvfs
->z_flags
&= ~ZSB_XATTR
;
151 zfsvfs
->z_flags
|= ZSB_XATTR
;
153 if (newval
== ZFS_XATTR_SA
)
154 zfsvfs
->z_xattr_sa
= B_TRUE
;
156 zfsvfs
->z_xattr_sa
= B_FALSE
;
161 acltype_changed_cb(void *arg
, uint64_t newval
)
163 zfsvfs_t
*zfsvfs
= arg
;
166 case ZFS_ACLTYPE_OFF
:
167 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_OFF
;
168 zfsvfs
->z_sb
->s_flags
&= ~MS_POSIXACL
;
170 case ZFS_ACLTYPE_POSIXACL
:
171 #ifdef CONFIG_FS_POSIX_ACL
172 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_POSIXACL
;
173 zfsvfs
->z_sb
->s_flags
|= MS_POSIXACL
;
175 zfsvfs
->z_acl_type
= ZFS_ACLTYPE_OFF
;
176 zfsvfs
->z_sb
->s_flags
&= ~MS_POSIXACL
;
177 #endif /* CONFIG_FS_POSIX_ACL */
185 blksz_changed_cb(void *arg
, uint64_t newval
)
187 zfsvfs_t
*zfsvfs
= arg
;
188 ASSERT3U(newval
, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs
->z_os
)));
189 ASSERT3U(newval
, >=, SPA_MINBLOCKSIZE
);
190 ASSERT(ISP2(newval
));
192 zfsvfs
->z_max_blksz
= newval
;
196 readonly_changed_cb(void *arg
, uint64_t newval
)
198 zfsvfs_t
*zfsvfs
= arg
;
199 struct super_block
*sb
= zfsvfs
->z_sb
;
205 sb
->s_flags
|= MS_RDONLY
;
207 sb
->s_flags
&= ~MS_RDONLY
;
211 devices_changed_cb(void *arg
, uint64_t newval
)
216 setuid_changed_cb(void *arg
, uint64_t newval
)
221 exec_changed_cb(void *arg
, uint64_t newval
)
226 nbmand_changed_cb(void *arg
, uint64_t newval
)
228 zfsvfs_t
*zfsvfs
= arg
;
229 struct super_block
*sb
= zfsvfs
->z_sb
;
235 sb
->s_flags
|= MS_MANDLOCK
;
237 sb
->s_flags
&= ~MS_MANDLOCK
;
241 snapdir_changed_cb(void *arg
, uint64_t newval
)
243 ((zfsvfs_t
*)arg
)->z_show_ctldir
= newval
;
247 vscan_changed_cb(void *arg
, uint64_t newval
)
249 ((zfsvfs_t
*)arg
)->z_vscan
= newval
;
253 acl_inherit_changed_cb(void *arg
, uint64_t newval
)
255 ((zfsvfs_t
*)arg
)->z_acl_inherit
= newval
;
259 zfs_register_callbacks(zfsvfs_t
*zfsvfs
)
261 struct dsl_dataset
*ds
= NULL
;
262 objset_t
*os
= zfsvfs
->z_os
;
263 zfs_mntopts_t
*zmo
= zfsvfs
->z_mntopts
;
270 * The act of registering our callbacks will destroy any mount
271 * options we may have. In order to enable temporary overrides
272 * of mount options, we stash away the current values and
273 * restore them after we register the callbacks.
275 if (zfs_is_readonly(zfsvfs
) || !spa_writeable(dmu_objset_spa(os
))) {
276 zmo
->z_do_readonly
= B_TRUE
;
277 zmo
->z_readonly
= B_TRUE
;
281 * Register property callbacks.
283 * It would probably be fine to just check for i/o error from
284 * the first prop_register(), but I guess I like to go
287 ds
= dmu_objset_ds(os
);
288 dsl_pool_config_enter(dmu_objset_pool(os
), FTAG
);
289 error
= dsl_prop_register(ds
,
290 zfs_prop_to_name(ZFS_PROP_ATIME
), atime_changed_cb
, zfsvfs
);
291 error
= error
? error
: dsl_prop_register(ds
,
292 zfs_prop_to_name(ZFS_PROP_RELATIME
), relatime_changed_cb
, zfsvfs
);
293 error
= error
? error
: dsl_prop_register(ds
,
294 zfs_prop_to_name(ZFS_PROP_XATTR
), xattr_changed_cb
, zfsvfs
);
295 error
= error
? error
: dsl_prop_register(ds
,
296 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
), blksz_changed_cb
, zfsvfs
);
297 error
= error
? error
: dsl_prop_register(ds
,
298 zfs_prop_to_name(ZFS_PROP_READONLY
), readonly_changed_cb
, zfsvfs
);
299 error
= error
? error
: dsl_prop_register(ds
,
300 zfs_prop_to_name(ZFS_PROP_DEVICES
), devices_changed_cb
, zfsvfs
);
301 error
= error
? error
: dsl_prop_register(ds
,
302 zfs_prop_to_name(ZFS_PROP_SETUID
), setuid_changed_cb
, zfsvfs
);
303 error
= error
? error
: dsl_prop_register(ds
,
304 zfs_prop_to_name(ZFS_PROP_EXEC
), exec_changed_cb
, zfsvfs
);
305 error
= error
? error
: dsl_prop_register(ds
,
306 zfs_prop_to_name(ZFS_PROP_SNAPDIR
), snapdir_changed_cb
, zfsvfs
);
307 error
= error
? error
: dsl_prop_register(ds
,
308 zfs_prop_to_name(ZFS_PROP_ACLTYPE
), acltype_changed_cb
, zfsvfs
);
309 error
= error
? error
: dsl_prop_register(ds
,
310 zfs_prop_to_name(ZFS_PROP_ACLINHERIT
), acl_inherit_changed_cb
,
312 error
= error
? error
: dsl_prop_register(ds
,
313 zfs_prop_to_name(ZFS_PROP_VSCAN
), vscan_changed_cb
, zfsvfs
);
314 error
= error
? error
: dsl_prop_register(ds
,
315 zfs_prop_to_name(ZFS_PROP_NBMAND
), nbmand_changed_cb
, zfsvfs
);
316 dsl_pool_config_exit(dmu_objset_pool(os
), FTAG
);
321 * Invoke our callbacks to restore temporary mount options.
323 if (zmo
->z_do_readonly
)
324 readonly_changed_cb(zfsvfs
, zmo
->z_readonly
);
325 if (zmo
->z_do_setuid
)
326 setuid_changed_cb(zfsvfs
, zmo
->z_setuid
);
328 exec_changed_cb(zfsvfs
, zmo
->z_exec
);
329 if (zmo
->z_do_devices
)
330 devices_changed_cb(zfsvfs
, zmo
->z_devices
);
332 xattr_changed_cb(zfsvfs
, zmo
->z_xattr
);
334 atime_changed_cb(zfsvfs
, zmo
->z_atime
);
335 if (zmo
->z_do_relatime
)
336 relatime_changed_cb(zfsvfs
, zmo
->z_relatime
);
337 if (zmo
->z_do_nbmand
)
338 nbmand_changed_cb(zfsvfs
, zmo
->z_nbmand
);
343 dsl_prop_unregister_all(ds
, zfsvfs
);
348 zfs_space_delta_cb(dmu_object_type_t bonustype
, void *data
,
349 uint64_t *userp
, uint64_t *groupp
)
352 * Is it a valid type of object to track?
354 if (bonustype
!= DMU_OT_ZNODE
&& bonustype
!= DMU_OT_SA
)
355 return (SET_ERROR(ENOENT
));
358 * If we have a NULL data pointer
359 * then assume the id's aren't changing and
360 * return EEXIST to the dmu to let it know to
364 return (SET_ERROR(EEXIST
));
366 if (bonustype
== DMU_OT_ZNODE
) {
367 znode_phys_t
*znp
= data
;
368 *userp
= znp
->zp_uid
;
369 *groupp
= znp
->zp_gid
;
372 sa_hdr_phys_t
*sap
= data
;
373 sa_hdr_phys_t sa
= *sap
;
374 boolean_t swap
= B_FALSE
;
376 ASSERT(bonustype
== DMU_OT_SA
);
378 if (sa
.sa_magic
== 0) {
380 * This should only happen for newly created
381 * files that haven't had the znode data filled
388 if (sa
.sa_magic
== BSWAP_32(SA_MAGIC
)) {
389 sa
.sa_magic
= SA_MAGIC
;
390 sa
.sa_layout_info
= BSWAP_16(sa
.sa_layout_info
);
393 VERIFY3U(sa
.sa_magic
, ==, SA_MAGIC
);
396 hdrsize
= sa_hdrsize(&sa
);
397 VERIFY3U(hdrsize
, >=, sizeof (sa_hdr_phys_t
));
398 *userp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
400 *groupp
= *((uint64_t *)((uintptr_t)data
+ hdrsize
+
403 *userp
= BSWAP_64(*userp
);
404 *groupp
= BSWAP_64(*groupp
);
411 fuidstr_to_sid(zfsvfs_t
*zfsvfs
, const char *fuidstr
,
412 char *domainbuf
, int buflen
, uid_t
*ridp
)
417 fuid
= strtonum(fuidstr
, NULL
);
419 domain
= zfs_fuid_find_by_idx(zfsvfs
, FUID_INDEX(fuid
));
421 (void) strlcpy(domainbuf
, domain
, buflen
);
424 *ridp
= FUID_RID(fuid
);
428 zfs_userquota_prop_to_obj(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
)
431 case ZFS_PROP_USERUSED
:
432 case ZFS_PROP_USEROBJUSED
:
433 return (DMU_USERUSED_OBJECT
);
434 case ZFS_PROP_GROUPUSED
:
435 case ZFS_PROP_GROUPOBJUSED
:
436 return (DMU_GROUPUSED_OBJECT
);
437 case ZFS_PROP_USERQUOTA
:
438 return (zfsvfs
->z_userquota_obj
);
439 case ZFS_PROP_GROUPQUOTA
:
440 return (zfsvfs
->z_groupquota_obj
);
441 case ZFS_PROP_USEROBJQUOTA
:
442 return (zfsvfs
->z_userobjquota_obj
);
443 case ZFS_PROP_GROUPOBJQUOTA
:
444 return (zfsvfs
->z_groupobjquota_obj
);
446 return (ZFS_NO_OBJECT
);
451 zfs_userspace_many(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
452 uint64_t *cookiep
, void *vbuf
, uint64_t *bufsizep
)
457 zfs_useracct_t
*buf
= vbuf
;
461 if (!dmu_objset_userspace_present(zfsvfs
->z_os
))
462 return (SET_ERROR(ENOTSUP
));
464 if ((type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
||
465 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
) &&
466 !dmu_objset_userobjspace_present(zfsvfs
->z_os
))
467 return (SET_ERROR(ENOTSUP
));
469 obj
= zfs_userquota_prop_to_obj(zfsvfs
, type
);
470 if (obj
== ZFS_NO_OBJECT
) {
475 if (type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
)
476 offset
= DMU_OBJACCT_PREFIX_LEN
;
478 for (zap_cursor_init_serialized(&zc
, zfsvfs
->z_os
, obj
, *cookiep
);
479 (error
= zap_cursor_retrieve(&zc
, &za
)) == 0;
480 zap_cursor_advance(&zc
)) {
481 if ((uintptr_t)buf
- (uintptr_t)vbuf
+ sizeof (zfs_useracct_t
) >
486 * skip object quota (with zap name prefix DMU_OBJACCT_PREFIX)
487 * when dealing with block quota and vice versa.
489 if ((offset
> 0) != (strncmp(za
.za_name
, DMU_OBJACCT_PREFIX
,
490 DMU_OBJACCT_PREFIX_LEN
) == 0))
493 fuidstr_to_sid(zfsvfs
, za
.za_name
+ offset
,
494 buf
->zu_domain
, sizeof (buf
->zu_domain
), &buf
->zu_rid
);
496 buf
->zu_space
= za
.za_first_integer
;
502 ASSERT3U((uintptr_t)buf
- (uintptr_t)vbuf
, <=, *bufsizep
);
503 *bufsizep
= (uintptr_t)buf
- (uintptr_t)vbuf
;
504 *cookiep
= zap_cursor_serialize(&zc
);
505 zap_cursor_fini(&zc
);
510 * buf must be big enough (eg, 32 bytes)
513 id_to_fuidstr(zfsvfs_t
*zfsvfs
, const char *domain
, uid_t rid
,
514 char *buf
, boolean_t addok
)
519 if (domain
&& domain
[0]) {
520 domainid
= zfs_fuid_find_by_domain(zfsvfs
, domain
, NULL
, addok
);
522 return (SET_ERROR(ENOENT
));
524 fuid
= FUID_ENCODE(domainid
, rid
);
525 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
530 zfs_userspace_one(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
531 const char *domain
, uint64_t rid
, uint64_t *valp
)
533 char buf
[20 + DMU_OBJACCT_PREFIX_LEN
];
540 if (!dmu_objset_userspace_present(zfsvfs
->z_os
))
541 return (SET_ERROR(ENOTSUP
));
543 if ((type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
||
544 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
) &&
545 !dmu_objset_userobjspace_present(zfsvfs
->z_os
))
546 return (SET_ERROR(ENOTSUP
));
548 obj
= zfs_userquota_prop_to_obj(zfsvfs
, type
);
549 if (obj
== ZFS_NO_OBJECT
)
552 if (type
== ZFS_PROP_USEROBJUSED
|| type
== ZFS_PROP_GROUPOBJUSED
) {
553 strlcpy(buf
, DMU_OBJACCT_PREFIX
, DMU_OBJACCT_PREFIX_LEN
);
554 offset
= DMU_OBJACCT_PREFIX_LEN
;
557 err
= id_to_fuidstr(zfsvfs
, domain
, rid
, buf
+ offset
, B_FALSE
);
561 err
= zap_lookup(zfsvfs
->z_os
, obj
, buf
, 8, 1, valp
);
568 zfs_set_userquota(zfsvfs_t
*zfsvfs
, zfs_userquota_prop_t type
,
569 const char *domain
, uint64_t rid
, uint64_t quota
)
575 boolean_t fuid_dirtied
;
577 if (zfsvfs
->z_version
< ZPL_VERSION_USERSPACE
)
578 return (SET_ERROR(ENOTSUP
));
581 case ZFS_PROP_USERQUOTA
:
582 objp
= &zfsvfs
->z_userquota_obj
;
584 case ZFS_PROP_GROUPQUOTA
:
585 objp
= &zfsvfs
->z_groupquota_obj
;
587 case ZFS_PROP_USEROBJQUOTA
:
588 objp
= &zfsvfs
->z_userobjquota_obj
;
590 case ZFS_PROP_GROUPOBJQUOTA
:
591 objp
= &zfsvfs
->z_groupobjquota_obj
;
594 return (SET_ERROR(EINVAL
));
597 err
= id_to_fuidstr(zfsvfs
, domain
, rid
, buf
, B_TRUE
);
600 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
602 tx
= dmu_tx_create(zfsvfs
->z_os
);
603 dmu_tx_hold_zap(tx
, *objp
? *objp
: DMU_NEW_OBJECT
, B_TRUE
, NULL
);
605 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
606 zfs_userquota_prop_prefixes
[type
]);
609 zfs_fuid_txhold(zfsvfs
, tx
);
610 err
= dmu_tx_assign(tx
, TXG_WAIT
);
616 mutex_enter(&zfsvfs
->z_lock
);
618 *objp
= zap_create(zfsvfs
->z_os
, DMU_OT_USERGROUP_QUOTA
,
620 VERIFY(0 == zap_add(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
621 zfs_userquota_prop_prefixes
[type
], 8, 1, objp
, tx
));
623 mutex_exit(&zfsvfs
->z_lock
);
626 err
= zap_remove(zfsvfs
->z_os
, *objp
, buf
, tx
);
630 err
= zap_update(zfsvfs
->z_os
, *objp
, buf
, 8, 1, "a
, tx
);
634 zfs_fuid_sync(zfsvfs
, tx
);
640 zfs_fuid_overobjquota(zfsvfs_t
*zfsvfs
, boolean_t isgroup
, uint64_t fuid
)
642 char buf
[20 + DMU_OBJACCT_PREFIX_LEN
];
643 uint64_t used
, quota
, usedobj
, quotaobj
;
646 if (!dmu_objset_userobjspace_present(zfsvfs
->z_os
)) {
647 if (dmu_objset_userobjspace_upgradable(zfsvfs
->z_os
))
648 dmu_objset_userobjspace_upgrade(zfsvfs
->z_os
);
652 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
653 quotaobj
= isgroup
? zfsvfs
->z_groupobjquota_obj
:
654 zfsvfs
->z_userobjquota_obj
;
655 if (quotaobj
== 0 || zfsvfs
->z_replay
)
658 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
659 err
= zap_lookup(zfsvfs
->z_os
, quotaobj
, buf
, 8, 1, "a
);
663 (void) sprintf(buf
, DMU_OBJACCT_PREFIX
"%llx", (longlong_t
)fuid
);
664 err
= zap_lookup(zfsvfs
->z_os
, usedobj
, buf
, 8, 1, &used
);
667 return (used
>= quota
);
671 zfs_fuid_overquota(zfsvfs_t
*zfsvfs
, boolean_t isgroup
, uint64_t fuid
)
674 uint64_t used
, quota
, usedobj
, quotaobj
;
677 usedobj
= isgroup
? DMU_GROUPUSED_OBJECT
: DMU_USERUSED_OBJECT
;
678 quotaobj
= isgroup
? zfsvfs
->z_groupquota_obj
: zfsvfs
->z_userquota_obj
;
680 if (quotaobj
== 0 || zfsvfs
->z_replay
)
683 (void) sprintf(buf
, "%llx", (longlong_t
)fuid
);
684 err
= zap_lookup(zfsvfs
->z_os
, quotaobj
, buf
, 8, 1, "a
);
688 err
= zap_lookup(zfsvfs
->z_os
, usedobj
, buf
, 8, 1, &used
);
691 return (used
>= quota
);
695 zfs_owner_overquota(zfsvfs_t
*zfsvfs
, znode_t
*zp
, boolean_t isgroup
)
699 struct inode
*ip
= ZTOI(zp
);
701 quotaobj
= isgroup
? zfsvfs
->z_groupquota_obj
: zfsvfs
->z_userquota_obj
;
703 fuid
= isgroup
? KGID_TO_SGID(ip
->i_gid
) : KUID_TO_SUID(ip
->i_uid
);
705 if (quotaobj
== 0 || zfsvfs
->z_replay
)
708 return (zfs_fuid_overquota(zfsvfs
, isgroup
, fuid
));
712 zfs_mntopts_alloc(void)
714 return (kmem_zalloc(sizeof (zfs_mntopts_t
), KM_SLEEP
));
718 zfs_mntopts_free(zfs_mntopts_t
*zmo
)
721 strfree(zmo
->z_osname
);
724 strfree(zmo
->z_mntpoint
);
726 kmem_free(zmo
, sizeof (zfs_mntopts_t
));
730 zfsvfs_create(const char *osname
, zfs_mntopts_t
*zmo
, zfsvfs_t
**zfvp
)
738 zfsvfs
= kmem_zalloc(sizeof (zfsvfs_t
), KM_SLEEP
);
741 * Optional temporary mount options, free'd in zfsvfs_free().
743 zfsvfs
->z_mntopts
= (zmo
? zmo
: zfs_mntopts_alloc());
746 * We claim to always be readonly so we can open snapshots;
747 * other ZPL code will prevent us from writing to snapshots.
749 error
= dmu_objset_own(osname
, DMU_OST_ZFS
, B_TRUE
, zfsvfs
, &os
);
754 * Initialize the zfs-specific filesystem structure.
755 * Should probably make this a kmem cache, shuffle fields.
758 zfsvfs
->z_parent
= zfsvfs
;
759 zfsvfs
->z_max_blksz
= SPA_OLD_MAXBLOCKSIZE
;
760 zfsvfs
->z_show_ctldir
= ZFS_SNAPDIR_VISIBLE
;
763 error
= zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zfsvfs
->z_version
);
766 } else if (zfsvfs
->z_version
> ZPL_VERSION
) {
767 error
= SET_ERROR(ENOTSUP
);
770 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &zval
)) != 0)
772 zfsvfs
->z_norm
= (int)zval
;
774 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &zval
)) != 0)
776 zfsvfs
->z_utf8
= (zval
!= 0);
778 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_CASE
, &zval
)) != 0)
780 zfsvfs
->z_case
= (uint_t
)zval
;
782 if ((error
= zfs_get_zplprop(os
, ZFS_PROP_ACLTYPE
, &zval
)) != 0)
784 zfsvfs
->z_acl_type
= (uint_t
)zval
;
787 * Fold case on file systems that are always or sometimes case
790 if (zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
||
791 zfsvfs
->z_case
== ZFS_CASE_MIXED
)
792 zfsvfs
->z_norm
|= U8_TEXTPREP_TOUPPER
;
794 zfsvfs
->z_use_fuids
= USE_FUIDS(zfsvfs
->z_version
, zfsvfs
->z_os
);
795 zfsvfs
->z_use_sa
= USE_SA(zfsvfs
->z_version
, zfsvfs
->z_os
);
797 if (zfsvfs
->z_use_sa
) {
798 /* should either have both of these objects or none */
799 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SA_ATTRS
, 8, 1,
804 error
= zfs_get_zplprop(os
, ZFS_PROP_XATTR
, &zval
);
805 if ((error
== 0) && (zval
== ZFS_XATTR_SA
))
806 zfsvfs
->z_xattr_sa
= B_TRUE
;
809 * Pre SA versions file systems should never touch
810 * either the attribute registration or layout objects.
815 error
= sa_setup(os
, sa_obj
, zfs_attr_table
, ZPL_END
,
816 &zfsvfs
->z_attr_table
);
820 if (zfsvfs
->z_version
>= ZPL_VERSION_SA
)
821 sa_register_update_callback(os
, zfs_sa_upgrade
);
823 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_ROOT_OBJ
, 8, 1,
827 ASSERT(zfsvfs
->z_root
!= 0);
829 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_UNLINKED_SET
, 8, 1,
830 &zfsvfs
->z_unlinkedobj
);
834 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
835 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
],
836 8, 1, &zfsvfs
->z_userquota_obj
);
837 if (error
&& error
!= ENOENT
)
840 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
841 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
],
842 8, 1, &zfsvfs
->z_groupquota_obj
);
843 if (error
&& error
!= ENOENT
)
846 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
847 zfs_userquota_prop_prefixes
[ZFS_PROP_USEROBJQUOTA
],
848 8, 1, &zfsvfs
->z_userobjquota_obj
);
849 if (error
&& error
!= ENOENT
)
852 error
= zap_lookup(os
, MASTER_NODE_OBJ
,
853 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPOBJQUOTA
],
854 8, 1, &zfsvfs
->z_groupobjquota_obj
);
855 if (error
&& error
!= ENOENT
)
858 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
, 8, 1,
859 &zfsvfs
->z_fuid_obj
);
860 if (error
&& error
!= ENOENT
)
863 error
= zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_SHARES_DIR
, 8, 1,
864 &zfsvfs
->z_shares_dir
);
865 if (error
&& error
!= ENOENT
)
868 mutex_init(&zfsvfs
->z_znodes_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
869 mutex_init(&zfsvfs
->z_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
870 list_create(&zfsvfs
->z_all_znodes
, sizeof (znode_t
),
871 offsetof(znode_t
, z_link_node
));
872 rrm_init(&zfsvfs
->z_teardown_lock
, B_FALSE
);
873 rw_init(&zfsvfs
->z_teardown_inactive_lock
, NULL
, RW_DEFAULT
, NULL
);
874 rw_init(&zfsvfs
->z_fuid_lock
, NULL
, RW_DEFAULT
, NULL
);
876 size
= MIN(1 << (highbit64(zfs_object_mutex_size
)-1), ZFS_OBJ_MTX_MAX
);
877 zfsvfs
->z_hold_size
= size
;
878 zfsvfs
->z_hold_trees
= vmem_zalloc(sizeof (avl_tree_t
) * size
,
880 zfsvfs
->z_hold_locks
= vmem_zalloc(sizeof (kmutex_t
) * size
, KM_SLEEP
);
881 for (i
= 0; i
!= size
; i
++) {
882 avl_create(&zfsvfs
->z_hold_trees
[i
], zfs_znode_hold_compare
,
883 sizeof (znode_hold_t
), offsetof(znode_hold_t
, zh_node
));
884 mutex_init(&zfsvfs
->z_hold_locks
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
891 dmu_objset_disown(os
, zfsvfs
);
894 zfs_mntopts_free(zfsvfs
->z_mntopts
);
895 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
900 zfsvfs_setup(zfsvfs_t
*zfsvfs
, boolean_t mounting
)
904 error
= zfs_register_callbacks(zfsvfs
);
908 zfsvfs
->z_log
= zil_open(zfsvfs
->z_os
, zfs_get_data
);
911 * If we are not mounting (ie: online recv), then we don't
912 * have to worry about replaying the log as we blocked all
913 * operations out since we closed the ZIL.
919 * During replay we remove the read only flag to
920 * allow replays to succeed.
922 readonly
= zfs_is_readonly(zfsvfs
);
924 readonly_changed_cb(zfsvfs
, B_FALSE
);
926 zfs_unlinked_drain(zfsvfs
);
929 * Parse and replay the intent log.
931 * Because of ziltest, this must be done after
932 * zfs_unlinked_drain(). (Further note: ziltest
933 * doesn't use readonly mounts, where
934 * zfs_unlinked_drain() isn't called.) This is because
935 * ziltest causes spa_sync() to think it's committed,
936 * but actually it is not, so the intent log contains
937 * many txg's worth of changes.
939 * In particular, if object N is in the unlinked set in
940 * the last txg to actually sync, then it could be
941 * actually freed in a later txg and then reallocated
942 * in a yet later txg. This would write a "create
943 * object N" record to the intent log. Normally, this
944 * would be fine because the spa_sync() would have
945 * written out the fact that object N is free, before
946 * we could write the "create object N" intent log
949 * But when we are in ziltest mode, we advance the "open
950 * txg" without actually spa_sync()-ing the changes to
951 * disk. So we would see that object N is still
952 * allocated and in the unlinked set, and there is an
953 * intent log record saying to allocate it.
955 if (spa_writeable(dmu_objset_spa(zfsvfs
->z_os
))) {
956 if (zil_replay_disable
) {
957 zil_destroy(zfsvfs
->z_log
, B_FALSE
);
959 zfsvfs
->z_replay
= B_TRUE
;
960 zil_replay(zfsvfs
->z_os
, zfsvfs
,
962 zfsvfs
->z_replay
= B_FALSE
;
966 /* restore readonly bit */
968 readonly_changed_cb(zfsvfs
, B_TRUE
);
972 * Set the objset user_ptr to track its zfsvfs.
974 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
975 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
976 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
982 zfsvfs_free(zfsvfs_t
*zfsvfs
)
984 int i
, size
= zfsvfs
->z_hold_size
;
986 zfs_fuid_destroy(zfsvfs
);
988 mutex_destroy(&zfsvfs
->z_znodes_lock
);
989 mutex_destroy(&zfsvfs
->z_lock
);
990 list_destroy(&zfsvfs
->z_all_znodes
);
991 rrm_destroy(&zfsvfs
->z_teardown_lock
);
992 rw_destroy(&zfsvfs
->z_teardown_inactive_lock
);
993 rw_destroy(&zfsvfs
->z_fuid_lock
);
994 for (i
= 0; i
!= size
; i
++) {
995 avl_destroy(&zfsvfs
->z_hold_trees
[i
]);
996 mutex_destroy(&zfsvfs
->z_hold_locks
[i
]);
998 vmem_free(zfsvfs
->z_hold_trees
, sizeof (avl_tree_t
) * size
);
999 vmem_free(zfsvfs
->z_hold_locks
, sizeof (kmutex_t
) * size
);
1000 zfs_mntopts_free(zfsvfs
->z_mntopts
);
1001 kmem_free(zfsvfs
, sizeof (zfsvfs_t
));
1005 zfs_set_fuid_feature(zfsvfs_t
*zfsvfs
)
1007 zfsvfs
->z_use_fuids
= USE_FUIDS(zfsvfs
->z_version
, zfsvfs
->z_os
);
1008 zfsvfs
->z_use_sa
= USE_SA(zfsvfs
->z_version
, zfsvfs
->z_os
);
1012 zfs_unregister_callbacks(zfsvfs_t
*zfsvfs
)
1014 objset_t
*os
= zfsvfs
->z_os
;
1016 if (!dmu_objset_is_snapshot(os
))
1017 dsl_prop_unregister_all(dmu_objset_ds(os
), zfsvfs
);
1020 #ifdef HAVE_MLSLABEL
1022 * Check that the hex label string is appropriate for the dataset being
1023 * mounted into the global_zone proper.
1025 * Return an error if the hex label string is not default or
1026 * admin_low/admin_high. For admin_low labels, the corresponding
1027 * dataset must be readonly.
1030 zfs_check_global_label(const char *dsname
, const char *hexsl
)
1032 if (strcasecmp(hexsl
, ZFS_MLSLABEL_DEFAULT
) == 0)
1034 if (strcasecmp(hexsl
, ADMIN_HIGH
) == 0)
1036 if (strcasecmp(hexsl
, ADMIN_LOW
) == 0) {
1037 /* must be readonly */
1040 if (dsl_prop_get_integer(dsname
,
1041 zfs_prop_to_name(ZFS_PROP_READONLY
), &rdonly
, NULL
))
1042 return (SET_ERROR(EACCES
));
1043 return (rdonly
? 0 : EACCES
);
1045 return (SET_ERROR(EACCES
));
1047 #endif /* HAVE_MLSLABEL */
1050 zfs_statvfs(struct dentry
*dentry
, struct kstatfs
*statp
)
1052 zfsvfs_t
*zfsvfs
= dentry
->d_sb
->s_fs_info
;
1053 uint64_t refdbytes
, availbytes
, usedobjs
, availobjs
;
1059 dmu_objset_space(zfsvfs
->z_os
,
1060 &refdbytes
, &availbytes
, &usedobjs
, &availobjs
);
1062 fsid
= dmu_objset_fsid_guid(zfsvfs
->z_os
);
1064 * The underlying storage pool actually uses multiple block
1065 * size. Under Solaris frsize (fragment size) is reported as
1066 * the smallest block size we support, and bsize (block size)
1067 * as the filesystem's maximum block size. Unfortunately,
1068 * under Linux the fragment size and block size are often used
1069 * interchangeably. Thus we are forced to report both of them
1070 * as the filesystem's maximum block size.
1072 statp
->f_frsize
= zfsvfs
->z_max_blksz
;
1073 statp
->f_bsize
= zfsvfs
->z_max_blksz
;
1074 bshift
= fls(statp
->f_bsize
) - 1;
1077 * The following report "total" blocks of various kinds in
1078 * the file system, but reported in terms of f_bsize - the
1082 statp
->f_blocks
= (refdbytes
+ availbytes
) >> bshift
;
1083 statp
->f_bfree
= availbytes
>> bshift
;
1084 statp
->f_bavail
= statp
->f_bfree
; /* no root reservation */
1087 * statvfs() should really be called statufs(), because it assumes
1088 * static metadata. ZFS doesn't preallocate files, so the best
1089 * we can do is report the max that could possibly fit in f_files,
1090 * and that minus the number actually used in f_ffree.
1091 * For f_ffree, report the smaller of the number of object available
1092 * and the number of blocks (each object will take at least a block).
1094 statp
->f_ffree
= MIN(availobjs
, availbytes
>> DNODE_SHIFT
);
1095 statp
->f_files
= statp
->f_ffree
+ usedobjs
;
1096 statp
->f_fsid
.val
[0] = (uint32_t)fsid
;
1097 statp
->f_fsid
.val
[1] = (uint32_t)(fsid
>> 32);
1098 statp
->f_type
= ZFS_SUPER_MAGIC
;
1099 statp
->f_namelen
= MAXNAMELEN
- 1;
1102 * We have all of 40 characters to stuff a string here.
1103 * Is there anything useful we could/should provide?
1105 bzero(statp
->f_spare
, sizeof (statp
->f_spare
));
1112 zfs_root(zfsvfs_t
*zfsvfs
, struct inode
**ipp
)
1119 error
= zfs_zget(zfsvfs
, zfsvfs
->z_root
, &rootzp
);
1121 *ipp
= ZTOI(rootzp
);
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_prune_aliases(zfsvfs_t
*zfsvfs
, 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(&zfsvfs
->z_znodes_lock
);
1148 while ((zp
= list_head(&zfsvfs
->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(&zfsvfs
->z_all_znodes
, zp
);
1155 list_insert_tail(&zfsvfs
->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(&zfsvfs
->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_prune(struct super_block
*sb
, unsigned long nr_to_scan
, int *objects
)
1195 zfsvfs_t
*zfsvfs
= 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
);
1216 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1219 #elif defined(HAVE_SPLIT_SHRINKER_CALLBACK)
1220 *objects
= (*shrinker
->scan_objects
)(shrinker
, &sc
);
1221 #elif defined(HAVE_SHRINK)
1222 *objects
= (*shrinker
->shrink
)(shrinker
, &sc
);
1223 #elif defined(HAVE_D_PRUNE_ALIASES)
1224 #define D_PRUNE_ALIASES_IS_DEFAULT
1225 *objects
= zfs_prune_aliases(zfsvfs
, nr_to_scan
);
1227 #error "No available dentry and inode cache pruning mechanism."
1230 #if defined(HAVE_D_PRUNE_ALIASES) && !defined(D_PRUNE_ALIASES_IS_DEFAULT)
1231 #undef D_PRUNE_ALIASES_IS_DEFAULT
1233 * Fall back to zfs_prune_aliases if the kernel's per-superblock
1234 * shrinker couldn't free anything, possibly due to the inodes being
1235 * allocated in a different memcg.
1238 *objects
= zfs_prune_aliases(zfsvfs
, nr_to_scan
);
1243 dprintf_ds(zfsvfs
->z_os
->os_dsl_dataset
,
1244 "pruning, nr_to_scan=%lu objects=%d error=%d\n",
1245 nr_to_scan
, *objects
, error
);
1251 * Teardown the zfsvfs_t.
1253 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
1254 * and 'z_teardown_inactive_lock' held.
1257 zfsvfs_teardown(zfsvfs_t
*zfsvfs
, 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 * zfsvfs_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 (zfsvfs
->z_nr_znodes
> 0) {
1282 taskq_wait_outstanding(dsl_pool_iput_taskq(
1283 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1284 if (++round
> 1 && !unmounting
)
1289 rrm_enter(&zfsvfs
->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(zfsvfs
->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.
1306 if (zfsvfs
->z_log
) {
1307 zil_close(zfsvfs
->z_log
);
1308 zfsvfs
->z_log
= NULL
;
1311 rw_enter(&zfsvfs
->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
&& (zfsvfs
->z_unmounted
|| zfsvfs
->z_os
== NULL
)) {
1319 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1320 rrm_exit(&zfsvfs
->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(&zfsvfs
->z_znodes_lock
);
1333 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
!= NULL
;
1334 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
)) {
1336 zfs_znode_dmu_fini(zp
);
1338 mutex_exit(&zfsvfs
->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 zfsvfs
->z_unmounted
= B_TRUE
;
1348 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1349 rrm_exit(&zfsvfs
->z_teardown_lock
, FTAG
);
1353 * z_os will be NULL if there was an error in attempting to reopen
1354 * zfsvfs, so just return as the properties had already been
1356 * unregistered and cached data had been evicted before.
1358 if (zfsvfs
->z_os
== NULL
)
1362 * Unregister properties.
1364 zfs_unregister_callbacks(zfsvfs
);
1369 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs
->z_os
)) &&
1370 !zfs_is_readonly(zfsvfs
))
1371 txg_wait_synced(dmu_objset_pool(zfsvfs
->z_os
), 0);
1372 dmu_objset_evict_dbufs(zfsvfs
->z_os
);
1377 #if !defined(HAVE_2ARGS_BDI_SETUP_AND_REGISTER) && \
1378 !defined(HAVE_3ARGS_BDI_SETUP_AND_REGISTER)
1379 atomic_long_t zfs_bdi_seq
= ATOMIC_LONG_INIT(0);
1383 zfs_domount(struct super_block
*sb
, zfs_mntopts_t
*zmo
, int silent
)
1385 const char *osname
= zmo
->z_osname
;
1387 struct inode
*root_inode
;
1388 uint64_t recordsize
;
1391 error
= zfsvfs_create(osname
, zmo
, &zfsvfs
);
1395 if ((error
= dsl_prop_get_integer(osname
, "recordsize",
1396 &recordsize
, NULL
)))
1400 sb
->s_fs_info
= zfsvfs
;
1401 sb
->s_magic
= ZFS_SUPER_MAGIC
;
1402 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1403 sb
->s_time_gran
= 1;
1404 sb
->s_blocksize
= recordsize
;
1405 sb
->s_blocksize_bits
= ilog2(recordsize
);
1406 zfsvfs
->z_bdi
.ra_pages
= 0;
1407 sb
->s_bdi
= &zfsvfs
->z_bdi
;
1409 error
= -zpl_bdi_setup_and_register(&zfsvfs
->z_bdi
, "zfs");
1413 /* Set callback operations for the file system. */
1414 sb
->s_op
= &zpl_super_operations
;
1415 sb
->s_xattr
= zpl_xattr_handlers
;
1416 sb
->s_export_op
= &zpl_export_operations
;
1418 sb
->s_d_op
= &zpl_dentry_operations
;
1419 #endif /* HAVE_S_D_OP */
1421 /* Set features for file system. */
1422 zfs_set_fuid_feature(zfsvfs
);
1424 if (dmu_objset_is_snapshot(zfsvfs
->z_os
)) {
1427 atime_changed_cb(zfsvfs
, B_FALSE
);
1428 readonly_changed_cb(zfsvfs
, B_TRUE
);
1429 if ((error
= dsl_prop_get_integer(osname
,
1430 "xattr", &pval
, NULL
)))
1432 xattr_changed_cb(zfsvfs
, pval
);
1433 if ((error
= dsl_prop_get_integer(osname
,
1434 "acltype", &pval
, NULL
)))
1436 acltype_changed_cb(zfsvfs
, pval
);
1437 zfsvfs
->z_issnap
= B_TRUE
;
1438 zfsvfs
->z_os
->os_sync
= ZFS_SYNC_DISABLED
;
1439 zfsvfs
->z_snap_defer_time
= jiffies
;
1441 mutex_enter(&zfsvfs
->z_os
->os_user_ptr_lock
);
1442 dmu_objset_set_user(zfsvfs
->z_os
, zfsvfs
);
1443 mutex_exit(&zfsvfs
->z_os
->os_user_ptr_lock
);
1445 if ((error
= zfsvfs_setup(zfsvfs
, B_TRUE
)))
1449 /* Allocate a root inode for the filesystem. */
1450 error
= zfs_root(zfsvfs
, &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
);
1464 if (!zfsvfs
->z_issnap
)
1465 zfsctl_create(zfsvfs
);
1467 zfsvfs
->z_arc_prune
= arc_add_prune_callback(zpl_prune_sb
, sb
);
1470 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1471 zfsvfs_free(zfsvfs
);
1473 * make sure we don't have dangling sb->s_fs_info which
1474 * zfs_preumount will use.
1476 sb
->s_fs_info
= NULL
;
1483 * Called when an unmount is requested and certain sanity checks have
1484 * already passed. At this point no dentries or inodes have been reclaimed
1485 * from their respective caches. We drop the extra reference on the .zfs
1486 * control directory to allow everything to be reclaimed. All snapshots
1487 * must already have been unmounted to reach this point.
1490 zfs_preumount(struct super_block
*sb
)
1492 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1494 /* zfsvfs is NULL when zfs_domount fails during mount */
1496 zfsctl_destroy(sb
->s_fs_info
);
1498 * Wait for iput_async before entering evict_inodes in
1499 * generic_shutdown_super. The reason we must finish before
1500 * evict_inodes is when lazytime is on, or when zfs_purgedir
1501 * calls zfs_zget, iput would bump i_count from 0 to 1. This
1502 * would race with the i_count check in evict_inodes. This means
1503 * it could destroy the inode while we are still using it.
1505 * We wait for two passes. xattr directories in the first pass
1506 * may add xattr entries in zfs_purgedir, so in the second pass
1507 * we wait for them. We don't use taskq_wait here because it is
1508 * a pool wide taskq. Other mounted filesystems can constantly
1509 * do iput_async and there's no guarantee when taskq will be
1512 taskq_wait_outstanding(dsl_pool_iput_taskq(
1513 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1514 taskq_wait_outstanding(dsl_pool_iput_taskq(
1515 dmu_objset_pool(zfsvfs
->z_os
)), 0);
1520 * Called once all other unmount released tear down has occurred.
1521 * It is our responsibility to release any remaining infrastructure.
1525 zfs_umount(struct super_block
*sb
)
1527 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1530 arc_remove_prune_callback(zfsvfs
->z_arc_prune
);
1531 VERIFY(zfsvfs_teardown(zfsvfs
, B_TRUE
) == 0);
1533 bdi_destroy(sb
->s_bdi
);
1536 * z_os will be NULL if there was an error in
1537 * attempting to reopen zfsvfs.
1541 * Unset the objset user_ptr.
1543 mutex_enter(&os
->os_user_ptr_lock
);
1544 dmu_objset_set_user(os
, NULL
);
1545 mutex_exit(&os
->os_user_ptr_lock
);
1548 * Finally release the objset
1550 dmu_objset_disown(os
, zfsvfs
);
1553 zfsvfs_free(zfsvfs
);
1558 zfs_remount(struct super_block
*sb
, int *flags
, zfs_mntopts_t
*zmo
)
1560 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1563 zfs_unregister_callbacks(zfsvfs
);
1564 error
= zfs_register_callbacks(zfsvfs
);
1570 zfs_vget(struct super_block
*sb
, struct inode
**ipp
, fid_t
*fidp
)
1572 zfsvfs_t
*zfsvfs
= sb
->s_fs_info
;
1574 uint64_t object
= 0;
1575 uint64_t fid_gen
= 0;
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
);
1591 return (SET_ERROR(EINVAL
));
1594 /* LONG_FID_LEN means snapdirs */
1595 if (fidp
->fid_len
== LONG_FID_LEN
) {
1596 zfid_long_t
*zlfid
= (zfid_long_t
*)fidp
;
1597 uint64_t objsetid
= 0;
1598 uint64_t setgen
= 0;
1600 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
1601 objsetid
|= ((uint64_t)zlfid
->zf_setid
[i
]) << (8 * i
);
1603 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
1604 setgen
|= ((uint64_t)zlfid
->zf_setgen
[i
]) << (8 * i
);
1606 if (objsetid
!= ZFSCTL_INO_SNAPDIRS
- object
) {
1607 dprintf("snapdir fid: objsetid (%llu) != "
1608 "ZFSCTL_INO_SNAPDIRS (%llu) - object (%llu)\n",
1609 objsetid
, ZFSCTL_INO_SNAPDIRS
, object
);
1611 return (SET_ERROR(EINVAL
));
1614 if (fid_gen
> 1 || setgen
!= 0) {
1615 dprintf("snapdir fid: fid_gen (%llu) and setgen "
1616 "(%llu)\n", fid_gen
, setgen
);
1617 return (SET_ERROR(EINVAL
));
1620 return (zfsctl_snapdir_vget(sb
, objsetid
, fid_gen
, ipp
));
1624 /* A zero fid_gen means we are in the .zfs control directories */
1626 (object
== ZFSCTL_INO_ROOT
|| object
== ZFSCTL_INO_SNAPDIR
)) {
1627 *ipp
= zfsvfs
->z_ctldir
;
1628 ASSERT(*ipp
!= NULL
);
1629 if (object
== ZFSCTL_INO_SNAPDIR
) {
1630 VERIFY(zfsctl_root_lookup(*ipp
, "snapshot", ipp
,
1631 0, kcred
, NULL
, NULL
) == 0);
1639 gen_mask
= -1ULL >> (64 - 8 * i
);
1641 dprintf("getting %llu [%llu mask %llx]\n", object
, fid_gen
, gen_mask
);
1642 if ((err
= zfs_zget(zfsvfs
, object
, &zp
))) {
1647 /* Don't export xattr stuff */
1648 if (zp
->z_pflags
& ZFS_XATTR
) {
1651 return (SET_ERROR(ENOENT
));
1654 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
), &zp_gen
,
1656 zp_gen
= zp_gen
& gen_mask
;
1659 if ((fid_gen
== 0) && (zfsvfs
->z_root
== object
))
1661 if (zp
->z_unlinked
|| zp_gen
!= fid_gen
) {
1662 dprintf("znode gen (%llu) != fid gen (%llu)\n", zp_gen
,
1666 return (SET_ERROR(ENOENT
));
1671 zfs_inode_update(ITOZ(*ipp
));
1678 * Block out VFS ops and close zfsvfs_t
1680 * Note, if successful, then we return with the 'z_teardown_lock' and
1681 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
1682 * dataset and objset intact so that they can be atomically handed off during
1683 * a subsequent rollback or recv operation and the resume thereafter.
1686 zfs_suspend_fs(zfsvfs_t
*zfsvfs
)
1690 if ((error
= zfsvfs_teardown(zfsvfs
, B_FALSE
)) != 0)
1697 * Reopen zfsvfs_t and release VFS ops.
1700 zfs_resume_fs(zfsvfs_t
*zfsvfs
, dsl_dataset_t
*ds
)
1704 uint64_t sa_obj
= 0;
1706 ASSERT(RRM_WRITE_HELD(&zfsvfs
->z_teardown_lock
));
1707 ASSERT(RW_WRITE_HELD(&zfsvfs
->z_teardown_inactive_lock
));
1710 * We already own this, so just update the objset_t, as the one we
1711 * had before may have been evicted.
1713 VERIFY3P(ds
->ds_owner
, ==, zfsvfs
);
1714 VERIFY(dsl_dataset_long_held(ds
));
1715 VERIFY0(dmu_objset_from_ds(ds
, &zfsvfs
->z_os
));
1718 * Make sure version hasn't changed
1721 err
= zfs_get_zplprop(zfsvfs
->z_os
, ZFS_PROP_VERSION
,
1722 &zfsvfs
->z_version
);
1727 err
= zap_lookup(zfsvfs
->z_os
, MASTER_NODE_OBJ
,
1728 ZFS_SA_ATTRS
, 8, 1, &sa_obj
);
1730 if (err
&& zfsvfs
->z_version
>= ZPL_VERSION_SA
)
1733 if ((err
= sa_setup(zfsvfs
->z_os
, sa_obj
,
1734 zfs_attr_table
, ZPL_END
, &zfsvfs
->z_attr_table
)) != 0)
1737 if (zfsvfs
->z_version
>= ZPL_VERSION_SA
)
1738 sa_register_update_callback(zfsvfs
->z_os
,
1741 VERIFY(zfsvfs_setup(zfsvfs
, B_FALSE
) == 0);
1743 zfs_set_fuid_feature(zfsvfs
);
1744 zfsvfs
->z_rollback_time
= jiffies
;
1747 * Attempt to re-establish all the active inodes with their
1748 * dbufs. If a zfs_rezget() fails, then we unhash the inode
1749 * and mark it stale. This prevents a collision if a new
1750 * inode/object is created which must use the same inode
1751 * number. The stale inode will be be released when the
1752 * VFS prunes the dentry holding the remaining references
1753 * on the stale inode.
1755 mutex_enter(&zfsvfs
->z_znodes_lock
);
1756 for (zp
= list_head(&zfsvfs
->z_all_znodes
); zp
;
1757 zp
= list_next(&zfsvfs
->z_all_znodes
, zp
)) {
1758 err2
= zfs_rezget(zp
);
1760 remove_inode_hash(ZTOI(zp
));
1761 zp
->z_is_stale
= B_TRUE
;
1764 mutex_exit(&zfsvfs
->z_znodes_lock
);
1767 /* release the VFS ops */
1768 rw_exit(&zfsvfs
->z_teardown_inactive_lock
);
1769 rrm_exit(&zfsvfs
->z_teardown_lock
, FTAG
);
1773 * Since we couldn't setup the sa framework, try to force
1774 * unmount this file system.
1777 (void) zfs_umount(zfsvfs
->z_sb
);
1783 zfs_set_version(zfsvfs_t
*zfsvfs
, uint64_t newvers
)
1786 objset_t
*os
= zfsvfs
->z_os
;
1789 if (newvers
< ZPL_VERSION_INITIAL
|| newvers
> ZPL_VERSION
)
1790 return (SET_ERROR(EINVAL
));
1792 if (newvers
< zfsvfs
->z_version
)
1793 return (SET_ERROR(EINVAL
));
1795 if (zfs_spa_version_map(newvers
) >
1796 spa_version(dmu_objset_spa(zfsvfs
->z_os
)))
1797 return (SET_ERROR(ENOTSUP
));
1799 tx
= dmu_tx_create(os
);
1800 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_FALSE
, ZPL_VERSION_STR
);
1801 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
1802 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, B_TRUE
,
1804 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1806 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1812 error
= zap_update(os
, MASTER_NODE_OBJ
, ZPL_VERSION_STR
,
1813 8, 1, &newvers
, tx
);
1820 if (newvers
>= ZPL_VERSION_SA
&& !zfsvfs
->z_use_sa
) {
1823 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs
->z_os
)), >=,
1825 sa_obj
= zap_create(os
, DMU_OT_SA_MASTER_NODE
,
1826 DMU_OT_NONE
, 0, tx
);
1828 error
= zap_add(os
, MASTER_NODE_OBJ
,
1829 ZFS_SA_ATTRS
, 8, 1, &sa_obj
, tx
);
1832 VERIFY(0 == sa_set_sa_object(os
, sa_obj
));
1833 sa_register_update_callback(os
, zfs_sa_upgrade
);
1836 spa_history_log_internal_ds(dmu_objset_ds(os
), "upgrade", tx
,
1837 "from %llu to %llu", zfsvfs
->z_version
, newvers
);
1841 zfsvfs
->z_version
= newvers
;
1843 zfs_set_fuid_feature(zfsvfs
);
1849 * Read a property stored within the master node.
1852 zfs_get_zplprop(objset_t
*os
, zfs_prop_t prop
, uint64_t *value
)
1855 int error
= SET_ERROR(ENOENT
);
1858 * Look up the file system's value for the property. For the
1859 * version property, we look up a slightly different string.
1861 if (prop
== ZFS_PROP_VERSION
)
1862 pname
= ZPL_VERSION_STR
;
1864 pname
= zfs_prop_to_name(prop
);
1867 error
= zap_lookup(os
, MASTER_NODE_OBJ
, pname
, 8, 1, value
);
1869 if (error
== ENOENT
) {
1870 /* No value set, use the default value */
1872 case ZFS_PROP_VERSION
:
1873 *value
= ZPL_VERSION
;
1875 case ZFS_PROP_NORMALIZE
:
1876 case ZFS_PROP_UTF8ONLY
:
1880 *value
= ZFS_CASE_SENSITIVE
;
1882 case ZFS_PROP_ACLTYPE
:
1883 *value
= ZFS_ACLTYPE_OFF
;
1894 * Return true if the coresponding vfs's unmounted flag is set.
1895 * Otherwise return false.
1896 * If this function returns true we know VFS unmount has been initiated.
1899 zfs_get_vfs_flag_unmounted(objset_t
*os
)
1902 boolean_t unmounted
= B_FALSE
;
1904 ASSERT(dmu_objset_type(os
) == DMU_OST_ZFS
);
1906 mutex_enter(&os
->os_user_ptr_lock
);
1907 zfvp
= dmu_objset_get_user(os
);
1908 if (zfvp
!= NULL
&& zfvp
->z_unmounted
)
1910 mutex_exit(&os
->os_user_ptr_lock
);
1920 dmu_objset_register_type(DMU_OST_ZFS
, zfs_space_delta_cb
);
1921 register_filesystem(&zpl_fs_type
);
1928 * we don't use outstanding because zpl_posix_acl_free might add more.
1930 taskq_wait(system_delay_taskq
);
1931 taskq_wait(system_taskq
);
1932 unregister_filesystem(&zpl_fs_type
);
1937 #if defined(_KERNEL) && defined(HAVE_SPL)
1938 EXPORT_SYMBOL(zfs_suspend_fs
);
1939 EXPORT_SYMBOL(zfs_resume_fs
);
1940 EXPORT_SYMBOL(zfs_userspace_one
);
1941 EXPORT_SYMBOL(zfs_userspace_many
);
1942 EXPORT_SYMBOL(zfs_set_userquota
);
1943 EXPORT_SYMBOL(zfs_owner_overquota
);
1944 EXPORT_SYMBOL(zfs_fuid_overquota
);
1945 EXPORT_SYMBOL(zfs_fuid_overobjquota
);
1946 EXPORT_SYMBOL(zfs_set_version
);
1947 EXPORT_SYMBOL(zfsvfs_create
);
1948 EXPORT_SYMBOL(zfsvfs_free
);
1949 EXPORT_SYMBOL(zfs_is_readonly
);
1950 EXPORT_SYMBOL(zfs_domount
);
1951 EXPORT_SYMBOL(zfs_preumount
);
1952 EXPORT_SYMBOL(zfs_umount
);
1953 EXPORT_SYMBOL(zfs_remount
);
1954 EXPORT_SYMBOL(zfs_statvfs
);
1955 EXPORT_SYMBOL(zfs_vget
);
1956 EXPORT_SYMBOL(zfs_prune
);