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