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