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