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