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