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 zfs_sb_t; 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);
853 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
854 * SYS_CONFIG privilege, which is not available in a local zone.
858 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
860 if (secpolicy_sys_config(cr, B_FALSE) != 0)
867 * Policy for object to name lookups.
871 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
875 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
878 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
883 * Policy for fault injection. Requires all privileges.
887 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
889 return (secpolicy_zinject(cr));
893 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
895 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
897 if (prop == ZPROP_INVAL) {
898 if (!zfs_prop_user(zc->zc_value))
900 return (zfs_secpolicy_write_perms(zc->zc_name,
901 ZFS_DELEG_PERM_USERPROP, cr));
903 return (zfs_secpolicy_setprop(zc->zc_name, prop,
909 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
911 int err = zfs_secpolicy_read(zc, cr);
915 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
918 if (zc->zc_value[0] == 0) {
920 * They are asking about a posix uid/gid. If it's
921 * themself, allow it.
923 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
924 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
925 if (zc->zc_guid == crgetuid(cr))
928 if (groupmember(zc->zc_guid, cr))
933 return (zfs_secpolicy_write_perms(zc->zc_name,
934 userquota_perms[zc->zc_objset_type], cr));
938 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
940 int err = zfs_secpolicy_read(zc, cr);
944 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
947 return (zfs_secpolicy_write_perms(zc->zc_name,
948 userquota_perms[zc->zc_objset_type], cr));
952 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
954 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
959 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
961 return (zfs_secpolicy_write_perms(zc->zc_name,
962 ZFS_DELEG_PERM_HOLD, cr));
966 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
968 return (zfs_secpolicy_write_perms(zc->zc_name,
969 ZFS_DELEG_PERM_RELEASE, cr));
973 * Policy for allowing temporary snapshots to be taken or released
976 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
979 * A temporary snapshot is the same as a snapshot,
980 * hold, destroy and release all rolled into one.
981 * Delegated diff alone is sufficient that we allow this.
985 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
986 ZFS_DELEG_PERM_DIFF, cr)) == 0)
989 error = zfs_secpolicy_snapshot(zc, cr);
991 error = zfs_secpolicy_hold(zc, cr);
993 error = zfs_secpolicy_release(zc, cr);
995 error = zfs_secpolicy_destroy(zc, cr);
1000 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1003 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1007 nvlist_t *list = NULL;
1010 * Read in and unpack the user-supplied nvlist.
1015 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1017 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1019 kmem_free(packed, size);
1023 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1024 kmem_free(packed, size);
1028 kmem_free(packed, size);
1035 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1039 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1041 if (size > zc->zc_nvlist_dst_size) {
1042 nvpair_t *more_errors;
1045 if (zc->zc_nvlist_dst_size < 1024)
1048 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1049 more_errors = nvlist_prev_nvpair(*errors, NULL);
1052 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1054 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1056 VERIFY(nvlist_size(*errors, &size,
1057 NV_ENCODE_NATIVE) == 0);
1058 } while (size > zc->zc_nvlist_dst_size);
1060 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1061 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1062 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1063 ASSERT(size <= zc->zc_nvlist_dst_size);
1070 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1072 char *packed = NULL;
1076 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1078 if (size > zc->zc_nvlist_dst_size) {
1081 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1082 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1084 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1085 size, zc->zc_iflags) != 0)
1087 kmem_free(packed, size);
1090 zc->zc_nvlist_dst_size = size;
1095 get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
1100 error = dmu_objset_hold(dsname, FTAG, &os);
1103 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1104 dmu_objset_rele(os, FTAG);
1108 mutex_enter(&os->os_user_ptr_lock);
1109 *zsbp = dmu_objset_get_user(os);
1110 if (*zsbp && (*zsbp)->z_sb) {
1111 if (atomic_inc_not_zero(&((*zsbp)->z_sb->s_active)))
1116 mutex_exit(&os->os_user_ptr_lock);
1117 dmu_objset_rele(os, FTAG);
1122 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1123 * case its z_sb will be NULL, and it will be opened as the owner.
1126 zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
1130 if (get_zfs_sb(name, zsbp) != 0)
1131 error = zfs_sb_create(name, zsbp);
1133 rrw_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
1135 if ((*zsbp)->z_unmounted) {
1137 * XXX we could probably try again, since the unmounting
1138 * thread should be just about to disassociate the
1139 * objset from the zfsvfs.
1141 rrw_exit(&(*zsbp)->z_teardown_lock, tag);
1149 zfs_sb_rele(zfs_sb_t *zsb, void *tag)
1151 rrw_exit(&zsb->z_teardown_lock, tag);
1154 deactivate_super(zsb->z_sb);
1156 dmu_objset_disown(zsb->z_os, zsb);
1162 zfs_ioc_pool_create(zfs_cmd_t *zc)
1165 nvlist_t *config, *props = NULL;
1166 nvlist_t *rootprops = NULL;
1167 nvlist_t *zplprops = NULL;
1170 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1171 zc->zc_iflags, &config)))
1174 if (zc->zc_nvlist_src_size != 0 && (error =
1175 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1176 zc->zc_iflags, &props))) {
1177 nvlist_free(config);
1182 nvlist_t *nvl = NULL;
1183 uint64_t version = SPA_VERSION;
1185 (void) nvlist_lookup_uint64(props,
1186 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1187 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1189 goto pool_props_bad;
1191 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1193 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1195 nvlist_free(config);
1199 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1201 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1202 error = zfs_fill_zplprops_root(version, rootprops,
1205 goto pool_props_bad;
1208 buf = history_str_get(zc);
1210 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1213 * Set the remaining root properties
1215 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1216 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1217 (void) spa_destroy(zc->zc_name);
1220 history_str_free(buf);
1223 nvlist_free(rootprops);
1224 nvlist_free(zplprops);
1225 nvlist_free(config);
1232 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1235 zfs_log_history(zc);
1236 error = spa_destroy(zc->zc_name);
1238 zvol_remove_minors(zc->zc_name);
1243 zfs_ioc_pool_import(zfs_cmd_t *zc)
1245 nvlist_t *config, *props = NULL;
1249 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1250 zc->zc_iflags, &config)) != 0)
1253 if (zc->zc_nvlist_src_size != 0 && (error =
1254 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1255 zc->zc_iflags, &props))) {
1256 nvlist_free(config);
1260 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1261 guid != zc->zc_guid)
1264 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1266 if (zc->zc_nvlist_dst != 0) {
1269 if ((err = put_nvlist(zc, config)) != 0)
1274 zvol_create_minors(zc->zc_name);
1276 nvlist_free(config);
1285 zfs_ioc_pool_export(zfs_cmd_t *zc)
1288 boolean_t force = (boolean_t)zc->zc_cookie;
1289 boolean_t hardforce = (boolean_t)zc->zc_guid;
1291 zfs_log_history(zc);
1292 error = spa_export(zc->zc_name, NULL, force, hardforce);
1294 zvol_remove_minors(zc->zc_name);
1299 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1304 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1307 error = put_nvlist(zc, configs);
1309 nvlist_free(configs);
1315 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1321 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1322 sizeof (zc->zc_value));
1324 if (config != NULL) {
1325 ret = put_nvlist(zc, config);
1326 nvlist_free(config);
1329 * The config may be present even if 'error' is non-zero.
1330 * In this case we return success, and preserve the real errno
1333 zc->zc_cookie = error;
1342 * Try to import the given pool, returning pool stats as appropriate so that
1343 * user land knows which devices are available and overall pool health.
1346 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1348 nvlist_t *tryconfig, *config;
1351 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1352 zc->zc_iflags, &tryconfig)) != 0)
1355 config = spa_tryimport(tryconfig);
1357 nvlist_free(tryconfig);
1362 error = put_nvlist(zc, config);
1363 nvlist_free(config);
1370 * zc_name name of the pool
1371 * zc_cookie scan func (pool_scan_func_t)
1374 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1379 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1382 if (zc->zc_cookie == POOL_SCAN_NONE)
1383 error = spa_scan_stop(spa);
1385 error = spa_scan(spa, zc->zc_cookie);
1387 spa_close(spa, FTAG);
1393 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1398 error = spa_open(zc->zc_name, &spa, FTAG);
1401 spa_close(spa, FTAG);
1407 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1412 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1415 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1416 spa_close(spa, FTAG);
1420 spa_upgrade(spa, zc->zc_cookie);
1421 spa_close(spa, FTAG);
1427 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1434 if ((size = zc->zc_history_len) == 0)
1437 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1440 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1441 spa_close(spa, FTAG);
1445 hist_buf = vmem_alloc(size, KM_SLEEP);
1446 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1447 &zc->zc_history_len, hist_buf)) == 0) {
1448 error = ddi_copyout(hist_buf,
1449 (void *)(uintptr_t)zc->zc_history,
1450 zc->zc_history_len, zc->zc_iflags);
1453 spa_close(spa, FTAG);
1454 vmem_free(hist_buf, size);
1459 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1463 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1471 * zc_name name of filesystem
1472 * zc_obj object to find
1475 * zc_value name of object
1478 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1483 /* XXX reading from objset not owned */
1484 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1486 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1487 dmu_objset_rele(os, FTAG);
1490 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1491 sizeof (zc->zc_value));
1492 dmu_objset_rele(os, FTAG);
1499 * zc_name name of filesystem
1500 * zc_obj object to find
1503 * zc_stat stats on object
1504 * zc_value path to object
1507 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1512 /* XXX reading from objset not owned */
1513 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1515 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1516 dmu_objset_rele(os, FTAG);
1519 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1520 sizeof (zc->zc_value));
1521 dmu_objset_rele(os, FTAG);
1527 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1531 nvlist_t *config, **l2cache, **spares;
1532 uint_t nl2cache = 0, nspares = 0;
1534 error = spa_open(zc->zc_name, &spa, FTAG);
1538 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1539 zc->zc_iflags, &config);
1540 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1541 &l2cache, &nl2cache);
1543 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1547 * A root pool with concatenated devices is not supported.
1548 * Thus, can not add a device to a root pool.
1550 * Intent log device can not be added to a rootpool because
1551 * during mountroot, zil is replayed, a seperated log device
1552 * can not be accessed during the mountroot time.
1554 * l2cache and spare devices are ok to be added to a rootpool.
1556 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1557 nvlist_free(config);
1558 spa_close(spa, FTAG);
1563 error = spa_vdev_add(spa, config);
1564 nvlist_free(config);
1566 spa_close(spa, FTAG);
1572 * zc_name name of the pool
1573 * zc_nvlist_conf nvlist of devices to remove
1574 * zc_cookie to stop the remove?
1577 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1582 error = spa_open(zc->zc_name, &spa, FTAG);
1585 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1586 spa_close(spa, FTAG);
1591 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1595 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1597 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1599 switch (zc->zc_cookie) {
1600 case VDEV_STATE_ONLINE:
1601 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1604 case VDEV_STATE_OFFLINE:
1605 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1608 case VDEV_STATE_FAULTED:
1609 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1610 zc->zc_obj != VDEV_AUX_EXTERNAL)
1611 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1613 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1616 case VDEV_STATE_DEGRADED:
1617 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1618 zc->zc_obj != VDEV_AUX_EXTERNAL)
1619 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1621 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1627 zc->zc_cookie = newstate;
1628 spa_close(spa, FTAG);
1633 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1636 int replacing = zc->zc_cookie;
1640 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1643 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1644 zc->zc_iflags, &config)) == 0) {
1645 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1646 nvlist_free(config);
1649 spa_close(spa, FTAG);
1654 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1659 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1662 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1664 spa_close(spa, FTAG);
1669 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1672 nvlist_t *config, *props = NULL;
1674 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1676 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1679 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1680 zc->zc_iflags, &config))) {
1681 spa_close(spa, FTAG);
1685 if (zc->zc_nvlist_src_size != 0 && (error =
1686 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1687 zc->zc_iflags, &props))) {
1688 spa_close(spa, FTAG);
1689 nvlist_free(config);
1693 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1695 spa_close(spa, FTAG);
1697 nvlist_free(config);
1704 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1707 char *path = zc->zc_value;
1708 uint64_t guid = zc->zc_guid;
1711 error = spa_open(zc->zc_name, &spa, FTAG);
1715 error = spa_vdev_setpath(spa, guid, path);
1716 spa_close(spa, FTAG);
1721 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1724 char *fru = zc->zc_value;
1725 uint64_t guid = zc->zc_guid;
1728 error = spa_open(zc->zc_name, &spa, FTAG);
1732 error = spa_vdev_setfru(spa, guid, fru);
1733 spa_close(spa, FTAG);
1738 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1743 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1745 if (zc->zc_nvlist_dst != 0 &&
1746 (error = dsl_prop_get_all(os, &nv)) == 0) {
1747 dmu_objset_stats(os, nv);
1749 * NB: zvol_get_stats() will read the objset contents,
1750 * which we aren't supposed to do with a
1751 * DS_MODE_USER hold, because it could be
1752 * inconsistent. So this is a bit of a workaround...
1753 * XXX reading with out owning
1755 if (!zc->zc_objset_stats.dds_inconsistent) {
1756 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1757 error = zvol_get_stats(os, nv);
1760 error = put_nvlist(zc, nv);
1769 * zc_name name of filesystem
1770 * zc_nvlist_dst_size size of buffer for property nvlist
1773 * zc_objset_stats stats
1774 * zc_nvlist_dst property nvlist
1775 * zc_nvlist_dst_size size of property nvlist
1778 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1780 objset_t *os = NULL;
1783 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1786 error = zfs_ioc_objset_stats_impl(zc, os);
1788 dmu_objset_rele(os, FTAG);
1795 * zc_name name of filesystem
1796 * zc_nvlist_dst_size size of buffer for property nvlist
1799 * zc_nvlist_dst received property nvlist
1800 * zc_nvlist_dst_size size of received property nvlist
1802 * Gets received properties (distinct from local properties on or after
1803 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1804 * local property values.
1807 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1809 objset_t *os = NULL;
1813 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1817 * Without this check, we would return local property values if the
1818 * caller has not already received properties on or after
1819 * SPA_VERSION_RECVD_PROPS.
1821 if (!dsl_prop_get_hasrecvd(os)) {
1822 dmu_objset_rele(os, FTAG);
1826 if (zc->zc_nvlist_dst != 0 &&
1827 (error = dsl_prop_get_received(os, &nv)) == 0) {
1828 error = put_nvlist(zc, nv);
1832 dmu_objset_rele(os, FTAG);
1837 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1843 * zfs_get_zplprop() will either find a value or give us
1844 * the default value (if there is one).
1846 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1848 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1854 * zc_name name of filesystem
1855 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1858 * zc_nvlist_dst zpl property nvlist
1859 * zc_nvlist_dst_size size of zpl property nvlist
1862 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1867 /* XXX reading without owning */
1868 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1871 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1874 * NB: nvl_add_zplprop() will read the objset contents,
1875 * which we aren't supposed to do with a DS_MODE_USER
1876 * hold, because it could be inconsistent.
1878 if (zc->zc_nvlist_dst != 0 &&
1879 !zc->zc_objset_stats.dds_inconsistent &&
1880 dmu_objset_type(os) == DMU_OST_ZFS) {
1883 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1884 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1885 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1886 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1887 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1888 err = put_nvlist(zc, nv);
1893 dmu_objset_rele(os, FTAG);
1898 dataset_name_hidden(const char *name)
1901 * Skip over datasets that are not visible in this zone,
1902 * internal datasets (which have a $ in their name), and
1903 * temporary datasets (which have a % in their name).
1905 if (strchr(name, '$') != NULL)
1907 if (strchr(name, '%') != NULL)
1909 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1916 * zc_name name of filesystem
1917 * zc_cookie zap cursor
1918 * zc_nvlist_dst_size size of buffer for property nvlist
1921 * zc_name name of next filesystem
1922 * zc_cookie zap cursor
1923 * zc_objset_stats stats
1924 * zc_nvlist_dst property nvlist
1925 * zc_nvlist_dst_size size of property nvlist
1928 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1933 size_t orig_len = strlen(zc->zc_name);
1936 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1937 if (error == ENOENT)
1942 p = strrchr(zc->zc_name, '/');
1943 if (p == NULL || p[1] != '\0')
1944 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1945 p = zc->zc_name + strlen(zc->zc_name);
1948 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1949 * but is not declared void because its called by dmu_objset_find().
1951 if (zc->zc_cookie == 0) {
1952 uint64_t cookie = 0;
1953 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1955 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1956 (void) dmu_objset_prefetch(p, NULL);
1960 error = dmu_dir_list_next(os,
1961 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1962 NULL, &zc->zc_cookie);
1963 if (error == ENOENT)
1965 } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1966 !(zc->zc_iflags & FKIOCTL));
1967 dmu_objset_rele(os, FTAG);
1970 * If it's an internal dataset (ie. with a '$' in its name),
1971 * don't try to get stats for it, otherwise we'll return ENOENT.
1973 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1974 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1975 if (error == ENOENT) {
1976 /* We lost a race with destroy, get the next one. */
1977 zc->zc_name[orig_len] = '\0';
1986 * zc_name name of filesystem
1987 * zc_cookie zap cursor
1988 * zc_nvlist_dst_size size of buffer for property nvlist
1991 * zc_name name of next snapshot
1992 * zc_objset_stats stats
1993 * zc_nvlist_dst property nvlist
1994 * zc_nvlist_dst_size size of property nvlist
1997 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2003 if (zc->zc_cookie == 0)
2004 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2005 NULL, DS_FIND_SNAPSHOTS);
2007 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2009 return (error == ENOENT ? ESRCH : error);
2012 * A dataset name of maximum length cannot have any snapshots,
2013 * so exit immediately.
2015 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2016 dmu_objset_rele(os, FTAG);
2020 error = dmu_snapshot_list_next(os,
2021 sizeof (zc->zc_name) - strlen(zc->zc_name),
2022 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2027 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2030 * Since we probably don't have a hold on this snapshot,
2031 * it's possible that the objsetid could have been destroyed
2032 * and reused for a new objset. It's OK if this happens during
2033 * a zfs send operation, since the new createtxg will be
2034 * beyond the range we're interested in.
2036 rw_enter(&dp->dp_config_rwlock, RW_READER);
2037 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2038 rw_exit(&dp->dp_config_rwlock);
2040 if (error == ENOENT) {
2041 /* Racing with destroy, get the next one. */
2042 *strchr(zc->zc_name, '@') = '\0';
2043 dmu_objset_rele(os, FTAG);
2049 error = dmu_objset_from_ds(ds, &ossnap);
2051 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2052 dsl_dataset_rele(ds, FTAG);
2054 } else if (error == ENOENT) {
2058 dmu_objset_rele(os, FTAG);
2059 /* if we failed, undo the @ that we tacked on to zc_name */
2061 *strchr(zc->zc_name, '@') = '\0';
2066 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2068 const char *propname = nvpair_name(pair);
2070 unsigned int vallen;
2073 zfs_userquota_prop_t type;
2079 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2081 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2082 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2088 * A correctly constructed propname is encoded as
2089 * userquota@<rid>-<domain>.
2091 if ((dash = strchr(propname, '-')) == NULL ||
2092 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2101 err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2103 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2104 zfs_sb_rele(zsb, FTAG);
2111 * If the named property is one that has a special function to set its value,
2112 * return 0 on success and a positive error code on failure; otherwise if it is
2113 * not one of the special properties handled by this function, return -1.
2115 * XXX: It would be better for callers of the property interface if we handled
2116 * these special cases in dsl_prop.c (in the dsl layer).
2119 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2122 const char *propname = nvpair_name(pair);
2123 zfs_prop_t prop = zfs_name_to_prop(propname);
2127 if (prop == ZPROP_INVAL) {
2128 if (zfs_prop_userquota(propname))
2129 return (zfs_prop_set_userquota(dsname, pair));
2133 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2135 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2136 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2140 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2143 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2146 case ZFS_PROP_QUOTA:
2147 err = dsl_dir_set_quota(dsname, source, intval);
2149 case ZFS_PROP_REFQUOTA:
2150 err = dsl_dataset_set_quota(dsname, source, intval);
2152 case ZFS_PROP_RESERVATION:
2153 err = dsl_dir_set_reservation(dsname, source, intval);
2155 case ZFS_PROP_REFRESERVATION:
2156 err = dsl_dataset_set_reservation(dsname, source, intval);
2158 case ZFS_PROP_VOLSIZE:
2159 err = zvol_set_volsize(dsname, intval);
2161 case ZFS_PROP_VERSION:
2165 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2168 err = zfs_set_version(zsb, intval);
2169 zfs_sb_rele(zsb, FTAG);
2171 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2174 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2175 (void) strcpy(zc->zc_name, dsname);
2176 (void) zfs_ioc_userspace_upgrade(zc);
2177 kmem_free(zc, sizeof (zfs_cmd_t));
2190 * This function is best effort. If it fails to set any of the given properties,
2191 * it continues to set as many as it can and returns the first error
2192 * encountered. If the caller provides a non-NULL errlist, it also gives the
2193 * complete list of names of all the properties it failed to set along with the
2194 * corresponding error numbers. The caller is responsible for freeing the
2197 * If every property is set successfully, zero is returned and the list pointed
2198 * at by errlist is NULL.
2201 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2209 nvlist_t *genericnvl;
2213 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2214 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2215 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2219 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2220 const char *propname = nvpair_name(pair);
2221 zfs_prop_t prop = zfs_name_to_prop(propname);
2224 /* decode the property value */
2226 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2228 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2229 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2234 /* Validate value type */
2235 if (err == 0 && prop == ZPROP_INVAL) {
2236 if (zfs_prop_user(propname)) {
2237 if (nvpair_type(propval) != DATA_TYPE_STRING)
2239 } else if (zfs_prop_userquota(propname)) {
2240 if (nvpair_type(propval) !=
2241 DATA_TYPE_UINT64_ARRAY)
2244 } else if (err == 0) {
2245 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2246 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2248 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2251 VERIFY(nvpair_value_uint64(propval,
2254 switch (zfs_prop_get_type(prop)) {
2255 case PROP_TYPE_NUMBER:
2257 case PROP_TYPE_STRING:
2260 case PROP_TYPE_INDEX:
2261 if (zfs_prop_index_to_string(prop,
2262 intval, &unused) != 0)
2267 "unknown property type");
2274 /* Validate permissions */
2276 err = zfs_check_settable(dsname, pair, CRED());
2279 err = zfs_prop_set_special(dsname, source, pair);
2282 * For better performance we build up a list of
2283 * properties to set in a single transaction.
2285 err = nvlist_add_nvpair(genericnvl, pair);
2286 } else if (err != 0 && nvl != retrynvl) {
2288 * This may be a spurious error caused by
2289 * receiving quota and reservation out of order.
2290 * Try again in a second pass.
2292 err = nvlist_add_nvpair(retrynvl, pair);
2297 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2300 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2305 if (!nvlist_empty(genericnvl) &&
2306 dsl_props_set(dsname, source, genericnvl) != 0) {
2308 * If this fails, we still want to set as many properties as we
2309 * can, so try setting them individually.
2312 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2313 const char *propname = nvpair_name(pair);
2317 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2319 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2320 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2324 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2325 VERIFY(nvpair_value_string(propval,
2327 err = dsl_prop_set(dsname, propname, source, 1,
2328 strlen(strval) + 1, strval);
2330 VERIFY(nvpair_value_uint64(propval,
2332 err = dsl_prop_set(dsname, propname, source, 8,
2337 VERIFY(nvlist_add_int32(errors, propname,
2342 nvlist_free(genericnvl);
2343 nvlist_free(retrynvl);
2345 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2346 nvlist_free(errors);
2349 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2352 if (errlist == NULL)
2353 nvlist_free(errors);
2361 * Check that all the properties are valid user properties.
2364 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2366 nvpair_t *pair = NULL;
2369 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2370 const char *propname = nvpair_name(pair);
2373 if (!zfs_prop_user(propname) ||
2374 nvpair_type(pair) != DATA_TYPE_STRING)
2377 if ((error = zfs_secpolicy_write_perms(fsname,
2378 ZFS_DELEG_PERM_USERPROP, CRED())))
2381 if (strlen(propname) >= ZAP_MAXNAMELEN)
2382 return (ENAMETOOLONG);
2384 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2385 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2392 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2396 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2399 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2400 if (nvlist_exists(skipped, nvpair_name(pair)))
2403 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2408 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2412 nvlist_t *cleared_props = NULL;
2413 props_skip(props, skipped, &cleared_props);
2414 if (!nvlist_empty(cleared_props)) {
2416 * Acts on local properties until the dataset has received
2417 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2419 zprop_source_t flags = (ZPROP_SRC_NONE |
2420 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2421 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2423 nvlist_free(cleared_props);
2429 * zc_name name of filesystem
2430 * zc_value name of property to set
2431 * zc_nvlist_src{_size} nvlist of properties to apply
2432 * zc_cookie received properties flag
2435 * zc_nvlist_dst{_size} error for each unapplied received property
2438 zfs_ioc_set_prop(zfs_cmd_t *zc)
2441 boolean_t received = zc->zc_cookie;
2442 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2444 nvlist_t *errors = NULL;
2447 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2448 zc->zc_iflags, &nvl)) != 0)
2452 nvlist_t *origprops;
2455 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2456 if (dsl_prop_get_received(os, &origprops) == 0) {
2457 (void) clear_received_props(os,
2458 zc->zc_name, origprops, nvl);
2459 nvlist_free(origprops);
2462 dsl_prop_set_hasrecvd(os);
2463 dmu_objset_rele(os, FTAG);
2467 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2469 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2470 (void) put_nvlist(zc, errors);
2473 nvlist_free(errors);
2480 * zc_name name of filesystem
2481 * zc_value name of property to inherit
2482 * zc_cookie revert to received value if TRUE
2487 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2489 const char *propname = zc->zc_value;
2490 zfs_prop_t prop = zfs_name_to_prop(propname);
2491 boolean_t received = zc->zc_cookie;
2492 zprop_source_t source = (received
2493 ? ZPROP_SRC_NONE /* revert to received value, if any */
2494 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2503 * zfs_prop_set_special() expects properties in the form of an
2504 * nvpair with type info.
2506 if (prop == ZPROP_INVAL) {
2507 if (!zfs_prop_user(propname))
2510 type = PROP_TYPE_STRING;
2511 } else if (prop == ZFS_PROP_VOLSIZE ||
2512 prop == ZFS_PROP_VERSION) {
2515 type = zfs_prop_get_type(prop);
2518 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2521 case PROP_TYPE_STRING:
2522 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2524 case PROP_TYPE_NUMBER:
2525 case PROP_TYPE_INDEX:
2526 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2533 pair = nvlist_next_nvpair(dummy, NULL);
2534 err = zfs_prop_set_special(zc->zc_name, source, pair);
2537 return (err); /* special property already handled */
2540 * Only check this in the non-received case. We want to allow
2541 * 'inherit -S' to revert non-inheritable properties like quota
2542 * and reservation to the received or default values even though
2543 * they are not considered inheritable.
2545 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2549 /* the property name has been validated by zfs_secpolicy_inherit() */
2550 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2554 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2561 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2562 zc->zc_iflags, &props)))
2566 * If the only property is the configfile, then just do a spa_lookup()
2567 * to handle the faulted case.
2569 pair = nvlist_next_nvpair(props, NULL);
2570 if (pair != NULL && strcmp(nvpair_name(pair),
2571 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2572 nvlist_next_nvpair(props, pair) == NULL) {
2573 mutex_enter(&spa_namespace_lock);
2574 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2575 spa_configfile_set(spa, props, B_FALSE);
2576 spa_config_sync(spa, B_FALSE, B_TRUE);
2578 mutex_exit(&spa_namespace_lock);
2585 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2590 error = spa_prop_set(spa, props);
2593 spa_close(spa, FTAG);
2599 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2603 nvlist_t *nvp = NULL;
2605 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2607 * If the pool is faulted, there may be properties we can still
2608 * get (such as altroot and cachefile), so attempt to get them
2611 mutex_enter(&spa_namespace_lock);
2612 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2613 error = spa_prop_get(spa, &nvp);
2614 mutex_exit(&spa_namespace_lock);
2616 error = spa_prop_get(spa, &nvp);
2617 spa_close(spa, FTAG);
2620 if (error == 0 && zc->zc_nvlist_dst != 0)
2621 error = put_nvlist(zc, nvp);
2631 * zc_name name of volume
2636 zfs_ioc_create_minor(zfs_cmd_t *zc)
2638 return (zvol_create_minor(zc->zc_name));
2643 * zc_name name of volume
2648 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2650 return (zvol_remove_minor(zc->zc_name));
2655 * zc_name name of filesystem
2656 * zc_nvlist_src{_size} nvlist of delegated permissions
2657 * zc_perm_action allow/unallow flag
2662 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2665 nvlist_t *fsaclnv = NULL;
2667 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2668 zc->zc_iflags, &fsaclnv)) != 0)
2672 * Verify nvlist is constructed correctly
2674 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2675 nvlist_free(fsaclnv);
2680 * If we don't have PRIV_SYS_MOUNT, then validate
2681 * that user is allowed to hand out each permission in
2685 error = secpolicy_zfs(CRED());
2687 if (zc->zc_perm_action == B_FALSE) {
2688 error = dsl_deleg_can_allow(zc->zc_name,
2691 error = dsl_deleg_can_unallow(zc->zc_name,
2697 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2699 nvlist_free(fsaclnv);
2705 * zc_name name of filesystem
2708 * zc_nvlist_src{_size} nvlist of delegated permissions
2711 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2716 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2717 error = put_nvlist(zc, nvp);
2724 #ifdef HAVE_SNAPSHOT
2726 * Search the vfs list for a specified resource. Returns a pointer to it
2727 * or NULL if no suitable entry is found. The caller of this routine
2728 * is responsible for releasing the returned vfs pointer.
2731 zfs_get_vfs(const char *resource)
2734 struct vfs *vfs_found = NULL;
2736 vfs_list_read_lock();
2739 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2744 vfsp = vfsp->vfs_next;
2745 } while (vfsp != rootvfs);
2749 #endif /* HAVE_SNAPSHOT */
2753 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2755 zfs_creat_t *zct = arg;
2757 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2760 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2764 * createprops list of properties requested by creator
2765 * default_zplver zpl version to use if unspecified in createprops
2766 * fuids_ok fuids allowed in this version of the spa?
2767 * os parent objset pointer (NULL if root fs)
2770 * zplprops values for the zplprops we attach to the master node object
2771 * is_ci true if requested file system will be purely case-insensitive
2773 * Determine the settings for utf8only, normalization and
2774 * casesensitivity. Specific values may have been requested by the
2775 * creator and/or we can inherit values from the parent dataset. If
2776 * the file system is of too early a vintage, a creator can not
2777 * request settings for these properties, even if the requested
2778 * setting is the default value. We don't actually want to create dsl
2779 * properties for these, so remove them from the source nvlist after
2783 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2784 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2785 nvlist_t *zplprops, boolean_t *is_ci)
2787 uint64_t sense = ZFS_PROP_UNDEFINED;
2788 uint64_t norm = ZFS_PROP_UNDEFINED;
2789 uint64_t u8 = ZFS_PROP_UNDEFINED;
2791 ASSERT(zplprops != NULL);
2794 * Pull out creator prop choices, if any.
2797 (void) nvlist_lookup_uint64(createprops,
2798 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2799 (void) nvlist_lookup_uint64(createprops,
2800 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2801 (void) nvlist_remove_all(createprops,
2802 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2803 (void) nvlist_lookup_uint64(createprops,
2804 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2805 (void) nvlist_remove_all(createprops,
2806 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2807 (void) nvlist_lookup_uint64(createprops,
2808 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2809 (void) nvlist_remove_all(createprops,
2810 zfs_prop_to_name(ZFS_PROP_CASE));
2814 * If the zpl version requested is whacky or the file system
2815 * or pool is version is too "young" to support normalization
2816 * and the creator tried to set a value for one of the props,
2819 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2820 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2821 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2822 (zplver < ZPL_VERSION_NORMALIZATION &&
2823 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2824 sense != ZFS_PROP_UNDEFINED)))
2828 * Put the version in the zplprops
2830 VERIFY(nvlist_add_uint64(zplprops,
2831 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2833 if (norm == ZFS_PROP_UNDEFINED)
2834 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2835 VERIFY(nvlist_add_uint64(zplprops,
2836 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2839 * If we're normalizing, names must always be valid UTF-8 strings.
2843 if (u8 == ZFS_PROP_UNDEFINED)
2844 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2845 VERIFY(nvlist_add_uint64(zplprops,
2846 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2848 if (sense == ZFS_PROP_UNDEFINED)
2849 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2850 VERIFY(nvlist_add_uint64(zplprops,
2851 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2854 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2860 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2861 nvlist_t *zplprops, boolean_t *is_ci)
2863 boolean_t fuids_ok, sa_ok;
2864 uint64_t zplver = ZPL_VERSION;
2865 objset_t *os = NULL;
2866 char parentname[MAXNAMELEN];
2872 (void) strlcpy(parentname, dataset, sizeof (parentname));
2873 cp = strrchr(parentname, '/');
2877 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2880 spa_vers = spa_version(spa);
2881 spa_close(spa, FTAG);
2883 zplver = zfs_zpl_version_map(spa_vers);
2884 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2885 sa_ok = (zplver >= ZPL_VERSION_SA);
2888 * Open parent object set so we can inherit zplprop values.
2890 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2893 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2895 dmu_objset_rele(os, FTAG);
2900 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2901 nvlist_t *zplprops, boolean_t *is_ci)
2905 uint64_t zplver = ZPL_VERSION;
2908 zplver = zfs_zpl_version_map(spa_vers);
2909 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2910 sa_ok = (zplver >= ZPL_VERSION_SA);
2912 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2913 createprops, zplprops, is_ci);
2919 * zc_objset_type type of objset to create (fs vs zvol)
2920 * zc_name name of new objset
2921 * zc_value name of snapshot to clone from (may be empty)
2922 * zc_nvlist_src{_size} nvlist of properties to apply
2927 zfs_ioc_create(zfs_cmd_t *zc)
2932 nvlist_t *nvprops = NULL;
2933 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2934 dmu_objset_type_t type = zc->zc_objset_type;
2939 cbfunc = zfs_create_cb;
2943 cbfunc = zvol_create_cb;
2950 if (strchr(zc->zc_name, '@') ||
2951 strchr(zc->zc_name, '%'))
2954 if (zc->zc_nvlist_src != 0 &&
2955 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2956 zc->zc_iflags, &nvprops)) != 0)
2959 zct.zct_zplprops = NULL;
2960 zct.zct_props = nvprops;
2962 if (zc->zc_value[0] != '\0') {
2964 * We're creating a clone of an existing snapshot.
2966 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2967 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2968 nvlist_free(nvprops);
2972 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2974 nvlist_free(nvprops);
2978 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2979 dmu_objset_rele(clone, FTAG);
2981 nvlist_free(nvprops);
2985 boolean_t is_insensitive = B_FALSE;
2987 if (cbfunc == NULL) {
2988 nvlist_free(nvprops);
2992 if (type == DMU_OST_ZVOL) {
2993 uint64_t volsize, volblocksize;
2995 if (nvprops == NULL ||
2996 nvlist_lookup_uint64(nvprops,
2997 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2999 nvlist_free(nvprops);
3003 if ((error = nvlist_lookup_uint64(nvprops,
3004 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3005 &volblocksize)) != 0 && error != ENOENT) {
3006 nvlist_free(nvprops);
3011 volblocksize = zfs_prop_default_numeric(
3012 ZFS_PROP_VOLBLOCKSIZE);
3014 if ((error = zvol_check_volblocksize(
3015 volblocksize)) != 0 ||
3016 (error = zvol_check_volsize(volsize,
3017 volblocksize)) != 0) {
3018 nvlist_free(nvprops);
3021 } else if (type == DMU_OST_ZFS) {
3025 * We have to have normalization and
3026 * case-folding flags correct when we do the
3027 * file system creation, so go figure them out
3030 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3031 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3032 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3033 zct.zct_zplprops, &is_insensitive);
3035 nvlist_free(nvprops);
3036 nvlist_free(zct.zct_zplprops);
3040 error = dmu_objset_create(zc->zc_name, type,
3041 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3042 nvlist_free(zct.zct_zplprops);
3046 * It would be nice to do this atomically.
3049 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3052 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3054 nvlist_free(nvprops);
3060 * zc_name name of filesystem
3061 * zc_value short name of snapshot
3062 * zc_cookie recursive flag
3063 * zc_nvlist_src[_size] property list
3066 * zc_value short snapname (i.e. part after the '@')
3069 zfs_ioc_snapshot(zfs_cmd_t *zc)
3071 nvlist_t *nvprops = NULL;
3073 boolean_t recursive = zc->zc_cookie;
3075 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3078 if (zc->zc_nvlist_src != 0 &&
3079 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3080 zc->zc_iflags, &nvprops)) != 0)
3083 error = zfs_check_userprops(zc->zc_name, nvprops);
3087 if (!nvlist_empty(nvprops) &&
3088 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3093 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3094 nvprops, recursive, B_FALSE, -1);
3097 nvlist_free(nvprops);
3102 zfs_unmount_snap(const char *name, void *arg)
3104 #ifdef HAVE_SNAPSHOT
3108 char *snapname = arg;
3109 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3110 vfsp = zfs_get_vfs(fullname);
3112 } else if (strchr(name, '@')) {
3113 vfsp = zfs_get_vfs(name);
3118 * Always force the unmount for snapshots.
3120 int flag = MS_FORCE;
3123 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3128 if ((err = dounmount(vfsp, flag, kcred)) != 0)
3131 #endif /* HAVE_SNAPSHOT */
3137 * zc_name name of filesystem
3138 * zc_value short name of snapshot
3139 * zc_defer_destroy mark for deferred destroy
3144 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3148 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3150 err = dmu_objset_find(zc->zc_name,
3151 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3154 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3155 zc->zc_defer_destroy));
3160 * zc_name name of dataset to destroy
3161 * zc_objset_type type of objset
3162 * zc_defer_destroy mark for deferred destroy
3167 zfs_ioc_destroy(zfs_cmd_t *zc)
3170 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3171 err = zfs_unmount_snap(zc->zc_name, NULL);
3176 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3177 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3178 (void) zvol_remove_minor(zc->zc_name);
3184 * zc_name name of dataset to rollback (to most recent snapshot)
3189 zfs_ioc_rollback(zfs_cmd_t *zc)
3191 dsl_dataset_t *ds, *clone;
3196 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3200 /* must not be a snapshot */
3201 if (dsl_dataset_is_snapshot(ds)) {
3202 dsl_dataset_rele(ds, FTAG);
3206 /* must have a most recent snapshot */
3207 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3208 dsl_dataset_rele(ds, FTAG);
3213 * Create clone of most recent snapshot.
3215 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3216 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3220 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3227 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
3228 error = zfs_suspend_fs(zsb);
3232 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3233 error = dsl_dataset_clone_swap(clone, ds,
3235 dsl_dataset_disown(ds, FTAG);
3240 resume_err = zfs_resume_fs(zsb, zc->zc_name);
3241 error = error ? error : resume_err;
3243 deactivate_super(zsb->z_sb);
3245 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3246 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3247 dsl_dataset_disown(ds, FTAG);
3255 * Destroy clone (which also closes it).
3257 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3260 strfree(clone_name);
3262 dsl_dataset_rele(ds, FTAG);
3268 * zc_name old name of dataset
3269 * zc_value new name of dataset
3270 * zc_cookie recursive flag (only valid for snapshots)
3275 zfs_ioc_rename(zfs_cmd_t *zc)
3277 boolean_t recursive = zc->zc_cookie & 1;
3280 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3281 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3282 strchr(zc->zc_value, '%'))
3286 * Unmount snapshot unless we're doing a recursive rename,
3287 * in which case the dataset code figures out which snapshots
3290 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3291 zc->zc_objset_type == DMU_OST_ZFS) {
3292 err = zfs_unmount_snap(zc->zc_name, NULL);
3297 err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3298 if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3299 (void) zvol_remove_minor(zc->zc_name);
3300 (void) zvol_create_minor(zc->zc_value);
3307 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3309 const char *propname = nvpair_name(pair);
3310 boolean_t issnap = (strchr(dsname, '@') != NULL);
3311 zfs_prop_t prop = zfs_name_to_prop(propname);
3315 if (prop == ZPROP_INVAL) {
3316 if (zfs_prop_user(propname)) {
3317 if ((err = zfs_secpolicy_write_perms(dsname,
3318 ZFS_DELEG_PERM_USERPROP, cr)))
3323 if (!issnap && zfs_prop_userquota(propname)) {
3324 const char *perm = NULL;
3325 const char *uq_prefix =
3326 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3327 const char *gq_prefix =
3328 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3330 if (strncmp(propname, uq_prefix,
3331 strlen(uq_prefix)) == 0) {
3332 perm = ZFS_DELEG_PERM_USERQUOTA;
3333 } else if (strncmp(propname, gq_prefix,
3334 strlen(gq_prefix)) == 0) {
3335 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3337 /* USERUSED and GROUPUSED are read-only */
3341 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3352 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3354 * dsl_prop_get_all_impl() returns properties in this
3358 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3359 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3364 * Check that this value is valid for this pool version
3367 case ZFS_PROP_COMPRESSION:
3369 * If the user specified gzip compression, make sure
3370 * the SPA supports it. We ignore any errors here since
3371 * we'll catch them later.
3373 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3374 nvpair_value_uint64(pair, &intval) == 0) {
3375 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3376 intval <= ZIO_COMPRESS_GZIP_9 &&
3377 zfs_earlier_version(dsname,
3378 SPA_VERSION_GZIP_COMPRESSION)) {
3382 if (intval == ZIO_COMPRESS_ZLE &&
3383 zfs_earlier_version(dsname,
3384 SPA_VERSION_ZLE_COMPRESSION))
3388 * If this is a bootable dataset then
3389 * verify that the compression algorithm
3390 * is supported for booting. We must return
3391 * something other than ENOTSUP since it
3392 * implies a downrev pool version.
3394 if (zfs_is_bootfs(dsname) &&
3395 !BOOTFS_COMPRESS_VALID(intval)) {
3401 case ZFS_PROP_COPIES:
3402 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3406 case ZFS_PROP_DEDUP:
3407 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3411 case ZFS_PROP_SHARESMB:
3412 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3416 case ZFS_PROP_ACLINHERIT:
3417 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3418 nvpair_value_uint64(pair, &intval) == 0) {
3419 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3420 zfs_earlier_version(dsname,
3421 SPA_VERSION_PASSTHROUGH_X))
3429 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3433 * Removes properties from the given props list that fail permission checks
3434 * needed to clear them and to restore them in case of a receive error. For each
3435 * property, make sure we have both set and inherit permissions.
3437 * Returns the first error encountered if any permission checks fail. If the
3438 * caller provides a non-NULL errlist, it also gives the complete list of names
3439 * of all the properties that failed a permission check along with the
3440 * corresponding error numbers. The caller is responsible for freeing the
3443 * If every property checks out successfully, zero is returned and the list
3444 * pointed at by errlist is NULL.
3447 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3450 nvpair_t *pair, *next_pair;
3457 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3459 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
3460 (void) strcpy(zc->zc_name, dataset);
3461 pair = nvlist_next_nvpair(props, NULL);
3462 while (pair != NULL) {
3463 next_pair = nvlist_next_nvpair(props, pair);
3465 (void) strcpy(zc->zc_value, nvpair_name(pair));
3466 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3467 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3468 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3469 VERIFY(nvlist_add_int32(errors,
3470 zc->zc_value, err) == 0);
3474 kmem_free(zc, sizeof (zfs_cmd_t));
3476 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3477 nvlist_free(errors);
3480 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3483 if (errlist == NULL)
3484 nvlist_free(errors);
3492 propval_equals(nvpair_t *p1, nvpair_t *p2)
3494 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3495 /* dsl_prop_get_all_impl() format */
3497 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3498 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3502 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3504 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3505 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3509 if (nvpair_type(p1) != nvpair_type(p2))
3512 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3513 char *valstr1, *valstr2;
3515 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3516 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3517 return (strcmp(valstr1, valstr2) == 0);
3519 uint64_t intval1, intval2;
3521 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3522 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3523 return (intval1 == intval2);
3528 * Remove properties from props if they are not going to change (as determined
3529 * by comparison with origprops). Remove them from origprops as well, since we
3530 * do not need to clear or restore properties that won't change.
3533 props_reduce(nvlist_t *props, nvlist_t *origprops)
3535 nvpair_t *pair, *next_pair;
3537 if (origprops == NULL)
3538 return; /* all props need to be received */
3540 pair = nvlist_next_nvpair(props, NULL);
3541 while (pair != NULL) {
3542 const char *propname = nvpair_name(pair);
3545 next_pair = nvlist_next_nvpair(props, pair);
3547 if ((nvlist_lookup_nvpair(origprops, propname,
3548 &match) != 0) || !propval_equals(pair, match))
3549 goto next; /* need to set received value */
3551 /* don't clear the existing received value */
3552 (void) nvlist_remove_nvpair(origprops, match);
3553 /* don't bother receiving the property */
3554 (void) nvlist_remove_nvpair(props, pair);
3561 static boolean_t zfs_ioc_recv_inject_err;
3566 * zc_name name of containing filesystem
3567 * zc_nvlist_src{_size} nvlist of properties to apply
3568 * zc_value name of snapshot to create
3569 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3570 * zc_cookie file descriptor to recv from
3571 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3572 * zc_guid force flag
3573 * zc_cleanup_fd cleanup-on-exit file descriptor
3574 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3577 * zc_cookie number of bytes read
3578 * zc_nvlist_dst{_size} error for each unapplied received property
3579 * zc_obj zprop_errflags_t
3580 * zc_action_handle handle for this guid/ds mapping
3583 zfs_ioc_recv(zfs_cmd_t *zc)
3587 dmu_recv_cookie_t drc;
3588 boolean_t force = (boolean_t)zc->zc_guid;
3591 int props_error = 0;
3594 nvlist_t *props = NULL; /* sent properties */
3595 nvlist_t *origprops = NULL; /* existing properties */
3596 objset_t *origin = NULL;
3598 char tofs[ZFS_MAXNAMELEN];
3599 boolean_t first_recvd_props = B_FALSE;
3601 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3602 strchr(zc->zc_value, '@') == NULL ||
3603 strchr(zc->zc_value, '%'))
3606 (void) strcpy(tofs, zc->zc_value);
3607 tosnap = strchr(tofs, '@');
3610 if (zc->zc_nvlist_src != 0 &&
3611 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3612 zc->zc_iflags, &props)) != 0)
3622 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3624 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3625 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3626 !dsl_prop_get_hasrecvd(os)) {
3627 first_recvd_props = B_TRUE;
3631 * If new received properties are supplied, they are to
3632 * completely replace the existing received properties, so stash
3633 * away the existing ones.
3635 if (dsl_prop_get_received(os, &origprops) == 0) {
3636 nvlist_t *errlist = NULL;
3638 * Don't bother writing a property if its value won't
3639 * change (and avoid the unnecessary security checks).
3641 * The first receive after SPA_VERSION_RECVD_PROPS is a
3642 * special case where we blow away all local properties
3645 if (!first_recvd_props)
3646 props_reduce(props, origprops);
3647 if (zfs_check_clearable(tofs, origprops,
3649 (void) nvlist_merge(errors, errlist, 0);
3650 nvlist_free(errlist);
3653 dmu_objset_rele(os, FTAG);
3656 if (zc->zc_string[0]) {
3657 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3662 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3663 &zc->zc_begin_record, force, origin, &drc);
3665 dmu_objset_rele(origin, FTAG);
3670 * Set properties before we receive the stream so that they are applied
3671 * to the new data. Note that we must call dmu_recv_stream() if
3672 * dmu_recv_begin() succeeds.
3677 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3678 if (drc.drc_newfs) {
3679 if (spa_version(os->os_spa) >=
3680 SPA_VERSION_RECVD_PROPS)
3681 first_recvd_props = B_TRUE;
3682 } else if (origprops != NULL) {
3683 if (clear_received_props(os, tofs, origprops,
3684 first_recvd_props ? NULL : props) != 0)
3685 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3687 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3689 dsl_prop_set_hasrecvd(os);
3690 } else if (!drc.drc_newfs) {
3691 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3694 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3696 (void) nvlist_merge(errors, errlist, 0);
3697 nvlist_free(errlist);
3700 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3702 * Caller made zc->zc_nvlist_dst less than the minimum expected
3703 * size or supplied an invalid address.
3705 props_error = EINVAL;
3709 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3710 &zc->zc_action_handle);
3713 zfs_sb_t *zsb = NULL;
3715 if (get_zfs_sb(tofs, &zsb) == 0) {
3719 error = zfs_suspend_fs(zsb);
3721 * If the suspend fails, then the recv_end will
3722 * likely also fail, and clean up after itself.
3724 end_err = dmu_recv_end(&drc);
3726 error = zfs_resume_fs(zsb, tofs);
3727 error = error ? error : end_err;
3728 deactivate_super(zsb->z_sb);
3730 error = dmu_recv_end(&drc);
3734 zc->zc_cookie = off - fp->f_offset;
3735 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3739 if (zfs_ioc_recv_inject_err) {
3740 zfs_ioc_recv_inject_err = B_FALSE;
3745 * On error, restore the original props.
3747 if (error && props) {
3748 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3749 if (clear_received_props(os, tofs, props, NULL) != 0) {
3751 * We failed to clear the received properties.
3752 * Since we may have left a $recvd value on the
3753 * system, we can't clear the $hasrecvd flag.
3755 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3756 } else if (first_recvd_props) {
3757 dsl_prop_unset_hasrecvd(os);
3759 dmu_objset_rele(os, FTAG);
3760 } else if (!drc.drc_newfs) {
3761 /* We failed to clear the received properties. */
3762 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3765 if (origprops == NULL && !drc.drc_newfs) {
3766 /* We failed to stash the original properties. */
3767 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3771 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3772 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3773 * explictly if we're restoring local properties cleared in the
3774 * first new-style receive.
3776 if (origprops != NULL &&
3777 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3778 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3779 origprops, NULL) != 0) {
3781 * We stashed the original properties but failed to
3784 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3789 nvlist_free(origprops);
3790 nvlist_free(errors);
3794 error = props_error;
3801 * zc_name name of snapshot to send
3802 * zc_cookie file descriptor to send stream to
3803 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3804 * zc_sendobj objsetid of snapshot to send
3805 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3810 zfs_ioc_send(zfs_cmd_t *zc)
3812 objset_t *fromsnap = NULL;
3818 dsl_dataset_t *dsfrom = NULL;
3822 error = spa_open(zc->zc_name, &spa, FTAG);
3826 dp = spa_get_dsl(spa);
3827 rw_enter(&dp->dp_config_rwlock, RW_READER);
3828 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3829 rw_exit(&dp->dp_config_rwlock);
3831 spa_close(spa, FTAG);
3835 error = dmu_objset_from_ds(ds, &tosnap);
3837 dsl_dataset_rele(ds, FTAG);
3838 spa_close(spa, FTAG);
3842 if (zc->zc_fromobj != 0) {
3843 rw_enter(&dp->dp_config_rwlock, RW_READER);
3844 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3845 rw_exit(&dp->dp_config_rwlock);
3846 spa_close(spa, FTAG);
3848 dsl_dataset_rele(ds, FTAG);
3851 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3853 dsl_dataset_rele(dsfrom, FTAG);
3854 dsl_dataset_rele(ds, FTAG);
3858 spa_close(spa, FTAG);
3861 fp = getf(zc->zc_cookie);
3863 dsl_dataset_rele(ds, FTAG);
3865 dsl_dataset_rele(dsfrom, FTAG);
3870 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3872 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3874 releasef(zc->zc_cookie);
3876 dsl_dataset_rele(dsfrom, FTAG);
3877 dsl_dataset_rele(ds, FTAG);
3882 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3886 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3887 &zc->zc_inject_record);
3890 zc->zc_guid = (uint64_t)id;
3896 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3898 return (zio_clear_fault((int)zc->zc_guid));
3902 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3904 int id = (int)zc->zc_guid;
3907 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3908 &zc->zc_inject_record);
3916 zfs_ioc_error_log(zfs_cmd_t *zc)
3920 size_t count = (size_t)zc->zc_nvlist_dst_size;
3922 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3925 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3928 zc->zc_nvlist_dst_size = count;
3930 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3932 spa_close(spa, FTAG);
3938 zfs_ioc_clear(zfs_cmd_t *zc)
3945 * On zpool clear we also fix up missing slogs
3947 mutex_enter(&spa_namespace_lock);
3948 spa = spa_lookup(zc->zc_name);
3950 mutex_exit(&spa_namespace_lock);
3953 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3954 /* we need to let spa_open/spa_load clear the chains */
3955 spa_set_log_state(spa, SPA_LOG_CLEAR);
3957 spa->spa_last_open_failed = 0;
3958 mutex_exit(&spa_namespace_lock);
3960 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3961 error = spa_open(zc->zc_name, &spa, FTAG);
3964 nvlist_t *config = NULL;
3966 if (zc->zc_nvlist_src == 0)
3969 if ((error = get_nvlist(zc->zc_nvlist_src,
3970 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3971 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3973 if (config != NULL) {
3976 if ((err = put_nvlist(zc, config)) != 0)
3978 nvlist_free(config);
3980 nvlist_free(policy);
3987 spa_vdev_state_enter(spa, SCL_NONE);
3989 if (zc->zc_guid == 0) {
3992 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3994 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
3995 spa_close(spa, FTAG);
4000 vdev_clear(spa, vd);
4002 (void) spa_vdev_state_exit(spa, NULL, 0);
4005 * Resume any suspended I/Os.
4007 if (zio_resume(spa) != 0)
4010 spa_close(spa, FTAG);
4017 * zc_name name of filesystem
4018 * zc_value name of origin snapshot
4021 * zc_string name of conflicting snapshot, if there is one
4024 zfs_ioc_promote(zfs_cmd_t *zc)
4029 * We don't need to unmount *all* the origin fs's snapshots, but
4032 cp = strchr(zc->zc_value, '@');
4035 (void) dmu_objset_find(zc->zc_value,
4036 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4037 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4041 * Retrieve a single {user|group}{used|quota}@... property.
4044 * zc_name name of filesystem
4045 * zc_objset_type zfs_userquota_prop_t
4046 * zc_value domain name (eg. "S-1-234-567-89")
4047 * zc_guid RID/UID/GID
4050 * zc_cookie property value
4053 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4058 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4061 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4065 error = zfs_userspace_one(zsb,
4066 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4067 zfs_sb_rele(zsb, FTAG);
4074 * zc_name name of filesystem
4075 * zc_cookie zap cursor
4076 * zc_objset_type zfs_userquota_prop_t
4077 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4080 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4081 * zc_cookie zap cursor
4084 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4087 int bufsize = zc->zc_nvlist_dst_size;
4094 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4098 buf = vmem_alloc(bufsize, KM_SLEEP);
4100 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4101 buf, &zc->zc_nvlist_dst_size);
4104 error = xcopyout(buf,
4105 (void *)(uintptr_t)zc->zc_nvlist_dst,
4106 zc->zc_nvlist_dst_size);
4108 vmem_free(buf, bufsize);
4109 zfs_sb_rele(zsb, FTAG);
4116 * zc_name name of filesystem
4122 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4128 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4129 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4131 * If userused is not enabled, it may be because the
4132 * objset needs to be closed & reopened (to grow the
4133 * objset_phys_t). Suspend/resume the fs will do that.
4135 error = zfs_suspend_fs(zsb);
4137 error = zfs_resume_fs(zsb, zc->zc_name);
4140 error = dmu_objset_userspace_upgrade(zsb->z_os);
4141 deactivate_super(zsb->z_sb);
4143 /* XXX kind of reading contents without owning */
4144 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4148 error = dmu_objset_userspace_upgrade(os);
4149 dmu_objset_rele(os, FTAG);
4156 * We don't want to have a hard dependency
4157 * against some special symbols in sharefs
4158 * nfs, and smbsrv. Determine them if needed when
4159 * the first file system is shared.
4160 * Neither sharefs, nfs or smbsrv are unloadable modules.
4163 int (*znfsexport_fs)(void *arg);
4164 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4165 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4167 int zfs_nfsshare_inited;
4168 int zfs_smbshare_inited;
4170 ddi_modhandle_t nfs_mod;
4171 ddi_modhandle_t sharefs_mod;
4172 ddi_modhandle_t smbsrv_mod;
4173 kmutex_t zfs_share_lock;
4180 ASSERT(MUTEX_HELD(&zfs_share_lock));
4181 /* Both NFS and SMB shares also require sharetab support. */
4182 if (sharefs_mod == NULL && ((sharefs_mod =
4183 ddi_modopen("fs/sharefs",
4184 KRTLD_MODE_FIRST, &error)) == NULL)) {
4187 if (zshare_fs == NULL && ((zshare_fs =
4188 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4189 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4194 #endif /* HAVE_SHARE */
4197 zfs_ioc_share(zfs_cmd_t *zc)
4203 switch (zc->zc_share.z_sharetype) {
4205 case ZFS_UNSHARE_NFS:
4206 if (zfs_nfsshare_inited == 0) {
4207 mutex_enter(&zfs_share_lock);
4208 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4209 KRTLD_MODE_FIRST, &error)) == NULL)) {
4210 mutex_exit(&zfs_share_lock);
4213 if (znfsexport_fs == NULL &&
4214 ((znfsexport_fs = (int (*)(void *))
4216 "nfs_export", &error)) == NULL)) {
4217 mutex_exit(&zfs_share_lock);
4220 error = zfs_init_sharefs();
4222 mutex_exit(&zfs_share_lock);
4225 zfs_nfsshare_inited = 1;
4226 mutex_exit(&zfs_share_lock);
4230 case ZFS_UNSHARE_SMB:
4231 if (zfs_smbshare_inited == 0) {
4232 mutex_enter(&zfs_share_lock);
4233 if (smbsrv_mod == NULL && ((smbsrv_mod =
4234 ddi_modopen("drv/smbsrv",
4235 KRTLD_MODE_FIRST, &error)) == NULL)) {
4236 mutex_exit(&zfs_share_lock);
4239 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4240 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4241 "smb_server_share", &error)) == NULL)) {
4242 mutex_exit(&zfs_share_lock);
4245 error = zfs_init_sharefs();
4247 mutex_exit(&zfs_share_lock);
4250 zfs_smbshare_inited = 1;
4251 mutex_exit(&zfs_share_lock);
4258 switch (zc->zc_share.z_sharetype) {
4260 case ZFS_UNSHARE_NFS:
4262 znfsexport_fs((void *)
4263 (uintptr_t)zc->zc_share.z_exportdata))
4267 case ZFS_UNSHARE_SMB:
4268 if (error = zsmbexport_fs((void *)
4269 (uintptr_t)zc->zc_share.z_exportdata,
4270 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4277 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4278 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4279 SHAREFS_ADD : SHAREFS_REMOVE;
4282 * Add or remove share from sharetab
4284 error = zshare_fs(opcode,
4285 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4286 zc->zc_share.z_sharemax);
4291 #endif /* HAVE_SHARE */
4294 ace_t full_access[] = {
4295 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4300 * zc_name name of containing filesystem
4301 * zc_obj object # beyond which we want next in-use object #
4304 * zc_obj next in-use object #
4307 zfs_ioc_next_obj(zfs_cmd_t *zc)
4309 objset_t *os = NULL;
4312 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4316 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4317 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4319 dmu_objset_rele(os, FTAG);
4325 * zc_name name of filesystem
4326 * zc_value prefix name for snapshot
4327 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4332 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4337 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4338 (u_longlong_t)ddi_get_lbolt64());
4340 if (strlen(snap_name) >= MAXNAMELEN) {
4345 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4346 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4352 (void) strcpy(zc->zc_value, snap_name);
4359 * zc_name name of "to" snapshot
4360 * zc_value name of "from" snapshot
4361 * zc_cookie file descriptor to write diff data on
4364 * dmu_diff_record_t's to the file descriptor
4367 zfs_ioc_diff(zfs_cmd_t *zc)
4375 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4379 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4381 dmu_objset_rele(tosnap, FTAG);
4385 fp = getf(zc->zc_cookie);
4387 dmu_objset_rele(fromsnap, FTAG);
4388 dmu_objset_rele(tosnap, FTAG);
4394 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4396 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4398 releasef(zc->zc_cookie);
4400 dmu_objset_rele(fromsnap, FTAG);
4401 dmu_objset_rele(tosnap, FTAG);
4406 * Remove all ACL files in shares dir
4410 zfs_smb_acl_purge(znode_t *dzp)
4413 zap_attribute_t zap;
4414 zfs_sb_t *zsb = ZTOZSB(dzp);
4417 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4418 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4419 zap_cursor_advance(&zc)) {
4420 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4424 zap_cursor_fini(&zc);
4427 #endif /* HAVE SHARE */
4430 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4435 vnode_t *resourcevp = NULL;
4444 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4445 NO_FOLLOW, NULL, &vp)) != 0)
4448 /* Now make sure mntpnt and dataset are ZFS */
4450 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4451 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4452 zc->zc_name) != 0)) {
4462 * Create share dir if its missing.
4464 mutex_enter(&zsb->z_lock);
4465 if (zsb->z_shares_dir == 0) {
4468 tx = dmu_tx_create(zsb->z_os);
4469 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4471 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4472 error = dmu_tx_assign(tx, TXG_WAIT);
4476 error = zfs_create_share_dir(zsb, tx);
4480 mutex_exit(&zsb->z_lock);
4486 mutex_exit(&zsb->z_lock);
4488 ASSERT(zsb->z_shares_dir);
4489 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
4495 switch (zc->zc_cookie) {
4496 case ZFS_SMB_ACL_ADD:
4497 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4498 vattr.va_mode = S_IFREG|0777;
4502 vsec.vsa_mask = VSA_ACE;
4503 vsec.vsa_aclentp = &full_access;
4504 vsec.vsa_aclentsz = sizeof (full_access);
4505 vsec.vsa_aclcnt = 1;
4507 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4508 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4510 VN_RELE(resourcevp);
4513 case ZFS_SMB_ACL_REMOVE:
4514 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4518 case ZFS_SMB_ACL_RENAME:
4519 if ((error = get_nvlist(zc->zc_nvlist_src,
4520 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4525 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4526 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4529 VN_RELE(ZTOV(sharedir));
4531 nvlist_free(nvlist);
4534 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4536 nvlist_free(nvlist);
4539 case ZFS_SMB_ACL_PURGE:
4540 error = zfs_smb_acl_purge(sharedir);
4549 VN_RELE(ZTOV(sharedir));
4556 #endif /* HAVE_SHARE */
4561 * zc_name name of filesystem
4562 * zc_value short name of snap
4563 * zc_string user-supplied tag for this hold
4564 * zc_cookie recursive flag
4565 * zc_temphold set if hold is temporary
4566 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4567 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4568 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4573 zfs_ioc_hold(zfs_cmd_t *zc)
4575 boolean_t recursive = zc->zc_cookie;
4582 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4585 if (zc->zc_sendobj == 0) {
4586 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4587 zc->zc_string, recursive, zc->zc_temphold,
4588 zc->zc_cleanup_fd));
4594 error = spa_open(zc->zc_name, &spa, FTAG);
4598 dp = spa_get_dsl(spa);
4599 rw_enter(&dp->dp_config_rwlock, RW_READER);
4600 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4601 rw_exit(&dp->dp_config_rwlock);
4602 spa_close(spa, FTAG);
4607 * Until we have a hold on this snapshot, it's possible that
4608 * zc_sendobj could've been destroyed and reused as part
4609 * of a later txg. Make sure we're looking at the right object.
4611 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4612 dsl_dataset_rele(ds, FTAG);
4616 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4617 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4619 dsl_dataset_rele(ds, FTAG);
4624 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4628 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4631 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4633 dsl_dataset_rele(ds, FTAG);
4640 * zc_name name of dataset from which we're releasing a user hold
4641 * zc_value short name of snap
4642 * zc_string user-supplied tag for this hold
4643 * zc_cookie recursive flag
4648 zfs_ioc_release(zfs_cmd_t *zc)
4650 boolean_t recursive = zc->zc_cookie;
4652 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4655 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4656 zc->zc_string, recursive));
4661 * zc_name name of filesystem
4664 * zc_nvlist_src{_size} nvlist of snapshot holds
4667 zfs_ioc_get_holds(zfs_cmd_t *zc)
4672 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4673 error = put_nvlist(zc, nvp);
4682 * zc_guid flags (ZEVENT_NONBLOCK)
4685 * zc_nvlist_dst next nvlist event
4686 * zc_cookie dropped events since last get
4687 * zc_cleanup_fd cleanup-on-exit file descriptor
4690 zfs_ioc_events_next(zfs_cmd_t *zc)
4693 nvlist_t *event = NULL;
4695 uint64_t dropped = 0;
4698 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4703 error = zfs_zevent_next(ze, &event,
4704 &zc->zc_nvlist_dst_size, &dropped);
4705 if (event != NULL) {
4706 zc->zc_cookie = dropped;
4707 error = put_nvlist(zc, event);
4711 if (zc->zc_guid & ZEVENT_NONBLOCK)
4714 if ((error == 0) || (error != ENOENT))
4717 error = zfs_zevent_wait(ze);
4722 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4729 * zc_cookie cleared events count
4732 zfs_ioc_events_clear(zfs_cmd_t *zc)
4736 zfs_zevent_drain_all(&count);
4737 zc->zc_cookie = count;
4743 * pool create, destroy, and export don't log the history as part of
4744 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4745 * do the logging of those commands.
4747 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4748 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4750 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4752 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4754 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4756 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4758 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4760 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4762 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4763 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4764 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4765 POOL_CHECK_READONLY },
4766 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4767 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4768 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4770 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4771 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4772 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4773 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4774 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4775 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4776 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4777 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4778 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4779 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4780 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4781 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4782 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4783 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4784 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4785 POOL_CHECK_SUSPENDED },
4786 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4788 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4789 POOL_CHECK_SUSPENDED },
4790 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4791 POOL_CHECK_SUSPENDED },
4792 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4793 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4794 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4796 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4798 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4799 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4800 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4801 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4802 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4803 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4804 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4805 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4806 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4807 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4808 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4810 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4812 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4814 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4816 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4818 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4820 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4821 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4822 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4823 B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4824 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4825 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4826 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4828 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4829 POOL_CHECK_SUSPENDED },
4830 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4831 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4832 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4834 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4835 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4836 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4838 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4840 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4841 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4842 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4844 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4845 B_FALSE, POOL_CHECK_NONE },
4846 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4847 B_FALSE, POOL_CHECK_NONE },
4848 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4849 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4850 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4851 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4852 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4853 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4854 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4855 POOL_CHECK_SUSPENDED },
4856 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4858 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4859 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4860 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4862 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4864 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4865 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4866 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4867 POOL_CHECK_SUSPENDED },
4868 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4870 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4875 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4876 zfs_ioc_poolcheck_t check)
4881 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4883 if (check & POOL_CHECK_NONE)
4886 error = spa_open(name, &spa, FTAG);
4888 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4890 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4892 spa_close(spa, FTAG);
4898 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4902 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4904 for (zs = list_head(&zfsdev_state_list); zs != NULL;
4905 zs = list_next(&zfsdev_state_list, zs)) {
4906 if (zs->zs_minor == minor) {
4908 case ZST_ONEXIT: return (zs->zs_onexit);
4909 case ZST_ZEVENT: return (zs->zs_zevent);
4910 case ZST_ALL: return (zs);
4919 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4923 mutex_enter(&zfsdev_state_lock);
4924 ptr = zfsdev_get_state_impl(minor, which);
4925 mutex_exit(&zfsdev_state_lock);
4931 zfsdev_getminor(struct file *filp)
4933 ASSERT(filp != NULL);
4934 ASSERT(filp->private_data != NULL);
4936 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4940 * Find a free minor number. The zfsdev_state_list is expected to
4941 * be short since it is only a list of currently open file handles.
4944 zfsdev_minor_alloc(void)
4946 static minor_t last_minor = 0;
4949 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4951 for (m = last_minor + 1; m != last_minor; m++) {
4952 if (m > ZFSDEV_MAX_MINOR)
4954 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
4964 zfsdev_state_init(struct file *filp)
4969 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4971 minor = zfsdev_minor_alloc();
4975 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
4980 zs->zs_minor = minor;
4981 filp->private_data = zs;
4983 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
4984 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
4986 list_insert_tail(&zfsdev_state_list, zs);
4992 zfsdev_state_destroy(struct file *filp)
4996 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4997 ASSERT(filp->private_data != NULL);
4999 zs = filp->private_data;
5000 zfs_onexit_destroy(zs->zs_onexit);
5001 zfs_zevent_destroy(zs->zs_zevent);
5003 list_remove(&zfsdev_state_list, zs);
5004 kmem_free(zs, sizeof(zfsdev_state_t));
5010 zfsdev_open(struct inode *ino, struct file *filp)
5014 mutex_enter(&zfsdev_state_lock);
5015 error = zfsdev_state_init(filp);
5016 mutex_exit(&zfsdev_state_lock);
5022 zfsdev_release(struct inode *ino, struct file *filp)
5026 mutex_enter(&zfsdev_state_lock);
5027 error = zfsdev_state_destroy(filp);
5028 mutex_exit(&zfsdev_state_lock);
5034 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5038 int error, rc, flag = 0;
5040 vec = cmd - ZFS_IOC;
5041 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5044 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
5046 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5050 if ((error == 0) && !(flag & FKIOCTL))
5051 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
5054 * Ensure that all pool/dataset names are valid before we pass down to
5058 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5059 zc->zc_iflags = flag & FKIOCTL;
5060 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5062 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5064 error = pool_status_check(zc->zc_name,
5065 zfs_ioc_vec[vec].zvec_namecheck,
5066 zfs_ioc_vec[vec].zvec_pool_check);
5070 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5072 error = pool_status_check(zc->zc_name,
5073 zfs_ioc_vec[vec].zvec_namecheck,
5074 zfs_ioc_vec[vec].zvec_pool_check);
5083 error = zfs_ioc_vec[vec].zvec_func(zc);
5085 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5089 if (zfs_ioc_vec[vec].zvec_his_log)
5090 zfs_log_history(zc);
5093 kmem_free(zc, sizeof (zfs_cmd_t));
5097 #ifdef CONFIG_COMPAT
5099 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5101 return zfsdev_ioctl(filp, cmd, arg);
5104 #define zfsdev_compat_ioctl NULL
5107 static const struct file_operations zfsdev_fops = {
5108 .open = zfsdev_open,
5109 .release = zfsdev_release,
5110 .unlocked_ioctl = zfsdev_ioctl,
5111 .compat_ioctl = zfsdev_compat_ioctl,
5112 .owner = THIS_MODULE,
5115 static struct miscdevice zfs_misc = {
5116 .minor = MISC_DYNAMIC_MINOR,
5118 .fops = &zfsdev_fops,
5126 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5127 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5128 offsetof(zfsdev_state_t, zs_next));
5130 error = misc_register(&zfs_misc);
5132 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5144 error = misc_deregister(&zfs_misc);
5146 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5148 mutex_destroy(&zfsdev_state_lock);
5149 list_destroy(&zfsdev_state_list);
5152 uint_t zfs_fsyncer_key;
5153 extern uint_t rrw_tsd_key;
5156 #define ZFS_DEBUG_STR " (DEBUG mode)"
5158 #define ZFS_DEBUG_STR ""
5166 spa_init(FREAD | FWRITE);
5169 if ((error = zvol_init()) != 0)
5172 if ((error = zfs_attach()) != 0)
5175 tsd_create(&zfs_fsyncer_key, NULL);
5176 tsd_create(&rrw_tsd_key, NULL);
5179 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5180 #endif /* HAVE_SHARE */
5182 printk(KERN_NOTICE "ZFS: Loaded module v%s%s, "
5183 "ZFS pool version %s, ZFS filesystem version %s\n",
5184 ZFS_META_VERSION, ZFS_DEBUG_STR,
5185 SPA_VERSION_STRING, ZPL_VERSION_STRING);
5194 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s%s"
5195 ", rc = %d\n", ZFS_META_VERSION, ZFS_DEBUG_STR, error);
5208 if (zfs_nfsshare_inited)
5209 (void) ddi_modclose(nfs_mod);
5210 if (zfs_smbshare_inited)
5211 (void) ddi_modclose(smbsrv_mod);
5212 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5213 (void) ddi_modclose(sharefs_mod);
5215 mutex_destroy(&zfs_share_lock);
5216 #endif /* HAVE_SHARE */
5217 tsd_destroy(&zfs_fsyncer_key);
5218 tsd_destroy(&rrw_tsd_key);
5220 printk(KERN_NOTICE "ZFS: Unloaded module v%s%s\n",
5221 ZFS_META_VERSION, ZFS_DEBUG_STR);
5227 spl_module_init(_init);
5228 spl_module_exit(_fini);
5230 MODULE_DESCRIPTION("ZFS");
5231 MODULE_AUTHOR(ZFS_META_AUTHOR);
5232 MODULE_LICENSE(ZFS_META_LICENSE);
5233 #endif /* HAVE_SPL */