4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/errno.h>
30 #include <sys/modctl.h>
35 #include <sys/cmn_err.h>
37 #include <sys/zfs_ioctl.h>
38 #include <sys/zfs_vfsops.h>
39 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
44 #include <sys/priv_impl.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_dir.h>
63 #include <sys/zfs_onexit.h>
65 #include <sys/dsl_scan.h>
66 #include <sharefs/share.h>
67 #include <sys/dmu_objset.h>
68 #include <sys/fm/util.h>
70 #include <linux/miscdevice.h>
72 #include "zfs_namecheck.h"
74 #include "zfs_deleg.h"
75 #include "zfs_comutil.h"
77 kmutex_t zfsdev_state_lock
;
78 list_t zfsdev_state_list
;
80 extern void zfs_init(void);
81 extern void zfs_fini(void);
83 typedef int zfs_ioc_func_t(zfs_cmd_t
*);
84 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, cred_t
*);
90 } zfs_ioc_namecheck_t
;
93 POOL_CHECK_NONE
= 1 << 0,
94 POOL_CHECK_SUSPENDED
= 1 << 1,
95 POOL_CHECK_READONLY
= 1 << 2
96 } zfs_ioc_poolcheck_t
;
98 typedef struct zfs_ioc_vec
{
99 zfs_ioc_func_t
*zvec_func
;
100 zfs_secpolicy_func_t
*zvec_secpolicy
;
101 zfs_ioc_namecheck_t zvec_namecheck
;
102 boolean_t zvec_his_log
;
103 zfs_ioc_poolcheck_t zvec_pool_check
;
106 /* This array is indexed by zfs_userquota_prop_t */
107 static const char *userquota_perms
[] = {
108 ZFS_DELEG_PERM_USERUSED
,
109 ZFS_DELEG_PERM_USERQUOTA
,
110 ZFS_DELEG_PERM_GROUPUSED
,
111 ZFS_DELEG_PERM_GROUPQUOTA
,
114 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
115 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
117 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
119 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
121 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
**);
124 history_str_free(char *buf
)
126 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
130 history_str_get(zfs_cmd_t
*zc
)
134 if (zc
->zc_history
== 0)
137 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
| KM_NODEBUG
);
138 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
139 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
140 history_str_free(buf
);
144 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
150 * Check to see if the named dataset is currently defined as bootable
153 zfs_is_bootfs(const char *name
)
157 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
159 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
160 dmu_objset_rele(os
, FTAG
);
167 * zfs_earlier_version
169 * Return non-zero if the spa version is less than requested version.
172 zfs_earlier_version(const char *name
, int version
)
176 if (spa_open(name
, &spa
, FTAG
) == 0) {
177 if (spa_version(spa
) < version
) {
178 spa_close(spa
, FTAG
);
181 spa_close(spa
, FTAG
);
187 * zpl_earlier_version
189 * Return TRUE if the ZPL version is less than requested version.
192 zpl_earlier_version(const char *name
, int version
)
195 boolean_t rc
= B_TRUE
;
197 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
200 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
201 dmu_objset_rele(os
, FTAG
);
204 /* XXX reading from non-owned objset */
205 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
206 rc
= zplversion
< version
;
207 dmu_objset_rele(os
, FTAG
);
213 zfs_log_history(zfs_cmd_t
*zc
)
218 if ((buf
= history_str_get(zc
)) == NULL
)
221 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
222 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
223 (void) spa_history_log(spa
, buf
, LOG_CMD_NORMAL
);
224 spa_close(spa
, FTAG
);
226 history_str_free(buf
);
230 * Policy for top-level read operations (list pools). Requires no privileges,
231 * and can be used in the local zone, as there is no associated dataset.
235 zfs_secpolicy_none(zfs_cmd_t
*zc
, cred_t
*cr
)
241 * Policy for dataset read operations (list children, get statistics). Requires
242 * no privileges, but must be visible in the local zone.
246 zfs_secpolicy_read(zfs_cmd_t
*zc
, cred_t
*cr
)
248 if (INGLOBALZONE(curproc
) ||
249 zone_dataset_visible(zc
->zc_name
, NULL
))
256 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
261 * The dataset must be visible by this zone -- check this first
262 * so they don't see EPERM on something they shouldn't know about.
264 if (!INGLOBALZONE(curproc
) &&
265 !zone_dataset_visible(dataset
, &writable
))
268 if (INGLOBALZONE(curproc
)) {
270 * If the fs is zoned, only root can access it from the
273 if (secpolicy_zfs(cr
) && zoned
)
277 * If we are in a local zone, the 'zoned' property must be set.
282 /* must be writable by this zone */
290 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
294 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
297 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
301 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
305 rw_enter(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
, RW_READER
);
306 if (dsl_prop_get_ds(ds
, "zoned", 8, 1, &zoned
, NULL
)) {
307 rw_exit(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
);
310 rw_exit(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
);
312 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
316 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
320 error
= zfs_dozonecheck(name
, cr
);
322 error
= secpolicy_zfs(cr
);
324 error
= dsl_deleg_access(name
, perm
, cr
);
330 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
331 const char *perm
, cred_t
*cr
)
335 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
337 error
= secpolicy_zfs(cr
);
339 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
345 * Policy for setting the security label property.
347 * Returns 0 for success, non-zero for access and other errors.
350 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
353 char ds_hexsl
[MAXNAMELEN
];
354 bslabel_t ds_sl
, new_sl
;
355 boolean_t new_default
= FALSE
;
357 int needed_priv
= -1;
360 /* First get the existing dataset label. */
361 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
362 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
366 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
369 /* The label must be translatable */
370 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
374 * In a non-global zone, disallow attempts to set a label that
375 * doesn't match that of the zone; otherwise no other checks
378 if (!INGLOBALZONE(curproc
)) {
379 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
385 * For global-zone datasets (i.e., those whose zoned property is
386 * "off", verify that the specified new label is valid for the
389 if (dsl_prop_get_integer(name
,
390 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
393 if (zfs_check_global_label(name
, strval
) != 0)
398 * If the existing dataset label is nondefault, check if the
399 * dataset is mounted (label cannot be changed while mounted).
400 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
401 * mounted (or isn't a dataset, doesn't exist, ...).
403 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
405 static char *setsl_tag
= "setsl_tag";
408 * Try to own the dataset; abort if there is any error,
409 * (e.g., already mounted, in use, or other error).
411 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
416 dmu_objset_disown(os
, setsl_tag
);
419 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
423 if (hexstr_to_label(strval
, &new_sl
) != 0)
426 if (blstrictdom(&ds_sl
, &new_sl
))
427 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
428 else if (blstrictdom(&new_sl
, &ds_sl
))
429 needed_priv
= PRIV_FILE_UPGRADE_SL
;
431 /* dataset currently has a default label */
433 needed_priv
= PRIV_FILE_UPGRADE_SL
;
437 if (needed_priv
!= -1)
438 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
442 #endif /* HAVE_MLSLABEL */
446 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
452 * Check permissions for special properties.
459 * Disallow setting of 'zoned' from within a local zone.
461 if (!INGLOBALZONE(curproc
))
466 if (!INGLOBALZONE(curproc
)) {
468 char setpoint
[MAXNAMELEN
];
470 * Unprivileged users are allowed to modify the
471 * quota on things *under* (ie. contained by)
472 * the thing they own.
474 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
477 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
482 case ZFS_PROP_MLSLABEL
:
483 if (!is_system_labeled())
486 if (nvpair_value_string(propval
, &strval
) == 0) {
489 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
496 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
500 zfs_secpolicy_fsacl(zfs_cmd_t
*zc
, cred_t
*cr
)
504 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
509 * permission to set permissions will be evaluated later in
510 * dsl_deleg_can_allow()
516 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, cred_t
*cr
)
518 return (zfs_secpolicy_write_perms(zc
->zc_name
,
519 ZFS_DELEG_PERM_ROLLBACK
, cr
));
523 zfs_secpolicy_send(zfs_cmd_t
*zc
, cred_t
*cr
)
532 * Generate the current snapshot name from the given objsetid, then
533 * use that name for the secpolicy/zone checks.
535 cp
= strchr(zc
->zc_name
, '@');
538 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
542 dp
= spa_get_dsl(spa
);
543 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
544 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
545 rw_exit(&dp
->dp_config_rwlock
);
546 spa_close(spa
, FTAG
);
550 dsl_dataset_name(ds
, zc
->zc_name
);
552 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
553 ZFS_DELEG_PERM_SEND
, cr
);
554 dsl_dataset_rele(ds
, FTAG
);
559 #ifdef HAVE_SMB_SHARE
561 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, cred_t
*cr
)
566 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
567 NO_FOLLOW
, NULL
, &vp
)) != 0)
570 /* Now make sure mntpnt and dataset are ZFS */
572 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
573 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
574 zc
->zc_name
) != 0)) {
580 return (dsl_deleg_access(zc
->zc_name
,
581 ZFS_DELEG_PERM_SHARE
, cr
));
583 #endif /* HAVE_SMB_SHARE */
586 zfs_secpolicy_share(zfs_cmd_t
*zc
, cred_t
*cr
)
588 #ifdef HAVE_SMB_SHARE
589 if (!INGLOBALZONE(curproc
))
592 if (secpolicy_nfs(cr
) == 0) {
595 return (zfs_secpolicy_deleg_share(zc
, cr
));
599 #endif /* HAVE_SMB_SHARE */
603 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, cred_t
*cr
)
605 #ifdef HAVE_SMB_SHARE
606 if (!INGLOBALZONE(curproc
))
609 if (secpolicy_smb(cr
) == 0) {
612 return (zfs_secpolicy_deleg_share(zc
, cr
));
616 #endif /* HAVE_SMB_SHARE */
620 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
625 * Remove the @bla or /bla from the end of the name to get the parent.
627 (void) strncpy(parent
, datasetname
, parentsize
);
628 cp
= strrchr(parent
, '@');
632 cp
= strrchr(parent
, '/');
642 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
646 if ((error
= zfs_secpolicy_write_perms(name
,
647 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
650 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
654 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, cred_t
*cr
)
656 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
660 * Destroying snapshots with delegated permissions requires
661 * descendent mount and destroy permissions.
662 * Reassemble the full filesystem@snap name so dsl_deleg_access()
663 * can do the correct permission check.
665 * Since this routine is used when doing a recursive destroy of snapshots
666 * and destroying snapshots requires descendent permissions, a successfull
667 * check of the top level snapshot applies to snapshots of all descendent
670 * The target snapshot may not exist when doing a recursive destroy.
671 * In this case fallback to permissions of the parent dataset.
674 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, cred_t
*cr
)
679 dsname
= kmem_asprintf("%s@%s", zc
->zc_name
, zc
->zc_value
);
681 error
= zfs_secpolicy_destroy_perms(dsname
, cr
);
683 error
= zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
);
690 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
692 char parentname
[MAXNAMELEN
];
695 if ((error
= zfs_secpolicy_write_perms(from
,
696 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
699 if ((error
= zfs_secpolicy_write_perms(from
,
700 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
703 if ((error
= zfs_get_parent(to
, parentname
,
704 sizeof (parentname
))) != 0)
707 if ((error
= zfs_secpolicy_write_perms(parentname
,
708 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
711 if ((error
= zfs_secpolicy_write_perms(parentname
,
712 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
719 zfs_secpolicy_rename(zfs_cmd_t
*zc
, cred_t
*cr
)
721 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
725 zfs_secpolicy_promote(zfs_cmd_t
*zc
, cred_t
*cr
)
727 char parentname
[MAXNAMELEN
];
731 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
732 ZFS_DELEG_PERM_PROMOTE
, cr
);
736 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &clone
);
739 dsl_dataset_t
*pclone
= NULL
;
741 dd
= clone
->os_dsl_dataset
->ds_dir
;
743 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
744 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
745 dd
->dd_phys
->dd_origin_obj
, FTAG
, &pclone
);
746 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
748 dmu_objset_rele(clone
, FTAG
);
752 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
753 ZFS_DELEG_PERM_MOUNT
, cr
);
755 dsl_dataset_name(pclone
, parentname
);
756 dmu_objset_rele(clone
, FTAG
);
757 dsl_dataset_rele(pclone
, FTAG
);
759 error
= zfs_secpolicy_write_perms(parentname
,
760 ZFS_DELEG_PERM_PROMOTE
, cr
);
766 zfs_secpolicy_receive(zfs_cmd_t
*zc
, cred_t
*cr
)
770 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
771 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
774 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
775 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
778 return (zfs_secpolicy_write_perms(zc
->zc_name
,
779 ZFS_DELEG_PERM_CREATE
, cr
));
783 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
785 return (zfs_secpolicy_write_perms(name
,
786 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
790 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
793 return (zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
));
797 zfs_secpolicy_create(zfs_cmd_t
*zc
, cred_t
*cr
)
799 char parentname
[MAXNAMELEN
];
802 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
803 sizeof (parentname
))) != 0)
806 if (zc
->zc_value
[0] != '\0') {
807 if ((error
= zfs_secpolicy_write_perms(zc
->zc_value
,
808 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
812 if ((error
= zfs_secpolicy_write_perms(parentname
,
813 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
816 error
= zfs_secpolicy_write_perms(parentname
,
817 ZFS_DELEG_PERM_MOUNT
, cr
);
823 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
824 * SYS_CONFIG privilege, which is not available in a local zone.
828 zfs_secpolicy_config(zfs_cmd_t
*zc
, cred_t
*cr
)
830 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
837 * Policy for object to name lookups.
841 zfs_secpolicy_diff(zfs_cmd_t
*zc
, cred_t
*cr
)
845 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
848 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
853 * Policy for fault injection. Requires all privileges.
857 zfs_secpolicy_inject(zfs_cmd_t
*zc
, cred_t
*cr
)
859 return (secpolicy_zinject(cr
));
863 zfs_secpolicy_inherit(zfs_cmd_t
*zc
, cred_t
*cr
)
865 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
867 if (prop
== ZPROP_INVAL
) {
868 if (!zfs_prop_user(zc
->zc_value
))
870 return (zfs_secpolicy_write_perms(zc
->zc_name
,
871 ZFS_DELEG_PERM_USERPROP
, cr
));
873 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
879 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, cred_t
*cr
)
881 int err
= zfs_secpolicy_read(zc
, cr
);
885 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
888 if (zc
->zc_value
[0] == 0) {
890 * They are asking about a posix uid/gid. If it's
891 * themself, allow it.
893 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
894 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
895 if (zc
->zc_guid
== crgetuid(cr
))
898 if (groupmember(zc
->zc_guid
, cr
))
903 return (zfs_secpolicy_write_perms(zc
->zc_name
,
904 userquota_perms
[zc
->zc_objset_type
], cr
));
908 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, cred_t
*cr
)
910 int err
= zfs_secpolicy_read(zc
, cr
);
914 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
917 return (zfs_secpolicy_write_perms(zc
->zc_name
,
918 userquota_perms
[zc
->zc_objset_type
], cr
));
922 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, cred_t
*cr
)
924 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
929 zfs_secpolicy_hold(zfs_cmd_t
*zc
, cred_t
*cr
)
931 return (zfs_secpolicy_write_perms(zc
->zc_name
,
932 ZFS_DELEG_PERM_HOLD
, cr
));
936 zfs_secpolicy_release(zfs_cmd_t
*zc
, cred_t
*cr
)
938 return (zfs_secpolicy_write_perms(zc
->zc_name
,
939 ZFS_DELEG_PERM_RELEASE
, cr
));
943 * Policy for allowing temporary snapshots to be taken or released
946 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
949 * A temporary snapshot is the same as a snapshot,
950 * hold, destroy and release all rolled into one.
951 * Delegated diff alone is sufficient that we allow this.
955 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
956 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
959 error
= zfs_secpolicy_snapshot(zc
, cr
);
961 error
= zfs_secpolicy_hold(zc
, cr
);
963 error
= zfs_secpolicy_release(zc
, cr
);
965 error
= zfs_secpolicy_destroy(zc
, cr
);
970 * Returns the nvlist as specified by the user in the zfs_cmd_t.
973 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
977 nvlist_t
*list
= NULL
;
980 * Read in and unpack the user-supplied nvlist.
985 packed
= kmem_alloc(size
, KM_SLEEP
| KM_NODEBUG
);
987 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
989 kmem_free(packed
, size
);
993 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
994 kmem_free(packed
, size
);
998 kmem_free(packed
, size
);
1005 fit_error_list(zfs_cmd_t
*zc
, nvlist_t
**errors
)
1009 VERIFY(nvlist_size(*errors
, &size
, NV_ENCODE_NATIVE
) == 0);
1011 if (size
> zc
->zc_nvlist_dst_size
) {
1012 nvpair_t
*more_errors
;
1015 if (zc
->zc_nvlist_dst_size
< 1024)
1018 VERIFY(nvlist_add_int32(*errors
, ZPROP_N_MORE_ERRORS
, 0) == 0);
1019 more_errors
= nvlist_prev_nvpair(*errors
, NULL
);
1022 nvpair_t
*pair
= nvlist_prev_nvpair(*errors
,
1024 VERIFY(nvlist_remove_nvpair(*errors
, pair
) == 0);
1026 VERIFY(nvlist_size(*errors
, &size
,
1027 NV_ENCODE_NATIVE
) == 0);
1028 } while (size
> zc
->zc_nvlist_dst_size
);
1030 VERIFY(nvlist_remove_nvpair(*errors
, more_errors
) == 0);
1031 VERIFY(nvlist_add_int32(*errors
, ZPROP_N_MORE_ERRORS
, n
) == 0);
1032 ASSERT(nvlist_size(*errors
, &size
, NV_ENCODE_NATIVE
) == 0);
1033 ASSERT(size
<= zc
->zc_nvlist_dst_size
);
1040 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1042 char *packed
= NULL
;
1046 VERIFY(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
1048 if (size
> zc
->zc_nvlist_dst_size
) {
1051 packed
= kmem_alloc(size
, KM_SLEEP
| KM_NODEBUG
);
1052 VERIFY(nvlist_pack(nvl
, &packed
, &size
, NV_ENCODE_NATIVE
,
1054 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1055 size
, zc
->zc_iflags
) != 0)
1057 kmem_free(packed
, size
);
1060 zc
->zc_nvlist_dst_size
= size
;
1065 get_zfs_sb(const char *dsname
, zfs_sb_t
**zsbp
)
1070 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1073 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1074 dmu_objset_rele(os
, FTAG
);
1078 mutex_enter(&os
->os_user_ptr_lock
);
1079 *zsbp
= dmu_objset_get_user(os
);
1080 if (*zsbp
&& (*zsbp
)->z_sb
) {
1081 atomic_inc(&((*zsbp
)->z_sb
->s_active
));
1085 mutex_exit(&os
->os_user_ptr_lock
);
1086 dmu_objset_rele(os
, FTAG
);
1091 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1092 * case its z_sb will be NULL, and it will be opened as the owner.
1095 zfs_sb_hold(const char *name
, void *tag
, zfs_sb_t
**zsbp
, boolean_t writer
)
1099 if (get_zfs_sb(name
, zsbp
) != 0)
1100 error
= zfs_sb_create(name
, zsbp
);
1102 rrw_enter(&(*zsbp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1104 if ((*zsbp
)->z_unmounted
) {
1106 * XXX we could probably try again, since the unmounting
1107 * thread should be just about to disassociate the
1108 * objset from the zfsvfs.
1110 rrw_exit(&(*zsbp
)->z_teardown_lock
, tag
);
1118 zfs_sb_rele(zfs_sb_t
*zsb
, void *tag
)
1120 rrw_exit(&zsb
->z_teardown_lock
, tag
);
1123 deactivate_super(zsb
->z_sb
);
1125 dmu_objset_disown(zsb
->z_os
, zsb
);
1131 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1134 nvlist_t
*config
, *props
= NULL
;
1135 nvlist_t
*rootprops
= NULL
;
1136 nvlist_t
*zplprops
= NULL
;
1139 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1140 zc
->zc_iflags
, &config
)))
1143 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1144 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1145 zc
->zc_iflags
, &props
))) {
1146 nvlist_free(config
);
1151 nvlist_t
*nvl
= NULL
;
1152 uint64_t version
= SPA_VERSION
;
1154 (void) nvlist_lookup_uint64(props
,
1155 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1156 if (version
< SPA_VERSION_INITIAL
|| version
> SPA_VERSION
) {
1158 goto pool_props_bad
;
1160 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1162 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1164 nvlist_free(config
);
1168 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1170 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1171 error
= zfs_fill_zplprops_root(version
, rootprops
,
1174 goto pool_props_bad
;
1177 buf
= history_str_get(zc
);
1179 error
= spa_create(zc
->zc_name
, config
, props
, buf
, zplprops
);
1182 * Set the remaining root properties
1184 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1185 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1186 (void) spa_destroy(zc
->zc_name
);
1189 history_str_free(buf
);
1192 nvlist_free(rootprops
);
1193 nvlist_free(zplprops
);
1194 nvlist_free(config
);
1201 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1204 zfs_log_history(zc
);
1205 error
= spa_destroy(zc
->zc_name
);
1207 zvol_remove_minors(zc
->zc_name
);
1212 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1214 nvlist_t
*config
, *props
= NULL
;
1218 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1219 zc
->zc_iflags
, &config
)) != 0)
1222 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1223 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1224 zc
->zc_iflags
, &props
))) {
1225 nvlist_free(config
);
1229 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1230 guid
!= zc
->zc_guid
)
1233 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1235 if (zc
->zc_nvlist_dst
!= 0) {
1238 if ((err
= put_nvlist(zc
, config
)) != 0)
1243 zvol_create_minors(zc
->zc_name
);
1245 nvlist_free(config
);
1254 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1257 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1258 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1260 zfs_log_history(zc
);
1261 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1263 zvol_remove_minors(zc
->zc_name
);
1268 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1273 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1276 error
= put_nvlist(zc
, configs
);
1278 nvlist_free(configs
);
1284 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1290 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1291 sizeof (zc
->zc_value
));
1293 if (config
!= NULL
) {
1294 ret
= put_nvlist(zc
, config
);
1295 nvlist_free(config
);
1298 * The config may be present even if 'error' is non-zero.
1299 * In this case we return success, and preserve the real errno
1302 zc
->zc_cookie
= error
;
1311 * Try to import the given pool, returning pool stats as appropriate so that
1312 * user land knows which devices are available and overall pool health.
1315 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1317 nvlist_t
*tryconfig
, *config
;
1320 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1321 zc
->zc_iflags
, &tryconfig
)) != 0)
1324 config
= spa_tryimport(tryconfig
);
1326 nvlist_free(tryconfig
);
1331 error
= put_nvlist(zc
, config
);
1332 nvlist_free(config
);
1339 * zc_name name of the pool
1340 * zc_cookie scan func (pool_scan_func_t)
1343 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1348 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1351 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1352 error
= spa_scan_stop(spa
);
1354 error
= spa_scan(spa
, zc
->zc_cookie
);
1356 spa_close(spa
, FTAG
);
1362 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1367 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1370 spa_close(spa
, FTAG
);
1376 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1381 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1384 if (zc
->zc_cookie
< spa_version(spa
) || zc
->zc_cookie
> SPA_VERSION
) {
1385 spa_close(spa
, FTAG
);
1389 spa_upgrade(spa
, zc
->zc_cookie
);
1390 spa_close(spa
, FTAG
);
1396 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1403 if ((size
= zc
->zc_history_len
) == 0)
1406 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1409 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1410 spa_close(spa
, FTAG
);
1414 hist_buf
= vmem_alloc(size
, KM_SLEEP
);
1415 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1416 &zc
->zc_history_len
, hist_buf
)) == 0) {
1417 error
= ddi_copyout(hist_buf
,
1418 (void *)(uintptr_t)zc
->zc_history
,
1419 zc
->zc_history_len
, zc
->zc_iflags
);
1422 spa_close(spa
, FTAG
);
1423 vmem_free(hist_buf
, size
);
1428 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1432 if ((error
= dsl_dsobj_to_dsname(zc
->zc_name
,zc
->zc_obj
,zc
->zc_value
)))
1440 * zc_name name of filesystem
1441 * zc_obj object to find
1444 * zc_value name of object
1447 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1452 /* XXX reading from objset not owned */
1453 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1455 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1456 dmu_objset_rele(os
, FTAG
);
1459 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1460 sizeof (zc
->zc_value
));
1461 dmu_objset_rele(os
, FTAG
);
1468 * zc_name name of filesystem
1469 * zc_obj object to find
1472 * zc_stat stats on object
1473 * zc_value path to object
1476 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1481 /* XXX reading from objset not owned */
1482 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1484 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1485 dmu_objset_rele(os
, FTAG
);
1488 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1489 sizeof (zc
->zc_value
));
1490 dmu_objset_rele(os
, FTAG
);
1496 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1500 nvlist_t
*config
, **l2cache
, **spares
;
1501 uint_t nl2cache
= 0, nspares
= 0;
1503 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1507 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1508 zc
->zc_iflags
, &config
);
1509 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1510 &l2cache
, &nl2cache
);
1512 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1516 * A root pool with concatenated devices is not supported.
1517 * Thus, can not add a device to a root pool.
1519 * Intent log device can not be added to a rootpool because
1520 * during mountroot, zil is replayed, a seperated log device
1521 * can not be accessed during the mountroot time.
1523 * l2cache and spare devices are ok to be added to a rootpool.
1525 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1526 nvlist_free(config
);
1527 spa_close(spa
, FTAG
);
1532 error
= spa_vdev_add(spa
, config
);
1533 nvlist_free(config
);
1535 spa_close(spa
, FTAG
);
1541 * zc_name name of the pool
1542 * zc_nvlist_conf nvlist of devices to remove
1543 * zc_cookie to stop the remove?
1546 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1551 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1554 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1555 spa_close(spa
, FTAG
);
1560 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1564 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1566 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1568 switch (zc
->zc_cookie
) {
1569 case VDEV_STATE_ONLINE
:
1570 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1573 case VDEV_STATE_OFFLINE
:
1574 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1577 case VDEV_STATE_FAULTED
:
1578 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1579 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1580 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1582 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1585 case VDEV_STATE_DEGRADED
:
1586 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1587 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1588 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1590 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1596 zc
->zc_cookie
= newstate
;
1597 spa_close(spa
, FTAG
);
1602 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1605 int replacing
= zc
->zc_cookie
;
1609 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1612 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1613 zc
->zc_iflags
, &config
)) == 0) {
1614 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1615 nvlist_free(config
);
1618 spa_close(spa
, FTAG
);
1623 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1628 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1631 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1633 spa_close(spa
, FTAG
);
1638 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1641 nvlist_t
*config
, *props
= NULL
;
1643 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1645 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1648 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1649 zc
->zc_iflags
, &config
))) {
1650 spa_close(spa
, FTAG
);
1654 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1655 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1656 zc
->zc_iflags
, &props
))) {
1657 spa_close(spa
, FTAG
);
1658 nvlist_free(config
);
1662 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1664 spa_close(spa
, FTAG
);
1666 nvlist_free(config
);
1673 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1676 char *path
= zc
->zc_value
;
1677 uint64_t guid
= zc
->zc_guid
;
1680 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1684 error
= spa_vdev_setpath(spa
, guid
, path
);
1685 spa_close(spa
, FTAG
);
1690 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
1693 char *fru
= zc
->zc_value
;
1694 uint64_t guid
= zc
->zc_guid
;
1697 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1701 error
= spa_vdev_setfru(spa
, guid
, fru
);
1702 spa_close(spa
, FTAG
);
1707 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
1712 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1714 if (zc
->zc_nvlist_dst
!= 0 &&
1715 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
1716 dmu_objset_stats(os
, nv
);
1718 * NB: zvol_get_stats() will read the objset contents,
1719 * which we aren't supposed to do with a
1720 * DS_MODE_USER hold, because it could be
1721 * inconsistent. So this is a bit of a workaround...
1722 * XXX reading with out owning
1724 if (!zc
->zc_objset_stats
.dds_inconsistent
) {
1725 if (dmu_objset_type(os
) == DMU_OST_ZVOL
)
1726 error
= zvol_get_stats(os
, nv
);
1729 error
= put_nvlist(zc
, nv
);
1738 * zc_name name of filesystem
1739 * zc_nvlist_dst_size size of buffer for property nvlist
1742 * zc_objset_stats stats
1743 * zc_nvlist_dst property nvlist
1744 * zc_nvlist_dst_size size of property nvlist
1747 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
1749 objset_t
*os
= NULL
;
1752 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
1755 error
= zfs_ioc_objset_stats_impl(zc
, os
);
1757 dmu_objset_rele(os
, FTAG
);
1764 * zc_name name of filesystem
1765 * zc_nvlist_dst_size size of buffer for property nvlist
1768 * zc_nvlist_dst received property nvlist
1769 * zc_nvlist_dst_size size of received property nvlist
1771 * Gets received properties (distinct from local properties on or after
1772 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1773 * local property values.
1776 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
1778 objset_t
*os
= NULL
;
1782 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
1786 * Without this check, we would return local property values if the
1787 * caller has not already received properties on or after
1788 * SPA_VERSION_RECVD_PROPS.
1790 if (!dsl_prop_get_hasrecvd(os
)) {
1791 dmu_objset_rele(os
, FTAG
);
1795 if (zc
->zc_nvlist_dst
!= 0 &&
1796 (error
= dsl_prop_get_received(os
, &nv
)) == 0) {
1797 error
= put_nvlist(zc
, nv
);
1801 dmu_objset_rele(os
, FTAG
);
1806 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
1812 * zfs_get_zplprop() will either find a value or give us
1813 * the default value (if there is one).
1815 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
1817 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
1823 * zc_name name of filesystem
1824 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1827 * zc_nvlist_dst zpl property nvlist
1828 * zc_nvlist_dst_size size of zpl property nvlist
1831 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
1836 /* XXX reading without owning */
1837 if ((err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)))
1840 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1843 * NB: nvl_add_zplprop() will read the objset contents,
1844 * which we aren't supposed to do with a DS_MODE_USER
1845 * hold, because it could be inconsistent.
1847 if (zc
->zc_nvlist_dst
!= 0 &&
1848 !zc
->zc_objset_stats
.dds_inconsistent
&&
1849 dmu_objset_type(os
) == DMU_OST_ZFS
) {
1852 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1853 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
1854 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
1855 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
1856 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
1857 err
= put_nvlist(zc
, nv
);
1862 dmu_objset_rele(os
, FTAG
);
1867 dataset_name_hidden(const char *name
)
1870 * Skip over datasets that are not visible in this zone,
1871 * internal datasets (which have a $ in their name), and
1872 * temporary datasets (which have a % in their name).
1874 if (strchr(name
, '$') != NULL
)
1876 if (strchr(name
, '%') != NULL
)
1878 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
1885 * zc_name name of filesystem
1886 * zc_cookie zap cursor
1887 * zc_nvlist_dst_size size of buffer for property nvlist
1890 * zc_name name of next filesystem
1891 * zc_cookie zap cursor
1892 * zc_objset_stats stats
1893 * zc_nvlist_dst property nvlist
1894 * zc_nvlist_dst_size size of property nvlist
1897 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
1902 size_t orig_len
= strlen(zc
->zc_name
);
1905 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))) {
1906 if (error
== ENOENT
)
1911 p
= strrchr(zc
->zc_name
, '/');
1912 if (p
== NULL
|| p
[1] != '\0')
1913 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
1914 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
1917 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1918 * but is not declared void because its called by dmu_objset_find().
1920 if (zc
->zc_cookie
== 0) {
1921 uint64_t cookie
= 0;
1922 int len
= sizeof (zc
->zc_name
) - (p
- zc
->zc_name
);
1924 while (dmu_dir_list_next(os
, len
, p
, NULL
, &cookie
) == 0)
1925 (void) dmu_objset_prefetch(p
, NULL
);
1929 error
= dmu_dir_list_next(os
,
1930 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1931 NULL
, &zc
->zc_cookie
);
1932 if (error
== ENOENT
)
1934 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
) &&
1935 !(zc
->zc_iflags
& FKIOCTL
));
1936 dmu_objset_rele(os
, FTAG
);
1939 * If it's an internal dataset (ie. with a '$' in its name),
1940 * don't try to get stats for it, otherwise we'll return ENOENT.
1942 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
1943 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
1944 if (error
== ENOENT
) {
1945 /* We lost a race with destroy, get the next one. */
1946 zc
->zc_name
[orig_len
] = '\0';
1955 * zc_name name of filesystem
1956 * zc_cookie zap cursor
1957 * zc_nvlist_dst_size size of buffer for property nvlist
1960 * zc_name name of next snapshot
1961 * zc_objset_stats stats
1962 * zc_nvlist_dst property nvlist
1963 * zc_nvlist_dst_size size of property nvlist
1966 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
1972 if (zc
->zc_cookie
== 0)
1973 (void) dmu_objset_find(zc
->zc_name
, dmu_objset_prefetch
,
1974 NULL
, DS_FIND_SNAPSHOTS
);
1976 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
1978 return (error
== ENOENT
? ESRCH
: error
);
1981 * A dataset name of maximum length cannot have any snapshots,
1982 * so exit immediately.
1984 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
1985 dmu_objset_rele(os
, FTAG
);
1989 error
= dmu_snapshot_list_next(os
,
1990 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
1991 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
1996 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
1999 * Since we probably don't have a hold on this snapshot,
2000 * it's possible that the objsetid could have been destroyed
2001 * and reused for a new objset. It's OK if this happens during
2002 * a zfs send operation, since the new createtxg will be
2003 * beyond the range we're interested in.
2005 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
2006 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2007 rw_exit(&dp
->dp_config_rwlock
);
2009 if (error
== ENOENT
) {
2010 /* Racing with destroy, get the next one. */
2011 *strchr(zc
->zc_name
, '@') = '\0';
2012 dmu_objset_rele(os
, FTAG
);
2018 error
= dmu_objset_from_ds(ds
, &ossnap
);
2020 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2021 dsl_dataset_rele(ds
, FTAG
);
2023 } else if (error
== ENOENT
) {
2027 dmu_objset_rele(os
, FTAG
);
2028 /* if we failed, undo the @ that we tacked on to zc_name */
2030 *strchr(zc
->zc_name
, '@') = '\0';
2035 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2037 const char *propname
= nvpair_name(pair
);
2039 unsigned int vallen
;
2042 zfs_userquota_prop_t type
;
2048 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2050 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2051 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2057 * A correctly constructed propname is encoded as
2058 * userquota@<rid>-<domain>.
2060 if ((dash
= strchr(propname
, '-')) == NULL
||
2061 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2070 err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
2072 err
= zfs_set_userquota(zsb
, type
, domain
, rid
, quota
);
2073 zfs_sb_rele(zsb
, FTAG
);
2080 * If the named property is one that has a special function to set its value,
2081 * return 0 on success and a positive error code on failure; otherwise if it is
2082 * not one of the special properties handled by this function, return -1.
2084 * XXX: It would be better for callers of the property interface if we handled
2085 * these special cases in dsl_prop.c (in the dsl layer).
2088 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2091 const char *propname
= nvpair_name(pair
);
2092 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2096 if (prop
== ZPROP_INVAL
) {
2097 if (zfs_prop_userquota(propname
))
2098 return (zfs_prop_set_userquota(dsname
, pair
));
2102 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2104 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2105 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2109 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2112 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2115 case ZFS_PROP_QUOTA
:
2116 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2118 case ZFS_PROP_REFQUOTA
:
2119 err
= dsl_dataset_set_quota(dsname
, source
, intval
);
2121 case ZFS_PROP_RESERVATION
:
2122 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2124 case ZFS_PROP_REFRESERVATION
:
2125 err
= dsl_dataset_set_reservation(dsname
, source
, intval
);
2127 case ZFS_PROP_VOLSIZE
:
2128 err
= zvol_set_volsize(dsname
, intval
);
2130 case ZFS_PROP_VERSION
:
2134 if ((err
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_TRUE
)) != 0)
2137 err
= zfs_set_version(zsb
, intval
);
2138 zfs_sb_rele(zsb
, FTAG
);
2140 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2143 zc
= kmem_zalloc(sizeof (zfs_cmd_t
),
2144 KM_SLEEP
| KM_NODEBUG
);
2145 (void) strcpy(zc
->zc_name
, dsname
);
2146 (void) zfs_ioc_userspace_upgrade(zc
);
2147 kmem_free(zc
, sizeof (zfs_cmd_t
));
2160 * This function is best effort. If it fails to set any of the given properties,
2161 * it continues to set as many as it can and returns the first error
2162 * encountered. If the caller provides a non-NULL errlist, it also gives the
2163 * complete list of names of all the properties it failed to set along with the
2164 * corresponding error numbers. The caller is responsible for freeing the
2167 * If every property is set successfully, zero is returned and the list pointed
2168 * at by errlist is NULL.
2171 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2179 nvlist_t
*genericnvl
;
2183 VERIFY(nvlist_alloc(&genericnvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2184 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2185 VERIFY(nvlist_alloc(&retrynvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2189 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2190 const char *propname
= nvpair_name(pair
);
2191 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2194 /* decode the property value */
2196 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2198 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2199 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2204 /* Validate value type */
2205 if (err
== 0 && prop
== ZPROP_INVAL
) {
2206 if (zfs_prop_user(propname
)) {
2207 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2209 } else if (zfs_prop_userquota(propname
)) {
2210 if (nvpair_type(propval
) !=
2211 DATA_TYPE_UINT64_ARRAY
)
2214 } else if (err
== 0) {
2215 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2216 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2218 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2221 VERIFY(nvpair_value_uint64(propval
,
2224 switch (zfs_prop_get_type(prop
)) {
2225 case PROP_TYPE_NUMBER
:
2227 case PROP_TYPE_STRING
:
2230 case PROP_TYPE_INDEX
:
2231 if (zfs_prop_index_to_string(prop
,
2232 intval
, &unused
) != 0)
2237 "unknown property type");
2244 /* Validate permissions */
2246 err
= zfs_check_settable(dsname
, pair
, CRED());
2249 err
= zfs_prop_set_special(dsname
, source
, pair
);
2252 * For better performance we build up a list of
2253 * properties to set in a single transaction.
2255 err
= nvlist_add_nvpair(genericnvl
, pair
);
2256 } else if (err
!= 0 && nvl
!= retrynvl
) {
2258 * This may be a spurious error caused by
2259 * receiving quota and reservation out of order.
2260 * Try again in a second pass.
2262 err
= nvlist_add_nvpair(retrynvl
, pair
);
2267 VERIFY(nvlist_add_int32(errors
, propname
, err
) == 0);
2270 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2275 if (!nvlist_empty(genericnvl
) &&
2276 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2278 * If this fails, we still want to set as many properties as we
2279 * can, so try setting them individually.
2282 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2283 const char *propname
= nvpair_name(pair
);
2287 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2289 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2290 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2294 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2295 VERIFY(nvpair_value_string(propval
,
2297 err
= dsl_prop_set(dsname
, propname
, source
, 1,
2298 strlen(strval
) + 1, strval
);
2300 VERIFY(nvpair_value_uint64(propval
,
2302 err
= dsl_prop_set(dsname
, propname
, source
, 8,
2307 VERIFY(nvlist_add_int32(errors
, propname
,
2312 nvlist_free(genericnvl
);
2313 nvlist_free(retrynvl
);
2315 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
2316 nvlist_free(errors
);
2319 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
2322 if (errlist
== NULL
)
2323 nvlist_free(errors
);
2331 * Check that all the properties are valid user properties.
2334 zfs_check_userprops(char *fsname
, nvlist_t
*nvl
)
2336 nvpair_t
*pair
= NULL
;
2339 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2340 const char *propname
= nvpair_name(pair
);
2343 if (!zfs_prop_user(propname
) ||
2344 nvpair_type(pair
) != DATA_TYPE_STRING
)
2347 if ((error
= zfs_secpolicy_write_perms(fsname
,
2348 ZFS_DELEG_PERM_USERPROP
, CRED())))
2351 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2352 return (ENAMETOOLONG
);
2354 VERIFY(nvpair_value_string(pair
, &valstr
) == 0);
2355 if (strlen(valstr
) >= ZAP_MAXVALUELEN
)
2362 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2366 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2369 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2370 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2373 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2378 clear_received_props(objset_t
*os
, const char *fs
, nvlist_t
*props
,
2382 nvlist_t
*cleared_props
= NULL
;
2383 props_skip(props
, skipped
, &cleared_props
);
2384 if (!nvlist_empty(cleared_props
)) {
2386 * Acts on local properties until the dataset has received
2387 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2389 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2390 (dsl_prop_get_hasrecvd(os
) ? ZPROP_SRC_RECEIVED
: 0));
2391 err
= zfs_set_prop_nvlist(fs
, flags
, cleared_props
, NULL
);
2393 nvlist_free(cleared_props
);
2399 * zc_name name of filesystem
2400 * zc_value name of property to set
2401 * zc_nvlist_src{_size} nvlist of properties to apply
2402 * zc_cookie received properties flag
2405 * zc_nvlist_dst{_size} error for each unapplied received property
2408 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2411 boolean_t received
= zc
->zc_cookie
;
2412 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2414 nvlist_t
*errors
= NULL
;
2417 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2418 zc
->zc_iflags
, &nvl
)) != 0)
2422 nvlist_t
*origprops
;
2425 if (dmu_objset_hold(zc
->zc_name
, FTAG
, &os
) == 0) {
2426 if (dsl_prop_get_received(os
, &origprops
) == 0) {
2427 (void) clear_received_props(os
,
2428 zc
->zc_name
, origprops
, nvl
);
2429 nvlist_free(origprops
);
2432 dsl_prop_set_hasrecvd(os
);
2433 dmu_objset_rele(os
, FTAG
);
2437 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, &errors
);
2439 if (zc
->zc_nvlist_dst
!= 0 && errors
!= NULL
) {
2440 (void) put_nvlist(zc
, errors
);
2443 nvlist_free(errors
);
2450 * zc_name name of filesystem
2451 * zc_value name of property to inherit
2452 * zc_cookie revert to received value if TRUE
2457 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2459 const char *propname
= zc
->zc_value
;
2460 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2461 boolean_t received
= zc
->zc_cookie
;
2462 zprop_source_t source
= (received
2463 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2464 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2473 * zfs_prop_set_special() expects properties in the form of an
2474 * nvpair with type info.
2476 if (prop
== ZPROP_INVAL
) {
2477 if (!zfs_prop_user(propname
))
2480 type
= PROP_TYPE_STRING
;
2481 } else if (prop
== ZFS_PROP_VOLSIZE
||
2482 prop
== ZFS_PROP_VERSION
) {
2485 type
= zfs_prop_get_type(prop
);
2488 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2491 case PROP_TYPE_STRING
:
2492 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2494 case PROP_TYPE_NUMBER
:
2495 case PROP_TYPE_INDEX
:
2496 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2503 pair
= nvlist_next_nvpair(dummy
, NULL
);
2504 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2507 return (err
); /* special property already handled */
2510 * Only check this in the non-received case. We want to allow
2511 * 'inherit -S' to revert non-inheritable properties like quota
2512 * and reservation to the received or default values even though
2513 * they are not considered inheritable.
2515 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2519 /* the property name has been validated by zfs_secpolicy_inherit() */
2520 return (dsl_prop_set(zc
->zc_name
, zc
->zc_value
, source
, 0, 0, NULL
));
2524 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2531 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2532 zc
->zc_iflags
, &props
)))
2536 * If the only property is the configfile, then just do a spa_lookup()
2537 * to handle the faulted case.
2539 pair
= nvlist_next_nvpair(props
, NULL
);
2540 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2541 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2542 nvlist_next_nvpair(props
, pair
) == NULL
) {
2543 mutex_enter(&spa_namespace_lock
);
2544 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2545 spa_configfile_set(spa
, props
, B_FALSE
);
2546 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2548 mutex_exit(&spa_namespace_lock
);
2555 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2560 error
= spa_prop_set(spa
, props
);
2563 spa_close(spa
, FTAG
);
2569 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2573 nvlist_t
*nvp
= NULL
;
2575 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2577 * If the pool is faulted, there may be properties we can still
2578 * get (such as altroot and cachefile), so attempt to get them
2581 mutex_enter(&spa_namespace_lock
);
2582 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2583 error
= spa_prop_get(spa
, &nvp
);
2584 mutex_exit(&spa_namespace_lock
);
2586 error
= spa_prop_get(spa
, &nvp
);
2587 spa_close(spa
, FTAG
);
2590 if (error
== 0 && zc
->zc_nvlist_dst
!= 0)
2591 error
= put_nvlist(zc
, nvp
);
2601 * zc_name name of volume
2606 zfs_ioc_create_minor(zfs_cmd_t
*zc
)
2608 return (zvol_create_minor(zc
->zc_name
));
2613 * zc_name name of volume
2618 zfs_ioc_remove_minor(zfs_cmd_t
*zc
)
2620 return (zvol_remove_minor(zc
->zc_name
));
2625 * zc_name name of filesystem
2626 * zc_nvlist_src{_size} nvlist of delegated permissions
2627 * zc_perm_action allow/unallow flag
2632 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2635 nvlist_t
*fsaclnv
= NULL
;
2637 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2638 zc
->zc_iflags
, &fsaclnv
)) != 0)
2642 * Verify nvlist is constructed correctly
2644 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2645 nvlist_free(fsaclnv
);
2650 * If we don't have PRIV_SYS_MOUNT, then validate
2651 * that user is allowed to hand out each permission in
2655 error
= secpolicy_zfs(CRED());
2657 if (zc
->zc_perm_action
== B_FALSE
) {
2658 error
= dsl_deleg_can_allow(zc
->zc_name
,
2661 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2667 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2669 nvlist_free(fsaclnv
);
2675 * zc_name name of filesystem
2678 * zc_nvlist_src{_size} nvlist of delegated permissions
2681 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2686 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2687 error
= put_nvlist(zc
, nvp
);
2696 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2698 zfs_creat_t
*zct
= arg
;
2700 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2703 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2707 * createprops list of properties requested by creator
2708 * default_zplver zpl version to use if unspecified in createprops
2709 * fuids_ok fuids allowed in this version of the spa?
2710 * os parent objset pointer (NULL if root fs)
2713 * zplprops values for the zplprops we attach to the master node object
2714 * is_ci true if requested file system will be purely case-insensitive
2716 * Determine the settings for utf8only, normalization and
2717 * casesensitivity. Specific values may have been requested by the
2718 * creator and/or we can inherit values from the parent dataset. If
2719 * the file system is of too early a vintage, a creator can not
2720 * request settings for these properties, even if the requested
2721 * setting is the default value. We don't actually want to create dsl
2722 * properties for these, so remove them from the source nvlist after
2726 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2727 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2728 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2730 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2731 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2732 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2734 ASSERT(zplprops
!= NULL
);
2737 * Pull out creator prop choices, if any.
2740 (void) nvlist_lookup_uint64(createprops
,
2741 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
2742 (void) nvlist_lookup_uint64(createprops
,
2743 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
2744 (void) nvlist_remove_all(createprops
,
2745 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
2746 (void) nvlist_lookup_uint64(createprops
,
2747 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
2748 (void) nvlist_remove_all(createprops
,
2749 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
2750 (void) nvlist_lookup_uint64(createprops
,
2751 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
2752 (void) nvlist_remove_all(createprops
,
2753 zfs_prop_to_name(ZFS_PROP_CASE
));
2757 * If the zpl version requested is whacky or the file system
2758 * or pool is version is too "young" to support normalization
2759 * and the creator tried to set a value for one of the props,
2762 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
2763 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
2764 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
2765 (zplver
< ZPL_VERSION_NORMALIZATION
&&
2766 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
2767 sense
!= ZFS_PROP_UNDEFINED
)))
2771 * Put the version in the zplprops
2773 VERIFY(nvlist_add_uint64(zplprops
,
2774 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
2776 if (norm
== ZFS_PROP_UNDEFINED
)
2777 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
2778 VERIFY(nvlist_add_uint64(zplprops
,
2779 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
2782 * If we're normalizing, names must always be valid UTF-8 strings.
2786 if (u8
== ZFS_PROP_UNDEFINED
)
2787 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
2788 VERIFY(nvlist_add_uint64(zplprops
,
2789 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
2791 if (sense
== ZFS_PROP_UNDEFINED
)
2792 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
2793 VERIFY(nvlist_add_uint64(zplprops
,
2794 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
2797 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
2803 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
2804 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2806 boolean_t fuids_ok
, sa_ok
;
2807 uint64_t zplver
= ZPL_VERSION
;
2808 objset_t
*os
= NULL
;
2809 char parentname
[MAXNAMELEN
];
2815 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
2816 cp
= strrchr(parentname
, '/');
2820 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
2823 spa_vers
= spa_version(spa
);
2824 spa_close(spa
, FTAG
);
2826 zplver
= zfs_zpl_version_map(spa_vers
);
2827 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
2828 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
2831 * Open parent object set so we can inherit zplprop values.
2833 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
2836 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
2838 dmu_objset_rele(os
, FTAG
);
2843 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
2844 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2848 uint64_t zplver
= ZPL_VERSION
;
2851 zplver
= zfs_zpl_version_map(spa_vers
);
2852 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
2853 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
2855 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
2856 createprops
, zplprops
, is_ci
);
2862 * zc_objset_type type of objset to create (fs vs zvol)
2863 * zc_name name of new objset
2864 * zc_value name of snapshot to clone from (may be empty)
2865 * zc_nvlist_src{_size} nvlist of properties to apply
2870 zfs_ioc_create(zfs_cmd_t
*zc
)
2875 nvlist_t
*nvprops
= NULL
;
2876 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
2877 dmu_objset_type_t type
= zc
->zc_objset_type
;
2882 cbfunc
= zfs_create_cb
;
2886 cbfunc
= zvol_create_cb
;
2893 if (strchr(zc
->zc_name
, '@') ||
2894 strchr(zc
->zc_name
, '%'))
2897 if (zc
->zc_nvlist_src
!= 0 &&
2898 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2899 zc
->zc_iflags
, &nvprops
)) != 0)
2902 zct
.zct_zplprops
= NULL
;
2903 zct
.zct_props
= nvprops
;
2905 if (zc
->zc_value
[0] != '\0') {
2907 * We're creating a clone of an existing snapshot.
2909 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2910 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0) {
2911 nvlist_free(nvprops
);
2915 error
= dmu_objset_hold(zc
->zc_value
, FTAG
, &clone
);
2917 nvlist_free(nvprops
);
2921 error
= dmu_objset_clone(zc
->zc_name
, dmu_objset_ds(clone
), 0);
2922 dmu_objset_rele(clone
, FTAG
);
2924 nvlist_free(nvprops
);
2928 boolean_t is_insensitive
= B_FALSE
;
2930 if (cbfunc
== NULL
) {
2931 nvlist_free(nvprops
);
2935 if (type
== DMU_OST_ZVOL
) {
2936 uint64_t volsize
, volblocksize
;
2938 if (nvprops
== NULL
||
2939 nvlist_lookup_uint64(nvprops
,
2940 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
2942 nvlist_free(nvprops
);
2946 if ((error
= nvlist_lookup_uint64(nvprops
,
2947 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2948 &volblocksize
)) != 0 && error
!= ENOENT
) {
2949 nvlist_free(nvprops
);
2954 volblocksize
= zfs_prop_default_numeric(
2955 ZFS_PROP_VOLBLOCKSIZE
);
2957 if ((error
= zvol_check_volblocksize(
2958 volblocksize
)) != 0 ||
2959 (error
= zvol_check_volsize(volsize
,
2960 volblocksize
)) != 0) {
2961 nvlist_free(nvprops
);
2964 } else if (type
== DMU_OST_ZFS
) {
2968 * We have to have normalization and
2969 * case-folding flags correct when we do the
2970 * file system creation, so go figure them out
2973 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
2974 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2975 error
= zfs_fill_zplprops(zc
->zc_name
, nvprops
,
2976 zct
.zct_zplprops
, &is_insensitive
);
2978 nvlist_free(nvprops
);
2979 nvlist_free(zct
.zct_zplprops
);
2983 error
= dmu_objset_create(zc
->zc_name
, type
,
2984 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
2985 nvlist_free(zct
.zct_zplprops
);
2989 * It would be nice to do this atomically.
2992 error
= zfs_set_prop_nvlist(zc
->zc_name
, ZPROP_SRC_LOCAL
,
2995 (void) dmu_objset_destroy(zc
->zc_name
, B_FALSE
);
2997 nvlist_free(nvprops
);
3003 * zc_name name of filesystem
3004 * zc_value short name of snapshot
3005 * zc_cookie recursive flag
3006 * zc_nvlist_src[_size] property list
3009 * zc_value short snapname (i.e. part after the '@')
3012 zfs_ioc_snapshot(zfs_cmd_t
*zc
)
3014 nvlist_t
*nvprops
= NULL
;
3016 boolean_t recursive
= zc
->zc_cookie
;
3018 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
3021 if (zc
->zc_nvlist_src
!= 0 &&
3022 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3023 zc
->zc_iflags
, &nvprops
)) != 0)
3026 error
= zfs_check_userprops(zc
->zc_name
, nvprops
);
3030 if (!nvlist_empty(nvprops
) &&
3031 zfs_earlier_version(zc
->zc_name
, SPA_VERSION_SNAP_PROPS
)) {
3036 error
= dmu_objset_snapshot(zc
->zc_name
, zc
->zc_value
, NULL
,
3037 nvprops
, recursive
, B_FALSE
, -1);
3040 nvlist_free(nvprops
);
3046 * name dataset name, or when 'arg == NULL' the full snapshot name
3047 * arg short snapshot name (i.e. part after the '@')
3050 zfs_unmount_snap(const char *name
, void *arg
)
3052 zfs_sb_t
*zsb
= NULL
;
3060 dsname
= strdup(name
);
3061 snapname
= strdup(arg
);
3063 ptr
= strchr(name
, '@');
3065 dsname
= strdup(name
);
3066 dsname
[ptr
- name
] = '\0';
3067 snapname
= strdup(ptr
+ 1);
3073 fullname
= kmem_asprintf("%s@%s", dsname
, snapname
);
3075 error
= zfs_sb_hold(dsname
, FTAG
, &zsb
, B_FALSE
);
3077 error
= zfsctl_unmount_snapshot(zsb
, fullname
, MNT_FORCE
);
3078 zfs_sb_rele(zsb
, FTAG
);
3080 /* Allow ENOENT for consistency with upstream */
3081 if (error
== ENOENT
)
3094 * zc_name name of filesystem
3095 * zc_value short name of snapshot
3096 * zc_defer_destroy mark for deferred destroy
3101 zfs_ioc_destroy_snaps(zfs_cmd_t
*zc
)
3105 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
3107 err
= dmu_objset_find(zc
->zc_name
,
3108 zfs_unmount_snap
, zc
->zc_value
, DS_FIND_CHILDREN
);
3111 return (dmu_snapshots_destroy(zc
->zc_name
, zc
->zc_value
,
3112 zc
->zc_defer_destroy
));
3117 * zc_name name of dataset to destroy
3118 * zc_objset_type type of objset
3119 * zc_defer_destroy mark for deferred destroy
3124 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3127 if (strchr(zc
->zc_name
, '@') && zc
->zc_objset_type
== DMU_OST_ZFS
) {
3128 err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
3133 err
= dmu_objset_destroy(zc
->zc_name
, zc
->zc_defer_destroy
);
3134 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3135 (void) zvol_remove_minor(zc
->zc_name
);
3141 * zc_name name of dataset to rollback (to most recent snapshot)
3146 zfs_ioc_rollback(zfs_cmd_t
*zc
)
3148 dsl_dataset_t
*ds
, *clone
;
3153 error
= dsl_dataset_hold(zc
->zc_name
, FTAG
, &ds
);
3157 /* must not be a snapshot */
3158 if (dsl_dataset_is_snapshot(ds
)) {
3159 dsl_dataset_rele(ds
, FTAG
);
3163 /* must have a most recent snapshot */
3164 if (ds
->ds_phys
->ds_prev_snap_txg
< TXG_INITIAL
) {
3165 dsl_dataset_rele(ds
, FTAG
);
3170 * Create clone of most recent snapshot.
3172 clone_name
= kmem_asprintf("%s/%%rollback", zc
->zc_name
);
3173 error
= dmu_objset_clone(clone_name
, ds
->ds_prev
, DS_FLAG_INCONSISTENT
);
3177 error
= dsl_dataset_own(clone_name
, B_TRUE
, FTAG
, &clone
);
3184 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
3185 error
= zfs_suspend_fs(zsb
);
3189 if (dsl_dataset_tryown(ds
, B_FALSE
, FTAG
)) {
3190 error
= dsl_dataset_clone_swap(clone
, ds
,
3192 dsl_dataset_disown(ds
, FTAG
);
3197 resume_err
= zfs_resume_fs(zsb
, zc
->zc_name
);
3198 error
= error
? error
: resume_err
;
3200 deactivate_super(zsb
->z_sb
);
3202 if (dsl_dataset_tryown(ds
, B_FALSE
, FTAG
)) {
3203 error
= dsl_dataset_clone_swap(clone
, ds
, B_TRUE
);
3204 dsl_dataset_disown(ds
, FTAG
);
3212 * Destroy clone (which also closes it).
3214 (void) dsl_dataset_destroy(clone
, FTAG
, B_FALSE
);
3217 strfree(clone_name
);
3219 dsl_dataset_rele(ds
, FTAG
);
3225 * zc_name old name of dataset
3226 * zc_value new name of dataset
3227 * zc_cookie recursive flag (only valid for snapshots)
3232 zfs_ioc_rename(zfs_cmd_t
*zc
)
3234 boolean_t recursive
= zc
->zc_cookie
& 1;
3237 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3238 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3239 strchr(zc
->zc_value
, '%'))
3243 * Unmount snapshot unless we're doing a recursive rename,
3244 * in which case the dataset code figures out which snapshots
3247 if (!recursive
&& strchr(zc
->zc_name
, '@') != NULL
&&
3248 zc
->zc_objset_type
== DMU_OST_ZFS
) {
3249 err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
3254 err
= dmu_objset_rename(zc
->zc_name
, zc
->zc_value
, recursive
);
3255 if ((err
== 0) && (zc
->zc_objset_type
== DMU_OST_ZVOL
)) {
3256 (void) zvol_remove_minor(zc
->zc_name
);
3257 (void) zvol_create_minor(zc
->zc_value
);
3264 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3266 const char *propname
= nvpair_name(pair
);
3267 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3268 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3272 if (prop
== ZPROP_INVAL
) {
3273 if (zfs_prop_user(propname
)) {
3274 if ((err
= zfs_secpolicy_write_perms(dsname
,
3275 ZFS_DELEG_PERM_USERPROP
, cr
)))
3280 if (!issnap
&& zfs_prop_userquota(propname
)) {
3281 const char *perm
= NULL
;
3282 const char *uq_prefix
=
3283 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3284 const char *gq_prefix
=
3285 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3287 if (strncmp(propname
, uq_prefix
,
3288 strlen(uq_prefix
)) == 0) {
3289 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3290 } else if (strncmp(propname
, gq_prefix
,
3291 strlen(gq_prefix
)) == 0) {
3292 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3294 /* USERUSED and GROUPUSED are read-only */
3298 if ((err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
)))
3309 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3311 * dsl_prop_get_all_impl() returns properties in this
3315 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3316 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3321 * Check that this value is valid for this pool version
3324 case ZFS_PROP_COMPRESSION
:
3326 * If the user specified gzip compression, make sure
3327 * the SPA supports it. We ignore any errors here since
3328 * we'll catch them later.
3330 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3331 nvpair_value_uint64(pair
, &intval
) == 0) {
3332 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3333 intval
<= ZIO_COMPRESS_GZIP_9
&&
3334 zfs_earlier_version(dsname
,
3335 SPA_VERSION_GZIP_COMPRESSION
)) {
3339 if (intval
== ZIO_COMPRESS_ZLE
&&
3340 zfs_earlier_version(dsname
,
3341 SPA_VERSION_ZLE_COMPRESSION
))
3345 * If this is a bootable dataset then
3346 * verify that the compression algorithm
3347 * is supported for booting. We must return
3348 * something other than ENOTSUP since it
3349 * implies a downrev pool version.
3351 if (zfs_is_bootfs(dsname
) &&
3352 !BOOTFS_COMPRESS_VALID(intval
)) {
3358 case ZFS_PROP_COPIES
:
3359 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3363 case ZFS_PROP_DEDUP
:
3364 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3368 case ZFS_PROP_SHARESMB
:
3369 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3373 case ZFS_PROP_ACLINHERIT
:
3374 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3375 nvpair_value_uint64(pair
, &intval
) == 0) {
3376 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3377 zfs_earlier_version(dsname
,
3378 SPA_VERSION_PASSTHROUGH_X
))
3386 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3390 * Removes properties from the given props list that fail permission checks
3391 * needed to clear them and to restore them in case of a receive error. For each
3392 * property, make sure we have both set and inherit permissions.
3394 * Returns the first error encountered if any permission checks fail. If the
3395 * caller provides a non-NULL errlist, it also gives the complete list of names
3396 * of all the properties that failed a permission check along with the
3397 * corresponding error numbers. The caller is responsible for freeing the
3400 * If every property checks out successfully, zero is returned and the list
3401 * pointed at by errlist is NULL.
3404 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3407 nvpair_t
*pair
, *next_pair
;
3414 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3416 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
| KM_NODEBUG
);
3417 (void) strcpy(zc
->zc_name
, dataset
);
3418 pair
= nvlist_next_nvpair(props
, NULL
);
3419 while (pair
!= NULL
) {
3420 next_pair
= nvlist_next_nvpair(props
, pair
);
3422 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3423 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3424 (err
= zfs_secpolicy_inherit(zc
, CRED())) != 0) {
3425 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3426 VERIFY(nvlist_add_int32(errors
,
3427 zc
->zc_value
, err
) == 0);
3431 kmem_free(zc
, sizeof (zfs_cmd_t
));
3433 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3434 nvlist_free(errors
);
3437 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3440 if (errlist
== NULL
)
3441 nvlist_free(errors
);
3449 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3451 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3452 /* dsl_prop_get_all_impl() format */
3454 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3455 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3459 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3461 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3462 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3466 if (nvpair_type(p1
) != nvpair_type(p2
))
3469 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
3470 char *valstr1
, *valstr2
;
3472 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
3473 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
3474 return (strcmp(valstr1
, valstr2
) == 0);
3476 uint64_t intval1
, intval2
;
3478 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
3479 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
3480 return (intval1
== intval2
);
3485 * Remove properties from props if they are not going to change (as determined
3486 * by comparison with origprops). Remove them from origprops as well, since we
3487 * do not need to clear or restore properties that won't change.
3490 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
3492 nvpair_t
*pair
, *next_pair
;
3494 if (origprops
== NULL
)
3495 return; /* all props need to be received */
3497 pair
= nvlist_next_nvpair(props
, NULL
);
3498 while (pair
!= NULL
) {
3499 const char *propname
= nvpair_name(pair
);
3502 next_pair
= nvlist_next_nvpair(props
, pair
);
3504 if ((nvlist_lookup_nvpair(origprops
, propname
,
3505 &match
) != 0) || !propval_equals(pair
, match
))
3506 goto next
; /* need to set received value */
3508 /* don't clear the existing received value */
3509 (void) nvlist_remove_nvpair(origprops
, match
);
3510 /* don't bother receiving the property */
3511 (void) nvlist_remove_nvpair(props
, pair
);
3518 static boolean_t zfs_ioc_recv_inject_err
;
3523 * zc_name name of containing filesystem
3524 * zc_nvlist_src{_size} nvlist of properties to apply
3525 * zc_value name of snapshot to create
3526 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3527 * zc_cookie file descriptor to recv from
3528 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3529 * zc_guid force flag
3530 * zc_cleanup_fd cleanup-on-exit file descriptor
3531 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3534 * zc_cookie number of bytes read
3535 * zc_nvlist_dst{_size} error for each unapplied received property
3536 * zc_obj zprop_errflags_t
3537 * zc_action_handle handle for this guid/ds mapping
3540 zfs_ioc_recv(zfs_cmd_t
*zc
)
3544 dmu_recv_cookie_t drc
;
3545 boolean_t force
= (boolean_t
)zc
->zc_guid
;
3548 int props_error
= 0;
3551 nvlist_t
*props
= NULL
; /* sent properties */
3552 nvlist_t
*origprops
= NULL
; /* existing properties */
3553 objset_t
*origin
= NULL
;
3555 char tofs
[ZFS_MAXNAMELEN
];
3556 boolean_t first_recvd_props
= B_FALSE
;
3558 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3559 strchr(zc
->zc_value
, '@') == NULL
||
3560 strchr(zc
->zc_value
, '%'))
3563 (void) strcpy(tofs
, zc
->zc_value
);
3564 tosnap
= strchr(tofs
, '@');
3567 if (zc
->zc_nvlist_src
!= 0 &&
3568 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3569 zc
->zc_iflags
, &props
)) != 0)
3579 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3581 if (props
&& dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
3582 if ((spa_version(os
->os_spa
) >= SPA_VERSION_RECVD_PROPS
) &&
3583 !dsl_prop_get_hasrecvd(os
)) {
3584 first_recvd_props
= B_TRUE
;
3588 * If new received properties are supplied, they are to
3589 * completely replace the existing received properties, so stash
3590 * away the existing ones.
3592 if (dsl_prop_get_received(os
, &origprops
) == 0) {
3593 nvlist_t
*errlist
= NULL
;
3595 * Don't bother writing a property if its value won't
3596 * change (and avoid the unnecessary security checks).
3598 * The first receive after SPA_VERSION_RECVD_PROPS is a
3599 * special case where we blow away all local properties
3602 if (!first_recvd_props
)
3603 props_reduce(props
, origprops
);
3604 if (zfs_check_clearable(tofs
, origprops
,
3606 (void) nvlist_merge(errors
, errlist
, 0);
3607 nvlist_free(errlist
);
3610 dmu_objset_rele(os
, FTAG
);
3613 if (zc
->zc_string
[0]) {
3614 error
= dmu_objset_hold(zc
->zc_string
, FTAG
, &origin
);
3619 error
= dmu_recv_begin(tofs
, tosnap
, zc
->zc_top_ds
,
3620 &zc
->zc_begin_record
, force
, origin
, &drc
);
3622 dmu_objset_rele(origin
, FTAG
);
3627 * Set properties before we receive the stream so that they are applied
3628 * to the new data. Note that we must call dmu_recv_stream() if
3629 * dmu_recv_begin() succeeds.
3634 if (dmu_objset_from_ds(drc
.drc_logical_ds
, &os
) == 0) {
3635 if (drc
.drc_newfs
) {
3636 if (spa_version(os
->os_spa
) >=
3637 SPA_VERSION_RECVD_PROPS
)
3638 first_recvd_props
= B_TRUE
;
3639 } else if (origprops
!= NULL
) {
3640 if (clear_received_props(os
, tofs
, origprops
,
3641 first_recvd_props
? NULL
: props
) != 0)
3642 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3644 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3646 dsl_prop_set_hasrecvd(os
);
3647 } else if (!drc
.drc_newfs
) {
3648 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3651 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
3653 (void) nvlist_merge(errors
, errlist
, 0);
3654 nvlist_free(errlist
);
3657 if (fit_error_list(zc
, &errors
) != 0 || put_nvlist(zc
, errors
) != 0) {
3659 * Caller made zc->zc_nvlist_dst less than the minimum expected
3660 * size or supplied an invalid address.
3662 props_error
= EINVAL
;
3666 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
3667 &zc
->zc_action_handle
);
3670 zfs_sb_t
*zsb
= NULL
;
3672 if (get_zfs_sb(tofs
, &zsb
) == 0) {
3676 error
= zfs_suspend_fs(zsb
);
3678 * If the suspend fails, then the recv_end will
3679 * likely also fail, and clean up after itself.
3681 end_err
= dmu_recv_end(&drc
);
3683 error
= zfs_resume_fs(zsb
, tofs
);
3684 error
= error
? error
: end_err
;
3685 deactivate_super(zsb
->z_sb
);
3687 error
= dmu_recv_end(&drc
);
3691 zc
->zc_cookie
= off
- fp
->f_offset
;
3692 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
3696 if (zfs_ioc_recv_inject_err
) {
3697 zfs_ioc_recv_inject_err
= B_FALSE
;
3702 * On error, restore the original props.
3704 if (error
&& props
) {
3705 if (dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
3706 if (clear_received_props(os
, tofs
, props
, NULL
) != 0) {
3708 * We failed to clear the received properties.
3709 * Since we may have left a $recvd value on the
3710 * system, we can't clear the $hasrecvd flag.
3712 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3713 } else if (first_recvd_props
) {
3714 dsl_prop_unset_hasrecvd(os
);
3716 dmu_objset_rele(os
, FTAG
);
3717 } else if (!drc
.drc_newfs
) {
3718 /* We failed to clear the received properties. */
3719 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3722 if (origprops
== NULL
&& !drc
.drc_newfs
) {
3723 /* We failed to stash the original properties. */
3724 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3728 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3729 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3730 * explictly if we're restoring local properties cleared in the
3731 * first new-style receive.
3733 if (origprops
!= NULL
&&
3734 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
3735 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
3736 origprops
, NULL
) != 0) {
3738 * We stashed the original properties but failed to
3741 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3746 nvlist_free(origprops
);
3747 nvlist_free(errors
);
3751 error
= props_error
;
3758 * zc_name name of snapshot to send
3759 * zc_cookie file descriptor to send stream to
3760 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3761 * zc_sendobj objsetid of snapshot to send
3762 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3767 zfs_ioc_send(zfs_cmd_t
*zc
)
3769 objset_t
*fromsnap
= NULL
;
3775 dsl_dataset_t
*dsfrom
= NULL
;
3779 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
3783 dp
= spa_get_dsl(spa
);
3784 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
3785 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
3786 rw_exit(&dp
->dp_config_rwlock
);
3788 spa_close(spa
, FTAG
);
3792 error
= dmu_objset_from_ds(ds
, &tosnap
);
3794 dsl_dataset_rele(ds
, FTAG
);
3795 spa_close(spa
, FTAG
);
3799 if (zc
->zc_fromobj
!= 0) {
3800 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
3801 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
, FTAG
, &dsfrom
);
3802 rw_exit(&dp
->dp_config_rwlock
);
3803 spa_close(spa
, FTAG
);
3805 dsl_dataset_rele(ds
, FTAG
);
3808 error
= dmu_objset_from_ds(dsfrom
, &fromsnap
);
3810 dsl_dataset_rele(dsfrom
, FTAG
);
3811 dsl_dataset_rele(ds
, FTAG
);
3815 spa_close(spa
, FTAG
);
3818 fp
= getf(zc
->zc_cookie
);
3820 dsl_dataset_rele(ds
, FTAG
);
3822 dsl_dataset_rele(dsfrom
, FTAG
);
3827 error
= dmu_sendbackup(tosnap
, fromsnap
, zc
->zc_obj
, fp
->f_vnode
, &off
);
3829 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
3831 releasef(zc
->zc_cookie
);
3833 dsl_dataset_rele(dsfrom
, FTAG
);
3834 dsl_dataset_rele(ds
, FTAG
);
3839 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
3843 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
3844 &zc
->zc_inject_record
);
3847 zc
->zc_guid
= (uint64_t)id
;
3853 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
3855 return (zio_clear_fault((int)zc
->zc_guid
));
3859 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
3861 int id
= (int)zc
->zc_guid
;
3864 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
3865 &zc
->zc_inject_record
);
3873 zfs_ioc_error_log(zfs_cmd_t
*zc
)
3877 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
3879 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
3882 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
3885 zc
->zc_nvlist_dst_size
= count
;
3887 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
3889 spa_close(spa
, FTAG
);
3895 zfs_ioc_clear(zfs_cmd_t
*zc
)
3902 * On zpool clear we also fix up missing slogs
3904 mutex_enter(&spa_namespace_lock
);
3905 spa
= spa_lookup(zc
->zc_name
);
3907 mutex_exit(&spa_namespace_lock
);
3910 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
3911 /* we need to let spa_open/spa_load clear the chains */
3912 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3914 spa
->spa_last_open_failed
= 0;
3915 mutex_exit(&spa_namespace_lock
);
3917 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
3918 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
3921 nvlist_t
*config
= NULL
;
3923 if (zc
->zc_nvlist_src
== 0)
3926 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
3927 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
3928 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
3930 if (config
!= NULL
) {
3933 if ((err
= put_nvlist(zc
, config
)) != 0)
3935 nvlist_free(config
);
3937 nvlist_free(policy
);
3944 spa_vdev_state_enter(spa
, SCL_NONE
);
3946 if (zc
->zc_guid
== 0) {
3949 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
3951 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
3952 spa_close(spa
, FTAG
);
3957 vdev_clear(spa
, vd
);
3959 (void) spa_vdev_state_exit(spa
, NULL
, 0);
3962 * Resume any suspended I/Os.
3964 if (zio_resume(spa
) != 0)
3967 spa_close(spa
, FTAG
);
3974 * zc_name name of filesystem
3975 * zc_value name of origin snapshot
3978 * zc_string name of conflicting snapshot, if there is one
3981 zfs_ioc_promote(zfs_cmd_t
*zc
)
3986 * We don't need to unmount *all* the origin fs's snapshots, but
3989 cp
= strchr(zc
->zc_value
, '@');
3992 (void) dmu_objset_find(zc
->zc_value
,
3993 zfs_unmount_snap
, NULL
, DS_FIND_SNAPSHOTS
);
3994 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
3998 * Retrieve a single {user|group}{used|quota}@... property.
4001 * zc_name name of filesystem
4002 * zc_objset_type zfs_userquota_prop_t
4003 * zc_value domain name (eg. "S-1-234-567-89")
4004 * zc_guid RID/UID/GID
4007 * zc_cookie property value
4010 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4015 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4018 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4022 error
= zfs_userspace_one(zsb
,
4023 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4024 zfs_sb_rele(zsb
, FTAG
);
4031 * zc_name name of filesystem
4032 * zc_cookie zap cursor
4033 * zc_objset_type zfs_userquota_prop_t
4034 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4037 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4038 * zc_cookie zap cursor
4041 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4044 int bufsize
= zc
->zc_nvlist_dst_size
;
4051 error
= zfs_sb_hold(zc
->zc_name
, FTAG
, &zsb
, B_FALSE
);
4055 buf
= vmem_alloc(bufsize
, KM_SLEEP
);
4057 error
= zfs_userspace_many(zsb
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4058 buf
, &zc
->zc_nvlist_dst_size
);
4061 error
= xcopyout(buf
,
4062 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4063 zc
->zc_nvlist_dst_size
);
4065 vmem_free(buf
, bufsize
);
4066 zfs_sb_rele(zsb
, FTAG
);
4073 * zc_name name of filesystem
4079 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4085 if (get_zfs_sb(zc
->zc_name
, &zsb
) == 0) {
4086 if (!dmu_objset_userused_enabled(zsb
->z_os
)) {
4088 * If userused is not enabled, it may be because the
4089 * objset needs to be closed & reopened (to grow the
4090 * objset_phys_t). Suspend/resume the fs will do that.
4092 error
= zfs_suspend_fs(zsb
);
4094 error
= zfs_resume_fs(zsb
, zc
->zc_name
);
4097 error
= dmu_objset_userspace_upgrade(zsb
->z_os
);
4098 deactivate_super(zsb
->z_sb
);
4100 /* XXX kind of reading contents without owning */
4101 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4105 error
= dmu_objset_userspace_upgrade(os
);
4106 dmu_objset_rele(os
, FTAG
);
4113 zfs_ioc_share(zfs_cmd_t
*zc
)
4118 ace_t full_access
[] = {
4119 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4124 * zc_name name of containing filesystem
4125 * zc_obj object # beyond which we want next in-use object #
4128 * zc_obj next in-use object #
4131 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4133 objset_t
*os
= NULL
;
4136 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4140 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
4141 os
->os_dsl_dataset
->ds_phys
->ds_prev_snap_txg
);
4143 dmu_objset_rele(os
, FTAG
);
4149 * zc_name name of filesystem
4150 * zc_value prefix name for snapshot
4151 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4156 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4161 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4162 (u_longlong_t
)ddi_get_lbolt64());
4164 if (strlen(snap_name
) >= MAXNAMELEN
) {
4169 error
= dmu_objset_snapshot(zc
->zc_name
, snap_name
, snap_name
,
4170 NULL
, B_FALSE
, B_TRUE
, zc
->zc_cleanup_fd
);
4176 (void) strcpy(zc
->zc_value
, snap_name
);
4183 * zc_name name of "to" snapshot
4184 * zc_value name of "from" snapshot
4185 * zc_cookie file descriptor to write diff data on
4188 * dmu_diff_record_t's to the file descriptor
4191 zfs_ioc_diff(zfs_cmd_t
*zc
)
4199 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &tosnap
);
4203 error
= dmu_objset_hold(zc
->zc_value
, FTAG
, &fromsnap
);
4205 dmu_objset_rele(tosnap
, FTAG
);
4209 fp
= getf(zc
->zc_cookie
);
4211 dmu_objset_rele(fromsnap
, FTAG
);
4212 dmu_objset_rele(tosnap
, FTAG
);
4218 error
= dmu_diff(tosnap
, fromsnap
, fp
->f_vnode
, &off
);
4220 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4222 releasef(zc
->zc_cookie
);
4224 dmu_objset_rele(fromsnap
, FTAG
);
4225 dmu_objset_rele(tosnap
, FTAG
);
4230 * Remove all ACL files in shares dir
4232 #ifdef HAVE_SMB_SHARE
4234 zfs_smb_acl_purge(znode_t
*dzp
)
4237 zap_attribute_t zap
;
4238 zfs_sb_t
*zsb
= ZTOZSB(dzp
);
4241 for (zap_cursor_init(&zc
, zsb
->z_os
, dzp
->z_id
);
4242 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4243 zap_cursor_advance(&zc
)) {
4244 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4248 zap_cursor_fini(&zc
);
4251 #endif /* HAVE_SMB_SHARE */
4254 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4256 #ifdef HAVE_SMB_SHARE
4259 vnode_t
*resourcevp
= NULL
;
4268 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4269 NO_FOLLOW
, NULL
, &vp
)) != 0)
4272 /* Now make sure mntpnt and dataset are ZFS */
4274 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4275 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4276 zc
->zc_name
) != 0)) {
4286 * Create share dir if its missing.
4288 mutex_enter(&zsb
->z_lock
);
4289 if (zsb
->z_shares_dir
== 0) {
4292 tx
= dmu_tx_create(zsb
->z_os
);
4293 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
4295 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
4296 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4300 error
= zfs_create_share_dir(zsb
, tx
);
4304 mutex_exit(&zsb
->z_lock
);
4310 mutex_exit(&zsb
->z_lock
);
4312 ASSERT(zsb
->z_shares_dir
);
4313 if ((error
= zfs_zget(zsb
, zsb
->z_shares_dir
, &sharedir
)) != 0) {
4319 switch (zc
->zc_cookie
) {
4320 case ZFS_SMB_ACL_ADD
:
4321 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
4322 vattr
.va_mode
= S_IFREG
|0777;
4326 vsec
.vsa_mask
= VSA_ACE
;
4327 vsec
.vsa_aclentp
= &full_access
;
4328 vsec
.vsa_aclentsz
= sizeof (full_access
);
4329 vsec
.vsa_aclcnt
= 1;
4331 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
4332 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
4334 VN_RELE(resourcevp
);
4337 case ZFS_SMB_ACL_REMOVE
:
4338 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
4342 case ZFS_SMB_ACL_RENAME
:
4343 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4344 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
4349 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
4350 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4353 VN_RELE(ZTOV(sharedir
));
4355 nvlist_free(nvlist
);
4358 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
4360 nvlist_free(nvlist
);
4363 case ZFS_SMB_ACL_PURGE
:
4364 error
= zfs_smb_acl_purge(sharedir
);
4373 VN_RELE(ZTOV(sharedir
));
4380 #endif /* HAVE_SMB_SHARE */
4385 * zc_name name of filesystem
4386 * zc_value short name of snap
4387 * zc_string user-supplied tag for this hold
4388 * zc_cookie recursive flag
4389 * zc_temphold set if hold is temporary
4390 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4391 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4392 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4397 zfs_ioc_hold(zfs_cmd_t
*zc
)
4399 boolean_t recursive
= zc
->zc_cookie
;
4406 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
4409 if (zc
->zc_sendobj
== 0) {
4410 return (dsl_dataset_user_hold(zc
->zc_name
, zc
->zc_value
,
4411 zc
->zc_string
, recursive
, zc
->zc_temphold
,
4412 zc
->zc_cleanup_fd
));
4418 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4422 dp
= spa_get_dsl(spa
);
4423 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
4424 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
4425 rw_exit(&dp
->dp_config_rwlock
);
4426 spa_close(spa
, FTAG
);
4431 * Until we have a hold on this snapshot, it's possible that
4432 * zc_sendobj could've been destroyed and reused as part
4433 * of a later txg. Make sure we're looking at the right object.
4435 if (zc
->zc_createtxg
!= ds
->ds_phys
->ds_creation_txg
) {
4436 dsl_dataset_rele(ds
, FTAG
);
4440 if (zc
->zc_cleanup_fd
!= -1 && zc
->zc_temphold
) {
4441 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4443 dsl_dataset_rele(ds
, FTAG
);
4448 error
= dsl_dataset_user_hold_for_send(ds
, zc
->zc_string
,
4452 dsl_register_onexit_hold_cleanup(ds
, zc
->zc_string
,
4455 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4457 dsl_dataset_rele(ds
, FTAG
);
4464 * zc_name name of dataset from which we're releasing a user hold
4465 * zc_value short name of snap
4466 * zc_string user-supplied tag for this hold
4467 * zc_cookie recursive flag
4472 zfs_ioc_release(zfs_cmd_t
*zc
)
4474 boolean_t recursive
= zc
->zc_cookie
;
4476 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
4479 return (dsl_dataset_user_release(zc
->zc_name
, zc
->zc_value
,
4480 zc
->zc_string
, recursive
));
4485 * zc_name name of filesystem
4488 * zc_nvlist_src{_size} nvlist of snapshot holds
4491 zfs_ioc_get_holds(zfs_cmd_t
*zc
)
4496 if ((error
= dsl_dataset_get_holds(zc
->zc_name
, &nvp
)) == 0) {
4497 error
= put_nvlist(zc
, nvp
);
4506 * zc_guid flags (ZEVENT_NONBLOCK)
4509 * zc_nvlist_dst next nvlist event
4510 * zc_cookie dropped events since last get
4511 * zc_cleanup_fd cleanup-on-exit file descriptor
4514 zfs_ioc_events_next(zfs_cmd_t
*zc
)
4517 nvlist_t
*event
= NULL
;
4519 uint64_t dropped
= 0;
4522 error
= zfs_zevent_fd_hold(zc
->zc_cleanup_fd
, &minor
, &ze
);
4527 error
= zfs_zevent_next(ze
, &event
,
4528 &zc
->zc_nvlist_dst_size
, &dropped
);
4529 if (event
!= NULL
) {
4530 zc
->zc_cookie
= dropped
;
4531 error
= put_nvlist(zc
, event
);
4535 if (zc
->zc_guid
& ZEVENT_NONBLOCK
)
4538 if ((error
== 0) || (error
!= ENOENT
))
4541 error
= zfs_zevent_wait(ze
);
4546 zfs_zevent_fd_rele(zc
->zc_cleanup_fd
);
4553 * zc_cookie cleared events count
4556 zfs_ioc_events_clear(zfs_cmd_t
*zc
)
4560 zfs_zevent_drain_all(&count
);
4561 zc
->zc_cookie
= count
;
4567 * pool create, destroy, and export don't log the history as part of
4568 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4569 * do the logging of those commands.
4571 static zfs_ioc_vec_t zfs_ioc_vec
[] = {
4572 { zfs_ioc_pool_create
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4574 { zfs_ioc_pool_destroy
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4576 { zfs_ioc_pool_import
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4578 { zfs_ioc_pool_export
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4580 { zfs_ioc_pool_configs
, zfs_secpolicy_none
, NO_NAME
, B_FALSE
,
4582 { zfs_ioc_pool_stats
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
,
4584 { zfs_ioc_pool_tryimport
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4586 { zfs_ioc_pool_scan
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4587 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4588 { zfs_ioc_pool_freeze
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4589 POOL_CHECK_READONLY
},
4590 { zfs_ioc_pool_upgrade
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4591 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4592 { zfs_ioc_pool_get_history
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4594 { zfs_ioc_vdev_add
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4595 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4596 { zfs_ioc_vdev_remove
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4597 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4598 { zfs_ioc_vdev_set_state
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4599 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4600 { zfs_ioc_vdev_attach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4601 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4602 { zfs_ioc_vdev_detach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4603 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4604 { zfs_ioc_vdev_setpath
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4605 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4606 { zfs_ioc_vdev_setfru
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4607 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4608 { zfs_ioc_objset_stats
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4609 POOL_CHECK_SUSPENDED
},
4610 { zfs_ioc_objset_zplprops
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4612 { zfs_ioc_dataset_list_next
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4613 POOL_CHECK_SUSPENDED
},
4614 { zfs_ioc_snapshot_list_next
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4615 POOL_CHECK_SUSPENDED
},
4616 { zfs_ioc_set_prop
, zfs_secpolicy_none
, DATASET_NAME
, B_TRUE
,
4617 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4618 { zfs_ioc_create_minor
, zfs_secpolicy_config
, DATASET_NAME
, B_FALSE
,
4620 { zfs_ioc_remove_minor
, zfs_secpolicy_config
, DATASET_NAME
, B_FALSE
,
4622 { zfs_ioc_create
, zfs_secpolicy_create
, DATASET_NAME
, B_TRUE
,
4623 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4624 { zfs_ioc_destroy
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
,
4625 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4626 { zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
, B_TRUE
,
4627 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4628 { zfs_ioc_rename
, zfs_secpolicy_rename
, DATASET_NAME
, B_TRUE
,
4629 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4630 { zfs_ioc_recv
, zfs_secpolicy_receive
, DATASET_NAME
, B_TRUE
,
4631 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4632 { zfs_ioc_send
, zfs_secpolicy_send
, DATASET_NAME
, B_TRUE
,
4634 { zfs_ioc_inject_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4636 { zfs_ioc_clear_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4638 { zfs_ioc_inject_list_next
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4640 { zfs_ioc_error_log
, zfs_secpolicy_inject
, POOL_NAME
, B_FALSE
,
4642 { zfs_ioc_clear
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4644 { zfs_ioc_promote
, zfs_secpolicy_promote
, DATASET_NAME
, B_TRUE
,
4645 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4646 { zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, DATASET_NAME
,
4647 B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4648 { zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, DATASET_NAME
, B_TRUE
,
4649 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4650 { zfs_ioc_dsobj_to_dsname
, zfs_secpolicy_diff
, POOL_NAME
, B_FALSE
,
4652 { zfs_ioc_obj_to_path
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4653 POOL_CHECK_SUSPENDED
},
4654 { zfs_ioc_pool_set_props
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4655 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4656 { zfs_ioc_pool_get_props
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
,
4658 { zfs_ioc_set_fsacl
, zfs_secpolicy_fsacl
, DATASET_NAME
, B_TRUE
,
4659 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4660 { zfs_ioc_get_fsacl
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4662 { zfs_ioc_share
, zfs_secpolicy_share
, DATASET_NAME
, B_FALSE
,
4664 { zfs_ioc_inherit_prop
, zfs_secpolicy_inherit
, DATASET_NAME
, B_TRUE
,
4665 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4666 { zfs_ioc_smb_acl
, zfs_secpolicy_smb_acl
, DATASET_NAME
, B_FALSE
,
4668 { zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
, DATASET_NAME
,
4669 B_FALSE
, POOL_CHECK_NONE
},
4670 { zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
, DATASET_NAME
,
4671 B_FALSE
, POOL_CHECK_NONE
},
4672 { zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
4673 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4674 { zfs_ioc_hold
, zfs_secpolicy_hold
, DATASET_NAME
, B_TRUE
,
4675 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4676 { zfs_ioc_release
, zfs_secpolicy_release
, DATASET_NAME
, B_TRUE
,
4677 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4678 { zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4679 POOL_CHECK_SUSPENDED
},
4680 { zfs_ioc_objset_recvd_props
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4682 { zfs_ioc_vdev_split
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4683 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4684 { zfs_ioc_next_obj
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4686 { zfs_ioc_diff
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4688 { zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
, DATASET_NAME
,
4689 B_FALSE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4690 { zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4691 POOL_CHECK_SUSPENDED
},
4692 { zfs_ioc_events_next
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4694 { zfs_ioc_events_clear
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4699 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
4700 zfs_ioc_poolcheck_t check
)
4705 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
4707 if (check
& POOL_CHECK_NONE
)
4710 error
= spa_open(name
, &spa
, FTAG
);
4712 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
4714 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
4716 spa_close(spa
, FTAG
);
4722 zfsdev_get_state_impl(minor_t minor
, enum zfsdev_state_type which
)
4726 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4728 for (zs
= list_head(&zfsdev_state_list
); zs
!= NULL
;
4729 zs
= list_next(&zfsdev_state_list
, zs
)) {
4730 if (zs
->zs_minor
== minor
) {
4732 case ZST_ONEXIT
: return (zs
->zs_onexit
);
4733 case ZST_ZEVENT
: return (zs
->zs_zevent
);
4734 case ZST_ALL
: return (zs
);
4743 zfsdev_get_state(minor_t minor
, enum zfsdev_state_type which
)
4747 mutex_enter(&zfsdev_state_lock
);
4748 ptr
= zfsdev_get_state_impl(minor
, which
);
4749 mutex_exit(&zfsdev_state_lock
);
4755 zfsdev_getminor(struct file
*filp
)
4757 ASSERT(filp
!= NULL
);
4758 ASSERT(filp
->private_data
!= NULL
);
4760 return (((zfsdev_state_t
*)filp
->private_data
)->zs_minor
);
4764 * Find a free minor number. The zfsdev_state_list is expected to
4765 * be short since it is only a list of currently open file handles.
4768 zfsdev_minor_alloc(void)
4770 static minor_t last_minor
= 0;
4773 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4775 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
4776 if (m
> ZFSDEV_MAX_MINOR
)
4778 if (zfsdev_get_state_impl(m
, ZST_ALL
) == NULL
) {
4788 zfsdev_state_init(struct file
*filp
)
4793 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4795 minor
= zfsdev_minor_alloc();
4799 zs
= kmem_zalloc( sizeof(zfsdev_state_t
), KM_SLEEP
);
4804 zs
->zs_minor
= minor
;
4805 filp
->private_data
= zs
;
4807 zfs_onexit_init((zfs_onexit_t
**)&zs
->zs_onexit
);
4808 zfs_zevent_init((zfs_zevent_t
**)&zs
->zs_zevent
);
4810 list_insert_tail(&zfsdev_state_list
, zs
);
4816 zfsdev_state_destroy(struct file
*filp
)
4820 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4821 ASSERT(filp
->private_data
!= NULL
);
4823 zs
= filp
->private_data
;
4824 zfs_onexit_destroy(zs
->zs_onexit
);
4825 zfs_zevent_destroy(zs
->zs_zevent
);
4827 list_remove(&zfsdev_state_list
, zs
);
4828 kmem_free(zs
, sizeof(zfsdev_state_t
));
4834 zfsdev_open(struct inode
*ino
, struct file
*filp
)
4838 mutex_enter(&zfsdev_state_lock
);
4839 error
= zfsdev_state_init(filp
);
4840 mutex_exit(&zfsdev_state_lock
);
4846 zfsdev_release(struct inode
*ino
, struct file
*filp
)
4850 mutex_enter(&zfsdev_state_lock
);
4851 error
= zfsdev_state_destroy(filp
);
4852 mutex_exit(&zfsdev_state_lock
);
4858 zfsdev_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
4862 int error
, rc
, flag
= 0;
4864 vec
= cmd
- ZFS_IOC
;
4865 if (vec
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
4868 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
| KM_NODEBUG
);
4870 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
4874 if ((error
== 0) && !(flag
& FKIOCTL
))
4875 error
= zfs_ioc_vec
[vec
].zvec_secpolicy(zc
, CRED());
4878 * Ensure that all pool/dataset names are valid before we pass down to
4882 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
4883 zc
->zc_iflags
= flag
& FKIOCTL
;
4884 switch (zfs_ioc_vec
[vec
].zvec_namecheck
) {
4886 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
4888 error
= pool_status_check(zc
->zc_name
,
4889 zfs_ioc_vec
[vec
].zvec_namecheck
,
4890 zfs_ioc_vec
[vec
].zvec_pool_check
);
4894 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
4896 error
= pool_status_check(zc
->zc_name
,
4897 zfs_ioc_vec
[vec
].zvec_namecheck
,
4898 zfs_ioc_vec
[vec
].zvec_pool_check
);
4907 error
= zfs_ioc_vec
[vec
].zvec_func(zc
);
4909 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
4913 if (zfs_ioc_vec
[vec
].zvec_his_log
)
4914 zfs_log_history(zc
);
4917 kmem_free(zc
, sizeof (zfs_cmd_t
));
4921 #ifdef CONFIG_COMPAT
4923 zfsdev_compat_ioctl(struct file
*filp
, unsigned cmd
, unsigned long arg
)
4925 return zfsdev_ioctl(filp
, cmd
, arg
);
4928 #define zfsdev_compat_ioctl NULL
4931 static const struct file_operations zfsdev_fops
= {
4932 .open
= zfsdev_open
,
4933 .release
= zfsdev_release
,
4934 .unlocked_ioctl
= zfsdev_ioctl
,
4935 .compat_ioctl
= zfsdev_compat_ioctl
,
4936 .owner
= THIS_MODULE
,
4939 static struct miscdevice zfs_misc
= {
4940 .minor
= MISC_DYNAMIC_MINOR
,
4942 .fops
= &zfsdev_fops
,
4950 mutex_init(&zfsdev_state_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
4951 list_create(&zfsdev_state_list
, sizeof (zfsdev_state_t
),
4952 offsetof(zfsdev_state_t
, zs_next
));
4954 error
= misc_register(&zfs_misc
);
4956 printk(KERN_INFO
"ZFS: misc_register() failed %d\n", error
);
4968 error
= misc_deregister(&zfs_misc
);
4970 printk(KERN_INFO
"ZFS: misc_deregister() failed %d\n", error
);
4972 mutex_destroy(&zfsdev_state_lock
);
4973 list_destroy(&zfsdev_state_list
);
4976 uint_t zfs_fsyncer_key
;
4977 extern uint_t rrw_tsd_key
;
4980 #define ZFS_DEBUG_STR " (DEBUG mode)"
4982 #define ZFS_DEBUG_STR ""
4990 spa_init(FREAD
| FWRITE
);
4993 if ((error
= zvol_init()) != 0)
4996 if ((error
= zfs_attach()) != 0)
4999 tsd_create(&zfs_fsyncer_key
, NULL
);
5000 tsd_create(&rrw_tsd_key
, NULL
);
5002 printk(KERN_NOTICE
"ZFS: Loaded module v%s-%s%s, "
5003 "ZFS pool version %s, ZFS filesystem version %s\n",
5004 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
,
5005 SPA_VERSION_STRING
, ZPL_VERSION_STRING
);
5014 printk(KERN_NOTICE
"ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5015 ", rc = %d\n", ZFS_META_VERSION
, ZFS_META_RELEASE
,
5016 ZFS_DEBUG_STR
, error
);
5029 tsd_destroy(&zfs_fsyncer_key
);
5030 tsd_destroy(&rrw_tsd_key
);
5032 printk(KERN_NOTICE
"ZFS: Unloaded module v%s-%s%s\n",
5033 ZFS_META_VERSION
, ZFS_META_RELEASE
, ZFS_DEBUG_STR
);
5039 spl_module_init(_init
);
5040 spl_module_exit(_fini
);
5042 MODULE_DESCRIPTION("ZFS");
5043 MODULE_AUTHOR(ZFS_META_AUTHOR
);
5044 MODULE_LICENSE(ZFS_META_LICENSE
);
5045 #endif /* HAVE_SPL */