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 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 */
29
30 /*
31 * ZFS ioctls.
32 *
33 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
34 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
35 *
36 * There are two ways that we handle ioctls: the legacy way where almost
37 * all of the logic is in the ioctl callback, and the new way where most
38 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
39 *
40 * Non-legacy ioctls should be registered by calling
41 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
42 * from userland by lzc_ioctl().
43 *
44 * The registration arguments are as follows:
45 *
46 * const char *name
47 * The name of the ioctl. This is used for history logging. If the
155 #include <sys/dsl_deleg.h>
156 #include <sys/dmu_objset.h>
157 #include <sys/dmu_impl.h>
158 #include <sys/ddi.h>
159 #include <sys/sunddi.h>
160 #include <sys/sunldi.h>
161 #include <sys/policy.h>
162 #include <sys/zone.h>
163 #include <sys/nvpair.h>
164 #include <sys/pathname.h>
165 #include <sys/mount.h>
166 #include <sys/sdt.h>
167 #include <sys/fs/zfs.h>
168 #include <sys/zfs_ctldir.h>
169 #include <sys/zfs_dir.h>
170 #include <sys/zfs_onexit.h>
171 #include <sys/zvol.h>
172 #include <sys/dsl_scan.h>
173 #include <sharefs/share.h>
174 #include <sys/dmu_objset.h>
175
176 #include "zfs_namecheck.h"
177 #include "zfs_prop.h"
178 #include "zfs_deleg.h"
179 #include "zfs_comutil.h"
180
181 extern struct modlfs zfs_modlfs;
182
183 extern void zfs_init(void);
184 extern void zfs_fini(void);
185
186 ldi_ident_t zfs_li = NULL;
187 dev_info_t *zfs_dip;
188
189 uint_t zfs_fsyncer_key;
190 extern uint_t rrw_tsd_key;
191 static uint_t zfs_allow_log_key;
192
193 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
194 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
218 } zfs_ioc_vec_t;
219
220 /* This array is indexed by zfs_userquota_prop_t */
221 static const char *userquota_perms[] = {
222 ZFS_DELEG_PERM_USERUSED,
223 ZFS_DELEG_PERM_USERQUOTA,
224 ZFS_DELEG_PERM_GROUPUSED,
225 ZFS_DELEG_PERM_GROUPQUOTA,
226 };
227
228 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
229 static int zfs_check_settable(const char *name, nvpair_t *property,
230 cred_t *cr);
231 static int zfs_check_clearable(char *dataset, nvlist_t *props,
232 nvlist_t **errors);
233 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
234 boolean_t *);
235 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
236 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
237
238 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
239 void
240 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
241 {
242 const char *newfile;
243 char buf[512];
244 va_list adx;
245
246 /*
247 * Get rid of annoying "../common/" prefix to filename.
248 */
249 newfile = strrchr(file, '/');
250 if (newfile != NULL) {
251 newfile = newfile + 1; /* Get rid of leading / */
252 } else {
253 newfile = file;
254 }
255
256 va_start(adx, fmt);
257 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
2366 case ZFS_PROP_VERSION:
2367 {
2368 zfsvfs_t *zfsvfs;
2369
2370 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2371 break;
2372
2373 err = zfs_set_version(zfsvfs, intval);
2374 zfsvfs_rele(zfsvfs, FTAG);
2375
2376 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2377 zfs_cmd_t *zc;
2378
2379 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2380 (void) strcpy(zc->zc_name, dsname);
2381 (void) zfs_ioc_userspace_upgrade(zc);
2382 kmem_free(zc, sizeof (zfs_cmd_t));
2383 }
2384 break;
2385 }
2386
2387 default:
2388 err = -1;
2389 }
2390
2391 return (err);
2392 }
2393
2394 /*
2395 * This function is best effort. If it fails to set any of the given properties,
2396 * it continues to set as many as it can and returns the last error
2397 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2398 * with the list of names of all the properties that failed along with the
2399 * corresponding error numbers.
2400 *
2401 * If every property is set successfully, zero is returned and errlist is not
2402 * modified.
2403 */
2404 int
2405 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2406 nvlist_t *errlist)
3610 case ZFS_PROP_COMPRESSION:
3611 /*
3612 * If the user specified gzip compression, make sure
3613 * the SPA supports it. We ignore any errors here since
3614 * we'll catch them later.
3615 */
3616 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3617 nvpair_value_uint64(pair, &intval) == 0) {
3618 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3619 intval <= ZIO_COMPRESS_GZIP_9 &&
3620 zfs_earlier_version(dsname,
3621 SPA_VERSION_GZIP_COMPRESSION)) {
3622 return (ENOTSUP);
3623 }
3624
3625 if (intval == ZIO_COMPRESS_ZLE &&
3626 zfs_earlier_version(dsname,
3627 SPA_VERSION_ZLE_COMPRESSION))
3628 return (ENOTSUP);
3629
3630 /*
3631 * If this is a bootable dataset then
3632 * verify that the compression algorithm
3633 * is supported for booting. We must return
3634 * something other than ENOTSUP since it
3635 * implies a downrev pool version.
3636 */
3637 if (zfs_is_bootfs(dsname) &&
3638 !BOOTFS_COMPRESS_VALID(intval)) {
3639 return (ERANGE);
3640 }
3641 }
3642 break;
3643
3644 case ZFS_PROP_COPIES:
3645 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3646 return (ENOTSUP);
3647 break;
3648
3649 case ZFS_PROP_DEDUP:
3653
3654 case ZFS_PROP_SHARESMB:
3655 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3656 return (ENOTSUP);
3657 break;
3658
3659 case ZFS_PROP_ACLINHERIT:
3660 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3661 nvpair_value_uint64(pair, &intval) == 0) {
3662 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3663 zfs_earlier_version(dsname,
3664 SPA_VERSION_PASSTHROUGH_X))
3665 return (ENOTSUP);
3666 }
3667 break;
3668 }
3669
3670 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3671 }
3672
3673 /*
3674 * Removes properties from the given props list that fail permission checks
3675 * needed to clear them and to restore them in case of a receive error. For each
3676 * property, make sure we have both set and inherit permissions.
3677 *
3678 * Returns the first error encountered if any permission checks fail. If the
3679 * caller provides a non-NULL errlist, it also gives the complete list of names
3680 * of all the properties that failed a permission check along with the
3681 * corresponding error numbers. The caller is responsible for freeing the
3682 * returned errlist.
3683 *
3684 * If every property checks out successfully, zero is returned and the list
3685 * pointed at by errlist is NULL.
3686 */
3687 static int
3688 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3689 {
3690 zfs_cmd_t *zc;
3691 nvpair_t *pair, *next_pair;
3692 nvlist_t *errors;
|
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 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29 */
30
31 /*
32 * ZFS ioctls.
33 *
34 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
35 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
36 *
37 * There are two ways that we handle ioctls: the legacy way where almost
38 * all of the logic is in the ioctl callback, and the new way where most
39 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
40 *
41 * Non-legacy ioctls should be registered by calling
42 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
43 * from userland by lzc_ioctl().
44 *
45 * The registration arguments are as follows:
46 *
47 * const char *name
48 * The name of the ioctl. This is used for history logging. If the
156 #include <sys/dsl_deleg.h>
157 #include <sys/dmu_objset.h>
158 #include <sys/dmu_impl.h>
159 #include <sys/ddi.h>
160 #include <sys/sunddi.h>
161 #include <sys/sunldi.h>
162 #include <sys/policy.h>
163 #include <sys/zone.h>
164 #include <sys/nvpair.h>
165 #include <sys/pathname.h>
166 #include <sys/mount.h>
167 #include <sys/sdt.h>
168 #include <sys/fs/zfs.h>
169 #include <sys/zfs_ctldir.h>
170 #include <sys/zfs_dir.h>
171 #include <sys/zfs_onexit.h>
172 #include <sys/zvol.h>
173 #include <sys/dsl_scan.h>
174 #include <sharefs/share.h>
175 #include <sys/dmu_objset.h>
176 #include <sys/zfeature.h>
177
178 #include "zfs_namecheck.h"
179 #include "zfs_prop.h"
180 #include "zfs_deleg.h"
181 #include "zfs_comutil.h"
182
183 extern struct modlfs zfs_modlfs;
184
185 extern void zfs_init(void);
186 extern void zfs_fini(void);
187
188 ldi_ident_t zfs_li = NULL;
189 dev_info_t *zfs_dip;
190
191 uint_t zfs_fsyncer_key;
192 extern uint_t rrw_tsd_key;
193 static uint_t zfs_allow_log_key;
194
195 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
196 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
220 } zfs_ioc_vec_t;
221
222 /* This array is indexed by zfs_userquota_prop_t */
223 static const char *userquota_perms[] = {
224 ZFS_DELEG_PERM_USERUSED,
225 ZFS_DELEG_PERM_USERQUOTA,
226 ZFS_DELEG_PERM_GROUPUSED,
227 ZFS_DELEG_PERM_GROUPQUOTA,
228 };
229
230 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
231 static int zfs_check_settable(const char *name, nvpair_t *property,
232 cred_t *cr);
233 static int zfs_check_clearable(char *dataset, nvlist_t *props,
234 nvlist_t **errors);
235 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
236 boolean_t *);
237 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
238 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
239
240 static int zfs_prop_activate_feature(dsl_pool_t *dp, zfeature_info_t *feature);
241 static int zfs_prop_activate_feature_check(void *arg1, void *arg2,
242 dmu_tx_t *tx);
243 static void zfs_prop_activate_feature_sync(void *arg1, void *arg2,
244 dmu_tx_t *tx);
245
246 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
247 void
248 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
249 {
250 const char *newfile;
251 char buf[512];
252 va_list adx;
253
254 /*
255 * Get rid of annoying "../common/" prefix to filename.
256 */
257 newfile = strrchr(file, '/');
258 if (newfile != NULL) {
259 newfile = newfile + 1; /* Get rid of leading / */
260 } else {
261 newfile = file;
262 }
263
264 va_start(adx, fmt);
265 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
2374 case ZFS_PROP_VERSION:
2375 {
2376 zfsvfs_t *zfsvfs;
2377
2378 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2379 break;
2380
2381 err = zfs_set_version(zfsvfs, intval);
2382 zfsvfs_rele(zfsvfs, FTAG);
2383
2384 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2385 zfs_cmd_t *zc;
2386
2387 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2388 (void) strcpy(zc->zc_name, dsname);
2389 (void) zfs_ioc_userspace_upgrade(zc);
2390 kmem_free(zc, sizeof (zfs_cmd_t));
2391 }
2392 break;
2393 }
2394 case ZFS_PROP_COMPRESSION:
2395 {
2396 if (intval == ZIO_COMPRESS_LZ4 ||
2397 intval == ZIO_COMPRESS_LZ4HC) {
2398 zfeature_info_t *feature =
2399 &spa_feature_table[SPA_FEATURE_LZ4_COMPRESS];
2400 spa_t *spa;
2401 dsl_pool_t *dp;
2402
2403 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
2404 return (err);
2405
2406 dp = spa->spa_dsl_pool;
2407
2408 /*
2409 * Setting the LZ4 compression algorithm activates
2410 * the feature.
2411 */
2412 if (!spa_feature_is_active(spa, feature)) {
2413 if ((err = zfs_prop_activate_feature(dp,
2414 feature)) != 0) {
2415 spa_close(spa, FTAG);
2416 return (err);
2417 }
2418 }
2419
2420 spa_close(spa, FTAG);
2421 }
2422 /*
2423 * We still want the default set action to be performed in the
2424 * caller, we only performed zfeature settings here.
2425 */
2426 err = -1;
2427 break;
2428 }
2429
2430 default:
2431 err = -1;
2432 }
2433
2434 return (err);
2435 }
2436
2437 /*
2438 * This function is best effort. If it fails to set any of the given properties,
2439 * it continues to set as many as it can and returns the last error
2440 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2441 * with the list of names of all the properties that failed along with the
2442 * corresponding error numbers.
2443 *
2444 * If every property is set successfully, zero is returned and errlist is not
2445 * modified.
2446 */
2447 int
2448 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2449 nvlist_t *errlist)
3653 case ZFS_PROP_COMPRESSION:
3654 /*
3655 * If the user specified gzip compression, make sure
3656 * the SPA supports it. We ignore any errors here since
3657 * we'll catch them later.
3658 */
3659 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3660 nvpair_value_uint64(pair, &intval) == 0) {
3661 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3662 intval <= ZIO_COMPRESS_GZIP_9 &&
3663 zfs_earlier_version(dsname,
3664 SPA_VERSION_GZIP_COMPRESSION)) {
3665 return (ENOTSUP);
3666 }
3667
3668 if (intval == ZIO_COMPRESS_ZLE &&
3669 zfs_earlier_version(dsname,
3670 SPA_VERSION_ZLE_COMPRESSION))
3671 return (ENOTSUP);
3672
3673 if (intval == ZIO_COMPRESS_LZ4 ||
3674 intval == ZIO_COMPRESS_LZ4HC) {
3675 zfeature_info_t *feature =
3676 &spa_feature_table[
3677 SPA_FEATURE_LZ4_COMPRESS];
3678 spa_t *spa;
3679
3680 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
3681 return (err);
3682
3683 if (!spa_feature_is_enabled(spa, feature)) {
3684 spa_close(spa, FTAG);
3685 return (ENOTSUP);
3686 }
3687 spa_close(spa, FTAG);
3688 }
3689
3690 /*
3691 * If this is a bootable dataset then
3692 * verify that the compression algorithm
3693 * is supported for booting. We must return
3694 * something other than ENOTSUP since it
3695 * implies a downrev pool version.
3696 */
3697 if (zfs_is_bootfs(dsname) &&
3698 !BOOTFS_COMPRESS_VALID(intval)) {
3699 return (ERANGE);
3700 }
3701 }
3702 break;
3703
3704 case ZFS_PROP_COPIES:
3705 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3706 return (ENOTSUP);
3707 break;
3708
3709 case ZFS_PROP_DEDUP:
3713
3714 case ZFS_PROP_SHARESMB:
3715 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3716 return (ENOTSUP);
3717 break;
3718
3719 case ZFS_PROP_ACLINHERIT:
3720 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3721 nvpair_value_uint64(pair, &intval) == 0) {
3722 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3723 zfs_earlier_version(dsname,
3724 SPA_VERSION_PASSTHROUGH_X))
3725 return (ENOTSUP);
3726 }
3727 break;
3728 }
3729
3730 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3731 }
3732
3733 /*
3734 * Activates a feature on a pool in response to a property setting. This
3735 * creates a new sync task which modifies the pool to reflect the feature
3736 * as being active.
3737 */
3738 static int
3739 zfs_prop_activate_feature(dsl_pool_t *dp, zfeature_info_t *feature)
3740 {
3741 int err;
3742
3743 /* EBUSY here indicates that the feature is already active */
3744 err = dsl_sync_task_do(dp, zfs_prop_activate_feature_check,
3745 zfs_prop_activate_feature_sync, dp->dp_spa, feature, 2);
3746
3747 if (err != 0 && err != EBUSY)
3748 return (err);
3749 else
3750 return (0);
3751 }
3752
3753 /*
3754 * Checks for a race condition to make sure we don't increment a feature flag
3755 * multiple times.
3756 */
3757 /*ARGSUSED*/
3758 static int
3759 zfs_prop_activate_feature_check(void *arg1, void *arg2, dmu_tx_t *tx)
3760 {
3761 spa_t *spa = arg1;
3762 zfeature_info_t *feature = arg2;
3763
3764 if (!spa_feature_is_active(spa, feature))
3765 return (0);
3766 else
3767 return (EBUSY);
3768 }
3769
3770 /*
3771 * The callback invoked on feature activation in the sync task caused by
3772 * zfs_prop_activate_feature.
3773 */
3774 static void
3775 zfs_prop_activate_feature_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3776 {
3777 spa_t *spa = arg1;
3778 zfeature_info_t *feature = arg2;
3779
3780 spa_feature_incr(spa, feature, tx);
3781 }
3782
3783 /*
3784 * Removes properties from the given props list that fail permission checks
3785 * needed to clear them and to restore them in case of a receive error. For each
3786 * property, make sure we have both set and inherit permissions.
3787 *
3788 * Returns the first error encountered if any permission checks fail. If the
3789 * caller provides a non-NULL errlist, it also gives the complete list of names
3790 * of all the properties that failed a permission check along with the
3791 * corresponding error numbers. The caller is responsible for freeing the
3792 * returned errlist.
3793 *
3794 * If every property checks out successfully, zero is returned and the list
3795 * pointed at by errlist is NULL.
3796 */
3797 static int
3798 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3799 {
3800 zfs_cmd_t *zc;
3801 nvpair_t *pair, *next_pair;
3802 nvlist_t *errors;
|