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