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