4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
24 * All rights reserved.
25 * Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org>
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2011 by Delphix. All rights reserved.
28 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <sys/errno.h>
46 #include <sys/cmn_err.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_vfsops.h>
50 #include <sys/zfs_znode.h>
53 #include <sys/spa_impl.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_prop.h>
59 #include <sys/dsl_deleg.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dmu_impl.h>
62 #include <sys/sunddi.h>
63 #include <sys/policy.h>
65 #include <sys/nvpair.h>
66 #include <sys/mount.h>
67 #include <sys/taskqueue.h>
69 #include <sys/varargs.h>
70 #include <sys/fs/zfs.h>
71 #include <sys/zfs_ctldir.h>
72 #include <sys/zfs_dir.h>
73 #include <sys/zfs_onexit.h>
75 #include <sys/dsl_scan.h>
76 #include <sys/dmu_objset.h>
78 #include "zfs_namecheck.h"
80 #include "zfs_deleg.h"
81 #include "zfs_comutil.h"
82 #include "zfs_ioctl_compat.h"
84 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
86 static int snapshot_list_prefetch;
87 SYSCTL_DECL(_vfs_zfs);
88 TUNABLE_INT("vfs.zfs.snapshot_list_prefetch", &snapshot_list_prefetch);
89 SYSCTL_INT(_vfs_zfs, OID_AUTO, snapshot_list_prefetch, CTLFLAG_RW,
90 &snapshot_list_prefetch, 0, "Prefetch data when listing snapshots");
92 static struct cdev *zfsdev;
94 extern void zfs_init(void);
95 extern void zfs_fini(void);
97 typedef int zfs_ioc_func_t(zfs_cmd_t *);
98 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
104 } zfs_ioc_namecheck_t;
106 typedef struct zfs_ioc_vec {
107 zfs_ioc_func_t *zvec_func;
108 zfs_secpolicy_func_t *zvec_secpolicy;
109 zfs_ioc_namecheck_t zvec_namecheck;
110 boolean_t zvec_his_log;
111 boolean_t zvec_pool_check;
114 /* This array is indexed by zfs_userquota_prop_t */
115 static const char *userquota_perms[] = {
116 ZFS_DELEG_PERM_USERUSED,
117 ZFS_DELEG_PERM_USERQUOTA,
118 ZFS_DELEG_PERM_GROUPUSED,
119 ZFS_DELEG_PERM_GROUPQUOTA,
122 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
123 static int zfs_check_settable(const char *name, nvpair_t *property,
125 static int zfs_check_clearable(char *dataset, nvlist_t *props,
127 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
129 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
131 static void zfsdev_close(void *data);
133 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
135 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
142 * Get rid of annoying "../common/" prefix to filename.
144 newfile = strrchr(file, '/');
145 if (newfile != NULL) {
146 newfile = newfile + 1; /* Get rid of leading / */
152 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
156 * To get this data, use the zfs-dprintf probe as so:
157 * dtrace -q -n 'zfs-dprintf \
158 * /stringof(arg0) == "dbuf.c"/ \
159 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
161 * arg1 = function name
165 DTRACE_PROBE4(zfs__dprintf,
166 char *, newfile, char *, func, int, line, char *, buf);
170 history_str_free(char *buf)
172 kmem_free(buf, HIS_MAX_RECORD_LEN);
176 history_str_get(zfs_cmd_t *zc)
180 if (zc->zc_history == 0)
183 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
184 if (copyinstr((void *)(uintptr_t)zc->zc_history,
185 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
186 history_str_free(buf);
190 buf[HIS_MAX_RECORD_LEN -1] = '\0';
196 * Check to see if the named dataset is currently defined as bootable
199 zfs_is_bootfs(const char *name)
203 if (dmu_objset_hold(name, FTAG, &os) == 0) {
205 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
206 dmu_objset_rele(os, FTAG);
213 * zfs_earlier_version
215 * Return non-zero if the spa version is less than requested version.
218 zfs_earlier_version(const char *name, int version)
222 if (spa_open(name, &spa, FTAG) == 0) {
223 if (spa_version(spa) < version) {
224 spa_close(spa, FTAG);
227 spa_close(spa, FTAG);
233 * zpl_earlier_version
235 * Return TRUE if the ZPL version is less than requested version.
238 zpl_earlier_version(const char *name, int version)
241 boolean_t rc = B_TRUE;
243 if (dmu_objset_hold(name, FTAG, &os) == 0) {
246 if (dmu_objset_type(os) != DMU_OST_ZFS) {
247 dmu_objset_rele(os, FTAG);
250 /* XXX reading from non-owned objset */
251 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
252 rc = zplversion < version;
253 dmu_objset_rele(os, FTAG);
259 zfs_log_history(zfs_cmd_t *zc)
264 if ((buf = history_str_get(zc)) == NULL)
267 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
268 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
269 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
270 spa_close(spa, FTAG);
272 history_str_free(buf);
276 * Policy for top-level read operations (list pools). Requires no privileges,
277 * and can be used in the local zone, as there is no associated dataset.
281 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
287 * Policy for dataset read operations (list children, get statistics). Requires
288 * no privileges, but must be visible in the local zone.
292 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
294 if (INGLOBALZONE(curthread) ||
295 zone_dataset_visible(zc->zc_name, NULL))
302 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
307 * The dataset must be visible by this zone -- check this first
308 * so they don't see EPERM on something they shouldn't know about.
310 if (!INGLOBALZONE(curthread) &&
311 !zone_dataset_visible(dataset, &writable))
314 if (INGLOBALZONE(curthread)) {
316 * If the fs is zoned, only root can access it from the
319 if (secpolicy_zfs(cr) && zoned)
323 * If we are in a local zone, the 'zoned' property must be set.
328 /* must be writable by this zone */
336 zfs_dozonecheck(const char *dataset, cred_t *cr)
340 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
343 return (zfs_dozonecheck_impl(dataset, zoned, cr));
347 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
351 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
352 if (dsl_prop_get_ds(ds, "jailed", 8, 1, &zoned, NULL)) {
353 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
356 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
358 return (zfs_dozonecheck_impl(dataset, zoned, cr));
362 * If name ends in a '@', then require recursive permissions.
365 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
368 boolean_t descendent = B_FALSE;
372 at = strchr(name, '@');
373 if (at != NULL && at[1] == '\0') {
378 error = dsl_dataset_hold(name, FTAG, &ds);
384 error = zfs_dozonecheck_ds(name, ds, cr);
386 error = secpolicy_zfs(cr);
388 error = dsl_deleg_access_impl(ds, descendent, perm, cr);
391 dsl_dataset_rele(ds, FTAG);
396 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
397 const char *perm, cred_t *cr)
401 error = zfs_dozonecheck_ds(name, ds, cr);
403 error = secpolicy_zfs(cr);
405 error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
412 * Policy for setting the security label property.
414 * Returns 0 for success, non-zero for access and other errors.
417 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
419 char ds_hexsl[MAXNAMELEN];
420 bslabel_t ds_sl, new_sl;
421 boolean_t new_default = FALSE;
423 int needed_priv = -1;
426 /* First get the existing dataset label. */
427 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
428 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
432 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
435 /* The label must be translatable */
436 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
440 * In a non-global zone, disallow attempts to set a label that
441 * doesn't match that of the zone; otherwise no other checks
444 if (!INGLOBALZONE(curproc)) {
445 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
451 * For global-zone datasets (i.e., those whose zoned property is
452 * "off", verify that the specified new label is valid for the
455 if (dsl_prop_get_integer(name,
456 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
459 if (zfs_check_global_label(name, strval) != 0)
464 * If the existing dataset label is nondefault, check if the
465 * dataset is mounted (label cannot be changed while mounted).
466 * Get the zfsvfs; if there isn't one, then the dataset isn't
467 * mounted (or isn't a dataset, doesn't exist, ...).
469 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
471 static char *setsl_tag = "setsl_tag";
474 * Try to own the dataset; abort if there is any error,
475 * (e.g., already mounted, in use, or other error).
477 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
482 dmu_objset_disown(os, setsl_tag);
485 needed_priv = PRIV_FILE_DOWNGRADE_SL;
489 if (hexstr_to_label(strval, &new_sl) != 0)
492 if (blstrictdom(&ds_sl, &new_sl))
493 needed_priv = PRIV_FILE_DOWNGRADE_SL;
494 else if (blstrictdom(&new_sl, &ds_sl))
495 needed_priv = PRIV_FILE_UPGRADE_SL;
497 /* dataset currently has a default label */
499 needed_priv = PRIV_FILE_UPGRADE_SL;
503 if (needed_priv != -1)
504 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
507 #endif /* SECLABEL */
510 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
516 * Check permissions for special properties.
521 * Disallow setting of 'zoned' from within a local zone.
523 if (!INGLOBALZONE(curthread))
528 if (!INGLOBALZONE(curthread)) {
530 char setpoint[MAXNAMELEN];
532 * Unprivileged users are allowed to modify the
533 * quota on things *under* (ie. contained by)
534 * the thing they own.
536 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
539 if (!zoned || strlen(dsname) <= strlen(setpoint))
544 case ZFS_PROP_MLSLABEL:
546 if (!is_system_labeled())
549 if (nvpair_value_string(propval, &strval) == 0) {
552 err = zfs_set_slabel_policy(dsname, strval, CRED());
562 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
566 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
570 error = zfs_dozonecheck(zc->zc_name, cr);
575 * permission to set permissions will be evaluated later in
576 * dsl_deleg_can_allow()
582 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
584 return (zfs_secpolicy_write_perms(zc->zc_name,
585 ZFS_DELEG_PERM_ROLLBACK, cr));
589 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
598 * Generate the current snapshot name from the given objsetid, then
599 * use that name for the secpolicy/zone checks.
601 cp = strchr(zc->zc_name, '@');
604 error = spa_open(zc->zc_name, &spa, FTAG);
608 dp = spa_get_dsl(spa);
609 rw_enter(&dp->dp_config_rwlock, RW_READER);
610 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
611 rw_exit(&dp->dp_config_rwlock);
612 spa_close(spa, FTAG);
616 dsl_dataset_name(ds, zc->zc_name);
618 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
619 ZFS_DELEG_PERM_SEND, cr);
620 dsl_dataset_rele(ds, FTAG);
626 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
631 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
632 NO_FOLLOW, NULL, &vp)) != 0)
635 /* Now make sure mntpnt and dataset are ZFS */
637 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
638 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
639 zc->zc_name) != 0)) {
645 return (dsl_deleg_access(zc->zc_name,
646 ZFS_DELEG_PERM_SHARE, cr));
650 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
652 if (!INGLOBALZONE(curthread))
655 if (secpolicy_nfs(cr) == 0) {
658 return (zfs_secpolicy_deleg_share(zc, cr));
663 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
665 if (!INGLOBALZONE(curthread))
668 if (secpolicy_smb(cr) == 0) {
671 return (zfs_secpolicy_deleg_share(zc, cr));
676 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
681 * Remove the @bla or /bla from the end of the name to get the parent.
683 (void) strncpy(parent, datasetname, parentsize);
684 cp = strrchr(parent, '@');
688 cp = strrchr(parent, '/');
698 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
702 if ((error = zfs_secpolicy_write_perms(name,
703 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
706 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
710 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
712 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
716 * Destroying snapshots with delegated permissions requires
717 * descendent mount and destroy permissions.
720 zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr)
725 dsname = kmem_asprintf("%s@", zc->zc_name);
727 error = zfs_secpolicy_destroy_perms(dsname, cr);
730 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
737 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
739 char parentname[MAXNAMELEN];
742 if ((error = zfs_secpolicy_write_perms(from,
743 ZFS_DELEG_PERM_RENAME, cr)) != 0)
746 if ((error = zfs_secpolicy_write_perms(from,
747 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
750 if ((error = zfs_get_parent(to, parentname,
751 sizeof (parentname))) != 0)
754 if ((error = zfs_secpolicy_write_perms(parentname,
755 ZFS_DELEG_PERM_CREATE, cr)) != 0)
758 if ((error = zfs_secpolicy_write_perms(parentname,
759 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
766 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
768 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
772 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
774 char parentname[MAXNAMELEN];
778 error = zfs_secpolicy_write_perms(zc->zc_name,
779 ZFS_DELEG_PERM_PROMOTE, cr);
783 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
786 dsl_dataset_t *pclone = NULL;
788 dd = clone->os_dsl_dataset->ds_dir;
790 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
791 error = dsl_dataset_hold_obj(dd->dd_pool,
792 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
793 rw_exit(&dd->dd_pool->dp_config_rwlock);
795 dmu_objset_rele(clone, FTAG);
799 error = zfs_secpolicy_write_perms(zc->zc_name,
800 ZFS_DELEG_PERM_MOUNT, cr);
802 dsl_dataset_name(pclone, parentname);
803 dmu_objset_rele(clone, FTAG);
804 dsl_dataset_rele(pclone, FTAG);
806 error = zfs_secpolicy_write_perms(parentname,
807 ZFS_DELEG_PERM_PROMOTE, cr);
813 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
817 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
818 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
821 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
822 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
825 return (zfs_secpolicy_write_perms(zc->zc_name,
826 ZFS_DELEG_PERM_CREATE, cr));
830 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
832 return (zfs_secpolicy_write_perms(name,
833 ZFS_DELEG_PERM_SNAPSHOT, cr));
837 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
840 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
844 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
846 char parentname[MAXNAMELEN];
849 if ((error = zfs_get_parent(zc->zc_name, parentname,
850 sizeof (parentname))) != 0)
853 if (zc->zc_value[0] != '\0') {
854 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
855 ZFS_DELEG_PERM_CLONE, cr)) != 0)
859 if ((error = zfs_secpolicy_write_perms(parentname,
860 ZFS_DELEG_PERM_CREATE, cr)) != 0)
863 error = zfs_secpolicy_write_perms(parentname,
864 ZFS_DELEG_PERM_MOUNT, cr);
870 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
874 error = secpolicy_fs_unmount(cr, NULL);
876 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
882 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
883 * SYS_CONFIG privilege, which is not available in a local zone.
887 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
889 if (secpolicy_sys_config(cr, B_FALSE) != 0)
896 * Policy for object to name lookups.
900 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
904 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
907 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
912 * Policy for fault injection. Requires all privileges.
916 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
918 return (secpolicy_zinject(cr));
922 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
924 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
926 if (prop == ZPROP_INVAL) {
927 if (!zfs_prop_user(zc->zc_value))
929 return (zfs_secpolicy_write_perms(zc->zc_name,
930 ZFS_DELEG_PERM_USERPROP, cr));
932 return (zfs_secpolicy_setprop(zc->zc_name, prop,
938 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
940 int err = zfs_secpolicy_read(zc, cr);
944 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
947 if (zc->zc_value[0] == 0) {
949 * They are asking about a posix uid/gid. If it's
950 * themself, allow it.
952 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
953 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
954 if (zc->zc_guid == crgetuid(cr))
957 if (groupmember(zc->zc_guid, cr))
962 return (zfs_secpolicy_write_perms(zc->zc_name,
963 userquota_perms[zc->zc_objset_type], cr));
967 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
969 int err = zfs_secpolicy_read(zc, cr);
973 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
976 return (zfs_secpolicy_write_perms(zc->zc_name,
977 userquota_perms[zc->zc_objset_type], cr));
981 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
983 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
988 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
990 return (zfs_secpolicy_write_perms(zc->zc_name,
991 ZFS_DELEG_PERM_HOLD, cr));
995 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
997 return (zfs_secpolicy_write_perms(zc->zc_name,
998 ZFS_DELEG_PERM_RELEASE, cr));
1002 * Policy for allowing temporary snapshots to be taken or released
1005 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
1008 * A temporary snapshot is the same as a snapshot,
1009 * hold, destroy and release all rolled into one.
1010 * Delegated diff alone is sufficient that we allow this.
1014 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1015 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1018 error = zfs_secpolicy_snapshot(zc, cr);
1020 error = zfs_secpolicy_hold(zc, cr);
1022 error = zfs_secpolicy_release(zc, cr);
1024 error = zfs_secpolicy_destroy(zc, cr);
1029 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1032 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1036 nvlist_t *list = NULL;
1039 * Read in and unpack the user-supplied nvlist.
1044 packed = kmem_alloc(size, KM_SLEEP);
1046 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1048 kmem_free(packed, size);
1052 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1053 kmem_free(packed, size);
1057 kmem_free(packed, size);
1064 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1068 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1070 if (size > zc->zc_nvlist_dst_size) {
1071 nvpair_t *more_errors;
1074 if (zc->zc_nvlist_dst_size < 1024)
1077 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1078 more_errors = nvlist_prev_nvpair(*errors, NULL);
1081 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1083 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1085 VERIFY(nvlist_size(*errors, &size,
1086 NV_ENCODE_NATIVE) == 0);
1087 } while (size > zc->zc_nvlist_dst_size);
1089 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1090 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1091 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1092 ASSERT(size <= zc->zc_nvlist_dst_size);
1099 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1101 char *packed = NULL;
1105 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1107 if (size > zc->zc_nvlist_dst_size) {
1109 * Solaris returns ENOMEM here, because even if an error is
1110 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1111 * passed to the userland. This is not the case for FreeBSD.
1112 * We need to return 0, so the kernel will copy the
1113 * zc_nvlist_dst_size back and the userland can discover that a
1114 * bigger buffer is needed.
1118 packed = kmem_alloc(size, KM_SLEEP);
1119 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1121 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1122 size, zc->zc_iflags) != 0)
1124 kmem_free(packed, size);
1127 zc->zc_nvlist_dst_size = size;
1132 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1137 error = dmu_objset_hold(dsname, FTAG, &os);
1140 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1141 dmu_objset_rele(os, FTAG);
1145 mutex_enter(&os->os_user_ptr_lock);
1146 *zfvp = dmu_objset_get_user(os);
1148 VFS_HOLD((*zfvp)->z_vfs);
1152 mutex_exit(&os->os_user_ptr_lock);
1153 dmu_objset_rele(os, FTAG);
1158 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1159 * case its z_vfs will be NULL, and it will be opened as the owner.
1162 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1166 if (getzfsvfs(name, zfvp) != 0)
1167 error = zfsvfs_create(name, zfvp);
1169 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1171 if ((*zfvp)->z_unmounted) {
1173 * XXX we could probably try again, since the unmounting
1174 * thread should be just about to disassociate the
1175 * objset from the zfsvfs.
1177 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1185 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1187 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1189 if (zfsvfs->z_vfs) {
1190 VFS_RELE(zfsvfs->z_vfs);
1192 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1193 zfsvfs_free(zfsvfs);
1198 zfs_ioc_pool_create(zfs_cmd_t *zc)
1201 nvlist_t *config, *props = NULL;
1202 nvlist_t *rootprops = NULL;
1203 nvlist_t *zplprops = NULL;
1206 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1207 zc->zc_iflags, &config))
1210 if (zc->zc_nvlist_src_size != 0 && (error =
1211 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1212 zc->zc_iflags, &props))) {
1213 nvlist_free(config);
1218 nvlist_t *nvl = NULL;
1219 uint64_t version = SPA_VERSION;
1221 (void) nvlist_lookup_uint64(props,
1222 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1223 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1225 goto pool_props_bad;
1227 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1229 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1231 nvlist_free(config);
1235 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1237 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1238 error = zfs_fill_zplprops_root(version, rootprops,
1241 goto pool_props_bad;
1244 buf = history_str_get(zc);
1246 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1249 * Set the remaining root properties
1251 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1252 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1253 (void) spa_destroy(zc->zc_name);
1256 history_str_free(buf);
1259 nvlist_free(rootprops);
1260 nvlist_free(zplprops);
1261 nvlist_free(config);
1268 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1271 zfs_log_history(zc);
1272 error = spa_destroy(zc->zc_name);
1274 zvol_remove_minors(zc->zc_name);
1279 zfs_ioc_pool_import(zfs_cmd_t *zc)
1281 nvlist_t *config, *props = NULL;
1285 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1286 zc->zc_iflags, &config)) != 0)
1289 if (zc->zc_nvlist_src_size != 0 && (error =
1290 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1291 zc->zc_iflags, &props))) {
1292 nvlist_free(config);
1296 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1297 guid != zc->zc_guid)
1300 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1302 if (zc->zc_nvlist_dst != 0) {
1305 if ((err = put_nvlist(zc, config)) != 0)
1309 nvlist_free(config);
1318 zfs_ioc_pool_export(zfs_cmd_t *zc)
1321 boolean_t force = (boolean_t)zc->zc_cookie;
1322 boolean_t hardforce = (boolean_t)zc->zc_guid;
1324 zfs_log_history(zc);
1325 error = spa_export(zc->zc_name, NULL, force, hardforce);
1327 zvol_remove_minors(zc->zc_name);
1332 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1337 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1340 error = put_nvlist(zc, configs);
1342 nvlist_free(configs);
1348 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1354 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1355 sizeof (zc->zc_value));
1357 if (config != NULL) {
1358 ret = put_nvlist(zc, config);
1359 nvlist_free(config);
1362 * The config may be present even if 'error' is non-zero.
1363 * In this case we return success, and preserve the real errno
1366 zc->zc_cookie = error;
1375 * Try to import the given pool, returning pool stats as appropriate so that
1376 * user land knows which devices are available and overall pool health.
1379 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1381 nvlist_t *tryconfig, *config;
1384 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1385 zc->zc_iflags, &tryconfig)) != 0)
1388 config = spa_tryimport(tryconfig);
1390 nvlist_free(tryconfig);
1395 error = put_nvlist(zc, config);
1396 nvlist_free(config);
1403 * zc_name name of the pool
1404 * zc_cookie scan func (pool_scan_func_t)
1407 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1412 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1415 if (zc->zc_cookie == POOL_SCAN_NONE)
1416 error = spa_scan_stop(spa);
1418 error = spa_scan(spa, zc->zc_cookie);
1420 spa_close(spa, FTAG);
1426 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1431 error = spa_open(zc->zc_name, &spa, FTAG);
1434 spa_close(spa, FTAG);
1440 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1445 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1448 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1449 spa_close(spa, FTAG);
1453 spa_upgrade(spa, zc->zc_cookie);
1454 spa_close(spa, FTAG);
1460 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1467 if ((size = zc->zc_history_len) == 0)
1470 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1473 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1474 spa_close(spa, FTAG);
1478 hist_buf = kmem_alloc(size, KM_SLEEP);
1479 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1480 &zc->zc_history_len, hist_buf)) == 0) {
1481 error = ddi_copyout(hist_buf,
1482 (void *)(uintptr_t)zc->zc_history,
1483 zc->zc_history_len, zc->zc_iflags);
1486 spa_close(spa, FTAG);
1487 kmem_free(hist_buf, size);
1492 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1497 error = spa_open(zc->zc_name, &spa, FTAG);
1499 error = spa_change_guid(spa);
1500 spa_close(spa, FTAG);
1506 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1510 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1518 * zc_name name of filesystem
1519 * zc_obj object to find
1522 * zc_value name of object
1525 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1530 /* XXX reading from objset not owned */
1531 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1533 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1534 dmu_objset_rele(os, FTAG);
1537 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1538 sizeof (zc->zc_value));
1539 dmu_objset_rele(os, FTAG);
1546 * zc_name name of filesystem
1547 * zc_obj object to find
1550 * zc_stat stats on object
1551 * zc_value path to object
1554 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1559 /* XXX reading from objset not owned */
1560 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1562 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1563 dmu_objset_rele(os, FTAG);
1566 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1567 sizeof (zc->zc_value));
1568 dmu_objset_rele(os, FTAG);
1574 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1578 nvlist_t *config, **l2cache, **spares;
1579 uint_t nl2cache = 0, nspares = 0;
1581 error = spa_open(zc->zc_name, &spa, FTAG);
1585 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1586 zc->zc_iflags, &config);
1587 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1588 &l2cache, &nl2cache);
1590 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1594 * A root pool with concatenated devices is not supported.
1595 * Thus, can not add a device to a root pool.
1597 * Intent log device can not be added to a rootpool because
1598 * during mountroot, zil is replayed, a seperated log device
1599 * can not be accessed during the mountroot time.
1601 * l2cache and spare devices are ok to be added to a rootpool.
1603 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1604 nvlist_free(config);
1605 spa_close(spa, FTAG);
1610 error = spa_vdev_add(spa, config);
1611 nvlist_free(config);
1613 spa_close(spa, FTAG);
1619 * zc_name name of the pool
1620 * zc_nvlist_conf nvlist of devices to remove
1621 * zc_cookie to stop the remove?
1624 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1629 error = spa_open(zc->zc_name, &spa, FTAG);
1632 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1633 spa_close(spa, FTAG);
1638 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1642 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1644 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1646 switch (zc->zc_cookie) {
1647 case VDEV_STATE_ONLINE:
1648 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1651 case VDEV_STATE_OFFLINE:
1652 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1655 case VDEV_STATE_FAULTED:
1656 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1657 zc->zc_obj != VDEV_AUX_EXTERNAL)
1658 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1660 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1663 case VDEV_STATE_DEGRADED:
1664 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1665 zc->zc_obj != VDEV_AUX_EXTERNAL)
1666 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1668 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1674 zc->zc_cookie = newstate;
1675 spa_close(spa, FTAG);
1680 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1683 int replacing = zc->zc_cookie;
1687 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1690 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1691 zc->zc_iflags, &config)) == 0) {
1692 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1693 nvlist_free(config);
1696 spa_close(spa, FTAG);
1701 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1706 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1709 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1711 spa_close(spa, FTAG);
1716 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1719 nvlist_t *config, *props = NULL;
1721 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1723 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1726 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1727 zc->zc_iflags, &config)) {
1728 spa_close(spa, FTAG);
1732 if (zc->zc_nvlist_src_size != 0 && (error =
1733 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1734 zc->zc_iflags, &props))) {
1735 spa_close(spa, FTAG);
1736 nvlist_free(config);
1740 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1742 spa_close(spa, FTAG);
1744 nvlist_free(config);
1751 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1754 char *path = zc->zc_value;
1755 uint64_t guid = zc->zc_guid;
1758 error = spa_open(zc->zc_name, &spa, FTAG);
1762 error = spa_vdev_setpath(spa, guid, path);
1763 spa_close(spa, FTAG);
1768 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1771 char *fru = zc->zc_value;
1772 uint64_t guid = zc->zc_guid;
1775 error = spa_open(zc->zc_name, &spa, FTAG);
1779 error = spa_vdev_setfru(spa, guid, fru);
1780 spa_close(spa, FTAG);
1785 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1790 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1792 if (zc->zc_nvlist_dst != 0 &&
1793 (error = dsl_prop_get_all(os, &nv)) == 0) {
1794 dmu_objset_stats(os, nv);
1796 * NB: zvol_get_stats() will read the objset contents,
1797 * which we aren't supposed to do with a
1798 * DS_MODE_USER hold, because it could be
1799 * inconsistent. So this is a bit of a workaround...
1800 * XXX reading with out owning
1802 if (!zc->zc_objset_stats.dds_inconsistent &&
1803 dmu_objset_type(os) == DMU_OST_ZVOL) {
1804 error = zvol_get_stats(os, nv);
1807 VERIFY3S(error, ==, 0);
1809 error = put_nvlist(zc, nv);
1818 * zc_name name of filesystem
1819 * zc_nvlist_dst_size size of buffer for property nvlist
1822 * zc_objset_stats stats
1823 * zc_nvlist_dst property nvlist
1824 * zc_nvlist_dst_size size of property nvlist
1827 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1829 objset_t *os = NULL;
1832 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1835 error = zfs_ioc_objset_stats_impl(zc, os);
1837 dmu_objset_rele(os, FTAG);
1839 if (error == ENOMEM)
1846 * zc_name name of filesystem
1847 * zc_nvlist_dst_size size of buffer for property nvlist
1850 * zc_nvlist_dst received property nvlist
1851 * zc_nvlist_dst_size size of received property nvlist
1853 * Gets received properties (distinct from local properties on or after
1854 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1855 * local property values.
1858 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1860 objset_t *os = NULL;
1864 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1868 * Without this check, we would return local property values if the
1869 * caller has not already received properties on or after
1870 * SPA_VERSION_RECVD_PROPS.
1872 if (!dsl_prop_get_hasrecvd(os)) {
1873 dmu_objset_rele(os, FTAG);
1877 if (zc->zc_nvlist_dst != 0 &&
1878 (error = dsl_prop_get_received(os, &nv)) == 0) {
1879 error = put_nvlist(zc, nv);
1883 dmu_objset_rele(os, FTAG);
1888 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1894 * zfs_get_zplprop() will either find a value or give us
1895 * the default value (if there is one).
1897 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1899 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1905 * zc_name name of filesystem
1906 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1909 * zc_nvlist_dst zpl property nvlist
1910 * zc_nvlist_dst_size size of zpl property nvlist
1913 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1918 /* XXX reading without owning */
1919 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1922 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1925 * NB: nvl_add_zplprop() will read the objset contents,
1926 * which we aren't supposed to do with a DS_MODE_USER
1927 * hold, because it could be inconsistent.
1929 if (zc->zc_nvlist_dst != 0 &&
1930 !zc->zc_objset_stats.dds_inconsistent &&
1931 dmu_objset_type(os) == DMU_OST_ZFS) {
1934 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1935 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1936 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1937 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1938 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1939 err = put_nvlist(zc, nv);
1944 dmu_objset_rele(os, FTAG);
1949 dataset_name_hidden(const char *name)
1952 * Skip over datasets that are not visible in this zone,
1953 * internal datasets (which have a $ in their name), and
1954 * temporary datasets (which have a % in their name).
1956 if (strchr(name, '$') != NULL)
1958 if (strchr(name, '%') != NULL)
1960 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
1967 * zc_name name of filesystem
1968 * zc_cookie zap cursor
1969 * zc_nvlist_dst_size size of buffer for property nvlist
1972 * zc_name name of next filesystem
1973 * zc_cookie zap cursor
1974 * zc_objset_stats stats
1975 * zc_nvlist_dst property nvlist
1976 * zc_nvlist_dst_size size of property nvlist
1979 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1984 size_t orig_len = strlen(zc->zc_name);
1987 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1988 if (error == ENOENT)
1993 p = strrchr(zc->zc_name, '/');
1994 if (p == NULL || p[1] != '\0')
1995 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1996 p = zc->zc_name + strlen(zc->zc_name);
1999 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
2000 * but is not declared void because its called by dmu_objset_find().
2002 if (zc->zc_cookie == 0) {
2003 uint64_t cookie = 0;
2004 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
2006 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2007 if (!dataset_name_hidden(zc->zc_name))
2008 (void) dmu_objset_prefetch(zc->zc_name, NULL);
2013 error = dmu_dir_list_next(os,
2014 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2015 NULL, &zc->zc_cookie);
2016 if (error == ENOENT)
2018 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2019 dmu_objset_rele(os, FTAG);
2022 * If it's an internal dataset (ie. with a '$' in its name),
2023 * don't try to get stats for it, otherwise we'll return ENOENT.
2025 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2026 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2027 if (error == ENOENT) {
2028 /* We lost a race with destroy, get the next one. */
2029 zc->zc_name[orig_len] = '\0';
2038 * zc_name name of filesystem
2039 * zc_cookie zap cursor
2040 * zc_nvlist_dst_size size of buffer for property nvlist
2041 * zc_simple when set, only name is requested
2044 * zc_name name of next snapshot
2045 * zc_objset_stats stats
2046 * zc_nvlist_dst property nvlist
2047 * zc_nvlist_dst_size size of property nvlist
2050 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2056 if (snapshot_list_prefetch && zc->zc_cookie == 0 && !zc->zc_simple)
2057 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2058 NULL, DS_FIND_SNAPSHOTS);
2060 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2062 return (error == ENOENT ? ESRCH : error);
2065 * A dataset name of maximum length cannot have any snapshots,
2066 * so exit immediately.
2068 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2069 dmu_objset_rele(os, FTAG);
2073 error = dmu_snapshot_list_next(os,
2074 sizeof (zc->zc_name) - strlen(zc->zc_name),
2075 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2078 if (error == 0 && !zc->zc_simple) {
2080 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2083 * Since we probably don't have a hold on this snapshot,
2084 * it's possible that the objsetid could have been destroyed
2085 * and reused for a new objset. It's OK if this happens during
2086 * a zfs send operation, since the new createtxg will be
2087 * beyond the range we're interested in.
2089 rw_enter(&dp->dp_config_rwlock, RW_READER);
2090 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2091 rw_exit(&dp->dp_config_rwlock);
2093 if (error == ENOENT) {
2094 /* Racing with destroy, get the next one. */
2095 *strchr(zc->zc_name, '@') = '\0';
2096 dmu_objset_rele(os, FTAG);
2102 error = dmu_objset_from_ds(ds, &ossnap);
2104 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2105 dsl_dataset_rele(ds, FTAG);
2107 } else if (error == ENOENT) {
2111 dmu_objset_rele(os, FTAG);
2112 /* if we failed, undo the @ that we tacked on to zc_name */
2114 *strchr(zc->zc_name, '@') = '\0';
2119 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2121 const char *propname = nvpair_name(pair);
2123 unsigned int vallen;
2126 zfs_userquota_prop_t type;
2132 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2134 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2135 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2141 * A correctly constructed propname is encoded as
2142 * userquota@<rid>-<domain>.
2144 if ((dash = strchr(propname, '-')) == NULL ||
2145 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2154 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2156 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2157 zfsvfs_rele(zfsvfs, FTAG);
2164 * If the named property is one that has a special function to set its value,
2165 * return 0 on success and a positive error code on failure; otherwise if it is
2166 * not one of the special properties handled by this function, return -1.
2168 * XXX: It would be better for callers of the property interface if we handled
2169 * these special cases in dsl_prop.c (in the dsl layer).
2172 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2175 const char *propname = nvpair_name(pair);
2176 zfs_prop_t prop = zfs_name_to_prop(propname);
2180 if (prop == ZPROP_INVAL) {
2181 if (zfs_prop_userquota(propname))
2182 return (zfs_prop_set_userquota(dsname, pair));
2186 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2188 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2189 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2193 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2196 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2199 case ZFS_PROP_QUOTA:
2200 err = dsl_dir_set_quota(dsname, source, intval);
2202 case ZFS_PROP_REFQUOTA:
2203 err = dsl_dataset_set_quota(dsname, source, intval);
2205 case ZFS_PROP_RESERVATION:
2206 err = dsl_dir_set_reservation(dsname, source, intval);
2208 case ZFS_PROP_REFRESERVATION:
2209 err = dsl_dataset_set_reservation(dsname, source, intval);
2211 case ZFS_PROP_VOLSIZE:
2212 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2215 case ZFS_PROP_VERSION:
2219 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2222 err = zfs_set_version(zfsvfs, intval);
2223 zfsvfs_rele(zfsvfs, FTAG);
2225 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2228 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2229 (void) strcpy(zc->zc_name, dsname);
2230 (void) zfs_ioc_userspace_upgrade(zc);
2231 kmem_free(zc, sizeof (zfs_cmd_t));
2244 * This function is best effort. If it fails to set any of the given properties,
2245 * it continues to set as many as it can and returns the first error
2246 * encountered. If the caller provides a non-NULL errlist, it also gives the
2247 * complete list of names of all the properties it failed to set along with the
2248 * corresponding error numbers. The caller is responsible for freeing the
2251 * If every property is set successfully, zero is returned and the list pointed
2252 * at by errlist is NULL.
2255 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2263 nvlist_t *genericnvl;
2267 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2268 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2269 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2273 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2274 const char *propname = nvpair_name(pair);
2275 zfs_prop_t prop = zfs_name_to_prop(propname);
2278 /* decode the property value */
2280 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2282 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2283 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2288 /* Validate value type */
2289 if (err == 0 && prop == ZPROP_INVAL) {
2290 if (zfs_prop_user(propname)) {
2291 if (nvpair_type(propval) != DATA_TYPE_STRING)
2293 } else if (zfs_prop_userquota(propname)) {
2294 if (nvpair_type(propval) !=
2295 DATA_TYPE_UINT64_ARRAY)
2300 } else if (err == 0) {
2301 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2302 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2304 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2307 VERIFY(nvpair_value_uint64(propval,
2310 switch (zfs_prop_get_type(prop)) {
2311 case PROP_TYPE_NUMBER:
2313 case PROP_TYPE_STRING:
2316 case PROP_TYPE_INDEX:
2317 if (zfs_prop_index_to_string(prop,
2318 intval, &unused) != 0)
2323 "unknown property type");
2330 /* Validate permissions */
2332 err = zfs_check_settable(dsname, pair, CRED());
2335 err = zfs_prop_set_special(dsname, source, pair);
2338 * For better performance we build up a list of
2339 * properties to set in a single transaction.
2341 err = nvlist_add_nvpair(genericnvl, pair);
2342 } else if (err != 0 && nvl != retrynvl) {
2344 * This may be a spurious error caused by
2345 * receiving quota and reservation out of order.
2346 * Try again in a second pass.
2348 err = nvlist_add_nvpair(retrynvl, pair);
2353 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2356 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2361 if (!nvlist_empty(genericnvl) &&
2362 dsl_props_set(dsname, source, genericnvl) != 0) {
2364 * If this fails, we still want to set as many properties as we
2365 * can, so try setting them individually.
2368 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2369 const char *propname = nvpair_name(pair);
2373 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2375 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2376 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2380 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2381 VERIFY(nvpair_value_string(propval,
2383 err = dsl_prop_set(dsname, propname, source, 1,
2384 strlen(strval) + 1, strval);
2386 VERIFY(nvpair_value_uint64(propval,
2388 err = dsl_prop_set(dsname, propname, source, 8,
2393 VERIFY(nvlist_add_int32(errors, propname,
2398 nvlist_free(genericnvl);
2399 nvlist_free(retrynvl);
2401 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2402 nvlist_free(errors);
2405 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2408 if (errlist == NULL)
2409 nvlist_free(errors);
2417 * Check that all the properties are valid user properties.
2420 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2422 nvpair_t *pair = NULL;
2425 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2426 const char *propname = nvpair_name(pair);
2429 if (!zfs_prop_user(propname) ||
2430 nvpair_type(pair) != DATA_TYPE_STRING)
2433 if (error = zfs_secpolicy_write_perms(fsname,
2434 ZFS_DELEG_PERM_USERPROP, CRED()))
2437 if (strlen(propname) >= ZAP_MAXNAMELEN)
2438 return (ENAMETOOLONG);
2440 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2441 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2448 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2452 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2455 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2456 if (nvlist_exists(skipped, nvpair_name(pair)))
2459 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2464 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2468 nvlist_t *cleared_props = NULL;
2469 props_skip(props, skipped, &cleared_props);
2470 if (!nvlist_empty(cleared_props)) {
2472 * Acts on local properties until the dataset has received
2473 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2475 zprop_source_t flags = (ZPROP_SRC_NONE |
2476 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2477 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2479 nvlist_free(cleared_props);
2485 * zc_name name of filesystem
2486 * zc_value name of property to set
2487 * zc_nvlist_src{_size} nvlist of properties to apply
2488 * zc_cookie received properties flag
2491 * zc_nvlist_dst{_size} error for each unapplied received property
2494 zfs_ioc_set_prop(zfs_cmd_t *zc)
2497 boolean_t received = zc->zc_cookie;
2498 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2500 nvlist_t *errors = NULL;
2503 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2504 zc->zc_iflags, &nvl)) != 0)
2508 nvlist_t *origprops;
2511 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2512 if (dsl_prop_get_received(os, &origprops) == 0) {
2513 (void) clear_received_props(os,
2514 zc->zc_name, origprops, nvl);
2515 nvlist_free(origprops);
2518 dsl_prop_set_hasrecvd(os);
2519 dmu_objset_rele(os, FTAG);
2523 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2525 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2526 (void) put_nvlist(zc, errors);
2529 nvlist_free(errors);
2536 * zc_name name of filesystem
2537 * zc_value name of property to inherit
2538 * zc_cookie revert to received value if TRUE
2543 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2545 const char *propname = zc->zc_value;
2546 zfs_prop_t prop = zfs_name_to_prop(propname);
2547 boolean_t received = zc->zc_cookie;
2548 zprop_source_t source = (received
2549 ? ZPROP_SRC_NONE /* revert to received value, if any */
2550 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2559 * zfs_prop_set_special() expects properties in the form of an
2560 * nvpair with type info.
2562 if (prop == ZPROP_INVAL) {
2563 if (!zfs_prop_user(propname))
2566 type = PROP_TYPE_STRING;
2567 } else if (prop == ZFS_PROP_VOLSIZE ||
2568 prop == ZFS_PROP_VERSION) {
2571 type = zfs_prop_get_type(prop);
2574 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2577 case PROP_TYPE_STRING:
2578 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2580 case PROP_TYPE_NUMBER:
2581 case PROP_TYPE_INDEX:
2582 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2589 pair = nvlist_next_nvpair(dummy, NULL);
2590 err = zfs_prop_set_special(zc->zc_name, source, pair);
2593 return (err); /* special property already handled */
2596 * Only check this in the non-received case. We want to allow
2597 * 'inherit -S' to revert non-inheritable properties like quota
2598 * and reservation to the received or default values even though
2599 * they are not considered inheritable.
2601 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2605 /* the property name has been validated by zfs_secpolicy_inherit() */
2606 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2610 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2617 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2618 zc->zc_iflags, &props))
2622 * If the only property is the configfile, then just do a spa_lookup()
2623 * to handle the faulted case.
2625 pair = nvlist_next_nvpair(props, NULL);
2626 if (pair != NULL && strcmp(nvpair_name(pair),
2627 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2628 nvlist_next_nvpair(props, pair) == NULL) {
2629 mutex_enter(&spa_namespace_lock);
2630 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2631 spa_configfile_set(spa, props, B_FALSE);
2632 spa_config_sync(spa, B_FALSE, B_TRUE);
2634 mutex_exit(&spa_namespace_lock);
2641 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2646 error = spa_prop_set(spa, props);
2649 spa_close(spa, FTAG);
2655 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2659 nvlist_t *nvp = NULL;
2661 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2663 * If the pool is faulted, there may be properties we can still
2664 * get (such as altroot and cachefile), so attempt to get them
2667 mutex_enter(&spa_namespace_lock);
2668 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2669 error = spa_prop_get(spa, &nvp);
2670 mutex_exit(&spa_namespace_lock);
2672 error = spa_prop_get(spa, &nvp);
2673 spa_close(spa, FTAG);
2676 if (error == 0 && zc->zc_nvlist_dst != 0)
2677 error = put_nvlist(zc, nvp);
2687 * zc_name name of filesystem
2688 * zc_nvlist_src{_size} nvlist of delegated permissions
2689 * zc_perm_action allow/unallow flag
2694 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2697 nvlist_t *fsaclnv = NULL;
2699 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2700 zc->zc_iflags, &fsaclnv)) != 0)
2704 * Verify nvlist is constructed correctly
2706 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2707 nvlist_free(fsaclnv);
2712 * If we don't have PRIV_SYS_MOUNT, then validate
2713 * that user is allowed to hand out each permission in
2717 error = secpolicy_zfs(CRED());
2719 if (zc->zc_perm_action == B_FALSE) {
2720 error = dsl_deleg_can_allow(zc->zc_name,
2723 error = dsl_deleg_can_unallow(zc->zc_name,
2729 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2731 nvlist_free(fsaclnv);
2737 * zc_name name of filesystem
2740 * zc_nvlist_src{_size} nvlist of delegated permissions
2743 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2748 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2749 error = put_nvlist(zc, nvp);
2757 * Search the vfs list for a specified resource. Returns a pointer to it
2758 * or NULL if no suitable entry is found. The caller of this routine
2759 * is responsible for releasing the returned vfs pointer.
2762 zfs_get_vfs(const char *resource)
2766 mtx_lock(&mountlist_mtx);
2767 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
2768 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2773 mtx_unlock(&mountlist_mtx);
2779 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2781 zfs_creat_t *zct = arg;
2783 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2786 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2790 * createprops list of properties requested by creator
2791 * default_zplver zpl version to use if unspecified in createprops
2792 * fuids_ok fuids allowed in this version of the spa?
2793 * os parent objset pointer (NULL if root fs)
2796 * zplprops values for the zplprops we attach to the master node object
2797 * is_ci true if requested file system will be purely case-insensitive
2799 * Determine the settings for utf8only, normalization and
2800 * casesensitivity. Specific values may have been requested by the
2801 * creator and/or we can inherit values from the parent dataset. If
2802 * the file system is of too early a vintage, a creator can not
2803 * request settings for these properties, even if the requested
2804 * setting is the default value. We don't actually want to create dsl
2805 * properties for these, so remove them from the source nvlist after
2809 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2810 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2811 nvlist_t *zplprops, boolean_t *is_ci)
2813 uint64_t sense = ZFS_PROP_UNDEFINED;
2814 uint64_t norm = ZFS_PROP_UNDEFINED;
2815 uint64_t u8 = ZFS_PROP_UNDEFINED;
2817 ASSERT(zplprops != NULL);
2820 * Pull out creator prop choices, if any.
2823 (void) nvlist_lookup_uint64(createprops,
2824 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2825 (void) nvlist_lookup_uint64(createprops,
2826 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2827 (void) nvlist_remove_all(createprops,
2828 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2829 (void) nvlist_lookup_uint64(createprops,
2830 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2831 (void) nvlist_remove_all(createprops,
2832 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2833 (void) nvlist_lookup_uint64(createprops,
2834 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2835 (void) nvlist_remove_all(createprops,
2836 zfs_prop_to_name(ZFS_PROP_CASE));
2840 * If the zpl version requested is whacky or the file system
2841 * or pool is version is too "young" to support normalization
2842 * and the creator tried to set a value for one of the props,
2845 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2846 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2847 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2848 (zplver < ZPL_VERSION_NORMALIZATION &&
2849 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2850 sense != ZFS_PROP_UNDEFINED)))
2854 * Put the version in the zplprops
2856 VERIFY(nvlist_add_uint64(zplprops,
2857 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2859 if (norm == ZFS_PROP_UNDEFINED)
2860 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2861 VERIFY(nvlist_add_uint64(zplprops,
2862 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2865 * If we're normalizing, names must always be valid UTF-8 strings.
2869 if (u8 == ZFS_PROP_UNDEFINED)
2870 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2871 VERIFY(nvlist_add_uint64(zplprops,
2872 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2874 if (sense == ZFS_PROP_UNDEFINED)
2875 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2876 VERIFY(nvlist_add_uint64(zplprops,
2877 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2880 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2886 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2887 nvlist_t *zplprops, boolean_t *is_ci)
2889 boolean_t fuids_ok, sa_ok;
2890 uint64_t zplver = ZPL_VERSION;
2891 objset_t *os = NULL;
2892 char parentname[MAXNAMELEN];
2898 (void) strlcpy(parentname, dataset, sizeof (parentname));
2899 cp = strrchr(parentname, '/');
2903 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2906 spa_vers = spa_version(spa);
2907 spa_close(spa, FTAG);
2909 zplver = zfs_zpl_version_map(spa_vers);
2910 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2911 sa_ok = (zplver >= ZPL_VERSION_SA);
2914 * Open parent object set so we can inherit zplprop values.
2916 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2919 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2921 dmu_objset_rele(os, FTAG);
2926 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2927 nvlist_t *zplprops, boolean_t *is_ci)
2931 uint64_t zplver = ZPL_VERSION;
2934 zplver = zfs_zpl_version_map(spa_vers);
2935 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2936 sa_ok = (zplver >= ZPL_VERSION_SA);
2938 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2939 createprops, zplprops, is_ci);
2945 * zc_objset_type type of objset to create (fs vs zvol)
2946 * zc_name name of new objset
2947 * zc_value name of snapshot to clone from (may be empty)
2948 * zc_nvlist_src{_size} nvlist of properties to apply
2953 zfs_ioc_create(zfs_cmd_t *zc)
2958 nvlist_t *nvprops = NULL;
2959 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2960 dmu_objset_type_t type = zc->zc_objset_type;
2965 cbfunc = zfs_create_cb;
2969 cbfunc = zvol_create_cb;
2976 if (strchr(zc->zc_name, '@') ||
2977 strchr(zc->zc_name, '%'))
2980 if (zc->zc_nvlist_src != 0 &&
2981 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2982 zc->zc_iflags, &nvprops)) != 0)
2985 zct.zct_zplprops = NULL;
2986 zct.zct_props = nvprops;
2988 if (zc->zc_value[0] != '\0') {
2990 * We're creating a clone of an existing snapshot.
2992 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2993 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2994 nvlist_free(nvprops);
2998 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
3000 nvlist_free(nvprops);
3004 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
3005 dmu_objset_rele(clone, FTAG);
3007 nvlist_free(nvprops);
3011 boolean_t is_insensitive = B_FALSE;
3013 if (cbfunc == NULL) {
3014 nvlist_free(nvprops);
3018 if (type == DMU_OST_ZVOL) {
3019 uint64_t volsize, volblocksize;
3021 if (nvprops == NULL ||
3022 nvlist_lookup_uint64(nvprops,
3023 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3025 nvlist_free(nvprops);
3029 if ((error = nvlist_lookup_uint64(nvprops,
3030 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3031 &volblocksize)) != 0 && error != ENOENT) {
3032 nvlist_free(nvprops);
3037 volblocksize = zfs_prop_default_numeric(
3038 ZFS_PROP_VOLBLOCKSIZE);
3040 if ((error = zvol_check_volblocksize(
3041 volblocksize)) != 0 ||
3042 (error = zvol_check_volsize(volsize,
3043 volblocksize)) != 0) {
3044 nvlist_free(nvprops);
3047 } else if (type == DMU_OST_ZFS) {
3051 * We have to have normalization and
3052 * case-folding flags correct when we do the
3053 * file system creation, so go figure them out
3056 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3057 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3058 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3059 zct.zct_zplprops, &is_insensitive);
3061 nvlist_free(nvprops);
3062 nvlist_free(zct.zct_zplprops);
3066 error = dmu_objset_create(zc->zc_name, type,
3067 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3068 nvlist_free(zct.zct_zplprops);
3072 * It would be nice to do this atomically.
3075 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3078 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3080 nvlist_free(nvprops);
3082 if (error == 0 && type == DMU_OST_ZVOL)
3083 zvol_create_minors(zc->zc_name);
3090 * zc_name name of filesystem
3091 * zc_value short name of snapshot
3092 * zc_cookie recursive flag
3093 * zc_nvlist_src[_size] property list
3096 * zc_value short snapname (i.e. part after the '@')
3099 zfs_ioc_snapshot(zfs_cmd_t *zc)
3101 nvlist_t *nvprops = NULL;
3103 boolean_t recursive = zc->zc_cookie;
3105 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3108 if (zc->zc_nvlist_src != 0 &&
3109 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3110 zc->zc_iflags, &nvprops)) != 0)
3113 error = zfs_check_userprops(zc->zc_name, nvprops);
3117 if (!nvlist_empty(nvprops) &&
3118 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3123 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3124 nvprops, recursive, B_FALSE, -1);
3127 nvlist_free(nvprops);
3132 zfs_unmount_snap(const char *name, void *arg)
3137 char *snapname = arg;
3138 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3139 vfsp = zfs_get_vfs(fullname);
3141 } else if (strchr(name, '@')) {
3142 vfsp = zfs_get_vfs(name);
3147 * Always force the unmount for snapshots.
3149 int flag = MS_FORCE;
3152 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3157 mtx_lock(&Giant); /* dounmount() */
3158 dounmount(vfsp, flag, curthread);
3159 mtx_unlock(&Giant); /* dounmount() */
3166 * zc_name name of filesystem, snaps must be under it
3167 * zc_nvlist_src[_size] full names of snapshots to destroy
3168 * zc_defer_destroy mark for deferred destroy
3171 * zc_name on failure, name of failed snapshot
3174 zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc)
3180 if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3181 zc->zc_iflags, &nvl)) != 0) {
3186 * We are probably called by older binaries,
3187 * allocate and populate nvlist with recursive snapshots
3189 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3191 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3192 err = dmu_get_recursive_snaps_nvl(zc->zc_name,
3198 #endif /* __FreeBSD__ */
3201 len = strlen(zc->zc_name);
3202 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
3203 pair = nvlist_next_nvpair(nvl, pair)) {
3204 const char *name = nvpair_name(pair);
3206 * The snap name must be underneath the zc_name. This ensures
3207 * that our permission checks were legitimate.
3209 if (strncmp(zc->zc_name, name, len) != 0 ||
3210 (name[len] != '@' && name[len] != '/')) {
3215 (void) zfs_unmount_snap(name, NULL);
3218 err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy,
3226 * zc_name name of dataset to destroy
3227 * zc_objset_type type of objset
3228 * zc_defer_destroy mark for deferred destroy
3233 zfs_ioc_destroy(zfs_cmd_t *zc)
3236 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3237 err = zfs_unmount_snap(zc->zc_name, NULL);
3242 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3243 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3244 (void) zvol_remove_minor(zc->zc_name);
3250 * zc_name name of dataset to rollback (to most recent snapshot)
3255 zfs_ioc_rollback(zfs_cmd_t *zc)
3257 dsl_dataset_t *ds, *clone;
3262 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3266 /* must not be a snapshot */
3267 if (dsl_dataset_is_snapshot(ds)) {
3268 dsl_dataset_rele(ds, FTAG);
3272 /* must have a most recent snapshot */
3273 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3274 dsl_dataset_rele(ds, FTAG);
3279 * Create clone of most recent snapshot.
3281 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3282 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3286 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3293 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3294 error = zfs_suspend_fs(zfsvfs);
3298 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3299 error = dsl_dataset_clone_swap(clone, ds,
3301 dsl_dataset_disown(ds, FTAG);
3306 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3307 error = error ? error : resume_err;
3309 VFS_RELE(zfsvfs->z_vfs);
3311 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3312 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3313 dsl_dataset_disown(ds, FTAG);
3321 * Destroy clone (which also closes it).
3323 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3326 strfree(clone_name);
3328 dsl_dataset_rele(ds, FTAG);
3334 * zc_name old name of dataset
3335 * zc_value new name of dataset
3336 * zc_cookie recursive flag (only valid for snapshots)
3341 zfs_ioc_rename(zfs_cmd_t *zc)
3345 if (zc->zc_cookie & 1)
3346 flags |= ZFS_RENAME_RECURSIVE;
3347 if (zc->zc_cookie & 2)
3348 flags |= ZFS_RENAME_ALLOW_MOUNTED;
3350 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3351 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3352 strchr(zc->zc_value, '%'))
3356 * Unmount snapshot unless we're doing a recursive rename,
3357 * in which case the dataset code figures out which snapshots
3360 if (!(flags & ZFS_RENAME_RECURSIVE) &&
3361 strchr(zc->zc_name, '@') != NULL &&
3362 zc->zc_objset_type == DMU_OST_ZFS) {
3363 int err = zfs_unmount_snap(zc->zc_name, NULL);
3367 return (dmu_objset_rename(zc->zc_name, zc->zc_value, flags));
3371 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3373 const char *propname = nvpair_name(pair);
3374 boolean_t issnap = (strchr(dsname, '@') != NULL);
3375 zfs_prop_t prop = zfs_name_to_prop(propname);
3379 if (prop == ZPROP_INVAL) {
3380 if (zfs_prop_user(propname)) {
3381 if (err = zfs_secpolicy_write_perms(dsname,
3382 ZFS_DELEG_PERM_USERPROP, cr))
3387 if (!issnap && zfs_prop_userquota(propname)) {
3388 const char *perm = NULL;
3389 const char *uq_prefix =
3390 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3391 const char *gq_prefix =
3392 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3394 if (strncmp(propname, uq_prefix,
3395 strlen(uq_prefix)) == 0) {
3396 perm = ZFS_DELEG_PERM_USERQUOTA;
3397 } else if (strncmp(propname, gq_prefix,
3398 strlen(gq_prefix)) == 0) {
3399 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3401 /* USERUSED and GROUPUSED are read-only */
3405 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3416 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3418 * dsl_prop_get_all_impl() returns properties in this
3422 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3423 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3428 * Check that this value is valid for this pool version
3431 case ZFS_PROP_COMPRESSION:
3433 * If the user specified gzip compression, make sure
3434 * the SPA supports it. We ignore any errors here since
3435 * we'll catch them later.
3437 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3438 nvpair_value_uint64(pair, &intval) == 0) {
3439 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3440 intval <= ZIO_COMPRESS_GZIP_9 &&
3441 zfs_earlier_version(dsname,
3442 SPA_VERSION_GZIP_COMPRESSION)) {
3446 if (intval == ZIO_COMPRESS_ZLE &&
3447 zfs_earlier_version(dsname,
3448 SPA_VERSION_ZLE_COMPRESSION))
3452 * If this is a bootable dataset then
3453 * verify that the compression algorithm
3454 * is supported for booting. We must return
3455 * something other than ENOTSUP since it
3456 * implies a downrev pool version.
3458 if (zfs_is_bootfs(dsname) &&
3459 !BOOTFS_COMPRESS_VALID(intval)) {
3465 case ZFS_PROP_COPIES:
3466 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3470 case ZFS_PROP_DEDUP:
3471 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3475 case ZFS_PROP_SHARESMB:
3476 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3480 case ZFS_PROP_ACLINHERIT:
3481 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3482 nvpair_value_uint64(pair, &intval) == 0) {
3483 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3484 zfs_earlier_version(dsname,
3485 SPA_VERSION_PASSTHROUGH_X))
3491 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3495 * Removes properties from the given props list that fail permission checks
3496 * needed to clear them and to restore them in case of a receive error. For each
3497 * property, make sure we have both set and inherit permissions.
3499 * Returns the first error encountered if any permission checks fail. If the
3500 * caller provides a non-NULL errlist, it also gives the complete list of names
3501 * of all the properties that failed a permission check along with the
3502 * corresponding error numbers. The caller is responsible for freeing the
3505 * If every property checks out successfully, zero is returned and the list
3506 * pointed at by errlist is NULL.
3509 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3512 nvpair_t *pair, *next_pair;
3519 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3521 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3522 (void) strcpy(zc->zc_name, dataset);
3523 pair = nvlist_next_nvpair(props, NULL);
3524 while (pair != NULL) {
3525 next_pair = nvlist_next_nvpair(props, pair);
3527 (void) strcpy(zc->zc_value, nvpair_name(pair));
3528 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3529 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3530 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3531 VERIFY(nvlist_add_int32(errors,
3532 zc->zc_value, err) == 0);
3536 kmem_free(zc, sizeof (zfs_cmd_t));
3538 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3539 nvlist_free(errors);
3542 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3545 if (errlist == NULL)
3546 nvlist_free(errors);
3554 propval_equals(nvpair_t *p1, nvpair_t *p2)
3556 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3557 /* dsl_prop_get_all_impl() format */
3559 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3560 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3564 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3566 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3567 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3571 if (nvpair_type(p1) != nvpair_type(p2))
3574 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3575 char *valstr1, *valstr2;
3577 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3578 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3579 return (strcmp(valstr1, valstr2) == 0);
3581 uint64_t intval1, intval2;
3583 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3584 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3585 return (intval1 == intval2);
3590 * Remove properties from props if they are not going to change (as determined
3591 * by comparison with origprops). Remove them from origprops as well, since we
3592 * do not need to clear or restore properties that won't change.
3595 props_reduce(nvlist_t *props, nvlist_t *origprops)
3597 nvpair_t *pair, *next_pair;
3599 if (origprops == NULL)
3600 return; /* all props need to be received */
3602 pair = nvlist_next_nvpair(props, NULL);
3603 while (pair != NULL) {
3604 const char *propname = nvpair_name(pair);
3607 next_pair = nvlist_next_nvpair(props, pair);
3609 if ((nvlist_lookup_nvpair(origprops, propname,
3610 &match) != 0) || !propval_equals(pair, match))
3611 goto next; /* need to set received value */
3613 /* don't clear the existing received value */
3614 (void) nvlist_remove_nvpair(origprops, match);
3615 /* don't bother receiving the property */
3616 (void) nvlist_remove_nvpair(props, pair);
3623 static boolean_t zfs_ioc_recv_inject_err;
3628 * zc_name name of containing filesystem
3629 * zc_nvlist_src{_size} nvlist of properties to apply
3630 * zc_value name of snapshot to create
3631 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3632 * zc_cookie file descriptor to recv from
3633 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3634 * zc_guid force flag
3635 * zc_cleanup_fd cleanup-on-exit file descriptor
3636 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3639 * zc_cookie number of bytes read
3640 * zc_nvlist_dst{_size} error for each unapplied received property
3641 * zc_obj zprop_errflags_t
3642 * zc_action_handle handle for this guid/ds mapping
3645 zfs_ioc_recv(zfs_cmd_t *zc)
3649 dmu_recv_cookie_t drc;
3650 boolean_t force = (boolean_t)zc->zc_guid;
3653 int props_error = 0;
3656 nvlist_t *props = NULL; /* sent properties */
3657 nvlist_t *origprops = NULL; /* existing properties */
3658 objset_t *origin = NULL;
3660 char tofs[ZFS_MAXNAMELEN];
3661 boolean_t first_recvd_props = B_FALSE;
3663 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3664 strchr(zc->zc_value, '@') == NULL ||
3665 strchr(zc->zc_value, '%'))
3668 (void) strcpy(tofs, zc->zc_value);
3669 tosnap = strchr(tofs, '@');
3672 if (zc->zc_nvlist_src != 0 &&
3673 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3674 zc->zc_iflags, &props)) != 0)
3684 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3686 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3687 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3688 !dsl_prop_get_hasrecvd(os)) {
3689 first_recvd_props = B_TRUE;
3693 * If new received properties are supplied, they are to
3694 * completely replace the existing received properties, so stash
3695 * away the existing ones.
3697 if (dsl_prop_get_received(os, &origprops) == 0) {
3698 nvlist_t *errlist = NULL;
3700 * Don't bother writing a property if its value won't
3701 * change (and avoid the unnecessary security checks).
3703 * The first receive after SPA_VERSION_RECVD_PROPS is a
3704 * special case where we blow away all local properties
3707 if (!first_recvd_props)
3708 props_reduce(props, origprops);
3709 if (zfs_check_clearable(tofs, origprops,
3711 (void) nvlist_merge(errors, errlist, 0);
3712 nvlist_free(errlist);
3715 dmu_objset_rele(os, FTAG);
3718 if (zc->zc_string[0]) {
3719 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3724 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3725 &zc->zc_begin_record, force, origin, &drc);
3727 dmu_objset_rele(origin, FTAG);
3732 * Set properties before we receive the stream so that they are applied
3733 * to the new data. Note that we must call dmu_recv_stream() if
3734 * dmu_recv_begin() succeeds.
3739 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3740 if (drc.drc_newfs) {
3741 if (spa_version(os->os_spa) >=
3742 SPA_VERSION_RECVD_PROPS)
3743 first_recvd_props = B_TRUE;
3744 } else if (origprops != NULL) {
3745 if (clear_received_props(os, tofs, origprops,
3746 first_recvd_props ? NULL : props) != 0)
3747 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3749 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3751 dsl_prop_set_hasrecvd(os);
3752 } else if (!drc.drc_newfs) {
3753 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3756 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3758 (void) nvlist_merge(errors, errlist, 0);
3759 nvlist_free(errlist);
3762 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3764 * Caller made zc->zc_nvlist_dst less than the minimum expected
3765 * size or supplied an invalid address.
3767 props_error = EINVAL;
3771 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
3772 &zc->zc_action_handle);
3775 zfsvfs_t *zfsvfs = NULL;
3777 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3781 error = zfs_suspend_fs(zfsvfs);
3783 * If the suspend fails, then the recv_end will
3784 * likely also fail, and clean up after itself.
3786 end_err = dmu_recv_end(&drc);
3788 error = zfs_resume_fs(zfsvfs, tofs);
3789 error = error ? error : end_err;
3790 VFS_RELE(zfsvfs->z_vfs);
3792 error = dmu_recv_end(&drc);
3796 zc->zc_cookie = off - fp->f_offset;
3797 if (off >= 0 && off <= MAXOFFSET_T)
3801 if (zfs_ioc_recv_inject_err) {
3802 zfs_ioc_recv_inject_err = B_FALSE;
3807 * On error, restore the original props.
3809 if (error && props) {
3810 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3811 if (clear_received_props(os, tofs, props, NULL) != 0) {
3813 * We failed to clear the received properties.
3814 * Since we may have left a $recvd value on the
3815 * system, we can't clear the $hasrecvd flag.
3817 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3818 } else if (first_recvd_props) {
3819 dsl_prop_unset_hasrecvd(os);
3821 dmu_objset_rele(os, FTAG);
3822 } else if (!drc.drc_newfs) {
3823 /* We failed to clear the received properties. */
3824 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3827 if (origprops == NULL && !drc.drc_newfs) {
3828 /* We failed to stash the original properties. */
3829 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3833 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3834 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3835 * explictly if we're restoring local properties cleared in the
3836 * first new-style receive.
3838 if (origprops != NULL &&
3839 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3840 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3841 origprops, NULL) != 0) {
3843 * We stashed the original properties but failed to
3846 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3851 nvlist_free(origprops);
3852 nvlist_free(errors);
3856 error = props_error;
3863 * zc_name name of snapshot to send
3864 * zc_cookie file descriptor to send stream to
3865 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3866 * zc_sendobj objsetid of snapshot to send
3867 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3868 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
3869 * output size in zc_objset_type.
3874 zfs_ioc_send(zfs_cmd_t *zc)
3876 objset_t *fromsnap = NULL;
3881 dsl_dataset_t *dsfrom = NULL;
3884 boolean_t estimate = (zc->zc_guid != 0);
3886 error = spa_open(zc->zc_name, &spa, FTAG);
3890 dp = spa_get_dsl(spa);
3891 rw_enter(&dp->dp_config_rwlock, RW_READER);
3892 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3893 rw_exit(&dp->dp_config_rwlock);
3895 spa_close(spa, FTAG);
3899 error = dmu_objset_from_ds(ds, &tosnap);
3901 dsl_dataset_rele(ds, FTAG);
3902 spa_close(spa, FTAG);
3906 if (zc->zc_fromobj != 0) {
3907 rw_enter(&dp->dp_config_rwlock, RW_READER);
3908 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3909 rw_exit(&dp->dp_config_rwlock);
3910 spa_close(spa, FTAG);
3912 dsl_dataset_rele(ds, FTAG);
3915 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3917 dsl_dataset_rele(dsfrom, FTAG);
3918 dsl_dataset_rele(ds, FTAG);
3922 spa_close(spa, FTAG);
3926 error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj,
3927 &zc->zc_objset_type);
3929 file_t *fp = getf(zc->zc_cookie);
3931 dsl_dataset_rele(ds, FTAG);
3933 dsl_dataset_rele(dsfrom, FTAG);
3938 error = dmu_send(tosnap, fromsnap, zc->zc_obj,
3939 zc->zc_cookie, fp, &off);
3941 if (off >= 0 && off <= MAXOFFSET_T)
3943 releasef(zc->zc_cookie);
3946 dsl_dataset_rele(dsfrom, FTAG);
3947 dsl_dataset_rele(ds, FTAG);
3953 * zc_name name of snapshot on which to report progress
3954 * zc_cookie file descriptor of send stream
3957 * zc_cookie number of bytes written in send stream thus far
3960 zfs_ioc_send_progress(zfs_cmd_t *zc)
3963 dmu_sendarg_t *dsp = NULL;
3966 if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0)
3969 mutex_enter(&ds->ds_sendstream_lock);
3972 * Iterate over all the send streams currently active on this dataset.
3973 * If there's one which matches the specified file descriptor _and_ the
3974 * stream was started by the current process, return the progress of
3977 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
3978 dsp = list_next(&ds->ds_sendstreams, dsp)) {
3979 if (dsp->dsa_outfd == zc->zc_cookie &&
3980 dsp->dsa_proc == curproc)
3985 zc->zc_cookie = *(dsp->dsa_off);
3989 mutex_exit(&ds->ds_sendstream_lock);
3990 dsl_dataset_rele(ds, FTAG);
3995 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3999 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4000 &zc->zc_inject_record);
4003 zc->zc_guid = (uint64_t)id;
4009 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4011 return (zio_clear_fault((int)zc->zc_guid));
4015 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4017 int id = (int)zc->zc_guid;
4020 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4021 &zc->zc_inject_record);
4029 zfs_ioc_error_log(zfs_cmd_t *zc)
4033 size_t count = (size_t)zc->zc_nvlist_dst_size;
4035 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4038 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4041 zc->zc_nvlist_dst_size = count;
4043 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4045 spa_close(spa, FTAG);
4051 zfs_ioc_clear(zfs_cmd_t *zc)
4058 * On zpool clear we also fix up missing slogs
4060 mutex_enter(&spa_namespace_lock);
4061 spa = spa_lookup(zc->zc_name);
4063 mutex_exit(&spa_namespace_lock);
4066 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4067 /* we need to let spa_open/spa_load clear the chains */
4068 spa_set_log_state(spa, SPA_LOG_CLEAR);
4070 spa->spa_last_open_failed = 0;
4071 mutex_exit(&spa_namespace_lock);
4073 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4074 error = spa_open(zc->zc_name, &spa, FTAG);
4077 nvlist_t *config = NULL;
4079 if (zc->zc_nvlist_src == 0)
4082 if ((error = get_nvlist(zc->zc_nvlist_src,
4083 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4084 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4086 if (config != NULL) {
4089 if ((err = put_nvlist(zc, config)) != 0)
4091 nvlist_free(config);
4093 nvlist_free(policy);
4100 spa_vdev_state_enter(spa, SCL_NONE);
4102 if (zc->zc_guid == 0) {
4105 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4107 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4108 spa_close(spa, FTAG);
4113 vdev_clear(spa, vd);
4115 (void) spa_vdev_state_exit(spa, NULL, 0);
4118 * Resume any suspended I/Os.
4120 if (zio_resume(spa) != 0)
4123 spa_close(spa, FTAG);
4130 * zc_name name of filesystem
4131 * zc_value name of origin snapshot
4134 * zc_string name of conflicting snapshot, if there is one
4137 zfs_ioc_promote(zfs_cmd_t *zc)
4142 * We don't need to unmount *all* the origin fs's snapshots, but
4145 cp = strchr(zc->zc_value, '@');
4148 (void) dmu_objset_find(zc->zc_value,
4149 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4150 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4154 * Retrieve a single {user|group}{used|quota}@... property.
4157 * zc_name name of filesystem
4158 * zc_objset_type zfs_userquota_prop_t
4159 * zc_value domain name (eg. "S-1-234-567-89")
4160 * zc_guid RID/UID/GID
4163 * zc_cookie property value
4166 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4171 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4174 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4178 error = zfs_userspace_one(zfsvfs,
4179 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4180 zfsvfs_rele(zfsvfs, FTAG);
4187 * zc_name name of filesystem
4188 * zc_cookie zap cursor
4189 * zc_objset_type zfs_userquota_prop_t
4190 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4193 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4194 * zc_cookie zap cursor
4197 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4200 int bufsize = zc->zc_nvlist_dst_size;
4205 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4209 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4211 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4212 buf, &zc->zc_nvlist_dst_size);
4215 error = ddi_copyout(buf,
4216 (void *)(uintptr_t)zc->zc_nvlist_dst,
4217 zc->zc_nvlist_dst_size, zc->zc_iflags);
4219 kmem_free(buf, bufsize);
4220 zfsvfs_rele(zfsvfs, FTAG);
4227 * zc_name name of filesystem
4233 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4239 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4240 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4242 * If userused is not enabled, it may be because the
4243 * objset needs to be closed & reopened (to grow the
4244 * objset_phys_t). Suspend/resume the fs will do that.
4246 error = zfs_suspend_fs(zfsvfs);
4248 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4251 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4252 VFS_RELE(zfsvfs->z_vfs);
4254 /* XXX kind of reading contents without owning */
4255 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4259 error = dmu_objset_userspace_upgrade(os);
4260 dmu_objset_rele(os, FTAG);
4268 * We don't want to have a hard dependency
4269 * against some special symbols in sharefs
4270 * nfs, and smbsrv. Determine them if needed when
4271 * the first file system is shared.
4272 * Neither sharefs, nfs or smbsrv are unloadable modules.
4274 int (*znfsexport_fs)(void *arg);
4275 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4276 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4278 int zfs_nfsshare_inited;
4279 int zfs_smbshare_inited;
4281 ddi_modhandle_t nfs_mod;
4282 ddi_modhandle_t sharefs_mod;
4283 ddi_modhandle_t smbsrv_mod;
4285 kmutex_t zfs_share_lock;
4293 ASSERT(MUTEX_HELD(&zfs_share_lock));
4294 /* Both NFS and SMB shares also require sharetab support. */
4295 if (sharefs_mod == NULL && ((sharefs_mod =
4296 ddi_modopen("fs/sharefs",
4297 KRTLD_MODE_FIRST, &error)) == NULL)) {
4300 if (zshare_fs == NULL && ((zshare_fs =
4301 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4302 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4310 zfs_ioc_share(zfs_cmd_t *zc)
4316 switch (zc->zc_share.z_sharetype) {
4318 case ZFS_UNSHARE_NFS:
4319 if (zfs_nfsshare_inited == 0) {
4320 mutex_enter(&zfs_share_lock);
4321 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4322 KRTLD_MODE_FIRST, &error)) == NULL)) {
4323 mutex_exit(&zfs_share_lock);
4326 if (znfsexport_fs == NULL &&
4327 ((znfsexport_fs = (int (*)(void *))
4329 "nfs_export", &error)) == NULL)) {
4330 mutex_exit(&zfs_share_lock);
4333 error = zfs_init_sharefs();
4335 mutex_exit(&zfs_share_lock);
4338 zfs_nfsshare_inited = 1;
4339 mutex_exit(&zfs_share_lock);
4343 case ZFS_UNSHARE_SMB:
4344 if (zfs_smbshare_inited == 0) {
4345 mutex_enter(&zfs_share_lock);
4346 if (smbsrv_mod == NULL && ((smbsrv_mod =
4347 ddi_modopen("drv/smbsrv",
4348 KRTLD_MODE_FIRST, &error)) == NULL)) {
4349 mutex_exit(&zfs_share_lock);
4352 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4353 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4354 "smb_server_share", &error)) == NULL)) {
4355 mutex_exit(&zfs_share_lock);
4358 error = zfs_init_sharefs();
4360 mutex_exit(&zfs_share_lock);
4363 zfs_smbshare_inited = 1;
4364 mutex_exit(&zfs_share_lock);
4371 switch (zc->zc_share.z_sharetype) {
4373 case ZFS_UNSHARE_NFS:
4375 znfsexport_fs((void *)
4376 (uintptr_t)zc->zc_share.z_exportdata))
4380 case ZFS_UNSHARE_SMB:
4381 if (error = zsmbexport_fs((void *)
4382 (uintptr_t)zc->zc_share.z_exportdata,
4383 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4390 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4391 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4392 SHAREFS_ADD : SHAREFS_REMOVE;
4395 * Add or remove share from sharetab
4397 error = zshare_fs(opcode,
4398 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4399 zc->zc_share.z_sharemax);
4408 ace_t full_access[] = {
4409 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4414 * zc_name name of containing filesystem
4415 * zc_obj object # beyond which we want next in-use object #
4418 * zc_obj next in-use object #
4421 zfs_ioc_next_obj(zfs_cmd_t *zc)
4423 objset_t *os = NULL;
4426 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4430 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4431 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4433 dmu_objset_rele(os, FTAG);
4439 * zc_name name of filesystem
4440 * zc_value prefix name for snapshot
4441 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4446 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4451 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4452 (u_longlong_t)ddi_get_lbolt64());
4454 if (strlen(snap_name) >= MAXNAMELEN) {
4459 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4460 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4466 (void) strcpy(zc->zc_value, snap_name);
4473 * zc_name name of "to" snapshot
4474 * zc_value name of "from" snapshot
4475 * zc_cookie file descriptor to write diff data on
4478 * dmu_diff_record_t's to the file descriptor
4481 zfs_ioc_diff(zfs_cmd_t *zc)
4489 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4493 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4495 dmu_objset_rele(tosnap, FTAG);
4499 fp = getf(zc->zc_cookie);
4501 dmu_objset_rele(fromsnap, FTAG);
4502 dmu_objset_rele(tosnap, FTAG);
4508 error = dmu_diff(tosnap, fromsnap, fp, &off);
4510 if (off >= 0 && off <= MAXOFFSET_T)
4512 releasef(zc->zc_cookie);
4514 dmu_objset_rele(fromsnap, FTAG);
4515 dmu_objset_rele(tosnap, FTAG);
4521 * Remove all ACL files in shares dir
4524 zfs_smb_acl_purge(znode_t *dzp)
4527 zap_attribute_t zap;
4528 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4531 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4532 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4533 zap_cursor_advance(&zc)) {
4534 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4538 zap_cursor_fini(&zc);
4544 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4549 vnode_t *resourcevp = NULL;
4558 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4559 NO_FOLLOW, NULL, &vp)) != 0)
4562 /* Now make sure mntpnt and dataset are ZFS */
4564 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
4565 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4566 zc->zc_name) != 0)) {
4572 zfsvfs = dzp->z_zfsvfs;
4576 * Create share dir if its missing.
4578 mutex_enter(&zfsvfs->z_lock);
4579 if (zfsvfs->z_shares_dir == 0) {
4582 tx = dmu_tx_create(zfsvfs->z_os);
4583 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4585 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4586 error = dmu_tx_assign(tx, TXG_WAIT);
4590 error = zfs_create_share_dir(zfsvfs, tx);
4594 mutex_exit(&zfsvfs->z_lock);
4600 mutex_exit(&zfsvfs->z_lock);
4602 ASSERT(zfsvfs->z_shares_dir);
4603 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4609 switch (zc->zc_cookie) {
4610 case ZFS_SMB_ACL_ADD:
4611 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4612 vattr.va_type = VREG;
4613 vattr.va_mode = S_IFREG|0777;
4617 vsec.vsa_mask = VSA_ACE;
4618 vsec.vsa_aclentp = &full_access;
4619 vsec.vsa_aclentsz = sizeof (full_access);
4620 vsec.vsa_aclcnt = 1;
4622 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4623 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4625 VN_RELE(resourcevp);
4628 case ZFS_SMB_ACL_REMOVE:
4629 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4633 case ZFS_SMB_ACL_RENAME:
4634 if ((error = get_nvlist(zc->zc_nvlist_src,
4635 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4640 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4641 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4644 VN_RELE(ZTOV(sharedir));
4646 nvlist_free(nvlist);
4649 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4651 nvlist_free(nvlist);
4654 case ZFS_SMB_ACL_PURGE:
4655 error = zfs_smb_acl_purge(sharedir);
4664 VN_RELE(ZTOV(sharedir));
4670 return (EOPNOTSUPP);
4676 * zc_name name of filesystem
4677 * zc_value short name of snap
4678 * zc_string user-supplied tag for this hold
4679 * zc_cookie recursive flag
4680 * zc_temphold set if hold is temporary
4681 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4682 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4683 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4688 zfs_ioc_hold(zfs_cmd_t *zc)
4690 boolean_t recursive = zc->zc_cookie;
4697 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4700 if (zc->zc_sendobj == 0) {
4701 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4702 zc->zc_string, recursive, zc->zc_temphold,
4703 zc->zc_cleanup_fd));
4709 error = spa_open(zc->zc_name, &spa, FTAG);
4713 dp = spa_get_dsl(spa);
4714 rw_enter(&dp->dp_config_rwlock, RW_READER);
4715 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4716 rw_exit(&dp->dp_config_rwlock);
4717 spa_close(spa, FTAG);
4722 * Until we have a hold on this snapshot, it's possible that
4723 * zc_sendobj could've been destroyed and reused as part
4724 * of a later txg. Make sure we're looking at the right object.
4726 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4727 dsl_dataset_rele(ds, FTAG);
4731 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4732 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4734 dsl_dataset_rele(ds, FTAG);
4739 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4743 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4746 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4748 dsl_dataset_rele(ds, FTAG);
4755 * zc_name name of dataset from which we're releasing a user hold
4756 * zc_value short name of snap
4757 * zc_string user-supplied tag for this hold
4758 * zc_cookie recursive flag
4763 zfs_ioc_release(zfs_cmd_t *zc)
4765 boolean_t recursive = zc->zc_cookie;
4767 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4770 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4771 zc->zc_string, recursive));
4776 * zc_name name of filesystem
4779 * zc_nvlist_src{_size} nvlist of snapshot holds
4782 zfs_ioc_get_holds(zfs_cmd_t *zc)
4787 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4788 error = put_nvlist(zc, nvp);
4797 * zc_name name of new filesystem or snapshot
4798 * zc_value full name of old snapshot
4801 * zc_cookie space in bytes
4802 * zc_objset_type compressed space in bytes
4803 * zc_perm_action uncompressed space in bytes
4806 zfs_ioc_space_written(zfs_cmd_t *zc)
4809 dsl_dataset_t *new, *old;
4811 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4814 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4816 dsl_dataset_rele(new, FTAG);
4820 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4821 &zc->zc_objset_type, &zc->zc_perm_action);
4822 dsl_dataset_rele(old, FTAG);
4823 dsl_dataset_rele(new, FTAG);
4829 * zc_name full name of last snapshot
4830 * zc_value full name of first snapshot
4833 * zc_cookie space in bytes
4834 * zc_objset_type compressed space in bytes
4835 * zc_perm_action uncompressed space in bytes
4838 zfs_ioc_space_snaps(zfs_cmd_t *zc)
4841 dsl_dataset_t *new, *old;
4843 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4846 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4848 dsl_dataset_rele(new, FTAG);
4852 error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4853 &zc->zc_objset_type, &zc->zc_perm_action);
4854 dsl_dataset_rele(old, FTAG);
4855 dsl_dataset_rele(new, FTAG);
4860 * pool create, destroy, and export don't log the history as part of
4861 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4862 * do the logging of those commands.
4865 zfs_ioc_jail(zfs_cmd_t *zc)
4868 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
4869 (int)zc->zc_jailid));
4873 zfs_ioc_unjail(zfs_cmd_t *zc)
4876 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
4877 (int)zc->zc_jailid));
4880 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4881 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4883 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4885 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4887 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4889 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4891 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4893 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4895 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4897 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4899 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4901 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4903 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4905 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4907 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4909 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4911 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4913 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4915 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4917 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4919 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4921 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4923 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4925 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
4926 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
4927 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4929 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4931 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE },
4932 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
4933 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE, B_FALSE },
4934 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4936 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4938 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4940 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4942 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
4943 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4945 { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive, DATASET_NAME,
4947 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4949 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4951 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4953 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4955 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4957 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4959 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4961 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
4962 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4964 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4966 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
4967 DATASET_NAME, B_FALSE, B_FALSE },
4968 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
4969 DATASET_NAME, B_FALSE, B_FALSE },
4970 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4971 DATASET_NAME, B_FALSE, B_TRUE },
4972 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
4973 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4975 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4977 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4979 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4981 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4983 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, B_FALSE },
4984 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4986 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4988 { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
4989 { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
4990 { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4992 { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4994 { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4996 { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5001 pool_status_check(const char *name, zfs_ioc_namecheck_t type)
5006 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5008 error = spa_open(name, &spa, FTAG);
5010 if (spa_suspended(spa))
5012 spa_close(spa, FTAG);
5018 * Find a free minor number.
5021 zfsdev_minor_alloc(void)
5023 static minor_t last_minor;
5026 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5028 for (m = last_minor + 1; m != last_minor; m++) {
5029 if (m > ZFSDEV_MAX_MINOR)
5031 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5041 zfs_ctldev_init(struct cdev *devp)
5044 zfs_soft_state_t *zs;
5046 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5048 minor = zfsdev_minor_alloc();
5052 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5055 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5057 zs = ddi_get_soft_state(zfsdev_state, minor);
5058 zs->zss_type = ZSST_CTLDEV;
5059 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5065 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5067 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5069 zfs_onexit_destroy(zo);
5070 ddi_soft_state_free(zfsdev_state, minor);
5074 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5076 zfs_soft_state_t *zp;
5078 zp = ddi_get_soft_state(zfsdev_state, minor);
5079 if (zp == NULL || zp->zss_type != which)
5082 return (zp->zss_data);
5086 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
5091 if (getminor(*devp) != 0)
5092 return (zvol_open(devp, flag, otyp, cr));
5095 /* This is the control device. Allocate a new minor if requested. */
5097 mutex_enter(&spa_namespace_lock);
5098 error = zfs_ctldev_init(devp);
5099 mutex_exit(&spa_namespace_lock);
5106 zfsdev_close(void *data)
5109 minor_t minor = (minor_t)(uintptr_t)data;
5114 mutex_enter(&spa_namespace_lock);
5115 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5117 mutex_exit(&spa_namespace_lock);
5120 zfs_ctldev_destroy(zo, minor);
5121 mutex_exit(&spa_namespace_lock);
5125 zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
5130 int cflag, error, len;
5132 cflag = ZFS_CMD_COMPAT_NONE;
5133 len = IOCPARM_LEN(cmd);
5136 * Check if we have sufficient kernel memory allocated
5137 * for the zfs_cmd_t request. Bail out if not so we
5138 * will not access undefined memory region.
5140 if (len < sizeof(zfs_cmd_t))
5141 if (len == sizeof(zfs_cmd_v15_t)) {
5142 cflag = ZFS_CMD_COMPAT_V15;
5143 vec = zfs_ioctl_v15_to_v28[ZFS_IOC(cmd)];
5149 if (cflag != ZFS_CMD_COMPAT_NONE) {
5150 if (vec == ZFS_IOC_COMPAT_PASS)
5152 else if (vec == ZFS_IOC_COMPAT_FAIL)
5156 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5159 if (cflag != ZFS_CMD_COMPAT_NONE) {
5160 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5161 bzero(zc, sizeof(zfs_cmd_t));
5162 zfs_cmd_compat_get(zc, addr, cflag);
5163 zfs_ioctl_compat_pre(zc, &vec, cflag);
5168 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred);
5171 * Ensure that all pool/dataset names are valid before we pass down to
5175 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5176 zc->zc_iflags = flag & FKIOCTL;
5177 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5179 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5181 if (zfs_ioc_vec[vec].zvec_pool_check)
5182 error = pool_status_check(zc->zc_name,
5183 zfs_ioc_vec[vec].zvec_namecheck);
5187 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5189 if (zfs_ioc_vec[vec].zvec_pool_check)
5190 error = pool_status_check(zc->zc_name,
5191 zfs_ioc_vec[vec].zvec_namecheck);
5200 error = zfs_ioc_vec[vec].zvec_func(zc);
5203 if (zfs_ioc_vec[vec].zvec_his_log)
5204 zfs_log_history(zc);
5207 if (cflag != ZFS_CMD_COMPAT_NONE) {
5208 zfs_ioctl_compat_post(zc, ZFS_IOC(cmd), cflag);
5209 zfs_cmd_compat_put(zc, addr, cflag);
5210 kmem_free(zc, sizeof(zfs_cmd_t));
5218 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5220 if (cmd != DDI_ATTACH)
5221 return (DDI_FAILURE);
5223 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5224 DDI_PSEUDO, 0) == DDI_FAILURE)
5225 return (DDI_FAILURE);
5229 ddi_report_dev(dip);
5231 return (DDI_SUCCESS);
5235 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5237 if (spa_busy() || zfs_busy() || zvol_busy())
5238 return (DDI_FAILURE);
5240 if (cmd != DDI_DETACH)
5241 return (DDI_FAILURE);
5245 ddi_prop_remove_all(dip);
5246 ddi_remove_minor_node(dip, NULL);
5248 return (DDI_SUCCESS);
5253 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5256 case DDI_INFO_DEVT2DEVINFO:
5258 return (DDI_SUCCESS);
5260 case DDI_INFO_DEVT2INSTANCE:
5261 *result = (void *)0;
5262 return (DDI_SUCCESS);
5265 return (DDI_FAILURE);
5270 * OK, so this is a little weird.
5272 * /dev/zfs is the control node, i.e. minor 0.
5273 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5275 * /dev/zfs has basically nothing to do except serve up ioctls,
5276 * so most of the standard driver entry points are in zvol.c.
5279 static struct cb_ops zfs_cb_ops = {
5280 zfsdev_open, /* open */
5281 zfsdev_close, /* close */
5282 zvol_strategy, /* strategy */
5284 zvol_dump, /* dump */
5285 zvol_read, /* read */
5286 zvol_write, /* write */
5287 zfsdev_ioctl, /* ioctl */
5291 nochpoll, /* poll */
5292 ddi_prop_op, /* prop_op */
5293 NULL, /* streamtab */
5294 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
5295 CB_REV, /* version */
5296 nodev, /* async read */
5297 nodev, /* async write */
5300 static struct dev_ops zfs_dev_ops = {
5301 DEVO_REV, /* version */
5303 zfs_info, /* info */
5304 nulldev, /* identify */
5305 nulldev, /* probe */
5306 zfs_attach, /* attach */
5307 zfs_detach, /* detach */
5309 &zfs_cb_ops, /* driver operations */
5310 NULL, /* no bus operations */
5312 ddi_quiesce_not_needed, /* quiesce */
5315 static struct modldrv zfs_modldrv = {
5321 static struct modlinkage modlinkage = {
5323 (void *)&zfs_modlfs,
5324 (void *)&zfs_modldrv,
5329 static struct cdevsw zfs_cdevsw = {
5330 .d_version = D_VERSION,
5331 .d_open = zfsdev_open,
5332 .d_ioctl = zfsdev_ioctl,
5333 .d_name = ZFS_DEV_NAME
5339 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
5347 destroy_dev(zfsdev);
5350 static struct root_hold_token *zfs_root_token;
5351 struct proc *zfsproc;
5353 uint_t zfs_fsyncer_key;
5354 extern uint_t rrw_tsd_key;
5362 spa_init(FREAD | FWRITE);
5366 if ((error = mod_install(&modlinkage)) != 0) {
5373 tsd_create(&zfs_fsyncer_key, NULL);
5374 tsd_create(&rrw_tsd_key, NULL);
5376 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5378 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5388 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5391 if ((error = mod_remove(&modlinkage)) != 0)
5397 if (zfs_nfsshare_inited)
5398 (void) ddi_modclose(nfs_mod);
5399 if (zfs_smbshare_inited)
5400 (void) ddi_modclose(smbsrv_mod);
5401 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5402 (void) ddi_modclose(sharefs_mod);
5404 tsd_destroy(&zfs_fsyncer_key);
5405 ldi_ident_release(zfs_li);
5407 mutex_destroy(&zfs_share_lock);
5413 _info(struct modinfo *modinfop)
5415 return (mod_info(&modlinkage, modinfop));
5420 zfs_modevent(module_t mod, int type, void *unused __unused)
5426 zfs_root_token = root_mount_hold("ZFS");
5428 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5430 spa_init(FREAD | FWRITE);
5434 tsd_create(&zfs_fsyncer_key, NULL);
5435 tsd_create(&rrw_tsd_key, NULL);
5437 printf("ZFS storage pool version " SPA_VERSION_STRING "\n");
5438 root_mount_rel(zfs_root_token);
5443 if (spa_busy() || zfs_busy() || zvol_busy() ||
5444 zio_injection_enabled) {
5454 tsd_destroy(&zfs_fsyncer_key);
5455 tsd_destroy(&rrw_tsd_key);
5457 mutex_destroy(&zfs_share_lock);
5466 static moduledata_t zfs_mod = {
5471 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
5472 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
5473 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);