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