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