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