]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_ioctl.c
Native Encryption for ZFS on Linux
[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.
6f1ffb06
MA
39 */
40
41/*
42 * ZFS ioctls.
43 *
44 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
45 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
46 *
47 * There are two ways that we handle ioctls: the legacy way where almost
48 * all of the logic is in the ioctl callback, and the new way where most
49 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
50 *
51 * Non-legacy ioctls should be registered by calling
52 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
53 * from userland by lzc_ioctl().
54 *
55 * The registration arguments are as follows:
56 *
57 * const char *name
58 * The name of the ioctl. This is used for history logging. If the
59 * ioctl returns successfully (the callback returns 0), and allow_log
60 * is true, then a history log entry will be recorded with the input &
61 * output nvlists. The log entry can be printed with "zpool history -i".
62 *
63 * zfs_ioc_t ioc
64 * The ioctl request number, which userland will pass to ioctl(2).
65 * The ioctl numbers can change from release to release, because
66 * the caller (libzfs) must be matched to the kernel.
67 *
68 * zfs_secpolicy_func_t *secpolicy
69 * This function will be called before the zfs_ioc_func_t, to
70 * determine if this operation is permitted. It should return EPERM
71 * on failure, and 0 on success. Checks include determining if the
72 * dataset is visible in this zone, and if the user has either all
73 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
74 * to do this operation on this dataset with "zfs allow".
75 *
76 * zfs_ioc_namecheck_t namecheck
77 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
78 * name, a dataset name, or nothing. If the name is not well-formed,
79 * the ioctl will fail and the callback will not be called.
80 * Therefore, the callback can assume that the name is well-formed
81 * (e.g. is null-terminated, doesn't have more than one '@' character,
82 * doesn't have invalid characters).
83 *
84 * zfs_ioc_poolcheck_t pool_check
85 * This specifies requirements on the pool state. If the pool does
86 * not meet them (is suspended or is readonly), the ioctl will fail
87 * and the callback will not be called. If any checks are specified
88 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
89 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
90 * POOL_CHECK_READONLY).
91 *
92 * boolean_t smush_outnvlist
93 * If smush_outnvlist is true, then the output is presumed to be a
94 * list of errors, and it will be "smushed" down to fit into the
95 * caller's buffer, by removing some entries and replacing them with a
96 * single "N_MORE_ERRORS" entry indicating how many were removed. See
97 * nvlist_smush() for details. If smush_outnvlist is false, and the
98 * outnvlist does not fit into the userland-provided buffer, then the
99 * ioctl will fail with ENOMEM.
100 *
101 * zfs_ioc_func_t *func
102 * The callback function that will perform the operation.
103 *
104 * The callback should return 0 on success, or an error number on
105 * failure. If the function fails, the userland ioctl will return -1,
106 * and errno will be set to the callback's return value. The callback
107 * will be called with the following arguments:
108 *
109 * const char *name
110 * The name of the pool or dataset to operate on, from
111 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
112 * expected type (pool, dataset, or none).
113 *
114 * nvlist_t *innvl
115 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
116 * NULL if no input nvlist was provided. Changes to this nvlist are
117 * ignored. If the input nvlist could not be deserialized, the
118 * ioctl will fail and the callback will not be called.
119 *
120 * nvlist_t *outnvl
121 * The output nvlist, initially empty. The callback can fill it in,
122 * and it will be returned to userland by serializing it into
123 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
124 * fails (e.g. because the caller didn't supply a large enough
125 * buffer), then the overall ioctl will fail. See the
126 * 'smush_nvlist' argument above for additional behaviors.
127 *
128 * There are two typical uses of the output nvlist:
129 * - To return state, e.g. property values. In this case,
130 * smush_outnvlist should be false. If the buffer was not large
131 * enough, the caller will reallocate a larger buffer and try
132 * the ioctl again.
133 *
134 * - To return multiple errors from an ioctl which makes on-disk
135 * changes. In this case, smush_outnvlist should be true.
136 * Ioctls which make on-disk modifications should generally not
137 * use the outnvl if they succeed, because the caller can not
138 * distinguish between the operation failing, and
139 * deserialization failing.
3541dc6d 140 */
34dc7c2f 141
34dc7c2f
BB
142#include <sys/types.h>
143#include <sys/param.h>
144#include <sys/errno.h>
145#include <sys/uio.h>
146#include <sys/buf.h>
147#include <sys/modctl.h>
148#include <sys/open.h>
149#include <sys/file.h>
150#include <sys/kmem.h>
151#include <sys/conf.h>
152#include <sys/cmn_err.h>
153#include <sys/stat.h>
154#include <sys/zfs_ioctl.h>
428870ff 155#include <sys/zfs_vfsops.h>
34dc7c2f
BB
156#include <sys/zfs_znode.h>
157#include <sys/zap.h>
158#include <sys/spa.h>
159#include <sys/spa_impl.h>
160#include <sys/vdev.h>
4a283c7f 161#include <sys/vdev_impl.h>
428870ff 162#include <sys/priv_impl.h>
34dc7c2f
BB
163#include <sys/dmu.h>
164#include <sys/dsl_dir.h>
165#include <sys/dsl_dataset.h>
166#include <sys/dsl_prop.h>
167#include <sys/dsl_deleg.h>
168#include <sys/dmu_objset.h>
37abac6d 169#include <sys/dmu_impl.h>
13fe0198 170#include <sys/dmu_tx.h>
34dc7c2f
BB
171#include <sys/ddi.h>
172#include <sys/sunddi.h>
173#include <sys/sunldi.h>
174#include <sys/policy.h>
175#include <sys/zone.h>
176#include <sys/nvpair.h>
177#include <sys/pathname.h>
178#include <sys/mount.h>
179#include <sys/sdt.h>
180#include <sys/fs/zfs.h>
ebe7e575 181#include <sys/zfs_ctldir.h>
34dc7c2f 182#include <sys/zfs_dir.h>
572e2857 183#include <sys/zfs_onexit.h>
34dc7c2f 184#include <sys/zvol.h>
428870ff 185#include <sys/dsl_scan.h>
34dc7c2f 186#include <sharefs/share.h>
325f0235 187#include <sys/fm/util.h>
b5256303 188#include <sys/dsl_crypt.h>
325f0235 189
13fe0198
MA
190#include <sys/dmu_send.h>
191#include <sys/dsl_destroy.h>
da536844 192#include <sys/dsl_bookmark.h>
13fe0198 193#include <sys/dsl_userhold.h>
9759c60f 194#include <sys/zfeature.h>
3c67d83a 195#include <sys/zio_checksum.h>
9759c60f 196
325f0235 197#include <linux/miscdevice.h>
f74b821a 198#include <linux/slab.h>
34dc7c2f
BB
199
200#include "zfs_namecheck.h"
201#include "zfs_prop.h"
202#include "zfs_deleg.h"
428870ff 203#include "zfs_comutil.h"
34dc7c2f 204
f74b821a
BB
205/*
206 * Limit maximum nvlist size. We don't want users passing in insane values
207 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
208 */
209#define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
210
325f0235 211kmutex_t zfsdev_state_lock;
3937ab20 212zfsdev_state_t *zfsdev_state_list;
34dc7c2f
BB
213
214extern void zfs_init(void);
215extern void zfs_fini(void);
216
6f1ffb06
MA
217uint_t zfs_fsyncer_key;
218extern uint_t rrw_tsd_key;
219static uint_t zfs_allow_log_key;
220
221typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
222typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
223typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
34dc7c2f 224
9babb374
BB
225typedef enum {
226 NO_NAME,
227 POOL_NAME,
228 DATASET_NAME
229} zfs_ioc_namecheck_t;
230
572e2857
BB
231typedef enum {
232 POOL_CHECK_NONE = 1 << 0,
233 POOL_CHECK_SUSPENDED = 1 << 1,
6f1ffb06 234 POOL_CHECK_READONLY = 1 << 2,
572e2857
BB
235} zfs_ioc_poolcheck_t;
236
34dc7c2f 237typedef struct zfs_ioc_vec {
6f1ffb06 238 zfs_ioc_legacy_func_t *zvec_legacy_func;
34dc7c2f
BB
239 zfs_ioc_func_t *zvec_func;
240 zfs_secpolicy_func_t *zvec_secpolicy;
9babb374 241 zfs_ioc_namecheck_t zvec_namecheck;
6f1ffb06 242 boolean_t zvec_allow_log;
572e2857 243 zfs_ioc_poolcheck_t zvec_pool_check;
6f1ffb06
MA
244 boolean_t zvec_smush_outnvlist;
245 const char *zvec_name;
34dc7c2f
BB
246} zfs_ioc_vec_t;
247
9babb374
BB
248/* This array is indexed by zfs_userquota_prop_t */
249static const char *userquota_perms[] = {
250 ZFS_DELEG_PERM_USERUSED,
251 ZFS_DELEG_PERM_USERQUOTA,
252 ZFS_DELEG_PERM_GROUPUSED,
253 ZFS_DELEG_PERM_GROUPQUOTA,
1de321e6
JX
254 ZFS_DELEG_PERM_USEROBJUSED,
255 ZFS_DELEG_PERM_USEROBJQUOTA,
256 ZFS_DELEG_PERM_GROUPOBJUSED,
257 ZFS_DELEG_PERM_GROUPOBJQUOTA,
9babb374
BB
258};
259
260static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
1de321e6 261static int zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc);
428870ff
BB
262static int zfs_check_settable(const char *name, nvpair_t *property,
263 cred_t *cr);
264static int zfs_check_clearable(char *dataset, nvlist_t *props,
265 nvlist_t **errors);
b128c09f
BB
266static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
267 boolean_t *);
6f1ffb06
MA
268int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
269static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
b128c09f 270
34dc7c2f
BB
271static void
272history_str_free(char *buf)
273{
274 kmem_free(buf, HIS_MAX_RECORD_LEN);
275}
276
277static char *
278history_str_get(zfs_cmd_t *zc)
279{
280 char *buf;
281
b8864a23 282 if (zc->zc_history == 0)
34dc7c2f
BB
283 return (NULL);
284
efcd79a8 285 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
34dc7c2f
BB
286 if (copyinstr((void *)(uintptr_t)zc->zc_history,
287 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
288 history_str_free(buf);
289 return (NULL);
290 }
291
292 buf[HIS_MAX_RECORD_LEN -1] = '\0';
293
294 return (buf);
295}
296
297/*
b128c09f
BB
298 * Check to see if the named dataset is currently defined as bootable
299 */
300static boolean_t
301zfs_is_bootfs(const char *name)
302{
428870ff 303 objset_t *os;
b128c09f 304
428870ff
BB
305 if (dmu_objset_hold(name, FTAG, &os) == 0) {
306 boolean_t ret;
307 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
308 dmu_objset_rele(os, FTAG);
309 return (ret);
b128c09f 310 }
428870ff 311 return (B_FALSE);
b128c09f
BB
312}
313
314/*
d3cc8b15 315 * Return non-zero if the spa version is less than requested version.
34dc7c2f
BB
316 */
317static int
b128c09f 318zfs_earlier_version(const char *name, int version)
34dc7c2f 319{
34dc7c2f
BB
320 spa_t *spa;
321
322 if (spa_open(name, &spa, FTAG) == 0) {
323 if (spa_version(spa) < version) {
324 spa_close(spa, FTAG);
325 return (1);
326 }
327 spa_close(spa, FTAG);
328 }
329 return (0);
330}
331
332/*
b128c09f 333 * Return TRUE if the ZPL version is less than requested version.
34dc7c2f 334 */
b128c09f
BB
335static boolean_t
336zpl_earlier_version(const char *name, int version)
34dc7c2f
BB
337{
338 objset_t *os;
b128c09f 339 boolean_t rc = B_TRUE;
34dc7c2f 340
428870ff 341 if (dmu_objset_hold(name, FTAG, &os) == 0) {
b128c09f 342 uint64_t zplversion;
34dc7c2f 343
428870ff
BB
344 if (dmu_objset_type(os) != DMU_OST_ZFS) {
345 dmu_objset_rele(os, FTAG);
346 return (B_TRUE);
347 }
348 /* XXX reading from non-owned objset */
b128c09f
BB
349 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
350 rc = zplversion < version;
428870ff 351 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
352 }
353 return (rc);
354}
355
356static void
357zfs_log_history(zfs_cmd_t *zc)
358{
359 spa_t *spa;
360 char *buf;
361
362 if ((buf = history_str_get(zc)) == NULL)
363 return;
364
365 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
366 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
6f1ffb06 367 (void) spa_history_log(spa, buf);
34dc7c2f
BB
368 spa_close(spa, FTAG);
369 }
370 history_str_free(buf);
371}
372
373/*
374 * Policy for top-level read operations (list pools). Requires no privileges,
375 * and can be used in the local zone, as there is no associated dataset.
376 */
377/* ARGSUSED */
378static int
6f1ffb06 379zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
380{
381 return (0);
382}
383
384/*
385 * Policy for dataset read operations (list children, get statistics). Requires
386 * no privileges, but must be visible in the local zone.
387 */
388/* ARGSUSED */
389static int
6f1ffb06 390zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
391{
392 if (INGLOBALZONE(curproc) ||
393 zone_dataset_visible(zc->zc_name, NULL))
394 return (0);
395
2e528b49 396 return (SET_ERROR(ENOENT));
34dc7c2f
BB
397}
398
399static int
572e2857 400zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
34dc7c2f 401{
34dc7c2f
BB
402 int writable = 1;
403
404 /*
405 * The dataset must be visible by this zone -- check this first
406 * so they don't see EPERM on something they shouldn't know about.
407 */
408 if (!INGLOBALZONE(curproc) &&
409 !zone_dataset_visible(dataset, &writable))
2e528b49 410 return (SET_ERROR(ENOENT));
34dc7c2f 411
34dc7c2f
BB
412 if (INGLOBALZONE(curproc)) {
413 /*
414 * If the fs is zoned, only root can access it from the
415 * global zone.
416 */
417 if (secpolicy_zfs(cr) && zoned)
2e528b49 418 return (SET_ERROR(EPERM));
34dc7c2f
BB
419 } else {
420 /*
421 * If we are in a local zone, the 'zoned' property must be set.
422 */
423 if (!zoned)
2e528b49 424 return (SET_ERROR(EPERM));
34dc7c2f
BB
425
426 /* must be writable by this zone */
427 if (!writable)
2e528b49 428 return (SET_ERROR(EPERM));
34dc7c2f
BB
429 }
430 return (0);
431}
432
572e2857
BB
433static int
434zfs_dozonecheck(const char *dataset, cred_t *cr)
435{
436 uint64_t zoned;
437
438 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
2e528b49 439 return (SET_ERROR(ENOENT));
572e2857
BB
440
441 return (zfs_dozonecheck_impl(dataset, zoned, cr));
442}
443
444static int
445zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
446{
447 uint64_t zoned;
448
13fe0198 449 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
2e528b49 450 return (SET_ERROR(ENOENT));
572e2857
BB
451
452 return (zfs_dozonecheck_impl(dataset, zoned, cr));
453}
454
6f1ffb06 455static int
13fe0198
MA
456zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
457 const char *perm, cred_t *cr)
34dc7c2f
BB
458{
459 int error;
460
330d06f9 461 error = zfs_dozonecheck_ds(name, ds, cr);
34dc7c2f
BB
462 if (error == 0) {
463 error = secpolicy_zfs(cr);
13fe0198 464 if (error != 0)
6f1ffb06 465 error = dsl_deleg_access_impl(ds, perm, cr);
34dc7c2f
BB
466 }
467 return (error);
468}
469
6f1ffb06 470static int
13fe0198 471zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
572e2857
BB
472{
473 int error;
13fe0198
MA
474 dsl_dataset_t *ds;
475 dsl_pool_t *dp;
572e2857 476
e88551d5
GM
477 /*
478 * First do a quick check for root in the global zone, which
479 * is allowed to do all write_perms. This ensures that zfs_ioc_*
480 * will get to handle nonexistent datasets.
481 */
482 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
483 return (0);
484
13fe0198
MA
485 error = dsl_pool_hold(name, FTAG, &dp);
486 if (error != 0)
487 return (error);
488
489 error = dsl_dataset_hold(dp, name, FTAG, &ds);
490 if (error != 0) {
491 dsl_pool_rele(dp, FTAG);
492 return (error);
572e2857 493 }
13fe0198
MA
494
495 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
496
497 dsl_dataset_rele(ds, FTAG);
498 dsl_pool_rele(dp, FTAG);
572e2857
BB
499 return (error);
500}
501
428870ff
BB
502/*
503 * Policy for setting the security label property.
504 *
505 * Returns 0 for success, non-zero for access and other errors.
506 */
34dc7c2f 507static int
428870ff 508zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
34dc7c2f 509{
d2c15e84 510#ifdef HAVE_MLSLABEL
428870ff
BB
511 char ds_hexsl[MAXNAMELEN];
512 bslabel_t ds_sl, new_sl;
513 boolean_t new_default = FALSE;
514 uint64_t zoned;
515 int needed_priv = -1;
516 int error;
517
518 /* First get the existing dataset label. */
519 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
520 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
13fe0198 521 if (error != 0)
2e528b49 522 return (SET_ERROR(EPERM));
428870ff
BB
523
524 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
525 new_default = TRUE;
526
527 /* The label must be translatable */
528 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
2e528b49 529 return (SET_ERROR(EINVAL));
428870ff
BB
530
531 /*
532 * In a non-global zone, disallow attempts to set a label that
533 * doesn't match that of the zone; otherwise no other checks
534 * are needed.
535 */
536 if (!INGLOBALZONE(curproc)) {
537 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
2e528b49 538 return (SET_ERROR(EPERM));
428870ff
BB
539 return (0);
540 }
541
542 /*
543 * For global-zone datasets (i.e., those whose zoned property is
544 * "off", verify that the specified new label is valid for the
545 * global zone.
546 */
547 if (dsl_prop_get_integer(name,
548 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
2e528b49 549 return (SET_ERROR(EPERM));
428870ff
BB
550 if (!zoned) {
551 if (zfs_check_global_label(name, strval) != 0)
2e528b49 552 return (SET_ERROR(EPERM));
428870ff
BB
553 }
554
555 /*
556 * If the existing dataset label is nondefault, check if the
557 * dataset is mounted (label cannot be changed while mounted).
0037b49e 558 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
428870ff
BB
559 * mounted (or isn't a dataset, doesn't exist, ...).
560 */
561 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
562 objset_t *os;
563 static char *setsl_tag = "setsl_tag";
564
565 /*
566 * Try to own the dataset; abort if there is any error,
567 * (e.g., already mounted, in use, or other error).
568 */
b5256303 569 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
428870ff 570 setsl_tag, &os);
13fe0198 571 if (error != 0)
2e528b49 572 return (SET_ERROR(EPERM));
428870ff 573
b5256303 574 dmu_objset_disown(os, B_TRUE, setsl_tag);
428870ff
BB
575
576 if (new_default) {
577 needed_priv = PRIV_FILE_DOWNGRADE_SL;
578 goto out_check;
579 }
580
581 if (hexstr_to_label(strval, &new_sl) != 0)
2e528b49 582 return (SET_ERROR(EPERM));
428870ff
BB
583
584 if (blstrictdom(&ds_sl, &new_sl))
585 needed_priv = PRIV_FILE_DOWNGRADE_SL;
586 else if (blstrictdom(&new_sl, &ds_sl))
587 needed_priv = PRIV_FILE_UPGRADE_SL;
588 } else {
589 /* dataset currently has a default label */
590 if (!new_default)
591 needed_priv = PRIV_FILE_UPGRADE_SL;
592 }
593
594out_check:
595 if (needed_priv != -1)
596 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
597 return (0);
d2c15e84 598#else
ecb2b7dc 599 return (SET_ERROR(ENOTSUP));
d2c15e84 600#endif /* HAVE_MLSLABEL */
428870ff
BB
601}
602
603static int
604zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
605 cred_t *cr)
606{
607 char *strval;
608
34dc7c2f
BB
609 /*
610 * Check permissions for special properties.
611 */
612 switch (prop) {
e75c13c3
BB
613 default:
614 break;
34dc7c2f
BB
615 case ZFS_PROP_ZONED:
616 /*
617 * Disallow setting of 'zoned' from within a local zone.
618 */
619 if (!INGLOBALZONE(curproc))
2e528b49 620 return (SET_ERROR(EPERM));
34dc7c2f
BB
621 break;
622
623 case ZFS_PROP_QUOTA:
788eb90c
JJ
624 case ZFS_PROP_FILESYSTEM_LIMIT:
625 case ZFS_PROP_SNAPSHOT_LIMIT:
34dc7c2f
BB
626 if (!INGLOBALZONE(curproc)) {
627 uint64_t zoned;
eca7b760 628 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
629 /*
630 * Unprivileged users are allowed to modify the
788eb90c 631 * limit on things *under* (ie. contained by)
34dc7c2f
BB
632 * the thing they own.
633 */
428870ff 634 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
34dc7c2f 635 setpoint))
2e528b49 636 return (SET_ERROR(EPERM));
428870ff 637 if (!zoned || strlen(dsname) <= strlen(setpoint))
2e528b49 638 return (SET_ERROR(EPERM));
34dc7c2f
BB
639 }
640 break;
428870ff
BB
641
642 case ZFS_PROP_MLSLABEL:
643 if (!is_system_labeled())
2e528b49 644 return (SET_ERROR(EPERM));
428870ff
BB
645
646 if (nvpair_value_string(propval, &strval) == 0) {
647 int err;
648
649 err = zfs_set_slabel_policy(dsname, strval, CRED());
650 if (err != 0)
651 return (err);
652 }
653 break;
34dc7c2f
BB
654 }
655
428870ff 656 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
34dc7c2f
BB
657}
658
6f1ffb06
MA
659/* ARGSUSED */
660static int
661zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
662{
663 int error;
664
665 error = zfs_dozonecheck(zc->zc_name, cr);
13fe0198 666 if (error != 0)
34dc7c2f
BB
667 return (error);
668
669 /*
670 * permission to set permissions will be evaluated later in
671 * dsl_deleg_can_allow()
672 */
673 return (0);
674}
675
6f1ffb06
MA
676/* ARGSUSED */
677static int
678zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 679{
428870ff
BB
680 return (zfs_secpolicy_write_perms(zc->zc_name,
681 ZFS_DELEG_PERM_ROLLBACK, cr));
34dc7c2f
BB
682}
683
6f1ffb06
MA
684/* ARGSUSED */
685static int
686zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 687{
572e2857
BB
688 dsl_pool_t *dp;
689 dsl_dataset_t *ds;
690 char *cp;
691 int error;
692
693 /*
694 * Generate the current snapshot name from the given objsetid, then
695 * use that name for the secpolicy/zone checks.
696 */
697 cp = strchr(zc->zc_name, '@');
698 if (cp == NULL)
2e528b49 699 return (SET_ERROR(EINVAL));
13fe0198
MA
700 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
701 if (error != 0)
572e2857
BB
702 return (error);
703
572e2857 704 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
13fe0198
MA
705 if (error != 0) {
706 dsl_pool_rele(dp, FTAG);
572e2857 707 return (error);
13fe0198 708 }
572e2857
BB
709
710 dsl_dataset_name(ds, zc->zc_name);
711
712 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
713 ZFS_DELEG_PERM_SEND, cr);
714 dsl_dataset_rele(ds, FTAG);
13fe0198 715 dsl_pool_rele(dp, FTAG);
572e2857
BB
716
717 return (error);
34dc7c2f
BB
718}
719
6f1ffb06
MA
720/* ARGSUSED */
721static int
722zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
723{
724 return (zfs_secpolicy_write_perms(zc->zc_name,
725 ZFS_DELEG_PERM_SEND, cr));
726}
727
3c9609b3 728#ifdef HAVE_SMB_SHARE
6f1ffb06 729/* ARGSUSED */
9babb374 730static int
6f1ffb06 731zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374
BB
732{
733 vnode_t *vp;
734 int error;
735
736 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
737 NO_FOLLOW, NULL, &vp)) != 0)
738 return (error);
739
740 /* Now make sure mntpnt and dataset are ZFS */
741
742 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
743 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
744 zc->zc_name) != 0)) {
745 VN_RELE(vp);
2e528b49 746 return (SET_ERROR(EPERM));
9babb374
BB
747 }
748
749 VN_RELE(vp);
750 return (dsl_deleg_access(zc->zc_name,
751 ZFS_DELEG_PERM_SHARE, cr));
752}
3c9609b3 753#endif /* HAVE_SMB_SHARE */
9babb374 754
34dc7c2f 755int
6f1ffb06 756zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 757{
3c9609b3 758#ifdef HAVE_SMB_SHARE
34dc7c2f 759 if (!INGLOBALZONE(curproc))
2e528b49 760 return (SET_ERROR(EPERM));
34dc7c2f
BB
761
762 if (secpolicy_nfs(cr) == 0) {
763 return (0);
764 } else {
6f1ffb06 765 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
9babb374 766 }
325f0235 767#else
2e528b49 768 return (SET_ERROR(ENOTSUP));
3c9609b3 769#endif /* HAVE_SMB_SHARE */
9babb374 770}
34dc7c2f 771
9babb374 772int
6f1ffb06 773zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 774{
3c9609b3 775#ifdef HAVE_SMB_SHARE
9babb374 776 if (!INGLOBALZONE(curproc))
2e528b49 777 return (SET_ERROR(EPERM));
34dc7c2f 778
9babb374
BB
779 if (secpolicy_smb(cr) == 0) {
780 return (0);
781 } else {
6f1ffb06 782 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
34dc7c2f 783 }
325f0235 784#else
2e528b49 785 return (SET_ERROR(ENOTSUP));
3c9609b3 786#endif /* HAVE_SMB_SHARE */
34dc7c2f
BB
787}
788
789static int
790zfs_get_parent(const char *datasetname, char *parent, int parentsize)
791{
792 char *cp;
793
794 /*
795 * Remove the @bla or /bla from the end of the name to get the parent.
796 */
797 (void) strncpy(parent, datasetname, parentsize);
798 cp = strrchr(parent, '@');
799 if (cp != NULL) {
800 cp[0] = '\0';
801 } else {
802 cp = strrchr(parent, '/');
803 if (cp == NULL)
2e528b49 804 return (SET_ERROR(ENOENT));
34dc7c2f
BB
805 cp[0] = '\0';
806 }
807
808 return (0);
809}
810
811int
812zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
813{
814 int error;
815
816 if ((error = zfs_secpolicy_write_perms(name,
817 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
818 return (error);
819
820 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
821}
822
6f1ffb06 823/* ARGSUSED */
34dc7c2f 824static int
6f1ffb06 825zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
826{
827 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
828}
829
830/*
428870ff 831 * Destroying snapshots with delegated permissions requires
6f1ffb06 832 * descendant mount and destroy permissions.
34dc7c2f 833 */
6f1ffb06 834/* ARGSUSED */
34dc7c2f 835static int
6f1ffb06 836zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 837{
6f1ffb06
MA
838 nvlist_t *snaps;
839 nvpair_t *pair, *nextpair;
840 int error = 0;
428870ff 841
6f1ffb06 842 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
2e528b49 843 return (SET_ERROR(EINVAL));
6f1ffb06
MA
844 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
845 pair = nextpair) {
6f1ffb06 846 nextpair = nvlist_next_nvpair(snaps, pair);
da536844
MA
847 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
848 if (error == ENOENT) {
6f1ffb06
MA
849 /*
850 * Ignore any snapshots that don't exist (we consider
851 * them "already destroyed"). Remove the name from the
852 * nvl here in case the snapshot is created between
853 * now and when we try to destroy it (in which case
854 * we don't want to destroy it since we haven't
855 * checked for permission).
856 */
857 fnvlist_remove_nvpair(snaps, pair);
858 error = 0;
6f1ffb06 859 }
6f1ffb06
MA
860 if (error != 0)
861 break;
862 }
428870ff 863
428870ff 864 return (error);
34dc7c2f
BB
865}
866
867int
868zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
869{
eca7b760 870 char parentname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
871 int error;
872
873 if ((error = zfs_secpolicy_write_perms(from,
874 ZFS_DELEG_PERM_RENAME, cr)) != 0)
875 return (error);
876
877 if ((error = zfs_secpolicy_write_perms(from,
878 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
879 return (error);
880
881 if ((error = zfs_get_parent(to, parentname,
882 sizeof (parentname))) != 0)
883 return (error);
884
885 if ((error = zfs_secpolicy_write_perms(parentname,
886 ZFS_DELEG_PERM_CREATE, cr)) != 0)
887 return (error);
888
889 if ((error = zfs_secpolicy_write_perms(parentname,
890 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
891 return (error);
892
893 return (error);
894}
895
6f1ffb06 896/* ARGSUSED */
34dc7c2f 897static int
6f1ffb06 898zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
899{
900 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
901}
902
6f1ffb06 903/* ARGSUSED */
34dc7c2f 904static int
6f1ffb06 905zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 906{
13fe0198
MA
907 dsl_pool_t *dp;
908 dsl_dataset_t *clone;
34dc7c2f
BB
909 int error;
910
911 error = zfs_secpolicy_write_perms(zc->zc_name,
912 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
913 if (error != 0)
914 return (error);
915
916 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
917 if (error != 0)
34dc7c2f
BB
918 return (error);
919
13fe0198 920 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
34dc7c2f
BB
921
922 if (error == 0) {
eca7b760 923 char parentname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198 924 dsl_dataset_t *origin = NULL;
34dc7c2f 925 dsl_dir_t *dd;
13fe0198 926 dd = clone->ds_dir;
34dc7c2f 927
b128c09f 928 error = dsl_dataset_hold_obj(dd->dd_pool,
d683ddbb 929 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
13fe0198
MA
930 if (error != 0) {
931 dsl_dataset_rele(clone, FTAG);
932 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
933 return (error);
934 }
935
13fe0198 936 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
34dc7c2f
BB
937 ZFS_DELEG_PERM_MOUNT, cr);
938
13fe0198
MA
939 dsl_dataset_name(origin, parentname);
940 if (error == 0) {
941 error = zfs_secpolicy_write_perms_ds(parentname, origin,
34dc7c2f 942 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
943 }
944 dsl_dataset_rele(clone, FTAG);
945 dsl_dataset_rele(origin, FTAG);
34dc7c2f 946 }
13fe0198 947 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
948 return (error);
949}
950
6f1ffb06 951/* ARGSUSED */
34dc7c2f 952static int
6f1ffb06 953zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f
BB
954{
955 int error;
956
957 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
958 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
959 return (error);
960
961 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
962 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
963 return (error);
964
965 return (zfs_secpolicy_write_perms(zc->zc_name,
966 ZFS_DELEG_PERM_CREATE, cr));
967}
968
43e52edd
BB
969/* ARGSUSED */
970static int
971zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
972{
973 return (zfs_secpolicy_recv(zc, innvl, cr));
974}
975
34dc7c2f
BB
976int
977zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
978{
428870ff
BB
979 return (zfs_secpolicy_write_perms(name,
980 ZFS_DELEG_PERM_SNAPSHOT, cr));
34dc7c2f
BB
981}
982
6f1ffb06
MA
983/*
984 * Check for permission to create each snapshot in the nvlist.
985 */
986/* ARGSUSED */
34dc7c2f 987static int
6f1ffb06 988zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 989{
6f1ffb06
MA
990 nvlist_t *snaps;
991 int error = 0;
992 nvpair_t *pair;
993
994 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
2e528b49 995 return (SET_ERROR(EINVAL));
6f1ffb06
MA
996 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
997 pair = nvlist_next_nvpair(snaps, pair)) {
998 char *name = nvpair_name(pair);
999 char *atp = strchr(name, '@');
34dc7c2f 1000
6f1ffb06 1001 if (atp == NULL) {
2e528b49 1002 error = SET_ERROR(EINVAL);
6f1ffb06
MA
1003 break;
1004 }
1005 *atp = '\0';
1006 error = zfs_secpolicy_snapshot_perms(name, cr);
1007 *atp = '@';
1008 if (error != 0)
1009 break;
1010 }
1011 return (error);
1012}
1013
da536844
MA
1014/*
1015 * Check for permission to create each snapshot in the nvlist.
1016 */
1017/* ARGSUSED */
1018static int
1019zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1020{
1021 int error = 0;
1022 nvpair_t *pair;
1023
1024 for (pair = nvlist_next_nvpair(innvl, NULL);
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;
3392 nvpair_t *pair, *pair2;
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. */
3426 for (pair2 = nvlist_next_nvpair(snaps, pair);
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{
3584 nvpair_t *pair, *pair2;
3585
3586 for (pair = nvlist_next_nvpair(innvl, NULL);
3587 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3588 char *snap_name;
3589
3590 /*
3591 * Verify the snapshot argument.
3592 */
3593 if (nvpair_value_string(pair, &snap_name) != 0)
3594 return (SET_ERROR(EINVAL));
3595
3596
3597 /* Verify that the keys (bookmarks) are unique */
3598 for (pair2 = nvlist_next_nvpair(innvl, pair);
3599 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3600 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3601 return (SET_ERROR(EINVAL));
3602 }
3603 }
3604
3605 return (dsl_bookmark_create(innvl, outnvl));
3606}
3607
3608/*
3609 * innvl: {
3610 * property 1, property 2, ...
3611 * }
3612 *
3613 * outnvl: {
3614 * bookmark name 1 -> { property 1, property 2, ... },
3615 * bookmark name 2 -> { property 1, property 2, ... }
3616 * }
3617 *
3618 */
3619static int
3620zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3621{
3622 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3623}
3624
3625/*
3626 * innvl: {
3627 * bookmark name 1, bookmark name 2
3628 * }
3629 *
3630 * outnvl: bookmark -> error code (int32)
3631 *
3632 */
3633static int
3634zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3635 nvlist_t *outnvl)
3636{
3637 int error, poollen;
3638 nvpair_t *pair;
3639
3640 poollen = strlen(poolname);
3641 for (pair = nvlist_next_nvpair(innvl, NULL);
3642 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
330d06f9 3643 const char *name = nvpair_name(pair);
da536844 3644 const char *cp = strchr(name, '#');
6f1ffb06 3645
330d06f9 3646 /*
da536844
MA
3647 * The bookmark name must contain an #, and the part after it
3648 * must contain only valid characters.
3649 */
3650 if (cp == NULL ||
3651 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3652 return (SET_ERROR(EINVAL));
3653
3654 /*
3655 * The bookmark must be in the specified pool.
330d06f9 3656 */
6f1ffb06 3657 if (strncmp(name, poolname, poollen) != 0 ||
da536844 3658 (name[poollen] != '/' && name[poollen] != '#'))
2e528b49 3659 return (SET_ERROR(EXDEV));
330d06f9
MA
3660 }
3661
da536844
MA
3662 error = dsl_bookmark_destroy(innvl, outnvl);
3663 return (error);
34dc7c2f
BB
3664}
3665
3666/*
3667 * inputs:
3668 * zc_name name of dataset to destroy
3669 * zc_objset_type type of objset
45d1cae3 3670 * zc_defer_destroy mark for deferred destroy
34dc7c2f
BB
3671 *
3672 * outputs: none
3673 */
3674static int
3675zfs_ioc_destroy(zfs_cmd_t *zc)
3676{
428870ff 3677 int err;
d09f25dc
WA
3678
3679 if (zc->zc_objset_type == DMU_OST_ZFS) {
3680 err = zfs_unmount_snap(zc->zc_name);
3681 if (err != 0)
3682 return (err);
3683 }
34dc7c2f 3684
1b87e0f5 3685 if (strchr(zc->zc_name, '@')) {
13fe0198 3686 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
1b87e0f5 3687 } else {
13fe0198 3688 err = dsl_destroy_head(zc->zc_name);
1b87e0f5
RS
3689 if (err == EEXIST) {
3690 /*
3691 * It is possible that the given DS may have
3692 * hidden child (%recv) datasets - "leftovers"
3693 * resulting from the previously interrupted
3694 * 'zfs receive'.
3695 *
3696 * 6 extra bytes for /%recv
3697 */
3698 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3699
682ce104
TH
3700 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3701 zc->zc_name, recv_clone_name) >=
3702 sizeof (namebuf))
3703 return (SET_ERROR(EINVAL));
1b87e0f5
RS
3704
3705 /*
3706 * Try to remove the hidden child (%recv) and after
3707 * that try to remove the target dataset.
3708 * If the hidden child (%recv) does not exist
3709 * the original error (EEXIST) will be returned
3710 */
3711 err = dsl_destroy_head(namebuf);
3712 if (err == 0)
3713 err = dsl_destroy_head(zc->zc_name);
3714 else if (err == ENOENT)
ecb2b7dc 3715 err = SET_ERROR(EEXIST);
1b87e0f5
RS
3716 }
3717 }
a0bd735a 3718
428870ff 3719 return (err);
34dc7c2f
BB
3720}
3721
3722/*
46ba1e59 3723 * fsname is name of dataset to rollback (to most recent snapshot)
34dc7c2f 3724 *
8ca78ab0 3725 * innvl may contain name of expected target snapshot
46ba1e59
MA
3726 *
3727 * outnvl: "target" -> name of most recent snapshot
3728 * }
34dc7c2f 3729 */
46ba1e59 3730/* ARGSUSED */
34dc7c2f 3731static int
8ca78ab0 3732zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3733{
0037b49e 3734 zfsvfs_t *zfsvfs;
040dab99 3735 zvol_state_t *zv;
8ca78ab0 3736 char *target = NULL;
13fe0198 3737 int error;
34dc7c2f 3738
8ca78ab0
AG
3739 (void) nvlist_lookup_string(innvl, "target", &target);
3740 if (target != NULL) {
3741 int fslen = strlen(fsname);
3742
3743 if (strncmp(fsname, target, fslen) != 0)
3744 return (SET_ERROR(EINVAL));
3745 if (target[fslen] != '@')
3746 return (SET_ERROR(EINVAL));
3747 }
3748
f298b24d 3749 if (getzfsvfs(fsname, &zfsvfs) == 0) {
ec923db2
GM
3750 dsl_dataset_t *ds;
3751
0037b49e
BB
3752 ds = dmu_objset_ds(zfsvfs->z_os);
3753 error = zfs_suspend_fs(zfsvfs);
34dc7c2f
BB
3754 if (error == 0) {
3755 int resume_err;
3756
8ca78ab0
AG
3757 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3758 outnvl);
0037b49e 3759 resume_err = zfs_resume_fs(zfsvfs, ds);
34dc7c2f 3760 error = error ? error : resume_err;
34dc7c2f 3761 }
0037b49e 3762 deactivate_super(zfsvfs->z_sb);
040dab99 3763 } else if ((zv = zvol_suspend(fsname)) != NULL) {
8ca78ab0
AG
3764 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
3765 outnvl);
040dab99 3766 zvol_resume(zv);
34dc7c2f 3767 } else {
8ca78ab0 3768 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
34dc7c2f 3769 }
13fe0198
MA
3770 return (error);
3771}
34dc7c2f 3772
13fe0198
MA
3773static int
3774recursive_unmount(const char *fsname, void *arg)
3775{
3776 const char *snapname = arg;
3777 char *fullname;
00fcdee1 3778 int error;
428870ff 3779
13fe0198 3780 fullname = kmem_asprintf("%s@%s", fsname, snapname);
00fcdee1 3781 error = zfs_unmount_snap(fullname);
13fe0198 3782 strfree(fullname);
00fcdee1
AV
3783
3784 return (error);
34dc7c2f
BB
3785}
3786
3787/*
3788 * inputs:
3789 * zc_name old name of dataset
3790 * zc_value new name of dataset
3791 * zc_cookie recursive flag (only valid for snapshots)
3792 *
3793 * outputs: none
3794 */
3795static int
3796zfs_ioc_rename(zfs_cmd_t *zc)
3797{
3798 boolean_t recursive = zc->zc_cookie & 1;
13fe0198 3799 char *at;
34dc7c2f 3800
650258d7 3801 /* "zfs rename" from and to ...%recv datasets should both fail */
3802 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
34dc7c2f 3803 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
650258d7 3804 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3805 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3806 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
2e528b49 3807 return (SET_ERROR(EINVAL));
34dc7c2f 3808
13fe0198
MA
3809 at = strchr(zc->zc_name, '@');
3810 if (at != NULL) {
3811 /* snaps must be in same fs */
9554185d
SH
3812 int error;
3813
13fe0198 3814 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
2e528b49 3815 return (SET_ERROR(EXDEV));
13fe0198
MA
3816 *at = '\0';
3817 if (zc->zc_objset_type == DMU_OST_ZFS) {
9554185d 3818 error = dmu_objset_find(zc->zc_name,
13fe0198
MA
3819 recursive_unmount, at + 1,
3820 recursive ? DS_FIND_CHILDREN : 0);
9554185d
SH
3821 if (error != 0) {
3822 *at = '@';
13fe0198 3823 return (error);
9554185d 3824 }
13fe0198 3825 }
9554185d
SH
3826 error = dsl_dataset_rename_snapshot(zc->zc_name,
3827 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3828 *at = '@';
3829
3830 return (error);
13fe0198 3831 } else {
ba6a2402 3832 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
95c73795 3833 }
34dc7c2f
BB
3834}
3835
428870ff
BB
3836static int
3837zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3838{
3839 const char *propname = nvpair_name(pair);
3840 boolean_t issnap = (strchr(dsname, '@') != NULL);
3841 zfs_prop_t prop = zfs_name_to_prop(propname);
3842 uint64_t intval;
3843 int err;
3844
3845 if (prop == ZPROP_INVAL) {
3846 if (zfs_prop_user(propname)) {
c65aa5b2
BB
3847 if ((err = zfs_secpolicy_write_perms(dsname,
3848 ZFS_DELEG_PERM_USERPROP, cr)))
428870ff
BB
3849 return (err);
3850 return (0);
3851 }
3852
3853 if (!issnap && zfs_prop_userquota(propname)) {
3854 const char *perm = NULL;
3855 const char *uq_prefix =
3856 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3857 const char *gq_prefix =
3858 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
1de321e6
JX
3859 const char *uiq_prefix =
3860 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
3861 const char *giq_prefix =
3862 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
428870ff
BB
3863
3864 if (strncmp(propname, uq_prefix,
3865 strlen(uq_prefix)) == 0) {
3866 perm = ZFS_DELEG_PERM_USERQUOTA;
1de321e6
JX
3867 } else if (strncmp(propname, uiq_prefix,
3868 strlen(uiq_prefix)) == 0) {
3869 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
428870ff
BB
3870 } else if (strncmp(propname, gq_prefix,
3871 strlen(gq_prefix)) == 0) {
3872 perm = ZFS_DELEG_PERM_GROUPQUOTA;
1de321e6
JX
3873 } else if (strncmp(propname, giq_prefix,
3874 strlen(giq_prefix)) == 0) {
3875 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
428870ff
BB
3876 } else {
3877 /* USERUSED and GROUPUSED are read-only */
2e528b49 3878 return (SET_ERROR(EINVAL));
428870ff
BB
3879 }
3880
c65aa5b2 3881 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
428870ff
BB
3882 return (err);
3883 return (0);
3884 }
3885
2e528b49 3886 return (SET_ERROR(EINVAL));
428870ff
BB
3887 }
3888
3889 if (issnap)
2e528b49 3890 return (SET_ERROR(EINVAL));
428870ff
BB
3891
3892 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3893 /*
3894 * dsl_prop_get_all_impl() returns properties in this
3895 * format.
3896 */
3897 nvlist_t *attrs;
3898 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3899 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3900 &pair) == 0);
3901 }
3902
3903 /*
3904 * Check that this value is valid for this pool version
3905 */
3906 switch (prop) {
3907 case ZFS_PROP_COMPRESSION:
3908 /*
3909 * If the user specified gzip compression, make sure
3910 * the SPA supports it. We ignore any errors here since
3911 * we'll catch them later.
3912 */
f1512ee6 3913 if (nvpair_value_uint64(pair, &intval) == 0) {
428870ff
BB
3914 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3915 intval <= ZIO_COMPRESS_GZIP_9 &&
3916 zfs_earlier_version(dsname,
3917 SPA_VERSION_GZIP_COMPRESSION)) {
2e528b49 3918 return (SET_ERROR(ENOTSUP));
428870ff
BB
3919 }
3920
3921 if (intval == ZIO_COMPRESS_ZLE &&
3922 zfs_earlier_version(dsname,
3923 SPA_VERSION_ZLE_COMPRESSION))
2e528b49 3924 return (SET_ERROR(ENOTSUP));
428870ff 3925
9759c60f 3926 if (intval == ZIO_COMPRESS_LZ4) {
9759c60f
ED
3927 spa_t *spa;
3928
3929 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3930 return (err);
3931
fa86b5db
MA
3932 if (!spa_feature_is_enabled(spa,
3933 SPA_FEATURE_LZ4_COMPRESS)) {
9759c60f 3934 spa_close(spa, FTAG);
2e528b49 3935 return (SET_ERROR(ENOTSUP));
9759c60f
ED
3936 }
3937 spa_close(spa, FTAG);
3938 }
3939
428870ff
BB
3940 /*
3941 * If this is a bootable dataset then
3942 * verify that the compression algorithm
3943 * is supported for booting. We must return
3944 * something other than ENOTSUP since it
3945 * implies a downrev pool version.
3946 */
3947 if (zfs_is_bootfs(dsname) &&
3948 !BOOTFS_COMPRESS_VALID(intval)) {
2e528b49 3949 return (SET_ERROR(ERANGE));
428870ff
BB
3950 }
3951 }
3952 break;
3953
3954 case ZFS_PROP_COPIES:
3955 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
2e528b49 3956 return (SET_ERROR(ENOTSUP));
428870ff
BB
3957 break;
3958
4cb7b9c5 3959 case ZFS_PROP_VOLBLOCKSIZE:
f1512ee6
MA
3960 case ZFS_PROP_RECORDSIZE:
3961 /* Record sizes above 128k need the feature to be enabled */
3962 if (nvpair_value_uint64(pair, &intval) == 0 &&
3963 intval > SPA_OLD_MAXBLOCKSIZE) {
3964 spa_t *spa;
3965
f1512ee6
MA
3966 /*
3967 * We don't allow setting the property above 1MB,
3968 * unless the tunable has been changed.
3969 */
3970 if (intval > zfs_max_recordsize ||
3971 intval > SPA_MAXBLOCKSIZE)
4b2a3e0c 3972 return (SET_ERROR(ERANGE));
f1512ee6
MA
3973
3974 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3975 return (err);
3976
3977 if (!spa_feature_is_enabled(spa,
3978 SPA_FEATURE_LARGE_BLOCKS)) {
3979 spa_close(spa, FTAG);
3980 return (SET_ERROR(ENOTSUP));
3981 }
3982 spa_close(spa, FTAG);
3983 }
3984 break;
3985
50c957f7
NB
3986 case ZFS_PROP_DNODESIZE:
3987 /* Dnode sizes above 512 need the feature to be enabled */
3988 if (nvpair_value_uint64(pair, &intval) == 0 &&
3989 intval != ZFS_DNSIZE_LEGACY) {
3990 spa_t *spa;
3991
3992 /*
3993 * If this is a bootable dataset then
3994 * we don't allow large (>512B) dnodes,
3995 * because GRUB doesn't support them.
3996 */
3997 if (zfs_is_bootfs(dsname) &&
02730c33 3998 intval != ZFS_DNSIZE_LEGACY) {
50c957f7
NB
3999 return (SET_ERROR(EDOM));
4000 }
4001
4002 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4003 return (err);
4004
4005 if (!spa_feature_is_enabled(spa,
4006 SPA_FEATURE_LARGE_DNODE)) {
4007 spa_close(spa, FTAG);
4008 return (SET_ERROR(ENOTSUP));
4009 }
4010 spa_close(spa, FTAG);
4011 }
4012 break;
4013
428870ff
BB
4014 case ZFS_PROP_SHARESMB:
4015 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
2e528b49 4016 return (SET_ERROR(ENOTSUP));
428870ff
BB
4017 break;
4018
4019 case ZFS_PROP_ACLINHERIT:
4020 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4021 nvpair_value_uint64(pair, &intval) == 0) {
4022 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4023 zfs_earlier_version(dsname,
4024 SPA_VERSION_PASSTHROUGH_X))
2e528b49 4025 return (SET_ERROR(ENOTSUP));
428870ff
BB
4026 }
4027 break;
3c67d83a
TH
4028 case ZFS_PROP_CHECKSUM:
4029 case ZFS_PROP_DEDUP:
4030 {
4031 spa_feature_t feature;
4032 spa_t *spa;
4033 uint64_t intval;
4034 int err;
4035
4036 /* dedup feature version checks */
4037 if (prop == ZFS_PROP_DEDUP &&
4038 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4039 return (SET_ERROR(ENOTSUP));
4040
4041 if (nvpair_value_uint64(pair, &intval) != 0)
4042 return (SET_ERROR(EINVAL));
4043
4044 /* check prop value is enabled in features */
4a2e9a17 4045 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3c67d83a
TH
4046 if (feature == SPA_FEATURE_NONE)
4047 break;
4048
4049 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4050 return (err);
4051 /*
4052 * Salted checksums are not supported on root pools.
4053 */
4054 if (spa_bootfs(spa) != 0 &&
4055 intval < ZIO_CHECKSUM_FUNCTIONS &&
4056 (zio_checksum_table[intval].ci_flags &
4057 ZCHECKSUM_FLAG_SALTED)) {
4058 spa_close(spa, FTAG);
4059 return (SET_ERROR(ERANGE));
4060 }
4061 if (!spa_feature_is_enabled(spa, feature)) {
4062 spa_close(spa, FTAG);
4063 return (SET_ERROR(ENOTSUP));
4064 }
4065 spa_close(spa, FTAG);
4066 break;
4067 }
4068
e75c13c3
BB
4069 default:
4070 break;
428870ff
BB
4071 }
4072
4073 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4074}
4075
4076/*
4077 * Removes properties from the given props list that fail permission checks
4078 * needed to clear them and to restore them in case of a receive error. For each
4079 * property, make sure we have both set and inherit permissions.
4080 *
4081 * Returns the first error encountered if any permission checks fail. If the
4082 * caller provides a non-NULL errlist, it also gives the complete list of names
4083 * of all the properties that failed a permission check along with the
4084 * corresponding error numbers. The caller is responsible for freeing the
4085 * returned errlist.
4086 *
4087 * If every property checks out successfully, zero is returned and the list
4088 * pointed at by errlist is NULL.
4089 */
4090static int
4091zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
b128c09f
BB
4092{
4093 zfs_cmd_t *zc;
428870ff
BB
4094 nvpair_t *pair, *next_pair;
4095 nvlist_t *errors;
4096 int err, rv = 0;
b128c09f
BB
4097
4098 if (props == NULL)
428870ff
BB
4099 return (0);
4100
4101 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4102
efcd79a8 4103 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
680eada9 4104 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
428870ff
BB
4105 pair = nvlist_next_nvpair(props, NULL);
4106 while (pair != NULL) {
4107 next_pair = nvlist_next_nvpair(props, pair);
4108
680eada9 4109 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4110 sizeof (zc->zc_value));
428870ff 4111 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
6f1ffb06 4112 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
428870ff
BB
4113 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4114 VERIFY(nvlist_add_int32(errors,
4115 zc->zc_value, err) == 0);
4116 }
4117 pair = next_pair;
b128c09f
BB
4118 }
4119 kmem_free(zc, sizeof (zfs_cmd_t));
428870ff
BB
4120
4121 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4122 nvlist_free(errors);
4123 errors = NULL;
4124 } else {
4125 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4126 }
4127
4128 if (errlist == NULL)
4129 nvlist_free(errors);
4130 else
4131 *errlist = errors;
4132
4133 return (rv);
4134}
4135
4136static boolean_t
4137propval_equals(nvpair_t *p1, nvpair_t *p2)
4138{
4139 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4140 /* dsl_prop_get_all_impl() format */
4141 nvlist_t *attrs;
4142 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4143 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4144 &p1) == 0);
4145 }
4146
4147 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4148 nvlist_t *attrs;
4149 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4150 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4151 &p2) == 0);
4152 }
4153
4154 if (nvpair_type(p1) != nvpair_type(p2))
4155 return (B_FALSE);
4156
4157 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4158 char *valstr1, *valstr2;
4159
4160 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4161 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4162 return (strcmp(valstr1, valstr2) == 0);
4163 } else {
4164 uint64_t intval1, intval2;
4165
4166 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4167 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4168 return (intval1 == intval2);
4169 }
b128c09f
BB
4170}
4171
428870ff
BB
4172/*
4173 * Remove properties from props if they are not going to change (as determined
4174 * by comparison with origprops). Remove them from origprops as well, since we
4175 * do not need to clear or restore properties that won't change.
4176 */
4177static void
4178props_reduce(nvlist_t *props, nvlist_t *origprops)
4179{
4180 nvpair_t *pair, *next_pair;
4181
4182 if (origprops == NULL)
4183 return; /* all props need to be received */
4184
4185 pair = nvlist_next_nvpair(props, NULL);
4186 while (pair != NULL) {
4187 const char *propname = nvpair_name(pair);
4188 nvpair_t *match;
4189
4190 next_pair = nvlist_next_nvpair(props, pair);
4191
4192 if ((nvlist_lookup_nvpair(origprops, propname,
4193 &match) != 0) || !propval_equals(pair, match))
4194 goto next; /* need to set received value */
4195
4196 /* don't clear the existing received value */
4197 (void) nvlist_remove_nvpair(origprops, match);
4198 /* don't bother receiving the property */
4199 (void) nvlist_remove_nvpair(props, pair);
4200next:
4201 pair = next_pair;
4202 }
4203}
4204
671c9354
DM
4205/*
4206 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4207 * For example, refquota cannot be set until after the receipt of a dataset,
4208 * because in replication streams, an older/earlier snapshot may exceed the
4209 * refquota. We want to receive the older/earlier snapshot, but setting
4210 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4211 * the older/earlier snapshot from being received (with EDQUOT).
4212 *
4213 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4214 *
4215 * libzfs will need to be judicious handling errors encountered by props
4216 * extracted by this function.
4217 */
4218static nvlist_t *
4219extract_delay_props(nvlist_t *props)
4220{
4221 nvlist_t *delayprops;
4222 nvpair_t *nvp, *tmp;
b5256303
TC
4223 static const zfs_prop_t delayable[] = {
4224 ZFS_PROP_REFQUOTA,
4225 ZFS_PROP_KEYLOCATION,
4226 0
4227 };
671c9354
DM
4228 int i;
4229
4230 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4231
4232 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4233 nvp = nvlist_next_nvpair(props, nvp)) {
4234 /*
4235 * strcmp() is safe because zfs_prop_to_name() always returns
4236 * a bounded string.
4237 */
4238 for (i = 0; delayable[i] != 0; i++) {
4239 if (strcmp(zfs_prop_to_name(delayable[i]),
4240 nvpair_name(nvp)) == 0) {
4241 break;
4242 }
4243 }
4244 if (delayable[i] != 0) {
4245 tmp = nvlist_prev_nvpair(props, nvp);
4246 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4247 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4248 nvp = tmp;
4249 }
4250 }
4251
4252 if (nvlist_empty(delayprops)) {
4253 nvlist_free(delayprops);
4254 delayprops = NULL;
4255 }
4256 return (delayprops);
4257}
4258
428870ff
BB
4259#ifdef DEBUG
4260static boolean_t zfs_ioc_recv_inject_err;
4261#endif
4262
34dc7c2f 4263/*
1bf3bf0e
GN
4264 * nvlist 'errors' is always allocated. It will contain descriptions of
4265 * encountered errors, if any. It's the callers responsibility to free.
34dc7c2f
BB
4266 */
4267static int
a3eeab2d 4268zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4269 nvlist_t *localprops, boolean_t force, boolean_t resumable, int input_fd,
43e52edd
BB
4270 dmu_replay_record_t *begin_record, int cleanup_fd, uint64_t *read_bytes,
4271 uint64_t *errflags, uint64_t *action_handle, nvlist_t **errors)
34dc7c2f 4272{
34dc7c2f 4273 dmu_recv_cookie_t drc;
428870ff
BB
4274 int error = 0;
4275 int props_error = 0;
34dc7c2f 4276 offset_t off;
671c9354 4277 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
43e52edd 4278 nvlist_t *origprops = NULL; /* existing properties */
a3eeab2d 4279 nvlist_t *origrecvd = NULL; /* existing received properties */
428870ff 4280 boolean_t first_recvd_props = B_FALSE;
43e52edd 4281 file_t *input_fp;
34dc7c2f 4282
1bf3bf0e
GN
4283 *read_bytes = 0;
4284 *errflags = 0;
4285 *errors = fnvlist_alloc();
4286
43e52edd
BB
4287 input_fp = getf(input_fd);
4288 if (input_fp == NULL)
2e528b49 4289 return (SET_ERROR(EBADF));
13fe0198
MA
4290
4291 error = dmu_recv_begin(tofs, tosnap,
43e52edd 4292 begin_record, force, resumable, origin, &drc);
13fe0198
MA
4293 if (error != 0)
4294 goto out;
4295
4296 /*
4297 * Set properties before we receive the stream so that they are applied
4298 * to the new data. Note that we must call dmu_recv_stream() if
4299 * dmu_recv_begin() succeeds.
4300 */
a3eeab2d 4301 if (recvprops != NULL && !drc.drc_newfs) {
13fe0198
MA
4302 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4303 SPA_VERSION_RECVD_PROPS &&
4304 !dsl_prop_get_hasrecvd(tofs))
428870ff 4305 first_recvd_props = B_TRUE;
428870ff 4306
b128c09f 4307 /*
428870ff
BB
4308 * If new received properties are supplied, they are to
4309 * completely replace the existing received properties, so stash
4310 * away the existing ones.
b128c09f 4311 */
a3eeab2d 4312 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
428870ff
BB
4313 nvlist_t *errlist = NULL;
4314 /*
4315 * Don't bother writing a property if its value won't
4316 * change (and avoid the unnecessary security checks).
4317 *
4318 * The first receive after SPA_VERSION_RECVD_PROPS is a
4319 * special case where we blow away all local properties
4320 * regardless.
4321 */
4322 if (!first_recvd_props)
a3eeab2d 4323 props_reduce(recvprops, origrecvd);
4324 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
43e52edd 4325 (void) nvlist_merge(*errors, errlist, 0);
428870ff 4326 nvlist_free(errlist);
b128c09f 4327
a3eeab2d 4328 if (clear_received_props(tofs, origrecvd,
4329 first_recvd_props ? NULL : recvprops) != 0)
4330 *errflags |= ZPROP_ERR_NOCLEAR;
4331 } else {
4332 *errflags |= ZPROP_ERR_NOCLEAR;
4333 }
4334 }
4335
4336 /*
4337 * Stash away existing properties so we can restore them on error unless
4338 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4339 * case "origrecvd" will take care of that.
4340 */
4341 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4342 objset_t *os;
4343 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4344 if (dsl_prop_get_all(os, &origprops) != 0) {
43e52edd 4345 *errflags |= ZPROP_ERR_NOCLEAR;
a3eeab2d 4346 }
4347 dmu_objset_rele(os, FTAG);
13fe0198 4348 } else {
43e52edd 4349 *errflags |= ZPROP_ERR_NOCLEAR;
428870ff 4350 }
13fe0198
MA
4351 }
4352
a3eeab2d 4353 if (recvprops != NULL) {
13fe0198 4354 props_error = dsl_prop_set_hasrecvd(tofs);
428870ff 4355
13fe0198 4356 if (props_error == 0) {
a3eeab2d 4357 delayprops = extract_delay_props(recvprops);
13fe0198 4358 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
a3eeab2d 4359 recvprops, *errors);
13fe0198 4360 }
428870ff
BB
4361 }
4362
a3eeab2d 4363 if (localprops != NULL) {
4364 nvlist_t *oprops = fnvlist_alloc();
4365 nvlist_t *xprops = fnvlist_alloc();
4366 nvpair_t *nvp = NULL;
4367
4368 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4369 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4370 /* -x property */
4371 const char *name = nvpair_name(nvp);
4372 zfs_prop_t prop = zfs_name_to_prop(name);
4373 if (prop != ZPROP_INVAL) {
4374 if (!zfs_prop_inheritable(prop))
4375 continue;
4376 } else if (!zfs_prop_user(name))
4377 continue;
4378 fnvlist_add_boolean(xprops, name);
4379 } else {
4380 /* -o property=value */
4381 fnvlist_add_nvpair(oprops, nvp);
4382 }
4383 }
4384 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4385 oprops, *errors);
4386 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4387 xprops, *errors);
4388
4389 nvlist_free(oprops);
4390 nvlist_free(xprops);
4391 }
4392
43e52edd
BB
4393 off = input_fp->f_offset;
4394 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4395 action_handle);
34dc7c2f 4396
45d1cae3 4397 if (error == 0) {
0037b49e 4398 zfsvfs_t *zfsvfs = NULL;
040dab99 4399 zvol_state_t *zv = NULL;
b128c09f 4400
f298b24d 4401 if (getzfsvfs(tofs, &zfsvfs) == 0) {
45d1cae3 4402 /* online recv */
ec923db2 4403 dsl_dataset_t *ds;
45d1cae3 4404 int end_err;
b128c09f 4405
0037b49e
BB
4406 ds = dmu_objset_ds(zfsvfs->z_os);
4407 error = zfs_suspend_fs(zfsvfs);
45d1cae3
BB
4408 /*
4409 * If the suspend fails, then the recv_end will
4410 * likely also fail, and clean up after itself.
4411 */
0037b49e 4412 end_err = dmu_recv_end(&drc, zfsvfs);
428870ff 4413 if (error == 0)
0037b49e 4414 error = zfs_resume_fs(zfsvfs, ds);
45d1cae3 4415 error = error ? error : end_err;
0037b49e 4416 deactivate_super(zfsvfs->z_sb);
040dab99
CC
4417 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4418 error = dmu_recv_end(&drc, zvol_tag(zv));
4419 zvol_resume(zv);
b128c09f 4420 } else {
831baf06 4421 error = dmu_recv_end(&drc, NULL);
34dc7c2f 4422 }
671c9354
DM
4423
4424 /* Set delayed properties now, after we're done receiving. */
4425 if (delayprops != NULL && error == 0) {
4426 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
43e52edd 4427 delayprops, *errors);
671c9354
DM
4428 }
4429 }
4430
4431 if (delayprops != NULL) {
4432 /*
4433 * Merge delayed props back in with initial props, in case
4434 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4435 * we have to make sure clear_received_props() includes
4436 * the delayed properties).
4437 *
4438 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4439 * using ASSERT() will be just like a VERIFY.
4440 */
a3eeab2d 4441 ASSERT(nvlist_merge(recvprops, delayprops, 0) == 0);
671c9354
DM
4442 nvlist_free(delayprops);
4443 }
4444
34dc7c2f 4445
43e52edd
BB
4446 *read_bytes = off - input_fp->f_offset;
4447 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
02730c33 4448 input_fp->f_offset = off;
34dc7c2f 4449
428870ff
BB
4450#ifdef DEBUG
4451 if (zfs_ioc_recv_inject_err) {
4452 zfs_ioc_recv_inject_err = B_FALSE;
4453 error = 1;
4454 }
4455#endif
ba6a2402 4456
b128c09f
BB
4457 /*
4458 * On error, restore the original props.
4459 */
a3eeab2d 4460 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4461 if (clear_received_props(tofs, recvprops, NULL) != 0) {
13fe0198
MA
4462 /*
4463 * We failed to clear the received properties.
4464 * Since we may have left a $recvd value on the
4465 * system, we can't clear the $hasrecvd flag.
4466 */
43e52edd 4467 *errflags |= ZPROP_ERR_NORESTORE;
13fe0198
MA
4468 } else if (first_recvd_props) {
4469 dsl_prop_unset_hasrecvd(tofs);
428870ff
BB
4470 }
4471
a3eeab2d 4472 if (origrecvd == NULL && !drc.drc_newfs) {
428870ff 4473 /* We failed to stash the original properties. */
43e52edd 4474 *errflags |= ZPROP_ERR_NORESTORE;
428870ff
BB
4475 }
4476
4477 /*
4478 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4479 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4e33ba4c 4480 * explicitly if we're restoring local properties cleared in the
428870ff
BB
4481 * first new-style receive.
4482 */
a3eeab2d 4483 if (origrecvd != NULL &&
428870ff
BB
4484 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4485 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
a3eeab2d 4486 origrecvd, NULL) != 0) {
428870ff
BB
4487 /*
4488 * We stashed the original properties but failed to
4489 * restore them.
4490 */
43e52edd 4491 *errflags |= ZPROP_ERR_NORESTORE;
428870ff 4492 }
b128c09f 4493 }
a3eeab2d 4494 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4495 !first_recvd_props) {
4496 nvlist_t *setprops;
4497 nvlist_t *inheritprops;
4498 nvpair_t *nvp;
4499
4500 if (origprops == NULL) {
4501 /* We failed to stash the original properties. */
4502 *errflags |= ZPROP_ERR_NORESTORE;
4503 goto out;
4504 }
4505
4506 /* Restore original props */
4507 setprops = fnvlist_alloc();
4508 inheritprops = fnvlist_alloc();
4509 nvp = NULL;
4510 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4511 const char *name = nvpair_name(nvp);
4512 const char *source;
4513 nvlist_t *attrs;
4514
4515 if (!nvlist_exists(origprops, name)) {
4516 /*
4517 * Property was not present or was explicitly
4518 * inherited before the receive, restore this.
4519 */
4520 fnvlist_add_boolean(inheritprops, name);
4521 continue;
4522 }
4523 attrs = fnvlist_lookup_nvlist(origprops, name);
4524 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4525
4526 /* Skip received properties */
4527 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4528 continue;
4529
4530 if (strcmp(source, tofs) == 0) {
4531 /* Property was locally set */
4532 fnvlist_add_nvlist(setprops, name, attrs);
4533 } else {
4534 /* Property was implicitly inherited */
4535 fnvlist_add_boolean(inheritprops, name);
4536 }
4537 }
4538
4539 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4540 NULL) != 0)
4541 *errflags |= ZPROP_ERR_NORESTORE;
4542 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4543 NULL) != 0)
4544 *errflags |= ZPROP_ERR_NORESTORE;
4545
4546 nvlist_free(setprops);
4547 nvlist_free(inheritprops);
4548 }
b128c09f 4549out:
43e52edd 4550 releasef(input_fd);
a3eeab2d 4551 nvlist_free(origrecvd);
b128c09f 4552 nvlist_free(origprops);
428870ff
BB
4553
4554 if (error == 0)
4555 error = props_error;
4556
34dc7c2f
BB
4557 return (error);
4558}
4559
43e52edd
BB
4560/*
4561 * inputs:
4562 * zc_name name of containing filesystem (unused)
4563 * zc_nvlist_src{_size} nvlist of properties to apply
a3eeab2d 4564 * zc_nvlist_conf{_size} nvlist of properties to exclude
4565 * (DATA_TYPE_BOOLEAN) and override (everything else)
43e52edd
BB
4566 * zc_value name of snapshot to create
4567 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4568 * zc_cookie file descriptor to recv from
4569 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4570 * zc_guid force flag
4571 * zc_cleanup_fd cleanup-on-exit file descriptor
4572 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4573 *
4574 * outputs:
4575 * zc_cookie number of bytes read
4576 * zc_obj zprop_errflags_t
4577 * zc_action_handle handle for this guid/ds mapping
4578 * zc_nvlist_dst{_size} error for each unapplied received property
4579 */
4580static int
4581zfs_ioc_recv(zfs_cmd_t *zc)
4582{
4583 dmu_replay_record_t begin_record;
4584 nvlist_t *errors = NULL;
a3eeab2d 4585 nvlist_t *recvdprops = NULL;
4586 nvlist_t *localprops = NULL;
43e52edd
BB
4587 char *origin = NULL;
4588 char *tosnap;
eca7b760 4589 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd
BB
4590 int error = 0;
4591
4592 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4593 strchr(zc->zc_value, '@') == NULL ||
4594 strchr(zc->zc_value, '%'))
4595 return (SET_ERROR(EINVAL));
4596
30f3f2e1 4597 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
43e52edd
BB
4598 tosnap = strchr(tofs, '@');
4599 *tosnap++ = '\0';
4600
4601 if (zc->zc_nvlist_src != 0 &&
4602 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
a3eeab2d 4603 zc->zc_iflags, &recvdprops)) != 0)
4604 return (error);
4605
4606 if (zc->zc_nvlist_conf != 0 &&
4607 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
4608 zc->zc_iflags, &localprops)) != 0)
43e52edd
BB
4609 return (error);
4610
4611 if (zc->zc_string[0])
4612 origin = zc->zc_string;
4613
4614 begin_record.drr_type = DRR_BEGIN;
4615 begin_record.drr_payloadlen = 0;
4616 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4617
a3eeab2d 4618 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
4619 zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
4620 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
4621 &zc->zc_action_handle, &errors);
4622 nvlist_free(recvdprops);
4623 nvlist_free(localprops);
43e52edd
BB
4624
4625 /*
4626 * Now that all props, initial and delayed, are set, report the prop
4627 * errors to the caller.
4628 */
4629 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4630 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4631 put_nvlist(zc, errors) != 0)) {
4632 /*
4633 * Caller made zc->zc_nvlist_dst less than the minimum expected
4634 * size or supplied an invalid address.
4635 */
4636 error = SET_ERROR(EINVAL);
4637 }
4638
4639 nvlist_free(errors);
4640
4641 return (error);
4642}
4643
4644/*
4645 * innvl: {
4646 * "snapname" -> full name of the snapshot to create
a3eeab2d 4647 * (optional) "props" -> received properties to set (nvlist)
4648 * (optional) "localprops" -> override and exclude properties (nvlist)
43e52edd
BB
4649 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4650 * "begin_record" -> non-byteswapped dmu_replay_record_t
4651 * "input_fd" -> file descriptor to read stream from (int32)
4652 * (optional) "force" -> force flag (value ignored)
4653 * (optional) "resumable" -> resumable flag (value ignored)
4654 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4655 * (optional) "action_handle" -> handle for this guid/ds mapping
4656 * }
4657 *
4658 * outnvl: {
4659 * "read_bytes" -> number of bytes read
4660 * "error_flags" -> zprop_errflags_t
4661 * "action_handle" -> handle for this guid/ds mapping
4662 * "errors" -> error for each unapplied received property (nvlist)
4663 * }
4664 */
4665static int
4666zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4667{
4668 dmu_replay_record_t *begin_record;
4669 uint_t begin_record_size;
4670 nvlist_t *errors = NULL;
a3eeab2d 4671 nvlist_t *recvprops = NULL;
4672 nvlist_t *localprops = NULL;
43e52edd
BB
4673 char *snapname = NULL;
4674 char *origin = NULL;
4675 char *tosnap;
eca7b760 4676 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd
BB
4677 boolean_t force;
4678 boolean_t resumable;
4679 uint64_t action_handle = 0;
4680 uint64_t read_bytes = 0;
4681 uint64_t errflags = 0;
4682 int input_fd = -1;
4683 int cleanup_fd = -1;
4684 int error;
4685
4686 error = nvlist_lookup_string(innvl, "snapname", &snapname);
4687 if (error != 0)
4688 return (SET_ERROR(EINVAL));
4689
4690 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
4691 strchr(snapname, '@') == NULL ||
4692 strchr(snapname, '%'))
4693 return (SET_ERROR(EINVAL));
4694
4695 (void) strcpy(tofs, snapname);
4696 tosnap = strchr(tofs, '@');
4697 *tosnap++ = '\0';
4698
4699 error = nvlist_lookup_string(innvl, "origin", &origin);
4700 if (error && error != ENOENT)
4701 return (error);
4702
4703 error = nvlist_lookup_byte_array(innvl, "begin_record",
02730c33 4704 (uchar_t **)&begin_record, &begin_record_size);
43e52edd
BB
4705 if (error != 0 || begin_record_size != sizeof (*begin_record))
4706 return (SET_ERROR(EINVAL));
4707
4708 error = nvlist_lookup_int32(innvl, "input_fd", &input_fd);
4709 if (error != 0)
4710 return (SET_ERROR(EINVAL));
4711
4712 force = nvlist_exists(innvl, "force");
4713 resumable = nvlist_exists(innvl, "resumable");
4714
4715 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
4716 if (error && error != ENOENT)
4717 return (error);
4718
4719 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
4720 if (error && error != ENOENT)
4721 return (error);
4722
a3eeab2d 4723 /* we still use "props" here for backwards compatibility */
4724 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
43e52edd
BB
4725 if (error && error != ENOENT)
4726 return (error);
4727
a3eeab2d 4728 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
4729 if (error && error != ENOENT)
4730 return (error);
4731
4732 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
4733 force, resumable, input_fd, begin_record, cleanup_fd, &read_bytes,
43e52edd
BB
4734 &errflags, &action_handle, &errors);
4735
4736 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
4737 fnvlist_add_uint64(outnvl, "error_flags", errflags);
4738 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
4739 fnvlist_add_nvlist(outnvl, "errors", errors);
4740
4741 nvlist_free(errors);
a3eeab2d 4742 nvlist_free(recvprops);
4743 nvlist_free(localprops);
43e52edd
BB
4744
4745 return (error);
4746}
4747
34dc7c2f
BB
4748/*
4749 * inputs:
4750 * zc_name name of snapshot to send
34dc7c2f 4751 * zc_cookie file descriptor to send stream to
572e2857
BB
4752 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4753 * zc_sendobj objsetid of snapshot to send
4754 * zc_fromobj objsetid of incremental fromsnap (may be zero)
330d06f9
MA
4755 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4756 * output size in zc_objset_type.
f1512ee6 4757 * zc_flags lzc_send_flags
34dc7c2f 4758 *
da536844
MA
4759 * outputs:
4760 * zc_objset_type estimated size, if zc_guid is set
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
1bd201e7
CS
5032static int
5033zfs_ioc_pool_reopen(zfs_cmd_t *zc)
5034{
5035 spa_t *spa;
5036 int error;
5037
5038 error = spa_open(zc->zc_name, &spa, FTAG);
13fe0198 5039 if (error != 0)
1bd201e7
CS
5040 return (error);
5041
5042 spa_vdev_state_enter(spa, SCL_NONE);
65947351
GW
5043
5044 /*
5045 * If a resilver is already in progress then set the
5046 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5047 * the scan as a side effect of the reopen. Otherwise, let
5048 * vdev_open() decided if a resilver is required.
5049 */
5050 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
1bd201e7 5051 vdev_reopen(spa->spa_root_vdev);
65947351
GW
5052 spa->spa_scrub_reopen = B_FALSE;
5053
1bd201e7
CS
5054 (void) spa_vdev_state_exit(spa, NULL, 0);
5055 spa_close(spa, FTAG);
5056 return (0);
5057}
34dc7c2f
BB
5058/*
5059 * inputs:
5060 * zc_name name of filesystem
34dc7c2f 5061 *
428870ff
BB
5062 * outputs:
5063 * zc_string name of conflicting snapshot, if there is one
34dc7c2f
BB
5064 */
5065static int
5066zfs_ioc_promote(zfs_cmd_t *zc)
5067{
d12f91fd
GDN
5068 dsl_pool_t *dp;
5069 dsl_dataset_t *ds, *ods;
5070 char origin[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 5071 char *cp;
d12f91fd
GDN
5072 int error;
5073
650258d7 5074 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5075 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5076 strchr(zc->zc_name, '%'))
5077 return (SET_ERROR(EINVAL));
5078
d12f91fd
GDN
5079 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5080 if (error != 0)
5081 return (error);
5082
5083 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5084 if (error != 0) {
5085 dsl_pool_rele(dp, FTAG);
5086 return (error);
5087 }
5088
5089 if (!dsl_dir_is_clone(ds->ds_dir)) {
5090 dsl_dataset_rele(ds, FTAG);
5091 dsl_pool_rele(dp, FTAG);
5092 return (SET_ERROR(EINVAL));
5093 }
5094
5095 error = dsl_dataset_hold_obj(dp,
5096 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5097 if (error != 0) {
5098 dsl_dataset_rele(ds, FTAG);
5099 dsl_pool_rele(dp, FTAG);
5100 return (error);
5101 }
5102
5103 dsl_dataset_name(ods, origin);
5104 dsl_dataset_rele(ods, FTAG);
5105 dsl_dataset_rele(ds, FTAG);
5106 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
5107
5108 /*
5109 * We don't need to unmount *all* the origin fs's snapshots, but
5110 * it's easier.
5111 */
d12f91fd 5112 cp = strchr(origin, '@');
34dc7c2f
BB
5113 if (cp)
5114 *cp = '\0';
d12f91fd 5115 (void) dmu_objset_find(origin,
13fe0198 5116 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
428870ff 5117 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
34dc7c2f
BB
5118}
5119
9babb374
BB
5120/*
5121 * Retrieve a single {user|group}{used|quota}@... property.
5122 *
5123 * inputs:
5124 * zc_name name of filesystem
5125 * zc_objset_type zfs_userquota_prop_t
5126 * zc_value domain name (eg. "S-1-234-567-89")
5127 * zc_guid RID/UID/GID
5128 *
5129 * outputs:
5130 * zc_cookie property value
5131 */
5132static int
5133zfs_ioc_userspace_one(zfs_cmd_t *zc)
5134{
0037b49e 5135 zfsvfs_t *zfsvfs;
9babb374
BB
5136 int error;
5137
5138 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 5139 return (SET_ERROR(EINVAL));
9babb374 5140
f298b24d 5141 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5142 if (error != 0)
9babb374
BB
5143 return (error);
5144
0037b49e 5145 error = zfs_userspace_one(zfsvfs,
9babb374 5146 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
f298b24d 5147 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5148
5149 return (error);
5150}
5151
5152/*
5153 * inputs:
5154 * zc_name name of filesystem
5155 * zc_cookie zap cursor
5156 * zc_objset_type zfs_userquota_prop_t
5157 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5158 *
5159 * outputs:
5160 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5161 * zc_cookie zap cursor
5162 */
5163static int
5164zfs_ioc_userspace_many(zfs_cmd_t *zc)
5165{
0037b49e 5166 zfsvfs_t *zfsvfs;
428870ff 5167 int bufsize = zc->zc_nvlist_dst_size;
3558fd73
BB
5168 int error;
5169 void *buf;
428870ff
BB
5170
5171 if (bufsize <= 0)
2e528b49 5172 return (SET_ERROR(ENOMEM));
9babb374 5173
f298b24d 5174 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5175 if (error != 0)
9babb374
BB
5176 return (error);
5177
2b8cad61 5178 buf = vmem_alloc(bufsize, KM_SLEEP);
9babb374 5179
0037b49e 5180 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
9babb374
BB
5181 buf, &zc->zc_nvlist_dst_size);
5182
5183 if (error == 0) {
5184 error = xcopyout(buf,
5185 (void *)(uintptr_t)zc->zc_nvlist_dst,
5186 zc->zc_nvlist_dst_size);
5187 }
2b8cad61 5188 vmem_free(buf, bufsize);
f298b24d 5189 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5190
5191 return (error);
5192}
5193
5194/*
5195 * inputs:
5196 * zc_name name of filesystem
5197 *
5198 * outputs:
5199 * none
5200 */
5201static int
5202zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5203{
5204 objset_t *os;
428870ff 5205 int error = 0;
0037b49e 5206 zfsvfs_t *zfsvfs;
9babb374 5207
f298b24d 5208 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
0037b49e 5209 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
9babb374
BB
5210 /*
5211 * If userused is not enabled, it may be because the
5212 * objset needs to be closed & reopened (to grow the
5213 * objset_phys_t). Suspend/resume the fs will do that.
5214 */
ec923db2
GM
5215 dsl_dataset_t *ds;
5216
0037b49e
BB
5217 ds = dmu_objset_ds(zfsvfs->z_os);
5218 error = zfs_suspend_fs(zfsvfs);
831baf06 5219 if (error == 0) {
0037b49e 5220 dmu_objset_refresh_ownership(zfsvfs->z_os,
b5256303 5221 B_TRUE, zfsvfs);
0037b49e 5222 error = zfs_resume_fs(zfsvfs, ds);
831baf06 5223 }
9babb374
BB
5224 }
5225 if (error == 0)
0037b49e
BB
5226 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5227 deactivate_super(zfsvfs->z_sb);
9babb374 5228 } else {
428870ff 5229 /* XXX kind of reading contents without owning */
b5256303 5230 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
13fe0198 5231 if (error != 0)
9babb374
BB
5232 return (error);
5233
5234 error = dmu_objset_userspace_upgrade(os);
b5256303 5235 dmu_objset_rele_flags(os, B_TRUE, FTAG);
9babb374
BB
5236 }
5237
5238 return (error);
5239}
5240
1de321e6
JX
5241/*
5242 * inputs:
5243 * zc_name name of filesystem
5244 *
5245 * outputs:
5246 * none
5247 */
5248static int
5249zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc)
5250{
5251 objset_t *os;
5252 int error;
5253
b5256303 5254 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
1de321e6
JX
5255 if (error != 0)
5256 return (error);
5257
5258 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
5259 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5260
5261 if (dmu_objset_userobjspace_upgradable(os)) {
5262 mutex_enter(&os->os_upgrade_lock);
5263 if (os->os_upgrade_id == 0) {
5264 /* clear potential error code and retry */
5265 os->os_upgrade_status = 0;
5266 mutex_exit(&os->os_upgrade_lock);
5267
5268 dmu_objset_userobjspace_upgrade(os);
5269 } else {
5270 mutex_exit(&os->os_upgrade_lock);
5271 }
5272
5273 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5274 error = os->os_upgrade_status;
5275 }
5276
5277 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
b5256303 5278 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
1de321e6
JX
5279
5280 return (error);
5281}
5282
34dc7c2f
BB
5283static int
5284zfs_ioc_share(zfs_cmd_t *zc)
5285{
2e528b49 5286 return (SET_ERROR(ENOSYS));
34dc7c2f
BB
5287}
5288
9babb374
BB
5289ace_t full_access[] = {
5290 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5291};
5292
572e2857
BB
5293/*
5294 * inputs:
5295 * zc_name name of containing filesystem
5296 * zc_obj object # beyond which we want next in-use object #
5297 *
5298 * outputs:
5299 * zc_obj next in-use object #
5300 */
5301static int
5302zfs_ioc_next_obj(zfs_cmd_t *zc)
5303{
5304 objset_t *os = NULL;
5305 int error;
5306
5307 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
13fe0198 5308 if (error != 0)
572e2857
BB
5309 return (error);
5310
7290cd3c 5311 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
572e2857
BB
5312
5313 dmu_objset_rele(os, FTAG);
5314 return (error);
5315}
5316
5317/*
5318 * inputs:
5319 * zc_name name of filesystem
5320 * zc_value prefix name for snapshot
5321 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5322 *
5323 * outputs:
6f1ffb06 5324 * zc_value short name of new snapshot
572e2857
BB
5325 */
5326static int
5327zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5328{
5329 char *snap_name;
13fe0198 5330 char *hold_name;
572e2857 5331 int error;
13fe0198 5332 minor_t minor;
572e2857 5333
13fe0198
MA
5334 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5335 if (error != 0)
572e2857 5336 return (error);
572e2857 5337
13fe0198
MA
5338 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5339 (u_longlong_t)ddi_get_lbolt64());
5340 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5341
5342 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5343 hold_name);
5344 if (error == 0)
680eada9 5345 (void) strlcpy(zc->zc_value, snap_name,
5346 sizeof (zc->zc_value));
572e2857 5347 strfree(snap_name);
13fe0198
MA
5348 strfree(hold_name);
5349 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5350 return (error);
572e2857
BB
5351}
5352
5353/*
5354 * inputs:
5355 * zc_name name of "to" snapshot
5356 * zc_value name of "from" snapshot
5357 * zc_cookie file descriptor to write diff data on
5358 *
5359 * outputs:
5360 * dmu_diff_record_t's to the file descriptor
5361 */
5362static int
5363zfs_ioc_diff(zfs_cmd_t *zc)
5364{
572e2857
BB
5365 file_t *fp;
5366 offset_t off;
5367 int error;
5368
572e2857 5369 fp = getf(zc->zc_cookie);
13fe0198 5370 if (fp == NULL)
2e528b49 5371 return (SET_ERROR(EBADF));
572e2857
BB
5372
5373 off = fp->f_offset;
5374
13fe0198 5375 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
572e2857
BB
5376
5377 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5378 fp->f_offset = off;
5379 releasef(zc->zc_cookie);
5380
572e2857
BB
5381 return (error);
5382}
5383
9babb374
BB
5384/*
5385 * Remove all ACL files in shares dir
5386 */
3c9609b3 5387#ifdef HAVE_SMB_SHARE
9babb374
BB
5388static int
5389zfs_smb_acl_purge(znode_t *dzp)
5390{
5391 zap_cursor_t zc;
5392 zap_attribute_t zap;
0037b49e 5393 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
9babb374
BB
5394 int error;
5395
0037b49e 5396 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
9babb374
BB
5397 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5398 zap_cursor_advance(&zc)) {
5399 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5400 NULL, 0)) != 0)
5401 break;
5402 }
5403 zap_cursor_fini(&zc);
5404 return (error);
5405}
3c9609b3 5406#endif /* HAVE_SMB_SHARE */
9babb374
BB
5407
5408static int
5409zfs_ioc_smb_acl(zfs_cmd_t *zc)
5410{
3c9609b3 5411#ifdef HAVE_SMB_SHARE
9babb374
BB
5412 vnode_t *vp;
5413 znode_t *dzp;
5414 vnode_t *resourcevp = NULL;
5415 znode_t *sharedir;
0037b49e 5416 zfsvfs_t *zfsvfs;
9babb374
BB
5417 nvlist_t *nvlist;
5418 char *src, *target;
5419 vattr_t vattr;
5420 vsecattr_t vsec;
5421 int error = 0;
5422
5423 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5424 NO_FOLLOW, NULL, &vp)) != 0)
5425 return (error);
5426
5427 /* Now make sure mntpnt and dataset are ZFS */
5428
5429 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5430 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5431 zc->zc_name) != 0)) {
5432 VN_RELE(vp);
2e528b49 5433 return (SET_ERROR(EINVAL));
9babb374
BB
5434 }
5435
5436 dzp = VTOZ(vp);
0037b49e
BB
5437 zfsvfs = ZTOZSB(dzp);
5438 ZFS_ENTER(zfsvfs);
9babb374
BB
5439
5440 /*
5441 * Create share dir if its missing.
5442 */
0037b49e
BB
5443 mutex_enter(&zfsvfs->z_lock);
5444 if (zfsvfs->z_shares_dir == 0) {
9babb374
BB
5445 dmu_tx_t *tx;
5446
0037b49e 5447 tx = dmu_tx_create(zfsvfs->z_os);
9babb374
BB
5448 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5449 ZFS_SHARES_DIR);
5450 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5451 error = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 5452 if (error != 0) {
9babb374
BB
5453 dmu_tx_abort(tx);
5454 } else {
0037b49e 5455 error = zfs_create_share_dir(zfsvfs, tx);
9babb374
BB
5456 dmu_tx_commit(tx);
5457 }
13fe0198 5458 if (error != 0) {
0037b49e 5459 mutex_exit(&zfsvfs->z_lock);
9babb374 5460 VN_RELE(vp);
0037b49e 5461 ZFS_EXIT(zfsvfs);
9babb374
BB
5462 return (error);
5463 }
5464 }
0037b49e 5465 mutex_exit(&zfsvfs->z_lock);
9babb374 5466
0037b49e
BB
5467 ASSERT(zfsvfs->z_shares_dir);
5468 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
9babb374 5469 VN_RELE(vp);
0037b49e 5470 ZFS_EXIT(zfsvfs);
9babb374
BB
5471 return (error);
5472 }
5473
5474 switch (zc->zc_cookie) {
5475 case ZFS_SMB_ACL_ADD:
5476 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
9babb374
BB
5477 vattr.va_mode = S_IFREG|0777;
5478 vattr.va_uid = 0;
5479 vattr.va_gid = 0;
5480
5481 vsec.vsa_mask = VSA_ACE;
5482 vsec.vsa_aclentp = &full_access;
5483 vsec.vsa_aclentsz = sizeof (full_access);
5484 vsec.vsa_aclcnt = 1;
5485
5486 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5487 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5488 if (resourcevp)
5489 VN_RELE(resourcevp);
5490 break;
5491
5492 case ZFS_SMB_ACL_REMOVE:
5493 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5494 NULL, 0);
5495 break;
5496
5497 case ZFS_SMB_ACL_RENAME:
5498 if ((error = get_nvlist(zc->zc_nvlist_src,
5499 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5500 VN_RELE(vp);
a4d179ef 5501 VN_RELE(ZTOV(sharedir));
0037b49e 5502 ZFS_EXIT(zfsvfs);
9babb374
BB
5503 return (error);
5504 }
5505 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5506 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5507 &target)) {
5508 VN_RELE(vp);
5509 VN_RELE(ZTOV(sharedir));
0037b49e 5510 ZFS_EXIT(zfsvfs);
428870ff 5511 nvlist_free(nvlist);
9babb374
BB
5512 return (error);
5513 }
5514 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5515 kcred, NULL, 0);
5516 nvlist_free(nvlist);
5517 break;
5518
5519 case ZFS_SMB_ACL_PURGE:
5520 error = zfs_smb_acl_purge(sharedir);
5521 break;
5522
5523 default:
2e528b49 5524 error = SET_ERROR(EINVAL);
9babb374
BB
5525 break;
5526 }
5527
5528 VN_RELE(vp);
5529 VN_RELE(ZTOV(sharedir));
5530
0037b49e 5531 ZFS_EXIT(zfsvfs);
9babb374
BB
5532
5533 return (error);
325f0235 5534#else
2e528b49 5535 return (SET_ERROR(ENOTSUP));
3c9609b3 5536#endif /* HAVE_SMB_SHARE */
9babb374
BB
5537}
5538
45d1cae3 5539/*
13fe0198
MA
5540 * innvl: {
5541 * "holds" -> { snapname -> holdname (string), ... }
5542 * (optional) "cleanup_fd" -> fd (int32)
5543 * }
45d1cae3 5544 *
13fe0198
MA
5545 * outnvl: {
5546 * snapname -> error value (int32)
5547 * ...
5548 * }
45d1cae3 5549 */
13fe0198 5550/* ARGSUSED */
45d1cae3 5551static int
13fe0198 5552zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
45d1cae3 5553{
fc581e05 5554 nvpair_t *pair;
13fe0198
MA
5555 nvlist_t *holds;
5556 int cleanup_fd = -1;
572e2857
BB
5557 int error;
5558 minor_t minor = 0;
45d1cae3 5559
13fe0198
MA
5560 error = nvlist_lookup_nvlist(args, "holds", &holds);
5561 if (error != 0)
2e528b49 5562 return (SET_ERROR(EINVAL));
572e2857 5563
fc581e05
JJS
5564 /* make sure the user didn't pass us any invalid (empty) tags */
5565 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5566 pair = nvlist_next_nvpair(holds, pair)) {
5567 char *htag;
5568
5569 error = nvpair_value_string(pair, &htag);
5570 if (error != 0)
5571 return (SET_ERROR(error));
5572
5573 if (strlen(htag) == 0)
5574 return (SET_ERROR(EINVAL));
5575 }
5576
13fe0198
MA
5577 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5578 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5579 if (error != 0)
572e2857 5580 return (error);
572e2857 5581 }
572e2857 5582
13fe0198
MA
5583 error = dsl_dataset_user_hold(holds, minor, errlist);
5584 if (minor != 0)
5585 zfs_onexit_fd_rele(cleanup_fd);
572e2857 5586 return (error);
45d1cae3
BB
5587}
5588
5589/*
13fe0198 5590 * innvl is not used.
45d1cae3 5591 *
13fe0198
MA
5592 * outnvl: {
5593 * holdname -> time added (uint64 seconds since epoch)
5594 * ...
5595 * }
45d1cae3 5596 */
13fe0198 5597/* ARGSUSED */
45d1cae3 5598static int
13fe0198 5599zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
45d1cae3 5600{
bec1067d 5601 ASSERT3P(args, ==, NULL);
13fe0198 5602 return (dsl_dataset_get_holds(snapname, outnvl));
45d1cae3
BB
5603}
5604
5605/*
13fe0198
MA
5606 * innvl: {
5607 * snapname -> { holdname, ... }
5608 * ...
5609 * }
45d1cae3 5610 *
13fe0198
MA
5611 * outnvl: {
5612 * snapname -> error value (int32)
5613 * ...
5614 * }
45d1cae3 5615 */
13fe0198 5616/* ARGSUSED */
45d1cae3 5617static int
13fe0198 5618zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
45d1cae3 5619{
13fe0198 5620 return (dsl_dataset_user_release(holds, errlist));
45d1cae3
BB
5621}
5622
26685276
BB
5623/*
5624 * inputs:
5625 * zc_guid flags (ZEVENT_NONBLOCK)
9b101a73 5626 * zc_cleanup_fd zevent file descriptor
26685276
BB
5627 *
5628 * outputs:
5629 * zc_nvlist_dst next nvlist event
5630 * zc_cookie dropped events since last get
26685276
BB
5631 */
5632static int
5633zfs_ioc_events_next(zfs_cmd_t *zc)
5634{
5635 zfs_zevent_t *ze;
5636 nvlist_t *event = NULL;
5637 minor_t minor;
5638 uint64_t dropped = 0;
5639 int error;
5640
5641 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5642 if (error != 0)
5643 return (error);
5644
5645 do {
baa40d45 5646 error = zfs_zevent_next(ze, &event,
02730c33 5647 &zc->zc_nvlist_dst_size, &dropped);
26685276
BB
5648 if (event != NULL) {
5649 zc->zc_cookie = dropped;
5650 error = put_nvlist(zc, event);
baa40d45 5651 nvlist_free(event);
26685276
BB
5652 }
5653
5654 if (zc->zc_guid & ZEVENT_NONBLOCK)
5655 break;
5656
5657 if ((error == 0) || (error != ENOENT))
5658 break;
5659
5660 error = zfs_zevent_wait(ze);
13fe0198 5661 if (error != 0)
26685276
BB
5662 break;
5663 } while (1);
5664
5665 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5666
5667 return (error);
5668}
5669
5670/*
5671 * outputs:
5672 * zc_cookie cleared events count
5673 */
5674static int
5675zfs_ioc_events_clear(zfs_cmd_t *zc)
5676{
5677 int count;
5678
5679 zfs_zevent_drain_all(&count);
5680 zc->zc_cookie = count;
5681
d1d7e268 5682 return (0);
26685276
BB
5683}
5684
75e3ff58
BB
5685/*
5686 * inputs:
5687 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5688 * zc_cleanup zevent file descriptor
5689 */
5690static int
5691zfs_ioc_events_seek(zfs_cmd_t *zc)
5692{
5693 zfs_zevent_t *ze;
5694 minor_t minor;
5695 int error;
5696
5697 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5698 if (error != 0)
5699 return (error);
5700
5701 error = zfs_zevent_seek(ze, zc->zc_guid);
5702 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5703
5704 return (error);
5705}
5706
330d06f9
MA
5707/*
5708 * inputs:
5709 * zc_name name of new filesystem or snapshot
5710 * zc_value full name of old snapshot
5711 *
5712 * outputs:
5713 * zc_cookie space in bytes
5714 * zc_objset_type compressed space in bytes
5715 * zc_perm_action uncompressed space in bytes
5716 */
5717static int
5718zfs_ioc_space_written(zfs_cmd_t *zc)
5719{
5720 int error;
13fe0198 5721 dsl_pool_t *dp;
330d06f9
MA
5722 dsl_dataset_t *new, *old;
5723
13fe0198 5724 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
330d06f9
MA
5725 if (error != 0)
5726 return (error);
13fe0198
MA
5727 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5728 if (error != 0) {
5729 dsl_pool_rele(dp, FTAG);
5730 return (error);
5731 }
5732 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
330d06f9
MA
5733 if (error != 0) {
5734 dsl_dataset_rele(new, FTAG);
13fe0198 5735 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5736 return (error);
5737 }
5738
5739 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5740 &zc->zc_objset_type, &zc->zc_perm_action);
5741 dsl_dataset_rele(old, FTAG);
5742 dsl_dataset_rele(new, FTAG);
13fe0198 5743 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5744 return (error);
5745}
5746
5747/*
6f1ffb06
MA
5748 * innvl: {
5749 * "firstsnap" -> snapshot name
5750 * }
330d06f9 5751 *
6f1ffb06
MA
5752 * outnvl: {
5753 * "used" -> space in bytes
5754 * "compressed" -> compressed space in bytes
5755 * "uncompressed" -> uncompressed space in bytes
5756 * }
330d06f9
MA
5757 */
5758static int
6f1ffb06 5759zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
330d06f9
MA
5760{
5761 int error;
13fe0198 5762 dsl_pool_t *dp;
330d06f9 5763 dsl_dataset_t *new, *old;
6f1ffb06
MA
5764 char *firstsnap;
5765 uint64_t used, comp, uncomp;
330d06f9 5766
6f1ffb06 5767 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
2e528b49 5768 return (SET_ERROR(EINVAL));
6f1ffb06 5769
13fe0198 5770 error = dsl_pool_hold(lastsnap, FTAG, &dp);
330d06f9
MA
5771 if (error != 0)
5772 return (error);
13fe0198
MA
5773
5774 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
71e2fe41
AG
5775 if (error == 0 && !new->ds_is_snapshot) {
5776 dsl_dataset_rele(new, FTAG);
5777 error = SET_ERROR(EINVAL);
5778 }
13fe0198
MA
5779 if (error != 0) {
5780 dsl_pool_rele(dp, FTAG);
5781 return (error);
5782 }
5783 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
71e2fe41
AG
5784 if (error == 0 && !old->ds_is_snapshot) {
5785 dsl_dataset_rele(old, FTAG);
5786 error = SET_ERROR(EINVAL);
5787 }
330d06f9
MA
5788 if (error != 0) {
5789 dsl_dataset_rele(new, FTAG);
13fe0198 5790 dsl_pool_rele(dp, FTAG);
330d06f9
MA
5791 return (error);
5792 }
5793
6f1ffb06 5794 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
330d06f9
MA
5795 dsl_dataset_rele(old, FTAG);
5796 dsl_dataset_rele(new, FTAG);
13fe0198 5797 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
5798 fnvlist_add_uint64(outnvl, "used", used);
5799 fnvlist_add_uint64(outnvl, "compressed", comp);
5800 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
330d06f9
MA
5801 return (error);
5802}
5803
34dc7c2f 5804/*
6f1ffb06
MA
5805 * innvl: {
5806 * "fd" -> file descriptor to write stream to (int32)
5807 * (optional) "fromsnap" -> full snap name to send an incremental from
f1512ee6
MA
5808 * (optional) "largeblockok" -> (value ignored)
5809 * indicates that blocks > 128KB are permitted
9b67f605
MA
5810 * (optional) "embedok" -> (value ignored)
5811 * presence indicates DRR_WRITE_EMBEDDED records are permitted
2aa34383
DK
5812 * (optional) "compressok" -> (value ignored)
5813 * presence indicates compressed DRR_WRITE records are permitted
b5256303
TC
5814 * (optional) "rawok" -> (value ignored)
5815 * presence indicates raw encrypted records should be used.
47dfff3b
MA
5816 * (optional) "resume_object" and "resume_offset" -> (uint64)
5817 * if present, resume send stream from specified object and offset.
6f1ffb06
MA
5818 * }
5819 *
5820 * outnvl is unused
34dc7c2f 5821 */
6f1ffb06
MA
5822/* ARGSUSED */
5823static int
5824zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5825{
6f1ffb06
MA
5826 int error;
5827 offset_t off;
13fe0198 5828 char *fromname = NULL;
6f1ffb06 5829 int fd;
13fe0198 5830 file_t *fp;
f1512ee6 5831 boolean_t largeblockok;
9b67f605 5832 boolean_t embedok;
2aa34383 5833 boolean_t compressok;
b5256303 5834 boolean_t rawok;
47dfff3b
MA
5835 uint64_t resumeobj = 0;
5836 uint64_t resumeoff = 0;
6f1ffb06
MA
5837
5838 error = nvlist_lookup_int32(innvl, "fd", &fd);
5839 if (error != 0)
2e528b49 5840 return (SET_ERROR(EINVAL));
6f1ffb06 5841
13fe0198 5842 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6f1ffb06 5843
f1512ee6 5844 largeblockok = nvlist_exists(innvl, "largeblockok");
9b67f605 5845 embedok = nvlist_exists(innvl, "embedok");
2aa34383 5846 compressok = nvlist_exists(innvl, "compressok");
b5256303 5847 rawok = nvlist_exists(innvl, "rawok");
9b67f605 5848
47dfff3b
MA
5849 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5850 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5851
13fe0198 5852 if ((fp = getf(fd)) == NULL)
2e528b49 5853 return (SET_ERROR(EBADF));
6f1ffb06
MA
5854
5855 off = fp->f_offset;
2aa34383 5856 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
b5256303 5857 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6f1ffb06
MA
5858
5859 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5860 fp->f_offset = off;
13fe0198 5861
6f1ffb06 5862 releasef(fd);
6f1ffb06
MA
5863 return (error);
5864}
5865
5866/*
5867 * Determine approximately how large a zfs send stream will be -- the number
5868 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5869 *
5870 * innvl: {
5dc8b736
MG
5871 * (optional) "from" -> full snap or bookmark name to send an incremental
5872 * from
2aa34383
DK
5873 * (optional) "largeblockok" -> (value ignored)
5874 * indicates that blocks > 128KB are permitted
5875 * (optional) "embedok" -> (value ignored)
5876 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5877 * (optional) "compressok" -> (value ignored)
5878 * presence indicates compressed DRR_WRITE records are permitted
6f1ffb06
MA
5879 * }
5880 *
5881 * outnvl: {
5882 * "space" -> bytes of space (uint64)
5883 * }
5884 */
5885static int
5886zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5887{
13fe0198 5888 dsl_pool_t *dp;
13fe0198 5889 dsl_dataset_t *tosnap;
6f1ffb06
MA
5890 int error;
5891 char *fromname;
2aa34383
DK
5892 /* LINTED E_FUNC_SET_NOT_USED */
5893 boolean_t largeblockok;
5894 /* LINTED E_FUNC_SET_NOT_USED */
5895 boolean_t embedok;
5896 boolean_t compressok;
b5256303 5897 boolean_t rawok;
6f1ffb06
MA
5898 uint64_t space;
5899
13fe0198
MA
5900 error = dsl_pool_hold(snapname, FTAG, &dp);
5901 if (error != 0)
6f1ffb06
MA
5902 return (error);
5903
13fe0198
MA
5904 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5905 if (error != 0) {
5906 dsl_pool_rele(dp, FTAG);
5907 return (error);
5908 }
5909
2aa34383
DK
5910 largeblockok = nvlist_exists(innvl, "largeblockok");
5911 embedok = nvlist_exists(innvl, "embedok");
5912 compressok = nvlist_exists(innvl, "compressok");
b5256303 5913 rawok = nvlist_exists(innvl, "rawok");
2aa34383 5914
5dc8b736 5915 error = nvlist_lookup_string(innvl, "from", &fromname);
6f1ffb06 5916 if (error == 0) {
5dc8b736
MG
5917 if (strchr(fromname, '@') != NULL) {
5918 /*
5919 * If from is a snapshot, hold it and use the more
5920 * efficient dmu_send_estimate to estimate send space
5921 * size using deadlists.
5922 */
5923 dsl_dataset_t *fromsnap;
5924 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5925 if (error != 0)
5926 goto out;
b5256303
TC
5927 error = dmu_send_estimate(tosnap, fromsnap,
5928 compressok || rawok, &space);
5dc8b736
MG
5929 dsl_dataset_rele(fromsnap, FTAG);
5930 } else if (strchr(fromname, '#') != NULL) {
5931 /*
5932 * If from is a bookmark, fetch the creation TXG of the
5933 * snapshot it was created from and use that to find
5934 * blocks that were born after it.
5935 */
5936 zfs_bookmark_phys_t frombm;
5937
5938 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5939 &frombm);
5940 if (error != 0)
5941 goto out;
5942 error = dmu_send_estimate_from_txg(tosnap,
b5256303
TC
5943 frombm.zbm_creation_txg, compressok || rawok,
5944 &space);
5dc8b736
MG
5945 } else {
5946 /*
5947 * from is not properly formatted as a snapshot or
5948 * bookmark
5949 */
5950 error = SET_ERROR(EINVAL);
5951 goto out;
6f1ffb06 5952 }
5dc8b736
MG
5953 } else {
5954 // If estimating the size of a full send, use dmu_send_estimate
b5256303
TC
5955 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
5956 &space);
6f1ffb06
MA
5957 }
5958
6f1ffb06
MA
5959 fnvlist_add_uint64(outnvl, "space", space);
5960
5dc8b736 5961out:
13fe0198
MA
5962 dsl_dataset_rele(tosnap, FTAG);
5963 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
5964 return (error);
5965}
5966
bec1067d
AP
5967/*
5968 * Sync the currently open TXG to disk for the specified pool.
5969 * This is somewhat similar to 'zfs_sync()'.
5970 * For cases that do not result in error this ioctl will wait for
5971 * the currently open TXG to commit before returning back to the caller.
5972 *
5973 * innvl: {
5974 * "force" -> when true, force uberblock update even if there is no dirty data.
5975 * In addition this will cause the vdev configuration to be written
5976 * out including updating the zpool cache file. (boolean_t)
5977 * }
5978 *
5979 * onvl is unused
5980 */
5981/* ARGSUSED */
5982static int
5983zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
5984{
5985 int err;
5986 boolean_t force;
5987 spa_t *spa;
5988
5989 if ((err = spa_open(pool, &spa, FTAG)) != 0)
5990 return (err);
5991
5992 force = fnvlist_lookup_boolean_value(innvl, "force");
5993 if (force) {
5994 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
5995 vdev_config_dirty(spa->spa_root_vdev);
5996 spa_config_exit(spa, SCL_CONFIG, FTAG);
5997 }
5998 txg_wait_synced(spa_get_dsl(spa), 0);
5999
6000 spa_close(spa, FTAG);
6001
6002 return (err);
6003}
6004
b5256303
TC
6005/*
6006 * Load a user's wrapping key into the kernel.
6007 * innvl: {
6008 * "hidden_args" -> { "wkeydata" -> value }
6009 * raw uint8_t array of encryption wrapping key data (32 bytes)
6010 * (optional) "noop" -> (value ignored)
6011 * presence indicated key should only be verified, not loaded
6012 * }
6013 */
6014/* ARGSUSED */
6015static int
6016zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6017{
6018 int ret;
6019 dsl_crypto_params_t *dcp = NULL;
6020 nvlist_t *hidden_args;
6021 boolean_t noop = nvlist_exists(innvl, "noop");
6022
6023 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6024 ret = SET_ERROR(EINVAL);
6025 goto error;
6026 }
6027
6028 ret = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6029 if (ret != 0) {
6030 ret = SET_ERROR(EINVAL);
6031 goto error;
6032 }
6033
6034 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6035 hidden_args, &dcp);
6036 if (ret != 0)
6037 goto error;
6038
6039 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6040 if (ret != 0)
6041 goto error;
6042
6043 dsl_crypto_params_free(dcp, noop);
6044
6045 return (0);
6046
6047error:
6048 dsl_crypto_params_free(dcp, B_TRUE);
6049 return (ret);
6050}
6051
6052/*
6053 * Unload a user's wrapping key from the kernel.
6054 * Both innvl and outnvl are unused.
6055 */
6056/* ARGSUSED */
6057static int
6058zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6059{
6060 int ret = 0;
6061
6062 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6063 ret = (SET_ERROR(EINVAL));
6064 goto out;
6065 }
6066
6067 ret = spa_keystore_unload_wkey(dsname);
6068 if (ret != 0)
6069 goto out;
6070
6071out:
6072 return (ret);
6073}
6074
6075/*
6076 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6077 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6078 * here to change how the key is derived in userspace.
6079 *
6080 * innvl: {
6081 * "hidden_args" (optional) -> { "wkeydata" -> value }
6082 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6083 * "props" (optional) -> { prop -> value }
6084 * }
6085 *
6086 * outnvl is unused
6087 */
6088/* ARGSUSED */
6089static int
6090zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6091{
6092 int ret;
6093 uint64_t cmd = DCP_CMD_NONE;
6094 dsl_crypto_params_t *dcp = NULL;
6095 nvlist_t *args = NULL, *hidden_args = NULL;
6096
6097 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6098 ret = (SET_ERROR(EINVAL));
6099 goto error;
6100 }
6101
6102 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6103 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6104 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6105
6106 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6107 if (ret != 0)
6108 goto error;
6109
6110 ret = spa_keystore_change_key(dsname, dcp);
6111 if (ret != 0)
6112 goto error;
6113
6114 dsl_crypto_params_free(dcp, B_FALSE);
6115
6116 return (0);
6117
6118error:
6119 dsl_crypto_params_free(dcp, B_TRUE);
6120 return (ret);
6121}
6122
6f1ffb06
MA
6123static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6124
6125static void
6126zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6127 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6128 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6129{
6130 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6131
6132 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6133 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6134 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6135 ASSERT3P(vec->zvec_func, ==, NULL);
6136
6137 vec->zvec_legacy_func = func;
6138 vec->zvec_secpolicy = secpolicy;
6139 vec->zvec_namecheck = namecheck;
6140 vec->zvec_allow_log = log_history;
6141 vec->zvec_pool_check = pool_check;
6142}
6143
6144/*
6145 * See the block comment at the beginning of this file for details on
6146 * each argument to this function.
6147 */
6148static void
6149zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6150 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6151 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6152 boolean_t allow_log)
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 /* if we are logging, the name must be valid */
6162 ASSERT(!allow_log || namecheck != NO_NAME);
6163
6164 vec->zvec_name = name;
6165 vec->zvec_func = func;
6166 vec->zvec_secpolicy = secpolicy;
6167 vec->zvec_namecheck = namecheck;
6168 vec->zvec_pool_check = pool_check;
6169 vec->zvec_smush_outnvlist = smush_outnvlist;
6170 vec->zvec_allow_log = allow_log;
6171}
6172
6173static void
6174zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6175 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6176 zfs_ioc_poolcheck_t pool_check)
6177{
6178 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6179 POOL_NAME, log_history, pool_check);
6180}
6181
6182static void
6183zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6184 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6185{
6186 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6187 DATASET_NAME, B_FALSE, pool_check);
6188}
6189
6190static void
6191zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6192{
6193 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6194 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6195}
6196
6197static void
6198zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6199 zfs_secpolicy_func_t *secpolicy)
6200{
6201 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6202 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6203}
6204
6205static void
6206zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6207 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6208{
6209 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6210 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6211}
6212
6213static void
6214zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6215{
6216 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6217 zfs_secpolicy_read);
6218}
6219
6220static void
6221zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
e9aa730c 6222 zfs_secpolicy_func_t *secpolicy)
6f1ffb06
MA
6223{
6224 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6225 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6226}
6227
6228static void
6229zfs_ioctl_init(void)
6230{
6231 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6232 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6233 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6234
6235 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6236 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6237 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6238
6239 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6240 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6241 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6242
6243 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6244 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6245 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6246
6247 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6248 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6249 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6250
6251 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6252 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6253 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6254
6255 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6256 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6257 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6258
6259 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6260 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6261 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6262
13fe0198
MA
6263 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6264 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6265 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6266 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6267 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6268 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6269
6270 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6271 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6272 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6273
46ba1e59
MA
6274 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6275 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6276 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6277
da536844
MA
6278 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6279 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6280 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6281
6282 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6283 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6284 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6285
6286 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6287 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6288 POOL_NAME,
6289 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6290
43e52edd
BB
6291 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6292 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6293 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
b5256303
TC
6294 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6295 zfs_ioc_load_key, zfs_secpolicy_load_key,
6296 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6297 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6298 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6299 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6300 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6301 zfs_ioc_change_key, zfs_secpolicy_change_key,
6302 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6303 B_TRUE, B_TRUE);
43e52edd 6304
bec1067d
AP
6305 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6306 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6307 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6308
6f1ffb06
MA
6309 /* IOCTLS that use the legacy function signature */
6310
6311 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6312 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6313
6314 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6315 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6316 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6317 zfs_ioc_pool_scan);
6318 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6319 zfs_ioc_pool_upgrade);
6320 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6321 zfs_ioc_vdev_add);
6322 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6323 zfs_ioc_vdev_remove);
6324 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6325 zfs_ioc_vdev_set_state);
6326 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6327 zfs_ioc_vdev_attach);
6328 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6329 zfs_ioc_vdev_detach);
6330 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6331 zfs_ioc_vdev_setpath);
6332 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6333 zfs_ioc_vdev_setfru);
6334 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6335 zfs_ioc_pool_set_props);
6336 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6337 zfs_ioc_vdev_split);
6338 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6339 zfs_ioc_pool_reguid);
6340
6341 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6342 zfs_ioc_pool_configs, zfs_secpolicy_none);
6343 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6344 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6345 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6346 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6347 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6348 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6349 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6350 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6351
6352 /*
6353 * pool destroy, and export don't log the history as part of
6354 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6355 * does the logging of those commands.
6356 */
6357 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
87a63dd7 6358 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06 6359 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
87a63dd7 6360 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06
MA
6361
6362 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6363 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6364 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6365 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6366
6367 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6368 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6369 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6370 zfs_ioc_dsobj_to_dsname,
6371 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6372 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6373 zfs_ioc_pool_get_history,
6374 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6375
6376 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6377 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6378
6379 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
92e43c17 6380 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6f1ffb06
MA
6381 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6382 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
6383
6384 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6385 zfs_ioc_space_written);
6f1ffb06
MA
6386 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6387 zfs_ioc_objset_recvd_props);
6388 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6389 zfs_ioc_next_obj);
6390 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6391 zfs_ioc_get_fsacl);
6392 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6393 zfs_ioc_objset_stats);
6394 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6395 zfs_ioc_objset_zplprops);
6396 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6397 zfs_ioc_dataset_list_next);
6398 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6399 zfs_ioc_snapshot_list_next);
6400 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6401 zfs_ioc_send_progress);
6402
6403 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6404 zfs_ioc_diff, zfs_secpolicy_diff);
6405 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6406 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6407 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6408 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6409 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6410 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6411 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6412 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6413 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6414 zfs_ioc_send, zfs_secpolicy_send);
6415
6416 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6417 zfs_secpolicy_none);
6418 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6419 zfs_secpolicy_destroy);
6f1ffb06
MA
6420 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6421 zfs_secpolicy_rename);
6422 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6423 zfs_secpolicy_recv);
6424 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6425 zfs_secpolicy_promote);
6f1ffb06
MA
6426 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6427 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6428 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6429 zfs_secpolicy_set_fsacl);
6430
6431 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6432 zfs_secpolicy_share, POOL_CHECK_NONE);
6433 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6434 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6435 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6436 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6437 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6438 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6439 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6440 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6441
6442 /*
ba6a2402 6443 * ZoL functions
6f1ffb06 6444 */
6f1ffb06
MA
6445 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
6446 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6447 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
6448 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
75e3ff58
BB
6449 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
6450 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6f1ffb06 6451}
34dc7c2f 6452
9babb374 6453int
572e2857
BB
6454pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6455 zfs_ioc_poolcheck_t check)
9babb374
BB
6456{
6457 spa_t *spa;
6458 int error;
6459
6460 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6461
572e2857
BB
6462 if (check & POOL_CHECK_NONE)
6463 return (0);
6464
9babb374
BB
6465 error = spa_open(name, &spa, FTAG);
6466 if (error == 0) {
572e2857 6467 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
2e528b49 6468 error = SET_ERROR(EAGAIN);
572e2857 6469 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
2e528b49 6470 error = SET_ERROR(EROFS);
9babb374
BB
6471 spa_close(spa, FTAG);
6472 }
6473 return (error);
6474}
6475
325f0235
BB
6476static void *
6477zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
6478{
6479 zfsdev_state_t *zs;
6480
3937ab20 6481 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
325f0235 6482 if (zs->zs_minor == minor) {
3937ab20 6483 smp_rmb();
325f0235 6484 switch (which) {
d1d7e268
MK
6485 case ZST_ONEXIT:
6486 return (zs->zs_onexit);
6487 case ZST_ZEVENT:
6488 return (zs->zs_zevent);
6489 case ZST_ALL:
6490 return (zs);
325f0235
BB
6491 }
6492 }
6493 }
6494
d1d7e268 6495 return (NULL);
325f0235
BB
6496}
6497
6498void *
6499zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
6500{
6501 void *ptr;
6502
325f0235 6503 ptr = zfsdev_get_state_impl(minor, which);
325f0235 6504
d1d7e268 6505 return (ptr);
325f0235
BB
6506}
6507
72540ea3
RY
6508int
6509zfsdev_getminor(struct file *filp, minor_t *minorp)
325f0235 6510{
72540ea3
RY
6511 zfsdev_state_t *zs, *fpd;
6512
325f0235 6513 ASSERT(filp != NULL);
72540ea3
RY
6514 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
6515
6516 fpd = filp->private_data;
6517 if (fpd == NULL)
ecb2b7dc 6518 return (SET_ERROR(EBADF));
72540ea3
RY
6519
6520 mutex_enter(&zfsdev_state_lock);
6521
6522 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6523
6524 if (zs->zs_minor == -1)
6525 continue;
6526
6527 if (fpd == zs) {
6528 *minorp = fpd->zs_minor;
6529 mutex_exit(&zfsdev_state_lock);
6530 return (0);
6531 }
6532 }
6533
6534 mutex_exit(&zfsdev_state_lock);
325f0235 6535
ecb2b7dc 6536 return (SET_ERROR(EBADF));
325f0235
BB
6537}
6538
572e2857 6539/*
325f0235
BB
6540 * Find a free minor number. The zfsdev_state_list is expected to
6541 * be short since it is only a list of currently open file handles.
572e2857
BB
6542 */
6543minor_t
6544zfsdev_minor_alloc(void)
6545{
325f0235 6546 static minor_t last_minor = 0;
572e2857
BB
6547 minor_t m;
6548
6549 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6550
6551 for (m = last_minor + 1; m != last_minor; m++) {
6552 if (m > ZFSDEV_MAX_MINOR)
6553 m = 1;
325f0235 6554 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
572e2857
BB
6555 last_minor = m;
6556 return (m);
6557 }
6558 }
6559
6560 return (0);
6561}
6562
6563static int
325f0235 6564zfsdev_state_init(struct file *filp)
572e2857 6565{
3937ab20 6566 zfsdev_state_t *zs, *zsprev = NULL;
572e2857 6567 minor_t minor;
3937ab20 6568 boolean_t newzs = B_FALSE;
572e2857
BB
6569
6570 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
572e2857 6571
d1d7e268
MK
6572 minor = zfsdev_minor_alloc();
6573 if (minor == 0)
6574 return (SET_ERROR(ENXIO));
325f0235 6575
3937ab20
TC
6576 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6577 if (zs->zs_minor == -1)
6578 break;
6579 zsprev = zs;
6580 }
6581
6582 if (!zs) {
6583 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6584 newzs = B_TRUE;
6585 }
572e2857 6586
325f0235 6587 zs->zs_file = filp;
325f0235 6588 filp->private_data = zs;
572e2857 6589
325f0235
BB
6590 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
6591 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
572e2857 6592
3937ab20
TC
6593
6594 /*
6595 * In order to provide for lock-free concurrent read access
6596 * to the minor list in zfsdev_get_state_impl(), new entries
6597 * must be completely written before linking them into the
6598 * list whereas existing entries are already linked; the last
6599 * operation must be updating zs_minor (from -1 to the new
6600 * value).
6601 */
6602 if (newzs) {
6603 zs->zs_minor = minor;
6604 smp_wmb();
6605 zsprev->zs_next = zs;
6606 } else {
6607 smp_wmb();
6608 zs->zs_minor = minor;
6609 }
572e2857
BB
6610
6611 return (0);
6612}
6613
325f0235
BB
6614static int
6615zfsdev_state_destroy(struct file *filp)
572e2857 6616{
325f0235 6617 zfsdev_state_t *zs;
572e2857 6618
325f0235
BB
6619 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6620 ASSERT(filp->private_data != NULL);
572e2857 6621
325f0235 6622 zs = filp->private_data;
3937ab20 6623 zs->zs_minor = -1;
325f0235
BB
6624 zfs_onexit_destroy(zs->zs_onexit);
6625 zfs_zevent_destroy(zs->zs_zevent);
572e2857 6626
d1d7e268 6627 return (0);
572e2857
BB
6628}
6629
6630static int
325f0235 6631zfsdev_open(struct inode *ino, struct file *filp)
572e2857 6632{
325f0235 6633 int error;
572e2857 6634
325f0235
BB
6635 mutex_enter(&zfsdev_state_lock);
6636 error = zfsdev_state_init(filp);
6637 mutex_exit(&zfsdev_state_lock);
572e2857 6638
325f0235 6639 return (-error);
572e2857
BB
6640}
6641
6642static int
325f0235 6643zfsdev_release(struct inode *ino, struct file *filp)
572e2857 6644{
325f0235 6645 int error;
572e2857
BB
6646
6647 mutex_enter(&zfsdev_state_lock);
325f0235 6648 error = zfsdev_state_destroy(filp);
572e2857
BB
6649 mutex_exit(&zfsdev_state_lock);
6650
325f0235 6651 return (-error);
572e2857
BB
6652}
6653
325f0235
BB
6654static long
6655zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f
BB
6656{
6657 zfs_cmd_t *zc;
6f1ffb06 6658 uint_t vecnum;
4fd762f8 6659 int error, rc, flag = 0;
6f1ffb06 6660 const zfs_ioc_vec_t *vec;
fb8e608d 6661 char *saved_poolname = NULL;
6f1ffb06 6662 nvlist_t *innvl = NULL;
40d06e3c 6663 fstrans_cookie_t cookie;
6f1ffb06
MA
6664
6665 vecnum = cmd - ZFS_IOC_FIRST;
6666 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
2e528b49 6667 return (-SET_ERROR(EINVAL));
6f1ffb06 6668 vec = &zfs_ioc_vec[vecnum];
34dc7c2f 6669
2e0358cb
BB
6670 /*
6671 * The registered ioctl list may be sparse, verify that either
6672 * a normal or legacy handler are registered.
6673 */
6674 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
6675 return (-SET_ERROR(EINVAL));
6676
efcd79a8 6677 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
34dc7c2f 6678
9babb374 6679 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6f1ffb06 6680 if (error != 0) {
2e528b49 6681 error = SET_ERROR(EFAULT);
6f1ffb06
MA
6682 goto out;
6683 }
34dc7c2f 6684
6f1ffb06 6685 zc->zc_iflags = flag & FKIOCTL;
f74b821a
BB
6686 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
6687 /*
6688 * Make sure the user doesn't pass in an insane value for
6689 * zc_nvlist_src_size. We have to check, since we will end
6690 * up allocating that much memory inside of get_nvlist(). This
6691 * prevents a nefarious user from allocating tons of kernel
6692 * memory.
6693 *
6694 * Also, we return EINVAL instead of ENOMEM here. The reason
6695 * being that returning ENOMEM from an ioctl() has a special
6696 * connotation; that the user's size value is too small and
6697 * needs to be expanded to hold the nvlist. See
6698 * zcmd_expand_dst_nvlist() for details.
6699 */
6700 error = SET_ERROR(EINVAL); /* User's size too big */
6701
6702 } else if (zc->zc_nvlist_src_size != 0) {
6f1ffb06
MA
6703 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6704 zc->zc_iflags, &innvl);
6705 if (error != 0)
6706 goto out;
6707 }
34dc7c2f
BB
6708
6709 /*
6710 * Ensure that all pool/dataset names are valid before we pass down to
6711 * the lower layers.
6712 */
6f1ffb06
MA
6713 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6714 switch (vec->zvec_namecheck) {
6715 case POOL_NAME:
6716 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 6717 error = SET_ERROR(EINVAL);
6f1ffb06 6718 else
572e2857 6719 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
6720 vec->zvec_namecheck, vec->zvec_pool_check);
6721 break;
34dc7c2f 6722
6f1ffb06
MA
6723 case DATASET_NAME:
6724 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 6725 error = SET_ERROR(EINVAL);
6f1ffb06 6726 else
572e2857 6727 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
6728 vec->zvec_namecheck, vec->zvec_pool_check);
6729 break;
34dc7c2f 6730
6f1ffb06
MA
6731 case NO_NAME:
6732 break;
34dc7c2f
BB
6733 }
6734
34dc7c2f 6735
005e27e3 6736 if (error == 0) {
ddab862d 6737 cookie = spl_fstrans_mark();
6f1ffb06 6738 error = vec->zvec_secpolicy(zc, innvl, CRED());
ddab862d
TC
6739 spl_fstrans_unmark(cookie);
6740 }
6f1ffb06
MA
6741
6742 if (error != 0)
6743 goto out;
6744
6745 /* legacy ioctls can modify zc_name */
4fd762f8
BB
6746 saved_poolname = strdup(zc->zc_name);
6747 if (saved_poolname == NULL) {
6748 error = SET_ERROR(ENOMEM);
6749 goto out;
6750 } else {
6751 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
6752 }
6f1ffb06
MA
6753
6754 if (vec->zvec_func != NULL) {
6755 nvlist_t *outnvl;
6756 int puterror = 0;
6757 spa_t *spa;
6758 nvlist_t *lognv = NULL;
6759
6760 ASSERT(vec->zvec_legacy_func == NULL);
6761
6762 /*
6763 * Add the innvl to the lognv before calling the func,
6764 * in case the func changes the innvl.
6765 */
6766 if (vec->zvec_allow_log) {
6767 lognv = fnvlist_alloc();
6768 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6769 vec->zvec_name);
6770 if (!nvlist_empty(innvl)) {
6771 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6772 innvl);
6773 }
6774 }
6775
79c76d5b 6776 outnvl = fnvlist_alloc();
40d06e3c 6777 cookie = spl_fstrans_mark();
6f1ffb06 6778 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
40d06e3c 6779 spl_fstrans_unmark(cookie);
6f1ffb06
MA
6780
6781 if (error == 0 && vec->zvec_allow_log &&
6782 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6783 if (!nvlist_empty(outnvl)) {
6784 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6785 outnvl);
6786 }
6787 (void) spa_history_log_nvl(spa, lognv);
6788 spa_close(spa, FTAG);
6789 }
6790 fnvlist_free(lognv);
6791
6792 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6793 int smusherror = 0;
6794 if (vec->zvec_smush_outnvlist) {
6795 smusherror = nvlist_smush(outnvl,
6796 zc->zc_nvlist_dst_size);
6797 }
6798 if (smusherror == 0)
6799 puterror = put_nvlist(zc, outnvl);
6800 }
6801
6802 if (puterror != 0)
6803 error = puterror;
6804
6805 nvlist_free(outnvl);
6806 } else {
40d06e3c 6807 cookie = spl_fstrans_mark();
6f1ffb06 6808 error = vec->zvec_legacy_func(zc);
40d06e3c 6809 spl_fstrans_unmark(cookie);
6f1ffb06
MA
6810 }
6811
6812out:
6813 nvlist_free(innvl);
9babb374 6814 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6f1ffb06 6815 if (error == 0 && rc != 0)
2e528b49 6816 error = SET_ERROR(EFAULT);
6f1ffb06
MA
6817 if (error == 0 && vec->zvec_allow_log) {
6818 char *s = tsd_get(zfs_allow_log_key);
6819 if (s != NULL)
6820 strfree(s);
fb8e608d
TC
6821 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6822 } else {
6823 if (saved_poolname != NULL)
4fd762f8 6824 strfree(saved_poolname);
34dc7c2f
BB
6825 }
6826
6827 kmem_free(zc, sizeof (zfs_cmd_t));
325f0235 6828 return (-error);
34dc7c2f
BB
6829}
6830
325f0235
BB
6831#ifdef CONFIG_COMPAT
6832static long
6833zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f 6834{
d1d7e268 6835 return (zfsdev_ioctl(filp, cmd, arg));
325f0235
BB
6836}
6837#else
d1d7e268 6838#define zfsdev_compat_ioctl NULL
325f0235 6839#endif
34dc7c2f 6840
325f0235 6841static const struct file_operations zfsdev_fops = {
d1d7e268
MK
6842 .open = zfsdev_open,
6843 .release = zfsdev_release,
6844 .unlocked_ioctl = zfsdev_ioctl,
6845 .compat_ioctl = zfsdev_compat_ioctl,
6846 .owner = THIS_MODULE,
325f0235 6847};
34dc7c2f 6848
325f0235 6849static struct miscdevice zfs_misc = {
d1d7e268
MK
6850 .minor = MISC_DYNAMIC_MINOR,
6851 .name = ZFS_DRIVER,
6852 .fops = &zfsdev_fops,
325f0235 6853};
34dc7c2f
BB
6854
6855static int
325f0235 6856zfs_attach(void)
34dc7c2f 6857{
325f0235 6858 int error;
34dc7c2f 6859
325f0235 6860 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
3937ab20
TC
6861 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6862 zfsdev_state_list->zs_minor = -1;
34dc7c2f 6863
325f0235 6864 error = misc_register(&zfs_misc);
d1d7e268 6865 if (error != 0) {
325f0235
BB
6866 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
6867 return (error);
6868 }
34dc7c2f 6869
325f0235 6870 return (0);
34dc7c2f
BB
6871}
6872
325f0235
BB
6873static void
6874zfs_detach(void)
34dc7c2f 6875{
3937ab20 6876 zfsdev_state_t *zs, *zsprev = NULL;
34dc7c2f 6877
324dcd37 6878 misc_deregister(&zfs_misc);
325f0235 6879 mutex_destroy(&zfsdev_state_lock);
3937ab20
TC
6880
6881 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6882 if (zsprev)
6883 kmem_free(zsprev, sizeof (zfsdev_state_t));
6884 zsprev = zs;
6885 }
6886 if (zsprev)
6887 kmem_free(zsprev, sizeof (zfsdev_state_t));
34dc7c2f
BB
6888}
6889
6f1ffb06
MA
6890static void
6891zfs_allow_log_destroy(void *arg)
6892{
6893 char *poolname = arg;
9f3d1407 6894
6895 if (poolname != NULL)
6896 strfree(poolname);
6f1ffb06 6897}
325f0235
BB
6898
6899#ifdef DEBUG
d1d7e268 6900#define ZFS_DEBUG_STR " (DEBUG mode)"
325f0235 6901#else
d1d7e268 6902#define ZFS_DEBUG_STR ""
325f0235 6903#endif
34dc7c2f 6904
b4f3666a 6905static int __init
34dc7c2f
BB
6906_init(void)
6907{
6908 int error;
6909
3d8d245f 6910 error = -vn_set_pwd("/");
b4f3666a
BB
6911 if (error) {
6912 printk(KERN_NOTICE
6913 "ZFS: Warning unable to set pwd to '/': %d\n", error);
6914 return (error);
6915 }
6916
a0bd735a
BP
6917 if ((error = -zvol_init()) != 0)
6918 return (error);
6919
34dc7c2f
BB
6920 spa_init(FREAD | FWRITE);
6921 zfs_init();
34dc7c2f 6922
6f1ffb06
MA
6923 zfs_ioctl_init();
6924
325f0235 6925 if ((error = zfs_attach()) != 0)
a0bd735a 6926 goto out;
34dc7c2f 6927
d5446cfc 6928 tsd_create(&zfs_fsyncer_key, NULL);
6f1ffb06
MA
6929 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6930 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
34dc7c2f 6931
4b5d425f 6932 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
d1d7e268
MK
6933 "ZFS pool version %s, ZFS filesystem version %s\n",
6934 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
6935 SPA_VERSION_STRING, ZPL_VERSION_STRING);
b695c34e
MM
6936#ifndef CONFIG_FS_POSIX_ACL
6937 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
6938#endif /* CONFIG_FS_POSIX_ACL */
34dc7c2f
BB
6939
6940 return (0);
325f0235 6941
a0bd735a 6942out:
325f0235
BB
6943 zfs_fini();
6944 spa_fini();
a0bd735a 6945 (void) zvol_fini();
4b5d425f 6946 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
d1d7e268
MK
6947 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
6948 ZFS_DEBUG_STR, error);
325f0235
BB
6949
6950 return (error);
34dc7c2f
BB
6951}
6952
b4f3666a 6953static void __exit
34dc7c2f
BB
6954_fini(void)
6955{
325f0235 6956 zfs_detach();
34dc7c2f
BB
6957 zfs_fini();
6958 spa_fini();
a0bd735a 6959 zvol_fini();
46e18b3f 6960
d5446cfc 6961 tsd_destroy(&zfs_fsyncer_key);
3fc050aa 6962 tsd_destroy(&rrw_tsd_key);
6f1ffb06 6963 tsd_destroy(&zfs_allow_log_key);
34dc7c2f 6964
4b5d425f 6965 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
d1d7e268 6966 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
34dc7c2f 6967}
325f0235
BB
6968
6969#ifdef HAVE_SPL
b4f3666a
BB
6970module_init(_init);
6971module_exit(_fini);
325f0235
BB
6972
6973MODULE_DESCRIPTION("ZFS");
6974MODULE_AUTHOR(ZFS_META_AUTHOR);
6975MODULE_LICENSE(ZFS_META_LICENSE);
99e349db 6976MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
325f0235 6977#endif /* HAVE_SPL */