]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ioctl.c
Mark functions as static
[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 extern void zfs_init(void);
226 extern void zfs_fini(void);
227
228 /*
229 * Limit maximum nvlist size. We don't want users passing in insane values
230 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
231 */
232 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
233
234 uint_t zfs_fsyncer_key;
235 uint_t zfs_allow_log_key;
236
237 /* DATA_TYPE_ANY is used when zkey_type can vary. */
238 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
239
240 typedef struct zfs_ioc_vec {
241 zfs_ioc_legacy_func_t *zvec_legacy_func;
242 zfs_ioc_func_t *zvec_func;
243 zfs_secpolicy_func_t *zvec_secpolicy;
244 zfs_ioc_namecheck_t zvec_namecheck;
245 boolean_t zvec_allow_log;
246 zfs_ioc_poolcheck_t zvec_pool_check;
247 boolean_t zvec_smush_outnvlist;
248 const char *zvec_name;
249 const zfs_ioc_key_t *zvec_nvl_keys;
250 size_t zvec_nvl_key_count;
251 } zfs_ioc_vec_t;
252
253 /* This array is indexed by zfs_userquota_prop_t */
254 static const char *userquota_perms[] = {
255 ZFS_DELEG_PERM_USERUSED,
256 ZFS_DELEG_PERM_USERQUOTA,
257 ZFS_DELEG_PERM_GROUPUSED,
258 ZFS_DELEG_PERM_GROUPQUOTA,
259 ZFS_DELEG_PERM_USEROBJUSED,
260 ZFS_DELEG_PERM_USEROBJQUOTA,
261 ZFS_DELEG_PERM_GROUPOBJUSED,
262 ZFS_DELEG_PERM_GROUPOBJQUOTA,
263 ZFS_DELEG_PERM_PROJECTUSED,
264 ZFS_DELEG_PERM_PROJECTQUOTA,
265 ZFS_DELEG_PERM_PROJECTOBJUSED,
266 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
267 };
268
269 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
270 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
271 static int zfs_check_settable(const char *name, nvpair_t *property,
272 cred_t *cr);
273 static int zfs_check_clearable(char *dataset, nvlist_t *props,
274 nvlist_t **errors);
275 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
276 boolean_t *);
277 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
278 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
279
280 static void
281 history_str_free(char *buf)
282 {
283 kmem_free(buf, HIS_MAX_RECORD_LEN);
284 }
285
286 static char *
287 history_str_get(zfs_cmd_t *zc)
288 {
289 char *buf;
290
291 if (zc->zc_history == 0)
292 return (NULL);
293
294 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
295 if (copyinstr((void *)(uintptr_t)zc->zc_history,
296 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
297 history_str_free(buf);
298 return (NULL);
299 }
300
301 buf[HIS_MAX_RECORD_LEN -1] = '\0';
302
303 return (buf);
304 }
305
306 /*
307 * Check to see if the named dataset is currently defined as bootable
308 */
309 static boolean_t
310 zfs_is_bootfs(const char *name)
311 {
312 objset_t *os;
313
314 if (dmu_objset_hold(name, FTAG, &os) == 0) {
315 boolean_t ret;
316 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
317 dmu_objset_rele(os, FTAG);
318 return (ret);
319 }
320 return (B_FALSE);
321 }
322
323 /*
324 * Return non-zero if the spa version is less than requested version.
325 */
326 static int
327 zfs_earlier_version(const char *name, int version)
328 {
329 spa_t *spa;
330
331 if (spa_open(name, &spa, FTAG) == 0) {
332 if (spa_version(spa) < version) {
333 spa_close(spa, FTAG);
334 return (1);
335 }
336 spa_close(spa, FTAG);
337 }
338 return (0);
339 }
340
341 /*
342 * Return TRUE if the ZPL version is less than requested version.
343 */
344 static boolean_t
345 zpl_earlier_version(const char *name, int version)
346 {
347 objset_t *os;
348 boolean_t rc = B_TRUE;
349
350 if (dmu_objset_hold(name, FTAG, &os) == 0) {
351 uint64_t zplversion;
352
353 if (dmu_objset_type(os) != DMU_OST_ZFS) {
354 dmu_objset_rele(os, FTAG);
355 return (B_TRUE);
356 }
357 /* XXX reading from non-owned objset */
358 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
359 rc = zplversion < version;
360 dmu_objset_rele(os, FTAG);
361 }
362 return (rc);
363 }
364
365 static void
366 zfs_log_history(zfs_cmd_t *zc)
367 {
368 spa_t *spa;
369 char *buf;
370
371 if ((buf = history_str_get(zc)) == NULL)
372 return;
373
374 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
375 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
376 (void) spa_history_log(spa, buf);
377 spa_close(spa, FTAG);
378 }
379 history_str_free(buf);
380 }
381
382 /*
383 * Policy for top-level read operations (list pools). Requires no privileges,
384 * and can be used in the local zone, as there is no associated dataset.
385 */
386 /* ARGSUSED */
387 static int
388 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
389 {
390 return (0);
391 }
392
393 /*
394 * Policy for dataset read operations (list children, get statistics). Requires
395 * no privileges, but must be visible in the local zone.
396 */
397 /* ARGSUSED */
398 static int
399 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
400 {
401 if (INGLOBALZONE(curproc) ||
402 zone_dataset_visible(zc->zc_name, NULL))
403 return (0);
404
405 return (SET_ERROR(ENOENT));
406 }
407
408 static int
409 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
410 {
411 int writable = 1;
412
413 /*
414 * The dataset must be visible by this zone -- check this first
415 * so they don't see EPERM on something they shouldn't know about.
416 */
417 if (!INGLOBALZONE(curproc) &&
418 !zone_dataset_visible(dataset, &writable))
419 return (SET_ERROR(ENOENT));
420
421 if (INGLOBALZONE(curproc)) {
422 /*
423 * If the fs is zoned, only root can access it from the
424 * global zone.
425 */
426 if (secpolicy_zfs(cr) && zoned)
427 return (SET_ERROR(EPERM));
428 } else {
429 /*
430 * If we are in a local zone, the 'zoned' property must be set.
431 */
432 if (!zoned)
433 return (SET_ERROR(EPERM));
434
435 /* must be writable by this zone */
436 if (!writable)
437 return (SET_ERROR(EPERM));
438 }
439 return (0);
440 }
441
442 static int
443 zfs_dozonecheck(const char *dataset, cred_t *cr)
444 {
445 uint64_t zoned;
446
447 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
448 &zoned, NULL))
449 return (SET_ERROR(ENOENT));
450
451 return (zfs_dozonecheck_impl(dataset, zoned, cr));
452 }
453
454 static int
455 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
456 {
457 uint64_t zoned;
458
459 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
460 return (SET_ERROR(ENOENT));
461
462 return (zfs_dozonecheck_impl(dataset, zoned, cr));
463 }
464
465 static int
466 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
467 const char *perm, cred_t *cr)
468 {
469 int error;
470
471 error = zfs_dozonecheck_ds(name, ds, cr);
472 if (error == 0) {
473 error = secpolicy_zfs(cr);
474 if (error != 0)
475 error = dsl_deleg_access_impl(ds, perm, cr);
476 }
477 return (error);
478 }
479
480 static int
481 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
482 {
483 int error;
484 dsl_dataset_t *ds;
485 dsl_pool_t *dp;
486
487 /*
488 * First do a quick check for root in the global zone, which
489 * is allowed to do all write_perms. This ensures that zfs_ioc_*
490 * will get to handle nonexistent datasets.
491 */
492 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
493 return (0);
494
495 error = dsl_pool_hold(name, FTAG, &dp);
496 if (error != 0)
497 return (error);
498
499 error = dsl_dataset_hold(dp, name, FTAG, &ds);
500 if (error != 0) {
501 dsl_pool_rele(dp, FTAG);
502 return (error);
503 }
504
505 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
506
507 dsl_dataset_rele(ds, FTAG);
508 dsl_pool_rele(dp, FTAG);
509 return (error);
510 }
511
512 /*
513 * Policy for setting the security label property.
514 *
515 * Returns 0 for success, non-zero for access and other errors.
516 */
517 static int
518 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
519 {
520 #ifdef HAVE_MLSLABEL
521 char ds_hexsl[MAXNAMELEN];
522 bslabel_t ds_sl, new_sl;
523 boolean_t new_default = FALSE;
524 uint64_t zoned;
525 int needed_priv = -1;
526 int error;
527
528 /* First get the existing dataset label. */
529 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
530 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
531 if (error != 0)
532 return (SET_ERROR(EPERM));
533
534 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
535 new_default = TRUE;
536
537 /* The label must be translatable */
538 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
539 return (SET_ERROR(EINVAL));
540
541 /*
542 * In a non-global zone, disallow attempts to set a label that
543 * doesn't match that of the zone; otherwise no other checks
544 * are needed.
545 */
546 if (!INGLOBALZONE(curproc)) {
547 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
548 return (SET_ERROR(EPERM));
549 return (0);
550 }
551
552 /*
553 * For global-zone datasets (i.e., those whose zoned property is
554 * "off", verify that the specified new label is valid for the
555 * global zone.
556 */
557 if (dsl_prop_get_integer(name,
558 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
559 return (SET_ERROR(EPERM));
560 if (!zoned) {
561 if (zfs_check_global_label(name, strval) != 0)
562 return (SET_ERROR(EPERM));
563 }
564
565 /*
566 * If the existing dataset label is nondefault, check if the
567 * dataset is mounted (label cannot be changed while mounted).
568 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
569 * mounted (or isn't a dataset, doesn't exist, ...).
570 */
571 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
572 objset_t *os;
573 static char *setsl_tag = "setsl_tag";
574
575 /*
576 * Try to own the dataset; abort if there is any error,
577 * (e.g., already mounted, in use, or other error).
578 */
579 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
580 setsl_tag, &os);
581 if (error != 0)
582 return (SET_ERROR(EPERM));
583
584 dmu_objset_disown(os, B_TRUE, setsl_tag);
585
586 if (new_default) {
587 needed_priv = PRIV_FILE_DOWNGRADE_SL;
588 goto out_check;
589 }
590
591 if (hexstr_to_label(strval, &new_sl) != 0)
592 return (SET_ERROR(EPERM));
593
594 if (blstrictdom(&ds_sl, &new_sl))
595 needed_priv = PRIV_FILE_DOWNGRADE_SL;
596 else if (blstrictdom(&new_sl, &ds_sl))
597 needed_priv = PRIV_FILE_UPGRADE_SL;
598 } else {
599 /* dataset currently has a default label */
600 if (!new_default)
601 needed_priv = PRIV_FILE_UPGRADE_SL;
602 }
603
604 out_check:
605 if (needed_priv != -1)
606 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
607 return (0);
608 #else
609 return (SET_ERROR(ENOTSUP));
610 #endif /* HAVE_MLSLABEL */
611 }
612
613 static int
614 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
615 cred_t *cr)
616 {
617 char *strval;
618
619 /*
620 * Check permissions for special properties.
621 */
622 switch (prop) {
623 default:
624 break;
625 case ZFS_PROP_ZONED:
626 /*
627 * Disallow setting of 'zoned' from within a local zone.
628 */
629 if (!INGLOBALZONE(curproc))
630 return (SET_ERROR(EPERM));
631 break;
632
633 case ZFS_PROP_QUOTA:
634 case ZFS_PROP_FILESYSTEM_LIMIT:
635 case ZFS_PROP_SNAPSHOT_LIMIT:
636 if (!INGLOBALZONE(curproc)) {
637 uint64_t zoned;
638 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
639 /*
640 * Unprivileged users are allowed to modify the
641 * limit on things *under* (ie. contained by)
642 * the thing they own.
643 */
644 if (dsl_prop_get_integer(dsname,
645 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
646 return (SET_ERROR(EPERM));
647 if (!zoned || strlen(dsname) <= strlen(setpoint))
648 return (SET_ERROR(EPERM));
649 }
650 break;
651
652 case ZFS_PROP_MLSLABEL:
653 if (!is_system_labeled())
654 return (SET_ERROR(EPERM));
655
656 if (nvpair_value_string(propval, &strval) == 0) {
657 int err;
658
659 err = zfs_set_slabel_policy(dsname, strval, CRED());
660 if (err != 0)
661 return (err);
662 }
663 break;
664 }
665
666 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
667 }
668
669 /* ARGSUSED */
670 static int
671 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
672 {
673 int error;
674
675 error = zfs_dozonecheck(zc->zc_name, cr);
676 if (error != 0)
677 return (error);
678
679 /*
680 * permission to set permissions will be evaluated later in
681 * dsl_deleg_can_allow()
682 */
683 return (0);
684 }
685
686 /* ARGSUSED */
687 static int
688 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
689 {
690 return (zfs_secpolicy_write_perms(zc->zc_name,
691 ZFS_DELEG_PERM_ROLLBACK, cr));
692 }
693
694 /* ARGSUSED */
695 static int
696 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
697 {
698 dsl_pool_t *dp;
699 dsl_dataset_t *ds;
700 char *cp;
701 int error;
702
703 /*
704 * Generate the current snapshot name from the given objsetid, then
705 * use that name for the secpolicy/zone checks.
706 */
707 cp = strchr(zc->zc_name, '@');
708 if (cp == NULL)
709 return (SET_ERROR(EINVAL));
710 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
711 if (error != 0)
712 return (error);
713
714 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
715 if (error != 0) {
716 dsl_pool_rele(dp, FTAG);
717 return (error);
718 }
719
720 dsl_dataset_name(ds, zc->zc_name);
721
722 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
723 ZFS_DELEG_PERM_SEND, cr);
724 dsl_dataset_rele(ds, FTAG);
725 dsl_pool_rele(dp, FTAG);
726
727 return (error);
728 }
729
730 /* ARGSUSED */
731 static int
732 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
733 {
734 return (zfs_secpolicy_write_perms(zc->zc_name,
735 ZFS_DELEG_PERM_SEND, cr));
736 }
737
738 static int
739 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
740 {
741 return (SET_ERROR(ENOTSUP));
742 }
743
744 static int
745 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
746 {
747 return (SET_ERROR(ENOTSUP));
748 }
749
750 static int
751 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
752 {
753 char *cp;
754
755 /*
756 * Remove the @bla or /bla from the end of the name to get the parent.
757 */
758 (void) strncpy(parent, datasetname, parentsize);
759 cp = strrchr(parent, '@');
760 if (cp != NULL) {
761 cp[0] = '\0';
762 } else {
763 cp = strrchr(parent, '/');
764 if (cp == NULL)
765 return (SET_ERROR(ENOENT));
766 cp[0] = '\0';
767 }
768
769 return (0);
770 }
771
772 int
773 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
774 {
775 int error;
776
777 if ((error = zfs_secpolicy_write_perms(name,
778 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
779 return (error);
780
781 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
782 }
783
784 /* ARGSUSED */
785 static int
786 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
787 {
788 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
789 }
790
791 /*
792 * Destroying snapshots with delegated permissions requires
793 * descendant mount and destroy permissions.
794 */
795 /* ARGSUSED */
796 static int
797 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
798 {
799 nvlist_t *snaps;
800 nvpair_t *pair, *nextpair;
801 int error = 0;
802
803 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
804
805 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
806 pair = nextpair) {
807 nextpair = nvlist_next_nvpair(snaps, pair);
808 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
809 if (error == ENOENT) {
810 /*
811 * Ignore any snapshots that don't exist (we consider
812 * them "already destroyed"). Remove the name from the
813 * nvl here in case the snapshot is created between
814 * now and when we try to destroy it (in which case
815 * we don't want to destroy it since we haven't
816 * checked for permission).
817 */
818 fnvlist_remove_nvpair(snaps, pair);
819 error = 0;
820 }
821 if (error != 0)
822 break;
823 }
824
825 return (error);
826 }
827
828 int
829 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
830 {
831 char parentname[ZFS_MAX_DATASET_NAME_LEN];
832 int error;
833
834 if ((error = zfs_secpolicy_write_perms(from,
835 ZFS_DELEG_PERM_RENAME, cr)) != 0)
836 return (error);
837
838 if ((error = zfs_secpolicy_write_perms(from,
839 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
840 return (error);
841
842 if ((error = zfs_get_parent(to, parentname,
843 sizeof (parentname))) != 0)
844 return (error);
845
846 if ((error = zfs_secpolicy_write_perms(parentname,
847 ZFS_DELEG_PERM_CREATE, cr)) != 0)
848 return (error);
849
850 if ((error = zfs_secpolicy_write_perms(parentname,
851 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
852 return (error);
853
854 return (error);
855 }
856
857 /* ARGSUSED */
858 static int
859 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
860 {
861 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
862 }
863
864 /* ARGSUSED */
865 static int
866 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
867 {
868 dsl_pool_t *dp;
869 dsl_dataset_t *clone;
870 int error;
871
872 error = zfs_secpolicy_write_perms(zc->zc_name,
873 ZFS_DELEG_PERM_PROMOTE, cr);
874 if (error != 0)
875 return (error);
876
877 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
878 if (error != 0)
879 return (error);
880
881 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
882
883 if (error == 0) {
884 char parentname[ZFS_MAX_DATASET_NAME_LEN];
885 dsl_dataset_t *origin = NULL;
886 dsl_dir_t *dd;
887 dd = clone->ds_dir;
888
889 error = dsl_dataset_hold_obj(dd->dd_pool,
890 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
891 if (error != 0) {
892 dsl_dataset_rele(clone, FTAG);
893 dsl_pool_rele(dp, FTAG);
894 return (error);
895 }
896
897 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
898 ZFS_DELEG_PERM_MOUNT, cr);
899
900 dsl_dataset_name(origin, parentname);
901 if (error == 0) {
902 error = zfs_secpolicy_write_perms_ds(parentname, origin,
903 ZFS_DELEG_PERM_PROMOTE, cr);
904 }
905 dsl_dataset_rele(clone, FTAG);
906 dsl_dataset_rele(origin, FTAG);
907 }
908 dsl_pool_rele(dp, FTAG);
909 return (error);
910 }
911
912 /* ARGSUSED */
913 static int
914 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
915 {
916 int error;
917
918 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
919 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
920 return (error);
921
922 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
923 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
924 return (error);
925
926 return (zfs_secpolicy_write_perms(zc->zc_name,
927 ZFS_DELEG_PERM_CREATE, cr));
928 }
929
930 /* ARGSUSED */
931 static int
932 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
933 {
934 return (zfs_secpolicy_recv(zc, innvl, cr));
935 }
936
937 int
938 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
939 {
940 return (zfs_secpolicy_write_perms(name,
941 ZFS_DELEG_PERM_SNAPSHOT, cr));
942 }
943
944 /*
945 * Check for permission to create each snapshot in the nvlist.
946 */
947 /* ARGSUSED */
948 static int
949 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
950 {
951 nvlist_t *snaps;
952 int error = 0;
953 nvpair_t *pair;
954
955 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
956
957 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
958 pair = nvlist_next_nvpair(snaps, pair)) {
959 char *name = nvpair_name(pair);
960 char *atp = strchr(name, '@');
961
962 if (atp == NULL) {
963 error = SET_ERROR(EINVAL);
964 break;
965 }
966 *atp = '\0';
967 error = zfs_secpolicy_snapshot_perms(name, cr);
968 *atp = '@';
969 if (error != 0)
970 break;
971 }
972 return (error);
973 }
974
975 /*
976 * Check for permission to create each bookmark in the nvlist.
977 */
978 /* ARGSUSED */
979 static int
980 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
981 {
982 int error = 0;
983
984 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
985 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
986 char *name = nvpair_name(pair);
987 char *hashp = strchr(name, '#');
988
989 if (hashp == NULL) {
990 error = SET_ERROR(EINVAL);
991 break;
992 }
993 *hashp = '\0';
994 error = zfs_secpolicy_write_perms(name,
995 ZFS_DELEG_PERM_BOOKMARK, cr);
996 *hashp = '#';
997 if (error != 0)
998 break;
999 }
1000 return (error);
1001 }
1002
1003 /* ARGSUSED */
1004 static int
1005 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1006 {
1007 nvpair_t *pair, *nextpair;
1008 int error = 0;
1009
1010 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1011 pair = nextpair) {
1012 char *name = nvpair_name(pair);
1013 char *hashp = strchr(name, '#');
1014 nextpair = nvlist_next_nvpair(innvl, pair);
1015
1016 if (hashp == NULL) {
1017 error = SET_ERROR(EINVAL);
1018 break;
1019 }
1020
1021 *hashp = '\0';
1022 error = zfs_secpolicy_write_perms(name,
1023 ZFS_DELEG_PERM_DESTROY, cr);
1024 *hashp = '#';
1025 if (error == ENOENT) {
1026 /*
1027 * Ignore any filesystems that don't exist (we consider
1028 * their bookmarks "already destroyed"). Remove
1029 * the name from the nvl here in case the filesystem
1030 * is created between now and when we try to destroy
1031 * the bookmark (in which case we don't want to
1032 * destroy it since we haven't checked for permission).
1033 */
1034 fnvlist_remove_nvpair(innvl, pair);
1035 error = 0;
1036 }
1037 if (error != 0)
1038 break;
1039 }
1040
1041 return (error);
1042 }
1043
1044 /* ARGSUSED */
1045 static int
1046 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1047 {
1048 /*
1049 * Even root must have a proper TSD so that we know what pool
1050 * to log to.
1051 */
1052 if (tsd_get(zfs_allow_log_key) == NULL)
1053 return (SET_ERROR(EPERM));
1054 return (0);
1055 }
1056
1057 static int
1058 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1059 {
1060 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1061 int error;
1062 char *origin;
1063
1064 if ((error = zfs_get_parent(zc->zc_name, parentname,
1065 sizeof (parentname))) != 0)
1066 return (error);
1067
1068 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1069 (error = zfs_secpolicy_write_perms(origin,
1070 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1071 return (error);
1072
1073 if ((error = zfs_secpolicy_write_perms(parentname,
1074 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1075 return (error);
1076
1077 return (zfs_secpolicy_write_perms(parentname,
1078 ZFS_DELEG_PERM_MOUNT, cr));
1079 }
1080
1081 /*
1082 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1083 * SYS_CONFIG privilege, which is not available in a local zone.
1084 */
1085 /* ARGSUSED */
1086 int
1087 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1088 {
1089 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1090 return (SET_ERROR(EPERM));
1091
1092 return (0);
1093 }
1094
1095 /*
1096 * Policy for object to name lookups.
1097 */
1098 /* ARGSUSED */
1099 static int
1100 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1101 {
1102 int error;
1103
1104 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1105 return (0);
1106
1107 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1108 return (error);
1109 }
1110
1111 /*
1112 * Policy for fault injection. Requires all privileges.
1113 */
1114 /* ARGSUSED */
1115 static int
1116 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1117 {
1118 return (secpolicy_zinject(cr));
1119 }
1120
1121 /* ARGSUSED */
1122 static int
1123 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1124 {
1125 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1126
1127 if (prop == ZPROP_INVAL) {
1128 if (!zfs_prop_user(zc->zc_value))
1129 return (SET_ERROR(EINVAL));
1130 return (zfs_secpolicy_write_perms(zc->zc_name,
1131 ZFS_DELEG_PERM_USERPROP, cr));
1132 } else {
1133 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1134 NULL, cr));
1135 }
1136 }
1137
1138 static int
1139 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1140 {
1141 int err = zfs_secpolicy_read(zc, innvl, cr);
1142 if (err)
1143 return (err);
1144
1145 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1146 return (SET_ERROR(EINVAL));
1147
1148 if (zc->zc_value[0] == 0) {
1149 /*
1150 * They are asking about a posix uid/gid. If it's
1151 * themself, allow it.
1152 */
1153 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1154 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1155 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1156 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1157 if (zc->zc_guid == crgetuid(cr))
1158 return (0);
1159 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1160 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1161 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1162 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1163 if (groupmember(zc->zc_guid, cr))
1164 return (0);
1165 }
1166 /* else is for project quota/used */
1167 }
1168
1169 return (zfs_secpolicy_write_perms(zc->zc_name,
1170 userquota_perms[zc->zc_objset_type], cr));
1171 }
1172
1173 static int
1174 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1175 {
1176 int err = zfs_secpolicy_read(zc, innvl, cr);
1177 if (err)
1178 return (err);
1179
1180 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1181 return (SET_ERROR(EINVAL));
1182
1183 return (zfs_secpolicy_write_perms(zc->zc_name,
1184 userquota_perms[zc->zc_objset_type], cr));
1185 }
1186
1187 /* ARGSUSED */
1188 static int
1189 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1190 {
1191 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1192 NULL, cr));
1193 }
1194
1195 /* ARGSUSED */
1196 static int
1197 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1198 {
1199 nvpair_t *pair;
1200 nvlist_t *holds;
1201 int error;
1202
1203 holds = fnvlist_lookup_nvlist(innvl, "holds");
1204
1205 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1206 pair = nvlist_next_nvpair(holds, pair)) {
1207 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1208 error = dmu_fsname(nvpair_name(pair), fsname);
1209 if (error != 0)
1210 return (error);
1211 error = zfs_secpolicy_write_perms(fsname,
1212 ZFS_DELEG_PERM_HOLD, cr);
1213 if (error != 0)
1214 return (error);
1215 }
1216 return (0);
1217 }
1218
1219 /* ARGSUSED */
1220 static int
1221 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1222 {
1223 nvpair_t *pair;
1224 int error;
1225
1226 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1227 pair = nvlist_next_nvpair(innvl, pair)) {
1228 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1229 error = dmu_fsname(nvpair_name(pair), fsname);
1230 if (error != 0)
1231 return (error);
1232 error = zfs_secpolicy_write_perms(fsname,
1233 ZFS_DELEG_PERM_RELEASE, cr);
1234 if (error != 0)
1235 return (error);
1236 }
1237 return (0);
1238 }
1239
1240 /*
1241 * Policy for allowing temporary snapshots to be taken or released
1242 */
1243 static int
1244 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1245 {
1246 /*
1247 * A temporary snapshot is the same as a snapshot,
1248 * hold, destroy and release all rolled into one.
1249 * Delegated diff alone is sufficient that we allow this.
1250 */
1251 int error;
1252
1253 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1254 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1255 return (0);
1256
1257 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1258
1259 if (innvl != NULL) {
1260 if (error == 0)
1261 error = zfs_secpolicy_hold(zc, innvl, cr);
1262 if (error == 0)
1263 error = zfs_secpolicy_release(zc, innvl, cr);
1264 if (error == 0)
1265 error = zfs_secpolicy_destroy(zc, innvl, cr);
1266 }
1267 return (error);
1268 }
1269
1270 static int
1271 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1272 {
1273 return (zfs_secpolicy_write_perms(zc->zc_name,
1274 ZFS_DELEG_PERM_LOAD_KEY, cr));
1275 }
1276
1277 static int
1278 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1279 {
1280 return (zfs_secpolicy_write_perms(zc->zc_name,
1281 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1282 }
1283
1284 /*
1285 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1286 */
1287 static int
1288 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1289 {
1290 char *packed;
1291 int error;
1292 nvlist_t *list = NULL;
1293
1294 /*
1295 * Read in and unpack the user-supplied nvlist.
1296 */
1297 if (size == 0)
1298 return (SET_ERROR(EINVAL));
1299
1300 packed = vmem_alloc(size, KM_SLEEP);
1301
1302 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1303 iflag)) != 0) {
1304 vmem_free(packed, size);
1305 return (SET_ERROR(EFAULT));
1306 }
1307
1308 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1309 vmem_free(packed, size);
1310 return (error);
1311 }
1312
1313 vmem_free(packed, size);
1314
1315 *nvp = list;
1316 return (0);
1317 }
1318
1319 /*
1320 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1321 * Entries will be removed from the end of the nvlist, and one int32 entry
1322 * named "N_MORE_ERRORS" will be added indicating how many entries were
1323 * removed.
1324 */
1325 static int
1326 nvlist_smush(nvlist_t *errors, size_t max)
1327 {
1328 size_t size;
1329
1330 size = fnvlist_size(errors);
1331
1332 if (size > max) {
1333 nvpair_t *more_errors;
1334 int n = 0;
1335
1336 if (max < 1024)
1337 return (SET_ERROR(ENOMEM));
1338
1339 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1340 more_errors = nvlist_prev_nvpair(errors, NULL);
1341
1342 do {
1343 nvpair_t *pair = nvlist_prev_nvpair(errors,
1344 more_errors);
1345 fnvlist_remove_nvpair(errors, pair);
1346 n++;
1347 size = fnvlist_size(errors);
1348 } while (size > max);
1349
1350 fnvlist_remove_nvpair(errors, more_errors);
1351 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1352 ASSERT3U(fnvlist_size(errors), <=, max);
1353 }
1354
1355 return (0);
1356 }
1357
1358 static int
1359 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1360 {
1361 char *packed = NULL;
1362 int error = 0;
1363 size_t size;
1364
1365 size = fnvlist_size(nvl);
1366
1367 if (size > zc->zc_nvlist_dst_size) {
1368 error = SET_ERROR(ENOMEM);
1369 } else {
1370 packed = fnvlist_pack(nvl, &size);
1371 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1372 size, zc->zc_iflags) != 0)
1373 error = SET_ERROR(EFAULT);
1374 fnvlist_pack_free(packed, size);
1375 }
1376
1377 zc->zc_nvlist_dst_size = size;
1378 zc->zc_nvlist_dst_filled = B_TRUE;
1379 return (error);
1380 }
1381
1382 int
1383 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1384 {
1385 int error = 0;
1386 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1387 return (SET_ERROR(EINVAL));
1388 }
1389
1390 mutex_enter(&os->os_user_ptr_lock);
1391 *zfvp = dmu_objset_get_user(os);
1392 /* bump s_active only when non-zero to prevent umount race */
1393 error = zfs_vfs_ref(zfvp);
1394 mutex_exit(&os->os_user_ptr_lock);
1395 return (error);
1396 }
1397
1398 int
1399 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1400 {
1401 objset_t *os;
1402 int error;
1403
1404 error = dmu_objset_hold(dsname, FTAG, &os);
1405 if (error != 0)
1406 return (error);
1407
1408 error = getzfsvfs_impl(os, zfvp);
1409 dmu_objset_rele(os, FTAG);
1410 return (error);
1411 }
1412
1413 /*
1414 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1415 * case its z_sb will be NULL, and it will be opened as the owner.
1416 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1417 * which prevents all inode ops from running.
1418 */
1419 static int
1420 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1421 {
1422 int error = 0;
1423
1424 if (getzfsvfs(name, zfvp) != 0)
1425 error = zfsvfs_create(name, B_FALSE, zfvp);
1426 if (error == 0) {
1427 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1428 RW_READER, tag);
1429 if ((*zfvp)->z_unmounted) {
1430 /*
1431 * XXX we could probably try again, since the unmounting
1432 * thread should be just about to disassociate the
1433 * objset from the zfsvfs.
1434 */
1435 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1436 return (SET_ERROR(EBUSY));
1437 }
1438 }
1439 return (error);
1440 }
1441
1442 static void
1443 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1444 {
1445 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1446
1447 if (zfs_vfs_held(zfsvfs)) {
1448 zfs_vfs_rele(zfsvfs);
1449 } else {
1450 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1451 zfsvfs_free(zfsvfs);
1452 }
1453 }
1454
1455 static int
1456 zfs_ioc_pool_create(zfs_cmd_t *zc)
1457 {
1458 int error;
1459 nvlist_t *config, *props = NULL;
1460 nvlist_t *rootprops = NULL;
1461 nvlist_t *zplprops = NULL;
1462 dsl_crypto_params_t *dcp = NULL;
1463 char *spa_name = zc->zc_name;
1464 boolean_t unload_wkey = B_TRUE;
1465
1466 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1467 zc->zc_iflags, &config)))
1468 return (error);
1469
1470 if (zc->zc_nvlist_src_size != 0 && (error =
1471 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1472 zc->zc_iflags, &props))) {
1473 nvlist_free(config);
1474 return (error);
1475 }
1476
1477 if (props) {
1478 nvlist_t *nvl = NULL;
1479 nvlist_t *hidden_args = NULL;
1480 uint64_t version = SPA_VERSION;
1481 char *tname;
1482
1483 (void) nvlist_lookup_uint64(props,
1484 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1485 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1486 error = SET_ERROR(EINVAL);
1487 goto pool_props_bad;
1488 }
1489 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1490 if (nvl) {
1491 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1492 if (error != 0)
1493 goto pool_props_bad;
1494 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1495 }
1496
1497 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1498 &hidden_args);
1499 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1500 rootprops, hidden_args, &dcp);
1501 if (error != 0)
1502 goto pool_props_bad;
1503 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1504
1505 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1506 error = zfs_fill_zplprops_root(version, rootprops,
1507 zplprops, NULL);
1508 if (error != 0)
1509 goto pool_props_bad;
1510
1511 if (nvlist_lookup_string(props,
1512 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1513 spa_name = tname;
1514 }
1515
1516 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1517
1518 /*
1519 * Set the remaining root properties
1520 */
1521 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1522 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1523 (void) spa_destroy(spa_name);
1524 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1525 }
1526
1527 pool_props_bad:
1528 nvlist_free(rootprops);
1529 nvlist_free(zplprops);
1530 nvlist_free(config);
1531 nvlist_free(props);
1532 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1533
1534 return (error);
1535 }
1536
1537 static int
1538 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1539 {
1540 int error;
1541 zfs_log_history(zc);
1542 error = spa_destroy(zc->zc_name);
1543
1544 return (error);
1545 }
1546
1547 static int
1548 zfs_ioc_pool_import(zfs_cmd_t *zc)
1549 {
1550 nvlist_t *config, *props = NULL;
1551 uint64_t guid;
1552 int error;
1553
1554 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1555 zc->zc_iflags, &config)) != 0)
1556 return (error);
1557
1558 if (zc->zc_nvlist_src_size != 0 && (error =
1559 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1560 zc->zc_iflags, &props))) {
1561 nvlist_free(config);
1562 return (error);
1563 }
1564
1565 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1566 guid != zc->zc_guid)
1567 error = SET_ERROR(EINVAL);
1568 else
1569 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1570
1571 if (zc->zc_nvlist_dst != 0) {
1572 int err;
1573
1574 if ((err = put_nvlist(zc, config)) != 0)
1575 error = err;
1576 }
1577
1578 nvlist_free(config);
1579 nvlist_free(props);
1580
1581 return (error);
1582 }
1583
1584 static int
1585 zfs_ioc_pool_export(zfs_cmd_t *zc)
1586 {
1587 int error;
1588 boolean_t force = (boolean_t)zc->zc_cookie;
1589 boolean_t hardforce = (boolean_t)zc->zc_guid;
1590
1591 zfs_log_history(zc);
1592 error = spa_export(zc->zc_name, NULL, force, hardforce);
1593
1594 return (error);
1595 }
1596
1597 static int
1598 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1599 {
1600 nvlist_t *configs;
1601 int error;
1602
1603 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1604 return (SET_ERROR(EEXIST));
1605
1606 error = put_nvlist(zc, configs);
1607
1608 nvlist_free(configs);
1609
1610 return (error);
1611 }
1612
1613 /*
1614 * inputs:
1615 * zc_name name of the pool
1616 *
1617 * outputs:
1618 * zc_cookie real errno
1619 * zc_nvlist_dst config nvlist
1620 * zc_nvlist_dst_size size of config nvlist
1621 */
1622 static int
1623 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1624 {
1625 nvlist_t *config;
1626 int error;
1627 int ret = 0;
1628
1629 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1630 sizeof (zc->zc_value));
1631
1632 if (config != NULL) {
1633 ret = put_nvlist(zc, config);
1634 nvlist_free(config);
1635
1636 /*
1637 * The config may be present even if 'error' is non-zero.
1638 * In this case we return success, and preserve the real errno
1639 * in 'zc_cookie'.
1640 */
1641 zc->zc_cookie = error;
1642 } else {
1643 ret = error;
1644 }
1645
1646 return (ret);
1647 }
1648
1649 /*
1650 * Try to import the given pool, returning pool stats as appropriate so that
1651 * user land knows which devices are available and overall pool health.
1652 */
1653 static int
1654 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1655 {
1656 nvlist_t *tryconfig, *config = NULL;
1657 int error;
1658
1659 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1660 zc->zc_iflags, &tryconfig)) != 0)
1661 return (error);
1662
1663 config = spa_tryimport(tryconfig);
1664
1665 nvlist_free(tryconfig);
1666
1667 if (config == NULL)
1668 return (SET_ERROR(EINVAL));
1669
1670 error = put_nvlist(zc, config);
1671 nvlist_free(config);
1672
1673 return (error);
1674 }
1675
1676 /*
1677 * inputs:
1678 * zc_name name of the pool
1679 * zc_cookie scan func (pool_scan_func_t)
1680 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1681 */
1682 static int
1683 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1684 {
1685 spa_t *spa;
1686 int error;
1687
1688 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1689 return (SET_ERROR(EINVAL));
1690
1691 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1692 return (error);
1693
1694 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1695 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1696 else if (zc->zc_cookie == POOL_SCAN_NONE)
1697 error = spa_scan_stop(spa);
1698 else
1699 error = spa_scan(spa, zc->zc_cookie);
1700
1701 spa_close(spa, FTAG);
1702
1703 return (error);
1704 }
1705
1706 static int
1707 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1708 {
1709 spa_t *spa;
1710 int error;
1711
1712 error = spa_open(zc->zc_name, &spa, FTAG);
1713 if (error == 0) {
1714 spa_freeze(spa);
1715 spa_close(spa, FTAG);
1716 }
1717 return (error);
1718 }
1719
1720 static int
1721 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1722 {
1723 spa_t *spa;
1724 int error;
1725
1726 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1727 return (error);
1728
1729 if (zc->zc_cookie < spa_version(spa) ||
1730 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1731 spa_close(spa, FTAG);
1732 return (SET_ERROR(EINVAL));
1733 }
1734
1735 spa_upgrade(spa, zc->zc_cookie);
1736 spa_close(spa, FTAG);
1737
1738 return (error);
1739 }
1740
1741 static int
1742 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1743 {
1744 spa_t *spa;
1745 char *hist_buf;
1746 uint64_t size;
1747 int error;
1748
1749 if ((size = zc->zc_history_len) == 0)
1750 return (SET_ERROR(EINVAL));
1751
1752 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1753 return (error);
1754
1755 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1756 spa_close(spa, FTAG);
1757 return (SET_ERROR(ENOTSUP));
1758 }
1759
1760 hist_buf = vmem_alloc(size, KM_SLEEP);
1761 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1762 &zc->zc_history_len, hist_buf)) == 0) {
1763 error = ddi_copyout(hist_buf,
1764 (void *)(uintptr_t)zc->zc_history,
1765 zc->zc_history_len, zc->zc_iflags);
1766 }
1767
1768 spa_close(spa, FTAG);
1769 vmem_free(hist_buf, size);
1770 return (error);
1771 }
1772
1773 static int
1774 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1775 {
1776 spa_t *spa;
1777 int error;
1778
1779 error = spa_open(zc->zc_name, &spa, FTAG);
1780 if (error == 0) {
1781 error = spa_change_guid(spa);
1782 spa_close(spa, FTAG);
1783 }
1784 return (error);
1785 }
1786
1787 static int
1788 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1789 {
1790 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1791 }
1792
1793 /*
1794 * inputs:
1795 * zc_name name of filesystem
1796 * zc_obj object to find
1797 *
1798 * outputs:
1799 * zc_value name of object
1800 */
1801 static int
1802 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1803 {
1804 objset_t *os;
1805 int error;
1806
1807 /* XXX reading from objset not owned */
1808 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1809 FTAG, &os)) != 0)
1810 return (error);
1811 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1812 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1813 return (SET_ERROR(EINVAL));
1814 }
1815 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1816 sizeof (zc->zc_value));
1817 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1818
1819 return (error);
1820 }
1821
1822 /*
1823 * inputs:
1824 * zc_name name of filesystem
1825 * zc_obj object to find
1826 *
1827 * outputs:
1828 * zc_stat stats on object
1829 * zc_value path to object
1830 */
1831 static int
1832 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1833 {
1834 objset_t *os;
1835 int error;
1836
1837 /* XXX reading from objset not owned */
1838 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1839 FTAG, &os)) != 0)
1840 return (error);
1841 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1842 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1843 return (SET_ERROR(EINVAL));
1844 }
1845 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1846 sizeof (zc->zc_value));
1847 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1848
1849 return (error);
1850 }
1851
1852 static int
1853 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1854 {
1855 spa_t *spa;
1856 int error;
1857 nvlist_t *config;
1858
1859 error = spa_open(zc->zc_name, &spa, FTAG);
1860 if (error != 0)
1861 return (error);
1862
1863 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1864 zc->zc_iflags, &config);
1865 if (error == 0) {
1866 error = spa_vdev_add(spa, config);
1867 nvlist_free(config);
1868 }
1869 spa_close(spa, FTAG);
1870 return (error);
1871 }
1872
1873 /*
1874 * inputs:
1875 * zc_name name of the pool
1876 * zc_guid guid of vdev to remove
1877 * zc_cookie cancel removal
1878 */
1879 static int
1880 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1881 {
1882 spa_t *spa;
1883 int error;
1884
1885 error = spa_open(zc->zc_name, &spa, FTAG);
1886 if (error != 0)
1887 return (error);
1888 if (zc->zc_cookie != 0) {
1889 error = spa_vdev_remove_cancel(spa);
1890 } else {
1891 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1892 }
1893 spa_close(spa, FTAG);
1894 return (error);
1895 }
1896
1897 static int
1898 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1899 {
1900 spa_t *spa;
1901 int error;
1902 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1903
1904 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1905 return (error);
1906 switch (zc->zc_cookie) {
1907 case VDEV_STATE_ONLINE:
1908 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1909 break;
1910
1911 case VDEV_STATE_OFFLINE:
1912 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1913 break;
1914
1915 case VDEV_STATE_FAULTED:
1916 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1917 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1918 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1919 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1920
1921 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1922 break;
1923
1924 case VDEV_STATE_DEGRADED:
1925 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1926 zc->zc_obj != VDEV_AUX_EXTERNAL)
1927 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1928
1929 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1930 break;
1931
1932 default:
1933 error = SET_ERROR(EINVAL);
1934 }
1935 zc->zc_cookie = newstate;
1936 spa_close(spa, FTAG);
1937 return (error);
1938 }
1939
1940 static int
1941 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1942 {
1943 spa_t *spa;
1944 int replacing = zc->zc_cookie;
1945 nvlist_t *config;
1946 int error;
1947
1948 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1949 return (error);
1950
1951 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1952 zc->zc_iflags, &config)) == 0) {
1953 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1954 nvlist_free(config);
1955 }
1956
1957 spa_close(spa, FTAG);
1958 return (error);
1959 }
1960
1961 static int
1962 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1963 {
1964 spa_t *spa;
1965 int error;
1966
1967 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1968 return (error);
1969
1970 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1971
1972 spa_close(spa, FTAG);
1973 return (error);
1974 }
1975
1976 static int
1977 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1978 {
1979 spa_t *spa;
1980 nvlist_t *config, *props = NULL;
1981 int error;
1982 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1983
1984 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1985 return (error);
1986
1987 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1988 zc->zc_iflags, &config))) {
1989 spa_close(spa, FTAG);
1990 return (error);
1991 }
1992
1993 if (zc->zc_nvlist_src_size != 0 && (error =
1994 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1995 zc->zc_iflags, &props))) {
1996 spa_close(spa, FTAG);
1997 nvlist_free(config);
1998 return (error);
1999 }
2000
2001 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2002
2003 spa_close(spa, FTAG);
2004
2005 nvlist_free(config);
2006 nvlist_free(props);
2007
2008 return (error);
2009 }
2010
2011 static int
2012 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2013 {
2014 spa_t *spa;
2015 char *path = zc->zc_value;
2016 uint64_t guid = zc->zc_guid;
2017 int error;
2018
2019 error = spa_open(zc->zc_name, &spa, FTAG);
2020 if (error != 0)
2021 return (error);
2022
2023 error = spa_vdev_setpath(spa, guid, path);
2024 spa_close(spa, FTAG);
2025 return (error);
2026 }
2027
2028 static int
2029 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2030 {
2031 spa_t *spa;
2032 char *fru = zc->zc_value;
2033 uint64_t guid = zc->zc_guid;
2034 int error;
2035
2036 error = spa_open(zc->zc_name, &spa, FTAG);
2037 if (error != 0)
2038 return (error);
2039
2040 error = spa_vdev_setfru(spa, guid, fru);
2041 spa_close(spa, FTAG);
2042 return (error);
2043 }
2044
2045 static int
2046 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2047 {
2048 int error = 0;
2049 nvlist_t *nv;
2050
2051 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2052
2053 if (zc->zc_nvlist_dst != 0 &&
2054 (error = dsl_prop_get_all(os, &nv)) == 0) {
2055 dmu_objset_stats(os, nv);
2056 /*
2057 * NB: zvol_get_stats() will read the objset contents,
2058 * which we aren't supposed to do with a
2059 * DS_MODE_USER hold, because it could be
2060 * inconsistent. So this is a bit of a workaround...
2061 * XXX reading without owning
2062 */
2063 if (!zc->zc_objset_stats.dds_inconsistent &&
2064 dmu_objset_type(os) == DMU_OST_ZVOL) {
2065 error = zvol_get_stats(os, nv);
2066 if (error == EIO) {
2067 nvlist_free(nv);
2068 return (error);
2069 }
2070 VERIFY0(error);
2071 }
2072 if (error == 0)
2073 error = put_nvlist(zc, nv);
2074 nvlist_free(nv);
2075 }
2076
2077 return (error);
2078 }
2079
2080 /*
2081 * inputs:
2082 * zc_name name of filesystem
2083 * zc_nvlist_dst_size size of buffer for property nvlist
2084 *
2085 * outputs:
2086 * zc_objset_stats stats
2087 * zc_nvlist_dst property nvlist
2088 * zc_nvlist_dst_size size of property nvlist
2089 */
2090 static int
2091 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2092 {
2093 objset_t *os;
2094 int error;
2095
2096 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2097 if (error == 0) {
2098 error = zfs_ioc_objset_stats_impl(zc, os);
2099 dmu_objset_rele(os, FTAG);
2100 }
2101
2102 return (error);
2103 }
2104
2105 /*
2106 * inputs:
2107 * zc_name name of filesystem
2108 * zc_nvlist_dst_size size of buffer for property nvlist
2109 *
2110 * outputs:
2111 * zc_nvlist_dst received property nvlist
2112 * zc_nvlist_dst_size size of received property nvlist
2113 *
2114 * Gets received properties (distinct from local properties on or after
2115 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2116 * local property values.
2117 */
2118 static int
2119 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2120 {
2121 int error = 0;
2122 nvlist_t *nv;
2123
2124 /*
2125 * Without this check, we would return local property values if the
2126 * caller has not already received properties on or after
2127 * SPA_VERSION_RECVD_PROPS.
2128 */
2129 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2130 return (SET_ERROR(ENOTSUP));
2131
2132 if (zc->zc_nvlist_dst != 0 &&
2133 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2134 error = put_nvlist(zc, nv);
2135 nvlist_free(nv);
2136 }
2137
2138 return (error);
2139 }
2140
2141 static int
2142 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2143 {
2144 uint64_t value;
2145 int error;
2146
2147 /*
2148 * zfs_get_zplprop() will either find a value or give us
2149 * the default value (if there is one).
2150 */
2151 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2152 return (error);
2153 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2154 return (0);
2155 }
2156
2157 /*
2158 * inputs:
2159 * zc_name name of filesystem
2160 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2161 *
2162 * outputs:
2163 * zc_nvlist_dst zpl property nvlist
2164 * zc_nvlist_dst_size size of zpl property nvlist
2165 */
2166 static int
2167 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2168 {
2169 objset_t *os;
2170 int err;
2171
2172 /* XXX reading without owning */
2173 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2174 return (err);
2175
2176 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2177
2178 /*
2179 * NB: nvl_add_zplprop() will read the objset contents,
2180 * which we aren't supposed to do with a DS_MODE_USER
2181 * hold, because it could be inconsistent.
2182 */
2183 if (zc->zc_nvlist_dst != 0 &&
2184 !zc->zc_objset_stats.dds_inconsistent &&
2185 dmu_objset_type(os) == DMU_OST_ZFS) {
2186 nvlist_t *nv;
2187
2188 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2189 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2190 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2191 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2192 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2193 err = put_nvlist(zc, nv);
2194 nvlist_free(nv);
2195 } else {
2196 err = SET_ERROR(ENOENT);
2197 }
2198 dmu_objset_rele(os, FTAG);
2199 return (err);
2200 }
2201
2202 /*
2203 * inputs:
2204 * zc_name name of filesystem
2205 * zc_cookie zap cursor
2206 * zc_nvlist_dst_size size of buffer for property nvlist
2207 *
2208 * outputs:
2209 * zc_name name of next filesystem
2210 * zc_cookie zap cursor
2211 * zc_objset_stats stats
2212 * zc_nvlist_dst property nvlist
2213 * zc_nvlist_dst_size size of property nvlist
2214 */
2215 static int
2216 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2217 {
2218 objset_t *os;
2219 int error;
2220 char *p;
2221 size_t orig_len = strlen(zc->zc_name);
2222
2223 top:
2224 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2225 if (error == ENOENT)
2226 error = SET_ERROR(ESRCH);
2227 return (error);
2228 }
2229
2230 p = strrchr(zc->zc_name, '/');
2231 if (p == NULL || p[1] != '\0')
2232 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2233 p = zc->zc_name + strlen(zc->zc_name);
2234
2235 do {
2236 error = dmu_dir_list_next(os,
2237 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2238 NULL, &zc->zc_cookie);
2239 if (error == ENOENT)
2240 error = SET_ERROR(ESRCH);
2241 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2242 dmu_objset_rele(os, FTAG);
2243
2244 /*
2245 * If it's an internal dataset (ie. with a '$' in its name),
2246 * don't try to get stats for it, otherwise we'll return ENOENT.
2247 */
2248 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2249 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2250 if (error == ENOENT) {
2251 /* We lost a race with destroy, get the next one. */
2252 zc->zc_name[orig_len] = '\0';
2253 goto top;
2254 }
2255 }
2256 return (error);
2257 }
2258
2259 /*
2260 * inputs:
2261 * zc_name name of filesystem
2262 * zc_cookie zap cursor
2263 * zc_nvlist_src iteration range nvlist
2264 * zc_nvlist_src_size size of iteration range nvlist
2265 *
2266 * outputs:
2267 * zc_name name of next snapshot
2268 * zc_objset_stats stats
2269 * zc_nvlist_dst property nvlist
2270 * zc_nvlist_dst_size size of property nvlist
2271 */
2272 static int
2273 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2274 {
2275 int error;
2276 objset_t *os, *ossnap;
2277 dsl_dataset_t *ds;
2278 uint64_t min_txg = 0, max_txg = 0;
2279
2280 if (zc->zc_nvlist_src_size != 0) {
2281 nvlist_t *props = NULL;
2282 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2283 zc->zc_iflags, &props);
2284 if (error != 0)
2285 return (error);
2286 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2287 &min_txg);
2288 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2289 &max_txg);
2290 nvlist_free(props);
2291 }
2292
2293 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2294 if (error != 0) {
2295 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2296 }
2297
2298 /*
2299 * A dataset name of maximum length cannot have any snapshots,
2300 * so exit immediately.
2301 */
2302 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2303 ZFS_MAX_DATASET_NAME_LEN) {
2304 dmu_objset_rele(os, FTAG);
2305 return (SET_ERROR(ESRCH));
2306 }
2307
2308 while (error == 0) {
2309 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2310 error = SET_ERROR(EINTR);
2311 break;
2312 }
2313
2314 error = dmu_snapshot_list_next(os,
2315 sizeof (zc->zc_name) - strlen(zc->zc_name),
2316 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2317 &zc->zc_cookie, NULL);
2318 if (error == ENOENT) {
2319 error = SET_ERROR(ESRCH);
2320 break;
2321 } else if (error != 0) {
2322 break;
2323 }
2324
2325 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2326 FTAG, &ds);
2327 if (error != 0)
2328 break;
2329
2330 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2331 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2332 dsl_dataset_rele(ds, FTAG);
2333 /* undo snapshot name append */
2334 *(strchr(zc->zc_name, '@') + 1) = '\0';
2335 /* skip snapshot */
2336 continue;
2337 }
2338
2339 if (zc->zc_simple) {
2340 dsl_dataset_rele(ds, FTAG);
2341 break;
2342 }
2343
2344 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2345 dsl_dataset_rele(ds, FTAG);
2346 break;
2347 }
2348 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2349 dsl_dataset_rele(ds, FTAG);
2350 break;
2351 }
2352 dsl_dataset_rele(ds, FTAG);
2353 break;
2354 }
2355
2356 dmu_objset_rele(os, FTAG);
2357 /* if we failed, undo the @ that we tacked on to zc_name */
2358 if (error != 0)
2359 *strchr(zc->zc_name, '@') = '\0';
2360 return (error);
2361 }
2362
2363 static int
2364 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2365 {
2366 const char *propname = nvpair_name(pair);
2367 uint64_t *valary;
2368 unsigned int vallen;
2369 const char *domain;
2370 char *dash;
2371 zfs_userquota_prop_t type;
2372 uint64_t rid;
2373 uint64_t quota;
2374 zfsvfs_t *zfsvfs;
2375 int err;
2376
2377 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2378 nvlist_t *attrs;
2379 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2380 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2381 &pair) != 0)
2382 return (SET_ERROR(EINVAL));
2383 }
2384
2385 /*
2386 * A correctly constructed propname is encoded as
2387 * userquota@<rid>-<domain>.
2388 */
2389 if ((dash = strchr(propname, '-')) == NULL ||
2390 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2391 vallen != 3)
2392 return (SET_ERROR(EINVAL));
2393
2394 domain = dash + 1;
2395 type = valary[0];
2396 rid = valary[1];
2397 quota = valary[2];
2398
2399 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2400 if (err == 0) {
2401 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2402 zfsvfs_rele(zfsvfs, FTAG);
2403 }
2404
2405 return (err);
2406 }
2407
2408 /*
2409 * If the named property is one that has a special function to set its value,
2410 * return 0 on success and a positive error code on failure; otherwise if it is
2411 * not one of the special properties handled by this function, return -1.
2412 *
2413 * XXX: It would be better for callers of the property interface if we handled
2414 * these special cases in dsl_prop.c (in the dsl layer).
2415 */
2416 static int
2417 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2418 nvpair_t *pair)
2419 {
2420 const char *propname = nvpair_name(pair);
2421 zfs_prop_t prop = zfs_name_to_prop(propname);
2422 uint64_t intval = 0;
2423 char *strval = NULL;
2424 int err = -1;
2425
2426 if (prop == ZPROP_INVAL) {
2427 if (zfs_prop_userquota(propname))
2428 return (zfs_prop_set_userquota(dsname, pair));
2429 return (-1);
2430 }
2431
2432 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2433 nvlist_t *attrs;
2434 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2435 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2436 &pair) == 0);
2437 }
2438
2439 /* all special properties are numeric except for keylocation */
2440 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2441 strval = fnvpair_value_string(pair);
2442 } else {
2443 intval = fnvpair_value_uint64(pair);
2444 }
2445
2446 switch (prop) {
2447 case ZFS_PROP_QUOTA:
2448 err = dsl_dir_set_quota(dsname, source, intval);
2449 break;
2450 case ZFS_PROP_REFQUOTA:
2451 err = dsl_dataset_set_refquota(dsname, source, intval);
2452 break;
2453 case ZFS_PROP_FILESYSTEM_LIMIT:
2454 case ZFS_PROP_SNAPSHOT_LIMIT:
2455 if (intval == UINT64_MAX) {
2456 /* clearing the limit, just do it */
2457 err = 0;
2458 } else {
2459 err = dsl_dir_activate_fs_ss_limit(dsname);
2460 }
2461 /*
2462 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2463 * default path to set the value in the nvlist.
2464 */
2465 if (err == 0)
2466 err = -1;
2467 break;
2468 case ZFS_PROP_KEYLOCATION:
2469 err = dsl_crypto_can_set_keylocation(dsname, strval);
2470
2471 /*
2472 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2473 * default path to set the value in the nvlist.
2474 */
2475 if (err == 0)
2476 err = -1;
2477 break;
2478 case ZFS_PROP_RESERVATION:
2479 err = dsl_dir_set_reservation(dsname, source, intval);
2480 break;
2481 case ZFS_PROP_REFRESERVATION:
2482 err = dsl_dataset_set_refreservation(dsname, source, intval);
2483 break;
2484 case ZFS_PROP_VOLSIZE:
2485 err = zvol_set_volsize(dsname, intval);
2486 break;
2487 case ZFS_PROP_SNAPDEV:
2488 err = zvol_set_snapdev(dsname, source, intval);
2489 break;
2490 case ZFS_PROP_VOLMODE:
2491 err = zvol_set_volmode(dsname, source, intval);
2492 break;
2493 case ZFS_PROP_VERSION:
2494 {
2495 zfsvfs_t *zfsvfs;
2496
2497 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2498 break;
2499
2500 err = zfs_set_version(zfsvfs, intval);
2501 zfsvfs_rele(zfsvfs, FTAG);
2502
2503 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2504 zfs_cmd_t *zc;
2505
2506 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2507 (void) strlcpy(zc->zc_name, dsname,
2508 sizeof (zc->zc_name));
2509 (void) zfs_ioc_userspace_upgrade(zc);
2510 (void) zfs_ioc_id_quota_upgrade(zc);
2511 kmem_free(zc, sizeof (zfs_cmd_t));
2512 }
2513 break;
2514 }
2515 default:
2516 err = -1;
2517 }
2518
2519 return (err);
2520 }
2521
2522 /*
2523 * This function is best effort. If it fails to set any of the given properties,
2524 * it continues to set as many as it can and returns the last error
2525 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2526 * with the list of names of all the properties that failed along with the
2527 * corresponding error numbers.
2528 *
2529 * If every property is set successfully, zero is returned and errlist is not
2530 * modified.
2531 */
2532 int
2533 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2534 nvlist_t *errlist)
2535 {
2536 nvpair_t *pair;
2537 nvpair_t *propval;
2538 int rv = 0;
2539 uint64_t intval;
2540 char *strval;
2541
2542 nvlist_t *genericnvl = fnvlist_alloc();
2543 nvlist_t *retrynvl = fnvlist_alloc();
2544 retry:
2545 pair = NULL;
2546 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2547 const char *propname = nvpair_name(pair);
2548 zfs_prop_t prop = zfs_name_to_prop(propname);
2549 int err = 0;
2550
2551 /* decode the property value */
2552 propval = pair;
2553 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2554 nvlist_t *attrs;
2555 attrs = fnvpair_value_nvlist(pair);
2556 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2557 &propval) != 0)
2558 err = SET_ERROR(EINVAL);
2559 }
2560
2561 /* Validate value type */
2562 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2563 /* inherited properties are expected to be booleans */
2564 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2565 err = SET_ERROR(EINVAL);
2566 } else if (err == 0 && prop == ZPROP_INVAL) {
2567 if (zfs_prop_user(propname)) {
2568 if (nvpair_type(propval) != DATA_TYPE_STRING)
2569 err = SET_ERROR(EINVAL);
2570 } else if (zfs_prop_userquota(propname)) {
2571 if (nvpair_type(propval) !=
2572 DATA_TYPE_UINT64_ARRAY)
2573 err = SET_ERROR(EINVAL);
2574 } else {
2575 err = SET_ERROR(EINVAL);
2576 }
2577 } else if (err == 0) {
2578 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2579 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2580 err = SET_ERROR(EINVAL);
2581 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2582 const char *unused;
2583
2584 intval = fnvpair_value_uint64(propval);
2585
2586 switch (zfs_prop_get_type(prop)) {
2587 case PROP_TYPE_NUMBER:
2588 break;
2589 case PROP_TYPE_STRING:
2590 err = SET_ERROR(EINVAL);
2591 break;
2592 case PROP_TYPE_INDEX:
2593 if (zfs_prop_index_to_string(prop,
2594 intval, &unused) != 0)
2595 err = SET_ERROR(EINVAL);
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 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 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, *zsprev = NULL;
7613
7614 zfsdev_detach();
7615
7616 mutex_destroy(&zfsdev_state_lock);
7617
7618 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7619 if (zsprev)
7620 kmem_free(zsprev, sizeof (zfsdev_state_t));
7621 zsprev = zs;
7622 }
7623 if (zsprev)
7624 kmem_free(zsprev, sizeof (zfsdev_state_t));
7625
7626 zfs_fini();
7627 spa_fini();
7628 zvol_fini();
7629
7630 tsd_destroy(&zfs_fsyncer_key);
7631 tsd_destroy(&rrw_tsd_key);
7632 tsd_destroy(&zfs_allow_log_key);
7633 }