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