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