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