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
886 zfs_log_history(zc);
887 error = spa_export(zc->zc_name, NULL, force);
888 return (error);
889}
890
891static int
892zfs_ioc_pool_configs(zfs_cmd_t *zc)
893{
894 nvlist_t *configs;
895 int error;
896
897 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
898 return (EEXIST);
899
900 error = put_nvlist(zc, configs);
901
902 nvlist_free(configs);
903
904 return (error);
905}
906
907static int
908zfs_ioc_pool_stats(zfs_cmd_t *zc)
909{
910 nvlist_t *config;
911 int error;
912 int ret = 0;
913
914 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
915 sizeof (zc->zc_value));
916
917 if (config != NULL) {
918 ret = put_nvlist(zc, config);
919 nvlist_free(config);
920
921 /*
922 * The config may be present even if 'error' is non-zero.
923 * In this case we return success, and preserve the real errno
924 * in 'zc_cookie'.
925 */
926 zc->zc_cookie = error;
927 } else {
928 ret = error;
929 }
930
931 return (ret);
932}
933
934/*
935 * Try to import the given pool, returning pool stats as appropriate so that
936 * user land knows which devices are available and overall pool health.
937 */
938static int
939zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
940{
941 nvlist_t *tryconfig, *config;
942 int error;
943
944 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
945 &tryconfig)) != 0)
946 return (error);
947
948 config = spa_tryimport(tryconfig);
949
950 nvlist_free(tryconfig);
951
952 if (config == NULL)
953 return (EINVAL);
954
955 error = put_nvlist(zc, config);
956 nvlist_free(config);
957
958 return (error);
959}
960
961static int
962zfs_ioc_pool_scrub(zfs_cmd_t *zc)
963{
964 spa_t *spa;
965 int error;
966
967 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
968 return (error);
969
970 error = spa_scrub(spa, zc->zc_cookie);
971
972 spa_close(spa, FTAG);
973
974 return (error);
975}
976
977static int
978zfs_ioc_pool_freeze(zfs_cmd_t *zc)
979{
980 spa_t *spa;
981 int error;
982
983 error = spa_open(zc->zc_name, &spa, FTAG);
984 if (error == 0) {
985 spa_freeze(spa);
986 spa_close(spa, FTAG);
987 }
988 return (error);
989}
990
991static int
992zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
993{
994 spa_t *spa;
995 int error;
996
997 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
998 return (error);
999
1000 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1001 spa_close(spa, FTAG);
1002 return (EINVAL);
1003 }
1004
1005 spa_upgrade(spa, zc->zc_cookie);
1006 spa_close(spa, FTAG);
1007
1008 return (error);
1009}
1010
1011static int
1012zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1013{
1014 spa_t *spa;
1015 char *hist_buf;
1016 uint64_t size;
1017 int error;
1018
1019 if ((size = zc->zc_history_len) == 0)
1020 return (EINVAL);
1021
1022 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1023 return (error);
1024
1025 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1026 spa_close(spa, FTAG);
1027 return (ENOTSUP);
1028 }
1029
1030 hist_buf = kmem_alloc(size, KM_SLEEP);
1031 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1032 &zc->zc_history_len, hist_buf)) == 0) {
1033 error = xcopyout(hist_buf,
1034 (char *)(uintptr_t)zc->zc_history,
1035 zc->zc_history_len);
1036 }
1037
1038 spa_close(spa, FTAG);
1039 kmem_free(hist_buf, size);
1040 return (error);
1041}
1042
1043static int
1044zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1045{
1046 int error;
1047
1048 if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
1049 return (error);
1050
1051 return (0);
1052}
1053
1054static int
1055zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1056{
1057 objset_t *osp;
1058 int error;
1059
1060 if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
1061 DS_MODE_USER | DS_MODE_READONLY, &osp)) != 0)
1062 return (error);
1063 error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
1064 sizeof (zc->zc_value));
1065 dmu_objset_close(osp);
1066
1067 return (error);
1068}
1069
1070static int
1071zfs_ioc_vdev_add(zfs_cmd_t *zc)
1072{
1073 spa_t *spa;
1074 int error;
1075 nvlist_t *config, **l2cache, **spares;
1076 uint_t nl2cache = 0, nspares = 0;
1077
1078 error = spa_open(zc->zc_name, &spa, FTAG);
1079 if (error != 0)
1080 return (error);
1081
1082 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1083 &config);
1084 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1085 &l2cache, &nl2cache);
1086
1087 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1088 &spares, &nspares);
1089
1090 /*
1091 * A root pool with concatenated devices is not supported.
1092 * Thus, can not add a device to a root pool.
1093 *
1094 * Intent log device can not be added to a rootpool because
1095 * during mountroot, zil is replayed, a seperated log device
1096 * can not be accessed during the mountroot time.
1097 *
1098 * l2cache and spare devices are ok to be added to a rootpool.
1099 */
1100 if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1101 spa_close(spa, FTAG);
1102 return (EDOM);
1103 }
1104
1105 if (error == 0) {
1106 error = spa_vdev_add(spa, config);
1107 nvlist_free(config);
1108 }
1109 spa_close(spa, FTAG);
1110 return (error);
1111}
1112
1113static int
1114zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1115{
1116 spa_t *spa;
1117 int error;
1118
1119 error = spa_open(zc->zc_name, &spa, FTAG);
1120 if (error != 0)
1121 return (error);
1122 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1123 spa_close(spa, FTAG);
1124 return (error);
1125}
1126
1127static int
1128zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1129{
1130 spa_t *spa;
1131 int error;
1132 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1133
1134 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1135 return (error);
1136 switch (zc->zc_cookie) {
1137 case VDEV_STATE_ONLINE:
1138 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1139 break;
1140
1141 case VDEV_STATE_OFFLINE:
1142 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1143 break;
1144
1145 case VDEV_STATE_FAULTED:
1146 error = vdev_fault(spa, zc->zc_guid);
1147 break;
1148
1149 case VDEV_STATE_DEGRADED:
1150 error = vdev_degrade(spa, zc->zc_guid);
1151 break;
1152
1153 default:
1154 error = EINVAL;
1155 }
1156 zc->zc_cookie = newstate;
1157 spa_close(spa, FTAG);
1158 return (error);
1159}
1160
1161static int
1162zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1163{
1164 spa_t *spa;
1165 int replacing = zc->zc_cookie;
1166 nvlist_t *config;
1167 int error;
1168
1169 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1170 return (error);
1171
1172 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1173 &config)) == 0) {
1174 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1175 nvlist_free(config);
1176 }
1177
1178 spa_close(spa, FTAG);
1179 return (error);
1180}
1181
1182static int
1183zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1184{
1185 spa_t *spa;
1186 int error;
1187
1188 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1189 return (error);
1190
1191 error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
1192
1193 spa_close(spa, FTAG);
1194 return (error);
1195}
1196
1197static int
1198zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1199{
1200 spa_t *spa;
1201 char *path = zc->zc_value;
1202 uint64_t guid = zc->zc_guid;
1203 int error;
1204
1205 error = spa_open(zc->zc_name, &spa, FTAG);
1206 if (error != 0)
1207 return (error);
1208
1209 error = spa_vdev_setpath(spa, guid, path);
1210 spa_close(spa, FTAG);
1211 return (error);
1212}
1213
1214/*
1215 * inputs:
1216 * zc_name name of filesystem
1217 * zc_nvlist_dst_size size of buffer for property nvlist
1218 *
1219 * outputs:
1220 * zc_objset_stats stats
1221 * zc_nvlist_dst property nvlist
1222 * zc_nvlist_dst_size size of property nvlist
1223 */
1224static int
1225zfs_ioc_objset_stats(zfs_cmd_t *zc)
1226{
1227 objset_t *os = NULL;
1228 int error;
1229 nvlist_t *nv;
1230
1231 if (error = dmu_objset_open(zc->zc_name,
1232 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1233 return (error);
1234
1235 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1236
1237 if (zc->zc_nvlist_dst != 0 &&
1238 (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1239 dmu_objset_stats(os, nv);
1240 /*
1241 * NB: zvol_get_stats() will read the objset contents,
1242 * which we aren't supposed to do with a
1243 * DS_MODE_USER hold, because it could be
1244 * inconsistent. So this is a bit of a workaround...
1245 */
1246 if (!zc->zc_objset_stats.dds_inconsistent) {
1247 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1248 VERIFY(zvol_get_stats(os, nv) == 0);
1249 }
1250 error = put_nvlist(zc, nv);
1251 nvlist_free(nv);
1252 }
1253
1254 dmu_objset_close(os);
1255 if (error == ENOMEM)
1256 error = 0;
1257 return (error);
1258}
1259
1260static int
1261nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1262{
1263 uint64_t value;
1264 int error;
1265
1266 /*
1267 * zfs_get_zplprop() will either find a value or give us
1268 * the default value (if there is one).
1269 */
1270 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1271 return (error);
1272 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1273 return (0);
1274}
1275
1276/*
1277 * inputs:
1278 * zc_name name of filesystem
1279 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1280 *
1281 * outputs:
1282 * zc_nvlist_dst zpl property nvlist
1283 * zc_nvlist_dst_size size of zpl property nvlist
1284 */
1285static int
1286zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1287{
1288 objset_t *os;
1289 int err;
1290
1291 if (err = dmu_objset_open(zc->zc_name,
1292 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1293 return (err);
1294
1295 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1296
1297 /*
1298 * NB: nvl_add_zplprop() will read the objset contents,
1299 * which we aren't supposed to do with a DS_MODE_USER
1300 * hold, because it could be inconsistent.
1301 */
1302 if (zc->zc_nvlist_dst != 0 &&
1303 !zc->zc_objset_stats.dds_inconsistent &&
1304 dmu_objset_type(os) == DMU_OST_ZFS) {
1305 nvlist_t *nv;
1306
1307 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1308 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1309 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1310 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1311 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1312 err = put_nvlist(zc, nv);
1313 nvlist_free(nv);
1314 } else {
1315 err = ENOENT;
1316 }
1317 dmu_objset_close(os);
1318 return (err);
1319}
1320
1321/*
1322 * inputs:
1323 * zc_name name of filesystem
1324 * zc_cookie zap cursor
1325 * zc_nvlist_dst_size size of buffer for property nvlist
1326 *
1327 * outputs:
1328 * zc_name name of next filesystem
1329 * zc_objset_stats stats
1330 * zc_nvlist_dst property nvlist
1331 * zc_nvlist_dst_size size of property nvlist
1332 */
1333static int
1334zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1335{
1336 objset_t *os;
1337 int error;
1338 char *p;
1339
1340 if (error = dmu_objset_open(zc->zc_name,
1341 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1342 if (error == ENOENT)
1343 error = ESRCH;
1344 return (error);
1345 }
1346
1347 p = strrchr(zc->zc_name, '/');
1348 if (p == NULL || p[1] != '\0')
1349 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1350 p = zc->zc_name + strlen(zc->zc_name);
1351
1352 if (zc->zc_cookie == 0) {
1353 uint64_t cookie = 0;
1354 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1355
1356 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1357 dmu_objset_prefetch(p, NULL);
1358 }
1359
1360 do {
1361 error = dmu_dir_list_next(os,
1362 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1363 NULL, &zc->zc_cookie);
1364 if (error == ENOENT)
1365 error = ESRCH;
1366 } while (error == 0 && !INGLOBALZONE(curthread) &&
1367 !zone_dataset_visible(zc->zc_name, NULL));
1368 dmu_objset_close(os);
1369
1370 /*
1371 * If it's a hidden dataset (ie. with a '$' in its name), don't
1372 * try to get stats for it. Userland will skip over it.
1373 */
1374 if (error == 0 && strchr(zc->zc_name, '$') == NULL)
1375 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1376
1377 return (error);
1378}
1379
1380/*
1381 * inputs:
1382 * zc_name name of filesystem
1383 * zc_cookie zap cursor
1384 * zc_nvlist_dst_size size of buffer for property nvlist
1385 *
1386 * outputs:
1387 * zc_name name of next snapshot
1388 * zc_objset_stats stats
1389 * zc_nvlist_dst property nvlist
1390 * zc_nvlist_dst_size size of property nvlist
1391 */
1392static int
1393zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1394{
1395 objset_t *os;
1396 int error;
1397
1398 if (zc->zc_cookie == 0)
1399 dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1400 NULL, DS_FIND_SNAPSHOTS);
1401 error = dmu_objset_open(zc->zc_name,
1402 DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
1403 if (error)
1404 return (error == ENOENT ? ESRCH : error);
1405
1406 /*
1407 * A dataset name of maximum length cannot have any snapshots,
1408 * so exit immediately.
1409 */
1410 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1411 dmu_objset_close(os);
1412 return (ESRCH);
1413 }
1414
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;
1423
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);
1428}
1429
1430int
1431zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1432{
1433 nvpair_t *elem;
1434 int error;
1435 uint64_t intval;
1436 char *strval;
1437
1438 /*
1439 * First validate permission to set all of the properties
1440 */
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);
1445
1446 if (prop == ZPROP_INVAL) {
1447 /*
1448 * If this is a user-defined property, it must be a
1449 * string, and there is no further validation to do.
1450 */
1451 if (!zfs_prop_user(propname) ||
1452 nvpair_type(elem) != DATA_TYPE_STRING)
1453 return (EINVAL);
1454
1455 if (error = zfs_secpolicy_write_perms(name,
1456 ZFS_DELEG_PERM_USERPROP, CRED()))
1457 return (error);
1458 continue;
1459 }
1460
1461 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1462 return (error);
1463
1464 /*
1465 * Check that this value is valid for this pool version
1466 */
1467 switch (prop) {
1468 case ZFS_PROP_COMPRESSION:
1469 /*
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.
1473 */
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);
1481
1482 /*
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.
1488 */
1489 if (zfs_is_bootfs(name) &&
1490 !BOOTFS_COMPRESS_VALID(intval))
1491 return (ERANGE);
1492 }
1493 break;
1494
1495 case ZFS_PROP_COPIES:
1496 if (zfs_earlier_version(name,
1497 SPA_VERSION_DITTO_BLOCKS))
1498 return (ENOTSUP);
1499 break;
1500
1501 case ZFS_PROP_SHARESMB:
1502 if (zpl_earlier_version(name, ZPL_VERSION_FUID))
1503 return (ENOTSUP);
1504 break;
1505
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);
1513 }
1514 }
1515
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);
1520
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);
1529 }
1530
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;
1537
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;
1543
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;
1550
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;
1557
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;
1564
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;
1570
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;
1576
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;
1589
1590 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1591
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;
1606 }
1607
1608 if ((error = dsl_prop_set(name, propname,
1609 8, 1, &intval)) != 0)
1610 return (error);
1611 } else {
1612 return (EINVAL);
1613 }
1614 break;
1615 }
1616 }
1617
1618 return (0);
1619}
1620
1621/*
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?
1627 *
1628 * outputs: none
1629 */
1630static int
1631zfs_ioc_set_prop(zfs_cmd_t *zc)
1632{
1633 nvlist_t *nvl;
1634 int error;
1635
1636 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1637 &nvl)) != 0)
1638 return (error);
1639
1640 if (zc->zc_cookie) {
1641 nvlist_t *origprops;
1642 objset_t *os;
1643
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);
1649 }
1650 dmu_objset_close(os);
1651 }
1652
1653 }
1654
1655 error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1656
1657 nvlist_free(nvl);
1658 return (error);
1659}
1660
1661/*
1662 * inputs:
1663 * zc_name name of filesystem
1664 * zc_value name of property to inherit
1665 *
1666 * outputs: none
1667 */
1668static int
1669zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1670{
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));
1673}
1674
1675static int
1676zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1677{
1678 nvlist_t *props;
1679 spa_t *spa;
1680 int error;
1681
1682 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1683 &props)))
1684 return (error);
1685
1686 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1687 nvlist_free(props);
1688 return (error);
1689 }
1690
1691 error = spa_prop_set(spa, props);
1692
1693 nvlist_free(props);
1694 spa_close(spa, FTAG);
1695
1696 return (error);
1697}
1698
1699static int
1700zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1701{
1702 spa_t *spa;
1703 int error;
1704 nvlist_t *nvp = NULL;
1705
1706 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1707 return (error);
1708
1709 error = spa_prop_get(spa, &nvp);
1710
1711 if (error == 0 && zc->zc_nvlist_dst != 0)
1712 error = put_nvlist(zc, nvp);
1713 else
1714 error = EFAULT;
1715
1716 spa_close(spa, FTAG);
1717
1718 if (nvp)
1719 nvlist_free(nvp);
1720 return (error);
1721}
1722
1723static int
1724zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
1725{
1726#ifdef TODO
1727 nvlist_t *nvp;
1728 int error;
1729 uint32_t uid;
1730 uint32_t gid;
1731 uint32_t *groups;
1732 uint_t group_cnt;
1733 cred_t *usercred;
1734
1735 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1736 &nvp)) != 0) {
1737 return (error);
1738 }
1739
1740 if ((error = nvlist_lookup_uint32(nvp,
1741 ZFS_DELEG_PERM_UID, &uid)) != 0) {
1742 nvlist_free(nvp);
1743 return (EPERM);
1744 }
1745
1746 if ((error = nvlist_lookup_uint32(nvp,
1747 ZFS_DELEG_PERM_GID, &gid)) != 0) {
1748 nvlist_free(nvp);
1749 return (EPERM);
1750 }
1751
1752 if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
1753 &groups, &group_cnt)) != 0) {
1754 nvlist_free(nvp);
1755 return (EPERM);
1756 }
1757 usercred = cralloc();
1758 if ((crsetugid(usercred, uid, gid) != 0) ||
1759 (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
1760 nvlist_free(nvp);
1761 crfree(usercred);
1762 return (EPERM);
1763 }
1764 nvlist_free(nvp);
1765 error = dsl_deleg_access(zc->zc_name,
1766 zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
1767 crfree(usercred);
1768 return (error);
1769#else
1770 return (EPERM);
1771#endif
1772}
1773
1774/*
1775 * inputs:
1776 * zc_name name of filesystem
1777 * zc_nvlist_src{_size} nvlist of delegated permissions
1778 * zc_perm_action allow/unallow flag
1779 *
1780 * outputs: none
1781 */
1782static int
1783zfs_ioc_set_fsacl(zfs_cmd_t *zc)
1784{
1785 int error;
1786 nvlist_t *fsaclnv = NULL;
1787
1788 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1789 &fsaclnv)) != 0)
1790 return (error);
1791
1792 /*
1793 * Verify nvlist is constructed correctly
1794 */
1795 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
1796 nvlist_free(fsaclnv);
1797 return (EINVAL);
1798 }
1799
1800 /*
1801 * If we don't have PRIV_SYS_MOUNT, then validate
1802 * that user is allowed to hand out each permission in
1803 * the nvlist(s)
1804 */
1805
1806 error = secpolicy_zfs(CRED());
1807 if (error) {
1808 if (zc->zc_perm_action == B_FALSE) {
1809 error = dsl_deleg_can_allow(zc->zc_name,
1810 fsaclnv, CRED());
1811 } else {
1812 error = dsl_deleg_can_unallow(zc->zc_name,
1813 fsaclnv, CRED());
1814 }
1815 }
1816
1817 if (error == 0)
1818 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
1819
1820 nvlist_free(fsaclnv);
1821 return (error);
1822}
1823
1824/*
1825 * inputs:
1826 * zc_name name of filesystem
1827 *
1828 * outputs:
1829 * zc_nvlist_src{_size} nvlist of delegated permissions
1830 */
1831static int
1832zfs_ioc_get_fsacl(zfs_cmd_t *zc)
1833{
1834 nvlist_t *nvp;
1835 int error;
1836
1837 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
1838 error = put_nvlist(zc, nvp);
1839 nvlist_free(nvp);
1840 }
1841
1842 return (error);
1843}
1844
1845/*
1846 * inputs:
1847 * zc_name name of volume
1848 *
1849 * outputs: none
1850 */
1851static int
1852zfs_ioc_create_minor(zfs_cmd_t *zc)
1853{
1854 return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
1855}
1856
1857/*
1858 * inputs:
1859 * zc_name name of volume
1860 *
1861 * outputs: none
1862 */
1863static int
1864zfs_ioc_remove_minor(zfs_cmd_t *zc)
1865{
1866 return (zvol_remove_minor(zc->zc_name));
1867}
1868
1869/*
1870 * Search the vfs list for a specified resource. Returns a pointer to it
1871 * or NULL if no suitable entry is found. The caller of this routine
1872 * is responsible for releasing the returned vfs pointer.
1873 */
1874static vfs_t *
1875zfs_get_vfs(const char *resource)
1876{
1877 vfs_t *vfsp;
1878
1879 mtx_lock(&mountlist_mtx);
1880 TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
1881 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
1882 VFS_HOLD(vfsp);
1883 break;
1884 }
1885 }
1886 mtx_unlock(&mountlist_mtx);
1887 return (vfsp);
1888}
1889
1890/* ARGSUSED */
1891static void
1892zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
1893{
1894 zfs_creat_t *zct = arg;
1895
1896 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
1897}
1898
1899#define ZFS_PROP_UNDEFINED ((uint64_t)-1)
1900
1901/*
1902 * inputs:
1903 * createprops list of properties requested by creator
1904 * default_zplver zpl version to use if unspecified in createprops
1905 * fuids_ok fuids allowed in this version of the spa?
1906 * os parent objset pointer (NULL if root fs)
1907 *
1908 * outputs:
1909 * zplprops values for the zplprops we attach to the master node object
1910 * is_ci true if requested file system will be purely case-insensitive
1911 *
1912 * Determine the settings for utf8only, normalization and
1913 * casesensitivity. Specific values may have been requested by the
1914 * creator and/or we can inherit values from the parent dataset. If
1915 * the file system is of too early a vintage, a creator can not
1916 * request settings for these properties, even if the requested
1917 * setting is the default value. We don't actually want to create dsl
1918 * properties for these, so remove them from the source nvlist after
1919 * processing.
1920 */
1921static int
1922zfs_fill_zplprops_impl(objset_t *os, uint64_t default_zplver,
1923 boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
1924 boolean_t *is_ci)
1925{
1926 uint64_t zplver = default_zplver;
1927 uint64_t sense = ZFS_PROP_UNDEFINED;
1928 uint64_t norm = ZFS_PROP_UNDEFINED;
1929 uint64_t u8 = ZFS_PROP_UNDEFINED;
1930
1931 ASSERT(zplprops != NULL);
1932
1933 /*
1934 * Pull out creator prop choices, if any.
1935 */
1936 if (createprops) {
1937 (void) nvlist_lookup_uint64(createprops,
1938 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
1939 (void) nvlist_lookup_uint64(createprops,
1940 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
1941 (void) nvlist_remove_all(createprops,
1942 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
1943 (void) nvlist_lookup_uint64(createprops,
1944 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
1945 (void) nvlist_remove_all(createprops,
1946 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1947 (void) nvlist_lookup_uint64(createprops,
1948 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
1949 (void) nvlist_remove_all(createprops,
1950 zfs_prop_to_name(ZFS_PROP_CASE));
1951 }
1952
1953 /*
1954 * If the zpl version requested is whacky or the file system
1955 * or pool is version is too "young" to support normalization
1956 * and the creator tried to set a value for one of the props,
1957 * error out.
1958 */
1959 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
1960 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
1961 (zplver < ZPL_VERSION_NORMALIZATION &&
1962 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
1963 sense != ZFS_PROP_UNDEFINED)))
1964 return (ENOTSUP);
1965
1966 /*
1967 * Put the version in the zplprops
1968 */
1969 VERIFY(nvlist_add_uint64(zplprops,
1970 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
1971
1972 if (norm == ZFS_PROP_UNDEFINED)
1973 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
1974 VERIFY(nvlist_add_uint64(zplprops,
1975 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
1976
1977 /*
1978 * If we're normalizing, names must always be valid UTF-8 strings.
1979 */
1980 if (norm)
1981 u8 = 1;
1982 if (u8 == ZFS_PROP_UNDEFINED)
1983 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
1984 VERIFY(nvlist_add_uint64(zplprops,
1985 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
1986
1987 if (sense == ZFS_PROP_UNDEFINED)
1988 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
1989 VERIFY(nvlist_add_uint64(zplprops,
1990 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
1991
1992 if (is_ci)
1993 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
1994
1995 return (0);
1996}
1997
1998static int
1999zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2000 nvlist_t *zplprops, boolean_t *is_ci)
2001{
2002 boolean_t fuids_ok = B_TRUE;
2003 uint64_t zplver = ZPL_VERSION;
2004 objset_t *os = NULL;
2005 char parentname[MAXNAMELEN];
2006 char *cp;
2007 int error;
2008
2009 (void) strlcpy(parentname, dataset, sizeof (parentname));
2010 cp = strrchr(parentname, '/');
2011 ASSERT(cp != NULL);
2012 cp[0] = '\0';
2013
2014 if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2015 zplver = ZPL_VERSION_FUID - 1;
2016 fuids_ok = B_FALSE;
2017 }
2018
2019 /*
2020 * Open parent object set so we can inherit zplprop values.
2021 */
2022 if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
2023 DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
2024 return (error);
2025
2026 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2027 zplprops, is_ci);
2028 dmu_objset_close(os);
2029 return (error);
2030}
2031
2032static int
2033zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2034 nvlist_t *zplprops, boolean_t *is_ci)
2035{
2036 boolean_t fuids_ok = B_TRUE;
2037 uint64_t zplver = ZPL_VERSION;
2038 int error;
2039
2040 if (spa_vers < SPA_VERSION_FUID) {
2041 zplver = ZPL_VERSION_FUID - 1;
2042 fuids_ok = B_FALSE;
2043 }
2044
2045 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2046 zplprops, is_ci);
2047 return (error);
2048}
2049
2050/*
2051 * inputs:
2052 * zc_objset_type type of objset to create (fs vs zvol)
2053 * zc_name name of new objset
2054 * zc_value name of snapshot to clone from (may be empty)
2055 * zc_nvlist_src{_size} nvlist of properties to apply
2056 *
2057 * outputs: none
2058 */
2059static int
2060zfs_ioc_create(zfs_cmd_t *zc)
2061{
2062 objset_t *clone;
2063 int error = 0;
2064 zfs_creat_t zct;
2065 nvlist_t *nvprops = NULL;
2066 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2067 dmu_objset_type_t type = zc->zc_objset_type;
2068
2069 switch (type) {
2070
2071 case DMU_OST_ZFS:
2072 cbfunc = zfs_create_cb;
2073 break;
2074
2075 case DMU_OST_ZVOL:
2076 cbfunc = zvol_create_cb;
2077 break;
2078
2079 default:
2080 cbfunc = NULL;
2081 break;
2082 }
2083 if (strchr(zc->zc_name, '@') ||
2084 strchr(zc->zc_name, '%'))
2085 return (EINVAL);
2086
2087 if (zc->zc_nvlist_src != 0 &&
2088 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2089 &nvprops)) != 0)
2090 return (error);
2091
2092 zct.zct_zplprops = NULL;
2093 zct.zct_props = nvprops;
2094
2095 if (zc->zc_value[0] != '\0') {
2096 /*
2097 * We're creating a clone of an existing snapshot.
2098 */
2099 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2100 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2101 nvlist_free(nvprops);
2102 return (EINVAL);
2103 }
2104
2105 error = dmu_objset_open(zc->zc_value, type,
2106 DS_MODE_USER | DS_MODE_READONLY, &clone);
2107 if (error) {
2108 nvlist_free(nvprops);
2109 return (error);
2110 }
2111
2112 error = dmu_objset_create(zc->zc_name, type, clone, 0,
2113 NULL, NULL);
2114 if (error) {
2115 dmu_objset_close(clone);
2116 nvlist_free(nvprops);
2117 return (error);
2118 }
2119 dmu_objset_close(clone);
2120 } else {
2121 boolean_t is_insensitive = B_FALSE;
2122
2123 if (cbfunc == NULL) {
2124 nvlist_free(nvprops);
2125 return (EINVAL);
2126 }
2127
2128 if (type == DMU_OST_ZVOL) {
2129 uint64_t volsize, volblocksize;
2130
2131 if (nvprops == NULL ||
2132 nvlist_lookup_uint64(nvprops,
2133 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2134 &volsize) != 0) {
2135 nvlist_free(nvprops);
2136 return (EINVAL);
2137 }
2138
2139 if ((error = nvlist_lookup_uint64(nvprops,
2140 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2141 &volblocksize)) != 0 && error != ENOENT) {
2142 nvlist_free(nvprops);
2143 return (EINVAL);
2144 }
2145
2146 if (error != 0)
2147 volblocksize = zfs_prop_default_numeric(
2148 ZFS_PROP_VOLBLOCKSIZE);
2149
2150 if ((error = zvol_check_volblocksize(
2151 volblocksize)) != 0 ||
2152 (error = zvol_check_volsize(volsize,
2153 volblocksize)) != 0) {
2154 nvlist_free(nvprops);
2155 return (error);
2156 }
2157 } else if (type == DMU_OST_ZFS) {
2158 int error;
2159
2160 /*
2161 * We have to have normalization and
2162 * case-folding flags correct when we do the
2163 * file system creation, so go figure them out
2164 * now.
2165 */
2166 VERIFY(nvlist_alloc(&zct.zct_zplprops,
2167 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2168 error = zfs_fill_zplprops(zc->zc_name, nvprops,
2169 zct.zct_zplprops, &is_insensitive);
2170 if (error != 0) {
2171 nvlist_free(nvprops);
2172 nvlist_free(zct.zct_zplprops);
2173 return (error);
2174 }
2175 }
2176 error = dmu_objset_create(zc->zc_name, type, NULL,
2177 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2178 nvlist_free(zct.zct_zplprops);
2179 }
2180
2181 /*
2182 * It would be nice to do this atomically.
2183 */
2184 if (error == 0) {
2185 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2186 (void) dmu_objset_destroy(zc->zc_name);
2187 }
2188 nvlist_free(nvprops);
2189 return (error);
2190}
2191
2192struct snap_prop_arg {
2193 nvlist_t *nvprops;
2194 const char *snapname;
2195};
2196
2197static int
2198set_snap_props(char *name, void *arg)
2199{
2200 struct snap_prop_arg *snpa = arg;
2201 int len = strlen(name) + strlen(snpa->snapname) + 2;
2202 char *buf = kmem_alloc(len, KM_SLEEP);
2203 int err;
2204
2205 (void) snprintf(buf, len, "%s@%s", name, snpa->snapname);
2206 err = zfs_set_prop_nvlist(buf, snpa->nvprops);
2207 if (err)
2208 (void) dmu_objset_destroy(buf);
2209 kmem_free(buf, len);
2210 return (err);
2211}
2212
2213/*
2214 * inputs:
2215 * zc_name name of filesystem
2216 * zc_value short name of snapshot
2217 * zc_cookie recursive flag
2218 *
2219 * outputs: none
2220 */
2221static int
2222zfs_ioc_snapshot(zfs_cmd_t *zc)
2223{
2224 nvlist_t *nvprops = NULL;
2225 int error;
2226 boolean_t recursive = zc->zc_cookie;
2227
2228 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2229 return (EINVAL);
2230
2231 if (zc->zc_nvlist_src != 0 &&
2232 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2233 &nvprops)) != 0)
2234 return (error);
2235
2236 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, recursive);
2237
2238 /*
2239 * It would be nice to do this atomically.
2240 */
2241 if (error == 0) {
2242 struct snap_prop_arg snpa;
2243 snpa.nvprops = nvprops;
2244 snpa.snapname = zc->zc_value;
2245 if (recursive) {
2246 error = dmu_objset_find(zc->zc_name,
2247 set_snap_props, &snpa, DS_FIND_CHILDREN);
2248 if (error) {
2249 (void) dmu_snapshots_destroy(zc->zc_name,
2250 zc->zc_value);
2251 }
2252 } else {
2253 error = set_snap_props(zc->zc_name, &snpa);
2254 }
2255 }
2256 nvlist_free(nvprops);
2257 return (error);
2258}
2259
2260int
2261zfs_unmount_snap(char *name, void *arg)
2262{
2263 vfs_t *vfsp = NULL;
2264
2265 if (arg) {
2266 char *snapname = arg;
2267 int len = strlen(name) + strlen(snapname) + 2;
2268 char *buf = kmem_alloc(len, KM_SLEEP);
2269
2270 (void) strcpy(buf, name);
2271 (void) strcat(buf, "@");
2272 (void) strcat(buf, snapname);
2273 vfsp = zfs_get_vfs(buf);
2274 kmem_free(buf, len);
2275 } else if (strchr(name, '@')) {
2276 vfsp = zfs_get_vfs(name);
2277 }
2278
2279 if (vfsp) {
2280 /*
2281 * Always force the unmount for snapshots.
2282 */
2283 int flag = MS_FORCE;
2284 int err;
2285
2286 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2287 VFS_RELE(vfsp);
2288 return (err);
2289 }
2290 VFS_RELE(vfsp);
2291 mtx_lock(&Giant); /* dounmount() */
2292 dounmount(vfsp, flag, curthread);
2293 mtx_unlock(&Giant); /* dounmount() */
2294 }
2295 return (0);
2296}
2297
2298/*
2299 * inputs:
2300 * zc_name name of filesystem
2301 * zc_value short name of snapshot
2302 *
2303 * outputs: none
2304 */
2305static int
2306zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2307{
2308 int err;
2309
2310 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2311 return (EINVAL);
2312 err = dmu_objset_find(zc->zc_name,
2313 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2314 if (err)
2315 return (err);
2316 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2317}
2318
2319/*
2320 * inputs:
2321 * zc_name name of dataset to destroy
2322 * zc_objset_type type of objset
2323 *
2324 * outputs: none
2325 */
2326static int
2327zfs_ioc_destroy(zfs_cmd_t *zc)
2328{
2329 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2330 int err = zfs_unmount_snap(zc->zc_name, NULL);
2331 if (err)
2332 return (err);
2333 }
2334
2335 return (dmu_objset_destroy(zc->zc_name));
2336}
2337
2338/*
2339 * inputs:
2340 * zc_name name of dataset to rollback (to most recent snapshot)
2341 *
2342 * outputs: none
2343 */
2344static int
2345zfs_ioc_rollback(zfs_cmd_t *zc)
2346{
2347 objset_t *os;
2348 int error;
2349 zfsvfs_t *zfsvfs = NULL;
2350
2351 /*
2352 * Get the zfsvfs for the receiving objset. There
2353 * won't be one if we're operating on a zvol, if the
2354 * objset doesn't exist yet, or is not mounted.
2355 */
2356 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
2357 if (error)
2358 return (error);
2359
2360 if (dmu_objset_type(os) == DMU_OST_ZFS) {
2361 mutex_enter(&os->os->os_user_ptr_lock);
2362 zfsvfs = dmu_objset_get_user(os);
2363 if (zfsvfs != NULL)
2364 VFS_HOLD(zfsvfs->z_vfs);
2365 mutex_exit(&os->os->os_user_ptr_lock);
2366 }
2367
2368 if (zfsvfs != NULL) {
2369 char osname[MAXNAMELEN];
2370 int mode;
2371
2372 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2373 if (error == 0) {
2374 int resume_err;
2375
2376 ASSERT(strcmp(osname, zc->zc_name) == 0);
2377 error = dmu_objset_rollback(os);
2378 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2379 error = error ? error : resume_err;
2380 } else {
2381 dmu_objset_close(os);
2382 }
2383 VFS_RELE(zfsvfs->z_vfs);
2384 } else {
2385 error = dmu_objset_rollback(os);
2386 }
2387 /* Note, the dmu_objset_rollback() releases the objset for us. */
2388
2389 return (error);
2390}
2391
2392/*
2393 * inputs:
2394 * zc_name old name of dataset
2395 * zc_value new name of dataset
2396 * zc_cookie recursive flag (only valid for snapshots)
2397 *
2398 * outputs: none
2399 */
2400static int
2401zfs_ioc_rename(zfs_cmd_t *zc)
2402{
2403 boolean_t recursive = zc->zc_cookie & 1;
2404
2405 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2406 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2407 strchr(zc->zc_value, '%'))
2408 return (EINVAL);
2409
2410 /*
2411 * Unmount snapshot unless we're doing a recursive rename,
2412 * in which case the dataset code figures out which snapshots
2413 * to unmount.
2414 */
2415 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2416 zc->zc_objset_type == DMU_OST_ZFS) {
2417 int err = zfs_unmount_snap(zc->zc_name, NULL);
2418 if (err)
2419 return (err);
2420 }
2421 return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2422}
2423
2424static void
2425clear_props(char *dataset, nvlist_t *props)
2426{
2427 zfs_cmd_t *zc;
2428 nvpair_t *prop;
2429
2430 if (props == NULL)
2431 return;
2432 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2433 (void) strcpy(zc->zc_name, dataset);
2434 for (prop = nvlist_next_nvpair(props, NULL); prop;
2435 prop = nvlist_next_nvpair(props, prop)) {
2436 (void) strcpy(zc->zc_value, nvpair_name(prop));
2437 if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2438 (void) zfs_ioc_inherit_prop(zc);
2439 }
2440 kmem_free(zc, sizeof (zfs_cmd_t));
2441}
2442
2443/*
2444 * inputs:
2445 * zc_name name of containing filesystem
2446 * zc_nvlist_src{_size} nvlist of properties to apply
2447 * zc_value name of snapshot to create
2448 * zc_string name of clone origin (if DRR_FLAG_CLONE)
2449 * zc_cookie file descriptor to recv from
2450 * zc_begin_record the BEGIN record of the stream (not byteswapped)
2451 * zc_guid force flag
2452 *
2453 * outputs:
2454 * zc_cookie number of bytes read
2455 */
2456static int
2457zfs_ioc_recv(zfs_cmd_t *zc)
2458{
2459 file_t *fp;
2460 objset_t *os;
2461 dmu_recv_cookie_t drc;
2462 zfsvfs_t *zfsvfs = NULL;
2463 boolean_t force = (boolean_t)zc->zc_guid;
2464 int error, fd;
2465 offset_t off;
2466 nvlist_t *props = NULL;
2467 nvlist_t *origprops = NULL;
2468 objset_t *origin = NULL;
2469 char *tosnap;
2470 char tofs[ZFS_MAXNAMELEN];
2471
2472 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2473 strchr(zc->zc_value, '@') == NULL ||
2474 strchr(zc->zc_value, '%'))
2475 return (EINVAL);
2476
2477 (void) strcpy(tofs, zc->zc_value);
2478 tosnap = strchr(tofs, '@');
2479 *tosnap = '\0';
2480 tosnap++;
2481
2482 if (zc->zc_nvlist_src != 0 &&
2483 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2484 &props)) != 0)
2485 return (error);
2486
2487 fd = zc->zc_cookie;
2488 fp = getf(fd, 0);
2489 if (fp == NULL) {
2490 nvlist_free(props);
2491 return (EBADF);
2492 }
2493
2494 if (dmu_objset_open(tofs, DMU_OST_ANY,
2495 DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2496 /*
2497 * Try to get the zfsvfs for the receiving objset.
2498 * There won't be one if we're operating on a zvol,
2499 * if the objset doesn't exist yet, or is not mounted.
2500 */
2501 mutex_enter(&os->os->os_user_ptr_lock);
2502 if (zfsvfs = dmu_objset_get_user(os)) {
2503 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2504 mutex_exit(&os->os->os_user_ptr_lock);
2505 dmu_objset_close(os);
2506 zfsvfs = NULL;
2507 error = EBUSY;
2508 goto out;
2509 }
2510 VFS_HOLD(zfsvfs->z_vfs);
2511 }
2512 mutex_exit(&os->os->os_user_ptr_lock);
2513
2514 /*
2515 * If new properties are supplied, they are to completely
2516 * replace the existing ones, so stash away the existing ones.
2517 */
2518 if (props)
2519 (void) dsl_prop_get_all(os, &origprops, TRUE);
2520
2521 dmu_objset_close(os);
2522 }
2523
2524 if (zc->zc_string[0]) {
2525 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2526 DS_MODE_USER | DS_MODE_READONLY, &origin);
2527 if (error)
2528 goto out;
2529 }
2530
2531 error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2532 force, origin, zfsvfs != NULL, &drc);
2533 if (origin)
2534 dmu_objset_close(origin);
2535 if (error)
2536 goto out;
2537
2538 /*
2539 * Reset properties. We do this before we receive the stream
2540 * so that the properties are applied to the new data.
2541 */
2542 if (props) {
2543 clear_props(tofs, origprops);
2544 /*
2545 * XXX - Note, this is all-or-nothing; should be best-effort.
2546 */
2547 (void) zfs_set_prop_nvlist(tofs, props);
2548 }
2549
2550 off = fp->f_offset;
2551 error = dmu_recv_stream(&drc, fp, &off);
2552
2553 if (error == 0 && zfsvfs) {
2554 char osname[MAXNAMELEN];
2555 int mode;
2556
2557 /* online recv */
2558 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2559 if (error == 0) {
2560 int resume_err;
2561
2562 error = dmu_recv_end(&drc);
2563 resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2564 error = error ? error : resume_err;
2565 } else {
2566 dmu_recv_abort_cleanup(&drc);
2567 }
2568 } else if (error == 0) {
2569 error = dmu_recv_end(&drc);
2570 }
2571
2572 zc->zc_cookie = off - fp->f_offset;
2573 if (off >= 0 && off <= MAXOFFSET_T)
2574 fp->f_offset = off;
2575
2576 /*
2577 * On error, restore the original props.
2578 */
2579 if (error && props) {
2580 clear_props(tofs, props);
2581 (void) zfs_set_prop_nvlist(tofs, origprops);
2582 }
2583out:
2584 if (zfsvfs) {
2585 mutex_exit(&zfsvfs->z_online_recv_lock);
2586 VFS_RELE(zfsvfs->z_vfs);
2587 }
2588 nvlist_free(props);
2589 nvlist_free(origprops);
2590 releasef(fp);
2591 return (error);
2592}
2593
2594/*
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)
2600 *
2601 * outputs: none
2602 */
2603static int
2604zfs_ioc_send(zfs_cmd_t *zc)
2605{
2606 objset_t *fromsnap = NULL;
2607 objset_t *tosnap;
2608 file_t *fp;
2609 int error;
2610 offset_t off;
2611
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);
2616
2617 if (zc->zc_value[0] != '\0') {
2618 char buf[MAXPATHLEN];
2619 char *cp;
2620
2621 (void) strncpy(buf, zc->zc_name, sizeof (buf));
2622 cp = strchr(buf, '@');
2623 if (cp)
2624 *(cp+1) = 0;
2625 (void) strlcat(buf, zc->zc_value, sizeof (buf));
2626 error = dmu_objset_open(buf, DMU_OST_ANY,
2627 DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2628 if (error) {
2629 dmu_objset_close(tosnap);
2630 return (error);
2631 }
2632 }
2633
2634 fp = getf(zc->zc_cookie, 1);
2635 if (fp == NULL) {
2636 dmu_objset_close(tosnap);
2637 if (fromsnap)
2638 dmu_objset_close(fromsnap);
2639 return (EBADF);
2640 }
2641
2642 off = fp->f_offset;
2643 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp, &off);
2644
2645 if (off >= 0 && off <= MAXOFFSET_T)
2646 fp->f_offset = off;
2647 releasef(fp);
2648 if (fromsnap)
2649 dmu_objset_close(fromsnap);
2650 dmu_objset_close(tosnap);
2651 return (error);
2652}
2653
2654static int
2655zfs_ioc_inject_fault(zfs_cmd_t *zc)
2656{
2657 int id, error;
2658
2659 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2660 &zc->zc_inject_record);
2661
2662 if (error == 0)
2663 zc->zc_guid = (uint64_t)id;
2664
2665 return (error);
2666}
2667
2668static int
2669zfs_ioc_clear_fault(zfs_cmd_t *zc)
2670{
2671 return (zio_clear_fault((int)zc->zc_guid));
2672}
2673
2674static int
2675zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2676{
2677 int id = (int)zc->zc_guid;
2678 int error;
2679
2680 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2681 &zc->zc_inject_record);
2682
2683 zc->zc_guid = id;
2684
2685 return (error);
2686}
2687
2688static int
2689zfs_ioc_error_log(zfs_cmd_t *zc)
2690{
2691 spa_t *spa;
2692 int error;
2693 size_t count = (size_t)zc->zc_nvlist_dst_size;
2694
2695 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2696 return (error);
2697
2698 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2699 &count);
2700 if (error == 0)
2701 zc->zc_nvlist_dst_size = count;
2702 else
2703 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2704
2705 spa_close(spa, FTAG);
2706
2707 return (error);
2708}
2709
2710static int
2711zfs_ioc_clear(zfs_cmd_t *zc)
2712{
2713 spa_t *spa;
2714 vdev_t *vd;
2715 int error;
2716
2717 /*
2718 * On zpool clear we also fix up missing slogs
2719 */
2720 mutex_enter(&spa_namespace_lock);
2721 spa = spa_lookup(zc->zc_name);
2722 if (spa == NULL) {
2723 mutex_exit(&spa_namespace_lock);
2724 return (EIO);
2725 }
2726 if (spa->spa_log_state == SPA_LOG_MISSING) {
2727 /* we need to let spa_open/spa_load clear the chains */
2728 spa->spa_log_state = SPA_LOG_CLEAR;
2729 }
2730 mutex_exit(&spa_namespace_lock);
2731
2732 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2733 return (error);
2734
2735 spa_vdev_state_enter(spa);
2736
2737 if (zc->zc_guid == 0) {
2738 vd = NULL;
2739 } else {
2740 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
2741 if (vd == NULL) {
2742 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
2743 spa_close(spa, FTAG);
2744 return (ENODEV);
2745 }
2746 }
2747
2748 vdev_clear(spa, vd);
2749
2750 (void) spa_vdev_state_exit(spa, NULL, 0);
2751
2752 /*
2753 * Resume any suspended I/Os.
2754 */
2755 zio_resume(spa);
2756
2757 spa_close(spa, FTAG);
2758
2759 return (0);
2760}
2761
2762/*
2763 * inputs:
2764 * zc_name name of filesystem
2765 * zc_value name of origin snapshot
2766 *
2767 * outputs: none
2768 */
2769static int
2770zfs_ioc_promote(zfs_cmd_t *zc)
2771{
2772 char *cp;
2773
2774 /*
2775 * We don't need to unmount *all* the origin fs's snapshots, but
2776 * it's easier.
2777 */
2778 cp = strchr(zc->zc_value, '@');
2779 if (cp)
2780 *cp = '\0';
2781 (void) dmu_objset_find(zc->zc_value,
2782 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
2783 return (dsl_dataset_promote(zc->zc_name));
2784}
2785
2786#ifdef TODO
2787/*
2788 * We don't want to have a hard dependency
2789 * against some special symbols in sharefs
2790 * nfs, and smbsrv. Determine them if needed when
2791 * the first file system is shared.
2792 * Neither sharefs, nfs or smbsrv are unloadable modules.
2793 */
2794int (*znfsexport_fs)(void *arg);
2795int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
2796int (*zsmbexport_fs)(void *arg, boolean_t add_share);
2797
2798int zfs_nfsshare_inited;
2799int zfs_smbshare_inited;
2800
2801ddi_modhandle_t nfs_mod;
2802ddi_modhandle_t sharefs_mod;
2803ddi_modhandle_t smbsrv_mod;
2804#endif
2805kmutex_t zfs_share_lock;
2806
2807#ifdef TODO
2808static int
2809zfs_init_sharefs()
2810{
2811 int error;
2812
2813 ASSERT(MUTEX_HELD(&zfs_share_lock));
2814 /* Both NFS and SMB shares also require sharetab support. */
2815 if (sharefs_mod == NULL && ((sharefs_mod =
2816 ddi_modopen("fs/sharefs",
2817 KRTLD_MODE_FIRST, &error)) == NULL)) {
2818 return (ENOSYS);
2819 }
2820 if (zshare_fs == NULL && ((zshare_fs =
2821 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
2822 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
2823 return (ENOSYS);
2824 }
2825 return (0);
2826}
2827#endif
2828
2829static int
2830zfs_ioc_share(zfs_cmd_t *zc)
2831{
2832#ifdef TODO
2833 int error;
2834 int opcode;
2835
2836 switch (zc->zc_share.z_sharetype) {
2837 case ZFS_SHARE_NFS:
2838 case ZFS_UNSHARE_NFS:
2839 if (zfs_nfsshare_inited == 0) {
2840 mutex_enter(&zfs_share_lock);
2841 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
2842 KRTLD_MODE_FIRST, &error)) == NULL)) {
2843 mutex_exit(&zfs_share_lock);
2844 return (ENOSYS);
2845 }
2846 if (znfsexport_fs == NULL &&
2847 ((znfsexport_fs = (int (*)(void *))
2848 ddi_modsym(nfs_mod,
2849 "nfs_export", &error)) == NULL)) {
2850 mutex_exit(&zfs_share_lock);
2851 return (ENOSYS);
2852 }
2853 error = zfs_init_sharefs();
2854 if (error) {
2855 mutex_exit(&zfs_share_lock);
2856 return (ENOSYS);
2857 }
2858 zfs_nfsshare_inited = 1;
2859 mutex_exit(&zfs_share_lock);
2860 }
2861 break;
2862 case ZFS_SHARE_SMB:
2863 case ZFS_UNSHARE_SMB:
2864 if (zfs_smbshare_inited == 0) {
2865 mutex_enter(&zfs_share_lock);
2866 if (smbsrv_mod == NULL && ((smbsrv_mod =
2867 ddi_modopen("drv/smbsrv",
2868 KRTLD_MODE_FIRST, &error)) == NULL)) {
2869 mutex_exit(&zfs_share_lock);
2870 return (ENOSYS);
2871 }
2872 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
2873 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
2874 "smb_server_share", &error)) == NULL)) {
2875 mutex_exit(&zfs_share_lock);
2876 return (ENOSYS);
2877 }
2878 error = zfs_init_sharefs();
2879 if (error) {
2880 mutex_exit(&zfs_share_lock);
2881 return (ENOSYS);
2882 }
2883 zfs_smbshare_inited = 1;
2884 mutex_exit(&zfs_share_lock);
2885 }
2886 break;
2887 default:
2888 return (EINVAL);
2889 }
2890
2891 switch (zc->zc_share.z_sharetype) {
2892 case ZFS_SHARE_NFS:
2893 case ZFS_UNSHARE_NFS:
2894 if (error =
2895 znfsexport_fs((void *)
2896 (uintptr_t)zc->zc_share.z_exportdata))
2897 return (error);
2898 break;
2899 case ZFS_SHARE_SMB:
2900 case ZFS_UNSHARE_SMB:
2901 if (error = zsmbexport_fs((void *)
2902 (uintptr_t)zc->zc_share.z_exportdata,
2903 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
2904 B_TRUE : B_FALSE)) {
2905 return (error);
2906 }
2907 break;
2908 }
2909
2910 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
2911 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
2912 SHAREFS_ADD : SHAREFS_REMOVE;
2913
2914 /*
2915 * Add or remove share from sharetab
2916 */
2917 error = zshare_fs(opcode,
2918 (void *)(uintptr_t)zc->zc_share.z_sharedata,
2919 zc->zc_share.z_sharemax);
2920
2921 return (error);
2922#else
2923 return (ENOSYS);
2924#endif
2925}
2926
2927/*
2928 * pool create, destroy, and export don't log the history as part of
2929 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
2930 * do the logging of those commands.
2931 */
2932static int
2933zfs_ioc_jail(zfs_cmd_t *zc)
2934{
2935
2936 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
2937 (int)zc->zc_jailid));
2938}
2939
2940static int
2941zfs_ioc_unjail(zfs_cmd_t *zc)
2942{
2943
2944 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
2945 (int)zc->zc_jailid));
2946}
2947
2948static zfs_ioc_vec_t zfs_ioc_vec[] = {
2949 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2950 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2951 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2952 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2953 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE },
2954 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2955 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE },
2956 { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2957 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE },
2958 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2959 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2960 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2961 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2962 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2963 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2964 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2965 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2966 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2967 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2968 { zfs_ioc_dataset_list_next, zfs_secpolicy_read,
2969 DATASET_NAME, B_FALSE },
2970 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read,
2971 DATASET_NAME, B_FALSE },
2972 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE },
2973 { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2974 { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE },
2975 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE },
2976 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2977 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE },
2978 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE },
2979 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE },
2980 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE },
2981 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2982 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2983 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE },
2984 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE },
2985 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2986 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE },
2987 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE },
2988 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE },
2989 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE },
2990 { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE },
2991 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE },
2992 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE },
2993 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE },
2994 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE },
2995 { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi,
2996 DATASET_NAME, B_FALSE },
2997 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE },
2998 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE },
2999 { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE },
3000 { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE }
3001};
3002
3003static int
3004zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
3005 struct thread *td)
3006{
3007 zfs_cmd_t *zc = (void *)addr;
3008 uint_t vec;
3009 int error;
3010
3011 vec = ZFS_IOC(cmd);
3012
3013 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3014 return (EINVAL);
3015
3016 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred);
3017
3018 /*
3019 * Ensure that all pool/dataset names are valid before we pass down to
3020 * the lower layers.
3021 */
3022 if (error == 0) {
3023 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3024 switch (zfs_ioc_vec[vec].zvec_namecheck) {
3025 case POOL_NAME:
3026 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3027 error = EINVAL;
3028 break;
3029
3030 case DATASET_NAME:
3031 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3032 error = EINVAL;
3033 break;
3034
3035 case NO_NAME:
3036 break;
3037 }
3038 }
3039
3040 if (error == 0)
3041 error = zfs_ioc_vec[vec].zvec_func(zc);
3042
3043 if (error == 0) {
3044 if (zfs_ioc_vec[vec].zvec_his_log == B_TRUE)
3045 zfs_log_history(zc);
3046 }
3047
3048 return (error);
3049}
3050
3051/*
3052 * OK, so this is a little weird.
3053 *
3054 * /dev/zfs is the control node, i.e. minor 0.
3055 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3056 *
3057 * /dev/zfs has basically nothing to do except serve up ioctls,
3058 * so most of the standard driver entry points are in zvol.c.
3059 */
3060static struct cdevsw zfs_cdevsw = {
3061 .d_version = D_VERSION,
3062 .d_ioctl = zfsdev_ioctl,
3063 .d_name = ZFS_DEV_NAME
3064};
3065
3066static void
3067zfsdev_init(void)
3068{
3069 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
3070 ZFS_DEV_NAME);
3071}
3072
3073static void
3074zfsdev_fini(void)
3075{
3076 if (zfsdev != NULL)
3077 destroy_dev(zfsdev);
3078}
3079
3080static struct root_hold_token *zfs_root_token;
3081struct proc *zfsproc;
3082
3083uint_t zfs_fsyncer_key;
3084extern uint_t rrw_tsd_key;
3085
3086static int
3087zfs_modevent(module_t mod, int type, void *unused __unused)
3088{
3089 int error = 0;
3090
3091 switch (type) {
3092 case MOD_LOAD:
3093 zfs_root_token = root_mount_hold("ZFS");
3094
3095 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3096
3097 spa_init(FREAD | FWRITE);
3098 zfs_init();
3099 zvol_init();
3100
3101 tsd_create(&zfs_fsyncer_key, NULL);
3102 tsd_create(&rrw_tsd_key, NULL);
3103
3104 printf("ZFS storage pool version " SPA_VERSION_STRING "\n");
3105 root_mount_rel(zfs_root_token);
3106
3107 zfsdev_init();
3108 break;
3109 case MOD_UNLOAD:
3110 if (spa_busy() || zfs_busy() || zvol_busy() ||
3111 zio_injection_enabled) {
3112 error = EBUSY;
3113 break;
3114 }
3115
3116 zfsdev_fini();
3117 zvol_fini();
3118 zfs_fini();
3119 spa_fini();
3120
3121 tsd_destroy(&zfs_fsyncer_key);
3122 tsd_destroy(&rrw_tsd_key);
3123
3124 mutex_destroy(&zfs_share_lock);
3125 break;
3126 default:
3127 error = EOPNOTSUPP;
3128 break;
3129 }
3130 return (error);
3131}
3132
3133static moduledata_t zfs_mod = {
3134 "zfsctrl",
3135 zfs_modevent,
3136 0
3137};
3138DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
3139MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
3140MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);