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