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