Suppress kmem_alloc() warning in zfs_prop_set_special()
[zfs.git] / module / 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/errno.h>
28 #include <sys/uio.h>
29 #include <sys/buf.h>
30 #include <sys/modctl.h>
31 #include <sys/open.h>
32 #include <sys/file.h>
33 #include <sys/kmem.h>
34 #include <sys/conf.h>
35 #include <sys/cmn_err.h>
36 #include <sys/stat.h>
37 #include <sys/zfs_ioctl.h>
38 #include <sys/zfs_vfsops.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/zap.h>
41 #include <sys/spa.h>
42 #include <sys/spa_impl.h>
43 #include <sys/vdev.h>
44 #include <sys/priv_impl.h>
45 #include <sys/dmu.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_deleg.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/ddi.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/policy.h>
55 #include <sys/zone.h>
56 #include <sys/nvpair.h>
57 #include <sys/pathname.h>
58 #include <sys/mount.h>
59 #include <sys/sdt.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/zfs_dir.h>
62 #include <sys/zfs_onexit.h>
63 #include <sys/zvol.h>
64 #include <sys/dsl_scan.h>
65 #include <sharefs/share.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/fm/util.h>
68
69 #include <linux/miscdevice.h>
70
71 #include "zfs_namecheck.h"
72 #include "zfs_prop.h"
73 #include "zfs_deleg.h"
74 #include "zfs_comutil.h"
75
76 kmutex_t zfsdev_state_lock;
77 list_t zfsdev_state_list;
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 enum {
92         POOL_CHECK_NONE         = 1 << 0,
93         POOL_CHECK_SUSPENDED    = 1 << 1,
94         POOL_CHECK_READONLY     = 1 << 2
95 } zfs_ioc_poolcheck_t;
96
97 typedef struct zfs_ioc_vec {
98         zfs_ioc_func_t          *zvec_func;
99         zfs_secpolicy_func_t    *zvec_secpolicy;
100         zfs_ioc_namecheck_t     zvec_namecheck;
101         boolean_t               zvec_his_log;
102         zfs_ioc_poolcheck_t     zvec_pool_check;
103 } zfs_ioc_vec_t;
104
105 /* This array is indexed by zfs_userquota_prop_t */
106 static const char *userquota_perms[] = {
107         ZFS_DELEG_PERM_USERUSED,
108         ZFS_DELEG_PERM_USERQUOTA,
109         ZFS_DELEG_PERM_GROUPUSED,
110         ZFS_DELEG_PERM_GROUPQUOTA,
111 };
112
113 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
114 static int zfs_check_settable(const char *name, nvpair_t *property,
115     cred_t *cr);
116 static int zfs_check_clearable(char *dataset, nvlist_t *props,
117     nvlist_t **errors);
118 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
119     boolean_t *);
120 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
121
122 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
123 void
124 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
125 {
126         const char *newfile;
127         char buf[512];
128         va_list adx;
129
130         /*
131          * Get rid of annoying "../common/" prefix to filename.
132          */
133         newfile = strrchr(file, '/');
134         if (newfile != NULL) {
135                 newfile = newfile + 1; /* Get rid of leading / */
136         } else {
137                 newfile = file;
138         }
139
140         va_start(adx, fmt);
141         (void) vsnprintf(buf, sizeof (buf), fmt, adx);
142         va_end(adx);
143
144         /*
145          * To get this data, use the zfs-dprintf probe as so:
146          * dtrace -q -n 'zfs-dprintf \
147          *      /stringof(arg0) == "dbuf.c"/ \
148          *      {printf("%s: %s", stringof(arg1), stringof(arg3))}'
149          * arg0 = file name
150          * arg1 = function name
151          * arg2 = line number
152          * arg3 = message
153          */
154         DTRACE_PROBE4(zfs__dprintf,
155             char *, newfile, char *, func, int, line, char *, buf);
156 }
157
158 static void
159 history_str_free(char *buf)
160 {
161         kmem_free(buf, HIS_MAX_RECORD_LEN);
162 }
163
164 static char *
165 history_str_get(zfs_cmd_t *zc)
166 {
167         char *buf;
168
169         if (zc->zc_history == 0)
170                 return (NULL);
171
172         buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP | KM_NODEBUG);
173         if (copyinstr((void *)(uintptr_t)zc->zc_history,
174             buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
175                 history_str_free(buf);
176                 return (NULL);
177         }
178
179         buf[HIS_MAX_RECORD_LEN -1] = '\0';
180
181         return (buf);
182 }
183
184 /*
185  * Check to see if the named dataset is currently defined as bootable
186  */
187 static boolean_t
188 zfs_is_bootfs(const char *name)
189 {
190         objset_t *os;
191
192         if (dmu_objset_hold(name, FTAG, &os) == 0) {
193                 boolean_t ret;
194                 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
195                 dmu_objset_rele(os, FTAG);
196                 return (ret);
197         }
198         return (B_FALSE);
199 }
200
201 /*
202  * zfs_earlier_version
203  *
204  *      Return non-zero if the spa version is less than requested version.
205  */
206 static int
207 zfs_earlier_version(const char *name, int version)
208 {
209         spa_t *spa;
210
211         if (spa_open(name, &spa, FTAG) == 0) {
212                 if (spa_version(spa) < version) {
213                         spa_close(spa, FTAG);
214                         return (1);
215                 }
216                 spa_close(spa, FTAG);
217         }
218         return (0);
219 }
220
221 /*
222  * zpl_earlier_version
223  *
224  * Return TRUE if the ZPL version is less than requested version.
225  */
226 static boolean_t
227 zpl_earlier_version(const char *name, int version)
228 {
229         objset_t *os;
230         boolean_t rc = B_TRUE;
231
232         if (dmu_objset_hold(name, FTAG, &os) == 0) {
233                 uint64_t zplversion;
234
235                 if (dmu_objset_type(os) != DMU_OST_ZFS) {
236                         dmu_objset_rele(os, FTAG);
237                         return (B_TRUE);
238                 }
239                 /* XXX reading from non-owned objset */
240                 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
241                         rc = zplversion < version;
242                 dmu_objset_rele(os, FTAG);
243         }
244         return (rc);
245 }
246
247 static void
248 zfs_log_history(zfs_cmd_t *zc)
249 {
250         spa_t *spa;
251         char *buf;
252
253         if ((buf = history_str_get(zc)) == NULL)
254                 return;
255
256         if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
257                 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
258                         (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
259                 spa_close(spa, FTAG);
260         }
261         history_str_free(buf);
262 }
263
264 /*
265  * Policy for top-level read operations (list pools).  Requires no privileges,
266  * and can be used in the local zone, as there is no associated dataset.
267  */
268 /* ARGSUSED */
269 static int
270 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
271 {
272         return (0);
273 }
274
275 /*
276  * Policy for dataset read operations (list children, get statistics).  Requires
277  * no privileges, but must be visible in the local zone.
278  */
279 /* ARGSUSED */
280 static int
281 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
282 {
283         if (INGLOBALZONE(curproc) ||
284             zone_dataset_visible(zc->zc_name, NULL))
285                 return (0);
286
287         return (ENOENT);
288 }
289
290 static int
291 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
292 {
293         int writable = 1;
294
295         /*
296          * The dataset must be visible by this zone -- check this first
297          * so they don't see EPERM on something they shouldn't know about.
298          */
299         if (!INGLOBALZONE(curproc) &&
300             !zone_dataset_visible(dataset, &writable))
301                 return (ENOENT);
302
303         if (INGLOBALZONE(curproc)) {
304                 /*
305                  * If the fs is zoned, only root can access it from the
306                  * global zone.
307                  */
308                 if (secpolicy_zfs(cr) && zoned)
309                         return (EPERM);
310         } else {
311                 /*
312                  * If we are in a local zone, the 'zoned' property must be set.
313                  */
314                 if (!zoned)
315                         return (EPERM);
316
317                 /* must be writable by this zone */
318                 if (!writable)
319                         return (EPERM);
320         }
321         return (0);
322 }
323
324 static int
325 zfs_dozonecheck(const char *dataset, cred_t *cr)
326 {
327         uint64_t zoned;
328
329         if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
330                 return (ENOENT);
331
332         return (zfs_dozonecheck_impl(dataset, zoned, cr));
333 }
334
335 static int
336 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
337 {
338         uint64_t zoned;
339
340         rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
341         if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
342                 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
343                 return (ENOENT);
344         }
345         rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
346
347         return (zfs_dozonecheck_impl(dataset, zoned, cr));
348 }
349
350 int
351 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
352 {
353         int error;
354
355         error = zfs_dozonecheck(name, cr);
356         if (error == 0) {
357                 error = secpolicy_zfs(cr);
358                 if (error)
359                         error = dsl_deleg_access(name, perm, cr);
360         }
361         return (error);
362 }
363
364 int
365 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
366     const char *perm, cred_t *cr)
367 {
368         int error;
369
370         error = zfs_dozonecheck_ds(name, ds, cr);
371         if (error == 0) {
372                 error = secpolicy_zfs(cr);
373                 if (error)
374                         error = dsl_deleg_access_impl(ds, perm, cr);
375         }
376         return (error);
377 }
378
379 /*
380  * Policy for setting the security label property.
381  *
382  * Returns 0 for success, non-zero for access and other errors.
383  */
384 static int
385 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
386 {
387 #ifdef HAVE_MLSLABEL
388         char            ds_hexsl[MAXNAMELEN];
389         bslabel_t       ds_sl, new_sl;
390         boolean_t       new_default = FALSE;
391         uint64_t        zoned;
392         int             needed_priv = -1;
393         int             error;
394
395         /* First get the existing dataset label. */
396         error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
397             1, sizeof (ds_hexsl), &ds_hexsl, NULL);
398         if (error)
399                 return (EPERM);
400
401         if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
402                 new_default = TRUE;
403
404         /* The label must be translatable */
405         if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
406                 return (EINVAL);
407
408         /*
409          * In a non-global zone, disallow attempts to set a label that
410          * doesn't match that of the zone; otherwise no other checks
411          * are needed.
412          */
413         if (!INGLOBALZONE(curproc)) {
414                 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
415                         return (EPERM);
416                 return (0);
417         }
418
419         /*
420          * For global-zone datasets (i.e., those whose zoned property is
421          * "off", verify that the specified new label is valid for the
422          * global zone.
423          */
424         if (dsl_prop_get_integer(name,
425             zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
426                 return (EPERM);
427         if (!zoned) {
428                 if (zfs_check_global_label(name, strval) != 0)
429                         return (EPERM);
430         }
431
432         /*
433          * If the existing dataset label is nondefault, check if the
434          * dataset is mounted (label cannot be changed while mounted).
435          * Get the zfs_sb_t; if there isn't one, then the dataset isn't
436          * mounted (or isn't a dataset, doesn't exist, ...).
437          */
438         if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
439                 objset_t *os;
440                 static char *setsl_tag = "setsl_tag";
441
442                 /*
443                  * Try to own the dataset; abort if there is any error,
444                  * (e.g., already mounted, in use, or other error).
445                  */
446                 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
447                     setsl_tag, &os);
448                 if (error)
449                         return (EPERM);
450
451                 dmu_objset_disown(os, setsl_tag);
452
453                 if (new_default) {
454                         needed_priv = PRIV_FILE_DOWNGRADE_SL;
455                         goto out_check;
456                 }
457
458                 if (hexstr_to_label(strval, &new_sl) != 0)
459                         return (EPERM);
460
461                 if (blstrictdom(&ds_sl, &new_sl))
462                         needed_priv = PRIV_FILE_DOWNGRADE_SL;
463                 else if (blstrictdom(&new_sl, &ds_sl))
464                         needed_priv = PRIV_FILE_UPGRADE_SL;
465         } else {
466                 /* dataset currently has a default label */
467                 if (!new_default)
468                         needed_priv = PRIV_FILE_UPGRADE_SL;
469         }
470
471 out_check:
472         if (needed_priv != -1)
473                 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
474         return (0);
475 #else
476         return ENOTSUP;
477 #endif /* HAVE_MLSLABEL */
478 }
479
480 static int
481 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
482     cred_t *cr)
483 {
484         char *strval;
485
486         /*
487          * Check permissions for special properties.
488          */
489         switch (prop) {
490         default:
491                 break;
492         case ZFS_PROP_ZONED:
493                 /*
494                  * Disallow setting of 'zoned' from within a local zone.
495                  */
496                 if (!INGLOBALZONE(curproc))
497                         return (EPERM);
498                 break;
499
500         case ZFS_PROP_QUOTA:
501                 if (!INGLOBALZONE(curproc)) {
502                         uint64_t zoned;
503                         char setpoint[MAXNAMELEN];
504                         /*
505                          * Unprivileged users are allowed to modify the
506                          * quota on things *under* (ie. contained by)
507                          * the thing they own.
508                          */
509                         if (dsl_prop_get_integer(dsname, "zoned", &zoned,
510                             setpoint))
511                                 return (EPERM);
512                         if (!zoned || strlen(dsname) <= strlen(setpoint))
513                                 return (EPERM);
514                 }
515                 break;
516
517         case ZFS_PROP_MLSLABEL:
518                 if (!is_system_labeled())
519                         return (EPERM);
520
521                 if (nvpair_value_string(propval, &strval) == 0) {
522                         int err;
523
524                         err = zfs_set_slabel_policy(dsname, strval, CRED());
525                         if (err != 0)
526                                 return (err);
527                 }
528                 break;
529         }
530
531         return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
532 }
533
534 int
535 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
536 {
537         int error;
538
539         error = zfs_dozonecheck(zc->zc_name, cr);
540         if (error)
541                 return (error);
542
543         /*
544          * permission to set permissions will be evaluated later in
545          * dsl_deleg_can_allow()
546          */
547         return (0);
548 }
549
550 int
551 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
552 {
553         return (zfs_secpolicy_write_perms(zc->zc_name,
554             ZFS_DELEG_PERM_ROLLBACK, cr));
555 }
556
557 int
558 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
559 {
560         spa_t *spa;
561         dsl_pool_t *dp;
562         dsl_dataset_t *ds;
563         char *cp;
564         int error;
565
566         /*
567          * Generate the current snapshot name from the given objsetid, then
568          * use that name for the secpolicy/zone checks.
569          */
570         cp = strchr(zc->zc_name, '@');
571         if (cp == NULL)
572                 return (EINVAL);
573         error = spa_open(zc->zc_name, &spa, FTAG);
574         if (error)
575                 return (error);
576
577         dp = spa_get_dsl(spa);
578         rw_enter(&dp->dp_config_rwlock, RW_READER);
579         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
580         rw_exit(&dp->dp_config_rwlock);
581         spa_close(spa, FTAG);
582         if (error)
583                 return (error);
584
585         dsl_dataset_name(ds, zc->zc_name);
586
587         error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
588             ZFS_DELEG_PERM_SEND, cr);
589         dsl_dataset_rele(ds, FTAG);
590
591         return (error);
592 }
593
594 #ifdef HAVE_SMB_SHARE
595 static int
596 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
597 {
598         vnode_t *vp;
599         int error;
600
601         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
602             NO_FOLLOW, NULL, &vp)) != 0)
603                 return (error);
604
605         /* Now make sure mntpnt and dataset are ZFS */
606
607         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
608             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
609             zc->zc_name) != 0)) {
610                 VN_RELE(vp);
611                 return (EPERM);
612         }
613
614         VN_RELE(vp);
615         return (dsl_deleg_access(zc->zc_name,
616             ZFS_DELEG_PERM_SHARE, cr));
617 }
618 #endif /* HAVE_SMB_SHARE */
619
620 int
621 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
622 {
623 #ifdef HAVE_SMB_SHARE
624         if (!INGLOBALZONE(curproc))
625                 return (EPERM);
626
627         if (secpolicy_nfs(cr) == 0) {
628                 return (0);
629         } else {
630                 return (zfs_secpolicy_deleg_share(zc, cr));
631         }
632 #else
633         return (ENOTSUP);
634 #endif /* HAVE_SMB_SHARE */
635 }
636
637 int
638 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
639 {
640 #ifdef HAVE_SMB_SHARE
641         if (!INGLOBALZONE(curproc))
642                 return (EPERM);
643
644         if (secpolicy_smb(cr) == 0) {
645                 return (0);
646         } else {
647                 return (zfs_secpolicy_deleg_share(zc, cr));
648         }
649 #else
650         return (ENOTSUP);
651 #endif /* HAVE_SMB_SHARE */
652 }
653
654 static int
655 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
656 {
657         char *cp;
658
659         /*
660          * Remove the @bla or /bla from the end of the name to get the parent.
661          */
662         (void) strncpy(parent, datasetname, parentsize);
663         cp = strrchr(parent, '@');
664         if (cp != NULL) {
665                 cp[0] = '\0';
666         } else {
667                 cp = strrchr(parent, '/');
668                 if (cp == NULL)
669                         return (ENOENT);
670                 cp[0] = '\0';
671         }
672
673         return (0);
674 }
675
676 int
677 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
678 {
679         int error;
680
681         if ((error = zfs_secpolicy_write_perms(name,
682             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
683                 return (error);
684
685         return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
686 }
687
688 static int
689 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
690 {
691         return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
692 }
693
694 /*
695  * Destroying snapshots with delegated permissions requires
696  * descendent mount and destroy permissions.
697  * Reassemble the full filesystem@snap name so dsl_deleg_access()
698  * can do the correct permission check.
699  *
700  * Since this routine is used when doing a recursive destroy of snapshots
701  * and destroying snapshots requires descendent permissions, a successfull
702  * check of the top level snapshot applies to snapshots of all descendent
703  * datasets as well.
704  *
705  * The target snapshot may not exist when doing a recursive destroy.
706  * In this case fallback to permissions of the parent dataset.
707  */
708 static int
709 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
710 {
711         int error;
712         char *dsname;
713
714         dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
715
716         error = zfs_secpolicy_destroy_perms(dsname, cr);
717         if (error == ENOENT)
718                 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
719
720         strfree(dsname);
721         return (error);
722 }
723
724 int
725 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
726 {
727         char    parentname[MAXNAMELEN];
728         int     error;
729
730         if ((error = zfs_secpolicy_write_perms(from,
731             ZFS_DELEG_PERM_RENAME, cr)) != 0)
732                 return (error);
733
734         if ((error = zfs_secpolicy_write_perms(from,
735             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
736                 return (error);
737
738         if ((error = zfs_get_parent(to, parentname,
739             sizeof (parentname))) != 0)
740                 return (error);
741
742         if ((error = zfs_secpolicy_write_perms(parentname,
743             ZFS_DELEG_PERM_CREATE, cr)) != 0)
744                 return (error);
745
746         if ((error = zfs_secpolicy_write_perms(parentname,
747             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
748                 return (error);
749
750         return (error);
751 }
752
753 static int
754 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
755 {
756         return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
757 }
758
759 static int
760 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
761 {
762         char    parentname[MAXNAMELEN];
763         objset_t *clone;
764         int error;
765
766         error = zfs_secpolicy_write_perms(zc->zc_name,
767             ZFS_DELEG_PERM_PROMOTE, cr);
768         if (error)
769                 return (error);
770
771         error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
772
773         if (error == 0) {
774                 dsl_dataset_t *pclone = NULL;
775                 dsl_dir_t *dd;
776                 dd = clone->os_dsl_dataset->ds_dir;
777
778                 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
779                 error = dsl_dataset_hold_obj(dd->dd_pool,
780                     dd->dd_phys->dd_origin_obj, FTAG, &pclone);
781                 rw_exit(&dd->dd_pool->dp_config_rwlock);
782                 if (error) {
783                         dmu_objset_rele(clone, FTAG);
784                         return (error);
785                 }
786
787                 error = zfs_secpolicy_write_perms(zc->zc_name,
788                     ZFS_DELEG_PERM_MOUNT, cr);
789
790                 dsl_dataset_name(pclone, parentname);
791                 dmu_objset_rele(clone, FTAG);
792                 dsl_dataset_rele(pclone, FTAG);
793                 if (error == 0)
794                         error = zfs_secpolicy_write_perms(parentname,
795                             ZFS_DELEG_PERM_PROMOTE, cr);
796         }
797         return (error);
798 }
799
800 static int
801 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
802 {
803         int error;
804
805         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
806             ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
807                 return (error);
808
809         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
810             ZFS_DELEG_PERM_MOUNT, cr)) != 0)
811                 return (error);
812
813         return (zfs_secpolicy_write_perms(zc->zc_name,
814             ZFS_DELEG_PERM_CREATE, cr));
815 }
816
817 int
818 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
819 {
820         return (zfs_secpolicy_write_perms(name,
821             ZFS_DELEG_PERM_SNAPSHOT, cr));
822 }
823
824 static int
825 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
826 {
827
828         return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
829 }
830
831 static int
832 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
833 {
834         char    parentname[MAXNAMELEN];
835         int     error;
836
837         if ((error = zfs_get_parent(zc->zc_name, parentname,
838             sizeof (parentname))) != 0)
839                 return (error);
840
841         if (zc->zc_value[0] != '\0') {
842                 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
843                     ZFS_DELEG_PERM_CLONE, cr)) != 0)
844                         return (error);
845         }
846
847         if ((error = zfs_secpolicy_write_perms(parentname,
848             ZFS_DELEG_PERM_CREATE, cr)) != 0)
849                 return (error);
850
851         error = zfs_secpolicy_write_perms(parentname,
852             ZFS_DELEG_PERM_MOUNT, cr);
853
854         return (error);
855 }
856
857 /*
858  * Policy for pool operations - create/destroy pools, add vdevs, etc.  Requires
859  * SYS_CONFIG privilege, which is not available in a local zone.
860  */
861 /* ARGSUSED */
862 static int
863 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
864 {
865         if (secpolicy_sys_config(cr, B_FALSE) != 0)
866                 return (EPERM);
867
868         return (0);
869 }
870
871 /*
872  * Policy for object to name lookups.
873  */
874 /* ARGSUSED */
875 static int
876 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
877 {
878         int error;
879
880         if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
881                 return (0);
882
883         error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
884         return (error);
885 }
886
887 /*
888  * Policy for fault injection.  Requires all privileges.
889  */
890 /* ARGSUSED */
891 static int
892 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
893 {
894         return (secpolicy_zinject(cr));
895 }
896
897 static int
898 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
899 {
900         zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
901
902         if (prop == ZPROP_INVAL) {
903                 if (!zfs_prop_user(zc->zc_value))
904                         return (EINVAL);
905                 return (zfs_secpolicy_write_perms(zc->zc_name,
906                     ZFS_DELEG_PERM_USERPROP, cr));
907         } else {
908                 return (zfs_secpolicy_setprop(zc->zc_name, prop,
909                     NULL, cr));
910         }
911 }
912
913 static int
914 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
915 {
916         int err = zfs_secpolicy_read(zc, cr);
917         if (err)
918                 return (err);
919
920         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
921                 return (EINVAL);
922
923         if (zc->zc_value[0] == 0) {
924                 /*
925                  * They are asking about a posix uid/gid.  If it's
926                  * themself, allow it.
927                  */
928                 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
929                     zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
930                         if (zc->zc_guid == crgetuid(cr))
931                                 return (0);
932                 } else {
933                         if (groupmember(zc->zc_guid, cr))
934                                 return (0);
935                 }
936         }
937
938         return (zfs_secpolicy_write_perms(zc->zc_name,
939             userquota_perms[zc->zc_objset_type], cr));
940 }
941
942 static int
943 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
944 {
945         int err = zfs_secpolicy_read(zc, cr);
946         if (err)
947                 return (err);
948
949         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
950                 return (EINVAL);
951
952         return (zfs_secpolicy_write_perms(zc->zc_name,
953             userquota_perms[zc->zc_objset_type], cr));
954 }
955
956 static int
957 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
958 {
959         return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
960             NULL, cr));
961 }
962
963 static int
964 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
965 {
966         return (zfs_secpolicy_write_perms(zc->zc_name,
967             ZFS_DELEG_PERM_HOLD, cr));
968 }
969
970 static int
971 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
972 {
973         return (zfs_secpolicy_write_perms(zc->zc_name,
974             ZFS_DELEG_PERM_RELEASE, cr));
975 }
976
977 /*
978  * Policy for allowing temporary snapshots to be taken or released
979  */
980 static int
981 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
982 {
983         /*
984          * A temporary snapshot is the same as a snapshot,
985          * hold, destroy and release all rolled into one.
986          * Delegated diff alone is sufficient that we allow this.
987          */
988         int error;
989
990         if ((error = zfs_secpolicy_write_perms(zc->zc_name,
991             ZFS_DELEG_PERM_DIFF, cr)) == 0)
992                 return (0);
993
994         error = zfs_secpolicy_snapshot(zc, cr);
995         if (!error)
996                 error = zfs_secpolicy_hold(zc, cr);
997         if (!error)
998                 error = zfs_secpolicy_release(zc, cr);
999         if (!error)
1000                 error = zfs_secpolicy_destroy(zc, cr);
1001         return (error);
1002 }
1003
1004 /*
1005  * Returns the nvlist as specified by the user in the zfs_cmd_t.
1006  */
1007 static int
1008 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1009 {
1010         char *packed;
1011         int error;
1012         nvlist_t *list = NULL;
1013
1014         /*
1015          * Read in and unpack the user-supplied nvlist.
1016          */
1017         if (size == 0)
1018                 return (EINVAL);
1019
1020         packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1021
1022         if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1023             iflag)) != 0) {
1024                 kmem_free(packed, size);
1025                 return (error);
1026         }
1027
1028         if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1029                 kmem_free(packed, size);
1030                 return (error);
1031         }
1032
1033         kmem_free(packed, size);
1034
1035         *nvp = list;
1036         return (0);
1037 }
1038
1039 static int
1040 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1041 {
1042         size_t size;
1043
1044         VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1045
1046         if (size > zc->zc_nvlist_dst_size) {
1047                 nvpair_t *more_errors;
1048                 int n = 0;
1049
1050                 if (zc->zc_nvlist_dst_size < 1024)
1051                         return (ENOMEM);
1052
1053                 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1054                 more_errors = nvlist_prev_nvpair(*errors, NULL);
1055
1056                 do {
1057                         nvpair_t *pair = nvlist_prev_nvpair(*errors,
1058                             more_errors);
1059                         VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1060                         n++;
1061                         VERIFY(nvlist_size(*errors, &size,
1062                             NV_ENCODE_NATIVE) == 0);
1063                 } while (size > zc->zc_nvlist_dst_size);
1064
1065                 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1066                 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1067                 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1068                 ASSERT(size <= zc->zc_nvlist_dst_size);
1069         }
1070
1071         return (0);
1072 }
1073
1074 static int
1075 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1076 {
1077         char *packed = NULL;
1078         int error = 0;
1079         size_t size;
1080
1081         VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1082
1083         if (size > zc->zc_nvlist_dst_size) {
1084                 error = ENOMEM;
1085         } else {
1086                 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1087                 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1088                     KM_SLEEP) == 0);
1089                 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1090                     size, zc->zc_iflags) != 0)
1091                         error = EFAULT;
1092                 kmem_free(packed, size);
1093         }
1094
1095         zc->zc_nvlist_dst_size = size;
1096         return (error);
1097 }
1098
1099 static int
1100 get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
1101 {
1102         objset_t *os;
1103         int error;
1104
1105         error = dmu_objset_hold(dsname, FTAG, &os);
1106         if (error)
1107                 return (error);
1108         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1109                 dmu_objset_rele(os, FTAG);
1110                 return (EINVAL);
1111         }
1112
1113         mutex_enter(&os->os_user_ptr_lock);
1114         *zsbp = dmu_objset_get_user(os);
1115         if (*zsbp && (*zsbp)->z_sb) {
1116                 atomic_inc(&((*zsbp)->z_sb->s_active));
1117         } else {
1118                 error = ESRCH;
1119         }
1120         mutex_exit(&os->os_user_ptr_lock);
1121         dmu_objset_rele(os, FTAG);
1122         return (error);
1123 }
1124
1125 /*
1126  * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1127  * case its z_sb will be NULL, and it will be opened as the owner.
1128  */
1129 static int
1130 zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
1131 {
1132         int error = 0;
1133
1134         if (get_zfs_sb(name, zsbp) != 0)
1135                 error = zfs_sb_create(name, zsbp);
1136         if (error == 0) {
1137                 rrw_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
1138                     RW_READER, tag);
1139                 if ((*zsbp)->z_unmounted) {
1140                         /*
1141                          * XXX we could probably try again, since the unmounting
1142                          * thread should be just about to disassociate the
1143                          * objset from the zfsvfs.
1144                          */
1145                         rrw_exit(&(*zsbp)->z_teardown_lock, tag);
1146                         return (EBUSY);
1147                 }
1148         }
1149         return (error);
1150 }
1151
1152 static void
1153 zfs_sb_rele(zfs_sb_t *zsb, void *tag)
1154 {
1155         rrw_exit(&zsb->z_teardown_lock, tag);
1156
1157         if (zsb->z_sb) {
1158                 deactivate_super(zsb->z_sb);
1159         } else {
1160                 dmu_objset_disown(zsb->z_os, zsb);
1161                 zfs_sb_free(zsb);
1162         }
1163 }
1164
1165 static int
1166 zfs_ioc_pool_create(zfs_cmd_t *zc)
1167 {
1168         int error;
1169         nvlist_t *config, *props = NULL;
1170         nvlist_t *rootprops = NULL;
1171         nvlist_t *zplprops = NULL;
1172         char *buf;
1173
1174         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1175             zc->zc_iflags, &config)))
1176                 return (error);
1177
1178         if (zc->zc_nvlist_src_size != 0 && (error =
1179             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1180             zc->zc_iflags, &props))) {
1181                 nvlist_free(config);
1182                 return (error);
1183         }
1184
1185         if (props) {
1186                 nvlist_t *nvl = NULL;
1187                 uint64_t version = SPA_VERSION;
1188
1189                 (void) nvlist_lookup_uint64(props,
1190                     zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1191                 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1192                         error = EINVAL;
1193                         goto pool_props_bad;
1194                 }
1195                 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1196                 if (nvl) {
1197                         error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1198                         if (error != 0) {
1199                                 nvlist_free(config);
1200                                 nvlist_free(props);
1201                                 return (error);
1202                         }
1203                         (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1204                 }
1205                 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1206                 error = zfs_fill_zplprops_root(version, rootprops,
1207                     zplprops, NULL);
1208                 if (error)
1209                         goto pool_props_bad;
1210         }
1211
1212         buf = history_str_get(zc);
1213
1214         error = spa_create(zc->zc_name, config, props, buf, zplprops);
1215
1216         /*
1217          * Set the remaining root properties
1218          */
1219         if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1220             ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1221                 (void) spa_destroy(zc->zc_name);
1222
1223         if (buf != NULL)
1224                 history_str_free(buf);
1225
1226 pool_props_bad:
1227         nvlist_free(rootprops);
1228         nvlist_free(zplprops);
1229         nvlist_free(config);
1230         nvlist_free(props);
1231
1232         return (error);
1233 }
1234
1235 static int
1236 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1237 {
1238         int error;
1239         zfs_log_history(zc);
1240         error = spa_destroy(zc->zc_name);
1241         if (error == 0)
1242                 zvol_remove_minors(zc->zc_name);
1243         return (error);
1244 }
1245
1246 static int
1247 zfs_ioc_pool_import(zfs_cmd_t *zc)
1248 {
1249         nvlist_t *config, *props = NULL;
1250         uint64_t guid;
1251         int error;
1252
1253         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1254             zc->zc_iflags, &config)) != 0)
1255                 return (error);
1256
1257         if (zc->zc_nvlist_src_size != 0 && (error =
1258             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1259             zc->zc_iflags, &props))) {
1260                 nvlist_free(config);
1261                 return (error);
1262         }
1263
1264         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1265             guid != zc->zc_guid)
1266                 error = EINVAL;
1267         else
1268                 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1269
1270         if (zc->zc_nvlist_dst != 0) {
1271                 int err;
1272
1273                 if ((err = put_nvlist(zc, config)) != 0)
1274                         error = err;
1275         }
1276
1277         if (error == 0)
1278                 zvol_create_minors(zc->zc_name);
1279
1280         nvlist_free(config);
1281
1282         if (props)
1283                 nvlist_free(props);
1284
1285         return (error);
1286 }
1287
1288 static int
1289 zfs_ioc_pool_export(zfs_cmd_t *zc)
1290 {
1291         int error;
1292         boolean_t force = (boolean_t)zc->zc_cookie;
1293         boolean_t hardforce = (boolean_t)zc->zc_guid;
1294
1295         zfs_log_history(zc);
1296         error = spa_export(zc->zc_name, NULL, force, hardforce);
1297         if (error == 0)
1298                 zvol_remove_minors(zc->zc_name);
1299         return (error);
1300 }
1301
1302 static int
1303 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1304 {
1305         nvlist_t *configs;
1306         int error;
1307
1308         if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1309                 return (EEXIST);
1310
1311         error = put_nvlist(zc, configs);
1312
1313         nvlist_free(configs);
1314
1315         return (error);
1316 }
1317
1318 static int
1319 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1320 {
1321         nvlist_t *config;
1322         int error;
1323         int ret = 0;
1324
1325         error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1326             sizeof (zc->zc_value));
1327
1328         if (config != NULL) {
1329                 ret = put_nvlist(zc, config);
1330                 nvlist_free(config);
1331
1332                 /*
1333                  * The config may be present even if 'error' is non-zero.
1334                  * In this case we return success, and preserve the real errno
1335                  * in 'zc_cookie'.
1336                  */
1337                 zc->zc_cookie = error;
1338         } else {
1339                 ret = error;
1340         }
1341
1342         return (ret);
1343 }
1344
1345 /*
1346  * Try to import the given pool, returning pool stats as appropriate so that
1347  * user land knows which devices are available and overall pool health.
1348  */
1349 static int
1350 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1351 {
1352         nvlist_t *tryconfig, *config;
1353         int error;
1354
1355         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1356             zc->zc_iflags, &tryconfig)) != 0)
1357                 return (error);
1358
1359         config = spa_tryimport(tryconfig);
1360
1361         nvlist_free(tryconfig);
1362
1363         if (config == NULL)
1364                 return (EINVAL);
1365
1366         error = put_nvlist(zc, config);
1367         nvlist_free(config);
1368
1369         return (error);
1370 }
1371
1372 /*
1373  * inputs:
1374  * zc_name              name of the pool
1375  * zc_cookie            scan func (pool_scan_func_t)
1376  */
1377 static int
1378 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1379 {
1380         spa_t *spa;
1381         int error;
1382
1383         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1384                 return (error);
1385
1386         if (zc->zc_cookie == POOL_SCAN_NONE)
1387                 error = spa_scan_stop(spa);
1388         else
1389                 error = spa_scan(spa, zc->zc_cookie);
1390
1391         spa_close(spa, FTAG);
1392
1393         return (error);
1394 }
1395
1396 static int
1397 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1398 {
1399         spa_t *spa;
1400         int error;
1401
1402         error = spa_open(zc->zc_name, &spa, FTAG);
1403         if (error == 0) {
1404                 spa_freeze(spa);
1405                 spa_close(spa, FTAG);
1406         }
1407         return (error);
1408 }
1409
1410 static int
1411 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1412 {
1413         spa_t *spa;
1414         int error;
1415
1416         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1417                 return (error);
1418
1419         if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1420                 spa_close(spa, FTAG);
1421                 return (EINVAL);
1422         }
1423
1424         spa_upgrade(spa, zc->zc_cookie);
1425         spa_close(spa, FTAG);
1426
1427         return (error);
1428 }
1429
1430 static int
1431 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1432 {
1433         spa_t *spa;
1434         char *hist_buf;
1435         uint64_t size;
1436         int error;
1437
1438         if ((size = zc->zc_history_len) == 0)
1439                 return (EINVAL);
1440
1441         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1442                 return (error);
1443
1444         if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1445                 spa_close(spa, FTAG);
1446                 return (ENOTSUP);
1447         }
1448
1449         hist_buf = vmem_alloc(size, KM_SLEEP);
1450         if ((error = spa_history_get(spa, &zc->zc_history_offset,
1451             &zc->zc_history_len, hist_buf)) == 0) {
1452                 error = ddi_copyout(hist_buf,
1453                     (void *)(uintptr_t)zc->zc_history,
1454                     zc->zc_history_len, zc->zc_iflags);
1455         }
1456
1457         spa_close(spa, FTAG);
1458         vmem_free(hist_buf, size);
1459         return (error);
1460 }
1461
1462 static int
1463 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1464 {
1465         int error;
1466
1467         if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1468                 return (error);
1469
1470         return (0);
1471 }
1472
1473 /*
1474  * inputs:
1475  * zc_name              name of filesystem
1476  * zc_obj               object to find
1477  *
1478  * outputs:
1479  * zc_value             name of object
1480  */
1481 static int
1482 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1483 {
1484         objset_t *os;
1485         int error;
1486
1487         /* XXX reading from objset not owned */
1488         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1489                 return (error);
1490         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1491                 dmu_objset_rele(os, FTAG);
1492                 return (EINVAL);
1493         }
1494         error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1495             sizeof (zc->zc_value));
1496         dmu_objset_rele(os, FTAG);
1497
1498         return (error);
1499 }
1500
1501 /*
1502  * inputs:
1503  * zc_name              name of filesystem
1504  * zc_obj               object to find
1505  *
1506  * outputs:
1507  * zc_stat              stats on object
1508  * zc_value             path to object
1509  */
1510 static int
1511 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1512 {
1513         objset_t *os;
1514         int error;
1515
1516         /* XXX reading from objset not owned */
1517         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1518                 return (error);
1519         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1520                 dmu_objset_rele(os, FTAG);
1521                 return (EINVAL);
1522         }
1523         error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1524             sizeof (zc->zc_value));
1525         dmu_objset_rele(os, FTAG);
1526
1527         return (error);
1528 }
1529
1530 static int
1531 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1532 {
1533         spa_t *spa;
1534         int error;
1535         nvlist_t *config, **l2cache, **spares;
1536         uint_t nl2cache = 0, nspares = 0;
1537
1538         error = spa_open(zc->zc_name, &spa, FTAG);
1539         if (error != 0)
1540                 return (error);
1541
1542         error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1543             zc->zc_iflags, &config);
1544         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1545             &l2cache, &nl2cache);
1546
1547         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1548             &spares, &nspares);
1549
1550         /*
1551          * A root pool with concatenated devices is not supported.
1552          * Thus, can not add a device to a root pool.
1553          *
1554          * Intent log device can not be added to a rootpool because
1555          * during mountroot, zil is replayed, a seperated log device
1556          * can not be accessed during the mountroot time.
1557          *
1558          * l2cache and spare devices are ok to be added to a rootpool.
1559          */
1560         if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1561                 nvlist_free(config);
1562                 spa_close(spa, FTAG);
1563                 return (EDOM);
1564         }
1565
1566         if (error == 0) {
1567                 error = spa_vdev_add(spa, config);
1568                 nvlist_free(config);
1569         }
1570         spa_close(spa, FTAG);
1571         return (error);
1572 }
1573
1574 /*
1575  * inputs:
1576  * zc_name              name of the pool
1577  * zc_nvlist_conf       nvlist of devices to remove
1578  * zc_cookie            to stop the remove?
1579  */
1580 static int
1581 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1582 {
1583         spa_t *spa;
1584         int error;
1585
1586         error = spa_open(zc->zc_name, &spa, FTAG);
1587         if (error != 0)
1588                 return (error);
1589         error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1590         spa_close(spa, FTAG);
1591         return (error);
1592 }
1593
1594 static int
1595 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1596 {
1597         spa_t *spa;
1598         int error;
1599         vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1600
1601         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1602                 return (error);
1603         switch (zc->zc_cookie) {
1604         case VDEV_STATE_ONLINE:
1605                 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1606                 break;
1607
1608         case VDEV_STATE_OFFLINE:
1609                 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1610                 break;
1611
1612         case VDEV_STATE_FAULTED:
1613                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1614                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1615                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1616
1617                 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1618                 break;
1619
1620         case VDEV_STATE_DEGRADED:
1621                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1622                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1623                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1624
1625                 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1626                 break;
1627
1628         default:
1629                 error = EINVAL;
1630         }
1631         zc->zc_cookie = newstate;
1632         spa_close(spa, FTAG);
1633         return (error);
1634 }
1635
1636 static int
1637 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1638 {
1639         spa_t *spa;
1640         int replacing = zc->zc_cookie;
1641         nvlist_t *config;
1642         int error;
1643
1644         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1645                 return (error);
1646
1647         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1648             zc->zc_iflags, &config)) == 0) {
1649                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1650                 nvlist_free(config);
1651         }
1652
1653         spa_close(spa, FTAG);
1654         return (error);
1655 }
1656
1657 static int
1658 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1659 {
1660         spa_t *spa;
1661         int error;
1662
1663         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1664                 return (error);
1665
1666         error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1667
1668         spa_close(spa, FTAG);
1669         return (error);
1670 }
1671
1672 static int
1673 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1674 {
1675         spa_t *spa;
1676         nvlist_t *config, *props = NULL;
1677         int error;
1678         boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1679
1680         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1681                 return (error);
1682
1683         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1684             zc->zc_iflags, &config))) {
1685                 spa_close(spa, FTAG);
1686                 return (error);
1687         }
1688
1689         if (zc->zc_nvlist_src_size != 0 && (error =
1690             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1691             zc->zc_iflags, &props))) {
1692                 spa_close(spa, FTAG);
1693                 nvlist_free(config);
1694                 return (error);
1695         }
1696
1697         error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1698
1699         spa_close(spa, FTAG);
1700
1701         nvlist_free(config);
1702         nvlist_free(props);
1703
1704         return (error);
1705 }
1706
1707 static int
1708 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1709 {
1710         spa_t *spa;
1711         char *path = zc->zc_value;
1712         uint64_t guid = zc->zc_guid;
1713         int error;
1714
1715         error = spa_open(zc->zc_name, &spa, FTAG);
1716         if (error != 0)
1717                 return (error);
1718
1719         error = spa_vdev_setpath(spa, guid, path);
1720         spa_close(spa, FTAG);
1721         return (error);
1722 }
1723
1724 static int
1725 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1726 {
1727         spa_t *spa;
1728         char *fru = zc->zc_value;
1729         uint64_t guid = zc->zc_guid;
1730         int error;
1731
1732         error = spa_open(zc->zc_name, &spa, FTAG);
1733         if (error != 0)
1734                 return (error);
1735
1736         error = spa_vdev_setfru(spa, guid, fru);
1737         spa_close(spa, FTAG);
1738         return (error);
1739 }
1740
1741 static int
1742 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1743 {
1744         int error = 0;
1745         nvlist_t *nv;
1746
1747         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1748
1749         if (zc->zc_nvlist_dst != 0 &&
1750             (error = dsl_prop_get_all(os, &nv)) == 0) {
1751                 dmu_objset_stats(os, nv);
1752                 /*
1753                  * NB: zvol_get_stats() will read the objset contents,
1754                  * which we aren't supposed to do with a
1755                  * DS_MODE_USER hold, because it could be
1756                  * inconsistent.  So this is a bit of a workaround...
1757                  * XXX reading with out owning
1758                  */
1759                 if (!zc->zc_objset_stats.dds_inconsistent) {
1760                         if (dmu_objset_type(os) == DMU_OST_ZVOL)
1761                                 error = zvol_get_stats(os, nv);
1762                 }
1763                 if (error == 0)
1764                         error = put_nvlist(zc, nv);
1765                 nvlist_free(nv);
1766         }
1767
1768         return (error);
1769 }
1770
1771 /*
1772  * inputs:
1773  * zc_name              name of filesystem
1774  * zc_nvlist_dst_size   size of buffer for property nvlist
1775  *
1776  * outputs:
1777  * zc_objset_stats      stats
1778  * zc_nvlist_dst        property nvlist
1779  * zc_nvlist_dst_size   size of property nvlist
1780  */
1781 static int
1782 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1783 {
1784         objset_t *os = NULL;
1785         int error;
1786
1787         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1788                 return (error);
1789
1790         error = zfs_ioc_objset_stats_impl(zc, os);
1791
1792         dmu_objset_rele(os, FTAG);
1793
1794         return (error);
1795 }
1796
1797 /*
1798  * inputs:
1799  * zc_name              name of filesystem
1800  * zc_nvlist_dst_size   size of buffer for property nvlist
1801  *
1802  * outputs:
1803  * zc_nvlist_dst        received property nvlist
1804  * zc_nvlist_dst_size   size of received property nvlist
1805  *
1806  * Gets received properties (distinct from local properties on or after
1807  * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1808  * local property values.
1809  */
1810 static int
1811 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1812 {
1813         objset_t *os = NULL;
1814         int error;
1815         nvlist_t *nv;
1816
1817         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1818                 return (error);
1819
1820         /*
1821          * Without this check, we would return local property values if the
1822          * caller has not already received properties on or after
1823          * SPA_VERSION_RECVD_PROPS.
1824          */
1825         if (!dsl_prop_get_hasrecvd(os)) {
1826                 dmu_objset_rele(os, FTAG);
1827                 return (ENOTSUP);
1828         }
1829
1830         if (zc->zc_nvlist_dst != 0 &&
1831             (error = dsl_prop_get_received(os, &nv)) == 0) {
1832                 error = put_nvlist(zc, nv);
1833                 nvlist_free(nv);
1834         }
1835
1836         dmu_objset_rele(os, FTAG);
1837         return (error);
1838 }
1839
1840 static int
1841 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1842 {
1843         uint64_t value;
1844         int error;
1845
1846         /*
1847          * zfs_get_zplprop() will either find a value or give us
1848          * the default value (if there is one).
1849          */
1850         if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1851                 return (error);
1852         VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1853         return (0);
1854 }
1855
1856 /*
1857  * inputs:
1858  * zc_name              name of filesystem
1859  * zc_nvlist_dst_size   size of buffer for zpl property nvlist
1860  *
1861  * outputs:
1862  * zc_nvlist_dst        zpl property nvlist
1863  * zc_nvlist_dst_size   size of zpl property nvlist
1864  */
1865 static int
1866 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1867 {
1868         objset_t *os;
1869         int err;
1870
1871         /* XXX reading without owning */
1872         if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1873                 return (err);
1874
1875         dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1876
1877         /*
1878          * NB: nvl_add_zplprop() will read the objset contents,
1879          * which we aren't supposed to do with a DS_MODE_USER
1880          * hold, because it could be inconsistent.
1881          */
1882         if (zc->zc_nvlist_dst != 0 &&
1883             !zc->zc_objset_stats.dds_inconsistent &&
1884             dmu_objset_type(os) == DMU_OST_ZFS) {
1885                 nvlist_t *nv;
1886
1887                 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1888                 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1889                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1890                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1891                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1892                         err = put_nvlist(zc, nv);
1893                 nvlist_free(nv);
1894         } else {
1895                 err = ENOENT;
1896         }
1897         dmu_objset_rele(os, FTAG);
1898         return (err);
1899 }
1900
1901 static boolean_t
1902 dataset_name_hidden(const char *name)
1903 {
1904         /*
1905          * Skip over datasets that are not visible in this zone,
1906          * internal datasets (which have a $ in their name), and
1907          * temporary datasets (which have a % in their name).
1908          */
1909         if (strchr(name, '$') != NULL)
1910                 return (B_TRUE);
1911         if (strchr(name, '%') != NULL)
1912                 return (B_TRUE);
1913         if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1914                 return (B_TRUE);
1915         return (B_FALSE);
1916 }
1917
1918 /*
1919  * inputs:
1920  * zc_name              name of filesystem
1921  * zc_cookie            zap cursor
1922  * zc_nvlist_dst_size   size of buffer for property nvlist
1923  *
1924  * outputs:
1925  * zc_name              name of next filesystem
1926  * zc_cookie            zap cursor
1927  * zc_objset_stats      stats
1928  * zc_nvlist_dst        property nvlist
1929  * zc_nvlist_dst_size   size of property nvlist
1930  */
1931 static int
1932 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1933 {
1934         objset_t *os;
1935         int error;
1936         char *p;
1937         size_t orig_len = strlen(zc->zc_name);
1938
1939 top:
1940         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1941                 if (error == ENOENT)
1942                         error = ESRCH;
1943                 return (error);
1944         }
1945
1946         p = strrchr(zc->zc_name, '/');
1947         if (p == NULL || p[1] != '\0')
1948                 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1949         p = zc->zc_name + strlen(zc->zc_name);
1950
1951         /*
1952          * Pre-fetch the datasets.  dmu_objset_prefetch() always returns 0
1953          * but is not declared void because its called by dmu_objset_find().
1954          */
1955         if (zc->zc_cookie == 0) {
1956                 uint64_t cookie = 0;
1957                 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1958
1959                 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0)
1960                         (void) dmu_objset_prefetch(p, NULL);
1961         }
1962
1963         do {
1964                 error = dmu_dir_list_next(os,
1965                     sizeof (zc->zc_name) - (p - zc->zc_name), p,
1966                     NULL, &zc->zc_cookie);
1967                 if (error == ENOENT)
1968                         error = ESRCH;
1969         } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1970             !(zc->zc_iflags & FKIOCTL));
1971         dmu_objset_rele(os, FTAG);
1972
1973         /*
1974          * If it's an internal dataset (ie. with a '$' in its name),
1975          * don't try to get stats for it, otherwise we'll return ENOENT.
1976          */
1977         if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1978                 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1979                 if (error == ENOENT) {
1980                         /* We lost a race with destroy, get the next one. */
1981                         zc->zc_name[orig_len] = '\0';
1982                         goto top;
1983                 }
1984         }
1985         return (error);
1986 }
1987
1988 /*
1989  * inputs:
1990  * zc_name              name of filesystem
1991  * zc_cookie            zap cursor
1992  * zc_nvlist_dst_size   size of buffer for property nvlist
1993  *
1994  * outputs:
1995  * zc_name              name of next snapshot
1996  * zc_objset_stats      stats
1997  * zc_nvlist_dst        property nvlist
1998  * zc_nvlist_dst_size   size of property nvlist
1999  */
2000 static int
2001 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2002 {
2003         objset_t *os;
2004         int error;
2005
2006 top:
2007         if (zc->zc_cookie == 0)
2008                 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2009                     NULL, DS_FIND_SNAPSHOTS);
2010
2011         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2012         if (error)
2013                 return (error == ENOENT ? ESRCH : error);
2014
2015         /*
2016          * A dataset name of maximum length cannot have any snapshots,
2017          * so exit immediately.
2018          */
2019         if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2020                 dmu_objset_rele(os, FTAG);
2021                 return (ESRCH);
2022         }
2023
2024         error = dmu_snapshot_list_next(os,
2025             sizeof (zc->zc_name) - strlen(zc->zc_name),
2026             zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2027             NULL);
2028
2029         if (error == 0) {
2030                 dsl_dataset_t *ds;
2031                 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2032
2033                 /*
2034                  * Since we probably don't have a hold on this snapshot,
2035                  * it's possible that the objsetid could have been destroyed
2036                  * and reused for a new objset. It's OK if this happens during
2037                  * a zfs send operation, since the new createtxg will be
2038                  * beyond the range we're interested in.
2039                  */
2040                 rw_enter(&dp->dp_config_rwlock, RW_READER);
2041                 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2042                 rw_exit(&dp->dp_config_rwlock);
2043                 if (error) {
2044                         if (error == ENOENT) {
2045                                 /* Racing with destroy, get the next one. */
2046                                 *strchr(zc->zc_name, '@') = '\0';
2047                                 dmu_objset_rele(os, FTAG);
2048                                 goto top;
2049                         }
2050                 } else {
2051                         objset_t *ossnap;
2052
2053                         error = dmu_objset_from_ds(ds, &ossnap);
2054                         if (error == 0)
2055                                 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2056                         dsl_dataset_rele(ds, FTAG);
2057                 }
2058         } else if (error == ENOENT) {
2059                 error = ESRCH;
2060         }
2061
2062         dmu_objset_rele(os, FTAG);
2063         /* if we failed, undo the @ that we tacked on to zc_name */
2064         if (error)
2065                 *strchr(zc->zc_name, '@') = '\0';
2066         return (error);
2067 }
2068
2069 static int
2070 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2071 {
2072         const char *propname = nvpair_name(pair);
2073         uint64_t *valary;
2074         unsigned int vallen;
2075         const char *domain;
2076         char *dash;
2077         zfs_userquota_prop_t type;
2078         uint64_t rid;
2079         uint64_t quota;
2080         zfs_sb_t *zsb;
2081         int err;
2082
2083         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2084                 nvlist_t *attrs;
2085                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2086                 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2087                     &pair) != 0)
2088                         return (EINVAL);
2089         }
2090
2091         /*
2092          * A correctly constructed propname is encoded as
2093          * userquota@<rid>-<domain>.
2094          */
2095         if ((dash = strchr(propname, '-')) == NULL ||
2096             nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2097             vallen != 3)
2098                 return (EINVAL);
2099
2100         domain = dash + 1;
2101         type = valary[0];
2102         rid = valary[1];
2103         quota = valary[2];
2104
2105         err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2106         if (err == 0) {
2107                 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2108                 zfs_sb_rele(zsb, FTAG);
2109         }
2110
2111         return (err);
2112 }
2113
2114 /*
2115  * If the named property is one that has a special function to set its value,
2116  * return 0 on success and a positive error code on failure; otherwise if it is
2117  * not one of the special properties handled by this function, return -1.
2118  *
2119  * XXX: It would be better for callers of the property interface if we handled
2120  * these special cases in dsl_prop.c (in the dsl layer).
2121  */
2122 static int
2123 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2124     nvpair_t *pair)
2125 {
2126         const char *propname = nvpair_name(pair);
2127         zfs_prop_t prop = zfs_name_to_prop(propname);
2128         uint64_t intval;
2129         int err;
2130
2131         if (prop == ZPROP_INVAL) {
2132                 if (zfs_prop_userquota(propname))
2133                         return (zfs_prop_set_userquota(dsname, pair));
2134                 return (-1);
2135         }
2136
2137         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2138                 nvlist_t *attrs;
2139                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2140                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2141                     &pair) == 0);
2142         }
2143
2144         if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2145                 return (-1);
2146
2147         VERIFY(0 == nvpair_value_uint64(pair, &intval));
2148
2149         switch (prop) {
2150         case ZFS_PROP_QUOTA:
2151                 err = dsl_dir_set_quota(dsname, source, intval);
2152                 break;
2153         case ZFS_PROP_REFQUOTA:
2154                 err = dsl_dataset_set_quota(dsname, source, intval);
2155                 break;
2156         case ZFS_PROP_RESERVATION:
2157                 err = dsl_dir_set_reservation(dsname, source, intval);
2158                 break;
2159         case ZFS_PROP_REFRESERVATION:
2160                 err = dsl_dataset_set_reservation(dsname, source, intval);
2161                 break;
2162         case ZFS_PROP_VOLSIZE:
2163                 err = zvol_set_volsize(dsname, intval);
2164                 break;
2165         case ZFS_PROP_VERSION:
2166         {
2167                 zfs_sb_t *zsb;
2168
2169                 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2170                         break;
2171
2172                 err = zfs_set_version(zsb, intval);
2173                 zfs_sb_rele(zsb, FTAG);
2174
2175                 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2176                         zfs_cmd_t *zc;
2177
2178                         zc = kmem_zalloc(sizeof (zfs_cmd_t),
2179                             KM_SLEEP | KM_NODEBUG);
2180                         (void) strcpy(zc->zc_name, dsname);
2181                         (void) zfs_ioc_userspace_upgrade(zc);
2182                         kmem_free(zc, sizeof (zfs_cmd_t));
2183                 }
2184                 break;
2185         }
2186
2187         default:
2188                 err = -1;
2189         }
2190
2191         return (err);
2192 }
2193
2194 /*
2195  * This function is best effort. If it fails to set any of the given properties,
2196  * it continues to set as many as it can and returns the first error
2197  * encountered. If the caller provides a non-NULL errlist, it also gives the
2198  * complete list of names of all the properties it failed to set along with the
2199  * corresponding error numbers. The caller is responsible for freeing the
2200  * returned errlist.
2201  *
2202  * If every property is set successfully, zero is returned and the list pointed
2203  * at by errlist is NULL.
2204  */
2205 int
2206 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2207     nvlist_t **errlist)
2208 {
2209         nvpair_t *pair;
2210         nvpair_t *propval;
2211         int rv = 0;
2212         uint64_t intval;
2213         char *strval;
2214         nvlist_t *genericnvl;
2215         nvlist_t *errors;
2216         nvlist_t *retrynvl;
2217
2218         VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2219         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2220         VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2221
2222 retry:
2223         pair = NULL;
2224         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2225                 const char *propname = nvpair_name(pair);
2226                 zfs_prop_t prop = zfs_name_to_prop(propname);
2227                 int err = 0;
2228
2229                 /* decode the property value */
2230                 propval = pair;
2231                 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2232                         nvlist_t *attrs;
2233                         VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2234                         if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2235                             &propval) != 0)
2236                                 err = EINVAL;
2237                 }
2238
2239                 /* Validate value type */
2240                 if (err == 0 && prop == ZPROP_INVAL) {
2241                         if (zfs_prop_user(propname)) {
2242                                 if (nvpair_type(propval) != DATA_TYPE_STRING)
2243                                         err = EINVAL;
2244                         } else if (zfs_prop_userquota(propname)) {
2245                                 if (nvpair_type(propval) !=
2246                                     DATA_TYPE_UINT64_ARRAY)
2247                                         err = EINVAL;
2248                         }
2249                 } else if (err == 0) {
2250                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
2251                                 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2252                                         err = EINVAL;
2253                         } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2254                                 const char *unused;
2255
2256                                 VERIFY(nvpair_value_uint64(propval,
2257                                     &intval) == 0);
2258
2259                                 switch (zfs_prop_get_type(prop)) {
2260                                 case PROP_TYPE_NUMBER:
2261                                         break;
2262                                 case PROP_TYPE_STRING:
2263                                         err = EINVAL;
2264                                         break;
2265                                 case PROP_TYPE_INDEX:
2266                                         if (zfs_prop_index_to_string(prop,
2267                                             intval, &unused) != 0)
2268                                                 err = EINVAL;
2269                                         break;
2270                                 default:
2271                                         cmn_err(CE_PANIC,
2272                                             "unknown property type");
2273                                 }
2274                         } else {
2275                                 err = EINVAL;
2276                         }
2277                 }
2278
2279                 /* Validate permissions */
2280                 if (err == 0)
2281                         err = zfs_check_settable(dsname, pair, CRED());
2282
2283                 if (err == 0) {
2284                         err = zfs_prop_set_special(dsname, source, pair);
2285                         if (err == -1) {
2286                                 /*
2287                                  * For better performance we build up a list of
2288                                  * properties to set in a single transaction.
2289                                  */
2290                                 err = nvlist_add_nvpair(genericnvl, pair);
2291                         } else if (err != 0 && nvl != retrynvl) {
2292                                 /*
2293                                  * This may be a spurious error caused by
2294                                  * receiving quota and reservation out of order.
2295                                  * Try again in a second pass.
2296                                  */
2297                                 err = nvlist_add_nvpair(retrynvl, pair);
2298                         }
2299                 }
2300
2301                 if (err != 0)
2302                         VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2303         }
2304
2305         if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2306                 nvl = retrynvl;
2307                 goto retry;
2308         }
2309
2310         if (!nvlist_empty(genericnvl) &&
2311             dsl_props_set(dsname, source, genericnvl) != 0) {
2312                 /*
2313                  * If this fails, we still want to set as many properties as we
2314                  * can, so try setting them individually.
2315                  */
2316                 pair = NULL;
2317                 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2318                         const char *propname = nvpair_name(pair);
2319                         int err = 0;
2320
2321                         propval = pair;
2322                         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2323                                 nvlist_t *attrs;
2324                                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2325                                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2326                                     &propval) == 0);
2327                         }
2328
2329                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
2330                                 VERIFY(nvpair_value_string(propval,
2331                                     &strval) == 0);
2332                                 err = dsl_prop_set(dsname, propname, source, 1,
2333                                     strlen(strval) + 1, strval);
2334                         } else {
2335                                 VERIFY(nvpair_value_uint64(propval,
2336                                     &intval) == 0);
2337                                 err = dsl_prop_set(dsname, propname, source, 8,
2338                                     1, &intval);
2339                         }
2340
2341                         if (err != 0) {
2342                                 VERIFY(nvlist_add_int32(errors, propname,
2343                                     err) == 0);
2344                         }
2345                 }
2346         }
2347         nvlist_free(genericnvl);
2348         nvlist_free(retrynvl);
2349
2350         if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2351                 nvlist_free(errors);
2352                 errors = NULL;
2353         } else {
2354                 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2355         }
2356
2357         if (errlist == NULL)
2358                 nvlist_free(errors);
2359         else
2360                 *errlist = errors;
2361
2362         return (rv);
2363 }
2364
2365 /*
2366  * Check that all the properties are valid user properties.
2367  */
2368 static int
2369 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2370 {
2371         nvpair_t *pair = NULL;
2372         int error = 0;
2373
2374         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2375                 const char *propname = nvpair_name(pair);
2376                 char *valstr;
2377
2378                 if (!zfs_prop_user(propname) ||
2379                     nvpair_type(pair) != DATA_TYPE_STRING)
2380                         return (EINVAL);
2381
2382                 if ((error = zfs_secpolicy_write_perms(fsname,
2383                     ZFS_DELEG_PERM_USERPROP, CRED())))
2384                         return (error);
2385
2386                 if (strlen(propname) >= ZAP_MAXNAMELEN)
2387                         return (ENAMETOOLONG);
2388
2389                 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2390                 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2391                         return (E2BIG);
2392         }
2393         return (0);
2394 }
2395
2396 static void
2397 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2398 {
2399         nvpair_t *pair;
2400
2401         VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2402
2403         pair = NULL;
2404         while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2405                 if (nvlist_exists(skipped, nvpair_name(pair)))
2406                         continue;
2407
2408                 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2409         }
2410 }
2411
2412 static int
2413 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2414     nvlist_t *skipped)
2415 {
2416         int err = 0;
2417         nvlist_t *cleared_props = NULL;
2418         props_skip(props, skipped, &cleared_props);
2419         if (!nvlist_empty(cleared_props)) {
2420                 /*
2421                  * Acts on local properties until the dataset has received
2422                  * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2423                  */
2424                 zprop_source_t flags = (ZPROP_SRC_NONE |
2425                     (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2426                 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2427         }
2428         nvlist_free(cleared_props);
2429         return (err);
2430 }
2431
2432 /*
2433  * inputs:
2434  * zc_name              name of filesystem
2435  * zc_value             name of property to set
2436  * zc_nvlist_src{_size} nvlist of properties to apply
2437  * zc_cookie            received properties flag
2438  *
2439  * outputs:
2440  * zc_nvlist_dst{_size} error for each unapplied received property
2441  */
2442 static int
2443 zfs_ioc_set_prop(zfs_cmd_t *zc)
2444 {
2445         nvlist_t *nvl;
2446         boolean_t received = zc->zc_cookie;
2447         zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2448             ZPROP_SRC_LOCAL);
2449         nvlist_t *errors = NULL;
2450         int error;
2451
2452         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2453             zc->zc_iflags, &nvl)) != 0)
2454                 return (error);
2455
2456         if (received) {
2457                 nvlist_t *origprops;
2458                 objset_t *os;
2459
2460                 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2461                         if (dsl_prop_get_received(os, &origprops) == 0) {
2462                                 (void) clear_received_props(os,
2463                                     zc->zc_name, origprops, nvl);
2464                                 nvlist_free(origprops);
2465                         }
2466
2467                         dsl_prop_set_hasrecvd(os);
2468                         dmu_objset_rele(os, FTAG);
2469                 }
2470         }
2471
2472         error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2473
2474         if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2475                 (void) put_nvlist(zc, errors);
2476         }
2477
2478         nvlist_free(errors);
2479         nvlist_free(nvl);
2480         return (error);
2481 }
2482
2483 /*
2484  * inputs:
2485  * zc_name              name of filesystem
2486  * zc_value             name of property to inherit
2487  * zc_cookie            revert to received value if TRUE
2488  *
2489  * outputs:             none
2490  */
2491 static int
2492 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2493 {
2494         const char *propname = zc->zc_value;
2495         zfs_prop_t prop = zfs_name_to_prop(propname);
2496         boolean_t received = zc->zc_cookie;
2497         zprop_source_t source = (received
2498             ? ZPROP_SRC_NONE            /* revert to received value, if any */
2499             : ZPROP_SRC_INHERITED);     /* explicitly inherit */
2500
2501         if (received) {
2502                 nvlist_t *dummy;
2503                 nvpair_t *pair;
2504                 zprop_type_t type;
2505                 int err;
2506
2507                 /*
2508                  * zfs_prop_set_special() expects properties in the form of an
2509                  * nvpair with type info.
2510                  */
2511                 if (prop == ZPROP_INVAL) {
2512                         if (!zfs_prop_user(propname))
2513                                 return (EINVAL);
2514
2515                         type = PROP_TYPE_STRING;
2516                 } else if (prop == ZFS_PROP_VOLSIZE ||
2517                     prop == ZFS_PROP_VERSION) {
2518                         return (EINVAL);
2519                 } else {
2520                         type = zfs_prop_get_type(prop);
2521                 }
2522
2523                 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2524
2525                 switch (type) {
2526                 case PROP_TYPE_STRING:
2527                         VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2528                         break;
2529                 case PROP_TYPE_NUMBER:
2530                 case PROP_TYPE_INDEX:
2531                         VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2532                         break;
2533                 default:
2534                         nvlist_free(dummy);
2535                         return (EINVAL);
2536                 }
2537
2538                 pair = nvlist_next_nvpair(dummy, NULL);
2539                 err = zfs_prop_set_special(zc->zc_name, source, pair);
2540                 nvlist_free(dummy);
2541                 if (err != -1)
2542                         return (err); /* special property already handled */
2543         } else {
2544                 /*
2545                  * Only check this in the non-received case. We want to allow
2546                  * 'inherit -S' to revert non-inheritable properties like quota
2547                  * and reservation to the received or default values even though
2548                  * they are not considered inheritable.
2549                  */
2550                 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2551                         return (EINVAL);
2552         }
2553
2554         /* the property name has been validated by zfs_secpolicy_inherit() */
2555         return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2556 }
2557
2558 static int
2559 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2560 {
2561         nvlist_t *props;
2562         spa_t *spa;
2563         int error;
2564         nvpair_t *pair;
2565
2566         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2567             zc->zc_iflags, &props)))
2568                 return (error);
2569
2570         /*
2571          * If the only property is the configfile, then just do a spa_lookup()
2572          * to handle the faulted case.
2573          */
2574         pair = nvlist_next_nvpair(props, NULL);
2575         if (pair != NULL && strcmp(nvpair_name(pair),
2576             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2577             nvlist_next_nvpair(props, pair) == NULL) {
2578                 mutex_enter(&spa_namespace_lock);
2579                 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2580                         spa_configfile_set(spa, props, B_FALSE);
2581                         spa_config_sync(spa, B_FALSE, B_TRUE);
2582                 }
2583                 mutex_exit(&spa_namespace_lock);
2584                 if (spa != NULL) {
2585                         nvlist_free(props);
2586                         return (0);
2587                 }
2588         }
2589
2590         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2591                 nvlist_free(props);
2592                 return (error);
2593         }
2594
2595         error = spa_prop_set(spa, props);
2596
2597         nvlist_free(props);
2598         spa_close(spa, FTAG);
2599
2600         return (error);
2601 }
2602
2603 static int
2604 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2605 {
2606         spa_t *spa;
2607         int error;
2608         nvlist_t *nvp = NULL;
2609
2610         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2611                 /*
2612                  * If the pool is faulted, there may be properties we can still
2613                  * get (such as altroot and cachefile), so attempt to get them
2614                  * anyway.
2615                  */
2616                 mutex_enter(&spa_namespace_lock);
2617                 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2618                         error = spa_prop_get(spa, &nvp);
2619                 mutex_exit(&spa_namespace_lock);
2620         } else {
2621                 error = spa_prop_get(spa, &nvp);
2622                 spa_close(spa, FTAG);
2623         }
2624
2625         if (error == 0 && zc->zc_nvlist_dst != 0)
2626                 error = put_nvlist(zc, nvp);
2627         else
2628                 error = EFAULT;
2629
2630         nvlist_free(nvp);
2631         return (error);
2632 }
2633
2634 /*
2635  * inputs:
2636  * zc_name              name of volume
2637  *
2638  * outputs:             none
2639  */
2640 static int
2641 zfs_ioc_create_minor(zfs_cmd_t *zc)
2642 {
2643         return (zvol_create_minor(zc->zc_name));
2644 }
2645
2646 /*
2647  * inputs:
2648  * zc_name              name of volume
2649  *
2650  * outputs:             none
2651  */
2652 static int
2653 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2654 {
2655         return (zvol_remove_minor(zc->zc_name));
2656 }
2657
2658 /*
2659  * inputs:
2660  * zc_name              name of filesystem
2661  * zc_nvlist_src{_size} nvlist of delegated permissions
2662  * zc_perm_action       allow/unallow flag
2663  *
2664  * outputs:             none
2665  */
2666 static int
2667 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2668 {
2669         int error;
2670         nvlist_t *fsaclnv = NULL;
2671
2672         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2673             zc->zc_iflags, &fsaclnv)) != 0)
2674                 return (error);
2675
2676         /*
2677          * Verify nvlist is constructed correctly
2678          */
2679         if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2680                 nvlist_free(fsaclnv);
2681                 return (EINVAL);
2682         }
2683
2684         /*
2685          * If we don't have PRIV_SYS_MOUNT, then validate
2686          * that user is allowed to hand out each permission in
2687          * the nvlist(s)
2688          */
2689
2690         error = secpolicy_zfs(CRED());
2691         if (error) {
2692                 if (zc->zc_perm_action == B_FALSE) {
2693                         error = dsl_deleg_can_allow(zc->zc_name,
2694                             fsaclnv, CRED());
2695                 } else {
2696                         error = dsl_deleg_can_unallow(zc->zc_name,
2697                             fsaclnv, CRED());
2698                 }
2699         }
2700
2701         if (error == 0)
2702                 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2703
2704         nvlist_free(fsaclnv);
2705         return (error);
2706 }
2707
2708 /*
2709  * inputs:
2710  * zc_name              name of filesystem
2711  *
2712  * outputs:
2713  * zc_nvlist_src{_size} nvlist of delegated permissions
2714  */
2715 static int
2716 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2717 {
2718         nvlist_t *nvp;
2719         int error;
2720
2721         if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2722                 error = put_nvlist(zc, nvp);
2723                 nvlist_free(nvp);
2724         }
2725
2726         return (error);
2727 }
2728
2729 #ifdef HAVE_SNAPSHOT
2730 /*
2731  * Search the vfs list for a specified resource.  Returns a pointer to it
2732  * or NULL if no suitable entry is found. The caller of this routine
2733  * is responsible for releasing the returned vfs pointer.
2734  */
2735 static vfs_t *
2736 zfs_get_vfs(const char *resource)
2737 {
2738         struct vfs *vfsp;
2739         struct vfs *vfs_found = NULL;
2740
2741         vfs_list_read_lock();
2742         vfsp = rootvfs;
2743         do {
2744                 if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) {
2745                         mntget(vfsp);
2746                         vfs_found = vfsp;
2747                         break;
2748                 }
2749                 vfsp = vfsp->vfs_next;
2750         } while (vfsp != rootvfs);
2751         vfs_list_unlock();
2752         return (vfs_found);
2753 }
2754 #endif /* HAVE_SNAPSHOT */
2755
2756 /* ARGSUSED */
2757 static void
2758 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2759 {
2760         zfs_creat_t *zct = arg;
2761
2762         zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2763 }
2764
2765 #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
2766
2767 /*
2768  * inputs:
2769  * createprops          list of properties requested by creator
2770  * default_zplver       zpl version to use if unspecified in createprops
2771  * fuids_ok             fuids allowed in this version of the spa?
2772  * os                   parent objset pointer (NULL if root fs)
2773  *
2774  * outputs:
2775  * zplprops     values for the zplprops we attach to the master node object
2776  * is_ci        true if requested file system will be purely case-insensitive
2777  *
2778  * Determine the settings for utf8only, normalization and
2779  * casesensitivity.  Specific values may have been requested by the
2780  * creator and/or we can inherit values from the parent dataset.  If
2781  * the file system is of too early a vintage, a creator can not
2782  * request settings for these properties, even if the requested
2783  * setting is the default value.  We don't actually want to create dsl
2784  * properties for these, so remove them from the source nvlist after
2785  * processing.
2786  */
2787 static int
2788 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2789     boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2790     nvlist_t *zplprops, boolean_t *is_ci)
2791 {
2792         uint64_t sense = ZFS_PROP_UNDEFINED;
2793         uint64_t norm = ZFS_PROP_UNDEFINED;
2794         uint64_t u8 = ZFS_PROP_UNDEFINED;
2795
2796         ASSERT(zplprops != NULL);
2797
2798         /*
2799          * Pull out creator prop choices, if any.
2800          */
2801         if (createprops) {
2802                 (void) nvlist_lookup_uint64(createprops,
2803                     zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2804                 (void) nvlist_lookup_uint64(createprops,
2805                     zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2806                 (void) nvlist_remove_all(createprops,
2807                     zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2808                 (void) nvlist_lookup_uint64(createprops,
2809                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2810                 (void) nvlist_remove_all(createprops,
2811                     zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2812                 (void) nvlist_lookup_uint64(createprops,
2813                     zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2814                 (void) nvlist_remove_all(createprops,
2815                     zfs_prop_to_name(ZFS_PROP_CASE));
2816         }
2817
2818         /*
2819          * If the zpl version requested is whacky or the file system
2820          * or pool is version is too "young" to support normalization
2821          * and the creator tried to set a value for one of the props,
2822          * error out.
2823          */
2824         if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2825             (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2826             (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2827             (zplver < ZPL_VERSION_NORMALIZATION &&
2828             (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2829             sense != ZFS_PROP_UNDEFINED)))
2830                 return (ENOTSUP);
2831
2832         /*
2833          * Put the version in the zplprops
2834          */
2835         VERIFY(nvlist_add_uint64(zplprops,
2836             zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2837
2838         if (norm == ZFS_PROP_UNDEFINED)
2839                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
2840         VERIFY(nvlist_add_uint64(zplprops,
2841             zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2842
2843         /*
2844          * If we're normalizing, names must always be valid UTF-8 strings.
2845          */
2846         if (norm)
2847                 u8 = 1;
2848         if (u8 == ZFS_PROP_UNDEFINED)
2849                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
2850         VERIFY(nvlist_add_uint64(zplprops,
2851             zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2852
2853         if (sense == ZFS_PROP_UNDEFINED)
2854                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
2855         VERIFY(nvlist_add_uint64(zplprops,
2856             zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2857
2858         if (is_ci)
2859                 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2860
2861         return (0);
2862 }
2863
2864 static int
2865 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2866     nvlist_t *zplprops, boolean_t *is_ci)
2867 {
2868         boolean_t fuids_ok, sa_ok;
2869         uint64_t zplver = ZPL_VERSION;
2870         objset_t *os = NULL;
2871         char parentname[MAXNAMELEN];
2872         char *cp;
2873         spa_t *spa;
2874         uint64_t spa_vers;
2875         int error;
2876
2877         (void) strlcpy(parentname, dataset, sizeof (parentname));
2878         cp = strrchr(parentname, '/');
2879         ASSERT(cp != NULL);
2880         cp[0] = '\0';
2881
2882         if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2883                 return (error);
2884
2885         spa_vers = spa_version(spa);
2886         spa_close(spa, FTAG);
2887
2888         zplver = zfs_zpl_version_map(spa_vers);
2889         fuids_ok = (zplver >= ZPL_VERSION_FUID);
2890         sa_ok = (zplver >= ZPL_VERSION_SA);
2891
2892         /*
2893          * Open parent object set so we can inherit zplprop values.
2894          */
2895         if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2896                 return (error);
2897
2898         error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2899             zplprops, is_ci);
2900         dmu_objset_rele(os, FTAG);
2901         return (error);
2902 }
2903
2904 static int
2905 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2906     nvlist_t *zplprops, boolean_t *is_ci)
2907 {
2908         boolean_t fuids_ok;
2909         boolean_t sa_ok;
2910         uint64_t zplver = ZPL_VERSION;
2911         int error;
2912
2913         zplver = zfs_zpl_version_map(spa_vers);
2914         fuids_ok = (zplver >= ZPL_VERSION_FUID);
2915         sa_ok = (zplver >= ZPL_VERSION_SA);
2916
2917         error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2918             createprops, zplprops, is_ci);
2919         return (error);
2920 }
2921
2922 /*
2923  * inputs:
2924  * zc_objset_type       type of objset to create (fs vs zvol)
2925  * zc_name              name of new objset
2926  * zc_value             name of snapshot to clone from (may be empty)
2927  * zc_nvlist_src{_size} nvlist of properties to apply
2928  *
2929  * outputs: none
2930  */
2931 static int
2932 zfs_ioc_create(zfs_cmd_t *zc)
2933 {
2934         objset_t *clone;
2935         int error = 0;
2936         zfs_creat_t zct;
2937         nvlist_t *nvprops = NULL;
2938         void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2939         dmu_objset_type_t type = zc->zc_objset_type;
2940
2941         switch (type) {
2942
2943         case DMU_OST_ZFS:
2944                 cbfunc = zfs_create_cb;
2945                 break;
2946
2947         case DMU_OST_ZVOL:
2948                 cbfunc = zvol_create_cb;
2949                 break;
2950
2951         default:
2952                 cbfunc = NULL;
2953                 break;
2954         }
2955         if (strchr(zc->zc_name, '@') ||
2956             strchr(zc->zc_name, '%'))
2957                 return (EINVAL);
2958
2959         if (zc->zc_nvlist_src != 0 &&
2960             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2961             zc->zc_iflags, &nvprops)) != 0)
2962                 return (error);
2963
2964         zct.zct_zplprops = NULL;
2965         zct.zct_props = nvprops;
2966
2967         if (zc->zc_value[0] != '\0') {
2968                 /*
2969                  * We're creating a clone of an existing snapshot.
2970                  */
2971                 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2972                 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2973                         nvlist_free(nvprops);
2974                         return (EINVAL);
2975                 }
2976
2977                 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2978                 if (error) {
2979                         nvlist_free(nvprops);
2980                         return (error);
2981                 }
2982
2983                 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2984                 dmu_objset_rele(clone, FTAG);
2985                 if (error) {
2986                         nvlist_free(nvprops);
2987                         return (error);
2988                 }
2989         } else {
2990                 boolean_t is_insensitive = B_FALSE;
2991
2992                 if (cbfunc == NULL) {
2993                         nvlist_free(nvprops);
2994                         return (EINVAL);
2995                 }
2996
2997                 if (type == DMU_OST_ZVOL) {
2998                         uint64_t volsize, volblocksize;
2999
3000                         if (nvprops == NULL ||
3001                             nvlist_lookup_uint64(nvprops,
3002                             zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3003                             &volsize) != 0) {
3004                                 nvlist_free(nvprops);
3005                                 return (EINVAL);
3006                         }
3007
3008                         if ((error = nvlist_lookup_uint64(nvprops,
3009                             zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3010                             &volblocksize)) != 0 && error != ENOENT) {
3011                                 nvlist_free(nvprops);
3012                                 return (EINVAL);
3013                         }
3014
3015                         if (error != 0)
3016                                 volblocksize = zfs_prop_default_numeric(
3017                                     ZFS_PROP_VOLBLOCKSIZE);
3018
3019                         if ((error = zvol_check_volblocksize(
3020                             volblocksize)) != 0 ||
3021                             (error = zvol_check_volsize(volsize,
3022                             volblocksize)) != 0) {
3023                                 nvlist_free(nvprops);
3024                                 return (error);
3025                         }
3026                 } else if (type == DMU_OST_ZFS) {
3027                         int error;
3028
3029                         /*
3030                          * We have to have normalization and
3031                          * case-folding flags correct when we do the
3032                          * file system creation, so go figure them out
3033                          * now.
3034                          */
3035                         VERIFY(nvlist_alloc(&zct.zct_zplprops,
3036                             NV_UNIQUE_NAME, KM_SLEEP) == 0);
3037                         error = zfs_fill_zplprops(zc->zc_name, nvprops,
3038                             zct.zct_zplprops, &is_insensitive);
3039                         if (error != 0) {
3040                                 nvlist_free(nvprops);
3041                                 nvlist_free(zct.zct_zplprops);
3042                                 return (error);
3043                         }
3044                 }
3045                 error = dmu_objset_create(zc->zc_name, type,
3046                     is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3047                 nvlist_free(zct.zct_zplprops);
3048         }
3049
3050         /*
3051          * It would be nice to do this atomically.
3052          */
3053         if (error == 0) {
3054                 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3055                     nvprops, NULL);
3056                 if (error != 0)
3057                         (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3058         }
3059         nvlist_free(nvprops);
3060         return (error);
3061 }
3062
3063 /*
3064  * inputs:
3065  * zc_name      name of filesystem
3066  * zc_value     short name of snapshot
3067  * zc_cookie    recursive flag
3068  * zc_nvlist_src[_size] property list
3069  *
3070  * outputs:
3071  * zc_value     short snapname (i.e. part after the '@')
3072  */
3073 static int
3074 zfs_ioc_snapshot(zfs_cmd_t *zc)
3075 {
3076         nvlist_t *nvprops = NULL;
3077         int error;
3078         boolean_t recursive = zc->zc_cookie;
3079
3080         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3081                 return (EINVAL);
3082
3083         if (zc->zc_nvlist_src != 0 &&
3084             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3085             zc->zc_iflags, &nvprops)) != 0)
3086                 return (error);
3087
3088         error = zfs_check_userprops(zc->zc_name, nvprops);
3089         if (error)
3090                 goto out;
3091
3092         if (!nvlist_empty(nvprops) &&
3093             zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3094                 error = ENOTSUP;
3095                 goto out;
3096         }
3097
3098         error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3099             nvprops, recursive, B_FALSE, -1);
3100
3101 out:
3102         nvlist_free(nvprops);
3103         return (error);
3104 }
3105
3106 int
3107 zfs_unmount_snap(const char *name, void *arg)
3108 {
3109 #ifdef HAVE_SNAPSHOT
3110         vfs_t *vfsp = NULL;
3111
3112         if (arg) {
3113                 char *snapname = arg;
3114                 char *fullname = kmem_asprintf("%s@%s", name, snapname);
3115                 vfsp = zfs_get_vfs(fullname);
3116                 strfree(fullname);
3117         } else if (strchr(name, '@')) {
3118                 vfsp = zfs_get_vfs(name);
3119         }
3120
3121         if (vfsp) {
3122                 /*
3123                  * Always force the unmount for snapshots.
3124                  */
3125                 int flag = MS_FORCE;
3126                 int err;
3127
3128                 if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) {
3129                         mntput(vfsp);
3130                         return (err);
3131                 }
3132                 mntput(vfsp);
3133                 if ((err = dounmount(vfsp, flag, kcred)) != 0)
3134                         return (err);
3135         }
3136 #endif /* HAVE_SNAPSHOT */
3137         return (0);
3138 }
3139
3140 /*
3141  * inputs:
3142  * zc_name              name of filesystem
3143  * zc_value             short name of snapshot
3144  * zc_defer_destroy     mark for deferred destroy
3145  *
3146  * outputs:     none
3147  */
3148 static int
3149 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3150 {
3151         int err;
3152
3153         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3154                 return (EINVAL);
3155         err = dmu_objset_find(zc->zc_name,
3156             zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3157         if (err)
3158                 return (err);
3159         return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3160             zc->zc_defer_destroy));
3161 }
3162
3163 /*
3164  * inputs:
3165  * zc_name              name of dataset to destroy
3166  * zc_objset_type       type of objset
3167  * zc_defer_destroy     mark for deferred destroy
3168  *
3169  * outputs:             none
3170  */
3171 static int
3172 zfs_ioc_destroy(zfs_cmd_t *zc)
3173 {
3174         int err;
3175         if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3176                 err = zfs_unmount_snap(zc->zc_name, NULL);
3177                 if (err)
3178                         return (err);
3179         }
3180
3181         err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3182         if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3183                 (void) zvol_remove_minor(zc->zc_name);
3184         return (err);
3185 }
3186
3187 /*
3188  * inputs:
3189  * zc_name      name of dataset to rollback (to most recent snapshot)
3190  *
3191  * outputs:     none
3192  */
3193 static int
3194 zfs_ioc_rollback(zfs_cmd_t *zc)
3195 {
3196         dsl_dataset_t *ds, *clone;
3197         int error;
3198         zfs_sb_t *zsb;
3199         char *clone_name;
3200
3201         error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3202         if (error)
3203                 return (error);
3204
3205         /* must not be a snapshot */
3206         if (dsl_dataset_is_snapshot(ds)) {
3207                 dsl_dataset_rele(ds, FTAG);
3208                 return (EINVAL);
3209         }
3210
3211         /* must have a most recent snapshot */
3212         if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3213                 dsl_dataset_rele(ds, FTAG);
3214                 return (EINVAL);
3215         }
3216
3217         /*
3218          * Create clone of most recent snapshot.
3219          */
3220         clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3221         error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3222         if (error)
3223                 goto out;
3224
3225         error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3226         if (error)
3227                 goto out;
3228
3229         /*
3230          * Do clone swap.
3231          */
3232         if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
3233                 error = zfs_suspend_fs(zsb);
3234                 if (error == 0) {
3235                         int resume_err;
3236
3237                         if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3238                                 error = dsl_dataset_clone_swap(clone, ds,
3239                                     B_TRUE);
3240                                 dsl_dataset_disown(ds, FTAG);
3241                                 ds = NULL;
3242                         } else {
3243                                 error = EBUSY;
3244                         }
3245                         resume_err = zfs_resume_fs(zsb, zc->zc_name);
3246                         error = error ? error : resume_err;
3247                 }
3248                 deactivate_super(zsb->z_sb);
3249         } else {
3250                 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3251                         error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3252                         dsl_dataset_disown(ds, FTAG);
3253                         ds = NULL;
3254                 } else {
3255                         error = EBUSY;
3256                 }
3257         }
3258
3259         /*
3260          * Destroy clone (which also closes it).
3261          */
3262         (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3263
3264 out:
3265         strfree(clone_name);
3266         if (ds)
3267                 dsl_dataset_rele(ds, FTAG);
3268         return (error);
3269 }
3270
3271 /*
3272  * inputs:
3273  * zc_name      old name of dataset
3274  * zc_value     new name of dataset
3275  * zc_cookie    recursive flag (only valid for snapshots)
3276  *
3277  * outputs:     none
3278  */
3279 static int
3280 zfs_ioc_rename(zfs_cmd_t *zc)
3281 {
3282         boolean_t recursive = zc->zc_cookie & 1;
3283         int err;
3284
3285         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3286         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3287             strchr(zc->zc_value, '%'))
3288                 return (EINVAL);
3289
3290         /*
3291          * Unmount snapshot unless we're doing a recursive rename,
3292          * in which case the dataset code figures out which snapshots
3293          * to unmount.
3294          */
3295         if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3296             zc->zc_objset_type == DMU_OST_ZFS) {
3297                 err = zfs_unmount_snap(zc->zc_name, NULL);
3298                 if (err)
3299                         return (err);
3300         }
3301
3302         err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3303         if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3304                 (void) zvol_remove_minor(zc->zc_name);
3305                 (void) zvol_create_minor(zc->zc_value);
3306         }
3307
3308         return (err);
3309 }
3310
3311 static int
3312 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3313 {
3314         const char *propname = nvpair_name(pair);
3315         boolean_t issnap = (strchr(dsname, '@') != NULL);
3316         zfs_prop_t prop = zfs_name_to_prop(propname);
3317         uint64_t intval;
3318         int err;
3319
3320         if (prop == ZPROP_INVAL) {
3321                 if (zfs_prop_user(propname)) {
3322                         if ((err = zfs_secpolicy_write_perms(dsname,
3323                             ZFS_DELEG_PERM_USERPROP, cr)))
3324                                 return (err);
3325                         return (0);
3326                 }
3327
3328                 if (!issnap && zfs_prop_userquota(propname)) {
3329                         const char *perm = NULL;
3330                         const char *uq_prefix =
3331                             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3332                         const char *gq_prefix =
3333                             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3334
3335                         if (strncmp(propname, uq_prefix,
3336                             strlen(uq_prefix)) == 0) {
3337                                 perm = ZFS_DELEG_PERM_USERQUOTA;
3338                         } else if (strncmp(propname, gq_prefix,
3339                             strlen(gq_prefix)) == 0) {
3340                                 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3341                         } else {
3342                                 /* USERUSED and GROUPUSED are read-only */
3343                                 return (EINVAL);
3344                         }
3345
3346                         if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3347                                 return (err);
3348                         return (0);
3349                 }
3350
3351                 return (EINVAL);
3352         }
3353
3354         if (issnap)
3355                 return (EINVAL);
3356
3357         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3358                 /*
3359                  * dsl_prop_get_all_impl() returns properties in this
3360                  * format.
3361                  */
3362                 nvlist_t *attrs;
3363                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3364                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3365                     &pair) == 0);
3366         }
3367
3368         /*
3369          * Check that this value is valid for this pool version
3370          */
3371         switch (prop) {
3372         case ZFS_PROP_COMPRESSION:
3373                 /*
3374                  * If the user specified gzip compression, make sure
3375                  * the SPA supports it. We ignore any errors here since
3376                  * we'll catch them later.
3377                  */
3378                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3379                     nvpair_value_uint64(pair, &intval) == 0) {
3380                         if (intval >= ZIO_COMPRESS_GZIP_1 &&
3381                             intval <= ZIO_COMPRESS_GZIP_9 &&
3382                             zfs_earlier_version(dsname,
3383                             SPA_VERSION_GZIP_COMPRESSION)) {
3384                                 return (ENOTSUP);
3385                         }
3386
3387                         if (intval == ZIO_COMPRESS_ZLE &&
3388                             zfs_earlier_version(dsname,
3389                             SPA_VERSION_ZLE_COMPRESSION))
3390                                 return (ENOTSUP);
3391
3392                         /*
3393                          * If this is a bootable dataset then
3394                          * verify that the compression algorithm
3395                          * is supported for booting. We must return
3396                          * something other than ENOTSUP since it
3397                          * implies a downrev pool version.
3398                          */
3399                         if (zfs_is_bootfs(dsname) &&
3400                             !BOOTFS_COMPRESS_VALID(intval)) {
3401                                 return (ERANGE);
3402                         }
3403                 }
3404                 break;
3405
3406         case ZFS_PROP_COPIES:
3407                 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3408                         return (ENOTSUP);
3409                 break;
3410
3411         case ZFS_PROP_DEDUP:
3412                 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3413                         return (ENOTSUP);
3414                 break;
3415
3416         case ZFS_PROP_SHARESMB:
3417                 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3418                         return (ENOTSUP);
3419                 break;
3420
3421         case ZFS_PROP_ACLINHERIT:
3422                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3423                     nvpair_value_uint64(pair, &intval) == 0) {
3424                         if (intval == ZFS_ACL_PASSTHROUGH_X &&
3425                             zfs_earlier_version(dsname,
3426                             SPA_VERSION_PASSTHROUGH_X))
3427                                 return (ENOTSUP);
3428                 }
3429                 break;
3430         default:
3431                 break;
3432         }
3433
3434         return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3435 }
3436
3437 /*
3438  * Removes properties from the given props list that fail permission checks
3439  * needed to clear them and to restore them in case of a receive error. For each
3440  * property, make sure we have both set and inherit permissions.
3441  *
3442  * Returns the first error encountered if any permission checks fail. If the
3443  * caller provides a non-NULL errlist, it also gives the complete list of names
3444  * of all the properties that failed a permission check along with the
3445  * corresponding error numbers. The caller is responsible for freeing the
3446  * returned errlist.
3447  *
3448  * If every property checks out successfully, zero is returned and the list
3449  * pointed at by errlist is NULL.
3450  */
3451 static int
3452 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3453 {
3454         zfs_cmd_t *zc;
3455         nvpair_t *pair, *next_pair;
3456         nvlist_t *errors;
3457         int err, rv = 0;
3458
3459         if (props == NULL)
3460                 return (0);
3461
3462         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3463
3464         zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
3465         (void) strcpy(zc->zc_name, dataset);
3466         pair = nvlist_next_nvpair(props, NULL);
3467         while (pair != NULL) {
3468                 next_pair = nvlist_next_nvpair(props, pair);
3469
3470                 (void) strcpy(zc->zc_value, nvpair_name(pair));
3471                 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3472                     (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3473                         VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3474                         VERIFY(nvlist_add_int32(errors,
3475                             zc->zc_value, err) == 0);
3476                 }
3477                 pair = next_pair;
3478         }
3479         kmem_free(zc, sizeof (zfs_cmd_t));
3480
3481         if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3482                 nvlist_free(errors);
3483                 errors = NULL;
3484         } else {
3485                 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3486         }
3487
3488         if (errlist == NULL)
3489                 nvlist_free(errors);
3490         else
3491                 *errlist = errors;
3492
3493         return (rv);
3494 }
3495
3496 static boolean_t
3497 propval_equals(nvpair_t *p1, nvpair_t *p2)
3498 {
3499         if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3500                 /* dsl_prop_get_all_impl() format */
3501                 nvlist_t *attrs;
3502                 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3503                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3504                     &p1) == 0);
3505         }
3506
3507         if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3508                 nvlist_t *attrs;
3509                 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3510                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3511                     &p2) == 0);
3512         }
3513
3514         if (nvpair_type(p1) != nvpair_type(p2))
3515                 return (B_FALSE);
3516
3517         if (nvpair_type(p1) == DATA_TYPE_STRING) {
3518                 char *valstr1, *valstr2;
3519
3520                 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3521                 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3522                 return (strcmp(valstr1, valstr2) == 0);
3523         } else {
3524                 uint64_t intval1, intval2;
3525
3526                 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3527                 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3528                 return (intval1 == intval2);
3529         }
3530 }
3531
3532 /*
3533  * Remove properties from props if they are not going to change (as determined
3534  * by comparison with origprops). Remove them from origprops as well, since we
3535  * do not need to clear or restore properties that won't change.
3536  */
3537 static void
3538 props_reduce(nvlist_t *props, nvlist_t *origprops)
3539 {
3540         nvpair_t *pair, *next_pair;
3541
3542         if (origprops == NULL)
3543                 return; /* all props need to be received */
3544
3545         pair = nvlist_next_nvpair(props, NULL);
3546         while (pair != NULL) {
3547                 const char *propname = nvpair_name(pair);
3548                 nvpair_t *match;
3549
3550                 next_pair = nvlist_next_nvpair(props, pair);
3551
3552                 if ((nvlist_lookup_nvpair(origprops, propname,
3553                     &match) != 0) || !propval_equals(pair, match))
3554                         goto next; /* need to set received value */
3555
3556                 /* don't clear the existing received value */
3557                 (void) nvlist_remove_nvpair(origprops, match);
3558                 /* don't bother receiving the property */
3559                 (void) nvlist_remove_nvpair(props, pair);
3560 next:
3561                 pair = next_pair;
3562         }
3563 }
3564
3565 #ifdef  DEBUG
3566 static boolean_t zfs_ioc_recv_inject_err;
3567 #endif
3568
3569 /*
3570  * inputs:
3571  * zc_name              name of containing filesystem
3572  * zc_nvlist_src{_size} nvlist of properties to apply
3573  * zc_value             name of snapshot to create
3574  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
3575  * zc_cookie            file descriptor to recv from
3576  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
3577  * zc_guid              force flag
3578  * zc_cleanup_fd        cleanup-on-exit file descriptor
3579  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
3580  *
3581  * outputs:
3582  * zc_cookie            number of bytes read
3583  * zc_nvlist_dst{_size} error for each unapplied received property
3584  * zc_obj               zprop_errflags_t
3585  * zc_action_handle     handle for this guid/ds mapping
3586  */
3587 static int
3588 zfs_ioc_recv(zfs_cmd_t *zc)
3589 {
3590         file_t *fp;
3591         objset_t *os;
3592         dmu_recv_cookie_t drc;
3593         boolean_t force = (boolean_t)zc->zc_guid;
3594         int fd;
3595         int error = 0;
3596         int props_error = 0;
3597         nvlist_t *errors;
3598         offset_t off;
3599         nvlist_t *props = NULL; /* sent properties */
3600         nvlist_t *origprops = NULL; /* existing properties */
3601         objset_t *origin = NULL;
3602         char *tosnap;
3603         char tofs[ZFS_MAXNAMELEN];
3604         boolean_t first_recvd_props = B_FALSE;
3605
3606         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3607             strchr(zc->zc_value, '@') == NULL ||
3608             strchr(zc->zc_value, '%'))
3609                 return (EINVAL);
3610
3611         (void) strcpy(tofs, zc->zc_value);
3612         tosnap = strchr(tofs, '@');
3613         *tosnap++ = '\0';
3614
3615         if (zc->zc_nvlist_src != 0 &&
3616             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3617             zc->zc_iflags, &props)) != 0)
3618                 return (error);
3619
3620         fd = zc->zc_cookie;
3621         fp = getf(fd);
3622         if (fp == NULL) {
3623                 nvlist_free(props);
3624                 return (EBADF);
3625         }
3626
3627         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3628
3629         if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3630                 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3631                     !dsl_prop_get_hasrecvd(os)) {
3632                         first_recvd_props = B_TRUE;
3633                 }
3634
3635                 /*
3636                  * If new received properties are supplied, they are to
3637                  * completely replace the existing received properties, so stash
3638                  * away the existing ones.
3639                  */
3640                 if (dsl_prop_get_received(os, &origprops) == 0) {
3641                         nvlist_t *errlist = NULL;
3642                         /*
3643                          * Don't bother writing a property if its value won't
3644                          * change (and avoid the unnecessary security checks).
3645                          *
3646                          * The first receive after SPA_VERSION_RECVD_PROPS is a
3647                          * special case where we blow away all local properties
3648                          * regardless.
3649                          */
3650                         if (!first_recvd_props)
3651                                 props_reduce(props, origprops);
3652                         if (zfs_check_clearable(tofs, origprops,
3653                             &errlist) != 0)
3654                                 (void) nvlist_merge(errors, errlist, 0);
3655                         nvlist_free(errlist);
3656                 }
3657
3658                 dmu_objset_rele(os, FTAG);
3659         }
3660
3661         if (zc->zc_string[0]) {
3662                 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3663                 if (error)
3664                         goto out;
3665         }
3666
3667         error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3668             &zc->zc_begin_record, force, origin, &drc);
3669         if (origin)
3670                 dmu_objset_rele(origin, FTAG);
3671         if (error)
3672                 goto out;
3673
3674         /*
3675          * Set properties before we receive the stream so that they are applied
3676          * to the new data. Note that we must call dmu_recv_stream() if
3677          * dmu_recv_begin() succeeds.
3678          */
3679         if (props) {
3680                 nvlist_t *errlist;
3681
3682                 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3683                         if (drc.drc_newfs) {
3684                                 if (spa_version(os->os_spa) >=
3685                                     SPA_VERSION_RECVD_PROPS)
3686                                         first_recvd_props = B_TRUE;
3687                         } else if (origprops != NULL) {
3688                                 if (clear_received_props(os, tofs, origprops,
3689                                     first_recvd_props ? NULL : props) != 0)
3690                                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3691                         } else {
3692                                 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3693                         }
3694                         dsl_prop_set_hasrecvd(os);
3695                 } else if (!drc.drc_newfs) {
3696                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3697                 }
3698
3699                 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3700                     props, &errlist);
3701                 (void) nvlist_merge(errors, errlist, 0);
3702                 nvlist_free(errlist);
3703         }
3704
3705         if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3706                 /*
3707                  * Caller made zc->zc_nvlist_dst less than the minimum expected
3708                  * size or supplied an invalid address.
3709                  */
3710                 props_error = EINVAL;
3711         }
3712
3713         off = fp->f_offset;
3714         error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3715             &zc->zc_action_handle);
3716
3717         if (error == 0) {
3718                 zfs_sb_t *zsb = NULL;
3719
3720                 if (get_zfs_sb(tofs, &zsb) == 0) {
3721                         /* online recv */
3722                         int end_err;
3723
3724                         error = zfs_suspend_fs(zsb);
3725                         /*
3726                          * If the suspend fails, then the recv_end will
3727                          * likely also fail, and clean up after itself.
3728                          */
3729                         end_err = dmu_recv_end(&drc);
3730                         if (error == 0)
3731                                 error = zfs_resume_fs(zsb, tofs);
3732                         error = error ? error : end_err;
3733                         deactivate_super(zsb->z_sb);
3734                 } else {
3735                         error = dmu_recv_end(&drc);
3736                 }
3737         }
3738
3739         zc->zc_cookie = off - fp->f_offset;
3740         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3741                 fp->f_offset = off;
3742
3743 #ifdef  DEBUG
3744         if (zfs_ioc_recv_inject_err) {
3745                 zfs_ioc_recv_inject_err = B_FALSE;
3746                 error = 1;
3747         }
3748 #endif
3749         /*
3750          * On error, restore the original props.
3751          */
3752         if (error && props) {
3753                 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3754                         if (clear_received_props(os, tofs, props, NULL) != 0) {
3755                                 /*
3756                                  * We failed to clear the received properties.
3757                                  * Since we may have left a $recvd value on the
3758                                  * system, we can't clear the $hasrecvd flag.
3759                                  */
3760                                 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3761                         } else if (first_recvd_props) {
3762                                 dsl_prop_unset_hasrecvd(os);
3763                         }
3764                         dmu_objset_rele(os, FTAG);
3765                 } else if (!drc.drc_newfs) {
3766                         /* We failed to clear the received properties. */
3767                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3768                 }
3769
3770                 if (origprops == NULL && !drc.drc_newfs) {
3771                         /* We failed to stash the original properties. */
3772                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3773                 }
3774
3775                 /*
3776                  * dsl_props_set() will not convert RECEIVED to LOCAL on or
3777                  * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3778                  * explictly if we're restoring local properties cleared in the
3779                  * first new-style receive.
3780                  */
3781                 if (origprops != NULL &&
3782                     zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3783                     ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3784                     origprops, NULL) != 0) {
3785                         /*
3786                          * We stashed the original properties but failed to
3787                          * restore them.
3788                          */
3789                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3790                 }
3791         }
3792 out:
3793         nvlist_free(props);
3794         nvlist_free(origprops);
3795         nvlist_free(errors);
3796         releasef(fd);
3797
3798         if (error == 0)
3799                 error = props_error;
3800
3801         return (error);
3802 }
3803
3804 /*
3805  * inputs:
3806  * zc_name      name of snapshot to send
3807  * zc_cookie    file descriptor to send stream to
3808  * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
3809  * zc_sendobj   objsetid of snapshot to send
3810  * zc_fromobj   objsetid of incremental fromsnap (may be zero)
3811  *
3812  * outputs: none
3813  */
3814 static int
3815 zfs_ioc_send(zfs_cmd_t *zc)
3816 {
3817         objset_t *fromsnap = NULL;
3818         objset_t *tosnap;
3819         file_t *fp;
3820         int error;
3821         offset_t off;
3822         dsl_dataset_t *ds;
3823         dsl_dataset_t *dsfrom = NULL;
3824         spa_t *spa;
3825         dsl_pool_t *dp;
3826
3827         error = spa_open(zc->zc_name, &spa, FTAG);
3828         if (error)
3829                 return (error);
3830
3831         dp = spa_get_dsl(spa);
3832         rw_enter(&dp->dp_config_rwlock, RW_READER);
3833         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3834         rw_exit(&dp->dp_config_rwlock);
3835         if (error) {
3836                 spa_close(spa, FTAG);
3837                 return (error);
3838         }
3839
3840         error = dmu_objset_from_ds(ds, &tosnap);
3841         if (error) {
3842                 dsl_dataset_rele(ds, FTAG);
3843                 spa_close(spa, FTAG);
3844                 return (error);
3845         }
3846
3847         if (zc->zc_fromobj != 0) {
3848                 rw_enter(&dp->dp_config_rwlock, RW_READER);
3849                 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3850                 rw_exit(&dp->dp_config_rwlock);
3851                 spa_close(spa, FTAG);
3852                 if (error) {
3853                         dsl_dataset_rele(ds, FTAG);
3854                         return (error);
3855                 }
3856                 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3857                 if (error) {
3858                         dsl_dataset_rele(dsfrom, FTAG);
3859                         dsl_dataset_rele(ds, FTAG);
3860                         return (error);
3861                 }
3862         } else {
3863                 spa_close(spa, FTAG);
3864         }
3865
3866         fp = getf(zc->zc_cookie);
3867         if (fp == NULL) {
3868                 dsl_dataset_rele(ds, FTAG);
3869                 if (dsfrom)
3870                         dsl_dataset_rele(dsfrom, FTAG);
3871                 return (EBADF);
3872         }
3873
3874         off = fp->f_offset;
3875         error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3876
3877         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3878                 fp->f_offset = off;
3879         releasef(zc->zc_cookie);
3880         if (dsfrom)
3881                 dsl_dataset_rele(dsfrom, FTAG);
3882         dsl_dataset_rele(ds, FTAG);
3883         return (error);
3884 }
3885
3886 static int
3887 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3888 {
3889         int id, error;
3890
3891         error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3892             &zc->zc_inject_record);
3893
3894         if (error == 0)
3895                 zc->zc_guid = (uint64_t)id;
3896
3897         return (error);
3898 }
3899
3900 static int
3901 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3902 {
3903         return (zio_clear_fault((int)zc->zc_guid));
3904 }
3905
3906 static int
3907 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3908 {
3909         int id = (int)zc->zc_guid;
3910         int error;
3911
3912         error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3913             &zc->zc_inject_record);
3914
3915         zc->zc_guid = id;
3916
3917         return (error);
3918 }
3919
3920 static int
3921 zfs_ioc_error_log(zfs_cmd_t *zc)
3922 {
3923         spa_t *spa;
3924         int error;
3925         size_t count = (size_t)zc->zc_nvlist_dst_size;
3926
3927         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3928                 return (error);
3929
3930         error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3931             &count);
3932         if (error == 0)
3933                 zc->zc_nvlist_dst_size = count;
3934         else
3935                 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3936
3937         spa_close(spa, FTAG);
3938
3939         return (error);
3940 }
3941
3942 static int
3943 zfs_ioc_clear(zfs_cmd_t *zc)
3944 {
3945         spa_t *spa;
3946         vdev_t *vd;
3947         int error;
3948
3949         /*
3950          * On zpool clear we also fix up missing slogs
3951          */
3952         mutex_enter(&spa_namespace_lock);
3953         spa = spa_lookup(zc->zc_name);
3954         if (spa == NULL) {
3955                 mutex_exit(&spa_namespace_lock);
3956                 return (EIO);
3957         }
3958         if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3959                 /* we need to let spa_open/spa_load clear the chains */
3960                 spa_set_log_state(spa, SPA_LOG_CLEAR);
3961         }
3962         spa->spa_last_open_failed = 0;
3963         mutex_exit(&spa_namespace_lock);
3964
3965         if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3966                 error = spa_open(zc->zc_name, &spa, FTAG);
3967         } else {
3968                 nvlist_t *policy;
3969                 nvlist_t *config = NULL;
3970
3971                 if (zc->zc_nvlist_src == 0)
3972                         return (EINVAL);
3973
3974                 if ((error = get_nvlist(zc->zc_nvlist_src,
3975                     zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3976                         error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3977                             policy, &config);
3978                         if (config != NULL) {
3979                                 int err;
3980
3981                                 if ((err = put_nvlist(zc, config)) != 0)
3982                                         error = err;
3983                                 nvlist_free(config);
3984                         }
3985                         nvlist_free(policy);
3986                 }
3987         }
3988
3989         if (error)
3990                 return (error);
3991
3992         spa_vdev_state_enter(spa, SCL_NONE);
3993
3994         if (zc->zc_guid == 0) {
3995                 vd = NULL;
3996         } else {
3997                 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3998                 if (vd == NULL) {
3999                         (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4000                         spa_close(spa, FTAG);
4001                         return (ENODEV);
4002                 }
4003         }
4004
4005         vdev_clear(spa, vd);
4006
4007         (void) spa_vdev_state_exit(spa, NULL, 0);
4008
4009         /*
4010          * Resume any suspended I/Os.
4011          */
4012         if (zio_resume(spa) != 0)
4013                 error = EIO;
4014
4015         spa_close(spa, FTAG);
4016
4017         return (error);
4018 }
4019
4020 /*
4021  * inputs:
4022  * zc_name      name of filesystem
4023  * zc_value     name of origin snapshot
4024  *
4025  * outputs:
4026  * zc_string    name of conflicting snapshot, if there is one
4027  */
4028 static int
4029 zfs_ioc_promote(zfs_cmd_t *zc)
4030 {
4031         char *cp;
4032
4033         /*
4034          * We don't need to unmount *all* the origin fs's snapshots, but
4035          * it's easier.
4036          */
4037         cp = strchr(zc->zc_value, '@');
4038         if (cp)
4039                 *cp = '\0';
4040         (void) dmu_objset_find(zc->zc_value,
4041             zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4042         return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4043 }
4044
4045 /*
4046  * Retrieve a single {user|group}{used|quota}@... property.
4047  *
4048  * inputs:
4049  * zc_name      name of filesystem
4050  * zc_objset_type zfs_userquota_prop_t
4051  * zc_value     domain name (eg. "S-1-234-567-89")
4052  * zc_guid      RID/UID/GID
4053  *
4054  * outputs:
4055  * zc_cookie    property value
4056  */
4057 static int
4058 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4059 {
4060         zfs_sb_t *zsb;
4061         int error;
4062
4063         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4064                 return (EINVAL);
4065
4066         error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4067         if (error)
4068                 return (error);
4069
4070         error = zfs_userspace_one(zsb,
4071             zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4072         zfs_sb_rele(zsb, FTAG);
4073
4074         return (error);
4075 }
4076
4077 /*
4078  * inputs:
4079  * zc_name              name of filesystem
4080  * zc_cookie            zap cursor
4081  * zc_objset_type       zfs_userquota_prop_t
4082  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4083  *
4084  * outputs:
4085  * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4086  * zc_cookie    zap cursor
4087  */
4088 static int
4089 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4090 {
4091         zfs_sb_t *zsb;
4092         int bufsize = zc->zc_nvlist_dst_size;
4093         int error;
4094         void *buf;
4095
4096         if (bufsize <= 0)
4097                 return (ENOMEM);
4098
4099         error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4100         if (error)
4101                 return (error);
4102
4103         buf = vmem_alloc(bufsize, KM_SLEEP);
4104
4105         error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4106             buf, &zc->zc_nvlist_dst_size);
4107
4108         if (error == 0) {
4109                 error = xcopyout(buf,
4110                     (void *)(uintptr_t)zc->zc_nvlist_dst,
4111                     zc->zc_nvlist_dst_size);
4112         }
4113         vmem_free(buf, bufsize);
4114         zfs_sb_rele(zsb, FTAG);
4115
4116         return (error);
4117 }
4118
4119 /*
4120  * inputs:
4121  * zc_name              name of filesystem
4122  *
4123  * outputs:
4124  * none
4125  */
4126 static int
4127 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4128 {
4129         objset_t *os;
4130         int error = 0;
4131         zfs_sb_t *zsb;
4132
4133         if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4134                 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4135                         /*
4136                          * If userused is not enabled, it may be because the
4137                          * objset needs to be closed & reopened (to grow the
4138                          * objset_phys_t).  Suspend/resume the fs will do that.
4139                          */
4140                         error = zfs_suspend_fs(zsb);
4141                         if (error == 0)
4142                                 error = zfs_resume_fs(zsb, zc->zc_name);
4143                 }
4144                 if (error == 0)
4145                         error = dmu_objset_userspace_upgrade(zsb->z_os);
4146                 deactivate_super(zsb->z_sb);
4147         } else {
4148                 /* XXX kind of reading contents without owning */
4149                 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4150                 if (error)
4151                         return (error);
4152
4153                 error = dmu_objset_userspace_upgrade(os);
4154                 dmu_objset_rele(os, FTAG);
4155         }
4156
4157         return (error);
4158 }
4159
4160 static int
4161 zfs_ioc_share(zfs_cmd_t *zc)
4162 {
4163         return (ENOSYS);
4164 }
4165
4166 ace_t full_access[] = {
4167         {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4168 };
4169
4170 /*
4171  * inputs:
4172  * zc_name              name of containing filesystem
4173  * zc_obj               object # beyond which we want next in-use object #
4174  *
4175  * outputs:
4176  * zc_obj               next in-use object #
4177  */
4178 static int
4179 zfs_ioc_next_obj(zfs_cmd_t *zc)
4180 {
4181         objset_t *os = NULL;
4182         int error;
4183
4184         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4185         if (error)
4186                 return (error);
4187
4188         error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4189             os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4190
4191         dmu_objset_rele(os, FTAG);
4192         return (error);
4193 }
4194
4195 /*
4196  * inputs:
4197  * zc_name              name of filesystem
4198  * zc_value             prefix name for snapshot
4199  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4200  *
4201  * outputs:
4202  */
4203 static int
4204 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4205 {
4206         char *snap_name;
4207         int error;
4208
4209         snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4210             (u_longlong_t)ddi_get_lbolt64());
4211
4212         if (strlen(snap_name) >= MAXNAMELEN) {
4213                 strfree(snap_name);
4214                 return (E2BIG);
4215         }
4216
4217         error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4218             NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4219         if (error != 0) {
4220                 strfree(snap_name);
4221                 return (error);
4222         }
4223
4224         (void) strcpy(zc->zc_value, snap_name);
4225         strfree(snap_name);
4226         return (0);
4227 }
4228
4229 /*
4230  * inputs:
4231  * zc_name              name of "to" snapshot
4232  * zc_value             name of "from" snapshot
4233  * zc_cookie            file descriptor to write diff data on
4234  *
4235  * outputs:
4236  * dmu_diff_record_t's to the file descriptor
4237  */
4238 static int
4239 zfs_ioc_diff(zfs_cmd_t *zc)
4240 {
4241         objset_t *fromsnap;
4242         objset_t *tosnap;
4243         file_t *fp;
4244         offset_t off;
4245         int error;
4246
4247         error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4248         if (error)
4249                 return (error);
4250
4251         error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4252         if (error) {
4253                 dmu_objset_rele(tosnap, FTAG);
4254                 return (error);
4255         }
4256
4257         fp = getf(zc->zc_cookie);
4258         if (fp == NULL) {
4259                 dmu_objset_rele(fromsnap, FTAG);
4260                 dmu_objset_rele(tosnap, FTAG);
4261                 return (EBADF);
4262         }
4263
4264         off = fp->f_offset;
4265
4266         error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4267
4268         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4269                 fp->f_offset = off;
4270         releasef(zc->zc_cookie);
4271
4272         dmu_objset_rele(fromsnap, FTAG);
4273         dmu_objset_rele(tosnap, FTAG);
4274         return (error);
4275 }
4276
4277 /*
4278  * Remove all ACL files in shares dir
4279  */
4280 #ifdef HAVE_SMB_SHARE
4281 static int
4282 zfs_smb_acl_purge(znode_t *dzp)
4283 {
4284         zap_cursor_t    zc;
4285         zap_attribute_t zap;
4286         zfs_sb_t *zsb = ZTOZSB(dzp);
4287         int error;
4288
4289         for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4290             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4291             zap_cursor_advance(&zc)) {
4292                 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4293                     NULL, 0)) != 0)
4294                         break;
4295         }
4296         zap_cursor_fini(&zc);
4297         return (error);
4298 }
4299 #endif /* HAVE_SMB_SHARE */
4300
4301 static int
4302 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4303 {
4304 #ifdef HAVE_SMB_SHARE
4305         vnode_t *vp;
4306         znode_t *dzp;
4307         vnode_t *resourcevp = NULL;
4308         znode_t *sharedir;
4309         zfs_sb_t *zsb;
4310         nvlist_t *nvlist;
4311         char *src, *target;
4312         vattr_t vattr;
4313         vsecattr_t vsec;
4314         int error = 0;
4315
4316         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4317             NO_FOLLOW, NULL, &vp)) != 0)
4318                 return (error);
4319
4320         /* Now make sure mntpnt and dataset are ZFS */
4321
4322         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4323             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4324             zc->zc_name) != 0)) {
4325                 VN_RELE(vp);
4326                 return (EINVAL);
4327         }
4328
4329         dzp = VTOZ(vp);
4330         zsb = ZTOZSB(dzp);
4331         ZFS_ENTER(zsb);
4332
4333         /*
4334          * Create share dir if its missing.
4335          */
4336         mutex_enter(&zsb->z_lock);
4337         if (zsb->z_shares_dir == 0) {
4338                 dmu_tx_t *tx;
4339
4340                 tx = dmu_tx_create(zsb->z_os);
4341                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4342                     ZFS_SHARES_DIR);
4343                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4344                 error = dmu_tx_assign(tx, TXG_WAIT);
4345                 if (error) {
4346                         dmu_tx_abort(tx);
4347                 } else {
4348                         error = zfs_create_share_dir(zsb, tx);
4349                         dmu_tx_commit(tx);
4350                 }
4351                 if (error) {
4352                         mutex_exit(&zsb->z_lock);
4353                         VN_RELE(vp);
4354                         ZFS_EXIT(zsb);
4355                         return (error);
4356                 }
4357         }
4358         mutex_exit(&zsb->z_lock);
4359
4360         ASSERT(zsb->z_shares_dir);
4361         if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
4362                 VN_RELE(vp);
4363                 ZFS_EXIT(zsb);
4364                 return (error);
4365         }
4366
4367         switch (zc->zc_cookie) {
4368         case ZFS_SMB_ACL_ADD:
4369                 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4370                 vattr.va_mode = S_IFREG|0777;
4371                 vattr.va_uid = 0;
4372                 vattr.va_gid = 0;
4373
4374                 vsec.vsa_mask = VSA_ACE;
4375                 vsec.vsa_aclentp = &full_access;
4376                 vsec.vsa_aclentsz = sizeof (full_access);
4377                 vsec.vsa_aclcnt = 1;
4378
4379                 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4380                     &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4381                 if (resourcevp)
4382                         VN_RELE(resourcevp);
4383                 break;
4384
4385         case ZFS_SMB_ACL_REMOVE:
4386                 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4387                     NULL, 0);
4388                 break;
4389
4390         case ZFS_SMB_ACL_RENAME:
4391                 if ((error = get_nvlist(zc->zc_nvlist_src,
4392                     zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4393                         VN_RELE(vp);
4394                         ZFS_EXIT(zsb);
4395                         return (error);
4396                 }
4397                 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4398                     nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4399                     &target)) {
4400                         VN_RELE(vp);
4401                         VN_RELE(ZTOV(sharedir));
4402                         ZFS_EXIT(zsb);
4403                         nvlist_free(nvlist);
4404                         return (error);
4405                 }
4406                 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4407                     kcred, NULL, 0);
4408                 nvlist_free(nvlist);
4409                 break;
4410
4411         case ZFS_SMB_ACL_PURGE:
4412                 error = zfs_smb_acl_purge(sharedir);
4413                 break;
4414
4415         default:
4416                 error = EINVAL;
4417                 break;
4418         }
4419
4420         VN_RELE(vp);
4421         VN_RELE(ZTOV(sharedir));
4422
4423         ZFS_EXIT(zsb);
4424
4425         return (error);
4426 #else
4427         return (ENOTSUP);
4428 #endif /* HAVE_SMB_SHARE */
4429 }
4430
4431 /*
4432  * inputs:
4433  * zc_name              name of filesystem
4434  * zc_value             short name of snap
4435  * zc_string            user-supplied tag for this hold
4436  * zc_cookie            recursive flag
4437  * zc_temphold          set if hold is temporary
4438  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4439  * zc_sendobj           if non-zero, the objid for zc_name@zc_value
4440  * zc_createtxg         if zc_sendobj is non-zero, snap must have zc_createtxg
4441  *
4442  * outputs:             none
4443  */
4444 static int
4445 zfs_ioc_hold(zfs_cmd_t *zc)
4446 {
4447         boolean_t recursive = zc->zc_cookie;
4448         spa_t *spa;
4449         dsl_pool_t *dp;
4450         dsl_dataset_t *ds;
4451         int error;
4452         minor_t minor = 0;
4453
4454         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4455                 return (EINVAL);
4456
4457         if (zc->zc_sendobj == 0) {
4458                 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4459                     zc->zc_string, recursive, zc->zc_temphold,
4460                     zc->zc_cleanup_fd));
4461         }
4462
4463         if (recursive)
4464                 return (EINVAL);
4465
4466         error = spa_open(zc->zc_name, &spa, FTAG);
4467         if (error)
4468                 return (error);
4469
4470         dp = spa_get_dsl(spa);
4471         rw_enter(&dp->dp_config_rwlock, RW_READER);
4472         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4473         rw_exit(&dp->dp_config_rwlock);
4474         spa_close(spa, FTAG);
4475         if (error)
4476                 return (error);
4477
4478         /*
4479          * Until we have a hold on this snapshot, it's possible that
4480          * zc_sendobj could've been destroyed and reused as part
4481          * of a later txg.  Make sure we're looking at the right object.
4482          */
4483         if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4484                 dsl_dataset_rele(ds, FTAG);
4485                 return (ENOENT);
4486         }
4487
4488         if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4489                 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4490                 if (error) {
4491                         dsl_dataset_rele(ds, FTAG);
4492                         return (error);
4493                 }
4494         }
4495
4496         error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4497             zc->zc_temphold);
4498         if (minor != 0) {
4499                 if (error == 0) {
4500                         dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4501                             minor);
4502                 }
4503                 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4504         }
4505         dsl_dataset_rele(ds, FTAG);
4506
4507         return (error);
4508 }
4509
4510 /*
4511  * inputs:
4512  * zc_name      name of dataset from which we're releasing a user hold
4513  * zc_value     short name of snap
4514  * zc_string    user-supplied tag for this hold
4515  * zc_cookie    recursive flag
4516  *
4517  * outputs:     none
4518  */
4519 static int
4520 zfs_ioc_release(zfs_cmd_t *zc)
4521 {
4522         boolean_t recursive = zc->zc_cookie;
4523
4524         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4525                 return (EINVAL);
4526
4527         return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4528             zc->zc_string, recursive));
4529 }
4530
4531 /*
4532  * inputs:
4533  * zc_name              name of filesystem
4534  *
4535  * outputs:
4536  * zc_nvlist_src{_size} nvlist of snapshot holds
4537  */
4538 static int
4539 zfs_ioc_get_holds(zfs_cmd_t *zc)
4540 {
4541         nvlist_t *nvp;
4542         int error;
4543
4544         if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4545                 error = put_nvlist(zc, nvp);
4546                 nvlist_free(nvp);
4547         }
4548
4549         return (error);
4550 }
4551
4552 /*
4553  * inputs:
4554  * zc_guid              flags (ZEVENT_NONBLOCK)
4555  *
4556  * outputs:
4557  * zc_nvlist_dst        next nvlist event
4558  * zc_cookie            dropped events since last get
4559  * zc_cleanup_fd        cleanup-on-exit file descriptor
4560  */
4561 static int
4562 zfs_ioc_events_next(zfs_cmd_t *zc)
4563 {
4564         zfs_zevent_t *ze;
4565         nvlist_t *event = NULL;
4566         minor_t minor;
4567         uint64_t dropped = 0;
4568         int error;
4569
4570         error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4571         if (error != 0)
4572                 return (error);
4573
4574         do {
4575                 error = zfs_zevent_next(ze, &event,
4576                         &zc->zc_nvlist_dst_size, &dropped);
4577                 if (event != NULL) {
4578                         zc->zc_cookie = dropped;
4579                         error = put_nvlist(zc, event);
4580                         nvlist_free(event);
4581                 }
4582
4583                 if (zc->zc_guid & ZEVENT_NONBLOCK)
4584                         break;
4585
4586                 if ((error == 0) || (error != ENOENT))
4587                         break;
4588
4589                 error = zfs_zevent_wait(ze);
4590                 if (error)
4591                         break;
4592         } while (1);
4593
4594         zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4595
4596         return (error);
4597 }
4598
4599 /*
4600  * outputs:
4601  * zc_cookie            cleared events count
4602  */
4603 static int
4604 zfs_ioc_events_clear(zfs_cmd_t *zc)
4605 {
4606         int count;
4607
4608         zfs_zevent_drain_all(&count);
4609         zc->zc_cookie = count;
4610
4611         return 0;
4612 }
4613
4614 /*
4615  * pool create, destroy, and export don't log the history as part of
4616  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4617  * do the logging of those commands.
4618  */
4619 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4620         { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4621             POOL_CHECK_NONE },
4622         { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4623             POOL_CHECK_NONE },
4624         { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4625             POOL_CHECK_NONE },
4626         { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4627             POOL_CHECK_NONE },
4628         { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4629             POOL_CHECK_NONE },
4630         { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4631             POOL_CHECK_NONE },
4632         { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4633             POOL_CHECK_NONE },
4634         { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4635             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4636         { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4637             POOL_CHECK_READONLY },
4638         { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4639             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4640         { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4641             POOL_CHECK_NONE },
4642         { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4643             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4644         { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4645             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4646         { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4647             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4648         { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4649             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4650         { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4651             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4652         { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4653             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4654         { zfs_ioc_vdev_setfru,  zfs_secpolicy_config, POOL_NAME, B_FALSE,
4655             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4656         { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4657             POOL_CHECK_SUSPENDED },
4658         { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4659             POOL_CHECK_NONE },
4660         { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4661             POOL_CHECK_SUSPENDED },
4662         { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4663             POOL_CHECK_SUSPENDED },
4664         { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4665             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4666         { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4667             POOL_CHECK_NONE },
4668         { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4669             POOL_CHECK_NONE },
4670         { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4671             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4672         { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4673             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4674         { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4675             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4676         { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4677             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4678         { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4679             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4680         { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4681             POOL_CHECK_NONE },
4682         { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4683             POOL_CHECK_NONE },
4684         { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4685             POOL_CHECK_NONE },
4686         { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4687             POOL_CHECK_NONE },
4688         { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4689             POOL_CHECK_NONE },
4690         { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4691             POOL_CHECK_NONE },
4692         { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4693             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4694         { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4695             B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4696         { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4697             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4698         { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4699             POOL_CHECK_NONE },
4700         { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4701             POOL_CHECK_SUSPENDED },
4702         { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4703             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4704         { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4705             POOL_CHECK_NONE },
4706         { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4707             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4708         { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4709             POOL_CHECK_NONE },
4710         { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4711             POOL_CHECK_NONE },
4712         { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4713             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4714         { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4715             POOL_CHECK_NONE },
4716         { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4717             B_FALSE, POOL_CHECK_NONE },
4718         { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4719             B_FALSE, POOL_CHECK_NONE },
4720         { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4721             DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4722         { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4723             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4724         { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4725             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4726         { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4727             POOL_CHECK_SUSPENDED },
4728         { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4729             POOL_CHECK_NONE },
4730         { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4731             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4732         { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4733             POOL_CHECK_NONE },
4734         { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4735             POOL_CHECK_NONE },
4736         { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4737             B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4738         { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4739             POOL_CHECK_SUSPENDED },
4740         { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4741             POOL_CHECK_NONE },
4742         { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4743             POOL_CHECK_NONE },
4744 };
4745
4746 int
4747 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4748     zfs_ioc_poolcheck_t check)
4749 {
4750         spa_t *spa;
4751         int error;
4752
4753         ASSERT(type == POOL_NAME || type == DATASET_NAME);
4754
4755         if (check & POOL_CHECK_NONE)
4756                 return (0);
4757
4758         error = spa_open(name, &spa, FTAG);
4759         if (error == 0) {
4760                 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4761                         error = EAGAIN;
4762                 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4763                         error = EROFS;
4764                 spa_close(spa, FTAG);
4765         }
4766         return (error);
4767 }
4768
4769 static void *
4770 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4771 {
4772         zfsdev_state_t *zs;
4773
4774         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4775
4776         for (zs = list_head(&zfsdev_state_list); zs != NULL;
4777              zs = list_next(&zfsdev_state_list, zs)) {
4778                 if (zs->zs_minor == minor) {
4779                         switch (which) {
4780                                 case ZST_ONEXIT:  return (zs->zs_onexit);
4781                                 case ZST_ZEVENT:  return (zs->zs_zevent);
4782                                 case ZST_ALL:     return (zs);
4783                         }
4784                 }
4785         }
4786
4787         return NULL;
4788 }
4789
4790 void *
4791 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4792 {
4793         void *ptr;
4794
4795         mutex_enter(&zfsdev_state_lock);
4796         ptr = zfsdev_get_state_impl(minor, which);
4797         mutex_exit(&zfsdev_state_lock);
4798
4799         return ptr;
4800 }
4801
4802 minor_t
4803 zfsdev_getminor(struct file *filp)
4804 {
4805         ASSERT(filp != NULL);
4806         ASSERT(filp->private_data != NULL);
4807
4808         return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4809 }
4810
4811 /*
4812  * Find a free minor number.  The zfsdev_state_list is expected to
4813  * be short since it is only a list of currently open file handles.
4814  */
4815 minor_t
4816 zfsdev_minor_alloc(void)
4817 {
4818         static minor_t last_minor = 0;
4819         minor_t m;
4820
4821         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4822
4823         for (m = last_minor + 1; m != last_minor; m++) {
4824                 if (m > ZFSDEV_MAX_MINOR)
4825                         m = 1;
4826                 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
4827                         last_minor = m;
4828                         return (m);
4829                 }
4830         }
4831
4832         return (0);
4833 }
4834
4835 static int
4836 zfsdev_state_init(struct file *filp)
4837 {
4838         zfsdev_state_t *zs;
4839         minor_t minor;
4840
4841         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4842
4843         minor = zfsdev_minor_alloc();
4844         if (minor == 0)
4845                 return (ENXIO);
4846
4847         zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
4848         if (zs == NULL)
4849                 return (ENOMEM);
4850
4851         zs->zs_file = filp;
4852         zs->zs_minor = minor;
4853         filp->private_data = zs;
4854
4855         zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
4856         zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
4857
4858         list_insert_tail(&zfsdev_state_list, zs);
4859
4860         return (0);
4861 }
4862
4863 static int
4864 zfsdev_state_destroy(struct file *filp)
4865 {
4866         zfsdev_state_t *zs;
4867
4868         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4869         ASSERT(filp->private_data != NULL);
4870
4871         zs = filp->private_data;
4872         zfs_onexit_destroy(zs->zs_onexit);
4873         zfs_zevent_destroy(zs->zs_zevent);
4874
4875         list_remove(&zfsdev_state_list, zs);
4876         kmem_free(zs, sizeof(zfsdev_state_t));
4877
4878         return 0;
4879 }
4880
4881 static int
4882 zfsdev_open(struct inode *ino, struct file *filp)
4883 {
4884         int error;
4885
4886         mutex_enter(&zfsdev_state_lock);
4887         error = zfsdev_state_init(filp);
4888         mutex_exit(&zfsdev_state_lock);
4889
4890         return (-error);
4891 }
4892
4893 static int
4894 zfsdev_release(struct inode *ino, struct file *filp)
4895 {
4896         int error;
4897
4898         mutex_enter(&zfsdev_state_lock);
4899         error = zfsdev_state_destroy(filp);
4900         mutex_exit(&zfsdev_state_lock);
4901
4902         return (-error);
4903 }
4904
4905 static long
4906 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
4907 {
4908         zfs_cmd_t *zc;
4909         uint_t vec;
4910         int error, rc, flag = 0;
4911
4912         vec = cmd - ZFS_IOC;
4913         if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4914                 return (-EINVAL);
4915
4916         zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
4917
4918         error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4919         if (error != 0)
4920                 error = EFAULT;
4921
4922         if ((error == 0) && !(flag & FKIOCTL))
4923                 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
4924
4925         /*
4926          * Ensure that all pool/dataset names are valid before we pass down to
4927          * the lower layers.
4928          */
4929         if (error == 0) {
4930                 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4931                 zc->zc_iflags = flag & FKIOCTL;
4932                 switch (zfs_ioc_vec[vec].zvec_namecheck) {
4933                 case POOL_NAME:
4934                         if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4935                                 error = EINVAL;
4936                         error = pool_status_check(zc->zc_name,
4937                             zfs_ioc_vec[vec].zvec_namecheck,
4938                             zfs_ioc_vec[vec].zvec_pool_check);
4939                         break;
4940
4941                 case DATASET_NAME:
4942                         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4943                                 error = EINVAL;
4944                         error = pool_status_check(zc->zc_name,
4945                             zfs_ioc_vec[vec].zvec_namecheck,
4946                             zfs_ioc_vec[vec].zvec_pool_check);
4947                         break;
4948
4949                 case NO_NAME:
4950                         break;
4951                 }
4952         }
4953
4954         if (error == 0)
4955                 error = zfs_ioc_vec[vec].zvec_func(zc);
4956
4957         rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4958         if (error == 0) {
4959                 if (rc != 0)
4960                         error = EFAULT;
4961                 if (zfs_ioc_vec[vec].zvec_his_log)
4962                         zfs_log_history(zc);
4963         }
4964
4965         kmem_free(zc, sizeof (zfs_cmd_t));
4966         return (-error);
4967 }
4968
4969 #ifdef CONFIG_COMPAT
4970 static long
4971 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
4972 {
4973         return zfsdev_ioctl(filp, cmd, arg);
4974 }
4975 #else
4976 #define zfsdev_compat_ioctl   NULL
4977 #endif
4978
4979 static const struct file_operations zfsdev_fops = {
4980         .open            = zfsdev_open,
4981         .release         = zfsdev_release,
4982         .unlocked_ioctl  = zfsdev_ioctl,
4983         .compat_ioctl    = zfsdev_compat_ioctl,
4984         .owner           = THIS_MODULE,
4985 };
4986
4987 static struct miscdevice zfs_misc = {
4988         .minor          = MISC_DYNAMIC_MINOR,
4989         .name           = ZFS_DRIVER,
4990         .fops           = &zfsdev_fops,
4991 };
4992
4993 static int
4994 zfs_attach(void)
4995 {
4996         int error;
4997
4998         mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
4999         list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5000             offsetof(zfsdev_state_t, zs_next));
5001
5002         error = misc_register(&zfs_misc);
5003         if (error) {
5004                 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5005                 return (error);
5006         }
5007
5008         return (0);
5009 }
5010
5011 static void
5012 zfs_detach(void)
5013 {
5014         int error;
5015
5016         error = misc_deregister(&zfs_misc);
5017         if (error)
5018                 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5019
5020         mutex_destroy(&zfsdev_state_lock);
5021         list_destroy(&zfsdev_state_list);
5022 }
5023
5024 uint_t zfs_fsyncer_key;
5025 extern uint_t rrw_tsd_key;
5026
5027 #ifdef DEBUG
5028 #define ZFS_DEBUG_STR   " (DEBUG mode)"
5029 #else
5030 #define ZFS_DEBUG_STR   ""
5031 #endif
5032
5033 int
5034 _init(void)
5035 {
5036         int error;
5037
5038         spa_init(FREAD | FWRITE);
5039         zfs_init();
5040
5041         if ((error = zvol_init()) != 0)
5042                 goto out1;
5043
5044         if ((error = zfs_attach()) != 0)
5045                 goto out2;
5046
5047         tsd_create(&zfs_fsyncer_key, NULL);
5048         tsd_create(&rrw_tsd_key, NULL);
5049
5050         printk(KERN_NOTICE "ZFS: Loaded module v%s%s, "
5051                "ZFS pool version %s, ZFS filesystem version %s\n",
5052                ZFS_META_VERSION, ZFS_DEBUG_STR,
5053                SPA_VERSION_STRING, ZPL_VERSION_STRING);
5054
5055         return (0);
5056
5057 out2:
5058         (void) zvol_fini();
5059 out1:
5060         zfs_fini();
5061         spa_fini();
5062         printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s%s"
5063                ", rc = %d\n", ZFS_META_VERSION, ZFS_DEBUG_STR, error);
5064
5065         return (error);
5066 }
5067
5068 int
5069 _fini(void)
5070 {
5071         zfs_detach();
5072         zvol_fini();
5073         zfs_fini();
5074         spa_fini();
5075
5076         tsd_destroy(&zfs_fsyncer_key);
5077         tsd_destroy(&rrw_tsd_key);
5078
5079         printk(KERN_NOTICE "ZFS: Unloaded module v%s%s\n",
5080                ZFS_META_VERSION, ZFS_DEBUG_STR);
5081
5082         return (0);
5083 }
5084
5085 #ifdef HAVE_SPL
5086 spl_module_init(_init);
5087 spl_module_exit(_fini);
5088
5089 MODULE_DESCRIPTION("ZFS");
5090 MODULE_AUTHOR(ZFS_META_AUTHOR);
5091 MODULE_LICENSE(ZFS_META_LICENSE);
5092 #endif /* HAVE_SPL */