4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/errno.h>
30 #include <sys/modctl.h>
35 #include <sys/cmn_err.h>
37 #include <sys/zfs_ioctl.h>
38 #include <sys/zfs_vfsops.h>
39 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
44 #include <sys/priv_impl.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_dir.h>
63 #include <sys/zfs_onexit.h>
65 #include <sys/dsl_scan.h>
66 #include <sharefs/share.h>
67 #include <sys/dmu_objset.h>
69 #include "zfs_namecheck.h"
71 #include "zfs_deleg.h"
72 #include "zfs_comutil.h"
74 extern struct modlfs zfs_modlfs;
76 extern void zfs_init(void);
77 extern void zfs_fini(void);
79 ldi_ident_t zfs_li = NULL;
82 typedef int zfs_ioc_func_t(zfs_cmd_t *);
83 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
89 } zfs_ioc_namecheck_t;
92 POOL_CHECK_NONE = 1 << 0,
93 POOL_CHECK_SUSPENDED = 1 << 1,
94 POOL_CHECK_READONLY = 1 << 2
95 } zfs_ioc_poolcheck_t;
97 typedef struct zfs_ioc_vec {
98 zfs_ioc_func_t *zvec_func;
99 zfs_secpolicy_func_t *zvec_secpolicy;
100 zfs_ioc_namecheck_t zvec_namecheck;
101 boolean_t zvec_his_log;
102 zfs_ioc_poolcheck_t zvec_pool_check;
105 /* This array is indexed by zfs_userquota_prop_t */
106 static const char *userquota_perms[] = {
107 ZFS_DELEG_PERM_USERUSED,
108 ZFS_DELEG_PERM_USERQUOTA,
109 ZFS_DELEG_PERM_GROUPUSED,
110 ZFS_DELEG_PERM_GROUPQUOTA,
113 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
114 static int zfs_check_settable(const char *name, nvpair_t *property,
116 static int zfs_check_clearable(char *dataset, nvlist_t *props,
118 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
120 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
122 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
124 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
131 * Get rid of annoying "../common/" prefix to filename.
133 newfile = strrchr(file, '/');
134 if (newfile != NULL) {
135 newfile = newfile + 1; /* Get rid of leading / */
141 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
145 * To get this data, use the zfs-dprintf probe as so:
146 * dtrace -q -n 'zfs-dprintf \
147 * /stringof(arg0) == "dbuf.c"/ \
148 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
150 * arg1 = function name
154 DTRACE_PROBE4(zfs__dprintf,
155 char *, newfile, char *, func, int, line, char *, buf);
159 history_str_free(char *buf)
161 kmem_free(buf, HIS_MAX_RECORD_LEN);
165 history_str_get(zfs_cmd_t *zc)
169 if (zc->zc_history == 0)
172 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
173 if (copyinstr((void *)(uintptr_t)zc->zc_history,
174 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
175 history_str_free(buf);
179 buf[HIS_MAX_RECORD_LEN -1] = '\0';
185 * Check to see if the named dataset is currently defined as bootable
188 zfs_is_bootfs(const char *name)
192 if (dmu_objset_hold(name, FTAG, &os) == 0) {
194 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
195 dmu_objset_rele(os, FTAG);
202 * zfs_earlier_version
204 * Return non-zero if the spa version is less than requested version.
207 zfs_earlier_version(const char *name, int version)
211 if (spa_open(name, &spa, FTAG) == 0) {
212 if (spa_version(spa) < version) {
213 spa_close(spa, FTAG);
216 spa_close(spa, FTAG);
222 * zpl_earlier_version
224 * Return TRUE if the ZPL version is less than requested version.
227 zpl_earlier_version(const char *name, int version)
230 boolean_t rc = B_TRUE;
232 if (dmu_objset_hold(name, FTAG, &os) == 0) {
235 if (dmu_objset_type(os) != DMU_OST_ZFS) {
236 dmu_objset_rele(os, FTAG);
239 /* XXX reading from non-owned objset */
240 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
241 rc = zplversion < version;
242 dmu_objset_rele(os, FTAG);
248 zfs_log_history(zfs_cmd_t *zc)
253 if ((buf = history_str_get(zc)) == NULL)
256 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
257 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
258 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
259 spa_close(spa, FTAG);
261 history_str_free(buf);
265 * Policy for top-level read operations (list pools). Requires no privileges,
266 * and can be used in the local zone, as there is no associated dataset.
270 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
276 * Policy for dataset read operations (list children, get statistics). Requires
277 * no privileges, but must be visible in the local zone.
281 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
283 if (INGLOBALZONE(curproc) ||
284 zone_dataset_visible(zc->zc_name, NULL))
291 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
296 * The dataset must be visible by this zone -- check this first
297 * so they don't see EPERM on something they shouldn't know about.
299 if (!INGLOBALZONE(curproc) &&
300 !zone_dataset_visible(dataset, &writable))
303 if (INGLOBALZONE(curproc)) {
305 * If the fs is zoned, only root can access it from the
308 if (secpolicy_zfs(cr) && zoned)
312 * If we are in a local zone, the 'zoned' property must be set.
317 /* must be writable by this zone */
325 zfs_dozonecheck(const char *dataset, cred_t *cr)
329 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
332 return (zfs_dozonecheck_impl(dataset, zoned, cr));
336 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
340 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
341 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
342 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
345 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
347 return (zfs_dozonecheck_impl(dataset, zoned, cr));
351 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
355 error = zfs_dozonecheck(name, cr);
357 error = secpolicy_zfs(cr);
359 error = dsl_deleg_access(name, perm, cr);
365 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
366 const char *perm, cred_t *cr)
370 error = zfs_dozonecheck_ds(name, ds, cr);
372 error = secpolicy_zfs(cr);
374 error = dsl_deleg_access_impl(ds, perm, cr);
380 * Policy for setting the security label property.
382 * Returns 0 for success, non-zero for access and other errors.
385 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
387 char ds_hexsl[MAXNAMELEN];
388 bslabel_t ds_sl, new_sl;
389 boolean_t new_default = FALSE;
391 int needed_priv = -1;
394 /* First get the existing dataset label. */
395 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
396 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
400 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
403 /* The label must be translatable */
404 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
408 * In a non-global zone, disallow attempts to set a label that
409 * doesn't match that of the zone; otherwise no other checks
412 if (!INGLOBALZONE(curproc)) {
413 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
419 * For global-zone datasets (i.e., those whose zoned property is
420 * "off", verify that the specified new label is valid for the
423 if (dsl_prop_get_integer(name,
424 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
427 if (zfs_check_global_label(name, strval) != 0)
432 * If the existing dataset label is nondefault, check if the
433 * dataset is mounted (label cannot be changed while mounted).
434 * Get the zfsvfs; if there isn't one, then the dataset isn't
435 * mounted (or isn't a dataset, doesn't exist, ...).
437 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
439 static char *setsl_tag = "setsl_tag";
442 * Try to own the dataset; abort if there is any error,
443 * (e.g., already mounted, in use, or other error).
445 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
450 dmu_objset_disown(os, setsl_tag);
453 needed_priv = PRIV_FILE_DOWNGRADE_SL;
457 if (hexstr_to_label(strval, &new_sl) != 0)
460 if (blstrictdom(&ds_sl, &new_sl))
461 needed_priv = PRIV_FILE_DOWNGRADE_SL;
462 else if (blstrictdom(&new_sl, &ds_sl))
463 needed_priv = PRIV_FILE_UPGRADE_SL;
465 /* dataset currently has a default label */
467 needed_priv = PRIV_FILE_UPGRADE_SL;
471 if (needed_priv != -1)
472 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
477 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
483 * Check permissions for special properties.
488 * Disallow setting of 'zoned' from within a local zone.
490 if (!INGLOBALZONE(curproc))
495 if (!INGLOBALZONE(curproc)) {
497 char setpoint[MAXNAMELEN];
499 * Unprivileged users are allowed to modify the
500 * quota on things *under* (ie. contained by)
501 * the thing they own.
503 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
506 if (!zoned || strlen(dsname) <= strlen(setpoint))
511 case ZFS_PROP_MLSLABEL:
512 if (!is_system_labeled())
515 if (nvpair_value_string(propval, &strval) == 0) {
518 err = zfs_set_slabel_policy(dsname, strval, CRED());
525 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
529 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
533 error = zfs_dozonecheck(zc->zc_name, cr);
538 * permission to set permissions will be evaluated later in
539 * dsl_deleg_can_allow()
545 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
547 return (zfs_secpolicy_write_perms(zc->zc_name,
548 ZFS_DELEG_PERM_ROLLBACK, cr));
552 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
561 * Generate the current snapshot name from the given objsetid, then
562 * use that name for the secpolicy/zone checks.
564 cp = strchr(zc->zc_name, '@');
567 error = spa_open(zc->zc_name, &spa, FTAG);
571 dp = spa_get_dsl(spa);
572 rw_enter(&dp->dp_config_rwlock, RW_READER);
573 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
574 rw_exit(&dp->dp_config_rwlock);
575 spa_close(spa, FTAG);
579 dsl_dataset_name(ds, zc->zc_name);
581 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
582 ZFS_DELEG_PERM_SEND, cr);
583 dsl_dataset_rele(ds, FTAG);
589 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
594 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
595 NO_FOLLOW, NULL, &vp)) != 0)
598 /* Now make sure mntpnt and dataset are ZFS */
600 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
601 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
602 zc->zc_name) != 0)) {
608 return (dsl_deleg_access(zc->zc_name,
609 ZFS_DELEG_PERM_SHARE, cr));
613 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
615 if (!INGLOBALZONE(curproc))
618 if (secpolicy_nfs(cr) == 0) {
621 return (zfs_secpolicy_deleg_share(zc, cr));
626 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
628 if (!INGLOBALZONE(curproc))
631 if (secpolicy_smb(cr) == 0) {
634 return (zfs_secpolicy_deleg_share(zc, cr));
639 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
644 * Remove the @bla or /bla from the end of the name to get the parent.
646 (void) strncpy(parent, datasetname, parentsize);
647 cp = strrchr(parent, '@');
651 cp = strrchr(parent, '/');
661 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
665 if ((error = zfs_secpolicy_write_perms(name,
666 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
669 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
673 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
675 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
679 * Destroying snapshots with delegated permissions requires
680 * descendent mount and destroy permissions.
681 * Reassemble the full filesystem@snap name so dsl_deleg_access()
682 * can do the correct permission check.
684 * Since this routine is used when doing a recursive destroy of snapshots
685 * and destroying snapshots requires descendent permissions, a successfull
686 * check of the top level snapshot applies to snapshots of all descendent
690 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
695 dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
697 error = zfs_secpolicy_destroy_perms(dsname, cr);
704 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
706 char parentname[MAXNAMELEN];
709 if ((error = zfs_secpolicy_write_perms(from,
710 ZFS_DELEG_PERM_RENAME, cr)) != 0)
713 if ((error = zfs_secpolicy_write_perms(from,
714 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
717 if ((error = zfs_get_parent(to, parentname,
718 sizeof (parentname))) != 0)
721 if ((error = zfs_secpolicy_write_perms(parentname,
722 ZFS_DELEG_PERM_CREATE, cr)) != 0)
725 if ((error = zfs_secpolicy_write_perms(parentname,
726 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
733 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
735 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
739 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
741 char parentname[MAXNAMELEN];
745 error = zfs_secpolicy_write_perms(zc->zc_name,
746 ZFS_DELEG_PERM_PROMOTE, cr);
750 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
753 dsl_dataset_t *pclone = NULL;
755 dd = clone->os_dsl_dataset->ds_dir;
757 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
758 error = dsl_dataset_hold_obj(dd->dd_pool,
759 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
760 rw_exit(&dd->dd_pool->dp_config_rwlock);
762 dmu_objset_rele(clone, FTAG);
766 error = zfs_secpolicy_write_perms(zc->zc_name,
767 ZFS_DELEG_PERM_MOUNT, cr);
769 dsl_dataset_name(pclone, parentname);
770 dmu_objset_rele(clone, FTAG);
771 dsl_dataset_rele(pclone, FTAG);
773 error = zfs_secpolicy_write_perms(parentname,
774 ZFS_DELEG_PERM_PROMOTE, cr);
780 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
784 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
785 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
788 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
789 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
792 return (zfs_secpolicy_write_perms(zc->zc_name,
793 ZFS_DELEG_PERM_CREATE, cr));
797 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
799 return (zfs_secpolicy_write_perms(name,
800 ZFS_DELEG_PERM_SNAPSHOT, cr));
804 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
807 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
811 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
813 char parentname[MAXNAMELEN];
816 if ((error = zfs_get_parent(zc->zc_name, parentname,
817 sizeof (parentname))) != 0)
820 if (zc->zc_value[0] != '\0') {
821 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
822 ZFS_DELEG_PERM_CLONE, cr)) != 0)
826 if ((error = zfs_secpolicy_write_perms(parentname,
827 ZFS_DELEG_PERM_CREATE, cr)) != 0)
830 error = zfs_secpolicy_write_perms(parentname,
831 ZFS_DELEG_PERM_MOUNT, cr);
837 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
841 error = secpolicy_fs_unmount(cr, NULL);
843 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
849 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
850 * SYS_CONFIG privilege, which is not available in a local zone.
854 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
856 if (secpolicy_sys_config(cr, B_FALSE) != 0)
863 * Policy for object to name lookups.
867 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
871 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
874 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
879 * Policy for fault injection. Requires all privileges.
883 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
885 return (secpolicy_zinject(cr));
889 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
891 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
893 if (prop == ZPROP_INVAL) {
894 if (!zfs_prop_user(zc->zc_value))
896 return (zfs_secpolicy_write_perms(zc->zc_name,
897 ZFS_DELEG_PERM_USERPROP, cr));
899 return (zfs_secpolicy_setprop(zc->zc_name, prop,
905 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
907 int err = zfs_secpolicy_read(zc, cr);
911 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
914 if (zc->zc_value[0] == 0) {
916 * They are asking about a posix uid/gid. If it's
917 * themself, allow it.
919 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
920 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
921 if (zc->zc_guid == crgetuid(cr))
924 if (groupmember(zc->zc_guid, cr))
929 return (zfs_secpolicy_write_perms(zc->zc_name,
930 userquota_perms[zc->zc_objset_type], cr));
934 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
936 int err = zfs_secpolicy_read(zc, cr);
940 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
943 return (zfs_secpolicy_write_perms(zc->zc_name,
944 userquota_perms[zc->zc_objset_type], cr));
948 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
950 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
955 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
957 return (zfs_secpolicy_write_perms(zc->zc_name,
958 ZFS_DELEG_PERM_HOLD, cr));
962 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
964 return (zfs_secpolicy_write_perms(zc->zc_name,
965 ZFS_DELEG_PERM_RELEASE, cr));
969 * Policy for allowing temporary snapshots to be taken or released
972 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
975 * A temporary snapshot is the same as a snapshot,
976 * hold, destroy and release all rolled into one.
977 * Delegated diff alone is sufficient that we allow this.
981 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
982 ZFS_DELEG_PERM_DIFF, cr)) == 0)
985 error = zfs_secpolicy_snapshot(zc, cr);
987 error = zfs_secpolicy_hold(zc, cr);
989 error = zfs_secpolicy_release(zc, cr);
991 error = zfs_secpolicy_destroy(zc, cr);
996 * Returns the nvlist as specified by the user in the zfs_cmd_t.
999 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1003 nvlist_t *list = NULL;
1006 * Read in and unpack the user-supplied nvlist.
1011 packed = kmem_alloc(size, KM_SLEEP);
1013 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1015 kmem_free(packed, size);
1019 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1020 kmem_free(packed, size);
1024 kmem_free(packed, size);
1031 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1035 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1037 if (size > zc->zc_nvlist_dst_size) {
1038 nvpair_t *more_errors;
1041 if (zc->zc_nvlist_dst_size < 1024)
1044 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1045 more_errors = nvlist_prev_nvpair(*errors, NULL);
1048 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1050 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1052 VERIFY(nvlist_size(*errors, &size,
1053 NV_ENCODE_NATIVE) == 0);
1054 } while (size > zc->zc_nvlist_dst_size);
1056 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1057 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1058 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1059 ASSERT(size <= zc->zc_nvlist_dst_size);
1066 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1068 char *packed = NULL;
1072 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1074 if (size > zc->zc_nvlist_dst_size) {
1077 packed = kmem_alloc(size, KM_SLEEP);
1078 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1080 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1081 size, zc->zc_iflags) != 0)
1083 kmem_free(packed, size);
1086 zc->zc_nvlist_dst_size = size;
1091 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1096 error = dmu_objset_hold(dsname, FTAG, &os);
1099 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1100 dmu_objset_rele(os, FTAG);
1104 mutex_enter(&os->os_user_ptr_lock);
1105 *zfvp = dmu_objset_get_user(os);
1107 VFS_HOLD((*zfvp)->z_vfs);
1111 mutex_exit(&os->os_user_ptr_lock);
1112 dmu_objset_rele(os, FTAG);
1117 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1118 * case its z_vfs will be NULL, and it will be opened as the owner.
1121 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1125 if (getzfsvfs(name, zfvp) != 0)
1126 error = zfsvfs_create(name, zfvp);
1128 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1130 if ((*zfvp)->z_unmounted) {
1132 * XXX we could probably try again, since the unmounting
1133 * thread should be just about to disassociate the
1134 * objset from the zfsvfs.
1136 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1144 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1146 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1148 if (zfsvfs->z_vfs) {
1149 VFS_RELE(zfsvfs->z_vfs);
1151 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1152 zfsvfs_free(zfsvfs);
1157 zfs_ioc_pool_create(zfs_cmd_t *zc)
1160 nvlist_t *config, *props = NULL;
1161 nvlist_t *rootprops = NULL;
1162 nvlist_t *zplprops = NULL;
1165 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1166 zc->zc_iflags, &config))
1169 if (zc->zc_nvlist_src_size != 0 && (error =
1170 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1171 zc->zc_iflags, &props))) {
1172 nvlist_free(config);
1177 nvlist_t *nvl = NULL;
1178 uint64_t version = SPA_VERSION;
1180 (void) nvlist_lookup_uint64(props,
1181 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1182 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1184 goto pool_props_bad;
1186 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1188 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1190 nvlist_free(config);
1194 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1196 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1197 error = zfs_fill_zplprops_root(version, rootprops,
1200 goto pool_props_bad;
1203 buf = history_str_get(zc);
1205 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1208 * Set the remaining root properties
1210 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1211 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1212 (void) spa_destroy(zc->zc_name);
1215 history_str_free(buf);
1218 nvlist_free(rootprops);
1219 nvlist_free(zplprops);
1220 nvlist_free(config);
1227 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1230 zfs_log_history(zc);
1231 error = spa_destroy(zc->zc_name);
1233 zvol_remove_minors(zc->zc_name);
1238 zfs_ioc_pool_import(zfs_cmd_t *zc)
1240 nvlist_t *config, *props = NULL;
1244 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1245 zc->zc_iflags, &config)) != 0)
1248 if (zc->zc_nvlist_src_size != 0 && (error =
1249 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1250 zc->zc_iflags, &props))) {
1251 nvlist_free(config);
1255 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1256 guid != zc->zc_guid)
1259 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1261 if (zc->zc_nvlist_dst != 0) {
1264 if ((err = put_nvlist(zc, config)) != 0)
1268 nvlist_free(config);
1277 zfs_ioc_pool_export(zfs_cmd_t *zc)
1280 boolean_t force = (boolean_t)zc->zc_cookie;
1281 boolean_t hardforce = (boolean_t)zc->zc_guid;
1283 zfs_log_history(zc);
1284 error = spa_export(zc->zc_name, NULL, force, hardforce);
1286 zvol_remove_minors(zc->zc_name);
1291 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1296 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1299 error = put_nvlist(zc, configs);
1301 nvlist_free(configs);
1307 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1313 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1314 sizeof (zc->zc_value));
1316 if (config != NULL) {
1317 ret = put_nvlist(zc, config);
1318 nvlist_free(config);
1321 * The config may be present even if 'error' is non-zero.
1322 * In this case we return success, and preserve the real errno
1325 zc->zc_cookie = error;
1334 * Try to import the given pool, returning pool stats as appropriate so that
1335 * user land knows which devices are available and overall pool health.
1338 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1340 nvlist_t *tryconfig, *config;
1343 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1344 zc->zc_iflags, &tryconfig)) != 0)
1347 config = spa_tryimport(tryconfig);
1349 nvlist_free(tryconfig);
1354 error = put_nvlist(zc, config);
1355 nvlist_free(config);
1362 * zc_name name of the pool
1363 * zc_cookie scan func (pool_scan_func_t)
1366 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1371 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1374 if (zc->zc_cookie == POOL_SCAN_NONE)
1375 error = spa_scan_stop(spa);
1377 error = spa_scan(spa, zc->zc_cookie);
1379 spa_close(spa, FTAG);
1385 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1390 error = spa_open(zc->zc_name, &spa, FTAG);
1393 spa_close(spa, FTAG);
1399 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1404 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1407 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1408 spa_close(spa, FTAG);
1412 spa_upgrade(spa, zc->zc_cookie);
1413 spa_close(spa, FTAG);
1419 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1426 if ((size = zc->zc_history_len) == 0)
1429 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1432 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1433 spa_close(spa, FTAG);
1437 hist_buf = kmem_alloc(size, KM_SLEEP);
1438 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1439 &zc->zc_history_len, hist_buf)) == 0) {
1440 error = ddi_copyout(hist_buf,
1441 (void *)(uintptr_t)zc->zc_history,
1442 zc->zc_history_len, zc->zc_iflags);
1445 spa_close(spa, FTAG);
1446 kmem_free(hist_buf, size);
1451 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1455 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1463 * zc_name name of filesystem
1464 * zc_obj object to find
1467 * zc_value name of object
1470 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1475 /* XXX reading from objset not owned */
1476 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1478 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1479 dmu_objset_rele(os, FTAG);
1482 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1483 sizeof (zc->zc_value));
1484 dmu_objset_rele(os, FTAG);
1491 * zc_name name of filesystem
1492 * zc_obj object to find
1495 * zc_stat stats on object
1496 * zc_value path to object
1499 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1504 /* XXX reading from objset not owned */
1505 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1507 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1508 dmu_objset_rele(os, FTAG);
1511 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1512 sizeof (zc->zc_value));
1513 dmu_objset_rele(os, FTAG);
1519 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1523 nvlist_t *config, **l2cache, **spares;
1524 uint_t nl2cache = 0, nspares = 0;
1526 error = spa_open(zc->zc_name, &spa, FTAG);
1530 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1531 zc->zc_iflags, &config);
1532 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1533 &l2cache, &nl2cache);
1535 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1539 * A root pool with concatenated devices is not supported.
1540 * Thus, can not add a device to a root pool.
1542 * Intent log device can not be added to a rootpool because
1543 * during mountroot, zil is replayed, a seperated log device
1544 * can not be accessed during the mountroot time.
1546 * l2cache and spare devices are ok to be added to a rootpool.
1548 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1549 nvlist_free(config);
1550 spa_close(spa, FTAG);
1555 error = spa_vdev_add(spa, config);
1556 nvlist_free(config);
1558 spa_close(spa, FTAG);
1564 * zc_name name of the pool
1565 * zc_nvlist_conf nvlist of devices to remove
1566 * zc_cookie to stop the remove?
1569 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1574 error = spa_open(zc->zc_name, &spa, FTAG);
1577 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1578 spa_close(spa, FTAG);
1583 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1587 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1589 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1591 switch (zc->zc_cookie) {
1592 case VDEV_STATE_ONLINE:
1593 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1596 case VDEV_STATE_OFFLINE:
1597 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1600 case VDEV_STATE_FAULTED:
1601 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1602 zc->zc_obj != VDEV_AUX_EXTERNAL)
1603 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1605 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1608 case VDEV_STATE_DEGRADED:
1609 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1610 zc->zc_obj != VDEV_AUX_EXTERNAL)
1611 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1613 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1619 zc->zc_cookie = newstate;
1620 spa_close(spa, FTAG);
1625 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1628 int replacing = zc->zc_cookie;
1632 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1635 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1636 zc->zc_iflags, &config)) == 0) {
1637 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1638 nvlist_free(config);
1641 spa_close(spa, FTAG);
1646 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1651 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1654 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1656 spa_close(spa, FTAG);
1661 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1664 nvlist_t *config, *props = NULL;
1666 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1668 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1671 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1672 zc->zc_iflags, &config)) {
1673 spa_close(spa, FTAG);
1677 if (zc->zc_nvlist_src_size != 0 && (error =
1678 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1679 zc->zc_iflags, &props))) {
1680 spa_close(spa, FTAG);
1681 nvlist_free(config);
1685 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1687 spa_close(spa, FTAG);
1689 nvlist_free(config);
1696 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1699 char *path = zc->zc_value;
1700 uint64_t guid = zc->zc_guid;
1703 error = spa_open(zc->zc_name, &spa, FTAG);
1707 error = spa_vdev_setpath(spa, guid, path);
1708 spa_close(spa, FTAG);
1713 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1716 char *fru = zc->zc_value;
1717 uint64_t guid = zc->zc_guid;
1720 error = spa_open(zc->zc_name, &spa, FTAG);
1724 error = spa_vdev_setfru(spa, guid, fru);
1725 spa_close(spa, FTAG);
1730 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1735 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1737 if (zc->zc_nvlist_dst != 0 &&
1738 (error = dsl_prop_get_all(os, &nv)) == 0) {
1739 dmu_objset_stats(os, nv);
1741 * NB: zvol_get_stats() will read the objset contents,
1742 * which we aren't supposed to do with a
1743 * DS_MODE_USER hold, because it could be
1744 * inconsistent. So this is a bit of a workaround...
1745 * XXX reading with out owning
1747 if (!zc->zc_objset_stats.dds_inconsistent) {
1748 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1749 VERIFY(zvol_get_stats(os, nv) == 0);
1751 error = put_nvlist(zc, nv);
1760 * zc_name name of filesystem
1761 * zc_nvlist_dst_size size of buffer for property nvlist
1764 * zc_objset_stats stats
1765 * zc_nvlist_dst property nvlist
1766 * zc_nvlist_dst_size size of property nvlist
1769 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1771 objset_t *os = NULL;
1774 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1777 error = zfs_ioc_objset_stats_impl(zc, os);
1779 dmu_objset_rele(os, FTAG);
1786 * zc_name name of filesystem
1787 * zc_nvlist_dst_size size of buffer for property nvlist
1790 * zc_nvlist_dst received property nvlist
1791 * zc_nvlist_dst_size size of received property nvlist
1793 * Gets received properties (distinct from local properties on or after
1794 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1795 * local property values.
1798 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1800 objset_t *os = NULL;
1804 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1808 * Without this check, we would return local property values if the
1809 * caller has not already received properties on or after
1810 * SPA_VERSION_RECVD_PROPS.
1812 if (!dsl_prop_get_hasrecvd(os)) {
1813 dmu_objset_rele(os, FTAG);
1817 if (zc->zc_nvlist_dst != 0 &&
1818 (error = dsl_prop_get_received(os, &nv)) == 0) {
1819 error = put_nvlist(zc, nv);
1823 dmu_objset_rele(os, FTAG);
1828 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1834 * zfs_get_zplprop() will either find a value or give us
1835 * the default value (if there is one).
1837 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1839 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1845 * zc_name name of filesystem
1846 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1849 * zc_nvlist_dst zpl property nvlist
1850 * zc_nvlist_dst_size size of zpl property nvlist
1853 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1858 /* XXX reading without owning */
1859 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1862 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1865 * NB: nvl_add_zplprop() will read the objset contents,
1866 * which we aren't supposed to do with a DS_MODE_USER
1867 * hold, because it could be inconsistent.
1869 if (zc->zc_nvlist_dst != 0 &&
1870 !zc->zc_objset_stats.dds_inconsistent &&
1871 dmu_objset_type(os) == DMU_OST_ZFS) {
1874 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1875 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1876 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1877 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1878 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1879 err = put_nvlist(zc, nv);
1884 dmu_objset_rele(os, FTAG);
1889 dataset_name_hidden(const char *name)
1892 * Skip over datasets that are not visible in this zone,
1893 * internal datasets (which have a $ in their name), and
1894 * temporary datasets (which have a % in their name).
1896 if (strchr(name, '$') != NULL)
1898 if (strchr(name, '%') != NULL)
1900 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1907 * zc_name name of filesystem
1908 * zc_cookie zap cursor
1909 * zc_nvlist_dst_size size of buffer for property nvlist
1912 * zc_name name of next filesystem
1913 * zc_cookie zap cursor
1914 * zc_objset_stats stats
1915 * zc_nvlist_dst property nvlist
1916 * zc_nvlist_dst_size size of property nvlist
1919 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1924 size_t orig_len = strlen(zc->zc_name);
1927 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
1928 if (error == ENOENT)
1933 p = strrchr(zc->zc_name, '/');
1934 if (p == NULL || p[1] != '\0')
1935 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1936 p = zc->zc_name + strlen(zc->zc_name);
1939 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1940 * but is not declared void because its called by dmu_objset_find().
1942 if (zc->zc_cookie == 0) {
1943 uint64_t cookie = 0;
1944 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1946 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1947 (void) dmu_objset_prefetch(p, NULL);
1951 error = dmu_dir_list_next(os,
1952 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1953 NULL, &zc->zc_cookie);
1954 if (error == ENOENT)
1956 } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1957 !(zc->zc_iflags & FKIOCTL));
1958 dmu_objset_rele(os, FTAG);
1961 * If it's an internal dataset (ie. with a '$' in its name),
1962 * don't try to get stats for it, otherwise we'll return ENOENT.
1964 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1965 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1966 if (error == ENOENT) {
1967 /* We lost a race with destroy, get the next one. */
1968 zc->zc_name[orig_len] = '\0';
1977 * zc_name name of filesystem
1978 * zc_cookie zap cursor
1979 * zc_nvlist_dst_size size of buffer for property nvlist
1982 * zc_name name of next snapshot
1983 * zc_objset_stats stats
1984 * zc_nvlist_dst property nvlist
1985 * zc_nvlist_dst_size size of property nvlist
1988 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1994 if (zc->zc_cookie == 0)
1995 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1996 NULL, DS_FIND_SNAPSHOTS);
1998 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2000 return (error == ENOENT ? ESRCH : error);
2003 * A dataset name of maximum length cannot have any snapshots,
2004 * so exit immediately.
2006 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2007 dmu_objset_rele(os, FTAG);
2011 error = dmu_snapshot_list_next(os,
2012 sizeof (zc->zc_name) - strlen(zc->zc_name),
2013 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2018 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2021 * Since we probably don't have a hold on this snapshot,
2022 * it's possible that the objsetid could have been destroyed
2023 * and reused for a new objset. It's OK if this happens during
2024 * a zfs send operation, since the new createtxg will be
2025 * beyond the range we're interested in.
2027 rw_enter(&dp->dp_config_rwlock, RW_READER);
2028 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2029 rw_exit(&dp->dp_config_rwlock);
2031 if (error == ENOENT) {
2032 /* Racing with destroy, get the next one. */
2033 *strchr(zc->zc_name, '@') = '\0';
2034 dmu_objset_rele(os, FTAG);
2040 error = dmu_objset_from_ds(ds, &ossnap);
2042 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2043 dsl_dataset_rele(ds, FTAG);
2045 } else if (error == ENOENT) {
2049 dmu_objset_rele(os, FTAG);
2050 /* if we failed, undo the @ that we tacked on to zc_name */
2052 *strchr(zc->zc_name, '@') = '\0';
2057 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2059 const char *propname = nvpair_name(pair);
2061 unsigned int vallen;
2064 zfs_userquota_prop_t type;
2070 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2072 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2073 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2079 * A correctly constructed propname is encoded as
2080 * userquota@<rid>-<domain>.
2082 if ((dash = strchr(propname, '-')) == NULL ||
2083 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2092 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2094 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2095 zfsvfs_rele(zfsvfs, FTAG);
2102 * If the named property is one that has a special function to set its value,
2103 * return 0 on success and a positive error code on failure; otherwise if it is
2104 * not one of the special properties handled by this function, return -1.
2106 * XXX: It would be better for callers of the property interface if we handled
2107 * these special cases in dsl_prop.c (in the dsl layer).
2110 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2113 const char *propname = nvpair_name(pair);
2114 zfs_prop_t prop = zfs_name_to_prop(propname);
2118 if (prop == ZPROP_INVAL) {
2119 if (zfs_prop_userquota(propname))
2120 return (zfs_prop_set_userquota(dsname, pair));
2124 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2126 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2127 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2131 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2134 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2137 case ZFS_PROP_QUOTA:
2138 err = dsl_dir_set_quota(dsname, source, intval);
2140 case ZFS_PROP_REFQUOTA:
2141 err = dsl_dataset_set_quota(dsname, source, intval);
2143 case ZFS_PROP_RESERVATION:
2144 err = dsl_dir_set_reservation(dsname, source, intval);
2146 case ZFS_PROP_REFRESERVATION:
2147 err = dsl_dataset_set_reservation(dsname, source, intval);
2149 case ZFS_PROP_VOLSIZE:
2150 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2153 case ZFS_PROP_VERSION:
2157 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2160 err = zfs_set_version(zfsvfs, intval);
2161 zfsvfs_rele(zfsvfs, FTAG);
2163 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2166 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2167 (void) strcpy(zc->zc_name, dsname);
2168 (void) zfs_ioc_userspace_upgrade(zc);
2169 kmem_free(zc, sizeof (zfs_cmd_t));
2182 * This function is best effort. If it fails to set any of the given properties,
2183 * it continues to set as many as it can and returns the first error
2184 * encountered. If the caller provides a non-NULL errlist, it also gives the
2185 * complete list of names of all the properties it failed to set along with the
2186 * corresponding error numbers. The caller is responsible for freeing the
2189 * If every property is set successfully, zero is returned and the list pointed
2190 * at by errlist is NULL.
2193 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2201 nvlist_t *genericnvl;
2205 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2206 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2207 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2211 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2212 const char *propname = nvpair_name(pair);
2213 zfs_prop_t prop = zfs_name_to_prop(propname);
2216 /* decode the property value */
2218 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2220 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2221 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2226 /* Validate value type */
2227 if (err == 0 && prop == ZPROP_INVAL) {
2228 if (zfs_prop_user(propname)) {
2229 if (nvpair_type(propval) != DATA_TYPE_STRING)
2231 } else if (zfs_prop_userquota(propname)) {
2232 if (nvpair_type(propval) !=
2233 DATA_TYPE_UINT64_ARRAY)
2236 } else if (err == 0) {
2237 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2238 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2240 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2243 VERIFY(nvpair_value_uint64(propval,
2246 switch (zfs_prop_get_type(prop)) {
2247 case PROP_TYPE_NUMBER:
2249 case PROP_TYPE_STRING:
2252 case PROP_TYPE_INDEX:
2253 if (zfs_prop_index_to_string(prop,
2254 intval, &unused) != 0)
2259 "unknown property type");
2266 /* Validate permissions */
2268 err = zfs_check_settable(dsname, pair, CRED());
2271 err = zfs_prop_set_special(dsname, source, pair);
2274 * For better performance we build up a list of
2275 * properties to set in a single transaction.
2277 err = nvlist_add_nvpair(genericnvl, pair);
2278 } else if (err != 0 && nvl != retrynvl) {
2280 * This may be a spurious error caused by
2281 * receiving quota and reservation out of order.
2282 * Try again in a second pass.
2284 err = nvlist_add_nvpair(retrynvl, pair);
2289 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2292 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2297 if (!nvlist_empty(genericnvl) &&
2298 dsl_props_set(dsname, source, genericnvl) != 0) {
2300 * If this fails, we still want to set as many properties as we
2301 * can, so try setting them individually.
2304 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2305 const char *propname = nvpair_name(pair);
2309 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2311 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2312 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2316 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2317 VERIFY(nvpair_value_string(propval,
2319 err = dsl_prop_set(dsname, propname, source, 1,
2320 strlen(strval) + 1, strval);
2322 VERIFY(nvpair_value_uint64(propval,
2324 err = dsl_prop_set(dsname, propname, source, 8,
2329 VERIFY(nvlist_add_int32(errors, propname,
2334 nvlist_free(genericnvl);
2335 nvlist_free(retrynvl);
2337 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2338 nvlist_free(errors);
2341 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2344 if (errlist == NULL)
2345 nvlist_free(errors);
2353 * Check that all the properties are valid user properties.
2356 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2358 nvpair_t *pair = NULL;
2361 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2362 const char *propname = nvpair_name(pair);
2365 if (!zfs_prop_user(propname) ||
2366 nvpair_type(pair) != DATA_TYPE_STRING)
2369 if (error = zfs_secpolicy_write_perms(fsname,
2370 ZFS_DELEG_PERM_USERPROP, CRED()))
2373 if (strlen(propname) >= ZAP_MAXNAMELEN)
2374 return (ENAMETOOLONG);
2376 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2377 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2384 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2388 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2391 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2392 if (nvlist_exists(skipped, nvpair_name(pair)))
2395 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2400 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2404 nvlist_t *cleared_props = NULL;
2405 props_skip(props, skipped, &cleared_props);
2406 if (!nvlist_empty(cleared_props)) {
2408 * Acts on local properties until the dataset has received
2409 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2411 zprop_source_t flags = (ZPROP_SRC_NONE |
2412 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2413 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2415 nvlist_free(cleared_props);
2421 * zc_name name of filesystem
2422 * zc_value name of property to set
2423 * zc_nvlist_src{_size} nvlist of properties to apply
2424 * zc_cookie received properties flag
2427 * zc_nvlist_dst{_size} error for each unapplied received property
2430 zfs_ioc_set_prop(zfs_cmd_t *zc)
2433 boolean_t received = zc->zc_cookie;
2434 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2436 nvlist_t *errors = NULL;
2439 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2440 zc->zc_iflags, &nvl)) != 0)
2444 nvlist_t *origprops;
2447 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2448 if (dsl_prop_get_received(os, &origprops) == 0) {
2449 (void) clear_received_props(os,
2450 zc->zc_name, origprops, nvl);
2451 nvlist_free(origprops);
2454 dsl_prop_set_hasrecvd(os);
2455 dmu_objset_rele(os, FTAG);
2459 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2461 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2462 (void) put_nvlist(zc, errors);
2465 nvlist_free(errors);
2472 * zc_name name of filesystem
2473 * zc_value name of property to inherit
2474 * zc_cookie revert to received value if TRUE
2479 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2481 const char *propname = zc->zc_value;
2482 zfs_prop_t prop = zfs_name_to_prop(propname);
2483 boolean_t received = zc->zc_cookie;
2484 zprop_source_t source = (received
2485 ? ZPROP_SRC_NONE /* revert to received value, if any */
2486 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2495 * zfs_prop_set_special() expects properties in the form of an
2496 * nvpair with type info.
2498 if (prop == ZPROP_INVAL) {
2499 if (!zfs_prop_user(propname))
2502 type = PROP_TYPE_STRING;
2503 } else if (prop == ZFS_PROP_VOLSIZE ||
2504 prop == ZFS_PROP_VERSION) {
2507 type = zfs_prop_get_type(prop);
2510 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2513 case PROP_TYPE_STRING:
2514 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2516 case PROP_TYPE_NUMBER:
2517 case PROP_TYPE_INDEX:
2518 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2525 pair = nvlist_next_nvpair(dummy, NULL);
2526 err = zfs_prop_set_special(zc->zc_name, source, pair);
2529 return (err); /* special property already handled */
2532 * Only check this in the non-received case. We want to allow
2533 * 'inherit -S' to revert non-inheritable properties like quota
2534 * and reservation to the received or default values even though
2535 * they are not considered inheritable.
2537 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2541 /* the property name has been validated by zfs_secpolicy_inherit() */
2542 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2546 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2553 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2554 zc->zc_iflags, &props))
2558 * If the only property is the configfile, then just do a spa_lookup()
2559 * to handle the faulted case.
2561 pair = nvlist_next_nvpair(props, NULL);
2562 if (pair != NULL && strcmp(nvpair_name(pair),
2563 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2564 nvlist_next_nvpair(props, pair) == NULL) {
2565 mutex_enter(&spa_namespace_lock);
2566 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2567 spa_configfile_set(spa, props, B_FALSE);
2568 spa_config_sync(spa, B_FALSE, B_TRUE);
2570 mutex_exit(&spa_namespace_lock);
2577 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2582 error = spa_prop_set(spa, props);
2585 spa_close(spa, FTAG);
2591 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2595 nvlist_t *nvp = NULL;
2597 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2599 * If the pool is faulted, there may be properties we can still
2600 * get (such as altroot and cachefile), so attempt to get them
2603 mutex_enter(&spa_namespace_lock);
2604 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2605 error = spa_prop_get(spa, &nvp);
2606 mutex_exit(&spa_namespace_lock);
2608 error = spa_prop_get(spa, &nvp);
2609 spa_close(spa, FTAG);
2612 if (error == 0 && zc->zc_nvlist_dst != 0)
2613 error = put_nvlist(zc, nvp);
2623 * zc_name name of filesystem
2624 * zc_nvlist_src{_size} nvlist of delegated permissions
2625 * zc_perm_action allow/unallow flag
2630 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2633 nvlist_t *fsaclnv = NULL;
2635 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2636 zc->zc_iflags, &fsaclnv)) != 0)
2640 * Verify nvlist is constructed correctly
2642 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2643 nvlist_free(fsaclnv);
2648 * If we don't have PRIV_SYS_MOUNT, then validate
2649 * that user is allowed to hand out each permission in
2653 error = secpolicy_zfs(CRED());
2655 if (zc->zc_perm_action == B_FALSE) {
2656 error = dsl_deleg_can_allow(zc->zc_name,
2659 error = dsl_deleg_can_unallow(zc->zc_name,
2665 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2667 nvlist_free(fsaclnv);
2673 * zc_name name of filesystem
2676 * zc_nvlist_src{_size} nvlist of delegated permissions
2679 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2684 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2685 error = put_nvlist(zc, nvp);
2693 * Search the vfs list for a specified resource. Returns a pointer to it
2694 * or NULL if no suitable entry is found. The caller of this routine
2695 * is responsible for releasing the returned vfs pointer.
2698 zfs_get_vfs(const char *resource)
2701 struct vfs *vfs_found = NULL;
2703 vfs_list_read_lock();
2706 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2711 vfsp = vfsp->vfs_next;
2712 } while (vfsp != rootvfs);
2719 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2721 zfs_creat_t *zct = arg;
2723 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2726 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2730 * createprops list of properties requested by creator
2731 * default_zplver zpl version to use if unspecified in createprops
2732 * fuids_ok fuids allowed in this version of the spa?
2733 * os parent objset pointer (NULL if root fs)
2736 * zplprops values for the zplprops we attach to the master node object
2737 * is_ci true if requested file system will be purely case-insensitive
2739 * Determine the settings for utf8only, normalization and
2740 * casesensitivity. Specific values may have been requested by the
2741 * creator and/or we can inherit values from the parent dataset. If
2742 * the file system is of too early a vintage, a creator can not
2743 * request settings for these properties, even if the requested
2744 * setting is the default value. We don't actually want to create dsl
2745 * properties for these, so remove them from the source nvlist after
2749 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2750 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2751 nvlist_t *zplprops, boolean_t *is_ci)
2753 uint64_t sense = ZFS_PROP_UNDEFINED;
2754 uint64_t norm = ZFS_PROP_UNDEFINED;
2755 uint64_t u8 = ZFS_PROP_UNDEFINED;
2757 ASSERT(zplprops != NULL);
2760 * Pull out creator prop choices, if any.
2763 (void) nvlist_lookup_uint64(createprops,
2764 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2765 (void) nvlist_lookup_uint64(createprops,
2766 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2767 (void) nvlist_remove_all(createprops,
2768 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2769 (void) nvlist_lookup_uint64(createprops,
2770 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2771 (void) nvlist_remove_all(createprops,
2772 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2773 (void) nvlist_lookup_uint64(createprops,
2774 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2775 (void) nvlist_remove_all(createprops,
2776 zfs_prop_to_name(ZFS_PROP_CASE));
2780 * If the zpl version requested is whacky or the file system
2781 * or pool is version is too "young" to support normalization
2782 * and the creator tried to set a value for one of the props,
2785 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2786 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2787 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2788 (zplver < ZPL_VERSION_NORMALIZATION &&
2789 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2790 sense != ZFS_PROP_UNDEFINED)))
2794 * Put the version in the zplprops
2796 VERIFY(nvlist_add_uint64(zplprops,
2797 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2799 if (norm == ZFS_PROP_UNDEFINED)
2800 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2801 VERIFY(nvlist_add_uint64(zplprops,
2802 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2805 * If we're normalizing, names must always be valid UTF-8 strings.
2809 if (u8 == ZFS_PROP_UNDEFINED)
2810 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2811 VERIFY(nvlist_add_uint64(zplprops,
2812 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2814 if (sense == ZFS_PROP_UNDEFINED)
2815 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2816 VERIFY(nvlist_add_uint64(zplprops,
2817 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2820 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2826 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2827 nvlist_t *zplprops, boolean_t *is_ci)
2829 boolean_t fuids_ok, sa_ok;
2830 uint64_t zplver = ZPL_VERSION;
2831 objset_t *os = NULL;
2832 char parentname[MAXNAMELEN];
2838 (void) strlcpy(parentname, dataset, sizeof (parentname));
2839 cp = strrchr(parentname, '/');
2843 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2846 spa_vers = spa_version(spa);
2847 spa_close(spa, FTAG);
2849 zplver = zfs_zpl_version_map(spa_vers);
2850 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2851 sa_ok = (zplver >= ZPL_VERSION_SA);
2854 * Open parent object set so we can inherit zplprop values.
2856 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2859 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2861 dmu_objset_rele(os, FTAG);
2866 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2867 nvlist_t *zplprops, boolean_t *is_ci)
2871 uint64_t zplver = ZPL_VERSION;
2874 zplver = zfs_zpl_version_map(spa_vers);
2875 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2876 sa_ok = (zplver >= ZPL_VERSION_SA);
2878 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2879 createprops, zplprops, is_ci);
2885 * zc_objset_type type of objset to create (fs vs zvol)
2886 * zc_name name of new objset
2887 * zc_value name of snapshot to clone from (may be empty)
2888 * zc_nvlist_src{_size} nvlist of properties to apply
2893 zfs_ioc_create(zfs_cmd_t *zc)
2898 nvlist_t *nvprops = NULL;
2899 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2900 dmu_objset_type_t type = zc->zc_objset_type;
2905 cbfunc = zfs_create_cb;
2909 cbfunc = zvol_create_cb;
2916 if (strchr(zc->zc_name, '@') ||
2917 strchr(zc->zc_name, '%'))
2920 if (zc->zc_nvlist_src != 0 &&
2921 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2922 zc->zc_iflags, &nvprops)) != 0)
2925 zct.zct_zplprops = NULL;
2926 zct.zct_props = nvprops;
2928 if (zc->zc_value[0] != '\0') {
2930 * We're creating a clone of an existing snapshot.
2932 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2933 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2934 nvlist_free(nvprops);
2938 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2940 nvlist_free(nvprops);
2944 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2945 dmu_objset_rele(clone, FTAG);
2947 nvlist_free(nvprops);
2951 boolean_t is_insensitive = B_FALSE;
2953 if (cbfunc == NULL) {
2954 nvlist_free(nvprops);
2958 if (type == DMU_OST_ZVOL) {
2959 uint64_t volsize, volblocksize;
2961 if (nvprops == NULL ||
2962 nvlist_lookup_uint64(nvprops,
2963 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2965 nvlist_free(nvprops);
2969 if ((error = nvlist_lookup_uint64(nvprops,
2970 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2971 &volblocksize)) != 0 && error != ENOENT) {
2972 nvlist_free(nvprops);
2977 volblocksize = zfs_prop_default_numeric(
2978 ZFS_PROP_VOLBLOCKSIZE);
2980 if ((error = zvol_check_volblocksize(
2981 volblocksize)) != 0 ||
2982 (error = zvol_check_volsize(volsize,
2983 volblocksize)) != 0) {
2984 nvlist_free(nvprops);
2987 } else if (type == DMU_OST_ZFS) {
2991 * We have to have normalization and
2992 * case-folding flags correct when we do the
2993 * file system creation, so go figure them out
2996 VERIFY(nvlist_alloc(&zct.zct_zplprops,
2997 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2998 error = zfs_fill_zplprops(zc->zc_name, nvprops,
2999 zct.zct_zplprops, &is_insensitive);
3001 nvlist_free(nvprops);
3002 nvlist_free(zct.zct_zplprops);
3006 error = dmu_objset_create(zc->zc_name, type,
3007 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3008 nvlist_free(zct.zct_zplprops);
3012 * It would be nice to do this atomically.
3015 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3018 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3020 nvlist_free(nvprops);
3026 * zc_name name of filesystem
3027 * zc_value short name of snapshot
3028 * zc_cookie recursive flag
3029 * zc_nvlist_src[_size] property list
3032 * zc_value short snapname (i.e. part after the '@')
3035 zfs_ioc_snapshot(zfs_cmd_t *zc)
3037 nvlist_t *nvprops = NULL;
3039 boolean_t recursive = zc->zc_cookie;
3041 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3044 if (zc->zc_nvlist_src != 0 &&
3045 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3046 zc->zc_iflags, &nvprops)) != 0)
3049 error = zfs_check_userprops(zc->zc_name, nvprops);
3053 if (!nvlist_empty(nvprops) &&
3054 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3059 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3060 nvprops, recursive, B_FALSE, -1);
3063 nvlist_free(nvprops);
3068 zfs_unmount_snap(const char *name, void *arg)
3073 char *snapname = arg;
3074 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3075 vfsp = zfs_get_vfs(fullname);
3077 } else if (strchr(name, '@')) {
3078 vfsp = zfs_get_vfs(name);
3083 * Always force the unmount for snapshots.
3085 int flag = MS_FORCE;
3088 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3093 if ((err = dounmount(vfsp, flag, kcred)) != 0)
3101 * zc_name name of filesystem
3102 * zc_value short name of snapshot
3103 * zc_defer_destroy mark for deferred destroy
3108 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3112 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3114 err = dmu_objset_find(zc->zc_name,
3115 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3118 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3119 zc->zc_defer_destroy));
3124 * zc_name name of dataset to destroy
3125 * zc_objset_type type of objset
3126 * zc_defer_destroy mark for deferred destroy
3131 zfs_ioc_destroy(zfs_cmd_t *zc)
3134 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3135 err = zfs_unmount_snap(zc->zc_name, NULL);
3140 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3141 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3142 (void) zvol_remove_minor(zc->zc_name);
3148 * zc_name name of dataset to rollback (to most recent snapshot)
3153 zfs_ioc_rollback(zfs_cmd_t *zc)
3155 dsl_dataset_t *ds, *clone;
3160 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3164 /* must not be a snapshot */
3165 if (dsl_dataset_is_snapshot(ds)) {
3166 dsl_dataset_rele(ds, FTAG);
3170 /* must have a most recent snapshot */
3171 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3172 dsl_dataset_rele(ds, FTAG);
3177 * Create clone of most recent snapshot.
3179 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3180 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3184 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3191 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3192 error = zfs_suspend_fs(zfsvfs);
3196 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3197 error = dsl_dataset_clone_swap(clone, ds,
3199 dsl_dataset_disown(ds, FTAG);
3204 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3205 error = error ? error : resume_err;
3207 VFS_RELE(zfsvfs->z_vfs);
3209 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3210 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3211 dsl_dataset_disown(ds, FTAG);
3219 * Destroy clone (which also closes it).
3221 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3224 strfree(clone_name);
3226 dsl_dataset_rele(ds, FTAG);
3232 * zc_name old name of dataset
3233 * zc_value new name of dataset
3234 * zc_cookie recursive flag (only valid for snapshots)
3239 zfs_ioc_rename(zfs_cmd_t *zc)
3241 boolean_t recursive = zc->zc_cookie & 1;
3243 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3244 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3245 strchr(zc->zc_value, '%'))
3249 * Unmount snapshot unless we're doing a recursive rename,
3250 * in which case the dataset code figures out which snapshots
3253 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3254 zc->zc_objset_type == DMU_OST_ZFS) {
3255 int err = zfs_unmount_snap(zc->zc_name, NULL);
3259 if (zc->zc_objset_type == DMU_OST_ZVOL)
3260 (void) zvol_remove_minor(zc->zc_name);
3261 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3265 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3267 const char *propname = nvpair_name(pair);
3268 boolean_t issnap = (strchr(dsname, '@') != NULL);
3269 zfs_prop_t prop = zfs_name_to_prop(propname);
3273 if (prop == ZPROP_INVAL) {
3274 if (zfs_prop_user(propname)) {
3275 if (err = zfs_secpolicy_write_perms(dsname,
3276 ZFS_DELEG_PERM_USERPROP, cr))
3281 if (!issnap && zfs_prop_userquota(propname)) {
3282 const char *perm = NULL;
3283 const char *uq_prefix =
3284 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3285 const char *gq_prefix =
3286 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3288 if (strncmp(propname, uq_prefix,
3289 strlen(uq_prefix)) == 0) {
3290 perm = ZFS_DELEG_PERM_USERQUOTA;
3291 } else if (strncmp(propname, gq_prefix,
3292 strlen(gq_prefix)) == 0) {
3293 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3295 /* USERUSED and GROUPUSED are read-only */
3299 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3310 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3312 * dsl_prop_get_all_impl() returns properties in this
3316 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3317 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3322 * Check that this value is valid for this pool version
3325 case ZFS_PROP_COMPRESSION:
3327 * If the user specified gzip compression, make sure
3328 * the SPA supports it. We ignore any errors here since
3329 * we'll catch them later.
3331 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3332 nvpair_value_uint64(pair, &intval) == 0) {
3333 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3334 intval <= ZIO_COMPRESS_GZIP_9 &&
3335 zfs_earlier_version(dsname,
3336 SPA_VERSION_GZIP_COMPRESSION)) {
3340 if (intval == ZIO_COMPRESS_ZLE &&
3341 zfs_earlier_version(dsname,
3342 SPA_VERSION_ZLE_COMPRESSION))
3346 * If this is a bootable dataset then
3347 * verify that the compression algorithm
3348 * is supported for booting. We must return
3349 * something other than ENOTSUP since it
3350 * implies a downrev pool version.
3352 if (zfs_is_bootfs(dsname) &&
3353 !BOOTFS_COMPRESS_VALID(intval)) {
3359 case ZFS_PROP_COPIES:
3360 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3364 case ZFS_PROP_DEDUP:
3365 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3369 case ZFS_PROP_SHARESMB:
3370 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3374 case ZFS_PROP_ACLINHERIT:
3375 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3376 nvpair_value_uint64(pair, &intval) == 0) {
3377 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3378 zfs_earlier_version(dsname,
3379 SPA_VERSION_PASSTHROUGH_X))
3385 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3389 * Removes properties from the given props list that fail permission checks
3390 * needed to clear them and to restore them in case of a receive error. For each
3391 * property, make sure we have both set and inherit permissions.
3393 * Returns the first error encountered if any permission checks fail. If the
3394 * caller provides a non-NULL errlist, it also gives the complete list of names
3395 * of all the properties that failed a permission check along with the
3396 * corresponding error numbers. The caller is responsible for freeing the
3399 * If every property checks out successfully, zero is returned and the list
3400 * pointed at by errlist is NULL.
3403 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3406 nvpair_t *pair, *next_pair;
3413 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3415 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3416 (void) strcpy(zc->zc_name, dataset);
3417 pair = nvlist_next_nvpair(props, NULL);
3418 while (pair != NULL) {
3419 next_pair = nvlist_next_nvpair(props, pair);
3421 (void) strcpy(zc->zc_value, nvpair_name(pair));
3422 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3423 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3424 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3425 VERIFY(nvlist_add_int32(errors,
3426 zc->zc_value, err) == 0);
3430 kmem_free(zc, sizeof (zfs_cmd_t));
3432 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3433 nvlist_free(errors);
3436 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3439 if (errlist == NULL)
3440 nvlist_free(errors);
3448 propval_equals(nvpair_t *p1, nvpair_t *p2)
3450 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3451 /* dsl_prop_get_all_impl() format */
3453 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3454 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3458 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3460 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3461 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3465 if (nvpair_type(p1) != nvpair_type(p2))
3468 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3469 char *valstr1, *valstr2;
3471 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3472 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3473 return (strcmp(valstr1, valstr2) == 0);
3475 uint64_t intval1, intval2;
3477 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3478 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3479 return (intval1 == intval2);
3484 * Remove properties from props if they are not going to change (as determined
3485 * by comparison with origprops). Remove them from origprops as well, since we
3486 * do not need to clear or restore properties that won't change.
3489 props_reduce(nvlist_t *props, nvlist_t *origprops)
3491 nvpair_t *pair, *next_pair;
3493 if (origprops == NULL)
3494 return; /* all props need to be received */
3496 pair = nvlist_next_nvpair(props, NULL);
3497 while (pair != NULL) {
3498 const char *propname = nvpair_name(pair);
3501 next_pair = nvlist_next_nvpair(props, pair);
3503 if ((nvlist_lookup_nvpair(origprops, propname,
3504 &match) != 0) || !propval_equals(pair, match))
3505 goto next; /* need to set received value */
3507 /* don't clear the existing received value */
3508 (void) nvlist_remove_nvpair(origprops, match);
3509 /* don't bother receiving the property */
3510 (void) nvlist_remove_nvpair(props, pair);
3517 static boolean_t zfs_ioc_recv_inject_err;
3522 * zc_name name of containing filesystem
3523 * zc_nvlist_src{_size} nvlist of properties to apply
3524 * zc_value name of snapshot to create
3525 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3526 * zc_cookie file descriptor to recv from
3527 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3528 * zc_guid force flag
3529 * zc_cleanup_fd cleanup-on-exit file descriptor
3530 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3533 * zc_cookie number of bytes read
3534 * zc_nvlist_dst{_size} error for each unapplied received property
3535 * zc_obj zprop_errflags_t
3536 * zc_action_handle handle for this guid/ds mapping
3539 zfs_ioc_recv(zfs_cmd_t *zc)
3543 dmu_recv_cookie_t drc;
3544 boolean_t force = (boolean_t)zc->zc_guid;
3547 int props_error = 0;
3550 nvlist_t *props = NULL; /* sent properties */
3551 nvlist_t *origprops = NULL; /* existing properties */
3552 objset_t *origin = NULL;
3554 char tofs[ZFS_MAXNAMELEN];
3555 boolean_t first_recvd_props = B_FALSE;
3557 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3558 strchr(zc->zc_value, '@') == NULL ||
3559 strchr(zc->zc_value, '%'))
3562 (void) strcpy(tofs, zc->zc_value);
3563 tosnap = strchr(tofs, '@');
3566 if (zc->zc_nvlist_src != 0 &&
3567 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3568 zc->zc_iflags, &props)) != 0)
3578 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3580 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3581 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3582 !dsl_prop_get_hasrecvd(os)) {
3583 first_recvd_props = B_TRUE;
3587 * If new received properties are supplied, they are to
3588 * completely replace the existing received properties, so stash
3589 * away the existing ones.
3591 if (dsl_prop_get_received(os, &origprops) == 0) {
3592 nvlist_t *errlist = NULL;
3594 * Don't bother writing a property if its value won't
3595 * change (and avoid the unnecessary security checks).
3597 * The first receive after SPA_VERSION_RECVD_PROPS is a
3598 * special case where we blow away all local properties
3601 if (!first_recvd_props)
3602 props_reduce(props, origprops);
3603 if (zfs_check_clearable(tofs, origprops,
3605 (void) nvlist_merge(errors, errlist, 0);
3606 nvlist_free(errlist);
3609 dmu_objset_rele(os, FTAG);
3612 if (zc->zc_string[0]) {
3613 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3618 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3619 &zc->zc_begin_record, force, origin, &drc);
3621 dmu_objset_rele(origin, FTAG);
3626 * Set properties before we receive the stream so that they are applied
3627 * to the new data. Note that we must call dmu_recv_stream() if
3628 * dmu_recv_begin() succeeds.
3633 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3634 if (drc.drc_newfs) {
3635 if (spa_version(os->os_spa) >=
3636 SPA_VERSION_RECVD_PROPS)
3637 first_recvd_props = B_TRUE;
3638 } else if (origprops != NULL) {
3639 if (clear_received_props(os, tofs, origprops,
3640 first_recvd_props ? NULL : props) != 0)
3641 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3643 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3645 dsl_prop_set_hasrecvd(os);
3646 } else if (!drc.drc_newfs) {
3647 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3650 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3652 (void) nvlist_merge(errors, errlist, 0);
3653 nvlist_free(errlist);
3656 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3658 * Caller made zc->zc_nvlist_dst less than the minimum expected
3659 * size or supplied an invalid address.
3661 props_error = EINVAL;
3665 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3666 &zc->zc_action_handle);
3669 zfsvfs_t *zfsvfs = NULL;
3671 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3675 error = zfs_suspend_fs(zfsvfs);
3677 * If the suspend fails, then the recv_end will
3678 * likely also fail, and clean up after itself.
3680 end_err = dmu_recv_end(&drc);
3682 error = zfs_resume_fs(zfsvfs, tofs);
3683 error = error ? error : end_err;
3684 VFS_RELE(zfsvfs->z_vfs);
3686 error = dmu_recv_end(&drc);
3690 zc->zc_cookie = off - fp->f_offset;
3691 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3695 if (zfs_ioc_recv_inject_err) {
3696 zfs_ioc_recv_inject_err = B_FALSE;
3701 * On error, restore the original props.
3703 if (error && props) {
3704 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3705 if (clear_received_props(os, tofs, props, NULL) != 0) {
3707 * We failed to clear the received properties.
3708 * Since we may have left a $recvd value on the
3709 * system, we can't clear the $hasrecvd flag.
3711 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3712 } else if (first_recvd_props) {
3713 dsl_prop_unset_hasrecvd(os);
3715 dmu_objset_rele(os, FTAG);
3716 } else if (!drc.drc_newfs) {
3717 /* We failed to clear the received properties. */
3718 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3721 if (origprops == NULL && !drc.drc_newfs) {
3722 /* We failed to stash the original properties. */
3723 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3727 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3728 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3729 * explictly if we're restoring local properties cleared in the
3730 * first new-style receive.
3732 if (origprops != NULL &&
3733 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3734 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3735 origprops, NULL) != 0) {
3737 * We stashed the original properties but failed to
3740 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3745 nvlist_free(origprops);
3746 nvlist_free(errors);
3750 error = props_error;
3757 * zc_name name of snapshot to send
3758 * zc_cookie file descriptor to send stream to
3759 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3760 * zc_sendobj objsetid of snapshot to send
3761 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3766 zfs_ioc_send(zfs_cmd_t *zc)
3768 objset_t *fromsnap = NULL;
3774 dsl_dataset_t *dsfrom = NULL;
3778 error = spa_open(zc->zc_name, &spa, FTAG);
3782 dp = spa_get_dsl(spa);
3783 rw_enter(&dp->dp_config_rwlock, RW_READER);
3784 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3785 rw_exit(&dp->dp_config_rwlock);
3787 spa_close(spa, FTAG);
3791 error = dmu_objset_from_ds(ds, &tosnap);
3793 dsl_dataset_rele(ds, FTAG);
3794 spa_close(spa, FTAG);
3798 if (zc->zc_fromobj != 0) {
3799 rw_enter(&dp->dp_config_rwlock, RW_READER);
3800 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3801 rw_exit(&dp->dp_config_rwlock);
3802 spa_close(spa, FTAG);
3804 dsl_dataset_rele(ds, FTAG);
3807 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3809 dsl_dataset_rele(dsfrom, FTAG);
3810 dsl_dataset_rele(ds, FTAG);
3814 spa_close(spa, FTAG);
3817 fp = getf(zc->zc_cookie);
3819 dsl_dataset_rele(ds, FTAG);
3821 dsl_dataset_rele(dsfrom, FTAG);
3826 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3828 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3830 releasef(zc->zc_cookie);
3832 dsl_dataset_rele(dsfrom, FTAG);
3833 dsl_dataset_rele(ds, FTAG);
3838 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3842 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3843 &zc->zc_inject_record);
3846 zc->zc_guid = (uint64_t)id;
3852 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3854 return (zio_clear_fault((int)zc->zc_guid));
3858 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3860 int id = (int)zc->zc_guid;
3863 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3864 &zc->zc_inject_record);
3872 zfs_ioc_error_log(zfs_cmd_t *zc)
3876 size_t count = (size_t)zc->zc_nvlist_dst_size;
3878 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3881 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3884 zc->zc_nvlist_dst_size = count;
3886 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3888 spa_close(spa, FTAG);
3894 zfs_ioc_clear(zfs_cmd_t *zc)
3901 * On zpool clear we also fix up missing slogs
3903 mutex_enter(&spa_namespace_lock);
3904 spa = spa_lookup(zc->zc_name);
3906 mutex_exit(&spa_namespace_lock);
3909 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3910 /* we need to let spa_open/spa_load clear the chains */
3911 spa_set_log_state(spa, SPA_LOG_CLEAR);
3913 spa->spa_last_open_failed = 0;
3914 mutex_exit(&spa_namespace_lock);
3916 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3917 error = spa_open(zc->zc_name, &spa, FTAG);
3920 nvlist_t *config = NULL;
3922 if (zc->zc_nvlist_src == 0)
3925 if ((error = get_nvlist(zc->zc_nvlist_src,
3926 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3927 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3929 if (config != NULL) {
3932 if ((err = put_nvlist(zc, config)) != 0)
3934 nvlist_free(config);
3936 nvlist_free(policy);
3943 spa_vdev_state_enter(spa, SCL_NONE);
3945 if (zc->zc_guid == 0) {
3948 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3950 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
3951 spa_close(spa, FTAG);
3956 vdev_clear(spa, vd);
3958 (void) spa_vdev_state_exit(spa, NULL, 0);
3961 * Resume any suspended I/Os.
3963 if (zio_resume(spa) != 0)
3966 spa_close(spa, FTAG);
3973 * zc_name name of filesystem
3974 * zc_value name of origin snapshot
3977 * zc_string name of conflicting snapshot, if there is one
3980 zfs_ioc_promote(zfs_cmd_t *zc)
3985 * We don't need to unmount *all* the origin fs's snapshots, but
3988 cp = strchr(zc->zc_value, '@');
3991 (void) dmu_objset_find(zc->zc_value,
3992 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3993 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
3997 * Retrieve a single {user|group}{used|quota}@... property.
4000 * zc_name name of filesystem
4001 * zc_objset_type zfs_userquota_prop_t
4002 * zc_value domain name (eg. "S-1-234-567-89")
4003 * zc_guid RID/UID/GID
4006 * zc_cookie property value
4009 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4014 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4017 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4021 error = zfs_userspace_one(zfsvfs,
4022 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4023 zfsvfs_rele(zfsvfs, FTAG);
4030 * zc_name name of filesystem
4031 * zc_cookie zap cursor
4032 * zc_objset_type zfs_userquota_prop_t
4033 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4036 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4037 * zc_cookie zap cursor
4040 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4043 int bufsize = zc->zc_nvlist_dst_size;
4048 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4052 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4054 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4055 buf, &zc->zc_nvlist_dst_size);
4058 error = xcopyout(buf,
4059 (void *)(uintptr_t)zc->zc_nvlist_dst,
4060 zc->zc_nvlist_dst_size);
4062 kmem_free(buf, bufsize);
4063 zfsvfs_rele(zfsvfs, FTAG);
4070 * zc_name name of filesystem
4076 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4082 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4083 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4085 * If userused is not enabled, it may be because the
4086 * objset needs to be closed & reopened (to grow the
4087 * objset_phys_t). Suspend/resume the fs will do that.
4089 error = zfs_suspend_fs(zfsvfs);
4091 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4094 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4095 VFS_RELE(zfsvfs->z_vfs);
4097 /* XXX kind of reading contents without owning */
4098 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4102 error = dmu_objset_userspace_upgrade(os);
4103 dmu_objset_rele(os, FTAG);
4110 * We don't want to have a hard dependency
4111 * against some special symbols in sharefs
4112 * nfs, and smbsrv. Determine them if needed when
4113 * the first file system is shared.
4114 * Neither sharefs, nfs or smbsrv are unloadable modules.
4116 int (*znfsexport_fs)(void *arg);
4117 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4118 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4120 int zfs_nfsshare_inited;
4121 int zfs_smbshare_inited;
4123 ddi_modhandle_t nfs_mod;
4124 ddi_modhandle_t sharefs_mod;
4125 ddi_modhandle_t smbsrv_mod;
4126 kmutex_t zfs_share_lock;
4133 ASSERT(MUTEX_HELD(&zfs_share_lock));
4134 /* Both NFS and SMB shares also require sharetab support. */
4135 if (sharefs_mod == NULL && ((sharefs_mod =
4136 ddi_modopen("fs/sharefs",
4137 KRTLD_MODE_FIRST, &error)) == NULL)) {
4140 if (zshare_fs == NULL && ((zshare_fs =
4141 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4142 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4149 zfs_ioc_share(zfs_cmd_t *zc)
4154 switch (zc->zc_share.z_sharetype) {
4156 case ZFS_UNSHARE_NFS:
4157 if (zfs_nfsshare_inited == 0) {
4158 mutex_enter(&zfs_share_lock);
4159 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4160 KRTLD_MODE_FIRST, &error)) == NULL)) {
4161 mutex_exit(&zfs_share_lock);
4164 if (znfsexport_fs == NULL &&
4165 ((znfsexport_fs = (int (*)(void *))
4167 "nfs_export", &error)) == NULL)) {
4168 mutex_exit(&zfs_share_lock);
4171 error = zfs_init_sharefs();
4173 mutex_exit(&zfs_share_lock);
4176 zfs_nfsshare_inited = 1;
4177 mutex_exit(&zfs_share_lock);
4181 case ZFS_UNSHARE_SMB:
4182 if (zfs_smbshare_inited == 0) {
4183 mutex_enter(&zfs_share_lock);
4184 if (smbsrv_mod == NULL && ((smbsrv_mod =
4185 ddi_modopen("drv/smbsrv",
4186 KRTLD_MODE_FIRST, &error)) == NULL)) {
4187 mutex_exit(&zfs_share_lock);
4190 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4191 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4192 "smb_server_share", &error)) == NULL)) {
4193 mutex_exit(&zfs_share_lock);
4196 error = zfs_init_sharefs();
4198 mutex_exit(&zfs_share_lock);
4201 zfs_smbshare_inited = 1;
4202 mutex_exit(&zfs_share_lock);
4209 switch (zc->zc_share.z_sharetype) {
4211 case ZFS_UNSHARE_NFS:
4213 znfsexport_fs((void *)
4214 (uintptr_t)zc->zc_share.z_exportdata))
4218 case ZFS_UNSHARE_SMB:
4219 if (error = zsmbexport_fs((void *)
4220 (uintptr_t)zc->zc_share.z_exportdata,
4221 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4228 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4229 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4230 SHAREFS_ADD : SHAREFS_REMOVE;
4233 * Add or remove share from sharetab
4235 error = zshare_fs(opcode,
4236 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4237 zc->zc_share.z_sharemax);
4243 ace_t full_access[] = {
4244 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4249 * zc_name name of containing filesystem
4250 * zc_obj object # beyond which we want next in-use object #
4253 * zc_obj next in-use object #
4256 zfs_ioc_next_obj(zfs_cmd_t *zc)
4258 objset_t *os = NULL;
4261 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4265 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4266 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4268 dmu_objset_rele(os, FTAG);
4274 * zc_name name of filesystem
4275 * zc_value prefix name for snapshot
4276 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4281 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4286 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4287 (u_longlong_t)ddi_get_lbolt64());
4289 if (strlen(snap_name) >= MAXNAMELEN) {
4294 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4295 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4301 (void) strcpy(zc->zc_value, snap_name);
4308 * zc_name name of "to" snapshot
4309 * zc_value name of "from" snapshot
4310 * zc_cookie file descriptor to write diff data on
4313 * dmu_diff_record_t's to the file descriptor
4316 zfs_ioc_diff(zfs_cmd_t *zc)
4324 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4328 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4330 dmu_objset_rele(tosnap, FTAG);
4334 fp = getf(zc->zc_cookie);
4336 dmu_objset_rele(fromsnap, FTAG);
4337 dmu_objset_rele(tosnap, FTAG);
4343 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4345 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4347 releasef(zc->zc_cookie);
4349 dmu_objset_rele(fromsnap, FTAG);
4350 dmu_objset_rele(tosnap, FTAG);
4355 * Remove all ACL files in shares dir
4358 zfs_smb_acl_purge(znode_t *dzp)
4361 zap_attribute_t zap;
4362 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4365 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4366 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4367 zap_cursor_advance(&zc)) {
4368 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4372 zap_cursor_fini(&zc);
4377 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4381 vnode_t *resourcevp = NULL;
4390 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4391 NO_FOLLOW, NULL, &vp)) != 0)
4394 /* Now make sure mntpnt and dataset are ZFS */
4396 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4397 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4398 zc->zc_name) != 0)) {
4404 zfsvfs = dzp->z_zfsvfs;
4408 * Create share dir if its missing.
4410 mutex_enter(&zfsvfs->z_lock);
4411 if (zfsvfs->z_shares_dir == 0) {
4414 tx = dmu_tx_create(zfsvfs->z_os);
4415 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4417 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4418 error = dmu_tx_assign(tx, TXG_WAIT);
4422 error = zfs_create_share_dir(zfsvfs, tx);
4426 mutex_exit(&zfsvfs->z_lock);
4432 mutex_exit(&zfsvfs->z_lock);
4434 ASSERT(zfsvfs->z_shares_dir);
4435 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4441 switch (zc->zc_cookie) {
4442 case ZFS_SMB_ACL_ADD:
4443 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4444 vattr.va_type = VREG;
4445 vattr.va_mode = S_IFREG|0777;
4449 vsec.vsa_mask = VSA_ACE;
4450 vsec.vsa_aclentp = &full_access;
4451 vsec.vsa_aclentsz = sizeof (full_access);
4452 vsec.vsa_aclcnt = 1;
4454 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4455 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4457 VN_RELE(resourcevp);
4460 case ZFS_SMB_ACL_REMOVE:
4461 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4465 case ZFS_SMB_ACL_RENAME:
4466 if ((error = get_nvlist(zc->zc_nvlist_src,
4467 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4472 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4473 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4476 VN_RELE(ZTOV(sharedir));
4478 nvlist_free(nvlist);
4481 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4483 nvlist_free(nvlist);
4486 case ZFS_SMB_ACL_PURGE:
4487 error = zfs_smb_acl_purge(sharedir);
4496 VN_RELE(ZTOV(sharedir));
4505 * zc_name name of filesystem
4506 * zc_value short name of snap
4507 * zc_string user-supplied tag for this hold
4508 * zc_cookie recursive flag
4509 * zc_temphold set if hold is temporary
4510 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4511 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4512 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4517 zfs_ioc_hold(zfs_cmd_t *zc)
4519 boolean_t recursive = zc->zc_cookie;
4526 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4529 if (zc->zc_sendobj == 0) {
4530 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4531 zc->zc_string, recursive, zc->zc_temphold,
4532 zc->zc_cleanup_fd));
4538 error = spa_open(zc->zc_name, &spa, FTAG);
4542 dp = spa_get_dsl(spa);
4543 rw_enter(&dp->dp_config_rwlock, RW_READER);
4544 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4545 rw_exit(&dp->dp_config_rwlock);
4546 spa_close(spa, FTAG);
4551 * Until we have a hold on this snapshot, it's possible that
4552 * zc_sendobj could've been destroyed and reused as part
4553 * of a later txg. Make sure we're looking at the right object.
4555 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4556 dsl_dataset_rele(ds, FTAG);
4560 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4561 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4563 dsl_dataset_rele(ds, FTAG);
4568 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4572 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4575 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4577 dsl_dataset_rele(ds, FTAG);
4584 * zc_name name of dataset from which we're releasing a user hold
4585 * zc_value short name of snap
4586 * zc_string user-supplied tag for this hold
4587 * zc_cookie recursive flag
4592 zfs_ioc_release(zfs_cmd_t *zc)
4594 boolean_t recursive = zc->zc_cookie;
4596 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4599 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4600 zc->zc_string, recursive));
4605 * zc_name name of filesystem
4608 * zc_nvlist_src{_size} nvlist of snapshot holds
4611 zfs_ioc_get_holds(zfs_cmd_t *zc)
4616 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4617 error = put_nvlist(zc, nvp);
4625 * pool create, destroy, and export don't log the history as part of
4626 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4627 * do the logging of those commands.
4629 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4630 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4632 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4634 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4636 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4638 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4640 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4642 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4644 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4645 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4646 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4647 POOL_CHECK_READONLY },
4648 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4649 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4650 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4652 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4653 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4654 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4655 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4656 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4657 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4658 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4659 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4660 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4661 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4662 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4663 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4664 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4665 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4666 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4667 POOL_CHECK_SUSPENDED },
4668 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4670 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4671 POOL_CHECK_SUSPENDED },
4672 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4673 POOL_CHECK_SUSPENDED },
4674 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4675 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4676 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4677 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4678 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4679 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4680 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4681 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4682 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4683 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4684 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4685 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4686 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4688 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4690 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4692 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4694 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4696 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4698 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4699 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4700 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4701 B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4702 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4703 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4704 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4706 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4707 POOL_CHECK_SUSPENDED },
4708 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4709 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4710 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4712 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4713 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4714 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4716 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4718 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4719 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4720 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4722 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4723 B_FALSE, POOL_CHECK_NONE },
4724 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4725 B_FALSE, POOL_CHECK_NONE },
4726 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4727 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4728 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4729 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4730 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4731 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4732 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4733 POOL_CHECK_SUSPENDED },
4734 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4736 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4737 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4738 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4740 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4742 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4743 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4744 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4745 POOL_CHECK_SUSPENDED }
4749 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4750 zfs_ioc_poolcheck_t check)
4755 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4757 if (check & POOL_CHECK_NONE)
4760 error = spa_open(name, &spa, FTAG);
4762 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4764 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4766 spa_close(spa, FTAG);
4772 * Find a free minor number.
4775 zfsdev_minor_alloc(void)
4777 static minor_t last_minor;
4780 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4782 for (m = last_minor + 1; m != last_minor; m++) {
4783 if (m > ZFSDEV_MAX_MINOR)
4785 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
4795 zfs_ctldev_init(dev_t *devp)
4798 zfs_soft_state_t *zs;
4800 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4801 ASSERT(getminor(*devp) == 0);
4803 minor = zfsdev_minor_alloc();
4807 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
4810 *devp = makedevice(getemajor(*devp), minor);
4812 zs = ddi_get_soft_state(zfsdev_state, minor);
4813 zs->zss_type = ZSST_CTLDEV;
4814 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
4820 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
4822 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4824 zfs_onexit_destroy(zo);
4825 ddi_soft_state_free(zfsdev_state, minor);
4829 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
4831 zfs_soft_state_t *zp;
4833 zp = ddi_get_soft_state(zfsdev_state, minor);
4834 if (zp == NULL || zp->zss_type != which)
4837 return (zp->zss_data);
4841 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
4845 if (getminor(*devp) != 0)
4846 return (zvol_open(devp, flag, otyp, cr));
4848 /* This is the control device. Allocate a new minor if requested. */
4850 mutex_enter(&zfsdev_state_lock);
4851 error = zfs_ctldev_init(devp);
4852 mutex_exit(&zfsdev_state_lock);
4859 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
4862 minor_t minor = getminor(dev);
4867 mutex_enter(&zfsdev_state_lock);
4868 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
4870 mutex_exit(&zfsdev_state_lock);
4871 return (zvol_close(dev, flag, otyp, cr));
4873 zfs_ctldev_destroy(zo, minor);
4874 mutex_exit(&zfsdev_state_lock);
4880 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4885 minor_t minor = getminor(dev);
4888 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
4889 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4891 vec = cmd - ZFS_IOC;
4892 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4894 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4897 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4899 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4903 if ((error == 0) && !(flag & FKIOCTL))
4904 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4907 * Ensure that all pool/dataset names are valid before we pass down to
4911 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4912 zc->zc_iflags = flag & FKIOCTL;
4913 switch (zfs_ioc_vec[vec].zvec_namecheck) {
4915 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4917 error = pool_status_check(zc->zc_name,
4918 zfs_ioc_vec[vec].zvec_namecheck,
4919 zfs_ioc_vec[vec].zvec_pool_check);
4923 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4925 error = pool_status_check(zc->zc_name,
4926 zfs_ioc_vec[vec].zvec_namecheck,
4927 zfs_ioc_vec[vec].zvec_pool_check);
4936 error = zfs_ioc_vec[vec].zvec_func(zc);
4938 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4942 if (zfs_ioc_vec[vec].zvec_his_log)
4943 zfs_log_history(zc);
4946 kmem_free(zc, sizeof (zfs_cmd_t));
4951 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4953 if (cmd != DDI_ATTACH)
4954 return (DDI_FAILURE);
4956 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4957 DDI_PSEUDO, 0) == DDI_FAILURE)
4958 return (DDI_FAILURE);
4962 ddi_report_dev(dip);
4964 return (DDI_SUCCESS);
4968 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4970 if (spa_busy() || zfs_busy() || zvol_busy())
4971 return (DDI_FAILURE);
4973 if (cmd != DDI_DETACH)
4974 return (DDI_FAILURE);
4978 ddi_prop_remove_all(dip);
4979 ddi_remove_minor_node(dip, NULL);
4981 return (DDI_SUCCESS);
4986 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4989 case DDI_INFO_DEVT2DEVINFO:
4991 return (DDI_SUCCESS);
4993 case DDI_INFO_DEVT2INSTANCE:
4994 *result = (void *)0;
4995 return (DDI_SUCCESS);
4998 return (DDI_FAILURE);
5002 * OK, so this is a little weird.
5004 * /dev/zfs is the control node, i.e. minor 0.
5005 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5007 * /dev/zfs has basically nothing to do except serve up ioctls,
5008 * so most of the standard driver entry points are in zvol.c.
5010 static struct cb_ops zfs_cb_ops = {
5011 zfsdev_open, /* open */
5012 zfsdev_close, /* close */
5013 zvol_strategy, /* strategy */
5015 zvol_dump, /* dump */
5016 zvol_read, /* read */
5017 zvol_write, /* write */
5018 zfsdev_ioctl, /* ioctl */
5022 nochpoll, /* poll */
5023 ddi_prop_op, /* prop_op */
5024 NULL, /* streamtab */
5025 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
5026 CB_REV, /* version */
5027 nodev, /* async read */
5028 nodev, /* async write */
5031 static struct dev_ops zfs_dev_ops = {
5032 DEVO_REV, /* version */
5034 zfs_info, /* info */
5035 nulldev, /* identify */
5036 nulldev, /* probe */
5037 zfs_attach, /* attach */
5038 zfs_detach, /* detach */
5040 &zfs_cb_ops, /* driver operations */
5041 NULL, /* no bus operations */
5043 ddi_quiesce_not_needed, /* quiesce */
5046 static struct modldrv zfs_modldrv = {
5052 static struct modlinkage modlinkage = {
5054 (void *)&zfs_modlfs,
5055 (void *)&zfs_modldrv,
5060 uint_t zfs_fsyncer_key;
5061 extern uint_t rrw_tsd_key;
5068 spa_init(FREAD | FWRITE);
5072 if ((error = mod_install(&modlinkage)) != 0) {
5079 tsd_create(&zfs_fsyncer_key, NULL);
5080 tsd_create(&rrw_tsd_key, NULL);
5082 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5084 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5094 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5097 if ((error = mod_remove(&modlinkage)) != 0)
5103 if (zfs_nfsshare_inited)
5104 (void) ddi_modclose(nfs_mod);
5105 if (zfs_smbshare_inited)
5106 (void) ddi_modclose(smbsrv_mod);
5107 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5108 (void) ddi_modclose(sharefs_mod);
5110 tsd_destroy(&zfs_fsyncer_key);
5111 ldi_ident_release(zfs_li);
5113 mutex_destroy(&zfs_share_lock);
5119 _info(struct modinfo *modinfop)
5121 return (mod_info(&modlinkage, modinfop));