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