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