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