Merge commit 'bb1f424574ac8e08069d0ba993c2a41ffe796794'
[unleashed.git] / kernel / fs / zfs / zfs_ioctl.c
blob22a73b3f307436145f2dc3ad014e2f5c6c0aa18a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
25 * Portions Copyright 2011 Martin Matuska
26 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
29 * Copyright (c) 2011, 2017 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 Integros [integros.com]
33 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 * Copyright 2017 RackTop Systems.
35 * Copyright (c) 2017 Datto Inc.
39 * ZFS ioctls.
41 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
42 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
44 * There are two ways that we handle ioctls: the legacy way where almost
45 * all of the logic is in the ioctl callback, and the new way where most
46 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
48 * Non-legacy ioctls should be registered by calling
49 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
50 * from userland by lzc_ioctl().
52 * The registration arguments are as follows:
54 * const char *name
55 * The name of the ioctl. This is used for history logging. If the
56 * ioctl returns successfully (the callback returns 0), and allow_log
57 * is true, then a history log entry will be recorded with the input &
58 * output nvlists. The log entry can be printed with "zpool history -i".
60 * zfs_ioc_t ioc
61 * The ioctl request number, which userland will pass to ioctl(2).
62 * The ioctl numbers can change from release to release, because
63 * the caller (libzfs) must be matched to the kernel.
65 * zfs_secpolicy_func_t *secpolicy
66 * This function will be called before the zfs_ioc_func_t, to
67 * determine if this operation is permitted. It should return EPERM
68 * on failure, and 0 on success. Checks include determining if the
69 * dataset is visible in this zone, and if the user has either all
70 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
71 * to do this operation on this dataset with "zfs allow".
73 * zfs_ioc_namecheck_t namecheck
74 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
75 * name, a dataset name, or nothing. If the name is not well-formed,
76 * the ioctl will fail and the callback will not be called.
77 * Therefore, the callback can assume that the name is well-formed
78 * (e.g. is null-terminated, doesn't have more than one '@' character,
79 * doesn't have invalid characters).
81 * zfs_ioc_poolcheck_t pool_check
82 * This specifies requirements on the pool state. If the pool does
83 * not meet them (is suspended or is readonly), the ioctl will fail
84 * and the callback will not be called. If any checks are specified
85 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
86 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
87 * POOL_CHECK_READONLY).
89 * boolean_t smush_outnvlist
90 * If smush_outnvlist is true, then the output is presumed to be a
91 * list of errors, and it will be "smushed" down to fit into the
92 * caller's buffer, by removing some entries and replacing them with a
93 * single "N_MORE_ERRORS" entry indicating how many were removed. See
94 * nvlist_smush() for details. If smush_outnvlist is false, and the
95 * outnvlist does not fit into the userland-provided buffer, then the
96 * ioctl will fail with ENOMEM.
98 * zfs_ioc_func_t *func
99 * The callback function that will perform the operation.
101 * The callback should return 0 on success, or an error number on
102 * failure. If the function fails, the userland ioctl will return -1,
103 * and errno will be set to the callback's return value. The callback
104 * will be called with the following arguments:
106 * const char *name
107 * The name of the pool or dataset to operate on, from
108 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
109 * expected type (pool, dataset, or none).
111 * nvlist_t *innvl
112 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
113 * NULL if no input nvlist was provided. Changes to this nvlist are
114 * ignored. If the input nvlist could not be deserialized, the
115 * ioctl will fail and the callback will not be called.
117 * nvlist_t *outnvl
118 * The output nvlist, initially empty. The callback can fill it in,
119 * and it will be returned to userland by serializing it into
120 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
121 * fails (e.g. because the caller didn't supply a large enough
122 * buffer), then the overall ioctl will fail. See the
123 * 'smush_nvlist' argument above for additional behaviors.
125 * There are two typical uses of the output nvlist:
126 * - To return state, e.g. property values. In this case,
127 * smush_outnvlist should be false. If the buffer was not large
128 * enough, the caller will reallocate a larger buffer and try
129 * the ioctl again.
131 * - To return multiple errors from an ioctl which makes on-disk
132 * changes. In this case, smush_outnvlist should be true.
133 * Ioctls which make on-disk modifications should generally not
134 * use the outnvl if they succeed, because the caller can not
135 * distinguish between the operation failing, and
136 * deserialization failing.
139 #include <sys/types.h>
140 #include <sys/param.h>
141 #include <sys/errno.h>
142 #include <sys/uio.h>
143 #include <sys/buf.h>
144 #include <sys/modctl.h>
145 #include <sys/open.h>
146 #include <sys/file.h>
147 #include <sys/kmem.h>
148 #include <sys/conf.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
154 #include <sys/zap.h>
155 #include <sys/spa.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
158 #include <sys/priv_impl.h>
159 #include <sys/dmu.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
167 #include <sys/ddi.h>
168 #include <sys/sunddi.h>
169 #include <sys/sunldi.h>
170 #include <sys/policy.h>
171 #include <sys/zone.h>
172 #include <sys/nvpair.h>
173 #include <sys/pathname.h>
174 #include <sys/mount.h>
175 #include <sys/sdt.h>
176 #include <sys/fs/zfs.h>
177 #include <sys/zfs_ctldir.h>
178 #include <sys/zfs_dir.h>
179 #include <sys/zfs_onexit.h>
180 #include <sys/zvol.h>
181 #include <sys/dsl_scan.h>
182 #include <sharefs/share.h>
183 #include <sys/dmu_objset.h>
184 #include <sys/dmu_send.h>
185 #include <sys/dsl_destroy.h>
186 #include <sys/dsl_bookmark.h>
187 #include <sys/dsl_userhold.h>
188 #include <sys/zfeature.h>
189 #include <sys/zcp.h>
190 #include <sys/zio_checksum.h>
191 #include <sys/vdev_removal.h>
192 #include <sys/vdev_impl.h>
193 #include <sys/vdev_initialize.h>
195 #include "zfs_namecheck.h"
196 #include "zfs_prop.h"
197 #include "zfs_deleg.h"
198 #include "zfs_comutil.h"
200 #include "lua.h"
201 #include "lauxlib.h"
203 extern struct modlfs zfs_modlfs;
205 extern void zfs_init(void);
206 extern void zfs_fini(void);
208 ldi_ident_t zfs_li = NULL;
209 dev_info_t *zfs_dip;
211 uint_t zfs_fsyncer_key;
212 extern uint_t rrw_tsd_key;
213 static uint_t zfs_allow_log_key;
215 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
216 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
217 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
219 typedef enum {
220 NO_NAME,
221 POOL_NAME,
222 DATASET_NAME
223 } zfs_ioc_namecheck_t;
225 typedef enum {
226 POOL_CHECK_NONE = 1 << 0,
227 POOL_CHECK_SUSPENDED = 1 << 1,
228 POOL_CHECK_READONLY = 1 << 2,
229 } zfs_ioc_poolcheck_t;
231 typedef struct zfs_ioc_vec {
232 zfs_ioc_legacy_func_t *zvec_legacy_func;
233 zfs_ioc_func_t *zvec_func;
234 zfs_secpolicy_func_t *zvec_secpolicy;
235 zfs_ioc_namecheck_t zvec_namecheck;
236 boolean_t zvec_allow_log;
237 zfs_ioc_poolcheck_t zvec_pool_check;
238 boolean_t zvec_smush_outnvlist;
239 const char *zvec_name;
240 } zfs_ioc_vec_t;
242 /* This array is indexed by zfs_userquota_prop_t */
243 static const char *userquota_perms[] = {
244 ZFS_DELEG_PERM_USERUSED,
245 ZFS_DELEG_PERM_USERQUOTA,
246 ZFS_DELEG_PERM_GROUPUSED,
247 ZFS_DELEG_PERM_GROUPQUOTA,
250 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
251 static int zfs_check_settable(const char *name, nvpair_t *property,
252 cred_t *cr);
253 static int zfs_check_clearable(char *dataset, nvlist_t *props,
254 nvlist_t **errors);
255 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
256 boolean_t *);
257 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
258 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
260 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
262 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
263 void
264 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
266 const char *newfile;
267 char buf[512];
268 va_list adx;
271 * Get rid of annoying "../common/" prefix to filename.
273 newfile = strrchr(file, '/');
274 if (newfile != NULL) {
275 newfile = newfile + 1; /* Get rid of leading / */
276 } else {
277 newfile = file;
280 va_start(adx, fmt);
281 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
282 va_end(adx);
285 * To get this data, use the zfs-dprintf probe as so:
286 * dtrace -q -n 'zfs-dprintf \
287 * /stringof(arg0) == "dbuf.c"/ \
288 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
289 * arg0 = file name
290 * arg1 = function name
291 * arg2 = line number
292 * arg3 = message
294 DTRACE_PROBE4(zfs__dprintf,
295 char *, newfile, char *, func, int, line, char *, buf);
298 static void
299 history_str_free(char *buf)
301 kmem_free(buf, HIS_MAX_RECORD_LEN);
304 static char *
305 history_str_get(zfs_cmd_t *zc)
307 char *buf;
309 if (zc->zc_history == (uintptr_t)NULL)
310 return (NULL);
312 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
313 if (copyinstr((void *)(uintptr_t)zc->zc_history,
314 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
315 history_str_free(buf);
316 return (NULL);
319 buf[HIS_MAX_RECORD_LEN -1] = '\0';
321 return (buf);
325 * Check to see if the named dataset is currently defined as bootable
327 static boolean_t
328 zfs_is_bootfs(const char *name)
330 objset_t *os;
332 if (dmu_objset_hold(name, FTAG, &os) == 0) {
333 boolean_t ret;
334 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
335 dmu_objset_rele(os, FTAG);
336 return (ret);
338 return (B_FALSE);
342 * Return non-zero if the spa version is less than requested version.
344 static int
345 zfs_earlier_version(const char *name, int version)
347 spa_t *spa;
349 if (spa_open(name, &spa, FTAG) == 0) {
350 if (spa_version(spa) < version) {
351 spa_close(spa, FTAG);
352 return (1);
354 spa_close(spa, FTAG);
356 return (0);
360 * Return TRUE if the ZPL version is less than requested version.
362 static boolean_t
363 zpl_earlier_version(const char *name, int version)
365 objset_t *os;
366 boolean_t rc = B_TRUE;
368 if (dmu_objset_hold(name, FTAG, &os) == 0) {
369 uint64_t zplversion;
371 if (dmu_objset_type(os) != DMU_OST_ZFS) {
372 dmu_objset_rele(os, FTAG);
373 return (B_TRUE);
375 /* XXX reading from non-owned objset */
376 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
377 rc = zplversion < version;
378 dmu_objset_rele(os, FTAG);
380 return (rc);
383 static void
384 zfs_log_history(zfs_cmd_t *zc)
386 spa_t *spa;
387 char *buf;
389 if ((buf = history_str_get(zc)) == NULL)
390 return;
392 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
393 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
394 (void) spa_history_log(spa, buf);
395 spa_close(spa, FTAG);
397 history_str_free(buf);
401 * Policy for top-level read operations (list pools). Requires no privileges,
402 * and can be used in the local zone, as there is no associated dataset.
404 /* ARGSUSED */
405 static int
406 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
408 return (0);
412 * Policy for dataset read operations (list children, get statistics). Requires
413 * no privileges, but must be visible in the local zone.
415 /* ARGSUSED */
416 static int
417 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
419 if (INGLOBALZONE(curproc) ||
420 zone_dataset_visible(zc->zc_name, NULL))
421 return (0);
423 return (SET_ERROR(ENOENT));
426 static int
427 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
429 int writable = 1;
432 * The dataset must be visible by this zone -- check this first
433 * so they don't see EPERM on something they shouldn't know about.
435 if (!INGLOBALZONE(curproc) &&
436 !zone_dataset_visible(dataset, &writable))
437 return (SET_ERROR(ENOENT));
439 if (INGLOBALZONE(curproc)) {
441 * If the fs is zoned, only root can access it from the
442 * global zone.
444 if (secpolicy_zfs(cr) && zoned)
445 return (SET_ERROR(EPERM));
446 } else {
448 * If we are in a local zone, the 'zoned' property must be set.
450 if (!zoned)
451 return (SET_ERROR(EPERM));
453 /* must be writable by this zone */
454 if (!writable)
455 return (SET_ERROR(EPERM));
457 return (0);
460 static int
461 zfs_dozonecheck(const char *dataset, cred_t *cr)
463 uint64_t zoned;
465 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
466 return (SET_ERROR(ENOENT));
468 return (zfs_dozonecheck_impl(dataset, zoned, cr));
471 static int
472 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
474 uint64_t zoned;
476 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
477 return (SET_ERROR(ENOENT));
479 return (zfs_dozonecheck_impl(dataset, zoned, cr));
482 static int
483 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
484 const char *perm, cred_t *cr)
486 int error;
488 error = zfs_dozonecheck_ds(name, ds, cr);
489 if (error == 0) {
490 error = secpolicy_zfs(cr);
491 if (error != 0)
492 error = dsl_deleg_access_impl(ds, perm, cr);
494 return (error);
497 static int
498 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
500 int error;
501 dsl_dataset_t *ds;
502 dsl_pool_t *dp;
505 * First do a quick check for root in the global zone, which
506 * is allowed to do all write_perms. This ensures that zfs_ioc_*
507 * will get to handle nonexistent datasets.
509 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
510 return (0);
512 error = dsl_pool_hold(name, FTAG, &dp);
513 if (error != 0)
514 return (error);
516 error = dsl_dataset_hold(dp, name, FTAG, &ds);
517 if (error != 0) {
518 dsl_pool_rele(dp, FTAG);
519 return (error);
522 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
524 dsl_dataset_rele(ds, FTAG);
525 dsl_pool_rele(dp, FTAG);
526 return (error);
529 static int
530 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
531 cred_t *cr)
533 char *strval;
536 * Check permissions for special properties.
538 switch (prop) {
539 case ZFS_PROP_ZONED:
541 * Disallow setting of 'zoned' from within a local zone.
543 if (!INGLOBALZONE(curproc))
544 return (SET_ERROR(EPERM));
545 break;
547 case ZFS_PROP_QUOTA:
548 case ZFS_PROP_FILESYSTEM_LIMIT:
549 case ZFS_PROP_SNAPSHOT_LIMIT:
550 if (!INGLOBALZONE(curproc)) {
551 uint64_t zoned;
552 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
554 * Unprivileged users are allowed to modify the
555 * limit on things *under* (ie. contained by)
556 * the thing they own.
558 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
559 setpoint))
560 return (SET_ERROR(EPERM));
561 if (!zoned || strlen(dsname) <= strlen(setpoint))
562 return (SET_ERROR(EPERM));
564 break;
567 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
570 /* ARGSUSED */
571 static int
572 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
574 int error;
576 error = zfs_dozonecheck(zc->zc_name, cr);
577 if (error != 0)
578 return (error);
581 * permission to set permissions will be evaluated later in
582 * dsl_deleg_can_allow()
584 return (0);
587 /* ARGSUSED */
588 static int
589 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
591 return (zfs_secpolicy_write_perms(zc->zc_name,
592 ZFS_DELEG_PERM_ROLLBACK, cr));
595 /* ARGSUSED */
596 static int
597 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
599 dsl_pool_t *dp;
600 dsl_dataset_t *ds;
601 char *cp;
602 int error;
605 * Generate the current snapshot name from the given objsetid, then
606 * use that name for the secpolicy/zone checks.
608 cp = strchr(zc->zc_name, '@');
609 if (cp == NULL)
610 return (SET_ERROR(EINVAL));
611 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
612 if (error != 0)
613 return (error);
615 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
616 if (error != 0) {
617 dsl_pool_rele(dp, FTAG);
618 return (error);
621 dsl_dataset_name(ds, zc->zc_name);
623 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
624 ZFS_DELEG_PERM_SEND, cr);
625 dsl_dataset_rele(ds, FTAG);
626 dsl_pool_rele(dp, FTAG);
628 return (error);
631 /* ARGSUSED */
632 static int
633 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
635 return (zfs_secpolicy_write_perms(zc->zc_name,
636 ZFS_DELEG_PERM_SEND, cr));
639 /* ARGSUSED */
640 static int
641 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
643 vnode_t *vp;
644 int error;
646 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
647 NO_FOLLOW, NULL, &vp)) != 0)
648 return (error);
650 /* Now make sure mntpnt and dataset are ZFS */
652 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
653 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
654 zc->zc_name) != 0)) {
655 VN_RELE(vp);
656 return (SET_ERROR(EPERM));
659 VN_RELE(vp);
660 return (dsl_deleg_access(zc->zc_name,
661 ZFS_DELEG_PERM_SHARE, cr));
665 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
667 if (!INGLOBALZONE(curproc))
668 return (SET_ERROR(EPERM));
670 if (secpolicy_nfs(cr) == 0) {
671 return (0);
672 } else {
673 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
678 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
680 if (!INGLOBALZONE(curproc))
681 return (SET_ERROR(EPERM));
683 if (secpolicy_smb(cr) == 0) {
684 return (0);
685 } else {
686 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
690 static int
691 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
693 char *cp;
696 * Remove the @bla or /bla from the end of the name to get the parent.
698 (void) strncpy(parent, datasetname, parentsize);
699 cp = strrchr(parent, '@');
700 if (cp != NULL) {
701 cp[0] = '\0';
702 } else {
703 cp = strrchr(parent, '/');
704 if (cp == NULL)
705 return (SET_ERROR(ENOENT));
706 cp[0] = '\0';
709 return (0);
713 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
715 int error;
717 if ((error = zfs_secpolicy_write_perms(name,
718 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
719 return (error);
721 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
724 /* ARGSUSED */
725 static int
726 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
728 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
732 * Destroying snapshots with delegated permissions requires
733 * descendant mount and destroy permissions.
735 /* ARGSUSED */
736 static int
737 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
739 nvlist_t *snaps;
740 nvpair_t *pair, *nextpair;
741 int error = 0;
743 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
744 return (SET_ERROR(EINVAL));
745 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
746 pair = nextpair) {
747 nextpair = nvlist_next_nvpair(snaps, pair);
748 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
749 if (error == ENOENT) {
751 * Ignore any snapshots that don't exist (we consider
752 * them "already destroyed"). Remove the name from the
753 * nvl here in case the snapshot is created between
754 * now and when we try to destroy it (in which case
755 * we don't want to destroy it since we haven't
756 * checked for permission).
758 fnvlist_remove_nvpair(snaps, pair);
759 error = 0;
761 if (error != 0)
762 break;
765 return (error);
769 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
771 char parentname[ZFS_MAX_DATASET_NAME_LEN];
772 int error;
774 if ((error = zfs_secpolicy_write_perms(from,
775 ZFS_DELEG_PERM_RENAME, cr)) != 0)
776 return (error);
778 if ((error = zfs_secpolicy_write_perms(from,
779 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
780 return (error);
782 if ((error = zfs_get_parent(to, parentname,
783 sizeof (parentname))) != 0)
784 return (error);
786 if ((error = zfs_secpolicy_write_perms(parentname,
787 ZFS_DELEG_PERM_CREATE, cr)) != 0)
788 return (error);
790 if ((error = zfs_secpolicy_write_perms(parentname,
791 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
792 return (error);
794 return (error);
797 /* ARGSUSED */
798 static int
799 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
801 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
804 /* ARGSUSED */
805 static int
806 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
808 dsl_pool_t *dp;
809 dsl_dataset_t *clone;
810 int error;
812 error = zfs_secpolicy_write_perms(zc->zc_name,
813 ZFS_DELEG_PERM_PROMOTE, cr);
814 if (error != 0)
815 return (error);
817 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
818 if (error != 0)
819 return (error);
821 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
823 if (error == 0) {
824 char parentname[ZFS_MAX_DATASET_NAME_LEN];
825 dsl_dataset_t *origin = NULL;
826 dsl_dir_t *dd;
827 dd = clone->ds_dir;
829 error = dsl_dataset_hold_obj(dd->dd_pool,
830 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
831 if (error != 0) {
832 dsl_dataset_rele(clone, FTAG);
833 dsl_pool_rele(dp, FTAG);
834 return (error);
837 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
838 ZFS_DELEG_PERM_MOUNT, cr);
840 dsl_dataset_name(origin, parentname);
841 if (error == 0) {
842 error = zfs_secpolicy_write_perms_ds(parentname, origin,
843 ZFS_DELEG_PERM_PROMOTE, cr);
845 dsl_dataset_rele(clone, FTAG);
846 dsl_dataset_rele(origin, FTAG);
848 dsl_pool_rele(dp, FTAG);
849 return (error);
852 /* ARGSUSED */
853 static int
854 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
856 int error;
858 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
859 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
860 return (error);
862 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
863 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
864 return (error);
866 return (zfs_secpolicy_write_perms(zc->zc_name,
867 ZFS_DELEG_PERM_CREATE, cr));
871 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
873 return (zfs_secpolicy_write_perms(name,
874 ZFS_DELEG_PERM_SNAPSHOT, cr));
878 * Check for permission to create each snapshot in the nvlist.
880 /* ARGSUSED */
881 static int
882 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
884 nvlist_t *snaps;
885 int error = 0;
886 nvpair_t *pair;
888 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
889 return (SET_ERROR(EINVAL));
890 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
891 pair = nvlist_next_nvpair(snaps, pair)) {
892 char *name = nvpair_name(pair);
893 char *atp = strchr(name, '@');
895 if (atp == NULL) {
896 error = SET_ERROR(EINVAL);
897 break;
899 *atp = '\0';
900 error = zfs_secpolicy_snapshot_perms(name, cr);
901 *atp = '@';
902 if (error != 0)
903 break;
905 return (error);
909 * Check for permission to create each snapshot in the nvlist.
911 /* ARGSUSED */
912 static int
913 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
915 int error = 0;
917 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
918 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
919 char *name = nvpair_name(pair);
920 char *hashp = strchr(name, '#');
922 if (hashp == NULL) {
923 error = SET_ERROR(EINVAL);
924 break;
926 *hashp = '\0';
927 error = zfs_secpolicy_write_perms(name,
928 ZFS_DELEG_PERM_BOOKMARK, cr);
929 *hashp = '#';
930 if (error != 0)
931 break;
933 return (error);
936 /* ARGSUSED */
937 static int
938 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
940 return (zfs_secpolicy_write_perms(zc->zc_name,
941 ZFS_DELEG_PERM_REMAP, cr));
944 /* ARGSUSED */
945 static int
946 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
948 nvpair_t *pair, *nextpair;
949 int error = 0;
951 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
952 pair = nextpair) {
953 char *name = nvpair_name(pair);
954 char *hashp = strchr(name, '#');
955 nextpair = nvlist_next_nvpair(innvl, pair);
957 if (hashp == NULL) {
958 error = SET_ERROR(EINVAL);
959 break;
962 *hashp = '\0';
963 error = zfs_secpolicy_write_perms(name,
964 ZFS_DELEG_PERM_DESTROY, cr);
965 *hashp = '#';
966 if (error == ENOENT) {
968 * Ignore any filesystems that don't exist (we consider
969 * their bookmarks "already destroyed"). Remove
970 * the name from the nvl here in case the filesystem
971 * is created between now and when we try to destroy
972 * the bookmark (in which case we don't want to
973 * destroy it since we haven't checked for permission).
975 fnvlist_remove_nvpair(innvl, pair);
976 error = 0;
978 if (error != 0)
979 break;
982 return (error);
985 /* ARGSUSED */
986 static int
987 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
990 * Even root must have a proper TSD so that we know what pool
991 * to log to.
993 if (tsd_get(zfs_allow_log_key) == NULL)
994 return (SET_ERROR(EPERM));
995 return (0);
998 static int
999 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1001 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1002 int error;
1003 char *origin;
1005 if ((error = zfs_get_parent(zc->zc_name, parentname,
1006 sizeof (parentname))) != 0)
1007 return (error);
1009 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1010 (error = zfs_secpolicy_write_perms(origin,
1011 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1012 return (error);
1014 if ((error = zfs_secpolicy_write_perms(parentname,
1015 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1016 return (error);
1018 return (zfs_secpolicy_write_perms(parentname,
1019 ZFS_DELEG_PERM_MOUNT, cr));
1023 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1024 * SYS_CONFIG privilege, which is not available in a local zone.
1026 /* ARGSUSED */
1027 static int
1028 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1030 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1031 return (SET_ERROR(EPERM));
1033 return (0);
1037 * Policy for object to name lookups.
1039 /* ARGSUSED */
1040 static int
1041 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1043 int error;
1045 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1046 return (0);
1048 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1049 return (error);
1053 * Policy for fault injection. Requires all privileges.
1055 /* ARGSUSED */
1056 static int
1057 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1059 return (secpolicy_zinject(cr));
1062 /* ARGSUSED */
1063 static int
1064 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1066 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1068 if (prop == ZPROP_INVAL) {
1069 if (!zfs_prop_user(zc->zc_value))
1070 return (SET_ERROR(EINVAL));
1071 return (zfs_secpolicy_write_perms(zc->zc_name,
1072 ZFS_DELEG_PERM_USERPROP, cr));
1073 } else {
1074 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1075 NULL, cr));
1079 static int
1080 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1082 int err = zfs_secpolicy_read(zc, innvl, cr);
1083 if (err)
1084 return (err);
1086 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1087 return (SET_ERROR(EINVAL));
1089 if (zc->zc_value[0] == 0) {
1091 * They are asking about a posix uid/gid. If it's
1092 * themself, allow it.
1094 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1095 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1096 if (zc->zc_guid == crgetuid(cr))
1097 return (0);
1098 } else {
1099 if (groupmember(zc->zc_guid, cr))
1100 return (0);
1104 return (zfs_secpolicy_write_perms(zc->zc_name,
1105 userquota_perms[zc->zc_objset_type], cr));
1108 static int
1109 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1111 int err = zfs_secpolicy_read(zc, innvl, cr);
1112 if (err)
1113 return (err);
1115 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1116 return (SET_ERROR(EINVAL));
1118 return (zfs_secpolicy_write_perms(zc->zc_name,
1119 userquota_perms[zc->zc_objset_type], cr));
1122 /* ARGSUSED */
1123 static int
1124 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1126 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1127 NULL, cr));
1130 /* ARGSUSED */
1131 static int
1132 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1134 nvpair_t *pair;
1135 nvlist_t *holds;
1136 int error;
1138 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1139 if (error != 0)
1140 return (SET_ERROR(EINVAL));
1142 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1143 pair = nvlist_next_nvpair(holds, pair)) {
1144 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1145 error = dmu_fsname(nvpair_name(pair), fsname);
1146 if (error != 0)
1147 return (error);
1148 error = zfs_secpolicy_write_perms(fsname,
1149 ZFS_DELEG_PERM_HOLD, cr);
1150 if (error != 0)
1151 return (error);
1153 return (0);
1156 /* ARGSUSED */
1157 static int
1158 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1160 nvpair_t *pair;
1161 int error;
1163 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1164 pair = nvlist_next_nvpair(innvl, pair)) {
1165 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1166 error = dmu_fsname(nvpair_name(pair), fsname);
1167 if (error != 0)
1168 return (error);
1169 error = zfs_secpolicy_write_perms(fsname,
1170 ZFS_DELEG_PERM_RELEASE, cr);
1171 if (error != 0)
1172 return (error);
1174 return (0);
1178 * Policy for allowing temporary snapshots to be taken or released
1180 static int
1181 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1184 * A temporary snapshot is the same as a snapshot,
1185 * hold, destroy and release all rolled into one.
1186 * Delegated diff alone is sufficient that we allow this.
1188 int error;
1190 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1191 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1192 return (0);
1194 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1195 if (error == 0)
1196 error = zfs_secpolicy_hold(zc, innvl, cr);
1197 if (error == 0)
1198 error = zfs_secpolicy_release(zc, innvl, cr);
1199 if (error == 0)
1200 error = zfs_secpolicy_destroy(zc, innvl, cr);
1201 return (error);
1205 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1207 static int
1208 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1210 char *packed;
1211 int error;
1212 nvlist_t *list = NULL;
1215 * Read in and unpack the user-supplied nvlist.
1217 if (size == 0)
1218 return (SET_ERROR(EINVAL));
1220 packed = kmem_alloc(size, KM_SLEEP);
1222 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1223 iflag)) != 0) {
1224 kmem_free(packed, size);
1225 return (SET_ERROR(EFAULT));
1228 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1229 kmem_free(packed, size);
1230 return (error);
1233 kmem_free(packed, size);
1235 *nvp = list;
1236 return (0);
1240 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1241 * Entries will be removed from the end of the nvlist, and one int32 entry
1242 * named "N_MORE_ERRORS" will be added indicating how many entries were
1243 * removed.
1245 static int
1246 nvlist_smush(nvlist_t *errors, size_t max)
1248 size_t size;
1250 size = fnvlist_size(errors);
1252 if (size > max) {
1253 nvpair_t *more_errors;
1254 int n = 0;
1256 if (max < 1024)
1257 return (SET_ERROR(ENOMEM));
1259 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1260 more_errors = nvlist_prev_nvpair(errors, NULL);
1262 do {
1263 nvpair_t *pair = nvlist_prev_nvpair(errors,
1264 more_errors);
1265 fnvlist_remove_nvpair(errors, pair);
1266 n++;
1267 size = fnvlist_size(errors);
1268 } while (size > max);
1270 fnvlist_remove_nvpair(errors, more_errors);
1271 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1272 ASSERT3U(fnvlist_size(errors), <=, max);
1275 return (0);
1278 static int
1279 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1281 char *packed = NULL;
1282 int error = 0;
1283 size_t size;
1285 size = fnvlist_size(nvl);
1287 if (size > zc->zc_nvlist_dst_size) {
1288 error = SET_ERROR(ENOMEM);
1289 } else {
1290 packed = fnvlist_pack(nvl, &size);
1291 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1292 size, zc->zc_iflags) != 0)
1293 error = SET_ERROR(EFAULT);
1294 fnvlist_pack_free(packed, size);
1297 zc->zc_nvlist_dst_size = size;
1298 zc->zc_nvlist_dst_filled = B_TRUE;
1299 return (error);
1303 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1305 int error = 0;
1306 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1307 return (SET_ERROR(EINVAL));
1310 mutex_enter(&os->os_user_ptr_lock);
1311 *zfvp = dmu_objset_get_user(os);
1312 if (*zfvp) {
1313 VFS_HOLD((*zfvp)->z_vfs);
1314 } else {
1315 error = SET_ERROR(ESRCH);
1317 mutex_exit(&os->os_user_ptr_lock);
1318 return (error);
1322 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1324 objset_t *os;
1325 int error;
1327 error = dmu_objset_hold(dsname, FTAG, &os);
1328 if (error != 0)
1329 return (error);
1331 error = getzfsvfs_impl(os, zfvp);
1332 dmu_objset_rele(os, FTAG);
1333 return (error);
1337 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1338 * case its z_vfs will be NULL, and it will be opened as the owner.
1339 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1340 * which prevents all vnode ops from running.
1342 static int
1343 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1345 int error = 0;
1347 if (getzfsvfs(name, zfvp) != 0)
1348 error = zfsvfs_create(name, zfvp);
1349 if (error == 0) {
1350 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1351 RW_READER, tag);
1352 if ((*zfvp)->z_unmounted) {
1354 * XXX we could probably try again, since the unmounting
1355 * thread should be just about to disassociate the
1356 * objset from the zfsvfs.
1358 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1359 return (SET_ERROR(EBUSY));
1362 return (error);
1365 static void
1366 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1368 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1370 if (zfsvfs->z_vfs) {
1371 VFS_RELE(zfsvfs->z_vfs);
1372 } else {
1373 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1374 zfsvfs_free(zfsvfs);
1378 static int
1379 zfs_ioc_pool_create(zfs_cmd_t *zc)
1381 int error;
1382 nvlist_t *config, *props = NULL;
1383 nvlist_t *rootprops = NULL;
1384 nvlist_t *zplprops = NULL;
1386 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1387 zc->zc_iflags, &config))
1388 return (error);
1390 if (zc->zc_nvlist_src_size != 0 && (error =
1391 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1392 zc->zc_iflags, &props))) {
1393 nvlist_free(config);
1394 return (error);
1397 if (props) {
1398 nvlist_t *nvl = NULL;
1399 uint64_t version = SPA_VERSION;
1401 (void) nvlist_lookup_uint64(props,
1402 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1403 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1404 error = SET_ERROR(EINVAL);
1405 goto pool_props_bad;
1407 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1408 if (nvl) {
1409 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1410 if (error != 0) {
1411 nvlist_free(config);
1412 nvlist_free(props);
1413 return (error);
1415 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1417 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1418 error = zfs_fill_zplprops_root(version, rootprops,
1419 zplprops, NULL);
1420 if (error != 0)
1421 goto pool_props_bad;
1424 error = spa_create(zc->zc_name, config, props, zplprops);
1427 * Set the remaining root properties
1429 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1430 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1431 (void) spa_destroy(zc->zc_name);
1433 pool_props_bad:
1434 nvlist_free(rootprops);
1435 nvlist_free(zplprops);
1436 nvlist_free(config);
1437 nvlist_free(props);
1439 return (error);
1442 static int
1443 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1445 int error;
1446 zfs_log_history(zc);
1447 error = spa_destroy(zc->zc_name);
1448 if (error == 0)
1449 zvol_remove_minors(zc->zc_name);
1450 return (error);
1453 static int
1454 zfs_ioc_pool_import(zfs_cmd_t *zc)
1456 nvlist_t *config, *props = NULL;
1457 uint64_t guid;
1458 int error;
1460 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1461 zc->zc_iflags, &config)) != 0)
1462 return (error);
1464 if (zc->zc_nvlist_src_size != 0 && (error =
1465 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1466 zc->zc_iflags, &props))) {
1467 nvlist_free(config);
1468 return (error);
1471 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1472 guid != zc->zc_guid)
1473 error = SET_ERROR(EINVAL);
1474 else
1475 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1477 if (zc->zc_nvlist_dst != 0) {
1478 int err;
1480 if ((err = put_nvlist(zc, config)) != 0)
1481 error = err;
1484 nvlist_free(config);
1486 nvlist_free(props);
1488 return (error);
1491 static int
1492 zfs_ioc_pool_export(zfs_cmd_t *zc)
1494 int error;
1495 boolean_t force = (boolean_t)zc->zc_cookie;
1496 boolean_t hardforce = (boolean_t)zc->zc_guid;
1498 zfs_log_history(zc);
1499 error = spa_export(zc->zc_name, NULL, force, hardforce);
1500 if (error == 0)
1501 zvol_remove_minors(zc->zc_name);
1502 return (error);
1505 static int
1506 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1508 nvlist_t *configs;
1509 int error;
1511 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1512 return (SET_ERROR(EEXIST));
1514 error = put_nvlist(zc, configs);
1516 nvlist_free(configs);
1518 return (error);
1522 * inputs:
1523 * zc_name name of the pool
1525 * outputs:
1526 * zc_cookie real errno
1527 * zc_nvlist_dst config nvlist
1528 * zc_nvlist_dst_size size of config nvlist
1530 static int
1531 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1533 nvlist_t *config;
1534 int error;
1535 int ret = 0;
1537 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1538 sizeof (zc->zc_value));
1540 if (config != NULL) {
1541 ret = put_nvlist(zc, config);
1542 nvlist_free(config);
1545 * The config may be present even if 'error' is non-zero.
1546 * In this case we return success, and preserve the real errno
1547 * in 'zc_cookie'.
1549 zc->zc_cookie = error;
1550 } else {
1551 ret = error;
1554 return (ret);
1558 * Try to import the given pool, returning pool stats as appropriate so that
1559 * user land knows which devices are available and overall pool health.
1561 static int
1562 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1564 nvlist_t *tryconfig, *config;
1565 int error;
1567 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1568 zc->zc_iflags, &tryconfig)) != 0)
1569 return (error);
1571 config = spa_tryimport(tryconfig);
1573 nvlist_free(tryconfig);
1575 if (config == NULL)
1576 return (SET_ERROR(EINVAL));
1578 error = put_nvlist(zc, config);
1579 nvlist_free(config);
1581 return (error);
1585 * inputs:
1586 * zc_name name of the pool
1587 * zc_cookie scan func (pool_scan_func_t)
1588 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1590 static int
1591 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1593 spa_t *spa;
1594 int error;
1596 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1597 return (error);
1599 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1600 return (SET_ERROR(EINVAL));
1602 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1603 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1604 else if (zc->zc_cookie == POOL_SCAN_NONE)
1605 error = spa_scan_stop(spa);
1606 else
1607 error = spa_scan(spa, zc->zc_cookie);
1609 spa_close(spa, FTAG);
1611 return (error);
1614 static int
1615 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1617 spa_t *spa;
1618 int error;
1620 error = spa_open(zc->zc_name, &spa, FTAG);
1621 if (error == 0) {
1622 spa_freeze(spa);
1623 spa_close(spa, FTAG);
1625 return (error);
1628 static int
1629 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1631 spa_t *spa;
1632 int error;
1634 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1635 return (error);
1637 if (zc->zc_cookie < spa_version(spa) ||
1638 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1639 spa_close(spa, FTAG);
1640 return (SET_ERROR(EINVAL));
1643 spa_upgrade(spa, zc->zc_cookie);
1644 spa_close(spa, FTAG);
1646 return (error);
1649 static int
1650 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1652 spa_t *spa;
1653 char *hist_buf;
1654 uint64_t size;
1655 int error;
1657 if ((size = zc->zc_history_len) == 0)
1658 return (SET_ERROR(EINVAL));
1660 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1661 return (error);
1663 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1664 spa_close(spa, FTAG);
1665 return (SET_ERROR(ENOTSUP));
1668 hist_buf = kmem_alloc(size, KM_SLEEP);
1669 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1670 &zc->zc_history_len, hist_buf)) == 0) {
1671 error = ddi_copyout(hist_buf,
1672 (void *)(uintptr_t)zc->zc_history,
1673 zc->zc_history_len, zc->zc_iflags);
1676 spa_close(spa, FTAG);
1677 kmem_free(hist_buf, size);
1678 return (error);
1681 static int
1682 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1684 spa_t *spa;
1685 int error;
1687 error = spa_open(zc->zc_name, &spa, FTAG);
1688 if (error == 0) {
1689 error = spa_change_guid(spa);
1690 spa_close(spa, FTAG);
1692 return (error);
1695 static int
1696 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1698 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1702 * inputs:
1703 * zc_name name of filesystem
1704 * zc_obj object to find
1706 * outputs:
1707 * zc_value name of object
1709 static int
1710 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1712 objset_t *os;
1713 int error;
1715 /* XXX reading from objset not owned */
1716 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1717 return (error);
1718 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1719 dmu_objset_rele(os, FTAG);
1720 return (SET_ERROR(EINVAL));
1722 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1723 sizeof (zc->zc_value));
1724 dmu_objset_rele(os, FTAG);
1726 return (error);
1730 * inputs:
1731 * zc_name name of filesystem
1732 * zc_obj object to find
1734 * outputs:
1735 * zc_stat stats on object
1736 * zc_value path to object
1738 static int
1739 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1741 objset_t *os;
1742 int error;
1744 /* XXX reading from objset not owned */
1745 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1746 return (error);
1747 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1748 dmu_objset_rele(os, FTAG);
1749 return (SET_ERROR(EINVAL));
1751 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1752 sizeof (zc->zc_value));
1753 dmu_objset_rele(os, FTAG);
1755 return (error);
1758 static int
1759 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1761 spa_t *spa;
1762 int error;
1763 nvlist_t *config, **l2cache, **spares;
1764 uint_t nl2cache = 0, nspares = 0;
1766 error = spa_open(zc->zc_name, &spa, FTAG);
1767 if (error != 0)
1768 return (error);
1770 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1771 zc->zc_iflags, &config);
1772 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1773 &l2cache, &nl2cache);
1775 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1776 &spares, &nspares);
1779 * A root pool with concatenated devices is not supported.
1780 * Thus, can not add a device to a root pool.
1782 * Intent log device can not be added to a rootpool because
1783 * during mountroot, zil is replayed, a seperated log device
1784 * can not be accessed during the mountroot time.
1786 * l2cache and spare devices are ok to be added to a rootpool.
1788 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1789 nvlist_free(config);
1790 spa_close(spa, FTAG);
1791 return (SET_ERROR(EDOM));
1794 if (error == 0) {
1795 error = spa_vdev_add(spa, config);
1796 nvlist_free(config);
1798 spa_close(spa, FTAG);
1799 return (error);
1803 * inputs:
1804 * zc_name name of the pool
1805 * zc_guid guid of vdev to remove
1806 * zc_cookie cancel removal
1808 static int
1809 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1811 spa_t *spa;
1812 int error;
1814 error = spa_open(zc->zc_name, &spa, FTAG);
1815 if (error != 0)
1816 return (error);
1817 if (zc->zc_cookie != 0) {
1818 error = spa_vdev_remove_cancel(spa);
1819 } else {
1820 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1822 spa_close(spa, FTAG);
1823 return (error);
1826 static int
1827 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1829 spa_t *spa;
1830 int error;
1831 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1833 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1834 return (error);
1835 switch (zc->zc_cookie) {
1836 case VDEV_STATE_ONLINE:
1837 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1838 break;
1840 case VDEV_STATE_OFFLINE:
1841 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1842 break;
1844 case VDEV_STATE_FAULTED:
1845 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1846 zc->zc_obj != VDEV_AUX_EXTERNAL)
1847 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1849 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1850 break;
1852 case VDEV_STATE_DEGRADED:
1853 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1854 zc->zc_obj != VDEV_AUX_EXTERNAL)
1855 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1857 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1858 break;
1860 default:
1861 error = SET_ERROR(EINVAL);
1863 zc->zc_cookie = newstate;
1864 spa_close(spa, FTAG);
1865 return (error);
1868 static int
1869 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1871 spa_t *spa;
1872 int replacing = zc->zc_cookie;
1873 nvlist_t *config;
1874 int error;
1876 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1877 return (error);
1879 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1880 zc->zc_iflags, &config)) == 0) {
1881 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1882 nvlist_free(config);
1885 spa_close(spa, FTAG);
1886 return (error);
1889 static int
1890 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1892 spa_t *spa;
1893 int error;
1895 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1896 return (error);
1898 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1900 spa_close(spa, FTAG);
1901 return (error);
1904 static int
1905 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1907 spa_t *spa;
1908 nvlist_t *config, *props = NULL;
1909 int error;
1910 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1912 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1913 return (error);
1915 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1916 zc->zc_iflags, &config)) {
1917 spa_close(spa, FTAG);
1918 return (error);
1921 if (zc->zc_nvlist_src_size != 0 && (error =
1922 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1923 zc->zc_iflags, &props))) {
1924 spa_close(spa, FTAG);
1925 nvlist_free(config);
1926 return (error);
1929 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1931 spa_close(spa, FTAG);
1933 nvlist_free(config);
1934 nvlist_free(props);
1936 return (error);
1939 static int
1940 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1942 spa_t *spa;
1943 char *path = zc->zc_value;
1944 uint64_t guid = zc->zc_guid;
1945 int error;
1947 error = spa_open(zc->zc_name, &spa, FTAG);
1948 if (error != 0)
1949 return (error);
1951 error = spa_vdev_setpath(spa, guid, path);
1952 spa_close(spa, FTAG);
1953 return (error);
1956 static int
1957 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1959 spa_t *spa;
1960 char *fru = zc->zc_value;
1961 uint64_t guid = zc->zc_guid;
1962 int error;
1964 error = spa_open(zc->zc_name, &spa, FTAG);
1965 if (error != 0)
1966 return (error);
1968 error = spa_vdev_setfru(spa, guid, fru);
1969 spa_close(spa, FTAG);
1970 return (error);
1973 static int
1974 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1976 int error = 0;
1977 nvlist_t *nv;
1979 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1981 if (zc->zc_nvlist_dst != 0 &&
1982 (error = dsl_prop_get_all(os, &nv)) == 0) {
1983 dmu_objset_stats(os, nv);
1985 * NB: zvol_get_stats() will read the objset contents,
1986 * which we aren't supposed to do with a
1987 * DS_MODE_USER hold, because it could be
1988 * inconsistent. So this is a bit of a workaround...
1989 * XXX reading with out owning
1991 if (!zc->zc_objset_stats.dds_inconsistent &&
1992 dmu_objset_type(os) == DMU_OST_ZVOL) {
1993 error = zvol_get_stats(os, nv);
1994 if (error == EIO)
1995 return (error);
1996 VERIFY0(error);
1998 error = put_nvlist(zc, nv);
1999 nvlist_free(nv);
2002 return (error);
2006 * inputs:
2007 * zc_name name of filesystem
2008 * zc_nvlist_dst_size size of buffer for property nvlist
2010 * outputs:
2011 * zc_objset_stats stats
2012 * zc_nvlist_dst property nvlist
2013 * zc_nvlist_dst_size size of property nvlist
2015 static int
2016 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2018 objset_t *os;
2019 int error;
2021 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2022 if (error == 0) {
2023 error = zfs_ioc_objset_stats_impl(zc, os);
2024 dmu_objset_rele(os, FTAG);
2027 return (error);
2031 * inputs:
2032 * zc_name name of filesystem
2033 * zc_nvlist_dst_size size of buffer for property nvlist
2035 * outputs:
2036 * zc_nvlist_dst received property nvlist
2037 * zc_nvlist_dst_size size of received property nvlist
2039 * Gets received properties (distinct from local properties on or after
2040 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2041 * local property values.
2043 static int
2044 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2046 int error = 0;
2047 nvlist_t *nv;
2050 * Without this check, we would return local property values if the
2051 * caller has not already received properties on or after
2052 * SPA_VERSION_RECVD_PROPS.
2054 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2055 return (SET_ERROR(ENOTSUP));
2057 if (zc->zc_nvlist_dst != 0 &&
2058 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2059 error = put_nvlist(zc, nv);
2060 nvlist_free(nv);
2063 return (error);
2066 static int
2067 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2069 uint64_t value;
2070 int error;
2073 * zfs_get_zplprop() will either find a value or give us
2074 * the default value (if there is one).
2076 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2077 return (error);
2078 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2079 return (0);
2083 * inputs:
2084 * zc_name name of filesystem
2085 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2087 * outputs:
2088 * zc_nvlist_dst zpl property nvlist
2089 * zc_nvlist_dst_size size of zpl property nvlist
2091 static int
2092 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2094 objset_t *os;
2095 int err;
2097 /* XXX reading without owning */
2098 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2099 return (err);
2101 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2104 * NB: nvl_add_zplprop() will read the objset contents,
2105 * which we aren't supposed to do with a DS_MODE_USER
2106 * hold, because it could be inconsistent.
2108 if (zc->zc_nvlist_dst != (uintptr_t)NULL &&
2109 !zc->zc_objset_stats.dds_inconsistent &&
2110 dmu_objset_type(os) == DMU_OST_ZFS) {
2111 nvlist_t *nv;
2113 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2114 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2115 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2116 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2117 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2118 err = put_nvlist(zc, nv);
2119 nvlist_free(nv);
2120 } else {
2121 err = SET_ERROR(ENOENT);
2123 dmu_objset_rele(os, FTAG);
2124 return (err);
2127 static boolean_t
2128 dataset_name_hidden(const char *name)
2131 * Skip over datasets that are not visible in this zone,
2132 * internal datasets (which have a $ in their name), and
2133 * temporary datasets (which have a % in their name).
2135 if (strchr(name, '$') != NULL)
2136 return (B_TRUE);
2137 if (strchr(name, '%') != NULL)
2138 return (B_TRUE);
2139 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2140 return (B_TRUE);
2141 return (B_FALSE);
2145 * inputs:
2146 * zc_name name of filesystem
2147 * zc_cookie zap cursor
2148 * zc_nvlist_dst_size size of buffer for property nvlist
2150 * outputs:
2151 * zc_name name of next filesystem
2152 * zc_cookie zap cursor
2153 * zc_objset_stats stats
2154 * zc_nvlist_dst property nvlist
2155 * zc_nvlist_dst_size size of property nvlist
2157 static int
2158 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2160 objset_t *os;
2161 int error;
2162 char *p;
2163 size_t orig_len = strlen(zc->zc_name);
2165 top:
2166 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2167 if (error == ENOENT)
2168 error = SET_ERROR(ESRCH);
2169 return (error);
2172 p = strrchr(zc->zc_name, '/');
2173 if (p == NULL || p[1] != '\0')
2174 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2175 p = zc->zc_name + strlen(zc->zc_name);
2177 do {
2178 error = dmu_dir_list_next(os,
2179 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2180 NULL, &zc->zc_cookie);
2181 if (error == ENOENT)
2182 error = SET_ERROR(ESRCH);
2183 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2184 dmu_objset_rele(os, FTAG);
2187 * If it's an internal dataset (ie. with a '$' in its name),
2188 * don't try to get stats for it, otherwise we'll return ENOENT.
2190 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2191 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2192 if (error == ENOENT) {
2193 /* We lost a race with destroy, get the next one. */
2194 zc->zc_name[orig_len] = '\0';
2195 goto top;
2198 return (error);
2202 * inputs:
2203 * zc_name name of filesystem
2204 * zc_cookie zap cursor
2205 * zc_nvlist_dst_size size of buffer for property nvlist
2206 * zc_simple when set, only name is requested
2208 * outputs:
2209 * zc_name name of next snapshot
2210 * zc_objset_stats stats
2211 * zc_nvlist_dst property nvlist
2212 * zc_nvlist_dst_size size of property nvlist
2214 static int
2215 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2217 objset_t *os;
2218 int error;
2220 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2221 if (error != 0) {
2222 return (error == ENOENT ? ESRCH : error);
2226 * A dataset name of maximum length cannot have any snapshots,
2227 * so exit immediately.
2229 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2230 ZFS_MAX_DATASET_NAME_LEN) {
2231 dmu_objset_rele(os, FTAG);
2232 return (SET_ERROR(ESRCH));
2235 error = dmu_snapshot_list_next(os,
2236 sizeof (zc->zc_name) - strlen(zc->zc_name),
2237 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2238 NULL);
2240 if (error == 0 && !zc->zc_simple) {
2241 dsl_dataset_t *ds;
2242 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2244 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2245 if (error == 0) {
2246 objset_t *ossnap;
2248 error = dmu_objset_from_ds(ds, &ossnap);
2249 if (error == 0)
2250 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2251 dsl_dataset_rele(ds, FTAG);
2253 } else if (error == ENOENT) {
2254 error = SET_ERROR(ESRCH);
2257 dmu_objset_rele(os, FTAG);
2258 /* if we failed, undo the @ that we tacked on to zc_name */
2259 if (error != 0)
2260 *strchr(zc->zc_name, '@') = '\0';
2261 return (error);
2264 static int
2265 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2267 const char *propname = nvpair_name(pair);
2268 uint64_t *valary;
2269 unsigned int vallen;
2270 const char *domain;
2271 char *dash;
2272 zfs_userquota_prop_t type;
2273 uint64_t rid;
2274 uint64_t quota;
2275 zfsvfs_t *zfsvfs;
2276 int err;
2278 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2279 nvlist_t *attrs;
2280 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2281 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2282 &pair) != 0)
2283 return (SET_ERROR(EINVAL));
2287 * A correctly constructed propname is encoded as
2288 * userquota@<rid>-<domain>.
2290 if ((dash = strchr(propname, '-')) == NULL ||
2291 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2292 vallen != 3)
2293 return (SET_ERROR(EINVAL));
2295 domain = dash + 1;
2296 type = valary[0];
2297 rid = valary[1];
2298 quota = valary[2];
2300 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2301 if (err == 0) {
2302 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2303 zfsvfs_rele(zfsvfs, FTAG);
2306 return (err);
2310 * If the named property is one that has a special function to set its value,
2311 * return 0 on success and a positive error code on failure; otherwise if it is
2312 * not one of the special properties handled by this function, return -1.
2314 * XXX: It would be better for callers of the property interface if we handled
2315 * these special cases in dsl_prop.c (in the dsl layer).
2317 static int
2318 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2319 nvpair_t *pair)
2321 const char *propname = nvpair_name(pair);
2322 zfs_prop_t prop = zfs_name_to_prop(propname);
2323 uint64_t intval;
2324 int err = -1;
2326 if (prop == ZPROP_INVAL) {
2327 if (zfs_prop_userquota(propname))
2328 return (zfs_prop_set_userquota(dsname, pair));
2329 return (-1);
2332 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2333 nvlist_t *attrs;
2334 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2335 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2336 &pair) == 0);
2339 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2340 return (-1);
2342 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2344 switch (prop) {
2345 case ZFS_PROP_QUOTA:
2346 err = dsl_dir_set_quota(dsname, source, intval);
2347 break;
2348 case ZFS_PROP_REFQUOTA:
2349 err = dsl_dataset_set_refquota(dsname, source, intval);
2350 break;
2351 case ZFS_PROP_FILESYSTEM_LIMIT:
2352 case ZFS_PROP_SNAPSHOT_LIMIT:
2353 if (intval == UINT64_MAX) {
2354 /* clearing the limit, just do it */
2355 err = 0;
2356 } else {
2357 err = dsl_dir_activate_fs_ss_limit(dsname);
2360 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2361 * default path to set the value in the nvlist.
2363 if (err == 0)
2364 err = -1;
2365 break;
2366 case ZFS_PROP_RESERVATION:
2367 err = dsl_dir_set_reservation(dsname, source, intval);
2368 break;
2369 case ZFS_PROP_REFRESERVATION:
2370 err = dsl_dataset_set_refreservation(dsname, source, intval);
2371 break;
2372 case ZFS_PROP_VOLSIZE:
2373 err = zvol_set_volsize(dsname, intval);
2374 break;
2375 case ZFS_PROP_VERSION:
2377 zfsvfs_t *zfsvfs;
2379 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2380 break;
2382 err = zfs_set_version(zfsvfs, intval);
2383 zfsvfs_rele(zfsvfs, FTAG);
2385 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2386 zfs_cmd_t *zc;
2388 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2389 (void) strcpy(zc->zc_name, dsname);
2390 (void) zfs_ioc_userspace_upgrade(zc);
2391 kmem_free(zc, sizeof (zfs_cmd_t));
2393 break;
2395 default:
2396 err = -1;
2399 return (err);
2403 * This function is best effort. If it fails to set any of the given properties,
2404 * it continues to set as many as it can and returns the last error
2405 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2406 * with the list of names of all the properties that failed along with the
2407 * corresponding error numbers.
2409 * If every property is set successfully, zero is returned and errlist is not
2410 * modified.
2413 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2414 nvlist_t *errlist)
2416 nvpair_t *pair;
2417 nvpair_t *propval;
2418 int rv = 0;
2419 uint64_t intval;
2420 char *strval;
2421 nvlist_t *genericnvl = fnvlist_alloc();
2422 nvlist_t *retrynvl = fnvlist_alloc();
2424 retry:
2425 pair = NULL;
2426 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2427 const char *propname = nvpair_name(pair);
2428 zfs_prop_t prop = zfs_name_to_prop(propname);
2429 int err = 0;
2431 /* decode the property value */
2432 propval = pair;
2433 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2434 nvlist_t *attrs;
2435 attrs = fnvpair_value_nvlist(pair);
2436 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2437 &propval) != 0)
2438 err = SET_ERROR(EINVAL);
2441 /* Validate value type */
2442 if (err == 0 && prop == ZPROP_INVAL) {
2443 if (zfs_prop_user(propname)) {
2444 if (nvpair_type(propval) != DATA_TYPE_STRING)
2445 err = SET_ERROR(EINVAL);
2446 } else if (zfs_prop_userquota(propname)) {
2447 if (nvpair_type(propval) !=
2448 DATA_TYPE_UINT64_ARRAY)
2449 err = SET_ERROR(EINVAL);
2450 } else {
2451 err = SET_ERROR(EINVAL);
2453 } else if (err == 0) {
2454 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2455 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2456 err = SET_ERROR(EINVAL);
2457 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2458 const char *unused;
2460 intval = fnvpair_value_uint64(propval);
2462 switch (zfs_prop_get_type(prop)) {
2463 case PROP_TYPE_NUMBER:
2464 break;
2465 case PROP_TYPE_STRING:
2466 err = SET_ERROR(EINVAL);
2467 break;
2468 case PROP_TYPE_INDEX:
2469 if (zfs_prop_index_to_string(prop,
2470 intval, &unused) != 0)
2471 err = SET_ERROR(EINVAL);
2472 break;
2473 default:
2474 cmn_err(CE_PANIC,
2475 "unknown property type");
2477 } else {
2478 err = SET_ERROR(EINVAL);
2482 /* Validate permissions */
2483 if (err == 0)
2484 err = zfs_check_settable(dsname, pair, CRED());
2486 if (err == 0) {
2487 err = zfs_prop_set_special(dsname, source, pair);
2488 if (err == -1) {
2490 * For better performance we build up a list of
2491 * properties to set in a single transaction.
2493 err = nvlist_add_nvpair(genericnvl, pair);
2494 } else if (err != 0 && nvl != retrynvl) {
2496 * This may be a spurious error caused by
2497 * receiving quota and reservation out of order.
2498 * Try again in a second pass.
2500 err = nvlist_add_nvpair(retrynvl, pair);
2504 if (err != 0) {
2505 if (errlist != NULL)
2506 fnvlist_add_int32(errlist, propname, err);
2507 rv = err;
2511 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2512 nvl = retrynvl;
2513 goto retry;
2516 if (!nvlist_empty(genericnvl) &&
2517 dsl_props_set(dsname, source, genericnvl) != 0) {
2519 * If this fails, we still want to set as many properties as we
2520 * can, so try setting them individually.
2522 pair = NULL;
2523 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2524 const char *propname = nvpair_name(pair);
2525 int err = 0;
2527 propval = pair;
2528 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2529 nvlist_t *attrs;
2530 attrs = fnvpair_value_nvlist(pair);
2531 propval = fnvlist_lookup_nvpair(attrs,
2532 ZPROP_VALUE);
2535 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2536 strval = fnvpair_value_string(propval);
2537 err = dsl_prop_set_string(dsname, propname,
2538 source, strval);
2539 } else {
2540 intval = fnvpair_value_uint64(propval);
2541 err = dsl_prop_set_int(dsname, propname, source,
2542 intval);
2545 if (err != 0) {
2546 if (errlist != NULL) {
2547 fnvlist_add_int32(errlist, propname,
2548 err);
2550 rv = err;
2554 nvlist_free(genericnvl);
2555 nvlist_free(retrynvl);
2557 return (rv);
2561 * Check that all the properties are valid user properties.
2563 static int
2564 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2566 nvpair_t *pair = NULL;
2567 int error = 0;
2569 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2570 const char *propname = nvpair_name(pair);
2572 if (!zfs_prop_user(propname) ||
2573 nvpair_type(pair) != DATA_TYPE_STRING)
2574 return (SET_ERROR(EINVAL));
2576 if (error = zfs_secpolicy_write_perms(fsname,
2577 ZFS_DELEG_PERM_USERPROP, CRED()))
2578 return (error);
2580 if (strlen(propname) >= ZAP_MAXNAMELEN)
2581 return (SET_ERROR(ENAMETOOLONG));
2583 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2584 return (E2BIG);
2586 return (0);
2589 static void
2590 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2592 nvpair_t *pair;
2594 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2596 pair = NULL;
2597 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2598 if (nvlist_exists(skipped, nvpair_name(pair)))
2599 continue;
2601 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2605 static int
2606 clear_received_props(const char *dsname, nvlist_t *props,
2607 nvlist_t *skipped)
2609 int err = 0;
2610 nvlist_t *cleared_props = NULL;
2611 props_skip(props, skipped, &cleared_props);
2612 if (!nvlist_empty(cleared_props)) {
2614 * Acts on local properties until the dataset has received
2615 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2617 zprop_source_t flags = (ZPROP_SRC_NONE |
2618 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2619 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2621 nvlist_free(cleared_props);
2622 return (err);
2626 * inputs:
2627 * zc_name name of filesystem
2628 * zc_value name of property to set
2629 * zc_nvlist_src{_size} nvlist of properties to apply
2630 * zc_cookie received properties flag
2632 * outputs:
2633 * zc_nvlist_dst{_size} error for each unapplied received property
2635 static int
2636 zfs_ioc_set_prop(zfs_cmd_t *zc)
2638 nvlist_t *nvl;
2639 boolean_t received = zc->zc_cookie;
2640 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2641 ZPROP_SRC_LOCAL);
2642 nvlist_t *errors;
2643 int error;
2645 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2646 zc->zc_iflags, &nvl)) != 0)
2647 return (error);
2649 if (received) {
2650 nvlist_t *origprops;
2652 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2653 (void) clear_received_props(zc->zc_name,
2654 origprops, nvl);
2655 nvlist_free(origprops);
2658 error = dsl_prop_set_hasrecvd(zc->zc_name);
2661 errors = fnvlist_alloc();
2662 if (error == 0)
2663 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2665 if (zc->zc_nvlist_dst != (uintptr_t)NULL && errors != NULL) {
2666 (void) put_nvlist(zc, errors);
2669 nvlist_free(errors);
2670 nvlist_free(nvl);
2671 return (error);
2675 * inputs:
2676 * zc_name name of filesystem
2677 * zc_value name of property to inherit
2678 * zc_cookie revert to received value if TRUE
2680 * outputs: none
2682 static int
2683 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2685 const char *propname = zc->zc_value;
2686 zfs_prop_t prop = zfs_name_to_prop(propname);
2687 boolean_t received = zc->zc_cookie;
2688 zprop_source_t source = (received
2689 ? ZPROP_SRC_NONE /* revert to received value, if any */
2690 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2692 if (received) {
2693 nvlist_t *dummy;
2694 nvpair_t *pair;
2695 zprop_type_t type;
2696 int err;
2699 * zfs_prop_set_special() expects properties in the form of an
2700 * nvpair with type info.
2702 if (prop == ZPROP_INVAL) {
2703 if (!zfs_prop_user(propname))
2704 return (SET_ERROR(EINVAL));
2706 type = PROP_TYPE_STRING;
2707 } else if (prop == ZFS_PROP_VOLSIZE ||
2708 prop == ZFS_PROP_VERSION) {
2709 return (SET_ERROR(EINVAL));
2710 } else {
2711 type = zfs_prop_get_type(prop);
2714 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2716 switch (type) {
2717 case PROP_TYPE_STRING:
2718 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2719 break;
2720 case PROP_TYPE_NUMBER:
2721 case PROP_TYPE_INDEX:
2722 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2723 break;
2724 default:
2725 nvlist_free(dummy);
2726 return (SET_ERROR(EINVAL));
2729 pair = nvlist_next_nvpair(dummy, NULL);
2730 err = zfs_prop_set_special(zc->zc_name, source, pair);
2731 nvlist_free(dummy);
2732 if (err != -1)
2733 return (err); /* special property already handled */
2734 } else {
2736 * Only check this in the non-received case. We want to allow
2737 * 'inherit -S' to revert non-inheritable properties like quota
2738 * and reservation to the received or default values even though
2739 * they are not considered inheritable.
2741 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2742 return (SET_ERROR(EINVAL));
2745 /* property name has been validated by zfs_secpolicy_inherit_prop() */
2746 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2749 static int
2750 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2752 nvlist_t *props;
2753 spa_t *spa;
2754 int error;
2755 nvpair_t *pair;
2757 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2758 zc->zc_iflags, &props))
2759 return (error);
2762 * If the only property is the configfile, then just do a spa_lookup()
2763 * to handle the faulted case.
2765 pair = nvlist_next_nvpair(props, NULL);
2766 if (pair != NULL && strcmp(nvpair_name(pair),
2767 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2768 nvlist_next_nvpair(props, pair) == NULL) {
2769 mutex_enter(&spa_namespace_lock);
2770 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2771 spa_configfile_set(spa, props, B_FALSE);
2772 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2774 mutex_exit(&spa_namespace_lock);
2775 if (spa != NULL) {
2776 nvlist_free(props);
2777 return (0);
2781 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2782 nvlist_free(props);
2783 return (error);
2786 error = spa_prop_set(spa, props);
2788 nvlist_free(props);
2789 spa_close(spa, FTAG);
2791 return (error);
2794 static int
2795 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2797 spa_t *spa;
2798 int error;
2799 nvlist_t *nvp = NULL;
2801 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2803 * If the pool is faulted, there may be properties we can still
2804 * get (such as altroot and cachefile), so attempt to get them
2805 * anyway.
2807 mutex_enter(&spa_namespace_lock);
2808 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2809 error = spa_prop_get(spa, &nvp);
2810 mutex_exit(&spa_namespace_lock);
2811 } else {
2812 error = spa_prop_get(spa, &nvp);
2813 spa_close(spa, FTAG);
2816 if (error == 0 && zc->zc_nvlist_dst != (uintptr_t)NULL)
2817 error = put_nvlist(zc, nvp);
2818 else
2819 error = SET_ERROR(EFAULT);
2821 nvlist_free(nvp);
2822 return (error);
2826 * inputs:
2827 * zc_name name of filesystem
2828 * zc_nvlist_src{_size} nvlist of delegated permissions
2829 * zc_perm_action allow/unallow flag
2831 * outputs: none
2833 static int
2834 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2836 int error;
2837 nvlist_t *fsaclnv = NULL;
2839 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2840 zc->zc_iflags, &fsaclnv)) != 0)
2841 return (error);
2844 * Verify nvlist is constructed correctly
2846 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2847 nvlist_free(fsaclnv);
2848 return (SET_ERROR(EINVAL));
2852 * If we don't have PRIV_SYS_MOUNT, then validate
2853 * that user is allowed to hand out each permission in
2854 * the nvlist(s)
2857 error = secpolicy_zfs(CRED());
2858 if (error != 0) {
2859 if (zc->zc_perm_action == B_FALSE) {
2860 error = dsl_deleg_can_allow(zc->zc_name,
2861 fsaclnv, CRED());
2862 } else {
2863 error = dsl_deleg_can_unallow(zc->zc_name,
2864 fsaclnv, CRED());
2868 if (error == 0)
2869 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2871 nvlist_free(fsaclnv);
2872 return (error);
2876 * inputs:
2877 * zc_name name of filesystem
2879 * outputs:
2880 * zc_nvlist_src{_size} nvlist of delegated permissions
2882 static int
2883 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2885 nvlist_t *nvp;
2886 int error;
2888 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2889 error = put_nvlist(zc, nvp);
2890 nvlist_free(nvp);
2893 return (error);
2896 /* ARGSUSED */
2897 static void
2898 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2900 zfs_creat_t *zct = arg;
2902 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2905 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2908 * inputs:
2909 * os parent objset pointer (NULL if root fs)
2910 * fuids_ok fuids allowed in this version of the spa?
2911 * sa_ok SAs allowed in this version of the spa?
2912 * createprops list of properties requested by creator
2914 * outputs:
2915 * zplprops values for the zplprops we attach to the master node object
2916 * is_ci true if requested file system will be purely case-insensitive
2918 * Determine the settings for utf8only, normalization and
2919 * casesensitivity. Specific values may have been requested by the
2920 * creator and/or we can inherit values from the parent dataset. If
2921 * the file system is of too early a vintage, a creator can not
2922 * request settings for these properties, even if the requested
2923 * setting is the default value. We don't actually want to create dsl
2924 * properties for these, so remove them from the source nvlist after
2925 * processing.
2927 static int
2928 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2929 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2930 nvlist_t *zplprops, boolean_t *is_ci)
2932 uint64_t sense = ZFS_PROP_UNDEFINED;
2933 uint64_t norm = ZFS_PROP_UNDEFINED;
2934 uint64_t u8 = ZFS_PROP_UNDEFINED;
2936 ASSERT(zplprops != NULL);
2938 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
2939 return (SET_ERROR(EINVAL));
2942 * Pull out creator prop choices, if any.
2944 if (createprops) {
2945 (void) nvlist_lookup_uint64(createprops,
2946 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2947 (void) nvlist_lookup_uint64(createprops,
2948 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2949 (void) nvlist_remove_all(createprops,
2950 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2951 (void) nvlist_lookup_uint64(createprops,
2952 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2953 (void) nvlist_remove_all(createprops,
2954 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2955 (void) nvlist_lookup_uint64(createprops,
2956 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2957 (void) nvlist_remove_all(createprops,
2958 zfs_prop_to_name(ZFS_PROP_CASE));
2962 * If the zpl version requested is whacky or the file system
2963 * or pool is version is too "young" to support normalization
2964 * and the creator tried to set a value for one of the props,
2965 * error out.
2967 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2968 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2969 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2970 (zplver < ZPL_VERSION_NORMALIZATION &&
2971 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2972 sense != ZFS_PROP_UNDEFINED)))
2973 return (SET_ERROR(ENOTSUP));
2976 * Put the version in the zplprops
2978 VERIFY(nvlist_add_uint64(zplprops,
2979 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2981 if (norm == ZFS_PROP_UNDEFINED)
2982 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2983 VERIFY(nvlist_add_uint64(zplprops,
2984 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2987 * If we're normalizing, names must always be valid UTF-8 strings.
2989 if (norm)
2990 u8 = 1;
2991 if (u8 == ZFS_PROP_UNDEFINED)
2992 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2993 VERIFY(nvlist_add_uint64(zplprops,
2994 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2996 if (sense == ZFS_PROP_UNDEFINED)
2997 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2998 VERIFY(nvlist_add_uint64(zplprops,
2999 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3001 if (is_ci)
3002 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3004 return (0);
3007 static int
3008 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3009 nvlist_t *zplprops, boolean_t *is_ci)
3011 boolean_t fuids_ok, sa_ok;
3012 uint64_t zplver = ZPL_VERSION;
3013 objset_t *os = NULL;
3014 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3015 char *cp;
3016 spa_t *spa;
3017 uint64_t spa_vers;
3018 int error;
3020 (void) strlcpy(parentname, dataset, sizeof (parentname));
3021 cp = strrchr(parentname, '/');
3022 ASSERT(cp != NULL);
3023 cp[0] = '\0';
3025 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3026 return (error);
3028 spa_vers = spa_version(spa);
3029 spa_close(spa, FTAG);
3031 zplver = zfs_zpl_version_map(spa_vers);
3032 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3033 sa_ok = (zplver >= ZPL_VERSION_SA);
3036 * Open parent object set so we can inherit zplprop values.
3038 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3039 return (error);
3041 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3042 zplprops, is_ci);
3043 dmu_objset_rele(os, FTAG);
3044 return (error);
3047 static int
3048 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3049 nvlist_t *zplprops, boolean_t *is_ci)
3051 boolean_t fuids_ok;
3052 boolean_t sa_ok;
3053 uint64_t zplver = ZPL_VERSION;
3054 int error;
3056 zplver = zfs_zpl_version_map(spa_vers);
3057 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3058 sa_ok = (zplver >= ZPL_VERSION_SA);
3060 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3061 createprops, zplprops, is_ci);
3062 return (error);
3066 * innvl: {
3067 * "type" -> dmu_objset_type_t (int32)
3068 * (optional) "props" -> { prop -> value }
3071 * outnvl: propname -> error code (int32)
3073 static int
3074 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3076 int error = 0;
3077 zfs_creat_t zct = { 0 };
3078 nvlist_t *nvprops = NULL;
3079 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3080 int32_t type32;
3081 dmu_objset_type_t type;
3082 boolean_t is_insensitive = B_FALSE;
3084 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3085 return (SET_ERROR(EINVAL));
3086 type = type32;
3087 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3089 switch (type) {
3090 case DMU_OST_ZFS:
3091 cbfunc = zfs_create_cb;
3092 break;
3094 case DMU_OST_ZVOL:
3095 cbfunc = zvol_create_cb;
3096 break;
3098 default:
3099 cbfunc = NULL;
3100 break;
3102 if (strchr(fsname, '@') ||
3103 strchr(fsname, '%'))
3104 return (SET_ERROR(EINVAL));
3106 zct.zct_props = nvprops;
3108 if (cbfunc == NULL)
3109 return (SET_ERROR(EINVAL));
3111 if (type == DMU_OST_ZVOL) {
3112 uint64_t volsize, volblocksize;
3114 if (nvprops == NULL)
3115 return (SET_ERROR(EINVAL));
3116 if (nvlist_lookup_uint64(nvprops,
3117 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3118 return (SET_ERROR(EINVAL));
3120 if ((error = nvlist_lookup_uint64(nvprops,
3121 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3122 &volblocksize)) != 0 && error != ENOENT)
3123 return (SET_ERROR(EINVAL));
3125 if (error != 0)
3126 volblocksize = zfs_prop_default_numeric(
3127 ZFS_PROP_VOLBLOCKSIZE);
3129 if ((error = zvol_check_volblocksize(
3130 volblocksize)) != 0 ||
3131 (error = zvol_check_volsize(volsize,
3132 volblocksize)) != 0)
3133 return (error);
3134 } else if (type == DMU_OST_ZFS) {
3135 int error;
3138 * We have to have normalization and
3139 * case-folding flags correct when we do the
3140 * file system creation, so go figure them out
3141 * now.
3143 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3144 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3145 error = zfs_fill_zplprops(fsname, nvprops,
3146 zct.zct_zplprops, &is_insensitive);
3147 if (error != 0) {
3148 nvlist_free(zct.zct_zplprops);
3149 return (error);
3153 error = dmu_objset_create(fsname, type,
3154 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3155 nvlist_free(zct.zct_zplprops);
3158 * It would be nice to do this atomically.
3160 if (error == 0) {
3161 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3162 nvprops, outnvl);
3163 if (error != 0)
3164 (void) dsl_destroy_head(fsname);
3166 return (error);
3170 * innvl: {
3171 * "origin" -> name of origin snapshot
3172 * (optional) "props" -> { prop -> value }
3175 * outnvl: propname -> error code (int32)
3177 static int
3178 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3180 int error = 0;
3181 nvlist_t *nvprops = NULL;
3182 char *origin_name;
3184 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3185 return (SET_ERROR(EINVAL));
3186 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3188 if (strchr(fsname, '@') ||
3189 strchr(fsname, '%'))
3190 return (SET_ERROR(EINVAL));
3192 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3193 return (SET_ERROR(EINVAL));
3194 error = dmu_objset_clone(fsname, origin_name);
3195 if (error != 0)
3196 return (error);
3199 * It would be nice to do this atomically.
3201 if (error == 0) {
3202 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3203 nvprops, outnvl);
3204 if (error != 0)
3205 (void) dsl_destroy_head(fsname);
3207 return (error);
3210 /* ARGSUSED */
3211 static int
3212 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3214 if (strchr(fsname, '@') ||
3215 strchr(fsname, '%'))
3216 return (SET_ERROR(EINVAL));
3218 return (dmu_objset_remap_indirects(fsname));
3222 * innvl: {
3223 * "snaps" -> { snapshot1, snapshot2 }
3224 * (optional) "props" -> { prop -> value (string) }
3227 * outnvl: snapshot -> error code (int32)
3229 static int
3230 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3232 nvlist_t *snaps;
3233 nvlist_t *props = NULL;
3234 int error, poollen;
3235 nvpair_t *pair;
3237 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3238 if ((error = zfs_check_userprops(poolname, props)) != 0)
3239 return (error);
3241 if (!nvlist_empty(props) &&
3242 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3243 return (SET_ERROR(ENOTSUP));
3245 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3246 return (SET_ERROR(EINVAL));
3247 poollen = strlen(poolname);
3248 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3249 pair = nvlist_next_nvpair(snaps, pair)) {
3250 const char *name = nvpair_name(pair);
3251 const char *cp = strchr(name, '@');
3254 * The snap name must contain an @, and the part after it must
3255 * contain only valid characters.
3257 if (cp == NULL ||
3258 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3259 return (SET_ERROR(EINVAL));
3262 * The snap must be in the specified pool.
3264 if (strncmp(name, poolname, poollen) != 0 ||
3265 (name[poollen] != '/' && name[poollen] != '@'))
3266 return (SET_ERROR(EXDEV));
3268 /* This must be the only snap of this fs. */
3269 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3270 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3271 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3272 == 0) {
3273 return (SET_ERROR(EXDEV));
3278 error = dsl_dataset_snapshot(snaps, props, outnvl);
3279 return (error);
3283 * innvl: "message" -> string
3285 /* ARGSUSED */
3286 static int
3287 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3289 char *message;
3290 spa_t *spa;
3291 int error;
3292 char *poolname;
3295 * The poolname in the ioctl is not set, we get it from the TSD,
3296 * which was set at the end of the last successful ioctl that allows
3297 * logging. The secpolicy func already checked that it is set.
3298 * Only one log ioctl is allowed after each successful ioctl, so
3299 * we clear the TSD here.
3301 poolname = tsd_get(zfs_allow_log_key);
3302 (void) tsd_set(zfs_allow_log_key, NULL);
3303 error = spa_open(poolname, &spa, FTAG);
3304 strfree(poolname);
3305 if (error != 0)
3306 return (error);
3308 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3309 spa_close(spa, FTAG);
3310 return (SET_ERROR(EINVAL));
3313 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3314 spa_close(spa, FTAG);
3315 return (SET_ERROR(ENOTSUP));
3318 error = spa_history_log(spa, message);
3319 spa_close(spa, FTAG);
3320 return (error);
3324 * The dp_config_rwlock must not be held when calling this, because the
3325 * unmount may need to write out data.
3327 * This function is best-effort. Callers must deal gracefully if it
3328 * remains mounted (or is remounted after this call).
3330 * Returns 0 if the argument is not a snapshot, or it is not currently a
3331 * filesystem, or we were able to unmount it. Returns error code otherwise.
3333 void
3334 zfs_unmount_snap(const char *snapname)
3336 vfs_t *vfsp = NULL;
3337 zfsvfs_t *zfsvfs = NULL;
3339 if (strchr(snapname, '@') == NULL)
3340 return;
3342 int err = getzfsvfs(snapname, &zfsvfs);
3343 if (err != 0) {
3344 ASSERT3P(zfsvfs, ==, NULL);
3345 return;
3347 vfsp = zfsvfs->z_vfs;
3349 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3351 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3352 VFS_RELE(vfsp);
3353 if (err != 0)
3354 return;
3357 * Always force the unmount for snapshots.
3359 (void) dounmount(vfsp, MS_FORCE, kcred);
3362 /* ARGSUSED */
3363 static int
3364 zfs_unmount_snap_cb(const char *snapname, void *arg)
3366 zfs_unmount_snap(snapname);
3367 return (0);
3371 * When a clone is destroyed, its origin may also need to be destroyed,
3372 * in which case it must be unmounted. This routine will do that unmount
3373 * if necessary.
3375 void
3376 zfs_destroy_unmount_origin(const char *fsname)
3378 int error;
3379 objset_t *os;
3380 dsl_dataset_t *ds;
3382 error = dmu_objset_hold(fsname, FTAG, &os);
3383 if (error != 0)
3384 return;
3385 ds = dmu_objset_ds(os);
3386 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3387 char originname[ZFS_MAX_DATASET_NAME_LEN];
3388 dsl_dataset_name(ds->ds_prev, originname);
3389 dmu_objset_rele(os, FTAG);
3390 zfs_unmount_snap(originname);
3391 } else {
3392 dmu_objset_rele(os, FTAG);
3397 * innvl: {
3398 * "snaps" -> { snapshot1, snapshot2 }
3399 * (optional boolean) "defer"
3402 * outnvl: snapshot -> error code (int32)
3405 /* ARGSUSED */
3406 static int
3407 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3409 nvlist_t *snaps;
3410 nvpair_t *pair;
3411 boolean_t defer;
3413 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3414 return (SET_ERROR(EINVAL));
3415 defer = nvlist_exists(innvl, "defer");
3417 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3418 pair = nvlist_next_nvpair(snaps, pair)) {
3419 zfs_unmount_snap(nvpair_name(pair));
3422 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3426 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3427 * All bookmarks must be in the same pool.
3429 * innvl: {
3430 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3433 * outnvl: bookmark -> error code (int32)
3436 /* ARGSUSED */
3437 static int
3438 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3440 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3441 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3442 char *snap_name;
3445 * Verify the snapshot argument.
3447 if (nvpair_value_string(pair, &snap_name) != 0)
3448 return (SET_ERROR(EINVAL));
3451 /* Verify that the keys (bookmarks) are unique */
3452 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3453 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3454 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3455 return (SET_ERROR(EINVAL));
3459 return (dsl_bookmark_create(innvl, outnvl));
3463 * innvl: {
3464 * property 1, property 2, ...
3467 * outnvl: {
3468 * bookmark name 1 -> { property 1, property 2, ... },
3469 * bookmark name 2 -> { property 1, property 2, ... }
3473 static int
3474 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3476 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3480 * innvl: {
3481 * bookmark name 1, bookmark name 2
3484 * outnvl: bookmark -> error code (int32)
3487 static int
3488 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3489 nvlist_t *outnvl)
3491 int error, poollen;
3493 poollen = strlen(poolname);
3494 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3495 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3496 const char *name = nvpair_name(pair);
3497 const char *cp = strchr(name, '#');
3500 * The bookmark name must contain an #, and the part after it
3501 * must contain only valid characters.
3503 if (cp == NULL ||
3504 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3505 return (SET_ERROR(EINVAL));
3508 * The bookmark must be in the specified pool.
3510 if (strncmp(name, poolname, poollen) != 0 ||
3511 (name[poollen] != '/' && name[poollen] != '#'))
3512 return (SET_ERROR(EXDEV));
3515 error = dsl_bookmark_destroy(innvl, outnvl);
3516 return (error);
3519 static int
3520 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3521 nvlist_t *outnvl)
3523 char *program;
3524 uint64_t instrlimit, memlimit;
3525 boolean_t sync_flag;
3526 nvpair_t *nvarg = NULL;
3528 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3529 return (EINVAL);
3531 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3532 sync_flag = B_TRUE;
3534 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3535 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3537 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3538 memlimit = ZCP_DEFAULT_MEMLIMIT;
3540 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3541 return (EINVAL);
3544 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3545 return (EINVAL);
3546 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3547 return (EINVAL);
3549 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3550 nvarg, outnvl));
3554 * innvl: unused
3555 * outnvl: empty
3557 /* ARGSUSED */
3558 static int
3559 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3561 return (spa_checkpoint(poolname));
3565 * innvl: unused
3566 * outnvl: empty
3568 /* ARGSUSED */
3569 static int
3570 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3571 nvlist_t *outnvl)
3573 return (spa_checkpoint_discard(poolname));
3577 * inputs:
3578 * zc_name name of dataset to destroy
3579 * zc_objset_type type of objset
3580 * zc_defer_destroy mark for deferred destroy
3582 * outputs: none
3584 static int
3585 zfs_ioc_destroy(zfs_cmd_t *zc)
3587 int err;
3589 if (zc->zc_objset_type == DMU_OST_ZFS)
3590 zfs_unmount_snap(zc->zc_name);
3592 if (strchr(zc->zc_name, '@'))
3593 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3594 else
3595 err = dsl_destroy_head(zc->zc_name);
3596 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3597 (void) zvol_remove_minor(zc->zc_name);
3598 return (err);
3602 * innvl: {
3603 * vdevs: {
3604 * guid 1, guid 2, ...
3605 * },
3606 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3609 * outnvl: {
3610 * [func: EINVAL (if provided command type didn't make sense)],
3611 * [vdevs: {
3612 * guid1: errno, (see function body for possible errnos)
3613 * ...
3614 * }]
3618 static int
3619 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3621 spa_t *spa;
3622 int error;
3624 error = spa_open(poolname, &spa, FTAG);
3625 if (error != 0)
3626 return (error);
3628 uint64_t cmd_type;
3629 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3630 &cmd_type) != 0) {
3631 spa_close(spa, FTAG);
3632 return (SET_ERROR(EINVAL));
3634 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3635 cmd_type == POOL_INITIALIZE_DO ||
3636 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3637 spa_close(spa, FTAG);
3638 return (SET_ERROR(EINVAL));
3641 nvlist_t *vdev_guids;
3642 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3643 &vdev_guids) != 0) {
3644 spa_close(spa, FTAG);
3645 return (SET_ERROR(EINVAL));
3648 nvlist_t *vdev_errlist = fnvlist_alloc();
3649 int total_errors = 0;
3651 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3652 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3653 uint64_t vdev_guid = fnvpair_value_uint64(pair);
3655 error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3656 if (error != 0) {
3657 char guid_as_str[MAXNAMELEN];
3659 (void) snprintf(guid_as_str, sizeof (guid_as_str),
3660 "%llu", (unsigned long long)vdev_guid);
3661 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3662 total_errors++;
3665 if (fnvlist_size(vdev_errlist) > 0) {
3666 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3667 vdev_errlist);
3669 fnvlist_free(vdev_errlist);
3671 spa_close(spa, FTAG);
3672 return (total_errors > 0 ? EINVAL : 0);
3676 * fsname is name of dataset to rollback (to most recent snapshot)
3678 * innvl may contain name of expected target snapshot
3680 * outnvl: "target" -> name of most recent snapshot
3683 /* ARGSUSED */
3684 static int
3685 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3687 zfsvfs_t *zfsvfs;
3688 char *target = NULL;
3689 int error;
3691 (void) nvlist_lookup_string(innvl, "target", &target);
3692 if (target != NULL) {
3693 const char *cp = strchr(target, '@');
3696 * The snap name must contain an @, and the part after it must
3697 * contain only valid characters.
3699 if (cp == NULL ||
3700 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3701 return (SET_ERROR(EINVAL));
3704 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3705 dsl_dataset_t *ds;
3707 ds = dmu_objset_ds(zfsvfs->z_os);
3708 error = zfs_suspend_fs(zfsvfs);
3709 if (error == 0) {
3710 int resume_err;
3712 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3713 outnvl);
3714 resume_err = zfs_resume_fs(zfsvfs, ds);
3715 error = error ? error : resume_err;
3717 VFS_RELE(zfsvfs->z_vfs);
3718 } else {
3719 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3721 return (error);
3724 static int
3725 recursive_unmount(const char *fsname, void *arg)
3727 const char *snapname = arg;
3728 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3730 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3731 zfs_unmount_snap(fullname);
3733 return (0);
3737 * inputs:
3738 * zc_name old name of dataset
3739 * zc_value new name of dataset
3740 * zc_cookie recursive flag (only valid for snapshots)
3742 * outputs: none
3744 static int
3745 zfs_ioc_rename(zfs_cmd_t *zc)
3747 boolean_t recursive = zc->zc_cookie & 1;
3748 char *at;
3750 /* "zfs rename" from and to ...%recv datasets should both fail */
3751 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3752 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3753 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3754 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3755 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3756 return (SET_ERROR(EINVAL));
3758 at = strchr(zc->zc_name, '@');
3759 if (at != NULL) {
3760 /* snaps must be in same fs */
3761 int error;
3763 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3764 return (SET_ERROR(EXDEV));
3765 *at = '\0';
3766 if (zc->zc_objset_type == DMU_OST_ZFS) {
3767 error = dmu_objset_find(zc->zc_name,
3768 recursive_unmount, at + 1,
3769 recursive ? DS_FIND_CHILDREN : 0);
3770 if (error != 0) {
3771 *at = '@';
3772 return (error);
3775 error = dsl_dataset_rename_snapshot(zc->zc_name,
3776 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3777 *at = '@';
3779 return (error);
3780 } else {
3781 if (zc->zc_objset_type == DMU_OST_ZVOL)
3782 (void) zvol_remove_minor(zc->zc_name);
3783 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3787 static int
3788 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3790 const char *propname = nvpair_name(pair);
3791 boolean_t issnap = (strchr(dsname, '@') != NULL);
3792 zfs_prop_t prop = zfs_name_to_prop(propname);
3793 uint64_t intval;
3794 int err;
3796 if (prop == ZPROP_INVAL) {
3797 if (zfs_prop_user(propname)) {
3798 if (err = zfs_secpolicy_write_perms(dsname,
3799 ZFS_DELEG_PERM_USERPROP, cr))
3800 return (err);
3801 return (0);
3804 if (!issnap && zfs_prop_userquota(propname)) {
3805 const char *perm = NULL;
3806 const char *uq_prefix =
3807 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3808 const char *gq_prefix =
3809 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3811 if (strncmp(propname, uq_prefix,
3812 strlen(uq_prefix)) == 0) {
3813 perm = ZFS_DELEG_PERM_USERQUOTA;
3814 } else if (strncmp(propname, gq_prefix,
3815 strlen(gq_prefix)) == 0) {
3816 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3817 } else {
3818 /* USERUSED and GROUPUSED are read-only */
3819 return (SET_ERROR(EINVAL));
3822 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3823 return (err);
3824 return (0);
3827 return (SET_ERROR(EINVAL));
3830 if (issnap)
3831 return (SET_ERROR(EINVAL));
3833 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3835 * dsl_prop_get_all_impl() returns properties in this
3836 * format.
3838 nvlist_t *attrs;
3839 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3840 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3841 &pair) == 0);
3845 * Check that this value is valid for this pool version
3847 switch (prop) {
3848 case ZFS_PROP_COMPRESSION:
3850 * If the user specified gzip compression, make sure
3851 * the SPA supports it. We ignore any errors here since
3852 * we'll catch them later.
3854 if (nvpair_value_uint64(pair, &intval) == 0) {
3855 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3856 intval <= ZIO_COMPRESS_GZIP_9 &&
3857 zfs_earlier_version(dsname,
3858 SPA_VERSION_GZIP_COMPRESSION)) {
3859 return (SET_ERROR(ENOTSUP));
3862 if (intval == ZIO_COMPRESS_ZLE &&
3863 zfs_earlier_version(dsname,
3864 SPA_VERSION_ZLE_COMPRESSION))
3865 return (SET_ERROR(ENOTSUP));
3867 if (intval == ZIO_COMPRESS_LZ4) {
3868 spa_t *spa;
3870 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3871 return (err);
3873 if (!spa_feature_is_enabled(spa,
3874 SPA_FEATURE_LZ4_COMPRESS)) {
3875 spa_close(spa, FTAG);
3876 return (SET_ERROR(ENOTSUP));
3878 spa_close(spa, FTAG);
3882 * If this is a bootable dataset then
3883 * verify that the compression algorithm
3884 * is supported for booting. We must return
3885 * something other than ENOTSUP since it
3886 * implies a downrev pool version.
3888 if (zfs_is_bootfs(dsname) &&
3889 !BOOTFS_COMPRESS_VALID(intval)) {
3890 return (SET_ERROR(ERANGE));
3893 break;
3895 case ZFS_PROP_COPIES:
3896 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3897 return (SET_ERROR(ENOTSUP));
3898 break;
3900 case ZFS_PROP_RECORDSIZE:
3901 /* Record sizes above 128k need the feature to be enabled */
3902 if (nvpair_value_uint64(pair, &intval) == 0 &&
3903 intval > SPA_OLD_MAXBLOCKSIZE) {
3904 spa_t *spa;
3907 * We don't allow setting the property above 1MB,
3908 * unless the tunable has been changed.
3910 if (intval > zfs_max_recordsize ||
3911 intval > SPA_MAXBLOCKSIZE)
3912 return (SET_ERROR(ERANGE));
3914 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3915 return (err);
3917 if (!spa_feature_is_enabled(spa,
3918 SPA_FEATURE_LARGE_BLOCKS)) {
3919 spa_close(spa, FTAG);
3920 return (SET_ERROR(ENOTSUP));
3922 spa_close(spa, FTAG);
3924 break;
3926 case ZFS_PROP_SHARESMB:
3927 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3928 return (SET_ERROR(ENOTSUP));
3929 break;
3931 case ZFS_PROP_ACLINHERIT:
3932 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3933 nvpair_value_uint64(pair, &intval) == 0) {
3934 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3935 zfs_earlier_version(dsname,
3936 SPA_VERSION_PASSTHROUGH_X))
3937 return (SET_ERROR(ENOTSUP));
3939 break;
3941 case ZFS_PROP_CHECKSUM:
3942 case ZFS_PROP_DEDUP:
3944 spa_feature_t feature;
3945 spa_t *spa;
3947 /* dedup feature version checks */
3948 if (prop == ZFS_PROP_DEDUP &&
3949 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3950 return (SET_ERROR(ENOTSUP));
3952 if (nvpair_value_uint64(pair, &intval) != 0)
3953 return (SET_ERROR(EINVAL));
3955 /* check prop value is enabled in features */
3956 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3957 if (feature == SPA_FEATURE_NONE)
3958 break;
3960 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3961 return (err);
3963 if (!spa_feature_is_enabled(spa, feature)) {
3964 spa_close(spa, FTAG);
3965 return (SET_ERROR(ENOTSUP));
3967 spa_close(spa, FTAG);
3968 break;
3972 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3976 * Checks for a race condition to make sure we don't increment a feature flag
3977 * multiple times.
3979 static int
3980 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3982 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3983 spa_feature_t *featurep = arg;
3985 if (!spa_feature_is_active(spa, *featurep))
3986 return (0);
3987 else
3988 return (SET_ERROR(EBUSY));
3992 * The callback invoked on feature activation in the sync task caused by
3993 * zfs_prop_activate_feature.
3995 static void
3996 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
3998 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3999 spa_feature_t *featurep = arg;
4001 spa_feature_incr(spa, *featurep, tx);
4005 * Activates a feature on a pool in response to a property setting. This
4006 * creates a new sync task which modifies the pool to reflect the feature
4007 * as being active.
4009 static int
4010 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4012 int err;
4014 /* EBUSY here indicates that the feature is already active */
4015 err = dsl_sync_task(spa_name(spa),
4016 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4017 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4019 if (err != 0 && err != EBUSY)
4020 return (err);
4021 else
4022 return (0);
4026 * Removes properties from the given props list that fail permission checks
4027 * needed to clear them and to restore them in case of a receive error. For each
4028 * property, make sure we have both set and inherit permissions.
4030 * Returns the first error encountered if any permission checks fail. If the
4031 * caller provides a non-NULL errlist, it also gives the complete list of names
4032 * of all the properties that failed a permission check along with the
4033 * corresponding error numbers. The caller is responsible for freeing the
4034 * returned errlist.
4036 * If every property checks out successfully, zero is returned and the list
4037 * pointed at by errlist is NULL.
4039 static int
4040 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4042 zfs_cmd_t *zc;
4043 nvpair_t *pair, *next_pair;
4044 nvlist_t *errors;
4045 int err, rv = 0;
4047 if (props == NULL)
4048 return (0);
4050 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4052 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4053 (void) strcpy(zc->zc_name, dataset);
4054 pair = nvlist_next_nvpair(props, NULL);
4055 while (pair != NULL) {
4056 next_pair = nvlist_next_nvpair(props, pair);
4058 (void) strcpy(zc->zc_value, nvpair_name(pair));
4059 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4060 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4061 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4062 VERIFY(nvlist_add_int32(errors,
4063 zc->zc_value, err) == 0);
4065 pair = next_pair;
4067 kmem_free(zc, sizeof (zfs_cmd_t));
4069 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4070 nvlist_free(errors);
4071 errors = NULL;
4072 } else {
4073 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4076 if (errlist == NULL)
4077 nvlist_free(errors);
4078 else
4079 *errlist = errors;
4081 return (rv);
4084 static boolean_t
4085 propval_equals(nvpair_t *p1, nvpair_t *p2)
4087 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4088 /* dsl_prop_get_all_impl() format */
4089 nvlist_t *attrs;
4090 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4091 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4092 &p1) == 0);
4095 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4096 nvlist_t *attrs;
4097 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4098 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4099 &p2) == 0);
4102 if (nvpair_type(p1) != nvpair_type(p2))
4103 return (B_FALSE);
4105 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4106 char *valstr1, *valstr2;
4108 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4109 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4110 return (strcmp(valstr1, valstr2) == 0);
4111 } else {
4112 uint64_t intval1, intval2;
4114 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4115 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4116 return (intval1 == intval2);
4121 * Remove properties from props if they are not going to change (as determined
4122 * by comparison with origprops). Remove them from origprops as well, since we
4123 * do not need to clear or restore properties that won't change.
4125 static void
4126 props_reduce(nvlist_t *props, nvlist_t *origprops)
4128 nvpair_t *pair, *next_pair;
4130 if (origprops == NULL)
4131 return; /* all props need to be received */
4133 pair = nvlist_next_nvpair(props, NULL);
4134 while (pair != NULL) {
4135 const char *propname = nvpair_name(pair);
4136 nvpair_t *match;
4138 next_pair = nvlist_next_nvpair(props, pair);
4140 if ((nvlist_lookup_nvpair(origprops, propname,
4141 &match) != 0) || !propval_equals(pair, match))
4142 goto next; /* need to set received value */
4144 /* don't clear the existing received value */
4145 (void) nvlist_remove_nvpair(origprops, match);
4146 /* don't bother receiving the property */
4147 (void) nvlist_remove_nvpair(props, pair);
4148 next:
4149 pair = next_pair;
4154 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4155 * For example, refquota cannot be set until after the receipt of a dataset,
4156 * because in replication streams, an older/earlier snapshot may exceed the
4157 * refquota. We want to receive the older/earlier snapshot, but setting
4158 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4159 * the older/earlier snapshot from being received (with EDQUOT).
4161 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4163 * libzfs will need to be judicious handling errors encountered by props
4164 * extracted by this function.
4166 static nvlist_t *
4167 extract_delay_props(nvlist_t *props)
4169 nvlist_t *delayprops;
4170 nvpair_t *nvp, *tmp;
4171 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4172 int i;
4174 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4176 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4177 nvp = nvlist_next_nvpair(props, nvp)) {
4179 * strcmp() is safe because zfs_prop_to_name() always returns
4180 * a bounded string.
4182 for (i = 0; delayable[i] != 0; i++) {
4183 if (strcmp(zfs_prop_to_name(delayable[i]),
4184 nvpair_name(nvp)) == 0) {
4185 break;
4188 if (delayable[i] != 0) {
4189 tmp = nvlist_prev_nvpair(props, nvp);
4190 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4191 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4192 nvp = tmp;
4196 if (nvlist_empty(delayprops)) {
4197 nvlist_free(delayprops);
4198 delayprops = NULL;
4200 return (delayprops);
4203 #ifdef DEBUG
4204 static boolean_t zfs_ioc_recv_inject_err;
4205 #endif
4208 * inputs:
4209 * zc_name name of containing filesystem
4210 * zc_nvlist_src{_size} nvlist of properties to apply
4211 * zc_value name of snapshot to create
4212 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4213 * zc_cookie file descriptor to recv from
4214 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4215 * zc_guid force flag
4216 * zc_cleanup_fd cleanup-on-exit file descriptor
4217 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4218 * zc_resumable if data is incomplete assume sender will resume
4220 * outputs:
4221 * zc_cookie number of bytes read
4222 * zc_nvlist_dst{_size} error for each unapplied received property
4223 * zc_obj zprop_errflags_t
4224 * zc_action_handle handle for this guid/ds mapping
4226 static int
4227 zfs_ioc_recv(zfs_cmd_t *zc)
4229 file_t *fp;
4230 dmu_recv_cookie_t drc;
4231 boolean_t force = (boolean_t)zc->zc_guid;
4232 int fd;
4233 int error = 0;
4234 int props_error = 0;
4235 nvlist_t *errors;
4236 offset_t off;
4237 nvlist_t *props = NULL; /* sent properties */
4238 nvlist_t *origprops = NULL; /* existing properties */
4239 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4240 char *origin = NULL;
4241 char *tosnap;
4242 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4243 boolean_t first_recvd_props = B_FALSE;
4245 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4246 strchr(zc->zc_value, '@') == NULL ||
4247 strchr(zc->zc_value, '%'))
4248 return (SET_ERROR(EINVAL));
4250 (void) strcpy(tofs, zc->zc_value);
4251 tosnap = strchr(tofs, '@');
4252 *tosnap++ = '\0';
4254 if (zc->zc_nvlist_src != (uintptr_t)NULL &&
4255 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4256 zc->zc_iflags, &props)) != 0)
4257 return (error);
4259 fd = zc->zc_cookie;
4260 fp = getf(fd);
4261 if (fp == NULL) {
4262 nvlist_free(props);
4263 return (SET_ERROR(EBADF));
4266 errors = fnvlist_alloc();
4268 if (zc->zc_string[0])
4269 origin = zc->zc_string;
4271 error = dmu_recv_begin(tofs, tosnap,
4272 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4273 if (error != 0)
4274 goto out;
4277 * Set properties before we receive the stream so that they are applied
4278 * to the new data. Note that we must call dmu_recv_stream() if
4279 * dmu_recv_begin() succeeds.
4281 if (props != NULL && !drc.drc_newfs) {
4282 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4283 SPA_VERSION_RECVD_PROPS &&
4284 !dsl_prop_get_hasrecvd(tofs))
4285 first_recvd_props = B_TRUE;
4288 * If new received properties are supplied, they are to
4289 * completely replace the existing received properties, so stash
4290 * away the existing ones.
4292 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4293 nvlist_t *errlist = NULL;
4295 * Don't bother writing a property if its value won't
4296 * change (and avoid the unnecessary security checks).
4298 * The first receive after SPA_VERSION_RECVD_PROPS is a
4299 * special case where we blow away all local properties
4300 * regardless.
4302 if (!first_recvd_props)
4303 props_reduce(props, origprops);
4304 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4305 (void) nvlist_merge(errors, errlist, 0);
4306 nvlist_free(errlist);
4308 if (clear_received_props(tofs, origprops,
4309 first_recvd_props ? NULL : props) != 0)
4310 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4311 } else {
4312 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4316 if (props != NULL) {
4317 props_error = dsl_prop_set_hasrecvd(tofs);
4319 if (props_error == 0) {
4320 delayprops = extract_delay_props(props);
4321 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4322 props, errors);
4326 off = fp->f_offset;
4327 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4328 &zc->zc_action_handle);
4330 if (error == 0) {
4331 zfsvfs_t *zfsvfs = NULL;
4333 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4334 /* online recv */
4335 dsl_dataset_t *ds;
4336 int end_err;
4338 ds = dmu_objset_ds(zfsvfs->z_os);
4339 error = zfs_suspend_fs(zfsvfs);
4341 * If the suspend fails, then the recv_end will
4342 * likely also fail, and clean up after itself.
4344 end_err = dmu_recv_end(&drc, zfsvfs);
4345 if (error == 0)
4346 error = zfs_resume_fs(zfsvfs, ds);
4347 error = error ? error : end_err;
4348 VFS_RELE(zfsvfs->z_vfs);
4349 } else {
4350 error = dmu_recv_end(&drc, NULL);
4353 /* Set delayed properties now, after we're done receiving. */
4354 if (delayprops != NULL && error == 0) {
4355 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4356 delayprops, errors);
4360 if (delayprops != NULL) {
4362 * Merge delayed props back in with initial props, in case
4363 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4364 * we have to make sure clear_received_props() includes
4365 * the delayed properties).
4367 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4368 * using ASSERT() will be just like a VERIFY.
4370 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4371 nvlist_free(delayprops);
4375 * Now that all props, initial and delayed, are set, report the prop
4376 * errors to the caller.
4378 if (zc->zc_nvlist_dst_size != 0 &&
4379 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4380 put_nvlist(zc, errors) != 0)) {
4382 * Caller made zc->zc_nvlist_dst less than the minimum expected
4383 * size or supplied an invalid address.
4385 props_error = SET_ERROR(EINVAL);
4388 zc->zc_cookie = off - fp->f_offset;
4389 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4390 fp->f_offset = off;
4392 #ifdef DEBUG
4393 if (zfs_ioc_recv_inject_err) {
4394 zfs_ioc_recv_inject_err = B_FALSE;
4395 error = 1;
4397 #endif
4399 * On error, restore the original props.
4401 if (error != 0 && props != NULL && !drc.drc_newfs) {
4402 if (clear_received_props(tofs, props, NULL) != 0) {
4404 * We failed to clear the received properties.
4405 * Since we may have left a $recvd value on the
4406 * system, we can't clear the $hasrecvd flag.
4408 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4409 } else if (first_recvd_props) {
4410 dsl_prop_unset_hasrecvd(tofs);
4413 if (origprops == NULL && !drc.drc_newfs) {
4414 /* We failed to stash the original properties. */
4415 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4419 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4420 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4421 * explictly if we're restoring local properties cleared in the
4422 * first new-style receive.
4424 if (origprops != NULL &&
4425 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4426 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4427 origprops, NULL) != 0) {
4429 * We stashed the original properties but failed to
4430 * restore them.
4432 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4435 out:
4436 nvlist_free(props);
4437 nvlist_free(origprops);
4438 nvlist_free(errors);
4439 releasef(fd);
4441 if (error == 0)
4442 error = props_error;
4444 return (error);
4448 * inputs:
4449 * zc_name name of snapshot to send
4450 * zc_cookie file descriptor to send stream to
4451 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4452 * zc_sendobj objsetid of snapshot to send
4453 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4454 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4455 * output size in zc_objset_type.
4456 * zc_flags lzc_send_flags
4458 * outputs:
4459 * zc_objset_type estimated size, if zc_guid is set
4461 static int
4462 zfs_ioc_send(zfs_cmd_t *zc)
4464 int error;
4465 offset_t off;
4466 boolean_t estimate = (zc->zc_guid != 0);
4467 boolean_t embedok = (zc->zc_flags & 0x1);
4468 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4469 boolean_t compressok = (zc->zc_flags & 0x4);
4471 if (zc->zc_obj != 0) {
4472 dsl_pool_t *dp;
4473 dsl_dataset_t *tosnap;
4475 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4476 if (error != 0)
4477 return (error);
4479 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4480 if (error != 0) {
4481 dsl_pool_rele(dp, FTAG);
4482 return (error);
4485 if (dsl_dir_is_clone(tosnap->ds_dir))
4486 zc->zc_fromobj =
4487 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4488 dsl_dataset_rele(tosnap, FTAG);
4489 dsl_pool_rele(dp, FTAG);
4492 if (estimate) {
4493 dsl_pool_t *dp;
4494 dsl_dataset_t *tosnap;
4495 dsl_dataset_t *fromsnap = NULL;
4497 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4498 if (error != 0)
4499 return (error);
4501 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4502 if (error != 0) {
4503 dsl_pool_rele(dp, FTAG);
4504 return (error);
4507 if (zc->zc_fromobj != 0) {
4508 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4509 FTAG, &fromsnap);
4510 if (error != 0) {
4511 dsl_dataset_rele(tosnap, FTAG);
4512 dsl_pool_rele(dp, FTAG);
4513 return (error);
4517 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4518 &zc->zc_objset_type);
4520 if (fromsnap != NULL)
4521 dsl_dataset_rele(fromsnap, FTAG);
4522 dsl_dataset_rele(tosnap, FTAG);
4523 dsl_pool_rele(dp, FTAG);
4524 } else {
4525 file_t *fp = getf(zc->zc_cookie);
4526 if (fp == NULL)
4527 return (SET_ERROR(EBADF));
4529 off = fp->f_offset;
4530 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4531 zc->zc_fromobj, embedok, large_block_ok, compressok,
4532 zc->zc_cookie, fp->f_vnode, &off);
4534 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4535 fp->f_offset = off;
4536 releasef(zc->zc_cookie);
4538 return (error);
4542 * inputs:
4543 * zc_name name of snapshot on which to report progress
4544 * zc_cookie file descriptor of send stream
4546 * outputs:
4547 * zc_cookie number of bytes written in send stream thus far
4549 static int
4550 zfs_ioc_send_progress(zfs_cmd_t *zc)
4552 dsl_pool_t *dp;
4553 dsl_dataset_t *ds;
4554 dmu_sendarg_t *dsp = NULL;
4555 int error;
4557 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4558 if (error != 0)
4559 return (error);
4561 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4562 if (error != 0) {
4563 dsl_pool_rele(dp, FTAG);
4564 return (error);
4567 mutex_enter(&ds->ds_sendstream_lock);
4570 * Iterate over all the send streams currently active on this dataset.
4571 * If there's one which matches the specified file descriptor _and_ the
4572 * stream was started by the current process, return the progress of
4573 * that stream.
4575 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4576 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4577 if (dsp->dsa_outfd == zc->zc_cookie &&
4578 dsp->dsa_proc == curproc)
4579 break;
4582 if (dsp != NULL)
4583 zc->zc_cookie = *(dsp->dsa_off);
4584 else
4585 error = SET_ERROR(ENOENT);
4587 mutex_exit(&ds->ds_sendstream_lock);
4588 dsl_dataset_rele(ds, FTAG);
4589 dsl_pool_rele(dp, FTAG);
4590 return (error);
4593 static int
4594 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4596 int id, error;
4598 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4599 &zc->zc_inject_record);
4601 if (error == 0)
4602 zc->zc_guid = (uint64_t)id;
4604 return (error);
4607 static int
4608 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4610 return (zio_clear_fault((int)zc->zc_guid));
4613 static int
4614 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4616 int id = (int)zc->zc_guid;
4617 int error;
4619 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4620 &zc->zc_inject_record);
4622 zc->zc_guid = id;
4624 return (error);
4627 static int
4628 zfs_ioc_error_log(zfs_cmd_t *zc)
4630 spa_t *spa;
4631 int error;
4632 size_t count = (size_t)zc->zc_nvlist_dst_size;
4634 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4635 return (error);
4637 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4638 &count);
4639 if (error == 0)
4640 zc->zc_nvlist_dst_size = count;
4641 else
4642 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4644 spa_close(spa, FTAG);
4646 return (error);
4649 static int
4650 zfs_ioc_clear(zfs_cmd_t *zc)
4652 spa_t *spa;
4653 vdev_t *vd;
4654 int error;
4657 * On zpool clear we also fix up missing slogs
4659 mutex_enter(&spa_namespace_lock);
4660 spa = spa_lookup(zc->zc_name);
4661 if (spa == NULL) {
4662 mutex_exit(&spa_namespace_lock);
4663 return (SET_ERROR(EIO));
4665 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4666 /* we need to let spa_open/spa_load clear the chains */
4667 spa_set_log_state(spa, SPA_LOG_CLEAR);
4669 spa->spa_last_open_failed = 0;
4670 mutex_exit(&spa_namespace_lock);
4672 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4673 error = spa_open(zc->zc_name, &spa, FTAG);
4674 } else {
4675 nvlist_t *policy;
4676 nvlist_t *config = NULL;
4678 if (zc->zc_nvlist_src == (uintptr_t)NULL)
4679 return (SET_ERROR(EINVAL));
4681 if ((error = get_nvlist(zc->zc_nvlist_src,
4682 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4683 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4684 policy, &config);
4685 if (config != NULL) {
4686 int err;
4688 if ((err = put_nvlist(zc, config)) != 0)
4689 error = err;
4690 nvlist_free(config);
4692 nvlist_free(policy);
4696 if (error != 0)
4697 return (error);
4699 spa_vdev_state_enter(spa, SCL_NONE);
4701 if (zc->zc_guid == 0) {
4702 vd = NULL;
4703 } else {
4704 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4705 if (vd == NULL) {
4706 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4707 spa_close(spa, FTAG);
4708 return (SET_ERROR(ENODEV));
4712 vdev_clear(spa, vd);
4714 (void) spa_vdev_state_exit(spa, NULL, 0);
4717 * Resume any suspended I/Os.
4719 if (zio_resume(spa) != 0)
4720 error = SET_ERROR(EIO);
4722 spa_close(spa, FTAG);
4724 return (error);
4727 static int
4728 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4730 spa_t *spa;
4731 int error;
4733 error = spa_open(zc->zc_name, &spa, FTAG);
4734 if (error != 0)
4735 return (error);
4737 spa_vdev_state_enter(spa, SCL_NONE);
4740 * If a resilver is already in progress then set the
4741 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4742 * the scan as a side effect of the reopen. Otherwise, let
4743 * vdev_open() decided if a resilver is required.
4745 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4746 vdev_reopen(spa->spa_root_vdev);
4747 spa->spa_scrub_reopen = B_FALSE;
4749 (void) spa_vdev_state_exit(spa, NULL, 0);
4750 spa_close(spa, FTAG);
4751 return (0);
4754 * inputs:
4755 * zc_name name of filesystem
4757 * outputs:
4758 * zc_string name of conflicting snapshot, if there is one
4760 static int
4761 zfs_ioc_promote(zfs_cmd_t *zc)
4763 dsl_pool_t *dp;
4764 dsl_dataset_t *ds, *ods;
4765 char origin[ZFS_MAX_DATASET_NAME_LEN];
4766 char *cp;
4767 int error;
4769 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4770 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4771 strchr(zc->zc_name, '%'))
4772 return (SET_ERROR(EINVAL));
4774 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4775 if (error != 0)
4776 return (error);
4778 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4779 if (error != 0) {
4780 dsl_pool_rele(dp, FTAG);
4781 return (error);
4784 if (!dsl_dir_is_clone(ds->ds_dir)) {
4785 dsl_dataset_rele(ds, FTAG);
4786 dsl_pool_rele(dp, FTAG);
4787 return (SET_ERROR(EINVAL));
4790 error = dsl_dataset_hold_obj(dp,
4791 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4792 if (error != 0) {
4793 dsl_dataset_rele(ds, FTAG);
4794 dsl_pool_rele(dp, FTAG);
4795 return (error);
4798 dsl_dataset_name(ods, origin);
4799 dsl_dataset_rele(ods, FTAG);
4800 dsl_dataset_rele(ds, FTAG);
4801 dsl_pool_rele(dp, FTAG);
4804 * We don't need to unmount *all* the origin fs's snapshots, but
4805 * it's easier.
4807 cp = strchr(origin, '@');
4808 if (cp)
4809 *cp = '\0';
4810 (void) dmu_objset_find(origin,
4811 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4812 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4816 * Retrieve a single {user|group}{used|quota}@... property.
4818 * inputs:
4819 * zc_name name of filesystem
4820 * zc_objset_type zfs_userquota_prop_t
4821 * zc_value domain name (eg. "S-1-234-567-89")
4822 * zc_guid RID/UID/GID
4824 * outputs:
4825 * zc_cookie property value
4827 static int
4828 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4830 zfsvfs_t *zfsvfs;
4831 int error;
4833 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4834 return (SET_ERROR(EINVAL));
4836 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4837 if (error != 0)
4838 return (error);
4840 error = zfs_userspace_one(zfsvfs,
4841 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4842 zfsvfs_rele(zfsvfs, FTAG);
4844 return (error);
4848 * inputs:
4849 * zc_name name of filesystem
4850 * zc_cookie zap cursor
4851 * zc_objset_type zfs_userquota_prop_t
4852 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4854 * outputs:
4855 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4856 * zc_cookie zap cursor
4858 static int
4859 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4861 zfsvfs_t *zfsvfs;
4862 int bufsize = zc->zc_nvlist_dst_size;
4864 if (bufsize <= 0)
4865 return (SET_ERROR(ENOMEM));
4867 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4868 if (error != 0)
4869 return (error);
4871 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4873 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4874 buf, &zc->zc_nvlist_dst_size);
4876 if (error == 0) {
4877 error = xcopyout(buf,
4878 (void *)(uintptr_t)zc->zc_nvlist_dst,
4879 zc->zc_nvlist_dst_size);
4881 kmem_free(buf, bufsize);
4882 zfsvfs_rele(zfsvfs, FTAG);
4884 return (error);
4888 * inputs:
4889 * zc_name name of filesystem
4891 * outputs:
4892 * none
4894 static int
4895 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4897 objset_t *os;
4898 int error = 0;
4899 zfsvfs_t *zfsvfs;
4901 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4902 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4904 * If userused is not enabled, it may be because the
4905 * objset needs to be closed & reopened (to grow the
4906 * objset_phys_t). Suspend/resume the fs will do that.
4908 dsl_dataset_t *ds, *newds;
4910 ds = dmu_objset_ds(zfsvfs->z_os);
4911 error = zfs_suspend_fs(zfsvfs);
4912 if (error == 0) {
4913 dmu_objset_refresh_ownership(ds, &newds,
4914 zfsvfs);
4915 error = zfs_resume_fs(zfsvfs, newds);
4918 if (error == 0)
4919 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4920 VFS_RELE(zfsvfs->z_vfs);
4921 } else {
4922 /* XXX kind of reading contents without owning */
4923 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4924 if (error != 0)
4925 return (error);
4927 error = dmu_objset_userspace_upgrade(os);
4928 dmu_objset_rele(os, FTAG);
4931 return (error);
4935 * We don't want to have a hard dependency
4936 * against some special symbols in sharefs
4937 * nfs, and smbsrv. Determine them if needed when
4938 * the first file system is shared.
4939 * Neither sharefs, nfs or smbsrv are unloadable modules.
4941 int (*znfsexport_fs)(void *arg);
4942 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4943 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4945 int zfs_nfsshare_inited;
4946 int zfs_smbshare_inited;
4948 ddi_modhandle_t nfs_mod;
4949 ddi_modhandle_t sharefs_mod;
4950 ddi_modhandle_t smbsrv_mod;
4951 kmutex_t zfs_share_lock;
4953 static int
4954 zfs_init_sharefs()
4956 int error;
4958 ASSERT(MUTEX_HELD(&zfs_share_lock));
4959 /* Both NFS and SMB shares also require sharetab support. */
4960 if (sharefs_mod == NULL && ((sharefs_mod =
4961 ddi_modopen("fs/sharefs",
4962 KRTLD_MODE_FIRST, &error)) == NULL)) {
4963 return (SET_ERROR(ENOSYS));
4965 if (zshare_fs == NULL && ((zshare_fs =
4966 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4967 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4968 return (SET_ERROR(ENOSYS));
4970 return (0);
4973 static int
4974 zfs_ioc_share(zfs_cmd_t *zc)
4976 int error;
4977 int opcode;
4979 switch (zc->zc_share.z_sharetype) {
4980 case ZFS_SHARE_NFS:
4981 case ZFS_UNSHARE_NFS:
4982 if (zfs_nfsshare_inited == 0) {
4983 mutex_enter(&zfs_share_lock);
4984 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4985 KRTLD_MODE_FIRST, &error)) == NULL)) {
4986 mutex_exit(&zfs_share_lock);
4987 return (SET_ERROR(ENOSYS));
4989 if (znfsexport_fs == NULL &&
4990 ((znfsexport_fs = (int (*)(void *))
4991 ddi_modsym(nfs_mod,
4992 "nfs_export", &error)) == NULL)) {
4993 mutex_exit(&zfs_share_lock);
4994 return (SET_ERROR(ENOSYS));
4996 error = zfs_init_sharefs();
4997 if (error != 0) {
4998 mutex_exit(&zfs_share_lock);
4999 return (SET_ERROR(ENOSYS));
5001 zfs_nfsshare_inited = 1;
5002 mutex_exit(&zfs_share_lock);
5004 break;
5005 case ZFS_SHARE_SMB:
5006 case ZFS_UNSHARE_SMB:
5007 if (zfs_smbshare_inited == 0) {
5008 mutex_enter(&zfs_share_lock);
5009 if (smbsrv_mod == NULL && ((smbsrv_mod =
5010 ddi_modopen("drv/smbsrv",
5011 KRTLD_MODE_FIRST, &error)) == NULL)) {
5012 mutex_exit(&zfs_share_lock);
5013 return (SET_ERROR(ENOSYS));
5015 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5016 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5017 "smb_server_share", &error)) == NULL)) {
5018 mutex_exit(&zfs_share_lock);
5019 return (SET_ERROR(ENOSYS));
5021 error = zfs_init_sharefs();
5022 if (error != 0) {
5023 mutex_exit(&zfs_share_lock);
5024 return (SET_ERROR(ENOSYS));
5026 zfs_smbshare_inited = 1;
5027 mutex_exit(&zfs_share_lock);
5029 break;
5030 default:
5031 return (SET_ERROR(EINVAL));
5034 switch (zc->zc_share.z_sharetype) {
5035 case ZFS_SHARE_NFS:
5036 case ZFS_UNSHARE_NFS:
5037 if (error =
5038 znfsexport_fs((void *)
5039 (uintptr_t)zc->zc_share.z_exportdata))
5040 return (error);
5041 break;
5042 case ZFS_SHARE_SMB:
5043 case ZFS_UNSHARE_SMB:
5044 if (error = zsmbexport_fs((void *)
5045 (uintptr_t)zc->zc_share.z_exportdata,
5046 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5047 B_TRUE: B_FALSE)) {
5048 return (error);
5050 break;
5053 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5054 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5055 SHAREFS_ADD : SHAREFS_REMOVE;
5058 * Add or remove share from sharetab
5060 error = zshare_fs(opcode,
5061 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5062 zc->zc_share.z_sharemax);
5064 return (error);
5068 ace_t full_access[] = {
5069 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5073 * inputs:
5074 * zc_name name of containing filesystem
5075 * zc_obj object # beyond which we want next in-use object #
5077 * outputs:
5078 * zc_obj next in-use object #
5080 static int
5081 zfs_ioc_next_obj(zfs_cmd_t *zc)
5083 objset_t *os = NULL;
5084 int error;
5086 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5087 if (error != 0)
5088 return (error);
5090 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5091 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5093 dmu_objset_rele(os, FTAG);
5094 return (error);
5098 * inputs:
5099 * zc_name name of filesystem
5100 * zc_value prefix name for snapshot
5101 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5103 * outputs:
5104 * zc_value short name of new snapshot
5106 static int
5107 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5109 char *snap_name;
5110 char *hold_name;
5111 int error;
5112 minor_t minor;
5114 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5115 if (error != 0)
5116 return (error);
5118 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5119 (u_longlong_t)ddi_get_lbolt64());
5120 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5122 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5123 hold_name);
5124 if (error == 0)
5125 (void) strcpy(zc->zc_value, snap_name);
5126 strfree(snap_name);
5127 strfree(hold_name);
5128 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5129 return (error);
5133 * inputs:
5134 * zc_name name of "to" snapshot
5135 * zc_value name of "from" snapshot
5136 * zc_cookie file descriptor to write diff data on
5138 * outputs:
5139 * dmu_diff_record_t's to the file descriptor
5141 static int
5142 zfs_ioc_diff(zfs_cmd_t *zc)
5144 file_t *fp;
5145 offset_t off;
5146 int error;
5148 fp = getf(zc->zc_cookie);
5149 if (fp == NULL)
5150 return (SET_ERROR(EBADF));
5152 off = fp->f_offset;
5154 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5156 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5157 fp->f_offset = off;
5158 releasef(zc->zc_cookie);
5160 return (error);
5164 * Remove all ACL files in shares dir
5166 static int
5167 zfs_smb_acl_purge(znode_t *dzp)
5169 zap_cursor_t zc;
5170 zap_attribute_t zap;
5171 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5172 int error;
5174 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5175 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5176 zap_cursor_advance(&zc)) {
5177 if ((error = fop_remove(ZTOV(dzp), zap.za_name, kcred,
5178 NULL, 0)) != 0)
5179 break;
5181 zap_cursor_fini(&zc);
5182 return (error);
5185 static int
5186 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5188 vnode_t *vp;
5189 znode_t *dzp;
5190 vnode_t *resourcevp = NULL;
5191 znode_t *sharedir;
5192 zfsvfs_t *zfsvfs;
5193 nvlist_t *nvlist;
5194 char *src, *target;
5195 vattr_t vattr;
5196 vsecattr_t vsec;
5197 int error = 0;
5199 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5200 NO_FOLLOW, NULL, &vp)) != 0)
5201 return (error);
5203 /* Now make sure mntpnt and dataset are ZFS */
5205 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5206 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5207 zc->zc_name) != 0)) {
5208 VN_RELE(vp);
5209 return (SET_ERROR(EINVAL));
5212 dzp = VTOZ(vp);
5213 zfsvfs = dzp->z_zfsvfs;
5214 ZFS_ENTER(zfsvfs);
5217 * Create share dir if its missing.
5219 mutex_enter(&zfsvfs->z_lock);
5220 if (zfsvfs->z_shares_dir == 0) {
5221 dmu_tx_t *tx;
5223 tx = dmu_tx_create(zfsvfs->z_os);
5224 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5225 ZFS_SHARES_DIR);
5226 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5227 error = dmu_tx_assign(tx, TXG_WAIT);
5228 if (error != 0) {
5229 dmu_tx_abort(tx);
5230 } else {
5231 error = zfs_create_share_dir(zfsvfs, tx);
5232 dmu_tx_commit(tx);
5234 if (error != 0) {
5235 mutex_exit(&zfsvfs->z_lock);
5236 VN_RELE(vp);
5237 ZFS_EXIT(zfsvfs);
5238 return (error);
5241 mutex_exit(&zfsvfs->z_lock);
5243 ASSERT(zfsvfs->z_shares_dir);
5244 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5245 VN_RELE(vp);
5246 ZFS_EXIT(zfsvfs);
5247 return (error);
5250 switch (zc->zc_cookie) {
5251 case ZFS_SMB_ACL_ADD:
5252 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5253 vattr.va_type = VREG;
5254 vattr.va_mode = S_IFREG|0777;
5255 vattr.va_uid = 0;
5256 vattr.va_gid = 0;
5258 vsec.vsa_mask = VSA_ACE;
5259 vsec.vsa_aclentp = &full_access;
5260 vsec.vsa_aclentsz = sizeof (full_access);
5261 vsec.vsa_aclcnt = 1;
5263 error = fop_create(ZTOV(sharedir), zc->zc_string,
5264 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5265 if (resourcevp)
5266 VN_RELE(resourcevp);
5267 break;
5269 case ZFS_SMB_ACL_REMOVE:
5270 error = fop_remove(ZTOV(sharedir), zc->zc_string, kcred,
5271 NULL, 0);
5272 break;
5274 case ZFS_SMB_ACL_RENAME:
5275 if ((error = get_nvlist(zc->zc_nvlist_src,
5276 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5277 VN_RELE(vp);
5278 VN_RELE(ZTOV(sharedir));
5279 ZFS_EXIT(zfsvfs);
5280 return (error);
5282 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5283 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5284 &target)) {
5285 VN_RELE(vp);
5286 VN_RELE(ZTOV(sharedir));
5287 ZFS_EXIT(zfsvfs);
5288 nvlist_free(nvlist);
5289 return (error);
5291 error = fop_rename(ZTOV(sharedir), src, ZTOV(sharedir), target,
5292 kcred, NULL, 0);
5293 nvlist_free(nvlist);
5294 break;
5296 case ZFS_SMB_ACL_PURGE:
5297 error = zfs_smb_acl_purge(sharedir);
5298 break;
5300 default:
5301 error = SET_ERROR(EINVAL);
5302 break;
5305 VN_RELE(vp);
5306 VN_RELE(ZTOV(sharedir));
5308 ZFS_EXIT(zfsvfs);
5310 return (error);
5314 * innvl: {
5315 * "holds" -> { snapname -> holdname (string), ... }
5316 * (optional) "cleanup_fd" -> fd (int32)
5319 * outnvl: {
5320 * snapname -> error value (int32)
5321 * ...
5324 /* ARGSUSED */
5325 static int
5326 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5328 nvpair_t *pair;
5329 nvlist_t *holds;
5330 int cleanup_fd = -1;
5331 int error;
5332 minor_t minor = 0;
5334 error = nvlist_lookup_nvlist(args, "holds", &holds);
5335 if (error != 0)
5336 return (SET_ERROR(EINVAL));
5338 /* make sure the user didn't pass us any invalid (empty) tags */
5339 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5340 pair = nvlist_next_nvpair(holds, pair)) {
5341 char *htag;
5343 error = nvpair_value_string(pair, &htag);
5344 if (error != 0)
5345 return (SET_ERROR(error));
5347 if (strlen(htag) == 0)
5348 return (SET_ERROR(EINVAL));
5351 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5352 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5353 if (error != 0)
5354 return (error);
5357 error = dsl_dataset_user_hold(holds, minor, errlist);
5358 if (minor != 0)
5359 zfs_onexit_fd_rele(cleanup_fd);
5360 return (error);
5364 * innvl is not used.
5366 * outnvl: {
5367 * holdname -> time added (uint64 seconds since epoch)
5368 * ...
5371 /* ARGSUSED */
5372 static int
5373 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5375 return (dsl_dataset_get_holds(snapname, outnvl));
5379 * innvl: {
5380 * snapname -> { holdname, ... }
5381 * ...
5384 * outnvl: {
5385 * snapname -> error value (int32)
5386 * ...
5389 /* ARGSUSED */
5390 static int
5391 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5393 return (dsl_dataset_user_release(holds, errlist));
5397 * inputs:
5398 * zc_name name of new filesystem or snapshot
5399 * zc_value full name of old snapshot
5401 * outputs:
5402 * zc_cookie space in bytes
5403 * zc_objset_type compressed space in bytes
5404 * zc_perm_action uncompressed space in bytes
5406 static int
5407 zfs_ioc_space_written(zfs_cmd_t *zc)
5409 int error;
5410 dsl_pool_t *dp;
5411 dsl_dataset_t *new, *old;
5413 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5414 if (error != 0)
5415 return (error);
5416 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5417 if (error != 0) {
5418 dsl_pool_rele(dp, FTAG);
5419 return (error);
5421 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5422 if (error != 0) {
5423 dsl_dataset_rele(new, FTAG);
5424 dsl_pool_rele(dp, FTAG);
5425 return (error);
5428 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5429 &zc->zc_objset_type, &zc->zc_perm_action);
5430 dsl_dataset_rele(old, FTAG);
5431 dsl_dataset_rele(new, FTAG);
5432 dsl_pool_rele(dp, FTAG);
5433 return (error);
5437 * innvl: {
5438 * "firstsnap" -> snapshot name
5441 * outnvl: {
5442 * "used" -> space in bytes
5443 * "compressed" -> compressed space in bytes
5444 * "uncompressed" -> uncompressed space in bytes
5447 static int
5448 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5450 int error;
5451 dsl_pool_t *dp;
5452 dsl_dataset_t *new, *old;
5453 char *firstsnap;
5454 uint64_t used, comp, uncomp;
5456 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5457 return (SET_ERROR(EINVAL));
5459 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5460 if (error != 0)
5461 return (error);
5463 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5464 if (error == 0 && !new->ds_is_snapshot) {
5465 dsl_dataset_rele(new, FTAG);
5466 error = SET_ERROR(EINVAL);
5468 if (error != 0) {
5469 dsl_pool_rele(dp, FTAG);
5470 return (error);
5472 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5473 if (error == 0 && !old->ds_is_snapshot) {
5474 dsl_dataset_rele(old, FTAG);
5475 error = SET_ERROR(EINVAL);
5477 if (error != 0) {
5478 dsl_dataset_rele(new, FTAG);
5479 dsl_pool_rele(dp, FTAG);
5480 return (error);
5483 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5484 dsl_dataset_rele(old, FTAG);
5485 dsl_dataset_rele(new, FTAG);
5486 dsl_pool_rele(dp, FTAG);
5487 fnvlist_add_uint64(outnvl, "used", used);
5488 fnvlist_add_uint64(outnvl, "compressed", comp);
5489 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5490 return (error);
5494 * innvl: {
5495 * "fd" -> file descriptor to write stream to (int32)
5496 * (optional) "fromsnap" -> full snap name to send an incremental from
5497 * (optional) "largeblockok" -> (value ignored)
5498 * indicates that blocks > 128KB are permitted
5499 * (optional) "embedok" -> (value ignored)
5500 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5501 * (optional) "compressok" -> (value ignored)
5502 * presence indicates compressed DRR_WRITE records are permitted
5503 * (optional) "resume_object" and "resume_offset" -> (uint64)
5504 * if present, resume send stream from specified object and offset.
5507 * outnvl is unused
5509 /* ARGSUSED */
5510 static int
5511 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5513 int error;
5514 offset_t off;
5515 char *fromname = NULL;
5516 int fd;
5517 boolean_t largeblockok;
5518 boolean_t embedok;
5519 boolean_t compressok;
5520 uint64_t resumeobj = 0;
5521 uint64_t resumeoff = 0;
5523 error = nvlist_lookup_int32(innvl, "fd", &fd);
5524 if (error != 0)
5525 return (SET_ERROR(EINVAL));
5527 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5529 largeblockok = nvlist_exists(innvl, "largeblockok");
5530 embedok = nvlist_exists(innvl, "embedok");
5531 compressok = nvlist_exists(innvl, "compressok");
5533 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5534 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5536 file_t *fp = getf(fd);
5537 if (fp == NULL)
5538 return (SET_ERROR(EBADF));
5540 off = fp->f_offset;
5541 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5542 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5544 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5545 fp->f_offset = off;
5546 releasef(fd);
5547 return (error);
5551 * Determine approximately how large a zfs send stream will be -- the number
5552 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5554 * innvl: {
5555 * (optional) "from" -> full snap or bookmark name to send an incremental
5556 * from
5557 * (optional) "largeblockok" -> (value ignored)
5558 * indicates that blocks > 128KB are permitted
5559 * (optional) "embedok" -> (value ignored)
5560 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5561 * (optional) "compressok" -> (value ignored)
5562 * presence indicates compressed DRR_WRITE records are permitted
5565 * outnvl: {
5566 * "space" -> bytes of space (uint64)
5569 static int
5570 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5572 dsl_pool_t *dp;
5573 dsl_dataset_t *tosnap;
5574 int error;
5575 char *fromname;
5576 boolean_t compressok;
5577 uint64_t space;
5579 error = dsl_pool_hold(snapname, FTAG, &dp);
5580 if (error != 0)
5581 return (error);
5583 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5584 if (error != 0) {
5585 dsl_pool_rele(dp, FTAG);
5586 return (error);
5589 compressok = nvlist_exists(innvl, "compressok");
5591 error = nvlist_lookup_string(innvl, "from", &fromname);
5592 if (error == 0) {
5593 if (strchr(fromname, '@') != NULL) {
5595 * If from is a snapshot, hold it and use the more
5596 * efficient dmu_send_estimate to estimate send space
5597 * size using deadlists.
5599 dsl_dataset_t *fromsnap;
5600 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5601 if (error != 0)
5602 goto out;
5603 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5604 &space);
5605 dsl_dataset_rele(fromsnap, FTAG);
5606 } else if (strchr(fromname, '#') != NULL) {
5608 * If from is a bookmark, fetch the creation TXG of the
5609 * snapshot it was created from and use that to find
5610 * blocks that were born after it.
5612 zfs_bookmark_phys_t frombm;
5614 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5615 &frombm);
5616 if (error != 0)
5617 goto out;
5618 error = dmu_send_estimate_from_txg(tosnap,
5619 frombm.zbm_creation_txg, compressok, &space);
5620 } else {
5622 * from is not properly formatted as a snapshot or
5623 * bookmark
5625 error = SET_ERROR(EINVAL);
5626 goto out;
5628 } else {
5630 * If estimating the size of a full send, use dmu_send_estimate.
5632 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5635 fnvlist_add_uint64(outnvl, "space", space);
5637 out:
5638 dsl_dataset_rele(tosnap, FTAG);
5639 dsl_pool_rele(dp, FTAG);
5640 return (error);
5643 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5645 static void
5646 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5647 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5648 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5650 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5652 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5653 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5654 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5655 ASSERT3P(vec->zvec_func, ==, NULL);
5657 vec->zvec_legacy_func = func;
5658 vec->zvec_secpolicy = secpolicy;
5659 vec->zvec_namecheck = namecheck;
5660 vec->zvec_allow_log = log_history;
5661 vec->zvec_pool_check = pool_check;
5665 * See the block comment at the beginning of this file for details on
5666 * each argument to this function.
5668 static void
5669 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5670 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5671 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5672 boolean_t allow_log)
5674 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5676 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5677 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5678 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5679 ASSERT3P(vec->zvec_func, ==, NULL);
5681 /* if we are logging, the name must be valid */
5682 ASSERT(!allow_log || namecheck != NO_NAME);
5684 vec->zvec_name = name;
5685 vec->zvec_func = func;
5686 vec->zvec_secpolicy = secpolicy;
5687 vec->zvec_namecheck = namecheck;
5688 vec->zvec_pool_check = pool_check;
5689 vec->zvec_smush_outnvlist = smush_outnvlist;
5690 vec->zvec_allow_log = allow_log;
5693 static void
5694 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5695 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5696 zfs_ioc_poolcheck_t pool_check)
5698 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5699 POOL_NAME, log_history, pool_check);
5702 static void
5703 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5704 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5706 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5707 DATASET_NAME, B_FALSE, pool_check);
5710 static void
5711 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5713 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5714 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5717 static void
5718 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5719 zfs_secpolicy_func_t *secpolicy)
5721 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5722 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5725 static void
5726 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5727 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5729 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5730 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5733 static void
5734 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5736 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5737 zfs_secpolicy_read);
5740 static void
5741 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5742 zfs_secpolicy_func_t *secpolicy)
5744 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5745 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5748 static void
5749 zfs_ioctl_init(void)
5751 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5752 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5753 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5755 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5756 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5757 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5759 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5760 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5761 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5763 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5764 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5765 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5767 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5768 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5769 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5771 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5772 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5773 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5775 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5776 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5777 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5779 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
5780 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
5781 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5783 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5784 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5785 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5787 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5788 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5789 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5790 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5791 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5792 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5794 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5795 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5796 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5798 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5799 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5800 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5802 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5803 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5804 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5806 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5807 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5808 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5810 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5811 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5812 POOL_NAME,
5813 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5815 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5816 zfs_ioc_channel_program, zfs_secpolicy_config,
5817 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5818 B_TRUE);
5820 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
5821 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
5822 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5824 zfs_ioctl_register("zpool_discard_checkpoint",
5825 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
5826 zfs_secpolicy_config, POOL_NAME,
5827 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5829 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
5830 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
5831 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5833 /* IOCTLS that use the legacy function signature */
5835 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5836 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5838 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5839 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5840 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5841 zfs_ioc_pool_scan);
5842 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5843 zfs_ioc_pool_upgrade);
5844 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5845 zfs_ioc_vdev_add);
5846 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5847 zfs_ioc_vdev_remove);
5848 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5849 zfs_ioc_vdev_set_state);
5850 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5851 zfs_ioc_vdev_attach);
5852 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5853 zfs_ioc_vdev_detach);
5854 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5855 zfs_ioc_vdev_setpath);
5856 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5857 zfs_ioc_vdev_setfru);
5858 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5859 zfs_ioc_pool_set_props);
5860 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5861 zfs_ioc_vdev_split);
5862 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5863 zfs_ioc_pool_reguid);
5865 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5866 zfs_ioc_pool_configs, zfs_secpolicy_none);
5867 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5868 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5869 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5870 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5871 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5872 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5873 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5874 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5877 * pool destroy, and export don't log the history as part of
5878 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5879 * does the logging of those commands.
5881 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5882 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5883 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5884 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5886 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5887 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5888 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5889 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5891 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5892 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5893 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5894 zfs_ioc_dsobj_to_dsname,
5895 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5896 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5897 zfs_ioc_pool_get_history,
5898 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5900 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5901 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5903 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5904 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
5905 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5906 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5908 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5909 zfs_ioc_space_written);
5910 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5911 zfs_ioc_objset_recvd_props);
5912 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5913 zfs_ioc_next_obj);
5914 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5915 zfs_ioc_get_fsacl);
5916 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5917 zfs_ioc_objset_stats);
5918 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5919 zfs_ioc_objset_zplprops);
5920 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5921 zfs_ioc_dataset_list_next);
5922 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5923 zfs_ioc_snapshot_list_next);
5924 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5925 zfs_ioc_send_progress);
5927 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5928 zfs_ioc_diff, zfs_secpolicy_diff);
5929 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5930 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5931 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5932 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5933 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5934 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5935 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5936 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5937 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5938 zfs_ioc_send, zfs_secpolicy_send);
5940 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5941 zfs_secpolicy_none);
5942 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5943 zfs_secpolicy_destroy);
5944 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5945 zfs_secpolicy_rename);
5946 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5947 zfs_secpolicy_recv);
5948 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5949 zfs_secpolicy_promote);
5950 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5951 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5952 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5953 zfs_secpolicy_set_fsacl);
5955 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5956 zfs_secpolicy_share, POOL_CHECK_NONE);
5957 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5958 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5959 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5960 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5961 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5962 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5963 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5964 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5968 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5969 zfs_ioc_poolcheck_t check)
5971 spa_t *spa;
5972 int error;
5974 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5976 if (check & POOL_CHECK_NONE)
5977 return (0);
5979 error = spa_open(name, &spa, FTAG);
5980 if (error == 0) {
5981 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5982 error = SET_ERROR(EAGAIN);
5983 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5984 error = SET_ERROR(EROFS);
5985 spa_close(spa, FTAG);
5987 return (error);
5991 * Find a free minor number.
5993 minor_t
5994 zfsdev_minor_alloc(void)
5996 static minor_t last_minor;
5997 minor_t m;
5999 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6001 for (m = last_minor + 1; m != last_minor; m++) {
6002 if (m > ZFSDEV_MAX_MINOR)
6003 m = 1;
6004 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6005 last_minor = m;
6006 return (m);
6010 return (0);
6013 static int
6014 zfs_ctldev_init(dev_t *devp)
6016 minor_t minor;
6017 zfs_soft_state_t *zs;
6019 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6020 ASSERT(getminor(*devp) == 0);
6022 minor = zfsdev_minor_alloc();
6023 if (minor == 0)
6024 return (SET_ERROR(ENXIO));
6026 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6027 return (SET_ERROR(EAGAIN));
6029 *devp = makedevice(getemajor(*devp), minor);
6031 zs = ddi_get_soft_state(zfsdev_state, minor);
6032 zs->zss_type = ZSST_CTLDEV;
6033 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6035 return (0);
6038 static void
6039 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6041 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6043 zfs_onexit_destroy(zo);
6044 ddi_soft_state_free(zfsdev_state, minor);
6047 void *
6048 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6050 zfs_soft_state_t *zp;
6052 zp = ddi_get_soft_state(zfsdev_state, minor);
6053 if (zp == NULL || zp->zss_type != which)
6054 return (NULL);
6056 return (zp->zss_data);
6059 static int
6060 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6062 int error = 0;
6064 if (getminor(*devp) != 0)
6065 return (zvol_open(devp, flag, otyp, cr));
6067 /* This is the control device. Allocate a new minor if requested. */
6068 if (flag & FEXCL) {
6069 mutex_enter(&zfsdev_state_lock);
6070 error = zfs_ctldev_init(devp);
6071 mutex_exit(&zfsdev_state_lock);
6074 return (error);
6077 static int
6078 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6080 zfs_onexit_t *zo;
6081 minor_t minor = getminor(dev);
6083 if (minor == 0)
6084 return (0);
6086 mutex_enter(&zfsdev_state_lock);
6087 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6088 if (zo == NULL) {
6089 mutex_exit(&zfsdev_state_lock);
6090 return (zvol_close(dev, flag, otyp, cr));
6092 zfs_ctldev_destroy(zo, minor);
6093 mutex_exit(&zfsdev_state_lock);
6095 return (0);
6098 static int
6099 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6101 zfs_cmd_t *zc;
6102 uint_t vecnum;
6103 int error, rc, len;
6104 minor_t minor = getminor(dev);
6105 const zfs_ioc_vec_t *vec;
6106 char *saved_poolname = NULL;
6107 nvlist_t *innvl = NULL;
6109 if (minor != 0 &&
6110 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6111 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6113 vecnum = cmd - ZFS_IOC_FIRST;
6114 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6116 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6117 return (SET_ERROR(EINVAL));
6118 vec = &zfs_ioc_vec[vecnum];
6120 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6122 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6123 if (error != 0) {
6124 error = SET_ERROR(EFAULT);
6125 goto out;
6128 zc->zc_iflags = flag & FKIOCTL;
6129 if (zc->zc_nvlist_src_size != 0) {
6130 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6131 zc->zc_iflags, &innvl);
6132 if (error != 0)
6133 goto out;
6137 * Ensure that all pool/dataset names are valid before we pass down to
6138 * the lower layers.
6140 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6141 switch (vec->zvec_namecheck) {
6142 case POOL_NAME:
6143 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6144 error = SET_ERROR(EINVAL);
6145 else
6146 error = pool_status_check(zc->zc_name,
6147 vec->zvec_namecheck, vec->zvec_pool_check);
6148 break;
6150 case DATASET_NAME:
6151 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6152 error = SET_ERROR(EINVAL);
6153 else
6154 error = pool_status_check(zc->zc_name,
6155 vec->zvec_namecheck, vec->zvec_pool_check);
6156 break;
6158 case NO_NAME:
6159 break;
6163 if (error == 0)
6164 error = vec->zvec_secpolicy(zc, innvl, cr);
6166 if (error != 0)
6167 goto out;
6169 /* legacy ioctls can modify zc_name */
6170 len = strcspn(zc->zc_name, "/@#") + 1;
6171 saved_poolname = kmem_alloc(len, KM_SLEEP);
6172 (void) strlcpy(saved_poolname, zc->zc_name, len);
6174 if (vec->zvec_func != NULL) {
6175 nvlist_t *outnvl;
6176 int puterror = 0;
6177 spa_t *spa;
6178 nvlist_t *lognv = NULL;
6180 ASSERT(vec->zvec_legacy_func == NULL);
6183 * Add the innvl to the lognv before calling the func,
6184 * in case the func changes the innvl.
6186 if (vec->zvec_allow_log) {
6187 lognv = fnvlist_alloc();
6188 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6189 vec->zvec_name);
6190 if (!nvlist_empty(innvl)) {
6191 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6192 innvl);
6196 outnvl = fnvlist_alloc();
6197 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6200 * Some commands can partially execute, modfiy state, and still
6201 * return an error. In these cases, attempt to record what
6202 * was modified.
6204 if ((error == 0 ||
6205 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6206 vec->zvec_allow_log &&
6207 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6208 if (!nvlist_empty(outnvl)) {
6209 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6210 outnvl);
6212 if (error != 0) {
6213 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6214 error);
6216 (void) spa_history_log_nvl(spa, lognv);
6217 spa_close(spa, FTAG);
6219 fnvlist_free(lognv);
6221 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6222 int smusherror = 0;
6223 if (vec->zvec_smush_outnvlist) {
6224 smusherror = nvlist_smush(outnvl,
6225 zc->zc_nvlist_dst_size);
6227 if (smusherror == 0)
6228 puterror = put_nvlist(zc, outnvl);
6231 if (puterror != 0)
6232 error = puterror;
6234 nvlist_free(outnvl);
6235 } else {
6236 error = vec->zvec_legacy_func(zc);
6239 out:
6240 nvlist_free(innvl);
6241 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6242 if (error == 0 && rc != 0)
6243 error = SET_ERROR(EFAULT);
6244 if (error == 0 && vec->zvec_allow_log) {
6245 char *s = tsd_get(zfs_allow_log_key);
6246 if (s != NULL)
6247 strfree(s);
6248 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6249 } else {
6250 if (saved_poolname != NULL)
6251 strfree(saved_poolname);
6254 kmem_free(zc, sizeof (zfs_cmd_t));
6255 return (error);
6258 static int
6259 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6261 if (cmd != DDI_ATTACH)
6262 return (DDI_FAILURE);
6264 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6265 DDI_PSEUDO, 0) == DDI_FAILURE)
6266 return (DDI_FAILURE);
6268 zfs_dip = dip;
6270 ddi_report_dev(dip);
6272 return (DDI_SUCCESS);
6275 static int
6276 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6278 if (spa_busy() || zfs_busy() || zvol_busy())
6279 return (DDI_FAILURE);
6281 if (cmd != DDI_DETACH)
6282 return (DDI_FAILURE);
6284 zfs_dip = NULL;
6286 ddi_prop_remove_all(dip);
6287 ddi_remove_minor_node(dip, NULL);
6289 return (DDI_SUCCESS);
6292 /*ARGSUSED*/
6293 static int
6294 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6296 switch (infocmd) {
6297 case DDI_INFO_DEVT2DEVINFO:
6298 *result = zfs_dip;
6299 return (DDI_SUCCESS);
6301 case DDI_INFO_DEVT2INSTANCE:
6302 *result = NULL;
6303 return (DDI_SUCCESS);
6306 return (DDI_FAILURE);
6310 * OK, so this is a little weird.
6312 * /dev/zfs is the control node, i.e. minor 0.
6313 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6315 * /dev/zfs has basically nothing to do except serve up ioctls,
6316 * so most of the standard driver entry points are in zvol.c.
6318 static struct cb_ops zfs_cb_ops = {
6319 zfsdev_open, /* open */
6320 zfsdev_close, /* close */
6321 zvol_strategy, /* strategy */
6322 nodev, /* print */
6323 zvol_dump, /* dump */
6324 zvol_read, /* read */
6325 zvol_write, /* write */
6326 zfsdev_ioctl, /* ioctl */
6327 nodev, /* devmap */
6328 nodev, /* mmap */
6329 nodev, /* segmap */
6330 nochpoll, /* poll */
6331 ddi_prop_op, /* prop_op */
6332 NULL, /* streamtab */
6333 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6334 CB_REV, /* version */
6335 nodev, /* async read */
6336 nodev, /* async write */
6339 static struct dev_ops zfs_dev_ops = {
6340 DEVO_REV, /* version */
6341 0, /* refcnt */
6342 zfs_info, /* info */
6343 nulldev, /* identify */
6344 nulldev, /* probe */
6345 zfs_attach, /* attach */
6346 zfs_detach, /* detach */
6347 nodev, /* reset */
6348 &zfs_cb_ops, /* driver operations */
6349 NULL, /* no bus operations */
6350 NULL, /* power */
6351 ddi_quiesce_not_needed, /* quiesce */
6354 static struct modldrv zfs_modldrv = {
6355 &mod_driverops,
6356 "ZFS storage pool",
6357 &zfs_dev_ops
6360 static struct modlinkage modlinkage = {
6361 MODREV_1,
6362 (void *)&zfs_modlfs,
6363 (void *)&zfs_modldrv,
6364 NULL
6367 static void
6368 zfs_allow_log_destroy(void *arg)
6370 char *poolname = arg;
6371 strfree(poolname);
6375 _init(void)
6377 int error;
6379 spa_init(FREAD | FWRITE);
6380 zfs_init();
6381 zvol_init();
6382 zfs_ioctl_init();
6384 if ((error = mod_install(&modlinkage)) != 0) {
6385 zvol_fini();
6386 zfs_fini();
6387 spa_fini();
6388 return (error);
6391 tsd_create(&zfs_fsyncer_key, NULL);
6392 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6393 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6395 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6396 ASSERT(error == 0);
6397 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6399 return (0);
6403 _fini(void)
6405 int error;
6407 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6408 return (SET_ERROR(EBUSY));
6410 if ((error = mod_remove(&modlinkage)) != 0)
6411 return (error);
6413 zvol_fini();
6414 zfs_fini();
6415 spa_fini();
6416 if (zfs_nfsshare_inited)
6417 (void) ddi_modclose(nfs_mod);
6418 if (zfs_smbshare_inited)
6419 (void) ddi_modclose(smbsrv_mod);
6420 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6421 (void) ddi_modclose(sharefs_mod);
6423 tsd_destroy(&zfs_fsyncer_key);
6424 ldi_ident_release(zfs_li);
6425 zfs_li = NULL;
6426 mutex_destroy(&zfs_share_lock);
6428 return (error);
6432 _info(struct modinfo *modinfop)
6434 return (mod_info(&modlinkage, modinfop));