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