Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / uts / common / fs / zfs / zfs_ioctl.c
blob0b7639a00191561da958a666effb062160841c57
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
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/errno.h>
29 #include <sys/uio.h>
30 #include <sys/buf.h>
31 #include <sys/modctl.h>
32 #include <sys/open.h>
33 #include <sys/file.h>
34 #include <sys/kmem.h>
35 #include <sys/conf.h>
36 #include <sys/cmn_err.h>
37 #include <sys/stat.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/zap.h>
41 #include <sys/spa.h>
42 #include <sys/spa_impl.h>
43 #include <sys/vdev.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/dmu.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/ddi.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
55 #include <sys/zone.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
59 #include <sys/sdt.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_ctldir.h>
62 #include <sys/zfs_dir.h>
63 #include <sys/zvol.h>
64 #include <sharefs/share.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/callb.h>
67 #include <sys/taskq.h>
69 #include "zfs_namecheck.h"
70 #include "zfs_prop.h"
71 #include "zfs_deleg.h"
73 #ifdef __NetBSD__
74 static int zfs_cmajor = -1;
75 static int zfs_bmajor = -1;
76 #define ddi_driver_major(x) zfs_cmajor
77 #endif
79 extern struct modlfs zfs_modlfs;
81 extern void zfs_init(void);
82 extern void zfs_fini(void);
84 ldi_ident_t zfs_li = NULL;
85 dev_info_t *zfs_dip;
87 typedef int zfs_ioc_func_t(zfs_cmd_t *);
88 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
90 typedef struct zfs_ioc_vec {
91 zfs_ioc_func_t *zvec_func;
92 zfs_secpolicy_func_t *zvec_secpolicy;
93 enum {
94 NO_NAME,
95 POOL_NAME,
96 DATASET_NAME
97 } zvec_namecheck;
98 boolean_t zvec_his_log;
99 } zfs_ioc_vec_t;
101 static void clear_props(char *dataset, nvlist_t *props);
102 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
103 boolean_t *);
104 int zfs_set_prop_nvlist(const char *, nvlist_t *);
106 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
107 void
108 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
110 const char *newfile;
111 char buf[256];
112 va_list adx;
115 * Get rid of annoying "../common/" prefix to filename.
117 newfile = strrchr(file, '/');
118 if (newfile != NULL) {
119 newfile = newfile + 1; /* Get rid of leading / */
120 } else {
121 newfile = file;
124 va_start(adx, fmt);
125 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
126 va_end(adx);
129 * To get this data, use the zfs-dprintf probe as so:
130 * dtrace -q -n 'zfs-dprintf \
131 * /stringof(arg0) == "dbuf.c"/ \
132 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
133 * arg0 = file name
134 * arg1 = function name
135 * arg2 = line number
136 * arg3 = message
138 DTRACE_PROBE4(zfs__dprintf,
139 char *, newfile, char *, func, int, line, char *, buf);
142 static void
143 history_str_free(char *buf)
145 kmem_free(buf, HIS_MAX_RECORD_LEN);
148 static char *
149 history_str_get(zfs_cmd_t *zc)
151 char *buf;
153 if (zc->zc_history == NULL)
154 return (NULL);
156 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
157 if (copyinstr((void *)(uintptr_t)zc->zc_history,
158 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
159 history_str_free(buf);
160 return (NULL);
163 buf[HIS_MAX_RECORD_LEN -1] = '\0';
165 return (buf);
169 * Check to see if the named dataset is currently defined as bootable
171 static boolean_t
172 zfs_is_bootfs(const char *name)
174 spa_t *spa;
175 boolean_t ret = B_FALSE;
177 if (spa_open(name, &spa, FTAG) == 0) {
178 if (spa->spa_bootfs) {
179 objset_t *os;
181 if (dmu_objset_open(name, DMU_OST_ZFS,
182 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
183 ret = (dmu_objset_id(os) == spa->spa_bootfs);
184 dmu_objset_close(os);
187 spa_close(spa, FTAG);
189 return (ret);
193 * zfs_earlier_version
195 * Return non-zero if the spa version is less than requested version.
197 static int
198 zfs_earlier_version(const char *name, int version)
200 spa_t *spa;
202 if (spa_open(name, &spa, FTAG) == 0) {
203 if (spa_version(spa) < version) {
204 spa_close(spa, FTAG);
205 return (1);
207 spa_close(spa, FTAG);
209 return (0);
213 * zpl_earlier_version
215 * Return TRUE if the ZPL version is less than requested version.
217 static boolean_t
218 zpl_earlier_version(const char *name, int version)
220 objset_t *os;
221 boolean_t rc = B_TRUE;
223 if (dmu_objset_open(name, DMU_OST_ANY,
224 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
225 uint64_t zplversion;
227 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
228 rc = zplversion < version;
229 dmu_objset_close(os);
231 return (rc);
234 static void
235 zfs_log_history(zfs_cmd_t *zc)
237 spa_t *spa;
238 char *buf;
240 if ((buf = history_str_get(zc)) == NULL)
241 return;
243 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
244 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
245 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
246 spa_close(spa, FTAG);
248 history_str_free(buf);
252 * Policy for top-level read operations (list pools). Requires no privileges,
253 * and can be used in the local zone, as there is no associated dataset.
255 /* ARGSUSED */
256 static int
257 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
259 return (0);
263 * Policy for dataset read operations (list children, get statistics). Requires
264 * no privileges, but must be visible in the local zone.
266 /* ARGSUSED */
267 static int
268 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
270 if (INGLOBALZONE(curproc) ||
271 zone_dataset_visible(zc->zc_name, NULL))
272 return (0);
274 return (ENOENT);
277 static int
278 zfs_dozonecheck(const char *dataset, cred_t *cr)
280 uint64_t zoned;
281 int writable = 1;
284 * The dataset must be visible by this zone -- check this first
285 * so they don't see EPERM on something they shouldn't know about.
287 if (!INGLOBALZONE(curproc) &&
288 !zone_dataset_visible(dataset, &writable))
289 return (ENOENT);
291 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
292 return (ENOENT);
294 if (INGLOBALZONE(curproc)) {
296 * If the fs is zoned, only root can access it from the
297 * global zone.
299 if (secpolicy_zfs(cr) && zoned)
300 return (EPERM);
301 } else {
303 * If we are in a local zone, the 'zoned' property must be set.
305 if (!zoned)
306 return (EPERM);
308 /* must be writable by this zone */
309 if (!writable)
310 return (EPERM);
312 return (0);
316 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
318 int error;
320 error = zfs_dozonecheck(name, cr);
321 if (error == 0) {
322 error = secpolicy_zfs(cr);
323 if (error)
324 error = dsl_deleg_access(name, perm, cr);
326 return (error);
329 static int
330 zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
333 * Check permissions for special properties.
335 switch (prop) {
336 case ZFS_PROP_ZONED:
338 * Disallow setting of 'zoned' from within a local zone.
340 if (!INGLOBALZONE(curproc))
341 return (EPERM);
342 break;
344 case ZFS_PROP_QUOTA:
345 if (!INGLOBALZONE(curproc)) {
346 uint64_t zoned;
347 char setpoint[MAXNAMELEN];
349 * Unprivileged users are allowed to modify the
350 * quota on things *under* (ie. contained by)
351 * the thing they own.
353 if (dsl_prop_get_integer(name, "zoned", &zoned,
354 setpoint))
355 return (EPERM);
356 if (!zoned || strlen(name) <= strlen(setpoint))
357 return (EPERM);
359 break;
362 return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
366 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
368 int error;
370 error = zfs_dozonecheck(zc->zc_name, cr);
371 if (error)
372 return (error);
375 * permission to set permissions will be evaluated later in
376 * dsl_deleg_can_allow()
378 return (0);
382 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
384 int error;
385 error = zfs_secpolicy_write_perms(zc->zc_name,
386 ZFS_DELEG_PERM_ROLLBACK, cr);
387 if (error == 0)
388 error = zfs_secpolicy_write_perms(zc->zc_name,
389 ZFS_DELEG_PERM_MOUNT, cr);
390 return (error);
394 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
396 return (zfs_secpolicy_write_perms(zc->zc_name,
397 ZFS_DELEG_PERM_SEND, cr));
401 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
403 #ifdef __NetBSD__
404 printf("XXX zfs_secpolicy_share write me\n");
405 return EPERM;
406 #else
407 if (!INGLOBALZONE(curproc))
408 return (EPERM);
410 if (secpolicy_nfs(cr) == 0) {
411 return (0);
412 } else {
413 vnode_t *vp;
414 int error;
416 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
417 NO_FOLLOW, NULL, &vp)) != 0)
418 return (error);
420 /* Now make sure mntpnt and dataset are ZFS */
422 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
423 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
424 zc->zc_name) != 0)) {
425 VN_RELE(vp);
426 return (EPERM);
429 VN_RELE(vp);
430 return (dsl_deleg_access(zc->zc_name,
431 ZFS_DELEG_PERM_SHARE, cr));
433 #endif /* __NetBSD__ */
436 static int
437 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
439 char *cp;
442 * Remove the @bla or /bla from the end of the name to get the parent.
444 (void) strncpy(parent, datasetname, parentsize);
445 cp = strrchr(parent, '@');
446 if (cp != NULL) {
447 cp[0] = '\0';
448 } else {
449 cp = strrchr(parent, '/');
450 if (cp == NULL)
451 return (ENOENT);
452 cp[0] = '\0';
455 return (0);
459 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
461 int error;
463 if ((error = zfs_secpolicy_write_perms(name,
464 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
465 return (error);
467 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
470 static int
471 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
473 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
477 * Must have sys_config privilege to check the iscsi permission
479 /* ARGSUSED */
480 static int
481 zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
483 return (secpolicy_zfs(cr));
487 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
489 char parentname[MAXNAMELEN];
490 int error;
492 if ((error = zfs_secpolicy_write_perms(from,
493 ZFS_DELEG_PERM_RENAME, cr)) != 0)
494 return (error);
496 if ((error = zfs_secpolicy_write_perms(from,
497 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
498 return (error);
500 if ((error = zfs_get_parent(to, parentname,
501 sizeof (parentname))) != 0)
502 return (error);
504 if ((error = zfs_secpolicy_write_perms(parentname,
505 ZFS_DELEG_PERM_CREATE, cr)) != 0)
506 return (error);
508 if ((error = zfs_secpolicy_write_perms(parentname,
509 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
510 return (error);
512 return (error);
515 static int
516 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
518 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
521 static int
522 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
524 char parentname[MAXNAMELEN];
525 objset_t *clone;
526 int error;
528 error = zfs_secpolicy_write_perms(zc->zc_name,
529 ZFS_DELEG_PERM_PROMOTE, cr);
530 if (error)
531 return (error);
533 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
534 DS_MODE_USER | DS_MODE_READONLY, &clone);
536 if (error == 0) {
537 dsl_dataset_t *pclone = NULL;
538 dsl_dir_t *dd;
539 dd = clone->os->os_dsl_dataset->ds_dir;
541 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
542 error = dsl_dataset_hold_obj(dd->dd_pool,
543 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
544 rw_exit(&dd->dd_pool->dp_config_rwlock);
545 if (error) {
546 dmu_objset_close(clone);
547 return (error);
550 error = zfs_secpolicy_write_perms(zc->zc_name,
551 ZFS_DELEG_PERM_MOUNT, cr);
553 dsl_dataset_name(pclone, parentname);
554 dmu_objset_close(clone);
555 dsl_dataset_rele(pclone, FTAG);
556 if (error == 0)
557 error = zfs_secpolicy_write_perms(parentname,
558 ZFS_DELEG_PERM_PROMOTE, cr);
560 return (error);
563 static int
564 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
566 int error;
568 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
569 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
570 return (error);
572 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
573 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
574 return (error);
576 return (zfs_secpolicy_write_perms(zc->zc_name,
577 ZFS_DELEG_PERM_CREATE, cr));
581 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
583 int error;
585 if ((error = zfs_secpolicy_write_perms(name,
586 ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
587 return (error);
589 error = zfs_secpolicy_write_perms(name,
590 ZFS_DELEG_PERM_MOUNT, cr);
592 return (error);
595 static int
596 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
599 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
602 static int
603 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
605 char parentname[MAXNAMELEN];
606 int error;
608 if ((error = zfs_get_parent(zc->zc_name, parentname,
609 sizeof (parentname))) != 0)
610 return (error);
612 if (zc->zc_value[0] != '\0') {
613 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
614 ZFS_DELEG_PERM_CLONE, cr)) != 0)
615 return (error);
618 if ((error = zfs_secpolicy_write_perms(parentname,
619 ZFS_DELEG_PERM_CREATE, cr)) != 0)
620 return (error);
622 error = zfs_secpolicy_write_perms(parentname,
623 ZFS_DELEG_PERM_MOUNT, cr);
625 return (error);
628 static int
629 zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
631 int error;
633 error = secpolicy_fs_unmount(cr, NULL);
634 if (error) {
635 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
637 return (error);
641 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
642 * SYS_CONFIG privilege, which is not available in a local zone.
644 /* ARGSUSED */
645 static int
646 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
648 if (secpolicy_sys_config(cr, B_FALSE) != 0)
649 return (EPERM);
651 return (0);
655 * Just like zfs_secpolicy_config, except that we will check for
656 * mount permission on the dataset for permission to create/remove
657 * the minor nodes.
659 static int
660 zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
662 if (secpolicy_sys_config(cr, B_FALSE) != 0) {
663 return (dsl_deleg_access(zc->zc_name,
664 ZFS_DELEG_PERM_MOUNT, cr));
667 return (0);
671 * Policy for fault injection. Requires all privileges.
673 /* ARGSUSED */
674 static int
675 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
677 return (secpolicy_zinject(cr));
680 static int
681 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
683 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
685 if (prop == ZPROP_INVAL) {
686 if (!zfs_prop_user(zc->zc_value))
687 return (EINVAL);
688 return (zfs_secpolicy_write_perms(zc->zc_name,
689 ZFS_DELEG_PERM_USERPROP, cr));
690 } else {
691 if (!zfs_prop_inheritable(prop))
692 return (EINVAL);
693 return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
698 * Returns the nvlist as specified by the user in the zfs_cmd_t.
700 static int
701 get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
703 char *packed;
704 int error;
705 nvlist_t *list = NULL;
708 * Read in and unpack the user-supplied nvlist.
710 if (size == 0)
711 return (EINVAL);
713 packed = kmem_alloc(size, KM_SLEEP);
715 if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
716 kmem_free(packed, size);
717 return (error);
720 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
721 kmem_free(packed, size);
722 return (error);
725 kmem_free(packed, size);
727 *nvp = list;
728 return (0);
731 static int
732 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
734 char *packed = NULL;
735 size_t size;
736 int error;
738 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
740 if (size > zc->zc_nvlist_dst_size) {
741 error = ENOMEM;
742 } else {
743 packed = kmem_alloc(size, KM_SLEEP);
744 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
745 KM_SLEEP) == 0);
746 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
747 size);
748 kmem_free(packed, size);
751 zc->zc_nvlist_dst_size = size;
752 return (error);
755 static int
756 zfs_ioc_pool_create(zfs_cmd_t *zc)
758 int error;
759 nvlist_t *config, *props = NULL;
760 nvlist_t *rootprops = NULL;
761 nvlist_t *zplprops = NULL;
762 char *buf;
764 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
765 &config))
766 return (error);
768 if (zc->zc_nvlist_src_size != 0 && (error =
769 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
770 nvlist_free(config);
771 return (error);
774 if (props) {
775 nvlist_t *nvl = NULL;
776 uint64_t version = SPA_VERSION;
778 (void) nvlist_lookup_uint64(props,
779 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
780 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
781 error = EINVAL;
782 goto pool_props_bad;
784 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
785 if (nvl) {
786 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
787 if (error != 0) {
788 nvlist_free(config);
789 nvlist_free(props);
790 return (error);
792 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
794 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
795 error = zfs_fill_zplprops_root(version, rootprops,
796 zplprops, NULL);
797 if (error)
798 goto pool_props_bad;
801 buf = history_str_get(zc);
803 error = spa_create(zc->zc_name, config, props, buf, zplprops);
806 * Set the remaining root properties
808 if (!error &&
809 (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
810 (void) spa_destroy(zc->zc_name);
812 if (buf != NULL)
813 history_str_free(buf);
815 pool_props_bad:
816 nvlist_free(rootprops);
817 nvlist_free(zplprops);
818 nvlist_free(config);
819 nvlist_free(props);
821 return (error);
824 static int
825 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
827 int error;
828 zfs_log_history(zc);
829 error = spa_destroy(zc->zc_name);
830 return (error);
833 static int
834 zfs_ioc_pool_import(zfs_cmd_t *zc)
836 int error;
837 nvlist_t *config, *props = NULL;
838 uint64_t guid;
840 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
841 &config)) != 0)
842 return (error);
844 if (zc->zc_nvlist_src_size != 0 && (error =
845 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
846 nvlist_free(config);
847 return (error);
850 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
851 guid != zc->zc_guid)
852 error = EINVAL;
853 else if (zc->zc_cookie)
854 error = spa_import_faulted(zc->zc_name, config,
855 props);
856 else
857 error = spa_import(zc->zc_name, config, props);
859 nvlist_free(config);
861 if (props)
862 nvlist_free(props);
864 return (error);
867 static int
868 zfs_ioc_pool_export(zfs_cmd_t *zc)
870 int error;
871 boolean_t force = (boolean_t)zc->zc_cookie;
873 zfs_log_history(zc);
874 error = spa_export(zc->zc_name, NULL, force);
875 return (error);
878 static int
879 zfs_ioc_pool_configs(zfs_cmd_t *zc)
881 nvlist_t *configs;
882 int error;
884 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
885 return (EEXIST);
887 error = put_nvlist(zc, configs);
889 nvlist_free(configs);
891 return (error);
894 static int
895 zfs_ioc_pool_stats(zfs_cmd_t *zc)
897 nvlist_t *config;
898 int error;
899 int ret = 0;
901 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
902 sizeof (zc->zc_value));
904 if (config != NULL) {
905 ret = put_nvlist(zc, config);
906 nvlist_free(config);
909 * The config may be present even if 'error' is non-zero.
910 * In this case we return success, and preserve the real errno
911 * in 'zc_cookie'.
913 zc->zc_cookie = error;
914 } else {
915 ret = error;
918 return (ret);
922 * Try to import the given pool, returning pool stats as appropriate so that
923 * user land knows which devices are available and overall pool health.
925 static int
926 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
928 nvlist_t *tryconfig, *config;
929 int error;
931 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
932 &tryconfig)) != 0)
933 return (error);
935 config = spa_tryimport(tryconfig);
937 nvlist_free(tryconfig);
939 if (config == NULL)
940 return (EINVAL);
942 error = put_nvlist(zc, config);
943 nvlist_free(config);
945 return (error);
948 static int
949 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
951 spa_t *spa;
952 int error;
954 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
955 return (error);
957 error = spa_scrub(spa, zc->zc_cookie);
959 spa_close(spa, FTAG);
961 return (error);
964 static int
965 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
967 spa_t *spa;
968 int error;
970 error = spa_open(zc->zc_name, &spa, FTAG);
971 if (error == 0) {
972 spa_freeze(spa);
973 spa_close(spa, FTAG);
975 return (error);
978 static int
979 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
981 spa_t *spa;
982 int error;
984 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
985 return (error);
987 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
988 spa_close(spa, FTAG);
989 return (EINVAL);
992 spa_upgrade(spa, zc->zc_cookie);
993 spa_close(spa, FTAG);
995 return (error);
998 static int
999 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1001 spa_t *spa;
1002 char *hist_buf;
1003 uint64_t size;
1004 int error;
1006 if ((size = zc->zc_history_len) == 0)
1007 return (EINVAL);
1009 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1010 return (error);
1012 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1013 spa_close(spa, FTAG);
1014 return (ENOTSUP);
1017 hist_buf = kmem_alloc(size, KM_SLEEP);
1018 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1019 &zc->zc_history_len, hist_buf)) == 0) {
1020 error = xcopyout(hist_buf,
1021 (char *)(uintptr_t)zc->zc_history,
1022 zc->zc_history_len);
1025 spa_close(spa, FTAG);
1026 kmem_free(hist_buf, size);
1027 return (error);
1030 static int
1031 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1033 int error;
1035 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1036 return (error);
1038 return (0);
1041 static int
1042 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1044 objset_t *osp;
1045 int error;
1047 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
1048 DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
1049 return (error);
1050 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
1051 sizeof (zc->zc_value));
1052 dmu_objset_close(osp);
1054 return (error);
1057 static int
1058 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1060 spa_t *spa;
1061 int error;
1062 nvlist_t *config, **l2cache, **spares;
1063 uint_t nl2cache = 0, nspares = 0;
1065 error = spa_open(zc->zc_name, &spa, FTAG);
1066 if (error != 0)
1067 return (error);
1069 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1070 &config);
1071 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1072 &l2cache, &nl2cache);
1074 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1075 &spares, &nspares);
1078 * A root pool with concatenated devices is not supported.
1079 * Thus, can not add a device to a root pool.
1081 * Intent log device can not be added to a rootpool because
1082 * during mountroot, zil is replayed, a seperated log device
1083 * can not be accessed during the mountroot time.
1085 * l2cache and spare devices are ok to be added to a rootpool.
1087 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1088 spa_close(spa, FTAG);
1089 return (EDOM);
1092 if (error == 0) {
1093 error = spa_vdev_add(spa, config);
1094 nvlist_free(config);
1096 spa_close(spa, FTAG);
1097 return (error);
1100 static int
1101 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1103 spa_t *spa;
1104 int error;
1106 error = spa_open(zc->zc_name, &spa, FTAG);
1107 if (error != 0)
1108 return (error);
1109 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1110 spa_close(spa, FTAG);
1111 return (error);
1114 static int
1115 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1117 spa_t *spa;
1118 int error;
1119 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1121 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1122 return (error);
1123 switch (zc->zc_cookie) {
1124 case VDEV_STATE_ONLINE:
1125 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1126 break;
1128 case VDEV_STATE_OFFLINE:
1129 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1130 break;
1132 case VDEV_STATE_FAULTED:
1133 error = vdev_fault(spa, zc->zc_guid);
1134 break;
1136 case VDEV_STATE_DEGRADED:
1137 error = vdev_degrade(spa, zc->zc_guid);
1138 break;
1140 default:
1141 error = EINVAL;
1143 zc->zc_cookie = newstate;
1144 spa_close(spa, FTAG);
1145 return (error);
1148 static int
1149 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1151 spa_t *spa;
1152 int replacing = zc->zc_cookie;
1153 nvlist_t *config;
1154 int error;
1156 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1157 return (error);
1159 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1160 &config)) == 0) {
1161 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1162 nvlist_free(config);
1165 spa_close(spa, FTAG);
1166 return (error);
1169 static int
1170 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1172 spa_t *spa;
1173 int error;
1175 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1176 return (error);
1178 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1180 spa_close(spa, FTAG);
1181 return (error);
1184 static int
1185 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1187 spa_t *spa;
1188 char *path = zc->zc_value;
1189 uint64_t guid = zc->zc_guid;
1190 int error;
1192 error = spa_open(zc->zc_name, &spa, FTAG);
1193 if (error != 0)
1194 return (error);
1196 error = spa_vdev_setpath(spa, guid, path);
1197 spa_close(spa, FTAG);
1198 return (error);
1202 * inputs:
1203 * zc_name name of filesystem
1204 * zc_nvlist_dst_size size of buffer for property nvlist
1206 * outputs:
1207 * zc_objset_stats stats
1208 * zc_nvlist_dst property nvlist
1209 * zc_nvlist_dst_size size of property nvlist
1211 static int
1212 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1214 objset_t *os = NULL;
1215 int error;
1216 nvlist_t *nv;
1218 if (error = dmu_objset_open(zc->zc_name,
1219 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1220 return (error);
1222 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1224 if (zc->zc_nvlist_dst != 0 &&
1225 (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1226 dmu_objset_stats(os, nv);
1228 * NB: zvol_get_stats() will read the objset contents,
1229 * which we aren't supposed to do with a
1230 * DS_MODE_USER hold, because it could be
1231 * inconsistent. So this is a bit of a workaround...
1233 if (!zc->zc_objset_stats.dds_inconsistent) {
1234 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1235 VERIFY(zvol_get_stats(os, nv) == 0);
1237 error = put_nvlist(zc, nv);
1238 nvlist_free(nv);
1241 dmu_objset_close(os);
1242 return (error);
1245 static int
1246 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1248 uint64_t value;
1249 int error;
1252 * zfs_get_zplprop() will either find a value or give us
1253 * the default value (if there is one).
1255 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1256 return (error);
1257 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1258 return (0);
1262 * inputs:
1263 * zc_name name of filesystem
1264 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1266 * outputs:
1267 * zc_nvlist_dst zpl property nvlist
1268 * zc_nvlist_dst_size size of zpl property nvlist
1270 static int
1271 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1273 objset_t *os;
1274 int err;
1276 if (err = dmu_objset_open(zc->zc_name,
1277 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1278 return (err);
1280 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1283 * NB: nvl_add_zplprop() will read the objset contents,
1284 * which we aren't supposed to do with a DS_MODE_USER
1285 * hold, because it could be inconsistent.
1287 if (zc->zc_nvlist_dst != NULL &&
1288 !zc->zc_objset_stats.dds_inconsistent &&
1289 dmu_objset_type(os) == DMU_OST_ZFS) {
1290 nvlist_t *nv;
1292 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1293 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1294 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1295 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1296 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1297 err = put_nvlist(zc, nv);
1298 nvlist_free(nv);
1299 } else {
1300 err = ENOENT;
1302 dmu_objset_close(os);
1303 return (err);
1306 static void
1307 zfs_prefetch_datasets(zfs_cmd_t *zc, objset_t *os, char *p)
1309 uint64_t cookie = 0;
1310 int error;
1312 do {
1313 error = dmu_dir_list_next(os,
1314 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1315 NULL, &cookie);
1316 } while (error == 0 && !INGLOBALZONE(curproc) &&
1317 !zone_dataset_visible(zc->zc_name, NULL) &&
1318 !dmu_objset_prefetch(zc->zc_name, NULL));
1321 static void
1322 zfs_prefetch_snapshots(zfs_cmd_t *zc)
1324 dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1325 NULL, DS_FIND_SNAPSHOTS);
1329 * inputs:
1330 * zc_name name of filesystem
1331 * zc_cookie zap cursor
1332 * zc_nvlist_dst_size size of buffer for property nvlist
1334 * outputs:
1335 * zc_name name of next filesystem
1336 * zc_objset_stats stats
1337 * zc_nvlist_dst property nvlist
1338 * zc_nvlist_dst_size size of property nvlist
1340 static int
1341 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1343 objset_t *os;
1344 int error;
1345 char *p;
1347 if (error = dmu_objset_open(zc->zc_name,
1348 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1349 if (error == ENOENT)
1350 error = ESRCH;
1351 return (error);
1354 p = strrchr(zc->zc_name, '/');
1355 if (p == NULL || p[1] != '\0')
1356 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1357 p = zc->zc_name + strlen(zc->zc_name);
1359 if (zc->zc_cookie == 0)
1360 zfs_prefetch_datasets(zc, os, p);
1361 do {
1362 error = dmu_dir_list_next(os,
1363 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1364 NULL, &zc->zc_cookie);
1365 if (error == ENOENT)
1366 error = ESRCH;
1367 } while (error == 0 && !INGLOBALZONE(curproc) &&
1368 !zone_dataset_visible(zc->zc_name, NULL));
1369 dmu_objset_close(os);
1372 * If it's a hidden dataset (ie. with a '$' in its name), don't
1373 * try to get stats for it. Userland will skip over it.
1375 if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1376 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1378 return (error);
1382 * inputs:
1383 * zc_name name of filesystem
1384 * zc_cookie zap cursor
1385 * zc_nvlist_dst_size size of buffer for property nvlist
1387 * outputs:
1388 * zc_name name of next snapshot
1389 * zc_objset_stats stats
1390 * zc_nvlist_dst property nvlist
1391 * zc_nvlist_dst_size size of property nvlist
1393 static int
1394 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1396 objset_t *os;
1397 int error;
1399 error = dmu_objset_open(zc->zc_name,
1400 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
1401 if (error)
1402 return (error == ENOENT ? ESRCH : error);
1404 if (zc->zc_cookie == 0)
1405 zfs_prefetch_snapshots(zc);
1407 * A dataset name of maximum length cannot have any snapshots,
1408 * so exit immediately.
1410 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1411 dmu_objset_close(os);
1412 return (ESRCH);
1415 error = dmu_snapshot_list_next(os,
1416 sizeof (zc->zc_name) - strlen(zc->zc_name),
1417 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1418 dmu_objset_close(os);
1419 if (error == 0)
1420 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1421 else if (error == ENOENT)
1422 error = ESRCH;
1424 /* if we failed, undo the @ that we tacked on to zc_name */
1425 if (error)
1426 *strchr(zc->zc_name, '@') = '\0';
1427 return (error);
1431 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1433 nvpair_t *elem;
1434 int error;
1435 uint64_t intval;
1436 char *strval;
1439 * First validate permission to set all of the properties
1441 elem = NULL;
1442 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1443 const char *propname = nvpair_name(elem);
1444 zfs_prop_t prop = zfs_name_to_prop(propname);
1446 if (prop == ZPROP_INVAL) {
1448 * If this is a user-defined property, it must be a
1449 * string, and there is no further validation to do.
1451 if (!zfs_prop_user(propname) ||
1452 nvpair_type(elem) != DATA_TYPE_STRING)
1453 return (EINVAL);
1455 if (error = zfs_secpolicy_write_perms(name,
1456 ZFS_DELEG_PERM_USERPROP, CRED()))
1457 return (error);
1458 continue;
1461 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1462 return (error);
1465 * Check that this value is valid for this pool version
1467 switch (prop) {
1468 case ZFS_PROP_COMPRESSION:
1470 * If the user specified gzip compression, make sure
1471 * the SPA supports it. We ignore any errors here since
1472 * we'll catch them later.
1474 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1475 nvpair_value_uint64(elem, &intval) == 0) {
1476 if (intval >= ZIO_COMPRESS_GZIP_1 &&
1477 intval <= ZIO_COMPRESS_GZIP_9 &&
1478 zfs_earlier_version(name,
1479 SPA_VERSION_GZIP_COMPRESSION))
1480 return (ENOTSUP);
1483 * If this is a bootable dataset then
1484 * verify that the compression algorithm
1485 * is supported for booting. We must return
1486 * something other than ENOTSUP since it
1487 * implies a downrev pool version.
1489 if (zfs_is_bootfs(name) &&
1490 !BOOTFS_COMPRESS_VALID(intval))
1491 return (ERANGE);
1493 break;
1495 case ZFS_PROP_COPIES:
1496 if (zfs_earlier_version(name,
1497 SPA_VERSION_DITTO_BLOCKS))
1498 return (ENOTSUP);
1499 break;
1501 case ZFS_PROP_SHARESMB:
1502 if (zpl_earlier_version(name, ZPL_VERSION_FUID))
1503 return (ENOTSUP);
1504 break;
1506 case ZFS_PROP_ACLINHERIT:
1507 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1508 nvpair_value_uint64(elem, &intval) == 0)
1509 if (intval == ZFS_ACL_PASSTHROUGH_X &&
1510 zfs_earlier_version(name,
1511 SPA_VERSION_PASSTHROUGH_X))
1512 return (ENOTSUP);
1516 elem = NULL;
1517 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1518 const char *propname = nvpair_name(elem);
1519 zfs_prop_t prop = zfs_name_to_prop(propname);
1521 if (prop == ZPROP_INVAL) {
1522 VERIFY(nvpair_value_string(elem, &strval) == 0);
1523 error = dsl_prop_set(name, propname, 1,
1524 strlen(strval) + 1, strval);
1525 if (error == 0)
1526 continue;
1527 else
1528 return (error);
1531 switch (prop) {
1532 case ZFS_PROP_QUOTA:
1533 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1534 (error = dsl_dir_set_quota(name, intval)) != 0)
1535 return (error);
1536 break;
1538 case ZFS_PROP_REFQUOTA:
1539 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1540 (error = dsl_dataset_set_quota(name, intval)) != 0)
1541 return (error);
1542 break;
1544 case ZFS_PROP_RESERVATION:
1545 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1546 (error = dsl_dir_set_reservation(name,
1547 intval)) != 0)
1548 return (error);
1549 break;
1551 case ZFS_PROP_REFRESERVATION:
1552 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1553 (error = dsl_dataset_set_reservation(name,
1554 intval)) != 0)
1555 return (error);
1556 break;
1558 case ZFS_PROP_VOLSIZE:
1559 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1560 (error = zvol_set_volsize(name,
1561 ddi_driver_major(zfs_dip), intval)) != 0)
1562 return (error);
1563 break;
1565 case ZFS_PROP_VOLBLOCKSIZE:
1566 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1567 (error = zvol_set_volblocksize(name, intval)) != 0)
1568 return (error);
1569 break;
1571 case ZFS_PROP_VERSION:
1572 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1573 (error = zfs_set_version(name, intval)) != 0)
1574 return (error);
1575 break;
1577 default:
1578 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1579 if (zfs_prop_get_type(prop) !=
1580 PROP_TYPE_STRING)
1581 return (EINVAL);
1582 VERIFY(nvpair_value_string(elem, &strval) == 0);
1583 if ((error = dsl_prop_set(name,
1584 nvpair_name(elem), 1, strlen(strval) + 1,
1585 strval)) != 0)
1586 return (error);
1587 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1588 const char *unused;
1590 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1592 switch (zfs_prop_get_type(prop)) {
1593 case PROP_TYPE_NUMBER:
1594 break;
1595 case PROP_TYPE_STRING:
1596 return (EINVAL);
1597 case PROP_TYPE_INDEX:
1598 if (zfs_prop_index_to_string(prop,
1599 intval, &unused) != 0)
1600 return (EINVAL);
1601 break;
1602 default:
1603 cmn_err(CE_PANIC,
1604 "unknown property type");
1605 break;
1608 if ((error = dsl_prop_set(name, propname,
1609 8, 1, &intval)) != 0)
1610 return (error);
1611 } else {
1612 return (EINVAL);
1614 break;
1618 return (0);
1622 * inputs:
1623 * zc_name name of filesystem
1624 * zc_value name of property to inherit
1625 * zc_nvlist_src{_size} nvlist of properties to apply
1626 * zc_cookie clear existing local props?
1628 * outputs: none
1630 static int
1631 zfs_ioc_set_prop(zfs_cmd_t *zc)
1633 nvlist_t *nvl;
1634 int error;
1636 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1637 &nvl)) != 0)
1638 return (error);
1640 if (zc->zc_cookie) {
1641 nvlist_t *origprops;
1642 objset_t *os;
1644 if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1645 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1646 if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
1647 clear_props(zc->zc_name, origprops);
1648 nvlist_free(origprops);
1650 dmu_objset_close(os);
1655 error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1657 nvlist_free(nvl);
1658 return (error);
1662 * inputs:
1663 * zc_name name of filesystem
1664 * zc_value name of property to inherit
1666 * outputs: none
1668 static int
1669 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1671 /* the property name has been validated by zfs_secpolicy_inherit() */
1672 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1675 static int
1676 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1678 nvlist_t *props;
1679 spa_t *spa;
1680 int error;
1682 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1683 &props)))
1684 return (error);
1686 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1687 nvlist_free(props);
1688 return (error);
1691 error = spa_prop_set(spa, props);
1693 nvlist_free(props);
1694 spa_close(spa, FTAG);
1696 return (error);
1699 static int
1700 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1702 spa_t *spa;
1703 int error;
1704 nvlist_t *nvp = NULL;
1706 dprintf("zfs_ioc_pool_get_props called %s \n", spa->spa_name);
1707 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1708 return (error);
1710 error = spa_prop_get(spa, &nvp);
1712 if (error == 0 && zc->zc_nvlist_dst != NULL)
1713 error = put_nvlist(zc, nvp);
1714 else
1715 error = EFAULT;
1717 spa_close(spa, FTAG);
1719 if (nvp)
1720 nvlist_free(nvp);
1721 dprintf("zfs_ioc_pool_get_props property list set\n");
1722 return (error);
1725 static int
1726 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1728 nvlist_t *nvp;
1729 int error;
1730 uint32_t uid;
1731 uint32_t gid;
1732 uint32_t *groups;
1733 uint_t group_cnt;
1734 cred_t *usercred;
1736 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1737 &nvp)) != 0) {
1738 return (error);
1741 if ((error = nvlist_lookup_uint32(nvp,
1742 ZFS_DELEG_PERM_UID, &uid)) != 0) {
1743 nvlist_free(nvp);
1744 return (EPERM);
1747 if ((error = nvlist_lookup_uint32(nvp,
1748 ZFS_DELEG_PERM_GID, &gid)) != 0) {
1749 nvlist_free(nvp);
1750 return (EPERM);
1753 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1754 &groups, &group_cnt)) != 0) {
1755 nvlist_free(nvp);
1756 return (EPERM);
1758 usercred = cralloc();
1759 if ((crsetugid(usercred, uid, gid) != 0) ||
1760 (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1761 nvlist_free(nvp);
1762 crfree(usercred);
1763 return (EPERM);
1765 nvlist_free(nvp);
1766 error = dsl_deleg_access(zc->zc_name,
1767 zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1768 crfree(usercred);
1769 return (error);
1773 * inputs:
1774 * zc_name name of filesystem
1775 * zc_nvlist_src{_size} nvlist of delegated permissions
1776 * zc_perm_action allow/unallow flag
1778 * outputs: none
1780 static int
1781 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1783 int error;
1784 nvlist_t *fsaclnv = NULL;
1786 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1787 &fsaclnv)) != 0)
1788 return (error);
1791 * Verify nvlist is constructed correctly
1793 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1794 nvlist_free(fsaclnv);
1795 return (EINVAL);
1799 * If we don't have PRIV_SYS_MOUNT, then validate
1800 * that user is allowed to hand out each permission in
1801 * the nvlist(s)
1804 error = secpolicy_zfs(CRED());
1805 if (error) {
1806 if (zc->zc_perm_action == B_FALSE) {
1807 error = dsl_deleg_can_allow(zc->zc_name,
1808 fsaclnv, CRED());
1809 } else {
1810 error = dsl_deleg_can_unallow(zc->zc_name,
1811 fsaclnv, CRED());
1815 if (error == 0)
1816 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1818 nvlist_free(fsaclnv);
1819 return (error);
1823 * inputs:
1824 * zc_name name of filesystem
1826 * outputs:
1827 * zc_nvlist_src{_size} nvlist of delegated permissions
1829 static int
1830 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1832 nvlist_t *nvp;
1833 int error;
1835 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1836 error = put_nvlist(zc, nvp);
1837 nvlist_free(nvp);
1840 return (error);
1844 * inputs:
1845 * zc_name name of volume
1847 * outputs: none
1849 static int
1850 zfs_ioc_create_minor(zfs_cmd_t *zc)
1852 return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1856 * inputs:
1857 * zc_name name of volume
1859 * outputs: none
1861 static int
1862 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1864 return (zvol_remove_minor(zc->zc_name));
1868 * Search the vfs list for a specified resource. Returns a pointer to it
1869 * or NULL if no suitable entry is found. The caller of this routine
1870 * is responsible for releasing the returned vfs pointer.
1872 static vfs_t *
1873 zfs_get_vfs(const char *resource)
1876 printf("XXX zfs_get_vfs write me\n");
1877 return NULL;
1880 /* ARGSUSED */
1881 static void
1882 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1884 zfs_creat_t *zct = arg;
1886 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1889 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1892 * inputs:
1893 * createprops list of properties requested by creator
1894 * default_zplver zpl version to use if unspecified in createprops
1895 * fuids_ok fuids allowed in this version of the spa?
1896 * os parent objset pointer (NULL if root fs)
1898 * outputs:
1899 * zplprops values for the zplprops we attach to the master node object
1900 * is_ci true if requested file system will be purely case-insensitive
1902 * Determine the settings for utf8only, normalization and
1903 * casesensitivity. Specific values may have been requested by the
1904 * creator and/or we can inherit values from the parent dataset. If
1905 * the file system is of too early a vintage, a creator can not
1906 * request settings for these properties, even if the requested
1907 * setting is the default value. We don't actually want to create dsl
1908 * properties for these, so remove them from the source nvlist after
1909 * processing.
1911 static int
1912 zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
1913 boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
1914 boolean_t *is_ci)
1916 uint64_t zplver = default_zplver;
1917 uint64_t sense = ZFS_PROP_UNDEFINED;
1918 uint64_t norm = ZFS_PROP_UNDEFINED;
1919 uint64_t u8 = ZFS_PROP_UNDEFINED;
1921 ASSERT(zplprops != NULL);
1924 * Pull out creator prop choices, if any.
1926 if (createprops) {
1927 (void) nvlist_lookup_uint64(createprops,
1928 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
1929 (void) nvlist_lookup_uint64(createprops,
1930 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1931 (void) nvlist_remove_all(createprops,
1932 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1933 (void) nvlist_lookup_uint64(createprops,
1934 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1935 (void) nvlist_remove_all(createprops,
1936 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1937 (void) nvlist_lookup_uint64(createprops,
1938 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1939 (void) nvlist_remove_all(createprops,
1940 zfs_prop_to_name(ZFS_PROP_CASE));
1944 * If the zpl version requested is whacky or the file system
1945 * or pool is version is too "young" to support normalization
1946 * and the creator tried to set a value for one of the props,
1947 * error out.
1949 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
1950 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
1951 (zplver < ZPL_VERSION_NORMALIZATION &&
1952 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1953 sense != ZFS_PROP_UNDEFINED)))
1954 return (ENOTSUP);
1957 * Put the version in the zplprops
1959 VERIFY(nvlist_add_uint64(zplprops,
1960 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
1962 if (norm == ZFS_PROP_UNDEFINED)
1963 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
1964 VERIFY(nvlist_add_uint64(zplprops,
1965 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
1968 * If we're normalizing, names must always be valid UTF-8 strings.
1970 if (norm)
1971 u8 = 1;
1972 if (u8 == ZFS_PROP_UNDEFINED)
1973 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
1974 VERIFY(nvlist_add_uint64(zplprops,
1975 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
1977 if (sense == ZFS_PROP_UNDEFINED)
1978 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
1979 VERIFY(nvlist_add_uint64(zplprops,
1980 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
1982 if (is_ci)
1983 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
1985 return (0);
1988 static int
1989 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
1990 nvlist_t *zplprops, boolean_t *is_ci)
1992 boolean_t fuids_ok = B_TRUE;
1993 uint64_t zplver = ZPL_VERSION;
1994 objset_t *os = NULL;
1995 char parentname[MAXNAMELEN];
1996 char *cp;
1997 int error;
1999 (void) strlcpy(parentname, dataset, sizeof (parentname));
2000 cp = strrchr(parentname, '/');
2001 ASSERT(cp != NULL);
2002 cp[0] = '\0';
2004 if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2005 zplver = ZPL_VERSION_FUID - 1;
2006 fuids_ok = B_FALSE;
2010 * Open parent object set so we can inherit zplprop values.
2012 if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
2013 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
2014 return (error);
2016 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2017 zplprops, is_ci);
2018 dmu_objset_close(os);
2019 return (error);
2022 static int
2023 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2024 nvlist_t *zplprops, boolean_t *is_ci)
2026 boolean_t fuids_ok = B_TRUE;
2027 uint64_t zplver = ZPL_VERSION;
2028 int error;
2030 if (spa_vers < SPA_VERSION_FUID) {
2031 zplver = ZPL_VERSION_FUID - 1;
2032 fuids_ok = B_FALSE;
2035 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2036 zplprops, is_ci);
2037 return (error);
2041 * inputs:
2042 * zc_objset_type type of objset to create (fs vs zvol)
2043 * zc_name name of new objset
2044 * zc_value name of snapshot to clone from (may be empty)
2045 * zc_nvlist_src{_size} nvlist of properties to apply
2047 * outputs: none
2049 static int
2050 zfs_ioc_create(zfs_cmd_t *zc)
2052 objset_t *clone;
2053 int error = 0;
2054 zfs_creat_t zct;
2055 nvlist_t *nvprops = NULL;
2056 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2057 dmu_objset_type_t type = zc->zc_objset_type;
2059 switch (type) {
2061 case DMU_OST_ZFS:
2062 cbfunc = zfs_create_cb;
2063 break;
2065 case DMU_OST_ZVOL:
2066 cbfunc = zvol_create_cb;
2067 break;
2069 default:
2070 cbfunc = NULL;
2071 break;
2073 if (strchr(zc->zc_name, '@') ||
2074 strchr(zc->zc_name, '%'))
2075 return (EINVAL);
2077 if (zc->zc_nvlist_src != NULL &&
2078 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2079 &nvprops)) != 0)
2080 return (error);
2082 zct.zct_zplprops = NULL;
2083 zct.zct_props = nvprops;
2085 if (zc->zc_value[0] != '\0') {
2087 * We're creating a clone of an existing snapshot.
2089 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2090 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2091 nvlist_free(nvprops);
2092 return (EINVAL);
2095 error = dmu_objset_open(zc->zc_value, type,
2096 DS_MODE_USER | DS_MODE_READONLY, &clone);
2097 if (error) {
2098 nvlist_free(nvprops);
2099 return (error);
2102 error = dmu_objset_create(zc->zc_name, type, clone, 0,
2103 NULL, NULL);
2104 if (error) {
2105 dmu_objset_close(clone);
2106 nvlist_free(nvprops);
2107 return (error);
2109 dmu_objset_close(clone);
2110 } else {
2111 boolean_t is_insensitive = B_FALSE;
2113 if (cbfunc == NULL) {
2114 nvlist_free(nvprops);
2115 return (EINVAL);
2118 if (type == DMU_OST_ZVOL) {
2119 uint64_t volsize, volblocksize;
2121 if (nvprops == NULL ||
2122 nvlist_lookup_uint64(nvprops,
2123 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2124 &volsize) != 0) {
2125 nvlist_free(nvprops);
2126 return (EINVAL);
2129 if ((error = nvlist_lookup_uint64(nvprops,
2130 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2131 &volblocksize)) != 0 && error != ENOENT) {
2132 nvlist_free(nvprops);
2133 return (EINVAL);
2136 if (error != 0)
2137 volblocksize = zfs_prop_default_numeric(
2138 ZFS_PROP_VOLBLOCKSIZE);
2140 if ((error = zvol_check_volblocksize(
2141 volblocksize)) != 0 ||
2142 (error = zvol_check_volsize(volsize,
2143 volblocksize)) != 0) {
2144 nvlist_free(nvprops);
2145 return (error);
2147 } else if (type == DMU_OST_ZFS) {
2148 int error;
2151 * We have to have normalization and
2152 * case-folding flags correct when we do the
2153 * file system creation, so go figure them out
2154 * now.
2156 VERIFY(nvlist_alloc(&zct.zct_zplprops,
2157 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2158 error = zfs_fill_zplprops(zc->zc_name, nvprops,
2159 zct.zct_zplprops, &is_insensitive);
2160 if (error != 0) {
2161 nvlist_free(nvprops);
2162 nvlist_free(zct.zct_zplprops);
2163 return (error);
2166 error = dmu_objset_create(zc->zc_name, type, NULL,
2167 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2168 nvlist_free(zct.zct_zplprops);
2172 * It would be nice to do this atomically.
2174 if (error == 0) {
2175 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2176 (void) dmu_objset_destroy(zc->zc_name);
2178 nvlist_free(nvprops);
2179 return (error);
2182 struct snap_prop_arg {
2183 nvlist_t *nvprops;
2184 const char *snapname;
2187 static int
2188 set_snap_props(char *name, void *arg)
2190 struct snap_prop_arg *snpa = arg;
2191 int len = strlen(name) + strlen(snpa->snapname) + 2;
2192 char *buf = kmem_alloc(len, KM_SLEEP);
2193 int err;
2195 (void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
2196 err = zfs_set_prop_nvlist(buf, snpa->nvprops);
2197 if (err)
2198 (void) dmu_objset_destroy(buf);
2199 kmem_free(buf, len);
2200 return (err);
2204 * inputs:
2205 * zc_name name of filesystem
2206 * zc_value short name of snapshot
2207 * zc_cookie recursive flag
2209 * outputs: none
2211 static int
2212 zfs_ioc_snapshot(zfs_cmd_t *zc)
2214 nvlist_t *nvprops = NULL;
2215 int error;
2216 boolean_t recursive = zc->zc_cookie;
2218 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2219 return (EINVAL);
2221 if (zc->zc_nvlist_src != NULL &&
2222 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2223 &nvprops)) != 0)
2224 return (error);
2226 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
2229 * It would be nice to do this atomically.
2231 if (error == 0) {
2232 struct snap_prop_arg snpa;
2233 snpa.nvprops = nvprops;
2234 snpa.snapname = zc->zc_value;
2235 if (recursive) {
2236 error = dmu_objset_find(zc->zc_name,
2237 set_snap_props, &snpa, DS_FIND_CHILDREN);
2238 if (error) {
2239 (void) dmu_snapshots_destroy(zc->zc_name,
2240 zc->zc_value);
2242 } else {
2243 error = set_snap_props(zc->zc_name, &snpa);
2246 nvlist_free(nvprops);
2247 return (error);
2251 zfs_unmount_snap(char *name, void *arg)
2253 vfs_t *vfsp = NULL;
2255 if (arg) {
2256 char *snapname = arg;
2257 int len = strlen(name) + strlen(snapname) + 2;
2258 char *buf = kmem_alloc(len, KM_SLEEP);
2260 (void) strcpy(buf, name);
2261 (void) strcat(buf, "@");
2262 (void) strcat(buf, snapname);
2263 vfsp = zfs_get_vfs(buf);
2264 kmem_free(buf, len);
2265 } else if (strchr(name, '@')) {
2266 vfsp = zfs_get_vfs(name);
2269 if (vfsp) {
2270 #ifdef __NetBSD__
2271 int err;
2272 if ((err = dounmount(vfsp, MNT_FORCE, curlwp)) != 0)
2273 return (err);
2274 #else
2277 * Always force the unmount for snapshots.
2279 int flag = MS_FORCE;
2280 int err;
2282 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2283 VFS_RELE(vfsp);
2284 return (err);
2286 VFS_RELE(vfsp);
2287 if ((err = dounmount(vfsp, flag, kcred)) != 0)
2288 return (err);
2289 #endif
2291 return (0);
2295 * inputs:
2296 * zc_name name of filesystem
2297 * zc_value short name of snapshot
2299 * outputs: none
2301 static int
2302 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2304 int err;
2306 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2307 return (EINVAL);
2308 err = dmu_objset_find(zc->zc_name,
2309 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2310 if (err)
2311 return (err);
2312 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2316 * inputs:
2317 * zc_name name of dataset to destroy
2318 * zc_objset_type type of objset
2320 * outputs: none
2322 static int
2323 zfs_ioc_destroy(zfs_cmd_t *zc)
2325 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2326 int err = zfs_unmount_snap(zc->zc_name, NULL);
2327 if (err)
2328 return (err);
2331 return (dmu_objset_destroy(zc->zc_name));
2335 * inputs:
2336 * zc_name name of dataset to rollback (to most recent snapshot)
2338 * outputs: none
2340 static int
2341 zfs_ioc_rollback(zfs_cmd_t *zc)
2343 objset_t *os;
2344 int error;
2345 zfsvfs_t *zfsvfs = NULL;
2348 * Get the zfsvfs for the receiving objset. There
2349 * won't be one if we're operating on a zvol, if the
2350 * objset doesn't exist yet, or is not mounted.
2352 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
2353 if (error)
2354 return (error);
2356 if (dmu_objset_type(os) == DMU_OST_ZFS) {
2357 mutex_enter(&os->os->os_user_ptr_lock);
2358 zfsvfs = dmu_objset_get_user(os);
2359 if (zfsvfs != NULL)
2360 VFS_HOLD(zfsvfs->z_vfs);
2361 mutex_exit(&os->os->os_user_ptr_lock);
2364 if (zfsvfs != NULL) {
2365 char *osname;
2366 int mode;
2368 osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2369 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2370 if (error == 0) {
2371 int resume_err;
2373 ASSERT(strcmp(osname, zc->zc_name) == 0);
2374 error = dmu_objset_rollback(os);
2375 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2376 error = error ? error : resume_err;
2377 } else {
2378 dmu_objset_close(os);
2380 kmem_free(osname, MAXNAMELEN);
2381 VFS_RELE(zfsvfs->z_vfs);
2382 } else {
2383 error = dmu_objset_rollback(os);
2385 /* Note, the dmu_objset_rollback() releases the objset for us. */
2387 return (error);
2391 * inputs:
2392 * zc_name old name of dataset
2393 * zc_value new name of dataset
2394 * zc_cookie recursive flag (only valid for snapshots)
2396 * outputs: none
2398 static int
2399 zfs_ioc_rename(zfs_cmd_t *zc)
2401 boolean_t recursive = zc->zc_cookie & 1;
2403 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2404 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2405 strchr(zc->zc_value, '%'))
2406 return (EINVAL);
2409 * Unmount snapshot unless we're doing a recursive rename,
2410 * in which case the dataset code figures out which snapshots
2411 * to unmount.
2413 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2414 zc->zc_objset_type == DMU_OST_ZFS) {
2415 int err = zfs_unmount_snap(zc->zc_name, NULL);
2416 if (err)
2417 return (err);
2419 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2422 static void
2423 clear_props(char *dataset, nvlist_t *props)
2425 zfs_cmd_t *zc;
2426 nvpair_t *prop;
2428 if (props == NULL)
2429 return;
2430 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2431 (void) strcpy(zc->zc_name, dataset);
2432 for (prop = nvlist_next_nvpair(props, NULL); prop;
2433 prop = nvlist_next_nvpair(props, prop)) {
2434 (void) strcpy(zc->zc_value, nvpair_name(prop));
2435 if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2436 (void) zfs_ioc_inherit_prop(zc);
2438 kmem_free(zc, sizeof (zfs_cmd_t));
2442 * inputs:
2443 * zc_name name of containing filesystem
2444 * zc_nvlist_src{_size} nvlist of properties to apply
2445 * zc_value name of snapshot to create
2446 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2447 * zc_cookie file descriptor to recv from
2448 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2449 * zc_guid force flag
2451 * outputs:
2452 * zc_cookie number of bytes read
2454 static int
2455 zfs_ioc_recv(zfs_cmd_t *zc)
2457 file_t *fp;
2458 objset_t *os;
2459 dmu_recv_cookie_t drc;
2460 zfsvfs_t *zfsvfs = NULL;
2461 boolean_t force = (boolean_t)zc->zc_guid;
2462 int error, fd;
2463 offset_t off;
2464 nvlist_t *props = NULL;
2465 nvlist_t *origprops = NULL;
2466 objset_t *origin = NULL;
2467 char *tosnap;
2468 char tofs[ZFS_MAXNAMELEN];
2470 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2471 strchr(zc->zc_value, '@') == NULL ||
2472 strchr(zc->zc_value, '%'))
2473 return (EINVAL);
2475 (void) strcpy(tofs, zc->zc_value);
2476 tosnap = strchr(tofs, '@');
2477 *tosnap = '\0';
2478 tosnap++;
2480 if (zc->zc_nvlist_src != NULL &&
2481 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2482 &props)) != 0)
2483 return (error);
2485 fd = zc->zc_cookie;
2486 error = fd_getvnode(fd, &fp);
2487 if (error != 0) {
2488 nvlist_free(props);
2489 return (error);
2492 if (dmu_objset_open(tofs, DMU_OST_ANY,
2493 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2495 * Try to get the zfsvfs for the receiving objset.
2496 * There won't be one if we're operating on a zvol,
2497 * if the objset doesn't exist yet, or is not mounted.
2499 mutex_enter(&os->os->os_user_ptr_lock);
2500 if (zfsvfs = dmu_objset_get_user(os)) {
2501 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2502 mutex_exit(&os->os->os_user_ptr_lock);
2503 dmu_objset_close(os);
2504 zfsvfs = NULL;
2505 error = EBUSY;
2506 goto out;
2508 VFS_HOLD(zfsvfs->z_vfs);
2510 mutex_exit(&os->os->os_user_ptr_lock);
2513 * If new properties are supplied, they are to completely
2514 * replace the existing ones, so stash away the existing ones.
2516 if (props)
2517 (void) dsl_prop_get_all(os, &origprops, TRUE);
2519 dmu_objset_close(os);
2522 if (zc->zc_string[0]) {
2523 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2524 DS_MODE_USER | DS_MODE_READONLY, &origin);
2525 if (error)
2526 goto out;
2529 error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2530 force, origin, zfsvfs != NULL, &drc);
2531 if (origin)
2532 dmu_objset_close(origin);
2533 if (error)
2534 goto out;
2537 * Reset properties. We do this before we receive the stream
2538 * so that the properties are applied to the new data.
2540 if (props) {
2541 clear_props(tofs, origprops);
2543 * XXX - Note, this is all-or-nothing; should be best-effort.
2545 (void) zfs_set_prop_nvlist(tofs, props);
2548 off = fp->f_offset;
2549 error = dmu_recv_stream(&drc, fp->f_data, &off);
2551 if (error == 0 && zfsvfs) {
2552 char *osname;
2553 int mode;
2555 /* online recv */
2556 osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2557 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2558 if (error == 0) {
2559 int resume_err;
2561 error = dmu_recv_end(&drc);
2562 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2563 error = error ? error : resume_err;
2564 } else {
2565 dmu_recv_abort_cleanup(&drc);
2567 kmem_free(osname, MAXNAMELEN);
2568 } else if (error == 0) {
2569 error = dmu_recv_end(&drc);
2572 zc->zc_cookie = off - fp->f_offset;
2573 if (VOP_SEEK(fp->f_data, fp->f_offset, &off, NULL) == 0)
2574 fp->f_offset = off;
2577 * On error, restore the original props.
2579 if (error && props) {
2580 clear_props(tofs, props);
2581 (void) zfs_set_prop_nvlist(tofs, origprops);
2583 out:
2584 if (zfsvfs) {
2585 mutex_exit(&zfsvfs->z_online_recv_lock);
2586 VFS_RELE(zfsvfs->z_vfs);
2588 nvlist_free(props);
2589 nvlist_free(origprops);
2590 fd_putfile(fd);
2591 return (error);
2595 * inputs:
2596 * zc_name name of snapshot to send
2597 * zc_value short name of incremental fromsnap (may be empty)
2598 * zc_cookie file descriptor to send stream to
2599 * zc_obj fromorigin flag (mutually exclusive with zc_value)
2601 * outputs: none
2603 static int
2604 zfs_ioc_send(zfs_cmd_t *zc)
2606 objset_t *fromsnap = NULL;
2607 objset_t *tosnap;
2608 file_t *fp;
2609 int error;
2610 offset_t off;
2612 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2613 DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2614 if (error)
2615 return (error);
2617 if (zc->zc_value[0] != '\0') {
2618 char *buf;
2619 char *cp;
2621 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2622 (void) strncpy(buf, zc->zc_name, MAXPATHLEN);
2623 cp = strchr(buf, '@');
2624 if (cp)
2625 *(cp+1) = 0;
2626 (void) strncat(buf, zc->zc_value, MAXPATHLEN);
2627 error = dmu_objset_open(buf, DMU_OST_ANY,
2628 DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2629 kmem_free(buf, MAXPATHLEN);
2630 if (error) {
2631 dmu_objset_close(tosnap);
2632 return (error);
2636 error = fd_getvnode(zc->zc_cookie, &fp);
2637 if (error != 0) {
2638 dmu_objset_close(tosnap);
2639 if (fromsnap)
2640 dmu_objset_close(fromsnap);
2641 return (error);
2644 off = fp->f_offset;
2645 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_data, &off);
2647 if (VOP_SEEK(fp->f_data, fp->f_offset, &off, NULL) == 0)
2648 fp->f_offset = off;
2649 fd_putfile(zc->zc_cookie);
2650 if (fromsnap)
2651 dmu_objset_close(fromsnap);
2652 dmu_objset_close(tosnap);
2653 return (error);
2656 static int
2657 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2659 int id, error;
2661 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2662 &zc->zc_inject_record);
2664 if (error == 0)
2665 zc->zc_guid = (uint64_t)id;
2667 return (error);
2670 static int
2671 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2673 return (zio_clear_fault((int)zc->zc_guid));
2676 static int
2677 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2679 int id = (int)zc->zc_guid;
2680 int error;
2682 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2683 &zc->zc_inject_record);
2685 zc->zc_guid = id;
2687 return (error);
2690 static int
2691 zfs_ioc_error_log(zfs_cmd_t *zc)
2693 spa_t *spa;
2694 int error;
2695 size_t count = (size_t)zc->zc_nvlist_dst_size;
2697 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2698 return (error);
2700 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2701 &count);
2702 if (error == 0)
2703 zc->zc_nvlist_dst_size = count;
2704 else
2705 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2707 spa_close(spa, FTAG);
2709 return (error);
2712 static int
2713 zfs_ioc_clear(zfs_cmd_t *zc)
2715 spa_t *spa;
2716 vdev_t *vd;
2717 int error;
2720 * On zpool clear we also fix up missing slogs
2722 mutex_enter(&spa_namespace_lock);
2723 spa = spa_lookup(zc->zc_name);
2724 if (spa == NULL) {
2725 mutex_exit(&spa_namespace_lock);
2726 return (EIO);
2728 if (spa->spa_log_state == SPA_LOG_MISSING) {
2729 /* we need to let spa_open/spa_load clear the chains */
2730 spa->spa_log_state = SPA_LOG_CLEAR;
2732 mutex_exit(&spa_namespace_lock);
2734 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2735 return (error);
2737 spa_vdev_state_enter(spa);
2739 if (zc->zc_guid == 0) {
2740 vd = NULL;
2741 } else {
2742 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
2743 if (vd == NULL) {
2744 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
2745 spa_close(spa, FTAG);
2746 return (ENODEV);
2750 vdev_clear(spa, vd);
2752 (void) spa_vdev_state_exit(spa, NULL, 0);
2755 * Resume any suspended I/Os.
2757 zio_resume(spa);
2759 spa_close(spa, FTAG);
2761 return (0);
2765 * inputs:
2766 * zc_name name of filesystem
2767 * zc_value name of origin snapshot
2769 * outputs: none
2771 static int
2772 zfs_ioc_promote(zfs_cmd_t *zc)
2774 char *cp;
2777 * We don't need to unmount *all* the origin fs's snapshots, but
2778 * it's easier.
2780 cp = strchr(zc->zc_value, '@');
2781 if (cp)
2782 *cp = '\0';
2783 (void) dmu_objset_find(zc->zc_value,
2784 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2785 return (dsl_dataset_promote(zc->zc_name));
2789 * We don't want to have a hard dependency
2790 * against some special symbols in sharefs
2791 * nfs, and smbsrv. Determine them if needed when
2792 * the first file system is shared.
2793 * Neither sharefs, nfs or smbsrv are unloadable modules.
2795 #ifdef __NetBSD__
2797 static int
2798 zfs_ioc_share(zfs_cmd_t *zc)
2801 return EOPNOTSUPP;
2804 #else /* __NetBSD__ */
2806 int (*znfsexport_fs)(void *arg);
2807 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2808 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2810 int zfs_nfsshare_inited;
2811 int zfs_smbshare_inited;
2813 ddi_modhandle_t nfs_mod;
2814 ddi_modhandle_t sharefs_mod;
2815 ddi_modhandle_t smbsrv_mod;
2816 kmutex_t zfs_share_lock;
2818 static int
2819 zfs_init_sharefs()
2821 int error;
2823 ASSERT(MUTEX_HELD(&zfs_share_lock));
2824 /* Both NFS and SMB shares also require sharetab support. */
2825 if (sharefs_mod == NULL && ((sharefs_mod =
2826 ddi_modopen("fs/sharefs",
2827 KRTLD_MODE_FIRST, &error)) == NULL)) {
2828 return (ENOSYS);
2830 if (zshare_fs == NULL && ((zshare_fs =
2831 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2832 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2833 return (ENOSYS);
2835 return (0);
2838 static int
2839 zfs_ioc_share(zfs_cmd_t *zc)
2841 int error;
2842 int opcode;
2844 switch (zc->zc_share.z_sharetype) {
2845 case ZFS_SHARE_NFS:
2846 case ZFS_UNSHARE_NFS:
2847 if (zfs_nfsshare_inited == 0) {
2848 mutex_enter(&zfs_share_lock);
2849 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2850 KRTLD_MODE_FIRST, &error)) == NULL)) {
2851 mutex_exit(&zfs_share_lock);
2852 return (ENOSYS);
2854 if (znfsexport_fs == NULL &&
2855 ((znfsexport_fs = (int (*)(void *))
2856 ddi_modsym(nfs_mod,
2857 "nfs_export", &error)) == NULL)) {
2858 mutex_exit(&zfs_share_lock);
2859 return (ENOSYS);
2861 error = zfs_init_sharefs();
2862 if (error) {
2863 mutex_exit(&zfs_share_lock);
2864 return (ENOSYS);
2866 zfs_nfsshare_inited = 1;
2867 mutex_exit(&zfs_share_lock);
2869 break;
2870 case ZFS_SHARE_SMB:
2871 case ZFS_UNSHARE_SMB:
2872 if (zfs_smbshare_inited == 0) {
2873 mutex_enter(&zfs_share_lock);
2874 if (smbsrv_mod == NULL && ((smbsrv_mod =
2875 ddi_modopen("drv/smbsrv",
2876 KRTLD_MODE_FIRST, &error)) == NULL)) {
2877 mutex_exit(&zfs_share_lock);
2878 return (ENOSYS);
2880 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2881 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2882 "smb_server_share", &error)) == NULL)) {
2883 mutex_exit(&zfs_share_lock);
2884 return (ENOSYS);
2886 error = zfs_init_sharefs();
2887 if (error) {
2888 mutex_exit(&zfs_share_lock);
2889 return (ENOSYS);
2891 zfs_smbshare_inited = 1;
2892 mutex_exit(&zfs_share_lock);
2894 break;
2895 default:
2896 return (EINVAL);
2899 switch (zc->zc_share.z_sharetype) {
2900 case ZFS_SHARE_NFS:
2901 case ZFS_UNSHARE_NFS:
2902 if (error =
2903 znfsexport_fs((void *)
2904 (uintptr_t)zc->zc_share.z_exportdata))
2905 return (error);
2906 break;
2907 case ZFS_SHARE_SMB:
2908 case ZFS_UNSHARE_SMB:
2909 if (error = zsmbexport_fs((void *)
2910 (uintptr_t)zc->zc_share.z_exportdata,
2911 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2912 B_TRUE : B_FALSE)) {
2913 return (error);
2915 break;
2918 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2919 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2920 SHAREFS_ADD : SHAREFS_REMOVE;
2923 * Add or remove share from sharetab
2925 error = zshare_fs(opcode,
2926 (void *)(uintptr_t)zc->zc_share.z_sharedata,
2927 zc->zc_share.z_sharemax);
2929 return (error);
2932 #endif /* __NetBSD__ */
2935 * pool create, destroy, and export don't log the history as part of
2936 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2937 * do the logging of those commands.
2939 static zfs_ioc_vec_t zfs_ioc_vec[] = {
2940 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE }, /* 0 */
2941 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2942 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2943 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2944 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE },
2945 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE }, /* 5 */
2946 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2947 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2948 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2949 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2950 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE }, /* 10 */
2951 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2952 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2953 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2954 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2955 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE }, /* 15 */
2956 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2957 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2958 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2959 { zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2960 DATASET_NAME, B_FALSE },
2961 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2962 DATASET_NAME, B_FALSE }, /* 20*/
2963 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2964 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2965 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2966 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2967 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE }, /* 25 */
2968 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2969 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE },
2970 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2971 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2972 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE }, /* 30 */
2973 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2974 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2975 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2976 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2977 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE }, /* 35 */
2978 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2979 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2980 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2981 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2982 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE }, /* 40 */
2983 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2984 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2985 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2986 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2987 DATASET_NAME, B_FALSE },
2988 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2989 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE }, /* 46 */
2992 static int
2993 zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2995 zfs_cmd_t *zc;
2996 uint_t vec;
2997 int error, rc;
2999 dprintf("zfsdev_ioctl called \n");
3001 if (getminor(dev) != 0)
3002 return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp));
3003 dprintf("zfsdev_ioctl -> zvol_ioctl\n");
3004 vec = cmd - ZFS_IOC;
3005 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
3007 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3008 return (EINVAL);
3010 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
3012 error = xcopyin((void *)arg, zc, sizeof (zfs_cmd_t));
3013 dprintf("zfsdev_ioct zc_value %s, zc_string\n", zc->zc_value, zc->zc_string);
3014 dprintf("zfsdev_ioctl -> calling zfs_ioc_vec secpolicy function on %d\n", vec);
3015 if (error == 0)
3016 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr);
3019 * Ensure that all pool/dataset names are valid before we pass down to
3020 * the lower layers.
3022 if (error == 0) {
3023 dprintf("zfsdev_ioctl, zc->zc_name %s\n", zc->zc_name);
3024 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3025 switch (zfs_ioc_vec[vec].zvec_namecheck) {
3026 case POOL_NAME:
3027 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3028 error = EINVAL;
3029 break;
3031 case DATASET_NAME:
3032 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3033 error = EINVAL;
3034 break;
3036 case NO_NAME:
3037 break;
3041 dprintf("zfsdev_ioctl -> calling zfs_ioc_vec zvec_func on %d\n", vec);
3042 if (error == 0)
3043 error = zfs_ioc_vec[vec].zvec_func(zc);
3045 rc = xcopyout(zc, (void *)arg, sizeof (zfs_cmd_t));
3046 if (error == 0) {
3047 error = rc;
3048 if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
3049 zfs_log_history(zc);
3052 kmem_free(zc, sizeof (zfs_cmd_t));
3053 return (error);
3056 #ifdef __NetBSD__
3058 #include <sys/module.h>
3059 #include <uvm/uvm_extern.h>
3061 MODULE(MODULE_CLASS_VFS, zfs, "solaris");
3063 static int
3064 nb_zvol_copen(dev_t dev, int flag, int mode, lwp_t *l)
3067 return zvol_open(&dev, flag, OTYPCHR, kauth_cred_get());
3070 static int
3071 nb_zvol_cclose(dev_t dev, int flag, int mode, lwp_t *l)
3074 return zvol_close(dev, flag, OTYPCHR, kauth_cred_get());
3077 static int
3078 nb_zvol_bopen(dev_t dev, int flag, int mode, lwp_t *l)
3081 return zvol_open(&dev, flag, OTYPBLK, kauth_cred_get());
3084 static int
3085 nb_zvol_bclose(dev_t dev, int flag, int mode, lwp_t *l)
3088 return zvol_close(dev, flag, OTYPBLK, kauth_cred_get());
3091 static int
3092 nb_zvol_read(dev_t dev, struct uio *uio, int flag)
3095 return zvol_read(dev, uio, kauth_cred_get());
3098 static int
3099 nb_zvol_write(dev_t dev, struct uio *uio, int flag)
3102 return zvol_write(dev, uio, kauth_cred_get());
3105 static int
3106 nb_zfsdev_ioctl(dev_t dev, u_long cmd, void *argp, int flag, lwp_t *l)
3108 int rval;
3110 return zfsdev_ioctl(dev, cmd, (intptr_t)argp, flag, kauth_cred_get(),
3111 &rval);
3114 const struct bdevsw zfs_bdevsw = {
3115 .d_open = nb_zvol_bopen,
3116 .d_close = nb_zvol_bclose,
3117 .d_strategy = zvol_strategy,
3118 .d_ioctl = nb_zfsdev_ioctl,
3119 .d_dump = nodump,
3120 .d_psize = nosize,
3121 .d_flag = D_DISK | D_MPSAFE
3124 const struct cdevsw zfs_cdevsw = {
3125 .d_open = nb_zvol_copen,
3126 .d_close = nb_zvol_cclose,
3127 .d_read = nb_zvol_read,
3128 .d_write = nb_zvol_write,
3129 .d_ioctl = nb_zfsdev_ioctl,
3130 .d_stop = nostop,
3131 .d_tty = notty,
3132 .d_poll = nopoll,
3133 .d_mmap = nommap,
3134 .d_kqfilter = nokqfilter,
3135 .d_flag = D_DISK | D_MPSAFE
3138 uint_t zfs_fsyncer_key;
3139 extern uint_t rrw_tsd_key;
3141 /* ZFS must be used on machines with at least 512Mb. */
3142 #define ZFS_MIN_MEGS 512
3144 static int
3145 zfs_modcmd(modcmd_t cmd, void *arg)
3147 int error;
3148 int active, inactive;
3149 uint64_t availrmem;
3151 switch (cmd) {
3152 case MODULE_CMD_INIT:
3153 printf("WARNING: ZFS on NetBSD is under development\n");
3154 availrmem = (uint64_t)physmem * PAGE_SIZE / 1048576;
3155 if (availrmem < ZFS_MIN_MEGS * 80 / 100) {
3156 printf("ERROR: at least %dMB of memory required to"
3157 "use ZFS\n", ZFS_MIN_MEGS);
3158 return ENOMEM;
3160 error = lwp_specific_key_create(&zfs_fsyncer_key, NULL);
3161 if (error != 0) {
3162 return error;
3164 error = lwp_specific_key_create(&rrw_tsd_key, NULL);
3165 if (error != 0) {
3166 lwp_specific_key_delete(zfs_fsyncer_key);
3167 return error;
3169 spa_init(FREAD | FWRITE);
3170 zfs_init();
3171 zvol_init();
3172 zfs_vfsinit(16, MOUNT_ZFS); /* I need to use well defined args. */
3173 error = devsw_attach("zfs", &zfs_bdevsw, &zfs_bmajor,
3174 &zfs_cdevsw, &zfs_cmajor);
3175 if (error != 0) {
3176 zvol_fini();
3177 zfs_fini();
3178 spa_fini();
3179 lwp_specific_key_delete(zfs_fsyncer_key);
3180 lwp_specific_key_delete(rrw_tsd_key);
3182 return error;
3184 case MODULE_CMD_FINI:
3185 if (spa_busy() || zfs_busy() || zvol_busy() ||
3186 zio_injection_enabled)
3187 return EBUSY;
3188 error = devsw_detach(&zfs_bdevsw, &zfs_cdevsw);
3189 zvol_fini();
3190 zfs_vfsfini();
3191 zfs_fini();
3192 spa_fini();
3193 lwp_specific_key_delete(zfs_fsyncer_key);
3194 lwp_specific_key_delete(rrw_tsd_key);
3195 return error;
3197 case MODULE_CMD_AUTOUNLOAD:
3199 * We don't want to be autounloaded because unlike
3200 * other subsystems, we read our own configuration
3201 * from disk and provide things that might be used
3202 * later (zvols).
3204 return EBUSY;
3206 default:
3207 return ENOTTY;
3211 #else /* __NetBSD__ */
3214 _fini(void)
3216 int error;
3218 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3219 return (EBUSY);
3221 if ((error = mod_remove(&modlinkage)) != 0)
3222 return (error);
3224 zvol_fini();
3225 zfs_fini();
3226 spa_fini();
3227 if (zfs_nfsshare_inited)
3228 (void) ddi_modclose(nfs_mod);
3229 if (zfs_smbshare_inited)
3230 (void) ddi_modclose(smbsrv_mod);
3231 if (zfs_nfsshare_inited || zfs_smbshare_inited)
3232 (void) ddi_modclose(sharefs_mod);
3234 tsd_destroy(&zfs_fsyncer_key);
3235 ldi_ident_release(zfs_li);
3236 zfs_li = NULL;
3237 mutex_destroy(&zfs_share_lock);
3239 return (error);
3242 static int
3243 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3245 if (cmd != DDI_ATTACH)
3246 return (DDI_FAILURE);
3248 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
3249 DDI_PSEUDO, 0) == DDI_FAILURE)
3250 return (DDI_FAILURE);
3252 zfs_dip = dip;
3254 ddi_report_dev(dip);
3256 return (DDI_SUCCESS);
3259 static int
3260 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3262 if (spa_busy() || zfs_busy() || zvol_busy())
3263 return (DDI_FAILURE);
3265 if (cmd != DDI_DETACH)
3266 return (DDI_FAILURE);
3268 zfs_dip = NULL;
3270 ddi_prop_remove_all(dip);
3271 ddi_remove_minor_node(dip, NULL);
3273 return (DDI_SUCCESS);
3276 /*ARGSUSED*/
3277 static int
3278 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3280 switch (infocmd) {
3281 case DDI_INFO_DEVT2DEVINFO:
3282 *result = zfs_dip;
3283 return (DDI_SUCCESS);
3285 case DDI_INFO_DEVT2INSTANCE:
3286 *result = (void *)0;
3287 return (DDI_SUCCESS);
3290 return (DDI_FAILURE);
3294 * OK, so this is a little weird.
3296 * /dev/zfs is the control node, i.e. minor 0.
3297 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3299 * /dev/zfs has basically nothing to do except serve up ioctls,
3300 * so most of the standard driver entry points are in zvol.c.
3302 static struct cb_ops zfs_cb_ops = {
3303 zvol_open, /* open */
3304 zvol_close, /* close */
3305 zvol_strategy, /* strategy */
3306 nodev, /* print */
3307 zvol_dump, /* dump */
3308 zvol_read, /* read */
3309 zvol_write, /* write */
3310 zfsdev_ioctl, /* ioctl */
3311 nodev, /* devmap */
3312 nodev, /* mmap */
3313 nodev, /* segmap */
3314 nochpoll, /* poll */
3315 ddi_prop_op, /* prop_op */
3316 NULL, /* streamtab */
3317 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
3318 CB_REV, /* version */
3319 nodev, /* async read */
3320 nodev, /* async write */
3323 static struct dev_ops zfs_dev_ops = {
3324 DEVO_REV, /* version */
3325 0, /* refcnt */
3326 zfs_info, /* info */
3327 nulldev, /* identify */
3328 nulldev, /* probe */
3329 zfs_attach, /* attach */
3330 zfs_detach, /* detach */
3331 nodev, /* reset */
3332 &zfs_cb_ops, /* driver operations */
3333 NULL, /* no bus operations */
3334 NULL, /* power */
3335 ddi_quiesce_not_needed, /* quiesce */
3338 static struct modldrv zfs_modldrv = {
3339 &mod_driverops,
3340 "ZFS storage pool",
3341 &zfs_dev_ops
3344 static struct modlinkage modlinkage = {
3345 MODREV_1,
3346 (void *)&zfs_modlfs,
3347 (void *)&zfs_modldrv,
3348 NULL
3352 uint_t zfs_fsyncer_key;
3353 extern uint_t rrw_tsd_key;
3356 _init(void)
3358 int error;
3360 spa_init(FREAD | FWRITE);
3361 zfs_init();
3362 zvol_init();
3364 if ((error = mod_install(&modlinkage)) != 0) {
3365 zvol_fini();
3366 zfs_fini();
3367 spa_fini();
3368 return (error);
3371 tsd_create(&zfs_fsyncer_key, NULL);
3372 tsd_create(&rrw_tsd_key, NULL);
3374 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
3375 ASSERT(error == 0);
3376 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3378 return (0);
3382 _fini(void)
3384 int error;
3386 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
3387 return (EBUSY);
3389 if ((error = mod_remove(&modlinkage)) != 0)
3390 return (error);
3392 zvol_fini();
3393 zfs_fini();
3394 spa_fini();
3395 if (zfs_nfsshare_inited)
3396 (void) ddi_modclose(nfs_mod);
3397 if (zfs_smbshare_inited)
3398 (void) ddi_modclose(smbsrv_mod);
3399 if (zfs_nfsshare_inited || zfs_smbshare_inited)
3400 (void) ddi_modclose(sharefs_mod);
3402 tsd_destroy(&zfs_fsyncer_key);
3403 ldi_ident_release(zfs_li);
3404 zfs_li = NULL;
3405 mutex_destroy(&zfs_share_lock);
3407 return (error);
3411 _info(struct modinfo *modinfop)
3413 return (mod_info(&modlinkage, modinfop));
3415 #endif /* __NetBSD__ */