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