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