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