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. All rights reserved.
25 * Portions Copyright 2011 Martin Matuska
26 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
29 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
33 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 * Copyright 2017 RackTop Systems.
35 * Copyright (c) 2017 Datto Inc.
41 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
42 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
44 * There are two ways that we handle ioctls: the legacy way where almost
45 * all of the logic is in the ioctl callback, and the new way where most
46 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
48 * Non-legacy ioctls should be registered by calling
49 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
50 * from userland by lzc_ioctl().
52 * The registration arguments are as follows:
55 * The name of the ioctl. This is used for history logging. If the
56 * ioctl returns successfully (the callback returns 0), and allow_log
57 * is true, then a history log entry will be recorded with the input &
58 * output nvlists. The log entry can be printed with "zpool history -i".
61 * The ioctl request number, which userland will pass to ioctl(2).
62 * The ioctl numbers can change from release to release, because
63 * the caller (libzfs) must be matched to the kernel.
65 * zfs_secpolicy_func_t *secpolicy
66 * This function will be called before the zfs_ioc_func_t, to
67 * determine if this operation is permitted. It should return EPERM
68 * on failure, and 0 on success. Checks include determining if the
69 * dataset is visible in this zone, and if the user has either all
70 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
71 * to do this operation on this dataset with "zfs allow".
73 * zfs_ioc_namecheck_t namecheck
74 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
75 * name, a dataset name, or nothing. If the name is not well-formed,
76 * the ioctl will fail and the callback will not be called.
77 * Therefore, the callback can assume that the name is well-formed
78 * (e.g. is null-terminated, doesn't have more than one '@' character,
79 * doesn't have invalid characters).
81 * zfs_ioc_poolcheck_t pool_check
82 * This specifies requirements on the pool state. If the pool does
83 * not meet them (is suspended or is readonly), the ioctl will fail
84 * and the callback will not be called. If any checks are specified
85 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
86 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
87 * POOL_CHECK_READONLY).
89 * boolean_t smush_outnvlist
90 * If smush_outnvlist is true, then the output is presumed to be a
91 * list of errors, and it will be "smushed" down to fit into the
92 * caller's buffer, by removing some entries and replacing them with a
93 * single "N_MORE_ERRORS" entry indicating how many were removed. See
94 * nvlist_smush() for details. If smush_outnvlist is false, and the
95 * outnvlist does not fit into the userland-provided buffer, then the
96 * ioctl will fail with ENOMEM.
98 * zfs_ioc_func_t *func
99 * The callback function that will perform the operation.
101 * The callback should return 0 on success, or an error number on
102 * failure. If the function fails, the userland ioctl will return -1,
103 * and errno will be set to the callback's return value. The callback
104 * will be called with the following arguments:
107 * The name of the pool or dataset to operate on, from
108 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
109 * expected type (pool, dataset, or none).
112 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
113 * NULL if no input nvlist was provided. Changes to this nvlist are
114 * ignored. If the input nvlist could not be deserialized, the
115 * ioctl will fail and the callback will not be called.
118 * The output nvlist, initially empty. The callback can fill it in,
119 * and it will be returned to userland by serializing it into
120 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
121 * fails (e.g. because the caller didn't supply a large enough
122 * buffer), then the overall ioctl will fail. See the
123 * 'smush_nvlist' argument above for additional behaviors.
125 * There are two typical uses of the output nvlist:
126 * - To return state, e.g. property values. In this case,
127 * smush_outnvlist should be false. If the buffer was not large
128 * enough, the caller will reallocate a larger buffer and try
131 * - To return multiple errors from an ioctl which makes on-disk
132 * changes. In this case, smush_outnvlist should be true.
133 * Ioctls which make on-disk modifications should generally not
134 * use the outnvl if they succeed, because the caller can not
135 * distinguish between the operation failing, and
136 * deserialization failing.
139 #include <sys/types.h>
140 #include <sys/param.h>
141 #include <sys/errno.h>
144 #include <sys/modctl.h>
145 #include <sys/open.h>
146 #include <sys/file.h>
147 #include <sys/kmem.h>
148 #include <sys/conf.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
158 #include <sys/priv_impl.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
168 #include <sys/sunddi.h>
169 #include <sys/sunldi.h>
170 #include <sys/policy.h>
171 #include <sys/zone.h>
172 #include <sys/nvpair.h>
173 #include <sys/pathname.h>
174 #include <sys/mount.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sharefs/share.h>
183 #include <sys/dmu_objset.h>
184 #include <sys/dmu_send.h>
185 #include <sys/dsl_destroy.h>
186 #include <sys/dsl_bookmark.h>
187 #include <sys/dsl_userhold.h>
188 #include <sys/zfeature.h>
190 #include <sys/zio_checksum.h>
192 #include "zfs_namecheck.h"
193 #include "zfs_prop.h"
194 #include "zfs_deleg.h"
195 #include "zfs_comutil.h"
200 extern struct modlfs zfs_modlfs
;
202 extern void zfs_init(void);
203 extern void zfs_fini(void);
205 ldi_ident_t zfs_li
= NULL
;
208 uint_t zfs_fsyncer_key
;
209 extern uint_t rrw_tsd_key
;
210 static uint_t zfs_allow_log_key
;
212 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t
*);
213 typedef int zfs_ioc_func_t(const char *, nvlist_t
*, nvlist_t
*);
214 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, nvlist_t
*, cred_t
*);
220 } zfs_ioc_namecheck_t
;
223 POOL_CHECK_NONE
= 1 << 0,
224 POOL_CHECK_SUSPENDED
= 1 << 1,
225 POOL_CHECK_READONLY
= 1 << 2,
226 } zfs_ioc_poolcheck_t
;
228 typedef struct zfs_ioc_vec
{
229 zfs_ioc_legacy_func_t
*zvec_legacy_func
;
230 zfs_ioc_func_t
*zvec_func
;
231 zfs_secpolicy_func_t
*zvec_secpolicy
;
232 zfs_ioc_namecheck_t zvec_namecheck
;
233 boolean_t zvec_allow_log
;
234 zfs_ioc_poolcheck_t zvec_pool_check
;
235 boolean_t zvec_smush_outnvlist
;
236 const char *zvec_name
;
239 /* This array is indexed by zfs_userquota_prop_t */
240 static const char *userquota_perms
[] = {
241 ZFS_DELEG_PERM_USERUSED
,
242 ZFS_DELEG_PERM_USERQUOTA
,
243 ZFS_DELEG_PERM_GROUPUSED
,
244 ZFS_DELEG_PERM_GROUPQUOTA
,
247 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
248 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
250 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
252 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
254 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
*);
255 static int get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
);
257 static int zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
);
259 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
261 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
268 * Get rid of annoying "../common/" prefix to filename.
270 newfile
= strrchr(file
, '/');
271 if (newfile
!= NULL
) {
272 newfile
= newfile
+ 1; /* Get rid of leading / */
278 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
282 * To get this data, use the zfs-dprintf probe as so:
283 * dtrace -q -n 'zfs-dprintf \
284 * /stringof(arg0) == "dbuf.c"/ \
285 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
287 * arg1 = function name
291 DTRACE_PROBE4(zfs__dprintf
,
292 char *, newfile
, char *, func
, int, line
, char *, buf
);
296 history_str_free(char *buf
)
298 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
302 history_str_get(zfs_cmd_t
*zc
)
306 if (zc
->zc_history
== NULL
)
309 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
310 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
311 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
312 history_str_free(buf
);
316 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
322 * Check to see if the named dataset is currently defined as bootable
325 zfs_is_bootfs(const char *name
)
329 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
331 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
332 dmu_objset_rele(os
, FTAG
);
339 * Return non-zero if the spa version is less than requested version.
342 zfs_earlier_version(const char *name
, int version
)
346 if (spa_open(name
, &spa
, FTAG
) == 0) {
347 if (spa_version(spa
) < version
) {
348 spa_close(spa
, FTAG
);
351 spa_close(spa
, FTAG
);
357 * Return TRUE if the ZPL version is less than requested version.
360 zpl_earlier_version(const char *name
, int version
)
363 boolean_t rc
= B_TRUE
;
365 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
368 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
369 dmu_objset_rele(os
, FTAG
);
372 /* XXX reading from non-owned objset */
373 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
374 rc
= zplversion
< version
;
375 dmu_objset_rele(os
, FTAG
);
381 zfs_log_history(zfs_cmd_t
*zc
)
386 if ((buf
= history_str_get(zc
)) == NULL
)
389 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
390 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
391 (void) spa_history_log(spa
, buf
);
392 spa_close(spa
, FTAG
);
394 history_str_free(buf
);
398 * Policy for top-level read operations (list pools). Requires no privileges,
399 * and can be used in the local zone, as there is no associated dataset.
403 zfs_secpolicy_none(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
409 * Policy for dataset read operations (list children, get statistics). Requires
410 * no privileges, but must be visible in the local zone.
414 zfs_secpolicy_read(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
416 if (INGLOBALZONE(curproc
) ||
417 zone_dataset_visible(zc
->zc_name
, NULL
))
420 return (SET_ERROR(ENOENT
));
424 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
429 * The dataset must be visible by this zone -- check this first
430 * so they don't see EPERM on something they shouldn't know about.
432 if (!INGLOBALZONE(curproc
) &&
433 !zone_dataset_visible(dataset
, &writable
))
434 return (SET_ERROR(ENOENT
));
436 if (INGLOBALZONE(curproc
)) {
438 * If the fs is zoned, only root can access it from the
441 if (secpolicy_zfs(cr
) && zoned
)
442 return (SET_ERROR(EPERM
));
445 * If we are in a local zone, the 'zoned' property must be set.
448 return (SET_ERROR(EPERM
));
450 /* must be writable by this zone */
452 return (SET_ERROR(EPERM
));
458 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
462 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
463 return (SET_ERROR(ENOENT
));
465 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
469 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
473 if (dsl_prop_get_int_ds(ds
, "zoned", &zoned
))
474 return (SET_ERROR(ENOENT
));
476 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
480 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
481 const char *perm
, cred_t
*cr
)
485 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
487 error
= secpolicy_zfs(cr
);
489 error
= dsl_deleg_access_impl(ds
, perm
, cr
);
495 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
502 * First do a quick check for root in the global zone, which
503 * is allowed to do all write_perms. This ensures that zfs_ioc_*
504 * will get to handle nonexistent datasets.
506 if (INGLOBALZONE(curproc
) && secpolicy_zfs(cr
) == 0)
509 error
= dsl_pool_hold(name
, FTAG
, &dp
);
513 error
= dsl_dataset_hold(dp
, name
, FTAG
, &ds
);
515 dsl_pool_rele(dp
, FTAG
);
519 error
= zfs_secpolicy_write_perms_ds(name
, ds
, perm
, cr
);
521 dsl_dataset_rele(ds
, FTAG
);
522 dsl_pool_rele(dp
, FTAG
);
527 * Policy for setting the security label property.
529 * Returns 0 for success, non-zero for access and other errors.
532 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
534 char ds_hexsl
[MAXNAMELEN
];
535 bslabel_t ds_sl
, new_sl
;
536 boolean_t new_default
= FALSE
;
538 int needed_priv
= -1;
541 /* First get the existing dataset label. */
542 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
543 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
545 return (SET_ERROR(EPERM
));
547 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
550 /* The label must be translatable */
551 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
552 return (SET_ERROR(EINVAL
));
555 * In a non-global zone, disallow attempts to set a label that
556 * doesn't match that of the zone; otherwise no other checks
559 if (!INGLOBALZONE(curproc
)) {
560 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
561 return (SET_ERROR(EPERM
));
566 * For global-zone datasets (i.e., those whose zoned property is
567 * "off", verify that the specified new label is valid for the
570 if (dsl_prop_get_integer(name
,
571 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
572 return (SET_ERROR(EPERM
));
574 if (zfs_check_global_label(name
, strval
) != 0)
575 return (SET_ERROR(EPERM
));
579 * If the existing dataset label is nondefault, check if the
580 * dataset is mounted (label cannot be changed while mounted).
581 * Get the zfsvfs; if there isn't one, then the dataset isn't
582 * mounted (or isn't a dataset, doesn't exist, ...).
584 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
586 static char *setsl_tag
= "setsl_tag";
589 * Try to own the dataset; abort if there is any error,
590 * (e.g., already mounted, in use, or other error).
592 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
595 return (SET_ERROR(EPERM
));
597 dmu_objset_disown(os
, setsl_tag
);
600 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
604 if (hexstr_to_label(strval
, &new_sl
) != 0)
605 return (SET_ERROR(EPERM
));
607 if (blstrictdom(&ds_sl
, &new_sl
))
608 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
609 else if (blstrictdom(&new_sl
, &ds_sl
))
610 needed_priv
= PRIV_FILE_UPGRADE_SL
;
612 /* dataset currently has a default label */
614 needed_priv
= PRIV_FILE_UPGRADE_SL
;
618 if (needed_priv
!= -1)
619 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
624 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
630 * Check permissions for special properties.
635 * Disallow setting of 'zoned' from within a local zone.
637 if (!INGLOBALZONE(curproc
))
638 return (SET_ERROR(EPERM
));
642 case ZFS_PROP_FILESYSTEM_LIMIT
:
643 case ZFS_PROP_SNAPSHOT_LIMIT
:
644 if (!INGLOBALZONE(curproc
)) {
646 char setpoint
[ZFS_MAX_DATASET_NAME_LEN
];
648 * Unprivileged users are allowed to modify the
649 * limit on things *under* (ie. contained by)
650 * the thing they own.
652 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
654 return (SET_ERROR(EPERM
));
655 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
656 return (SET_ERROR(EPERM
));
660 case ZFS_PROP_MLSLABEL
:
661 if (!is_system_labeled())
662 return (SET_ERROR(EPERM
));
664 if (nvpair_value_string(propval
, &strval
) == 0) {
667 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
674 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
679 zfs_secpolicy_set_fsacl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
683 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
688 * permission to set permissions will be evaluated later in
689 * dsl_deleg_can_allow()
696 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
698 return (zfs_secpolicy_write_perms(zc
->zc_name
,
699 ZFS_DELEG_PERM_ROLLBACK
, cr
));
704 zfs_secpolicy_send(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
712 * Generate the current snapshot name from the given objsetid, then
713 * use that name for the secpolicy/zone checks.
715 cp
= strchr(zc
->zc_name
, '@');
717 return (SET_ERROR(EINVAL
));
718 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
722 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
724 dsl_pool_rele(dp
, FTAG
);
728 dsl_dataset_name(ds
, zc
->zc_name
);
730 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
731 ZFS_DELEG_PERM_SEND
, cr
);
732 dsl_dataset_rele(ds
, FTAG
);
733 dsl_pool_rele(dp
, FTAG
);
740 zfs_secpolicy_send_new(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
742 return (zfs_secpolicy_write_perms(zc
->zc_name
,
743 ZFS_DELEG_PERM_SEND
, cr
));
748 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
753 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
754 NO_FOLLOW
, NULL
, &vp
)) != 0)
757 /* Now make sure mntpnt and dataset are ZFS */
759 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
760 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
761 zc
->zc_name
) != 0)) {
763 return (SET_ERROR(EPERM
));
767 return (dsl_deleg_access(zc
->zc_name
,
768 ZFS_DELEG_PERM_SHARE
, cr
));
772 zfs_secpolicy_share(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
774 if (!INGLOBALZONE(curproc
))
775 return (SET_ERROR(EPERM
));
777 if (secpolicy_nfs(cr
) == 0) {
780 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
785 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
787 if (!INGLOBALZONE(curproc
))
788 return (SET_ERROR(EPERM
));
790 if (secpolicy_smb(cr
) == 0) {
793 return (zfs_secpolicy_deleg_share(zc
, innvl
, cr
));
798 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
803 * Remove the @bla or /bla from the end of the name to get the parent.
805 (void) strncpy(parent
, datasetname
, parentsize
);
806 cp
= strrchr(parent
, '@');
810 cp
= strrchr(parent
, '/');
812 return (SET_ERROR(ENOENT
));
820 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
824 if ((error
= zfs_secpolicy_write_perms(name
,
825 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
828 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
833 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
835 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
839 * Destroying snapshots with delegated permissions requires
840 * descendant mount and destroy permissions.
844 zfs_secpolicy_destroy_snaps(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
847 nvpair_t
*pair
, *nextpair
;
850 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
851 return (SET_ERROR(EINVAL
));
852 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
854 nextpair
= nvlist_next_nvpair(snaps
, pair
);
855 error
= zfs_secpolicy_destroy_perms(nvpair_name(pair
), cr
);
856 if (error
== ENOENT
) {
858 * Ignore any snapshots that don't exist (we consider
859 * them "already destroyed"). Remove the name from the
860 * nvl here in case the snapshot is created between
861 * now and when we try to destroy it (in which case
862 * we don't want to destroy it since we haven't
863 * checked for permission).
865 fnvlist_remove_nvpair(snaps
, pair
);
876 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
878 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
881 if ((error
= zfs_secpolicy_write_perms(from
,
882 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
885 if ((error
= zfs_secpolicy_write_perms(from
,
886 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
889 if ((error
= zfs_get_parent(to
, parentname
,
890 sizeof (parentname
))) != 0)
893 if ((error
= zfs_secpolicy_write_perms(parentname
,
894 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
897 if ((error
= zfs_secpolicy_write_perms(parentname
,
898 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
906 zfs_secpolicy_rename(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
908 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
913 zfs_secpolicy_promote(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
916 dsl_dataset_t
*clone
;
919 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
920 ZFS_DELEG_PERM_PROMOTE
, cr
);
924 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
928 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &clone
);
931 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
932 dsl_dataset_t
*origin
= NULL
;
936 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
937 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &origin
);
939 dsl_dataset_rele(clone
, FTAG
);
940 dsl_pool_rele(dp
, FTAG
);
944 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, clone
,
945 ZFS_DELEG_PERM_MOUNT
, cr
);
947 dsl_dataset_name(origin
, parentname
);
949 error
= zfs_secpolicy_write_perms_ds(parentname
, origin
,
950 ZFS_DELEG_PERM_PROMOTE
, cr
);
952 dsl_dataset_rele(clone
, FTAG
);
953 dsl_dataset_rele(origin
, FTAG
);
955 dsl_pool_rele(dp
, FTAG
);
961 zfs_secpolicy_recv(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
965 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
966 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
969 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
970 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
973 return (zfs_secpolicy_write_perms(zc
->zc_name
,
974 ZFS_DELEG_PERM_CREATE
, cr
));
978 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
980 return (zfs_secpolicy_write_perms(name
,
981 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
985 * Check for permission to create each snapshot in the nvlist.
989 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
995 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
996 return (SET_ERROR(EINVAL
));
997 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
998 pair
= nvlist_next_nvpair(snaps
, pair
)) {
999 char *name
= nvpair_name(pair
);
1000 char *atp
= strchr(name
, '@');
1003 error
= SET_ERROR(EINVAL
);
1007 error
= zfs_secpolicy_snapshot_perms(name
, cr
);
1016 * Check for permission to create each snapshot in the nvlist.
1020 zfs_secpolicy_bookmark(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1024 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
1025 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
1026 char *name
= nvpair_name(pair
);
1027 char *hashp
= strchr(name
, '#');
1029 if (hashp
== NULL
) {
1030 error
= SET_ERROR(EINVAL
);
1034 error
= zfs_secpolicy_write_perms(name
,
1035 ZFS_DELEG_PERM_BOOKMARK
, cr
);
1045 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1047 nvpair_t
*pair
, *nextpair
;
1050 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1052 char *name
= nvpair_name(pair
);
1053 char *hashp
= strchr(name
, '#');
1054 nextpair
= nvlist_next_nvpair(innvl
, pair
);
1056 if (hashp
== NULL
) {
1057 error
= SET_ERROR(EINVAL
);
1062 error
= zfs_secpolicy_write_perms(name
,
1063 ZFS_DELEG_PERM_DESTROY
, cr
);
1065 if (error
== ENOENT
) {
1067 * Ignore any filesystems that don't exist (we consider
1068 * their bookmarks "already destroyed"). Remove
1069 * the name from the nvl here in case the filesystem
1070 * is created between now and when we try to destroy
1071 * the bookmark (in which case we don't want to
1072 * destroy it since we haven't checked for permission).
1074 fnvlist_remove_nvpair(innvl
, pair
);
1086 zfs_secpolicy_log_history(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1089 * Even root must have a proper TSD so that we know what pool
1092 if (tsd_get(zfs_allow_log_key
) == NULL
)
1093 return (SET_ERROR(EPERM
));
1098 zfs_secpolicy_create_clone(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1100 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
1104 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
1105 sizeof (parentname
))) != 0)
1108 if (nvlist_lookup_string(innvl
, "origin", &origin
) == 0 &&
1109 (error
= zfs_secpolicy_write_perms(origin
,
1110 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
1113 if ((error
= zfs_secpolicy_write_perms(parentname
,
1114 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
1117 return (zfs_secpolicy_write_perms(parentname
,
1118 ZFS_DELEG_PERM_MOUNT
, cr
));
1122 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1123 * SYS_CONFIG privilege, which is not available in a local zone.
1127 zfs_secpolicy_config(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1129 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
1130 return (SET_ERROR(EPERM
));
1136 * Policy for object to name lookups.
1140 zfs_secpolicy_diff(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1144 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
1147 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
1152 * Policy for fault injection. Requires all privileges.
1156 zfs_secpolicy_inject(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1158 return (secpolicy_zinject(cr
));
1163 zfs_secpolicy_inherit_prop(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1165 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
1167 if (prop
== ZPROP_INVAL
) {
1168 if (!zfs_prop_user(zc
->zc_value
))
1169 return (SET_ERROR(EINVAL
));
1170 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1171 ZFS_DELEG_PERM_USERPROP
, cr
));
1173 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
1179 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1181 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1185 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1186 return (SET_ERROR(EINVAL
));
1188 if (zc
->zc_value
[0] == 0) {
1190 * They are asking about a posix uid/gid. If it's
1191 * themself, allow it.
1193 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
1194 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
1195 if (zc
->zc_guid
== crgetuid(cr
))
1198 if (groupmember(zc
->zc_guid
, cr
))
1203 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1204 userquota_perms
[zc
->zc_objset_type
], cr
));
1208 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1210 int err
= zfs_secpolicy_read(zc
, innvl
, cr
);
1214 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
1215 return (SET_ERROR(EINVAL
));
1217 return (zfs_secpolicy_write_perms(zc
->zc_name
,
1218 userquota_perms
[zc
->zc_objset_type
], cr
));
1223 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1225 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
1231 zfs_secpolicy_hold(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1237 error
= nvlist_lookup_nvlist(innvl
, "holds", &holds
);
1239 return (SET_ERROR(EINVAL
));
1241 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
1242 pair
= nvlist_next_nvpair(holds
, pair
)) {
1243 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1244 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1247 error
= zfs_secpolicy_write_perms(fsname
,
1248 ZFS_DELEG_PERM_HOLD
, cr
);
1257 zfs_secpolicy_release(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1262 for (pair
= nvlist_next_nvpair(innvl
, NULL
); pair
!= NULL
;
1263 pair
= nvlist_next_nvpair(innvl
, pair
)) {
1264 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
1265 error
= dmu_fsname(nvpair_name(pair
), fsname
);
1268 error
= zfs_secpolicy_write_perms(fsname
,
1269 ZFS_DELEG_PERM_RELEASE
, cr
);
1277 * Policy for allowing temporary snapshots to be taken or released
1280 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, nvlist_t
*innvl
, cred_t
*cr
)
1283 * A temporary snapshot is the same as a snapshot,
1284 * hold, destroy and release all rolled into one.
1285 * Delegated diff alone is sufficient that we allow this.
1289 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
1290 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1293 error
= zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
);
1295 error
= zfs_secpolicy_hold(zc
, innvl
, cr
);
1297 error
= zfs_secpolicy_release(zc
, innvl
, cr
);
1299 error
= zfs_secpolicy_destroy(zc
, innvl
, cr
);
1304 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1307 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1311 nvlist_t
*list
= NULL
;
1314 * Read in and unpack the user-supplied nvlist.
1317 return (SET_ERROR(EINVAL
));
1319 packed
= kmem_alloc(size
, KM_SLEEP
);
1321 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1323 kmem_free(packed
, size
);
1324 return (SET_ERROR(EFAULT
));
1327 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1328 kmem_free(packed
, size
);
1332 kmem_free(packed
, size
);
1339 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1340 * Entries will be removed from the end of the nvlist, and one int32 entry
1341 * named "N_MORE_ERRORS" will be added indicating how many entries were
1345 nvlist_smush(nvlist_t
*errors
, size_t max
)
1349 size
= fnvlist_size(errors
);
1352 nvpair_t
*more_errors
;
1356 return (SET_ERROR(ENOMEM
));
1358 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, 0);
1359 more_errors
= nvlist_prev_nvpair(errors
, NULL
);
1362 nvpair_t
*pair
= nvlist_prev_nvpair(errors
,
1364 fnvlist_remove_nvpair(errors
, pair
);
1366 size
= fnvlist_size(errors
);
1367 } while (size
> max
);
1369 fnvlist_remove_nvpair(errors
, more_errors
);
1370 fnvlist_add_int32(errors
, ZPROP_N_MORE_ERRORS
, n
);
1371 ASSERT3U(fnvlist_size(errors
), <=, max
);
1378 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1380 char *packed
= NULL
;
1384 size
= fnvlist_size(nvl
);
1386 if (size
> zc
->zc_nvlist_dst_size
) {
1387 error
= SET_ERROR(ENOMEM
);
1389 packed
= fnvlist_pack(nvl
, &size
);
1390 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1391 size
, zc
->zc_iflags
) != 0)
1392 error
= SET_ERROR(EFAULT
);
1393 fnvlist_pack_free(packed
, size
);
1396 zc
->zc_nvlist_dst_size
= size
;
1397 zc
->zc_nvlist_dst_filled
= B_TRUE
;
1402 getzfsvfs_impl(objset_t
*os
, zfsvfs_t
**zfvp
)
1405 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1406 return (SET_ERROR(EINVAL
));
1409 mutex_enter(&os
->os_user_ptr_lock
);
1410 *zfvp
= dmu_objset_get_user(os
);
1412 VFS_HOLD((*zfvp
)->z_vfs
);
1414 error
= SET_ERROR(ESRCH
);
1416 mutex_exit(&os
->os_user_ptr_lock
);
1421 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1426 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1430 error
= getzfsvfs_impl(os
, zfvp
);
1431 dmu_objset_rele(os
, FTAG
);
1436 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1437 * case its z_vfs will be NULL, and it will be opened as the owner.
1438 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1439 * which prevents all vnode ops from running.
1442 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1446 if (getzfsvfs(name
, zfvp
) != 0)
1447 error
= zfsvfs_create(name
, zfvp
);
1449 rrm_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1451 if ((*zfvp
)->z_unmounted
) {
1453 * XXX we could probably try again, since the unmounting
1454 * thread should be just about to disassociate the
1455 * objset from the zfsvfs.
1457 rrm_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1458 return (SET_ERROR(EBUSY
));
1465 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1467 rrm_exit(&zfsvfs
->z_teardown_lock
, tag
);
1469 if (zfsvfs
->z_vfs
) {
1470 VFS_RELE(zfsvfs
->z_vfs
);
1472 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1473 zfsvfs_free(zfsvfs
);
1478 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1481 nvlist_t
*config
, *props
= NULL
;
1482 nvlist_t
*rootprops
= NULL
;
1483 nvlist_t
*zplprops
= NULL
;
1485 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1486 zc
->zc_iflags
, &config
))
1489 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1490 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1491 zc
->zc_iflags
, &props
))) {
1492 nvlist_free(config
);
1497 nvlist_t
*nvl
= NULL
;
1498 uint64_t version
= SPA_VERSION
;
1500 (void) nvlist_lookup_uint64(props
,
1501 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1502 if (!SPA_VERSION_IS_SUPPORTED(version
)) {
1503 error
= SET_ERROR(EINVAL
);
1504 goto pool_props_bad
;
1506 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1508 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1510 nvlist_free(config
);
1514 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1516 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1517 error
= zfs_fill_zplprops_root(version
, rootprops
,
1520 goto pool_props_bad
;
1523 error
= spa_create(zc
->zc_name
, config
, props
, zplprops
);
1526 * Set the remaining root properties
1528 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1529 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1530 (void) spa_destroy(zc
->zc_name
);
1533 nvlist_free(rootprops
);
1534 nvlist_free(zplprops
);
1535 nvlist_free(config
);
1542 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1545 zfs_log_history(zc
);
1546 error
= spa_destroy(zc
->zc_name
);
1548 zvol_remove_minors(zc
->zc_name
);
1553 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1555 nvlist_t
*config
, *props
= NULL
;
1559 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1560 zc
->zc_iflags
, &config
)) != 0)
1563 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1564 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1565 zc
->zc_iflags
, &props
))) {
1566 nvlist_free(config
);
1570 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1571 guid
!= zc
->zc_guid
)
1572 error
= SET_ERROR(EINVAL
);
1574 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1576 if (zc
->zc_nvlist_dst
!= 0) {
1579 if ((err
= put_nvlist(zc
, config
)) != 0)
1583 nvlist_free(config
);
1591 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1594 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1595 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1597 zfs_log_history(zc
);
1598 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1600 zvol_remove_minors(zc
->zc_name
);
1605 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1610 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1611 return (SET_ERROR(EEXIST
));
1613 error
= put_nvlist(zc
, configs
);
1615 nvlist_free(configs
);
1622 * zc_name name of the pool
1625 * zc_cookie real errno
1626 * zc_nvlist_dst config nvlist
1627 * zc_nvlist_dst_size size of config nvlist
1630 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1636 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1637 sizeof (zc
->zc_value
));
1639 if (config
!= NULL
) {
1640 ret
= put_nvlist(zc
, config
);
1641 nvlist_free(config
);
1644 * The config may be present even if 'error' is non-zero.
1645 * In this case we return success, and preserve the real errno
1648 zc
->zc_cookie
= error
;
1657 * Try to import the given pool, returning pool stats as appropriate so that
1658 * user land knows which devices are available and overall pool health.
1661 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1663 nvlist_t
*tryconfig
, *config
;
1666 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1667 zc
->zc_iflags
, &tryconfig
)) != 0)
1670 config
= spa_tryimport(tryconfig
);
1672 nvlist_free(tryconfig
);
1675 return (SET_ERROR(EINVAL
));
1677 error
= put_nvlist(zc
, config
);
1678 nvlist_free(config
);
1685 * zc_name name of the pool
1686 * zc_cookie scan func (pool_scan_func_t)
1687 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1690 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1695 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1698 if (zc
->zc_flags
>= POOL_SCRUB_FLAGS_END
)
1699 return (SET_ERROR(EINVAL
));
1701 if (zc
->zc_flags
== POOL_SCRUB_PAUSE
)
1702 error
= spa_scrub_pause_resume(spa
, POOL_SCRUB_PAUSE
);
1703 else if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1704 error
= spa_scan_stop(spa
);
1706 error
= spa_scan(spa
, zc
->zc_cookie
);
1708 spa_close(spa
, FTAG
);
1714 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1719 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1722 spa_close(spa
, FTAG
);
1728 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1733 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1736 if (zc
->zc_cookie
< spa_version(spa
) ||
1737 !SPA_VERSION_IS_SUPPORTED(zc
->zc_cookie
)) {
1738 spa_close(spa
, FTAG
);
1739 return (SET_ERROR(EINVAL
));
1742 spa_upgrade(spa
, zc
->zc_cookie
);
1743 spa_close(spa
, FTAG
);
1749 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1756 if ((size
= zc
->zc_history_len
) == 0)
1757 return (SET_ERROR(EINVAL
));
1759 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1762 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1763 spa_close(spa
, FTAG
);
1764 return (SET_ERROR(ENOTSUP
));
1767 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1768 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1769 &zc
->zc_history_len
, hist_buf
)) == 0) {
1770 error
= ddi_copyout(hist_buf
,
1771 (void *)(uintptr_t)zc
->zc_history
,
1772 zc
->zc_history_len
, zc
->zc_iflags
);
1775 spa_close(spa
, FTAG
);
1776 kmem_free(hist_buf
, size
);
1781 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1786 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1788 error
= spa_change_guid(spa
);
1789 spa_close(spa
, FTAG
);
1795 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1797 return (dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
));
1802 * zc_name name of filesystem
1803 * zc_obj object to find
1806 * zc_value name of object
1809 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1814 /* XXX reading from objset not owned */
1815 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1817 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1818 dmu_objset_rele(os
, FTAG
);
1819 return (SET_ERROR(EINVAL
));
1821 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1822 sizeof (zc
->zc_value
));
1823 dmu_objset_rele(os
, FTAG
);
1830 * zc_name name of filesystem
1831 * zc_obj object to find
1834 * zc_stat stats on object
1835 * zc_value path to object
1838 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1843 /* XXX reading from objset not owned */
1844 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1846 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1847 dmu_objset_rele(os
, FTAG
);
1848 return (SET_ERROR(EINVAL
));
1850 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1851 sizeof (zc
->zc_value
));
1852 dmu_objset_rele(os
, FTAG
);
1858 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1862 nvlist_t
*config
, **l2cache
, **spares
;
1863 uint_t nl2cache
= 0, nspares
= 0;
1865 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1869 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1870 zc
->zc_iflags
, &config
);
1871 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1872 &l2cache
, &nl2cache
);
1874 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1878 * A root pool with concatenated devices is not supported.
1879 * Thus, can not add a device to a root pool.
1881 * Intent log device can not be added to a rootpool because
1882 * during mountroot, zil is replayed, a seperated log device
1883 * can not be accessed during the mountroot time.
1885 * l2cache and spare devices are ok to be added to a rootpool.
1887 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1888 nvlist_free(config
);
1889 spa_close(spa
, FTAG
);
1890 return (SET_ERROR(EDOM
));
1894 error
= spa_vdev_add(spa
, config
);
1895 nvlist_free(config
);
1897 spa_close(spa
, FTAG
);
1903 * zc_name name of the pool
1904 * zc_nvlist_conf nvlist of devices to remove
1905 * zc_cookie to stop the remove?
1908 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1913 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1916 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1917 spa_close(spa
, FTAG
);
1922 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1926 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1928 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1930 switch (zc
->zc_cookie
) {
1931 case VDEV_STATE_ONLINE
:
1932 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1935 case VDEV_STATE_OFFLINE
:
1936 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1939 case VDEV_STATE_FAULTED
:
1940 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1941 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1942 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1944 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1947 case VDEV_STATE_DEGRADED
:
1948 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1949 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1950 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1952 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1956 error
= SET_ERROR(EINVAL
);
1958 zc
->zc_cookie
= newstate
;
1959 spa_close(spa
, FTAG
);
1964 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1967 int replacing
= zc
->zc_cookie
;
1971 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1974 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1975 zc
->zc_iflags
, &config
)) == 0) {
1976 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1977 nvlist_free(config
);
1980 spa_close(spa
, FTAG
);
1985 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1990 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1993 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1995 spa_close(spa
, FTAG
);
2000 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
2003 nvlist_t
*config
, *props
= NULL
;
2005 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
2007 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
2010 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
2011 zc
->zc_iflags
, &config
)) {
2012 spa_close(spa
, FTAG
);
2016 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
2017 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2018 zc
->zc_iflags
, &props
))) {
2019 spa_close(spa
, FTAG
);
2020 nvlist_free(config
);
2024 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
2026 spa_close(spa
, FTAG
);
2028 nvlist_free(config
);
2035 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
2038 char *path
= zc
->zc_value
;
2039 uint64_t guid
= zc
->zc_guid
;
2042 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2046 error
= spa_vdev_setpath(spa
, guid
, path
);
2047 spa_close(spa
, FTAG
);
2052 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
2055 char *fru
= zc
->zc_value
;
2056 uint64_t guid
= zc
->zc_guid
;
2059 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
2063 error
= spa_vdev_setfru(spa
, guid
, fru
);
2064 spa_close(spa
, FTAG
);
2069 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
2074 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2076 if (zc
->zc_nvlist_dst
!= 0 &&
2077 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
2078 dmu_objset_stats(os
, nv
);
2080 * NB: zvol_get_stats() will read the objset contents,
2081 * which we aren't supposed to do with a
2082 * DS_MODE_USER hold, because it could be
2083 * inconsistent. So this is a bit of a workaround...
2084 * XXX reading with out owning
2086 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
2087 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
2088 error
= zvol_get_stats(os
, nv
);
2093 error
= put_nvlist(zc
, nv
);
2102 * zc_name name of filesystem
2103 * zc_nvlist_dst_size size of buffer for property nvlist
2106 * zc_objset_stats stats
2107 * zc_nvlist_dst property nvlist
2108 * zc_nvlist_dst_size size of property nvlist
2111 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
2116 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2118 error
= zfs_ioc_objset_stats_impl(zc
, os
);
2119 dmu_objset_rele(os
, FTAG
);
2127 * zc_name name of filesystem
2128 * zc_nvlist_dst_size size of buffer for property nvlist
2131 * zc_nvlist_dst received property nvlist
2132 * zc_nvlist_dst_size size of received property nvlist
2134 * Gets received properties (distinct from local properties on or after
2135 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2136 * local property values.
2139 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
2145 * Without this check, we would return local property values if the
2146 * caller has not already received properties on or after
2147 * SPA_VERSION_RECVD_PROPS.
2149 if (!dsl_prop_get_hasrecvd(zc
->zc_name
))
2150 return (SET_ERROR(ENOTSUP
));
2152 if (zc
->zc_nvlist_dst
!= 0 &&
2153 (error
= dsl_prop_get_received(zc
->zc_name
, &nv
)) == 0) {
2154 error
= put_nvlist(zc
, nv
);
2162 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
2168 * zfs_get_zplprop() will either find a value or give us
2169 * the default value (if there is one).
2171 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
2173 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
2179 * zc_name name of filesystem
2180 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2183 * zc_nvlist_dst zpl property nvlist
2184 * zc_nvlist_dst_size size of zpl property nvlist
2187 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
2192 /* XXX reading without owning */
2193 if (err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
2196 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
2199 * NB: nvl_add_zplprop() will read the objset contents,
2200 * which we aren't supposed to do with a DS_MODE_USER
2201 * hold, because it could be inconsistent.
2203 if (zc
->zc_nvlist_dst
!= NULL
&&
2204 !zc
->zc_objset_stats
.dds_inconsistent
&&
2205 dmu_objset_type(os
) == DMU_OST_ZFS
) {
2208 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2209 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
2210 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
2211 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
2212 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
2213 err
= put_nvlist(zc
, nv
);
2216 err
= SET_ERROR(ENOENT
);
2218 dmu_objset_rele(os
, FTAG
);
2223 dataset_name_hidden(const char *name
)
2226 * Skip over datasets that are not visible in this zone,
2227 * internal datasets (which have a $ in their name), and
2228 * temporary datasets (which have a % in their name).
2230 if (strchr(name
, '$') != NULL
)
2232 if (strchr(name
, '%') != NULL
)
2234 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
2241 * zc_name name of filesystem
2242 * zc_cookie zap cursor
2243 * zc_nvlist_dst_size size of buffer for property nvlist
2246 * zc_name name of next filesystem
2247 * zc_cookie zap cursor
2248 * zc_objset_stats stats
2249 * zc_nvlist_dst property nvlist
2250 * zc_nvlist_dst_size size of property nvlist
2253 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
2258 size_t orig_len
= strlen(zc
->zc_name
);
2261 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) {
2262 if (error
== ENOENT
)
2263 error
= SET_ERROR(ESRCH
);
2267 p
= strrchr(zc
->zc_name
, '/');
2268 if (p
== NULL
|| p
[1] != '\0')
2269 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
2270 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
2273 error
= dmu_dir_list_next(os
,
2274 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
2275 NULL
, &zc
->zc_cookie
);
2276 if (error
== ENOENT
)
2277 error
= SET_ERROR(ESRCH
);
2278 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
2279 dmu_objset_rele(os
, FTAG
);
2282 * If it's an internal dataset (ie. with a '$' in its name),
2283 * don't try to get stats for it, otherwise we'll return ENOENT.
2285 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
2286 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2287 if (error
== ENOENT
) {
2288 /* We lost a race with destroy, get the next one. */
2289 zc
->zc_name
[orig_len
] = '\0';
2298 * zc_name name of filesystem
2299 * zc_cookie zap cursor
2300 * zc_nvlist_dst_size size of buffer for property nvlist
2301 * zc_simple when set, only name is requested
2304 * zc_name name of next snapshot
2305 * zc_objset_stats stats
2306 * zc_nvlist_dst property nvlist
2307 * zc_nvlist_dst_size size of property nvlist
2310 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2315 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2317 return (error
== ENOENT
? ESRCH
: error
);
2321 * A dataset name of maximum length cannot have any snapshots,
2322 * so exit immediately.
2324 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >=
2325 ZFS_MAX_DATASET_NAME_LEN
) {
2326 dmu_objset_rele(os
, FTAG
);
2327 return (SET_ERROR(ESRCH
));
2330 error
= dmu_snapshot_list_next(os
,
2331 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2332 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2335 if (error
== 0 && !zc
->zc_simple
) {
2337 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2339 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2343 error
= dmu_objset_from_ds(ds
, &ossnap
);
2345 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2346 dsl_dataset_rele(ds
, FTAG
);
2348 } else if (error
== ENOENT
) {
2349 error
= SET_ERROR(ESRCH
);
2352 dmu_objset_rele(os
, FTAG
);
2353 /* if we failed, undo the @ that we tacked on to zc_name */
2355 *strchr(zc
->zc_name
, '@') = '\0';
2360 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2362 const char *propname
= nvpair_name(pair
);
2364 unsigned int vallen
;
2367 zfs_userquota_prop_t type
;
2373 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2375 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2376 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2378 return (SET_ERROR(EINVAL
));
2382 * A correctly constructed propname is encoded as
2383 * userquota@<rid>-<domain>.
2385 if ((dash
= strchr(propname
, '-')) == NULL
||
2386 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2388 return (SET_ERROR(EINVAL
));
2395 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2397 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2398 zfsvfs_rele(zfsvfs
, FTAG
);
2405 * If the named property is one that has a special function to set its value,
2406 * return 0 on success and a positive error code on failure; otherwise if it is
2407 * not one of the special properties handled by this function, return -1.
2409 * XXX: It would be better for callers of the property interface if we handled
2410 * these special cases in dsl_prop.c (in the dsl layer).
2413 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2416 const char *propname
= nvpair_name(pair
);
2417 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2421 if (prop
== ZPROP_INVAL
) {
2422 if (zfs_prop_userquota(propname
))
2423 return (zfs_prop_set_userquota(dsname
, pair
));
2427 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2429 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2430 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2434 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2437 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2440 case ZFS_PROP_QUOTA
:
2441 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2443 case ZFS_PROP_REFQUOTA
:
2444 err
= dsl_dataset_set_refquota(dsname
, source
, intval
);
2446 case ZFS_PROP_FILESYSTEM_LIMIT
:
2447 case ZFS_PROP_SNAPSHOT_LIMIT
:
2448 if (intval
== UINT64_MAX
) {
2449 /* clearing the limit, just do it */
2452 err
= dsl_dir_activate_fs_ss_limit(dsname
);
2455 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2456 * default path to set the value in the nvlist.
2461 case ZFS_PROP_RESERVATION
:
2462 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2464 case ZFS_PROP_REFRESERVATION
:
2465 err
= dsl_dataset_set_refreservation(dsname
, source
, intval
);
2467 case ZFS_PROP_VOLSIZE
:
2468 err
= zvol_set_volsize(dsname
, intval
);
2470 case ZFS_PROP_VERSION
:
2474 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2477 err
= zfs_set_version(zfsvfs
, intval
);
2478 zfsvfs_rele(zfsvfs
, FTAG
);
2480 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2483 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2484 (void) strcpy(zc
->zc_name
, dsname
);
2485 (void) zfs_ioc_userspace_upgrade(zc
);
2486 kmem_free(zc
, sizeof (zfs_cmd_t
));
2498 * This function is best effort. If it fails to set any of the given properties,
2499 * it continues to set as many as it can and returns the last error
2500 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2501 * with the list of names of all the properties that failed along with the
2502 * corresponding error numbers.
2504 * If every property is set successfully, zero is returned and errlist is not
2508 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2516 nvlist_t
*genericnvl
= fnvlist_alloc();
2517 nvlist_t
*retrynvl
= fnvlist_alloc();
2521 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2522 const char *propname
= nvpair_name(pair
);
2523 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2526 /* decode the property value */
2528 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2530 attrs
= fnvpair_value_nvlist(pair
);
2531 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2533 err
= SET_ERROR(EINVAL
);
2536 /* Validate value type */
2537 if (err
== 0 && prop
== ZPROP_INVAL
) {
2538 if (zfs_prop_user(propname
)) {
2539 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2540 err
= SET_ERROR(EINVAL
);
2541 } else if (zfs_prop_userquota(propname
)) {
2542 if (nvpair_type(propval
) !=
2543 DATA_TYPE_UINT64_ARRAY
)
2544 err
= SET_ERROR(EINVAL
);
2546 err
= SET_ERROR(EINVAL
);
2548 } else if (err
== 0) {
2549 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2550 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2551 err
= SET_ERROR(EINVAL
);
2552 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2555 intval
= fnvpair_value_uint64(propval
);
2557 switch (zfs_prop_get_type(prop
)) {
2558 case PROP_TYPE_NUMBER
:
2560 case PROP_TYPE_STRING
:
2561 err
= SET_ERROR(EINVAL
);
2563 case PROP_TYPE_INDEX
:
2564 if (zfs_prop_index_to_string(prop
,
2565 intval
, &unused
) != 0)
2566 err
= SET_ERROR(EINVAL
);
2570 "unknown property type");
2573 err
= SET_ERROR(EINVAL
);
2577 /* Validate permissions */
2579 err
= zfs_check_settable(dsname
, pair
, CRED());
2582 err
= zfs_prop_set_special(dsname
, source
, pair
);
2585 * For better performance we build up a list of
2586 * properties to set in a single transaction.
2588 err
= nvlist_add_nvpair(genericnvl
, pair
);
2589 } else if (err
!= 0 && nvl
!= retrynvl
) {
2591 * This may be a spurious error caused by
2592 * receiving quota and reservation out of order.
2593 * Try again in a second pass.
2595 err
= nvlist_add_nvpair(retrynvl
, pair
);
2600 if (errlist
!= NULL
)
2601 fnvlist_add_int32(errlist
, propname
, err
);
2606 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2611 if (!nvlist_empty(genericnvl
) &&
2612 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2614 * If this fails, we still want to set as many properties as we
2615 * can, so try setting them individually.
2618 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2619 const char *propname
= nvpair_name(pair
);
2623 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2625 attrs
= fnvpair_value_nvlist(pair
);
2626 propval
= fnvlist_lookup_nvpair(attrs
,
2630 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2631 strval
= fnvpair_value_string(propval
);
2632 err
= dsl_prop_set_string(dsname
, propname
,
2635 intval
= fnvpair_value_uint64(propval
);
2636 err
= dsl_prop_set_int(dsname
, propname
, source
,
2641 if (errlist
!= NULL
) {
2642 fnvlist_add_int32(errlist
, propname
,
2649 nvlist_free(genericnvl
);
2650 nvlist_free(retrynvl
);
2656 * Check that all the properties are valid user properties.
2659 zfs_check_userprops(const char *fsname
, nvlist_t
*nvl
)
2661 nvpair_t
*pair
= NULL
;
2664 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2665 const char *propname
= nvpair_name(pair
);
2667 if (!zfs_prop_user(propname
) ||
2668 nvpair_type(pair
) != DATA_TYPE_STRING
)
2669 return (SET_ERROR(EINVAL
));
2671 if (error
= zfs_secpolicy_write_perms(fsname
,
2672 ZFS_DELEG_PERM_USERPROP
, CRED()))
2675 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2676 return (SET_ERROR(ENAMETOOLONG
));
2678 if (strlen(fnvpair_value_string(pair
)) >= ZAP_MAXVALUELEN
)
2685 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2689 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2692 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2693 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2696 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2701 clear_received_props(const char *dsname
, nvlist_t
*props
,
2705 nvlist_t
*cleared_props
= NULL
;
2706 props_skip(props
, skipped
, &cleared_props
);
2707 if (!nvlist_empty(cleared_props
)) {
2709 * Acts on local properties until the dataset has received
2710 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2712 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2713 (dsl_prop_get_hasrecvd(dsname
) ? ZPROP_SRC_RECEIVED
: 0));
2714 err
= zfs_set_prop_nvlist(dsname
, flags
, cleared_props
, NULL
);
2716 nvlist_free(cleared_props
);
2722 * zc_name name of filesystem
2723 * zc_value name of property to set
2724 * zc_nvlist_src{_size} nvlist of properties to apply
2725 * zc_cookie received properties flag
2728 * zc_nvlist_dst{_size} error for each unapplied received property
2731 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2734 boolean_t received
= zc
->zc_cookie
;
2735 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2740 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2741 zc
->zc_iflags
, &nvl
)) != 0)
2745 nvlist_t
*origprops
;
2747 if (dsl_prop_get_received(zc
->zc_name
, &origprops
) == 0) {
2748 (void) clear_received_props(zc
->zc_name
,
2750 nvlist_free(origprops
);
2753 error
= dsl_prop_set_hasrecvd(zc
->zc_name
);
2756 errors
= fnvlist_alloc();
2758 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, errors
);
2760 if (zc
->zc_nvlist_dst
!= NULL
&& errors
!= NULL
) {
2761 (void) put_nvlist(zc
, errors
);
2764 nvlist_free(errors
);
2771 * zc_name name of filesystem
2772 * zc_value name of property to inherit
2773 * zc_cookie revert to received value if TRUE
2778 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2780 const char *propname
= zc
->zc_value
;
2781 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2782 boolean_t received
= zc
->zc_cookie
;
2783 zprop_source_t source
= (received
2784 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2785 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2794 * zfs_prop_set_special() expects properties in the form of an
2795 * nvpair with type info.
2797 if (prop
== ZPROP_INVAL
) {
2798 if (!zfs_prop_user(propname
))
2799 return (SET_ERROR(EINVAL
));
2801 type
= PROP_TYPE_STRING
;
2802 } else if (prop
== ZFS_PROP_VOLSIZE
||
2803 prop
== ZFS_PROP_VERSION
) {
2804 return (SET_ERROR(EINVAL
));
2806 type
= zfs_prop_get_type(prop
);
2809 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2812 case PROP_TYPE_STRING
:
2813 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2815 case PROP_TYPE_NUMBER
:
2816 case PROP_TYPE_INDEX
:
2817 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2821 return (SET_ERROR(EINVAL
));
2824 pair
= nvlist_next_nvpair(dummy
, NULL
);
2825 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2828 return (err
); /* special property already handled */
2831 * Only check this in the non-received case. We want to allow
2832 * 'inherit -S' to revert non-inheritable properties like quota
2833 * and reservation to the received or default values even though
2834 * they are not considered inheritable.
2836 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2837 return (SET_ERROR(EINVAL
));
2840 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2841 return (dsl_prop_inherit(zc
->zc_name
, zc
->zc_value
, source
));
2845 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2852 if (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2853 zc
->zc_iflags
, &props
))
2857 * If the only property is the configfile, then just do a spa_lookup()
2858 * to handle the faulted case.
2860 pair
= nvlist_next_nvpair(props
, NULL
);
2861 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2862 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2863 nvlist_next_nvpair(props
, pair
) == NULL
) {
2864 mutex_enter(&spa_namespace_lock
);
2865 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2866 spa_configfile_set(spa
, props
, B_FALSE
);
2867 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2869 mutex_exit(&spa_namespace_lock
);
2876 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2881 error
= spa_prop_set(spa
, props
);
2884 spa_close(spa
, FTAG
);
2890 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2894 nvlist_t
*nvp
= NULL
;
2896 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2898 * If the pool is faulted, there may be properties we can still
2899 * get (such as altroot and cachefile), so attempt to get them
2902 mutex_enter(&spa_namespace_lock
);
2903 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2904 error
= spa_prop_get(spa
, &nvp
);
2905 mutex_exit(&spa_namespace_lock
);
2907 error
= spa_prop_get(spa
, &nvp
);
2908 spa_close(spa
, FTAG
);
2911 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
2912 error
= put_nvlist(zc
, nvp
);
2914 error
= SET_ERROR(EFAULT
);
2922 * zc_name name of filesystem
2923 * zc_nvlist_src{_size} nvlist of delegated permissions
2924 * zc_perm_action allow/unallow flag
2929 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2932 nvlist_t
*fsaclnv
= NULL
;
2934 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2935 zc
->zc_iflags
, &fsaclnv
)) != 0)
2939 * Verify nvlist is constructed correctly
2941 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2942 nvlist_free(fsaclnv
);
2943 return (SET_ERROR(EINVAL
));
2947 * If we don't have PRIV_SYS_MOUNT, then validate
2948 * that user is allowed to hand out each permission in
2952 error
= secpolicy_zfs(CRED());
2954 if (zc
->zc_perm_action
== B_FALSE
) {
2955 error
= dsl_deleg_can_allow(zc
->zc_name
,
2958 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2964 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2966 nvlist_free(fsaclnv
);
2972 * zc_name name of filesystem
2975 * zc_nvlist_src{_size} nvlist of delegated permissions
2978 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2983 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2984 error
= put_nvlist(zc
, nvp
);
2993 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2995 zfs_creat_t
*zct
= arg
;
2997 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
3000 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3004 * os parent objset pointer (NULL if root fs)
3005 * fuids_ok fuids allowed in this version of the spa?
3006 * sa_ok SAs allowed in this version of the spa?
3007 * createprops list of properties requested by creator
3010 * zplprops values for the zplprops we attach to the master node object
3011 * is_ci true if requested file system will be purely case-insensitive
3013 * Determine the settings for utf8only, normalization and
3014 * casesensitivity. Specific values may have been requested by the
3015 * creator and/or we can inherit values from the parent dataset. If
3016 * the file system is of too early a vintage, a creator can not
3017 * request settings for these properties, even if the requested
3018 * setting is the default value. We don't actually want to create dsl
3019 * properties for these, so remove them from the source nvlist after
3023 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
3024 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
3025 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3027 uint64_t sense
= ZFS_PROP_UNDEFINED
;
3028 uint64_t norm
= ZFS_PROP_UNDEFINED
;
3029 uint64_t u8
= ZFS_PROP_UNDEFINED
;
3031 ASSERT(zplprops
!= NULL
);
3033 if (os
!= NULL
&& os
->os_phys
->os_type
!= DMU_OST_ZFS
)
3034 return (SET_ERROR(EINVAL
));
3037 * Pull out creator prop choices, if any.
3040 (void) nvlist_lookup_uint64(createprops
,
3041 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
3042 (void) nvlist_lookup_uint64(createprops
,
3043 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
3044 (void) nvlist_remove_all(createprops
,
3045 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
3046 (void) nvlist_lookup_uint64(createprops
,
3047 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
3048 (void) nvlist_remove_all(createprops
,
3049 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
3050 (void) nvlist_lookup_uint64(createprops
,
3051 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
3052 (void) nvlist_remove_all(createprops
,
3053 zfs_prop_to_name(ZFS_PROP_CASE
));
3057 * If the zpl version requested is whacky or the file system
3058 * or pool is version is too "young" to support normalization
3059 * and the creator tried to set a value for one of the props,
3062 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
3063 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
3064 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
3065 (zplver
< ZPL_VERSION_NORMALIZATION
&&
3066 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
3067 sense
!= ZFS_PROP_UNDEFINED
)))
3068 return (SET_ERROR(ENOTSUP
));
3071 * Put the version in the zplprops
3073 VERIFY(nvlist_add_uint64(zplprops
,
3074 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
3076 if (norm
== ZFS_PROP_UNDEFINED
)
3077 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
3078 VERIFY(nvlist_add_uint64(zplprops
,
3079 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
3082 * If we're normalizing, names must always be valid UTF-8 strings.
3086 if (u8
== ZFS_PROP_UNDEFINED
)
3087 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
3088 VERIFY(nvlist_add_uint64(zplprops
,
3089 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
3091 if (sense
== ZFS_PROP_UNDEFINED
)
3092 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
3093 VERIFY(nvlist_add_uint64(zplprops
,
3094 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
3097 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
3103 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
3104 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3106 boolean_t fuids_ok
, sa_ok
;
3107 uint64_t zplver
= ZPL_VERSION
;
3108 objset_t
*os
= NULL
;
3109 char parentname
[ZFS_MAX_DATASET_NAME_LEN
];
3115 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
3116 cp
= strrchr(parentname
, '/');
3120 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
3123 spa_vers
= spa_version(spa
);
3124 spa_close(spa
, FTAG
);
3126 zplver
= zfs_zpl_version_map(spa_vers
);
3127 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3128 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3131 * Open parent object set so we can inherit zplprop values.
3133 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
3136 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
3138 dmu_objset_rele(os
, FTAG
);
3143 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
3144 nvlist_t
*zplprops
, boolean_t
*is_ci
)
3148 uint64_t zplver
= ZPL_VERSION
;
3151 zplver
= zfs_zpl_version_map(spa_vers
);
3152 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
3153 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
3155 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
3156 createprops
, zplprops
, is_ci
);
3162 * "type" -> dmu_objset_type_t (int32)
3163 * (optional) "props" -> { prop -> value }
3166 * outnvl: propname -> error code (int32)
3169 zfs_ioc_create(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3172 zfs_creat_t zct
= { 0 };
3173 nvlist_t
*nvprops
= NULL
;
3174 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
3176 dmu_objset_type_t type
;
3177 boolean_t is_insensitive
= B_FALSE
;
3179 if (nvlist_lookup_int32(innvl
, "type", &type32
) != 0)
3180 return (SET_ERROR(EINVAL
));
3182 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3186 cbfunc
= zfs_create_cb
;
3190 cbfunc
= zvol_create_cb
;
3197 if (strchr(fsname
, '@') ||
3198 strchr(fsname
, '%'))
3199 return (SET_ERROR(EINVAL
));
3201 zct
.zct_props
= nvprops
;
3204 return (SET_ERROR(EINVAL
));
3206 if (type
== DMU_OST_ZVOL
) {
3207 uint64_t volsize
, volblocksize
;
3209 if (nvprops
== NULL
)
3210 return (SET_ERROR(EINVAL
));
3211 if (nvlist_lookup_uint64(nvprops
,
3212 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &volsize
) != 0)
3213 return (SET_ERROR(EINVAL
));
3215 if ((error
= nvlist_lookup_uint64(nvprops
,
3216 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3217 &volblocksize
)) != 0 && error
!= ENOENT
)
3218 return (SET_ERROR(EINVAL
));
3221 volblocksize
= zfs_prop_default_numeric(
3222 ZFS_PROP_VOLBLOCKSIZE
);
3224 if ((error
= zvol_check_volblocksize(
3225 volblocksize
)) != 0 ||
3226 (error
= zvol_check_volsize(volsize
,
3227 volblocksize
)) != 0)
3229 } else if (type
== DMU_OST_ZFS
) {
3233 * We have to have normalization and
3234 * case-folding flags correct when we do the
3235 * file system creation, so go figure them out
3238 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3239 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3240 error
= zfs_fill_zplprops(fsname
, nvprops
,
3241 zct
.zct_zplprops
, &is_insensitive
);
3243 nvlist_free(zct
.zct_zplprops
);
3248 error
= dmu_objset_create(fsname
, type
,
3249 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3250 nvlist_free(zct
.zct_zplprops
);
3253 * It would be nice to do this atomically.
3256 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3259 (void) dsl_destroy_head(fsname
);
3266 * "origin" -> name of origin snapshot
3267 * (optional) "props" -> { prop -> value }
3270 * outnvl: propname -> error code (int32)
3273 zfs_ioc_clone(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3276 nvlist_t
*nvprops
= NULL
;
3279 if (nvlist_lookup_string(innvl
, "origin", &origin_name
) != 0)
3280 return (SET_ERROR(EINVAL
));
3281 (void) nvlist_lookup_nvlist(innvl
, "props", &nvprops
);
3283 if (strchr(fsname
, '@') ||
3284 strchr(fsname
, '%'))
3285 return (SET_ERROR(EINVAL
));
3287 if (dataset_namecheck(origin_name
, NULL
, NULL
) != 0)
3288 return (SET_ERROR(EINVAL
));
3289 error
= dmu_objset_clone(fsname
, origin_name
);
3294 * It would be nice to do this atomically.
3297 error
= zfs_set_prop_nvlist(fsname
, ZPROP_SRC_LOCAL
,
3300 (void) dsl_destroy_head(fsname
);
3307 * "snaps" -> { snapshot1, snapshot2 }
3308 * (optional) "props" -> { prop -> value (string) }
3311 * outnvl: snapshot -> error code (int32)
3314 zfs_ioc_snapshot(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3317 nvlist_t
*props
= NULL
;
3321 (void) nvlist_lookup_nvlist(innvl
, "props", &props
);
3322 if ((error
= zfs_check_userprops(poolname
, props
)) != 0)
3325 if (!nvlist_empty(props
) &&
3326 zfs_earlier_version(poolname
, SPA_VERSION_SNAP_PROPS
))
3327 return (SET_ERROR(ENOTSUP
));
3329 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3330 return (SET_ERROR(EINVAL
));
3331 poollen
= strlen(poolname
);
3332 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3333 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3334 const char *name
= nvpair_name(pair
);
3335 const char *cp
= strchr(name
, '@');
3338 * The snap name must contain an @, and the part after it must
3339 * contain only valid characters.
3342 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3343 return (SET_ERROR(EINVAL
));
3346 * The snap must be in the specified pool.
3348 if (strncmp(name
, poolname
, poollen
) != 0 ||
3349 (name
[poollen
] != '/' && name
[poollen
] != '@'))
3350 return (SET_ERROR(EXDEV
));
3352 /* This must be the only snap of this fs. */
3353 for (nvpair_t
*pair2
= nvlist_next_nvpair(snaps
, pair
);
3354 pair2
!= NULL
; pair2
= nvlist_next_nvpair(snaps
, pair2
)) {
3355 if (strncmp(name
, nvpair_name(pair2
), cp
- name
+ 1)
3357 return (SET_ERROR(EXDEV
));
3362 error
= dsl_dataset_snapshot(snaps
, props
, outnvl
);
3367 * innvl: "message" -> string
3371 zfs_ioc_log_history(const char *unused
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3379 * The poolname in the ioctl is not set, we get it from the TSD,
3380 * which was set at the end of the last successful ioctl that allows
3381 * logging. The secpolicy func already checked that it is set.
3382 * Only one log ioctl is allowed after each successful ioctl, so
3383 * we clear the TSD here.
3385 poolname
= tsd_get(zfs_allow_log_key
);
3386 (void) tsd_set(zfs_allow_log_key
, NULL
);
3387 error
= spa_open(poolname
, &spa
, FTAG
);
3392 if (nvlist_lookup_string(innvl
, "message", &message
) != 0) {
3393 spa_close(spa
, FTAG
);
3394 return (SET_ERROR(EINVAL
));
3397 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
3398 spa_close(spa
, FTAG
);
3399 return (SET_ERROR(ENOTSUP
));
3402 error
= spa_history_log(spa
, message
);
3403 spa_close(spa
, FTAG
);
3408 * The dp_config_rwlock must not be held when calling this, because the
3409 * unmount may need to write out data.
3411 * This function is best-effort. Callers must deal gracefully if it
3412 * remains mounted (or is remounted after this call).
3414 * Returns 0 if the argument is not a snapshot, or it is not currently a
3415 * filesystem, or we were able to unmount it. Returns error code otherwise.
3418 zfs_unmount_snap(const char *snapname
)
3421 zfsvfs_t
*zfsvfs
= NULL
;
3423 if (strchr(snapname
, '@') == NULL
)
3426 int err
= getzfsvfs(snapname
, &zfsvfs
);
3428 ASSERT3P(zfsvfs
, ==, NULL
);
3431 vfsp
= zfsvfs
->z_vfs
;
3433 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs
->z_os
)));
3435 err
= vn_vfswlock(vfsp
->vfs_vnodecovered
);
3441 * Always force the unmount for snapshots.
3443 (void) dounmount(vfsp
, MS_FORCE
, kcred
);
3448 zfs_unmount_snap_cb(const char *snapname
, void *arg
)
3450 zfs_unmount_snap(snapname
);
3455 * When a clone is destroyed, its origin may also need to be destroyed,
3456 * in which case it must be unmounted. This routine will do that unmount
3460 zfs_destroy_unmount_origin(const char *fsname
)
3466 error
= dmu_objset_hold(fsname
, FTAG
, &os
);
3469 ds
= dmu_objset_ds(os
);
3470 if (dsl_dir_is_clone(ds
->ds_dir
) && DS_IS_DEFER_DESTROY(ds
->ds_prev
)) {
3471 char originname
[ZFS_MAX_DATASET_NAME_LEN
];
3472 dsl_dataset_name(ds
->ds_prev
, originname
);
3473 dmu_objset_rele(os
, FTAG
);
3474 zfs_unmount_snap(originname
);
3476 dmu_objset_rele(os
, FTAG
);
3482 * "snaps" -> { snapshot1, snapshot2 }
3483 * (optional boolean) "defer"
3486 * outnvl: snapshot -> error code (int32)
3491 zfs_ioc_destroy_snaps(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3497 if (nvlist_lookup_nvlist(innvl
, "snaps", &snaps
) != 0)
3498 return (SET_ERROR(EINVAL
));
3499 defer
= nvlist_exists(innvl
, "defer");
3501 for (pair
= nvlist_next_nvpair(snaps
, NULL
); pair
!= NULL
;
3502 pair
= nvlist_next_nvpair(snaps
, pair
)) {
3503 zfs_unmount_snap(nvpair_name(pair
));
3506 return (dsl_destroy_snapshots_nvl(snaps
, defer
, outnvl
));
3510 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3511 * All bookmarks must be in the same pool.
3514 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3517 * outnvl: bookmark -> error code (int32)
3522 zfs_ioc_bookmark(const char *poolname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3524 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3525 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3529 * Verify the snapshot argument.
3531 if (nvpair_value_string(pair
, &snap_name
) != 0)
3532 return (SET_ERROR(EINVAL
));
3535 /* Verify that the keys (bookmarks) are unique */
3536 for (nvpair_t
*pair2
= nvlist_next_nvpair(innvl
, pair
);
3537 pair2
!= NULL
; pair2
= nvlist_next_nvpair(innvl
, pair2
)) {
3538 if (strcmp(nvpair_name(pair
), nvpair_name(pair2
)) == 0)
3539 return (SET_ERROR(EINVAL
));
3543 return (dsl_bookmark_create(innvl
, outnvl
));
3548 * property 1, property 2, ...
3552 * bookmark name 1 -> { property 1, property 2, ... },
3553 * bookmark name 2 -> { property 1, property 2, ... }
3558 zfs_ioc_get_bookmarks(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3560 return (dsl_get_bookmarks(fsname
, innvl
, outnvl
));
3565 * bookmark name 1, bookmark name 2
3568 * outnvl: bookmark -> error code (int32)
3572 zfs_ioc_destroy_bookmarks(const char *poolname
, nvlist_t
*innvl
,
3577 poollen
= strlen(poolname
);
3578 for (nvpair_t
*pair
= nvlist_next_nvpair(innvl
, NULL
);
3579 pair
!= NULL
; pair
= nvlist_next_nvpair(innvl
, pair
)) {
3580 const char *name
= nvpair_name(pair
);
3581 const char *cp
= strchr(name
, '#');
3584 * The bookmark name must contain an #, and the part after it
3585 * must contain only valid characters.
3588 zfs_component_namecheck(cp
+ 1, NULL
, NULL
) != 0)
3589 return (SET_ERROR(EINVAL
));
3592 * The bookmark must be in the specified pool.
3594 if (strncmp(name
, poolname
, poollen
) != 0 ||
3595 (name
[poollen
] != '/' && name
[poollen
] != '#'))
3596 return (SET_ERROR(EXDEV
));
3599 error
= dsl_bookmark_destroy(innvl
, outnvl
);
3604 zfs_ioc_channel_program(const char *poolname
, nvlist_t
*innvl
,
3608 uint64_t instrlimit
, memlimit
;
3609 nvpair_t
*nvarg
= NULL
;
3611 if (0 != nvlist_lookup_string(innvl
, ZCP_ARG_PROGRAM
, &program
)) {
3614 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_INSTRLIMIT
, &instrlimit
)) {
3615 instrlimit
= ZCP_DEFAULT_INSTRLIMIT
;
3617 if (0 != nvlist_lookup_uint64(innvl
, ZCP_ARG_MEMLIMIT
, &memlimit
)) {
3618 memlimit
= ZCP_DEFAULT_MEMLIMIT
;
3620 if (0 != nvlist_lookup_nvpair(innvl
, ZCP_ARG_ARGLIST
, &nvarg
)) {
3624 if (instrlimit
== 0 || instrlimit
> zfs_lua_max_instrlimit
)
3626 if (memlimit
== 0 || memlimit
> zfs_lua_max_memlimit
)
3629 return (zcp_eval(poolname
, program
, instrlimit
, memlimit
,
3635 * zc_name name of dataset to destroy
3636 * zc_objset_type type of objset
3637 * zc_defer_destroy mark for deferred destroy
3642 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3646 if (zc
->zc_objset_type
== DMU_OST_ZFS
)
3647 zfs_unmount_snap(zc
->zc_name
);
3649 if (strchr(zc
->zc_name
, '@'))
3650 err
= dsl_destroy_snapshot(zc
->zc_name
, zc
->zc_defer_destroy
);
3652 err
= dsl_destroy_head(zc
->zc_name
);
3653 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3654 (void) zvol_remove_minor(zc
->zc_name
);
3659 * fsname is name of dataset to rollback (to most recent snapshot)
3661 * innvl may contain name of expected target snapshot
3663 * outnvl: "target" -> name of most recent snapshot
3668 zfs_ioc_rollback(const char *fsname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
3671 char *target
= NULL
;
3674 (void) nvlist_lookup_string(innvl
, "target", &target
);
3675 if (target
!= NULL
) {
3676 int fslen
= strlen(fsname
);
3678 if (strncmp(fsname
, target
, fslen
) != 0)
3679 return (SET_ERROR(EINVAL
));
3680 if (target
[fslen
] != '@')
3681 return (SET_ERROR(EINVAL
));
3684 if (getzfsvfs(fsname
, &zfsvfs
) == 0) {
3687 ds
= dmu_objset_ds(zfsvfs
->z_os
);
3688 error
= zfs_suspend_fs(zfsvfs
);
3692 error
= dsl_dataset_rollback(fsname
, target
, zfsvfs
,
3694 resume_err
= zfs_resume_fs(zfsvfs
, ds
);
3695 error
= error
? error
: resume_err
;
3697 VFS_RELE(zfsvfs
->z_vfs
);
3699 error
= dsl_dataset_rollback(fsname
, target
, NULL
, outnvl
);
3705 recursive_unmount(const char *fsname
, void *arg
)
3707 const char *snapname
= arg
;
3708 char fullname
[ZFS_MAX_DATASET_NAME_LEN
];
3710 (void) snprintf(fullname
, sizeof (fullname
), "%s@%s", fsname
, snapname
);
3711 zfs_unmount_snap(fullname
);
3718 * zc_name old name of dataset
3719 * zc_value new name of dataset
3720 * zc_cookie recursive flag (only valid for snapshots)
3725 zfs_ioc_rename(zfs_cmd_t
*zc
)
3727 boolean_t recursive
= zc
->zc_cookie
& 1;
3730 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3731 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3732 strchr(zc
->zc_value
, '%'))
3733 return (SET_ERROR(EINVAL
));
3735 at
= strchr(zc
->zc_name
, '@');
3737 /* snaps must be in same fs */
3740 if (strncmp(zc
->zc_name
, zc
->zc_value
, at
- zc
->zc_name
+ 1))
3741 return (SET_ERROR(EXDEV
));
3743 if (zc
->zc_objset_type
== DMU_OST_ZFS
) {
3744 error
= dmu_objset_find(zc
->zc_name
,
3745 recursive_unmount
, at
+ 1,
3746 recursive
? DS_FIND_CHILDREN
: 0);
3752 error
= dsl_dataset_rename_snapshot(zc
->zc_name
,
3753 at
+ 1, strchr(zc
->zc_value
, '@') + 1, recursive
);
3758 if (zc
->zc_objset_type
== DMU_OST_ZVOL
)
3759 (void) zvol_remove_minor(zc
->zc_name
);
3760 return (dsl_dir_rename(zc
->zc_name
, zc
->zc_value
));
3765 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3767 const char *propname
= nvpair_name(pair
);
3768 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3769 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3773 if (prop
== ZPROP_INVAL
) {
3774 if (zfs_prop_user(propname
)) {
3775 if (err
= zfs_secpolicy_write_perms(dsname
,
3776 ZFS_DELEG_PERM_USERPROP
, cr
))
3781 if (!issnap
&& zfs_prop_userquota(propname
)) {
3782 const char *perm
= NULL
;
3783 const char *uq_prefix
=
3784 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3785 const char *gq_prefix
=
3786 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3788 if (strncmp(propname
, uq_prefix
,
3789 strlen(uq_prefix
)) == 0) {
3790 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3791 } else if (strncmp(propname
, gq_prefix
,
3792 strlen(gq_prefix
)) == 0) {
3793 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3795 /* USERUSED and GROUPUSED are read-only */
3796 return (SET_ERROR(EINVAL
));
3799 if (err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
))
3804 return (SET_ERROR(EINVAL
));
3808 return (SET_ERROR(EINVAL
));
3810 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3812 * dsl_prop_get_all_impl() returns properties in this
3816 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3817 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3822 * Check that this value is valid for this pool version
3825 case ZFS_PROP_COMPRESSION
:
3827 * If the user specified gzip compression, make sure
3828 * the SPA supports it. We ignore any errors here since
3829 * we'll catch them later.
3831 if (nvpair_value_uint64(pair
, &intval
) == 0) {
3832 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3833 intval
<= ZIO_COMPRESS_GZIP_9
&&
3834 zfs_earlier_version(dsname
,
3835 SPA_VERSION_GZIP_COMPRESSION
)) {
3836 return (SET_ERROR(ENOTSUP
));
3839 if (intval
== ZIO_COMPRESS_ZLE
&&
3840 zfs_earlier_version(dsname
,
3841 SPA_VERSION_ZLE_COMPRESSION
))
3842 return (SET_ERROR(ENOTSUP
));
3844 if (intval
== ZIO_COMPRESS_LZ4
) {
3847 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3850 if (!spa_feature_is_enabled(spa
,
3851 SPA_FEATURE_LZ4_COMPRESS
)) {
3852 spa_close(spa
, FTAG
);
3853 return (SET_ERROR(ENOTSUP
));
3855 spa_close(spa
, FTAG
);
3859 * If this is a bootable dataset then
3860 * verify that the compression algorithm
3861 * is supported for booting. We must return
3862 * something other than ENOTSUP since it
3863 * implies a downrev pool version.
3865 if (zfs_is_bootfs(dsname
) &&
3866 !BOOTFS_COMPRESS_VALID(intval
)) {
3867 return (SET_ERROR(ERANGE
));
3872 case ZFS_PROP_COPIES
:
3873 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3874 return (SET_ERROR(ENOTSUP
));
3877 case ZFS_PROP_RECORDSIZE
:
3878 /* Record sizes above 128k need the feature to be enabled */
3879 if (nvpair_value_uint64(pair
, &intval
) == 0 &&
3880 intval
> SPA_OLD_MAXBLOCKSIZE
) {
3884 * We don't allow setting the property above 1MB,
3885 * unless the tunable has been changed.
3887 if (intval
> zfs_max_recordsize
||
3888 intval
> SPA_MAXBLOCKSIZE
)
3889 return (SET_ERROR(ERANGE
));
3891 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3894 if (!spa_feature_is_enabled(spa
,
3895 SPA_FEATURE_LARGE_BLOCKS
)) {
3896 spa_close(spa
, FTAG
);
3897 return (SET_ERROR(ENOTSUP
));
3899 spa_close(spa
, FTAG
);
3903 case ZFS_PROP_SHARESMB
:
3904 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3905 return (SET_ERROR(ENOTSUP
));
3908 case ZFS_PROP_ACLINHERIT
:
3909 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3910 nvpair_value_uint64(pair
, &intval
) == 0) {
3911 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3912 zfs_earlier_version(dsname
,
3913 SPA_VERSION_PASSTHROUGH_X
))
3914 return (SET_ERROR(ENOTSUP
));
3918 case ZFS_PROP_CHECKSUM
:
3919 case ZFS_PROP_DEDUP
:
3921 spa_feature_t feature
;
3924 /* dedup feature version checks */
3925 if (prop
== ZFS_PROP_DEDUP
&&
3926 zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3927 return (SET_ERROR(ENOTSUP
));
3929 if (nvpair_value_uint64(pair
, &intval
) != 0)
3930 return (SET_ERROR(EINVAL
));
3932 /* check prop value is enabled in features */
3933 feature
= zio_checksum_to_feature(intval
& ZIO_CHECKSUM_MASK
);
3934 if (feature
== SPA_FEATURE_NONE
)
3937 if ((err
= spa_open(dsname
, &spa
, FTAG
)) != 0)
3940 * Salted checksums are not supported on root pools.
3942 if (spa_bootfs(spa
) != 0 &&
3943 intval
< ZIO_CHECKSUM_FUNCTIONS
&&
3944 (zio_checksum_table
[intval
].ci_flags
&
3945 ZCHECKSUM_FLAG_SALTED
)) {
3946 spa_close(spa
, FTAG
);
3947 return (SET_ERROR(ERANGE
));
3949 if (!spa_feature_is_enabled(spa
, feature
)) {
3950 spa_close(spa
, FTAG
);
3951 return (SET_ERROR(ENOTSUP
));
3953 spa_close(spa
, FTAG
);
3958 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3962 * Checks for a race condition to make sure we don't increment a feature flag
3966 zfs_prop_activate_feature_check(void *arg
, dmu_tx_t
*tx
)
3968 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3969 spa_feature_t
*featurep
= arg
;
3971 if (!spa_feature_is_active(spa
, *featurep
))
3974 return (SET_ERROR(EBUSY
));
3978 * The callback invoked on feature activation in the sync task caused by
3979 * zfs_prop_activate_feature.
3982 zfs_prop_activate_feature_sync(void *arg
, dmu_tx_t
*tx
)
3984 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
3985 spa_feature_t
*featurep
= arg
;
3987 spa_feature_incr(spa
, *featurep
, tx
);
3991 * Activates a feature on a pool in response to a property setting. This
3992 * creates a new sync task which modifies the pool to reflect the feature
3996 zfs_prop_activate_feature(spa_t
*spa
, spa_feature_t feature
)
4000 /* EBUSY here indicates that the feature is already active */
4001 err
= dsl_sync_task(spa_name(spa
),
4002 zfs_prop_activate_feature_check
, zfs_prop_activate_feature_sync
,
4003 &feature
, 2, ZFS_SPACE_CHECK_RESERVED
);
4005 if (err
!= 0 && err
!= EBUSY
)
4012 * Removes properties from the given props list that fail permission checks
4013 * needed to clear them and to restore them in case of a receive error. For each
4014 * property, make sure we have both set and inherit permissions.
4016 * Returns the first error encountered if any permission checks fail. If the
4017 * caller provides a non-NULL errlist, it also gives the complete list of names
4018 * of all the properties that failed a permission check along with the
4019 * corresponding error numbers. The caller is responsible for freeing the
4022 * If every property checks out successfully, zero is returned and the list
4023 * pointed at by errlist is NULL.
4026 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
4029 nvpair_t
*pair
, *next_pair
;
4036 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4038 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
4039 (void) strcpy(zc
->zc_name
, dataset
);
4040 pair
= nvlist_next_nvpair(props
, NULL
);
4041 while (pair
!= NULL
) {
4042 next_pair
= nvlist_next_nvpair(props
, pair
);
4044 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
4045 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
4046 (err
= zfs_secpolicy_inherit_prop(zc
, NULL
, CRED())) != 0) {
4047 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
4048 VERIFY(nvlist_add_int32(errors
,
4049 zc
->zc_value
, err
) == 0);
4053 kmem_free(zc
, sizeof (zfs_cmd_t
));
4055 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
4056 nvlist_free(errors
);
4059 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
4062 if (errlist
== NULL
)
4063 nvlist_free(errors
);
4071 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
4073 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
4074 /* dsl_prop_get_all_impl() format */
4076 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
4077 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4081 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
4083 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
4084 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
4088 if (nvpair_type(p1
) != nvpair_type(p2
))
4091 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
4092 char *valstr1
, *valstr2
;
4094 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
4095 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
4096 return (strcmp(valstr1
, valstr2
) == 0);
4098 uint64_t intval1
, intval2
;
4100 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
4101 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
4102 return (intval1
== intval2
);
4107 * Remove properties from props if they are not going to change (as determined
4108 * by comparison with origprops). Remove them from origprops as well, since we
4109 * do not need to clear or restore properties that won't change.
4112 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
4114 nvpair_t
*pair
, *next_pair
;
4116 if (origprops
== NULL
)
4117 return; /* all props need to be received */
4119 pair
= nvlist_next_nvpair(props
, NULL
);
4120 while (pair
!= NULL
) {
4121 const char *propname
= nvpair_name(pair
);
4124 next_pair
= nvlist_next_nvpair(props
, pair
);
4126 if ((nvlist_lookup_nvpair(origprops
, propname
,
4127 &match
) != 0) || !propval_equals(pair
, match
))
4128 goto next
; /* need to set received value */
4130 /* don't clear the existing received value */
4131 (void) nvlist_remove_nvpair(origprops
, match
);
4132 /* don't bother receiving the property */
4133 (void) nvlist_remove_nvpair(props
, pair
);
4140 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4141 * For example, refquota cannot be set until after the receipt of a dataset,
4142 * because in replication streams, an older/earlier snapshot may exceed the
4143 * refquota. We want to receive the older/earlier snapshot, but setting
4144 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4145 * the older/earlier snapshot from being received (with EDQUOT).
4147 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4149 * libzfs will need to be judicious handling errors encountered by props
4150 * extracted by this function.
4153 extract_delay_props(nvlist_t
*props
)
4155 nvlist_t
*delayprops
;
4156 nvpair_t
*nvp
, *tmp
;
4157 static const zfs_prop_t delayable
[] = { ZFS_PROP_REFQUOTA
, 0 };
4160 VERIFY(nvlist_alloc(&delayprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4162 for (nvp
= nvlist_next_nvpair(props
, NULL
); nvp
!= NULL
;
4163 nvp
= nvlist_next_nvpair(props
, nvp
)) {
4165 * strcmp() is safe because zfs_prop_to_name() always returns
4168 for (i
= 0; delayable
[i
] != 0; i
++) {
4169 if (strcmp(zfs_prop_to_name(delayable
[i
]),
4170 nvpair_name(nvp
)) == 0) {
4174 if (delayable
[i
] != 0) {
4175 tmp
= nvlist_prev_nvpair(props
, nvp
);
4176 VERIFY(nvlist_add_nvpair(delayprops
, nvp
) == 0);
4177 VERIFY(nvlist_remove_nvpair(props
, nvp
) == 0);
4182 if (nvlist_empty(delayprops
)) {
4183 nvlist_free(delayprops
);
4186 return (delayprops
);
4190 static boolean_t zfs_ioc_recv_inject_err
;
4195 * zc_name name of containing filesystem
4196 * zc_nvlist_src{_size} nvlist of properties to apply
4197 * zc_value name of snapshot to create
4198 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4199 * zc_cookie file descriptor to recv from
4200 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4201 * zc_guid force flag
4202 * zc_cleanup_fd cleanup-on-exit file descriptor
4203 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4204 * zc_resumable if data is incomplete assume sender will resume
4207 * zc_cookie number of bytes read
4208 * zc_nvlist_dst{_size} error for each unapplied received property
4209 * zc_obj zprop_errflags_t
4210 * zc_action_handle handle for this guid/ds mapping
4213 zfs_ioc_recv(zfs_cmd_t
*zc
)
4216 dmu_recv_cookie_t drc
;
4217 boolean_t force
= (boolean_t
)zc
->zc_guid
;
4220 int props_error
= 0;
4223 nvlist_t
*props
= NULL
; /* sent properties */
4224 nvlist_t
*origprops
= NULL
; /* existing properties */
4225 nvlist_t
*delayprops
= NULL
; /* sent properties applied post-receive */
4226 char *origin
= NULL
;
4228 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
4229 boolean_t first_recvd_props
= B_FALSE
;
4231 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
4232 strchr(zc
->zc_value
, '@') == NULL
||
4233 strchr(zc
->zc_value
, '%'))
4234 return (SET_ERROR(EINVAL
));
4236 (void) strcpy(tofs
, zc
->zc_value
);
4237 tosnap
= strchr(tofs
, '@');
4240 if (zc
->zc_nvlist_src
!= NULL
&&
4241 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
4242 zc
->zc_iflags
, &props
)) != 0)
4249 return (SET_ERROR(EBADF
));
4252 errors
= fnvlist_alloc();
4254 if (zc
->zc_string
[0])
4255 origin
= zc
->zc_string
;
4257 error
= dmu_recv_begin(tofs
, tosnap
,
4258 &zc
->zc_begin_record
, force
, zc
->zc_resumable
, origin
, &drc
);
4263 * Set properties before we receive the stream so that they are applied
4264 * to the new data. Note that we must call dmu_recv_stream() if
4265 * dmu_recv_begin() succeeds.
4267 if (props
!= NULL
&& !drc
.drc_newfs
) {
4268 if (spa_version(dsl_dataset_get_spa(drc
.drc_ds
)) >=
4269 SPA_VERSION_RECVD_PROPS
&&
4270 !dsl_prop_get_hasrecvd(tofs
))
4271 first_recvd_props
= B_TRUE
;
4274 * If new received properties are supplied, they are to
4275 * completely replace the existing received properties, so stash
4276 * away the existing ones.
4278 if (dsl_prop_get_received(tofs
, &origprops
) == 0) {
4279 nvlist_t
*errlist
= NULL
;
4281 * Don't bother writing a property if its value won't
4282 * change (and avoid the unnecessary security checks).
4284 * The first receive after SPA_VERSION_RECVD_PROPS is a
4285 * special case where we blow away all local properties
4288 if (!first_recvd_props
)
4289 props_reduce(props
, origprops
);
4290 if (zfs_check_clearable(tofs
, origprops
, &errlist
) != 0)
4291 (void) nvlist_merge(errors
, errlist
, 0);
4292 nvlist_free(errlist
);
4294 if (clear_received_props(tofs
, origprops
,
4295 first_recvd_props
? NULL
: props
) != 0)
4296 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4298 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
4302 if (props
!= NULL
) {
4303 props_error
= dsl_prop_set_hasrecvd(tofs
);
4305 if (props_error
== 0) {
4306 delayprops
= extract_delay_props(props
);
4307 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4313 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
4314 &zc
->zc_action_handle
);
4317 zfsvfs_t
*zfsvfs
= NULL
;
4319 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
4324 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4325 error
= zfs_suspend_fs(zfsvfs
);
4327 * If the suspend fails, then the recv_end will
4328 * likely also fail, and clean up after itself.
4330 end_err
= dmu_recv_end(&drc
, zfsvfs
);
4332 error
= zfs_resume_fs(zfsvfs
, ds
);
4333 error
= error
? error
: end_err
;
4334 VFS_RELE(zfsvfs
->z_vfs
);
4336 error
= dmu_recv_end(&drc
, NULL
);
4339 /* Set delayed properties now, after we're done receiving. */
4340 if (delayprops
!= NULL
&& error
== 0) {
4341 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
4342 delayprops
, errors
);
4346 if (delayprops
!= NULL
) {
4348 * Merge delayed props back in with initial props, in case
4349 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4350 * we have to make sure clear_received_props() includes
4351 * the delayed properties).
4353 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4354 * using ASSERT() will be just like a VERIFY.
4356 ASSERT(nvlist_merge(props
, delayprops
, 0) == 0);
4357 nvlist_free(delayprops
);
4361 * Now that all props, initial and delayed, are set, report the prop
4362 * errors to the caller.
4364 if (zc
->zc_nvlist_dst_size
!= 0 &&
4365 (nvlist_smush(errors
, zc
->zc_nvlist_dst_size
) != 0 ||
4366 put_nvlist(zc
, errors
) != 0)) {
4368 * Caller made zc->zc_nvlist_dst less than the minimum expected
4369 * size or supplied an invalid address.
4371 props_error
= SET_ERROR(EINVAL
);
4374 zc
->zc_cookie
= off
- fp
->f_offset
;
4375 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4379 if (zfs_ioc_recv_inject_err
) {
4380 zfs_ioc_recv_inject_err
= B_FALSE
;
4385 * On error, restore the original props.
4387 if (error
!= 0 && props
!= NULL
&& !drc
.drc_newfs
) {
4388 if (clear_received_props(tofs
, props
, NULL
) != 0) {
4390 * We failed to clear the received properties.
4391 * Since we may have left a $recvd value on the
4392 * system, we can't clear the $hasrecvd flag.
4394 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4395 } else if (first_recvd_props
) {
4396 dsl_prop_unset_hasrecvd(tofs
);
4399 if (origprops
== NULL
&& !drc
.drc_newfs
) {
4400 /* We failed to stash the original properties. */
4401 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4405 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4406 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4407 * explictly if we're restoring local properties cleared in the
4408 * first new-style receive.
4410 if (origprops
!= NULL
&&
4411 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
4412 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
4413 origprops
, NULL
) != 0) {
4415 * We stashed the original properties but failed to
4418 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
4423 nvlist_free(origprops
);
4424 nvlist_free(errors
);
4428 error
= props_error
;
4435 * zc_name name of snapshot to send
4436 * zc_cookie file descriptor to send stream to
4437 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4438 * zc_sendobj objsetid of snapshot to send
4439 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4440 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4441 * output size in zc_objset_type.
4442 * zc_flags lzc_send_flags
4445 * zc_objset_type estimated size, if zc_guid is set
4448 zfs_ioc_send(zfs_cmd_t
*zc
)
4452 boolean_t estimate
= (zc
->zc_guid
!= 0);
4453 boolean_t embedok
= (zc
->zc_flags
& 0x1);
4454 boolean_t large_block_ok
= (zc
->zc_flags
& 0x2);
4455 boolean_t compressok
= (zc
->zc_flags
& 0x4);
4457 if (zc
->zc_obj
!= 0) {
4459 dsl_dataset_t
*tosnap
;
4461 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4465 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4467 dsl_pool_rele(dp
, FTAG
);
4471 if (dsl_dir_is_clone(tosnap
->ds_dir
))
4473 dsl_dir_phys(tosnap
->ds_dir
)->dd_origin_obj
;
4474 dsl_dataset_rele(tosnap
, FTAG
);
4475 dsl_pool_rele(dp
, FTAG
);
4480 dsl_dataset_t
*tosnap
;
4481 dsl_dataset_t
*fromsnap
= NULL
;
4483 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4487 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &tosnap
);
4489 dsl_pool_rele(dp
, FTAG
);
4493 if (zc
->zc_fromobj
!= 0) {
4494 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
,
4497 dsl_dataset_rele(tosnap
, FTAG
);
4498 dsl_pool_rele(dp
, FTAG
);
4503 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
4504 &zc
->zc_objset_type
);
4506 if (fromsnap
!= NULL
)
4507 dsl_dataset_rele(fromsnap
, FTAG
);
4508 dsl_dataset_rele(tosnap
, FTAG
);
4509 dsl_pool_rele(dp
, FTAG
);
4511 file_t
*fp
= getf(zc
->zc_cookie
);
4513 return (SET_ERROR(EBADF
));
4516 error
= dmu_send_obj(zc
->zc_name
, zc
->zc_sendobj
,
4517 zc
->zc_fromobj
, embedok
, large_block_ok
, compressok
,
4518 zc
->zc_cookie
, fp
->f_vnode
, &off
);
4520 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4522 releasef(zc
->zc_cookie
);
4529 * zc_name name of snapshot on which to report progress
4530 * zc_cookie file descriptor of send stream
4533 * zc_cookie number of bytes written in send stream thus far
4536 zfs_ioc_send_progress(zfs_cmd_t
*zc
)
4540 dmu_sendarg_t
*dsp
= NULL
;
4543 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4547 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4549 dsl_pool_rele(dp
, FTAG
);
4553 mutex_enter(&ds
->ds_sendstream_lock
);
4556 * Iterate over all the send streams currently active on this dataset.
4557 * If there's one which matches the specified file descriptor _and_ the
4558 * stream was started by the current process, return the progress of
4561 for (dsp
= list_head(&ds
->ds_sendstreams
); dsp
!= NULL
;
4562 dsp
= list_next(&ds
->ds_sendstreams
, dsp
)) {
4563 if (dsp
->dsa_outfd
== zc
->zc_cookie
&&
4564 dsp
->dsa_proc
== curproc
)
4569 zc
->zc_cookie
= *(dsp
->dsa_off
);
4571 error
= SET_ERROR(ENOENT
);
4573 mutex_exit(&ds
->ds_sendstream_lock
);
4574 dsl_dataset_rele(ds
, FTAG
);
4575 dsl_pool_rele(dp
, FTAG
);
4580 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
4584 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
4585 &zc
->zc_inject_record
);
4588 zc
->zc_guid
= (uint64_t)id
;
4594 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
4596 return (zio_clear_fault((int)zc
->zc_guid
));
4600 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
4602 int id
= (int)zc
->zc_guid
;
4605 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
4606 &zc
->zc_inject_record
);
4614 zfs_ioc_error_log(zfs_cmd_t
*zc
)
4618 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
4620 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
4623 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4626 zc
->zc_nvlist_dst_size
= count
;
4628 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
4630 spa_close(spa
, FTAG
);
4636 zfs_ioc_clear(zfs_cmd_t
*zc
)
4643 * On zpool clear we also fix up missing slogs
4645 mutex_enter(&spa_namespace_lock
);
4646 spa
= spa_lookup(zc
->zc_name
);
4648 mutex_exit(&spa_namespace_lock
);
4649 return (SET_ERROR(EIO
));
4651 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
4652 /* we need to let spa_open/spa_load clear the chains */
4653 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
4655 spa
->spa_last_open_failed
= 0;
4656 mutex_exit(&spa_namespace_lock
);
4658 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
4659 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4662 nvlist_t
*config
= NULL
;
4664 if (zc
->zc_nvlist_src
== NULL
)
4665 return (SET_ERROR(EINVAL
));
4667 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4668 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
4669 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
4671 if (config
!= NULL
) {
4674 if ((err
= put_nvlist(zc
, config
)) != 0)
4676 nvlist_free(config
);
4678 nvlist_free(policy
);
4685 spa_vdev_state_enter(spa
, SCL_NONE
);
4687 if (zc
->zc_guid
== 0) {
4690 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4692 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4693 spa_close(spa
, FTAG
);
4694 return (SET_ERROR(ENODEV
));
4698 vdev_clear(spa
, vd
);
4700 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4703 * Resume any suspended I/Os.
4705 if (zio_resume(spa
) != 0)
4706 error
= SET_ERROR(EIO
);
4708 spa_close(spa
, FTAG
);
4714 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4719 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4723 spa_vdev_state_enter(spa
, SCL_NONE
);
4726 * If a resilver is already in progress then set the
4727 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4728 * the scan as a side effect of the reopen. Otherwise, let
4729 * vdev_open() decided if a resilver is required.
4731 spa
->spa_scrub_reopen
= dsl_scan_resilvering(spa
->spa_dsl_pool
);
4732 vdev_reopen(spa
->spa_root_vdev
);
4733 spa
->spa_scrub_reopen
= B_FALSE
;
4735 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4736 spa_close(spa
, FTAG
);
4741 * zc_name name of filesystem
4744 * zc_string name of conflicting snapshot, if there is one
4747 zfs_ioc_promote(zfs_cmd_t
*zc
)
4750 dsl_dataset_t
*ds
, *ods
;
4751 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
4755 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
4759 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &ds
);
4761 dsl_pool_rele(dp
, FTAG
);
4765 if (!dsl_dir_is_clone(ds
->ds_dir
)) {
4766 dsl_dataset_rele(ds
, FTAG
);
4767 dsl_pool_rele(dp
, FTAG
);
4768 return (SET_ERROR(EINVAL
));
4771 error
= dsl_dataset_hold_obj(dp
,
4772 dsl_dir_phys(ds
->ds_dir
)->dd_origin_obj
, FTAG
, &ods
);
4774 dsl_dataset_rele(ds
, FTAG
);
4775 dsl_pool_rele(dp
, FTAG
);
4779 dsl_dataset_name(ods
, origin
);
4780 dsl_dataset_rele(ods
, FTAG
);
4781 dsl_dataset_rele(ds
, FTAG
);
4782 dsl_pool_rele(dp
, FTAG
);
4785 * We don't need to unmount *all* the origin fs's snapshots, but
4788 cp
= strchr(origin
, '@');
4791 (void) dmu_objset_find(origin
,
4792 zfs_unmount_snap_cb
, NULL
, DS_FIND_SNAPSHOTS
);
4793 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4797 * Retrieve a single {user|group}{used|quota}@... property.
4800 * zc_name name of filesystem
4801 * zc_objset_type zfs_userquota_prop_t
4802 * zc_value domain name (eg. "S-1-234-567-89")
4803 * zc_guid RID/UID/GID
4806 * zc_cookie property value
4809 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4814 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4815 return (SET_ERROR(EINVAL
));
4817 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4821 error
= zfs_userspace_one(zfsvfs
,
4822 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4823 zfsvfs_rele(zfsvfs
, FTAG
);
4830 * zc_name name of filesystem
4831 * zc_cookie zap cursor
4832 * zc_objset_type zfs_userquota_prop_t
4833 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4836 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4837 * zc_cookie zap cursor
4840 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4843 int bufsize
= zc
->zc_nvlist_dst_size
;
4846 return (SET_ERROR(ENOMEM
));
4848 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4852 void *buf
= kmem_alloc(bufsize
, KM_SLEEP
);
4854 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4855 buf
, &zc
->zc_nvlist_dst_size
);
4858 error
= xcopyout(buf
,
4859 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4860 zc
->zc_nvlist_dst_size
);
4862 kmem_free(buf
, bufsize
);
4863 zfsvfs_rele(zfsvfs
, FTAG
);
4870 * zc_name name of filesystem
4876 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4882 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
4883 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
4885 * If userused is not enabled, it may be because the
4886 * objset needs to be closed & reopened (to grow the
4887 * objset_phys_t). Suspend/resume the fs will do that.
4891 ds
= dmu_objset_ds(zfsvfs
->z_os
);
4892 error
= zfs_suspend_fs(zfsvfs
);
4894 dmu_objset_refresh_ownership(zfsvfs
->z_os
,
4896 error
= zfs_resume_fs(zfsvfs
, ds
);
4900 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
4901 VFS_RELE(zfsvfs
->z_vfs
);
4903 /* XXX kind of reading contents without owning */
4904 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4908 error
= dmu_objset_userspace_upgrade(os
);
4909 dmu_objset_rele(os
, FTAG
);
4916 * We don't want to have a hard dependency
4917 * against some special symbols in sharefs
4918 * nfs, and smbsrv. Determine them if needed when
4919 * the first file system is shared.
4920 * Neither sharefs, nfs or smbsrv are unloadable modules.
4922 int (*znfsexport_fs
)(void *arg
);
4923 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
4924 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
4926 int zfs_nfsshare_inited
;
4927 int zfs_smbshare_inited
;
4929 ddi_modhandle_t nfs_mod
;
4930 ddi_modhandle_t sharefs_mod
;
4931 ddi_modhandle_t smbsrv_mod
;
4932 kmutex_t zfs_share_lock
;
4939 ASSERT(MUTEX_HELD(&zfs_share_lock
));
4940 /* Both NFS and SMB shares also require sharetab support. */
4941 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
4942 ddi_modopen("fs/sharefs",
4943 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4944 return (SET_ERROR(ENOSYS
));
4946 if (zshare_fs
== NULL
&& ((zshare_fs
=
4947 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
4948 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
4949 return (SET_ERROR(ENOSYS
));
4955 zfs_ioc_share(zfs_cmd_t
*zc
)
4960 switch (zc
->zc_share
.z_sharetype
) {
4962 case ZFS_UNSHARE_NFS
:
4963 if (zfs_nfsshare_inited
== 0) {
4964 mutex_enter(&zfs_share_lock
);
4965 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
4966 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4967 mutex_exit(&zfs_share_lock
);
4968 return (SET_ERROR(ENOSYS
));
4970 if (znfsexport_fs
== NULL
&&
4971 ((znfsexport_fs
= (int (*)(void *))
4973 "nfs_export", &error
)) == NULL
)) {
4974 mutex_exit(&zfs_share_lock
);
4975 return (SET_ERROR(ENOSYS
));
4977 error
= zfs_init_sharefs();
4979 mutex_exit(&zfs_share_lock
);
4980 return (SET_ERROR(ENOSYS
));
4982 zfs_nfsshare_inited
= 1;
4983 mutex_exit(&zfs_share_lock
);
4987 case ZFS_UNSHARE_SMB
:
4988 if (zfs_smbshare_inited
== 0) {
4989 mutex_enter(&zfs_share_lock
);
4990 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
4991 ddi_modopen("drv/smbsrv",
4992 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4993 mutex_exit(&zfs_share_lock
);
4994 return (SET_ERROR(ENOSYS
));
4996 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
4997 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
4998 "smb_server_share", &error
)) == NULL
)) {
4999 mutex_exit(&zfs_share_lock
);
5000 return (SET_ERROR(ENOSYS
));
5002 error
= zfs_init_sharefs();
5004 mutex_exit(&zfs_share_lock
);
5005 return (SET_ERROR(ENOSYS
));
5007 zfs_smbshare_inited
= 1;
5008 mutex_exit(&zfs_share_lock
);
5012 return (SET_ERROR(EINVAL
));
5015 switch (zc
->zc_share
.z_sharetype
) {
5017 case ZFS_UNSHARE_NFS
:
5019 znfsexport_fs((void *)
5020 (uintptr_t)zc
->zc_share
.z_exportdata
))
5024 case ZFS_UNSHARE_SMB
:
5025 if (error
= zsmbexport_fs((void *)
5026 (uintptr_t)zc
->zc_share
.z_exportdata
,
5027 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
5034 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
5035 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
5036 SHAREFS_ADD
: SHAREFS_REMOVE
;
5039 * Add or remove share from sharetab
5041 error
= zshare_fs(opcode
,
5042 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
5043 zc
->zc_share
.z_sharemax
);
5049 ace_t full_access
[] = {
5050 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
5055 * zc_name name of containing filesystem
5056 * zc_obj object # beyond which we want next in-use object #
5059 * zc_obj next in-use object #
5062 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
5064 objset_t
*os
= NULL
;
5067 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
5071 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
5072 dsl_dataset_phys(os
->os_dsl_dataset
)->ds_prev_snap_txg
);
5074 dmu_objset_rele(os
, FTAG
);
5080 * zc_name name of filesystem
5081 * zc_value prefix name for snapshot
5082 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5085 * zc_value short name of new snapshot
5088 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
5095 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
5099 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
5100 (u_longlong_t
)ddi_get_lbolt64());
5101 hold_name
= kmem_asprintf("%%%s", zc
->zc_value
);
5103 error
= dsl_dataset_snapshot_tmp(zc
->zc_name
, snap_name
, minor
,
5106 (void) strcpy(zc
->zc_value
, snap_name
);
5109 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
5115 * zc_name name of "to" snapshot
5116 * zc_value name of "from" snapshot
5117 * zc_cookie file descriptor to write diff data on
5120 * dmu_diff_record_t's to the file descriptor
5123 zfs_ioc_diff(zfs_cmd_t
*zc
)
5129 fp
= getf(zc
->zc_cookie
);
5131 return (SET_ERROR(EBADF
));
5135 error
= dmu_diff(zc
->zc_name
, zc
->zc_value
, fp
->f_vnode
, &off
);
5137 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5139 releasef(zc
->zc_cookie
);
5145 * Remove all ACL files in shares dir
5148 zfs_smb_acl_purge(znode_t
*dzp
)
5151 zap_attribute_t zap
;
5152 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
5155 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
5156 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
5157 zap_cursor_advance(&zc
)) {
5158 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
5162 zap_cursor_fini(&zc
);
5167 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
5171 vnode_t
*resourcevp
= NULL
;
5180 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
5181 NO_FOLLOW
, NULL
, &vp
)) != 0)
5184 /* Now make sure mntpnt and dataset are ZFS */
5186 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
5187 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
5188 zc
->zc_name
) != 0)) {
5190 return (SET_ERROR(EINVAL
));
5194 zfsvfs
= dzp
->z_zfsvfs
;
5198 * Create share dir if its missing.
5200 mutex_enter(&zfsvfs
->z_lock
);
5201 if (zfsvfs
->z_shares_dir
== 0) {
5204 tx
= dmu_tx_create(zfsvfs
->z_os
);
5205 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
5207 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
5208 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5212 error
= zfs_create_share_dir(zfsvfs
, tx
);
5216 mutex_exit(&zfsvfs
->z_lock
);
5222 mutex_exit(&zfsvfs
->z_lock
);
5224 ASSERT(zfsvfs
->z_shares_dir
);
5225 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
5231 switch (zc
->zc_cookie
) {
5232 case ZFS_SMB_ACL_ADD
:
5233 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
5234 vattr
.va_type
= VREG
;
5235 vattr
.va_mode
= S_IFREG
|0777;
5239 vsec
.vsa_mask
= VSA_ACE
;
5240 vsec
.vsa_aclentp
= &full_access
;
5241 vsec
.vsa_aclentsz
= sizeof (full_access
);
5242 vsec
.vsa_aclcnt
= 1;
5244 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
5245 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
5247 VN_RELE(resourcevp
);
5250 case ZFS_SMB_ACL_REMOVE
:
5251 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
5255 case ZFS_SMB_ACL_RENAME
:
5256 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
5257 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
5259 VN_RELE(ZTOV(sharedir
));
5263 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
5264 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
5267 VN_RELE(ZTOV(sharedir
));
5269 nvlist_free(nvlist
);
5272 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
5274 nvlist_free(nvlist
);
5277 case ZFS_SMB_ACL_PURGE
:
5278 error
= zfs_smb_acl_purge(sharedir
);
5282 error
= SET_ERROR(EINVAL
);
5287 VN_RELE(ZTOV(sharedir
));
5296 * "holds" -> { snapname -> holdname (string), ... }
5297 * (optional) "cleanup_fd" -> fd (int32)
5301 * snapname -> error value (int32)
5307 zfs_ioc_hold(const char *pool
, nvlist_t
*args
, nvlist_t
*errlist
)
5311 int cleanup_fd
= -1;
5315 error
= nvlist_lookup_nvlist(args
, "holds", &holds
);
5317 return (SET_ERROR(EINVAL
));
5319 /* make sure the user didn't pass us any invalid (empty) tags */
5320 for (pair
= nvlist_next_nvpair(holds
, NULL
); pair
!= NULL
;
5321 pair
= nvlist_next_nvpair(holds
, pair
)) {
5324 error
= nvpair_value_string(pair
, &htag
);
5326 return (SET_ERROR(error
));
5328 if (strlen(htag
) == 0)
5329 return (SET_ERROR(EINVAL
));
5332 if (nvlist_lookup_int32(args
, "cleanup_fd", &cleanup_fd
) == 0) {
5333 error
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
5338 error
= dsl_dataset_user_hold(holds
, minor
, errlist
);
5340 zfs_onexit_fd_rele(cleanup_fd
);
5345 * innvl is not used.
5348 * holdname -> time added (uint64 seconds since epoch)
5354 zfs_ioc_get_holds(const char *snapname
, nvlist_t
*args
, nvlist_t
*outnvl
)
5356 return (dsl_dataset_get_holds(snapname
, outnvl
));
5361 * snapname -> { holdname, ... }
5366 * snapname -> error value (int32)
5372 zfs_ioc_release(const char *pool
, nvlist_t
*holds
, nvlist_t
*errlist
)
5374 return (dsl_dataset_user_release(holds
, errlist
));
5379 * zc_name name of new filesystem or snapshot
5380 * zc_value full name of old snapshot
5383 * zc_cookie space in bytes
5384 * zc_objset_type compressed space in bytes
5385 * zc_perm_action uncompressed space in bytes
5388 zfs_ioc_space_written(zfs_cmd_t
*zc
)
5392 dsl_dataset_t
*new, *old
;
5394 error
= dsl_pool_hold(zc
->zc_name
, FTAG
, &dp
);
5397 error
= dsl_dataset_hold(dp
, zc
->zc_name
, FTAG
, &new);
5399 dsl_pool_rele(dp
, FTAG
);
5402 error
= dsl_dataset_hold(dp
, zc
->zc_value
, FTAG
, &old
);
5404 dsl_dataset_rele(new, FTAG
);
5405 dsl_pool_rele(dp
, FTAG
);
5409 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
5410 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
5411 dsl_dataset_rele(old
, FTAG
);
5412 dsl_dataset_rele(new, FTAG
);
5413 dsl_pool_rele(dp
, FTAG
);
5419 * "firstsnap" -> snapshot name
5423 * "used" -> space in bytes
5424 * "compressed" -> compressed space in bytes
5425 * "uncompressed" -> uncompressed space in bytes
5429 zfs_ioc_space_snaps(const char *lastsnap
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5433 dsl_dataset_t
*new, *old
;
5435 uint64_t used
, comp
, uncomp
;
5437 if (nvlist_lookup_string(innvl
, "firstsnap", &firstsnap
) != 0)
5438 return (SET_ERROR(EINVAL
));
5440 error
= dsl_pool_hold(lastsnap
, FTAG
, &dp
);
5444 error
= dsl_dataset_hold(dp
, lastsnap
, FTAG
, &new);
5445 if (error
== 0 && !new->ds_is_snapshot
) {
5446 dsl_dataset_rele(new, FTAG
);
5447 error
= SET_ERROR(EINVAL
);
5450 dsl_pool_rele(dp
, FTAG
);
5453 error
= dsl_dataset_hold(dp
, firstsnap
, FTAG
, &old
);
5454 if (error
== 0 && !old
->ds_is_snapshot
) {
5455 dsl_dataset_rele(old
, FTAG
);
5456 error
= SET_ERROR(EINVAL
);
5459 dsl_dataset_rele(new, FTAG
);
5460 dsl_pool_rele(dp
, FTAG
);
5464 error
= dsl_dataset_space_wouldfree(old
, new, &used
, &comp
, &uncomp
);
5465 dsl_dataset_rele(old
, FTAG
);
5466 dsl_dataset_rele(new, FTAG
);
5467 dsl_pool_rele(dp
, FTAG
);
5468 fnvlist_add_uint64(outnvl
, "used", used
);
5469 fnvlist_add_uint64(outnvl
, "compressed", comp
);
5470 fnvlist_add_uint64(outnvl
, "uncompressed", uncomp
);
5476 * "fd" -> file descriptor to write stream to (int32)
5477 * (optional) "fromsnap" -> full snap name to send an incremental from
5478 * (optional) "largeblockok" -> (value ignored)
5479 * indicates that blocks > 128KB are permitted
5480 * (optional) "embedok" -> (value ignored)
5481 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5482 * (optional) "compressok" -> (value ignored)
5483 * presence indicates compressed DRR_WRITE records are permitted
5484 * (optional) "resume_object" and "resume_offset" -> (uint64)
5485 * if present, resume send stream from specified object and offset.
5492 zfs_ioc_send_new(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5496 char *fromname
= NULL
;
5498 boolean_t largeblockok
;
5500 boolean_t compressok
;
5501 uint64_t resumeobj
= 0;
5502 uint64_t resumeoff
= 0;
5504 error
= nvlist_lookup_int32(innvl
, "fd", &fd
);
5506 return (SET_ERROR(EINVAL
));
5508 (void) nvlist_lookup_string(innvl
, "fromsnap", &fromname
);
5510 largeblockok
= nvlist_exists(innvl
, "largeblockok");
5511 embedok
= nvlist_exists(innvl
, "embedok");
5512 compressok
= nvlist_exists(innvl
, "compressok");
5514 (void) nvlist_lookup_uint64(innvl
, "resume_object", &resumeobj
);
5515 (void) nvlist_lookup_uint64(innvl
, "resume_offset", &resumeoff
);
5517 file_t
*fp
= getf(fd
);
5519 return (SET_ERROR(EBADF
));
5522 error
= dmu_send(snapname
, fromname
, embedok
, largeblockok
, compressok
,
5523 fd
, resumeobj
, resumeoff
, fp
->f_vnode
, &off
);
5525 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
5532 * Determine approximately how large a zfs send stream will be -- the number
5533 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5536 * (optional) "from" -> full snap or bookmark name to send an incremental
5538 * (optional) "largeblockok" -> (value ignored)
5539 * indicates that blocks > 128KB are permitted
5540 * (optional) "embedok" -> (value ignored)
5541 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5542 * (optional) "compressok" -> (value ignored)
5543 * presence indicates compressed DRR_WRITE records are permitted
5547 * "space" -> bytes of space (uint64)
5551 zfs_ioc_send_space(const char *snapname
, nvlist_t
*innvl
, nvlist_t
*outnvl
)
5554 dsl_dataset_t
*tosnap
;
5557 boolean_t compressok
;
5560 error
= dsl_pool_hold(snapname
, FTAG
, &dp
);
5564 error
= dsl_dataset_hold(dp
, snapname
, FTAG
, &tosnap
);
5566 dsl_pool_rele(dp
, FTAG
);
5570 compressok
= nvlist_exists(innvl
, "compressok");
5572 error
= nvlist_lookup_string(innvl
, "from", &fromname
);
5574 if (strchr(fromname
, '@') != NULL
) {
5576 * If from is a snapshot, hold it and use the more
5577 * efficient dmu_send_estimate to estimate send space
5578 * size using deadlists.
5580 dsl_dataset_t
*fromsnap
;
5581 error
= dsl_dataset_hold(dp
, fromname
, FTAG
, &fromsnap
);
5584 error
= dmu_send_estimate(tosnap
, fromsnap
, compressok
,
5586 dsl_dataset_rele(fromsnap
, FTAG
);
5587 } else if (strchr(fromname
, '#') != NULL
) {
5589 * If from is a bookmark, fetch the creation TXG of the
5590 * snapshot it was created from and use that to find
5591 * blocks that were born after it.
5593 zfs_bookmark_phys_t frombm
;
5595 error
= dsl_bookmark_lookup(dp
, fromname
, tosnap
,
5599 error
= dmu_send_estimate_from_txg(tosnap
,
5600 frombm
.zbm_creation_txg
, compressok
, &space
);
5603 * from is not properly formatted as a snapshot or
5606 error
= SET_ERROR(EINVAL
);
5611 * If estimating the size of a full send, use dmu_send_estimate.
5613 error
= dmu_send_estimate(tosnap
, NULL
, compressok
, &space
);
5616 fnvlist_add_uint64(outnvl
, "space", space
);
5619 dsl_dataset_rele(tosnap
, FTAG
);
5620 dsl_pool_rele(dp
, FTAG
);
5624 static zfs_ioc_vec_t zfs_ioc_vec
[ZFS_IOC_LAST
- ZFS_IOC_FIRST
];
5627 zfs_ioctl_register_legacy(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5628 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5629 boolean_t log_history
, zfs_ioc_poolcheck_t pool_check
)
5631 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5633 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5634 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5635 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5636 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5638 vec
->zvec_legacy_func
= func
;
5639 vec
->zvec_secpolicy
= secpolicy
;
5640 vec
->zvec_namecheck
= namecheck
;
5641 vec
->zvec_allow_log
= log_history
;
5642 vec
->zvec_pool_check
= pool_check
;
5646 * See the block comment at the beginning of this file for details on
5647 * each argument to this function.
5650 zfs_ioctl_register(const char *name
, zfs_ioc_t ioc
, zfs_ioc_func_t
*func
,
5651 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_namecheck_t namecheck
,
5652 zfs_ioc_poolcheck_t pool_check
, boolean_t smush_outnvlist
,
5653 boolean_t allow_log
)
5655 zfs_ioc_vec_t
*vec
= &zfs_ioc_vec
[ioc
- ZFS_IOC_FIRST
];
5657 ASSERT3U(ioc
, >=, ZFS_IOC_FIRST
);
5658 ASSERT3U(ioc
, <, ZFS_IOC_LAST
);
5659 ASSERT3P(vec
->zvec_legacy_func
, ==, NULL
);
5660 ASSERT3P(vec
->zvec_func
, ==, NULL
);
5662 /* if we are logging, the name must be valid */
5663 ASSERT(!allow_log
|| namecheck
!= NO_NAME
);
5665 vec
->zvec_name
= name
;
5666 vec
->zvec_func
= func
;
5667 vec
->zvec_secpolicy
= secpolicy
;
5668 vec
->zvec_namecheck
= namecheck
;
5669 vec
->zvec_pool_check
= pool_check
;
5670 vec
->zvec_smush_outnvlist
= smush_outnvlist
;
5671 vec
->zvec_allow_log
= allow_log
;
5675 zfs_ioctl_register_pool(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5676 zfs_secpolicy_func_t
*secpolicy
, boolean_t log_history
,
5677 zfs_ioc_poolcheck_t pool_check
)
5679 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5680 POOL_NAME
, log_history
, pool_check
);
5684 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5685 zfs_secpolicy_func_t
*secpolicy
, zfs_ioc_poolcheck_t pool_check
)
5687 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5688 DATASET_NAME
, B_FALSE
, pool_check
);
5692 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5694 zfs_ioctl_register_legacy(ioc
, func
, zfs_secpolicy_config
,
5695 POOL_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5699 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5700 zfs_secpolicy_func_t
*secpolicy
)
5702 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5703 NO_NAME
, B_FALSE
, POOL_CHECK_NONE
);
5707 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc
,
5708 zfs_ioc_legacy_func_t
*func
, zfs_secpolicy_func_t
*secpolicy
)
5710 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5711 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5715 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
)
5717 zfs_ioctl_register_dataset_read_secpolicy(ioc
, func
,
5718 zfs_secpolicy_read
);
5722 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc
, zfs_ioc_legacy_func_t
*func
,
5723 zfs_secpolicy_func_t
*secpolicy
)
5725 zfs_ioctl_register_legacy(ioc
, func
, secpolicy
,
5726 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5730 zfs_ioctl_init(void)
5732 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT
,
5733 zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, POOL_NAME
,
5734 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5736 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY
,
5737 zfs_ioc_log_history
, zfs_secpolicy_log_history
, NO_NAME
,
5738 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_FALSE
);
5740 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS
,
5741 zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
,
5742 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5744 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW
,
5745 zfs_ioc_send_new
, zfs_secpolicy_send_new
, DATASET_NAME
,
5746 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5748 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE
,
5749 zfs_ioc_send_space
, zfs_secpolicy_read
, DATASET_NAME
,
5750 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5752 zfs_ioctl_register("create", ZFS_IOC_CREATE
,
5753 zfs_ioc_create
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5754 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5756 zfs_ioctl_register("clone", ZFS_IOC_CLONE
,
5757 zfs_ioc_clone
, zfs_secpolicy_create_clone
, DATASET_NAME
,
5758 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5760 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS
,
5761 zfs_ioc_destroy_snaps
, zfs_secpolicy_destroy_snaps
, POOL_NAME
,
5762 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5764 zfs_ioctl_register("hold", ZFS_IOC_HOLD
,
5765 zfs_ioc_hold
, zfs_secpolicy_hold
, POOL_NAME
,
5766 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5767 zfs_ioctl_register("release", ZFS_IOC_RELEASE
,
5768 zfs_ioc_release
, zfs_secpolicy_release
, POOL_NAME
,
5769 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5771 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS
,
5772 zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
,
5773 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5775 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK
,
5776 zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
,
5777 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_FALSE
, B_TRUE
);
5779 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK
,
5780 zfs_ioc_bookmark
, zfs_secpolicy_bookmark
, POOL_NAME
,
5781 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5783 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS
,
5784 zfs_ioc_get_bookmarks
, zfs_secpolicy_read
, DATASET_NAME
,
5785 POOL_CHECK_SUSPENDED
, B_FALSE
, B_FALSE
);
5787 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS
,
5788 zfs_ioc_destroy_bookmarks
, zfs_secpolicy_destroy_bookmarks
,
5790 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
, B_TRUE
);
5792 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM
,
5793 zfs_ioc_channel_program
, zfs_secpolicy_config
,
5794 POOL_NAME
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
, B_TRUE
,
5797 /* IOCTLS that use the legacy function signature */
5799 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE
, zfs_ioc_pool_freeze
,
5800 zfs_secpolicy_config
, NO_NAME
, B_FALSE
, POOL_CHECK_READONLY
);
5802 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE
, zfs_ioc_pool_create
,
5803 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5804 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN
,
5806 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE
,
5807 zfs_ioc_pool_upgrade
);
5808 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD
,
5810 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE
,
5811 zfs_ioc_vdev_remove
);
5812 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE
,
5813 zfs_ioc_vdev_set_state
);
5814 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH
,
5815 zfs_ioc_vdev_attach
);
5816 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH
,
5817 zfs_ioc_vdev_detach
);
5818 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH
,
5819 zfs_ioc_vdev_setpath
);
5820 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU
,
5821 zfs_ioc_vdev_setfru
);
5822 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS
,
5823 zfs_ioc_pool_set_props
);
5824 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT
,
5825 zfs_ioc_vdev_split
);
5826 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID
,
5827 zfs_ioc_pool_reguid
);
5829 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS
,
5830 zfs_ioc_pool_configs
, zfs_secpolicy_none
);
5831 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT
,
5832 zfs_ioc_pool_tryimport
, zfs_secpolicy_config
);
5833 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT
,
5834 zfs_ioc_inject_fault
, zfs_secpolicy_inject
);
5835 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT
,
5836 zfs_ioc_clear_fault
, zfs_secpolicy_inject
);
5837 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT
,
5838 zfs_ioc_inject_list_next
, zfs_secpolicy_inject
);
5841 * pool destroy, and export don't log the history as part of
5842 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5843 * does the logging of those commands.
5845 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY
, zfs_ioc_pool_destroy
,
5846 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5847 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT
, zfs_ioc_pool_export
,
5848 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_NONE
);
5850 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS
, zfs_ioc_pool_stats
,
5851 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5852 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS
, zfs_ioc_pool_get_props
,
5853 zfs_secpolicy_read
, B_FALSE
, POOL_CHECK_NONE
);
5855 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG
, zfs_ioc_error_log
,
5856 zfs_secpolicy_inject
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5857 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME
,
5858 zfs_ioc_dsobj_to_dsname
,
5859 zfs_secpolicy_diff
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5860 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY
,
5861 zfs_ioc_pool_get_history
,
5862 zfs_secpolicy_config
, B_FALSE
, POOL_CHECK_SUSPENDED
);
5864 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT
, zfs_ioc_pool_import
,
5865 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5867 zfs_ioctl_register_pool(ZFS_IOC_CLEAR
, zfs_ioc_clear
,
5868 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_NONE
);
5869 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN
, zfs_ioc_pool_reopen
,
5870 zfs_secpolicy_config
, B_TRUE
, POOL_CHECK_SUSPENDED
);
5872 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN
,
5873 zfs_ioc_space_written
);
5874 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS
,
5875 zfs_ioc_objset_recvd_props
);
5876 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ
,
5878 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL
,
5880 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS
,
5881 zfs_ioc_objset_stats
);
5882 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS
,
5883 zfs_ioc_objset_zplprops
);
5884 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT
,
5885 zfs_ioc_dataset_list_next
);
5886 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT
,
5887 zfs_ioc_snapshot_list_next
);
5888 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS
,
5889 zfs_ioc_send_progress
);
5891 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF
,
5892 zfs_ioc_diff
, zfs_secpolicy_diff
);
5893 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS
,
5894 zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
);
5895 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH
,
5896 zfs_ioc_obj_to_path
, zfs_secpolicy_diff
);
5897 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE
,
5898 zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
);
5899 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY
,
5900 zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
);
5901 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND
,
5902 zfs_ioc_send
, zfs_secpolicy_send
);
5904 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP
, zfs_ioc_set_prop
,
5905 zfs_secpolicy_none
);
5906 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY
, zfs_ioc_destroy
,
5907 zfs_secpolicy_destroy
);
5908 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME
, zfs_ioc_rename
,
5909 zfs_secpolicy_rename
);
5910 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV
, zfs_ioc_recv
,
5911 zfs_secpolicy_recv
);
5912 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE
, zfs_ioc_promote
,
5913 zfs_secpolicy_promote
);
5914 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP
,
5915 zfs_ioc_inherit_prop
, zfs_secpolicy_inherit_prop
);
5916 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL
, zfs_ioc_set_fsacl
,
5917 zfs_secpolicy_set_fsacl
);
5919 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE
, zfs_ioc_share
,
5920 zfs_secpolicy_share
, POOL_CHECK_NONE
);
5921 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL
, zfs_ioc_smb_acl
,
5922 zfs_secpolicy_smb_acl
, POOL_CHECK_NONE
);
5923 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE
,
5924 zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
5925 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5926 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT
,
5927 zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
,
5928 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
);
5932 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
5933 zfs_ioc_poolcheck_t check
)
5938 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
5940 if (check
& POOL_CHECK_NONE
)
5943 error
= spa_open(name
, &spa
, FTAG
);
5945 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
5946 error
= SET_ERROR(EAGAIN
);
5947 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
5948 error
= SET_ERROR(EROFS
);
5949 spa_close(spa
, FTAG
);
5955 * Find a free minor number.
5958 zfsdev_minor_alloc(void)
5960 static minor_t last_minor
;
5963 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5965 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
5966 if (m
> ZFSDEV_MAX_MINOR
)
5968 if (ddi_get_soft_state(zfsdev_state
, m
) == NULL
) {
5978 zfs_ctldev_init(dev_t
*devp
)
5981 zfs_soft_state_t
*zs
;
5983 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
5984 ASSERT(getminor(*devp
) == 0);
5986 minor
= zfsdev_minor_alloc();
5988 return (SET_ERROR(ENXIO
));
5990 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
)
5991 return (SET_ERROR(EAGAIN
));
5993 *devp
= makedevice(getemajor(*devp
), minor
);
5995 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
5996 zs
->zss_type
= ZSST_CTLDEV
;
5997 zfs_onexit_init((zfs_onexit_t
**)&zs
->zss_data
);
6003 zfs_ctldev_destroy(zfs_onexit_t
*zo
, minor_t minor
)
6005 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
6007 zfs_onexit_destroy(zo
);
6008 ddi_soft_state_free(zfsdev_state
, minor
);
6012 zfsdev_get_soft_state(minor_t minor
, enum zfs_soft_state_type which
)
6014 zfs_soft_state_t
*zp
;
6016 zp
= ddi_get_soft_state(zfsdev_state
, minor
);
6017 if (zp
== NULL
|| zp
->zss_type
!= which
)
6020 return (zp
->zss_data
);
6024 zfsdev_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
6028 if (getminor(*devp
) != 0)
6029 return (zvol_open(devp
, flag
, otyp
, cr
));
6031 /* This is the control device. Allocate a new minor if requested. */
6033 mutex_enter(&zfsdev_state_lock
);
6034 error
= zfs_ctldev_init(devp
);
6035 mutex_exit(&zfsdev_state_lock
);
6042 zfsdev_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
6045 minor_t minor
= getminor(dev
);
6050 mutex_enter(&zfsdev_state_lock
);
6051 zo
= zfsdev_get_soft_state(minor
, ZSST_CTLDEV
);
6053 mutex_exit(&zfsdev_state_lock
);
6054 return (zvol_close(dev
, flag
, otyp
, cr
));
6056 zfs_ctldev_destroy(zo
, minor
);
6057 mutex_exit(&zfsdev_state_lock
);
6063 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
6068 minor_t minor
= getminor(dev
);
6069 const zfs_ioc_vec_t
*vec
;
6070 char *saved_poolname
= NULL
;
6071 nvlist_t
*innvl
= NULL
;
6074 zfsdev_get_soft_state(minor
, ZSST_CTLDEV
) == NULL
)
6075 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
6077 vecnum
= cmd
- ZFS_IOC_FIRST
;
6078 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
6080 if (vecnum
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
6081 return (SET_ERROR(EINVAL
));
6082 vec
= &zfs_ioc_vec
[vecnum
];
6084 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
6086 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
6088 error
= SET_ERROR(EFAULT
);
6092 zc
->zc_iflags
= flag
& FKIOCTL
;
6093 if (zc
->zc_nvlist_src_size
!= 0) {
6094 error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
6095 zc
->zc_iflags
, &innvl
);
6101 * Ensure that all pool/dataset names are valid before we pass down to
6104 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
6105 switch (vec
->zvec_namecheck
) {
6107 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6108 error
= SET_ERROR(EINVAL
);
6110 error
= pool_status_check(zc
->zc_name
,
6111 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6115 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
6116 error
= SET_ERROR(EINVAL
);
6118 error
= pool_status_check(zc
->zc_name
,
6119 vec
->zvec_namecheck
, vec
->zvec_pool_check
);
6128 error
= vec
->zvec_secpolicy(zc
, innvl
, cr
);
6133 /* legacy ioctls can modify zc_name */
6134 len
= strcspn(zc
->zc_name
, "/@#") + 1;
6135 saved_poolname
= kmem_alloc(len
, KM_SLEEP
);
6136 (void) strlcpy(saved_poolname
, zc
->zc_name
, len
);
6138 if (vec
->zvec_func
!= NULL
) {
6142 nvlist_t
*lognv
= NULL
;
6144 ASSERT(vec
->zvec_legacy_func
== NULL
);
6147 * Add the innvl to the lognv before calling the func,
6148 * in case the func changes the innvl.
6150 if (vec
->zvec_allow_log
) {
6151 lognv
= fnvlist_alloc();
6152 fnvlist_add_string(lognv
, ZPOOL_HIST_IOCTL
,
6154 if (!nvlist_empty(innvl
)) {
6155 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_INPUT_NVL
,
6160 outnvl
= fnvlist_alloc();
6161 error
= vec
->zvec_func(zc
->zc_name
, innvl
, outnvl
);
6164 * Some commands can partially execute, modfiy state, and still
6165 * return an error. In these cases, attempt to record what
6169 (cmd
== ZFS_IOC_CHANNEL_PROGRAM
&& error
!= EINVAL
)) &&
6170 vec
->zvec_allow_log
&&
6171 spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
6172 if (!nvlist_empty(outnvl
)) {
6173 fnvlist_add_nvlist(lognv
, ZPOOL_HIST_OUTPUT_NVL
,
6177 fnvlist_add_int64(lognv
, ZPOOL_HIST_ERRNO
,
6180 (void) spa_history_log_nvl(spa
, lognv
);
6181 spa_close(spa
, FTAG
);
6183 fnvlist_free(lognv
);
6185 if (!nvlist_empty(outnvl
) || zc
->zc_nvlist_dst_size
!= 0) {
6187 if (vec
->zvec_smush_outnvlist
) {
6188 smusherror
= nvlist_smush(outnvl
,
6189 zc
->zc_nvlist_dst_size
);
6191 if (smusherror
== 0)
6192 puterror
= put_nvlist(zc
, outnvl
);
6198 nvlist_free(outnvl
);
6200 error
= vec
->zvec_legacy_func(zc
);
6205 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
6206 if (error
== 0 && rc
!= 0)
6207 error
= SET_ERROR(EFAULT
);
6208 if (error
== 0 && vec
->zvec_allow_log
) {
6209 char *s
= tsd_get(zfs_allow_log_key
);
6212 (void) tsd_set(zfs_allow_log_key
, saved_poolname
);
6214 if (saved_poolname
!= NULL
)
6215 strfree(saved_poolname
);
6218 kmem_free(zc
, sizeof (zfs_cmd_t
));
6223 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
6225 if (cmd
!= DDI_ATTACH
)
6226 return (DDI_FAILURE
);
6228 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
6229 DDI_PSEUDO
, 0) == DDI_FAILURE
)
6230 return (DDI_FAILURE
);
6234 ddi_report_dev(dip
);
6236 return (DDI_SUCCESS
);
6240 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
6242 if (spa_busy() || zfs_busy() || zvol_busy())
6243 return (DDI_FAILURE
);
6245 if (cmd
!= DDI_DETACH
)
6246 return (DDI_FAILURE
);
6250 ddi_prop_remove_all(dip
);
6251 ddi_remove_minor_node(dip
, NULL
);
6253 return (DDI_SUCCESS
);
6258 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
6261 case DDI_INFO_DEVT2DEVINFO
:
6263 return (DDI_SUCCESS
);
6265 case DDI_INFO_DEVT2INSTANCE
:
6266 *result
= (void *)0;
6267 return (DDI_SUCCESS
);
6270 return (DDI_FAILURE
);
6274 * OK, so this is a little weird.
6276 * /dev/zfs is the control node, i.e. minor 0.
6277 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6279 * /dev/zfs has basically nothing to do except serve up ioctls,
6280 * so most of the standard driver entry points are in zvol.c.
6282 static struct cb_ops zfs_cb_ops
= {
6283 zfsdev_open
, /* open */
6284 zfsdev_close
, /* close */
6285 zvol_strategy
, /* strategy */
6287 zvol_dump
, /* dump */
6288 zvol_read
, /* read */
6289 zvol_write
, /* write */
6290 zfsdev_ioctl
, /* ioctl */
6294 nochpoll
, /* poll */
6295 ddi_prop_op
, /* prop_op */
6296 NULL
, /* streamtab */
6297 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
6298 CB_REV
, /* version */
6299 nodev
, /* async read */
6300 nodev
, /* async write */
6303 static struct dev_ops zfs_dev_ops
= {
6304 DEVO_REV
, /* version */
6306 zfs_info
, /* info */
6307 nulldev
, /* identify */
6308 nulldev
, /* probe */
6309 zfs_attach
, /* attach */
6310 zfs_detach
, /* detach */
6312 &zfs_cb_ops
, /* driver operations */
6313 NULL
, /* no bus operations */
6315 ddi_quiesce_not_needed
, /* quiesce */
6318 static struct modldrv zfs_modldrv
= {
6324 static struct modlinkage modlinkage
= {
6326 (void *)&zfs_modlfs
,
6327 (void *)&zfs_modldrv
,
6332 zfs_allow_log_destroy(void *arg
)
6334 char *poolname
= arg
;
6343 spa_init(FREAD
| FWRITE
);
6348 if ((error
= mod_install(&modlinkage
)) != 0) {
6355 tsd_create(&zfs_fsyncer_key
, NULL
);
6356 tsd_create(&rrw_tsd_key
, rrw_tsd_destroy
);
6357 tsd_create(&zfs_allow_log_key
, zfs_allow_log_destroy
);
6359 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
6361 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6371 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
6372 return (SET_ERROR(EBUSY
));
6374 if ((error
= mod_remove(&modlinkage
)) != 0)
6380 if (zfs_nfsshare_inited
)
6381 (void) ddi_modclose(nfs_mod
);
6382 if (zfs_smbshare_inited
)
6383 (void) ddi_modclose(smbsrv_mod
);
6384 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
6385 (void) ddi_modclose(sharefs_mod
);
6387 tsd_destroy(&zfs_fsyncer_key
);
6388 ldi_ident_release(zfs_li
);
6390 mutex_destroy(&zfs_share_lock
);
6396 _info(struct modinfo
*modinfop
)
6398 return (mod_info(&modlinkage
, modinfop
));