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