]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_ioctl.c
Temporarily disable the reguid test.
[mirror_zfs.git] / module / zfs / zfs_ioctl.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
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);
4097 vdev_reopen(spa->spa_root_vdev);
4098 (void) spa_vdev_state_exit(spa, NULL, 0);
4099 spa_close(spa, FTAG);
4100 return (0);
4101}
34dc7c2f
BB
4102/*
4103 * inputs:
4104 * zc_name name of filesystem
4105 * zc_value name of origin snapshot
4106 *
428870ff
BB
4107 * outputs:
4108 * zc_string name of conflicting snapshot, if there is one
34dc7c2f
BB
4109 */
4110static int
4111zfs_ioc_promote(zfs_cmd_t *zc)
4112{
4113 char *cp;
4114
4115 /*
4116 * We don't need to unmount *all* the origin fs's snapshots, but
4117 * it's easier.
4118 */
4119 cp = strchr(zc->zc_value, '@');
4120 if (cp)
4121 *cp = '\0';
4122 (void) dmu_objset_find(zc->zc_value,
4123 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
428870ff 4124 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
34dc7c2f
BB
4125}
4126
9babb374
BB
4127/*
4128 * Retrieve a single {user|group}{used|quota}@... property.
4129 *
4130 * inputs:
4131 * zc_name name of filesystem
4132 * zc_objset_type zfs_userquota_prop_t
4133 * zc_value domain name (eg. "S-1-234-567-89")
4134 * zc_guid RID/UID/GID
4135 *
4136 * outputs:
4137 * zc_cookie property value
4138 */
4139static int
4140zfs_ioc_userspace_one(zfs_cmd_t *zc)
4141{
3558fd73 4142 zfs_sb_t *zsb;
9babb374
BB
4143 int error;
4144
4145 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4146 return (EINVAL);
4147
3558fd73 4148 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
9babb374
BB
4149 if (error)
4150 return (error);
4151
3558fd73 4152 error = zfs_userspace_one(zsb,
9babb374 4153 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
3558fd73 4154 zfs_sb_rele(zsb, FTAG);
9babb374
BB
4155
4156 return (error);
4157}
4158
4159/*
4160 * inputs:
4161 * zc_name name of filesystem
4162 * zc_cookie zap cursor
4163 * zc_objset_type zfs_userquota_prop_t
4164 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4165 *
4166 * outputs:
4167 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4168 * zc_cookie zap cursor
4169 */
4170static int
4171zfs_ioc_userspace_many(zfs_cmd_t *zc)
4172{
3558fd73 4173 zfs_sb_t *zsb;
428870ff 4174 int bufsize = zc->zc_nvlist_dst_size;
3558fd73
BB
4175 int error;
4176 void *buf;
428870ff
BB
4177
4178 if (bufsize <= 0)
4179 return (ENOMEM);
9babb374 4180
3558fd73 4181 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
9babb374
BB
4182 if (error)
4183 return (error);
4184
2b8cad61 4185 buf = vmem_alloc(bufsize, KM_SLEEP);
9babb374 4186
3558fd73 4187 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
9babb374
BB
4188 buf, &zc->zc_nvlist_dst_size);
4189
4190 if (error == 0) {
4191 error = xcopyout(buf,
4192 (void *)(uintptr_t)zc->zc_nvlist_dst,
4193 zc->zc_nvlist_dst_size);
4194 }
2b8cad61 4195 vmem_free(buf, bufsize);
3558fd73 4196 zfs_sb_rele(zsb, FTAG);
9babb374
BB
4197
4198 return (error);
4199}
4200
4201/*
4202 * inputs:
4203 * zc_name name of filesystem
4204 *
4205 * outputs:
4206 * none
4207 */
4208static int
4209zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4210{
4211 objset_t *os;
428870ff 4212 int error = 0;
3558fd73 4213 zfs_sb_t *zsb;
9babb374 4214
3558fd73
BB
4215 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4216 if (!dmu_objset_userused_enabled(zsb->z_os)) {
9babb374
BB
4217 /*
4218 * If userused is not enabled, it may be because the
4219 * objset needs to be closed & reopened (to grow the
4220 * objset_phys_t). Suspend/resume the fs will do that.
4221 */
3558fd73 4222 error = zfs_suspend_fs(zsb);
428870ff 4223 if (error == 0)
3558fd73 4224 error = zfs_resume_fs(zsb, zc->zc_name);
9babb374
BB
4225 }
4226 if (error == 0)
3558fd73 4227 error = dmu_objset_userspace_upgrade(zsb->z_os);
2cf7f52b 4228 deactivate_super(zsb->z_sb);
9babb374 4229 } else {
428870ff
BB
4230 /* XXX kind of reading contents without owning */
4231 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
9babb374
BB
4232 if (error)
4233 return (error);
4234
4235 error = dmu_objset_userspace_upgrade(os);
428870ff 4236 dmu_objset_rele(os, FTAG);
9babb374
BB
4237 }
4238
4239 return (error);
4240}
4241
34dc7c2f
BB
4242static int
4243zfs_ioc_share(zfs_cmd_t *zc)
4244{
46e18b3f 4245 return (ENOSYS);
34dc7c2f
BB
4246}
4247
9babb374
BB
4248ace_t full_access[] = {
4249 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4250};
4251
572e2857
BB
4252/*
4253 * inputs:
4254 * zc_name name of containing filesystem
4255 * zc_obj object # beyond which we want next in-use object #
4256 *
4257 * outputs:
4258 * zc_obj next in-use object #
4259 */
4260static int
4261zfs_ioc_next_obj(zfs_cmd_t *zc)
4262{
4263 objset_t *os = NULL;
4264 int error;
4265
4266 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4267 if (error)
4268 return (error);
4269
4270 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4271 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4272
4273 dmu_objset_rele(os, FTAG);
4274 return (error);
4275}
4276
4277/*
4278 * inputs:
4279 * zc_name name of filesystem
4280 * zc_value prefix name for snapshot
4281 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4282 *
4283 * outputs:
4284 */
4285static int
4286zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4287{
4288 char *snap_name;
4289 int error;
4290
4291 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4292 (u_longlong_t)ddi_get_lbolt64());
4293
4294 if (strlen(snap_name) >= MAXNAMELEN) {
4295 strfree(snap_name);
4296 return (E2BIG);
4297 }
4298
4299 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4300 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4301 if (error != 0) {
4302 strfree(snap_name);
4303 return (error);
4304 }
4305
4306 (void) strcpy(zc->zc_value, snap_name);
4307 strfree(snap_name);
4308 return (0);
4309}
4310
4311/*
4312 * inputs:
4313 * zc_name name of "to" snapshot
4314 * zc_value name of "from" snapshot
4315 * zc_cookie file descriptor to write diff data on
4316 *
4317 * outputs:
4318 * dmu_diff_record_t's to the file descriptor
4319 */
4320static int
4321zfs_ioc_diff(zfs_cmd_t *zc)
4322{
4323 objset_t *fromsnap;
4324 objset_t *tosnap;
4325 file_t *fp;
4326 offset_t off;
4327 int error;
4328
4329 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4330 if (error)
4331 return (error);
4332
4333 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4334 if (error) {
4335 dmu_objset_rele(tosnap, FTAG);
4336 return (error);
4337 }
4338
4339 fp = getf(zc->zc_cookie);
4340 if (fp == NULL) {
4341 dmu_objset_rele(fromsnap, FTAG);
4342 dmu_objset_rele(tosnap, FTAG);
4343 return (EBADF);
4344 }
4345
4346 off = fp->f_offset;
4347
4348 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4349
4350 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4351 fp->f_offset = off;
4352 releasef(zc->zc_cookie);
4353
4354 dmu_objset_rele(fromsnap, FTAG);
4355 dmu_objset_rele(tosnap, FTAG);
4356 return (error);
4357}
4358
9babb374
BB
4359/*
4360 * Remove all ACL files in shares dir
4361 */
3c9609b3 4362#ifdef HAVE_SMB_SHARE
9babb374
BB
4363static int
4364zfs_smb_acl_purge(znode_t *dzp)
4365{
4366 zap_cursor_t zc;
4367 zap_attribute_t zap;
3558fd73 4368 zfs_sb_t *zsb = ZTOZSB(dzp);
9babb374
BB
4369 int error;
4370
3558fd73 4371 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
9babb374
BB
4372 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4373 zap_cursor_advance(&zc)) {
4374 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4375 NULL, 0)) != 0)
4376 break;
4377 }
4378 zap_cursor_fini(&zc);
4379 return (error);
4380}
3c9609b3 4381#endif /* HAVE_SMB_SHARE */
9babb374
BB
4382
4383static int
4384zfs_ioc_smb_acl(zfs_cmd_t *zc)
4385{
3c9609b3 4386#ifdef HAVE_SMB_SHARE
9babb374
BB
4387 vnode_t *vp;
4388 znode_t *dzp;
4389 vnode_t *resourcevp = NULL;
4390 znode_t *sharedir;
3558fd73 4391 zfs_sb_t *zsb;
9babb374
BB
4392 nvlist_t *nvlist;
4393 char *src, *target;
4394 vattr_t vattr;
4395 vsecattr_t vsec;
4396 int error = 0;
4397
4398 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4399 NO_FOLLOW, NULL, &vp)) != 0)
4400 return (error);
4401
4402 /* Now make sure mntpnt and dataset are ZFS */
4403
4404 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4405 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4406 zc->zc_name) != 0)) {
4407 VN_RELE(vp);
4408 return (EINVAL);
4409 }
4410
4411 dzp = VTOZ(vp);
3558fd73
BB
4412 zsb = ZTOZSB(dzp);
4413 ZFS_ENTER(zsb);
9babb374
BB
4414
4415 /*
4416 * Create share dir if its missing.
4417 */
3558fd73
BB
4418 mutex_enter(&zsb->z_lock);
4419 if (zsb->z_shares_dir == 0) {
9babb374
BB
4420 dmu_tx_t *tx;
4421
3558fd73 4422 tx = dmu_tx_create(zsb->z_os);
9babb374
BB
4423 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4424 ZFS_SHARES_DIR);
4425 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4426 error = dmu_tx_assign(tx, TXG_WAIT);
4427 if (error) {
4428 dmu_tx_abort(tx);
4429 } else {
3558fd73 4430 error = zfs_create_share_dir(zsb, tx);
9babb374
BB
4431 dmu_tx_commit(tx);
4432 }
4433 if (error) {
3558fd73 4434 mutex_exit(&zsb->z_lock);
9babb374 4435 VN_RELE(vp);
3558fd73 4436 ZFS_EXIT(zsb);
9babb374
BB
4437 return (error);
4438 }
4439 }
3558fd73 4440 mutex_exit(&zsb->z_lock);
9babb374 4441
3558fd73
BB
4442 ASSERT(zsb->z_shares_dir);
4443 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
9babb374 4444 VN_RELE(vp);
3558fd73 4445 ZFS_EXIT(zsb);
9babb374
BB
4446 return (error);
4447 }
4448
4449 switch (zc->zc_cookie) {
4450 case ZFS_SMB_ACL_ADD:
4451 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
9babb374
BB
4452 vattr.va_mode = S_IFREG|0777;
4453 vattr.va_uid = 0;
4454 vattr.va_gid = 0;
4455
4456 vsec.vsa_mask = VSA_ACE;
4457 vsec.vsa_aclentp = &full_access;
4458 vsec.vsa_aclentsz = sizeof (full_access);
4459 vsec.vsa_aclcnt = 1;
4460
4461 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4462 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4463 if (resourcevp)
4464 VN_RELE(resourcevp);
4465 break;
4466
4467 case ZFS_SMB_ACL_REMOVE:
4468 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4469 NULL, 0);
4470 break;
4471
4472 case ZFS_SMB_ACL_RENAME:
4473 if ((error = get_nvlist(zc->zc_nvlist_src,
4474 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4475 VN_RELE(vp);
3558fd73 4476 ZFS_EXIT(zsb);
9babb374
BB
4477 return (error);
4478 }
4479 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4480 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4481 &target)) {
4482 VN_RELE(vp);
4483 VN_RELE(ZTOV(sharedir));
3558fd73 4484 ZFS_EXIT(zsb);
428870ff 4485 nvlist_free(nvlist);
9babb374
BB
4486 return (error);
4487 }
4488 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4489 kcred, NULL, 0);
4490 nvlist_free(nvlist);
4491 break;
4492
4493 case ZFS_SMB_ACL_PURGE:
4494 error = zfs_smb_acl_purge(sharedir);
4495 break;
4496
4497 default:
4498 error = EINVAL;
4499 break;
4500 }
4501
4502 VN_RELE(vp);
4503 VN_RELE(ZTOV(sharedir));
4504
3558fd73 4505 ZFS_EXIT(zsb);
9babb374
BB
4506
4507 return (error);
325f0235
BB
4508#else
4509 return (ENOTSUP);
3c9609b3 4510#endif /* HAVE_SMB_SHARE */
9babb374
BB
4511}
4512
45d1cae3
BB
4513/*
4514 * inputs:
572e2857
BB
4515 * zc_name name of filesystem
4516 * zc_value short name of snap
4517 * zc_string user-supplied tag for this hold
4518 * zc_cookie recursive flag
4519 * zc_temphold set if hold is temporary
4520 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4521 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4522 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
45d1cae3
BB
4523 *
4524 * outputs: none
4525 */
4526static int
4527zfs_ioc_hold(zfs_cmd_t *zc)
4528{
4529 boolean_t recursive = zc->zc_cookie;
572e2857
BB
4530 spa_t *spa;
4531 dsl_pool_t *dp;
4532 dsl_dataset_t *ds;
4533 int error;
4534 minor_t minor = 0;
45d1cae3
BB
4535
4536 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4537 return (EINVAL);
4538
572e2857
BB
4539 if (zc->zc_sendobj == 0) {
4540 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4541 zc->zc_string, recursive, zc->zc_temphold,
4542 zc->zc_cleanup_fd));
4543 }
4544
4545 if (recursive)
4546 return (EINVAL);
4547
4548 error = spa_open(zc->zc_name, &spa, FTAG);
4549 if (error)
4550 return (error);
4551
4552 dp = spa_get_dsl(spa);
4553 rw_enter(&dp->dp_config_rwlock, RW_READER);
4554 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4555 rw_exit(&dp->dp_config_rwlock);
4556 spa_close(spa, FTAG);
4557 if (error)
4558 return (error);
4559
4560 /*
4561 * Until we have a hold on this snapshot, it's possible that
4562 * zc_sendobj could've been destroyed and reused as part
4563 * of a later txg. Make sure we're looking at the right object.
4564 */
4565 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4566 dsl_dataset_rele(ds, FTAG);
4567 return (ENOENT);
4568 }
4569
4570 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4571 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4572 if (error) {
4573 dsl_dataset_rele(ds, FTAG);
4574 return (error);
4575 }
4576 }
4577
4578 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4579 zc->zc_temphold);
4580 if (minor != 0) {
4581 if (error == 0) {
4582 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4583 minor);
4584 }
4585 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4586 }
4587 dsl_dataset_rele(ds, FTAG);
4588
4589 return (error);
45d1cae3
BB
4590}
4591
4592/*
4593 * inputs:
572e2857 4594 * zc_name name of dataset from which we're releasing a user hold
45d1cae3 4595 * zc_value short name of snap
572e2857 4596 * zc_string user-supplied tag for this hold
45d1cae3
BB
4597 * zc_cookie recursive flag
4598 *
572e2857 4599 * outputs: none
45d1cae3
BB
4600 */
4601static int
4602zfs_ioc_release(zfs_cmd_t *zc)
4603{
4604 boolean_t recursive = zc->zc_cookie;
4605
4606 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4607 return (EINVAL);
4608
4609 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4610 zc->zc_string, recursive));
4611}
4612
4613/*
4614 * inputs:
4615 * zc_name name of filesystem
4616 *
4617 * outputs:
4618 * zc_nvlist_src{_size} nvlist of snapshot holds
4619 */
4620static int
4621zfs_ioc_get_holds(zfs_cmd_t *zc)
4622{
4623 nvlist_t *nvp;
4624 int error;
4625
4626 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4627 error = put_nvlist(zc, nvp);
4628 nvlist_free(nvp);
4629 }
4630
4631 return (error);
4632}
4633
26685276
BB
4634/*
4635 * inputs:
4636 * zc_guid flags (ZEVENT_NONBLOCK)
4637 *
4638 * outputs:
4639 * zc_nvlist_dst next nvlist event
4640 * zc_cookie dropped events since last get
4641 * zc_cleanup_fd cleanup-on-exit file descriptor
4642 */
4643static int
4644zfs_ioc_events_next(zfs_cmd_t *zc)
4645{
4646 zfs_zevent_t *ze;
4647 nvlist_t *event = NULL;
4648 minor_t minor;
4649 uint64_t dropped = 0;
4650 int error;
4651
4652 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4653 if (error != 0)
4654 return (error);
4655
4656 do {
baa40d45
BB
4657 error = zfs_zevent_next(ze, &event,
4658 &zc->zc_nvlist_dst_size, &dropped);
26685276
BB
4659 if (event != NULL) {
4660 zc->zc_cookie = dropped;
4661 error = put_nvlist(zc, event);
baa40d45 4662 nvlist_free(event);
26685276
BB
4663 }
4664
4665 if (zc->zc_guid & ZEVENT_NONBLOCK)
4666 break;
4667
4668 if ((error == 0) || (error != ENOENT))
4669 break;
4670
4671 error = zfs_zevent_wait(ze);
4672 if (error)
4673 break;
4674 } while (1);
4675
4676 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4677
4678 return (error);
4679}
4680
4681/*
4682 * outputs:
4683 * zc_cookie cleared events count
4684 */
4685static int
4686zfs_ioc_events_clear(zfs_cmd_t *zc)
4687{
4688 int count;
4689
4690 zfs_zevent_drain_all(&count);
4691 zc->zc_cookie = count;
4692
4693 return 0;
4694}
4695
330d06f9
MA
4696/*
4697 * inputs:
4698 * zc_name name of new filesystem or snapshot
4699 * zc_value full name of old snapshot
4700 *
4701 * outputs:
4702 * zc_cookie space in bytes
4703 * zc_objset_type compressed space in bytes
4704 * zc_perm_action uncompressed space in bytes
4705 */
4706static int
4707zfs_ioc_space_written(zfs_cmd_t *zc)
4708{
4709 int error;
4710 dsl_dataset_t *new, *old;
4711
4712 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4713 if (error != 0)
4714 return (error);
4715 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4716 if (error != 0) {
4717 dsl_dataset_rele(new, FTAG);
4718 return (error);
4719 }
4720
4721 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4722 &zc->zc_objset_type, &zc->zc_perm_action);
4723 dsl_dataset_rele(old, FTAG);
4724 dsl_dataset_rele(new, FTAG);
4725 return (error);
4726}
4727
4728/*
4729 * inputs:
4730 * zc_name full name of last snapshot
4731 * zc_value full name of first snapshot
4732 *
4733 * outputs:
4734 * zc_cookie space in bytes
4735 * zc_objset_type compressed space in bytes
4736 * zc_perm_action uncompressed space in bytes
4737 */
4738static int
4739zfs_ioc_space_snaps(zfs_cmd_t *zc)
4740{
4741 int error;
4742 dsl_dataset_t *new, *old;
4743
4744 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4745 if (error != 0)
4746 return (error);
4747 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4748 if (error != 0) {
4749 dsl_dataset_rele(new, FTAG);
4750 return (error);
4751 }
4752
4753 error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4754 &zc->zc_objset_type, &zc->zc_perm_action);
4755 dsl_dataset_rele(old, FTAG);
4756 dsl_dataset_rele(new, FTAG);
4757 return (error);
4758}
4759
34dc7c2f
BB
4760/*
4761 * pool create, destroy, and export don't log the history as part of
4762 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4763 * do the logging of those commands.
4764 */
4765static zfs_ioc_vec_t zfs_ioc_vec[] = {
9babb374 4766 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4767 POOL_CHECK_NONE },
9babb374 4768 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4769 POOL_CHECK_NONE },
9babb374 4770 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4771 POOL_CHECK_NONE },
9babb374 4772 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4773 POOL_CHECK_NONE },
9babb374 4774 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
572e2857 4775 POOL_CHECK_NONE },
9babb374 4776 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
572e2857 4777 POOL_CHECK_NONE },
9babb374 4778 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
572e2857 4779 POOL_CHECK_NONE },
428870ff 4780 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4781 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4782 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
572e2857 4783 POOL_CHECK_READONLY },
9babb374 4784 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4785 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4786 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4787 POOL_CHECK_NONE },
9babb374 4788 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4789 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4790 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4791 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4792 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4793 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4794 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4795 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4796 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4797 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4798 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4799 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4800 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
572e2857 4801 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4802 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857 4803 POOL_CHECK_SUSPENDED },
9babb374 4804 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857 4805 POOL_CHECK_NONE },
9babb374 4806 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857 4807 POOL_CHECK_SUSPENDED },
9babb374 4808 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857
BB
4809 POOL_CHECK_SUSPENDED },
4810 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4811 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
60101509
BB
4812 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4813 POOL_CHECK_NONE },
4814 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4815 POOL_CHECK_NONE },
572e2857
BB
4816 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4817 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4818 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
572e2857 4819 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4820 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
572e2857
BB
4821 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4822 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4823 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4824 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4825 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
330d06f9 4826 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE,
572e2857 4827 POOL_CHECK_NONE },
9babb374 4828 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
572e2857 4829 POOL_CHECK_NONE },
9babb374 4830 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
572e2857 4831 POOL_CHECK_NONE },
9babb374 4832 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
572e2857 4833 POOL_CHECK_NONE },
9babb374 4834 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
572e2857
BB
4835 POOL_CHECK_NONE },
4836 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4837 POOL_CHECK_NONE },
9babb374 4838 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
572e2857 4839 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
330d06f9
MA
4840 { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive,
4841 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4842 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
572e2857
BB
4843 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4844 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4845 POOL_CHECK_NONE },
4846 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4847 POOL_CHECK_SUSPENDED },
9babb374 4848 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857 4849 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4850 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
572e2857 4851 POOL_CHECK_NONE },
9babb374 4852 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
572e2857 4853 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4854 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857
BB
4855 POOL_CHECK_NONE },
4856 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4857 POOL_CHECK_NONE },
9babb374 4858 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
572e2857 4859 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
9babb374 4860 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
572e2857
BB
4861 POOL_CHECK_NONE },
4862 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4863 B_FALSE, POOL_CHECK_NONE },
4864 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4865 B_FALSE, POOL_CHECK_NONE },
9babb374 4866 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
572e2857
BB
4867 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4868 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4869 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
45d1cae3 4870 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
572e2857 4871 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
45d1cae3 4872 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857 4873 POOL_CHECK_SUSPENDED },
428870ff 4874 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
572e2857 4875 POOL_CHECK_NONE },
428870ff 4876 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
572e2857
BB
4877 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4878 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4879 POOL_CHECK_NONE },
4880 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4881 POOL_CHECK_NONE },
4882 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4883 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4884 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
26685276
BB
4885 POOL_CHECK_SUSPENDED },
4886 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4887 POOL_CHECK_NONE },
4888 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
330d06f9
MA
4889 POOL_CHECK_NONE },
4890 { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4891 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4892 { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4893 POOL_CHECK_SUSPENDED },
4894 { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4895 POOL_CHECK_SUSPENDED },
1bd201e7
CS
4896 { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4897 POOL_CHECK_SUSPENDED },
37abac6d
BP
4898 { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4899 POOL_CHECK_NONE }
34dc7c2f
BB
4900};
4901
9babb374 4902int
572e2857
BB
4903pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4904 zfs_ioc_poolcheck_t check)
9babb374
BB
4905{
4906 spa_t *spa;
4907 int error;
4908
4909 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4910
572e2857
BB
4911 if (check & POOL_CHECK_NONE)
4912 return (0);
4913
9babb374
BB
4914 error = spa_open(name, &spa, FTAG);
4915 if (error == 0) {
572e2857 4916 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
9babb374 4917 error = EAGAIN;
572e2857
BB
4918 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4919 error = EROFS;
9babb374
BB
4920 spa_close(spa, FTAG);
4921 }
4922 return (error);
4923}
4924
325f0235
BB
4925static void *
4926zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4927{
4928 zfsdev_state_t *zs;
4929
4930 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4931
4932 for (zs = list_head(&zfsdev_state_list); zs != NULL;
4933 zs = list_next(&zfsdev_state_list, zs)) {
4934 if (zs->zs_minor == minor) {
4935 switch (which) {
4936 case ZST_ONEXIT: return (zs->zs_onexit);
4937 case ZST_ZEVENT: return (zs->zs_zevent);
4938 case ZST_ALL: return (zs);
4939 }
4940 }
4941 }
4942
4943 return NULL;
4944}
4945
4946void *
4947zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4948{
4949 void *ptr;
4950
4951 mutex_enter(&zfsdev_state_lock);
4952 ptr = zfsdev_get_state_impl(minor, which);
4953 mutex_exit(&zfsdev_state_lock);
4954
4955 return ptr;
4956}
4957
4958minor_t
4959zfsdev_getminor(struct file *filp)
4960{
4961 ASSERT(filp != NULL);
4962 ASSERT(filp->private_data != NULL);
4963
4964 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4965}
4966
572e2857 4967/*
325f0235
BB
4968 * Find a free minor number. The zfsdev_state_list is expected to
4969 * be short since it is only a list of currently open file handles.
572e2857
BB
4970 */
4971minor_t
4972zfsdev_minor_alloc(void)
4973{
325f0235 4974 static minor_t last_minor = 0;
572e2857
BB
4975 minor_t m;
4976
4977 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4978
4979 for (m = last_minor + 1; m != last_minor; m++) {
4980 if (m > ZFSDEV_MAX_MINOR)
4981 m = 1;
325f0235 4982 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
572e2857
BB
4983 last_minor = m;
4984 return (m);
4985 }
4986 }
4987
4988 return (0);
4989}
4990
4991static int
325f0235 4992zfsdev_state_init(struct file *filp)
572e2857 4993{
325f0235 4994 zfsdev_state_t *zs;
572e2857 4995 minor_t minor;
572e2857
BB
4996
4997 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
572e2857 4998
325f0235
BB
4999 minor = zfsdev_minor_alloc();
5000 if (minor == 0)
5001 return (ENXIO);
5002
5003 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
5004 if (zs == NULL)
5005 return (ENOMEM);
572e2857 5006
325f0235
BB
5007 zs->zs_file = filp;
5008 zs->zs_minor = minor;
5009 filp->private_data = zs;
572e2857 5010
325f0235
BB
5011 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
5012 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
572e2857 5013
325f0235 5014 list_insert_tail(&zfsdev_state_list, zs);
572e2857
BB
5015
5016 return (0);
5017}
5018
325f0235
BB
5019static int
5020zfsdev_state_destroy(struct file *filp)
572e2857 5021{
325f0235 5022 zfsdev_state_t *zs;
572e2857 5023
325f0235
BB
5024 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5025 ASSERT(filp->private_data != NULL);
572e2857 5026
325f0235
BB
5027 zs = filp->private_data;
5028 zfs_onexit_destroy(zs->zs_onexit);
5029 zfs_zevent_destroy(zs->zs_zevent);
572e2857 5030
325f0235
BB
5031 list_remove(&zfsdev_state_list, zs);
5032 kmem_free(zs, sizeof(zfsdev_state_t));
572e2857 5033
325f0235 5034 return 0;
572e2857
BB
5035}
5036
5037static int
325f0235 5038zfsdev_open(struct inode *ino, struct file *filp)
572e2857 5039{
325f0235 5040 int error;
572e2857 5041
325f0235
BB
5042 mutex_enter(&zfsdev_state_lock);
5043 error = zfsdev_state_init(filp);
5044 mutex_exit(&zfsdev_state_lock);
572e2857 5045
325f0235 5046 return (-error);
572e2857
BB
5047}
5048
5049static int
325f0235 5050zfsdev_release(struct inode *ino, struct file *filp)
572e2857 5051{
325f0235 5052 int error;
572e2857
BB
5053
5054 mutex_enter(&zfsdev_state_lock);
325f0235 5055 error = zfsdev_state_destroy(filp);
572e2857
BB
5056 mutex_exit(&zfsdev_state_lock);
5057
325f0235 5058 return (-error);
572e2857
BB
5059}
5060
325f0235
BB
5061static long
5062zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f
BB
5063{
5064 zfs_cmd_t *zc;
5065 uint_t vec;
325f0235 5066 int error, rc, flag = 0;
34dc7c2f
BB
5067
5068 vec = cmd - ZFS_IOC;
34dc7c2f 5069 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
325f0235 5070 return (-EINVAL);
34dc7c2f 5071
00b46022 5072 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
34dc7c2f 5073
9babb374 5074 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
428870ff
BB
5075 if (error != 0)
5076 error = EFAULT;
34dc7c2f 5077
428870ff 5078 if ((error == 0) && !(flag & FKIOCTL))
f01b360e 5079 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
34dc7c2f
BB
5080
5081 /*
5082 * Ensure that all pool/dataset names are valid before we pass down to
5083 * the lower layers.
5084 */
5085 if (error == 0) {
5086 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
9babb374 5087 zc->zc_iflags = flag & FKIOCTL;
34dc7c2f
BB
5088 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5089 case POOL_NAME:
5090 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5091 error = EINVAL;
572e2857
BB
5092 error = pool_status_check(zc->zc_name,
5093 zfs_ioc_vec[vec].zvec_namecheck,
5094 zfs_ioc_vec[vec].zvec_pool_check);
34dc7c2f
BB
5095 break;
5096
5097 case DATASET_NAME:
5098 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5099 error = EINVAL;
572e2857
BB
5100 error = pool_status_check(zc->zc_name,
5101 zfs_ioc_vec[vec].zvec_namecheck,
5102 zfs_ioc_vec[vec].zvec_pool_check);
34dc7c2f
BB
5103 break;
5104
5105 case NO_NAME:
5106 break;
5107 }
5108 }
5109
5110 if (error == 0)
5111 error = zfs_ioc_vec[vec].zvec_func(zc);
5112
9babb374 5113 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
34dc7c2f 5114 if (error == 0) {
428870ff
BB
5115 if (rc != 0)
5116 error = EFAULT;
9babb374 5117 if (zfs_ioc_vec[vec].zvec_his_log)
34dc7c2f
BB
5118 zfs_log_history(zc);
5119 }
5120
5121 kmem_free(zc, sizeof (zfs_cmd_t));
325f0235 5122 return (-error);
34dc7c2f
BB
5123}
5124
325f0235
BB
5125#ifdef CONFIG_COMPAT
5126static long
5127zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
34dc7c2f 5128{
325f0235
BB
5129 return zfsdev_ioctl(filp, cmd, arg);
5130}
5131#else
f5e79474 5132#define zfsdev_compat_ioctl NULL
325f0235 5133#endif
34dc7c2f 5134
325f0235
BB
5135static const struct file_operations zfsdev_fops = {
5136 .open = zfsdev_open,
5137 .release = zfsdev_release,
5138 .unlocked_ioctl = zfsdev_ioctl,
5139 .compat_ioctl = zfsdev_compat_ioctl,
5140 .owner = THIS_MODULE,
5141};
34dc7c2f 5142
325f0235
BB
5143static struct miscdevice zfs_misc = {
5144 .minor = MISC_DYNAMIC_MINOR,
5145 .name = ZFS_DRIVER,
5146 .fops = &zfsdev_fops,
5147};
34dc7c2f
BB
5148
5149static int
325f0235 5150zfs_attach(void)
34dc7c2f 5151{
325f0235 5152 int error;
34dc7c2f 5153
325f0235
BB
5154 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5155 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5156 offsetof(zfsdev_state_t, zs_next));
34dc7c2f 5157
325f0235
BB
5158 error = misc_register(&zfs_misc);
5159 if (error) {
5160 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5161 return (error);
5162 }
34dc7c2f 5163
325f0235 5164 return (0);
34dc7c2f
BB
5165}
5166
325f0235
BB
5167static void
5168zfs_detach(void)
34dc7c2f 5169{
325f0235 5170 int error;
34dc7c2f 5171
325f0235
BB
5172 error = misc_deregister(&zfs_misc);
5173 if (error)
5174 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
34dc7c2f 5175
325f0235
BB
5176 mutex_destroy(&zfsdev_state_lock);
5177 list_destroy(&zfsdev_state_list);
34dc7c2f
BB
5178}
5179
34dc7c2f
BB
5180uint_t zfs_fsyncer_key;
5181extern uint_t rrw_tsd_key;
325f0235
BB
5182
5183#ifdef DEBUG
5184#define ZFS_DEBUG_STR " (DEBUG mode)"
5185#else
5186#define ZFS_DEBUG_STR ""
5187#endif
34dc7c2f
BB
5188
5189int
5190_init(void)
5191{
5192 int error;
5193
5194 spa_init(FREAD | FWRITE);
5195 zfs_init();
34dc7c2f 5196
325f0235
BB
5197 if ((error = zvol_init()) != 0)
5198 goto out1;
5199
5200 if ((error = zfs_attach()) != 0)
5201 goto out2;
34dc7c2f
BB
5202
5203 tsd_create(&zfs_fsyncer_key, NULL);
5204 tsd_create(&rrw_tsd_key, NULL);
5205
4b5d425f 5206 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
a23cc0a4 5207 "ZFS pool version %s, ZFS filesystem version %s\n",
4b5d425f 5208 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
a23cc0a4 5209 SPA_VERSION_STRING, ZPL_VERSION_STRING);
34dc7c2f
BB
5210
5211 return (0);
325f0235
BB
5212
5213out2:
5214 (void) zvol_fini();
5215out1:
5216 zfs_fini();
5217 spa_fini();
4b5d425f
BB
5218 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5219 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
5220 ZFS_DEBUG_STR, error);
325f0235
BB
5221
5222 return (error);
34dc7c2f
BB
5223}
5224
5225int
5226_fini(void)
5227{
325f0235 5228 zfs_detach();
34dc7c2f
BB
5229 zvol_fini();
5230 zfs_fini();
5231 spa_fini();
46e18b3f 5232
325f0235 5233 tsd_destroy(&zfs_fsyncer_key);
3fc050aa 5234 tsd_destroy(&rrw_tsd_key);
34dc7c2f 5235
4b5d425f
BB
5236 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
5237 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
34dc7c2f 5238
325f0235 5239 return (0);
34dc7c2f 5240}
325f0235
BB
5241
5242#ifdef HAVE_SPL
5243spl_module_init(_init);
5244spl_module_exit(_fini);
5245
5246MODULE_DESCRIPTION("ZFS");
5247MODULE_AUTHOR(ZFS_META_AUTHOR);
5248MODULE_LICENSE(ZFS_META_LICENSE);
5249#endif /* HAVE_SPL */