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