Merge commit '720b16875295d57e0e6a4e0ec32db4d47412f896'
[unleashed.git] / kernel / fs / zfs / zfs_ioctl.c
blob7c43c851067a5dd8d901bedf7b0f4bd0fbca1e87
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 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
25 * Portions Copyright 2011 Martin Matuska
26 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
29 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
30 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
33 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 * Copyright 2017 RackTop Systems.
35 * Copyright (c) 2017 Datto Inc.
39 * ZFS ioctls.
41 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
42 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
44 * There are two ways that we handle ioctls: the legacy way where almost
45 * all of the logic is in the ioctl callback, and the new way where most
46 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
48 * Non-legacy ioctls should be registered by calling
49 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
50 * from userland by lzc_ioctl().
52 * The registration arguments are as follows:
54 * const char *name
55 * The name of the ioctl. This is used for history logging. If the
56 * ioctl returns successfully (the callback returns 0), and allow_log
57 * is true, then a history log entry will be recorded with the input &
58 * output nvlists. The log entry can be printed with "zpool history -i".
60 * zfs_ioc_t ioc
61 * The ioctl request number, which userland will pass to ioctl(2).
62 * The ioctl numbers can change from release to release, because
63 * the caller (libzfs) must be matched to the kernel.
65 * zfs_secpolicy_func_t *secpolicy
66 * This function will be called before the zfs_ioc_func_t, to
67 * determine if this operation is permitted. It should return EPERM
68 * on failure, and 0 on success. Checks include determining if the
69 * dataset is visible in this zone, and if the user has either all
70 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
71 * to do this operation on this dataset with "zfs allow".
73 * zfs_ioc_namecheck_t namecheck
74 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
75 * name, a dataset name, or nothing. If the name is not well-formed,
76 * the ioctl will fail and the callback will not be called.
77 * Therefore, the callback can assume that the name is well-formed
78 * (e.g. is null-terminated, doesn't have more than one '@' character,
79 * doesn't have invalid characters).
81 * zfs_ioc_poolcheck_t pool_check
82 * This specifies requirements on the pool state. If the pool does
83 * not meet them (is suspended or is readonly), the ioctl will fail
84 * and the callback will not be called. If any checks are specified
85 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
86 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
87 * POOL_CHECK_READONLY).
89 * boolean_t smush_outnvlist
90 * If smush_outnvlist is true, then the output is presumed to be a
91 * list of errors, and it will be "smushed" down to fit into the
92 * caller's buffer, by removing some entries and replacing them with a
93 * single "N_MORE_ERRORS" entry indicating how many were removed. See
94 * nvlist_smush() for details. If smush_outnvlist is false, and the
95 * outnvlist does not fit into the userland-provided buffer, then the
96 * ioctl will fail with ENOMEM.
98 * zfs_ioc_func_t *func
99 * The callback function that will perform the operation.
101 * The callback should return 0 on success, or an error number on
102 * failure. If the function fails, the userland ioctl will return -1,
103 * and errno will be set to the callback's return value. The callback
104 * will be called with the following arguments:
106 * const char *name
107 * The name of the pool or dataset to operate on, from
108 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
109 * expected type (pool, dataset, or none).
111 * nvlist_t *innvl
112 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
113 * NULL if no input nvlist was provided. Changes to this nvlist are
114 * ignored. If the input nvlist could not be deserialized, the
115 * ioctl will fail and the callback will not be called.
117 * nvlist_t *outnvl
118 * The output nvlist, initially empty. The callback can fill it in,
119 * and it will be returned to userland by serializing it into
120 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
121 * fails (e.g. because the caller didn't supply a large enough
122 * buffer), then the overall ioctl will fail. See the
123 * 'smush_nvlist' argument above for additional behaviors.
125 * There are two typical uses of the output nvlist:
126 * - To return state, e.g. property values. In this case,
127 * smush_outnvlist should be false. If the buffer was not large
128 * enough, the caller will reallocate a larger buffer and try
129 * the ioctl again.
131 * - To return multiple errors from an ioctl which makes on-disk
132 * changes. In this case, smush_outnvlist should be true.
133 * Ioctls which make on-disk modifications should generally not
134 * use the outnvl if they succeed, because the caller can not
135 * distinguish between the operation failing, and
136 * deserialization failing.
139 #include <sys/types.h>
140 #include <sys/param.h>
141 #include <sys/errno.h>
142 #include <sys/uio.h>
143 #include <sys/buf.h>
144 #include <sys/modctl.h>
145 #include <sys/open.h>
146 #include <sys/file.h>
147 #include <sys/kmem.h>
148 #include <sys/conf.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
154 #include <sys/zap.h>
155 #include <sys/spa.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
158 #include <sys/priv_impl.h>
159 #include <sys/dmu.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
167 #include <sys/ddi.h>
168 #include <sys/sunddi.h>
169 #include <sys/sunldi.h>
170 #include <sys/policy.h>
171 #include <sys/zone.h>
172 #include <sys/nvpair.h>
173 #include <sys/pathname.h>
174 #include <sys/mount.h>
175 #include <sys/sdt.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sharefs/share.h>
183 #include <sys/dmu_objset.h>
184 #include <sys/dmu_send.h>
185 #include <sys/dsl_destroy.h>
186 #include <sys/dsl_bookmark.h>
187 #include <sys/dsl_userhold.h>
188 #include <sys/zfeature.h>
189 #include <sys/zcp.h>
190 #include <sys/zio_checksum.h>
192 #include "zfs_namecheck.h"
193 #include "zfs_prop.h"
194 #include "zfs_deleg.h"
195 #include "zfs_comutil.h"
197 #include "lua.h"
198 #include "lauxlib.h"
200 extern struct modlfs zfs_modlfs;
202 extern void zfs_init(void);
203 extern void zfs_fini(void);
205 ldi_ident_t zfs_li = NULL;
206 dev_info_t *zfs_dip;
208 uint_t zfs_fsyncer_key;
209 extern uint_t rrw_tsd_key;
210 static uint_t zfs_allow_log_key;
212 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
213 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
214 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
216 typedef enum {
217 NO_NAME,
218 POOL_NAME,
219 DATASET_NAME
220 } zfs_ioc_namecheck_t;
222 typedef enum {
223 POOL_CHECK_NONE = 1 << 0,
224 POOL_CHECK_SUSPENDED = 1 << 1,
225 POOL_CHECK_READONLY = 1 << 2,
226 } zfs_ioc_poolcheck_t;
228 typedef struct zfs_ioc_vec {
229 zfs_ioc_legacy_func_t *zvec_legacy_func;
230 zfs_ioc_func_t *zvec_func;
231 zfs_secpolicy_func_t *zvec_secpolicy;
232 zfs_ioc_namecheck_t zvec_namecheck;
233 boolean_t zvec_allow_log;
234 zfs_ioc_poolcheck_t zvec_pool_check;
235 boolean_t zvec_smush_outnvlist;
236 const char *zvec_name;
237 } zfs_ioc_vec_t;
239 /* This array is indexed by zfs_userquota_prop_t */
240 static const char *userquota_perms[] = {
241 ZFS_DELEG_PERM_USERUSED,
242 ZFS_DELEG_PERM_USERQUOTA,
243 ZFS_DELEG_PERM_GROUPUSED,
244 ZFS_DELEG_PERM_GROUPQUOTA,
247 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
248 static int zfs_check_settable(const char *name, nvpair_t *property,
249 cred_t *cr);
250 static int zfs_check_clearable(char *dataset, nvlist_t *props,
251 nvlist_t **errors);
252 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
253 boolean_t *);
254 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
255 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
257 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
259 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
260 void
261 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
263 const char *newfile;
264 char buf[512];
265 va_list adx;
268 * Get rid of annoying "../common/" prefix to filename.
270 newfile = strrchr(file, '/');
271 if (newfile != NULL) {
272 newfile = newfile + 1; /* Get rid of leading / */
273 } else {
274 newfile = file;
277 va_start(adx, fmt);
278 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
279 va_end(adx);
282 * To get this data, use the zfs-dprintf probe as so:
283 * dtrace -q -n 'zfs-dprintf \
284 * /stringof(arg0) == "dbuf.c"/ \
285 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
286 * arg0 = file name
287 * arg1 = function name
288 * arg2 = line number
289 * arg3 = message
291 DTRACE_PROBE4(zfs__dprintf,
292 char *, newfile, char *, func, int, line, char *, buf);
295 static void
296 history_str_free(char *buf)
298 kmem_free(buf, HIS_MAX_RECORD_LEN);
301 static char *
302 history_str_get(zfs_cmd_t *zc)
304 char *buf;
306 if (zc->zc_history == (uintptr_t)NULL)
307 return (NULL);
309 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
310 if (copyinstr((void *)(uintptr_t)zc->zc_history,
311 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
312 history_str_free(buf);
313 return (NULL);
316 buf[HIS_MAX_RECORD_LEN -1] = '\0';
318 return (buf);
322 * Check to see if the named dataset is currently defined as bootable
324 static boolean_t
325 zfs_is_bootfs(const char *name)
327 objset_t *os;
329 if (dmu_objset_hold(name, FTAG, &os) == 0) {
330 boolean_t ret;
331 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
332 dmu_objset_rele(os, FTAG);
333 return (ret);
335 return (B_FALSE);
339 * Return non-zero if the spa version is less than requested version.
341 static int
342 zfs_earlier_version(const char *name, int version)
344 spa_t *spa;
346 if (spa_open(name, &spa, FTAG) == 0) {
347 if (spa_version(spa) < version) {
348 spa_close(spa, FTAG);
349 return (1);
351 spa_close(spa, FTAG);
353 return (0);
357 * Return TRUE if the ZPL version is less than requested version.
359 static boolean_t
360 zpl_earlier_version(const char *name, int version)
362 objset_t *os;
363 boolean_t rc = B_TRUE;
365 if (dmu_objset_hold(name, FTAG, &os) == 0) {
366 uint64_t zplversion;
368 if (dmu_objset_type(os) != DMU_OST_ZFS) {
369 dmu_objset_rele(os, FTAG);
370 return (B_TRUE);
372 /* XXX reading from non-owned objset */
373 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
374 rc = zplversion < version;
375 dmu_objset_rele(os, FTAG);
377 return (rc);
380 static void
381 zfs_log_history(zfs_cmd_t *zc)
383 spa_t *spa;
384 char *buf;
386 if ((buf = history_str_get(zc)) == NULL)
387 return;
389 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
390 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
391 (void) spa_history_log(spa, buf);
392 spa_close(spa, FTAG);
394 history_str_free(buf);
398 * Policy for top-level read operations (list pools). Requires no privileges,
399 * and can be used in the local zone, as there is no associated dataset.
401 /* ARGSUSED */
402 static int
403 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
405 return (0);
409 * Policy for dataset read operations (list children, get statistics). Requires
410 * no privileges, but must be visible in the local zone.
412 /* ARGSUSED */
413 static int
414 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
416 if (INGLOBALZONE(curproc) ||
417 zone_dataset_visible(zc->zc_name, NULL))
418 return (0);
420 return (SET_ERROR(ENOENT));
423 static int
424 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
426 int writable = 1;
429 * The dataset must be visible by this zone -- check this first
430 * so they don't see EPERM on something they shouldn't know about.
432 if (!INGLOBALZONE(curproc) &&
433 !zone_dataset_visible(dataset, &writable))
434 return (SET_ERROR(ENOENT));
436 if (INGLOBALZONE(curproc)) {
438 * If the fs is zoned, only root can access it from the
439 * global zone.
441 if (secpolicy_zfs(cr) && zoned)
442 return (SET_ERROR(EPERM));
443 } else {
445 * If we are in a local zone, the 'zoned' property must be set.
447 if (!zoned)
448 return (SET_ERROR(EPERM));
450 /* must be writable by this zone */
451 if (!writable)
452 return (SET_ERROR(EPERM));
454 return (0);
457 static int
458 zfs_dozonecheck(const char *dataset, cred_t *cr)
460 uint64_t zoned;
462 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
463 return (SET_ERROR(ENOENT));
465 return (zfs_dozonecheck_impl(dataset, zoned, cr));
468 static int
469 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
471 uint64_t zoned;
473 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
474 return (SET_ERROR(ENOENT));
476 return (zfs_dozonecheck_impl(dataset, zoned, cr));
479 static int
480 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
481 const char *perm, cred_t *cr)
483 int error;
485 error = zfs_dozonecheck_ds(name, ds, cr);
486 if (error == 0) {
487 error = secpolicy_zfs(cr);
488 if (error != 0)
489 error = dsl_deleg_access_impl(ds, perm, cr);
491 return (error);
494 static int
495 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
497 int error;
498 dsl_dataset_t *ds;
499 dsl_pool_t *dp;
502 * First do a quick check for root in the global zone, which
503 * is allowed to do all write_perms. This ensures that zfs_ioc_*
504 * will get to handle nonexistent datasets.
506 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
507 return (0);
509 error = dsl_pool_hold(name, FTAG, &dp);
510 if (error != 0)
511 return (error);
513 error = dsl_dataset_hold(dp, name, FTAG, &ds);
514 if (error != 0) {
515 dsl_pool_rele(dp, FTAG);
516 return (error);
519 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
521 dsl_dataset_rele(ds, FTAG);
522 dsl_pool_rele(dp, FTAG);
523 return (error);
526 static int
527 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
528 cred_t *cr)
530 char *strval;
533 * Check permissions for special properties.
535 switch (prop) {
536 case ZFS_PROP_ZONED:
538 * Disallow setting of 'zoned' from within a local zone.
540 if (!INGLOBALZONE(curproc))
541 return (SET_ERROR(EPERM));
542 break;
544 case ZFS_PROP_QUOTA:
545 case ZFS_PROP_FILESYSTEM_LIMIT:
546 case ZFS_PROP_SNAPSHOT_LIMIT:
547 if (!INGLOBALZONE(curproc)) {
548 uint64_t zoned;
549 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
551 * Unprivileged users are allowed to modify the
552 * limit on things *under* (ie. contained by)
553 * the thing they own.
555 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
556 setpoint))
557 return (SET_ERROR(EPERM));
558 if (!zoned || strlen(dsname) <= strlen(setpoint))
559 return (SET_ERROR(EPERM));
561 break;
564 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
567 /* ARGSUSED */
568 static int
569 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
571 int error;
573 error = zfs_dozonecheck(zc->zc_name, cr);
574 if (error != 0)
575 return (error);
578 * permission to set permissions will be evaluated later in
579 * dsl_deleg_can_allow()
581 return (0);
584 /* ARGSUSED */
585 static int
586 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
588 return (zfs_secpolicy_write_perms(zc->zc_name,
589 ZFS_DELEG_PERM_ROLLBACK, cr));
592 /* ARGSUSED */
593 static int
594 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
596 dsl_pool_t *dp;
597 dsl_dataset_t *ds;
598 char *cp;
599 int error;
602 * Generate the current snapshot name from the given objsetid, then
603 * use that name for the secpolicy/zone checks.
605 cp = strchr(zc->zc_name, '@');
606 if (cp == NULL)
607 return (SET_ERROR(EINVAL));
608 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
609 if (error != 0)
610 return (error);
612 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
613 if (error != 0) {
614 dsl_pool_rele(dp, FTAG);
615 return (error);
618 dsl_dataset_name(ds, zc->zc_name);
620 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
621 ZFS_DELEG_PERM_SEND, cr);
622 dsl_dataset_rele(ds, FTAG);
623 dsl_pool_rele(dp, FTAG);
625 return (error);
628 /* ARGSUSED */
629 static int
630 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
632 return (zfs_secpolicy_write_perms(zc->zc_name,
633 ZFS_DELEG_PERM_SEND, cr));
636 /* ARGSUSED */
637 static int
638 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
640 vnode_t *vp;
641 int error;
643 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
644 NO_FOLLOW, NULL, &vp)) != 0)
645 return (error);
647 /* Now make sure mntpnt and dataset are ZFS */
649 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
650 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
651 zc->zc_name) != 0)) {
652 VN_RELE(vp);
653 return (SET_ERROR(EPERM));
656 VN_RELE(vp);
657 return (dsl_deleg_access(zc->zc_name,
658 ZFS_DELEG_PERM_SHARE, cr));
662 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
664 if (!INGLOBALZONE(curproc))
665 return (SET_ERROR(EPERM));
667 if (secpolicy_nfs(cr) == 0) {
668 return (0);
669 } else {
670 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
675 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
677 if (!INGLOBALZONE(curproc))
678 return (SET_ERROR(EPERM));
680 if (secpolicy_smb(cr) == 0) {
681 return (0);
682 } else {
683 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
687 static int
688 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
690 char *cp;
693 * Remove the @bla or /bla from the end of the name to get the parent.
695 (void) strncpy(parent, datasetname, parentsize);
696 cp = strrchr(parent, '@');
697 if (cp != NULL) {
698 cp[0] = '\0';
699 } else {
700 cp = strrchr(parent, '/');
701 if (cp == NULL)
702 return (SET_ERROR(ENOENT));
703 cp[0] = '\0';
706 return (0);
710 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
712 int error;
714 if ((error = zfs_secpolicy_write_perms(name,
715 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
716 return (error);
718 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
721 /* ARGSUSED */
722 static int
723 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
725 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
729 * Destroying snapshots with delegated permissions requires
730 * descendant mount and destroy permissions.
732 /* ARGSUSED */
733 static int
734 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
736 nvlist_t *snaps;
737 nvpair_t *pair, *nextpair;
738 int error = 0;
740 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
741 return (SET_ERROR(EINVAL));
742 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
743 pair = nextpair) {
744 nextpair = nvlist_next_nvpair(snaps, pair);
745 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
746 if (error == ENOENT) {
748 * Ignore any snapshots that don't exist (we consider
749 * them "already destroyed"). Remove the name from the
750 * nvl here in case the snapshot is created between
751 * now and when we try to destroy it (in which case
752 * we don't want to destroy it since we haven't
753 * checked for permission).
755 fnvlist_remove_nvpair(snaps, pair);
756 error = 0;
758 if (error != 0)
759 break;
762 return (error);
766 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
768 char parentname[ZFS_MAX_DATASET_NAME_LEN];
769 int error;
771 if ((error = zfs_secpolicy_write_perms(from,
772 ZFS_DELEG_PERM_RENAME, cr)) != 0)
773 return (error);
775 if ((error = zfs_secpolicy_write_perms(from,
776 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
777 return (error);
779 if ((error = zfs_get_parent(to, parentname,
780 sizeof (parentname))) != 0)
781 return (error);
783 if ((error = zfs_secpolicy_write_perms(parentname,
784 ZFS_DELEG_PERM_CREATE, cr)) != 0)
785 return (error);
787 if ((error = zfs_secpolicy_write_perms(parentname,
788 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
789 return (error);
791 return (error);
794 /* ARGSUSED */
795 static int
796 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
798 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
801 /* ARGSUSED */
802 static int
803 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
805 dsl_pool_t *dp;
806 dsl_dataset_t *clone;
807 int error;
809 error = zfs_secpolicy_write_perms(zc->zc_name,
810 ZFS_DELEG_PERM_PROMOTE, cr);
811 if (error != 0)
812 return (error);
814 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
815 if (error != 0)
816 return (error);
818 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
820 if (error == 0) {
821 char parentname[ZFS_MAX_DATASET_NAME_LEN];
822 dsl_dataset_t *origin = NULL;
823 dsl_dir_t *dd;
824 dd = clone->ds_dir;
826 error = dsl_dataset_hold_obj(dd->dd_pool,
827 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
828 if (error != 0) {
829 dsl_dataset_rele(clone, FTAG);
830 dsl_pool_rele(dp, FTAG);
831 return (error);
834 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
835 ZFS_DELEG_PERM_MOUNT, cr);
837 dsl_dataset_name(origin, parentname);
838 if (error == 0) {
839 error = zfs_secpolicy_write_perms_ds(parentname, origin,
840 ZFS_DELEG_PERM_PROMOTE, cr);
842 dsl_dataset_rele(clone, FTAG);
843 dsl_dataset_rele(origin, FTAG);
845 dsl_pool_rele(dp, FTAG);
846 return (error);
849 /* ARGSUSED */
850 static int
851 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
853 int error;
855 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
856 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
857 return (error);
859 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
860 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
861 return (error);
863 return (zfs_secpolicy_write_perms(zc->zc_name,
864 ZFS_DELEG_PERM_CREATE, cr));
868 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
870 return (zfs_secpolicy_write_perms(name,
871 ZFS_DELEG_PERM_SNAPSHOT, cr));
875 * Check for permission to create each snapshot in the nvlist.
877 /* ARGSUSED */
878 static int
879 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
881 nvlist_t *snaps;
882 int error = 0;
883 nvpair_t *pair;
885 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
886 return (SET_ERROR(EINVAL));
887 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
888 pair = nvlist_next_nvpair(snaps, pair)) {
889 char *name = nvpair_name(pair);
890 char *atp = strchr(name, '@');
892 if (atp == NULL) {
893 error = SET_ERROR(EINVAL);
894 break;
896 *atp = '\0';
897 error = zfs_secpolicy_snapshot_perms(name, cr);
898 *atp = '@';
899 if (error != 0)
900 break;
902 return (error);
906 * Check for permission to create each snapshot in the nvlist.
908 /* ARGSUSED */
909 static int
910 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
912 int error = 0;
914 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
915 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
916 char *name = nvpair_name(pair);
917 char *hashp = strchr(name, '#');
919 if (hashp == NULL) {
920 error = SET_ERROR(EINVAL);
921 break;
923 *hashp = '\0';
924 error = zfs_secpolicy_write_perms(name,
925 ZFS_DELEG_PERM_BOOKMARK, cr);
926 *hashp = '#';
927 if (error != 0)
928 break;
930 return (error);
933 /* ARGSUSED */
934 static int
935 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
937 nvpair_t *pair, *nextpair;
938 int error = 0;
940 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
941 pair = nextpair) {
942 char *name = nvpair_name(pair);
943 char *hashp = strchr(name, '#');
944 nextpair = nvlist_next_nvpair(innvl, pair);
946 if (hashp == NULL) {
947 error = SET_ERROR(EINVAL);
948 break;
951 *hashp = '\0';
952 error = zfs_secpolicy_write_perms(name,
953 ZFS_DELEG_PERM_DESTROY, cr);
954 *hashp = '#';
955 if (error == ENOENT) {
957 * Ignore any filesystems that don't exist (we consider
958 * their bookmarks "already destroyed"). Remove
959 * the name from the nvl here in case the filesystem
960 * is created between now and when we try to destroy
961 * the bookmark (in which case we don't want to
962 * destroy it since we haven't checked for permission).
964 fnvlist_remove_nvpair(innvl, pair);
965 error = 0;
967 if (error != 0)
968 break;
971 return (error);
974 /* ARGSUSED */
975 static int
976 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
979 * Even root must have a proper TSD so that we know what pool
980 * to log to.
982 if (tsd_get(zfs_allow_log_key) == NULL)
983 return (SET_ERROR(EPERM));
984 return (0);
987 static int
988 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
990 char parentname[ZFS_MAX_DATASET_NAME_LEN];
991 int error;
992 char *origin;
994 if ((error = zfs_get_parent(zc->zc_name, parentname,
995 sizeof (parentname))) != 0)
996 return (error);
998 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
999 (error = zfs_secpolicy_write_perms(origin,
1000 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1001 return (error);
1003 if ((error = zfs_secpolicy_write_perms(parentname,
1004 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1005 return (error);
1007 return (zfs_secpolicy_write_perms(parentname,
1008 ZFS_DELEG_PERM_MOUNT, cr));
1012 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1013 * SYS_CONFIG privilege, which is not available in a local zone.
1015 /* ARGSUSED */
1016 static int
1017 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1019 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1020 return (SET_ERROR(EPERM));
1022 return (0);
1026 * Policy for object to name lookups.
1028 /* ARGSUSED */
1029 static int
1030 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1032 int error;
1034 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1035 return (0);
1037 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1038 return (error);
1042 * Policy for fault injection. Requires all privileges.
1044 /* ARGSUSED */
1045 static int
1046 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1048 return (secpolicy_zinject(cr));
1051 /* ARGSUSED */
1052 static int
1053 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1055 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1057 if (prop == ZPROP_INVAL) {
1058 if (!zfs_prop_user(zc->zc_value))
1059 return (SET_ERROR(EINVAL));
1060 return (zfs_secpolicy_write_perms(zc->zc_name,
1061 ZFS_DELEG_PERM_USERPROP, cr));
1062 } else {
1063 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1064 NULL, cr));
1068 static int
1069 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1071 int err = zfs_secpolicy_read(zc, innvl, cr);
1072 if (err)
1073 return (err);
1075 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1076 return (SET_ERROR(EINVAL));
1078 if (zc->zc_value[0] == 0) {
1080 * They are asking about a posix uid/gid. If it's
1081 * themself, allow it.
1083 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1084 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1085 if (zc->zc_guid == crgetuid(cr))
1086 return (0);
1087 } else {
1088 if (groupmember(zc->zc_guid, cr))
1089 return (0);
1093 return (zfs_secpolicy_write_perms(zc->zc_name,
1094 userquota_perms[zc->zc_objset_type], cr));
1097 static int
1098 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1100 int err = zfs_secpolicy_read(zc, innvl, cr);
1101 if (err)
1102 return (err);
1104 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1105 return (SET_ERROR(EINVAL));
1107 return (zfs_secpolicy_write_perms(zc->zc_name,
1108 userquota_perms[zc->zc_objset_type], cr));
1111 /* ARGSUSED */
1112 static int
1113 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1115 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1116 NULL, cr));
1119 /* ARGSUSED */
1120 static int
1121 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1123 nvpair_t *pair;
1124 nvlist_t *holds;
1125 int error;
1127 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1128 if (error != 0)
1129 return (SET_ERROR(EINVAL));
1131 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1132 pair = nvlist_next_nvpair(holds, pair)) {
1133 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1134 error = dmu_fsname(nvpair_name(pair), fsname);
1135 if (error != 0)
1136 return (error);
1137 error = zfs_secpolicy_write_perms(fsname,
1138 ZFS_DELEG_PERM_HOLD, cr);
1139 if (error != 0)
1140 return (error);
1142 return (0);
1145 /* ARGSUSED */
1146 static int
1147 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1149 nvpair_t *pair;
1150 int error;
1152 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1153 pair = nvlist_next_nvpair(innvl, pair)) {
1154 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1155 error = dmu_fsname(nvpair_name(pair), fsname);
1156 if (error != 0)
1157 return (error);
1158 error = zfs_secpolicy_write_perms(fsname,
1159 ZFS_DELEG_PERM_RELEASE, cr);
1160 if (error != 0)
1161 return (error);
1163 return (0);
1167 * Policy for allowing temporary snapshots to be taken or released
1169 static int
1170 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1173 * A temporary snapshot is the same as a snapshot,
1174 * hold, destroy and release all rolled into one.
1175 * Delegated diff alone is sufficient that we allow this.
1177 int error;
1179 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1180 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1181 return (0);
1183 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1184 if (error == 0)
1185 error = zfs_secpolicy_hold(zc, innvl, cr);
1186 if (error == 0)
1187 error = zfs_secpolicy_release(zc, innvl, cr);
1188 if (error == 0)
1189 error = zfs_secpolicy_destroy(zc, innvl, cr);
1190 return (error);
1194 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1196 static int
1197 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1199 char *packed;
1200 int error;
1201 nvlist_t *list = NULL;
1204 * Read in and unpack the user-supplied nvlist.
1206 if (size == 0)
1207 return (SET_ERROR(EINVAL));
1209 packed = kmem_alloc(size, KM_SLEEP);
1211 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1212 iflag)) != 0) {
1213 kmem_free(packed, size);
1214 return (SET_ERROR(EFAULT));
1217 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1218 kmem_free(packed, size);
1219 return (error);
1222 kmem_free(packed, size);
1224 *nvp = list;
1225 return (0);
1229 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1230 * Entries will be removed from the end of the nvlist, and one int32 entry
1231 * named "N_MORE_ERRORS" will be added indicating how many entries were
1232 * removed.
1234 static int
1235 nvlist_smush(nvlist_t *errors, size_t max)
1237 size_t size;
1239 size = fnvlist_size(errors);
1241 if (size > max) {
1242 nvpair_t *more_errors;
1243 int n = 0;
1245 if (max < 1024)
1246 return (SET_ERROR(ENOMEM));
1248 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1249 more_errors = nvlist_prev_nvpair(errors, NULL);
1251 do {
1252 nvpair_t *pair = nvlist_prev_nvpair(errors,
1253 more_errors);
1254 fnvlist_remove_nvpair(errors, pair);
1255 n++;
1256 size = fnvlist_size(errors);
1257 } while (size > max);
1259 fnvlist_remove_nvpair(errors, more_errors);
1260 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1261 ASSERT3U(fnvlist_size(errors), <=, max);
1264 return (0);
1267 static int
1268 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1270 char *packed = NULL;
1271 int error = 0;
1272 size_t size;
1274 size = fnvlist_size(nvl);
1276 if (size > zc->zc_nvlist_dst_size) {
1277 error = SET_ERROR(ENOMEM);
1278 } else {
1279 packed = fnvlist_pack(nvl, &size);
1280 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1281 size, zc->zc_iflags) != 0)
1282 error = SET_ERROR(EFAULT);
1283 fnvlist_pack_free(packed, size);
1286 zc->zc_nvlist_dst_size = size;
1287 zc->zc_nvlist_dst_filled = B_TRUE;
1288 return (error);
1292 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1294 int error = 0;
1295 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1296 return (SET_ERROR(EINVAL));
1299 mutex_enter(&os->os_user_ptr_lock);
1300 *zfvp = dmu_objset_get_user(os);
1301 if (*zfvp) {
1302 VFS_HOLD((*zfvp)->z_vfs);
1303 } else {
1304 error = SET_ERROR(ESRCH);
1306 mutex_exit(&os->os_user_ptr_lock);
1307 return (error);
1311 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1313 objset_t *os;
1314 int error;
1316 error = dmu_objset_hold(dsname, FTAG, &os);
1317 if (error != 0)
1318 return (error);
1320 error = getzfsvfs_impl(os, zfvp);
1321 dmu_objset_rele(os, FTAG);
1322 return (error);
1326 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1327 * case its z_vfs will be NULL, and it will be opened as the owner.
1328 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1329 * which prevents all vnode ops from running.
1331 static int
1332 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1334 int error = 0;
1336 if (getzfsvfs(name, zfvp) != 0)
1337 error = zfsvfs_create(name, zfvp);
1338 if (error == 0) {
1339 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1340 RW_READER, tag);
1341 if ((*zfvp)->z_unmounted) {
1343 * XXX we could probably try again, since the unmounting
1344 * thread should be just about to disassociate the
1345 * objset from the zfsvfs.
1347 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1348 return (SET_ERROR(EBUSY));
1351 return (error);
1354 static void
1355 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1357 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1359 if (zfsvfs->z_vfs) {
1360 VFS_RELE(zfsvfs->z_vfs);
1361 } else {
1362 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1363 zfsvfs_free(zfsvfs);
1367 static int
1368 zfs_ioc_pool_create(zfs_cmd_t *zc)
1370 int error;
1371 nvlist_t *config, *props = NULL;
1372 nvlist_t *rootprops = NULL;
1373 nvlist_t *zplprops = NULL;
1375 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1376 zc->zc_iflags, &config))
1377 return (error);
1379 if (zc->zc_nvlist_src_size != 0 && (error =
1380 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1381 zc->zc_iflags, &props))) {
1382 nvlist_free(config);
1383 return (error);
1386 if (props) {
1387 nvlist_t *nvl = NULL;
1388 uint64_t version = SPA_VERSION;
1390 (void) nvlist_lookup_uint64(props,
1391 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1392 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1393 error = SET_ERROR(EINVAL);
1394 goto pool_props_bad;
1396 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1397 if (nvl) {
1398 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1399 if (error != 0) {
1400 nvlist_free(config);
1401 nvlist_free(props);
1402 return (error);
1404 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1406 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1407 error = zfs_fill_zplprops_root(version, rootprops,
1408 zplprops, NULL);
1409 if (error != 0)
1410 goto pool_props_bad;
1413 error = spa_create(zc->zc_name, config, props, zplprops);
1416 * Set the remaining root properties
1418 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1419 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1420 (void) spa_destroy(zc->zc_name);
1422 pool_props_bad:
1423 nvlist_free(rootprops);
1424 nvlist_free(zplprops);
1425 nvlist_free(config);
1426 nvlist_free(props);
1428 return (error);
1431 static int
1432 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1434 int error;
1435 zfs_log_history(zc);
1436 error = spa_destroy(zc->zc_name);
1437 if (error == 0)
1438 zvol_remove_minors(zc->zc_name);
1439 return (error);
1442 static int
1443 zfs_ioc_pool_import(zfs_cmd_t *zc)
1445 nvlist_t *config, *props = NULL;
1446 uint64_t guid;
1447 int error;
1449 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1450 zc->zc_iflags, &config)) != 0)
1451 return (error);
1453 if (zc->zc_nvlist_src_size != 0 && (error =
1454 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1455 zc->zc_iflags, &props))) {
1456 nvlist_free(config);
1457 return (error);
1460 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1461 guid != zc->zc_guid)
1462 error = SET_ERROR(EINVAL);
1463 else
1464 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1466 if (zc->zc_nvlist_dst != 0) {
1467 int err;
1469 if ((err = put_nvlist(zc, config)) != 0)
1470 error = err;
1473 nvlist_free(config);
1475 nvlist_free(props);
1477 return (error);
1480 static int
1481 zfs_ioc_pool_export(zfs_cmd_t *zc)
1483 int error;
1484 boolean_t force = (boolean_t)zc->zc_cookie;
1485 boolean_t hardforce = (boolean_t)zc->zc_guid;
1487 zfs_log_history(zc);
1488 error = spa_export(zc->zc_name, NULL, force, hardforce);
1489 if (error == 0)
1490 zvol_remove_minors(zc->zc_name);
1491 return (error);
1494 static int
1495 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1497 nvlist_t *configs;
1498 int error;
1500 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1501 return (SET_ERROR(EEXIST));
1503 error = put_nvlist(zc, configs);
1505 nvlist_free(configs);
1507 return (error);
1511 * inputs:
1512 * zc_name name of the pool
1514 * outputs:
1515 * zc_cookie real errno
1516 * zc_nvlist_dst config nvlist
1517 * zc_nvlist_dst_size size of config nvlist
1519 static int
1520 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1522 nvlist_t *config;
1523 int error;
1524 int ret = 0;
1526 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1527 sizeof (zc->zc_value));
1529 if (config != NULL) {
1530 ret = put_nvlist(zc, config);
1531 nvlist_free(config);
1534 * The config may be present even if 'error' is non-zero.
1535 * In this case we return success, and preserve the real errno
1536 * in 'zc_cookie'.
1538 zc->zc_cookie = error;
1539 } else {
1540 ret = error;
1543 return (ret);
1547 * Try to import the given pool, returning pool stats as appropriate so that
1548 * user land knows which devices are available and overall pool health.
1550 static int
1551 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1553 nvlist_t *tryconfig, *config;
1554 int error;
1556 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1557 zc->zc_iflags, &tryconfig)) != 0)
1558 return (error);
1560 config = spa_tryimport(tryconfig);
1562 nvlist_free(tryconfig);
1564 if (config == NULL)
1565 return (SET_ERROR(EINVAL));
1567 error = put_nvlist(zc, config);
1568 nvlist_free(config);
1570 return (error);
1574 * inputs:
1575 * zc_name name of the pool
1576 * zc_cookie scan func (pool_scan_func_t)
1577 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1579 static int
1580 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1582 spa_t *spa;
1583 int error;
1585 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1586 return (error);
1588 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1589 return (SET_ERROR(EINVAL));
1591 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1592 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1593 else if (zc->zc_cookie == POOL_SCAN_NONE)
1594 error = spa_scan_stop(spa);
1595 else
1596 error = spa_scan(spa, zc->zc_cookie);
1598 spa_close(spa, FTAG);
1600 return (error);
1603 static int
1604 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1606 spa_t *spa;
1607 int error;
1609 error = spa_open(zc->zc_name, &spa, FTAG);
1610 if (error == 0) {
1611 spa_freeze(spa);
1612 spa_close(spa, FTAG);
1614 return (error);
1617 static int
1618 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1620 spa_t *spa;
1621 int error;
1623 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1624 return (error);
1626 if (zc->zc_cookie < spa_version(spa) ||
1627 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1628 spa_close(spa, FTAG);
1629 return (SET_ERROR(EINVAL));
1632 spa_upgrade(spa, zc->zc_cookie);
1633 spa_close(spa, FTAG);
1635 return (error);
1638 static int
1639 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1641 spa_t *spa;
1642 char *hist_buf;
1643 uint64_t size;
1644 int error;
1646 if ((size = zc->zc_history_len) == 0)
1647 return (SET_ERROR(EINVAL));
1649 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1650 return (error);
1652 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1653 spa_close(spa, FTAG);
1654 return (SET_ERROR(ENOTSUP));
1657 hist_buf = kmem_alloc(size, KM_SLEEP);
1658 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1659 &zc->zc_history_len, hist_buf)) == 0) {
1660 error = ddi_copyout(hist_buf,
1661 (void *)(uintptr_t)zc->zc_history,
1662 zc->zc_history_len, zc->zc_iflags);
1665 spa_close(spa, FTAG);
1666 kmem_free(hist_buf, size);
1667 return (error);
1670 static int
1671 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1673 spa_t *spa;
1674 int error;
1676 error = spa_open(zc->zc_name, &spa, FTAG);
1677 if (error == 0) {
1678 error = spa_change_guid(spa);
1679 spa_close(spa, FTAG);
1681 return (error);
1684 static int
1685 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1687 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1691 * inputs:
1692 * zc_name name of filesystem
1693 * zc_obj object to find
1695 * outputs:
1696 * zc_value name of object
1698 static int
1699 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1701 objset_t *os;
1702 int error;
1704 /* XXX reading from objset not owned */
1705 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1706 return (error);
1707 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1708 dmu_objset_rele(os, FTAG);
1709 return (SET_ERROR(EINVAL));
1711 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1712 sizeof (zc->zc_value));
1713 dmu_objset_rele(os, FTAG);
1715 return (error);
1719 * inputs:
1720 * zc_name name of filesystem
1721 * zc_obj object to find
1723 * outputs:
1724 * zc_stat stats on object
1725 * zc_value path to object
1727 static int
1728 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1730 objset_t *os;
1731 int error;
1733 /* XXX reading from objset not owned */
1734 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1735 return (error);
1736 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1737 dmu_objset_rele(os, FTAG);
1738 return (SET_ERROR(EINVAL));
1740 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1741 sizeof (zc->zc_value));
1742 dmu_objset_rele(os, FTAG);
1744 return (error);
1747 static int
1748 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1750 spa_t *spa;
1751 int error;
1752 nvlist_t *config, **l2cache, **spares;
1753 uint_t nl2cache = 0, nspares = 0;
1755 error = spa_open(zc->zc_name, &spa, FTAG);
1756 if (error != 0)
1757 return (error);
1759 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1760 zc->zc_iflags, &config);
1761 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1762 &l2cache, &nl2cache);
1764 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1765 &spares, &nspares);
1768 * A root pool with concatenated devices is not supported.
1769 * Thus, can not add a device to a root pool.
1771 * Intent log device can not be added to a rootpool because
1772 * during mountroot, zil is replayed, a seperated log device
1773 * can not be accessed during the mountroot time.
1775 * l2cache and spare devices are ok to be added to a rootpool.
1777 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1778 nvlist_free(config);
1779 spa_close(spa, FTAG);
1780 return (SET_ERROR(EDOM));
1783 if (error == 0) {
1784 error = spa_vdev_add(spa, config);
1785 nvlist_free(config);
1787 spa_close(spa, FTAG);
1788 return (error);
1792 * inputs:
1793 * zc_name name of the pool
1794 * zc_nvlist_conf nvlist of devices to remove
1795 * zc_cookie to stop the remove?
1797 static int
1798 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1800 spa_t *spa;
1801 int error;
1803 error = spa_open(zc->zc_name, &spa, FTAG);
1804 if (error != 0)
1805 return (error);
1806 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1807 spa_close(spa, FTAG);
1808 return (error);
1811 static int
1812 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1814 spa_t *spa;
1815 int error;
1816 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1818 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1819 return (error);
1820 switch (zc->zc_cookie) {
1821 case VDEV_STATE_ONLINE:
1822 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1823 break;
1825 case VDEV_STATE_OFFLINE:
1826 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1827 break;
1829 case VDEV_STATE_FAULTED:
1830 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1831 zc->zc_obj != VDEV_AUX_EXTERNAL)
1832 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1834 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1835 break;
1837 case VDEV_STATE_DEGRADED:
1838 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1839 zc->zc_obj != VDEV_AUX_EXTERNAL)
1840 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1842 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1843 break;
1845 default:
1846 error = SET_ERROR(EINVAL);
1848 zc->zc_cookie = newstate;
1849 spa_close(spa, FTAG);
1850 return (error);
1853 static int
1854 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1856 spa_t *spa;
1857 int replacing = zc->zc_cookie;
1858 nvlist_t *config;
1859 int error;
1861 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1862 return (error);
1864 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1865 zc->zc_iflags, &config)) == 0) {
1866 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1867 nvlist_free(config);
1870 spa_close(spa, FTAG);
1871 return (error);
1874 static int
1875 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1877 spa_t *spa;
1878 int error;
1880 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1881 return (error);
1883 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1885 spa_close(spa, FTAG);
1886 return (error);
1889 static int
1890 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1892 spa_t *spa;
1893 nvlist_t *config, *props = NULL;
1894 int error;
1895 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1897 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1898 return (error);
1900 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1901 zc->zc_iflags, &config)) {
1902 spa_close(spa, FTAG);
1903 return (error);
1906 if (zc->zc_nvlist_src_size != 0 && (error =
1907 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1908 zc->zc_iflags, &props))) {
1909 spa_close(spa, FTAG);
1910 nvlist_free(config);
1911 return (error);
1914 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1916 spa_close(spa, FTAG);
1918 nvlist_free(config);
1919 nvlist_free(props);
1921 return (error);
1924 static int
1925 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1927 spa_t *spa;
1928 char *path = zc->zc_value;
1929 uint64_t guid = zc->zc_guid;
1930 int error;
1932 error = spa_open(zc->zc_name, &spa, FTAG);
1933 if (error != 0)
1934 return (error);
1936 error = spa_vdev_setpath(spa, guid, path);
1937 spa_close(spa, FTAG);
1938 return (error);
1941 static int
1942 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1944 spa_t *spa;
1945 char *fru = zc->zc_value;
1946 uint64_t guid = zc->zc_guid;
1947 int error;
1949 error = spa_open(zc->zc_name, &spa, FTAG);
1950 if (error != 0)
1951 return (error);
1953 error = spa_vdev_setfru(spa, guid, fru);
1954 spa_close(spa, FTAG);
1955 return (error);
1958 static int
1959 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1961 int error = 0;
1962 nvlist_t *nv;
1964 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1966 if (zc->zc_nvlist_dst != 0 &&
1967 (error = dsl_prop_get_all(os, &nv)) == 0) {
1968 dmu_objset_stats(os, nv);
1970 * NB: zvol_get_stats() will read the objset contents,
1971 * which we aren't supposed to do with a
1972 * DS_MODE_USER hold, because it could be
1973 * inconsistent. So this is a bit of a workaround...
1974 * XXX reading with out owning
1976 if (!zc->zc_objset_stats.dds_inconsistent &&
1977 dmu_objset_type(os) == DMU_OST_ZVOL) {
1978 error = zvol_get_stats(os, nv);
1979 if (error == EIO)
1980 return (error);
1981 VERIFY0(error);
1983 error = put_nvlist(zc, nv);
1984 nvlist_free(nv);
1987 return (error);
1991 * inputs:
1992 * zc_name name of filesystem
1993 * zc_nvlist_dst_size size of buffer for property nvlist
1995 * outputs:
1996 * zc_objset_stats stats
1997 * zc_nvlist_dst property nvlist
1998 * zc_nvlist_dst_size size of property nvlist
2000 static int
2001 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2003 objset_t *os;
2004 int error;
2006 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2007 if (error == 0) {
2008 error = zfs_ioc_objset_stats_impl(zc, os);
2009 dmu_objset_rele(os, FTAG);
2012 return (error);
2016 * inputs:
2017 * zc_name name of filesystem
2018 * zc_nvlist_dst_size size of buffer for property nvlist
2020 * outputs:
2021 * zc_nvlist_dst received property nvlist
2022 * zc_nvlist_dst_size size of received property nvlist
2024 * Gets received properties (distinct from local properties on or after
2025 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2026 * local property values.
2028 static int
2029 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2031 int error = 0;
2032 nvlist_t *nv;
2035 * Without this check, we would return local property values if the
2036 * caller has not already received properties on or after
2037 * SPA_VERSION_RECVD_PROPS.
2039 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2040 return (SET_ERROR(ENOTSUP));
2042 if (zc->zc_nvlist_dst != 0 &&
2043 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2044 error = put_nvlist(zc, nv);
2045 nvlist_free(nv);
2048 return (error);
2051 static int
2052 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2054 uint64_t value;
2055 int error;
2058 * zfs_get_zplprop() will either find a value or give us
2059 * the default value (if there is one).
2061 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2062 return (error);
2063 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2064 return (0);
2068 * inputs:
2069 * zc_name name of filesystem
2070 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2072 * outputs:
2073 * zc_nvlist_dst zpl property nvlist
2074 * zc_nvlist_dst_size size of zpl property nvlist
2076 static int
2077 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2079 objset_t *os;
2080 int err;
2082 /* XXX reading without owning */
2083 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2084 return (err);
2086 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2089 * NB: nvl_add_zplprop() will read the objset contents,
2090 * which we aren't supposed to do with a DS_MODE_USER
2091 * hold, because it could be inconsistent.
2093 if (zc->zc_nvlist_dst != (uintptr_t)NULL &&
2094 !zc->zc_objset_stats.dds_inconsistent &&
2095 dmu_objset_type(os) == DMU_OST_ZFS) {
2096 nvlist_t *nv;
2098 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2099 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2100 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2101 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2102 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2103 err = put_nvlist(zc, nv);
2104 nvlist_free(nv);
2105 } else {
2106 err = SET_ERROR(ENOENT);
2108 dmu_objset_rele(os, FTAG);
2109 return (err);
2112 static boolean_t
2113 dataset_name_hidden(const char *name)
2116 * Skip over datasets that are not visible in this zone,
2117 * internal datasets (which have a $ in their name), and
2118 * temporary datasets (which have a % in their name).
2120 if (strchr(name, '$') != NULL)
2121 return (B_TRUE);
2122 if (strchr(name, '%') != NULL)
2123 return (B_TRUE);
2124 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2125 return (B_TRUE);
2126 return (B_FALSE);
2130 * inputs:
2131 * zc_name name of filesystem
2132 * zc_cookie zap cursor
2133 * zc_nvlist_dst_size size of buffer for property nvlist
2135 * outputs:
2136 * zc_name name of next filesystem
2137 * zc_cookie zap cursor
2138 * zc_objset_stats stats
2139 * zc_nvlist_dst property nvlist
2140 * zc_nvlist_dst_size size of property nvlist
2142 static int
2143 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2145 objset_t *os;
2146 int error;
2147 char *p;
2148 size_t orig_len = strlen(zc->zc_name);
2150 top:
2151 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2152 if (error == ENOENT)
2153 error = SET_ERROR(ESRCH);
2154 return (error);
2157 p = strrchr(zc->zc_name, '/');
2158 if (p == NULL || p[1] != '\0')
2159 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2160 p = zc->zc_name + strlen(zc->zc_name);
2162 do {
2163 error = dmu_dir_list_next(os,
2164 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2165 NULL, &zc->zc_cookie);
2166 if (error == ENOENT)
2167 error = SET_ERROR(ESRCH);
2168 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2169 dmu_objset_rele(os, FTAG);
2172 * If it's an internal dataset (ie. with a '$' in its name),
2173 * don't try to get stats for it, otherwise we'll return ENOENT.
2175 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2176 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2177 if (error == ENOENT) {
2178 /* We lost a race with destroy, get the next one. */
2179 zc->zc_name[orig_len] = '\0';
2180 goto top;
2183 return (error);
2187 * inputs:
2188 * zc_name name of filesystem
2189 * zc_cookie zap cursor
2190 * zc_nvlist_dst_size size of buffer for property nvlist
2191 * zc_simple when set, only name is requested
2193 * outputs:
2194 * zc_name name of next snapshot
2195 * zc_objset_stats stats
2196 * zc_nvlist_dst property nvlist
2197 * zc_nvlist_dst_size size of property nvlist
2199 static int
2200 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2202 objset_t *os;
2203 int error;
2205 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2206 if (error != 0) {
2207 return (error == ENOENT ? ESRCH : error);
2211 * A dataset name of maximum length cannot have any snapshots,
2212 * so exit immediately.
2214 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2215 ZFS_MAX_DATASET_NAME_LEN) {
2216 dmu_objset_rele(os, FTAG);
2217 return (SET_ERROR(ESRCH));
2220 error = dmu_snapshot_list_next(os,
2221 sizeof (zc->zc_name) - strlen(zc->zc_name),
2222 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2223 NULL);
2225 if (error == 0 && !zc->zc_simple) {
2226 dsl_dataset_t *ds;
2227 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2229 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2230 if (error == 0) {
2231 objset_t *ossnap;
2233 error = dmu_objset_from_ds(ds, &ossnap);
2234 if (error == 0)
2235 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2236 dsl_dataset_rele(ds, FTAG);
2238 } else if (error == ENOENT) {
2239 error = SET_ERROR(ESRCH);
2242 dmu_objset_rele(os, FTAG);
2243 /* if we failed, undo the @ that we tacked on to zc_name */
2244 if (error != 0)
2245 *strchr(zc->zc_name, '@') = '\0';
2246 return (error);
2249 static int
2250 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2252 const char *propname = nvpair_name(pair);
2253 uint64_t *valary;
2254 unsigned int vallen;
2255 const char *domain;
2256 char *dash;
2257 zfs_userquota_prop_t type;
2258 uint64_t rid;
2259 uint64_t quota;
2260 zfsvfs_t *zfsvfs;
2261 int err;
2263 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2264 nvlist_t *attrs;
2265 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2266 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2267 &pair) != 0)
2268 return (SET_ERROR(EINVAL));
2272 * A correctly constructed propname is encoded as
2273 * userquota@<rid>-<domain>.
2275 if ((dash = strchr(propname, '-')) == NULL ||
2276 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2277 vallen != 3)
2278 return (SET_ERROR(EINVAL));
2280 domain = dash + 1;
2281 type = valary[0];
2282 rid = valary[1];
2283 quota = valary[2];
2285 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2286 if (err == 0) {
2287 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2288 zfsvfs_rele(zfsvfs, FTAG);
2291 return (err);
2295 * If the named property is one that has a special function to set its value,
2296 * return 0 on success and a positive error code on failure; otherwise if it is
2297 * not one of the special properties handled by this function, return -1.
2299 * XXX: It would be better for callers of the property interface if we handled
2300 * these special cases in dsl_prop.c (in the dsl layer).
2302 static int
2303 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2304 nvpair_t *pair)
2306 const char *propname = nvpair_name(pair);
2307 zfs_prop_t prop = zfs_name_to_prop(propname);
2308 uint64_t intval;
2309 int err = -1;
2311 if (prop == ZPROP_INVAL) {
2312 if (zfs_prop_userquota(propname))
2313 return (zfs_prop_set_userquota(dsname, pair));
2314 return (-1);
2317 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2318 nvlist_t *attrs;
2319 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2320 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2321 &pair) == 0);
2324 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2325 return (-1);
2327 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2329 switch (prop) {
2330 case ZFS_PROP_QUOTA:
2331 err = dsl_dir_set_quota(dsname, source, intval);
2332 break;
2333 case ZFS_PROP_REFQUOTA:
2334 err = dsl_dataset_set_refquota(dsname, source, intval);
2335 break;
2336 case ZFS_PROP_FILESYSTEM_LIMIT:
2337 case ZFS_PROP_SNAPSHOT_LIMIT:
2338 if (intval == UINT64_MAX) {
2339 /* clearing the limit, just do it */
2340 err = 0;
2341 } else {
2342 err = dsl_dir_activate_fs_ss_limit(dsname);
2345 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2346 * default path to set the value in the nvlist.
2348 if (err == 0)
2349 err = -1;
2350 break;
2351 case ZFS_PROP_RESERVATION:
2352 err = dsl_dir_set_reservation(dsname, source, intval);
2353 break;
2354 case ZFS_PROP_REFRESERVATION:
2355 err = dsl_dataset_set_refreservation(dsname, source, intval);
2356 break;
2357 case ZFS_PROP_VOLSIZE:
2358 err = zvol_set_volsize(dsname, intval);
2359 break;
2360 case ZFS_PROP_VERSION:
2362 zfsvfs_t *zfsvfs;
2364 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2365 break;
2367 err = zfs_set_version(zfsvfs, intval);
2368 zfsvfs_rele(zfsvfs, FTAG);
2370 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2371 zfs_cmd_t *zc;
2373 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2374 (void) strcpy(zc->zc_name, dsname);
2375 (void) zfs_ioc_userspace_upgrade(zc);
2376 kmem_free(zc, sizeof (zfs_cmd_t));
2378 break;
2380 default:
2381 err = -1;
2384 return (err);
2388 * This function is best effort. If it fails to set any of the given properties,
2389 * it continues to set as many as it can and returns the last error
2390 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2391 * with the list of names of all the properties that failed along with the
2392 * corresponding error numbers.
2394 * If every property is set successfully, zero is returned and errlist is not
2395 * modified.
2398 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2399 nvlist_t *errlist)
2401 nvpair_t *pair;
2402 nvpair_t *propval;
2403 int rv = 0;
2404 uint64_t intval;
2405 char *strval;
2406 nvlist_t *genericnvl = fnvlist_alloc();
2407 nvlist_t *retrynvl = fnvlist_alloc();
2409 retry:
2410 pair = NULL;
2411 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2412 const char *propname = nvpair_name(pair);
2413 zfs_prop_t prop = zfs_name_to_prop(propname);
2414 int err = 0;
2416 /* decode the property value */
2417 propval = pair;
2418 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2419 nvlist_t *attrs;
2420 attrs = fnvpair_value_nvlist(pair);
2421 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2422 &propval) != 0)
2423 err = SET_ERROR(EINVAL);
2426 /* Validate value type */
2427 if (err == 0 && prop == ZPROP_INVAL) {
2428 if (zfs_prop_user(propname)) {
2429 if (nvpair_type(propval) != DATA_TYPE_STRING)
2430 err = SET_ERROR(EINVAL);
2431 } else if (zfs_prop_userquota(propname)) {
2432 if (nvpair_type(propval) !=
2433 DATA_TYPE_UINT64_ARRAY)
2434 err = SET_ERROR(EINVAL);
2435 } else {
2436 err = SET_ERROR(EINVAL);
2438 } else if (err == 0) {
2439 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2440 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2441 err = SET_ERROR(EINVAL);
2442 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2443 const char *unused;
2445 intval = fnvpair_value_uint64(propval);
2447 switch (zfs_prop_get_type(prop)) {
2448 case PROP_TYPE_NUMBER:
2449 break;
2450 case PROP_TYPE_STRING:
2451 err = SET_ERROR(EINVAL);
2452 break;
2453 case PROP_TYPE_INDEX:
2454 if (zfs_prop_index_to_string(prop,
2455 intval, &unused) != 0)
2456 err = SET_ERROR(EINVAL);
2457 break;
2458 default:
2459 cmn_err(CE_PANIC,
2460 "unknown property type");
2462 } else {
2463 err = SET_ERROR(EINVAL);
2467 /* Validate permissions */
2468 if (err == 0)
2469 err = zfs_check_settable(dsname, pair, CRED());
2471 if (err == 0) {
2472 err = zfs_prop_set_special(dsname, source, pair);
2473 if (err == -1) {
2475 * For better performance we build up a list of
2476 * properties to set in a single transaction.
2478 err = nvlist_add_nvpair(genericnvl, pair);
2479 } else if (err != 0 && nvl != retrynvl) {
2481 * This may be a spurious error caused by
2482 * receiving quota and reservation out of order.
2483 * Try again in a second pass.
2485 err = nvlist_add_nvpair(retrynvl, pair);
2489 if (err != 0) {
2490 if (errlist != NULL)
2491 fnvlist_add_int32(errlist, propname, err);
2492 rv = err;
2496 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2497 nvl = retrynvl;
2498 goto retry;
2501 if (!nvlist_empty(genericnvl) &&
2502 dsl_props_set(dsname, source, genericnvl) != 0) {
2504 * If this fails, we still want to set as many properties as we
2505 * can, so try setting them individually.
2507 pair = NULL;
2508 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2509 const char *propname = nvpair_name(pair);
2510 int err = 0;
2512 propval = pair;
2513 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2514 nvlist_t *attrs;
2515 attrs = fnvpair_value_nvlist(pair);
2516 propval = fnvlist_lookup_nvpair(attrs,
2517 ZPROP_VALUE);
2520 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2521 strval = fnvpair_value_string(propval);
2522 err = dsl_prop_set_string(dsname, propname,
2523 source, strval);
2524 } else {
2525 intval = fnvpair_value_uint64(propval);
2526 err = dsl_prop_set_int(dsname, propname, source,
2527 intval);
2530 if (err != 0) {
2531 if (errlist != NULL) {
2532 fnvlist_add_int32(errlist, propname,
2533 err);
2535 rv = err;
2539 nvlist_free(genericnvl);
2540 nvlist_free(retrynvl);
2542 return (rv);
2546 * Check that all the properties are valid user properties.
2548 static int
2549 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2551 nvpair_t *pair = NULL;
2552 int error = 0;
2554 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2555 const char *propname = nvpair_name(pair);
2557 if (!zfs_prop_user(propname) ||
2558 nvpair_type(pair) != DATA_TYPE_STRING)
2559 return (SET_ERROR(EINVAL));
2561 if (error = zfs_secpolicy_write_perms(fsname,
2562 ZFS_DELEG_PERM_USERPROP, CRED()))
2563 return (error);
2565 if (strlen(propname) >= ZAP_MAXNAMELEN)
2566 return (SET_ERROR(ENAMETOOLONG));
2568 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2569 return (E2BIG);
2571 return (0);
2574 static void
2575 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2577 nvpair_t *pair;
2579 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2581 pair = NULL;
2582 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2583 if (nvlist_exists(skipped, nvpair_name(pair)))
2584 continue;
2586 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2590 static int
2591 clear_received_props(const char *dsname, nvlist_t *props,
2592 nvlist_t *skipped)
2594 int err = 0;
2595 nvlist_t *cleared_props = NULL;
2596 props_skip(props, skipped, &cleared_props);
2597 if (!nvlist_empty(cleared_props)) {
2599 * Acts on local properties until the dataset has received
2600 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2602 zprop_source_t flags = (ZPROP_SRC_NONE |
2603 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2604 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2606 nvlist_free(cleared_props);
2607 return (err);
2611 * inputs:
2612 * zc_name name of filesystem
2613 * zc_value name of property to set
2614 * zc_nvlist_src{_size} nvlist of properties to apply
2615 * zc_cookie received properties flag
2617 * outputs:
2618 * zc_nvlist_dst{_size} error for each unapplied received property
2620 static int
2621 zfs_ioc_set_prop(zfs_cmd_t *zc)
2623 nvlist_t *nvl;
2624 boolean_t received = zc->zc_cookie;
2625 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2626 ZPROP_SRC_LOCAL);
2627 nvlist_t *errors;
2628 int error;
2630 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2631 zc->zc_iflags, &nvl)) != 0)
2632 return (error);
2634 if (received) {
2635 nvlist_t *origprops;
2637 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2638 (void) clear_received_props(zc->zc_name,
2639 origprops, nvl);
2640 nvlist_free(origprops);
2643 error = dsl_prop_set_hasrecvd(zc->zc_name);
2646 errors = fnvlist_alloc();
2647 if (error == 0)
2648 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2650 if (zc->zc_nvlist_dst != (uintptr_t)NULL && errors != NULL) {
2651 (void) put_nvlist(zc, errors);
2654 nvlist_free(errors);
2655 nvlist_free(nvl);
2656 return (error);
2660 * inputs:
2661 * zc_name name of filesystem
2662 * zc_value name of property to inherit
2663 * zc_cookie revert to received value if TRUE
2665 * outputs: none
2667 static int
2668 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2670 const char *propname = zc->zc_value;
2671 zfs_prop_t prop = zfs_name_to_prop(propname);
2672 boolean_t received = zc->zc_cookie;
2673 zprop_source_t source = (received
2674 ? ZPROP_SRC_NONE /* revert to received value, if any */
2675 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2677 if (received) {
2678 nvlist_t *dummy;
2679 nvpair_t *pair;
2680 zprop_type_t type;
2681 int err;
2684 * zfs_prop_set_special() expects properties in the form of an
2685 * nvpair with type info.
2687 if (prop == ZPROP_INVAL) {
2688 if (!zfs_prop_user(propname))
2689 return (SET_ERROR(EINVAL));
2691 type = PROP_TYPE_STRING;
2692 } else if (prop == ZFS_PROP_VOLSIZE ||
2693 prop == ZFS_PROP_VERSION) {
2694 return (SET_ERROR(EINVAL));
2695 } else {
2696 type = zfs_prop_get_type(prop);
2699 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2701 switch (type) {
2702 case PROP_TYPE_STRING:
2703 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2704 break;
2705 case PROP_TYPE_NUMBER:
2706 case PROP_TYPE_INDEX:
2707 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2708 break;
2709 default:
2710 nvlist_free(dummy);
2711 return (SET_ERROR(EINVAL));
2714 pair = nvlist_next_nvpair(dummy, NULL);
2715 err = zfs_prop_set_special(zc->zc_name, source, pair);
2716 nvlist_free(dummy);
2717 if (err != -1)
2718 return (err); /* special property already handled */
2719 } else {
2721 * Only check this in the non-received case. We want to allow
2722 * 'inherit -S' to revert non-inheritable properties like quota
2723 * and reservation to the received or default values even though
2724 * they are not considered inheritable.
2726 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2727 return (SET_ERROR(EINVAL));
2730 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2731 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2734 static int
2735 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2737 nvlist_t *props;
2738 spa_t *spa;
2739 int error;
2740 nvpair_t *pair;
2742 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2743 zc->zc_iflags, &props))
2744 return (error);
2747 * If the only property is the configfile, then just do a spa_lookup()
2748 * to handle the faulted case.
2750 pair = nvlist_next_nvpair(props, NULL);
2751 if (pair != NULL && strcmp(nvpair_name(pair),
2752 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2753 nvlist_next_nvpair(props, pair) == NULL) {
2754 mutex_enter(&spa_namespace_lock);
2755 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2756 spa_configfile_set(spa, props, B_FALSE);
2757 spa_config_sync(spa, B_FALSE, B_TRUE);
2759 mutex_exit(&spa_namespace_lock);
2760 if (spa != NULL) {
2761 nvlist_free(props);
2762 return (0);
2766 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2767 nvlist_free(props);
2768 return (error);
2771 error = spa_prop_set(spa, props);
2773 nvlist_free(props);
2774 spa_close(spa, FTAG);
2776 return (error);
2779 static int
2780 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2782 spa_t *spa;
2783 int error;
2784 nvlist_t *nvp = NULL;
2786 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2788 * If the pool is faulted, there may be properties we can still
2789 * get (such as altroot and cachefile), so attempt to get them
2790 * anyway.
2792 mutex_enter(&spa_namespace_lock);
2793 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2794 error = spa_prop_get(spa, &nvp);
2795 mutex_exit(&spa_namespace_lock);
2796 } else {
2797 error = spa_prop_get(spa, &nvp);
2798 spa_close(spa, FTAG);
2801 if (error == 0 && zc->zc_nvlist_dst != (uintptr_t)NULL)
2802 error = put_nvlist(zc, nvp);
2803 else
2804 error = SET_ERROR(EFAULT);
2806 nvlist_free(nvp);
2807 return (error);
2811 * inputs:
2812 * zc_name name of filesystem
2813 * zc_nvlist_src{_size} nvlist of delegated permissions
2814 * zc_perm_action allow/unallow flag
2816 * outputs: none
2818 static int
2819 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2821 int error;
2822 nvlist_t *fsaclnv = NULL;
2824 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2825 zc->zc_iflags, &fsaclnv)) != 0)
2826 return (error);
2829 * Verify nvlist is constructed correctly
2831 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2832 nvlist_free(fsaclnv);
2833 return (SET_ERROR(EINVAL));
2837 * If we don't have PRIV_SYS_MOUNT, then validate
2838 * that user is allowed to hand out each permission in
2839 * the nvlist(s)
2842 error = secpolicy_zfs(CRED());
2843 if (error != 0) {
2844 if (zc->zc_perm_action == B_FALSE) {
2845 error = dsl_deleg_can_allow(zc->zc_name,
2846 fsaclnv, CRED());
2847 } else {
2848 error = dsl_deleg_can_unallow(zc->zc_name,
2849 fsaclnv, CRED());
2853 if (error == 0)
2854 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2856 nvlist_free(fsaclnv);
2857 return (error);
2861 * inputs:
2862 * zc_name name of filesystem
2864 * outputs:
2865 * zc_nvlist_src{_size} nvlist of delegated permissions
2867 static int
2868 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2870 nvlist_t *nvp;
2871 int error;
2873 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2874 error = put_nvlist(zc, nvp);
2875 nvlist_free(nvp);
2878 return (error);
2881 /* ARGSUSED */
2882 static void
2883 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2885 zfs_creat_t *zct = arg;
2887 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2890 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2893 * inputs:
2894 * os parent objset pointer (NULL if root fs)
2895 * fuids_ok fuids allowed in this version of the spa?
2896 * sa_ok SAs allowed in this version of the spa?
2897 * createprops list of properties requested by creator
2899 * outputs:
2900 * zplprops values for the zplprops we attach to the master node object
2901 * is_ci true if requested file system will be purely case-insensitive
2903 * Determine the settings for utf8only, normalization and
2904 * casesensitivity. Specific values may have been requested by the
2905 * creator and/or we can inherit values from the parent dataset. If
2906 * the file system is of too early a vintage, a creator can not
2907 * request settings for these properties, even if the requested
2908 * setting is the default value. We don't actually want to create dsl
2909 * properties for these, so remove them from the source nvlist after
2910 * processing.
2912 static int
2913 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2914 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2915 nvlist_t *zplprops, boolean_t *is_ci)
2917 uint64_t sense = ZFS_PROP_UNDEFINED;
2918 uint64_t norm = ZFS_PROP_UNDEFINED;
2919 uint64_t u8 = ZFS_PROP_UNDEFINED;
2921 ASSERT(zplprops != NULL);
2923 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
2924 return (SET_ERROR(EINVAL));
2927 * Pull out creator prop choices, if any.
2929 if (createprops) {
2930 (void) nvlist_lookup_uint64(createprops,
2931 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2932 (void) nvlist_lookup_uint64(createprops,
2933 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2934 (void) nvlist_remove_all(createprops,
2935 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2936 (void) nvlist_lookup_uint64(createprops,
2937 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2938 (void) nvlist_remove_all(createprops,
2939 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2940 (void) nvlist_lookup_uint64(createprops,
2941 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2942 (void) nvlist_remove_all(createprops,
2943 zfs_prop_to_name(ZFS_PROP_CASE));
2947 * If the zpl version requested is whacky or the file system
2948 * or pool is version is too "young" to support normalization
2949 * and the creator tried to set a value for one of the props,
2950 * error out.
2952 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2953 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2954 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2955 (zplver < ZPL_VERSION_NORMALIZATION &&
2956 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2957 sense != ZFS_PROP_UNDEFINED)))
2958 return (SET_ERROR(ENOTSUP));
2961 * Put the version in the zplprops
2963 VERIFY(nvlist_add_uint64(zplprops,
2964 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2966 if (norm == ZFS_PROP_UNDEFINED)
2967 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2968 VERIFY(nvlist_add_uint64(zplprops,
2969 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2972 * If we're normalizing, names must always be valid UTF-8 strings.
2974 if (norm)
2975 u8 = 1;
2976 if (u8 == ZFS_PROP_UNDEFINED)
2977 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2978 VERIFY(nvlist_add_uint64(zplprops,
2979 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2981 if (sense == ZFS_PROP_UNDEFINED)
2982 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2983 VERIFY(nvlist_add_uint64(zplprops,
2984 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2986 if (is_ci)
2987 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2989 return (0);
2992 static int
2993 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2994 nvlist_t *zplprops, boolean_t *is_ci)
2996 boolean_t fuids_ok, sa_ok;
2997 uint64_t zplver = ZPL_VERSION;
2998 objset_t *os = NULL;
2999 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3000 char *cp;
3001 spa_t *spa;
3002 uint64_t spa_vers;
3003 int error;
3005 (void) strlcpy(parentname, dataset, sizeof (parentname));
3006 cp = strrchr(parentname, '/');
3007 ASSERT(cp != NULL);
3008 cp[0] = '\0';
3010 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3011 return (error);
3013 spa_vers = spa_version(spa);
3014 spa_close(spa, FTAG);
3016 zplver = zfs_zpl_version_map(spa_vers);
3017 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3018 sa_ok = (zplver >= ZPL_VERSION_SA);
3021 * Open parent object set so we can inherit zplprop values.
3023 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3024 return (error);
3026 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3027 zplprops, is_ci);
3028 dmu_objset_rele(os, FTAG);
3029 return (error);
3032 static int
3033 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3034 nvlist_t *zplprops, boolean_t *is_ci)
3036 boolean_t fuids_ok;
3037 boolean_t sa_ok;
3038 uint64_t zplver = ZPL_VERSION;
3039 int error;
3041 zplver = zfs_zpl_version_map(spa_vers);
3042 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3043 sa_ok = (zplver >= ZPL_VERSION_SA);
3045 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3046 createprops, zplprops, is_ci);
3047 return (error);
3051 * innvl: {
3052 * "type" -> dmu_objset_type_t (int32)
3053 * (optional) "props" -> { prop -> value }
3056 * outnvl: propname -> error code (int32)
3058 static int
3059 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3061 int error = 0;
3062 zfs_creat_t zct = { 0 };
3063 nvlist_t *nvprops = NULL;
3064 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3065 int32_t type32;
3066 dmu_objset_type_t type;
3067 boolean_t is_insensitive = B_FALSE;
3069 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3070 return (SET_ERROR(EINVAL));
3071 type = type32;
3072 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3074 switch (type) {
3075 case DMU_OST_ZFS:
3076 cbfunc = zfs_create_cb;
3077 break;
3079 case DMU_OST_ZVOL:
3080 cbfunc = zvol_create_cb;
3081 break;
3083 default:
3084 cbfunc = NULL;
3085 break;
3087 if (strchr(fsname, '@') ||
3088 strchr(fsname, '%'))
3089 return (SET_ERROR(EINVAL));
3091 zct.zct_props = nvprops;
3093 if (cbfunc == NULL)
3094 return (SET_ERROR(EINVAL));
3096 if (type == DMU_OST_ZVOL) {
3097 uint64_t volsize, volblocksize;
3099 if (nvprops == NULL)
3100 return (SET_ERROR(EINVAL));
3101 if (nvlist_lookup_uint64(nvprops,
3102 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3103 return (SET_ERROR(EINVAL));
3105 if ((error = nvlist_lookup_uint64(nvprops,
3106 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3107 &volblocksize)) != 0 && error != ENOENT)
3108 return (SET_ERROR(EINVAL));
3110 if (error != 0)
3111 volblocksize = zfs_prop_default_numeric(
3112 ZFS_PROP_VOLBLOCKSIZE);
3114 if ((error = zvol_check_volblocksize(
3115 volblocksize)) != 0 ||
3116 (error = zvol_check_volsize(volsize,
3117 volblocksize)) != 0)
3118 return (error);
3119 } else if (type == DMU_OST_ZFS) {
3120 int error;
3123 * We have to have normalization and
3124 * case-folding flags correct when we do the
3125 * file system creation, so go figure them out
3126 * now.
3128 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3129 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3130 error = zfs_fill_zplprops(fsname, nvprops,
3131 zct.zct_zplprops, &is_insensitive);
3132 if (error != 0) {
3133 nvlist_free(zct.zct_zplprops);
3134 return (error);
3138 error = dmu_objset_create(fsname, type,
3139 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3140 nvlist_free(zct.zct_zplprops);
3143 * It would be nice to do this atomically.
3145 if (error == 0) {
3146 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3147 nvprops, outnvl);
3148 if (error != 0)
3149 (void) dsl_destroy_head(fsname);
3151 return (error);
3155 * innvl: {
3156 * "origin" -> name of origin snapshot
3157 * (optional) "props" -> { prop -> value }
3160 * outnvl: propname -> error code (int32)
3162 static int
3163 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3165 int error = 0;
3166 nvlist_t *nvprops = NULL;
3167 char *origin_name;
3169 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3170 return (SET_ERROR(EINVAL));
3171 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3173 if (strchr(fsname, '@') ||
3174 strchr(fsname, '%'))
3175 return (SET_ERROR(EINVAL));
3177 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3178 return (SET_ERROR(EINVAL));
3179 error = dmu_objset_clone(fsname, origin_name);
3180 if (error != 0)
3181 return (error);
3184 * It would be nice to do this atomically.
3186 if (error == 0) {
3187 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3188 nvprops, outnvl);
3189 if (error != 0)
3190 (void) dsl_destroy_head(fsname);
3192 return (error);
3196 * innvl: {
3197 * "snaps" -> { snapshot1, snapshot2 }
3198 * (optional) "props" -> { prop -> value (string) }
3201 * outnvl: snapshot -> error code (int32)
3203 static int
3204 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3206 nvlist_t *snaps;
3207 nvlist_t *props = NULL;
3208 int error, poollen;
3209 nvpair_t *pair;
3211 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3212 if ((error = zfs_check_userprops(poolname, props)) != 0)
3213 return (error);
3215 if (!nvlist_empty(props) &&
3216 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3217 return (SET_ERROR(ENOTSUP));
3219 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3220 return (SET_ERROR(EINVAL));
3221 poollen = strlen(poolname);
3222 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3223 pair = nvlist_next_nvpair(snaps, pair)) {
3224 const char *name = nvpair_name(pair);
3225 const char *cp = strchr(name, '@');
3228 * The snap name must contain an @, and the part after it must
3229 * contain only valid characters.
3231 if (cp == NULL ||
3232 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3233 return (SET_ERROR(EINVAL));
3236 * The snap must be in the specified pool.
3238 if (strncmp(name, poolname, poollen) != 0 ||
3239 (name[poollen] != '/' && name[poollen] != '@'))
3240 return (SET_ERROR(EXDEV));
3242 /* This must be the only snap of this fs. */
3243 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3244 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3245 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3246 == 0) {
3247 return (SET_ERROR(EXDEV));
3252 error = dsl_dataset_snapshot(snaps, props, outnvl);
3253 return (error);
3257 * innvl: "message" -> string
3259 /* ARGSUSED */
3260 static int
3261 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3263 char *message;
3264 spa_t *spa;
3265 int error;
3266 char *poolname;
3269 * The poolname in the ioctl is not set, we get it from the TSD,
3270 * which was set at the end of the last successful ioctl that allows
3271 * logging. The secpolicy func already checked that it is set.
3272 * Only one log ioctl is allowed after each successful ioctl, so
3273 * we clear the TSD here.
3275 poolname = tsd_get(zfs_allow_log_key);
3276 (void) tsd_set(zfs_allow_log_key, NULL);
3277 error = spa_open(poolname, &spa, FTAG);
3278 strfree(poolname);
3279 if (error != 0)
3280 return (error);
3282 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3283 spa_close(spa, FTAG);
3284 return (SET_ERROR(EINVAL));
3287 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3288 spa_close(spa, FTAG);
3289 return (SET_ERROR(ENOTSUP));
3292 error = spa_history_log(spa, message);
3293 spa_close(spa, FTAG);
3294 return (error);
3298 * The dp_config_rwlock must not be held when calling this, because the
3299 * unmount may need to write out data.
3301 * This function is best-effort. Callers must deal gracefully if it
3302 * remains mounted (or is remounted after this call).
3304 * Returns 0 if the argument is not a snapshot, or it is not currently a
3305 * filesystem, or we were able to unmount it. Returns error code otherwise.
3307 void
3308 zfs_unmount_snap(const char *snapname)
3310 vfs_t *vfsp = NULL;
3311 zfsvfs_t *zfsvfs = NULL;
3313 if (strchr(snapname, '@') == NULL)
3314 return;
3316 int err = getzfsvfs(snapname, &zfsvfs);
3317 if (err != 0) {
3318 ASSERT3P(zfsvfs, ==, NULL);
3319 return;
3321 vfsp = zfsvfs->z_vfs;
3323 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3325 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3326 VFS_RELE(vfsp);
3327 if (err != 0)
3328 return;
3331 * Always force the unmount for snapshots.
3333 (void) dounmount(vfsp, MS_FORCE, kcred);
3336 /* ARGSUSED */
3337 static int
3338 zfs_unmount_snap_cb(const char *snapname, void *arg)
3340 zfs_unmount_snap(snapname);
3341 return (0);
3345 * When a clone is destroyed, its origin may also need to be destroyed,
3346 * in which case it must be unmounted. This routine will do that unmount
3347 * if necessary.
3349 void
3350 zfs_destroy_unmount_origin(const char *fsname)
3352 int error;
3353 objset_t *os;
3354 dsl_dataset_t *ds;
3356 error = dmu_objset_hold(fsname, FTAG, &os);
3357 if (error != 0)
3358 return;
3359 ds = dmu_objset_ds(os);
3360 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3361 char originname[ZFS_MAX_DATASET_NAME_LEN];
3362 dsl_dataset_name(ds->ds_prev, originname);
3363 dmu_objset_rele(os, FTAG);
3364 zfs_unmount_snap(originname);
3365 } else {
3366 dmu_objset_rele(os, FTAG);
3371 * innvl: {
3372 * "snaps" -> { snapshot1, snapshot2 }
3373 * (optional boolean) "defer"
3376 * outnvl: snapshot -> error code (int32)
3379 /* ARGSUSED */
3380 static int
3381 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3383 nvlist_t *snaps;
3384 nvpair_t *pair;
3385 boolean_t defer;
3387 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3388 return (SET_ERROR(EINVAL));
3389 defer = nvlist_exists(innvl, "defer");
3391 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3392 pair = nvlist_next_nvpair(snaps, pair)) {
3393 zfs_unmount_snap(nvpair_name(pair));
3396 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3400 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3401 * All bookmarks must be in the same pool.
3403 * innvl: {
3404 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3407 * outnvl: bookmark -> error code (int32)
3410 /* ARGSUSED */
3411 static int
3412 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3414 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3415 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3416 char *snap_name;
3419 * Verify the snapshot argument.
3421 if (nvpair_value_string(pair, &snap_name) != 0)
3422 return (SET_ERROR(EINVAL));
3425 /* Verify that the keys (bookmarks) are unique */
3426 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3427 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3428 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3429 return (SET_ERROR(EINVAL));
3433 return (dsl_bookmark_create(innvl, outnvl));
3437 * innvl: {
3438 * property 1, property 2, ...
3441 * outnvl: {
3442 * bookmark name 1 -> { property 1, property 2, ... },
3443 * bookmark name 2 -> { property 1, property 2, ... }
3447 static int
3448 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3450 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3454 * innvl: {
3455 * bookmark name 1, bookmark name 2
3458 * outnvl: bookmark -> error code (int32)
3461 static int
3462 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3463 nvlist_t *outnvl)
3465 int error, poollen;
3467 poollen = strlen(poolname);
3468 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3469 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3470 const char *name = nvpair_name(pair);
3471 const char *cp = strchr(name, '#');
3474 * The bookmark name must contain an #, and the part after it
3475 * must contain only valid characters.
3477 if (cp == NULL ||
3478 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3479 return (SET_ERROR(EINVAL));
3482 * The bookmark must be in the specified pool.
3484 if (strncmp(name, poolname, poollen) != 0 ||
3485 (name[poollen] != '/' && name[poollen] != '#'))
3486 return (SET_ERROR(EXDEV));
3489 error = dsl_bookmark_destroy(innvl, outnvl);
3490 return (error);
3493 static int
3494 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3495 nvlist_t *outnvl)
3497 char *program;
3498 uint64_t instrlimit, memlimit;
3499 nvpair_t *nvarg = NULL;
3501 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3502 return (EINVAL);
3504 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3505 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3507 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3508 memlimit = ZCP_DEFAULT_MEMLIMIT;
3510 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3511 return (EINVAL);
3514 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3515 return (EINVAL);
3516 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3517 return (EINVAL);
3519 return (zcp_eval(poolname, program, instrlimit, memlimit,
3520 nvarg, outnvl));
3524 * inputs:
3525 * zc_name name of dataset to destroy
3526 * zc_objset_type type of objset
3527 * zc_defer_destroy mark for deferred destroy
3529 * outputs: none
3531 static int
3532 zfs_ioc_destroy(zfs_cmd_t *zc)
3534 int err;
3536 if (zc->zc_objset_type == DMU_OST_ZFS)
3537 zfs_unmount_snap(zc->zc_name);
3539 if (strchr(zc->zc_name, '@'))
3540 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3541 else
3542 err = dsl_destroy_head(zc->zc_name);
3543 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3544 (void) zvol_remove_minor(zc->zc_name);
3545 return (err);
3549 * fsname is name of dataset to rollback (to most recent snapshot)
3551 * innvl may contain name of expected target snapshot
3553 * outnvl: "target" -> name of most recent snapshot
3556 /* ARGSUSED */
3557 static int
3558 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3560 zfsvfs_t *zfsvfs;
3561 char *target = NULL;
3562 int error;
3564 (void) nvlist_lookup_string(innvl, "target", &target);
3565 if (target != NULL) {
3566 int fslen = strlen(fsname);
3568 if (strncmp(fsname, target, fslen) != 0)
3569 return (SET_ERROR(EINVAL));
3570 if (target[fslen] != '@')
3571 return (SET_ERROR(EINVAL));
3574 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3575 dsl_dataset_t *ds;
3577 ds = dmu_objset_ds(zfsvfs->z_os);
3578 error = zfs_suspend_fs(zfsvfs);
3579 if (error == 0) {
3580 int resume_err;
3582 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3583 outnvl);
3584 resume_err = zfs_resume_fs(zfsvfs, ds);
3585 error = error ? error : resume_err;
3587 VFS_RELE(zfsvfs->z_vfs);
3588 } else {
3589 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3591 return (error);
3594 static int
3595 recursive_unmount(const char *fsname, void *arg)
3597 const char *snapname = arg;
3598 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3600 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3601 zfs_unmount_snap(fullname);
3603 return (0);
3607 * inputs:
3608 * zc_name old name of dataset
3609 * zc_value new name of dataset
3610 * zc_cookie recursive flag (only valid for snapshots)
3612 * outputs: none
3614 static int
3615 zfs_ioc_rename(zfs_cmd_t *zc)
3617 boolean_t recursive = zc->zc_cookie & 1;
3618 char *at;
3620 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3621 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3622 strchr(zc->zc_value, '%'))
3623 return (SET_ERROR(EINVAL));
3625 at = strchr(zc->zc_name, '@');
3626 if (at != NULL) {
3627 /* snaps must be in same fs */
3628 int error;
3630 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3631 return (SET_ERROR(EXDEV));
3632 *at = '\0';
3633 if (zc->zc_objset_type == DMU_OST_ZFS) {
3634 error = dmu_objset_find(zc->zc_name,
3635 recursive_unmount, at + 1,
3636 recursive ? DS_FIND_CHILDREN : 0);
3637 if (error != 0) {
3638 *at = '@';
3639 return (error);
3642 error = dsl_dataset_rename_snapshot(zc->zc_name,
3643 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3644 *at = '@';
3646 return (error);
3647 } else {
3648 if (zc->zc_objset_type == DMU_OST_ZVOL)
3649 (void) zvol_remove_minor(zc->zc_name);
3650 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3654 static int
3655 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3657 const char *propname = nvpair_name(pair);
3658 boolean_t issnap = (strchr(dsname, '@') != NULL);
3659 zfs_prop_t prop = zfs_name_to_prop(propname);
3660 uint64_t intval;
3661 int err;
3663 if (prop == ZPROP_INVAL) {
3664 if (zfs_prop_user(propname)) {
3665 if (err = zfs_secpolicy_write_perms(dsname,
3666 ZFS_DELEG_PERM_USERPROP, cr))
3667 return (err);
3668 return (0);
3671 if (!issnap && zfs_prop_userquota(propname)) {
3672 const char *perm = NULL;
3673 const char *uq_prefix =
3674 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3675 const char *gq_prefix =
3676 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3678 if (strncmp(propname, uq_prefix,
3679 strlen(uq_prefix)) == 0) {
3680 perm = ZFS_DELEG_PERM_USERQUOTA;
3681 } else if (strncmp(propname, gq_prefix,
3682 strlen(gq_prefix)) == 0) {
3683 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3684 } else {
3685 /* USERUSED and GROUPUSED are read-only */
3686 return (SET_ERROR(EINVAL));
3689 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3690 return (err);
3691 return (0);
3694 return (SET_ERROR(EINVAL));
3697 if (issnap)
3698 return (SET_ERROR(EINVAL));
3700 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3702 * dsl_prop_get_all_impl() returns properties in this
3703 * format.
3705 nvlist_t *attrs;
3706 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3707 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3708 &pair) == 0);
3712 * Check that this value is valid for this pool version
3714 switch (prop) {
3715 case ZFS_PROP_COMPRESSION:
3717 * If the user specified gzip compression, make sure
3718 * the SPA supports it. We ignore any errors here since
3719 * we'll catch them later.
3721 if (nvpair_value_uint64(pair, &intval) == 0) {
3722 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3723 intval <= ZIO_COMPRESS_GZIP_9 &&
3724 zfs_earlier_version(dsname,
3725 SPA_VERSION_GZIP_COMPRESSION)) {
3726 return (SET_ERROR(ENOTSUP));
3729 if (intval == ZIO_COMPRESS_ZLE &&
3730 zfs_earlier_version(dsname,
3731 SPA_VERSION_ZLE_COMPRESSION))
3732 return (SET_ERROR(ENOTSUP));
3734 if (intval == ZIO_COMPRESS_LZ4) {
3735 spa_t *spa;
3737 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3738 return (err);
3740 if (!spa_feature_is_enabled(spa,
3741 SPA_FEATURE_LZ4_COMPRESS)) {
3742 spa_close(spa, FTAG);
3743 return (SET_ERROR(ENOTSUP));
3745 spa_close(spa, FTAG);
3749 * If this is a bootable dataset then
3750 * verify that the compression algorithm
3751 * is supported for booting. We must return
3752 * something other than ENOTSUP since it
3753 * implies a downrev pool version.
3755 if (zfs_is_bootfs(dsname) &&
3756 !BOOTFS_COMPRESS_VALID(intval)) {
3757 return (SET_ERROR(ERANGE));
3760 break;
3762 case ZFS_PROP_COPIES:
3763 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3764 return (SET_ERROR(ENOTSUP));
3765 break;
3767 case ZFS_PROP_RECORDSIZE:
3768 /* Record sizes above 128k need the feature to be enabled */
3769 if (nvpair_value_uint64(pair, &intval) == 0 &&
3770 intval > SPA_OLD_MAXBLOCKSIZE) {
3771 spa_t *spa;
3774 * We don't allow setting the property above 1MB,
3775 * unless the tunable has been changed.
3777 if (intval > zfs_max_recordsize ||
3778 intval > SPA_MAXBLOCKSIZE)
3779 return (SET_ERROR(ERANGE));
3781 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3782 return (err);
3784 if (!spa_feature_is_enabled(spa,
3785 SPA_FEATURE_LARGE_BLOCKS)) {
3786 spa_close(spa, FTAG);
3787 return (SET_ERROR(ENOTSUP));
3789 spa_close(spa, FTAG);
3791 break;
3793 case ZFS_PROP_SHARESMB:
3794 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3795 return (SET_ERROR(ENOTSUP));
3796 break;
3798 case ZFS_PROP_ACLINHERIT:
3799 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3800 nvpair_value_uint64(pair, &intval) == 0) {
3801 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3802 zfs_earlier_version(dsname,
3803 SPA_VERSION_PASSTHROUGH_X))
3804 return (SET_ERROR(ENOTSUP));
3806 break;
3808 case ZFS_PROP_CHECKSUM:
3809 case ZFS_PROP_DEDUP:
3811 spa_feature_t feature;
3812 spa_t *spa;
3814 /* dedup feature version checks */
3815 if (prop == ZFS_PROP_DEDUP &&
3816 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3817 return (SET_ERROR(ENOTSUP));
3819 if (nvpair_value_uint64(pair, &intval) != 0)
3820 return (SET_ERROR(EINVAL));
3822 /* check prop value is enabled in features */
3823 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3824 if (feature == SPA_FEATURE_NONE)
3825 break;
3827 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3828 return (err);
3830 * Salted checksums are not supported on root pools.
3832 if (spa_bootfs(spa) != 0 &&
3833 intval < ZIO_CHECKSUM_FUNCTIONS &&
3834 (zio_checksum_table[intval].ci_flags &
3835 ZCHECKSUM_FLAG_SALTED)) {
3836 spa_close(spa, FTAG);
3837 return (SET_ERROR(ERANGE));
3839 if (!spa_feature_is_enabled(spa, feature)) {
3840 spa_close(spa, FTAG);
3841 return (SET_ERROR(ENOTSUP));
3843 spa_close(spa, FTAG);
3844 break;
3848 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3852 * Checks for a race condition to make sure we don't increment a feature flag
3853 * multiple times.
3855 static int
3856 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3858 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3859 spa_feature_t *featurep = arg;
3861 if (!spa_feature_is_active(spa, *featurep))
3862 return (0);
3863 else
3864 return (SET_ERROR(EBUSY));
3868 * The callback invoked on feature activation in the sync task caused by
3869 * zfs_prop_activate_feature.
3871 static void
3872 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3874 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3875 spa_feature_t *featurep = arg;
3877 spa_feature_incr(spa, *featurep, tx);
3881 * Activates a feature on a pool in response to a property setting. This
3882 * creates a new sync task which modifies the pool to reflect the feature
3883 * as being active.
3885 static int
3886 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
3888 int err;
3890 /* EBUSY here indicates that the feature is already active */
3891 err = dsl_sync_task(spa_name(spa),
3892 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3893 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
3895 if (err != 0 && err != EBUSY)
3896 return (err);
3897 else
3898 return (0);
3902 * Removes properties from the given props list that fail permission checks
3903 * needed to clear them and to restore them in case of a receive error. For each
3904 * property, make sure we have both set and inherit permissions.
3906 * Returns the first error encountered if any permission checks fail. If the
3907 * caller provides a non-NULL errlist, it also gives the complete list of names
3908 * of all the properties that failed a permission check along with the
3909 * corresponding error numbers. The caller is responsible for freeing the
3910 * returned errlist.
3912 * If every property checks out successfully, zero is returned and the list
3913 * pointed at by errlist is NULL.
3915 static int
3916 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3918 zfs_cmd_t *zc;
3919 nvpair_t *pair, *next_pair;
3920 nvlist_t *errors;
3921 int err, rv = 0;
3923 if (props == NULL)
3924 return (0);
3926 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3928 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3929 (void) strcpy(zc->zc_name, dataset);
3930 pair = nvlist_next_nvpair(props, NULL);
3931 while (pair != NULL) {
3932 next_pair = nvlist_next_nvpair(props, pair);
3934 (void) strcpy(zc->zc_value, nvpair_name(pair));
3935 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3936 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3937 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3938 VERIFY(nvlist_add_int32(errors,
3939 zc->zc_value, err) == 0);
3941 pair = next_pair;
3943 kmem_free(zc, sizeof (zfs_cmd_t));
3945 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3946 nvlist_free(errors);
3947 errors = NULL;
3948 } else {
3949 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3952 if (errlist == NULL)
3953 nvlist_free(errors);
3954 else
3955 *errlist = errors;
3957 return (rv);
3960 static boolean_t
3961 propval_equals(nvpair_t *p1, nvpair_t *p2)
3963 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3964 /* dsl_prop_get_all_impl() format */
3965 nvlist_t *attrs;
3966 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3967 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3968 &p1) == 0);
3971 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3972 nvlist_t *attrs;
3973 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3974 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3975 &p2) == 0);
3978 if (nvpair_type(p1) != nvpair_type(p2))
3979 return (B_FALSE);
3981 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3982 char *valstr1, *valstr2;
3984 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3985 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3986 return (strcmp(valstr1, valstr2) == 0);
3987 } else {
3988 uint64_t intval1, intval2;
3990 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3991 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3992 return (intval1 == intval2);
3997 * Remove properties from props if they are not going to change (as determined
3998 * by comparison with origprops). Remove them from origprops as well, since we
3999 * do not need to clear or restore properties that won't change.
4001 static void
4002 props_reduce(nvlist_t *props, nvlist_t *origprops)
4004 nvpair_t *pair, *next_pair;
4006 if (origprops == NULL)
4007 return; /* all props need to be received */
4009 pair = nvlist_next_nvpair(props, NULL);
4010 while (pair != NULL) {
4011 const char *propname = nvpair_name(pair);
4012 nvpair_t *match;
4014 next_pair = nvlist_next_nvpair(props, pair);
4016 if ((nvlist_lookup_nvpair(origprops, propname,
4017 &match) != 0) || !propval_equals(pair, match))
4018 goto next; /* need to set received value */
4020 /* don't clear the existing received value */
4021 (void) nvlist_remove_nvpair(origprops, match);
4022 /* don't bother receiving the property */
4023 (void) nvlist_remove_nvpair(props, pair);
4024 next:
4025 pair = next_pair;
4030 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4031 * For example, refquota cannot be set until after the receipt of a dataset,
4032 * because in replication streams, an older/earlier snapshot may exceed the
4033 * refquota. We want to receive the older/earlier snapshot, but setting
4034 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4035 * the older/earlier snapshot from being received (with EDQUOT).
4037 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4039 * libzfs will need to be judicious handling errors encountered by props
4040 * extracted by this function.
4042 static nvlist_t *
4043 extract_delay_props(nvlist_t *props)
4045 nvlist_t *delayprops;
4046 nvpair_t *nvp, *tmp;
4047 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4048 int i;
4050 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4052 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4053 nvp = nvlist_next_nvpair(props, nvp)) {
4055 * strcmp() is safe because zfs_prop_to_name() always returns
4056 * a bounded string.
4058 for (i = 0; delayable[i] != 0; i++) {
4059 if (strcmp(zfs_prop_to_name(delayable[i]),
4060 nvpair_name(nvp)) == 0) {
4061 break;
4064 if (delayable[i] != 0) {
4065 tmp = nvlist_prev_nvpair(props, nvp);
4066 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4067 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4068 nvp = tmp;
4072 if (nvlist_empty(delayprops)) {
4073 nvlist_free(delayprops);
4074 delayprops = NULL;
4076 return (delayprops);
4079 #ifdef DEBUG
4080 static boolean_t zfs_ioc_recv_inject_err;
4081 #endif
4084 * inputs:
4085 * zc_name name of containing filesystem
4086 * zc_nvlist_src{_size} nvlist of properties to apply
4087 * zc_value name of snapshot to create
4088 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4089 * zc_cookie file descriptor to recv from
4090 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4091 * zc_guid force flag
4092 * zc_cleanup_fd cleanup-on-exit file descriptor
4093 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4094 * zc_resumable if data is incomplete assume sender will resume
4096 * outputs:
4097 * zc_cookie number of bytes read
4098 * zc_nvlist_dst{_size} error for each unapplied received property
4099 * zc_obj zprop_errflags_t
4100 * zc_action_handle handle for this guid/ds mapping
4102 static int
4103 zfs_ioc_recv(zfs_cmd_t *zc)
4105 file_t *fp;
4106 dmu_recv_cookie_t drc;
4107 boolean_t force = (boolean_t)zc->zc_guid;
4108 int fd;
4109 int error = 0;
4110 int props_error = 0;
4111 nvlist_t *errors;
4112 offset_t off;
4113 nvlist_t *props = NULL; /* sent properties */
4114 nvlist_t *origprops = NULL; /* existing properties */
4115 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4116 char *origin = NULL;
4117 char *tosnap;
4118 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4119 boolean_t first_recvd_props = B_FALSE;
4121 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4122 strchr(zc->zc_value, '@') == NULL ||
4123 strchr(zc->zc_value, '%'))
4124 return (SET_ERROR(EINVAL));
4126 (void) strcpy(tofs, zc->zc_value);
4127 tosnap = strchr(tofs, '@');
4128 *tosnap++ = '\0';
4130 if (zc->zc_nvlist_src != (uintptr_t)NULL &&
4131 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4132 zc->zc_iflags, &props)) != 0)
4133 return (error);
4135 fd = zc->zc_cookie;
4136 fp = getf(fd);
4137 if (fp == NULL) {
4138 nvlist_free(props);
4139 return (SET_ERROR(EBADF));
4142 errors = fnvlist_alloc();
4144 if (zc->zc_string[0])
4145 origin = zc->zc_string;
4147 error = dmu_recv_begin(tofs, tosnap,
4148 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4149 if (error != 0)
4150 goto out;
4153 * Set properties before we receive the stream so that they are applied
4154 * to the new data. Note that we must call dmu_recv_stream() if
4155 * dmu_recv_begin() succeeds.
4157 if (props != NULL && !drc.drc_newfs) {
4158 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4159 SPA_VERSION_RECVD_PROPS &&
4160 !dsl_prop_get_hasrecvd(tofs))
4161 first_recvd_props = B_TRUE;
4164 * If new received properties are supplied, they are to
4165 * completely replace the existing received properties, so stash
4166 * away the existing ones.
4168 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4169 nvlist_t *errlist = NULL;
4171 * Don't bother writing a property if its value won't
4172 * change (and avoid the unnecessary security checks).
4174 * The first receive after SPA_VERSION_RECVD_PROPS is a
4175 * special case where we blow away all local properties
4176 * regardless.
4178 if (!first_recvd_props)
4179 props_reduce(props, origprops);
4180 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4181 (void) nvlist_merge(errors, errlist, 0);
4182 nvlist_free(errlist);
4184 if (clear_received_props(tofs, origprops,
4185 first_recvd_props ? NULL : props) != 0)
4186 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4187 } else {
4188 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4192 if (props != NULL) {
4193 props_error = dsl_prop_set_hasrecvd(tofs);
4195 if (props_error == 0) {
4196 delayprops = extract_delay_props(props);
4197 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4198 props, errors);
4202 off = fp->f_offset;
4203 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4204 &zc->zc_action_handle);
4206 if (error == 0) {
4207 zfsvfs_t *zfsvfs = NULL;
4209 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4210 /* online recv */
4211 dsl_dataset_t *ds;
4212 int end_err;
4214 ds = dmu_objset_ds(zfsvfs->z_os);
4215 error = zfs_suspend_fs(zfsvfs);
4217 * If the suspend fails, then the recv_end will
4218 * likely also fail, and clean up after itself.
4220 end_err = dmu_recv_end(&drc, zfsvfs);
4221 if (error == 0)
4222 error = zfs_resume_fs(zfsvfs, ds);
4223 error = error ? error : end_err;
4224 VFS_RELE(zfsvfs->z_vfs);
4225 } else {
4226 error = dmu_recv_end(&drc, NULL);
4229 /* Set delayed properties now, after we're done receiving. */
4230 if (delayprops != NULL && error == 0) {
4231 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4232 delayprops, errors);
4236 if (delayprops != NULL) {
4238 * Merge delayed props back in with initial props, in case
4239 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4240 * we have to make sure clear_received_props() includes
4241 * the delayed properties).
4243 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4244 * using ASSERT() will be just like a VERIFY.
4246 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4247 nvlist_free(delayprops);
4251 * Now that all props, initial and delayed, are set, report the prop
4252 * errors to the caller.
4254 if (zc->zc_nvlist_dst_size != 0 &&
4255 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4256 put_nvlist(zc, errors) != 0)) {
4258 * Caller made zc->zc_nvlist_dst less than the minimum expected
4259 * size or supplied an invalid address.
4261 props_error = SET_ERROR(EINVAL);
4264 zc->zc_cookie = off - fp->f_offset;
4265 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4266 fp->f_offset = off;
4268 #ifdef DEBUG
4269 if (zfs_ioc_recv_inject_err) {
4270 zfs_ioc_recv_inject_err = B_FALSE;
4271 error = 1;
4273 #endif
4275 * On error, restore the original props.
4277 if (error != 0 && props != NULL && !drc.drc_newfs) {
4278 if (clear_received_props(tofs, props, NULL) != 0) {
4280 * We failed to clear the received properties.
4281 * Since we may have left a $recvd value on the
4282 * system, we can't clear the $hasrecvd flag.
4284 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4285 } else if (first_recvd_props) {
4286 dsl_prop_unset_hasrecvd(tofs);
4289 if (origprops == NULL && !drc.drc_newfs) {
4290 /* We failed to stash the original properties. */
4291 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4295 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4296 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4297 * explictly if we're restoring local properties cleared in the
4298 * first new-style receive.
4300 if (origprops != NULL &&
4301 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4302 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4303 origprops, NULL) != 0) {
4305 * We stashed the original properties but failed to
4306 * restore them.
4308 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4311 out:
4312 nvlist_free(props);
4313 nvlist_free(origprops);
4314 nvlist_free(errors);
4315 releasef(fd);
4317 if (error == 0)
4318 error = props_error;
4320 return (error);
4324 * inputs:
4325 * zc_name name of snapshot to send
4326 * zc_cookie file descriptor to send stream to
4327 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4328 * zc_sendobj objsetid of snapshot to send
4329 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4330 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4331 * output size in zc_objset_type.
4332 * zc_flags lzc_send_flags
4334 * outputs:
4335 * zc_objset_type estimated size, if zc_guid is set
4337 static int
4338 zfs_ioc_send(zfs_cmd_t *zc)
4340 int error;
4341 offset_t off;
4342 boolean_t estimate = (zc->zc_guid != 0);
4343 boolean_t embedok = (zc->zc_flags & 0x1);
4344 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4345 boolean_t compressok = (zc->zc_flags & 0x4);
4347 if (zc->zc_obj != 0) {
4348 dsl_pool_t *dp;
4349 dsl_dataset_t *tosnap;
4351 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4352 if (error != 0)
4353 return (error);
4355 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4356 if (error != 0) {
4357 dsl_pool_rele(dp, FTAG);
4358 return (error);
4361 if (dsl_dir_is_clone(tosnap->ds_dir))
4362 zc->zc_fromobj =
4363 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4364 dsl_dataset_rele(tosnap, FTAG);
4365 dsl_pool_rele(dp, FTAG);
4368 if (estimate) {
4369 dsl_pool_t *dp;
4370 dsl_dataset_t *tosnap;
4371 dsl_dataset_t *fromsnap = NULL;
4373 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4374 if (error != 0)
4375 return (error);
4377 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4378 if (error != 0) {
4379 dsl_pool_rele(dp, FTAG);
4380 return (error);
4383 if (zc->zc_fromobj != 0) {
4384 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4385 FTAG, &fromsnap);
4386 if (error != 0) {
4387 dsl_dataset_rele(tosnap, FTAG);
4388 dsl_pool_rele(dp, FTAG);
4389 return (error);
4393 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4394 &zc->zc_objset_type);
4396 if (fromsnap != NULL)
4397 dsl_dataset_rele(fromsnap, FTAG);
4398 dsl_dataset_rele(tosnap, FTAG);
4399 dsl_pool_rele(dp, FTAG);
4400 } else {
4401 file_t *fp = getf(zc->zc_cookie);
4402 if (fp == NULL)
4403 return (SET_ERROR(EBADF));
4405 off = fp->f_offset;
4406 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4407 zc->zc_fromobj, embedok, large_block_ok, compressok,
4408 zc->zc_cookie, fp->f_vnode, &off);
4410 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4411 fp->f_offset = off;
4412 releasef(zc->zc_cookie);
4414 return (error);
4418 * inputs:
4419 * zc_name name of snapshot on which to report progress
4420 * zc_cookie file descriptor of send stream
4422 * outputs:
4423 * zc_cookie number of bytes written in send stream thus far
4425 static int
4426 zfs_ioc_send_progress(zfs_cmd_t *zc)
4428 dsl_pool_t *dp;
4429 dsl_dataset_t *ds;
4430 dmu_sendarg_t *dsp = NULL;
4431 int error;
4433 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4434 if (error != 0)
4435 return (error);
4437 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4438 if (error != 0) {
4439 dsl_pool_rele(dp, FTAG);
4440 return (error);
4443 mutex_enter(&ds->ds_sendstream_lock);
4446 * Iterate over all the send streams currently active on this dataset.
4447 * If there's one which matches the specified file descriptor _and_ the
4448 * stream was started by the current process, return the progress of
4449 * that stream.
4451 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4452 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4453 if (dsp->dsa_outfd == zc->zc_cookie &&
4454 dsp->dsa_proc == curproc)
4455 break;
4458 if (dsp != NULL)
4459 zc->zc_cookie = *(dsp->dsa_off);
4460 else
4461 error = SET_ERROR(ENOENT);
4463 mutex_exit(&ds->ds_sendstream_lock);
4464 dsl_dataset_rele(ds, FTAG);
4465 dsl_pool_rele(dp, FTAG);
4466 return (error);
4469 static int
4470 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4472 int id, error;
4474 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4475 &zc->zc_inject_record);
4477 if (error == 0)
4478 zc->zc_guid = (uint64_t)id;
4480 return (error);
4483 static int
4484 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4486 return (zio_clear_fault((int)zc->zc_guid));
4489 static int
4490 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4492 int id = (int)zc->zc_guid;
4493 int error;
4495 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4496 &zc->zc_inject_record);
4498 zc->zc_guid = id;
4500 return (error);
4503 static int
4504 zfs_ioc_error_log(zfs_cmd_t *zc)
4506 spa_t *spa;
4507 int error;
4508 size_t count = (size_t)zc->zc_nvlist_dst_size;
4510 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4511 return (error);
4513 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4514 &count);
4515 if (error == 0)
4516 zc->zc_nvlist_dst_size = count;
4517 else
4518 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4520 spa_close(spa, FTAG);
4522 return (error);
4525 static int
4526 zfs_ioc_clear(zfs_cmd_t *zc)
4528 spa_t *spa;
4529 vdev_t *vd;
4530 int error;
4533 * On zpool clear we also fix up missing slogs
4535 mutex_enter(&spa_namespace_lock);
4536 spa = spa_lookup(zc->zc_name);
4537 if (spa == NULL) {
4538 mutex_exit(&spa_namespace_lock);
4539 return (SET_ERROR(EIO));
4541 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4542 /* we need to let spa_open/spa_load clear the chains */
4543 spa_set_log_state(spa, SPA_LOG_CLEAR);
4545 spa->spa_last_open_failed = 0;
4546 mutex_exit(&spa_namespace_lock);
4548 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4549 error = spa_open(zc->zc_name, &spa, FTAG);
4550 } else {
4551 nvlist_t *policy;
4552 nvlist_t *config = NULL;
4554 if (zc->zc_nvlist_src == (uintptr_t)NULL)
4555 return (SET_ERROR(EINVAL));
4557 if ((error = get_nvlist(zc->zc_nvlist_src,
4558 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4559 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4560 policy, &config);
4561 if (config != NULL) {
4562 int err;
4564 if ((err = put_nvlist(zc, config)) != 0)
4565 error = err;
4566 nvlist_free(config);
4568 nvlist_free(policy);
4572 if (error != 0)
4573 return (error);
4575 spa_vdev_state_enter(spa, SCL_NONE);
4577 if (zc->zc_guid == 0) {
4578 vd = NULL;
4579 } else {
4580 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4581 if (vd == NULL) {
4582 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4583 spa_close(spa, FTAG);
4584 return (SET_ERROR(ENODEV));
4588 vdev_clear(spa, vd);
4590 (void) spa_vdev_state_exit(spa, NULL, 0);
4593 * Resume any suspended I/Os.
4595 if (zio_resume(spa) != 0)
4596 error = SET_ERROR(EIO);
4598 spa_close(spa, FTAG);
4600 return (error);
4603 static int
4604 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4606 spa_t *spa;
4607 int error;
4609 error = spa_open(zc->zc_name, &spa, FTAG);
4610 if (error != 0)
4611 return (error);
4613 spa_vdev_state_enter(spa, SCL_NONE);
4616 * If a resilver is already in progress then set the
4617 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4618 * the scan as a side effect of the reopen. Otherwise, let
4619 * vdev_open() decided if a resilver is required.
4621 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4622 vdev_reopen(spa->spa_root_vdev);
4623 spa->spa_scrub_reopen = B_FALSE;
4625 (void) spa_vdev_state_exit(spa, NULL, 0);
4626 spa_close(spa, FTAG);
4627 return (0);
4630 * inputs:
4631 * zc_name name of filesystem
4633 * outputs:
4634 * zc_string name of conflicting snapshot, if there is one
4636 static int
4637 zfs_ioc_promote(zfs_cmd_t *zc)
4639 dsl_pool_t *dp;
4640 dsl_dataset_t *ds, *ods;
4641 char origin[ZFS_MAX_DATASET_NAME_LEN];
4642 char *cp;
4643 int error;
4645 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4646 if (error != 0)
4647 return (error);
4649 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4650 if (error != 0) {
4651 dsl_pool_rele(dp, FTAG);
4652 return (error);
4655 if (!dsl_dir_is_clone(ds->ds_dir)) {
4656 dsl_dataset_rele(ds, FTAG);
4657 dsl_pool_rele(dp, FTAG);
4658 return (SET_ERROR(EINVAL));
4661 error = dsl_dataset_hold_obj(dp,
4662 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4663 if (error != 0) {
4664 dsl_dataset_rele(ds, FTAG);
4665 dsl_pool_rele(dp, FTAG);
4666 return (error);
4669 dsl_dataset_name(ods, origin);
4670 dsl_dataset_rele(ods, FTAG);
4671 dsl_dataset_rele(ds, FTAG);
4672 dsl_pool_rele(dp, FTAG);
4675 * We don't need to unmount *all* the origin fs's snapshots, but
4676 * it's easier.
4678 cp = strchr(origin, '@');
4679 if (cp)
4680 *cp = '\0';
4681 (void) dmu_objset_find(origin,
4682 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4683 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4687 * Retrieve a single {user|group}{used|quota}@... property.
4689 * inputs:
4690 * zc_name name of filesystem
4691 * zc_objset_type zfs_userquota_prop_t
4692 * zc_value domain name (eg. "S-1-234-567-89")
4693 * zc_guid RID/UID/GID
4695 * outputs:
4696 * zc_cookie property value
4698 static int
4699 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4701 zfsvfs_t *zfsvfs;
4702 int error;
4704 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4705 return (SET_ERROR(EINVAL));
4707 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4708 if (error != 0)
4709 return (error);
4711 error = zfs_userspace_one(zfsvfs,
4712 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4713 zfsvfs_rele(zfsvfs, FTAG);
4715 return (error);
4719 * inputs:
4720 * zc_name name of filesystem
4721 * zc_cookie zap cursor
4722 * zc_objset_type zfs_userquota_prop_t
4723 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4725 * outputs:
4726 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4727 * zc_cookie zap cursor
4729 static int
4730 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4732 zfsvfs_t *zfsvfs;
4733 int bufsize = zc->zc_nvlist_dst_size;
4735 if (bufsize <= 0)
4736 return (SET_ERROR(ENOMEM));
4738 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4739 if (error != 0)
4740 return (error);
4742 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4744 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4745 buf, &zc->zc_nvlist_dst_size);
4747 if (error == 0) {
4748 error = xcopyout(buf,
4749 (void *)(uintptr_t)zc->zc_nvlist_dst,
4750 zc->zc_nvlist_dst_size);
4752 kmem_free(buf, bufsize);
4753 zfsvfs_rele(zfsvfs, FTAG);
4755 return (error);
4759 * inputs:
4760 * zc_name name of filesystem
4762 * outputs:
4763 * none
4765 static int
4766 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4768 objset_t *os;
4769 int error = 0;
4770 zfsvfs_t *zfsvfs;
4772 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4773 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4775 * If userused is not enabled, it may be because the
4776 * objset needs to be closed & reopened (to grow the
4777 * objset_phys_t). Suspend/resume the fs will do that.
4779 dsl_dataset_t *ds;
4781 ds = dmu_objset_ds(zfsvfs->z_os);
4782 error = zfs_suspend_fs(zfsvfs);
4783 if (error == 0) {
4784 dmu_objset_refresh_ownership(zfsvfs->z_os,
4785 zfsvfs);
4786 error = zfs_resume_fs(zfsvfs, ds);
4789 if (error == 0)
4790 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4791 VFS_RELE(zfsvfs->z_vfs);
4792 } else {
4793 /* XXX kind of reading contents without owning */
4794 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4795 if (error != 0)
4796 return (error);
4798 error = dmu_objset_userspace_upgrade(os);
4799 dmu_objset_rele(os, FTAG);
4802 return (error);
4806 * We don't want to have a hard dependency
4807 * against some special symbols in sharefs
4808 * nfs, and smbsrv. Determine them if needed when
4809 * the first file system is shared.
4810 * Neither sharefs, nfs or smbsrv are unloadable modules.
4812 int (*znfsexport_fs)(void *arg);
4813 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4814 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4816 int zfs_nfsshare_inited;
4817 int zfs_smbshare_inited;
4819 ddi_modhandle_t nfs_mod;
4820 ddi_modhandle_t sharefs_mod;
4821 ddi_modhandle_t smbsrv_mod;
4822 kmutex_t zfs_share_lock;
4824 static int
4825 zfs_init_sharefs()
4827 int error;
4829 ASSERT(MUTEX_HELD(&zfs_share_lock));
4830 /* Both NFS and SMB shares also require sharetab support. */
4831 if (sharefs_mod == NULL && ((sharefs_mod =
4832 ddi_modopen("fs/sharefs",
4833 KRTLD_MODE_FIRST, &error)) == NULL)) {
4834 return (SET_ERROR(ENOSYS));
4836 if (zshare_fs == NULL && ((zshare_fs =
4837 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4838 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4839 return (SET_ERROR(ENOSYS));
4841 return (0);
4844 static int
4845 zfs_ioc_share(zfs_cmd_t *zc)
4847 int error;
4848 int opcode;
4850 switch (zc->zc_share.z_sharetype) {
4851 case ZFS_SHARE_NFS:
4852 case ZFS_UNSHARE_NFS:
4853 if (zfs_nfsshare_inited == 0) {
4854 mutex_enter(&zfs_share_lock);
4855 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4856 KRTLD_MODE_FIRST, &error)) == NULL)) {
4857 mutex_exit(&zfs_share_lock);
4858 return (SET_ERROR(ENOSYS));
4860 if (znfsexport_fs == NULL &&
4861 ((znfsexport_fs = (int (*)(void *))
4862 ddi_modsym(nfs_mod,
4863 "nfs_export", &error)) == NULL)) {
4864 mutex_exit(&zfs_share_lock);
4865 return (SET_ERROR(ENOSYS));
4867 error = zfs_init_sharefs();
4868 if (error != 0) {
4869 mutex_exit(&zfs_share_lock);
4870 return (SET_ERROR(ENOSYS));
4872 zfs_nfsshare_inited = 1;
4873 mutex_exit(&zfs_share_lock);
4875 break;
4876 case ZFS_SHARE_SMB:
4877 case ZFS_UNSHARE_SMB:
4878 if (zfs_smbshare_inited == 0) {
4879 mutex_enter(&zfs_share_lock);
4880 if (smbsrv_mod == NULL && ((smbsrv_mod =
4881 ddi_modopen("drv/smbsrv",
4882 KRTLD_MODE_FIRST, &error)) == NULL)) {
4883 mutex_exit(&zfs_share_lock);
4884 return (SET_ERROR(ENOSYS));
4886 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4887 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4888 "smb_server_share", &error)) == NULL)) {
4889 mutex_exit(&zfs_share_lock);
4890 return (SET_ERROR(ENOSYS));
4892 error = zfs_init_sharefs();
4893 if (error != 0) {
4894 mutex_exit(&zfs_share_lock);
4895 return (SET_ERROR(ENOSYS));
4897 zfs_smbshare_inited = 1;
4898 mutex_exit(&zfs_share_lock);
4900 break;
4901 default:
4902 return (SET_ERROR(EINVAL));
4905 switch (zc->zc_share.z_sharetype) {
4906 case ZFS_SHARE_NFS:
4907 case ZFS_UNSHARE_NFS:
4908 if (error =
4909 znfsexport_fs((void *)
4910 (uintptr_t)zc->zc_share.z_exportdata))
4911 return (error);
4912 break;
4913 case ZFS_SHARE_SMB:
4914 case ZFS_UNSHARE_SMB:
4915 if (error = zsmbexport_fs((void *)
4916 (uintptr_t)zc->zc_share.z_exportdata,
4917 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4918 B_TRUE: B_FALSE)) {
4919 return (error);
4921 break;
4924 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4925 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4926 SHAREFS_ADD : SHAREFS_REMOVE;
4929 * Add or remove share from sharetab
4931 error = zshare_fs(opcode,
4932 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4933 zc->zc_share.z_sharemax);
4935 return (error);
4939 ace_t full_access[] = {
4940 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4944 * inputs:
4945 * zc_name name of containing filesystem
4946 * zc_obj object # beyond which we want next in-use object #
4948 * outputs:
4949 * zc_obj next in-use object #
4951 static int
4952 zfs_ioc_next_obj(zfs_cmd_t *zc)
4954 objset_t *os = NULL;
4955 int error;
4957 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4958 if (error != 0)
4959 return (error);
4961 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4962 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
4964 dmu_objset_rele(os, FTAG);
4965 return (error);
4969 * inputs:
4970 * zc_name name of filesystem
4971 * zc_value prefix name for snapshot
4972 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4974 * outputs:
4975 * zc_value short name of new snapshot
4977 static int
4978 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4980 char *snap_name;
4981 char *hold_name;
4982 int error;
4983 minor_t minor;
4985 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4986 if (error != 0)
4987 return (error);
4989 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4990 (u_longlong_t)ddi_get_lbolt64());
4991 hold_name = kmem_asprintf("%%%s", zc->zc_value);
4993 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
4994 hold_name);
4995 if (error == 0)
4996 (void) strcpy(zc->zc_value, snap_name);
4997 strfree(snap_name);
4998 strfree(hold_name);
4999 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5000 return (error);
5004 * inputs:
5005 * zc_name name of "to" snapshot
5006 * zc_value name of "from" snapshot
5007 * zc_cookie file descriptor to write diff data on
5009 * outputs:
5010 * dmu_diff_record_t's to the file descriptor
5012 static int
5013 zfs_ioc_diff(zfs_cmd_t *zc)
5015 file_t *fp;
5016 offset_t off;
5017 int error;
5019 fp = getf(zc->zc_cookie);
5020 if (fp == NULL)
5021 return (SET_ERROR(EBADF));
5023 off = fp->f_offset;
5025 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5027 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5028 fp->f_offset = off;
5029 releasef(zc->zc_cookie);
5031 return (error);
5035 * Remove all ACL files in shares dir
5037 static int
5038 zfs_smb_acl_purge(znode_t *dzp)
5040 zap_cursor_t zc;
5041 zap_attribute_t zap;
5042 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5043 int error;
5045 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5046 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5047 zap_cursor_advance(&zc)) {
5048 if ((error = fop_remove(ZTOV(dzp), zap.za_name, kcred,
5049 NULL, 0)) != 0)
5050 break;
5052 zap_cursor_fini(&zc);
5053 return (error);
5056 static int
5057 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5059 vnode_t *vp;
5060 znode_t *dzp;
5061 vnode_t *resourcevp = NULL;
5062 znode_t *sharedir;
5063 zfsvfs_t *zfsvfs;
5064 nvlist_t *nvlist;
5065 char *src, *target;
5066 vattr_t vattr;
5067 vsecattr_t vsec;
5068 int error = 0;
5070 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5071 NO_FOLLOW, NULL, &vp)) != 0)
5072 return (error);
5074 /* Now make sure mntpnt and dataset are ZFS */
5076 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5077 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5078 zc->zc_name) != 0)) {
5079 VN_RELE(vp);
5080 return (SET_ERROR(EINVAL));
5083 dzp = VTOZ(vp);
5084 zfsvfs = dzp->z_zfsvfs;
5085 ZFS_ENTER(zfsvfs);
5088 * Create share dir if its missing.
5090 mutex_enter(&zfsvfs->z_lock);
5091 if (zfsvfs->z_shares_dir == 0) {
5092 dmu_tx_t *tx;
5094 tx = dmu_tx_create(zfsvfs->z_os);
5095 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5096 ZFS_SHARES_DIR);
5097 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5098 error = dmu_tx_assign(tx, TXG_WAIT);
5099 if (error != 0) {
5100 dmu_tx_abort(tx);
5101 } else {
5102 error = zfs_create_share_dir(zfsvfs, tx);
5103 dmu_tx_commit(tx);
5105 if (error != 0) {
5106 mutex_exit(&zfsvfs->z_lock);
5107 VN_RELE(vp);
5108 ZFS_EXIT(zfsvfs);
5109 return (error);
5112 mutex_exit(&zfsvfs->z_lock);
5114 ASSERT(zfsvfs->z_shares_dir);
5115 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5116 VN_RELE(vp);
5117 ZFS_EXIT(zfsvfs);
5118 return (error);
5121 switch (zc->zc_cookie) {
5122 case ZFS_SMB_ACL_ADD:
5123 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5124 vattr.va_type = VREG;
5125 vattr.va_mode = S_IFREG|0777;
5126 vattr.va_uid = 0;
5127 vattr.va_gid = 0;
5129 vsec.vsa_mask = VSA_ACE;
5130 vsec.vsa_aclentp = &full_access;
5131 vsec.vsa_aclentsz = sizeof (full_access);
5132 vsec.vsa_aclcnt = 1;
5134 error = fop_create(ZTOV(sharedir), zc->zc_string,
5135 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5136 if (resourcevp)
5137 VN_RELE(resourcevp);
5138 break;
5140 case ZFS_SMB_ACL_REMOVE:
5141 error = fop_remove(ZTOV(sharedir), zc->zc_string, kcred,
5142 NULL, 0);
5143 break;
5145 case ZFS_SMB_ACL_RENAME:
5146 if ((error = get_nvlist(zc->zc_nvlist_src,
5147 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5148 VN_RELE(vp);
5149 VN_RELE(ZTOV(sharedir));
5150 ZFS_EXIT(zfsvfs);
5151 return (error);
5153 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5154 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5155 &target)) {
5156 VN_RELE(vp);
5157 VN_RELE(ZTOV(sharedir));
5158 ZFS_EXIT(zfsvfs);
5159 nvlist_free(nvlist);
5160 return (error);
5162 error = fop_rename(ZTOV(sharedir), src, ZTOV(sharedir), target,
5163 kcred, NULL, 0);
5164 nvlist_free(nvlist);
5165 break;
5167 case ZFS_SMB_ACL_PURGE:
5168 error = zfs_smb_acl_purge(sharedir);
5169 break;
5171 default:
5172 error = SET_ERROR(EINVAL);
5173 break;
5176 VN_RELE(vp);
5177 VN_RELE(ZTOV(sharedir));
5179 ZFS_EXIT(zfsvfs);
5181 return (error);
5185 * innvl: {
5186 * "holds" -> { snapname -> holdname (string), ... }
5187 * (optional) "cleanup_fd" -> fd (int32)
5190 * outnvl: {
5191 * snapname -> error value (int32)
5192 * ...
5195 /* ARGSUSED */
5196 static int
5197 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5199 nvpair_t *pair;
5200 nvlist_t *holds;
5201 int cleanup_fd = -1;
5202 int error;
5203 minor_t minor = 0;
5205 error = nvlist_lookup_nvlist(args, "holds", &holds);
5206 if (error != 0)
5207 return (SET_ERROR(EINVAL));
5209 /* make sure the user didn't pass us any invalid (empty) tags */
5210 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5211 pair = nvlist_next_nvpair(holds, pair)) {
5212 char *htag;
5214 error = nvpair_value_string(pair, &htag);
5215 if (error != 0)
5216 return (SET_ERROR(error));
5218 if (strlen(htag) == 0)
5219 return (SET_ERROR(EINVAL));
5222 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5223 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5224 if (error != 0)
5225 return (error);
5228 error = dsl_dataset_user_hold(holds, minor, errlist);
5229 if (minor != 0)
5230 zfs_onexit_fd_rele(cleanup_fd);
5231 return (error);
5235 * innvl is not used.
5237 * outnvl: {
5238 * holdname -> time added (uint64 seconds since epoch)
5239 * ...
5242 /* ARGSUSED */
5243 static int
5244 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5246 return (dsl_dataset_get_holds(snapname, outnvl));
5250 * innvl: {
5251 * snapname -> { holdname, ... }
5252 * ...
5255 * outnvl: {
5256 * snapname -> error value (int32)
5257 * ...
5260 /* ARGSUSED */
5261 static int
5262 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5264 return (dsl_dataset_user_release(holds, errlist));
5268 * inputs:
5269 * zc_name name of new filesystem or snapshot
5270 * zc_value full name of old snapshot
5272 * outputs:
5273 * zc_cookie space in bytes
5274 * zc_objset_type compressed space in bytes
5275 * zc_perm_action uncompressed space in bytes
5277 static int
5278 zfs_ioc_space_written(zfs_cmd_t *zc)
5280 int error;
5281 dsl_pool_t *dp;
5282 dsl_dataset_t *new, *old;
5284 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5285 if (error != 0)
5286 return (error);
5287 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5288 if (error != 0) {
5289 dsl_pool_rele(dp, FTAG);
5290 return (error);
5292 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5293 if (error != 0) {
5294 dsl_dataset_rele(new, FTAG);
5295 dsl_pool_rele(dp, FTAG);
5296 return (error);
5299 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5300 &zc->zc_objset_type, &zc->zc_perm_action);
5301 dsl_dataset_rele(old, FTAG);
5302 dsl_dataset_rele(new, FTAG);
5303 dsl_pool_rele(dp, FTAG);
5304 return (error);
5308 * innvl: {
5309 * "firstsnap" -> snapshot name
5312 * outnvl: {
5313 * "used" -> space in bytes
5314 * "compressed" -> compressed space in bytes
5315 * "uncompressed" -> uncompressed space in bytes
5318 static int
5319 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5321 int error;
5322 dsl_pool_t *dp;
5323 dsl_dataset_t *new, *old;
5324 char *firstsnap;
5325 uint64_t used, comp, uncomp;
5327 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5328 return (SET_ERROR(EINVAL));
5330 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5331 if (error != 0)
5332 return (error);
5334 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5335 if (error == 0 && !new->ds_is_snapshot) {
5336 dsl_dataset_rele(new, FTAG);
5337 error = SET_ERROR(EINVAL);
5339 if (error != 0) {
5340 dsl_pool_rele(dp, FTAG);
5341 return (error);
5343 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5344 if (error == 0 && !old->ds_is_snapshot) {
5345 dsl_dataset_rele(old, FTAG);
5346 error = SET_ERROR(EINVAL);
5348 if (error != 0) {
5349 dsl_dataset_rele(new, FTAG);
5350 dsl_pool_rele(dp, FTAG);
5351 return (error);
5354 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5355 dsl_dataset_rele(old, FTAG);
5356 dsl_dataset_rele(new, FTAG);
5357 dsl_pool_rele(dp, FTAG);
5358 fnvlist_add_uint64(outnvl, "used", used);
5359 fnvlist_add_uint64(outnvl, "compressed", comp);
5360 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5361 return (error);
5365 * innvl: {
5366 * "fd" -> file descriptor to write stream to (int32)
5367 * (optional) "fromsnap" -> full snap name to send an incremental from
5368 * (optional) "largeblockok" -> (value ignored)
5369 * indicates that blocks > 128KB are permitted
5370 * (optional) "embedok" -> (value ignored)
5371 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5372 * (optional) "compressok" -> (value ignored)
5373 * presence indicates compressed DRR_WRITE records are permitted
5374 * (optional) "resume_object" and "resume_offset" -> (uint64)
5375 * if present, resume send stream from specified object and offset.
5378 * outnvl is unused
5380 /* ARGSUSED */
5381 static int
5382 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5384 int error;
5385 offset_t off;
5386 char *fromname = NULL;
5387 int fd;
5388 boolean_t largeblockok;
5389 boolean_t embedok;
5390 boolean_t compressok;
5391 uint64_t resumeobj = 0;
5392 uint64_t resumeoff = 0;
5394 error = nvlist_lookup_int32(innvl, "fd", &fd);
5395 if (error != 0)
5396 return (SET_ERROR(EINVAL));
5398 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5400 largeblockok = nvlist_exists(innvl, "largeblockok");
5401 embedok = nvlist_exists(innvl, "embedok");
5402 compressok = nvlist_exists(innvl, "compressok");
5404 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5405 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5407 file_t *fp = getf(fd);
5408 if (fp == NULL)
5409 return (SET_ERROR(EBADF));
5411 off = fp->f_offset;
5412 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5413 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5415 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5416 fp->f_offset = off;
5417 releasef(fd);
5418 return (error);
5422 * Determine approximately how large a zfs send stream will be -- the number
5423 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5425 * innvl: {
5426 * (optional) "from" -> full snap or bookmark name to send an incremental
5427 * from
5428 * (optional) "largeblockok" -> (value ignored)
5429 * indicates that blocks > 128KB are permitted
5430 * (optional) "embedok" -> (value ignored)
5431 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5432 * (optional) "compressok" -> (value ignored)
5433 * presence indicates compressed DRR_WRITE records are permitted
5436 * outnvl: {
5437 * "space" -> bytes of space (uint64)
5440 static int
5441 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5443 dsl_pool_t *dp;
5444 dsl_dataset_t *tosnap;
5445 int error;
5446 char *fromname;
5447 boolean_t compressok;
5448 uint64_t space;
5450 error = dsl_pool_hold(snapname, FTAG, &dp);
5451 if (error != 0)
5452 return (error);
5454 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5455 if (error != 0) {
5456 dsl_pool_rele(dp, FTAG);
5457 return (error);
5460 compressok = nvlist_exists(innvl, "compressok");
5462 error = nvlist_lookup_string(innvl, "from", &fromname);
5463 if (error == 0) {
5464 if (strchr(fromname, '@') != NULL) {
5466 * If from is a snapshot, hold it and use the more
5467 * efficient dmu_send_estimate to estimate send space
5468 * size using deadlists.
5470 dsl_dataset_t *fromsnap;
5471 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5472 if (error != 0)
5473 goto out;
5474 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5475 &space);
5476 dsl_dataset_rele(fromsnap, FTAG);
5477 } else if (strchr(fromname, '#') != NULL) {
5479 * If from is a bookmark, fetch the creation TXG of the
5480 * snapshot it was created from and use that to find
5481 * blocks that were born after it.
5483 zfs_bookmark_phys_t frombm;
5485 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5486 &frombm);
5487 if (error != 0)
5488 goto out;
5489 error = dmu_send_estimate_from_txg(tosnap,
5490 frombm.zbm_creation_txg, compressok, &space);
5491 } else {
5493 * from is not properly formatted as a snapshot or
5494 * bookmark
5496 error = SET_ERROR(EINVAL);
5497 goto out;
5499 } else {
5501 * If estimating the size of a full send, use dmu_send_estimate.
5503 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5506 fnvlist_add_uint64(outnvl, "space", space);
5508 out:
5509 dsl_dataset_rele(tosnap, FTAG);
5510 dsl_pool_rele(dp, FTAG);
5511 return (error);
5514 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5516 static void
5517 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5518 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5519 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5521 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5523 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5524 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5525 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5526 ASSERT3P(vec->zvec_func, ==, NULL);
5528 vec->zvec_legacy_func = func;
5529 vec->zvec_secpolicy = secpolicy;
5530 vec->zvec_namecheck = namecheck;
5531 vec->zvec_allow_log = log_history;
5532 vec->zvec_pool_check = pool_check;
5536 * See the block comment at the beginning of this file for details on
5537 * each argument to this function.
5539 static void
5540 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5541 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5542 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5543 boolean_t allow_log)
5545 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5547 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5548 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5549 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5550 ASSERT3P(vec->zvec_func, ==, NULL);
5552 /* if we are logging, the name must be valid */
5553 ASSERT(!allow_log || namecheck != NO_NAME);
5555 vec->zvec_name = name;
5556 vec->zvec_func = func;
5557 vec->zvec_secpolicy = secpolicy;
5558 vec->zvec_namecheck = namecheck;
5559 vec->zvec_pool_check = pool_check;
5560 vec->zvec_smush_outnvlist = smush_outnvlist;
5561 vec->zvec_allow_log = allow_log;
5564 static void
5565 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5566 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5567 zfs_ioc_poolcheck_t pool_check)
5569 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5570 POOL_NAME, log_history, pool_check);
5573 static void
5574 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5575 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5577 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5578 DATASET_NAME, B_FALSE, pool_check);
5581 static void
5582 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5584 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5585 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5588 static void
5589 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5590 zfs_secpolicy_func_t *secpolicy)
5592 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5593 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5596 static void
5597 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5598 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5600 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5601 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5604 static void
5605 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5607 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5608 zfs_secpolicy_read);
5611 static void
5612 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5613 zfs_secpolicy_func_t *secpolicy)
5615 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5616 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5619 static void
5620 zfs_ioctl_init(void)
5622 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5623 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5624 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5626 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5627 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5628 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5630 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5631 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5632 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5634 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5635 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5636 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5638 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5639 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5640 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5642 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5643 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5644 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5646 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5647 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5648 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5650 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5651 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5652 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5654 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5655 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5656 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5657 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5658 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5659 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5661 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5662 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5663 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5665 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5666 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5667 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5669 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5670 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5671 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5673 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5674 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5675 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5677 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5678 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5679 POOL_NAME,
5680 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5682 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5683 zfs_ioc_channel_program, zfs_secpolicy_config,
5684 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5685 B_TRUE);
5687 /* IOCTLS that use the legacy function signature */
5689 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5690 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5692 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5693 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5694 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5695 zfs_ioc_pool_scan);
5696 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5697 zfs_ioc_pool_upgrade);
5698 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5699 zfs_ioc_vdev_add);
5700 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5701 zfs_ioc_vdev_remove);
5702 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5703 zfs_ioc_vdev_set_state);
5704 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5705 zfs_ioc_vdev_attach);
5706 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5707 zfs_ioc_vdev_detach);
5708 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5709 zfs_ioc_vdev_setpath);
5710 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5711 zfs_ioc_vdev_setfru);
5712 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5713 zfs_ioc_pool_set_props);
5714 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5715 zfs_ioc_vdev_split);
5716 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5717 zfs_ioc_pool_reguid);
5719 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5720 zfs_ioc_pool_configs, zfs_secpolicy_none);
5721 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5722 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5723 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5724 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5725 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5726 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5727 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5728 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5731 * pool destroy, and export don't log the history as part of
5732 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5733 * does the logging of those commands.
5735 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5736 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5737 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5738 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5740 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5741 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5742 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5743 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5745 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5746 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5747 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5748 zfs_ioc_dsobj_to_dsname,
5749 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5750 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5751 zfs_ioc_pool_get_history,
5752 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5754 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5755 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5757 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5758 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5759 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5760 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5762 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5763 zfs_ioc_space_written);
5764 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5765 zfs_ioc_objset_recvd_props);
5766 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5767 zfs_ioc_next_obj);
5768 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5769 zfs_ioc_get_fsacl);
5770 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5771 zfs_ioc_objset_stats);
5772 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5773 zfs_ioc_objset_zplprops);
5774 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5775 zfs_ioc_dataset_list_next);
5776 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5777 zfs_ioc_snapshot_list_next);
5778 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5779 zfs_ioc_send_progress);
5781 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5782 zfs_ioc_diff, zfs_secpolicy_diff);
5783 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5784 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5785 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5786 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5787 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5788 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5789 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5790 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5791 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5792 zfs_ioc_send, zfs_secpolicy_send);
5794 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5795 zfs_secpolicy_none);
5796 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5797 zfs_secpolicy_destroy);
5798 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5799 zfs_secpolicy_rename);
5800 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5801 zfs_secpolicy_recv);
5802 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5803 zfs_secpolicy_promote);
5804 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5805 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5806 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5807 zfs_secpolicy_set_fsacl);
5809 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5810 zfs_secpolicy_share, POOL_CHECK_NONE);
5811 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5812 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5813 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5814 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5815 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5816 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5817 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5818 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5822 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5823 zfs_ioc_poolcheck_t check)
5825 spa_t *spa;
5826 int error;
5828 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5830 if (check & POOL_CHECK_NONE)
5831 return (0);
5833 error = spa_open(name, &spa, FTAG);
5834 if (error == 0) {
5835 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5836 error = SET_ERROR(EAGAIN);
5837 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5838 error = SET_ERROR(EROFS);
5839 spa_close(spa, FTAG);
5841 return (error);
5845 * Find a free minor number.
5847 minor_t
5848 zfsdev_minor_alloc(void)
5850 static minor_t last_minor;
5851 minor_t m;
5853 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5855 for (m = last_minor + 1; m != last_minor; m++) {
5856 if (m > ZFSDEV_MAX_MINOR)
5857 m = 1;
5858 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5859 last_minor = m;
5860 return (m);
5864 return (0);
5867 static int
5868 zfs_ctldev_init(dev_t *devp)
5870 minor_t minor;
5871 zfs_soft_state_t *zs;
5873 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5874 ASSERT(getminor(*devp) == 0);
5876 minor = zfsdev_minor_alloc();
5877 if (minor == 0)
5878 return (SET_ERROR(ENXIO));
5880 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5881 return (SET_ERROR(EAGAIN));
5883 *devp = makedevice(getemajor(*devp), minor);
5885 zs = ddi_get_soft_state(zfsdev_state, minor);
5886 zs->zss_type = ZSST_CTLDEV;
5887 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5889 return (0);
5892 static void
5893 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5895 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5897 zfs_onexit_destroy(zo);
5898 ddi_soft_state_free(zfsdev_state, minor);
5901 void *
5902 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5904 zfs_soft_state_t *zp;
5906 zp = ddi_get_soft_state(zfsdev_state, minor);
5907 if (zp == NULL || zp->zss_type != which)
5908 return (NULL);
5910 return (zp->zss_data);
5913 static int
5914 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5916 int error = 0;
5918 if (getminor(*devp) != 0)
5919 return (zvol_open(devp, flag, otyp, cr));
5921 /* This is the control device. Allocate a new minor if requested. */
5922 if (flag & FEXCL) {
5923 mutex_enter(&zfsdev_state_lock);
5924 error = zfs_ctldev_init(devp);
5925 mutex_exit(&zfsdev_state_lock);
5928 return (error);
5931 static int
5932 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5934 zfs_onexit_t *zo;
5935 minor_t minor = getminor(dev);
5937 if (minor == 0)
5938 return (0);
5940 mutex_enter(&zfsdev_state_lock);
5941 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5942 if (zo == NULL) {
5943 mutex_exit(&zfsdev_state_lock);
5944 return (zvol_close(dev, flag, otyp, cr));
5946 zfs_ctldev_destroy(zo, minor);
5947 mutex_exit(&zfsdev_state_lock);
5949 return (0);
5952 static int
5953 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5955 zfs_cmd_t *zc;
5956 uint_t vecnum;
5957 int error, rc, len;
5958 minor_t minor = getminor(dev);
5959 const zfs_ioc_vec_t *vec;
5960 char *saved_poolname = NULL;
5961 nvlist_t *innvl = NULL;
5963 if (minor != 0 &&
5964 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5965 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
5967 vecnum = cmd - ZFS_IOC_FIRST;
5968 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
5970 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5971 return (SET_ERROR(EINVAL));
5972 vec = &zfs_ioc_vec[vecnum];
5974 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
5976 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5977 if (error != 0) {
5978 error = SET_ERROR(EFAULT);
5979 goto out;
5982 zc->zc_iflags = flag & FKIOCTL;
5983 if (zc->zc_nvlist_src_size != 0) {
5984 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5985 zc->zc_iflags, &innvl);
5986 if (error != 0)
5987 goto out;
5991 * Ensure that all pool/dataset names are valid before we pass down to
5992 * the lower layers.
5994 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5995 switch (vec->zvec_namecheck) {
5996 case POOL_NAME:
5997 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5998 error = SET_ERROR(EINVAL);
5999 else
6000 error = pool_status_check(zc->zc_name,
6001 vec->zvec_namecheck, vec->zvec_pool_check);
6002 break;
6004 case DATASET_NAME:
6005 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6006 error = SET_ERROR(EINVAL);
6007 else
6008 error = pool_status_check(zc->zc_name,
6009 vec->zvec_namecheck, vec->zvec_pool_check);
6010 break;
6012 case NO_NAME:
6013 break;
6017 if (error == 0)
6018 error = vec->zvec_secpolicy(zc, innvl, cr);
6020 if (error != 0)
6021 goto out;
6023 /* legacy ioctls can modify zc_name */
6024 len = strcspn(zc->zc_name, "/@#") + 1;
6025 saved_poolname = kmem_alloc(len, KM_SLEEP);
6026 (void) strlcpy(saved_poolname, zc->zc_name, len);
6028 if (vec->zvec_func != NULL) {
6029 nvlist_t *outnvl;
6030 int puterror = 0;
6031 spa_t *spa;
6032 nvlist_t *lognv = NULL;
6034 ASSERT(vec->zvec_legacy_func == NULL);
6037 * Add the innvl to the lognv before calling the func,
6038 * in case the func changes the innvl.
6040 if (vec->zvec_allow_log) {
6041 lognv = fnvlist_alloc();
6042 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6043 vec->zvec_name);
6044 if (!nvlist_empty(innvl)) {
6045 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6046 innvl);
6050 outnvl = fnvlist_alloc();
6051 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6054 * Some commands can partially execute, modfiy state, and still
6055 * return an error. In these cases, attempt to record what
6056 * was modified.
6058 if ((error == 0 ||
6059 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6060 vec->zvec_allow_log &&
6061 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6062 if (!nvlist_empty(outnvl)) {
6063 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6064 outnvl);
6066 if (error != 0) {
6067 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6068 error);
6070 (void) spa_history_log_nvl(spa, lognv);
6071 spa_close(spa, FTAG);
6073 fnvlist_free(lognv);
6075 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6076 int smusherror = 0;
6077 if (vec->zvec_smush_outnvlist) {
6078 smusherror = nvlist_smush(outnvl,
6079 zc->zc_nvlist_dst_size);
6081 if (smusherror == 0)
6082 puterror = put_nvlist(zc, outnvl);
6085 if (puterror != 0)
6086 error = puterror;
6088 nvlist_free(outnvl);
6089 } else {
6090 error = vec->zvec_legacy_func(zc);
6093 out:
6094 nvlist_free(innvl);
6095 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6096 if (error == 0 && rc != 0)
6097 error = SET_ERROR(EFAULT);
6098 if (error == 0 && vec->zvec_allow_log) {
6099 char *s = tsd_get(zfs_allow_log_key);
6100 if (s != NULL)
6101 strfree(s);
6102 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6103 } else {
6104 if (saved_poolname != NULL)
6105 strfree(saved_poolname);
6108 kmem_free(zc, sizeof (zfs_cmd_t));
6109 return (error);
6112 static int
6113 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6115 if (cmd != DDI_ATTACH)
6116 return (DDI_FAILURE);
6118 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6119 DDI_PSEUDO, 0) == DDI_FAILURE)
6120 return (DDI_FAILURE);
6122 zfs_dip = dip;
6124 ddi_report_dev(dip);
6126 return (DDI_SUCCESS);
6129 static int
6130 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6132 if (spa_busy() || zfs_busy() || zvol_busy())
6133 return (DDI_FAILURE);
6135 if (cmd != DDI_DETACH)
6136 return (DDI_FAILURE);
6138 zfs_dip = NULL;
6140 ddi_prop_remove_all(dip);
6141 ddi_remove_minor_node(dip, NULL);
6143 return (DDI_SUCCESS);
6146 /*ARGSUSED*/
6147 static int
6148 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6150 switch (infocmd) {
6151 case DDI_INFO_DEVT2DEVINFO:
6152 *result = zfs_dip;
6153 return (DDI_SUCCESS);
6155 case DDI_INFO_DEVT2INSTANCE:
6156 *result = NULL;
6157 return (DDI_SUCCESS);
6160 return (DDI_FAILURE);
6164 * OK, so this is a little weird.
6166 * /dev/zfs is the control node, i.e. minor 0.
6167 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6169 * /dev/zfs has basically nothing to do except serve up ioctls,
6170 * so most of the standard driver entry points are in zvol.c.
6172 static struct cb_ops zfs_cb_ops = {
6173 zfsdev_open, /* open */
6174 zfsdev_close, /* close */
6175 zvol_strategy, /* strategy */
6176 nodev, /* print */
6177 zvol_dump, /* dump */
6178 zvol_read, /* read */
6179 zvol_write, /* write */
6180 zfsdev_ioctl, /* ioctl */
6181 nodev, /* devmap */
6182 nodev, /* mmap */
6183 nodev, /* segmap */
6184 nochpoll, /* poll */
6185 ddi_prop_op, /* prop_op */
6186 NULL, /* streamtab */
6187 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6188 CB_REV, /* version */
6189 nodev, /* async read */
6190 nodev, /* async write */
6193 static struct dev_ops zfs_dev_ops = {
6194 DEVO_REV, /* version */
6195 0, /* refcnt */
6196 zfs_info, /* info */
6197 nulldev, /* identify */
6198 nulldev, /* probe */
6199 zfs_attach, /* attach */
6200 zfs_detach, /* detach */
6201 nodev, /* reset */
6202 &zfs_cb_ops, /* driver operations */
6203 NULL, /* no bus operations */
6204 NULL, /* power */
6205 ddi_quiesce_not_needed, /* quiesce */
6208 static struct modldrv zfs_modldrv = {
6209 &mod_driverops,
6210 "ZFS storage pool",
6211 &zfs_dev_ops
6214 static struct modlinkage modlinkage = {
6215 MODREV_1,
6216 (void *)&zfs_modlfs,
6217 (void *)&zfs_modldrv,
6218 NULL
6221 static void
6222 zfs_allow_log_destroy(void *arg)
6224 char *poolname = arg;
6225 strfree(poolname);
6229 _init(void)
6231 int error;
6233 spa_init(FREAD | FWRITE);
6234 zfs_init();
6235 zvol_init();
6236 zfs_ioctl_init();
6238 if ((error = mod_install(&modlinkage)) != 0) {
6239 zvol_fini();
6240 zfs_fini();
6241 spa_fini();
6242 return (error);
6245 tsd_create(&zfs_fsyncer_key, NULL);
6246 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6247 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6249 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6250 ASSERT(error == 0);
6251 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6253 return (0);
6257 _fini(void)
6259 int error;
6261 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6262 return (SET_ERROR(EBUSY));
6264 if ((error = mod_remove(&modlinkage)) != 0)
6265 return (error);
6267 zvol_fini();
6268 zfs_fini();
6269 spa_fini();
6270 if (zfs_nfsshare_inited)
6271 (void) ddi_modclose(nfs_mod);
6272 if (zfs_smbshare_inited)
6273 (void) ddi_modclose(smbsrv_mod);
6274 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6275 (void) ddi_modclose(sharefs_mod);
6277 tsd_destroy(&zfs_fsyncer_key);
6278 ldi_ident_release(zfs_li);
6279 zfs_li = NULL;
6280 mutex_destroy(&zfs_share_lock);
6282 return (error);
6286 _info(struct modinfo *modinfop)
6288 return (mod_info(&modlinkage, modinfop));