]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_ioctl.c
Add `cut` binary to the initramfs
[mirror_zfs.git] / module / zfs / zfs_ioctl.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
b129c659 24 * Portions Copyright 2011 Martin Matuska
671c9354 25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
0cee2406 26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
005e27e3 27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
1b87e0f5 28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
788eb90c 29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
b83a0e2d 30 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
9759c60f 31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
95fd54a1 32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
e550644f
BB
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
a0bd735a 35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
a3eeab2d 36 * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
b5256303 37 * Copyright (c) 2017 Datto Inc. All rights reserved.
d12f91fd 38 * Copyright 2017 RackTop Systems.
d3f2cd7e 39 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
6f1ffb06
MA
40 */
41
42/*
43 * ZFS ioctls.
44 *
45 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
46 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
47 *
48 * There are two ways that we handle ioctls: the legacy way where almost
49 * all of the logic is in the ioctl callback, and the new way where most
50 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
51 *
52 * Non-legacy ioctls should be registered by calling
53 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
54 * from userland by lzc_ioctl().
55 *
56 * The registration arguments are as follows:
57 *
58 * const char *name
59 * The name of the ioctl. This is used for history logging. If the
60 * ioctl returns successfully (the callback returns 0), and allow_log
61 * is true, then a history log entry will be recorded with the input &
62 * output nvlists. The log entry can be printed with "zpool history -i".
63 *
64 * zfs_ioc_t ioc
65 * The ioctl request number, which userland will pass to ioctl(2).
b83a0e2d
DB
66 * We want newer versions of libzfs and libzfs_core to run against
67 * existing zfs kernel modules (i.e. a deferred reboot after an update).
68 * Therefore the ioctl numbers cannot change from release to release.
6f1ffb06
MA
69 *
70 * zfs_secpolicy_func_t *secpolicy
71 * This function will be called before the zfs_ioc_func_t, to
72 * determine if this operation is permitted. It should return EPERM
73 * on failure, and 0 on success. Checks include determining if the
74 * dataset is visible in this zone, and if the user has either all
75 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
76 * to do this operation on this dataset with "zfs allow".
77 *
78 * zfs_ioc_namecheck_t namecheck
79 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
80 * name, a dataset name, or nothing. If the name is not well-formed,
81 * the ioctl will fail and the callback will not be called.
82 * Therefore, the callback can assume that the name is well-formed
83 * (e.g. is null-terminated, doesn't have more than one '@' character,
84 * doesn't have invalid characters).
85 *
86 * zfs_ioc_poolcheck_t pool_check
87 * This specifies requirements on the pool state. If the pool does
88 * not meet them (is suspended or is readonly), the ioctl will fail
89 * and the callback will not be called. If any checks are specified
90 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
91 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
92 * POOL_CHECK_READONLY).
93 *
b83a0e2d
DB
94 * zfs_ioc_key_t *nvl_keys
95 * The list of expected/allowable innvl input keys. This list is used
96 * to validate the nvlist input to the ioctl.
97 *
6f1ffb06
MA
98 * boolean_t smush_outnvlist
99 * If smush_outnvlist is true, then the output is presumed to be a
100 * list of errors, and it will be "smushed" down to fit into the
101 * caller's buffer, by removing some entries and replacing them with a
102 * single "N_MORE_ERRORS" entry indicating how many were removed. See
103 * nvlist_smush() for details. If smush_outnvlist is false, and the
104 * outnvlist does not fit into the userland-provided buffer, then the
105 * ioctl will fail with ENOMEM.
106 *
107 * zfs_ioc_func_t *func
108 * The callback function that will perform the operation.
109 *
110 * The callback should return 0 on success, or an error number on
111 * failure. If the function fails, the userland ioctl will return -1,
112 * and errno will be set to the callback's return value. The callback
113 * will be called with the following arguments:
114 *
115 * const char *name
116 * The name of the pool or dataset to operate on, from
117 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
118 * expected type (pool, dataset, or none).
119 *
120 * nvlist_t *innvl
121 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
122 * NULL if no input nvlist was provided. Changes to this nvlist are
123 * ignored. If the input nvlist could not be deserialized, the
124 * ioctl will fail and the callback will not be called.
125 *
126 * nvlist_t *outnvl
127 * The output nvlist, initially empty. The callback can fill it in,
128 * and it will be returned to userland by serializing it into
129 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
130 * fails (e.g. because the caller didn't supply a large enough
131 * buffer), then the overall ioctl will fail. See the
132 * 'smush_nvlist' argument above for additional behaviors.
133 *
134 * There are two typical uses of the output nvlist:
135 * - To return state, e.g. property values. In this case,
136 * smush_outnvlist should be false. If the buffer was not large
137 * enough, the caller will reallocate a larger buffer and try
138 * the ioctl again.
139 *
140 * - To return multiple errors from an ioctl which makes on-disk
141 * changes. In this case, smush_outnvlist should be true.
142 * Ioctls which make on-disk modifications should generally not
143 * use the outnvl if they succeed, because the caller can not
144 * distinguish between the operation failing, and
145 * deserialization failing.
b83a0e2d
DB
146 *
147 * IOCTL Interface Errors
148 *
149 * The following ioctl input errors can be returned:
150 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
151 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
152 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
153 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
3541dc6d 154 */
34dc7c2f 155
34dc7c2f
BB
156#include <sys/types.h>
157#include <sys/param.h>
158#include <sys/errno.h>
159#include <sys/uio.h>
34dc7c2f
BB
160#include <sys/file.h>
161#include <sys/kmem.h>
34dc7c2f
BB
162#include <sys/cmn_err.h>
163#include <sys/stat.h>
164#include <sys/zfs_ioctl.h>
428870ff 165#include <sys/zfs_vfsops.h>
34dc7c2f
BB
166#include <sys/zfs_znode.h>
167#include <sys/zap.h>
168#include <sys/spa.h>
169#include <sys/spa_impl.h>
170#include <sys/vdev.h>
4a283c7f 171#include <sys/vdev_impl.h>
34dc7c2f
BB
172#include <sys/dmu.h>
173#include <sys/dsl_dir.h>
174#include <sys/dsl_dataset.h>
175#include <sys/dsl_prop.h>
176#include <sys/dsl_deleg.h>
177#include <sys/dmu_objset.h>
37abac6d 178#include <sys/dmu_impl.h>
13fe0198 179#include <sys/dmu_tx.h>
34dc7c2f 180#include <sys/sunddi.h>
34dc7c2f
BB
181#include <sys/policy.h>
182#include <sys/zone.h>
183#include <sys/nvpair.h>
184#include <sys/pathname.h>
34dc7c2f
BB
185#include <sys/sdt.h>
186#include <sys/fs/zfs.h>
ebe7e575 187#include <sys/zfs_ctldir.h>
34dc7c2f 188#include <sys/zfs_dir.h>
572e2857 189#include <sys/zfs_onexit.h>
34dc7c2f 190#include <sys/zvol.h>
428870ff 191#include <sys/dsl_scan.h>
325f0235 192#include <sys/fm/util.h>
b5256303 193#include <sys/dsl_crypt.h>
325f0235 194
03916905 195#include <sys/dmu_recv.h>
13fe0198
MA
196#include <sys/dmu_send.h>
197#include <sys/dsl_destroy.h>
da536844 198#include <sys/dsl_bookmark.h>
13fe0198 199#include <sys/dsl_userhold.h>
9759c60f 200#include <sys/zfeature.h>
d99a0153 201#include <sys/zcp.h>
3c67d83a 202#include <sys/zio_checksum.h>
a1d477c2 203#include <sys/vdev_removal.h>
e8bcb693 204#include <sys/zfs_sysfs.h>
9759c60f 205
325f0235 206#include <linux/miscdevice.h>
f74b821a 207#include <linux/slab.h>
34dc7c2f
BB
208
209#include "zfs_namecheck.h"
210#include "zfs_prop.h"
211#include "zfs_deleg.h"
428870ff 212#include "zfs_comutil.h"
34dc7c2f 213
d99a0153
CW
214#include <sys/lua/lua.h>
215#include <sys/lua/lauxlib.h>
216
f74b821a
BB
217/*
218 * Limit maximum nvlist size. We don't want users passing in insane values
219 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
220 */
221#define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
222
325f0235 223kmutex_t zfsdev_state_lock;
3937ab20 224zfsdev_state_t *zfsdev_state_list;
34dc7c2f
BB
225
226extern void zfs_init(void);
227extern void zfs_fini(void);
228
6f1ffb06
MA
229uint_t zfs_fsyncer_key;
230extern uint_t rrw_tsd_key;
231static uint_t zfs_allow_log_key;
232
233typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
234typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
235typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
34dc7c2f 236
b83a0e2d
DB
237/*
238 * IOC Keys are used to document and validate user->kernel interface inputs.
239 * See zfs_keys_recv_new for an example declaration. Any key name that is not
240 * listed will be rejected as input.
241 *
242 * The keyname 'optional' is always allowed, and must be an nvlist if present.
243 * Arguments which older kernels can safely ignore can be placed under the
244 * "optional" key.
245 *
246 * When adding new keys to an existing ioc for new functionality, consider:
247 * - adding an entry into zfs_sysfs.c zfs_features[] list
248 * - updating the libzfs_input_check.c test utility
249 *
250 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
251 * for the expected name (bookmark, snapshot, property, etc) but there
252 * is no validation in the preflight zfs_check_input_nvpairs() check.
253 */
254typedef enum {
255 ZK_OPTIONAL = 1 << 0, /* pair is optional */
256 ZK_WILDCARDLIST = 1 << 1, /* one or more unspecified key names */
257} ioc_key_flag_t;
258
259/* DATA_TYPE_ANY is used when zkey_type can vary. */
260#define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
261
262typedef struct zfs_ioc_key {
263 const char *zkey_name;
264 data_type_t zkey_type;
265 ioc_key_flag_t zkey_flags;
266} zfs_ioc_key_t;
267
9babb374
BB
268typedef enum {
269 NO_NAME,
270 POOL_NAME,
271 DATASET_NAME
272} zfs_ioc_namecheck_t;
273
572e2857
BB
274typedef enum {
275 POOL_CHECK_NONE = 1 << 0,
276 POOL_CHECK_SUSPENDED = 1 << 1,
6f1ffb06 277 POOL_CHECK_READONLY = 1 << 2,
572e2857
BB
278} zfs_ioc_poolcheck_t;
279
34dc7c2f 280typedef struct zfs_ioc_vec {
6f1ffb06 281 zfs_ioc_legacy_func_t *zvec_legacy_func;
34dc7c2f
BB
282 zfs_ioc_func_t *zvec_func;
283 zfs_secpolicy_func_t *zvec_secpolicy;
9babb374 284 zfs_ioc_namecheck_t zvec_namecheck;
6f1ffb06 285 boolean_t zvec_allow_log;
572e2857 286 zfs_ioc_poolcheck_t zvec_pool_check;
6f1ffb06
MA
287 boolean_t zvec_smush_outnvlist;
288 const char *zvec_name;
b83a0e2d
DB
289 const zfs_ioc_key_t *zvec_nvl_keys;
290 size_t zvec_nvl_key_count;
34dc7c2f
BB
291} zfs_ioc_vec_t;
292
9babb374
BB
293/* This array is indexed by zfs_userquota_prop_t */
294static const char *userquota_perms[] = {
295 ZFS_DELEG_PERM_USERUSED,
296 ZFS_DELEG_PERM_USERQUOTA,
297 ZFS_DELEG_PERM_GROUPUSED,
298 ZFS_DELEG_PERM_GROUPQUOTA,
1de321e6
JX
299 ZFS_DELEG_PERM_USEROBJUSED,
300 ZFS_DELEG_PERM_USEROBJQUOTA,
301 ZFS_DELEG_PERM_GROUPOBJUSED,
302 ZFS_DELEG_PERM_GROUPOBJQUOTA,
9c5167d1
NF
303 ZFS_DELEG_PERM_PROJECTUSED,
304 ZFS_DELEG_PERM_PROJECTQUOTA,
305 ZFS_DELEG_PERM_PROJECTOBJUSED,
306 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
9babb374
BB
307};
308
309static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
9c5167d1 310static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
428870ff
BB
311static int zfs_check_settable(const char *name, nvpair_t *property,
312 cred_t *cr);
313static int zfs_check_clearable(char *dataset, nvlist_t *props,
314 nvlist_t **errors);
b128c09f
BB
315static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
316 boolean_t *);
6f1ffb06
MA
317int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
318static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
b128c09f 319
34dc7c2f
BB
320static void
321history_str_free(char *buf)
322{
323 kmem_free(buf, HIS_MAX_RECORD_LEN);
324}
325
326static char *
327history_str_get(zfs_cmd_t *zc)
328{
329 char *buf;
330
b8864a23 331 if (zc->zc_history == 0)
34dc7c2f
BB
332 return (NULL);
333
efcd79a8 334 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
34dc7c2f
BB
335 if (copyinstr((void *)(uintptr_t)zc->zc_history,
336 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
337 history_str_free(buf);
338 return (NULL);
339 }
340
341 buf[HIS_MAX_RECORD_LEN -1] = '\0';
342
343 return (buf);
344}
345
346/*
b128c09f
BB
347 * Check to see if the named dataset is currently defined as bootable
348 */
349static boolean_t
350zfs_is_bootfs(const char *name)
351{
428870ff 352 objset_t *os;
b128c09f 353
428870ff
BB
354 if (dmu_objset_hold(name, FTAG, &os) == 0) {
355 boolean_t ret;
356 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
357 dmu_objset_rele(os, FTAG);
358 return (ret);
b128c09f 359 }
428870ff 360 return (B_FALSE);
b128c09f
BB
361}
362
363/*
d3cc8b15 364 * Return non-zero if the spa version is less than requested version.
34dc7c2f
BB
365 */
366static int
b128c09f 367zfs_earlier_version(const char *name, int version)
34dc7c2f 368{
34dc7c2f
BB
369 spa_t *spa;
370
371 if (spa_open(name, &spa, FTAG) == 0) {
372 if (spa_version(spa) < version) {
373 spa_close(spa, FTAG);
374 return (1);
375 }
376 spa_close(spa, FTAG);
377 }
378 return (0);
379}
380
381/*
b128c09f 382 * Return TRUE if the ZPL version is less than requested version.
34dc7c2f 383 */
b128c09f
BB
384static boolean_t
385zpl_earlier_version(const char *name, int version)
34dc7c2f
BB
386{
387 objset_t *os;
b128c09f 388 boolean_t rc = B_TRUE;
34dc7c2f 389
428870ff 390 if (dmu_objset_hold(name, FTAG, &os) == 0) {
b128c09f 391 uint64_t zplversion;
34dc7c2f 392
428870ff
BB
393 if (dmu_objset_type(os) != DMU_OST_ZFS) {
394 dmu_objset_rele(os, FTAG);
395 return (B_TRUE);
396 }
397 /* XXX reading from non-owned objset */
b128c09f
BB
398 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
399 rc = zplversion < version;
428870ff 400 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
401 }
402 return (rc);
403}
404
405static void
406zfs_log_history(zfs_cmd_t *zc)
407{
408 spa_t *spa;
409 char *buf;
410
411 if ((buf = history_str_get(zc)) == NULL)
412 return;
413
414 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
415 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
6f1ffb06 416 (void) spa_history_log(spa, buf);
34dc7c2f
BB
417 spa_close(spa, FTAG);
418 }
419 history_str_free(buf);
420}
421
422/*
423 * Policy for top-level read operations (list pools). Requires no privileges,
424 * and can be used in the local zone, as there is no associated dataset.
425 */
426/* ARGSUSED */
427static int
6f1ffb06 428zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
429{
430 return (0);
431}
432
433/*
434 * Policy for dataset read operations (list children, get statistics). Requires
435 * no privileges, but must be visible in the local zone.
436 */
437/* ARGSUSED */
438static int
6f1ffb06 439zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
440{
441 if (INGLOBALZONE(curproc) ||
442 zone_dataset_visible(zc->zc_name, NULL))
443 return (0);
444
2e528b49 445 return (SET_ERROR(ENOENT));
34dc7c2f
BB
446}
447
448static int
572e2857 449zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
34dc7c2f 450{
34dc7c2f
BB
451 int writable = 1;
452
453 /*
454 * The dataset must be visible by this zone -- check this first
455 * so they don't see EPERM on something they shouldn't know about.
456 */
457 if (!INGLOBALZONE(curproc) &&
458 !zone_dataset_visible(dataset, &writable))
2e528b49 459 return (SET_ERROR(ENOENT));
34dc7c2f 460
34dc7c2f
BB
461 if (INGLOBALZONE(curproc)) {
462 /*
463 * If the fs is zoned, only root can access it from the
464 * global zone.
465 */
466 if (secpolicy_zfs(cr) && zoned)
2e528b49 467 return (SET_ERROR(EPERM));
34dc7c2f
BB
468 } else {
469 /*
470 * If we are in a local zone, the 'zoned' property must be set.
471 */
472 if (!zoned)
2e528b49 473 return (SET_ERROR(EPERM));
34dc7c2f
BB
474
475 /* must be writable by this zone */
476 if (!writable)
2e528b49 477 return (SET_ERROR(EPERM));
34dc7c2f
BB
478 }
479 return (0);
480}
481
572e2857
BB
482static int
483zfs_dozonecheck(const char *dataset, cred_t *cr)
484{
485 uint64_t zoned;
486
487 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
2e528b49 488 return (SET_ERROR(ENOENT));
572e2857
BB
489
490 return (zfs_dozonecheck_impl(dataset, zoned, cr));
491}
492
493static int
494zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
495{
496 uint64_t zoned;
497
13fe0198 498 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
2e528b49 499 return (SET_ERROR(ENOENT));
572e2857
BB
500
501 return (zfs_dozonecheck_impl(dataset, zoned, cr));
502}
503
6f1ffb06 504static int
13fe0198
MA
505zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
506 const char *perm, cred_t *cr)
34dc7c2f
BB
507{
508 int error;
509
330d06f9 510 error = zfs_dozonecheck_ds(name, ds, cr);
34dc7c2f
BB
511 if (error == 0) {
512 error = secpolicy_zfs(cr);
13fe0198 513 if (error != 0)
6f1ffb06 514 error = dsl_deleg_access_impl(ds, perm, cr);
34dc7c2f
BB
515 }
516 return (error);
517}
518
6f1ffb06 519static int
13fe0198 520zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
572e2857
BB
521{
522 int error;
13fe0198
MA
523 dsl_dataset_t *ds;
524 dsl_pool_t *dp;
572e2857 525
e88551d5
GM
526 /*
527 * First do a quick check for root in the global zone, which
528 * is allowed to do all write_perms. This ensures that zfs_ioc_*
529 * will get to handle nonexistent datasets.
530 */
531 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
532 return (0);
533
13fe0198
MA
534 error = dsl_pool_hold(name, FTAG, &dp);
535 if (error != 0)
536 return (error);
537
538 error = dsl_dataset_hold(dp, name, FTAG, &ds);
539 if (error != 0) {
540 dsl_pool_rele(dp, FTAG);
541 return (error);
572e2857 542 }
13fe0198
MA
543
544 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
545
546 dsl_dataset_rele(ds, FTAG);
547 dsl_pool_rele(dp, FTAG);
572e2857
BB
548 return (error);
549}
550
428870ff
BB
551/*
552 * Policy for setting the security label property.
553 *
554 * Returns 0 for success, non-zero for access and other errors.
555 */
34dc7c2f 556static int
428870ff 557zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
34dc7c2f 558{
d2c15e84 559#ifdef HAVE_MLSLABEL
428870ff
BB
560 char ds_hexsl[MAXNAMELEN];
561 bslabel_t ds_sl, new_sl;
562 boolean_t new_default = FALSE;
563 uint64_t zoned;
564 int needed_priv = -1;
565 int error;
566
567 /* First get the existing dataset label. */
568 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
569 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
13fe0198 570 if (error != 0)
2e528b49 571 return (SET_ERROR(EPERM));
428870ff
BB
572
573 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
574 new_default = TRUE;
575
576 /* The label must be translatable */
577 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
2e528b49 578 return (SET_ERROR(EINVAL));
428870ff
BB
579
580 /*
581 * In a non-global zone, disallow attempts to set a label that
582 * doesn't match that of the zone; otherwise no other checks
583 * are needed.
584 */
585 if (!INGLOBALZONE(curproc)) {
586 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
2e528b49 587 return (SET_ERROR(EPERM));
428870ff
BB
588 return (0);
589 }
590
591 /*
592 * For global-zone datasets (i.e., those whose zoned property is
593 * "off", verify that the specified new label is valid for the
594 * global zone.
595 */
596 if (dsl_prop_get_integer(name,
597 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
2e528b49 598 return (SET_ERROR(EPERM));
428870ff
BB
599 if (!zoned) {
600 if (zfs_check_global_label(name, strval) != 0)
2e528b49 601 return (SET_ERROR(EPERM));
428870ff
BB
602 }
603
604 /*
605 * If the existing dataset label is nondefault, check if the
606 * dataset is mounted (label cannot be changed while mounted).
0037b49e 607 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
428870ff
BB
608 * mounted (or isn't a dataset, doesn't exist, ...).
609 */
610 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
611 objset_t *os;
612 static char *setsl_tag = "setsl_tag";
613
614 /*
615 * Try to own the dataset; abort if there is any error,
616 * (e.g., already mounted, in use, or other error).
617 */
b5256303 618 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
428870ff 619 setsl_tag, &os);
13fe0198 620 if (error != 0)
2e528b49 621 return (SET_ERROR(EPERM));
428870ff 622
b5256303 623 dmu_objset_disown(os, B_TRUE, setsl_tag);
428870ff
BB
624
625 if (new_default) {
626 needed_priv = PRIV_FILE_DOWNGRADE_SL;
627 goto out_check;
628 }
629
630 if (hexstr_to_label(strval, &new_sl) != 0)
2e528b49 631 return (SET_ERROR(EPERM));
428870ff
BB
632
633 if (blstrictdom(&ds_sl, &new_sl))
634 needed_priv = PRIV_FILE_DOWNGRADE_SL;
635 else if (blstrictdom(&new_sl, &ds_sl))
636 needed_priv = PRIV_FILE_UPGRADE_SL;
637 } else {
638 /* dataset currently has a default label */
639 if (!new_default)
640 needed_priv = PRIV_FILE_UPGRADE_SL;
641 }
642
643out_check:
644 if (needed_priv != -1)
645 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
646 return (0);
d2c15e84 647#else
ecb2b7dc 648 return (SET_ERROR(ENOTSUP));
d2c15e84 649#endif /* HAVE_MLSLABEL */
428870ff
BB
650}
651
652static int
653zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
654 cred_t *cr)
655{
656 char *strval;
657
34dc7c2f
BB
658 /*
659 * Check permissions for special properties.
660 */
661 switch (prop) {
e75c13c3
BB
662 default:
663 break;
34dc7c2f
BB
664 case ZFS_PROP_ZONED:
665 /*
666 * Disallow setting of 'zoned' from within a local zone.
667 */
668 if (!INGLOBALZONE(curproc))
2e528b49 669 return (SET_ERROR(EPERM));
34dc7c2f
BB
670 break;
671
672 case ZFS_PROP_QUOTA:
788eb90c
JJ
673 case ZFS_PROP_FILESYSTEM_LIMIT:
674 case ZFS_PROP_SNAPSHOT_LIMIT:
34dc7c2f
BB
675 if (!INGLOBALZONE(curproc)) {
676 uint64_t zoned;
eca7b760 677 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
678 /*
679 * Unprivileged users are allowed to modify the
788eb90c 680 * limit on things *under* (ie. contained by)
34dc7c2f
BB
681 * the thing they own.
682 */
428870ff 683 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
34dc7c2f 684 setpoint))
2e528b49 685 return (SET_ERROR(EPERM));
428870ff 686 if (!zoned || strlen(dsname) <= strlen(setpoint))
2e528b49 687 return (SET_ERROR(EPERM));
34dc7c2f
BB
688 }
689 break;
428870ff
BB
690
691 case ZFS_PROP_MLSLABEL:
692 if (!is_system_labeled())
2e528b49 693 return (SET_ERROR(EPERM));
428870ff
BB
694
695 if (nvpair_value_string(propval, &strval) == 0) {
696 int err;
697
698 err = zfs_set_slabel_policy(dsname, strval, CRED());
699 if (err != 0)
700 return (err);
701 }
702 break;
34dc7c2f
BB
703 }
704
428870ff 705 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
34dc7c2f
BB
706}
707
6f1ffb06
MA
708/* ARGSUSED */
709static int
710zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
711{
712 int error;
713
714 error = zfs_dozonecheck(zc->zc_name, cr);
13fe0198 715 if (error != 0)
34dc7c2f
BB
716 return (error);
717
718 /*
719 * permission to set permissions will be evaluated later in
720 * dsl_deleg_can_allow()
721 */
722 return (0);
723}
724
6f1ffb06
MA
725/* ARGSUSED */
726static int
727zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 728{
428870ff
BB
729 return (zfs_secpolicy_write_perms(zc->zc_name,
730 ZFS_DELEG_PERM_ROLLBACK, cr));
34dc7c2f
BB
731}
732
6f1ffb06
MA
733/* ARGSUSED */
734static int
735zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 736{
572e2857
BB
737 dsl_pool_t *dp;
738 dsl_dataset_t *ds;
739 char *cp;
740 int error;
741
742 /*
743 * Generate the current snapshot name from the given objsetid, then
744 * use that name for the secpolicy/zone checks.
745 */
746 cp = strchr(zc->zc_name, '@');
747 if (cp == NULL)
2e528b49 748 return (SET_ERROR(EINVAL));
13fe0198
MA
749 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
750 if (error != 0)
572e2857
BB
751 return (error);
752
572e2857 753 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
13fe0198
MA
754 if (error != 0) {
755 dsl_pool_rele(dp, FTAG);
572e2857 756 return (error);
13fe0198 757 }
572e2857
BB
758
759 dsl_dataset_name(ds, zc->zc_name);
760
761 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
762 ZFS_DELEG_PERM_SEND, cr);
763 dsl_dataset_rele(ds, FTAG);
13fe0198 764 dsl_pool_rele(dp, FTAG);
572e2857
BB
765
766 return (error);
34dc7c2f
BB
767}
768
6f1ffb06
MA
769/* ARGSUSED */
770static int
771zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
772{
773 return (zfs_secpolicy_write_perms(zc->zc_name,
774 ZFS_DELEG_PERM_SEND, cr));
775}
776
34dc7c2f 777int
6f1ffb06 778zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 779{
2e528b49 780 return (SET_ERROR(ENOTSUP));
9babb374 781}
34dc7c2f 782
9babb374 783int
6f1ffb06 784zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 785{
2e528b49 786 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
787}
788
789static int
790zfs_get_parent(const char *datasetname, char *parent, int parentsize)
791{
792 char *cp;
793
794 /*
795 * Remove the @bla or /bla from the end of the name to get the parent.
796 */
797 (void) strncpy(parent, datasetname, parentsize);
798 cp = strrchr(parent, '@');
799 if (cp != NULL) {
800 cp[0] = '\0';
801 } else {
802 cp = strrchr(parent, '/');
803 if (cp == NULL)
2e528b49 804 return (SET_ERROR(ENOENT));
34dc7c2f
BB
805 cp[0] = '\0';
806 }
807
808 return (0);
809}
810
811int
812zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
813{
814 int error;
815
816 if ((error = zfs_secpolicy_write_perms(name,
817 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
818 return (error);
819
820 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
821}
822
6f1ffb06 823/* ARGSUSED */
34dc7c2f 824static int
6f1ffb06 825zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
826{
827 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
828}
829
830/*
428870ff 831 * Destroying snapshots with delegated permissions requires
6f1ffb06 832 * descendant mount and destroy permissions.
34dc7c2f 833 */
6f1ffb06 834/* ARGSUSED */
34dc7c2f 835static int
6f1ffb06 836zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 837{
6f1ffb06
MA
838 nvlist_t *snaps;
839 nvpair_t *pair, *nextpair;
840 int error = 0;
428870ff 841
b83a0e2d
DB
842 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
843
6f1ffb06
MA
844 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
845 pair = nextpair) {
6f1ffb06 846 nextpair = nvlist_next_nvpair(snaps, pair);
da536844
MA
847 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
848 if (error == ENOENT) {
6f1ffb06
MA
849 /*
850 * Ignore any snapshots that don't exist (we consider
851 * them "already destroyed"). Remove the name from the
852 * nvl here in case the snapshot is created between
853 * now and when we try to destroy it (in which case
854 * we don't want to destroy it since we haven't
855 * checked for permission).
856 */
857 fnvlist_remove_nvpair(snaps, pair);
858 error = 0;
6f1ffb06 859 }
6f1ffb06
MA
860 if (error != 0)
861 break;
862 }
428870ff 863
428870ff 864 return (error);
34dc7c2f
BB
865}
866
867int
868zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
869{
eca7b760 870 char parentname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
871 int error;
872
873 if ((error = zfs_secpolicy_write_perms(from,
874 ZFS_DELEG_PERM_RENAME, cr)) != 0)
875 return (error);
876
877 if ((error = zfs_secpolicy_write_perms(from,
878 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
879 return (error);
880
881 if ((error = zfs_get_parent(to, parentname,
882 sizeof (parentname))) != 0)
883 return (error);
884
885 if ((error = zfs_secpolicy_write_perms(parentname,
886 ZFS_DELEG_PERM_CREATE, cr)) != 0)
887 return (error);
888
889 if ((error = zfs_secpolicy_write_perms(parentname,
890 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
891 return (error);
892
893 return (error);
894}
895
6f1ffb06 896/* ARGSUSED */
34dc7c2f 897static int
6f1ffb06 898zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
899{
900 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
901}
902
6f1ffb06 903/* ARGSUSED */
34dc7c2f 904static int
6f1ffb06 905zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 906{
13fe0198
MA
907 dsl_pool_t *dp;
908 dsl_dataset_t *clone;
34dc7c2f
BB
909 int error;
910
911 error = zfs_secpolicy_write_perms(zc->zc_name,
912 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
913 if (error != 0)
914 return (error);
915
916 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
917 if (error != 0)
34dc7c2f
BB
918 return (error);
919
13fe0198 920 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
34dc7c2f
BB
921
922 if (error == 0) {
eca7b760 923 char parentname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198 924 dsl_dataset_t *origin = NULL;
34dc7c2f 925 dsl_dir_t *dd;
13fe0198 926 dd = clone->ds_dir;
34dc7c2f 927
b128c09f 928 error = dsl_dataset_hold_obj(dd->dd_pool,
d683ddbb 929 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
13fe0198
MA
930 if (error != 0) {
931 dsl_dataset_rele(clone, FTAG);
932 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
933 return (error);
934 }
935
13fe0198 936 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
34dc7c2f
BB
937 ZFS_DELEG_PERM_MOUNT, cr);
938
13fe0198
MA
939 dsl_dataset_name(origin, parentname);
940 if (error == 0) {
941 error = zfs_secpolicy_write_perms_ds(parentname, origin,
34dc7c2f 942 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
943 }
944 dsl_dataset_rele(clone, FTAG);
945 dsl_dataset_rele(origin, FTAG);
34dc7c2f 946 }
13fe0198 947 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
948 return (error);
949}
950
6f1ffb06 951/* ARGSUSED */
34dc7c2f 952static int
6f1ffb06 953zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
954{
955 int error;
956
957 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
958 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
959 return (error);
960
961 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
962 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
963 return (error);
964
965 return (zfs_secpolicy_write_perms(zc->zc_name,
966 ZFS_DELEG_PERM_CREATE, cr));
967}
968
43e52edd
BB
969/* ARGSUSED */
970static int
971zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
972{
973 return (zfs_secpolicy_recv(zc, innvl, cr));
974}
975
34dc7c2f
BB
976int
977zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
978{
428870ff
BB
979 return (zfs_secpolicy_write_perms(name,
980 ZFS_DELEG_PERM_SNAPSHOT, cr));
34dc7c2f
BB
981}
982
6f1ffb06
MA
983/*
984 * Check for permission to create each snapshot in the nvlist.
985 */
986/* ARGSUSED */
34dc7c2f 987static int
6f1ffb06 988zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 989{
6f1ffb06
MA
990 nvlist_t *snaps;
991 int error = 0;
992 nvpair_t *pair;
993
b83a0e2d
DB
994 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
995
6f1ffb06
MA
996 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
997 pair = nvlist_next_nvpair(snaps, pair)) {
998 char *name = nvpair_name(pair);
999 char *atp = strchr(name, '@');
34dc7c2f 1000
6f1ffb06 1001 if (atp == NULL) {
2e528b49 1002 error = SET_ERROR(EINVAL);
6f1ffb06
MA
1003 break;
1004 }
1005 *atp = '\0';
1006 error = zfs_secpolicy_snapshot_perms(name, cr);
1007 *atp = '@';
1008 if (error != 0)
1009 break;
1010 }
1011 return (error);
1012}
1013
da536844 1014/*
b83a0e2d 1015 * Check for permission to create each bookmark in the nvlist.
da536844
MA
1016 */
1017/* ARGSUSED */
1018static int
1019zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1020{
1021 int error = 0;
da536844 1022
1c27024e 1023 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
da536844
MA
1024 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1025 char *name = nvpair_name(pair);
1026 char *hashp = strchr(name, '#');
1027
1028 if (hashp == NULL) {
1029 error = SET_ERROR(EINVAL);
1030 break;
1031 }
1032 *hashp = '\0';
1033 error = zfs_secpolicy_write_perms(name,
1034 ZFS_DELEG_PERM_BOOKMARK, cr);
1035 *hashp = '#';
1036 if (error != 0)
1037 break;
1038 }
1039 return (error);
1040}
1041
a1d477c2
MA
1042/* ARGSUSED */
1043static int
1044zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1045{
1046 return (zfs_secpolicy_write_perms(zc->zc_name,
1047 ZFS_DELEG_PERM_REMAP, cr));
1048}
1049
da536844
MA
1050/* ARGSUSED */
1051static int
1052zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1053{
1054 nvpair_t *pair, *nextpair;
1055 int error = 0;
1056
1057 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1058 pair = nextpair) {
1059 char *name = nvpair_name(pair);
1060 char *hashp = strchr(name, '#');
1061 nextpair = nvlist_next_nvpair(innvl, pair);
1062
1063 if (hashp == NULL) {
1064 error = SET_ERROR(EINVAL);
1065 break;
1066 }
1067
1068 *hashp = '\0';
1069 error = zfs_secpolicy_write_perms(name,
1070 ZFS_DELEG_PERM_DESTROY, cr);
1071 *hashp = '#';
1072 if (error == ENOENT) {
1073 /*
1074 * Ignore any filesystems that don't exist (we consider
1075 * their bookmarks "already destroyed"). Remove
1076 * the name from the nvl here in case the filesystem
1077 * is created between now and when we try to destroy
1078 * the bookmark (in which case we don't want to
1079 * destroy it since we haven't checked for permission).
1080 */
1081 fnvlist_remove_nvpair(innvl, pair);
1082 error = 0;
1083 }
1084 if (error != 0)
1085 break;
1086 }
1087
1088 return (error);
1089}
1090
6f1ffb06
MA
1091/* ARGSUSED */
1092static int
1093zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1094{
1095 /*
1096 * Even root must have a proper TSD so that we know what pool
1097 * to log to.
1098 */
1099 if (tsd_get(zfs_allow_log_key) == NULL)
2e528b49 1100 return (SET_ERROR(EPERM));
6f1ffb06 1101 return (0);
34dc7c2f
BB
1102}
1103
1104static int
6f1ffb06 1105zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 1106{
eca7b760 1107 char parentname[ZFS_MAX_DATASET_NAME_LEN];
428870ff 1108 int error;
6f1ffb06 1109 char *origin;
34dc7c2f
BB
1110
1111 if ((error = zfs_get_parent(zc->zc_name, parentname,
1112 sizeof (parentname))) != 0)
1113 return (error);
1114
6f1ffb06
MA
1115 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1116 (error = zfs_secpolicy_write_perms(origin,
1117 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1118 return (error);
34dc7c2f
BB
1119
1120 if ((error = zfs_secpolicy_write_perms(parentname,
1121 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1122 return (error);
1123
6f1ffb06
MA
1124 return (zfs_secpolicy_write_perms(parentname,
1125 ZFS_DELEG_PERM_MOUNT, cr));
34dc7c2f
BB
1126}
1127
34dc7c2f
BB
1128/*
1129 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1130 * SYS_CONFIG privilege, which is not available in a local zone.
1131 */
1132/* ARGSUSED */
1133static int
6f1ffb06 1134zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
1135{
1136 if (secpolicy_sys_config(cr, B_FALSE) != 0)
2e528b49 1137 return (SET_ERROR(EPERM));
34dc7c2f
BB
1138
1139 return (0);
1140}
1141
572e2857
BB
1142/*
1143 * Policy for object to name lookups.
1144 */
1145/* ARGSUSED */
1146static int
6f1ffb06 1147zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
572e2857
BB
1148{
1149 int error;
1150
1151 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1152 return (0);
1153
1154 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1155 return (error);
1156}
1157
34dc7c2f
BB
1158/*
1159 * Policy for fault injection. Requires all privileges.
1160 */
1161/* ARGSUSED */
1162static int
6f1ffb06 1163zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
1164{
1165 return (secpolicy_zinject(cr));
1166}
1167
6f1ffb06 1168/* ARGSUSED */
34dc7c2f 1169static int
6f1ffb06 1170zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
1171{
1172 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1173
1174 if (prop == ZPROP_INVAL) {
1175 if (!zfs_prop_user(zc->zc_value))
2e528b49 1176 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1177 return (zfs_secpolicy_write_perms(zc->zc_name,
1178 ZFS_DELEG_PERM_USERPROP, cr));
1179 } else {
428870ff
BB
1180 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1181 NULL, cr));
34dc7c2f
BB
1182 }
1183}
1184
9babb374 1185static int
6f1ffb06 1186zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1187{
6f1ffb06 1188 int err = zfs_secpolicy_read(zc, innvl, cr);
9babb374
BB
1189 if (err)
1190 return (err);
1191
1192 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 1193 return (SET_ERROR(EINVAL));
9babb374
BB
1194
1195 if (zc->zc_value[0] == 0) {
1196 /*
1197 * They are asking about a posix uid/gid. If it's
1198 * themself, allow it.
1199 */
1200 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1de321e6
JX
1201 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1202 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1203 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
9babb374
BB
1204 if (zc->zc_guid == crgetuid(cr))
1205 return (0);
9c5167d1
NF
1206 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1207 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1208 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1209 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
9babb374
BB
1210 if (groupmember(zc->zc_guid, cr))
1211 return (0);
1212 }
9c5167d1 1213 /* else is for project quota/used */
9babb374
BB
1214 }
1215
1216 return (zfs_secpolicy_write_perms(zc->zc_name,
1217 userquota_perms[zc->zc_objset_type], cr));
1218}
1219
1220static int
6f1ffb06 1221zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1222{
6f1ffb06 1223 int err = zfs_secpolicy_read(zc, innvl, cr);
9babb374
BB
1224 if (err)
1225 return (err);
1226
1227 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 1228 return (SET_ERROR(EINVAL));
9babb374
BB
1229
1230 return (zfs_secpolicy_write_perms(zc->zc_name,
1231 userquota_perms[zc->zc_objset_type], cr));
1232}
1233
6f1ffb06 1234/* ARGSUSED */
9babb374 1235static int
6f1ffb06 1236zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1237{
428870ff
BB
1238 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1239 NULL, cr));
9babb374
BB
1240}
1241
6f1ffb06 1242/* ARGSUSED */
45d1cae3 1243static int
6f1ffb06 1244zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
45d1cae3 1245{
13fe0198
MA
1246 nvpair_t *pair;
1247 nvlist_t *holds;
1248 int error;
1249
b83a0e2d 1250 holds = fnvlist_lookup_nvlist(innvl, "holds");
13fe0198
MA
1251
1252 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1253 pair = nvlist_next_nvpair(holds, pair)) {
eca7b760 1254 char fsname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
1255 error = dmu_fsname(nvpair_name(pair), fsname);
1256 if (error != 0)
1257 return (error);
1258 error = zfs_secpolicy_write_perms(fsname,
1259 ZFS_DELEG_PERM_HOLD, cr);
1260 if (error != 0)
1261 return (error);
1262 }
1263 return (0);
45d1cae3
BB
1264}
1265
6f1ffb06 1266/* ARGSUSED */
45d1cae3 1267static int
6f1ffb06 1268zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
45d1cae3 1269{
13fe0198
MA
1270 nvpair_t *pair;
1271 int error;
1272
1273 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1274 pair = nvlist_next_nvpair(innvl, pair)) {
eca7b760 1275 char fsname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
1276 error = dmu_fsname(nvpair_name(pair), fsname);
1277 if (error != 0)
1278 return (error);
1279 error = zfs_secpolicy_write_perms(fsname,
1280 ZFS_DELEG_PERM_RELEASE, cr);
1281 if (error != 0)
1282 return (error);
1283 }
1284 return (0);
45d1cae3
BB
1285}
1286
572e2857
BB
1287/*
1288 * Policy for allowing temporary snapshots to be taken or released
1289 */
1290static int
6f1ffb06 1291zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
572e2857
BB
1292{
1293 /*
1294 * A temporary snapshot is the same as a snapshot,
1295 * hold, destroy and release all rolled into one.
1296 * Delegated diff alone is sufficient that we allow this.
1297 */
1298 int error;
1299
1300 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1301 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1302 return (0);
1303
6f1ffb06 1304 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
b83a0e2d
DB
1305
1306 if (innvl != NULL) {
1307 if (error == 0)
1308 error = zfs_secpolicy_hold(zc, innvl, cr);
1309 if (error == 0)
1310 error = zfs_secpolicy_release(zc, innvl, cr);
1311 if (error == 0)
1312 error = zfs_secpolicy_destroy(zc, innvl, cr);
1313 }
572e2857
BB
1314 return (error);
1315}
1316
b5256303
TC
1317static int
1318zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1319{
1320 return (zfs_secpolicy_write_perms(zc->zc_name,
1321 ZFS_DELEG_PERM_LOAD_KEY, cr));
1322}
1323
1324static int
1325zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1326{
1327 return (zfs_secpolicy_write_perms(zc->zc_name,
1328 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1329}
1330
34dc7c2f
BB
1331/*
1332 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1333 */
1334static int
9babb374 1335get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
34dc7c2f
BB
1336{
1337 char *packed;
1338 int error;
1339 nvlist_t *list = NULL;
1340
1341 /*
1342 * Read in and unpack the user-supplied nvlist.
1343 */
1344 if (size == 0)
2e528b49 1345 return (SET_ERROR(EINVAL));
34dc7c2f 1346
77aef6f6 1347 packed = vmem_alloc(size, KM_SLEEP);
34dc7c2f 1348
9babb374
BB
1349 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1350 iflag)) != 0) {
77aef6f6 1351 vmem_free(packed, size);
0de7c552 1352 return (SET_ERROR(EFAULT));
34dc7c2f
BB
1353 }
1354
1355 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
77aef6f6 1356 vmem_free(packed, size);
34dc7c2f
BB
1357 return (error);
1358 }
1359
77aef6f6 1360 vmem_free(packed, size);
34dc7c2f
BB
1361
1362 *nvp = list;
1363 return (0);
1364}
1365
6f1ffb06
MA
1366/*
1367 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1368 * Entries will be removed from the end of the nvlist, and one int32 entry
1369 * named "N_MORE_ERRORS" will be added indicating how many entries were
1370 * removed.
1371 */
428870ff 1372static int
6f1ffb06 1373nvlist_smush(nvlist_t *errors, size_t max)
428870ff
BB
1374{
1375 size_t size;
1376
6f1ffb06 1377 size = fnvlist_size(errors);
428870ff 1378
6f1ffb06 1379 if (size > max) {
428870ff
BB
1380 nvpair_t *more_errors;
1381 int n = 0;
1382
6f1ffb06 1383 if (max < 1024)
2e528b49 1384 return (SET_ERROR(ENOMEM));
428870ff 1385
6f1ffb06
MA
1386 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1387 more_errors = nvlist_prev_nvpair(errors, NULL);
428870ff
BB
1388
1389 do {
6f1ffb06 1390 nvpair_t *pair = nvlist_prev_nvpair(errors,
428870ff 1391 more_errors);
6f1ffb06 1392 fnvlist_remove_nvpair(errors, pair);
428870ff 1393 n++;
6f1ffb06
MA
1394 size = fnvlist_size(errors);
1395 } while (size > max);
428870ff 1396
6f1ffb06
MA
1397 fnvlist_remove_nvpair(errors, more_errors);
1398 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1399 ASSERT3U(fnvlist_size(errors), <=, max);
428870ff
BB
1400 }
1401
1402 return (0);
1403}
1404
34dc7c2f
BB
1405static int
1406put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1407{
1408 char *packed = NULL;
428870ff 1409 int error = 0;
34dc7c2f 1410 size_t size;
34dc7c2f 1411
6f1ffb06 1412 size = fnvlist_size(nvl);
34dc7c2f
BB
1413
1414 if (size > zc->zc_nvlist_dst_size) {
2e528b49 1415 error = SET_ERROR(ENOMEM);
34dc7c2f 1416 } else {
6f1ffb06 1417 packed = fnvlist_pack(nvl, &size);
428870ff
BB
1418 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1419 size, zc->zc_iflags) != 0)
2e528b49 1420 error = SET_ERROR(EFAULT);
6f1ffb06 1421 fnvlist_pack_free(packed, size);
34dc7c2f
BB
1422 }
1423
1424 zc->zc_nvlist_dst_size = size;
6f1ffb06 1425 zc->zc_nvlist_dst_filled = B_TRUE;
34dc7c2f
BB
1426 return (error);
1427}
1428
d99a0153
CW
1429int
1430getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
9babb374 1431{
d99a0153 1432 int error = 0;
428870ff 1433 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2e528b49 1434 return (SET_ERROR(EINVAL));
428870ff 1435 }
9babb374 1436
428870ff 1437 mutex_enter(&os->os_user_ptr_lock);
0037b49e 1438 *zfvp = dmu_objset_get_user(os);
061460df 1439 /* bump s_active only when non-zero to prevent umount race */
0037b49e
BB
1440 if (*zfvp == NULL || (*zfvp)->z_sb == NULL ||
1441 !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) {
2e528b49 1442 error = SET_ERROR(ESRCH);
9babb374 1443 }
428870ff 1444 mutex_exit(&os->os_user_ptr_lock);
d99a0153
CW
1445 return (error);
1446}
1447
8d103d88 1448int
d99a0153
CW
1449getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1450{
1451 objset_t *os;
1452 int error;
1453
1454 error = dmu_objset_hold(dsname, FTAG, &os);
1455 if (error != 0)
1456 return (error);
1457
1458 error = getzfsvfs_impl(os, zfvp);
428870ff 1459 dmu_objset_rele(os, FTAG);
9babb374
BB
1460 return (error);
1461}
1462
1463/*
0037b49e 1464 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
2cf7f52b 1465 * case its z_sb will be NULL, and it will be opened as the owner.
9ae529ec
CS
1466 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1467 * which prevents all inode ops from running.
9babb374
BB
1468 */
1469static int
f298b24d 1470zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
9babb374
BB
1471{
1472 int error = 0;
9babb374 1473
f298b24d 1474 if (getzfsvfs(name, zfvp) != 0)
163a8c28 1475 error = zfsvfs_create(name, B_FALSE, zfvp);
9babb374 1476 if (error == 0) {
0037b49e 1477 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
572e2857 1478 RW_READER, tag);
0037b49e 1479 if ((*zfvp)->z_unmounted) {
9babb374
BB
1480 /*
1481 * XXX we could probably try again, since the unmounting
1482 * thread should be just about to disassociate the
0037b49e 1483 * objset from the zfsvfs.
9babb374 1484 */
0037b49e 1485 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
2e528b49 1486 return (SET_ERROR(EBUSY));
9babb374
BB
1487 }
1488 }
1489 return (error);
1490}
1491
1492static void
f298b24d 1493zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
9babb374 1494{
0037b49e 1495 rrm_exit(&zfsvfs->z_teardown_lock, tag);
9babb374 1496
0037b49e
BB
1497 if (zfsvfs->z_sb) {
1498 deactivate_super(zfsvfs->z_sb);
9babb374 1499 } else {
b5256303 1500 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
f298b24d 1501 zfsvfs_free(zfsvfs);
9babb374
BB
1502 }
1503}
1504
34dc7c2f
BB
1505static int
1506zfs_ioc_pool_create(zfs_cmd_t *zc)
1507{
1508 int error;
1509 nvlist_t *config, *props = NULL;
b128c09f
BB
1510 nvlist_t *rootprops = NULL;
1511 nvlist_t *zplprops = NULL;
b5256303 1512 dsl_crypto_params_t *dcp = NULL;
4ceb8dd6 1513 char *spa_name = zc->zc_name;
34dc7c2f 1514
c65aa5b2
BB
1515 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1516 zc->zc_iflags, &config)))
34dc7c2f
BB
1517 return (error);
1518
1519 if (zc->zc_nvlist_src_size != 0 && (error =
9babb374
BB
1520 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1521 zc->zc_iflags, &props))) {
34dc7c2f
BB
1522 nvlist_free(config);
1523 return (error);
1524 }
1525
b128c09f
BB
1526 if (props) {
1527 nvlist_t *nvl = NULL;
b5256303 1528 nvlist_t *hidden_args = NULL;
b128c09f 1529 uint64_t version = SPA_VERSION;
4ceb8dd6 1530 char *tname;
b128c09f
BB
1531
1532 (void) nvlist_lookup_uint64(props,
1533 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9ae529ec 1534 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2e528b49 1535 error = SET_ERROR(EINVAL);
b128c09f
BB
1536 goto pool_props_bad;
1537 }
1538 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1539 if (nvl) {
1540 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1541 if (error != 0) {
1542 nvlist_free(config);
1543 nvlist_free(props);
1544 return (error);
1545 }
1546 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1547 }
b5256303
TC
1548
1549 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1550 &hidden_args);
1551 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1552 rootprops, hidden_args, &dcp);
1553 if (error != 0) {
1554 nvlist_free(config);
1555 nvlist_free(props);
1556 return (error);
1557 }
1558 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1559
b128c09f
BB
1560 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1561 error = zfs_fill_zplprops_root(version, rootprops,
1562 zplprops, NULL);
13fe0198 1563 if (error != 0)
b128c09f 1564 goto pool_props_bad;
4ceb8dd6 1565
1566 if (nvlist_lookup_string(props,
1567 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1568 spa_name = tname;
b128c09f
BB
1569 }
1570
b5256303 1571 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
b128c09f
BB
1572
1573 /*
1574 * Set the remaining root properties
1575 */
4ceb8dd6 1576 if (!error && (error = zfs_set_prop_nvlist(spa_name,
428870ff 1577 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
4ceb8dd6 1578 (void) spa_destroy(spa_name);
34dc7c2f 1579
b128c09f
BB
1580pool_props_bad:
1581 nvlist_free(rootprops);
1582 nvlist_free(zplprops);
34dc7c2f 1583 nvlist_free(config);
b128c09f 1584 nvlist_free(props);
b5256303 1585 dsl_crypto_params_free(dcp, !!error);
34dc7c2f
BB
1586
1587 return (error);
1588}
1589
1590static int
1591zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1592{
1593 int error;
1594 zfs_log_history(zc);
1595 error = spa_destroy(zc->zc_name);
a0bd735a 1596
34dc7c2f
BB
1597 return (error);
1598}
1599
1600static int
1601zfs_ioc_pool_import(zfs_cmd_t *zc)
1602{
34dc7c2f
BB
1603 nvlist_t *config, *props = NULL;
1604 uint64_t guid;
428870ff 1605 int error;
34dc7c2f
BB
1606
1607 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1608 zc->zc_iflags, &config)) != 0)
34dc7c2f
BB
1609 return (error);
1610
1611 if (zc->zc_nvlist_src_size != 0 && (error =
9babb374
BB
1612 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1613 zc->zc_iflags, &props))) {
34dc7c2f
BB
1614 nvlist_free(config);
1615 return (error);
1616 }
1617
1618 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1619 guid != zc->zc_guid)
2e528b49 1620 error = SET_ERROR(EINVAL);
34dc7c2f 1621 else
572e2857 1622 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
34dc7c2f 1623
572e2857
BB
1624 if (zc->zc_nvlist_dst != 0) {
1625 int err;
1626
1627 if ((err = put_nvlist(zc, config)) != 0)
1628 error = err;
1629 }
428870ff 1630
34dc7c2f 1631 nvlist_free(config);
8a5fc748 1632 nvlist_free(props);
34dc7c2f
BB
1633
1634 return (error);
1635}
1636
1637static int
1638zfs_ioc_pool_export(zfs_cmd_t *zc)
1639{
1640 int error;
b128c09f 1641 boolean_t force = (boolean_t)zc->zc_cookie;
fb5f0bc8 1642 boolean_t hardforce = (boolean_t)zc->zc_guid;
b128c09f 1643
34dc7c2f 1644 zfs_log_history(zc);
fb5f0bc8 1645 error = spa_export(zc->zc_name, NULL, force, hardforce);
a0bd735a 1646
34dc7c2f
BB
1647 return (error);
1648}
1649
1650static int
1651zfs_ioc_pool_configs(zfs_cmd_t *zc)
1652{
1653 nvlist_t *configs;
1654 int error;
1655
1656 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
2e528b49 1657 return (SET_ERROR(EEXIST));
34dc7c2f
BB
1658
1659 error = put_nvlist(zc, configs);
1660
1661 nvlist_free(configs);
1662
1663 return (error);
1664}
1665
9ae529ec
CS
1666/*
1667 * inputs:
1668 * zc_name name of the pool
1669 *
1670 * outputs:
1671 * zc_cookie real errno
1672 * zc_nvlist_dst config nvlist
1673 * zc_nvlist_dst_size size of config nvlist
1674 */
34dc7c2f
BB
1675static int
1676zfs_ioc_pool_stats(zfs_cmd_t *zc)
1677{
1678 nvlist_t *config;
1679 int error;
1680 int ret = 0;
1681
1682 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1683 sizeof (zc->zc_value));
1684
1685 if (config != NULL) {
1686 ret = put_nvlist(zc, config);
1687 nvlist_free(config);
1688
1689 /*
1690 * The config may be present even if 'error' is non-zero.
1691 * In this case we return success, and preserve the real errno
1692 * in 'zc_cookie'.
1693 */
1694 zc->zc_cookie = error;
1695 } else {
1696 ret = error;
1697 }
1698
1699 return (ret);
1700}
1701
1702/*
1703 * Try to import the given pool, returning pool stats as appropriate so that
1704 * user land knows which devices are available and overall pool health.
1705 */
1706static int
1707zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1708{
379ca9cf 1709 nvlist_t *tryconfig, *config = NULL;
34dc7c2f
BB
1710 int error;
1711
1712 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1713 zc->zc_iflags, &tryconfig)) != 0)
34dc7c2f
BB
1714 return (error);
1715
1716 config = spa_tryimport(tryconfig);
1717
1718 nvlist_free(tryconfig);
1719
1720 if (config == NULL)
2e528b49 1721 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1722
1723 error = put_nvlist(zc, config);
1724 nvlist_free(config);
1725
1726 return (error);
1727}
1728
428870ff
BB
1729/*
1730 * inputs:
1731 * zc_name name of the pool
1732 * zc_cookie scan func (pool_scan_func_t)
0ea05c64 1733 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
428870ff 1734 */
34dc7c2f 1735static int
428870ff 1736zfs_ioc_pool_scan(zfs_cmd_t *zc)
34dc7c2f
BB
1737{
1738 spa_t *spa;
1739 int error;
1740
0ea05c64
AP
1741 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1742 return (SET_ERROR(EINVAL));
1743
1724eb62 1744 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1745 return (error);
1746
0ea05c64
AP
1747 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1748 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1749 else if (zc->zc_cookie == POOL_SCAN_NONE)
428870ff
BB
1750 error = spa_scan_stop(spa);
1751 else
1752 error = spa_scan(spa, zc->zc_cookie);
34dc7c2f
BB
1753
1754 spa_close(spa, FTAG);
1755
1756 return (error);
1757}
1758
1759static int
1760zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1761{
1762 spa_t *spa;
1763 int error;
1764
1765 error = spa_open(zc->zc_name, &spa, FTAG);
1766 if (error == 0) {
1767 spa_freeze(spa);
1768 spa_close(spa, FTAG);
1769 }
1770 return (error);
1771}
1772
1773static int
1774zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1775{
1776 spa_t *spa;
1777 int error;
1778
1779 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1780 return (error);
1781
9ae529ec
CS
1782 if (zc->zc_cookie < spa_version(spa) ||
1783 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
34dc7c2f 1784 spa_close(spa, FTAG);
2e528b49 1785 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1786 }
1787
1788 spa_upgrade(spa, zc->zc_cookie);
1789 spa_close(spa, FTAG);
1790
1791 return (error);
1792}
1793
1794static int
1795zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1796{
1797 spa_t *spa;
1798 char *hist_buf;
1799 uint64_t size;
1800 int error;
1801
1802 if ((size = zc->zc_history_len) == 0)
2e528b49 1803 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1804
1805 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1806 return (error);
1807
1808 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1809 spa_close(spa, FTAG);
2e528b49 1810 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1811 }
1812
34b84cb8 1813 hist_buf = vmem_alloc(size, KM_SLEEP);
34dc7c2f
BB
1814 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1815 &zc->zc_history_len, hist_buf)) == 0) {
9babb374
BB
1816 error = ddi_copyout(hist_buf,
1817 (void *)(uintptr_t)zc->zc_history,
1818 zc->zc_history_len, zc->zc_iflags);
34dc7c2f
BB
1819 }
1820
1821 spa_close(spa, FTAG);
34b84cb8 1822 vmem_free(hist_buf, size);
34dc7c2f
BB
1823 return (error);
1824}
1825
3541dc6d
GA
1826static int
1827zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1828{
1829 spa_t *spa;
1830 int error;
1831
1832 error = spa_open(zc->zc_name, &spa, FTAG);
1833 if (error == 0) {
1834 error = spa_change_guid(spa);
1835 spa_close(spa, FTAG);
1836 }
1837 return (error);
1838}
1839
34dc7c2f
BB
1840static int
1841zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1842{
13fe0198 1843 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
34dc7c2f
BB
1844}
1845
428870ff
BB
1846/*
1847 * inputs:
1848 * zc_name name of filesystem
1849 * zc_obj object to find
1850 *
1851 * outputs:
1852 * zc_value name of object
1853 */
34dc7c2f
BB
1854static int
1855zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1856{
428870ff 1857 objset_t *os;
34dc7c2f
BB
1858 int error;
1859
428870ff 1860 /* XXX reading from objset not owned */
b5256303
TC
1861 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1862 FTAG, &os)) != 0)
34dc7c2f 1863 return (error);
428870ff 1864 if (dmu_objset_type(os) != DMU_OST_ZFS) {
b5256303 1865 dmu_objset_rele_flags(os, B_TRUE, FTAG);
2e528b49 1866 return (SET_ERROR(EINVAL));
428870ff
BB
1867 }
1868 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
34dc7c2f 1869 sizeof (zc->zc_value));
b5256303 1870 dmu_objset_rele_flags(os, B_TRUE, FTAG);
34dc7c2f
BB
1871
1872 return (error);
1873}
1874
572e2857
BB
1875/*
1876 * inputs:
1877 * zc_name name of filesystem
1878 * zc_obj object to find
1879 *
1880 * outputs:
1881 * zc_stat stats on object
1882 * zc_value path to object
1883 */
1884static int
1885zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1886{
1887 objset_t *os;
1888 int error;
1889
1890 /* XXX reading from objset not owned */
b5256303
TC
1891 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1892 FTAG, &os)) != 0)
572e2857
BB
1893 return (error);
1894 if (dmu_objset_type(os) != DMU_OST_ZFS) {
b5256303 1895 dmu_objset_rele_flags(os, B_TRUE, FTAG);
2e528b49 1896 return (SET_ERROR(EINVAL));
572e2857
BB
1897 }
1898 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1899 sizeof (zc->zc_value));
b5256303 1900 dmu_objset_rele_flags(os, B_TRUE, FTAG);
572e2857
BB
1901
1902 return (error);
1903}
1904
34dc7c2f
BB
1905static int
1906zfs_ioc_vdev_add(zfs_cmd_t *zc)
1907{
1908 spa_t *spa;
1909 int error;
64ad2b26 1910 nvlist_t *config;
34dc7c2f
BB
1911
1912 error = spa_open(zc->zc_name, &spa, FTAG);
1913 if (error != 0)
1914 return (error);
1915
1916 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1917 zc->zc_iflags, &config);
34dc7c2f
BB
1918 if (error == 0) {
1919 error = spa_vdev_add(spa, config);
1920 nvlist_free(config);
1921 }
1922 spa_close(spa, FTAG);
1923 return (error);
1924}
1925
428870ff
BB
1926/*
1927 * inputs:
1928 * zc_name name of the pool
a1d477c2
MA
1929 * zc_guid guid of vdev to remove
1930 * zc_cookie cancel removal
428870ff 1931 */
34dc7c2f
BB
1932static int
1933zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1934{
1935 spa_t *spa;
1936 int error;
1937
1938 error = spa_open(zc->zc_name, &spa, FTAG);
1939 if (error != 0)
1940 return (error);
a1d477c2
MA
1941 if (zc->zc_cookie != 0) {
1942 error = spa_vdev_remove_cancel(spa);
1943 } else {
1944 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1945 }
34dc7c2f
BB
1946 spa_close(spa, FTAG);
1947 return (error);
1948}
1949
1950static int
1951zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1952{
1953 spa_t *spa;
1954 int error;
1955 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1956
1957 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1958 return (error);
1959 switch (zc->zc_cookie) {
1960 case VDEV_STATE_ONLINE:
1961 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1962 break;
1963
1964 case VDEV_STATE_OFFLINE:
1965 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1966 break;
1967
1968 case VDEV_STATE_FAULTED:
428870ff 1969 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
4a283c7f
TH
1970 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1971 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
428870ff
BB
1972 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1973
1974 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
34dc7c2f
BB
1975 break;
1976
1977 case VDEV_STATE_DEGRADED:
428870ff
BB
1978 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1979 zc->zc_obj != VDEV_AUX_EXTERNAL)
1980 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1981
1982 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
34dc7c2f
BB
1983 break;
1984
1985 default:
2e528b49 1986 error = SET_ERROR(EINVAL);
34dc7c2f
BB
1987 }
1988 zc->zc_cookie = newstate;
1989 spa_close(spa, FTAG);
1990 return (error);
1991}
1992
1993static int
1994zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1995{
1996 spa_t *spa;
1997 int replacing = zc->zc_cookie;
1998 nvlist_t *config;
1999 int error;
2000
2001 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2002 return (error);
2003
2004 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 2005 zc->zc_iflags, &config)) == 0) {
34dc7c2f
BB
2006 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2007 nvlist_free(config);
2008 }
2009
2010 spa_close(spa, FTAG);
2011 return (error);
2012}
2013
2014static int
2015zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2016{
2017 spa_t *spa;
2018 int error;
2019
2020 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2021 return (error);
2022
fb5f0bc8 2023 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
34dc7c2f
BB
2024
2025 spa_close(spa, FTAG);
2026 return (error);
2027}
2028
428870ff
BB
2029static int
2030zfs_ioc_vdev_split(zfs_cmd_t *zc)
2031{
2032 spa_t *spa;
2033 nvlist_t *config, *props = NULL;
2034 int error;
2035 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2036
2037 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2038 return (error);
2039
c65aa5b2
BB
2040 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2041 zc->zc_iflags, &config))) {
428870ff
BB
2042 spa_close(spa, FTAG);
2043 return (error);
2044 }
2045
2046 if (zc->zc_nvlist_src_size != 0 && (error =
2047 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2048 zc->zc_iflags, &props))) {
2049 spa_close(spa, FTAG);
2050 nvlist_free(config);
2051 return (error);
2052 }
2053
2054 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2055
2056 spa_close(spa, FTAG);
2057
2058 nvlist_free(config);
2059 nvlist_free(props);
2060
2061 return (error);
2062}
2063
34dc7c2f
BB
2064static int
2065zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2066{
2067 spa_t *spa;
2068 char *path = zc->zc_value;
2069 uint64_t guid = zc->zc_guid;
2070 int error;
2071
2072 error = spa_open(zc->zc_name, &spa, FTAG);
2073 if (error != 0)
2074 return (error);
2075
2076 error = spa_vdev_setpath(spa, guid, path);
2077 spa_close(spa, FTAG);
2078 return (error);
2079}
2080
9babb374
BB
2081static int
2082zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2083{
2084 spa_t *spa;
2085 char *fru = zc->zc_value;
2086 uint64_t guid = zc->zc_guid;
2087 int error;
2088
2089 error = spa_open(zc->zc_name, &spa, FTAG);
2090 if (error != 0)
2091 return (error);
2092
2093 error = spa_vdev_setfru(spa, guid, fru);
2094 spa_close(spa, FTAG);
2095 return (error);
2096}
2097
34dc7c2f 2098static int
572e2857 2099zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
34dc7c2f 2100{
572e2857 2101 int error = 0;
34dc7c2f
BB
2102 nvlist_t *nv;
2103
34dc7c2f
BB
2104 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2105
2106 if (zc->zc_nvlist_dst != 0 &&
428870ff 2107 (error = dsl_prop_get_all(os, &nv)) == 0) {
34dc7c2f
BB
2108 dmu_objset_stats(os, nv);
2109 /*
2110 * NB: zvol_get_stats() will read the objset contents,
2111 * which we aren't supposed to do with a
b128c09f 2112 * DS_MODE_USER hold, because it could be
34dc7c2f 2113 * inconsistent. So this is a bit of a workaround...
428870ff 2114 * XXX reading with out owning
34dc7c2f 2115 */
330d06f9
MA
2116 if (!zc->zc_objset_stats.dds_inconsistent &&
2117 dmu_objset_type(os) == DMU_OST_ZVOL) {
2118 error = zvol_get_stats(os, nv);
6d421005
B
2119 if (error == EIO) {
2120 nvlist_free(nv);
330d06f9 2121 return (error);
6d421005 2122 }
c99c9001 2123 VERIFY0(error);
34dc7c2f 2124 }
8a8f5c6b
BB
2125 if (error == 0)
2126 error = put_nvlist(zc, nv);
34dc7c2f
BB
2127 nvlist_free(nv);
2128 }
2129
572e2857
BB
2130 return (error);
2131}
2132
2133/*
2134 * inputs:
2135 * zc_name name of filesystem
2136 * zc_nvlist_dst_size size of buffer for property nvlist
2137 *
2138 * outputs:
2139 * zc_objset_stats stats
2140 * zc_nvlist_dst property nvlist
2141 * zc_nvlist_dst_size size of property nvlist
2142 */
2143static int
2144zfs_ioc_objset_stats(zfs_cmd_t *zc)
2145{
13fe0198 2146 objset_t *os;
572e2857
BB
2147 int error;
2148
13fe0198
MA
2149 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2150 if (error == 0) {
2151 error = zfs_ioc_objset_stats_impl(zc, os);
2152 dmu_objset_rele(os, FTAG);
2153 }
572e2857 2154
428870ff
BB
2155 return (error);
2156}
2157
2158/*
2159 * inputs:
2160 * zc_name name of filesystem
2161 * zc_nvlist_dst_size size of buffer for property nvlist
2162 *
2163 * outputs:
2164 * zc_nvlist_dst received property nvlist
2165 * zc_nvlist_dst_size size of received property nvlist
2166 *
2167 * Gets received properties (distinct from local properties on or after
2168 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2169 * local property values.
2170 */
2171static int
26685276 2172zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
428870ff 2173{
13fe0198 2174 int error = 0;
428870ff
BB
2175 nvlist_t *nv;
2176
428870ff
BB
2177 /*
2178 * Without this check, we would return local property values if the
2179 * caller has not already received properties on or after
2180 * SPA_VERSION_RECVD_PROPS.
2181 */
13fe0198 2182 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2e528b49 2183 return (SET_ERROR(ENOTSUP));
428870ff
BB
2184
2185 if (zc->zc_nvlist_dst != 0 &&
13fe0198 2186 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
428870ff
BB
2187 error = put_nvlist(zc, nv);
2188 nvlist_free(nv);
2189 }
2190
34dc7c2f
BB
2191 return (error);
2192}
2193
2194static int
2195nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2196{
2197 uint64_t value;
2198 int error;
2199
2200 /*
2201 * zfs_get_zplprop() will either find a value or give us
2202 * the default value (if there is one).
2203 */
2204 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2205 return (error);
2206 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2207 return (0);
2208}
2209
2210/*
2211 * inputs:
2212 * zc_name name of filesystem
2213 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2214 *
2215 * outputs:
2216 * zc_nvlist_dst zpl property nvlist
2217 * zc_nvlist_dst_size size of zpl property nvlist
2218 */
2219static int
2220zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2221{
2222 objset_t *os;
2223 int err;
2224
428870ff 2225 /* XXX reading without owning */
c65aa5b2 2226 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
34dc7c2f
BB
2227 return (err);
2228
2229 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2230
2231 /*
2232 * NB: nvl_add_zplprop() will read the objset contents,
b128c09f
BB
2233 * which we aren't supposed to do with a DS_MODE_USER
2234 * hold, because it could be inconsistent.
34dc7c2f 2235 */
b8864a23 2236 if (zc->zc_nvlist_dst != 0 &&
34dc7c2f
BB
2237 !zc->zc_objset_stats.dds_inconsistent &&
2238 dmu_objset_type(os) == DMU_OST_ZFS) {
2239 nvlist_t *nv;
2240
2241 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2242 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2243 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2244 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2245 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2246 err = put_nvlist(zc, nv);
2247 nvlist_free(nv);
2248 } else {
2e528b49 2249 err = SET_ERROR(ENOENT);
34dc7c2f 2250 }
428870ff 2251 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
2252 return (err);
2253}
2254
2255/*
2256 * inputs:
2257 * zc_name name of filesystem
2258 * zc_cookie zap cursor
2259 * zc_nvlist_dst_size size of buffer for property nvlist
2260 *
2261 * outputs:
2262 * zc_name name of next filesystem
9babb374 2263 * zc_cookie zap cursor
34dc7c2f
BB
2264 * zc_objset_stats stats
2265 * zc_nvlist_dst property nvlist
2266 * zc_nvlist_dst_size size of property nvlist
34dc7c2f
BB
2267 */
2268static int
2269zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2270{
2271 objset_t *os;
2272 int error;
2273 char *p;
428870ff 2274 size_t orig_len = strlen(zc->zc_name);
34dc7c2f 2275
428870ff 2276top:
c65aa5b2 2277 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
34dc7c2f 2278 if (error == ENOENT)
2e528b49 2279 error = SET_ERROR(ESRCH);
34dc7c2f
BB
2280 return (error);
2281 }
2282
2283 p = strrchr(zc->zc_name, '/');
2284 if (p == NULL || p[1] != '\0')
2285 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2286 p = zc->zc_name + strlen(zc->zc_name);
2287
2288 do {
2289 error = dmu_dir_list_next(os,
2290 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2291 NULL, &zc->zc_cookie);
2292 if (error == ENOENT)
2e528b49 2293 error = SET_ERROR(ESRCH);
2e5dc449 2294 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
428870ff 2295 dmu_objset_rele(os, FTAG);
34dc7c2f 2296
428870ff
BB
2297 /*
2298 * If it's an internal dataset (ie. with a '$' in its name),
2299 * don't try to get stats for it, otherwise we'll return ENOENT.
2300 */
2301 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
34dc7c2f 2302 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
428870ff
BB
2303 if (error == ENOENT) {
2304 /* We lost a race with destroy, get the next one. */
2305 zc->zc_name[orig_len] = '\0';
2306 goto top;
2307 }
2308 }
34dc7c2f
BB
2309 return (error);
2310}
2311
2312/*
2313 * inputs:
2314 * zc_name name of filesystem
2315 * zc_cookie zap cursor
2316 * zc_nvlist_dst_size size of buffer for property nvlist
2317 *
2318 * outputs:
2319 * zc_name name of next snapshot
2320 * zc_objset_stats stats
2321 * zc_nvlist_dst property nvlist
2322 * zc_nvlist_dst_size size of property nvlist
34dc7c2f
BB
2323 */
2324static int
2325zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2326{
2327 objset_t *os;
2328 int error;
2329
428870ff 2330 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
13fe0198 2331 if (error != 0) {
b128c09f 2332 return (error == ENOENT ? ESRCH : error);
13fe0198 2333 }
34dc7c2f
BB
2334
2335 /*
2336 * A dataset name of maximum length cannot have any snapshots,
2337 * so exit immediately.
2338 */
eca7b760
IK
2339 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2340 ZFS_MAX_DATASET_NAME_LEN) {
428870ff 2341 dmu_objset_rele(os, FTAG);
2e528b49 2342 return (SET_ERROR(ESRCH));
34dc7c2f
BB
2343 }
2344
2345 error = dmu_snapshot_list_next(os,
2346 sizeof (zc->zc_name) - strlen(zc->zc_name),
572e2857
BB
2347 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2348 NULL);
2349
0cee2406 2350 if (error == 0 && !zc->zc_simple) {
572e2857
BB
2351 dsl_dataset_t *ds;
2352 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2353
572e2857 2354 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
13fe0198 2355 if (error == 0) {
572e2857
BB
2356 objset_t *ossnap;
2357
2358 error = dmu_objset_from_ds(ds, &ossnap);
2359 if (error == 0)
2360 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2361 dsl_dataset_rele(ds, FTAG);
428870ff
BB
2362 }
2363 } else if (error == ENOENT) {
2e528b49 2364 error = SET_ERROR(ESRCH);
428870ff 2365 }
34dc7c2f 2366
572e2857 2367 dmu_objset_rele(os, FTAG);
34dc7c2f 2368 /* if we failed, undo the @ that we tacked on to zc_name */
13fe0198 2369 if (error != 0)
34dc7c2f 2370 *strchr(zc->zc_name, '@') = '\0';
34dc7c2f
BB
2371 return (error);
2372}
2373
428870ff
BB
2374static int
2375zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
34dc7c2f 2376{
428870ff
BB
2377 const char *propname = nvpair_name(pair);
2378 uint64_t *valary;
2379 unsigned int vallen;
2380 const char *domain;
2381 char *dash;
2382 zfs_userquota_prop_t type;
2383 uint64_t rid;
2384 uint64_t quota;
0037b49e 2385 zfsvfs_t *zfsvfs;
428870ff
BB
2386 int err;
2387
2388 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2389 nvlist_t *attrs;
2390 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2391 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2392 &pair) != 0)
2e528b49 2393 return (SET_ERROR(EINVAL));
428870ff 2394 }
34dc7c2f
BB
2395
2396 /*
428870ff
BB
2397 * A correctly constructed propname is encoded as
2398 * userquota@<rid>-<domain>.
34dc7c2f 2399 */
428870ff
BB
2400 if ((dash = strchr(propname, '-')) == NULL ||
2401 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2402 vallen != 3)
2e528b49 2403 return (SET_ERROR(EINVAL));
34dc7c2f 2404
428870ff
BB
2405 domain = dash + 1;
2406 type = valary[0];
2407 rid = valary[1];
2408 quota = valary[2];
34dc7c2f 2409
f298b24d 2410 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
428870ff 2411 if (err == 0) {
0037b49e 2412 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
f298b24d 2413 zfsvfs_rele(zfsvfs, FTAG);
428870ff 2414 }
9babb374 2415
428870ff
BB
2416 return (err);
2417}
34dc7c2f 2418
428870ff
BB
2419/*
2420 * If the named property is one that has a special function to set its value,
2421 * return 0 on success and a positive error code on failure; otherwise if it is
2422 * not one of the special properties handled by this function, return -1.
2423 *
2424 * XXX: It would be better for callers of the property interface if we handled
2425 * these special cases in dsl_prop.c (in the dsl layer).
2426 */
2427static int
2428zfs_prop_set_special(const char *dsname, zprop_source_t source,
2429 nvpair_t *pair)
2430{
2431 const char *propname = nvpair_name(pair);
2432 zfs_prop_t prop = zfs_name_to_prop(propname);
b5256303
TC
2433 uint64_t intval = 0;
2434 char *strval = NULL;
f1512ee6 2435 int err = -1;
9babb374 2436
428870ff
BB
2437 if (prop == ZPROP_INVAL) {
2438 if (zfs_prop_userquota(propname))
2439 return (zfs_prop_set_userquota(dsname, pair));
2440 return (-1);
2441 }
34dc7c2f 2442
428870ff
BB
2443 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2444 nvlist_t *attrs;
2445 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2446 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2447 &pair) == 0);
2448 }
b128c09f 2449
b5256303
TC
2450 /* all special properties are numeric except for keylocation */
2451 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2452 strval = fnvpair_value_string(pair);
2453 } else {
2454 intval = fnvpair_value_uint64(pair);
2455 }
34dc7c2f 2456
428870ff
BB
2457 switch (prop) {
2458 case ZFS_PROP_QUOTA:
2459 err = dsl_dir_set_quota(dsname, source, intval);
2460 break;
2461 case ZFS_PROP_REFQUOTA:
13fe0198 2462 err = dsl_dataset_set_refquota(dsname, source, intval);
428870ff 2463 break;
788eb90c
JJ
2464 case ZFS_PROP_FILESYSTEM_LIMIT:
2465 case ZFS_PROP_SNAPSHOT_LIMIT:
2466 if (intval == UINT64_MAX) {
2467 /* clearing the limit, just do it */
2468 err = 0;
2469 } else {
2470 err = dsl_dir_activate_fs_ss_limit(dsname);
2471 }
b5256303
TC
2472 /*
2473 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2474 * default path to set the value in the nvlist.
2475 */
2476 if (err == 0)
2477 err = -1;
2478 break;
2479 case ZFS_PROP_KEYLOCATION:
2480 err = dsl_crypto_can_set_keylocation(dsname, strval);
2481
788eb90c
JJ
2482 /*
2483 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2484 * default path to set the value in the nvlist.
2485 */
2486 if (err == 0)
2487 err = -1;
2488 break;
428870ff
BB
2489 case ZFS_PROP_RESERVATION:
2490 err = dsl_dir_set_reservation(dsname, source, intval);
2491 break;
2492 case ZFS_PROP_REFRESERVATION:
13fe0198 2493 err = dsl_dataset_set_refreservation(dsname, source, intval);
428870ff
BB
2494 break;
2495 case ZFS_PROP_VOLSIZE:
60101509 2496 err = zvol_set_volsize(dsname, intval);
428870ff 2497 break;
0b4d1b58 2498 case ZFS_PROP_SNAPDEV:
a0bd735a 2499 err = zvol_set_snapdev(dsname, source, intval);
0b4d1b58 2500 break;
cf8738d8 2501 case ZFS_PROP_VOLMODE:
2502 err = zvol_set_volmode(dsname, source, intval);
2503 break;
428870ff
BB
2504 case ZFS_PROP_VERSION:
2505 {
0037b49e 2506 zfsvfs_t *zfsvfs;
428870ff 2507
f298b24d 2508 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
34dc7c2f 2509 break;
b128c09f 2510
0037b49e 2511 err = zfs_set_version(zfsvfs, intval);
f298b24d 2512 zfsvfs_rele(zfsvfs, FTAG);
34dc7c2f 2513
428870ff
BB
2514 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2515 zfs_cmd_t *zc;
34dc7c2f 2516
efcd79a8 2517 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
428870ff
BB
2518 (void) strcpy(zc->zc_name, dsname);
2519 (void) zfs_ioc_userspace_upgrade(zc);
9c5167d1 2520 (void) zfs_ioc_id_quota_upgrade(zc);
428870ff 2521 kmem_free(zc, sizeof (zfs_cmd_t));
34dc7c2f 2522 }
428870ff
BB
2523 break;
2524 }
428870ff
BB
2525 default:
2526 err = -1;
2527 }
34dc7c2f 2528
428870ff
BB
2529 return (err);
2530}
34dc7c2f 2531
428870ff
BB
2532/*
2533 * This function is best effort. If it fails to set any of the given properties,
6f1ffb06
MA
2534 * it continues to set as many as it can and returns the last error
2535 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2536 * with the list of names of all the properties that failed along with the
2537 * corresponding error numbers.
428870ff 2538 *
6f1ffb06
MA
2539 * If every property is set successfully, zero is returned and errlist is not
2540 * modified.
428870ff
BB
2541 */
2542int
2543zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
6f1ffb06 2544 nvlist_t *errlist)
428870ff
BB
2545{
2546 nvpair_t *pair;
2547 nvpair_t *propval;
2548 int rv = 0;
2549 uint64_t intval;
2550 char *strval;
34dc7c2f 2551
6f1ffb06
MA
2552 nvlist_t *genericnvl = fnvlist_alloc();
2553 nvlist_t *retrynvl = fnvlist_alloc();
428870ff
BB
2554retry:
2555 pair = NULL;
2556 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2557 const char *propname = nvpair_name(pair);
2558 zfs_prop_t prop = zfs_name_to_prop(propname);
2559 int err = 0;
2560
2561 /* decode the property value */
2562 propval = pair;
2563 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2564 nvlist_t *attrs;
6f1ffb06 2565 attrs = fnvpair_value_nvlist(pair);
428870ff
BB
2566 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2567 &propval) != 0)
2e528b49 2568 err = SET_ERROR(EINVAL);
9babb374 2569 }
34dc7c2f 2570
428870ff 2571 /* Validate value type */
a3eeab2d 2572 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2573 /* inherited properties are expected to be booleans */
2574 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2575 err = SET_ERROR(EINVAL);
2576 } else if (err == 0 && prop == ZPROP_INVAL) {
428870ff
BB
2577 if (zfs_prop_user(propname)) {
2578 if (nvpair_type(propval) != DATA_TYPE_STRING)
2e528b49 2579 err = SET_ERROR(EINVAL);
428870ff
BB
2580 } else if (zfs_prop_userquota(propname)) {
2581 if (nvpair_type(propval) !=
2582 DATA_TYPE_UINT64_ARRAY)
2e528b49 2583 err = SET_ERROR(EINVAL);
330d06f9 2584 } else {
2e528b49 2585 err = SET_ERROR(EINVAL);
428870ff
BB
2586 }
2587 } else if (err == 0) {
2588 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2589 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2e528b49 2590 err = SET_ERROR(EINVAL);
428870ff 2591 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
34dc7c2f
BB
2592 const char *unused;
2593
6f1ffb06 2594 intval = fnvpair_value_uint64(propval);
34dc7c2f
BB
2595
2596 switch (zfs_prop_get_type(prop)) {
2597 case PROP_TYPE_NUMBER:
2598 break;
2599 case PROP_TYPE_STRING:
2e528b49 2600 err = SET_ERROR(EINVAL);
428870ff 2601 break;
34dc7c2f
BB
2602 case PROP_TYPE_INDEX:
2603 if (zfs_prop_index_to_string(prop,
428870ff 2604 intval, &unused) != 0)
2e528b49 2605 err = SET_ERROR(EINVAL);
34dc7c2f
BB
2606 break;
2607 default:
2608 cmn_err(CE_PANIC,
2609 "unknown property type");
34dc7c2f 2610 }
34dc7c2f 2611 } else {
2e528b49 2612 err = SET_ERROR(EINVAL);
34dc7c2f 2613 }
34dc7c2f 2614 }
428870ff
BB
2615
2616 /* Validate permissions */
2617 if (err == 0)
2618 err = zfs_check_settable(dsname, pair, CRED());
2619
2620 if (err == 0) {
a3eeab2d 2621 if (source == ZPROP_SRC_INHERITED)
2622 err = -1; /* does not need special handling */
2623 else
2624 err = zfs_prop_set_special(dsname, source,
2625 pair);
428870ff
BB
2626 if (err == -1) {
2627 /*
2628 * For better performance we build up a list of
2629 * properties to set in a single transaction.
2630 */
2631 err = nvlist_add_nvpair(genericnvl, pair);
2632 } else if (err != 0 && nvl != retrynvl) {
2633 /*
2634 * This may be a spurious error caused by
2635 * receiving quota and reservation out of order.
2636 * Try again in a second pass.
2637 */
2638 err = nvlist_add_nvpair(retrynvl, pair);
2639 }
2640 }
2641
6f1ffb06
MA
2642 if (err != 0) {
2643 if (errlist != NULL)
2644 fnvlist_add_int32(errlist, propname, err);
2645 rv = err;
2646 }
34dc7c2f
BB
2647 }
2648
428870ff
BB
2649 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2650 nvl = retrynvl;
2651 goto retry;
2652 }
2653
2654 if (!nvlist_empty(genericnvl) &&
2655 dsl_props_set(dsname, source, genericnvl) != 0) {
2656 /*
2657 * If this fails, we still want to set as many properties as we
2658 * can, so try setting them individually.
2659 */
2660 pair = NULL;
2661 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2662 const char *propname = nvpair_name(pair);
2663 int err = 0;
2664
2665 propval = pair;
2666 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2667 nvlist_t *attrs;
6f1ffb06
MA
2668 attrs = fnvpair_value_nvlist(pair);
2669 propval = fnvlist_lookup_nvpair(attrs,
2670 ZPROP_VALUE);
428870ff
BB
2671 }
2672
2673 if (nvpair_type(propval) == DATA_TYPE_STRING) {
6f1ffb06 2674 strval = fnvpair_value_string(propval);
13fe0198
MA
2675 err = dsl_prop_set_string(dsname, propname,
2676 source, strval);
a3eeab2d 2677 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2678 err = dsl_prop_inherit(dsname, propname,
2679 source);
428870ff 2680 } else {
6f1ffb06 2681 intval = fnvpair_value_uint64(propval);
13fe0198
MA
2682 err = dsl_prop_set_int(dsname, propname, source,
2683 intval);
428870ff
BB
2684 }
2685
2686 if (err != 0) {
6f1ffb06
MA
2687 if (errlist != NULL) {
2688 fnvlist_add_int32(errlist, propname,
2689 err);
2690 }
2691 rv = err;
428870ff
BB
2692 }
2693 }
9babb374 2694 }
9babb374 2695 nvlist_free(genericnvl);
428870ff
BB
2696 nvlist_free(retrynvl);
2697
428870ff 2698 return (rv);
9babb374
BB
2699}
2700
2701/*
2702 * Check that all the properties are valid user properties.
2703 */
2704static int
6f1ffb06 2705zfs_check_userprops(const char *fsname, nvlist_t *nvl)
9babb374 2706{
428870ff 2707 nvpair_t *pair = NULL;
9babb374
BB
2708 int error = 0;
2709
428870ff
BB
2710 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2711 const char *propname = nvpair_name(pair);
9babb374
BB
2712
2713 if (!zfs_prop_user(propname) ||
428870ff 2714 nvpair_type(pair) != DATA_TYPE_STRING)
2e528b49 2715 return (SET_ERROR(EINVAL));
9babb374 2716
c65aa5b2
BB
2717 if ((error = zfs_secpolicy_write_perms(fsname,
2718 ZFS_DELEG_PERM_USERPROP, CRED())))
9babb374
BB
2719 return (error);
2720
2721 if (strlen(propname) >= ZAP_MAXNAMELEN)
2e528b49 2722 return (SET_ERROR(ENAMETOOLONG));
9babb374 2723
da536844 2724 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2e528b49 2725 return (SET_ERROR(E2BIG));
9babb374 2726 }
34dc7c2f
BB
2727 return (0);
2728}
2729
428870ff
BB
2730static void
2731props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2732{
2733 nvpair_t *pair;
2734
2735 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2736
2737 pair = NULL;
2738 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2739 if (nvlist_exists(skipped, nvpair_name(pair)))
2740 continue;
2741
2742 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2743 }
2744}
2745
2746static int
13fe0198 2747clear_received_props(const char *dsname, nvlist_t *props,
428870ff
BB
2748 nvlist_t *skipped)
2749{
2750 int err = 0;
2751 nvlist_t *cleared_props = NULL;
2752 props_skip(props, skipped, &cleared_props);
2753 if (!nvlist_empty(cleared_props)) {
2754 /*
2755 * Acts on local properties until the dataset has received
2756 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2757 */
2758 zprop_source_t flags = (ZPROP_SRC_NONE |
13fe0198
MA
2759 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2760 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
428870ff
BB
2761 }
2762 nvlist_free(cleared_props);
2763 return (err);
2764}
2765
34dc7c2f
BB
2766/*
2767 * inputs:
2768 * zc_name name of filesystem
9babb374 2769 * zc_value name of property to set
34dc7c2f 2770 * zc_nvlist_src{_size} nvlist of properties to apply
428870ff 2771 * zc_cookie received properties flag
34dc7c2f 2772 *
428870ff
BB
2773 * outputs:
2774 * zc_nvlist_dst{_size} error for each unapplied received property
34dc7c2f
BB
2775 */
2776static int
2777zfs_ioc_set_prop(zfs_cmd_t *zc)
2778{
2779 nvlist_t *nvl;
428870ff
BB
2780 boolean_t received = zc->zc_cookie;
2781 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2782 ZPROP_SRC_LOCAL);
6f1ffb06 2783 nvlist_t *errors;
34dc7c2f
BB
2784 int error;
2785
2786 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9babb374 2787 zc->zc_iflags, &nvl)) != 0)
34dc7c2f
BB
2788 return (error);
2789
428870ff 2790 if (received) {
b128c09f 2791 nvlist_t *origprops;
b128c09f 2792
13fe0198
MA
2793 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2794 (void) clear_received_props(zc->zc_name,
2795 origprops, nvl);
2796 nvlist_free(origprops);
428870ff 2797 }
13fe0198
MA
2798
2799 error = dsl_prop_set_hasrecvd(zc->zc_name);
b128c09f
BB
2800 }
2801
6f1ffb06 2802 errors = fnvlist_alloc();
13fe0198
MA
2803 if (error == 0)
2804 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
34dc7c2f 2805
b8864a23 2806 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
428870ff
BB
2807 (void) put_nvlist(zc, errors);
2808 }
2809
2810 nvlist_free(errors);
34dc7c2f
BB
2811 nvlist_free(nvl);
2812 return (error);
2813}
2814
2815/*
2816 * inputs:
2817 * zc_name name of filesystem
2818 * zc_value name of property to inherit
428870ff 2819 * zc_cookie revert to received value if TRUE
34dc7c2f
BB
2820 *
2821 * outputs: none
2822 */
2823static int
2824zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2825{
428870ff
BB
2826 const char *propname = zc->zc_value;
2827 zfs_prop_t prop = zfs_name_to_prop(propname);
2828 boolean_t received = zc->zc_cookie;
2829 zprop_source_t source = (received
2830 ? ZPROP_SRC_NONE /* revert to received value, if any */
2831 : ZPROP_SRC_INHERITED); /* explicitly inherit */
92aceb2a 2832 nvlist_t *dummy;
2833 nvpair_t *pair;
2834 zprop_type_t type;
2835 int err;
428870ff 2836
92aceb2a 2837 if (!received) {
261c013f
BB
2838 /*
2839 * Only check this in the non-received case. We want to allow
2840 * 'inherit -S' to revert non-inheritable properties like quota
2841 * and reservation to the received or default values even though
2842 * they are not considered inheritable.
2843 */
2844 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2845 return (SET_ERROR(EINVAL));
959f56b9 2846 }
2847
92aceb2a 2848 if (prop == ZPROP_INVAL) {
2849 if (!zfs_prop_user(propname))
2850 return (SET_ERROR(EINVAL));
2851
2852 type = PROP_TYPE_STRING;
2853 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2854 return (SET_ERROR(EINVAL));
2855 } else {
2856 type = zfs_prop_get_type(prop);
2857 }
2858
2859 /*
2860 * zfs_prop_set_special() expects properties in the form of an
2861 * nvpair with type info.
2862 */
2863 dummy = fnvlist_alloc();
2864
2865 switch (type) {
2866 case PROP_TYPE_STRING:
2867 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2868 break;
2869 case PROP_TYPE_NUMBER:
2870 case PROP_TYPE_INDEX:
2871 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2872 break;
2873 default:
2874 err = SET_ERROR(EINVAL);
2875 goto errout;
2876 }
2877
2878 pair = nvlist_next_nvpair(dummy, NULL);
2879 if (pair == NULL) {
2880 err = SET_ERROR(EINVAL);
2881 } else {
2882 err = zfs_prop_set_special(zc->zc_name, source, pair);
2883 if (err == -1) /* property is not "special", needs handling */
2884 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2885 source);
2886 }
2887
2888errout:
2889 nvlist_free(dummy);
2890 return (err);
34dc7c2f
BB
2891}
2892
2893static int
2894zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2895{
2896 nvlist_t *props;
2897 spa_t *spa;
2898 int error;
428870ff 2899 nvpair_t *pair;
34dc7c2f 2900
c65aa5b2
BB
2901 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2902 zc->zc_iflags, &props)))
34dc7c2f
BB
2903 return (error);
2904
d164b209
BB
2905 /*
2906 * If the only property is the configfile, then just do a spa_lookup()
2907 * to handle the faulted case.
2908 */
428870ff
BB
2909 pair = nvlist_next_nvpair(props, NULL);
2910 if (pair != NULL && strcmp(nvpair_name(pair),
d164b209 2911 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
428870ff 2912 nvlist_next_nvpair(props, pair) == NULL) {
d164b209
BB
2913 mutex_enter(&spa_namespace_lock);
2914 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2915 spa_configfile_set(spa, props, B_FALSE);
a1d477c2 2916 spa_write_cachefile(spa, B_FALSE, B_TRUE);
d164b209
BB
2917 }
2918 mutex_exit(&spa_namespace_lock);
428870ff
BB
2919 if (spa != NULL) {
2920 nvlist_free(props);
d164b209 2921 return (0);
428870ff 2922 }
d164b209
BB
2923 }
2924
34dc7c2f
BB
2925 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2926 nvlist_free(props);
2927 return (error);
2928 }
2929
2930 error = spa_prop_set(spa, props);
2931
2932 nvlist_free(props);
2933 spa_close(spa, FTAG);
2934
2935 return (error);
2936}
2937
2938static int
2939zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2940{
2941 spa_t *spa;
2942 int error;
2943 nvlist_t *nvp = NULL;
2944
d164b209
BB
2945 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2946 /*
2947 * If the pool is faulted, there may be properties we can still
2948 * get (such as altroot and cachefile), so attempt to get them
2949 * anyway.
2950 */
2951 mutex_enter(&spa_namespace_lock);
2952 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2953 error = spa_prop_get(spa, &nvp);
2954 mutex_exit(&spa_namespace_lock);
2955 } else {
2956 error = spa_prop_get(spa, &nvp);
2957 spa_close(spa, FTAG);
2958 }
34dc7c2f 2959
b8864a23 2960 if (error == 0 && zc->zc_nvlist_dst != 0)
34dc7c2f
BB
2961 error = put_nvlist(zc, nvp);
2962 else
2e528b49 2963 error = SET_ERROR(EFAULT);
34dc7c2f 2964
d164b209 2965 nvlist_free(nvp);
34dc7c2f
BB
2966 return (error);
2967}
2968
34dc7c2f
BB
2969/*
2970 * inputs:
2971 * zc_name name of filesystem
2972 * zc_nvlist_src{_size} nvlist of delegated permissions
2973 * zc_perm_action allow/unallow flag
2974 *
2975 * outputs: none
2976 */
2977static int
2978zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2979{
2980 int error;
2981 nvlist_t *fsaclnv = NULL;
2982
2983 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9babb374 2984 zc->zc_iflags, &fsaclnv)) != 0)
34dc7c2f
BB
2985 return (error);
2986
2987 /*
2988 * Verify nvlist is constructed correctly
2989 */
2990 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2991 nvlist_free(fsaclnv);
2e528b49 2992 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2993 }
2994
2995 /*
2996 * If we don't have PRIV_SYS_MOUNT, then validate
2997 * that user is allowed to hand out each permission in
2998 * the nvlist(s)
2999 */
3000
3001 error = secpolicy_zfs(CRED());
13fe0198 3002 if (error != 0) {
34dc7c2f
BB
3003 if (zc->zc_perm_action == B_FALSE) {
3004 error = dsl_deleg_can_allow(zc->zc_name,
3005 fsaclnv, CRED());
3006 } else {
3007 error = dsl_deleg_can_unallow(zc->zc_name,
3008 fsaclnv, CRED());
3009 }
3010 }
3011
3012 if (error == 0)
3013 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3014
3015 nvlist_free(fsaclnv);
3016 return (error);
3017}
3018
3019/*
3020 * inputs:
3021 * zc_name name of filesystem
3022 *
3023 * outputs:
3024 * zc_nvlist_src{_size} nvlist of delegated permissions
3025 */
3026static int
3027zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3028{
3029 nvlist_t *nvp;
3030 int error;
3031
3032 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3033 error = put_nvlist(zc, nvp);
3034 nvlist_free(nvp);
3035 }
3036
3037 return (error);
3038}
3039
34dc7c2f
BB
3040/* ARGSUSED */
3041static void
3042zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3043{
3044 zfs_creat_t *zct = arg;
3045
3046 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3047}
3048
3049#define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3050
3051/*
3052 * inputs:
b128c09f 3053 * os parent objset pointer (NULL if root fs)
d3cc8b15
WA
3054 * fuids_ok fuids allowed in this version of the spa?
3055 * sa_ok SAs allowed in this version of the spa?
3056 * createprops list of properties requested by creator
34dc7c2f
BB
3057 *
3058 * outputs:
3059 * zplprops values for the zplprops we attach to the master node object
b128c09f 3060 * is_ci true if requested file system will be purely case-insensitive
34dc7c2f
BB
3061 *
3062 * Determine the settings for utf8only, normalization and
3063 * casesensitivity. Specific values may have been requested by the
3064 * creator and/or we can inherit values from the parent dataset. If
3065 * the file system is of too early a vintage, a creator can not
3066 * request settings for these properties, even if the requested
3067 * setting is the default value. We don't actually want to create dsl
3068 * properties for these, so remove them from the source nvlist after
3069 * processing.
3070 */
3071static int
9babb374 3072zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
428870ff
BB
3073 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3074 nvlist_t *zplprops, boolean_t *is_ci)
34dc7c2f 3075{
34dc7c2f
BB
3076 uint64_t sense = ZFS_PROP_UNDEFINED;
3077 uint64_t norm = ZFS_PROP_UNDEFINED;
3078 uint64_t u8 = ZFS_PROP_UNDEFINED;
b129c659 3079 int error;
34dc7c2f
BB
3080
3081 ASSERT(zplprops != NULL);
3082
87a275d9
AG
3083 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3084 return (SET_ERROR(EINVAL));
3085
34dc7c2f
BB
3086 /*
3087 * Pull out creator prop choices, if any.
3088 */
3089 if (createprops) {
b128c09f
BB
3090 (void) nvlist_lookup_uint64(createprops,
3091 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
34dc7c2f
BB
3092 (void) nvlist_lookup_uint64(createprops,
3093 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3094 (void) nvlist_remove_all(createprops,
3095 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3096 (void) nvlist_lookup_uint64(createprops,
3097 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3098 (void) nvlist_remove_all(createprops,
3099 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3100 (void) nvlist_lookup_uint64(createprops,
3101 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3102 (void) nvlist_remove_all(createprops,
3103 zfs_prop_to_name(ZFS_PROP_CASE));
3104 }
3105
3106 /*
b128c09f
BB
3107 * If the zpl version requested is whacky or the file system
3108 * or pool is version is too "young" to support normalization
3109 * and the creator tried to set a value for one of the props,
3110 * error out.
34dc7c2f 3111 */
b128c09f
BB
3112 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3113 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
428870ff 3114 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
b128c09f 3115 (zplver < ZPL_VERSION_NORMALIZATION &&
34dc7c2f 3116 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
b128c09f 3117 sense != ZFS_PROP_UNDEFINED)))
2e528b49 3118 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
3119
3120 /*
3121 * Put the version in the zplprops
3122 */
3123 VERIFY(nvlist_add_uint64(zplprops,
3124 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3125
b129c659
MM
3126 if (norm == ZFS_PROP_UNDEFINED &&
3127 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3128 return (error);
34dc7c2f
BB
3129 VERIFY(nvlist_add_uint64(zplprops,
3130 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3131
3132 /*
3133 * If we're normalizing, names must always be valid UTF-8 strings.
3134 */
3135 if (norm)
3136 u8 = 1;
b129c659
MM
3137 if (u8 == ZFS_PROP_UNDEFINED &&
3138 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3139 return (error);
34dc7c2f
BB
3140 VERIFY(nvlist_add_uint64(zplprops,
3141 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3142
b129c659
MM
3143 if (sense == ZFS_PROP_UNDEFINED &&
3144 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3145 return (error);
34dc7c2f
BB
3146 VERIFY(nvlist_add_uint64(zplprops,
3147 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3148
3149 if (is_ci)
3150 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3151
34dc7c2f
BB
3152 return (0);
3153}
3154
b128c09f
BB
3155static int
3156zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3157 nvlist_t *zplprops, boolean_t *is_ci)
3158{
428870ff 3159 boolean_t fuids_ok, sa_ok;
b128c09f
BB
3160 uint64_t zplver = ZPL_VERSION;
3161 objset_t *os = NULL;
eca7b760 3162 char parentname[ZFS_MAX_DATASET_NAME_LEN];
b128c09f 3163 char *cp;
428870ff
BB
3164 spa_t *spa;
3165 uint64_t spa_vers;
b128c09f
BB
3166 int error;
3167
3168 (void) strlcpy(parentname, dataset, sizeof (parentname));
3169 cp = strrchr(parentname, '/');
3170 ASSERT(cp != NULL);
3171 cp[0] = '\0';
3172
428870ff
BB
3173 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3174 return (error);
3175
3176 spa_vers = spa_version(spa);
3177 spa_close(spa, FTAG);
3178
3179 zplver = zfs_zpl_version_map(spa_vers);
3180 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3181 sa_ok = (zplver >= ZPL_VERSION_SA);
b128c09f
BB
3182
3183 /*
3184 * Open parent object set so we can inherit zplprop values.
3185 */
428870ff 3186 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
b128c09f
BB
3187 return (error);
3188
428870ff 3189 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
b128c09f 3190 zplprops, is_ci);
428870ff 3191 dmu_objset_rele(os, FTAG);
b128c09f
BB
3192 return (error);
3193}
3194
3195static int
3196zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3197 nvlist_t *zplprops, boolean_t *is_ci)
3198{
428870ff
BB
3199 boolean_t fuids_ok;
3200 boolean_t sa_ok;
b128c09f
BB
3201 uint64_t zplver = ZPL_VERSION;
3202 int error;
3203
428870ff
BB
3204 zplver = zfs_zpl_version_map(spa_vers);
3205 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3206 sa_ok = (zplver >= ZPL_VERSION_SA);
b128c09f 3207
428870ff
BB
3208 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3209 createprops, zplprops, is_ci);
b128c09f
BB
3210 return (error);
3211}
3212
34dc7c2f 3213/*
6f1ffb06
MA
3214 * innvl: {
3215 * "type" -> dmu_objset_type_t (int32)
3216 * (optional) "props" -> { prop -> value }
b5256303
TC
3217 * (optional) "hidden_args" -> { "wkeydata" -> value }
3218 * raw uint8_t array of encryption wrapping key data (32 bytes)
6f1ffb06 3219 * }
34dc7c2f 3220 *
6f1ffb06 3221 * outnvl: propname -> error code (int32)
34dc7c2f 3222 */
b83a0e2d
DB
3223
3224static const zfs_ioc_key_t zfs_keys_create[] = {
3225 {"type", DATA_TYPE_INT32, 0},
3226 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3227 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3228};
3229
34dc7c2f 3230static int
6f1ffb06 3231zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3232{
34dc7c2f 3233 int error = 0;
6f1ffb06 3234 zfs_creat_t zct = { 0 };
34dc7c2f 3235 nvlist_t *nvprops = NULL;
b5256303 3236 nvlist_t *hidden_args = NULL;
34dc7c2f 3237 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
6f1ffb06
MA
3238 dmu_objset_type_t type;
3239 boolean_t is_insensitive = B_FALSE;
b5256303 3240 dsl_crypto_params_t *dcp = NULL;
34dc7c2f 3241
b83a0e2d 3242 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
6f1ffb06 3243 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
b5256303 3244 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
34dc7c2f 3245
6f1ffb06 3246 switch (type) {
34dc7c2f
BB
3247 case DMU_OST_ZFS:
3248 cbfunc = zfs_create_cb;
3249 break;
3250
3251 case DMU_OST_ZVOL:
3252 cbfunc = zvol_create_cb;
3253 break;
3254
3255 default:
3256 cbfunc = NULL;
3257 break;
3258 }
6f1ffb06
MA
3259 if (strchr(fsname, '@') ||
3260 strchr(fsname, '%'))
2e528b49 3261 return (SET_ERROR(EINVAL));
34dc7c2f 3262
34dc7c2f
BB
3263 zct.zct_props = nvprops;
3264
6f1ffb06 3265 if (cbfunc == NULL)
2e528b49 3266 return (SET_ERROR(EINVAL));
34dc7c2f 3267
6f1ffb06
MA
3268 if (type == DMU_OST_ZVOL) {
3269 uint64_t volsize, volblocksize;
34dc7c2f 3270
6f1ffb06 3271 if (nvprops == NULL)
2e528b49 3272 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3273 if (nvlist_lookup_uint64(nvprops,
3274 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
2e528b49 3275 return (SET_ERROR(EINVAL));
34dc7c2f 3276
6f1ffb06
MA
3277 if ((error = nvlist_lookup_uint64(nvprops,
3278 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3279 &volblocksize)) != 0 && error != ENOENT)
2e528b49 3280 return (SET_ERROR(EINVAL));
34dc7c2f 3281
6f1ffb06
MA
3282 if (error != 0)
3283 volblocksize = zfs_prop_default_numeric(
3284 ZFS_PROP_VOLBLOCKSIZE);
34dc7c2f 3285
4cb7b9c5 3286 if ((error = zvol_check_volblocksize(fsname,
6f1ffb06
MA
3287 volblocksize)) != 0 ||
3288 (error = zvol_check_volsize(volsize,
3289 volblocksize)) != 0)
3290 return (error);
3291 } else if (type == DMU_OST_ZFS) {
3292 int error;
34dc7c2f 3293
6f1ffb06
MA
3294 /*
3295 * We have to have normalization and
3296 * case-folding flags correct when we do the
3297 * file system creation, so go figure them out
3298 * now.
3299 */
3300 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3301 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3302 error = zfs_fill_zplprops(fsname, nvprops,
3303 zct.zct_zplprops, &is_insensitive);
3304 if (error != 0) {
3305 nvlist_free(zct.zct_zplprops);
3306 return (error);
34dc7c2f 3307 }
34dc7c2f
BB
3308 }
3309
b5256303
TC
3310 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3311 hidden_args, &dcp);
3312 if (error != 0) {
3313 nvlist_free(zct.zct_zplprops);
3314 return (error);
3315 }
3316
6f1ffb06 3317 error = dmu_objset_create(fsname, type,
b5256303
TC
3318 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3319
6f1ffb06 3320 nvlist_free(zct.zct_zplprops);
b5256303 3321 dsl_crypto_params_free(dcp, !!error);
6f1ffb06 3322
34dc7c2f
BB
3323 /*
3324 * It would be nice to do this atomically.
3325 */
3326 if (error == 0) {
6f1ffb06
MA
3327 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3328 nvprops, outnvl);
f74b821a
BB
3329 if (error != 0) {
3330 spa_t *spa;
3331 int error2;
3332
3333 /*
3334 * Volumes will return EBUSY and cannot be destroyed
3335 * until all asynchronous minor handling has completed.
3336 * Wait for the spa_zvol_taskq to drain then retry.
3337 */
3338 error2 = dsl_destroy_head(fsname);
3339 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3340 error2 = spa_open(fsname, &spa, FTAG);
3341 if (error2 == 0) {
3342 taskq_wait(spa->spa_zvol_taskq);
3343 spa_close(spa, FTAG);
3344 }
3345 error2 = dsl_destroy_head(fsname);
3346 }
3347 }
34dc7c2f 3348 }
34dc7c2f
BB
3349 return (error);
3350}
3351
3352/*
6f1ffb06
MA
3353 * innvl: {
3354 * "origin" -> name of origin snapshot
3355 * (optional) "props" -> { prop -> value }
b5256303
TC
3356 * (optional) "hidden_args" -> { "wkeydata" -> value }
3357 * raw uint8_t array of encryption wrapping key data (32 bytes)
6f1ffb06 3358 * }
34dc7c2f 3359 *
428870ff 3360 * outputs:
6f1ffb06 3361 * outnvl: propname -> error code (int32)
34dc7c2f 3362 */
b83a0e2d
DB
3363static const zfs_ioc_key_t zfs_keys_clone[] = {
3364 {"origin", DATA_TYPE_STRING, 0},
3365 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3366 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3367};
3368
34dc7c2f 3369static int
6f1ffb06 3370zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3371{
6f1ffb06 3372 int error = 0;
b128c09f 3373 nvlist_t *nvprops = NULL;
6f1ffb06 3374 char *origin_name;
b128c09f 3375
b83a0e2d 3376 origin_name = fnvlist_lookup_string(innvl, "origin");
6f1ffb06 3377 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
b128c09f 3378
6f1ffb06
MA
3379 if (strchr(fsname, '@') ||
3380 strchr(fsname, '%'))
2e528b49 3381 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3382
3383 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
2e528b49 3384 return (SET_ERROR(EINVAL));
b5256303 3385
13fe0198 3386 error = dmu_objset_clone(fsname, origin_name);
b128c09f 3387
6f1ffb06
MA
3388 /*
3389 * It would be nice to do this atomically.
3390 */
3391 if (error == 0) {
3392 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3393 nvprops, outnvl);
3394 if (error != 0)
13fe0198 3395 (void) dsl_destroy_head(fsname);
b128c09f 3396 }
6f1ffb06
MA
3397 return (error);
3398}
9babb374 3399
b83a0e2d
DB
3400static const zfs_ioc_key_t zfs_keys_remap[] = {
3401 /* no nvl keys */
3402};
3403
a1d477c2
MA
3404/* ARGSUSED */
3405static int
3406zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3407{
3408 if (strchr(fsname, '@') ||
3409 strchr(fsname, '%'))
3410 return (SET_ERROR(EINVAL));
3411
3412 return (dmu_objset_remap_indirects(fsname));
3413}
3414
6f1ffb06
MA
3415/*
3416 * innvl: {
3417 * "snaps" -> { snapshot1, snapshot2 }
3418 * (optional) "props" -> { prop -> value (string) }
3419 * }
3420 *
3421 * outnvl: snapshot -> error code (int32)
6f1ffb06 3422 */
b83a0e2d
DB
3423static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3424 {"snaps", DATA_TYPE_NVLIST, 0},
3425 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3426};
3427
6f1ffb06
MA
3428static int
3429zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3430{
3431 nvlist_t *snaps;
3432 nvlist_t *props = NULL;
3433 int error, poollen;
1c27024e 3434 nvpair_t *pair;
9babb374 3435
6f1ffb06
MA
3436 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3437 if ((error = zfs_check_userprops(poolname, props)) != 0)
3438 return (error);
3439
3440 if (!nvlist_empty(props) &&
3441 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
2e528b49 3442 return (SET_ERROR(ENOTSUP));
6f1ffb06 3443
b83a0e2d 3444 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
6f1ffb06
MA
3445 poollen = strlen(poolname);
3446 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3447 pair = nvlist_next_nvpair(snaps, pair)) {
3448 const char *name = nvpair_name(pair);
3449 const char *cp = strchr(name, '@');
3450
3451 /*
3452 * The snap name must contain an @, and the part after it must
3453 * contain only valid characters.
3454 */
da536844
MA
3455 if (cp == NULL ||
3456 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
2e528b49 3457 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3458
3459 /*
3460 * The snap must be in the specified pool.
3461 */
3462 if (strncmp(name, poolname, poollen) != 0 ||
3463 (name[poollen] != '/' && name[poollen] != '@'))
2e528b49 3464 return (SET_ERROR(EXDEV));
6f1ffb06
MA
3465
3466 /* This must be the only snap of this fs. */
1c27024e 3467 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
6f1ffb06
MA
3468 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3469 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3470 == 0) {
2e528b49 3471 return (SET_ERROR(EXDEV));
6f1ffb06
MA
3472 }
3473 }
3474 }
3475
13fe0198 3476 error = dsl_dataset_snapshot(snaps, props, outnvl);
ba6a2402 3477
6f1ffb06
MA
3478 return (error);
3479}
3480
3481/*
3482 * innvl: "message" -> string
3483 */
b83a0e2d
DB
3484static const zfs_ioc_key_t zfs_keys_log_history[] = {
3485 {"message", DATA_TYPE_STRING, 0},
3486};
3487
6f1ffb06
MA
3488/* ARGSUSED */
3489static int
3490zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3491{
3492 char *message;
3493 spa_t *spa;
3494 int error;
3495 char *poolname;
3496
3497 /*
3498 * The poolname in the ioctl is not set, we get it from the TSD,
3499 * which was set at the end of the last successful ioctl that allows
3500 * logging. The secpolicy func already checked that it is set.
3501 * Only one log ioctl is allowed after each successful ioctl, so
3502 * we clear the TSD here.
3503 */
3504 poolname = tsd_get(zfs_allow_log_key);
9f3d1407 3505 if (poolname == NULL)
3506 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3507 (void) tsd_set(zfs_allow_log_key, NULL);
3508 error = spa_open(poolname, &spa, FTAG);
3509 strfree(poolname);
3510 if (error != 0)
3511 return (error);
3512
b83a0e2d 3513 message = fnvlist_lookup_string(innvl, "message");
6f1ffb06
MA
3514
3515 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3516 spa_close(spa, FTAG);
2e528b49 3517 return (SET_ERROR(ENOTSUP));
6f1ffb06
MA
3518 }
3519
3520 error = spa_history_log(spa, message);
3521 spa_close(spa, FTAG);
b128c09f 3522 return (error);
34dc7c2f
BB
3523}
3524
ebe7e575 3525/*
13fe0198
MA
3526 * The dp_config_rwlock must not be held when calling this, because the
3527 * unmount may need to write out data.
3528 *
3529 * This function is best-effort. Callers must deal gracefully if it
3530 * remains mounted (or is remounted after this call).
d09f25dc 3531 *
278bee93
BB
3532 * Returns 0 if the argument is not a snapshot, or it is not currently a
3533 * filesystem, or we were able to unmount it. Returns error code otherwise.
ebe7e575 3534 */
8d103d88 3535void
13fe0198 3536zfs_unmount_snap(const char *snapname)
34dc7c2f 3537{
278bee93 3538 if (strchr(snapname, '@') == NULL)
8d103d88 3539 return;
ebe7e575 3540
8d103d88 3541 (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
13fe0198
MA
3542}
3543
3544/* ARGSUSED */
3545static int
3546zfs_unmount_snap_cb(const char *snapname, void *arg)
3547{
8d103d88
SD
3548 zfs_unmount_snap(snapname);
3549 return (0);
13fe0198
MA
3550}
3551
3552/*
3553 * When a clone is destroyed, its origin may also need to be destroyed,
3554 * in which case it must be unmounted. This routine will do that unmount
3555 * if necessary.
3556 */
3557void
3558zfs_destroy_unmount_origin(const char *fsname)
3559{
3560 int error;
3561 objset_t *os;
3562 dsl_dataset_t *ds;
3563
3564 error = dmu_objset_hold(fsname, FTAG, &os);
3565 if (error != 0)
3566 return;
3567 ds = dmu_objset_ds(os);
3568 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
eca7b760 3569 char originname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
3570 dsl_dataset_name(ds->ds_prev, originname);
3571 dmu_objset_rele(os, FTAG);
8d103d88 3572 zfs_unmount_snap(originname);
13fe0198
MA
3573 } else {
3574 dmu_objset_rele(os, FTAG);
3575 }
34dc7c2f
BB
3576}
3577
3578/*
6f1ffb06
MA
3579 * innvl: {
3580 * "snaps" -> { snapshot1, snapshot2 }
3581 * (optional boolean) "defer"
3582 * }
34dc7c2f 3583 *
6f1ffb06 3584 * outnvl: snapshot -> error code (int32)
34dc7c2f 3585 */
b83a0e2d
DB
3586static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3587 {"snaps", DATA_TYPE_NVLIST, 0},
3588 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3589};
3590
da536844 3591/* ARGSUSED */
34dc7c2f 3592static int
6f1ffb06 3593zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3594{
6f1ffb06 3595 nvlist_t *snaps;
330d06f9 3596 nvpair_t *pair;
6f1ffb06 3597 boolean_t defer;
34dc7c2f 3598
b83a0e2d 3599 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
6f1ffb06 3600 defer = nvlist_exists(innvl, "defer");
330d06f9 3601
6f1ffb06
MA
3602 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3603 pair = nvlist_next_nvpair(snaps, pair)) {
8d103d88 3604 zfs_unmount_snap(nvpair_name(pair));
da536844
MA
3605 }
3606
3607 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3608}
3609
3610/*
3611 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3612 * All bookmarks must be in the same pool.
3613 *
3614 * innvl: {
3615 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3616 * }
3617 *
3618 * outnvl: bookmark -> error code (int32)
3619 *
3620 */
b83a0e2d
DB
3621static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3622 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3623};
3624
da536844
MA
3625/* ARGSUSED */
3626static int
3627zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3628{
1c27024e 3629 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
da536844
MA
3630 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3631 char *snap_name;
3632
3633 /*
3634 * Verify the snapshot argument.
3635 */
3636 if (nvpair_value_string(pair, &snap_name) != 0)
3637 return (SET_ERROR(EINVAL));
3638
3639
3640 /* Verify that the keys (bookmarks) are unique */
1c27024e 3641 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
da536844
MA
3642 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3643 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3644 return (SET_ERROR(EINVAL));
3645 }
3646 }
3647
3648 return (dsl_bookmark_create(innvl, outnvl));
3649}
3650
3651/*
3652 * innvl: {
3653 * property 1, property 2, ...
3654 * }
3655 *
3656 * outnvl: {
3657 * bookmark name 1 -> { property 1, property 2, ... },
3658 * bookmark name 2 -> { property 1, property 2, ... }
3659 * }
3660 *
3661 */
b83a0e2d
DB
3662static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3663 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3664};
3665
da536844
MA
3666static int
3667zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3668{
3669 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3670}
3671
3672/*
3673 * innvl: {
3674 * bookmark name 1, bookmark name 2
3675 * }
3676 *
3677 * outnvl: bookmark -> error code (int32)
3678 *
3679 */
b83a0e2d
DB
3680static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3681 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3682};
3683
da536844
MA
3684static int
3685zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3686 nvlist_t *outnvl)
3687{
3688 int error, poollen;
da536844
MA
3689
3690 poollen = strlen(poolname);
1c27024e 3691 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
da536844 3692 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
330d06f9 3693 const char *name = nvpair_name(pair);
da536844 3694 const char *cp = strchr(name, '#');
6f1ffb06 3695
330d06f9 3696 /*
da536844
MA
3697 * The bookmark name must contain an #, and the part after it
3698 * must contain only valid characters.
3699 */
3700 if (cp == NULL ||
3701 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3702 return (SET_ERROR(EINVAL));
3703
3704 /*
3705 * The bookmark must be in the specified pool.
330d06f9 3706 */
6f1ffb06 3707 if (strncmp(name, poolname, poollen) != 0 ||
da536844 3708 (name[poollen] != '/' && name[poollen] != '#'))
2e528b49 3709 return (SET_ERROR(EXDEV));
330d06f9
MA
3710 }
3711
da536844
MA
3712 error = dsl_bookmark_destroy(innvl, outnvl);
3713 return (error);
34dc7c2f
BB
3714}
3715
b83a0e2d
DB
3716static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3717 {"program", DATA_TYPE_STRING, 0},
3718 {"arg", DATA_TYPE_ANY, 0},
3719 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3720 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3721 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3722};
3723
d99a0153
CW
3724static int
3725zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3726 nvlist_t *outnvl)
3727{
3728 char *program;
3729 uint64_t instrlimit, memlimit;
5b72a38d 3730 boolean_t sync_flag;
d99a0153
CW
3731 nvpair_t *nvarg = NULL;
3732
b83a0e2d 3733 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
5b72a38d
SD
3734 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3735 sync_flag = B_TRUE;
3736 }
d99a0153
CW
3737 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3738 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3739 }
3740 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3741 memlimit = ZCP_DEFAULT_MEMLIMIT;
3742 }
b83a0e2d 3743 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
d99a0153
CW
3744
3745 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3746 return (EINVAL);
234c91c5 3747 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
d99a0153
CW
3748 return (EINVAL);
3749
5b72a38d 3750 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
d99a0153
CW
3751 nvarg, outnvl));
3752}
3753
d2734cce
SD
3754/*
3755 * innvl: unused
3756 * outnvl: empty
3757 */
b83a0e2d
DB
3758static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3759 /* no nvl keys */
3760};
3761
d2734cce
SD
3762/* ARGSUSED */
3763static int
3764zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3765{
3766 return (spa_checkpoint(poolname));
3767}
3768
3769/*
3770 * innvl: unused
3771 * outnvl: empty
3772 */
b83a0e2d
DB
3773static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3774 /* no nvl keys */
3775};
3776
d2734cce
SD
3777/* ARGSUSED */
3778static int
3779zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3780 nvlist_t *outnvl)
3781{
3782 return (spa_checkpoint_discard(poolname));
3783}
3784
34dc7c2f
BB
3785/*
3786 * inputs:
3787 * zc_name name of dataset to destroy
3788 * zc_objset_type type of objset
45d1cae3 3789 * zc_defer_destroy mark for deferred destroy
34dc7c2f
BB
3790 *
3791 * outputs: none
3792 */
3793static int
3794zfs_ioc_destroy(zfs_cmd_t *zc)
3795{
428870ff 3796 int err;
d09f25dc 3797
8d103d88
SD
3798 if (zc->zc_objset_type == DMU_OST_ZFS)
3799 zfs_unmount_snap(zc->zc_name);
34dc7c2f 3800
1b87e0f5 3801 if (strchr(zc->zc_name, '@')) {
13fe0198 3802 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
1b87e0f5 3803 } else {
13fe0198 3804 err = dsl_destroy_head(zc->zc_name);
1b87e0f5
RS
3805 if (err == EEXIST) {
3806 /*
3807 * It is possible that the given DS may have
3808 * hidden child (%recv) datasets - "leftovers"
3809 * resulting from the previously interrupted
3810 * 'zfs receive'.
3811 *
3812 * 6 extra bytes for /%recv
3813 */
3814 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3815
682ce104
TH
3816 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3817 zc->zc_name, recv_clone_name) >=
3818 sizeof (namebuf))
3819 return (SET_ERROR(EINVAL));
1b87e0f5
RS
3820
3821 /*
3822 * Try to remove the hidden child (%recv) and after
3823 * that try to remove the target dataset.
3824 * If the hidden child (%recv) does not exist
3825 * the original error (EEXIST) will be returned
3826 */
3827 err = dsl_destroy_head(namebuf);
3828 if (err == 0)
3829 err = dsl_destroy_head(zc->zc_name);
3830 else if (err == ENOENT)
ecb2b7dc 3831 err = SET_ERROR(EEXIST);
1b87e0f5
RS
3832 }
3833 }
a0bd735a 3834
428870ff 3835 return (err);
34dc7c2f
BB
3836}
3837
3838/*
46ba1e59 3839 * fsname is name of dataset to rollback (to most recent snapshot)
34dc7c2f 3840 *
8ca78ab0 3841 * innvl may contain name of expected target snapshot
46ba1e59
MA
3842 *
3843 * outnvl: "target" -> name of most recent snapshot
3844 * }
34dc7c2f 3845 */
b83a0e2d
DB
3846static const zfs_ioc_key_t zfs_keys_rollback[] = {
3847 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
3848};
3849
46ba1e59 3850/* ARGSUSED */
34dc7c2f 3851static int
8ca78ab0 3852zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3853{
0037b49e 3854 zfsvfs_t *zfsvfs;
040dab99 3855 zvol_state_t *zv;
8ca78ab0 3856 char *target = NULL;
13fe0198 3857 int error;
34dc7c2f 3858
8ca78ab0
AG
3859 (void) nvlist_lookup_string(innvl, "target", &target);
3860 if (target != NULL) {
13342832 3861 const char *cp = strchr(target, '@');
8ca78ab0 3862
13342832
AG
3863 /*
3864 * The snap name must contain an @, and the part after it must
3865 * contain only valid characters.
3866 */
3867 if (cp == NULL ||
3868 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
8ca78ab0
AG
3869 return (SET_ERROR(EINVAL));
3870 }
3871
f298b24d 3872 if (getzfsvfs(fsname, &zfsvfs) == 0) {
ec923db2
GM
3873 dsl_dataset_t *ds;
3874
0037b49e
BB
3875 ds = dmu_objset_ds(zfsvfs->z_os);
3876 error = zfs_suspend_fs(zfsvfs);
34dc7c2f
BB
3877 if (error == 0) {
3878 int resume_err;
3879
8ca78ab0
AG
3880 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3881 outnvl);
0037b49e 3882 resume_err = zfs_resume_fs(zfsvfs, ds);
34dc7c2f 3883 error = error ? error : resume_err;
34dc7c2f 3884 }
0037b49e 3885 deactivate_super(zfsvfs->z_sb);
040dab99 3886 } else if ((zv = zvol_suspend(fsname)) != NULL) {
8ca78ab0
AG
3887 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
3888 outnvl);
040dab99 3889 zvol_resume(zv);
34dc7c2f 3890 } else {
8ca78ab0 3891 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
34dc7c2f 3892 }
13fe0198
MA
3893 return (error);
3894}
34dc7c2f 3895
13fe0198
MA
3896static int
3897recursive_unmount(const char *fsname, void *arg)
3898{
3899 const char *snapname = arg;
3900 char *fullname;
428870ff 3901
13fe0198 3902 fullname = kmem_asprintf("%s@%s", fsname, snapname);
8d103d88 3903 zfs_unmount_snap(fullname);
13fe0198 3904 strfree(fullname);
00fcdee1 3905
8d103d88 3906 return (0);
34dc7c2f
BB
3907}
3908
3909/*
3910 * inputs:
3911 * zc_name old name of dataset
3912 * zc_value new name of dataset
3913 * zc_cookie recursive flag (only valid for snapshots)
3914 *
3915 * outputs: none
3916 */
3917static int
3918zfs_ioc_rename(zfs_cmd_t *zc)
3919{
3920 boolean_t recursive = zc->zc_cookie & 1;
13fe0198 3921 char *at;
34dc7c2f 3922
650258d7 3923 /* "zfs rename" from and to ...%recv datasets should both fail */
3924 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
34dc7c2f 3925 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
650258d7 3926 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3927 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3928 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
2e528b49 3929 return (SET_ERROR(EINVAL));
34dc7c2f 3930
13fe0198
MA
3931 at = strchr(zc->zc_name, '@');
3932 if (at != NULL) {
3933 /* snaps must be in same fs */
9554185d
SH
3934 int error;
3935
13fe0198 3936 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
2e528b49 3937 return (SET_ERROR(EXDEV));
13fe0198
MA
3938 *at = '\0';
3939 if (zc->zc_objset_type == DMU_OST_ZFS) {
9554185d 3940 error = dmu_objset_find(zc->zc_name,
13fe0198
MA
3941 recursive_unmount, at + 1,
3942 recursive ? DS_FIND_CHILDREN : 0);
9554185d
SH
3943 if (error != 0) {
3944 *at = '@';
13fe0198 3945 return (error);
9554185d 3946 }
13fe0198 3947 }
9554185d
SH
3948 error = dsl_dataset_rename_snapshot(zc->zc_name,
3949 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3950 *at = '@';
3951
3952 return (error);
13fe0198 3953 } else {
ba6a2402 3954 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
95c73795 3955 }
34dc7c2f
BB
3956}
3957
428870ff
BB
3958static int
3959zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3960{
3961 const char *propname = nvpair_name(pair);
3962 boolean_t issnap = (strchr(dsname, '@') != NULL);
3963 zfs_prop_t prop = zfs_name_to_prop(propname);
3964 uint64_t intval;
3965 int err;
3966
3967 if (prop == ZPROP_INVAL) {
3968 if (zfs_prop_user(propname)) {
c65aa5b2
BB
3969 if ((err = zfs_secpolicy_write_perms(dsname,
3970 ZFS_DELEG_PERM_USERPROP, cr)))
428870ff
BB
3971 return (err);
3972 return (0);
3973 }
3974
3975 if (!issnap && zfs_prop_userquota(propname)) {
3976 const char *perm = NULL;
3977 const char *uq_prefix =
3978 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3979 const char *gq_prefix =
3980 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
1de321e6
JX
3981 const char *uiq_prefix =
3982 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
3983 const char *giq_prefix =
3984 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
9c5167d1
NF
3985 const char *pq_prefix =
3986 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
3987 const char *piq_prefix = zfs_userquota_prop_prefixes[\
3988 ZFS_PROP_PROJECTOBJQUOTA];
428870ff
BB
3989
3990 if (strncmp(propname, uq_prefix,
3991 strlen(uq_prefix)) == 0) {
3992 perm = ZFS_DELEG_PERM_USERQUOTA;
1de321e6
JX
3993 } else if (strncmp(propname, uiq_prefix,
3994 strlen(uiq_prefix)) == 0) {
3995 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
428870ff
BB
3996 } else if (strncmp(propname, gq_prefix,
3997 strlen(gq_prefix)) == 0) {
3998 perm = ZFS_DELEG_PERM_GROUPQUOTA;
1de321e6
JX
3999 } else if (strncmp(propname, giq_prefix,
4000 strlen(giq_prefix)) == 0) {
4001 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
9c5167d1
NF
4002 } else if (strncmp(propname, pq_prefix,
4003 strlen(pq_prefix)) == 0) {
4004 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4005 } else if (strncmp(propname, piq_prefix,
4006 strlen(piq_prefix)) == 0) {
4007 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
428870ff 4008 } else {
9c5167d1 4009 /* {USER|GROUP|PROJECT}USED are read-only */
2e528b49 4010 return (SET_ERROR(EINVAL));
428870ff
BB
4011 }
4012
c65aa5b2 4013 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
428870ff
BB
4014 return (err);
4015 return (0);
4016 }
4017
2e528b49 4018 return (SET_ERROR(EINVAL));
428870ff
BB
4019 }
4020
4021 if (issnap)
2e528b49 4022 return (SET_ERROR(EINVAL));
428870ff
BB
4023
4024 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4025 /*
4026 * dsl_prop_get_all_impl() returns properties in this
4027 * format.
4028 */
4029 nvlist_t *attrs;
4030 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4031 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4032 &pair) == 0);
4033 }
4034
4035 /*
4036 * Check that this value is valid for this pool version
4037 */
4038 switch (prop) {
4039 case ZFS_PROP_COMPRESSION:
4040 /*
4041 * If the user specified gzip compression, make sure
4042 * the SPA supports it. We ignore any errors here since
4043 * we'll catch them later.
4044 */
f1512ee6 4045 if (nvpair_value_uint64(pair, &intval) == 0) {
428870ff
BB
4046 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4047 intval <= ZIO_COMPRESS_GZIP_9 &&
4048 zfs_earlier_version(dsname,
4049 SPA_VERSION_GZIP_COMPRESSION)) {
2e528b49 4050 return (SET_ERROR(ENOTSUP));
428870ff
BB
4051 }
4052
4053 if (intval == ZIO_COMPRESS_ZLE &&
4054 zfs_earlier_version(dsname,
4055 SPA_VERSION_ZLE_COMPRESSION))
2e528b49 4056 return (SET_ERROR(ENOTSUP));
428870ff 4057
9759c60f 4058 if (intval == ZIO_COMPRESS_LZ4) {
9759c60f
ED
4059 spa_t *spa;
4060
4061 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4062 return (err);
4063
fa86b5db
MA
4064 if (!spa_feature_is_enabled(spa,
4065 SPA_FEATURE_LZ4_COMPRESS)) {
9759c60f 4066 spa_close(spa, FTAG);
2e528b49 4067 return (SET_ERROR(ENOTSUP));
9759c60f
ED
4068 }
4069 spa_close(spa, FTAG);
4070 }
4071
428870ff
BB
4072 /*
4073 * If this is a bootable dataset then
4074 * verify that the compression algorithm
4075 * is supported for booting. We must return
4076 * something other than ENOTSUP since it
4077 * implies a downrev pool version.
4078 */
4079 if (zfs_is_bootfs(dsname) &&
4080 !BOOTFS_COMPRESS_VALID(intval)) {
2e528b49 4081 return (SET_ERROR(ERANGE));
428870ff
BB
4082 }
4083 }
4084 break;
4085
4086 case ZFS_PROP_COPIES:
4087 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
2e528b49 4088 return (SET_ERROR(ENOTSUP));
428870ff
BB
4089 break;
4090
4cb7b9c5 4091 case ZFS_PROP_VOLBLOCKSIZE:
f1512ee6
MA
4092 case ZFS_PROP_RECORDSIZE:
4093 /* Record sizes above 128k need the feature to be enabled */
4094 if (nvpair_value_uint64(pair, &intval) == 0 &&
4095 intval > SPA_OLD_MAXBLOCKSIZE) {
4096 spa_t *spa;
4097
f1512ee6
MA
4098 /*
4099 * We don't allow setting the property above 1MB,
4100 * unless the tunable has been changed.
4101 */
4102 if (intval > zfs_max_recordsize ||
4103 intval > SPA_MAXBLOCKSIZE)
4b2a3e0c 4104 return (SET_ERROR(ERANGE));
f1512ee6
MA
4105
4106 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4107 return (err);
4108
4109 if (!spa_feature_is_enabled(spa,
4110 SPA_FEATURE_LARGE_BLOCKS)) {
4111 spa_close(spa, FTAG);
4112 return (SET_ERROR(ENOTSUP));
4113 }
4114 spa_close(spa, FTAG);
4115 }
4116 break;
4117
50c957f7
NB
4118 case ZFS_PROP_DNODESIZE:
4119 /* Dnode sizes above 512 need the feature to be enabled */
4120 if (nvpair_value_uint64(pair, &intval) == 0 &&
4121 intval != ZFS_DNSIZE_LEGACY) {
4122 spa_t *spa;
4123
4124 /*
4125 * If this is a bootable dataset then
4126 * we don't allow large (>512B) dnodes,
4127 * because GRUB doesn't support them.
4128 */
4129 if (zfs_is_bootfs(dsname) &&
02730c33 4130 intval != ZFS_DNSIZE_LEGACY) {
50c957f7
NB
4131 return (SET_ERROR(EDOM));
4132 }
4133
4134 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4135 return (err);
4136
4137 if (!spa_feature_is_enabled(spa,
4138 SPA_FEATURE_LARGE_DNODE)) {
4139 spa_close(spa, FTAG);
4140 return (SET_ERROR(ENOTSUP));
4141 }
4142 spa_close(spa, FTAG);
4143 }
4144 break;
4145
cc99f275
DB
4146 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4147 /*
4148 * This property could require the allocation classes
4149 * feature to be active for setting, however we allow
4150 * it so that tests of settable properties succeed.
4151 * The CLI will issue a warning in this case.
4152 */
4153 break;
4154
428870ff
BB
4155 case ZFS_PROP_SHARESMB:
4156 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
2e528b49 4157 return (SET_ERROR(ENOTSUP));
428870ff
BB
4158 break;
4159
4160 case ZFS_PROP_ACLINHERIT:
4161 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4162 nvpair_value_uint64(pair, &intval) == 0) {
4163 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4164 zfs_earlier_version(dsname,
4165 SPA_VERSION_PASSTHROUGH_X))
2e528b49 4166 return (SET_ERROR(ENOTSUP));
428870ff
BB
4167 }
4168 break;
3c67d83a
TH
4169 case ZFS_PROP_CHECKSUM:
4170 case ZFS_PROP_DEDUP:
4171 {
4172 spa_feature_t feature;
4173 spa_t *spa;
3c67d83a
TH
4174 int err;
4175
4176 /* dedup feature version checks */
4177 if (prop == ZFS_PROP_DEDUP &&
4178 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4179 return (SET_ERROR(ENOTSUP));
4180
c8c30836 4181 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4182 nvpair_value_uint64(pair, &intval) == 0) {
4183 /* check prop value is enabled in features */
4184 feature = zio_checksum_to_feature(
4185 intval & ZIO_CHECKSUM_MASK);
4186 if (feature == SPA_FEATURE_NONE)
4187 break;
3c67d83a 4188
c8c30836 4189 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4190 return (err);
5fadb7fb 4191
c8c30836 4192 if (!spa_feature_is_enabled(spa, feature)) {
4193 spa_close(spa, FTAG);
4194 return (SET_ERROR(ENOTSUP));
4195 }
3c67d83a 4196 spa_close(spa, FTAG);
3c67d83a 4197 }
3c67d83a
TH
4198 break;
4199 }
4200
e75c13c3
BB
4201 default:
4202 break;
428870ff
BB
4203 }
4204
4205 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4206}
4207
4208/*
4209 * Removes properties from the given props list that fail permission checks
4210 * needed to clear them and to restore them in case of a receive error. For each
4211 * property, make sure we have both set and inherit permissions.
4212 *
4213 * Returns the first error encountered if any permission checks fail. If the
4214 * caller provides a non-NULL errlist, it also gives the complete list of names
4215 * of all the properties that failed a permission check along with the
4216 * corresponding error numbers. The caller is responsible for freeing the
4217 * returned errlist.
4218 *
4219 * If every property checks out successfully, zero is returned and the list
4220 * pointed at by errlist is NULL.
4221 */
4222static int
4223zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
b128c09f
BB
4224{
4225 zfs_cmd_t *zc;
428870ff
BB
4226 nvpair_t *pair, *next_pair;
4227 nvlist_t *errors;
4228 int err, rv = 0;
b128c09f
BB
4229
4230 if (props == NULL)
428870ff
BB
4231 return (0);
4232
4233 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4234
efcd79a8 4235 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
680eada9 4236 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
428870ff
BB
4237 pair = nvlist_next_nvpair(props, NULL);
4238 while (pair != NULL) {
4239 next_pair = nvlist_next_nvpair(props, pair);
4240
680eada9 4241 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4242 sizeof (zc->zc_value));
428870ff 4243 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
6f1ffb06 4244 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
428870ff
BB
4245 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4246 VERIFY(nvlist_add_int32(errors,
4247 zc->zc_value, err) == 0);
4248 }
4249 pair = next_pair;
b128c09f
BB
4250 }
4251 kmem_free(zc, sizeof (zfs_cmd_t));
428870ff
BB
4252
4253 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4254 nvlist_free(errors);
4255 errors = NULL;
4256 } else {
4257 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4258 }
4259
4260 if (errlist == NULL)
4261 nvlist_free(errors);
4262 else
4263 *errlist = errors;
4264
4265 return (rv);
4266}
4267
4268static boolean_t
4269propval_equals(nvpair_t *p1, nvpair_t *p2)
4270{
4271 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4272 /* dsl_prop_get_all_impl() format */
4273 nvlist_t *attrs;
4274 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4275 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4276 &p1) == 0);
4277 }
4278
4279 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4280 nvlist_t *attrs;
4281 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4282 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4283 &p2) == 0);
4284 }
4285
4286 if (nvpair_type(p1) != nvpair_type(p2))
4287 return (B_FALSE);
4288
4289 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4290 char *valstr1, *valstr2;
4291
4292 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4293 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4294 return (strcmp(valstr1, valstr2) == 0);
4295 } else {
4296 uint64_t intval1, intval2;
4297
4298 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4299 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4300 return (intval1 == intval2);
4301 }
b128c09f
BB
4302}
4303
428870ff
BB
4304/*
4305 * Remove properties from props if they are not going to change (as determined
4306 * by comparison with origprops). Remove them from origprops as well, since we
4307 * do not need to clear or restore properties that won't change.
4308 */
4309static void
4310props_reduce(nvlist_t *props, nvlist_t *origprops)
4311{
4312 nvpair_t *pair, *next_pair;
4313
4314 if (origprops == NULL)
4315 return; /* all props need to be received */
4316
4317 pair = nvlist_next_nvpair(props, NULL);
4318 while (pair != NULL) {
4319 const char *propname = nvpair_name(pair);
4320 nvpair_t *match;
4321
4322 next_pair = nvlist_next_nvpair(props, pair);
4323
4324 if ((nvlist_lookup_nvpair(origprops, propname,
4325 &match) != 0) || !propval_equals(pair, match))
4326 goto next; /* need to set received value */
4327
4328 /* don't clear the existing received value */
4329 (void) nvlist_remove_nvpair(origprops, match);
4330 /* don't bother receiving the property */
4331 (void) nvlist_remove_nvpair(props, pair);
4332next:
4333 pair = next_pair;
4334 }
4335}
4336
671c9354
DM
4337/*
4338 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4339 * For example, refquota cannot be set until after the receipt of a dataset,
4340 * because in replication streams, an older/earlier snapshot may exceed the
4341 * refquota. We want to receive the older/earlier snapshot, but setting
4342 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4343 * the older/earlier snapshot from being received (with EDQUOT).
4344 *
4345 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4346 *
4347 * libzfs will need to be judicious handling errors encountered by props
4348 * extracted by this function.
4349 */
4350static nvlist_t *
4351extract_delay_props(nvlist_t *props)
4352{
4353 nvlist_t *delayprops;
4354 nvpair_t *nvp, *tmp;
b5256303
TC
4355 static const zfs_prop_t delayable[] = {
4356 ZFS_PROP_REFQUOTA,
4357 ZFS_PROP_KEYLOCATION,
4358 0
4359 };
671c9354
DM
4360 int i;
4361
4362 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4363
4364 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4365 nvp = nvlist_next_nvpair(props, nvp)) {
4366 /*
4367 * strcmp() is safe because zfs_prop_to_name() always returns
4368 * a bounded string.
4369 */
4370 for (i = 0; delayable[i] != 0; i++) {
4371 if (strcmp(zfs_prop_to_name(delayable[i]),
4372 nvpair_name(nvp)) == 0) {
4373 break;
4374 }
4375 }
4376 if (delayable[i] != 0) {
4377 tmp = nvlist_prev_nvpair(props, nvp);
4378 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4379 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4380 nvp = tmp;
4381 }
4382 }
4383
4384 if (nvlist_empty(delayprops)) {
4385 nvlist_free(delayprops);
4386 delayprops = NULL;
4387 }
4388 return (delayprops);
4389}
4390
428870ff
BB
4391#ifdef DEBUG
4392static boolean_t zfs_ioc_recv_inject_err;
4393#endif
4394
34dc7c2f 4395/*
1bf3bf0e
GN
4396 * nvlist 'errors' is always allocated. It will contain descriptions of
4397 * encountered errors, if any. It's the callers responsibility to free.
34dc7c2f
BB
4398 */
4399static int
a3eeab2d 4400zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
d9c460a0
TC
4401 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4402 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4403 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4404 uint64_t *action_handle, nvlist_t **errors)
34dc7c2f 4405{
34dc7c2f 4406 dmu_recv_cookie_t drc;
428870ff
BB
4407 int error = 0;
4408 int props_error = 0;
34dc7c2f 4409 offset_t off;
d9c460a0
TC
4410 nvlist_t *local_delayprops = NULL;
4411 nvlist_t *recv_delayprops = NULL;
43e52edd 4412 nvlist_t *origprops = NULL; /* existing properties */
a3eeab2d 4413 nvlist_t *origrecvd = NULL; /* existing received properties */
428870ff 4414 boolean_t first_recvd_props = B_FALSE;
43e52edd 4415 file_t *input_fp;
34dc7c2f 4416
1bf3bf0e
GN
4417 *read_bytes = 0;
4418 *errflags = 0;
4419 *errors = fnvlist_alloc();
4420
43e52edd
BB
4421 input_fp = getf(input_fd);
4422 if (input_fp == NULL)
2e528b49 4423 return (SET_ERROR(EBADF));
13fe0198 4424
d9c460a0
TC
4425 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4426 resumable, localprops, hidden_args, origin, &drc);
13fe0198
MA
4427 if (error != 0)
4428 goto out;
4429
4430 /*
4431 * Set properties before we receive the stream so that they are applied
4432 * to the new data. Note that we must call dmu_recv_stream() if
4433 * dmu_recv_begin() succeeds.
4434 */
a3eeab2d 4435 if (recvprops != NULL && !drc.drc_newfs) {
13fe0198
MA
4436 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4437 SPA_VERSION_RECVD_PROPS &&
4438 !dsl_prop_get_hasrecvd(tofs))
428870ff 4439 first_recvd_props = B_TRUE;
428870ff 4440
b128c09f 4441 /*
428870ff 4442 * If new received properties are supplied, they are to
d9c460a0
TC
4443 * completely replace the existing received properties,
4444 * so stash away the existing ones.
b128c09f 4445 */
a3eeab2d 4446 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
428870ff
BB
4447 nvlist_t *errlist = NULL;
4448 /*
4449 * Don't bother writing a property if its value won't
4450 * change (and avoid the unnecessary security checks).
4451 *
4452 * The first receive after SPA_VERSION_RECVD_PROPS is a
4453 * special case where we blow away all local properties
4454 * regardless.
4455 */
4456 if (!first_recvd_props)
a3eeab2d 4457 props_reduce(recvprops, origrecvd);
4458 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
43e52edd 4459 (void) nvlist_merge(*errors, errlist, 0);
428870ff 4460 nvlist_free(errlist);
b128c09f 4461
a3eeab2d 4462 if (clear_received_props(tofs, origrecvd,
4463 first_recvd_props ? NULL : recvprops) != 0)
4464 *errflags |= ZPROP_ERR_NOCLEAR;
4465 } else {
4466 *errflags |= ZPROP_ERR_NOCLEAR;
4467 }
4468 }
4469
4470 /*
4471 * Stash away existing properties so we can restore them on error unless
4472 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4473 * case "origrecvd" will take care of that.
4474 */
4475 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4476 objset_t *os;
4477 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4478 if (dsl_prop_get_all(os, &origprops) != 0) {
43e52edd 4479 *errflags |= ZPROP_ERR_NOCLEAR;
a3eeab2d 4480 }
4481 dmu_objset_rele(os, FTAG);
13fe0198 4482 } else {
43e52edd 4483 *errflags |= ZPROP_ERR_NOCLEAR;
428870ff 4484 }
13fe0198
MA
4485 }
4486
a3eeab2d 4487 if (recvprops != NULL) {
13fe0198 4488 props_error = dsl_prop_set_hasrecvd(tofs);
428870ff 4489
13fe0198 4490 if (props_error == 0) {
d9c460a0 4491 recv_delayprops = extract_delay_props(recvprops);
13fe0198 4492 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
a3eeab2d 4493 recvprops, *errors);
13fe0198 4494 }
428870ff
BB
4495 }
4496
a3eeab2d 4497 if (localprops != NULL) {
4498 nvlist_t *oprops = fnvlist_alloc();
4499 nvlist_t *xprops = fnvlist_alloc();
4500 nvpair_t *nvp = NULL;
4501
4502 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4503 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4504 /* -x property */
4505 const char *name = nvpair_name(nvp);
4506 zfs_prop_t prop = zfs_name_to_prop(name);
4507 if (prop != ZPROP_INVAL) {
4508 if (!zfs_prop_inheritable(prop))
4509 continue;
4510 } else if (!zfs_prop_user(name))
4511 continue;
4512 fnvlist_add_boolean(xprops, name);
4513 } else {
4514 /* -o property=value */
4515 fnvlist_add_nvpair(oprops, nvp);
4516 }
4517 }
d9c460a0
TC
4518
4519 local_delayprops = extract_delay_props(oprops);
a3eeab2d 4520 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4521 oprops, *errors);
4522 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4523 xprops, *errors);
4524
4525 nvlist_free(oprops);
4526 nvlist_free(xprops);
4527 }
4528
43e52edd
BB
4529 off = input_fp->f_offset;
4530 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4531 action_handle);
34dc7c2f 4532
45d1cae3 4533 if (error == 0) {
0037b49e 4534 zfsvfs_t *zfsvfs = NULL;
040dab99 4535 zvol_state_t *zv = NULL;
b128c09f 4536
f298b24d 4537 if (getzfsvfs(tofs, &zfsvfs) == 0) {
45d1cae3 4538 /* online recv */
ec923db2 4539 dsl_dataset_t *ds;
45d1cae3 4540 int end_err;
b128c09f 4541
0037b49e
BB
4542 ds = dmu_objset_ds(zfsvfs->z_os);
4543 error = zfs_suspend_fs(zfsvfs);
45d1cae3
BB
4544 /*
4545 * If the suspend fails, then the recv_end will
4546 * likely also fail, and clean up after itself.
4547 */
0037b49e 4548 end_err = dmu_recv_end(&drc, zfsvfs);
428870ff 4549 if (error == 0)
0037b49e 4550 error = zfs_resume_fs(zfsvfs, ds);
45d1cae3 4551 error = error ? error : end_err;
0037b49e 4552 deactivate_super(zfsvfs->z_sb);
040dab99
CC
4553 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4554 error = dmu_recv_end(&drc, zvol_tag(zv));
4555 zvol_resume(zv);
b128c09f 4556 } else {
831baf06 4557 error = dmu_recv_end(&drc, NULL);
34dc7c2f 4558 }
671c9354
DM
4559
4560 /* Set delayed properties now, after we're done receiving. */
d9c460a0 4561 if (recv_delayprops != NULL && error == 0) {
671c9354 4562 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
d9c460a0
TC
4563 recv_delayprops, *errors);
4564 }
4565 if (local_delayprops != NULL && error == 0) {
4566 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4567 local_delayprops, *errors);
671c9354
DM
4568 }
4569 }
4570
d9c460a0
TC
4571 /*
4572 * Merge delayed props back in with initial props, in case
4573 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4574 * we have to make sure clear_received_props() includes
4575 * the delayed properties).
4576 *
4577 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4578 * using ASSERT() will be just like a VERIFY.
4579 */
4580 if (recv_delayprops != NULL) {
4581 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4582 nvlist_free(recv_delayprops);
4583 }
4584 if (local_delayprops != NULL) {
4585 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4586 nvlist_free(local_delayprops);
671c9354 4587 }
34dc7c2f 4588
43e52edd
BB
4589 *read_bytes = off - input_fp->f_offset;
4590 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
02730c33 4591 input_fp->f_offset = off;
34dc7c2f 4592
428870ff
BB
4593#ifdef DEBUG
4594 if (zfs_ioc_recv_inject_err) {
4595 zfs_ioc_recv_inject_err = B_FALSE;
4596 error = 1;
4597 }
4598#endif
ba6a2402 4599
b128c09f
BB
4600 /*
4601 * On error, restore the original props.
4602 */
a3eeab2d 4603 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4604 if (clear_received_props(tofs, recvprops, NULL) != 0) {
13fe0198
MA
4605 /*
4606 * We failed to clear the received properties.
4607 * Since we may have left a $recvd value on the
4608 * system, we can't clear the $hasrecvd flag.
4609 */
43e52edd 4610 *errflags |= ZPROP_ERR_NORESTORE;
13fe0198
MA
4611 } else if (first_recvd_props) {
4612 dsl_prop_unset_hasrecvd(tofs);
428870ff
BB
4613 }
4614
a3eeab2d 4615 if (origrecvd == NULL && !drc.drc_newfs) {
428870ff 4616 /* We failed to stash the original properties. */
43e52edd 4617 *errflags |= ZPROP_ERR_NORESTORE;
428870ff
BB
4618 }
4619
4620 /*
4621 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4622 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4e33ba4c 4623 * explicitly if we're restoring local properties cleared in the
428870ff
BB
4624 * first new-style receive.
4625 */
a3eeab2d 4626 if (origrecvd != NULL &&
428870ff
BB
4627 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4628 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
a3eeab2d 4629 origrecvd, NULL) != 0) {
428870ff
BB
4630 /*
4631 * We stashed the original properties but failed to
4632 * restore them.
4633 */
43e52edd 4634 *errflags |= ZPROP_ERR_NORESTORE;
428870ff 4635 }
b128c09f 4636 }
a3eeab2d 4637 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4638 !first_recvd_props) {
4639 nvlist_t *setprops;
4640 nvlist_t *inheritprops;
4641 nvpair_t *nvp;
4642
4643 if (origprops == NULL) {
4644 /* We failed to stash the original properties. */
4645 *errflags |= ZPROP_ERR_NORESTORE;
4646 goto out;
4647 }
4648
4649 /* Restore original props */
4650 setprops = fnvlist_alloc();
4651 inheritprops = fnvlist_alloc();
4652 nvp = NULL;
4653 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4654 const char *name = nvpair_name(nvp);
4655 const char *source;
4656 nvlist_t *attrs;
4657
4658 if (!nvlist_exists(origprops, name)) {
4659 /*
4660 * Property was not present or was explicitly
4661 * inherited before the receive, restore this.
4662 */
4663 fnvlist_add_boolean(inheritprops, name);
4664 continue;
4665 }
4666 attrs = fnvlist_lookup_nvlist(origprops, name);
4667 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4668
4669 /* Skip received properties */
4670 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4671 continue;
4672
4673 if (strcmp(source, tofs) == 0) {
4674 /* Property was locally set */
4675 fnvlist_add_nvlist(setprops, name, attrs);
4676 } else {
4677 /* Property was implicitly inherited */
4678 fnvlist_add_boolean(inheritprops, name);
4679 }
4680 }
4681
4682 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4683 NULL) != 0)
4684 *errflags |= ZPROP_ERR_NORESTORE;
4685 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4686 NULL) != 0)
4687 *errflags |= ZPROP_ERR_NORESTORE;
4688
4689 nvlist_free(setprops);
4690 nvlist_free(inheritprops);
4691 }
b128c09f 4692out:
43e52edd 4693 releasef(input_fd);
a3eeab2d 4694 nvlist_free(origrecvd);
b128c09f 4695 nvlist_free(origprops);
428870ff
BB
4696
4697 if (error == 0)
4698 error = props_error;
4699
34dc7c2f
BB
4700 return (error);
4701}
4702
43e52edd
BB
4703/*
4704 * inputs:
4705 * zc_name name of containing filesystem (unused)
4706 * zc_nvlist_src{_size} nvlist of properties to apply
a3eeab2d 4707 * zc_nvlist_conf{_size} nvlist of properties to exclude
4708 * (DATA_TYPE_BOOLEAN) and override (everything else)
43e52edd
BB
4709 * zc_value name of snapshot to create
4710 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4711 * zc_cookie file descriptor to recv from
4712 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4713 * zc_guid force flag
4714 * zc_cleanup_fd cleanup-on-exit file descriptor
4715 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4716 *
4717 * outputs:
4718 * zc_cookie number of bytes read
4719 * zc_obj zprop_errflags_t
4720 * zc_action_handle handle for this guid/ds mapping
4721 * zc_nvlist_dst{_size} error for each unapplied received property
4722 */
4723static int
4724zfs_ioc_recv(zfs_cmd_t *zc)
4725{
4726 dmu_replay_record_t begin_record;
4727 nvlist_t *errors = NULL;
a3eeab2d 4728 nvlist_t *recvdprops = NULL;
4729 nvlist_t *localprops = NULL;
43e52edd
BB
4730 char *origin = NULL;
4731 char *tosnap;
eca7b760 4732 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd
BB
4733 int error = 0;
4734
4735 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4736 strchr(zc->zc_value, '@') == NULL ||
4737 strchr(zc->zc_value, '%'))
4738 return (SET_ERROR(EINVAL));
4739
30f3f2e1 4740 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
43e52edd
BB
4741 tosnap = strchr(tofs, '@');
4742 *tosnap++ = '\0';
4743
4744 if (zc->zc_nvlist_src != 0 &&
4745 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
a3eeab2d 4746 zc->zc_iflags, &recvdprops)) != 0)
4747 return (error);
4748
4749 if (zc->zc_nvlist_conf != 0 &&
4750 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
4751 zc->zc_iflags, &localprops)) != 0)
43e52edd
BB
4752 return (error);
4753
4754 if (zc->zc_string[0])
4755 origin = zc->zc_string;
4756
4757 begin_record.drr_type = DRR_BEGIN;
4758 begin_record.drr_payloadlen = 0;
4759 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4760
a3eeab2d 4761 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
d9c460a0 4762 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
a3eeab2d 4763 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
4764 &zc->zc_action_handle, &errors);
4765 nvlist_free(recvdprops);
4766 nvlist_free(localprops);
43e52edd
BB
4767
4768 /*
4769 * Now that all props, initial and delayed, are set, report the prop
4770 * errors to the caller.
4771 */
4772 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4773 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4774 put_nvlist(zc, errors) != 0)) {
4775 /*
4776 * Caller made zc->zc_nvlist_dst less than the minimum expected
4777 * size or supplied an invalid address.
4778 */
4779 error = SET_ERROR(EINVAL);
4780 }
4781
4782 nvlist_free(errors);
4783
4784 return (error);
4785}
4786
4787/*
4788 * innvl: {
4789 * "snapname" -> full name of the snapshot to create
a3eeab2d 4790 * (optional) "props" -> received properties to set (nvlist)
4791 * (optional) "localprops" -> override and exclude properties (nvlist)
43e52edd
BB
4792 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4793 * "begin_record" -> non-byteswapped dmu_replay_record_t
4794 * "input_fd" -> file descriptor to read stream from (int32)
4795 * (optional) "force" -> force flag (value ignored)
4796 * (optional) "resumable" -> resumable flag (value ignored)
4797 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4798 * (optional) "action_handle" -> handle for this guid/ds mapping
b83a0e2d 4799 * (optional) "hidden_args" -> { "wkeydata" -> value }
43e52edd
BB
4800 * }
4801 *
4802 * outnvl: {
4803 * "read_bytes" -> number of bytes read
4804 * "error_flags" -> zprop_errflags_t
4805 * "action_handle" -> handle for this guid/ds mapping
4806 * "errors" -> error for each unapplied received property (nvlist)
4807 * }
4808 */
b83a0e2d
DB
4809static const zfs_ioc_key_t zfs_keys_recv_new[] = {
4810 {"snapname", DATA_TYPE_STRING, 0},
4811 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4812 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4813 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
4814 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
4815 {"input_fd", DATA_TYPE_INT32, 0},
4816 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
4817 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
4818 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
4819 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
4820 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4821};
4822
43e52edd
BB
4823static int
4824zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4825{
4826 dmu_replay_record_t *begin_record;
4827 uint_t begin_record_size;
4828 nvlist_t *errors = NULL;
a3eeab2d 4829 nvlist_t *recvprops = NULL;
4830 nvlist_t *localprops = NULL;
d9c460a0 4831 nvlist_t *hidden_args = NULL;
b83a0e2d 4832 char *snapname;
43e52edd
BB
4833 char *origin = NULL;
4834 char *tosnap;
eca7b760 4835 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd
BB
4836 boolean_t force;
4837 boolean_t resumable;
4838 uint64_t action_handle = 0;
4839 uint64_t read_bytes = 0;
4840 uint64_t errflags = 0;
4841 int input_fd = -1;
4842 int cleanup_fd = -1;
4843 int error;
4844
b83a0e2d 4845 snapname = fnvlist_lookup_string(innvl, "snapname");
43e52edd
BB
4846
4847 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
4848 strchr(snapname, '@') == NULL ||
4849 strchr(snapname, '%'))
4850 return (SET_ERROR(EINVAL));
4851
4852 (void) strcpy(tofs, snapname);
4853 tosnap = strchr(tofs, '@');
4854 *tosnap++ = '\0';
4855
4856 error = nvlist_lookup_string(innvl, "origin", &origin);
4857 if (error && error != ENOENT)
4858 return (error);
4859
4860 error = nvlist_lookup_byte_array(innvl, "begin_record",
02730c33 4861 (uchar_t **)&begin_record, &begin_record_size);
43e52edd
BB
4862 if (error != 0 || begin_record_size != sizeof (*begin_record))
4863 return (SET_ERROR(EINVAL));
4864
b83a0e2d 4865 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
43e52edd
BB
4866
4867 force = nvlist_exists(innvl, "force");
4868 resumable = nvlist_exists(innvl, "resumable");
4869
4870 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
4871 if (error && error != ENOENT)
4872 return (error);
4873
4874 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
4875 if (error && error != ENOENT)
4876 return (error);
4877
a3eeab2d 4878 /* we still use "props" here for backwards compatibility */
4879 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
43e52edd
BB
4880 if (error && error != ENOENT)
4881 return (error);
4882
a3eeab2d 4883 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
4884 if (error && error != ENOENT)
4885 return (error);
4886
d9c460a0
TC
4887 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
4888 if (error && error != ENOENT)
4889 return (error);
4890
a3eeab2d 4891 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
d9c460a0
TC
4892 hidden_args, force, resumable, input_fd, begin_record, cleanup_fd,
4893 &read_bytes, &errflags, &action_handle, &errors);
43e52edd
BB
4894
4895 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
4896 fnvlist_add_uint64(outnvl, "error_flags", errflags);
4897 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
4898 fnvlist_add_nvlist(outnvl, "errors", errors);
4899
4900 nvlist_free(errors);
a3eeab2d 4901 nvlist_free(recvprops);
4902 nvlist_free(localprops);
43e52edd
BB
4903
4904 return (error);
4905}
4906
34dc7c2f
BB
4907/*
4908 * inputs:
4909 * zc_name name of snapshot to send
34dc7c2f 4910 * zc_cookie file descriptor to send stream to
572e2857
BB
4911 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4912 * zc_sendobj objsetid of snapshot to send
4913 * zc_fromobj objsetid of incremental fromsnap (may be zero)
330d06f9
MA
4914 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4915 * output size in zc_objset_type.
f1512ee6 4916 * zc_flags lzc_send_flags
34dc7c2f 4917 *
da536844
MA
4918 * outputs:
4919 * zc_objset_type estimated size, if zc_guid is set
cf7684bc 4920 *
4921 * NOTE: This is no longer the preferred interface, any new functionality
4922 * should be added to zfs_ioc_send_new() instead.
34dc7c2f
BB
4923 */
4924static int
4925zfs_ioc_send(zfs_cmd_t *zc)
4926{
34dc7c2f
BB
4927 int error;
4928 offset_t off;
330d06f9 4929 boolean_t estimate = (zc->zc_guid != 0);
9b67f605 4930 boolean_t embedok = (zc->zc_flags & 0x1);
f1512ee6 4931 boolean_t large_block_ok = (zc->zc_flags & 0x2);
2aa34383 4932 boolean_t compressok = (zc->zc_flags & 0x4);
b5256303 4933 boolean_t rawok = (zc->zc_flags & 0x8);
34dc7c2f 4934
13fe0198
MA
4935 if (zc->zc_obj != 0) {
4936 dsl_pool_t *dp;
4937 dsl_dataset_t *tosnap;
34dc7c2f 4938
13fe0198
MA
4939 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4940 if (error != 0)
572e2857 4941 return (error);
13fe0198
MA
4942
4943 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4944 if (error != 0) {
4945 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
4946 return (error);
4947 }
13fe0198
MA
4948
4949 if (dsl_dir_is_clone(tosnap->ds_dir))
d683ddbb
JG
4950 zc->zc_fromobj =
4951 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
13fe0198
MA
4952 dsl_dataset_rele(tosnap, FTAG);
4953 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
4954 }
4955
13fe0198
MA
4956 if (estimate) {
4957 dsl_pool_t *dp;
4958 dsl_dataset_t *tosnap;
4959 dsl_dataset_t *fromsnap = NULL;
4960
4961 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4962 if (error != 0)
4963 return (error);
6f1ffb06 4964
b5256303
TC
4965 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
4966 FTAG, &tosnap);
13fe0198
MA
4967 if (error != 0) {
4968 dsl_pool_rele(dp, FTAG);
4969 return (error);
6f1ffb06
MA
4970 }
4971
13fe0198
MA
4972 if (zc->zc_fromobj != 0) {
4973 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4974 FTAG, &fromsnap);
4975 if (error != 0) {
4976 dsl_dataset_rele(tosnap, FTAG);
4977 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
4978 return (error);
4979 }
4980 }
34dc7c2f 4981
b5256303 4982 error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
330d06f9 4983 &zc->zc_objset_type);
13fe0198
MA
4984
4985 if (fromsnap != NULL)
4986 dsl_dataset_rele(fromsnap, FTAG);
4987 dsl_dataset_rele(tosnap, FTAG);
4988 dsl_pool_rele(dp, FTAG);
330d06f9
MA
4989 } else {
4990 file_t *fp = getf(zc->zc_cookie);
13fe0198 4991 if (fp == NULL)
2e528b49 4992 return (SET_ERROR(EBADF));
34dc7c2f 4993
330d06f9 4994 off = fp->f_offset;
13fe0198 4995 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
b5256303 4996 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
f1512ee6 4997 zc->zc_cookie, fp->f_vnode, &off);
34dc7c2f 4998
330d06f9
MA
4999 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5000 fp->f_offset = off;
5001 releasef(zc->zc_cookie);
5002 }
34dc7c2f
BB
5003 return (error);
5004}
5005
37abac6d
BP
5006/*
5007 * inputs:
5008 * zc_name name of snapshot on which to report progress
5009 * zc_cookie file descriptor of send stream
5010 *
5011 * outputs:
5012 * zc_cookie number of bytes written in send stream thus far
5013 */
5014static int
5015zfs_ioc_send_progress(zfs_cmd_t *zc)
5016{
13fe0198 5017 dsl_pool_t *dp;
37abac6d
BP
5018 dsl_dataset_t *ds;
5019 dmu_sendarg_t *dsp = NULL;
5020 int error;
5021
13fe0198
MA
5022 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5023 if (error != 0)
37abac6d
BP
5024 return (error);
5025
13fe0198
MA
5026 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5027 if (error != 0) {
5028 dsl_pool_rele(dp, FTAG);
5029 return (error);
5030 }
5031
37abac6d
BP
5032 mutex_enter(&ds->ds_sendstream_lock);
5033
5034 /*
5035 * Iterate over all the send streams currently active on this dataset.
5036 * If there's one which matches the specified file descriptor _and_ the
5037 * stream was started by the current process, return the progress of
5038 * that stream.
5039 */
5040
5041 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5042 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5043 if (dsp->dsa_outfd == zc->zc_cookie &&
5044 dsp->dsa_proc->group_leader == curproc->group_leader)
5045 break;
5046 }
5047
5048 if (dsp != NULL)
5049 zc->zc_cookie = *(dsp->dsa_off);
5050 else
2e528b49 5051 error = SET_ERROR(ENOENT);
37abac6d
BP
5052
5053 mutex_exit(&ds->ds_sendstream_lock);
5054 dsl_dataset_rele(ds, FTAG);
13fe0198 5055 dsl_pool_rele(dp, FTAG);
37abac6d
BP
5056 return (error);
5057}
5058
34dc7c2f
BB
5059static int
5060zfs_ioc_inject_fault(zfs_cmd_t *zc)
5061{
5062 int id, error;
5063
5064 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5065 &zc->zc_inject_record);
5066
5067 if (error == 0)
5068 zc->zc_guid = (uint64_t)id;
5069
5070 return (error);
5071}
5072
5073static int
5074zfs_ioc_clear_fault(zfs_cmd_t *zc)
5075{
5076 return (zio_clear_fault((int)zc->zc_guid));
5077}
5078
5079static int
5080zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5081{
5082 int id = (int)zc->zc_guid;
5083 int error;
5084
5085 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5086 &zc->zc_inject_record);
5087
5088 zc->zc_guid = id;
5089
5090 return (error);
5091}
5092
5093static int
5094zfs_ioc_error_log(zfs_cmd_t *zc)
5095{
5096 spa_t *spa;
5097 int error;
5098 size_t count = (size_t)zc->zc_nvlist_dst_size;
5099
5100 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5101 return (error);
5102
5103 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5104 &count);
5105 if (error == 0)
5106 zc->zc_nvlist_dst_size = count;
5107 else
5108 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5109
5110 spa_close(spa, FTAG);
5111
5112 return (error);
5113}
5114
5115static int
5116zfs_ioc_clear(zfs_cmd_t *zc)
5117{
5118 spa_t *spa;
5119 vdev_t *vd;
34dc7c2f
BB
5120 int error;
5121
34dc7c2f 5122 /*
b128c09f 5123 * On zpool clear we also fix up missing slogs
34dc7c2f 5124 */
b128c09f
BB
5125 mutex_enter(&spa_namespace_lock);
5126 spa = spa_lookup(zc->zc_name);
5127 if (spa == NULL) {
5128 mutex_exit(&spa_namespace_lock);
2e528b49 5129 return (SET_ERROR(EIO));
b128c09f 5130 }
428870ff 5131 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
b128c09f 5132 /* we need to let spa_open/spa_load clear the chains */
428870ff 5133 spa_set_log_state(spa, SPA_LOG_CLEAR);
34dc7c2f 5134 }
428870ff 5135 spa->spa_last_open_failed = 0;
b128c09f 5136 mutex_exit(&spa_namespace_lock);
34dc7c2f 5137
428870ff
BB
5138 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5139 error = spa_open(zc->zc_name, &spa, FTAG);
5140 } else {
5141 nvlist_t *policy;
5142 nvlist_t *config = NULL;
5143
b8864a23 5144 if (zc->zc_nvlist_src == 0)
2e528b49 5145 return (SET_ERROR(EINVAL));
428870ff
BB
5146
5147 if ((error = get_nvlist(zc->zc_nvlist_src,
5148 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5149 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5150 policy, &config);
5151 if (config != NULL) {
572e2857
BB
5152 int err;
5153
5154 if ((err = put_nvlist(zc, config)) != 0)
5155 error = err;
428870ff
BB
5156 nvlist_free(config);
5157 }
5158 nvlist_free(policy);
5159 }
5160 }
5161
13fe0198 5162 if (error != 0)
b128c09f
BB
5163 return (error);
5164
428870ff 5165 spa_vdev_state_enter(spa, SCL_NONE);
34dc7c2f
BB
5166
5167 if (zc->zc_guid == 0) {
5168 vd = NULL;
b128c09f
BB
5169 } else {
5170 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
34dc7c2f 5171 if (vd == NULL) {
b128c09f 5172 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
34dc7c2f 5173 spa_close(spa, FTAG);
2e528b49 5174 return (SET_ERROR(ENODEV));
34dc7c2f
BB
5175 }
5176 }
5177
b128c09f
BB
5178 vdev_clear(spa, vd);
5179
3f759c0c
BB
5180 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5181 NULL : spa->spa_root_vdev, 0);
34dc7c2f 5182
b128c09f
BB
5183 /*
5184 * Resume any suspended I/Os.
5185 */
9babb374 5186 if (zio_resume(spa) != 0)
2e528b49 5187 error = SET_ERROR(EIO);
34dc7c2f
BB
5188
5189 spa_close(spa, FTAG);
5190
9babb374 5191 return (error);
34dc7c2f
BB
5192}
5193
d3f2cd7e
AB
5194/*
5195 * Reopen all the vdevs associated with the pool.
5196 *
5197 * innvl: {
5198 * "scrub_restart" -> when true and scrub is running, allow to restart
5199 * scrub as the side effect of the reopen (boolean).
5200 * }
5201 *
5202 * outnvl is unused
5203 */
b83a0e2d
DB
5204static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5205 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, 0},
5206};
5207
d3f2cd7e 5208/* ARGSUSED */
1bd201e7 5209static int
d3f2cd7e 5210zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
1bd201e7
CS
5211{
5212 spa_t *spa;
5213 int error;
d3f2cd7e 5214 boolean_t scrub_restart = B_TRUE;
1bd201e7 5215
d3f2cd7e 5216 if (innvl) {
b83a0e2d
DB
5217 scrub_restart = fnvlist_lookup_boolean_value(innvl,
5218 "scrub_restart");
d3f2cd7e
AB
5219 }
5220
5221 error = spa_open(pool, &spa, FTAG);
13fe0198 5222 if (error != 0)
1bd201e7
CS
5223 return (error);
5224
5225 spa_vdev_state_enter(spa, SCL_NONE);
65947351
GW
5226
5227 /*
d3f2cd7e
AB
5228 * If the scrub_restart flag is B_FALSE and a scrub is already
5229 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5230 * we don't restart the scrub as a side effect of the reopen.
5231 * Otherwise, let vdev_open() decided if a resilver is required.
65947351 5232 */
d3f2cd7e
AB
5233
5234 spa->spa_scrub_reopen = (!scrub_restart &&
5235 dsl_scan_scrubbing(spa->spa_dsl_pool));
1bd201e7 5236 vdev_reopen(spa->spa_root_vdev);
65947351
GW
5237 spa->spa_scrub_reopen = B_FALSE;
5238
1bd201e7
CS
5239 (void) spa_vdev_state_exit(spa, NULL, 0);
5240 spa_close(spa, FTAG);
5241 return (0);
5242}
d3f2cd7e 5243
34dc7c2f
BB
5244/*
5245 * inputs:
5246 * zc_name name of filesystem
34dc7c2f 5247 *
428870ff
BB
5248 * outputs:
5249 * zc_string name of conflicting snapshot, if there is one
34dc7c2f
BB
5250 */
5251static int
5252zfs_ioc_promote(zfs_cmd_t *zc)
5253{
d12f91fd
GDN
5254 dsl_pool_t *dp;
5255 dsl_dataset_t *ds, *ods;
5256 char origin[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 5257 char *cp;
d12f91fd
GDN
5258 int error;
5259
650258d7 5260 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5261 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5262 strchr(zc->zc_name, '%'))
5263 return (SET_ERROR(EINVAL));
5264
d12f91fd
GDN
5265 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5266 if (error != 0)
5267 return (error);
5268
5269 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5270 if (error != 0) {
5271 dsl_pool_rele(dp, FTAG);
5272 return (error);
5273 }
5274
5275 if (!dsl_dir_is_clone(ds->ds_dir)) {
5276 dsl_dataset_rele(ds, FTAG);
5277 dsl_pool_rele(dp, FTAG);
5278 return (SET_ERROR(EINVAL));
5279 }
5280
5281 error = dsl_dataset_hold_obj(dp,
5282 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5283 if (error != 0) {
5284 dsl_dataset_rele(ds, FTAG);
5285 dsl_pool_rele(dp, FTAG);
5286 return (error);
5287 }
5288
5289 dsl_dataset_name(ods, origin);
5290 dsl_dataset_rele(ods, FTAG);
5291 dsl_dataset_rele(ds, FTAG);
5292 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
5293
5294 /*
5295 * We don't need to unmount *all* the origin fs's snapshots, but
5296 * it's easier.
5297 */
d12f91fd 5298 cp = strchr(origin, '@');
34dc7c2f
BB
5299 if (cp)
5300 *cp = '\0';
d12f91fd 5301 (void) dmu_objset_find(origin,
13fe0198 5302 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
428870ff 5303 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
34dc7c2f
BB
5304}
5305
9babb374 5306/*
9c5167d1 5307 * Retrieve a single {user|group|project}{used|quota}@... property.
9babb374
BB
5308 *
5309 * inputs:
5310 * zc_name name of filesystem
5311 * zc_objset_type zfs_userquota_prop_t
5312 * zc_value domain name (eg. "S-1-234-567-89")
5313 * zc_guid RID/UID/GID
5314 *
5315 * outputs:
5316 * zc_cookie property value
5317 */
5318static int
5319zfs_ioc_userspace_one(zfs_cmd_t *zc)
5320{
0037b49e 5321 zfsvfs_t *zfsvfs;
9babb374
BB
5322 int error;
5323
5324 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 5325 return (SET_ERROR(EINVAL));
9babb374 5326
f298b24d 5327 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5328 if (error != 0)
9babb374
BB
5329 return (error);
5330
0037b49e 5331 error = zfs_userspace_one(zfsvfs,
9babb374 5332 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
f298b24d 5333 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5334
5335 return (error);
5336}
5337
5338/*
5339 * inputs:
5340 * zc_name name of filesystem
5341 * zc_cookie zap cursor
5342 * zc_objset_type zfs_userquota_prop_t
5343 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5344 *
5345 * outputs:
5346 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5347 * zc_cookie zap cursor
5348 */
5349static int
5350zfs_ioc_userspace_many(zfs_cmd_t *zc)
5351{
0037b49e 5352 zfsvfs_t *zfsvfs;
428870ff
BB
5353 int bufsize = zc->zc_nvlist_dst_size;
5354
5355 if (bufsize <= 0)
2e528b49 5356 return (SET_ERROR(ENOMEM));
9babb374 5357
1c27024e 5358 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5359 if (error != 0)
9babb374
BB
5360 return (error);
5361
1c27024e 5362 void *buf = vmem_alloc(bufsize, KM_SLEEP);
9babb374 5363
0037b49e 5364 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
9babb374
BB
5365 buf, &zc->zc_nvlist_dst_size);
5366
5367 if (error == 0) {
5368 error = xcopyout(buf,
5369 (void *)(uintptr_t)zc->zc_nvlist_dst,
5370 zc->zc_nvlist_dst_size);
5371 }
2b8cad61 5372 vmem_free(buf, bufsize);
f298b24d 5373 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5374
5375 return (error);
5376}
5377
5378/*
5379 * inputs:
5380 * zc_name name of filesystem
5381 *
5382 * outputs:
5383 * none
5384 */
5385static int
5386zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5387{
5388 objset_t *os;
428870ff 5389 int error = 0;
0037b49e 5390 zfsvfs_t *zfsvfs;
9babb374 5391
f298b24d 5392 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
0037b49e 5393 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
9babb374
BB
5394 /*
5395 * If userused is not enabled, it may be because the
5396 * objset needs to be closed & reopened (to grow the
5397 * objset_phys_t). Suspend/resume the fs will do that.
5398 */
5e00213e 5399 dsl_dataset_t *ds, *newds;
ec923db2 5400
0037b49e
BB
5401 ds = dmu_objset_ds(zfsvfs->z_os);
5402 error = zfs_suspend_fs(zfsvfs);
831baf06 5403 if (error == 0) {
5e00213e 5404 dmu_objset_refresh_ownership(ds, &newds,
b5256303 5405 B_TRUE, zfsvfs);
5e00213e 5406 error = zfs_resume_fs(zfsvfs, newds);
831baf06 5407 }
9babb374
BB
5408 }
5409 if (error == 0)
0037b49e
BB
5410 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5411 deactivate_super(zfsvfs->z_sb);
9babb374 5412 } else {
428870ff 5413 /* XXX kind of reading contents without owning */
b5256303 5414 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
13fe0198 5415 if (error != 0)
9babb374
BB
5416 return (error);
5417
5418 error = dmu_objset_userspace_upgrade(os);
b5256303 5419 dmu_objset_rele_flags(os, B_TRUE, FTAG);
9babb374
BB
5420 }
5421
5422 return (error);
5423}
5424
1de321e6
JX
5425/*
5426 * inputs:
5427 * zc_name name of filesystem
5428 *
5429 * outputs:
5430 * none
5431 */
5432static int
9c5167d1 5433zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
1de321e6
JX
5434{
5435 objset_t *os;
5436 int error;
5437
b5256303 5438 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
1de321e6
JX
5439 if (error != 0)
5440 return (error);
5441
9c5167d1
NF
5442 if (dmu_objset_userobjspace_upgradable(os) ||
5443 dmu_objset_projectquota_upgradable(os)) {
1de321e6
JX
5444 mutex_enter(&os->os_upgrade_lock);
5445 if (os->os_upgrade_id == 0) {
5446 /* clear potential error code and retry */
5447 os->os_upgrade_status = 0;
5448 mutex_exit(&os->os_upgrade_lock);
5449
9c5167d1 5450 dmu_objset_id_quota_upgrade(os);
1de321e6
JX
5451 } else {
5452 mutex_exit(&os->os_upgrade_lock);
5453 }
5454
c0daec32
AB
5455 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5456
1de321e6
JX
5457 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5458 error = os->os_upgrade_status;
c0daec32
AB
5459 } else {
5460 dsl_pool_rele(dmu_objset_pool(os), FTAG);
1de321e6
JX
5461 }
5462
b5256303 5463 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
1de321e6
JX
5464
5465 return (error);
5466}
5467
34dc7c2f
BB
5468static int
5469zfs_ioc_share(zfs_cmd_t *zc)
5470{
2e528b49 5471 return (SET_ERROR(ENOSYS));
34dc7c2f
BB
5472}
5473
9babb374
BB
5474ace_t full_access[] = {
5475 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5476};
5477
572e2857
BB
5478/*
5479 * inputs:
5480 * zc_name name of containing filesystem
5481 * zc_obj object # beyond which we want next in-use object #
5482 *
5483 * outputs:
5484 * zc_obj next in-use object #
5485 */
5486static int
5487zfs_ioc_next_obj(zfs_cmd_t *zc)
5488{
5489 objset_t *os = NULL;
5490 int error;
5491
5492 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
13fe0198 5493 if (error != 0)
572e2857
BB
5494 return (error);
5495
7290cd3c 5496 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
572e2857
BB
5497
5498 dmu_objset_rele(os, FTAG);
5499 return (error);
5500}
5501
5502/*
5503 * inputs:
5504 * zc_name name of filesystem
5505 * zc_value prefix name for snapshot
5506 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5507 *
5508 * outputs:
6f1ffb06 5509 * zc_value short name of new snapshot
572e2857
BB
5510 */
5511static int
5512zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5513{
5514 char *snap_name;
13fe0198 5515 char *hold_name;
572e2857 5516 int error;
13fe0198 5517 minor_t minor;
572e2857 5518
13fe0198
MA
5519 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5520 if (error != 0)
572e2857 5521 return (error);
572e2857 5522
13fe0198
MA
5523 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5524 (u_longlong_t)ddi_get_lbolt64());
5525 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5526
5527 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5528 hold_name);
5529 if (error == 0)
680eada9 5530 (void) strlcpy(zc->zc_value, snap_name,
5531 sizeof (zc->zc_value));
572e2857 5532 strfree(snap_name);
13fe0198
MA
5533 strfree(hold_name);
5534 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5535 return (error);
572e2857
BB
5536}
5537
5538/*
5539 * inputs:
5540 * zc_name name of "to" snapshot
5541 * zc_value name of "from" snapshot
5542 * zc_cookie file descriptor to write diff data on
5543 *
5544 * outputs:
5545 * dmu_diff_record_t's to the file descriptor
5546 */
5547static int
5548zfs_ioc_diff(zfs_cmd_t *zc)
5549{
572e2857
BB
5550 file_t *fp;
5551 offset_t off;
5552 int error;
5553
572e2857 5554 fp = getf(zc->zc_cookie);
13fe0198 5555 if (fp == NULL)
2e528b49 5556 return (SET_ERROR(EBADF));
572e2857
BB
5557
5558 off = fp->f_offset;
5559
13fe0198 5560 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
572e2857
BB
5561
5562 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5563 fp->f_offset = off;
5564 releasef(zc->zc_cookie);
5565
572e2857
BB
5566 return (error);
5567}
5568
9babb374
BB
5569static int
5570zfs_ioc_smb_acl(zfs_cmd_t *zc)
5571{
2e528b49 5572 return (SET_ERROR(ENOTSUP));
9babb374
BB
5573}
5574
45d1cae3 5575/*
13fe0198
MA
5576 * innvl: {
5577 * "holds" -> { snapname -> holdname (string), ... }
5578 * (optional) "cleanup_fd" -> fd (int32)
5579 * }
45d1cae3 5580 *
13fe0198
MA
5581 * outnvl: {
5582 * snapname -> error value (int32)
5583 * ...
5584 * }
45d1cae3 5585 */
b83a0e2d
DB
5586static const zfs_ioc_key_t zfs_keys_hold[] = {
5587 {"holds", DATA_TYPE_NVLIST, 0},
5588 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5589};
5590
13fe0198 5591/* ARGSUSED */
45d1cae3 5592static int
13fe0198 5593zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
45d1cae3 5594{
fc581e05 5595 nvpair_t *pair;
13fe0198
MA
5596 nvlist_t *holds;
5597 int cleanup_fd = -1;
572e2857
BB
5598 int error;
5599 minor_t minor = 0;
45d1cae3 5600
b83a0e2d 5601 holds = fnvlist_lookup_nvlist(args, "holds");
572e2857 5602
fc581e05
JJS
5603 /* make sure the user didn't pass us any invalid (empty) tags */
5604 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5605 pair = nvlist_next_nvpair(holds, pair)) {
5606 char *htag;
5607
5608 error = nvpair_value_string(pair, &htag);
5609 if (error != 0)
5610 return (SET_ERROR(error));
5611
5612 if (strlen(htag) == 0)
5613 return (SET_ERROR(EINVAL));
5614 }
5615
13fe0198
MA
5616 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5617 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5618 if (error != 0)
572e2857 5619 return (error);
572e2857 5620 }
572e2857 5621
13fe0198
MA
5622 error = dsl_dataset_user_hold(holds, minor, errlist);
5623 if (minor != 0)
5624 zfs_onexit_fd_rele(cleanup_fd);
572e2857 5625 return (error);
45d1cae3
BB
5626}
5627
5628/*
13fe0198 5629 * innvl is not used.
45d1cae3 5630 *
13fe0198
MA
5631 * outnvl: {
5632 * holdname -> time added (uint64 seconds since epoch)
5633 * ...
5634 * }
45d1cae3 5635 */
b83a0e2d
DB
5636static const zfs_ioc_key_t zfs_keys_get_holds[] = {
5637 /* no nvl keys */
5638};
5639
13fe0198 5640/* ARGSUSED */
45d1cae3 5641static int
13fe0198 5642zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
45d1cae3 5643{
13fe0198 5644 return (dsl_dataset_get_holds(snapname, outnvl));
45d1cae3
BB
5645}
5646
5647/*
13fe0198
MA
5648 * innvl: {
5649 * snapname -> { holdname, ... }
5650 * ...
5651 * }
45d1cae3 5652 *
13fe0198
MA
5653 * outnvl: {
5654 * snapname -> error value (int32)
5655 * ...
5656 * }
45d1cae3 5657 */
b83a0e2d
DB
5658static const zfs_ioc_key_t zfs_keys_release[] = {
5659 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
5660};
5661
13fe0198 5662/* ARGSUSED */
45d1cae3 5663static int
13fe0198 5664zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
45d1cae3 5665{
13fe0198 5666 return (dsl_dataset_user_release(holds, errlist));
45d1cae3
BB
5667}
5668
26685276
BB
5669/*
5670 * inputs:
5671 * zc_guid flags (ZEVENT_NONBLOCK)
9b101a73 5672 * zc_cleanup_fd zevent file descriptor
26685276
BB
5673 *
5674 * outputs:
5675 * zc_nvlist_dst next nvlist event
5676 * zc_cookie dropped events since last get
26685276
BB
5677 */
5678static int
5679zfs_ioc_events_next(zfs_cmd_t *zc)
5680{
5681 zfs_zevent_t *ze;
5682 nvlist_t *event = NULL;
5683 minor_t minor;
5684 uint64_t dropped = 0;
5685 int error;
5686
5687 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5688 if (error != 0)
5689 return (error);
5690
5691 do {
baa40d45 5692 error = zfs_zevent_next(ze, &event,
02730c33 5693 &zc->zc_nvlist_dst_size, &dropped);
26685276
BB
5694 if (event != NULL) {
5695 zc->zc_cookie = dropped;
5696 error = put_nvlist(zc, event);
baa40d45 5697 nvlist_free(event);
26685276
BB
5698 }
5699
5700 if (zc->zc_guid & ZEVENT_NONBLOCK)
5701 break;
5702
5703 if ((error == 0) || (error != ENOENT))
5704 break;
5705
5706 error = zfs_zevent_wait(ze);
13fe0198 5707 if (error != 0)
26685276
BB
5708 break;
5709 } while (1);
5710
5711 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5712
5713 return (error);
5714}
5715
5716/*
5717 * outputs:
5718 * zc_cookie cleared events count
5719 */
5720static int
5721zfs_ioc_events_clear(zfs_cmd_t *zc)
5722{
5723 int count;
5724
5725 zfs_zevent_drain_all(&count);
5726 zc->zc_cookie = count;
5727
d1d7e268 5728 return (0);
26685276
BB
5729}
5730
75e3ff58
BB
5731/*
5732 * inputs:
5733 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5734 * zc_cleanup zevent file descriptor
5735 */
5736static int
5737zfs_ioc_events_seek(zfs_cmd_t *zc)
5738{
5739 zfs_zevent_t *ze;
5740 minor_t minor;
5741 int error;
5742
5743 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5744 if (error != 0)
5745 return (error);
5746
5747 error = zfs_zevent_seek(ze, zc->zc_guid);
5748 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5749
5750 return (error);
5751}
5752
330d06f9
MA
5753/*
5754 * inputs:
5755 * zc_name name of new filesystem or snapshot
5756 * zc_value full name of old snapshot
5757 *
5758 * outputs:
5759 * zc_cookie space in bytes
5760 * zc_objset_type compressed space in bytes
5761 * zc_perm_action uncompressed space in bytes
5762 */
5763static int
5764zfs_ioc_space_written(zfs_cmd_t *zc)
5765{
5766 int error;
13fe0198 5767 dsl_pool_t *dp;
330d06f9
MA
5768 dsl_dataset_t *new, *old;
5769
13fe0198 5770 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
330d06f9
MA
5771 if (error != 0)
5772 return (error);
13fe0198
MA
5773 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5774 if (error != 0) {
5775 dsl_pool_rele(dp, FTAG);
5776 return (error);
5777 }
5778 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
330d06f9
MA
5779 if (error != 0) {
5780 dsl_dataset_rele(new, FTAG);
13fe0198 5781 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5782 return (error);
5783 }
5784
5785 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5786 &zc->zc_objset_type, &zc->zc_perm_action);
5787 dsl_dataset_rele(old, FTAG);
5788 dsl_dataset_rele(new, FTAG);
13fe0198 5789 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5790 return (error);
5791}
5792
5793/*
6f1ffb06
MA
5794 * innvl: {
5795 * "firstsnap" -> snapshot name
5796 * }
330d06f9 5797 *
6f1ffb06
MA
5798 * outnvl: {
5799 * "used" -> space in bytes
5800 * "compressed" -> compressed space in bytes
5801 * "uncompressed" -> uncompressed space in bytes
5802 * }
330d06f9 5803 */
b83a0e2d
DB
5804static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
5805 {"firstsnap", DATA_TYPE_STRING, 0},
5806};
5807
330d06f9 5808static int
6f1ffb06 5809zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
330d06f9
MA
5810{
5811 int error;
13fe0198 5812 dsl_pool_t *dp;
330d06f9 5813 dsl_dataset_t *new, *old;
6f1ffb06
MA
5814 char *firstsnap;
5815 uint64_t used, comp, uncomp;
330d06f9 5816
b83a0e2d 5817 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6f1ffb06 5818
13fe0198 5819 error = dsl_pool_hold(lastsnap, FTAG, &dp);
330d06f9
MA
5820 if (error != 0)
5821 return (error);
13fe0198
MA
5822
5823 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
71e2fe41
AG
5824 if (error == 0 && !new->ds_is_snapshot) {
5825 dsl_dataset_rele(new, FTAG);
5826 error = SET_ERROR(EINVAL);
5827 }
13fe0198
MA
5828 if (error != 0) {
5829 dsl_pool_rele(dp, FTAG);
5830 return (error);
5831 }
5832 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
71e2fe41
AG
5833 if (error == 0 && !old->ds_is_snapshot) {
5834 dsl_dataset_rele(old, FTAG);
5835 error = SET_ERROR(EINVAL);
5836 }
330d06f9
MA
5837 if (error != 0) {
5838 dsl_dataset_rele(new, FTAG);
13fe0198 5839 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5840 return (error);
5841 }
5842
6f1ffb06 5843 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
330d06f9
MA
5844 dsl_dataset_rele(old, FTAG);
5845 dsl_dataset_rele(new, FTAG);
13fe0198 5846 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
5847 fnvlist_add_uint64(outnvl, "used", used);
5848 fnvlist_add_uint64(outnvl, "compressed", comp);
5849 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
330d06f9
MA
5850 return (error);
5851}
5852
34dc7c2f 5853/*
6f1ffb06
MA
5854 * innvl: {
5855 * "fd" -> file descriptor to write stream to (int32)
5856 * (optional) "fromsnap" -> full snap name to send an incremental from
f1512ee6
MA
5857 * (optional) "largeblockok" -> (value ignored)
5858 * indicates that blocks > 128KB are permitted
9b67f605
MA
5859 * (optional) "embedok" -> (value ignored)
5860 * presence indicates DRR_WRITE_EMBEDDED records are permitted
2aa34383
DK
5861 * (optional) "compressok" -> (value ignored)
5862 * presence indicates compressed DRR_WRITE records are permitted
b5256303
TC
5863 * (optional) "rawok" -> (value ignored)
5864 * presence indicates raw encrypted records should be used.
47dfff3b
MA
5865 * (optional) "resume_object" and "resume_offset" -> (uint64)
5866 * if present, resume send stream from specified object and offset.
6f1ffb06
MA
5867 * }
5868 *
5869 * outnvl is unused
34dc7c2f 5870 */
b83a0e2d
DB
5871static const zfs_ioc_key_t zfs_keys_send_new[] = {
5872 {"fd", DATA_TYPE_INT32, 0},
5873 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
5874 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5875 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5876 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5877 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5878 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
5879 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
5880};
5881
6f1ffb06
MA
5882/* ARGSUSED */
5883static int
5884zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5885{
6f1ffb06
MA
5886 int error;
5887 offset_t off;
13fe0198 5888 char *fromname = NULL;
6f1ffb06 5889 int fd;
13fe0198 5890 file_t *fp;
f1512ee6 5891 boolean_t largeblockok;
9b67f605 5892 boolean_t embedok;
2aa34383 5893 boolean_t compressok;
b5256303 5894 boolean_t rawok;
47dfff3b
MA
5895 uint64_t resumeobj = 0;
5896 uint64_t resumeoff = 0;
6f1ffb06 5897
b83a0e2d 5898 fd = fnvlist_lookup_int32(innvl, "fd");
6f1ffb06 5899
13fe0198 5900 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6f1ffb06 5901
f1512ee6 5902 largeblockok = nvlist_exists(innvl, "largeblockok");
9b67f605 5903 embedok = nvlist_exists(innvl, "embedok");
2aa34383 5904 compressok = nvlist_exists(innvl, "compressok");
b5256303 5905 rawok = nvlist_exists(innvl, "rawok");
9b67f605 5906
47dfff3b
MA
5907 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5908 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5909
13fe0198 5910 if ((fp = getf(fd)) == NULL)
2e528b49 5911 return (SET_ERROR(EBADF));
6f1ffb06
MA
5912
5913 off = fp->f_offset;
2aa34383 5914 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
b5256303 5915 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6f1ffb06
MA
5916
5917 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5918 fp->f_offset = off;
13fe0198 5919
6f1ffb06 5920 releasef(fd);
6f1ffb06
MA
5921 return (error);
5922}
5923
5924/*
5925 * Determine approximately how large a zfs send stream will be -- the number
5926 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5927 *
5928 * innvl: {
5dc8b736
MG
5929 * (optional) "from" -> full snap or bookmark name to send an incremental
5930 * from
2aa34383
DK
5931 * (optional) "largeblockok" -> (value ignored)
5932 * indicates that blocks > 128KB are permitted
5933 * (optional) "embedok" -> (value ignored)
5934 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5935 * (optional) "compressok" -> (value ignored)
5936 * presence indicates compressed DRR_WRITE records are permitted
cf7684bc 5937 * (optional) "rawok" -> (value ignored)
5938 * presence indicates raw encrypted records should be used.
6f1ffb06
MA
5939 * }
5940 *
5941 * outnvl: {
5942 * "space" -> bytes of space (uint64)
5943 * }
5944 */
b83a0e2d
DB
5945static const zfs_ioc_key_t zfs_keys_send_space[] = {
5946 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
5947 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
5948 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5949 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5950 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5951 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5952};
5953
6f1ffb06
MA
5954static int
5955zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5956{
13fe0198 5957 dsl_pool_t *dp;
13fe0198 5958 dsl_dataset_t *tosnap;
6f1ffb06
MA
5959 int error;
5960 char *fromname;
2aa34383 5961 boolean_t compressok;
b5256303 5962 boolean_t rawok;
6f1ffb06
MA
5963 uint64_t space;
5964
13fe0198
MA
5965 error = dsl_pool_hold(snapname, FTAG, &dp);
5966 if (error != 0)
6f1ffb06
MA
5967 return (error);
5968
13fe0198
MA
5969 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5970 if (error != 0) {
5971 dsl_pool_rele(dp, FTAG);
5972 return (error);
5973 }
5974
2aa34383 5975 compressok = nvlist_exists(innvl, "compressok");
b5256303 5976 rawok = nvlist_exists(innvl, "rawok");
2aa34383 5977
5dc8b736 5978 error = nvlist_lookup_string(innvl, "from", &fromname);
6f1ffb06 5979 if (error == 0) {
5dc8b736
MG
5980 if (strchr(fromname, '@') != NULL) {
5981 /*
5982 * If from is a snapshot, hold it and use the more
5983 * efficient dmu_send_estimate to estimate send space
5984 * size using deadlists.
5985 */
5986 dsl_dataset_t *fromsnap;
5987 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5988 if (error != 0)
5989 goto out;
b5256303
TC
5990 error = dmu_send_estimate(tosnap, fromsnap,
5991 compressok || rawok, &space);
5dc8b736
MG
5992 dsl_dataset_rele(fromsnap, FTAG);
5993 } else if (strchr(fromname, '#') != NULL) {
5994 /*
5995 * If from is a bookmark, fetch the creation TXG of the
5996 * snapshot it was created from and use that to find
5997 * blocks that were born after it.
5998 */
5999 zfs_bookmark_phys_t frombm;
6000
6001 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6002 &frombm);
6003 if (error != 0)
6004 goto out;
6005 error = dmu_send_estimate_from_txg(tosnap,
b5256303
TC
6006 frombm.zbm_creation_txg, compressok || rawok,
6007 &space);
5dc8b736
MG
6008 } else {
6009 /*
6010 * from is not properly formatted as a snapshot or
6011 * bookmark
6012 */
6013 error = SET_ERROR(EINVAL);
6014 goto out;
6f1ffb06 6015 }
5dc8b736 6016 } else {
d8fdfc2d
BB
6017 /*
6018 * If estimating the size of a full send, use dmu_send_estimate.
6019 */
b5256303
TC
6020 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6021 &space);
6f1ffb06
MA
6022 }
6023
6f1ffb06
MA
6024 fnvlist_add_uint64(outnvl, "space", space);
6025
5dc8b736 6026out:
13fe0198
MA
6027 dsl_dataset_rele(tosnap, FTAG);
6028 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
6029 return (error);
6030}
6031
bec1067d
AP
6032/*
6033 * Sync the currently open TXG to disk for the specified pool.
6034 * This is somewhat similar to 'zfs_sync()'.
6035 * For cases that do not result in error this ioctl will wait for
6036 * the currently open TXG to commit before returning back to the caller.
6037 *
6038 * innvl: {
6039 * "force" -> when true, force uberblock update even if there is no dirty data.
6040 * In addition this will cause the vdev configuration to be written
6041 * out including updating the zpool cache file. (boolean_t)
6042 * }
6043 *
6044 * onvl is unused
6045 */
b83a0e2d
DB
6046static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6047 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6048};
6049
bec1067d
AP
6050/* ARGSUSED */
6051static int
6052zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6053{
6054 int err;
05f85a6a 6055 boolean_t force = B_FALSE;
bec1067d
AP
6056 spa_t *spa;
6057
6058 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6059 return (err);
6060
b83a0e2d
DB
6061 if (innvl)
6062 force = fnvlist_lookup_boolean_value(innvl, "force");
05f85a6a 6063
bec1067d
AP
6064 if (force) {
6065 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6066 vdev_config_dirty(spa->spa_root_vdev);
6067 spa_config_exit(spa, SCL_CONFIG, FTAG);
6068 }
6069 txg_wait_synced(spa_get_dsl(spa), 0);
b83a0e2d 6070
bec1067d
AP
6071 spa_close(spa, FTAG);
6072
6073 return (err);
6074}
6075
b5256303
TC
6076/*
6077 * Load a user's wrapping key into the kernel.
6078 * innvl: {
6079 * "hidden_args" -> { "wkeydata" -> value }
6080 * raw uint8_t array of encryption wrapping key data (32 bytes)
6081 * (optional) "noop" -> (value ignored)
6082 * presence indicated key should only be verified, not loaded
6083 * }
6084 */
b83a0e2d
DB
6085static const zfs_ioc_key_t zfs_keys_load_key[] = {
6086 {"hidden_args", DATA_TYPE_NVLIST, 0},
6087 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6088};
6089
b5256303
TC
6090/* ARGSUSED */
6091static int
6092zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6093{
6094 int ret;
6095 dsl_crypto_params_t *dcp = NULL;
6096 nvlist_t *hidden_args;
6097 boolean_t noop = nvlist_exists(innvl, "noop");
6098
6099 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6100 ret = SET_ERROR(EINVAL);
6101 goto error;
6102 }
6103
b83a0e2d 6104 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
b5256303
TC
6105
6106 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6107 hidden_args, &dcp);
6108 if (ret != 0)
6109 goto error;
6110
6111 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6112 if (ret != 0)
6113 goto error;
6114
6115 dsl_crypto_params_free(dcp, noop);
6116
6117 return (0);
6118
6119error:
6120 dsl_crypto_params_free(dcp, B_TRUE);
6121 return (ret);
6122}
6123
6124/*
6125 * Unload a user's wrapping key from the kernel.
6126 * Both innvl and outnvl are unused.
6127 */
b83a0e2d
DB
6128static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6129 /* no nvl keys */
6130};
6131
b5256303
TC
6132/* ARGSUSED */
6133static int
6134zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6135{
6136 int ret = 0;
6137
6138 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6139 ret = (SET_ERROR(EINVAL));
6140 goto out;
6141 }
6142
6143 ret = spa_keystore_unload_wkey(dsname);
6144 if (ret != 0)
6145 goto out;
6146
6147out:
6148 return (ret);
6149}
6150
6151/*
6152 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6153 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6154 * here to change how the key is derived in userspace.
6155 *
6156 * innvl: {
6157 * "hidden_args" (optional) -> { "wkeydata" -> value }
6158 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6159 * "props" (optional) -> { prop -> value }
6160 * }
6161 *
6162 * outnvl is unused
6163 */
b83a0e2d
DB
6164static const zfs_ioc_key_t zfs_keys_change_key[] = {
6165 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6166 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6167 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6168};
6169
b5256303
TC
6170/* ARGSUSED */
6171static int
6172zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6173{
6174 int ret;
6175 uint64_t cmd = DCP_CMD_NONE;
6176 dsl_crypto_params_t *dcp = NULL;
6177 nvlist_t *args = NULL, *hidden_args = NULL;
6178
6179 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6180 ret = (SET_ERROR(EINVAL));
6181 goto error;
6182 }
6183
6184 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6185 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6186 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6187
6188 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6189 if (ret != 0)
6190 goto error;
6191
6192 ret = spa_keystore_change_key(dsname, dcp);
6193 if (ret != 0)
6194 goto error;
6195
6196 dsl_crypto_params_free(dcp, B_FALSE);
6197
6198 return (0);
6199
6200error:
6201 dsl_crypto_params_free(dcp, B_TRUE);
6202 return (ret);
6203}
6204
6f1ffb06
MA
6205static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6206
6207static void
6208zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6209 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6210 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6211{
6212 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6213
6214 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6215 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6216 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6217 ASSERT3P(vec->zvec_func, ==, NULL);
6218
6219 vec->zvec_legacy_func = func;
6220 vec->zvec_secpolicy = secpolicy;
6221 vec->zvec_namecheck = namecheck;
6222 vec->zvec_allow_log = log_history;
6223 vec->zvec_pool_check = pool_check;
6224}
6225
6226/*
6227 * See the block comment at the beginning of this file for details on
6228 * each argument to this function.
6229 */
6230static void
6231zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6232 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6233 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
b83a0e2d 6234 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6f1ffb06
MA
6235{
6236 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6237
6238 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6239 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6240 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6241 ASSERT3P(vec->zvec_func, ==, NULL);
6242
6243 /* if we are logging, the name must be valid */
6244 ASSERT(!allow_log || namecheck != NO_NAME);
6245
6246 vec->zvec_name = name;
6247 vec->zvec_func = func;
6248 vec->zvec_secpolicy = secpolicy;
6249 vec->zvec_namecheck = namecheck;
6250 vec->zvec_pool_check = pool_check;
6251 vec->zvec_smush_outnvlist = smush_outnvlist;
6252 vec->zvec_allow_log = allow_log;
b83a0e2d
DB
6253 vec->zvec_nvl_keys = nvl_keys;
6254 vec->zvec_nvl_key_count = num_keys;
6f1ffb06
MA
6255}
6256
6257static void
6258zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6259 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6260 zfs_ioc_poolcheck_t pool_check)
6261{
6262 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6263 POOL_NAME, log_history, pool_check);
6264}
6265
6266static void
6267zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6268 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6269{
6270 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6271 DATASET_NAME, B_FALSE, pool_check);
6272}
6273
6274static void
6275zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6276{
6277 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6278 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6279}
6280
6281static void
6282zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6283 zfs_secpolicy_func_t *secpolicy)
6284{
6285 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6286 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6287}
6288
6289static void
6290zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6291 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6292{
6293 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6294 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6295}
6296
6297static void
6298zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6299{
6300 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6301 zfs_secpolicy_read);
6302}
6303
6304static void
6305zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
e9aa730c 6306 zfs_secpolicy_func_t *secpolicy)
6f1ffb06
MA
6307{
6308 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6309 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6310}
6311
6312static void
6313zfs_ioctl_init(void)
6314{
6315 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6316 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
b83a0e2d
DB
6317 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6318 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6f1ffb06
MA
6319
6320 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6321 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
b83a0e2d
DB
6322 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6323 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6f1ffb06
MA
6324
6325 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6326 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
6327 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6328 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6f1ffb06
MA
6329
6330 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6331 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
b83a0e2d
DB
6332 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6333 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6f1ffb06
MA
6334
6335 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6336 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
6337 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6338 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6f1ffb06
MA
6339
6340 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6341 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
b83a0e2d
DB
6342 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6343 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6f1ffb06
MA
6344
6345 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6346 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
b83a0e2d
DB
6347 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6348 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6f1ffb06 6349
a1d477c2
MA
6350 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6351 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
b83a0e2d
DB
6352 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6353 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
a1d477c2 6354
6f1ffb06
MA
6355 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6356 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
b83a0e2d
DB
6357 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6358 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6f1ffb06 6359
13fe0198
MA
6360 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6361 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
b83a0e2d
DB
6362 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6363 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
13fe0198
MA
6364 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6365 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
b83a0e2d
DB
6366 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6367 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
13fe0198
MA
6368
6369 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6370 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
6371 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6372 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
13fe0198 6373
46ba1e59
MA
6374 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6375 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
b83a0e2d
DB
6376 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6377 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
46ba1e59 6378
da536844
MA
6379 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6380 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
b83a0e2d
DB
6381 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6382 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
da536844
MA
6383
6384 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6385 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
6386 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6387 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
da536844
MA
6388
6389 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6390 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6391 POOL_NAME,
b83a0e2d
DB
6392 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6393 zfs_keys_destroy_bookmarks,
6394 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
da536844 6395
43e52edd
BB
6396 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6397 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
b83a0e2d
DB
6398 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6399 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
b5256303
TC
6400 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6401 zfs_ioc_load_key, zfs_secpolicy_load_key,
b83a0e2d
DB
6402 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6403 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
b5256303
TC
6404 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6405 zfs_ioc_unload_key, zfs_secpolicy_load_key,
b83a0e2d
DB
6406 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6407 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
b5256303
TC
6408 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6409 zfs_ioc_change_key, zfs_secpolicy_change_key,
6410 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
b83a0e2d
DB
6411 B_TRUE, B_TRUE, zfs_keys_change_key,
6412 ARRAY_SIZE(zfs_keys_change_key));
43e52edd 6413
bec1067d
AP
6414 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6415 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
b83a0e2d
DB
6416 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6417 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
d3f2cd7e
AB
6418 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6419 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
b83a0e2d 6420 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
bec1067d 6421
d99a0153
CW
6422 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6423 zfs_ioc_channel_program, zfs_secpolicy_config,
6424 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
b83a0e2d
DB
6425 B_TRUE, zfs_keys_channel_program,
6426 ARRAY_SIZE(zfs_keys_channel_program));
d99a0153 6427
d2734cce
SD
6428 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6429 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
b83a0e2d
DB
6430 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6431 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
d2734cce
SD
6432
6433 zfs_ioctl_register("zpool_discard_checkpoint",
6434 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6435 zfs_secpolicy_config, POOL_NAME,
b83a0e2d
DB
6436 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6437 zfs_keys_pool_discard_checkpoint,
6438 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
d2734cce 6439
6f1ffb06
MA
6440 /* IOCTLS that use the legacy function signature */
6441
6442 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6443 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6444
6445 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6446 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6447 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6448 zfs_ioc_pool_scan);
6449 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6450 zfs_ioc_pool_upgrade);
6451 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6452 zfs_ioc_vdev_add);
6453 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6454 zfs_ioc_vdev_remove);
6455 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6456 zfs_ioc_vdev_set_state);
6457 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6458 zfs_ioc_vdev_attach);
6459 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6460 zfs_ioc_vdev_detach);
6461 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6462 zfs_ioc_vdev_setpath);
6463 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6464 zfs_ioc_vdev_setfru);
6465 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6466 zfs_ioc_pool_set_props);
6467 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6468 zfs_ioc_vdev_split);
6469 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6470 zfs_ioc_pool_reguid);
6471
6472 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6473 zfs_ioc_pool_configs, zfs_secpolicy_none);
6474 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6475 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6476 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6477 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6478 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6479 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6480 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6481 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6482
6483 /*
6484 * pool destroy, and export don't log the history as part of
6485 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6486 * does the logging of those commands.
6487 */
6488 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
87a63dd7 6489 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06 6490 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
87a63dd7 6491 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06
MA
6492
6493 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6494 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6495 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6496 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6497
6498 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6499 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6500 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6501 zfs_ioc_dsobj_to_dsname,
6502 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6503 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6504 zfs_ioc_pool_get_history,
6505 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6506
6507 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6508 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6509
6510 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
92e43c17 6511 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6f1ffb06
MA
6512
6513 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6514 zfs_ioc_space_written);
6f1ffb06
MA
6515 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6516 zfs_ioc_objset_recvd_props);
6517 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6518 zfs_ioc_next_obj);
6519 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6520 zfs_ioc_get_fsacl);
6521 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6522 zfs_ioc_objset_stats);
6523 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6524 zfs_ioc_objset_zplprops);
6525 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6526 zfs_ioc_dataset_list_next);
6527 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6528 zfs_ioc_snapshot_list_next);
6529 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6530 zfs_ioc_send_progress);
6531
6532 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6533 zfs_ioc_diff, zfs_secpolicy_diff);
6534 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6535 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6536 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6537 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6538 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6539 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6540 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6541 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6542 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6543 zfs_ioc_send, zfs_secpolicy_send);
6544
6545 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6546 zfs_secpolicy_none);
6547 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6548 zfs_secpolicy_destroy);
6f1ffb06
MA
6549 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6550 zfs_secpolicy_rename);
6551 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6552 zfs_secpolicy_recv);
6553 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6554 zfs_secpolicy_promote);
6f1ffb06
MA
6555 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6556 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6557 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6558 zfs_secpolicy_set_fsacl);
6559
6560 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6561 zfs_secpolicy_share, POOL_CHECK_NONE);
6562 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6563 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6564 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6565 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6566 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6567 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6568 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6569 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6570
6571 /*
ba6a2402 6572 * ZoL functions
6f1ffb06 6573 */
6f1ffb06
MA
6574 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
6575 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6576 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
6577 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
75e3ff58
BB
6578 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
6579 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6f1ffb06 6580}
34dc7c2f 6581
b83a0e2d
DB
6582/*
6583 * Verify that for non-legacy ioctls the input nvlist
6584 * pairs match against the expected input.
6585 *
6586 * Possible errors are:
6587 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
6588 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
6589 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
6590 */
6591static int
6592zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
6593{
6594 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
6595 boolean_t required_keys_found = B_FALSE;
6596
6597 /*
6598 * examine each input pair
6599 */
6600 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
6601 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
6602 char *name = nvpair_name(pair);
6603 data_type_t type = nvpair_type(pair);
6604 boolean_t identified = B_FALSE;
6605
6606 /*
6607 * check pair against the documented names and type
6608 */
6609 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6610 /* if not a wild card name, check for an exact match */
6611 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
6612 strcmp(nvl_keys[k].zkey_name, name) != 0)
6613 continue;
6614
6615 identified = B_TRUE;
6616
6617 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
6618 nvl_keys[k].zkey_type != type) {
6619 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
6620 }
6621
6622 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6623 continue;
6624
6625 required_keys_found = B_TRUE;
6626 break;
6627 }
6628
6629 /* allow an 'optional' key, everything else is invalid */
6630 if (!identified &&
6631 (strcmp(name, "optional") != 0 ||
6632 type != DATA_TYPE_NVLIST)) {
6633 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
6634 }
6635 }
6636
6637 /* verify that all required keys were found */
6638 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6639 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6640 continue;
6641
6642 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
6643 /* at least one non-optionial key is expected here */
6644 if (!required_keys_found)
6645 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6646 continue;
6647 }
6648
6649 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
6650 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6651 }
6652
6653 return (0);
6654}
6655
9babb374 6656int
572e2857
BB
6657pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6658 zfs_ioc_poolcheck_t check)
9babb374
BB
6659{
6660 spa_t *spa;
6661 int error;
6662
6663 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6664
572e2857
BB
6665 if (check & POOL_CHECK_NONE)
6666 return (0);
6667
9babb374
BB
6668 error = spa_open(name, &spa, FTAG);
6669 if (error == 0) {
572e2857 6670 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
2e528b49 6671 error = SET_ERROR(EAGAIN);
572e2857 6672 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
2e528b49 6673 error = SET_ERROR(EROFS);
9babb374
BB
6674 spa_close(spa, FTAG);
6675 }
6676 return (error);
6677}
6678
325f0235
BB
6679static void *
6680zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
6681{
6682 zfsdev_state_t *zs;
6683
3937ab20 6684 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
325f0235 6685 if (zs->zs_minor == minor) {
3937ab20 6686 smp_rmb();
325f0235 6687 switch (which) {
d1d7e268
MK
6688 case ZST_ONEXIT:
6689 return (zs->zs_onexit);
6690 case ZST_ZEVENT:
6691 return (zs->zs_zevent);
6692 case ZST_ALL:
6693 return (zs);
325f0235
BB
6694 }
6695 }
6696 }
6697
d1d7e268 6698 return (NULL);
325f0235
BB
6699}
6700
6701void *
6702zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
6703{
6704 void *ptr;
6705
325f0235 6706 ptr = zfsdev_get_state_impl(minor, which);
325f0235 6707
d1d7e268 6708 return (ptr);
325f0235
BB
6709}
6710
72540ea3
RY
6711int
6712zfsdev_getminor(struct file *filp, minor_t *minorp)
325f0235 6713{
72540ea3
RY
6714 zfsdev_state_t *zs, *fpd;
6715
325f0235 6716 ASSERT(filp != NULL);
72540ea3
RY
6717 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
6718
6719 fpd = filp->private_data;
6720 if (fpd == NULL)
ecb2b7dc 6721 return (SET_ERROR(EBADF));
72540ea3
RY
6722
6723 mutex_enter(&zfsdev_state_lock);
6724
6725 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6726
6727 if (zs->zs_minor == -1)
6728 continue;
6729
6730 if (fpd == zs) {
6731 *minorp = fpd->zs_minor;
6732 mutex_exit(&zfsdev_state_lock);
6733 return (0);
6734 }
6735 }
6736
6737 mutex_exit(&zfsdev_state_lock);
325f0235 6738
ecb2b7dc 6739 return (SET_ERROR(EBADF));
325f0235
BB
6740}
6741
572e2857 6742/*
325f0235
BB
6743 * Find a free minor number. The zfsdev_state_list is expected to
6744 * be short since it is only a list of currently open file handles.
572e2857
BB
6745 */
6746minor_t
6747zfsdev_minor_alloc(void)
6748{
325f0235 6749 static minor_t last_minor = 0;
572e2857
BB
6750 minor_t m;
6751
6752 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6753
6754 for (m = last_minor + 1; m != last_minor; m++) {
6755 if (m > ZFSDEV_MAX_MINOR)
6756 m = 1;
325f0235 6757 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
572e2857
BB
6758 last_minor = m;
6759 return (m);
6760 }
6761 }
6762
6763 return (0);
6764}
6765
6766static int
325f0235 6767zfsdev_state_init(struct file *filp)
572e2857 6768{
3937ab20 6769 zfsdev_state_t *zs, *zsprev = NULL;
572e2857 6770 minor_t minor;
3937ab20 6771 boolean_t newzs = B_FALSE;
572e2857
BB
6772
6773 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
572e2857 6774
d1d7e268
MK
6775 minor = zfsdev_minor_alloc();
6776 if (minor == 0)
6777 return (SET_ERROR(ENXIO));
325f0235 6778
3937ab20
TC
6779 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6780 if (zs->zs_minor == -1)
6781 break;
6782 zsprev = zs;
6783 }
6784
6785 if (!zs) {
6786 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6787 newzs = B_TRUE;
6788 }
572e2857 6789
325f0235 6790 zs->zs_file = filp;
325f0235 6791 filp->private_data = zs;
572e2857 6792
325f0235
BB
6793 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
6794 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
572e2857 6795
3937ab20
TC
6796
6797 /*
6798 * In order to provide for lock-free concurrent read access
6799 * to the minor list in zfsdev_get_state_impl(), new entries
6800 * must be completely written before linking them into the
6801 * list whereas existing entries are already linked; the last
6802 * operation must be updating zs_minor (from -1 to the new
6803 * value).
6804 */
6805 if (newzs) {
6806 zs->zs_minor = minor;
6807 smp_wmb();
6808 zsprev->zs_next = zs;
6809 } else {
6810 smp_wmb();
6811 zs->zs_minor = minor;
6812 }
572e2857
BB
6813
6814 return (0);
6815}
6816
325f0235
BB
6817static int
6818zfsdev_state_destroy(struct file *filp)
572e2857 6819{
325f0235 6820 zfsdev_state_t *zs;
572e2857 6821
325f0235
BB
6822 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6823 ASSERT(filp->private_data != NULL);
572e2857 6824
325f0235 6825 zs = filp->private_data;
3937ab20 6826 zs->zs_minor = -1;
325f0235
BB
6827 zfs_onexit_destroy(zs->zs_onexit);
6828 zfs_zevent_destroy(zs->zs_zevent);
572e2857 6829
d1d7e268 6830 return (0);
572e2857
BB
6831}
6832
6833static int
325f0235 6834zfsdev_open(struct inode *ino, struct file *filp)
572e2857 6835{
325f0235 6836 int error;
572e2857 6837
325f0235
BB
6838 mutex_enter(&zfsdev_state_lock);
6839 error = zfsdev_state_init(filp);
6840 mutex_exit(&zfsdev_state_lock);
572e2857 6841
325f0235 6842 return (-error);
572e2857
BB
6843}
6844
6845static int
325f0235 6846zfsdev_release(struct inode *ino, struct file *filp)
572e2857 6847{
325f0235 6848 int error;
572e2857
BB
6849
6850 mutex_enter(&zfsdev_state_lock);
325f0235 6851 error = zfsdev_state_destroy(filp);
572e2857
BB
6852 mutex_exit(&zfsdev_state_lock);
6853
325f0235 6854 return (-error);
572e2857
BB
6855}
6856
325f0235
BB
6857static long
6858zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f
BB
6859{
6860 zfs_cmd_t *zc;
6f1ffb06 6861 uint_t vecnum;
4fd762f8 6862 int error, rc, flag = 0;
6f1ffb06 6863 const zfs_ioc_vec_t *vec;
fb8e608d 6864 char *saved_poolname = NULL;
6f1ffb06 6865 nvlist_t *innvl = NULL;
40d06e3c 6866 fstrans_cookie_t cookie;
6f1ffb06
MA
6867
6868 vecnum = cmd - ZFS_IOC_FIRST;
6869 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
b83a0e2d 6870 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
6f1ffb06 6871 vec = &zfs_ioc_vec[vecnum];
34dc7c2f 6872
2e0358cb
BB
6873 /*
6874 * The registered ioctl list may be sparse, verify that either
6875 * a normal or legacy handler are registered.
6876 */
6877 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
b83a0e2d 6878 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
2e0358cb 6879
efcd79a8 6880 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
34dc7c2f 6881
9babb374 6882 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6f1ffb06 6883 if (error != 0) {
2e528b49 6884 error = SET_ERROR(EFAULT);
6f1ffb06
MA
6885 goto out;
6886 }
34dc7c2f 6887
6f1ffb06 6888 zc->zc_iflags = flag & FKIOCTL;
f74b821a
BB
6889 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
6890 /*
6891 * Make sure the user doesn't pass in an insane value for
6892 * zc_nvlist_src_size. We have to check, since we will end
6893 * up allocating that much memory inside of get_nvlist(). This
6894 * prevents a nefarious user from allocating tons of kernel
6895 * memory.
6896 *
6897 * Also, we return EINVAL instead of ENOMEM here. The reason
6898 * being that returning ENOMEM from an ioctl() has a special
6899 * connotation; that the user's size value is too small and
6900 * needs to be expanded to hold the nvlist. See
6901 * zcmd_expand_dst_nvlist() for details.
6902 */
6903 error = SET_ERROR(EINVAL); /* User's size too big */
6904
6905 } else if (zc->zc_nvlist_src_size != 0) {
6f1ffb06
MA
6906 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6907 zc->zc_iflags, &innvl);
6908 if (error != 0)
6909 goto out;
6910 }
34dc7c2f
BB
6911
6912 /*
6913 * Ensure that all pool/dataset names are valid before we pass down to
6914 * the lower layers.
6915 */
6f1ffb06
MA
6916 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6917 switch (vec->zvec_namecheck) {
6918 case POOL_NAME:
6919 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 6920 error = SET_ERROR(EINVAL);
6f1ffb06 6921 else
572e2857 6922 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
6923 vec->zvec_namecheck, vec->zvec_pool_check);
6924 break;
34dc7c2f 6925
6f1ffb06
MA
6926 case DATASET_NAME:
6927 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 6928 error = SET_ERROR(EINVAL);
6f1ffb06 6929 else
572e2857 6930 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
6931 vec->zvec_namecheck, vec->zvec_pool_check);
6932 break;
34dc7c2f 6933
6f1ffb06
MA
6934 case NO_NAME:
6935 break;
34dc7c2f
BB
6936 }
6937
b83a0e2d
DB
6938 /*
6939 * Ensure that all input pairs are valid before we pass them down
6940 * to the lower layers.
6941 *
6942 * The vectored functions can use fnvlist_lookup_{type} for any
6943 * required pairs since zfs_check_input_nvpairs() confirmed that
6944 * they exist and are of the correct type.
6945 */
6946 if (error == 0 && vec->zvec_func != NULL) {
6947 error = zfs_check_input_nvpairs(innvl, vec);
6948 if (error != 0)
6949 goto out;
6950 }
34dc7c2f 6951
005e27e3 6952 if (error == 0) {
ddab862d 6953 cookie = spl_fstrans_mark();
6f1ffb06 6954 error = vec->zvec_secpolicy(zc, innvl, CRED());
ddab862d
TC
6955 spl_fstrans_unmark(cookie);
6956 }
6f1ffb06
MA
6957
6958 if (error != 0)
6959 goto out;
6960
6961 /* legacy ioctls can modify zc_name */
4fd762f8
BB
6962 saved_poolname = strdup(zc->zc_name);
6963 if (saved_poolname == NULL) {
6964 error = SET_ERROR(ENOMEM);
6965 goto out;
6966 } else {
6967 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
6968 }
6f1ffb06
MA
6969
6970 if (vec->zvec_func != NULL) {
6971 nvlist_t *outnvl;
6972 int puterror = 0;
6973 spa_t *spa;
6974 nvlist_t *lognv = NULL;
6975
6976 ASSERT(vec->zvec_legacy_func == NULL);
6977
6978 /*
6979 * Add the innvl to the lognv before calling the func,
6980 * in case the func changes the innvl.
6981 */
6982 if (vec->zvec_allow_log) {
6983 lognv = fnvlist_alloc();
6984 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6985 vec->zvec_name);
6986 if (!nvlist_empty(innvl)) {
6987 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6988 innvl);
6989 }
6990 }
6991
79c76d5b 6992 outnvl = fnvlist_alloc();
40d06e3c 6993 cookie = spl_fstrans_mark();
6f1ffb06 6994 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
40d06e3c 6995 spl_fstrans_unmark(cookie);
6f1ffb06 6996
d99a0153
CW
6997 /*
6998 * Some commands can partially execute, modify state, and still
6999 * return an error. In these cases, attempt to record what
7000 * was modified.
7001 */
7002 if ((error == 0 ||
7003 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7004 vec->zvec_allow_log &&
6f1ffb06
MA
7005 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7006 if (!nvlist_empty(outnvl)) {
7007 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7008 outnvl);
7009 }
d99a0153
CW
7010 if (error != 0) {
7011 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7012 error);
7013 }
6f1ffb06
MA
7014 (void) spa_history_log_nvl(spa, lognv);
7015 spa_close(spa, FTAG);
7016 }
7017 fnvlist_free(lognv);
7018
7019 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7020 int smusherror = 0;
7021 if (vec->zvec_smush_outnvlist) {
7022 smusherror = nvlist_smush(outnvl,
7023 zc->zc_nvlist_dst_size);
7024 }
7025 if (smusherror == 0)
7026 puterror = put_nvlist(zc, outnvl);
7027 }
7028
7029 if (puterror != 0)
7030 error = puterror;
7031
7032 nvlist_free(outnvl);
7033 } else {
40d06e3c 7034 cookie = spl_fstrans_mark();
6f1ffb06 7035 error = vec->zvec_legacy_func(zc);
40d06e3c 7036 spl_fstrans_unmark(cookie);
6f1ffb06
MA
7037 }
7038
7039out:
7040 nvlist_free(innvl);
9babb374 7041 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6f1ffb06 7042 if (error == 0 && rc != 0)
2e528b49 7043 error = SET_ERROR(EFAULT);
6f1ffb06
MA
7044 if (error == 0 && vec->zvec_allow_log) {
7045 char *s = tsd_get(zfs_allow_log_key);
7046 if (s != NULL)
7047 strfree(s);
fb8e608d
TC
7048 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7049 } else {
7050 if (saved_poolname != NULL)
4fd762f8 7051 strfree(saved_poolname);
34dc7c2f
BB
7052 }
7053
7054 kmem_free(zc, sizeof (zfs_cmd_t));
325f0235 7055 return (-error);
34dc7c2f
BB
7056}
7057
325f0235
BB
7058#ifdef CONFIG_COMPAT
7059static long
7060zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f 7061{
d1d7e268 7062 return (zfsdev_ioctl(filp, cmd, arg));
325f0235
BB
7063}
7064#else
d1d7e268 7065#define zfsdev_compat_ioctl NULL
325f0235 7066#endif
34dc7c2f 7067
325f0235 7068static const struct file_operations zfsdev_fops = {
d1d7e268
MK
7069 .open = zfsdev_open,
7070 .release = zfsdev_release,
7071 .unlocked_ioctl = zfsdev_ioctl,
7072 .compat_ioctl = zfsdev_compat_ioctl,
7073 .owner = THIS_MODULE,
325f0235 7074};
34dc7c2f 7075
325f0235 7076static struct miscdevice zfs_misc = {
fa61e723 7077 .minor = ZFS_DEVICE_MINOR,
d1d7e268
MK
7078 .name = ZFS_DRIVER,
7079 .fops = &zfsdev_fops,
325f0235 7080};
34dc7c2f 7081
fa61e723 7082MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR);
a6cc9756
BB
7083MODULE_ALIAS("devname:zfs");
7084
34dc7c2f 7085static int
325f0235 7086zfs_attach(void)
34dc7c2f 7087{
325f0235 7088 int error;
34dc7c2f 7089
325f0235 7090 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
3937ab20
TC
7091 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7092 zfsdev_state_list->zs_minor = -1;
34dc7c2f 7093
325f0235 7094 error = misc_register(&zfs_misc);
a6cc9756
BB
7095 if (error == -EBUSY) {
7096 /*
7097 * Fallback to dynamic minor allocation in the event of a
7098 * collision with a reserved minor in linux/miscdevice.h.
7099 * In this case the kernel modules must be manually loaded.
7100 */
7101 printk(KERN_INFO "ZFS: misc_register() with static minor %d "
7102 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
fa61e723 7103 ZFS_DEVICE_MINOR, error);
a6cc9756
BB
7104
7105 zfs_misc.minor = MISC_DYNAMIC_MINOR;
7106 error = misc_register(&zfs_misc);
325f0235 7107 }
34dc7c2f 7108
a6cc9756
BB
7109 if (error)
7110 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
7111
7112 return (error);
34dc7c2f
BB
7113}
7114
325f0235
BB
7115static void
7116zfs_detach(void)
34dc7c2f 7117{
3937ab20 7118 zfsdev_state_t *zs, *zsprev = NULL;
34dc7c2f 7119
324dcd37 7120 misc_deregister(&zfs_misc);
325f0235 7121 mutex_destroy(&zfsdev_state_lock);
3937ab20
TC
7122
7123 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7124 if (zsprev)
7125 kmem_free(zsprev, sizeof (zfsdev_state_t));
7126 zsprev = zs;
7127 }
7128 if (zsprev)
7129 kmem_free(zsprev, sizeof (zfsdev_state_t));
34dc7c2f
BB
7130}
7131
6f1ffb06
MA
7132static void
7133zfs_allow_log_destroy(void *arg)
7134{
7135 char *poolname = arg;
9f3d1407 7136
7137 if (poolname != NULL)
7138 strfree(poolname);
6f1ffb06 7139}
325f0235
BB
7140
7141#ifdef DEBUG
d1d7e268 7142#define ZFS_DEBUG_STR " (DEBUG mode)"
325f0235 7143#else
d1d7e268 7144#define ZFS_DEBUG_STR ""
325f0235 7145#endif
34dc7c2f 7146
b4f3666a 7147static int __init
34dc7c2f
BB
7148_init(void)
7149{
7150 int error;
7151
3d8d245f 7152 error = -vn_set_pwd("/");
b4f3666a
BB
7153 if (error) {
7154 printk(KERN_NOTICE
7155 "ZFS: Warning unable to set pwd to '/': %d\n", error);
7156 return (error);
7157 }
7158
a0bd735a
BP
7159 if ((error = -zvol_init()) != 0)
7160 return (error);
7161
34dc7c2f
BB
7162 spa_init(FREAD | FWRITE);
7163 zfs_init();
34dc7c2f 7164
6f1ffb06 7165 zfs_ioctl_init();
e8bcb693 7166 zfs_sysfs_init();
6f1ffb06 7167
325f0235 7168 if ((error = zfs_attach()) != 0)
a0bd735a 7169 goto out;
34dc7c2f 7170
d5446cfc 7171 tsd_create(&zfs_fsyncer_key, NULL);
6f1ffb06
MA
7172 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7173 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
34dc7c2f 7174
4b5d425f 7175 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
d1d7e268
MK
7176 "ZFS pool version %s, ZFS filesystem version %s\n",
7177 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
7178 SPA_VERSION_STRING, ZPL_VERSION_STRING);
b695c34e
MM
7179#ifndef CONFIG_FS_POSIX_ACL
7180 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
7181#endif /* CONFIG_FS_POSIX_ACL */
34dc7c2f
BB
7182
7183 return (0);
325f0235 7184
a0bd735a 7185out:
e8bcb693 7186 zfs_sysfs_fini();
325f0235
BB
7187 zfs_fini();
7188 spa_fini();
a0bd735a 7189 (void) zvol_fini();
4b5d425f 7190 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
d1d7e268
MK
7191 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
7192 ZFS_DEBUG_STR, error);
325f0235
BB
7193
7194 return (error);
34dc7c2f
BB
7195}
7196
b4f3666a 7197static void __exit
34dc7c2f
BB
7198_fini(void)
7199{
325f0235 7200 zfs_detach();
e8bcb693 7201 zfs_sysfs_fini();
34dc7c2f
BB
7202 zfs_fini();
7203 spa_fini();
a0bd735a 7204 zvol_fini();
46e18b3f 7205
d5446cfc 7206 tsd_destroy(&zfs_fsyncer_key);
3fc050aa 7207 tsd_destroy(&rrw_tsd_key);
6f1ffb06 7208 tsd_destroy(&zfs_allow_log_key);
34dc7c2f 7209
4b5d425f 7210 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
d1d7e268 7211 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
34dc7c2f 7212}
325f0235 7213
93ce2b4c 7214#if defined(_KERNEL)
b4f3666a
BB
7215module_init(_init);
7216module_exit(_fini);
325f0235
BB
7217
7218MODULE_DESCRIPTION("ZFS");
7219MODULE_AUTHOR(ZFS_META_AUTHOR);
7220MODULE_LICENSE(ZFS_META_LICENSE);
99e349db 7221MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
93ce2b4c 7222#endif