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