Deleted Added
sdiff udiff text old ( 207626 ) new ( 207670 )
full compact
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#include <sys/types.h>
27#include <sys/param.h>
28#include <sys/systm.h>
29#include <sys/conf.h>
30#include <sys/kernel.h>
31#include <sys/lock.h>
32#include <sys/malloc.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/errno.h>
36#include <sys/uio.h>
37#include <sys/buf.h>
38#include <sys/file.h>
39#include <sys/kmem.h>
40#include <sys/conf.h>
41#include <sys/cmn_err.h>
42#include <sys/stat.h>
43#include <sys/zfs_ioctl.h>
44#include <sys/zfs_znode.h>
45#include <sys/zap.h>
46#include <sys/spa.h>
47#include <sys/spa_impl.h>
48#include <sys/vdev.h>
49#include <sys/vdev_impl.h>
50#include <sys/dmu.h>
51#include <sys/dsl_dir.h>
52#include <sys/dsl_dataset.h>
53#include <sys/dsl_prop.h>
54#include <sys/dsl_deleg.h>
55#include <sys/dmu_objset.h>
56#include <sys/sunddi.h>
57#include <sys/policy.h>
58#include <sys/zone.h>
59#include <sys/nvpair.h>
60#include <sys/mount.h>
61#include <sys/taskqueue.h>
62#include <sys/sdt.h>
63#include <sys/varargs.h>
64#include <sys/fs/zfs.h>
65#include <sys/zfs_ctldir.h>
66#include <sys/zfs_dir.h>
67#include <sys/zvol.h>
68#include <sys/dmu_objset.h>
69
70#include "zfs_namecheck.h"
71#include "zfs_prop.h"
72#include "zfs_deleg.h"
73
74CTASSERT(sizeof(zfs_cmd_t) <= PAGE_SIZE);
75
76static struct cdev *zfsdev;
77
78extern void zfs_init(void);
79extern void zfs_fini(void);
80
81typedef int zfs_ioc_func_t(zfs_cmd_t *);
82typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
83
84typedef struct zfs_ioc_vec {
85 zfs_ioc_func_t *zvec_func;
86 zfs_secpolicy_func_t *zvec_secpolicy;
87 enum {
88 NO_NAME,
89 POOL_NAME,
90 DATASET_NAME
91 } zvec_namecheck;
92 boolean_t zvec_his_log;
93} zfs_ioc_vec_t;
94
95static void clear_props(char *dataset, nvlist_t *props);
96static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
97 boolean_t *);
98int zfs_set_prop_nvlist(const char *, nvlist_t *);
99
100/* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
101void
102__dprintf(const char *file, const char *func, int line, const char *fmt, ...)
103{
104 const char *newfile;
105 char buf[256];
106 va_list adx;
107
108 /*
109 * Get rid of annoying "../common/" prefix to filename.
110 */
111 newfile = strrchr(file, '/');
112 if (newfile != NULL) {
113 newfile = newfile + 1; /* Get rid of leading / */
114 } else {
115 newfile = file;
116 }
117
118 va_start(adx, fmt);
119 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
120 va_end(adx);
121
122 /*
123 * To get this data, use the zfs-dprintf probe as so:
124 * dtrace -q -n 'zfs-dprintf \
125 * /stringof(arg0) == "dbuf.c"/ \
126 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
127 * arg0 = file name
128 * arg1 = function name
129 * arg2 = line number
130 * arg3 = message
131 */
132 DTRACE_PROBE4(zfs__dprintf,
133 char *, newfile, char *, func, int, line, char *, buf);
134}
135
136static void
137history_str_free(char *buf)
138{
139 kmem_free(buf, HIS_MAX_RECORD_LEN);
140}
141
142static char *
143history_str_get(zfs_cmd_t *zc)
144{
145 char *buf;
146
147 if (zc->zc_history == 0)
148 return (NULL);
149
150 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
151 if (copyinstr((void *)(uintptr_t)zc->zc_history,
152 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
153 history_str_free(buf);
154 return (NULL);
155 }
156
157 buf[HIS_MAX_RECORD_LEN -1] = '\0';
158
159 return (buf);
160}
161
162/*
163 * Check to see if the named dataset is currently defined as bootable
164 */
165static boolean_t
166zfs_is_bootfs(const char *name)
167{
168 spa_t *spa;
169 boolean_t ret = B_FALSE;
170
171 if (spa_open(name, &spa, FTAG) == 0) {
172 if (spa->spa_bootfs) {
173 objset_t *os;
174
175 if (dmu_objset_open(name, DMU_OST_ZFS,
176 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
177 ret = (dmu_objset_id(os) == spa->spa_bootfs);
178 dmu_objset_close(os);
179 }
180 }
181 spa_close(spa, FTAG);
182 }
183 return (ret);
184}
185
186/*
187 * zfs_earlier_version
188 *
189 * Return non-zero if the spa version is less than requested version.
190 */
191static int
192zfs_earlier_version(const char *name, int version)
193{
194 spa_t *spa;
195
196 if (spa_open(name, &spa, FTAG) == 0) {
197 if (spa_version(spa) < version) {
198 spa_close(spa, FTAG);
199 return (1);
200 }
201 spa_close(spa, FTAG);
202 }
203 return (0);
204}
205
206/*
207 * zpl_earlier_version
208 *
209 * Return TRUE if the ZPL version is less than requested version.
210 */
211static boolean_t
212zpl_earlier_version(const char *name, int version)
213{
214 objset_t *os;
215 boolean_t rc = B_TRUE;
216
217 if (dmu_objset_open(name, DMU_OST_ANY,
218 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
219 uint64_t zplversion;
220
221 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
222 rc = zplversion < version;
223 dmu_objset_close(os);
224 }
225 return (rc);
226}
227
228static void
229zfs_log_history(zfs_cmd_t *zc)
230{
231 spa_t *spa;
232 char *buf;
233
234 if ((buf = history_str_get(zc)) == NULL)
235 return;
236
237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
238 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
239 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
240 spa_close(spa, FTAG);
241 }
242 history_str_free(buf);
243}
244
245/*
246 * Policy for top-level read operations (list pools). Requires no privileges,
247 * and can be used in the local zone, as there is no associated dataset.
248 */
249/* ARGSUSED */
250static int
251zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
252{
253 return (0);
254}
255
256/*
257 * Policy for dataset read operations (list children, get statistics). Requires
258 * no privileges, but must be visible in the local zone.
259 */
260/* ARGSUSED */
261static int
262zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
263{
264 if (INGLOBALZONE(curthread) ||
265 zone_dataset_visible(zc->zc_name, NULL))
266 return (0);
267
268 return (ENOENT);
269}
270
271static int
272zfs_dozonecheck(const char *dataset, cred_t *cr)
273{
274 uint64_t zoned;
275 int writable = 1;
276
277 /*
278 * The dataset must be visible by this zone -- check this first
279 * so they don't see EPERM on something they shouldn't know about.
280 */
281 if (!INGLOBALZONE(curthread) &&
282 !zone_dataset_visible(dataset, &writable))
283 return (ENOENT);
284
285 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
286 return (ENOENT);
287
288 if (INGLOBALZONE(curthread)) {
289 /*
290 * If the fs is zoned, only root can access it from the
291 * global zone.
292 */
293 if (secpolicy_zfs(cr) && zoned)
294 return (EPERM);
295 } else {
296 /*
297 * If we are in a local zone, the 'zoned' property must be set.
298 */
299 if (!zoned)
300 return (EPERM);
301
302 /* must be writable by this zone */
303 if (!writable)
304 return (EPERM);
305 }
306 return (0);
307}
308
309int
310zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
311{
312 int error;
313
314 error = zfs_dozonecheck(name, cr);
315 if (error == 0) {
316 error = secpolicy_zfs(cr);
317 if (error)
318 error = dsl_deleg_access(name, perm, cr);
319 }
320 return (error);
321}
322
323static int
324zfs_secpolicy_setprop(const char *name, zfs_prop_t prop, cred_t *cr)
325{
326 /*
327 * Check permissions for special properties.
328 */
329 switch (prop) {
330 case ZFS_PROP_ZONED:
331 /*
332 * Disallow setting of 'zoned' from within a local zone.
333 */
334 if (!INGLOBALZONE(curthread))
335 return (EPERM);
336 break;
337
338 case ZFS_PROP_QUOTA:
339 if (!INGLOBALZONE(curthread)) {
340 uint64_t zoned;
341 char setpoint[MAXNAMELEN];
342 /*
343 * Unprivileged users are allowed to modify the
344 * quota on things *under* (ie. contained by)
345 * the thing they own.
346 */
347 if (dsl_prop_get_integer(name, "zoned", &zoned,
348 setpoint))
349 return (EPERM);
350 if (!zoned || strlen(name) <= strlen(setpoint))
351 return (EPERM);
352 }
353 break;
354 }
355
356 return (zfs_secpolicy_write_perms(name, zfs_prop_to_name(prop), cr));
357}
358
359int
360zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
361{
362 int error;
363
364 error = zfs_dozonecheck(zc->zc_name, cr);
365 if (error)
366 return (error);
367
368 /*
369 * permission to set permissions will be evaluated later in
370 * dsl_deleg_can_allow()
371 */
372 return (0);
373}
374
375int
376zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
377{
378 int error;
379 error = zfs_secpolicy_write_perms(zc->zc_name,
380 ZFS_DELEG_PERM_ROLLBACK, cr);
381 if (error == 0)
382 error = zfs_secpolicy_write_perms(zc->zc_name,
383 ZFS_DELEG_PERM_MOUNT, cr);
384 return (error);
385}
386
387int
388zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
389{
390 return (zfs_secpolicy_write_perms(zc->zc_name,
391 ZFS_DELEG_PERM_SEND, cr));
392}
393
394int
395zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
396{
397 if (!INGLOBALZONE(curthread))
398 return (EPERM);
399
400 if (secpolicy_nfs(cr) == 0) {
401 return (0);
402 } else {
403 vnode_t *vp;
404 int error;
405
406 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
407 NO_FOLLOW, NULL, &vp)) != 0)
408 return (error);
409
410 /* Now make sure mntpnt and dataset are ZFS */
411
412 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
413 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
414 zc->zc_name) != 0)) {
415 VN_RELE(vp);
416 return (EPERM);
417 }
418
419 VN_RELE(vp);
420 return (dsl_deleg_access(zc->zc_name,
421 ZFS_DELEG_PERM_SHARE, cr));
422 }
423}
424
425static int
426zfs_get_parent(const char *datasetname, char *parent, int parentsize)
427{
428 char *cp;
429
430 /*
431 * Remove the @bla or /bla from the end of the name to get the parent.
432 */
433 (void) strncpy(parent, datasetname, parentsize);
434 cp = strrchr(parent, '@');
435 if (cp != NULL) {
436 cp[0] = '\0';
437 } else {
438 cp = strrchr(parent, '/');
439 if (cp == NULL)
440 return (ENOENT);
441 cp[0] = '\0';
442 }
443
444 return (0);
445}
446
447int
448zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
449{
450 int error;
451
452 if ((error = zfs_secpolicy_write_perms(name,
453 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
454 return (error);
455
456 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
457}
458
459static int
460zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
461{
462 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
463}
464
465/*
466 * Must have sys_config privilege to check the iscsi permission
467 */
468/* ARGSUSED */
469static int
470zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr)
471{
472 return (secpolicy_zfs(cr));
473}
474
475int
476zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
477{
478 char parentname[MAXNAMELEN];
479 int error;
480
481 if ((error = zfs_secpolicy_write_perms(from,
482 ZFS_DELEG_PERM_RENAME, cr)) != 0)
483 return (error);
484
485 if ((error = zfs_secpolicy_write_perms(from,
486 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
487 return (error);
488
489 if ((error = zfs_get_parent(to, parentname,
490 sizeof (parentname))) != 0)
491 return (error);
492
493 if ((error = zfs_secpolicy_write_perms(parentname,
494 ZFS_DELEG_PERM_CREATE, cr)) != 0)
495 return (error);
496
497 if ((error = zfs_secpolicy_write_perms(parentname,
498 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
499 return (error);
500
501 return (error);
502}
503
504static int
505zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
506{
507 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
508}
509
510static int
511zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
512{
513 char parentname[MAXNAMELEN];
514 objset_t *clone;
515 int error;
516
517 error = zfs_secpolicy_write_perms(zc->zc_name,
518 ZFS_DELEG_PERM_PROMOTE, cr);
519 if (error)
520 return (error);
521
522 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
523 DS_MODE_USER | DS_MODE_READONLY, &clone);
524
525 if (error == 0) {
526 dsl_dataset_t *pclone = NULL;
527 dsl_dir_t *dd;
528 dd = clone->os->os_dsl_dataset->ds_dir;
529
530 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
531 error = dsl_dataset_hold_obj(dd->dd_pool,
532 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
533 rw_exit(&dd->dd_pool->dp_config_rwlock);
534 if (error) {
535 dmu_objset_close(clone);
536 return (error);
537 }
538
539 error = zfs_secpolicy_write_perms(zc->zc_name,
540 ZFS_DELEG_PERM_MOUNT, cr);
541
542 dsl_dataset_name(pclone, parentname);
543 dmu_objset_close(clone);
544 dsl_dataset_rele(pclone, FTAG);
545 if (error == 0)
546 error = zfs_secpolicy_write_perms(parentname,
547 ZFS_DELEG_PERM_PROMOTE, cr);
548 }
549 return (error);
550}
551
552static int
553zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
554{
555 int error;
556
557 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
558 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
559 return (error);
560
561 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
562 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
563 return (error);
564
565 return (zfs_secpolicy_write_perms(zc->zc_name,
566 ZFS_DELEG_PERM_CREATE, cr));
567}
568
569int
570zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
571{
572 int error;
573
574 if ((error = zfs_secpolicy_write_perms(name,
575 ZFS_DELEG_PERM_SNAPSHOT, cr)) != 0)
576 return (error);
577
578 error = zfs_secpolicy_write_perms(name,
579 ZFS_DELEG_PERM_MOUNT, cr);
580
581 return (error);
582}
583
584static int
585zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
586{
587
588 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
589}
590
591static int
592zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
593{
594 char parentname[MAXNAMELEN];
595 int error;
596
597 if ((error = zfs_get_parent(zc->zc_name, parentname,
598 sizeof (parentname))) != 0)
599 return (error);
600
601 if (zc->zc_value[0] != '\0') {
602 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
603 ZFS_DELEG_PERM_CLONE, cr)) != 0)
604 return (error);
605 }
606
607 if ((error = zfs_secpolicy_write_perms(parentname,
608 ZFS_DELEG_PERM_CREATE, cr)) != 0)
609 return (error);
610
611 error = zfs_secpolicy_write_perms(parentname,
612 ZFS_DELEG_PERM_MOUNT, cr);
613
614 return (error);
615}
616
617static int
618zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr)
619{
620 int error;
621
622 error = secpolicy_fs_unmount(cr, NULL);
623 if (error) {
624 error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr);
625 }
626 return (error);
627}
628
629/*
630 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
631 * SYS_CONFIG privilege, which is not available in a local zone.
632 */
633/* ARGSUSED */
634static int
635zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
636{
637 if (secpolicy_sys_config(cr, B_FALSE) != 0)
638 return (EPERM);
639
640 return (0);
641}
642
643/*
644 * Just like zfs_secpolicy_config, except that we will check for
645 * mount permission on the dataset for permission to create/remove
646 * the minor nodes.
647 */
648static int
649zfs_secpolicy_minor(zfs_cmd_t *zc, cred_t *cr)
650{
651 if (secpolicy_sys_config(cr, B_FALSE) != 0) {
652 return (dsl_deleg_access(zc->zc_name,
653 ZFS_DELEG_PERM_MOUNT, cr));
654 }
655
656 return (0);
657}
658
659/*
660 * Policy for fault injection. Requires all privileges.
661 */
662/* ARGSUSED */
663static int
664zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
665{
666 return (secpolicy_zinject(cr));
667}
668
669static int
670zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
671{
672 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
673
674 if (prop == ZPROP_INVAL) {
675 if (!zfs_prop_user(zc->zc_value))
676 return (EINVAL);
677 return (zfs_secpolicy_write_perms(zc->zc_name,
678 ZFS_DELEG_PERM_USERPROP, cr));
679 } else {
680 if (!zfs_prop_inheritable(prop))
681 return (EINVAL);
682 return (zfs_secpolicy_setprop(zc->zc_name, prop, cr));
683 }
684}
685
686/*
687 * Policy for dataset backup operations (sendbackup).
688 * Requires SYS_MOUNT privilege, and must be writable in the local zone.
689 */
690static int
691zfs_secpolicy_operator(const char *dataset, cred_t *cr)
692{
693 int writable = 1;
694
695 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(dataset, &writable))
696 return (ENOENT);
697 if (secpolicy_zfs(cr) != 0 && !groupmember(GID_OPERATOR, cr))
698 return (EPERM);
699 return (0);
700}
701
702/*
703 * Returns the nvlist as specified by the user in the zfs_cmd_t.
704 */
705static int
706get_nvlist(uint64_t nvl, uint64_t size, nvlist_t **nvp)
707{
708 char *packed;
709 int error;
710 nvlist_t *list = NULL;
711
712 /*
713 * Read in and unpack the user-supplied nvlist.
714 */
715 if (size == 0)
716 return (EINVAL);
717
718 packed = kmem_alloc(size, KM_SLEEP);
719
720 if ((error = xcopyin((void *)(uintptr_t)nvl, packed, size)) != 0) {
721 kmem_free(packed, size);
722 return (error);
723 }
724
725 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
726 kmem_free(packed, size);
727 return (error);
728 }
729
730 kmem_free(packed, size);
731
732 *nvp = list;
733 return (0);
734}
735
736static int
737put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
738{
739 char *packed = NULL;
740 size_t size;
741 int error;
742
743 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
744
745 if (size > zc->zc_nvlist_dst_size) {
746 /*
747 * Solaris returns ENOMEM here, because even if an error is
748 * returned from an ioctl(2), new zc_nvlist_dst_size will be
749 * passed to the userland. This is not the case for FreeBSD.
750 * We need to return 0, so the kernel will copy the
751 * zc_nvlist_dst_size back and the userland can discover that a
752 * bigger buffer is needed.
753 */
754 error = 0;
755 } else {
756 packed = kmem_alloc(size, KM_SLEEP);
757 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
758 KM_SLEEP) == 0);
759 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
760 size);
761 kmem_free(packed, size);
762 }
763
764 zc->zc_nvlist_dst_size = size;
765 return (error);
766}
767
768static int
769zfs_ioc_pool_create(zfs_cmd_t *zc)
770{
771 int error;
772 nvlist_t *config, *props = NULL;
773 nvlist_t *rootprops = NULL;
774 nvlist_t *zplprops = NULL;
775 char *buf;
776
777 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
778 &config))
779 return (error);
780
781 if (zc->zc_nvlist_src_size != 0 && (error =
782 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
783 nvlist_free(config);
784 return (error);
785 }
786
787 if (props) {
788 nvlist_t *nvl = NULL;
789 uint64_t version = SPA_VERSION;
790
791 (void) nvlist_lookup_uint64(props,
792 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
793 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
794 error = EINVAL;
795 goto pool_props_bad;
796 }
797 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
798 if (nvl) {
799 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
800 if (error != 0) {
801 nvlist_free(config);
802 nvlist_free(props);
803 return (error);
804 }
805 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
806 }
807 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
808 error = zfs_fill_zplprops_root(version, rootprops,
809 zplprops, NULL);
810 if (error)
811 goto pool_props_bad;
812 }
813
814 buf = history_str_get(zc);
815
816 error = spa_create(zc->zc_name, config, props, buf, zplprops);
817
818 /*
819 * Set the remaining root properties
820 */
821 if (!error &&
822 (error = zfs_set_prop_nvlist(zc->zc_name, rootprops)) != 0)
823 (void) spa_destroy(zc->zc_name);
824
825 if (buf != NULL)
826 history_str_free(buf);
827
828pool_props_bad:
829 nvlist_free(rootprops);
830 nvlist_free(zplprops);
831 nvlist_free(config);
832 nvlist_free(props);
833
834 return (error);
835}
836
837static int
838zfs_ioc_pool_destroy(zfs_cmd_t *zc)
839{
840 int error;
841 zfs_log_history(zc);
842 error = spa_destroy(zc->zc_name);
843 return (error);
844}
845
846static int
847zfs_ioc_pool_import(zfs_cmd_t *zc)
848{
849 int error;
850 nvlist_t *config, *props = NULL;
851 uint64_t guid;
852
853 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
854 &config)) != 0)
855 return (error);
856
857 if (zc->zc_nvlist_src_size != 0 && (error =
858 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, &props))) {
859 nvlist_free(config);
860 return (error);
861 }
862
863 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
864 guid != zc->zc_guid)
865 error = EINVAL;
866 else if (zc->zc_cookie)
867 error = spa_import_faulted(zc->zc_name, config,
868 props);
869 else
870 error = spa_import(zc->zc_name, config, props);
871
872 nvlist_free(config);
873
874 if (props)
875 nvlist_free(props);
876
877 return (error);
878}
879
880static int
881zfs_ioc_pool_export(zfs_cmd_t *zc)
882{
883 int error;
884 boolean_t force = (boolean_t)zc->zc_cookie;
885 boolean_t hardforce = (boolean_t)zc->zc_guid;
886
887 zfs_log_history(zc);
888 error = spa_export(zc->zc_name, NULL, force, hardforce);
889 return (error);
890}
891
892static int
893zfs_ioc_pool_configs(zfs_cmd_t *zc)
894{
895 nvlist_t *configs;
896 int error;
897
898 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
899 return (EEXIST);
900
901 error = put_nvlist(zc, configs);
902
903 nvlist_free(configs);
904
905 return (error);
906}
907
908static int
909zfs_ioc_pool_stats(zfs_cmd_t *zc)
910{
911 nvlist_t *config;
912 int error;
913 int ret = 0;
914
915 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
916 sizeof (zc->zc_value));
917
918 if (config != NULL) {
919 ret = put_nvlist(zc, config);
920 nvlist_free(config);
921
922 /*
923 * The config may be present even if 'error' is non-zero.
924 * In this case we return success, and preserve the real errno
925 * in 'zc_cookie'.
926 */
927 zc->zc_cookie = error;
928 } else {
929 ret = error;
930 }
931
932 return (ret);
933}
934
935/*
936 * Try to import the given pool, returning pool stats as appropriate so that
937 * user land knows which devices are available and overall pool health.
938 */
939static int
940zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
941{
942 nvlist_t *tryconfig, *config;
943 int error;
944
945 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
946 &tryconfig)) != 0)
947 return (error);
948
949 config = spa_tryimport(tryconfig);
950
951 nvlist_free(tryconfig);
952
953 if (config == NULL)
954 return (EINVAL);
955
956 error = put_nvlist(zc, config);
957 nvlist_free(config);
958
959 return (error);
960}
961
962static int
963zfs_ioc_pool_scrub(zfs_cmd_t *zc)
964{
965 spa_t *spa;
966 int error;
967
968 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
969 return (error);
970
971 error = spa_scrub(spa, zc->zc_cookie);
972
973 spa_close(spa, FTAG);
974
975 return (error);
976}
977
978static int
979zfs_ioc_pool_freeze(zfs_cmd_t *zc)
980{
981 spa_t *spa;
982 int error;
983
984 error = spa_open(zc->zc_name, &spa, FTAG);
985 if (error == 0) {
986 spa_freeze(spa);
987 spa_close(spa, FTAG);
988 }
989 return (error);
990}
991
992static int
993zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
994{
995 spa_t *spa;
996 int error;
997
998 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
999 return (error);
1000
1001 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1002 spa_close(spa, FTAG);
1003 return (EINVAL);
1004 }
1005
1006 spa_upgrade(spa, zc->zc_cookie);
1007 spa_close(spa, FTAG);
1008
1009 return (error);
1010}
1011
1012static int
1013zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1014{
1015 spa_t *spa;
1016 char *hist_buf;
1017 uint64_t size;
1018 int error;
1019
1020 if ((size = zc->zc_history_len) == 0)
1021 return (EINVAL);
1022
1023 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1024 return (error);
1025
1026 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1027 spa_close(spa, FTAG);
1028 return (ENOTSUP);
1029 }
1030
1031 hist_buf = kmem_alloc(size, KM_SLEEP);
1032 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1033 &zc->zc_history_len, hist_buf)) == 0) {
1034 error = xcopyout(hist_buf,
1035 (char *)(uintptr_t)zc->zc_history,
1036 zc->zc_history_len);
1037 }
1038
1039 spa_close(spa, FTAG);
1040 kmem_free(hist_buf, size);
1041 return (error);
1042}
1043
1044static int
1045zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1046{
1047 int error;
1048
1049 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1050 return (error);
1051
1052 return (0);
1053}
1054
1055static int
1056zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1057{
1058 objset_t *osp;
1059 int error;
1060
1061 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
1062 DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
1063 return (error);
1064 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
1065 sizeof (zc->zc_value));
1066 dmu_objset_close(osp);
1067
1068 return (error);
1069}
1070
1071static int
1072zfs_ioc_vdev_add(zfs_cmd_t *zc)
1073{
1074 spa_t *spa;
1075 int error;
1076 nvlist_t *config, **l2cache, **spares;
1077 uint_t nl2cache = 0, nspares = 0;
1078
1079 error = spa_open(zc->zc_name, &spa, FTAG);
1080 if (error != 0)
1081 return (error);
1082
1083 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1084 &config);
1085 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1086 &l2cache, &nl2cache);
1087
1088 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1089 &spares, &nspares);
1090
1091 /*
1092 * A root pool with concatenated devices is not supported.
1093 * Thus, can not add a device to a root pool.
1094 *
1095 * Intent log device can not be added to a rootpool because
1096 * during mountroot, zil is replayed, a seperated log device
1097 * can not be accessed during the mountroot time.
1098 *
1099 * l2cache and spare devices are ok to be added to a rootpool.
1100 */
1101 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1102 spa_close(spa, FTAG);
1103 return (EDOM);
1104 }
1105
1106 if (error == 0) {
1107 error = spa_vdev_add(spa, config);
1108 nvlist_free(config);
1109 }
1110 spa_close(spa, FTAG);
1111 return (error);
1112}
1113
1114static int
1115zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1116{
1117 spa_t *spa;
1118 int error;
1119
1120 error = spa_open(zc->zc_name, &spa, FTAG);
1121 if (error != 0)
1122 return (error);
1123 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1124 spa_close(spa, FTAG);
1125 return (error);
1126}
1127
1128static int
1129zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1130{
1131 spa_t *spa;
1132 int error;
1133 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1134
1135 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1136 return (error);
1137 switch (zc->zc_cookie) {
1138 case VDEV_STATE_ONLINE:
1139 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1140 break;
1141
1142 case VDEV_STATE_OFFLINE:
1143 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1144 break;
1145
1146 case VDEV_STATE_FAULTED:
1147 error = vdev_fault(spa, zc->zc_guid);
1148 break;
1149
1150 case VDEV_STATE_DEGRADED:
1151 error = vdev_degrade(spa, zc->zc_guid);
1152 break;
1153
1154 default:
1155 error = EINVAL;
1156 }
1157 zc->zc_cookie = newstate;
1158 spa_close(spa, FTAG);
1159 return (error);
1160}
1161
1162static int
1163zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1164{
1165 spa_t *spa;
1166 int replacing = zc->zc_cookie;
1167 nvlist_t *config;
1168 int error;
1169
1170 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1171 return (error);
1172
1173 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1174 &config)) == 0) {
1175 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1176 nvlist_free(config);
1177 }
1178
1179 spa_close(spa, FTAG);
1180 return (error);
1181}
1182
1183static int
1184zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1185{
1186 spa_t *spa;
1187 int error;
1188
1189 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1190 return (error);
1191
1192 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1193
1194 spa_close(spa, FTAG);
1195 return (error);
1196}
1197
1198static int
1199zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1200{
1201 spa_t *spa;
1202 char *path = zc->zc_value;
1203 uint64_t guid = zc->zc_guid;
1204 int error;
1205
1206 error = spa_open(zc->zc_name, &spa, FTAG);
1207 if (error != 0)
1208 return (error);
1209
1210 error = spa_vdev_setpath(spa, guid, path);
1211 spa_close(spa, FTAG);
1212 return (error);
1213}
1214
1215/*
1216 * inputs:
1217 * zc_name name of filesystem
1218 * zc_nvlist_dst_size size of buffer for property nvlist
1219 *
1220 * outputs:
1221 * zc_objset_stats stats
1222 * zc_nvlist_dst property nvlist
1223 * zc_nvlist_dst_size size of property nvlist
1224 */
1225static int
1226zfs_ioc_objset_stats(zfs_cmd_t *zc)
1227{
1228 objset_t *os = NULL;
1229 int error;
1230 nvlist_t *nv;
1231
1232 if (error = dmu_objset_open(zc->zc_name,
1233 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1234 return (error);
1235
1236 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1237
1238 if (zc->zc_nvlist_dst != 0 &&
1239 (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1240 dmu_objset_stats(os, nv);
1241 /*
1242 * NB: zvol_get_stats() will read the objset contents,
1243 * which we aren't supposed to do with a
1244 * DS_MODE_USER hold, because it could be
1245 * inconsistent. So this is a bit of a workaround...
1246 */
1247 if (!zc->zc_objset_stats.dds_inconsistent) {
1248 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1249 VERIFY(zvol_get_stats(os, nv) == 0);
1250 }
1251 error = put_nvlist(zc, nv);
1252 nvlist_free(nv);
1253 }
1254
1255 dmu_objset_close(os);
1256 if (error == ENOMEM)
1257 error = 0;
1258 return (error);
1259}
1260
1261static int
1262nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1263{
1264 uint64_t value;
1265 int error;
1266
1267 /*
1268 * zfs_get_zplprop() will either find a value or give us
1269 * the default value (if there is one).
1270 */
1271 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1272 return (error);
1273 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1274 return (0);
1275}
1276
1277/*
1278 * inputs:
1279 * zc_name name of filesystem
1280 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1281 *
1282 * outputs:
1283 * zc_nvlist_dst zpl property nvlist
1284 * zc_nvlist_dst_size size of zpl property nvlist
1285 */
1286static int
1287zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1288{
1289 objset_t *os;
1290 int err;
1291
1292 if (err = dmu_objset_open(zc->zc_name,
1293 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1294 return (err);
1295
1296 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1297
1298 /*
1299 * NB: nvl_add_zplprop() will read the objset contents,
1300 * which we aren't supposed to do with a DS_MODE_USER
1301 * hold, because it could be inconsistent.
1302 */
1303 if (zc->zc_nvlist_dst != 0 &&
1304 !zc->zc_objset_stats.dds_inconsistent &&
1305 dmu_objset_type(os) == DMU_OST_ZFS) {
1306 nvlist_t *nv;
1307
1308 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1309 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1310 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1311 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1312 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1313 err = put_nvlist(zc, nv);
1314 nvlist_free(nv);
1315 } else {
1316 err = ENOENT;
1317 }
1318 dmu_objset_close(os);
1319 return (err);
1320}
1321
1322/*
1323 * inputs:
1324 * zc_name name of filesystem
1325 * zc_cookie zap cursor
1326 * zc_nvlist_dst_size size of buffer for property nvlist
1327 *
1328 * outputs:
1329 * zc_name name of next filesystem
1330 * zc_objset_stats stats
1331 * zc_nvlist_dst property nvlist
1332 * zc_nvlist_dst_size size of property nvlist
1333 */
1334static int
1335zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1336{
1337 objset_t *os;
1338 int error;
1339 char *p;
1340
1341 if (error = dmu_objset_open(zc->zc_name,
1342 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1343 if (error == ENOENT)
1344 error = ESRCH;
1345 return (error);
1346 }
1347
1348 p = strrchr(zc->zc_name, '/');
1349 if (p == NULL || p[1] != '\0')
1350 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1351 p = zc->zc_name + strlen(zc->zc_name);
1352
1353 if (zc->zc_cookie == 0) {
1354 uint64_t cookie = 0;
1355 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1356
1357 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1358 dmu_objset_prefetch(p, NULL);
1359 }
1360
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(curthread) &&
1368 !zone_dataset_visible(zc->zc_name, NULL));
1369 dmu_objset_close(os);
1370
1371 /*
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.
1374 */
1375 if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1376 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1377
1378 return (error);
1379}
1380
1381/*
1382 * inputs:
1383 * zc_name name of filesystem
1384 * zc_cookie zap cursor
1385 * zc_nvlist_dst_size size of buffer for property nvlist
1386 *
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
1392 */
1393static int
1394zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1395{
1396 objset_t *os;
1397 int error;
1398
1399 if (zc->zc_cookie == 0)
1400 dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1401 NULL, DS_FIND_SNAPSHOTS);
1402 error = dmu_objset_open(zc->zc_name,
1403 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
1404 if (error)
1405 return (error == ENOENT ? ESRCH : error);
1406
1407 /*
1408 * A dataset name of maximum length cannot have any snapshots,
1409 * so exit immediately.
1410 */
1411 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1412 dmu_objset_close(os);
1413 return (ESRCH);
1414 }
1415
1416 error = dmu_snapshot_list_next(os,
1417 sizeof (zc->zc_name) - strlen(zc->zc_name),
1418 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1419 dmu_objset_close(os);
1420 if (error == 0)
1421 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1422 else if (error == ENOENT)
1423 error = ESRCH;
1424
1425 /* if we failed, undo the @ that we tacked on to zc_name */
1426 if (error)
1427 *strchr(zc->zc_name, '@') = '\0';
1428 return (error);
1429}
1430
1431int
1432zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1433{
1434 nvpair_t *elem;
1435 int error;
1436 uint64_t intval;
1437 char *strval;
1438
1439 /*
1440 * First validate permission to set all of the properties
1441 */
1442 elem = NULL;
1443 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1444 const char *propname = nvpair_name(elem);
1445 zfs_prop_t prop = zfs_name_to_prop(propname);
1446
1447 if (prop == ZPROP_INVAL) {
1448 /*
1449 * If this is a user-defined property, it must be a
1450 * string, and there is no further validation to do.
1451 */
1452 if (!zfs_prop_user(propname) ||
1453 nvpair_type(elem) != DATA_TYPE_STRING)
1454 return (EINVAL);
1455
1456 if (error = zfs_secpolicy_write_perms(name,
1457 ZFS_DELEG_PERM_USERPROP, CRED()))
1458 return (error);
1459 continue;
1460 }
1461
1462 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1463 return (error);
1464
1465 /*
1466 * Check that this value is valid for this pool version
1467 */
1468 switch (prop) {
1469 case ZFS_PROP_COMPRESSION:
1470 /*
1471 * If the user specified gzip compression, make sure
1472 * the SPA supports it. We ignore any errors here since
1473 * we'll catch them later.
1474 */
1475 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1476 nvpair_value_uint64(elem, &intval) == 0) {
1477 if (intval >= ZIO_COMPRESS_GZIP_1 &&
1478 intval <= ZIO_COMPRESS_GZIP_9 &&
1479 zfs_earlier_version(name,
1480 SPA_VERSION_GZIP_COMPRESSION))
1481 return (ENOTSUP);
1482
1483 /*
1484 * If this is a bootable dataset then
1485 * verify that the compression algorithm
1486 * is supported for booting. We must return
1487 * something other than ENOTSUP since it
1488 * implies a downrev pool version.
1489 */
1490 if (zfs_is_bootfs(name) &&
1491 !BOOTFS_COMPRESS_VALID(intval))
1492 return (ERANGE);
1493 }
1494 break;
1495
1496 case ZFS_PROP_COPIES:
1497 if (zfs_earlier_version(name,
1498 SPA_VERSION_DITTO_BLOCKS))
1499 return (ENOTSUP);
1500 break;
1501
1502 case ZFS_PROP_SHARESMB:
1503 if (zpl_earlier_version(name, ZPL_VERSION_FUID))
1504 return (ENOTSUP);
1505 break;
1506
1507 case ZFS_PROP_ACLINHERIT:
1508 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1509 nvpair_value_uint64(elem, &intval) == 0)
1510 if (intval == ZFS_ACL_PASSTHROUGH_X &&
1511 zfs_earlier_version(name,
1512 SPA_VERSION_PASSTHROUGH_X))
1513 return (ENOTSUP);
1514 }
1515 }
1516
1517 elem = NULL;
1518 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1519 const char *propname = nvpair_name(elem);
1520 zfs_prop_t prop = zfs_name_to_prop(propname);
1521
1522 if (prop == ZPROP_INVAL) {
1523 VERIFY(nvpair_value_string(elem, &strval) == 0);
1524 error = dsl_prop_set(name, propname, 1,
1525 strlen(strval) + 1, strval);
1526 if (error == 0)
1527 continue;
1528 else
1529 return (error);
1530 }
1531
1532 switch (prop) {
1533 case ZFS_PROP_QUOTA:
1534 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1535 (error = dsl_dir_set_quota(name, intval)) != 0)
1536 return (error);
1537 break;
1538
1539 case ZFS_PROP_REFQUOTA:
1540 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1541 (error = dsl_dataset_set_quota(name, intval)) != 0)
1542 return (error);
1543 break;
1544
1545 case ZFS_PROP_RESERVATION:
1546 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1547 (error = dsl_dir_set_reservation(name,
1548 intval)) != 0)
1549 return (error);
1550 break;
1551
1552 case ZFS_PROP_REFRESERVATION:
1553 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1554 (error = dsl_dataset_set_reservation(name,
1555 intval)) != 0)
1556 return (error);
1557 break;
1558
1559 case ZFS_PROP_VOLSIZE:
1560 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1561 (error = zvol_set_volsize(name,
1562 ddi_driver_major(zfs_dip), intval)) != 0)
1563 return (error);
1564 break;
1565
1566 case ZFS_PROP_VOLBLOCKSIZE:
1567 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1568 (error = zvol_set_volblocksize(name, intval)) != 0)
1569 return (error);
1570 break;
1571
1572 case ZFS_PROP_VERSION:
1573 if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1574 (error = zfs_set_version(name, intval)) != 0)
1575 return (error);
1576 break;
1577
1578 default:
1579 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1580 if (zfs_prop_get_type(prop) !=
1581 PROP_TYPE_STRING)
1582 return (EINVAL);
1583 VERIFY(nvpair_value_string(elem, &strval) == 0);
1584 if ((error = dsl_prop_set(name,
1585 nvpair_name(elem), 1, strlen(strval) + 1,
1586 strval)) != 0)
1587 return (error);
1588 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1589 const char *unused;
1590
1591 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1592
1593 switch (zfs_prop_get_type(prop)) {
1594 case PROP_TYPE_NUMBER:
1595 break;
1596 case PROP_TYPE_STRING:
1597 return (EINVAL);
1598 case PROP_TYPE_INDEX:
1599 if (zfs_prop_index_to_string(prop,
1600 intval, &unused) != 0)
1601 return (EINVAL);
1602 break;
1603 default:
1604 cmn_err(CE_PANIC,
1605 "unknown property type");
1606 break;
1607 }
1608
1609 if ((error = dsl_prop_set(name, propname,
1610 8, 1, &intval)) != 0)
1611 return (error);
1612 } else {
1613 return (EINVAL);
1614 }
1615 break;
1616 }
1617 }
1618
1619 return (0);
1620}
1621
1622/*
1623 * inputs:
1624 * zc_name name of filesystem
1625 * zc_value name of property to inherit
1626 * zc_nvlist_src{_size} nvlist of properties to apply
1627 * zc_cookie clear existing local props?
1628 *
1629 * outputs: none
1630 */
1631static int
1632zfs_ioc_set_prop(zfs_cmd_t *zc)
1633{
1634 nvlist_t *nvl;
1635 int error;
1636
1637 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1638 &nvl)) != 0)
1639 return (error);
1640
1641 if (zc->zc_cookie) {
1642 nvlist_t *origprops;
1643 objset_t *os;
1644
1645 if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1646 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1647 if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
1648 clear_props(zc->zc_name, origprops);
1649 nvlist_free(origprops);
1650 }
1651 dmu_objset_close(os);
1652 }
1653
1654 }
1655
1656 error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1657
1658 nvlist_free(nvl);
1659 return (error);
1660}
1661
1662/*
1663 * inputs:
1664 * zc_name name of filesystem
1665 * zc_value name of property to inherit
1666 *
1667 * outputs: none
1668 */
1669static int
1670zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1671{
1672 /* the property name has been validated by zfs_secpolicy_inherit() */
1673 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1674}
1675
1676static int
1677zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1678{
1679 nvlist_t *props;
1680 spa_t *spa;
1681 int error;
1682
1683 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1684 &props)))
1685 return (error);
1686
1687 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1688 nvlist_free(props);
1689 return (error);
1690 }
1691
1692 error = spa_prop_set(spa, props);
1693
1694 nvlist_free(props);
1695 spa_close(spa, FTAG);
1696
1697 return (error);
1698}
1699
1700static int
1701zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1702{
1703 spa_t *spa;
1704 int error;
1705 nvlist_t *nvp = NULL;
1706
1707 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1708 return (error);
1709
1710 error = spa_prop_get(spa, &nvp);
1711
1712 if (error == 0 && zc->zc_nvlist_dst != 0)
1713 error = put_nvlist(zc, nvp);
1714 else
1715 error = EFAULT;
1716
1717 spa_close(spa, FTAG);
1718
1719 if (nvp)
1720 nvlist_free(nvp);
1721 return (error);
1722}
1723
1724static int
1725zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1726{
1727#ifdef TODO
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;
1735
1736 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1737 &nvp)) != 0) {
1738 return (error);
1739 }
1740
1741 if ((error = nvlist_lookup_uint32(nvp,
1742 ZFS_DELEG_PERM_UID, &uid)) != 0) {
1743 nvlist_free(nvp);
1744 return (EPERM);
1745 }
1746
1747 if ((error = nvlist_lookup_uint32(nvp,
1748 ZFS_DELEG_PERM_GID, &gid)) != 0) {
1749 nvlist_free(nvp);
1750 return (EPERM);
1751 }
1752
1753 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1754 &groups, &group_cnt)) != 0) {
1755 nvlist_free(nvp);
1756 return (EPERM);
1757 }
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);
1764 }
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);
1770#else
1771 return (EPERM);
1772#endif
1773}
1774
1775/*
1776 * inputs:
1777 * zc_name name of filesystem
1778 * zc_nvlist_src{_size} nvlist of delegated permissions
1779 * zc_perm_action allow/unallow flag
1780 *
1781 * outputs: none
1782 */
1783static int
1784zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1785{
1786 int error;
1787 nvlist_t *fsaclnv = NULL;
1788
1789 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1790 &fsaclnv)) != 0)
1791 return (error);
1792
1793 /*
1794 * Verify nvlist is constructed correctly
1795 */
1796 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1797 nvlist_free(fsaclnv);
1798 return (EINVAL);
1799 }
1800
1801 /*
1802 * If we don't have PRIV_SYS_MOUNT, then validate
1803 * that user is allowed to hand out each permission in
1804 * the nvlist(s)
1805 */
1806
1807 error = secpolicy_zfs(CRED());
1808 if (error) {
1809 if (zc->zc_perm_action == B_FALSE) {
1810 error = dsl_deleg_can_allow(zc->zc_name,
1811 fsaclnv, CRED());
1812 } else {
1813 error = dsl_deleg_can_unallow(zc->zc_name,
1814 fsaclnv, CRED());
1815 }
1816 }
1817
1818 if (error == 0)
1819 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1820
1821 nvlist_free(fsaclnv);
1822 return (error);
1823}
1824
1825/*
1826 * inputs:
1827 * zc_name name of filesystem
1828 *
1829 * outputs:
1830 * zc_nvlist_src{_size} nvlist of delegated permissions
1831 */
1832static int
1833zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1834{
1835 nvlist_t *nvp;
1836 int error;
1837
1838 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1839 error = put_nvlist(zc, nvp);
1840 nvlist_free(nvp);
1841 }
1842
1843 return (error);
1844}
1845
1846/*
1847 * inputs:
1848 * zc_name name of volume
1849 *
1850 * outputs: none
1851 */
1852static int
1853zfs_ioc_create_minor(zfs_cmd_t *zc)
1854{
1855 return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1856}
1857
1858/*
1859 * inputs:
1860 * zc_name name of volume
1861 *
1862 * outputs: none
1863 */
1864static int
1865zfs_ioc_remove_minor(zfs_cmd_t *zc)
1866{
1867 return (zvol_remove_minor(zc->zc_name));
1868}
1869
1870/*
1871 * Search the vfs list for a specified resource. Returns a pointer to it
1872 * or NULL if no suitable entry is found. The caller of this routine
1873 * is responsible for releasing the returned vfs pointer.
1874 */
1875static vfs_t *
1876zfs_get_vfs(const char *resource)
1877{
1878 vfs_t *vfsp;
1879
1880 mtx_lock(&mountlist_mtx);
1881 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
1882 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1883 VFS_HOLD(vfsp);
1884 break;
1885 }
1886 }
1887 mtx_unlock(&mountlist_mtx);
1888 return (vfsp);
1889}
1890
1891/* ARGSUSED */
1892static void
1893zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1894{
1895 zfs_creat_t *zct = arg;
1896
1897 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1898}
1899
1900#define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1901
1902/*
1903 * inputs:
1904 * createprops list of properties requested by creator
1905 * default_zplver zpl version to use if unspecified in createprops
1906 * fuids_ok fuids allowed in this version of the spa?
1907 * os parent objset pointer (NULL if root fs)
1908 *
1909 * outputs:
1910 * zplprops values for the zplprops we attach to the master node object
1911 * is_ci true if requested file system will be purely case-insensitive
1912 *
1913 * Determine the settings for utf8only, normalization and
1914 * casesensitivity. Specific values may have been requested by the
1915 * creator and/or we can inherit values from the parent dataset. If
1916 * the file system is of too early a vintage, a creator can not
1917 * request settings for these properties, even if the requested
1918 * setting is the default value. We don't actually want to create dsl
1919 * properties for these, so remove them from the source nvlist after
1920 * processing.
1921 */
1922static int
1923zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
1924 boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
1925 boolean_t *is_ci)
1926{
1927 uint64_t zplver = default_zplver;
1928 uint64_t sense = ZFS_PROP_UNDEFINED;
1929 uint64_t norm = ZFS_PROP_UNDEFINED;
1930 uint64_t u8 = ZFS_PROP_UNDEFINED;
1931
1932 ASSERT(zplprops != NULL);
1933
1934 /*
1935 * Pull out creator prop choices, if any.
1936 */
1937 if (createprops) {
1938 (void) nvlist_lookup_uint64(createprops,
1939 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
1940 (void) nvlist_lookup_uint64(createprops,
1941 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1942 (void) nvlist_remove_all(createprops,
1943 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1944 (void) nvlist_lookup_uint64(createprops,
1945 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1946 (void) nvlist_remove_all(createprops,
1947 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1948 (void) nvlist_lookup_uint64(createprops,
1949 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1950 (void) nvlist_remove_all(createprops,
1951 zfs_prop_to_name(ZFS_PROP_CASE));
1952 }
1953
1954 /*
1955 * If the zpl version requested is whacky or the file system
1956 * or pool is version is too "young" to support normalization
1957 * and the creator tried to set a value for one of the props,
1958 * error out.
1959 */
1960 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
1961 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
1962 (zplver < ZPL_VERSION_NORMALIZATION &&
1963 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1964 sense != ZFS_PROP_UNDEFINED)))
1965 return (ENOTSUP);
1966
1967 /*
1968 * Put the version in the zplprops
1969 */
1970 VERIFY(nvlist_add_uint64(zplprops,
1971 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
1972
1973 if (norm == ZFS_PROP_UNDEFINED)
1974 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
1975 VERIFY(nvlist_add_uint64(zplprops,
1976 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
1977
1978 /*
1979 * If we're normalizing, names must always be valid UTF-8 strings.
1980 */
1981 if (norm)
1982 u8 = 1;
1983 if (u8 == ZFS_PROP_UNDEFINED)
1984 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
1985 VERIFY(nvlist_add_uint64(zplprops,
1986 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
1987
1988 if (sense == ZFS_PROP_UNDEFINED)
1989 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
1990 VERIFY(nvlist_add_uint64(zplprops,
1991 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
1992
1993 if (is_ci)
1994 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
1995
1996 return (0);
1997}
1998
1999static int
2000zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2001 nvlist_t *zplprops, boolean_t *is_ci)
2002{
2003 boolean_t fuids_ok = B_TRUE;
2004 uint64_t zplver = ZPL_VERSION;
2005 objset_t *os = NULL;
2006 char parentname[MAXNAMELEN];
2007 char *cp;
2008 int error;
2009
2010 (void) strlcpy(parentname, dataset, sizeof (parentname));
2011 cp = strrchr(parentname, '/');
2012 ASSERT(cp != NULL);
2013 cp[0] = '\0';
2014
2015 if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2016 zplver = ZPL_VERSION_FUID - 1;
2017 fuids_ok = B_FALSE;
2018 }
2019
2020 /*
2021 * Open parent object set so we can inherit zplprop values.
2022 */
2023 if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
2024 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
2025 return (error);
2026
2027 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2028 zplprops, is_ci);
2029 dmu_objset_close(os);
2030 return (error);
2031}
2032
2033static int
2034zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2035 nvlist_t *zplprops, boolean_t *is_ci)
2036{
2037 boolean_t fuids_ok = B_TRUE;
2038 uint64_t zplver = ZPL_VERSION;
2039 int error;
2040
2041 if (spa_vers < SPA_VERSION_FUID) {
2042 zplver = ZPL_VERSION_FUID - 1;
2043 fuids_ok = B_FALSE;
2044 }
2045
2046 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2047 zplprops, is_ci);
2048 return (error);
2049}
2050
2051/*
2052 * inputs:
2053 * zc_objset_type type of objset to create (fs vs zvol)
2054 * zc_name name of new objset
2055 * zc_value name of snapshot to clone from (may be empty)
2056 * zc_nvlist_src{_size} nvlist of properties to apply
2057 *
2058 * outputs: none
2059 */
2060static int
2061zfs_ioc_create(zfs_cmd_t *zc)
2062{
2063 objset_t *clone;
2064 int error = 0;
2065 zfs_creat_t zct;
2066 nvlist_t *nvprops = NULL;
2067 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2068 dmu_objset_type_t type = zc->zc_objset_type;
2069
2070 switch (type) {
2071
2072 case DMU_OST_ZFS:
2073 cbfunc = zfs_create_cb;
2074 break;
2075
2076 case DMU_OST_ZVOL:
2077 cbfunc = zvol_create_cb;
2078 break;
2079
2080 default:
2081 cbfunc = NULL;
2082 break;
2083 }
2084 if (strchr(zc->zc_name, '@') ||
2085 strchr(zc->zc_name, '%'))
2086 return (EINVAL);
2087
2088 if (zc->zc_nvlist_src != 0 &&
2089 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2090 &nvprops)) != 0)
2091 return (error);
2092
2093 zct.zct_zplprops = NULL;
2094 zct.zct_props = nvprops;
2095
2096 if (zc->zc_value[0] != '\0') {
2097 /*
2098 * We're creating a clone of an existing snapshot.
2099 */
2100 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2101 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2102 nvlist_free(nvprops);
2103 return (EINVAL);
2104 }
2105
2106 error = dmu_objset_open(zc->zc_value, type,
2107 DS_MODE_USER | DS_MODE_READONLY, &clone);
2108 if (error) {
2109 nvlist_free(nvprops);
2110 return (error);
2111 }
2112
2113 error = dmu_objset_create(zc->zc_name, type, clone, 0,
2114 NULL, NULL);
2115 if (error) {
2116 dmu_objset_close(clone);
2117 nvlist_free(nvprops);
2118 return (error);
2119 }
2120 dmu_objset_close(clone);
2121 } else {
2122 boolean_t is_insensitive = B_FALSE;
2123
2124 if (cbfunc == NULL) {
2125 nvlist_free(nvprops);
2126 return (EINVAL);
2127 }
2128
2129 if (type == DMU_OST_ZVOL) {
2130 uint64_t volsize, volblocksize;
2131
2132 if (nvprops == NULL ||
2133 nvlist_lookup_uint64(nvprops,
2134 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2135 &volsize) != 0) {
2136 nvlist_free(nvprops);
2137 return (EINVAL);
2138 }
2139
2140 if ((error = nvlist_lookup_uint64(nvprops,
2141 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2142 &volblocksize)) != 0 && error != ENOENT) {
2143 nvlist_free(nvprops);
2144 return (EINVAL);
2145 }
2146
2147 if (error != 0)
2148 volblocksize = zfs_prop_default_numeric(
2149 ZFS_PROP_VOLBLOCKSIZE);
2150
2151 if ((error = zvol_check_volblocksize(
2152 volblocksize)) != 0 ||
2153 (error = zvol_check_volsize(volsize,
2154 volblocksize)) != 0) {
2155 nvlist_free(nvprops);
2156 return (error);
2157 }
2158 } else if (type == DMU_OST_ZFS) {
2159 int error;
2160
2161 /*
2162 * We have to have normalization and
2163 * case-folding flags correct when we do the
2164 * file system creation, so go figure them out
2165 * now.
2166 */
2167 VERIFY(nvlist_alloc(&zct.zct_zplprops,
2168 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2169 error = zfs_fill_zplprops(zc->zc_name, nvprops,
2170 zct.zct_zplprops, &is_insensitive);
2171 if (error != 0) {
2172 nvlist_free(nvprops);
2173 nvlist_free(zct.zct_zplprops);
2174 return (error);
2175 }
2176 }
2177 error = dmu_objset_create(zc->zc_name, type, NULL,
2178 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2179 nvlist_free(zct.zct_zplprops);
2180 }
2181
2182 /*
2183 * It would be nice to do this atomically.
2184 */
2185 if (error == 0) {
2186 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2187 (void) dmu_objset_destroy(zc->zc_name);
2188 }
2189 nvlist_free(nvprops);
2190 return (error);
2191}
2192
2193struct snap_prop_arg {
2194 nvlist_t *nvprops;
2195 const char *snapname;
2196};
2197
2198static int
2199set_snap_props(char *name, void *arg)
2200{
2201 struct snap_prop_arg *snpa = arg;
2202 int len = strlen(name) + strlen(snpa->snapname) + 2;
2203 char *buf = kmem_alloc(len, KM_SLEEP);
2204 int err;
2205
2206 (void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
2207 err = zfs_set_prop_nvlist(buf, snpa->nvprops);
2208 if (err)
2209 (void) dmu_objset_destroy(buf);
2210 kmem_free(buf, len);
2211 return (err);
2212}
2213
2214/*
2215 * inputs:
2216 * zc_name name of filesystem
2217 * zc_value short name of snapshot
2218 * zc_cookie recursive flag
2219 *
2220 * outputs: none
2221 */
2222static int
2223zfs_ioc_snapshot(zfs_cmd_t *zc)
2224{
2225 nvlist_t *nvprops = NULL;
2226 int error;
2227 boolean_t recursive = zc->zc_cookie;
2228
2229 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2230 return (EINVAL);
2231
2232 if (zc->zc_nvlist_src != 0 &&
2233 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2234 &nvprops)) != 0)
2235 return (error);
2236
2237 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
2238
2239 /*
2240 * It would be nice to do this atomically.
2241 */
2242 if (error == 0) {
2243 struct snap_prop_arg snpa;
2244 snpa.nvprops = nvprops;
2245 snpa.snapname = zc->zc_value;
2246 if (recursive) {
2247 error = dmu_objset_find(zc->zc_name,
2248 set_snap_props, &snpa, DS_FIND_CHILDREN);
2249 if (error) {
2250 (void) dmu_snapshots_destroy(zc->zc_name,
2251 zc->zc_value);
2252 }
2253 } else {
2254 error = set_snap_props(zc->zc_name, &snpa);
2255 }
2256 }
2257 nvlist_free(nvprops);
2258 return (error);
2259}
2260
2261int
2262zfs_unmount_snap(char *name, void *arg)
2263{
2264 vfs_t *vfsp = NULL;
2265
2266 if (arg) {
2267 char *snapname = arg;
2268 int len = strlen(name) + strlen(snapname) + 2;
2269 char *buf = kmem_alloc(len, KM_SLEEP);
2270
2271 (void) strcpy(buf, name);
2272 (void) strcat(buf, "@");
2273 (void) strcat(buf, snapname);
2274 vfsp = zfs_get_vfs(buf);
2275 kmem_free(buf, len);
2276 } else if (strchr(name, '@')) {
2277 vfsp = zfs_get_vfs(name);
2278 }
2279
2280 if (vfsp) {
2281 /*
2282 * Always force the unmount for snapshots.
2283 */
2284 int flag = MS_FORCE;
2285 int err;
2286
2287 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2288 VFS_RELE(vfsp);
2289 return (err);
2290 }
2291 VFS_RELE(vfsp);
2292 mtx_lock(&Giant); /* dounmount() */
2293 dounmount(vfsp, flag, curthread);
2294 mtx_unlock(&Giant); /* dounmount() */
2295 }
2296 return (0);
2297}
2298
2299/*
2300 * inputs:
2301 * zc_name name of filesystem
2302 * zc_value short name of snapshot
2303 *
2304 * outputs: none
2305 */
2306static int
2307zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2308{
2309 int err;
2310
2311 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2312 return (EINVAL);
2313 err = dmu_objset_find(zc->zc_name,
2314 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2315 if (err)
2316 return (err);
2317 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2318}
2319
2320/*
2321 * inputs:
2322 * zc_name name of dataset to destroy
2323 * zc_objset_type type of objset
2324 *
2325 * outputs: none
2326 */
2327static int
2328zfs_ioc_destroy(zfs_cmd_t *zc)
2329{
2330 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2331 int err = zfs_unmount_snap(zc->zc_name, NULL);
2332 if (err)
2333 return (err);
2334 }
2335
2336 return (dmu_objset_destroy(zc->zc_name));
2337}
2338
2339/*
2340 * inputs:
2341 * zc_name name of dataset to rollback (to most recent snapshot)
2342 *
2343 * outputs: none
2344 */
2345static int
2346zfs_ioc_rollback(zfs_cmd_t *zc)
2347{
2348 objset_t *os;
2349 int error;
2350 zfsvfs_t *zfsvfs = NULL;
2351
2352 /*
2353 * Get the zfsvfs for the receiving objset. There
2354 * won't be one if we're operating on a zvol, if the
2355 * objset doesn't exist yet, or is not mounted.
2356 */
2357 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
2358 if (error)
2359 return (error);
2360
2361 if (dmu_objset_type(os) == DMU_OST_ZFS) {
2362 mutex_enter(&os->os->os_user_ptr_lock);
2363 zfsvfs = dmu_objset_get_user(os);
2364 if (zfsvfs != NULL)
2365 VFS_HOLD(zfsvfs->z_vfs);
2366 mutex_exit(&os->os->os_user_ptr_lock);
2367 }
2368
2369 if (zfsvfs != NULL) {
2370 char osname[MAXNAMELEN];
2371 int mode;
2372
2373 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2374 if (error == 0) {
2375 int resume_err;
2376
2377 ASSERT(strcmp(osname, zc->zc_name) == 0);
2378 error = dmu_objset_rollback(os);
2379 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2380 error = error ? error : resume_err;
2381 } else {
2382 dmu_objset_close(os);
2383 }
2384 VFS_RELE(zfsvfs->z_vfs);
2385 } else {
2386 error = dmu_objset_rollback(os);
2387 }
2388 /* Note, the dmu_objset_rollback() releases the objset for us. */
2389
2390 return (error);
2391}
2392
2393/*
2394 * inputs:
2395 * zc_name old name of dataset
2396 * zc_value new name of dataset
2397 * zc_cookie recursive flag (only valid for snapshots)
2398 *
2399 * outputs: none
2400 */
2401static int
2402zfs_ioc_rename(zfs_cmd_t *zc)
2403{
2404 boolean_t recursive = zc->zc_cookie & 1;
2405
2406 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2407 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2408 strchr(zc->zc_value, '%'))
2409 return (EINVAL);
2410
2411 /*
2412 * Unmount snapshot unless we're doing a recursive rename,
2413 * in which case the dataset code figures out which snapshots
2414 * to unmount.
2415 */
2416 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2417 zc->zc_objset_type == DMU_OST_ZFS) {
2418 int err = zfs_unmount_snap(zc->zc_name, NULL);
2419 if (err)
2420 return (err);
2421 }
2422 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2423}
2424
2425static void
2426clear_props(char *dataset, nvlist_t *props)
2427{
2428 zfs_cmd_t *zc;
2429 nvpair_t *prop;
2430
2431 if (props == NULL)
2432 return;
2433 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2434 (void) strcpy(zc->zc_name, dataset);
2435 for (prop = nvlist_next_nvpair(props, NULL); prop;
2436 prop = nvlist_next_nvpair(props, prop)) {
2437 (void) strcpy(zc->zc_value, nvpair_name(prop));
2438 if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2439 (void) zfs_ioc_inherit_prop(zc);
2440 }
2441 kmem_free(zc, sizeof (zfs_cmd_t));
2442}
2443
2444/*
2445 * inputs:
2446 * zc_name name of containing filesystem
2447 * zc_nvlist_src{_size} nvlist of properties to apply
2448 * zc_value name of snapshot to create
2449 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2450 * zc_cookie file descriptor to recv from
2451 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2452 * zc_guid force flag
2453 *
2454 * outputs:
2455 * zc_cookie number of bytes read
2456 */
2457static int
2458zfs_ioc_recv(zfs_cmd_t *zc)
2459{
2460 file_t *fp;
2461 objset_t *os;
2462 dmu_recv_cookie_t drc;
2463 zfsvfs_t *zfsvfs = NULL;
2464 boolean_t force = (boolean_t)zc->zc_guid;
2465 int error, fd;
2466 offset_t off;
2467 nvlist_t *props = NULL;
2468 nvlist_t *origprops = NULL;
2469 objset_t *origin = NULL;
2470 char *tosnap;
2471 char tofs[ZFS_MAXNAMELEN];
2472
2473 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2474 strchr(zc->zc_value, '@') == NULL ||
2475 strchr(zc->zc_value, '%'))
2476 return (EINVAL);
2477
2478 (void) strcpy(tofs, zc->zc_value);
2479 tosnap = strchr(tofs, '@');
2480 *tosnap = '\0';
2481 tosnap++;
2482
2483 if (zc->zc_nvlist_src != 0 &&
2484 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2485 &props)) != 0)
2486 return (error);
2487
2488 fd = zc->zc_cookie;
2489 fp = getf(fd, 0);
2490 if (fp == NULL) {
2491 nvlist_free(props);
2492 return (EBADF);
2493 }
2494
2495 if (dmu_objset_open(tofs, DMU_OST_ANY,
2496 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2497 /*
2498 * Try to get the zfsvfs for the receiving objset.
2499 * There won't be one if we're operating on a zvol,
2500 * if the objset doesn't exist yet, or is not mounted.
2501 */
2502 mutex_enter(&os->os->os_user_ptr_lock);
2503 if (zfsvfs = dmu_objset_get_user(os)) {
2504 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2505 mutex_exit(&os->os->os_user_ptr_lock);
2506 dmu_objset_close(os);
2507 zfsvfs = NULL;
2508 error = EBUSY;
2509 goto out;
2510 }
2511 VFS_HOLD(zfsvfs->z_vfs);
2512 }
2513 mutex_exit(&os->os->os_user_ptr_lock);
2514
2515 /*
2516 * If new properties are supplied, they are to completely
2517 * replace the existing ones, so stash away the existing ones.
2518 */
2519 if (props)
2520 (void) dsl_prop_get_all(os, &origprops, TRUE);
2521
2522 dmu_objset_close(os);
2523 }
2524
2525 if (zc->zc_string[0]) {
2526 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2527 DS_MODE_USER | DS_MODE_READONLY, &origin);
2528 if (error)
2529 goto out;
2530 }
2531
2532 error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2533 force, origin, zfsvfs != NULL, &drc);
2534 if (origin)
2535 dmu_objset_close(origin);
2536 if (error)
2537 goto out;
2538
2539 /*
2540 * Reset properties. We do this before we receive the stream
2541 * so that the properties are applied to the new data.
2542 */
2543 if (props) {
2544 clear_props(tofs, origprops);
2545 /*
2546 * XXX - Note, this is all-or-nothing; should be best-effort.
2547 */
2548 (void) zfs_set_prop_nvlist(tofs, props);
2549 }
2550
2551 off = fp->f_offset;
2552 error = dmu_recv_stream(&drc, fp, &off);
2553
2554 if (error == 0 && zfsvfs) {
2555 char osname[MAXNAMELEN];
2556 int mode;
2557
2558 /* online recv */
2559 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2560 if (error == 0) {
2561 int resume_err;
2562
2563 error = dmu_recv_end(&drc);
2564 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2565 error = error ? error : resume_err;
2566 } else {
2567 dmu_recv_abort_cleanup(&drc);
2568 }
2569 } else if (error == 0) {
2570 error = dmu_recv_end(&drc);
2571 }
2572
2573 zc->zc_cookie = off - fp->f_offset;
2574 if (off >= 0 && off <= MAXOFFSET_T)
2575 fp->f_offset = off;
2576
2577 /*
2578 * On error, restore the original props.
2579 */
2580 if (error && props) {
2581 clear_props(tofs, props);
2582 (void) zfs_set_prop_nvlist(tofs, origprops);
2583 }
2584out:
2585 if (zfsvfs) {
2586 mutex_exit(&zfsvfs->z_online_recv_lock);
2587 VFS_RELE(zfsvfs->z_vfs);
2588 }
2589 nvlist_free(props);
2590 nvlist_free(origprops);
2591 releasef(fp);
2592 return (error);
2593}
2594
2595/*
2596 * inputs:
2597 * zc_name name of snapshot to send
2598 * zc_value short name of incremental fromsnap (may be empty)
2599 * zc_cookie file descriptor to send stream to
2600 * zc_obj fromorigin flag (mutually exclusive with zc_value)
2601 *
2602 * outputs: none
2603 */
2604static int
2605zfs_ioc_send(zfs_cmd_t *zc)
2606{
2607 objset_t *fromsnap = NULL;
2608 objset_t *tosnap;
2609 file_t *fp;
2610 int error;
2611 offset_t off;
2612
2613 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2614 DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2615 if (error)
2616 return (error);
2617
2618 if (zc->zc_value[0] != '\0') {
2619 char buf[MAXPATHLEN];
2620 char *cp;
2621
2622 (void) strncpy(buf, zc->zc_name, sizeof (buf));
2623 cp = strchr(buf, '@');
2624 if (cp)
2625 *(cp+1) = 0;
2626 (void) strlcat(buf, zc->zc_value, sizeof (buf));
2627 error = dmu_objset_open(buf, DMU_OST_ANY,
2628 DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2629 if (error) {
2630 dmu_objset_close(tosnap);
2631 return (error);
2632 }
2633 }
2634
2635 fp = getf(zc->zc_cookie, 1);
2636 if (fp == NULL) {
2637 dmu_objset_close(tosnap);
2638 if (fromsnap)
2639 dmu_objset_close(fromsnap);
2640 return (EBADF);
2641 }
2642
2643 off = fp->f_offset;
2644 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp, &off);
2645
2646 if (off >= 0 && off <= MAXOFFSET_T)
2647 fp->f_offset = off;
2648 releasef(fp);
2649 if (fromsnap)
2650 dmu_objset_close(fromsnap);
2651 dmu_objset_close(tosnap);
2652 return (error);
2653}
2654
2655static int
2656zfs_ioc_inject_fault(zfs_cmd_t *zc)
2657{
2658 int id, error;
2659
2660 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2661 &zc->zc_inject_record);
2662
2663 if (error == 0)
2664 zc->zc_guid = (uint64_t)id;
2665
2666 return (error);
2667}
2668
2669static int
2670zfs_ioc_clear_fault(zfs_cmd_t *zc)
2671{
2672 return (zio_clear_fault((int)zc->zc_guid));
2673}
2674
2675static int
2676zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2677{
2678 int id = (int)zc->zc_guid;
2679 int error;
2680
2681 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2682 &zc->zc_inject_record);
2683
2684 zc->zc_guid = id;
2685
2686 return (error);
2687}
2688
2689static int
2690zfs_ioc_error_log(zfs_cmd_t *zc)
2691{
2692 spa_t *spa;
2693 int error;
2694 size_t count = (size_t)zc->zc_nvlist_dst_size;
2695
2696 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2697 return (error);
2698
2699 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2700 &count);
2701 if (error == 0)
2702 zc->zc_nvlist_dst_size = count;
2703 else
2704 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2705
2706 spa_close(spa, FTAG);
2707
2708 return (error);
2709}
2710
2711static int
2712zfs_ioc_clear(zfs_cmd_t *zc)
2713{
2714 spa_t *spa;
2715 vdev_t *vd;
2716 int error;
2717
2718 /*
2719 * On zpool clear we also fix up missing slogs
2720 */
2721 mutex_enter(&spa_namespace_lock);
2722 spa = spa_lookup(zc->zc_name);
2723 if (spa == NULL) {
2724 mutex_exit(&spa_namespace_lock);
2725 return (EIO);
2726 }
2727 if (spa->spa_log_state == SPA_LOG_MISSING) {
2728 /* we need to let spa_open/spa_load clear the chains */
2729 spa->spa_log_state = SPA_LOG_CLEAR;
2730 }
2731 mutex_exit(&spa_namespace_lock);
2732
2733 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2734 return (error);
2735
2736 spa_vdev_state_enter(spa);
2737
2738 if (zc->zc_guid == 0) {
2739 vd = NULL;
2740 } else {
2741 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
2742 if (vd == NULL) {
2743 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
2744 spa_close(spa, FTAG);
2745 return (ENODEV);
2746 }
2747 }
2748
2749 vdev_clear(spa, vd);
2750
2751 (void) spa_vdev_state_exit(spa, NULL, 0);
2752
2753 /*
2754 * Resume any suspended I/Os.
2755 */
2756 zio_resume(spa);
2757
2758 spa_close(spa, FTAG);
2759
2760 return (0);
2761}
2762
2763/*
2764 * inputs:
2765 * zc_name name of filesystem
2766 * zc_value name of origin snapshot
2767 *
2768 * outputs: none
2769 */
2770static int
2771zfs_ioc_promote(zfs_cmd_t *zc)
2772{
2773 char *cp;
2774
2775 /*
2776 * We don't need to unmount *all* the origin fs's snapshots, but
2777 * it's easier.
2778 */
2779 cp = strchr(zc->zc_value, '@');
2780 if (cp)
2781 *cp = '\0';
2782 (void) dmu_objset_find(zc->zc_value,
2783 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2784 return (dsl_dataset_promote(zc->zc_name));
2785}
2786
2787#ifdef TODO
2788/*
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.
2794 */
2795int (*znfsexport_fs)(void *arg);
2796int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2797int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2798
2799int zfs_nfsshare_inited;
2800int zfs_smbshare_inited;
2801
2802ddi_modhandle_t nfs_mod;
2803ddi_modhandle_t sharefs_mod;
2804ddi_modhandle_t smbsrv_mod;
2805#endif
2806kmutex_t zfs_share_lock;
2807
2808#ifdef TODO
2809static int
2810zfs_init_sharefs()
2811{
2812 int error;
2813
2814 ASSERT(MUTEX_HELD(&zfs_share_lock));
2815 /* Both NFS and SMB shares also require sharetab support. */
2816 if (sharefs_mod == NULL && ((sharefs_mod =
2817 ddi_modopen("fs/sharefs",
2818 KRTLD_MODE_FIRST, &error)) == NULL)) {
2819 return (ENOSYS);
2820 }
2821 if (zshare_fs == NULL && ((zshare_fs =
2822 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2823 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2824 return (ENOSYS);
2825 }
2826 return (0);
2827}
2828#endif
2829
2830static int
2831zfs_ioc_share(zfs_cmd_t *zc)
2832{
2833#ifdef TODO
2834 int error;
2835 int opcode;
2836
2837 switch (zc->zc_share.z_sharetype) {
2838 case ZFS_SHARE_NFS:
2839 case ZFS_UNSHARE_NFS:
2840 if (zfs_nfsshare_inited == 0) {
2841 mutex_enter(&zfs_share_lock);
2842 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2843 KRTLD_MODE_FIRST, &error)) == NULL)) {
2844 mutex_exit(&zfs_share_lock);
2845 return (ENOSYS);
2846 }
2847 if (znfsexport_fs == NULL &&
2848 ((znfsexport_fs = (int (*)(void *))
2849 ddi_modsym(nfs_mod,
2850 "nfs_export", &error)) == NULL)) {
2851 mutex_exit(&zfs_share_lock);
2852 return (ENOSYS);
2853 }
2854 error = zfs_init_sharefs();
2855 if (error) {
2856 mutex_exit(&zfs_share_lock);
2857 return (ENOSYS);
2858 }
2859 zfs_nfsshare_inited = 1;
2860 mutex_exit(&zfs_share_lock);
2861 }
2862 break;
2863 case ZFS_SHARE_SMB:
2864 case ZFS_UNSHARE_SMB:
2865 if (zfs_smbshare_inited == 0) {
2866 mutex_enter(&zfs_share_lock);
2867 if (smbsrv_mod == NULL && ((smbsrv_mod =
2868 ddi_modopen("drv/smbsrv",
2869 KRTLD_MODE_FIRST, &error)) == NULL)) {
2870 mutex_exit(&zfs_share_lock);
2871 return (ENOSYS);
2872 }
2873 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2874 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2875 "smb_server_share", &error)) == NULL)) {
2876 mutex_exit(&zfs_share_lock);
2877 return (ENOSYS);
2878 }
2879 error = zfs_init_sharefs();
2880 if (error) {
2881 mutex_exit(&zfs_share_lock);
2882 return (ENOSYS);
2883 }
2884 zfs_smbshare_inited = 1;
2885 mutex_exit(&zfs_share_lock);
2886 }
2887 break;
2888 default:
2889 return (EINVAL);
2890 }
2891
2892 switch (zc->zc_share.z_sharetype) {
2893 case ZFS_SHARE_NFS:
2894 case ZFS_UNSHARE_NFS:
2895 if (error =
2896 znfsexport_fs((void *)
2897 (uintptr_t)zc->zc_share.z_exportdata))
2898 return (error);
2899 break;
2900 case ZFS_SHARE_SMB:
2901 case ZFS_UNSHARE_SMB:
2902 if (error = zsmbexport_fs((void *)
2903 (uintptr_t)zc->zc_share.z_exportdata,
2904 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2905 B_TRUE : B_FALSE)) {
2906 return (error);
2907 }
2908 break;
2909 }
2910
2911 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2912 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2913 SHAREFS_ADD : SHAREFS_REMOVE;
2914
2915 /*
2916 * Add or remove share from sharetab
2917 */
2918 error = zshare_fs(opcode,
2919 (void *)(uintptr_t)zc->zc_share.z_sharedata,
2920 zc->zc_share.z_sharemax);
2921
2922 return (error);
2923#else
2924 return (ENOSYS);
2925#endif
2926}
2927
2928/*
2929 * pool create, destroy, and export don't log the history as part of
2930 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2931 * do the logging of those commands.
2932 */
2933static int
2934zfs_ioc_jail(zfs_cmd_t *zc)
2935{
2936
2937 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
2938 (int)zc->zc_jailid));
2939}
2940
2941static int
2942zfs_ioc_unjail(zfs_cmd_t *zc)
2943{
2944
2945 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
2946 (int)zc->zc_jailid));
2947}
2948
2949static zfs_ioc_vec_t zfs_ioc_vec[] = {
2950 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2951 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2952 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2953 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2954 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE },
2955 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2956 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2957 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2958 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2959 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2960 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2961 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2962 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2963 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2964 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2965 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2966 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2967 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2968 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2969 { zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2970 DATASET_NAME, B_FALSE },
2971 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2972 DATASET_NAME, B_FALSE },
2973 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2974 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2975 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2976 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2977 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2978 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2979 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE },
2980 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2981 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2982 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2983 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2984 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2985 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2986 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2987 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2988 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2989 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2990 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2991 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2992 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2993 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2994 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2995 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2996 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2997 DATASET_NAME, B_FALSE },
2998 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2999 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
3000 { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE },
3001 { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE }
3002};
3003
3004static int
3005zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
3006 struct thread *td)
3007{
3008 zfs_cmd_t *zc = (void *)addr;
3009 uint_t vec;
3010 int error;
3011
3012 vec = ZFS_IOC(cmd);
3013
3014 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3015 return (EINVAL);
3016
3017 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred);
3018
3019 /*
3020 * Ensure that all pool/dataset names are valid before we pass down to
3021 * the lower layers.
3022 */
3023 if (error == 0) {
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;
3030
3031 case DATASET_NAME:
3032 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3033 error = EINVAL;
3034 break;
3035
3036 case NO_NAME:
3037 break;
3038 }
3039 }
3040
3041 if (error == 0)
3042 error = zfs_ioc_vec[vec].zvec_func(zc);
3043
3044 if (error == 0) {
3045 if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
3046 zfs_log_history(zc);
3047 }
3048
3049 return (error);
3050}
3051
3052/*
3053 * OK, so this is a little weird.
3054 *
3055 * /dev/zfs is the control node, i.e. minor 0.
3056 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3057 *
3058 * /dev/zfs has basically nothing to do except serve up ioctls,
3059 * so most of the standard driver entry points are in zvol.c.
3060 */
3061static struct cdevsw zfs_cdevsw = {
3062 .d_version = D_VERSION,
3063 .d_ioctl = zfsdev_ioctl,
3064 .d_name = ZFS_DEV_NAME
3065};
3066
3067static void
3068zfsdev_init(void)
3069{
3070 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
3071 ZFS_DEV_NAME);
3072}
3073
3074static void
3075zfsdev_fini(void)
3076{
3077 if (zfsdev != NULL)
3078 destroy_dev(zfsdev);
3079}
3080
3081static struct root_hold_token *zfs_root_token;
3082struct proc *zfsproc;
3083
3084uint_t zfs_fsyncer_key;
3085extern uint_t rrw_tsd_key;
3086
3087static int
3088zfs_modevent(module_t mod, int type, void *unused __unused)
3089{
3090 int error = 0;
3091
3092 switch (type) {
3093 case MOD_LOAD:
3094 zfs_root_token = root_mount_hold("ZFS");
3095
3096 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3097
3098 spa_init(FREAD | FWRITE);
3099 zfs_init();
3100 zvol_init();
3101
3102 tsd_create(&zfs_fsyncer_key, NULL);
3103 tsd_create(&rrw_tsd_key, NULL);
3104
3105 printf("ZFS storage pool version " SPA_VERSION_STRING "\n");
3106 root_mount_rel(zfs_root_token);
3107
3108 zfsdev_init();
3109 break;
3110 case MOD_UNLOAD:
3111 if (spa_busy() || zfs_busy() || zvol_busy() ||
3112 zio_injection_enabled) {
3113 error = EBUSY;
3114 break;
3115 }
3116
3117 zfsdev_fini();
3118 zvol_fini();
3119 zfs_fini();
3120 spa_fini();
3121
3122 tsd_destroy(&zfs_fsyncer_key);
3123 tsd_destroy(&rrw_tsd_key);
3124
3125 mutex_destroy(&zfs_share_lock);
3126 break;
3127 default:
3128 error = EOPNOTSUPP;
3129 break;
3130 }
3131 return (error);
3132}
3133
3134static moduledata_t zfs_mod = {
3135 "zfsctrl",
3136 zfs_modevent,
3137 0
3138};
3139DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
3140MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
3141MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);