4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/errno.h>
30 #include <sys/modctl.h>
35 #include <sys/cmn_err.h>
37 #include <sys/zfs_ioctl.h>
38 #include <sys/zfs_vfsops.h>
39 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
44 #include <sys/priv_impl.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_dir.h>
63 #include <sys/zfs_onexit.h>
65 #include <sys/dsl_scan.h>
66 #include <sharefs/share.h>
67 #include <sys/dmu_objset.h>
69 #include "zfs_namecheck.h"
71 #include "zfs_deleg.h"
72 #include "zfs_comutil.h"
74 extern struct modlfs zfs_modlfs;
76 extern void zfs_init(void);
77 extern void zfs_fini(void);
79 ldi_ident_t zfs_li = NULL;
82 typedef int zfs_ioc_func_t(zfs_cmd_t *);
83 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
89 } zfs_ioc_namecheck_t;
92 POOL_CHECK_NONE = 1 << 0,
93 POOL_CHECK_SUSPENDED = 1 << 1,
94 POOL_CHECK_READONLY = 1 << 2
95 } zfs_ioc_poolcheck_t;
97 typedef struct zfs_ioc_vec {
98 zfs_ioc_func_t *zvec_func;
99 zfs_secpolicy_func_t *zvec_secpolicy;
100 zfs_ioc_namecheck_t zvec_namecheck;
101 boolean_t zvec_his_log;
102 zfs_ioc_poolcheck_t zvec_pool_check;
105 /* This array is indexed by zfs_userquota_prop_t */
106 static const char *userquota_perms[] = {
107 ZFS_DELEG_PERM_USERUSED,
108 ZFS_DELEG_PERM_USERQUOTA,
109 ZFS_DELEG_PERM_GROUPUSED,
110 ZFS_DELEG_PERM_GROUPQUOTA,
113 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
114 static int zfs_check_settable(const char *name, nvpair_t *property,
116 static int zfs_check_clearable(char *dataset, nvlist_t *props,
118 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
120 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
122 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
124 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
131 * Get rid of annoying "../common/" prefix to filename.
133 newfile = strrchr(file, '/');
134 if (newfile != NULL) {
135 newfile = newfile + 1; /* Get rid of leading / */
141 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
145 * To get this data, use the zfs-dprintf probe as so:
146 * dtrace -q -n 'zfs-dprintf \
147 * /stringof(arg0) == "dbuf.c"/ \
148 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
150 * arg1 = function name
154 DTRACE_PROBE4(zfs__dprintf,
155 char *, newfile, char *, func, int, line, char *, buf);
159 history_str_free(char *buf)
161 kmem_free(buf, HIS_MAX_RECORD_LEN);
165 history_str_get(zfs_cmd_t *zc)
169 if (zc->zc_history == 0)
172 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
173 if (copyinstr((void *)(uintptr_t)zc->zc_history,
174 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
175 history_str_free(buf);
179 buf[HIS_MAX_RECORD_LEN -1] = '\0';
185 * Check to see if the named dataset is currently defined as bootable
188 zfs_is_bootfs(const char *name)
192 if (dmu_objset_hold(name, FTAG, &os) == 0) {
194 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
195 dmu_objset_rele(os, FTAG);
202 * zfs_earlier_version
204 * Return non-zero if the spa version is less than requested version.
207 zfs_earlier_version(const char *name, int version)
211 if (spa_open(name, &spa, FTAG) == 0) {
212 if (spa_version(spa) < version) {
213 spa_close(spa, FTAG);
216 spa_close(spa, FTAG);
222 * zpl_earlier_version
224 * Return TRUE if the ZPL version is less than requested version.
227 zpl_earlier_version(const char *name, int version)
230 boolean_t rc = B_TRUE;
232 if (dmu_objset_hold(name, FTAG, &os) == 0) {
235 if (dmu_objset_type(os) != DMU_OST_ZFS) {
236 dmu_objset_rele(os, FTAG);
239 /* XXX reading from non-owned objset */
240 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
241 rc = zplversion < version;
242 dmu_objset_rele(os, FTAG);
248 zfs_log_history(zfs_cmd_t *zc)
253 if ((buf = history_str_get(zc)) == NULL)
256 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
257 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
258 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
259 spa_close(spa, FTAG);
261 history_str_free(buf);
265 * Policy for top-level read operations (list pools). Requires no privileges,
266 * and can be used in the local zone, as there is no associated dataset.
270 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
276 * Policy for dataset read operations (list children, get statistics). Requires
277 * no privileges, but must be visible in the local zone.
281 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
283 if (INGLOBALZONE(curproc) ||
284 zone_dataset_visible(zc->zc_name, NULL))
291 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
296 * The dataset must be visible by this zone -- check this first
297 * so they don't see EPERM on something they shouldn't know about.
299 if (!INGLOBALZONE(curproc) &&
300 !zone_dataset_visible(dataset, &writable))
303 if (INGLOBALZONE(curproc)) {
305 * If the fs is zoned, only root can access it from the
308 if (secpolicy_zfs(cr) && zoned)
312 * If we are in a local zone, the 'zoned' property must be set.
317 /* must be writable by this zone */
325 zfs_dozonecheck(const char *dataset, cred_t *cr)
329 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
332 return (zfs_dozonecheck_impl(dataset, zoned, cr));
336 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
340 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
341 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
342 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
345 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
347 return (zfs_dozonecheck_impl(dataset, zoned, cr));
351 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
355 error = zfs_dozonecheck(name, cr);
357 error = secpolicy_zfs(cr);
359 error = dsl_deleg_access(name, perm, cr);
365 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
366 const char *perm, cred_t *cr)
370 error = zfs_dozonecheck_ds(name, ds, cr);
372 error = secpolicy_zfs(cr);
374 error = dsl_deleg_access_impl(ds, perm, cr);
380 * Policy for setting the security label property.
382 * Returns 0 for success, non-zero for access and other errors.
385 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
387 char ds_hexsl[MAXNAMELEN];
388 bslabel_t ds_sl, new_sl;
389 boolean_t new_default = FALSE;
391 int needed_priv = -1;
394 /* First get the existing dataset label. */
395 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
396 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
400 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
403 /* The label must be translatable */
404 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
408 * In a non-global zone, disallow attempts to set a label that
409 * doesn't match that of the zone; otherwise no other checks
412 if (!INGLOBALZONE(curproc)) {
413 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
419 * For global-zone datasets (i.e., those whose zoned property is
420 * "off", verify that the specified new label is valid for the
423 if (dsl_prop_get_integer(name,
424 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
427 if (zfs_check_global_label(name, strval) != 0)
432 * If the existing dataset label is nondefault, check if the
433 * dataset is mounted (label cannot be changed while mounted).
434 * Get the zfsvfs; if there isn't one, then the dataset isn't
435 * mounted (or isn't a dataset, doesn't exist, ...).
437 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
439 static char *setsl_tag = "setsl_tag";
442 * Try to own the dataset; abort if there is any error,
443 * (e.g., already mounted, in use, or other error).
445 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
450 dmu_objset_disown(os, setsl_tag);
453 needed_priv = PRIV_FILE_DOWNGRADE_SL;
457 if (hexstr_to_label(strval, &new_sl) != 0)
460 if (blstrictdom(&ds_sl, &new_sl))
461 needed_priv = PRIV_FILE_DOWNGRADE_SL;
462 else if (blstrictdom(&new_sl, &ds_sl))
463 needed_priv = PRIV_FILE_UPGRADE_SL;
465 /* dataset currently has a default label */
467 needed_priv = PRIV_FILE_UPGRADE_SL;
471 if (needed_priv != -1)
472 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
477 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
483 * Check permissions for special properties.
490 * Disallow setting of 'zoned' from within a local zone.
492 if (!INGLOBALZONE(curproc))
497 if (!INGLOBALZONE(curproc)) {
499 char setpoint[MAXNAMELEN];
501 * Unprivileged users are allowed to modify the
502 * quota on things *under* (ie. contained by)
503 * the thing they own.
505 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
508 if (!zoned || strlen(dsname) <= strlen(setpoint))
513 case ZFS_PROP_MLSLABEL:
514 if (!is_system_labeled())
517 if (nvpair_value_string(propval, &strval) == 0) {
520 err = zfs_set_slabel_policy(dsname, strval, CRED());
527 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
531 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
535 error = zfs_dozonecheck(zc->zc_name, cr);
540 * permission to set permissions will be evaluated later in
541 * dsl_deleg_can_allow()
547 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
549 return (zfs_secpolicy_write_perms(zc->zc_name,
550 ZFS_DELEG_PERM_ROLLBACK, cr));
554 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
563 * Generate the current snapshot name from the given objsetid, then
564 * use that name for the secpolicy/zone checks.
566 cp = strchr(zc->zc_name, '@');
569 error = spa_open(zc->zc_name, &spa, FTAG);
573 dp = spa_get_dsl(spa);
574 rw_enter(&dp->dp_config_rwlock, RW_READER);
575 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
576 rw_exit(&dp->dp_config_rwlock);
577 spa_close(spa, FTAG);
581 dsl_dataset_name(ds, zc->zc_name);
583 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
584 ZFS_DELEG_PERM_SEND, cr);
585 dsl_dataset_rele(ds, FTAG);
591 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
596 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
597 NO_FOLLOW, NULL, &vp)) != 0)
600 /* Now make sure mntpnt and dataset are ZFS */
602 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
603 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
604 zc->zc_name) != 0)) {
610 return (dsl_deleg_access(zc->zc_name,
611 ZFS_DELEG_PERM_SHARE, cr));
615 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
617 if (!INGLOBALZONE(curproc))
620 if (secpolicy_nfs(cr) == 0) {
623 return (zfs_secpolicy_deleg_share(zc, cr));
628 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
630 if (!INGLOBALZONE(curproc))
633 if (secpolicy_smb(cr) == 0) {
636 return (zfs_secpolicy_deleg_share(zc, cr));
641 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
646 * Remove the @bla or /bla from the end of the name to get the parent.
648 (void) strncpy(parent, datasetname, parentsize);
649 cp = strrchr(parent, '@');
653 cp = strrchr(parent, '/');
663 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
667 if ((error = zfs_secpolicy_write_perms(name,
668 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
671 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
675 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
677 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
681 * Destroying snapshots with delegated permissions requires
682 * descendent mount and destroy permissions.
683 * Reassemble the full filesystem@snap name so dsl_deleg_access()
684 * can do the correct permission check.
686 * Since this routine is used when doing a recursive destroy of snapshots
687 * and destroying snapshots requires descendent permissions, a successfull
688 * check of the top level snapshot applies to snapshots of all descendent
692 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
697 dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
699 error = zfs_secpolicy_destroy_perms(dsname, cr);
706 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
708 char parentname[MAXNAMELEN];
711 if ((error = zfs_secpolicy_write_perms(from,
712 ZFS_DELEG_PERM_RENAME, cr)) != 0)
715 if ((error = zfs_secpolicy_write_perms(from,
716 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
719 if ((error = zfs_get_parent(to, parentname,
720 sizeof (parentname))) != 0)
723 if ((error = zfs_secpolicy_write_perms(parentname,
724 ZFS_DELEG_PERM_CREATE, cr)) != 0)
727 if ((error = zfs_secpolicy_write_perms(parentname,
728 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
735 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
737 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
741 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
743 char parentname[MAXNAMELEN];
747 error = zfs_secpolicy_write_perms(zc->zc_name,
748 ZFS_DELEG_PERM_PROMOTE, cr);
752 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
755 dsl_dataset_t *pclone = NULL;
757 dd = clone->os_dsl_dataset->ds_dir;
759 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
760 error = dsl_dataset_hold_obj(dd->dd_pool,
761 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
762 rw_exit(&dd->dd_pool->dp_config_rwlock);
764 dmu_objset_rele(clone, FTAG);
768 error = zfs_secpolicy_write_perms(zc->zc_name,
769 ZFS_DELEG_PERM_MOUNT, cr);
771 dsl_dataset_name(pclone, parentname);
772 dmu_objset_rele(clone, FTAG);
773 dsl_dataset_rele(pclone, FTAG);
775 error = zfs_secpolicy_write_perms(parentname,
776 ZFS_DELEG_PERM_PROMOTE, cr);
782 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
786 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
787 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
790 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
791 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
794 return (zfs_secpolicy_write_perms(zc->zc_name,
795 ZFS_DELEG_PERM_CREATE, cr));
799 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
801 return (zfs_secpolicy_write_perms(name,
802 ZFS_DELEG_PERM_SNAPSHOT, cr));
806 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
809 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
813 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
815 char parentname[MAXNAMELEN];
818 if ((error = zfs_get_parent(zc->zc_name, parentname,
819 sizeof (parentname))) != 0)
822 if (zc->zc_value[0] != '\0') {
823 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
824 ZFS_DELEG_PERM_CLONE, cr)) != 0)
828 if ((error = zfs_secpolicy_write_perms(parentname,
829 ZFS_DELEG_PERM_CREATE, cr)) != 0)
832 error = zfs_secpolicy_write_perms(parentname,
833 ZFS_DELEG_PERM_MOUNT, cr);
839 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
843 error = secpolicy_fs_unmount(cr, NULL);
845 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
851 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
852 * SYS_CONFIG privilege, which is not available in a local zone.
856 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
858 if (secpolicy_sys_config(cr, B_FALSE) != 0)
865 * Policy for object to name lookups.
869 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
873 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
876 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
881 * Policy for fault injection. Requires all privileges.
885 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
887 return (secpolicy_zinject(cr));
891 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
893 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
895 if (prop == ZPROP_INVAL) {
896 if (!zfs_prop_user(zc->zc_value))
898 return (zfs_secpolicy_write_perms(zc->zc_name,
899 ZFS_DELEG_PERM_USERPROP, cr));
901 return (zfs_secpolicy_setprop(zc->zc_name, prop,
907 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
909 int err = zfs_secpolicy_read(zc, cr);
913 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
916 if (zc->zc_value[0] == 0) {
918 * They are asking about a posix uid/gid. If it's
919 * themself, allow it.
921 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
922 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
923 if (zc->zc_guid == crgetuid(cr))
926 if (groupmember(zc->zc_guid, cr))
931 return (zfs_secpolicy_write_perms(zc->zc_name,
932 userquota_perms[zc->zc_objset_type], cr));
936 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
938 int err = zfs_secpolicy_read(zc, cr);
942 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
945 return (zfs_secpolicy_write_perms(zc->zc_name,
946 userquota_perms[zc->zc_objset_type], cr));
950 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
952 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
957 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
959 return (zfs_secpolicy_write_perms(zc->zc_name,
960 ZFS_DELEG_PERM_HOLD, cr));
964 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
966 return (zfs_secpolicy_write_perms(zc->zc_name,
967 ZFS_DELEG_PERM_RELEASE, cr));
971 * Policy for allowing temporary snapshots to be taken or released
974 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
977 * A temporary snapshot is the same as a snapshot,
978 * hold, destroy and release all rolled into one.
979 * Delegated diff alone is sufficient that we allow this.
983 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
984 ZFS_DELEG_PERM_DIFF, cr)) == 0)
987 error = zfs_secpolicy_snapshot(zc, cr);
989 error = zfs_secpolicy_hold(zc, cr);
991 error = zfs_secpolicy_release(zc, cr);
993 error = zfs_secpolicy_destroy(zc, cr);
998 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1001 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1005 nvlist_t *list = NULL;
1008 * Read in and unpack the user-supplied nvlist.
1013 packed = kmem_alloc(size, KM_SLEEP);
1015 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1017 kmem_free(packed, size);
1021 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1022 kmem_free(packed, size);
1026 kmem_free(packed, size);
1033 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1037 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1039 if (size > zc->zc_nvlist_dst_size) {
1040 nvpair_t *more_errors;
1043 if (zc->zc_nvlist_dst_size < 1024)
1046 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1047 more_errors = nvlist_prev_nvpair(*errors, NULL);
1050 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1052 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1054 VERIFY(nvlist_size(*errors, &size,
1055 NV_ENCODE_NATIVE) == 0);
1056 } while (size > zc->zc_nvlist_dst_size);
1058 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1059 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1060 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1061 ASSERT(size <= zc->zc_nvlist_dst_size);
1068 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1070 char *packed = NULL;
1074 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1076 if (size > zc->zc_nvlist_dst_size) {
1079 packed = kmem_alloc(size, KM_SLEEP);
1080 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1082 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1083 size, zc->zc_iflags) != 0)
1085 kmem_free(packed, size);
1088 zc->zc_nvlist_dst_size = size;
1093 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1098 error = dmu_objset_hold(dsname, FTAG, &os);
1101 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1102 dmu_objset_rele(os, FTAG);
1106 mutex_enter(&os->os_user_ptr_lock);
1107 *zfvp = dmu_objset_get_user(os);
1109 VFS_HOLD((*zfvp)->z_vfs);
1113 mutex_exit(&os->os_user_ptr_lock);
1114 dmu_objset_rele(os, FTAG);
1119 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1120 * case its z_vfs will be NULL, and it will be opened as the owner.
1123 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1127 if (getzfsvfs(name, zfvp) != 0)
1128 error = zfsvfs_create(name, zfvp);
1130 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1132 if ((*zfvp)->z_unmounted) {
1134 * XXX we could probably try again, since the unmounting
1135 * thread should be just about to disassociate the
1136 * objset from the zfsvfs.
1138 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1146 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1148 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1150 if (zfsvfs->z_vfs) {
1151 VFS_RELE(zfsvfs->z_vfs);
1153 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1154 zfsvfs_free(zfsvfs);
1159 zfs_ioc_pool_create(zfs_cmd_t *zc)
1162 nvlist_t *config, *props = NULL;
1163 nvlist_t *rootprops = NULL;
1164 nvlist_t *zplprops = NULL;
1167 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1168 zc->zc_iflags, &config)))
1171 if (zc->zc_nvlist_src_size != 0 && (error =
1172 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1173 zc->zc_iflags, &props))) {
1174 nvlist_free(config);
1179 nvlist_t *nvl = NULL;
1180 uint64_t version = SPA_VERSION;
1182 (void) nvlist_lookup_uint64(props,
1183 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1184 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1186 goto pool_props_bad;
1188 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1190 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1192 nvlist_free(config);
1196 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1198 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1199 error = zfs_fill_zplprops_root(version, rootprops,
1202 goto pool_props_bad;
1205 buf = history_str_get(zc);
1207 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1210 * Set the remaining root properties
1212 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1213 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1214 (void) spa_destroy(zc->zc_name);
1217 history_str_free(buf);
1220 nvlist_free(rootprops);
1221 nvlist_free(zplprops);
1222 nvlist_free(config);
1229 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1232 zfs_log_history(zc);
1233 error = spa_destroy(zc->zc_name);
1235 zvol_remove_minors(zc->zc_name);
1240 zfs_ioc_pool_import(zfs_cmd_t *zc)
1242 nvlist_t *config, *props = NULL;
1246 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1247 zc->zc_iflags, &config)) != 0)
1250 if (zc->zc_nvlist_src_size != 0 && (error =
1251 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1252 zc->zc_iflags, &props))) {
1253 nvlist_free(config);
1257 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1258 guid != zc->zc_guid)
1261 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1263 if (zc->zc_nvlist_dst != 0) {
1266 if ((err = put_nvlist(zc, config)) != 0)
1270 nvlist_free(config);
1279 zfs_ioc_pool_export(zfs_cmd_t *zc)
1282 boolean_t force = (boolean_t)zc->zc_cookie;
1283 boolean_t hardforce = (boolean_t)zc->zc_guid;
1285 zfs_log_history(zc);
1286 error = spa_export(zc->zc_name, NULL, force, hardforce);
1288 zvol_remove_minors(zc->zc_name);
1293 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1298 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1301 error = put_nvlist(zc, configs);
1303 nvlist_free(configs);
1309 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1315 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1316 sizeof (zc->zc_value));
1318 if (config != NULL) {
1319 ret = put_nvlist(zc, config);
1320 nvlist_free(config);
1323 * The config may be present even if 'error' is non-zero.
1324 * In this case we return success, and preserve the real errno
1327 zc->zc_cookie = error;
1336 * Try to import the given pool, returning pool stats as appropriate so that
1337 * user land knows which devices are available and overall pool health.
1340 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1342 nvlist_t *tryconfig, *config;
1345 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1346 zc->zc_iflags, &tryconfig)) != 0)
1349 config = spa_tryimport(tryconfig);
1351 nvlist_free(tryconfig);
1356 error = put_nvlist(zc, config);
1357 nvlist_free(config);
1364 * zc_name name of the pool
1365 * zc_cookie scan func (pool_scan_func_t)
1368 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1373 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1376 if (zc->zc_cookie == POOL_SCAN_NONE)
1377 error = spa_scan_stop(spa);
1379 error = spa_scan(spa, zc->zc_cookie);
1381 spa_close(spa, FTAG);
1387 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1392 error = spa_open(zc->zc_name, &spa, FTAG);
1395 spa_close(spa, FTAG);
1401 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1406 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1409 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1410 spa_close(spa, FTAG);
1414 spa_upgrade(spa, zc->zc_cookie);
1415 spa_close(spa, FTAG);
1421 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1428 if ((size = zc->zc_history_len) == 0)
1431 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1434 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1435 spa_close(spa, FTAG);
1439 hist_buf = kmem_alloc(size, KM_SLEEP);
1440 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1441 &zc->zc_history_len, hist_buf)) == 0) {
1442 error = ddi_copyout(hist_buf,
1443 (void *)(uintptr_t)zc->zc_history,
1444 zc->zc_history_len, zc->zc_iflags);
1447 spa_close(spa, FTAG);
1448 kmem_free(hist_buf, size);
1453 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1457 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1465 * zc_name name of filesystem
1466 * zc_obj object to find
1469 * zc_value name of object
1472 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1477 /* XXX reading from objset not owned */
1478 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1480 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1481 dmu_objset_rele(os, FTAG);
1484 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1485 sizeof (zc->zc_value));
1486 dmu_objset_rele(os, FTAG);
1493 * zc_name name of filesystem
1494 * zc_obj object to find
1497 * zc_stat stats on object
1498 * zc_value path to object
1501 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1506 /* XXX reading from objset not owned */
1507 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1509 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1510 dmu_objset_rele(os, FTAG);
1513 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1514 sizeof (zc->zc_value));
1515 dmu_objset_rele(os, FTAG);
1521 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1525 nvlist_t *config, **l2cache, **spares;
1526 uint_t nl2cache = 0, nspares = 0;
1528 error = spa_open(zc->zc_name, &spa, FTAG);
1532 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1533 zc->zc_iflags, &config);
1534 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1535 &l2cache, &nl2cache);
1537 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1541 * A root pool with concatenated devices is not supported.
1542 * Thus, can not add a device to a root pool.
1544 * Intent log device can not be added to a rootpool because
1545 * during mountroot, zil is replayed, a seperated log device
1546 * can not be accessed during the mountroot time.
1548 * l2cache and spare devices are ok to be added to a rootpool.
1550 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1551 nvlist_free(config);
1552 spa_close(spa, FTAG);
1557 error = spa_vdev_add(spa, config);
1558 nvlist_free(config);
1560 spa_close(spa, FTAG);
1566 * zc_name name of the pool
1567 * zc_nvlist_conf nvlist of devices to remove
1568 * zc_cookie to stop the remove?
1571 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1576 error = spa_open(zc->zc_name, &spa, FTAG);
1579 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1580 spa_close(spa, FTAG);
1585 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1589 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1591 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1593 switch (zc->zc_cookie) {
1594 case VDEV_STATE_ONLINE:
1595 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1598 case VDEV_STATE_OFFLINE:
1599 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1602 case VDEV_STATE_FAULTED:
1603 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1604 zc->zc_obj != VDEV_AUX_EXTERNAL)
1605 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1607 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1610 case VDEV_STATE_DEGRADED:
1611 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1612 zc->zc_obj != VDEV_AUX_EXTERNAL)
1613 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1615 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1621 zc->zc_cookie = newstate;
1622 spa_close(spa, FTAG);
1627 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1630 int replacing = zc->zc_cookie;
1634 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1637 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1638 zc->zc_iflags, &config)) == 0) {
1639 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1640 nvlist_free(config);
1643 spa_close(spa, FTAG);
1648 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1653 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1656 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1658 spa_close(spa, FTAG);
1663 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1666 nvlist_t *config, *props = NULL;
1668 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1670 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1673 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1674 zc->zc_iflags, &config))) {
1675 spa_close(spa, FTAG);
1679 if (zc->zc_nvlist_src_size != 0 && (error =
1680 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1681 zc->zc_iflags, &props))) {
1682 spa_close(spa, FTAG);
1683 nvlist_free(config);
1687 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1689 spa_close(spa, FTAG);
1691 nvlist_free(config);
1698 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1701 char *path = zc->zc_value;
1702 uint64_t guid = zc->zc_guid;
1705 error = spa_open(zc->zc_name, &spa, FTAG);
1709 error = spa_vdev_setpath(spa, guid, path);
1710 spa_close(spa, FTAG);
1715 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1718 char *fru = zc->zc_value;
1719 uint64_t guid = zc->zc_guid;
1722 error = spa_open(zc->zc_name, &spa, FTAG);
1726 error = spa_vdev_setfru(spa, guid, fru);
1727 spa_close(spa, FTAG);
1732 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1737 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1739 if (zc->zc_nvlist_dst != 0 &&
1740 (error = dsl_prop_get_all(os, &nv)) == 0) {
1741 dmu_objset_stats(os, nv);
1743 * NB: zvol_get_stats() will read the objset contents,
1744 * which we aren't supposed to do with a
1745 * DS_MODE_USER hold, because it could be
1746 * inconsistent. So this is a bit of a workaround...
1747 * XXX reading with out owning
1749 if (!zc->zc_objset_stats.dds_inconsistent) {
1750 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1751 error = zvol_get_stats(os, nv);
1754 error = put_nvlist(zc, nv);
1763 * zc_name name of filesystem
1764 * zc_nvlist_dst_size size of buffer for property nvlist
1767 * zc_objset_stats stats
1768 * zc_nvlist_dst property nvlist
1769 * zc_nvlist_dst_size size of property nvlist
1772 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1774 objset_t *os = NULL;
1777 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1780 error = zfs_ioc_objset_stats_impl(zc, os);
1782 dmu_objset_rele(os, FTAG);
1789 * zc_name name of filesystem
1790 * zc_nvlist_dst_size size of buffer for property nvlist
1793 * zc_nvlist_dst received property nvlist
1794 * zc_nvlist_dst_size size of received property nvlist
1796 * Gets received properties (distinct from local properties on or after
1797 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1798 * local property values.
1801 zfs_ioc_objset_recvd_props(struct file *filp, zfs_cmd_t *zc)
1803 objset_t *os = NULL;
1807 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1811 * Without this check, we would return local property values if the
1812 * caller has not already received properties on or after
1813 * SPA_VERSION_RECVD_PROPS.
1815 if (!dsl_prop_get_hasrecvd(os)) {
1816 dmu_objset_rele(os, FTAG);
1820 if (zc->zc_nvlist_dst != 0 &&
1821 (error = dsl_prop_get_received(os, &nv)) == 0) {
1822 error = put_nvlist(zc, nv);
1826 dmu_objset_rele(os, FTAG);
1831 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1837 * zfs_get_zplprop() will either find a value or give us
1838 * the default value (if there is one).
1840 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1842 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1848 * zc_name name of filesystem
1849 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1852 * zc_nvlist_dst zpl property nvlist
1853 * zc_nvlist_dst_size size of zpl property nvlist
1856 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1861 /* XXX reading without owning */
1862 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1865 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1868 * NB: nvl_add_zplprop() will read the objset contents,
1869 * which we aren't supposed to do with a DS_MODE_USER
1870 * hold, because it could be inconsistent.
1872 if (zc->zc_nvlist_dst != 0 &&
1873 !zc->zc_objset_stats.dds_inconsistent &&
1874 dmu_objset_type(os) == DMU_OST_ZFS) {
1877 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1878 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1879 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1880 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1881 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1882 err = put_nvlist(zc, nv);
1887 dmu_objset_rele(os, FTAG);
1892 dataset_name_hidden(const char *name)
1895 * Skip over datasets that are not visible in this zone,
1896 * internal datasets (which have a $ in their name), and
1897 * temporary datasets (which have a % in their name).
1899 if (strchr(name, '$') != NULL)
1901 if (strchr(name, '%') != NULL)
1903 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1910 * zc_name name of filesystem
1911 * zc_cookie zap cursor
1912 * zc_nvlist_dst_size size of buffer for property nvlist
1915 * zc_name name of next filesystem
1916 * zc_cookie zap cursor
1917 * zc_objset_stats stats
1918 * zc_nvlist_dst property nvlist
1919 * zc_nvlist_dst_size size of property nvlist
1922 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1927 size_t orig_len = strlen(zc->zc_name);
1930 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1931 if (error == ENOENT)
1936 p = strrchr(zc->zc_name, '/');
1937 if (p == NULL || p[1] != '\0')
1938 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1939 p = zc->zc_name + strlen(zc->zc_name);
1942 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1943 * but is not declared void because its called by dmu_objset_find().
1945 if (zc->zc_cookie == 0) {
1946 uint64_t cookie = 0;
1947 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1949 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1950 (void) dmu_objset_prefetch(p, NULL);
1954 error = dmu_dir_list_next(os,
1955 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1956 NULL, &zc->zc_cookie);
1957 if (error == ENOENT)
1959 } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1960 !(zc->zc_iflags & FKIOCTL));
1961 dmu_objset_rele(os, FTAG);
1964 * If it's an internal dataset (ie. with a '$' in its name),
1965 * don't try to get stats for it, otherwise we'll return ENOENT.
1967 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1968 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1969 if (error == ENOENT) {
1970 /* We lost a race with destroy, get the next one. */
1971 zc->zc_name[orig_len] = '\0';
1980 * zc_name name of filesystem
1981 * zc_cookie zap cursor
1982 * zc_nvlist_dst_size size of buffer for property nvlist
1985 * zc_name name of next snapshot
1986 * zc_objset_stats stats
1987 * zc_nvlist_dst property nvlist
1988 * zc_nvlist_dst_size size of property nvlist
1991 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1997 if (zc->zc_cookie == 0)
1998 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1999 NULL, DS_FIND_SNAPSHOTS);
2001 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2003 return (error == ENOENT ? ESRCH : error);
2006 * A dataset name of maximum length cannot have any snapshots,
2007 * so exit immediately.
2009 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2010 dmu_objset_rele(os, FTAG);
2014 error = dmu_snapshot_list_next(os,
2015 sizeof (zc->zc_name) - strlen(zc->zc_name),
2016 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2021 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2024 * Since we probably don't have a hold on this snapshot,
2025 * it's possible that the objsetid could have been destroyed
2026 * and reused for a new objset. It's OK if this happens during
2027 * a zfs send operation, since the new createtxg will be
2028 * beyond the range we're interested in.
2030 rw_enter(&dp->dp_config_rwlock, RW_READER);
2031 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2032 rw_exit(&dp->dp_config_rwlock);
2034 if (error == ENOENT) {
2035 /* Racing with destroy, get the next one. */
2036 *strchr(zc->zc_name, '@') = '\0';
2037 dmu_objset_rele(os, FTAG);
2043 error = dmu_objset_from_ds(ds, &ossnap);
2045 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2046 dsl_dataset_rele(ds, FTAG);
2048 } else if (error == ENOENT) {
2052 dmu_objset_rele(os, FTAG);
2053 /* if we failed, undo the @ that we tacked on to zc_name */
2055 *strchr(zc->zc_name, '@') = '\0';
2060 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2062 const char *propname = nvpair_name(pair);
2064 unsigned int vallen;
2067 zfs_userquota_prop_t type;
2073 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2075 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2076 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2082 * A correctly constructed propname is encoded as
2083 * userquota@<rid>-<domain>.
2085 if ((dash = strchr(propname, '-')) == NULL ||
2086 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2095 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2097 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2098 zfsvfs_rele(zfsvfs, FTAG);
2105 * If the named property is one that has a special function to set its value,
2106 * return 0 on success and a positive error code on failure; otherwise if it is
2107 * not one of the special properties handled by this function, return -1.
2109 * XXX: It would be better for callers of the property interface if we handled
2110 * these special cases in dsl_prop.c (in the dsl layer).
2113 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2116 const char *propname = nvpair_name(pair);
2117 zfs_prop_t prop = zfs_name_to_prop(propname);
2121 if (prop == ZPROP_INVAL) {
2122 if (zfs_prop_userquota(propname))
2123 return (zfs_prop_set_userquota(dsname, pair));
2127 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2129 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2130 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2134 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2137 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2140 case ZFS_PROP_QUOTA:
2141 err = dsl_dir_set_quota(dsname, source, intval);
2143 case ZFS_PROP_REFQUOTA:
2144 err = dsl_dataset_set_quota(dsname, source, intval);
2146 case ZFS_PROP_RESERVATION:
2147 err = dsl_dir_set_reservation(dsname, source, intval);
2149 case ZFS_PROP_REFRESERVATION:
2150 err = dsl_dataset_set_reservation(dsname, source, intval);
2152 case ZFS_PROP_VOLSIZE:
2153 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2156 case ZFS_PROP_VERSION:
2160 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2163 err = zfs_set_version(zfsvfs, intval);
2164 zfsvfs_rele(zfsvfs, FTAG);
2166 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2169 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2170 (void) strcpy(zc->zc_name, dsname);
2171 (void) zfs_ioc_userspace_upgrade(zc);
2172 kmem_free(zc, sizeof (zfs_cmd_t));
2185 * This function is best effort. If it fails to set any of the given properties,
2186 * it continues to set as many as it can and returns the first error
2187 * encountered. If the caller provides a non-NULL errlist, it also gives the
2188 * complete list of names of all the properties it failed to set along with the
2189 * corresponding error numbers. The caller is responsible for freeing the
2192 * If every property is set successfully, zero is returned and the list pointed
2193 * at by errlist is NULL.
2196 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2204 nvlist_t *genericnvl;
2208 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2209 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2210 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2214 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2215 const char *propname = nvpair_name(pair);
2216 zfs_prop_t prop = zfs_name_to_prop(propname);
2219 /* decode the property value */
2221 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2223 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2224 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2229 /* Validate value type */
2230 if (err == 0 && prop == ZPROP_INVAL) {
2231 if (zfs_prop_user(propname)) {
2232 if (nvpair_type(propval) != DATA_TYPE_STRING)
2234 } else if (zfs_prop_userquota(propname)) {
2235 if (nvpair_type(propval) !=
2236 DATA_TYPE_UINT64_ARRAY)
2239 } else if (err == 0) {
2240 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2241 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2243 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2246 VERIFY(nvpair_value_uint64(propval,
2249 switch (zfs_prop_get_type(prop)) {
2250 case PROP_TYPE_NUMBER:
2252 case PROP_TYPE_STRING:
2255 case PROP_TYPE_INDEX:
2256 if (zfs_prop_index_to_string(prop,
2257 intval, &unused) != 0)
2262 "unknown property type");
2269 /* Validate permissions */
2271 err = zfs_check_settable(dsname, pair, CRED());
2274 err = zfs_prop_set_special(dsname, source, pair);
2277 * For better performance we build up a list of
2278 * properties to set in a single transaction.
2280 err = nvlist_add_nvpair(genericnvl, pair);
2281 } else if (err != 0 && nvl != retrynvl) {
2283 * This may be a spurious error caused by
2284 * receiving quota and reservation out of order.
2285 * Try again in a second pass.
2287 err = nvlist_add_nvpair(retrynvl, pair);
2292 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2295 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2300 if (!nvlist_empty(genericnvl) &&
2301 dsl_props_set(dsname, source, genericnvl) != 0) {
2303 * If this fails, we still want to set as many properties as we
2304 * can, so try setting them individually.
2307 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2308 const char *propname = nvpair_name(pair);
2312 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2314 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2315 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2319 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2320 VERIFY(nvpair_value_string(propval,
2322 err = dsl_prop_set(dsname, propname, source, 1,
2323 strlen(strval) + 1, strval);
2325 VERIFY(nvpair_value_uint64(propval,
2327 err = dsl_prop_set(dsname, propname, source, 8,
2332 VERIFY(nvlist_add_int32(errors, propname,
2337 nvlist_free(genericnvl);
2338 nvlist_free(retrynvl);
2340 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2341 nvlist_free(errors);
2344 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2347 if (errlist == NULL)
2348 nvlist_free(errors);
2356 * Check that all the properties are valid user properties.
2359 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2361 nvpair_t *pair = NULL;
2364 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2365 const char *propname = nvpair_name(pair);
2368 if (!zfs_prop_user(propname) ||
2369 nvpair_type(pair) != DATA_TYPE_STRING)
2372 if ((error = zfs_secpolicy_write_perms(fsname,
2373 ZFS_DELEG_PERM_USERPROP, CRED())))
2376 if (strlen(propname) >= ZAP_MAXNAMELEN)
2377 return (ENAMETOOLONG);
2379 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2380 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2387 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2391 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2394 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2395 if (nvlist_exists(skipped, nvpair_name(pair)))
2398 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2403 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2407 nvlist_t *cleared_props = NULL;
2408 props_skip(props, skipped, &cleared_props);
2409 if (!nvlist_empty(cleared_props)) {
2411 * Acts on local properties until the dataset has received
2412 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2414 zprop_source_t flags = (ZPROP_SRC_NONE |
2415 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2416 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2418 nvlist_free(cleared_props);
2424 * zc_name name of filesystem
2425 * zc_value name of property to set
2426 * zc_nvlist_src{_size} nvlist of properties to apply
2427 * zc_cookie received properties flag
2430 * zc_nvlist_dst{_size} error for each unapplied received property
2433 zfs_ioc_set_prop(zfs_cmd_t *zc)
2436 boolean_t received = zc->zc_cookie;
2437 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2439 nvlist_t *errors = NULL;
2442 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2443 zc->zc_iflags, &nvl)) != 0)
2447 nvlist_t *origprops;
2450 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2451 if (dsl_prop_get_received(os, &origprops) == 0) {
2452 (void) clear_received_props(os,
2453 zc->zc_name, origprops, nvl);
2454 nvlist_free(origprops);
2457 dsl_prop_set_hasrecvd(os);
2458 dmu_objset_rele(os, FTAG);
2462 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2464 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2465 (void) put_nvlist(zc, errors);
2468 nvlist_free(errors);
2475 * zc_name name of filesystem
2476 * zc_value name of property to inherit
2477 * zc_cookie revert to received value if TRUE
2482 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2484 const char *propname = zc->zc_value;
2485 zfs_prop_t prop = zfs_name_to_prop(propname);
2486 boolean_t received = zc->zc_cookie;
2487 zprop_source_t source = (received
2488 ? ZPROP_SRC_NONE /* revert to received value, if any */
2489 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2498 * zfs_prop_set_special() expects properties in the form of an
2499 * nvpair with type info.
2501 if (prop == ZPROP_INVAL) {
2502 if (!zfs_prop_user(propname))
2505 type = PROP_TYPE_STRING;
2506 } else if (prop == ZFS_PROP_VOLSIZE ||
2507 prop == ZFS_PROP_VERSION) {
2510 type = zfs_prop_get_type(prop);
2513 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2516 case PROP_TYPE_STRING:
2517 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2519 case PROP_TYPE_NUMBER:
2520 case PROP_TYPE_INDEX:
2521 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2528 pair = nvlist_next_nvpair(dummy, NULL);
2529 err = zfs_prop_set_special(zc->zc_name, source, pair);
2532 return (err); /* special property already handled */
2535 * Only check this in the non-received case. We want to allow
2536 * 'inherit -S' to revert non-inheritable properties like quota
2537 * and reservation to the received or default values even though
2538 * they are not considered inheritable.
2540 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2544 /* the property name has been validated by zfs_secpolicy_inherit() */
2545 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2549 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2556 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2557 zc->zc_iflags, &props)))
2561 * If the only property is the configfile, then just do a spa_lookup()
2562 * to handle the faulted case.
2564 pair = nvlist_next_nvpair(props, NULL);
2565 if (pair != NULL && strcmp(nvpair_name(pair),
2566 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2567 nvlist_next_nvpair(props, pair) == NULL) {
2568 mutex_enter(&spa_namespace_lock);
2569 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2570 spa_configfile_set(spa, props, B_FALSE);
2571 spa_config_sync(spa, B_FALSE, B_TRUE);
2573 mutex_exit(&spa_namespace_lock);
2580 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2585 error = spa_prop_set(spa, props);
2588 spa_close(spa, FTAG);
2594 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2598 nvlist_t *nvp = NULL;
2600 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2602 * If the pool is faulted, there may be properties we can still
2603 * get (such as altroot and cachefile), so attempt to get them
2606 mutex_enter(&spa_namespace_lock);
2607 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2608 error = spa_prop_get(spa, &nvp);
2609 mutex_exit(&spa_namespace_lock);
2611 error = spa_prop_get(spa, &nvp);
2612 spa_close(spa, FTAG);
2615 if (error == 0 && zc->zc_nvlist_dst != 0)
2616 error = put_nvlist(zc, nvp);
2626 * zc_name name of filesystem
2627 * zc_nvlist_src{_size} nvlist of delegated permissions
2628 * zc_perm_action allow/unallow flag
2633 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2636 nvlist_t *fsaclnv = NULL;
2638 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2639 zc->zc_iflags, &fsaclnv)) != 0)
2643 * Verify nvlist is constructed correctly
2645 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2646 nvlist_free(fsaclnv);
2651 * If we don't have PRIV_SYS_MOUNT, then validate
2652 * that user is allowed to hand out each permission in
2656 error = secpolicy_zfs(CRED());
2658 if (zc->zc_perm_action == B_FALSE) {
2659 error = dsl_deleg_can_allow(zc->zc_name,
2662 error = dsl_deleg_can_unallow(zc->zc_name,
2668 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2670 nvlist_free(fsaclnv);
2676 * zc_name name of filesystem
2679 * zc_nvlist_src{_size} nvlist of delegated permissions
2682 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2687 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2688 error = put_nvlist(zc, nvp);
2696 * Search the vfs list for a specified resource. Returns a pointer to it
2697 * or NULL if no suitable entry is found. The caller of this routine
2698 * is responsible for releasing the returned vfs pointer.
2701 zfs_get_vfs(const char *resource)
2704 struct vfs *vfs_found = NULL;
2706 vfs_list_read_lock();
2709 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2714 vfsp = vfsp->vfs_next;
2715 } while (vfsp != rootvfs);
2722 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2724 zfs_creat_t *zct = arg;
2726 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2729 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2733 * createprops list of properties requested by creator
2734 * default_zplver zpl version to use if unspecified in createprops
2735 * fuids_ok fuids allowed in this version of the spa?
2736 * os parent objset pointer (NULL if root fs)
2739 * zplprops values for the zplprops we attach to the master node object
2740 * is_ci true if requested file system will be purely case-insensitive
2742 * Determine the settings for utf8only, normalization and
2743 * casesensitivity. Specific values may have been requested by the
2744 * creator and/or we can inherit values from the parent dataset. If
2745 * the file system is of too early a vintage, a creator can not
2746 * request settings for these properties, even if the requested
2747 * setting is the default value. We don't actually want to create dsl
2748 * properties for these, so remove them from the source nvlist after
2752 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2753 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2754 nvlist_t *zplprops, boolean_t *is_ci)
2756 uint64_t sense = ZFS_PROP_UNDEFINED;
2757 uint64_t norm = ZFS_PROP_UNDEFINED;
2758 uint64_t u8 = ZFS_PROP_UNDEFINED;
2760 ASSERT(zplprops != NULL);
2763 * Pull out creator prop choices, if any.
2766 (void) nvlist_lookup_uint64(createprops,
2767 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2768 (void) nvlist_lookup_uint64(createprops,
2769 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2770 (void) nvlist_remove_all(createprops,
2771 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2772 (void) nvlist_lookup_uint64(createprops,
2773 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2774 (void) nvlist_remove_all(createprops,
2775 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2776 (void) nvlist_lookup_uint64(createprops,
2777 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2778 (void) nvlist_remove_all(createprops,
2779 zfs_prop_to_name(ZFS_PROP_CASE));
2783 * If the zpl version requested is whacky or the file system
2784 * or pool is version is too "young" to support normalization
2785 * and the creator tried to set a value for one of the props,
2788 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2789 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2790 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2791 (zplver < ZPL_VERSION_NORMALIZATION &&
2792 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2793 sense != ZFS_PROP_UNDEFINED)))
2797 * Put the version in the zplprops
2799 VERIFY(nvlist_add_uint64(zplprops,
2800 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2802 if (norm == ZFS_PROP_UNDEFINED)
2803 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2804 VERIFY(nvlist_add_uint64(zplprops,
2805 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2808 * If we're normalizing, names must always be valid UTF-8 strings.
2812 if (u8 == ZFS_PROP_UNDEFINED)
2813 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2814 VERIFY(nvlist_add_uint64(zplprops,
2815 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2817 if (sense == ZFS_PROP_UNDEFINED)
2818 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2819 VERIFY(nvlist_add_uint64(zplprops,
2820 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2823 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2829 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2830 nvlist_t *zplprops, boolean_t *is_ci)
2832 boolean_t fuids_ok, sa_ok;
2833 uint64_t zplver = ZPL_VERSION;
2834 objset_t *os = NULL;
2835 char parentname[MAXNAMELEN];
2841 (void) strlcpy(parentname, dataset, sizeof (parentname));
2842 cp = strrchr(parentname, '/');
2846 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2849 spa_vers = spa_version(spa);
2850 spa_close(spa, FTAG);
2852 zplver = zfs_zpl_version_map(spa_vers);
2853 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2854 sa_ok = (zplver >= ZPL_VERSION_SA);
2857 * Open parent object set so we can inherit zplprop values.
2859 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2862 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2864 dmu_objset_rele(os, FTAG);
2869 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2870 nvlist_t *zplprops, boolean_t *is_ci)
2874 uint64_t zplver = ZPL_VERSION;
2877 zplver = zfs_zpl_version_map(spa_vers);
2878 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2879 sa_ok = (zplver >= ZPL_VERSION_SA);
2881 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2882 createprops, zplprops, is_ci);
2888 * zc_objset_type type of objset to create (fs vs zvol)
2889 * zc_name name of new objset
2890 * zc_value name of snapshot to clone from (may be empty)
2891 * zc_nvlist_src{_size} nvlist of properties to apply
2896 zfs_ioc_create(zfs_cmd_t *zc)
2901 nvlist_t *nvprops = NULL;
2902 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2903 dmu_objset_type_t type = zc->zc_objset_type;
2908 cbfunc = zfs_create_cb;
2912 cbfunc = zvol_create_cb;
2919 if (strchr(zc->zc_name, '@') ||
2920 strchr(zc->zc_name, '%'))
2923 if (zc->zc_nvlist_src != 0 &&
2924 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2925 zc->zc_iflags, &nvprops)) != 0)
2928 zct.zct_zplprops = NULL;
2929 zct.zct_props = nvprops;
2931 if (zc->zc_value[0] != '\0') {
2933 * We're creating a clone of an existing snapshot.
2935 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2936 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2937 nvlist_free(nvprops);
2941 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2943 nvlist_free(nvprops);
2947 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2948 dmu_objset_rele(clone, FTAG);
2950 nvlist_free(nvprops);
2954 boolean_t is_insensitive = B_FALSE;
2956 if (cbfunc == NULL) {
2957 nvlist_free(nvprops);
2961 if (type == DMU_OST_ZVOL) {
2962 uint64_t volsize, volblocksize;
2964 if (nvprops == NULL ||
2965 nvlist_lookup_uint64(nvprops,
2966 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2968 nvlist_free(nvprops);
2972 if ((error = nvlist_lookup_uint64(nvprops,
2973 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2974 &volblocksize)) != 0 && error != ENOENT) {
2975 nvlist_free(nvprops);
2980 volblocksize = zfs_prop_default_numeric(
2981 ZFS_PROP_VOLBLOCKSIZE);
2983 if ((error = zvol_check_volblocksize(
2984 volblocksize)) != 0 ||
2985 (error = zvol_check_volsize(volsize,
2986 volblocksize)) != 0) {
2987 nvlist_free(nvprops);
2990 } else if (type == DMU_OST_ZFS) {
2994 * We have to have normalization and
2995 * case-folding flags correct when we do the
2996 * file system creation, so go figure them out
2999 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3000 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3001 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3002 zct.zct_zplprops, &is_insensitive);
3004 nvlist_free(nvprops);
3005 nvlist_free(zct.zct_zplprops);
3009 error = dmu_objset_create(zc->zc_name, type,
3010 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3011 nvlist_free(zct.zct_zplprops);
3015 * It would be nice to do this atomically.
3018 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3021 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3023 nvlist_free(nvprops);
3029 * zc_name name of filesystem
3030 * zc_value short name of snapshot
3031 * zc_cookie recursive flag
3032 * zc_nvlist_src[_size] property list
3035 * zc_value short snapname (i.e. part after the '@')
3038 zfs_ioc_snapshot(zfs_cmd_t *zc)
3040 nvlist_t *nvprops = NULL;
3042 boolean_t recursive = zc->zc_cookie;
3044 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3047 if (zc->zc_nvlist_src != 0 &&
3048 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3049 zc->zc_iflags, &nvprops)) != 0)
3052 error = zfs_check_userprops(zc->zc_name, nvprops);
3056 if (!nvlist_empty(nvprops) &&
3057 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3062 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3063 nvprops, recursive, B_FALSE, -1);
3066 nvlist_free(nvprops);
3071 zfs_unmount_snap(const char *name, void *arg)
3076 char *snapname = arg;
3077 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3078 vfsp = zfs_get_vfs(fullname);
3080 } else if (strchr(name, '@')) {
3081 vfsp = zfs_get_vfs(name);
3086 * Always force the unmount for snapshots.
3088 int flag = MS_FORCE;
3091 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3096 if ((err = dounmount(vfsp, flag, kcred)) != 0)
3104 * zc_name name of filesystem
3105 * zc_value short name of snapshot
3106 * zc_defer_destroy mark for deferred destroy
3111 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3115 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3117 err = dmu_objset_find(zc->zc_name,
3118 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3121 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3122 zc->zc_defer_destroy));
3127 * zc_name name of dataset to destroy
3128 * zc_objset_type type of objset
3129 * zc_defer_destroy mark for deferred destroy
3134 zfs_ioc_destroy(zfs_cmd_t *zc)
3137 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3138 err = zfs_unmount_snap(zc->zc_name, NULL);
3143 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3144 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3145 (void) zvol_remove_minor(zc->zc_name);
3151 * zc_name name of dataset to rollback (to most recent snapshot)
3156 zfs_ioc_rollback(zfs_cmd_t *zc)
3158 dsl_dataset_t *ds, *clone;
3163 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3167 /* must not be a snapshot */
3168 if (dsl_dataset_is_snapshot(ds)) {
3169 dsl_dataset_rele(ds, FTAG);
3173 /* must have a most recent snapshot */
3174 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3175 dsl_dataset_rele(ds, FTAG);
3180 * Create clone of most recent snapshot.
3182 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3183 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3187 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3194 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3195 error = zfs_suspend_fs(zfsvfs);
3199 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3200 error = dsl_dataset_clone_swap(clone, ds,
3202 dsl_dataset_disown(ds, FTAG);
3207 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3208 error = error ? error : resume_err;
3210 VFS_RELE(zfsvfs->z_vfs);
3212 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3213 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3214 dsl_dataset_disown(ds, FTAG);
3222 * Destroy clone (which also closes it).
3224 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3227 strfree(clone_name);
3229 dsl_dataset_rele(ds, FTAG);
3235 * zc_name old name of dataset
3236 * zc_value new name of dataset
3237 * zc_cookie recursive flag (only valid for snapshots)
3242 zfs_ioc_rename(zfs_cmd_t *zc)
3244 boolean_t recursive = zc->zc_cookie & 1;
3246 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3247 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3248 strchr(zc->zc_value, '%'))
3252 * Unmount snapshot unless we're doing a recursive rename,
3253 * in which case the dataset code figures out which snapshots
3256 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3257 zc->zc_objset_type == DMU_OST_ZFS) {
3258 int err = zfs_unmount_snap(zc->zc_name, NULL);
3262 if (zc->zc_objset_type == DMU_OST_ZVOL)
3263 (void) zvol_remove_minor(zc->zc_name);
3264 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3268 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3270 const char *propname = nvpair_name(pair);
3271 boolean_t issnap = (strchr(dsname, '@') != NULL);
3272 zfs_prop_t prop = zfs_name_to_prop(propname);
3276 if (prop == ZPROP_INVAL) {
3277 if (zfs_prop_user(propname)) {
3278 if ((err = zfs_secpolicy_write_perms(dsname,
3279 ZFS_DELEG_PERM_USERPROP, cr)))
3284 if (!issnap && zfs_prop_userquota(propname)) {
3285 const char *perm = NULL;
3286 const char *uq_prefix =
3287 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3288 const char *gq_prefix =
3289 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3291 if (strncmp(propname, uq_prefix,
3292 strlen(uq_prefix)) == 0) {
3293 perm = ZFS_DELEG_PERM_USERQUOTA;
3294 } else if (strncmp(propname, gq_prefix,
3295 strlen(gq_prefix)) == 0) {
3296 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3298 /* USERUSED and GROUPUSED are read-only */
3302 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3313 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3315 * dsl_prop_get_all_impl() returns properties in this
3319 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3320 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3325 * Check that this value is valid for this pool version
3328 case ZFS_PROP_COMPRESSION:
3330 * If the user specified gzip compression, make sure
3331 * the SPA supports it. We ignore any errors here since
3332 * we'll catch them later.
3334 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3335 nvpair_value_uint64(pair, &intval) == 0) {
3336 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3337 intval <= ZIO_COMPRESS_GZIP_9 &&
3338 zfs_earlier_version(dsname,
3339 SPA_VERSION_GZIP_COMPRESSION)) {
3343 if (intval == ZIO_COMPRESS_ZLE &&
3344 zfs_earlier_version(dsname,
3345 SPA_VERSION_ZLE_COMPRESSION))
3349 * If this is a bootable dataset then
3350 * verify that the compression algorithm
3351 * is supported for booting. We must return
3352 * something other than ENOTSUP since it
3353 * implies a downrev pool version.
3355 if (zfs_is_bootfs(dsname) &&
3356 !BOOTFS_COMPRESS_VALID(intval)) {
3362 case ZFS_PROP_COPIES:
3363 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3367 case ZFS_PROP_DEDUP:
3368 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3372 case ZFS_PROP_SHARESMB:
3373 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3377 case ZFS_PROP_ACLINHERIT:
3378 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3379 nvpair_value_uint64(pair, &intval) == 0) {
3380 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3381 zfs_earlier_version(dsname,
3382 SPA_VERSION_PASSTHROUGH_X))
3390 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3394 * Removes properties from the given props list that fail permission checks
3395 * needed to clear them and to restore them in case of a receive error. For each
3396 * property, make sure we have both set and inherit permissions.
3398 * Returns the first error encountered if any permission checks fail. If the
3399 * caller provides a non-NULL errlist, it also gives the complete list of names
3400 * of all the properties that failed a permission check along with the
3401 * corresponding error numbers. The caller is responsible for freeing the
3404 * If every property checks out successfully, zero is returned and the list
3405 * pointed at by errlist is NULL.
3408 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3411 nvpair_t *pair, *next_pair;
3418 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3420 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3421 (void) strcpy(zc->zc_name, dataset);
3422 pair = nvlist_next_nvpair(props, NULL);
3423 while (pair != NULL) {
3424 next_pair = nvlist_next_nvpair(props, pair);
3426 (void) strcpy(zc->zc_value, nvpair_name(pair));
3427 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3428 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3429 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3430 VERIFY(nvlist_add_int32(errors,
3431 zc->zc_value, err) == 0);
3435 kmem_free(zc, sizeof (zfs_cmd_t));
3437 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3438 nvlist_free(errors);
3441 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3444 if (errlist == NULL)
3445 nvlist_free(errors);
3453 propval_equals(nvpair_t *p1, nvpair_t *p2)
3455 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3456 /* dsl_prop_get_all_impl() format */
3458 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3459 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3463 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3465 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3466 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3470 if (nvpair_type(p1) != nvpair_type(p2))
3473 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3474 char *valstr1, *valstr2;
3476 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3477 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3478 return (strcmp(valstr1, valstr2) == 0);
3480 uint64_t intval1, intval2;
3482 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3483 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3484 return (intval1 == intval2);
3489 * Remove properties from props if they are not going to change (as determined
3490 * by comparison with origprops). Remove them from origprops as well, since we
3491 * do not need to clear or restore properties that won't change.
3494 props_reduce(nvlist_t *props, nvlist_t *origprops)
3496 nvpair_t *pair, *next_pair;
3498 if (origprops == NULL)
3499 return; /* all props need to be received */
3501 pair = nvlist_next_nvpair(props, NULL);
3502 while (pair != NULL) {
3503 const char *propname = nvpair_name(pair);
3506 next_pair = nvlist_next_nvpair(props, pair);
3508 if ((nvlist_lookup_nvpair(origprops, propname,
3509 &match) != 0) || !propval_equals(pair, match))
3510 goto next; /* need to set received value */
3512 /* don't clear the existing received value */
3513 (void) nvlist_remove_nvpair(origprops, match);
3514 /* don't bother receiving the property */
3515 (void) nvlist_remove_nvpair(props, pair);
3522 static boolean_t zfs_ioc_recv_inject_err;
3527 * zc_name name of containing filesystem
3528 * zc_nvlist_src{_size} nvlist of properties to apply
3529 * zc_value name of snapshot to create
3530 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3531 * zc_cookie file descriptor to recv from
3532 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3533 * zc_guid force flag
3534 * zc_cleanup_fd cleanup-on-exit file descriptor
3535 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3538 * zc_cookie number of bytes read
3539 * zc_nvlist_dst{_size} error for each unapplied received property
3540 * zc_obj zprop_errflags_t
3541 * zc_action_handle handle for this guid/ds mapping
3544 zfs_ioc_recv(zfs_cmd_t *zc)
3548 dmu_recv_cookie_t drc;
3549 boolean_t force = (boolean_t)zc->zc_guid;
3552 int props_error = 0;
3555 nvlist_t *props = NULL; /* sent properties */
3556 nvlist_t *origprops = NULL; /* existing properties */
3557 objset_t *origin = NULL;
3559 char tofs[ZFS_MAXNAMELEN];
3560 boolean_t first_recvd_props = B_FALSE;
3562 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3563 strchr(zc->zc_value, '@') == NULL ||
3564 strchr(zc->zc_value, '%'))
3567 (void) strcpy(tofs, zc->zc_value);
3568 tosnap = strchr(tofs, '@');
3571 if (zc->zc_nvlist_src != 0 &&
3572 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3573 zc->zc_iflags, &props)) != 0)
3583 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3585 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3586 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3587 !dsl_prop_get_hasrecvd(os)) {
3588 first_recvd_props = B_TRUE;
3592 * If new received properties are supplied, they are to
3593 * completely replace the existing received properties, so stash
3594 * away the existing ones.
3596 if (dsl_prop_get_received(os, &origprops) == 0) {
3597 nvlist_t *errlist = NULL;
3599 * Don't bother writing a property if its value won't
3600 * change (and avoid the unnecessary security checks).
3602 * The first receive after SPA_VERSION_RECVD_PROPS is a
3603 * special case where we blow away all local properties
3606 if (!first_recvd_props)
3607 props_reduce(props, origprops);
3608 if (zfs_check_clearable(tofs, origprops,
3610 (void) nvlist_merge(errors, errlist, 0);
3611 nvlist_free(errlist);
3614 dmu_objset_rele(os, FTAG);
3617 if (zc->zc_string[0]) {
3618 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3623 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3624 &zc->zc_begin_record, force, origin, &drc);
3626 dmu_objset_rele(origin, FTAG);
3631 * Set properties before we receive the stream so that they are applied
3632 * to the new data. Note that we must call dmu_recv_stream() if
3633 * dmu_recv_begin() succeeds.
3638 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3639 if (drc.drc_newfs) {
3640 if (spa_version(os->os_spa) >=
3641 SPA_VERSION_RECVD_PROPS)
3642 first_recvd_props = B_TRUE;
3643 } else if (origprops != NULL) {
3644 if (clear_received_props(os, tofs, origprops,
3645 first_recvd_props ? NULL : props) != 0)
3646 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3648 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3650 dsl_prop_set_hasrecvd(os);
3651 } else if (!drc.drc_newfs) {
3652 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3655 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3657 (void) nvlist_merge(errors, errlist, 0);
3658 nvlist_free(errlist);
3661 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3663 * Caller made zc->zc_nvlist_dst less than the minimum expected
3664 * size or supplied an invalid address.
3666 props_error = EINVAL;
3670 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3671 &zc->zc_action_handle);
3674 zfsvfs_t *zfsvfs = NULL;
3676 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3680 error = zfs_suspend_fs(zfsvfs);
3682 * If the suspend fails, then the recv_end will
3683 * likely also fail, and clean up after itself.
3685 end_err = dmu_recv_end(&drc);
3687 error = zfs_resume_fs(zfsvfs, tofs);
3688 error = error ? error : end_err;
3689 VFS_RELE(zfsvfs->z_vfs);
3691 error = dmu_recv_end(&drc);
3695 zc->zc_cookie = off - fp->f_offset;
3696 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3700 if (zfs_ioc_recv_inject_err) {
3701 zfs_ioc_recv_inject_err = B_FALSE;
3706 * On error, restore the original props.
3708 if (error && props) {
3709 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3710 if (clear_received_props(os, tofs, props, NULL) != 0) {
3712 * We failed to clear the received properties.
3713 * Since we may have left a $recvd value on the
3714 * system, we can't clear the $hasrecvd flag.
3716 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3717 } else if (first_recvd_props) {
3718 dsl_prop_unset_hasrecvd(os);
3720 dmu_objset_rele(os, FTAG);
3721 } else if (!drc.drc_newfs) {
3722 /* We failed to clear the received properties. */
3723 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3726 if (origprops == NULL && !drc.drc_newfs) {
3727 /* We failed to stash the original properties. */
3728 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3732 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3733 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3734 * explictly if we're restoring local properties cleared in the
3735 * first new-style receive.
3737 if (origprops != NULL &&
3738 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3739 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3740 origprops, NULL) != 0) {
3742 * We stashed the original properties but failed to
3745 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3750 nvlist_free(origprops);
3751 nvlist_free(errors);
3755 error = props_error;
3762 * zc_name name of snapshot to send
3763 * zc_cookie file descriptor to send stream to
3764 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3765 * zc_sendobj objsetid of snapshot to send
3766 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3771 zfs_ioc_send(zfs_cmd_t *zc)
3773 objset_t *fromsnap = NULL;
3779 dsl_dataset_t *dsfrom = NULL;
3783 error = spa_open(zc->zc_name, &spa, FTAG);
3787 dp = spa_get_dsl(spa);
3788 rw_enter(&dp->dp_config_rwlock, RW_READER);
3789 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3790 rw_exit(&dp->dp_config_rwlock);
3792 spa_close(spa, FTAG);
3796 error = dmu_objset_from_ds(ds, &tosnap);
3798 dsl_dataset_rele(ds, FTAG);
3799 spa_close(spa, FTAG);
3803 if (zc->zc_fromobj != 0) {
3804 rw_enter(&dp->dp_config_rwlock, RW_READER);
3805 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3806 rw_exit(&dp->dp_config_rwlock);
3807 spa_close(spa, FTAG);
3809 dsl_dataset_rele(ds, FTAG);
3812 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3814 dsl_dataset_rele(dsfrom, FTAG);
3815 dsl_dataset_rele(ds, FTAG);
3819 spa_close(spa, FTAG);
3822 fp = getf(zc->zc_cookie);
3824 dsl_dataset_rele(ds, FTAG);
3826 dsl_dataset_rele(dsfrom, FTAG);
3831 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3833 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3835 releasef(zc->zc_cookie);
3837 dsl_dataset_rele(dsfrom, FTAG);
3838 dsl_dataset_rele(ds, FTAG);
3843 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3847 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3848 &zc->zc_inject_record);
3851 zc->zc_guid = (uint64_t)id;
3857 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3859 return (zio_clear_fault((int)zc->zc_guid));
3863 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3865 int id = (int)zc->zc_guid;
3868 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3869 &zc->zc_inject_record);
3877 zfs_ioc_error_log(zfs_cmd_t *zc)
3881 size_t count = (size_t)zc->zc_nvlist_dst_size;
3883 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3886 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3889 zc->zc_nvlist_dst_size = count;
3891 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3893 spa_close(spa, FTAG);
3899 zfs_ioc_clear(zfs_cmd_t *zc)
3906 * On zpool clear we also fix up missing slogs
3908 mutex_enter(&spa_namespace_lock);
3909 spa = spa_lookup(zc->zc_name);
3911 mutex_exit(&spa_namespace_lock);
3914 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3915 /* we need to let spa_open/spa_load clear the chains */
3916 spa_set_log_state(spa, SPA_LOG_CLEAR);
3918 spa->spa_last_open_failed = 0;
3919 mutex_exit(&spa_namespace_lock);
3921 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3922 error = spa_open(zc->zc_name, &spa, FTAG);
3925 nvlist_t *config = NULL;
3927 if (zc->zc_nvlist_src == 0)
3930 if ((error = get_nvlist(zc->zc_nvlist_src,
3931 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3932 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3934 if (config != NULL) {
3937 if ((err = put_nvlist(zc, config)) != 0)
3939 nvlist_free(config);
3941 nvlist_free(policy);
3948 spa_vdev_state_enter(spa, SCL_NONE);
3950 if (zc->zc_guid == 0) {
3953 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3955 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
3956 spa_close(spa, FTAG);
3961 vdev_clear(spa, vd);
3963 (void) spa_vdev_state_exit(spa, NULL, 0);
3966 * Resume any suspended I/Os.
3968 if (zio_resume(spa) != 0)
3971 spa_close(spa, FTAG);
3978 * zc_name name of filesystem
3979 * zc_value name of origin snapshot
3982 * zc_string name of conflicting snapshot, if there is one
3985 zfs_ioc_promote(zfs_cmd_t *zc)
3990 * We don't need to unmount *all* the origin fs's snapshots, but
3993 cp = strchr(zc->zc_value, '@');
3996 (void) dmu_objset_find(zc->zc_value,
3997 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3998 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4002 * Retrieve a single {user|group}{used|quota}@... property.
4005 * zc_name name of filesystem
4006 * zc_objset_type zfs_userquota_prop_t
4007 * zc_value domain name (eg. "S-1-234-567-89")
4008 * zc_guid RID/UID/GID
4011 * zc_cookie property value
4014 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4019 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4022 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4026 error = zfs_userspace_one(zfsvfs,
4027 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4028 zfsvfs_rele(zfsvfs, FTAG);
4035 * zc_name name of filesystem
4036 * zc_cookie zap cursor
4037 * zc_objset_type zfs_userquota_prop_t
4038 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4041 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4042 * zc_cookie zap cursor
4045 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4048 int bufsize = zc->zc_nvlist_dst_size;
4053 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4057 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4059 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4060 buf, &zc->zc_nvlist_dst_size);
4063 error = xcopyout(buf,
4064 (void *)(uintptr_t)zc->zc_nvlist_dst,
4065 zc->zc_nvlist_dst_size);
4067 kmem_free(buf, bufsize);
4068 zfsvfs_rele(zfsvfs, FTAG);
4075 * zc_name name of filesystem
4081 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4087 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4088 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4090 * If userused is not enabled, it may be because the
4091 * objset needs to be closed & reopened (to grow the
4092 * objset_phys_t). Suspend/resume the fs will do that.
4094 error = zfs_suspend_fs(zfsvfs);
4096 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4099 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4100 VFS_RELE(zfsvfs->z_vfs);
4102 /* XXX kind of reading contents without owning */
4103 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4107 error = dmu_objset_userspace_upgrade(os);
4108 dmu_objset_rele(os, FTAG);
4115 * We don't want to have a hard dependency
4116 * against some special symbols in sharefs
4117 * nfs, and smbsrv. Determine them if needed when
4118 * the first file system is shared.
4119 * Neither sharefs, nfs or smbsrv are unloadable modules.
4121 int (*znfsexport_fs)(void *arg);
4122 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4123 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4125 int zfs_nfsshare_inited;
4126 int zfs_smbshare_inited;
4128 ddi_modhandle_t nfs_mod;
4129 ddi_modhandle_t sharefs_mod;
4130 ddi_modhandle_t smbsrv_mod;
4131 kmutex_t zfs_share_lock;
4138 ASSERT(MUTEX_HELD(&zfs_share_lock));
4139 /* Both NFS and SMB shares also require sharetab support. */
4140 if (sharefs_mod == NULL && ((sharefs_mod =
4141 ddi_modopen("fs/sharefs",
4142 KRTLD_MODE_FIRST, &error)) == NULL)) {
4145 if (zshare_fs == NULL && ((zshare_fs =
4146 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4147 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4154 zfs_ioc_share(zfs_cmd_t *zc)
4159 switch (zc->zc_share.z_sharetype) {
4161 case ZFS_UNSHARE_NFS:
4162 if (zfs_nfsshare_inited == 0) {
4163 mutex_enter(&zfs_share_lock);
4164 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4165 KRTLD_MODE_FIRST, &error)) == NULL)) {
4166 mutex_exit(&zfs_share_lock);
4169 if (znfsexport_fs == NULL &&
4170 ((znfsexport_fs = (int (*)(void *))
4172 "nfs_export", &error)) == NULL)) {
4173 mutex_exit(&zfs_share_lock);
4176 error = zfs_init_sharefs();
4178 mutex_exit(&zfs_share_lock);
4181 zfs_nfsshare_inited = 1;
4182 mutex_exit(&zfs_share_lock);
4186 case ZFS_UNSHARE_SMB:
4187 if (zfs_smbshare_inited == 0) {
4188 mutex_enter(&zfs_share_lock);
4189 if (smbsrv_mod == NULL && ((smbsrv_mod =
4190 ddi_modopen("drv/smbsrv",
4191 KRTLD_MODE_FIRST, &error)) == NULL)) {
4192 mutex_exit(&zfs_share_lock);
4195 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4196 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4197 "smb_server_share", &error)) == NULL)) {
4198 mutex_exit(&zfs_share_lock);
4201 error = zfs_init_sharefs();
4203 mutex_exit(&zfs_share_lock);
4206 zfs_smbshare_inited = 1;
4207 mutex_exit(&zfs_share_lock);
4214 switch (zc->zc_share.z_sharetype) {
4216 case ZFS_UNSHARE_NFS:
4218 znfsexport_fs((void *)
4219 (uintptr_t)zc->zc_share.z_exportdata))
4223 case ZFS_UNSHARE_SMB:
4224 if (error = zsmbexport_fs((void *)
4225 (uintptr_t)zc->zc_share.z_exportdata,
4226 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4233 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4234 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4235 SHAREFS_ADD : SHAREFS_REMOVE;
4238 * Add or remove share from sharetab
4240 error = zshare_fs(opcode,
4241 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4242 zc->zc_share.z_sharemax);
4248 ace_t full_access[] = {
4249 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4254 * zc_name name of containing filesystem
4255 * zc_obj object # beyond which we want next in-use object #
4258 * zc_obj next in-use object #
4261 zfs_ioc_next_obj(zfs_cmd_t *zc)
4263 objset_t *os = NULL;
4266 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4270 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4271 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4273 dmu_objset_rele(os, FTAG);
4279 * zc_name name of filesystem
4280 * zc_value prefix name for snapshot
4281 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4286 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4291 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4292 (u_longlong_t)ddi_get_lbolt64());
4294 if (strlen(snap_name) >= MAXNAMELEN) {
4299 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4300 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4306 (void) strcpy(zc->zc_value, snap_name);
4313 * zc_name name of "to" snapshot
4314 * zc_value name of "from" snapshot
4315 * zc_cookie file descriptor to write diff data on
4318 * dmu_diff_record_t's to the file descriptor
4321 zfs_ioc_diff(zfs_cmd_t *zc)
4329 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4333 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4335 dmu_objset_rele(tosnap, FTAG);
4339 fp = getf(zc->zc_cookie);
4341 dmu_objset_rele(fromsnap, FTAG);
4342 dmu_objset_rele(tosnap, FTAG);
4348 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4350 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4352 releasef(zc->zc_cookie);
4354 dmu_objset_rele(fromsnap, FTAG);
4355 dmu_objset_rele(tosnap, FTAG);
4360 * Remove all ACL files in shares dir
4363 zfs_smb_acl_purge(znode_t *dzp)
4366 zap_attribute_t zap;
4367 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4370 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4371 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4372 zap_cursor_advance(&zc)) {
4373 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4377 zap_cursor_fini(&zc);
4382 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4386 vnode_t *resourcevp = NULL;
4395 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4396 NO_FOLLOW, NULL, &vp)) != 0)
4399 /* Now make sure mntpnt and dataset are ZFS */
4401 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4402 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4403 zc->zc_name) != 0)) {
4409 zfsvfs = dzp->z_zfsvfs;
4413 * Create share dir if its missing.
4415 mutex_enter(&zfsvfs->z_lock);
4416 if (zfsvfs->z_shares_dir == 0) {
4419 tx = dmu_tx_create(zfsvfs->z_os);
4420 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4422 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4423 error = dmu_tx_assign(tx, TXG_WAIT);
4427 error = zfs_create_share_dir(zfsvfs, tx);
4431 mutex_exit(&zfsvfs->z_lock);
4437 mutex_exit(&zfsvfs->z_lock);
4439 ASSERT(zfsvfs->z_shares_dir);
4440 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4446 switch (zc->zc_cookie) {
4447 case ZFS_SMB_ACL_ADD:
4448 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4449 vattr.va_type = VREG;
4450 vattr.va_mode = S_IFREG|0777;
4454 vsec.vsa_mask = VSA_ACE;
4455 vsec.vsa_aclentp = &full_access;
4456 vsec.vsa_aclentsz = sizeof (full_access);
4457 vsec.vsa_aclcnt = 1;
4459 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4460 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4462 VN_RELE(resourcevp);
4465 case ZFS_SMB_ACL_REMOVE:
4466 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4470 case ZFS_SMB_ACL_RENAME:
4471 if ((error = get_nvlist(zc->zc_nvlist_src,
4472 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4477 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4478 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4481 VN_RELE(ZTOV(sharedir));
4483 nvlist_free(nvlist);
4486 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4488 nvlist_free(nvlist);
4491 case ZFS_SMB_ACL_PURGE:
4492 error = zfs_smb_acl_purge(sharedir);
4501 VN_RELE(ZTOV(sharedir));
4510 * zc_name name of filesystem
4511 * zc_value short name of snap
4512 * zc_string user-supplied tag for this hold
4513 * zc_cookie recursive flag
4514 * zc_temphold set if hold is temporary
4515 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4516 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4517 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4522 zfs_ioc_hold(zfs_cmd_t *zc)
4524 boolean_t recursive = zc->zc_cookie;
4531 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4534 if (zc->zc_sendobj == 0) {
4535 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4536 zc->zc_string, recursive, zc->zc_temphold,
4537 zc->zc_cleanup_fd));
4543 error = spa_open(zc->zc_name, &spa, FTAG);
4547 dp = spa_get_dsl(spa);
4548 rw_enter(&dp->dp_config_rwlock, RW_READER);
4549 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4550 rw_exit(&dp->dp_config_rwlock);
4551 spa_close(spa, FTAG);
4556 * Until we have a hold on this snapshot, it's possible that
4557 * zc_sendobj could've been destroyed and reused as part
4558 * of a later txg. Make sure we're looking at the right object.
4560 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4561 dsl_dataset_rele(ds, FTAG);
4565 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4566 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4568 dsl_dataset_rele(ds, FTAG);
4573 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4577 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4580 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4582 dsl_dataset_rele(ds, FTAG);
4589 * zc_name name of dataset from which we're releasing a user hold
4590 * zc_value short name of snap
4591 * zc_string user-supplied tag for this hold
4592 * zc_cookie recursive flag
4597 zfs_ioc_release(zfs_cmd_t *zc)
4599 boolean_t recursive = zc->zc_cookie;
4601 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4604 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4605 zc->zc_string, recursive));
4610 * zc_name name of filesystem
4613 * zc_nvlist_src{_size} nvlist of snapshot holds
4616 zfs_ioc_get_holds(zfs_cmd_t *zc)
4621 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4622 error = put_nvlist(zc, nvp);
4630 * pool create, destroy, and export don't log the history as part of
4631 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4632 * do the logging of those commands.
4634 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4635 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4637 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4639 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4641 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4643 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4645 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4647 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4649 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4650 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4651 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4652 POOL_CHECK_READONLY },
4653 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4654 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4655 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4657 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4658 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4659 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4660 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4661 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4662 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4663 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4664 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4665 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4666 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4667 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4668 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4669 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4670 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4671 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4672 POOL_CHECK_SUSPENDED },
4673 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4675 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4676 POOL_CHECK_SUSPENDED },
4677 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4678 POOL_CHECK_SUSPENDED },
4679 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4680 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4681 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4682 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4683 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4684 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4685 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4686 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4687 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4688 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4689 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4690 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4691 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4693 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4695 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4697 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4699 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4701 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4703 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4704 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4705 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4706 B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4707 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4708 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4709 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4711 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4712 POOL_CHECK_SUSPENDED },
4713 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4714 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4715 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4717 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4718 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4719 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4721 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4723 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4724 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4725 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4727 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4728 B_FALSE, POOL_CHECK_NONE },
4729 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4730 B_FALSE, POOL_CHECK_NONE },
4731 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4732 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4733 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4734 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4735 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4736 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4737 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4738 POOL_CHECK_SUSPENDED },
4739 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4741 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4742 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4743 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4745 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4747 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4748 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4749 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4750 POOL_CHECK_SUSPENDED }
4754 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4755 zfs_ioc_poolcheck_t check)
4760 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4762 if (check & POOL_CHECK_NONE)
4765 error = spa_open(name, &spa, FTAG);
4767 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4769 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4771 spa_close(spa, FTAG);
4777 * Find a free minor number.
4780 zfsdev_minor_alloc(void)
4782 static minor_t last_minor;
4785 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4787 for (m = last_minor + 1; m != last_minor; m++) {
4788 if (m > ZFSDEV_MAX_MINOR)
4790 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
4800 zfs_ctldev_init(dev_t *devp)
4803 zfs_soft_state_t *zs;
4805 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4806 ASSERT(getminor(*devp) == 0);
4808 minor = zfsdev_minor_alloc();
4812 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
4815 *devp = makedevice(getemajor(*devp), minor);
4817 zs = ddi_get_soft_state(zfsdev_state, minor);
4818 zs->zss_type = ZSST_CTLDEV;
4819 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
4825 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
4827 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4829 zfs_onexit_destroy(zo);
4830 ddi_soft_state_free(zfsdev_state, minor);
4834 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
4836 zfs_soft_state_t *zp;
4838 zp = ddi_get_soft_state(zfsdev_state, minor);
4839 if (zp == NULL || zp->zss_type != which)
4842 return (zp->zss_data);
4846 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
4850 if (getminor(*devp) != 0)
4851 return (zvol_open(devp, flag, otyp, cr));
4853 /* This is the control device. Allocate a new minor if requested. */
4855 mutex_enter(&zfsdev_state_lock);
4856 error = zfs_ctldev_init(devp);
4857 mutex_exit(&zfsdev_state_lock);
4864 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
4867 minor_t minor = getminor(dev);
4872 mutex_enter(&zfsdev_state_lock);
4873 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
4875 mutex_exit(&zfsdev_state_lock);
4876 return (zvol_close(dev, flag, otyp, cr));
4878 zfs_ctldev_destroy(zo, minor);
4879 mutex_exit(&zfsdev_state_lock);
4885 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
4890 minor_t minor = getminor(dev);
4893 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
4894 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
4896 vec = cmd - ZFS_IOC;
4897 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
4899 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4902 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
4904 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4908 if ((error == 0) && !(flag & FKIOCTL))
4909 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
4912 * Ensure that all pool/dataset names are valid before we pass down to
4916 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4917 zc->zc_iflags = flag & FKIOCTL;
4918 switch (zfs_ioc_vec[vec].zvec_namecheck) {
4920 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4922 error = pool_status_check(zc->zc_name,
4923 zfs_ioc_vec[vec].zvec_namecheck,
4924 zfs_ioc_vec[vec].zvec_pool_check);
4928 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4930 error = pool_status_check(zc->zc_name,
4931 zfs_ioc_vec[vec].zvec_namecheck,
4932 zfs_ioc_vec[vec].zvec_pool_check);
4941 error = zfs_ioc_vec[vec].zvec_func(zc);
4943 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4947 if (zfs_ioc_vec[vec].zvec_his_log)
4948 zfs_log_history(zc);
4951 kmem_free(zc, sizeof (zfs_cmd_t));
4956 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4958 if (cmd != DDI_ATTACH)
4959 return (DDI_FAILURE);
4961 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
4962 DDI_PSEUDO, 0) == DDI_FAILURE)
4963 return (DDI_FAILURE);
4967 ddi_report_dev(dip);
4969 return (DDI_SUCCESS);
4973 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4975 if (spa_busy() || zfs_busy() || zvol_busy())
4976 return (DDI_FAILURE);
4978 if (cmd != DDI_DETACH)
4979 return (DDI_FAILURE);
4983 ddi_prop_remove_all(dip);
4984 ddi_remove_minor_node(dip, NULL);
4986 return (DDI_SUCCESS);
4991 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4994 case DDI_INFO_DEVT2DEVINFO:
4996 return (DDI_SUCCESS);
4998 case DDI_INFO_DEVT2INSTANCE:
4999 *result = (void *)0;
5000 return (DDI_SUCCESS);
5003 return (DDI_FAILURE);
5007 * OK, so this is a little weird.
5009 * /dev/zfs is the control node, i.e. minor 0.
5010 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5012 * /dev/zfs has basically nothing to do except serve up ioctls,
5013 * so most of the standard driver entry points are in zvol.c.
5015 static struct cb_ops zfs_cb_ops = {
5016 zfsdev_open, /* open */
5017 zfsdev_close, /* close */
5018 zvol_strategy, /* strategy */
5020 zvol_dump, /* dump */
5021 zvol_read, /* read */
5022 zvol_write, /* write */
5023 zfsdev_ioctl, /* ioctl */
5027 nochpoll, /* poll */
5028 ddi_prop_op, /* prop_op */
5029 NULL, /* streamtab */
5030 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
5031 CB_REV, /* version */
5032 nodev, /* async read */
5033 nodev, /* async write */
5036 static struct dev_ops zfs_dev_ops = {
5037 DEVO_REV, /* version */
5039 zfs_info, /* info */
5040 nulldev, /* identify */
5041 nulldev, /* probe */
5042 zfs_attach, /* attach */
5043 zfs_detach, /* detach */
5045 &zfs_cb_ops, /* driver operations */
5046 NULL, /* no bus operations */
5048 ddi_quiesce_not_needed, /* quiesce */
5051 static struct modldrv zfs_modldrv = {
5057 static struct modlinkage modlinkage = {
5059 (void *)&zfs_modlfs,
5060 (void *)&zfs_modldrv,
5065 uint_t zfs_fsyncer_key;
5066 extern uint_t rrw_tsd_key;
5073 spa_init(FREAD | FWRITE);
5077 if ((error = mod_install(&modlinkage)) != 0) {
5084 tsd_create(&zfs_fsyncer_key, NULL);
5085 tsd_create(&rrw_tsd_key, NULL);
5087 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5089 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5099 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5102 if ((error = mod_remove(&modlinkage)) != 0)
5108 if (zfs_nfsshare_inited)
5109 (void) ddi_modclose(nfs_mod);
5110 if (zfs_smbshare_inited)
5111 (void) ddi_modclose(smbsrv_mod);
5112 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5113 (void) ddi_modclose(sharefs_mod);
5115 tsd_destroy(&zfs_fsyncer_key);
5116 ldi_ident_release(zfs_li);
5118 mutex_destroy(&zfs_share_lock);
5124 _info(struct modinfo *modinfop)
5126 return (mod_info(&modlinkage, modinfop));