Merge commit '5f5913bb83405db87f982abee80162a479d363af'
[unleashed.git] / kernel / fs / zfs / zfs_ioctl.c
blobbc90f71d5ad378b557b257651ea3089aac7819b7
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>
191 #include <sys/vdev_removal.h>
193 #include "zfs_namecheck.h"
194 #include "zfs_prop.h"
195 #include "zfs_deleg.h"
196 #include "zfs_comutil.h"
198 #include "lua.h"
199 #include "lauxlib.h"
201 extern struct modlfs zfs_modlfs;
203 extern void zfs_init(void);
204 extern void zfs_fini(void);
206 ldi_ident_t zfs_li = NULL;
207 dev_info_t *zfs_dip;
209 uint_t zfs_fsyncer_key;
210 extern uint_t rrw_tsd_key;
211 static uint_t zfs_allow_log_key;
213 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
214 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
215 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
217 typedef enum {
218 NO_NAME,
219 POOL_NAME,
220 DATASET_NAME
221 } zfs_ioc_namecheck_t;
223 typedef enum {
224 POOL_CHECK_NONE = 1 << 0,
225 POOL_CHECK_SUSPENDED = 1 << 1,
226 POOL_CHECK_READONLY = 1 << 2,
227 } zfs_ioc_poolcheck_t;
229 typedef struct zfs_ioc_vec {
230 zfs_ioc_legacy_func_t *zvec_legacy_func;
231 zfs_ioc_func_t *zvec_func;
232 zfs_secpolicy_func_t *zvec_secpolicy;
233 zfs_ioc_namecheck_t zvec_namecheck;
234 boolean_t zvec_allow_log;
235 zfs_ioc_poolcheck_t zvec_pool_check;
236 boolean_t zvec_smush_outnvlist;
237 const char *zvec_name;
238 } zfs_ioc_vec_t;
240 /* This array is indexed by zfs_userquota_prop_t */
241 static const char *userquota_perms[] = {
242 ZFS_DELEG_PERM_USERUSED,
243 ZFS_DELEG_PERM_USERQUOTA,
244 ZFS_DELEG_PERM_GROUPUSED,
245 ZFS_DELEG_PERM_GROUPQUOTA,
248 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
249 static int zfs_check_settable(const char *name, nvpair_t *property,
250 cred_t *cr);
251 static int zfs_check_clearable(char *dataset, nvlist_t *props,
252 nvlist_t **errors);
253 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
254 boolean_t *);
255 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
256 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
258 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
260 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
261 void
262 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
264 const char *newfile;
265 char buf[512];
266 va_list adx;
269 * Get rid of annoying "../common/" prefix to filename.
271 newfile = strrchr(file, '/');
272 if (newfile != NULL) {
273 newfile = newfile + 1; /* Get rid of leading / */
274 } else {
275 newfile = file;
278 va_start(adx, fmt);
279 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
280 va_end(adx);
283 * To get this data, use the zfs-dprintf probe as so:
284 * dtrace -q -n 'zfs-dprintf \
285 * /stringof(arg0) == "dbuf.c"/ \
286 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
287 * arg0 = file name
288 * arg1 = function name
289 * arg2 = line number
290 * arg3 = message
292 DTRACE_PROBE4(zfs__dprintf,
293 char *, newfile, char *, func, int, line, char *, buf);
296 static void
297 history_str_free(char *buf)
299 kmem_free(buf, HIS_MAX_RECORD_LEN);
302 static char *
303 history_str_get(zfs_cmd_t *zc)
305 char *buf;
307 if (zc->zc_history == (uintptr_t)NULL)
308 return (NULL);
310 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
311 if (copyinstr((void *)(uintptr_t)zc->zc_history,
312 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
313 history_str_free(buf);
314 return (NULL);
317 buf[HIS_MAX_RECORD_LEN -1] = '\0';
319 return (buf);
323 * Check to see if the named dataset is currently defined as bootable
325 static boolean_t
326 zfs_is_bootfs(const char *name)
328 objset_t *os;
330 if (dmu_objset_hold(name, FTAG, &os) == 0) {
331 boolean_t ret;
332 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
333 dmu_objset_rele(os, FTAG);
334 return (ret);
336 return (B_FALSE);
340 * Return non-zero if the spa version is less than requested version.
342 static int
343 zfs_earlier_version(const char *name, int version)
345 spa_t *spa;
347 if (spa_open(name, &spa, FTAG) == 0) {
348 if (spa_version(spa) < version) {
349 spa_close(spa, FTAG);
350 return (1);
352 spa_close(spa, FTAG);
354 return (0);
358 * Return TRUE if the ZPL version is less than requested version.
360 static boolean_t
361 zpl_earlier_version(const char *name, int version)
363 objset_t *os;
364 boolean_t rc = B_TRUE;
366 if (dmu_objset_hold(name, FTAG, &os) == 0) {
367 uint64_t zplversion;
369 if (dmu_objset_type(os) != DMU_OST_ZFS) {
370 dmu_objset_rele(os, FTAG);
371 return (B_TRUE);
373 /* XXX reading from non-owned objset */
374 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
375 rc = zplversion < version;
376 dmu_objset_rele(os, FTAG);
378 return (rc);
381 static void
382 zfs_log_history(zfs_cmd_t *zc)
384 spa_t *spa;
385 char *buf;
387 if ((buf = history_str_get(zc)) == NULL)
388 return;
390 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
391 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
392 (void) spa_history_log(spa, buf);
393 spa_close(spa, FTAG);
395 history_str_free(buf);
399 * Policy for top-level read operations (list pools). Requires no privileges,
400 * and can be used in the local zone, as there is no associated dataset.
402 /* ARGSUSED */
403 static int
404 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
406 return (0);
410 * Policy for dataset read operations (list children, get statistics). Requires
411 * no privileges, but must be visible in the local zone.
413 /* ARGSUSED */
414 static int
415 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
417 if (INGLOBALZONE(curproc) ||
418 zone_dataset_visible(zc->zc_name, NULL))
419 return (0);
421 return (SET_ERROR(ENOENT));
424 static int
425 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
427 int writable = 1;
430 * The dataset must be visible by this zone -- check this first
431 * so they don't see EPERM on something they shouldn't know about.
433 if (!INGLOBALZONE(curproc) &&
434 !zone_dataset_visible(dataset, &writable))
435 return (SET_ERROR(ENOENT));
437 if (INGLOBALZONE(curproc)) {
439 * If the fs is zoned, only root can access it from the
440 * global zone.
442 if (secpolicy_zfs(cr) && zoned)
443 return (SET_ERROR(EPERM));
444 } else {
446 * If we are in a local zone, the 'zoned' property must be set.
448 if (!zoned)
449 return (SET_ERROR(EPERM));
451 /* must be writable by this zone */
452 if (!writable)
453 return (SET_ERROR(EPERM));
455 return (0);
458 static int
459 zfs_dozonecheck(const char *dataset, cred_t *cr)
461 uint64_t zoned;
463 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
464 return (SET_ERROR(ENOENT));
466 return (zfs_dozonecheck_impl(dataset, zoned, cr));
469 static int
470 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
472 uint64_t zoned;
474 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
475 return (SET_ERROR(ENOENT));
477 return (zfs_dozonecheck_impl(dataset, zoned, cr));
480 static int
481 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
482 const char *perm, cred_t *cr)
484 int error;
486 error = zfs_dozonecheck_ds(name, ds, cr);
487 if (error == 0) {
488 error = secpolicy_zfs(cr);
489 if (error != 0)
490 error = dsl_deleg_access_impl(ds, perm, cr);
492 return (error);
495 static int
496 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
498 int error;
499 dsl_dataset_t *ds;
500 dsl_pool_t *dp;
503 * First do a quick check for root in the global zone, which
504 * is allowed to do all write_perms. This ensures that zfs_ioc_*
505 * will get to handle nonexistent datasets.
507 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
508 return (0);
510 error = dsl_pool_hold(name, FTAG, &dp);
511 if (error != 0)
512 return (error);
514 error = dsl_dataset_hold(dp, name, FTAG, &ds);
515 if (error != 0) {
516 dsl_pool_rele(dp, FTAG);
517 return (error);
520 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
522 dsl_dataset_rele(ds, FTAG);
523 dsl_pool_rele(dp, FTAG);
524 return (error);
527 static int
528 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
529 cred_t *cr)
531 char *strval;
534 * Check permissions for special properties.
536 switch (prop) {
537 case ZFS_PROP_ZONED:
539 * Disallow setting of 'zoned' from within a local zone.
541 if (!INGLOBALZONE(curproc))
542 return (SET_ERROR(EPERM));
543 break;
545 case ZFS_PROP_QUOTA:
546 case ZFS_PROP_FILESYSTEM_LIMIT:
547 case ZFS_PROP_SNAPSHOT_LIMIT:
548 if (!INGLOBALZONE(curproc)) {
549 uint64_t zoned;
550 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
552 * Unprivileged users are allowed to modify the
553 * limit on things *under* (ie. contained by)
554 * the thing they own.
556 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
557 setpoint))
558 return (SET_ERROR(EPERM));
559 if (!zoned || strlen(dsname) <= strlen(setpoint))
560 return (SET_ERROR(EPERM));
562 break;
565 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
568 /* ARGSUSED */
569 static int
570 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
572 int error;
574 error = zfs_dozonecheck(zc->zc_name, cr);
575 if (error != 0)
576 return (error);
579 * permission to set permissions will be evaluated later in
580 * dsl_deleg_can_allow()
582 return (0);
585 /* ARGSUSED */
586 static int
587 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
589 return (zfs_secpolicy_write_perms(zc->zc_name,
590 ZFS_DELEG_PERM_ROLLBACK, cr));
593 /* ARGSUSED */
594 static int
595 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
597 dsl_pool_t *dp;
598 dsl_dataset_t *ds;
599 char *cp;
600 int error;
603 * Generate the current snapshot name from the given objsetid, then
604 * use that name for the secpolicy/zone checks.
606 cp = strchr(zc->zc_name, '@');
607 if (cp == NULL)
608 return (SET_ERROR(EINVAL));
609 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
610 if (error != 0)
611 return (error);
613 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
614 if (error != 0) {
615 dsl_pool_rele(dp, FTAG);
616 return (error);
619 dsl_dataset_name(ds, zc->zc_name);
621 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
622 ZFS_DELEG_PERM_SEND, cr);
623 dsl_dataset_rele(ds, FTAG);
624 dsl_pool_rele(dp, FTAG);
626 return (error);
629 /* ARGSUSED */
630 static int
631 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
633 return (zfs_secpolicy_write_perms(zc->zc_name,
634 ZFS_DELEG_PERM_SEND, cr));
637 /* ARGSUSED */
638 static int
639 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
641 vnode_t *vp;
642 int error;
644 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
645 NO_FOLLOW, NULL, &vp)) != 0)
646 return (error);
648 /* Now make sure mntpnt and dataset are ZFS */
650 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
651 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
652 zc->zc_name) != 0)) {
653 VN_RELE(vp);
654 return (SET_ERROR(EPERM));
657 VN_RELE(vp);
658 return (dsl_deleg_access(zc->zc_name,
659 ZFS_DELEG_PERM_SHARE, cr));
663 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
665 if (!INGLOBALZONE(curproc))
666 return (SET_ERROR(EPERM));
668 if (secpolicy_nfs(cr) == 0) {
669 return (0);
670 } else {
671 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
676 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
678 if (!INGLOBALZONE(curproc))
679 return (SET_ERROR(EPERM));
681 if (secpolicy_smb(cr) == 0) {
682 return (0);
683 } else {
684 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
688 static int
689 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
691 char *cp;
694 * Remove the @bla or /bla from the end of the name to get the parent.
696 (void) strncpy(parent, datasetname, parentsize);
697 cp = strrchr(parent, '@');
698 if (cp != NULL) {
699 cp[0] = '\0';
700 } else {
701 cp = strrchr(parent, '/');
702 if (cp == NULL)
703 return (SET_ERROR(ENOENT));
704 cp[0] = '\0';
707 return (0);
711 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
713 int error;
715 if ((error = zfs_secpolicy_write_perms(name,
716 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
717 return (error);
719 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
722 /* ARGSUSED */
723 static int
724 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
726 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
730 * Destroying snapshots with delegated permissions requires
731 * descendant mount and destroy permissions.
733 /* ARGSUSED */
734 static int
735 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
737 nvlist_t *snaps;
738 nvpair_t *pair, *nextpair;
739 int error = 0;
741 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
742 return (SET_ERROR(EINVAL));
743 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
744 pair = nextpair) {
745 nextpair = nvlist_next_nvpair(snaps, pair);
746 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
747 if (error == ENOENT) {
749 * Ignore any snapshots that don't exist (we consider
750 * them "already destroyed"). Remove the name from the
751 * nvl here in case the snapshot is created between
752 * now and when we try to destroy it (in which case
753 * we don't want to destroy it since we haven't
754 * checked for permission).
756 fnvlist_remove_nvpair(snaps, pair);
757 error = 0;
759 if (error != 0)
760 break;
763 return (error);
767 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
769 char parentname[ZFS_MAX_DATASET_NAME_LEN];
770 int error;
772 if ((error = zfs_secpolicy_write_perms(from,
773 ZFS_DELEG_PERM_RENAME, cr)) != 0)
774 return (error);
776 if ((error = zfs_secpolicy_write_perms(from,
777 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
778 return (error);
780 if ((error = zfs_get_parent(to, parentname,
781 sizeof (parentname))) != 0)
782 return (error);
784 if ((error = zfs_secpolicy_write_perms(parentname,
785 ZFS_DELEG_PERM_CREATE, cr)) != 0)
786 return (error);
788 if ((error = zfs_secpolicy_write_perms(parentname,
789 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
790 return (error);
792 return (error);
795 /* ARGSUSED */
796 static int
797 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
799 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
802 /* ARGSUSED */
803 static int
804 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
806 dsl_pool_t *dp;
807 dsl_dataset_t *clone;
808 int error;
810 error = zfs_secpolicy_write_perms(zc->zc_name,
811 ZFS_DELEG_PERM_PROMOTE, cr);
812 if (error != 0)
813 return (error);
815 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
816 if (error != 0)
817 return (error);
819 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
821 if (error == 0) {
822 char parentname[ZFS_MAX_DATASET_NAME_LEN];
823 dsl_dataset_t *origin = NULL;
824 dsl_dir_t *dd;
825 dd = clone->ds_dir;
827 error = dsl_dataset_hold_obj(dd->dd_pool,
828 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
829 if (error != 0) {
830 dsl_dataset_rele(clone, FTAG);
831 dsl_pool_rele(dp, FTAG);
832 return (error);
835 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
836 ZFS_DELEG_PERM_MOUNT, cr);
838 dsl_dataset_name(origin, parentname);
839 if (error == 0) {
840 error = zfs_secpolicy_write_perms_ds(parentname, origin,
841 ZFS_DELEG_PERM_PROMOTE, cr);
843 dsl_dataset_rele(clone, FTAG);
844 dsl_dataset_rele(origin, FTAG);
846 dsl_pool_rele(dp, FTAG);
847 return (error);
850 /* ARGSUSED */
851 static int
852 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
854 int error;
856 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
857 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
858 return (error);
860 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
861 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
862 return (error);
864 return (zfs_secpolicy_write_perms(zc->zc_name,
865 ZFS_DELEG_PERM_CREATE, cr));
869 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
871 return (zfs_secpolicy_write_perms(name,
872 ZFS_DELEG_PERM_SNAPSHOT, cr));
876 * Check for permission to create each snapshot in the nvlist.
878 /* ARGSUSED */
879 static int
880 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
882 nvlist_t *snaps;
883 int error = 0;
884 nvpair_t *pair;
886 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
887 return (SET_ERROR(EINVAL));
888 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
889 pair = nvlist_next_nvpair(snaps, pair)) {
890 char *name = nvpair_name(pair);
891 char *atp = strchr(name, '@');
893 if (atp == NULL) {
894 error = SET_ERROR(EINVAL);
895 break;
897 *atp = '\0';
898 error = zfs_secpolicy_snapshot_perms(name, cr);
899 *atp = '@';
900 if (error != 0)
901 break;
903 return (error);
907 * Check for permission to create each snapshot in the nvlist.
909 /* ARGSUSED */
910 static int
911 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
913 int error = 0;
915 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
916 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
917 char *name = nvpair_name(pair);
918 char *hashp = strchr(name, '#');
920 if (hashp == NULL) {
921 error = SET_ERROR(EINVAL);
922 break;
924 *hashp = '\0';
925 error = zfs_secpolicy_write_perms(name,
926 ZFS_DELEG_PERM_BOOKMARK, cr);
927 *hashp = '#';
928 if (error != 0)
929 break;
931 return (error);
934 /* ARGSUSED */
935 static int
936 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
938 return (zfs_secpolicy_write_perms(zc->zc_name,
939 ZFS_DELEG_PERM_REMAP, cr));
942 /* ARGSUSED */
943 static int
944 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
946 nvpair_t *pair, *nextpair;
947 int error = 0;
949 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
950 pair = nextpair) {
951 char *name = nvpair_name(pair);
952 char *hashp = strchr(name, '#');
953 nextpair = nvlist_next_nvpair(innvl, pair);
955 if (hashp == NULL) {
956 error = SET_ERROR(EINVAL);
957 break;
960 *hashp = '\0';
961 error = zfs_secpolicy_write_perms(name,
962 ZFS_DELEG_PERM_DESTROY, cr);
963 *hashp = '#';
964 if (error == ENOENT) {
966 * Ignore any filesystems that don't exist (we consider
967 * their bookmarks "already destroyed"). Remove
968 * the name from the nvl here in case the filesystem
969 * is created between now and when we try to destroy
970 * the bookmark (in which case we don't want to
971 * destroy it since we haven't checked for permission).
973 fnvlist_remove_nvpair(innvl, pair);
974 error = 0;
976 if (error != 0)
977 break;
980 return (error);
983 /* ARGSUSED */
984 static int
985 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
988 * Even root must have a proper TSD so that we know what pool
989 * to log to.
991 if (tsd_get(zfs_allow_log_key) == NULL)
992 return (SET_ERROR(EPERM));
993 return (0);
996 static int
997 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
999 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1000 int error;
1001 char *origin;
1003 if ((error = zfs_get_parent(zc->zc_name, parentname,
1004 sizeof (parentname))) != 0)
1005 return (error);
1007 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1008 (error = zfs_secpolicy_write_perms(origin,
1009 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1010 return (error);
1012 if ((error = zfs_secpolicy_write_perms(parentname,
1013 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1014 return (error);
1016 return (zfs_secpolicy_write_perms(parentname,
1017 ZFS_DELEG_PERM_MOUNT, cr));
1021 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1022 * SYS_CONFIG privilege, which is not available in a local zone.
1024 /* ARGSUSED */
1025 static int
1026 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1028 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1029 return (SET_ERROR(EPERM));
1031 return (0);
1035 * Policy for object to name lookups.
1037 /* ARGSUSED */
1038 static int
1039 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1041 int error;
1043 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1044 return (0);
1046 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1047 return (error);
1051 * Policy for fault injection. Requires all privileges.
1053 /* ARGSUSED */
1054 static int
1055 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1057 return (secpolicy_zinject(cr));
1060 /* ARGSUSED */
1061 static int
1062 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1064 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1066 if (prop == ZPROP_INVAL) {
1067 if (!zfs_prop_user(zc->zc_value))
1068 return (SET_ERROR(EINVAL));
1069 return (zfs_secpolicy_write_perms(zc->zc_name,
1070 ZFS_DELEG_PERM_USERPROP, cr));
1071 } else {
1072 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1073 NULL, cr));
1077 static int
1078 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1080 int err = zfs_secpolicy_read(zc, innvl, cr);
1081 if (err)
1082 return (err);
1084 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1085 return (SET_ERROR(EINVAL));
1087 if (zc->zc_value[0] == 0) {
1089 * They are asking about a posix uid/gid. If it's
1090 * themself, allow it.
1092 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1093 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1094 if (zc->zc_guid == crgetuid(cr))
1095 return (0);
1096 } else {
1097 if (groupmember(zc->zc_guid, cr))
1098 return (0);
1102 return (zfs_secpolicy_write_perms(zc->zc_name,
1103 userquota_perms[zc->zc_objset_type], cr));
1106 static int
1107 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1109 int err = zfs_secpolicy_read(zc, innvl, cr);
1110 if (err)
1111 return (err);
1113 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1114 return (SET_ERROR(EINVAL));
1116 return (zfs_secpolicy_write_perms(zc->zc_name,
1117 userquota_perms[zc->zc_objset_type], cr));
1120 /* ARGSUSED */
1121 static int
1122 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1124 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1125 NULL, cr));
1128 /* ARGSUSED */
1129 static int
1130 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1132 nvpair_t *pair;
1133 nvlist_t *holds;
1134 int error;
1136 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1137 if (error != 0)
1138 return (SET_ERROR(EINVAL));
1140 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1141 pair = nvlist_next_nvpair(holds, pair)) {
1142 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1143 error = dmu_fsname(nvpair_name(pair), fsname);
1144 if (error != 0)
1145 return (error);
1146 error = zfs_secpolicy_write_perms(fsname,
1147 ZFS_DELEG_PERM_HOLD, cr);
1148 if (error != 0)
1149 return (error);
1151 return (0);
1154 /* ARGSUSED */
1155 static int
1156 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1158 nvpair_t *pair;
1159 int error;
1161 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1162 pair = nvlist_next_nvpair(innvl, pair)) {
1163 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1164 error = dmu_fsname(nvpair_name(pair), fsname);
1165 if (error != 0)
1166 return (error);
1167 error = zfs_secpolicy_write_perms(fsname,
1168 ZFS_DELEG_PERM_RELEASE, cr);
1169 if (error != 0)
1170 return (error);
1172 return (0);
1176 * Policy for allowing temporary snapshots to be taken or released
1178 static int
1179 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1182 * A temporary snapshot is the same as a snapshot,
1183 * hold, destroy and release all rolled into one.
1184 * Delegated diff alone is sufficient that we allow this.
1186 int error;
1188 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1189 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1190 return (0);
1192 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1193 if (error == 0)
1194 error = zfs_secpolicy_hold(zc, innvl, cr);
1195 if (error == 0)
1196 error = zfs_secpolicy_release(zc, innvl, cr);
1197 if (error == 0)
1198 error = zfs_secpolicy_destroy(zc, innvl, cr);
1199 return (error);
1203 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1205 static int
1206 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1208 char *packed;
1209 int error;
1210 nvlist_t *list = NULL;
1213 * Read in and unpack the user-supplied nvlist.
1215 if (size == 0)
1216 return (SET_ERROR(EINVAL));
1218 packed = kmem_alloc(size, KM_SLEEP);
1220 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1221 iflag)) != 0) {
1222 kmem_free(packed, size);
1223 return (SET_ERROR(EFAULT));
1226 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1227 kmem_free(packed, size);
1228 return (error);
1231 kmem_free(packed, size);
1233 *nvp = list;
1234 return (0);
1238 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1239 * Entries will be removed from the end of the nvlist, and one int32 entry
1240 * named "N_MORE_ERRORS" will be added indicating how many entries were
1241 * removed.
1243 static int
1244 nvlist_smush(nvlist_t *errors, size_t max)
1246 size_t size;
1248 size = fnvlist_size(errors);
1250 if (size > max) {
1251 nvpair_t *more_errors;
1252 int n = 0;
1254 if (max < 1024)
1255 return (SET_ERROR(ENOMEM));
1257 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1258 more_errors = nvlist_prev_nvpair(errors, NULL);
1260 do {
1261 nvpair_t *pair = nvlist_prev_nvpair(errors,
1262 more_errors);
1263 fnvlist_remove_nvpair(errors, pair);
1264 n++;
1265 size = fnvlist_size(errors);
1266 } while (size > max);
1268 fnvlist_remove_nvpair(errors, more_errors);
1269 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1270 ASSERT3U(fnvlist_size(errors), <=, max);
1273 return (0);
1276 static int
1277 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1279 char *packed = NULL;
1280 int error = 0;
1281 size_t size;
1283 size = fnvlist_size(nvl);
1285 if (size > zc->zc_nvlist_dst_size) {
1286 error = SET_ERROR(ENOMEM);
1287 } else {
1288 packed = fnvlist_pack(nvl, &size);
1289 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1290 size, zc->zc_iflags) != 0)
1291 error = SET_ERROR(EFAULT);
1292 fnvlist_pack_free(packed, size);
1295 zc->zc_nvlist_dst_size = size;
1296 zc->zc_nvlist_dst_filled = B_TRUE;
1297 return (error);
1301 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1303 int error = 0;
1304 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1305 return (SET_ERROR(EINVAL));
1308 mutex_enter(&os->os_user_ptr_lock);
1309 *zfvp = dmu_objset_get_user(os);
1310 if (*zfvp) {
1311 VFS_HOLD((*zfvp)->z_vfs);
1312 } else {
1313 error = SET_ERROR(ESRCH);
1315 mutex_exit(&os->os_user_ptr_lock);
1316 return (error);
1320 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1322 objset_t *os;
1323 int error;
1325 error = dmu_objset_hold(dsname, FTAG, &os);
1326 if (error != 0)
1327 return (error);
1329 error = getzfsvfs_impl(os, zfvp);
1330 dmu_objset_rele(os, FTAG);
1331 return (error);
1335 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1336 * case its z_vfs will be NULL, and it will be opened as the owner.
1337 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1338 * which prevents all vnode ops from running.
1340 static int
1341 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1343 int error = 0;
1345 if (getzfsvfs(name, zfvp) != 0)
1346 error = zfsvfs_create(name, zfvp);
1347 if (error == 0) {
1348 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1349 RW_READER, tag);
1350 if ((*zfvp)->z_unmounted) {
1352 * XXX we could probably try again, since the unmounting
1353 * thread should be just about to disassociate the
1354 * objset from the zfsvfs.
1356 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1357 return (SET_ERROR(EBUSY));
1360 return (error);
1363 static void
1364 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1366 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1368 if (zfsvfs->z_vfs) {
1369 VFS_RELE(zfsvfs->z_vfs);
1370 } else {
1371 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1372 zfsvfs_free(zfsvfs);
1376 static int
1377 zfs_ioc_pool_create(zfs_cmd_t *zc)
1379 int error;
1380 nvlist_t *config, *props = NULL;
1381 nvlist_t *rootprops = NULL;
1382 nvlist_t *zplprops = NULL;
1384 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1385 zc->zc_iflags, &config))
1386 return (error);
1388 if (zc->zc_nvlist_src_size != 0 && (error =
1389 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1390 zc->zc_iflags, &props))) {
1391 nvlist_free(config);
1392 return (error);
1395 if (props) {
1396 nvlist_t *nvl = NULL;
1397 uint64_t version = SPA_VERSION;
1399 (void) nvlist_lookup_uint64(props,
1400 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1401 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1402 error = SET_ERROR(EINVAL);
1403 goto pool_props_bad;
1405 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1406 if (nvl) {
1407 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1408 if (error != 0) {
1409 nvlist_free(config);
1410 nvlist_free(props);
1411 return (error);
1413 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1415 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1416 error = zfs_fill_zplprops_root(version, rootprops,
1417 zplprops, NULL);
1418 if (error != 0)
1419 goto pool_props_bad;
1422 error = spa_create(zc->zc_name, config, props, zplprops);
1425 * Set the remaining root properties
1427 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1428 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1429 (void) spa_destroy(zc->zc_name);
1431 pool_props_bad:
1432 nvlist_free(rootprops);
1433 nvlist_free(zplprops);
1434 nvlist_free(config);
1435 nvlist_free(props);
1437 return (error);
1440 static int
1441 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1443 int error;
1444 zfs_log_history(zc);
1445 error = spa_destroy(zc->zc_name);
1446 if (error == 0)
1447 zvol_remove_minors(zc->zc_name);
1448 return (error);
1451 static int
1452 zfs_ioc_pool_import(zfs_cmd_t *zc)
1454 nvlist_t *config, *props = NULL;
1455 uint64_t guid;
1456 int error;
1458 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1459 zc->zc_iflags, &config)) != 0)
1460 return (error);
1462 if (zc->zc_nvlist_src_size != 0 && (error =
1463 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1464 zc->zc_iflags, &props))) {
1465 nvlist_free(config);
1466 return (error);
1469 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1470 guid != zc->zc_guid)
1471 error = SET_ERROR(EINVAL);
1472 else
1473 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1475 if (zc->zc_nvlist_dst != 0) {
1476 int err;
1478 if ((err = put_nvlist(zc, config)) != 0)
1479 error = err;
1482 nvlist_free(config);
1484 nvlist_free(props);
1486 return (error);
1489 static int
1490 zfs_ioc_pool_export(zfs_cmd_t *zc)
1492 int error;
1493 boolean_t force = (boolean_t)zc->zc_cookie;
1494 boolean_t hardforce = (boolean_t)zc->zc_guid;
1496 zfs_log_history(zc);
1497 error = spa_export(zc->zc_name, NULL, force, hardforce);
1498 if (error == 0)
1499 zvol_remove_minors(zc->zc_name);
1500 return (error);
1503 static int
1504 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1506 nvlist_t *configs;
1507 int error;
1509 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1510 return (SET_ERROR(EEXIST));
1512 error = put_nvlist(zc, configs);
1514 nvlist_free(configs);
1516 return (error);
1520 * inputs:
1521 * zc_name name of the pool
1523 * outputs:
1524 * zc_cookie real errno
1525 * zc_nvlist_dst config nvlist
1526 * zc_nvlist_dst_size size of config nvlist
1528 static int
1529 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1531 nvlist_t *config;
1532 int error;
1533 int ret = 0;
1535 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1536 sizeof (zc->zc_value));
1538 if (config != NULL) {
1539 ret = put_nvlist(zc, config);
1540 nvlist_free(config);
1543 * The config may be present even if 'error' is non-zero.
1544 * In this case we return success, and preserve the real errno
1545 * in 'zc_cookie'.
1547 zc->zc_cookie = error;
1548 } else {
1549 ret = error;
1552 return (ret);
1556 * Try to import the given pool, returning pool stats as appropriate so that
1557 * user land knows which devices are available and overall pool health.
1559 static int
1560 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1562 nvlist_t *tryconfig, *config;
1563 int error;
1565 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1566 zc->zc_iflags, &tryconfig)) != 0)
1567 return (error);
1569 config = spa_tryimport(tryconfig);
1571 nvlist_free(tryconfig);
1573 if (config == NULL)
1574 return (SET_ERROR(EINVAL));
1576 error = put_nvlist(zc, config);
1577 nvlist_free(config);
1579 return (error);
1583 * inputs:
1584 * zc_name name of the pool
1585 * zc_cookie scan func (pool_scan_func_t)
1586 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1588 static int
1589 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1591 spa_t *spa;
1592 int error;
1594 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1595 return (error);
1597 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1598 return (SET_ERROR(EINVAL));
1600 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1601 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1602 else if (zc->zc_cookie == POOL_SCAN_NONE)
1603 error = spa_scan_stop(spa);
1604 else
1605 error = spa_scan(spa, zc->zc_cookie);
1607 spa_close(spa, FTAG);
1609 return (error);
1612 static int
1613 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1615 spa_t *spa;
1616 int error;
1618 error = spa_open(zc->zc_name, &spa, FTAG);
1619 if (error == 0) {
1620 spa_freeze(spa);
1621 spa_close(spa, FTAG);
1623 return (error);
1626 static int
1627 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1629 spa_t *spa;
1630 int error;
1632 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1633 return (error);
1635 if (zc->zc_cookie < spa_version(spa) ||
1636 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1637 spa_close(spa, FTAG);
1638 return (SET_ERROR(EINVAL));
1641 spa_upgrade(spa, zc->zc_cookie);
1642 spa_close(spa, FTAG);
1644 return (error);
1647 static int
1648 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1650 spa_t *spa;
1651 char *hist_buf;
1652 uint64_t size;
1653 int error;
1655 if ((size = zc->zc_history_len) == 0)
1656 return (SET_ERROR(EINVAL));
1658 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1659 return (error);
1661 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1662 spa_close(spa, FTAG);
1663 return (SET_ERROR(ENOTSUP));
1666 hist_buf = kmem_alloc(size, KM_SLEEP);
1667 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1668 &zc->zc_history_len, hist_buf)) == 0) {
1669 error = ddi_copyout(hist_buf,
1670 (void *)(uintptr_t)zc->zc_history,
1671 zc->zc_history_len, zc->zc_iflags);
1674 spa_close(spa, FTAG);
1675 kmem_free(hist_buf, size);
1676 return (error);
1679 static int
1680 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1682 spa_t *spa;
1683 int error;
1685 error = spa_open(zc->zc_name, &spa, FTAG);
1686 if (error == 0) {
1687 error = spa_change_guid(spa);
1688 spa_close(spa, FTAG);
1690 return (error);
1693 static int
1694 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1696 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1700 * inputs:
1701 * zc_name name of filesystem
1702 * zc_obj object to find
1704 * outputs:
1705 * zc_value name of object
1707 static int
1708 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1710 objset_t *os;
1711 int error;
1713 /* XXX reading from objset not owned */
1714 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1715 return (error);
1716 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1717 dmu_objset_rele(os, FTAG);
1718 return (SET_ERROR(EINVAL));
1720 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1721 sizeof (zc->zc_value));
1722 dmu_objset_rele(os, FTAG);
1724 return (error);
1728 * inputs:
1729 * zc_name name of filesystem
1730 * zc_obj object to find
1732 * outputs:
1733 * zc_stat stats on object
1734 * zc_value path to object
1736 static int
1737 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1739 objset_t *os;
1740 int error;
1742 /* XXX reading from objset not owned */
1743 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1744 return (error);
1745 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1746 dmu_objset_rele(os, FTAG);
1747 return (SET_ERROR(EINVAL));
1749 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1750 sizeof (zc->zc_value));
1751 dmu_objset_rele(os, FTAG);
1753 return (error);
1756 static int
1757 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1759 spa_t *spa;
1760 int error;
1761 nvlist_t *config, **l2cache, **spares;
1762 uint_t nl2cache = 0, nspares = 0;
1764 error = spa_open(zc->zc_name, &spa, FTAG);
1765 if (error != 0)
1766 return (error);
1768 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1769 zc->zc_iflags, &config);
1770 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1771 &l2cache, &nl2cache);
1773 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1774 &spares, &nspares);
1777 * A root pool with concatenated devices is not supported.
1778 * Thus, can not add a device to a root pool.
1780 * Intent log device can not be added to a rootpool because
1781 * during mountroot, zil is replayed, a seperated log device
1782 * can not be accessed during the mountroot time.
1784 * l2cache and spare devices are ok to be added to a rootpool.
1786 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1787 nvlist_free(config);
1788 spa_close(spa, FTAG);
1789 return (SET_ERROR(EDOM));
1792 if (error == 0) {
1793 error = spa_vdev_add(spa, config);
1794 nvlist_free(config);
1796 spa_close(spa, FTAG);
1797 return (error);
1801 * inputs:
1802 * zc_name name of the pool
1803 * zc_guid guid of vdev to remove
1804 * zc_cookie cancel removal
1806 static int
1807 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1809 spa_t *spa;
1810 int error;
1812 error = spa_open(zc->zc_name, &spa, FTAG);
1813 if (error != 0)
1814 return (error);
1815 if (zc->zc_cookie != 0) {
1816 error = spa_vdev_remove_cancel(spa);
1817 } else {
1818 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1820 spa_close(spa, FTAG);
1821 return (error);
1824 static int
1825 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1827 spa_t *spa;
1828 int error;
1829 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1831 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1832 return (error);
1833 switch (zc->zc_cookie) {
1834 case VDEV_STATE_ONLINE:
1835 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1836 break;
1838 case VDEV_STATE_OFFLINE:
1839 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1840 break;
1842 case VDEV_STATE_FAULTED:
1843 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1844 zc->zc_obj != VDEV_AUX_EXTERNAL)
1845 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1847 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1848 break;
1850 case VDEV_STATE_DEGRADED:
1851 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1852 zc->zc_obj != VDEV_AUX_EXTERNAL)
1853 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1855 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1856 break;
1858 default:
1859 error = SET_ERROR(EINVAL);
1861 zc->zc_cookie = newstate;
1862 spa_close(spa, FTAG);
1863 return (error);
1866 static int
1867 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1869 spa_t *spa;
1870 int replacing = zc->zc_cookie;
1871 nvlist_t *config;
1872 int error;
1874 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1875 return (error);
1877 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1878 zc->zc_iflags, &config)) == 0) {
1879 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1880 nvlist_free(config);
1883 spa_close(spa, FTAG);
1884 return (error);
1887 static int
1888 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1890 spa_t *spa;
1891 int error;
1893 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1894 return (error);
1896 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1898 spa_close(spa, FTAG);
1899 return (error);
1902 static int
1903 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1905 spa_t *spa;
1906 nvlist_t *config, *props = NULL;
1907 int error;
1908 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1910 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1911 return (error);
1913 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1914 zc->zc_iflags, &config)) {
1915 spa_close(spa, FTAG);
1916 return (error);
1919 if (zc->zc_nvlist_src_size != 0 && (error =
1920 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1921 zc->zc_iflags, &props))) {
1922 spa_close(spa, FTAG);
1923 nvlist_free(config);
1924 return (error);
1927 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1929 spa_close(spa, FTAG);
1931 nvlist_free(config);
1932 nvlist_free(props);
1934 return (error);
1937 static int
1938 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1940 spa_t *spa;
1941 char *path = zc->zc_value;
1942 uint64_t guid = zc->zc_guid;
1943 int error;
1945 error = spa_open(zc->zc_name, &spa, FTAG);
1946 if (error != 0)
1947 return (error);
1949 error = spa_vdev_setpath(spa, guid, path);
1950 spa_close(spa, FTAG);
1951 return (error);
1954 static int
1955 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1957 spa_t *spa;
1958 char *fru = zc->zc_value;
1959 uint64_t guid = zc->zc_guid;
1960 int error;
1962 error = spa_open(zc->zc_name, &spa, FTAG);
1963 if (error != 0)
1964 return (error);
1966 error = spa_vdev_setfru(spa, guid, fru);
1967 spa_close(spa, FTAG);
1968 return (error);
1971 static int
1972 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1974 int error = 0;
1975 nvlist_t *nv;
1977 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1979 if (zc->zc_nvlist_dst != 0 &&
1980 (error = dsl_prop_get_all(os, &nv)) == 0) {
1981 dmu_objset_stats(os, nv);
1983 * NB: zvol_get_stats() will read the objset contents,
1984 * which we aren't supposed to do with a
1985 * DS_MODE_USER hold, because it could be
1986 * inconsistent. So this is a bit of a workaround...
1987 * XXX reading with out owning
1989 if (!zc->zc_objset_stats.dds_inconsistent &&
1990 dmu_objset_type(os) == DMU_OST_ZVOL) {
1991 error = zvol_get_stats(os, nv);
1992 if (error == EIO)
1993 return (error);
1994 VERIFY0(error);
1996 error = put_nvlist(zc, nv);
1997 nvlist_free(nv);
2000 return (error);
2004 * inputs:
2005 * zc_name name of filesystem
2006 * zc_nvlist_dst_size size of buffer for property nvlist
2008 * outputs:
2009 * zc_objset_stats stats
2010 * zc_nvlist_dst property nvlist
2011 * zc_nvlist_dst_size size of property nvlist
2013 static int
2014 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2016 objset_t *os;
2017 int error;
2019 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2020 if (error == 0) {
2021 error = zfs_ioc_objset_stats_impl(zc, os);
2022 dmu_objset_rele(os, FTAG);
2025 return (error);
2029 * inputs:
2030 * zc_name name of filesystem
2031 * zc_nvlist_dst_size size of buffer for property nvlist
2033 * outputs:
2034 * zc_nvlist_dst received property nvlist
2035 * zc_nvlist_dst_size size of received property nvlist
2037 * Gets received properties (distinct from local properties on or after
2038 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2039 * local property values.
2041 static int
2042 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2044 int error = 0;
2045 nvlist_t *nv;
2048 * Without this check, we would return local property values if the
2049 * caller has not already received properties on or after
2050 * SPA_VERSION_RECVD_PROPS.
2052 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2053 return (SET_ERROR(ENOTSUP));
2055 if (zc->zc_nvlist_dst != 0 &&
2056 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2057 error = put_nvlist(zc, nv);
2058 nvlist_free(nv);
2061 return (error);
2064 static int
2065 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2067 uint64_t value;
2068 int error;
2071 * zfs_get_zplprop() will either find a value or give us
2072 * the default value (if there is one).
2074 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2075 return (error);
2076 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2077 return (0);
2081 * inputs:
2082 * zc_name name of filesystem
2083 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2085 * outputs:
2086 * zc_nvlist_dst zpl property nvlist
2087 * zc_nvlist_dst_size size of zpl property nvlist
2089 static int
2090 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2092 objset_t *os;
2093 int err;
2095 /* XXX reading without owning */
2096 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2097 return (err);
2099 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2102 * NB: nvl_add_zplprop() will read the objset contents,
2103 * which we aren't supposed to do with a DS_MODE_USER
2104 * hold, because it could be inconsistent.
2106 if (zc->zc_nvlist_dst != (uintptr_t)NULL &&
2107 !zc->zc_objset_stats.dds_inconsistent &&
2108 dmu_objset_type(os) == DMU_OST_ZFS) {
2109 nvlist_t *nv;
2111 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2112 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2113 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2114 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2115 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2116 err = put_nvlist(zc, nv);
2117 nvlist_free(nv);
2118 } else {
2119 err = SET_ERROR(ENOENT);
2121 dmu_objset_rele(os, FTAG);
2122 return (err);
2125 static boolean_t
2126 dataset_name_hidden(const char *name)
2129 * Skip over datasets that are not visible in this zone,
2130 * internal datasets (which have a $ in their name), and
2131 * temporary datasets (which have a % in their name).
2133 if (strchr(name, '$') != NULL)
2134 return (B_TRUE);
2135 if (strchr(name, '%') != NULL)
2136 return (B_TRUE);
2137 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2138 return (B_TRUE);
2139 return (B_FALSE);
2143 * inputs:
2144 * zc_name name of filesystem
2145 * zc_cookie zap cursor
2146 * zc_nvlist_dst_size size of buffer for property nvlist
2148 * outputs:
2149 * zc_name name of next filesystem
2150 * zc_cookie zap cursor
2151 * zc_objset_stats stats
2152 * zc_nvlist_dst property nvlist
2153 * zc_nvlist_dst_size size of property nvlist
2155 static int
2156 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2158 objset_t *os;
2159 int error;
2160 char *p;
2161 size_t orig_len = strlen(zc->zc_name);
2163 top:
2164 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2165 if (error == ENOENT)
2166 error = SET_ERROR(ESRCH);
2167 return (error);
2170 p = strrchr(zc->zc_name, '/');
2171 if (p == NULL || p[1] != '\0')
2172 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2173 p = zc->zc_name + strlen(zc->zc_name);
2175 do {
2176 error = dmu_dir_list_next(os,
2177 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2178 NULL, &zc->zc_cookie);
2179 if (error == ENOENT)
2180 error = SET_ERROR(ESRCH);
2181 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2182 dmu_objset_rele(os, FTAG);
2185 * If it's an internal dataset (ie. with a '$' in its name),
2186 * don't try to get stats for it, otherwise we'll return ENOENT.
2188 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2189 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2190 if (error == ENOENT) {
2191 /* We lost a race with destroy, get the next one. */
2192 zc->zc_name[orig_len] = '\0';
2193 goto top;
2196 return (error);
2200 * inputs:
2201 * zc_name name of filesystem
2202 * zc_cookie zap cursor
2203 * zc_nvlist_dst_size size of buffer for property nvlist
2204 * zc_simple when set, only name is requested
2206 * outputs:
2207 * zc_name name of next snapshot
2208 * zc_objset_stats stats
2209 * zc_nvlist_dst property nvlist
2210 * zc_nvlist_dst_size size of property nvlist
2212 static int
2213 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2215 objset_t *os;
2216 int error;
2218 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2219 if (error != 0) {
2220 return (error == ENOENT ? ESRCH : error);
2224 * A dataset name of maximum length cannot have any snapshots,
2225 * so exit immediately.
2227 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2228 ZFS_MAX_DATASET_NAME_LEN) {
2229 dmu_objset_rele(os, FTAG);
2230 return (SET_ERROR(ESRCH));
2233 error = dmu_snapshot_list_next(os,
2234 sizeof (zc->zc_name) - strlen(zc->zc_name),
2235 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2236 NULL);
2238 if (error == 0 && !zc->zc_simple) {
2239 dsl_dataset_t *ds;
2240 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2242 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2243 if (error == 0) {
2244 objset_t *ossnap;
2246 error = dmu_objset_from_ds(ds, &ossnap);
2247 if (error == 0)
2248 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2249 dsl_dataset_rele(ds, FTAG);
2251 } else if (error == ENOENT) {
2252 error = SET_ERROR(ESRCH);
2255 dmu_objset_rele(os, FTAG);
2256 /* if we failed, undo the @ that we tacked on to zc_name */
2257 if (error != 0)
2258 *strchr(zc->zc_name, '@') = '\0';
2259 return (error);
2262 static int
2263 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2265 const char *propname = nvpair_name(pair);
2266 uint64_t *valary;
2267 unsigned int vallen;
2268 const char *domain;
2269 char *dash;
2270 zfs_userquota_prop_t type;
2271 uint64_t rid;
2272 uint64_t quota;
2273 zfsvfs_t *zfsvfs;
2274 int err;
2276 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2277 nvlist_t *attrs;
2278 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2279 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2280 &pair) != 0)
2281 return (SET_ERROR(EINVAL));
2285 * A correctly constructed propname is encoded as
2286 * userquota@<rid>-<domain>.
2288 if ((dash = strchr(propname, '-')) == NULL ||
2289 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2290 vallen != 3)
2291 return (SET_ERROR(EINVAL));
2293 domain = dash + 1;
2294 type = valary[0];
2295 rid = valary[1];
2296 quota = valary[2];
2298 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2299 if (err == 0) {
2300 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2301 zfsvfs_rele(zfsvfs, FTAG);
2304 return (err);
2308 * If the named property is one that has a special function to set its value,
2309 * return 0 on success and a positive error code on failure; otherwise if it is
2310 * not one of the special properties handled by this function, return -1.
2312 * XXX: It would be better for callers of the property interface if we handled
2313 * these special cases in dsl_prop.c (in the dsl layer).
2315 static int
2316 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2317 nvpair_t *pair)
2319 const char *propname = nvpair_name(pair);
2320 zfs_prop_t prop = zfs_name_to_prop(propname);
2321 uint64_t intval;
2322 int err = -1;
2324 if (prop == ZPROP_INVAL) {
2325 if (zfs_prop_userquota(propname))
2326 return (zfs_prop_set_userquota(dsname, pair));
2327 return (-1);
2330 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2331 nvlist_t *attrs;
2332 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2333 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2334 &pair) == 0);
2337 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2338 return (-1);
2340 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2342 switch (prop) {
2343 case ZFS_PROP_QUOTA:
2344 err = dsl_dir_set_quota(dsname, source, intval);
2345 break;
2346 case ZFS_PROP_REFQUOTA:
2347 err = dsl_dataset_set_refquota(dsname, source, intval);
2348 break;
2349 case ZFS_PROP_FILESYSTEM_LIMIT:
2350 case ZFS_PROP_SNAPSHOT_LIMIT:
2351 if (intval == UINT64_MAX) {
2352 /* clearing the limit, just do it */
2353 err = 0;
2354 } else {
2355 err = dsl_dir_activate_fs_ss_limit(dsname);
2358 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2359 * default path to set the value in the nvlist.
2361 if (err == 0)
2362 err = -1;
2363 break;
2364 case ZFS_PROP_RESERVATION:
2365 err = dsl_dir_set_reservation(dsname, source, intval);
2366 break;
2367 case ZFS_PROP_REFRESERVATION:
2368 err = dsl_dataset_set_refreservation(dsname, source, intval);
2369 break;
2370 case ZFS_PROP_VOLSIZE:
2371 err = zvol_set_volsize(dsname, intval);
2372 break;
2373 case ZFS_PROP_VERSION:
2375 zfsvfs_t *zfsvfs;
2377 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2378 break;
2380 err = zfs_set_version(zfsvfs, intval);
2381 zfsvfs_rele(zfsvfs, FTAG);
2383 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2384 zfs_cmd_t *zc;
2386 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2387 (void) strcpy(zc->zc_name, dsname);
2388 (void) zfs_ioc_userspace_upgrade(zc);
2389 kmem_free(zc, sizeof (zfs_cmd_t));
2391 break;
2393 default:
2394 err = -1;
2397 return (err);
2401 * This function is best effort. If it fails to set any of the given properties,
2402 * it continues to set as many as it can and returns the last error
2403 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2404 * with the list of names of all the properties that failed along with the
2405 * corresponding error numbers.
2407 * If every property is set successfully, zero is returned and errlist is not
2408 * modified.
2411 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2412 nvlist_t *errlist)
2414 nvpair_t *pair;
2415 nvpair_t *propval;
2416 int rv = 0;
2417 uint64_t intval;
2418 char *strval;
2419 nvlist_t *genericnvl = fnvlist_alloc();
2420 nvlist_t *retrynvl = fnvlist_alloc();
2422 retry:
2423 pair = NULL;
2424 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2425 const char *propname = nvpair_name(pair);
2426 zfs_prop_t prop = zfs_name_to_prop(propname);
2427 int err = 0;
2429 /* decode the property value */
2430 propval = pair;
2431 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2432 nvlist_t *attrs;
2433 attrs = fnvpair_value_nvlist(pair);
2434 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2435 &propval) != 0)
2436 err = SET_ERROR(EINVAL);
2439 /* Validate value type */
2440 if (err == 0 && prop == ZPROP_INVAL) {
2441 if (zfs_prop_user(propname)) {
2442 if (nvpair_type(propval) != DATA_TYPE_STRING)
2443 err = SET_ERROR(EINVAL);
2444 } else if (zfs_prop_userquota(propname)) {
2445 if (nvpair_type(propval) !=
2446 DATA_TYPE_UINT64_ARRAY)
2447 err = SET_ERROR(EINVAL);
2448 } else {
2449 err = SET_ERROR(EINVAL);
2451 } else if (err == 0) {
2452 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2453 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2454 err = SET_ERROR(EINVAL);
2455 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2456 const char *unused;
2458 intval = fnvpair_value_uint64(propval);
2460 switch (zfs_prop_get_type(prop)) {
2461 case PROP_TYPE_NUMBER:
2462 break;
2463 case PROP_TYPE_STRING:
2464 err = SET_ERROR(EINVAL);
2465 break;
2466 case PROP_TYPE_INDEX:
2467 if (zfs_prop_index_to_string(prop,
2468 intval, &unused) != 0)
2469 err = SET_ERROR(EINVAL);
2470 break;
2471 default:
2472 cmn_err(CE_PANIC,
2473 "unknown property type");
2475 } else {
2476 err = SET_ERROR(EINVAL);
2480 /* Validate permissions */
2481 if (err == 0)
2482 err = zfs_check_settable(dsname, pair, CRED());
2484 if (err == 0) {
2485 err = zfs_prop_set_special(dsname, source, pair);
2486 if (err == -1) {
2488 * For better performance we build up a list of
2489 * properties to set in a single transaction.
2491 err = nvlist_add_nvpair(genericnvl, pair);
2492 } else if (err != 0 && nvl != retrynvl) {
2494 * This may be a spurious error caused by
2495 * receiving quota and reservation out of order.
2496 * Try again in a second pass.
2498 err = nvlist_add_nvpair(retrynvl, pair);
2502 if (err != 0) {
2503 if (errlist != NULL)
2504 fnvlist_add_int32(errlist, propname, err);
2505 rv = err;
2509 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2510 nvl = retrynvl;
2511 goto retry;
2514 if (!nvlist_empty(genericnvl) &&
2515 dsl_props_set(dsname, source, genericnvl) != 0) {
2517 * If this fails, we still want to set as many properties as we
2518 * can, so try setting them individually.
2520 pair = NULL;
2521 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2522 const char *propname = nvpair_name(pair);
2523 int err = 0;
2525 propval = pair;
2526 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2527 nvlist_t *attrs;
2528 attrs = fnvpair_value_nvlist(pair);
2529 propval = fnvlist_lookup_nvpair(attrs,
2530 ZPROP_VALUE);
2533 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2534 strval = fnvpair_value_string(propval);
2535 err = dsl_prop_set_string(dsname, propname,
2536 source, strval);
2537 } else {
2538 intval = fnvpair_value_uint64(propval);
2539 err = dsl_prop_set_int(dsname, propname, source,
2540 intval);
2543 if (err != 0) {
2544 if (errlist != NULL) {
2545 fnvlist_add_int32(errlist, propname,
2546 err);
2548 rv = err;
2552 nvlist_free(genericnvl);
2553 nvlist_free(retrynvl);
2555 return (rv);
2559 * Check that all the properties are valid user properties.
2561 static int
2562 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2564 nvpair_t *pair = NULL;
2565 int error = 0;
2567 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2568 const char *propname = nvpair_name(pair);
2570 if (!zfs_prop_user(propname) ||
2571 nvpair_type(pair) != DATA_TYPE_STRING)
2572 return (SET_ERROR(EINVAL));
2574 if (error = zfs_secpolicy_write_perms(fsname,
2575 ZFS_DELEG_PERM_USERPROP, CRED()))
2576 return (error);
2578 if (strlen(propname) >= ZAP_MAXNAMELEN)
2579 return (SET_ERROR(ENAMETOOLONG));
2581 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2582 return (E2BIG);
2584 return (0);
2587 static void
2588 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2590 nvpair_t *pair;
2592 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2594 pair = NULL;
2595 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2596 if (nvlist_exists(skipped, nvpair_name(pair)))
2597 continue;
2599 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2603 static int
2604 clear_received_props(const char *dsname, nvlist_t *props,
2605 nvlist_t *skipped)
2607 int err = 0;
2608 nvlist_t *cleared_props = NULL;
2609 props_skip(props, skipped, &cleared_props);
2610 if (!nvlist_empty(cleared_props)) {
2612 * Acts on local properties until the dataset has received
2613 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2615 zprop_source_t flags = (ZPROP_SRC_NONE |
2616 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2617 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2619 nvlist_free(cleared_props);
2620 return (err);
2624 * inputs:
2625 * zc_name name of filesystem
2626 * zc_value name of property to set
2627 * zc_nvlist_src{_size} nvlist of properties to apply
2628 * zc_cookie received properties flag
2630 * outputs:
2631 * zc_nvlist_dst{_size} error for each unapplied received property
2633 static int
2634 zfs_ioc_set_prop(zfs_cmd_t *zc)
2636 nvlist_t *nvl;
2637 boolean_t received = zc->zc_cookie;
2638 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2639 ZPROP_SRC_LOCAL);
2640 nvlist_t *errors;
2641 int error;
2643 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2644 zc->zc_iflags, &nvl)) != 0)
2645 return (error);
2647 if (received) {
2648 nvlist_t *origprops;
2650 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2651 (void) clear_received_props(zc->zc_name,
2652 origprops, nvl);
2653 nvlist_free(origprops);
2656 error = dsl_prop_set_hasrecvd(zc->zc_name);
2659 errors = fnvlist_alloc();
2660 if (error == 0)
2661 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2663 if (zc->zc_nvlist_dst != (uintptr_t)NULL && errors != NULL) {
2664 (void) put_nvlist(zc, errors);
2667 nvlist_free(errors);
2668 nvlist_free(nvl);
2669 return (error);
2673 * inputs:
2674 * zc_name name of filesystem
2675 * zc_value name of property to inherit
2676 * zc_cookie revert to received value if TRUE
2678 * outputs: none
2680 static int
2681 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2683 const char *propname = zc->zc_value;
2684 zfs_prop_t prop = zfs_name_to_prop(propname);
2685 boolean_t received = zc->zc_cookie;
2686 zprop_source_t source = (received
2687 ? ZPROP_SRC_NONE /* revert to received value, if any */
2688 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2690 if (received) {
2691 nvlist_t *dummy;
2692 nvpair_t *pair;
2693 zprop_type_t type;
2694 int err;
2697 * zfs_prop_set_special() expects properties in the form of an
2698 * nvpair with type info.
2700 if (prop == ZPROP_INVAL) {
2701 if (!zfs_prop_user(propname))
2702 return (SET_ERROR(EINVAL));
2704 type = PROP_TYPE_STRING;
2705 } else if (prop == ZFS_PROP_VOLSIZE ||
2706 prop == ZFS_PROP_VERSION) {
2707 return (SET_ERROR(EINVAL));
2708 } else {
2709 type = zfs_prop_get_type(prop);
2712 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2714 switch (type) {
2715 case PROP_TYPE_STRING:
2716 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2717 break;
2718 case PROP_TYPE_NUMBER:
2719 case PROP_TYPE_INDEX:
2720 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2721 break;
2722 default:
2723 nvlist_free(dummy);
2724 return (SET_ERROR(EINVAL));
2727 pair = nvlist_next_nvpair(dummy, NULL);
2728 err = zfs_prop_set_special(zc->zc_name, source, pair);
2729 nvlist_free(dummy);
2730 if (err != -1)
2731 return (err); /* special property already handled */
2732 } else {
2734 * Only check this in the non-received case. We want to allow
2735 * 'inherit -S' to revert non-inheritable properties like quota
2736 * and reservation to the received or default values even though
2737 * they are not considered inheritable.
2739 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2740 return (SET_ERROR(EINVAL));
2743 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2744 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2747 static int
2748 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2750 nvlist_t *props;
2751 spa_t *spa;
2752 int error;
2753 nvpair_t *pair;
2755 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2756 zc->zc_iflags, &props))
2757 return (error);
2760 * If the only property is the configfile, then just do a spa_lookup()
2761 * to handle the faulted case.
2763 pair = nvlist_next_nvpair(props, NULL);
2764 if (pair != NULL && strcmp(nvpair_name(pair),
2765 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2766 nvlist_next_nvpair(props, pair) == NULL) {
2767 mutex_enter(&spa_namespace_lock);
2768 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2769 spa_configfile_set(spa, props, B_FALSE);
2770 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2772 mutex_exit(&spa_namespace_lock);
2773 if (spa != NULL) {
2774 nvlist_free(props);
2775 return (0);
2779 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2780 nvlist_free(props);
2781 return (error);
2784 error = spa_prop_set(spa, props);
2786 nvlist_free(props);
2787 spa_close(spa, FTAG);
2789 return (error);
2792 static int
2793 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2795 spa_t *spa;
2796 int error;
2797 nvlist_t *nvp = NULL;
2799 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2801 * If the pool is faulted, there may be properties we can still
2802 * get (such as altroot and cachefile), so attempt to get them
2803 * anyway.
2805 mutex_enter(&spa_namespace_lock);
2806 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2807 error = spa_prop_get(spa, &nvp);
2808 mutex_exit(&spa_namespace_lock);
2809 } else {
2810 error = spa_prop_get(spa, &nvp);
2811 spa_close(spa, FTAG);
2814 if (error == 0 && zc->zc_nvlist_dst != (uintptr_t)NULL)
2815 error = put_nvlist(zc, nvp);
2816 else
2817 error = SET_ERROR(EFAULT);
2819 nvlist_free(nvp);
2820 return (error);
2824 * inputs:
2825 * zc_name name of filesystem
2826 * zc_nvlist_src{_size} nvlist of delegated permissions
2827 * zc_perm_action allow/unallow flag
2829 * outputs: none
2831 static int
2832 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2834 int error;
2835 nvlist_t *fsaclnv = NULL;
2837 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2838 zc->zc_iflags, &fsaclnv)) != 0)
2839 return (error);
2842 * Verify nvlist is constructed correctly
2844 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2845 nvlist_free(fsaclnv);
2846 return (SET_ERROR(EINVAL));
2850 * If we don't have PRIV_SYS_MOUNT, then validate
2851 * that user is allowed to hand out each permission in
2852 * the nvlist(s)
2855 error = secpolicy_zfs(CRED());
2856 if (error != 0) {
2857 if (zc->zc_perm_action == B_FALSE) {
2858 error = dsl_deleg_can_allow(zc->zc_name,
2859 fsaclnv, CRED());
2860 } else {
2861 error = dsl_deleg_can_unallow(zc->zc_name,
2862 fsaclnv, CRED());
2866 if (error == 0)
2867 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2869 nvlist_free(fsaclnv);
2870 return (error);
2874 * inputs:
2875 * zc_name name of filesystem
2877 * outputs:
2878 * zc_nvlist_src{_size} nvlist of delegated permissions
2880 static int
2881 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2883 nvlist_t *nvp;
2884 int error;
2886 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2887 error = put_nvlist(zc, nvp);
2888 nvlist_free(nvp);
2891 return (error);
2894 /* ARGSUSED */
2895 static void
2896 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2898 zfs_creat_t *zct = arg;
2900 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2903 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2906 * inputs:
2907 * os parent objset pointer (NULL if root fs)
2908 * fuids_ok fuids allowed in this version of the spa?
2909 * sa_ok SAs allowed in this version of the spa?
2910 * createprops list of properties requested by creator
2912 * outputs:
2913 * zplprops values for the zplprops we attach to the master node object
2914 * is_ci true if requested file system will be purely case-insensitive
2916 * Determine the settings for utf8only, normalization and
2917 * casesensitivity. Specific values may have been requested by the
2918 * creator and/or we can inherit values from the parent dataset. If
2919 * the file system is of too early a vintage, a creator can not
2920 * request settings for these properties, even if the requested
2921 * setting is the default value. We don't actually want to create dsl
2922 * properties for these, so remove them from the source nvlist after
2923 * processing.
2925 static int
2926 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2927 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2928 nvlist_t *zplprops, boolean_t *is_ci)
2930 uint64_t sense = ZFS_PROP_UNDEFINED;
2931 uint64_t norm = ZFS_PROP_UNDEFINED;
2932 uint64_t u8 = ZFS_PROP_UNDEFINED;
2934 ASSERT(zplprops != NULL);
2936 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
2937 return (SET_ERROR(EINVAL));
2940 * Pull out creator prop choices, if any.
2942 if (createprops) {
2943 (void) nvlist_lookup_uint64(createprops,
2944 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2945 (void) nvlist_lookup_uint64(createprops,
2946 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2947 (void) nvlist_remove_all(createprops,
2948 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2949 (void) nvlist_lookup_uint64(createprops,
2950 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2951 (void) nvlist_remove_all(createprops,
2952 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2953 (void) nvlist_lookup_uint64(createprops,
2954 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2955 (void) nvlist_remove_all(createprops,
2956 zfs_prop_to_name(ZFS_PROP_CASE));
2960 * If the zpl version requested is whacky or the file system
2961 * or pool is version is too "young" to support normalization
2962 * and the creator tried to set a value for one of the props,
2963 * error out.
2965 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2966 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2967 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2968 (zplver < ZPL_VERSION_NORMALIZATION &&
2969 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2970 sense != ZFS_PROP_UNDEFINED)))
2971 return (SET_ERROR(ENOTSUP));
2974 * Put the version in the zplprops
2976 VERIFY(nvlist_add_uint64(zplprops,
2977 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2979 if (norm == ZFS_PROP_UNDEFINED)
2980 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2981 VERIFY(nvlist_add_uint64(zplprops,
2982 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2985 * If we're normalizing, names must always be valid UTF-8 strings.
2987 if (norm)
2988 u8 = 1;
2989 if (u8 == ZFS_PROP_UNDEFINED)
2990 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2991 VERIFY(nvlist_add_uint64(zplprops,
2992 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2994 if (sense == ZFS_PROP_UNDEFINED)
2995 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2996 VERIFY(nvlist_add_uint64(zplprops,
2997 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2999 if (is_ci)
3000 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3002 return (0);
3005 static int
3006 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3007 nvlist_t *zplprops, boolean_t *is_ci)
3009 boolean_t fuids_ok, sa_ok;
3010 uint64_t zplver = ZPL_VERSION;
3011 objset_t *os = NULL;
3012 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3013 char *cp;
3014 spa_t *spa;
3015 uint64_t spa_vers;
3016 int error;
3018 (void) strlcpy(parentname, dataset, sizeof (parentname));
3019 cp = strrchr(parentname, '/');
3020 ASSERT(cp != NULL);
3021 cp[0] = '\0';
3023 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3024 return (error);
3026 spa_vers = spa_version(spa);
3027 spa_close(spa, FTAG);
3029 zplver = zfs_zpl_version_map(spa_vers);
3030 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3031 sa_ok = (zplver >= ZPL_VERSION_SA);
3034 * Open parent object set so we can inherit zplprop values.
3036 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3037 return (error);
3039 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3040 zplprops, is_ci);
3041 dmu_objset_rele(os, FTAG);
3042 return (error);
3045 static int
3046 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3047 nvlist_t *zplprops, boolean_t *is_ci)
3049 boolean_t fuids_ok;
3050 boolean_t sa_ok;
3051 uint64_t zplver = ZPL_VERSION;
3052 int error;
3054 zplver = zfs_zpl_version_map(spa_vers);
3055 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3056 sa_ok = (zplver >= ZPL_VERSION_SA);
3058 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3059 createprops, zplprops, is_ci);
3060 return (error);
3064 * innvl: {
3065 * "type" -> dmu_objset_type_t (int32)
3066 * (optional) "props" -> { prop -> value }
3069 * outnvl: propname -> error code (int32)
3071 static int
3072 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3074 int error = 0;
3075 zfs_creat_t zct = { 0 };
3076 nvlist_t *nvprops = NULL;
3077 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3078 int32_t type32;
3079 dmu_objset_type_t type;
3080 boolean_t is_insensitive = B_FALSE;
3082 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3083 return (SET_ERROR(EINVAL));
3084 type = type32;
3085 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3087 switch (type) {
3088 case DMU_OST_ZFS:
3089 cbfunc = zfs_create_cb;
3090 break;
3092 case DMU_OST_ZVOL:
3093 cbfunc = zvol_create_cb;
3094 break;
3096 default:
3097 cbfunc = NULL;
3098 break;
3100 if (strchr(fsname, '@') ||
3101 strchr(fsname, '%'))
3102 return (SET_ERROR(EINVAL));
3104 zct.zct_props = nvprops;
3106 if (cbfunc == NULL)
3107 return (SET_ERROR(EINVAL));
3109 if (type == DMU_OST_ZVOL) {
3110 uint64_t volsize, volblocksize;
3112 if (nvprops == NULL)
3113 return (SET_ERROR(EINVAL));
3114 if (nvlist_lookup_uint64(nvprops,
3115 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3116 return (SET_ERROR(EINVAL));
3118 if ((error = nvlist_lookup_uint64(nvprops,
3119 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3120 &volblocksize)) != 0 && error != ENOENT)
3121 return (SET_ERROR(EINVAL));
3123 if (error != 0)
3124 volblocksize = zfs_prop_default_numeric(
3125 ZFS_PROP_VOLBLOCKSIZE);
3127 if ((error = zvol_check_volblocksize(
3128 volblocksize)) != 0 ||
3129 (error = zvol_check_volsize(volsize,
3130 volblocksize)) != 0)
3131 return (error);
3132 } else if (type == DMU_OST_ZFS) {
3133 int error;
3136 * We have to have normalization and
3137 * case-folding flags correct when we do the
3138 * file system creation, so go figure them out
3139 * now.
3141 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3142 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3143 error = zfs_fill_zplprops(fsname, nvprops,
3144 zct.zct_zplprops, &is_insensitive);
3145 if (error != 0) {
3146 nvlist_free(zct.zct_zplprops);
3147 return (error);
3151 error = dmu_objset_create(fsname, type,
3152 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3153 nvlist_free(zct.zct_zplprops);
3156 * It would be nice to do this atomically.
3158 if (error == 0) {
3159 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3160 nvprops, outnvl);
3161 if (error != 0)
3162 (void) dsl_destroy_head(fsname);
3164 return (error);
3168 * innvl: {
3169 * "origin" -> name of origin snapshot
3170 * (optional) "props" -> { prop -> value }
3173 * outnvl: propname -> error code (int32)
3175 static int
3176 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3178 int error = 0;
3179 nvlist_t *nvprops = NULL;
3180 char *origin_name;
3182 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3183 return (SET_ERROR(EINVAL));
3184 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3186 if (strchr(fsname, '@') ||
3187 strchr(fsname, '%'))
3188 return (SET_ERROR(EINVAL));
3190 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3191 return (SET_ERROR(EINVAL));
3192 error = dmu_objset_clone(fsname, origin_name);
3193 if (error != 0)
3194 return (error);
3197 * It would be nice to do this atomically.
3199 if (error == 0) {
3200 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3201 nvprops, outnvl);
3202 if (error != 0)
3203 (void) dsl_destroy_head(fsname);
3205 return (error);
3208 /* ARGSUSED */
3209 static int
3210 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3212 if (strchr(fsname, '@') ||
3213 strchr(fsname, '%'))
3214 return (SET_ERROR(EINVAL));
3216 return (dmu_objset_remap_indirects(fsname));
3220 * innvl: {
3221 * "snaps" -> { snapshot1, snapshot2 }
3222 * (optional) "props" -> { prop -> value (string) }
3225 * outnvl: snapshot -> error code (int32)
3227 static int
3228 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3230 nvlist_t *snaps;
3231 nvlist_t *props = NULL;
3232 int error, poollen;
3233 nvpair_t *pair;
3235 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3236 if ((error = zfs_check_userprops(poolname, props)) != 0)
3237 return (error);
3239 if (!nvlist_empty(props) &&
3240 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3241 return (SET_ERROR(ENOTSUP));
3243 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3244 return (SET_ERROR(EINVAL));
3245 poollen = strlen(poolname);
3246 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3247 pair = nvlist_next_nvpair(snaps, pair)) {
3248 const char *name = nvpair_name(pair);
3249 const char *cp = strchr(name, '@');
3252 * The snap name must contain an @, and the part after it must
3253 * contain only valid characters.
3255 if (cp == NULL ||
3256 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3257 return (SET_ERROR(EINVAL));
3260 * The snap must be in the specified pool.
3262 if (strncmp(name, poolname, poollen) != 0 ||
3263 (name[poollen] != '/' && name[poollen] != '@'))
3264 return (SET_ERROR(EXDEV));
3266 /* This must be the only snap of this fs. */
3267 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3268 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3269 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3270 == 0) {
3271 return (SET_ERROR(EXDEV));
3276 error = dsl_dataset_snapshot(snaps, props, outnvl);
3277 return (error);
3281 * innvl: "message" -> string
3283 /* ARGSUSED */
3284 static int
3285 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3287 char *message;
3288 spa_t *spa;
3289 int error;
3290 char *poolname;
3293 * The poolname in the ioctl is not set, we get it from the TSD,
3294 * which was set at the end of the last successful ioctl that allows
3295 * logging. The secpolicy func already checked that it is set.
3296 * Only one log ioctl is allowed after each successful ioctl, so
3297 * we clear the TSD here.
3299 poolname = tsd_get(zfs_allow_log_key);
3300 (void) tsd_set(zfs_allow_log_key, NULL);
3301 error = spa_open(poolname, &spa, FTAG);
3302 strfree(poolname);
3303 if (error != 0)
3304 return (error);
3306 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3307 spa_close(spa, FTAG);
3308 return (SET_ERROR(EINVAL));
3311 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3312 spa_close(spa, FTAG);
3313 return (SET_ERROR(ENOTSUP));
3316 error = spa_history_log(spa, message);
3317 spa_close(spa, FTAG);
3318 return (error);
3322 * The dp_config_rwlock must not be held when calling this, because the
3323 * unmount may need to write out data.
3325 * This function is best-effort. Callers must deal gracefully if it
3326 * remains mounted (or is remounted after this call).
3328 * Returns 0 if the argument is not a snapshot, or it is not currently a
3329 * filesystem, or we were able to unmount it. Returns error code otherwise.
3331 void
3332 zfs_unmount_snap(const char *snapname)
3334 vfs_t *vfsp = NULL;
3335 zfsvfs_t *zfsvfs = NULL;
3337 if (strchr(snapname, '@') == NULL)
3338 return;
3340 int err = getzfsvfs(snapname, &zfsvfs);
3341 if (err != 0) {
3342 ASSERT3P(zfsvfs, ==, NULL);
3343 return;
3345 vfsp = zfsvfs->z_vfs;
3347 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3349 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3350 VFS_RELE(vfsp);
3351 if (err != 0)
3352 return;
3355 * Always force the unmount for snapshots.
3357 (void) dounmount(vfsp, MS_FORCE, kcred);
3360 /* ARGSUSED */
3361 static int
3362 zfs_unmount_snap_cb(const char *snapname, void *arg)
3364 zfs_unmount_snap(snapname);
3365 return (0);
3369 * When a clone is destroyed, its origin may also need to be destroyed,
3370 * in which case it must be unmounted. This routine will do that unmount
3371 * if necessary.
3373 void
3374 zfs_destroy_unmount_origin(const char *fsname)
3376 int error;
3377 objset_t *os;
3378 dsl_dataset_t *ds;
3380 error = dmu_objset_hold(fsname, FTAG, &os);
3381 if (error != 0)
3382 return;
3383 ds = dmu_objset_ds(os);
3384 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3385 char originname[ZFS_MAX_DATASET_NAME_LEN];
3386 dsl_dataset_name(ds->ds_prev, originname);
3387 dmu_objset_rele(os, FTAG);
3388 zfs_unmount_snap(originname);
3389 } else {
3390 dmu_objset_rele(os, FTAG);
3395 * innvl: {
3396 * "snaps" -> { snapshot1, snapshot2 }
3397 * (optional boolean) "defer"
3400 * outnvl: snapshot -> error code (int32)
3403 /* ARGSUSED */
3404 static int
3405 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3407 nvlist_t *snaps;
3408 nvpair_t *pair;
3409 boolean_t defer;
3411 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3412 return (SET_ERROR(EINVAL));
3413 defer = nvlist_exists(innvl, "defer");
3415 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3416 pair = nvlist_next_nvpair(snaps, pair)) {
3417 zfs_unmount_snap(nvpair_name(pair));
3420 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3424 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3425 * All bookmarks must be in the same pool.
3427 * innvl: {
3428 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3431 * outnvl: bookmark -> error code (int32)
3434 /* ARGSUSED */
3435 static int
3436 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3438 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3439 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3440 char *snap_name;
3443 * Verify the snapshot argument.
3445 if (nvpair_value_string(pair, &snap_name) != 0)
3446 return (SET_ERROR(EINVAL));
3449 /* Verify that the keys (bookmarks) are unique */
3450 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3451 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3452 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3453 return (SET_ERROR(EINVAL));
3457 return (dsl_bookmark_create(innvl, outnvl));
3461 * innvl: {
3462 * property 1, property 2, ...
3465 * outnvl: {
3466 * bookmark name 1 -> { property 1, property 2, ... },
3467 * bookmark name 2 -> { property 1, property 2, ... }
3471 static int
3472 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3474 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3478 * innvl: {
3479 * bookmark name 1, bookmark name 2
3482 * outnvl: bookmark -> error code (int32)
3485 static int
3486 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3487 nvlist_t *outnvl)
3489 int error, poollen;
3491 poollen = strlen(poolname);
3492 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3493 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3494 const char *name = nvpair_name(pair);
3495 const char *cp = strchr(name, '#');
3498 * The bookmark name must contain an #, and the part after it
3499 * must contain only valid characters.
3501 if (cp == NULL ||
3502 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3503 return (SET_ERROR(EINVAL));
3506 * The bookmark must be in the specified pool.
3508 if (strncmp(name, poolname, poollen) != 0 ||
3509 (name[poollen] != '/' && name[poollen] != '#'))
3510 return (SET_ERROR(EXDEV));
3513 error = dsl_bookmark_destroy(innvl, outnvl);
3514 return (error);
3517 static int
3518 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3519 nvlist_t *outnvl)
3521 char *program;
3522 uint64_t instrlimit, memlimit;
3523 boolean_t sync_flag;
3524 nvpair_t *nvarg = NULL;
3526 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3527 return (EINVAL);
3529 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3530 sync_flag = B_TRUE;
3532 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3533 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3535 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3536 memlimit = ZCP_DEFAULT_MEMLIMIT;
3538 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3539 return (EINVAL);
3542 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3543 return (EINVAL);
3544 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3545 return (EINVAL);
3547 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3548 nvarg, outnvl));
3552 * inputs:
3553 * zc_name name of dataset to destroy
3554 * zc_objset_type type of objset
3555 * zc_defer_destroy mark for deferred destroy
3557 * outputs: none
3559 static int
3560 zfs_ioc_destroy(zfs_cmd_t *zc)
3562 int err;
3564 if (zc->zc_objset_type == DMU_OST_ZFS)
3565 zfs_unmount_snap(zc->zc_name);
3567 if (strchr(zc->zc_name, '@'))
3568 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3569 else
3570 err = dsl_destroy_head(zc->zc_name);
3571 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3572 (void) zvol_remove_minor(zc->zc_name);
3573 return (err);
3577 * fsname is name of dataset to rollback (to most recent snapshot)
3579 * innvl may contain name of expected target snapshot
3581 * outnvl: "target" -> name of most recent snapshot
3584 /* ARGSUSED */
3585 static int
3586 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3588 zfsvfs_t *zfsvfs;
3589 char *target = NULL;
3590 int error;
3592 (void) nvlist_lookup_string(innvl, "target", &target);
3593 if (target != NULL) {
3594 const char *cp = strchr(target, '@');
3597 * The snap name must contain an @, and the part after it must
3598 * contain only valid characters.
3600 if (cp == NULL ||
3601 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3602 return (SET_ERROR(EINVAL));
3605 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3606 dsl_dataset_t *ds;
3608 ds = dmu_objset_ds(zfsvfs->z_os);
3609 error = zfs_suspend_fs(zfsvfs);
3610 if (error == 0) {
3611 int resume_err;
3613 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3614 outnvl);
3615 resume_err = zfs_resume_fs(zfsvfs, ds);
3616 error = error ? error : resume_err;
3618 VFS_RELE(zfsvfs->z_vfs);
3619 } else {
3620 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3622 return (error);
3625 static int
3626 recursive_unmount(const char *fsname, void *arg)
3628 const char *snapname = arg;
3629 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3631 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3632 zfs_unmount_snap(fullname);
3634 return (0);
3638 * inputs:
3639 * zc_name old name of dataset
3640 * zc_value new name of dataset
3641 * zc_cookie recursive flag (only valid for snapshots)
3643 * outputs: none
3645 static int
3646 zfs_ioc_rename(zfs_cmd_t *zc)
3648 boolean_t recursive = zc->zc_cookie & 1;
3649 char *at;
3651 /* "zfs rename" from and to ...%recv datasets should both fail */
3652 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3653 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3654 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3655 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3656 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3657 return (SET_ERROR(EINVAL));
3659 at = strchr(zc->zc_name, '@');
3660 if (at != NULL) {
3661 /* snaps must be in same fs */
3662 int error;
3664 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3665 return (SET_ERROR(EXDEV));
3666 *at = '\0';
3667 if (zc->zc_objset_type == DMU_OST_ZFS) {
3668 error = dmu_objset_find(zc->zc_name,
3669 recursive_unmount, at + 1,
3670 recursive ? DS_FIND_CHILDREN : 0);
3671 if (error != 0) {
3672 *at = '@';
3673 return (error);
3676 error = dsl_dataset_rename_snapshot(zc->zc_name,
3677 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3678 *at = '@';
3680 return (error);
3681 } else {
3682 if (zc->zc_objset_type == DMU_OST_ZVOL)
3683 (void) zvol_remove_minor(zc->zc_name);
3684 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3688 static int
3689 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3691 const char *propname = nvpair_name(pair);
3692 boolean_t issnap = (strchr(dsname, '@') != NULL);
3693 zfs_prop_t prop = zfs_name_to_prop(propname);
3694 uint64_t intval;
3695 int err;
3697 if (prop == ZPROP_INVAL) {
3698 if (zfs_prop_user(propname)) {
3699 if (err = zfs_secpolicy_write_perms(dsname,
3700 ZFS_DELEG_PERM_USERPROP, cr))
3701 return (err);
3702 return (0);
3705 if (!issnap && zfs_prop_userquota(propname)) {
3706 const char *perm = NULL;
3707 const char *uq_prefix =
3708 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3709 const char *gq_prefix =
3710 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3712 if (strncmp(propname, uq_prefix,
3713 strlen(uq_prefix)) == 0) {
3714 perm = ZFS_DELEG_PERM_USERQUOTA;
3715 } else if (strncmp(propname, gq_prefix,
3716 strlen(gq_prefix)) == 0) {
3717 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3718 } else {
3719 /* USERUSED and GROUPUSED are read-only */
3720 return (SET_ERROR(EINVAL));
3723 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3724 return (err);
3725 return (0);
3728 return (SET_ERROR(EINVAL));
3731 if (issnap)
3732 return (SET_ERROR(EINVAL));
3734 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3736 * dsl_prop_get_all_impl() returns properties in this
3737 * format.
3739 nvlist_t *attrs;
3740 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3741 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3742 &pair) == 0);
3746 * Check that this value is valid for this pool version
3748 switch (prop) {
3749 case ZFS_PROP_COMPRESSION:
3751 * If the user specified gzip compression, make sure
3752 * the SPA supports it. We ignore any errors here since
3753 * we'll catch them later.
3755 if (nvpair_value_uint64(pair, &intval) == 0) {
3756 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3757 intval <= ZIO_COMPRESS_GZIP_9 &&
3758 zfs_earlier_version(dsname,
3759 SPA_VERSION_GZIP_COMPRESSION)) {
3760 return (SET_ERROR(ENOTSUP));
3763 if (intval == ZIO_COMPRESS_ZLE &&
3764 zfs_earlier_version(dsname,
3765 SPA_VERSION_ZLE_COMPRESSION))
3766 return (SET_ERROR(ENOTSUP));
3768 if (intval == ZIO_COMPRESS_LZ4) {
3769 spa_t *spa;
3771 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3772 return (err);
3774 if (!spa_feature_is_enabled(spa,
3775 SPA_FEATURE_LZ4_COMPRESS)) {
3776 spa_close(spa, FTAG);
3777 return (SET_ERROR(ENOTSUP));
3779 spa_close(spa, FTAG);
3783 * If this is a bootable dataset then
3784 * verify that the compression algorithm
3785 * is supported for booting. We must return
3786 * something other than ENOTSUP since it
3787 * implies a downrev pool version.
3789 if (zfs_is_bootfs(dsname) &&
3790 !BOOTFS_COMPRESS_VALID(intval)) {
3791 return (SET_ERROR(ERANGE));
3794 break;
3796 case ZFS_PROP_COPIES:
3797 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3798 return (SET_ERROR(ENOTSUP));
3799 break;
3801 case ZFS_PROP_RECORDSIZE:
3802 /* Record sizes above 128k need the feature to be enabled */
3803 if (nvpair_value_uint64(pair, &intval) == 0 &&
3804 intval > SPA_OLD_MAXBLOCKSIZE) {
3805 spa_t *spa;
3808 * We don't allow setting the property above 1MB,
3809 * unless the tunable has been changed.
3811 if (intval > zfs_max_recordsize ||
3812 intval > SPA_MAXBLOCKSIZE)
3813 return (SET_ERROR(ERANGE));
3815 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3816 return (err);
3818 if (!spa_feature_is_enabled(spa,
3819 SPA_FEATURE_LARGE_BLOCKS)) {
3820 spa_close(spa, FTAG);
3821 return (SET_ERROR(ENOTSUP));
3823 spa_close(spa, FTAG);
3825 break;
3827 case ZFS_PROP_SHARESMB:
3828 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3829 return (SET_ERROR(ENOTSUP));
3830 break;
3832 case ZFS_PROP_ACLINHERIT:
3833 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3834 nvpair_value_uint64(pair, &intval) == 0) {
3835 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3836 zfs_earlier_version(dsname,
3837 SPA_VERSION_PASSTHROUGH_X))
3838 return (SET_ERROR(ENOTSUP));
3840 break;
3842 case ZFS_PROP_CHECKSUM:
3843 case ZFS_PROP_DEDUP:
3845 spa_feature_t feature;
3846 spa_t *spa;
3848 /* dedup feature version checks */
3849 if (prop == ZFS_PROP_DEDUP &&
3850 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3851 return (SET_ERROR(ENOTSUP));
3853 if (nvpair_value_uint64(pair, &intval) != 0)
3854 return (SET_ERROR(EINVAL));
3856 /* check prop value is enabled in features */
3857 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3858 if (feature == SPA_FEATURE_NONE)
3859 break;
3861 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3862 return (err);
3864 if (!spa_feature_is_enabled(spa, feature)) {
3865 spa_close(spa, FTAG);
3866 return (SET_ERROR(ENOTSUP));
3868 spa_close(spa, FTAG);
3869 break;
3873 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3877 * Checks for a race condition to make sure we don't increment a feature flag
3878 * multiple times.
3880 static int
3881 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3883 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3884 spa_feature_t *featurep = arg;
3886 if (!spa_feature_is_active(spa, *featurep))
3887 return (0);
3888 else
3889 return (SET_ERROR(EBUSY));
3893 * The callback invoked on feature activation in the sync task caused by
3894 * zfs_prop_activate_feature.
3896 static void
3897 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3899 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3900 spa_feature_t *featurep = arg;
3902 spa_feature_incr(spa, *featurep, tx);
3906 * Activates a feature on a pool in response to a property setting. This
3907 * creates a new sync task which modifies the pool to reflect the feature
3908 * as being active.
3910 static int
3911 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
3913 int err;
3915 /* EBUSY here indicates that the feature is already active */
3916 err = dsl_sync_task(spa_name(spa),
3917 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
3918 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
3920 if (err != 0 && err != EBUSY)
3921 return (err);
3922 else
3923 return (0);
3927 * Removes properties from the given props list that fail permission checks
3928 * needed to clear them and to restore them in case of a receive error. For each
3929 * property, make sure we have both set and inherit permissions.
3931 * Returns the first error encountered if any permission checks fail. If the
3932 * caller provides a non-NULL errlist, it also gives the complete list of names
3933 * of all the properties that failed a permission check along with the
3934 * corresponding error numbers. The caller is responsible for freeing the
3935 * returned errlist.
3937 * If every property checks out successfully, zero is returned and the list
3938 * pointed at by errlist is NULL.
3940 static int
3941 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3943 zfs_cmd_t *zc;
3944 nvpair_t *pair, *next_pair;
3945 nvlist_t *errors;
3946 int err, rv = 0;
3948 if (props == NULL)
3949 return (0);
3951 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3953 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3954 (void) strcpy(zc->zc_name, dataset);
3955 pair = nvlist_next_nvpair(props, NULL);
3956 while (pair != NULL) {
3957 next_pair = nvlist_next_nvpair(props, pair);
3959 (void) strcpy(zc->zc_value, nvpair_name(pair));
3960 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3961 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
3962 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3963 VERIFY(nvlist_add_int32(errors,
3964 zc->zc_value, err) == 0);
3966 pair = next_pair;
3968 kmem_free(zc, sizeof (zfs_cmd_t));
3970 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3971 nvlist_free(errors);
3972 errors = NULL;
3973 } else {
3974 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3977 if (errlist == NULL)
3978 nvlist_free(errors);
3979 else
3980 *errlist = errors;
3982 return (rv);
3985 static boolean_t
3986 propval_equals(nvpair_t *p1, nvpair_t *p2)
3988 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3989 /* dsl_prop_get_all_impl() format */
3990 nvlist_t *attrs;
3991 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3992 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3993 &p1) == 0);
3996 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3997 nvlist_t *attrs;
3998 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3999 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4000 &p2) == 0);
4003 if (nvpair_type(p1) != nvpair_type(p2))
4004 return (B_FALSE);
4006 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4007 char *valstr1, *valstr2;
4009 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4010 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4011 return (strcmp(valstr1, valstr2) == 0);
4012 } else {
4013 uint64_t intval1, intval2;
4015 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4016 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4017 return (intval1 == intval2);
4022 * Remove properties from props if they are not going to change (as determined
4023 * by comparison with origprops). Remove them from origprops as well, since we
4024 * do not need to clear or restore properties that won't change.
4026 static void
4027 props_reduce(nvlist_t *props, nvlist_t *origprops)
4029 nvpair_t *pair, *next_pair;
4031 if (origprops == NULL)
4032 return; /* all props need to be received */
4034 pair = nvlist_next_nvpair(props, NULL);
4035 while (pair != NULL) {
4036 const char *propname = nvpair_name(pair);
4037 nvpair_t *match;
4039 next_pair = nvlist_next_nvpair(props, pair);
4041 if ((nvlist_lookup_nvpair(origprops, propname,
4042 &match) != 0) || !propval_equals(pair, match))
4043 goto next; /* need to set received value */
4045 /* don't clear the existing received value */
4046 (void) nvlist_remove_nvpair(origprops, match);
4047 /* don't bother receiving the property */
4048 (void) nvlist_remove_nvpair(props, pair);
4049 next:
4050 pair = next_pair;
4055 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4056 * For example, refquota cannot be set until after the receipt of a dataset,
4057 * because in replication streams, an older/earlier snapshot may exceed the
4058 * refquota. We want to receive the older/earlier snapshot, but setting
4059 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4060 * the older/earlier snapshot from being received (with EDQUOT).
4062 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4064 * libzfs will need to be judicious handling errors encountered by props
4065 * extracted by this function.
4067 static nvlist_t *
4068 extract_delay_props(nvlist_t *props)
4070 nvlist_t *delayprops;
4071 nvpair_t *nvp, *tmp;
4072 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4073 int i;
4075 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4077 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4078 nvp = nvlist_next_nvpair(props, nvp)) {
4080 * strcmp() is safe because zfs_prop_to_name() always returns
4081 * a bounded string.
4083 for (i = 0; delayable[i] != 0; i++) {
4084 if (strcmp(zfs_prop_to_name(delayable[i]),
4085 nvpair_name(nvp)) == 0) {
4086 break;
4089 if (delayable[i] != 0) {
4090 tmp = nvlist_prev_nvpair(props, nvp);
4091 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4092 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4093 nvp = tmp;
4097 if (nvlist_empty(delayprops)) {
4098 nvlist_free(delayprops);
4099 delayprops = NULL;
4101 return (delayprops);
4104 #ifdef DEBUG
4105 static boolean_t zfs_ioc_recv_inject_err;
4106 #endif
4109 * inputs:
4110 * zc_name name of containing filesystem
4111 * zc_nvlist_src{_size} nvlist of properties to apply
4112 * zc_value name of snapshot to create
4113 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4114 * zc_cookie file descriptor to recv from
4115 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4116 * zc_guid force flag
4117 * zc_cleanup_fd cleanup-on-exit file descriptor
4118 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4119 * zc_resumable if data is incomplete assume sender will resume
4121 * outputs:
4122 * zc_cookie number of bytes read
4123 * zc_nvlist_dst{_size} error for each unapplied received property
4124 * zc_obj zprop_errflags_t
4125 * zc_action_handle handle for this guid/ds mapping
4127 static int
4128 zfs_ioc_recv(zfs_cmd_t *zc)
4130 file_t *fp;
4131 dmu_recv_cookie_t drc;
4132 boolean_t force = (boolean_t)zc->zc_guid;
4133 int fd;
4134 int error = 0;
4135 int props_error = 0;
4136 nvlist_t *errors;
4137 offset_t off;
4138 nvlist_t *props = NULL; /* sent properties */
4139 nvlist_t *origprops = NULL; /* existing properties */
4140 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4141 char *origin = NULL;
4142 char *tosnap;
4143 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4144 boolean_t first_recvd_props = B_FALSE;
4146 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4147 strchr(zc->zc_value, '@') == NULL ||
4148 strchr(zc->zc_value, '%'))
4149 return (SET_ERROR(EINVAL));
4151 (void) strcpy(tofs, zc->zc_value);
4152 tosnap = strchr(tofs, '@');
4153 *tosnap++ = '\0';
4155 if (zc->zc_nvlist_src != (uintptr_t)NULL &&
4156 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4157 zc->zc_iflags, &props)) != 0)
4158 return (error);
4160 fd = zc->zc_cookie;
4161 fp = getf(fd);
4162 if (fp == NULL) {
4163 nvlist_free(props);
4164 return (SET_ERROR(EBADF));
4167 errors = fnvlist_alloc();
4169 if (zc->zc_string[0])
4170 origin = zc->zc_string;
4172 error = dmu_recv_begin(tofs, tosnap,
4173 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4174 if (error != 0)
4175 goto out;
4178 * Set properties before we receive the stream so that they are applied
4179 * to the new data. Note that we must call dmu_recv_stream() if
4180 * dmu_recv_begin() succeeds.
4182 if (props != NULL && !drc.drc_newfs) {
4183 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4184 SPA_VERSION_RECVD_PROPS &&
4185 !dsl_prop_get_hasrecvd(tofs))
4186 first_recvd_props = B_TRUE;
4189 * If new received properties are supplied, they are to
4190 * completely replace the existing received properties, so stash
4191 * away the existing ones.
4193 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4194 nvlist_t *errlist = NULL;
4196 * Don't bother writing a property if its value won't
4197 * change (and avoid the unnecessary security checks).
4199 * The first receive after SPA_VERSION_RECVD_PROPS is a
4200 * special case where we blow away all local properties
4201 * regardless.
4203 if (!first_recvd_props)
4204 props_reduce(props, origprops);
4205 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4206 (void) nvlist_merge(errors, errlist, 0);
4207 nvlist_free(errlist);
4209 if (clear_received_props(tofs, origprops,
4210 first_recvd_props ? NULL : props) != 0)
4211 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4212 } else {
4213 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4217 if (props != NULL) {
4218 props_error = dsl_prop_set_hasrecvd(tofs);
4220 if (props_error == 0) {
4221 delayprops = extract_delay_props(props);
4222 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4223 props, errors);
4227 off = fp->f_offset;
4228 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4229 &zc->zc_action_handle);
4231 if (error == 0) {
4232 zfsvfs_t *zfsvfs = NULL;
4234 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4235 /* online recv */
4236 dsl_dataset_t *ds;
4237 int end_err;
4239 ds = dmu_objset_ds(zfsvfs->z_os);
4240 error = zfs_suspend_fs(zfsvfs);
4242 * If the suspend fails, then the recv_end will
4243 * likely also fail, and clean up after itself.
4245 end_err = dmu_recv_end(&drc, zfsvfs);
4246 if (error == 0)
4247 error = zfs_resume_fs(zfsvfs, ds);
4248 error = error ? error : end_err;
4249 VFS_RELE(zfsvfs->z_vfs);
4250 } else {
4251 error = dmu_recv_end(&drc, NULL);
4254 /* Set delayed properties now, after we're done receiving. */
4255 if (delayprops != NULL && error == 0) {
4256 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4257 delayprops, errors);
4261 if (delayprops != NULL) {
4263 * Merge delayed props back in with initial props, in case
4264 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4265 * we have to make sure clear_received_props() includes
4266 * the delayed properties).
4268 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4269 * using ASSERT() will be just like a VERIFY.
4271 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4272 nvlist_free(delayprops);
4276 * Now that all props, initial and delayed, are set, report the prop
4277 * errors to the caller.
4279 if (zc->zc_nvlist_dst_size != 0 &&
4280 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4281 put_nvlist(zc, errors) != 0)) {
4283 * Caller made zc->zc_nvlist_dst less than the minimum expected
4284 * size or supplied an invalid address.
4286 props_error = SET_ERROR(EINVAL);
4289 zc->zc_cookie = off - fp->f_offset;
4290 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4291 fp->f_offset = off;
4293 #ifdef DEBUG
4294 if (zfs_ioc_recv_inject_err) {
4295 zfs_ioc_recv_inject_err = B_FALSE;
4296 error = 1;
4298 #endif
4300 * On error, restore the original props.
4302 if (error != 0 && props != NULL && !drc.drc_newfs) {
4303 if (clear_received_props(tofs, props, NULL) != 0) {
4305 * We failed to clear the received properties.
4306 * Since we may have left a $recvd value on the
4307 * system, we can't clear the $hasrecvd flag.
4309 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4310 } else if (first_recvd_props) {
4311 dsl_prop_unset_hasrecvd(tofs);
4314 if (origprops == NULL && !drc.drc_newfs) {
4315 /* We failed to stash the original properties. */
4316 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4320 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4321 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4322 * explictly if we're restoring local properties cleared in the
4323 * first new-style receive.
4325 if (origprops != NULL &&
4326 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4327 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4328 origprops, NULL) != 0) {
4330 * We stashed the original properties but failed to
4331 * restore them.
4333 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4336 out:
4337 nvlist_free(props);
4338 nvlist_free(origprops);
4339 nvlist_free(errors);
4340 releasef(fd);
4342 if (error == 0)
4343 error = props_error;
4345 return (error);
4349 * inputs:
4350 * zc_name name of snapshot to send
4351 * zc_cookie file descriptor to send stream to
4352 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4353 * zc_sendobj objsetid of snapshot to send
4354 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4355 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4356 * output size in zc_objset_type.
4357 * zc_flags lzc_send_flags
4359 * outputs:
4360 * zc_objset_type estimated size, if zc_guid is set
4362 static int
4363 zfs_ioc_send(zfs_cmd_t *zc)
4365 int error;
4366 offset_t off;
4367 boolean_t estimate = (zc->zc_guid != 0);
4368 boolean_t embedok = (zc->zc_flags & 0x1);
4369 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4370 boolean_t compressok = (zc->zc_flags & 0x4);
4372 if (zc->zc_obj != 0) {
4373 dsl_pool_t *dp;
4374 dsl_dataset_t *tosnap;
4376 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4377 if (error != 0)
4378 return (error);
4380 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4381 if (error != 0) {
4382 dsl_pool_rele(dp, FTAG);
4383 return (error);
4386 if (dsl_dir_is_clone(tosnap->ds_dir))
4387 zc->zc_fromobj =
4388 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4389 dsl_dataset_rele(tosnap, FTAG);
4390 dsl_pool_rele(dp, FTAG);
4393 if (estimate) {
4394 dsl_pool_t *dp;
4395 dsl_dataset_t *tosnap;
4396 dsl_dataset_t *fromsnap = NULL;
4398 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4399 if (error != 0)
4400 return (error);
4402 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4403 if (error != 0) {
4404 dsl_pool_rele(dp, FTAG);
4405 return (error);
4408 if (zc->zc_fromobj != 0) {
4409 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4410 FTAG, &fromsnap);
4411 if (error != 0) {
4412 dsl_dataset_rele(tosnap, FTAG);
4413 dsl_pool_rele(dp, FTAG);
4414 return (error);
4418 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4419 &zc->zc_objset_type);
4421 if (fromsnap != NULL)
4422 dsl_dataset_rele(fromsnap, FTAG);
4423 dsl_dataset_rele(tosnap, FTAG);
4424 dsl_pool_rele(dp, FTAG);
4425 } else {
4426 file_t *fp = getf(zc->zc_cookie);
4427 if (fp == NULL)
4428 return (SET_ERROR(EBADF));
4430 off = fp->f_offset;
4431 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4432 zc->zc_fromobj, embedok, large_block_ok, compressok,
4433 zc->zc_cookie, fp->f_vnode, &off);
4435 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4436 fp->f_offset = off;
4437 releasef(zc->zc_cookie);
4439 return (error);
4443 * inputs:
4444 * zc_name name of snapshot on which to report progress
4445 * zc_cookie file descriptor of send stream
4447 * outputs:
4448 * zc_cookie number of bytes written in send stream thus far
4450 static int
4451 zfs_ioc_send_progress(zfs_cmd_t *zc)
4453 dsl_pool_t *dp;
4454 dsl_dataset_t *ds;
4455 dmu_sendarg_t *dsp = NULL;
4456 int error;
4458 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4459 if (error != 0)
4460 return (error);
4462 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4463 if (error != 0) {
4464 dsl_pool_rele(dp, FTAG);
4465 return (error);
4468 mutex_enter(&ds->ds_sendstream_lock);
4471 * Iterate over all the send streams currently active on this dataset.
4472 * If there's one which matches the specified file descriptor _and_ the
4473 * stream was started by the current process, return the progress of
4474 * that stream.
4476 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4477 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4478 if (dsp->dsa_outfd == zc->zc_cookie &&
4479 dsp->dsa_proc == curproc)
4480 break;
4483 if (dsp != NULL)
4484 zc->zc_cookie = *(dsp->dsa_off);
4485 else
4486 error = SET_ERROR(ENOENT);
4488 mutex_exit(&ds->ds_sendstream_lock);
4489 dsl_dataset_rele(ds, FTAG);
4490 dsl_pool_rele(dp, FTAG);
4491 return (error);
4494 static int
4495 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4497 int id, error;
4499 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4500 &zc->zc_inject_record);
4502 if (error == 0)
4503 zc->zc_guid = (uint64_t)id;
4505 return (error);
4508 static int
4509 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4511 return (zio_clear_fault((int)zc->zc_guid));
4514 static int
4515 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4517 int id = (int)zc->zc_guid;
4518 int error;
4520 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4521 &zc->zc_inject_record);
4523 zc->zc_guid = id;
4525 return (error);
4528 static int
4529 zfs_ioc_error_log(zfs_cmd_t *zc)
4531 spa_t *spa;
4532 int error;
4533 size_t count = (size_t)zc->zc_nvlist_dst_size;
4535 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4536 return (error);
4538 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4539 &count);
4540 if (error == 0)
4541 zc->zc_nvlist_dst_size = count;
4542 else
4543 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4545 spa_close(spa, FTAG);
4547 return (error);
4550 static int
4551 zfs_ioc_clear(zfs_cmd_t *zc)
4553 spa_t *spa;
4554 vdev_t *vd;
4555 int error;
4558 * On zpool clear we also fix up missing slogs
4560 mutex_enter(&spa_namespace_lock);
4561 spa = spa_lookup(zc->zc_name);
4562 if (spa == NULL) {
4563 mutex_exit(&spa_namespace_lock);
4564 return (SET_ERROR(EIO));
4566 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4567 /* we need to let spa_open/spa_load clear the chains */
4568 spa_set_log_state(spa, SPA_LOG_CLEAR);
4570 spa->spa_last_open_failed = 0;
4571 mutex_exit(&spa_namespace_lock);
4573 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4574 error = spa_open(zc->zc_name, &spa, FTAG);
4575 } else {
4576 nvlist_t *policy;
4577 nvlist_t *config = NULL;
4579 if (zc->zc_nvlist_src == (uintptr_t)NULL)
4580 return (SET_ERROR(EINVAL));
4582 if ((error = get_nvlist(zc->zc_nvlist_src,
4583 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4584 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4585 policy, &config);
4586 if (config != NULL) {
4587 int err;
4589 if ((err = put_nvlist(zc, config)) != 0)
4590 error = err;
4591 nvlist_free(config);
4593 nvlist_free(policy);
4597 if (error != 0)
4598 return (error);
4600 spa_vdev_state_enter(spa, SCL_NONE);
4602 if (zc->zc_guid == 0) {
4603 vd = NULL;
4604 } else {
4605 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4606 if (vd == NULL) {
4607 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4608 spa_close(spa, FTAG);
4609 return (SET_ERROR(ENODEV));
4613 vdev_clear(spa, vd);
4615 (void) spa_vdev_state_exit(spa, NULL, 0);
4618 * Resume any suspended I/Os.
4620 if (zio_resume(spa) != 0)
4621 error = SET_ERROR(EIO);
4623 spa_close(spa, FTAG);
4625 return (error);
4628 static int
4629 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4631 spa_t *spa;
4632 int error;
4634 error = spa_open(zc->zc_name, &spa, FTAG);
4635 if (error != 0)
4636 return (error);
4638 spa_vdev_state_enter(spa, SCL_NONE);
4641 * If a resilver is already in progress then set the
4642 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4643 * the scan as a side effect of the reopen. Otherwise, let
4644 * vdev_open() decided if a resilver is required.
4646 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4647 vdev_reopen(spa->spa_root_vdev);
4648 spa->spa_scrub_reopen = B_FALSE;
4650 (void) spa_vdev_state_exit(spa, NULL, 0);
4651 spa_close(spa, FTAG);
4652 return (0);
4655 * inputs:
4656 * zc_name name of filesystem
4658 * outputs:
4659 * zc_string name of conflicting snapshot, if there is one
4661 static int
4662 zfs_ioc_promote(zfs_cmd_t *zc)
4664 dsl_pool_t *dp;
4665 dsl_dataset_t *ds, *ods;
4666 char origin[ZFS_MAX_DATASET_NAME_LEN];
4667 char *cp;
4668 int error;
4670 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4671 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4672 strchr(zc->zc_name, '%'))
4673 return (SET_ERROR(EINVAL));
4675 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4676 if (error != 0)
4677 return (error);
4679 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4680 if (error != 0) {
4681 dsl_pool_rele(dp, FTAG);
4682 return (error);
4685 if (!dsl_dir_is_clone(ds->ds_dir)) {
4686 dsl_dataset_rele(ds, FTAG);
4687 dsl_pool_rele(dp, FTAG);
4688 return (SET_ERROR(EINVAL));
4691 error = dsl_dataset_hold_obj(dp,
4692 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4693 if (error != 0) {
4694 dsl_dataset_rele(ds, FTAG);
4695 dsl_pool_rele(dp, FTAG);
4696 return (error);
4699 dsl_dataset_name(ods, origin);
4700 dsl_dataset_rele(ods, FTAG);
4701 dsl_dataset_rele(ds, FTAG);
4702 dsl_pool_rele(dp, FTAG);
4705 * We don't need to unmount *all* the origin fs's snapshots, but
4706 * it's easier.
4708 cp = strchr(origin, '@');
4709 if (cp)
4710 *cp = '\0';
4711 (void) dmu_objset_find(origin,
4712 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4713 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4717 * Retrieve a single {user|group}{used|quota}@... property.
4719 * inputs:
4720 * zc_name name of filesystem
4721 * zc_objset_type zfs_userquota_prop_t
4722 * zc_value domain name (eg. "S-1-234-567-89")
4723 * zc_guid RID/UID/GID
4725 * outputs:
4726 * zc_cookie property value
4728 static int
4729 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4731 zfsvfs_t *zfsvfs;
4732 int error;
4734 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4735 return (SET_ERROR(EINVAL));
4737 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4738 if (error != 0)
4739 return (error);
4741 error = zfs_userspace_one(zfsvfs,
4742 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4743 zfsvfs_rele(zfsvfs, FTAG);
4745 return (error);
4749 * inputs:
4750 * zc_name name of filesystem
4751 * zc_cookie zap cursor
4752 * zc_objset_type zfs_userquota_prop_t
4753 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4755 * outputs:
4756 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4757 * zc_cookie zap cursor
4759 static int
4760 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4762 zfsvfs_t *zfsvfs;
4763 int bufsize = zc->zc_nvlist_dst_size;
4765 if (bufsize <= 0)
4766 return (SET_ERROR(ENOMEM));
4768 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4769 if (error != 0)
4770 return (error);
4772 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4774 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4775 buf, &zc->zc_nvlist_dst_size);
4777 if (error == 0) {
4778 error = xcopyout(buf,
4779 (void *)(uintptr_t)zc->zc_nvlist_dst,
4780 zc->zc_nvlist_dst_size);
4782 kmem_free(buf, bufsize);
4783 zfsvfs_rele(zfsvfs, FTAG);
4785 return (error);
4789 * inputs:
4790 * zc_name name of filesystem
4792 * outputs:
4793 * none
4795 static int
4796 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4798 objset_t *os;
4799 int error = 0;
4800 zfsvfs_t *zfsvfs;
4802 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4803 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4805 * If userused is not enabled, it may be because the
4806 * objset needs to be closed & reopened (to grow the
4807 * objset_phys_t). Suspend/resume the fs will do that.
4809 dsl_dataset_t *ds, *newds;
4811 ds = dmu_objset_ds(zfsvfs->z_os);
4812 error = zfs_suspend_fs(zfsvfs);
4813 if (error == 0) {
4814 dmu_objset_refresh_ownership(ds, &newds,
4815 zfsvfs);
4816 error = zfs_resume_fs(zfsvfs, newds);
4819 if (error == 0)
4820 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4821 VFS_RELE(zfsvfs->z_vfs);
4822 } else {
4823 /* XXX kind of reading contents without owning */
4824 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4825 if (error != 0)
4826 return (error);
4828 error = dmu_objset_userspace_upgrade(os);
4829 dmu_objset_rele(os, FTAG);
4832 return (error);
4836 * We don't want to have a hard dependency
4837 * against some special symbols in sharefs
4838 * nfs, and smbsrv. Determine them if needed when
4839 * the first file system is shared.
4840 * Neither sharefs, nfs or smbsrv are unloadable modules.
4842 int (*znfsexport_fs)(void *arg);
4843 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4844 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4846 int zfs_nfsshare_inited;
4847 int zfs_smbshare_inited;
4849 ddi_modhandle_t nfs_mod;
4850 ddi_modhandle_t sharefs_mod;
4851 ddi_modhandle_t smbsrv_mod;
4852 kmutex_t zfs_share_lock;
4854 static int
4855 zfs_init_sharefs()
4857 int error;
4859 ASSERT(MUTEX_HELD(&zfs_share_lock));
4860 /* Both NFS and SMB shares also require sharetab support. */
4861 if (sharefs_mod == NULL && ((sharefs_mod =
4862 ddi_modopen("fs/sharefs",
4863 KRTLD_MODE_FIRST, &error)) == NULL)) {
4864 return (SET_ERROR(ENOSYS));
4866 if (zshare_fs == NULL && ((zshare_fs =
4867 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4868 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4869 return (SET_ERROR(ENOSYS));
4871 return (0);
4874 static int
4875 zfs_ioc_share(zfs_cmd_t *zc)
4877 int error;
4878 int opcode;
4880 switch (zc->zc_share.z_sharetype) {
4881 case ZFS_SHARE_NFS:
4882 case ZFS_UNSHARE_NFS:
4883 if (zfs_nfsshare_inited == 0) {
4884 mutex_enter(&zfs_share_lock);
4885 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4886 KRTLD_MODE_FIRST, &error)) == NULL)) {
4887 mutex_exit(&zfs_share_lock);
4888 return (SET_ERROR(ENOSYS));
4890 if (znfsexport_fs == NULL &&
4891 ((znfsexport_fs = (int (*)(void *))
4892 ddi_modsym(nfs_mod,
4893 "nfs_export", &error)) == NULL)) {
4894 mutex_exit(&zfs_share_lock);
4895 return (SET_ERROR(ENOSYS));
4897 error = zfs_init_sharefs();
4898 if (error != 0) {
4899 mutex_exit(&zfs_share_lock);
4900 return (SET_ERROR(ENOSYS));
4902 zfs_nfsshare_inited = 1;
4903 mutex_exit(&zfs_share_lock);
4905 break;
4906 case ZFS_SHARE_SMB:
4907 case ZFS_UNSHARE_SMB:
4908 if (zfs_smbshare_inited == 0) {
4909 mutex_enter(&zfs_share_lock);
4910 if (smbsrv_mod == NULL && ((smbsrv_mod =
4911 ddi_modopen("drv/smbsrv",
4912 KRTLD_MODE_FIRST, &error)) == NULL)) {
4913 mutex_exit(&zfs_share_lock);
4914 return (SET_ERROR(ENOSYS));
4916 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4917 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4918 "smb_server_share", &error)) == NULL)) {
4919 mutex_exit(&zfs_share_lock);
4920 return (SET_ERROR(ENOSYS));
4922 error = zfs_init_sharefs();
4923 if (error != 0) {
4924 mutex_exit(&zfs_share_lock);
4925 return (SET_ERROR(ENOSYS));
4927 zfs_smbshare_inited = 1;
4928 mutex_exit(&zfs_share_lock);
4930 break;
4931 default:
4932 return (SET_ERROR(EINVAL));
4935 switch (zc->zc_share.z_sharetype) {
4936 case ZFS_SHARE_NFS:
4937 case ZFS_UNSHARE_NFS:
4938 if (error =
4939 znfsexport_fs((void *)
4940 (uintptr_t)zc->zc_share.z_exportdata))
4941 return (error);
4942 break;
4943 case ZFS_SHARE_SMB:
4944 case ZFS_UNSHARE_SMB:
4945 if (error = zsmbexport_fs((void *)
4946 (uintptr_t)zc->zc_share.z_exportdata,
4947 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4948 B_TRUE: B_FALSE)) {
4949 return (error);
4951 break;
4954 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4955 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4956 SHAREFS_ADD : SHAREFS_REMOVE;
4959 * Add or remove share from sharetab
4961 error = zshare_fs(opcode,
4962 (void *)(uintptr_t)zc->zc_share.z_sharedata,
4963 zc->zc_share.z_sharemax);
4965 return (error);
4969 ace_t full_access[] = {
4970 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4974 * inputs:
4975 * zc_name name of containing filesystem
4976 * zc_obj object # beyond which we want next in-use object #
4978 * outputs:
4979 * zc_obj next in-use object #
4981 static int
4982 zfs_ioc_next_obj(zfs_cmd_t *zc)
4984 objset_t *os = NULL;
4985 int error;
4987 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4988 if (error != 0)
4989 return (error);
4991 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4992 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
4994 dmu_objset_rele(os, FTAG);
4995 return (error);
4999 * inputs:
5000 * zc_name name of filesystem
5001 * zc_value prefix name for snapshot
5002 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5004 * outputs:
5005 * zc_value short name of new snapshot
5007 static int
5008 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5010 char *snap_name;
5011 char *hold_name;
5012 int error;
5013 minor_t minor;
5015 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5016 if (error != 0)
5017 return (error);
5019 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5020 (u_longlong_t)ddi_get_lbolt64());
5021 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5023 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5024 hold_name);
5025 if (error == 0)
5026 (void) strcpy(zc->zc_value, snap_name);
5027 strfree(snap_name);
5028 strfree(hold_name);
5029 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5030 return (error);
5034 * inputs:
5035 * zc_name name of "to" snapshot
5036 * zc_value name of "from" snapshot
5037 * zc_cookie file descriptor to write diff data on
5039 * outputs:
5040 * dmu_diff_record_t's to the file descriptor
5042 static int
5043 zfs_ioc_diff(zfs_cmd_t *zc)
5045 file_t *fp;
5046 offset_t off;
5047 int error;
5049 fp = getf(zc->zc_cookie);
5050 if (fp == NULL)
5051 return (SET_ERROR(EBADF));
5053 off = fp->f_offset;
5055 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5057 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5058 fp->f_offset = off;
5059 releasef(zc->zc_cookie);
5061 return (error);
5065 * Remove all ACL files in shares dir
5067 static int
5068 zfs_smb_acl_purge(znode_t *dzp)
5070 zap_cursor_t zc;
5071 zap_attribute_t zap;
5072 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5073 int error;
5075 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5076 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5077 zap_cursor_advance(&zc)) {
5078 if ((error = fop_remove(ZTOV(dzp), zap.za_name, kcred,
5079 NULL, 0)) != 0)
5080 break;
5082 zap_cursor_fini(&zc);
5083 return (error);
5086 static int
5087 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5089 vnode_t *vp;
5090 znode_t *dzp;
5091 vnode_t *resourcevp = NULL;
5092 znode_t *sharedir;
5093 zfsvfs_t *zfsvfs;
5094 nvlist_t *nvlist;
5095 char *src, *target;
5096 vattr_t vattr;
5097 vsecattr_t vsec;
5098 int error = 0;
5100 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5101 NO_FOLLOW, NULL, &vp)) != 0)
5102 return (error);
5104 /* Now make sure mntpnt and dataset are ZFS */
5106 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5107 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5108 zc->zc_name) != 0)) {
5109 VN_RELE(vp);
5110 return (SET_ERROR(EINVAL));
5113 dzp = VTOZ(vp);
5114 zfsvfs = dzp->z_zfsvfs;
5115 ZFS_ENTER(zfsvfs);
5118 * Create share dir if its missing.
5120 mutex_enter(&zfsvfs->z_lock);
5121 if (zfsvfs->z_shares_dir == 0) {
5122 dmu_tx_t *tx;
5124 tx = dmu_tx_create(zfsvfs->z_os);
5125 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5126 ZFS_SHARES_DIR);
5127 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5128 error = dmu_tx_assign(tx, TXG_WAIT);
5129 if (error != 0) {
5130 dmu_tx_abort(tx);
5131 } else {
5132 error = zfs_create_share_dir(zfsvfs, tx);
5133 dmu_tx_commit(tx);
5135 if (error != 0) {
5136 mutex_exit(&zfsvfs->z_lock);
5137 VN_RELE(vp);
5138 ZFS_EXIT(zfsvfs);
5139 return (error);
5142 mutex_exit(&zfsvfs->z_lock);
5144 ASSERT(zfsvfs->z_shares_dir);
5145 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5146 VN_RELE(vp);
5147 ZFS_EXIT(zfsvfs);
5148 return (error);
5151 switch (zc->zc_cookie) {
5152 case ZFS_SMB_ACL_ADD:
5153 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5154 vattr.va_type = VREG;
5155 vattr.va_mode = S_IFREG|0777;
5156 vattr.va_uid = 0;
5157 vattr.va_gid = 0;
5159 vsec.vsa_mask = VSA_ACE;
5160 vsec.vsa_aclentp = &full_access;
5161 vsec.vsa_aclentsz = sizeof (full_access);
5162 vsec.vsa_aclcnt = 1;
5164 error = fop_create(ZTOV(sharedir), zc->zc_string,
5165 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5166 if (resourcevp)
5167 VN_RELE(resourcevp);
5168 break;
5170 case ZFS_SMB_ACL_REMOVE:
5171 error = fop_remove(ZTOV(sharedir), zc->zc_string, kcred,
5172 NULL, 0);
5173 break;
5175 case ZFS_SMB_ACL_RENAME:
5176 if ((error = get_nvlist(zc->zc_nvlist_src,
5177 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5178 VN_RELE(vp);
5179 VN_RELE(ZTOV(sharedir));
5180 ZFS_EXIT(zfsvfs);
5181 return (error);
5183 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5184 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5185 &target)) {
5186 VN_RELE(vp);
5187 VN_RELE(ZTOV(sharedir));
5188 ZFS_EXIT(zfsvfs);
5189 nvlist_free(nvlist);
5190 return (error);
5192 error = fop_rename(ZTOV(sharedir), src, ZTOV(sharedir), target,
5193 kcred, NULL, 0);
5194 nvlist_free(nvlist);
5195 break;
5197 case ZFS_SMB_ACL_PURGE:
5198 error = zfs_smb_acl_purge(sharedir);
5199 break;
5201 default:
5202 error = SET_ERROR(EINVAL);
5203 break;
5206 VN_RELE(vp);
5207 VN_RELE(ZTOV(sharedir));
5209 ZFS_EXIT(zfsvfs);
5211 return (error);
5215 * innvl: {
5216 * "holds" -> { snapname -> holdname (string), ... }
5217 * (optional) "cleanup_fd" -> fd (int32)
5220 * outnvl: {
5221 * snapname -> error value (int32)
5222 * ...
5225 /* ARGSUSED */
5226 static int
5227 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5229 nvpair_t *pair;
5230 nvlist_t *holds;
5231 int cleanup_fd = -1;
5232 int error;
5233 minor_t minor = 0;
5235 error = nvlist_lookup_nvlist(args, "holds", &holds);
5236 if (error != 0)
5237 return (SET_ERROR(EINVAL));
5239 /* make sure the user didn't pass us any invalid (empty) tags */
5240 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5241 pair = nvlist_next_nvpair(holds, pair)) {
5242 char *htag;
5244 error = nvpair_value_string(pair, &htag);
5245 if (error != 0)
5246 return (SET_ERROR(error));
5248 if (strlen(htag) == 0)
5249 return (SET_ERROR(EINVAL));
5252 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5253 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5254 if (error != 0)
5255 return (error);
5258 error = dsl_dataset_user_hold(holds, minor, errlist);
5259 if (minor != 0)
5260 zfs_onexit_fd_rele(cleanup_fd);
5261 return (error);
5265 * innvl is not used.
5267 * outnvl: {
5268 * holdname -> time added (uint64 seconds since epoch)
5269 * ...
5272 /* ARGSUSED */
5273 static int
5274 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5276 return (dsl_dataset_get_holds(snapname, outnvl));
5280 * innvl: {
5281 * snapname -> { holdname, ... }
5282 * ...
5285 * outnvl: {
5286 * snapname -> error value (int32)
5287 * ...
5290 /* ARGSUSED */
5291 static int
5292 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5294 return (dsl_dataset_user_release(holds, errlist));
5298 * inputs:
5299 * zc_name name of new filesystem or snapshot
5300 * zc_value full name of old snapshot
5302 * outputs:
5303 * zc_cookie space in bytes
5304 * zc_objset_type compressed space in bytes
5305 * zc_perm_action uncompressed space in bytes
5307 static int
5308 zfs_ioc_space_written(zfs_cmd_t *zc)
5310 int error;
5311 dsl_pool_t *dp;
5312 dsl_dataset_t *new, *old;
5314 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5315 if (error != 0)
5316 return (error);
5317 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5318 if (error != 0) {
5319 dsl_pool_rele(dp, FTAG);
5320 return (error);
5322 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5323 if (error != 0) {
5324 dsl_dataset_rele(new, FTAG);
5325 dsl_pool_rele(dp, FTAG);
5326 return (error);
5329 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5330 &zc->zc_objset_type, &zc->zc_perm_action);
5331 dsl_dataset_rele(old, FTAG);
5332 dsl_dataset_rele(new, FTAG);
5333 dsl_pool_rele(dp, FTAG);
5334 return (error);
5338 * innvl: {
5339 * "firstsnap" -> snapshot name
5342 * outnvl: {
5343 * "used" -> space in bytes
5344 * "compressed" -> compressed space in bytes
5345 * "uncompressed" -> uncompressed space in bytes
5348 static int
5349 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5351 int error;
5352 dsl_pool_t *dp;
5353 dsl_dataset_t *new, *old;
5354 char *firstsnap;
5355 uint64_t used, comp, uncomp;
5357 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5358 return (SET_ERROR(EINVAL));
5360 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5361 if (error != 0)
5362 return (error);
5364 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5365 if (error == 0 && !new->ds_is_snapshot) {
5366 dsl_dataset_rele(new, FTAG);
5367 error = SET_ERROR(EINVAL);
5369 if (error != 0) {
5370 dsl_pool_rele(dp, FTAG);
5371 return (error);
5373 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5374 if (error == 0 && !old->ds_is_snapshot) {
5375 dsl_dataset_rele(old, FTAG);
5376 error = SET_ERROR(EINVAL);
5378 if (error != 0) {
5379 dsl_dataset_rele(new, FTAG);
5380 dsl_pool_rele(dp, FTAG);
5381 return (error);
5384 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5385 dsl_dataset_rele(old, FTAG);
5386 dsl_dataset_rele(new, FTAG);
5387 dsl_pool_rele(dp, FTAG);
5388 fnvlist_add_uint64(outnvl, "used", used);
5389 fnvlist_add_uint64(outnvl, "compressed", comp);
5390 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5391 return (error);
5395 * innvl: {
5396 * "fd" -> file descriptor to write stream to (int32)
5397 * (optional) "fromsnap" -> full snap name to send an incremental from
5398 * (optional) "largeblockok" -> (value ignored)
5399 * indicates that blocks > 128KB are permitted
5400 * (optional) "embedok" -> (value ignored)
5401 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5402 * (optional) "compressok" -> (value ignored)
5403 * presence indicates compressed DRR_WRITE records are permitted
5404 * (optional) "resume_object" and "resume_offset" -> (uint64)
5405 * if present, resume send stream from specified object and offset.
5408 * outnvl is unused
5410 /* ARGSUSED */
5411 static int
5412 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5414 int error;
5415 offset_t off;
5416 char *fromname = NULL;
5417 int fd;
5418 boolean_t largeblockok;
5419 boolean_t embedok;
5420 boolean_t compressok;
5421 uint64_t resumeobj = 0;
5422 uint64_t resumeoff = 0;
5424 error = nvlist_lookup_int32(innvl, "fd", &fd);
5425 if (error != 0)
5426 return (SET_ERROR(EINVAL));
5428 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5430 largeblockok = nvlist_exists(innvl, "largeblockok");
5431 embedok = nvlist_exists(innvl, "embedok");
5432 compressok = nvlist_exists(innvl, "compressok");
5434 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5435 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5437 file_t *fp = getf(fd);
5438 if (fp == NULL)
5439 return (SET_ERROR(EBADF));
5441 off = fp->f_offset;
5442 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5443 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5445 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5446 fp->f_offset = off;
5447 releasef(fd);
5448 return (error);
5452 * Determine approximately how large a zfs send stream will be -- the number
5453 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5455 * innvl: {
5456 * (optional) "from" -> full snap or bookmark name to send an incremental
5457 * from
5458 * (optional) "largeblockok" -> (value ignored)
5459 * indicates that blocks > 128KB are permitted
5460 * (optional) "embedok" -> (value ignored)
5461 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5462 * (optional) "compressok" -> (value ignored)
5463 * presence indicates compressed DRR_WRITE records are permitted
5466 * outnvl: {
5467 * "space" -> bytes of space (uint64)
5470 static int
5471 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5473 dsl_pool_t *dp;
5474 dsl_dataset_t *tosnap;
5475 int error;
5476 char *fromname;
5477 boolean_t compressok;
5478 uint64_t space;
5480 error = dsl_pool_hold(snapname, FTAG, &dp);
5481 if (error != 0)
5482 return (error);
5484 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5485 if (error != 0) {
5486 dsl_pool_rele(dp, FTAG);
5487 return (error);
5490 compressok = nvlist_exists(innvl, "compressok");
5492 error = nvlist_lookup_string(innvl, "from", &fromname);
5493 if (error == 0) {
5494 if (strchr(fromname, '@') != NULL) {
5496 * If from is a snapshot, hold it and use the more
5497 * efficient dmu_send_estimate to estimate send space
5498 * size using deadlists.
5500 dsl_dataset_t *fromsnap;
5501 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5502 if (error != 0)
5503 goto out;
5504 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5505 &space);
5506 dsl_dataset_rele(fromsnap, FTAG);
5507 } else if (strchr(fromname, '#') != NULL) {
5509 * If from is a bookmark, fetch the creation TXG of the
5510 * snapshot it was created from and use that to find
5511 * blocks that were born after it.
5513 zfs_bookmark_phys_t frombm;
5515 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5516 &frombm);
5517 if (error != 0)
5518 goto out;
5519 error = dmu_send_estimate_from_txg(tosnap,
5520 frombm.zbm_creation_txg, compressok, &space);
5521 } else {
5523 * from is not properly formatted as a snapshot or
5524 * bookmark
5526 error = SET_ERROR(EINVAL);
5527 goto out;
5529 } else {
5531 * If estimating the size of a full send, use dmu_send_estimate.
5533 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5536 fnvlist_add_uint64(outnvl, "space", space);
5538 out:
5539 dsl_dataset_rele(tosnap, FTAG);
5540 dsl_pool_rele(dp, FTAG);
5541 return (error);
5544 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5546 static void
5547 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5548 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5549 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5551 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5553 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5554 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5555 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5556 ASSERT3P(vec->zvec_func, ==, NULL);
5558 vec->zvec_legacy_func = func;
5559 vec->zvec_secpolicy = secpolicy;
5560 vec->zvec_namecheck = namecheck;
5561 vec->zvec_allow_log = log_history;
5562 vec->zvec_pool_check = pool_check;
5566 * See the block comment at the beginning of this file for details on
5567 * each argument to this function.
5569 static void
5570 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5571 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5572 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5573 boolean_t allow_log)
5575 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5577 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5578 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5579 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5580 ASSERT3P(vec->zvec_func, ==, NULL);
5582 /* if we are logging, the name must be valid */
5583 ASSERT(!allow_log || namecheck != NO_NAME);
5585 vec->zvec_name = name;
5586 vec->zvec_func = func;
5587 vec->zvec_secpolicy = secpolicy;
5588 vec->zvec_namecheck = namecheck;
5589 vec->zvec_pool_check = pool_check;
5590 vec->zvec_smush_outnvlist = smush_outnvlist;
5591 vec->zvec_allow_log = allow_log;
5594 static void
5595 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5596 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5597 zfs_ioc_poolcheck_t pool_check)
5599 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5600 POOL_NAME, log_history, pool_check);
5603 static void
5604 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5605 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5607 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5608 DATASET_NAME, B_FALSE, pool_check);
5611 static void
5612 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5614 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5615 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5618 static void
5619 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5620 zfs_secpolicy_func_t *secpolicy)
5622 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5623 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5626 static void
5627 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5628 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5630 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5631 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5634 static void
5635 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5637 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5638 zfs_secpolicy_read);
5641 static void
5642 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5643 zfs_secpolicy_func_t *secpolicy)
5645 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5646 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5649 static void
5650 zfs_ioctl_init(void)
5652 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5653 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5654 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5656 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5657 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5658 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5660 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5661 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5662 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5664 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5665 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5666 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5668 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5669 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5670 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5672 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5673 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5674 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5676 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5677 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5678 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5680 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
5681 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
5682 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5684 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5685 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5686 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5688 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5689 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5690 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5691 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5692 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5693 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5695 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5696 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5697 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5699 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5700 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5701 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5703 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5704 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5705 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5707 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5708 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5709 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5711 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5712 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5713 POOL_NAME,
5714 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5716 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5717 zfs_ioc_channel_program, zfs_secpolicy_config,
5718 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5719 B_TRUE);
5721 /* IOCTLS that use the legacy function signature */
5723 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5724 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5726 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5727 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5728 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5729 zfs_ioc_pool_scan);
5730 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5731 zfs_ioc_pool_upgrade);
5732 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5733 zfs_ioc_vdev_add);
5734 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5735 zfs_ioc_vdev_remove);
5736 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5737 zfs_ioc_vdev_set_state);
5738 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5739 zfs_ioc_vdev_attach);
5740 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5741 zfs_ioc_vdev_detach);
5742 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5743 zfs_ioc_vdev_setpath);
5744 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5745 zfs_ioc_vdev_setfru);
5746 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5747 zfs_ioc_pool_set_props);
5748 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5749 zfs_ioc_vdev_split);
5750 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5751 zfs_ioc_pool_reguid);
5753 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5754 zfs_ioc_pool_configs, zfs_secpolicy_none);
5755 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5756 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5757 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5758 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5759 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5760 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5761 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5762 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5765 * pool destroy, and export don't log the history as part of
5766 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5767 * does the logging of those commands.
5769 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5770 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5771 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5772 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5774 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5775 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5776 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5777 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5779 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5780 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5781 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5782 zfs_ioc_dsobj_to_dsname,
5783 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5784 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5785 zfs_ioc_pool_get_history,
5786 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5788 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5789 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5791 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5792 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
5793 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5794 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5796 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5797 zfs_ioc_space_written);
5798 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5799 zfs_ioc_objset_recvd_props);
5800 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5801 zfs_ioc_next_obj);
5802 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5803 zfs_ioc_get_fsacl);
5804 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5805 zfs_ioc_objset_stats);
5806 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5807 zfs_ioc_objset_zplprops);
5808 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5809 zfs_ioc_dataset_list_next);
5810 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5811 zfs_ioc_snapshot_list_next);
5812 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5813 zfs_ioc_send_progress);
5815 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5816 zfs_ioc_diff, zfs_secpolicy_diff);
5817 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5818 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5819 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5820 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5821 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5822 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5823 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5824 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5825 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5826 zfs_ioc_send, zfs_secpolicy_send);
5828 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5829 zfs_secpolicy_none);
5830 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5831 zfs_secpolicy_destroy);
5832 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5833 zfs_secpolicy_rename);
5834 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5835 zfs_secpolicy_recv);
5836 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5837 zfs_secpolicy_promote);
5838 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5839 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5840 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5841 zfs_secpolicy_set_fsacl);
5843 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5844 zfs_secpolicy_share, POOL_CHECK_NONE);
5845 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5846 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5847 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5848 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5849 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5850 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5851 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5852 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5856 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5857 zfs_ioc_poolcheck_t check)
5859 spa_t *spa;
5860 int error;
5862 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5864 if (check & POOL_CHECK_NONE)
5865 return (0);
5867 error = spa_open(name, &spa, FTAG);
5868 if (error == 0) {
5869 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5870 error = SET_ERROR(EAGAIN);
5871 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5872 error = SET_ERROR(EROFS);
5873 spa_close(spa, FTAG);
5875 return (error);
5879 * Find a free minor number.
5881 minor_t
5882 zfsdev_minor_alloc(void)
5884 static minor_t last_minor;
5885 minor_t m;
5887 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5889 for (m = last_minor + 1; m != last_minor; m++) {
5890 if (m > ZFSDEV_MAX_MINOR)
5891 m = 1;
5892 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
5893 last_minor = m;
5894 return (m);
5898 return (0);
5901 static int
5902 zfs_ctldev_init(dev_t *devp)
5904 minor_t minor;
5905 zfs_soft_state_t *zs;
5907 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5908 ASSERT(getminor(*devp) == 0);
5910 minor = zfsdev_minor_alloc();
5911 if (minor == 0)
5912 return (SET_ERROR(ENXIO));
5914 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
5915 return (SET_ERROR(EAGAIN));
5917 *devp = makedevice(getemajor(*devp), minor);
5919 zs = ddi_get_soft_state(zfsdev_state, minor);
5920 zs->zss_type = ZSST_CTLDEV;
5921 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
5923 return (0);
5926 static void
5927 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
5929 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5931 zfs_onexit_destroy(zo);
5932 ddi_soft_state_free(zfsdev_state, minor);
5935 void *
5936 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
5938 zfs_soft_state_t *zp;
5940 zp = ddi_get_soft_state(zfsdev_state, minor);
5941 if (zp == NULL || zp->zss_type != which)
5942 return (NULL);
5944 return (zp->zss_data);
5947 static int
5948 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
5950 int error = 0;
5952 if (getminor(*devp) != 0)
5953 return (zvol_open(devp, flag, otyp, cr));
5955 /* This is the control device. Allocate a new minor if requested. */
5956 if (flag & FEXCL) {
5957 mutex_enter(&zfsdev_state_lock);
5958 error = zfs_ctldev_init(devp);
5959 mutex_exit(&zfsdev_state_lock);
5962 return (error);
5965 static int
5966 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
5968 zfs_onexit_t *zo;
5969 minor_t minor = getminor(dev);
5971 if (minor == 0)
5972 return (0);
5974 mutex_enter(&zfsdev_state_lock);
5975 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
5976 if (zo == NULL) {
5977 mutex_exit(&zfsdev_state_lock);
5978 return (zvol_close(dev, flag, otyp, cr));
5980 zfs_ctldev_destroy(zo, minor);
5981 mutex_exit(&zfsdev_state_lock);
5983 return (0);
5986 static int
5987 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
5989 zfs_cmd_t *zc;
5990 uint_t vecnum;
5991 int error, rc, len;
5992 minor_t minor = getminor(dev);
5993 const zfs_ioc_vec_t *vec;
5994 char *saved_poolname = NULL;
5995 nvlist_t *innvl = NULL;
5997 if (minor != 0 &&
5998 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
5999 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6001 vecnum = cmd - ZFS_IOC_FIRST;
6002 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6004 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6005 return (SET_ERROR(EINVAL));
6006 vec = &zfs_ioc_vec[vecnum];
6008 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6010 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6011 if (error != 0) {
6012 error = SET_ERROR(EFAULT);
6013 goto out;
6016 zc->zc_iflags = flag & FKIOCTL;
6017 if (zc->zc_nvlist_src_size != 0) {
6018 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6019 zc->zc_iflags, &innvl);
6020 if (error != 0)
6021 goto out;
6025 * Ensure that all pool/dataset names are valid before we pass down to
6026 * the lower layers.
6028 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6029 switch (vec->zvec_namecheck) {
6030 case POOL_NAME:
6031 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6032 error = SET_ERROR(EINVAL);
6033 else
6034 error = pool_status_check(zc->zc_name,
6035 vec->zvec_namecheck, vec->zvec_pool_check);
6036 break;
6038 case DATASET_NAME:
6039 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6040 error = SET_ERROR(EINVAL);
6041 else
6042 error = pool_status_check(zc->zc_name,
6043 vec->zvec_namecheck, vec->zvec_pool_check);
6044 break;
6046 case NO_NAME:
6047 break;
6051 if (error == 0)
6052 error = vec->zvec_secpolicy(zc, innvl, cr);
6054 if (error != 0)
6055 goto out;
6057 /* legacy ioctls can modify zc_name */
6058 len = strcspn(zc->zc_name, "/@#") + 1;
6059 saved_poolname = kmem_alloc(len, KM_SLEEP);
6060 (void) strlcpy(saved_poolname, zc->zc_name, len);
6062 if (vec->zvec_func != NULL) {
6063 nvlist_t *outnvl;
6064 int puterror = 0;
6065 spa_t *spa;
6066 nvlist_t *lognv = NULL;
6068 ASSERT(vec->zvec_legacy_func == NULL);
6071 * Add the innvl to the lognv before calling the func,
6072 * in case the func changes the innvl.
6074 if (vec->zvec_allow_log) {
6075 lognv = fnvlist_alloc();
6076 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6077 vec->zvec_name);
6078 if (!nvlist_empty(innvl)) {
6079 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6080 innvl);
6084 outnvl = fnvlist_alloc();
6085 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6088 * Some commands can partially execute, modfiy state, and still
6089 * return an error. In these cases, attempt to record what
6090 * was modified.
6092 if ((error == 0 ||
6093 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6094 vec->zvec_allow_log &&
6095 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6096 if (!nvlist_empty(outnvl)) {
6097 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6098 outnvl);
6100 if (error != 0) {
6101 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6102 error);
6104 (void) spa_history_log_nvl(spa, lognv);
6105 spa_close(spa, FTAG);
6107 fnvlist_free(lognv);
6109 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6110 int smusherror = 0;
6111 if (vec->zvec_smush_outnvlist) {
6112 smusherror = nvlist_smush(outnvl,
6113 zc->zc_nvlist_dst_size);
6115 if (smusherror == 0)
6116 puterror = put_nvlist(zc, outnvl);
6119 if (puterror != 0)
6120 error = puterror;
6122 nvlist_free(outnvl);
6123 } else {
6124 error = vec->zvec_legacy_func(zc);
6127 out:
6128 nvlist_free(innvl);
6129 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6130 if (error == 0 && rc != 0)
6131 error = SET_ERROR(EFAULT);
6132 if (error == 0 && vec->zvec_allow_log) {
6133 char *s = tsd_get(zfs_allow_log_key);
6134 if (s != NULL)
6135 strfree(s);
6136 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6137 } else {
6138 if (saved_poolname != NULL)
6139 strfree(saved_poolname);
6142 kmem_free(zc, sizeof (zfs_cmd_t));
6143 return (error);
6146 static int
6147 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6149 if (cmd != DDI_ATTACH)
6150 return (DDI_FAILURE);
6152 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6153 DDI_PSEUDO, 0) == DDI_FAILURE)
6154 return (DDI_FAILURE);
6156 zfs_dip = dip;
6158 ddi_report_dev(dip);
6160 return (DDI_SUCCESS);
6163 static int
6164 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6166 if (spa_busy() || zfs_busy() || zvol_busy())
6167 return (DDI_FAILURE);
6169 if (cmd != DDI_DETACH)
6170 return (DDI_FAILURE);
6172 zfs_dip = NULL;
6174 ddi_prop_remove_all(dip);
6175 ddi_remove_minor_node(dip, NULL);
6177 return (DDI_SUCCESS);
6180 /*ARGSUSED*/
6181 static int
6182 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6184 switch (infocmd) {
6185 case DDI_INFO_DEVT2DEVINFO:
6186 *result = zfs_dip;
6187 return (DDI_SUCCESS);
6189 case DDI_INFO_DEVT2INSTANCE:
6190 *result = NULL;
6191 return (DDI_SUCCESS);
6194 return (DDI_FAILURE);
6198 * OK, so this is a little weird.
6200 * /dev/zfs is the control node, i.e. minor 0.
6201 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6203 * /dev/zfs has basically nothing to do except serve up ioctls,
6204 * so most of the standard driver entry points are in zvol.c.
6206 static struct cb_ops zfs_cb_ops = {
6207 zfsdev_open, /* open */
6208 zfsdev_close, /* close */
6209 zvol_strategy, /* strategy */
6210 nodev, /* print */
6211 zvol_dump, /* dump */
6212 zvol_read, /* read */
6213 zvol_write, /* write */
6214 zfsdev_ioctl, /* ioctl */
6215 nodev, /* devmap */
6216 nodev, /* mmap */
6217 nodev, /* segmap */
6218 nochpoll, /* poll */
6219 ddi_prop_op, /* prop_op */
6220 NULL, /* streamtab */
6221 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6222 CB_REV, /* version */
6223 nodev, /* async read */
6224 nodev, /* async write */
6227 static struct dev_ops zfs_dev_ops = {
6228 DEVO_REV, /* version */
6229 0, /* refcnt */
6230 zfs_info, /* info */
6231 nulldev, /* identify */
6232 nulldev, /* probe */
6233 zfs_attach, /* attach */
6234 zfs_detach, /* detach */
6235 nodev, /* reset */
6236 &zfs_cb_ops, /* driver operations */
6237 NULL, /* no bus operations */
6238 NULL, /* power */
6239 ddi_quiesce_not_needed, /* quiesce */
6242 static struct modldrv zfs_modldrv = {
6243 &mod_driverops,
6244 "ZFS storage pool",
6245 &zfs_dev_ops
6248 static struct modlinkage modlinkage = {
6249 MODREV_1,
6250 (void *)&zfs_modlfs,
6251 (void *)&zfs_modldrv,
6252 NULL
6255 static void
6256 zfs_allow_log_destroy(void *arg)
6258 char *poolname = arg;
6259 strfree(poolname);
6263 _init(void)
6265 int error;
6267 spa_init(FREAD | FWRITE);
6268 zfs_init();
6269 zvol_init();
6270 zfs_ioctl_init();
6272 if ((error = mod_install(&modlinkage)) != 0) {
6273 zvol_fini();
6274 zfs_fini();
6275 spa_fini();
6276 return (error);
6279 tsd_create(&zfs_fsyncer_key, NULL);
6280 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6281 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6283 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6284 ASSERT(error == 0);
6285 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6287 return (0);
6291 _fini(void)
6293 int error;
6295 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6296 return (SET_ERROR(EBUSY));
6298 if ((error = mod_remove(&modlinkage)) != 0)
6299 return (error);
6301 zvol_fini();
6302 zfs_fini();
6303 spa_fini();
6304 if (zfs_nfsshare_inited)
6305 (void) ddi_modclose(nfs_mod);
6306 if (zfs_smbshare_inited)
6307 (void) ddi_modclose(smbsrv_mod);
6308 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6309 (void) ddi_modclose(sharefs_mod);
6311 tsd_destroy(&zfs_fsyncer_key);
6312 ldi_ident_release(zfs_li);
6313 zfs_li = NULL;
6314 mutex_destroy(&zfs_share_lock);
6316 return (error);
6320 _info(struct modinfo *modinfop)
6322 return (mod_info(&modlinkage, modinfop));