]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ioctl.c
Avoid retrieving unused snapshot props
[mirror_zfs.git] / module / zfs / zfs_ioctl.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright 2017 RackTop Systems.
38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39 * Copyright (c) 2019 Datto Inc.
40 */
41
42 /*
43 * ZFS ioctls.
44 *
45 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
46 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
47 *
48 * There are two ways that we handle ioctls: the legacy way where almost
49 * all of the logic is in the ioctl callback, and the new way where most
50 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
51 *
52 * Non-legacy ioctls should be registered by calling
53 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
54 * from userland by lzc_ioctl().
55 *
56 * The registration arguments are as follows:
57 *
58 * const char *name
59 * The name of the ioctl. This is used for history logging. If the
60 * ioctl returns successfully (the callback returns 0), and allow_log
61 * is true, then a history log entry will be recorded with the input &
62 * output nvlists. The log entry can be printed with "zpool history -i".
63 *
64 * zfs_ioc_t ioc
65 * The ioctl request number, which userland will pass to ioctl(2).
66 * We want newer versions of libzfs and libzfs_core to run against
67 * existing zfs kernel modules (i.e. a deferred reboot after an update).
68 * Therefore the ioctl numbers cannot change from release to release.
69 *
70 * zfs_secpolicy_func_t *secpolicy
71 * This function will be called before the zfs_ioc_func_t, to
72 * determine if this operation is permitted. It should return EPERM
73 * on failure, and 0 on success. Checks include determining if the
74 * dataset is visible in this zone, and if the user has either all
75 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
76 * to do this operation on this dataset with "zfs allow".
77 *
78 * zfs_ioc_namecheck_t namecheck
79 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
80 * name, a dataset name, or nothing. If the name is not well-formed,
81 * the ioctl will fail and the callback will not be called.
82 * Therefore, the callback can assume that the name is well-formed
83 * (e.g. is null-terminated, doesn't have more than one '@' character,
84 * doesn't have invalid characters).
85 *
86 * zfs_ioc_poolcheck_t pool_check
87 * This specifies requirements on the pool state. If the pool does
88 * not meet them (is suspended or is readonly), the ioctl will fail
89 * and the callback will not be called. If any checks are specified
90 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
91 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
92 * POOL_CHECK_READONLY).
93 *
94 * zfs_ioc_key_t *nvl_keys
95 * The list of expected/allowable innvl input keys. This list is used
96 * to validate the nvlist input to the ioctl.
97 *
98 * boolean_t smush_outnvlist
99 * If smush_outnvlist is true, then the output is presumed to be a
100 * list of errors, and it will be "smushed" down to fit into the
101 * caller's buffer, by removing some entries and replacing them with a
102 * single "N_MORE_ERRORS" entry indicating how many were removed. See
103 * nvlist_smush() for details. If smush_outnvlist is false, and the
104 * outnvlist does not fit into the userland-provided buffer, then the
105 * ioctl will fail with ENOMEM.
106 *
107 * zfs_ioc_func_t *func
108 * The callback function that will perform the operation.
109 *
110 * The callback should return 0 on success, or an error number on
111 * failure. If the function fails, the userland ioctl will return -1,
112 * and errno will be set to the callback's return value. The callback
113 * will be called with the following arguments:
114 *
115 * const char *name
116 * The name of the pool or dataset to operate on, from
117 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
118 * expected type (pool, dataset, or none).
119 *
120 * nvlist_t *innvl
121 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
122 * NULL if no input nvlist was provided. Changes to this nvlist are
123 * ignored. If the input nvlist could not be deserialized, the
124 * ioctl will fail and the callback will not be called.
125 *
126 * nvlist_t *outnvl
127 * The output nvlist, initially empty. The callback can fill it in,
128 * and it will be returned to userland by serializing it into
129 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
130 * fails (e.g. because the caller didn't supply a large enough
131 * buffer), then the overall ioctl will fail. See the
132 * 'smush_nvlist' argument above for additional behaviors.
133 *
134 * There are two typical uses of the output nvlist:
135 * - To return state, e.g. property values. In this case,
136 * smush_outnvlist should be false. If the buffer was not large
137 * enough, the caller will reallocate a larger buffer and try
138 * the ioctl again.
139 *
140 * - To return multiple errors from an ioctl which makes on-disk
141 * changes. In this case, smush_outnvlist should be true.
142 * Ioctls which make on-disk modifications should generally not
143 * use the outnvl if they succeed, because the caller can not
144 * distinguish between the operation failing, and
145 * deserialization failing.
146 *
147 * IOCTL Interface Errors
148 *
149 * The following ioctl input errors can be returned:
150 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
151 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
152 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
153 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
154 */
155
156 #include <sys/types.h>
157 #include <sys/param.h>
158 #include <sys/errno.h>
159 #include <sys/uio.h>
160 #include <sys/file.h>
161 #include <sys/kmem.h>
162 #include <sys/cmn_err.h>
163 #include <sys/stat.h>
164 #include <sys/zfs_ioctl.h>
165 #include <sys/zfs_vfsops.h>
166 #include <sys/zfs_znode.h>
167 #include <sys/zap.h>
168 #include <sys/spa.h>
169 #include <sys/spa_impl.h>
170 #include <sys/vdev.h>
171 #include <sys/vdev_impl.h>
172 #include <sys/dmu.h>
173 #include <sys/dsl_dir.h>
174 #include <sys/dsl_dataset.h>
175 #include <sys/dsl_prop.h>
176 #include <sys/dsl_deleg.h>
177 #include <sys/dmu_objset.h>
178 #include <sys/dmu_impl.h>
179 #include <sys/dmu_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_src iteration range nvlist
2319 * zc_nvlist_src_size size of iteration range nvlist
2320 *
2321 * outputs:
2322 * zc_name name of next snapshot
2323 * zc_objset_stats stats
2324 * zc_nvlist_dst property nvlist
2325 * zc_nvlist_dst_size size of property nvlist
2326 */
2327 static int
2328 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2329 {
2330 int error;
2331 objset_t *os, *ossnap;
2332 dsl_dataset_t *ds;
2333 uint64_t min_txg = 0, max_txg = 0;
2334
2335 if (zc->zc_nvlist_src_size != 0) {
2336 nvlist_t *props = NULL;
2337 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2338 zc->zc_iflags, &props);
2339 if (error != 0)
2340 return (error);
2341 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2342 &min_txg);
2343 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2344 &max_txg);
2345 nvlist_free(props);
2346 }
2347
2348 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2349 if (error != 0) {
2350 return (error == ENOENT ? ESRCH : error);
2351 }
2352
2353 /*
2354 * A dataset name of maximum length cannot have any snapshots,
2355 * so exit immediately.
2356 */
2357 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2358 ZFS_MAX_DATASET_NAME_LEN) {
2359 dmu_objset_rele(os, FTAG);
2360 return (SET_ERROR(ESRCH));
2361 }
2362
2363 while (error == 0) {
2364 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2365 error = SET_ERROR(EINTR);
2366 break;
2367 }
2368
2369 error = dmu_snapshot_list_next(os,
2370 sizeof (zc->zc_name) - strlen(zc->zc_name),
2371 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2372 &zc->zc_cookie, NULL);
2373 if (error == ENOENT) {
2374 error = SET_ERROR(ESRCH);
2375 break;
2376 } else if (error != 0) {
2377 break;
2378 }
2379
2380 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2381 FTAG, &ds);
2382 if (error != 0)
2383 break;
2384
2385 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2386 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2387 dsl_dataset_rele(ds, FTAG);
2388 /* undo snapshot name append */
2389 *(strchr(zc->zc_name, '@') + 1) = '\0';
2390 /* skip snapshot */
2391 continue;
2392 }
2393
2394 if (zc->zc_simple) {
2395 dsl_dataset_rele(ds, FTAG);
2396 break;
2397 }
2398
2399 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2400 dsl_dataset_rele(ds, FTAG);
2401 break;
2402 }
2403 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2404 dsl_dataset_rele(ds, FTAG);
2405 break;
2406 }
2407 dsl_dataset_rele(ds, FTAG);
2408 break;
2409 }
2410
2411 dmu_objset_rele(os, FTAG);
2412 /* if we failed, undo the @ that we tacked on to zc_name */
2413 if (error != 0)
2414 *strchr(zc->zc_name, '@') = '\0';
2415 return (error);
2416 }
2417
2418 static int
2419 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2420 {
2421 const char *propname = nvpair_name(pair);
2422 uint64_t *valary;
2423 unsigned int vallen;
2424 const char *domain;
2425 char *dash;
2426 zfs_userquota_prop_t type;
2427 uint64_t rid;
2428 uint64_t quota;
2429 zfsvfs_t *zfsvfs;
2430 int err;
2431
2432 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2433 nvlist_t *attrs;
2434 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2435 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2436 &pair) != 0)
2437 return (SET_ERROR(EINVAL));
2438 }
2439
2440 /*
2441 * A correctly constructed propname is encoded as
2442 * userquota@<rid>-<domain>.
2443 */
2444 if ((dash = strchr(propname, '-')) == NULL ||
2445 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2446 vallen != 3)
2447 return (SET_ERROR(EINVAL));
2448
2449 domain = dash + 1;
2450 type = valary[0];
2451 rid = valary[1];
2452 quota = valary[2];
2453
2454 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2455 if (err == 0) {
2456 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2457 zfsvfs_rele(zfsvfs, FTAG);
2458 }
2459
2460 return (err);
2461 }
2462
2463 /*
2464 * If the named property is one that has a special function to set its value,
2465 * return 0 on success and a positive error code on failure; otherwise if it is
2466 * not one of the special properties handled by this function, return -1.
2467 *
2468 * XXX: It would be better for callers of the property interface if we handled
2469 * these special cases in dsl_prop.c (in the dsl layer).
2470 */
2471 static int
2472 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2473 nvpair_t *pair)
2474 {
2475 const char *propname = nvpair_name(pair);
2476 zfs_prop_t prop = zfs_name_to_prop(propname);
2477 uint64_t intval = 0;
2478 char *strval = NULL;
2479 int err = -1;
2480
2481 if (prop == ZPROP_INVAL) {
2482 if (zfs_prop_userquota(propname))
2483 return (zfs_prop_set_userquota(dsname, pair));
2484 return (-1);
2485 }
2486
2487 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2488 nvlist_t *attrs;
2489 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2490 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2491 &pair) == 0);
2492 }
2493
2494 /* all special properties are numeric except for keylocation */
2495 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2496 strval = fnvpair_value_string(pair);
2497 } else {
2498 intval = fnvpair_value_uint64(pair);
2499 }
2500
2501 switch (prop) {
2502 case ZFS_PROP_QUOTA:
2503 err = dsl_dir_set_quota(dsname, source, intval);
2504 break;
2505 case ZFS_PROP_REFQUOTA:
2506 err = dsl_dataset_set_refquota(dsname, source, intval);
2507 break;
2508 case ZFS_PROP_FILESYSTEM_LIMIT:
2509 case ZFS_PROP_SNAPSHOT_LIMIT:
2510 if (intval == UINT64_MAX) {
2511 /* clearing the limit, just do it */
2512 err = 0;
2513 } else {
2514 err = dsl_dir_activate_fs_ss_limit(dsname);
2515 }
2516 /*
2517 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2518 * default path to set the value in the nvlist.
2519 */
2520 if (err == 0)
2521 err = -1;
2522 break;
2523 case ZFS_PROP_KEYLOCATION:
2524 err = dsl_crypto_can_set_keylocation(dsname, strval);
2525
2526 /*
2527 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2528 * default path to set the value in the nvlist.
2529 */
2530 if (err == 0)
2531 err = -1;
2532 break;
2533 case ZFS_PROP_RESERVATION:
2534 err = dsl_dir_set_reservation(dsname, source, intval);
2535 break;
2536 case ZFS_PROP_REFRESERVATION:
2537 err = dsl_dataset_set_refreservation(dsname, source, intval);
2538 break;
2539 case ZFS_PROP_VOLSIZE:
2540 err = zvol_set_volsize(dsname, intval);
2541 break;
2542 case ZFS_PROP_SNAPDEV:
2543 err = zvol_set_snapdev(dsname, source, intval);
2544 break;
2545 case ZFS_PROP_VOLMODE:
2546 err = zvol_set_volmode(dsname, source, intval);
2547 break;
2548 case ZFS_PROP_VERSION:
2549 {
2550 zfsvfs_t *zfsvfs;
2551
2552 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2553 break;
2554
2555 err = zfs_set_version(zfsvfs, intval);
2556 zfsvfs_rele(zfsvfs, FTAG);
2557
2558 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2559 zfs_cmd_t *zc;
2560
2561 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2562 (void) strcpy(zc->zc_name, dsname);
2563 (void) zfs_ioc_userspace_upgrade(zc);
2564 (void) zfs_ioc_id_quota_upgrade(zc);
2565 kmem_free(zc, sizeof (zfs_cmd_t));
2566 }
2567 break;
2568 }
2569 default:
2570 err = -1;
2571 }
2572
2573 return (err);
2574 }
2575
2576 /*
2577 * This function is best effort. If it fails to set any of the given properties,
2578 * it continues to set as many as it can and returns the last error
2579 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2580 * with the list of names of all the properties that failed along with the
2581 * corresponding error numbers.
2582 *
2583 * If every property is set successfully, zero is returned and errlist is not
2584 * modified.
2585 */
2586 int
2587 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2588 nvlist_t *errlist)
2589 {
2590 nvpair_t *pair;
2591 nvpair_t *propval;
2592 int rv = 0;
2593 uint64_t intval;
2594 char *strval;
2595
2596 nvlist_t *genericnvl = fnvlist_alloc();
2597 nvlist_t *retrynvl = fnvlist_alloc();
2598 retry:
2599 pair = NULL;
2600 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2601 const char *propname = nvpair_name(pair);
2602 zfs_prop_t prop = zfs_name_to_prop(propname);
2603 int err = 0;
2604
2605 /* decode the property value */
2606 propval = pair;
2607 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2608 nvlist_t *attrs;
2609 attrs = fnvpair_value_nvlist(pair);
2610 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2611 &propval) != 0)
2612 err = SET_ERROR(EINVAL);
2613 }
2614
2615 /* Validate value type */
2616 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2617 /* inherited properties are expected to be booleans */
2618 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2619 err = SET_ERROR(EINVAL);
2620 } else if (err == 0 && prop == ZPROP_INVAL) {
2621 if (zfs_prop_user(propname)) {
2622 if (nvpair_type(propval) != DATA_TYPE_STRING)
2623 err = SET_ERROR(EINVAL);
2624 } else if (zfs_prop_userquota(propname)) {
2625 if (nvpair_type(propval) !=
2626 DATA_TYPE_UINT64_ARRAY)
2627 err = SET_ERROR(EINVAL);
2628 } else {
2629 err = SET_ERROR(EINVAL);
2630 }
2631 } else if (err == 0) {
2632 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2633 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2634 err = SET_ERROR(EINVAL);
2635 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2636 const char *unused;
2637
2638 intval = fnvpair_value_uint64(propval);
2639
2640 switch (zfs_prop_get_type(prop)) {
2641 case PROP_TYPE_NUMBER:
2642 break;
2643 case PROP_TYPE_STRING:
2644 err = SET_ERROR(EINVAL);
2645 break;
2646 case PROP_TYPE_INDEX:
2647 if (zfs_prop_index_to_string(prop,
2648 intval, &unused) != 0)
2649 err = SET_ERROR(EINVAL);
2650 break;
2651 default:
2652 cmn_err(CE_PANIC,
2653 "unknown property type");
2654 }
2655 } else {
2656 err = SET_ERROR(EINVAL);
2657 }
2658 }
2659
2660 /* Validate permissions */
2661 if (err == 0)
2662 err = zfs_check_settable(dsname, pair, CRED());
2663
2664 if (err == 0) {
2665 if (source == ZPROP_SRC_INHERITED)
2666 err = -1; /* does not need special handling */
2667 else
2668 err = zfs_prop_set_special(dsname, source,
2669 pair);
2670 if (err == -1) {
2671 /*
2672 * For better performance we build up a list of
2673 * properties to set in a single transaction.
2674 */
2675 err = nvlist_add_nvpair(genericnvl, pair);
2676 } else if (err != 0 && nvl != retrynvl) {
2677 /*
2678 * This may be a spurious error caused by
2679 * receiving quota and reservation out of order.
2680 * Try again in a second pass.
2681 */
2682 err = nvlist_add_nvpair(retrynvl, pair);
2683 }
2684 }
2685
2686 if (err != 0) {
2687 if (errlist != NULL)
2688 fnvlist_add_int32(errlist, propname, err);
2689 rv = err;
2690 }
2691 }
2692
2693 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2694 nvl = retrynvl;
2695 goto retry;
2696 }
2697
2698 if (!nvlist_empty(genericnvl) &&
2699 dsl_props_set(dsname, source, genericnvl) != 0) {
2700 /*
2701 * If this fails, we still want to set as many properties as we
2702 * can, so try setting them individually.
2703 */
2704 pair = NULL;
2705 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2706 const char *propname = nvpair_name(pair);
2707 int err = 0;
2708
2709 propval = pair;
2710 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2711 nvlist_t *attrs;
2712 attrs = fnvpair_value_nvlist(pair);
2713 propval = fnvlist_lookup_nvpair(attrs,
2714 ZPROP_VALUE);
2715 }
2716
2717 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2718 strval = fnvpair_value_string(propval);
2719 err = dsl_prop_set_string(dsname, propname,
2720 source, strval);
2721 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2722 err = dsl_prop_inherit(dsname, propname,
2723 source);
2724 } else {
2725 intval = fnvpair_value_uint64(propval);
2726 err = dsl_prop_set_int(dsname, propname, source,
2727 intval);
2728 }
2729
2730 if (err != 0) {
2731 if (errlist != NULL) {
2732 fnvlist_add_int32(errlist, propname,
2733 err);
2734 }
2735 rv = err;
2736 }
2737 }
2738 }
2739 nvlist_free(genericnvl);
2740 nvlist_free(retrynvl);
2741
2742 return (rv);
2743 }
2744
2745 /*
2746 * Check that all the properties are valid user properties.
2747 */
2748 static int
2749 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2750 {
2751 nvpair_t *pair = NULL;
2752 int error = 0;
2753
2754 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2755 const char *propname = nvpair_name(pair);
2756
2757 if (!zfs_prop_user(propname) ||
2758 nvpair_type(pair) != DATA_TYPE_STRING)
2759 return (SET_ERROR(EINVAL));
2760
2761 if ((error = zfs_secpolicy_write_perms(fsname,
2762 ZFS_DELEG_PERM_USERPROP, CRED())))
2763 return (error);
2764
2765 if (strlen(propname) >= ZAP_MAXNAMELEN)
2766 return (SET_ERROR(ENAMETOOLONG));
2767
2768 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2769 return (SET_ERROR(E2BIG));
2770 }
2771 return (0);
2772 }
2773
2774 static void
2775 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2776 {
2777 nvpair_t *pair;
2778
2779 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2780
2781 pair = NULL;
2782 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2783 if (nvlist_exists(skipped, nvpair_name(pair)))
2784 continue;
2785
2786 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2787 }
2788 }
2789
2790 static int
2791 clear_received_props(const char *dsname, nvlist_t *props,
2792 nvlist_t *skipped)
2793 {
2794 int err = 0;
2795 nvlist_t *cleared_props = NULL;
2796 props_skip(props, skipped, &cleared_props);
2797 if (!nvlist_empty(cleared_props)) {
2798 /*
2799 * Acts on local properties until the dataset has received
2800 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2801 */
2802 zprop_source_t flags = (ZPROP_SRC_NONE |
2803 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2804 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2805 }
2806 nvlist_free(cleared_props);
2807 return (err);
2808 }
2809
2810 /*
2811 * inputs:
2812 * zc_name name of filesystem
2813 * zc_value name of property to set
2814 * zc_nvlist_src{_size} nvlist of properties to apply
2815 * zc_cookie received properties flag
2816 *
2817 * outputs:
2818 * zc_nvlist_dst{_size} error for each unapplied received property
2819 */
2820 static int
2821 zfs_ioc_set_prop(zfs_cmd_t *zc)
2822 {
2823 nvlist_t *nvl;
2824 boolean_t received = zc->zc_cookie;
2825 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2826 ZPROP_SRC_LOCAL);
2827 nvlist_t *errors;
2828 int error;
2829
2830 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2831 zc->zc_iflags, &nvl)) != 0)
2832 return (error);
2833
2834 if (received) {
2835 nvlist_t *origprops;
2836
2837 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2838 (void) clear_received_props(zc->zc_name,
2839 origprops, nvl);
2840 nvlist_free(origprops);
2841 }
2842
2843 error = dsl_prop_set_hasrecvd(zc->zc_name);
2844 }
2845
2846 errors = fnvlist_alloc();
2847 if (error == 0)
2848 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2849
2850 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2851 (void) put_nvlist(zc, errors);
2852 }
2853
2854 nvlist_free(errors);
2855 nvlist_free(nvl);
2856 return (error);
2857 }
2858
2859 /*
2860 * inputs:
2861 * zc_name name of filesystem
2862 * zc_value name of property to inherit
2863 * zc_cookie revert to received value if TRUE
2864 *
2865 * outputs: none
2866 */
2867 static int
2868 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2869 {
2870 const char *propname = zc->zc_value;
2871 zfs_prop_t prop = zfs_name_to_prop(propname);
2872 boolean_t received = zc->zc_cookie;
2873 zprop_source_t source = (received
2874 ? ZPROP_SRC_NONE /* revert to received value, if any */
2875 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2876 nvlist_t *dummy;
2877 nvpair_t *pair;
2878 zprop_type_t type;
2879 int err;
2880
2881 if (!received) {
2882 /*
2883 * Only check this in the non-received case. We want to allow
2884 * 'inherit -S' to revert non-inheritable properties like quota
2885 * and reservation to the received or default values even though
2886 * they are not considered inheritable.
2887 */
2888 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2889 return (SET_ERROR(EINVAL));
2890 }
2891
2892 if (prop == ZPROP_INVAL) {
2893 if (!zfs_prop_user(propname))
2894 return (SET_ERROR(EINVAL));
2895
2896 type = PROP_TYPE_STRING;
2897 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2898 return (SET_ERROR(EINVAL));
2899 } else {
2900 type = zfs_prop_get_type(prop);
2901 }
2902
2903 /*
2904 * zfs_prop_set_special() expects properties in the form of an
2905 * nvpair with type info.
2906 */
2907 dummy = fnvlist_alloc();
2908
2909 switch (type) {
2910 case PROP_TYPE_STRING:
2911 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2912 break;
2913 case PROP_TYPE_NUMBER:
2914 case PROP_TYPE_INDEX:
2915 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2916 break;
2917 default:
2918 err = SET_ERROR(EINVAL);
2919 goto errout;
2920 }
2921
2922 pair = nvlist_next_nvpair(dummy, NULL);
2923 if (pair == NULL) {
2924 err = SET_ERROR(EINVAL);
2925 } else {
2926 err = zfs_prop_set_special(zc->zc_name, source, pair);
2927 if (err == -1) /* property is not "special", needs handling */
2928 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2929 source);
2930 }
2931
2932 errout:
2933 nvlist_free(dummy);
2934 return (err);
2935 }
2936
2937 static int
2938 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2939 {
2940 nvlist_t *props;
2941 spa_t *spa;
2942 int error;
2943 nvpair_t *pair;
2944
2945 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2946 zc->zc_iflags, &props)))
2947 return (error);
2948
2949 /*
2950 * If the only property is the configfile, then just do a spa_lookup()
2951 * to handle the faulted case.
2952 */
2953 pair = nvlist_next_nvpair(props, NULL);
2954 if (pair != NULL && strcmp(nvpair_name(pair),
2955 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2956 nvlist_next_nvpair(props, pair) == NULL) {
2957 mutex_enter(&spa_namespace_lock);
2958 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2959 spa_configfile_set(spa, props, B_FALSE);
2960 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2961 }
2962 mutex_exit(&spa_namespace_lock);
2963 if (spa != NULL) {
2964 nvlist_free(props);
2965 return (0);
2966 }
2967 }
2968
2969 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2970 nvlist_free(props);
2971 return (error);
2972 }
2973
2974 error = spa_prop_set(spa, props);
2975
2976 nvlist_free(props);
2977 spa_close(spa, FTAG);
2978
2979 return (error);
2980 }
2981
2982 static int
2983 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2984 {
2985 spa_t *spa;
2986 int error;
2987 nvlist_t *nvp = NULL;
2988
2989 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2990 /*
2991 * If the pool is faulted, there may be properties we can still
2992 * get (such as altroot and cachefile), so attempt to get them
2993 * anyway.
2994 */
2995 mutex_enter(&spa_namespace_lock);
2996 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2997 error = spa_prop_get(spa, &nvp);
2998 mutex_exit(&spa_namespace_lock);
2999 } else {
3000 error = spa_prop_get(spa, &nvp);
3001 spa_close(spa, FTAG);
3002 }
3003
3004 if (error == 0 && zc->zc_nvlist_dst != 0)
3005 error = put_nvlist(zc, nvp);
3006 else
3007 error = SET_ERROR(EFAULT);
3008
3009 nvlist_free(nvp);
3010 return (error);
3011 }
3012
3013 /*
3014 * inputs:
3015 * zc_name name of filesystem
3016 * zc_nvlist_src{_size} nvlist of delegated permissions
3017 * zc_perm_action allow/unallow flag
3018 *
3019 * outputs: none
3020 */
3021 static int
3022 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3023 {
3024 int error;
3025 nvlist_t *fsaclnv = NULL;
3026
3027 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3028 zc->zc_iflags, &fsaclnv)) != 0)
3029 return (error);
3030
3031 /*
3032 * Verify nvlist is constructed correctly
3033 */
3034 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3035 nvlist_free(fsaclnv);
3036 return (SET_ERROR(EINVAL));
3037 }
3038
3039 /*
3040 * If we don't have PRIV_SYS_MOUNT, then validate
3041 * that user is allowed to hand out each permission in
3042 * the nvlist(s)
3043 */
3044
3045 error = secpolicy_zfs(CRED());
3046 if (error != 0) {
3047 if (zc->zc_perm_action == B_FALSE) {
3048 error = dsl_deleg_can_allow(zc->zc_name,
3049 fsaclnv, CRED());
3050 } else {
3051 error = dsl_deleg_can_unallow(zc->zc_name,
3052 fsaclnv, CRED());
3053 }
3054 }
3055
3056 if (error == 0)
3057 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3058
3059 nvlist_free(fsaclnv);
3060 return (error);
3061 }
3062
3063 /*
3064 * inputs:
3065 * zc_name name of filesystem
3066 *
3067 * outputs:
3068 * zc_nvlist_src{_size} nvlist of delegated permissions
3069 */
3070 static int
3071 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3072 {
3073 nvlist_t *nvp;
3074 int error;
3075
3076 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3077 error = put_nvlist(zc, nvp);
3078 nvlist_free(nvp);
3079 }
3080
3081 return (error);
3082 }
3083
3084 /* ARGSUSED */
3085 static void
3086 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3087 {
3088 zfs_creat_t *zct = arg;
3089
3090 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3091 }
3092
3093 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3094
3095 /*
3096 * inputs:
3097 * os parent objset pointer (NULL if root fs)
3098 * fuids_ok fuids allowed in this version of the spa?
3099 * sa_ok SAs allowed in this version of the spa?
3100 * createprops list of properties requested by creator
3101 *
3102 * outputs:
3103 * zplprops values for the zplprops we attach to the master node object
3104 * is_ci true if requested file system will be purely case-insensitive
3105 *
3106 * Determine the settings for utf8only, normalization and
3107 * casesensitivity. Specific values may have been requested by the
3108 * creator and/or we can inherit values from the parent dataset. If
3109 * the file system is of too early a vintage, a creator can not
3110 * request settings for these properties, even if the requested
3111 * setting is the default value. We don't actually want to create dsl
3112 * properties for these, so remove them from the source nvlist after
3113 * processing.
3114 */
3115 static int
3116 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3117 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3118 nvlist_t *zplprops, boolean_t *is_ci)
3119 {
3120 uint64_t sense = ZFS_PROP_UNDEFINED;
3121 uint64_t norm = ZFS_PROP_UNDEFINED;
3122 uint64_t u8 = ZFS_PROP_UNDEFINED;
3123 int error;
3124
3125 ASSERT(zplprops != NULL);
3126
3127 /* parent dataset must be a filesystem */
3128 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3129 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3130
3131 /*
3132 * Pull out creator prop choices, if any.
3133 */
3134 if (createprops) {
3135 (void) nvlist_lookup_uint64(createprops,
3136 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3137 (void) nvlist_lookup_uint64(createprops,
3138 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3139 (void) nvlist_remove_all(createprops,
3140 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3141 (void) nvlist_lookup_uint64(createprops,
3142 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3143 (void) nvlist_remove_all(createprops,
3144 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3145 (void) nvlist_lookup_uint64(createprops,
3146 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3147 (void) nvlist_remove_all(createprops,
3148 zfs_prop_to_name(ZFS_PROP_CASE));
3149 }
3150
3151 /*
3152 * If the zpl version requested is whacky or the file system
3153 * or pool is version is too "young" to support normalization
3154 * and the creator tried to set a value for one of the props,
3155 * error out.
3156 */
3157 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3158 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3159 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3160 (zplver < ZPL_VERSION_NORMALIZATION &&
3161 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3162 sense != ZFS_PROP_UNDEFINED)))
3163 return (SET_ERROR(ENOTSUP));
3164
3165 /*
3166 * Put the version in the zplprops
3167 */
3168 VERIFY(nvlist_add_uint64(zplprops,
3169 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3170
3171 if (norm == ZFS_PROP_UNDEFINED &&
3172 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3173 return (error);
3174 VERIFY(nvlist_add_uint64(zplprops,
3175 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3176
3177 /*
3178 * If we're normalizing, names must always be valid UTF-8 strings.
3179 */
3180 if (norm)
3181 u8 = 1;
3182 if (u8 == ZFS_PROP_UNDEFINED &&
3183 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3184 return (error);
3185 VERIFY(nvlist_add_uint64(zplprops,
3186 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3187
3188 if (sense == ZFS_PROP_UNDEFINED &&
3189 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3190 return (error);
3191 VERIFY(nvlist_add_uint64(zplprops,
3192 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3193
3194 if (is_ci)
3195 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3196
3197 return (0);
3198 }
3199
3200 static int
3201 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3202 nvlist_t *zplprops, boolean_t *is_ci)
3203 {
3204 boolean_t fuids_ok, sa_ok;
3205 uint64_t zplver = ZPL_VERSION;
3206 objset_t *os = NULL;
3207 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3208 spa_t *spa;
3209 uint64_t spa_vers;
3210 int error;
3211
3212 zfs_get_parent(dataset, parentname, sizeof (parentname));
3213
3214 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3215 return (error);
3216
3217 spa_vers = spa_version(spa);
3218 spa_close(spa, FTAG);
3219
3220 zplver = zfs_zpl_version_map(spa_vers);
3221 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3222 sa_ok = (zplver >= ZPL_VERSION_SA);
3223
3224 /*
3225 * Open parent object set so we can inherit zplprop values.
3226 */
3227 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3228 return (error);
3229
3230 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3231 zplprops, is_ci);
3232 dmu_objset_rele(os, FTAG);
3233 return (error);
3234 }
3235
3236 static int
3237 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3238 nvlist_t *zplprops, boolean_t *is_ci)
3239 {
3240 boolean_t fuids_ok;
3241 boolean_t sa_ok;
3242 uint64_t zplver = ZPL_VERSION;
3243 int error;
3244
3245 zplver = zfs_zpl_version_map(spa_vers);
3246 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3247 sa_ok = (zplver >= ZPL_VERSION_SA);
3248
3249 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3250 createprops, zplprops, is_ci);
3251 return (error);
3252 }
3253
3254 /*
3255 * innvl: {
3256 * "type" -> dmu_objset_type_t (int32)
3257 * (optional) "props" -> { prop -> value }
3258 * (optional) "hidden_args" -> { "wkeydata" -> value }
3259 * raw uint8_t array of encryption wrapping key data (32 bytes)
3260 * }
3261 *
3262 * outnvl: propname -> error code (int32)
3263 */
3264
3265 static const zfs_ioc_key_t zfs_keys_create[] = {
3266 {"type", DATA_TYPE_INT32, 0},
3267 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3268 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3269 };
3270
3271 static int
3272 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3273 {
3274 int error = 0;
3275 zfs_creat_t zct = { 0 };
3276 nvlist_t *nvprops = NULL;
3277 nvlist_t *hidden_args = NULL;
3278 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3279 dmu_objset_type_t type;
3280 boolean_t is_insensitive = B_FALSE;
3281 dsl_crypto_params_t *dcp = NULL;
3282
3283 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3284 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3285 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3286
3287 switch (type) {
3288 case DMU_OST_ZFS:
3289 cbfunc = zfs_create_cb;
3290 break;
3291
3292 case DMU_OST_ZVOL:
3293 cbfunc = zvol_create_cb;
3294 break;
3295
3296 default:
3297 cbfunc = NULL;
3298 break;
3299 }
3300 if (strchr(fsname, '@') ||
3301 strchr(fsname, '%'))
3302 return (SET_ERROR(EINVAL));
3303
3304 zct.zct_props = nvprops;
3305
3306 if (cbfunc == NULL)
3307 return (SET_ERROR(EINVAL));
3308
3309 if (type == DMU_OST_ZVOL) {
3310 uint64_t volsize, volblocksize;
3311
3312 if (nvprops == NULL)
3313 return (SET_ERROR(EINVAL));
3314 if (nvlist_lookup_uint64(nvprops,
3315 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3316 return (SET_ERROR(EINVAL));
3317
3318 if ((error = nvlist_lookup_uint64(nvprops,
3319 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3320 &volblocksize)) != 0 && error != ENOENT)
3321 return (SET_ERROR(EINVAL));
3322
3323 if (error != 0)
3324 volblocksize = zfs_prop_default_numeric(
3325 ZFS_PROP_VOLBLOCKSIZE);
3326
3327 if ((error = zvol_check_volblocksize(fsname,
3328 volblocksize)) != 0 ||
3329 (error = zvol_check_volsize(volsize,
3330 volblocksize)) != 0)
3331 return (error);
3332 } else if (type == DMU_OST_ZFS) {
3333 int error;
3334
3335 /*
3336 * We have to have normalization and
3337 * case-folding flags correct when we do the
3338 * file system creation, so go figure them out
3339 * now.
3340 */
3341 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3342 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3343 error = zfs_fill_zplprops(fsname, nvprops,
3344 zct.zct_zplprops, &is_insensitive);
3345 if (error != 0) {
3346 nvlist_free(zct.zct_zplprops);
3347 return (error);
3348 }
3349 }
3350
3351 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3352 hidden_args, &dcp);
3353 if (error != 0) {
3354 nvlist_free(zct.zct_zplprops);
3355 return (error);
3356 }
3357
3358 error = dmu_objset_create(fsname, type,
3359 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3360
3361 nvlist_free(zct.zct_zplprops);
3362 dsl_crypto_params_free(dcp, !!error);
3363
3364 /*
3365 * It would be nice to do this atomically.
3366 */
3367 if (error == 0) {
3368 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3369 nvprops, outnvl);
3370 if (error != 0) {
3371 spa_t *spa;
3372 int error2;
3373
3374 /*
3375 * Volumes will return EBUSY and cannot be destroyed
3376 * until all asynchronous minor handling has completed.
3377 * Wait for the spa_zvol_taskq to drain then retry.
3378 */
3379 error2 = dsl_destroy_head(fsname);
3380 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3381 error2 = spa_open(fsname, &spa, FTAG);
3382 if (error2 == 0) {
3383 taskq_wait(spa->spa_zvol_taskq);
3384 spa_close(spa, FTAG);
3385 }
3386 error2 = dsl_destroy_head(fsname);
3387 }
3388 }
3389 }
3390 return (error);
3391 }
3392
3393 /*
3394 * innvl: {
3395 * "origin" -> name of origin snapshot
3396 * (optional) "props" -> { prop -> value }
3397 * (optional) "hidden_args" -> { "wkeydata" -> value }
3398 * raw uint8_t array of encryption wrapping key data (32 bytes)
3399 * }
3400 *
3401 * outputs:
3402 * outnvl: propname -> error code (int32)
3403 */
3404 static const zfs_ioc_key_t zfs_keys_clone[] = {
3405 {"origin", DATA_TYPE_STRING, 0},
3406 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3407 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3408 };
3409
3410 static int
3411 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3412 {
3413 int error = 0;
3414 nvlist_t *nvprops = NULL;
3415 char *origin_name;
3416
3417 origin_name = fnvlist_lookup_string(innvl, "origin");
3418 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3419
3420 if (strchr(fsname, '@') ||
3421 strchr(fsname, '%'))
3422 return (SET_ERROR(EINVAL));
3423
3424 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3425 return (SET_ERROR(EINVAL));
3426
3427 error = dmu_objset_clone(fsname, origin_name);
3428
3429 /*
3430 * It would be nice to do this atomically.
3431 */
3432 if (error == 0) {
3433 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3434 nvprops, outnvl);
3435 if (error != 0)
3436 (void) dsl_destroy_head(fsname);
3437 }
3438 return (error);
3439 }
3440
3441 static const zfs_ioc_key_t zfs_keys_remap[] = {
3442 /* no nvl keys */
3443 };
3444
3445 /* ARGSUSED */
3446 static int
3447 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3448 {
3449 if (strchr(fsname, '@') ||
3450 strchr(fsname, '%'))
3451 return (SET_ERROR(EINVAL));
3452
3453 return (dmu_objset_remap_indirects(fsname));
3454 }
3455
3456 /*
3457 * innvl: {
3458 * "snaps" -> { snapshot1, snapshot2 }
3459 * (optional) "props" -> { prop -> value (string) }
3460 * }
3461 *
3462 * outnvl: snapshot -> error code (int32)
3463 */
3464 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3465 {"snaps", DATA_TYPE_NVLIST, 0},
3466 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3467 };
3468
3469 static int
3470 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3471 {
3472 nvlist_t *snaps;
3473 nvlist_t *props = NULL;
3474 int error, poollen;
3475 nvpair_t *pair;
3476
3477 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3478 if ((error = zfs_check_userprops(poolname, props)) != 0)
3479 return (error);
3480
3481 if (!nvlist_empty(props) &&
3482 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3483 return (SET_ERROR(ENOTSUP));
3484
3485 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3486 poollen = strlen(poolname);
3487 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3488 pair = nvlist_next_nvpair(snaps, pair)) {
3489 const char *name = nvpair_name(pair);
3490 const char *cp = strchr(name, '@');
3491
3492 /*
3493 * The snap name must contain an @, and the part after it must
3494 * contain only valid characters.
3495 */
3496 if (cp == NULL ||
3497 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3498 return (SET_ERROR(EINVAL));
3499
3500 /*
3501 * The snap must be in the specified pool.
3502 */
3503 if (strncmp(name, poolname, poollen) != 0 ||
3504 (name[poollen] != '/' && name[poollen] != '@'))
3505 return (SET_ERROR(EXDEV));
3506
3507 /* This must be the only snap of this fs. */
3508 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3509 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3510 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3511 == 0) {
3512 return (SET_ERROR(EXDEV));
3513 }
3514 }
3515 }
3516
3517 error = dsl_dataset_snapshot(snaps, props, outnvl);
3518
3519 return (error);
3520 }
3521
3522 /*
3523 * innvl: "message" -> string
3524 */
3525 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3526 {"message", DATA_TYPE_STRING, 0},
3527 };
3528
3529 /* ARGSUSED */
3530 static int
3531 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3532 {
3533 char *message;
3534 spa_t *spa;
3535 int error;
3536 char *poolname;
3537
3538 /*
3539 * The poolname in the ioctl is not set, we get it from the TSD,
3540 * which was set at the end of the last successful ioctl that allows
3541 * logging. The secpolicy func already checked that it is set.
3542 * Only one log ioctl is allowed after each successful ioctl, so
3543 * we clear the TSD here.
3544 */
3545 poolname = tsd_get(zfs_allow_log_key);
3546 if (poolname == NULL)
3547 return (SET_ERROR(EINVAL));
3548 (void) tsd_set(zfs_allow_log_key, NULL);
3549 error = spa_open(poolname, &spa, FTAG);
3550 strfree(poolname);
3551 if (error != 0)
3552 return (error);
3553
3554 message = fnvlist_lookup_string(innvl, "message");
3555
3556 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3557 spa_close(spa, FTAG);
3558 return (SET_ERROR(ENOTSUP));
3559 }
3560
3561 error = spa_history_log(spa, message);
3562 spa_close(spa, FTAG);
3563 return (error);
3564 }
3565
3566 /*
3567 * The dp_config_rwlock must not be held when calling this, because the
3568 * unmount may need to write out data.
3569 *
3570 * This function is best-effort. Callers must deal gracefully if it
3571 * remains mounted (or is remounted after this call).
3572 *
3573 * Returns 0 if the argument is not a snapshot, or it is not currently a
3574 * filesystem, or we were able to unmount it. Returns error code otherwise.
3575 */
3576 void
3577 zfs_unmount_snap(const char *snapname)
3578 {
3579 if (strchr(snapname, '@') == NULL)
3580 return;
3581
3582 (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
3583 }
3584
3585 /* ARGSUSED */
3586 static int
3587 zfs_unmount_snap_cb(const char *snapname, void *arg)
3588 {
3589 zfs_unmount_snap(snapname);
3590 return (0);
3591 }
3592
3593 /*
3594 * When a clone is destroyed, its origin may also need to be destroyed,
3595 * in which case it must be unmounted. This routine will do that unmount
3596 * if necessary.
3597 */
3598 void
3599 zfs_destroy_unmount_origin(const char *fsname)
3600 {
3601 int error;
3602 objset_t *os;
3603 dsl_dataset_t *ds;
3604
3605 error = dmu_objset_hold(fsname, FTAG, &os);
3606 if (error != 0)
3607 return;
3608 ds = dmu_objset_ds(os);
3609 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3610 char originname[ZFS_MAX_DATASET_NAME_LEN];
3611 dsl_dataset_name(ds->ds_prev, originname);
3612 dmu_objset_rele(os, FTAG);
3613 zfs_unmount_snap(originname);
3614 } else {
3615 dmu_objset_rele(os, FTAG);
3616 }
3617 }
3618
3619 /*
3620 * innvl: {
3621 * "snaps" -> { snapshot1, snapshot2 }
3622 * (optional boolean) "defer"
3623 * }
3624 *
3625 * outnvl: snapshot -> error code (int32)
3626 */
3627 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3628 {"snaps", DATA_TYPE_NVLIST, 0},
3629 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3630 };
3631
3632 /* ARGSUSED */
3633 static int
3634 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3635 {
3636 nvlist_t *snaps;
3637 nvpair_t *pair;
3638 boolean_t defer;
3639
3640 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3641 defer = nvlist_exists(innvl, "defer");
3642
3643 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3644 pair = nvlist_next_nvpair(snaps, pair)) {
3645 zfs_unmount_snap(nvpair_name(pair));
3646 }
3647
3648 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3649 }
3650
3651 /*
3652 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3653 * All bookmarks must be in the same pool.
3654 *
3655 * innvl: {
3656 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3657 * }
3658 *
3659 * outnvl: bookmark -> error code (int32)
3660 *
3661 */
3662 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3663 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3664 };
3665
3666 /* ARGSUSED */
3667 static int
3668 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3669 {
3670 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3671 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3672 char *snap_name;
3673
3674 /*
3675 * Verify the snapshot argument.
3676 */
3677 if (nvpair_value_string(pair, &snap_name) != 0)
3678 return (SET_ERROR(EINVAL));
3679
3680
3681 /* Verify that the keys (bookmarks) are unique */
3682 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3683 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3684 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3685 return (SET_ERROR(EINVAL));
3686 }
3687 }
3688
3689 return (dsl_bookmark_create(innvl, outnvl));
3690 }
3691
3692 /*
3693 * innvl: {
3694 * property 1, property 2, ...
3695 * }
3696 *
3697 * outnvl: {
3698 * bookmark name 1 -> { property 1, property 2, ... },
3699 * bookmark name 2 -> { property 1, property 2, ... }
3700 * }
3701 *
3702 */
3703 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3704 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3705 };
3706
3707 static int
3708 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3709 {
3710 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3711 }
3712
3713 /*
3714 * innvl: {
3715 * bookmark name 1, bookmark name 2
3716 * }
3717 *
3718 * outnvl: bookmark -> error code (int32)
3719 *
3720 */
3721 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3722 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3723 };
3724
3725 static int
3726 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3727 nvlist_t *outnvl)
3728 {
3729 int error, poollen;
3730
3731 poollen = strlen(poolname);
3732 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3733 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3734 const char *name = nvpair_name(pair);
3735 const char *cp = strchr(name, '#');
3736
3737 /*
3738 * The bookmark name must contain an #, and the part after it
3739 * must contain only valid characters.
3740 */
3741 if (cp == NULL ||
3742 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3743 return (SET_ERROR(EINVAL));
3744
3745 /*
3746 * The bookmark must be in the specified pool.
3747 */
3748 if (strncmp(name, poolname, poollen) != 0 ||
3749 (name[poollen] != '/' && name[poollen] != '#'))
3750 return (SET_ERROR(EXDEV));
3751 }
3752
3753 error = dsl_bookmark_destroy(innvl, outnvl);
3754 return (error);
3755 }
3756
3757 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3758 {"program", DATA_TYPE_STRING, 0},
3759 {"arg", DATA_TYPE_ANY, 0},
3760 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3761 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3762 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3763 };
3764
3765 static int
3766 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3767 nvlist_t *outnvl)
3768 {
3769 char *program;
3770 uint64_t instrlimit, memlimit;
3771 boolean_t sync_flag;
3772 nvpair_t *nvarg = NULL;
3773
3774 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3775 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3776 sync_flag = B_TRUE;
3777 }
3778 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3779 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3780 }
3781 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3782 memlimit = ZCP_DEFAULT_MEMLIMIT;
3783 }
3784 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3785
3786 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3787 return (EINVAL);
3788 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3789 return (EINVAL);
3790
3791 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3792 nvarg, outnvl));
3793 }
3794
3795 /*
3796 * innvl: unused
3797 * outnvl: empty
3798 */
3799 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3800 /* no nvl keys */
3801 };
3802
3803 /* ARGSUSED */
3804 static int
3805 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3806 {
3807 return (spa_checkpoint(poolname));
3808 }
3809
3810 /*
3811 * innvl: unused
3812 * outnvl: empty
3813 */
3814 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3815 /* no nvl keys */
3816 };
3817
3818 /* ARGSUSED */
3819 static int
3820 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3821 nvlist_t *outnvl)
3822 {
3823 return (spa_checkpoint_discard(poolname));
3824 }
3825
3826 /*
3827 * inputs:
3828 * zc_name name of dataset to destroy
3829 * zc_defer_destroy mark for deferred destroy
3830 *
3831 * outputs: none
3832 */
3833 static int
3834 zfs_ioc_destroy(zfs_cmd_t *zc)
3835 {
3836 objset_t *os;
3837 dmu_objset_type_t ost;
3838 int err;
3839
3840 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3841 if (err != 0)
3842 return (err);
3843 ost = dmu_objset_type(os);
3844 dmu_objset_rele(os, FTAG);
3845
3846 if (ost == DMU_OST_ZFS)
3847 zfs_unmount_snap(zc->zc_name);
3848
3849 if (strchr(zc->zc_name, '@')) {
3850 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3851 } else {
3852 err = dsl_destroy_head(zc->zc_name);
3853 if (err == EEXIST) {
3854 /*
3855 * It is possible that the given DS may have
3856 * hidden child (%recv) datasets - "leftovers"
3857 * resulting from the previously interrupted
3858 * 'zfs receive'.
3859 *
3860 * 6 extra bytes for /%recv
3861 */
3862 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3863
3864 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3865 zc->zc_name, recv_clone_name) >=
3866 sizeof (namebuf))
3867 return (SET_ERROR(EINVAL));
3868
3869 /*
3870 * Try to remove the hidden child (%recv) and after
3871 * that try to remove the target dataset.
3872 * If the hidden child (%recv) does not exist
3873 * the original error (EEXIST) will be returned
3874 */
3875 err = dsl_destroy_head(namebuf);
3876 if (err == 0)
3877 err = dsl_destroy_head(zc->zc_name);
3878 else if (err == ENOENT)
3879 err = SET_ERROR(EEXIST);
3880 }
3881 }
3882
3883 return (err);
3884 }
3885
3886 /*
3887 * innvl: {
3888 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|DO|SUSPEND} (uint64)
3889 * "initialize_vdevs": { -> guids to initialize (nvlist)
3890 * "vdev_path_1": vdev_guid_1, (uint64),
3891 * "vdev_path_2": vdev_guid_2, (uint64),
3892 * ...
3893 * },
3894 * }
3895 *
3896 * outnvl: {
3897 * "initialize_vdevs": { -> initialization errors (nvlist)
3898 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3899 * "vdev_path_2": errno, ... (uint64)
3900 * ...
3901 * }
3902 * }
3903 *
3904 * EINVAL is returned for an unknown commands or if any of the provided vdev
3905 * guids have be specified with a type other than uint64.
3906 */
3907 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
3908 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
3909 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
3910 };
3911
3912 static int
3913 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3914 {
3915 uint64_t cmd_type;
3916 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3917 &cmd_type) != 0) {
3918 return (SET_ERROR(EINVAL));
3919 }
3920
3921 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3922 cmd_type == POOL_INITIALIZE_DO ||
3923 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3924 return (SET_ERROR(EINVAL));
3925 }
3926
3927 nvlist_t *vdev_guids;
3928 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3929 &vdev_guids) != 0) {
3930 return (SET_ERROR(EINVAL));
3931 }
3932
3933 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3934 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3935 uint64_t vdev_guid;
3936 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
3937 return (SET_ERROR(EINVAL));
3938 }
3939 }
3940
3941 spa_t *spa;
3942 int error = spa_open(poolname, &spa, FTAG);
3943 if (error != 0)
3944 return (error);
3945
3946 nvlist_t *vdev_errlist = fnvlist_alloc();
3947 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
3948 vdev_errlist);
3949
3950 if (fnvlist_size(vdev_errlist) > 0) {
3951 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3952 vdev_errlist);
3953 }
3954 fnvlist_free(vdev_errlist);
3955
3956 spa_close(spa, FTAG);
3957 return (total_errors > 0 ? EINVAL : 0);
3958 }
3959
3960 /*
3961 * fsname is name of dataset to rollback (to most recent snapshot)
3962 *
3963 * innvl may contain name of expected target snapshot
3964 *
3965 * outnvl: "target" -> name of most recent snapshot
3966 * }
3967 */
3968 static const zfs_ioc_key_t zfs_keys_rollback[] = {
3969 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
3970 };
3971
3972 /* ARGSUSED */
3973 static int
3974 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3975 {
3976 zfsvfs_t *zfsvfs;
3977 zvol_state_t *zv;
3978 char *target = NULL;
3979 int error;
3980
3981 (void) nvlist_lookup_string(innvl, "target", &target);
3982 if (target != NULL) {
3983 const char *cp = strchr(target, '@');
3984
3985 /*
3986 * The snap name must contain an @, and the part after it must
3987 * contain only valid characters.
3988 */
3989 if (cp == NULL ||
3990 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3991 return (SET_ERROR(EINVAL));
3992 }
3993
3994 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3995 dsl_dataset_t *ds;
3996
3997 ds = dmu_objset_ds(zfsvfs->z_os);
3998 error = zfs_suspend_fs(zfsvfs);
3999 if (error == 0) {
4000 int resume_err;
4001
4002 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4003 outnvl);
4004 resume_err = zfs_resume_fs(zfsvfs, ds);
4005 error = error ? error : resume_err;
4006 }
4007 deactivate_super(zfsvfs->z_sb);
4008 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4009 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4010 outnvl);
4011 zvol_resume(zv);
4012 } else {
4013 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4014 }
4015 return (error);
4016 }
4017
4018 static int
4019 recursive_unmount(const char *fsname, void *arg)
4020 {
4021 const char *snapname = arg;
4022 char *fullname;
4023
4024 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4025 zfs_unmount_snap(fullname);
4026 strfree(fullname);
4027
4028 return (0);
4029 }
4030
4031 /*
4032 * inputs:
4033 * zc_name old name of dataset
4034 * zc_value new name of dataset
4035 * zc_cookie recursive flag (only valid for snapshots)
4036 *
4037 * outputs: none
4038 */
4039 static int
4040 zfs_ioc_rename(zfs_cmd_t *zc)
4041 {
4042 objset_t *os;
4043 dmu_objset_type_t ost;
4044 boolean_t recursive = zc->zc_cookie & 1;
4045 char *at;
4046 int err;
4047
4048 /* "zfs rename" from and to ...%recv datasets should both fail */
4049 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4050 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4051 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4052 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4053 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4054 return (SET_ERROR(EINVAL));
4055
4056 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4057 if (err != 0)
4058 return (err);
4059 ost = dmu_objset_type(os);
4060 dmu_objset_rele(os, FTAG);
4061
4062 at = strchr(zc->zc_name, '@');
4063 if (at != NULL) {
4064 /* snaps must be in same fs */
4065 int error;
4066
4067 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4068 return (SET_ERROR(EXDEV));
4069 *at = '\0';
4070 if (ost == DMU_OST_ZFS) {
4071 error = dmu_objset_find(zc->zc_name,
4072 recursive_unmount, at + 1,
4073 recursive ? DS_FIND_CHILDREN : 0);
4074 if (error != 0) {
4075 *at = '@';
4076 return (error);
4077 }
4078 }
4079 error = dsl_dataset_rename_snapshot(zc->zc_name,
4080 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4081 *at = '@';
4082
4083 return (error);
4084 } else {
4085 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4086 }
4087 }
4088
4089 static int
4090 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4091 {
4092 const char *propname = nvpair_name(pair);
4093 boolean_t issnap = (strchr(dsname, '@') != NULL);
4094 zfs_prop_t prop = zfs_name_to_prop(propname);
4095 uint64_t intval;
4096 int err;
4097
4098 if (prop == ZPROP_INVAL) {
4099 if (zfs_prop_user(propname)) {
4100 if ((err = zfs_secpolicy_write_perms(dsname,
4101 ZFS_DELEG_PERM_USERPROP, cr)))
4102 return (err);
4103 return (0);
4104 }
4105
4106 if (!issnap && zfs_prop_userquota(propname)) {
4107 const char *perm = NULL;
4108 const char *uq_prefix =
4109 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4110 const char *gq_prefix =
4111 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4112 const char *uiq_prefix =
4113 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4114 const char *giq_prefix =
4115 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4116 const char *pq_prefix =
4117 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4118 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4119 ZFS_PROP_PROJECTOBJQUOTA];
4120
4121 if (strncmp(propname, uq_prefix,
4122 strlen(uq_prefix)) == 0) {
4123 perm = ZFS_DELEG_PERM_USERQUOTA;
4124 } else if (strncmp(propname, uiq_prefix,
4125 strlen(uiq_prefix)) == 0) {
4126 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4127 } else if (strncmp(propname, gq_prefix,
4128 strlen(gq_prefix)) == 0) {
4129 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4130 } else if (strncmp(propname, giq_prefix,
4131 strlen(giq_prefix)) == 0) {
4132 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4133 } else if (strncmp(propname, pq_prefix,
4134 strlen(pq_prefix)) == 0) {
4135 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4136 } else if (strncmp(propname, piq_prefix,
4137 strlen(piq_prefix)) == 0) {
4138 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4139 } else {
4140 /* {USER|GROUP|PROJECT}USED are read-only */
4141 return (SET_ERROR(EINVAL));
4142 }
4143
4144 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4145 return (err);
4146 return (0);
4147 }
4148
4149 return (SET_ERROR(EINVAL));
4150 }
4151
4152 if (issnap)
4153 return (SET_ERROR(EINVAL));
4154
4155 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4156 /*
4157 * dsl_prop_get_all_impl() returns properties in this
4158 * format.
4159 */
4160 nvlist_t *attrs;
4161 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4162 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4163 &pair) == 0);
4164 }
4165
4166 /*
4167 * Check that this value is valid for this pool version
4168 */
4169 switch (prop) {
4170 case ZFS_PROP_COMPRESSION:
4171 /*
4172 * If the user specified gzip compression, make sure
4173 * the SPA supports it. We ignore any errors here since
4174 * we'll catch them later.
4175 */
4176 if (nvpair_value_uint64(pair, &intval) == 0) {
4177 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4178 intval <= ZIO_COMPRESS_GZIP_9 &&
4179 zfs_earlier_version(dsname,
4180 SPA_VERSION_GZIP_COMPRESSION)) {
4181 return (SET_ERROR(ENOTSUP));
4182 }
4183
4184 if (intval == ZIO_COMPRESS_ZLE &&
4185 zfs_earlier_version(dsname,
4186 SPA_VERSION_ZLE_COMPRESSION))
4187 return (SET_ERROR(ENOTSUP));
4188
4189 if (intval == ZIO_COMPRESS_LZ4) {
4190 spa_t *spa;
4191
4192 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4193 return (err);
4194
4195 if (!spa_feature_is_enabled(spa,
4196 SPA_FEATURE_LZ4_COMPRESS)) {
4197 spa_close(spa, FTAG);
4198 return (SET_ERROR(ENOTSUP));
4199 }
4200 spa_close(spa, FTAG);
4201 }
4202
4203 /*
4204 * If this is a bootable dataset then
4205 * verify that the compression algorithm
4206 * is supported for booting. We must return
4207 * something other than ENOTSUP since it
4208 * implies a downrev pool version.
4209 */
4210 if (zfs_is_bootfs(dsname) &&
4211 !BOOTFS_COMPRESS_VALID(intval)) {
4212 return (SET_ERROR(ERANGE));
4213 }
4214 }
4215 break;
4216
4217 case ZFS_PROP_COPIES:
4218 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4219 return (SET_ERROR(ENOTSUP));
4220 break;
4221
4222 case ZFS_PROP_VOLBLOCKSIZE:
4223 case ZFS_PROP_RECORDSIZE:
4224 /* Record sizes above 128k need the feature to be enabled */
4225 if (nvpair_value_uint64(pair, &intval) == 0 &&
4226 intval > SPA_OLD_MAXBLOCKSIZE) {
4227 spa_t *spa;
4228
4229 /*
4230 * We don't allow setting the property above 1MB,
4231 * unless the tunable has been changed.
4232 */
4233 if (intval > zfs_max_recordsize ||
4234 intval > SPA_MAXBLOCKSIZE)
4235 return (SET_ERROR(ERANGE));
4236
4237 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4238 return (err);
4239
4240 if (!spa_feature_is_enabled(spa,
4241 SPA_FEATURE_LARGE_BLOCKS)) {
4242 spa_close(spa, FTAG);
4243 return (SET_ERROR(ENOTSUP));
4244 }
4245 spa_close(spa, FTAG);
4246 }
4247 break;
4248
4249 case ZFS_PROP_DNODESIZE:
4250 /* Dnode sizes above 512 need the feature to be enabled */
4251 if (nvpair_value_uint64(pair, &intval) == 0 &&
4252 intval != ZFS_DNSIZE_LEGACY) {
4253 spa_t *spa;
4254
4255 /*
4256 * If this is a bootable dataset then
4257 * we don't allow large (>512B) dnodes,
4258 * because GRUB doesn't support them.
4259 */
4260 if (zfs_is_bootfs(dsname) &&
4261 intval != ZFS_DNSIZE_LEGACY) {
4262 return (SET_ERROR(EDOM));
4263 }
4264
4265 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4266 return (err);
4267
4268 if (!spa_feature_is_enabled(spa,
4269 SPA_FEATURE_LARGE_DNODE)) {
4270 spa_close(spa, FTAG);
4271 return (SET_ERROR(ENOTSUP));
4272 }
4273 spa_close(spa, FTAG);
4274 }
4275 break;
4276
4277 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4278 /*
4279 * This property could require the allocation classes
4280 * feature to be active for setting, however we allow
4281 * it so that tests of settable properties succeed.
4282 * The CLI will issue a warning in this case.
4283 */
4284 break;
4285
4286 case ZFS_PROP_SHARESMB:
4287 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4288 return (SET_ERROR(ENOTSUP));
4289 break;
4290
4291 case ZFS_PROP_ACLINHERIT:
4292 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4293 nvpair_value_uint64(pair, &intval) == 0) {
4294 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4295 zfs_earlier_version(dsname,
4296 SPA_VERSION_PASSTHROUGH_X))
4297 return (SET_ERROR(ENOTSUP));
4298 }
4299 break;
4300 case ZFS_PROP_CHECKSUM:
4301 case ZFS_PROP_DEDUP:
4302 {
4303 spa_feature_t feature;
4304 spa_t *spa;
4305 int err;
4306
4307 /* dedup feature version checks */
4308 if (prop == ZFS_PROP_DEDUP &&
4309 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4310 return (SET_ERROR(ENOTSUP));
4311
4312 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4313 nvpair_value_uint64(pair, &intval) == 0) {
4314 /* check prop value is enabled in features */
4315 feature = zio_checksum_to_feature(
4316 intval & ZIO_CHECKSUM_MASK);
4317 if (feature == SPA_FEATURE_NONE)
4318 break;
4319
4320 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4321 return (err);
4322
4323 if (!spa_feature_is_enabled(spa, feature)) {
4324 spa_close(spa, FTAG);
4325 return (SET_ERROR(ENOTSUP));
4326 }
4327 spa_close(spa, FTAG);
4328 }
4329 break;
4330 }
4331
4332 default:
4333 break;
4334 }
4335
4336 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4337 }
4338
4339 /*
4340 * Removes properties from the given props list that fail permission checks
4341 * needed to clear them and to restore them in case of a receive error. For each
4342 * property, make sure we have both set and inherit permissions.
4343 *
4344 * Returns the first error encountered if any permission checks fail. If the
4345 * caller provides a non-NULL errlist, it also gives the complete list of names
4346 * of all the properties that failed a permission check along with the
4347 * corresponding error numbers. The caller is responsible for freeing the
4348 * returned errlist.
4349 *
4350 * If every property checks out successfully, zero is returned and the list
4351 * pointed at by errlist is NULL.
4352 */
4353 static int
4354 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4355 {
4356 zfs_cmd_t *zc;
4357 nvpair_t *pair, *next_pair;
4358 nvlist_t *errors;
4359 int err, rv = 0;
4360
4361 if (props == NULL)
4362 return (0);
4363
4364 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4365
4366 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4367 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4368 pair = nvlist_next_nvpair(props, NULL);
4369 while (pair != NULL) {
4370 next_pair = nvlist_next_nvpair(props, pair);
4371
4372 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4373 sizeof (zc->zc_value));
4374 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4375 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4376 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4377 VERIFY(nvlist_add_int32(errors,
4378 zc->zc_value, err) == 0);
4379 }
4380 pair = next_pair;
4381 }
4382 kmem_free(zc, sizeof (zfs_cmd_t));
4383
4384 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4385 nvlist_free(errors);
4386 errors = NULL;
4387 } else {
4388 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4389 }
4390
4391 if (errlist == NULL)
4392 nvlist_free(errors);
4393 else
4394 *errlist = errors;
4395
4396 return (rv);
4397 }
4398
4399 static boolean_t
4400 propval_equals(nvpair_t *p1, nvpair_t *p2)
4401 {
4402 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4403 /* dsl_prop_get_all_impl() format */
4404 nvlist_t *attrs;
4405 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4406 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4407 &p1) == 0);
4408 }
4409
4410 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4411 nvlist_t *attrs;
4412 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4413 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4414 &p2) == 0);
4415 }
4416
4417 if (nvpair_type(p1) != nvpair_type(p2))
4418 return (B_FALSE);
4419
4420 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4421 char *valstr1, *valstr2;
4422
4423 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4424 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4425 return (strcmp(valstr1, valstr2) == 0);
4426 } else {
4427 uint64_t intval1, intval2;
4428
4429 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4430 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4431 return (intval1 == intval2);
4432 }
4433 }
4434
4435 /*
4436 * Remove properties from props if they are not going to change (as determined
4437 * by comparison with origprops). Remove them from origprops as well, since we
4438 * do not need to clear or restore properties that won't change.
4439 */
4440 static void
4441 props_reduce(nvlist_t *props, nvlist_t *origprops)
4442 {
4443 nvpair_t *pair, *next_pair;
4444
4445 if (origprops == NULL)
4446 return; /* all props need to be received */
4447
4448 pair = nvlist_next_nvpair(props, NULL);
4449 while (pair != NULL) {
4450 const char *propname = nvpair_name(pair);
4451 nvpair_t *match;
4452
4453 next_pair = nvlist_next_nvpair(props, pair);
4454
4455 if ((nvlist_lookup_nvpair(origprops, propname,
4456 &match) != 0) || !propval_equals(pair, match))
4457 goto next; /* need to set received value */
4458
4459 /* don't clear the existing received value */
4460 (void) nvlist_remove_nvpair(origprops, match);
4461 /* don't bother receiving the property */
4462 (void) nvlist_remove_nvpair(props, pair);
4463 next:
4464 pair = next_pair;
4465 }
4466 }
4467
4468 /*
4469 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4470 * For example, refquota cannot be set until after the receipt of a dataset,
4471 * because in replication streams, an older/earlier snapshot may exceed the
4472 * refquota. We want to receive the older/earlier snapshot, but setting
4473 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4474 * the older/earlier snapshot from being received (with EDQUOT).
4475 *
4476 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4477 *
4478 * libzfs will need to be judicious handling errors encountered by props
4479 * extracted by this function.
4480 */
4481 static nvlist_t *
4482 extract_delay_props(nvlist_t *props)
4483 {
4484 nvlist_t *delayprops;
4485 nvpair_t *nvp, *tmp;
4486 static const zfs_prop_t delayable[] = {
4487 ZFS_PROP_REFQUOTA,
4488 ZFS_PROP_KEYLOCATION,
4489 0
4490 };
4491 int i;
4492
4493 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4494
4495 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4496 nvp = nvlist_next_nvpair(props, nvp)) {
4497 /*
4498 * strcmp() is safe because zfs_prop_to_name() always returns
4499 * a bounded string.
4500 */
4501 for (i = 0; delayable[i] != 0; i++) {
4502 if (strcmp(zfs_prop_to_name(delayable[i]),
4503 nvpair_name(nvp)) == 0) {
4504 break;
4505 }
4506 }
4507 if (delayable[i] != 0) {
4508 tmp = nvlist_prev_nvpair(props, nvp);
4509 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4510 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4511 nvp = tmp;
4512 }
4513 }
4514
4515 if (nvlist_empty(delayprops)) {
4516 nvlist_free(delayprops);
4517 delayprops = NULL;
4518 }
4519 return (delayprops);
4520 }
4521
4522 #ifdef DEBUG
4523 static boolean_t zfs_ioc_recv_inject_err;
4524 #endif
4525
4526 /*
4527 * nvlist 'errors' is always allocated. It will contain descriptions of
4528 * encountered errors, if any. It's the callers responsibility to free.
4529 */
4530 static int
4531 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4532 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4533 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4534 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4535 uint64_t *action_handle, nvlist_t **errors)
4536 {
4537 dmu_recv_cookie_t drc;
4538 int error = 0;
4539 int props_error = 0;
4540 offset_t off;
4541 nvlist_t *local_delayprops = NULL;
4542 nvlist_t *recv_delayprops = NULL;
4543 nvlist_t *origprops = NULL; /* existing properties */
4544 nvlist_t *origrecvd = NULL; /* existing received properties */
4545 boolean_t first_recvd_props = B_FALSE;
4546 file_t *input_fp;
4547
4548 *read_bytes = 0;
4549 *errflags = 0;
4550 *errors = fnvlist_alloc();
4551
4552 input_fp = getf(input_fd);
4553 if (input_fp == NULL)
4554 return (SET_ERROR(EBADF));
4555
4556 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4557 resumable, localprops, hidden_args, origin, &drc);
4558 if (error != 0)
4559 goto out;
4560
4561 /*
4562 * Set properties before we receive the stream so that they are applied
4563 * to the new data. Note that we must call dmu_recv_stream() if
4564 * dmu_recv_begin() succeeds.
4565 */
4566 if (recvprops != NULL && !drc.drc_newfs) {
4567 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4568 SPA_VERSION_RECVD_PROPS &&
4569 !dsl_prop_get_hasrecvd(tofs))
4570 first_recvd_props = B_TRUE;
4571
4572 /*
4573 * If new received properties are supplied, they are to
4574 * completely replace the existing received properties,
4575 * so stash away the existing ones.
4576 */
4577 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4578 nvlist_t *errlist = NULL;
4579 /*
4580 * Don't bother writing a property if its value won't
4581 * change (and avoid the unnecessary security checks).
4582 *
4583 * The first receive after SPA_VERSION_RECVD_PROPS is a
4584 * special case where we blow away all local properties
4585 * regardless.
4586 */
4587 if (!first_recvd_props)
4588 props_reduce(recvprops, origrecvd);
4589 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4590 (void) nvlist_merge(*errors, errlist, 0);
4591 nvlist_free(errlist);
4592
4593 if (clear_received_props(tofs, origrecvd,
4594 first_recvd_props ? NULL : recvprops) != 0)
4595 *errflags |= ZPROP_ERR_NOCLEAR;
4596 } else {
4597 *errflags |= ZPROP_ERR_NOCLEAR;
4598 }
4599 }
4600
4601 /*
4602 * Stash away existing properties so we can restore them on error unless
4603 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4604 * case "origrecvd" will take care of that.
4605 */
4606 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4607 objset_t *os;
4608 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4609 if (dsl_prop_get_all(os, &origprops) != 0) {
4610 *errflags |= ZPROP_ERR_NOCLEAR;
4611 }
4612 dmu_objset_rele(os, FTAG);
4613 } else {
4614 *errflags |= ZPROP_ERR_NOCLEAR;
4615 }
4616 }
4617
4618 if (recvprops != NULL) {
4619 props_error = dsl_prop_set_hasrecvd(tofs);
4620
4621 if (props_error == 0) {
4622 recv_delayprops = extract_delay_props(recvprops);
4623 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4624 recvprops, *errors);
4625 }
4626 }
4627
4628 if (localprops != NULL) {
4629 nvlist_t *oprops = fnvlist_alloc();
4630 nvlist_t *xprops = fnvlist_alloc();
4631 nvpair_t *nvp = NULL;
4632
4633 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4634 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4635 /* -x property */
4636 const char *name = nvpair_name(nvp);
4637 zfs_prop_t prop = zfs_name_to_prop(name);
4638 if (prop != ZPROP_INVAL) {
4639 if (!zfs_prop_inheritable(prop))
4640 continue;
4641 } else if (!zfs_prop_user(name))
4642 continue;
4643 fnvlist_add_boolean(xprops, name);
4644 } else {
4645 /* -o property=value */
4646 fnvlist_add_nvpair(oprops, nvp);
4647 }
4648 }
4649
4650 local_delayprops = extract_delay_props(oprops);
4651 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4652 oprops, *errors);
4653 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4654 xprops, *errors);
4655
4656 nvlist_free(oprops);
4657 nvlist_free(xprops);
4658 }
4659
4660 off = input_fp->f_offset;
4661 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4662 action_handle);
4663
4664 if (error == 0) {
4665 zfsvfs_t *zfsvfs = NULL;
4666 zvol_state_t *zv = NULL;
4667
4668 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4669 /* online recv */
4670 dsl_dataset_t *ds;
4671 int end_err;
4672
4673 ds = dmu_objset_ds(zfsvfs->z_os);
4674 error = zfs_suspend_fs(zfsvfs);
4675 /*
4676 * If the suspend fails, then the recv_end will
4677 * likely also fail, and clean up after itself.
4678 */
4679 end_err = dmu_recv_end(&drc, zfsvfs);
4680 if (error == 0)
4681 error = zfs_resume_fs(zfsvfs, ds);
4682 error = error ? error : end_err;
4683 deactivate_super(zfsvfs->z_sb);
4684 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4685 error = dmu_recv_end(&drc, zvol_tag(zv));
4686 zvol_resume(zv);
4687 } else {
4688 error = dmu_recv_end(&drc, NULL);
4689 }
4690
4691 /* Set delayed properties now, after we're done receiving. */
4692 if (recv_delayprops != NULL && error == 0) {
4693 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4694 recv_delayprops, *errors);
4695 }
4696 if (local_delayprops != NULL && error == 0) {
4697 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4698 local_delayprops, *errors);
4699 }
4700 }
4701
4702 /*
4703 * Merge delayed props back in with initial props, in case
4704 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4705 * we have to make sure clear_received_props() includes
4706 * the delayed properties).
4707 *
4708 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4709 * using ASSERT() will be just like a VERIFY.
4710 */
4711 if (recv_delayprops != NULL) {
4712 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4713 nvlist_free(recv_delayprops);
4714 }
4715 if (local_delayprops != NULL) {
4716 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4717 nvlist_free(local_delayprops);
4718 }
4719
4720 *read_bytes = off - input_fp->f_offset;
4721 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4722 input_fp->f_offset = off;
4723
4724 #ifdef DEBUG
4725 if (zfs_ioc_recv_inject_err) {
4726 zfs_ioc_recv_inject_err = B_FALSE;
4727 error = 1;
4728 }
4729 #endif
4730
4731 /*
4732 * On error, restore the original props.
4733 */
4734 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4735 if (clear_received_props(tofs, recvprops, NULL) != 0) {
4736 /*
4737 * We failed to clear the received properties.
4738 * Since we may have left a $recvd value on the
4739 * system, we can't clear the $hasrecvd flag.
4740 */
4741 *errflags |= ZPROP_ERR_NORESTORE;
4742 } else if (first_recvd_props) {
4743 dsl_prop_unset_hasrecvd(tofs);
4744 }
4745
4746 if (origrecvd == NULL && !drc.drc_newfs) {
4747 /* We failed to stash the original properties. */
4748 *errflags |= ZPROP_ERR_NORESTORE;
4749 }
4750
4751 /*
4752 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4753 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4754 * explicitly if we're restoring local properties cleared in the
4755 * first new-style receive.
4756 */
4757 if (origrecvd != NULL &&
4758 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4759 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4760 origrecvd, NULL) != 0) {
4761 /*
4762 * We stashed the original properties but failed to
4763 * restore them.
4764 */
4765 *errflags |= ZPROP_ERR_NORESTORE;
4766 }
4767 }
4768 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4769 !first_recvd_props) {
4770 nvlist_t *setprops;
4771 nvlist_t *inheritprops;
4772 nvpair_t *nvp;
4773
4774 if (origprops == NULL) {
4775 /* We failed to stash the original properties. */
4776 *errflags |= ZPROP_ERR_NORESTORE;
4777 goto out;
4778 }
4779
4780 /* Restore original props */
4781 setprops = fnvlist_alloc();
4782 inheritprops = fnvlist_alloc();
4783 nvp = NULL;
4784 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4785 const char *name = nvpair_name(nvp);
4786 const char *source;
4787 nvlist_t *attrs;
4788
4789 if (!nvlist_exists(origprops, name)) {
4790 /*
4791 * Property was not present or was explicitly
4792 * inherited before the receive, restore this.
4793 */
4794 fnvlist_add_boolean(inheritprops, name);
4795 continue;
4796 }
4797 attrs = fnvlist_lookup_nvlist(origprops, name);
4798 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4799
4800 /* Skip received properties */
4801 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4802 continue;
4803
4804 if (strcmp(source, tofs) == 0) {
4805 /* Property was locally set */
4806 fnvlist_add_nvlist(setprops, name, attrs);
4807 } else {
4808 /* Property was implicitly inherited */
4809 fnvlist_add_boolean(inheritprops, name);
4810 }
4811 }
4812
4813 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4814 NULL) != 0)
4815 *errflags |= ZPROP_ERR_NORESTORE;
4816 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4817 NULL) != 0)
4818 *errflags |= ZPROP_ERR_NORESTORE;
4819
4820 nvlist_free(setprops);
4821 nvlist_free(inheritprops);
4822 }
4823 out:
4824 releasef(input_fd);
4825 nvlist_free(origrecvd);
4826 nvlist_free(origprops);
4827
4828 if (error == 0)
4829 error = props_error;
4830
4831 return (error);
4832 }
4833
4834 /*
4835 * inputs:
4836 * zc_name name of containing filesystem (unused)
4837 * zc_nvlist_src{_size} nvlist of properties to apply
4838 * zc_nvlist_conf{_size} nvlist of properties to exclude
4839 * (DATA_TYPE_BOOLEAN) and override (everything else)
4840 * zc_value name of snapshot to create
4841 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4842 * zc_cookie file descriptor to recv from
4843 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4844 * zc_guid force flag
4845 * zc_cleanup_fd cleanup-on-exit file descriptor
4846 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4847 *
4848 * outputs:
4849 * zc_cookie number of bytes read
4850 * zc_obj zprop_errflags_t
4851 * zc_action_handle handle for this guid/ds mapping
4852 * zc_nvlist_dst{_size} error for each unapplied received property
4853 */
4854 static int
4855 zfs_ioc_recv(zfs_cmd_t *zc)
4856 {
4857 dmu_replay_record_t begin_record;
4858 nvlist_t *errors = NULL;
4859 nvlist_t *recvdprops = NULL;
4860 nvlist_t *localprops = NULL;
4861 char *origin = NULL;
4862 char *tosnap;
4863 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4864 int error = 0;
4865
4866 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4867 strchr(zc->zc_value, '@') == NULL ||
4868 strchr(zc->zc_value, '%'))
4869 return (SET_ERROR(EINVAL));
4870
4871 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
4872 tosnap = strchr(tofs, '@');
4873 *tosnap++ = '\0';
4874
4875 if (zc->zc_nvlist_src != 0 &&
4876 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4877 zc->zc_iflags, &recvdprops)) != 0)
4878 return (error);
4879
4880 if (zc->zc_nvlist_conf != 0 &&
4881 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
4882 zc->zc_iflags, &localprops)) != 0)
4883 return (error);
4884
4885 if (zc->zc_string[0])
4886 origin = zc->zc_string;
4887
4888 begin_record.drr_type = DRR_BEGIN;
4889 begin_record.drr_payloadlen = 0;
4890 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4891
4892 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
4893 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
4894 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
4895 &zc->zc_action_handle, &errors);
4896 nvlist_free(recvdprops);
4897 nvlist_free(localprops);
4898
4899 /*
4900 * Now that all props, initial and delayed, are set, report the prop
4901 * errors to the caller.
4902 */
4903 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4904 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4905 put_nvlist(zc, errors) != 0)) {
4906 /*
4907 * Caller made zc->zc_nvlist_dst less than the minimum expected
4908 * size or supplied an invalid address.
4909 */
4910 error = SET_ERROR(EINVAL);
4911 }
4912
4913 nvlist_free(errors);
4914
4915 return (error);
4916 }
4917
4918 /*
4919 * innvl: {
4920 * "snapname" -> full name of the snapshot to create
4921 * (optional) "props" -> received properties to set (nvlist)
4922 * (optional) "localprops" -> override and exclude properties (nvlist)
4923 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4924 * "begin_record" -> non-byteswapped dmu_replay_record_t
4925 * "input_fd" -> file descriptor to read stream from (int32)
4926 * (optional) "force" -> force flag (value ignored)
4927 * (optional) "resumable" -> resumable flag (value ignored)
4928 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4929 * (optional) "action_handle" -> handle for this guid/ds mapping
4930 * (optional) "hidden_args" -> { "wkeydata" -> value }
4931 * }
4932 *
4933 * outnvl: {
4934 * "read_bytes" -> number of bytes read
4935 * "error_flags" -> zprop_errflags_t
4936 * "action_handle" -> handle for this guid/ds mapping
4937 * "errors" -> error for each unapplied received property (nvlist)
4938 * }
4939 */
4940 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
4941 {"snapname", DATA_TYPE_STRING, 0},
4942 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4943 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4944 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
4945 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
4946 {"input_fd", DATA_TYPE_INT32, 0},
4947 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
4948 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
4949 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
4950 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
4951 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
4952 };
4953
4954 static int
4955 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4956 {
4957 dmu_replay_record_t *begin_record;
4958 uint_t begin_record_size;
4959 nvlist_t *errors = NULL;
4960 nvlist_t *recvprops = NULL;
4961 nvlist_t *localprops = NULL;
4962 nvlist_t *hidden_args = NULL;
4963 char *snapname;
4964 char *origin = NULL;
4965 char *tosnap;
4966 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4967 boolean_t force;
4968 boolean_t resumable;
4969 uint64_t action_handle = 0;
4970 uint64_t read_bytes = 0;
4971 uint64_t errflags = 0;
4972 int input_fd = -1;
4973 int cleanup_fd = -1;
4974 int error;
4975
4976 snapname = fnvlist_lookup_string(innvl, "snapname");
4977
4978 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
4979 strchr(snapname, '@') == NULL ||
4980 strchr(snapname, '%'))
4981 return (SET_ERROR(EINVAL));
4982
4983 (void) strcpy(tofs, snapname);
4984 tosnap = strchr(tofs, '@');
4985 *tosnap++ = '\0';
4986
4987 error = nvlist_lookup_string(innvl, "origin", &origin);
4988 if (error && error != ENOENT)
4989 return (error);
4990
4991 error = nvlist_lookup_byte_array(innvl, "begin_record",
4992 (uchar_t **)&begin_record, &begin_record_size);
4993 if (error != 0 || begin_record_size != sizeof (*begin_record))
4994 return (SET_ERROR(EINVAL));
4995
4996 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
4997
4998 force = nvlist_exists(innvl, "force");
4999 resumable = nvlist_exists(innvl, "resumable");
5000
5001 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
5002 if (error && error != ENOENT)
5003 return (error);
5004
5005 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
5006 if (error && error != ENOENT)
5007 return (error);
5008
5009 /* we still use "props" here for backwards compatibility */
5010 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5011 if (error && error != ENOENT)
5012 return (error);
5013
5014 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5015 if (error && error != ENOENT)
5016 return (error);
5017
5018 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5019 if (error && error != ENOENT)
5020 return (error);
5021
5022 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5023 hidden_args, force, resumable, input_fd, begin_record, cleanup_fd,
5024 &read_bytes, &errflags, &action_handle, &errors);
5025
5026 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5027 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5028 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
5029 fnvlist_add_nvlist(outnvl, "errors", errors);
5030
5031 nvlist_free(errors);
5032 nvlist_free(recvprops);
5033 nvlist_free(localprops);
5034
5035 return (error);
5036 }
5037
5038 /*
5039 * inputs:
5040 * zc_name name of snapshot to send
5041 * zc_cookie file descriptor to send stream to
5042 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5043 * zc_sendobj objsetid of snapshot to send
5044 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5045 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5046 * output size in zc_objset_type.
5047 * zc_flags lzc_send_flags
5048 *
5049 * outputs:
5050 * zc_objset_type estimated size, if zc_guid is set
5051 *
5052 * NOTE: This is no longer the preferred interface, any new functionality
5053 * should be added to zfs_ioc_send_new() instead.
5054 */
5055 static int
5056 zfs_ioc_send(zfs_cmd_t *zc)
5057 {
5058 int error;
5059 offset_t off;
5060 boolean_t estimate = (zc->zc_guid != 0);
5061 boolean_t embedok = (zc->zc_flags & 0x1);
5062 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5063 boolean_t compressok = (zc->zc_flags & 0x4);
5064 boolean_t rawok = (zc->zc_flags & 0x8);
5065
5066 if (zc->zc_obj != 0) {
5067 dsl_pool_t *dp;
5068 dsl_dataset_t *tosnap;
5069
5070 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5071 if (error != 0)
5072 return (error);
5073
5074 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5075 if (error != 0) {
5076 dsl_pool_rele(dp, FTAG);
5077 return (error);
5078 }
5079
5080 if (dsl_dir_is_clone(tosnap->ds_dir))
5081 zc->zc_fromobj =
5082 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5083 dsl_dataset_rele(tosnap, FTAG);
5084 dsl_pool_rele(dp, FTAG);
5085 }
5086
5087 if (estimate) {
5088 dsl_pool_t *dp;
5089 dsl_dataset_t *tosnap;
5090 dsl_dataset_t *fromsnap = NULL;
5091
5092 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5093 if (error != 0)
5094 return (error);
5095
5096 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5097 FTAG, &tosnap);
5098 if (error != 0) {
5099 dsl_pool_rele(dp, FTAG);
5100 return (error);
5101 }
5102
5103 if (zc->zc_fromobj != 0) {
5104 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5105 FTAG, &fromsnap);
5106 if (error != 0) {
5107 dsl_dataset_rele(tosnap, FTAG);
5108 dsl_pool_rele(dp, FTAG);
5109 return (error);
5110 }
5111 }
5112
5113 error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
5114 &zc->zc_objset_type);
5115
5116 if (fromsnap != NULL)
5117 dsl_dataset_rele(fromsnap, FTAG);
5118 dsl_dataset_rele(tosnap, FTAG);
5119 dsl_pool_rele(dp, FTAG);
5120 } else {
5121 file_t *fp = getf(zc->zc_cookie);
5122 if (fp == NULL)
5123 return (SET_ERROR(EBADF));
5124
5125 off = fp->f_offset;
5126 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5127 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
5128 zc->zc_cookie, fp->f_vnode, &off);
5129
5130 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5131 fp->f_offset = off;
5132 releasef(zc->zc_cookie);
5133 }
5134 return (error);
5135 }
5136
5137 /*
5138 * inputs:
5139 * zc_name name of snapshot on which to report progress
5140 * zc_cookie file descriptor of send stream
5141 *
5142 * outputs:
5143 * zc_cookie number of bytes written in send stream thus far
5144 */
5145 static int
5146 zfs_ioc_send_progress(zfs_cmd_t *zc)
5147 {
5148 dsl_pool_t *dp;
5149 dsl_dataset_t *ds;
5150 dmu_sendarg_t *dsp = NULL;
5151 int error;
5152
5153 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5154 if (error != 0)
5155 return (error);
5156
5157 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5158 if (error != 0) {
5159 dsl_pool_rele(dp, FTAG);
5160 return (error);
5161 }
5162
5163 mutex_enter(&ds->ds_sendstream_lock);
5164
5165 /*
5166 * Iterate over all the send streams currently active on this dataset.
5167 * If there's one which matches the specified file descriptor _and_ the
5168 * stream was started by the current process, return the progress of
5169 * that stream.
5170 */
5171
5172 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5173 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5174 if (dsp->dsa_outfd == zc->zc_cookie &&
5175 dsp->dsa_proc->group_leader == curproc->group_leader)
5176 break;
5177 }
5178
5179 if (dsp != NULL)
5180 zc->zc_cookie = *(dsp->dsa_off);
5181 else
5182 error = SET_ERROR(ENOENT);
5183
5184 mutex_exit(&ds->ds_sendstream_lock);
5185 dsl_dataset_rele(ds, FTAG);
5186 dsl_pool_rele(dp, FTAG);
5187 return (error);
5188 }
5189
5190 static int
5191 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5192 {
5193 int id, error;
5194
5195 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5196 &zc->zc_inject_record);
5197
5198 if (error == 0)
5199 zc->zc_guid = (uint64_t)id;
5200
5201 return (error);
5202 }
5203
5204 static int
5205 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5206 {
5207 return (zio_clear_fault((int)zc->zc_guid));
5208 }
5209
5210 static int
5211 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5212 {
5213 int id = (int)zc->zc_guid;
5214 int error;
5215
5216 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5217 &zc->zc_inject_record);
5218
5219 zc->zc_guid = id;
5220
5221 return (error);
5222 }
5223
5224 static int
5225 zfs_ioc_error_log(zfs_cmd_t *zc)
5226 {
5227 spa_t *spa;
5228 int error;
5229 size_t count = (size_t)zc->zc_nvlist_dst_size;
5230
5231 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5232 return (error);
5233
5234 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5235 &count);
5236 if (error == 0)
5237 zc->zc_nvlist_dst_size = count;
5238 else
5239 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5240
5241 spa_close(spa, FTAG);
5242
5243 return (error);
5244 }
5245
5246 static int
5247 zfs_ioc_clear(zfs_cmd_t *zc)
5248 {
5249 spa_t *spa;
5250 vdev_t *vd;
5251 int error;
5252
5253 /*
5254 * On zpool clear we also fix up missing slogs
5255 */
5256 mutex_enter(&spa_namespace_lock);
5257 spa = spa_lookup(zc->zc_name);
5258 if (spa == NULL) {
5259 mutex_exit(&spa_namespace_lock);
5260 return (SET_ERROR(EIO));
5261 }
5262 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5263 /* we need to let spa_open/spa_load clear the chains */
5264 spa_set_log_state(spa, SPA_LOG_CLEAR);
5265 }
5266 spa->spa_last_open_failed = 0;
5267 mutex_exit(&spa_namespace_lock);
5268
5269 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5270 error = spa_open(zc->zc_name, &spa, FTAG);
5271 } else {
5272 nvlist_t *policy;
5273 nvlist_t *config = NULL;
5274
5275 if (zc->zc_nvlist_src == 0)
5276 return (SET_ERROR(EINVAL));
5277
5278 if ((error = get_nvlist(zc->zc_nvlist_src,
5279 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5280 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5281 policy, &config);
5282 if (config != NULL) {
5283 int err;
5284
5285 if ((err = put_nvlist(zc, config)) != 0)
5286 error = err;
5287 nvlist_free(config);
5288 }
5289 nvlist_free(policy);
5290 }
5291 }
5292
5293 if (error != 0)
5294 return (error);
5295
5296 /*
5297 * If multihost is enabled, resuming I/O is unsafe as another
5298 * host may have imported the pool.
5299 */
5300 if (spa_multihost(spa) && spa_suspended(spa))
5301 return (SET_ERROR(EINVAL));
5302
5303 spa_vdev_state_enter(spa, SCL_NONE);
5304
5305 if (zc->zc_guid == 0) {
5306 vd = NULL;
5307 } else {
5308 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5309 if (vd == NULL) {
5310 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5311 spa_close(spa, FTAG);
5312 return (SET_ERROR(ENODEV));
5313 }
5314 }
5315
5316 vdev_clear(spa, vd);
5317
5318 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5319 NULL : spa->spa_root_vdev, 0);
5320
5321 /*
5322 * Resume any suspended I/Os.
5323 */
5324 if (zio_resume(spa) != 0)
5325 error = SET_ERROR(EIO);
5326
5327 spa_close(spa, FTAG);
5328
5329 return (error);
5330 }
5331
5332 /*
5333 * Reopen all the vdevs associated with the pool.
5334 *
5335 * innvl: {
5336 * "scrub_restart" -> when true and scrub is running, allow to restart
5337 * scrub as the side effect of the reopen (boolean).
5338 * }
5339 *
5340 * outnvl is unused
5341 */
5342 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5343 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, 0},
5344 };
5345
5346 /* ARGSUSED */
5347 static int
5348 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5349 {
5350 spa_t *spa;
5351 int error;
5352 boolean_t scrub_restart = B_TRUE;
5353
5354 if (innvl) {
5355 scrub_restart = fnvlist_lookup_boolean_value(innvl,
5356 "scrub_restart");
5357 }
5358
5359 error = spa_open(pool, &spa, FTAG);
5360 if (error != 0)
5361 return (error);
5362
5363 spa_vdev_state_enter(spa, SCL_NONE);
5364
5365 /*
5366 * If the scrub_restart flag is B_FALSE and a scrub is already
5367 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5368 * we don't restart the scrub as a side effect of the reopen.
5369 * Otherwise, let vdev_open() decided if a resilver is required.
5370 */
5371
5372 spa->spa_scrub_reopen = (!scrub_restart &&
5373 dsl_scan_scrubbing(spa->spa_dsl_pool));
5374 vdev_reopen(spa->spa_root_vdev);
5375 spa->spa_scrub_reopen = B_FALSE;
5376
5377 (void) spa_vdev_state_exit(spa, NULL, 0);
5378 spa_close(spa, FTAG);
5379 return (0);
5380 }
5381
5382 /*
5383 * inputs:
5384 * zc_name name of filesystem
5385 *
5386 * outputs:
5387 * zc_string name of conflicting snapshot, if there is one
5388 */
5389 static int
5390 zfs_ioc_promote(zfs_cmd_t *zc)
5391 {
5392 dsl_pool_t *dp;
5393 dsl_dataset_t *ds, *ods;
5394 char origin[ZFS_MAX_DATASET_NAME_LEN];
5395 char *cp;
5396 int error;
5397
5398 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5399 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5400 strchr(zc->zc_name, '%'))
5401 return (SET_ERROR(EINVAL));
5402
5403 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5404 if (error != 0)
5405 return (error);
5406
5407 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5408 if (error != 0) {
5409 dsl_pool_rele(dp, FTAG);
5410 return (error);
5411 }
5412
5413 if (!dsl_dir_is_clone(ds->ds_dir)) {
5414 dsl_dataset_rele(ds, FTAG);
5415 dsl_pool_rele(dp, FTAG);
5416 return (SET_ERROR(EINVAL));
5417 }
5418
5419 error = dsl_dataset_hold_obj(dp,
5420 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5421 if (error != 0) {
5422 dsl_dataset_rele(ds, FTAG);
5423 dsl_pool_rele(dp, FTAG);
5424 return (error);
5425 }
5426
5427 dsl_dataset_name(ods, origin);
5428 dsl_dataset_rele(ods, FTAG);
5429 dsl_dataset_rele(ds, FTAG);
5430 dsl_pool_rele(dp, FTAG);
5431
5432 /*
5433 * We don't need to unmount *all* the origin fs's snapshots, but
5434 * it's easier.
5435 */
5436 cp = strchr(origin, '@');
5437 if (cp)
5438 *cp = '\0';
5439 (void) dmu_objset_find(origin,
5440 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5441 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5442 }
5443
5444 /*
5445 * Retrieve a single {user|group|project}{used|quota}@... property.
5446 *
5447 * inputs:
5448 * zc_name name of filesystem
5449 * zc_objset_type zfs_userquota_prop_t
5450 * zc_value domain name (eg. "S-1-234-567-89")
5451 * zc_guid RID/UID/GID
5452 *
5453 * outputs:
5454 * zc_cookie property value
5455 */
5456 static int
5457 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5458 {
5459 zfsvfs_t *zfsvfs;
5460 int error;
5461
5462 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5463 return (SET_ERROR(EINVAL));
5464
5465 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5466 if (error != 0)
5467 return (error);
5468
5469 error = zfs_userspace_one(zfsvfs,
5470 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5471 zfsvfs_rele(zfsvfs, FTAG);
5472
5473 return (error);
5474 }
5475
5476 /*
5477 * inputs:
5478 * zc_name name of filesystem
5479 * zc_cookie zap cursor
5480 * zc_objset_type zfs_userquota_prop_t
5481 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5482 *
5483 * outputs:
5484 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5485 * zc_cookie zap cursor
5486 */
5487 static int
5488 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5489 {
5490 zfsvfs_t *zfsvfs;
5491 int bufsize = zc->zc_nvlist_dst_size;
5492
5493 if (bufsize <= 0)
5494 return (SET_ERROR(ENOMEM));
5495
5496 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5497 if (error != 0)
5498 return (error);
5499
5500 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5501
5502 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5503 buf, &zc->zc_nvlist_dst_size);
5504
5505 if (error == 0) {
5506 error = xcopyout(buf,
5507 (void *)(uintptr_t)zc->zc_nvlist_dst,
5508 zc->zc_nvlist_dst_size);
5509 }
5510 vmem_free(buf, bufsize);
5511 zfsvfs_rele(zfsvfs, FTAG);
5512
5513 return (error);
5514 }
5515
5516 /*
5517 * inputs:
5518 * zc_name name of filesystem
5519 *
5520 * outputs:
5521 * none
5522 */
5523 static int
5524 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5525 {
5526 objset_t *os;
5527 int error = 0;
5528 zfsvfs_t *zfsvfs;
5529
5530 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5531 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5532 /*
5533 * If userused is not enabled, it may be because the
5534 * objset needs to be closed & reopened (to grow the
5535 * objset_phys_t). Suspend/resume the fs will do that.
5536 */
5537 dsl_dataset_t *ds, *newds;
5538
5539 ds = dmu_objset_ds(zfsvfs->z_os);
5540 error = zfs_suspend_fs(zfsvfs);
5541 if (error == 0) {
5542 dmu_objset_refresh_ownership(ds, &newds,
5543 B_TRUE, zfsvfs);
5544 error = zfs_resume_fs(zfsvfs, newds);
5545 }
5546 }
5547 if (error == 0)
5548 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5549 deactivate_super(zfsvfs->z_sb);
5550 } else {
5551 /* XXX kind of reading contents without owning */
5552 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5553 if (error != 0)
5554 return (error);
5555
5556 error = dmu_objset_userspace_upgrade(os);
5557 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5558 }
5559
5560 return (error);
5561 }
5562
5563 /*
5564 * inputs:
5565 * zc_name name of filesystem
5566 *
5567 * outputs:
5568 * none
5569 */
5570 static int
5571 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5572 {
5573 objset_t *os;
5574 int error;
5575
5576 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5577 if (error != 0)
5578 return (error);
5579
5580 if (dmu_objset_userobjspace_upgradable(os) ||
5581 dmu_objset_projectquota_upgradable(os)) {
5582 mutex_enter(&os->os_upgrade_lock);
5583 if (os->os_upgrade_id == 0) {
5584 /* clear potential error code and retry */
5585 os->os_upgrade_status = 0;
5586 mutex_exit(&os->os_upgrade_lock);
5587
5588 dmu_objset_id_quota_upgrade(os);
5589 } else {
5590 mutex_exit(&os->os_upgrade_lock);
5591 }
5592
5593 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5594
5595 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5596 error = os->os_upgrade_status;
5597 } else {
5598 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5599 }
5600
5601 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
5602
5603 return (error);
5604 }
5605
5606 static int
5607 zfs_ioc_share(zfs_cmd_t *zc)
5608 {
5609 return (SET_ERROR(ENOSYS));
5610 }
5611
5612 ace_t full_access[] = {
5613 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5614 };
5615
5616 /*
5617 * inputs:
5618 * zc_name name of containing filesystem
5619 * zc_obj object # beyond which we want next in-use object #
5620 *
5621 * outputs:
5622 * zc_obj next in-use object #
5623 */
5624 static int
5625 zfs_ioc_next_obj(zfs_cmd_t *zc)
5626 {
5627 objset_t *os = NULL;
5628 int error;
5629
5630 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5631 if (error != 0)
5632 return (error);
5633
5634 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5635
5636 dmu_objset_rele(os, FTAG);
5637 return (error);
5638 }
5639
5640 /*
5641 * inputs:
5642 * zc_name name of filesystem
5643 * zc_value prefix name for snapshot
5644 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5645 *
5646 * outputs:
5647 * zc_value short name of new snapshot
5648 */
5649 static int
5650 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5651 {
5652 char *snap_name;
5653 char *hold_name;
5654 int error;
5655 minor_t minor;
5656
5657 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5658 if (error != 0)
5659 return (error);
5660
5661 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5662 (u_longlong_t)ddi_get_lbolt64());
5663 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5664
5665 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5666 hold_name);
5667 if (error == 0)
5668 (void) strlcpy(zc->zc_value, snap_name,
5669 sizeof (zc->zc_value));
5670 strfree(snap_name);
5671 strfree(hold_name);
5672 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5673 return (error);
5674 }
5675
5676 /*
5677 * inputs:
5678 * zc_name name of "to" snapshot
5679 * zc_value name of "from" snapshot
5680 * zc_cookie file descriptor to write diff data on
5681 *
5682 * outputs:
5683 * dmu_diff_record_t's to the file descriptor
5684 */
5685 static int
5686 zfs_ioc_diff(zfs_cmd_t *zc)
5687 {
5688 file_t *fp;
5689 offset_t off;
5690 int error;
5691
5692 fp = getf(zc->zc_cookie);
5693 if (fp == NULL)
5694 return (SET_ERROR(EBADF));
5695
5696 off = fp->f_offset;
5697
5698 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5699
5700 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5701 fp->f_offset = off;
5702 releasef(zc->zc_cookie);
5703
5704 return (error);
5705 }
5706
5707 static int
5708 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5709 {
5710 return (SET_ERROR(ENOTSUP));
5711 }
5712
5713 /*
5714 * innvl: {
5715 * "holds" -> { snapname -> holdname (string), ... }
5716 * (optional) "cleanup_fd" -> fd (int32)
5717 * }
5718 *
5719 * outnvl: {
5720 * snapname -> error value (int32)
5721 * ...
5722 * }
5723 */
5724 static const zfs_ioc_key_t zfs_keys_hold[] = {
5725 {"holds", DATA_TYPE_NVLIST, 0},
5726 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5727 };
5728
5729 /* ARGSUSED */
5730 static int
5731 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5732 {
5733 nvpair_t *pair;
5734 nvlist_t *holds;
5735 int cleanup_fd = -1;
5736 int error;
5737 minor_t minor = 0;
5738
5739 holds = fnvlist_lookup_nvlist(args, "holds");
5740
5741 /* make sure the user didn't pass us any invalid (empty) tags */
5742 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5743 pair = nvlist_next_nvpair(holds, pair)) {
5744 char *htag;
5745
5746 error = nvpair_value_string(pair, &htag);
5747 if (error != 0)
5748 return (SET_ERROR(error));
5749
5750 if (strlen(htag) == 0)
5751 return (SET_ERROR(EINVAL));
5752 }
5753
5754 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5755 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5756 if (error != 0)
5757 return (error);
5758 }
5759
5760 error = dsl_dataset_user_hold(holds, minor, errlist);
5761 if (minor != 0)
5762 zfs_onexit_fd_rele(cleanup_fd);
5763 return (error);
5764 }
5765
5766 /*
5767 * innvl is not used.
5768 *
5769 * outnvl: {
5770 * holdname -> time added (uint64 seconds since epoch)
5771 * ...
5772 * }
5773 */
5774 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
5775 /* no nvl keys */
5776 };
5777
5778 /* ARGSUSED */
5779 static int
5780 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5781 {
5782 return (dsl_dataset_get_holds(snapname, outnvl));
5783 }
5784
5785 /*
5786 * innvl: {
5787 * snapname -> { holdname, ... }
5788 * ...
5789 * }
5790 *
5791 * outnvl: {
5792 * snapname -> error value (int32)
5793 * ...
5794 * }
5795 */
5796 static const zfs_ioc_key_t zfs_keys_release[] = {
5797 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
5798 };
5799
5800 /* ARGSUSED */
5801 static int
5802 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5803 {
5804 return (dsl_dataset_user_release(holds, errlist));
5805 }
5806
5807 /*
5808 * inputs:
5809 * zc_guid flags (ZEVENT_NONBLOCK)
5810 * zc_cleanup_fd zevent file descriptor
5811 *
5812 * outputs:
5813 * zc_nvlist_dst next nvlist event
5814 * zc_cookie dropped events since last get
5815 */
5816 static int
5817 zfs_ioc_events_next(zfs_cmd_t *zc)
5818 {
5819 zfs_zevent_t *ze;
5820 nvlist_t *event = NULL;
5821 minor_t minor;
5822 uint64_t dropped = 0;
5823 int error;
5824
5825 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5826 if (error != 0)
5827 return (error);
5828
5829 do {
5830 error = zfs_zevent_next(ze, &event,
5831 &zc->zc_nvlist_dst_size, &dropped);
5832 if (event != NULL) {
5833 zc->zc_cookie = dropped;
5834 error = put_nvlist(zc, event);
5835 nvlist_free(event);
5836 }
5837
5838 if (zc->zc_guid & ZEVENT_NONBLOCK)
5839 break;
5840
5841 if ((error == 0) || (error != ENOENT))
5842 break;
5843
5844 error = zfs_zevent_wait(ze);
5845 if (error != 0)
5846 break;
5847 } while (1);
5848
5849 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5850
5851 return (error);
5852 }
5853
5854 /*
5855 * outputs:
5856 * zc_cookie cleared events count
5857 */
5858 static int
5859 zfs_ioc_events_clear(zfs_cmd_t *zc)
5860 {
5861 int count;
5862
5863 zfs_zevent_drain_all(&count);
5864 zc->zc_cookie = count;
5865
5866 return (0);
5867 }
5868
5869 /*
5870 * inputs:
5871 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5872 * zc_cleanup zevent file descriptor
5873 */
5874 static int
5875 zfs_ioc_events_seek(zfs_cmd_t *zc)
5876 {
5877 zfs_zevent_t *ze;
5878 minor_t minor;
5879 int error;
5880
5881 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5882 if (error != 0)
5883 return (error);
5884
5885 error = zfs_zevent_seek(ze, zc->zc_guid);
5886 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5887
5888 return (error);
5889 }
5890
5891 /*
5892 * inputs:
5893 * zc_name name of new filesystem or snapshot
5894 * zc_value full name of old snapshot
5895 *
5896 * outputs:
5897 * zc_cookie space in bytes
5898 * zc_objset_type compressed space in bytes
5899 * zc_perm_action uncompressed space in bytes
5900 */
5901 static int
5902 zfs_ioc_space_written(zfs_cmd_t *zc)
5903 {
5904 int error;
5905 dsl_pool_t *dp;
5906 dsl_dataset_t *new, *old;
5907
5908 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5909 if (error != 0)
5910 return (error);
5911 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5912 if (error != 0) {
5913 dsl_pool_rele(dp, FTAG);
5914 return (error);
5915 }
5916 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5917 if (error != 0) {
5918 dsl_dataset_rele(new, FTAG);
5919 dsl_pool_rele(dp, FTAG);
5920 return (error);
5921 }
5922
5923 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5924 &zc->zc_objset_type, &zc->zc_perm_action);
5925 dsl_dataset_rele(old, FTAG);
5926 dsl_dataset_rele(new, FTAG);
5927 dsl_pool_rele(dp, FTAG);
5928 return (error);
5929 }
5930
5931 /*
5932 * innvl: {
5933 * "firstsnap" -> snapshot name
5934 * }
5935 *
5936 * outnvl: {
5937 * "used" -> space in bytes
5938 * "compressed" -> compressed space in bytes
5939 * "uncompressed" -> uncompressed space in bytes
5940 * }
5941 */
5942 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
5943 {"firstsnap", DATA_TYPE_STRING, 0},
5944 };
5945
5946 static int
5947 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5948 {
5949 int error;
5950 dsl_pool_t *dp;
5951 dsl_dataset_t *new, *old;
5952 char *firstsnap;
5953 uint64_t used, comp, uncomp;
5954
5955 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
5956
5957 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5958 if (error != 0)
5959 return (error);
5960
5961 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5962 if (error == 0 && !new->ds_is_snapshot) {
5963 dsl_dataset_rele(new, FTAG);
5964 error = SET_ERROR(EINVAL);
5965 }
5966 if (error != 0) {
5967 dsl_pool_rele(dp, FTAG);
5968 return (error);
5969 }
5970 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5971 if (error == 0 && !old->ds_is_snapshot) {
5972 dsl_dataset_rele(old, FTAG);
5973 error = SET_ERROR(EINVAL);
5974 }
5975 if (error != 0) {
5976 dsl_dataset_rele(new, FTAG);
5977 dsl_pool_rele(dp, FTAG);
5978 return (error);
5979 }
5980
5981 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5982 dsl_dataset_rele(old, FTAG);
5983 dsl_dataset_rele(new, FTAG);
5984 dsl_pool_rele(dp, FTAG);
5985 fnvlist_add_uint64(outnvl, "used", used);
5986 fnvlist_add_uint64(outnvl, "compressed", comp);
5987 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5988 return (error);
5989 }
5990
5991 /*
5992 * innvl: {
5993 * "fd" -> file descriptor to write stream to (int32)
5994 * (optional) "fromsnap" -> full snap name to send an incremental from
5995 * (optional) "largeblockok" -> (value ignored)
5996 * indicates that blocks > 128KB are permitted
5997 * (optional) "embedok" -> (value ignored)
5998 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5999 * (optional) "compressok" -> (value ignored)
6000 * presence indicates compressed DRR_WRITE records are permitted
6001 * (optional) "rawok" -> (value ignored)
6002 * presence indicates raw encrypted records should be used.
6003 * (optional) "resume_object" and "resume_offset" -> (uint64)
6004 * if present, resume send stream from specified object and offset.
6005 * }
6006 *
6007 * outnvl is unused
6008 */
6009 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6010 {"fd", DATA_TYPE_INT32, 0},
6011 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6012 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6013 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6014 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6015 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6016 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6017 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6018 };
6019
6020 /* ARGSUSED */
6021 static int
6022 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6023 {
6024 int error;
6025 offset_t off;
6026 char *fromname = NULL;
6027 int fd;
6028 file_t *fp;
6029 boolean_t largeblockok;
6030 boolean_t embedok;
6031 boolean_t compressok;
6032 boolean_t rawok;
6033 uint64_t resumeobj = 0;
6034 uint64_t resumeoff = 0;
6035
6036 fd = fnvlist_lookup_int32(innvl, "fd");
6037
6038 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6039
6040 largeblockok = nvlist_exists(innvl, "largeblockok");
6041 embedok = nvlist_exists(innvl, "embedok");
6042 compressok = nvlist_exists(innvl, "compressok");
6043 rawok = nvlist_exists(innvl, "rawok");
6044
6045 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6046 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6047
6048 if ((fp = getf(fd)) == NULL)
6049 return (SET_ERROR(EBADF));
6050
6051 off = fp->f_offset;
6052 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6053 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6054
6055 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6056 fp->f_offset = off;
6057
6058 releasef(fd);
6059 return (error);
6060 }
6061
6062 /*
6063 * Determine approximately how large a zfs send stream will be -- the number
6064 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6065 *
6066 * innvl: {
6067 * (optional) "from" -> full snap or bookmark name to send an incremental
6068 * from
6069 * (optional) "largeblockok" -> (value ignored)
6070 * indicates that blocks > 128KB are permitted
6071 * (optional) "embedok" -> (value ignored)
6072 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6073 * (optional) "compressok" -> (value ignored)
6074 * presence indicates compressed DRR_WRITE records are permitted
6075 * (optional) "rawok" -> (value ignored)
6076 * presence indicates raw encrypted records should be used.
6077 * }
6078 *
6079 * outnvl: {
6080 * "space" -> bytes of space (uint64)
6081 * }
6082 */
6083 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6084 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6085 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6086 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6087 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6088 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6089 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6090 };
6091
6092 static int
6093 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6094 {
6095 dsl_pool_t *dp;
6096 dsl_dataset_t *tosnap;
6097 int error;
6098 char *fromname;
6099 boolean_t compressok;
6100 boolean_t rawok;
6101 uint64_t space;
6102
6103 error = dsl_pool_hold(snapname, FTAG, &dp);
6104 if (error != 0)
6105 return (error);
6106
6107 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6108 if (error != 0) {
6109 dsl_pool_rele(dp, FTAG);
6110 return (error);
6111 }
6112
6113 compressok = nvlist_exists(innvl, "compressok");
6114 rawok = nvlist_exists(innvl, "rawok");
6115
6116 error = nvlist_lookup_string(innvl, "from", &fromname);
6117 if (error == 0) {
6118 if (strchr(fromname, '@') != NULL) {
6119 /*
6120 * If from is a snapshot, hold it and use the more
6121 * efficient dmu_send_estimate to estimate send space
6122 * size using deadlists.
6123 */
6124 dsl_dataset_t *fromsnap;
6125 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6126 if (error != 0)
6127 goto out;
6128 error = dmu_send_estimate(tosnap, fromsnap,
6129 compressok || rawok, &space);
6130 dsl_dataset_rele(fromsnap, FTAG);
6131 } else if (strchr(fromname, '#') != NULL) {
6132 /*
6133 * If from is a bookmark, fetch the creation TXG of the
6134 * snapshot it was created from and use that to find
6135 * blocks that were born after it.
6136 */
6137 zfs_bookmark_phys_t frombm;
6138
6139 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6140 &frombm);
6141 if (error != 0)
6142 goto out;
6143 error = dmu_send_estimate_from_txg(tosnap,
6144 frombm.zbm_creation_txg, compressok || rawok,
6145 &space);
6146 } else {
6147 /*
6148 * from is not properly formatted as a snapshot or
6149 * bookmark
6150 */
6151 error = SET_ERROR(EINVAL);
6152 goto out;
6153 }
6154 } else {
6155 /*
6156 * If estimating the size of a full send, use dmu_send_estimate.
6157 */
6158 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6159 &space);
6160 }
6161
6162 fnvlist_add_uint64(outnvl, "space", space);
6163
6164 out:
6165 dsl_dataset_rele(tosnap, FTAG);
6166 dsl_pool_rele(dp, FTAG);
6167 return (error);
6168 }
6169
6170 /*
6171 * Sync the currently open TXG to disk for the specified pool.
6172 * This is somewhat similar to 'zfs_sync()'.
6173 * For cases that do not result in error this ioctl will wait for
6174 * the currently open TXG to commit before returning back to the caller.
6175 *
6176 * innvl: {
6177 * "force" -> when true, force uberblock update even if there is no dirty data.
6178 * In addition this will cause the vdev configuration to be written
6179 * out including updating the zpool cache file. (boolean_t)
6180 * }
6181 *
6182 * onvl is unused
6183 */
6184 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6185 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6186 };
6187
6188 /* ARGSUSED */
6189 static int
6190 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6191 {
6192 int err;
6193 boolean_t force = B_FALSE;
6194 spa_t *spa;
6195
6196 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6197 return (err);
6198
6199 if (innvl)
6200 force = fnvlist_lookup_boolean_value(innvl, "force");
6201
6202 if (force) {
6203 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6204 vdev_config_dirty(spa->spa_root_vdev);
6205 spa_config_exit(spa, SCL_CONFIG, FTAG);
6206 }
6207 txg_wait_synced(spa_get_dsl(spa), 0);
6208
6209 spa_close(spa, FTAG);
6210
6211 return (err);
6212 }
6213
6214 /*
6215 * Load a user's wrapping key into the kernel.
6216 * innvl: {
6217 * "hidden_args" -> { "wkeydata" -> value }
6218 * raw uint8_t array of encryption wrapping key data (32 bytes)
6219 * (optional) "noop" -> (value ignored)
6220 * presence indicated key should only be verified, not loaded
6221 * }
6222 */
6223 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6224 {"hidden_args", DATA_TYPE_NVLIST, 0},
6225 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6226 };
6227
6228 /* ARGSUSED */
6229 static int
6230 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6231 {
6232 int ret;
6233 dsl_crypto_params_t *dcp = NULL;
6234 nvlist_t *hidden_args;
6235 boolean_t noop = nvlist_exists(innvl, "noop");
6236
6237 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6238 ret = SET_ERROR(EINVAL);
6239 goto error;
6240 }
6241
6242 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6243
6244 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6245 hidden_args, &dcp);
6246 if (ret != 0)
6247 goto error;
6248
6249 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6250 if (ret != 0)
6251 goto error;
6252
6253 dsl_crypto_params_free(dcp, noop);
6254
6255 return (0);
6256
6257 error:
6258 dsl_crypto_params_free(dcp, B_TRUE);
6259 return (ret);
6260 }
6261
6262 /*
6263 * Unload a user's wrapping key from the kernel.
6264 * Both innvl and outnvl are unused.
6265 */
6266 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6267 /* no nvl keys */
6268 };
6269
6270 /* ARGSUSED */
6271 static int
6272 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6273 {
6274 int ret = 0;
6275
6276 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6277 ret = (SET_ERROR(EINVAL));
6278 goto out;
6279 }
6280
6281 ret = spa_keystore_unload_wkey(dsname);
6282 if (ret != 0)
6283 goto out;
6284
6285 out:
6286 return (ret);
6287 }
6288
6289 /*
6290 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6291 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6292 * here to change how the key is derived in userspace.
6293 *
6294 * innvl: {
6295 * "hidden_args" (optional) -> { "wkeydata" -> value }
6296 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6297 * "props" (optional) -> { prop -> value }
6298 * }
6299 *
6300 * outnvl is unused
6301 */
6302 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6303 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6304 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6305 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6306 };
6307
6308 /* ARGSUSED */
6309 static int
6310 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6311 {
6312 int ret;
6313 uint64_t cmd = DCP_CMD_NONE;
6314 dsl_crypto_params_t *dcp = NULL;
6315 nvlist_t *args = NULL, *hidden_args = NULL;
6316
6317 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6318 ret = (SET_ERROR(EINVAL));
6319 goto error;
6320 }
6321
6322 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6323 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6324 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6325
6326 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6327 if (ret != 0)
6328 goto error;
6329
6330 ret = spa_keystore_change_key(dsname, dcp);
6331 if (ret != 0)
6332 goto error;
6333
6334 dsl_crypto_params_free(dcp, B_FALSE);
6335
6336 return (0);
6337
6338 error:
6339 dsl_crypto_params_free(dcp, B_TRUE);
6340 return (ret);
6341 }
6342
6343 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6344
6345 static void
6346 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6347 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6348 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6349 {
6350 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6351
6352 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6353 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6354 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6355 ASSERT3P(vec->zvec_func, ==, NULL);
6356
6357 vec->zvec_legacy_func = func;
6358 vec->zvec_secpolicy = secpolicy;
6359 vec->zvec_namecheck = namecheck;
6360 vec->zvec_allow_log = log_history;
6361 vec->zvec_pool_check = pool_check;
6362 }
6363
6364 /*
6365 * See the block comment at the beginning of this file for details on
6366 * each argument to this function.
6367 */
6368 static void
6369 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6370 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6371 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6372 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6373 {
6374 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6375
6376 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6377 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6378 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6379 ASSERT3P(vec->zvec_func, ==, NULL);
6380
6381 /* if we are logging, the name must be valid */
6382 ASSERT(!allow_log || namecheck != NO_NAME);
6383
6384 vec->zvec_name = name;
6385 vec->zvec_func = func;
6386 vec->zvec_secpolicy = secpolicy;
6387 vec->zvec_namecheck = namecheck;
6388 vec->zvec_pool_check = pool_check;
6389 vec->zvec_smush_outnvlist = smush_outnvlist;
6390 vec->zvec_allow_log = allow_log;
6391 vec->zvec_nvl_keys = nvl_keys;
6392 vec->zvec_nvl_key_count = num_keys;
6393 }
6394
6395 static void
6396 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6397 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6398 zfs_ioc_poolcheck_t pool_check)
6399 {
6400 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6401 POOL_NAME, log_history, pool_check);
6402 }
6403
6404 static void
6405 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6406 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6407 {
6408 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6409 DATASET_NAME, B_FALSE, pool_check);
6410 }
6411
6412 static void
6413 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6414 {
6415 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6416 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6417 }
6418
6419 static void
6420 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6421 zfs_secpolicy_func_t *secpolicy)
6422 {
6423 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6424 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6425 }
6426
6427 static void
6428 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6429 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6430 {
6431 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6432 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6433 }
6434
6435 static void
6436 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6437 {
6438 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6439 zfs_secpolicy_read);
6440 }
6441
6442 static void
6443 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6444 zfs_secpolicy_func_t *secpolicy)
6445 {
6446 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6447 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6448 }
6449
6450 static void
6451 zfs_ioctl_init(void)
6452 {
6453 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6454 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6455 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6456 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6457
6458 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6459 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6460 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6461 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6462
6463 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6464 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6465 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6466 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6467
6468 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6469 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6470 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6471 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6472
6473 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6474 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6475 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6476 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6477
6478 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6479 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6480 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6481 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6482
6483 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6484 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6485 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6486 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6487
6488 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6489 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6490 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6491 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6492
6493 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6494 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6495 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6496 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6497
6498 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6499 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6500 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6501 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6502 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6503 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6504 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6505 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6506
6507 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6508 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6509 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6510 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6511
6512 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6513 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6514 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6515 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6516
6517 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6518 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6519 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6520 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6521
6522 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6523 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6524 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6525 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6526
6527 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6528 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6529 POOL_NAME,
6530 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6531 zfs_keys_destroy_bookmarks,
6532 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6533
6534 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6535 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6536 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6537 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
6538 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6539 zfs_ioc_load_key, zfs_secpolicy_load_key,
6540 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6541 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6542 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6543 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6544 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6545 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6546 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6547 zfs_ioc_change_key, zfs_secpolicy_change_key,
6548 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6549 B_TRUE, B_TRUE, zfs_keys_change_key,
6550 ARRAY_SIZE(zfs_keys_change_key));
6551
6552 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6553 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6554 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6555 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6556 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6557 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6558 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
6559
6560 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6561 zfs_ioc_channel_program, zfs_secpolicy_config,
6562 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6563 B_TRUE, zfs_keys_channel_program,
6564 ARRAY_SIZE(zfs_keys_channel_program));
6565
6566 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6567 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6568 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6569 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6570
6571 zfs_ioctl_register("zpool_discard_checkpoint",
6572 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6573 zfs_secpolicy_config, POOL_NAME,
6574 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6575 zfs_keys_pool_discard_checkpoint,
6576 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6577
6578 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6579 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6580 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6581 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6582
6583 /* IOCTLS that use the legacy function signature */
6584
6585 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6586 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6587
6588 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6589 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6590 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6591 zfs_ioc_pool_scan);
6592 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6593 zfs_ioc_pool_upgrade);
6594 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6595 zfs_ioc_vdev_add);
6596 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6597 zfs_ioc_vdev_remove);
6598 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6599 zfs_ioc_vdev_set_state);
6600 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6601 zfs_ioc_vdev_attach);
6602 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6603 zfs_ioc_vdev_detach);
6604 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6605 zfs_ioc_vdev_setpath);
6606 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6607 zfs_ioc_vdev_setfru);
6608 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6609 zfs_ioc_pool_set_props);
6610 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6611 zfs_ioc_vdev_split);
6612 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6613 zfs_ioc_pool_reguid);
6614
6615 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6616 zfs_ioc_pool_configs, zfs_secpolicy_none);
6617 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6618 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6619 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6620 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6621 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6622 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6623 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6624 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6625
6626 /*
6627 * pool destroy, and export don't log the history as part of
6628 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6629 * does the logging of those commands.
6630 */
6631 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6632 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6633 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6634 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6635
6636 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6637 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6638 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6639 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6640
6641 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6642 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6643 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6644 zfs_ioc_dsobj_to_dsname,
6645 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6646 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6647 zfs_ioc_pool_get_history,
6648 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6649
6650 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6651 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6652
6653 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6654 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6655
6656 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6657 zfs_ioc_space_written);
6658 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6659 zfs_ioc_objset_recvd_props);
6660 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6661 zfs_ioc_next_obj);
6662 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6663 zfs_ioc_get_fsacl);
6664 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6665 zfs_ioc_objset_stats);
6666 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6667 zfs_ioc_objset_zplprops);
6668 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6669 zfs_ioc_dataset_list_next);
6670 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6671 zfs_ioc_snapshot_list_next);
6672 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6673 zfs_ioc_send_progress);
6674
6675 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6676 zfs_ioc_diff, zfs_secpolicy_diff);
6677 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6678 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6679 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6680 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6681 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6682 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6683 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6684 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6685 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6686 zfs_ioc_send, zfs_secpolicy_send);
6687
6688 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6689 zfs_secpolicy_none);
6690 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6691 zfs_secpolicy_destroy);
6692 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6693 zfs_secpolicy_rename);
6694 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6695 zfs_secpolicy_recv);
6696 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6697 zfs_secpolicy_promote);
6698 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6699 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6700 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6701 zfs_secpolicy_set_fsacl);
6702
6703 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6704 zfs_secpolicy_share, POOL_CHECK_NONE);
6705 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6706 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6707 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6708 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6709 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6710 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6711 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6712 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6713
6714 /*
6715 * ZoL functions
6716 */
6717 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
6718 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6719 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
6720 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6721 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
6722 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6723 }
6724
6725 /*
6726 * Verify that for non-legacy ioctls the input nvlist
6727 * pairs match against the expected input.
6728 *
6729 * Possible errors are:
6730 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
6731 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
6732 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
6733 */
6734 static int
6735 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
6736 {
6737 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
6738 boolean_t required_keys_found = B_FALSE;
6739
6740 /*
6741 * examine each input pair
6742 */
6743 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
6744 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
6745 char *name = nvpair_name(pair);
6746 data_type_t type = nvpair_type(pair);
6747 boolean_t identified = B_FALSE;
6748
6749 /*
6750 * check pair against the documented names and type
6751 */
6752 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6753 /* if not a wild card name, check for an exact match */
6754 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
6755 strcmp(nvl_keys[k].zkey_name, name) != 0)
6756 continue;
6757
6758 identified = B_TRUE;
6759
6760 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
6761 nvl_keys[k].zkey_type != type) {
6762 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
6763 }
6764
6765 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6766 continue;
6767
6768 required_keys_found = B_TRUE;
6769 break;
6770 }
6771
6772 /* allow an 'optional' key, everything else is invalid */
6773 if (!identified &&
6774 (strcmp(name, "optional") != 0 ||
6775 type != DATA_TYPE_NVLIST)) {
6776 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
6777 }
6778 }
6779
6780 /* verify that all required keys were found */
6781 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6782 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6783 continue;
6784
6785 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
6786 /* at least one non-optionial key is expected here */
6787 if (!required_keys_found)
6788 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6789 continue;
6790 }
6791
6792 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
6793 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6794 }
6795
6796 return (0);
6797 }
6798
6799 int
6800 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6801 zfs_ioc_poolcheck_t check)
6802 {
6803 spa_t *spa;
6804 int error;
6805
6806 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6807
6808 if (check & POOL_CHECK_NONE)
6809 return (0);
6810
6811 error = spa_open(name, &spa, FTAG);
6812 if (error == 0) {
6813 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6814 error = SET_ERROR(EAGAIN);
6815 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6816 error = SET_ERROR(EROFS);
6817 spa_close(spa, FTAG);
6818 }
6819 return (error);
6820 }
6821
6822 static void *
6823 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
6824 {
6825 zfsdev_state_t *zs;
6826
6827 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6828 if (zs->zs_minor == minor) {
6829 smp_rmb();
6830 switch (which) {
6831 case ZST_ONEXIT:
6832 return (zs->zs_onexit);
6833 case ZST_ZEVENT:
6834 return (zs->zs_zevent);
6835 case ZST_ALL:
6836 return (zs);
6837 }
6838 }
6839 }
6840
6841 return (NULL);
6842 }
6843
6844 void *
6845 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
6846 {
6847 void *ptr;
6848
6849 ptr = zfsdev_get_state_impl(minor, which);
6850
6851 return (ptr);
6852 }
6853
6854 int
6855 zfsdev_getminor(struct file *filp, minor_t *minorp)
6856 {
6857 zfsdev_state_t *zs, *fpd;
6858
6859 ASSERT(filp != NULL);
6860 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
6861
6862 fpd = filp->private_data;
6863 if (fpd == NULL)
6864 return (SET_ERROR(EBADF));
6865
6866 mutex_enter(&zfsdev_state_lock);
6867
6868 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6869
6870 if (zs->zs_minor == -1)
6871 continue;
6872
6873 if (fpd == zs) {
6874 *minorp = fpd->zs_minor;
6875 mutex_exit(&zfsdev_state_lock);
6876 return (0);
6877 }
6878 }
6879
6880 mutex_exit(&zfsdev_state_lock);
6881
6882 return (SET_ERROR(EBADF));
6883 }
6884
6885 /*
6886 * Find a free minor number. The zfsdev_state_list is expected to
6887 * be short since it is only a list of currently open file handles.
6888 */
6889 minor_t
6890 zfsdev_minor_alloc(void)
6891 {
6892 static minor_t last_minor = 0;
6893 minor_t m;
6894
6895 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6896
6897 for (m = last_minor + 1; m != last_minor; m++) {
6898 if (m > ZFSDEV_MAX_MINOR)
6899 m = 1;
6900 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
6901 last_minor = m;
6902 return (m);
6903 }
6904 }
6905
6906 return (0);
6907 }
6908
6909 static int
6910 zfsdev_state_init(struct file *filp)
6911 {
6912 zfsdev_state_t *zs, *zsprev = NULL;
6913 minor_t minor;
6914 boolean_t newzs = B_FALSE;
6915
6916 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6917
6918 minor = zfsdev_minor_alloc();
6919 if (minor == 0)
6920 return (SET_ERROR(ENXIO));
6921
6922 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6923 if (zs->zs_minor == -1)
6924 break;
6925 zsprev = zs;
6926 }
6927
6928 if (!zs) {
6929 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6930 newzs = B_TRUE;
6931 }
6932
6933 zs->zs_file = filp;
6934 filp->private_data = zs;
6935
6936 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
6937 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
6938
6939
6940 /*
6941 * In order to provide for lock-free concurrent read access
6942 * to the minor list in zfsdev_get_state_impl(), new entries
6943 * must be completely written before linking them into the
6944 * list whereas existing entries are already linked; the last
6945 * operation must be updating zs_minor (from -1 to the new
6946 * value).
6947 */
6948 if (newzs) {
6949 zs->zs_minor = minor;
6950 smp_wmb();
6951 zsprev->zs_next = zs;
6952 } else {
6953 smp_wmb();
6954 zs->zs_minor = minor;
6955 }
6956
6957 return (0);
6958 }
6959
6960 static int
6961 zfsdev_state_destroy(struct file *filp)
6962 {
6963 zfsdev_state_t *zs;
6964
6965 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6966 ASSERT(filp->private_data != NULL);
6967
6968 zs = filp->private_data;
6969 zs->zs_minor = -1;
6970 zfs_onexit_destroy(zs->zs_onexit);
6971 zfs_zevent_destroy(zs->zs_zevent);
6972
6973 return (0);
6974 }
6975
6976 static int
6977 zfsdev_open(struct inode *ino, struct file *filp)
6978 {
6979 int error;
6980
6981 mutex_enter(&zfsdev_state_lock);
6982 error = zfsdev_state_init(filp);
6983 mutex_exit(&zfsdev_state_lock);
6984
6985 return (-error);
6986 }
6987
6988 static int
6989 zfsdev_release(struct inode *ino, struct file *filp)
6990 {
6991 int error;
6992
6993 mutex_enter(&zfsdev_state_lock);
6994 error = zfsdev_state_destroy(filp);
6995 mutex_exit(&zfsdev_state_lock);
6996
6997 return (-error);
6998 }
6999
7000 static long
7001 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7002 {
7003 zfs_cmd_t *zc;
7004 uint_t vecnum;
7005 int error, rc, flag = 0;
7006 const zfs_ioc_vec_t *vec;
7007 char *saved_poolname = NULL;
7008 nvlist_t *innvl = NULL;
7009 fstrans_cookie_t cookie;
7010
7011 vecnum = cmd - ZFS_IOC_FIRST;
7012 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7013 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7014 vec = &zfs_ioc_vec[vecnum];
7015
7016 /*
7017 * The registered ioctl list may be sparse, verify that either
7018 * a normal or legacy handler are registered.
7019 */
7020 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7021 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7022
7023 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
7024
7025 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7026 if (error != 0) {
7027 error = SET_ERROR(EFAULT);
7028 goto out;
7029 }
7030
7031 zc->zc_iflags = flag & FKIOCTL;
7032 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
7033 /*
7034 * Make sure the user doesn't pass in an insane value for
7035 * zc_nvlist_src_size. We have to check, since we will end
7036 * up allocating that much memory inside of get_nvlist(). This
7037 * prevents a nefarious user from allocating tons of kernel
7038 * memory.
7039 *
7040 * Also, we return EINVAL instead of ENOMEM here. The reason
7041 * being that returning ENOMEM from an ioctl() has a special
7042 * connotation; that the user's size value is too small and
7043 * needs to be expanded to hold the nvlist. See
7044 * zcmd_expand_dst_nvlist() for details.
7045 */
7046 error = SET_ERROR(EINVAL); /* User's size too big */
7047
7048 } else if (zc->zc_nvlist_src_size != 0) {
7049 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7050 zc->zc_iflags, &innvl);
7051 if (error != 0)
7052 goto out;
7053 }
7054
7055 /*
7056 * Ensure that all pool/dataset names are valid before we pass down to
7057 * the lower layers.
7058 */
7059 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7060 switch (vec->zvec_namecheck) {
7061 case POOL_NAME:
7062 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7063 error = SET_ERROR(EINVAL);
7064 else
7065 error = pool_status_check(zc->zc_name,
7066 vec->zvec_namecheck, vec->zvec_pool_check);
7067 break;
7068
7069 case DATASET_NAME:
7070 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7071 error = SET_ERROR(EINVAL);
7072 else
7073 error = pool_status_check(zc->zc_name,
7074 vec->zvec_namecheck, vec->zvec_pool_check);
7075 break;
7076
7077 case NO_NAME:
7078 break;
7079 }
7080
7081 /*
7082 * Ensure that all input pairs are valid before we pass them down
7083 * to the lower layers.
7084 *
7085 * The vectored functions can use fnvlist_lookup_{type} for any
7086 * required pairs since zfs_check_input_nvpairs() confirmed that
7087 * they exist and are of the correct type.
7088 */
7089 if (error == 0 && vec->zvec_func != NULL) {
7090 error = zfs_check_input_nvpairs(innvl, vec);
7091 if (error != 0)
7092 goto out;
7093 }
7094
7095 if (error == 0) {
7096 cookie = spl_fstrans_mark();
7097 error = vec->zvec_secpolicy(zc, innvl, CRED());
7098 spl_fstrans_unmark(cookie);
7099 }
7100
7101 if (error != 0)
7102 goto out;
7103
7104 /* legacy ioctls can modify zc_name */
7105 saved_poolname = strdup(zc->zc_name);
7106 if (saved_poolname == NULL) {
7107 error = SET_ERROR(ENOMEM);
7108 goto out;
7109 } else {
7110 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7111 }
7112
7113 if (vec->zvec_func != NULL) {
7114 nvlist_t *outnvl;
7115 int puterror = 0;
7116 spa_t *spa;
7117 nvlist_t *lognv = NULL;
7118
7119 ASSERT(vec->zvec_legacy_func == NULL);
7120
7121 /*
7122 * Add the innvl to the lognv before calling the func,
7123 * in case the func changes the innvl.
7124 */
7125 if (vec->zvec_allow_log) {
7126 lognv = fnvlist_alloc();
7127 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7128 vec->zvec_name);
7129 if (!nvlist_empty(innvl)) {
7130 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7131 innvl);
7132 }
7133 }
7134
7135 outnvl = fnvlist_alloc();
7136 cookie = spl_fstrans_mark();
7137 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7138 spl_fstrans_unmark(cookie);
7139
7140 /*
7141 * Some commands can partially execute, modify state, and still
7142 * return an error. In these cases, attempt to record what
7143 * was modified.
7144 */
7145 if ((error == 0 ||
7146 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7147 vec->zvec_allow_log &&
7148 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7149 if (!nvlist_empty(outnvl)) {
7150 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7151 outnvl);
7152 }
7153 if (error != 0) {
7154 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7155 error);
7156 }
7157 (void) spa_history_log_nvl(spa, lognv);
7158 spa_close(spa, FTAG);
7159 }
7160 fnvlist_free(lognv);
7161
7162 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7163 int smusherror = 0;
7164 if (vec->zvec_smush_outnvlist) {
7165 smusherror = nvlist_smush(outnvl,
7166 zc->zc_nvlist_dst_size);
7167 }
7168 if (smusherror == 0)
7169 puterror = put_nvlist(zc, outnvl);
7170 }
7171
7172 if (puterror != 0)
7173 error = puterror;
7174
7175 nvlist_free(outnvl);
7176 } else {
7177 cookie = spl_fstrans_mark();
7178 error = vec->zvec_legacy_func(zc);
7179 spl_fstrans_unmark(cookie);
7180 }
7181
7182 out:
7183 nvlist_free(innvl);
7184 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7185 if (error == 0 && rc != 0)
7186 error = SET_ERROR(EFAULT);
7187 if (error == 0 && vec->zvec_allow_log) {
7188 char *s = tsd_get(zfs_allow_log_key);
7189 if (s != NULL)
7190 strfree(s);
7191 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7192 } else {
7193 if (saved_poolname != NULL)
7194 strfree(saved_poolname);
7195 }
7196
7197 kmem_free(zc, sizeof (zfs_cmd_t));
7198 return (-error);
7199 }
7200
7201 #ifdef CONFIG_COMPAT
7202 static long
7203 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7204 {
7205 return (zfsdev_ioctl(filp, cmd, arg));
7206 }
7207 #else
7208 #define zfsdev_compat_ioctl NULL
7209 #endif
7210
7211 static const struct file_operations zfsdev_fops = {
7212 .open = zfsdev_open,
7213 .release = zfsdev_release,
7214 .unlocked_ioctl = zfsdev_ioctl,
7215 .compat_ioctl = zfsdev_compat_ioctl,
7216 .owner = THIS_MODULE,
7217 };
7218
7219 static struct miscdevice zfs_misc = {
7220 .minor = ZFS_DEVICE_MINOR,
7221 .name = ZFS_DRIVER,
7222 .fops = &zfsdev_fops,
7223 };
7224
7225 MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR);
7226 MODULE_ALIAS("devname:zfs");
7227
7228 static int
7229 zfs_attach(void)
7230 {
7231 int error;
7232
7233 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7234 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7235 zfsdev_state_list->zs_minor = -1;
7236
7237 error = misc_register(&zfs_misc);
7238 if (error == -EBUSY) {
7239 /*
7240 * Fallback to dynamic minor allocation in the event of a
7241 * collision with a reserved minor in linux/miscdevice.h.
7242 * In this case the kernel modules must be manually loaded.
7243 */
7244 printk(KERN_INFO "ZFS: misc_register() with static minor %d "
7245 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
7246 ZFS_DEVICE_MINOR, error);
7247
7248 zfs_misc.minor = MISC_DYNAMIC_MINOR;
7249 error = misc_register(&zfs_misc);
7250 }
7251
7252 if (error)
7253 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
7254
7255 return (error);
7256 }
7257
7258 static void
7259 zfs_detach(void)
7260 {
7261 zfsdev_state_t *zs, *zsprev = NULL;
7262
7263 misc_deregister(&zfs_misc);
7264 mutex_destroy(&zfsdev_state_lock);
7265
7266 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7267 if (zsprev)
7268 kmem_free(zsprev, sizeof (zfsdev_state_t));
7269 zsprev = zs;
7270 }
7271 if (zsprev)
7272 kmem_free(zsprev, sizeof (zfsdev_state_t));
7273 }
7274
7275 static void
7276 zfs_allow_log_destroy(void *arg)
7277 {
7278 char *poolname = arg;
7279
7280 if (poolname != NULL)
7281 strfree(poolname);
7282 }
7283
7284 #ifdef DEBUG
7285 #define ZFS_DEBUG_STR " (DEBUG mode)"
7286 #else
7287 #define ZFS_DEBUG_STR ""
7288 #endif
7289
7290 static int __init
7291 _init(void)
7292 {
7293 int error;
7294
7295 error = -vn_set_pwd("/");
7296 if (error) {
7297 printk(KERN_NOTICE
7298 "ZFS: Warning unable to set pwd to '/': %d\n", error);
7299 return (error);
7300 }
7301
7302 if ((error = -zvol_init()) != 0)
7303 return (error);
7304
7305 spa_init(FREAD | FWRITE);
7306 zfs_init();
7307
7308 zfs_ioctl_init();
7309 zfs_sysfs_init();
7310
7311 if ((error = zfs_attach()) != 0)
7312 goto out;
7313
7314 tsd_create(&zfs_fsyncer_key, NULL);
7315 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7316 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7317
7318 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
7319 "ZFS pool version %s, ZFS filesystem version %s\n",
7320 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
7321 SPA_VERSION_STRING, ZPL_VERSION_STRING);
7322 #ifndef CONFIG_FS_POSIX_ACL
7323 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
7324 #endif /* CONFIG_FS_POSIX_ACL */
7325
7326 return (0);
7327
7328 out:
7329 zfs_sysfs_fini();
7330 zfs_fini();
7331 spa_fini();
7332 (void) zvol_fini();
7333 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
7334 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
7335 ZFS_DEBUG_STR, error);
7336
7337 return (error);
7338 }
7339
7340 static void __exit
7341 _fini(void)
7342 {
7343 zfs_detach();
7344 zfs_sysfs_fini();
7345 zfs_fini();
7346 spa_fini();
7347 zvol_fini();
7348
7349 tsd_destroy(&zfs_fsyncer_key);
7350 tsd_destroy(&rrw_tsd_key);
7351 tsd_destroy(&zfs_allow_log_key);
7352
7353 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
7354 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
7355 }
7356
7357 #if defined(_KERNEL)
7358 module_init(_init);
7359 module_exit(_fini);
7360
7361 MODULE_DESCRIPTION("ZFS");
7362 MODULE_AUTHOR(ZFS_META_AUTHOR);
7363 MODULE_LICENSE(ZFS_META_LICENSE);
7364 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
7365 #endif