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