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