5515 dataset user hold doesn't reject empty tags
[illumos-gate.git] / usr / src / uts / common / fs / zfs / zfs_ioctl.c
blob692b49611d2d2085805db5fb3ed258db1a76e207
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 2015 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011, 2014 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 dsl_dir_phys(dd)->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 rrm_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 rrm_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 rrm_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 = -1;
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 default:
2457 err = -1;
2460 return (err);
2464 * This function is best effort. If it fails to set any of the given properties,
2465 * it continues to set as many as it can and returns the last error
2466 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2467 * with the list of names of all the properties that failed along with the
2468 * corresponding error numbers.
2470 * If every property is set successfully, zero is returned and errlist is not
2471 * modified.
2474 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2475 nvlist_t *errlist)
2477 nvpair_t *pair;
2478 nvpair_t *propval;
2479 int rv = 0;
2480 uint64_t intval;
2481 char *strval;
2482 nvlist_t *genericnvl = fnvlist_alloc();
2483 nvlist_t *retrynvl = fnvlist_alloc();
2485 retry:
2486 pair = NULL;
2487 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2488 const char *propname = nvpair_name(pair);
2489 zfs_prop_t prop = zfs_name_to_prop(propname);
2490 int err = 0;
2492 /* decode the property value */
2493 propval = pair;
2494 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2495 nvlist_t *attrs;
2496 attrs = fnvpair_value_nvlist(pair);
2497 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2498 &propval) != 0)
2499 err = SET_ERROR(EINVAL);
2502 /* Validate value type */
2503 if (err == 0 && prop == ZPROP_INVAL) {
2504 if (zfs_prop_user(propname)) {
2505 if (nvpair_type(propval) != DATA_TYPE_STRING)
2506 err = SET_ERROR(EINVAL);
2507 } else if (zfs_prop_userquota(propname)) {
2508 if (nvpair_type(propval) !=
2509 DATA_TYPE_UINT64_ARRAY)
2510 err = SET_ERROR(EINVAL);
2511 } else {
2512 err = SET_ERROR(EINVAL);
2514 } else if (err == 0) {
2515 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2516 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2517 err = SET_ERROR(EINVAL);
2518 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2519 const char *unused;
2521 intval = fnvpair_value_uint64(propval);
2523 switch (zfs_prop_get_type(prop)) {
2524 case PROP_TYPE_NUMBER:
2525 break;
2526 case PROP_TYPE_STRING:
2527 err = SET_ERROR(EINVAL);
2528 break;
2529 case PROP_TYPE_INDEX:
2530 if (zfs_prop_index_to_string(prop,
2531 intval, &unused) != 0)
2532 err = SET_ERROR(EINVAL);
2533 break;
2534 default:
2535 cmn_err(CE_PANIC,
2536 "unknown property type");
2538 } else {
2539 err = SET_ERROR(EINVAL);
2543 /* Validate permissions */
2544 if (err == 0)
2545 err = zfs_check_settable(dsname, pair, CRED());
2547 if (err == 0) {
2548 err = zfs_prop_set_special(dsname, source, pair);
2549 if (err == -1) {
2551 * For better performance we build up a list of
2552 * properties to set in a single transaction.
2554 err = nvlist_add_nvpair(genericnvl, pair);
2555 } else if (err != 0 && nvl != retrynvl) {
2557 * This may be a spurious error caused by
2558 * receiving quota and reservation out of order.
2559 * Try again in a second pass.
2561 err = nvlist_add_nvpair(retrynvl, pair);
2565 if (err != 0) {
2566 if (errlist != NULL)
2567 fnvlist_add_int32(errlist, propname, err);
2568 rv = err;
2572 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2573 nvl = retrynvl;
2574 goto retry;
2577 if (!nvlist_empty(genericnvl) &&
2578 dsl_props_set(dsname, source, genericnvl) != 0) {
2580 * If this fails, we still want to set as many properties as we
2581 * can, so try setting them individually.
2583 pair = NULL;
2584 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2585 const char *propname = nvpair_name(pair);
2586 int err = 0;
2588 propval = pair;
2589 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2590 nvlist_t *attrs;
2591 attrs = fnvpair_value_nvlist(pair);
2592 propval = fnvlist_lookup_nvpair(attrs,
2593 ZPROP_VALUE);
2596 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2597 strval = fnvpair_value_string(propval);
2598 err = dsl_prop_set_string(dsname, propname,
2599 source, strval);
2600 } else {
2601 intval = fnvpair_value_uint64(propval);
2602 err = dsl_prop_set_int(dsname, propname, source,
2603 intval);
2606 if (err != 0) {
2607 if (errlist != NULL) {
2608 fnvlist_add_int32(errlist, propname,
2609 err);
2611 rv = err;
2615 nvlist_free(genericnvl);
2616 nvlist_free(retrynvl);
2618 return (rv);
2622 * Check that all the properties are valid user properties.
2624 static int
2625 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2627 nvpair_t *pair = NULL;
2628 int error = 0;
2630 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2631 const char *propname = nvpair_name(pair);
2633 if (!zfs_prop_user(propname) ||
2634 nvpair_type(pair) != DATA_TYPE_STRING)
2635 return (SET_ERROR(EINVAL));
2637 if (error = zfs_secpolicy_write_perms(fsname,
2638 ZFS_DELEG_PERM_USERPROP, CRED()))
2639 return (error);
2641 if (strlen(propname) >= ZAP_MAXNAMELEN)
2642 return (SET_ERROR(ENAMETOOLONG));
2644 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2645 return (E2BIG);
2647 return (0);
2650 static void
2651 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2653 nvpair_t *pair;
2655 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2657 pair = NULL;
2658 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2659 if (nvlist_exists(skipped, nvpair_name(pair)))
2660 continue;
2662 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2666 static int
2667 clear_received_props(const char *dsname, nvlist_t *props,
2668 nvlist_t *skipped)
2670 int err = 0;
2671 nvlist_t *cleared_props = NULL;
2672 props_skip(props, skipped, &cleared_props);
2673 if (!nvlist_empty(cleared_props)) {
2675 * Acts on local properties until the dataset has received
2676 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2678 zprop_source_t flags = (ZPROP_SRC_NONE |
2679 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2680 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2682 nvlist_free(cleared_props);
2683 return (err);
2687 * inputs:
2688 * zc_name name of filesystem
2689 * zc_value name of property to set
2690 * zc_nvlist_src{_size} nvlist of properties to apply
2691 * zc_cookie received properties flag
2693 * outputs:
2694 * zc_nvlist_dst{_size} error for each unapplied received property
2696 static int
2697 zfs_ioc_set_prop(zfs_cmd_t *zc)
2699 nvlist_t *nvl;
2700 boolean_t received = zc->zc_cookie;
2701 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2702 ZPROP_SRC_LOCAL);
2703 nvlist_t *errors;
2704 int error;
2706 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2707 zc->zc_iflags, &nvl)) != 0)
2708 return (error);
2710 if (received) {
2711 nvlist_t *origprops;
2713 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2714 (void) clear_received_props(zc->zc_name,
2715 origprops, nvl);
2716 nvlist_free(origprops);
2719 error = dsl_prop_set_hasrecvd(zc->zc_name);
2722 errors = fnvlist_alloc();
2723 if (error == 0)
2724 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2726 if (zc->zc_nvlist_dst != NULL && errors != NULL) {
2727 (void) put_nvlist(zc, errors);
2730 nvlist_free(errors);
2731 nvlist_free(nvl);
2732 return (error);
2736 * inputs:
2737 * zc_name name of filesystem
2738 * zc_value name of property to inherit
2739 * zc_cookie revert to received value if TRUE
2741 * outputs: none
2743 static int
2744 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2746 const char *propname = zc->zc_value;
2747 zfs_prop_t prop = zfs_name_to_prop(propname);
2748 boolean_t received = zc->zc_cookie;
2749 zprop_source_t source = (received
2750 ? ZPROP_SRC_NONE /* revert to received value, if any */
2751 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2753 if (received) {
2754 nvlist_t *dummy;
2755 nvpair_t *pair;
2756 zprop_type_t type;
2757 int err;
2760 * zfs_prop_set_special() expects properties in the form of an
2761 * nvpair with type info.
2763 if (prop == ZPROP_INVAL) {
2764 if (!zfs_prop_user(propname))
2765 return (SET_ERROR(EINVAL));
2767 type = PROP_TYPE_STRING;
2768 } else if (prop == ZFS_PROP_VOLSIZE ||
2769 prop == ZFS_PROP_VERSION) {
2770 return (SET_ERROR(EINVAL));
2771 } else {
2772 type = zfs_prop_get_type(prop);
2775 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2777 switch (type) {
2778 case PROP_TYPE_STRING:
2779 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2780 break;
2781 case PROP_TYPE_NUMBER:
2782 case PROP_TYPE_INDEX:
2783 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2784 break;
2785 default:
2786 nvlist_free(dummy);
2787 return (SET_ERROR(EINVAL));
2790 pair = nvlist_next_nvpair(dummy, NULL);
2791 err = zfs_prop_set_special(zc->zc_name, source, pair);
2792 nvlist_free(dummy);
2793 if (err != -1)
2794 return (err); /* special property already handled */
2795 } else {
2797 * Only check this in the non-received case. We want to allow
2798 * 'inherit -S' to revert non-inheritable properties like quota
2799 * and reservation to the received or default values even though
2800 * they are not considered inheritable.
2802 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2803 return (SET_ERROR(EINVAL));
2806 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2807 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2810 static int
2811 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2813 nvlist_t *props;
2814 spa_t *spa;
2815 int error;
2816 nvpair_t *pair;
2818 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2819 zc->zc_iflags, &props))
2820 return (error);
2823 * If the only property is the configfile, then just do a spa_lookup()
2824 * to handle the faulted case.
2826 pair = nvlist_next_nvpair(props, NULL);
2827 if (pair != NULL && strcmp(nvpair_name(pair),
2828 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2829 nvlist_next_nvpair(props, pair) == NULL) {
2830 mutex_enter(&spa_namespace_lock);
2831 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2832 spa_configfile_set(spa, props, B_FALSE);
2833 spa_config_sync(spa, B_FALSE, B_TRUE);
2835 mutex_exit(&spa_namespace_lock);
2836 if (spa != NULL) {
2837 nvlist_free(props);
2838 return (0);
2842 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2843 nvlist_free(props);
2844 return (error);
2847 error = spa_prop_set(spa, props);
2849 nvlist_free(props);
2850 spa_close(spa, FTAG);
2852 return (error);
2855 static int
2856 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2858 spa_t *spa;
2859 int error;
2860 nvlist_t *nvp = NULL;
2862 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2864 * If the pool is faulted, there may be properties we can still
2865 * get (such as altroot and cachefile), so attempt to get them
2866 * anyway.
2868 mutex_enter(&spa_namespace_lock);
2869 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2870 error = spa_prop_get(spa, &nvp);
2871 mutex_exit(&spa_namespace_lock);
2872 } else {
2873 error = spa_prop_get(spa, &nvp);
2874 spa_close(spa, FTAG);
2877 if (error == 0 && zc->zc_nvlist_dst != NULL)
2878 error = put_nvlist(zc, nvp);
2879 else
2880 error = SET_ERROR(EFAULT);
2882 nvlist_free(nvp);
2883 return (error);
2887 * inputs:
2888 * zc_name name of filesystem
2889 * zc_nvlist_src{_size} nvlist of delegated permissions
2890 * zc_perm_action allow/unallow flag
2892 * outputs: none
2894 static int
2895 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2897 int error;
2898 nvlist_t *fsaclnv = NULL;
2900 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2901 zc->zc_iflags, &fsaclnv)) != 0)
2902 return (error);
2905 * Verify nvlist is constructed correctly
2907 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2908 nvlist_free(fsaclnv);
2909 return (SET_ERROR(EINVAL));
2913 * If we don't have PRIV_SYS_MOUNT, then validate
2914 * that user is allowed to hand out each permission in
2915 * the nvlist(s)
2918 error = secpolicy_zfs(CRED());
2919 if (error != 0) {
2920 if (zc->zc_perm_action == B_FALSE) {
2921 error = dsl_deleg_can_allow(zc->zc_name,
2922 fsaclnv, CRED());
2923 } else {
2924 error = dsl_deleg_can_unallow(zc->zc_name,
2925 fsaclnv, CRED());
2929 if (error == 0)
2930 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2932 nvlist_free(fsaclnv);
2933 return (error);
2937 * inputs:
2938 * zc_name name of filesystem
2940 * outputs:
2941 * zc_nvlist_src{_size} nvlist of delegated permissions
2943 static int
2944 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2946 nvlist_t *nvp;
2947 int error;
2949 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2950 error = put_nvlist(zc, nvp);
2951 nvlist_free(nvp);
2954 return (error);
2958 * Search the vfs list for a specified resource. Returns a pointer to it
2959 * or NULL if no suitable entry is found. The caller of this routine
2960 * is responsible for releasing the returned vfs pointer.
2962 static vfs_t *
2963 zfs_get_vfs(const char *resource)
2965 struct vfs *vfsp;
2966 struct vfs *vfs_found = NULL;
2968 vfs_list_read_lock();
2969 vfsp = rootvfs;
2970 do {
2971 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2972 VFS_HOLD(vfsp);
2973 vfs_found = vfsp;
2974 break;
2976 vfsp = vfsp->vfs_next;
2977 } while (vfsp != rootvfs);
2978 vfs_list_unlock();
2979 return (vfs_found);
2982 /* ARGSUSED */
2983 static void
2984 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2986 zfs_creat_t *zct = arg;
2988 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2991 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2994 * inputs:
2995 * os parent objset pointer (NULL if root fs)
2996 * fuids_ok fuids allowed in this version of the spa?
2997 * sa_ok SAs allowed in this version of the spa?
2998 * createprops list of properties requested by creator
3000 * outputs:
3001 * zplprops values for the zplprops we attach to the master node object
3002 * is_ci true if requested file system will be purely case-insensitive
3004 * Determine the settings for utf8only, normalization and
3005 * casesensitivity. Specific values may have been requested by the
3006 * creator and/or we can inherit values from the parent dataset. If
3007 * the file system is of too early a vintage, a creator can not
3008 * request settings for these properties, even if the requested
3009 * setting is the default value. We don't actually want to create dsl
3010 * properties for these, so remove them from the source nvlist after
3011 * processing.
3013 static int
3014 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3015 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3016 nvlist_t *zplprops, boolean_t *is_ci)
3018 uint64_t sense = ZFS_PROP_UNDEFINED;
3019 uint64_t norm = ZFS_PROP_UNDEFINED;
3020 uint64_t u8 = ZFS_PROP_UNDEFINED;
3022 ASSERT(zplprops != NULL);
3025 * Pull out creator prop choices, if any.
3027 if (createprops) {
3028 (void) nvlist_lookup_uint64(createprops,
3029 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3030 (void) nvlist_lookup_uint64(createprops,
3031 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3032 (void) nvlist_remove_all(createprops,
3033 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3034 (void) nvlist_lookup_uint64(createprops,
3035 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3036 (void) nvlist_remove_all(createprops,
3037 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3038 (void) nvlist_lookup_uint64(createprops,
3039 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3040 (void) nvlist_remove_all(createprops,
3041 zfs_prop_to_name(ZFS_PROP_CASE));
3045 * If the zpl version requested is whacky or the file system
3046 * or pool is version is too "young" to support normalization
3047 * and the creator tried to set a value for one of the props,
3048 * error out.
3050 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3051 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3052 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3053 (zplver < ZPL_VERSION_NORMALIZATION &&
3054 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3055 sense != ZFS_PROP_UNDEFINED)))
3056 return (SET_ERROR(ENOTSUP));
3059 * Put the version in the zplprops
3061 VERIFY(nvlist_add_uint64(zplprops,
3062 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3064 if (norm == ZFS_PROP_UNDEFINED)
3065 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3066 VERIFY(nvlist_add_uint64(zplprops,
3067 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3070 * If we're normalizing, names must always be valid UTF-8 strings.
3072 if (norm)
3073 u8 = 1;
3074 if (u8 == ZFS_PROP_UNDEFINED)
3075 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3076 VERIFY(nvlist_add_uint64(zplprops,
3077 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3079 if (sense == ZFS_PROP_UNDEFINED)
3080 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3081 VERIFY(nvlist_add_uint64(zplprops,
3082 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3084 if (is_ci)
3085 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3087 return (0);
3090 static int
3091 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3092 nvlist_t *zplprops, boolean_t *is_ci)
3094 boolean_t fuids_ok, sa_ok;
3095 uint64_t zplver = ZPL_VERSION;
3096 objset_t *os = NULL;
3097 char parentname[MAXNAMELEN];
3098 char *cp;
3099 spa_t *spa;
3100 uint64_t spa_vers;
3101 int error;
3103 (void) strlcpy(parentname, dataset, sizeof (parentname));
3104 cp = strrchr(parentname, '/');
3105 ASSERT(cp != NULL);
3106 cp[0] = '\0';
3108 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3109 return (error);
3111 spa_vers = spa_version(spa);
3112 spa_close(spa, FTAG);
3114 zplver = zfs_zpl_version_map(spa_vers);
3115 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3116 sa_ok = (zplver >= ZPL_VERSION_SA);
3119 * Open parent object set so we can inherit zplprop values.
3121 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3122 return (error);
3124 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3125 zplprops, is_ci);
3126 dmu_objset_rele(os, FTAG);
3127 return (error);
3130 static int
3131 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3132 nvlist_t *zplprops, boolean_t *is_ci)
3134 boolean_t fuids_ok;
3135 boolean_t sa_ok;
3136 uint64_t zplver = ZPL_VERSION;
3137 int error;
3139 zplver = zfs_zpl_version_map(spa_vers);
3140 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3141 sa_ok = (zplver >= ZPL_VERSION_SA);
3143 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3144 createprops, zplprops, is_ci);
3145 return (error);
3149 * innvl: {
3150 * "type" -> dmu_objset_type_t (int32)
3151 * (optional) "props" -> { prop -> value }
3154 * outnvl: propname -> error code (int32)
3156 static int
3157 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3159 int error = 0;
3160 zfs_creat_t zct = { 0 };
3161 nvlist_t *nvprops = NULL;
3162 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3163 int32_t type32;
3164 dmu_objset_type_t type;
3165 boolean_t is_insensitive = B_FALSE;
3167 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3168 return (SET_ERROR(EINVAL));
3169 type = type32;
3170 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3172 switch (type) {
3173 case DMU_OST_ZFS:
3174 cbfunc = zfs_create_cb;
3175 break;
3177 case DMU_OST_ZVOL:
3178 cbfunc = zvol_create_cb;
3179 break;
3181 default:
3182 cbfunc = NULL;
3183 break;
3185 if (strchr(fsname, '@') ||
3186 strchr(fsname, '%'))
3187 return (SET_ERROR(EINVAL));
3189 zct.zct_props = nvprops;
3191 if (cbfunc == NULL)
3192 return (SET_ERROR(EINVAL));
3194 if (type == DMU_OST_ZVOL) {
3195 uint64_t volsize, volblocksize;
3197 if (nvprops == NULL)
3198 return (SET_ERROR(EINVAL));
3199 if (nvlist_lookup_uint64(nvprops,
3200 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3201 return (SET_ERROR(EINVAL));
3203 if ((error = nvlist_lookup_uint64(nvprops,
3204 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3205 &volblocksize)) != 0 && error != ENOENT)
3206 return (SET_ERROR(EINVAL));
3208 if (error != 0)
3209 volblocksize = zfs_prop_default_numeric(
3210 ZFS_PROP_VOLBLOCKSIZE);
3212 if ((error = zvol_check_volblocksize(
3213 volblocksize)) != 0 ||
3214 (error = zvol_check_volsize(volsize,
3215 volblocksize)) != 0)
3216 return (error);
3217 } else if (type == DMU_OST_ZFS) {
3218 int error;
3221 * We have to have normalization and
3222 * case-folding flags correct when we do the
3223 * file system creation, so go figure them out
3224 * now.
3226 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3227 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3228 error = zfs_fill_zplprops(fsname, nvprops,
3229 zct.zct_zplprops, &is_insensitive);
3230 if (error != 0) {
3231 nvlist_free(zct.zct_zplprops);
3232 return (error);
3236 error = dmu_objset_create(fsname, type,
3237 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3238 nvlist_free(zct.zct_zplprops);
3241 * It would be nice to do this atomically.
3243 if (error == 0) {
3244 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3245 nvprops, outnvl);
3246 if (error != 0)
3247 (void) dsl_destroy_head(fsname);
3249 return (error);
3253 * innvl: {
3254 * "origin" -> name of origin snapshot
3255 * (optional) "props" -> { prop -> value }
3258 * outnvl: propname -> error code (int32)
3260 static int
3261 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3263 int error = 0;
3264 nvlist_t *nvprops = NULL;
3265 char *origin_name;
3267 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3268 return (SET_ERROR(EINVAL));
3269 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3271 if (strchr(fsname, '@') ||
3272 strchr(fsname, '%'))
3273 return (SET_ERROR(EINVAL));
3275 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3276 return (SET_ERROR(EINVAL));
3277 error = dmu_objset_clone(fsname, origin_name);
3278 if (error != 0)
3279 return (error);
3282 * It would be nice to do this atomically.
3284 if (error == 0) {
3285 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3286 nvprops, outnvl);
3287 if (error != 0)
3288 (void) dsl_destroy_head(fsname);
3290 return (error);
3294 * innvl: {
3295 * "snaps" -> { snapshot1, snapshot2 }
3296 * (optional) "props" -> { prop -> value (string) }
3299 * outnvl: snapshot -> error code (int32)
3301 static int
3302 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3304 nvlist_t *snaps;
3305 nvlist_t *props = NULL;
3306 int error, poollen;
3307 nvpair_t *pair;
3309 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3310 if ((error = zfs_check_userprops(poolname, props)) != 0)
3311 return (error);
3313 if (!nvlist_empty(props) &&
3314 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3315 return (SET_ERROR(ENOTSUP));
3317 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3318 return (SET_ERROR(EINVAL));
3319 poollen = strlen(poolname);
3320 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3321 pair = nvlist_next_nvpair(snaps, pair)) {
3322 const char *name = nvpair_name(pair);
3323 const char *cp = strchr(name, '@');
3326 * The snap name must contain an @, and the part after it must
3327 * contain only valid characters.
3329 if (cp == NULL ||
3330 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3331 return (SET_ERROR(EINVAL));
3334 * The snap must be in the specified pool.
3336 if (strncmp(name, poolname, poollen) != 0 ||
3337 (name[poollen] != '/' && name[poollen] != '@'))
3338 return (SET_ERROR(EXDEV));
3340 /* This must be the only snap of this fs. */
3341 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3342 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3343 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3344 == 0) {
3345 return (SET_ERROR(EXDEV));
3350 error = dsl_dataset_snapshot(snaps, props, outnvl);
3351 return (error);
3355 * innvl: "message" -> string
3357 /* ARGSUSED */
3358 static int
3359 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3361 char *message;
3362 spa_t *spa;
3363 int error;
3364 char *poolname;
3367 * The poolname in the ioctl is not set, we get it from the TSD,
3368 * which was set at the end of the last successful ioctl that allows
3369 * logging. The secpolicy func already checked that it is set.
3370 * Only one log ioctl is allowed after each successful ioctl, so
3371 * we clear the TSD here.
3373 poolname = tsd_get(zfs_allow_log_key);
3374 (void) tsd_set(zfs_allow_log_key, NULL);
3375 error = spa_open(poolname, &spa, FTAG);
3376 strfree(poolname);
3377 if (error != 0)
3378 return (error);
3380 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3381 spa_close(spa, FTAG);
3382 return (SET_ERROR(EINVAL));
3385 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3386 spa_close(spa, FTAG);
3387 return (SET_ERROR(ENOTSUP));
3390 error = spa_history_log(spa, message);
3391 spa_close(spa, FTAG);
3392 return (error);
3396 * The dp_config_rwlock must not be held when calling this, because the
3397 * unmount may need to write out data.
3399 * This function is best-effort. Callers must deal gracefully if it
3400 * remains mounted (or is remounted after this call).
3402 * Returns 0 if the argument is not a snapshot, or it is not currently a
3403 * filesystem, or we were able to unmount it. Returns error code otherwise.
3406 zfs_unmount_snap(const char *snapname)
3408 vfs_t *vfsp;
3409 zfsvfs_t *zfsvfs;
3410 int err;
3412 if (strchr(snapname, '@') == NULL)
3413 return (0);
3415 vfsp = zfs_get_vfs(snapname);
3416 if (vfsp == NULL)
3417 return (0);
3419 zfsvfs = vfsp->vfs_data;
3420 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3422 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3423 VFS_RELE(vfsp);
3424 if (err != 0)
3425 return (SET_ERROR(err));
3428 * Always force the unmount for snapshots.
3430 (void) dounmount(vfsp, MS_FORCE, kcred);
3431 return (0);
3434 /* ARGSUSED */
3435 static int
3436 zfs_unmount_snap_cb(const char *snapname, void *arg)
3438 return (zfs_unmount_snap(snapname));
3442 * When a clone is destroyed, its origin may also need to be destroyed,
3443 * in which case it must be unmounted. This routine will do that unmount
3444 * if necessary.
3446 void
3447 zfs_destroy_unmount_origin(const char *fsname)
3449 int error;
3450 objset_t *os;
3451 dsl_dataset_t *ds;
3453 error = dmu_objset_hold(fsname, FTAG, &os);
3454 if (error != 0)
3455 return;
3456 ds = dmu_objset_ds(os);
3457 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3458 char originname[MAXNAMELEN];
3459 dsl_dataset_name(ds->ds_prev, originname);
3460 dmu_objset_rele(os, FTAG);
3461 (void) zfs_unmount_snap(originname);
3462 } else {
3463 dmu_objset_rele(os, FTAG);
3468 * innvl: {
3469 * "snaps" -> { snapshot1, snapshot2 }
3470 * (optional boolean) "defer"
3473 * outnvl: snapshot -> error code (int32)
3476 /* ARGSUSED */
3477 static int
3478 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3480 nvlist_t *snaps;
3481 nvpair_t *pair;
3482 boolean_t defer;
3484 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3485 return (SET_ERROR(EINVAL));
3486 defer = nvlist_exists(innvl, "defer");
3488 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3489 pair = nvlist_next_nvpair(snaps, pair)) {
3490 (void) zfs_unmount_snap(nvpair_name(pair));
3493 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3497 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3498 * All bookmarks must be in the same pool.
3500 * innvl: {
3501 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3504 * outnvl: bookmark -> error code (int32)
3507 /* ARGSUSED */
3508 static int
3509 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3511 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3512 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3513 char *snap_name;
3516 * Verify the snapshot argument.
3518 if (nvpair_value_string(pair, &snap_name) != 0)
3519 return (SET_ERROR(EINVAL));
3522 /* Verify that the keys (bookmarks) are unique */
3523 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3524 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3525 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3526 return (SET_ERROR(EINVAL));
3530 return (dsl_bookmark_create(innvl, outnvl));
3534 * innvl: {
3535 * property 1, property 2, ...
3538 * outnvl: {
3539 * bookmark name 1 -> { property 1, property 2, ... },
3540 * bookmark name 2 -> { property 1, property 2, ... }
3544 static int
3545 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3547 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3551 * innvl: {
3552 * bookmark name 1, bookmark name 2
3555 * outnvl: bookmark -> error code (int32)
3558 static int
3559 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3560 nvlist_t *outnvl)
3562 int error, poollen;
3564 poollen = strlen(poolname);
3565 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3566 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3567 const char *name = nvpair_name(pair);
3568 const char *cp = strchr(name, '#');
3571 * The bookmark name must contain an #, and the part after it
3572 * must contain only valid characters.
3574 if (cp == NULL ||
3575 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3576 return (SET_ERROR(EINVAL));
3579 * The bookmark must be in the specified pool.
3581 if (strncmp(name, poolname, poollen) != 0 ||
3582 (name[poollen] != '/' && name[poollen] != '#'))
3583 return (SET_ERROR(EXDEV));
3586 error = dsl_bookmark_destroy(innvl, outnvl);
3587 return (error);
3591 * inputs:
3592 * zc_name name of dataset to destroy
3593 * zc_objset_type type of objset
3594 * zc_defer_destroy mark for deferred destroy
3596 * outputs: none
3598 static int
3599 zfs_ioc_destroy(zfs_cmd_t *zc)
3601 int err;
3603 if (zc->zc_objset_type == DMU_OST_ZFS) {
3604 err = zfs_unmount_snap(zc->zc_name);
3605 if (err != 0)
3606 return (err);
3609 if (strchr(zc->zc_name, '@'))
3610 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3611 else
3612 err = dsl_destroy_head(zc->zc_name);
3613 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3614 (void) zvol_remove_minor(zc->zc_name);
3615 return (err);
3619 * fsname is name of dataset to rollback (to most recent snapshot)
3621 * innvl is not used.
3623 * outnvl: "target" -> name of most recent snapshot
3626 /* ARGSUSED */
3627 static int
3628 zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
3630 zfsvfs_t *zfsvfs;
3631 int error;
3633 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3634 error = zfs_suspend_fs(zfsvfs);
3635 if (error == 0) {
3636 int resume_err;
3638 error = dsl_dataset_rollback(fsname, zfsvfs, outnvl);
3639 resume_err = zfs_resume_fs(zfsvfs, fsname);
3640 error = error ? error : resume_err;
3642 VFS_RELE(zfsvfs->z_vfs);
3643 } else {
3644 error = dsl_dataset_rollback(fsname, NULL, outnvl);
3646 return (error);
3649 static int
3650 recursive_unmount(const char *fsname, void *arg)
3652 const char *snapname = arg;
3653 char fullname[MAXNAMELEN];
3655 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3656 return (zfs_unmount_snap(fullname));
3660 * inputs:
3661 * zc_name old name of dataset
3662 * zc_value new name of dataset
3663 * zc_cookie recursive flag (only valid for snapshots)
3665 * outputs: none
3667 static int
3668 zfs_ioc_rename(zfs_cmd_t *zc)
3670 boolean_t recursive = zc->zc_cookie & 1;
3671 char *at;
3673 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3674 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3675 strchr(zc->zc_value, '%'))
3676 return (SET_ERROR(EINVAL));
3678 at = strchr(zc->zc_name, '@');
3679 if (at != NULL) {
3680 /* snaps must be in same fs */
3681 int error;
3683 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3684 return (SET_ERROR(EXDEV));
3685 *at = '\0';
3686 if (zc->zc_objset_type == DMU_OST_ZFS) {
3687 error = dmu_objset_find(zc->zc_name,
3688 recursive_unmount, at + 1,
3689 recursive ? DS_FIND_CHILDREN : 0);
3690 if (error != 0) {
3691 *at = '@';
3692 return (error);
3695 error = dsl_dataset_rename_snapshot(zc->zc_name,
3696 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3697 *at = '@';
3699 return (error);
3700 } else {
3701 if (zc->zc_objset_type == DMU_OST_ZVOL)
3702 (void) zvol_remove_minor(zc->zc_name);
3703 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3707 static int
3708 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3710 const char *propname = nvpair_name(pair);
3711 boolean_t issnap = (strchr(dsname, '@') != NULL);
3712 zfs_prop_t prop = zfs_name_to_prop(propname);
3713 uint64_t intval;
3714 int err;
3716 if (prop == ZPROP_INVAL) {
3717 if (zfs_prop_user(propname)) {
3718 if (err = zfs_secpolicy_write_perms(dsname,
3719 ZFS_DELEG_PERM_USERPROP, cr))
3720 return (err);
3721 return (0);
3724 if (!issnap && zfs_prop_userquota(propname)) {
3725 const char *perm = NULL;
3726 const char *uq_prefix =
3727 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3728 const char *gq_prefix =
3729 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3731 if (strncmp(propname, uq_prefix,
3732 strlen(uq_prefix)) == 0) {
3733 perm = ZFS_DELEG_PERM_USERQUOTA;
3734 } else if (strncmp(propname, gq_prefix,
3735 strlen(gq_prefix)) == 0) {
3736 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3737 } else {
3738 /* USERUSED and GROUPUSED are read-only */
3739 return (SET_ERROR(EINVAL));
3742 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3743 return (err);
3744 return (0);
3747 return (SET_ERROR(EINVAL));
3750 if (issnap)
3751 return (SET_ERROR(EINVAL));
3753 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3755 * dsl_prop_get_all_impl() returns properties in this
3756 * format.
3758 nvlist_t *attrs;
3759 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3760 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3761 &pair) == 0);
3765 * Check that this value is valid for this pool version
3767 switch (prop) {
3768 case ZFS_PROP_COMPRESSION:
3770 * If the user specified gzip compression, make sure
3771 * the SPA supports it. We ignore any errors here since
3772 * we'll catch them later.
3774 if (nvpair_value_uint64(pair, &intval) == 0) {
3775 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3776 intval <= ZIO_COMPRESS_GZIP_9 &&
3777 zfs_earlier_version(dsname,
3778 SPA_VERSION_GZIP_COMPRESSION)) {
3779 return (SET_ERROR(ENOTSUP));
3782 if (intval == ZIO_COMPRESS_ZLE &&
3783 zfs_earlier_version(dsname,
3784 SPA_VERSION_ZLE_COMPRESSION))
3785 return (SET_ERROR(ENOTSUP));
3787 if (intval == ZIO_COMPRESS_LZ4) {
3788 spa_t *spa;
3790 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3791 return (err);
3793 if (!spa_feature_is_enabled(spa,
3794 SPA_FEATURE_LZ4_COMPRESS)) {
3795 spa_close(spa, FTAG);
3796 return (SET_ERROR(ENOTSUP));
3798 spa_close(spa, FTAG);
3802 * If this is a bootable dataset then
3803 * verify that the compression algorithm
3804 * is supported for booting. We must return
3805 * something other than ENOTSUP since it
3806 * implies a downrev pool version.
3808 if (zfs_is_bootfs(dsname) &&
3809 !BOOTFS_COMPRESS_VALID(intval)) {
3810 return (SET_ERROR(ERANGE));
3813 break;
3815 case ZFS_PROP_COPIES:
3816 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3817 return (SET_ERROR(ENOTSUP));
3818 break;
3820 case ZFS_PROP_DEDUP:
3821 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3822 return (SET_ERROR(ENOTSUP));
3823 break;
3825 case ZFS_PROP_RECORDSIZE:
3826 /* Record sizes above 128k need the feature to be enabled */
3827 if (nvpair_value_uint64(pair, &intval) == 0 &&
3828 intval > SPA_OLD_MAXBLOCKSIZE) {
3829 spa_t *spa;
3832 * If this is a bootable dataset then
3833 * the we don't allow large (>128K) blocks,
3834 * because GRUB doesn't support them.
3836 if (zfs_is_bootfs(dsname) &&
3837 intval > SPA_OLD_MAXBLOCKSIZE) {
3838 return (SET_ERROR(EDOM));
3842 * We don't allow setting the property above 1MB,
3843 * unless the tunable has been changed.
3845 if (intval > zfs_max_recordsize ||
3846 intval > SPA_MAXBLOCKSIZE)
3847 return (SET_ERROR(EDOM));
3849 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3850 return (err);
3852 if (!spa_feature_is_enabled(spa,
3853 SPA_FEATURE_LARGE_BLOCKS)) {
3854 spa_close(spa, FTAG);
3855 return (SET_ERROR(ENOTSUP));
3857 spa_close(spa, FTAG);
3859 break;
3861 case ZFS_PROP_SHARESMB:
3862 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3863 return (SET_ERROR(ENOTSUP));
3864 break;
3866 case ZFS_PROP_ACLINHERIT:
3867 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3868 nvpair_value_uint64(pair, &intval) == 0) {
3869 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3870 zfs_earlier_version(dsname,
3871 SPA_VERSION_PASSTHROUGH_X))
3872 return (SET_ERROR(ENOTSUP));
3874 break;
3877 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3881 * Checks for a race condition to make sure we don't increment a feature flag
3882 * multiple times.
3884 static int
3885 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3887 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3888 spa_feature_t *featurep = arg;
3890 if (!spa_feature_is_active(spa, *featurep))
3891 return (0);
3892 else
3893 return (SET_ERROR(EBUSY));
3897 * The callback invoked on feature activation in the sync task caused by
3898 * zfs_prop_activate_feature.
3900 static void
3901 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3903 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3904 spa_feature_t *featurep = arg;
3906 spa_feature_incr(spa, *featurep, tx);
3910 * Activates a feature on a pool in response to a property setting. This
3911 * creates a new sync task which modifies the pool to reflect the feature
3912 * as being active.
3914 static int
3915 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
3917 int err;
3919 /* EBUSY here indicates that the feature is already active */
3920 err = dsl_sync_task(spa_name(spa),
3921 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3922 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
3924 if (err != 0 && err != EBUSY)
3925 return (err);
3926 else
3927 return (0);
3931 * Removes properties from the given props list that fail permission checks
3932 * needed to clear them and to restore them in case of a receive error. For each
3933 * property, make sure we have both set and inherit permissions.
3935 * Returns the first error encountered if any permission checks fail. If the
3936 * caller provides a non-NULL errlist, it also gives the complete list of names
3937 * of all the properties that failed a permission check along with the
3938 * corresponding error numbers. The caller is responsible for freeing the
3939 * returned errlist.
3941 * If every property checks out successfully, zero is returned and the list
3942 * pointed at by errlist is NULL.
3944 static int
3945 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3947 zfs_cmd_t *zc;
3948 nvpair_t *pair, *next_pair;
3949 nvlist_t *errors;
3950 int err, rv = 0;
3952 if (props == NULL)
3953 return (0);
3955 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3957 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3958 (void) strcpy(zc->zc_name, dataset);
3959 pair = nvlist_next_nvpair(props, NULL);
3960 while (pair != NULL) {
3961 next_pair = nvlist_next_nvpair(props, pair);
3963 (void) strcpy(zc->zc_value, nvpair_name(pair));
3964 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3965 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3966 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3967 VERIFY(nvlist_add_int32(errors,
3968 zc->zc_value, err) == 0);
3970 pair = next_pair;
3972 kmem_free(zc, sizeof (zfs_cmd_t));
3974 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3975 nvlist_free(errors);
3976 errors = NULL;
3977 } else {
3978 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3981 if (errlist == NULL)
3982 nvlist_free(errors);
3983 else
3984 *errlist = errors;
3986 return (rv);
3989 static boolean_t
3990 propval_equals(nvpair_t *p1, nvpair_t *p2)
3992 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3993 /* dsl_prop_get_all_impl() format */
3994 nvlist_t *attrs;
3995 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3996 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3997 &p1) == 0);
4000 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4001 nvlist_t *attrs;
4002 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4003 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4004 &p2) == 0);
4007 if (nvpair_type(p1) != nvpair_type(p2))
4008 return (B_FALSE);
4010 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4011 char *valstr1, *valstr2;
4013 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4014 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4015 return (strcmp(valstr1, valstr2) == 0);
4016 } else {
4017 uint64_t intval1, intval2;
4019 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4020 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4021 return (intval1 == intval2);
4026 * Remove properties from props if they are not going to change (as determined
4027 * by comparison with origprops). Remove them from origprops as well, since we
4028 * do not need to clear or restore properties that won't change.
4030 static void
4031 props_reduce(nvlist_t *props, nvlist_t *origprops)
4033 nvpair_t *pair, *next_pair;
4035 if (origprops == NULL)
4036 return; /* all props need to be received */
4038 pair = nvlist_next_nvpair(props, NULL);
4039 while (pair != NULL) {
4040 const char *propname = nvpair_name(pair);
4041 nvpair_t *match;
4043 next_pair = nvlist_next_nvpair(props, pair);
4045 if ((nvlist_lookup_nvpair(origprops, propname,
4046 &match) != 0) || !propval_equals(pair, match))
4047 goto next; /* need to set received value */
4049 /* don't clear the existing received value */
4050 (void) nvlist_remove_nvpair(origprops, match);
4051 /* don't bother receiving the property */
4052 (void) nvlist_remove_nvpair(props, pair);
4053 next:
4054 pair = next_pair;
4058 #ifdef DEBUG
4059 static boolean_t zfs_ioc_recv_inject_err;
4060 #endif
4063 * inputs:
4064 * zc_name name of containing filesystem
4065 * zc_nvlist_src{_size} nvlist of properties to apply
4066 * zc_value name of snapshot to create
4067 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4068 * zc_cookie file descriptor to recv from
4069 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4070 * zc_guid force flag
4071 * zc_cleanup_fd cleanup-on-exit file descriptor
4072 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4074 * outputs:
4075 * zc_cookie number of bytes read
4076 * zc_nvlist_dst{_size} error for each unapplied received property
4077 * zc_obj zprop_errflags_t
4078 * zc_action_handle handle for this guid/ds mapping
4080 static int
4081 zfs_ioc_recv(zfs_cmd_t *zc)
4083 file_t *fp;
4084 dmu_recv_cookie_t drc;
4085 boolean_t force = (boolean_t)zc->zc_guid;
4086 int fd;
4087 int error = 0;
4088 int props_error = 0;
4089 nvlist_t *errors;
4090 offset_t off;
4091 nvlist_t *props = NULL; /* sent properties */
4092 nvlist_t *origprops = NULL; /* existing properties */
4093 char *origin = NULL;
4094 char *tosnap;
4095 char tofs[ZFS_MAXNAMELEN];
4096 boolean_t first_recvd_props = B_FALSE;
4098 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4099 strchr(zc->zc_value, '@') == NULL ||
4100 strchr(zc->zc_value, '%'))
4101 return (SET_ERROR(EINVAL));
4103 (void) strcpy(tofs, zc->zc_value);
4104 tosnap = strchr(tofs, '@');
4105 *tosnap++ = '\0';
4107 if (zc->zc_nvlist_src != NULL &&
4108 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4109 zc->zc_iflags, &props)) != 0)
4110 return (error);
4112 fd = zc->zc_cookie;
4113 fp = getf(fd);
4114 if (fp == NULL) {
4115 nvlist_free(props);
4116 return (SET_ERROR(EBADF));
4119 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4121 if (zc->zc_string[0])
4122 origin = zc->zc_string;
4124 error = dmu_recv_begin(tofs, tosnap,
4125 &zc->zc_begin_record, force, origin, &drc);
4126 if (error != 0)
4127 goto out;
4130 * Set properties before we receive the stream so that they are applied
4131 * to the new data. Note that we must call dmu_recv_stream() if
4132 * dmu_recv_begin() succeeds.
4134 if (props != NULL && !drc.drc_newfs) {
4135 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4136 SPA_VERSION_RECVD_PROPS &&
4137 !dsl_prop_get_hasrecvd(tofs))
4138 first_recvd_props = B_TRUE;
4141 * If new received properties are supplied, they are to
4142 * completely replace the existing received properties, so stash
4143 * away the existing ones.
4145 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4146 nvlist_t *errlist = NULL;
4148 * Don't bother writing a property if its value won't
4149 * change (and avoid the unnecessary security checks).
4151 * The first receive after SPA_VERSION_RECVD_PROPS is a
4152 * special case where we blow away all local properties
4153 * regardless.
4155 if (!first_recvd_props)
4156 props_reduce(props, origprops);
4157 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4158 (void) nvlist_merge(errors, errlist, 0);
4159 nvlist_free(errlist);
4161 if (clear_received_props(tofs, origprops,
4162 first_recvd_props ? NULL : props) != 0)
4163 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4164 } else {
4165 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4169 if (props != NULL) {
4170 props_error = dsl_prop_set_hasrecvd(tofs);
4172 if (props_error == 0) {
4173 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4174 props, errors);
4178 if (zc->zc_nvlist_dst_size != 0 &&
4179 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4180 put_nvlist(zc, errors) != 0)) {
4182 * Caller made zc->zc_nvlist_dst less than the minimum expected
4183 * size or supplied an invalid address.
4185 props_error = SET_ERROR(EINVAL);
4188 off = fp->f_offset;
4189 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4190 &zc->zc_action_handle);
4192 if (error == 0) {
4193 zfsvfs_t *zfsvfs = NULL;
4195 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4196 /* online recv */
4197 int end_err;
4199 error = zfs_suspend_fs(zfsvfs);
4201 * If the suspend fails, then the recv_end will
4202 * likely also fail, and clean up after itself.
4204 end_err = dmu_recv_end(&drc, zfsvfs);
4205 if (error == 0)
4206 error = zfs_resume_fs(zfsvfs, tofs);
4207 error = error ? error : end_err;
4208 VFS_RELE(zfsvfs->z_vfs);
4209 } else {
4210 error = dmu_recv_end(&drc, NULL);
4214 zc->zc_cookie = off - fp->f_offset;
4215 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4216 fp->f_offset = off;
4218 #ifdef DEBUG
4219 if (zfs_ioc_recv_inject_err) {
4220 zfs_ioc_recv_inject_err = B_FALSE;
4221 error = 1;
4223 #endif
4225 * On error, restore the original props.
4227 if (error != 0 && props != NULL && !drc.drc_newfs) {
4228 if (clear_received_props(tofs, props, NULL) != 0) {
4230 * We failed to clear the received properties.
4231 * Since we may have left a $recvd value on the
4232 * system, we can't clear the $hasrecvd flag.
4234 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4235 } else if (first_recvd_props) {
4236 dsl_prop_unset_hasrecvd(tofs);
4239 if (origprops == NULL && !drc.drc_newfs) {
4240 /* We failed to stash the original properties. */
4241 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4245 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4246 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4247 * explictly if we're restoring local properties cleared in the
4248 * first new-style receive.
4250 if (origprops != NULL &&
4251 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4252 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4253 origprops, NULL) != 0) {
4255 * We stashed the original properties but failed to
4256 * restore them.
4258 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4261 out:
4262 nvlist_free(props);
4263 nvlist_free(origprops);
4264 nvlist_free(errors);
4265 releasef(fd);
4267 if (error == 0)
4268 error = props_error;
4270 return (error);
4274 * inputs:
4275 * zc_name name of snapshot to send
4276 * zc_cookie file descriptor to send stream to
4277 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4278 * zc_sendobj objsetid of snapshot to send
4279 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4280 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4281 * output size in zc_objset_type.
4282 * zc_flags lzc_send_flags
4284 * outputs:
4285 * zc_objset_type estimated size, if zc_guid is set
4287 static int
4288 zfs_ioc_send(zfs_cmd_t *zc)
4290 int error;
4291 offset_t off;
4292 boolean_t estimate = (zc->zc_guid != 0);
4293 boolean_t embedok = (zc->zc_flags & 0x1);
4294 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4296 if (zc->zc_obj != 0) {
4297 dsl_pool_t *dp;
4298 dsl_dataset_t *tosnap;
4300 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4301 if (error != 0)
4302 return (error);
4304 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4305 if (error != 0) {
4306 dsl_pool_rele(dp, FTAG);
4307 return (error);
4310 if (dsl_dir_is_clone(tosnap->ds_dir))
4311 zc->zc_fromobj =
4312 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4313 dsl_dataset_rele(tosnap, FTAG);
4314 dsl_pool_rele(dp, FTAG);
4317 if (estimate) {
4318 dsl_pool_t *dp;
4319 dsl_dataset_t *tosnap;
4320 dsl_dataset_t *fromsnap = NULL;
4322 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4323 if (error != 0)
4324 return (error);
4326 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4327 if (error != 0) {
4328 dsl_pool_rele(dp, FTAG);
4329 return (error);
4332 if (zc->zc_fromobj != 0) {
4333 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4334 FTAG, &fromsnap);
4335 if (error != 0) {
4336 dsl_dataset_rele(tosnap, FTAG);
4337 dsl_pool_rele(dp, FTAG);
4338 return (error);
4342 error = dmu_send_estimate(tosnap, fromsnap,
4343 &zc->zc_objset_type);
4345 if (fromsnap != NULL)
4346 dsl_dataset_rele(fromsnap, FTAG);
4347 dsl_dataset_rele(tosnap, FTAG);
4348 dsl_pool_rele(dp, FTAG);
4349 } else {
4350 file_t *fp = getf(zc->zc_cookie);
4351 if (fp == NULL)
4352 return (SET_ERROR(EBADF));
4354 off = fp->f_offset;
4355 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4356 zc->zc_fromobj, embedok, large_block_ok,
4357 zc->zc_cookie, fp->f_vnode, &off);
4359 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4360 fp->f_offset = off;
4361 releasef(zc->zc_cookie);
4363 return (error);
4367 * inputs:
4368 * zc_name name of snapshot on which to report progress
4369 * zc_cookie file descriptor of send stream
4371 * outputs:
4372 * zc_cookie number of bytes written in send stream thus far
4374 static int
4375 zfs_ioc_send_progress(zfs_cmd_t *zc)
4377 dsl_pool_t *dp;
4378 dsl_dataset_t *ds;
4379 dmu_sendarg_t *dsp = NULL;
4380 int error;
4382 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4383 if (error != 0)
4384 return (error);
4386 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4387 if (error != 0) {
4388 dsl_pool_rele(dp, FTAG);
4389 return (error);
4392 mutex_enter(&ds->ds_sendstream_lock);
4395 * Iterate over all the send streams currently active on this dataset.
4396 * If there's one which matches the specified file descriptor _and_ the
4397 * stream was started by the current process, return the progress of
4398 * that stream.
4400 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4401 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4402 if (dsp->dsa_outfd == zc->zc_cookie &&
4403 dsp->dsa_proc == curproc)
4404 break;
4407 if (dsp != NULL)
4408 zc->zc_cookie = *(dsp->dsa_off);
4409 else
4410 error = SET_ERROR(ENOENT);
4412 mutex_exit(&ds->ds_sendstream_lock);
4413 dsl_dataset_rele(ds, FTAG);
4414 dsl_pool_rele(dp, FTAG);
4415 return (error);
4418 static int
4419 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4421 int id, error;
4423 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4424 &zc->zc_inject_record);
4426 if (error == 0)
4427 zc->zc_guid = (uint64_t)id;
4429 return (error);
4432 static int
4433 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4435 return (zio_clear_fault((int)zc->zc_guid));
4438 static int
4439 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4441 int id = (int)zc->zc_guid;
4442 int error;
4444 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4445 &zc->zc_inject_record);
4447 zc->zc_guid = id;
4449 return (error);
4452 static int
4453 zfs_ioc_error_log(zfs_cmd_t *zc)
4455 spa_t *spa;
4456 int error;
4457 size_t count = (size_t)zc->zc_nvlist_dst_size;
4459 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4460 return (error);
4462 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4463 &count);
4464 if (error == 0)
4465 zc->zc_nvlist_dst_size = count;
4466 else
4467 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4469 spa_close(spa, FTAG);
4471 return (error);
4474 static int
4475 zfs_ioc_clear(zfs_cmd_t *zc)
4477 spa_t *spa;
4478 vdev_t *vd;
4479 int error;
4482 * On zpool clear we also fix up missing slogs
4484 mutex_enter(&spa_namespace_lock);
4485 spa = spa_lookup(zc->zc_name);
4486 if (spa == NULL) {
4487 mutex_exit(&spa_namespace_lock);
4488 return (SET_ERROR(EIO));
4490 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4491 /* we need to let spa_open/spa_load clear the chains */
4492 spa_set_log_state(spa, SPA_LOG_CLEAR);
4494 spa->spa_last_open_failed = 0;
4495 mutex_exit(&spa_namespace_lock);
4497 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4498 error = spa_open(zc->zc_name, &spa, FTAG);
4499 } else {
4500 nvlist_t *policy;
4501 nvlist_t *config = NULL;
4503 if (zc->zc_nvlist_src == NULL)
4504 return (SET_ERROR(EINVAL));
4506 if ((error = get_nvlist(zc->zc_nvlist_src,
4507 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4508 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4509 policy, &config);
4510 if (config != NULL) {
4511 int err;
4513 if ((err = put_nvlist(zc, config)) != 0)
4514 error = err;
4515 nvlist_free(config);
4517 nvlist_free(policy);
4521 if (error != 0)
4522 return (error);
4524 spa_vdev_state_enter(spa, SCL_NONE);
4526 if (zc->zc_guid == 0) {
4527 vd = NULL;
4528 } else {
4529 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4530 if (vd == NULL) {
4531 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4532 spa_close(spa, FTAG);
4533 return (SET_ERROR(ENODEV));
4537 vdev_clear(spa, vd);
4539 (void) spa_vdev_state_exit(spa, NULL, 0);
4542 * Resume any suspended I/Os.
4544 if (zio_resume(spa) != 0)
4545 error = SET_ERROR(EIO);
4547 spa_close(spa, FTAG);
4549 return (error);
4552 static int
4553 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4555 spa_t *spa;
4556 int error;
4558 error = spa_open(zc->zc_name, &spa, FTAG);
4559 if (error != 0)
4560 return (error);
4562 spa_vdev_state_enter(spa, SCL_NONE);
4565 * If a resilver is already in progress then set the
4566 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4567 * the scan as a side effect of the reopen. Otherwise, let
4568 * vdev_open() decided if a resilver is required.
4570 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4571 vdev_reopen(spa->spa_root_vdev);
4572 spa->spa_scrub_reopen = B_FALSE;
4574 (void) spa_vdev_state_exit(spa, NULL, 0);
4575 spa_close(spa, FTAG);
4576 return (0);
4579 * inputs:
4580 * zc_name name of filesystem
4581 * zc_value name of origin snapshot
4583 * outputs:
4584 * zc_string name of conflicting snapshot, if there is one
4586 static int
4587 zfs_ioc_promote(zfs_cmd_t *zc)
4589 char *cp;
4592 * We don't need to unmount *all* the origin fs's snapshots, but
4593 * it's easier.
4595 cp = strchr(zc->zc_value, '@');
4596 if (cp)
4597 *cp = '\0';
4598 (void) dmu_objset_find(zc->zc_value,
4599 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4600 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4604 * Retrieve a single {user|group}{used|quota}@... property.
4606 * inputs:
4607 * zc_name name of filesystem
4608 * zc_objset_type zfs_userquota_prop_t
4609 * zc_value domain name (eg. "S-1-234-567-89")
4610 * zc_guid RID/UID/GID
4612 * outputs:
4613 * zc_cookie property value
4615 static int
4616 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4618 zfsvfs_t *zfsvfs;
4619 int error;
4621 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4622 return (SET_ERROR(EINVAL));
4624 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4625 if (error != 0)
4626 return (error);
4628 error = zfs_userspace_one(zfsvfs,
4629 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4630 zfsvfs_rele(zfsvfs, FTAG);
4632 return (error);
4636 * inputs:
4637 * zc_name name of filesystem
4638 * zc_cookie zap cursor
4639 * zc_objset_type zfs_userquota_prop_t
4640 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4642 * outputs:
4643 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4644 * zc_cookie zap cursor
4646 static int
4647 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4649 zfsvfs_t *zfsvfs;
4650 int bufsize = zc->zc_nvlist_dst_size;
4652 if (bufsize <= 0)
4653 return (SET_ERROR(ENOMEM));
4655 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4656 if (error != 0)
4657 return (error);
4659 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4661 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4662 buf, &zc->zc_nvlist_dst_size);
4664 if (error == 0) {
4665 error = xcopyout(buf,
4666 (void *)(uintptr_t)zc->zc_nvlist_dst,
4667 zc->zc_nvlist_dst_size);
4669 kmem_free(buf, bufsize);
4670 zfsvfs_rele(zfsvfs, FTAG);
4672 return (error);
4676 * inputs:
4677 * zc_name name of filesystem
4679 * outputs:
4680 * none
4682 static int
4683 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4685 objset_t *os;
4686 int error = 0;
4687 zfsvfs_t *zfsvfs;
4689 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4690 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4692 * If userused is not enabled, it may be because the
4693 * objset needs to be closed & reopened (to grow the
4694 * objset_phys_t). Suspend/resume the fs will do that.
4696 error = zfs_suspend_fs(zfsvfs);
4697 if (error == 0) {
4698 dmu_objset_refresh_ownership(zfsvfs->z_os,
4699 zfsvfs);
4700 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4703 if (error == 0)
4704 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4705 VFS_RELE(zfsvfs->z_vfs);
4706 } else {
4707 /* XXX kind of reading contents without owning */
4708 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4709 if (error != 0)
4710 return (error);
4712 error = dmu_objset_userspace_upgrade(os);
4713 dmu_objset_rele(os, FTAG);
4716 return (error);
4720 * We don't want to have a hard dependency
4721 * against some special symbols in sharefs
4722 * nfs, and smbsrv. Determine them if needed when
4723 * the first file system is shared.
4724 * Neither sharefs, nfs or smbsrv are unloadable modules.
4726 int (*znfsexport_fs)(void *arg);
4727 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4728 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4730 int zfs_nfsshare_inited;
4731 int zfs_smbshare_inited;
4733 ddi_modhandle_t nfs_mod;
4734 ddi_modhandle_t sharefs_mod;
4735 ddi_modhandle_t smbsrv_mod;
4736 kmutex_t zfs_share_lock;
4738 static int
4739 zfs_init_sharefs()
4741 int error;
4743 ASSERT(MUTEX_HELD(&zfs_share_lock));
4744 /* Both NFS and SMB shares also require sharetab support. */
4745 if (sharefs_mod == NULL && ((sharefs_mod =
4746 ddi_modopen("fs/sharefs",
4747 KRTLD_MODE_FIRST, &error)) == NULL)) {
4748 return (SET_ERROR(ENOSYS));
4750 if (zshare_fs == NULL && ((zshare_fs =
4751 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4752 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4753 return (SET_ERROR(ENOSYS));
4755 return (0);
4758 static int
4759 zfs_ioc_share(zfs_cmd_t *zc)
4761 int error;
4762 int opcode;
4764 switch (zc->zc_share.z_sharetype) {
4765 case ZFS_SHARE_NFS:
4766 case ZFS_UNSHARE_NFS:
4767 if (zfs_nfsshare_inited == 0) {
4768 mutex_enter(&zfs_share_lock);
4769 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4770 KRTLD_MODE_FIRST, &error)) == NULL)) {
4771 mutex_exit(&zfs_share_lock);
4772 return (SET_ERROR(ENOSYS));
4774 if (znfsexport_fs == NULL &&
4775 ((znfsexport_fs = (int (*)(void *))
4776 ddi_modsym(nfs_mod,
4777 "nfs_export", &error)) == NULL)) {
4778 mutex_exit(&zfs_share_lock);
4779 return (SET_ERROR(ENOSYS));
4781 error = zfs_init_sharefs();
4782 if (error != 0) {
4783 mutex_exit(&zfs_share_lock);
4784 return (SET_ERROR(ENOSYS));
4786 zfs_nfsshare_inited = 1;
4787 mutex_exit(&zfs_share_lock);
4789 break;
4790 case ZFS_SHARE_SMB:
4791 case ZFS_UNSHARE_SMB:
4792 if (zfs_smbshare_inited == 0) {
4793 mutex_enter(&zfs_share_lock);
4794 if (smbsrv_mod == NULL && ((smbsrv_mod =
4795 ddi_modopen("drv/smbsrv",
4796 KRTLD_MODE_FIRST, &error)) == NULL)) {
4797 mutex_exit(&zfs_share_lock);
4798 return (SET_ERROR(ENOSYS));
4800 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4801 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4802 "smb_server_share", &error)) == NULL)) {
4803 mutex_exit(&zfs_share_lock);
4804 return (SET_ERROR(ENOSYS));
4806 error = zfs_init_sharefs();
4807 if (error != 0) {
4808 mutex_exit(&zfs_share_lock);
4809 return (SET_ERROR(ENOSYS));
4811 zfs_smbshare_inited = 1;
4812 mutex_exit(&zfs_share_lock);
4814 break;
4815 default:
4816 return (SET_ERROR(EINVAL));
4819 switch (zc->zc_share.z_sharetype) {
4820 case ZFS_SHARE_NFS:
4821 case ZFS_UNSHARE_NFS:
4822 if (error =
4823 znfsexport_fs((void *)
4824 (uintptr_t)zc->zc_share.z_exportdata))
4825 return (error);
4826 break;
4827 case ZFS_SHARE_SMB:
4828 case ZFS_UNSHARE_SMB:
4829 if (error = zsmbexport_fs((void *)
4830 (uintptr_t)zc->zc_share.z_exportdata,
4831 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4832 B_TRUE: B_FALSE)) {
4833 return (error);
4835 break;
4838 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4839 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4840 SHAREFS_ADD : SHAREFS_REMOVE;
4843 * Add or remove share from sharetab
4845 error = zshare_fs(opcode,
4846 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4847 zc->zc_share.z_sharemax);
4849 return (error);
4853 ace_t full_access[] = {
4854 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4858 * inputs:
4859 * zc_name name of containing filesystem
4860 * zc_obj object # beyond which we want next in-use object #
4862 * outputs:
4863 * zc_obj next in-use object #
4865 static int
4866 zfs_ioc_next_obj(zfs_cmd_t *zc)
4868 objset_t *os = NULL;
4869 int error;
4871 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4872 if (error != 0)
4873 return (error);
4875 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4876 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
4878 dmu_objset_rele(os, FTAG);
4879 return (error);
4883 * inputs:
4884 * zc_name name of filesystem
4885 * zc_value prefix name for snapshot
4886 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4888 * outputs:
4889 * zc_value short name of new snapshot
4891 static int
4892 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4894 char *snap_name;
4895 char *hold_name;
4896 int error;
4897 minor_t minor;
4899 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4900 if (error != 0)
4901 return (error);
4903 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4904 (u_longlong_t)ddi_get_lbolt64());
4905 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4907 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4908 hold_name);
4909 if (error == 0)
4910 (void) strcpy(zc->zc_value, snap_name);
4911 strfree(snap_name);
4912 strfree(hold_name);
4913 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4914 return (error);
4918 * inputs:
4919 * zc_name name of "to" snapshot
4920 * zc_value name of "from" snapshot
4921 * zc_cookie file descriptor to write diff data on
4923 * outputs:
4924 * dmu_diff_record_t's to the file descriptor
4926 static int
4927 zfs_ioc_diff(zfs_cmd_t *zc)
4929 file_t *fp;
4930 offset_t off;
4931 int error;
4933 fp = getf(zc->zc_cookie);
4934 if (fp == NULL)
4935 return (SET_ERROR(EBADF));
4937 off = fp->f_offset;
4939 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
4941 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4942 fp->f_offset = off;
4943 releasef(zc->zc_cookie);
4945 return (error);
4949 * Remove all ACL files in shares dir
4951 static int
4952 zfs_smb_acl_purge(znode_t *dzp)
4954 zap_cursor_t zc;
4955 zap_attribute_t zap;
4956 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4957 int error;
4959 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4960 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4961 zap_cursor_advance(&zc)) {
4962 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4963 NULL, 0)) != 0)
4964 break;
4966 zap_cursor_fini(&zc);
4967 return (error);
4970 static int
4971 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4973 vnode_t *vp;
4974 znode_t *dzp;
4975 vnode_t *resourcevp = NULL;
4976 znode_t *sharedir;
4977 zfsvfs_t *zfsvfs;
4978 nvlist_t *nvlist;
4979 char *src, *target;
4980 vattr_t vattr;
4981 vsecattr_t vsec;
4982 int error = 0;
4984 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4985 NO_FOLLOW, NULL, &vp)) != 0)
4986 return (error);
4988 /* Now make sure mntpnt and dataset are ZFS */
4990 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4991 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4992 zc->zc_name) != 0)) {
4993 VN_RELE(vp);
4994 return (SET_ERROR(EINVAL));
4997 dzp = VTOZ(vp);
4998 zfsvfs = dzp->z_zfsvfs;
4999 ZFS_ENTER(zfsvfs);
5002 * Create share dir if its missing.
5004 mutex_enter(&zfsvfs->z_lock);
5005 if (zfsvfs->z_shares_dir == 0) {
5006 dmu_tx_t *tx;
5008 tx = dmu_tx_create(zfsvfs->z_os);
5009 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5010 ZFS_SHARES_DIR);
5011 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5012 error = dmu_tx_assign(tx, TXG_WAIT);
5013 if (error != 0) {
5014 dmu_tx_abort(tx);
5015 } else {
5016 error = zfs_create_share_dir(zfsvfs, tx);
5017 dmu_tx_commit(tx);
5019 if (error != 0) {
5020 mutex_exit(&zfsvfs->z_lock);
5021 VN_RELE(vp);
5022 ZFS_EXIT(zfsvfs);
5023 return (error);
5026 mutex_exit(&zfsvfs->z_lock);
5028 ASSERT(zfsvfs->z_shares_dir);
5029 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5030 VN_RELE(vp);
5031 ZFS_EXIT(zfsvfs);
5032 return (error);
5035 switch (zc->zc_cookie) {
5036 case ZFS_SMB_ACL_ADD:
5037 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5038 vattr.va_type = VREG;
5039 vattr.va_mode = S_IFREG|0777;
5040 vattr.va_uid = 0;
5041 vattr.va_gid = 0;
5043 vsec.vsa_mask = VSA_ACE;
5044 vsec.vsa_aclentp = &full_access;
5045 vsec.vsa_aclentsz = sizeof (full_access);
5046 vsec.vsa_aclcnt = 1;
5048 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5049 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5050 if (resourcevp)
5051 VN_RELE(resourcevp);
5052 break;
5054 case ZFS_SMB_ACL_REMOVE:
5055 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5056 NULL, 0);
5057 break;
5059 case ZFS_SMB_ACL_RENAME:
5060 if ((error = get_nvlist(zc->zc_nvlist_src,
5061 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5062 VN_RELE(vp);
5063 ZFS_EXIT(zfsvfs);
5064 return (error);
5066 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5067 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5068 &target)) {
5069 VN_RELE(vp);
5070 VN_RELE(ZTOV(sharedir));
5071 ZFS_EXIT(zfsvfs);
5072 nvlist_free(nvlist);
5073 return (error);
5075 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5076 kcred, NULL, 0);
5077 nvlist_free(nvlist);
5078 break;
5080 case ZFS_SMB_ACL_PURGE:
5081 error = zfs_smb_acl_purge(sharedir);
5082 break;
5084 default:
5085 error = SET_ERROR(EINVAL);
5086 break;
5089 VN_RELE(vp);
5090 VN_RELE(ZTOV(sharedir));
5092 ZFS_EXIT(zfsvfs);
5094 return (error);
5098 * innvl: {
5099 * "holds" -> { snapname -> holdname (string), ... }
5100 * (optional) "cleanup_fd" -> fd (int32)
5103 * outnvl: {
5104 * snapname -> error value (int32)
5105 * ...
5108 /* ARGSUSED */
5109 static int
5110 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5112 nvpair_t *pair;
5113 nvlist_t *holds;
5114 int cleanup_fd = -1;
5115 int error;
5116 minor_t minor = 0;
5118 error = nvlist_lookup_nvlist(args, "holds", &holds);
5119 if (error != 0)
5120 return (SET_ERROR(EINVAL));
5122 /* make sure the user didn't pass us any invalid (empty) tags */
5123 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5124 pair = nvlist_next_nvpair(holds, pair)) {
5125 char *htag;
5127 error = nvpair_value_string(pair, &htag);
5128 if (error != 0)
5129 return (SET_ERROR(error));
5131 if (strlen(htag) == 0)
5132 return (SET_ERROR(EINVAL));
5135 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5136 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5137 if (error != 0)
5138 return (error);
5141 error = dsl_dataset_user_hold(holds, minor, errlist);
5142 if (minor != 0)
5143 zfs_onexit_fd_rele(cleanup_fd);
5144 return (error);
5148 * innvl is not used.
5150 * outnvl: {
5151 * holdname -> time added (uint64 seconds since epoch)
5152 * ...
5155 /* ARGSUSED */
5156 static int
5157 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5159 return (dsl_dataset_get_holds(snapname, outnvl));
5163 * innvl: {
5164 * snapname -> { holdname, ... }
5165 * ...
5168 * outnvl: {
5169 * snapname -> error value (int32)
5170 * ...
5173 /* ARGSUSED */
5174 static int
5175 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5177 return (dsl_dataset_user_release(holds, errlist));
5181 * inputs:
5182 * zc_name name of new filesystem or snapshot
5183 * zc_value full name of old snapshot
5185 * outputs:
5186 * zc_cookie space in bytes
5187 * zc_objset_type compressed space in bytes
5188 * zc_perm_action uncompressed space in bytes
5190 static int
5191 zfs_ioc_space_written(zfs_cmd_t *zc)
5193 int error;
5194 dsl_pool_t *dp;
5195 dsl_dataset_t *new, *old;
5197 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5198 if (error != 0)
5199 return (error);
5200 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5201 if (error != 0) {
5202 dsl_pool_rele(dp, FTAG);
5203 return (error);
5205 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5206 if (error != 0) {
5207 dsl_dataset_rele(new, FTAG);
5208 dsl_pool_rele(dp, FTAG);
5209 return (error);
5212 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5213 &zc->zc_objset_type, &zc->zc_perm_action);
5214 dsl_dataset_rele(old, FTAG);
5215 dsl_dataset_rele(new, FTAG);
5216 dsl_pool_rele(dp, FTAG);
5217 return (error);
5221 * innvl: {
5222 * "firstsnap" -> snapshot name
5225 * outnvl: {
5226 * "used" -> space in bytes
5227 * "compressed" -> compressed space in bytes
5228 * "uncompressed" -> uncompressed space in bytes
5231 static int
5232 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5234 int error;
5235 dsl_pool_t *dp;
5236 dsl_dataset_t *new, *old;
5237 char *firstsnap;
5238 uint64_t used, comp, uncomp;
5240 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5241 return (SET_ERROR(EINVAL));
5243 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5244 if (error != 0)
5245 return (error);
5247 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5248 if (error != 0) {
5249 dsl_pool_rele(dp, FTAG);
5250 return (error);
5252 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5253 if (error != 0) {
5254 dsl_dataset_rele(new, FTAG);
5255 dsl_pool_rele(dp, FTAG);
5256 return (error);
5259 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5260 dsl_dataset_rele(old, FTAG);
5261 dsl_dataset_rele(new, FTAG);
5262 dsl_pool_rele(dp, FTAG);
5263 fnvlist_add_uint64(outnvl, "used", used);
5264 fnvlist_add_uint64(outnvl, "compressed", comp);
5265 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5266 return (error);
5270 * innvl: {
5271 * "fd" -> file descriptor to write stream to (int32)
5272 * (optional) "fromsnap" -> full snap name to send an incremental from
5273 * (optional) "largeblockok" -> (value ignored)
5274 * indicates that blocks > 128KB are permitted
5275 * (optional) "embedok" -> (value ignored)
5276 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5279 * outnvl is unused
5281 /* ARGSUSED */
5282 static int
5283 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5285 int error;
5286 offset_t off;
5287 char *fromname = NULL;
5288 int fd;
5289 boolean_t largeblockok;
5290 boolean_t embedok;
5292 error = nvlist_lookup_int32(innvl, "fd", &fd);
5293 if (error != 0)
5294 return (SET_ERROR(EINVAL));
5296 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5298 largeblockok = nvlist_exists(innvl, "largeblockok");
5299 embedok = nvlist_exists(innvl, "embedok");
5301 file_t *fp = getf(fd);
5302 if (fp == NULL)
5303 return (SET_ERROR(EBADF));
5305 off = fp->f_offset;
5306 error = dmu_send(snapname, fromname, embedok, largeblockok,
5307 fd, fp->f_vnode, &off);
5309 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5310 fp->f_offset = off;
5311 releasef(fd);
5312 return (error);
5316 * Determine approximately how large a zfs send stream will be -- the number
5317 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5319 * innvl: {
5320 * (optional) "from" -> full snap or bookmark name to send an incremental
5321 * from
5324 * outnvl: {
5325 * "space" -> bytes of space (uint64)
5328 static int
5329 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5331 dsl_pool_t *dp;
5332 dsl_dataset_t *tosnap;
5333 int error;
5334 char *fromname;
5335 uint64_t space;
5337 error = dsl_pool_hold(snapname, FTAG, &dp);
5338 if (error != 0)
5339 return (error);
5341 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5342 if (error != 0) {
5343 dsl_pool_rele(dp, FTAG);
5344 return (error);
5347 error = nvlist_lookup_string(innvl, "from", &fromname);
5348 if (error == 0) {
5349 if (strchr(fromname, '@') != NULL) {
5351 * If from is a snapshot, hold it and use the more
5352 * efficient dmu_send_estimate to estimate send space
5353 * size using deadlists.
5355 dsl_dataset_t *fromsnap;
5356 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5357 if (error != 0)
5358 goto out;
5359 error = dmu_send_estimate(tosnap, fromsnap, &space);
5360 dsl_dataset_rele(fromsnap, FTAG);
5361 } else if (strchr(fromname, '#') != NULL) {
5363 * If from is a bookmark, fetch the creation TXG of the
5364 * snapshot it was created from and use that to find
5365 * blocks that were born after it.
5367 zfs_bookmark_phys_t frombm;
5369 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5370 &frombm);
5371 if (error != 0)
5372 goto out;
5373 error = dmu_send_estimate_from_txg(tosnap,
5374 frombm.zbm_creation_txg, &space);
5375 } else {
5377 * from is not properly formatted as a snapshot or
5378 * bookmark
5380 error = SET_ERROR(EINVAL);
5381 goto out;
5383 } else {
5384 // If estimating the size of a full send, use dmu_send_estimate
5385 error = dmu_send_estimate(tosnap, NULL, &space);
5388 fnvlist_add_uint64(outnvl, "space", space);
5390 out:
5391 dsl_dataset_rele(tosnap, FTAG);
5392 dsl_pool_rele(dp, FTAG);
5393 return (error);
5396 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5398 static void
5399 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5400 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5401 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5403 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5405 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5406 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5407 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5408 ASSERT3P(vec->zvec_func, ==, NULL);
5410 vec->zvec_legacy_func = func;
5411 vec->zvec_secpolicy = secpolicy;
5412 vec->zvec_namecheck = namecheck;
5413 vec->zvec_allow_log = log_history;
5414 vec->zvec_pool_check = pool_check;
5418 * See the block comment at the beginning of this file for details on
5419 * each argument to this function.
5421 static void
5422 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5423 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5424 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5425 boolean_t allow_log)
5427 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5429 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5430 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5431 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5432 ASSERT3P(vec->zvec_func, ==, NULL);
5434 /* if we are logging, the name must be valid */
5435 ASSERT(!allow_log || namecheck != NO_NAME);
5437 vec->zvec_name = name;
5438 vec->zvec_func = func;
5439 vec->zvec_secpolicy = secpolicy;
5440 vec->zvec_namecheck = namecheck;
5441 vec->zvec_pool_check = pool_check;
5442 vec->zvec_smush_outnvlist = smush_outnvlist;
5443 vec->zvec_allow_log = allow_log;
5446 static void
5447 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5448 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5449 zfs_ioc_poolcheck_t pool_check)
5451 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5452 POOL_NAME, log_history, pool_check);
5455 static void
5456 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5457 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5459 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5460 DATASET_NAME, B_FALSE, pool_check);
5463 static void
5464 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5466 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5467 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5470 static void
5471 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5472 zfs_secpolicy_func_t *secpolicy)
5474 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5475 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5478 static void
5479 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5480 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5482 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5483 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5486 static void
5487 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5489 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5490 zfs_secpolicy_read);
5493 static void
5494 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5495 zfs_secpolicy_func_t *secpolicy)
5497 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5498 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5501 static void
5502 zfs_ioctl_init(void)
5504 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5505 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5506 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5508 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5509 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5510 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5512 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5513 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5514 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5516 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5517 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5518 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5520 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5521 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5522 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5524 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5525 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5526 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5528 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5529 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5530 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5532 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5533 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5534 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5536 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5537 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5538 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5539 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5540 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5541 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5543 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5544 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5545 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5547 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5548 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5549 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5551 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5552 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5553 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5555 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5556 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5557 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5559 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5560 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5561 POOL_NAME,
5562 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5564 /* IOCTLS that use the legacy function signature */
5566 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5567 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5569 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5570 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5571 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5572 zfs_ioc_pool_scan);
5573 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5574 zfs_ioc_pool_upgrade);
5575 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5576 zfs_ioc_vdev_add);
5577 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5578 zfs_ioc_vdev_remove);
5579 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5580 zfs_ioc_vdev_set_state);
5581 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5582 zfs_ioc_vdev_attach);
5583 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5584 zfs_ioc_vdev_detach);
5585 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5586 zfs_ioc_vdev_setpath);
5587 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5588 zfs_ioc_vdev_setfru);
5589 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5590 zfs_ioc_pool_set_props);
5591 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5592 zfs_ioc_vdev_split);
5593 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5594 zfs_ioc_pool_reguid);
5596 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5597 zfs_ioc_pool_configs, zfs_secpolicy_none);
5598 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5599 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5600 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5601 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5602 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5603 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5604 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5605 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5608 * pool destroy, and export don't log the history as part of
5609 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5610 * does the logging of those commands.
5612 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5613 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5614 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5615 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5617 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5618 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5619 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5620 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5622 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5623 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5624 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5625 zfs_ioc_dsobj_to_dsname,
5626 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5627 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5628 zfs_ioc_pool_get_history,
5629 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5631 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5632 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5634 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5635 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5636 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5637 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5639 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5640 zfs_ioc_space_written);
5641 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5642 zfs_ioc_objset_recvd_props);
5643 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5644 zfs_ioc_next_obj);
5645 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5646 zfs_ioc_get_fsacl);
5647 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5648 zfs_ioc_objset_stats);
5649 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5650 zfs_ioc_objset_zplprops);
5651 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5652 zfs_ioc_dataset_list_next);
5653 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5654 zfs_ioc_snapshot_list_next);
5655 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5656 zfs_ioc_send_progress);
5658 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5659 zfs_ioc_diff, zfs_secpolicy_diff);
5660 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5661 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5662 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5663 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5664 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5665 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5666 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5667 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5668 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5669 zfs_ioc_send, zfs_secpolicy_send);
5671 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5672 zfs_secpolicy_none);
5673 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5674 zfs_secpolicy_destroy);
5675 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5676 zfs_secpolicy_rename);
5677 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5678 zfs_secpolicy_recv);
5679 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5680 zfs_secpolicy_promote);
5681 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5682 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5683 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5684 zfs_secpolicy_set_fsacl);
5686 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5687 zfs_secpolicy_share, POOL_CHECK_NONE);
5688 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5689 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5690 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5691 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5692 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5693 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5694 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5695 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5699 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5700 zfs_ioc_poolcheck_t check)
5702 spa_t *spa;
5703 int error;
5705 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5707 if (check & POOL_CHECK_NONE)
5708 return (0);
5710 error = spa_open(name, &spa, FTAG);
5711 if (error == 0) {
5712 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5713 error = SET_ERROR(EAGAIN);
5714 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5715 error = SET_ERROR(EROFS);
5716 spa_close(spa, FTAG);
5718 return (error);
5722 * Find a free minor number.
5724 minor_t
5725 zfsdev_minor_alloc(void)
5727 static minor_t last_minor;
5728 minor_t m;
5730 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5732 for (m = last_minor + 1; m != last_minor; m++) {
5733 if (m > ZFSDEV_MAX_MINOR)
5734 m = 1;
5735 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5736 last_minor = m;
5737 return (m);
5741 return (0);
5744 static int
5745 zfs_ctldev_init(dev_t *devp)
5747 minor_t minor;
5748 zfs_soft_state_t *zs;
5750 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5751 ASSERT(getminor(*devp) == 0);
5753 minor = zfsdev_minor_alloc();
5754 if (minor == 0)
5755 return (SET_ERROR(ENXIO));
5757 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5758 return (SET_ERROR(EAGAIN));
5760 *devp = makedevice(getemajor(*devp), minor);
5762 zs = ddi_get_soft_state(zfsdev_state, minor);
5763 zs->zss_type = ZSST_CTLDEV;
5764 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5766 return (0);
5769 static void
5770 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5772 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5774 zfs_onexit_destroy(zo);
5775 ddi_soft_state_free(zfsdev_state, minor);
5778 void *
5779 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5781 zfs_soft_state_t *zp;
5783 zp = ddi_get_soft_state(zfsdev_state, minor);
5784 if (zp == NULL || zp->zss_type != which)
5785 return (NULL);
5787 return (zp->zss_data);
5790 static int
5791 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5793 int error = 0;
5795 if (getminor(*devp) != 0)
5796 return (zvol_open(devp, flag, otyp, cr));
5798 /* This is the control device. Allocate a new minor if requested. */
5799 if (flag & FEXCL) {
5800 mutex_enter(&zfsdev_state_lock);
5801 error = zfs_ctldev_init(devp);
5802 mutex_exit(&zfsdev_state_lock);
5805 return (error);
5808 static int
5809 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5811 zfs_onexit_t *zo;
5812 minor_t minor = getminor(dev);
5814 if (minor == 0)
5815 return (0);
5817 mutex_enter(&zfsdev_state_lock);
5818 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5819 if (zo == NULL) {
5820 mutex_exit(&zfsdev_state_lock);
5821 return (zvol_close(dev, flag, otyp, cr));
5823 zfs_ctldev_destroy(zo, minor);
5824 mutex_exit(&zfsdev_state_lock);
5826 return (0);
5829 static int
5830 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5832 zfs_cmd_t *zc;
5833 uint_t vecnum;
5834 int error, rc, len;
5835 minor_t minor = getminor(dev);
5836 const zfs_ioc_vec_t *vec;
5837 char *saved_poolname = NULL;
5838 nvlist_t *innvl = NULL;
5840 if (minor != 0 &&
5841 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5842 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5844 vecnum = cmd - ZFS_IOC_FIRST;
5845 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5847 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5848 return (SET_ERROR(EINVAL));
5849 vec = &zfs_ioc_vec[vecnum];
5851 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5853 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5854 if (error != 0) {
5855 error = SET_ERROR(EFAULT);
5856 goto out;
5859 zc->zc_iflags = flag & FKIOCTL;
5860 if (zc->zc_nvlist_src_size != 0) {
5861 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5862 zc->zc_iflags, &innvl);
5863 if (error != 0)
5864 goto out;
5868 * Ensure that all pool/dataset names are valid before we pass down to
5869 * the lower layers.
5871 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5872 switch (vec->zvec_namecheck) {
5873 case POOL_NAME:
5874 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5875 error = SET_ERROR(EINVAL);
5876 else
5877 error = pool_status_check(zc->zc_name,
5878 vec->zvec_namecheck, vec->zvec_pool_check);
5879 break;
5881 case DATASET_NAME:
5882 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5883 error = SET_ERROR(EINVAL);
5884 else
5885 error = pool_status_check(zc->zc_name,
5886 vec->zvec_namecheck, vec->zvec_pool_check);
5887 break;
5889 case NO_NAME:
5890 break;
5894 if (error == 0 && !(flag & FKIOCTL))
5895 error = vec->zvec_secpolicy(zc, innvl, cr);
5897 if (error != 0)
5898 goto out;
5900 /* legacy ioctls can modify zc_name */
5901 len = strcspn(zc->zc_name, "/@#") + 1;
5902 saved_poolname = kmem_alloc(len, KM_SLEEP);
5903 (void) strlcpy(saved_poolname, zc->zc_name, len);
5905 if (vec->zvec_func != NULL) {
5906 nvlist_t *outnvl;
5907 int puterror = 0;
5908 spa_t *spa;
5909 nvlist_t *lognv = NULL;
5911 ASSERT(vec->zvec_legacy_func == NULL);
5914 * Add the innvl to the lognv before calling the func,
5915 * in case the func changes the innvl.
5917 if (vec->zvec_allow_log) {
5918 lognv = fnvlist_alloc();
5919 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
5920 vec->zvec_name);
5921 if (!nvlist_empty(innvl)) {
5922 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
5923 innvl);
5927 outnvl = fnvlist_alloc();
5928 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
5930 if (error == 0 && vec->zvec_allow_log &&
5931 spa_open(zc->zc_name, &spa, FTAG) == 0) {
5932 if (!nvlist_empty(outnvl)) {
5933 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
5934 outnvl);
5936 (void) spa_history_log_nvl(spa, lognv);
5937 spa_close(spa, FTAG);
5939 fnvlist_free(lognv);
5941 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
5942 int smusherror = 0;
5943 if (vec->zvec_smush_outnvlist) {
5944 smusherror = nvlist_smush(outnvl,
5945 zc->zc_nvlist_dst_size);
5947 if (smusherror == 0)
5948 puterror = put_nvlist(zc, outnvl);
5951 if (puterror != 0)
5952 error = puterror;
5954 nvlist_free(outnvl);
5955 } else {
5956 error = vec->zvec_legacy_func(zc);
5959 out:
5960 nvlist_free(innvl);
5961 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5962 if (error == 0 && rc != 0)
5963 error = SET_ERROR(EFAULT);
5964 if (error == 0 && vec->zvec_allow_log) {
5965 char *s = tsd_get(zfs_allow_log_key);
5966 if (s != NULL)
5967 strfree(s);
5968 (void) tsd_set(zfs_allow_log_key, saved_poolname);
5969 } else {
5970 if (saved_poolname != NULL)
5971 strfree(saved_poolname);
5974 kmem_free(zc, sizeof (zfs_cmd_t));
5975 return (error);
5978 static int
5979 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5981 if (cmd != DDI_ATTACH)
5982 return (DDI_FAILURE);
5984 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
5985 DDI_PSEUDO, 0) == DDI_FAILURE)
5986 return (DDI_FAILURE);
5988 zfs_dip = dip;
5990 ddi_report_dev(dip);
5992 return (DDI_SUCCESS);
5995 static int
5996 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5998 if (spa_busy() || zfs_busy() || zvol_busy())
5999 return (DDI_FAILURE);
6001 if (cmd != DDI_DETACH)
6002 return (DDI_FAILURE);
6004 zfs_dip = NULL;
6006 ddi_prop_remove_all(dip);
6007 ddi_remove_minor_node(dip, NULL);
6009 return (DDI_SUCCESS);
6012 /*ARGSUSED*/
6013 static int
6014 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6016 switch (infocmd) {
6017 case DDI_INFO_DEVT2DEVINFO:
6018 *result = zfs_dip;
6019 return (DDI_SUCCESS);
6021 case DDI_INFO_DEVT2INSTANCE:
6022 *result = (void *)0;
6023 return (DDI_SUCCESS);
6026 return (DDI_FAILURE);
6030 * OK, so this is a little weird.
6032 * /dev/zfs is the control node, i.e. minor 0.
6033 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6035 * /dev/zfs has basically nothing to do except serve up ioctls,
6036 * so most of the standard driver entry points are in zvol.c.
6038 static struct cb_ops zfs_cb_ops = {
6039 zfsdev_open, /* open */
6040 zfsdev_close, /* close */
6041 zvol_strategy, /* strategy */
6042 nodev, /* print */
6043 zvol_dump, /* dump */
6044 zvol_read, /* read */
6045 zvol_write, /* write */
6046 zfsdev_ioctl, /* ioctl */
6047 nodev, /* devmap */
6048 nodev, /* mmap */
6049 nodev, /* segmap */
6050 nochpoll, /* poll */
6051 ddi_prop_op, /* prop_op */
6052 NULL, /* streamtab */
6053 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6054 CB_REV, /* version */
6055 nodev, /* async read */
6056 nodev, /* async write */
6059 static struct dev_ops zfs_dev_ops = {
6060 DEVO_REV, /* version */
6061 0, /* refcnt */
6062 zfs_info, /* info */
6063 nulldev, /* identify */
6064 nulldev, /* probe */
6065 zfs_attach, /* attach */
6066 zfs_detach, /* detach */
6067 nodev, /* reset */
6068 &zfs_cb_ops, /* driver operations */
6069 NULL, /* no bus operations */
6070 NULL, /* power */
6071 ddi_quiesce_not_needed, /* quiesce */
6074 static struct modldrv zfs_modldrv = {
6075 &mod_driverops,
6076 "ZFS storage pool",
6077 &zfs_dev_ops
6080 static struct modlinkage modlinkage = {
6081 MODREV_1,
6082 (void *)&zfs_modlfs,
6083 (void *)&zfs_modldrv,
6084 NULL
6087 static void
6088 zfs_allow_log_destroy(void *arg)
6090 char *poolname = arg;
6091 strfree(poolname);
6095 _init(void)
6097 int error;
6099 spa_init(FREAD | FWRITE);
6100 zfs_init();
6101 zvol_init();
6102 zfs_ioctl_init();
6104 if ((error = mod_install(&modlinkage)) != 0) {
6105 zvol_fini();
6106 zfs_fini();
6107 spa_fini();
6108 return (error);
6111 tsd_create(&zfs_fsyncer_key, NULL);
6112 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6113 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6115 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6116 ASSERT(error == 0);
6117 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6119 return (0);
6123 _fini(void)
6125 int error;
6127 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6128 return (SET_ERROR(EBUSY));
6130 if ((error = mod_remove(&modlinkage)) != 0)
6131 return (error);
6133 zvol_fini();
6134 zfs_fini();
6135 spa_fini();
6136 if (zfs_nfsshare_inited)
6137 (void) ddi_modclose(nfs_mod);
6138 if (zfs_smbshare_inited)
6139 (void) ddi_modclose(smbsrv_mod);
6140 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6141 (void) ddi_modclose(sharefs_mod);
6143 tsd_destroy(&zfs_fsyncer_key);
6144 ldi_ident_release(zfs_li);
6145 zfs_li = NULL;
6146 mutex_destroy(&zfs_share_lock);
6148 return (error);
6152 _info(struct modinfo *modinfop)
6154 return (mod_info(&modlinkage, modinfop));