Merge illumos-gate
[unleashed.git] / kernel / fs / zfs / zfs_ioctl.c
blob0da1e418bdc2f92809a744809d627c40cb346448
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_defer_destroy mark for deferred destroy
3581 * outputs: none
3583 static int
3584 zfs_ioc_destroy(zfs_cmd_t *zc)
3586 objset_t *os;
3587 dmu_objset_type_t ost;
3588 int err;
3590 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3591 if (err != 0)
3592 return (err);
3593 ost = dmu_objset_type(os);
3594 dmu_objset_rele(os, FTAG);
3596 if (ost == DMU_OST_ZFS)
3597 zfs_unmount_snap(zc->zc_name);
3599 if (strchr(zc->zc_name, '@'))
3600 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3601 else
3602 err = dsl_destroy_head(zc->zc_name);
3603 if (ost == DMU_OST_ZVOL && err == 0)
3604 (void) zvol_remove_minor(zc->zc_name);
3605 return (err);
3609 * innvl: {
3610 * vdevs: {
3611 * guid 1, guid 2, ...
3612 * },
3613 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
3616 * outnvl: {
3617 * [func: EINVAL (if provided command type didn't make sense)],
3618 * [vdevs: {
3619 * guid1: errno, (see function body for possible errnos)
3620 * ...
3621 * }]
3625 static int
3626 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3628 spa_t *spa;
3629 int error;
3631 error = spa_open(poolname, &spa, FTAG);
3632 if (error != 0)
3633 return (error);
3635 uint64_t cmd_type;
3636 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3637 &cmd_type) != 0) {
3638 spa_close(spa, FTAG);
3639 return (SET_ERROR(EINVAL));
3641 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3642 cmd_type == POOL_INITIALIZE_DO ||
3643 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3644 spa_close(spa, FTAG);
3645 return (SET_ERROR(EINVAL));
3648 nvlist_t *vdev_guids;
3649 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3650 &vdev_guids) != 0) {
3651 spa_close(spa, FTAG);
3652 return (SET_ERROR(EINVAL));
3655 nvlist_t *vdev_errlist = fnvlist_alloc();
3656 int total_errors = 0;
3658 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3659 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3660 uint64_t vdev_guid = fnvpair_value_uint64(pair);
3662 error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
3663 if (error != 0) {
3664 char guid_as_str[MAXNAMELEN];
3666 (void) snprintf(guid_as_str, sizeof (guid_as_str),
3667 "%llu", (unsigned long long)vdev_guid);
3668 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
3669 total_errors++;
3672 if (fnvlist_size(vdev_errlist) > 0) {
3673 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3674 vdev_errlist);
3676 fnvlist_free(vdev_errlist);
3678 spa_close(spa, FTAG);
3679 return (total_errors > 0 ? EINVAL : 0);
3683 * fsname is name of dataset to rollback (to most recent snapshot)
3685 * innvl may contain name of expected target snapshot
3687 * outnvl: "target" -> name of most recent snapshot
3690 /* ARGSUSED */
3691 static int
3692 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3694 zfsvfs_t *zfsvfs;
3695 char *target = NULL;
3696 int error;
3698 (void) nvlist_lookup_string(innvl, "target", &target);
3699 if (target != NULL) {
3700 const char *cp = strchr(target, '@');
3703 * The snap name must contain an @, and the part after it must
3704 * contain only valid characters.
3706 if (cp == NULL ||
3707 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3708 return (SET_ERROR(EINVAL));
3711 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3712 dsl_dataset_t *ds;
3714 ds = dmu_objset_ds(zfsvfs->z_os);
3715 error = zfs_suspend_fs(zfsvfs);
3716 if (error == 0) {
3717 int resume_err;
3719 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3720 outnvl);
3721 resume_err = zfs_resume_fs(zfsvfs, ds);
3722 error = error ? error : resume_err;
3724 VFS_RELE(zfsvfs->z_vfs);
3725 } else {
3726 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3728 return (error);
3731 static int
3732 recursive_unmount(const char *fsname, void *arg)
3734 const char *snapname = arg;
3735 char fullname[ZFS_MAX_DATASET_NAME_LEN];
3737 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3738 zfs_unmount_snap(fullname);
3740 return (0);
3744 * inputs:
3745 * zc_name old name of dataset
3746 * zc_value new name of dataset
3747 * zc_cookie recursive flag (only valid for snapshots)
3749 * outputs: none
3751 static int
3752 zfs_ioc_rename(zfs_cmd_t *zc)
3754 objset_t *os;
3755 dmu_objset_type_t ost;
3756 boolean_t recursive = zc->zc_cookie & 1;
3757 char *at;
3758 int err;
3760 /* "zfs rename" from and to ...%recv datasets should both fail */
3761 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3762 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3763 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3764 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3765 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3766 return (SET_ERROR(EINVAL));
3768 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3769 if (err != 0)
3770 return (err);
3771 ost = dmu_objset_type(os);
3772 dmu_objset_rele(os, FTAG);
3774 at = strchr(zc->zc_name, '@');
3775 if (at != NULL) {
3776 /* snaps must be in same fs */
3777 int error;
3779 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3780 return (SET_ERROR(EXDEV));
3781 *at = '\0';
3782 if (ost == DMU_OST_ZFS) {
3783 error = dmu_objset_find(zc->zc_name,
3784 recursive_unmount, at + 1,
3785 recursive ? DS_FIND_CHILDREN : 0);
3786 if (error != 0) {
3787 *at = '@';
3788 return (error);
3791 error = dsl_dataset_rename_snapshot(zc->zc_name,
3792 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3793 *at = '@';
3795 return (error);
3796 } else {
3797 if (ost == DMU_OST_ZVOL)
3798 (void) zvol_remove_minor(zc->zc_name);
3799 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3803 static int
3804 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3806 const char *propname = nvpair_name(pair);
3807 boolean_t issnap = (strchr(dsname, '@') != NULL);
3808 zfs_prop_t prop = zfs_name_to_prop(propname);
3809 uint64_t intval;
3810 int err;
3812 if (prop == ZPROP_INVAL) {
3813 if (zfs_prop_user(propname)) {
3814 if (err = zfs_secpolicy_write_perms(dsname,
3815 ZFS_DELEG_PERM_USERPROP, cr))
3816 return (err);
3817 return (0);
3820 if (!issnap && zfs_prop_userquota(propname)) {
3821 const char *perm = NULL;
3822 const char *uq_prefix =
3823 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3824 const char *gq_prefix =
3825 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3827 if (strncmp(propname, uq_prefix,
3828 strlen(uq_prefix)) == 0) {
3829 perm = ZFS_DELEG_PERM_USERQUOTA;
3830 } else if (strncmp(propname, gq_prefix,
3831 strlen(gq_prefix)) == 0) {
3832 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3833 } else {
3834 /* USERUSED and GROUPUSED are read-only */
3835 return (SET_ERROR(EINVAL));
3838 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
3839 return (err);
3840 return (0);
3843 return (SET_ERROR(EINVAL));
3846 if (issnap)
3847 return (SET_ERROR(EINVAL));
3849 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3851 * dsl_prop_get_all_impl() returns properties in this
3852 * format.
3854 nvlist_t *attrs;
3855 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3856 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3857 &pair) == 0);
3861 * Check that this value is valid for this pool version
3863 switch (prop) {
3864 case ZFS_PROP_COMPRESSION:
3866 * If the user specified gzip compression, make sure
3867 * the SPA supports it. We ignore any errors here since
3868 * we'll catch them later.
3870 if (nvpair_value_uint64(pair, &intval) == 0) {
3871 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3872 intval <= ZIO_COMPRESS_GZIP_9 &&
3873 zfs_earlier_version(dsname,
3874 SPA_VERSION_GZIP_COMPRESSION)) {
3875 return (SET_ERROR(ENOTSUP));
3878 if (intval == ZIO_COMPRESS_ZLE &&
3879 zfs_earlier_version(dsname,
3880 SPA_VERSION_ZLE_COMPRESSION))
3881 return (SET_ERROR(ENOTSUP));
3883 if (intval == ZIO_COMPRESS_LZ4) {
3884 spa_t *spa;
3886 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3887 return (err);
3889 if (!spa_feature_is_enabled(spa,
3890 SPA_FEATURE_LZ4_COMPRESS)) {
3891 spa_close(spa, FTAG);
3892 return (SET_ERROR(ENOTSUP));
3894 spa_close(spa, FTAG);
3898 * If this is a bootable dataset then
3899 * verify that the compression algorithm
3900 * is supported for booting. We must return
3901 * something other than ENOTSUP since it
3902 * implies a downrev pool version.
3904 if (zfs_is_bootfs(dsname) &&
3905 !BOOTFS_COMPRESS_VALID(intval)) {
3906 return (SET_ERROR(ERANGE));
3909 break;
3911 case ZFS_PROP_COPIES:
3912 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3913 return (SET_ERROR(ENOTSUP));
3914 break;
3916 case ZFS_PROP_RECORDSIZE:
3917 /* Record sizes above 128k need the feature to be enabled */
3918 if (nvpair_value_uint64(pair, &intval) == 0 &&
3919 intval > SPA_OLD_MAXBLOCKSIZE) {
3920 spa_t *spa;
3923 * We don't allow setting the property above 1MB,
3924 * unless the tunable has been changed.
3926 if (intval > zfs_max_recordsize ||
3927 intval > SPA_MAXBLOCKSIZE)
3928 return (SET_ERROR(ERANGE));
3930 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3931 return (err);
3933 if (!spa_feature_is_enabled(spa,
3934 SPA_FEATURE_LARGE_BLOCKS)) {
3935 spa_close(spa, FTAG);
3936 return (SET_ERROR(ENOTSUP));
3938 spa_close(spa, FTAG);
3940 break;
3942 case ZFS_PROP_SHARESMB:
3943 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3944 return (SET_ERROR(ENOTSUP));
3945 break;
3947 case ZFS_PROP_ACLINHERIT:
3948 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3949 nvpair_value_uint64(pair, &intval) == 0) {
3950 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3951 zfs_earlier_version(dsname,
3952 SPA_VERSION_PASSTHROUGH_X))
3953 return (SET_ERROR(ENOTSUP));
3955 break;
3957 case ZFS_PROP_CHECKSUM:
3958 case ZFS_PROP_DEDUP:
3960 spa_feature_t feature;
3961 spa_t *spa;
3963 /* dedup feature version checks */
3964 if (prop == ZFS_PROP_DEDUP &&
3965 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3966 return (SET_ERROR(ENOTSUP));
3968 if (nvpair_value_uint64(pair, &intval) != 0)
3969 return (SET_ERROR(EINVAL));
3971 /* check prop value is enabled in features */
3972 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3973 if (feature == SPA_FEATURE_NONE)
3974 break;
3976 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3977 return (err);
3979 if (!spa_feature_is_enabled(spa, feature)) {
3980 spa_close(spa, FTAG);
3981 return (SET_ERROR(ENOTSUP));
3983 spa_close(spa, FTAG);
3984 break;
3988 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3992 * Checks for a race condition to make sure we don't increment a feature flag
3993 * multiple times.
3995 static int
3996 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
3998 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3999 spa_feature_t *featurep = arg;
4001 if (!spa_feature_is_active(spa, *featurep))
4002 return (0);
4003 else
4004 return (SET_ERROR(EBUSY));
4008 * The callback invoked on feature activation in the sync task caused by
4009 * zfs_prop_activate_feature.
4011 static void
4012 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4014 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4015 spa_feature_t *featurep = arg;
4017 spa_feature_incr(spa, *featurep, tx);
4021 * Activates a feature on a pool in response to a property setting. This
4022 * creates a new sync task which modifies the pool to reflect the feature
4023 * as being active.
4025 static int
4026 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4028 int err;
4030 /* EBUSY here indicates that the feature is already active */
4031 err = dsl_sync_task(spa_name(spa),
4032 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4033 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4035 if (err != 0 && err != EBUSY)
4036 return (err);
4037 else
4038 return (0);
4042 * Removes properties from the given props list that fail permission checks
4043 * needed to clear them and to restore them in case of a receive error. For each
4044 * property, make sure we have both set and inherit permissions.
4046 * Returns the first error encountered if any permission checks fail. If the
4047 * caller provides a non-NULL errlist, it also gives the complete list of names
4048 * of all the properties that failed a permission check along with the
4049 * corresponding error numbers. The caller is responsible for freeing the
4050 * returned errlist.
4052 * If every property checks out successfully, zero is returned and the list
4053 * pointed at by errlist is NULL.
4055 static int
4056 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4058 zfs_cmd_t *zc;
4059 nvpair_t *pair, *next_pair;
4060 nvlist_t *errors;
4061 int err, rv = 0;
4063 if (props == NULL)
4064 return (0);
4066 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4068 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4069 (void) strcpy(zc->zc_name, dataset);
4070 pair = nvlist_next_nvpair(props, NULL);
4071 while (pair != NULL) {
4072 next_pair = nvlist_next_nvpair(props, pair);
4074 (void) strcpy(zc->zc_value, nvpair_name(pair));
4075 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4076 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4077 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4078 VERIFY(nvlist_add_int32(errors,
4079 zc->zc_value, err) == 0);
4081 pair = next_pair;
4083 kmem_free(zc, sizeof (zfs_cmd_t));
4085 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4086 nvlist_free(errors);
4087 errors = NULL;
4088 } else {
4089 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4092 if (errlist == NULL)
4093 nvlist_free(errors);
4094 else
4095 *errlist = errors;
4097 return (rv);
4100 static boolean_t
4101 propval_equals(nvpair_t *p1, nvpair_t *p2)
4103 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4104 /* dsl_prop_get_all_impl() format */
4105 nvlist_t *attrs;
4106 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4107 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4108 &p1) == 0);
4111 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4112 nvlist_t *attrs;
4113 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4114 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4115 &p2) == 0);
4118 if (nvpair_type(p1) != nvpair_type(p2))
4119 return (B_FALSE);
4121 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4122 char *valstr1, *valstr2;
4124 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4125 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4126 return (strcmp(valstr1, valstr2) == 0);
4127 } else {
4128 uint64_t intval1, intval2;
4130 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4131 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4132 return (intval1 == intval2);
4137 * Remove properties from props if they are not going to change (as determined
4138 * by comparison with origprops). Remove them from origprops as well, since we
4139 * do not need to clear or restore properties that won't change.
4141 static void
4142 props_reduce(nvlist_t *props, nvlist_t *origprops)
4144 nvpair_t *pair, *next_pair;
4146 if (origprops == NULL)
4147 return; /* all props need to be received */
4149 pair = nvlist_next_nvpair(props, NULL);
4150 while (pair != NULL) {
4151 const char *propname = nvpair_name(pair);
4152 nvpair_t *match;
4154 next_pair = nvlist_next_nvpair(props, pair);
4156 if ((nvlist_lookup_nvpair(origprops, propname,
4157 &match) != 0) || !propval_equals(pair, match))
4158 goto next; /* need to set received value */
4160 /* don't clear the existing received value */
4161 (void) nvlist_remove_nvpair(origprops, match);
4162 /* don't bother receiving the property */
4163 (void) nvlist_remove_nvpair(props, pair);
4164 next:
4165 pair = next_pair;
4170 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4171 * For example, refquota cannot be set until after the receipt of a dataset,
4172 * because in replication streams, an older/earlier snapshot may exceed the
4173 * refquota. We want to receive the older/earlier snapshot, but setting
4174 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4175 * the older/earlier snapshot from being received (with EDQUOT).
4177 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4179 * libzfs will need to be judicious handling errors encountered by props
4180 * extracted by this function.
4182 static nvlist_t *
4183 extract_delay_props(nvlist_t *props)
4185 nvlist_t *delayprops;
4186 nvpair_t *nvp, *tmp;
4187 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4188 int i;
4190 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4192 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4193 nvp = nvlist_next_nvpair(props, nvp)) {
4195 * strcmp() is safe because zfs_prop_to_name() always returns
4196 * a bounded string.
4198 for (i = 0; delayable[i] != 0; i++) {
4199 if (strcmp(zfs_prop_to_name(delayable[i]),
4200 nvpair_name(nvp)) == 0) {
4201 break;
4204 if (delayable[i] != 0) {
4205 tmp = nvlist_prev_nvpair(props, nvp);
4206 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4207 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4208 nvp = tmp;
4212 if (nvlist_empty(delayprops)) {
4213 nvlist_free(delayprops);
4214 delayprops = NULL;
4216 return (delayprops);
4219 #ifdef DEBUG
4220 static boolean_t zfs_ioc_recv_inject_err;
4221 #endif
4224 * inputs:
4225 * zc_name name of containing filesystem
4226 * zc_nvlist_src{_size} nvlist of properties to apply
4227 * zc_value name of snapshot to create
4228 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4229 * zc_cookie file descriptor to recv from
4230 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4231 * zc_guid force flag
4232 * zc_cleanup_fd cleanup-on-exit file descriptor
4233 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4234 * zc_resumable if data is incomplete assume sender will resume
4236 * outputs:
4237 * zc_cookie number of bytes read
4238 * zc_nvlist_dst{_size} error for each unapplied received property
4239 * zc_obj zprop_errflags_t
4240 * zc_action_handle handle for this guid/ds mapping
4242 static int
4243 zfs_ioc_recv(zfs_cmd_t *zc)
4245 file_t *fp;
4246 dmu_recv_cookie_t drc;
4247 boolean_t force = (boolean_t)zc->zc_guid;
4248 int fd;
4249 int error = 0;
4250 int props_error = 0;
4251 nvlist_t *errors;
4252 offset_t off;
4253 nvlist_t *props = NULL; /* sent properties */
4254 nvlist_t *origprops = NULL; /* existing properties */
4255 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4256 char *origin = NULL;
4257 char *tosnap;
4258 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4259 boolean_t first_recvd_props = B_FALSE;
4261 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4262 strchr(zc->zc_value, '@') == NULL ||
4263 strchr(zc->zc_value, '%'))
4264 return (SET_ERROR(EINVAL));
4266 (void) strcpy(tofs, zc->zc_value);
4267 tosnap = strchr(tofs, '@');
4268 *tosnap++ = '\0';
4270 if (zc->zc_nvlist_src != (uintptr_t)NULL &&
4271 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4272 zc->zc_iflags, &props)) != 0)
4273 return (error);
4275 fd = zc->zc_cookie;
4276 fp = getf(fd);
4277 if (fp == NULL) {
4278 nvlist_free(props);
4279 return (SET_ERROR(EBADF));
4282 errors = fnvlist_alloc();
4284 if (zc->zc_string[0])
4285 origin = zc->zc_string;
4287 error = dmu_recv_begin(tofs, tosnap,
4288 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4289 if (error != 0)
4290 goto out;
4293 * Set properties before we receive the stream so that they are applied
4294 * to the new data. Note that we must call dmu_recv_stream() if
4295 * dmu_recv_begin() succeeds.
4297 if (props != NULL && !drc.drc_newfs) {
4298 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4299 SPA_VERSION_RECVD_PROPS &&
4300 !dsl_prop_get_hasrecvd(tofs))
4301 first_recvd_props = B_TRUE;
4304 * If new received properties are supplied, they are to
4305 * completely replace the existing received properties, so stash
4306 * away the existing ones.
4308 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4309 nvlist_t *errlist = NULL;
4311 * Don't bother writing a property if its value won't
4312 * change (and avoid the unnecessary security checks).
4314 * The first receive after SPA_VERSION_RECVD_PROPS is a
4315 * special case where we blow away all local properties
4316 * regardless.
4318 if (!first_recvd_props)
4319 props_reduce(props, origprops);
4320 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4321 (void) nvlist_merge(errors, errlist, 0);
4322 nvlist_free(errlist);
4324 if (clear_received_props(tofs, origprops,
4325 first_recvd_props ? NULL : props) != 0)
4326 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4327 } else {
4328 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4332 if (props != NULL) {
4333 props_error = dsl_prop_set_hasrecvd(tofs);
4335 if (props_error == 0) {
4336 delayprops = extract_delay_props(props);
4337 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4338 props, errors);
4342 off = fp->f_offset;
4343 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4344 &zc->zc_action_handle);
4346 if (error == 0) {
4347 zfsvfs_t *zfsvfs = NULL;
4349 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4350 /* online recv */
4351 dsl_dataset_t *ds;
4352 int end_err;
4354 ds = dmu_objset_ds(zfsvfs->z_os);
4355 error = zfs_suspend_fs(zfsvfs);
4357 * If the suspend fails, then the recv_end will
4358 * likely also fail, and clean up after itself.
4360 end_err = dmu_recv_end(&drc, zfsvfs);
4361 if (error == 0)
4362 error = zfs_resume_fs(zfsvfs, ds);
4363 error = error ? error : end_err;
4364 VFS_RELE(zfsvfs->z_vfs);
4365 } else {
4366 error = dmu_recv_end(&drc, NULL);
4369 /* Set delayed properties now, after we're done receiving. */
4370 if (delayprops != NULL && error == 0) {
4371 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4372 delayprops, errors);
4376 if (delayprops != NULL) {
4378 * Merge delayed props back in with initial props, in case
4379 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4380 * we have to make sure clear_received_props() includes
4381 * the delayed properties).
4383 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4384 * using ASSERT() will be just like a VERIFY.
4386 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4387 nvlist_free(delayprops);
4391 * Now that all props, initial and delayed, are set, report the prop
4392 * errors to the caller.
4394 if (zc->zc_nvlist_dst_size != 0 &&
4395 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4396 put_nvlist(zc, errors) != 0)) {
4398 * Caller made zc->zc_nvlist_dst less than the minimum expected
4399 * size or supplied an invalid address.
4401 props_error = SET_ERROR(EINVAL);
4404 zc->zc_cookie = off - fp->f_offset;
4405 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4406 fp->f_offset = off;
4408 #ifdef DEBUG
4409 if (zfs_ioc_recv_inject_err) {
4410 zfs_ioc_recv_inject_err = B_FALSE;
4411 error = 1;
4413 #endif
4415 * On error, restore the original props.
4417 if (error != 0 && props != NULL && !drc.drc_newfs) {
4418 if (clear_received_props(tofs, props, NULL) != 0) {
4420 * We failed to clear the received properties.
4421 * Since we may have left a $recvd value on the
4422 * system, we can't clear the $hasrecvd flag.
4424 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4425 } else if (first_recvd_props) {
4426 dsl_prop_unset_hasrecvd(tofs);
4429 if (origprops == NULL && !drc.drc_newfs) {
4430 /* We failed to stash the original properties. */
4431 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4435 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4436 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4437 * explictly if we're restoring local properties cleared in the
4438 * first new-style receive.
4440 if (origprops != NULL &&
4441 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4442 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4443 origprops, NULL) != 0) {
4445 * We stashed the original properties but failed to
4446 * restore them.
4448 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4451 out:
4452 nvlist_free(props);
4453 nvlist_free(origprops);
4454 nvlist_free(errors);
4455 releasef(fd);
4457 if (error == 0)
4458 error = props_error;
4460 return (error);
4464 * inputs:
4465 * zc_name name of snapshot to send
4466 * zc_cookie file descriptor to send stream to
4467 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4468 * zc_sendobj objsetid of snapshot to send
4469 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4470 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4471 * output size in zc_objset_type.
4472 * zc_flags lzc_send_flags
4474 * outputs:
4475 * zc_objset_type estimated size, if zc_guid is set
4477 static int
4478 zfs_ioc_send(zfs_cmd_t *zc)
4480 int error;
4481 offset_t off;
4482 boolean_t estimate = (zc->zc_guid != 0);
4483 boolean_t embedok = (zc->zc_flags & 0x1);
4484 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4485 boolean_t compressok = (zc->zc_flags & 0x4);
4487 if (zc->zc_obj != 0) {
4488 dsl_pool_t *dp;
4489 dsl_dataset_t *tosnap;
4491 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4492 if (error != 0)
4493 return (error);
4495 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4496 if (error != 0) {
4497 dsl_pool_rele(dp, FTAG);
4498 return (error);
4501 if (dsl_dir_is_clone(tosnap->ds_dir))
4502 zc->zc_fromobj =
4503 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4504 dsl_dataset_rele(tosnap, FTAG);
4505 dsl_pool_rele(dp, FTAG);
4508 if (estimate) {
4509 dsl_pool_t *dp;
4510 dsl_dataset_t *tosnap;
4511 dsl_dataset_t *fromsnap = NULL;
4513 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4514 if (error != 0)
4515 return (error);
4517 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4518 if (error != 0) {
4519 dsl_pool_rele(dp, FTAG);
4520 return (error);
4523 if (zc->zc_fromobj != 0) {
4524 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4525 FTAG, &fromsnap);
4526 if (error != 0) {
4527 dsl_dataset_rele(tosnap, FTAG);
4528 dsl_pool_rele(dp, FTAG);
4529 return (error);
4533 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4534 &zc->zc_objset_type);
4536 if (fromsnap != NULL)
4537 dsl_dataset_rele(fromsnap, FTAG);
4538 dsl_dataset_rele(tosnap, FTAG);
4539 dsl_pool_rele(dp, FTAG);
4540 } else {
4541 file_t *fp = getf(zc->zc_cookie);
4542 if (fp == NULL)
4543 return (SET_ERROR(EBADF));
4545 off = fp->f_offset;
4546 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4547 zc->zc_fromobj, embedok, large_block_ok, compressok,
4548 zc->zc_cookie, fp->f_vnode, &off);
4550 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4551 fp->f_offset = off;
4552 releasef(zc->zc_cookie);
4554 return (error);
4558 * inputs:
4559 * zc_name name of snapshot on which to report progress
4560 * zc_cookie file descriptor of send stream
4562 * outputs:
4563 * zc_cookie number of bytes written in send stream thus far
4565 static int
4566 zfs_ioc_send_progress(zfs_cmd_t *zc)
4568 dsl_pool_t *dp;
4569 dsl_dataset_t *ds;
4570 dmu_sendarg_t *dsp = NULL;
4571 int error;
4573 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4574 if (error != 0)
4575 return (error);
4577 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4578 if (error != 0) {
4579 dsl_pool_rele(dp, FTAG);
4580 return (error);
4583 mutex_enter(&ds->ds_sendstream_lock);
4586 * Iterate over all the send streams currently active on this dataset.
4587 * If there's one which matches the specified file descriptor _and_ the
4588 * stream was started by the current process, return the progress of
4589 * that stream.
4591 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4592 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4593 if (dsp->dsa_outfd == zc->zc_cookie &&
4594 dsp->dsa_proc == curproc)
4595 break;
4598 if (dsp != NULL)
4599 zc->zc_cookie = *(dsp->dsa_off);
4600 else
4601 error = SET_ERROR(ENOENT);
4603 mutex_exit(&ds->ds_sendstream_lock);
4604 dsl_dataset_rele(ds, FTAG);
4605 dsl_pool_rele(dp, FTAG);
4606 return (error);
4609 static int
4610 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4612 int id, error;
4614 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4615 &zc->zc_inject_record);
4617 if (error == 0)
4618 zc->zc_guid = (uint64_t)id;
4620 return (error);
4623 static int
4624 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4626 return (zio_clear_fault((int)zc->zc_guid));
4629 static int
4630 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4632 int id = (int)zc->zc_guid;
4633 int error;
4635 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4636 &zc->zc_inject_record);
4638 zc->zc_guid = id;
4640 return (error);
4643 static int
4644 zfs_ioc_error_log(zfs_cmd_t *zc)
4646 spa_t *spa;
4647 int error;
4648 size_t count = (size_t)zc->zc_nvlist_dst_size;
4650 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4651 return (error);
4653 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4654 &count);
4655 if (error == 0)
4656 zc->zc_nvlist_dst_size = count;
4657 else
4658 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4660 spa_close(spa, FTAG);
4662 return (error);
4665 static int
4666 zfs_ioc_clear(zfs_cmd_t *zc)
4668 spa_t *spa;
4669 vdev_t *vd;
4670 int error;
4673 * On zpool clear we also fix up missing slogs
4675 mutex_enter(&spa_namespace_lock);
4676 spa = spa_lookup(zc->zc_name);
4677 if (spa == NULL) {
4678 mutex_exit(&spa_namespace_lock);
4679 return (SET_ERROR(EIO));
4681 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4682 /* we need to let spa_open/spa_load clear the chains */
4683 spa_set_log_state(spa, SPA_LOG_CLEAR);
4685 spa->spa_last_open_failed = 0;
4686 mutex_exit(&spa_namespace_lock);
4688 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4689 error = spa_open(zc->zc_name, &spa, FTAG);
4690 } else {
4691 nvlist_t *policy;
4692 nvlist_t *config = NULL;
4694 if (zc->zc_nvlist_src == (uintptr_t)NULL)
4695 return (SET_ERROR(EINVAL));
4697 if ((error = get_nvlist(zc->zc_nvlist_src,
4698 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4699 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4700 policy, &config);
4701 if (config != NULL) {
4702 int err;
4704 if ((err = put_nvlist(zc, config)) != 0)
4705 error = err;
4706 nvlist_free(config);
4708 nvlist_free(policy);
4712 if (error != 0)
4713 return (error);
4715 spa_vdev_state_enter(spa, SCL_NONE);
4717 if (zc->zc_guid == 0) {
4718 vd = NULL;
4719 } else {
4720 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4721 if (vd == NULL) {
4722 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4723 spa_close(spa, FTAG);
4724 return (SET_ERROR(ENODEV));
4728 vdev_clear(spa, vd);
4730 (void) spa_vdev_state_exit(spa, NULL, 0);
4733 * Resume any suspended I/Os.
4735 if (zio_resume(spa) != 0)
4736 error = SET_ERROR(EIO);
4738 spa_close(spa, FTAG);
4740 return (error);
4743 static int
4744 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4746 spa_t *spa;
4747 int error;
4749 error = spa_open(zc->zc_name, &spa, FTAG);
4750 if (error != 0)
4751 return (error);
4753 spa_vdev_state_enter(spa, SCL_NONE);
4756 * If a resilver is already in progress then set the
4757 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4758 * the scan as a side effect of the reopen. Otherwise, let
4759 * vdev_open() decided if a resilver is required.
4761 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4762 vdev_reopen(spa->spa_root_vdev);
4763 spa->spa_scrub_reopen = B_FALSE;
4765 (void) spa_vdev_state_exit(spa, NULL, 0);
4766 spa_close(spa, FTAG);
4767 return (0);
4770 * inputs:
4771 * zc_name name of filesystem
4773 * outputs:
4774 * zc_string name of conflicting snapshot, if there is one
4776 static int
4777 zfs_ioc_promote(zfs_cmd_t *zc)
4779 dsl_pool_t *dp;
4780 dsl_dataset_t *ds, *ods;
4781 char origin[ZFS_MAX_DATASET_NAME_LEN];
4782 char *cp;
4783 int error;
4785 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4786 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4787 strchr(zc->zc_name, '%'))
4788 return (SET_ERROR(EINVAL));
4790 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4791 if (error != 0)
4792 return (error);
4794 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4795 if (error != 0) {
4796 dsl_pool_rele(dp, FTAG);
4797 return (error);
4800 if (!dsl_dir_is_clone(ds->ds_dir)) {
4801 dsl_dataset_rele(ds, FTAG);
4802 dsl_pool_rele(dp, FTAG);
4803 return (SET_ERROR(EINVAL));
4806 error = dsl_dataset_hold_obj(dp,
4807 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4808 if (error != 0) {
4809 dsl_dataset_rele(ds, FTAG);
4810 dsl_pool_rele(dp, FTAG);
4811 return (error);
4814 dsl_dataset_name(ods, origin);
4815 dsl_dataset_rele(ods, FTAG);
4816 dsl_dataset_rele(ds, FTAG);
4817 dsl_pool_rele(dp, FTAG);
4820 * We don't need to unmount *all* the origin fs's snapshots, but
4821 * it's easier.
4823 cp = strchr(origin, '@');
4824 if (cp)
4825 *cp = '\0';
4826 (void) dmu_objset_find(origin,
4827 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4828 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4832 * Retrieve a single {user|group}{used|quota}@... property.
4834 * inputs:
4835 * zc_name name of filesystem
4836 * zc_objset_type zfs_userquota_prop_t
4837 * zc_value domain name (eg. "S-1-234-567-89")
4838 * zc_guid RID/UID/GID
4840 * outputs:
4841 * zc_cookie property value
4843 static int
4844 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4846 zfsvfs_t *zfsvfs;
4847 int error;
4849 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4850 return (SET_ERROR(EINVAL));
4852 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4853 if (error != 0)
4854 return (error);
4856 error = zfs_userspace_one(zfsvfs,
4857 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4858 zfsvfs_rele(zfsvfs, FTAG);
4860 return (error);
4864 * inputs:
4865 * zc_name name of filesystem
4866 * zc_cookie zap cursor
4867 * zc_objset_type zfs_userquota_prop_t
4868 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4870 * outputs:
4871 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4872 * zc_cookie zap cursor
4874 static int
4875 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4877 zfsvfs_t *zfsvfs;
4878 int bufsize = zc->zc_nvlist_dst_size;
4880 if (bufsize <= 0)
4881 return (SET_ERROR(ENOMEM));
4883 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4884 if (error != 0)
4885 return (error);
4887 void *buf = kmem_alloc(bufsize, KM_SLEEP);
4889 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4890 buf, &zc->zc_nvlist_dst_size);
4892 if (error == 0) {
4893 error = xcopyout(buf,
4894 (void *)(uintptr_t)zc->zc_nvlist_dst,
4895 zc->zc_nvlist_dst_size);
4897 kmem_free(buf, bufsize);
4898 zfsvfs_rele(zfsvfs, FTAG);
4900 return (error);
4904 * inputs:
4905 * zc_name name of filesystem
4907 * outputs:
4908 * none
4910 static int
4911 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4913 objset_t *os;
4914 int error = 0;
4915 zfsvfs_t *zfsvfs;
4917 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4918 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4920 * If userused is not enabled, it may be because the
4921 * objset needs to be closed & reopened (to grow the
4922 * objset_phys_t). Suspend/resume the fs will do that.
4924 dsl_dataset_t *ds, *newds;
4926 ds = dmu_objset_ds(zfsvfs->z_os);
4927 error = zfs_suspend_fs(zfsvfs);
4928 if (error == 0) {
4929 dmu_objset_refresh_ownership(ds, &newds,
4930 zfsvfs);
4931 error = zfs_resume_fs(zfsvfs, newds);
4934 if (error == 0)
4935 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4936 VFS_RELE(zfsvfs->z_vfs);
4937 } else {
4938 /* XXX kind of reading contents without owning */
4939 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4940 if (error != 0)
4941 return (error);
4943 error = dmu_objset_userspace_upgrade(os);
4944 dmu_objset_rele(os, FTAG);
4947 return (error);
4951 * We don't want to have a hard dependency
4952 * against some special symbols in sharefs
4953 * nfs, and smbsrv. Determine them if needed when
4954 * the first file system is shared.
4955 * Neither sharefs, nfs or smbsrv are unloadable modules.
4957 int (*znfsexport_fs)(void *arg);
4958 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4959 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4961 int zfs_nfsshare_inited;
4962 int zfs_smbshare_inited;
4964 ddi_modhandle_t nfs_mod;
4965 ddi_modhandle_t sharefs_mod;
4966 ddi_modhandle_t smbsrv_mod;
4967 kmutex_t zfs_share_lock;
4969 static int
4970 zfs_init_sharefs()
4972 int error;
4974 ASSERT(MUTEX_HELD(&zfs_share_lock));
4975 /* Both NFS and SMB shares also require sharetab support. */
4976 if (sharefs_mod == NULL && ((sharefs_mod =
4977 ddi_modopen("fs/sharefs",
4978 KRTLD_MODE_FIRST, &error)) == NULL)) {
4979 return (SET_ERROR(ENOSYS));
4981 if (zshare_fs == NULL && ((zshare_fs =
4982 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4983 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4984 return (SET_ERROR(ENOSYS));
4986 return (0);
4989 static int
4990 zfs_ioc_share(zfs_cmd_t *zc)
4992 int error;
4993 int opcode;
4995 switch (zc->zc_share.z_sharetype) {
4996 case ZFS_SHARE_NFS:
4997 case ZFS_UNSHARE_NFS:
4998 if (zfs_nfsshare_inited == 0) {
4999 mutex_enter(&zfs_share_lock);
5000 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5001 KRTLD_MODE_FIRST, &error)) == NULL)) {
5002 mutex_exit(&zfs_share_lock);
5003 return (SET_ERROR(ENOSYS));
5005 if (znfsexport_fs == NULL &&
5006 ((znfsexport_fs = (int (*)(void *))
5007 ddi_modsym(nfs_mod,
5008 "nfs_export", &error)) == NULL)) {
5009 mutex_exit(&zfs_share_lock);
5010 return (SET_ERROR(ENOSYS));
5012 error = zfs_init_sharefs();
5013 if (error != 0) {
5014 mutex_exit(&zfs_share_lock);
5015 return (SET_ERROR(ENOSYS));
5017 zfs_nfsshare_inited = 1;
5018 mutex_exit(&zfs_share_lock);
5020 break;
5021 case ZFS_SHARE_SMB:
5022 case ZFS_UNSHARE_SMB:
5023 if (zfs_smbshare_inited == 0) {
5024 mutex_enter(&zfs_share_lock);
5025 if (smbsrv_mod == NULL && ((smbsrv_mod =
5026 ddi_modopen("drv/smbsrv",
5027 KRTLD_MODE_FIRST, &error)) == NULL)) {
5028 mutex_exit(&zfs_share_lock);
5029 return (SET_ERROR(ENOSYS));
5031 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5032 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5033 "smb_server_share", &error)) == NULL)) {
5034 mutex_exit(&zfs_share_lock);
5035 return (SET_ERROR(ENOSYS));
5037 error = zfs_init_sharefs();
5038 if (error != 0) {
5039 mutex_exit(&zfs_share_lock);
5040 return (SET_ERROR(ENOSYS));
5042 zfs_smbshare_inited = 1;
5043 mutex_exit(&zfs_share_lock);
5045 break;
5046 default:
5047 return (SET_ERROR(EINVAL));
5050 switch (zc->zc_share.z_sharetype) {
5051 case ZFS_SHARE_NFS:
5052 case ZFS_UNSHARE_NFS:
5053 if (error =
5054 znfsexport_fs((void *)
5055 (uintptr_t)zc->zc_share.z_exportdata))
5056 return (error);
5057 break;
5058 case ZFS_SHARE_SMB:
5059 case ZFS_UNSHARE_SMB:
5060 if (error = zsmbexport_fs((void *)
5061 (uintptr_t)zc->zc_share.z_exportdata,
5062 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5063 B_TRUE: B_FALSE)) {
5064 return (error);
5066 break;
5069 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5070 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5071 SHAREFS_ADD : SHAREFS_REMOVE;
5074 * Add or remove share from sharetab
5076 error = zshare_fs(opcode,
5077 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5078 zc->zc_share.z_sharemax);
5080 return (error);
5084 ace_t full_access[] = {
5085 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5089 * inputs:
5090 * zc_name name of containing filesystem
5091 * zc_obj object # beyond which we want next in-use object #
5093 * outputs:
5094 * zc_obj next in-use object #
5096 static int
5097 zfs_ioc_next_obj(zfs_cmd_t *zc)
5099 objset_t *os = NULL;
5100 int error;
5102 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5103 if (error != 0)
5104 return (error);
5106 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
5107 dsl_dataset_phys(os->os_dsl_dataset)->ds_prev_snap_txg);
5109 dmu_objset_rele(os, FTAG);
5110 return (error);
5114 * inputs:
5115 * zc_name name of filesystem
5116 * zc_value prefix name for snapshot
5117 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5119 * outputs:
5120 * zc_value short name of new snapshot
5122 static int
5123 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5125 char *snap_name;
5126 char *hold_name;
5127 int error;
5128 minor_t minor;
5130 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5131 if (error != 0)
5132 return (error);
5134 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5135 (u_longlong_t)ddi_get_lbolt64());
5136 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5138 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5139 hold_name);
5140 if (error == 0)
5141 (void) strcpy(zc->zc_value, snap_name);
5142 strfree(snap_name);
5143 strfree(hold_name);
5144 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5145 return (error);
5149 * inputs:
5150 * zc_name name of "to" snapshot
5151 * zc_value name of "from" snapshot
5152 * zc_cookie file descriptor to write diff data on
5154 * outputs:
5155 * dmu_diff_record_t's to the file descriptor
5157 static int
5158 zfs_ioc_diff(zfs_cmd_t *zc)
5160 file_t *fp;
5161 offset_t off;
5162 int error;
5164 fp = getf(zc->zc_cookie);
5165 if (fp == NULL)
5166 return (SET_ERROR(EBADF));
5168 off = fp->f_offset;
5170 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5172 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5173 fp->f_offset = off;
5174 releasef(zc->zc_cookie);
5176 return (error);
5180 * Remove all ACL files in shares dir
5182 static int
5183 zfs_smb_acl_purge(znode_t *dzp)
5185 zap_cursor_t zc;
5186 zap_attribute_t zap;
5187 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5188 int error;
5190 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5191 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5192 zap_cursor_advance(&zc)) {
5193 if ((error = fop_remove(ZTOV(dzp), zap.za_name, kcred,
5194 NULL, 0)) != 0)
5195 break;
5197 zap_cursor_fini(&zc);
5198 return (error);
5201 static int
5202 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5204 vnode_t *vp;
5205 znode_t *dzp;
5206 vnode_t *resourcevp = NULL;
5207 znode_t *sharedir;
5208 zfsvfs_t *zfsvfs;
5209 nvlist_t *nvlist;
5210 char *src, *target;
5211 vattr_t vattr;
5212 vsecattr_t vsec;
5213 int error = 0;
5215 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5216 NO_FOLLOW, NULL, &vp)) != 0)
5217 return (error);
5219 /* Now make sure mntpnt and dataset are ZFS */
5221 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5222 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5223 zc->zc_name) != 0)) {
5224 VN_RELE(vp);
5225 return (SET_ERROR(EINVAL));
5228 dzp = VTOZ(vp);
5229 zfsvfs = dzp->z_zfsvfs;
5230 ZFS_ENTER(zfsvfs);
5233 * Create share dir if its missing.
5235 mutex_enter(&zfsvfs->z_lock);
5236 if (zfsvfs->z_shares_dir == 0) {
5237 dmu_tx_t *tx;
5239 tx = dmu_tx_create(zfsvfs->z_os);
5240 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5241 ZFS_SHARES_DIR);
5242 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5243 error = dmu_tx_assign(tx, TXG_WAIT);
5244 if (error != 0) {
5245 dmu_tx_abort(tx);
5246 } else {
5247 error = zfs_create_share_dir(zfsvfs, tx);
5248 dmu_tx_commit(tx);
5250 if (error != 0) {
5251 mutex_exit(&zfsvfs->z_lock);
5252 VN_RELE(vp);
5253 ZFS_EXIT(zfsvfs);
5254 return (error);
5257 mutex_exit(&zfsvfs->z_lock);
5259 ASSERT(zfsvfs->z_shares_dir);
5260 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5261 VN_RELE(vp);
5262 ZFS_EXIT(zfsvfs);
5263 return (error);
5266 switch (zc->zc_cookie) {
5267 case ZFS_SMB_ACL_ADD:
5268 vattr.va_mask = VATTR_MODE|VATTR_UID|VATTR_GID|VATTR_TYPE;
5269 vattr.va_type = VREG;
5270 vattr.va_mode = S_IFREG|0777;
5271 vattr.va_uid = 0;
5272 vattr.va_gid = 0;
5274 vsec.vsa_mask = VSA_ACE;
5275 vsec.vsa_aclentp = &full_access;
5276 vsec.vsa_aclentsz = sizeof (full_access);
5277 vsec.vsa_aclcnt = 1;
5279 error = fop_create(ZTOV(sharedir), zc->zc_string,
5280 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5281 if (resourcevp)
5282 VN_RELE(resourcevp);
5283 break;
5285 case ZFS_SMB_ACL_REMOVE:
5286 error = fop_remove(ZTOV(sharedir), zc->zc_string, kcred,
5287 NULL, 0);
5288 break;
5290 case ZFS_SMB_ACL_RENAME:
5291 if ((error = get_nvlist(zc->zc_nvlist_src,
5292 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5293 VN_RELE(vp);
5294 VN_RELE(ZTOV(sharedir));
5295 ZFS_EXIT(zfsvfs);
5296 return (error);
5298 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5299 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5300 &target)) {
5301 VN_RELE(vp);
5302 VN_RELE(ZTOV(sharedir));
5303 ZFS_EXIT(zfsvfs);
5304 nvlist_free(nvlist);
5305 return (error);
5307 error = fop_rename(ZTOV(sharedir), src, ZTOV(sharedir), target,
5308 kcred, NULL, 0);
5309 nvlist_free(nvlist);
5310 break;
5312 case ZFS_SMB_ACL_PURGE:
5313 error = zfs_smb_acl_purge(sharedir);
5314 break;
5316 default:
5317 error = SET_ERROR(EINVAL);
5318 break;
5321 VN_RELE(vp);
5322 VN_RELE(ZTOV(sharedir));
5324 ZFS_EXIT(zfsvfs);
5326 return (error);
5330 * innvl: {
5331 * "holds" -> { snapname -> holdname (string), ... }
5332 * (optional) "cleanup_fd" -> fd (int32)
5335 * outnvl: {
5336 * snapname -> error value (int32)
5337 * ...
5340 /* ARGSUSED */
5341 static int
5342 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5344 nvpair_t *pair;
5345 nvlist_t *holds;
5346 int cleanup_fd = -1;
5347 int error;
5348 minor_t minor = 0;
5350 error = nvlist_lookup_nvlist(args, "holds", &holds);
5351 if (error != 0)
5352 return (SET_ERROR(EINVAL));
5354 /* make sure the user didn't pass us any invalid (empty) tags */
5355 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5356 pair = nvlist_next_nvpair(holds, pair)) {
5357 char *htag;
5359 error = nvpair_value_string(pair, &htag);
5360 if (error != 0)
5361 return (SET_ERROR(error));
5363 if (strlen(htag) == 0)
5364 return (SET_ERROR(EINVAL));
5367 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5368 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5369 if (error != 0)
5370 return (error);
5373 error = dsl_dataset_user_hold(holds, minor, errlist);
5374 if (minor != 0)
5375 zfs_onexit_fd_rele(cleanup_fd);
5376 return (error);
5380 * innvl is not used.
5382 * outnvl: {
5383 * holdname -> time added (uint64 seconds since epoch)
5384 * ...
5387 /* ARGSUSED */
5388 static int
5389 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5391 return (dsl_dataset_get_holds(snapname, outnvl));
5395 * innvl: {
5396 * snapname -> { holdname, ... }
5397 * ...
5400 * outnvl: {
5401 * snapname -> error value (int32)
5402 * ...
5405 /* ARGSUSED */
5406 static int
5407 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5409 return (dsl_dataset_user_release(holds, errlist));
5413 * inputs:
5414 * zc_name name of new filesystem or snapshot
5415 * zc_value full name of old snapshot
5417 * outputs:
5418 * zc_cookie space in bytes
5419 * zc_objset_type compressed space in bytes
5420 * zc_perm_action uncompressed space in bytes
5422 static int
5423 zfs_ioc_space_written(zfs_cmd_t *zc)
5425 int error;
5426 dsl_pool_t *dp;
5427 dsl_dataset_t *new, *old;
5429 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5430 if (error != 0)
5431 return (error);
5432 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5433 if (error != 0) {
5434 dsl_pool_rele(dp, FTAG);
5435 return (error);
5437 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5438 if (error != 0) {
5439 dsl_dataset_rele(new, FTAG);
5440 dsl_pool_rele(dp, FTAG);
5441 return (error);
5444 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5445 &zc->zc_objset_type, &zc->zc_perm_action);
5446 dsl_dataset_rele(old, FTAG);
5447 dsl_dataset_rele(new, FTAG);
5448 dsl_pool_rele(dp, FTAG);
5449 return (error);
5453 * innvl: {
5454 * "firstsnap" -> snapshot name
5457 * outnvl: {
5458 * "used" -> space in bytes
5459 * "compressed" -> compressed space in bytes
5460 * "uncompressed" -> uncompressed space in bytes
5463 static int
5464 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5466 int error;
5467 dsl_pool_t *dp;
5468 dsl_dataset_t *new, *old;
5469 char *firstsnap;
5470 uint64_t used, comp, uncomp;
5472 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5473 return (SET_ERROR(EINVAL));
5475 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5476 if (error != 0)
5477 return (error);
5479 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5480 if (error == 0 && !new->ds_is_snapshot) {
5481 dsl_dataset_rele(new, FTAG);
5482 error = SET_ERROR(EINVAL);
5484 if (error != 0) {
5485 dsl_pool_rele(dp, FTAG);
5486 return (error);
5488 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5489 if (error == 0 && !old->ds_is_snapshot) {
5490 dsl_dataset_rele(old, FTAG);
5491 error = SET_ERROR(EINVAL);
5493 if (error != 0) {
5494 dsl_dataset_rele(new, FTAG);
5495 dsl_pool_rele(dp, FTAG);
5496 return (error);
5499 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5500 dsl_dataset_rele(old, FTAG);
5501 dsl_dataset_rele(new, FTAG);
5502 dsl_pool_rele(dp, FTAG);
5503 fnvlist_add_uint64(outnvl, "used", used);
5504 fnvlist_add_uint64(outnvl, "compressed", comp);
5505 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5506 return (error);
5510 * innvl: {
5511 * "fd" -> file descriptor to write stream to (int32)
5512 * (optional) "fromsnap" -> full snap name to send an incremental from
5513 * (optional) "largeblockok" -> (value ignored)
5514 * indicates that blocks > 128KB are permitted
5515 * (optional) "embedok" -> (value ignored)
5516 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5517 * (optional) "compressok" -> (value ignored)
5518 * presence indicates compressed DRR_WRITE records are permitted
5519 * (optional) "resume_object" and "resume_offset" -> (uint64)
5520 * if present, resume send stream from specified object and offset.
5523 * outnvl is unused
5525 /* ARGSUSED */
5526 static int
5527 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5529 int error;
5530 offset_t off;
5531 char *fromname = NULL;
5532 int fd;
5533 boolean_t largeblockok;
5534 boolean_t embedok;
5535 boolean_t compressok;
5536 uint64_t resumeobj = 0;
5537 uint64_t resumeoff = 0;
5539 error = nvlist_lookup_int32(innvl, "fd", &fd);
5540 if (error != 0)
5541 return (SET_ERROR(EINVAL));
5543 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5545 largeblockok = nvlist_exists(innvl, "largeblockok");
5546 embedok = nvlist_exists(innvl, "embedok");
5547 compressok = nvlist_exists(innvl, "compressok");
5549 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5550 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5552 file_t *fp = getf(fd);
5553 if (fp == NULL)
5554 return (SET_ERROR(EBADF));
5556 off = fp->f_offset;
5557 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5558 fd, resumeobj, resumeoff, fp->f_vnode, &off);
5560 if (fop_seek(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5561 fp->f_offset = off;
5562 releasef(fd);
5563 return (error);
5567 * Determine approximately how large a zfs send stream will be -- the number
5568 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5570 * innvl: {
5571 * (optional) "from" -> full snap or bookmark name to send an incremental
5572 * from
5573 * (optional) "largeblockok" -> (value ignored)
5574 * indicates that blocks > 128KB are permitted
5575 * (optional) "embedok" -> (value ignored)
5576 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5577 * (optional) "compressok" -> (value ignored)
5578 * presence indicates compressed DRR_WRITE records are permitted
5581 * outnvl: {
5582 * "space" -> bytes of space (uint64)
5585 static int
5586 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5588 dsl_pool_t *dp;
5589 dsl_dataset_t *tosnap;
5590 int error;
5591 char *fromname;
5592 boolean_t compressok;
5593 uint64_t space;
5595 error = dsl_pool_hold(snapname, FTAG, &dp);
5596 if (error != 0)
5597 return (error);
5599 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
5600 if (error != 0) {
5601 dsl_pool_rele(dp, FTAG);
5602 return (error);
5605 compressok = nvlist_exists(innvl, "compressok");
5607 error = nvlist_lookup_string(innvl, "from", &fromname);
5608 if (error == 0) {
5609 if (strchr(fromname, '@') != NULL) {
5611 * If from is a snapshot, hold it and use the more
5612 * efficient dmu_send_estimate to estimate send space
5613 * size using deadlists.
5615 dsl_dataset_t *fromsnap;
5616 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
5617 if (error != 0)
5618 goto out;
5619 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5620 &space);
5621 dsl_dataset_rele(fromsnap, FTAG);
5622 } else if (strchr(fromname, '#') != NULL) {
5624 * If from is a bookmark, fetch the creation TXG of the
5625 * snapshot it was created from and use that to find
5626 * blocks that were born after it.
5628 zfs_bookmark_phys_t frombm;
5630 error = dsl_bookmark_lookup(dp, fromname, tosnap,
5631 &frombm);
5632 if (error != 0)
5633 goto out;
5634 error = dmu_send_estimate_from_txg(tosnap,
5635 frombm.zbm_creation_txg, compressok, &space);
5636 } else {
5638 * from is not properly formatted as a snapshot or
5639 * bookmark
5641 error = SET_ERROR(EINVAL);
5642 goto out;
5644 } else {
5646 * If estimating the size of a full send, use dmu_send_estimate.
5648 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5651 fnvlist_add_uint64(outnvl, "space", space);
5653 out:
5654 dsl_dataset_rele(tosnap, FTAG);
5655 dsl_pool_rele(dp, FTAG);
5656 return (error);
5659 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5661 static void
5662 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5663 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5664 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5666 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5668 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5669 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5670 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5671 ASSERT3P(vec->zvec_func, ==, NULL);
5673 vec->zvec_legacy_func = func;
5674 vec->zvec_secpolicy = secpolicy;
5675 vec->zvec_namecheck = namecheck;
5676 vec->zvec_allow_log = log_history;
5677 vec->zvec_pool_check = pool_check;
5681 * See the block comment at the beginning of this file for details on
5682 * each argument to this function.
5684 static void
5685 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
5686 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5687 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
5688 boolean_t allow_log)
5690 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5692 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5693 ASSERT3U(ioc, <, ZFS_IOC_LAST);
5694 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5695 ASSERT3P(vec->zvec_func, ==, NULL);
5697 /* if we are logging, the name must be valid */
5698 ASSERT(!allow_log || namecheck != NO_NAME);
5700 vec->zvec_name = name;
5701 vec->zvec_func = func;
5702 vec->zvec_secpolicy = secpolicy;
5703 vec->zvec_namecheck = namecheck;
5704 vec->zvec_pool_check = pool_check;
5705 vec->zvec_smush_outnvlist = smush_outnvlist;
5706 vec->zvec_allow_log = allow_log;
5709 static void
5710 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5711 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
5712 zfs_ioc_poolcheck_t pool_check)
5714 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5715 POOL_NAME, log_history, pool_check);
5718 static void
5719 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5720 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
5722 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5723 DATASET_NAME, B_FALSE, pool_check);
5726 static void
5727 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5729 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
5730 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5733 static void
5734 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5735 zfs_secpolicy_func_t *secpolicy)
5737 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5738 NO_NAME, B_FALSE, POOL_CHECK_NONE);
5741 static void
5742 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
5743 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
5745 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5746 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5749 static void
5750 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5752 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5753 zfs_secpolicy_read);
5756 static void
5757 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5758 zfs_secpolicy_func_t *secpolicy)
5760 zfs_ioctl_register_legacy(ioc, func, secpolicy,
5761 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5764 static void
5765 zfs_ioctl_init(void)
5767 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5768 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5769 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5771 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5772 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5773 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5775 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5776 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5777 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5779 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5780 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5781 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5783 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5784 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5785 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5787 zfs_ioctl_register("create", ZFS_IOC_CREATE,
5788 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5789 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5791 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5792 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5793 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5795 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
5796 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
5797 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5799 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5800 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5801 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5803 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5804 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5805 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5806 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5807 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5808 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5810 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5811 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5812 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5814 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5815 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5816 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5818 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5819 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5820 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5822 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5823 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5824 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5826 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5827 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5828 POOL_NAME,
5829 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5831 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5832 zfs_ioc_channel_program, zfs_secpolicy_config,
5833 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5834 B_TRUE);
5836 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
5837 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
5838 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5840 zfs_ioctl_register("zpool_discard_checkpoint",
5841 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
5842 zfs_secpolicy_config, POOL_NAME,
5843 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5845 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
5846 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
5847 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5849 /* IOCTLS that use the legacy function signature */
5851 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5852 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5854 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5855 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5856 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5857 zfs_ioc_pool_scan);
5858 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5859 zfs_ioc_pool_upgrade);
5860 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5861 zfs_ioc_vdev_add);
5862 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5863 zfs_ioc_vdev_remove);
5864 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5865 zfs_ioc_vdev_set_state);
5866 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5867 zfs_ioc_vdev_attach);
5868 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5869 zfs_ioc_vdev_detach);
5870 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5871 zfs_ioc_vdev_setpath);
5872 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5873 zfs_ioc_vdev_setfru);
5874 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5875 zfs_ioc_pool_set_props);
5876 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5877 zfs_ioc_vdev_split);
5878 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5879 zfs_ioc_pool_reguid);
5881 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5882 zfs_ioc_pool_configs, zfs_secpolicy_none);
5883 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5884 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5885 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5886 zfs_ioc_inject_fault, zfs_secpolicy_inject);
5887 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5888 zfs_ioc_clear_fault, zfs_secpolicy_inject);
5889 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
5890 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
5893 * pool destroy, and export don't log the history as part of
5894 * zfsdev_ioctl, but rather zfs_ioc_pool_export
5895 * does the logging of those commands.
5897 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
5898 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5899 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
5900 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5902 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5903 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5904 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5905 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5907 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5908 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5909 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5910 zfs_ioc_dsobj_to_dsname,
5911 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5912 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5913 zfs_ioc_pool_get_history,
5914 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5916 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5917 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5919 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5920 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
5921 zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5922 zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5924 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5925 zfs_ioc_space_written);
5926 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5927 zfs_ioc_objset_recvd_props);
5928 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5929 zfs_ioc_next_obj);
5930 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5931 zfs_ioc_get_fsacl);
5932 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5933 zfs_ioc_objset_stats);
5934 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5935 zfs_ioc_objset_zplprops);
5936 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5937 zfs_ioc_dataset_list_next);
5938 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5939 zfs_ioc_snapshot_list_next);
5940 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5941 zfs_ioc_send_progress);
5943 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
5944 zfs_ioc_diff, zfs_secpolicy_diff);
5945 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
5946 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
5947 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
5948 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
5949 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
5950 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
5951 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
5952 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
5953 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
5954 zfs_ioc_send, zfs_secpolicy_send);
5956 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
5957 zfs_secpolicy_none);
5958 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
5959 zfs_secpolicy_destroy);
5960 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
5961 zfs_secpolicy_rename);
5962 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
5963 zfs_secpolicy_recv);
5964 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
5965 zfs_secpolicy_promote);
5966 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
5967 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
5968 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
5969 zfs_secpolicy_set_fsacl);
5971 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
5972 zfs_secpolicy_share, POOL_CHECK_NONE);
5973 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
5974 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
5975 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
5976 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
5977 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5978 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
5979 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
5980 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5984 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5985 zfs_ioc_poolcheck_t check)
5987 spa_t *spa;
5988 int error;
5990 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5992 if (check & POOL_CHECK_NONE)
5993 return (0);
5995 error = spa_open(name, &spa, FTAG);
5996 if (error == 0) {
5997 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5998 error = SET_ERROR(EAGAIN);
5999 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6000 error = SET_ERROR(EROFS);
6001 spa_close(spa, FTAG);
6003 return (error);
6007 * Find a free minor number.
6009 minor_t
6010 zfsdev_minor_alloc(void)
6012 static minor_t last_minor;
6013 minor_t m;
6015 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6017 for (m = last_minor + 1; m != last_minor; m++) {
6018 if (m > ZFSDEV_MAX_MINOR)
6019 m = 1;
6020 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6021 last_minor = m;
6022 return (m);
6026 return (0);
6029 static int
6030 zfs_ctldev_init(dev_t *devp)
6032 minor_t minor;
6033 zfs_soft_state_t *zs;
6035 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6036 ASSERT(getminor(*devp) == 0);
6038 minor = zfsdev_minor_alloc();
6039 if (minor == 0)
6040 return (SET_ERROR(ENXIO));
6042 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6043 return (SET_ERROR(EAGAIN));
6045 *devp = makedevice(getemajor(*devp), minor);
6047 zs = ddi_get_soft_state(zfsdev_state, minor);
6048 zs->zss_type = ZSST_CTLDEV;
6049 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6051 return (0);
6054 static void
6055 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6057 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6059 zfs_onexit_destroy(zo);
6060 ddi_soft_state_free(zfsdev_state, minor);
6063 void *
6064 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6066 zfs_soft_state_t *zp;
6068 zp = ddi_get_soft_state(zfsdev_state, minor);
6069 if (zp == NULL || zp->zss_type != which)
6070 return (NULL);
6072 return (zp->zss_data);
6075 static int
6076 zfsdev_open(dev_t *devp, int flag, int otyp, cred_t *cr)
6078 int error = 0;
6080 if (getminor(*devp) != 0)
6081 return (zvol_open(devp, flag, otyp, cr));
6083 /* This is the control device. Allocate a new minor if requested. */
6084 if (flag & FEXCL) {
6085 mutex_enter(&zfsdev_state_lock);
6086 error = zfs_ctldev_init(devp);
6087 mutex_exit(&zfsdev_state_lock);
6090 return (error);
6093 static int
6094 zfsdev_close(dev_t dev, int flag, int otyp, cred_t *cr)
6096 zfs_onexit_t *zo;
6097 minor_t minor = getminor(dev);
6099 if (minor == 0)
6100 return (0);
6102 mutex_enter(&zfsdev_state_lock);
6103 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6104 if (zo == NULL) {
6105 mutex_exit(&zfsdev_state_lock);
6106 return (zvol_close(dev, flag, otyp, cr));
6108 zfs_ctldev_destroy(zo, minor);
6109 mutex_exit(&zfsdev_state_lock);
6111 return (0);
6114 static int
6115 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
6117 zfs_cmd_t *zc;
6118 uint_t vecnum;
6119 int error, rc, len;
6120 minor_t minor = getminor(dev);
6121 const zfs_ioc_vec_t *vec;
6122 char *saved_poolname = NULL;
6123 nvlist_t *innvl = NULL;
6125 if (minor != 0 &&
6126 zfsdev_get_soft_state(minor, ZSST_CTLDEV) == NULL)
6127 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
6129 vecnum = cmd - ZFS_IOC_FIRST;
6130 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
6132 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6133 return (SET_ERROR(EINVAL));
6134 vec = &zfs_ioc_vec[vecnum];
6136 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6138 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6139 if (error != 0) {
6140 error = SET_ERROR(EFAULT);
6141 goto out;
6144 zc->zc_iflags = flag & FKIOCTL;
6145 if (zc->zc_nvlist_src_size != 0) {
6146 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6147 zc->zc_iflags, &innvl);
6148 if (error != 0)
6149 goto out;
6153 * Ensure that all pool/dataset names are valid before we pass down to
6154 * the lower layers.
6156 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6157 switch (vec->zvec_namecheck) {
6158 case POOL_NAME:
6159 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6160 error = SET_ERROR(EINVAL);
6161 else
6162 error = pool_status_check(zc->zc_name,
6163 vec->zvec_namecheck, vec->zvec_pool_check);
6164 break;
6166 case DATASET_NAME:
6167 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6168 error = SET_ERROR(EINVAL);
6169 else
6170 error = pool_status_check(zc->zc_name,
6171 vec->zvec_namecheck, vec->zvec_pool_check);
6172 break;
6174 case NO_NAME:
6175 break;
6179 if (error == 0)
6180 error = vec->zvec_secpolicy(zc, innvl, cr);
6182 if (error != 0)
6183 goto out;
6185 /* legacy ioctls can modify zc_name */
6186 len = strcspn(zc->zc_name, "/@#") + 1;
6187 saved_poolname = kmem_alloc(len, KM_SLEEP);
6188 (void) strlcpy(saved_poolname, zc->zc_name, len);
6190 if (vec->zvec_func != NULL) {
6191 nvlist_t *outnvl;
6192 int puterror = 0;
6193 spa_t *spa;
6194 nvlist_t *lognv = NULL;
6196 ASSERT(vec->zvec_legacy_func == NULL);
6199 * Add the innvl to the lognv before calling the func,
6200 * in case the func changes the innvl.
6202 if (vec->zvec_allow_log) {
6203 lognv = fnvlist_alloc();
6204 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6205 vec->zvec_name);
6206 if (!nvlist_empty(innvl)) {
6207 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6208 innvl);
6212 outnvl = fnvlist_alloc();
6213 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6216 * Some commands can partially execute, modfiy state, and still
6217 * return an error. In these cases, attempt to record what
6218 * was modified.
6220 if ((error == 0 ||
6221 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6222 vec->zvec_allow_log &&
6223 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6224 if (!nvlist_empty(outnvl)) {
6225 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6226 outnvl);
6228 if (error != 0) {
6229 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6230 error);
6232 (void) spa_history_log_nvl(spa, lognv);
6233 spa_close(spa, FTAG);
6235 fnvlist_free(lognv);
6237 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6238 int smusherror = 0;
6239 if (vec->zvec_smush_outnvlist) {
6240 smusherror = nvlist_smush(outnvl,
6241 zc->zc_nvlist_dst_size);
6243 if (smusherror == 0)
6244 puterror = put_nvlist(zc, outnvl);
6247 if (puterror != 0)
6248 error = puterror;
6250 nvlist_free(outnvl);
6251 } else {
6252 error = vec->zvec_legacy_func(zc);
6255 out:
6256 nvlist_free(innvl);
6257 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6258 if (error == 0 && rc != 0)
6259 error = SET_ERROR(EFAULT);
6260 if (error == 0 && vec->zvec_allow_log) {
6261 char *s = tsd_get(zfs_allow_log_key);
6262 if (s != NULL)
6263 strfree(s);
6264 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6265 } else {
6266 if (saved_poolname != NULL)
6267 strfree(saved_poolname);
6270 kmem_free(zc, sizeof (zfs_cmd_t));
6271 return (error);
6274 static int
6275 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6277 if (cmd != DDI_ATTACH)
6278 return (DDI_FAILURE);
6280 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
6281 DDI_PSEUDO, 0) == DDI_FAILURE)
6282 return (DDI_FAILURE);
6284 zfs_dip = dip;
6286 ddi_report_dev(dip);
6288 return (DDI_SUCCESS);
6291 static int
6292 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6294 if (spa_busy() || zfs_busy() || zvol_busy())
6295 return (DDI_FAILURE);
6297 if (cmd != DDI_DETACH)
6298 return (DDI_FAILURE);
6300 zfs_dip = NULL;
6302 ddi_prop_remove_all(dip);
6303 ddi_remove_minor_node(dip, NULL);
6305 return (DDI_SUCCESS);
6308 /*ARGSUSED*/
6309 static int
6310 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6312 switch (infocmd) {
6313 case DDI_INFO_DEVT2DEVINFO:
6314 *result = zfs_dip;
6315 return (DDI_SUCCESS);
6317 case DDI_INFO_DEVT2INSTANCE:
6318 *result = NULL;
6319 return (DDI_SUCCESS);
6322 return (DDI_FAILURE);
6326 * OK, so this is a little weird.
6328 * /dev/zfs is the control node, i.e. minor 0.
6329 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
6331 * /dev/zfs has basically nothing to do except serve up ioctls,
6332 * so most of the standard driver entry points are in zvol.c.
6334 static struct cb_ops zfs_cb_ops = {
6335 zfsdev_open, /* open */
6336 zfsdev_close, /* close */
6337 zvol_strategy, /* strategy */
6338 nodev, /* print */
6339 zvol_dump, /* dump */
6340 zvol_read, /* read */
6341 zvol_write, /* write */
6342 zfsdev_ioctl, /* ioctl */
6343 nodev, /* devmap */
6344 nodev, /* mmap */
6345 nodev, /* segmap */
6346 nochpoll, /* poll */
6347 ddi_prop_op, /* prop_op */
6348 NULL, /* streamtab */
6349 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
6350 CB_REV, /* version */
6351 nodev, /* async read */
6352 nodev, /* async write */
6355 static struct dev_ops zfs_dev_ops = {
6356 DEVO_REV, /* version */
6357 0, /* refcnt */
6358 zfs_info, /* info */
6359 nulldev, /* identify */
6360 nulldev, /* probe */
6361 zfs_attach, /* attach */
6362 zfs_detach, /* detach */
6363 nodev, /* reset */
6364 &zfs_cb_ops, /* driver operations */
6365 NULL, /* no bus operations */
6366 NULL, /* power */
6367 ddi_quiesce_not_needed, /* quiesce */
6370 static struct modldrv zfs_modldrv = {
6371 &mod_driverops,
6372 "ZFS storage pool",
6373 &zfs_dev_ops
6376 static struct modlinkage modlinkage = {
6377 MODREV_1,
6378 (void *)&zfs_modlfs,
6379 (void *)&zfs_modldrv,
6380 NULL
6383 static void
6384 zfs_allow_log_destroy(void *arg)
6386 char *poolname = arg;
6387 strfree(poolname);
6391 _init(void)
6393 int error;
6395 spa_init(FREAD | FWRITE);
6396 zfs_init();
6397 zvol_init();
6398 zfs_ioctl_init();
6400 if ((error = mod_install(&modlinkage)) != 0) {
6401 zvol_fini();
6402 zfs_fini();
6403 spa_fini();
6404 return (error);
6407 tsd_create(&zfs_fsyncer_key, NULL);
6408 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6409 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6411 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6412 ASSERT(error == 0);
6413 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6415 return (0);
6419 _fini(void)
6421 int error;
6423 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6424 return (SET_ERROR(EBUSY));
6426 if ((error = mod_remove(&modlinkage)) != 0)
6427 return (error);
6429 zvol_fini();
6430 zfs_fini();
6431 spa_fini();
6432 if (zfs_nfsshare_inited)
6433 (void) ddi_modclose(nfs_mod);
6434 if (zfs_smbshare_inited)
6435 (void) ddi_modclose(smbsrv_mod);
6436 if (zfs_nfsshare_inited || zfs_smbshare_inited)
6437 (void) ddi_modclose(sharefs_mod);
6439 tsd_destroy(&zfs_fsyncer_key);
6440 ldi_ident_release(zfs_li);
6441 zfs_li = NULL;
6442 mutex_destroy(&zfs_share_lock);
6444 return (error);
6448 _info(struct modinfo *modinfop)
6450 return (mod_info(&modlinkage, modinfop));