]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_ioctl.c
MFC r209962, r211970-r211972, r212050, r212605, r212611
[FreeBSD/stable/8.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, "zoned", &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         nvlist_t *config, **l2cache, **spares;
1221         uint_t nl2cache = 0, nspares = 0;
1222
1223         error = spa_open(zc->zc_name, &spa, FTAG);
1224         if (error != 0)
1225                 return (error);
1226
1227         error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1228             &config);
1229         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1230             &l2cache, &nl2cache);
1231
1232         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1233             &spares, &nspares);
1234
1235         /*
1236          * A root pool with concatenated devices is not supported.
1237          * Thus, can not add a device to a root pool.
1238          *
1239          * Intent log device can not be added to a rootpool because
1240          * during mountroot, zil is replayed, a seperated log device
1241          * can not be accessed during the mountroot time.
1242          *
1243          * l2cache and spare devices are ok to be added to a rootpool.
1244          */
1245         if (spa->spa_bootfs != 0 && nl2cache == 0 && nspares == 0) {
1246                 spa_close(spa, FTAG);
1247                 return (EDOM);
1248         }
1249
1250         if (error == 0) {
1251                 error = spa_vdev_add(spa, config);
1252                 nvlist_free(config);
1253         }
1254         spa_close(spa, FTAG);
1255         return (error);
1256 }
1257
1258 static int
1259 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1260 {
1261         spa_t *spa;
1262         int error;
1263
1264         error = spa_open(zc->zc_name, &spa, FTAG);
1265         if (error != 0)
1266                 return (error);
1267         error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1268         spa_close(spa, FTAG);
1269         return (error);
1270 }
1271
1272 static int
1273 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1274 {
1275         spa_t *spa;
1276         int error;
1277         vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1278
1279         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1280                 return (error);
1281         switch (zc->zc_cookie) {
1282         case VDEV_STATE_ONLINE:
1283                 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1284                 break;
1285
1286         case VDEV_STATE_OFFLINE:
1287                 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1288                 break;
1289
1290         case VDEV_STATE_FAULTED:
1291                 error = vdev_fault(spa, zc->zc_guid);
1292                 break;
1293
1294         case VDEV_STATE_DEGRADED:
1295                 error = vdev_degrade(spa, zc->zc_guid);
1296                 break;
1297
1298         default:
1299                 error = EINVAL;
1300         }
1301         zc->zc_cookie = newstate;
1302         spa_close(spa, FTAG);
1303         return (error);
1304 }
1305
1306 static int
1307 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1308 {
1309         spa_t *spa;
1310         int replacing = zc->zc_cookie;
1311         nvlist_t *config;
1312         int error;
1313
1314         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1315                 return (error);
1316
1317         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1318             &config)) == 0) {
1319                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1320                 nvlist_free(config);
1321         }
1322
1323         spa_close(spa, FTAG);
1324         return (error);
1325 }
1326
1327 static int
1328 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1329 {
1330         spa_t *spa;
1331         int error;
1332
1333         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1334                 return (error);
1335
1336         error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1337
1338         spa_close(spa, FTAG);
1339         return (error);
1340 }
1341
1342 static int
1343 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1344 {
1345         spa_t *spa;
1346         char *path = zc->zc_value;
1347         uint64_t guid = zc->zc_guid;
1348         int error;
1349
1350         error = spa_open(zc->zc_name, &spa, FTAG);
1351         if (error != 0)
1352                 return (error);
1353
1354         error = spa_vdev_setpath(spa, guid, path);
1355         spa_close(spa, FTAG);
1356         return (error);
1357 }
1358
1359 static int
1360 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1361 {
1362         spa_t *spa;
1363         char *fru = zc->zc_value;
1364         uint64_t guid = zc->zc_guid;
1365         int error;
1366
1367         error = spa_open(zc->zc_name, &spa, FTAG);
1368         if (error != 0)
1369                 return (error);
1370
1371         error = spa_vdev_setfru(spa, guid, fru);
1372         spa_close(spa, FTAG);
1373         return (error);
1374 }
1375
1376 /*
1377  * inputs:
1378  * zc_name              name of filesystem
1379  * zc_nvlist_dst_size   size of buffer for property nvlist
1380  *
1381  * outputs:
1382  * zc_objset_stats      stats
1383  * zc_nvlist_dst        property nvlist
1384  * zc_nvlist_dst_size   size of property nvlist
1385  */
1386 static int
1387 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1388 {
1389         objset_t *os = NULL;
1390         int error;
1391         nvlist_t *nv;
1392
1393         if (error = dmu_objset_open(zc->zc_name,
1394             DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1395                 return (error);
1396
1397         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1398
1399         if (zc->zc_nvlist_dst != 0 &&
1400             (error = dsl_prop_get_all(os, &nv, FALSE)) == 0) {
1401                 dmu_objset_stats(os, nv);
1402                 /*
1403                  * NB: zvol_get_stats() will read the objset contents,
1404                  * which we aren't supposed to do with a
1405                  * DS_MODE_USER hold, because it could be
1406                  * inconsistent.  So this is a bit of a workaround...
1407                  */
1408                 if (!zc->zc_objset_stats.dds_inconsistent) {
1409                         if (dmu_objset_type(os) == DMU_OST_ZVOL)
1410                                 VERIFY(zvol_get_stats(os, nv) == 0);
1411                 }
1412                 error = put_nvlist(zc, nv);
1413                 nvlist_free(nv);
1414         }
1415
1416         dmu_objset_close(os);
1417         if (error == ENOMEM)
1418                 error = 0;
1419         return (error);
1420 }
1421
1422 static int
1423 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1424 {
1425         uint64_t value;
1426         int error;
1427
1428         /*
1429          * zfs_get_zplprop() will either find a value or give us
1430          * the default value (if there is one).
1431          */
1432         if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1433                 return (error);
1434         VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1435         return (0);
1436 }
1437
1438 /*
1439  * inputs:
1440  * zc_name              name of filesystem
1441  * zc_nvlist_dst_size   size of buffer for zpl property nvlist
1442  *
1443  * outputs:
1444  * zc_nvlist_dst        zpl property nvlist
1445  * zc_nvlist_dst_size   size of zpl property nvlist
1446  */
1447 static int
1448 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1449 {
1450         objset_t *os;
1451         int err;
1452
1453         if (err = dmu_objset_open(zc->zc_name,
1454             DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os))
1455                 return (err);
1456
1457         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1458
1459         /*
1460          * NB: nvl_add_zplprop() will read the objset contents,
1461          * which we aren't supposed to do with a DS_MODE_USER
1462          * hold, because it could be inconsistent.
1463          */
1464         if (zc->zc_nvlist_dst != 0 &&
1465             !zc->zc_objset_stats.dds_inconsistent &&
1466             dmu_objset_type(os) == DMU_OST_ZFS) {
1467                 nvlist_t *nv;
1468
1469                 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1470                 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1471                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1472                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1473                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1474                         err = put_nvlist(zc, nv);
1475                 nvlist_free(nv);
1476         } else {
1477                 err = ENOENT;
1478         }
1479         dmu_objset_close(os);
1480         return (err);
1481 }
1482
1483 static boolean_t
1484 dataset_name_hidden(const char *name)
1485 {
1486         /*
1487          * Skip over datasets that are not visible in this zone,
1488          * internal datasets (which have a $ in their name), and
1489          * temporary datasets (which have a % in their name).
1490          */
1491         if (strchr(name, '$') != NULL)
1492                 return (B_TRUE);
1493         if (strchr(name, '%') != NULL)
1494                 return (B_TRUE);
1495         if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
1496                 return (B_TRUE);
1497         return (B_FALSE);
1498 }
1499
1500 /*
1501  * inputs:
1502  * zc_name              name of filesystem
1503  * zc_cookie            zap cursor
1504  * zc_nvlist_dst_size   size of buffer for property nvlist
1505  *
1506  * outputs:
1507  * zc_name              name of next filesystem
1508  * zc_cookie            zap cursor
1509  * zc_objset_stats      stats
1510  * zc_nvlist_dst        property nvlist
1511  * zc_nvlist_dst_size   size of property nvlist
1512  */
1513 static int
1514 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1515 {
1516         objset_t *os;
1517         int error;
1518         char *p;
1519
1520         if (error = dmu_objset_open(zc->zc_name,
1521             DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os)) {
1522                 if (error == ENOENT)
1523                         error = ESRCH;
1524                 return (error);
1525         }
1526
1527         p = strrchr(zc->zc_name, '/');
1528         if (p == NULL || p[1] != '\0')
1529                 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1530         p = zc->zc_name + strlen(zc->zc_name);
1531
1532         /*
1533          * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
1534          * but is not declared void because its called by dmu_objset_find().
1535          */
1536         if (zc->zc_cookie == 0) {
1537                 uint64_t cookie = 0;
1538                 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1539
1540                 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1541                         (void) dmu_objset_prefetch(p, NULL);
1542         }
1543
1544         do {
1545                 error = dmu_dir_list_next(os,
1546                     sizeof (zc->zc_name) - (p - zc->zc_name), p,
1547                     NULL, &zc->zc_cookie);
1548                 if (error == ENOENT)
1549                         error = ESRCH;
1550         } while (error == 0 && dataset_name_hidden(zc->zc_name));
1551         dmu_objset_close(os);
1552
1553         if (error == 0)
1554                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1555
1556         return (error);
1557 }
1558
1559 /*
1560  * inputs:
1561  * zc_name              name of filesystem
1562  * zc_cookie            zap cursor
1563  * zc_nvlist_dst_size   size of buffer for property nvlist
1564  *
1565  * outputs:
1566  * zc_name              name of next snapshot
1567  * zc_objset_stats      stats
1568  * zc_nvlist_dst        property nvlist
1569  * zc_nvlist_dst_size   size of property nvlist
1570  */
1571 static int
1572 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1573 {
1574         objset_t *os;
1575         int error;
1576
1577         error = dmu_objset_open(zc->zc_name,
1578             DMU_OST_ANY, DS_MODE_USER | DS_MODE_READONLY, &os);
1579         if (error)
1580                 return (error == ENOENT ? ESRCH : error);
1581
1582         if (zc->zc_cookie == 0) {
1583                 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1584                     NULL, DS_FIND_SNAPSHOTS);
1585         }
1586         /*
1587          * A dataset name of maximum length cannot have any snapshots,
1588          * so exit immediately.
1589          */
1590         if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1591                 dmu_objset_close(os);
1592                 return (ESRCH);
1593         }
1594
1595         error = dmu_snapshot_list_next(os,
1596             sizeof (zc->zc_name) - strlen(zc->zc_name),
1597             zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL);
1598         dmu_objset_close(os);
1599         if (error == 0)
1600                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1601         else if (error == ENOENT)
1602                 error = ESRCH;
1603
1604         /* if we failed, undo the @ that we tacked on to zc_name */
1605         if (error)
1606                 *strchr(zc->zc_name, '@') = '\0';
1607         return (error);
1608 }
1609
1610 int
1611 zfs_set_prop_nvlist(const char *name, nvlist_t *nvl)
1612 {
1613         nvpair_t *elem;
1614         int error = 0;
1615         uint64_t intval;
1616         char *strval;
1617         nvlist_t *genericnvl;
1618         boolean_t issnap = (strchr(name, '@') != NULL);
1619
1620         /*
1621          * First validate permission to set all of the properties
1622          */
1623         VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1624         elem = NULL;
1625         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1626                 const char *propname = nvpair_name(elem);
1627                 zfs_prop_t prop = zfs_name_to_prop(propname);
1628
1629                 if (prop == ZPROP_INVAL) {
1630                         /*
1631                          * If this is a user-defined property, it must be a
1632                          * string, and there is no further validation to do.
1633                          */
1634                         if (zfs_prop_user(propname) &&
1635                             nvpair_type(elem) == DATA_TYPE_STRING) {
1636                                 if (error = zfs_secpolicy_write_perms(name,
1637                                     ZFS_DELEG_PERM_USERPROP, CRED()))
1638                                         return (error);
1639                                 continue;
1640                         }
1641
1642                         if (!issnap && zfs_prop_userquota(propname) &&
1643                             nvpair_type(elem) == DATA_TYPE_UINT64_ARRAY) {
1644                                 const char *perm;
1645                                 const char *up = zfs_userquota_prop_prefixes
1646                                     [ZFS_PROP_USERQUOTA];
1647                                 if (strncmp(propname, up, strlen(up)) == 0)
1648                                         perm = ZFS_DELEG_PERM_USERQUOTA;
1649                                 else
1650                                         perm = ZFS_DELEG_PERM_GROUPQUOTA;
1651                                 if (error = zfs_secpolicy_write_perms(name,
1652                                     perm, CRED()))
1653                                         return (error);
1654                                 continue;
1655                         }
1656
1657                         return (EINVAL);
1658                 }
1659
1660                 if (issnap)
1661                         return (EINVAL);
1662
1663                 if ((error = zfs_secpolicy_setprop(name, prop, CRED())) != 0)
1664                         return (error);
1665
1666                 /*
1667                  * Check that this value is valid for this pool version
1668                  */
1669                 switch (prop) {
1670                 case ZFS_PROP_COMPRESSION:
1671                         /*
1672                          * If the user specified gzip compression, make sure
1673                          * the SPA supports it. We ignore any errors here since
1674                          * we'll catch them later.
1675                          */
1676                         if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1677                             nvpair_value_uint64(elem, &intval) == 0) {
1678                                 if (intval >= ZIO_COMPRESS_GZIP_1 &&
1679                                     intval <= ZIO_COMPRESS_GZIP_9 &&
1680                                     zfs_earlier_version(name,
1681                                     SPA_VERSION_GZIP_COMPRESSION))
1682                                         return (ENOTSUP);
1683
1684                                 /*
1685                                  * If this is a bootable dataset then
1686                                  * verify that the compression algorithm
1687                                  * is supported for booting. We must return
1688                                  * something other than ENOTSUP since it
1689                                  * implies a downrev pool version.
1690                                  */
1691                                 if (zfs_is_bootfs(name) &&
1692                                     !BOOTFS_COMPRESS_VALID(intval))
1693                                         return (ERANGE);
1694                         }
1695                         break;
1696
1697                 case ZFS_PROP_COPIES:
1698                         if (zfs_earlier_version(name, SPA_VERSION_DITTO_BLOCKS))
1699                                 return (ENOTSUP);
1700                         break;
1701
1702                 case ZFS_PROP_SHARESMB:
1703                         if (zpl_earlier_version(name, ZPL_VERSION_FUID))
1704                                 return (ENOTSUP);
1705                         break;
1706
1707                 case ZFS_PROP_ACLINHERIT:
1708                         if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
1709                             nvpair_value_uint64(elem, &intval) == 0)
1710                                 if (intval == ZFS_ACL_PASSTHROUGH_X &&
1711                                     zfs_earlier_version(name,
1712                                     SPA_VERSION_PASSTHROUGH_X))
1713                                         return (ENOTSUP);
1714                 }
1715         }
1716
1717         elem = NULL;
1718         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1719                 const char *propname = nvpair_name(elem);
1720                 zfs_prop_t prop = zfs_name_to_prop(propname);
1721
1722                 if (prop == ZPROP_INVAL) {
1723                         if (zfs_prop_userquota(propname)) {
1724                                 uint64_t *valary;
1725                                 unsigned int vallen;
1726                                 const char *domain;
1727                                 zfs_userquota_prop_t type;
1728                                 uint64_t rid;
1729                                 uint64_t quota;
1730                                 zfsvfs_t *zfsvfs;
1731
1732                                 VERIFY(nvpair_value_uint64_array(elem,
1733                                     &valary, &vallen) == 0);
1734                                 VERIFY(vallen == 3);
1735                                 type = valary[0];
1736                                 rid = valary[1];
1737                                 quota = valary[2];
1738                                 domain = propname +
1739                                     strlen(zfs_userquota_prop_prefixes[type]);
1740
1741                                 error = zfsvfs_hold(name, B_FALSE, FTAG,
1742                                     &zfsvfs);
1743                                 if (error == 0) {
1744                                         error = zfs_set_userquota(zfsvfs,
1745                                             type, domain, rid, quota);
1746                                         zfsvfs_rele(zfsvfs, FTAG);
1747                                 }
1748                                 if (error == 0)
1749                                         continue;
1750                                 else
1751                                         goto out;
1752                         } else if (zfs_prop_user(propname)) {
1753                                 VERIFY(nvpair_value_string(elem, &strval) == 0);
1754                                 error = dsl_prop_set(name, propname, 1,
1755                                     strlen(strval) + 1, strval);
1756                                 if (error == 0)
1757                                         continue;
1758                                 else
1759                                         goto out;
1760                         }
1761                 }
1762
1763                 switch (prop) {
1764                 case ZFS_PROP_QUOTA:
1765                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1766                             (error = dsl_dir_set_quota(name, intval)) != 0)
1767                                 goto out;
1768                         break;
1769
1770                 case ZFS_PROP_REFQUOTA:
1771                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1772                             (error = dsl_dataset_set_quota(name, intval)) != 0)
1773                                 goto out;
1774                         break;
1775
1776                 case ZFS_PROP_RESERVATION:
1777                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1778                             (error = dsl_dir_set_reservation(name,
1779                             intval)) != 0)
1780                                 goto out;
1781                         break;
1782
1783                 case ZFS_PROP_REFRESERVATION:
1784                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1785                             (error = dsl_dataset_set_reservation(name,
1786                             intval)) != 0)
1787                                 goto out;
1788                         break;
1789
1790                 case ZFS_PROP_VOLSIZE:
1791                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1792                             (error = zvol_set_volsize(name,
1793                             ddi_driver_major(zfs_dip), intval)) != 0)
1794                                 goto out;
1795                         break;
1796
1797                 case ZFS_PROP_VOLBLOCKSIZE:
1798                         if ((error = nvpair_value_uint64(elem, &intval)) != 0 ||
1799                             (error = zvol_set_volblocksize(name, intval)) != 0)
1800                                 goto out;
1801                         break;
1802
1803                 case ZFS_PROP_VERSION:
1804                 {
1805                         zfsvfs_t *zfsvfs;
1806
1807                         if ((error = nvpair_value_uint64(elem, &intval)) != 0)
1808                                 goto out;
1809                         if ((error = zfsvfs_hold(name, B_FALSE, FTAG,
1810                             &zfsvfs)) != 0)
1811                                 goto out;
1812                         error = zfs_set_version(zfsvfs, intval);
1813                         zfsvfs_rele(zfsvfs, FTAG);
1814
1815                         if (error == 0 && intval >= ZPL_VERSION_USERSPACE) {
1816                                 zfs_cmd_t zc = { 0 };
1817                                 (void) strcpy(zc.zc_name, name);
1818                                 (void) zfs_ioc_userspace_upgrade(&zc);
1819                         }
1820                         if (error)
1821                                 goto out;
1822                         break;
1823                 }
1824
1825                 default:
1826                         if (nvpair_type(elem) == DATA_TYPE_STRING) {
1827                                 if (zfs_prop_get_type(prop) !=
1828                                     PROP_TYPE_STRING) {
1829                                         error = EINVAL;
1830                                         goto out;
1831                                 }
1832                         } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
1833                                 const char *unused;
1834
1835                                 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
1836
1837                                 switch (zfs_prop_get_type(prop)) {
1838                                 case PROP_TYPE_NUMBER:
1839                                         break;
1840                                 case PROP_TYPE_STRING:
1841                                         error = EINVAL;
1842                                         goto out;
1843                                 case PROP_TYPE_INDEX:
1844                                         if (zfs_prop_index_to_string(prop,
1845                                             intval, &unused) != 0) {
1846                                                 error = EINVAL;
1847                                                 goto out;
1848                                         }
1849                                         break;
1850                                 default:
1851                                         cmn_err(CE_PANIC,
1852                                             "unknown property type");
1853                                         break;
1854                                 }
1855                         } else {
1856                                 error = EINVAL;
1857                                 goto out;
1858                         }
1859                         if ((error = nvlist_add_nvpair(genericnvl, elem)) != 0)
1860                                 goto out;
1861                 }
1862         }
1863
1864         if (nvlist_next_nvpair(genericnvl, NULL) != NULL) {
1865                 error = dsl_props_set(name, genericnvl);
1866         }
1867 out:
1868         nvlist_free(genericnvl);
1869         return (error);
1870 }
1871
1872 /*
1873  * Check that all the properties are valid user properties.
1874  */
1875 static int
1876 zfs_check_userprops(char *fsname, nvlist_t *nvl)
1877 {
1878         nvpair_t *elem = NULL;
1879         int error = 0;
1880
1881         while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1882                 const char *propname = nvpair_name(elem);
1883                 char *valstr;
1884
1885                 if (!zfs_prop_user(propname) ||
1886                     nvpair_type(elem) != DATA_TYPE_STRING)
1887                         return (EINVAL);
1888
1889                 if (error = zfs_secpolicy_write_perms(fsname,
1890                     ZFS_DELEG_PERM_USERPROP, CRED()))
1891                         return (error);
1892
1893                 if (strlen(propname) >= ZAP_MAXNAMELEN)
1894                         return (ENAMETOOLONG);
1895
1896                 VERIFY(nvpair_value_string(elem, &valstr) == 0);
1897                 if (strlen(valstr) >= ZAP_MAXVALUELEN)
1898                         return (E2BIG);
1899         }
1900         return (0);
1901 }
1902
1903 /*
1904  * inputs:
1905  * zc_name              name of filesystem
1906  * zc_value             name of property to set
1907  * zc_nvlist_src{_size} nvlist of properties to apply
1908  * zc_cookie            clear existing local props?
1909  *
1910  * outputs:             none
1911  */
1912 static int
1913 zfs_ioc_set_prop(zfs_cmd_t *zc)
1914 {
1915         nvlist_t *nvl;
1916         int error;
1917
1918         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1919             &nvl)) != 0)
1920                 return (error);
1921
1922         if (zc->zc_cookie) {
1923                 nvlist_t *origprops;
1924                 objset_t *os;
1925
1926                 if (dmu_objset_open(zc->zc_name, DMU_OST_ANY,
1927                     DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
1928                         if (dsl_prop_get_all(os, &origprops, TRUE) == 0) {
1929                                 clear_props(zc->zc_name, origprops, nvl);
1930                                 nvlist_free(origprops);
1931                         }
1932                         dmu_objset_close(os);
1933                 }
1934
1935         }
1936
1937         error = zfs_set_prop_nvlist(zc->zc_name, nvl);
1938
1939         nvlist_free(nvl);
1940         return (error);
1941 }
1942
1943 /*
1944  * inputs:
1945  * zc_name              name of filesystem
1946  * zc_value             name of property to inherit
1947  *
1948  * outputs:             none
1949  */
1950 static int
1951 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
1952 {
1953         /* the property name has been validated by zfs_secpolicy_inherit() */
1954         return (dsl_prop_set(zc->zc_name, zc->zc_value, 0, 0, NULL));
1955 }
1956
1957 static int
1958 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
1959 {
1960         nvlist_t *props;
1961         spa_t *spa;
1962         int error;
1963         nvpair_t *elem;
1964
1965         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1966             &props)))
1967                 return (error);
1968
1969         /*
1970          * If the only property is the configfile, then just do a spa_lookup()
1971          * to handle the faulted case.
1972          */
1973         elem = nvlist_next_nvpair(props, NULL);
1974         if (elem != NULL && strcmp(nvpair_name(elem),
1975             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
1976             nvlist_next_nvpair(props, elem) == NULL) {
1977                 mutex_enter(&spa_namespace_lock);
1978                 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
1979                         spa_configfile_set(spa, props, B_FALSE);
1980                         spa_config_sync(spa, B_FALSE, B_TRUE);
1981                 }
1982                 mutex_exit(&spa_namespace_lock);
1983                 if (spa != NULL)
1984                         return (0);
1985         }
1986
1987         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
1988                 nvlist_free(props);
1989                 return (error);
1990         }
1991
1992         error = spa_prop_set(spa, props);
1993
1994         nvlist_free(props);
1995         spa_close(spa, FTAG);
1996
1997         return (error);
1998 }
1999
2000 static int
2001 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2002 {
2003         spa_t *spa;
2004         int error;
2005         nvlist_t *nvp = NULL;
2006
2007         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2008                 /*
2009                  * If the pool is faulted, there may be properties we can still
2010                  * get (such as altroot and cachefile), so attempt to get them
2011                  * anyway.
2012                  */
2013                 mutex_enter(&spa_namespace_lock);
2014                 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2015                         error = spa_prop_get(spa, &nvp);
2016                 mutex_exit(&spa_namespace_lock);
2017         } else {
2018                 error = spa_prop_get(spa, &nvp);
2019                 spa_close(spa, FTAG);
2020         }
2021
2022         if (error == 0 && zc->zc_nvlist_dst != 0)
2023                 error = put_nvlist(zc, nvp);
2024         else
2025                 error = EFAULT;
2026
2027         nvlist_free(nvp);
2028         return (error);
2029 }
2030
2031 static int
2032 zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc)
2033 {
2034 #ifdef sun
2035         nvlist_t *nvp;
2036         int error;
2037         uint32_t uid;
2038         uint32_t gid;
2039         uint32_t *groups;
2040         uint_t group_cnt;
2041         cred_t  *usercred;
2042
2043         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2044             &nvp)) != 0) {
2045                 return (error);
2046         }
2047
2048         if ((error = nvlist_lookup_uint32(nvp,
2049             ZFS_DELEG_PERM_UID, &uid)) != 0) {
2050                 nvlist_free(nvp);
2051                 return (EPERM);
2052         }
2053
2054         if ((error = nvlist_lookup_uint32(nvp,
2055             ZFS_DELEG_PERM_GID, &gid)) != 0) {
2056                 nvlist_free(nvp);
2057                 return (EPERM);
2058         }
2059
2060         if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS,
2061             &groups, &group_cnt)) != 0) {
2062                 nvlist_free(nvp);
2063                 return (EPERM);
2064         }
2065         usercred = cralloc();
2066         if ((crsetugid(usercred, uid, gid) != 0) ||
2067             (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) {
2068                 nvlist_free(nvp);
2069                 crfree(usercred);
2070                 return (EPERM);
2071         }
2072         nvlist_free(nvp);
2073         error = dsl_deleg_access(zc->zc_name,
2074             zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred);
2075         crfree(usercred);
2076         return (error);
2077 #else   /* sun */
2078         return (EPERM);
2079 #endif  /* sun */
2080 }
2081
2082 /*
2083  * inputs:
2084  * zc_name              name of filesystem
2085  * zc_nvlist_src{_size} nvlist of delegated permissions
2086  * zc_perm_action       allow/unallow flag
2087  *
2088  * outputs:             none
2089  */
2090 static int
2091 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2092 {
2093         int error;
2094         nvlist_t *fsaclnv = NULL;
2095
2096         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2097             &fsaclnv)) != 0)
2098                 return (error);
2099
2100         /*
2101          * Verify nvlist is constructed correctly
2102          */
2103         if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2104                 nvlist_free(fsaclnv);
2105                 return (EINVAL);
2106         }
2107
2108         /*
2109          * If we don't have PRIV_SYS_MOUNT, then validate
2110          * that user is allowed to hand out each permission in
2111          * the nvlist(s)
2112          */
2113
2114         error = secpolicy_zfs(CRED());
2115         if (error) {
2116                 if (zc->zc_perm_action == B_FALSE) {
2117                         error = dsl_deleg_can_allow(zc->zc_name,
2118                             fsaclnv, CRED());
2119                 } else {
2120                         error = dsl_deleg_can_unallow(zc->zc_name,
2121                             fsaclnv, CRED());
2122                 }
2123         }
2124
2125         if (error == 0)
2126                 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2127
2128         nvlist_free(fsaclnv);
2129         return (error);
2130 }
2131
2132 /*
2133  * inputs:
2134  * zc_name              name of filesystem
2135  *
2136  * outputs:
2137  * zc_nvlist_src{_size} nvlist of delegated permissions
2138  */
2139 static int
2140 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2141 {
2142         nvlist_t *nvp;
2143         int error;
2144
2145         if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2146                 error = put_nvlist(zc, nvp);
2147                 nvlist_free(nvp);
2148         }
2149
2150         return (error);
2151 }
2152
2153 /*
2154  * inputs:
2155  * zc_name              name of volume
2156  *
2157  * outputs:             none
2158  */
2159 static int
2160 zfs_ioc_create_minor(zfs_cmd_t *zc)
2161 {
2162         return (zvol_create_minor(zc->zc_name, ddi_driver_major(zfs_dip)));
2163 }
2164
2165 /*
2166  * inputs:
2167  * zc_name              name of volume
2168  *
2169  * outputs:             none
2170  */
2171 static int
2172 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2173 {
2174         return (zvol_remove_minor(zc->zc_name));
2175 }
2176
2177 /*
2178  * Search the vfs list for a specified resource.  Returns a pointer to it
2179  * or NULL if no suitable entry is found. The caller of this routine
2180  * is responsible for releasing the returned vfs pointer.
2181  */
2182 static vfs_t *
2183 zfs_get_vfs(const char *resource)
2184 {
2185         vfs_t *vfsp;
2186
2187         mtx_lock(&mountlist_mtx);
2188         TAILQ_FOREACH(vfsp, &mountlist, mnt_list) {
2189                 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2190                         VFS_HOLD(vfsp);
2191                         break;
2192                 }
2193         }
2194         mtx_unlock(&mountlist_mtx);
2195         return (vfsp);
2196 }
2197
2198 /* ARGSUSED */
2199 static void
2200 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2201 {
2202         zfs_creat_t *zct = arg;
2203
2204         zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2205 }
2206
2207 #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
2208
2209 /*
2210  * inputs:
2211  * createprops          list of properties requested by creator
2212  * default_zplver       zpl version to use if unspecified in createprops
2213  * fuids_ok             fuids allowed in this version of the spa?
2214  * os                   parent objset pointer (NULL if root fs)
2215  *
2216  * outputs:
2217  * zplprops     values for the zplprops we attach to the master node object
2218  * is_ci        true if requested file system will be purely case-insensitive
2219  *
2220  * Determine the settings for utf8only, normalization and
2221  * casesensitivity.  Specific values may have been requested by the
2222  * creator and/or we can inherit values from the parent dataset.  If
2223  * the file system is of too early a vintage, a creator can not
2224  * request settings for these properties, even if the requested
2225  * setting is the default value.  We don't actually want to create dsl
2226  * properties for these, so remove them from the source nvlist after
2227  * processing.
2228  */
2229 static int
2230 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2231     boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops,
2232     boolean_t *is_ci)
2233 {
2234         uint64_t sense = ZFS_PROP_UNDEFINED;
2235         uint64_t norm = ZFS_PROP_UNDEFINED;
2236         uint64_t u8 = ZFS_PROP_UNDEFINED;
2237
2238         ASSERT(zplprops != NULL);
2239
2240         /*
2241          * Pull out creator prop choices, if any.
2242          */
2243         if (createprops) {
2244                 (void) nvlist_lookup_uint64(createprops,
2245                     zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2246                 (void) nvlist_lookup_uint64(createprops,
2247                     zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2248                 (void) nvlist_remove_all(createprops,
2249                     zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2250                 (void) nvlist_lookup_uint64(createprops,
2251                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2252                 (void) nvlist_remove_all(createprops,
2253                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2254                 (void) nvlist_lookup_uint64(createprops,
2255                     zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2256                 (void) nvlist_remove_all(createprops,
2257                     zfs_prop_to_name(ZFS_PROP_CASE));
2258         }
2259
2260         /*
2261          * If the zpl version requested is whacky or the file system
2262          * or pool is version is too "young" to support normalization
2263          * and the creator tried to set a value for one of the props,
2264          * error out.
2265          */
2266         if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2267             (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2268             (zplver < ZPL_VERSION_NORMALIZATION &&
2269             (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2270             sense != ZFS_PROP_UNDEFINED)))
2271                 return (ENOTSUP);
2272
2273         /*
2274          * Put the version in the zplprops
2275          */
2276         VERIFY(nvlist_add_uint64(zplprops,
2277             zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2278
2279         if (norm == ZFS_PROP_UNDEFINED)
2280                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2281         VERIFY(nvlist_add_uint64(zplprops,
2282             zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2283
2284         /*
2285          * If we're normalizing, names must always be valid UTF-8 strings.
2286          */
2287         if (norm)
2288                 u8 = 1;
2289         if (u8 == ZFS_PROP_UNDEFINED)
2290                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2291         VERIFY(nvlist_add_uint64(zplprops,
2292             zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2293
2294         if (sense == ZFS_PROP_UNDEFINED)
2295                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2296         VERIFY(nvlist_add_uint64(zplprops,
2297             zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2298
2299         if (is_ci)
2300                 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2301
2302         return (0);
2303 }
2304
2305 static int
2306 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2307     nvlist_t *zplprops, boolean_t *is_ci)
2308 {
2309         boolean_t fuids_ok = B_TRUE;
2310         uint64_t zplver = ZPL_VERSION;
2311         objset_t *os = NULL;
2312         char parentname[MAXNAMELEN];
2313         char *cp;
2314         int error;
2315
2316         (void) strlcpy(parentname, dataset, sizeof (parentname));
2317         cp = strrchr(parentname, '/');
2318         ASSERT(cp != NULL);
2319         cp[0] = '\0';
2320
2321         if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE))
2322                 zplver = ZPL_VERSION_USERSPACE - 1;
2323         if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) {
2324                 zplver = ZPL_VERSION_FUID - 1;
2325                 fuids_ok = B_FALSE;
2326         }
2327
2328         /*
2329          * Open parent object set so we can inherit zplprop values.
2330          */
2331         if ((error = dmu_objset_open(parentname, DMU_OST_ANY,
2332             DS_MODE_USER | DS_MODE_READONLY, &os)) != 0)
2333                 return (error);
2334
2335         error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops,
2336             zplprops, is_ci);
2337         dmu_objset_close(os);
2338         return (error);
2339 }
2340
2341 static int
2342 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2343     nvlist_t *zplprops, boolean_t *is_ci)
2344 {
2345         boolean_t fuids_ok = B_TRUE;
2346         uint64_t zplver = ZPL_VERSION;
2347         int error;
2348
2349         if (spa_vers < SPA_VERSION_FUID) {
2350                 zplver = ZPL_VERSION_FUID - 1;
2351                 fuids_ok = B_FALSE;
2352         }
2353
2354         error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops,
2355             zplprops, is_ci);
2356         return (error);
2357 }
2358
2359 /*
2360  * inputs:
2361  * zc_objset_type       type of objset to create (fs vs zvol)
2362  * zc_name              name of new objset
2363  * zc_value             name of snapshot to clone from (may be empty)
2364  * zc_nvlist_src{_size} nvlist of properties to apply
2365  *
2366  * outputs: none
2367  */
2368 static int
2369 zfs_ioc_create(zfs_cmd_t *zc)
2370 {
2371         objset_t *clone;
2372         int error = 0;
2373         zfs_creat_t zct;
2374         nvlist_t *nvprops = NULL;
2375         void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2376         dmu_objset_type_t type = zc->zc_objset_type;
2377
2378         switch (type) {
2379
2380         case DMU_OST_ZFS:
2381                 cbfunc = zfs_create_cb;
2382                 break;
2383
2384         case DMU_OST_ZVOL:
2385                 cbfunc = zvol_create_cb;
2386                 break;
2387
2388         default:
2389                 cbfunc = NULL;
2390                 break;
2391         }
2392         if (strchr(zc->zc_name, '@') ||
2393             strchr(zc->zc_name, '%'))
2394                 return (EINVAL);
2395
2396         if (zc->zc_nvlist_src != 0 &&
2397             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2398             &nvprops)) != 0)
2399                 return (error);
2400
2401         zct.zct_zplprops = NULL;
2402         zct.zct_props = nvprops;
2403
2404         if (zc->zc_value[0] != '\0') {
2405                 /*
2406                  * We're creating a clone of an existing snapshot.
2407                  */
2408                 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2409                 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2410                         nvlist_free(nvprops);
2411                         return (EINVAL);
2412                 }
2413
2414                 error = dmu_objset_open(zc->zc_value, type,
2415                     DS_MODE_USER | DS_MODE_READONLY, &clone);
2416                 if (error) {
2417                         nvlist_free(nvprops);
2418                         return (error);
2419                 }
2420
2421                 error = dmu_objset_create(zc->zc_name, type, clone, 0,
2422                     NULL, NULL);
2423                 if (error) {
2424                         dmu_objset_close(clone);
2425                         nvlist_free(nvprops);
2426                         return (error);
2427                 }
2428                 dmu_objset_close(clone);
2429         } else {
2430                 boolean_t is_insensitive = B_FALSE;
2431
2432                 if (cbfunc == NULL) {
2433                         nvlist_free(nvprops);
2434                         return (EINVAL);
2435                 }
2436
2437                 if (type == DMU_OST_ZVOL) {
2438                         uint64_t volsize, volblocksize;
2439
2440                         if (nvprops == NULL ||
2441                             nvlist_lookup_uint64(nvprops,
2442                             zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2443                             &volsize) != 0) {
2444                                 nvlist_free(nvprops);
2445                                 return (EINVAL);
2446                         }
2447
2448                         if ((error = nvlist_lookup_uint64(nvprops,
2449                             zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2450                             &volblocksize)) != 0 && error != ENOENT) {
2451                                 nvlist_free(nvprops);
2452                                 return (EINVAL);
2453                         }
2454
2455                         if (error != 0)
2456                                 volblocksize = zfs_prop_default_numeric(
2457                                     ZFS_PROP_VOLBLOCKSIZE);
2458
2459                         if ((error = zvol_check_volblocksize(
2460                             volblocksize)) != 0 ||
2461                             (error = zvol_check_volsize(volsize,
2462                             volblocksize)) != 0) {
2463                                 nvlist_free(nvprops);
2464                                 return (error);
2465                         }
2466                 } else if (type == DMU_OST_ZFS) {
2467                         int error;
2468
2469                         /*
2470                          * We have to have normalization and
2471                          * case-folding flags correct when we do the
2472                          * file system creation, so go figure them out
2473                          * now.
2474                          */
2475                         VERIFY(nvlist_alloc(&zct.zct_zplprops,
2476                             NV_UNIQUE_NAME, KM_SLEEP) == 0);
2477                         error = zfs_fill_zplprops(zc->zc_name, nvprops,
2478                             zct.zct_zplprops, &is_insensitive);
2479                         if (error != 0) {
2480                                 nvlist_free(nvprops);
2481                                 nvlist_free(zct.zct_zplprops);
2482                                 return (error);
2483                         }
2484                 }
2485                 error = dmu_objset_create(zc->zc_name, type, NULL,
2486                     is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2487                 nvlist_free(zct.zct_zplprops);
2488         }
2489
2490         /*
2491          * It would be nice to do this atomically.
2492          */
2493         if (error == 0) {
2494                 if ((error = zfs_set_prop_nvlist(zc->zc_name, nvprops)) != 0)
2495                         (void) dmu_objset_destroy(zc->zc_name);
2496         }
2497         nvlist_free(nvprops);
2498         return (error);
2499 }
2500
2501 /*
2502  * inputs:
2503  * zc_name      name of filesystem
2504  * zc_value     short name of snapshot
2505  * zc_cookie    recursive flag
2506  * zc_nvlist_src[_size] property list
2507  *
2508  * outputs:     none
2509  */
2510 static int
2511 zfs_ioc_snapshot(zfs_cmd_t *zc)
2512 {
2513         nvlist_t *nvprops = NULL;
2514         int error;
2515         boolean_t recursive = zc->zc_cookie;
2516
2517         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2518                 return (EINVAL);
2519
2520         if (zc->zc_nvlist_src != 0 &&
2521             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2522             &nvprops)) != 0)
2523                 return (error);
2524
2525         error = zfs_check_userprops(zc->zc_name, nvprops);
2526         if (error)
2527                 goto out;
2528
2529         if (nvprops != NULL && nvlist_next_nvpair(nvprops, NULL) != NULL &&
2530             zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
2531                 error = ENOTSUP;
2532                 goto out;
2533         }
2534
2535         error = dmu_objset_snapshot(zc->zc_name, zc->zc_value,
2536             nvprops, recursive);
2537
2538 out:
2539         nvlist_free(nvprops);
2540         return (error);
2541 }
2542
2543 int
2544 zfs_unmount_snap(char *name, void *arg)
2545 {
2546         vfs_t *vfsp = NULL;
2547
2548         if (arg) {
2549                 char *snapname = arg;
2550                 int len = strlen(name) + strlen(snapname) + 2;
2551                 char *buf = kmem_alloc(len, KM_SLEEP);
2552
2553                 (void) strcpy(buf, name);
2554                 (void) strcat(buf, "@");
2555                 (void) strcat(buf, snapname);
2556                 vfsp = zfs_get_vfs(buf);
2557                 kmem_free(buf, len);
2558         } else if (strchr(name, '@')) {
2559                 vfsp = zfs_get_vfs(name);
2560         }
2561
2562         if (vfsp) {
2563                 /*
2564                  * Always force the unmount for snapshots.
2565                  */
2566                 int flag = MS_FORCE;
2567                 int err;
2568
2569                 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
2570                         VFS_RELE(vfsp);
2571                         return (err);
2572                 }
2573                 VFS_RELE(vfsp);
2574                 mtx_lock(&Giant);       /* dounmount() */
2575                 dounmount(vfsp, flag, curthread);
2576                 mtx_unlock(&Giant);     /* dounmount() */
2577         }
2578         return (0);
2579 }
2580
2581 /*
2582  * inputs:
2583  * zc_name      name of filesystem
2584  * zc_value     short name of snapshot
2585  *
2586  * outputs:     none
2587  */
2588 static int
2589 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
2590 {
2591         int err;
2592
2593         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
2594                 return (EINVAL);
2595         err = dmu_objset_find(zc->zc_name,
2596             zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
2597         if (err)
2598                 return (err);
2599         return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value));
2600 }
2601
2602 /*
2603  * inputs:
2604  * zc_name              name of dataset to destroy
2605  * zc_objset_type       type of objset
2606  *
2607  * outputs:             none
2608  */
2609 static int
2610 zfs_ioc_destroy(zfs_cmd_t *zc)
2611 {
2612         if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
2613                 int err = zfs_unmount_snap(zc->zc_name, NULL);
2614                 if (err)
2615                         return (err);
2616         }
2617
2618         return (dmu_objset_destroy(zc->zc_name));
2619 }
2620
2621 /*
2622  * inputs:
2623  * zc_name      name of dataset to rollback (to most recent snapshot)
2624  *
2625  * outputs:     none
2626  */
2627 static int
2628 zfs_ioc_rollback(zfs_cmd_t *zc)
2629 {
2630         objset_t *os;
2631         int error;
2632         zfsvfs_t *zfsvfs = NULL;
2633
2634         /*
2635          * Get the zfsvfs for the receiving objset. There
2636          * won't be one if we're operating on a zvol, if the
2637          * objset doesn't exist yet, or is not mounted.
2638          */
2639         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY, DS_MODE_USER, &os);
2640         if (error)
2641                 return (error);
2642
2643         if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
2644                 int mode;
2645
2646                 error = zfs_suspend_fs(zfsvfs, NULL, &mode);
2647                 if (error == 0) {
2648                         int resume_err;
2649
2650                         error = dmu_objset_rollback(os);
2651                         resume_err = zfs_resume_fs(zfsvfs, zc->zc_name, mode);
2652                         error = error ? error : resume_err;
2653                 } else {
2654                         dmu_objset_close(os);
2655                 }
2656                 VFS_RELE(zfsvfs->z_vfs);
2657         } else {
2658                 error = dmu_objset_rollback(os);
2659         }
2660         /* Note, the dmu_objset_rollback() releases the objset for us. */
2661
2662         return (error);
2663 }
2664
2665 /*
2666  * inputs:
2667  * zc_name      old name of dataset
2668  * zc_value     new name of dataset
2669  * zc_cookie    recursive flag (only valid for snapshots)
2670  *
2671  * outputs:     none
2672  */
2673 static int
2674 zfs_ioc_rename(zfs_cmd_t *zc)
2675 {
2676         boolean_t recursive = zc->zc_cookie & 1;
2677
2678         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2679         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2680             strchr(zc->zc_value, '%'))
2681                 return (EINVAL);
2682
2683         /*
2684          * Unmount snapshot unless we're doing a recursive rename,
2685          * in which case the dataset code figures out which snapshots
2686          * to unmount.
2687          */
2688         if (!recursive && strchr(zc->zc_name, '@') != NULL &&
2689             zc->zc_objset_type == DMU_OST_ZFS) {
2690                 int err = zfs_unmount_snap(zc->zc_name, NULL);
2691                 if (err)
2692                         return (err);
2693         }
2694         return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
2695 }
2696
2697 static void
2698 clear_props(char *dataset, nvlist_t *props, nvlist_t *newprops)
2699 {
2700         zfs_cmd_t *zc;
2701         nvpair_t *prop;
2702
2703         if (props == NULL)
2704                 return;
2705         zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
2706         (void) strcpy(zc->zc_name, dataset);
2707         for (prop = nvlist_next_nvpair(props, NULL); prop;
2708             prop = nvlist_next_nvpair(props, prop)) {
2709                 if (newprops != NULL &&
2710                     nvlist_exists(newprops, nvpair_name(prop)))
2711                         continue;
2712                 (void) strcpy(zc->zc_value, nvpair_name(prop));
2713                 if (zfs_secpolicy_inherit(zc, CRED()) == 0)
2714                         (void) zfs_ioc_inherit_prop(zc);
2715         }
2716         kmem_free(zc, sizeof (zfs_cmd_t));
2717 }
2718
2719 /*
2720  * inputs:
2721  * zc_name              name of containing filesystem
2722  * zc_nvlist_src{_size} nvlist of properties to apply
2723  * zc_value             name of snapshot to create
2724  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
2725  * zc_cookie            file descriptor to recv from
2726  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
2727  * zc_guid              force flag
2728  *
2729  * outputs:
2730  * zc_cookie            number of bytes read
2731  */
2732 static int
2733 zfs_ioc_recv(zfs_cmd_t *zc)
2734 {
2735         file_t *fp;
2736         objset_t *os;
2737         dmu_recv_cookie_t drc;
2738         zfsvfs_t *zfsvfs = NULL;
2739         boolean_t force = (boolean_t)zc->zc_guid;
2740         int error, fd;
2741         offset_t off;
2742         nvlist_t *props = NULL;
2743         nvlist_t *origprops = NULL;
2744         objset_t *origin = NULL;
2745         char *tosnap;
2746         char tofs[ZFS_MAXNAMELEN];
2747
2748         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
2749             strchr(zc->zc_value, '@') == NULL ||
2750             strchr(zc->zc_value, '%'))
2751                 return (EINVAL);
2752
2753         (void) strcpy(tofs, zc->zc_value);
2754         tosnap = strchr(tofs, '@');
2755         *tosnap = '\0';
2756         tosnap++;
2757
2758         if (zc->zc_nvlist_src != 0 &&
2759             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2760             &props)) != 0)
2761                 return (error);
2762
2763         fd = zc->zc_cookie;
2764         fp = getf(fd, 0);
2765         if (fp == NULL) {
2766                 nvlist_free(props);
2767                 return (EBADF);
2768         }
2769
2770         if (getzfsvfs(tofs, &zfsvfs) == 0) {
2771                 if (!mutex_tryenter(&zfsvfs->z_online_recv_lock)) {
2772                         VFS_RELE(zfsvfs->z_vfs);
2773                         zfsvfs = NULL;
2774                         error = EBUSY;
2775                         goto out;
2776                 }
2777                 /*
2778                  * If new properties are supplied, they are to completely
2779                  * replace the existing ones, so stash away the existing ones.
2780                  */
2781                 if (props)
2782                         (void) dsl_prop_get_all(zfsvfs->z_os, &origprops, TRUE);
2783         } else if (props && dmu_objset_open(tofs, DMU_OST_ANY,
2784             DS_MODE_USER | DS_MODE_READONLY, &os) == 0) {
2785                 /*
2786                  * Get the props even if there was no zfsvfs (zvol or
2787                  * unmounted zpl).
2788                  */
2789                 (void) dsl_prop_get_all(os, &origprops, TRUE);
2790
2791                 dmu_objset_close(os);
2792         }
2793
2794         if (zc->zc_string[0]) {
2795                 error = dmu_objset_open(zc->zc_string, DMU_OST_ANY,
2796                     DS_MODE_USER | DS_MODE_READONLY, &origin);
2797                 if (error)
2798                         goto out;
2799         }
2800
2801         error = dmu_recv_begin(tofs, tosnap, &zc->zc_begin_record,
2802             force, origin, zfsvfs != NULL, &drc);
2803         if (origin)
2804                 dmu_objset_close(origin);
2805         if (error)
2806                 goto out;
2807
2808         /*
2809          * Reset properties.  We do this before we receive the stream
2810          * so that the properties are applied to the new data.
2811          */
2812         if (props) {
2813                 clear_props(tofs, origprops, props);
2814                 /*
2815                  * XXX - Note, this is all-or-nothing; should be best-effort.
2816                  */
2817                 (void) zfs_set_prop_nvlist(tofs, props);
2818         }
2819
2820         off = fp->f_offset;
2821         error = dmu_recv_stream(&drc, fp, &off);
2822
2823         if (error == 0 && zfsvfs) {
2824                 char *osname;
2825                 int mode;
2826
2827                 /* online recv */
2828                 osname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2829                 error = zfs_suspend_fs(zfsvfs, osname, &mode);
2830                 if (error == 0) {
2831                         int resume_err;
2832
2833                         error = dmu_recv_end(&drc);
2834                         resume_err = zfs_resume_fs(zfsvfs, osname, mode);
2835                         error = error ? error : resume_err;
2836                 } else {
2837                         dmu_recv_abort_cleanup(&drc);
2838                 }
2839                 kmem_free(osname, MAXNAMELEN);
2840         } else if (error == 0) {
2841                 error = dmu_recv_end(&drc);
2842         }
2843
2844         zc->zc_cookie = off - fp->f_offset;
2845         if (off >= 0 && off <= MAXOFFSET_T)
2846                 fp->f_offset = off;
2847
2848         /*
2849          * On error, restore the original props.
2850          */
2851         if (error && props) {
2852                 clear_props(tofs, props, NULL);
2853                 (void) zfs_set_prop_nvlist(tofs, origprops);
2854         }
2855 out:
2856         if (zfsvfs) {
2857                 mutex_exit(&zfsvfs->z_online_recv_lock);
2858                 VFS_RELE(zfsvfs->z_vfs);
2859         }
2860         nvlist_free(props);
2861         nvlist_free(origprops);
2862         releasef(fp);
2863         return (error);
2864 }
2865
2866 /*
2867  * inputs:
2868  * zc_name      name of snapshot to send
2869  * zc_value     short name of incremental fromsnap (may be empty)
2870  * zc_cookie    file descriptor to send stream to
2871  * zc_obj       fromorigin flag (mutually exclusive with zc_value)
2872  *
2873  * outputs: none
2874  */
2875 static int
2876 zfs_ioc_send(zfs_cmd_t *zc)
2877 {
2878         objset_t *fromsnap = NULL;
2879         objset_t *tosnap;
2880         file_t *fp;
2881         int error;
2882         offset_t off;
2883
2884         error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
2885             DS_MODE_USER | DS_MODE_READONLY, &tosnap);
2886         if (error)
2887                 return (error);
2888
2889         if (zc->zc_value[0] != '\0') {
2890                 char *buf;
2891                 char *cp;
2892
2893                 buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2894                 (void) strncpy(buf, zc->zc_name, MAXPATHLEN);
2895                 cp = strchr(buf, '@');
2896                 if (cp)
2897                         *(cp+1) = 0;
2898                 (void) strlcat(buf, zc->zc_value, MAXPATHLEN);
2899                 error = dmu_objset_open(buf, DMU_OST_ANY,
2900                     DS_MODE_USER | DS_MODE_READONLY, &fromsnap);
2901                 kmem_free(buf, MAXPATHLEN);
2902                 if (error) {
2903                         dmu_objset_close(tosnap);
2904                         return (error);
2905                 }
2906         }
2907
2908         fp = getf(zc->zc_cookie, 1);
2909         if (fp == NULL) {
2910                 dmu_objset_close(tosnap);
2911                 if (fromsnap)
2912                         dmu_objset_close(fromsnap);
2913                 return (EBADF);
2914         }
2915
2916         off = fp->f_offset;
2917         error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp, &off);
2918
2919         if (off >= 0 && off <= MAXOFFSET_T)
2920                 fp->f_offset = off;
2921         releasef(fp);
2922         if (fromsnap)
2923                 dmu_objset_close(fromsnap);
2924         dmu_objset_close(tosnap);
2925         return (error);
2926 }
2927
2928 static int
2929 zfs_ioc_inject_fault(zfs_cmd_t *zc)
2930 {
2931         int id, error;
2932
2933         error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
2934             &zc->zc_inject_record);
2935
2936         if (error == 0)
2937                 zc->zc_guid = (uint64_t)id;
2938
2939         return (error);
2940 }
2941
2942 static int
2943 zfs_ioc_clear_fault(zfs_cmd_t *zc)
2944 {
2945         return (zio_clear_fault((int)zc->zc_guid));
2946 }
2947
2948 static int
2949 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
2950 {
2951         int id = (int)zc->zc_guid;
2952         int error;
2953
2954         error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
2955             &zc->zc_inject_record);
2956
2957         zc->zc_guid = id;
2958
2959         return (error);
2960 }
2961
2962 static int
2963 zfs_ioc_error_log(zfs_cmd_t *zc)
2964 {
2965         spa_t *spa;
2966         int error;
2967         size_t count = (size_t)zc->zc_nvlist_dst_size;
2968
2969         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2970                 return (error);
2971
2972         error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
2973             &count);
2974         if (error == 0)
2975                 zc->zc_nvlist_dst_size = count;
2976         else
2977                 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
2978
2979         spa_close(spa, FTAG);
2980
2981         return (error);
2982 }
2983
2984 static int
2985 zfs_ioc_clear(zfs_cmd_t *zc)
2986 {
2987         spa_t *spa;
2988         vdev_t *vd;
2989         int error;
2990
2991         /*
2992          * On zpool clear we also fix up missing slogs
2993          */
2994         mutex_enter(&spa_namespace_lock);
2995         spa = spa_lookup(zc->zc_name);
2996         if (spa == NULL) {
2997                 mutex_exit(&spa_namespace_lock);
2998                 return (EIO);
2999         }
3000         if (spa->spa_log_state == SPA_LOG_MISSING) {
3001                 /* we need to let spa_open/spa_load clear the chains */
3002                 spa->spa_log_state = SPA_LOG_CLEAR;
3003         }
3004         mutex_exit(&spa_namespace_lock);
3005
3006         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3007                 return (error);
3008
3009         spa_vdev_state_enter(spa);
3010
3011         if (zc->zc_guid == 0) {
3012                 vd = NULL;
3013         } else {
3014                 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3015                 if (vd == NULL) {
3016                         (void) spa_vdev_state_exit(spa, NULL, ENODEV);
3017                         spa_close(spa, FTAG);
3018                         return (ENODEV);
3019                 }
3020         }
3021
3022         vdev_clear(spa, vd);
3023
3024         (void) spa_vdev_state_exit(spa, NULL, 0);
3025
3026         /*
3027          * Resume any suspended I/Os.
3028          */
3029         if (zio_resume(spa) != 0)
3030                 error = EIO;
3031
3032         spa_close(spa, FTAG);
3033
3034         return (error);
3035 }
3036
3037 /*
3038  * inputs:
3039  * zc_name      name of filesystem
3040  * zc_value     name of origin snapshot
3041  *
3042  * outputs:     none
3043  */
3044 static int
3045 zfs_ioc_promote(zfs_cmd_t *zc)
3046 {
3047         char *cp;
3048
3049         /*
3050          * We don't need to unmount *all* the origin fs's snapshots, but
3051          * it's easier.
3052          */
3053         cp = strchr(zc->zc_value, '@');
3054         if (cp)
3055                 *cp = '\0';
3056         (void) dmu_objset_find(zc->zc_value,
3057             zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
3058         return (dsl_dataset_promote(zc->zc_name));
3059 }
3060
3061 /*
3062  * Retrieve a single {user|group}{used|quota}@... property.
3063  *
3064  * inputs:
3065  * zc_name      name of filesystem
3066  * zc_objset_type zfs_userquota_prop_t
3067  * zc_value     domain name (eg. "S-1-234-567-89")
3068  * zc_guid      RID/UID/GID
3069  *
3070  * outputs:
3071  * zc_cookie    property value
3072  */
3073 static int
3074 zfs_ioc_userspace_one(zfs_cmd_t *zc)
3075 {
3076         zfsvfs_t *zfsvfs;
3077         int error;
3078
3079         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
3080                 return (EINVAL);
3081
3082         error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
3083         if (error)
3084                 return (error);
3085
3086         error = zfs_userspace_one(zfsvfs,
3087             zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
3088         zfsvfs_rele(zfsvfs, FTAG);
3089
3090         return (error);
3091 }
3092
3093 /*
3094  * inputs:
3095  * zc_name              name of filesystem
3096  * zc_cookie            zap cursor
3097  * zc_objset_type       zfs_userquota_prop_t
3098  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
3099  *
3100  * outputs:
3101  * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
3102  * zc_cookie    zap cursor
3103  */
3104 static int
3105 zfs_ioc_userspace_many(zfs_cmd_t *zc)
3106 {
3107         zfsvfs_t *zfsvfs;
3108         int error;
3109
3110         error = zfsvfs_hold(zc->zc_name, B_TRUE, FTAG, &zfsvfs);
3111         if (error)
3112                 return (error);
3113
3114         int bufsize = zc->zc_nvlist_dst_size;
3115         void *buf = kmem_alloc(bufsize, KM_SLEEP);
3116
3117         error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
3118             buf, &zc->zc_nvlist_dst_size);
3119
3120         if (error == 0) {
3121                 error = xcopyout(buf,
3122                     (void *)(uintptr_t)zc->zc_nvlist_dst,
3123                     zc->zc_nvlist_dst_size);
3124         }
3125         kmem_free(buf, bufsize);
3126         zfsvfs_rele(zfsvfs, FTAG);
3127
3128         return (error);
3129 }
3130
3131 /*
3132  * inputs:
3133  * zc_name              name of filesystem
3134  *
3135  * outputs:
3136  * none
3137  */
3138 static int
3139 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
3140 {
3141         objset_t *os;
3142         int error;
3143         zfsvfs_t *zfsvfs;
3144
3145         if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
3146                 if (!dmu_objset_userused_enabled(zfsvfs->z_os->os)) {
3147                         /*
3148                          * If userused is not enabled, it may be because the
3149                          * objset needs to be closed & reopened (to grow the
3150                          * objset_phys_t).  Suspend/resume the fs will do that.
3151                          */
3152                         int mode;
3153                         error = zfs_suspend_fs(zfsvfs, NULL, &mode);
3154                         if (error == 0) {
3155                                 error = zfs_resume_fs(zfsvfs,
3156                                     zc->zc_name, mode);
3157                         }
3158                 }
3159                 if (error == 0)
3160                         error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
3161                 VFS_RELE(zfsvfs->z_vfs);
3162         } else {
3163                 error = dmu_objset_open(zc->zc_name, DMU_OST_ANY,
3164                     DS_MODE_USER, &os);
3165                 if (error)
3166                         return (error);
3167
3168                 error = dmu_objset_userspace_upgrade(os);
3169                 dmu_objset_close(os);
3170         }
3171
3172         return (error);
3173 }
3174
3175 #ifdef sun
3176 /*
3177  * We don't want to have a hard dependency
3178  * against some special symbols in sharefs
3179  * nfs, and smbsrv.  Determine them if needed when
3180  * the first file system is shared.
3181  * Neither sharefs, nfs or smbsrv are unloadable modules.
3182  */
3183 int (*znfsexport_fs)(void *arg);
3184 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
3185 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
3186
3187 int zfs_nfsshare_inited;
3188 int zfs_smbshare_inited;
3189
3190 ddi_modhandle_t nfs_mod;
3191 ddi_modhandle_t sharefs_mod;
3192 ddi_modhandle_t smbsrv_mod;
3193 #endif  /* sun */
3194 kmutex_t zfs_share_lock;
3195
3196 #ifdef sun
3197 static int
3198 zfs_init_sharefs()
3199 {
3200         int error;
3201
3202         ASSERT(MUTEX_HELD(&zfs_share_lock));
3203         /* Both NFS and SMB shares also require sharetab support. */
3204         if (sharefs_mod == NULL && ((sharefs_mod =
3205             ddi_modopen("fs/sharefs",
3206             KRTLD_MODE_FIRST, &error)) == NULL)) {
3207                 return (ENOSYS);
3208         }
3209         if (zshare_fs == NULL && ((zshare_fs =
3210             (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
3211             ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
3212                 return (ENOSYS);
3213         }
3214         return (0);
3215 }
3216 #endif /* sun */
3217
3218 static int
3219 zfs_ioc_share(zfs_cmd_t *zc)
3220 {
3221 #ifdef sun
3222         int error;
3223         int opcode;
3224
3225         switch (zc->zc_share.z_sharetype) {
3226         case ZFS_SHARE_NFS:
3227         case ZFS_UNSHARE_NFS:
3228                 if (zfs_nfsshare_inited == 0) {
3229                         mutex_enter(&zfs_share_lock);
3230                         if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
3231                             KRTLD_MODE_FIRST, &error)) == NULL)) {
3232                                 mutex_exit(&zfs_share_lock);
3233                                 return (ENOSYS);
3234                         }
3235                         if (znfsexport_fs == NULL &&
3236                             ((znfsexport_fs = (int (*)(void *))
3237                             ddi_modsym(nfs_mod,
3238                             "nfs_export", &error)) == NULL)) {
3239                                 mutex_exit(&zfs_share_lock);
3240                                 return (ENOSYS);
3241                         }
3242                         error = zfs_init_sharefs();
3243                         if (error) {
3244                                 mutex_exit(&zfs_share_lock);
3245                                 return (ENOSYS);
3246                         }
3247                         zfs_nfsshare_inited = 1;
3248                         mutex_exit(&zfs_share_lock);
3249                 }
3250                 break;
3251         case ZFS_SHARE_SMB:
3252         case ZFS_UNSHARE_SMB:
3253                 if (zfs_smbshare_inited == 0) {
3254                         mutex_enter(&zfs_share_lock);
3255                         if (smbsrv_mod == NULL && ((smbsrv_mod =
3256                             ddi_modopen("drv/smbsrv",
3257                             KRTLD_MODE_FIRST, &error)) == NULL)) {
3258                                 mutex_exit(&zfs_share_lock);
3259                                 return (ENOSYS);
3260                         }
3261                         if (zsmbexport_fs == NULL && ((zsmbexport_fs =
3262                             (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
3263                             "smb_server_share", &error)) == NULL)) {
3264                                 mutex_exit(&zfs_share_lock);
3265                                 return (ENOSYS);
3266                         }
3267                         error = zfs_init_sharefs();
3268                         if (error) {
3269                                 mutex_exit(&zfs_share_lock);
3270                                 return (ENOSYS);
3271                         }
3272                         zfs_smbshare_inited = 1;
3273                         mutex_exit(&zfs_share_lock);
3274                 }
3275                 break;
3276         default:
3277                 return (EINVAL);
3278         }
3279
3280         switch (zc->zc_share.z_sharetype) {
3281         case ZFS_SHARE_NFS:
3282         case ZFS_UNSHARE_NFS:
3283                 if (error =
3284                     znfsexport_fs((void *)
3285                     (uintptr_t)zc->zc_share.z_exportdata))
3286                         return (error);
3287                 break;
3288         case ZFS_SHARE_SMB:
3289         case ZFS_UNSHARE_SMB:
3290                 if (error = zsmbexport_fs((void *)
3291                     (uintptr_t)zc->zc_share.z_exportdata,
3292                     zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
3293                     B_TRUE: B_FALSE)) {
3294                         return (error);
3295                 }
3296                 break;
3297         }
3298
3299         opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
3300             zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
3301             SHAREFS_ADD : SHAREFS_REMOVE;
3302
3303         /*
3304          * Add or remove share from sharetab
3305          */
3306         error = zshare_fs(opcode,
3307             (void *)(uintptr_t)zc->zc_share.z_sharedata,
3308             zc->zc_share.z_sharemax);
3309
3310         return (error);
3311 #else   /* sun */
3312         return (ENOSYS);
3313 #endif  /* sun */
3314 }
3315
3316 ace_t full_access[] = {
3317         {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
3318 };
3319
3320 #ifdef sun
3321 /*
3322  * Remove all ACL files in shares dir
3323  */
3324 static int
3325 zfs_smb_acl_purge(znode_t *dzp)
3326 {
3327         zap_cursor_t    zc;
3328         zap_attribute_t zap;
3329         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3330         int error;
3331
3332         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
3333             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
3334             zap_cursor_advance(&zc)) {
3335                 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
3336                     NULL, 0)) != 0)
3337                         break;
3338         }
3339         zap_cursor_fini(&zc);
3340         return (error);
3341 }
3342 #endif  /* sun */
3343
3344 static int
3345 zfs_ioc_smb_acl(zfs_cmd_t *zc)
3346 {
3347 #ifdef sun
3348         vnode_t *vp;
3349         znode_t *dzp;
3350         vnode_t *resourcevp = NULL;
3351         znode_t *sharedir;
3352         zfsvfs_t *zfsvfs;
3353         nvlist_t *nvlist;
3354         char *src, *target;
3355         vattr_t vattr;
3356         vsecattr_t vsec;
3357         int error = 0;
3358
3359         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
3360             NO_FOLLOW, NULL, &vp)) != 0)
3361                 return (error);
3362
3363         /* Now make sure mntpnt and dataset are ZFS */
3364
3365         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
3366             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
3367             zc->zc_name) != 0)) {
3368                 VN_RELE(vp);
3369                 return (EINVAL);
3370         }
3371
3372         dzp = VTOZ(vp);
3373         zfsvfs = dzp->z_zfsvfs;
3374
3375         ZFS_ENTER(zfsvfs);
3376
3377         /*
3378          * Create share dir if its missing.
3379          */
3380         mutex_enter(&zfsvfs->z_lock);
3381         if (zfsvfs->z_shares_dir == 0) {
3382                 dmu_tx_t *tx;
3383
3384                 tx = dmu_tx_create(zfsvfs->z_os);
3385                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
3386                     ZFS_SHARES_DIR);
3387                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
3388                 error = dmu_tx_assign(tx, TXG_WAIT);
3389                 if (error) {
3390                         dmu_tx_abort(tx);
3391                 } else {
3392                         error = zfs_create_share_dir(zfsvfs, tx);
3393                         dmu_tx_commit(tx);
3394                 }
3395                 if (error) {
3396                         mutex_exit(&zfsvfs->z_lock);
3397                         VN_RELE(vp);
3398                         ZFS_EXIT(zfsvfs);
3399                         return (error);
3400                 }
3401         }
3402         mutex_exit(&zfsvfs->z_lock);
3403
3404         ASSERT(zfsvfs->z_shares_dir);
3405         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
3406                 VN_RELE(vp);
3407                 ZFS_EXIT(zfsvfs);
3408                 return (error);
3409         }
3410
3411         switch (zc->zc_cookie) {
3412         case ZFS_SMB_ACL_ADD:
3413                 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
3414                 vattr.va_type = VREG;
3415                 vattr.va_mode = S_IFREG|0777;
3416                 vattr.va_uid = 0;
3417                 vattr.va_gid = 0;
3418
3419                 vsec.vsa_mask = VSA_ACE;
3420                 vsec.vsa_aclentp = &full_access;
3421                 vsec.vsa_aclentsz = sizeof (full_access);
3422                 vsec.vsa_aclcnt = 1;
3423
3424                 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
3425                     &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
3426                 if (resourcevp)
3427                         VN_RELE(resourcevp);
3428                 break;
3429
3430         case ZFS_SMB_ACL_REMOVE:
3431                 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
3432                     NULL, 0);
3433                 break;
3434
3435         case ZFS_SMB_ACL_RENAME:
3436                 if ((error = get_nvlist(zc->zc_nvlist_src,
3437                     zc->zc_nvlist_src_size, &nvlist)) != 0) {
3438                         VN_RELE(vp);
3439                         ZFS_EXIT(zfsvfs);
3440                         return (error);
3441                 }
3442                 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
3443                     nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
3444                     &target)) {
3445                         VN_RELE(vp);
3446                         VN_RELE(ZTOV(sharedir));
3447                         ZFS_EXIT(zfsvfs);
3448                         return (error);
3449                 }
3450                 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
3451                     kcred, NULL, 0);
3452                 nvlist_free(nvlist);
3453                 break;
3454
3455         case ZFS_SMB_ACL_PURGE:
3456                 error = zfs_smb_acl_purge(sharedir);
3457                 break;
3458
3459         default:
3460                 error = EINVAL;
3461                 break;
3462         }
3463
3464         VN_RELE(vp);
3465         VN_RELE(ZTOV(sharedir));
3466
3467         ZFS_EXIT(zfsvfs);
3468
3469         return (error);
3470 #else   /* !sun */
3471         return (EOPNOTSUPP);
3472 #endif  /* !sun */
3473 }
3474
3475 /*
3476  * pool create, destroy, and export don't log the history as part of
3477  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
3478  * do the logging of those commands.
3479  */
3480 static int
3481 zfs_ioc_jail(zfs_cmd_t *zc)
3482 {
3483
3484         return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
3485             (int)zc->zc_jailid));
3486 }
3487
3488 static int
3489 zfs_ioc_unjail(zfs_cmd_t *zc)
3490 {
3491
3492         return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
3493             (int)zc->zc_jailid));
3494 }
3495
3496 static zfs_ioc_vec_t zfs_ioc_vec[] = {
3497         { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3498             B_FALSE },
3499         { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3500             B_FALSE },
3501         { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3502             B_FALSE },
3503         { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3504             B_FALSE },
3505         { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
3506             B_FALSE },
3507         { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3508             B_FALSE },
3509         { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
3510             B_FALSE },
3511         { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3512             B_TRUE },
3513         { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
3514             B_FALSE },
3515         { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3516             B_TRUE },
3517         { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3518             B_FALSE },
3519         { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3520             B_TRUE },
3521         { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3522             B_TRUE },
3523         { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3524             B_FALSE },
3525         { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3526             B_TRUE },
3527         { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3528             B_TRUE },
3529         { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3530             B_TRUE },
3531         { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3532             B_TRUE },
3533         { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3534             B_FALSE },
3535         { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3536             B_TRUE },
3537         { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3538             B_TRUE },
3539         { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE },
3540         { zfs_ioc_create_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3541             B_FALSE },
3542         { zfs_ioc_remove_minor, zfs_secpolicy_minor, DATASET_NAME, B_FALSE,
3543             B_FALSE },
3544         { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE },
3545         { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
3546             B_TRUE},
3547         { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
3548             B_TRUE },
3549         { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE },
3550         { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE },
3551         { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE },
3552         { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3553             B_FALSE },
3554         { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3555             B_FALSE },
3556         { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
3557             B_FALSE },
3558         { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
3559             B_FALSE },
3560         { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE },
3561         { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
3562             B_TRUE },
3563         { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
3564             B_TRUE },
3565         { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
3566             B_TRUE },
3567         { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE,
3568             B_FALSE },
3569         { zfs_ioc_obj_to_path, zfs_secpolicy_config, NO_NAME, B_FALSE,
3570             B_FALSE },
3571         { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
3572             B_TRUE },
3573         { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
3574             B_FALSE },
3575         { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
3576             B_TRUE },
3577         { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
3578             B_FALSE },
3579         { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE,
3580             B_FALSE },
3581         { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE },
3582         { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
3583             B_TRUE },
3584         { zfs_ioc_jail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
3585         { zfs_ioc_unjail, zfs_secpolicy_config, DATASET_NAME, B_TRUE, B_FALSE },
3586         { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
3587             B_FALSE },
3588         { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one,
3589             DATASET_NAME, B_FALSE, B_FALSE },
3590         { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many,
3591             DATASET_NAME, B_FALSE, B_FALSE },
3592         { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
3593             DATASET_NAME, B_FALSE, B_TRUE },
3594         { zfs_ioc_vdev_setfru,  zfs_secpolicy_config, POOL_NAME, B_FALSE,
3595             B_TRUE }
3596 };
3597
3598 int
3599 pool_status_check(const char *name, zfs_ioc_namecheck_t type)
3600 {
3601         spa_t *spa;
3602         char pool[ZFS_MAXNAMELEN];
3603         int error;
3604
3605         ASSERT(type == POOL_NAME || type == DATASET_NAME);
3606
3607         error = spa_open(name, &spa, FTAG);
3608         if (error == 0) {
3609                 if (spa_suspended(spa))
3610                         error = EAGAIN;
3611                 spa_close(spa, FTAG);
3612         }
3613         return (error);
3614 }
3615
3616 static int
3617 zfsdev_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
3618     struct thread *td)
3619 {
3620         zfs_cmd_t *zc = (void *)addr;
3621         uint_t vec;
3622         int error;
3623
3624         vec = ZFS_IOC(cmd);
3625
3626         if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
3627                 return (EINVAL);
3628
3629         error = zfs_ioc_vec[vec].zvec_secpolicy(zc, td->td_ucred);
3630
3631         /*
3632          * Ensure that all pool/dataset names are valid before we pass down to
3633          * the lower layers.
3634          */
3635         if (error == 0) {
3636                 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3637                 switch (zfs_ioc_vec[vec].zvec_namecheck) {
3638                 case POOL_NAME:
3639                         if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
3640                                 error = EINVAL;
3641                         if (zfs_ioc_vec[vec].zvec_pool_check)
3642                                 error = pool_status_check(zc->zc_name,
3643                                     zfs_ioc_vec[vec].zvec_namecheck);
3644                         break;
3645
3646                 case DATASET_NAME:
3647                         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
3648                                 error = EINVAL;
3649                         if (zfs_ioc_vec[vec].zvec_pool_check)
3650                                 error = pool_status_check(zc->zc_name,
3651                                     zfs_ioc_vec[vec].zvec_namecheck);
3652                         break;
3653
3654                 case NO_NAME:
3655                         break;
3656                 }
3657         }
3658
3659         if (error == 0)
3660                 error = zfs_ioc_vec[vec].zvec_func(zc);
3661
3662         if (error == 0) {
3663                 if (zfs_ioc_vec[vec].zvec_his_log)
3664                         zfs_log_history(zc);
3665         }
3666
3667         return (error);
3668 }
3669
3670 /*
3671  * OK, so this is a little weird.
3672  *
3673  * /dev/zfs is the control node, i.e. minor 0.
3674  * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
3675  *
3676  * /dev/zfs has basically nothing to do except serve up ioctls,
3677  * so most of the standard driver entry points are in zvol.c.
3678  */
3679 static struct cdevsw zfs_cdevsw = {
3680         .d_version =    D_VERSION,
3681         .d_ioctl =      zfsdev_ioctl,
3682         .d_name =       ZFS_DEV_NAME
3683 };
3684
3685 static void
3686 zfsdev_init(void)
3687 {
3688         zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
3689             ZFS_DEV_NAME);
3690 }
3691
3692 static void
3693 zfsdev_fini(void)
3694 {
3695         if (zfsdev != NULL)
3696                 destroy_dev(zfsdev);
3697 }
3698
3699 static struct root_hold_token *zfs_root_token;
3700 struct proc *zfsproc;
3701
3702 uint_t zfs_fsyncer_key;
3703 extern uint_t rrw_tsd_key;
3704
3705 static int
3706 zfs_modevent(module_t mod, int type, void *unused __unused)
3707 {
3708         int error = 0;
3709
3710         switch (type) {
3711         case MOD_LOAD:
3712                 zfs_root_token = root_mount_hold("ZFS");
3713
3714                 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
3715
3716                 spa_init(FREAD | FWRITE);
3717                 zfs_init();
3718                 zvol_init();
3719
3720                 tsd_create(&zfs_fsyncer_key, NULL);
3721                 tsd_create(&rrw_tsd_key, NULL);
3722
3723                 printf("ZFS storage pool version " SPA_VERSION_STRING "\n");
3724                 root_mount_rel(zfs_root_token);
3725
3726                 zfsdev_init();
3727                 break;
3728         case MOD_UNLOAD:
3729                 if (spa_busy() || zfs_busy() || zvol_busy() ||
3730                     zio_injection_enabled) {
3731                         error = EBUSY;
3732                         break;
3733                 }
3734
3735                 zfsdev_fini();
3736                 zvol_fini();
3737                 zfs_fini();
3738                 spa_fini();
3739
3740                 tsd_destroy(&zfs_fsyncer_key);
3741                 tsd_destroy(&rrw_tsd_key);
3742
3743                 mutex_destroy(&zfs_share_lock);
3744                 break;
3745         default:
3746                 error = EOPNOTSUPP;
3747                 break;
3748         }
3749         return (error);
3750 }
3751
3752 static moduledata_t zfs_mod = {
3753         "zfsctrl",
3754         zfs_modevent,
3755         0
3756 };
3757 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
3758 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
3759 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);