]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ioctl.c
Call zvol_create_minors() in spa_open_common() when initializing pool
[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 nvlist_free(config);
1272
1273 if (props)
1274 nvlist_free(props);
1275
1276 return (error);
1277 }
1278
1279 static int
1280 zfs_ioc_pool_export(zfs_cmd_t *zc)
1281 {
1282 int error;
1283 boolean_t force = (boolean_t)zc->zc_cookie;
1284 boolean_t hardforce = (boolean_t)zc->zc_guid;
1285
1286 zfs_log_history(zc);
1287 error = spa_export(zc->zc_name, NULL, force, hardforce);
1288 if (error == 0)
1289 zvol_remove_minors(zc->zc_name);
1290 return (error);
1291 }
1292
1293 static int
1294 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1295 {
1296 nvlist_t *configs;
1297 int error;
1298
1299 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1300 return (EEXIST);
1301
1302 error = put_nvlist(zc, configs);
1303
1304 nvlist_free(configs);
1305
1306 return (error);
1307 }
1308
1309 /*
1310 * inputs:
1311 * zc_name name of the pool
1312 *
1313 * outputs:
1314 * zc_cookie real errno
1315 * zc_nvlist_dst config nvlist
1316 * zc_nvlist_dst_size size of config nvlist
1317 */
1318 static int
1319 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1320 {
1321 nvlist_t *config;
1322 int error;
1323 int ret = 0;
1324
1325 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1326 sizeof (zc->zc_value));
1327
1328 if (config != NULL) {
1329 ret = put_nvlist(zc, config);
1330 nvlist_free(config);
1331
1332 /*
1333 * The config may be present even if 'error' is non-zero.
1334 * In this case we return success, and preserve the real errno
1335 * in 'zc_cookie'.
1336 */
1337 zc->zc_cookie = error;
1338 } else {
1339 ret = error;
1340 }
1341
1342 return (ret);
1343 }
1344
1345 /*
1346 * Try to import the given pool, returning pool stats as appropriate so that
1347 * user land knows which devices are available and overall pool health.
1348 */
1349 static int
1350 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1351 {
1352 nvlist_t *tryconfig, *config;
1353 int error;
1354
1355 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1356 zc->zc_iflags, &tryconfig)) != 0)
1357 return (error);
1358
1359 config = spa_tryimport(tryconfig);
1360
1361 nvlist_free(tryconfig);
1362
1363 if (config == NULL)
1364 return (EINVAL);
1365
1366 error = put_nvlist(zc, config);
1367 nvlist_free(config);
1368
1369 return (error);
1370 }
1371
1372 /*
1373 * inputs:
1374 * zc_name name of the pool
1375 * zc_cookie scan func (pool_scan_func_t)
1376 */
1377 static int
1378 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1379 {
1380 spa_t *spa;
1381 int error;
1382
1383 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1384 return (error);
1385
1386 if (zc->zc_cookie == POOL_SCAN_NONE)
1387 error = spa_scan_stop(spa);
1388 else
1389 error = spa_scan(spa, zc->zc_cookie);
1390
1391 spa_close(spa, FTAG);
1392
1393 return (error);
1394 }
1395
1396 static int
1397 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1398 {
1399 spa_t *spa;
1400 int error;
1401
1402 error = spa_open(zc->zc_name, &spa, FTAG);
1403 if (error == 0) {
1404 spa_freeze(spa);
1405 spa_close(spa, FTAG);
1406 }
1407 return (error);
1408 }
1409
1410 static int
1411 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1412 {
1413 spa_t *spa;
1414 int error;
1415
1416 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1417 return (error);
1418
1419 if (zc->zc_cookie < spa_version(spa) ||
1420 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1421 spa_close(spa, FTAG);
1422 return (EINVAL);
1423 }
1424
1425 spa_upgrade(spa, zc->zc_cookie);
1426 spa_close(spa, FTAG);
1427
1428 return (error);
1429 }
1430
1431 static int
1432 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1433 {
1434 spa_t *spa;
1435 char *hist_buf;
1436 uint64_t size;
1437 int error;
1438
1439 if ((size = zc->zc_history_len) == 0)
1440 return (EINVAL);
1441
1442 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1443 return (error);
1444
1445 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1446 spa_close(spa, FTAG);
1447 return (ENOTSUP);
1448 }
1449
1450 hist_buf = vmem_alloc(size, KM_SLEEP);
1451 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1452 &zc->zc_history_len, hist_buf)) == 0) {
1453 error = ddi_copyout(hist_buf,
1454 (void *)(uintptr_t)zc->zc_history,
1455 zc->zc_history_len, zc->zc_iflags);
1456 }
1457
1458 spa_close(spa, FTAG);
1459 vmem_free(hist_buf, size);
1460 return (error);
1461 }
1462
1463 static int
1464 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1465 {
1466 spa_t *spa;
1467 int error;
1468
1469 error = spa_open(zc->zc_name, &spa, FTAG);
1470 if (error == 0) {
1471 error = spa_change_guid(spa);
1472 spa_close(spa, FTAG);
1473 }
1474 return (error);
1475 }
1476
1477 static int
1478 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1479 {
1480 int error;
1481
1482 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1483 return (error);
1484
1485 return (0);
1486 }
1487
1488 /*
1489 * inputs:
1490 * zc_name name of filesystem
1491 * zc_obj object to find
1492 *
1493 * outputs:
1494 * zc_value name of object
1495 */
1496 static int
1497 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1498 {
1499 objset_t *os;
1500 int error;
1501
1502 /* XXX reading from objset not owned */
1503 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1504 return (error);
1505 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1506 dmu_objset_rele(os, FTAG);
1507 return (EINVAL);
1508 }
1509 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1510 sizeof (zc->zc_value));
1511 dmu_objset_rele(os, FTAG);
1512
1513 return (error);
1514 }
1515
1516 /*
1517 * inputs:
1518 * zc_name name of filesystem
1519 * zc_obj object to find
1520 *
1521 * outputs:
1522 * zc_stat stats on object
1523 * zc_value path to object
1524 */
1525 static int
1526 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1527 {
1528 objset_t *os;
1529 int error;
1530
1531 /* XXX reading from objset not owned */
1532 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1533 return (error);
1534 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1535 dmu_objset_rele(os, FTAG);
1536 return (EINVAL);
1537 }
1538 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1539 sizeof (zc->zc_value));
1540 dmu_objset_rele(os, FTAG);
1541
1542 return (error);
1543 }
1544
1545 static int
1546 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1547 {
1548 spa_t *spa;
1549 int error;
1550 nvlist_t *config, **l2cache, **spares;
1551 uint_t nl2cache = 0, nspares = 0;
1552
1553 error = spa_open(zc->zc_name, &spa, FTAG);
1554 if (error != 0)
1555 return (error);
1556
1557 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1558 zc->zc_iflags, &config);
1559 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1560 &l2cache, &nl2cache);
1561
1562 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1563 &spares, &nspares);
1564
1565 /*
1566 * A root pool with concatenated devices is not supported.
1567 * Thus, can not add a device to a root pool.
1568 *
1569 * Intent log device can not be added to a rootpool because
1570 * during mountroot, zil is replayed, a seperated log device
1571 * can not be accessed during the mountroot time.
1572 *
1573 * l2cache and spare devices are ok to be added to a rootpool.
1574 */
1575 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1576 nvlist_free(config);
1577 spa_close(spa, FTAG);
1578 return (EDOM);
1579 }
1580
1581 if (error == 0) {
1582 error = spa_vdev_add(spa, config);
1583 nvlist_free(config);
1584 }
1585 spa_close(spa, FTAG);
1586 return (error);
1587 }
1588
1589 /*
1590 * inputs:
1591 * zc_name name of the pool
1592 * zc_nvlist_conf nvlist of devices to remove
1593 * zc_cookie to stop the remove?
1594 */
1595 static int
1596 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1597 {
1598 spa_t *spa;
1599 int error;
1600
1601 error = spa_open(zc->zc_name, &spa, FTAG);
1602 if (error != 0)
1603 return (error);
1604 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1605 spa_close(spa, FTAG);
1606 return (error);
1607 }
1608
1609 static int
1610 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1611 {
1612 spa_t *spa;
1613 int error;
1614 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1615
1616 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1617 return (error);
1618 switch (zc->zc_cookie) {
1619 case VDEV_STATE_ONLINE:
1620 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1621 break;
1622
1623 case VDEV_STATE_OFFLINE:
1624 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1625 break;
1626
1627 case VDEV_STATE_FAULTED:
1628 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1629 zc->zc_obj != VDEV_AUX_EXTERNAL)
1630 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1631
1632 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1633 break;
1634
1635 case VDEV_STATE_DEGRADED:
1636 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1637 zc->zc_obj != VDEV_AUX_EXTERNAL)
1638 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1639
1640 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1641 break;
1642
1643 default:
1644 error = EINVAL;
1645 }
1646 zc->zc_cookie = newstate;
1647 spa_close(spa, FTAG);
1648 return (error);
1649 }
1650
1651 static int
1652 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1653 {
1654 spa_t *spa;
1655 int replacing = zc->zc_cookie;
1656 nvlist_t *config;
1657 int error;
1658
1659 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1660 return (error);
1661
1662 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1663 zc->zc_iflags, &config)) == 0) {
1664 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1665 nvlist_free(config);
1666 }
1667
1668 spa_close(spa, FTAG);
1669 return (error);
1670 }
1671
1672 static int
1673 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1674 {
1675 spa_t *spa;
1676 int error;
1677
1678 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1679 return (error);
1680
1681 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1682
1683 spa_close(spa, FTAG);
1684 return (error);
1685 }
1686
1687 static int
1688 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1689 {
1690 spa_t *spa;
1691 nvlist_t *config, *props = NULL;
1692 int error;
1693 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1694
1695 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1696 return (error);
1697
1698 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1699 zc->zc_iflags, &config))) {
1700 spa_close(spa, FTAG);
1701 return (error);
1702 }
1703
1704 if (zc->zc_nvlist_src_size != 0 && (error =
1705 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1706 zc->zc_iflags, &props))) {
1707 spa_close(spa, FTAG);
1708 nvlist_free(config);
1709 return (error);
1710 }
1711
1712 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1713
1714 spa_close(spa, FTAG);
1715
1716 nvlist_free(config);
1717 nvlist_free(props);
1718
1719 return (error);
1720 }
1721
1722 static int
1723 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1724 {
1725 spa_t *spa;
1726 char *path = zc->zc_value;
1727 uint64_t guid = zc->zc_guid;
1728 int error;
1729
1730 error = spa_open(zc->zc_name, &spa, FTAG);
1731 if (error != 0)
1732 return (error);
1733
1734 error = spa_vdev_setpath(spa, guid, path);
1735 spa_close(spa, FTAG);
1736 return (error);
1737 }
1738
1739 static int
1740 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1741 {
1742 spa_t *spa;
1743 char *fru = zc->zc_value;
1744 uint64_t guid = zc->zc_guid;
1745 int error;
1746
1747 error = spa_open(zc->zc_name, &spa, FTAG);
1748 if (error != 0)
1749 return (error);
1750
1751 error = spa_vdev_setfru(spa, guid, fru);
1752 spa_close(spa, FTAG);
1753 return (error);
1754 }
1755
1756 static int
1757 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1758 {
1759 int error = 0;
1760 nvlist_t *nv;
1761
1762 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1763
1764 if (zc->zc_nvlist_dst != 0 &&
1765 (error = dsl_prop_get_all(os, &nv)) == 0) {
1766 dmu_objset_stats(os, nv);
1767 /*
1768 * NB: zvol_get_stats() will read the objset contents,
1769 * which we aren't supposed to do with a
1770 * DS_MODE_USER hold, because it could be
1771 * inconsistent. So this is a bit of a workaround...
1772 * XXX reading with out owning
1773 */
1774 if (!zc->zc_objset_stats.dds_inconsistent &&
1775 dmu_objset_type(os) == DMU_OST_ZVOL) {
1776 error = zvol_get_stats(os, nv);
1777 if (error == EIO)
1778 return (error);
1779 VERIFY0(error);
1780 }
1781 if (error == 0)
1782 error = put_nvlist(zc, nv);
1783 nvlist_free(nv);
1784 }
1785
1786 return (error);
1787 }
1788
1789 /*
1790 * inputs:
1791 * zc_name name of filesystem
1792 * zc_nvlist_dst_size size of buffer for property nvlist
1793 *
1794 * outputs:
1795 * zc_objset_stats stats
1796 * zc_nvlist_dst property nvlist
1797 * zc_nvlist_dst_size size of property nvlist
1798 */
1799 static int
1800 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1801 {
1802 objset_t *os = NULL;
1803 int error;
1804
1805 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1806 return (error);
1807
1808 error = zfs_ioc_objset_stats_impl(zc, os);
1809
1810 dmu_objset_rele(os, FTAG);
1811
1812 return (error);
1813 }
1814
1815 /*
1816 * inputs:
1817 * zc_name name of filesystem
1818 * zc_nvlist_dst_size size of buffer for property nvlist
1819 *
1820 * outputs:
1821 * zc_nvlist_dst received property nvlist
1822 * zc_nvlist_dst_size size of received property nvlist
1823 *
1824 * Gets received properties (distinct from local properties on or after
1825 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1826 * local property values.
1827 */
1828 static int
1829 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1830 {
1831 objset_t *os = NULL;
1832 int error;
1833 nvlist_t *nv;
1834
1835 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1836 return (error);
1837
1838 /*
1839 * Without this check, we would return local property values if the
1840 * caller has not already received properties on or after
1841 * SPA_VERSION_RECVD_PROPS.
1842 */
1843 if (!dsl_prop_get_hasrecvd(os)) {
1844 dmu_objset_rele(os, FTAG);
1845 return (ENOTSUP);
1846 }
1847
1848 if (zc->zc_nvlist_dst != 0 &&
1849 (error = dsl_prop_get_received(os, &nv)) == 0) {
1850 error = put_nvlist(zc, nv);
1851 nvlist_free(nv);
1852 }
1853
1854 dmu_objset_rele(os, FTAG);
1855 return (error);
1856 }
1857
1858 static int
1859 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1860 {
1861 uint64_t value;
1862 int error;
1863
1864 /*
1865 * zfs_get_zplprop() will either find a value or give us
1866 * the default value (if there is one).
1867 */
1868 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1869 return (error);
1870 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1871 return (0);
1872 }
1873
1874 /*
1875 * inputs:
1876 * zc_name name of filesystem
1877 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1878 *
1879 * outputs:
1880 * zc_nvlist_dst zpl property nvlist
1881 * zc_nvlist_dst_size size of zpl property nvlist
1882 */
1883 static int
1884 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1885 {
1886 objset_t *os;
1887 int err;
1888
1889 /* XXX reading without owning */
1890 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1891 return (err);
1892
1893 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1894
1895 /*
1896 * NB: nvl_add_zplprop() will read the objset contents,
1897 * which we aren't supposed to do with a DS_MODE_USER
1898 * hold, because it could be inconsistent.
1899 */
1900 if (zc->zc_nvlist_dst != 0 &&
1901 !zc->zc_objset_stats.dds_inconsistent &&
1902 dmu_objset_type(os) == DMU_OST_ZFS) {
1903 nvlist_t *nv;
1904
1905 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1906 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1907 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1908 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1909 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1910 err = put_nvlist(zc, nv);
1911 nvlist_free(nv);
1912 } else {
1913 err = ENOENT;
1914 }
1915 dmu_objset_rele(os, FTAG);
1916 return (err);
1917 }
1918
1919 static boolean_t
1920 dataset_name_hidden(const char *name)
1921 {
1922 /*
1923 * Skip over datasets that are not visible in this zone,
1924 * internal datasets (which have a $ in their name), and
1925 * temporary datasets (which have a % in their name).
1926 */
1927 if (strchr(name, '$') != NULL)
1928 return (B_TRUE);
1929 if (strchr(name, '%') != NULL)
1930 return (B_TRUE);
1931 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1932 return (B_TRUE);
1933 return (B_FALSE);
1934 }
1935
1936 /*
1937 * inputs:
1938 * zc_name name of filesystem
1939 * zc_cookie zap cursor
1940 * zc_nvlist_dst_size size of buffer for property nvlist
1941 *
1942 * outputs:
1943 * zc_name name of next filesystem
1944 * zc_cookie zap cursor
1945 * zc_objset_stats stats
1946 * zc_nvlist_dst property nvlist
1947 * zc_nvlist_dst_size size of property nvlist
1948 */
1949 static int
1950 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1951 {
1952 objset_t *os;
1953 int error;
1954 char *p;
1955 size_t orig_len = strlen(zc->zc_name);
1956
1957 top:
1958 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1959 if (error == ENOENT)
1960 error = ESRCH;
1961 return (error);
1962 }
1963
1964 p = strrchr(zc->zc_name, '/');
1965 if (p == NULL || p[1] != '\0')
1966 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1967 p = zc->zc_name + strlen(zc->zc_name);
1968
1969 /*
1970 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1971 * but is not declared void because its called by dmu_objset_find().
1972 */
1973 if (zc->zc_cookie == 0) {
1974 uint64_t cookie = 0;
1975 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1976
1977 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
1978 if (!dataset_name_hidden(zc->zc_name))
1979 (void) dmu_objset_prefetch(zc->zc_name, NULL);
1980 }
1981 }
1982
1983 do {
1984 error = dmu_dir_list_next(os,
1985 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1986 NULL, &zc->zc_cookie);
1987 if (error == ENOENT)
1988 error = ESRCH;
1989 } while (error == 0 && dataset_name_hidden(zc->zc_name));
1990 dmu_objset_rele(os, FTAG);
1991
1992 /*
1993 * If it's an internal dataset (ie. with a '$' in its name),
1994 * don't try to get stats for it, otherwise we'll return ENOENT.
1995 */
1996 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1997 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1998 if (error == ENOENT) {
1999 /* We lost a race with destroy, get the next one. */
2000 zc->zc_name[orig_len] = '\0';
2001 goto top;
2002 }
2003 }
2004 return (error);
2005 }
2006
2007 /*
2008 * inputs:
2009 * zc_name name of filesystem
2010 * zc_cookie zap cursor
2011 * zc_nvlist_dst_size size of buffer for property nvlist
2012 *
2013 * outputs:
2014 * zc_name name of next snapshot
2015 * zc_objset_stats stats
2016 * zc_nvlist_dst property nvlist
2017 * zc_nvlist_dst_size size of property nvlist
2018 */
2019 static int
2020 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2021 {
2022 objset_t *os;
2023 int error;
2024
2025 top:
2026 if (zc->zc_cookie == 0 && !zc->zc_simple)
2027 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2028 NULL, DS_FIND_SNAPSHOTS);
2029
2030 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2031 if (error)
2032 return (error == ENOENT ? ESRCH : error);
2033
2034 /*
2035 * A dataset name of maximum length cannot have any snapshots,
2036 * so exit immediately.
2037 */
2038 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2039 dmu_objset_rele(os, FTAG);
2040 return (ESRCH);
2041 }
2042
2043 error = dmu_snapshot_list_next(os,
2044 sizeof (zc->zc_name) - strlen(zc->zc_name),
2045 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2046 NULL);
2047
2048 if (error == 0 && !zc->zc_simple) {
2049 dsl_dataset_t *ds;
2050 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2051
2052 /*
2053 * Since we probably don't have a hold on this snapshot,
2054 * it's possible that the objsetid could have been destroyed
2055 * and reused for a new objset. It's OK if this happens during
2056 * a zfs send operation, since the new createtxg will be
2057 * beyond the range we're interested in.
2058 */
2059 rw_enter(&dp->dp_config_rwlock, RW_READER);
2060 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2061 rw_exit(&dp->dp_config_rwlock);
2062 if (error) {
2063 if (error == ENOENT) {
2064 /* Racing with destroy, get the next one. */
2065 *strchr(zc->zc_name, '@') = '\0';
2066 dmu_objset_rele(os, FTAG);
2067 goto top;
2068 }
2069 } else {
2070 objset_t *ossnap;
2071
2072 error = dmu_objset_from_ds(ds, &ossnap);
2073 if (error == 0)
2074 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2075 dsl_dataset_rele(ds, FTAG);
2076 }
2077 } else if (error == ENOENT) {
2078 error = ESRCH;
2079 }
2080
2081 dmu_objset_rele(os, FTAG);
2082 /* if we failed, undo the @ that we tacked on to zc_name */
2083 if (error)
2084 *strchr(zc->zc_name, '@') = '\0';
2085 return (error);
2086 }
2087
2088 static int
2089 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2090 {
2091 const char *propname = nvpair_name(pair);
2092 uint64_t *valary;
2093 unsigned int vallen;
2094 const char *domain;
2095 char *dash;
2096 zfs_userquota_prop_t type;
2097 uint64_t rid;
2098 uint64_t quota;
2099 zfs_sb_t *zsb;
2100 int err;
2101
2102 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2103 nvlist_t *attrs;
2104 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2105 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2106 &pair) != 0)
2107 return (EINVAL);
2108 }
2109
2110 /*
2111 * A correctly constructed propname is encoded as
2112 * userquota@<rid>-<domain>.
2113 */
2114 if ((dash = strchr(propname, '-')) == NULL ||
2115 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2116 vallen != 3)
2117 return (EINVAL);
2118
2119 domain = dash + 1;
2120 type = valary[0];
2121 rid = valary[1];
2122 quota = valary[2];
2123
2124 err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2125 if (err == 0) {
2126 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2127 zfs_sb_rele(zsb, FTAG);
2128 }
2129
2130 return (err);
2131 }
2132
2133 /*
2134 * If the named property is one that has a special function to set its value,
2135 * return 0 on success and a positive error code on failure; otherwise if it is
2136 * not one of the special properties handled by this function, return -1.
2137 *
2138 * XXX: It would be better for callers of the property interface if we handled
2139 * these special cases in dsl_prop.c (in the dsl layer).
2140 */
2141 static int
2142 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2143 nvpair_t *pair)
2144 {
2145 const char *propname = nvpair_name(pair);
2146 zfs_prop_t prop = zfs_name_to_prop(propname);
2147 uint64_t intval;
2148 int err;
2149
2150 if (prop == ZPROP_INVAL) {
2151 if (zfs_prop_userquota(propname))
2152 return (zfs_prop_set_userquota(dsname, pair));
2153 return (-1);
2154 }
2155
2156 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2157 nvlist_t *attrs;
2158 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2159 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2160 &pair) == 0);
2161 }
2162
2163 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2164 return (-1);
2165
2166 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2167
2168 switch (prop) {
2169 case ZFS_PROP_QUOTA:
2170 err = dsl_dir_set_quota(dsname, source, intval);
2171 break;
2172 case ZFS_PROP_REFQUOTA:
2173 err = dsl_dataset_set_quota(dsname, source, intval);
2174 break;
2175 case ZFS_PROP_RESERVATION:
2176 err = dsl_dir_set_reservation(dsname, source, intval);
2177 break;
2178 case ZFS_PROP_REFRESERVATION:
2179 err = dsl_dataset_set_reservation(dsname, source, intval);
2180 break;
2181 case ZFS_PROP_VOLSIZE:
2182 err = zvol_set_volsize(dsname, intval);
2183 break;
2184 case ZFS_PROP_SNAPDEV:
2185 err = zvol_set_snapdev(dsname, intval);
2186 break;
2187 case ZFS_PROP_VERSION:
2188 {
2189 zfs_sb_t *zsb;
2190
2191 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2192 break;
2193
2194 err = zfs_set_version(zsb, intval);
2195 zfs_sb_rele(zsb, FTAG);
2196
2197 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2198 zfs_cmd_t *zc;
2199
2200 zc = kmem_zalloc(sizeof (zfs_cmd_t),
2201 KM_SLEEP | KM_NODEBUG);
2202 (void) strcpy(zc->zc_name, dsname);
2203 (void) zfs_ioc_userspace_upgrade(zc);
2204 kmem_free(zc, sizeof (zfs_cmd_t));
2205 }
2206 break;
2207 }
2208 case ZFS_PROP_COMPRESSION:
2209 {
2210 if (intval == ZIO_COMPRESS_LZ4) {
2211 zfeature_info_t *feature =
2212 &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
2213 spa_t *spa;
2214 dsl_pool_t *dp;
2215
2216 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2217 return (err);
2218
2219 dp = spa->spa_dsl_pool;
2220
2221 /*
2222 * Setting the LZ4 compression algorithm activates
2223 * the feature.
2224 */
2225 if (!spa_feature_is_active(spa, feature)) {
2226 if ((err = zfs_prop_activate_feature(dp,
2227 feature)) != 0) {
2228 spa_close(spa, FTAG);
2229 return (err);
2230 }
2231 }
2232
2233 spa_close(spa, FTAG);
2234 }
2235 /*
2236 * We still want the default set action to be performed in the
2237 * caller, we only performed zfeature settings here.
2238 */
2239 err = -1;
2240 break;
2241 }
2242
2243 default:
2244 err = -1;
2245 }
2246
2247 return (err);
2248 }
2249
2250 /*
2251 * This function is best effort. If it fails to set any of the given properties,
2252 * it continues to set as many as it can and returns the first error
2253 * encountered. If the caller provides a non-NULL errlist, it also gives the
2254 * complete list of names of all the properties it failed to set along with the
2255 * corresponding error numbers. The caller is responsible for freeing the
2256 * returned errlist.
2257 *
2258 * If every property is set successfully, zero is returned and the list pointed
2259 * at by errlist is NULL.
2260 */
2261 int
2262 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2263 nvlist_t **errlist)
2264 {
2265 nvpair_t *pair;
2266 nvpair_t *propval;
2267 int rv = 0;
2268 uint64_t intval;
2269 char *strval;
2270 nvlist_t *genericnvl;
2271 nvlist_t *errors;
2272 nvlist_t *retrynvl;
2273
2274 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2275 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2276 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2277
2278 retry:
2279 pair = NULL;
2280 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2281 const char *propname = nvpair_name(pair);
2282 zfs_prop_t prop = zfs_name_to_prop(propname);
2283 int err = 0;
2284
2285 /* decode the property value */
2286 propval = pair;
2287 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2288 nvlist_t *attrs;
2289 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2290 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2291 &propval) != 0)
2292 err = EINVAL;
2293 }
2294
2295 /* Validate value type */
2296 if (err == 0 && prop == ZPROP_INVAL) {
2297 if (zfs_prop_user(propname)) {
2298 if (nvpair_type(propval) != DATA_TYPE_STRING)
2299 err = EINVAL;
2300 } else if (zfs_prop_userquota(propname)) {
2301 if (nvpair_type(propval) !=
2302 DATA_TYPE_UINT64_ARRAY)
2303 err = EINVAL;
2304 } else {
2305 err = EINVAL;
2306 }
2307 } else if (err == 0) {
2308 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2309 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2310 err = EINVAL;
2311 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2312 const char *unused;
2313
2314 VERIFY(nvpair_value_uint64(propval,
2315 &intval) == 0);
2316
2317 switch (zfs_prop_get_type(prop)) {
2318 case PROP_TYPE_NUMBER:
2319 break;
2320 case PROP_TYPE_STRING:
2321 err = EINVAL;
2322 break;
2323 case PROP_TYPE_INDEX:
2324 if (zfs_prop_index_to_string(prop,
2325 intval, &unused) != 0)
2326 err = EINVAL;
2327 break;
2328 default:
2329 cmn_err(CE_PANIC,
2330 "unknown property type");
2331 }
2332 } else {
2333 err = EINVAL;
2334 }
2335 }
2336
2337 /* Validate permissions */
2338 if (err == 0)
2339 err = zfs_check_settable(dsname, pair, CRED());
2340
2341 if (err == 0) {
2342 err = zfs_prop_set_special(dsname, source, pair);
2343 if (err == -1) {
2344 /*
2345 * For better performance we build up a list of
2346 * properties to set in a single transaction.
2347 */
2348 err = nvlist_add_nvpair(genericnvl, pair);
2349 } else if (err != 0 && nvl != retrynvl) {
2350 /*
2351 * This may be a spurious error caused by
2352 * receiving quota and reservation out of order.
2353 * Try again in a second pass.
2354 */
2355 err = nvlist_add_nvpair(retrynvl, pair);
2356 }
2357 }
2358
2359 if (err != 0)
2360 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2361 }
2362
2363 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2364 nvl = retrynvl;
2365 goto retry;
2366 }
2367
2368 if (!nvlist_empty(genericnvl) &&
2369 dsl_props_set(dsname, source, genericnvl) != 0) {
2370 /*
2371 * If this fails, we still want to set as many properties as we
2372 * can, so try setting them individually.
2373 */
2374 pair = NULL;
2375 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2376 const char *propname = nvpair_name(pair);
2377 int err = 0;
2378
2379 propval = pair;
2380 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2381 nvlist_t *attrs;
2382 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2383 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2384 &propval) == 0);
2385 }
2386
2387 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2388 VERIFY(nvpair_value_string(propval,
2389 &strval) == 0);
2390 err = dsl_prop_set(dsname, propname, source, 1,
2391 strlen(strval) + 1, strval);
2392 } else {
2393 VERIFY(nvpair_value_uint64(propval,
2394 &intval) == 0);
2395 err = dsl_prop_set(dsname, propname, source, 8,
2396 1, &intval);
2397 }
2398
2399 if (err != 0) {
2400 VERIFY(nvlist_add_int32(errors, propname,
2401 err) == 0);
2402 }
2403 }
2404 }
2405 nvlist_free(genericnvl);
2406 nvlist_free(retrynvl);
2407
2408 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2409 nvlist_free(errors);
2410 errors = NULL;
2411 } else {
2412 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2413 }
2414
2415 if (errlist == NULL)
2416 nvlist_free(errors);
2417 else
2418 *errlist = errors;
2419
2420 return (rv);
2421 }
2422
2423 /*
2424 * Check that all the properties are valid user properties.
2425 */
2426 static int
2427 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2428 {
2429 nvpair_t *pair = NULL;
2430 int error = 0;
2431
2432 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2433 const char *propname = nvpair_name(pair);
2434 char *valstr;
2435
2436 if (!zfs_prop_user(propname) ||
2437 nvpair_type(pair) != DATA_TYPE_STRING)
2438 return (EINVAL);
2439
2440 if ((error = zfs_secpolicy_write_perms(fsname,
2441 ZFS_DELEG_PERM_USERPROP, CRED())))
2442 return (error);
2443
2444 if (strlen(propname) >= ZAP_MAXNAMELEN)
2445 return (ENAMETOOLONG);
2446
2447 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2448 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2449 return (E2BIG);
2450 }
2451 return (0);
2452 }
2453
2454 static void
2455 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2456 {
2457 nvpair_t *pair;
2458
2459 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2460
2461 pair = NULL;
2462 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2463 if (nvlist_exists(skipped, nvpair_name(pair)))
2464 continue;
2465
2466 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2467 }
2468 }
2469
2470 static int
2471 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2472 nvlist_t *skipped)
2473 {
2474 int err = 0;
2475 nvlist_t *cleared_props = NULL;
2476 props_skip(props, skipped, &cleared_props);
2477 if (!nvlist_empty(cleared_props)) {
2478 /*
2479 * Acts on local properties until the dataset has received
2480 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2481 */
2482 zprop_source_t flags = (ZPROP_SRC_NONE |
2483 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2484 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2485 }
2486 nvlist_free(cleared_props);
2487 return (err);
2488 }
2489
2490 /*
2491 * inputs:
2492 * zc_name name of filesystem
2493 * zc_value name of property to set
2494 * zc_nvlist_src{_size} nvlist of properties to apply
2495 * zc_cookie received properties flag
2496 *
2497 * outputs:
2498 * zc_nvlist_dst{_size} error for each unapplied received property
2499 */
2500 static int
2501 zfs_ioc_set_prop(zfs_cmd_t *zc)
2502 {
2503 nvlist_t *nvl;
2504 boolean_t received = zc->zc_cookie;
2505 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2506 ZPROP_SRC_LOCAL);
2507 nvlist_t *errors = NULL;
2508 int error;
2509
2510 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2511 zc->zc_iflags, &nvl)) != 0)
2512 return (error);
2513
2514 if (received) {
2515 nvlist_t *origprops;
2516 objset_t *os;
2517
2518 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2519 if (dsl_prop_get_received(os, &origprops) == 0) {
2520 (void) clear_received_props(os,
2521 zc->zc_name, origprops, nvl);
2522 nvlist_free(origprops);
2523 }
2524
2525 dsl_prop_set_hasrecvd(os);
2526 dmu_objset_rele(os, FTAG);
2527 }
2528 }
2529
2530 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2531
2532 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2533 (void) put_nvlist(zc, errors);
2534 }
2535
2536 nvlist_free(errors);
2537 nvlist_free(nvl);
2538 return (error);
2539 }
2540
2541 /*
2542 * inputs:
2543 * zc_name name of filesystem
2544 * zc_value name of property to inherit
2545 * zc_cookie revert to received value if TRUE
2546 *
2547 * outputs: none
2548 */
2549 static int
2550 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2551 {
2552 const char *propname = zc->zc_value;
2553 zfs_prop_t prop = zfs_name_to_prop(propname);
2554 boolean_t received = zc->zc_cookie;
2555 zprop_source_t source = (received
2556 ? ZPROP_SRC_NONE /* revert to received value, if any */
2557 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2558
2559 if (received) {
2560 nvlist_t *dummy;
2561 nvpair_t *pair;
2562 zprop_type_t type;
2563 int err;
2564
2565 /*
2566 * zfs_prop_set_special() expects properties in the form of an
2567 * nvpair with type info.
2568 */
2569 if (prop == ZPROP_INVAL) {
2570 if (!zfs_prop_user(propname))
2571 return (EINVAL);
2572
2573 type = PROP_TYPE_STRING;
2574 } else if (prop == ZFS_PROP_VOLSIZE ||
2575 prop == ZFS_PROP_VERSION) {
2576 return (EINVAL);
2577 } else {
2578 type = zfs_prop_get_type(prop);
2579 }
2580
2581 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2582
2583 switch (type) {
2584 case PROP_TYPE_STRING:
2585 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2586 break;
2587 case PROP_TYPE_NUMBER:
2588 case PROP_TYPE_INDEX:
2589 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2590 break;
2591 default:
2592 nvlist_free(dummy);
2593 return (EINVAL);
2594 }
2595
2596 pair = nvlist_next_nvpair(dummy, NULL);
2597 err = zfs_prop_set_special(zc->zc_name, source, pair);
2598 nvlist_free(dummy);
2599 if (err != -1)
2600 return (err); /* special property already handled */
2601 } else {
2602 /*
2603 * Only check this in the non-received case. We want to allow
2604 * 'inherit -S' to revert non-inheritable properties like quota
2605 * and reservation to the received or default values even though
2606 * they are not considered inheritable.
2607 */
2608 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2609 return (EINVAL);
2610 }
2611
2612 /* the property name has been validated by zfs_secpolicy_inherit() */
2613 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2614 }
2615
2616 static int
2617 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2618 {
2619 nvlist_t *props;
2620 spa_t *spa;
2621 int error;
2622 nvpair_t *pair;
2623
2624 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2625 zc->zc_iflags, &props)))
2626 return (error);
2627
2628 /*
2629 * If the only property is the configfile, then just do a spa_lookup()
2630 * to handle the faulted case.
2631 */
2632 pair = nvlist_next_nvpair(props, NULL);
2633 if (pair != NULL && strcmp(nvpair_name(pair),
2634 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2635 nvlist_next_nvpair(props, pair) == NULL) {
2636 mutex_enter(&spa_namespace_lock);
2637 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2638 spa_configfile_set(spa, props, B_FALSE);
2639 spa_config_sync(spa, B_FALSE, B_TRUE);
2640 }
2641 mutex_exit(&spa_namespace_lock);
2642 if (spa != NULL) {
2643 nvlist_free(props);
2644 return (0);
2645 }
2646 }
2647
2648 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2649 nvlist_free(props);
2650 return (error);
2651 }
2652
2653 error = spa_prop_set(spa, props);
2654
2655 nvlist_free(props);
2656 spa_close(spa, FTAG);
2657
2658 return (error);
2659 }
2660
2661 static int
2662 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2663 {
2664 spa_t *spa;
2665 int error;
2666 nvlist_t *nvp = NULL;
2667
2668 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2669 /*
2670 * If the pool is faulted, there may be properties we can still
2671 * get (such as altroot and cachefile), so attempt to get them
2672 * anyway.
2673 */
2674 mutex_enter(&spa_namespace_lock);
2675 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2676 error = spa_prop_get(spa, &nvp);
2677 mutex_exit(&spa_namespace_lock);
2678 } else {
2679 error = spa_prop_get(spa, &nvp);
2680 spa_close(spa, FTAG);
2681 }
2682
2683 if (error == 0 && zc->zc_nvlist_dst != 0)
2684 error = put_nvlist(zc, nvp);
2685 else
2686 error = EFAULT;
2687
2688 nvlist_free(nvp);
2689 return (error);
2690 }
2691
2692 /*
2693 * inputs:
2694 * zc_name name of volume
2695 *
2696 * outputs: none
2697 */
2698 static int
2699 zfs_ioc_create_minor(zfs_cmd_t *zc)
2700 {
2701 return (zvol_create_minor(zc->zc_name));
2702 }
2703
2704 /*
2705 * inputs:
2706 * zc_name name of volume
2707 *
2708 * outputs: none
2709 */
2710 static int
2711 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2712 {
2713 return (zvol_remove_minor(zc->zc_name));
2714 }
2715
2716 /*
2717 * inputs:
2718 * zc_name name of filesystem
2719 * zc_nvlist_src{_size} nvlist of delegated permissions
2720 * zc_perm_action allow/unallow flag
2721 *
2722 * outputs: none
2723 */
2724 static int
2725 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2726 {
2727 int error;
2728 nvlist_t *fsaclnv = NULL;
2729
2730 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2731 zc->zc_iflags, &fsaclnv)) != 0)
2732 return (error);
2733
2734 /*
2735 * Verify nvlist is constructed correctly
2736 */
2737 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2738 nvlist_free(fsaclnv);
2739 return (EINVAL);
2740 }
2741
2742 /*
2743 * If we don't have PRIV_SYS_MOUNT, then validate
2744 * that user is allowed to hand out each permission in
2745 * the nvlist(s)
2746 */
2747
2748 error = secpolicy_zfs(CRED());
2749 if (error) {
2750 if (zc->zc_perm_action == B_FALSE) {
2751 error = dsl_deleg_can_allow(zc->zc_name,
2752 fsaclnv, CRED());
2753 } else {
2754 error = dsl_deleg_can_unallow(zc->zc_name,
2755 fsaclnv, CRED());
2756 }
2757 }
2758
2759 if (error == 0)
2760 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2761
2762 nvlist_free(fsaclnv);
2763 return (error);
2764 }
2765
2766 /*
2767 * inputs:
2768 * zc_name name of filesystem
2769 *
2770 * outputs:
2771 * zc_nvlist_src{_size} nvlist of delegated permissions
2772 */
2773 static int
2774 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2775 {
2776 nvlist_t *nvp;
2777 int error;
2778
2779 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2780 error = put_nvlist(zc, nvp);
2781 nvlist_free(nvp);
2782 }
2783
2784 return (error);
2785 }
2786
2787 /* ARGSUSED */
2788 static void
2789 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2790 {
2791 zfs_creat_t *zct = arg;
2792
2793 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2794 }
2795
2796 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2797
2798 /*
2799 * inputs:
2800 * createprops list of properties requested by creator
2801 * default_zplver zpl version to use if unspecified in createprops
2802 * fuids_ok fuids allowed in this version of the spa?
2803 * os parent objset pointer (NULL if root fs)
2804 *
2805 * outputs:
2806 * zplprops values for the zplprops we attach to the master node object
2807 * is_ci true if requested file system will be purely case-insensitive
2808 *
2809 * Determine the settings for utf8only, normalization and
2810 * casesensitivity. Specific values may have been requested by the
2811 * creator and/or we can inherit values from the parent dataset. If
2812 * the file system is of too early a vintage, a creator can not
2813 * request settings for these properties, even if the requested
2814 * setting is the default value. We don't actually want to create dsl
2815 * properties for these, so remove them from the source nvlist after
2816 * processing.
2817 */
2818 static int
2819 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2820 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2821 nvlist_t *zplprops, boolean_t *is_ci)
2822 {
2823 uint64_t sense = ZFS_PROP_UNDEFINED;
2824 uint64_t norm = ZFS_PROP_UNDEFINED;
2825 uint64_t u8 = ZFS_PROP_UNDEFINED;
2826 int error;
2827
2828 ASSERT(zplprops != NULL);
2829
2830 /*
2831 * Pull out creator prop choices, if any.
2832 */
2833 if (createprops) {
2834 (void) nvlist_lookup_uint64(createprops,
2835 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2836 (void) nvlist_lookup_uint64(createprops,
2837 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2838 (void) nvlist_remove_all(createprops,
2839 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2840 (void) nvlist_lookup_uint64(createprops,
2841 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2842 (void) nvlist_remove_all(createprops,
2843 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2844 (void) nvlist_lookup_uint64(createprops,
2845 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2846 (void) nvlist_remove_all(createprops,
2847 zfs_prop_to_name(ZFS_PROP_CASE));
2848 }
2849
2850 /*
2851 * If the zpl version requested is whacky or the file system
2852 * or pool is version is too "young" to support normalization
2853 * and the creator tried to set a value for one of the props,
2854 * error out.
2855 */
2856 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2857 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2858 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2859 (zplver < ZPL_VERSION_NORMALIZATION &&
2860 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2861 sense != ZFS_PROP_UNDEFINED)))
2862 return (ENOTSUP);
2863
2864 /*
2865 * Put the version in the zplprops
2866 */
2867 VERIFY(nvlist_add_uint64(zplprops,
2868 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2869
2870 if (norm == ZFS_PROP_UNDEFINED &&
2871 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
2872 return (error);
2873 VERIFY(nvlist_add_uint64(zplprops,
2874 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2875
2876 /*
2877 * If we're normalizing, names must always be valid UTF-8 strings.
2878 */
2879 if (norm)
2880 u8 = 1;
2881 if (u8 == ZFS_PROP_UNDEFINED &&
2882 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
2883 return (error);
2884 VERIFY(nvlist_add_uint64(zplprops,
2885 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2886
2887 if (sense == ZFS_PROP_UNDEFINED &&
2888 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
2889 return (error);
2890 VERIFY(nvlist_add_uint64(zplprops,
2891 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2892
2893 if (is_ci)
2894 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2895
2896 return (0);
2897 }
2898
2899 static int
2900 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2901 nvlist_t *zplprops, boolean_t *is_ci)
2902 {
2903 boolean_t fuids_ok, sa_ok;
2904 uint64_t zplver = ZPL_VERSION;
2905 objset_t *os = NULL;
2906 char parentname[MAXNAMELEN];
2907 char *cp;
2908 spa_t *spa;
2909 uint64_t spa_vers;
2910 int error;
2911
2912 (void) strlcpy(parentname, dataset, sizeof (parentname));
2913 cp = strrchr(parentname, '/');
2914 ASSERT(cp != NULL);
2915 cp[0] = '\0';
2916
2917 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2918 return (error);
2919
2920 spa_vers = spa_version(spa);
2921 spa_close(spa, FTAG);
2922
2923 zplver = zfs_zpl_version_map(spa_vers);
2924 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2925 sa_ok = (zplver >= ZPL_VERSION_SA);
2926
2927 /*
2928 * Open parent object set so we can inherit zplprop values.
2929 */
2930 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2931 return (error);
2932
2933 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2934 zplprops, is_ci);
2935 dmu_objset_rele(os, FTAG);
2936 return (error);
2937 }
2938
2939 static int
2940 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2941 nvlist_t *zplprops, boolean_t *is_ci)
2942 {
2943 boolean_t fuids_ok;
2944 boolean_t sa_ok;
2945 uint64_t zplver = ZPL_VERSION;
2946 int error;
2947
2948 zplver = zfs_zpl_version_map(spa_vers);
2949 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2950 sa_ok = (zplver >= ZPL_VERSION_SA);
2951
2952 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2953 createprops, zplprops, is_ci);
2954 return (error);
2955 }
2956
2957 /*
2958 * inputs:
2959 * zc_objset_type type of objset to create (fs vs zvol)
2960 * zc_name name of new objset
2961 * zc_value name of snapshot to clone from (may be empty)
2962 * zc_nvlist_src{_size} nvlist of properties to apply
2963 *
2964 * outputs: none
2965 */
2966 static int
2967 zfs_ioc_create(zfs_cmd_t *zc)
2968 {
2969 objset_t *clone;
2970 int error = 0;
2971 zfs_creat_t zct;
2972 nvlist_t *nvprops = NULL;
2973 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2974 dmu_objset_type_t type = zc->zc_objset_type;
2975
2976 switch (type) {
2977
2978 case DMU_OST_ZFS:
2979 cbfunc = zfs_create_cb;
2980 break;
2981
2982 case DMU_OST_ZVOL:
2983 cbfunc = zvol_create_cb;
2984 break;
2985
2986 default:
2987 cbfunc = NULL;
2988 break;
2989 }
2990 if (strchr(zc->zc_name, '@') ||
2991 strchr(zc->zc_name, '%'))
2992 return (EINVAL);
2993
2994 if (zc->zc_nvlist_src != 0 &&
2995 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2996 zc->zc_iflags, &nvprops)) != 0)
2997 return (error);
2998
2999 zct.zct_zplprops = NULL;
3000 zct.zct_props = nvprops;
3001
3002 if (zc->zc_value[0] != '\0') {
3003 /*
3004 * We're creating a clone of an existing snapshot.
3005 */
3006 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3007 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
3008 nvlist_free(nvprops);
3009 return (EINVAL);
3010 }
3011
3012 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
3013 if (error) {
3014 nvlist_free(nvprops);
3015 return (error);
3016 }
3017
3018 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
3019 dmu_objset_rele(clone, FTAG);
3020 if (error) {
3021 nvlist_free(nvprops);
3022 return (error);
3023 }
3024 } else {
3025 boolean_t is_insensitive = B_FALSE;
3026
3027 if (cbfunc == NULL) {
3028 nvlist_free(nvprops);
3029 return (EINVAL);
3030 }
3031
3032 if (type == DMU_OST_ZVOL) {
3033 uint64_t volsize, volblocksize;
3034
3035 if (nvprops == NULL ||
3036 nvlist_lookup_uint64(nvprops,
3037 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
3038 &volsize) != 0) {
3039 nvlist_free(nvprops);
3040 return (EINVAL);
3041 }
3042
3043 if ((error = nvlist_lookup_uint64(nvprops,
3044 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3045 &volblocksize)) != 0 && error != ENOENT) {
3046 nvlist_free(nvprops);
3047 return (EINVAL);
3048 }
3049
3050 if (error != 0)
3051 volblocksize = zfs_prop_default_numeric(
3052 ZFS_PROP_VOLBLOCKSIZE);
3053
3054 if ((error = zvol_check_volblocksize(
3055 volblocksize)) != 0 ||
3056 (error = zvol_check_volsize(volsize,
3057 volblocksize)) != 0) {
3058 nvlist_free(nvprops);
3059 return (error);
3060 }
3061 } else if (type == DMU_OST_ZFS) {
3062 int error;
3063
3064 /*
3065 * We have to have normalization and
3066 * case-folding flags correct when we do the
3067 * file system creation, so go figure them out
3068 * now.
3069 */
3070 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3071 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3072 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3073 zct.zct_zplprops, &is_insensitive);
3074 if (error != 0) {
3075 nvlist_free(nvprops);
3076 nvlist_free(zct.zct_zplprops);
3077 return (error);
3078 }
3079 }
3080 error = dmu_objset_create(zc->zc_name, type,
3081 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3082 nvlist_free(zct.zct_zplprops);
3083 }
3084
3085 /*
3086 * It would be nice to do this atomically.
3087 */
3088 if (error == 0) {
3089 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3090 nvprops, NULL);
3091 if (error != 0)
3092 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3093 }
3094 nvlist_free(nvprops);
3095 return (error);
3096 }
3097
3098 /*
3099 * inputs:
3100 * zc_name name of filesystem
3101 * zc_value short name of snapshot
3102 * zc_cookie recursive flag
3103 * zc_nvlist_src[_size] property list
3104 *
3105 * outputs:
3106 * zc_value short snapname (i.e. part after the '@')
3107 */
3108 static int
3109 zfs_ioc_snapshot(zfs_cmd_t *zc)
3110 {
3111 nvlist_t *nvprops = NULL;
3112 int error;
3113 boolean_t recursive = zc->zc_cookie;
3114
3115 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3116 return (EINVAL);
3117
3118 if (zc->zc_nvlist_src != 0 &&
3119 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3120 zc->zc_iflags, &nvprops)) != 0)
3121 return (error);
3122
3123 error = zfs_check_userprops(zc->zc_name, nvprops);
3124 if (error)
3125 goto out;
3126
3127 if (!nvlist_empty(nvprops) &&
3128 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3129 error = ENOTSUP;
3130 goto out;
3131 }
3132
3133 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3134 nvprops, recursive, B_FALSE, -1);
3135
3136 out:
3137 nvlist_free(nvprops);
3138 return (error);
3139 }
3140
3141 /*
3142 * inputs:
3143 * name dataset name, or when 'arg == NULL' the full snapshot name
3144 * arg short snapshot name (i.e. part after the '@')
3145 */
3146 int
3147 zfs_unmount_snap(const char *name, void *arg)
3148 {
3149 zfs_sb_t *zsb = NULL;
3150 char *dsname;
3151 char *snapname;
3152 char *fullname;
3153 char *ptr;
3154 int error;
3155
3156 if (arg) {
3157 dsname = strdup(name);
3158 snapname = strdup(arg);
3159 } else {
3160 ptr = strchr(name, '@');
3161 if (ptr) {
3162 dsname = strdup(name);
3163 dsname[ptr - name] = '\0';
3164 snapname = strdup(ptr + 1);
3165 } else {
3166 return (0);
3167 }
3168 }
3169
3170 fullname = kmem_asprintf("%s@%s", dsname, snapname);
3171
3172 error = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
3173 if (error == 0) {
3174 error = zfsctl_unmount_snapshot(zsb, fullname, MNT_FORCE);
3175 zfs_sb_rele(zsb, FTAG);
3176
3177 /* Allow ENOENT for consistency with upstream */
3178 if (error == ENOENT)
3179 error = 0;
3180 }
3181
3182 strfree(dsname);
3183 strfree(snapname);
3184 strfree(fullname);
3185
3186 return (error);
3187 }
3188
3189 /*
3190 * inputs:
3191 * zc_name name of filesystem, snaps must be under it
3192 * zc_nvlist_src[_size] full names of snapshots to destroy
3193 * zc_defer_destroy mark for deferred destroy
3194 *
3195 * outputs:
3196 * zc_name on failure, name of failed snapshot
3197 */
3198 static int
3199 zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc)
3200 {
3201 int err, len;
3202 nvlist_t *nvl;
3203 nvpair_t *pair;
3204
3205 if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3206 zc->zc_iflags, &nvl)) != 0)
3207 return (err);
3208
3209 len = strlen(zc->zc_name);
3210 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
3211 pair = nvlist_next_nvpair(nvl, pair)) {
3212 const char *name = nvpair_name(pair);
3213 /*
3214 * The snap name must be underneath the zc_name. This ensures
3215 * that our permission checks were legitimate.
3216 */
3217 if (strncmp(zc->zc_name, name, len) != 0 ||
3218 (name[len] != '@' && name[len] != '/')) {
3219 nvlist_free(nvl);
3220 return (EINVAL);
3221 }
3222
3223 (void) zfs_unmount_snap(name, NULL);
3224 (void) zvol_remove_minor(name);
3225 }
3226
3227 err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy,
3228 zc->zc_name);
3229 nvlist_free(nvl);
3230 return (err);
3231 }
3232
3233 /*
3234 * inputs:
3235 * zc_name name of dataset to destroy
3236 * zc_objset_type type of objset
3237 * zc_defer_destroy mark for deferred destroy
3238 *
3239 * outputs: none
3240 */
3241 static int
3242 zfs_ioc_destroy(zfs_cmd_t *zc)
3243 {
3244 int err;
3245 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3246 err = zfs_unmount_snap(zc->zc_name, NULL);
3247 if (err)
3248 return (err);
3249 }
3250
3251 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3252 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3253 (void) zvol_remove_minor(zc->zc_name);
3254 return (err);
3255 }
3256
3257 /*
3258 * inputs:
3259 * zc_name name of dataset to rollback (to most recent snapshot)
3260 *
3261 * outputs: none
3262 */
3263 static int
3264 zfs_ioc_rollback(zfs_cmd_t *zc)
3265 {
3266 dsl_dataset_t *ds, *clone;
3267 int error;
3268 zfs_sb_t *zsb;
3269 char *clone_name;
3270
3271 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3272 if (error)
3273 return (error);
3274
3275 /* must not be a snapshot */
3276 if (dsl_dataset_is_snapshot(ds)) {
3277 dsl_dataset_rele(ds, FTAG);
3278 return (EINVAL);
3279 }
3280
3281 /* must have a most recent snapshot */
3282 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3283 dsl_dataset_rele(ds, FTAG);
3284 return (EINVAL);
3285 }
3286
3287 /*
3288 * Create clone of most recent snapshot.
3289 */
3290 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3291 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3292 if (error)
3293 goto out;
3294
3295 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3296 if (error)
3297 goto out;
3298
3299 /*
3300 * Do clone swap.
3301 */
3302 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
3303 error = zfs_suspend_fs(zsb);
3304 if (error == 0) {
3305 int resume_err;
3306
3307 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3308 error = dsl_dataset_clone_swap(clone, ds,
3309 B_TRUE);
3310 dsl_dataset_disown(ds, FTAG);
3311 ds = NULL;
3312 } else {
3313 error = EBUSY;
3314 }
3315 resume_err = zfs_resume_fs(zsb, zc->zc_name);
3316 error = error ? error : resume_err;
3317 }
3318 deactivate_super(zsb->z_sb);
3319 } else {
3320 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3321 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3322 dsl_dataset_disown(ds, FTAG);
3323 ds = NULL;
3324 } else {
3325 error = EBUSY;
3326 }
3327 }
3328
3329 /*
3330 * Destroy clone (which also closes it).
3331 */
3332 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3333
3334 out:
3335 strfree(clone_name);
3336 if (ds)
3337 dsl_dataset_rele(ds, FTAG);
3338 return (error);
3339 }
3340
3341 /*
3342 * inputs:
3343 * zc_name old name of dataset
3344 * zc_value new name of dataset
3345 * zc_cookie recursive flag (only valid for snapshots)
3346 *
3347 * outputs: none
3348 */
3349 static int
3350 zfs_ioc_rename(zfs_cmd_t *zc)
3351 {
3352 boolean_t recursive = zc->zc_cookie & 1;
3353 int err;
3354
3355 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3356 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3357 strchr(zc->zc_value, '%'))
3358 return (EINVAL);
3359
3360 /*
3361 * Unmount snapshot unless we're doing a recursive rename,
3362 * in which case the dataset code figures out which snapshots
3363 * to unmount.
3364 */
3365 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3366 zc->zc_objset_type == DMU_OST_ZFS) {
3367 err = zfs_unmount_snap(zc->zc_name, NULL);
3368 if (err)
3369 return (err);
3370 }
3371
3372 err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3373 if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3374 (void) zvol_remove_minor(zc->zc_name);
3375 (void) zvol_create_minor(zc->zc_value);
3376 }
3377
3378 return (err);
3379 }
3380
3381 static int
3382 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3383 {
3384 const char *propname = nvpair_name(pair);
3385 boolean_t issnap = (strchr(dsname, '@') != NULL);
3386 zfs_prop_t prop = zfs_name_to_prop(propname);
3387 uint64_t intval;
3388 int err;
3389
3390 if (prop == ZPROP_INVAL) {
3391 if (zfs_prop_user(propname)) {
3392 if ((err = zfs_secpolicy_write_perms(dsname,
3393 ZFS_DELEG_PERM_USERPROP, cr)))
3394 return (err);
3395 return (0);
3396 }
3397
3398 if (!issnap && zfs_prop_userquota(propname)) {
3399 const char *perm = NULL;
3400 const char *uq_prefix =
3401 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3402 const char *gq_prefix =
3403 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3404
3405 if (strncmp(propname, uq_prefix,
3406 strlen(uq_prefix)) == 0) {
3407 perm = ZFS_DELEG_PERM_USERQUOTA;
3408 } else if (strncmp(propname, gq_prefix,
3409 strlen(gq_prefix)) == 0) {
3410 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3411 } else {
3412 /* USERUSED and GROUPUSED are read-only */
3413 return (EINVAL);
3414 }
3415
3416 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3417 return (err);
3418 return (0);
3419 }
3420
3421 return (EINVAL);
3422 }
3423
3424 if (issnap)
3425 return (EINVAL);
3426
3427 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3428 /*
3429 * dsl_prop_get_all_impl() returns properties in this
3430 * format.
3431 */
3432 nvlist_t *attrs;
3433 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3434 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3435 &pair) == 0);
3436 }
3437
3438 /*
3439 * Check that this value is valid for this pool version
3440 */
3441 switch (prop) {
3442 case ZFS_PROP_COMPRESSION:
3443 /*
3444 * If the user specified gzip compression, make sure
3445 * the SPA supports it. We ignore any errors here since
3446 * we'll catch them later.
3447 */
3448 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3449 nvpair_value_uint64(pair, &intval) == 0) {
3450 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3451 intval <= ZIO_COMPRESS_GZIP_9 &&
3452 zfs_earlier_version(dsname,
3453 SPA_VERSION_GZIP_COMPRESSION)) {
3454 return (ENOTSUP);
3455 }
3456
3457 if (intval == ZIO_COMPRESS_ZLE &&
3458 zfs_earlier_version(dsname,
3459 SPA_VERSION_ZLE_COMPRESSION))
3460 return (ENOTSUP);
3461
3462 if (intval == ZIO_COMPRESS_LZ4) {
3463 zfeature_info_t *feature =
3464 &spa_feature_table[
3465 SPA_FEATURE_LZ4_COMPRESS];
3466 spa_t *spa;
3467
3468 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3469 return (err);
3470
3471 if (!spa_feature_is_enabled(spa, feature)) {
3472 spa_close(spa, FTAG);
3473 return (ENOTSUP);
3474 }
3475 spa_close(spa, FTAG);
3476 }
3477
3478 /*
3479 * If this is a bootable dataset then
3480 * verify that the compression algorithm
3481 * is supported for booting. We must return
3482 * something other than ENOTSUP since it
3483 * implies a downrev pool version.
3484 */
3485 if (zfs_is_bootfs(dsname) &&
3486 !BOOTFS_COMPRESS_VALID(intval)) {
3487 return (ERANGE);
3488 }
3489 }
3490 break;
3491
3492 case ZFS_PROP_COPIES:
3493 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3494 return (ENOTSUP);
3495 break;
3496
3497 case ZFS_PROP_DEDUP:
3498 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3499 return (ENOTSUP);
3500 break;
3501
3502 case ZFS_PROP_SHARESMB:
3503 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3504 return (ENOTSUP);
3505 break;
3506
3507 case ZFS_PROP_ACLINHERIT:
3508 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3509 nvpair_value_uint64(pair, &intval) == 0) {
3510 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3511 zfs_earlier_version(dsname,
3512 SPA_VERSION_PASSTHROUGH_X))
3513 return (ENOTSUP);
3514 }
3515 break;
3516 default:
3517 break;
3518 }
3519
3520 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3521 }
3522
3523 /*
3524 * Activates a feature on a pool in response to a property setting. This
3525 * creates a new sync task which modifies the pool to reflect the feature
3526 * as being active.
3527 */
3528 static int
3529 zfs_prop_activate_feature(dsl_pool_t *dp, zfeature_info_t *feature)
3530 {
3531 int err;
3532
3533 /* EBUSY here indicates that the feature is already active */
3534 err = dsl_sync_task_do(dp, zfs_prop_activate_feature_check,
3535 zfs_prop_activate_feature_sync, dp->dp_spa, feature, 2);
3536
3537 if (err != 0 && err != EBUSY)
3538 return (err);
3539 else
3540 return (0);
3541 }
3542
3543 /*
3544 * Checks for a race condition to make sure we don't increment a feature flag
3545 * multiple times.
3546 */
3547 /*ARGSUSED*/
3548 static int
3549 zfs_prop_activate_feature_check(void *arg1, void *arg2, dmu_tx_t *tx)
3550 {
3551 spa_t *spa = arg1;
3552 zfeature_info_t *feature = arg2;
3553
3554 if (!spa_feature_is_active(spa, feature))
3555 return (0);
3556 else
3557 return (EBUSY);
3558 }
3559
3560 /*
3561 * The callback invoked on feature activation in the sync task caused by
3562 * zfs_prop_activate_feature.
3563 */
3564 static void
3565 zfs_prop_activate_feature_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3566 {
3567 spa_t *spa = arg1;
3568 zfeature_info_t *feature = arg2;
3569
3570 spa_feature_incr(spa, feature, tx);
3571 }
3572
3573 /*
3574 * Removes properties from the given props list that fail permission checks
3575 * needed to clear them and to restore them in case of a receive error. For each
3576 * property, make sure we have both set and inherit permissions.
3577 *
3578 * Returns the first error encountered if any permission checks fail. If the
3579 * caller provides a non-NULL errlist, it also gives the complete list of names
3580 * of all the properties that failed a permission check along with the
3581 * corresponding error numbers. The caller is responsible for freeing the
3582 * returned errlist.
3583 *
3584 * If every property checks out successfully, zero is returned and the list
3585 * pointed at by errlist is NULL.
3586 */
3587 static int
3588 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3589 {
3590 zfs_cmd_t *zc;
3591 nvpair_t *pair, *next_pair;
3592 nvlist_t *errors;
3593 int err, rv = 0;
3594
3595 if (props == NULL)
3596 return (0);
3597
3598 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3599
3600 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
3601 (void) strcpy(zc->zc_name, dataset);
3602 pair = nvlist_next_nvpair(props, NULL);
3603 while (pair != NULL) {
3604 next_pair = nvlist_next_nvpair(props, pair);
3605
3606 (void) strcpy(zc->zc_value, nvpair_name(pair));
3607 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3608 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3609 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3610 VERIFY(nvlist_add_int32(errors,
3611 zc->zc_value, err) == 0);
3612 }
3613 pair = next_pair;
3614 }
3615 kmem_free(zc, sizeof (zfs_cmd_t));
3616
3617 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3618 nvlist_free(errors);
3619 errors = NULL;
3620 } else {
3621 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3622 }
3623
3624 if (errlist == NULL)
3625 nvlist_free(errors);
3626 else
3627 *errlist = errors;
3628
3629 return (rv);
3630 }
3631
3632 static boolean_t
3633 propval_equals(nvpair_t *p1, nvpair_t *p2)
3634 {
3635 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3636 /* dsl_prop_get_all_impl() format */
3637 nvlist_t *attrs;
3638 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3639 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3640 &p1) == 0);
3641 }
3642
3643 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3644 nvlist_t *attrs;
3645 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3646 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3647 &p2) == 0);
3648 }
3649
3650 if (nvpair_type(p1) != nvpair_type(p2))
3651 return (B_FALSE);
3652
3653 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3654 char *valstr1, *valstr2;
3655
3656 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3657 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3658 return (strcmp(valstr1, valstr2) == 0);
3659 } else {
3660 uint64_t intval1, intval2;
3661
3662 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3663 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3664 return (intval1 == intval2);
3665 }
3666 }
3667
3668 /*
3669 * Remove properties from props if they are not going to change (as determined
3670 * by comparison with origprops). Remove them from origprops as well, since we
3671 * do not need to clear or restore properties that won't change.
3672 */
3673 static void
3674 props_reduce(nvlist_t *props, nvlist_t *origprops)
3675 {
3676 nvpair_t *pair, *next_pair;
3677
3678 if (origprops == NULL)
3679 return; /* all props need to be received */
3680
3681 pair = nvlist_next_nvpair(props, NULL);
3682 while (pair != NULL) {
3683 const char *propname = nvpair_name(pair);
3684 nvpair_t *match;
3685
3686 next_pair = nvlist_next_nvpair(props, pair);
3687
3688 if ((nvlist_lookup_nvpair(origprops, propname,
3689 &match) != 0) || !propval_equals(pair, match))
3690 goto next; /* need to set received value */
3691
3692 /* don't clear the existing received value */
3693 (void) nvlist_remove_nvpair(origprops, match);
3694 /* don't bother receiving the property */
3695 (void) nvlist_remove_nvpair(props, pair);
3696 next:
3697 pair = next_pair;
3698 }
3699 }
3700
3701 #ifdef DEBUG
3702 static boolean_t zfs_ioc_recv_inject_err;
3703 #endif
3704
3705 /*
3706 * inputs:
3707 * zc_name name of containing filesystem
3708 * zc_nvlist_src{_size} nvlist of properties to apply
3709 * zc_value name of snapshot to create
3710 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3711 * zc_cookie file descriptor to recv from
3712 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3713 * zc_guid force flag
3714 * zc_cleanup_fd cleanup-on-exit file descriptor
3715 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3716 *
3717 * outputs:
3718 * zc_cookie number of bytes read
3719 * zc_nvlist_dst{_size} error for each unapplied received property
3720 * zc_obj zprop_errflags_t
3721 * zc_action_handle handle for this guid/ds mapping
3722 */
3723 static int
3724 zfs_ioc_recv(zfs_cmd_t *zc)
3725 {
3726 file_t *fp;
3727 objset_t *os;
3728 dmu_recv_cookie_t drc;
3729 boolean_t force = (boolean_t)zc->zc_guid;
3730 int fd;
3731 int error = 0;
3732 int props_error = 0;
3733 nvlist_t *errors;
3734 offset_t off;
3735 nvlist_t *props = NULL; /* sent properties */
3736 nvlist_t *origprops = NULL; /* existing properties */
3737 objset_t *origin = NULL;
3738 char *tosnap;
3739 char tofs[ZFS_MAXNAMELEN];
3740 boolean_t first_recvd_props = B_FALSE;
3741
3742 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3743 strchr(zc->zc_value, '@') == NULL ||
3744 strchr(zc->zc_value, '%'))
3745 return (EINVAL);
3746
3747 (void) strcpy(tofs, zc->zc_value);
3748 tosnap = strchr(tofs, '@');
3749 *tosnap++ = '\0';
3750
3751 if (zc->zc_nvlist_src != 0 &&
3752 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3753 zc->zc_iflags, &props)) != 0)
3754 return (error);
3755
3756 fd = zc->zc_cookie;
3757 fp = getf(fd);
3758 if (fp == NULL) {
3759 nvlist_free(props);
3760 return (EBADF);
3761 }
3762
3763 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3764
3765 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3766 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3767 !dsl_prop_get_hasrecvd(os)) {
3768 first_recvd_props = B_TRUE;
3769 }
3770
3771 /*
3772 * If new received properties are supplied, they are to
3773 * completely replace the existing received properties, so stash
3774 * away the existing ones.
3775 */
3776 if (dsl_prop_get_received(os, &origprops) == 0) {
3777 nvlist_t *errlist = NULL;
3778 /*
3779 * Don't bother writing a property if its value won't
3780 * change (and avoid the unnecessary security checks).
3781 *
3782 * The first receive after SPA_VERSION_RECVD_PROPS is a
3783 * special case where we blow away all local properties
3784 * regardless.
3785 */
3786 if (!first_recvd_props)
3787 props_reduce(props, origprops);
3788 if (zfs_check_clearable(tofs, origprops,
3789 &errlist) != 0)
3790 (void) nvlist_merge(errors, errlist, 0);
3791 nvlist_free(errlist);
3792 }
3793
3794 dmu_objset_rele(os, FTAG);
3795 }
3796
3797 if (zc->zc_string[0]) {
3798 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3799 if (error)
3800 goto out;
3801 }
3802
3803 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3804 &zc->zc_begin_record, force, origin, &drc);
3805 if (origin)
3806 dmu_objset_rele(origin, FTAG);
3807 if (error)
3808 goto out;
3809
3810 /*
3811 * Set properties before we receive the stream so that they are applied
3812 * to the new data. Note that we must call dmu_recv_stream() if
3813 * dmu_recv_begin() succeeds.
3814 */
3815 if (props) {
3816 nvlist_t *errlist;
3817
3818 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3819 if (drc.drc_newfs) {
3820 if (spa_version(os->os_spa) >=
3821 SPA_VERSION_RECVD_PROPS)
3822 first_recvd_props = B_TRUE;
3823 } else if (origprops != NULL) {
3824 if (clear_received_props(os, tofs, origprops,
3825 first_recvd_props ? NULL : props) != 0)
3826 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3827 } else {
3828 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3829 }
3830 dsl_prop_set_hasrecvd(os);
3831 } else if (!drc.drc_newfs) {
3832 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3833 }
3834
3835 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3836 props, &errlist);
3837 (void) nvlist_merge(errors, errlist, 0);
3838 nvlist_free(errlist);
3839 }
3840
3841 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3842 /*
3843 * Caller made zc->zc_nvlist_dst less than the minimum expected
3844 * size or supplied an invalid address.
3845 */
3846 props_error = EINVAL;
3847 }
3848
3849 off = fp->f_offset;
3850 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3851 &zc->zc_action_handle);
3852
3853 if (error == 0) {
3854 zfs_sb_t *zsb = NULL;
3855
3856 if (get_zfs_sb(tofs, &zsb) == 0) {
3857 /* online recv */
3858 int end_err;
3859
3860 error = zfs_suspend_fs(zsb);
3861 /*
3862 * If the suspend fails, then the recv_end will
3863 * likely also fail, and clean up after itself.
3864 */
3865 end_err = dmu_recv_end(&drc);
3866 if (error == 0)
3867 error = zfs_resume_fs(zsb, tofs);
3868 error = error ? error : end_err;
3869 deactivate_super(zsb->z_sb);
3870 } else {
3871 error = dmu_recv_end(&drc);
3872 }
3873 }
3874
3875 zc->zc_cookie = off - fp->f_offset;
3876 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3877 fp->f_offset = off;
3878
3879 #ifdef DEBUG
3880 if (zfs_ioc_recv_inject_err) {
3881 zfs_ioc_recv_inject_err = B_FALSE;
3882 error = 1;
3883 }
3884 #endif
3885 /*
3886 * On error, restore the original props.
3887 */
3888 if (error && props) {
3889 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3890 if (clear_received_props(os, tofs, props, NULL) != 0) {
3891 /*
3892 * We failed to clear the received properties.
3893 * Since we may have left a $recvd value on the
3894 * system, we can't clear the $hasrecvd flag.
3895 */
3896 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3897 } else if (first_recvd_props) {
3898 dsl_prop_unset_hasrecvd(os);
3899 }
3900 dmu_objset_rele(os, FTAG);
3901 } else if (!drc.drc_newfs) {
3902 /* We failed to clear the received properties. */
3903 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3904 }
3905
3906 if (origprops == NULL && !drc.drc_newfs) {
3907 /* We failed to stash the original properties. */
3908 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3909 }
3910
3911 /*
3912 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3913 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3914 * explictly if we're restoring local properties cleared in the
3915 * first new-style receive.
3916 */
3917 if (origprops != NULL &&
3918 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3919 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3920 origprops, NULL) != 0) {
3921 /*
3922 * We stashed the original properties but failed to
3923 * restore them.
3924 */
3925 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3926 }
3927 }
3928 out:
3929 nvlist_free(props);
3930 nvlist_free(origprops);
3931 nvlist_free(errors);
3932 releasef(fd);
3933
3934 if (error == 0)
3935 error = props_error;
3936
3937 return (error);
3938 }
3939
3940 /*
3941 * inputs:
3942 * zc_name name of snapshot to send
3943 * zc_cookie file descriptor to send stream to
3944 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3945 * zc_sendobj objsetid of snapshot to send
3946 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3947 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
3948 * output size in zc_objset_type.
3949 *
3950 * outputs: none
3951 */
3952 static int
3953 zfs_ioc_send(zfs_cmd_t *zc)
3954 {
3955 objset_t *fromsnap = NULL;
3956 objset_t *tosnap;
3957 int error;
3958 offset_t off;
3959 dsl_dataset_t *ds;
3960 dsl_dataset_t *dsfrom = NULL;
3961 spa_t *spa;
3962 dsl_pool_t *dp;
3963 boolean_t estimate = (zc->zc_guid != 0);
3964
3965 error = spa_open(zc->zc_name, &spa, FTAG);
3966 if (error)
3967 return (error);
3968
3969 dp = spa_get_dsl(spa);
3970 rw_enter(&dp->dp_config_rwlock, RW_READER);
3971 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3972 rw_exit(&dp->dp_config_rwlock);
3973 if (error) {
3974 spa_close(spa, FTAG);
3975 return (error);
3976 }
3977
3978 error = dmu_objset_from_ds(ds, &tosnap);
3979 if (error) {
3980 dsl_dataset_rele(ds, FTAG);
3981 spa_close(spa, FTAG);
3982 return (error);
3983 }
3984
3985 if (zc->zc_fromobj != 0) {
3986 rw_enter(&dp->dp_config_rwlock, RW_READER);
3987 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3988 rw_exit(&dp->dp_config_rwlock);
3989 spa_close(spa, FTAG);
3990 if (error) {
3991 dsl_dataset_rele(ds, FTAG);
3992 return (error);
3993 }
3994 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3995 if (error) {
3996 dsl_dataset_rele(dsfrom, FTAG);
3997 dsl_dataset_rele(ds, FTAG);
3998 return (error);
3999 }
4000 } else {
4001 spa_close(spa, FTAG);
4002 }
4003
4004 if (estimate) {
4005 error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj,
4006 &zc->zc_objset_type);
4007 } else {
4008 file_t *fp = getf(zc->zc_cookie);
4009 if (fp == NULL) {
4010 dsl_dataset_rele(ds, FTAG);
4011 if (dsfrom)
4012 dsl_dataset_rele(dsfrom, FTAG);
4013 return (EBADF);
4014 }
4015
4016 off = fp->f_offset;
4017 error = dmu_send(tosnap, fromsnap, zc->zc_obj,
4018 zc->zc_cookie, fp->f_vnode, &off);
4019
4020 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4021 fp->f_offset = off;
4022 releasef(zc->zc_cookie);
4023 }
4024 if (dsfrom)
4025 dsl_dataset_rele(dsfrom, FTAG);
4026 dsl_dataset_rele(ds, FTAG);
4027 return (error);
4028 }
4029
4030 /*
4031 * inputs:
4032 * zc_name name of snapshot on which to report progress
4033 * zc_cookie file descriptor of send stream
4034 *
4035 * outputs:
4036 * zc_cookie number of bytes written in send stream thus far
4037 */
4038 static int
4039 zfs_ioc_send_progress(zfs_cmd_t *zc)
4040 {
4041 dsl_dataset_t *ds;
4042 dmu_sendarg_t *dsp = NULL;
4043 int error;
4044
4045 if ((error = dsl_dataset_hold(zc->zc_name, FTAG, &ds)) != 0)
4046 return (error);
4047
4048 mutex_enter(&ds->ds_sendstream_lock);
4049
4050 /*
4051 * Iterate over all the send streams currently active on this dataset.
4052 * If there's one which matches the specified file descriptor _and_ the
4053 * stream was started by the current process, return the progress of
4054 * that stream.
4055 */
4056
4057 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4058 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4059 if (dsp->dsa_outfd == zc->zc_cookie &&
4060 dsp->dsa_proc->group_leader == curproc->group_leader)
4061 break;
4062 }
4063
4064 if (dsp != NULL)
4065 zc->zc_cookie = *(dsp->dsa_off);
4066 else
4067 error = ENOENT;
4068
4069 mutex_exit(&ds->ds_sendstream_lock);
4070 dsl_dataset_rele(ds, FTAG);
4071 return (error);
4072 }
4073
4074 static int
4075 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4076 {
4077 int id, error;
4078
4079 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4080 &zc->zc_inject_record);
4081
4082 if (error == 0)
4083 zc->zc_guid = (uint64_t)id;
4084
4085 return (error);
4086 }
4087
4088 static int
4089 zfs_ioc_clear_fault(zfs_cmd_t *zc)
4090 {
4091 return (zio_clear_fault((int)zc->zc_guid));
4092 }
4093
4094 static int
4095 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
4096 {
4097 int id = (int)zc->zc_guid;
4098 int error;
4099
4100 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
4101 &zc->zc_inject_record);
4102
4103 zc->zc_guid = id;
4104
4105 return (error);
4106 }
4107
4108 static int
4109 zfs_ioc_error_log(zfs_cmd_t *zc)
4110 {
4111 spa_t *spa;
4112 int error;
4113 size_t count = (size_t)zc->zc_nvlist_dst_size;
4114
4115 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4116 return (error);
4117
4118 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
4119 &count);
4120 if (error == 0)
4121 zc->zc_nvlist_dst_size = count;
4122 else
4123 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
4124
4125 spa_close(spa, FTAG);
4126
4127 return (error);
4128 }
4129
4130 static int
4131 zfs_ioc_clear(zfs_cmd_t *zc)
4132 {
4133 spa_t *spa;
4134 vdev_t *vd;
4135 int error;
4136
4137 /*
4138 * On zpool clear we also fix up missing slogs
4139 */
4140 mutex_enter(&spa_namespace_lock);
4141 spa = spa_lookup(zc->zc_name);
4142 if (spa == NULL) {
4143 mutex_exit(&spa_namespace_lock);
4144 return (EIO);
4145 }
4146 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
4147 /* we need to let spa_open/spa_load clear the chains */
4148 spa_set_log_state(spa, SPA_LOG_CLEAR);
4149 }
4150 spa->spa_last_open_failed = 0;
4151 mutex_exit(&spa_namespace_lock);
4152
4153 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
4154 error = spa_open(zc->zc_name, &spa, FTAG);
4155 } else {
4156 nvlist_t *policy;
4157 nvlist_t *config = NULL;
4158
4159 if (zc->zc_nvlist_src == 0)
4160 return (EINVAL);
4161
4162 if ((error = get_nvlist(zc->zc_nvlist_src,
4163 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
4164 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
4165 policy, &config);
4166 if (config != NULL) {
4167 int err;
4168
4169 if ((err = put_nvlist(zc, config)) != 0)
4170 error = err;
4171 nvlist_free(config);
4172 }
4173 nvlist_free(policy);
4174 }
4175 }
4176
4177 if (error)
4178 return (error);
4179
4180 spa_vdev_state_enter(spa, SCL_NONE);
4181
4182 if (zc->zc_guid == 0) {
4183 vd = NULL;
4184 } else {
4185 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4186 if (vd == NULL) {
4187 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4188 spa_close(spa, FTAG);
4189 return (ENODEV);
4190 }
4191 }
4192
4193 vdev_clear(spa, vd);
4194
4195 (void) spa_vdev_state_exit(spa, NULL, 0);
4196
4197 /*
4198 * Resume any suspended I/Os.
4199 */
4200 if (zio_resume(spa) != 0)
4201 error = EIO;
4202
4203 spa_close(spa, FTAG);
4204
4205 return (error);
4206 }
4207
4208 static int
4209 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4210 {
4211 spa_t *spa;
4212 int error;
4213
4214 error = spa_open(zc->zc_name, &spa, FTAG);
4215 if (error)
4216 return (error);
4217
4218 spa_vdev_state_enter(spa, SCL_NONE);
4219
4220 /*
4221 * If a resilver is already in progress then set the
4222 * spa_scrub_reopen flag to B_TRUE so that we don't restart
4223 * the scan as a side effect of the reopen. Otherwise, let
4224 * vdev_open() decided if a resilver is required.
4225 */
4226 spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool);
4227 vdev_reopen(spa->spa_root_vdev);
4228 spa->spa_scrub_reopen = B_FALSE;
4229
4230 (void) spa_vdev_state_exit(spa, NULL, 0);
4231 spa_close(spa, FTAG);
4232 return (0);
4233 }
4234 /*
4235 * inputs:
4236 * zc_name name of filesystem
4237 * zc_value name of origin snapshot
4238 *
4239 * outputs:
4240 * zc_string name of conflicting snapshot, if there is one
4241 */
4242 static int
4243 zfs_ioc_promote(zfs_cmd_t *zc)
4244 {
4245 char *cp;
4246
4247 /*
4248 * We don't need to unmount *all* the origin fs's snapshots, but
4249 * it's easier.
4250 */
4251 cp = strchr(zc->zc_value, '@');
4252 if (cp)
4253 *cp = '\0';
4254 (void) dmu_objset_find(zc->zc_value,
4255 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4256 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4257 }
4258
4259 /*
4260 * Retrieve a single {user|group}{used|quota}@... property.
4261 *
4262 * inputs:
4263 * zc_name name of filesystem
4264 * zc_objset_type zfs_userquota_prop_t
4265 * zc_value domain name (eg. "S-1-234-567-89")
4266 * zc_guid RID/UID/GID
4267 *
4268 * outputs:
4269 * zc_cookie property value
4270 */
4271 static int
4272 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4273 {
4274 zfs_sb_t *zsb;
4275 int error;
4276
4277 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4278 return (EINVAL);
4279
4280 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4281 if (error)
4282 return (error);
4283
4284 error = zfs_userspace_one(zsb,
4285 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4286 zfs_sb_rele(zsb, FTAG);
4287
4288 return (error);
4289 }
4290
4291 /*
4292 * inputs:
4293 * zc_name name of filesystem
4294 * zc_cookie zap cursor
4295 * zc_objset_type zfs_userquota_prop_t
4296 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4297 *
4298 * outputs:
4299 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4300 * zc_cookie zap cursor
4301 */
4302 static int
4303 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4304 {
4305 zfs_sb_t *zsb;
4306 int bufsize = zc->zc_nvlist_dst_size;
4307 int error;
4308 void *buf;
4309
4310 if (bufsize <= 0)
4311 return (ENOMEM);
4312
4313 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4314 if (error)
4315 return (error);
4316
4317 buf = vmem_alloc(bufsize, KM_SLEEP);
4318
4319 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4320 buf, &zc->zc_nvlist_dst_size);
4321
4322 if (error == 0) {
4323 error = xcopyout(buf,
4324 (void *)(uintptr_t)zc->zc_nvlist_dst,
4325 zc->zc_nvlist_dst_size);
4326 }
4327 vmem_free(buf, bufsize);
4328 zfs_sb_rele(zsb, FTAG);
4329
4330 return (error);
4331 }
4332
4333 /*
4334 * inputs:
4335 * zc_name name of filesystem
4336 *
4337 * outputs:
4338 * none
4339 */
4340 static int
4341 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4342 {
4343 objset_t *os;
4344 int error = 0;
4345 zfs_sb_t *zsb;
4346
4347 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4348 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4349 /*
4350 * If userused is not enabled, it may be because the
4351 * objset needs to be closed & reopened (to grow the
4352 * objset_phys_t). Suspend/resume the fs will do that.
4353 */
4354 error = zfs_suspend_fs(zsb);
4355 if (error == 0)
4356 error = zfs_resume_fs(zsb, zc->zc_name);
4357 }
4358 if (error == 0)
4359 error = dmu_objset_userspace_upgrade(zsb->z_os);
4360 deactivate_super(zsb->z_sb);
4361 } else {
4362 /* XXX kind of reading contents without owning */
4363 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4364 if (error)
4365 return (error);
4366
4367 error = dmu_objset_userspace_upgrade(os);
4368 dmu_objset_rele(os, FTAG);
4369 }
4370
4371 return (error);
4372 }
4373
4374 static int
4375 zfs_ioc_share(zfs_cmd_t *zc)
4376 {
4377 return (ENOSYS);
4378 }
4379
4380 ace_t full_access[] = {
4381 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4382 };
4383
4384 /*
4385 * inputs:
4386 * zc_name name of containing filesystem
4387 * zc_obj object # beyond which we want next in-use object #
4388 *
4389 * outputs:
4390 * zc_obj next in-use object #
4391 */
4392 static int
4393 zfs_ioc_next_obj(zfs_cmd_t *zc)
4394 {
4395 objset_t *os = NULL;
4396 int error;
4397
4398 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4399 if (error)
4400 return (error);
4401
4402 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4403 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4404
4405 dmu_objset_rele(os, FTAG);
4406 return (error);
4407 }
4408
4409 /*
4410 * inputs:
4411 * zc_name name of filesystem
4412 * zc_value prefix name for snapshot
4413 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4414 *
4415 * outputs:
4416 */
4417 static int
4418 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4419 {
4420 char *snap_name;
4421 int error;
4422
4423 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4424 (u_longlong_t)ddi_get_lbolt64());
4425
4426 if (strlen(snap_name) >= MAXNAMELEN) {
4427 strfree(snap_name);
4428 return (E2BIG);
4429 }
4430
4431 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4432 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4433 if (error != 0) {
4434 strfree(snap_name);
4435 return (error);
4436 }
4437
4438 (void) strcpy(zc->zc_value, snap_name);
4439 strfree(snap_name);
4440 return (0);
4441 }
4442
4443 /*
4444 * inputs:
4445 * zc_name name of "to" snapshot
4446 * zc_value name of "from" snapshot
4447 * zc_cookie file descriptor to write diff data on
4448 *
4449 * outputs:
4450 * dmu_diff_record_t's to the file descriptor
4451 */
4452 static int
4453 zfs_ioc_diff(zfs_cmd_t *zc)
4454 {
4455 objset_t *fromsnap;
4456 objset_t *tosnap;
4457 file_t *fp;
4458 offset_t off;
4459 int error;
4460
4461 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4462 if (error)
4463 return (error);
4464
4465 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4466 if (error) {
4467 dmu_objset_rele(tosnap, FTAG);
4468 return (error);
4469 }
4470
4471 fp = getf(zc->zc_cookie);
4472 if (fp == NULL) {
4473 dmu_objset_rele(fromsnap, FTAG);
4474 dmu_objset_rele(tosnap, FTAG);
4475 return (EBADF);
4476 }
4477
4478 off = fp->f_offset;
4479
4480 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4481
4482 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4483 fp->f_offset = off;
4484 releasef(zc->zc_cookie);
4485
4486 dmu_objset_rele(fromsnap, FTAG);
4487 dmu_objset_rele(tosnap, FTAG);
4488 return (error);
4489 }
4490
4491 /*
4492 * Remove all ACL files in shares dir
4493 */
4494 #ifdef HAVE_SMB_SHARE
4495 static int
4496 zfs_smb_acl_purge(znode_t *dzp)
4497 {
4498 zap_cursor_t zc;
4499 zap_attribute_t zap;
4500 zfs_sb_t *zsb = ZTOZSB(dzp);
4501 int error;
4502
4503 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4504 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4505 zap_cursor_advance(&zc)) {
4506 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4507 NULL, 0)) != 0)
4508 break;
4509 }
4510 zap_cursor_fini(&zc);
4511 return (error);
4512 }
4513 #endif /* HAVE_SMB_SHARE */
4514
4515 static int
4516 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4517 {
4518 #ifdef HAVE_SMB_SHARE
4519 vnode_t *vp;
4520 znode_t *dzp;
4521 vnode_t *resourcevp = NULL;
4522 znode_t *sharedir;
4523 zfs_sb_t *zsb;
4524 nvlist_t *nvlist;
4525 char *src, *target;
4526 vattr_t vattr;
4527 vsecattr_t vsec;
4528 int error = 0;
4529
4530 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4531 NO_FOLLOW, NULL, &vp)) != 0)
4532 return (error);
4533
4534 /* Now make sure mntpnt and dataset are ZFS */
4535
4536 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4537 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4538 zc->zc_name) != 0)) {
4539 VN_RELE(vp);
4540 return (EINVAL);
4541 }
4542
4543 dzp = VTOZ(vp);
4544 zsb = ZTOZSB(dzp);
4545 ZFS_ENTER(zsb);
4546
4547 /*
4548 * Create share dir if its missing.
4549 */
4550 mutex_enter(&zsb->z_lock);
4551 if (zsb->z_shares_dir == 0) {
4552 dmu_tx_t *tx;
4553
4554 tx = dmu_tx_create(zsb->z_os);
4555 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4556 ZFS_SHARES_DIR);
4557 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4558 error = dmu_tx_assign(tx, TXG_WAIT);
4559 if (error) {
4560 dmu_tx_abort(tx);
4561 } else {
4562 error = zfs_create_share_dir(zsb, tx);
4563 dmu_tx_commit(tx);
4564 }
4565 if (error) {
4566 mutex_exit(&zsb->z_lock);
4567 VN_RELE(vp);
4568 ZFS_EXIT(zsb);
4569 return (error);
4570 }
4571 }
4572 mutex_exit(&zsb->z_lock);
4573
4574 ASSERT(zsb->z_shares_dir);
4575 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
4576 VN_RELE(vp);
4577 ZFS_EXIT(zsb);
4578 return (error);
4579 }
4580
4581 switch (zc->zc_cookie) {
4582 case ZFS_SMB_ACL_ADD:
4583 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4584 vattr.va_mode = S_IFREG|0777;
4585 vattr.va_uid = 0;
4586 vattr.va_gid = 0;
4587
4588 vsec.vsa_mask = VSA_ACE;
4589 vsec.vsa_aclentp = &full_access;
4590 vsec.vsa_aclentsz = sizeof (full_access);
4591 vsec.vsa_aclcnt = 1;
4592
4593 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4594 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4595 if (resourcevp)
4596 VN_RELE(resourcevp);
4597 break;
4598
4599 case ZFS_SMB_ACL_REMOVE:
4600 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4601 NULL, 0);
4602 break;
4603
4604 case ZFS_SMB_ACL_RENAME:
4605 if ((error = get_nvlist(zc->zc_nvlist_src,
4606 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4607 VN_RELE(vp);
4608 ZFS_EXIT(zsb);
4609 return (error);
4610 }
4611 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4612 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4613 &target)) {
4614 VN_RELE(vp);
4615 VN_RELE(ZTOV(sharedir));
4616 ZFS_EXIT(zsb);
4617 nvlist_free(nvlist);
4618 return (error);
4619 }
4620 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4621 kcred, NULL, 0);
4622 nvlist_free(nvlist);
4623 break;
4624
4625 case ZFS_SMB_ACL_PURGE:
4626 error = zfs_smb_acl_purge(sharedir);
4627 break;
4628
4629 default:
4630 error = EINVAL;
4631 break;
4632 }
4633
4634 VN_RELE(vp);
4635 VN_RELE(ZTOV(sharedir));
4636
4637 ZFS_EXIT(zsb);
4638
4639 return (error);
4640 #else
4641 return (ENOTSUP);
4642 #endif /* HAVE_SMB_SHARE */
4643 }
4644
4645 /*
4646 * inputs:
4647 * zc_name name of filesystem
4648 * zc_value short name of snap
4649 * zc_string user-supplied tag for this hold
4650 * zc_cookie recursive flag
4651 * zc_temphold set if hold is temporary
4652 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4653 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4654 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4655 *
4656 * outputs: none
4657 */
4658 static int
4659 zfs_ioc_hold(zfs_cmd_t *zc)
4660 {
4661 boolean_t recursive = zc->zc_cookie;
4662 spa_t *spa;
4663 dsl_pool_t *dp;
4664 dsl_dataset_t *ds;
4665 int error;
4666 minor_t minor = 0;
4667
4668 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4669 return (EINVAL);
4670
4671 if (zc->zc_sendobj == 0) {
4672 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4673 zc->zc_string, recursive, zc->zc_temphold,
4674 zc->zc_cleanup_fd));
4675 }
4676
4677 if (recursive)
4678 return (EINVAL);
4679
4680 error = spa_open(zc->zc_name, &spa, FTAG);
4681 if (error)
4682 return (error);
4683
4684 dp = spa_get_dsl(spa);
4685 rw_enter(&dp->dp_config_rwlock, RW_READER);
4686 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4687 rw_exit(&dp->dp_config_rwlock);
4688 spa_close(spa, FTAG);
4689 if (error)
4690 return (error);
4691
4692 /*
4693 * Until we have a hold on this snapshot, it's possible that
4694 * zc_sendobj could've been destroyed and reused as part
4695 * of a later txg. Make sure we're looking at the right object.
4696 */
4697 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4698 dsl_dataset_rele(ds, FTAG);
4699 return (ENOENT);
4700 }
4701
4702 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4703 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4704 if (error) {
4705 dsl_dataset_rele(ds, FTAG);
4706 return (error);
4707 }
4708 }
4709
4710 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4711 zc->zc_temphold);
4712 if (minor != 0) {
4713 if (error == 0) {
4714 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4715 minor);
4716 }
4717 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4718 }
4719 dsl_dataset_rele(ds, FTAG);
4720
4721 return (error);
4722 }
4723
4724 /*
4725 * inputs:
4726 * zc_name name of dataset from which we're releasing a user hold
4727 * zc_value short name of snap
4728 * zc_string user-supplied tag for this hold
4729 * zc_cookie recursive flag
4730 *
4731 * outputs: none
4732 */
4733 static int
4734 zfs_ioc_release(zfs_cmd_t *zc)
4735 {
4736 boolean_t recursive = zc->zc_cookie;
4737
4738 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4739 return (EINVAL);
4740
4741 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4742 zc->zc_string, recursive));
4743 }
4744
4745 /*
4746 * inputs:
4747 * zc_name name of filesystem
4748 *
4749 * outputs:
4750 * zc_nvlist_src{_size} nvlist of snapshot holds
4751 */
4752 static int
4753 zfs_ioc_get_holds(zfs_cmd_t *zc)
4754 {
4755 nvlist_t *nvp;
4756 int error;
4757
4758 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4759 error = put_nvlist(zc, nvp);
4760 nvlist_free(nvp);
4761 }
4762
4763 return (error);
4764 }
4765
4766 /*
4767 * inputs:
4768 * zc_guid flags (ZEVENT_NONBLOCK)
4769 *
4770 * outputs:
4771 * zc_nvlist_dst next nvlist event
4772 * zc_cookie dropped events since last get
4773 * zc_cleanup_fd cleanup-on-exit file descriptor
4774 */
4775 static int
4776 zfs_ioc_events_next(zfs_cmd_t *zc)
4777 {
4778 zfs_zevent_t *ze;
4779 nvlist_t *event = NULL;
4780 minor_t minor;
4781 uint64_t dropped = 0;
4782 int error;
4783
4784 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4785 if (error != 0)
4786 return (error);
4787
4788 do {
4789 error = zfs_zevent_next(ze, &event,
4790 &zc->zc_nvlist_dst_size, &dropped);
4791 if (event != NULL) {
4792 zc->zc_cookie = dropped;
4793 error = put_nvlist(zc, event);
4794 nvlist_free(event);
4795 }
4796
4797 if (zc->zc_guid & ZEVENT_NONBLOCK)
4798 break;
4799
4800 if ((error == 0) || (error != ENOENT))
4801 break;
4802
4803 error = zfs_zevent_wait(ze);
4804 if (error)
4805 break;
4806 } while (1);
4807
4808 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4809
4810 return (error);
4811 }
4812
4813 /*
4814 * outputs:
4815 * zc_cookie cleared events count
4816 */
4817 static int
4818 zfs_ioc_events_clear(zfs_cmd_t *zc)
4819 {
4820 int count;
4821
4822 zfs_zevent_drain_all(&count);
4823 zc->zc_cookie = count;
4824
4825 return 0;
4826 }
4827
4828 /*
4829 * inputs:
4830 * zc_name name of new filesystem or snapshot
4831 * zc_value full name of old snapshot
4832 *
4833 * outputs:
4834 * zc_cookie space in bytes
4835 * zc_objset_type compressed space in bytes
4836 * zc_perm_action uncompressed space in bytes
4837 */
4838 static int
4839 zfs_ioc_space_written(zfs_cmd_t *zc)
4840 {
4841 int error;
4842 dsl_dataset_t *new, *old;
4843
4844 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4845 if (error != 0)
4846 return (error);
4847 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4848 if (error != 0) {
4849 dsl_dataset_rele(new, FTAG);
4850 return (error);
4851 }
4852
4853 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4854 &zc->zc_objset_type, &zc->zc_perm_action);
4855 dsl_dataset_rele(old, FTAG);
4856 dsl_dataset_rele(new, FTAG);
4857 return (error);
4858 }
4859
4860 /*
4861 * inputs:
4862 * zc_name full name of last snapshot
4863 * zc_value full name of first snapshot
4864 *
4865 * outputs:
4866 * zc_cookie space in bytes
4867 * zc_objset_type compressed space in bytes
4868 * zc_perm_action uncompressed space in bytes
4869 */
4870 static int
4871 zfs_ioc_space_snaps(zfs_cmd_t *zc)
4872 {
4873 int error;
4874 dsl_dataset_t *new, *old;
4875
4876 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4877 if (error != 0)
4878 return (error);
4879 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4880 if (error != 0) {
4881 dsl_dataset_rele(new, FTAG);
4882 return (error);
4883 }
4884
4885 error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4886 &zc->zc_objset_type, &zc->zc_perm_action);
4887 dsl_dataset_rele(old, FTAG);
4888 dsl_dataset_rele(new, FTAG);
4889 return (error);
4890 }
4891
4892 /*
4893 * pool create, destroy, and export don't log the history as part of
4894 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4895 * do the logging of those commands.
4896 */
4897 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4898 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4899 POOL_CHECK_NONE },
4900 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4901 POOL_CHECK_NONE },
4902 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4903 POOL_CHECK_NONE },
4904 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4905 POOL_CHECK_NONE },
4906 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4907 POOL_CHECK_NONE },
4908 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4909 POOL_CHECK_NONE },
4910 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4911 POOL_CHECK_NONE },
4912 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4913 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4914 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4915 POOL_CHECK_READONLY },
4916 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4917 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4918 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4919 POOL_CHECK_NONE },
4920 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4921 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4922 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4923 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4924 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4925 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4926 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4927 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4928 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4929 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4930 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4931 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4932 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4933 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4934 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4935 POOL_CHECK_SUSPENDED },
4936 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4937 POOL_CHECK_NONE },
4938 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4939 POOL_CHECK_SUSPENDED },
4940 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4941 POOL_CHECK_SUSPENDED },
4942 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4943 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4944 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4945 POOL_CHECK_NONE },
4946 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4947 POOL_CHECK_NONE },
4948 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4949 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4950 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4951 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4952 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4953 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4954 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4955 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4956 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4957 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4958 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE,
4959 POOL_CHECK_NONE },
4960 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4961 POOL_CHECK_NONE },
4962 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4963 POOL_CHECK_NONE },
4964 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4965 POOL_CHECK_NONE },
4966 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4967 POOL_CHECK_NONE },
4968 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4969 POOL_CHECK_NONE },
4970 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4971 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4972 { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive,
4973 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4974 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4975 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4976 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4977 POOL_CHECK_NONE },
4978 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4979 POOL_CHECK_SUSPENDED },
4980 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4981 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4982 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4983 POOL_CHECK_NONE },
4984 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4985 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4986 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4987 POOL_CHECK_NONE },
4988 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4989 POOL_CHECK_NONE },
4990 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4991 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4992 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4993 POOL_CHECK_NONE },
4994 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4995 B_FALSE, POOL_CHECK_NONE },
4996 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4997 B_FALSE, POOL_CHECK_NONE },
4998 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4999 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5000 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
5001 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5002 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
5003 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5004 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5005 POOL_CHECK_SUSPENDED },
5006 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5007 POOL_CHECK_NONE },
5008 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5009 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5010 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5011 POOL_CHECK_NONE },
5012 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
5013 POOL_CHECK_NONE },
5014 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
5015 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5016 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
5017 POOL_CHECK_SUSPENDED },
5018 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
5019 POOL_CHECK_NONE },
5020 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
5021 POOL_CHECK_NONE },
5022 { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5023 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
5024 { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5025 POOL_CHECK_SUSPENDED },
5026 { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5027 POOL_CHECK_SUSPENDED },
5028 { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE,
5029 POOL_CHECK_SUSPENDED },
5030 { zfs_ioc_send_progress, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
5031 POOL_CHECK_NONE }
5032 };
5033
5034 int
5035 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
5036 zfs_ioc_poolcheck_t check)
5037 {
5038 spa_t *spa;
5039 int error;
5040
5041 ASSERT(type == POOL_NAME || type == DATASET_NAME);
5042
5043 if (check & POOL_CHECK_NONE)
5044 return (0);
5045
5046 error = spa_open(name, &spa, FTAG);
5047 if (error == 0) {
5048 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
5049 error = EAGAIN;
5050 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
5051 error = EROFS;
5052 spa_close(spa, FTAG);
5053 }
5054 return (error);
5055 }
5056
5057 static void *
5058 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
5059 {
5060 zfsdev_state_t *zs;
5061
5062 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5063
5064 for (zs = list_head(&zfsdev_state_list); zs != NULL;
5065 zs = list_next(&zfsdev_state_list, zs)) {
5066 if (zs->zs_minor == minor) {
5067 switch (which) {
5068 case ZST_ONEXIT: return (zs->zs_onexit);
5069 case ZST_ZEVENT: return (zs->zs_zevent);
5070 case ZST_ALL: return (zs);
5071 }
5072 }
5073 }
5074
5075 return NULL;
5076 }
5077
5078 void *
5079 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
5080 {
5081 void *ptr;
5082
5083 mutex_enter(&zfsdev_state_lock);
5084 ptr = zfsdev_get_state_impl(minor, which);
5085 mutex_exit(&zfsdev_state_lock);
5086
5087 return ptr;
5088 }
5089
5090 minor_t
5091 zfsdev_getminor(struct file *filp)
5092 {
5093 ASSERT(filp != NULL);
5094 ASSERT(filp->private_data != NULL);
5095
5096 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
5097 }
5098
5099 /*
5100 * Find a free minor number. The zfsdev_state_list is expected to
5101 * be short since it is only a list of currently open file handles.
5102 */
5103 minor_t
5104 zfsdev_minor_alloc(void)
5105 {
5106 static minor_t last_minor = 0;
5107 minor_t m;
5108
5109 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5110
5111 for (m = last_minor + 1; m != last_minor; m++) {
5112 if (m > ZFSDEV_MAX_MINOR)
5113 m = 1;
5114 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
5115 last_minor = m;
5116 return (m);
5117 }
5118 }
5119
5120 return (0);
5121 }
5122
5123 static int
5124 zfsdev_state_init(struct file *filp)
5125 {
5126 zfsdev_state_t *zs;
5127 minor_t minor;
5128
5129 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5130
5131 minor = zfsdev_minor_alloc();
5132 if (minor == 0)
5133 return (ENXIO);
5134
5135 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
5136
5137 zs->zs_file = filp;
5138 zs->zs_minor = minor;
5139 filp->private_data = zs;
5140
5141 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
5142 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
5143
5144 list_insert_tail(&zfsdev_state_list, zs);
5145
5146 return (0);
5147 }
5148
5149 static int
5150 zfsdev_state_destroy(struct file *filp)
5151 {
5152 zfsdev_state_t *zs;
5153
5154 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
5155 ASSERT(filp->private_data != NULL);
5156
5157 zs = filp->private_data;
5158 zfs_onexit_destroy(zs->zs_onexit);
5159 zfs_zevent_destroy(zs->zs_zevent);
5160
5161 list_remove(&zfsdev_state_list, zs);
5162 kmem_free(zs, sizeof(zfsdev_state_t));
5163
5164 return 0;
5165 }
5166
5167 static int
5168 zfsdev_open(struct inode *ino, struct file *filp)
5169 {
5170 int error;
5171
5172 mutex_enter(&zfsdev_state_lock);
5173 error = zfsdev_state_init(filp);
5174 mutex_exit(&zfsdev_state_lock);
5175
5176 return (-error);
5177 }
5178
5179 static int
5180 zfsdev_release(struct inode *ino, struct file *filp)
5181 {
5182 int error;
5183
5184 mutex_enter(&zfsdev_state_lock);
5185 error = zfsdev_state_destroy(filp);
5186 mutex_exit(&zfsdev_state_lock);
5187
5188 return (-error);
5189 }
5190
5191 static long
5192 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5193 {
5194 zfs_cmd_t *zc;
5195 uint_t vec;
5196 int error, rc, flag = 0;
5197
5198 vec = cmd - ZFS_IOC;
5199 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5200 return (-EINVAL);
5201
5202 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
5203
5204 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5205 if (error != 0)
5206 error = EFAULT;
5207
5208 if ((error == 0) && !(flag & FKIOCTL))
5209 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
5210
5211 /*
5212 * Ensure that all pool/dataset names are valid before we pass down to
5213 * the lower layers.
5214 */
5215 if (error == 0) {
5216 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5217 zc->zc_iflags = flag & FKIOCTL;
5218 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5219 case POOL_NAME:
5220 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5221 error = EINVAL;
5222 error = pool_status_check(zc->zc_name,
5223 zfs_ioc_vec[vec].zvec_namecheck,
5224 zfs_ioc_vec[vec].zvec_pool_check);
5225 break;
5226
5227 case DATASET_NAME:
5228 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5229 error = EINVAL;
5230 error = pool_status_check(zc->zc_name,
5231 zfs_ioc_vec[vec].zvec_namecheck,
5232 zfs_ioc_vec[vec].zvec_pool_check);
5233 break;
5234
5235 case NO_NAME:
5236 break;
5237 }
5238 }
5239
5240 if (error == 0)
5241 error = zfs_ioc_vec[vec].zvec_func(zc);
5242
5243 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5244 if (error == 0) {
5245 if (rc != 0)
5246 error = EFAULT;
5247 if (zfs_ioc_vec[vec].zvec_his_log)
5248 zfs_log_history(zc);
5249 }
5250
5251 kmem_free(zc, sizeof (zfs_cmd_t));
5252 return (-error);
5253 }
5254
5255 #ifdef CONFIG_COMPAT
5256 static long
5257 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5258 {
5259 return zfsdev_ioctl(filp, cmd, arg);
5260 }
5261 #else
5262 #define zfsdev_compat_ioctl NULL
5263 #endif
5264
5265 static const struct file_operations zfsdev_fops = {
5266 .open = zfsdev_open,
5267 .release = zfsdev_release,
5268 .unlocked_ioctl = zfsdev_ioctl,
5269 .compat_ioctl = zfsdev_compat_ioctl,
5270 .owner = THIS_MODULE,
5271 };
5272
5273 static struct miscdevice zfs_misc = {
5274 .minor = MISC_DYNAMIC_MINOR,
5275 .name = ZFS_DRIVER,
5276 .fops = &zfsdev_fops,
5277 };
5278
5279 static int
5280 zfs_attach(void)
5281 {
5282 int error;
5283
5284 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5285 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5286 offsetof(zfsdev_state_t, zs_next));
5287
5288 error = misc_register(&zfs_misc);
5289 if (error) {
5290 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5291 return (error);
5292 }
5293
5294 return (0);
5295 }
5296
5297 static void
5298 zfs_detach(void)
5299 {
5300 int error;
5301
5302 error = misc_deregister(&zfs_misc);
5303 if (error)
5304 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5305
5306 mutex_destroy(&zfsdev_state_lock);
5307 list_destroy(&zfsdev_state_list);
5308 }
5309
5310 uint_t zfs_fsyncer_key;
5311 extern uint_t rrw_tsd_key;
5312
5313 #ifdef DEBUG
5314 #define ZFS_DEBUG_STR " (DEBUG mode)"
5315 #else
5316 #define ZFS_DEBUG_STR ""
5317 #endif
5318
5319 int
5320 _init(void)
5321 {
5322 int error;
5323
5324 spa_init(FREAD | FWRITE);
5325 zfs_init();
5326
5327 if ((error = zvol_init()) != 0)
5328 goto out1;
5329
5330 if ((error = zfs_attach()) != 0)
5331 goto out2;
5332
5333 tsd_create(&zfs_fsyncer_key, NULL);
5334 tsd_create(&rrw_tsd_key, NULL);
5335
5336 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
5337 "ZFS pool version %s, ZFS filesystem version %s\n",
5338 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
5339 SPA_VERSION_STRING, ZPL_VERSION_STRING);
5340
5341 return (0);
5342
5343 out2:
5344 (void) zvol_fini();
5345 out1:
5346 zfs_fini();
5347 spa_fini();
5348 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5349 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
5350 ZFS_DEBUG_STR, error);
5351
5352 return (error);
5353 }
5354
5355 int
5356 _fini(void)
5357 {
5358 zfs_detach();
5359 zvol_fini();
5360 zfs_fini();
5361 spa_fini();
5362
5363 tsd_destroy(&zfs_fsyncer_key);
5364 tsd_destroy(&rrw_tsd_key);
5365
5366 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
5367 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
5368
5369 return (0);
5370 }
5371
5372 #ifdef HAVE_SPL
5373 spl_module_init(_init);
5374 spl_module_exit(_fini);
5375
5376 MODULE_DESCRIPTION("ZFS");
5377 MODULE_AUTHOR(ZFS_META_AUTHOR);
5378 MODULE_LICENSE(ZFS_META_LICENSE);
5379 #endif /* HAVE_SPL */