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_dir.h>
62 #include <sys/zfs_onexit.h>
64 #include <sys/dsl_scan.h>
65 #include <sharefs/share.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/fm/util.h>
69 #include <linux/miscdevice.h>
71 #include "zfs_namecheck.h"
73 #include "zfs_deleg.h"
74 #include "zfs_comutil.h"
76 kmutex_t zfsdev_state_lock;
77 list_t zfsdev_state_list;
79 extern void zfs_init(void);
80 extern void zfs_fini(void);
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 | KM_NODEBUG);
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)
388 char ds_hexsl[MAXNAMELEN];
389 bslabel_t ds_sl, new_sl;
390 boolean_t new_default = FALSE;
392 int needed_priv = -1;
395 /* First get the existing dataset label. */
396 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
397 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
401 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
404 /* The label must be translatable */
405 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
409 * In a non-global zone, disallow attempts to set a label that
410 * doesn't match that of the zone; otherwise no other checks
413 if (!INGLOBALZONE(curproc)) {
414 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
420 * For global-zone datasets (i.e., those whose zoned property is
421 * "off", verify that the specified new label is valid for the
424 if (dsl_prop_get_integer(name,
425 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
428 if (zfs_check_global_label(name, strval) != 0)
433 * If the existing dataset label is nondefault, check if the
434 * dataset is mounted (label cannot be changed while mounted).
435 * Get the zfsvfs; if there isn't one, then the dataset isn't
436 * mounted (or isn't a dataset, doesn't exist, ...).
438 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
440 static char *setsl_tag = "setsl_tag";
443 * Try to own the dataset; abort if there is any error,
444 * (e.g., already mounted, in use, or other error).
446 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
451 dmu_objset_disown(os, setsl_tag);
454 needed_priv = PRIV_FILE_DOWNGRADE_SL;
458 if (hexstr_to_label(strval, &new_sl) != 0)
461 if (blstrictdom(&ds_sl, &new_sl))
462 needed_priv = PRIV_FILE_DOWNGRADE_SL;
463 else if (blstrictdom(&new_sl, &ds_sl))
464 needed_priv = PRIV_FILE_UPGRADE_SL;
466 /* dataset currently has a default label */
468 needed_priv = PRIV_FILE_UPGRADE_SL;
472 if (needed_priv != -1)
473 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
477 #endif /* HAVE_MLSLABEL */
481 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
487 * Check permissions for special properties.
494 * Disallow setting of 'zoned' from within a local zone.
496 if (!INGLOBALZONE(curproc))
501 if (!INGLOBALZONE(curproc)) {
503 char setpoint[MAXNAMELEN];
505 * Unprivileged users are allowed to modify the
506 * quota on things *under* (ie. contained by)
507 * the thing they own.
509 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
512 if (!zoned || strlen(dsname) <= strlen(setpoint))
517 case ZFS_PROP_MLSLABEL:
518 if (!is_system_labeled())
521 if (nvpair_value_string(propval, &strval) == 0) {
524 err = zfs_set_slabel_policy(dsname, strval, CRED());
531 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
535 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
539 error = zfs_dozonecheck(zc->zc_name, cr);
544 * permission to set permissions will be evaluated later in
545 * dsl_deleg_can_allow()
551 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
553 return (zfs_secpolicy_write_perms(zc->zc_name,
554 ZFS_DELEG_PERM_ROLLBACK, cr));
558 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
567 * Generate the current snapshot name from the given objsetid, then
568 * use that name for the secpolicy/zone checks.
570 cp = strchr(zc->zc_name, '@');
573 error = spa_open(zc->zc_name, &spa, FTAG);
577 dp = spa_get_dsl(spa);
578 rw_enter(&dp->dp_config_rwlock, RW_READER);
579 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
580 rw_exit(&dp->dp_config_rwlock);
581 spa_close(spa, FTAG);
585 dsl_dataset_name(ds, zc->zc_name);
587 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
588 ZFS_DELEG_PERM_SEND, cr);
589 dsl_dataset_rele(ds, FTAG);
596 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
601 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
602 NO_FOLLOW, NULL, &vp)) != 0)
605 /* Now make sure mntpnt and dataset are ZFS */
607 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
608 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
609 zc->zc_name) != 0)) {
615 return (dsl_deleg_access(zc->zc_name,
616 ZFS_DELEG_PERM_SHARE, cr));
618 #endif /* HAVE_SHARE */
621 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
624 if (!INGLOBALZONE(curproc))
627 if (secpolicy_nfs(cr) == 0) {
630 return (zfs_secpolicy_deleg_share(zc, cr));
634 #endif /* HAVE_SHARE */
638 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
641 if (!INGLOBALZONE(curproc))
644 if (secpolicy_smb(cr) == 0) {
647 return (zfs_secpolicy_deleg_share(zc, cr));
651 #endif /* HAVE_SHARE */
655 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
660 * Remove the @bla or /bla from the end of the name to get the parent.
662 (void) strncpy(parent, datasetname, parentsize);
663 cp = strrchr(parent, '@');
667 cp = strrchr(parent, '/');
677 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
681 if ((error = zfs_secpolicy_write_perms(name,
682 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
685 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
689 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
691 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
695 * Destroying snapshots with delegated permissions requires
696 * descendent mount and destroy permissions.
697 * Reassemble the full filesystem@snap name so dsl_deleg_access()
698 * can do the correct permission check.
700 * Since this routine is used when doing a recursive destroy of snapshots
701 * and destroying snapshots requires descendent permissions, a successfull
702 * check of the top level snapshot applies to snapshots of all descendent
706 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
711 dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
713 error = zfs_secpolicy_destroy_perms(dsname, cr);
720 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
722 char parentname[MAXNAMELEN];
725 if ((error = zfs_secpolicy_write_perms(from,
726 ZFS_DELEG_PERM_RENAME, cr)) != 0)
729 if ((error = zfs_secpolicy_write_perms(from,
730 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
733 if ((error = zfs_get_parent(to, parentname,
734 sizeof (parentname))) != 0)
737 if ((error = zfs_secpolicy_write_perms(parentname,
738 ZFS_DELEG_PERM_CREATE, cr)) != 0)
741 if ((error = zfs_secpolicy_write_perms(parentname,
742 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
749 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
751 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
755 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
757 char parentname[MAXNAMELEN];
761 error = zfs_secpolicy_write_perms(zc->zc_name,
762 ZFS_DELEG_PERM_PROMOTE, cr);
766 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
769 dsl_dataset_t *pclone = NULL;
771 dd = clone->os_dsl_dataset->ds_dir;
773 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
774 error = dsl_dataset_hold_obj(dd->dd_pool,
775 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
776 rw_exit(&dd->dd_pool->dp_config_rwlock);
778 dmu_objset_rele(clone, FTAG);
782 error = zfs_secpolicy_write_perms(zc->zc_name,
783 ZFS_DELEG_PERM_MOUNT, cr);
785 dsl_dataset_name(pclone, parentname);
786 dmu_objset_rele(clone, FTAG);
787 dsl_dataset_rele(pclone, FTAG);
789 error = zfs_secpolicy_write_perms(parentname,
790 ZFS_DELEG_PERM_PROMOTE, cr);
796 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
800 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
801 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
804 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
805 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
808 return (zfs_secpolicy_write_perms(zc->zc_name,
809 ZFS_DELEG_PERM_CREATE, cr));
813 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
815 return (zfs_secpolicy_write_perms(name,
816 ZFS_DELEG_PERM_SNAPSHOT, cr));
820 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
823 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
827 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
829 char parentname[MAXNAMELEN];
832 if ((error = zfs_get_parent(zc->zc_name, parentname,
833 sizeof (parentname))) != 0)
836 if (zc->zc_value[0] != '\0') {
837 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
838 ZFS_DELEG_PERM_CLONE, cr)) != 0)
842 if ((error = zfs_secpolicy_write_perms(parentname,
843 ZFS_DELEG_PERM_CREATE, cr)) != 0)
846 error = zfs_secpolicy_write_perms(parentname,
847 ZFS_DELEG_PERM_MOUNT, cr);
854 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
858 error = secpolicy_fs_unmount(cr, NULL);
860 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
864 #endif /* HAVE_ZPL */
867 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
868 * SYS_CONFIG privilege, which is not available in a local zone.
872 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
874 if (secpolicy_sys_config(cr, B_FALSE) != 0)
881 * Policy for object to name lookups.
885 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
889 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
892 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
897 * Policy for fault injection. Requires all privileges.
901 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
903 return (secpolicy_zinject(cr));
907 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
909 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
911 if (prop == ZPROP_INVAL) {
912 if (!zfs_prop_user(zc->zc_value))
914 return (zfs_secpolicy_write_perms(zc->zc_name,
915 ZFS_DELEG_PERM_USERPROP, cr));
917 return (zfs_secpolicy_setprop(zc->zc_name, prop,
923 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
925 int err = zfs_secpolicy_read(zc, cr);
929 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
932 if (zc->zc_value[0] == 0) {
934 * They are asking about a posix uid/gid. If it's
935 * themself, allow it.
937 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
938 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
939 if (zc->zc_guid == crgetuid(cr))
942 if (groupmember(zc->zc_guid, cr))
947 return (zfs_secpolicy_write_perms(zc->zc_name,
948 userquota_perms[zc->zc_objset_type], cr));
952 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
954 int err = zfs_secpolicy_read(zc, cr);
958 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
961 return (zfs_secpolicy_write_perms(zc->zc_name,
962 userquota_perms[zc->zc_objset_type], cr));
966 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
968 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
973 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
975 return (zfs_secpolicy_write_perms(zc->zc_name,
976 ZFS_DELEG_PERM_HOLD, cr));
980 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
982 return (zfs_secpolicy_write_perms(zc->zc_name,
983 ZFS_DELEG_PERM_RELEASE, cr));
987 * Policy for allowing temporary snapshots to be taken or released
990 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
993 * A temporary snapshot is the same as a snapshot,
994 * hold, destroy and release all rolled into one.
995 * Delegated diff alone is sufficient that we allow this.
999 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1000 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1003 error = zfs_secpolicy_snapshot(zc, cr);
1005 error = zfs_secpolicy_hold(zc, cr);
1007 error = zfs_secpolicy_release(zc, cr);
1009 error = zfs_secpolicy_destroy(zc, cr);
1014 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1017 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1021 nvlist_t *list = NULL;
1024 * Read in and unpack the user-supplied nvlist.
1029 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1031 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1033 kmem_free(packed, size);
1037 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1038 kmem_free(packed, size);
1042 kmem_free(packed, size);
1049 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1053 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1055 if (size > zc->zc_nvlist_dst_size) {
1056 nvpair_t *more_errors;
1059 if (zc->zc_nvlist_dst_size < 1024)
1062 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1063 more_errors = nvlist_prev_nvpair(*errors, NULL);
1066 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1068 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1070 VERIFY(nvlist_size(*errors, &size,
1071 NV_ENCODE_NATIVE) == 0);
1072 } while (size > zc->zc_nvlist_dst_size);
1074 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1075 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1076 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1077 ASSERT(size <= zc->zc_nvlist_dst_size);
1084 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1086 char *packed = NULL;
1090 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1092 if (size > zc->zc_nvlist_dst_size) {
1095 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1096 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1098 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1099 size, zc->zc_iflags) != 0)
1101 kmem_free(packed, size);
1104 zc->zc_nvlist_dst_size = size;
1110 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1115 error = dmu_objset_hold(dsname, FTAG, &os);
1118 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1119 dmu_objset_rele(os, FTAG);
1123 mutex_enter(&os->os_user_ptr_lock);
1124 *zfvp = dmu_objset_get_user(os);
1126 VFS_HOLD((*zfvp)->z_vfs);
1130 mutex_exit(&os->os_user_ptr_lock);
1131 dmu_objset_rele(os, FTAG);
1137 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1138 * case its z_vfs will be NULL, and it will be opened as the owner.
1141 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1146 if (getzfsvfs(name, zfvp) != 0)
1147 error = zfsvfs_create(name, zfvp);
1149 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1151 if ((*zfvp)->z_unmounted) {
1153 * XXX we could probably try again, since the unmounting
1154 * thread should be just about to disassociate the
1155 * objset from the zfsvfs.
1157 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1168 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1171 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1173 if (zfsvfs->z_vfs) {
1174 VFS_RELE(zfsvfs->z_vfs);
1176 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1177 zfsvfs_free(zfsvfs);
1183 zfs_ioc_pool_create(zfs_cmd_t *zc)
1186 nvlist_t *config, *props = NULL;
1187 nvlist_t *rootprops = NULL;
1188 nvlist_t *zplprops = NULL;
1191 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1192 zc->zc_iflags, &config)))
1195 if (zc->zc_nvlist_src_size != 0 && (error =
1196 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1197 zc->zc_iflags, &props))) {
1198 nvlist_free(config);
1203 nvlist_t *nvl = NULL;
1204 uint64_t version = SPA_VERSION;
1206 (void) nvlist_lookup_uint64(props,
1207 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1208 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1210 goto pool_props_bad;
1212 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1214 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1216 nvlist_free(config);
1220 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1222 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1223 error = zfs_fill_zplprops_root(version, rootprops,
1226 goto pool_props_bad;
1229 buf = history_str_get(zc);
1231 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1234 * Set the remaining root properties
1236 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1237 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1238 (void) spa_destroy(zc->zc_name);
1241 history_str_free(buf);
1244 nvlist_free(rootprops);
1245 nvlist_free(zplprops);
1246 nvlist_free(config);
1253 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1256 zfs_log_history(zc);
1257 error = spa_destroy(zc->zc_name);
1259 zvol_remove_minors(zc->zc_name);
1264 zfs_ioc_pool_import(zfs_cmd_t *zc)
1266 nvlist_t *config, *props = NULL;
1270 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1271 zc->zc_iflags, &config)) != 0)
1274 if (zc->zc_nvlist_src_size != 0 && (error =
1275 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1276 zc->zc_iflags, &props))) {
1277 nvlist_free(config);
1281 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1282 guid != zc->zc_guid)
1285 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1287 if (zc->zc_nvlist_dst != 0) {
1290 if ((err = put_nvlist(zc, config)) != 0)
1295 zvol_create_minors(zc->zc_name);
1297 nvlist_free(config);
1306 zfs_ioc_pool_export(zfs_cmd_t *zc)
1309 boolean_t force = (boolean_t)zc->zc_cookie;
1310 boolean_t hardforce = (boolean_t)zc->zc_guid;
1312 zfs_log_history(zc);
1313 error = spa_export(zc->zc_name, NULL, force, hardforce);
1315 zvol_remove_minors(zc->zc_name);
1320 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1325 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1328 error = put_nvlist(zc, configs);
1330 nvlist_free(configs);
1336 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1342 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1343 sizeof (zc->zc_value));
1345 if (config != NULL) {
1346 ret = put_nvlist(zc, config);
1347 nvlist_free(config);
1350 * The config may be present even if 'error' is non-zero.
1351 * In this case we return success, and preserve the real errno
1354 zc->zc_cookie = error;
1363 * Try to import the given pool, returning pool stats as appropriate so that
1364 * user land knows which devices are available and overall pool health.
1367 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1369 nvlist_t *tryconfig, *config;
1372 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1373 zc->zc_iflags, &tryconfig)) != 0)
1376 config = spa_tryimport(tryconfig);
1378 nvlist_free(tryconfig);
1383 error = put_nvlist(zc, config);
1384 nvlist_free(config);
1391 * zc_name name of the pool
1392 * zc_cookie scan func (pool_scan_func_t)
1395 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1400 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1403 if (zc->zc_cookie == POOL_SCAN_NONE)
1404 error = spa_scan_stop(spa);
1406 error = spa_scan(spa, zc->zc_cookie);
1408 spa_close(spa, FTAG);
1414 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1419 error = spa_open(zc->zc_name, &spa, FTAG);
1422 spa_close(spa, FTAG);
1428 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1433 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1436 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1437 spa_close(spa, FTAG);
1441 spa_upgrade(spa, zc->zc_cookie);
1442 spa_close(spa, FTAG);
1448 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1455 if ((size = zc->zc_history_len) == 0)
1458 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1461 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1462 spa_close(spa, FTAG);
1466 hist_buf = kmem_alloc(size, KM_SLEEP);
1467 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1468 &zc->zc_history_len, hist_buf)) == 0) {
1469 error = ddi_copyout(hist_buf,
1470 (void *)(uintptr_t)zc->zc_history,
1471 zc->zc_history_len, zc->zc_iflags);
1474 spa_close(spa, FTAG);
1475 kmem_free(hist_buf, size);
1480 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1484 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1492 * zc_name name of filesystem
1493 * zc_obj object to find
1496 * zc_value name of object
1499 zfs_ioc_obj_to_path(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_path(os, zc->zc_obj, zc->zc_value,
1512 sizeof (zc->zc_value));
1513 dmu_objset_rele(os, FTAG);
1520 * zc_name name of filesystem
1521 * zc_obj object to find
1524 * zc_stat stats on object
1525 * zc_value path to object
1528 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1533 /* XXX reading from objset not owned */
1534 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1536 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1537 dmu_objset_rele(os, FTAG);
1540 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1541 sizeof (zc->zc_value));
1542 dmu_objset_rele(os, FTAG);
1548 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1552 nvlist_t *config, **l2cache, **spares;
1553 uint_t nl2cache = 0, nspares = 0;
1555 error = spa_open(zc->zc_name, &spa, FTAG);
1559 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1560 zc->zc_iflags, &config);
1561 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1562 &l2cache, &nl2cache);
1564 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1568 * A root pool with concatenated devices is not supported.
1569 * Thus, can not add a device to a root pool.
1571 * Intent log device can not be added to a rootpool because
1572 * during mountroot, zil is replayed, a seperated log device
1573 * can not be accessed during the mountroot time.
1575 * l2cache and spare devices are ok to be added to a rootpool.
1577 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1578 nvlist_free(config);
1579 spa_close(spa, FTAG);
1584 error = spa_vdev_add(spa, config);
1585 nvlist_free(config);
1587 spa_close(spa, FTAG);
1593 * zc_name name of the pool
1594 * zc_nvlist_conf nvlist of devices to remove
1595 * zc_cookie to stop the remove?
1598 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1603 error = spa_open(zc->zc_name, &spa, FTAG);
1606 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1607 spa_close(spa, FTAG);
1612 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1616 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1618 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1620 switch (zc->zc_cookie) {
1621 case VDEV_STATE_ONLINE:
1622 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1625 case VDEV_STATE_OFFLINE:
1626 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1629 case VDEV_STATE_FAULTED:
1630 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1631 zc->zc_obj != VDEV_AUX_EXTERNAL)
1632 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1634 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1637 case VDEV_STATE_DEGRADED:
1638 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1639 zc->zc_obj != VDEV_AUX_EXTERNAL)
1640 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1642 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1648 zc->zc_cookie = newstate;
1649 spa_close(spa, FTAG);
1654 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1657 int replacing = zc->zc_cookie;
1661 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1664 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1665 zc->zc_iflags, &config)) == 0) {
1666 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1667 nvlist_free(config);
1670 spa_close(spa, FTAG);
1675 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1680 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1683 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1685 spa_close(spa, FTAG);
1690 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1693 nvlist_t *config, *props = NULL;
1695 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1697 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1700 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1701 zc->zc_iflags, &config))) {
1702 spa_close(spa, FTAG);
1706 if (zc->zc_nvlist_src_size != 0 && (error =
1707 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1708 zc->zc_iflags, &props))) {
1709 spa_close(spa, FTAG);
1710 nvlist_free(config);
1714 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1716 spa_close(spa, FTAG);
1718 nvlist_free(config);
1725 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1728 char *path = zc->zc_value;
1729 uint64_t guid = zc->zc_guid;
1732 error = spa_open(zc->zc_name, &spa, FTAG);
1736 error = spa_vdev_setpath(spa, guid, path);
1737 spa_close(spa, FTAG);
1742 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1745 char *fru = zc->zc_value;
1746 uint64_t guid = zc->zc_guid;
1749 error = spa_open(zc->zc_name, &spa, FTAG);
1753 error = spa_vdev_setfru(spa, guid, fru);
1754 spa_close(spa, FTAG);
1759 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1764 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1766 if (zc->zc_nvlist_dst != 0 &&
1767 (error = dsl_prop_get_all(os, &nv)) == 0) {
1768 dmu_objset_stats(os, nv);
1770 * NB: zvol_get_stats() will read the objset contents,
1771 * which we aren't supposed to do with a
1772 * DS_MODE_USER hold, because it could be
1773 * inconsistent. So this is a bit of a workaround...
1774 * XXX reading with out owning
1776 if (!zc->zc_objset_stats.dds_inconsistent) {
1777 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1778 error = zvol_get_stats(os, nv);
1781 error = put_nvlist(zc, nv);
1790 * zc_name name of filesystem
1791 * zc_nvlist_dst_size size of buffer for property nvlist
1794 * zc_objset_stats stats
1795 * zc_nvlist_dst property nvlist
1796 * zc_nvlist_dst_size size of property nvlist
1799 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1801 objset_t *os = NULL;
1804 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1807 error = zfs_ioc_objset_stats_impl(zc, os);
1809 dmu_objset_rele(os, FTAG);
1816 * zc_name name of filesystem
1817 * zc_nvlist_dst_size size of buffer for property nvlist
1820 * zc_nvlist_dst received property nvlist
1821 * zc_nvlist_dst_size size of received property nvlist
1823 * Gets received properties (distinct from local properties on or after
1824 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1825 * local property values.
1828 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1830 objset_t *os = NULL;
1834 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1838 * Without this check, we would return local property values if the
1839 * caller has not already received properties on or after
1840 * SPA_VERSION_RECVD_PROPS.
1842 if (!dsl_prop_get_hasrecvd(os)) {
1843 dmu_objset_rele(os, FTAG);
1847 if (zc->zc_nvlist_dst != 0 &&
1848 (error = dsl_prop_get_received(os, &nv)) == 0) {
1849 error = put_nvlist(zc, nv);
1853 dmu_objset_rele(os, FTAG);
1858 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1864 * zfs_get_zplprop() will either find a value or give us
1865 * the default value (if there is one).
1867 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1869 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1875 * zc_name name of filesystem
1876 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1879 * zc_nvlist_dst zpl property nvlist
1880 * zc_nvlist_dst_size size of zpl property nvlist
1883 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1888 /* XXX reading without owning */
1889 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1892 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1895 * NB: nvl_add_zplprop() will read the objset contents,
1896 * which we aren't supposed to do with a DS_MODE_USER
1897 * hold, because it could be inconsistent.
1899 if (zc->zc_nvlist_dst != 0 &&
1900 !zc->zc_objset_stats.dds_inconsistent &&
1901 dmu_objset_type(os) == DMU_OST_ZFS) {
1904 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1905 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1906 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1907 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1908 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1909 err = put_nvlist(zc, nv);
1914 dmu_objset_rele(os, FTAG);
1919 dataset_name_hidden(const char *name)
1922 * Skip over datasets that are not visible in this zone,
1923 * internal datasets (which have a $ in their name), and
1924 * temporary datasets (which have a % in their name).
1926 if (strchr(name, '$') != NULL)
1928 if (strchr(name, '%') != NULL)
1930 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1937 * zc_name name of filesystem
1938 * zc_cookie zap cursor
1939 * zc_nvlist_dst_size size of buffer for property nvlist
1942 * zc_name name of next filesystem
1943 * zc_cookie zap cursor
1944 * zc_objset_stats stats
1945 * zc_nvlist_dst property nvlist
1946 * zc_nvlist_dst_size size of property nvlist
1949 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1954 size_t orig_len = strlen(zc->zc_name);
1957 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1958 if (error == ENOENT)
1963 p = strrchr(zc->zc_name, '/');
1964 if (p == NULL || p[1] != '\0')
1965 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1966 p = zc->zc_name + strlen(zc->zc_name);
1969 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1970 * but is not declared void because its called by dmu_objset_find().
1972 if (zc->zc_cookie == 0) {
1973 uint64_t cookie = 0;
1974 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1976 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1977 (void) dmu_objset_prefetch(p, NULL);
1981 error = dmu_dir_list_next(os,
1982 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1983 NULL, &zc->zc_cookie);
1984 if (error == ENOENT)
1986 } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1987 !(zc->zc_iflags & FKIOCTL));
1988 dmu_objset_rele(os, FTAG);
1991 * If it's an internal dataset (ie. with a '$' in its name),
1992 * don't try to get stats for it, otherwise we'll return ENOENT.
1994 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1995 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1996 if (error == ENOENT) {
1997 /* We lost a race with destroy, get the next one. */
1998 zc->zc_name[orig_len] = '\0';
2007 * zc_name name of filesystem
2008 * zc_cookie zap cursor
2009 * zc_nvlist_dst_size size of buffer for property nvlist
2012 * zc_name name of next snapshot
2013 * zc_objset_stats stats
2014 * zc_nvlist_dst property nvlist
2015 * zc_nvlist_dst_size size of property nvlist
2018 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2024 if (zc->zc_cookie == 0)
2025 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2026 NULL, DS_FIND_SNAPSHOTS);
2028 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2030 return (error == ENOENT ? ESRCH : error);
2033 * A dataset name of maximum length cannot have any snapshots,
2034 * so exit immediately.
2036 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2037 dmu_objset_rele(os, FTAG);
2041 error = dmu_snapshot_list_next(os,
2042 sizeof (zc->zc_name) - strlen(zc->zc_name),
2043 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2048 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2051 * Since we probably don't have a hold on this snapshot,
2052 * it's possible that the objsetid could have been destroyed
2053 * and reused for a new objset. It's OK if this happens during
2054 * a zfs send operation, since the new createtxg will be
2055 * beyond the range we're interested in.
2057 rw_enter(&dp->dp_config_rwlock, RW_READER);
2058 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2059 rw_exit(&dp->dp_config_rwlock);
2061 if (error == ENOENT) {
2062 /* Racing with destroy, get the next one. */
2063 *strchr(zc->zc_name, '@') = '\0';
2064 dmu_objset_rele(os, FTAG);
2070 error = dmu_objset_from_ds(ds, &ossnap);
2072 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2073 dsl_dataset_rele(ds, FTAG);
2075 } else if (error == ENOENT) {
2079 dmu_objset_rele(os, FTAG);
2080 /* if we failed, undo the @ that we tacked on to zc_name */
2082 *strchr(zc->zc_name, '@') = '\0';
2087 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2090 const char *propname = nvpair_name(pair);
2092 unsigned int vallen;
2095 zfs_userquota_prop_t type;
2101 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2103 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2104 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2110 * A correctly constructed propname is encoded as
2111 * userquota@<rid>-<domain>.
2113 if ((dash = strchr(propname, '-')) == NULL ||
2114 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2123 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2125 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2126 zfsvfs_rele(zfsvfs, FTAG);
2136 * If the named property is one that has a special function to set its value,
2137 * return 0 on success and a positive error code on failure; otherwise if it is
2138 * not one of the special properties handled by this function, return -1.
2140 * XXX: It would be better for callers of the property interface if we handled
2141 * these special cases in dsl_prop.c (in the dsl layer).
2144 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2147 const char *propname = nvpair_name(pair);
2148 zfs_prop_t prop = zfs_name_to_prop(propname);
2152 if (prop == ZPROP_INVAL) {
2153 if (zfs_prop_userquota(propname))
2154 return (zfs_prop_set_userquota(dsname, pair));
2158 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2160 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2161 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2165 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2168 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2171 case ZFS_PROP_QUOTA:
2172 err = dsl_dir_set_quota(dsname, source, intval);
2174 case ZFS_PROP_REFQUOTA:
2175 err = dsl_dataset_set_quota(dsname, source, intval);
2177 case ZFS_PROP_RESERVATION:
2178 err = dsl_dir_set_reservation(dsname, source, intval);
2180 case ZFS_PROP_REFRESERVATION:
2181 err = dsl_dataset_set_reservation(dsname, source, intval);
2183 case ZFS_PROP_VOLSIZE:
2184 err = zvol_set_volsize(dsname, intval);
2186 case ZFS_PROP_VERSION:
2190 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2194 err = zfs_set_version(zfsvfs, intval);
2196 zfsvfs_rele(zfsvfs, FTAG);
2198 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2201 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2202 (void) strcpy(zc->zc_name, dsname);
2203 (void) zfs_ioc_userspace_upgrade(zc);
2204 kmem_free(zc, sizeof (zfs_cmd_t));
2217 * This function is best effort. If it fails to set any of the given properties,
2218 * it continues to set as many as it can and returns the first error
2219 * encountered. If the caller provides a non-NULL errlist, it also gives the
2220 * complete list of names of all the properties it failed to set along with the
2221 * corresponding error numbers. The caller is responsible for freeing the
2224 * If every property is set successfully, zero is returned and the list pointed
2225 * at by errlist is NULL.
2228 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2236 nvlist_t *genericnvl;
2240 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2241 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2242 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2246 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2247 const char *propname = nvpair_name(pair);
2248 zfs_prop_t prop = zfs_name_to_prop(propname);
2251 /* decode the property value */
2253 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2255 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2256 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2261 /* Validate value type */
2262 if (err == 0 && prop == ZPROP_INVAL) {
2263 if (zfs_prop_user(propname)) {
2264 if (nvpair_type(propval) != DATA_TYPE_STRING)
2266 } else if (zfs_prop_userquota(propname)) {
2267 if (nvpair_type(propval) !=
2268 DATA_TYPE_UINT64_ARRAY)
2271 } else if (err == 0) {
2272 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2273 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2275 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2278 VERIFY(nvpair_value_uint64(propval,
2281 switch (zfs_prop_get_type(prop)) {
2282 case PROP_TYPE_NUMBER:
2284 case PROP_TYPE_STRING:
2287 case PROP_TYPE_INDEX:
2288 if (zfs_prop_index_to_string(prop,
2289 intval, &unused) != 0)
2294 "unknown property type");
2301 /* Validate permissions */
2303 err = zfs_check_settable(dsname, pair, CRED());
2306 err = zfs_prop_set_special(dsname, source, pair);
2309 * For better performance we build up a list of
2310 * properties to set in a single transaction.
2312 err = nvlist_add_nvpair(genericnvl, pair);
2313 } else if (err != 0 && nvl != retrynvl) {
2315 * This may be a spurious error caused by
2316 * receiving quota and reservation out of order.
2317 * Try again in a second pass.
2319 err = nvlist_add_nvpair(retrynvl, pair);
2324 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2327 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2332 if (!nvlist_empty(genericnvl) &&
2333 dsl_props_set(dsname, source, genericnvl) != 0) {
2335 * If this fails, we still want to set as many properties as we
2336 * can, so try setting them individually.
2339 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2340 const char *propname = nvpair_name(pair);
2344 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2346 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2347 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2351 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2352 VERIFY(nvpair_value_string(propval,
2354 err = dsl_prop_set(dsname, propname, source, 1,
2355 strlen(strval) + 1, strval);
2357 VERIFY(nvpair_value_uint64(propval,
2359 err = dsl_prop_set(dsname, propname, source, 8,
2364 VERIFY(nvlist_add_int32(errors, propname,
2369 nvlist_free(genericnvl);
2370 nvlist_free(retrynvl);
2372 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2373 nvlist_free(errors);
2376 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2379 if (errlist == NULL)
2380 nvlist_free(errors);
2388 * Check that all the properties are valid user properties.
2391 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2393 nvpair_t *pair = NULL;
2396 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2397 const char *propname = nvpair_name(pair);
2400 if (!zfs_prop_user(propname) ||
2401 nvpair_type(pair) != DATA_TYPE_STRING)
2404 if ((error = zfs_secpolicy_write_perms(fsname,
2405 ZFS_DELEG_PERM_USERPROP, CRED())))
2408 if (strlen(propname) >= ZAP_MAXNAMELEN)
2409 return (ENAMETOOLONG);
2411 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2412 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2419 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2423 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2426 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2427 if (nvlist_exists(skipped, nvpair_name(pair)))
2430 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2435 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2439 nvlist_t *cleared_props = NULL;
2440 props_skip(props, skipped, &cleared_props);
2441 if (!nvlist_empty(cleared_props)) {
2443 * Acts on local properties until the dataset has received
2444 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2446 zprop_source_t flags = (ZPROP_SRC_NONE |
2447 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2448 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2450 nvlist_free(cleared_props);
2456 * zc_name name of filesystem
2457 * zc_value name of property to set
2458 * zc_nvlist_src{_size} nvlist of properties to apply
2459 * zc_cookie received properties flag
2462 * zc_nvlist_dst{_size} error for each unapplied received property
2465 zfs_ioc_set_prop(zfs_cmd_t *zc)
2468 boolean_t received = zc->zc_cookie;
2469 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2471 nvlist_t *errors = NULL;
2474 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2475 zc->zc_iflags, &nvl)) != 0)
2479 nvlist_t *origprops;
2482 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2483 if (dsl_prop_get_received(os, &origprops) == 0) {
2484 (void) clear_received_props(os,
2485 zc->zc_name, origprops, nvl);
2486 nvlist_free(origprops);
2489 dsl_prop_set_hasrecvd(os);
2490 dmu_objset_rele(os, FTAG);
2494 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2496 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2497 (void) put_nvlist(zc, errors);
2500 nvlist_free(errors);
2507 * zc_name name of filesystem
2508 * zc_value name of property to inherit
2509 * zc_cookie revert to received value if TRUE
2514 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2516 const char *propname = zc->zc_value;
2517 zfs_prop_t prop = zfs_name_to_prop(propname);
2518 boolean_t received = zc->zc_cookie;
2519 zprop_source_t source = (received
2520 ? ZPROP_SRC_NONE /* revert to received value, if any */
2521 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2530 * zfs_prop_set_special() expects properties in the form of an
2531 * nvpair with type info.
2533 if (prop == ZPROP_INVAL) {
2534 if (!zfs_prop_user(propname))
2537 type = PROP_TYPE_STRING;
2538 } else if (prop == ZFS_PROP_VOLSIZE ||
2539 prop == ZFS_PROP_VERSION) {
2542 type = zfs_prop_get_type(prop);
2545 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2548 case PROP_TYPE_STRING:
2549 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2551 case PROP_TYPE_NUMBER:
2552 case PROP_TYPE_INDEX:
2553 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2560 pair = nvlist_next_nvpair(dummy, NULL);
2561 err = zfs_prop_set_special(zc->zc_name, source, pair);
2564 return (err); /* special property already handled */
2567 * Only check this in the non-received case. We want to allow
2568 * 'inherit -S' to revert non-inheritable properties like quota
2569 * and reservation to the received or default values even though
2570 * they are not considered inheritable.
2572 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2576 /* the property name has been validated by zfs_secpolicy_inherit() */
2577 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2581 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2588 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2589 zc->zc_iflags, &props)))
2593 * If the only property is the configfile, then just do a spa_lookup()
2594 * to handle the faulted case.
2596 pair = nvlist_next_nvpair(props, NULL);
2597 if (pair != NULL && strcmp(nvpair_name(pair),
2598 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2599 nvlist_next_nvpair(props, pair) == NULL) {
2600 mutex_enter(&spa_namespace_lock);
2601 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2602 spa_configfile_set(spa, props, B_FALSE);
2603 spa_config_sync(spa, B_FALSE, B_TRUE);
2605 mutex_exit(&spa_namespace_lock);
2612 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2617 error = spa_prop_set(spa, props);
2620 spa_close(spa, FTAG);
2626 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2630 nvlist_t *nvp = NULL;
2632 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2634 * If the pool is faulted, there may be properties we can still
2635 * get (such as altroot and cachefile), so attempt to get them
2638 mutex_enter(&spa_namespace_lock);
2639 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2640 error = spa_prop_get(spa, &nvp);
2641 mutex_exit(&spa_namespace_lock);
2643 error = spa_prop_get(spa, &nvp);
2644 spa_close(spa, FTAG);
2647 if (error == 0 && zc->zc_nvlist_dst != 0)
2648 error = put_nvlist(zc, nvp);
2658 * zc_name name of volume
2663 zfs_ioc_create_minor(zfs_cmd_t *zc)
2665 return (zvol_create_minor(zc->zc_name));
2670 * zc_name name of volume
2675 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2677 return (zvol_remove_minor(zc->zc_name));
2682 * zc_name name of filesystem
2683 * zc_nvlist_src{_size} nvlist of delegated permissions
2684 * zc_perm_action allow/unallow flag
2689 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2692 nvlist_t *fsaclnv = NULL;
2694 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2695 zc->zc_iflags, &fsaclnv)) != 0)
2699 * Verify nvlist is constructed correctly
2701 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2702 nvlist_free(fsaclnv);
2707 * If we don't have PRIV_SYS_MOUNT, then validate
2708 * that user is allowed to hand out each permission in
2712 error = secpolicy_zfs(CRED());
2714 if (zc->zc_perm_action == B_FALSE) {
2715 error = dsl_deleg_can_allow(zc->zc_name,
2718 error = dsl_deleg_can_unallow(zc->zc_name,
2724 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2726 nvlist_free(fsaclnv);
2732 * zc_name name of filesystem
2735 * zc_nvlist_src{_size} nvlist of delegated permissions
2738 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2743 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2744 error = put_nvlist(zc, nvp);
2753 * Search the vfs list for a specified resource. Returns a pointer to it
2754 * or NULL if no suitable entry is found. The caller of this routine
2755 * is responsible for releasing the returned vfs pointer.
2758 zfs_get_vfs(const char *resource)
2761 struct vfs *vfs_found = NULL;
2763 vfs_list_read_lock();
2766 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2771 vfsp = vfsp->vfs_next;
2772 } while (vfsp != rootvfs);
2776 #endif /* HAVE_ZPL */
2780 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2782 zfs_creat_t *zct = arg;
2784 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2787 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2791 * createprops list of properties requested by creator
2792 * default_zplver zpl version to use if unspecified in createprops
2793 * fuids_ok fuids allowed in this version of the spa?
2794 * os parent objset pointer (NULL if root fs)
2797 * zplprops values for the zplprops we attach to the master node object
2798 * is_ci true if requested file system will be purely case-insensitive
2800 * Determine the settings for utf8only, normalization and
2801 * casesensitivity. Specific values may have been requested by the
2802 * creator and/or we can inherit values from the parent dataset. If
2803 * the file system is of too early a vintage, a creator can not
2804 * request settings for these properties, even if the requested
2805 * setting is the default value. We don't actually want to create dsl
2806 * properties for these, so remove them from the source nvlist after
2810 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2811 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2812 nvlist_t *zplprops, boolean_t *is_ci)
2814 uint64_t sense = ZFS_PROP_UNDEFINED;
2815 uint64_t norm = ZFS_PROP_UNDEFINED;
2816 uint64_t u8 = ZFS_PROP_UNDEFINED;
2818 ASSERT(zplprops != NULL);
2821 * Pull out creator prop choices, if any.
2824 (void) nvlist_lookup_uint64(createprops,
2825 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2826 (void) nvlist_lookup_uint64(createprops,
2827 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2828 (void) nvlist_remove_all(createprops,
2829 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2830 (void) nvlist_lookup_uint64(createprops,
2831 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2832 (void) nvlist_remove_all(createprops,
2833 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2834 (void) nvlist_lookup_uint64(createprops,
2835 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2836 (void) nvlist_remove_all(createprops,
2837 zfs_prop_to_name(ZFS_PROP_CASE));
2841 * If the zpl version requested is whacky or the file system
2842 * or pool is version is too "young" to support normalization
2843 * and the creator tried to set a value for one of the props,
2846 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2847 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2848 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2849 (zplver < ZPL_VERSION_NORMALIZATION &&
2850 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2851 sense != ZFS_PROP_UNDEFINED)))
2855 * Put the version in the zplprops
2857 VERIFY(nvlist_add_uint64(zplprops,
2858 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2860 if (norm == ZFS_PROP_UNDEFINED)
2861 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2862 VERIFY(nvlist_add_uint64(zplprops,
2863 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2866 * If we're normalizing, names must always be valid UTF-8 strings.
2870 if (u8 == ZFS_PROP_UNDEFINED)
2871 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2872 VERIFY(nvlist_add_uint64(zplprops,
2873 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2875 if (sense == ZFS_PROP_UNDEFINED)
2876 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2877 VERIFY(nvlist_add_uint64(zplprops,
2878 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2881 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2887 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2888 nvlist_t *zplprops, boolean_t *is_ci)
2890 boolean_t fuids_ok, sa_ok;
2891 uint64_t zplver = ZPL_VERSION;
2892 objset_t *os = NULL;
2893 char parentname[MAXNAMELEN];
2899 (void) strlcpy(parentname, dataset, sizeof (parentname));
2900 cp = strrchr(parentname, '/');
2904 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2907 spa_vers = spa_version(spa);
2908 spa_close(spa, FTAG);
2910 zplver = zfs_zpl_version_map(spa_vers);
2911 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2912 sa_ok = (zplver >= ZPL_VERSION_SA);
2915 * Open parent object set so we can inherit zplprop values.
2917 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2920 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2922 dmu_objset_rele(os, FTAG);
2927 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2928 nvlist_t *zplprops, boolean_t *is_ci)
2932 uint64_t zplver = ZPL_VERSION;
2935 zplver = zfs_zpl_version_map(spa_vers);
2936 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2937 sa_ok = (zplver >= ZPL_VERSION_SA);
2939 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2940 createprops, zplprops, is_ci);
2946 * zc_objset_type type of objset to create (fs vs zvol)
2947 * zc_name name of new objset
2948 * zc_value name of snapshot to clone from (may be empty)
2949 * zc_nvlist_src{_size} nvlist of properties to apply
2954 zfs_ioc_create(zfs_cmd_t *zc)
2959 nvlist_t *nvprops = NULL;
2960 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2961 dmu_objset_type_t type = zc->zc_objset_type;
2966 cbfunc = zfs_create_cb;
2970 cbfunc = zvol_create_cb;
2977 if (strchr(zc->zc_name, '@') ||
2978 strchr(zc->zc_name, '%'))
2981 if (zc->zc_nvlist_src != 0 &&
2982 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2983 zc->zc_iflags, &nvprops)) != 0)
2986 zct.zct_zplprops = NULL;
2987 zct.zct_props = nvprops;
2989 if (zc->zc_value[0] != '\0') {
2991 * We're creating a clone of an existing snapshot.
2993 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2994 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2995 nvlist_free(nvprops);
2999 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
3001 nvlist_free(nvprops);
3005 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
3006 dmu_objset_rele(clone, FTAG);
3008 nvlist_free(nvprops);
3012 boolean_t is_insensitive = B_FALSE;
3014 if (cbfunc == NULL) {
3015 nvlist_free(nvprops);
3019 if (type == DMU_OST_ZVOL) {
3020 uint64_t volsize, volblocksize;
3022 if (nvprops == NULL ||
3023 nvlist_lookup_uint64(nvprops,
3024 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3026 nvlist_free(nvprops);
3030 if ((error = nvlist_lookup_uint64(nvprops,
3031 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3032 &volblocksize)) != 0 && error != ENOENT) {
3033 nvlist_free(nvprops);
3038 volblocksize = zfs_prop_default_numeric(
3039 ZFS_PROP_VOLBLOCKSIZE);
3041 if ((error = zvol_check_volblocksize(
3042 volblocksize)) != 0 ||
3043 (error = zvol_check_volsize(volsize,
3044 volblocksize)) != 0) {
3045 nvlist_free(nvprops);
3048 } else if (type == DMU_OST_ZFS) {
3052 * We have to have normalization and
3053 * case-folding flags correct when we do the
3054 * file system creation, so go figure them out
3057 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3058 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3059 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3060 zct.zct_zplprops, &is_insensitive);
3062 nvlist_free(nvprops);
3063 nvlist_free(zct.zct_zplprops);
3067 error = dmu_objset_create(zc->zc_name, type,
3068 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3069 nvlist_free(zct.zct_zplprops);
3073 * It would be nice to do this atomically.
3076 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3079 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3081 nvlist_free(nvprops);
3087 * zc_name name of filesystem
3088 * zc_value short name of snapshot
3089 * zc_cookie recursive flag
3090 * zc_nvlist_src[_size] property list
3093 * zc_value short snapname (i.e. part after the '@')
3096 zfs_ioc_snapshot(zfs_cmd_t *zc)
3098 nvlist_t *nvprops = NULL;
3100 boolean_t recursive = zc->zc_cookie;
3102 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3105 if (zc->zc_nvlist_src != 0 &&
3106 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3107 zc->zc_iflags, &nvprops)) != 0)
3110 error = zfs_check_userprops(zc->zc_name, nvprops);
3114 if (!nvlist_empty(nvprops) &&
3115 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3120 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3121 nvprops, recursive, B_FALSE, -1);
3124 nvlist_free(nvprops);
3129 zfs_unmount_snap(const char *name, void *arg)
3135 char *snapname = arg;
3136 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3137 vfsp = zfs_get_vfs(fullname);
3139 } else if (strchr(name, '@')) {
3140 vfsp = zfs_get_vfs(name);
3145 * Always force the unmount for snapshots.
3147 int flag = MS_FORCE;
3150 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3155 if ((err = dounmount(vfsp, flag, kcred)) != 0)
3158 #endif /* HAVE_ZPL */
3164 * zc_name name of filesystem
3165 * zc_value short name of snapshot
3166 * zc_defer_destroy mark for deferred destroy
3171 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3175 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3177 err = dmu_objset_find(zc->zc_name,
3178 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3181 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3182 zc->zc_defer_destroy));
3187 * zc_name name of dataset to destroy
3188 * zc_objset_type type of objset
3189 * zc_defer_destroy mark for deferred destroy
3194 zfs_ioc_destroy(zfs_cmd_t *zc)
3197 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3198 err = zfs_unmount_snap(zc->zc_name, NULL);
3203 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3204 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3205 (void) zvol_remove_minor(zc->zc_name);
3211 * zc_name name of dataset to rollback (to most recent snapshot)
3216 zfs_ioc_rollback(zfs_cmd_t *zc)
3219 dsl_dataset_t *ds, *clone;
3224 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3228 /* must not be a snapshot */
3229 if (dsl_dataset_is_snapshot(ds)) {
3230 dsl_dataset_rele(ds, FTAG);
3234 /* must have a most recent snapshot */
3235 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3236 dsl_dataset_rele(ds, FTAG);
3241 * Create clone of most recent snapshot.
3243 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3244 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3248 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3255 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3256 error = zfs_suspend_fs(zfsvfs);
3260 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3261 error = dsl_dataset_clone_swap(clone, ds,
3263 dsl_dataset_disown(ds, FTAG);
3268 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3269 error = error ? error : resume_err;
3271 VFS_RELE(zfsvfs->z_vfs);
3273 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3274 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3275 dsl_dataset_disown(ds, FTAG);
3283 * Destroy clone (which also closes it).
3285 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3288 strfree(clone_name);
3290 dsl_dataset_rele(ds, FTAG);
3294 #endif /* HAVE_ZPL */
3299 * zc_name old name of dataset
3300 * zc_value new name of dataset
3301 * zc_cookie recursive flag (only valid for snapshots)
3306 zfs_ioc_rename(zfs_cmd_t *zc)
3308 boolean_t recursive = zc->zc_cookie & 1;
3311 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3312 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3313 strchr(zc->zc_value, '%'))
3317 * Unmount snapshot unless we're doing a recursive rename,
3318 * in which case the dataset code figures out which snapshots
3321 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3322 zc->zc_objset_type == DMU_OST_ZFS) {
3323 err = zfs_unmount_snap(zc->zc_name, NULL);
3328 err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3329 if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3330 (void) zvol_remove_minor(zc->zc_name);
3331 (void) zvol_create_minor(zc->zc_value);
3338 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3340 const char *propname = nvpair_name(pair);
3341 boolean_t issnap = (strchr(dsname, '@') != NULL);
3342 zfs_prop_t prop = zfs_name_to_prop(propname);
3346 if (prop == ZPROP_INVAL) {
3347 if (zfs_prop_user(propname)) {
3348 if ((err = zfs_secpolicy_write_perms(dsname,
3349 ZFS_DELEG_PERM_USERPROP, cr)))
3354 if (!issnap && zfs_prop_userquota(propname)) {
3355 const char *perm = NULL;
3356 const char *uq_prefix =
3357 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3358 const char *gq_prefix =
3359 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3361 if (strncmp(propname, uq_prefix,
3362 strlen(uq_prefix)) == 0) {
3363 perm = ZFS_DELEG_PERM_USERQUOTA;
3364 } else if (strncmp(propname, gq_prefix,
3365 strlen(gq_prefix)) == 0) {
3366 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3368 /* USERUSED and GROUPUSED are read-only */
3372 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3383 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3385 * dsl_prop_get_all_impl() returns properties in this
3389 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3390 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3395 * Check that this value is valid for this pool version
3398 case ZFS_PROP_COMPRESSION:
3400 * If the user specified gzip compression, make sure
3401 * the SPA supports it. We ignore any errors here since
3402 * we'll catch them later.
3404 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3405 nvpair_value_uint64(pair, &intval) == 0) {
3406 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3407 intval <= ZIO_COMPRESS_GZIP_9 &&
3408 zfs_earlier_version(dsname,
3409 SPA_VERSION_GZIP_COMPRESSION)) {
3413 if (intval == ZIO_COMPRESS_ZLE &&
3414 zfs_earlier_version(dsname,
3415 SPA_VERSION_ZLE_COMPRESSION))
3419 * If this is a bootable dataset then
3420 * verify that the compression algorithm
3421 * is supported for booting. We must return
3422 * something other than ENOTSUP since it
3423 * implies a downrev pool version.
3425 if (zfs_is_bootfs(dsname) &&
3426 !BOOTFS_COMPRESS_VALID(intval)) {
3432 case ZFS_PROP_COPIES:
3433 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3437 case ZFS_PROP_DEDUP:
3438 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3442 case ZFS_PROP_SHARESMB:
3443 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3447 case ZFS_PROP_ACLINHERIT:
3448 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3449 nvpair_value_uint64(pair, &intval) == 0) {
3450 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3451 zfs_earlier_version(dsname,
3452 SPA_VERSION_PASSTHROUGH_X))
3460 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3464 * Removes properties from the given props list that fail permission checks
3465 * needed to clear them and to restore them in case of a receive error. For each
3466 * property, make sure we have both set and inherit permissions.
3468 * Returns the first error encountered if any permission checks fail. If the
3469 * caller provides a non-NULL errlist, it also gives the complete list of names
3470 * of all the properties that failed a permission check along with the
3471 * corresponding error numbers. The caller is responsible for freeing the
3474 * If every property checks out successfully, zero is returned and the list
3475 * pointed at by errlist is NULL.
3478 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3481 nvpair_t *pair, *next_pair;
3488 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3490 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3491 (void) strcpy(zc->zc_name, dataset);
3492 pair = nvlist_next_nvpair(props, NULL);
3493 while (pair != NULL) {
3494 next_pair = nvlist_next_nvpair(props, pair);
3496 (void) strcpy(zc->zc_value, nvpair_name(pair));
3497 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3498 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3499 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3500 VERIFY(nvlist_add_int32(errors,
3501 zc->zc_value, err) == 0);
3505 kmem_free(zc, sizeof (zfs_cmd_t));
3507 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3508 nvlist_free(errors);
3511 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3514 if (errlist == NULL)
3515 nvlist_free(errors);
3523 propval_equals(nvpair_t *p1, nvpair_t *p2)
3525 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3526 /* dsl_prop_get_all_impl() format */
3528 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3529 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3533 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3535 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3536 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3540 if (nvpair_type(p1) != nvpair_type(p2))
3543 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3544 char *valstr1, *valstr2;
3546 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3547 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3548 return (strcmp(valstr1, valstr2) == 0);
3550 uint64_t intval1, intval2;
3552 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3553 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3554 return (intval1 == intval2);
3559 * Remove properties from props if they are not going to change (as determined
3560 * by comparison with origprops). Remove them from origprops as well, since we
3561 * do not need to clear or restore properties that won't change.
3564 props_reduce(nvlist_t *props, nvlist_t *origprops)
3566 nvpair_t *pair, *next_pair;
3568 if (origprops == NULL)
3569 return; /* all props need to be received */
3571 pair = nvlist_next_nvpair(props, NULL);
3572 while (pair != NULL) {
3573 const char *propname = nvpair_name(pair);
3576 next_pair = nvlist_next_nvpair(props, pair);
3578 if ((nvlist_lookup_nvpair(origprops, propname,
3579 &match) != 0) || !propval_equals(pair, match))
3580 goto next; /* need to set received value */
3582 /* don't clear the existing received value */
3583 (void) nvlist_remove_nvpair(origprops, match);
3584 /* don't bother receiving the property */
3585 (void) nvlist_remove_nvpair(props, pair);
3592 static boolean_t zfs_ioc_recv_inject_err;
3597 * zc_name name of containing filesystem
3598 * zc_nvlist_src{_size} nvlist of properties to apply
3599 * zc_value name of snapshot to create
3600 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3601 * zc_cookie file descriptor to recv from
3602 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3603 * zc_guid force flag
3604 * zc_cleanup_fd cleanup-on-exit file descriptor
3605 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3608 * zc_cookie number of bytes read
3609 * zc_nvlist_dst{_size} error for each unapplied received property
3610 * zc_obj zprop_errflags_t
3611 * zc_action_handle handle for this guid/ds mapping
3614 zfs_ioc_recv(zfs_cmd_t *zc)
3618 dmu_recv_cookie_t drc;
3619 boolean_t force = (boolean_t)zc->zc_guid;
3622 int props_error = 0;
3625 nvlist_t *props = NULL; /* sent properties */
3626 nvlist_t *origprops = NULL; /* existing properties */
3627 objset_t *origin = NULL;
3629 char tofs[ZFS_MAXNAMELEN];
3630 boolean_t first_recvd_props = B_FALSE;
3632 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3633 strchr(zc->zc_value, '@') == NULL ||
3634 strchr(zc->zc_value, '%'))
3637 (void) strcpy(tofs, zc->zc_value);
3638 tosnap = strchr(tofs, '@');
3641 if (zc->zc_nvlist_src != 0 &&
3642 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3643 zc->zc_iflags, &props)) != 0)
3653 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3655 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3656 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3657 !dsl_prop_get_hasrecvd(os)) {
3658 first_recvd_props = B_TRUE;
3662 * If new received properties are supplied, they are to
3663 * completely replace the existing received properties, so stash
3664 * away the existing ones.
3666 if (dsl_prop_get_received(os, &origprops) == 0) {
3667 nvlist_t *errlist = NULL;
3669 * Don't bother writing a property if its value won't
3670 * change (and avoid the unnecessary security checks).
3672 * The first receive after SPA_VERSION_RECVD_PROPS is a
3673 * special case where we blow away all local properties
3676 if (!first_recvd_props)
3677 props_reduce(props, origprops);
3678 if (zfs_check_clearable(tofs, origprops,
3680 (void) nvlist_merge(errors, errlist, 0);
3681 nvlist_free(errlist);
3684 dmu_objset_rele(os, FTAG);
3687 if (zc->zc_string[0]) {
3688 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3693 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3694 &zc->zc_begin_record, force, origin, &drc);
3696 dmu_objset_rele(origin, FTAG);
3701 * Set properties before we receive the stream so that they are applied
3702 * to the new data. Note that we must call dmu_recv_stream() if
3703 * dmu_recv_begin() succeeds.
3708 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3709 if (drc.drc_newfs) {
3710 if (spa_version(os->os_spa) >=
3711 SPA_VERSION_RECVD_PROPS)
3712 first_recvd_props = B_TRUE;
3713 } else if (origprops != NULL) {
3714 if (clear_received_props(os, tofs, origprops,
3715 first_recvd_props ? NULL : props) != 0)
3716 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3718 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3720 dsl_prop_set_hasrecvd(os);
3721 } else if (!drc.drc_newfs) {
3722 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3725 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3727 (void) nvlist_merge(errors, errlist, 0);
3728 nvlist_free(errlist);
3731 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3733 * Caller made zc->zc_nvlist_dst less than the minimum expected
3734 * size or supplied an invalid address.
3736 props_error = EINVAL;
3740 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3741 &zc->zc_action_handle);
3745 zfsvfs_t *zfsvfs = NULL;
3747 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3751 error = zfs_suspend_fs(zfsvfs);
3753 * If the suspend fails, then the recv_end will
3754 * likely also fail, and clean up after itself.
3756 end_err = dmu_recv_end(&drc);
3758 error = zfs_resume_fs(zfsvfs, tofs);
3759 error = error ? error : end_err;
3760 VFS_RELE(zfsvfs->z_vfs);
3762 error = dmu_recv_end(&drc);
3765 error = dmu_recv_end(&drc);
3766 #endif /* HAVE_ZPL */
3769 zc->zc_cookie = off - fp->f_offset;
3770 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3774 if (zfs_ioc_recv_inject_err) {
3775 zfs_ioc_recv_inject_err = B_FALSE;
3780 * On error, restore the original props.
3782 if (error && props) {
3783 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3784 if (clear_received_props(os, tofs, props, NULL) != 0) {
3786 * We failed to clear the received properties.
3787 * Since we may have left a $recvd value on the
3788 * system, we can't clear the $hasrecvd flag.
3790 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3791 } else if (first_recvd_props) {
3792 dsl_prop_unset_hasrecvd(os);
3794 dmu_objset_rele(os, FTAG);
3795 } else if (!drc.drc_newfs) {
3796 /* We failed to clear the received properties. */
3797 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3800 if (origprops == NULL && !drc.drc_newfs) {
3801 /* We failed to stash the original properties. */
3802 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3806 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3807 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3808 * explictly if we're restoring local properties cleared in the
3809 * first new-style receive.
3811 if (origprops != NULL &&
3812 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3813 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3814 origprops, NULL) != 0) {
3816 * We stashed the original properties but failed to
3819 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3824 nvlist_free(origprops);
3825 nvlist_free(errors);
3829 error = props_error;
3836 * zc_name name of snapshot to send
3837 * zc_cookie file descriptor to send stream to
3838 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3839 * zc_sendobj objsetid of snapshot to send
3840 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3845 zfs_ioc_send(zfs_cmd_t *zc)
3847 objset_t *fromsnap = NULL;
3853 dsl_dataset_t *dsfrom = NULL;
3857 error = spa_open(zc->zc_name, &spa, FTAG);
3861 dp = spa_get_dsl(spa);
3862 rw_enter(&dp->dp_config_rwlock, RW_READER);
3863 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3864 rw_exit(&dp->dp_config_rwlock);
3866 spa_close(spa, FTAG);
3870 error = dmu_objset_from_ds(ds, &tosnap);
3872 dsl_dataset_rele(ds, FTAG);
3873 spa_close(spa, FTAG);
3877 if (zc->zc_fromobj != 0) {
3878 rw_enter(&dp->dp_config_rwlock, RW_READER);
3879 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3880 rw_exit(&dp->dp_config_rwlock);
3881 spa_close(spa, FTAG);
3883 dsl_dataset_rele(ds, FTAG);
3886 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3888 dsl_dataset_rele(dsfrom, FTAG);
3889 dsl_dataset_rele(ds, FTAG);
3893 spa_close(spa, FTAG);
3896 fp = getf(zc->zc_cookie);
3898 dsl_dataset_rele(ds, FTAG);
3900 dsl_dataset_rele(dsfrom, FTAG);
3905 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3907 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3909 releasef(zc->zc_cookie);
3911 dsl_dataset_rele(dsfrom, FTAG);
3912 dsl_dataset_rele(ds, FTAG);
3917 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3921 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3922 &zc->zc_inject_record);
3925 zc->zc_guid = (uint64_t)id;
3931 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3933 return (zio_clear_fault((int)zc->zc_guid));
3937 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3939 int id = (int)zc->zc_guid;
3942 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3943 &zc->zc_inject_record);
3951 zfs_ioc_error_log(zfs_cmd_t *zc)
3955 size_t count = (size_t)zc->zc_nvlist_dst_size;
3957 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3960 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3963 zc->zc_nvlist_dst_size = count;
3965 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3967 spa_close(spa, FTAG);
3973 zfs_ioc_clear(zfs_cmd_t *zc)
3980 * On zpool clear we also fix up missing slogs
3982 mutex_enter(&spa_namespace_lock);
3983 spa = spa_lookup(zc->zc_name);
3985 mutex_exit(&spa_namespace_lock);
3988 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3989 /* we need to let spa_open/spa_load clear the chains */
3990 spa_set_log_state(spa, SPA_LOG_CLEAR);
3992 spa->spa_last_open_failed = 0;
3993 mutex_exit(&spa_namespace_lock);
3995 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3996 error = spa_open(zc->zc_name, &spa, FTAG);
3999 nvlist_t *config = NULL;
4001 if (zc->zc_nvlist_src == 0)
4004 if ((error = get_nvlist(zc->zc_nvlist_src,
4005 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4006 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4008 if (config != NULL) {
4011 if ((err = put_nvlist(zc, config)) != 0)
4013 nvlist_free(config);
4015 nvlist_free(policy);
4022 spa_vdev_state_enter(spa, SCL_NONE);
4024 if (zc->zc_guid == 0) {
4027 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4029 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4030 spa_close(spa, FTAG);
4035 vdev_clear(spa, vd);
4037 (void) spa_vdev_state_exit(spa, NULL, 0);
4040 * Resume any suspended I/Os.
4042 if (zio_resume(spa) != 0)
4045 spa_close(spa, FTAG);
4052 * zc_name name of filesystem
4053 * zc_value name of origin snapshot
4056 * zc_string name of conflicting snapshot, if there is one
4059 zfs_ioc_promote(zfs_cmd_t *zc)
4064 * We don't need to unmount *all* the origin fs's snapshots, but
4067 cp = strchr(zc->zc_value, '@');
4070 (void) dmu_objset_find(zc->zc_value,
4071 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4072 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4076 * Retrieve a single {user|group}{used|quota}@... property.
4079 * zc_name name of filesystem
4080 * zc_objset_type zfs_userquota_prop_t
4081 * zc_value domain name (eg. "S-1-234-567-89")
4082 * zc_guid RID/UID/GID
4085 * zc_cookie property value
4088 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4094 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4097 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4101 error = zfs_userspace_one(zfsvfs,
4102 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4103 zfsvfs_rele(zfsvfs, FTAG);
4108 #endif /* HAVE_ZPL */
4113 * zc_name name of filesystem
4114 * zc_cookie zap cursor
4115 * zc_objset_type zfs_userquota_prop_t
4116 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4119 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4120 * zc_cookie zap cursor
4123 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4127 int bufsize = zc->zc_nvlist_dst_size;
4132 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4136 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4138 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4139 buf, &zc->zc_nvlist_dst_size);
4142 error = xcopyout(buf,
4143 (void *)(uintptr_t)zc->zc_nvlist_dst,
4144 zc->zc_nvlist_dst_size);
4146 kmem_free(buf, bufsize);
4147 zfsvfs_rele(zfsvfs, FTAG);
4152 #endif /* HAVE_ZPL */
4157 * zc_name name of filesystem
4163 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4170 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4171 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4173 * If userused is not enabled, it may be because the
4174 * objset needs to be closed & reopened (to grow the
4175 * objset_phys_t). Suspend/resume the fs will do that.
4177 error = zfs_suspend_fs(zfsvfs);
4179 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4182 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4183 VFS_RELE(zfsvfs->z_vfs);
4185 /* XXX kind of reading contents without owning */
4186 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4190 error = dmu_objset_userspace_upgrade(os);
4191 dmu_objset_rele(os, FTAG);
4197 #endif /* HAVE_ZPL */
4201 * We don't want to have a hard dependency
4202 * against some special symbols in sharefs
4203 * nfs, and smbsrv. Determine them if needed when
4204 * the first file system is shared.
4205 * Neither sharefs, nfs or smbsrv are unloadable modules.
4208 int (*znfsexport_fs)(void *arg);
4209 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4210 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4212 int zfs_nfsshare_inited;
4213 int zfs_smbshare_inited;
4215 ddi_modhandle_t nfs_mod;
4216 ddi_modhandle_t sharefs_mod;
4217 ddi_modhandle_t smbsrv_mod;
4218 kmutex_t zfs_share_lock;
4225 ASSERT(MUTEX_HELD(&zfs_share_lock));
4226 /* Both NFS and SMB shares also require sharetab support. */
4227 if (sharefs_mod == NULL && ((sharefs_mod =
4228 ddi_modopen("fs/sharefs",
4229 KRTLD_MODE_FIRST, &error)) == NULL)) {
4232 if (zshare_fs == NULL && ((zshare_fs =
4233 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4234 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4239 #endif /* HAVE_SHARE */
4242 zfs_ioc_share(zfs_cmd_t *zc)
4248 switch (zc->zc_share.z_sharetype) {
4250 case ZFS_UNSHARE_NFS:
4251 if (zfs_nfsshare_inited == 0) {
4252 mutex_enter(&zfs_share_lock);
4253 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4254 KRTLD_MODE_FIRST, &error)) == NULL)) {
4255 mutex_exit(&zfs_share_lock);
4258 if (znfsexport_fs == NULL &&
4259 ((znfsexport_fs = (int (*)(void *))
4261 "nfs_export", &error)) == NULL)) {
4262 mutex_exit(&zfs_share_lock);
4265 error = zfs_init_sharefs();
4267 mutex_exit(&zfs_share_lock);
4270 zfs_nfsshare_inited = 1;
4271 mutex_exit(&zfs_share_lock);
4275 case ZFS_UNSHARE_SMB:
4276 if (zfs_smbshare_inited == 0) {
4277 mutex_enter(&zfs_share_lock);
4278 if (smbsrv_mod == NULL && ((smbsrv_mod =
4279 ddi_modopen("drv/smbsrv",
4280 KRTLD_MODE_FIRST, &error)) == NULL)) {
4281 mutex_exit(&zfs_share_lock);
4284 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4285 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4286 "smb_server_share", &error)) == NULL)) {
4287 mutex_exit(&zfs_share_lock);
4290 error = zfs_init_sharefs();
4292 mutex_exit(&zfs_share_lock);
4295 zfs_smbshare_inited = 1;
4296 mutex_exit(&zfs_share_lock);
4303 switch (zc->zc_share.z_sharetype) {
4305 case ZFS_UNSHARE_NFS:
4307 znfsexport_fs((void *)
4308 (uintptr_t)zc->zc_share.z_exportdata))
4312 case ZFS_UNSHARE_SMB:
4313 if (error = zsmbexport_fs((void *)
4314 (uintptr_t)zc->zc_share.z_exportdata,
4315 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4322 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4323 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4324 SHAREFS_ADD : SHAREFS_REMOVE;
4327 * Add or remove share from sharetab
4329 error = zshare_fs(opcode,
4330 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4331 zc->zc_share.z_sharemax);
4336 #endif /* HAVE_SHARE */
4339 ace_t full_access[] = {
4340 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4345 * zc_name name of containing filesystem
4346 * zc_obj object # beyond which we want next in-use object #
4349 * zc_obj next in-use object #
4352 zfs_ioc_next_obj(zfs_cmd_t *zc)
4354 objset_t *os = NULL;
4357 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4361 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4362 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4364 dmu_objset_rele(os, FTAG);
4370 * zc_name name of filesystem
4371 * zc_value prefix name for snapshot
4372 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4377 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4382 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4383 (u_longlong_t)ddi_get_lbolt64());
4385 if (strlen(snap_name) >= MAXNAMELEN) {
4390 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4391 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4397 (void) strcpy(zc->zc_value, snap_name);
4404 * zc_name name of "to" snapshot
4405 * zc_value name of "from" snapshot
4406 * zc_cookie file descriptor to write diff data on
4409 * dmu_diff_record_t's to the file descriptor
4412 zfs_ioc_diff(zfs_cmd_t *zc)
4420 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4424 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4426 dmu_objset_rele(tosnap, FTAG);
4430 fp = getf(zc->zc_cookie);
4432 dmu_objset_rele(fromsnap, FTAG);
4433 dmu_objset_rele(tosnap, FTAG);
4439 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4441 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4443 releasef(zc->zc_cookie);
4445 dmu_objset_rele(fromsnap, FTAG);
4446 dmu_objset_rele(tosnap, FTAG);
4451 * Remove all ACL files in shares dir
4455 zfs_smb_acl_purge(znode_t *dzp)
4458 zap_attribute_t zap;
4459 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4462 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4463 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4464 zap_cursor_advance(&zc)) {
4465 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4469 zap_cursor_fini(&zc);
4472 #endif /* HAVE SHARE */
4475 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4480 vnode_t *resourcevp = NULL;
4489 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4490 NO_FOLLOW, NULL, &vp)) != 0)
4493 /* Now make sure mntpnt and dataset are ZFS */
4495 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4496 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4497 zc->zc_name) != 0)) {
4503 zfsvfs = dzp->z_zfsvfs;
4507 * Create share dir if its missing.
4509 mutex_enter(&zfsvfs->z_lock);
4510 if (zfsvfs->z_shares_dir == 0) {
4513 tx = dmu_tx_create(zfsvfs->z_os);
4514 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4516 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4517 error = dmu_tx_assign(tx, TXG_WAIT);
4521 error = zfs_create_share_dir(zfsvfs, tx);
4525 mutex_exit(&zfsvfs->z_lock);
4531 mutex_exit(&zfsvfs->z_lock);
4533 ASSERT(zfsvfs->z_shares_dir);
4534 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4540 switch (zc->zc_cookie) {
4541 case ZFS_SMB_ACL_ADD:
4542 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4543 vattr.va_type = VREG;
4544 vattr.va_mode = S_IFREG|0777;
4548 vsec.vsa_mask = VSA_ACE;
4549 vsec.vsa_aclentp = &full_access;
4550 vsec.vsa_aclentsz = sizeof (full_access);
4551 vsec.vsa_aclcnt = 1;
4553 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4554 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4556 VN_RELE(resourcevp);
4559 case ZFS_SMB_ACL_REMOVE:
4560 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4564 case ZFS_SMB_ACL_RENAME:
4565 if ((error = get_nvlist(zc->zc_nvlist_src,
4566 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4571 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4572 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4575 VN_RELE(ZTOV(sharedir));
4577 nvlist_free(nvlist);
4580 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4582 nvlist_free(nvlist);
4585 case ZFS_SMB_ACL_PURGE:
4586 error = zfs_smb_acl_purge(sharedir);
4595 VN_RELE(ZTOV(sharedir));
4602 #endif /* HAVE_SHARE */
4607 * zc_name name of filesystem
4608 * zc_value short name of snap
4609 * zc_string user-supplied tag for this hold
4610 * zc_cookie recursive flag
4611 * zc_temphold set if hold is temporary
4612 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4613 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4614 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4619 zfs_ioc_hold(zfs_cmd_t *zc)
4621 boolean_t recursive = zc->zc_cookie;
4628 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4631 if (zc->zc_sendobj == 0) {
4632 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4633 zc->zc_string, recursive, zc->zc_temphold,
4634 zc->zc_cleanup_fd));
4640 error = spa_open(zc->zc_name, &spa, FTAG);
4644 dp = spa_get_dsl(spa);
4645 rw_enter(&dp->dp_config_rwlock, RW_READER);
4646 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4647 rw_exit(&dp->dp_config_rwlock);
4648 spa_close(spa, FTAG);
4653 * Until we have a hold on this snapshot, it's possible that
4654 * zc_sendobj could've been destroyed and reused as part
4655 * of a later txg. Make sure we're looking at the right object.
4657 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4658 dsl_dataset_rele(ds, FTAG);
4662 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4663 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4665 dsl_dataset_rele(ds, FTAG);
4670 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4674 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4677 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4679 dsl_dataset_rele(ds, FTAG);
4686 * zc_name name of dataset from which we're releasing a user hold
4687 * zc_value short name of snap
4688 * zc_string user-supplied tag for this hold
4689 * zc_cookie recursive flag
4694 zfs_ioc_release(zfs_cmd_t *zc)
4696 boolean_t recursive = zc->zc_cookie;
4698 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4701 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4702 zc->zc_string, recursive));
4707 * zc_name name of filesystem
4710 * zc_nvlist_src{_size} nvlist of snapshot holds
4713 zfs_ioc_get_holds(zfs_cmd_t *zc)
4718 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4719 error = put_nvlist(zc, nvp);
4728 * zc_guid flags (ZEVENT_NONBLOCK)
4731 * zc_nvlist_dst next nvlist event
4732 * zc_cookie dropped events since last get
4733 * zc_cleanup_fd cleanup-on-exit file descriptor
4736 zfs_ioc_events_next(zfs_cmd_t *zc)
4739 nvlist_t *event = NULL;
4741 uint64_t dropped = 0;
4744 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4749 error = zfs_zevent_next(ze, &event,
4750 &zc->zc_nvlist_dst_size, &dropped);
4751 if (event != NULL) {
4752 zc->zc_cookie = dropped;
4753 error = put_nvlist(zc, event);
4757 if (zc->zc_guid & ZEVENT_NONBLOCK)
4760 if ((error == 0) || (error != ENOENT))
4763 error = zfs_zevent_wait(ze);
4768 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4775 * zc_cookie cleared events count
4778 zfs_ioc_events_clear(zfs_cmd_t *zc)
4782 zfs_zevent_drain_all(&count);
4783 zc->zc_cookie = count;
4789 * pool create, destroy, and export don't log the history as part of
4790 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4791 * do the logging of those commands.
4793 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4794 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4796 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4798 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4800 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4802 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4804 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4806 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4808 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4809 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4810 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4811 POOL_CHECK_READONLY },
4812 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4813 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4814 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4816 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4817 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4818 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4819 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4820 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4821 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4822 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4823 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4824 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4825 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4826 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4827 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4828 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4829 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4830 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4831 POOL_CHECK_SUSPENDED },
4832 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4834 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4835 POOL_CHECK_SUSPENDED },
4836 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4837 POOL_CHECK_SUSPENDED },
4838 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4839 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4840 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4842 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4844 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4845 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4846 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4847 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4848 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4849 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4850 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4851 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4852 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4853 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4854 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4856 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4858 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4860 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4862 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4864 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4866 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4867 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4868 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4869 B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4870 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4871 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4872 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4874 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4875 POOL_CHECK_SUSPENDED },
4876 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4877 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4878 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4880 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4881 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4882 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4884 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4886 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4887 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4888 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4890 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4891 B_FALSE, POOL_CHECK_NONE },
4892 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4893 B_FALSE, POOL_CHECK_NONE },
4894 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4895 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4896 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4897 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4898 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4899 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4900 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4901 POOL_CHECK_SUSPENDED },
4902 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4904 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4905 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4906 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4908 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4910 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4911 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4912 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4913 POOL_CHECK_SUSPENDED },
4914 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4916 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4921 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4922 zfs_ioc_poolcheck_t check)
4927 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4929 if (check & POOL_CHECK_NONE)
4932 error = spa_open(name, &spa, FTAG);
4934 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4936 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4938 spa_close(spa, FTAG);
4944 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4948 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4950 for (zs = list_head(&zfsdev_state_list); zs != NULL;
4951 zs = list_next(&zfsdev_state_list, zs)) {
4952 if (zs->zs_minor == minor) {
4954 case ZST_ONEXIT: return (zs->zs_onexit);
4955 case ZST_ZEVENT: return (zs->zs_zevent);
4956 case ZST_ALL: return (zs);
4965 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4969 mutex_enter(&zfsdev_state_lock);
4970 ptr = zfsdev_get_state_impl(minor, which);
4971 mutex_exit(&zfsdev_state_lock);
4977 zfsdev_getminor(struct file *filp)
4979 ASSERT(filp != NULL);
4980 ASSERT(filp->private_data != NULL);
4982 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4986 * Find a free minor number. The zfsdev_state_list is expected to
4987 * be short since it is only a list of currently open file handles.
4990 zfsdev_minor_alloc(void)
4992 static minor_t last_minor = 0;
4995 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4997 for (m = last_minor + 1; m != last_minor; m++) {
4998 if (m > ZFSDEV_MAX_MINOR)
5000 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
5010 zfsdev_state_init(struct file *filp)
5015 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5017 minor = zfsdev_minor_alloc();
5021 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
5026 zs->zs_minor = minor;
5027 filp->private_data = zs;
5029 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
5030 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
5032 list_insert_tail(&zfsdev_state_list, zs);
5038 zfsdev_state_destroy(struct file *filp)
5042 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5043 ASSERT(filp->private_data != NULL);
5045 zs = filp->private_data;
5046 zfs_onexit_destroy(zs->zs_onexit);
5047 zfs_zevent_destroy(zs->zs_zevent);
5049 list_remove(&zfsdev_state_list, zs);
5050 kmem_free(zs, sizeof(zfsdev_state_t));
5056 zfsdev_open(struct inode *ino, struct file *filp)
5060 mutex_enter(&zfsdev_state_lock);
5061 error = zfsdev_state_init(filp);
5062 mutex_exit(&zfsdev_state_lock);
5068 zfsdev_release(struct inode *ino, struct file *filp)
5072 mutex_enter(&zfsdev_state_lock);
5073 error = zfsdev_state_destroy(filp);
5074 mutex_exit(&zfsdev_state_lock);
5080 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5084 int error, rc, flag = 0;
5086 vec = cmd - ZFS_IOC;
5087 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5090 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
5092 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5096 if ((error == 0) && !(flag & FKIOCTL))
5097 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, NULL);
5100 * Ensure that all pool/dataset names are valid before we pass down to
5104 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5105 zc->zc_iflags = flag & FKIOCTL;
5106 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5108 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5110 error = pool_status_check(zc->zc_name,
5111 zfs_ioc_vec[vec].zvec_namecheck,
5112 zfs_ioc_vec[vec].zvec_pool_check);
5116 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5118 error = pool_status_check(zc->zc_name,
5119 zfs_ioc_vec[vec].zvec_namecheck,
5120 zfs_ioc_vec[vec].zvec_pool_check);
5129 error = zfs_ioc_vec[vec].zvec_func(zc);
5131 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5135 if (zfs_ioc_vec[vec].zvec_his_log)
5136 zfs_log_history(zc);
5139 kmem_free(zc, sizeof (zfs_cmd_t));
5143 #ifdef CONFIG_COMPAT
5145 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5147 return zfsdev_ioctl(filp, cmd, arg);
5150 #define zfsdev_compat_ioctl NULL
5153 static const struct file_operations zfsdev_fops = {
5154 .open = zfsdev_open,
5155 .release = zfsdev_release,
5156 .unlocked_ioctl = zfsdev_ioctl,
5157 .compat_ioctl = zfsdev_compat_ioctl,
5158 .owner = THIS_MODULE,
5161 static struct miscdevice zfs_misc = {
5162 .minor = MISC_DYNAMIC_MINOR,
5164 .fops = &zfsdev_fops,
5172 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5173 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5174 offsetof(zfsdev_state_t, zs_next));
5176 error = misc_register(&zfs_misc);
5178 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5190 error = misc_deregister(&zfs_misc);
5192 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5194 mutex_destroy(&zfsdev_state_lock);
5195 list_destroy(&zfsdev_state_list);
5198 uint_t zfs_fsyncer_key;
5199 extern uint_t rrw_tsd_key;
5202 #define ZFS_DEBUG_STR " (DEBUG mode)"
5204 #define ZFS_DEBUG_STR ""
5212 spa_init(FREAD | FWRITE);
5215 if ((error = zvol_init()) != 0)
5218 if ((error = zfs_attach()) != 0)
5221 tsd_create(&zfs_fsyncer_key, NULL);
5222 tsd_create(&rrw_tsd_key, NULL);
5225 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5226 #endif /* HAVE_SHARE */
5228 printk(KERN_NOTICE "ZFS: Loaded ZFS Filesystem v%s%s\n",
5229 ZFS_META_VERSION, ZFS_DEBUG_STR);
5238 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s%s"
5239 ", rc = %d\n", ZFS_META_VERSION, ZFS_DEBUG_STR, error);
5252 if (zfs_nfsshare_inited)
5253 (void) ddi_modclose(nfs_mod);
5254 if (zfs_smbshare_inited)
5255 (void) ddi_modclose(smbsrv_mod);
5256 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5257 (void) ddi_modclose(sharefs_mod);
5259 mutex_destroy(&zfs_share_lock);
5260 #endif /* HAVE_SHARE */
5261 tsd_destroy(&zfs_fsyncer_key);
5262 tsd_destroy(&rrw_tsd_key);
5264 printk(KERN_NOTICE "ZFS: Unloaded ZFS Filesystem v%s%s\n",
5265 ZFS_META_VERSION, ZFS_DEBUG_STR);
5271 spl_module_init(_init);
5272 spl_module_exit(_fini);
5274 MODULE_DESCRIPTION("ZFS");
5275 MODULE_AUTHOR(ZFS_META_AUTHOR);
5276 MODULE_LICENSE(ZFS_META_LICENSE);
5277 #endif /* HAVE_SPL */