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 };
1708
1709 static int
zfs_ioc_pool_scrub(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)1710 zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
1711 {
1712 spa_t *spa;
1713 int error;
1714 uint64_t scan_type, scan_cmd;
1715
1716 if (nvlist_lookup_uint64(innvl, "scan_type", &scan_type) != 0)
1717 return (SET_ERROR(EINVAL));
1718 if (nvlist_lookup_uint64(innvl, "scan_command", &scan_cmd) != 0)
1719 return (SET_ERROR(EINVAL));
1720
1721 if (scan_cmd >= POOL_SCRUB_FLAGS_END)
1722 return (SET_ERROR(EINVAL));
1723
1724 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
1725 return (error);
1726
1727 if (scan_cmd == POOL_SCRUB_PAUSE) {
1728 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1729 } else if (scan_type == POOL_SCAN_NONE) {
1730 error = spa_scan_stop(spa);
1731 } else if (scan_cmd == POOL_SCRUB_FROM_LAST_TXG) {
1732 error = spa_scan_range(spa, scan_type,
1733 spa_get_last_scrubbed_txg(spa), 0);
1734 } else {
1735 error = spa_scan(spa, scan_type);
1736 }
1737
1738 spa_close(spa, FTAG);
1739 return (error);
1740 }
1741
1742 static int
zfs_ioc_pool_freeze(zfs_cmd_t * zc)1743 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1744 {
1745 spa_t *spa;
1746 int error;
1747
1748 error = spa_open(zc->zc_name, &spa, FTAG);
1749 if (error == 0) {
1750 spa_freeze(spa);
1751 spa_close(spa, FTAG);
1752 }
1753 return (error);
1754 }
1755
1756 static int
zfs_ioc_pool_upgrade(zfs_cmd_t * zc)1757 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1758 {
1759 spa_t *spa;
1760 int error;
1761
1762 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1763 return (error);
1764
1765 if (zc->zc_cookie < spa_version(spa) ||
1766 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1767 spa_close(spa, FTAG);
1768 return (SET_ERROR(EINVAL));
1769 }
1770
1771 spa_upgrade(spa, zc->zc_cookie);
1772 spa_close(spa, FTAG);
1773
1774 return (error);
1775 }
1776
1777 static int
zfs_ioc_pool_get_history(zfs_cmd_t * zc)1778 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1779 {
1780 spa_t *spa;
1781 char *hist_buf;
1782 uint64_t size;
1783 int error;
1784
1785 if ((size = zc->zc_history_len) == 0)
1786 return (SET_ERROR(EINVAL));
1787
1788 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1789 return (error);
1790
1791 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1792 spa_close(spa, FTAG);
1793 return (SET_ERROR(ENOTSUP));
1794 }
1795
1796 hist_buf = vmem_alloc(size, KM_SLEEP);
1797 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1798 &zc->zc_history_len, hist_buf)) == 0) {
1799 error = ddi_copyout(hist_buf,
1800 (void *)(uintptr_t)zc->zc_history,
1801 zc->zc_history_len, zc->zc_iflags);
1802 }
1803
1804 spa_close(spa, FTAG);
1805 vmem_free(hist_buf, size);
1806 return (error);
1807 }
1808
1809 /*
1810 * inputs:
1811 * zc_nvlist_src nvlist optionally containing ZPOOL_REGUID_GUID
1812 * zc_nvlist_src_size size of the nvlist
1813 */
1814 static int
zfs_ioc_pool_reguid(zfs_cmd_t * zc)1815 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1816 {
1817 uint64_t *guidp = NULL;
1818 nvlist_t *props = NULL;
1819 spa_t *spa;
1820 uint64_t guid;
1821 int error;
1822
1823 if (zc->zc_nvlist_src_size != 0) {
1824 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1825 zc->zc_iflags, &props);
1826 if (error != 0)
1827 return (error);
1828
1829 error = nvlist_lookup_uint64(props, ZPOOL_REGUID_GUID, &guid);
1830 if (error == 0)
1831 guidp = &guid;
1832 else if (error == ENOENT)
1833 guidp = NULL;
1834 else
1835 goto out;
1836 }
1837
1838 error = spa_open(zc->zc_name, &spa, FTAG);
1839 if (error == 0) {
1840 error = spa_change_guid(spa, guidp);
1841 spa_close(spa, FTAG);
1842 }
1843
1844 out:
1845 if (props != NULL)
1846 nvlist_free(props);
1847
1848 return (error);
1849 }
1850
1851 static int
zfs_ioc_dsobj_to_dsname(zfs_cmd_t * zc)1852 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1853 {
1854 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1855 }
1856
1857 /*
1858 * inputs:
1859 * zc_name name of filesystem
1860 * zc_obj object to find
1861 *
1862 * outputs:
1863 * zc_value name of object
1864 */
1865 static int
zfs_ioc_obj_to_path(zfs_cmd_t * zc)1866 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1867 {
1868 objset_t *os;
1869 int error;
1870
1871 /* XXX reading from objset not owned */
1872 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1873 FTAG, &os)) != 0)
1874 return (error);
1875 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1876 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1877 return (SET_ERROR(EINVAL));
1878 }
1879 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1880 sizeof (zc->zc_value));
1881 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1882
1883 return (error);
1884 }
1885
1886 /*
1887 * inputs:
1888 * zc_name name of filesystem
1889 * zc_obj object to find
1890 *
1891 * outputs:
1892 * zc_stat stats on object
1893 * zc_value path to object
1894 */
1895 static int
zfs_ioc_obj_to_stats(zfs_cmd_t * zc)1896 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1897 {
1898 objset_t *os;
1899 int error;
1900
1901 /* XXX reading from objset not owned */
1902 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1903 FTAG, &os)) != 0)
1904 return (error);
1905 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1906 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1907 return (SET_ERROR(EINVAL));
1908 }
1909 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1910 sizeof (zc->zc_value));
1911 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1912
1913 return (error);
1914 }
1915
1916 static int
zfs_ioc_vdev_add(zfs_cmd_t * zc)1917 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1918 {
1919 spa_t *spa;
1920 int error;
1921 nvlist_t *config;
1922
1923 error = spa_open(zc->zc_name, &spa, FTAG);
1924 if (error != 0)
1925 return (error);
1926
1927 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1928 zc->zc_iflags, &config);
1929 if (error == 0) {
1930 error = spa_vdev_add(spa, config, zc->zc_flags);
1931 nvlist_free(config);
1932 }
1933 spa_close(spa, FTAG);
1934 return (error);
1935 }
1936
1937 /*
1938 * inputs:
1939 * zc_name name of the pool
1940 * zc_guid guid of vdev to remove
1941 * zc_cookie cancel removal
1942 */
1943 static int
zfs_ioc_vdev_remove(zfs_cmd_t * zc)1944 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1945 {
1946 spa_t *spa;
1947 int error;
1948
1949 error = spa_open(zc->zc_name, &spa, FTAG);
1950 if (error != 0)
1951 return (error);
1952 if (zc->zc_cookie != 0) {
1953 error = spa_vdev_remove_cancel(spa);
1954 } else {
1955 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1956 }
1957 spa_close(spa, FTAG);
1958 return (error);
1959 }
1960
1961 static int
zfs_ioc_vdev_set_state(zfs_cmd_t * zc)1962 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1963 {
1964 spa_t *spa;
1965 int error;
1966 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1967
1968 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1969 return (error);
1970 switch (zc->zc_cookie) {
1971 case VDEV_STATE_ONLINE:
1972 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1973 break;
1974
1975 case VDEV_STATE_OFFLINE:
1976 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1977 break;
1978
1979 case VDEV_STATE_FAULTED:
1980 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1981 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1982 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1983 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1984
1985 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1986 break;
1987
1988 case VDEV_STATE_DEGRADED:
1989 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1990 zc->zc_obj != VDEV_AUX_EXTERNAL)
1991 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1992
1993 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1994 break;
1995
1996 case VDEV_STATE_REMOVED:
1997 error = vdev_remove_wanted(spa, zc->zc_guid);
1998 break;
1999
2000 default:
2001 error = SET_ERROR(EINVAL);
2002 }
2003 zc->zc_cookie = newstate;
2004 spa_close(spa, FTAG);
2005 return (error);
2006 }
2007
2008 static int
zfs_ioc_vdev_attach(zfs_cmd_t * zc)2009 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2010 {
2011 spa_t *spa;
2012 nvlist_t *config;
2013 int replacing = zc->zc_cookie;
2014 int rebuild = zc->zc_simple;
2015 int error;
2016
2017 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2018 return (error);
2019
2020 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2021 zc->zc_iflags, &config)) == 0) {
2022 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
2023 rebuild);
2024 nvlist_free(config);
2025 }
2026
2027 spa_close(spa, FTAG);
2028 return (error);
2029 }
2030
2031 static int
zfs_ioc_vdev_detach(zfs_cmd_t * zc)2032 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2033 {
2034 spa_t *spa;
2035 int error;
2036
2037 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2038 return (error);
2039
2040 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2041
2042 spa_close(spa, FTAG);
2043 return (error);
2044 }
2045
2046 static int
zfs_ioc_vdev_split(zfs_cmd_t * zc)2047 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2048 {
2049 spa_t *spa;
2050 nvlist_t *config, *props = NULL;
2051 int error;
2052 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2053
2054 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2055 return (error);
2056
2057 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2058 zc->zc_iflags, &config))) {
2059 spa_close(spa, FTAG);
2060 return (error);
2061 }
2062
2063 if (zc->zc_nvlist_src_size != 0 && (error =
2064 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2065 zc->zc_iflags, &props))) {
2066 spa_close(spa, FTAG);
2067 nvlist_free(config);
2068 return (error);
2069 }
2070
2071 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2072
2073 spa_close(spa, FTAG);
2074
2075 nvlist_free(config);
2076 nvlist_free(props);
2077
2078 return (error);
2079 }
2080
2081 static int
zfs_ioc_vdev_setpath(zfs_cmd_t * zc)2082 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2083 {
2084 spa_t *spa;
2085 const char *path = zc->zc_value;
2086 uint64_t guid = zc->zc_guid;
2087 int error;
2088
2089 error = spa_open(zc->zc_name, &spa, FTAG);
2090 if (error != 0)
2091 return (error);
2092
2093 error = spa_vdev_setpath(spa, guid, path);
2094 spa_close(spa, FTAG);
2095 return (error);
2096 }
2097
2098 static int
zfs_ioc_vdev_setfru(zfs_cmd_t * zc)2099 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2100 {
2101 spa_t *spa;
2102 const char *fru = zc->zc_value;
2103 uint64_t guid = zc->zc_guid;
2104 int error;
2105
2106 error = spa_open(zc->zc_name, &spa, FTAG);
2107 if (error != 0)
2108 return (error);
2109
2110 error = spa_vdev_setfru(spa, guid, fru);
2111 spa_close(spa, FTAG);
2112 return (error);
2113 }
2114
2115 static int
zfs_ioc_objset_stats_impl(zfs_cmd_t * zc,objset_t * os)2116 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2117 {
2118 int error = 0;
2119 nvlist_t *nv;
2120
2121 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2122
2123 if (!zc->zc_simple && zc->zc_nvlist_dst != 0 &&
2124 (error = dsl_prop_get_all(os, &nv)) == 0) {
2125 dmu_objset_stats(os, nv);
2126 /*
2127 * NB: zvol_get_stats() will read the objset contents,
2128 * which we aren't supposed to do with a
2129 * DS_MODE_USER hold, because it could be
2130 * inconsistent. So this is a bit of a workaround...
2131 * XXX reading without owning
2132 */
2133 if (!zc->zc_objset_stats.dds_inconsistent &&
2134 dmu_objset_type(os) == DMU_OST_ZVOL) {
2135 error = zvol_get_stats(os, nv);
2136 if (error == EIO) {
2137 nvlist_free(nv);
2138 return (error);
2139 }
2140 VERIFY0(error);
2141 }
2142 if (error == 0)
2143 error = put_nvlist(zc, nv);
2144 nvlist_free(nv);
2145 }
2146
2147 return (error);
2148 }
2149
2150 /*
2151 * inputs:
2152 * zc_name name of filesystem
2153 * zc_nvlist_dst_size size of buffer for property nvlist
2154 *
2155 * outputs:
2156 * zc_objset_stats stats
2157 * zc_nvlist_dst property nvlist
2158 * zc_nvlist_dst_size size of property nvlist
2159 */
2160 static int
zfs_ioc_objset_stats(zfs_cmd_t * zc)2161 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2162 {
2163 objset_t *os;
2164 int error;
2165
2166 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2167 if (error == 0) {
2168 error = zfs_ioc_objset_stats_impl(zc, os);
2169 dmu_objset_rele(os, FTAG);
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_nvlist_dst received property nvlist
2182 * zc_nvlist_dst_size size of received property nvlist
2183 *
2184 * Gets received properties (distinct from local properties on or after
2185 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2186 * local property values.
2187 */
2188 static int
zfs_ioc_objset_recvd_props(zfs_cmd_t * zc)2189 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2190 {
2191 int error = 0;
2192 nvlist_t *nv;
2193
2194 /*
2195 * Without this check, we would return local property values if the
2196 * caller has not already received properties on or after
2197 * SPA_VERSION_RECVD_PROPS.
2198 */
2199 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2200 return (SET_ERROR(ENOTSUP));
2201
2202 if (zc->zc_nvlist_dst != 0 &&
2203 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2204 error = put_nvlist(zc, nv);
2205 nvlist_free(nv);
2206 }
2207
2208 return (error);
2209 }
2210
2211 static int
nvl_add_zplprop(objset_t * os,nvlist_t * props,zfs_prop_t prop)2212 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2213 {
2214 uint64_t value;
2215 int error;
2216
2217 /*
2218 * zfs_get_zplprop() will either find a value or give us
2219 * the default value (if there is one).
2220 */
2221 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2222 return (error);
2223 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2224 return (0);
2225 }
2226
2227 /*
2228 * inputs:
2229 * zc_name name of filesystem
2230 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2231 *
2232 * outputs:
2233 * zc_nvlist_dst zpl property nvlist
2234 * zc_nvlist_dst_size size of zpl property nvlist
2235 */
2236 static int
zfs_ioc_objset_zplprops(zfs_cmd_t * zc)2237 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2238 {
2239 objset_t *os;
2240 int err;
2241
2242 /* XXX reading without owning */
2243 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2244 return (err);
2245
2246 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2247
2248 /*
2249 * NB: nvl_add_zplprop() will read the objset contents,
2250 * which we aren't supposed to do with a DS_MODE_USER
2251 * hold, because it could be inconsistent.
2252 */
2253 if (zc->zc_nvlist_dst != 0 &&
2254 !zc->zc_objset_stats.dds_inconsistent &&
2255 dmu_objset_type(os) == DMU_OST_ZFS) {
2256 nvlist_t *nv;
2257
2258 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2259 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2260 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2261 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2262 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0 &&
2263 (err = nvl_add_zplprop(os, nv,
2264 ZFS_PROP_DEFAULTUSERQUOTA)) == 0 &&
2265 (err = nvl_add_zplprop(os, nv,
2266 ZFS_PROP_DEFAULTGROUPQUOTA)) == 0 &&
2267 (err = nvl_add_zplprop(os, nv,
2268 ZFS_PROP_DEFAULTPROJECTQUOTA)) == 0 &&
2269 (err = nvl_add_zplprop(os, nv,
2270 ZFS_PROP_DEFAULTUSEROBJQUOTA)) == 0 &&
2271 (err = nvl_add_zplprop(os, nv,
2272 ZFS_PROP_DEFAULTGROUPOBJQUOTA)) == 0 &&
2273 (err = nvl_add_zplprop(os, nv,
2274 ZFS_PROP_DEFAULTPROJECTOBJQUOTA)) == 0)
2275 err = put_nvlist(zc, nv);
2276 nvlist_free(nv);
2277 } else {
2278 err = SET_ERROR(ENOENT);
2279 }
2280 dmu_objset_rele(os, FTAG);
2281 return (err);
2282 }
2283
2284 /*
2285 * inputs:
2286 * zc_name name of filesystem
2287 * zc_cookie zap cursor
2288 * zc_nvlist_dst_size size of buffer for property nvlist
2289 *
2290 * outputs:
2291 * zc_name name of next filesystem
2292 * zc_cookie zap cursor
2293 * zc_objset_stats stats
2294 * zc_nvlist_dst property nvlist
2295 * zc_nvlist_dst_size size of property nvlist
2296 */
2297 static int
zfs_ioc_dataset_list_next(zfs_cmd_t * zc)2298 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2299 {
2300 objset_t *os;
2301 int error;
2302 char *p;
2303 size_t orig_len = strlen(zc->zc_name);
2304
2305 top:
2306 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2307 if (error == ENOENT)
2308 error = SET_ERROR(ESRCH);
2309 return (error);
2310 }
2311
2312 p = strrchr(zc->zc_name, '/');
2313 if (p == NULL || p[1] != '\0')
2314 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2315 p = zc->zc_name + strlen(zc->zc_name);
2316
2317 do {
2318 error = dmu_dir_list_next(os,
2319 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2320 NULL, &zc->zc_cookie);
2321 if (error == ENOENT)
2322 error = SET_ERROR(ESRCH);
2323 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2324 dmu_objset_rele(os, FTAG);
2325
2326 /*
2327 * If it's an internal dataset (ie. with a '$' in its name),
2328 * don't try to get stats for it, otherwise we'll return ENOENT.
2329 */
2330 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2331 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2332 if (error == ENOENT) {
2333 /* We lost a race with destroy, get the next one. */
2334 zc->zc_name[orig_len] = '\0';
2335 goto top;
2336 }
2337 }
2338 return (error);
2339 }
2340
2341 /*
2342 * inputs:
2343 * zc_name name of filesystem
2344 * zc_cookie zap cursor
2345 * zc_nvlist_src iteration range nvlist
2346 * zc_nvlist_src_size size of iteration range nvlist
2347 *
2348 * outputs:
2349 * zc_name name of next snapshot
2350 * zc_objset_stats stats
2351 * zc_nvlist_dst property nvlist
2352 * zc_nvlist_dst_size size of property nvlist
2353 */
2354 static int
zfs_ioc_snapshot_list_next(zfs_cmd_t * zc)2355 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2356 {
2357 int error;
2358 objset_t *os, *ossnap;
2359 dsl_dataset_t *ds;
2360 uint64_t min_txg = 0, max_txg = 0;
2361
2362 if (zc->zc_nvlist_src_size != 0) {
2363 nvlist_t *props = NULL;
2364 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2365 zc->zc_iflags, &props);
2366 if (error != 0)
2367 return (error);
2368 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2369 &min_txg);
2370 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2371 &max_txg);
2372 nvlist_free(props);
2373 }
2374
2375 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2376 if (error != 0) {
2377 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2378 }
2379
2380 /*
2381 * A dataset name of maximum length cannot have any snapshots,
2382 * so exit immediately.
2383 */
2384 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2385 ZFS_MAX_DATASET_NAME_LEN) {
2386 dmu_objset_rele(os, FTAG);
2387 return (SET_ERROR(ESRCH));
2388 }
2389
2390 while (error == 0) {
2391 if (issig()) {
2392 error = SET_ERROR(EINTR);
2393 break;
2394 }
2395
2396 error = dmu_snapshot_list_next(os,
2397 sizeof (zc->zc_name) - strlen(zc->zc_name),
2398 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2399 &zc->zc_cookie, NULL);
2400 if (error == ENOENT) {
2401 error = SET_ERROR(ESRCH);
2402 break;
2403 } else if (error != 0) {
2404 break;
2405 }
2406
2407 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2408 FTAG, &ds);
2409 if (error != 0)
2410 break;
2411
2412 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2413 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2414 dsl_dataset_rele(ds, FTAG);
2415 /* undo snapshot name append */
2416 *(strchr(zc->zc_name, '@') + 1) = '\0';
2417 /* skip snapshot */
2418 continue;
2419 }
2420
2421 if (zc->zc_simple) {
2422 dsl_dataset_fast_stat(ds, &zc->zc_objset_stats);
2423 dsl_dataset_rele(ds, FTAG);
2424 break;
2425 }
2426
2427 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2428 dsl_dataset_rele(ds, FTAG);
2429 break;
2430 }
2431 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2432 dsl_dataset_rele(ds, FTAG);
2433 break;
2434 }
2435 dsl_dataset_rele(ds, FTAG);
2436 break;
2437 }
2438
2439 dmu_objset_rele(os, FTAG);
2440 /* if we failed, undo the @ that we tacked on to zc_name */
2441 if (error != 0)
2442 *strchr(zc->zc_name, '@') = '\0';
2443 return (error);
2444 }
2445
2446 static int
zfs_prop_set_userquota(const char * dsname,nvpair_t * pair)2447 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2448 {
2449 const char *propname = nvpair_name(pair);
2450 uint64_t *valary;
2451 unsigned int vallen;
2452 const char *dash, *domain;
2453 zfs_userquota_prop_t type;
2454 uint64_t rid;
2455 uint64_t quota;
2456 zfsvfs_t *zfsvfs;
2457 int err;
2458
2459 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2460 nvlist_t *attrs;
2461 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2462 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2463 &pair) != 0)
2464 return (SET_ERROR(EINVAL));
2465 }
2466
2467 /*
2468 * A correctly constructed propname is encoded as
2469 * userquota@<rid>-<domain>.
2470 */
2471 if ((dash = strchr(propname, '-')) == NULL ||
2472 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2473 vallen != 3)
2474 return (SET_ERROR(EINVAL));
2475
2476 domain = dash + 1;
2477 type = valary[0];
2478 rid = valary[1];
2479 quota = valary[2];
2480
2481 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2482 if (err == 0) {
2483 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2484 zfsvfs_rele(zfsvfs, FTAG);
2485 }
2486
2487 return (err);
2488 }
2489
2490 /*
2491 * If the named property is one that has a special function to set its value,
2492 * return 0 on success and a positive error code on failure; otherwise if it is
2493 * not one of the special properties handled by this function, return -1.
2494 *
2495 * XXX: It would be better for callers of the property interface if we handled
2496 * these special cases in dsl_prop.c (in the dsl layer).
2497 */
2498 static int
zfs_prop_set_special(const char * dsname,zprop_source_t source,nvpair_t * pair)2499 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2500 nvpair_t *pair)
2501 {
2502 const char *propname = nvpair_name(pair);
2503 zfs_prop_t prop = zfs_name_to_prop(propname);
2504 uint64_t intval = 0;
2505 const char *strval = NULL;
2506 int err = -1;
2507
2508 if (prop == ZPROP_USERPROP) {
2509 if (zfs_prop_userquota(propname))
2510 return (zfs_prop_set_userquota(dsname, pair));
2511 return (-1);
2512 }
2513
2514 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2515 nvlist_t *attrs;
2516 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2517 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2518 &pair) == 0);
2519 }
2520
2521 /* all special properties are numeric except for keylocation */
2522 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2523 strval = fnvpair_value_string(pair);
2524 } else {
2525 intval = fnvpair_value_uint64(pair);
2526 }
2527
2528 switch (prop) {
2529 case ZFS_PROP_QUOTA:
2530 err = dsl_dir_set_quota(dsname, source, intval);
2531 break;
2532 case ZFS_PROP_REFQUOTA:
2533 err = dsl_dataset_set_refquota(dsname, source, intval);
2534 break;
2535 case ZFS_PROP_FILESYSTEM_LIMIT:
2536 case ZFS_PROP_SNAPSHOT_LIMIT:
2537 if (intval == UINT64_MAX) {
2538 /* clearing the limit, just do it */
2539 err = 0;
2540 } else {
2541 err = dsl_dir_activate_fs_ss_limit(dsname);
2542 }
2543 /*
2544 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2545 * default path to set the value in the nvlist.
2546 */
2547 if (err == 0)
2548 err = -1;
2549 break;
2550 case ZFS_PROP_KEYLOCATION:
2551 err = dsl_crypto_can_set_keylocation(dsname, strval);
2552
2553 /*
2554 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2555 * default path to set the value in the nvlist.
2556 */
2557 if (err == 0)
2558 err = -1;
2559 break;
2560 case ZFS_PROP_RESERVATION:
2561 err = dsl_dir_set_reservation(dsname, source, intval);
2562 break;
2563 case ZFS_PROP_REFRESERVATION:
2564 err = dsl_dataset_set_refreservation(dsname, source, intval);
2565 break;
2566 case ZFS_PROP_COMPRESSION:
2567 err = dsl_dataset_set_compression(dsname, source, intval);
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_VOLSIZE:
2576 err = zvol_set_volsize(dsname, intval);
2577 break;
2578 case ZFS_PROP_VOLTHREADING:
2579 err = zvol_set_volthreading(dsname, intval);
2580 /*
2581 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2582 * default path to set the value in the nvlist.
2583 */
2584 if (err == 0)
2585 err = -1;
2586 break;
2587 case ZFS_PROP_SNAPDEV:
2588 case ZFS_PROP_VOLMODE:
2589 err = zvol_set_common(dsname, prop, source, intval);
2590 break;
2591 case ZFS_PROP_READONLY:
2592 err = zvol_set_ro(dsname, 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_VERSION:
2601 {
2602 zfsvfs_t *zfsvfs;
2603
2604 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2605 break;
2606
2607 err = zfs_set_version(zfsvfs, intval);
2608 zfsvfs_rele(zfsvfs, FTAG);
2609
2610 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2611 zfs_cmd_t *zc;
2612
2613 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2614 (void) strlcpy(zc->zc_name, dsname,
2615 sizeof (zc->zc_name));
2616 (void) zfs_ioc_userspace_upgrade(zc);
2617 (void) zfs_ioc_id_quota_upgrade(zc);
2618 kmem_free(zc, sizeof (zfs_cmd_t));
2619 }
2620 break;
2621 }
2622 case ZFS_PROP_LONGNAME:
2623 {
2624 zfsvfs_t *zfsvfs;
2625
2626 /*
2627 * Ignore the checks if the property is being applied as part of
2628 * 'zfs receive'. Because, we already check if the local pool
2629 * has SPA_FEATURE_LONGNAME enabled in dmu_recv_begin_check().
2630 */
2631 if (source == ZPROP_SRC_RECEIVED) {
2632 cmn_err(CE_NOTE, "Skipping ZFS_PROP_LONGNAME checks "
2633 "for dsname=%s\n", dsname);
2634 err = -1;
2635 break;
2636 }
2637
2638 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE)) != 0) {
2639 cmn_err(CE_WARN, "%s:%d Failed to hold for dsname=%s "
2640 "err=%d\n", __FILE__, __LINE__, dsname, err);
2641 break;
2642 }
2643
2644 if (!spa_feature_is_enabled(zfsvfs->z_os->os_spa,
2645 SPA_FEATURE_LONGNAME)) {
2646 err = ENOTSUP;
2647 } else {
2648 /*
2649 * Set err to -1 to force the zfs_set_prop_nvlist code
2650 * down the default path to set the value in the nvlist.
2651 */
2652 err = -1;
2653 }
2654 zfsvfs_rele(zfsvfs, FTAG);
2655 break;
2656 }
2657 case ZFS_PROP_DEFAULTUSERQUOTA:
2658 case ZFS_PROP_DEFAULTGROUPQUOTA:
2659 case ZFS_PROP_DEFAULTPROJECTQUOTA:
2660 case ZFS_PROP_DEFAULTUSEROBJQUOTA:
2661 case ZFS_PROP_DEFAULTGROUPOBJQUOTA:
2662 case ZFS_PROP_DEFAULTPROJECTOBJQUOTA:
2663 {
2664 zfsvfs_t *zfsvfs;
2665 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2666 break;
2667 err = zfs_set_default_quota(zfsvfs, prop, intval);
2668 zfsvfs_rele(zfsvfs, FTAG);
2669 break;
2670 }
2671 default:
2672 err = -1;
2673 }
2674
2675 return (err);
2676 }
2677
2678 static boolean_t
zfs_is_namespace_prop(zfs_prop_t prop)2679 zfs_is_namespace_prop(zfs_prop_t prop)
2680 {
2681 switch (prop) {
2682
2683 case ZFS_PROP_ATIME:
2684 case ZFS_PROP_RELATIME:
2685 case ZFS_PROP_DEVICES:
2686 case ZFS_PROP_EXEC:
2687 case ZFS_PROP_SETUID:
2688 case ZFS_PROP_READONLY:
2689 case ZFS_PROP_XATTR:
2690 case ZFS_PROP_NBMAND:
2691 return (B_TRUE);
2692
2693 default:
2694 return (B_FALSE);
2695 }
2696 }
2697
2698 /*
2699 * This function is best effort. If it fails to set any of the given properties,
2700 * it continues to set as many as it can and returns the last error
2701 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2702 * with the list of names of all the properties that failed along with the
2703 * corresponding error numbers.
2704 *
2705 * If every property is set successfully, zero is returned and errlist is not
2706 * modified.
2707 */
2708 int
zfs_set_prop_nvlist(const char * dsname,zprop_source_t source,nvlist_t * nvl,nvlist_t * errlist)2709 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2710 nvlist_t *errlist)
2711 {
2712 nvpair_t *pair;
2713 nvpair_t *propval;
2714 int rv = 0;
2715 int err;
2716 uint64_t intval;
2717 const char *strval;
2718 boolean_t should_update_mount_cache = B_FALSE;
2719
2720 nvlist_t *genericnvl = fnvlist_alloc();
2721 nvlist_t *retrynvl = fnvlist_alloc();
2722 retry:
2723 pair = NULL;
2724 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2725 const char *propname = nvpair_name(pair);
2726 zfs_prop_t prop = zfs_name_to_prop(propname);
2727 err = 0;
2728
2729 /* decode the property value */
2730 propval = pair;
2731 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2732 nvlist_t *attrs;
2733 attrs = fnvpair_value_nvlist(pair);
2734 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2735 &propval) != 0)
2736 err = SET_ERROR(EINVAL);
2737 }
2738
2739 /* Validate value type */
2740 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2741 /* inherited properties are expected to be booleans */
2742 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2743 err = SET_ERROR(EINVAL);
2744 } else if (err == 0 && prop == ZPROP_USERPROP) {
2745 if (zfs_prop_user(propname)) {
2746 if (nvpair_type(propval) != DATA_TYPE_STRING)
2747 err = SET_ERROR(EINVAL);
2748 } else if (zfs_prop_userquota(propname)) {
2749 if (nvpair_type(propval) !=
2750 DATA_TYPE_UINT64_ARRAY)
2751 err = SET_ERROR(EINVAL);
2752 } else {
2753 err = SET_ERROR(EINVAL);
2754 }
2755 } else if (err == 0) {
2756 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2757 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2758 err = SET_ERROR(EINVAL);
2759 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2760 const char *unused;
2761
2762 intval = fnvpair_value_uint64(propval);
2763
2764 switch (zfs_prop_get_type(prop)) {
2765 case PROP_TYPE_NUMBER:
2766 break;
2767 case PROP_TYPE_STRING:
2768 err = SET_ERROR(EINVAL);
2769 break;
2770 case PROP_TYPE_INDEX:
2771 if (zfs_prop_index_to_string(prop,
2772 intval, &unused) != 0)
2773 err =
2774 SET_ERROR(ZFS_ERR_BADPROP);
2775 break;
2776 default:
2777 cmn_err(CE_PANIC,
2778 "unknown property type");
2779 }
2780 } else {
2781 err = SET_ERROR(EINVAL);
2782 }
2783 }
2784
2785 /* Validate permissions */
2786 if (err == 0)
2787 err = zfs_check_settable(dsname, pair, CRED());
2788
2789 if (err == 0) {
2790 if (source == ZPROP_SRC_INHERITED)
2791 err = -1; /* does not need special handling */
2792 else
2793 err = zfs_prop_set_special(dsname, source,
2794 pair);
2795 if (err == -1) {
2796 /*
2797 * For better performance we build up a list of
2798 * properties to set in a single transaction.
2799 */
2800 err = nvlist_add_nvpair(genericnvl, pair);
2801 } else if (err != 0 && nvl != retrynvl) {
2802 /*
2803 * This may be a spurious error caused by
2804 * receiving quota and reservation out of order.
2805 * Try again in a second pass.
2806 */
2807 err = nvlist_add_nvpair(retrynvl, pair);
2808 }
2809 }
2810
2811 if (err != 0) {
2812 if (errlist != NULL)
2813 fnvlist_add_int32(errlist, propname, err);
2814 rv = err;
2815 }
2816
2817 if (zfs_is_namespace_prop(prop))
2818 should_update_mount_cache = B_TRUE;
2819 }
2820
2821 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2822 nvl = retrynvl;
2823 goto retry;
2824 }
2825
2826 if (nvlist_empty(genericnvl))
2827 goto out;
2828
2829 /*
2830 * Try to set them all in one batch.
2831 */
2832 err = dsl_props_set(dsname, source, genericnvl);
2833 if (err == 0)
2834 goto out;
2835
2836 /*
2837 * If batching fails, we still want to set as many properties as we
2838 * can, so try setting them individually.
2839 */
2840 pair = NULL;
2841 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2842 const char *propname = nvpair_name(pair);
2843
2844 propval = pair;
2845 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2846 nvlist_t *attrs;
2847 attrs = fnvpair_value_nvlist(pair);
2848 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
2849 }
2850
2851 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2852 strval = fnvpair_value_string(propval);
2853 err = dsl_prop_set_string(dsname, propname,
2854 source, strval);
2855 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2856 err = dsl_prop_inherit(dsname, propname, source);
2857 } else {
2858 intval = fnvpair_value_uint64(propval);
2859 err = dsl_prop_set_int(dsname, propname, source,
2860 intval);
2861 }
2862
2863 if (err != 0) {
2864 if (errlist != NULL) {
2865 fnvlist_add_int32(errlist, propname, err);
2866 }
2867 rv = err;
2868 }
2869 }
2870
2871 out:
2872 if (should_update_mount_cache)
2873 zfs_ioctl_update_mount_cache(dsname);
2874
2875 nvlist_free(genericnvl);
2876 nvlist_free(retrynvl);
2877
2878 return (rv);
2879 }
2880
2881 /*
2882 * Check that all the properties are valid user properties.
2883 */
2884 static int
zfs_check_userprops(nvlist_t * nvl)2885 zfs_check_userprops(nvlist_t *nvl)
2886 {
2887 nvpair_t *pair = NULL;
2888
2889 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2890 const char *propname = nvpair_name(pair);
2891
2892 if (!zfs_prop_user(propname) ||
2893 nvpair_type(pair) != DATA_TYPE_STRING)
2894 return (SET_ERROR(EINVAL));
2895
2896 if (strlen(propname) >= ZAP_MAXNAMELEN)
2897 return (SET_ERROR(ENAMETOOLONG));
2898
2899 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2900 return (SET_ERROR(E2BIG));
2901 }
2902 return (0);
2903 }
2904
2905 static void
props_skip(nvlist_t * props,nvlist_t * skipped,nvlist_t ** newprops)2906 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2907 {
2908 nvpair_t *pair;
2909
2910 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2911
2912 pair = NULL;
2913 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2914 if (nvlist_exists(skipped, nvpair_name(pair)))
2915 continue;
2916
2917 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2918 }
2919 }
2920
2921 static int
clear_received_props(const char * dsname,nvlist_t * props,nvlist_t * skipped)2922 clear_received_props(const char *dsname, nvlist_t *props,
2923 nvlist_t *skipped)
2924 {
2925 int err = 0;
2926 nvlist_t *cleared_props = NULL;
2927 props_skip(props, skipped, &cleared_props);
2928 if (!nvlist_empty(cleared_props)) {
2929 /*
2930 * Acts on local properties until the dataset has received
2931 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2932 */
2933 zprop_source_t flags = (ZPROP_SRC_NONE |
2934 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2935 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2936 }
2937 nvlist_free(cleared_props);
2938 return (err);
2939 }
2940
2941 /*
2942 * inputs:
2943 * zc_name name of filesystem
2944 * zc_value name of property to set
2945 * zc_nvlist_src{_size} nvlist of properties to apply
2946 * zc_cookie received properties flag
2947 *
2948 * outputs:
2949 * zc_nvlist_dst{_size} error for each unapplied received property
2950 */
2951 static int
zfs_ioc_set_prop(zfs_cmd_t * zc)2952 zfs_ioc_set_prop(zfs_cmd_t *zc)
2953 {
2954 nvlist_t *nvl;
2955 boolean_t received = zc->zc_cookie;
2956 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2957 ZPROP_SRC_LOCAL);
2958 nvlist_t *errors;
2959 int error;
2960
2961 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2962 zc->zc_iflags, &nvl)) != 0)
2963 return (error);
2964
2965 if (received) {
2966 nvlist_t *origprops;
2967
2968 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2969 (void) clear_received_props(zc->zc_name,
2970 origprops, nvl);
2971 nvlist_free(origprops);
2972 }
2973
2974 error = dsl_prop_set_hasrecvd(zc->zc_name);
2975 }
2976
2977 errors = fnvlist_alloc();
2978 if (error == 0)
2979 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2980
2981 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2982 (void) put_nvlist(zc, errors);
2983 }
2984
2985 nvlist_free(errors);
2986 nvlist_free(nvl);
2987 return (error);
2988 }
2989
2990 /*
2991 * inputs:
2992 * zc_name name of filesystem
2993 * zc_value name of property to inherit
2994 * zc_cookie revert to received value if TRUE
2995 *
2996 * outputs: none
2997 */
2998 static int
zfs_ioc_inherit_prop(zfs_cmd_t * zc)2999 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
3000 {
3001 const char *propname = zc->zc_value;
3002 zfs_prop_t prop = zfs_name_to_prop(propname);
3003 boolean_t received = zc->zc_cookie;
3004 zprop_source_t source = (received
3005 ? ZPROP_SRC_NONE /* revert to received value, if any */
3006 : ZPROP_SRC_INHERITED); /* explicitly inherit */
3007 nvlist_t *dummy;
3008 nvpair_t *pair;
3009 zprop_type_t type;
3010 int err;
3011
3012 if (!received) {
3013 /*
3014 * Only check this in the non-received case. We want to allow
3015 * 'inherit -S' to revert non-inheritable properties like quota
3016 * and reservation to the received or default values even though
3017 * they are not considered inheritable.
3018 */
3019 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
3020 return (SET_ERROR(EINVAL));
3021 }
3022
3023 if (prop == ZPROP_USERPROP) {
3024 if (!zfs_prop_user(propname))
3025 return (SET_ERROR(EINVAL));
3026
3027 type = PROP_TYPE_STRING;
3028 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
3029 return (SET_ERROR(EINVAL));
3030 } else {
3031 type = zfs_prop_get_type(prop);
3032 }
3033
3034 /*
3035 * zfs_prop_set_special() expects properties in the form of an
3036 * nvpair with type info.
3037 */
3038 dummy = fnvlist_alloc();
3039
3040 switch (type) {
3041 case PROP_TYPE_STRING:
3042 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
3043 break;
3044 case PROP_TYPE_NUMBER:
3045 case PROP_TYPE_INDEX:
3046 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
3047 break;
3048 default:
3049 err = SET_ERROR(EINVAL);
3050 goto errout;
3051 }
3052
3053 pair = nvlist_next_nvpair(dummy, NULL);
3054 if (pair == NULL) {
3055 err = SET_ERROR(EINVAL);
3056 } else {
3057 err = zfs_prop_set_special(zc->zc_name, source, pair);
3058 if (err == -1) /* property is not "special", needs handling */
3059 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
3060 source);
3061 }
3062
3063 errout:
3064 nvlist_free(dummy);
3065 return (err);
3066 }
3067
3068 static int
zfs_ioc_pool_set_props(zfs_cmd_t * zc)3069 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
3070 {
3071 nvlist_t *props;
3072 spa_t *spa;
3073 int error;
3074 nvpair_t *pair;
3075
3076 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3077 zc->zc_iflags, &props)))
3078 return (error);
3079
3080 /*
3081 * If the only property is the configfile, then just do a spa_lookup()
3082 * to handle the faulted case.
3083 */
3084 pair = nvlist_next_nvpair(props, NULL);
3085 if (pair != NULL && strcmp(nvpair_name(pair),
3086 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
3087 nvlist_next_nvpair(props, pair) == NULL) {
3088 mutex_enter(&spa_namespace_lock);
3089 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
3090 spa_configfile_set(spa, props, B_FALSE);
3091 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
3092 }
3093 mutex_exit(&spa_namespace_lock);
3094 if (spa != NULL) {
3095 nvlist_free(props);
3096 return (0);
3097 }
3098 }
3099
3100 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3101 nvlist_free(props);
3102 return (error);
3103 }
3104
3105 error = spa_prop_set(spa, props);
3106
3107 nvlist_free(props);
3108 spa_close(spa, FTAG);
3109
3110 return (error);
3111 }
3112
3113 /*
3114 * innvl: {
3115 * "get_props_names": [ "prop1", "prop2", ..., "propN" ]
3116 * }
3117 */
3118
3119 static const zfs_ioc_key_t zfs_keys_get_props[] = {
3120 { ZPOOL_GET_PROPS_NAMES, DATA_TYPE_STRING_ARRAY, ZK_OPTIONAL },
3121 };
3122
3123 static int
zfs_ioc_pool_get_props(const char * pool,nvlist_t * innvl,nvlist_t * outnvl)3124 zfs_ioc_pool_get_props(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
3125 {
3126 spa_t *spa;
3127 char **props = NULL;
3128 unsigned int n_props = 0;
3129 int error;
3130
3131 if (nvlist_lookup_string_array(innvl, ZPOOL_GET_PROPS_NAMES,
3132 &props, &n_props) != 0) {
3133 props = NULL;
3134 }
3135
3136 if ((error = spa_open(pool, &spa, FTAG)) != 0) {
3137 /*
3138 * If the pool is faulted, there may be properties we can still
3139 * get (such as altroot and cachefile), so attempt to get them
3140 * anyway.
3141 */
3142 mutex_enter(&spa_namespace_lock);
3143 if ((spa = spa_lookup(pool)) != NULL) {
3144 error = spa_prop_get(spa, outnvl);
3145 if (error == 0 && props != NULL)
3146 error = spa_prop_get_nvlist(spa, props, n_props,
3147 outnvl);
3148 }
3149 mutex_exit(&spa_namespace_lock);
3150 } else {
3151 error = spa_prop_get(spa, outnvl);
3152 if (error == 0 && props != NULL)
3153 error = spa_prop_get_nvlist(spa, props, n_props,
3154 outnvl);
3155 spa_close(spa, FTAG);
3156 }
3157
3158 return (error);
3159 }
3160
3161 /*
3162 * innvl: {
3163 * "vdevprops_set_vdev" -> guid
3164 * "vdevprops_set_props" -> { prop -> value }
3165 * }
3166 *
3167 * outnvl: propname -> error code (int32)
3168 */
3169 static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = {
3170 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0},
3171 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0}
3172 };
3173
3174 static int
zfs_ioc_vdev_set_props(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3175 zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3176 {
3177 spa_t *spa;
3178 int error;
3179 vdev_t *vd;
3180 uint64_t vdev_guid;
3181
3182 /* Early validation */
3183 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
3184 &vdev_guid) != 0)
3185 return (SET_ERROR(EINVAL));
3186
3187 if (outnvl == NULL)
3188 return (SET_ERROR(EINVAL));
3189
3190 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3191 return (error);
3192
3193 ASSERT(spa_writeable(spa));
3194
3195 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3196 spa_close(spa, FTAG);
3197 return (SET_ERROR(ENOENT));
3198 }
3199
3200 error = vdev_prop_set(vd, innvl, outnvl);
3201
3202 spa_close(spa, FTAG);
3203
3204 return (error);
3205 }
3206
3207 /*
3208 * innvl: {
3209 * "vdevprops_get_vdev" -> guid
3210 * (optional) "vdevprops_get_props" -> { propname -> propid }
3211 * }
3212 *
3213 * outnvl: propname -> value
3214 */
3215 static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = {
3216 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0},
3217 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL}
3218 };
3219
3220 static int
zfs_ioc_vdev_get_props(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3221 zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3222 {
3223 spa_t *spa;
3224 int error;
3225 vdev_t *vd;
3226 uint64_t vdev_guid;
3227
3228 /* Early validation */
3229 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
3230 &vdev_guid) != 0)
3231 return (SET_ERROR(EINVAL));
3232
3233 if (outnvl == NULL)
3234 return (SET_ERROR(EINVAL));
3235
3236 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3237 return (error);
3238
3239 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3240 spa_close(spa, FTAG);
3241 return (SET_ERROR(ENOENT));
3242 }
3243
3244 error = vdev_prop_get(vd, innvl, outnvl);
3245
3246 spa_close(spa, FTAG);
3247
3248 return (error);
3249 }
3250
3251 /*
3252 * inputs:
3253 * zc_name name of filesystem
3254 * zc_nvlist_src{_size} nvlist of delegated permissions
3255 * zc_perm_action allow/unallow flag
3256 *
3257 * outputs: none
3258 */
3259 static int
zfs_ioc_set_fsacl(zfs_cmd_t * zc)3260 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3261 {
3262 int error;
3263 nvlist_t *fsaclnv = NULL;
3264
3265 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3266 zc->zc_iflags, &fsaclnv)) != 0)
3267 return (error);
3268
3269 /*
3270 * Verify nvlist is constructed correctly
3271 */
3272 if (zfs_deleg_verify_nvlist(fsaclnv) != 0) {
3273 nvlist_free(fsaclnv);
3274 return (SET_ERROR(EINVAL));
3275 }
3276
3277 /*
3278 * If we don't have PRIV_SYS_MOUNT, then validate
3279 * that user is allowed to hand out each permission in
3280 * the nvlist(s)
3281 */
3282
3283 error = secpolicy_zfs(CRED());
3284 if (error != 0) {
3285 if (zc->zc_perm_action == B_FALSE) {
3286 error = dsl_deleg_can_allow(zc->zc_name,
3287 fsaclnv, CRED());
3288 } else {
3289 error = dsl_deleg_can_unallow(zc->zc_name,
3290 fsaclnv, CRED());
3291 }
3292 }
3293
3294 if (error == 0)
3295 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3296
3297 nvlist_free(fsaclnv);
3298 return (error);
3299 }
3300
3301 /*
3302 * inputs:
3303 * zc_name name of filesystem
3304 *
3305 * outputs:
3306 * zc_nvlist_src{_size} nvlist of delegated permissions
3307 */
3308 static int
zfs_ioc_get_fsacl(zfs_cmd_t * zc)3309 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3310 {
3311 nvlist_t *nvp;
3312 int error;
3313
3314 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3315 error = put_nvlist(zc, nvp);
3316 nvlist_free(nvp);
3317 }
3318
3319 return (error);
3320 }
3321
3322 static void
zfs_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)3323 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3324 {
3325 zfs_creat_t *zct = arg;
3326
3327 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3328 }
3329
3330 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3331
3332 /*
3333 * inputs:
3334 * os parent objset pointer (NULL if root fs)
3335 * fuids_ok fuids allowed in this version of the spa?
3336 * sa_ok SAs allowed in this version of the spa?
3337 * createprops list of properties requested by creator
3338 *
3339 * outputs:
3340 * zplprops values for the zplprops we attach to the master node object
3341 * is_ci true if requested file system will be purely case-insensitive
3342 *
3343 * Determine the settings for utf8only, normalization and
3344 * casesensitivity. Specific values may have been requested by the
3345 * creator and/or we can inherit values from the parent dataset. If
3346 * the file system is of too early a vintage, a creator can not
3347 * request settings for these properties, even if the requested
3348 * setting is the default value. We don't actually want to create dsl
3349 * properties for these, so remove them from the source nvlist after
3350 * processing.
3351 */
3352 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)3353 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3354 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3355 nvlist_t *zplprops, boolean_t *is_ci)
3356 {
3357 uint64_t sense = ZFS_PROP_UNDEFINED;
3358 uint64_t norm = ZFS_PROP_UNDEFINED;
3359 uint64_t u8 = ZFS_PROP_UNDEFINED;
3360 uint64_t duq = ZFS_PROP_UNDEFINED, duoq = ZFS_PROP_UNDEFINED;
3361 uint64_t dgq = ZFS_PROP_UNDEFINED, dgoq = ZFS_PROP_UNDEFINED;
3362 uint64_t dpq = ZFS_PROP_UNDEFINED, dpoq = ZFS_PROP_UNDEFINED;
3363 int error;
3364
3365 ASSERT(zplprops != NULL);
3366
3367 /* parent dataset must be a filesystem */
3368 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3369 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3370
3371 /*
3372 * Pull out creator prop choices, if any.
3373 */
3374 if (createprops) {
3375 (void) nvlist_lookup_uint64(createprops,
3376 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3377 (void) nvlist_lookup_uint64(createprops,
3378 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3379 (void) nvlist_remove_all(createprops,
3380 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3381 (void) nvlist_lookup_uint64(createprops,
3382 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3383 (void) nvlist_remove_all(createprops,
3384 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3385 (void) nvlist_lookup_uint64(createprops,
3386 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3387 (void) nvlist_remove_all(createprops,
3388 zfs_prop_to_name(ZFS_PROP_CASE));
3389 (void) nvlist_lookup_uint64(createprops,
3390 zfs_prop_to_name(ZFS_PROP_DEFAULTUSERQUOTA), &duq);
3391 (void) nvlist_remove_all(createprops,
3392 zfs_prop_to_name(ZFS_PROP_DEFAULTUSERQUOTA));
3393 (void) nvlist_lookup_uint64(createprops,
3394 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPQUOTA), &dgq);
3395 (void) nvlist_remove_all(createprops,
3396 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPQUOTA));
3397 (void) nvlist_lookup_uint64(createprops,
3398 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTQUOTA), &dpq);
3399 (void) nvlist_remove_all(createprops,
3400 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTQUOTA));
3401 (void) nvlist_lookup_uint64(createprops,
3402 zfs_prop_to_name(ZFS_PROP_DEFAULTUSEROBJQUOTA), &duoq);
3403 (void) nvlist_remove_all(createprops,
3404 zfs_prop_to_name(ZFS_PROP_DEFAULTUSEROBJQUOTA));
3405 (void) nvlist_lookup_uint64(createprops,
3406 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPOBJQUOTA), &dgoq);
3407 (void) nvlist_remove_all(createprops,
3408 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPOBJQUOTA));
3409 (void) nvlist_lookup_uint64(createprops,
3410 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTOBJQUOTA), &dpoq);
3411 (void) nvlist_remove_all(createprops,
3412 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTOBJQUOTA));
3413 }
3414
3415 /*
3416 * If the zpl version requested is whacky or the file system
3417 * or pool is version is too "young" to support normalization
3418 * and the creator tried to set a value for one of the props,
3419 * error out.
3420 */
3421 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3422 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3423 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3424 (zplver < ZPL_VERSION_NORMALIZATION &&
3425 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3426 sense != ZFS_PROP_UNDEFINED)))
3427 return (SET_ERROR(ENOTSUP));
3428
3429 /*
3430 * Put the version in the zplprops
3431 */
3432 VERIFY(nvlist_add_uint64(zplprops,
3433 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3434
3435 if (norm == ZFS_PROP_UNDEFINED &&
3436 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3437 return (error);
3438 VERIFY(nvlist_add_uint64(zplprops,
3439 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3440
3441 /*
3442 * If we're normalizing, names must always be valid UTF-8 strings.
3443 */
3444 if (norm)
3445 u8 = 1;
3446 if (u8 == ZFS_PROP_UNDEFINED &&
3447 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3448 return (error);
3449 VERIFY(nvlist_add_uint64(zplprops,
3450 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3451
3452 if (sense == ZFS_PROP_UNDEFINED &&
3453 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3454 return (error);
3455 VERIFY(nvlist_add_uint64(zplprops,
3456 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3457
3458 if (duq == ZFS_PROP_UNDEFINED &&
3459 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTUSERQUOTA, &duq)) != 0)
3460 return (error);
3461 VERIFY(nvlist_add_uint64(zplprops,
3462 zfs_prop_to_name(ZFS_PROP_DEFAULTUSERQUOTA), duq) == 0);
3463
3464 if (dgq == ZFS_PROP_UNDEFINED &&
3465 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTGROUPQUOTA,
3466 &dgq)) != 0)
3467 return (error);
3468 VERIFY(nvlist_add_uint64(zplprops,
3469 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPQUOTA), dgq) == 0);
3470
3471 if (dpq == ZFS_PROP_UNDEFINED &&
3472 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTPROJECTQUOTA,
3473 &dpq)) != 0)
3474 return (error);
3475 VERIFY(nvlist_add_uint64(zplprops,
3476 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTQUOTA), dpq) == 0);
3477
3478 if (duoq == ZFS_PROP_UNDEFINED &&
3479 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTUSEROBJQUOTA,
3480 &duoq)) != 0)
3481 return (error);
3482 VERIFY(nvlist_add_uint64(zplprops,
3483 zfs_prop_to_name(ZFS_PROP_DEFAULTUSEROBJQUOTA), duoq) == 0);
3484
3485 if (dgoq == ZFS_PROP_UNDEFINED &&
3486 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTGROUPOBJQUOTA,
3487 &dgoq)) != 0)
3488 return (error);
3489 VERIFY(nvlist_add_uint64(zplprops,
3490 zfs_prop_to_name(ZFS_PROP_DEFAULTGROUPOBJQUOTA), dgoq) == 0);
3491
3492 if (dpoq == ZFS_PROP_UNDEFINED &&
3493 (error = zfs_get_zplprop(os, ZFS_PROP_DEFAULTPROJECTOBJQUOTA,
3494 &dpoq)) != 0)
3495 return (error);
3496 VERIFY(nvlist_add_uint64(zplprops,
3497 zfs_prop_to_name(ZFS_PROP_DEFAULTPROJECTOBJQUOTA), dpoq) == 0);
3498
3499 if (is_ci)
3500 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3501
3502 return (0);
3503 }
3504
3505 static int
zfs_fill_zplprops(const char * dataset,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)3506 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3507 nvlist_t *zplprops, boolean_t *is_ci)
3508 {
3509 boolean_t fuids_ok, sa_ok;
3510 uint64_t zplver = ZPL_VERSION;
3511 objset_t *os = NULL;
3512 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3513 spa_t *spa;
3514 uint64_t spa_vers;
3515 int error;
3516
3517 zfs_get_parent(dataset, parentname, sizeof (parentname));
3518
3519 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3520 return (error);
3521
3522 spa_vers = spa_version(spa);
3523 spa_close(spa, FTAG);
3524
3525 zplver = zfs_zpl_version_map(spa_vers);
3526 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3527 sa_ok = (zplver >= ZPL_VERSION_SA);
3528
3529 /*
3530 * Open parent object set so we can inherit zplprop values.
3531 */
3532 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3533 return (error);
3534
3535 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3536 zplprops, is_ci);
3537 dmu_objset_rele(os, FTAG);
3538 return (error);
3539 }
3540
3541 static int
zfs_fill_zplprops_root(uint64_t spa_vers,nvlist_t * createprops,nvlist_t * zplprops,boolean_t * is_ci)3542 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3543 nvlist_t *zplprops, boolean_t *is_ci)
3544 {
3545 boolean_t fuids_ok;
3546 boolean_t sa_ok;
3547 uint64_t zplver = ZPL_VERSION;
3548 int error;
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 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3555 createprops, zplprops, is_ci);
3556 return (error);
3557 }
3558
3559 /*
3560 * innvl: {
3561 * "type" -> dmu_objset_type_t (int32)
3562 * (optional) "props" -> { prop -> value }
3563 * (optional) "hidden_args" -> { "wkeydata" -> value }
3564 * raw uint8_t array of encryption wrapping key data (32 bytes)
3565 * }
3566 *
3567 * outnvl: propname -> error code (int32)
3568 */
3569
3570 static const zfs_ioc_key_t zfs_keys_create[] = {
3571 {"type", DATA_TYPE_INT32, 0},
3572 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3573 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3574 };
3575
3576 static int
zfs_ioc_create(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3577 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3578 {
3579 int error = 0;
3580 zfs_creat_t zct = { 0 };
3581 nvlist_t *nvprops = NULL;
3582 nvlist_t *hidden_args = NULL;
3583 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3584 dmu_objset_type_t type;
3585 boolean_t is_insensitive = B_FALSE;
3586 dsl_crypto_params_t *dcp = NULL;
3587
3588 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3589 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3590 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3591
3592 switch (type) {
3593 case DMU_OST_ZFS:
3594 cbfunc = zfs_create_cb;
3595 break;
3596
3597 case DMU_OST_ZVOL:
3598 cbfunc = zvol_create_cb;
3599 break;
3600
3601 default:
3602 cbfunc = NULL;
3603 break;
3604 }
3605 if (strchr(fsname, '@') ||
3606 strchr(fsname, '%'))
3607 return (SET_ERROR(EINVAL));
3608
3609 zct.zct_props = nvprops;
3610
3611 if (cbfunc == NULL)
3612 return (SET_ERROR(EINVAL));
3613
3614 if (type == DMU_OST_ZVOL) {
3615 uint64_t volsize, volblocksize;
3616
3617 if (nvprops == NULL)
3618 return (SET_ERROR(EINVAL));
3619 if (nvlist_lookup_uint64(nvprops,
3620 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3621 return (SET_ERROR(EINVAL));
3622
3623 if ((error = nvlist_lookup_uint64(nvprops,
3624 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3625 &volblocksize)) != 0 && error != ENOENT)
3626 return (SET_ERROR(EINVAL));
3627
3628 if (error != 0)
3629 volblocksize = zfs_prop_default_numeric(
3630 ZFS_PROP_VOLBLOCKSIZE);
3631
3632 if ((error = zvol_check_volblocksize(fsname,
3633 volblocksize)) != 0 ||
3634 (error = zvol_check_volsize(volsize,
3635 volblocksize)) != 0)
3636 return (error);
3637 } else if (type == DMU_OST_ZFS) {
3638 int error;
3639
3640 /*
3641 * We have to have normalization and
3642 * case-folding flags correct when we do the
3643 * file system creation, so go figure them out
3644 * now.
3645 */
3646 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3647 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3648 error = zfs_fill_zplprops(fsname, nvprops,
3649 zct.zct_zplprops, &is_insensitive);
3650 if (error != 0) {
3651 nvlist_free(zct.zct_zplprops);
3652 return (error);
3653 }
3654 }
3655
3656 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3657 hidden_args, &dcp);
3658 if (error != 0) {
3659 nvlist_free(zct.zct_zplprops);
3660 return (error);
3661 }
3662
3663 error = dmu_objset_create(fsname, type,
3664 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3665
3666 nvlist_free(zct.zct_zplprops);
3667 dsl_crypto_params_free(dcp, !!error);
3668
3669 /*
3670 * It would be nice to do this atomically.
3671 */
3672 if (error == 0) {
3673 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3674 nvprops, outnvl);
3675 if (error != 0) {
3676 spa_t *spa;
3677 int error2;
3678
3679 /*
3680 * Volumes will return EBUSY and cannot be destroyed
3681 * until all asynchronous minor handling (e.g. from
3682 * setting the volmode property) has completed. Wait for
3683 * the spa_zvol_taskq to drain then retry.
3684 */
3685 error2 = dsl_destroy_head(fsname);
3686 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3687 error2 = spa_open(fsname, &spa, FTAG);
3688 if (error2 == 0) {
3689 taskq_wait(spa->spa_zvol_taskq);
3690 spa_close(spa, FTAG);
3691 }
3692 error2 = dsl_destroy_head(fsname);
3693 }
3694 }
3695 }
3696 return (error);
3697 }
3698
3699 /*
3700 * innvl: {
3701 * "origin" -> name of origin snapshot
3702 * (optional) "props" -> { prop -> value }
3703 * (optional) "hidden_args" -> { "wkeydata" -> value }
3704 * raw uint8_t array of encryption wrapping key data (32 bytes)
3705 * }
3706 *
3707 * outputs:
3708 * outnvl: propname -> error code (int32)
3709 */
3710 static const zfs_ioc_key_t zfs_keys_clone[] = {
3711 {"origin", DATA_TYPE_STRING, 0},
3712 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3713 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3714 };
3715
3716 static int
zfs_ioc_clone(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3717 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3718 {
3719 int error = 0;
3720 nvlist_t *nvprops = NULL;
3721 const char *origin_name;
3722
3723 origin_name = fnvlist_lookup_string(innvl, "origin");
3724 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3725
3726 if (strchr(fsname, '@') ||
3727 strchr(fsname, '%'))
3728 return (SET_ERROR(EINVAL));
3729
3730 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3731 return (SET_ERROR(EINVAL));
3732
3733 error = dsl_dataset_clone(fsname, origin_name);
3734
3735 /*
3736 * It would be nice to do this atomically.
3737 */
3738 if (error == 0) {
3739 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3740 nvprops, outnvl);
3741 if (error != 0)
3742 (void) dsl_destroy_head(fsname);
3743 }
3744 return (error);
3745 }
3746
3747 static const zfs_ioc_key_t zfs_keys_remap[] = {
3748 /* no nvl keys */
3749 };
3750
3751 static int
zfs_ioc_remap(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)3752 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3753 {
3754 /* This IOCTL is no longer supported. */
3755 (void) fsname, (void) innvl, (void) outnvl;
3756 return (0);
3757 }
3758
3759 /*
3760 * innvl: {
3761 * "snaps" -> { snapshot1, snapshot2 }
3762 * (optional) "props" -> { prop -> value (string) }
3763 * }
3764 *
3765 * outnvl: snapshot -> error code (int32)
3766 */
3767 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3768 {"snaps", DATA_TYPE_NVLIST, 0},
3769 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3770 };
3771
3772 static int
zfs_ioc_snapshot(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3773 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3774 {
3775 nvlist_t *snaps;
3776 nvlist_t *props = NULL;
3777 int error, poollen;
3778 nvpair_t *pair;
3779
3780 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3781 if (!nvlist_empty(props) &&
3782 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3783 return (SET_ERROR(ENOTSUP));
3784 if ((error = zfs_check_userprops(props)) != 0)
3785 return (error);
3786
3787 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3788 poollen = strlen(poolname);
3789 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3790 pair = nvlist_next_nvpair(snaps, pair)) {
3791 const char *name = nvpair_name(pair);
3792 char *cp = strchr(name, '@');
3793
3794 /*
3795 * The snap name must contain an @, and the part after it must
3796 * contain only valid characters.
3797 */
3798 if (cp == NULL ||
3799 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3800 return (SET_ERROR(EINVAL));
3801
3802 /*
3803 * The snap must be in the specified pool.
3804 */
3805 if (strncmp(name, poolname, poollen) != 0 ||
3806 (name[poollen] != '/' && name[poollen] != '@'))
3807 return (SET_ERROR(EXDEV));
3808
3809 /*
3810 * Check for permission to set the properties on the fs.
3811 */
3812 if (!nvlist_empty(props)) {
3813 *cp = '\0';
3814 error = zfs_secpolicy_write_perms(name,
3815 ZFS_DELEG_PERM_USERPROP, CRED());
3816 *cp = '@';
3817 if (error != 0)
3818 return (error);
3819 }
3820
3821 /* This must be the only snap of this fs. */
3822 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3823 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3824 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3825 == 0) {
3826 return (SET_ERROR(EXDEV));
3827 }
3828 }
3829 }
3830
3831 error = dsl_dataset_snapshot(snaps, props, outnvl);
3832
3833 return (error);
3834 }
3835
3836 /*
3837 * innvl: "message" -> string
3838 */
3839 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3840 {"message", DATA_TYPE_STRING, 0},
3841 };
3842
3843 static int
zfs_ioc_log_history(const char * unused,nvlist_t * innvl,nvlist_t * outnvl)3844 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3845 {
3846 (void) unused, (void) outnvl;
3847 const char *message;
3848 char *poolname;
3849 spa_t *spa;
3850 int error;
3851
3852 /*
3853 * The poolname in the ioctl is not set, we get it from the TSD,
3854 * which was set at the end of the last successful ioctl that allows
3855 * logging. The secpolicy func already checked that it is set.
3856 * Only one log ioctl is allowed after each successful ioctl, so
3857 * we clear the TSD here.
3858 */
3859 poolname = tsd_get(zfs_allow_log_key);
3860 if (poolname == NULL)
3861 return (SET_ERROR(EINVAL));
3862 (void) tsd_set(zfs_allow_log_key, NULL);
3863 error = spa_open(poolname, &spa, FTAG);
3864 kmem_strfree(poolname);
3865 if (error != 0)
3866 return (error);
3867
3868 message = fnvlist_lookup_string(innvl, "message");
3869
3870 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3871 spa_close(spa, FTAG);
3872 return (SET_ERROR(ENOTSUP));
3873 }
3874
3875 error = spa_history_log(spa, message);
3876 spa_close(spa, FTAG);
3877 return (error);
3878 }
3879
3880 /*
3881 * This ioctl is used to set the bootenv configuration on the current
3882 * pool. This configuration is stored in the second padding area of the label,
3883 * and it is used by the bootloader(s) to store the bootloader and/or system
3884 * specific data.
3885 * The data is stored as nvlist data stream, and is protected by
3886 * an embedded checksum.
3887 * The version can have two possible values:
3888 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3889 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3890 */
3891 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3892 {"version", DATA_TYPE_UINT64, 0},
3893 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3894 };
3895
3896 static int
zfs_ioc_set_bootenv(const char * name,nvlist_t * innvl,nvlist_t * outnvl)3897 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3898 {
3899 int error;
3900 spa_t *spa;
3901
3902 if ((error = spa_open(name, &spa, FTAG)) != 0)
3903 return (error);
3904 spa_vdev_state_enter(spa, SCL_ALL);
3905 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3906 (void) spa_vdev_state_exit(spa, NULL, 0);
3907 spa_close(spa, FTAG);
3908 return (error);
3909 }
3910
3911 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3912 /* no nvl keys */
3913 };
3914
3915 static int
zfs_ioc_get_bootenv(const char * name,nvlist_t * innvl,nvlist_t * outnvl)3916 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3917 {
3918 spa_t *spa;
3919 int error;
3920
3921 if ((error = spa_open(name, &spa, FTAG)) != 0)
3922 return (error);
3923 spa_vdev_state_enter(spa, SCL_ALL);
3924 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3925 (void) spa_vdev_state_exit(spa, NULL, 0);
3926 spa_close(spa, FTAG);
3927 return (error);
3928 }
3929
3930 /*
3931 * The dp_config_rwlock must not be held when calling this, because the
3932 * unmount may need to write out data.
3933 *
3934 * This function is best-effort. Callers must deal gracefully if it
3935 * remains mounted (or is remounted after this call).
3936 *
3937 * Returns 0 if the argument is not a snapshot, or it is not currently a
3938 * filesystem, or we were able to unmount it. Returns error code otherwise.
3939 */
3940 void
zfs_unmount_snap(const char * snapname)3941 zfs_unmount_snap(const char *snapname)
3942 {
3943 if (strchr(snapname, '@') == NULL)
3944 return;
3945
3946 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
3947 }
3948
3949 static int
zfs_unmount_snap_cb(const char * snapname,void * arg)3950 zfs_unmount_snap_cb(const char *snapname, void *arg)
3951 {
3952 (void) arg;
3953 zfs_unmount_snap(snapname);
3954 return (0);
3955 }
3956
3957 /*
3958 * When a clone is destroyed, its origin may also need to be destroyed,
3959 * in which case it must be unmounted. This routine will do that unmount
3960 * if necessary.
3961 */
3962 void
zfs_destroy_unmount_origin(const char * fsname)3963 zfs_destroy_unmount_origin(const char *fsname)
3964 {
3965 int error;
3966 objset_t *os;
3967 dsl_dataset_t *ds;
3968
3969 error = dmu_objset_hold(fsname, FTAG, &os);
3970 if (error != 0)
3971 return;
3972 ds = dmu_objset_ds(os);
3973 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3974 char originname[ZFS_MAX_DATASET_NAME_LEN];
3975 dsl_dataset_name(ds->ds_prev, originname);
3976 dmu_objset_rele(os, FTAG);
3977 zfs_unmount_snap(originname);
3978 } else {
3979 dmu_objset_rele(os, FTAG);
3980 }
3981 }
3982
3983 /*
3984 * innvl: {
3985 * "snaps" -> { snapshot1, snapshot2 }
3986 * (optional boolean) "defer"
3987 * }
3988 *
3989 * outnvl: snapshot -> error code (int32)
3990 */
3991 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3992 {"snaps", DATA_TYPE_NVLIST, 0},
3993 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3994 };
3995
3996 static int
zfs_ioc_destroy_snaps(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)3997 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3998 {
3999 int poollen;
4000 nvlist_t *snaps;
4001 nvpair_t *pair;
4002 boolean_t defer;
4003 spa_t *spa;
4004
4005 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
4006 defer = nvlist_exists(innvl, "defer");
4007
4008 poollen = strlen(poolname);
4009 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
4010 pair = nvlist_next_nvpair(snaps, pair)) {
4011 const char *name = nvpair_name(pair);
4012
4013 /*
4014 * The snap must be in the specified pool to prevent the
4015 * invalid removal of zvol minors below.
4016 */
4017 if (strncmp(name, poolname, poollen) != 0 ||
4018 (name[poollen] != '/' && name[poollen] != '@'))
4019 return (SET_ERROR(EXDEV));
4020
4021 zfs_unmount_snap(nvpair_name(pair));
4022 if (spa_open(name, &spa, FTAG) == 0) {
4023 zvol_remove_minors(spa, name, B_TRUE);
4024 spa_close(spa, FTAG);
4025 }
4026 }
4027
4028 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
4029 }
4030
4031 /*
4032 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
4033 * All bookmarks and snapshots must be in the same pool.
4034 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
4035 *
4036 * innvl: {
4037 * new_bookmark1 -> existing_snapshot,
4038 * new_bookmark2 -> existing_bookmark,
4039 * }
4040 *
4041 * outnvl: bookmark -> error code (int32)
4042 *
4043 */
4044 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
4045 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
4046 };
4047
4048 static int
zfs_ioc_bookmark(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4049 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4050 {
4051 (void) poolname;
4052 return (dsl_bookmark_create(innvl, outnvl));
4053 }
4054
4055 /*
4056 * innvl: {
4057 * property 1, property 2, ...
4058 * }
4059 *
4060 * outnvl: {
4061 * bookmark name 1 -> { property 1, property 2, ... },
4062 * bookmark name 2 -> { property 1, property 2, ... }
4063 * }
4064 *
4065 */
4066 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
4067 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
4068 };
4069
4070 static int
zfs_ioc_get_bookmarks(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)4071 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4072 {
4073 return (dsl_get_bookmarks(fsname, innvl, outnvl));
4074 }
4075
4076 /*
4077 * innvl is not used.
4078 *
4079 * outnvl: {
4080 * property 1, property 2, ...
4081 * }
4082 *
4083 */
4084 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
4085 /* no nvl keys */
4086 };
4087
4088 static int
zfs_ioc_get_bookmark_props(const char * bookmark,nvlist_t * innvl,nvlist_t * outnvl)4089 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
4090 nvlist_t *outnvl)
4091 {
4092 (void) innvl;
4093 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4094 char *bmname;
4095
4096 bmname = strchr(bookmark, '#');
4097 if (bmname == NULL)
4098 return (SET_ERROR(EINVAL));
4099 bmname++;
4100
4101 (void) strlcpy(fsname, bookmark, sizeof (fsname));
4102 *(strchr(fsname, '#')) = '\0';
4103
4104 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
4105 }
4106
4107 /*
4108 * innvl: {
4109 * bookmark name 1, bookmark name 2
4110 * }
4111 *
4112 * outnvl: bookmark -> error code (int32)
4113 *
4114 */
4115 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
4116 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
4117 };
4118
4119 static int
zfs_ioc_destroy_bookmarks(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4120 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
4121 nvlist_t *outnvl)
4122 {
4123 int error, poollen;
4124
4125 poollen = strlen(poolname);
4126 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
4127 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
4128 const char *name = nvpair_name(pair);
4129 const char *cp = strchr(name, '#');
4130
4131 /*
4132 * The bookmark name must contain an #, and the part after it
4133 * must contain only valid characters.
4134 */
4135 if (cp == NULL ||
4136 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4137 return (SET_ERROR(EINVAL));
4138
4139 /*
4140 * The bookmark must be in the specified pool.
4141 */
4142 if (strncmp(name, poolname, poollen) != 0 ||
4143 (name[poollen] != '/' && name[poollen] != '#'))
4144 return (SET_ERROR(EXDEV));
4145 }
4146
4147 error = dsl_bookmark_destroy(innvl, outnvl);
4148 return (error);
4149 }
4150
4151 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
4152 {"program", DATA_TYPE_STRING, 0},
4153 {"arg", DATA_TYPE_ANY, 0},
4154 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4155 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
4156 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
4157 };
4158
4159 static int
zfs_ioc_channel_program(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4160 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
4161 nvlist_t *outnvl)
4162 {
4163 const char *program;
4164 uint64_t instrlimit, memlimit;
4165 boolean_t sync_flag;
4166 nvpair_t *nvarg = NULL;
4167
4168 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
4169 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
4170 sync_flag = B_TRUE;
4171 }
4172 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
4173 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
4174 }
4175 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
4176 memlimit = ZCP_DEFAULT_MEMLIMIT;
4177 }
4178 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
4179
4180 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
4181 return (SET_ERROR(EINVAL));
4182 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
4183 return (SET_ERROR(EINVAL));
4184
4185 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
4186 nvarg, outnvl));
4187 }
4188
4189 /*
4190 * innvl: unused
4191 * outnvl: empty
4192 */
4193 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
4194 /* no nvl keys */
4195 };
4196
4197 static int
zfs_ioc_pool_checkpoint(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4198 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4199 {
4200 (void) innvl, (void) outnvl;
4201 return (spa_checkpoint(poolname));
4202 }
4203
4204 /*
4205 * innvl: unused
4206 * outnvl: empty
4207 */
4208 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
4209 /* no nvl keys */
4210 };
4211
4212 static int
zfs_ioc_pool_discard_checkpoint(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4213 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
4214 nvlist_t *outnvl)
4215 {
4216 (void) innvl, (void) outnvl;
4217 return (spa_checkpoint_discard(poolname));
4218 }
4219
4220 /*
4221 * Loads specific types of data for the given pool
4222 *
4223 * innvl: {
4224 * "prefetch_type" -> int32_t
4225 * }
4226 *
4227 * outnvl: empty
4228 */
4229 static const zfs_ioc_key_t zfs_keys_pool_prefetch[] = {
4230 {ZPOOL_PREFETCH_TYPE, DATA_TYPE_INT32, 0},
4231 };
4232
4233 static int
zfs_ioc_pool_prefetch(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4234 zfs_ioc_pool_prefetch(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4235 {
4236 (void) outnvl;
4237
4238 int error;
4239 spa_t *spa;
4240 int32_t type;
4241
4242 /*
4243 * Currently, only ZPOOL_PREFETCH_DDT is supported
4244 */
4245 if (nvlist_lookup_int32(innvl, ZPOOL_PREFETCH_TYPE, &type) != 0 ||
4246 type != ZPOOL_PREFETCH_DDT) {
4247 return (EINVAL);
4248 }
4249
4250 error = spa_open(poolname, &spa, FTAG);
4251 if (error != 0)
4252 return (error);
4253
4254 hrtime_t start_time = gethrtime();
4255
4256 ddt_prefetch_all(spa);
4257
4258 zfs_dbgmsg("pool '%s': loaded ddt into ARC in %llu ms", spa->spa_name,
4259 (u_longlong_t)NSEC2MSEC(gethrtime() - start_time));
4260
4261 spa_close(spa, FTAG);
4262
4263 return (error);
4264 }
4265
4266 /*
4267 * inputs:
4268 * zc_name name of dataset to destroy
4269 * zc_defer_destroy mark for deferred destroy
4270 *
4271 * outputs: none
4272 */
4273 static int
zfs_ioc_destroy(zfs_cmd_t * zc)4274 zfs_ioc_destroy(zfs_cmd_t *zc)
4275 {
4276 objset_t *os;
4277 dmu_objset_type_t ost;
4278 int err;
4279
4280 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4281 if (err != 0)
4282 return (err);
4283 ost = dmu_objset_type(os);
4284 dmu_objset_rele(os, FTAG);
4285
4286 if (ost == DMU_OST_ZFS)
4287 zfs_unmount_snap(zc->zc_name);
4288
4289 if (strchr(zc->zc_name, '@')) {
4290 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
4291 } else {
4292 err = dsl_destroy_head(zc->zc_name);
4293 if (err == EEXIST) {
4294 /*
4295 * It is possible that the given DS may have
4296 * hidden child (%recv) datasets - "leftovers"
4297 * resulting from the previously interrupted
4298 * 'zfs receive'.
4299 *
4300 * 6 extra bytes for /%recv
4301 */
4302 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
4303
4304 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
4305 zc->zc_name, recv_clone_name) >=
4306 sizeof (namebuf))
4307 return (SET_ERROR(EINVAL));
4308
4309 /*
4310 * Try to remove the hidden child (%recv) and after
4311 * that try to remove the target dataset.
4312 * If the hidden child (%recv) does not exist
4313 * the original error (EEXIST) will be returned
4314 */
4315 err = dsl_destroy_head(namebuf);
4316 if (err == 0)
4317 err = dsl_destroy_head(zc->zc_name);
4318 else if (err == ENOENT)
4319 err = SET_ERROR(EEXIST);
4320 }
4321 }
4322
4323 return (err);
4324 }
4325
4326 /*
4327 * innvl: {
4328 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
4329 * "initialize_vdevs": { -> guids to initialize (nvlist)
4330 * "vdev_path_1": vdev_guid_1, (uint64),
4331 * "vdev_path_2": vdev_guid_2, (uint64),
4332 * ...
4333 * },
4334 * }
4335 *
4336 * outnvl: {
4337 * "initialize_vdevs": { -> initialization errors (nvlist)
4338 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4339 * "vdev_path_2": errno, ... (uint64)
4340 * ...
4341 * }
4342 * }
4343 *
4344 * EINVAL is returned for an unknown commands or if any of the provided vdev
4345 * guids have be specified with a type other than uint64.
4346 */
4347 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4348 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4349 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4350 };
4351
4352 static int
zfs_ioc_pool_initialize(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4353 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4354 {
4355 uint64_t cmd_type;
4356 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4357 &cmd_type) != 0) {
4358 return (SET_ERROR(EINVAL));
4359 }
4360
4361 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4362 cmd_type == POOL_INITIALIZE_START ||
4363 cmd_type == POOL_INITIALIZE_SUSPEND ||
4364 cmd_type == POOL_INITIALIZE_UNINIT)) {
4365 return (SET_ERROR(EINVAL));
4366 }
4367
4368 nvlist_t *vdev_guids;
4369 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4370 &vdev_guids) != 0) {
4371 return (SET_ERROR(EINVAL));
4372 }
4373
4374 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4375 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4376 uint64_t vdev_guid;
4377 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4378 return (SET_ERROR(EINVAL));
4379 }
4380 }
4381
4382 spa_t *spa;
4383 int error = spa_open(poolname, &spa, FTAG);
4384 if (error != 0)
4385 return (error);
4386
4387 nvlist_t *vdev_errlist = fnvlist_alloc();
4388 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4389 vdev_errlist);
4390
4391 if (fnvlist_size(vdev_errlist) > 0) {
4392 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4393 vdev_errlist);
4394 }
4395 fnvlist_free(vdev_errlist);
4396
4397 spa_close(spa, FTAG);
4398 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4399 }
4400
4401 /*
4402 * innvl: {
4403 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4404 * "trim_vdevs": { -> guids to TRIM (nvlist)
4405 * "vdev_path_1": vdev_guid_1, (uint64),
4406 * "vdev_path_2": vdev_guid_2, (uint64),
4407 * ...
4408 * },
4409 * "trim_rate" -> Target TRIM rate in bytes/sec.
4410 * "trim_secure" -> Set to request a secure TRIM.
4411 * }
4412 *
4413 * outnvl: {
4414 * "trim_vdevs": { -> TRIM errors (nvlist)
4415 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4416 * "vdev_path_2": errno, ... (uint64)
4417 * ...
4418 * }
4419 * }
4420 *
4421 * EINVAL is returned for an unknown commands or if any of the provided vdev
4422 * guids have be specified with a type other than uint64.
4423 */
4424 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4425 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4426 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4427 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4428 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4429 };
4430
4431 static int
zfs_ioc_pool_trim(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4432 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4433 {
4434 uint64_t cmd_type;
4435 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4436 return (SET_ERROR(EINVAL));
4437
4438 if (!(cmd_type == POOL_TRIM_CANCEL ||
4439 cmd_type == POOL_TRIM_START ||
4440 cmd_type == POOL_TRIM_SUSPEND)) {
4441 return (SET_ERROR(EINVAL));
4442 }
4443
4444 nvlist_t *vdev_guids;
4445 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4446 return (SET_ERROR(EINVAL));
4447
4448 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4449 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4450 uint64_t vdev_guid;
4451 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4452 return (SET_ERROR(EINVAL));
4453 }
4454 }
4455
4456 /* Optional, defaults to maximum rate when not provided */
4457 uint64_t rate;
4458 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4459 rate = 0;
4460
4461 /* Optional, defaults to standard TRIM when not provided */
4462 boolean_t secure;
4463 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4464 &secure) != 0) {
4465 secure = B_FALSE;
4466 }
4467
4468 spa_t *spa;
4469 int error = spa_open(poolname, &spa, FTAG);
4470 if (error != 0)
4471 return (error);
4472
4473 nvlist_t *vdev_errlist = fnvlist_alloc();
4474 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4475 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4476
4477 if (fnvlist_size(vdev_errlist) > 0)
4478 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4479
4480 fnvlist_free(vdev_errlist);
4481
4482 spa_close(spa, FTAG);
4483 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4484 }
4485
4486 #define DDT_PRUNE_UNIT "ddt_prune_unit"
4487 #define DDT_PRUNE_AMOUNT "ddt_prune_amount"
4488
4489 /*
4490 * innvl: {
4491 * "ddt_prune_unit" -> uint32_t
4492 * "ddt_prune_amount" -> uint64_t
4493 * }
4494 *
4495 * outnvl: "waited" -> boolean_t
4496 */
4497 static const zfs_ioc_key_t zfs_keys_ddt_prune[] = {
4498 {DDT_PRUNE_UNIT, DATA_TYPE_INT32, 0},
4499 {DDT_PRUNE_AMOUNT, DATA_TYPE_UINT64, 0},
4500 };
4501
4502 static int
zfs_ioc_ddt_prune(const char * poolname,nvlist_t * innvl,nvlist_t * outnvl)4503 zfs_ioc_ddt_prune(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4504 {
4505 int32_t unit;
4506 uint64_t amount;
4507
4508 if (nvlist_lookup_int32(innvl, DDT_PRUNE_UNIT, &unit) != 0 ||
4509 nvlist_lookup_uint64(innvl, DDT_PRUNE_AMOUNT, &amount) != 0) {
4510 return (EINVAL);
4511 }
4512
4513 spa_t *spa;
4514 int error = spa_open(poolname, &spa, FTAG);
4515 if (error != 0)
4516 return (error);
4517
4518 if (!spa_feature_is_enabled(spa, SPA_FEATURE_FAST_DEDUP)) {
4519 spa_close(spa, FTAG);
4520 return (SET_ERROR(ENOTSUP));
4521 }
4522
4523 error = ddt_prune_unique_entries(spa, (zpool_ddt_prune_unit_t)unit,
4524 amount);
4525
4526 spa_close(spa, FTAG);
4527
4528 return (error);
4529 }
4530
4531 /*
4532 * This ioctl waits for activity of a particular type to complete. If there is
4533 * no activity of that type in progress, it returns immediately, and the
4534 * returned value "waited" is false. If there is activity in progress, and no
4535 * tag is passed in, the ioctl blocks until all activity of that type is
4536 * complete, and then returns with "waited" set to true.
4537 *
4538 * If a tag is provided, it identifies a particular instance of an activity to
4539 * wait for. Currently, this is only valid for use with 'initialize', because
4540 * that is the only activity for which there can be multiple instances running
4541 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4542 * the vdev on which to wait.
4543 *
4544 * If a thread waiting in the ioctl receives a signal, the call will return
4545 * immediately, and the return value will be EINTR.
4546 *
4547 * innvl: {
4548 * "wait_activity" -> int32_t
4549 * (optional) "wait_tag" -> uint64_t
4550 * }
4551 *
4552 * outnvl: "waited" -> boolean_t
4553 */
4554 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4555 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4556 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4557 };
4558
4559 static int
zfs_ioc_wait(const char * name,nvlist_t * innvl,nvlist_t * outnvl)4560 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4561 {
4562 int32_t activity;
4563 uint64_t tag;
4564 boolean_t waited;
4565 int error;
4566
4567 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4568 return (EINVAL);
4569
4570 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4571 error = spa_wait_tag(name, activity, tag, &waited);
4572 else
4573 error = spa_wait(name, activity, &waited);
4574
4575 if (error == 0)
4576 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4577
4578 return (error);
4579 }
4580
4581 /*
4582 * This ioctl waits for activity of a particular type to complete. If there is
4583 * no activity of that type in progress, it returns immediately, and the
4584 * returned value "waited" is false. If there is activity in progress, and no
4585 * tag is passed in, the ioctl blocks until all activity of that type is
4586 * complete, and then returns with "waited" set to true.
4587 *
4588 * If a thread waiting in the ioctl receives a signal, the call will return
4589 * immediately, and the return value will be EINTR.
4590 *
4591 * innvl: {
4592 * "wait_activity" -> int32_t
4593 * }
4594 *
4595 * outnvl: "waited" -> boolean_t
4596 */
4597 static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4598 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4599 };
4600
4601 static int
zfs_ioc_wait_fs(const char * name,nvlist_t * innvl,nvlist_t * outnvl)4602 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4603 {
4604 int32_t activity;
4605 boolean_t waited = B_FALSE;
4606 int error;
4607 dsl_pool_t *dp;
4608 dsl_dir_t *dd;
4609 dsl_dataset_t *ds;
4610
4611 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4612 return (SET_ERROR(EINVAL));
4613
4614 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4615 return (SET_ERROR(EINVAL));
4616
4617 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4618 return (error);
4619
4620 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4621 dsl_pool_rele(dp, FTAG);
4622 return (error);
4623 }
4624
4625 dd = ds->ds_dir;
4626 mutex_enter(&dd->dd_activity_lock);
4627 dd->dd_activity_waiters++;
4628
4629 /*
4630 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4631 * aren't evicted while we're waiting. Normally this is prevented by
4632 * holding the pool, but we can't do that while we're waiting since
4633 * that would prevent TXGs from syncing out. Some of the functionality
4634 * of long-holds (e.g. preventing deletion) is unnecessary for this
4635 * case, since we would cancel the waiters before proceeding with a
4636 * deletion. An alternative mechanism for keeping the dataset around
4637 * could be developed but this is simpler.
4638 */
4639 dsl_dataset_long_hold(ds, FTAG);
4640 dsl_pool_rele(dp, FTAG);
4641
4642 error = dsl_dir_wait(dd, ds, activity, &waited);
4643
4644 dsl_dataset_long_rele(ds, FTAG);
4645 dd->dd_activity_waiters--;
4646 if (dd->dd_activity_waiters == 0)
4647 cv_signal(&dd->dd_activity_cv);
4648 mutex_exit(&dd->dd_activity_lock);
4649
4650 dsl_dataset_rele(ds, FTAG);
4651
4652 if (error == 0)
4653 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4654
4655 return (error);
4656 }
4657
4658 /*
4659 * fsname is name of dataset to rollback (to most recent snapshot)
4660 *
4661 * innvl may contain name of expected target snapshot
4662 *
4663 * outnvl: "target" -> name of most recent snapshot
4664 * }
4665 */
4666 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4667 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4668 };
4669
4670 static int
zfs_ioc_rollback(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)4671 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4672 {
4673 zfsvfs_t *zfsvfs;
4674 zvol_state_handle_t *zv;
4675 const char *target = NULL;
4676 int error;
4677
4678 (void) nvlist_lookup_string(innvl, "target", &target);
4679 if (target != NULL) {
4680 const char *cp = strchr(target, '@');
4681
4682 /*
4683 * The snap name must contain an @, and the part after it must
4684 * contain only valid characters.
4685 */
4686 if (cp == NULL ||
4687 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4688 return (SET_ERROR(EINVAL));
4689 }
4690
4691 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4692 dsl_dataset_t *ds;
4693
4694 ds = dmu_objset_ds(zfsvfs->z_os);
4695 error = zfs_suspend_fs(zfsvfs);
4696 if (error == 0) {
4697 int resume_err;
4698
4699 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4700 outnvl);
4701 resume_err = zfs_resume_fs(zfsvfs, ds);
4702 error = error ? error : resume_err;
4703 }
4704 zfs_vfs_rele(zfsvfs);
4705 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4706 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4707 outnvl);
4708 zvol_resume(zv);
4709 } else {
4710 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4711 }
4712 return (error);
4713 }
4714
4715 static int
recursive_unmount(const char * fsname,void * arg)4716 recursive_unmount(const char *fsname, void *arg)
4717 {
4718 const char *snapname = arg;
4719 char *fullname;
4720
4721 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4722 zfs_unmount_snap(fullname);
4723 kmem_strfree(fullname);
4724
4725 return (0);
4726 }
4727
4728 /*
4729 *
4730 * snapname is the snapshot to redact.
4731 * innvl: {
4732 * "bookname" -> (string)
4733 * shortname of the redaction bookmark to generate
4734 * "snapnv" -> (nvlist, values ignored)
4735 * snapshots to redact snapname with respect to
4736 * }
4737 *
4738 * outnvl is unused
4739 */
4740
4741 static const zfs_ioc_key_t zfs_keys_redact[] = {
4742 {"bookname", DATA_TYPE_STRING, 0},
4743 {"snapnv", DATA_TYPE_NVLIST, 0},
4744 };
4745
4746 static int
zfs_ioc_redact(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)4747 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4748 {
4749 (void) outnvl;
4750 nvlist_t *redactnvl = NULL;
4751 const char *redactbook = NULL;
4752
4753 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4754 return (SET_ERROR(EINVAL));
4755 if (fnvlist_num_pairs(redactnvl) == 0)
4756 return (SET_ERROR(ENXIO));
4757 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4758 return (SET_ERROR(EINVAL));
4759
4760 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4761 }
4762
4763 /*
4764 * inputs:
4765 * zc_name old name of dataset
4766 * zc_value new name of dataset
4767 * zc_cookie recursive flag (only valid for snapshots)
4768 *
4769 * outputs: none
4770 */
4771 static int
zfs_ioc_rename(zfs_cmd_t * zc)4772 zfs_ioc_rename(zfs_cmd_t *zc)
4773 {
4774 objset_t *os;
4775 dmu_objset_type_t ost;
4776 boolean_t recursive = zc->zc_cookie & 1;
4777 boolean_t nounmount = !!(zc->zc_cookie & 2);
4778 char *at;
4779 int err;
4780
4781 /* "zfs rename" from and to ...%recv datasets should both fail */
4782 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4783 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4784 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4785 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4786 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4787 return (SET_ERROR(EINVAL));
4788
4789 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4790 if (err != 0)
4791 return (err);
4792 ost = dmu_objset_type(os);
4793 dmu_objset_rele(os, FTAG);
4794
4795 at = strchr(zc->zc_name, '@');
4796 if (at != NULL) {
4797 /* snaps must be in same fs */
4798 int error;
4799
4800 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4801 return (SET_ERROR(EXDEV));
4802 *at = '\0';
4803 if (ost == DMU_OST_ZFS && !nounmount) {
4804 error = dmu_objset_find(zc->zc_name,
4805 recursive_unmount, at + 1,
4806 recursive ? DS_FIND_CHILDREN : 0);
4807 if (error != 0) {
4808 *at = '@';
4809 return (error);
4810 }
4811 }
4812 error = dsl_dataset_rename_snapshot(zc->zc_name,
4813 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4814 *at = '@';
4815
4816 return (error);
4817 } else {
4818 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4819 }
4820 }
4821
4822 static int
zfs_check_settable(const char * dsname,nvpair_t * pair,cred_t * cr)4823 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4824 {
4825 const char *propname = nvpair_name(pair);
4826 boolean_t issnap = (strchr(dsname, '@') != NULL);
4827 zfs_prop_t prop = zfs_name_to_prop(propname);
4828 uint64_t intval, compval;
4829 int err;
4830
4831 if (prop == ZPROP_USERPROP) {
4832 if (zfs_prop_user(propname)) {
4833 if ((err = zfs_secpolicy_write_perms(dsname,
4834 ZFS_DELEG_PERM_USERPROP, cr)))
4835 return (err);
4836 return (0);
4837 }
4838
4839 if (!issnap && zfs_prop_userquota(propname)) {
4840 const char *perm = NULL;
4841 const char *uq_prefix =
4842 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4843 const char *gq_prefix =
4844 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4845 const char *uiq_prefix =
4846 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4847 const char *giq_prefix =
4848 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4849 const char *pq_prefix =
4850 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4851 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4852 ZFS_PROP_PROJECTOBJQUOTA];
4853
4854 if (strncmp(propname, uq_prefix,
4855 strlen(uq_prefix)) == 0) {
4856 perm = ZFS_DELEG_PERM_USERQUOTA;
4857 } else if (strncmp(propname, uiq_prefix,
4858 strlen(uiq_prefix)) == 0) {
4859 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4860 } else if (strncmp(propname, gq_prefix,
4861 strlen(gq_prefix)) == 0) {
4862 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4863 } else if (strncmp(propname, giq_prefix,
4864 strlen(giq_prefix)) == 0) {
4865 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4866 } else if (strncmp(propname, pq_prefix,
4867 strlen(pq_prefix)) == 0) {
4868 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4869 } else if (strncmp(propname, piq_prefix,
4870 strlen(piq_prefix)) == 0) {
4871 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4872 } else {
4873 /* {USER|GROUP|PROJECT}USED are read-only */
4874 return (SET_ERROR(EINVAL));
4875 }
4876
4877 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4878 return (err);
4879 return (0);
4880 }
4881
4882 return (SET_ERROR(EINVAL));
4883 }
4884
4885 if (issnap)
4886 return (SET_ERROR(EINVAL));
4887
4888 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4889 /*
4890 * dsl_prop_get_all_impl() returns properties in this
4891 * format.
4892 */
4893 nvlist_t *attrs;
4894 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4895 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4896 &pair) == 0);
4897 }
4898
4899 /*
4900 * Check that this value is valid for this pool version
4901 */
4902 switch (prop) {
4903 case ZFS_PROP_COMPRESSION:
4904 /*
4905 * If the user specified gzip compression, make sure
4906 * the SPA supports it. We ignore any errors here since
4907 * we'll catch them later.
4908 */
4909 if (nvpair_value_uint64(pair, &intval) == 0) {
4910 compval = ZIO_COMPRESS_ALGO(intval);
4911 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4912 compval <= ZIO_COMPRESS_GZIP_9 &&
4913 zfs_earlier_version(dsname,
4914 SPA_VERSION_GZIP_COMPRESSION)) {
4915 return (SET_ERROR(ENOTSUP));
4916 }
4917
4918 if (compval == ZIO_COMPRESS_ZLE &&
4919 zfs_earlier_version(dsname,
4920 SPA_VERSION_ZLE_COMPRESSION))
4921 return (SET_ERROR(ENOTSUP));
4922
4923 if (compval == ZIO_COMPRESS_LZ4) {
4924 spa_t *spa;
4925
4926 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4927 return (err);
4928
4929 if (!spa_feature_is_enabled(spa,
4930 SPA_FEATURE_LZ4_COMPRESS)) {
4931 spa_close(spa, FTAG);
4932 return (SET_ERROR(ENOTSUP));
4933 }
4934 spa_close(spa, FTAG);
4935 }
4936
4937 if (compval == ZIO_COMPRESS_ZSTD) {
4938 spa_t *spa;
4939
4940 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4941 return (err);
4942
4943 if (!spa_feature_is_enabled(spa,
4944 SPA_FEATURE_ZSTD_COMPRESS)) {
4945 spa_close(spa, FTAG);
4946 return (SET_ERROR(ENOTSUP));
4947 }
4948 spa_close(spa, FTAG);
4949 }
4950 }
4951 break;
4952
4953 case ZFS_PROP_COPIES:
4954 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4955 return (SET_ERROR(ENOTSUP));
4956 break;
4957
4958 case ZFS_PROP_VOLBLOCKSIZE:
4959 case ZFS_PROP_RECORDSIZE:
4960 /* Record sizes above 128k need the feature to be enabled */
4961 if (nvpair_value_uint64(pair, &intval) == 0 &&
4962 intval > SPA_OLD_MAXBLOCKSIZE) {
4963 spa_t *spa;
4964
4965 /*
4966 * We don't allow setting the property above 1MB,
4967 * unless the tunable has been changed.
4968 */
4969 if (intval > zfs_max_recordsize ||
4970 intval > SPA_MAXBLOCKSIZE)
4971 return (SET_ERROR(ERANGE));
4972
4973 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4974 return (err);
4975
4976 if (!spa_feature_is_enabled(spa,
4977 SPA_FEATURE_LARGE_BLOCKS)) {
4978 spa_close(spa, FTAG);
4979 return (SET_ERROR(ENOTSUP));
4980 }
4981 spa_close(spa, FTAG);
4982 }
4983 break;
4984
4985 case ZFS_PROP_DNODESIZE:
4986 /* Dnode sizes above 512 need the feature to be enabled */
4987 if (nvpair_value_uint64(pair, &intval) == 0 &&
4988 intval != ZFS_DNSIZE_LEGACY) {
4989 spa_t *spa;
4990
4991 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4992 return (err);
4993
4994 if (!spa_feature_is_enabled(spa,
4995 SPA_FEATURE_LARGE_DNODE)) {
4996 spa_close(spa, FTAG);
4997 return (SET_ERROR(ENOTSUP));
4998 }
4999 spa_close(spa, FTAG);
5000 }
5001 break;
5002
5003 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
5004 /*
5005 * This property could require the allocation classes
5006 * feature to be active for setting, however we allow
5007 * it so that tests of settable properties succeed.
5008 * The CLI will issue a warning in this case.
5009 */
5010 break;
5011
5012 case ZFS_PROP_SHARESMB:
5013 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
5014 return (SET_ERROR(ENOTSUP));
5015 break;
5016
5017 case ZFS_PROP_ACLINHERIT:
5018 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
5019 nvpair_value_uint64(pair, &intval) == 0) {
5020 if (intval == ZFS_ACL_PASSTHROUGH_X &&
5021 zfs_earlier_version(dsname,
5022 SPA_VERSION_PASSTHROUGH_X))
5023 return (SET_ERROR(ENOTSUP));
5024 }
5025 break;
5026 case ZFS_PROP_CHECKSUM:
5027 case ZFS_PROP_DEDUP:
5028 {
5029 spa_feature_t feature;
5030 spa_t *spa;
5031 int err;
5032
5033 /* dedup feature version checks */
5034 if (prop == ZFS_PROP_DEDUP &&
5035 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
5036 return (SET_ERROR(ENOTSUP));
5037
5038 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
5039 nvpair_value_uint64(pair, &intval) == 0) {
5040 /* check prop value is enabled in features */
5041 feature = zio_checksum_to_feature(
5042 intval & ZIO_CHECKSUM_MASK);
5043 if (feature == SPA_FEATURE_NONE)
5044 break;
5045
5046 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
5047 return (err);
5048
5049 if (!spa_feature_is_enabled(spa, feature)) {
5050 spa_close(spa, FTAG);
5051 return (SET_ERROR(ENOTSUP));
5052 }
5053 spa_close(spa, FTAG);
5054 }
5055 break;
5056 }
5057
5058 default:
5059 break;
5060 }
5061
5062 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
5063 }
5064
5065 /*
5066 * Removes properties from the given props list that fail permission checks
5067 * needed to clear them and to restore them in case of a receive error. For each
5068 * property, make sure we have both set and inherit permissions.
5069 *
5070 * Returns the first error encountered if any permission checks fail. If the
5071 * caller provides a non-NULL errlist, it also gives the complete list of names
5072 * of all the properties that failed a permission check along with the
5073 * corresponding error numbers. The caller is responsible for freeing the
5074 * returned errlist.
5075 *
5076 * If every property checks out successfully, zero is returned and the list
5077 * pointed at by errlist is NULL.
5078 */
5079 static int
zfs_check_clearable(const char * dataset,nvlist_t * props,nvlist_t ** errlist)5080 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
5081 {
5082 zfs_cmd_t *zc;
5083 nvpair_t *pair, *next_pair;
5084 nvlist_t *errors;
5085 int err, rv = 0;
5086
5087 if (props == NULL)
5088 return (0);
5089
5090 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5091
5092 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
5093 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
5094 pair = nvlist_next_nvpair(props, NULL);
5095 while (pair != NULL) {
5096 next_pair = nvlist_next_nvpair(props, pair);
5097
5098 (void) strlcpy(zc->zc_value, nvpair_name(pair),
5099 sizeof (zc->zc_value));
5100 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
5101 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
5102 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
5103 VERIFY(nvlist_add_int32(errors,
5104 zc->zc_value, err) == 0);
5105 }
5106 pair = next_pair;
5107 }
5108 kmem_free(zc, sizeof (zfs_cmd_t));
5109
5110 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
5111 nvlist_free(errors);
5112 errors = NULL;
5113 } else {
5114 VERIFY(nvpair_value_int32(pair, &rv) == 0);
5115 }
5116
5117 if (errlist == NULL)
5118 nvlist_free(errors);
5119 else
5120 *errlist = errors;
5121
5122 return (rv);
5123 }
5124
5125 static boolean_t
propval_equals(nvpair_t * p1,nvpair_t * p2)5126 propval_equals(nvpair_t *p1, nvpair_t *p2)
5127 {
5128 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
5129 /* dsl_prop_get_all_impl() format */
5130 nvlist_t *attrs;
5131 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
5132 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
5133 &p1) == 0);
5134 }
5135
5136 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
5137 nvlist_t *attrs;
5138 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
5139 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
5140 &p2) == 0);
5141 }
5142
5143 if (nvpair_type(p1) != nvpair_type(p2))
5144 return (B_FALSE);
5145
5146 if (nvpair_type(p1) == DATA_TYPE_STRING) {
5147 const char *valstr1, *valstr2;
5148
5149 VERIFY(nvpair_value_string(p1, &valstr1) == 0);
5150 VERIFY(nvpair_value_string(p2, &valstr2) == 0);
5151 return (strcmp(valstr1, valstr2) == 0);
5152 } else {
5153 uint64_t intval1, intval2;
5154
5155 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
5156 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
5157 return (intval1 == intval2);
5158 }
5159 }
5160
5161 /*
5162 * Remove properties from props if they are not going to change (as determined
5163 * by comparison with origprops). Remove them from origprops as well, since we
5164 * do not need to clear or restore properties that won't change.
5165 */
5166 static void
props_reduce(nvlist_t * props,nvlist_t * origprops)5167 props_reduce(nvlist_t *props, nvlist_t *origprops)
5168 {
5169 nvpair_t *pair, *next_pair;
5170
5171 if (origprops == NULL)
5172 return; /* all props need to be received */
5173
5174 pair = nvlist_next_nvpair(props, NULL);
5175 while (pair != NULL) {
5176 const char *propname = nvpair_name(pair);
5177 nvpair_t *match;
5178
5179 next_pair = nvlist_next_nvpair(props, pair);
5180
5181 if ((nvlist_lookup_nvpair(origprops, propname,
5182 &match) != 0) || !propval_equals(pair, match))
5183 goto next; /* need to set received value */
5184
5185 /* don't clear the existing received value */
5186 (void) nvlist_remove_nvpair(origprops, match);
5187 /* don't bother receiving the property */
5188 (void) nvlist_remove_nvpair(props, pair);
5189 next:
5190 pair = next_pair;
5191 }
5192 }
5193
5194 /*
5195 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
5196 * For example, refquota cannot be set until after the receipt of a dataset,
5197 * because in replication streams, an older/earlier snapshot may exceed the
5198 * refquota. We want to receive the older/earlier snapshot, but setting
5199 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
5200 * the older/earlier snapshot from being received (with EDQUOT).
5201 *
5202 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
5203 *
5204 * libzfs will need to be judicious handling errors encountered by props
5205 * extracted by this function.
5206 */
5207 static nvlist_t *
extract_delay_props(nvlist_t * props)5208 extract_delay_props(nvlist_t *props)
5209 {
5210 nvlist_t *delayprops;
5211 nvpair_t *nvp, *tmp;
5212 static const zfs_prop_t delayable[] = {
5213 ZFS_PROP_REFQUOTA,
5214 ZFS_PROP_KEYLOCATION,
5215 /*
5216 * Setting ZFS_PROP_SHARESMB requires the objset type to be
5217 * known, which is not possible prior to receipt of raw sends.
5218 */
5219 ZFS_PROP_SHARESMB,
5220 0
5221 };
5222 int i;
5223
5224 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5225
5226 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
5227 nvp = nvlist_next_nvpair(props, nvp)) {
5228 /*
5229 * strcmp() is safe because zfs_prop_to_name() always returns
5230 * a bounded string.
5231 */
5232 for (i = 0; delayable[i] != 0; i++) {
5233 if (strcmp(zfs_prop_to_name(delayable[i]),
5234 nvpair_name(nvp)) == 0) {
5235 break;
5236 }
5237 }
5238 if (delayable[i] != 0) {
5239 tmp = nvlist_prev_nvpair(props, nvp);
5240 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
5241 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
5242 nvp = tmp;
5243 }
5244 }
5245
5246 if (nvlist_empty(delayprops)) {
5247 nvlist_free(delayprops);
5248 delayprops = NULL;
5249 }
5250 return (delayprops);
5251 }
5252
5253 static void
zfs_allow_log_destroy(void * arg)5254 zfs_allow_log_destroy(void *arg)
5255 {
5256 char *poolname = arg;
5257
5258 if (poolname != NULL)
5259 kmem_strfree(poolname);
5260 }
5261
5262 #ifdef ZFS_DEBUG
5263 static boolean_t zfs_ioc_recv_inject_err;
5264 #endif
5265
5266 /*
5267 * nvlist 'errors' is always allocated. It will contain descriptions of
5268 * encountered errors, if any. It's the callers responsibility to free.
5269 */
5270 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)5271 zfs_ioc_recv_impl(char *tofs, char *tosnap, const char *origin,
5272 nvlist_t *recvprops, nvlist_t *localprops, nvlist_t *hidden_args,
5273 boolean_t force, boolean_t heal, boolean_t resumable, int input_fd,
5274 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
5275 uint64_t *errflags, nvlist_t **errors)
5276 {
5277 dmu_recv_cookie_t drc;
5278 int error = 0;
5279 int props_error = 0;
5280 offset_t off, noff;
5281 nvlist_t *local_delayprops = NULL;
5282 nvlist_t *recv_delayprops = NULL;
5283 nvlist_t *inherited_delayprops = NULL;
5284 nvlist_t *origprops = NULL; /* existing properties */
5285 nvlist_t *origrecvd = NULL; /* existing received properties */
5286 boolean_t first_recvd_props = B_FALSE;
5287 boolean_t tofs_was_redacted;
5288 zfs_file_t *input_fp;
5289
5290 *read_bytes = 0;
5291 *errflags = 0;
5292 *errors = fnvlist_alloc();
5293 off = 0;
5294
5295 if ((input_fp = zfs_file_get(input_fd)) == NULL)
5296 return (SET_ERROR(EBADF));
5297
5298 noff = off = zfs_file_off(input_fp);
5299 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal,
5300 resumable, localprops, hidden_args, origin, &drc, input_fp,
5301 &off);
5302 if (error != 0)
5303 goto out;
5304 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
5305
5306 /*
5307 * Set properties before we receive the stream so that they are applied
5308 * to the new data. Note that we must call dmu_recv_stream() if
5309 * dmu_recv_begin() succeeds.
5310 */
5311 if (recvprops != NULL && !drc.drc_newfs) {
5312 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
5313 SPA_VERSION_RECVD_PROPS &&
5314 !dsl_prop_get_hasrecvd(tofs))
5315 first_recvd_props = B_TRUE;
5316
5317 /*
5318 * If new received properties are supplied, they are to
5319 * completely replace the existing received properties,
5320 * so stash away the existing ones.
5321 */
5322 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
5323 nvlist_t *errlist = NULL;
5324 /*
5325 * Don't bother writing a property if its value won't
5326 * change (and avoid the unnecessary security checks).
5327 *
5328 * The first receive after SPA_VERSION_RECVD_PROPS is a
5329 * special case where we blow away all local properties
5330 * regardless.
5331 */
5332 if (!first_recvd_props)
5333 props_reduce(recvprops, origrecvd);
5334 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
5335 (void) nvlist_merge(*errors, errlist, 0);
5336 nvlist_free(errlist);
5337
5338 if (clear_received_props(tofs, origrecvd,
5339 first_recvd_props ? NULL : recvprops) != 0)
5340 *errflags |= ZPROP_ERR_NOCLEAR;
5341 } else {
5342 *errflags |= ZPROP_ERR_NOCLEAR;
5343 }
5344 }
5345
5346 /*
5347 * Stash away existing properties so we can restore them on error unless
5348 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
5349 * case "origrecvd" will take care of that.
5350 */
5351 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
5352 objset_t *os;
5353 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
5354 if (dsl_prop_get_all(os, &origprops) != 0) {
5355 *errflags |= ZPROP_ERR_NOCLEAR;
5356 }
5357 dmu_objset_rele(os, FTAG);
5358 } else {
5359 *errflags |= ZPROP_ERR_NOCLEAR;
5360 }
5361 }
5362
5363 if (recvprops != NULL) {
5364 props_error = dsl_prop_set_hasrecvd(tofs);
5365
5366 if (props_error == 0) {
5367 recv_delayprops = extract_delay_props(recvprops);
5368 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5369 recvprops, *errors);
5370 }
5371 }
5372
5373 if (localprops != NULL) {
5374 nvlist_t *oprops = fnvlist_alloc();
5375 nvlist_t *xprops = fnvlist_alloc();
5376 nvpair_t *nvp = NULL;
5377
5378 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5379 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
5380 /* -x property */
5381 const char *name = nvpair_name(nvp);
5382 zfs_prop_t prop = zfs_name_to_prop(name);
5383 if (prop != ZPROP_USERPROP) {
5384 if (!zfs_prop_inheritable(prop))
5385 continue;
5386 } else if (!zfs_prop_user(name))
5387 continue;
5388 fnvlist_add_boolean(xprops, name);
5389 } else {
5390 /* -o property=value */
5391 fnvlist_add_nvpair(oprops, nvp);
5392 }
5393 }
5394
5395 local_delayprops = extract_delay_props(oprops);
5396 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5397 oprops, *errors);
5398 inherited_delayprops = extract_delay_props(xprops);
5399 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5400 xprops, *errors);
5401
5402 nvlist_free(oprops);
5403 nvlist_free(xprops);
5404 }
5405
5406 error = dmu_recv_stream(&drc, &off);
5407
5408 if (error == 0) {
5409 zfsvfs_t *zfsvfs = NULL;
5410 zvol_state_handle_t *zv = NULL;
5411
5412 if (getzfsvfs(tofs, &zfsvfs) == 0) {
5413 /* online recv */
5414 dsl_dataset_t *ds;
5415 int end_err;
5416 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
5417 begin_record->drr_u.drr_begin.
5418 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
5419
5420 ds = dmu_objset_ds(zfsvfs->z_os);
5421 error = zfs_suspend_fs(zfsvfs);
5422 /*
5423 * If the suspend fails, then the recv_end will
5424 * likely also fail, and clean up after itself.
5425 */
5426 end_err = dmu_recv_end(&drc, zfsvfs);
5427 /*
5428 * If the dataset was not redacted, but we received a
5429 * redacted stream onto it, we need to unmount the
5430 * dataset. Otherwise, resume the filesystem.
5431 */
5432 if (error == 0 && !drc.drc_newfs &&
5433 stream_is_redacted && !tofs_was_redacted) {
5434 error = zfs_end_fs(zfsvfs, ds);
5435 } else if (error == 0) {
5436 error = zfs_resume_fs(zfsvfs, ds);
5437 }
5438 error = error ? error : end_err;
5439 zfs_vfs_rele(zfsvfs);
5440 } else if ((zv = zvol_suspend(tofs)) != NULL) {
5441 error = dmu_recv_end(&drc, zvol_tag(zv));
5442 zvol_resume(zv);
5443 } else {
5444 error = dmu_recv_end(&drc, NULL);
5445 }
5446
5447 /* Set delayed properties now, after we're done receiving. */
5448 if (recv_delayprops != NULL && error == 0) {
5449 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5450 recv_delayprops, *errors);
5451 }
5452 if (local_delayprops != NULL && error == 0) {
5453 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5454 local_delayprops, *errors);
5455 }
5456 if (inherited_delayprops != NULL && error == 0) {
5457 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5458 inherited_delayprops, *errors);
5459 }
5460 }
5461
5462 /*
5463 * Merge delayed props back in with initial props, in case
5464 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
5465 * we have to make sure clear_received_props() includes
5466 * the delayed properties).
5467 *
5468 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
5469 * using ASSERT() will be just like a VERIFY.
5470 */
5471 if (recv_delayprops != NULL) {
5472 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5473 nvlist_free(recv_delayprops);
5474 }
5475 if (local_delayprops != NULL) {
5476 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5477 nvlist_free(local_delayprops);
5478 }
5479 if (inherited_delayprops != NULL) {
5480 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0);
5481 nvlist_free(inherited_delayprops);
5482 }
5483 *read_bytes = off - noff;
5484
5485 #ifdef ZFS_DEBUG
5486 if (zfs_ioc_recv_inject_err) {
5487 zfs_ioc_recv_inject_err = B_FALSE;
5488 error = 1;
5489 }
5490 #endif
5491
5492 /*
5493 * On error, restore the original props.
5494 */
5495 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5496 if (clear_received_props(tofs, recvprops, NULL) != 0) {
5497 /*
5498 * We failed to clear the received properties.
5499 * Since we may have left a $recvd value on the
5500 * system, we can't clear the $hasrecvd flag.
5501 */
5502 *errflags |= ZPROP_ERR_NORESTORE;
5503 } else if (first_recvd_props) {
5504 dsl_prop_unset_hasrecvd(tofs);
5505 }
5506
5507 if (origrecvd == NULL && !drc.drc_newfs) {
5508 /* We failed to stash the original properties. */
5509 *errflags |= ZPROP_ERR_NORESTORE;
5510 }
5511
5512 /*
5513 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5514 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5515 * explicitly if we're restoring local properties cleared in the
5516 * first new-style receive.
5517 */
5518 if (origrecvd != NULL &&
5519 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5520 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5521 origrecvd, NULL) != 0) {
5522 /*
5523 * We stashed the original properties but failed to
5524 * restore them.
5525 */
5526 *errflags |= ZPROP_ERR_NORESTORE;
5527 }
5528 }
5529 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5530 !first_recvd_props) {
5531 nvlist_t *setprops;
5532 nvlist_t *inheritprops;
5533 nvpair_t *nvp;
5534
5535 if (origprops == NULL) {
5536 /* We failed to stash the original properties. */
5537 *errflags |= ZPROP_ERR_NORESTORE;
5538 goto out;
5539 }
5540
5541 /* Restore original props */
5542 setprops = fnvlist_alloc();
5543 inheritprops = fnvlist_alloc();
5544 nvp = NULL;
5545 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5546 const char *name = nvpair_name(nvp);
5547 const char *source;
5548 nvlist_t *attrs;
5549
5550 if (!nvlist_exists(origprops, name)) {
5551 /*
5552 * Property was not present or was explicitly
5553 * inherited before the receive, restore this.
5554 */
5555 fnvlist_add_boolean(inheritprops, name);
5556 continue;
5557 }
5558 attrs = fnvlist_lookup_nvlist(origprops, name);
5559 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5560
5561 /* Skip received properties */
5562 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5563 continue;
5564
5565 if (strcmp(source, tofs) == 0) {
5566 /* Property was locally set */
5567 fnvlist_add_nvlist(setprops, name, attrs);
5568 } else {
5569 /* Property was implicitly inherited */
5570 fnvlist_add_boolean(inheritprops, name);
5571 }
5572 }
5573
5574 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5575 NULL) != 0)
5576 *errflags |= ZPROP_ERR_NORESTORE;
5577 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5578 NULL) != 0)
5579 *errflags |= ZPROP_ERR_NORESTORE;
5580
5581 nvlist_free(setprops);
5582 nvlist_free(inheritprops);
5583 }
5584 out:
5585 zfs_file_put(input_fp);
5586 nvlist_free(origrecvd);
5587 nvlist_free(origprops);
5588
5589 if (error == 0)
5590 error = props_error;
5591
5592 return (error);
5593 }
5594
5595 /*
5596 * inputs:
5597 * zc_name name of containing filesystem (unused)
5598 * zc_nvlist_src{_size} nvlist of properties to apply
5599 * zc_nvlist_conf{_size} nvlist of properties to exclude
5600 * (DATA_TYPE_BOOLEAN) and override (everything else)
5601 * zc_value name of snapshot to create
5602 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5603 * zc_cookie file descriptor to recv from
5604 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5605 * zc_guid force flag
5606 *
5607 * outputs:
5608 * zc_cookie number of bytes read
5609 * zc_obj zprop_errflags_t
5610 * zc_nvlist_dst{_size} error for each unapplied received property
5611 */
5612 static int
zfs_ioc_recv(zfs_cmd_t * zc)5613 zfs_ioc_recv(zfs_cmd_t *zc)
5614 {
5615 dmu_replay_record_t begin_record;
5616 nvlist_t *errors = NULL;
5617 nvlist_t *recvdprops = NULL;
5618 nvlist_t *localprops = NULL;
5619 const char *origin = NULL;
5620 char *tosnap;
5621 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5622 int error = 0;
5623
5624 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5625 strchr(zc->zc_value, '@') == NULL ||
5626 strchr(zc->zc_value, '%') != NULL) {
5627 return (SET_ERROR(EINVAL));
5628 }
5629
5630 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5631 tosnap = strchr(tofs, '@');
5632 *tosnap++ = '\0';
5633
5634 if (zc->zc_nvlist_src != 0 &&
5635 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5636 zc->zc_iflags, &recvdprops)) != 0) {
5637 goto out;
5638 }
5639
5640 if (zc->zc_nvlist_conf != 0 &&
5641 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5642 zc->zc_iflags, &localprops)) != 0) {
5643 goto out;
5644 }
5645
5646 if (zc->zc_string[0])
5647 origin = zc->zc_string;
5648
5649 begin_record.drr_type = DRR_BEGIN;
5650 begin_record.drr_payloadlen = 0;
5651 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5652
5653 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5654 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record,
5655 &zc->zc_cookie, &zc->zc_obj, &errors);
5656
5657 /*
5658 * Now that all props, initial and delayed, are set, report the prop
5659 * errors to the caller.
5660 */
5661 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5662 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5663 put_nvlist(zc, errors) != 0)) {
5664 /*
5665 * Caller made zc->zc_nvlist_dst less than the minimum expected
5666 * size or supplied an invalid address.
5667 */
5668 error = SET_ERROR(EINVAL);
5669 }
5670
5671 out:
5672 nvlist_free(errors);
5673 nvlist_free(recvdprops);
5674 nvlist_free(localprops);
5675
5676 return (error);
5677 }
5678
5679 /*
5680 * innvl: {
5681 * "snapname" -> full name of the snapshot to create
5682 * (optional) "props" -> received properties to set (nvlist)
5683 * (optional) "localprops" -> override and exclude properties (nvlist)
5684 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5685 * "begin_record" -> non-byteswapped dmu_replay_record_t
5686 * "input_fd" -> file descriptor to read stream from (int32)
5687 * (optional) "force" -> force flag (value ignored)
5688 * (optional) "heal" -> use send stream to heal data corruption
5689 * (optional) "resumable" -> resumable flag (value ignored)
5690 * (optional) "cleanup_fd" -> unused
5691 * (optional) "action_handle" -> unused
5692 * (optional) "hidden_args" -> { "wkeydata" -> value }
5693 * }
5694 *
5695 * outnvl: {
5696 * "read_bytes" -> number of bytes read
5697 * "error_flags" -> zprop_errflags_t
5698 * "errors" -> error for each unapplied received property (nvlist)
5699 * }
5700 */
5701 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5702 {"snapname", DATA_TYPE_STRING, 0},
5703 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5704 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5705 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5706 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5707 {"input_fd", DATA_TYPE_INT32, 0},
5708 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5709 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5710 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5711 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5712 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5713 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5714 };
5715
5716 static int
zfs_ioc_recv_new(const char * fsname,nvlist_t * innvl,nvlist_t * outnvl)5717 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5718 {
5719 dmu_replay_record_t *begin_record;
5720 uint_t begin_record_size;
5721 nvlist_t *errors = NULL;
5722 nvlist_t *recvprops = NULL;
5723 nvlist_t *localprops = NULL;
5724 nvlist_t *hidden_args = NULL;
5725 const char *snapname;
5726 const char *origin = NULL;
5727 char *tosnap;
5728 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5729 boolean_t force;
5730 boolean_t heal;
5731 boolean_t resumable;
5732 uint64_t read_bytes = 0;
5733 uint64_t errflags = 0;
5734 int input_fd = -1;
5735 int error;
5736
5737 snapname = fnvlist_lookup_string(innvl, "snapname");
5738
5739 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5740 strchr(snapname, '@') == NULL ||
5741 strchr(snapname, '%') != NULL) {
5742 return (SET_ERROR(EINVAL));
5743 }
5744
5745 (void) strlcpy(tofs, snapname, sizeof (tofs));
5746 tosnap = strchr(tofs, '@');
5747 *tosnap++ = '\0';
5748
5749 error = nvlist_lookup_string(innvl, "origin", &origin);
5750 if (error && error != ENOENT)
5751 return (error);
5752
5753 error = nvlist_lookup_byte_array(innvl, "begin_record",
5754 (uchar_t **)&begin_record, &begin_record_size);
5755 if (error != 0 || begin_record_size != sizeof (*begin_record))
5756 return (SET_ERROR(EINVAL));
5757
5758 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5759
5760 force = nvlist_exists(innvl, "force");
5761 heal = nvlist_exists(innvl, "heal");
5762 resumable = nvlist_exists(innvl, "resumable");
5763
5764 /* we still use "props" here for backwards compatibility */
5765 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5766 if (error && error != ENOENT)
5767 goto out;
5768
5769 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5770 if (error && error != ENOENT)
5771 goto out;
5772
5773 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5774 if (error && error != ENOENT)
5775 goto out;
5776
5777 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5778 hidden_args, force, heal, resumable, input_fd, begin_record,
5779 &read_bytes, &errflags, &errors);
5780
5781 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5782 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5783 fnvlist_add_nvlist(outnvl, "errors", errors);
5784
5785 out:
5786 nvlist_free(errors);
5787 nvlist_free(recvprops);
5788 nvlist_free(localprops);
5789 nvlist_free(hidden_args);
5790
5791 return (error);
5792 }
5793
5794 /*
5795 * When stack space is limited, we write replication stream data to the target
5796 * on a separate taskq thread, to make sure there's enough stack space.
5797 */
5798 #ifndef HAVE_LARGE_STACKS
5799 #define USE_SEND_TASKQ 1
5800 #endif
5801
5802 typedef struct dump_bytes_io {
5803 zfs_file_t *dbi_fp;
5804 caddr_t dbi_buf;
5805 int dbi_len;
5806 int dbi_err;
5807 } dump_bytes_io_t;
5808
5809 static void
dump_bytes_cb(void * arg)5810 dump_bytes_cb(void *arg)
5811 {
5812 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5813 zfs_file_t *fp;
5814 caddr_t buf;
5815
5816 fp = dbi->dbi_fp;
5817 buf = dbi->dbi_buf;
5818
5819 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
5820 }
5821
5822 typedef struct dump_bytes_arg {
5823 zfs_file_t *dba_fp;
5824 #ifdef USE_SEND_TASKQ
5825 taskq_t *dba_tq;
5826 taskq_ent_t dba_tqent;
5827 #endif
5828 } dump_bytes_arg_t;
5829
5830 static int
dump_bytes(objset_t * os,void * buf,int len,void * arg)5831 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5832 {
5833 dump_bytes_arg_t *dba = (dump_bytes_arg_t *)arg;
5834 dump_bytes_io_t dbi;
5835
5836 dbi.dbi_fp = dba->dba_fp;
5837 dbi.dbi_buf = buf;
5838 dbi.dbi_len = len;
5839
5840 #ifdef USE_SEND_TASKQ
5841 taskq_dispatch_ent(dba->dba_tq, dump_bytes_cb, &dbi, TQ_SLEEP,
5842 &dba->dba_tqent);
5843 taskq_wait(dba->dba_tq);
5844 #else
5845 dump_bytes_cb(&dbi);
5846 #endif
5847
5848 return (dbi.dbi_err);
5849 }
5850
5851 static int
dump_bytes_init(dump_bytes_arg_t * dba,int fd,dmu_send_outparams_t * out)5852 dump_bytes_init(dump_bytes_arg_t *dba, int fd, dmu_send_outparams_t *out)
5853 {
5854 zfs_file_t *fp = zfs_file_get(fd);
5855 if (fp == NULL)
5856 return (SET_ERROR(EBADF));
5857
5858 dba->dba_fp = fp;
5859 #ifdef USE_SEND_TASKQ
5860 dba->dba_tq = taskq_create("z_send", 1, defclsyspri, 0, 0, 0);
5861 taskq_init_ent(&dba->dba_tqent);
5862 #endif
5863
5864 memset(out, 0, sizeof (dmu_send_outparams_t));
5865 out->dso_outfunc = dump_bytes;
5866 out->dso_arg = dba;
5867 out->dso_dryrun = B_FALSE;
5868
5869 return (0);
5870 }
5871
5872 static void
dump_bytes_fini(dump_bytes_arg_t * dba)5873 dump_bytes_fini(dump_bytes_arg_t *dba)
5874 {
5875 zfs_file_put(dba->dba_fp);
5876 #ifdef USE_SEND_TASKQ
5877 taskq_destroy(dba->dba_tq);
5878 #endif
5879 }
5880
5881 /*
5882 * inputs:
5883 * zc_name name of snapshot to send
5884 * zc_cookie file descriptor to send stream to
5885 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5886 * zc_sendobj objsetid of snapshot to send
5887 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5888 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5889 * output size in zc_objset_type.
5890 * zc_flags lzc_send_flags
5891 *
5892 * outputs:
5893 * zc_objset_type estimated size, if zc_guid is set
5894 *
5895 * NOTE: This is no longer the preferred interface, any new functionality
5896 * should be added to zfs_ioc_send_new() instead.
5897 */
5898 static int
zfs_ioc_send(zfs_cmd_t * zc)5899 zfs_ioc_send(zfs_cmd_t *zc)
5900 {
5901 int error;
5902 offset_t off;
5903 boolean_t estimate = (zc->zc_guid != 0);
5904 boolean_t embedok = (zc->zc_flags & 0x1);
5905 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5906 boolean_t compressok = (zc->zc_flags & 0x4);
5907 boolean_t rawok = (zc->zc_flags & 0x8);
5908 boolean_t savedok = (zc->zc_flags & 0x10);
5909
5910 if (zc->zc_obj != 0) {
5911 dsl_pool_t *dp;
5912 dsl_dataset_t *tosnap;
5913
5914 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5915 if (error != 0)
5916 return (error);
5917
5918 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5919 if (error != 0) {
5920 dsl_pool_rele(dp, FTAG);
5921 return (error);
5922 }
5923
5924 if (dsl_dir_is_clone(tosnap->ds_dir))
5925 zc->zc_fromobj =
5926 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5927 dsl_dataset_rele(tosnap, FTAG);
5928 dsl_pool_rele(dp, FTAG);
5929 }
5930
5931 if (estimate) {
5932 dsl_pool_t *dp;
5933 dsl_dataset_t *tosnap;
5934 dsl_dataset_t *fromsnap = NULL;
5935
5936 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5937 if (error != 0)
5938 return (error);
5939
5940 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5941 FTAG, &tosnap);
5942 if (error != 0) {
5943 dsl_pool_rele(dp, FTAG);
5944 return (error);
5945 }
5946
5947 if (zc->zc_fromobj != 0) {
5948 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5949 FTAG, &fromsnap);
5950 if (error != 0) {
5951 dsl_dataset_rele(tosnap, FTAG);
5952 dsl_pool_rele(dp, FTAG);
5953 return (error);
5954 }
5955 }
5956
5957 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5958 compressok || rawok, savedok, &zc->zc_objset_type);
5959
5960 if (fromsnap != NULL)
5961 dsl_dataset_rele(fromsnap, FTAG);
5962 dsl_dataset_rele(tosnap, FTAG);
5963 dsl_pool_rele(dp, FTAG);
5964 } else {
5965 dump_bytes_arg_t dba;
5966 dmu_send_outparams_t out;
5967 error = dump_bytes_init(&dba, zc->zc_cookie, &out);
5968 if (error)
5969 return (error);
5970
5971 off = zfs_file_off(dba.dba_fp);
5972 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5973 zc->zc_fromobj, embedok, large_block_ok, compressok,
5974 rawok, savedok, zc->zc_cookie, &off, &out);
5975
5976 dump_bytes_fini(&dba);
5977 }
5978 return (error);
5979 }
5980
5981 /*
5982 * inputs:
5983 * zc_name name of snapshot on which to report progress
5984 * zc_cookie file descriptor of send stream
5985 *
5986 * outputs:
5987 * zc_cookie number of bytes written in send stream thus far
5988 * zc_objset_type logical size of data traversed by send thus far
5989 */
5990 static int
zfs_ioc_send_progress(zfs_cmd_t * zc)5991 zfs_ioc_send_progress(zfs_cmd_t *zc)
5992 {
5993 dsl_pool_t *dp;
5994 dsl_dataset_t *ds;
5995 dmu_sendstatus_t *dsp = NULL;
5996 int error;
5997
5998 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5999 if (error != 0)
6000 return (error);
6001
6002 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
6003 if (error != 0) {
6004 dsl_pool_rele(dp, FTAG);
6005 return (error);
6006 }
6007
6008 mutex_enter(&ds->ds_sendstream_lock);
6009
6010 /*
6011 * Iterate over all the send streams currently active on this dataset.
6012 * If there's one which matches the specified file descriptor _and_ the
6013 * stream was started by the current process, return the progress of
6014 * that stream.
6015 */
6016
6017 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
6018 dsp = list_next(&ds->ds_sendstreams, dsp)) {
6019 if (dsp->dss_outfd == zc->zc_cookie &&
6020 zfs_proc_is_caller(dsp->dss_proc))
6021 break;
6022 }
6023
6024 if (dsp != NULL) {
6025 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
6026 0, 0);
6027 /* This is the closest thing we have to atomic_read_64. */
6028 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
6029 } else {
6030 error = SET_ERROR(ENOENT);
6031 }
6032
6033 mutex_exit(&ds->ds_sendstream_lock);
6034 dsl_dataset_rele(ds, FTAG);
6035 dsl_pool_rele(dp, FTAG);
6036 return (error);
6037 }
6038
6039 static int
zfs_ioc_inject_fault(zfs_cmd_t * zc)6040 zfs_ioc_inject_fault(zfs_cmd_t *zc)
6041 {
6042 int id, error;
6043
6044 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
6045 &zc->zc_inject_record);
6046
6047 if (error == 0)
6048 zc->zc_guid = (uint64_t)id;
6049
6050 return (error);
6051 }
6052
6053 static int
zfs_ioc_clear_fault(zfs_cmd_t * zc)6054 zfs_ioc_clear_fault(zfs_cmd_t *zc)
6055 {
6056 return (zio_clear_fault((int)zc->zc_guid));
6057 }
6058
6059 static int
zfs_ioc_inject_list_next(zfs_cmd_t * zc)6060 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
6061 {
6062 int id = (int)zc->zc_guid;
6063 int error;
6064
6065 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
6066 &zc->zc_inject_record);
6067
6068 zc->zc_guid = id;
6069
6070 return (error);
6071 }
6072
6073 static int
zfs_ioc_error_log(zfs_cmd_t * zc)6074 zfs_ioc_error_log(zfs_cmd_t *zc)
6075 {
6076 spa_t *spa;
6077 int error;
6078
6079 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
6080 return (error);
6081
6082 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
6083 &zc->zc_nvlist_dst_size);
6084
6085 spa_close(spa, FTAG);
6086
6087 return (error);
6088 }
6089
6090 static int
zfs_ioc_clear(zfs_cmd_t * zc)6091 zfs_ioc_clear(zfs_cmd_t *zc)
6092 {
6093 spa_t *spa;
6094 vdev_t *vd;
6095 int error;
6096
6097 /*
6098 * On zpool clear we also fix up missing slogs
6099 */
6100 mutex_enter(&spa_namespace_lock);
6101 spa = spa_lookup(zc->zc_name);
6102 if (spa == NULL) {
6103 mutex_exit(&spa_namespace_lock);
6104 return (SET_ERROR(EIO));
6105 }
6106 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
6107 /* we need to let spa_open/spa_load clear the chains */
6108 spa_set_log_state(spa, SPA_LOG_CLEAR);
6109 }
6110 spa->spa_last_open_failed = 0;
6111 mutex_exit(&spa_namespace_lock);
6112
6113 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
6114 error = spa_open(zc->zc_name, &spa, FTAG);
6115 } else {
6116 nvlist_t *policy;
6117 nvlist_t *config = NULL;
6118
6119 if (zc->zc_nvlist_src == 0)
6120 return (SET_ERROR(EINVAL));
6121
6122 if ((error = get_nvlist(zc->zc_nvlist_src,
6123 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
6124 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
6125 policy, &config);
6126 if (config != NULL) {
6127 int err;
6128
6129 if ((err = put_nvlist(zc, config)) != 0)
6130 error = err;
6131 nvlist_free(config);
6132 }
6133 nvlist_free(policy);
6134 }
6135 }
6136
6137 if (error != 0)
6138 return (error);
6139
6140 /*
6141 * If multihost is enabled, resuming I/O is unsafe as another
6142 * host may have imported the pool. Check for remote activity.
6143 */
6144 if (spa_multihost(spa) && spa_suspended(spa) &&
6145 spa_mmp_remote_host_activity(spa)) {
6146 spa_close(spa, FTAG);
6147 return (SET_ERROR(EREMOTEIO));
6148 }
6149
6150 spa_vdev_state_enter(spa, SCL_NONE);
6151
6152 if (zc->zc_guid == 0) {
6153 vd = NULL;
6154 } else {
6155 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
6156 if (vd == NULL) {
6157 error = SET_ERROR(ENODEV);
6158 (void) spa_vdev_state_exit(spa, NULL, error);
6159 spa_close(spa, FTAG);
6160 return (error);
6161 }
6162 }
6163
6164 vdev_clear(spa, vd);
6165
6166 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
6167 NULL : spa->spa_root_vdev, 0);
6168
6169 /*
6170 * Resume any suspended I/Os.
6171 */
6172 if (zio_resume(spa) != 0)
6173 error = SET_ERROR(EIO);
6174
6175 spa_close(spa, FTAG);
6176
6177 return (error);
6178 }
6179
6180 /*
6181 * Reopen all the vdevs associated with the pool.
6182 *
6183 * innvl: {
6184 * "scrub_restart" -> when true and scrub is running, allow to restart
6185 * scrub as the side effect of the reopen (boolean).
6186 * }
6187 *
6188 * outnvl is unused
6189 */
6190 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
6191 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
6192 };
6193
6194 static int
zfs_ioc_pool_reopen(const char * pool,nvlist_t * innvl,nvlist_t * outnvl)6195 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
6196 {
6197 (void) outnvl;
6198 spa_t *spa;
6199 int error;
6200 boolean_t rc, scrub_restart = B_TRUE;
6201
6202 if (innvl) {
6203 error = nvlist_lookup_boolean_value(innvl,
6204 "scrub_restart", &rc);
6205 if (error == 0)
6206 scrub_restart = rc;
6207 }
6208
6209 error = spa_open(pool, &spa, FTAG);
6210 if (error != 0)
6211 return (error);
6212
6213 spa_vdev_state_enter(spa, SCL_NONE);
6214
6215 /*
6216 * If the scrub_restart flag is B_FALSE and a scrub is already
6217 * in progress then set spa_scrub_reopen flag to B_TRUE so that
6218 * we don't restart the scrub as a side effect of the reopen.
6219 * Otherwise, let vdev_open() decided if a resilver is required.
6220 */
6221
6222 spa->spa_scrub_reopen = (!scrub_restart &&
6223 dsl_scan_scrubbing(spa->spa_dsl_pool));
6224 vdev_reopen(spa->spa_root_vdev);
6225 spa->spa_scrub_reopen = B_FALSE;
6226
6227 (void) spa_vdev_state_exit(spa, NULL, 0);
6228 spa_close(spa, FTAG);
6229 return (0);
6230 }
6231
6232 /*
6233 * inputs:
6234 * zc_name name of filesystem
6235 *
6236 * outputs:
6237 * zc_string name of conflicting snapshot, if there is one
6238 */
6239 static int
zfs_ioc_promote(zfs_cmd_t * zc)6240 zfs_ioc_promote(zfs_cmd_t *zc)
6241 {
6242 dsl_pool_t *dp;
6243 dsl_dataset_t *ds, *ods;
6244 char origin[ZFS_MAX_DATASET_NAME_LEN];
6245 char *cp;
6246 int error;
6247
6248 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6249 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
6250 strchr(zc->zc_name, '%'))
6251 return (SET_ERROR(EINVAL));
6252
6253 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6254 if (error != 0)
6255 return (error);
6256
6257 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
6258 if (error != 0) {
6259 dsl_pool_rele(dp, FTAG);
6260 return (error);
6261 }
6262
6263 if (!dsl_dir_is_clone(ds->ds_dir)) {
6264 dsl_dataset_rele(ds, FTAG);
6265 dsl_pool_rele(dp, FTAG);
6266 return (SET_ERROR(EINVAL));
6267 }
6268
6269 error = dsl_dataset_hold_obj(dp,
6270 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
6271 if (error != 0) {
6272 dsl_dataset_rele(ds, FTAG);
6273 dsl_pool_rele(dp, FTAG);
6274 return (error);
6275 }
6276
6277 dsl_dataset_name(ods, origin);
6278 dsl_dataset_rele(ods, FTAG);
6279 dsl_dataset_rele(ds, FTAG);
6280 dsl_pool_rele(dp, FTAG);
6281
6282 /*
6283 * We don't need to unmount *all* the origin fs's snapshots, but
6284 * it's easier.
6285 */
6286 cp = strchr(origin, '@');
6287 if (cp)
6288 *cp = '\0';
6289 (void) dmu_objset_find(origin,
6290 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
6291 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
6292 }
6293
6294 /*
6295 * Retrieve a single {user|group|project}{used|quota}@... property.
6296 *
6297 * inputs:
6298 * zc_name name of filesystem
6299 * zc_objset_type zfs_userquota_prop_t
6300 * zc_value domain name (eg. "S-1-234-567-89")
6301 * zc_guid RID/UID/GID
6302 *
6303 * outputs:
6304 * zc_cookie property value
6305 */
6306 static int
zfs_ioc_userspace_one(zfs_cmd_t * zc)6307 zfs_ioc_userspace_one(zfs_cmd_t *zc)
6308 {
6309 zfsvfs_t *zfsvfs;
6310 int error;
6311
6312 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
6313 return (SET_ERROR(EINVAL));
6314
6315 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
6316 if (error != 0)
6317 return (error);
6318
6319 error = zfs_userspace_one(zfsvfs,
6320 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
6321 zfsvfs_rele(zfsvfs, FTAG);
6322
6323 return (error);
6324 }
6325
6326 /*
6327 * inputs:
6328 * zc_name name of filesystem
6329 * zc_cookie zap cursor
6330 * zc_objset_type zfs_userquota_prop_t
6331 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
6332 *
6333 * outputs:
6334 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
6335 * zc_cookie zap cursor
6336 */
6337 static int
zfs_ioc_userspace_many(zfs_cmd_t * zc)6338 zfs_ioc_userspace_many(zfs_cmd_t *zc)
6339 {
6340 zfsvfs_t *zfsvfs;
6341 int bufsize = zc->zc_nvlist_dst_size;
6342
6343 if (bufsize <= 0)
6344 return (SET_ERROR(ENOMEM));
6345
6346 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
6347 if (error != 0)
6348 return (error);
6349
6350 void *buf = vmem_alloc(bufsize, KM_SLEEP);
6351
6352 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
6353 buf, &zc->zc_nvlist_dst_size, &zc->zc_guid);
6354
6355 if (error == 0) {
6356 error = xcopyout(buf,
6357 (void *)(uintptr_t)zc->zc_nvlist_dst,
6358 zc->zc_nvlist_dst_size);
6359 }
6360 vmem_free(buf, bufsize);
6361 zfsvfs_rele(zfsvfs, FTAG);
6362
6363 return (error);
6364 }
6365
6366 /*
6367 * inputs:
6368 * zc_name name of filesystem
6369 *
6370 * outputs:
6371 * none
6372 */
6373 static int
zfs_ioc_userspace_upgrade(zfs_cmd_t * zc)6374 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
6375 {
6376 int error = 0;
6377 zfsvfs_t *zfsvfs;
6378
6379 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
6380 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
6381 /*
6382 * If userused is not enabled, it may be because the
6383 * objset needs to be closed & reopened (to grow the
6384 * objset_phys_t). Suspend/resume the fs will do that.
6385 */
6386 dsl_dataset_t *ds, *newds;
6387
6388 ds = dmu_objset_ds(zfsvfs->z_os);
6389 error = zfs_suspend_fs(zfsvfs);
6390 if (error == 0) {
6391 dmu_objset_refresh_ownership(ds, &newds,
6392 B_TRUE, zfsvfs);
6393 error = zfs_resume_fs(zfsvfs, newds);
6394 }
6395 }
6396 if (error == 0) {
6397 mutex_enter(&zfsvfs->z_os->os_upgrade_lock);
6398 if (zfsvfs->z_os->os_upgrade_id == 0) {
6399 /* clear potential error code and retry */
6400 zfsvfs->z_os->os_upgrade_status = 0;
6401 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6402
6403 dsl_pool_config_enter(
6404 dmu_objset_pool(zfsvfs->z_os), FTAG);
6405 dmu_objset_userspace_upgrade(zfsvfs->z_os);
6406 dsl_pool_config_exit(
6407 dmu_objset_pool(zfsvfs->z_os), FTAG);
6408 } else {
6409 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6410 }
6411
6412 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq,
6413 zfsvfs->z_os->os_upgrade_id);
6414 error = zfsvfs->z_os->os_upgrade_status;
6415 }
6416 zfs_vfs_rele(zfsvfs);
6417 } else {
6418 objset_t *os;
6419
6420 /* XXX kind of reading contents without owning */
6421 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6422 if (error != 0)
6423 return (error);
6424
6425 mutex_enter(&os->os_upgrade_lock);
6426 if (os->os_upgrade_id == 0) {
6427 /* clear potential error code and retry */
6428 os->os_upgrade_status = 0;
6429 mutex_exit(&os->os_upgrade_lock);
6430
6431 dmu_objset_userspace_upgrade(os);
6432 } else {
6433 mutex_exit(&os->os_upgrade_lock);
6434 }
6435
6436 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6437
6438 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6439 error = os->os_upgrade_status;
6440
6441 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT,
6442 FTAG);
6443 }
6444 return (error);
6445 }
6446
6447 /*
6448 * inputs:
6449 * zc_name name of filesystem
6450 *
6451 * outputs:
6452 * none
6453 */
6454 static int
zfs_ioc_id_quota_upgrade(zfs_cmd_t * zc)6455 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
6456 {
6457 objset_t *os;
6458 int error;
6459
6460 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6461 if (error != 0)
6462 return (error);
6463
6464 if (dmu_objset_userobjspace_upgradable(os) ||
6465 dmu_objset_projectquota_upgradable(os)) {
6466 mutex_enter(&os->os_upgrade_lock);
6467 if (os->os_upgrade_id == 0) {
6468 /* clear potential error code and retry */
6469 os->os_upgrade_status = 0;
6470 mutex_exit(&os->os_upgrade_lock);
6471
6472 dmu_objset_id_quota_upgrade(os);
6473 } else {
6474 mutex_exit(&os->os_upgrade_lock);
6475 }
6476
6477 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6478
6479 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6480 error = os->os_upgrade_status;
6481 } else {
6482 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6483 }
6484
6485 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
6486
6487 return (error);
6488 }
6489
6490 static int
zfs_ioc_share(zfs_cmd_t * zc)6491 zfs_ioc_share(zfs_cmd_t *zc)
6492 {
6493 return (SET_ERROR(ENOSYS));
6494 }
6495
6496 /*
6497 * inputs:
6498 * zc_name name of containing filesystem
6499 * zc_obj object # beyond which we want next in-use object #
6500 *
6501 * outputs:
6502 * zc_obj next in-use object #
6503 */
6504 static int
zfs_ioc_next_obj(zfs_cmd_t * zc)6505 zfs_ioc_next_obj(zfs_cmd_t *zc)
6506 {
6507 objset_t *os = NULL;
6508 int error;
6509
6510 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
6511 if (error != 0)
6512 return (error);
6513
6514 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
6515
6516 dmu_objset_rele(os, FTAG);
6517 return (error);
6518 }
6519
6520 /*
6521 * inputs:
6522 * zc_name name of filesystem
6523 * zc_value prefix name for snapshot
6524 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
6525 *
6526 * outputs:
6527 * zc_value short name of new snapshot
6528 */
6529 static int
zfs_ioc_tmp_snapshot(zfs_cmd_t * zc)6530 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
6531 {
6532 char *snap_name;
6533 char *hold_name;
6534 minor_t minor;
6535
6536 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
6537 if (fp == NULL)
6538 return (SET_ERROR(EBADF));
6539
6540 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
6541 (u_longlong_t)ddi_get_lbolt64());
6542 hold_name = kmem_asprintf("%%%s", zc->zc_value);
6543
6544 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
6545 hold_name);
6546 if (error == 0)
6547 (void) strlcpy(zc->zc_value, snap_name,
6548 sizeof (zc->zc_value));
6549 kmem_strfree(snap_name);
6550 kmem_strfree(hold_name);
6551 zfs_onexit_fd_rele(fp);
6552 return (error);
6553 }
6554
6555 /*
6556 * inputs:
6557 * zc_name name of "to" snapshot
6558 * zc_value name of "from" snapshot
6559 * zc_cookie file descriptor to write diff data on
6560 *
6561 * outputs:
6562 * dmu_diff_record_t's to the file descriptor
6563 */
6564 static int
zfs_ioc_diff(zfs_cmd_t * zc)6565 zfs_ioc_diff(zfs_cmd_t *zc)
6566 {
6567 zfs_file_t *fp;
6568 offset_t off;
6569 int error;
6570
6571 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
6572 return (SET_ERROR(EBADF));
6573
6574 off = zfs_file_off(fp);
6575 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
6576
6577 zfs_file_put(fp);
6578
6579 return (error);
6580 }
6581
6582 static int
zfs_ioc_smb_acl(zfs_cmd_t * zc)6583 zfs_ioc_smb_acl(zfs_cmd_t *zc)
6584 {
6585 return (SET_ERROR(ENOTSUP));
6586 }
6587
6588 /*
6589 * innvl: {
6590 * "holds" -> { snapname -> holdname (string), ... }
6591 * (optional) "cleanup_fd" -> fd (int32)
6592 * }
6593 *
6594 * outnvl: {
6595 * snapname -> error value (int32)
6596 * ...
6597 * }
6598 */
6599 static const zfs_ioc_key_t zfs_keys_hold[] = {
6600 {"holds", DATA_TYPE_NVLIST, 0},
6601 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6602 };
6603
6604 static int
zfs_ioc_hold(const char * pool,nvlist_t * args,nvlist_t * errlist)6605 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6606 {
6607 (void) pool;
6608 nvpair_t *pair;
6609 nvlist_t *holds;
6610 int cleanup_fd = -1;
6611 int error;
6612 minor_t minor = 0;
6613 zfs_file_t *fp = NULL;
6614
6615 holds = fnvlist_lookup_nvlist(args, "holds");
6616
6617 /* make sure the user didn't pass us any invalid (empty) tags */
6618 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6619 pair = nvlist_next_nvpair(holds, pair)) {
6620 const char *htag;
6621
6622 error = nvpair_value_string(pair, &htag);
6623 if (error != 0)
6624 return (SET_ERROR(error));
6625
6626 if (strlen(htag) == 0)
6627 return (SET_ERROR(EINVAL));
6628 }
6629
6630 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6631 fp = zfs_onexit_fd_hold(cleanup_fd, &minor);
6632 if (fp == NULL)
6633 return (SET_ERROR(EBADF));
6634 }
6635
6636 error = dsl_dataset_user_hold(holds, minor, errlist);
6637 if (fp != NULL) {
6638 ASSERT3U(minor, !=, 0);
6639 zfs_onexit_fd_rele(fp);
6640 }
6641 return (SET_ERROR(error));
6642 }
6643
6644 /*
6645 * innvl is not used.
6646 *
6647 * outnvl: {
6648 * holdname -> time added (uint64 seconds since epoch)
6649 * ...
6650 * }
6651 */
6652 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6653 /* no nvl keys */
6654 };
6655
6656 static int
zfs_ioc_get_holds(const char * snapname,nvlist_t * args,nvlist_t * outnvl)6657 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6658 {
6659 (void) args;
6660 return (dsl_dataset_get_holds(snapname, outnvl));
6661 }
6662
6663 /*
6664 * innvl: {
6665 * snapname -> { holdname, ... }
6666 * ...
6667 * }
6668 *
6669 * outnvl: {
6670 * snapname -> error value (int32)
6671 * ...
6672 * }
6673 */
6674 static const zfs_ioc_key_t zfs_keys_release[] = {
6675 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6676 };
6677
6678 static int
zfs_ioc_release(const char * pool,nvlist_t * holds,nvlist_t * errlist)6679 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6680 {
6681 (void) pool;
6682 return (dsl_dataset_user_release(holds, errlist));
6683 }
6684
6685 /*
6686 * inputs:
6687 * zc_guid flags (ZEVENT_NONBLOCK)
6688 * zc_cleanup_fd zevent file descriptor
6689 *
6690 * outputs:
6691 * zc_nvlist_dst next nvlist event
6692 * zc_cookie dropped events since last get
6693 */
6694 static int
zfs_ioc_events_next(zfs_cmd_t * zc)6695 zfs_ioc_events_next(zfs_cmd_t *zc)
6696 {
6697 zfs_zevent_t *ze;
6698 nvlist_t *event = NULL;
6699 minor_t minor;
6700 uint64_t dropped = 0;
6701 int error;
6702
6703 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6704 if (fp == NULL)
6705 return (SET_ERROR(EBADF));
6706
6707 do {
6708 error = zfs_zevent_next(ze, &event,
6709 &zc->zc_nvlist_dst_size, &dropped);
6710 if (event != NULL) {
6711 zc->zc_cookie = dropped;
6712 error = put_nvlist(zc, event);
6713 nvlist_free(event);
6714 }
6715
6716 if (zc->zc_guid & ZEVENT_NONBLOCK)
6717 break;
6718
6719 if ((error == 0) || (error != ENOENT))
6720 break;
6721
6722 error = zfs_zevent_wait(ze);
6723 if (error != 0)
6724 break;
6725 } while (1);
6726
6727 zfs_zevent_fd_rele(fp);
6728
6729 return (error);
6730 }
6731
6732 /*
6733 * outputs:
6734 * zc_cookie cleared events count
6735 */
6736 static int
zfs_ioc_events_clear(zfs_cmd_t * zc)6737 zfs_ioc_events_clear(zfs_cmd_t *zc)
6738 {
6739 uint_t count;
6740
6741 zfs_zevent_drain_all(&count);
6742 zc->zc_cookie = count;
6743
6744 return (0);
6745 }
6746
6747 /*
6748 * inputs:
6749 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6750 * zc_cleanup zevent file descriptor
6751 */
6752 static int
zfs_ioc_events_seek(zfs_cmd_t * zc)6753 zfs_ioc_events_seek(zfs_cmd_t *zc)
6754 {
6755 zfs_zevent_t *ze;
6756 minor_t minor;
6757 int error;
6758
6759 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6760 if (fp == NULL)
6761 return (SET_ERROR(EBADF));
6762
6763 error = zfs_zevent_seek(ze, zc->zc_guid);
6764 zfs_zevent_fd_rele(fp);
6765
6766 return (error);
6767 }
6768
6769 /*
6770 * inputs:
6771 * zc_name name of later filesystem or snapshot
6772 * zc_value full name of old snapshot or bookmark
6773 *
6774 * outputs:
6775 * zc_cookie space in bytes
6776 * zc_objset_type compressed space in bytes
6777 * zc_perm_action uncompressed space in bytes
6778 */
6779 static int
zfs_ioc_space_written(zfs_cmd_t * zc)6780 zfs_ioc_space_written(zfs_cmd_t *zc)
6781 {
6782 int error;
6783 dsl_pool_t *dp;
6784 dsl_dataset_t *new;
6785
6786 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6787 if (error != 0)
6788 return (error);
6789 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6790 if (error != 0) {
6791 dsl_pool_rele(dp, FTAG);
6792 return (error);
6793 }
6794 if (strchr(zc->zc_value, '#') != NULL) {
6795 zfs_bookmark_phys_t bmp;
6796 error = dsl_bookmark_lookup(dp, zc->zc_value,
6797 new, &bmp);
6798 if (error == 0) {
6799 error = dsl_dataset_space_written_bookmark(&bmp, new,
6800 &zc->zc_cookie,
6801 &zc->zc_objset_type, &zc->zc_perm_action);
6802 }
6803 } else {
6804 dsl_dataset_t *old;
6805 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6806
6807 if (error == 0) {
6808 error = dsl_dataset_space_written(old, new,
6809 &zc->zc_cookie,
6810 &zc->zc_objset_type, &zc->zc_perm_action);
6811 dsl_dataset_rele(old, FTAG);
6812 }
6813 }
6814 dsl_dataset_rele(new, FTAG);
6815 dsl_pool_rele(dp, FTAG);
6816 return (error);
6817 }
6818
6819 /*
6820 * innvl: {
6821 * "firstsnap" -> snapshot name
6822 * }
6823 *
6824 * outnvl: {
6825 * "used" -> space in bytes
6826 * "compressed" -> compressed space in bytes
6827 * "uncompressed" -> uncompressed space in bytes
6828 * }
6829 */
6830 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6831 {"firstsnap", DATA_TYPE_STRING, 0},
6832 };
6833
6834 static int
zfs_ioc_space_snaps(const char * lastsnap,nvlist_t * innvl,nvlist_t * outnvl)6835 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6836 {
6837 int error;
6838 dsl_pool_t *dp;
6839 dsl_dataset_t *new, *old;
6840 const char *firstsnap;
6841 uint64_t used, comp, uncomp;
6842
6843 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6844
6845 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6846 if (error != 0)
6847 return (error);
6848
6849 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6850 if (error == 0 && !new->ds_is_snapshot) {
6851 dsl_dataset_rele(new, FTAG);
6852 error = SET_ERROR(EINVAL);
6853 }
6854 if (error != 0) {
6855 dsl_pool_rele(dp, FTAG);
6856 return (error);
6857 }
6858 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6859 if (error == 0 && !old->ds_is_snapshot) {
6860 dsl_dataset_rele(old, FTAG);
6861 error = SET_ERROR(EINVAL);
6862 }
6863 if (error != 0) {
6864 dsl_dataset_rele(new, FTAG);
6865 dsl_pool_rele(dp, FTAG);
6866 return (error);
6867 }
6868
6869 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6870 dsl_dataset_rele(old, FTAG);
6871 dsl_dataset_rele(new, FTAG);
6872 dsl_pool_rele(dp, FTAG);
6873 fnvlist_add_uint64(outnvl, "used", used);
6874 fnvlist_add_uint64(outnvl, "compressed", comp);
6875 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6876 return (error);
6877 }
6878
6879 /*
6880 * innvl: {
6881 * "fd" -> file descriptor to write stream to (int32)
6882 * (optional) "fromsnap" -> full snap name to send an incremental from
6883 * (optional) "largeblockok" -> (value ignored)
6884 * indicates that blocks > 128KB are permitted
6885 * (optional) "embedok" -> (value ignored)
6886 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6887 * (optional) "compressok" -> (value ignored)
6888 * presence indicates compressed DRR_WRITE records are permitted
6889 * (optional) "rawok" -> (value ignored)
6890 * presence indicates raw encrypted records should be used.
6891 * (optional) "savedok" -> (value ignored)
6892 * presence indicates we should send a partially received snapshot
6893 * (optional) "resume_object" and "resume_offset" -> (uint64)
6894 * if present, resume send stream from specified object and offset.
6895 * (optional) "redactbook" -> (string)
6896 * if present, use this bookmark's redaction list to generate a redacted
6897 * send stream
6898 * }
6899 *
6900 * outnvl is unused
6901 */
6902 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6903 {"fd", DATA_TYPE_INT32, 0},
6904 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6905 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6906 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6907 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6908 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6909 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6910 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6911 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6912 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6913 };
6914
6915 static int
zfs_ioc_send_new(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)6916 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6917 {
6918 (void) outnvl;
6919 int error;
6920 offset_t off;
6921 const char *fromname = NULL;
6922 int fd;
6923 boolean_t largeblockok;
6924 boolean_t embedok;
6925 boolean_t compressok;
6926 boolean_t rawok;
6927 boolean_t savedok;
6928 uint64_t resumeobj = 0;
6929 uint64_t resumeoff = 0;
6930 const char *redactbook = NULL;
6931
6932 fd = fnvlist_lookup_int32(innvl, "fd");
6933
6934 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6935
6936 largeblockok = nvlist_exists(innvl, "largeblockok");
6937 embedok = nvlist_exists(innvl, "embedok");
6938 compressok = nvlist_exists(innvl, "compressok");
6939 rawok = nvlist_exists(innvl, "rawok");
6940 savedok = nvlist_exists(innvl, "savedok");
6941
6942 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6943 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6944
6945 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6946
6947 dump_bytes_arg_t dba;
6948 dmu_send_outparams_t out;
6949 error = dump_bytes_init(&dba, fd, &out);
6950 if (error)
6951 return (error);
6952
6953 off = zfs_file_off(dba.dba_fp);
6954 error = dmu_send(snapname, fromname, embedok, largeblockok,
6955 compressok, rawok, savedok, resumeobj, resumeoff,
6956 redactbook, fd, &off, &out);
6957
6958 dump_bytes_fini(&dba);
6959
6960 return (error);
6961 }
6962
6963 static int
send_space_sum(objset_t * os,void * buf,int len,void * arg)6964 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6965 {
6966 (void) os, (void) buf;
6967 uint64_t *size = arg;
6968
6969 *size += len;
6970 return (0);
6971 }
6972
6973 /*
6974 * Determine approximately how large a zfs send stream will be -- the number
6975 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6976 *
6977 * innvl: {
6978 * (optional) "from" -> full snap or bookmark name to send an incremental
6979 * from
6980 * (optional) "largeblockok" -> (value ignored)
6981 * indicates that blocks > 128KB are permitted
6982 * (optional) "embedok" -> (value ignored)
6983 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6984 * (optional) "compressok" -> (value ignored)
6985 * presence indicates compressed DRR_WRITE records are permitted
6986 * (optional) "rawok" -> (value ignored)
6987 * presence indicates raw encrypted records should be used.
6988 * (optional) "resume_object" and "resume_offset" -> (uint64)
6989 * if present, resume send stream from specified object and offset.
6990 * (optional) "fd" -> file descriptor to use as a cookie for progress
6991 * tracking (int32)
6992 * }
6993 *
6994 * outnvl: {
6995 * "space" -> bytes of space (uint64)
6996 * }
6997 */
6998 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6999 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
7000 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
7001 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
7002 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
7003 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
7004 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
7005 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
7006 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
7007 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
7008 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
7009 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
7010 };
7011
7012 static int
zfs_ioc_send_space(const char * snapname,nvlist_t * innvl,nvlist_t * outnvl)7013 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
7014 {
7015 dsl_pool_t *dp;
7016 dsl_dataset_t *tosnap;
7017 dsl_dataset_t *fromsnap = NULL;
7018 int error;
7019 const char *fromname = NULL;
7020 const char *redactlist_book = NULL;
7021 boolean_t largeblockok;
7022 boolean_t embedok;
7023 boolean_t compressok;
7024 boolean_t rawok;
7025 boolean_t savedok;
7026 uint64_t space = 0;
7027 boolean_t full_estimate = B_FALSE;
7028 uint64_t resumeobj = 0;
7029 uint64_t resumeoff = 0;
7030 uint64_t resume_bytes = 0;
7031 int32_t fd = -1;
7032 zfs_bookmark_phys_t zbm = {0};
7033
7034 error = dsl_pool_hold(snapname, FTAG, &dp);
7035 if (error != 0)
7036 return (error);
7037
7038 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
7039 if (error != 0) {
7040 dsl_pool_rele(dp, FTAG);
7041 return (error);
7042 }
7043 (void) nvlist_lookup_int32(innvl, "fd", &fd);
7044
7045 largeblockok = nvlist_exists(innvl, "largeblockok");
7046 embedok = nvlist_exists(innvl, "embedok");
7047 compressok = nvlist_exists(innvl, "compressok");
7048 rawok = nvlist_exists(innvl, "rawok");
7049 savedok = nvlist_exists(innvl, "savedok");
7050 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
7051 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
7052 &redactlist_book) == 0);
7053
7054 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
7055 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
7056 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
7057
7058 if (altbook) {
7059 full_estimate = B_TRUE;
7060 } else if (from) {
7061 if (strchr(fromname, '#')) {
7062 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
7063
7064 /*
7065 * dsl_bookmark_lookup() will fail with EXDEV if
7066 * the from-bookmark and tosnap are at the same txg.
7067 * However, it's valid to do a send (and therefore,
7068 * a send estimate) from and to the same time point,
7069 * if the bookmark is redacted (the incremental send
7070 * can change what's redacted on the target). In
7071 * this case, dsl_bookmark_lookup() fills in zbm
7072 * but returns EXDEV. Ignore this error.
7073 */
7074 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
7075 zbm.zbm_guid ==
7076 dsl_dataset_phys(tosnap)->ds_guid)
7077 error = 0;
7078
7079 if (error != 0) {
7080 dsl_dataset_rele(tosnap, FTAG);
7081 dsl_pool_rele(dp, FTAG);
7082 return (error);
7083 }
7084 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
7085 ZBM_FLAG_HAS_FBN)) {
7086 full_estimate = B_TRUE;
7087 }
7088 } else if (strchr(fromname, '@')) {
7089 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
7090 if (error != 0) {
7091 dsl_dataset_rele(tosnap, FTAG);
7092 dsl_pool_rele(dp, FTAG);
7093 return (error);
7094 }
7095
7096 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
7097 full_estimate = B_TRUE;
7098 dsl_dataset_rele(fromsnap, FTAG);
7099 }
7100 } else {
7101 /*
7102 * from is not properly formatted as a snapshot or
7103 * bookmark
7104 */
7105 dsl_dataset_rele(tosnap, FTAG);
7106 dsl_pool_rele(dp, FTAG);
7107 return (SET_ERROR(EINVAL));
7108 }
7109 }
7110
7111 if (full_estimate) {
7112 dmu_send_outparams_t out = {0};
7113 offset_t off = 0;
7114 out.dso_outfunc = send_space_sum;
7115 out.dso_arg = &space;
7116 out.dso_dryrun = B_TRUE;
7117 /*
7118 * We have to release these holds so dmu_send can take them. It
7119 * will do all the error checking we need.
7120 */
7121 dsl_dataset_rele(tosnap, FTAG);
7122 dsl_pool_rele(dp, FTAG);
7123 error = dmu_send(snapname, fromname, embedok, largeblockok,
7124 compressok, rawok, savedok, resumeobj, resumeoff,
7125 redactlist_book, fd, &off, &out);
7126 } else {
7127 error = dmu_send_estimate_fast(tosnap, fromsnap,
7128 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
7129 compressok || rawok, savedok, &space);
7130 space -= resume_bytes;
7131 if (fromsnap != NULL)
7132 dsl_dataset_rele(fromsnap, FTAG);
7133 dsl_dataset_rele(tosnap, FTAG);
7134 dsl_pool_rele(dp, FTAG);
7135 }
7136
7137 fnvlist_add_uint64(outnvl, "space", space);
7138
7139 return (error);
7140 }
7141
7142 /*
7143 * Sync the currently open TXG to disk for the specified pool.
7144 * This is somewhat similar to 'zfs_sync()'.
7145 * For cases that do not result in error this ioctl will wait for
7146 * the currently open TXG to commit before returning back to the caller.
7147 *
7148 * innvl: {
7149 * "force" -> when true, force uberblock update even if there is no dirty data.
7150 * In addition this will cause the vdev configuration to be written
7151 * out including updating the zpool cache file. (boolean_t)
7152 * }
7153 *
7154 * onvl is unused
7155 */
7156 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
7157 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
7158 };
7159
7160 static int
zfs_ioc_pool_sync(const char * pool,nvlist_t * innvl,nvlist_t * onvl)7161 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
7162 {
7163 (void) onvl;
7164 int err;
7165 boolean_t rc, force = B_FALSE;
7166 spa_t *spa;
7167
7168 if ((err = spa_open(pool, &spa, FTAG)) != 0)
7169 return (err);
7170
7171 if (innvl) {
7172 err = nvlist_lookup_boolean_value(innvl, "force", &rc);
7173 if (err == 0)
7174 force = rc;
7175 }
7176
7177 if (force) {
7178 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
7179 vdev_config_dirty(spa->spa_root_vdev);
7180 spa_config_exit(spa, SCL_CONFIG, FTAG);
7181 }
7182 txg_wait_synced(spa_get_dsl(spa), 0);
7183
7184 spa_close(spa, FTAG);
7185
7186 return (0);
7187 }
7188
7189 /*
7190 * Load a user's wrapping key into the kernel.
7191 * innvl: {
7192 * "hidden_args" -> { "wkeydata" -> value }
7193 * raw uint8_t array of encryption wrapping key data (32 bytes)
7194 * (optional) "noop" -> (value ignored)
7195 * presence indicated key should only be verified, not loaded
7196 * }
7197 */
7198 static const zfs_ioc_key_t zfs_keys_load_key[] = {
7199 {"hidden_args", DATA_TYPE_NVLIST, 0},
7200 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
7201 };
7202
7203 static int
zfs_ioc_load_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)7204 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
7205 {
7206 (void) outnvl;
7207 int ret;
7208 dsl_crypto_params_t *dcp = NULL;
7209 nvlist_t *hidden_args;
7210 boolean_t noop = nvlist_exists(innvl, "noop");
7211
7212 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
7213 ret = SET_ERROR(EINVAL);
7214 goto error;
7215 }
7216
7217 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
7218
7219 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
7220 hidden_args, &dcp);
7221 if (ret != 0)
7222 goto error;
7223
7224 ret = spa_keystore_load_wkey(dsname, dcp, noop);
7225 if (ret != 0)
7226 goto error;
7227
7228 dsl_crypto_params_free(dcp, noop);
7229
7230 return (0);
7231
7232 error:
7233 dsl_crypto_params_free(dcp, B_TRUE);
7234 return (ret);
7235 }
7236
7237 /*
7238 * Unload a user's wrapping key from the kernel.
7239 * Both innvl and outnvl are unused.
7240 */
7241 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
7242 /* no nvl keys */
7243 };
7244
7245 static int
zfs_ioc_unload_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)7246 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
7247 {
7248 (void) innvl, (void) outnvl;
7249 int ret = 0;
7250
7251 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
7252 ret = (SET_ERROR(EINVAL));
7253 goto out;
7254 }
7255
7256 ret = spa_keystore_unload_wkey(dsname);
7257 if (ret != 0)
7258 goto out;
7259
7260 out:
7261 return (ret);
7262 }
7263
7264 /*
7265 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
7266 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
7267 * here to change how the key is derived in userspace.
7268 *
7269 * innvl: {
7270 * "hidden_args" (optional) -> { "wkeydata" -> value }
7271 * raw uint8_t array of new encryption wrapping key data (32 bytes)
7272 * "props" (optional) -> { prop -> value }
7273 * }
7274 *
7275 * outnvl is unused
7276 */
7277 static const zfs_ioc_key_t zfs_keys_change_key[] = {
7278 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
7279 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
7280 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
7281 };
7282
7283 static int
zfs_ioc_change_key(const char * dsname,nvlist_t * innvl,nvlist_t * outnvl)7284 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
7285 {
7286 (void) outnvl;
7287 int ret;
7288 uint64_t cmd = DCP_CMD_NONE;
7289 dsl_crypto_params_t *dcp = NULL;
7290 nvlist_t *args = NULL, *hidden_args = NULL;
7291
7292 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
7293 ret = (SET_ERROR(EINVAL));
7294 goto error;
7295 }
7296
7297 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
7298 (void) nvlist_lookup_nvlist(innvl, "props", &args);
7299 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
7300
7301 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
7302 if (ret != 0)
7303 goto error;
7304
7305 ret = spa_keystore_change_key(dsname, dcp);
7306 if (ret != 0)
7307 goto error;
7308
7309 dsl_crypto_params_free(dcp, B_FALSE);
7310
7311 return (0);
7312
7313 error:
7314 dsl_crypto_params_free(dcp, B_TRUE);
7315 return (ret);
7316 }
7317
7318 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
7319
7320 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)7321 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7322 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
7323 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
7324 {
7325 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
7326
7327 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
7328 ASSERT3U(ioc, <, ZFS_IOC_LAST);
7329 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
7330 ASSERT3P(vec->zvec_func, ==, NULL);
7331
7332 vec->zvec_legacy_func = func;
7333 vec->zvec_secpolicy = secpolicy;
7334 vec->zvec_namecheck = namecheck;
7335 vec->zvec_allow_log = log_history;
7336 vec->zvec_pool_check = pool_check;
7337 }
7338
7339 /*
7340 * See the block comment at the beginning of this file for details on
7341 * each argument to this function.
7342 */
7343 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)7344 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
7345 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
7346 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
7347 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
7348 {
7349 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
7350
7351 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
7352 ASSERT3U(ioc, <, ZFS_IOC_LAST);
7353 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
7354 ASSERT3P(vec->zvec_func, ==, NULL);
7355
7356 /* if we are logging, the name must be valid */
7357 ASSERT(!allow_log || namecheck != NO_NAME);
7358
7359 vec->zvec_name = name;
7360 vec->zvec_func = func;
7361 vec->zvec_secpolicy = secpolicy;
7362 vec->zvec_namecheck = namecheck;
7363 vec->zvec_pool_check = pool_check;
7364 vec->zvec_smush_outnvlist = smush_outnvlist;
7365 vec->zvec_allow_log = allow_log;
7366 vec->zvec_nvl_keys = nvl_keys;
7367 vec->zvec_nvl_key_count = num_keys;
7368 }
7369
7370 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)7371 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7372 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
7373 zfs_ioc_poolcheck_t pool_check)
7374 {
7375 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7376 POOL_NAME, log_history, pool_check);
7377 }
7378
7379 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)7380 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7381 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
7382 {
7383 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7384 DATASET_NAME, B_FALSE, pool_check);
7385 }
7386
7387 static void
zfs_ioctl_register_pool_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)7388 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7389 {
7390 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
7391 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7392 }
7393
7394 static void
zfs_ioctl_register_pool_meta(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)7395 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7396 zfs_secpolicy_func_t *secpolicy)
7397 {
7398 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7399 NO_NAME, B_FALSE, POOL_CHECK_NONE);
7400 }
7401
7402 static void
zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)7403 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
7404 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
7405 {
7406 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7407 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
7408 }
7409
7410 static void
zfs_ioctl_register_dataset_read(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func)7411 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7412 {
7413 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
7414 zfs_secpolicy_read);
7415 }
7416
7417 static void
zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc,zfs_ioc_legacy_func_t * func,zfs_secpolicy_func_t * secpolicy)7418 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7419 zfs_secpolicy_func_t *secpolicy)
7420 {
7421 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7422 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7423 }
7424
7425 static void
zfs_ioctl_init(void)7426 zfs_ioctl_init(void)
7427 {
7428 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7429 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
7430 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7431 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
7432
7433 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7434 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
7435 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7436 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
7437
7438 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7439 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
7440 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7441 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
7442
7443 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7444 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
7445 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7446 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
7447
7448 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7449 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
7450 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7451 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
7452
7453 zfs_ioctl_register("create", ZFS_IOC_CREATE,
7454 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
7455 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7456 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
7457
7458 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7459 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
7460 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7461 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
7462
7463 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
7464 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
7465 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7466 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
7467
7468 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7469 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
7470 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7471 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
7472
7473 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7474 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
7475 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7476 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
7477 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7478 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
7479 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7480 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
7481
7482 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7483 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
7484 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7485 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
7486
7487 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7488 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
7489 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7490 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
7491
7492 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7493 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
7494 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7495 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
7496
7497 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7498 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
7499 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7500 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
7501
7502 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
7503 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
7504 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
7505 ARRAY_SIZE(zfs_keys_get_bookmark_props));
7506
7507 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7508 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7509 POOL_NAME,
7510 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7511 zfs_keys_destroy_bookmarks,
7512 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
7513
7514 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
7515 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME,
7516 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7517 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
7518 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
7519 zfs_ioc_load_key, zfs_secpolicy_load_key,
7520 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7521 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
7522 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
7523 zfs_ioc_unload_key, zfs_secpolicy_load_key,
7524 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7525 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
7526 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
7527 zfs_ioc_change_key, zfs_secpolicy_change_key,
7528 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
7529 B_TRUE, B_TRUE, zfs_keys_change_key,
7530 ARRAY_SIZE(zfs_keys_change_key));
7531
7532 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
7533 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
7534 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7535 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
7536 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7537 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
7538 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
7539
7540 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7541 zfs_ioc_channel_program, zfs_secpolicy_config,
7542 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
7543 B_TRUE, zfs_keys_channel_program,
7544 ARRAY_SIZE(zfs_keys_channel_program));
7545
7546 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
7547 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
7548 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7549 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
7550
7551 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
7552 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
7553 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7554 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
7555
7556 zfs_ioctl_register("zpool_discard_checkpoint",
7557 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7558 zfs_secpolicy_config, POOL_NAME,
7559 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7560 zfs_keys_pool_discard_checkpoint,
7561 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
7562
7563 zfs_ioctl_register("zpool_prefetch",
7564 ZFS_IOC_POOL_PREFETCH, zfs_ioc_pool_prefetch,
7565 zfs_secpolicy_config, POOL_NAME,
7566 POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7567 zfs_keys_pool_prefetch, ARRAY_SIZE(zfs_keys_pool_prefetch));
7568
7569 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7570 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7571 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7572 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7573
7574 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7575 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7576 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7577 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7578
7579 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7580 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7581 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7582 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7583
7584 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7585 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7586 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7587 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7588
7589 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7590 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7591 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7592 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7593
7594 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7595 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7596 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7597 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7598
7599 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS,
7600 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME,
7601 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props,
7602 ARRAY_SIZE(zfs_keys_vdev_get_props));
7603
7604 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS,
7605 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME,
7606 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7607 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props));
7608
7609 zfs_ioctl_register("scrub", ZFS_IOC_POOL_SCRUB,
7610 zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME,
7611 POOL_CHECK_NONE, B_TRUE, B_TRUE,
7612 zfs_keys_pool_scrub, ARRAY_SIZE(zfs_keys_pool_scrub));
7613
7614 zfs_ioctl_register("get_props", ZFS_IOC_POOL_GET_PROPS,
7615 zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME,
7616 POOL_CHECK_NONE, B_FALSE, B_FALSE,
7617 zfs_keys_get_props, ARRAY_SIZE(zfs_keys_get_props));
7618
7619 zfs_ioctl_register("zpool_ddt_prune", ZFS_IOC_DDT_PRUNE,
7620 zfs_ioc_ddt_prune, zfs_secpolicy_config, POOL_NAME,
7621 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7622 zfs_keys_ddt_prune, ARRAY_SIZE(zfs_keys_ddt_prune));
7623
7624 /* IOCTLS that use the legacy function signature */
7625
7626 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7627 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7628
7629 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7630 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7631 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7632 zfs_ioc_pool_scan);
7633 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7634 zfs_ioc_pool_upgrade);
7635 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7636 zfs_ioc_vdev_add);
7637 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7638 zfs_ioc_vdev_remove);
7639 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7640 zfs_ioc_vdev_set_state);
7641 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7642 zfs_ioc_vdev_attach);
7643 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7644 zfs_ioc_vdev_detach);
7645 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7646 zfs_ioc_vdev_setpath);
7647 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7648 zfs_ioc_vdev_setfru);
7649 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7650 zfs_ioc_pool_set_props);
7651 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7652 zfs_ioc_vdev_split);
7653 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7654 zfs_ioc_pool_reguid);
7655
7656 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7657 zfs_ioc_pool_configs, zfs_secpolicy_none);
7658 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7659 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7660 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7661 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7662 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7663 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7664 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7665 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7666
7667 /*
7668 * pool destroy, and export don't log the history as part of
7669 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7670 * does the logging of those commands.
7671 */
7672 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
7673 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7674 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
7675 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7676
7677 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7678 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7679
7680 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7681 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7682 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7683 zfs_ioc_dsobj_to_dsname,
7684 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7685 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7686 zfs_ioc_pool_get_history,
7687 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7688
7689 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7690 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7691
7692 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7693 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
7694
7695 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7696 zfs_ioc_space_written);
7697 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7698 zfs_ioc_objset_recvd_props);
7699 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7700 zfs_ioc_next_obj);
7701 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7702 zfs_ioc_get_fsacl);
7703 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7704 zfs_ioc_objset_stats);
7705 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7706 zfs_ioc_objset_zplprops);
7707 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7708 zfs_ioc_dataset_list_next);
7709 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7710 zfs_ioc_snapshot_list_next);
7711 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7712 zfs_ioc_send_progress);
7713
7714 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7715 zfs_ioc_diff, zfs_secpolicy_diff);
7716 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7717 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7718 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7719 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7720 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7721 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7722 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7723 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7724 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7725 zfs_ioc_send, zfs_secpolicy_send);
7726
7727 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7728 zfs_secpolicy_none);
7729 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7730 zfs_secpolicy_destroy);
7731 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7732 zfs_secpolicy_rename);
7733 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7734 zfs_secpolicy_recv);
7735 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7736 zfs_secpolicy_promote);
7737 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7738 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7739 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7740 zfs_secpolicy_set_fsacl);
7741
7742 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7743 zfs_secpolicy_share, POOL_CHECK_NONE);
7744 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7745 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7746 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7747 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7748 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7749 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7750 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7751 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7752
7753 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7754 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7755 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7756 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7757 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7758 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7759
7760 zfs_ioctl_init_os();
7761 }
7762
7763 /*
7764 * Verify that for non-legacy ioctls the input nvlist
7765 * pairs match against the expected input.
7766 *
7767 * Possible errors are:
7768 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7769 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7770 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7771 */
7772 static int
zfs_check_input_nvpairs(nvlist_t * innvl,const zfs_ioc_vec_t * vec)7773 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7774 {
7775 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7776 boolean_t required_keys_found = B_FALSE;
7777
7778 /*
7779 * examine each input pair
7780 */
7781 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7782 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7783 const char *name = nvpair_name(pair);
7784 data_type_t type = nvpair_type(pair);
7785 boolean_t identified = B_FALSE;
7786
7787 /*
7788 * check pair against the documented names and type
7789 */
7790 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7791 /* if not a wild card name, check for an exact match */
7792 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7793 strcmp(nvl_keys[k].zkey_name, name) != 0)
7794 continue;
7795
7796 identified = B_TRUE;
7797
7798 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7799 nvl_keys[k].zkey_type != type) {
7800 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7801 }
7802
7803 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7804 continue;
7805
7806 required_keys_found = B_TRUE;
7807 break;
7808 }
7809
7810 /* allow an 'optional' key, everything else is invalid */
7811 if (!identified &&
7812 (strcmp(name, "optional") != 0 ||
7813 type != DATA_TYPE_NVLIST)) {
7814 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7815 }
7816 }
7817
7818 /* verify that all required keys were found */
7819 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7820 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7821 continue;
7822
7823 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7824 /* at least one non-optional key is expected here */
7825 if (!required_keys_found)
7826 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7827 continue;
7828 }
7829
7830 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7831 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7832 }
7833
7834 return (0);
7835 }
7836
7837 static int
pool_status_check(const char * name,zfs_ioc_namecheck_t type,zfs_ioc_poolcheck_t check)7838 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7839 zfs_ioc_poolcheck_t check)
7840 {
7841 spa_t *spa;
7842 int error;
7843
7844 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7845 type == ENTITY_NAME);
7846
7847 if (check & POOL_CHECK_NONE)
7848 return (0);
7849
7850 error = spa_open(name, &spa, FTAG);
7851 if (error == 0) {
7852 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7853 error = SET_ERROR(EAGAIN);
7854 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7855 error = SET_ERROR(EROFS);
7856 spa_close(spa, FTAG);
7857 }
7858 return (error);
7859 }
7860
7861 int
zfsdev_getminor(zfs_file_t * fp,minor_t * minorp)7862 zfsdev_getminor(zfs_file_t *fp, minor_t *minorp)
7863 {
7864 zfsdev_state_t *zs, *fpd;
7865
7866 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7867
7868 fpd = zfs_file_private(fp);
7869 if (fpd == NULL)
7870 return (SET_ERROR(EBADF));
7871
7872 mutex_enter(&zfsdev_state_lock);
7873
7874 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7875
7876 if (zs->zs_minor == -1)
7877 continue;
7878
7879 if (fpd == zs) {
7880 *minorp = fpd->zs_minor;
7881 mutex_exit(&zfsdev_state_lock);
7882 return (0);
7883 }
7884 }
7885
7886 mutex_exit(&zfsdev_state_lock);
7887
7888 return (SET_ERROR(EBADF));
7889 }
7890
7891 void *
zfsdev_get_state(minor_t minor,enum zfsdev_state_type which)7892 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7893 {
7894 zfsdev_state_t *zs;
7895
7896 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7897 if (zs->zs_minor == minor) {
7898 membar_consumer();
7899 switch (which) {
7900 case ZST_ONEXIT:
7901 return (zs->zs_onexit);
7902 case ZST_ZEVENT:
7903 return (zs->zs_zevent);
7904 case ZST_ALL:
7905 return (zs);
7906 }
7907 }
7908 }
7909
7910 return (NULL);
7911 }
7912
7913 /*
7914 * Find a free minor number. The zfsdev_state_list is expected to
7915 * be short since it is only a list of currently open file handles.
7916 */
7917 static minor_t
zfsdev_minor_alloc(void)7918 zfsdev_minor_alloc(void)
7919 {
7920 static minor_t last_minor = 0;
7921 minor_t m;
7922
7923 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7924
7925 for (m = last_minor + 1; m != last_minor; m++) {
7926 if (m > ZFSDEV_MAX_MINOR)
7927 m = 1;
7928 if (zfsdev_get_state(m, ZST_ALL) == NULL) {
7929 last_minor = m;
7930 return (m);
7931 }
7932 }
7933
7934 return (0);
7935 }
7936
7937 int
zfsdev_state_init(void * priv)7938 zfsdev_state_init(void *priv)
7939 {
7940 zfsdev_state_t *zs, *zsprev = NULL;
7941 minor_t minor;
7942 boolean_t newzs = B_FALSE;
7943
7944 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7945
7946 minor = zfsdev_minor_alloc();
7947 if (minor == 0)
7948 return (SET_ERROR(ENXIO));
7949
7950 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7951 if (zs->zs_minor == -1)
7952 break;
7953 zsprev = zs;
7954 }
7955
7956 if (!zs) {
7957 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7958 newzs = B_TRUE;
7959 }
7960
7961 zfsdev_private_set_state(priv, zs);
7962
7963 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7964 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7965
7966 /*
7967 * In order to provide for lock-free concurrent read access
7968 * to the minor list in zfsdev_get_state(), new entries
7969 * must be completely written before linking them into the
7970 * list whereas existing entries are already linked; the last
7971 * operation must be updating zs_minor (from -1 to the new
7972 * value).
7973 */
7974 if (newzs) {
7975 zs->zs_minor = minor;
7976 membar_producer();
7977 zsprev->zs_next = zs;
7978 } else {
7979 membar_producer();
7980 zs->zs_minor = minor;
7981 }
7982
7983 return (0);
7984 }
7985
7986 void
zfsdev_state_destroy(void * priv)7987 zfsdev_state_destroy(void *priv)
7988 {
7989 zfsdev_state_t *zs = zfsdev_private_get_state(priv);
7990
7991 ASSERT(zs != NULL);
7992 ASSERT3S(zs->zs_minor, >, 0);
7993
7994 /*
7995 * The last reference to this zfsdev file descriptor is being dropped.
7996 * We don't have to worry about lookup grabbing this state object, and
7997 * zfsdev_state_init() will not try to reuse this object until it is
7998 * invalidated by setting zs_minor to -1. Invalidation must be done
7999 * last, with a memory barrier to ensure ordering. This lets us avoid
8000 * taking the global zfsdev state lock around destruction.
8001 */
8002 zfs_onexit_destroy(zs->zs_onexit);
8003 zfs_zevent_destroy(zs->zs_zevent);
8004 zs->zs_onexit = NULL;
8005 zs->zs_zevent = NULL;
8006 membar_producer();
8007 zs->zs_minor = -1;
8008 }
8009
8010 long
zfsdev_ioctl_common(uint_t vecnum,zfs_cmd_t * zc,int flag)8011 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
8012 {
8013 int error, cmd;
8014 const zfs_ioc_vec_t *vec;
8015 char *saved_poolname = NULL;
8016 uint64_t max_nvlist_src_size;
8017 size_t saved_poolname_len = 0;
8018 nvlist_t *innvl = NULL;
8019 fstrans_cookie_t cookie;
8020 hrtime_t start_time = gethrtime();
8021
8022 cmd = vecnum;
8023 error = 0;
8024 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
8025 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
8026
8027 vec = &zfs_ioc_vec[vecnum];
8028
8029 /*
8030 * The registered ioctl list may be sparse, verify that either
8031 * a normal or legacy handler are registered.
8032 */
8033 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
8034 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
8035
8036 zc->zc_iflags = flag & FKIOCTL;
8037 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
8038 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
8039 /*
8040 * Make sure the user doesn't pass in an insane value for
8041 * zc_nvlist_src_size. We have to check, since we will end
8042 * up allocating that much memory inside of get_nvlist(). This
8043 * prevents a nefarious user from allocating tons of kernel
8044 * memory.
8045 *
8046 * Also, we return EINVAL instead of ENOMEM here. The reason
8047 * being that returning ENOMEM from an ioctl() has a special
8048 * connotation; that the user's size value is too small and
8049 * needs to be expanded to hold the nvlist. See
8050 * zcmd_expand_dst_nvlist() for details.
8051 */
8052 error = SET_ERROR(EINVAL); /* User's size too big */
8053
8054 } else if (zc->zc_nvlist_src_size != 0) {
8055 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
8056 zc->zc_iflags, &innvl);
8057 if (error != 0)
8058 goto out;
8059 }
8060
8061 /*
8062 * Ensure that all pool/dataset names are valid before we pass down to
8063 * the lower layers.
8064 */
8065 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
8066 switch (vec->zvec_namecheck) {
8067 case POOL_NAME:
8068 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
8069 error = SET_ERROR(EINVAL);
8070 else
8071 error = pool_status_check(zc->zc_name,
8072 vec->zvec_namecheck, vec->zvec_pool_check);
8073 break;
8074
8075 case DATASET_NAME:
8076 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
8077 error = SET_ERROR(EINVAL);
8078 else
8079 error = pool_status_check(zc->zc_name,
8080 vec->zvec_namecheck, vec->zvec_pool_check);
8081 break;
8082
8083 case ENTITY_NAME:
8084 if (entity_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 }
8090 break;
8091
8092 case NO_NAME:
8093 break;
8094 }
8095 /*
8096 * Ensure that all input pairs are valid before we pass them down
8097 * to the lower layers.
8098 *
8099 * The vectored functions can use fnvlist_lookup_{type} for any
8100 * required pairs since zfs_check_input_nvpairs() confirmed that
8101 * they exist and are of the correct type.
8102 */
8103 if (error == 0 && vec->zvec_func != NULL) {
8104 error = zfs_check_input_nvpairs(innvl, vec);
8105 if (error != 0)
8106 goto out;
8107 }
8108
8109 if (error == 0) {
8110 cookie = spl_fstrans_mark();
8111 error = vec->zvec_secpolicy(zc, innvl, CRED());
8112 spl_fstrans_unmark(cookie);
8113 }
8114
8115 if (error != 0)
8116 goto out;
8117
8118 /* legacy ioctls can modify zc_name */
8119 /*
8120 * Can't use kmem_strdup() as we might truncate the string and
8121 * kmem_strfree() would then free with incorrect size.
8122 */
8123 saved_poolname_len = strlen(zc->zc_name) + 1;
8124 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
8125
8126 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
8127 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
8128
8129 if (vec->zvec_func != NULL) {
8130 nvlist_t *outnvl;
8131 int puterror = 0;
8132 spa_t *spa;
8133 nvlist_t *lognv = NULL;
8134
8135 ASSERT(vec->zvec_legacy_func == NULL);
8136
8137 /*
8138 * Add the innvl to the lognv before calling the func,
8139 * in case the func changes the innvl.
8140 */
8141 if (vec->zvec_allow_log) {
8142 lognv = fnvlist_alloc();
8143 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
8144 vec->zvec_name);
8145 if (!nvlist_empty(innvl)) {
8146 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
8147 innvl);
8148 }
8149 }
8150
8151 outnvl = fnvlist_alloc();
8152 cookie = spl_fstrans_mark();
8153 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
8154 spl_fstrans_unmark(cookie);
8155
8156 /*
8157 * Some commands can partially execute, modify state, and still
8158 * return an error. In these cases, attempt to record what
8159 * was modified.
8160 */
8161 if ((error == 0 ||
8162 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
8163 vec->zvec_allow_log &&
8164 spa_open(zc->zc_name, &spa, FTAG) == 0) {
8165 if (!nvlist_empty(outnvl)) {
8166 size_t out_size = fnvlist_size(outnvl);
8167 if (out_size > zfs_history_output_max) {
8168 fnvlist_add_int64(lognv,
8169 ZPOOL_HIST_OUTPUT_SIZE, out_size);
8170 } else {
8171 fnvlist_add_nvlist(lognv,
8172 ZPOOL_HIST_OUTPUT_NVL, outnvl);
8173 }
8174 }
8175 if (error != 0) {
8176 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
8177 error);
8178 }
8179 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
8180 gethrtime() - start_time);
8181 (void) spa_history_log_nvl(spa, lognv);
8182 spa_close(spa, FTAG);
8183 }
8184 fnvlist_free(lognv);
8185
8186 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
8187 int smusherror = 0;
8188 if (vec->zvec_smush_outnvlist) {
8189 smusherror = nvlist_smush(outnvl,
8190 zc->zc_nvlist_dst_size);
8191 }
8192 if (smusherror == 0)
8193 puterror = put_nvlist(zc, outnvl);
8194 }
8195
8196 if (puterror != 0)
8197 error = puterror;
8198
8199 nvlist_free(outnvl);
8200 } else {
8201 cookie = spl_fstrans_mark();
8202 error = vec->zvec_legacy_func(zc);
8203 spl_fstrans_unmark(cookie);
8204 }
8205
8206 out:
8207 nvlist_free(innvl);
8208 if (error == 0 && vec->zvec_allow_log) {
8209 char *s = tsd_get(zfs_allow_log_key);
8210 if (s != NULL)
8211 kmem_strfree(s);
8212 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
8213 }
8214 if (saved_poolname != NULL)
8215 kmem_free(saved_poolname, saved_poolname_len);
8216
8217 return (error);
8218 }
8219
8220 int
zfs_kmod_init(void)8221 zfs_kmod_init(void)
8222 {
8223 int error;
8224
8225 if ((error = zvol_init()) != 0)
8226 return (error);
8227
8228 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
8229 zfs_init();
8230
8231 zfs_ioctl_init();
8232
8233 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
8234 zfsdev_state_listhead.zs_minor = -1;
8235
8236 if ((error = zfsdev_attach()) != 0)
8237 goto out;
8238
8239 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
8240 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
8241
8242 return (0);
8243 out:
8244 zfs_fini();
8245 spa_fini();
8246 zvol_fini();
8247
8248 return (error);
8249 }
8250
8251 void
zfs_kmod_fini(void)8252 zfs_kmod_fini(void)
8253 {
8254 zfsdev_state_t *zs, *zsnext = NULL;
8255
8256 zfsdev_detach();
8257
8258 mutex_destroy(&zfsdev_state_lock);
8259
8260 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zsnext) {
8261 zsnext = zs->zs_next;
8262 if (zs->zs_onexit)
8263 zfs_onexit_destroy(zs->zs_onexit);
8264 if (zs->zs_zevent)
8265 zfs_zevent_destroy(zs->zs_zevent);
8266 if (zs != &zfsdev_state_listhead)
8267 kmem_free(zs, sizeof (zfsdev_state_t));
8268 }
8269
8270 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
8271 zfs_fini();
8272 spa_fini();
8273 zvol_fini();
8274
8275 tsd_destroy(&rrw_tsd_key);
8276 tsd_destroy(&zfs_allow_log_key);
8277 }
8278
8279 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, U64, ZMOD_RW,
8280 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");
8281
8282 ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, U64, ZMOD_RW,
8283 "Maximum size in bytes of ZFS ioctl output that will be logged");
8284