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