4757 ZFS embedded-data block pointers ("zero block compression")
[unleashed.git] / usr / src / uts / common / fs / zfs / zfs_ioctl.c
blob01105be5c51c0f82b9947b837a45cc90653fa84d
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * ZFS ioctls.
35 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
36 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
38 * There are two ways that we handle ioctls: the legacy way where almost
39 * all of the logic is in the ioctl callback, and the new way where most
40 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
42 * Non-legacy ioctls should be registered by calling
43 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
44 * from userland by lzc_ioctl().
46 * The registration arguments are as follows:
48 * const char *name
49 * The name of the ioctl. This is used for history logging. If the
50 * ioctl returns successfully (the callback returns 0), and allow_log
51 * is true, then a history log entry will be recorded with the input &
52 * output nvlists. The log entry can be printed with "zpool history -i".
54 * zfs_ioc_t ioc
55 * The ioctl request number, which userland will pass to ioctl(2).
56 * The ioctl numbers can change from release to release, because
57 * the caller (libzfs) must be matched to the kernel.
59 * zfs_secpolicy_func_t *secpolicy
60 * This function will be called before the zfs_ioc_func_t, to
61 * determine if this operation is permitted. It should return EPERM
62 * on failure, and 0 on success. Checks include determining if the
63 * dataset is visible in this zone, and if the user has either all
64 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
65 * to do this operation on this dataset with "zfs allow".
67 * zfs_ioc_namecheck_t namecheck
68 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
69 * name, a dataset name, or nothing. If the name is not well-formed,
70 * the ioctl will fail and the callback will not be called.
71 * Therefore, the callback can assume that the name is well-formed
72 * (e.g. is null-terminated, doesn't have more than one '@' character,
73 * doesn't have invalid characters).
75 * zfs_ioc_poolcheck_t pool_check
76 * This specifies requirements on the pool state. If the pool does
77 * not meet them (is suspended or is readonly), the ioctl will fail
78 * and the callback will not be called. If any checks are specified
79 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
80 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
81 * POOL_CHECK_READONLY).
83 * boolean_t smush_outnvlist
84 * If smush_outnvlist is true, then the output is presumed to be a
85 * list of errors, and it will be "smushed" down to fit into the
86 * caller's buffer, by removing some entries and replacing them with a
87 * single "N_MORE_ERRORS" entry indicating how many were removed. See
88 * nvlist_smush() for details. If smush_outnvlist is false, and the
89 * outnvlist does not fit into the userland-provided buffer, then the
90 * ioctl will fail with ENOMEM.
92 * zfs_ioc_func_t *func
93 * The callback function that will perform the operation.
95 * The callback should return 0 on success, or an error number on
96 * failure. If the function fails, the userland ioctl will return -1,
97 * and errno will be set to the callback's return value. The callback
98 * will be called with the following arguments:
100 * const char *name
101 * The name of the pool or dataset to operate on, from
102 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
103 * expected type (pool, dataset, or none).
105 * nvlist_t *innvl
106 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
107 * NULL if no input nvlist was provided. Changes to this nvlist are
108 * ignored. If the input nvlist could not be deserialized, the
109 * ioctl will fail and the callback will not be called.
111 * nvlist_t *outnvl
112 * The output nvlist, initially empty. The callback can fill it in,
113 * and it will be returned to userland by serializing it into
114 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
115 * fails (e.g. because the caller didn't supply a large enough
116 * buffer), then the overall ioctl will fail. See the
117 * 'smush_nvlist' argument above for additional behaviors.
119 * There are two typical uses of the output nvlist:
120 * - To return state, e.g. property values. In this case,
121 * smush_outnvlist should be false. If the buffer was not large
122 * enough, the caller will reallocate a larger buffer and try
123 * the ioctl again.
125 * - To return multiple errors from an ioctl which makes on-disk
126 * changes. In this case, smush_outnvlist should be true.
127 * Ioctls which make on-disk modifications should generally not
128 * use the outnvl if they succeed, because the caller can not
129 * distinguish between the operation failing, and
130 * deserialization failing.
133 #include <sys/types.h>
134 #include <sys/param.h>
135 #include <sys/errno.h>
136 #include <sys/uio.h>
137 #include <sys/buf.h>
138 #include <sys/modctl.h>
139 #include <sys/open.h>
140 #include <sys/file.h>
141 #include <sys/kmem.h>
142 #include <sys/conf.h>
143 #include <sys/cmn_err.h>
144 #include <sys/stat.h>
145 #include <sys/zfs_ioctl.h>
146 #include <sys/zfs_vfsops.h>
147 #include <sys/zfs_znode.h>
148 #include <sys/zap.h>
149 #include <sys/spa.h>
150 #include <sys/spa_impl.h>
151 #include <sys/vdev.h>
152 #include <sys/priv_impl.h>
153 #include <sys/dmu.h>
154 #include <sys/dsl_dir.h>
155 #include <sys/dsl_dataset.h>
156 #include <sys/dsl_prop.h>
157 #include <sys/dsl_deleg.h>
158 #include <sys/dmu_objset.h>
159 #include <sys/dmu_impl.h>
160 #include <sys/dmu_tx.h>
161 #include <sys/ddi.h>
162 #include <sys/sunddi.h>
163 #include <sys/sunldi.h>
164 #include <sys/policy.h>
165 #include <sys/zone.h>
166 #include <sys/nvpair.h>
167 #include <sys/pathname.h>
168 #include <sys/mount.h>
169 #include <sys/sdt.h>
170 #include <sys/fs/zfs.h>
171 #include <sys/zfs_ctldir.h>
172 #include <sys/zfs_dir.h>
173 #include <sys/zfs_onexit.h>
174 #include <sys/zvol.h>
175 #include <sys/dsl_scan.h>
176 #include <sharefs/share.h>
177 #include <sys/dmu_objset.h>
178 #include <sys/dmu_send.h>
179 #include <sys/dsl_destroy.h>
180 #include <sys/dsl_bookmark.h>
181 #include <sys/dsl_userhold.h>
182 #include <sys/zfeature.h>
184 #include "zfs_namecheck.h"
185 #include "zfs_prop.h"
186 #include "zfs_deleg.h"
187 #include "zfs_comutil.h"
189 extern struct modlfs zfs_modlfs;
191 extern void zfs_init(void);
192 extern void zfs_fini(void);
194 ldi_ident_t zfs_li = NULL;
195 dev_info_t *zfs_dip;
197 uint_t zfs_fsyncer_key;
198 extern uint_t rrw_tsd_key;
199 static uint_t zfs_allow_log_key;
201 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
202 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
203 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
205 typedef enum {
206 NO_NAME,
207 POOL_NAME,
208 DATASET_NAME
209 } zfs_ioc_namecheck_t;
211 typedef enum {
212 POOL_CHECK_NONE = 1 << 0,
213 POOL_CHECK_SUSPENDED = 1 << 1,
214 POOL_CHECK_READONLY = 1 << 2,
215 } zfs_ioc_poolcheck_t;
217 typedef struct zfs_ioc_vec {
218 zfs_ioc_legacy_func_t *zvec_legacy_func;
219 zfs_ioc_func_t *zvec_func;
220 zfs_secpolicy_func_t *zvec_secpolicy;
221 zfs_ioc_namecheck_t zvec_namecheck;
222 boolean_t zvec_allow_log;
223 zfs_ioc_poolcheck_t zvec_pool_check;
224 boolean_t zvec_smush_outnvlist;
225 const char *zvec_name;
226 } zfs_ioc_vec_t;
228 /* This array is indexed by zfs_userquota_prop_t */
229 static const char *userquota_perms[] = {
230 ZFS_DELEG_PERM_USERUSED,
231 ZFS_DELEG_PERM_USERQUOTA,
232 ZFS_DELEG_PERM_GROUPUSED,
233 ZFS_DELEG_PERM_GROUPQUOTA,
236 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
237 static int zfs_check_settable(const char *name, nvpair_t *property,
238 cred_t *cr);
239 static int zfs_check_clearable(char *dataset, nvlist_t *props,
240 nvlist_t **errors);
241 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
242 boolean_t *);
243 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
244 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
246 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
248 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
249 void
250 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
252 const char *newfile;
253 char buf[512];
254 va_list adx;
257 * Get rid of annoying "../common/" prefix to filename.
259 newfile = strrchr(file, '/');
260 if (newfile != NULL) {
261 newfile = newfile + 1; /* Get rid of leading / */
262 } else {
263 newfile = file;
266 va_start(adx, fmt);
267 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
268 va_end(adx);
271 * To get this data, use the zfs-dprintf probe as so:
272 * dtrace -q -n 'zfs-dprintf \
273 * /stringof(arg0) == "dbuf.c"/ \
274 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
275 * arg0 = file name
276 * arg1 = function name
277 * arg2 = line number
278 * arg3 = message
280 DTRACE_PROBE4(zfs__dprintf,
281 char *, newfile, char *, func, int, line, char *, buf);
284 static void
285 history_str_free(char *buf)
287 kmem_free(buf, HIS_MAX_RECORD_LEN);
290 static char *
291 history_str_get(zfs_cmd_t *zc)
293 char *buf;
295 if (zc->zc_history == NULL)
296 return (NULL);
298 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
299 if (copyinstr((void *)(uintptr_t)zc->zc_history,
300 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
301 history_str_free(buf);
302 return (NULL);
305 buf[HIS_MAX_RECORD_LEN -1] = '\0';
307 return (buf);
311 * Check to see if the named dataset is currently defined as bootable
313 static boolean_t
314 zfs_is_bootfs(const char *name)
316 objset_t *os;
318 if (dmu_objset_hold(name, FTAG, &os) == 0) {
319 boolean_t ret;
320 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
321 dmu_objset_rele(os, FTAG);
322 return (ret);
324 return (B_FALSE);
328 * Return non-zero if the spa version is less than requested version.
330 static int
331 zfs_earlier_version(const char *name, int version)
333 spa_t *spa;
335 if (spa_open(name, &spa, FTAG) == 0) {
336 if (spa_version(spa) < version) {
337 spa_close(spa, FTAG);
338 return (1);
340 spa_close(spa, FTAG);
342 return (0);
346 * Return TRUE if the ZPL version is less than requested version.
348 static boolean_t
349 zpl_earlier_version(const char *name, int version)
351 objset_t *os;
352 boolean_t rc = B_TRUE;
354 if (dmu_objset_hold(name, FTAG, &os) == 0) {
355 uint64_t zplversion;
357 if (dmu_objset_type(os) != DMU_OST_ZFS) {
358 dmu_objset_rele(os, FTAG);
359 return (B_TRUE);
361 /* XXX reading from non-owned objset */
362 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
363 rc = zplversion < version;
364 dmu_objset_rele(os, FTAG);
366 return (rc);
369 static void
370 zfs_log_history(zfs_cmd_t *zc)
372 spa_t *spa;
373 char *buf;
375 if ((buf = history_str_get(zc)) == NULL)
376 return;
378 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
379 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
380 (void) spa_history_log(spa, buf);
381 spa_close(spa, FTAG);
383 history_str_free(buf);
387 * Policy for top-level read operations (list pools). Requires no privileges,
388 * and can be used in the local zone, as there is no associated dataset.
390 /* ARGSUSED */
391 static int
392 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
394 return (0);
398 * Policy for dataset read operations (list children, get statistics). Requires
399 * no privileges, but must be visible in the local zone.
401 /* ARGSUSED */
402 static int
403 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
405 if (INGLOBALZONE(curproc) ||
406 zone_dataset_visible(zc->zc_name, NULL))
407 return (0);
409 return (SET_ERROR(ENOENT));
412 static int
413 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
415 int writable = 1;
418 * The dataset must be visible by this zone -- check this first
419 * so they don't see EPERM on something they shouldn't know about.
421 if (!INGLOBALZONE(curproc) &&
422 !zone_dataset_visible(dataset, &writable))
423 return (SET_ERROR(ENOENT));
425 if (INGLOBALZONE(curproc)) {
427 * If the fs is zoned, only root can access it from the
428 * global zone.
430 if (secpolicy_zfs(cr) && zoned)
431 return (SET_ERROR(EPERM));
432 } else {
434 * If we are in a local zone, the 'zoned' property must be set.
436 if (!zoned)
437 return (SET_ERROR(EPERM));
439 /* must be writable by this zone */
440 if (!writable)
441 return (SET_ERROR(EPERM));
443 return (0);
446 static int
447 zfs_dozonecheck(const char *dataset, cred_t *cr)
449 uint64_t zoned;
451 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
452 return (SET_ERROR(ENOENT));
454 return (zfs_dozonecheck_impl(dataset, zoned, cr));
457 static int
458 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
460 uint64_t zoned;
462 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
463 return (SET_ERROR(ENOENT));
465 return (zfs_dozonecheck_impl(dataset, zoned, cr));
468 static int
469 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
470 const char *perm, cred_t *cr)
472 int error;
474 error = zfs_dozonecheck_ds(name, ds, cr);
475 if (error == 0) {
476 error = secpolicy_zfs(cr);
477 if (error != 0)
478 error = dsl_deleg_access_impl(ds, perm, cr);
480 return (error);
483 static int
484 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
486 int error;
487 dsl_dataset_t *ds;
488 dsl_pool_t *dp;
490 error = dsl_pool_hold(name, FTAG, &dp);
491 if (error != 0)
492 return (error);
494 error = dsl_dataset_hold(dp, name, FTAG, &ds);
495 if (error != 0) {
496 dsl_pool_rele(dp, FTAG);
497 return (error);
500 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
502 dsl_dataset_rele(ds, FTAG);
503 dsl_pool_rele(dp, FTAG);
504 return (error);
508 * Policy for setting the security label property.
510 * Returns 0 for success, non-zero for access and other errors.
512 static int
513 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
515 char ds_hexsl[MAXNAMELEN];
516 bslabel_t ds_sl, new_sl;
517 boolean_t new_default = FALSE;
518 uint64_t zoned;
519 int needed_priv = -1;
520 int error;
522 /* First get the existing dataset label. */
523 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
524 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
525 if (error != 0)
526 return (SET_ERROR(EPERM));
528 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
529 new_default = TRUE;
531 /* The label must be translatable */
532 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
533 return (SET_ERROR(EINVAL));
536 * In a non-global zone, disallow attempts to set a label that
537 * doesn't match that of the zone; otherwise no other checks
538 * are needed.
540 if (!INGLOBALZONE(curproc)) {
541 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
542 return (SET_ERROR(EPERM));
543 return (0);
547 * For global-zone datasets (i.e., those whose zoned property is
548 * "off", verify that the specified new label is valid for the
549 * global zone.
551 if (dsl_prop_get_integer(name,
552 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
553 return (SET_ERROR(EPERM));
554 if (!zoned) {
555 if (zfs_check_global_label(name, strval) != 0)
556 return (SET_ERROR(EPERM));
560 * If the existing dataset label is nondefault, check if the
561 * dataset is mounted (label cannot be changed while mounted).
562 * Get the zfsvfs; if there isn't one, then the dataset isn't
563 * mounted (or isn't a dataset, doesn't exist, ...).
565 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
566 objset_t *os;
567 static char *setsl_tag = "setsl_tag";
570 * Try to own the dataset; abort if there is any error,
571 * (e.g., already mounted, in use, or other error).
573 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
574 setsl_tag, &os);
575 if (error != 0)
576 return (SET_ERROR(EPERM));
578 dmu_objset_disown(os, setsl_tag);
580 if (new_default) {
581 needed_priv = PRIV_FILE_DOWNGRADE_SL;
582 goto out_check;
585 if (hexstr_to_label(strval, &new_sl) != 0)
586 return (SET_ERROR(EPERM));
588 if (blstrictdom(&ds_sl, &new_sl))
589 needed_priv = PRIV_FILE_DOWNGRADE_SL;
590 else if (blstrictdom(&new_sl, &ds_sl))
591 needed_priv = PRIV_FILE_UPGRADE_SL;
592 } else {
593 /* dataset currently has a default label */
594 if (!new_default)
595 needed_priv = PRIV_FILE_UPGRADE_SL;
598 out_check:
599 if (needed_priv != -1)
600 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
601 return (0);
604 static int
605 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
606 cred_t *cr)
608 char *strval;
611 * Check permissions for special properties.
613 switch (prop) {
614 case ZFS_PROP_ZONED:
616 * Disallow setting of 'zoned' from within a local zone.
618 if (!INGLOBALZONE(curproc))
619 return (SET_ERROR(EPERM));
620 break;
622 case ZFS_PROP_QUOTA:
623 case ZFS_PROP_FILESYSTEM_LIMIT:
624 case ZFS_PROP_SNAPSHOT_LIMIT:
625 if (!INGLOBALZONE(curproc)) {
626 uint64_t zoned;
627 char setpoint[MAXNAMELEN];
629 * Unprivileged users are allowed to modify the
630 * limit on things *under* (ie. contained by)
631 * the thing they own.
633 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
634 setpoint))
635 return (SET_ERROR(EPERM));
636 if (!zoned || strlen(dsname) <= strlen(setpoint))
637 return (SET_ERROR(EPERM));
639 break;
641 case ZFS_PROP_MLSLABEL:
642 if (!is_system_labeled())
643 return (SET_ERROR(EPERM));
645 if (nvpair_value_string(propval, &strval) == 0) {
646 int err;
648 err = zfs_set_slabel_policy(dsname, strval, CRED());
649 if (err != 0)
650 return (err);
652 break;
655 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
658 /* ARGSUSED */
659 static int
660 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
662 int error;
664 error = zfs_dozonecheck(zc->zc_name, cr);
665 if (error != 0)
666 return (error);
669 * permission to set permissions will be evaluated later in
670 * dsl_deleg_can_allow()
672 return (0);
675 /* ARGSUSED */
676 static int
677 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
679 return (zfs_secpolicy_write_perms(zc->zc_name,
680 ZFS_DELEG_PERM_ROLLBACK, cr));
683 /* ARGSUSED */
684 static int
685 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
687 dsl_pool_t *dp;
688 dsl_dataset_t *ds;
689 char *cp;
690 int error;
693 * Generate the current snapshot name from the given objsetid, then
694 * use that name for the secpolicy/zone checks.
696 cp = strchr(zc->zc_name, '@');
697 if (cp == NULL)
698 return (SET_ERROR(EINVAL));
699 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
700 if (error != 0)
701 return (error);
703 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
704 if (error != 0) {
705 dsl_pool_rele(dp, FTAG);
706 return (error);
709 dsl_dataset_name(ds, zc->zc_name);
711 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
712 ZFS_DELEG_PERM_SEND, cr);
713 dsl_dataset_rele(ds, FTAG);
714 dsl_pool_rele(dp, FTAG);
716 return (error);
719 /* ARGSUSED */
720 static int
721 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
723 return (zfs_secpolicy_write_perms(zc->zc_name,
724 ZFS_DELEG_PERM_SEND, cr));
727 /* ARGSUSED */
728 static int
729 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
731 vnode_t *vp;
732 int error;
734 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
735 NO_FOLLOW, NULL, &vp)) != 0)
736 return (error);
738 /* Now make sure mntpnt and dataset are ZFS */
740 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
741 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
742 zc->zc_name) != 0)) {
743 VN_RELE(vp);
744 return (SET_ERROR(EPERM));
747 VN_RELE(vp);
748 return (dsl_deleg_access(zc->zc_name,
749 ZFS_DELEG_PERM_SHARE, cr));
753 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
755 if (!INGLOBALZONE(curproc))
756 return (SET_ERROR(EPERM));
758 if (secpolicy_nfs(cr) == 0) {
759 return (0);
760 } else {
761 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
766 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
768 if (!INGLOBALZONE(curproc))
769 return (SET_ERROR(EPERM));
771 if (secpolicy_smb(cr) == 0) {
772 return (0);
773 } else {
774 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
778 static int
779 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
781 char *cp;
784 * Remove the @bla or /bla from the end of the name to get the parent.
786 (void) strncpy(parent, datasetname, parentsize);
787 cp = strrchr(parent, '@');
788 if (cp != NULL) {
789 cp[0] = '\0';
790 } else {
791 cp = strrchr(parent, '/');
792 if (cp == NULL)
793 return (SET_ERROR(ENOENT));
794 cp[0] = '\0';
797 return (0);
801 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
803 int error;
805 if ((error = zfs_secpolicy_write_perms(name,
806 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
807 return (error);
809 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
812 /* ARGSUSED */
813 static int
814 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
816 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
820 * Destroying snapshots with delegated permissions requires
821 * descendant mount and destroy permissions.
823 /* ARGSUSED */
824 static int
825 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
827 nvlist_t *snaps;
828 nvpair_t *pair, *nextpair;
829 int error = 0;
831 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
832 return (SET_ERROR(EINVAL));
833 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
834 pair = nextpair) {
835 nextpair = nvlist_next_nvpair(snaps, pair);
836 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
837 if (error == ENOENT) {
839 * Ignore any snapshots that don't exist (we consider
840 * them "already destroyed"). Remove the name from the
841 * nvl here in case the snapshot is created between
842 * now and when we try to destroy it (in which case
843 * we don't want to destroy it since we haven't
844 * checked for permission).
846 fnvlist_remove_nvpair(snaps, pair);
847 error = 0;
849 if (error != 0)
850 break;
853 return (error);
857 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
859 char parentname[MAXNAMELEN];
860 int error;
862 if ((error = zfs_secpolicy_write_perms(from,
863 ZFS_DELEG_PERM_RENAME, cr)) != 0)
864 return (error);
866 if ((error = zfs_secpolicy_write_perms(from,
867 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
868 return (error);
870 if ((error = zfs_get_parent(to, parentname,
871 sizeof (parentname))) != 0)
872 return (error);
874 if ((error = zfs_secpolicy_write_perms(parentname,
875 ZFS_DELEG_PERM_CREATE, cr)) != 0)
876 return (error);
878 if ((error = zfs_secpolicy_write_perms(parentname,
879 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
880 return (error);
882 return (error);
885 /* ARGSUSED */
886 static int
887 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
889 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
892 /* ARGSUSED */
893 static int
894 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
896 dsl_pool_t *dp;
897 dsl_dataset_t *clone;
898 int error;
900 error = zfs_secpolicy_write_perms(zc->zc_name,
901 ZFS_DELEG_PERM_PROMOTE, cr);
902 if (error != 0)
903 return (error);
905 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
906 if (error != 0)
907 return (error);
909 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
911 if (error == 0) {
912 char parentname[MAXNAMELEN];
913 dsl_dataset_t *origin = NULL;
914 dsl_dir_t *dd;
915 dd = clone->ds_dir;
917 error = dsl_dataset_hold_obj(dd->dd_pool,
918 dd->dd_phys->dd_origin_obj, FTAG, &origin);
919 if (error != 0) {
920 dsl_dataset_rele(clone, FTAG);
921 dsl_pool_rele(dp, FTAG);
922 return (error);
925 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
926 ZFS_DELEG_PERM_MOUNT, cr);
928 dsl_dataset_name(origin, parentname);
929 if (error == 0) {
930 error = zfs_secpolicy_write_perms_ds(parentname, origin,
931 ZFS_DELEG_PERM_PROMOTE, cr);
933 dsl_dataset_rele(clone, FTAG);
934 dsl_dataset_rele(origin, FTAG);
936 dsl_pool_rele(dp, FTAG);
937 return (error);
940 /* ARGSUSED */
941 static int
942 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
944 int error;
946 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
947 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
948 return (error);
950 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
951 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
952 return (error);
954 return (zfs_secpolicy_write_perms(zc->zc_name,
955 ZFS_DELEG_PERM_CREATE, cr));
959 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
961 return (zfs_secpolicy_write_perms(name,
962 ZFS_DELEG_PERM_SNAPSHOT, cr));
966 * Check for permission to create each snapshot in the nvlist.
968 /* ARGSUSED */
969 static int
970 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
972 nvlist_t *snaps;
973 int error = 0;
974 nvpair_t *pair;
976 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
977 return (SET_ERROR(EINVAL));
978 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
979 pair = nvlist_next_nvpair(snaps, pair)) {
980 char *name = nvpair_name(pair);
981 char *atp = strchr(name, '@');
983 if (atp == NULL) {
984 error = SET_ERROR(EINVAL);
985 break;
987 *atp = '\0';
988 error = zfs_secpolicy_snapshot_perms(name, cr);
989 *atp = '@';
990 if (error != 0)
991 break;
993 return (error);
997 * Check for permission to create each snapshot in the nvlist.
999 /* ARGSUSED */
1000 static int
1001 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1003 int error = 0;
1005 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1006 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1007 char *name = nvpair_name(pair);
1008 char *hashp = strchr(name, '#');
1010 if (hashp == NULL) {
1011 error = SET_ERROR(EINVAL);
1012 break;
1014 *hashp = '\0';
1015 error = zfs_secpolicy_write_perms(name,
1016 ZFS_DELEG_PERM_BOOKMARK, cr);
1017 *hashp = '#';
1018 if (error != 0)
1019 break;
1021 return (error);
1024 /* ARGSUSED */
1025 static int
1026 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1028 nvpair_t *pair, *nextpair;
1029 int error = 0;
1031 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1032 pair = nextpair) {
1033 char *name = nvpair_name(pair);
1034 char *hashp = strchr(name, '#');
1035 nextpair = nvlist_next_nvpair(innvl, pair);
1037 if (hashp == NULL) {
1038 error = SET_ERROR(EINVAL);
1039 break;
1042 *hashp = '\0';
1043 error = zfs_secpolicy_write_perms(name,
1044 ZFS_DELEG_PERM_DESTROY, cr);
1045 *hashp = '#';
1046 if (error == ENOENT) {
1048 * Ignore any filesystems that don't exist (we consider
1049 * their bookmarks "already destroyed"). Remove
1050 * the name from the nvl here in case the filesystem
1051 * is created between now and when we try to destroy
1052 * the bookmark (in which case we don't want to
1053 * destroy it since we haven't checked for permission).
1055 fnvlist_remove_nvpair(innvl, pair);
1056 error = 0;
1058 if (error != 0)
1059 break;
1062 return (error);
1065 /* ARGSUSED */
1066 static int
1067 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1070 * Even root must have a proper TSD so that we know what pool
1071 * to log to.
1073 if (tsd_get(zfs_allow_log_key) == NULL)
1074 return (SET_ERROR(EPERM));
1075 return (0);
1078 static int
1079 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1081 char parentname[MAXNAMELEN];
1082 int error;
1083 char *origin;
1085 if ((error = zfs_get_parent(zc->zc_name, parentname,
1086 sizeof (parentname))) != 0)
1087 return (error);
1089 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1090 (error = zfs_secpolicy_write_perms(origin,
1091 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1092 return (error);
1094 if ((error = zfs_secpolicy_write_perms(parentname,
1095 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1096 return (error);
1098 return (zfs_secpolicy_write_perms(parentname,
1099 ZFS_DELEG_PERM_MOUNT, cr));
1103 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1104 * SYS_CONFIG privilege, which is not available in a local zone.
1106 /* ARGSUSED */
1107 static int
1108 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1110 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1111 return (SET_ERROR(EPERM));
1113 return (0);
1117 * Policy for object to name lookups.
1119 /* ARGSUSED */
1120 static int
1121 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1123 int error;
1125 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1126 return (0);
1128 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1129 return (error);
1133 * Policy for fault injection. Requires all privileges.
1135 /* ARGSUSED */
1136 static int
1137 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1139 return (secpolicy_zinject(cr));
1142 /* ARGSUSED */
1143 static int
1144 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1146 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1148 if (prop == ZPROP_INVAL) {
1149 if (!zfs_prop_user(zc->zc_value))
1150 return (SET_ERROR(EINVAL));
1151 return (zfs_secpolicy_write_perms(zc->zc_name,
1152 ZFS_DELEG_PERM_USERPROP, cr));
1153 } else {
1154 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1155 NULL, cr));
1159 static int
1160 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1162 int err = zfs_secpolicy_read(zc, innvl, cr);
1163 if (err)
1164 return (err);
1166 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1167 return (SET_ERROR(EINVAL));
1169 if (zc->zc_value[0] == 0) {
1171 * They are asking about a posix uid/gid. If it's
1172 * themself, allow it.
1174 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1175 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1176 if (zc->zc_guid == crgetuid(cr))
1177 return (0);
1178 } else {
1179 if (groupmember(zc->zc_guid, cr))
1180 return (0);
1184 return (zfs_secpolicy_write_perms(zc->zc_name,
1185 userquota_perms[zc->zc_objset_type], cr));
1188 static int
1189 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1191 int err = zfs_secpolicy_read(zc, innvl, cr);
1192 if (err)
1193 return (err);
1195 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1196 return (SET_ERROR(EINVAL));
1198 return (zfs_secpolicy_write_perms(zc->zc_name,
1199 userquota_perms[zc->zc_objset_type], cr));
1202 /* ARGSUSED */
1203 static int
1204 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1206 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1207 NULL, cr));
1210 /* ARGSUSED */
1211 static int
1212 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1214 nvpair_t *pair;
1215 nvlist_t *holds;
1216 int error;
1218 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1219 if (error != 0)
1220 return (SET_ERROR(EINVAL));
1222 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1223 pair = nvlist_next_nvpair(holds, pair)) {
1224 char fsname[MAXNAMELEN];
1225 error = dmu_fsname(nvpair_name(pair), fsname);
1226 if (error != 0)
1227 return (error);
1228 error = zfs_secpolicy_write_perms(fsname,
1229 ZFS_DELEG_PERM_HOLD, cr);
1230 if (error != 0)
1231 return (error);
1233 return (0);
1236 /* ARGSUSED */
1237 static int
1238 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1240 nvpair_t *pair;
1241 int error;
1243 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1244 pair = nvlist_next_nvpair(innvl, pair)) {
1245 char fsname[MAXNAMELEN];
1246 error = dmu_fsname(nvpair_name(pair), fsname);
1247 if (error != 0)
1248 return (error);
1249 error = zfs_secpolicy_write_perms(fsname,
1250 ZFS_DELEG_PERM_RELEASE, cr);
1251 if (error != 0)
1252 return (error);
1254 return (0);
1258 * Policy for allowing temporary snapshots to be taken or released
1260 static int
1261 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1264 * A temporary snapshot is the same as a snapshot,
1265 * hold, destroy and release all rolled into one.
1266 * Delegated diff alone is sufficient that we allow this.
1268 int error;
1270 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1271 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1272 return (0);
1274 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1275 if (error == 0)
1276 error = zfs_secpolicy_hold(zc, innvl, cr);
1277 if (error == 0)
1278 error = zfs_secpolicy_release(zc, innvl, cr);
1279 if (error == 0)
1280 error = zfs_secpolicy_destroy(zc, innvl, cr);
1281 return (error);
1285 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1287 static int
1288 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1290 char *packed;
1291 int error;
1292 nvlist_t *list = NULL;
1295 * Read in and unpack the user-supplied nvlist.
1297 if (size == 0)
1298 return (SET_ERROR(EINVAL));
1300 packed = kmem_alloc(size, KM_SLEEP);
1302 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1303 iflag)) != 0) {
1304 kmem_free(packed, size);
1305 return (error);
1308 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1309 kmem_free(packed, size);
1310 return (error);
1313 kmem_free(packed, size);
1315 *nvp = list;
1316 return (0);
1320 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1321 * Entries will be removed from the end of the nvlist, and one int32 entry
1322 * named "N_MORE_ERRORS" will be added indicating how many entries were
1323 * removed.
1325 static int
1326 nvlist_smush(nvlist_t *errors, size_t max)
1328 size_t size;
1330 size = fnvlist_size(errors);
1332 if (size > max) {
1333 nvpair_t *more_errors;
1334 int n = 0;
1336 if (max < 1024)
1337 return (SET_ERROR(ENOMEM));
1339 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1340 more_errors = nvlist_prev_nvpair(errors, NULL);
1342 do {
1343 nvpair_t *pair = nvlist_prev_nvpair(errors,
1344 more_errors);
1345 fnvlist_remove_nvpair(errors, pair);
1346 n++;
1347 size = fnvlist_size(errors);
1348 } while (size > max);
1350 fnvlist_remove_nvpair(errors, more_errors);
1351 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1352 ASSERT3U(fnvlist_size(errors), <=, max);
1355 return (0);
1358 static int
1359 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1361 char *packed = NULL;
1362 int error = 0;
1363 size_t size;
1365 size = fnvlist_size(nvl);
1367 if (size > zc->zc_nvlist_dst_size) {
1368 error = SET_ERROR(ENOMEM);
1369 } else {
1370 packed = fnvlist_pack(nvl, &size);
1371 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1372 size, zc->zc_iflags) != 0)
1373 error = SET_ERROR(EFAULT);
1374 fnvlist_pack_free(packed, size);
1377 zc->zc_nvlist_dst_size = size;
1378 zc->zc_nvlist_dst_filled = B_TRUE;
1379 return (error);
1382 static int
1383 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1385 objset_t *os;
1386 int error;
1388 error = dmu_objset_hold(dsname, FTAG, &os);
1389 if (error != 0)
1390 return (error);
1391 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1392 dmu_objset_rele(os, FTAG);
1393 return (SET_ERROR(EINVAL));
1396 mutex_enter(&os->os_user_ptr_lock);
1397 *zfvp = dmu_objset_get_user(os);
1398 if (*zfvp) {
1399 VFS_HOLD((*zfvp)->z_vfs);
1400 } else {
1401 error = SET_ERROR(ESRCH);
1403 mutex_exit(&os->os_user_ptr_lock);
1404 dmu_objset_rele(os, FTAG);
1405 return (error);
1409 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1410 * case its z_vfs will be NULL, and it will be opened as the owner.
1411 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1412 * which prevents all vnode ops from running.
1414 static int
1415 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1417 int error = 0;
1419 if (getzfsvfs(name, zfvp) != 0)
1420 error = zfsvfs_create(name, zfvp);
1421 if (error == 0) {
1422 rrw_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1423 RW_READER, tag);
1424 if ((*zfvp)->z_unmounted) {
1426 * XXX we could probably try again, since the unmounting
1427 * thread should be just about to disassociate the
1428 * objset from the zfsvfs.
1430 rrw_exit(&(*zfvp)->z_teardown_lock, tag);
1431 return (SET_ERROR(EBUSY));
1434 return (error);
1437 static void
1438 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1440 rrw_exit(&zfsvfs->z_teardown_lock, tag);
1442 if (zfsvfs->z_vfs) {
1443 VFS_RELE(zfsvfs->z_vfs);
1444 } else {
1445 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1446 zfsvfs_free(zfsvfs);
1450 static int
1451 zfs_ioc_pool_create(zfs_cmd_t *zc)
1453 int error;
1454 nvlist_t *config, *props = NULL;
1455 nvlist_t *rootprops = NULL;
1456 nvlist_t *zplprops = NULL;
1458 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1459 zc->zc_iflags, &config))
1460 return (error);
1462 if (zc->zc_nvlist_src_size != 0 && (error =
1463 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1464 zc->zc_iflags, &props))) {
1465 nvlist_free(config);
1466 return (error);
1469 if (props) {
1470 nvlist_t *nvl = NULL;
1471 uint64_t version = SPA_VERSION;
1473 (void) nvlist_lookup_uint64(props,
1474 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1475 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1476 error = SET_ERROR(EINVAL);
1477 goto pool_props_bad;
1479 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1480 if (nvl) {
1481 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1482 if (error != 0) {
1483 nvlist_free(config);
1484 nvlist_free(props);
1485 return (error);
1487 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1489 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1490 error = zfs_fill_zplprops_root(version, rootprops,
1491 zplprops, NULL);
1492 if (error != 0)
1493 goto pool_props_bad;
1496 error = spa_create(zc->zc_name, config, props, zplprops);
1499 * Set the remaining root properties
1501 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1502 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1503 (void) spa_destroy(zc->zc_name);
1505 pool_props_bad:
1506 nvlist_free(rootprops);
1507 nvlist_free(zplprops);
1508 nvlist_free(config);
1509 nvlist_free(props);
1511 return (error);
1514 static int
1515 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1517 int error;
1518 zfs_log_history(zc);
1519 error = spa_destroy(zc->zc_name);
1520 if (error == 0)
1521 zvol_remove_minors(zc->zc_name);
1522 return (error);
1525 static int
1526 zfs_ioc_pool_import(zfs_cmd_t *zc)
1528 nvlist_t *config, *props = NULL;
1529 uint64_t guid;
1530 int error;
1532 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1533 zc->zc_iflags, &config)) != 0)
1534 return (error);
1536 if (zc->zc_nvlist_src_size != 0 && (error =
1537 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1538 zc->zc_iflags, &props))) {
1539 nvlist_free(config);
1540 return (error);
1543 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1544 guid != zc->zc_guid)
1545 error = SET_ERROR(EINVAL);
1546 else
1547 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1549 if (zc->zc_nvlist_dst != 0) {
1550 int err;
1552 if ((err = put_nvlist(zc, config)) != 0)
1553 error = err;
1556 nvlist_free(config);
1558 if (props)
1559 nvlist_free(props);
1561 return (error);
1564 static int
1565 zfs_ioc_pool_export(zfs_cmd_t *zc)
1567 int error;
1568 boolean_t force = (boolean_t)zc->zc_cookie;
1569 boolean_t hardforce = (boolean_t)zc->zc_guid;
1571 zfs_log_history(zc);
1572 error = spa_export(zc->zc_name, NULL, force, hardforce);
1573 if (error == 0)
1574 zvol_remove_minors(zc->zc_name);
1575 return (error);
1578 static int
1579 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1581 nvlist_t *configs;
1582 int error;
1584 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1585 return (SET_ERROR(EEXIST));
1587 error = put_nvlist(zc, configs);
1589 nvlist_free(configs);
1591 return (error);
1595 * inputs:
1596 * zc_name name of the pool
1598 * outputs:
1599 * zc_cookie real errno
1600 * zc_nvlist_dst config nvlist
1601 * zc_nvlist_dst_size size of config nvlist
1603 static int
1604 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1606 nvlist_t *config;
1607 int error;
1608 int ret = 0;
1610 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1611 sizeof (zc->zc_value));
1613 if (config != NULL) {
1614 ret = put_nvlist(zc, config);
1615 nvlist_free(config);
1618 * The config may be present even if 'error' is non-zero.
1619 * In this case we return success, and preserve the real errno
1620 * in 'zc_cookie'.
1622 zc->zc_cookie = error;
1623 } else {
1624 ret = error;
1627 return (ret);
1631 * Try to import the given pool, returning pool stats as appropriate so that
1632 * user land knows which devices are available and overall pool health.
1634 static int
1635 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1637 nvlist_t *tryconfig, *config;
1638 int error;
1640 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1641 zc->zc_iflags, &tryconfig)) != 0)
1642 return (error);
1644 config = spa_tryimport(tryconfig);
1646 nvlist_free(tryconfig);
1648 if (config == NULL)
1649 return (SET_ERROR(EINVAL));
1651 error = put_nvlist(zc, config);
1652 nvlist_free(config);
1654 return (error);
1658 * inputs:
1659 * zc_name name of the pool
1660 * zc_cookie scan func (pool_scan_func_t)
1662 static int
1663 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1665 spa_t *spa;
1666 int error;
1668 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1669 return (error);
1671 if (zc->zc_cookie == POOL_SCAN_NONE)
1672 error = spa_scan_stop(spa);
1673 else
1674 error = spa_scan(spa, zc->zc_cookie);
1676 spa_close(spa, FTAG);
1678 return (error);
1681 static int
1682 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1684 spa_t *spa;
1685 int error;
1687 error = spa_open(zc->zc_name, &spa, FTAG);
1688 if (error == 0) {
1689 spa_freeze(spa);
1690 spa_close(spa, FTAG);
1692 return (error);
1695 static int
1696 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1698 spa_t *spa;
1699 int error;
1701 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1702 return (error);
1704 if (zc->zc_cookie < spa_version(spa) ||
1705 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1706 spa_close(spa, FTAG);
1707 return (SET_ERROR(EINVAL));
1710 spa_upgrade(spa, zc->zc_cookie);
1711 spa_close(spa, FTAG);
1713 return (error);
1716 static int
1717 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1719 spa_t *spa;
1720 char *hist_buf;
1721 uint64_t size;
1722 int error;
1724 if ((size = zc->zc_history_len) == 0)
1725 return (SET_ERROR(EINVAL));
1727 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1728 return (error);
1730 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1731 spa_close(spa, FTAG);
1732 return (SET_ERROR(ENOTSUP));
1735 hist_buf = kmem_alloc(size, KM_SLEEP);
1736 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1737 &zc->zc_history_len, hist_buf)) == 0) {
1738 error = ddi_copyout(hist_buf,
1739 (void *)(uintptr_t)zc->zc_history,
1740 zc->zc_history_len, zc->zc_iflags);
1743 spa_close(spa, FTAG);
1744 kmem_free(hist_buf, size);
1745 return (error);
1748 static int
1749 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1751 spa_t *spa;
1752 int error;
1754 error = spa_open(zc->zc_name, &spa, FTAG);
1755 if (error == 0) {
1756 error = spa_change_guid(spa);
1757 spa_close(spa, FTAG);
1759 return (error);
1762 static int
1763 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1765 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1769 * inputs:
1770 * zc_name name of filesystem
1771 * zc_obj object to find
1773 * outputs:
1774 * zc_value name of object
1776 static int
1777 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1779 objset_t *os;
1780 int error;
1782 /* XXX reading from objset not owned */
1783 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1784 return (error);
1785 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1786 dmu_objset_rele(os, FTAG);
1787 return (SET_ERROR(EINVAL));
1789 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1790 sizeof (zc->zc_value));
1791 dmu_objset_rele(os, FTAG);
1793 return (error);
1797 * inputs:
1798 * zc_name name of filesystem
1799 * zc_obj object to find
1801 * outputs:
1802 * zc_stat stats on object
1803 * zc_value path to object
1805 static int
1806 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1808 objset_t *os;
1809 int error;
1811 /* XXX reading from objset not owned */
1812 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1813 return (error);
1814 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1815 dmu_objset_rele(os, FTAG);
1816 return (SET_ERROR(EINVAL));
1818 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1819 sizeof (zc->zc_value));
1820 dmu_objset_rele(os, FTAG);
1822 return (error);
1825 static int
1826 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1828 spa_t *spa;
1829 int error;
1830 nvlist_t *config, **l2cache, **spares;
1831 uint_t nl2cache = 0, nspares = 0;
1833 error = spa_open(zc->zc_name, &spa, FTAG);
1834 if (error != 0)
1835 return (error);
1837 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1838 zc->zc_iflags, &config);
1839 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1840 &l2cache, &nl2cache);
1842 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1843 &spares, &nspares);
1846 * A root pool with concatenated devices is not supported.
1847 * Thus, can not add a device to a root pool.
1849 * Intent log device can not be added to a rootpool because
1850 * during mountroot, zil is replayed, a seperated log device
1851 * can not be accessed during the mountroot time.
1853 * l2cache and spare devices are ok to be added to a rootpool.
1855 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1856 nvlist_free(config);
1857 spa_close(spa, FTAG);
1858 return (SET_ERROR(EDOM));
1861 if (error == 0) {
1862 error = spa_vdev_add(spa, config);
1863 nvlist_free(config);
1865 spa_close(spa, FTAG);
1866 return (error);
1870 * inputs:
1871 * zc_name name of the pool
1872 * zc_nvlist_conf nvlist of devices to remove
1873 * zc_cookie to stop the remove?
1875 static int
1876 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1878 spa_t *spa;
1879 int error;
1881 error = spa_open(zc->zc_name, &spa, FTAG);
1882 if (error != 0)
1883 return (error);
1884 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1885 spa_close(spa, FTAG);
1886 return (error);
1889 static int
1890 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1892 spa_t *spa;
1893 int error;
1894 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1896 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1897 return (error);
1898 switch (zc->zc_cookie) {
1899 case VDEV_STATE_ONLINE:
1900 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1901 break;
1903 case VDEV_STATE_OFFLINE:
1904 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1905 break;
1907 case VDEV_STATE_FAULTED:
1908 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1909 zc->zc_obj != VDEV_AUX_EXTERNAL)
1910 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1912 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1913 break;
1915 case VDEV_STATE_DEGRADED:
1916 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1917 zc->zc_obj != VDEV_AUX_EXTERNAL)
1918 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1920 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1921 break;
1923 default:
1924 error = SET_ERROR(EINVAL);
1926 zc->zc_cookie = newstate;
1927 spa_close(spa, FTAG);
1928 return (error);
1931 static int
1932 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1934 spa_t *spa;
1935 int replacing = zc->zc_cookie;
1936 nvlist_t *config;
1937 int error;
1939 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1940 return (error);
1942 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1943 zc->zc_iflags, &config)) == 0) {
1944 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1945 nvlist_free(config);
1948 spa_close(spa, FTAG);
1949 return (error);
1952 static int
1953 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1955 spa_t *spa;
1956 int error;
1958 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1959 return (error);
1961 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1963 spa_close(spa, FTAG);
1964 return (error);
1967 static int
1968 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1970 spa_t *spa;
1971 nvlist_t *config, *props = NULL;
1972 int error;
1973 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1975 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1976 return (error);
1978 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1979 zc->zc_iflags, &config)) {
1980 spa_close(spa, FTAG);
1981 return (error);
1984 if (zc->zc_nvlist_src_size != 0 && (error =
1985 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1986 zc->zc_iflags, &props))) {
1987 spa_close(spa, FTAG);
1988 nvlist_free(config);
1989 return (error);
1992 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1994 spa_close(spa, FTAG);
1996 nvlist_free(config);
1997 nvlist_free(props);
1999 return (error);
2002 static int
2003 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2005 spa_t *spa;
2006 char *path = zc->zc_value;
2007 uint64_t guid = zc->zc_guid;
2008 int error;
2010 error = spa_open(zc->zc_name, &spa, FTAG);
2011 if (error != 0)
2012 return (error);
2014 error = spa_vdev_setpath(spa, guid, path);
2015 spa_close(spa, FTAG);
2016 return (error);
2019 static int
2020 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2022 spa_t *spa;
2023 char *fru = zc->zc_value;
2024 uint64_t guid = zc->zc_guid;
2025 int error;
2027 error = spa_open(zc->zc_name, &spa, FTAG);
2028 if (error != 0)
2029 return (error);
2031 error = spa_vdev_setfru(spa, guid, fru);
2032 spa_close(spa, FTAG);
2033 return (error);
2036 static int
2037 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2039 int error = 0;
2040 nvlist_t *nv;
2042 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2044 if (zc->zc_nvlist_dst != 0 &&
2045 (error = dsl_prop_get_all(os, &nv)) == 0) {
2046 dmu_objset_stats(os, nv);
2048 * NB: zvol_get_stats() will read the objset contents,
2049 * which we aren't supposed to do with a
2050 * DS_MODE_USER hold, because it could be
2051 * inconsistent. So this is a bit of a workaround...
2052 * XXX reading with out owning
2054 if (!zc->zc_objset_stats.dds_inconsistent &&
2055 dmu_objset_type(os) == DMU_OST_ZVOL) {
2056 error = zvol_get_stats(os, nv);
2057 if (error == EIO)
2058 return (error);
2059 VERIFY0(error);
2061 error = put_nvlist(zc, nv);
2062 nvlist_free(nv);
2065 return (error);
2069 * inputs:
2070 * zc_name name of filesystem
2071 * zc_nvlist_dst_size size of buffer for property nvlist
2073 * outputs:
2074 * zc_objset_stats stats
2075 * zc_nvlist_dst property nvlist
2076 * zc_nvlist_dst_size size of property nvlist
2078 static int
2079 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2081 objset_t *os;
2082 int error;
2084 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2085 if (error == 0) {
2086 error = zfs_ioc_objset_stats_impl(zc, os);
2087 dmu_objset_rele(os, FTAG);
2090 return (error);
2094 * inputs:
2095 * zc_name name of filesystem
2096 * zc_nvlist_dst_size size of buffer for property nvlist
2098 * outputs:
2099 * zc_nvlist_dst received property nvlist
2100 * zc_nvlist_dst_size size of received property nvlist
2102 * Gets received properties (distinct from local properties on or after
2103 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2104 * local property values.
2106 static int
2107 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2109 int error = 0;
2110 nvlist_t *nv;
2113 * Without this check, we would return local property values if the
2114 * caller has not already received properties on or after
2115 * SPA_VERSION_RECVD_PROPS.
2117 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2118 return (SET_ERROR(ENOTSUP));
2120 if (zc->zc_nvlist_dst != 0 &&
2121 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2122 error = put_nvlist(zc, nv);
2123 nvlist_free(nv);
2126 return (error);
2129 static int
2130 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2132 uint64_t value;
2133 int error;
2136 * zfs_get_zplprop() will either find a value or give us
2137 * the default value (if there is one).
2139 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2140 return (error);
2141 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2142 return (0);
2146 * inputs:
2147 * zc_name name of filesystem
2148 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2150 * outputs:
2151 * zc_nvlist_dst zpl property nvlist
2152 * zc_nvlist_dst_size size of zpl property nvlist
2154 static int
2155 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2157 objset_t *os;
2158 int err;
2160 /* XXX reading without owning */
2161 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2162 return (err);
2164 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2167 * NB: nvl_add_zplprop() will read the objset contents,
2168 * which we aren't supposed to do with a DS_MODE_USER
2169 * hold, because it could be inconsistent.
2171 if (zc->zc_nvlist_dst != NULL &&
2172 !zc->zc_objset_stats.dds_inconsistent &&
2173 dmu_objset_type(os) == DMU_OST_ZFS) {
2174 nvlist_t *nv;
2176 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2177 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2178 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2179 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2180 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2181 err = put_nvlist(zc, nv);
2182 nvlist_free(nv);
2183 } else {
2184 err = SET_ERROR(ENOENT);
2186 dmu_objset_rele(os, FTAG);
2187 return (err);
2190 static boolean_t
2191 dataset_name_hidden(const char *name)
2194 * Skip over datasets that are not visible in this zone,
2195 * internal datasets (which have a $ in their name), and
2196 * temporary datasets (which have a % in their name).
2198 if (strchr(name, '$') != NULL)
2199 return (B_TRUE);
2200 if (strchr(name, '%') != NULL)
2201 return (B_TRUE);
2202 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2203 return (B_TRUE);
2204 return (B_FALSE);
2208 * inputs:
2209 * zc_name name of filesystem
2210 * zc_cookie zap cursor
2211 * zc_nvlist_dst_size size of buffer for property nvlist
2213 * outputs:
2214 * zc_name name of next filesystem
2215 * zc_cookie zap cursor
2216 * zc_objset_stats stats
2217 * zc_nvlist_dst property nvlist
2218 * zc_nvlist_dst_size size of property nvlist
2220 static int
2221 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2223 objset_t *os;
2224 int error;
2225 char *p;
2226 size_t orig_len = strlen(zc->zc_name);
2228 top:
2229 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2230 if (error == ENOENT)
2231 error = SET_ERROR(ESRCH);
2232 return (error);
2235 p = strrchr(zc->zc_name, '/');
2236 if (p == NULL || p[1] != '\0')
2237 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2238 p = zc->zc_name + strlen(zc->zc_name);
2240 do {
2241 error = dmu_dir_list_next(os,
2242 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2243 NULL, &zc->zc_cookie);
2244 if (error == ENOENT)
2245 error = SET_ERROR(ESRCH);
2246 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2247 dmu_objset_rele(os, FTAG);
2250 * If it's an internal dataset (ie. with a '$' in its name),
2251 * don't try to get stats for it, otherwise we'll return ENOENT.
2253 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2254 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2255 if (error == ENOENT) {
2256 /* We lost a race with destroy, get the next one. */
2257 zc->zc_name[orig_len] = '\0';
2258 goto top;
2261 return (error);
2265 * inputs:
2266 * zc_name name of filesystem
2267 * zc_cookie zap cursor
2268 * zc_nvlist_dst_size size of buffer for property nvlist
2270 * outputs:
2271 * zc_name name of next snapshot
2272 * zc_objset_stats stats
2273 * zc_nvlist_dst property nvlist
2274 * zc_nvlist_dst_size size of property nvlist
2276 static int
2277 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2279 objset_t *os;
2280 int error;
2282 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2283 if (error != 0) {
2284 return (error == ENOENT ? ESRCH : error);
2288 * A dataset name of maximum length cannot have any snapshots,
2289 * so exit immediately.
2291 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2292 dmu_objset_rele(os, FTAG);
2293 return (SET_ERROR(ESRCH));
2296 error = dmu_snapshot_list_next(os,
2297 sizeof (zc->zc_name) - strlen(zc->zc_name),
2298 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2299 NULL);
2301 if (error == 0) {
2302 dsl_dataset_t *ds;
2303 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2305 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2306 if (error == 0) {
2307 objset_t *ossnap;
2309 error = dmu_objset_from_ds(ds, &ossnap);
2310 if (error == 0)
2311 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2312 dsl_dataset_rele(ds, FTAG);
2314 } else if (error == ENOENT) {
2315 error = SET_ERROR(ESRCH);
2318 dmu_objset_rele(os, FTAG);
2319 /* if we failed, undo the @ that we tacked on to zc_name */
2320 if (error != 0)
2321 *strchr(zc->zc_name, '@') = '\0';
2322 return (error);
2325 static int
2326 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2328 const char *propname = nvpair_name(pair);
2329 uint64_t *valary;
2330 unsigned int vallen;
2331 const char *domain;
2332 char *dash;
2333 zfs_userquota_prop_t type;
2334 uint64_t rid;
2335 uint64_t quota;
2336 zfsvfs_t *zfsvfs;
2337 int err;
2339 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2340 nvlist_t *attrs;
2341 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2342 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2343 &pair) != 0)
2344 return (SET_ERROR(EINVAL));
2348 * A correctly constructed propname is encoded as
2349 * userquota@<rid>-<domain>.
2351 if ((dash = strchr(propname, '-')) == NULL ||
2352 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2353 vallen != 3)
2354 return (SET_ERROR(EINVAL));
2356 domain = dash + 1;
2357 type = valary[0];
2358 rid = valary[1];
2359 quota = valary[2];
2361 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2362 if (err == 0) {
2363 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2364 zfsvfs_rele(zfsvfs, FTAG);
2367 return (err);
2371 * If the named property is one that has a special function to set its value,
2372 * return 0 on success and a positive error code on failure; otherwise if it is
2373 * not one of the special properties handled by this function, return -1.
2375 * XXX: It would be better for callers of the property interface if we handled
2376 * these special cases in dsl_prop.c (in the dsl layer).
2378 static int
2379 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2380 nvpair_t *pair)
2382 const char *propname = nvpair_name(pair);
2383 zfs_prop_t prop = zfs_name_to_prop(propname);
2384 uint64_t intval;
2385 int err;
2387 if (prop == ZPROP_INVAL) {
2388 if (zfs_prop_userquota(propname))
2389 return (zfs_prop_set_userquota(dsname, pair));
2390 return (-1);
2393 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2394 nvlist_t *attrs;
2395 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2396 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2397 &pair) == 0);
2400 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2401 return (-1);
2403 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2405 switch (prop) {
2406 case ZFS_PROP_QUOTA:
2407 err = dsl_dir_set_quota(dsname, source, intval);
2408 break;
2409 case ZFS_PROP_REFQUOTA:
2410 err = dsl_dataset_set_refquota(dsname, source, intval);
2411 break;
2412 case ZFS_PROP_FILESYSTEM_LIMIT:
2413 case ZFS_PROP_SNAPSHOT_LIMIT:
2414 if (intval == UINT64_MAX) {
2415 /* clearing the limit, just do it */
2416 err = 0;
2417 } else {
2418 err = dsl_dir_activate_fs_ss_limit(dsname);
2421 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2422 * default path to set the value in the nvlist.
2424 if (err == 0)
2425 err = -1;
2426 break;
2427 case ZFS_PROP_RESERVATION:
2428 err = dsl_dir_set_reservation(dsname, source, intval);
2429 break;
2430 case ZFS_PROP_REFRESERVATION:
2431 err = dsl_dataset_set_refreservation(dsname, source, intval);
2432 break;
2433 case ZFS_PROP_VOLSIZE:
2434 err = zvol_set_volsize(dsname, intval);
2435 break;
2436 case ZFS_PROP_VERSION:
2438 zfsvfs_t *zfsvfs;
2440 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2441 break;
2443 err = zfs_set_version(zfsvfs, intval);
2444 zfsvfs_rele(zfsvfs, FTAG);
2446 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2447 zfs_cmd_t *zc;
2449 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2450 (void) strcpy(zc->zc_name, dsname);
2451 (void) zfs_ioc_userspace_upgrade(zc);
2452 kmem_free(zc, sizeof (zfs_cmd_t));
2454 break;
2456 case ZFS_PROP_COMPRESSION:
2458 if (intval == ZIO_COMPRESS_LZ4) {
2459 spa_t *spa;
2461 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2462 return (err);
2465 * Setting the LZ4 compression algorithm activates
2466 * the feature.
2468 if (!spa_feature_is_active(spa,
2469 SPA_FEATURE_LZ4_COMPRESS)) {
2470 if ((err = zfs_prop_activate_feature(spa,
2471 SPA_FEATURE_LZ4_COMPRESS)) != 0) {
2472 spa_close(spa, FTAG);
2473 return (err);
2477 spa_close(spa, FTAG);
2480 * We still want the default set action to be performed in the
2481 * caller, we only performed zfeature settings here.
2483 err = -1;
2484 break;
2487 default:
2488 err = -1;
2491 return (err);
2495 * This function is best effort. If it fails to set any of the given properties,
2496 * it continues to set as many as it can and returns the last error
2497 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2498 * with the list of names of all the properties that failed along with the
2499 * corresponding error numbers.
2501 * If every property is set successfully, zero is returned and errlist is not
2502 * modified.
2505 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2506 nvlist_t *errlist)
2508 nvpair_t *pair;
2509 nvpair_t *propval;
2510 int rv = 0;
2511 uint64_t intval;
2512 char *strval;
2513 nvlist_t *genericnvl = fnvlist_alloc();
2514 nvlist_t *retrynvl = fnvlist_alloc();
2516 retry:
2517 pair = NULL;
2518 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2519 const char *propname = nvpair_name(pair);
2520 zfs_prop_t prop = zfs_name_to_prop(propname);
2521 int err = 0;
2523 /* decode the property value */
2524 propval = pair;
2525 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2526 nvlist_t *attrs;
2527 attrs = fnvpair_value_nvlist(pair);
2528 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2529 &propval) != 0)
2530 err = SET_ERROR(EINVAL);
2533 /* Validate value type */
2534 if (err == 0 && prop == ZPROP_INVAL) {
2535 if (zfs_prop_user(propname)) {
2536 if (nvpair_type(propval) != DATA_TYPE_STRING)
2537 err = SET_ERROR(EINVAL);
2538 } else if (zfs_prop_userquota(propname)) {
2539 if (nvpair_type(propval) !=
2540 DATA_TYPE_UINT64_ARRAY)
2541 err = SET_ERROR(EINVAL);
2542 } else {
2543 err = SET_ERROR(EINVAL);
2545 } else if (err == 0) {
2546 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2547 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2548 err = SET_ERROR(EINVAL);
2549 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2550 const char *unused;
2552 intval = fnvpair_value_uint64(propval);
2554 switch (zfs_prop_get_type(prop)) {
2555 case PROP_TYPE_NUMBER:
2556 break;
2557 case PROP_TYPE_STRING:
2558 err = SET_ERROR(EINVAL);
2559 break;
2560 case PROP_TYPE_INDEX:
2561 if (zfs_prop_index_to_string(prop,
2562 intval, &unused) != 0)
2563 err = SET_ERROR(EINVAL);
2564 break;
2565 default:
2566 cmn_err(CE_PANIC,
2567 "unknown property type");
2569 } else {
2570 err = SET_ERROR(EINVAL);
2574 /* Validate permissions */
2575 if (err == 0)
2576 err = zfs_check_settable(dsname, pair, CRED());
2578 if (err == 0) {
2579 err = zfs_prop_set_special(dsname, source, pair);
2580 if (err == -1) {
2582 * For better performance we build up a list of
2583 * properties to set in a single transaction.
2585 err = nvlist_add_nvpair(genericnvl, pair);
2586 } else if (err != 0 && nvl != retrynvl) {
2588 * This may be a spurious error caused by
2589 * receiving quota and reservation out of order.
2590 * Try again in a second pass.
2592 err = nvlist_add_nvpair(retrynvl, pair);
2596 if (err != 0) {
2597 if (errlist != NULL)
2598 fnvlist_add_int32(errlist, propname, err);
2599 rv = err;
2603 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2604 nvl = retrynvl;
2605 goto retry;
2608 if (!nvlist_empty(genericnvl) &&
2609 dsl_props_set(dsname, source, genericnvl) != 0) {
2611 * If this fails, we still want to set as many properties as we
2612 * can, so try setting them individually.
2614 pair = NULL;
2615 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2616 const char *propname = nvpair_name(pair);
2617 int err = 0;
2619 propval = pair;
2620 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2621 nvlist_t *attrs;
2622 attrs = fnvpair_value_nvlist(pair);
2623 propval = fnvlist_lookup_nvpair(attrs,
2624 ZPROP_VALUE);
2627 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2628 strval = fnvpair_value_string(propval);
2629 err = dsl_prop_set_string(dsname, propname,
2630 source, strval);
2631 } else {
2632 intval = fnvpair_value_uint64(propval);
2633 err = dsl_prop_set_int(dsname, propname, source,
2634 intval);
2637 if (err != 0) {
2638 if (errlist != NULL) {
2639 fnvlist_add_int32(errlist, propname,
2640 err);
2642 rv = err;
2646 nvlist_free(genericnvl);
2647 nvlist_free(retrynvl);
2649 return (rv);
2653 * Check that all the properties are valid user properties.
2655 static int
2656 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2658 nvpair_t *pair = NULL;
2659 int error = 0;
2661 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2662 const char *propname = nvpair_name(pair);
2664 if (!zfs_prop_user(propname) ||
2665 nvpair_type(pair) != DATA_TYPE_STRING)
2666 return (SET_ERROR(EINVAL));
2668 if (error = zfs_secpolicy_write_perms(fsname,
2669 ZFS_DELEG_PERM_USERPROP, CRED()))
2670 return (error);
2672 if (strlen(propname) >= ZAP_MAXNAMELEN)
2673 return (SET_ERROR(ENAMETOOLONG));
2675 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2676 return (E2BIG);
2678 return (0);
2681 static void
2682 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2684 nvpair_t *pair;
2686 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2688 pair = NULL;
2689 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2690 if (nvlist_exists(skipped, nvpair_name(pair)))
2691 continue;
2693 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2697 static int
2698 clear_received_props(const char *dsname, nvlist_t *props,
2699 nvlist_t *skipped)
2701 int err = 0;
2702 nvlist_t *cleared_props = NULL;
2703 props_skip(props, skipped, &cleared_props);
2704 if (!nvlist_empty(cleared_props)) {
2706 * Acts on local properties until the dataset has received
2707 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2709 zprop_source_t flags = (ZPROP_SRC_NONE |
2710 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2711 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2713 nvlist_free(cleared_props);
2714 return (err);
2718 * inputs:
2719 * zc_name name of filesystem
2720 * zc_value name of property to set
2721 * zc_nvlist_src{_size} nvlist of properties to apply
2722 * zc_cookie received properties flag
2724 * outputs:
2725 * zc_nvlist_dst{_size} error for each unapplied received property
2727 static int
2728 zfs_ioc_set_prop(zfs_cmd_t *zc)
2730 nvlist_t *nvl;
2731 boolean_t received = zc->zc_cookie;
2732 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2733 ZPROP_SRC_LOCAL);
2734 nvlist_t *errors;
2735 int error;
2737 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2738 zc->zc_iflags, &nvl)) != 0)
2739 return (error);
2741 if (received) {
2742 nvlist_t *origprops;
2744 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2745 (void) clear_received_props(zc->zc_name,
2746 origprops, nvl);
2747 nvlist_free(origprops);
2750 error = dsl_prop_set_hasrecvd(zc->zc_name);
2753 errors = fnvlist_alloc();
2754 if (error == 0)
2755 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2757 if (zc->zc_nvlist_dst != NULL && errors != NULL) {
2758 (void) put_nvlist(zc, errors);
2761 nvlist_free(errors);
2762 nvlist_free(nvl);
2763 return (error);
2767 * inputs:
2768 * zc_name name of filesystem
2769 * zc_value name of property to inherit
2770 * zc_cookie revert to received value if TRUE
2772 * outputs: none
2774 static int
2775 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2777 const char *propname = zc->zc_value;
2778 zfs_prop_t prop = zfs_name_to_prop(propname);
2779 boolean_t received = zc->zc_cookie;
2780 zprop_source_t source = (received
2781 ? ZPROP_SRC_NONE /* revert to received value, if any */
2782 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2784 if (received) {
2785 nvlist_t *dummy;
2786 nvpair_t *pair;
2787 zprop_type_t type;
2788 int err;
2791 * zfs_prop_set_special() expects properties in the form of an
2792 * nvpair with type info.
2794 if (prop == ZPROP_INVAL) {
2795 if (!zfs_prop_user(propname))
2796 return (SET_ERROR(EINVAL));
2798 type = PROP_TYPE_STRING;
2799 } else if (prop == ZFS_PROP_VOLSIZE ||
2800 prop == ZFS_PROP_VERSION) {
2801 return (SET_ERROR(EINVAL));
2802 } else {
2803 type = zfs_prop_get_type(prop);
2806 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2808 switch (type) {
2809 case PROP_TYPE_STRING:
2810 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2811 break;
2812 case PROP_TYPE_NUMBER:
2813 case PROP_TYPE_INDEX:
2814 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2815 break;
2816 default:
2817 nvlist_free(dummy);
2818 return (SET_ERROR(EINVAL));
2821 pair = nvlist_next_nvpair(dummy, NULL);
2822 err = zfs_prop_set_special(zc->zc_name, source, pair);
2823 nvlist_free(dummy);
2824 if (err != -1)
2825 return (err); /* special property already handled */
2826 } else {
2828 * Only check this in the non-received case. We want to allow
2829 * 'inherit -S' to revert non-inheritable properties like quota
2830 * and reservation to the received or default values even though
2831 * they are not considered inheritable.
2833 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2834 return (SET_ERROR(EINVAL));
2837 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2838 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2841 static int
2842 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2844 nvlist_t *props;
2845 spa_t *spa;
2846 int error;
2847 nvpair_t *pair;
2849 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2850 zc->zc_iflags, &props))
2851 return (error);
2854 * If the only property is the configfile, then just do a spa_lookup()
2855 * to handle the faulted case.
2857 pair = nvlist_next_nvpair(props, NULL);
2858 if (pair != NULL && strcmp(nvpair_name(pair),
2859 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2860 nvlist_next_nvpair(props, pair) == NULL) {
2861 mutex_enter(&spa_namespace_lock);
2862 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2863 spa_configfile_set(spa, props, B_FALSE);
2864 spa_config_sync(spa, B_FALSE, B_TRUE);
2866 mutex_exit(&spa_namespace_lock);
2867 if (spa != NULL) {
2868 nvlist_free(props);
2869 return (0);
2873 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2874 nvlist_free(props);
2875 return (error);
2878 error = spa_prop_set(spa, props);
2880 nvlist_free(props);
2881 spa_close(spa, FTAG);
2883 return (error);
2886 static int
2887 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2889 spa_t *spa;
2890 int error;
2891 nvlist_t *nvp = NULL;
2893 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2895 * If the pool is faulted, there may be properties we can still
2896 * get (such as altroot and cachefile), so attempt to get them
2897 * anyway.
2899 mutex_enter(&spa_namespace_lock);
2900 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2901 error = spa_prop_get(spa, &nvp);
2902 mutex_exit(&spa_namespace_lock);
2903 } else {
2904 error = spa_prop_get(spa, &nvp);
2905 spa_close(spa, FTAG);
2908 if (error == 0 && zc->zc_nvlist_dst != NULL)
2909 error = put_nvlist(zc, nvp);
2910 else
2911 error = SET_ERROR(EFAULT);
2913 nvlist_free(nvp);
2914 return (error);
2918 * inputs:
2919 * zc_name name of filesystem
2920 * zc_nvlist_src{_size} nvlist of delegated permissions
2921 * zc_perm_action allow/unallow flag
2923 * outputs: none
2925 static int
2926 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2928 int error;
2929 nvlist_t *fsaclnv = NULL;
2931 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2932 zc->zc_iflags, &fsaclnv)) != 0)
2933 return (error);
2936 * Verify nvlist is constructed correctly
2938 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2939 nvlist_free(fsaclnv);
2940 return (SET_ERROR(EINVAL));
2944 * If we don't have PRIV_SYS_MOUNT, then validate
2945 * that user is allowed to hand out each permission in
2946 * the nvlist(s)
2949 error = secpolicy_zfs(CRED());
2950 if (error != 0) {
2951 if (zc->zc_perm_action == B_FALSE) {
2952 error = dsl_deleg_can_allow(zc->zc_name,
2953 fsaclnv, CRED());
2954 } else {
2955 error = dsl_deleg_can_unallow(zc->zc_name,
2956 fsaclnv, CRED());
2960 if (error == 0)
2961 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2963 nvlist_free(fsaclnv);
2964 return (error);
2968 * inputs:
2969 * zc_name name of filesystem
2971 * outputs:
2972 * zc_nvlist_src{_size} nvlist of delegated permissions
2974 static int
2975 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2977 nvlist_t *nvp;
2978 int error;
2980 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2981 error = put_nvlist(zc, nvp);
2982 nvlist_free(nvp);
2985 return (error);
2989 * Search the vfs list for a specified resource. Returns a pointer to it
2990 * or NULL if no suitable entry is found. The caller of this routine
2991 * is responsible for releasing the returned vfs pointer.
2993 static vfs_t *
2994 zfs_get_vfs(const char *resource)
2996 struct vfs *vfsp;
2997 struct vfs *vfs_found = NULL;
2999 vfs_list_read_lock();
3000 vfsp = rootvfs;
3001 do {
3002 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
3003 VFS_HOLD(vfsp);
3004 vfs_found = vfsp;
3005 break;
3007 vfsp = vfsp->vfs_next;
3008 } while (vfsp != rootvfs);
3009 vfs_list_unlock();
3010 return (vfs_found);
3013 /* ARGSUSED */
3014 static void
3015 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3017 zfs_creat_t *zct = arg;
3019 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3022 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3025 * inputs:
3026 * os parent objset pointer (NULL if root fs)
3027 * fuids_ok fuids allowed in this version of the spa?
3028 * sa_ok SAs allowed in this version of the spa?
3029 * createprops list of properties requested by creator
3031 * outputs:
3032 * zplprops values for the zplprops we attach to the master node object
3033 * is_ci true if requested file system will be purely case-insensitive
3035 * Determine the settings for utf8only, normalization and
3036 * casesensitivity. Specific values may have been requested by the
3037 * creator and/or we can inherit values from the parent dataset. If
3038 * the file system is of too early a vintage, a creator can not
3039 * request settings for these properties, even if the requested
3040 * setting is the default value. We don't actually want to create dsl
3041 * properties for these, so remove them from the source nvlist after
3042 * processing.
3044 static int
3045 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3046 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3047 nvlist_t *zplprops, boolean_t *is_ci)
3049 uint64_t sense = ZFS_PROP_UNDEFINED;
3050 uint64_t norm = ZFS_PROP_UNDEFINED;
3051 uint64_t u8 = ZFS_PROP_UNDEFINED;
3053 ASSERT(zplprops != NULL);
3056 * Pull out creator prop choices, if any.
3058 if (createprops) {
3059 (void) nvlist_lookup_uint64(createprops,
3060 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3061 (void) nvlist_lookup_uint64(createprops,
3062 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3063 (void) nvlist_remove_all(createprops,
3064 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3065 (void) nvlist_lookup_uint64(createprops,
3066 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3067 (void) nvlist_remove_all(createprops,
3068 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3069 (void) nvlist_lookup_uint64(createprops,
3070 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3071 (void) nvlist_remove_all(createprops,
3072 zfs_prop_to_name(ZFS_PROP_CASE));
3076 * If the zpl version requested is whacky or the file system
3077 * or pool is version is too "young" to support normalization
3078 * and the creator tried to set a value for one of the props,
3079 * error out.
3081 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3082 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3083 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3084 (zplver < ZPL_VERSION_NORMALIZATION &&
3085 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3086 sense != ZFS_PROP_UNDEFINED)))
3087 return (SET_ERROR(ENOTSUP));
3090 * Put the version in the zplprops
3092 VERIFY(nvlist_add_uint64(zplprops,
3093 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3095 if (norm == ZFS_PROP_UNDEFINED)
3096 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3097 VERIFY(nvlist_add_uint64(zplprops,
3098 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3101 * If we're normalizing, names must always be valid UTF-8 strings.
3103 if (norm)
3104 u8 = 1;
3105 if (u8 == ZFS_PROP_UNDEFINED)
3106 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3107 VERIFY(nvlist_add_uint64(zplprops,
3108 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3110 if (sense == ZFS_PROP_UNDEFINED)
3111 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3112 VERIFY(nvlist_add_uint64(zplprops,
3113 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3115 if (is_ci)
3116 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3118 return (0);
3121 static int
3122 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3123 nvlist_t *zplprops, boolean_t *is_ci)
3125 boolean_t fuids_ok, sa_ok;
3126 uint64_t zplver = ZPL_VERSION;
3127 objset_t *os = NULL;
3128 char parentname[MAXNAMELEN];
3129 char *cp;
3130 spa_t *spa;
3131 uint64_t spa_vers;
3132 int error;
3134 (void) strlcpy(parentname, dataset, sizeof (parentname));
3135 cp = strrchr(parentname, '/');
3136 ASSERT(cp != NULL);
3137 cp[0] = '\0';
3139 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3140 return (error);
3142 spa_vers = spa_version(spa);
3143 spa_close(spa, FTAG);
3145 zplver = zfs_zpl_version_map(spa_vers);
3146 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3147 sa_ok = (zplver >= ZPL_VERSION_SA);
3150 * Open parent object set so we can inherit zplprop values.
3152 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3153 return (error);
3155 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3156 zplprops, is_ci);
3157 dmu_objset_rele(os, FTAG);
3158 return (error);
3161 static int
3162 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3163 nvlist_t *zplprops, boolean_t *is_ci)
3165 boolean_t fuids_ok;
3166 boolean_t sa_ok;
3167 uint64_t zplver = ZPL_VERSION;
3168 int error;
3170 zplver = zfs_zpl_version_map(spa_vers);
3171 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3172 sa_ok = (zplver >= ZPL_VERSION_SA);
3174 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3175 createprops, zplprops, is_ci);
3176 return (error);
3180 * innvl: {
3181 * "type" -> dmu_objset_type_t (int32)
3182 * (optional) "props" -> { prop -> value }
3185 * outnvl: propname -> error code (int32)
3187 static int
3188 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3190 int error = 0;
3191 zfs_creat_t zct = { 0 };
3192 nvlist_t *nvprops = NULL;
3193 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3194 int32_t type32;
3195 dmu_objset_type_t type;
3196 boolean_t is_insensitive = B_FALSE;
3198 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3199 return (SET_ERROR(EINVAL));
3200 type = type32;
3201 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3203 switch (type) {
3204 case DMU_OST_ZFS:
3205 cbfunc = zfs_create_cb;
3206 break;
3208 case DMU_OST_ZVOL:
3209 cbfunc = zvol_create_cb;
3210 break;
3212 default:
3213 cbfunc = NULL;
3214 break;
3216 if (strchr(fsname, '@') ||
3217 strchr(fsname, '%'))
3218 return (SET_ERROR(EINVAL));
3220 zct.zct_props = nvprops;
3222 if (cbfunc == NULL)
3223 return (SET_ERROR(EINVAL));
3225 if (type == DMU_OST_ZVOL) {
3226 uint64_t volsize, volblocksize;
3228 if (nvprops == NULL)
3229 return (SET_ERROR(EINVAL));
3230 if (nvlist_lookup_uint64(nvprops,
3231 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3232 return (SET_ERROR(EINVAL));
3234 if ((error = nvlist_lookup_uint64(nvprops,
3235 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3236 &volblocksize)) != 0 && error != ENOENT)
3237 return (SET_ERROR(EINVAL));
3239 if (error != 0)
3240 volblocksize = zfs_prop_default_numeric(
3241 ZFS_PROP_VOLBLOCKSIZE);
3243 if ((error = zvol_check_volblocksize(
3244 volblocksize)) != 0 ||
3245 (error = zvol_check_volsize(volsize,
3246 volblocksize)) != 0)
3247 return (error);
3248 } else if (type == DMU_OST_ZFS) {
3249 int error;
3252 * We have to have normalization and
3253 * case-folding flags correct when we do the
3254 * file system creation, so go figure them out
3255 * now.
3257 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3258 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3259 error = zfs_fill_zplprops(fsname, nvprops,
3260 zct.zct_zplprops, &is_insensitive);
3261 if (error != 0) {
3262 nvlist_free(zct.zct_zplprops);
3263 return (error);
3267 error = dmu_objset_create(fsname, type,
3268 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3269 nvlist_free(zct.zct_zplprops);
3272 * It would be nice to do this atomically.
3274 if (error == 0) {
3275 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3276 nvprops, outnvl);
3277 if (error != 0)
3278 (void) dsl_destroy_head(fsname);
3280 return (error);
3284 * innvl: {
3285 * "origin" -> name of origin snapshot
3286 * (optional) "props" -> { prop -> value }
3289 * outnvl: propname -> error code (int32)
3291 static int
3292 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3294 int error = 0;
3295 nvlist_t *nvprops = NULL;
3296 char *origin_name;
3298 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3299 return (SET_ERROR(EINVAL));
3300 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3302 if (strchr(fsname, '@') ||
3303 strchr(fsname, '%'))
3304 return (SET_ERROR(EINVAL));
3306 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3307 return (SET_ERROR(EINVAL));
3308 error = dmu_objset_clone(fsname, origin_name);
3309 if (error != 0)
3310 return (error);
3313 * It would be nice to do this atomically.
3315 if (error == 0) {
3316 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3317 nvprops, outnvl);
3318 if (error != 0)
3319 (void) dsl_destroy_head(fsname);
3321 return (error);
3325 * innvl: {
3326 * "snaps" -> { snapshot1, snapshot2 }
3327 * (optional) "props" -> { prop -> value (string) }
3330 * outnvl: snapshot -> error code (int32)
3332 static int
3333 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3335 nvlist_t *snaps;
3336 nvlist_t *props = NULL;
3337 int error, poollen;
3338 nvpair_t *pair;
3340 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3341 if ((error = zfs_check_userprops(poolname, props)) != 0)
3342 return (error);
3344 if (!nvlist_empty(props) &&
3345 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3346 return (SET_ERROR(ENOTSUP));
3348 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3349 return (SET_ERROR(EINVAL));
3350 poollen = strlen(poolname);
3351 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3352 pair = nvlist_next_nvpair(snaps, pair)) {
3353 const char *name = nvpair_name(pair);
3354 const char *cp = strchr(name, '@');
3357 * The snap name must contain an @, and the part after it must
3358 * contain only valid characters.
3360 if (cp == NULL ||
3361 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3362 return (SET_ERROR(EINVAL));
3365 * The snap must be in the specified pool.
3367 if (strncmp(name, poolname, poollen) != 0 ||
3368 (name[poollen] != '/' && name[poollen] != '@'))
3369 return (SET_ERROR(EXDEV));
3371 /* This must be the only snap of this fs. */
3372 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3373 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3374 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3375 == 0) {
3376 return (SET_ERROR(EXDEV));
3381 error = dsl_dataset_snapshot(snaps, props, outnvl);
3382 return (error);
3386 * innvl: "message" -> string
3388 /* ARGSUSED */
3389 static int
3390 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3392 char *message;
3393 spa_t *spa;
3394 int error;
3395 char *poolname;
3398 * The poolname in the ioctl is not set, we get it from the TSD,
3399 * which was set at the end of the last successful ioctl that allows
3400 * logging. The secpolicy func already checked that it is set.
3401 * Only one log ioctl is allowed after each successful ioctl, so
3402 * we clear the TSD here.
3404 poolname = tsd_get(zfs_allow_log_key);
3405 (void) tsd_set(zfs_allow_log_key, NULL);
3406 error = spa_open(poolname, &spa, FTAG);
3407 strfree(poolname);
3408 if (error != 0)
3409 return (error);
3411 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3412 spa_close(spa, FTAG);
3413 return (SET_ERROR(EINVAL));
3416 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3417 spa_close(spa, FTAG);
3418 return (SET_ERROR(ENOTSUP));
3421 error = spa_history_log(spa, message);
3422 spa_close(spa, FTAG);
3423 return (error);
3427 * The dp_config_rwlock must not be held when calling this, because the
3428 * unmount may need to write out data.
3430 * This function is best-effort. Callers must deal gracefully if it
3431 * remains mounted (or is remounted after this call).
3433 * Returns 0 if the argument is not a snapshot, or it is not currently a
3434 * filesystem, or we were able to unmount it. Returns error code otherwise.
3437 zfs_unmount_snap(const char *snapname)
3439 vfs_t *vfsp;
3440 zfsvfs_t *zfsvfs;
3441 int err;
3443 if (strchr(snapname, '@') == NULL)
3444 return (0);
3446 vfsp = zfs_get_vfs(snapname);
3447 if (vfsp == NULL)
3448 return (0);
3450 zfsvfs = vfsp->vfs_data;
3451 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3453 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3454 VFS_RELE(vfsp);
3455 if (err != 0)
3456 return (SET_ERROR(err));
3459 * Always force the unmount for snapshots.
3461 (void) dounmount(vfsp, MS_FORCE, kcred);
3462 return (0);
3465 /* ARGSUSED */
3466 static int
3467 zfs_unmount_snap_cb(const char *snapname, void *arg)
3469 return (zfs_unmount_snap(snapname));
3473 * When a clone is destroyed, its origin may also need to be destroyed,
3474 * in which case it must be unmounted. This routine will do that unmount
3475 * if necessary.
3477 void
3478 zfs_destroy_unmount_origin(const char *fsname)
3480 int error;
3481 objset_t *os;
3482 dsl_dataset_t *ds;
3484 error = dmu_objset_hold(fsname, FTAG, &os);
3485 if (error != 0)
3486 return;
3487 ds = dmu_objset_ds(os);
3488 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3489 char originname[MAXNAMELEN];
3490 dsl_dataset_name(ds->ds_prev, originname);
3491 dmu_objset_rele(os, FTAG);
3492 (void) zfs_unmount_snap(originname);
3493 } else {
3494 dmu_objset_rele(os, FTAG);
3499 * innvl: {
3500 * "snaps" -> { snapshot1, snapshot2 }
3501 * (optional boolean) "defer"
3504 * outnvl: snapshot -> error code (int32)
3507 /* ARGSUSED */
3508 static int
3509 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3511 nvlist_t *snaps;
3512 nvpair_t *pair;
3513 boolean_t defer;
3515 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3516 return (SET_ERROR(EINVAL));
3517 defer = nvlist_exists(innvl, "defer");
3519 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3520 pair = nvlist_next_nvpair(snaps, pair)) {
3521 (void) zfs_unmount_snap(nvpair_name(pair));
3524 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3528 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3529 * All bookmarks must be in the same pool.
3531 * innvl: {
3532 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3535 * outnvl: bookmark -> error code (int32)
3538 /* ARGSUSED */
3539 static int
3540 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3542 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3543 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3544 char *snap_name;
3547 * Verify the snapshot argument.
3549 if (nvpair_value_string(pair, &snap_name) != 0)
3550 return (SET_ERROR(EINVAL));
3553 /* Verify that the keys (bookmarks) are unique */
3554 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3555 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3556 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3557 return (SET_ERROR(EINVAL));
3561 return (dsl_bookmark_create(innvl, outnvl));
3565 * innvl: {
3566 * property 1, property 2, ...
3569 * outnvl: {
3570 * bookmark name 1 -> { property 1, property 2, ... },
3571 * bookmark name 2 -> { property 1, property 2, ... }
3575 static int
3576 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3578 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3582 * innvl: {
3583 * bookmark name 1, bookmark name 2
3586 * outnvl: bookmark -> error code (int32)
3589 static int
3590 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3591 nvlist_t *outnvl)
3593 int error, poollen;
3595 poollen = strlen(poolname);
3596 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3597 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3598 const char *name = nvpair_name(pair);
3599 const char *cp = strchr(name, '#');
3602 * The bookmark name must contain an #, and the part after it
3603 * must contain only valid characters.
3605 if (cp == NULL ||
3606 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3607 return (SET_ERROR(EINVAL));
3610 * The bookmark must be in the specified pool.
3612 if (strncmp(name, poolname, poollen) != 0 ||
3613 (name[poollen] != '/' && name[poollen] != '#'))
3614 return (SET_ERROR(EXDEV));
3617 error = dsl_bookmark_destroy(innvl, outnvl);
3618 return (error);
3622 * inputs:
3623 * zc_name name of dataset to destroy
3624 * zc_objset_type type of objset
3625 * zc_defer_destroy mark for deferred destroy
3627 * outputs: none
3629 static int
3630 zfs_ioc_destroy(zfs_cmd_t *zc)
3632 int err;
3634 if (zc->zc_objset_type == DMU_OST_ZFS) {
3635 err = zfs_unmount_snap(zc->zc_name);
3636 if (err != 0)
3637 return (err);
3640 if (strchr(zc->zc_name, '@'))
3641 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3642 else
3643 err = dsl_destroy_head(zc->zc_name);
3644 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3645 (void) zvol_remove_minor(zc->zc_name);
3646 return (err);
3650 * fsname is name of dataset to rollback (to most recent snapshot)
3652 * innvl is not used.
3654 * outnvl: "target" -> name of most recent snapshot
3657 /* ARGSUSED */
3658 static int
3659 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3661 zfsvfs_t *zfsvfs;
3662 int error;
3664 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3665 error = zfs_suspend_fs(zfsvfs);
3666 if (error == 0) {
3667 int resume_err;
3669 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3670 resume_err = zfs_resume_fs(zfsvfs, fsname);
3671 error = error ? error : resume_err;
3673 VFS_RELE(zfsvfs->z_vfs);
3674 } else {
3675 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3677 return (error);
3680 static int
3681 recursive_unmount(const char *fsname, void *arg)
3683 const char *snapname = arg;
3684 char fullname[MAXNAMELEN];
3686 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3687 return (zfs_unmount_snap(fullname));
3691 * inputs:
3692 * zc_name old name of dataset
3693 * zc_value new name of dataset
3694 * zc_cookie recursive flag (only valid for snapshots)
3696 * outputs: none
3698 static int
3699 zfs_ioc_rename(zfs_cmd_t *zc)
3701 boolean_t recursive = zc->zc_cookie & 1;
3702 char *at;
3704 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3705 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3706 strchr(zc->zc_value, '%'))
3707 return (SET_ERROR(EINVAL));
3709 at = strchr(zc->zc_name, '@');
3710 if (at != NULL) {
3711 /* snaps must be in same fs */
3712 int error;
3714 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3715 return (SET_ERROR(EXDEV));
3716 *at = '\0';
3717 if (zc->zc_objset_type == DMU_OST_ZFS) {
3718 error = dmu_objset_find(zc->zc_name,
3719 recursive_unmount, at + 1,
3720 recursive ? DS_FIND_CHILDREN : 0);
3721 if (error != 0) {
3722 *at = '@';
3723 return (error);
3726 error = dsl_dataset_rename_snapshot(zc->zc_name,
3727 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3728 *at = '@';
3730 return (error);
3731 } else {
3732 if (zc->zc_objset_type == DMU_OST_ZVOL)
3733 (void) zvol_remove_minor(zc->zc_name);
3734 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3738 static int
3739 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3741 const char *propname = nvpair_name(pair);
3742 boolean_t issnap = (strchr(dsname, '@') != NULL);
3743 zfs_prop_t prop = zfs_name_to_prop(propname);
3744 uint64_t intval;
3745 int err;
3747 if (prop == ZPROP_INVAL) {
3748 if (zfs_prop_user(propname)) {
3749 if (err = zfs_secpolicy_write_perms(dsname,
3750 ZFS_DELEG_PERM_USERPROP, cr))
3751 return (err);
3752 return (0);
3755 if (!issnap && zfs_prop_userquota(propname)) {
3756 const char *perm = NULL;
3757 const char *uq_prefix =
3758 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3759 const char *gq_prefix =
3760 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3762 if (strncmp(propname, uq_prefix,
3763 strlen(uq_prefix)) == 0) {
3764 perm = ZFS_DELEG_PERM_USERQUOTA;
3765 } else if (strncmp(propname, gq_prefix,
3766 strlen(gq_prefix)) == 0) {
3767 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3768 } else {
3769 /* USERUSED and GROUPUSED are read-only */
3770 return (SET_ERROR(EINVAL));
3773 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3774 return (err);
3775 return (0);
3778 return (SET_ERROR(EINVAL));
3781 if (issnap)
3782 return (SET_ERROR(EINVAL));
3784 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3786 * dsl_prop_get_all_impl() returns properties in this
3787 * format.
3789 nvlist_t *attrs;
3790 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3791 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3792 &pair) == 0);
3796 * Check that this value is valid for this pool version
3798 switch (prop) {
3799 case ZFS_PROP_COMPRESSION:
3801 * If the user specified gzip compression, make sure
3802 * the SPA supports it. We ignore any errors here since
3803 * we'll catch them later.
3805 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3806 nvpair_value_uint64(pair, &intval) == 0) {
3807 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3808 intval <= ZIO_COMPRESS_GZIP_9 &&
3809 zfs_earlier_version(dsname,
3810 SPA_VERSION_GZIP_COMPRESSION)) {
3811 return (SET_ERROR(ENOTSUP));
3814 if (intval == ZIO_COMPRESS_ZLE &&
3815 zfs_earlier_version(dsname,
3816 SPA_VERSION_ZLE_COMPRESSION))
3817 return (SET_ERROR(ENOTSUP));
3819 if (intval == ZIO_COMPRESS_LZ4) {
3820 spa_t *spa;
3822 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3823 return (err);
3825 if (!spa_feature_is_enabled(spa,
3826 SPA_FEATURE_LZ4_COMPRESS)) {
3827 spa_close(spa, FTAG);
3828 return (SET_ERROR(ENOTSUP));
3830 spa_close(spa, FTAG);
3834 * If this is a bootable dataset then
3835 * verify that the compression algorithm
3836 * is supported for booting. We must return
3837 * something other than ENOTSUP since it
3838 * implies a downrev pool version.
3840 if (zfs_is_bootfs(dsname) &&
3841 !BOOTFS_COMPRESS_VALID(intval)) {
3842 return (SET_ERROR(ERANGE));
3845 break;
3847 case ZFS_PROP_COPIES:
3848 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3849 return (SET_ERROR(ENOTSUP));
3850 break;
3852 case ZFS_PROP_DEDUP:
3853 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3854 return (SET_ERROR(ENOTSUP));
3855 break;
3857 case ZFS_PROP_SHARESMB:
3858 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3859 return (SET_ERROR(ENOTSUP));
3860 break;
3862 case ZFS_PROP_ACLINHERIT:
3863 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3864 nvpair_value_uint64(pair, &intval) == 0) {
3865 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3866 zfs_earlier_version(dsname,
3867 SPA_VERSION_PASSTHROUGH_X))
3868 return (SET_ERROR(ENOTSUP));
3870 break;
3873 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3877 * Checks for a race condition to make sure we don't increment a feature flag
3878 * multiple times.
3880 static int
3881 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3883 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3884 spa_feature_t *featurep = arg;
3886 if (!spa_feature_is_active(spa, *featurep))
3887 return (0);
3888 else
3889 return (SET_ERROR(EBUSY));
3893 * The callback invoked on feature activation in the sync task caused by
3894 * zfs_prop_activate_feature.
3896 static void
3897 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3899 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3900 spa_feature_t *featurep = arg;
3902 spa_feature_incr(spa, *featurep, tx);
3906 * Activates a feature on a pool in response to a property setting. This
3907 * creates a new sync task which modifies the pool to reflect the feature
3908 * as being active.
3910 static int
3911 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
3913 int err;
3915 /* EBUSY here indicates that the feature is already active */
3916 err = dsl_sync_task(spa_name(spa),
3917 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3918 &feature, 2);
3920 if (err != 0 && err != EBUSY)
3921 return (err);
3922 else
3923 return (0);
3927 * Removes properties from the given props list that fail permission checks
3928 * needed to clear them and to restore them in case of a receive error. For each
3929 * property, make sure we have both set and inherit permissions.
3931 * Returns the first error encountered if any permission checks fail. If the
3932 * caller provides a non-NULL errlist, it also gives the complete list of names
3933 * of all the properties that failed a permission check along with the
3934 * corresponding error numbers. The caller is responsible for freeing the
3935 * returned errlist.
3937 * If every property checks out successfully, zero is returned and the list
3938 * pointed at by errlist is NULL.
3940 static int
3941 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3943 zfs_cmd_t *zc;
3944 nvpair_t *pair, *next_pair;
3945 nvlist_t *errors;
3946 int err, rv = 0;
3948 if (props == NULL)
3949 return (0);
3951 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3953 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3954 (void) strcpy(zc->zc_name, dataset);
3955 pair = nvlist_next_nvpair(props, NULL);
3956 while (pair != NULL) {
3957 next_pair = nvlist_next_nvpair(props, pair);
3959 (void) strcpy(zc->zc_value, nvpair_name(pair));
3960 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3961 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3962 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3963 VERIFY(nvlist_add_int32(errors,
3964 zc->zc_value, err) == 0);
3966 pair = next_pair;
3968 kmem_free(zc, sizeof (zfs_cmd_t));
3970 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3971 nvlist_free(errors);
3972 errors = NULL;
3973 } else {
3974 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3977 if (errlist == NULL)
3978 nvlist_free(errors);
3979 else
3980 *errlist = errors;
3982 return (rv);
3985 static boolean_t
3986 propval_equals(nvpair_t *p1, nvpair_t *p2)
3988 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3989 /* dsl_prop_get_all_impl() format */
3990 nvlist_t *attrs;
3991 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3992 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3993 &p1) == 0);
3996 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3997 nvlist_t *attrs;
3998 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3999 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4000 &p2) == 0);
4003 if (nvpair_type(p1) != nvpair_type(p2))
4004 return (B_FALSE);
4006 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4007 char *valstr1, *valstr2;
4009 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4010 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4011 return (strcmp(valstr1, valstr2) == 0);
4012 } else {
4013 uint64_t intval1, intval2;
4015 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4016 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4017 return (intval1 == intval2);
4022 * Remove properties from props if they are not going to change (as determined
4023 * by comparison with origprops). Remove them from origprops as well, since we
4024 * do not need to clear or restore properties that won't change.
4026 static void
4027 props_reduce(nvlist_t *props, nvlist_t *origprops)
4029 nvpair_t *pair, *next_pair;
4031 if (origprops == NULL)
4032 return; /* all props need to be received */
4034 pair = nvlist_next_nvpair(props, NULL);
4035 while (pair != NULL) {
4036 const char *propname = nvpair_name(pair);
4037 nvpair_t *match;
4039 next_pair = nvlist_next_nvpair(props, pair);
4041 if ((nvlist_lookup_nvpair(origprops, propname,
4042 &match) != 0) || !propval_equals(pair, match))
4043 goto next; /* need to set received value */
4045 /* don't clear the existing received value */
4046 (void) nvlist_remove_nvpair(origprops, match);
4047 /* don't bother receiving the property */
4048 (void) nvlist_remove_nvpair(props, pair);
4049 next:
4050 pair = next_pair;
4054 #ifdef DEBUG
4055 static boolean_t zfs_ioc_recv_inject_err;
4056 #endif
4059 * inputs:
4060 * zc_name name of containing filesystem
4061 * zc_nvlist_src{_size} nvlist of properties to apply
4062 * zc_value name of snapshot to create
4063 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4064 * zc_cookie file descriptor to recv from
4065 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4066 * zc_guid force flag
4067 * zc_cleanup_fd cleanup-on-exit file descriptor
4068 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4070 * outputs:
4071 * zc_cookie number of bytes read
4072 * zc_nvlist_dst{_size} error for each unapplied received property
4073 * zc_obj zprop_errflags_t
4074 * zc_action_handle handle for this guid/ds mapping
4076 static int
4077 zfs_ioc_recv(zfs_cmd_t *zc)
4079 file_t *fp;
4080 dmu_recv_cookie_t drc;
4081 boolean_t force = (boolean_t)zc->zc_guid;
4082 int fd;
4083 int error = 0;
4084 int props_error = 0;
4085 nvlist_t *errors;
4086 offset_t off;
4087 nvlist_t *props = NULL; /* sent properties */
4088 nvlist_t *origprops = NULL; /* existing properties */
4089 char *origin = NULL;
4090 char *tosnap;
4091 char tofs[ZFS_MAXNAMELEN];
4092 boolean_t first_recvd_props = B_FALSE;
4094 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4095 strchr(zc->zc_value, '@') == NULL ||
4096 strchr(zc->zc_value, '%'))
4097 return (SET_ERROR(EINVAL));
4099 (void) strcpy(tofs, zc->zc_value);
4100 tosnap = strchr(tofs, '@');
4101 *tosnap++ = '\0';
4103 if (zc->zc_nvlist_src != NULL &&
4104 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4105 zc->zc_iflags, &props)) != 0)
4106 return (error);
4108 fd = zc->zc_cookie;
4109 fp = getf(fd);
4110 if (fp == NULL) {
4111 nvlist_free(props);
4112 return (SET_ERROR(EBADF));
4115 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4117 if (zc->zc_string[0])
4118 origin = zc->zc_string;
4120 error = dmu_recv_begin(tofs, tosnap,
4121 &zc->zc_begin_record, force, origin, &drc);
4122 if (error != 0)
4123 goto out;
4126 * Set properties before we receive the stream so that they are applied
4127 * to the new data. Note that we must call dmu_recv_stream() if
4128 * dmu_recv_begin() succeeds.
4130 if (props != NULL && !drc.drc_newfs) {
4131 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4132 SPA_VERSION_RECVD_PROPS &&
4133 !dsl_prop_get_hasrecvd(tofs))
4134 first_recvd_props = B_TRUE;
4137 * If new received properties are supplied, they are to
4138 * completely replace the existing received properties, so stash
4139 * away the existing ones.
4141 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4142 nvlist_t *errlist = NULL;
4144 * Don't bother writing a property if its value won't
4145 * change (and avoid the unnecessary security checks).
4147 * The first receive after SPA_VERSION_RECVD_PROPS is a
4148 * special case where we blow away all local properties
4149 * regardless.
4151 if (!first_recvd_props)
4152 props_reduce(props, origprops);
4153 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4154 (void) nvlist_merge(errors, errlist, 0);
4155 nvlist_free(errlist);
4157 if (clear_received_props(tofs, origprops,
4158 first_recvd_props ? NULL : props) != 0)
4159 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4160 } else {
4161 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4165 if (props != NULL) {
4166 props_error = dsl_prop_set_hasrecvd(tofs);
4168 if (props_error == 0) {
4169 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4170 props, errors);
4174 if (zc->zc_nvlist_dst_size != 0 &&
4175 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4176 put_nvlist(zc, errors) != 0)) {
4178 * Caller made zc->zc_nvlist_dst less than the minimum expected
4179 * size or supplied an invalid address.
4181 props_error = SET_ERROR(EINVAL);
4184 off = fp->f_offset;
4185 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4186 &zc->zc_action_handle);
4188 if (error == 0) {
4189 zfsvfs_t *zfsvfs = NULL;
4191 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4192 /* online recv */
4193 int end_err;
4195 error = zfs_suspend_fs(zfsvfs);
4197 * If the suspend fails, then the recv_end will
4198 * likely also fail, and clean up after itself.
4200 end_err = dmu_recv_end(&drc, zfsvfs);
4201 if (error == 0)
4202 error = zfs_resume_fs(zfsvfs, tofs);
4203 error = error ? error : end_err;
4204 VFS_RELE(zfsvfs->z_vfs);
4205 } else {
4206 error = dmu_recv_end(&drc, NULL);
4210 zc->zc_cookie = off - fp->f_offset;
4211 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4212 fp->f_offset = off;
4214 #ifdef DEBUG
4215 if (zfs_ioc_recv_inject_err) {
4216 zfs_ioc_recv_inject_err = B_FALSE;
4217 error = 1;
4219 #endif
4221 * On error, restore the original props.
4223 if (error != 0 && props != NULL && !drc.drc_newfs) {
4224 if (clear_received_props(tofs, props, NULL) != 0) {
4226 * We failed to clear the received properties.
4227 * Since we may have left a $recvd value on the
4228 * system, we can't clear the $hasrecvd flag.
4230 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4231 } else if (first_recvd_props) {
4232 dsl_prop_unset_hasrecvd(tofs);
4235 if (origprops == NULL && !drc.drc_newfs) {
4236 /* We failed to stash the original properties. */
4237 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4241 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4242 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4243 * explictly if we're restoring local properties cleared in the
4244 * first new-style receive.
4246 if (origprops != NULL &&
4247 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4248 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4249 origprops, NULL) != 0) {
4251 * We stashed the original properties but failed to
4252 * restore them.
4254 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4257 out:
4258 nvlist_free(props);
4259 nvlist_free(origprops);
4260 nvlist_free(errors);
4261 releasef(fd);
4263 if (error == 0)
4264 error = props_error;
4266 return (error);
4270 * inputs:
4271 * zc_name name of snapshot to send
4272 * zc_cookie file descriptor to send stream to
4273 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4274 * zc_sendobj objsetid of snapshot to send
4275 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4276 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4277 * output size in zc_objset_type.
4278 * zc_flags if =1, WRITE_EMBEDDED records are permitted
4280 * outputs:
4281 * zc_objset_type estimated size, if zc_guid is set
4283 static int
4284 zfs_ioc_send(zfs_cmd_t *zc)
4286 int error;
4287 offset_t off;
4288 boolean_t estimate = (zc->zc_guid != 0);
4289 boolean_t embedok = (zc->zc_flags & 0x1);
4291 if (zc->zc_obj != 0) {
4292 dsl_pool_t *dp;
4293 dsl_dataset_t *tosnap;
4295 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4296 if (error != 0)
4297 return (error);
4299 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4300 if (error != 0) {
4301 dsl_pool_rele(dp, FTAG);
4302 return (error);
4305 if (dsl_dir_is_clone(tosnap->ds_dir))
4306 zc->zc_fromobj = tosnap->ds_dir->dd_phys->dd_origin_obj;
4307 dsl_dataset_rele(tosnap, FTAG);
4308 dsl_pool_rele(dp, FTAG);
4311 if (estimate) {
4312 dsl_pool_t *dp;
4313 dsl_dataset_t *tosnap;
4314 dsl_dataset_t *fromsnap = NULL;
4316 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4317 if (error != 0)
4318 return (error);
4320 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4321 if (error != 0) {
4322 dsl_pool_rele(dp, FTAG);
4323 return (error);
4326 if (zc->zc_fromobj != 0) {
4327 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4328 FTAG, &fromsnap);
4329 if (error != 0) {
4330 dsl_dataset_rele(tosnap, FTAG);
4331 dsl_pool_rele(dp, FTAG);
4332 return (error);
4336 error = dmu_send_estimate(tosnap, fromsnap,
4337 &zc->zc_objset_type);
4339 if (fromsnap != NULL)
4340 dsl_dataset_rele(fromsnap, FTAG);
4341 dsl_dataset_rele(tosnap, FTAG);
4342 dsl_pool_rele(dp, FTAG);
4343 } else {
4344 file_t *fp = getf(zc->zc_cookie);
4345 if (fp == NULL)
4346 return (SET_ERROR(EBADF));
4348 off = fp->f_offset;
4349 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4350 zc->zc_fromobj, embedok, zc->zc_cookie, fp->f_vnode, &off);
4352 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4353 fp->f_offset = off;
4354 releasef(zc->zc_cookie);
4356 return (error);
4360 * inputs:
4361 * zc_name name of snapshot on which to report progress
4362 * zc_cookie file descriptor of send stream
4364 * outputs:
4365 * zc_cookie number of bytes written in send stream thus far
4367 static int
4368 zfs_ioc_send_progress(zfs_cmd_t *zc)
4370 dsl_pool_t *dp;
4371 dsl_dataset_t *ds;
4372 dmu_sendarg_t *dsp = NULL;
4373 int error;
4375 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4376 if (error != 0)
4377 return (error);
4379 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4380 if (error != 0) {
4381 dsl_pool_rele(dp, FTAG);
4382 return (error);
4385 mutex_enter(&ds->ds_sendstream_lock);
4388 * Iterate over all the send streams currently active on this dataset.
4389 * If there's one which matches the specified file descriptor _and_ the
4390 * stream was started by the current process, return the progress of
4391 * that stream.
4393 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4394 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4395 if (dsp->dsa_outfd == zc->zc_cookie &&
4396 dsp->dsa_proc == curproc)
4397 break;
4400 if (dsp != NULL)
4401 zc->zc_cookie = *(dsp->dsa_off);
4402 else
4403 error = SET_ERROR(ENOENT);
4405 mutex_exit(&ds->ds_sendstream_lock);
4406 dsl_dataset_rele(ds, FTAG);
4407 dsl_pool_rele(dp, FTAG);
4408 return (error);
4411 static int
4412 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4414 int id, error;
4416 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4417 &zc->zc_inject_record);
4419 if (error == 0)
4420 zc->zc_guid = (uint64_t)id;
4422 return (error);
4425 static int
4426 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4428 return (zio_clear_fault((int)zc->zc_guid));
4431 static int
4432 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4434 int id = (int)zc->zc_guid;
4435 int error;
4437 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4438 &zc->zc_inject_record);
4440 zc->zc_guid = id;
4442 return (error);
4445 static int
4446 zfs_ioc_error_log(zfs_cmd_t *zc)
4448 spa_t *spa;
4449 int error;
4450 size_t count = (size_t)zc->zc_nvlist_dst_size;
4452 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4453 return (error);
4455 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4456 &count);
4457 if (error == 0)
4458 zc->zc_nvlist_dst_size = count;
4459 else
4460 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4462 spa_close(spa, FTAG);
4464 return (error);
4467 static int
4468 zfs_ioc_clear(zfs_cmd_t *zc)
4470 spa_t *spa;
4471 vdev_t *vd;
4472 int error;
4475 * On zpool clear we also fix up missing slogs
4477 mutex_enter(&spa_namespace_lock);
4478 spa = spa_lookup(zc->zc_name);
4479 if (spa == NULL) {
4480 mutex_exit(&spa_namespace_lock);
4481 return (SET_ERROR(EIO));
4483 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4484 /* we need to let spa_open/spa_load clear the chains */
4485 spa_set_log_state(spa, SPA_LOG_CLEAR);
4487 spa->spa_last_open_failed = 0;
4488 mutex_exit(&spa_namespace_lock);
4490 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4491 error = spa_open(zc->zc_name, &spa, FTAG);
4492 } else {
4493 nvlist_t *policy;
4494 nvlist_t *config = NULL;
4496 if (zc->zc_nvlist_src == NULL)
4497 return (SET_ERROR(EINVAL));
4499 if ((error = get_nvlist(zc->zc_nvlist_src,
4500 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4501 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4502 policy, &config);
4503 if (config != NULL) {
4504 int err;
4506 if ((err = put_nvlist(zc, config)) != 0)
4507 error = err;
4508 nvlist_free(config);
4510 nvlist_free(policy);
4514 if (error != 0)
4515 return (error);
4517 spa_vdev_state_enter(spa, SCL_NONE);
4519 if (zc->zc_guid == 0) {
4520 vd = NULL;
4521 } else {
4522 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4523 if (vd == NULL) {
4524 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4525 spa_close(spa, FTAG);
4526 return (SET_ERROR(ENODEV));
4530 vdev_clear(spa, vd);
4532 (void) spa_vdev_state_exit(spa, NULL, 0);
4535 * Resume any suspended I/Os.
4537 if (zio_resume(spa) != 0)
4538 error = SET_ERROR(EIO);
4540 spa_close(spa, FTAG);
4542 return (error);
4545 static int
4546 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4548 spa_t *spa;
4549 int error;
4551 error = spa_open(zc->zc_name, &spa, FTAG);
4552 if (error != 0)
4553 return (error);
4555 spa_vdev_state_enter(spa, SCL_NONE);
4558 * If a resilver is already in progress then set the
4559 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4560 * the scan as a side effect of the reopen. Otherwise, let
4561 * vdev_open() decided if a resilver is required.
4563 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4564 vdev_reopen(spa->spa_root_vdev);
4565 spa->spa_scrub_reopen = B_FALSE;
4567 (void) spa_vdev_state_exit(spa, NULL, 0);
4568 spa_close(spa, FTAG);
4569 return (0);
4572 * inputs:
4573 * zc_name name of filesystem
4574 * zc_value name of origin snapshot
4576 * outputs:
4577 * zc_string name of conflicting snapshot, if there is one
4579 static int
4580 zfs_ioc_promote(zfs_cmd_t *zc)
4582 char *cp;
4585 * We don't need to unmount *all* the origin fs's snapshots, but
4586 * it's easier.
4588 cp = strchr(zc->zc_value, '@');
4589 if (cp)
4590 *cp = '\0';
4591 (void) dmu_objset_find(zc->zc_value,
4592 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4593 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4597 * Retrieve a single {user|group}{used|quota}@... property.
4599 * inputs:
4600 * zc_name name of filesystem
4601 * zc_objset_type zfs_userquota_prop_t
4602 * zc_value domain name (eg. "S-1-234-567-89")
4603 * zc_guid RID/UID/GID
4605 * outputs:
4606 * zc_cookie property value
4608 static int
4609 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4611 zfsvfs_t *zfsvfs;
4612 int error;
4614 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4615 return (SET_ERROR(EINVAL));
4617 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4618 if (error != 0)
4619 return (error);
4621 error = zfs_userspace_one(zfsvfs,
4622 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4623 zfsvfs_rele(zfsvfs, FTAG);
4625 return (error);
4629 * inputs:
4630 * zc_name name of filesystem
4631 * zc_cookie zap cursor
4632 * zc_objset_type zfs_userquota_prop_t
4633 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4635 * outputs:
4636 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4637 * zc_cookie zap cursor
4639 static int
4640 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4642 zfsvfs_t *zfsvfs;
4643 int bufsize = zc->zc_nvlist_dst_size;
4645 if (bufsize <= 0)
4646 return (SET_ERROR(ENOMEM));
4648 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4649 if (error != 0)
4650 return (error);
4652 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4654 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4655 buf, &zc->zc_nvlist_dst_size);
4657 if (error == 0) {
4658 error = xcopyout(buf,
4659 (void *)(uintptr_t)zc->zc_nvlist_dst,
4660 zc->zc_nvlist_dst_size);
4662 kmem_free(buf, bufsize);
4663 zfsvfs_rele(zfsvfs, FTAG);
4665 return (error);
4669 * inputs:
4670 * zc_name name of filesystem
4672 * outputs:
4673 * none
4675 static int
4676 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4678 objset_t *os;
4679 int error = 0;
4680 zfsvfs_t *zfsvfs;
4682 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4683 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4685 * If userused is not enabled, it may be because the
4686 * objset needs to be closed & reopened (to grow the
4687 * objset_phys_t). Suspend/resume the fs will do that.
4689 error = zfs_suspend_fs(zfsvfs);
4690 if (error == 0) {
4691 dmu_objset_refresh_ownership(zfsvfs->z_os,
4692 zfsvfs);
4693 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4696 if (error == 0)
4697 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4698 VFS_RELE(zfsvfs->z_vfs);
4699 } else {
4700 /* XXX kind of reading contents without owning */
4701 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4702 if (error != 0)
4703 return (error);
4705 error = dmu_objset_userspace_upgrade(os);
4706 dmu_objset_rele(os, FTAG);
4709 return (error);
4713 * We don't want to have a hard dependency
4714 * against some special symbols in sharefs
4715 * nfs, and smbsrv. Determine them if needed when
4716 * the first file system is shared.
4717 * Neither sharefs, nfs or smbsrv are unloadable modules.
4719 int (*znfsexport_fs)(void *arg);
4720 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4721 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4723 int zfs_nfsshare_inited;
4724 int zfs_smbshare_inited;
4726 ddi_modhandle_t nfs_mod;
4727 ddi_modhandle_t sharefs_mod;
4728 ddi_modhandle_t smbsrv_mod;
4729 kmutex_t zfs_share_lock;
4731 static int
4732 zfs_init_sharefs()
4734 int error;
4736 ASSERT(MUTEX_HELD(&zfs_share_lock));
4737 /* Both NFS and SMB shares also require sharetab support. */
4738 if (sharefs_mod == NULL && ((sharefs_mod =
4739 ddi_modopen("fs/sharefs",
4740 KRTLD_MODE_FIRST, &error)) == NULL)) {
4741 return (SET_ERROR(ENOSYS));
4743 if (zshare_fs == NULL && ((zshare_fs =
4744 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4745 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4746 return (SET_ERROR(ENOSYS));
4748 return (0);
4751 static int
4752 zfs_ioc_share(zfs_cmd_t *zc)
4754 int error;
4755 int opcode;
4757 switch (zc->zc_share.z_sharetype) {
4758 case ZFS_SHARE_NFS:
4759 case ZFS_UNSHARE_NFS:
4760 if (zfs_nfsshare_inited == 0) {
4761 mutex_enter(&zfs_share_lock);
4762 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4763 KRTLD_MODE_FIRST, &error)) == NULL)) {
4764 mutex_exit(&zfs_share_lock);
4765 return (SET_ERROR(ENOSYS));
4767 if (znfsexport_fs == NULL &&
4768 ((znfsexport_fs = (int (*)(void *))
4769 ddi_modsym(nfs_mod,
4770 "nfs_export", &error)) == NULL)) {
4771 mutex_exit(&zfs_share_lock);
4772 return (SET_ERROR(ENOSYS));
4774 error = zfs_init_sharefs();
4775 if (error != 0) {
4776 mutex_exit(&zfs_share_lock);
4777 return (SET_ERROR(ENOSYS));
4779 zfs_nfsshare_inited = 1;
4780 mutex_exit(&zfs_share_lock);
4782 break;
4783 case ZFS_SHARE_SMB:
4784 case ZFS_UNSHARE_SMB:
4785 if (zfs_smbshare_inited == 0) {
4786 mutex_enter(&zfs_share_lock);
4787 if (smbsrv_mod == NULL && ((smbsrv_mod =
4788 ddi_modopen("drv/smbsrv",
4789 KRTLD_MODE_FIRST, &error)) == NULL)) {
4790 mutex_exit(&zfs_share_lock);
4791 return (SET_ERROR(ENOSYS));
4793 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4794 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4795 "smb_server_share", &error)) == NULL)) {
4796 mutex_exit(&zfs_share_lock);
4797 return (SET_ERROR(ENOSYS));
4799 error = zfs_init_sharefs();
4800 if (error != 0) {
4801 mutex_exit(&zfs_share_lock);
4802 return (SET_ERROR(ENOSYS));
4804 zfs_smbshare_inited = 1;
4805 mutex_exit(&zfs_share_lock);
4807 break;
4808 default:
4809 return (SET_ERROR(EINVAL));
4812 switch (zc->zc_share.z_sharetype) {
4813 case ZFS_SHARE_NFS:
4814 case ZFS_UNSHARE_NFS:
4815 if (error =
4816 znfsexport_fs((void *)
4817 (uintptr_t)zc->zc_share.z_exportdata))
4818 return (error);
4819 break;
4820 case ZFS_SHARE_SMB:
4821 case ZFS_UNSHARE_SMB:
4822 if (error = zsmbexport_fs((void *)
4823 (uintptr_t)zc->zc_share.z_exportdata,
4824 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4825 B_TRUE: B_FALSE)) {
4826 return (error);
4828 break;
4831 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4832 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4833 SHAREFS_ADD : SHAREFS_REMOVE;
4836 * Add or remove share from sharetab
4838 error = zshare_fs(opcode,
4839 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4840 zc->zc_share.z_sharemax);
4842 return (error);
4846 ace_t full_access[] = {
4847 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4851 * inputs:
4852 * zc_name name of containing filesystem
4853 * zc_obj object # beyond which we want next in-use object #
4855 * outputs:
4856 * zc_obj next in-use object #
4858 static int
4859 zfs_ioc_next_obj(zfs_cmd_t *zc)
4861 objset_t *os = NULL;
4862 int error;
4864 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4865 if (error != 0)
4866 return (error);
4868 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4869 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4871 dmu_objset_rele(os, FTAG);
4872 return (error);
4876 * inputs:
4877 * zc_name name of filesystem
4878 * zc_value prefix name for snapshot
4879 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4881 * outputs:
4882 * zc_value short name of new snapshot
4884 static int
4885 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4887 char *snap_name;
4888 char *hold_name;
4889 int error;
4890 minor_t minor;
4892 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4893 if (error != 0)
4894 return (error);
4896 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4897 (u_longlong_t)ddi_get_lbolt64());
4898 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4900 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4901 hold_name);
4902 if (error == 0)
4903 (void) strcpy(zc->zc_value, snap_name);
4904 strfree(snap_name);
4905 strfree(hold_name);
4906 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4907 return (error);
4911 * inputs:
4912 * zc_name name of "to" snapshot
4913 * zc_value name of "from" snapshot
4914 * zc_cookie file descriptor to write diff data on
4916 * outputs:
4917 * dmu_diff_record_t's to the file descriptor
4919 static int
4920 zfs_ioc_diff(zfs_cmd_t *zc)
4922 file_t *fp;
4923 offset_t off;
4924 int error;
4926 fp = getf(zc->zc_cookie);
4927 if (fp == NULL)
4928 return (SET_ERROR(EBADF));
4930 off = fp->f_offset;
4932 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
4934 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4935 fp->f_offset = off;
4936 releasef(zc->zc_cookie);
4938 return (error);
4942 * Remove all ACL files in shares dir
4944 static int
4945 zfs_smb_acl_purge(znode_t *dzp)
4947 zap_cursor_t zc;
4948 zap_attribute_t zap;
4949 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4950 int error;
4952 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4953 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4954 zap_cursor_advance(&zc)) {
4955 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4956 NULL, 0)) != 0)
4957 break;
4959 zap_cursor_fini(&zc);
4960 return (error);
4963 static int
4964 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4966 vnode_t *vp;
4967 znode_t *dzp;
4968 vnode_t *resourcevp = NULL;
4969 znode_t *sharedir;
4970 zfsvfs_t *zfsvfs;
4971 nvlist_t *nvlist;
4972 char *src, *target;
4973 vattr_t vattr;
4974 vsecattr_t vsec;
4975 int error = 0;
4977 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4978 NO_FOLLOW, NULL, &vp)) != 0)
4979 return (error);
4981 /* Now make sure mntpnt and dataset are ZFS */
4983 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4984 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4985 zc->zc_name) != 0)) {
4986 VN_RELE(vp);
4987 return (SET_ERROR(EINVAL));
4990 dzp = VTOZ(vp);
4991 zfsvfs = dzp->z_zfsvfs;
4992 ZFS_ENTER(zfsvfs);
4995 * Create share dir if its missing.
4997 mutex_enter(&zfsvfs->z_lock);
4998 if (zfsvfs->z_shares_dir == 0) {
4999 dmu_tx_t *tx;
5001 tx = dmu_tx_create(zfsvfs->z_os);
5002 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5003 ZFS_SHARES_DIR);
5004 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5005 error = dmu_tx_assign(tx, TXG_WAIT);
5006 if (error != 0) {
5007 dmu_tx_abort(tx);
5008 } else {
5009 error = zfs_create_share_dir(zfsvfs, tx);
5010 dmu_tx_commit(tx);
5012 if (error != 0) {
5013 mutex_exit(&zfsvfs->z_lock);
5014 VN_RELE(vp);
5015 ZFS_EXIT(zfsvfs);
5016 return (error);
5019 mutex_exit(&zfsvfs->z_lock);
5021 ASSERT(zfsvfs->z_shares_dir);
5022 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5023 VN_RELE(vp);
5024 ZFS_EXIT(zfsvfs);
5025 return (error);
5028 switch (zc->zc_cookie) {
5029 case ZFS_SMB_ACL_ADD:
5030 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5031 vattr.va_type = VREG;
5032 vattr.va_mode = S_IFREG|0777;
5033 vattr.va_uid = 0;
5034 vattr.va_gid = 0;
5036 vsec.vsa_mask = VSA_ACE;
5037 vsec.vsa_aclentp = &full_access;
5038 vsec.vsa_aclentsz = sizeof (full_access);
5039 vsec.vsa_aclcnt = 1;
5041 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5042 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5043 if (resourcevp)
5044 VN_RELE(resourcevp);
5045 break;
5047 case ZFS_SMB_ACL_REMOVE:
5048 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5049 NULL, 0);
5050 break;
5052 case ZFS_SMB_ACL_RENAME:
5053 if ((error = get_nvlist(zc->zc_nvlist_src,
5054 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5055 VN_RELE(vp);
5056 ZFS_EXIT(zfsvfs);
5057 return (error);
5059 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5060 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5061 &target)) {
5062 VN_RELE(vp);
5063 VN_RELE(ZTOV(sharedir));
5064 ZFS_EXIT(zfsvfs);
5065 nvlist_free(nvlist);
5066 return (error);
5068 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5069 kcred, NULL, 0);
5070 nvlist_free(nvlist);
5071 break;
5073 case ZFS_SMB_ACL_PURGE:
5074 error = zfs_smb_acl_purge(sharedir);
5075 break;
5077 default:
5078 error = SET_ERROR(EINVAL);
5079 break;
5082 VN_RELE(vp);
5083 VN_RELE(ZTOV(sharedir));
5085 ZFS_EXIT(zfsvfs);
5087 return (error);
5091 * innvl: {
5092 * "holds" -> { snapname -> holdname (string), ... }
5093 * (optional) "cleanup_fd" -> fd (int32)
5096 * outnvl: {
5097 * snapname -> error value (int32)
5098 * ...
5101 /* ARGSUSED */
5102 static int
5103 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5105 nvlist_t *holds;
5106 int cleanup_fd = -1;
5107 int error;
5108 minor_t minor = 0;
5110 error = nvlist_lookup_nvlist(args, "holds", &holds);
5111 if (error != 0)
5112 return (SET_ERROR(EINVAL));
5114 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5115 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5116 if (error != 0)
5117 return (error);
5120 error = dsl_dataset_user_hold(holds, minor, errlist);
5121 if (minor != 0)
5122 zfs_onexit_fd_rele(cleanup_fd);
5123 return (error);
5127 * innvl is not used.
5129 * outnvl: {
5130 * holdname -> time added (uint64 seconds since epoch)
5131 * ...
5134 /* ARGSUSED */
5135 static int
5136 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5138 return (dsl_dataset_get_holds(snapname, outnvl));
5142 * innvl: {
5143 * snapname -> { holdname, ... }
5144 * ...
5147 * outnvl: {
5148 * snapname -> error value (int32)
5149 * ...
5152 /* ARGSUSED */
5153 static int
5154 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5156 return (dsl_dataset_user_release(holds, errlist));
5160 * inputs:
5161 * zc_name name of new filesystem or snapshot
5162 * zc_value full name of old snapshot
5164 * outputs:
5165 * zc_cookie space in bytes
5166 * zc_objset_type compressed space in bytes
5167 * zc_perm_action uncompressed space in bytes
5169 static int
5170 zfs_ioc_space_written(zfs_cmd_t *zc)
5172 int error;
5173 dsl_pool_t *dp;
5174 dsl_dataset_t *new, *old;
5176 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5177 if (error != 0)
5178 return (error);
5179 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5180 if (error != 0) {
5181 dsl_pool_rele(dp, FTAG);
5182 return (error);
5184 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5185 if (error != 0) {
5186 dsl_dataset_rele(new, FTAG);
5187 dsl_pool_rele(dp, FTAG);
5188 return (error);
5191 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5192 &zc->zc_objset_type, &zc->zc_perm_action);
5193 dsl_dataset_rele(old, FTAG);
5194 dsl_dataset_rele(new, FTAG);
5195 dsl_pool_rele(dp, FTAG);
5196 return (error);
5200 * innvl: {
5201 * "firstsnap" -> snapshot name
5204 * outnvl: {
5205 * "used" -> space in bytes
5206 * "compressed" -> compressed space in bytes
5207 * "uncompressed" -> uncompressed space in bytes
5210 static int
5211 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5213 int error;
5214 dsl_pool_t *dp;
5215 dsl_dataset_t *new, *old;
5216 char *firstsnap;
5217 uint64_t used, comp, uncomp;
5219 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5220 return (SET_ERROR(EINVAL));
5222 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5223 if (error != 0)
5224 return (error);
5226 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5227 if (error != 0) {
5228 dsl_pool_rele(dp, FTAG);
5229 return (error);
5231 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5232 if (error != 0) {
5233 dsl_dataset_rele(new, FTAG);
5234 dsl_pool_rele(dp, FTAG);
5235 return (error);
5238 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5239 dsl_dataset_rele(old, FTAG);
5240 dsl_dataset_rele(new, FTAG);
5241 dsl_pool_rele(dp, FTAG);
5242 fnvlist_add_uint64(outnvl, "used", used);
5243 fnvlist_add_uint64(outnvl, "compressed", comp);
5244 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5245 return (error);
5249 * innvl: {
5250 * "fd" -> file descriptor to write stream to (int32)
5251 * (optional) "fromsnap" -> full snap name to send an incremental from
5252 * (optional) "embedok" -> (value ignored)
5253 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5256 * outnvl is unused
5258 /* ARGSUSED */
5259 static int
5260 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5262 int error;
5263 offset_t off;
5264 char *fromname = NULL;
5265 int fd;
5266 boolean_t embedok;
5268 error = nvlist_lookup_int32(innvl, "fd", &fd);
5269 if (error != 0)
5270 return (SET_ERROR(EINVAL));
5272 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5274 embedok = nvlist_exists(innvl, "embedok");
5276 file_t *fp = getf(fd);
5277 if (fp == NULL)
5278 return (SET_ERROR(EBADF));
5280 off = fp->f_offset;
5281 error = dmu_send(snapname, fromname, embedok, fd, fp->f_vnode, &off);
5283 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5284 fp->f_offset = off;
5285 releasef(fd);
5286 return (error);
5290 * Determine approximately how large a zfs send stream will be -- the number
5291 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5293 * innvl: {
5294 * (optional) "fromsnap" -> full snap name to send an incremental from
5297 * outnvl: {
5298 * "space" -> bytes of space (uint64)
5301 static int
5302 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5304 dsl_pool_t *dp;
5305 dsl_dataset_t *fromsnap = NULL;
5306 dsl_dataset_t *tosnap;
5307 int error;
5308 char *fromname;
5309 uint64_t space;
5311 error = dsl_pool_hold(snapname, FTAG, &dp);
5312 if (error != 0)
5313 return (error);
5315 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5316 if (error != 0) {
5317 dsl_pool_rele(dp, FTAG);
5318 return (error);
5321 error = nvlist_lookup_string(innvl, "fromsnap", &fromname);
5322 if (error == 0) {
5323 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5324 if (error != 0) {
5325 dsl_dataset_rele(tosnap, FTAG);
5326 dsl_pool_rele(dp, FTAG);
5327 return (error);
5331 error = dmu_send_estimate(tosnap, fromsnap, &space);
5332 fnvlist_add_uint64(outnvl, "space", space);
5334 if (fromsnap != NULL)
5335 dsl_dataset_rele(fromsnap, FTAG);
5336 dsl_dataset_rele(tosnap, FTAG);
5337 dsl_pool_rele(dp, FTAG);
5338 return (error);
5342 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5344 static void
5345 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5346 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5347 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5349 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5351 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5352 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5353 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5354 ASSERT3P(vec->zvec_func, ==, NULL);
5356 vec->zvec_legacy_func = func;
5357 vec->zvec_secpolicy = secpolicy;
5358 vec->zvec_namecheck = namecheck;
5359 vec->zvec_allow_log = log_history;
5360 vec->zvec_pool_check = pool_check;
5364 * See the block comment at the beginning of this file for details on
5365 * each argument to this function.
5367 static void
5368 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5369 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5370 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5371 boolean_t allow_log)
5373 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5375 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5376 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5377 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5378 ASSERT3P(vec->zvec_func, ==, NULL);
5380 /* if we are logging, the name must be valid */
5381 ASSERT(!allow_log || namecheck != NO_NAME);
5383 vec->zvec_name = name;
5384 vec->zvec_func = func;
5385 vec->zvec_secpolicy = secpolicy;
5386 vec->zvec_namecheck = namecheck;
5387 vec->zvec_pool_check = pool_check;
5388 vec->zvec_smush_outnvlist = smush_outnvlist;
5389 vec->zvec_allow_log = allow_log;
5392 static void
5393 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5394 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5395 zfs_ioc_poolcheck_t pool_check)
5397 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5398 POOL_NAME, log_history, pool_check);
5401 static void
5402 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5403 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5405 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5406 DATASET_NAME, B_FALSE, pool_check);
5409 static void
5410 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5412 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5413 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5416 static void
5417 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5418 zfs_secpolicy_func_t *secpolicy)
5420 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5421 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5424 static void
5425 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5426 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5428 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5429 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5432 static void
5433 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5435 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5436 zfs_secpolicy_read);
5439 static void
5440 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5441 zfs_secpolicy_func_t *secpolicy)
5443 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5444 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5447 static void
5448 zfs_ioctl_init(void)
5450 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5451 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5452 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5454 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5455 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5456 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5458 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5459 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5460 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5462 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5463 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5464 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5466 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5467 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5468 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5470 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5471 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5472 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5474 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5475 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5476 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5478 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5479 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5480 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5482 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5483 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5484 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5485 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5486 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5487 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5489 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5490 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5491 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5493 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5494 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5495 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5497 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5498 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5499 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5501 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5502 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5503 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5505 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5506 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5507 POOL_NAME,
5508 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5510 /* IOCTLS that use the legacy function signature */
5512 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5513 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5515 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5516 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5517 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5518 zfs_ioc_pool_scan);
5519 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5520 zfs_ioc_pool_upgrade);
5521 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5522 zfs_ioc_vdev_add);
5523 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5524 zfs_ioc_vdev_remove);
5525 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5526 zfs_ioc_vdev_set_state);
5527 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5528 zfs_ioc_vdev_attach);
5529 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5530 zfs_ioc_vdev_detach);
5531 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5532 zfs_ioc_vdev_setpath);
5533 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5534 zfs_ioc_vdev_setfru);
5535 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5536 zfs_ioc_pool_set_props);
5537 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5538 zfs_ioc_vdev_split);
5539 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5540 zfs_ioc_pool_reguid);
5542 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5543 zfs_ioc_pool_configs, zfs_secpolicy_none);
5544 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5545 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5546 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5547 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5548 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5549 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5550 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5551 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5554 * pool destroy, and export don't log the history as part of
5555 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5556 * does the logging of those commands.
5558 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5559 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5560 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5561 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5563 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5564 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5565 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5566 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5568 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5569 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5570 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5571 zfs_ioc_dsobj_to_dsname,
5572 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5573 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5574 zfs_ioc_pool_get_history,
5575 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5577 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5578 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5580 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5581 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5582 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5583 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5585 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5586 zfs_ioc_space_written);
5587 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5588 zfs_ioc_objset_recvd_props);
5589 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5590 zfs_ioc_next_obj);
5591 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5592 zfs_ioc_get_fsacl);
5593 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5594 zfs_ioc_objset_stats);
5595 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5596 zfs_ioc_objset_zplprops);
5597 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5598 zfs_ioc_dataset_list_next);
5599 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5600 zfs_ioc_snapshot_list_next);
5601 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5602 zfs_ioc_send_progress);
5604 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5605 zfs_ioc_diff, zfs_secpolicy_diff);
5606 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5607 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5608 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5609 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5610 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5611 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5612 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5613 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5614 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5615 zfs_ioc_send, zfs_secpolicy_send);
5617 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5618 zfs_secpolicy_none);
5619 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5620 zfs_secpolicy_destroy);
5621 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5622 zfs_secpolicy_rename);
5623 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5624 zfs_secpolicy_recv);
5625 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5626 zfs_secpolicy_promote);
5627 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5628 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5629 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5630 zfs_secpolicy_set_fsacl);
5632 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5633 zfs_secpolicy_share, POOL_CHECK_NONE);
5634 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5635 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5636 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5637 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5638 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5639 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5640 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5641 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5645 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5646 zfs_ioc_poolcheck_t check)
5648 spa_t *spa;
5649 int error;
5651 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5653 if (check & POOL_CHECK_NONE)
5654 return (0);
5656 error = spa_open(name, &spa, FTAG);
5657 if (error == 0) {
5658 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5659 error = SET_ERROR(EAGAIN);
5660 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5661 error = SET_ERROR(EROFS);
5662 spa_close(spa, FTAG);
5664 return (error);
5668 * Find a free minor number.
5670 minor_t
5671 zfsdev_minor_alloc(void)
5673 static minor_t last_minor;
5674 minor_t m;
5676 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5678 for (m = last_minor + 1; m != last_minor; m++) {
5679 if (m > ZFSDEV_MAX_MINOR)
5680 m = 1;
5681 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5682 last_minor = m;
5683 return (m);
5687 return (0);
5690 static int
5691 zfs_ctldev_init(dev_t *devp)
5693 minor_t minor;
5694 zfs_soft_state_t *zs;
5696 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5697 ASSERT(getminor(*devp) == 0);
5699 minor = zfsdev_minor_alloc();
5700 if (minor == 0)
5701 return (SET_ERROR(ENXIO));
5703 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5704 return (SET_ERROR(EAGAIN));
5706 *devp = makedevice(getemajor(*devp), minor);
5708 zs = ddi_get_soft_state(zfsdev_state, minor);
5709 zs->zss_type = ZSST_CTLDEV;
5710 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5712 return (0);
5715 static void
5716 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5718 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5720 zfs_onexit_destroy(zo);
5721 ddi_soft_state_free(zfsdev_state, minor);
5724 void *
5725 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5727 zfs_soft_state_t *zp;
5729 zp = ddi_get_soft_state(zfsdev_state, minor);
5730 if (zp == NULL || zp->zss_type != which)
5731 return (NULL);
5733 return (zp->zss_data);
5736 static int
5737 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5739 int error = 0;
5741 if (getminor(*devp) != 0)
5742 return (zvol_open(devp, flag, otyp, cr));
5744 /* This is the control device. Allocate a new minor if requested. */
5745 if (flag & FEXCL) {
5746 mutex_enter(&zfsdev_state_lock);
5747 error = zfs_ctldev_init(devp);
5748 mutex_exit(&zfsdev_state_lock);
5751 return (error);
5754 static int
5755 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5757 zfs_onexit_t *zo;
5758 minor_t minor = getminor(dev);
5760 if (minor == 0)
5761 return (0);
5763 mutex_enter(&zfsdev_state_lock);
5764 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5765 if (zo == NULL) {
5766 mutex_exit(&zfsdev_state_lock);
5767 return (zvol_close(dev, flag, otyp, cr));
5769 zfs_ctldev_destroy(zo, minor);
5770 mutex_exit(&zfsdev_state_lock);
5772 return (0);
5775 static int
5776 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5778 zfs_cmd_t *zc;
5779 uint_t vecnum;
5780 int error, rc, len;
5781 minor_t minor = getminor(dev);
5782 const zfs_ioc_vec_t *vec;
5783 char *saved_poolname = NULL;
5784 nvlist_t *innvl = NULL;
5786 if (minor != 0 &&
5787 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5788 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5790 vecnum = cmd - ZFS_IOC_FIRST;
5791 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5793 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5794 return (SET_ERROR(EINVAL));
5795 vec = &zfs_ioc_vec[vecnum];
5797 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5799 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5800 if (error != 0) {
5801 error = SET_ERROR(EFAULT);
5802 goto out;
5805 zc->zc_iflags = flag & FKIOCTL;
5806 if (zc->zc_nvlist_src_size != 0) {
5807 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5808 zc->zc_iflags, &innvl);
5809 if (error != 0)
5810 goto out;
5814 * Ensure that all pool/dataset names are valid before we pass down to
5815 * the lower layers.
5817 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5818 switch (vec->zvec_namecheck) {
5819 case POOL_NAME:
5820 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5821 error = SET_ERROR(EINVAL);
5822 else
5823 error = pool_status_check(zc->zc_name,
5824 vec->zvec_namecheck, vec->zvec_pool_check);
5825 break;
5827 case DATASET_NAME:
5828 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5829 error = SET_ERROR(EINVAL);
5830 else
5831 error = pool_status_check(zc->zc_name,
5832 vec->zvec_namecheck, vec->zvec_pool_check);
5833 break;
5835 case NO_NAME:
5836 break;
5840 if (error == 0 && !(flag & FKIOCTL))
5841 error = vec->zvec_secpolicy(zc, innvl, cr);
5843 if (error != 0)
5844 goto out;
5846 /* legacy ioctls can modify zc_name */
5847 len = strcspn(zc->zc_name, "/@#") + 1;
5848 saved_poolname = kmem_alloc(len, KM_SLEEP);
5849 (void) strlcpy(saved_poolname, zc->zc_name, len);
5851 if (vec->zvec_func != NULL) {
5852 nvlist_t *outnvl;
5853 int puterror = 0;
5854 spa_t *spa;
5855 nvlist_t *lognv = NULL;
5857 ASSERT(vec->zvec_legacy_func == NULL);
5860 * Add the innvl to the lognv before calling the func,
5861 * in case the func changes the innvl.
5863 if (vec->zvec_allow_log) {
5864 lognv = fnvlist_alloc();
5865 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
5866 vec->zvec_name);
5867 if (!nvlist_empty(innvl)) {
5868 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
5869 innvl);
5873 outnvl = fnvlist_alloc();
5874 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
5876 if (error == 0 && vec->zvec_allow_log &&
5877 spa_open(zc->zc_name, &spa, FTAG) == 0) {
5878 if (!nvlist_empty(outnvl)) {
5879 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
5880 outnvl);
5882 (void) spa_history_log_nvl(spa, lognv);
5883 spa_close(spa, FTAG);
5885 fnvlist_free(lognv);
5887 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
5888 int smusherror = 0;
5889 if (vec->zvec_smush_outnvlist) {
5890 smusherror = nvlist_smush(outnvl,
5891 zc->zc_nvlist_dst_size);
5893 if (smusherror == 0)
5894 puterror = put_nvlist(zc, outnvl);
5897 if (puterror != 0)
5898 error = puterror;
5900 nvlist_free(outnvl);
5901 } else {
5902 error = vec->zvec_legacy_func(zc);
5905 out:
5906 nvlist_free(innvl);
5907 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5908 if (error == 0 && rc != 0)
5909 error = SET_ERROR(EFAULT);
5910 if (error == 0 && vec->zvec_allow_log) {
5911 char *s = tsd_get(zfs_allow_log_key);
5912 if (s != NULL)
5913 strfree(s);
5914 (void) tsd_set(zfs_allow_log_key, saved_poolname);
5915 } else {
5916 if (saved_poolname != NULL)
5917 strfree(saved_poolname);
5920 kmem_free(zc, sizeof (zfs_cmd_t));
5921 return (error);
5924 static int
5925 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5927 if (cmd != DDI_ATTACH)
5928 return (DDI_FAILURE);
5930 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5931 DDI_PSEUDO, 0) == DDI_FAILURE)
5932 return (DDI_FAILURE);
5934 zfs_dip = dip;
5936 ddi_report_dev(dip);
5938 return (DDI_SUCCESS);
5941 static int
5942 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5944 if (spa_busy() || zfs_busy() || zvol_busy())
5945 return (DDI_FAILURE);
5947 if (cmd != DDI_DETACH)
5948 return (DDI_FAILURE);
5950 zfs_dip = NULL;
5952 ddi_prop_remove_all(dip);
5953 ddi_remove_minor_node(dip, NULL);
5955 return (DDI_SUCCESS);
5958 /*ARGSUSED*/
5959 static int
5960 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5962 switch (infocmd) {
5963 case DDI_INFO_DEVT2DEVINFO:
5964 *result = zfs_dip;
5965 return (DDI_SUCCESS);
5967 case DDI_INFO_DEVT2INSTANCE:
5968 *result = (void *)0;
5969 return (DDI_SUCCESS);
5972 return (DDI_FAILURE);
5976 * OK, so this is a little weird.
5978 * /dev/zfs is the control node, i.e. minor 0.
5979 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5981 * /dev/zfs has basically nothing to do except serve up ioctls,
5982 * so most of the standard driver entry points are in zvol.c.
5984 static struct cb_ops zfs_cb_ops = {
5985 zfsdev_open, /* open */
5986 zfsdev_close, /* close */
5987 zvol_strategy, /* strategy */
5988 nodev, /* print */
5989 zvol_dump, /* dump */
5990 zvol_read, /* read */
5991 zvol_write, /* write */
5992 zfsdev_ioctl, /* ioctl */
5993 nodev, /* devmap */
5994 nodev, /* mmap */
5995 nodev, /* segmap */
5996 nochpoll, /* poll */
5997 ddi_prop_op, /* prop_op */
5998 NULL, /* streamtab */
5999 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6000 CB_REV, /* version */
6001 nodev, /* async read */
6002 nodev, /* async write */
6005 static struct dev_ops zfs_dev_ops = {
6006 DEVO_REV, /* version */
6007 0, /* refcnt */
6008 zfs_info, /* info */
6009 nulldev, /* identify */
6010 nulldev, /* probe */
6011 zfs_attach, /* attach */
6012 zfs_detach, /* detach */
6013 nodev, /* reset */
6014 &zfs_cb_ops, /* driver operations */
6015 NULL, /* no bus operations */
6016 NULL, /* power */
6017 ddi_quiesce_not_needed, /* quiesce */
6020 static struct modldrv zfs_modldrv = {
6021 &mod_driverops,
6022 "ZFS storage pool",
6023 &zfs_dev_ops
6026 static struct modlinkage modlinkage = {
6027 MODREV_1,
6028 (void *)&zfs_modlfs,
6029 (void *)&zfs_modldrv,
6030 NULL
6033 static void
6034 zfs_allow_log_destroy(void *arg)
6036 char *poolname = arg;
6037 strfree(poolname);
6041 _init(void)
6043 int error;
6045 spa_init(FREAD | FWRITE);
6046 zfs_init();
6047 zvol_init();
6048 zfs_ioctl_init();
6050 if ((error = mod_install(&modlinkage)) != 0) {
6051 zvol_fini();
6052 zfs_fini();
6053 spa_fini();
6054 return (error);
6057 tsd_create(&zfs_fsyncer_key, NULL);
6058 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6059 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6061 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6062 ASSERT(error == 0);
6063 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6065 return (0);
6069 _fini(void)
6071 int error;
6073 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6074 return (SET_ERROR(EBUSY));
6076 if ((error = mod_remove(&modlinkage)) != 0)
6077 return (error);
6079 zvol_fini();
6080 zfs_fini();
6081 spa_fini();
6082 if (zfs_nfsshare_inited)
6083 (void) ddi_modclose(nfs_mod);
6084 if (zfs_smbshare_inited)
6085 (void) ddi_modclose(smbsrv_mod);
6086 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6087 (void) ddi_modclose(sharefs_mod);
6089 tsd_destroy(&zfs_fsyncer_key);
6090 ldi_ident_release(zfs_li);
6091 zfs_li = NULL;
6092 mutex_destroy(&zfs_share_lock);
6094 return (error);
6098 _info(struct modinfo *modinfop)
6100 return (mod_info(&modlinkage, modinfop));