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