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