]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_ioctl.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_ioctl.c
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #pragma ident   "%Z%%M% %I%     %E% SMI"
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/errno.h>
38 #include <sys/uio.h>
39 #include <sys/buf.h>
40 #include <sys/file.h>
41 #include <sys/kmem.h>
42 #include <sys/conf.h>
43 #include <sys/cmn_err.h>
44 #include <sys/stat.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/zap.h>
47 #include <sys/spa.h>
48 #include <sys/spa_impl.h>
49 #include <sys/vdev.h>
50 #include <sys/vdev_impl.h>
51 #include <sys/dmu.h>
52 #include <sys/dsl_dir.h>
53 #include <sys/dsl_dataset.h>
54 #include <sys/dsl_prop.h>
55 #include <sys/sunddi.h>
56 #include <sys/policy.h>
57 #include <sys/zone.h>
58 #include <sys/nvpair.h>
59 #include <sys/mount.h>
60 #include <sys/taskqueue.h>
61 #include <sys/sdt.h>
62 #include <sys/varargs.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zvol.h>
66
67 #include "zfs_namecheck.h"
68 #include "zfs_prop.h"
69
70 CTASSERT(sizeof(zfs_cmd_t) <= PAGE_SIZE);
71
72 static struct cdev *zfsdev;
73
74 extern void zfs_init(void);
75 extern void zfs_fini(void);
76
77 typedef int zfs_ioc_func_t(zfs_cmd_t *);
78 typedef int zfs_secpolicy_func_t(const char *, cred_t *);
79
80 typedef struct zfs_ioc_vec {
81         zfs_ioc_func_t          *zvec_func;
82         zfs_secpolicy_func_t    *zvec_secpolicy;
83         enum {
84                 no_name,
85                 pool_name,
86                 dataset_name
87         }                       zvec_namecheck;
88 } zfs_ioc_vec_t;
89
90 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
91 void
92 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
93 {
94         const char *newfile;
95         char buf[256];
96         va_list adx;
97
98         /*
99          * Get rid of annoying "../common/" prefix to filename.
100          */
101         newfile = strrchr(file, '/');
102         if (newfile != NULL) {
103                 newfile = newfile + 1; /* Get rid of leading / */
104         } else {
105                 newfile = file;
106         }
107
108         va_start(adx, fmt);
109         (void) vsnprintf(buf, sizeof (buf), fmt, adx);
110         va_end(adx);
111
112         /*
113          * To get this data, use the zfs-dprintf probe as so:
114          * dtrace -q -n 'zfs-dprintf \
115          *      /stringof(arg0) == "dbuf.c"/ \
116          *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
117          * arg0 = file name
118          * arg1 = function name
119          * arg2 = line number
120          * arg3 = message
121          */
122         DTRACE_PROBE4(zfs__dprintf,
123             char *, newfile, char *, func, int, line, char *, buf);
124 }
125
126 /*
127  * Policy for top-level read operations (list pools).  Requires no privileges,
128  * and can be used in the local zone, as there is no associated dataset.
129  */
130 /* ARGSUSED */
131 static int
132 zfs_secpolicy_none(const char *unused1, cred_t *cr)
133 {
134         return (0);
135 }
136
137 /*
138  * Policy for dataset read operations (list children, get statistics).  Requires
139  * no privileges, but must be visible in the local zone.
140  */
141 /* ARGSUSED */
142 static int
143 zfs_secpolicy_read(const char *dataset, cred_t *cr)
144 {
145         if (INGLOBALZONE(curproc) ||
146             zone_dataset_visible(dataset, NULL))
147                 return (0);
148
149         return (ENOENT);
150 }
151
152 static int
153 zfs_dozonecheck(const char *dataset, cred_t *cr)
154 {
155         uint64_t zoned;
156         int writable = 1;
157
158         /*
159          * The dataset must be visible by this zone -- check this first
160          * so they don't see EPERM on something they shouldn't know about.
161          */
162         if (!INGLOBALZONE(curproc) &&
163             !zone_dataset_visible(dataset, &writable))
164                 return (ENOENT);
165
166         if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
167                 return (ENOENT);
168
169         if (INGLOBALZONE(curproc)) {
170                 /*
171                  * If the fs is zoned, only root can access it from the
172                  * global zone.
173                  */
174                 if (secpolicy_zfs(cr) && zoned)
175                         return (EPERM);
176         } else {
177                 /*
178                  * If we are in a local zone, the 'zoned' property must be set.
179                  */
180                 if (!zoned)
181                         return (EPERM);
182
183                 /* must be writable by this zone */
184                 if (!writable)
185                         return (EPERM);
186         }
187         return (0);
188 }
189
190 /*
191  * Policy for dataset write operations (create children, set properties, etc).
192  * Requires SYS_MOUNT privilege, and must be writable in the local zone.
193  */
194 int
195 zfs_secpolicy_write(const char *dataset, cred_t *cr)
196 {
197         int error;
198
199         if (error = zfs_dozonecheck(dataset, cr))
200                 return (error);
201
202         return (secpolicy_zfs(cr));
203 }
204
205 /*
206  * Policy for operations that want to write a dataset's parent:
207  * create, destroy, snapshot, clone, restore.
208  */
209 static int
210 zfs_secpolicy_parent(const char *dataset, cred_t *cr)
211 {
212         char parentname[MAXNAMELEN];
213         char *cp;
214
215         /*
216          * Remove the @bla or /bla from the end of the name to get the parent.
217          */
218         (void) strncpy(parentname, dataset, sizeof (parentname));
219         cp = strrchr(parentname, '@');
220         if (cp != NULL) {
221                 cp[0] = '\0';
222         } else {
223                 cp = strrchr(parentname, '/');
224                 if (cp == NULL)
225                         return (ENOENT);
226                 cp[0] = '\0';
227
228         }
229
230         return (zfs_secpolicy_write(parentname, cr));
231 }
232
233 /*
234  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
235  * SYS_CONFIG privilege, which is not available in a local zone.
236  */
237 /* ARGSUSED */
238 static int
239 zfs_secpolicy_config(const char *unused, cred_t *cr)
240 {
241         if (secpolicy_sys_config(cr, B_FALSE) != 0)
242                 return (EPERM);
243
244         return (0);
245 }
246
247 /*
248  * Policy for fault injection.  Requires all privileges.
249  */
250 /* ARGSUSED */
251 static int
252 zfs_secpolicy_inject(const char *unused, cred_t *cr)
253 {
254         return (secpolicy_zinject(cr));
255 }
256
257 /*
258  * Policy for dataset backup operations (sendbackup).
259  * Requires SYS_MOUNT privilege, and must be writable in the local zone.
260  */
261 static int
262 zfs_secpolicy_operator(const char *dataset, cred_t *cr)
263 {
264         int writable = 1;
265
266         if (!INGLOBALZONE(curproc) && !zone_dataset_visible(dataset, &writable))
267                 return (ENOENT);
268         if (secpolicy_zfs(cr) != 0 && !groupmember(GID_OPERATOR, cr))
269                 return (EPERM);
270         return (0);
271 }
272
273 /*
274  * Returns the nvlist as specified by the user in the zfs_cmd_t.
275  */
276 static int
277 get_nvlist(zfs_cmd_t *zc, nvlist_t **nvp)
278 {
279         char *packed;
280         size_t size;
281         int error;
282         nvlist_t *config = NULL;
283
284         /*
285          * Read in and unpack the user-supplied nvlist.
286          */
287         if ((size = zc->zc_nvlist_src_size) == 0)
288                 return (EINVAL);
289
290         packed = kmem_alloc(size, KM_SLEEP);
291
292         if ((error = xcopyin((void *)(uintptr_t)zc->zc_nvlist_src, packed,
293             size)) != 0) {
294                 kmem_free(packed, size);
295                 return (error);
296         }
297
298         if ((error = nvlist_unpack(packed, size, &config, 0)) != 0) {
299                 kmem_free(packed, size);
300                 return (error);
301         }
302
303         kmem_free(packed, size);
304
305         *nvp = config;
306         return (0);
307 }
308
309 static int
310 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
311 {
312         char *packed = NULL;
313         size_t size;
314         int error;
315
316         VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
317
318         if (size > zc->zc_nvlist_dst_size) {
319                 /*
320                  * Solaris returns ENOMEM here, because even if an error is
321                  * returned from an ioctl(2), new zc_nvlist_dst_size will be
322                  * passed to the userland. This is not the case for FreeBSD.
323                  * We need to return 0, so the kernel will copy the
324                  * zc_nvlist_dst_size back and the userland can discover that a
325                  * bigger buffer is needed.
326                  */
327                 error = 0;
328         } else {
329                 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
330                     KM_SLEEP) == 0);
331                 error = xcopyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
332                     size);
333                 kmem_free(packed, size);
334         }
335
336         zc->zc_nvlist_dst_size = size;
337         return (error);
338 }
339
340 static int
341 zfs_ioc_pool_create(zfs_cmd_t *zc)
342 {
343         int error;
344         nvlist_t *config;
345
346         if ((error = get_nvlist(zc, &config)) != 0)
347                 return (error);
348
349         error = spa_create(zc->zc_name, config, zc->zc_value[0] == '\0' ?
350             NULL : zc->zc_value);
351
352         nvlist_free(config);
353
354         return (error);
355 }
356
357 static int
358 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
359 {
360         return (spa_destroy(zc->zc_name));
361 }
362
363 static int
364 zfs_ioc_pool_import(zfs_cmd_t *zc)
365 {
366         int error;
367         nvlist_t *config;
368         uint64_t guid;
369
370         if ((error = get_nvlist(zc, &config)) != 0)
371                 return (error);
372
373         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
374             guid != zc->zc_guid)
375                 error = EINVAL;
376         else
377                 error = spa_import(zc->zc_name, config,
378                     zc->zc_value[0] == '\0' ? NULL : zc->zc_value);
379
380         nvlist_free(config);
381
382         return (error);
383 }
384
385 static int
386 zfs_ioc_pool_export(zfs_cmd_t *zc)
387 {
388         return (spa_export(zc->zc_name, NULL));
389 }
390
391 static int
392 zfs_ioc_pool_configs(zfs_cmd_t *zc)
393 {
394         nvlist_t *configs;
395         int error;
396
397         if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
398                 return (EEXIST);
399
400         error = put_nvlist(zc, configs);
401
402         nvlist_free(configs);
403
404         return (error);
405 }
406
407 static int
408 zfs_ioc_pool_stats(zfs_cmd_t *zc)
409 {
410         nvlist_t *config;
411         int error;
412         int ret = 0;
413
414         error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
415             sizeof (zc->zc_value));
416
417         if (config != NULL) {
418                 ret = put_nvlist(zc, config);
419                 nvlist_free(config);
420
421                 /*
422                  * The config may be present even if 'error' is non-zero.
423                  * In this case we return success, and preserve the real errno
424                  * in 'zc_cookie'.
425                  */
426                 zc->zc_cookie = error;
427         } else {
428                 ret = error;
429         }
430
431         return (ret);
432 }
433
434 /*
435  * Try to import the given pool, returning pool stats as appropriate so that
436  * user land knows which devices are available and overall pool health.
437  */
438 static int
439 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
440 {
441         nvlist_t *tryconfig, *config;
442         int error;
443
444         if ((error = get_nvlist(zc, &tryconfig)) != 0)
445                 return (error);
446
447         config = spa_tryimport(tryconfig);
448
449         nvlist_free(tryconfig);
450
451         if (config == NULL)
452                 return (EINVAL);
453
454         error = put_nvlist(zc, config);
455         nvlist_free(config);
456
457         return (error);
458 }
459
460 static int
461 zfs_ioc_pool_scrub(zfs_cmd_t *zc)
462 {
463         spa_t *spa;
464         int error;
465
466         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
467                 return (error);
468
469         error = spa_scrub(spa, zc->zc_cookie, B_FALSE);
470
471         spa_close(spa, FTAG);
472
473         return (error);
474 }
475
476 static int
477 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
478 {
479         spa_t *spa;
480         int error;
481
482         error = spa_open(zc->zc_name, &spa, FTAG);
483         if (error == 0) {
484                 spa_freeze(spa);
485                 spa_close(spa, FTAG);
486         }
487         return (error);
488 }
489
490 static int
491 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
492 {
493         spa_t *spa;
494         int error;
495
496         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
497                 return (error);
498
499         spa_upgrade(spa);
500
501         spa_close(spa, FTAG);
502
503         return (error);
504 }
505
506 static int
507 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
508 {
509         spa_t *spa;
510         char *hist_buf;
511         uint64_t size;
512         int error;
513
514         if ((size = zc->zc_history_len) == 0)
515                 return (EINVAL);
516
517         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
518                 return (error);
519
520         if (spa_version(spa) < ZFS_VERSION_ZPOOL_HISTORY) {
521                 spa_close(spa, FTAG);
522                 return (ENOTSUP);
523         }
524
525         hist_buf = kmem_alloc(size, KM_SLEEP);
526         if ((error = spa_history_get(spa, &zc->zc_history_offset,
527             &zc->zc_history_len, hist_buf)) == 0) {
528                 error = xcopyout(hist_buf, (char *)(uintptr_t)zc->zc_history,
529                     zc->zc_history_len);
530         }
531
532         spa_close(spa, FTAG);
533         kmem_free(hist_buf, size);
534         return (error);
535 }
536
537 static int
538 zfs_ioc_pool_log_history(zfs_cmd_t *zc)
539 {
540         spa_t *spa;
541         char *history_str = NULL;
542         size_t size;
543         int error;
544
545         size = zc->zc_history_len;
546         if (size == 0 || size > HIS_MAX_RECORD_LEN)
547                 return (EINVAL);
548
549         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
550                 return (error);
551
552         if (spa_version(spa) < ZFS_VERSION_ZPOOL_HISTORY) {
553                 spa_close(spa, FTAG);
554                 return (ENOTSUP);
555         }
556
557         /* add one for the NULL delimiter */
558         size++;
559         history_str = kmem_alloc(size, KM_SLEEP);
560         if ((error = xcopyin((void *)(uintptr_t)zc->zc_history, history_str,
561             size)) != 0) {
562                 spa_close(spa, FTAG);
563                 kmem_free(history_str, size);
564                 return (error);
565         }
566         history_str[size - 1] = '\0';
567
568         error = spa_history_log(spa, history_str, zc->zc_history_offset);
569
570         spa_close(spa, FTAG);
571         kmem_free(history_str, size);
572
573         return (error);
574 }
575
576 static int
577 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
578 {
579         int error;
580
581         if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value))
582                 return (error);
583
584         return (0);
585 }
586
587 static int
588 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
589 {
590         objset_t *osp;
591         int error;
592
593         if ((error = dmu_objset_open(zc->zc_name, DMU_OST_ZFS,
594             DS_MODE_NONE | DS_MODE_READONLY, &osp)) != 0)
595                 return (error);
596
597         error = zfs_obj_to_path(osp, zc->zc_obj, zc->zc_value,
598             sizeof (zc->zc_value));
599         dmu_objset_close(osp);
600
601         return (error);
602 }
603
604 static int
605 zfs_ioc_vdev_add(zfs_cmd_t *zc)
606 {
607         spa_t *spa;
608         int error;
609         nvlist_t *config;
610
611         error = spa_open(zc->zc_name, &spa, FTAG);
612         if (error != 0)
613                 return (error);
614
615         /*
616          * A root pool with concatenated devices is not supported.
617          * Thus, can not add a device to a root pool with one device.
618          */
619         if (spa->spa_root_vdev->vdev_children == 1 && spa->spa_bootfs != 0) {
620                 spa_close(spa, FTAG);
621                 return (EDOM);
622         }
623
624         if ((error = get_nvlist(zc, &config)) == 0) {
625                 error = spa_vdev_add(spa, config);
626                 nvlist_free(config);
627         }
628
629         spa_close(spa, FTAG);
630         return (error);
631 }
632
633 static int
634 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
635 {
636         spa_t *spa;
637         int error;
638
639         error = spa_open(zc->zc_name, &spa, FTAG);
640         if (error != 0)
641                 return (error);
642         error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
643         spa_close(spa, FTAG);
644         return (error);
645 }
646
647 static int
648 zfs_ioc_vdev_online(zfs_cmd_t *zc)
649 {
650         spa_t *spa;
651         int error;
652
653         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
654                 return (error);
655         error = vdev_online(spa, zc->zc_guid);
656         spa_close(spa, FTAG);
657         return (error);
658 }
659
660 static int
661 zfs_ioc_vdev_offline(zfs_cmd_t *zc)
662 {
663         spa_t *spa;
664         int istmp = zc->zc_cookie;
665         int error;
666
667         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
668                 return (error);
669         error = vdev_offline(spa, zc->zc_guid, istmp);
670         spa_close(spa, FTAG);
671         return (error);
672 }
673
674 static int
675 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
676 {
677         spa_t *spa;
678         int replacing = zc->zc_cookie;
679         nvlist_t *config;
680         int error;
681
682         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
683                 return (error);
684
685         if ((error = get_nvlist(zc, &config)) == 0) {
686                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
687                 nvlist_free(config);
688         }
689
690         spa_close(spa, FTAG);
691         return (error);
692 }
693
694 static int
695 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
696 {
697         spa_t *spa;
698         int error;
699
700         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
701                 return (error);
702
703         error = spa_vdev_detach(spa, zc->zc_guid, B_FALSE);
704
705         spa_close(spa, FTAG);
706         return (error);
707 }
708
709 static int
710 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
711 {
712         spa_t *spa;
713         char *path = zc->zc_value;
714         uint64_t guid = zc->zc_guid;
715         int error;
716
717         error = spa_open(zc->zc_name, &spa, FTAG);
718         if (error != 0)
719                 return (error);
720
721         error = spa_vdev_setpath(spa, guid, path);
722         spa_close(spa, FTAG);
723         return (error);
724 }
725
726 static int
727 zfs_ioc_objset_stats(zfs_cmd_t *zc)
728 {
729         objset_t *os = NULL;
730         int error;
731         nvlist_t *nv;
732
733 retry:
734         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
735             DS_MODE_STANDARD | DS_MODE_READONLY, &os);
736         if (error != 0) {
737                 /*
738                  * This is ugly: dmu_objset_open() can return EBUSY if
739                  * the objset is held exclusively. Fortunately this hold is
740                  * only for a short while, so we retry here.
741                  * This avoids user code having to handle EBUSY,
742                  * for example for a "zfs list".
743                  */
744                 if (error == EBUSY) {
745                         delay(1);
746                         goto retry;
747                 }
748                 return (error);
749         }
750
751         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
752
753         if (zc->zc_nvlist_dst != 0 &&
754             (error = dsl_prop_get_all(os, &nv)) == 0) {
755                 dmu_objset_stats(os, nv);
756                 /*
757                  * NB: zvol_get_stats() will read the objset contents,
758                  * which we aren't supposed to do with a
759                  * DS_MODE_STANDARD open, because it could be
760                  * inconsistent.  So this is a bit of a workaround...
761                  */
762                 if (!zc->zc_objset_stats.dds_inconsistent &&
763                     dmu_objset_type(os) == DMU_OST_ZVOL)
764                         VERIFY(zvol_get_stats(os, nv) == 0);
765                 error = put_nvlist(zc, nv);
766                 nvlist_free(nv);
767         }
768
769         spa_altroot(dmu_objset_spa(os), zc->zc_value, sizeof (zc->zc_value));
770
771         dmu_objset_close(os);
772         if (error == ENOMEM)
773                 error = 0;
774         return (error);
775 }
776
777 static int
778 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
779 {
780         objset_t *os;
781         int error;
782         char *p;
783
784 retry:
785         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
786             DS_MODE_STANDARD | DS_MODE_READONLY, &os);
787         if (error != 0) {
788                 /*
789                  * This is ugly: dmu_objset_open() can return EBUSY if
790                  * the objset is held exclusively. Fortunately this hold is
791                  * only for a short while, so we retry here.
792                  * This avoids user code having to handle EBUSY,
793                  * for example for a "zfs list".
794                  */
795                 if (error == EBUSY) {
796                         delay(1);
797                         goto retry;
798                 }
799                 if (error == ENOENT)
800                         error = ESRCH;
801                 return (error);
802         }
803
804         p = strrchr(zc->zc_name, '/');
805         if (p == NULL || p[1] != '\0')
806                 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
807         p = zc->zc_name + strlen(zc->zc_name);
808
809         do {
810                 error = dmu_dir_list_next(os,
811                     sizeof (zc->zc_name) - (p - zc->zc_name), p,
812                     NULL, &zc->zc_cookie);
813                 if (error == ENOENT)
814                         error = ESRCH;
815         } while (error == 0 && !INGLOBALZONE(curproc) &&
816             !zone_dataset_visible(zc->zc_name, NULL));
817
818         /*
819          * If it's a hidden dataset (ie. with a '$' in its name), don't
820          * try to get stats for it.  Userland will skip over it.
821          */
822         if (error == 0 && strchr(zc->zc_name, '$') == NULL)
823                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
824
825         dmu_objset_close(os);
826         return (error);
827 }
828
829 static int
830 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
831 {
832         objset_t *os;
833         int error;
834
835 retry:
836         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
837             DS_MODE_STANDARD | DS_MODE_READONLY, &os);
838         if (error != 0) {
839                 /*
840                  * This is ugly: dmu_objset_open() can return EBUSY if
841                  * the objset is held exclusively. Fortunately this hold is
842                  * only for a short while, so we retry here.
843                  * This avoids user code having to handle EBUSY,
844                  * for example for a "zfs list".
845                  */
846                 if (error == EBUSY) {
847                         delay(1);
848                         goto retry;
849                 }
850                 if (error == ENOENT)
851                         error = ESRCH;
852                 return (error);
853         }
854
855         /*
856          * A dataset name of maximum length cannot have any snapshots,
857          * so exit immediately.
858          */
859         if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
860                 dmu_objset_close(os);
861                 return (ESRCH);
862         }
863
864         error = dmu_snapshot_list_next(os,
865             sizeof (zc->zc_name) - strlen(zc->zc_name),
866             zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie);
867         if (error == ENOENT)
868                 error = ESRCH;
869
870         if (error == 0)
871                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
872
873         dmu_objset_close(os);
874         return (error);
875 }
876
877 static int
878 zfs_set_prop_nvlist(const char *name, dev_t dev, cred_t *cr, nvlist_t *nvl)
879 {
880         nvpair_t *elem;
881         int error;
882         const char *propname;
883         zfs_prop_t prop;
884         uint64_t intval;
885         char *strval;
886         char buf[MAXNAMELEN];
887         const char *p;
888         spa_t *spa;
889
890         elem = NULL;
891         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
892                 propname = nvpair_name(elem);
893
894                 if ((prop = zfs_name_to_prop(propname)) ==
895                     ZFS_PROP_INVAL) {
896                         /*
897                          * If this is a user-defined property, it must be a
898                          * string, and there is no further validation to do.
899                          */
900                         if (!zfs_prop_user(propname) ||
901                             nvpair_type(elem) != DATA_TYPE_STRING)
902                                 return (EINVAL);
903
904                         VERIFY(nvpair_value_string(elem, &strval) == 0);
905                         error = dsl_prop_set(name, propname, 1,
906                             strlen(strval) + 1, strval);
907                         if (error == 0)
908                                 continue;
909                         else
910                                 return (error);
911                 }
912
913                 /*
914                  * Check permissions for special properties.
915                  */
916                 switch (prop) {
917                 case ZFS_PROP_ZONED:
918                         /*
919                          * Disallow setting of 'zoned' from within a local zone.
920                          */
921                         if (!INGLOBALZONE(curproc))
922                                 return (EPERM);
923                         break;
924
925                 case ZFS_PROP_QUOTA:
926                         if (error = zfs_dozonecheck(name, cr))
927                                 return (error);
928
929                         if (!INGLOBALZONE(curproc)) {
930                                 uint64_t zoned;
931                                 char setpoint[MAXNAMELEN];
932                                 int dslen;
933                                 /*
934                                  * Unprivileged users are allowed to modify the
935                                  * quota on things *under* (ie. contained by)
936                                  * the thing they own.
937                                  */
938                                 if (dsl_prop_get_integer(name, "jailed", &zoned,
939                                     setpoint))
940                                         return (EPERM);
941                                 if (!zoned) /* this shouldn't happen */
942                                         return (EPERM);
943                                 dslen = strlen(name);
944                                 if (dslen <= strlen(setpoint))
945                                         return (EPERM);
946                         }
947                         break;
948
949                 case ZFS_PROP_COMPRESSION:
950                         /*
951                          * If the user specified gzip compression, make sure
952                          * the SPA supports it. We ignore any errors here since
953                          * we'll catch them later.
954                          */
955                         if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
956                             nvpair_value_uint64(elem, &intval) == 0 &&
957                             intval >= ZIO_COMPRESS_GZIP_1 &&
958                             intval <= ZIO_COMPRESS_GZIP_9) {
959                                 if ((p = strchr(name, '/')) == NULL) {
960                                         p = name;
961                                 } else {
962                                         bcopy(name, buf, p - name);
963                                         buf[p - name] = '\0';
964                                         p = buf;
965                                 }
966
967                                 if (spa_open(p, &spa, FTAG) == 0) {
968                                         if (spa_version(spa) <
969                                             ZFS_VERSION_GZIP_COMPRESSION) {
970                                                 spa_close(spa, FTAG);
971                                                 return (ENOTSUP);
972                                         }
973
974                                         spa_close(spa, FTAG);
975                                 }
976                         }
977                         break;
978                 }
979
980                 switch (prop) {
981                 case ZFS_PROP_QUOTA:
982                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
983                             (error = dsl_dir_set_quota(name,
984                             intval)) != 0)
985                                 return (error);
986                         break;
987
988                 case ZFS_PROP_RESERVATION:
989                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
990                             (error = dsl_dir_set_reservation(name,
991                             intval)) != 0)
992                                 return (error);
993                         break;
994
995                 case ZFS_PROP_VOLSIZE:
996                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
997                             (error = zvol_set_volsize(name, dev,
998                             intval)) != 0)
999                                 return (error);
1000                         break;
1001
1002                 case ZFS_PROP_VOLBLOCKSIZE:
1003                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1004                             (error = zvol_set_volblocksize(name,
1005                             intval)) != 0)
1006                                 return (error);
1007                         break;
1008
1009                 default:
1010                         if (nvpair_type(elem) == DATA_TYPE_STRING) {
1011                                 if (zfs_prop_get_type(prop) !=
1012                                     prop_type_string)
1013                                         return (EINVAL);
1014                                 VERIFY(nvpair_value_string(elem, &strval) == 0);
1015                                 if ((error = dsl_prop_set(name,
1016                                     nvpair_name(elem), 1, strlen(strval) + 1,
1017                                     strval)) != 0)
1018                                         return (error);
1019                         } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1020                                 const char *unused;
1021
1022                                 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1023
1024                                 switch (zfs_prop_get_type(prop)) {
1025                                 case prop_type_number:
1026                                         break;
1027                                 case prop_type_boolean:
1028                                         if (intval > 1)
1029                                                 return (EINVAL);
1030                                         break;
1031                                 case prop_type_string:
1032                                         return (EINVAL);
1033                                 case prop_type_index:
1034                                         if (zfs_prop_index_to_string(prop,
1035                                             intval, &unused) != 0)
1036                                                 return (EINVAL);
1037                                         break;
1038                                 default:
1039                                         cmn_err(CE_PANIC, "unknown property "
1040                                             "type");
1041                                         break;
1042                                 }
1043
1044                                 if ((error = dsl_prop_set(name, propname,
1045                                     8, 1, &intval)) != 0)
1046                                         return (error);
1047                         } else {
1048                                 return (EINVAL);
1049                         }
1050                         break;
1051                 }
1052         }
1053
1054         return (0);
1055 }
1056
1057 static int
1058 zfs_ioc_set_prop(zfs_cmd_t *zc)
1059 {
1060         nvlist_t *nvl;
1061         int error;
1062         zfs_prop_t prop;
1063
1064         /*
1065          * If zc_value is set, then this is an attempt to inherit a value.
1066          * Otherwise, zc_nvlist refers to a list of properties to set.
1067          */
1068         if (zc->zc_value[0] != '\0') {
1069                 if (!zfs_prop_user(zc->zc_value) &&
1070                     ((prop = zfs_name_to_prop(zc->zc_value)) ==
1071                     ZFS_PROP_INVAL ||
1072                     !zfs_prop_inheritable(prop)))
1073                         return (EINVAL);
1074
1075                 return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1076         }
1077
1078         if ((error = get_nvlist(zc, &nvl)) != 0)
1079                 return (error);
1080
1081         error = zfs_set_prop_nvlist(zc->zc_name, zc->zc_dev,
1082             (cred_t *)(uintptr_t)zc->zc_cred, nvl);
1083         nvlist_free(nvl);
1084         return (error);
1085 }
1086
1087 static int
1088 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1089 {
1090         nvlist_t *nvl;
1091         int error, reset_bootfs = 0;
1092         uint64_t objnum;
1093         zpool_prop_t prop;
1094         nvpair_t *elem;
1095         char *propname, *strval;
1096         spa_t *spa;
1097         vdev_t *rvdev;
1098         char *vdev_type;
1099         objset_t *os;
1100
1101         if ((error = get_nvlist(zc, &nvl)) != 0)
1102                 return (error);
1103
1104         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1105                 nvlist_free(nvl);
1106                 return (error);
1107         }
1108
1109         if (spa_version(spa) < ZFS_VERSION_BOOTFS) {
1110                 nvlist_free(nvl);
1111                 spa_close(spa, FTAG);
1112                 return (ENOTSUP);
1113         }
1114
1115         elem = NULL;
1116         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1117
1118                 propname = nvpair_name(elem);
1119
1120                 if ((prop = zpool_name_to_prop(propname)) ==
1121                     ZFS_PROP_INVAL) {
1122                         nvlist_free(nvl);
1123                         spa_close(spa, FTAG);
1124                         return (EINVAL);
1125                 }
1126
1127                 switch (prop) {
1128                 case ZFS_PROP_BOOTFS:
1129                         /*
1130                          * A bootable filesystem can not be on a RAIDZ pool
1131                          * nor a striped pool with more than 1 device.
1132                          */
1133                         rvdev = spa->spa_root_vdev;
1134                         vdev_type =
1135                             rvdev->vdev_child[0]->vdev_ops->vdev_op_type;
1136                         if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
1137                             (strcmp(vdev_type, VDEV_TYPE_MIRROR) != 0 &&
1138                             rvdev->vdev_children > 1)) {
1139                                 error = ENOTSUP;
1140                                 break;
1141                         }
1142
1143                         reset_bootfs = 1;
1144
1145                         VERIFY(nvpair_value_string(elem, &strval) == 0);
1146                         if (strval == NULL || strval[0] == '\0') {
1147                                 objnum =
1148                                     zfs_prop_default_numeric(ZFS_PROP_BOOTFS);
1149                                 break;
1150                         }
1151
1152                         if (error = dmu_objset_open(strval, DMU_OST_ZFS,
1153                             DS_MODE_STANDARD | DS_MODE_READONLY, &os))
1154                                 break;
1155                         objnum = dmu_objset_id(os);
1156                         dmu_objset_close(os);
1157                         break;
1158
1159                 default:
1160                         error = EINVAL;
1161                 }
1162
1163                 if (error)
1164                         break;
1165         }
1166         if (error == 0) {
1167                 if (reset_bootfs) {
1168                         VERIFY(nvlist_remove(nvl,
1169                             zpool_prop_to_name(ZFS_PROP_BOOTFS),
1170                             DATA_TYPE_STRING) == 0);
1171                         VERIFY(nvlist_add_uint64(nvl,
1172                             zpool_prop_to_name(ZFS_PROP_BOOTFS), objnum) == 0);
1173                 }
1174                 error = spa_set_props(spa, nvl);
1175         }
1176
1177         nvlist_free(nvl);
1178         spa_close(spa, FTAG);
1179
1180         return (error);
1181 }
1182
1183 static int
1184 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
1185 {
1186         spa_t *spa;
1187         int error;
1188         nvlist_t *nvp = NULL;
1189
1190         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1191                 return (error);
1192
1193         error = spa_get_props(spa, &nvp);
1194
1195         if (error == 0 && zc->zc_nvlist_dst != 0)
1196                 error = put_nvlist(zc, nvp);
1197         else
1198                 error = EFAULT;
1199
1200         spa_close(spa, FTAG);
1201
1202         if (nvp)
1203                 nvlist_free(nvp);
1204         return (error);
1205 }
1206
1207 static int
1208 zfs_ioc_create_minor(zfs_cmd_t *zc)
1209 {
1210         return (zvol_create_minor(zc->zc_name, zc->zc_dev));
1211 }
1212
1213 static int
1214 zfs_ioc_remove_minor(zfs_cmd_t *zc)
1215 {
1216         return (zvol_remove_minor(zc->zc_name));
1217 }
1218
1219 /*
1220  * Search the vfs list for a specified resource.  Returns a pointer to it
1221  * or NULL if no suitable entry is found. The caller of this routine
1222  * is responsible for releasing the returned vfs pointer.
1223  */
1224 static vfs_t *
1225 zfs_get_vfs(const char *resource)
1226 {
1227         vfs_t *vfsp;
1228
1229         mtx_lock(&mountlist_mtx);
1230         TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
1231                 if (strcmp(vfsp->mnt_stat.f_mntfromname, resource) == 0) {
1232                         VFS_HOLD(vfsp);
1233                         break;
1234                 }
1235         }
1236         mtx_unlock(&mountlist_mtx);
1237         return (vfsp);
1238 }
1239
1240 static void
1241 zfs_create_cb(objset_t *os, void *arg, dmu_tx_t *tx)
1242 {
1243         zfs_create_data_t *zc = arg;
1244
1245         zfs_create_fs(os, (cred_t *)(uintptr_t)zc->zc_cred, tx);
1246 }
1247
1248 static int
1249 zfs_ioc_create(zfs_cmd_t *zc)
1250 {
1251         objset_t *clone;
1252         int error = 0;
1253         zfs_create_data_t cbdata = { 0 };
1254         void (*cbfunc)(objset_t *os, void *arg, dmu_tx_t *tx);
1255         dmu_objset_type_t type = zc->zc_objset_type;
1256
1257         switch (type) {
1258
1259         case DMU_OST_ZFS:
1260                 cbfunc = zfs_create_cb;
1261                 break;
1262
1263         case DMU_OST_ZVOL:
1264                 cbfunc = zvol_create_cb;
1265                 break;
1266
1267         default:
1268                 cbfunc = NULL;
1269         }
1270         if (strchr(zc->zc_name, '@'))
1271                 return (EINVAL);
1272
1273         if (zc->zc_nvlist_src != 0 &&
1274             (error = get_nvlist(zc, &cbdata.zc_props)) != 0)
1275                 return (error);
1276
1277         cbdata.zc_cred = (cred_t *)(uintptr_t)zc->zc_cred;
1278         cbdata.zc_dev = (dev_t)zc->zc_dev;
1279
1280         if (zc->zc_value[0] != '\0') {
1281                 /*
1282                  * We're creating a clone of an existing snapshot.
1283                  */
1284                 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1285                 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
1286                         nvlist_free(cbdata.zc_props);
1287                         return (EINVAL);
1288                 }
1289
1290                 error = dmu_objset_open(zc->zc_value, type,
1291                     DS_MODE_STANDARD | DS_MODE_READONLY, &clone);
1292                 if (error) {
1293                         nvlist_free(cbdata.zc_props);
1294                         return (error);
1295                 }
1296                 error = dmu_objset_create(zc->zc_name, type, clone, NULL, NULL);
1297                 dmu_objset_close(clone);
1298         } else {
1299                 if (cbfunc == NULL) {
1300                         nvlist_free(cbdata.zc_props);
1301                         return (EINVAL);
1302                 }
1303
1304                 if (type == DMU_OST_ZVOL) {
1305                         uint64_t volsize, volblocksize;
1306
1307                         if (cbdata.zc_props == NULL ||
1308                             nvlist_lookup_uint64(cbdata.zc_props,
1309                             zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1310                             &volsize) != 0) {
1311                                 nvlist_free(cbdata.zc_props);
1312                                 return (EINVAL);
1313                         }
1314
1315                         if ((error = nvlist_lookup_uint64(cbdata.zc_props,
1316                             zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1317                             &volblocksize)) != 0 && error != ENOENT) {
1318                                 nvlist_free(cbdata.zc_props);
1319                                 return (EINVAL);
1320                         }
1321
1322                         if (error != 0)
1323                                 volblocksize = zfs_prop_default_numeric(
1324                                     ZFS_PROP_VOLBLOCKSIZE);
1325
1326                         if ((error = zvol_check_volblocksize(
1327                             volblocksize)) != 0 ||
1328                             (error = zvol_check_volsize(volsize,
1329                             volblocksize)) != 0) {
1330                                 nvlist_free(cbdata.zc_props);
1331                                 return (error);
1332                         }
1333                 }
1334
1335                 error = dmu_objset_create(zc->zc_name, type, NULL, cbfunc,
1336                     &cbdata);
1337         }
1338
1339         /*
1340          * It would be nice to do this atomically.
1341          */
1342         if (error == 0) {
1343                 if ((error = zfs_set_prop_nvlist(zc->zc_name,
1344                     zc->zc_dev, (cred_t *)(uintptr_t)zc->zc_cred,
1345                     cbdata.zc_props)) != 0)
1346                         (void) dmu_objset_destroy(zc->zc_name);
1347         }
1348
1349         nvlist_free(cbdata.zc_props);
1350         return (error);
1351 }
1352
1353 static int
1354 zfs_ioc_snapshot(zfs_cmd_t *zc)
1355 {
1356         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1357                 return (EINVAL);
1358         return (dmu_objset_snapshot(zc->zc_name,
1359             zc->zc_value, zc->zc_cookie));
1360 }
1361
1362 int
1363 zfs_unmount_snap(char *name, void *arg)
1364 {
1365         char *snapname = arg;
1366         char *cp;
1367         vfs_t *vfsp = NULL;
1368
1369         /*
1370          * Snapshots (which are under .zfs control) must be unmounted
1371          * before they can be destroyed.
1372          */
1373
1374         if (snapname) {
1375                 (void) strcat(name, "@");
1376                 (void) strcat(name, snapname);
1377                 vfsp = zfs_get_vfs(name);
1378                 cp = strchr(name, '@');
1379                 *cp = '\0';
1380         } else if (strchr(name, '@')) {
1381                 vfsp = zfs_get_vfs(name);
1382         }
1383
1384         if (vfsp) {
1385                 /*
1386                  * Always force the unmount for snapshots.
1387                  */
1388                 int flag = MS_FORCE;
1389                 int err;
1390
1391                 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
1392                         VFS_RELE(vfsp);
1393                         return (err);
1394                 }
1395                 VFS_RELE(vfsp);
1396                 mtx_lock(&Giant);       /* dounmount() */
1397                 dounmount(vfsp, flag, curthread);
1398                 mtx_unlock(&Giant);     /* dounmount() */
1399         }
1400         return (0);
1401 }
1402
1403 static int
1404 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
1405 {
1406         int err;
1407
1408         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
1409                 return (EINVAL);
1410         err = dmu_objset_find(zc->zc_name,
1411             zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
1412         if (err)
1413                 return (err);
1414         return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
1415 }
1416
1417 static int
1418 zfs_ioc_destroy(zfs_cmd_t *zc)
1419 {
1420         if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
1421                 int err = zfs_unmount_snap(zc->zc_name, NULL);
1422                 if (err)
1423                         return (err);
1424         }
1425
1426         return (dmu_objset_destroy(zc->zc_name));
1427 }
1428
1429 static int
1430 zfs_ioc_rollback(zfs_cmd_t *zc)
1431 {
1432         return (dmu_objset_rollback(zc->zc_name));
1433 }
1434
1435 static int
1436 zfs_ioc_rename(zfs_cmd_t *zc)
1437 {
1438         int recursive = zc->zc_cookie & 1;
1439
1440         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
1441         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0)
1442                 return (EINVAL);
1443
1444         /*
1445          * Unmount snapshot unless we're doing a recursive rename,
1446          * in which case the dataset code figures out which snapshots
1447          * to unmount.
1448          */
1449         if (!recursive && strchr(zc->zc_name, '@') != NULL &&
1450             zc->zc_objset_type == DMU_OST_ZFS) {
1451                 int err = zfs_unmount_snap(zc->zc_name, NULL);
1452                 if (err)
1453                         return (err);
1454         }
1455
1456         return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
1457 }
1458
1459 static int
1460 zfs_ioc_recvbackup(zfs_cmd_t *zc)
1461 {
1462         kthread_t *td = curthread;
1463         struct file *fp;
1464         int error;
1465         offset_t new_off;
1466
1467         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
1468             strchr(zc->zc_value, '@') == NULL)
1469                 return (EINVAL);
1470
1471         error = fget_read(td, zc->zc_cookie, &fp);
1472         if (error)
1473                 return (error);
1474
1475         error = dmu_recvbackup(zc->zc_value, &zc->zc_begin_record,
1476             &zc->zc_cookie, (boolean_t)zc->zc_guid, fp,
1477             fp->f_offset);
1478
1479         new_off = fp->f_offset + zc->zc_cookie;
1480         fp->f_offset = new_off;
1481
1482         fdrop(fp, td);
1483         return (error);
1484 }
1485
1486 static int
1487 zfs_ioc_sendbackup(zfs_cmd_t *zc)
1488 {
1489         kthread_t *td = curthread;
1490         struct file *fp;
1491         objset_t *fromsnap = NULL;
1492         objset_t *tosnap;
1493         int error, fd;
1494
1495         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1496             DS_MODE_STANDARD | DS_MODE_READONLY, &tosnap);
1497         if (error)
1498                 return (error);
1499
1500         if (zc->zc_value[0] != '\0') {
1501                 char buf[MAXPATHLEN];
1502                 char *cp;
1503
1504                 (void) strncpy(buf, zc->zc_name, sizeof (buf));
1505                 cp = strchr(buf, '@');
1506                 if (cp)
1507                         *(cp+1) = 0;
1508                 (void) strlcat(buf, zc->zc_value, sizeof (buf));
1509                 error = dmu_objset_open(buf, DMU_OST_ANY,
1510                     DS_MODE_STANDARD | DS_MODE_READONLY, &fromsnap);
1511                 if (error) {
1512                         dmu_objset_close(tosnap);
1513                         return (error);
1514                 }
1515         }
1516
1517         fd = zc->zc_cookie;
1518         error = fget_write(td, fd, &fp);
1519         if (error) {
1520                 dmu_objset_close(tosnap);
1521                 if (fromsnap)
1522                         dmu_objset_close(fromsnap);
1523                 return (error);
1524         }
1525
1526         error = dmu_sendbackup(tosnap, fromsnap, fp);
1527
1528         fdrop(fp, td);
1529         if (fromsnap)
1530                 dmu_objset_close(fromsnap);
1531         dmu_objset_close(tosnap);
1532         return (error);
1533 }
1534
1535 static int
1536 zfs_ioc_inject_fault(zfs_cmd_t *zc)
1537 {
1538         int id, error;
1539
1540         error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
1541             &zc->zc_inject_record);
1542
1543         if (error == 0)
1544                 zc->zc_guid = (uint64_t)id;
1545
1546         return (error);
1547 }
1548
1549 static int
1550 zfs_ioc_clear_fault(zfs_cmd_t *zc)
1551 {
1552         return (zio_clear_fault((int)zc->zc_guid));
1553 }
1554
1555 static int
1556 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
1557 {
1558         int id = (int)zc->zc_guid;
1559         int error;
1560
1561         error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
1562             &zc->zc_inject_record);
1563
1564         zc->zc_guid = id;
1565
1566         return (error);
1567 }
1568
1569 static int
1570 zfs_ioc_error_log(zfs_cmd_t *zc)
1571 {
1572         spa_t *spa;
1573         int error;
1574         size_t count = (size_t)zc->zc_nvlist_dst_size;
1575
1576         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1577                 return (error);
1578
1579         error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
1580             &count);
1581         if (error == 0)
1582                 zc->zc_nvlist_dst_size = count;
1583         else
1584                 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
1585
1586         spa_close(spa, FTAG);
1587
1588         return (error);
1589 }
1590
1591 static int
1592 zfs_ioc_clear(zfs_cmd_t *zc)
1593 {
1594         spa_t *spa;
1595         vdev_t *vd;
1596         int error;
1597
1598         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1599                 return (error);
1600
1601         spa_config_enter(spa, RW_WRITER, FTAG);
1602
1603         if (zc->zc_guid == 0) {
1604                 vd = NULL;
1605         } else if ((vd = spa_lookup_by_guid(spa, zc->zc_guid)) == NULL) {
1606                 spa_config_exit(spa, FTAG);
1607                 spa_close(spa, FTAG);
1608                 return (ENODEV);
1609         }
1610
1611         vdev_clear(spa, vd);
1612
1613         spa_config_exit(spa, FTAG);
1614
1615         spa_close(spa, FTAG);
1616
1617         return (0);
1618 }
1619
1620 static int
1621 zfs_ioc_promote(zfs_cmd_t *zc)
1622 {
1623         char *cp;
1624
1625         /*
1626          * We don't need to unmount *all* the origin fs's snapshots, but
1627          * it's easier.
1628          */
1629         cp = strchr(zc->zc_value, '@');
1630         if (cp)
1631                 *cp = '\0';
1632         (void) dmu_objset_find(zc->zc_value,
1633             zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
1634         return (dsl_dataset_promote(zc->zc_name));
1635 }
1636
1637 static int
1638 zfs_ioc_jail(zfs_cmd_t *zc)
1639 {
1640
1641         return (zone_dataset_attach((cred_t *)(uintptr_t)zc->zc_cred,
1642             zc->zc_name, (int)zc->zc_jailid));
1643 }
1644
1645 static int
1646 zfs_ioc_unjail(zfs_cmd_t *zc)
1647 {
1648
1649         return (zone_dataset_detach((cred_t *)(uintptr_t)zc->zc_cred,
1650             zc->zc_name, (int)zc->zc_jailid));
1651 }
1652
1653 static zfs_ioc_vec_t zfs_ioc_vec[] = {
1654         { zfs_ioc_pool_create,          zfs_secpolicy_config,   pool_name },
1655         { zfs_ioc_pool_destroy,         zfs_secpolicy_config,   pool_name },
1656         { zfs_ioc_pool_import,          zfs_secpolicy_config,   pool_name },
1657         { zfs_ioc_pool_export,          zfs_secpolicy_config,   pool_name },
1658         { zfs_ioc_pool_configs,         zfs_secpolicy_none,     no_name },
1659         { zfs_ioc_pool_stats,           zfs_secpolicy_read,     pool_name },
1660         { zfs_ioc_pool_tryimport,       zfs_secpolicy_config,   no_name },
1661         { zfs_ioc_pool_scrub,           zfs_secpolicy_config,   pool_name },
1662         { zfs_ioc_pool_freeze,          zfs_secpolicy_config,   no_name },
1663         { zfs_ioc_pool_upgrade,         zfs_secpolicy_config,   pool_name },
1664         { zfs_ioc_pool_get_history,     zfs_secpolicy_config,   pool_name },
1665         { zfs_ioc_pool_log_history,     zfs_secpolicy_config,   pool_name },
1666         { zfs_ioc_vdev_add,             zfs_secpolicy_config,   pool_name },
1667         { zfs_ioc_vdev_remove,          zfs_secpolicy_config,   pool_name },
1668         { zfs_ioc_vdev_online,          zfs_secpolicy_config,   pool_name },
1669         { zfs_ioc_vdev_offline,         zfs_secpolicy_config,   pool_name },
1670         { zfs_ioc_vdev_attach,          zfs_secpolicy_config,   pool_name },
1671         { zfs_ioc_vdev_detach,          zfs_secpolicy_config,   pool_name },
1672         { zfs_ioc_vdev_setpath,         zfs_secpolicy_config,   pool_name },
1673         { zfs_ioc_objset_stats,         zfs_secpolicy_read,     dataset_name },
1674         { zfs_ioc_dataset_list_next,    zfs_secpolicy_read,     dataset_name },
1675         { zfs_ioc_snapshot_list_next,   zfs_secpolicy_read,     dataset_name },
1676         { zfs_ioc_set_prop,             zfs_secpolicy_write,    dataset_name },
1677         { zfs_ioc_create_minor,         zfs_secpolicy_config,   dataset_name },
1678         { zfs_ioc_remove_minor,         zfs_secpolicy_config,   dataset_name },
1679         { zfs_ioc_create,               zfs_secpolicy_parent,   dataset_name },
1680         { zfs_ioc_destroy,              zfs_secpolicy_parent,   dataset_name },
1681         { zfs_ioc_rollback,             zfs_secpolicy_write,    dataset_name },
1682         { zfs_ioc_rename,               zfs_secpolicy_write,    dataset_name },
1683         { zfs_ioc_recvbackup,           zfs_secpolicy_write,    dataset_name },
1684         { zfs_ioc_sendbackup,           zfs_secpolicy_operator, dataset_name },
1685         { zfs_ioc_inject_fault,         zfs_secpolicy_inject,   no_name },
1686         { zfs_ioc_clear_fault,          zfs_secpolicy_inject,   no_name },
1687         { zfs_ioc_inject_list_next,     zfs_secpolicy_inject,   no_name },
1688         { zfs_ioc_error_log,            zfs_secpolicy_inject,   pool_name },
1689         { zfs_ioc_clear,                zfs_secpolicy_config,   pool_name },
1690         { zfs_ioc_promote,              zfs_secpolicy_write,    dataset_name },
1691         { zfs_ioc_destroy_snaps,        zfs_secpolicy_write,    dataset_name },
1692         { zfs_ioc_snapshot,             zfs_secpolicy_operator, dataset_name },
1693         { zfs_ioc_dsobj_to_dsname,      zfs_secpolicy_config,   pool_name },
1694         { zfs_ioc_obj_to_path,          zfs_secpolicy_config,   no_name },
1695         { zfs_ioc_pool_set_props,       zfs_secpolicy_config,   pool_name },
1696         { zfs_ioc_pool_get_props,       zfs_secpolicy_read,     pool_name },
1697         { zfs_ioc_jail,                 zfs_secpolicy_config,   dataset_name },
1698         { zfs_ioc_unjail,               zfs_secpolicy_config,   dataset_name }
1699 };
1700
1701 static int
1702 zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
1703     struct thread *td)
1704 {
1705         zfs_cmd_t *zc = (void *)addr;
1706         uint_t vec;
1707         int error;
1708
1709         vec = ZFS_IOC(cmd);
1710
1711         if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
1712                 return (EINVAL);
1713
1714         zc->zc_cred = (uintptr_t)td->td_ucred;
1715         zc->zc_dev = (uintptr_t)dev;
1716         error = zfs_ioc_vec[vec].zvec_secpolicy(zc->zc_name, td->td_ucred);
1717
1718         /*
1719          * Ensure that all pool/dataset names are valid before we pass down to
1720          * the lower layers.
1721          */
1722         if (error == 0) {
1723                 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
1724                 switch (zfs_ioc_vec[vec].zvec_namecheck) {
1725                 case pool_name:
1726                         if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
1727                                 error = EINVAL;
1728                         break;
1729
1730                 case dataset_name:
1731                         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
1732                                 error = EINVAL;
1733                         break;
1734
1735                 case no_name:
1736                         break;
1737                 }
1738         }
1739
1740         if (error == 0)
1741                 error = zfs_ioc_vec[vec].zvec_func(zc);
1742
1743         return (error);
1744 }
1745
1746 /*
1747  * OK, so this is a little weird.
1748  *
1749  * /dev/zfs is the control node, i.e. minor 0.
1750  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
1751  *
1752  * /dev/zfs has basically nothing to do except serve up ioctls,
1753  * so most of the standard driver entry points are in zvol.c.
1754  */
1755 static struct cdevsw zfs_cdevsw = {
1756         .d_version =    D_VERSION,
1757         .d_ioctl =      zfsdev_ioctl,
1758         .d_name =       ZFS_DEV_NAME
1759 };
1760
1761 static void
1762 zfsdev_init(void)
1763 {
1764         zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0660,
1765             ZFS_DEV_NAME);
1766 }
1767
1768 static void
1769 zfsdev_fini(void)
1770 {
1771         if (zfsdev != NULL)
1772                 destroy_dev(zfsdev);
1773 }
1774
1775 static struct task zfs_start_task;
1776 static struct root_hold_token *zfs_root_token;
1777
1778 static void
1779 zfs_start(void *context __unused, int pending __unused)
1780 {
1781
1782         zfsdev_init();
1783         spa_init(FREAD | FWRITE);
1784         zfs_init();
1785         zvol_init();
1786         printf("ZFS storage pool version " ZFS_VERSION_STRING "\n");
1787         root_mount_rel(zfs_root_token);
1788 }
1789
1790 static int
1791 zfs_modevent(module_t mod, int type, void *unused __unused)
1792 {
1793         int error;
1794
1795         error = EOPNOTSUPP;
1796         switch (type) {
1797         case MOD_LOAD:
1798                 zfs_root_token = root_mount_hold("ZFS");
1799                 printf("WARNING: ZFS is considered to be an experimental "
1800                     "feature in FreeBSD.\n");
1801                 TASK_INIT(&zfs_start_task, 0, zfs_start, NULL);
1802                 taskqueue_enqueue(taskqueue_thread, &zfs_start_task);
1803                 error = 0;
1804                 break;
1805         case MOD_UNLOAD:
1806                 if (spa_busy() || zfs_busy() || zvol_busy() ||
1807                     zio_injection_enabled) {
1808                         error = EBUSY;
1809                         break;
1810                 }
1811                 zvol_fini();
1812                 zfs_fini();
1813                 spa_fini();
1814                 zfsdev_fini();
1815                 error = 0;
1816                 break;
1817         }
1818         return (error);
1819 }
1820
1821 static moduledata_t zfs_mod = {
1822         "zfsctrl",
1823         zfs_modevent,
1824         0
1825 };
1826 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
1827 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);