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