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