Fix missing 'zpool events'
[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_ZPL
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_ZPL */
620
621 int
622 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
623 {
624 #ifdef HAVE_ZPL
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_ZPL */
636 }
637
638 int
639 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
640 {
641 #ifdef HAVE_ZPL
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_ZPL */
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
3311         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3312         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3313             strchr(zc->zc_value, '%'))
3314                 return (EINVAL);
3315
3316         /*
3317          * Unmount snapshot unless we're doing a recursive rename,
3318          * in which case the dataset code figures out which snapshots
3319          * to unmount.
3320          */
3321         if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3322             zc->zc_objset_type == DMU_OST_ZFS) {
3323                 int err = zfs_unmount_snap(zc->zc_name, NULL);
3324                 if (err)
3325                         return (err);
3326         }
3327         if (zc->zc_objset_type == DMU_OST_ZVOL)
3328                 (void) zvol_remove_minor(zc->zc_name);
3329         return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive));
3330 }
3331
3332 static int
3333 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3334 {
3335         const char *propname = nvpair_name(pair);
3336         boolean_t issnap = (strchr(dsname, '@') != NULL);
3337         zfs_prop_t prop = zfs_name_to_prop(propname);
3338         uint64_t intval;
3339         int err;
3340
3341         if (prop == ZPROP_INVAL) {
3342                 if (zfs_prop_user(propname)) {
3343                         if ((err = zfs_secpolicy_write_perms(dsname,
3344                             ZFS_DELEG_PERM_USERPROP, cr)))
3345                                 return (err);
3346                         return (0);
3347                 }
3348
3349                 if (!issnap && zfs_prop_userquota(propname)) {
3350                         const char *perm = NULL;
3351                         const char *uq_prefix =
3352                             zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3353                         const char *gq_prefix =
3354                             zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3355
3356                         if (strncmp(propname, uq_prefix,
3357                             strlen(uq_prefix)) == 0) {
3358                                 perm = ZFS_DELEG_PERM_USERQUOTA;
3359                         } else if (strncmp(propname, gq_prefix,
3360                             strlen(gq_prefix)) == 0) {
3361                                 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3362                         } else {
3363                                 /* USERUSED and GROUPUSED are read-only */
3364                                 return (EINVAL);
3365                         }
3366
3367                         if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3368                                 return (err);
3369                         return (0);
3370                 }
3371
3372                 return (EINVAL);
3373         }
3374
3375         if (issnap)
3376                 return (EINVAL);
3377
3378         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3379                 /*
3380                  * dsl_prop_get_all_impl() returns properties in this
3381                  * format.
3382                  */
3383                 nvlist_t *attrs;
3384                 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3385                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3386                     &pair) == 0);
3387         }
3388
3389         /*
3390          * Check that this value is valid for this pool version
3391          */
3392         switch (prop) {
3393         case ZFS_PROP_COMPRESSION:
3394                 /*
3395                  * If the user specified gzip compression, make sure
3396                  * the SPA supports it. We ignore any errors here since
3397                  * we'll catch them later.
3398                  */
3399                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3400                     nvpair_value_uint64(pair, &intval) == 0) {
3401                         if (intval >= ZIO_COMPRESS_GZIP_1 &&
3402                             intval <= ZIO_COMPRESS_GZIP_9 &&
3403                             zfs_earlier_version(dsname,
3404                             SPA_VERSION_GZIP_COMPRESSION)) {
3405                                 return (ENOTSUP);
3406                         }
3407
3408                         if (intval == ZIO_COMPRESS_ZLE &&
3409                             zfs_earlier_version(dsname,
3410                             SPA_VERSION_ZLE_COMPRESSION))
3411                                 return (ENOTSUP);
3412
3413                         /*
3414                          * If this is a bootable dataset then
3415                          * verify that the compression algorithm
3416                          * is supported for booting. We must return
3417                          * something other than ENOTSUP since it
3418                          * implies a downrev pool version.
3419                          */
3420                         if (zfs_is_bootfs(dsname) &&
3421                             !BOOTFS_COMPRESS_VALID(intval)) {
3422                                 return (ERANGE);
3423                         }
3424                 }
3425                 break;
3426
3427         case ZFS_PROP_COPIES:
3428                 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3429                         return (ENOTSUP);
3430                 break;
3431
3432         case ZFS_PROP_DEDUP:
3433                 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3434                         return (ENOTSUP);
3435                 break;
3436
3437         case ZFS_PROP_SHARESMB:
3438                 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3439                         return (ENOTSUP);
3440                 break;
3441
3442         case ZFS_PROP_ACLINHERIT:
3443                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3444                     nvpair_value_uint64(pair, &intval) == 0) {
3445                         if (intval == ZFS_ACL_PASSTHROUGH_X &&
3446                             zfs_earlier_version(dsname,
3447                             SPA_VERSION_PASSTHROUGH_X))
3448                                 return (ENOTSUP);
3449                 }
3450                 break;
3451         default:
3452                 break;
3453         }
3454
3455         return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3456 }
3457
3458 /*
3459  * Removes properties from the given props list that fail permission checks
3460  * needed to clear them and to restore them in case of a receive error. For each
3461  * property, make sure we have both set and inherit permissions.
3462  *
3463  * Returns the first error encountered if any permission checks fail. If the
3464  * caller provides a non-NULL errlist, it also gives the complete list of names
3465  * of all the properties that failed a permission check along with the
3466  * corresponding error numbers. The caller is responsible for freeing the
3467  * returned errlist.
3468  *
3469  * If every property checks out successfully, zero is returned and the list
3470  * pointed at by errlist is NULL.
3471  */
3472 static int
3473 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3474 {
3475         zfs_cmd_t *zc;
3476         nvpair_t *pair, *next_pair;
3477         nvlist_t *errors;
3478         int err, rv = 0;
3479
3480         if (props == NULL)
3481                 return (0);
3482
3483         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3484
3485         zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
3486         (void) strcpy(zc->zc_name, dataset);
3487         pair = nvlist_next_nvpair(props, NULL);
3488         while (pair != NULL) {
3489                 next_pair = nvlist_next_nvpair(props, pair);
3490
3491                 (void) strcpy(zc->zc_value, nvpair_name(pair));
3492                 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3493                     (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3494                         VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3495                         VERIFY(nvlist_add_int32(errors,
3496                             zc->zc_value, err) == 0);
3497                 }
3498                 pair = next_pair;
3499         }
3500         kmem_free(zc, sizeof (zfs_cmd_t));
3501
3502         if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3503                 nvlist_free(errors);
3504                 errors = NULL;
3505         } else {
3506                 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3507         }
3508
3509         if (errlist == NULL)
3510                 nvlist_free(errors);
3511         else
3512                 *errlist = errors;
3513
3514         return (rv);
3515 }
3516
3517 static boolean_t
3518 propval_equals(nvpair_t *p1, nvpair_t *p2)
3519 {
3520         if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3521                 /* dsl_prop_get_all_impl() format */
3522                 nvlist_t *attrs;
3523                 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3524                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3525                     &p1) == 0);
3526         }
3527
3528         if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3529                 nvlist_t *attrs;
3530                 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3531                 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3532                     &p2) == 0);
3533         }
3534
3535         if (nvpair_type(p1) != nvpair_type(p2))
3536                 return (B_FALSE);
3537
3538         if (nvpair_type(p1) == DATA_TYPE_STRING) {
3539                 char *valstr1, *valstr2;
3540
3541                 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3542                 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3543                 return (strcmp(valstr1, valstr2) == 0);
3544         } else {
3545                 uint64_t intval1, intval2;
3546
3547                 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3548                 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3549                 return (intval1 == intval2);
3550         }
3551 }
3552
3553 /*
3554  * Remove properties from props if they are not going to change (as determined
3555  * by comparison with origprops). Remove them from origprops as well, since we
3556  * do not need to clear or restore properties that won't change.
3557  */
3558 static void
3559 props_reduce(nvlist_t *props, nvlist_t *origprops)
3560 {
3561         nvpair_t *pair, *next_pair;
3562
3563         if (origprops == NULL)
3564                 return; /* all props need to be received */
3565
3566         pair = nvlist_next_nvpair(props, NULL);
3567         while (pair != NULL) {
3568                 const char *propname = nvpair_name(pair);
3569                 nvpair_t *match;
3570
3571                 next_pair = nvlist_next_nvpair(props, pair);
3572
3573                 if ((nvlist_lookup_nvpair(origprops, propname,
3574                     &match) != 0) || !propval_equals(pair, match))
3575                         goto next; /* need to set received value */
3576
3577                 /* don't clear the existing received value */
3578                 (void) nvlist_remove_nvpair(origprops, match);
3579                 /* don't bother receiving the property */
3580                 (void) nvlist_remove_nvpair(props, pair);
3581 next:
3582                 pair = next_pair;
3583         }
3584 }
3585
3586 #ifdef  DEBUG
3587 static boolean_t zfs_ioc_recv_inject_err;
3588 #endif
3589
3590 /*
3591  * inputs:
3592  * zc_name              name of containing filesystem
3593  * zc_nvlist_src{_size} nvlist of properties to apply
3594  * zc_value             name of snapshot to create
3595  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
3596  * zc_cookie            file descriptor to recv from
3597  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
3598  * zc_guid              force flag
3599  * zc_cleanup_fd        cleanup-on-exit file descriptor
3600  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
3601  *
3602  * outputs:
3603  * zc_cookie            number of bytes read
3604  * zc_nvlist_dst{_size} error for each unapplied received property
3605  * zc_obj               zprop_errflags_t
3606  * zc_action_handle     handle for this guid/ds mapping
3607  */
3608 static int
3609 zfs_ioc_recv(zfs_cmd_t *zc)
3610 {
3611         file_t *fp;
3612         objset_t *os;
3613         dmu_recv_cookie_t drc;
3614         boolean_t force = (boolean_t)zc->zc_guid;
3615         int fd;
3616         int error = 0;
3617         int props_error = 0;
3618         nvlist_t *errors;
3619         offset_t off;
3620         nvlist_t *props = NULL; /* sent properties */
3621         nvlist_t *origprops = NULL; /* existing properties */
3622         objset_t *origin = NULL;
3623         char *tosnap;
3624         char tofs[ZFS_MAXNAMELEN];
3625         boolean_t first_recvd_props = B_FALSE;
3626
3627         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3628             strchr(zc->zc_value, '@') == NULL ||
3629             strchr(zc->zc_value, '%'))
3630                 return (EINVAL);
3631
3632         (void) strcpy(tofs, zc->zc_value);
3633         tosnap = strchr(tofs, '@');
3634         *tosnap++ = '\0';
3635
3636         if (zc->zc_nvlist_src != 0 &&
3637             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3638             zc->zc_iflags, &props)) != 0)
3639                 return (error);
3640
3641         fd = zc->zc_cookie;
3642         fp = getf(fd);
3643         if (fp == NULL) {
3644                 nvlist_free(props);
3645                 return (EBADF);
3646         }
3647
3648         VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3649
3650         if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3651                 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3652                     !dsl_prop_get_hasrecvd(os)) {
3653                         first_recvd_props = B_TRUE;
3654                 }
3655
3656                 /*
3657                  * If new received properties are supplied, they are to
3658                  * completely replace the existing received properties, so stash
3659                  * away the existing ones.
3660                  */
3661                 if (dsl_prop_get_received(os, &origprops) == 0) {
3662                         nvlist_t *errlist = NULL;
3663                         /*
3664                          * Don't bother writing a property if its value won't
3665                          * change (and avoid the unnecessary security checks).
3666                          *
3667                          * The first receive after SPA_VERSION_RECVD_PROPS is a
3668                          * special case where we blow away all local properties
3669                          * regardless.
3670                          */
3671                         if (!first_recvd_props)
3672                                 props_reduce(props, origprops);
3673                         if (zfs_check_clearable(tofs, origprops,
3674                             &errlist) != 0)
3675                                 (void) nvlist_merge(errors, errlist, 0);
3676                         nvlist_free(errlist);
3677                 }
3678
3679                 dmu_objset_rele(os, FTAG);
3680         }
3681
3682         if (zc->zc_string[0]) {
3683                 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3684                 if (error)
3685                         goto out;
3686         }
3687
3688         error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3689             &zc->zc_begin_record, force, origin, &drc);
3690         if (origin)
3691                 dmu_objset_rele(origin, FTAG);
3692         if (error)
3693                 goto out;
3694
3695         /*
3696          * Set properties before we receive the stream so that they are applied
3697          * to the new data. Note that we must call dmu_recv_stream() if
3698          * dmu_recv_begin() succeeds.
3699          */
3700         if (props) {
3701                 nvlist_t *errlist;
3702
3703                 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3704                         if (drc.drc_newfs) {
3705                                 if (spa_version(os->os_spa) >=
3706                                     SPA_VERSION_RECVD_PROPS)
3707                                         first_recvd_props = B_TRUE;
3708                         } else if (origprops != NULL) {
3709                                 if (clear_received_props(os, tofs, origprops,
3710                                     first_recvd_props ? NULL : props) != 0)
3711                                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3712                         } else {
3713                                 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3714                         }
3715                         dsl_prop_set_hasrecvd(os);
3716                 } else if (!drc.drc_newfs) {
3717                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3718                 }
3719
3720                 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3721                     props, &errlist);
3722                 (void) nvlist_merge(errors, errlist, 0);
3723                 nvlist_free(errlist);
3724         }
3725
3726         if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3727                 /*
3728                  * Caller made zc->zc_nvlist_dst less than the minimum expected
3729                  * size or supplied an invalid address.
3730                  */
3731                 props_error = EINVAL;
3732         }
3733
3734         off = fp->f_offset;
3735         error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3736             &zc->zc_action_handle);
3737
3738         if (error == 0) {
3739 #ifdef HAVE_ZPL
3740                 zfsvfs_t *zfsvfs = NULL;
3741
3742                 if (getzfsvfs(tofs, &zfsvfs) == 0) {
3743                         /* online recv */
3744                         int end_err;
3745
3746                         error = zfs_suspend_fs(zfsvfs);
3747                         /*
3748                          * If the suspend fails, then the recv_end will
3749                          * likely also fail, and clean up after itself.
3750                          */
3751                         end_err = dmu_recv_end(&drc);
3752                         if (error == 0)
3753                                 error = zfs_resume_fs(zfsvfs, tofs);
3754                         error = error ? error : end_err;
3755                         VFS_RELE(zfsvfs->z_vfs);
3756                 } else {
3757                         error = dmu_recv_end(&drc);
3758                 }
3759 #else
3760                 error = dmu_recv_end(&drc);
3761 #endif /* HAVE_ZPL */
3762         }
3763
3764         zc->zc_cookie = off - fp->f_offset;
3765         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3766                 fp->f_offset = off;
3767
3768 #ifdef  DEBUG
3769         if (zfs_ioc_recv_inject_err) {
3770                 zfs_ioc_recv_inject_err = B_FALSE;
3771                 error = 1;
3772         }
3773 #endif
3774         /*
3775          * On error, restore the original props.
3776          */
3777         if (error && props) {
3778                 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3779                         if (clear_received_props(os, tofs, props, NULL) != 0) {
3780                                 /*
3781                                  * We failed to clear the received properties.
3782                                  * Since we may have left a $recvd value on the
3783                                  * system, we can't clear the $hasrecvd flag.
3784                                  */
3785                                 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3786                         } else if (first_recvd_props) {
3787                                 dsl_prop_unset_hasrecvd(os);
3788                         }
3789                         dmu_objset_rele(os, FTAG);
3790                 } else if (!drc.drc_newfs) {
3791                         /* We failed to clear the received properties. */
3792                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3793                 }
3794
3795                 if (origprops == NULL && !drc.drc_newfs) {
3796                         /* We failed to stash the original properties. */
3797                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3798                 }
3799
3800                 /*
3801                  * dsl_props_set() will not convert RECEIVED to LOCAL on or
3802                  * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3803                  * explictly if we're restoring local properties cleared in the
3804                  * first new-style receive.
3805                  */
3806                 if (origprops != NULL &&
3807                     zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3808                     ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3809                     origprops, NULL) != 0) {
3810                         /*
3811                          * We stashed the original properties but failed to
3812                          * restore them.
3813                          */
3814                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
3815                 }
3816         }
3817 out:
3818         nvlist_free(props);
3819         nvlist_free(origprops);
3820         nvlist_free(errors);
3821         releasef(fd);
3822
3823         if (error == 0)
3824                 error = props_error;
3825
3826         return (error);
3827 }
3828
3829 /*
3830  * inputs:
3831  * zc_name      name of snapshot to send
3832  * zc_cookie    file descriptor to send stream to
3833  * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
3834  * zc_sendobj   objsetid of snapshot to send
3835  * zc_fromobj   objsetid of incremental fromsnap (may be zero)
3836  *
3837  * outputs: none
3838  */
3839 static int
3840 zfs_ioc_send(zfs_cmd_t *zc)
3841 {
3842         objset_t *fromsnap = NULL;
3843         objset_t *tosnap;
3844         file_t *fp;
3845         int error;
3846         offset_t off;
3847         dsl_dataset_t *ds;
3848         dsl_dataset_t *dsfrom = NULL;
3849         spa_t *spa;
3850         dsl_pool_t *dp;
3851
3852         error = spa_open(zc->zc_name, &spa, FTAG);
3853         if (error)
3854                 return (error);
3855
3856         dp = spa_get_dsl(spa);
3857         rw_enter(&dp->dp_config_rwlock, RW_READER);
3858         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3859         rw_exit(&dp->dp_config_rwlock);
3860         if (error) {
3861                 spa_close(spa, FTAG);
3862                 return (error);
3863         }
3864
3865         error = dmu_objset_from_ds(ds, &tosnap);
3866         if (error) {
3867                 dsl_dataset_rele(ds, FTAG);
3868                 spa_close(spa, FTAG);
3869                 return (error);
3870         }
3871
3872         if (zc->zc_fromobj != 0) {
3873                 rw_enter(&dp->dp_config_rwlock, RW_READER);
3874                 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3875                 rw_exit(&dp->dp_config_rwlock);
3876                 spa_close(spa, FTAG);
3877                 if (error) {
3878                         dsl_dataset_rele(ds, FTAG);
3879                         return (error);
3880                 }
3881                 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3882                 if (error) {
3883                         dsl_dataset_rele(dsfrom, FTAG);
3884                         dsl_dataset_rele(ds, FTAG);
3885                         return (error);
3886                 }
3887         } else {
3888                 spa_close(spa, FTAG);
3889         }
3890
3891         fp = getf(zc->zc_cookie);
3892         if (fp == NULL) {
3893                 dsl_dataset_rele(ds, FTAG);
3894                 if (dsfrom)
3895                         dsl_dataset_rele(dsfrom, FTAG);
3896                 return (EBADF);
3897         }
3898
3899         off = fp->f_offset;
3900         error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3901
3902         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3903                 fp->f_offset = off;
3904         releasef(zc->zc_cookie);
3905         if (dsfrom)
3906                 dsl_dataset_rele(dsfrom, FTAG);
3907         dsl_dataset_rele(ds, FTAG);
3908         return (error);
3909 }
3910
3911 static int
3912 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3913 {
3914         int id, error;
3915
3916         error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3917             &zc->zc_inject_record);
3918
3919         if (error == 0)
3920                 zc->zc_guid = (uint64_t)id;
3921
3922         return (error);
3923 }
3924
3925 static int
3926 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3927 {
3928         return (zio_clear_fault((int)zc->zc_guid));
3929 }
3930
3931 static int
3932 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3933 {
3934         int id = (int)zc->zc_guid;
3935         int error;
3936
3937         error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3938             &zc->zc_inject_record);
3939
3940         zc->zc_guid = id;
3941
3942         return (error);
3943 }
3944
3945 static int
3946 zfs_ioc_error_log(zfs_cmd_t *zc)
3947 {
3948         spa_t *spa;
3949         int error;
3950         size_t count = (size_t)zc->zc_nvlist_dst_size;
3951
3952         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3953                 return (error);
3954
3955         error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3956             &count);
3957         if (error == 0)
3958                 zc->zc_nvlist_dst_size = count;
3959         else
3960                 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3961
3962         spa_close(spa, FTAG);
3963
3964         return (error);
3965 }
3966
3967 static int
3968 zfs_ioc_clear(zfs_cmd_t *zc)
3969 {
3970         spa_t *spa;
3971         vdev_t *vd;
3972         int error;
3973
3974         /*
3975          * On zpool clear we also fix up missing slogs
3976          */
3977         mutex_enter(&spa_namespace_lock);
3978         spa = spa_lookup(zc->zc_name);
3979         if (spa == NULL) {
3980                 mutex_exit(&spa_namespace_lock);
3981                 return (EIO);
3982         }
3983         if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3984                 /* we need to let spa_open/spa_load clear the chains */
3985                 spa_set_log_state(spa, SPA_LOG_CLEAR);
3986         }
3987         spa->spa_last_open_failed = 0;
3988         mutex_exit(&spa_namespace_lock);
3989
3990         if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3991                 error = spa_open(zc->zc_name, &spa, FTAG);
3992         } else {
3993                 nvlist_t *policy;
3994                 nvlist_t *config = NULL;
3995
3996                 if (zc->zc_nvlist_src == 0)
3997                         return (EINVAL);
3998
3999                 if ((error = get_nvlist(zc->zc_nvlist_src,
4000                     zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4001                         error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4002                             policy, &config);
4003                         if (config != NULL) {
4004                                 int err;
4005
4006                                 if ((err = put_nvlist(zc, config)) != 0)
4007                                         error = err;
4008                                 nvlist_free(config);
4009                         }
4010                         nvlist_free(policy);
4011                 }
4012         }
4013
4014         if (error)
4015                 return (error);
4016
4017         spa_vdev_state_enter(spa, SCL_NONE);
4018
4019         if (zc->zc_guid == 0) {
4020                 vd = NULL;
4021         } else {
4022                 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4023                 if (vd == NULL) {
4024                         (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4025                         spa_close(spa, FTAG);
4026                         return (ENODEV);
4027                 }
4028         }
4029
4030         vdev_clear(spa, vd);
4031
4032         (void) spa_vdev_state_exit(spa, NULL, 0);
4033
4034         /*
4035          * Resume any suspended I/Os.
4036          */
4037         if (zio_resume(spa) != 0)
4038                 error = EIO;
4039
4040         spa_close(spa, FTAG);
4041
4042         return (error);
4043 }
4044
4045 /*
4046  * inputs:
4047  * zc_name      name of filesystem
4048  * zc_value     name of origin snapshot
4049  *
4050  * outputs:
4051  * zc_string    name of conflicting snapshot, if there is one
4052  */
4053 static int
4054 zfs_ioc_promote(zfs_cmd_t *zc)
4055 {
4056         char *cp;
4057
4058         /*
4059          * We don't need to unmount *all* the origin fs's snapshots, but
4060          * it's easier.
4061          */
4062         cp = strchr(zc->zc_value, '@');
4063         if (cp)
4064                 *cp = '\0';
4065         (void) dmu_objset_find(zc->zc_value,
4066             zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4067         return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4068 }
4069
4070 /*
4071  * Retrieve a single {user|group}{used|quota}@... property.
4072  *
4073  * inputs:
4074  * zc_name      name of filesystem
4075  * zc_objset_type zfs_userquota_prop_t
4076  * zc_value     domain name (eg. "S-1-234-567-89")
4077  * zc_guid      RID/UID/GID
4078  *
4079  * outputs:
4080  * zc_cookie    property value
4081  */
4082 static int
4083 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4084 {
4085 #ifdef HAVE_ZPL
4086         zfsvfs_t *zfsvfs;
4087         int error;
4088
4089         if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4090                 return (EINVAL);
4091
4092         error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4093         if (error)
4094                 return (error);
4095
4096         error = zfs_userspace_one(zfsvfs,
4097             zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4098         zfsvfs_rele(zfsvfs, FTAG);
4099
4100         return (error);
4101 #else
4102         return (ENOTSUP);
4103 #endif /* HAVE_ZPL */
4104 }
4105
4106 /*
4107  * inputs:
4108  * zc_name              name of filesystem
4109  * zc_cookie            zap cursor
4110  * zc_objset_type       zfs_userquota_prop_t
4111  * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4112  *
4113  * outputs:
4114  * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4115  * zc_cookie    zap cursor
4116  */
4117 static int
4118 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4119 {
4120 #ifdef HAVE_ZPL
4121         zfsvfs_t *zfsvfs;
4122         int bufsize = zc->zc_nvlist_dst_size;
4123
4124         if (bufsize <= 0)
4125                 return (ENOMEM);
4126
4127         int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
4128         if (error)
4129                 return (error);
4130
4131         void *buf = kmem_alloc(bufsize, KM_SLEEP);
4132
4133         error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
4134             buf, &zc->zc_nvlist_dst_size);
4135
4136         if (error == 0) {
4137                 error = xcopyout(buf,
4138                     (void *)(uintptr_t)zc->zc_nvlist_dst,
4139                     zc->zc_nvlist_dst_size);
4140         }
4141         kmem_free(buf, bufsize);
4142         zfsvfs_rele(zfsvfs, FTAG);
4143
4144         return (error);
4145 #else
4146         return (ENOTSUP);
4147 #endif /* HAVE_ZPL */
4148 }
4149
4150 /*
4151  * inputs:
4152  * zc_name              name of filesystem
4153  *
4154  * outputs:
4155  * none
4156  */
4157 static int
4158 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4159 {
4160 #ifdef HAVE_ZPL
4161         objset_t *os;
4162         int error = 0;
4163         zfsvfs_t *zfsvfs;
4164
4165         if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
4166                 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
4167                         /*
4168                          * If userused is not enabled, it may be because the
4169                          * objset needs to be closed & reopened (to grow the
4170                          * objset_phys_t).  Suspend/resume the fs will do that.
4171                          */
4172                         error = zfs_suspend_fs(zfsvfs);
4173                         if (error == 0)
4174                                 error = zfs_resume_fs(zfsvfs, zc->zc_name);
4175                 }
4176                 if (error == 0)
4177                         error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
4178                 VFS_RELE(zfsvfs->z_vfs);
4179         } else {
4180                 /* XXX kind of reading contents without owning */
4181                 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4182                 if (error)
4183                         return (error);
4184
4185                 error = dmu_objset_userspace_upgrade(os);
4186                 dmu_objset_rele(os, FTAG);
4187         }
4188
4189         return (error);
4190 #else
4191         return (ENOTSUP);
4192 #endif /* HAVE_ZPL */
4193 }
4194
4195 /*
4196  * We don't want to have a hard dependency
4197  * against some special symbols in sharefs
4198  * nfs, and smbsrv.  Determine them if needed when
4199  * the first file system is shared.
4200  * Neither sharefs, nfs or smbsrv are unloadable modules.
4201  */
4202 #ifdef HAVE_ZPL
4203 int (*znfsexport_fs)(void *arg);
4204 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
4205 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
4206
4207 int zfs_nfsshare_inited;
4208 int zfs_smbshare_inited;
4209
4210 ddi_modhandle_t nfs_mod;
4211 ddi_modhandle_t sharefs_mod;
4212 ddi_modhandle_t smbsrv_mod;
4213 kmutex_t zfs_share_lock;
4214
4215 static int
4216 zfs_init_sharefs()
4217 {
4218         int error;
4219
4220         ASSERT(MUTEX_HELD(&zfs_share_lock));
4221         /* Both NFS and SMB shares also require sharetab support. */
4222         if (sharefs_mod == NULL && ((sharefs_mod =
4223             ddi_modopen("fs/sharefs",
4224             KRTLD_MODE_FIRST, &error)) == NULL)) {
4225                 return (ENOSYS);
4226         }
4227         if (zshare_fs == NULL && ((zshare_fs =
4228             (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
4229             ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
4230                 return (ENOSYS);
4231         }
4232         return (0);
4233 }
4234 #endif /* HAVE_ZPL */
4235
4236 static int
4237 zfs_ioc_share(zfs_cmd_t *zc)
4238 {
4239 #ifdef HAVE_ZPL
4240         int error;
4241         int opcode;
4242
4243         switch (zc->zc_share.z_sharetype) {
4244         case ZFS_SHARE_NFS:
4245         case ZFS_UNSHARE_NFS:
4246                 if (zfs_nfsshare_inited == 0) {
4247                         mutex_enter(&zfs_share_lock);
4248                         if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
4249                             KRTLD_MODE_FIRST, &error)) == NULL)) {
4250                                 mutex_exit(&zfs_share_lock);
4251                                 return (ENOSYS);
4252                         }
4253                         if (znfsexport_fs == NULL &&
4254                             ((znfsexport_fs = (int (*)(void *))
4255                             ddi_modsym(nfs_mod,
4256                             "nfs_export", &error)) == NULL)) {
4257                                 mutex_exit(&zfs_share_lock);
4258                                 return (ENOSYS);
4259                         }
4260                         error = zfs_init_sharefs();
4261                         if (error) {
4262                                 mutex_exit(&zfs_share_lock);
4263                                 return (ENOSYS);
4264                         }
4265                         zfs_nfsshare_inited = 1;
4266                         mutex_exit(&zfs_share_lock);
4267                 }
4268                 break;
4269         case ZFS_SHARE_SMB:
4270         case ZFS_UNSHARE_SMB:
4271                 if (zfs_smbshare_inited == 0) {
4272                         mutex_enter(&zfs_share_lock);
4273                         if (smbsrv_mod == NULL && ((smbsrv_mod =
4274                             ddi_modopen("drv/smbsrv",
4275                             KRTLD_MODE_FIRST, &error)) == NULL)) {
4276                                 mutex_exit(&zfs_share_lock);
4277                                 return (ENOSYS);
4278                         }
4279                         if (zsmbexport_fs == NULL && ((zsmbexport_fs =
4280                             (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
4281                             "smb_server_share", &error)) == NULL)) {
4282                                 mutex_exit(&zfs_share_lock);
4283                                 return (ENOSYS);
4284                         }
4285                         error = zfs_init_sharefs();
4286                         if (error) {
4287                                 mutex_exit(&zfs_share_lock);
4288                                 return (ENOSYS);
4289                         }
4290                         zfs_smbshare_inited = 1;
4291                         mutex_exit(&zfs_share_lock);
4292                 }
4293                 break;
4294         default:
4295                 return (EINVAL);
4296         }
4297
4298         switch (zc->zc_share.z_sharetype) {
4299         case ZFS_SHARE_NFS:
4300         case ZFS_UNSHARE_NFS:
4301                 if (error =
4302                     znfsexport_fs((void *)
4303                     (uintptr_t)zc->zc_share.z_exportdata))
4304                         return (error);
4305                 break;
4306         case ZFS_SHARE_SMB:
4307         case ZFS_UNSHARE_SMB:
4308                 if (error = zsmbexport_fs((void *)
4309                     (uintptr_t)zc->zc_share.z_exportdata,
4310                     zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
4311                     B_TRUE: B_FALSE)) {
4312                         return (error);
4313                 }
4314                 break;
4315         }
4316
4317         opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
4318             zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
4319             SHAREFS_ADD : SHAREFS_REMOVE;
4320
4321         /*
4322          * Add or remove share from sharetab
4323          */
4324         error = zshare_fs(opcode,
4325             (void *)(uintptr_t)zc->zc_share.z_sharedata,
4326             zc->zc_share.z_sharemax);
4327
4328         return (error);
4329 #else
4330         return (ENOTSUP);
4331 #endif /* HAVE_ZPL */
4332 }
4333
4334 ace_t full_access[] = {
4335         {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4336 };
4337
4338 /*
4339  * inputs:
4340  * zc_name              name of containing filesystem
4341  * zc_obj               object # beyond which we want next in-use object #
4342  *
4343  * outputs:
4344  * zc_obj               next in-use object #
4345  */
4346 static int
4347 zfs_ioc_next_obj(zfs_cmd_t *zc)
4348 {
4349         objset_t *os = NULL;
4350         int error;
4351
4352         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4353         if (error)
4354                 return (error);
4355
4356         error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4357             os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4358
4359         dmu_objset_rele(os, FTAG);
4360         return (error);
4361 }
4362
4363 /*
4364  * inputs:
4365  * zc_name              name of filesystem
4366  * zc_value             prefix name for snapshot
4367  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4368  *
4369  * outputs:
4370  */
4371 static int
4372 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4373 {
4374         char *snap_name;
4375         int error;
4376
4377         snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4378             (u_longlong_t)ddi_get_lbolt64());
4379
4380         if (strlen(snap_name) >= MAXNAMELEN) {
4381                 strfree(snap_name);
4382                 return (E2BIG);
4383         }
4384
4385         error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4386             NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4387         if (error != 0) {
4388                 strfree(snap_name);
4389                 return (error);
4390         }
4391
4392         (void) strcpy(zc->zc_value, snap_name);
4393         strfree(snap_name);
4394         return (0);
4395 }
4396
4397 /*
4398  * inputs:
4399  * zc_name              name of "to" snapshot
4400  * zc_value             name of "from" snapshot
4401  * zc_cookie            file descriptor to write diff data on
4402  *
4403  * outputs:
4404  * dmu_diff_record_t's to the file descriptor
4405  */
4406 static int
4407 zfs_ioc_diff(zfs_cmd_t *zc)
4408 {
4409         objset_t *fromsnap;
4410         objset_t *tosnap;
4411         file_t *fp;
4412         offset_t off;
4413         int error;
4414
4415         error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4416         if (error)
4417                 return (error);
4418
4419         error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4420         if (error) {
4421                 dmu_objset_rele(tosnap, FTAG);
4422                 return (error);
4423         }
4424
4425         fp = getf(zc->zc_cookie);
4426         if (fp == NULL) {
4427                 dmu_objset_rele(fromsnap, FTAG);
4428                 dmu_objset_rele(tosnap, FTAG);
4429                 return (EBADF);
4430         }
4431
4432         off = fp->f_offset;
4433
4434         error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4435
4436         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4437                 fp->f_offset = off;
4438         releasef(zc->zc_cookie);
4439
4440         dmu_objset_rele(fromsnap, FTAG);
4441         dmu_objset_rele(tosnap, FTAG);
4442         return (error);
4443 }
4444
4445 /*
4446  * Remove all ACL files in shares dir
4447  */
4448 #ifdef HAVE_ZPL
4449 static int
4450 zfs_smb_acl_purge(znode_t *dzp)
4451 {
4452         zap_cursor_t    zc;
4453         zap_attribute_t zap;
4454         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4455         int error;
4456
4457         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
4458             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4459             zap_cursor_advance(&zc)) {
4460                 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4461                     NULL, 0)) != 0)
4462                         break;
4463         }
4464         zap_cursor_fini(&zc);
4465         return (error);
4466 }
4467 #endif /* HAVE ZPL */
4468
4469 static int
4470 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4471 {
4472 #ifdef HAVE_ZPL
4473         vnode_t *vp;
4474         znode_t *dzp;
4475         vnode_t *resourcevp = NULL;
4476         znode_t *sharedir;
4477         zfsvfs_t *zfsvfs;
4478         nvlist_t *nvlist;
4479         char *src, *target;
4480         vattr_t vattr;
4481         vsecattr_t vsec;
4482         int error = 0;
4483
4484         if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4485             NO_FOLLOW, NULL, &vp)) != 0)
4486                 return (error);
4487
4488         /* Now make sure mntpnt and dataset are ZFS */
4489
4490         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4491             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4492             zc->zc_name) != 0)) {
4493                 VN_RELE(vp);
4494                 return (EINVAL);
4495         }
4496
4497         dzp = VTOZ(vp);
4498         zfsvfs = dzp->z_zfsvfs;
4499         ZFS_ENTER(zfsvfs);
4500
4501         /*
4502          * Create share dir if its missing.
4503          */
4504         mutex_enter(&zfsvfs->z_lock);
4505         if (zfsvfs->z_shares_dir == 0) {
4506                 dmu_tx_t *tx;
4507
4508                 tx = dmu_tx_create(zfsvfs->z_os);
4509                 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4510                     ZFS_SHARES_DIR);
4511                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4512                 error = dmu_tx_assign(tx, TXG_WAIT);
4513                 if (error) {
4514                         dmu_tx_abort(tx);
4515                 } else {
4516                         error = zfs_create_share_dir(zfsvfs, tx);
4517                         dmu_tx_commit(tx);
4518                 }
4519                 if (error) {
4520                         mutex_exit(&zfsvfs->z_lock);
4521                         VN_RELE(vp);
4522                         ZFS_EXIT(zfsvfs);
4523                         return (error);
4524                 }
4525         }
4526         mutex_exit(&zfsvfs->z_lock);
4527
4528         ASSERT(zfsvfs->z_shares_dir);
4529         if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
4530                 VN_RELE(vp);
4531                 ZFS_EXIT(zfsvfs);
4532                 return (error);
4533         }
4534
4535         switch (zc->zc_cookie) {
4536         case ZFS_SMB_ACL_ADD:
4537                 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4538                 vattr.va_type = VREG;
4539                 vattr.va_mode = S_IFREG|0777;
4540                 vattr.va_uid = 0;
4541                 vattr.va_gid = 0;
4542
4543                 vsec.vsa_mask = VSA_ACE;
4544                 vsec.vsa_aclentp = &full_access;
4545                 vsec.vsa_aclentsz = sizeof (full_access);
4546                 vsec.vsa_aclcnt = 1;
4547
4548                 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4549                     &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4550                 if (resourcevp)
4551                         VN_RELE(resourcevp);
4552                 break;
4553
4554         case ZFS_SMB_ACL_REMOVE:
4555                 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4556                     NULL, 0);
4557                 break;
4558
4559         case ZFS_SMB_ACL_RENAME:
4560                 if ((error = get_nvlist(zc->zc_nvlist_src,
4561                     zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4562                         VN_RELE(vp);
4563                         ZFS_EXIT(zfsvfs);
4564                         return (error);
4565                 }
4566                 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4567                     nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4568                     &target)) {
4569                         VN_RELE(vp);
4570                         VN_RELE(ZTOV(sharedir));
4571                         ZFS_EXIT(zfsvfs);
4572                         nvlist_free(nvlist);
4573                         return (error);
4574                 }
4575                 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4576                     kcred, NULL, 0);
4577                 nvlist_free(nvlist);
4578                 break;
4579
4580         case ZFS_SMB_ACL_PURGE:
4581                 error = zfs_smb_acl_purge(sharedir);
4582                 break;
4583
4584         default:
4585                 error = EINVAL;
4586                 break;
4587         }
4588
4589         VN_RELE(vp);
4590         VN_RELE(ZTOV(sharedir));
4591
4592         ZFS_EXIT(zfsvfs);
4593
4594         return (error);
4595 #else
4596         return (ENOTSUP);
4597 #endif /* HAVE_ZPL */
4598 }
4599
4600 /*
4601  * inputs:
4602  * zc_name              name of filesystem
4603  * zc_value             short name of snap
4604  * zc_string            user-supplied tag for this hold
4605  * zc_cookie            recursive flag
4606  * zc_temphold          set if hold is temporary
4607  * zc_cleanup_fd        cleanup-on-exit file descriptor for calling process
4608  * zc_sendobj           if non-zero, the objid for zc_name@zc_value
4609  * zc_createtxg         if zc_sendobj is non-zero, snap must have zc_createtxg
4610  *
4611  * outputs:             none
4612  */
4613 static int
4614 zfs_ioc_hold(zfs_cmd_t *zc)
4615 {
4616         boolean_t recursive = zc->zc_cookie;
4617         spa_t *spa;
4618         dsl_pool_t *dp;
4619         dsl_dataset_t *ds;
4620         int error;
4621         minor_t minor = 0;
4622
4623         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4624                 return (EINVAL);
4625
4626         if (zc->zc_sendobj == 0) {
4627                 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4628                     zc->zc_string, recursive, zc->zc_temphold,
4629                     zc->zc_cleanup_fd));
4630         }
4631
4632         if (recursive)
4633                 return (EINVAL);
4634
4635         error = spa_open(zc->zc_name, &spa, FTAG);
4636         if (error)
4637                 return (error);
4638
4639         dp = spa_get_dsl(spa);
4640         rw_enter(&dp->dp_config_rwlock, RW_READER);
4641         error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4642         rw_exit(&dp->dp_config_rwlock);
4643         spa_close(spa, FTAG);
4644         if (error)
4645                 return (error);
4646
4647         /*
4648          * Until we have a hold on this snapshot, it's possible that
4649          * zc_sendobj could've been destroyed and reused as part
4650          * of a later txg.  Make sure we're looking at the right object.
4651          */
4652         if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4653                 dsl_dataset_rele(ds, FTAG);
4654                 return (ENOENT);
4655         }
4656
4657         if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4658                 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4659                 if (error) {
4660                         dsl_dataset_rele(ds, FTAG);
4661                         return (error);
4662                 }
4663         }
4664
4665         error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4666             zc->zc_temphold);
4667         if (minor != 0) {
4668                 if (error == 0) {
4669                         dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4670                             minor);
4671                 }
4672                 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4673         }
4674         dsl_dataset_rele(ds, FTAG);
4675
4676         return (error);
4677 }
4678
4679 /*
4680  * inputs:
4681  * zc_name      name of dataset from which we're releasing a user hold
4682  * zc_value     short name of snap
4683  * zc_string    user-supplied tag for this hold
4684  * zc_cookie    recursive flag
4685  *
4686  * outputs:     none
4687  */
4688 static int
4689 zfs_ioc_release(zfs_cmd_t *zc)
4690 {
4691         boolean_t recursive = zc->zc_cookie;
4692
4693         if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4694                 return (EINVAL);
4695
4696         return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4697             zc->zc_string, recursive));
4698 }
4699
4700 /*
4701  * inputs:
4702  * zc_name              name of filesystem
4703  *
4704  * outputs:
4705  * zc_nvlist_src{_size} nvlist of snapshot holds
4706  */
4707 static int
4708 zfs_ioc_get_holds(zfs_cmd_t *zc)
4709 {
4710         nvlist_t *nvp;
4711         int error;
4712
4713         if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4714                 error = put_nvlist(zc, nvp);
4715                 nvlist_free(nvp);
4716         }
4717
4718         return (error);
4719 }
4720
4721 /*
4722  * inputs:
4723  * zc_guid              flags (ZEVENT_NONBLOCK)
4724  *
4725  * outputs:
4726  * zc_nvlist_dst        next nvlist event
4727  * zc_cookie            dropped events since last get
4728  * zc_cleanup_fd        cleanup-on-exit file descriptor
4729  */
4730 static int
4731 zfs_ioc_events_next(zfs_cmd_t *zc)
4732 {
4733         zfs_zevent_t *ze;
4734         nvlist_t *event = NULL;
4735         minor_t minor;
4736         uint64_t dropped = 0;
4737         int error;
4738
4739         error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4740         if (error != 0)
4741                 return (error);
4742
4743         do {
4744                 error = zfs_zevent_next(ze, &event,
4745                         &zc->zc_nvlist_dst_size, &dropped);
4746                 if (event != NULL) {
4747                         zc->zc_cookie = dropped;
4748                         error = put_nvlist(zc, event);
4749                         nvlist_free(event);
4750                 }
4751
4752                 if (zc->zc_guid & ZEVENT_NONBLOCK)
4753                         break;
4754
4755                 if ((error == 0) || (error != ENOENT))
4756                         break;
4757
4758                 error = zfs_zevent_wait(ze);
4759                 if (error)
4760                         break;
4761         } while (1);
4762
4763         zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4764
4765         return (error);
4766 }
4767
4768 /*
4769  * outputs:
4770  * zc_cookie            cleared events count
4771  */
4772 static int
4773 zfs_ioc_events_clear(zfs_cmd_t *zc)
4774 {
4775         int count;
4776
4777         zfs_zevent_drain_all(&count);
4778         zc->zc_cookie = count;
4779
4780         return 0;
4781 }
4782
4783 /*
4784  * pool create, destroy, and export don't log the history as part of
4785  * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4786  * do the logging of those commands.
4787  */
4788 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4789         { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4790             POOL_CHECK_NONE },
4791         { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4792             POOL_CHECK_NONE },
4793         { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4794             POOL_CHECK_NONE },
4795         { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4796             POOL_CHECK_NONE },
4797         { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4798             POOL_CHECK_NONE },
4799         { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4800             POOL_CHECK_NONE },
4801         { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4802             POOL_CHECK_NONE },
4803         { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4804             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4805         { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4806             POOL_CHECK_READONLY },
4807         { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4808             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4809         { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4810             POOL_CHECK_NONE },
4811         { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4812             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4813         { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4814             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4815         { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4816             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4817         { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4818             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4819         { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4820             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4821         { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4822             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4823         { zfs_ioc_vdev_setfru,  zfs_secpolicy_config, POOL_NAME, B_FALSE,
4824             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4825         { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4826             POOL_CHECK_SUSPENDED },
4827         { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4828             POOL_CHECK_NONE },
4829         { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4830             POOL_CHECK_SUSPENDED },
4831         { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4832             POOL_CHECK_SUSPENDED },
4833         { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4834             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4835         { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4836             POOL_CHECK_NONE },
4837         { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4838             POOL_CHECK_NONE },
4839         { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4840             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4841         { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4842             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4843         { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4844             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4845         { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4846             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4847         { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4848             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4849         { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4850             POOL_CHECK_NONE },
4851         { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4852             POOL_CHECK_NONE },
4853         { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4854             POOL_CHECK_NONE },
4855         { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4856             POOL_CHECK_NONE },
4857         { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4858             POOL_CHECK_NONE },
4859         { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4860             POOL_CHECK_NONE },
4861         { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4862             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4863         { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4864             B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4865         { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4866             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4867         { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4868             POOL_CHECK_NONE },
4869         { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4870             POOL_CHECK_SUSPENDED },
4871         { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4872             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4873         { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4874             POOL_CHECK_NONE },
4875         { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4876             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4877         { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4878             POOL_CHECK_NONE },
4879         { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4880             POOL_CHECK_NONE },
4881         { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4882             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4883         { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4884             POOL_CHECK_NONE },
4885         { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4886             B_FALSE, POOL_CHECK_NONE },
4887         { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4888             B_FALSE, POOL_CHECK_NONE },
4889         { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4890             DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4891         { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4892             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4893         { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4894             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4895         { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4896             POOL_CHECK_SUSPENDED },
4897         { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4898             POOL_CHECK_NONE },
4899         { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4900             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4901         { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4902             POOL_CHECK_NONE },
4903         { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4904             POOL_CHECK_NONE },
4905         { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4906             B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4907         { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4908             POOL_CHECK_SUSPENDED },
4909         { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4910             POOL_CHECK_NONE },
4911         { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4912             POOL_CHECK_NONE },
4913 };
4914
4915 int
4916 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4917     zfs_ioc_poolcheck_t check)
4918 {
4919         spa_t *spa;
4920         int error;
4921
4922         ASSERT(type == POOL_NAME || type == DATASET_NAME);
4923
4924         if (check & POOL_CHECK_NONE)
4925                 return (0);
4926
4927         error = spa_open(name, &spa, FTAG);
4928         if (error == 0) {
4929                 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4930                         error = EAGAIN;
4931                 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4932                         error = EROFS;
4933                 spa_close(spa, FTAG);
4934         }
4935         return (error);
4936 }
4937
4938 static void *
4939 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4940 {
4941         zfsdev_state_t *zs;
4942
4943         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4944
4945         for (zs = list_head(&zfsdev_state_list); zs != NULL;
4946              zs = list_next(&zfsdev_state_list, zs)) {
4947                 if (zs->zs_minor == minor) {
4948                         switch (which) {
4949                                 case ZST_ONEXIT:  return (zs->zs_onexit);
4950                                 case ZST_ZEVENT:  return (zs->zs_zevent);
4951                                 case ZST_ALL:     return (zs);
4952                         }
4953                 }
4954         }
4955
4956         return NULL;
4957 }
4958
4959 void *
4960 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4961 {
4962         void *ptr;
4963
4964         mutex_enter(&zfsdev_state_lock);
4965         ptr = zfsdev_get_state_impl(minor, which);
4966         mutex_exit(&zfsdev_state_lock);
4967
4968         return ptr;
4969 }
4970
4971 minor_t
4972 zfsdev_getminor(struct file *filp)
4973 {
4974         ASSERT(filp != NULL);
4975         ASSERT(filp->private_data != NULL);
4976
4977         return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4978 }
4979
4980 /*
4981  * Find a free minor number.  The zfsdev_state_list is expected to
4982  * be short since it is only a list of currently open file handles.
4983  */
4984 minor_t
4985 zfsdev_minor_alloc(void)
4986 {
4987         static minor_t last_minor = 0;
4988         minor_t m;
4989
4990         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4991
4992         for (m = last_minor + 1; m != last_minor; m++) {
4993                 if (m > ZFSDEV_MAX_MINOR)
4994                         m = 1;
4995                 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
4996                         last_minor = m;
4997                         return (m);
4998                 }
4999         }
5000
5001         return (0);
5002 }
5003
5004 static int
5005 zfsdev_state_init(struct file *filp)
5006 {
5007         zfsdev_state_t *zs;
5008         minor_t minor;
5009
5010         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5011
5012         minor = zfsdev_minor_alloc();
5013         if (minor == 0)
5014                 return (ENXIO);
5015
5016         zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
5017         if (zs == NULL)
5018                 return (ENOMEM);
5019
5020         zs->zs_file = filp;
5021         zs->zs_minor = minor;
5022         filp->private_data = zs;
5023
5024         zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
5025         zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
5026
5027         list_insert_tail(&zfsdev_state_list, zs);
5028
5029         return (0);
5030 }
5031
5032 static int
5033 zfsdev_state_destroy(struct file *filp)
5034 {
5035         zfsdev_state_t *zs;
5036
5037         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5038         ASSERT(filp->private_data != NULL);
5039
5040         zs = filp->private_data;
5041         zfs_onexit_destroy(zs->zs_onexit);
5042         zfs_zevent_destroy(zs->zs_zevent);
5043
5044         list_remove(&zfsdev_state_list, zs);
5045         kmem_free(zs, sizeof(zfsdev_state_t));
5046
5047         return 0;
5048 }
5049
5050 static int
5051 zfsdev_open(struct inode *ino, struct file *filp)
5052 {
5053         int error;
5054
5055         mutex_enter(&zfsdev_state_lock);
5056         error = zfsdev_state_init(filp);
5057         mutex_exit(&zfsdev_state_lock);
5058
5059         return (-error);
5060 }
5061
5062 static int
5063 zfsdev_release(struct inode *ino, struct file *filp)
5064 {
5065         int error;
5066
5067         mutex_enter(&zfsdev_state_lock);
5068         error = zfsdev_state_destroy(filp);
5069         mutex_exit(&zfsdev_state_lock);
5070
5071         return (-error);
5072 }
5073
5074 static long
5075 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5076 {
5077         zfs_cmd_t *zc;
5078         uint_t vec;
5079         int error, rc, flag = 0;
5080
5081         vec = cmd - ZFS_IOC;
5082         if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5083                 return (-EINVAL);
5084
5085         zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
5086
5087         error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5088         if (error != 0)
5089                 error = EFAULT;
5090
5091         if ((error == 0) && !(flag & FKIOCTL))
5092                 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, NULL);
5093
5094         /*
5095          * Ensure that all pool/dataset names are valid before we pass down to
5096          * the lower layers.
5097          */
5098         if (error == 0) {
5099                 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5100                 zc->zc_iflags = flag & FKIOCTL;
5101                 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5102                 case POOL_NAME:
5103                         if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5104                                 error = EINVAL;
5105                         error = pool_status_check(zc->zc_name,
5106                             zfs_ioc_vec[vec].zvec_namecheck,
5107                             zfs_ioc_vec[vec].zvec_pool_check);
5108                         break;
5109
5110                 case DATASET_NAME:
5111                         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5112                                 error = EINVAL;
5113                         error = pool_status_check(zc->zc_name,
5114                             zfs_ioc_vec[vec].zvec_namecheck,
5115                             zfs_ioc_vec[vec].zvec_pool_check);
5116                         break;
5117
5118                 case NO_NAME:
5119                         break;
5120                 }
5121         }
5122
5123         if (error == 0)
5124                 error = zfs_ioc_vec[vec].zvec_func(zc);
5125
5126         rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5127         if (error == 0) {
5128                 if (rc != 0)
5129                         error = EFAULT;
5130                 if (zfs_ioc_vec[vec].zvec_his_log)
5131                         zfs_log_history(zc);
5132         }
5133
5134         kmem_free(zc, sizeof (zfs_cmd_t));
5135         return (-error);
5136 }
5137
5138 #ifdef CONFIG_COMPAT
5139 static long
5140 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5141 {
5142         return zfsdev_ioctl(filp, cmd, arg);
5143 }
5144 #else
5145 #define zfsdev_compat_ioctl   NULL
5146 #endif
5147
5148 static const struct file_operations zfsdev_fops = {
5149         .open            = zfsdev_open,
5150         .release         = zfsdev_release,
5151         .unlocked_ioctl  = zfsdev_ioctl,
5152         .compat_ioctl    = zfsdev_compat_ioctl,
5153         .owner           = THIS_MODULE,
5154 };
5155
5156 static struct miscdevice zfs_misc = {
5157         .minor          = MISC_DYNAMIC_MINOR,
5158         .name           = ZFS_DRIVER,
5159         .fops           = &zfsdev_fops,
5160 };
5161
5162 static int
5163 zfs_attach(void)
5164 {
5165         int error;
5166
5167         mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5168         list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5169             offsetof(zfsdev_state_t, zs_next));
5170
5171         error = misc_register(&zfs_misc);
5172         if (error) {
5173                 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5174                 return (error);
5175         }
5176
5177         return (0);
5178 }
5179
5180 static void
5181 zfs_detach(void)
5182 {
5183         int error;
5184
5185         error = misc_deregister(&zfs_misc);
5186         if (error)
5187                 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5188
5189         mutex_destroy(&zfsdev_state_lock);
5190         list_destroy(&zfsdev_state_list);
5191 }
5192
5193 #ifdef HAVE_ZPL
5194 uint_t zfs_fsyncer_key;
5195 extern uint_t rrw_tsd_key;
5196 #endif
5197
5198 #ifdef DEBUG
5199 #define ZFS_DEBUG_STR   " (DEBUG mode)"
5200 #else
5201 #define ZFS_DEBUG_STR   ""
5202 #endif
5203
5204 int
5205 _init(void)
5206 {
5207         int error;
5208
5209         spa_init(FREAD | FWRITE);
5210         zfs_init();
5211
5212         if ((error = zvol_init()) != 0)
5213                 goto out1;
5214
5215         if ((error = zfs_attach()) != 0)
5216                 goto out2;
5217
5218 #ifdef HAVE_ZPL
5219         tsd_create(&zfs_fsyncer_key, NULL);
5220         tsd_create(&rrw_tsd_key, NULL);
5221
5222         mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
5223 #endif /* HAVE_ZPL */
5224
5225         printk(KERN_NOTICE "ZFS: Loaded ZFS Filesystem v%s%s\n",
5226                ZFS_META_VERSION, ZFS_DEBUG_STR);
5227
5228         return (0);
5229
5230 out2:
5231         (void) zvol_fini();
5232 out1:
5233         zfs_fini();
5234         spa_fini();
5235         printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s%s"
5236                ", rc = %d\n", ZFS_META_VERSION, ZFS_DEBUG_STR, error);
5237
5238         return (error);
5239 }
5240
5241 int
5242 _fini(void)
5243 {
5244         zfs_detach();
5245         zvol_fini();
5246         zfs_fini();
5247         spa_fini();
5248 #ifdef HAVE_ZPL
5249         if (zfs_nfsshare_inited)
5250                 (void) ddi_modclose(nfs_mod);
5251         if (zfs_smbshare_inited)
5252                 (void) ddi_modclose(smbsrv_mod);
5253         if (zfs_nfsshare_inited || zfs_smbshare_inited)
5254                 (void) ddi_modclose(sharefs_mod);
5255
5256         mutex_destroy(&zfs_share_lock);
5257         tsd_destroy(&zfs_fsyncer_key);
5258 #endif /* HAVE_ZPL */
5259
5260         printk(KERN_NOTICE "ZFS: Unloaded ZFS Filesystem v%s%s\n",
5261                ZFS_META_VERSION, ZFS_DEBUG_STR);
5262
5263         return (0);
5264 }
5265
5266 #ifdef HAVE_SPL
5267 spl_module_init(_init);
5268 spl_module_exit(_fini);
5269
5270 MODULE_DESCRIPTION("ZFS");
5271 MODULE_AUTHOR(ZFS_META_AUTHOR);
5272 MODULE_LICENSE(ZFS_META_LICENSE);
5273 #endif /* HAVE_SPL */