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