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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
25 * All rights reserved.
26 * Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org>
27 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/errno.h>
47 #include <sys/cmn_err.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/zfs_vfsops.h>
51 #include <sys/zfs_znode.h>
54 #include <sys/spa_impl.h>
57 #include <sys/dsl_dir.h>
58 #include <sys/dsl_dataset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_deleg.h>
61 #include <sys/dmu_objset.h>
62 #include <sys/dmu_impl.h>
63 #include <sys/sunddi.h>
64 #include <sys/policy.h>
66 #include <sys/nvpair.h>
67 #include <sys/mount.h>
68 #include <sys/taskqueue.h>
70 #include <sys/varargs.h>
71 #include <sys/fs/zfs.h>
72 #include <sys/zfs_ctldir.h>
73 #include <sys/zfs_dir.h>
74 #include <sys/zfs_onexit.h>
76 #include <sys/dsl_scan.h>
77 #include <sys/dmu_objset.h>
79 #include "zfs_namecheck.h"
81 #include "zfs_deleg.h"
82 #include "zfs_comutil.h"
83 #include "zfs_ioctl_compat.h"
85 CTASSERT(sizeof(zfs_cmd_t) < IOCPARM_MAX);
87 static int snapshot_list_prefetch;
88 SYSCTL_DECL(_vfs_zfs);
89 TUNABLE_INT("vfs.zfs.snapshot_list_prefetch", &snapshot_list_prefetch);
90 SYSCTL_INT(_vfs_zfs, OID_AUTO, snapshot_list_prefetch, CTLFLAG_RW,
91 &snapshot_list_prefetch, 0, "Prefetch data when listing snapshots");
93 static struct cdev *zfsdev;
95 extern void zfs_init(void);
96 extern void zfs_fini(void);
98 typedef int zfs_ioc_func_t(zfs_cmd_t *);
99 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
105 } zfs_ioc_namecheck_t;
107 typedef struct zfs_ioc_vec {
108 zfs_ioc_func_t *zvec_func;
109 zfs_secpolicy_func_t *zvec_secpolicy;
110 zfs_ioc_namecheck_t zvec_namecheck;
111 boolean_t zvec_his_log;
112 boolean_t zvec_pool_check;
115 /* This array is indexed by zfs_userquota_prop_t */
116 static const char *userquota_perms[] = {
117 ZFS_DELEG_PERM_USERUSED,
118 ZFS_DELEG_PERM_USERQUOTA,
119 ZFS_DELEG_PERM_GROUPUSED,
120 ZFS_DELEG_PERM_GROUPQUOTA,
123 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
124 static int zfs_check_settable(const char *name, nvpair_t *property,
126 static int zfs_check_clearable(char *dataset, nvlist_t *props,
128 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
130 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
132 static void zfsdev_close(void *data);
134 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
136 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
143 * Get rid of annoying "../common/" prefix to filename.
145 newfile = strrchr(file, '/');
146 if (newfile != NULL) {
147 newfile = newfile + 1; /* Get rid of leading / */
153 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
157 * To get this data, use the zfs-dprintf probe as so:
158 * dtrace -q -n 'zfs-dprintf \
159 * /stringof(arg0) == "dbuf.c"/ \
160 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
162 * arg1 = function name
166 DTRACE_PROBE4(zfs__dprintf,
167 char *, newfile, char *, func, int, line, char *, buf);
171 history_str_free(char *buf)
173 kmem_free(buf, HIS_MAX_RECORD_LEN);
177 history_str_get(zfs_cmd_t *zc)
181 if (zc->zc_history == 0)
184 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
185 if (copyinstr((void *)(uintptr_t)zc->zc_history,
186 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
187 history_str_free(buf);
191 buf[HIS_MAX_RECORD_LEN -1] = '\0';
197 * Check to see if the named dataset is currently defined as bootable
200 zfs_is_bootfs(const char *name)
204 if (dmu_objset_hold(name, FTAG, &os) == 0) {
206 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
207 dmu_objset_rele(os, FTAG);
214 * zfs_earlier_version
216 * Return non-zero if the spa version is less than requested version.
219 zfs_earlier_version(const char *name, int version)
223 if (spa_open(name, &spa, FTAG) == 0) {
224 if (spa_version(spa) < version) {
225 spa_close(spa, FTAG);
228 spa_close(spa, FTAG);
234 * zpl_earlier_version
236 * Return TRUE if the ZPL version is less than requested version.
239 zpl_earlier_version(const char *name, int version)
242 boolean_t rc = B_TRUE;
244 if (dmu_objset_hold(name, FTAG, &os) == 0) {
247 if (dmu_objset_type(os) != DMU_OST_ZFS) {
248 dmu_objset_rele(os, FTAG);
251 /* XXX reading from non-owned objset */
252 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
253 rc = zplversion < version;
254 dmu_objset_rele(os, FTAG);
260 zfs_log_history(zfs_cmd_t *zc)
265 if ((buf = history_str_get(zc)) == NULL)
268 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
269 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
270 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
271 spa_close(spa, FTAG);
273 history_str_free(buf);
277 * Policy for top-level read operations (list pools). Requires no privileges,
278 * and can be used in the local zone, as there is no associated dataset.
282 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
288 * Policy for dataset read operations (list children, get statistics). Requires
289 * no privileges, but must be visible in the local zone.
293 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
295 if (INGLOBALZONE(curthread) ||
296 zone_dataset_visible(zc->zc_name, NULL))
303 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
308 * The dataset must be visible by this zone -- check this first
309 * so they don't see EPERM on something they shouldn't know about.
311 if (!INGLOBALZONE(curthread) &&
312 !zone_dataset_visible(dataset, &writable))
315 if (INGLOBALZONE(curthread)) {
317 * If the fs is zoned, only root can access it from the
320 if (secpolicy_zfs(cr) && zoned)
324 * If we are in a local zone, the 'zoned' property must be set.
329 /* must be writable by this zone */
337 zfs_dozonecheck(const char *dataset, cred_t *cr)
341 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
344 return (zfs_dozonecheck_impl(dataset, zoned, cr));
348 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
352 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
353 if (dsl_prop_get_ds(ds, "jailed", 8, 1, &zoned, NULL)) {
354 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
357 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
359 return (zfs_dozonecheck_impl(dataset, zoned, cr));
363 * If name ends in a '@', then require recursive permissions.
366 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
369 boolean_t descendent = B_FALSE;
373 at = strchr(name, '@');
374 if (at != NULL && at[1] == '\0') {
379 error = dsl_dataset_hold(name, FTAG, &ds);
385 error = zfs_dozonecheck_ds(name, ds, cr);
387 error = secpolicy_zfs(cr);
389 error = dsl_deleg_access_impl(ds, descendent, perm, cr);
392 dsl_dataset_rele(ds, FTAG);
397 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
398 const char *perm, cred_t *cr)
402 error = zfs_dozonecheck_ds(name, ds, cr);
404 error = secpolicy_zfs(cr);
406 error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
413 * Policy for setting the security label property.
415 * Returns 0 for success, non-zero for access and other errors.
418 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
420 char ds_hexsl[MAXNAMELEN];
421 bslabel_t ds_sl, new_sl;
422 boolean_t new_default = FALSE;
424 int needed_priv = -1;
427 /* First get the existing dataset label. */
428 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
429 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
433 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
436 /* The label must be translatable */
437 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
441 * In a non-global zone, disallow attempts to set a label that
442 * doesn't match that of the zone; otherwise no other checks
445 if (!INGLOBALZONE(curproc)) {
446 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
452 * For global-zone datasets (i.e., those whose zoned property is
453 * "off", verify that the specified new label is valid for the
456 if (dsl_prop_get_integer(name,
457 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
460 if (zfs_check_global_label(name, strval) != 0)
465 * If the existing dataset label is nondefault, check if the
466 * dataset is mounted (label cannot be changed while mounted).
467 * Get the zfsvfs; if there isn't one, then the dataset isn't
468 * mounted (or isn't a dataset, doesn't exist, ...).
470 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
472 static char *setsl_tag = "setsl_tag";
475 * Try to own the dataset; abort if there is any error,
476 * (e.g., already mounted, in use, or other error).
478 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
483 dmu_objset_disown(os, setsl_tag);
486 needed_priv = PRIV_FILE_DOWNGRADE_SL;
490 if (hexstr_to_label(strval, &new_sl) != 0)
493 if (blstrictdom(&ds_sl, &new_sl))
494 needed_priv = PRIV_FILE_DOWNGRADE_SL;
495 else if (blstrictdom(&new_sl, &ds_sl))
496 needed_priv = PRIV_FILE_UPGRADE_SL;
498 /* dataset currently has a default label */
500 needed_priv = PRIV_FILE_UPGRADE_SL;
504 if (needed_priv != -1)
505 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
508 #endif /* SECLABEL */
511 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
517 * Check permissions for special properties.
522 * Disallow setting of 'zoned' from within a local zone.
524 if (!INGLOBALZONE(curthread))
529 if (!INGLOBALZONE(curthread)) {
531 char setpoint[MAXNAMELEN];
533 * Unprivileged users are allowed to modify the
534 * quota on things *under* (ie. contained by)
535 * the thing they own.
537 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
540 if (!zoned || strlen(dsname) <= strlen(setpoint))
545 case ZFS_PROP_MLSLABEL:
547 if (!is_system_labeled())
550 if (nvpair_value_string(propval, &strval) == 0) {
553 err = zfs_set_slabel_policy(dsname, strval, CRED());
563 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
567 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
571 error = zfs_dozonecheck(zc->zc_name, cr);
576 * permission to set permissions will be evaluated later in
577 * dsl_deleg_can_allow()
583 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
585 return (zfs_secpolicy_write_perms(zc->zc_name,
586 ZFS_DELEG_PERM_ROLLBACK, cr));
590 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
599 * Generate the current snapshot name from the given objsetid, then
600 * use that name for the secpolicy/zone checks.
602 cp = strchr(zc->zc_name, '@');
605 error = spa_open(zc->zc_name, &spa, FTAG);
609 dp = spa_get_dsl(spa);
610 rw_enter(&dp->dp_config_rwlock, RW_READER);
611 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
612 rw_exit(&dp->dp_config_rwlock);
613 spa_close(spa, FTAG);
617 dsl_dataset_name(ds, zc->zc_name);
619 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
620 ZFS_DELEG_PERM_SEND, cr);
621 dsl_dataset_rele(ds, FTAG);
627 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
632 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
633 NO_FOLLOW, NULL, &vp)) != 0)
636 /* Now make sure mntpnt and dataset are ZFS */
638 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
639 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
640 zc->zc_name) != 0)) {
646 return (dsl_deleg_access(zc->zc_name,
647 ZFS_DELEG_PERM_SHARE, cr));
651 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
653 if (!INGLOBALZONE(curthread))
656 if (secpolicy_nfs(cr) == 0) {
659 return (zfs_secpolicy_deleg_share(zc, cr));
664 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
666 if (!INGLOBALZONE(curthread))
669 if (secpolicy_smb(cr) == 0) {
672 return (zfs_secpolicy_deleg_share(zc, cr));
677 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
682 * Remove the @bla or /bla from the end of the name to get the parent.
684 (void) strncpy(parent, datasetname, parentsize);
685 cp = strrchr(parent, '@');
689 cp = strrchr(parent, '/');
699 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
703 if ((error = zfs_secpolicy_write_perms(name,
704 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
707 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
711 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
713 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
717 * Destroying snapshots with delegated permissions requires
718 * descendent mount and destroy permissions.
721 zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr)
726 dsname = kmem_asprintf("%s@", zc->zc_name);
728 error = zfs_secpolicy_destroy_perms(dsname, cr);
731 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
738 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
740 char parentname[MAXNAMELEN];
743 if ((error = zfs_secpolicy_write_perms(from,
744 ZFS_DELEG_PERM_RENAME, cr)) != 0)
747 if ((error = zfs_secpolicy_write_perms(from,
748 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
751 if ((error = zfs_get_parent(to, parentname,
752 sizeof (parentname))) != 0)
755 if ((error = zfs_secpolicy_write_perms(parentname,
756 ZFS_DELEG_PERM_CREATE, cr)) != 0)
759 if ((error = zfs_secpolicy_write_perms(parentname,
760 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
767 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
769 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
773 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
775 char parentname[MAXNAMELEN];
779 error = zfs_secpolicy_write_perms(zc->zc_name,
780 ZFS_DELEG_PERM_PROMOTE, cr);
784 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
787 dsl_dataset_t *pclone = NULL;
789 dd = clone->os_dsl_dataset->ds_dir;
791 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
792 error = dsl_dataset_hold_obj(dd->dd_pool,
793 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
794 rw_exit(&dd->dd_pool->dp_config_rwlock);
796 dmu_objset_rele(clone, FTAG);
800 error = zfs_secpolicy_write_perms(zc->zc_name,
801 ZFS_DELEG_PERM_MOUNT, cr);
803 dsl_dataset_name(pclone, parentname);
804 dmu_objset_rele(clone, FTAG);
805 dsl_dataset_rele(pclone, FTAG);
807 error = zfs_secpolicy_write_perms(parentname,
808 ZFS_DELEG_PERM_PROMOTE, cr);
814 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
818 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
819 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
822 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
823 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
826 return (zfs_secpolicy_write_perms(zc->zc_name,
827 ZFS_DELEG_PERM_CREATE, cr));
831 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
833 return (zfs_secpolicy_write_perms(name,
834 ZFS_DELEG_PERM_SNAPSHOT, cr));
838 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
841 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
845 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
847 char parentname[MAXNAMELEN];
850 if ((error = zfs_get_parent(zc->zc_name, parentname,
851 sizeof (parentname))) != 0)
854 if (zc->zc_value[0] != '\0') {
855 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
856 ZFS_DELEG_PERM_CLONE, cr)) != 0)
860 if ((error = zfs_secpolicy_write_perms(parentname,
861 ZFS_DELEG_PERM_CREATE, cr)) != 0)
864 error = zfs_secpolicy_write_perms(parentname,
865 ZFS_DELEG_PERM_MOUNT, cr);
871 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
875 error = secpolicy_fs_unmount(cr, NULL);
877 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
883 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
884 * SYS_CONFIG privilege, which is not available in a local zone.
888 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
890 if (secpolicy_sys_config(cr, B_FALSE) != 0)
897 * Policy for object to name lookups.
901 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
905 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
908 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
913 * Policy for fault injection. Requires all privileges.
917 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
919 return (secpolicy_zinject(cr));
923 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
925 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
927 if (prop == ZPROP_INVAL) {
928 if (!zfs_prop_user(zc->zc_value))
930 return (zfs_secpolicy_write_perms(zc->zc_name,
931 ZFS_DELEG_PERM_USERPROP, cr));
933 return (zfs_secpolicy_setprop(zc->zc_name, prop,
939 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
941 int err = zfs_secpolicy_read(zc, cr);
945 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
948 if (zc->zc_value[0] == 0) {
950 * They are asking about a posix uid/gid. If it's
951 * themself, allow it.
953 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
954 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
955 if (zc->zc_guid == crgetuid(cr))
958 if (groupmember(zc->zc_guid, cr))
963 return (zfs_secpolicy_write_perms(zc->zc_name,
964 userquota_perms[zc->zc_objset_type], cr));
968 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
970 int err = zfs_secpolicy_read(zc, cr);
974 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
977 return (zfs_secpolicy_write_perms(zc->zc_name,
978 userquota_perms[zc->zc_objset_type], cr));
982 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
984 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
989 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
991 return (zfs_secpolicy_write_perms(zc->zc_name,
992 ZFS_DELEG_PERM_HOLD, cr));
996 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
998 return (zfs_secpolicy_write_perms(zc->zc_name,
999 ZFS_DELEG_PERM_RELEASE, cr));
1003 * Policy for allowing temporary snapshots to be taken or released
1006 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
1009 * A temporary snapshot is the same as a snapshot,
1010 * hold, destroy and release all rolled into one.
1011 * Delegated diff alone is sufficient that we allow this.
1015 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1016 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1019 error = zfs_secpolicy_snapshot(zc, cr);
1021 error = zfs_secpolicy_hold(zc, cr);
1023 error = zfs_secpolicy_release(zc, cr);
1025 error = zfs_secpolicy_destroy(zc, cr);
1030 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1033 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1037 nvlist_t *list = NULL;
1040 * Read in and unpack the user-supplied nvlist.
1045 packed = kmem_alloc(size, KM_SLEEP);
1047 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1049 kmem_free(packed, size);
1053 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1054 kmem_free(packed, size);
1058 kmem_free(packed, size);
1065 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1069 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1071 if (size > zc->zc_nvlist_dst_size) {
1072 nvpair_t *more_errors;
1075 if (zc->zc_nvlist_dst_size < 1024)
1078 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1079 more_errors = nvlist_prev_nvpair(*errors, NULL);
1082 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1084 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1086 VERIFY(nvlist_size(*errors, &size,
1087 NV_ENCODE_NATIVE) == 0);
1088 } while (size > zc->zc_nvlist_dst_size);
1090 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1091 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1092 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1093 ASSERT(size <= zc->zc_nvlist_dst_size);
1100 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1102 char *packed = NULL;
1106 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1108 if (size > zc->zc_nvlist_dst_size) {
1110 * Solaris returns ENOMEM here, because even if an error is
1111 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1112 * passed to the userland. This is not the case for FreeBSD.
1113 * We need to return 0, so the kernel will copy the
1114 * zc_nvlist_dst_size back and the userland can discover that a
1115 * bigger buffer is needed.
1119 packed = kmem_alloc(size, KM_SLEEP);
1120 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1122 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1123 size, zc->zc_iflags) != 0)
1125 kmem_free(packed, size);
1128 zc->zc_nvlist_dst_size = size;
1133 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1138 error = dmu_objset_hold(dsname, FTAG, &os);
1141 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1142 dmu_objset_rele(os, FTAG);
1146 mutex_enter(&os->os_user_ptr_lock);
1147 *zfvp = dmu_objset_get_user(os);
1149 VFS_HOLD((*zfvp)->z_vfs);
1153 mutex_exit(&os->os_user_ptr_lock);
1154 dmu_objset_rele(os, FTAG);
1159 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1160 * case its z_vfs will be NULL, and it will be opened as the owner.
1161 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1162 * which prevents all vnode ops from running.
1165 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1169 if (getzfsvfs(name, zfvp) != 0)
1170 error = zfsvfs_create(name, zfvp);
1172 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1174 if ((*zfvp)->z_unmounted) {
1176 * XXX we could probably try again, since the unmounting
1177 * thread should be just about to disassociate the
1178 * objset from the zfsvfs.
1180 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1188 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1190 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1192 if (zfsvfs->z_vfs) {
1193 VFS_RELE(zfsvfs->z_vfs);
1195 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1196 zfsvfs_free(zfsvfs);
1201 zfs_ioc_pool_create(zfs_cmd_t *zc)
1204 nvlist_t *config, *props = NULL;
1205 nvlist_t *rootprops = NULL;
1206 nvlist_t *zplprops = NULL;
1209 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1210 zc->zc_iflags, &config))
1213 if (zc->zc_nvlist_src_size != 0 && (error =
1214 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1215 zc->zc_iflags, &props))) {
1216 nvlist_free(config);
1221 nvlist_t *nvl = NULL;
1222 uint64_t version = SPA_VERSION;
1224 (void) nvlist_lookup_uint64(props,
1225 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1226 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1228 goto pool_props_bad;
1230 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1232 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1234 nvlist_free(config);
1238 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1240 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1241 error = zfs_fill_zplprops_root(version, rootprops,
1244 goto pool_props_bad;
1247 buf = history_str_get(zc);
1249 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1252 * Set the remaining root properties
1254 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1255 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1256 (void) spa_destroy(zc->zc_name);
1259 history_str_free(buf);
1262 nvlist_free(rootprops);
1263 nvlist_free(zplprops);
1264 nvlist_free(config);
1271 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1274 zfs_log_history(zc);
1275 error = spa_destroy(zc->zc_name);
1277 zvol_remove_minors(zc->zc_name);
1282 zfs_ioc_pool_import(zfs_cmd_t *zc)
1284 nvlist_t *config, *props = NULL;
1288 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1289 zc->zc_iflags, &config)) != 0)
1292 if (zc->zc_nvlist_src_size != 0 && (error =
1293 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1294 zc->zc_iflags, &props))) {
1295 nvlist_free(config);
1299 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1300 guid != zc->zc_guid)
1303 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1305 if (zc->zc_nvlist_dst != 0) {
1308 if ((err = put_nvlist(zc, config)) != 0)
1312 nvlist_free(config);
1321 zfs_ioc_pool_export(zfs_cmd_t *zc)
1324 boolean_t force = (boolean_t)zc->zc_cookie;
1325 boolean_t hardforce = (boolean_t)zc->zc_guid;
1327 zfs_log_history(zc);
1328 error = spa_export(zc->zc_name, NULL, force, hardforce);
1330 zvol_remove_minors(zc->zc_name);
1335 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1340 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1343 error = put_nvlist(zc, configs);
1345 nvlist_free(configs);
1352 * zc_name name of the pool
1355 * zc_cookie real errno
1356 * zc_nvlist_dst config nvlist
1357 * zc_nvlist_dst_size size of config nvlist
1360 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1366 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1367 sizeof (zc->zc_value));
1369 if (config != NULL) {
1370 ret = put_nvlist(zc, config);
1371 nvlist_free(config);
1374 * The config may be present even if 'error' is non-zero.
1375 * In this case we return success, and preserve the real errno
1378 zc->zc_cookie = error;
1387 * Try to import the given pool, returning pool stats as appropriate so that
1388 * user land knows which devices are available and overall pool health.
1391 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1393 nvlist_t *tryconfig, *config;
1396 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1397 zc->zc_iflags, &tryconfig)) != 0)
1400 config = spa_tryimport(tryconfig);
1402 nvlist_free(tryconfig);
1407 error = put_nvlist(zc, config);
1408 nvlist_free(config);
1415 * zc_name name of the pool
1416 * zc_cookie scan func (pool_scan_func_t)
1419 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1424 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1427 if (zc->zc_cookie == POOL_SCAN_NONE)
1428 error = spa_scan_stop(spa);
1430 error = spa_scan(spa, zc->zc_cookie);
1432 spa_close(spa, FTAG);
1438 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1443 error = spa_open(zc->zc_name, &spa, FTAG);
1446 spa_close(spa, FTAG);
1452 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1457 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1460 if (zc->zc_cookie < spa_version(spa) ||
1461 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1462 spa_close(spa, FTAG);
1466 spa_upgrade(spa, zc->zc_cookie);
1467 spa_close(spa, FTAG);
1473 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1480 if ((size = zc->zc_history_len) == 0)
1483 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1486 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1487 spa_close(spa, FTAG);
1491 hist_buf = kmem_alloc(size, KM_SLEEP);
1492 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1493 &zc->zc_history_len, hist_buf)) == 0) {
1494 error = ddi_copyout(hist_buf,
1495 (void *)(uintptr_t)zc->zc_history,
1496 zc->zc_history_len, zc->zc_iflags);
1499 spa_close(spa, FTAG);
1500 kmem_free(hist_buf, size);
1505 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1510 error = spa_open(zc->zc_name, &spa, FTAG);
1512 error = spa_change_guid(spa);
1513 spa_close(spa, FTAG);
1519 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1523 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1531 * zc_name name of filesystem
1532 * zc_obj object to find
1535 * zc_value name of object
1538 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1543 /* XXX reading from objset not owned */
1544 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1546 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1547 dmu_objset_rele(os, FTAG);
1550 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1551 sizeof (zc->zc_value));
1552 dmu_objset_rele(os, FTAG);
1559 * zc_name name of filesystem
1560 * zc_obj object to find
1563 * zc_stat stats on object
1564 * zc_value path to object
1567 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1572 /* XXX reading from objset not owned */
1573 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1575 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1576 dmu_objset_rele(os, FTAG);
1579 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1580 sizeof (zc->zc_value));
1581 dmu_objset_rele(os, FTAG);
1587 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1591 nvlist_t *config, **l2cache, **spares;
1592 uint_t nl2cache = 0, nspares = 0;
1594 error = spa_open(zc->zc_name, &spa, FTAG);
1598 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1599 zc->zc_iflags, &config);
1600 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1601 &l2cache, &nl2cache);
1603 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1607 * A root pool with concatenated devices is not supported.
1608 * Thus, can not add a device to a root pool.
1610 * Intent log device can not be added to a rootpool because
1611 * during mountroot, zil is replayed, a seperated log device
1612 * can not be accessed during the mountroot time.
1614 * l2cache and spare devices are ok to be added to a rootpool.
1616 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1617 nvlist_free(config);
1618 spa_close(spa, FTAG);
1623 error = spa_vdev_add(spa, config);
1624 nvlist_free(config);
1626 spa_close(spa, FTAG);
1632 * zc_name name of the pool
1633 * zc_nvlist_conf nvlist of devices to remove
1634 * zc_cookie to stop the remove?
1637 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1642 error = spa_open(zc->zc_name, &spa, FTAG);
1645 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1646 spa_close(spa, FTAG);
1651 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1655 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1657 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1659 switch (zc->zc_cookie) {
1660 case VDEV_STATE_ONLINE:
1661 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1664 case VDEV_STATE_OFFLINE:
1665 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1668 case VDEV_STATE_FAULTED:
1669 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1670 zc->zc_obj != VDEV_AUX_EXTERNAL)
1671 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1673 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1676 case VDEV_STATE_DEGRADED:
1677 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1678 zc->zc_obj != VDEV_AUX_EXTERNAL)
1679 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1681 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1687 zc->zc_cookie = newstate;
1688 spa_close(spa, FTAG);
1693 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1696 int replacing = zc->zc_cookie;
1700 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1703 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1704 zc->zc_iflags, &config)) == 0) {
1705 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1706 nvlist_free(config);
1709 spa_close(spa, FTAG);
1714 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1719 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1722 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1724 spa_close(spa, FTAG);
1729 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1732 nvlist_t *config, *props = NULL;
1734 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1736 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1739 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1740 zc->zc_iflags, &config)) {
1741 spa_close(spa, FTAG);
1745 if (zc->zc_nvlist_src_size != 0 && (error =
1746 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1747 zc->zc_iflags, &props))) {
1748 spa_close(spa, FTAG);
1749 nvlist_free(config);
1753 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1755 spa_close(spa, FTAG);
1757 nvlist_free(config);
1764 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1767 char *path = zc->zc_value;
1768 uint64_t guid = zc->zc_guid;
1771 error = spa_open(zc->zc_name, &spa, FTAG);
1775 error = spa_vdev_setpath(spa, guid, path);
1776 spa_close(spa, FTAG);
1781 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1784 char *fru = zc->zc_value;
1785 uint64_t guid = zc->zc_guid;
1788 error = spa_open(zc->zc_name, &spa, FTAG);
1792 error = spa_vdev_setfru(spa, guid, fru);
1793 spa_close(spa, FTAG);
1798 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1803 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1805 if (zc->zc_nvlist_dst != 0 &&
1806 (error = dsl_prop_get_all(os, &nv)) == 0) {
1807 dmu_objset_stats(os, nv);
1809 * NB: zvol_get_stats() will read the objset contents,
1810 * which we aren't supposed to do with a
1811 * DS_MODE_USER hold, because it could be
1812 * inconsistent. So this is a bit of a workaround...
1813 * XXX reading with out owning
1815 if (!zc->zc_objset_stats.dds_inconsistent &&
1816 dmu_objset_type(os) == DMU_OST_ZVOL) {
1817 error = zvol_get_stats(os, nv);
1822 error = put_nvlist(zc, nv);
1831 * zc_name name of filesystem
1832 * zc_nvlist_dst_size size of buffer for property nvlist
1835 * zc_objset_stats stats
1836 * zc_nvlist_dst property nvlist
1837 * zc_nvlist_dst_size size of property nvlist
1840 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1842 objset_t *os = NULL;
1845 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1848 error = zfs_ioc_objset_stats_impl(zc, os);
1850 dmu_objset_rele(os, FTAG);
1852 if (error == ENOMEM)
1859 * zc_name name of filesystem
1860 * zc_nvlist_dst_size size of buffer for property nvlist
1863 * zc_nvlist_dst received property nvlist
1864 * zc_nvlist_dst_size size of received property nvlist
1866 * Gets received properties (distinct from local properties on or after
1867 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1868 * local property values.
1871 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1873 objset_t *os = NULL;
1877 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os))
1881 * Without this check, we would return local property values if the
1882 * caller has not already received properties on or after
1883 * SPA_VERSION_RECVD_PROPS.
1885 if (!dsl_prop_get_hasrecvd(os)) {
1886 dmu_objset_rele(os, FTAG);
1890 if (zc->zc_nvlist_dst != 0 &&
1891 (error = dsl_prop_get_received(os, &nv)) == 0) {
1892 error = put_nvlist(zc, nv);
1896 dmu_objset_rele(os, FTAG);
1901 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1907 * zfs_get_zplprop() will either find a value or give us
1908 * the default value (if there is one).
1910 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1912 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1918 * zc_name name of filesystem
1919 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1922 * zc_nvlist_dst zpl property nvlist
1923 * zc_nvlist_dst_size size of zpl property nvlist
1926 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1931 /* XXX reading without owning */
1932 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
1935 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1938 * NB: nvl_add_zplprop() will read the objset contents,
1939 * which we aren't supposed to do with a DS_MODE_USER
1940 * hold, because it could be inconsistent.
1942 if (zc->zc_nvlist_dst != 0 &&
1943 !zc->zc_objset_stats.dds_inconsistent &&
1944 dmu_objset_type(os) == DMU_OST_ZFS) {
1947 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1948 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1949 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1950 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1951 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1952 err = put_nvlist(zc, nv);
1957 dmu_objset_rele(os, FTAG);
1962 dataset_name_hidden(const char *name)
1965 * Skip over datasets that are not visible in this zone,
1966 * internal datasets (which have a $ in their name), and
1967 * temporary datasets (which have a % in their name).
1969 if (strchr(name, '$') != NULL)
1971 if (strchr(name, '%') != NULL)
1973 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
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 filesystem
1986 * zc_cookie zap cursor
1987 * zc_objset_stats stats
1988 * zc_nvlist_dst property nvlist
1989 * zc_nvlist_dst_size size of property nvlist
1992 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1997 size_t orig_len = strlen(zc->zc_name);
2000 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2001 if (error == ENOENT)
2006 p = strrchr(zc->zc_name, '/');
2007 if (p == NULL || p[1] != '\0')
2008 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2009 p = zc->zc_name + strlen(zc->zc_name);
2012 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
2013 * but is not declared void because its called by dmu_objset_find().
2015 if (zc->zc_cookie == 0) {
2016 uint64_t cookie = 0;
2017 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
2019 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2020 if (!dataset_name_hidden(zc->zc_name))
2021 (void) dmu_objset_prefetch(zc->zc_name, NULL);
2026 error = dmu_dir_list_next(os,
2027 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2028 NULL, &zc->zc_cookie);
2029 if (error == ENOENT)
2031 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2032 dmu_objset_rele(os, FTAG);
2035 * If it's an internal dataset (ie. with a '$' in its name),
2036 * don't try to get stats for it, otherwise we'll return ENOENT.
2038 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2039 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2040 if (error == ENOENT) {
2041 /* We lost a race with destroy, get the next one. */
2042 zc->zc_name[orig_len] = '\0';
2051 * zc_name name of filesystem
2052 * zc_cookie zap cursor
2053 * zc_nvlist_dst_size size of buffer for property nvlist
2054 * zc_simple when set, only name is requested
2057 * zc_name name of next snapshot
2058 * zc_objset_stats stats
2059 * zc_nvlist_dst property nvlist
2060 * zc_nvlist_dst_size size of property nvlist
2063 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2069 if (snapshot_list_prefetch && zc->zc_cookie == 0 && !zc->zc_simple)
2070 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2071 NULL, DS_FIND_SNAPSHOTS);
2073 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2075 return (error == ENOENT ? ESRCH : error);
2078 * A dataset name of maximum length cannot have any snapshots,
2079 * so exit immediately.
2081 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2082 dmu_objset_rele(os, FTAG);
2086 error = dmu_snapshot_list_next(os,
2087 sizeof (zc->zc_name) - strlen(zc->zc_name),
2088 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2091 if (error == 0 && !zc->zc_simple) {
2093 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2096 * Since we probably don't have a hold on this snapshot,
2097 * it's possible that the objsetid could have been destroyed
2098 * and reused for a new objset. It's OK if this happens during
2099 * a zfs send operation, since the new createtxg will be
2100 * beyond the range we're interested in.
2102 rw_enter(&dp->dp_config_rwlock, RW_READER);
2103 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2104 rw_exit(&dp->dp_config_rwlock);
2106 if (error == ENOENT) {
2107 /* Racing with destroy, get the next one. */
2108 *strchr(zc->zc_name, '@') = '\0';
2109 dmu_objset_rele(os, FTAG);
2115 error = dmu_objset_from_ds(ds, &ossnap);
2117 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2118 dsl_dataset_rele(ds, FTAG);
2120 } else if (error == ENOENT) {
2124 dmu_objset_rele(os, FTAG);
2125 /* if we failed, undo the @ that we tacked on to zc_name */
2127 *strchr(zc->zc_name, '@') = '\0';
2132 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2134 const char *propname = nvpair_name(pair);
2136 unsigned int vallen;
2139 zfs_userquota_prop_t type;
2145 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2147 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2148 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2154 * A correctly constructed propname is encoded as
2155 * userquota@<rid>-<domain>.
2157 if ((dash = strchr(propname, '-')) == NULL ||
2158 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2167 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2169 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2170 zfsvfs_rele(zfsvfs, FTAG);
2177 * If the named property is one that has a special function to set its value,
2178 * return 0 on success and a positive error code on failure; otherwise if it is
2179 * not one of the special properties handled by this function, return -1.
2181 * XXX: It would be better for callers of the property interface if we handled
2182 * these special cases in dsl_prop.c (in the dsl layer).
2185 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2188 const char *propname = nvpair_name(pair);
2189 zfs_prop_t prop = zfs_name_to_prop(propname);
2193 if (prop == ZPROP_INVAL) {
2194 if (zfs_prop_userquota(propname))
2195 return (zfs_prop_set_userquota(dsname, pair));
2199 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2201 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2202 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2206 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2209 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2212 case ZFS_PROP_QUOTA:
2213 err = dsl_dir_set_quota(dsname, source, intval);
2215 case ZFS_PROP_REFQUOTA:
2216 err = dsl_dataset_set_quota(dsname, source, intval);
2218 case ZFS_PROP_RESERVATION:
2219 err = dsl_dir_set_reservation(dsname, source, intval);
2221 case ZFS_PROP_REFRESERVATION:
2222 err = dsl_dataset_set_reservation(dsname, source, intval);
2224 case ZFS_PROP_VOLSIZE:
2225 err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip),
2228 case ZFS_PROP_VERSION:
2232 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2235 err = zfs_set_version(zfsvfs, intval);
2236 zfsvfs_rele(zfsvfs, FTAG);
2238 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2241 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2242 (void) strcpy(zc->zc_name, dsname);
2243 (void) zfs_ioc_userspace_upgrade(zc);
2244 kmem_free(zc, sizeof (zfs_cmd_t));
2257 * This function is best effort. If it fails to set any of the given properties,
2258 * it continues to set as many as it can and returns the first error
2259 * encountered. If the caller provides a non-NULL errlist, it also gives the
2260 * complete list of names of all the properties it failed to set along with the
2261 * corresponding error numbers. The caller is responsible for freeing the
2264 * If every property is set successfully, zero is returned and the list pointed
2265 * at by errlist is NULL.
2268 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2276 nvlist_t *genericnvl;
2280 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2281 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2282 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2286 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2287 const char *propname = nvpair_name(pair);
2288 zfs_prop_t prop = zfs_name_to_prop(propname);
2291 /* decode the property value */
2293 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2295 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2296 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2301 /* Validate value type */
2302 if (err == 0 && prop == ZPROP_INVAL) {
2303 if (zfs_prop_user(propname)) {
2304 if (nvpair_type(propval) != DATA_TYPE_STRING)
2306 } else if (zfs_prop_userquota(propname)) {
2307 if (nvpair_type(propval) !=
2308 DATA_TYPE_UINT64_ARRAY)
2313 } else if (err == 0) {
2314 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2315 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2317 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2320 VERIFY(nvpair_value_uint64(propval,
2323 switch (zfs_prop_get_type(prop)) {
2324 case PROP_TYPE_NUMBER:
2326 case PROP_TYPE_STRING:
2329 case PROP_TYPE_INDEX:
2330 if (zfs_prop_index_to_string(prop,
2331 intval, &unused) != 0)
2336 "unknown property type");
2343 /* Validate permissions */
2345 err = zfs_check_settable(dsname, pair, CRED());
2348 err = zfs_prop_set_special(dsname, source, pair);
2351 * For better performance we build up a list of
2352 * properties to set in a single transaction.
2354 err = nvlist_add_nvpair(genericnvl, pair);
2355 } else if (err != 0 && nvl != retrynvl) {
2357 * This may be a spurious error caused by
2358 * receiving quota and reservation out of order.
2359 * Try again in a second pass.
2361 err = nvlist_add_nvpair(retrynvl, pair);
2366 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2369 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2374 if (!nvlist_empty(genericnvl) &&
2375 dsl_props_set(dsname, source, genericnvl) != 0) {
2377 * If this fails, we still want to set as many properties as we
2378 * can, so try setting them individually.
2381 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2382 const char *propname = nvpair_name(pair);
2386 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2388 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2389 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2393 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2394 VERIFY(nvpair_value_string(propval,
2396 err = dsl_prop_set(dsname, propname, source, 1,
2397 strlen(strval) + 1, strval);
2399 VERIFY(nvpair_value_uint64(propval,
2401 err = dsl_prop_set(dsname, propname, source, 8,
2406 VERIFY(nvlist_add_int32(errors, propname,
2411 nvlist_free(genericnvl);
2412 nvlist_free(retrynvl);
2414 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2415 nvlist_free(errors);
2418 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2421 if (errlist == NULL)
2422 nvlist_free(errors);
2430 * Check that all the properties are valid user properties.
2433 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2435 nvpair_t *pair = NULL;
2438 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2439 const char *propname = nvpair_name(pair);
2442 if (!zfs_prop_user(propname) ||
2443 nvpair_type(pair) != DATA_TYPE_STRING)
2446 if (error = zfs_secpolicy_write_perms(fsname,
2447 ZFS_DELEG_PERM_USERPROP, CRED()))
2450 if (strlen(propname) >= ZAP_MAXNAMELEN)
2451 return (ENAMETOOLONG);
2453 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2454 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2461 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2465 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2468 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2469 if (nvlist_exists(skipped, nvpair_name(pair)))
2472 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2477 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2481 nvlist_t *cleared_props = NULL;
2482 props_skip(props, skipped, &cleared_props);
2483 if (!nvlist_empty(cleared_props)) {
2485 * Acts on local properties until the dataset has received
2486 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2488 zprop_source_t flags = (ZPROP_SRC_NONE |
2489 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2490 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2492 nvlist_free(cleared_props);
2498 * zc_name name of filesystem
2499 * zc_value name of property to set
2500 * zc_nvlist_src{_size} nvlist of properties to apply
2501 * zc_cookie received properties flag
2504 * zc_nvlist_dst{_size} error for each unapplied received property
2507 zfs_ioc_set_prop(zfs_cmd_t *zc)
2510 boolean_t received = zc->zc_cookie;
2511 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2513 nvlist_t *errors = NULL;
2516 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2517 zc->zc_iflags, &nvl)) != 0)
2521 nvlist_t *origprops;
2524 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2525 if (dsl_prop_get_received(os, &origprops) == 0) {
2526 (void) clear_received_props(os,
2527 zc->zc_name, origprops, nvl);
2528 nvlist_free(origprops);
2531 dsl_prop_set_hasrecvd(os);
2532 dmu_objset_rele(os, FTAG);
2536 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2538 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2539 (void) put_nvlist(zc, errors);
2542 nvlist_free(errors);
2549 * zc_name name of filesystem
2550 * zc_value name of property to inherit
2551 * zc_cookie revert to received value if TRUE
2556 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2558 const char *propname = zc->zc_value;
2559 zfs_prop_t prop = zfs_name_to_prop(propname);
2560 boolean_t received = zc->zc_cookie;
2561 zprop_source_t source = (received
2562 ? ZPROP_SRC_NONE /* revert to received value, if any */
2563 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2572 * zfs_prop_set_special() expects properties in the form of an
2573 * nvpair with type info.
2575 if (prop == ZPROP_INVAL) {
2576 if (!zfs_prop_user(propname))
2579 type = PROP_TYPE_STRING;
2580 } else if (prop == ZFS_PROP_VOLSIZE ||
2581 prop == ZFS_PROP_VERSION) {
2584 type = zfs_prop_get_type(prop);
2587 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2590 case PROP_TYPE_STRING:
2591 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2593 case PROP_TYPE_NUMBER:
2594 case PROP_TYPE_INDEX:
2595 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2602 pair = nvlist_next_nvpair(dummy, NULL);
2603 err = zfs_prop_set_special(zc->zc_name, source, pair);
2606 return (err); /* special property already handled */
2609 * Only check this in the non-received case. We want to allow
2610 * 'inherit -S' to revert non-inheritable properties like quota
2611 * and reservation to the received or default values even though
2612 * they are not considered inheritable.
2614 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2618 /* the property name has been validated by zfs_secpolicy_inherit() */
2619 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2623 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2630 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2631 zc->zc_iflags, &props))
2635 * If the only property is the configfile, then just do a spa_lookup()
2636 * to handle the faulted case.
2638 pair = nvlist_next_nvpair(props, NULL);
2639 if (pair != NULL && strcmp(nvpair_name(pair),
2640 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2641 nvlist_next_nvpair(props, pair) == NULL) {
2642 mutex_enter(&spa_namespace_lock);
2643 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2644 spa_configfile_set(spa, props, B_FALSE);
2645 spa_config_sync(spa, B_FALSE, B_TRUE);
2647 mutex_exit(&spa_namespace_lock);
2654 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2659 error = spa_prop_set(spa, props);
2662 spa_close(spa, FTAG);
2668 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2672 nvlist_t *nvp = NULL;
2674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2676 * If the pool is faulted, there may be properties we can still
2677 * get (such as altroot and cachefile), so attempt to get them
2680 mutex_enter(&spa_namespace_lock);
2681 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2682 error = spa_prop_get(spa, &nvp);
2683 mutex_exit(&spa_namespace_lock);
2685 error = spa_prop_get(spa, &nvp);
2686 spa_close(spa, FTAG);
2689 if (error == 0 && zc->zc_nvlist_dst != 0)
2690 error = put_nvlist(zc, nvp);
2700 * zc_name name of filesystem
2701 * zc_nvlist_src{_size} nvlist of delegated permissions
2702 * zc_perm_action allow/unallow flag
2707 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2710 nvlist_t *fsaclnv = NULL;
2712 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2713 zc->zc_iflags, &fsaclnv)) != 0)
2717 * Verify nvlist is constructed correctly
2719 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2720 nvlist_free(fsaclnv);
2725 * If we don't have PRIV_SYS_MOUNT, then validate
2726 * that user is allowed to hand out each permission in
2730 error = secpolicy_zfs(CRED());
2732 if (zc->zc_perm_action == B_FALSE) {
2733 error = dsl_deleg_can_allow(zc->zc_name,
2736 error = dsl_deleg_can_unallow(zc->zc_name,
2742 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2744 nvlist_free(fsaclnv);
2750 * zc_name name of filesystem
2753 * zc_nvlist_src{_size} nvlist of delegated permissions
2756 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2761 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2762 error = put_nvlist(zc, nvp);
2770 * Search the vfs list for a specified resource. Returns a pointer to it
2771 * or NULL if no suitable entry is found. The caller of this routine
2772 * is responsible for releasing the returned vfs pointer.
2775 zfs_get_vfs(const char *resource)
2779 mtx_lock(&mountlist_mtx);
2780 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
2781 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2786 mtx_unlock(&mountlist_mtx);
2792 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2794 zfs_creat_t *zct = arg;
2796 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2799 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2803 * createprops list of properties requested by creator
2804 * default_zplver zpl version to use if unspecified in createprops
2805 * fuids_ok fuids allowed in this version of the spa?
2806 * os parent objset pointer (NULL if root fs)
2809 * zplprops values for the zplprops we attach to the master node object
2810 * is_ci true if requested file system will be purely case-insensitive
2812 * Determine the settings for utf8only, normalization and
2813 * casesensitivity. Specific values may have been requested by the
2814 * creator and/or we can inherit values from the parent dataset. If
2815 * the file system is of too early a vintage, a creator can not
2816 * request settings for these properties, even if the requested
2817 * setting is the default value. We don't actually want to create dsl
2818 * properties for these, so remove them from the source nvlist after
2822 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2823 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2824 nvlist_t *zplprops, boolean_t *is_ci)
2826 uint64_t sense = ZFS_PROP_UNDEFINED;
2827 uint64_t norm = ZFS_PROP_UNDEFINED;
2828 uint64_t u8 = ZFS_PROP_UNDEFINED;
2830 ASSERT(zplprops != NULL);
2833 * Pull out creator prop choices, if any.
2836 (void) nvlist_lookup_uint64(createprops,
2837 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2838 (void) nvlist_lookup_uint64(createprops,
2839 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2840 (void) nvlist_remove_all(createprops,
2841 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2842 (void) nvlist_lookup_uint64(createprops,
2843 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2844 (void) nvlist_remove_all(createprops,
2845 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2846 (void) nvlist_lookup_uint64(createprops,
2847 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2848 (void) nvlist_remove_all(createprops,
2849 zfs_prop_to_name(ZFS_PROP_CASE));
2853 * If the zpl version requested is whacky or the file system
2854 * or pool is version is too "young" to support normalization
2855 * and the creator tried to set a value for one of the props,
2858 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2859 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2860 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2861 (zplver < ZPL_VERSION_NORMALIZATION &&
2862 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2863 sense != ZFS_PROP_UNDEFINED)))
2867 * Put the version in the zplprops
2869 VERIFY(nvlist_add_uint64(zplprops,
2870 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2872 if (norm == ZFS_PROP_UNDEFINED)
2873 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2874 VERIFY(nvlist_add_uint64(zplprops,
2875 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2878 * If we're normalizing, names must always be valid UTF-8 strings.
2882 if (u8 == ZFS_PROP_UNDEFINED)
2883 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2884 VERIFY(nvlist_add_uint64(zplprops,
2885 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2887 if (sense == ZFS_PROP_UNDEFINED)
2888 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2889 VERIFY(nvlist_add_uint64(zplprops,
2890 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2893 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2899 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2900 nvlist_t *zplprops, boolean_t *is_ci)
2902 boolean_t fuids_ok, sa_ok;
2903 uint64_t zplver = ZPL_VERSION;
2904 objset_t *os = NULL;
2905 char parentname[MAXNAMELEN];
2911 (void) strlcpy(parentname, dataset, sizeof (parentname));
2912 cp = strrchr(parentname, '/');
2916 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2919 spa_vers = spa_version(spa);
2920 spa_close(spa, FTAG);
2922 zplver = zfs_zpl_version_map(spa_vers);
2923 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2924 sa_ok = (zplver >= ZPL_VERSION_SA);
2927 * Open parent object set so we can inherit zplprop values.
2929 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2932 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2934 dmu_objset_rele(os, FTAG);
2939 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2940 nvlist_t *zplprops, boolean_t *is_ci)
2944 uint64_t zplver = ZPL_VERSION;
2947 zplver = zfs_zpl_version_map(spa_vers);
2948 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2949 sa_ok = (zplver >= ZPL_VERSION_SA);
2951 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2952 createprops, zplprops, is_ci);
2958 * zc_objset_type type of objset to create (fs vs zvol)
2959 * zc_name name of new objset
2960 * zc_value name of snapshot to clone from (may be empty)
2961 * zc_nvlist_src{_size} nvlist of properties to apply
2966 zfs_ioc_create(zfs_cmd_t *zc)
2971 nvlist_t *nvprops = NULL;
2972 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2973 dmu_objset_type_t type = zc->zc_objset_type;
2978 cbfunc = zfs_create_cb;
2982 cbfunc = zvol_create_cb;
2989 if (strchr(zc->zc_name, '@') ||
2990 strchr(zc->zc_name, '%'))
2993 if (zc->zc_nvlist_src != 0 &&
2994 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2995 zc->zc_iflags, &nvprops)) != 0)
2998 zct.zct_zplprops = NULL;
2999 zct.zct_props = nvprops;
3001 if (zc->zc_value[0] != '\0') {
3003 * We're creating a clone of an existing snapshot.
3005 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3006 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
3007 nvlist_free(nvprops);
3011 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
3013 nvlist_free(nvprops);
3017 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
3018 dmu_objset_rele(clone, FTAG);
3020 nvlist_free(nvprops);
3024 boolean_t is_insensitive = B_FALSE;
3026 if (cbfunc == NULL) {
3027 nvlist_free(nvprops);
3031 if (type == DMU_OST_ZVOL) {
3032 uint64_t volsize, volblocksize;
3034 if (nvprops == NULL ||
3035 nvlist_lookup_uint64(nvprops,
3036 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3038 nvlist_free(nvprops);
3042 if ((error = nvlist_lookup_uint64(nvprops,
3043 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3044 &volblocksize)) != 0 && error != ENOENT) {
3045 nvlist_free(nvprops);
3050 volblocksize = zfs_prop_default_numeric(
3051 ZFS_PROP_VOLBLOCKSIZE);
3053 if ((error = zvol_check_volblocksize(
3054 volblocksize)) != 0 ||
3055 (error = zvol_check_volsize(volsize,
3056 volblocksize)) != 0) {
3057 nvlist_free(nvprops);
3060 } else if (type == DMU_OST_ZFS) {
3064 * We have to have normalization and
3065 * case-folding flags correct when we do the
3066 * file system creation, so go figure them out
3069 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3070 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3071 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3072 zct.zct_zplprops, &is_insensitive);
3074 nvlist_free(nvprops);
3075 nvlist_free(zct.zct_zplprops);
3079 error = dmu_objset_create(zc->zc_name, type,
3080 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3081 nvlist_free(zct.zct_zplprops);
3085 * It would be nice to do this atomically.
3088 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3091 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3093 nvlist_free(nvprops);
3095 if (error == 0 && type == DMU_OST_ZVOL)
3096 zvol_create_minors(zc->zc_name);
3103 * zc_name name of filesystem
3104 * zc_value short name of snapshot
3105 * zc_cookie recursive flag
3106 * zc_nvlist_src[_size] property list
3109 * zc_value short snapname (i.e. part after the '@')
3112 zfs_ioc_snapshot(zfs_cmd_t *zc)
3114 nvlist_t *nvprops = NULL;
3116 boolean_t recursive = zc->zc_cookie;
3118 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3121 if (zc->zc_nvlist_src != 0 &&
3122 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3123 zc->zc_iflags, &nvprops)) != 0)
3126 error = zfs_check_userprops(zc->zc_name, nvprops);
3130 if (!nvlist_empty(nvprops) &&
3131 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3136 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3137 nvprops, recursive, B_FALSE, -1);
3140 nvlist_free(nvprops);
3145 zfs_unmount_snap(const char *name, void *arg)
3150 char *snapname = arg;
3151 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3152 vfsp = zfs_get_vfs(fullname);
3154 } else if (strchr(name, '@')) {
3155 vfsp = zfs_get_vfs(name);
3160 * Always force the unmount for snapshots.
3162 int flag = MS_FORCE;
3165 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3170 mtx_lock(&Giant); /* dounmount() */
3171 dounmount(vfsp, flag, curthread);
3172 mtx_unlock(&Giant); /* dounmount() */
3179 * zc_name name of filesystem, snaps must be under it
3180 * zc_nvlist_src[_size] full names of snapshots to destroy
3181 * zc_defer_destroy mark for deferred destroy
3184 * zc_name on failure, name of failed snapshot
3187 zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc)
3193 if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3194 zc->zc_iflags, &nvl)) != 0) {
3199 * We are probably called by older binaries,
3200 * allocate and populate nvlist with recursive snapshots
3202 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3204 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3205 err = dmu_get_recursive_snaps_nvl(zc->zc_name,
3211 #endif /* __FreeBSD__ */
3214 len = strlen(zc->zc_name);
3215 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
3216 pair = nvlist_next_nvpair(nvl, pair)) {
3217 const char *name = nvpair_name(pair);
3219 * The snap name must be underneath the zc_name. This ensures
3220 * that our permission checks were legitimate.
3222 if (strncmp(zc->zc_name, name, len) != 0 ||
3223 (name[len] != '@' && name[len] != '/')) {
3228 (void) zfs_unmount_snap(name, NULL);
3229 (void) zvol_remove_minor(name);
3232 err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy,
3240 * zc_name name of dataset to destroy
3241 * zc_objset_type type of objset
3242 * zc_defer_destroy mark for deferred destroy
3247 zfs_ioc_destroy(zfs_cmd_t *zc)
3250 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3251 err = zfs_unmount_snap(zc->zc_name, NULL);
3256 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3257 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3258 (void) zvol_remove_minor(zc->zc_name);
3264 * zc_name name of dataset to rollback (to most recent snapshot)
3269 zfs_ioc_rollback(zfs_cmd_t *zc)
3271 dsl_dataset_t *ds, *clone;
3276 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3280 /* must not be a snapshot */
3281 if (dsl_dataset_is_snapshot(ds)) {
3282 dsl_dataset_rele(ds, FTAG);
3286 /* must have a most recent snapshot */
3287 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3288 dsl_dataset_rele(ds, FTAG);
3293 * Create clone of most recent snapshot.
3295 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3296 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3300 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3307 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3308 error = zfs_suspend_fs(zfsvfs);
3312 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3313 error = dsl_dataset_clone_swap(clone, ds,
3315 dsl_dataset_disown(ds, FTAG);
3320 resume_err = zfs_resume_fs(zfsvfs, zc->zc_name);
3321 error = error ? error : resume_err;
3323 VFS_RELE(zfsvfs->z_vfs);
3325 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3326 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3327 dsl_dataset_disown(ds, FTAG);
3335 * Destroy clone (which also closes it).
3337 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3340 strfree(clone_name);
3342 dsl_dataset_rele(ds, FTAG);
3348 * zc_name old name of dataset
3349 * zc_value new name of dataset
3350 * zc_cookie recursive flag (only valid for snapshots)
3355 zfs_ioc_rename(zfs_cmd_t *zc)
3359 if (zc->zc_cookie & 1)
3360 flags |= ZFS_RENAME_RECURSIVE;
3361 if (zc->zc_cookie & 2)
3362 flags |= ZFS_RENAME_ALLOW_MOUNTED;
3364 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3365 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3366 strchr(zc->zc_value, '%'))
3370 * Unmount snapshot unless we're doing a recursive rename,
3371 * in which case the dataset code figures out which snapshots
3374 if (!(flags & ZFS_RENAME_RECURSIVE) &&
3375 strchr(zc->zc_name, '@') != NULL &&
3376 zc->zc_objset_type == DMU_OST_ZFS) {
3377 int err = zfs_unmount_snap(zc->zc_name, NULL);
3381 return (dmu_objset_rename(zc->zc_name, zc->zc_value, flags));
3385 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3387 const char *propname = nvpair_name(pair);
3388 boolean_t issnap = (strchr(dsname, '@') != NULL);
3389 zfs_prop_t prop = zfs_name_to_prop(propname);
3393 if (prop == ZPROP_INVAL) {
3394 if (zfs_prop_user(propname)) {
3395 if (err = zfs_secpolicy_write_perms(dsname,
3396 ZFS_DELEG_PERM_USERPROP, cr))
3401 if (!issnap && zfs_prop_userquota(propname)) {
3402 const char *perm = NULL;
3403 const char *uq_prefix =
3404 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3405 const char *gq_prefix =
3406 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3408 if (strncmp(propname, uq_prefix,
3409 strlen(uq_prefix)) == 0) {
3410 perm = ZFS_DELEG_PERM_USERQUOTA;
3411 } else if (strncmp(propname, gq_prefix,
3412 strlen(gq_prefix)) == 0) {
3413 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3415 /* USERUSED and GROUPUSED are read-only */
3419 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3430 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3432 * dsl_prop_get_all_impl() returns properties in this
3436 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3437 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3442 * Check that this value is valid for this pool version
3445 case ZFS_PROP_COMPRESSION:
3447 * If the user specified gzip compression, make sure
3448 * the SPA supports it. We ignore any errors here since
3449 * we'll catch them later.
3451 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3452 nvpair_value_uint64(pair, &intval) == 0) {
3453 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3454 intval <= ZIO_COMPRESS_GZIP_9 &&
3455 zfs_earlier_version(dsname,
3456 SPA_VERSION_GZIP_COMPRESSION)) {
3460 if (intval == ZIO_COMPRESS_ZLE &&
3461 zfs_earlier_version(dsname,
3462 SPA_VERSION_ZLE_COMPRESSION))
3466 * If this is a bootable dataset then
3467 * verify that the compression algorithm
3468 * is supported for booting. We must return
3469 * something other than ENOTSUP since it
3470 * implies a downrev pool version.
3472 if (zfs_is_bootfs(dsname) &&
3473 !BOOTFS_COMPRESS_VALID(intval)) {
3479 case ZFS_PROP_COPIES:
3480 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3484 case ZFS_PROP_DEDUP:
3485 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3489 case ZFS_PROP_SHARESMB:
3490 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3494 case ZFS_PROP_ACLINHERIT:
3495 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3496 nvpair_value_uint64(pair, &intval) == 0) {
3497 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3498 zfs_earlier_version(dsname,
3499 SPA_VERSION_PASSTHROUGH_X))
3505 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3509 * Removes properties from the given props list that fail permission checks
3510 * needed to clear them and to restore them in case of a receive error. For each
3511 * property, make sure we have both set and inherit permissions.
3513 * Returns the first error encountered if any permission checks fail. If the
3514 * caller provides a non-NULL errlist, it also gives the complete list of names
3515 * of all the properties that failed a permission check along with the
3516 * corresponding error numbers. The caller is responsible for freeing the
3519 * If every property checks out successfully, zero is returned and the list
3520 * pointed at by errlist is NULL.
3523 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3526 nvpair_t *pair, *next_pair;
3533 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3535 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3536 (void) strcpy(zc->zc_name, dataset);
3537 pair = nvlist_next_nvpair(props, NULL);
3538 while (pair != NULL) {
3539 next_pair = nvlist_next_nvpair(props, pair);
3541 (void) strcpy(zc->zc_value, nvpair_name(pair));
3542 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3543 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3544 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3545 VERIFY(nvlist_add_int32(errors,
3546 zc->zc_value, err) == 0);
3550 kmem_free(zc, sizeof (zfs_cmd_t));
3552 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3553 nvlist_free(errors);
3556 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3559 if (errlist == NULL)
3560 nvlist_free(errors);
3568 propval_equals(nvpair_t *p1, nvpair_t *p2)
3570 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3571 /* dsl_prop_get_all_impl() format */
3573 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3574 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3578 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3580 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3581 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3585 if (nvpair_type(p1) != nvpair_type(p2))
3588 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3589 char *valstr1, *valstr2;
3591 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3592 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3593 return (strcmp(valstr1, valstr2) == 0);
3595 uint64_t intval1, intval2;
3597 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3598 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3599 return (intval1 == intval2);
3604 * Remove properties from props if they are not going to change (as determined
3605 * by comparison with origprops). Remove them from origprops as well, since we
3606 * do not need to clear or restore properties that won't change.
3609 props_reduce(nvlist_t *props, nvlist_t *origprops)
3611 nvpair_t *pair, *next_pair;
3613 if (origprops == NULL)
3614 return; /* all props need to be received */
3616 pair = nvlist_next_nvpair(props, NULL);
3617 while (pair != NULL) {
3618 const char *propname = nvpair_name(pair);
3621 next_pair = nvlist_next_nvpair(props, pair);
3623 if ((nvlist_lookup_nvpair(origprops, propname,
3624 &match) != 0) || !propval_equals(pair, match))
3625 goto next; /* need to set received value */
3627 /* don't clear the existing received value */
3628 (void) nvlist_remove_nvpair(origprops, match);
3629 /* don't bother receiving the property */
3630 (void) nvlist_remove_nvpair(props, pair);
3637 static boolean_t zfs_ioc_recv_inject_err;
3642 * zc_name name of containing filesystem
3643 * zc_nvlist_src{_size} nvlist of properties to apply
3644 * zc_value name of snapshot to create
3645 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3646 * zc_cookie file descriptor to recv from
3647 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3648 * zc_guid force flag
3649 * zc_cleanup_fd cleanup-on-exit file descriptor
3650 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3653 * zc_cookie number of bytes read
3654 * zc_nvlist_dst{_size} error for each unapplied received property
3655 * zc_obj zprop_errflags_t
3656 * zc_action_handle handle for this guid/ds mapping
3659 zfs_ioc_recv(zfs_cmd_t *zc)
3663 dmu_recv_cookie_t drc;
3664 boolean_t force = (boolean_t)zc->zc_guid;
3667 int props_error = 0;
3670 nvlist_t *props = NULL; /* sent properties */
3671 nvlist_t *origprops = NULL; /* existing properties */
3672 objset_t *origin = NULL;
3674 char tofs[ZFS_MAXNAMELEN];
3675 boolean_t first_recvd_props = B_FALSE;
3677 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3678 strchr(zc->zc_value, '@') == NULL ||
3679 strchr(zc->zc_value, '%'))
3682 (void) strcpy(tofs, zc->zc_value);
3683 tosnap = strchr(tofs, '@');
3686 if (zc->zc_nvlist_src != 0 &&
3687 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3688 zc->zc_iflags, &props)) != 0)
3698 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3700 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3701 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3702 !dsl_prop_get_hasrecvd(os)) {
3703 first_recvd_props = B_TRUE;
3707 * If new received properties are supplied, they are to
3708 * completely replace the existing received properties, so stash
3709 * away the existing ones.
3711 if (dsl_prop_get_received(os, &origprops) == 0) {
3712 nvlist_t *errlist = NULL;
3714 * Don't bother writing a property if its value won't
3715 * change (and avoid the unnecessary security checks).
3717 * The first receive after SPA_VERSION_RECVD_PROPS is a
3718 * special case where we blow away all local properties
3721 if (!first_recvd_props)
3722 props_reduce(props, origprops);
3723 if (zfs_check_clearable(tofs, origprops,
3725 (void) nvlist_merge(errors, errlist, 0);
3726 nvlist_free(errlist);
3729 dmu_objset_rele(os, FTAG);
3732 if (zc->zc_string[0]) {
3733 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3738 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3739 &zc->zc_begin_record, force, origin, &drc);
3741 dmu_objset_rele(origin, FTAG);
3746 * Set properties before we receive the stream so that they are applied
3747 * to the new data. Note that we must call dmu_recv_stream() if
3748 * dmu_recv_begin() succeeds.
3753 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3754 if (drc.drc_newfs) {
3755 if (spa_version(os->os_spa) >=
3756 SPA_VERSION_RECVD_PROPS)
3757 first_recvd_props = B_TRUE;
3758 } else if (origprops != NULL) {
3759 if (clear_received_props(os, tofs, origprops,
3760 first_recvd_props ? NULL : props) != 0)
3761 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3763 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3765 dsl_prop_set_hasrecvd(os);
3766 } else if (!drc.drc_newfs) {
3767 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3770 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3772 (void) nvlist_merge(errors, errlist, 0);
3773 nvlist_free(errlist);
3776 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3778 * Caller made zc->zc_nvlist_dst less than the minimum expected
3779 * size or supplied an invalid address.
3781 props_error = EINVAL;
3785 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
3786 &zc->zc_action_handle);
3789 zfsvfs_t *zfsvfs = NULL;
3791 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3795 error = zfs_suspend_fs(zfsvfs);
3797 * If the suspend fails, then the recv_end will
3798 * likely also fail, and clean up after itself.
3800 end_err = dmu_recv_end(&drc);
3802 error = zfs_resume_fs(zfsvfs, tofs);
3803 error = error ? error : end_err;
3804 VFS_RELE(zfsvfs->z_vfs);
3806 error = dmu_recv_end(&drc);
3810 zc->zc_cookie = off - fp->f_offset;
3811 if (off >= 0 && off <= MAXOFFSET_T)
3815 if (zfs_ioc_recv_inject_err) {
3816 zfs_ioc_recv_inject_err = B_FALSE;
3821 * On error, restore the original props.
3823 if (error && props) {
3824 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3825 if (clear_received_props(os, tofs, props, NULL) != 0) {
3827 * We failed to clear the received properties.
3828 * Since we may have left a $recvd value on the
3829 * system, we can't clear the $hasrecvd flag.
3831 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3832 } else if (first_recvd_props) {
3833 dsl_prop_unset_hasrecvd(os);
3835 dmu_objset_rele(os, FTAG);
3836 } else if (!drc.drc_newfs) {
3837 /* We failed to clear the received properties. */
3838 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3841 if (origprops == NULL && !drc.drc_newfs) {
3842 /* We failed to stash the original properties. */
3843 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3847 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3848 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3849 * explictly if we're restoring local properties cleared in the
3850 * first new-style receive.
3852 if (origprops != NULL &&
3853 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3854 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3855 origprops, NULL) != 0) {
3857 * We stashed the original properties but failed to
3860 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3865 nvlist_free(origprops);
3866 nvlist_free(errors);
3870 error = props_error;
3877 * zc_name name of snapshot to send
3878 * zc_cookie file descriptor to send stream to
3879 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3880 * zc_sendobj objsetid of snapshot to send
3881 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3882 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
3883 * output size in zc_objset_type.
3888 zfs_ioc_send(zfs_cmd_t *zc)
3890 objset_t *fromsnap = NULL;
3895 dsl_dataset_t *dsfrom = NULL;
3898 boolean_t estimate = (zc->zc_guid != 0);
3900 error = spa_open(zc->zc_name, &spa, FTAG);
3904 dp = spa_get_dsl(spa);
3905 rw_enter(&dp->dp_config_rwlock, RW_READER);
3906 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3907 rw_exit(&dp->dp_config_rwlock);
3909 spa_close(spa, FTAG);
3913 error = dmu_objset_from_ds(ds, &tosnap);
3915 dsl_dataset_rele(ds, FTAG);
3916 spa_close(spa, FTAG);
3920 if (zc->zc_fromobj != 0) {
3921 rw_enter(&dp->dp_config_rwlock, RW_READER);
3922 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3923 rw_exit(&dp->dp_config_rwlock);
3924 spa_close(spa, FTAG);
3926 dsl_dataset_rele(ds, FTAG);
3929 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3931 dsl_dataset_rele(dsfrom, FTAG);
3932 dsl_dataset_rele(ds, FTAG);
3936 spa_close(spa, FTAG);
3940 error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj,
3941 &zc->zc_objset_type);
3943 file_t *fp = getf(zc->zc_cookie);
3945 dsl_dataset_rele(ds, FTAG);
3947 dsl_dataset_rele(dsfrom, FTAG);
3952 error = dmu_send(tosnap, fromsnap, zc->zc_obj,
3953 zc->zc_cookie, fp, &off);
3955 if (off >= 0 && off <= MAXOFFSET_T)
3957 releasef(zc->zc_cookie);
3960 dsl_dataset_rele(dsfrom, FTAG);
3961 dsl_dataset_rele(ds, FTAG);
3967 * zc_name name of snapshot on which to report progress
3968 * zc_cookie file descriptor of send stream
3971 * zc_cookie number of bytes written in send stream thus far
3974 zfs_ioc_send_progress(zfs_cmd_t *zc)
3977 dmu_sendarg_t *dsp = NULL;
3980 if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0)
3983 mutex_enter(&ds->ds_sendstream_lock);
3986 * Iterate over all the send streams currently active on this dataset.
3987 * If there's one which matches the specified file descriptor _and_ the
3988 * stream was started by the current process, return the progress of
3991 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
3992 dsp = list_next(&ds->ds_sendstreams, dsp)) {
3993 if (dsp->dsa_outfd == zc->zc_cookie &&
3994 dsp->dsa_proc == curproc)
3999 zc->zc_cookie = *(dsp->dsa_off);
4003 mutex_exit(&ds->ds_sendstream_lock);
4004 dsl_dataset_rele(ds, FTAG);
4009 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4013 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4014 &zc->zc_inject_record);
4017 zc->zc_guid = (uint64_t)id;
4023 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4025 return (zio_clear_fault((int)zc->zc_guid));
4029 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4031 int id = (int)zc->zc_guid;
4034 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4035 &zc->zc_inject_record);
4043 zfs_ioc_error_log(zfs_cmd_t *zc)
4047 size_t count = (size_t)zc->zc_nvlist_dst_size;
4049 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4052 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4055 zc->zc_nvlist_dst_size = count;
4057 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4059 spa_close(spa, FTAG);
4065 zfs_ioc_clear(zfs_cmd_t *zc)
4072 * On zpool clear we also fix up missing slogs
4074 mutex_enter(&spa_namespace_lock);
4075 spa = spa_lookup(zc->zc_name);
4077 mutex_exit(&spa_namespace_lock);
4080 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4081 /* we need to let spa_open/spa_load clear the chains */
4082 spa_set_log_state(spa, SPA_LOG_CLEAR);
4084 spa->spa_last_open_failed = 0;
4085 mutex_exit(&spa_namespace_lock);
4087 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4088 error = spa_open(zc->zc_name, &spa, FTAG);
4091 nvlist_t *config = NULL;
4093 if (zc->zc_nvlist_src == 0)
4096 if ((error = get_nvlist(zc->zc_nvlist_src,
4097 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4098 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4100 if (config != NULL) {
4103 if ((err = put_nvlist(zc, config)) != 0)
4105 nvlist_free(config);
4107 nvlist_free(policy);
4114 spa_vdev_state_enter(spa, SCL_NONE);
4116 if (zc->zc_guid == 0) {
4119 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4121 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4122 spa_close(spa, FTAG);
4127 vdev_clear(spa, vd);
4129 (void) spa_vdev_state_exit(spa, NULL, 0);
4132 * Resume any suspended I/Os.
4134 if (zio_resume(spa) != 0)
4137 spa_close(spa, FTAG);
4143 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4148 error = spa_open(zc->zc_name, &spa, FTAG);
4152 spa_vdev_state_enter(spa, SCL_NONE);
4155 * If a resilver is already in progress then set the
4156 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4157 * the scan as a side effect of the reopen. Otherwise, let
4158 * vdev_open() decided if a resilver is required.
4160 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4161 vdev_reopen(spa->spa_root_vdev);
4162 spa->spa_scrub_reopen = B_FALSE;
4164 (void) spa_vdev_state_exit(spa, NULL, 0);
4165 spa_close(spa, FTAG);
4170 * zc_name name of filesystem
4171 * zc_value name of origin snapshot
4174 * zc_string name of conflicting snapshot, if there is one
4177 zfs_ioc_promote(zfs_cmd_t *zc)
4182 * We don't need to unmount *all* the origin fs's snapshots, but
4185 cp = strchr(zc->zc_value, '@');
4188 (void) dmu_objset_find(zc->zc_value,
4189 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4190 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4194 * Retrieve a single {user|group}{used|quota}@... property.
4197 * zc_name name of filesystem
4198 * zc_objset_type zfs_userquota_prop_t
4199 * zc_value domain name (eg. "S-1-234-567-89")
4200 * zc_guid RID/UID/GID
4203 * zc_cookie property value
4206 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4211 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4214 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4218 error = zfs_userspace_one(zfsvfs,
4219 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4220 zfsvfs_rele(zfsvfs, FTAG);
4227 * zc_name name of filesystem
4228 * zc_cookie zap cursor
4229 * zc_objset_type zfs_userquota_prop_t
4230 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4233 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4234 * zc_cookie zap cursor
4237 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4240 int bufsize = zc->zc_nvlist_dst_size;
4245 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4249 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4251 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4252 buf, &zc->zc_nvlist_dst_size);
4255 error = ddi_copyout(buf,
4256 (void *)(uintptr_t)zc->zc_nvlist_dst,
4257 zc->zc_nvlist_dst_size, zc->zc_iflags);
4259 kmem_free(buf, bufsize);
4260 zfsvfs_rele(zfsvfs, FTAG);
4267 * zc_name name of filesystem
4273 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4279 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4280 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4282 * If userused is not enabled, it may be because the
4283 * objset needs to be closed & reopened (to grow the
4284 * objset_phys_t). Suspend/resume the fs will do that.
4286 error = zfs_suspend_fs(zfsvfs);
4288 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4291 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4292 VFS_RELE(zfsvfs->z_vfs);
4294 /* XXX kind of reading contents without owning */
4295 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4299 error = dmu_objset_userspace_upgrade(os);
4300 dmu_objset_rele(os, FTAG);
4308 * We don't want to have a hard dependency
4309 * against some special symbols in sharefs
4310 * nfs, and smbsrv. Determine them if needed when
4311 * the first file system is shared.
4312 * Neither sharefs, nfs or smbsrv are unloadable modules.
4314 int (*znfsexport_fs)(void *arg);
4315 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4316 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4318 int zfs_nfsshare_inited;
4319 int zfs_smbshare_inited;
4321 ddi_modhandle_t nfs_mod;
4322 ddi_modhandle_t sharefs_mod;
4323 ddi_modhandle_t smbsrv_mod;
4325 kmutex_t zfs_share_lock;
4333 ASSERT(MUTEX_HELD(&zfs_share_lock));
4334 /* Both NFS and SMB shares also require sharetab support. */
4335 if (sharefs_mod == NULL && ((sharefs_mod =
4336 ddi_modopen("fs/sharefs",
4337 KRTLD_MODE_FIRST, &error)) == NULL)) {
4340 if (zshare_fs == NULL && ((zshare_fs =
4341 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4342 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4350 zfs_ioc_share(zfs_cmd_t *zc)
4356 switch (zc->zc_share.z_sharetype) {
4358 case ZFS_UNSHARE_NFS:
4359 if (zfs_nfsshare_inited == 0) {
4360 mutex_enter(&zfs_share_lock);
4361 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4362 KRTLD_MODE_FIRST, &error)) == NULL)) {
4363 mutex_exit(&zfs_share_lock);
4366 if (znfsexport_fs == NULL &&
4367 ((znfsexport_fs = (int (*)(void *))
4369 "nfs_export", &error)) == NULL)) {
4370 mutex_exit(&zfs_share_lock);
4373 error = zfs_init_sharefs();
4375 mutex_exit(&zfs_share_lock);
4378 zfs_nfsshare_inited = 1;
4379 mutex_exit(&zfs_share_lock);
4383 case ZFS_UNSHARE_SMB:
4384 if (zfs_smbshare_inited == 0) {
4385 mutex_enter(&zfs_share_lock);
4386 if (smbsrv_mod == NULL && ((smbsrv_mod =
4387 ddi_modopen("drv/smbsrv",
4388 KRTLD_MODE_FIRST, &error)) == NULL)) {
4389 mutex_exit(&zfs_share_lock);
4392 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4393 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4394 "smb_server_share", &error)) == NULL)) {
4395 mutex_exit(&zfs_share_lock);
4398 error = zfs_init_sharefs();
4400 mutex_exit(&zfs_share_lock);
4403 zfs_smbshare_inited = 1;
4404 mutex_exit(&zfs_share_lock);
4411 switch (zc->zc_share.z_sharetype) {
4413 case ZFS_UNSHARE_NFS:
4415 znfsexport_fs((void *)
4416 (uintptr_t)zc->zc_share.z_exportdata))
4420 case ZFS_UNSHARE_SMB:
4421 if (error = zsmbexport_fs((void *)
4422 (uintptr_t)zc->zc_share.z_exportdata,
4423 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4430 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4431 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4432 SHAREFS_ADD : SHAREFS_REMOVE;
4435 * Add or remove share from sharetab
4437 error = zshare_fs(opcode,
4438 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4439 zc->zc_share.z_sharemax);
4448 ace_t full_access[] = {
4449 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4454 * zc_name name of containing filesystem
4455 * zc_obj object # beyond which we want next in-use object #
4458 * zc_obj next in-use object #
4461 zfs_ioc_next_obj(zfs_cmd_t *zc)
4463 objset_t *os = NULL;
4466 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4470 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4471 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4473 dmu_objset_rele(os, FTAG);
4479 * zc_name name of filesystem
4480 * zc_value prefix name for snapshot
4481 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4486 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4491 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4492 (u_longlong_t)ddi_get_lbolt64());
4494 if (strlen(snap_name) >= MAXNAMELEN) {
4499 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4500 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4506 (void) strcpy(zc->zc_value, snap_name);
4513 * zc_name name of "to" snapshot
4514 * zc_value name of "from" snapshot
4515 * zc_cookie file descriptor to write diff data on
4518 * dmu_diff_record_t's to the file descriptor
4521 zfs_ioc_diff(zfs_cmd_t *zc)
4529 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4533 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4535 dmu_objset_rele(tosnap, FTAG);
4539 fp = getf(zc->zc_cookie);
4541 dmu_objset_rele(fromsnap, FTAG);
4542 dmu_objset_rele(tosnap, FTAG);
4548 error = dmu_diff(tosnap, fromsnap, fp, &off);
4550 if (off >= 0 && off <= MAXOFFSET_T)
4552 releasef(zc->zc_cookie);
4554 dmu_objset_rele(fromsnap, FTAG);
4555 dmu_objset_rele(tosnap, FTAG);
4561 * Remove all ACL files in shares dir
4564 zfs_smb_acl_purge(znode_t *dzp)
4567 zap_attribute_t zap;
4568 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4571 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4572 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4573 zap_cursor_advance(&zc)) {
4574 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4578 zap_cursor_fini(&zc);
4584 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4589 vnode_t *resourcevp = NULL;
4598 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4599 NO_FOLLOW, NULL, &vp)) != 0)
4602 /* Now make sure mntpnt and dataset are ZFS */
4604 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
4605 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4606 zc->zc_name) != 0)) {
4612 zfsvfs = dzp->z_zfsvfs;
4616 * Create share dir if its missing.
4618 mutex_enter(&zfsvfs->z_lock);
4619 if (zfsvfs->z_shares_dir == 0) {
4622 tx = dmu_tx_create(zfsvfs->z_os);
4623 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4625 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4626 error = dmu_tx_assign(tx, TXG_WAIT);
4630 error = zfs_create_share_dir(zfsvfs, tx);
4634 mutex_exit(&zfsvfs->z_lock);
4640 mutex_exit(&zfsvfs->z_lock);
4642 ASSERT(zfsvfs->z_shares_dir);
4643 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4649 switch (zc->zc_cookie) {
4650 case ZFS_SMB_ACL_ADD:
4651 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4652 vattr.va_type = VREG;
4653 vattr.va_mode = S_IFREG|0777;
4657 vsec.vsa_mask = VSA_ACE;
4658 vsec.vsa_aclentp = &full_access;
4659 vsec.vsa_aclentsz = sizeof (full_access);
4660 vsec.vsa_aclcnt = 1;
4662 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4663 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4665 VN_RELE(resourcevp);
4668 case ZFS_SMB_ACL_REMOVE:
4669 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4673 case ZFS_SMB_ACL_RENAME:
4674 if ((error = get_nvlist(zc->zc_nvlist_src,
4675 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4680 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4681 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4684 VN_RELE(ZTOV(sharedir));
4686 nvlist_free(nvlist);
4689 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4691 nvlist_free(nvlist);
4694 case ZFS_SMB_ACL_PURGE:
4695 error = zfs_smb_acl_purge(sharedir);
4704 VN_RELE(ZTOV(sharedir));
4710 return (EOPNOTSUPP);
4716 * zc_name name of filesystem
4717 * zc_value short name of snap
4718 * zc_string user-supplied tag for this hold
4719 * zc_cookie recursive flag
4720 * zc_temphold set if hold is temporary
4721 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4722 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4723 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4728 zfs_ioc_hold(zfs_cmd_t *zc)
4730 boolean_t recursive = zc->zc_cookie;
4737 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4740 if (zc->zc_sendobj == 0) {
4741 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4742 zc->zc_string, recursive, zc->zc_temphold,
4743 zc->zc_cleanup_fd));
4749 error = spa_open(zc->zc_name, &spa, FTAG);
4753 dp = spa_get_dsl(spa);
4754 rw_enter(&dp->dp_config_rwlock, RW_READER);
4755 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4756 rw_exit(&dp->dp_config_rwlock);
4757 spa_close(spa, FTAG);
4762 * Until we have a hold on this snapshot, it's possible that
4763 * zc_sendobj could've been destroyed and reused as part
4764 * of a later txg. Make sure we're looking at the right object.
4766 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4767 dsl_dataset_rele(ds, FTAG);
4771 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4772 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4774 dsl_dataset_rele(ds, FTAG);
4779 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4783 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4786 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4788 dsl_dataset_rele(ds, FTAG);
4795 * zc_name name of dataset from which we're releasing a user hold
4796 * zc_value short name of snap
4797 * zc_string user-supplied tag for this hold
4798 * zc_cookie recursive flag
4803 zfs_ioc_release(zfs_cmd_t *zc)
4805 boolean_t recursive = zc->zc_cookie;
4807 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4810 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4811 zc->zc_string, recursive));
4816 * zc_name name of filesystem
4819 * zc_nvlist_src{_size} nvlist of snapshot holds
4822 zfs_ioc_get_holds(zfs_cmd_t *zc)
4827 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4828 error = put_nvlist(zc, nvp);
4837 * zc_name name of new filesystem or snapshot
4838 * zc_value full name of old snapshot
4841 * zc_cookie space in bytes
4842 * zc_objset_type compressed space in bytes
4843 * zc_perm_action uncompressed space in bytes
4846 zfs_ioc_space_written(zfs_cmd_t *zc)
4849 dsl_dataset_t *new, *old;
4851 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4854 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4856 dsl_dataset_rele(new, FTAG);
4860 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4861 &zc->zc_objset_type, &zc->zc_perm_action);
4862 dsl_dataset_rele(old, FTAG);
4863 dsl_dataset_rele(new, FTAG);
4869 * zc_name full name of last snapshot
4870 * zc_value full name of first snapshot
4873 * zc_cookie space in bytes
4874 * zc_objset_type compressed space in bytes
4875 * zc_perm_action uncompressed space in bytes
4878 zfs_ioc_space_snaps(zfs_cmd_t *zc)
4881 dsl_dataset_t *new, *old;
4883 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4886 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4888 dsl_dataset_rele(new, FTAG);
4892 error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4893 &zc->zc_objset_type, &zc->zc_perm_action);
4894 dsl_dataset_rele(old, FTAG);
4895 dsl_dataset_rele(new, FTAG);
4900 * pool create, destroy, and export don't log the history as part of
4901 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4902 * do the logging of those commands.
4905 zfs_ioc_jail(zfs_cmd_t *zc)
4908 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
4909 (int)zc->zc_jailid));
4913 zfs_ioc_unjail(zfs_cmd_t *zc)
4916 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
4917 (int)zc->zc_jailid));
4920 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4921 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4923 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4925 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4927 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4929 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4931 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4933 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4935 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4937 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4939 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4941 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4943 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4945 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4947 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4949 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4951 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4953 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4955 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4957 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4959 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4961 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4963 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4965 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
4966 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
4967 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4969 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4971 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE },
4972 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
4973 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE, B_FALSE },
4974 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4976 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4978 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4980 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4982 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
4983 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4985 { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive, DATASET_NAME,
4987 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4989 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4991 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4993 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4995 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4997 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4999 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5001 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
5002 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
5004 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
5006 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
5007 DATASET_NAME, B_FALSE, B_FALSE },
5008 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
5009 DATASET_NAME, B_FALSE, B_FALSE },
5010 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5011 DATASET_NAME, B_FALSE, B_TRUE },
5012 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE },
5013 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
5015 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5017 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5019 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5021 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5023 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE, B_FALSE },
5024 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
5026 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
5028 { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
5029 { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
5030 { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5032 { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5034 { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5036 { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5038 { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5043 pool_status_check(const char *name, zfs_ioc_namecheck_t type)
5048 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5050 error = spa_open(name, &spa, FTAG);
5052 if (spa_suspended(spa))
5054 spa_close(spa, FTAG);
5060 * Find a free minor number.
5063 zfsdev_minor_alloc(void)
5065 static minor_t last_minor;
5068 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5070 for (m = last_minor + 1; m != last_minor; m++) {
5071 if (m > ZFSDEV_MAX_MINOR)
5073 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5083 zfs_ctldev_init(struct cdev *devp)
5086 zfs_soft_state_t *zs;
5088 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5090 minor = zfsdev_minor_alloc();
5094 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5097 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
5099 zs = ddi_get_soft_state(zfsdev_state, minor);
5100 zs->zss_type = ZSST_CTLDEV;
5101 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5107 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5109 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5111 zfs_onexit_destroy(zo);
5112 ddi_soft_state_free(zfsdev_state, minor);
5116 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5118 zfs_soft_state_t *zp;
5120 zp = ddi_get_soft_state(zfsdev_state, minor);
5121 if (zp == NULL || zp->zss_type != which)
5124 return (zp->zss_data);
5128 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
5133 if (getminor(*devp) != 0)
5134 return (zvol_open(devp, flag, otyp, cr));
5137 /* This is the control device. Allocate a new minor if requested. */
5139 mutex_enter(&spa_namespace_lock);
5140 error = zfs_ctldev_init(devp);
5141 mutex_exit(&spa_namespace_lock);
5148 zfsdev_close(void *data)
5151 minor_t minor = (minor_t)(uintptr_t)data;
5156 mutex_enter(&spa_namespace_lock);
5157 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5159 mutex_exit(&spa_namespace_lock);
5162 zfs_ctldev_destroy(zo, minor);
5163 mutex_exit(&spa_namespace_lock);
5167 zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
5172 int cflag, error, len;
5174 cflag = ZFS_CMD_COMPAT_NONE;
5175 len = IOCPARM_LEN(cmd);
5178 * Check if we have sufficient kernel memory allocated
5179 * for the zfs_cmd_t request. Bail out if not so we
5180 * will not access undefined memory region.
5182 if (len < sizeof(zfs_cmd_t))
5183 if (len == sizeof(zfs_cmd_v15_t)) {
5184 cflag = ZFS_CMD_COMPAT_V15;
5185 vec = zfs_ioctl_v15_to_v28[ZFS_IOC(cmd)];
5191 if (cflag != ZFS_CMD_COMPAT_NONE) {
5192 if (vec == ZFS_IOC_COMPAT_PASS)
5194 else if (vec == ZFS_IOC_COMPAT_FAIL)
5198 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5201 if (cflag != ZFS_CMD_COMPAT_NONE) {
5202 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
5203 bzero(zc, sizeof(zfs_cmd_t));
5204 zfs_cmd_compat_get(zc, addr, cflag);
5205 zfs_ioctl_compat_pre(zc, &vec, cflag);
5210 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred);
5213 * Ensure that all pool/dataset names are valid before we pass down to
5217 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5218 zc->zc_iflags = flag & FKIOCTL;
5219 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5221 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5223 if (zfs_ioc_vec[vec].zvec_pool_check)
5224 error = pool_status_check(zc->zc_name,
5225 zfs_ioc_vec[vec].zvec_namecheck);
5229 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5231 if (zfs_ioc_vec[vec].zvec_pool_check)
5232 error = pool_status_check(zc->zc_name,
5233 zfs_ioc_vec[vec].zvec_namecheck);
5242 error = zfs_ioc_vec[vec].zvec_func(zc);
5245 if (zfs_ioc_vec[vec].zvec_his_log)
5246 zfs_log_history(zc);
5249 if (cflag != ZFS_CMD_COMPAT_NONE) {
5250 zfs_ioctl_compat_post(zc, ZFS_IOC(cmd), cflag);
5251 zfs_cmd_compat_put(zc, addr, cflag);
5252 kmem_free(zc, sizeof(zfs_cmd_t));
5260 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5262 if (cmd != DDI_ATTACH)
5263 return (DDI_FAILURE);
5265 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5266 DDI_PSEUDO, 0) == DDI_FAILURE)
5267 return (DDI_FAILURE);
5271 ddi_report_dev(dip);
5273 return (DDI_SUCCESS);
5277 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5279 if (spa_busy() || zfs_busy() || zvol_busy())
5280 return (DDI_FAILURE);
5282 if (cmd != DDI_DETACH)
5283 return (DDI_FAILURE);
5287 ddi_prop_remove_all(dip);
5288 ddi_remove_minor_node(dip, NULL);
5290 return (DDI_SUCCESS);
5295 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5298 case DDI_INFO_DEVT2DEVINFO:
5300 return (DDI_SUCCESS);
5302 case DDI_INFO_DEVT2INSTANCE:
5303 *result = (void *)0;
5304 return (DDI_SUCCESS);
5307 return (DDI_FAILURE);
5312 * OK, so this is a little weird.
5314 * /dev/zfs is the control node, i.e. minor 0.
5315 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5317 * /dev/zfs has basically nothing to do except serve up ioctls,
5318 * so most of the standard driver entry points are in zvol.c.
5321 static struct cb_ops zfs_cb_ops = {
5322 zfsdev_open, /* open */
5323 zfsdev_close, /* close */
5324 zvol_strategy, /* strategy */
5326 zvol_dump, /* dump */
5327 zvol_read, /* read */
5328 zvol_write, /* write */
5329 zfsdev_ioctl, /* ioctl */
5333 nochpoll, /* poll */
5334 ddi_prop_op, /* prop_op */
5335 NULL, /* streamtab */
5336 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
5337 CB_REV, /* version */
5338 nodev, /* async read */
5339 nodev, /* async write */
5342 static struct dev_ops zfs_dev_ops = {
5343 DEVO_REV, /* version */
5345 zfs_info, /* info */
5346 nulldev, /* identify */
5347 nulldev, /* probe */
5348 zfs_attach, /* attach */
5349 zfs_detach, /* detach */
5351 &zfs_cb_ops, /* driver operations */
5352 NULL, /* no bus operations */
5354 ddi_quiesce_not_needed, /* quiesce */
5357 static struct modldrv zfs_modldrv = {
5363 static struct modlinkage modlinkage = {
5365 (void *)&zfs_modlfs,
5366 (void *)&zfs_modldrv,
5371 static struct cdevsw zfs_cdevsw = {
5372 .d_version = D_VERSION,
5373 .d_open = zfsdev_open,
5374 .d_ioctl = zfsdev_ioctl,
5375 .d_name = ZFS_DEV_NAME
5381 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
5389 destroy_dev(zfsdev);
5392 static struct root_hold_token *zfs_root_token;
5393 struct proc *zfsproc;
5395 uint_t zfs_fsyncer_key;
5396 extern uint_t rrw_tsd_key;
5404 spa_init(FREAD | FWRITE);
5408 if ((error = mod_install(&modlinkage)) != 0) {
5415 tsd_create(&zfs_fsyncer_key, NULL);
5416 tsd_create(&rrw_tsd_key, NULL);
5418 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
5420 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5430 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
5433 if ((error = mod_remove(&modlinkage)) != 0)
5439 if (zfs_nfsshare_inited)
5440 (void) ddi_modclose(nfs_mod);
5441 if (zfs_smbshare_inited)
5442 (void) ddi_modclose(smbsrv_mod);
5443 if (zfs_nfsshare_inited || zfs_smbshare_inited)
5444 (void) ddi_modclose(sharefs_mod);
5446 tsd_destroy(&zfs_fsyncer_key);
5447 ldi_ident_release(zfs_li);
5449 mutex_destroy(&zfs_share_lock);
5455 _info(struct modinfo *modinfop)
5457 return (mod_info(&modlinkage, modinfop));
5462 zfs_modevent(module_t mod, int type, void *unused __unused)
5468 zfs_root_token = root_mount_hold("ZFS");
5470 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5472 spa_init(FREAD | FWRITE);
5476 tsd_create(&zfs_fsyncer_key, NULL);
5477 tsd_create(&rrw_tsd_key, NULL);
5479 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
5480 root_mount_rel(zfs_root_token);
5485 if (spa_busy() || zfs_busy() || zvol_busy() ||
5486 zio_injection_enabled) {
5496 tsd_destroy(&zfs_fsyncer_key);
5497 tsd_destroy(&rrw_tsd_key);
5499 mutex_destroy(&zfs_share_lock);
5508 static moduledata_t zfs_mod = {
5513 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
5514 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
5515 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);