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