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