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