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