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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Portions Copyright 2011 Martin Matuska
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/errno.h>
33 #include <sys/modctl.h>
38 #include <sys/cmn_err.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zfs_vfsops.h>
42 #include <sys/zfs_znode.h>
45 #include <sys/spa_impl.h>
47 #include <sys/priv_impl.h>
49 #include <sys/dsl_dir.h>
50 #include <sys/dsl_dataset.h>
51 #include <sys/dsl_prop.h>
52 #include <sys/dsl_deleg.h>
53 #include <sys/dmu_objset.h>
55 #include <sys/sunddi.h>
56 #include <sys/sunldi.h>
57 #include <sys/policy.h>
59 #include <sys/nvpair.h>
60 #include <sys/pathname.h>
61 #include <sys/mount.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_dir.h>
66 #include <sys/zfs_onexit.h>
68 #include <sys/dsl_scan.h>
69 #include <sharefs/share.h>
70 #include <sys/dmu_objset.h>
72 #include "zfs_namecheck.h"
74 #include "zfs_deleg.h"
75 #include "zfs_comutil.h"
77 extern struct modlfs zfs_modlfs
;
79 extern void zfs_init(void);
80 extern void zfs_fini(void);
82 ldi_ident_t zfs_li
= NULL
;
85 typedef int zfs_ioc_func_t(zfs_cmd_t
*);
86 typedef int zfs_secpolicy_func_t(zfs_cmd_t
*, cred_t
*);
92 } zfs_ioc_namecheck_t
;
95 POOL_CHECK_NONE
= 1 << 0,
96 POOL_CHECK_SUSPENDED
= 1 << 1,
97 POOL_CHECK_READONLY
= 1 << 2
98 } zfs_ioc_poolcheck_t
;
100 typedef struct zfs_ioc_vec
{
101 zfs_ioc_func_t
*zvec_func
;
102 zfs_secpolicy_func_t
*zvec_secpolicy
;
103 zfs_ioc_namecheck_t zvec_namecheck
;
104 boolean_t zvec_his_log
;
105 zfs_ioc_poolcheck_t zvec_pool_check
;
108 /* This array is indexed by zfs_userquota_prop_t */
109 static const char *userquota_perms
[] = {
110 ZFS_DELEG_PERM_USERUSED
,
111 ZFS_DELEG_PERM_USERQUOTA
,
112 ZFS_DELEG_PERM_GROUPUSED
,
113 ZFS_DELEG_PERM_GROUPQUOTA
,
116 static int zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
);
117 static int zfs_check_settable(const char *name
, nvpair_t
*property
,
119 static int zfs_check_clearable(char *dataset
, nvlist_t
*props
,
121 static int zfs_fill_zplprops_root(uint64_t, nvlist_t
*, nvlist_t
*,
123 int zfs_set_prop_nvlist(const char *, zprop_source_t
, nvlist_t
*, nvlist_t
**);
125 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
127 __dprintf(const char *file
, const char *func
, int line
, const char *fmt
, ...)
134 * Get rid of annoying "../common/" prefix to filename.
136 newfile
= strrchr(file
, '/');
137 if (newfile
!= NULL
) {
138 newfile
= newfile
+ 1; /* Get rid of leading / */
144 (void) vsnprintf(buf
, sizeof (buf
), fmt
, adx
);
148 * To get this data, use the zfs-dprintf probe as so:
149 * dtrace -q -n 'zfs-dprintf \
150 * /stringof(arg0) == "dbuf.c"/ \
151 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
153 * arg1 = function name
157 DTRACE_PROBE4(zfs__dprintf
,
158 char *, newfile
, char *, func
, int, line
, char *, buf
);
162 history_str_free(char *buf
)
164 kmem_free(buf
, HIS_MAX_RECORD_LEN
);
168 history_str_get(zfs_cmd_t
*zc
)
172 if (zc
->zc_history
== NULL
)
175 buf
= kmem_alloc(HIS_MAX_RECORD_LEN
, KM_SLEEP
);
176 if (copyinstr((void *)(uintptr_t)zc
->zc_history
,
177 buf
, HIS_MAX_RECORD_LEN
, NULL
) != 0) {
178 history_str_free(buf
);
182 buf
[HIS_MAX_RECORD_LEN
-1] = '\0';
188 * Check to see if the named dataset is currently defined as bootable
191 zfs_is_bootfs(const char *name
)
195 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
197 ret
= (dmu_objset_id(os
) == spa_bootfs(dmu_objset_spa(os
)));
198 dmu_objset_rele(os
, FTAG
);
205 * zfs_earlier_version
207 * Return non-zero if the spa version is less than requested version.
210 zfs_earlier_version(const char *name
, int version
)
214 if (spa_open(name
, &spa
, FTAG
) == 0) {
215 if (spa_version(spa
) < version
) {
216 spa_close(spa
, FTAG
);
219 spa_close(spa
, FTAG
);
225 * zpl_earlier_version
227 * Return TRUE if the ZPL version is less than requested version.
230 zpl_earlier_version(const char *name
, int version
)
233 boolean_t rc
= B_TRUE
;
235 if (dmu_objset_hold(name
, FTAG
, &os
) == 0) {
238 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
239 dmu_objset_rele(os
, FTAG
);
242 /* XXX reading from non-owned objset */
243 if (zfs_get_zplprop(os
, ZFS_PROP_VERSION
, &zplversion
) == 0)
244 rc
= zplversion
< version
;
245 dmu_objset_rele(os
, FTAG
);
251 zfs_log_history(zfs_cmd_t
*zc
)
256 if ((buf
= history_str_get(zc
)) == NULL
)
259 if (spa_open(zc
->zc_name
, &spa
, FTAG
) == 0) {
260 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
)
261 (void) spa_history_log(spa
, buf
, LOG_CMD_NORMAL
);
262 spa_close(spa
, FTAG
);
264 history_str_free(buf
);
268 * Policy for top-level read operations (list pools). Requires no privileges,
269 * and can be used in the local zone, as there is no associated dataset.
273 zfs_secpolicy_none(zfs_cmd_t
*zc
, cred_t
*cr
)
279 * Policy for dataset read operations (list children, get statistics). Requires
280 * no privileges, but must be visible in the local zone.
284 zfs_secpolicy_read(zfs_cmd_t
*zc
, cred_t
*cr
)
286 if (INGLOBALZONE(curproc
) ||
287 zone_dataset_visible(zc
->zc_name
, NULL
))
294 zfs_dozonecheck_impl(const char *dataset
, uint64_t zoned
, cred_t
*cr
)
299 * The dataset must be visible by this zone -- check this first
300 * so they don't see EPERM on something they shouldn't know about.
302 if (!INGLOBALZONE(curproc
) &&
303 !zone_dataset_visible(dataset
, &writable
))
306 if (INGLOBALZONE(curproc
)) {
308 * If the fs is zoned, only root can access it from the
311 if (secpolicy_zfs(cr
) && zoned
)
315 * If we are in a local zone, the 'zoned' property must be set.
320 /* must be writable by this zone */
328 zfs_dozonecheck(const char *dataset
, cred_t
*cr
)
332 if (dsl_prop_get_integer(dataset
, "zoned", &zoned
, NULL
))
335 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
339 zfs_dozonecheck_ds(const char *dataset
, dsl_dataset_t
*ds
, cred_t
*cr
)
343 rw_enter(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
, RW_READER
);
344 if (dsl_prop_get_ds(ds
, "zoned", 8, 1, &zoned
, NULL
)) {
345 rw_exit(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
);
348 rw_exit(&ds
->ds_dir
->dd_pool
->dp_config_rwlock
);
350 return (zfs_dozonecheck_impl(dataset
, zoned
, cr
));
354 * If name ends in a '@', then require recursive permissions.
357 zfs_secpolicy_write_perms(const char *name
, const char *perm
, cred_t
*cr
)
360 boolean_t descendent
= B_FALSE
;
364 at
= strchr(name
, '@');
365 if (at
!= NULL
&& at
[1] == '\0') {
370 error
= dsl_dataset_hold(name
, FTAG
, &ds
);
376 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
378 error
= secpolicy_zfs(cr
);
380 error
= dsl_deleg_access_impl(ds
, descendent
, perm
, cr
);
383 dsl_dataset_rele(ds
, FTAG
);
388 zfs_secpolicy_write_perms_ds(const char *name
, dsl_dataset_t
*ds
,
389 const char *perm
, cred_t
*cr
)
393 error
= zfs_dozonecheck_ds(name
, ds
, cr
);
395 error
= secpolicy_zfs(cr
);
397 error
= dsl_deleg_access_impl(ds
, B_FALSE
, perm
, cr
);
403 * Policy for setting the security label property.
405 * Returns 0 for success, non-zero for access and other errors.
408 zfs_set_slabel_policy(const char *name
, char *strval
, cred_t
*cr
)
410 char ds_hexsl
[MAXNAMELEN
];
411 bslabel_t ds_sl
, new_sl
;
412 boolean_t new_default
= FALSE
;
414 int needed_priv
= -1;
417 /* First get the existing dataset label. */
418 error
= dsl_prop_get(name
, zfs_prop_to_name(ZFS_PROP_MLSLABEL
),
419 1, sizeof (ds_hexsl
), &ds_hexsl
, NULL
);
423 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
426 /* The label must be translatable */
427 if (!new_default
&& (hexstr_to_label(strval
, &new_sl
) != 0))
431 * In a non-global zone, disallow attempts to set a label that
432 * doesn't match that of the zone; otherwise no other checks
435 if (!INGLOBALZONE(curproc
)) {
436 if (new_default
|| !blequal(&new_sl
, CR_SL(CRED())))
442 * For global-zone datasets (i.e., those whose zoned property is
443 * "off", verify that the specified new label is valid for the
446 if (dsl_prop_get_integer(name
,
447 zfs_prop_to_name(ZFS_PROP_ZONED
), &zoned
, NULL
))
450 if (zfs_check_global_label(name
, strval
) != 0)
455 * If the existing dataset label is nondefault, check if the
456 * dataset is mounted (label cannot be changed while mounted).
457 * Get the zfsvfs; if there isn't one, then the dataset isn't
458 * mounted (or isn't a dataset, doesn't exist, ...).
460 if (strcasecmp(ds_hexsl
, ZFS_MLSLABEL_DEFAULT
) != 0) {
462 static char *setsl_tag
= "setsl_tag";
465 * Try to own the dataset; abort if there is any error,
466 * (e.g., already mounted, in use, or other error).
468 error
= dmu_objset_own(name
, DMU_OST_ZFS
, B_TRUE
,
473 dmu_objset_disown(os
, setsl_tag
);
476 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
480 if (hexstr_to_label(strval
, &new_sl
) != 0)
483 if (blstrictdom(&ds_sl
, &new_sl
))
484 needed_priv
= PRIV_FILE_DOWNGRADE_SL
;
485 else if (blstrictdom(&new_sl
, &ds_sl
))
486 needed_priv
= PRIV_FILE_UPGRADE_SL
;
488 /* dataset currently has a default label */
490 needed_priv
= PRIV_FILE_UPGRADE_SL
;
494 if (needed_priv
!= -1)
495 return (PRIV_POLICY(cr
, needed_priv
, B_FALSE
, EPERM
, NULL
));
500 zfs_secpolicy_setprop(const char *dsname
, zfs_prop_t prop
, nvpair_t
*propval
,
506 * Check permissions for special properties.
511 * Disallow setting of 'zoned' from within a local zone.
513 if (!INGLOBALZONE(curproc
))
518 if (!INGLOBALZONE(curproc
)) {
520 char setpoint
[MAXNAMELEN
];
522 * Unprivileged users are allowed to modify the
523 * quota on things *under* (ie. contained by)
524 * the thing they own.
526 if (dsl_prop_get_integer(dsname
, "zoned", &zoned
,
529 if (!zoned
|| strlen(dsname
) <= strlen(setpoint
))
534 case ZFS_PROP_MLSLABEL
:
535 if (!is_system_labeled())
538 if (nvpair_value_string(propval
, &strval
) == 0) {
541 err
= zfs_set_slabel_policy(dsname
, strval
, CRED());
548 return (zfs_secpolicy_write_perms(dsname
, zfs_prop_to_name(prop
), cr
));
552 zfs_secpolicy_fsacl(zfs_cmd_t
*zc
, cred_t
*cr
)
556 error
= zfs_dozonecheck(zc
->zc_name
, cr
);
561 * permission to set permissions will be evaluated later in
562 * dsl_deleg_can_allow()
568 zfs_secpolicy_rollback(zfs_cmd_t
*zc
, cred_t
*cr
)
570 return (zfs_secpolicy_write_perms(zc
->zc_name
,
571 ZFS_DELEG_PERM_ROLLBACK
, cr
));
575 zfs_secpolicy_send(zfs_cmd_t
*zc
, cred_t
*cr
)
584 * Generate the current snapshot name from the given objsetid, then
585 * use that name for the secpolicy/zone checks.
587 cp
= strchr(zc
->zc_name
, '@');
590 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
594 dp
= spa_get_dsl(spa
);
595 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
596 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
597 rw_exit(&dp
->dp_config_rwlock
);
598 spa_close(spa
, FTAG
);
602 dsl_dataset_name(ds
, zc
->zc_name
);
604 error
= zfs_secpolicy_write_perms_ds(zc
->zc_name
, ds
,
605 ZFS_DELEG_PERM_SEND
, cr
);
606 dsl_dataset_rele(ds
, FTAG
);
612 zfs_secpolicy_deleg_share(zfs_cmd_t
*zc
, cred_t
*cr
)
617 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
618 NO_FOLLOW
, NULL
, &vp
)) != 0)
621 /* Now make sure mntpnt and dataset are ZFS */
623 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
624 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
625 zc
->zc_name
) != 0)) {
631 return (dsl_deleg_access(zc
->zc_name
,
632 ZFS_DELEG_PERM_SHARE
, cr
));
636 zfs_secpolicy_share(zfs_cmd_t
*zc
, cred_t
*cr
)
638 if (!INGLOBALZONE(curproc
))
641 if (secpolicy_nfs(cr
) == 0) {
644 return (zfs_secpolicy_deleg_share(zc
, cr
));
649 zfs_secpolicy_smb_acl(zfs_cmd_t
*zc
, cred_t
*cr
)
651 if (!INGLOBALZONE(curproc
))
654 if (secpolicy_smb(cr
) == 0) {
657 return (zfs_secpolicy_deleg_share(zc
, cr
));
662 zfs_get_parent(const char *datasetname
, char *parent
, int parentsize
)
667 * Remove the @bla or /bla from the end of the name to get the parent.
669 (void) strncpy(parent
, datasetname
, parentsize
);
670 cp
= strrchr(parent
, '@');
674 cp
= strrchr(parent
, '/');
684 zfs_secpolicy_destroy_perms(const char *name
, cred_t
*cr
)
688 if ((error
= zfs_secpolicy_write_perms(name
,
689 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
692 return (zfs_secpolicy_write_perms(name
, ZFS_DELEG_PERM_DESTROY
, cr
));
696 zfs_secpolicy_destroy(zfs_cmd_t
*zc
, cred_t
*cr
)
698 return (zfs_secpolicy_destroy_perms(zc
->zc_name
, cr
));
702 * Destroying snapshots with delegated permissions requires
703 * descendent mount and destroy permissions.
706 zfs_secpolicy_destroy_recursive(zfs_cmd_t
*zc
, cred_t
*cr
)
711 dsname
= kmem_asprintf("%s@", zc
->zc_name
);
713 error
= zfs_secpolicy_destroy_perms(dsname
, cr
);
720 zfs_secpolicy_rename_perms(const char *from
, const char *to
, cred_t
*cr
)
722 char parentname
[MAXNAMELEN
];
725 if ((error
= zfs_secpolicy_write_perms(from
,
726 ZFS_DELEG_PERM_RENAME
, cr
)) != 0)
729 if ((error
= zfs_secpolicy_write_perms(from
,
730 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
733 if ((error
= zfs_get_parent(to
, parentname
,
734 sizeof (parentname
))) != 0)
737 if ((error
= zfs_secpolicy_write_perms(parentname
,
738 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
741 if ((error
= zfs_secpolicy_write_perms(parentname
,
742 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
749 zfs_secpolicy_rename(zfs_cmd_t
*zc
, cred_t
*cr
)
751 return (zfs_secpolicy_rename_perms(zc
->zc_name
, zc
->zc_value
, cr
));
755 zfs_secpolicy_promote(zfs_cmd_t
*zc
, cred_t
*cr
)
757 char parentname
[MAXNAMELEN
];
761 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
762 ZFS_DELEG_PERM_PROMOTE
, cr
);
766 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &clone
);
769 dsl_dataset_t
*pclone
= NULL
;
771 dd
= clone
->os_dsl_dataset
->ds_dir
;
773 rw_enter(&dd
->dd_pool
->dp_config_rwlock
, RW_READER
);
774 error
= dsl_dataset_hold_obj(dd
->dd_pool
,
775 dd
->dd_phys
->dd_origin_obj
, FTAG
, &pclone
);
776 rw_exit(&dd
->dd_pool
->dp_config_rwlock
);
778 dmu_objset_rele(clone
, FTAG
);
782 error
= zfs_secpolicy_write_perms(zc
->zc_name
,
783 ZFS_DELEG_PERM_MOUNT
, cr
);
785 dsl_dataset_name(pclone
, parentname
);
786 dmu_objset_rele(clone
, FTAG
);
787 dsl_dataset_rele(pclone
, FTAG
);
789 error
= zfs_secpolicy_write_perms(parentname
,
790 ZFS_DELEG_PERM_PROMOTE
, cr
);
796 zfs_secpolicy_receive(zfs_cmd_t
*zc
, cred_t
*cr
)
800 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
801 ZFS_DELEG_PERM_RECEIVE
, cr
)) != 0)
804 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
805 ZFS_DELEG_PERM_MOUNT
, cr
)) != 0)
808 return (zfs_secpolicy_write_perms(zc
->zc_name
,
809 ZFS_DELEG_PERM_CREATE
, cr
));
813 zfs_secpolicy_snapshot_perms(const char *name
, cred_t
*cr
)
815 return (zfs_secpolicy_write_perms(name
,
816 ZFS_DELEG_PERM_SNAPSHOT
, cr
));
820 zfs_secpolicy_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
823 return (zfs_secpolicy_snapshot_perms(zc
->zc_name
, cr
));
827 zfs_secpolicy_create(zfs_cmd_t
*zc
, cred_t
*cr
)
829 char parentname
[MAXNAMELEN
];
832 if ((error
= zfs_get_parent(zc
->zc_name
, parentname
,
833 sizeof (parentname
))) != 0)
836 if (zc
->zc_value
[0] != '\0') {
837 if ((error
= zfs_secpolicy_write_perms(zc
->zc_value
,
838 ZFS_DELEG_PERM_CLONE
, cr
)) != 0)
842 if ((error
= zfs_secpolicy_write_perms(parentname
,
843 ZFS_DELEG_PERM_CREATE
, cr
)) != 0)
846 error
= zfs_secpolicy_write_perms(parentname
,
847 ZFS_DELEG_PERM_MOUNT
, cr
);
853 zfs_secpolicy_umount(zfs_cmd_t
*zc
, cred_t
*cr
)
857 error
= secpolicy_fs_unmount(cr
, NULL
);
859 error
= dsl_deleg_access(zc
->zc_name
, ZFS_DELEG_PERM_MOUNT
, cr
);
865 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
866 * SYS_CONFIG privilege, which is not available in a local zone.
870 zfs_secpolicy_config(zfs_cmd_t
*zc
, cred_t
*cr
)
872 if (secpolicy_sys_config(cr
, B_FALSE
) != 0)
879 * Policy for object to name lookups.
883 zfs_secpolicy_diff(zfs_cmd_t
*zc
, cred_t
*cr
)
887 if ((error
= secpolicy_sys_config(cr
, B_FALSE
)) == 0)
890 error
= zfs_secpolicy_write_perms(zc
->zc_name
, ZFS_DELEG_PERM_DIFF
, cr
);
895 * Policy for fault injection. Requires all privileges.
899 zfs_secpolicy_inject(zfs_cmd_t
*zc
, cred_t
*cr
)
901 return (secpolicy_zinject(cr
));
905 zfs_secpolicy_inherit(zfs_cmd_t
*zc
, cred_t
*cr
)
907 zfs_prop_t prop
= zfs_name_to_prop(zc
->zc_value
);
909 if (prop
== ZPROP_INVAL
) {
910 if (!zfs_prop_user(zc
->zc_value
))
912 return (zfs_secpolicy_write_perms(zc
->zc_name
,
913 ZFS_DELEG_PERM_USERPROP
, cr
));
915 return (zfs_secpolicy_setprop(zc
->zc_name
, prop
,
921 zfs_secpolicy_userspace_one(zfs_cmd_t
*zc
, cred_t
*cr
)
923 int err
= zfs_secpolicy_read(zc
, cr
);
927 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
930 if (zc
->zc_value
[0] == 0) {
932 * They are asking about a posix uid/gid. If it's
933 * themself, allow it.
935 if (zc
->zc_objset_type
== ZFS_PROP_USERUSED
||
936 zc
->zc_objset_type
== ZFS_PROP_USERQUOTA
) {
937 if (zc
->zc_guid
== crgetuid(cr
))
940 if (groupmember(zc
->zc_guid
, cr
))
945 return (zfs_secpolicy_write_perms(zc
->zc_name
,
946 userquota_perms
[zc
->zc_objset_type
], cr
));
950 zfs_secpolicy_userspace_many(zfs_cmd_t
*zc
, cred_t
*cr
)
952 int err
= zfs_secpolicy_read(zc
, cr
);
956 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
959 return (zfs_secpolicy_write_perms(zc
->zc_name
,
960 userquota_perms
[zc
->zc_objset_type
], cr
));
964 zfs_secpolicy_userspace_upgrade(zfs_cmd_t
*zc
, cred_t
*cr
)
966 return (zfs_secpolicy_setprop(zc
->zc_name
, ZFS_PROP_VERSION
,
971 zfs_secpolicy_hold(zfs_cmd_t
*zc
, cred_t
*cr
)
973 return (zfs_secpolicy_write_perms(zc
->zc_name
,
974 ZFS_DELEG_PERM_HOLD
, cr
));
978 zfs_secpolicy_release(zfs_cmd_t
*zc
, cred_t
*cr
)
980 return (zfs_secpolicy_write_perms(zc
->zc_name
,
981 ZFS_DELEG_PERM_RELEASE
, cr
));
985 * Policy for allowing temporary snapshots to be taken or released
988 zfs_secpolicy_tmp_snapshot(zfs_cmd_t
*zc
, cred_t
*cr
)
991 * A temporary snapshot is the same as a snapshot,
992 * hold, destroy and release all rolled into one.
993 * Delegated diff alone is sufficient that we allow this.
997 if ((error
= zfs_secpolicy_write_perms(zc
->zc_name
,
998 ZFS_DELEG_PERM_DIFF
, cr
)) == 0)
1001 error
= zfs_secpolicy_snapshot(zc
, cr
);
1003 error
= zfs_secpolicy_hold(zc
, cr
);
1005 error
= zfs_secpolicy_release(zc
, cr
);
1007 error
= zfs_secpolicy_destroy(zc
, cr
);
1012 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1015 get_nvlist(uint64_t nvl
, uint64_t size
, int iflag
, nvlist_t
**nvp
)
1019 nvlist_t
*list
= NULL
;
1022 * Read in and unpack the user-supplied nvlist.
1027 packed
= kmem_alloc(size
, KM_SLEEP
);
1029 if ((error
= ddi_copyin((void *)(uintptr_t)nvl
, packed
, size
,
1031 kmem_free(packed
, size
);
1035 if ((error
= nvlist_unpack(packed
, size
, &list
, 0)) != 0) {
1036 kmem_free(packed
, size
);
1040 kmem_free(packed
, size
);
1047 fit_error_list(zfs_cmd_t
*zc
, nvlist_t
**errors
)
1051 VERIFY(nvlist_size(*errors
, &size
, NV_ENCODE_NATIVE
) == 0);
1053 if (size
> zc
->zc_nvlist_dst_size
) {
1054 nvpair_t
*more_errors
;
1057 if (zc
->zc_nvlist_dst_size
< 1024)
1060 VERIFY(nvlist_add_int32(*errors
, ZPROP_N_MORE_ERRORS
, 0) == 0);
1061 more_errors
= nvlist_prev_nvpair(*errors
, NULL
);
1064 nvpair_t
*pair
= nvlist_prev_nvpair(*errors
,
1066 VERIFY(nvlist_remove_nvpair(*errors
, pair
) == 0);
1068 VERIFY(nvlist_size(*errors
, &size
,
1069 NV_ENCODE_NATIVE
) == 0);
1070 } while (size
> zc
->zc_nvlist_dst_size
);
1072 VERIFY(nvlist_remove_nvpair(*errors
, more_errors
) == 0);
1073 VERIFY(nvlist_add_int32(*errors
, ZPROP_N_MORE_ERRORS
, n
) == 0);
1074 ASSERT(nvlist_size(*errors
, &size
, NV_ENCODE_NATIVE
) == 0);
1075 ASSERT(size
<= zc
->zc_nvlist_dst_size
);
1082 put_nvlist(zfs_cmd_t
*zc
, nvlist_t
*nvl
)
1084 char *packed
= NULL
;
1088 VERIFY(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
) == 0);
1090 if (size
> zc
->zc_nvlist_dst_size
) {
1093 packed
= kmem_alloc(size
, KM_SLEEP
);
1094 VERIFY(nvlist_pack(nvl
, &packed
, &size
, NV_ENCODE_NATIVE
,
1096 if (ddi_copyout(packed
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
1097 size
, zc
->zc_iflags
) != 0)
1099 kmem_free(packed
, size
);
1102 zc
->zc_nvlist_dst_size
= size
;
1107 getzfsvfs(const char *dsname
, zfsvfs_t
**zfvp
)
1112 error
= dmu_objset_hold(dsname
, FTAG
, &os
);
1115 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1116 dmu_objset_rele(os
, FTAG
);
1120 mutex_enter(&os
->os_user_ptr_lock
);
1121 *zfvp
= dmu_objset_get_user(os
);
1123 VFS_HOLD((*zfvp
)->z_vfs
);
1127 mutex_exit(&os
->os_user_ptr_lock
);
1128 dmu_objset_rele(os
, FTAG
);
1133 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1134 * case its z_vfs will be NULL, and it will be opened as the owner.
1137 zfsvfs_hold(const char *name
, void *tag
, zfsvfs_t
**zfvp
, boolean_t writer
)
1141 if (getzfsvfs(name
, zfvp
) != 0)
1142 error
= zfsvfs_create(name
, zfvp
);
1144 rrw_enter(&(*zfvp
)->z_teardown_lock
, (writer
) ? RW_WRITER
:
1146 if ((*zfvp
)->z_unmounted
) {
1148 * XXX we could probably try again, since the unmounting
1149 * thread should be just about to disassociate the
1150 * objset from the zfsvfs.
1152 rrw_exit(&(*zfvp
)->z_teardown_lock
, tag
);
1160 zfsvfs_rele(zfsvfs_t
*zfsvfs
, void *tag
)
1162 rrw_exit(&zfsvfs
->z_teardown_lock
, tag
);
1164 if (zfsvfs
->z_vfs
) {
1165 VFS_RELE(zfsvfs
->z_vfs
);
1167 dmu_objset_disown(zfsvfs
->z_os
, zfsvfs
);
1168 zfsvfs_free(zfsvfs
);
1173 zfs_ioc_pool_create(zfs_cmd_t
*zc
)
1176 nvlist_t
*config
, *props
= NULL
;
1177 nvlist_t
*rootprops
= NULL
;
1178 nvlist_t
*zplprops
= NULL
;
1181 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1182 zc
->zc_iflags
, &config
))
1185 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1186 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1187 zc
->zc_iflags
, &props
))) {
1188 nvlist_free(config
);
1193 nvlist_t
*nvl
= NULL
;
1194 uint64_t version
= SPA_VERSION
;
1196 (void) nvlist_lookup_uint64(props
,
1197 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
);
1198 if (version
< SPA_VERSION_INITIAL
|| version
> SPA_VERSION
) {
1200 goto pool_props_bad
;
1202 (void) nvlist_lookup_nvlist(props
, ZPOOL_ROOTFS_PROPS
, &nvl
);
1204 error
= nvlist_dup(nvl
, &rootprops
, KM_SLEEP
);
1206 nvlist_free(config
);
1210 (void) nvlist_remove_all(props
, ZPOOL_ROOTFS_PROPS
);
1212 VERIFY(nvlist_alloc(&zplprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1213 error
= zfs_fill_zplprops_root(version
, rootprops
,
1216 goto pool_props_bad
;
1219 buf
= history_str_get(zc
);
1221 error
= spa_create(zc
->zc_name
, config
, props
, buf
, zplprops
);
1224 * Set the remaining root properties
1226 if (!error
&& (error
= zfs_set_prop_nvlist(zc
->zc_name
,
1227 ZPROP_SRC_LOCAL
, rootprops
, NULL
)) != 0)
1228 (void) spa_destroy(zc
->zc_name
);
1231 history_str_free(buf
);
1234 nvlist_free(rootprops
);
1235 nvlist_free(zplprops
);
1236 nvlist_free(config
);
1243 zfs_ioc_pool_destroy(zfs_cmd_t
*zc
)
1246 zfs_log_history(zc
);
1247 error
= spa_destroy(zc
->zc_name
);
1249 zvol_remove_minors(zc
->zc_name
);
1254 zfs_ioc_pool_import(zfs_cmd_t
*zc
)
1256 nvlist_t
*config
, *props
= NULL
;
1260 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1261 zc
->zc_iflags
, &config
)) != 0)
1264 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1265 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1266 zc
->zc_iflags
, &props
))) {
1267 nvlist_free(config
);
1271 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &guid
) != 0 ||
1272 guid
!= zc
->zc_guid
)
1275 error
= spa_import(zc
->zc_name
, config
, props
, zc
->zc_cookie
);
1277 if (zc
->zc_nvlist_dst
!= 0) {
1280 if ((err
= put_nvlist(zc
, config
)) != 0)
1284 nvlist_free(config
);
1293 zfs_ioc_pool_export(zfs_cmd_t
*zc
)
1296 boolean_t force
= (boolean_t
)zc
->zc_cookie
;
1297 boolean_t hardforce
= (boolean_t
)zc
->zc_guid
;
1299 zfs_log_history(zc
);
1300 error
= spa_export(zc
->zc_name
, NULL
, force
, hardforce
);
1302 zvol_remove_minors(zc
->zc_name
);
1307 zfs_ioc_pool_configs(zfs_cmd_t
*zc
)
1312 if ((configs
= spa_all_configs(&zc
->zc_cookie
)) == NULL
)
1315 error
= put_nvlist(zc
, configs
);
1317 nvlist_free(configs
);
1323 zfs_ioc_pool_stats(zfs_cmd_t
*zc
)
1329 error
= spa_get_stats(zc
->zc_name
, &config
, zc
->zc_value
,
1330 sizeof (zc
->zc_value
));
1332 if (config
!= NULL
) {
1333 ret
= put_nvlist(zc
, config
);
1334 nvlist_free(config
);
1337 * The config may be present even if 'error' is non-zero.
1338 * In this case we return success, and preserve the real errno
1341 zc
->zc_cookie
= error
;
1350 * Try to import the given pool, returning pool stats as appropriate so that
1351 * user land knows which devices are available and overall pool health.
1354 zfs_ioc_pool_tryimport(zfs_cmd_t
*zc
)
1356 nvlist_t
*tryconfig
, *config
;
1359 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1360 zc
->zc_iflags
, &tryconfig
)) != 0)
1363 config
= spa_tryimport(tryconfig
);
1365 nvlist_free(tryconfig
);
1370 error
= put_nvlist(zc
, config
);
1371 nvlist_free(config
);
1378 * zc_name name of the pool
1379 * zc_cookie scan func (pool_scan_func_t)
1382 zfs_ioc_pool_scan(zfs_cmd_t
*zc
)
1387 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1390 if (zc
->zc_cookie
== POOL_SCAN_NONE
)
1391 error
= spa_scan_stop(spa
);
1393 error
= spa_scan(spa
, zc
->zc_cookie
);
1395 spa_close(spa
, FTAG
);
1401 zfs_ioc_pool_freeze(zfs_cmd_t
*zc
)
1406 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1409 spa_close(spa
, FTAG
);
1415 zfs_ioc_pool_upgrade(zfs_cmd_t
*zc
)
1420 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1423 if (zc
->zc_cookie
< spa_version(spa
) || zc
->zc_cookie
> SPA_VERSION
) {
1424 spa_close(spa
, FTAG
);
1428 spa_upgrade(spa
, zc
->zc_cookie
);
1429 spa_close(spa
, FTAG
);
1435 zfs_ioc_pool_get_history(zfs_cmd_t
*zc
)
1442 if ((size
= zc
->zc_history_len
) == 0)
1445 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1448 if (spa_version(spa
) < SPA_VERSION_ZPOOL_HISTORY
) {
1449 spa_close(spa
, FTAG
);
1453 hist_buf
= kmem_alloc(size
, KM_SLEEP
);
1454 if ((error
= spa_history_get(spa
, &zc
->zc_history_offset
,
1455 &zc
->zc_history_len
, hist_buf
)) == 0) {
1456 error
= ddi_copyout(hist_buf
,
1457 (void *)(uintptr_t)zc
->zc_history
,
1458 zc
->zc_history_len
, zc
->zc_iflags
);
1461 spa_close(spa
, FTAG
);
1462 kmem_free(hist_buf
, size
);
1467 zfs_ioc_pool_reguid(zfs_cmd_t
*zc
)
1472 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1474 error
= spa_change_guid(spa
);
1475 spa_close(spa
, FTAG
);
1481 zfs_ioc_dsobj_to_dsname(zfs_cmd_t
*zc
)
1485 if (error
= dsl_dsobj_to_dsname(zc
->zc_name
, zc
->zc_obj
, zc
->zc_value
))
1493 * zc_name name of filesystem
1494 * zc_obj object to find
1497 * zc_value name of object
1500 zfs_ioc_obj_to_path(zfs_cmd_t
*zc
)
1505 /* XXX reading from objset not owned */
1506 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1508 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1509 dmu_objset_rele(os
, FTAG
);
1512 error
= zfs_obj_to_path(os
, zc
->zc_obj
, zc
->zc_value
,
1513 sizeof (zc
->zc_value
));
1514 dmu_objset_rele(os
, FTAG
);
1521 * zc_name name of filesystem
1522 * zc_obj object to find
1525 * zc_stat stats on object
1526 * zc_value path to object
1529 zfs_ioc_obj_to_stats(zfs_cmd_t
*zc
)
1534 /* XXX reading from objset not owned */
1535 if ((error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) != 0)
1537 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
1538 dmu_objset_rele(os
, FTAG
);
1541 error
= zfs_obj_to_stats(os
, zc
->zc_obj
, &zc
->zc_stat
, zc
->zc_value
,
1542 sizeof (zc
->zc_value
));
1543 dmu_objset_rele(os
, FTAG
);
1549 zfs_ioc_vdev_add(zfs_cmd_t
*zc
)
1553 nvlist_t
*config
, **l2cache
, **spares
;
1554 uint_t nl2cache
= 0, nspares
= 0;
1556 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1560 error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1561 zc
->zc_iflags
, &config
);
1562 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_L2CACHE
,
1563 &l2cache
, &nl2cache
);
1565 (void) nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_SPARES
,
1569 * A root pool with concatenated devices is not supported.
1570 * Thus, can not add a device to a root pool.
1572 * Intent log device can not be added to a rootpool because
1573 * during mountroot, zil is replayed, a seperated log device
1574 * can not be accessed during the mountroot time.
1576 * l2cache and spare devices are ok to be added to a rootpool.
1578 if (spa_bootfs(spa
) != 0 && nl2cache
== 0 && nspares
== 0) {
1579 nvlist_free(config
);
1580 spa_close(spa
, FTAG
);
1585 error
= spa_vdev_add(spa
, config
);
1586 nvlist_free(config
);
1588 spa_close(spa
, FTAG
);
1594 * zc_name name of the pool
1595 * zc_nvlist_conf nvlist of devices to remove
1596 * zc_cookie to stop the remove?
1599 zfs_ioc_vdev_remove(zfs_cmd_t
*zc
)
1604 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1607 error
= spa_vdev_remove(spa
, zc
->zc_guid
, B_FALSE
);
1608 spa_close(spa
, FTAG
);
1613 zfs_ioc_vdev_set_state(zfs_cmd_t
*zc
)
1617 vdev_state_t newstate
= VDEV_STATE_UNKNOWN
;
1619 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1621 switch (zc
->zc_cookie
) {
1622 case VDEV_STATE_ONLINE
:
1623 error
= vdev_online(spa
, zc
->zc_guid
, zc
->zc_obj
, &newstate
);
1626 case VDEV_STATE_OFFLINE
:
1627 error
= vdev_offline(spa
, zc
->zc_guid
, zc
->zc_obj
);
1630 case VDEV_STATE_FAULTED
:
1631 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1632 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1633 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1635 error
= vdev_fault(spa
, zc
->zc_guid
, zc
->zc_obj
);
1638 case VDEV_STATE_DEGRADED
:
1639 if (zc
->zc_obj
!= VDEV_AUX_ERR_EXCEEDED
&&
1640 zc
->zc_obj
!= VDEV_AUX_EXTERNAL
)
1641 zc
->zc_obj
= VDEV_AUX_ERR_EXCEEDED
;
1643 error
= vdev_degrade(spa
, zc
->zc_guid
, zc
->zc_obj
);
1649 zc
->zc_cookie
= newstate
;
1650 spa_close(spa
, FTAG
);
1655 zfs_ioc_vdev_attach(zfs_cmd_t
*zc
)
1658 int replacing
= zc
->zc_cookie
;
1662 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1665 if ((error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1666 zc
->zc_iflags
, &config
)) == 0) {
1667 error
= spa_vdev_attach(spa
, zc
->zc_guid
, config
, replacing
);
1668 nvlist_free(config
);
1671 spa_close(spa
, FTAG
);
1676 zfs_ioc_vdev_detach(zfs_cmd_t
*zc
)
1681 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1684 error
= spa_vdev_detach(spa
, zc
->zc_guid
, 0, B_FALSE
);
1686 spa_close(spa
, FTAG
);
1691 zfs_ioc_vdev_split(zfs_cmd_t
*zc
)
1694 nvlist_t
*config
, *props
= NULL
;
1696 boolean_t exp
= !!(zc
->zc_cookie
& ZPOOL_EXPORT_AFTER_SPLIT
);
1698 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
1701 if (error
= get_nvlist(zc
->zc_nvlist_conf
, zc
->zc_nvlist_conf_size
,
1702 zc
->zc_iflags
, &config
)) {
1703 spa_close(spa
, FTAG
);
1707 if (zc
->zc_nvlist_src_size
!= 0 && (error
=
1708 get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
1709 zc
->zc_iflags
, &props
))) {
1710 spa_close(spa
, FTAG
);
1711 nvlist_free(config
);
1715 error
= spa_vdev_split_mirror(spa
, zc
->zc_string
, config
, props
, exp
);
1717 spa_close(spa
, FTAG
);
1719 nvlist_free(config
);
1726 zfs_ioc_vdev_setpath(zfs_cmd_t
*zc
)
1729 char *path
= zc
->zc_value
;
1730 uint64_t guid
= zc
->zc_guid
;
1733 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1737 error
= spa_vdev_setpath(spa
, guid
, path
);
1738 spa_close(spa
, FTAG
);
1743 zfs_ioc_vdev_setfru(zfs_cmd_t
*zc
)
1746 char *fru
= zc
->zc_value
;
1747 uint64_t guid
= zc
->zc_guid
;
1750 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
1754 error
= spa_vdev_setfru(spa
, guid
, fru
);
1755 spa_close(spa
, FTAG
);
1760 zfs_ioc_objset_stats_impl(zfs_cmd_t
*zc
, objset_t
*os
)
1765 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1767 if (zc
->zc_nvlist_dst
!= 0 &&
1768 (error
= dsl_prop_get_all(os
, &nv
)) == 0) {
1769 dmu_objset_stats(os
, nv
);
1771 * NB: zvol_get_stats() will read the objset contents,
1772 * which we aren't supposed to do with a
1773 * DS_MODE_USER hold, because it could be
1774 * inconsistent. So this is a bit of a workaround...
1775 * XXX reading with out owning
1777 if (!zc
->zc_objset_stats
.dds_inconsistent
&&
1778 dmu_objset_type(os
) == DMU_OST_ZVOL
) {
1779 error
= zvol_get_stats(os
, nv
);
1782 VERIFY3S(error
, ==, 0);
1784 error
= put_nvlist(zc
, nv
);
1793 * zc_name name of filesystem
1794 * zc_nvlist_dst_size size of buffer for property nvlist
1797 * zc_objset_stats stats
1798 * zc_nvlist_dst property nvlist
1799 * zc_nvlist_dst_size size of property nvlist
1802 zfs_ioc_objset_stats(zfs_cmd_t
*zc
)
1804 objset_t
*os
= NULL
;
1807 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
1810 error
= zfs_ioc_objset_stats_impl(zc
, os
);
1812 dmu_objset_rele(os
, FTAG
);
1819 * zc_name name of filesystem
1820 * zc_nvlist_dst_size size of buffer for property nvlist
1823 * zc_nvlist_dst received property nvlist
1824 * zc_nvlist_dst_size size of received property nvlist
1826 * Gets received properties (distinct from local properties on or after
1827 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1828 * local property values.
1831 zfs_ioc_objset_recvd_props(zfs_cmd_t
*zc
)
1833 objset_t
*os
= NULL
;
1837 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
1841 * Without this check, we would return local property values if the
1842 * caller has not already received properties on or after
1843 * SPA_VERSION_RECVD_PROPS.
1845 if (!dsl_prop_get_hasrecvd(os
)) {
1846 dmu_objset_rele(os
, FTAG
);
1850 if (zc
->zc_nvlist_dst
!= 0 &&
1851 (error
= dsl_prop_get_received(os
, &nv
)) == 0) {
1852 error
= put_nvlist(zc
, nv
);
1856 dmu_objset_rele(os
, FTAG
);
1861 nvl_add_zplprop(objset_t
*os
, nvlist_t
*props
, zfs_prop_t prop
)
1867 * zfs_get_zplprop() will either find a value or give us
1868 * the default value (if there is one).
1870 if ((error
= zfs_get_zplprop(os
, prop
, &value
)) != 0)
1872 VERIFY(nvlist_add_uint64(props
, zfs_prop_to_name(prop
), value
) == 0);
1878 * zc_name name of filesystem
1879 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1882 * zc_nvlist_dst zpl property nvlist
1883 * zc_nvlist_dst_size size of zpl property nvlist
1886 zfs_ioc_objset_zplprops(zfs_cmd_t
*zc
)
1891 /* XXX reading without owning */
1892 if (err
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
))
1895 dmu_objset_fast_stat(os
, &zc
->zc_objset_stats
);
1898 * NB: nvl_add_zplprop() will read the objset contents,
1899 * which we aren't supposed to do with a DS_MODE_USER
1900 * hold, because it could be inconsistent.
1902 if (zc
->zc_nvlist_dst
!= NULL
&&
1903 !zc
->zc_objset_stats
.dds_inconsistent
&&
1904 dmu_objset_type(os
) == DMU_OST_ZFS
) {
1907 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1908 if ((err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_VERSION
)) == 0 &&
1909 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_NORMALIZE
)) == 0 &&
1910 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_UTF8ONLY
)) == 0 &&
1911 (err
= nvl_add_zplprop(os
, nv
, ZFS_PROP_CASE
)) == 0)
1912 err
= put_nvlist(zc
, nv
);
1917 dmu_objset_rele(os
, FTAG
);
1922 dataset_name_hidden(const char *name
)
1925 * Skip over datasets that are not visible in this zone,
1926 * internal datasets (which have a $ in their name), and
1927 * temporary datasets (which have a % in their name).
1929 if (strchr(name
, '$') != NULL
)
1931 if (strchr(name
, '%') != NULL
)
1933 if (!INGLOBALZONE(curproc
) && !zone_dataset_visible(name
, NULL
))
1940 * zc_name name of filesystem
1941 * zc_cookie zap cursor
1942 * zc_nvlist_dst_size size of buffer for property nvlist
1945 * zc_name name of next filesystem
1946 * zc_cookie zap cursor
1947 * zc_objset_stats stats
1948 * zc_nvlist_dst property nvlist
1949 * zc_nvlist_dst_size size of property nvlist
1952 zfs_ioc_dataset_list_next(zfs_cmd_t
*zc
)
1957 size_t orig_len
= strlen(zc
->zc_name
);
1960 if (error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
)) {
1961 if (error
== ENOENT
)
1966 p
= strrchr(zc
->zc_name
, '/');
1967 if (p
== NULL
|| p
[1] != '\0')
1968 (void) strlcat(zc
->zc_name
, "/", sizeof (zc
->zc_name
));
1969 p
= zc
->zc_name
+ strlen(zc
->zc_name
);
1972 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1973 * but is not declared void because its called by dmu_objset_find().
1975 if (zc
->zc_cookie
== 0) {
1976 uint64_t cookie
= 0;
1977 int len
= sizeof (zc
->zc_name
) - (p
- zc
->zc_name
);
1979 while (dmu_dir_list_next(os
, len
, p
, NULL
, &cookie
) == 0) {
1980 if (!dataset_name_hidden(zc
->zc_name
))
1981 (void) dmu_objset_prefetch(zc
->zc_name
, NULL
);
1986 error
= dmu_dir_list_next(os
,
1987 sizeof (zc
->zc_name
) - (p
- zc
->zc_name
), p
,
1988 NULL
, &zc
->zc_cookie
);
1989 if (error
== ENOENT
)
1991 } while (error
== 0 && dataset_name_hidden(zc
->zc_name
));
1992 dmu_objset_rele(os
, FTAG
);
1995 * If it's an internal dataset (ie. with a '$' in its name),
1996 * don't try to get stats for it, otherwise we'll return ENOENT.
1998 if (error
== 0 && strchr(zc
->zc_name
, '$') == NULL
) {
1999 error
= zfs_ioc_objset_stats(zc
); /* fill in the stats */
2000 if (error
== ENOENT
) {
2001 /* We lost a race with destroy, get the next one. */
2002 zc
->zc_name
[orig_len
] = '\0';
2011 * zc_name name of filesystem
2012 * zc_cookie zap cursor
2013 * zc_nvlist_dst_size size of buffer for property nvlist
2016 * zc_name name of next snapshot
2017 * zc_objset_stats stats
2018 * zc_nvlist_dst property nvlist
2019 * zc_nvlist_dst_size size of property nvlist
2022 zfs_ioc_snapshot_list_next(zfs_cmd_t
*zc
)
2028 if (zc
->zc_cookie
== 0)
2029 (void) dmu_objset_find(zc
->zc_name
, dmu_objset_prefetch
,
2030 NULL
, DS_FIND_SNAPSHOTS
);
2032 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
2034 return (error
== ENOENT
? ESRCH
: error
);
2037 * A dataset name of maximum length cannot have any snapshots,
2038 * so exit immediately.
2040 if (strlcat(zc
->zc_name
, "@", sizeof (zc
->zc_name
)) >= MAXNAMELEN
) {
2041 dmu_objset_rele(os
, FTAG
);
2045 error
= dmu_snapshot_list_next(os
,
2046 sizeof (zc
->zc_name
) - strlen(zc
->zc_name
),
2047 zc
->zc_name
+ strlen(zc
->zc_name
), &zc
->zc_obj
, &zc
->zc_cookie
,
2052 dsl_pool_t
*dp
= os
->os_dsl_dataset
->ds_dir
->dd_pool
;
2055 * Since we probably don't have a hold on this snapshot,
2056 * it's possible that the objsetid could have been destroyed
2057 * and reused for a new objset. It's OK if this happens during
2058 * a zfs send operation, since the new createtxg will be
2059 * beyond the range we're interested in.
2061 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
2062 error
= dsl_dataset_hold_obj(dp
, zc
->zc_obj
, FTAG
, &ds
);
2063 rw_exit(&dp
->dp_config_rwlock
);
2065 if (error
== ENOENT
) {
2066 /* Racing with destroy, get the next one. */
2067 *strchr(zc
->zc_name
, '@') = '\0';
2068 dmu_objset_rele(os
, FTAG
);
2074 error
= dmu_objset_from_ds(ds
, &ossnap
);
2076 error
= zfs_ioc_objset_stats_impl(zc
, ossnap
);
2077 dsl_dataset_rele(ds
, FTAG
);
2079 } else if (error
== ENOENT
) {
2083 dmu_objset_rele(os
, FTAG
);
2084 /* if we failed, undo the @ that we tacked on to zc_name */
2086 *strchr(zc
->zc_name
, '@') = '\0';
2091 zfs_prop_set_userquota(const char *dsname
, nvpair_t
*pair
)
2093 const char *propname
= nvpair_name(pair
);
2095 unsigned int vallen
;
2098 zfs_userquota_prop_t type
;
2104 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2106 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2107 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2113 * A correctly constructed propname is encoded as
2114 * userquota@<rid>-<domain>.
2116 if ((dash
= strchr(propname
, '-')) == NULL
||
2117 nvpair_value_uint64_array(pair
, &valary
, &vallen
) != 0 ||
2126 err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_FALSE
);
2128 err
= zfs_set_userquota(zfsvfs
, type
, domain
, rid
, quota
);
2129 zfsvfs_rele(zfsvfs
, FTAG
);
2136 * If the named property is one that has a special function to set its value,
2137 * return 0 on success and a positive error code on failure; otherwise if it is
2138 * not one of the special properties handled by this function, return -1.
2140 * XXX: It would be better for callers of the property interface if we handled
2141 * these special cases in dsl_prop.c (in the dsl layer).
2144 zfs_prop_set_special(const char *dsname
, zprop_source_t source
,
2147 const char *propname
= nvpair_name(pair
);
2148 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2152 if (prop
== ZPROP_INVAL
) {
2153 if (zfs_prop_userquota(propname
))
2154 return (zfs_prop_set_userquota(dsname
, pair
));
2158 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2160 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2161 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2165 if (zfs_prop_get_type(prop
) == PROP_TYPE_STRING
)
2168 VERIFY(0 == nvpair_value_uint64(pair
, &intval
));
2171 case ZFS_PROP_QUOTA
:
2172 err
= dsl_dir_set_quota(dsname
, source
, intval
);
2174 case ZFS_PROP_REFQUOTA
:
2175 err
= dsl_dataset_set_quota(dsname
, source
, intval
);
2177 case ZFS_PROP_RESERVATION
:
2178 err
= dsl_dir_set_reservation(dsname
, source
, intval
);
2180 case ZFS_PROP_REFRESERVATION
:
2181 err
= dsl_dataset_set_reservation(dsname
, source
, intval
);
2183 case ZFS_PROP_VOLSIZE
:
2184 err
= zvol_set_volsize(dsname
, ddi_driver_major(zfs_dip
),
2187 case ZFS_PROP_VERSION
:
2191 if ((err
= zfsvfs_hold(dsname
, FTAG
, &zfsvfs
, B_TRUE
)) != 0)
2194 err
= zfs_set_version(zfsvfs
, intval
);
2195 zfsvfs_rele(zfsvfs
, FTAG
);
2197 if (err
== 0 && intval
>= ZPL_VERSION_USERSPACE
) {
2200 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
2201 (void) strcpy(zc
->zc_name
, dsname
);
2202 (void) zfs_ioc_userspace_upgrade(zc
);
2203 kmem_free(zc
, sizeof (zfs_cmd_t
));
2216 * This function is best effort. If it fails to set any of the given properties,
2217 * it continues to set as many as it can and returns the first error
2218 * encountered. If the caller provides a non-NULL errlist, it also gives the
2219 * complete list of names of all the properties it failed to set along with the
2220 * corresponding error numbers. The caller is responsible for freeing the
2223 * If every property is set successfully, zero is returned and the list pointed
2224 * at by errlist is NULL.
2227 zfs_set_prop_nvlist(const char *dsname
, zprop_source_t source
, nvlist_t
*nvl
,
2235 nvlist_t
*genericnvl
;
2239 VERIFY(nvlist_alloc(&genericnvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2240 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2241 VERIFY(nvlist_alloc(&retrynvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2245 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2246 const char *propname
= nvpair_name(pair
);
2247 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2250 /* decode the property value */
2252 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2254 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2255 if (nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2260 /* Validate value type */
2261 if (err
== 0 && prop
== ZPROP_INVAL
) {
2262 if (zfs_prop_user(propname
)) {
2263 if (nvpair_type(propval
) != DATA_TYPE_STRING
)
2265 } else if (zfs_prop_userquota(propname
)) {
2266 if (nvpair_type(propval
) !=
2267 DATA_TYPE_UINT64_ARRAY
)
2272 } else if (err
== 0) {
2273 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2274 if (zfs_prop_get_type(prop
) != PROP_TYPE_STRING
)
2276 } else if (nvpair_type(propval
) == DATA_TYPE_UINT64
) {
2279 VERIFY(nvpair_value_uint64(propval
,
2282 switch (zfs_prop_get_type(prop
)) {
2283 case PROP_TYPE_NUMBER
:
2285 case PROP_TYPE_STRING
:
2288 case PROP_TYPE_INDEX
:
2289 if (zfs_prop_index_to_string(prop
,
2290 intval
, &unused
) != 0)
2295 "unknown property type");
2302 /* Validate permissions */
2304 err
= zfs_check_settable(dsname
, pair
, CRED());
2307 err
= zfs_prop_set_special(dsname
, source
, pair
);
2310 * For better performance we build up a list of
2311 * properties to set in a single transaction.
2313 err
= nvlist_add_nvpair(genericnvl
, pair
);
2314 } else if (err
!= 0 && nvl
!= retrynvl
) {
2316 * This may be a spurious error caused by
2317 * receiving quota and reservation out of order.
2318 * Try again in a second pass.
2320 err
= nvlist_add_nvpair(retrynvl
, pair
);
2325 VERIFY(nvlist_add_int32(errors
, propname
, err
) == 0);
2328 if (nvl
!= retrynvl
&& !nvlist_empty(retrynvl
)) {
2333 if (!nvlist_empty(genericnvl
) &&
2334 dsl_props_set(dsname
, source
, genericnvl
) != 0) {
2336 * If this fails, we still want to set as many properties as we
2337 * can, so try setting them individually.
2340 while ((pair
= nvlist_next_nvpair(genericnvl
, pair
)) != NULL
) {
2341 const char *propname
= nvpair_name(pair
);
2345 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
2347 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
2348 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
2352 if (nvpair_type(propval
) == DATA_TYPE_STRING
) {
2353 VERIFY(nvpair_value_string(propval
,
2355 err
= dsl_prop_set(dsname
, propname
, source
, 1,
2356 strlen(strval
) + 1, strval
);
2358 VERIFY(nvpair_value_uint64(propval
,
2360 err
= dsl_prop_set(dsname
, propname
, source
, 8,
2365 VERIFY(nvlist_add_int32(errors
, propname
,
2370 nvlist_free(genericnvl
);
2371 nvlist_free(retrynvl
);
2373 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
2374 nvlist_free(errors
);
2377 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
2380 if (errlist
== NULL
)
2381 nvlist_free(errors
);
2389 * Check that all the properties are valid user properties.
2392 zfs_check_userprops(char *fsname
, nvlist_t
*nvl
)
2394 nvpair_t
*pair
= NULL
;
2397 while ((pair
= nvlist_next_nvpair(nvl
, pair
)) != NULL
) {
2398 const char *propname
= nvpair_name(pair
);
2401 if (!zfs_prop_user(propname
) ||
2402 nvpair_type(pair
) != DATA_TYPE_STRING
)
2405 if (error
= zfs_secpolicy_write_perms(fsname
,
2406 ZFS_DELEG_PERM_USERPROP
, CRED()))
2409 if (strlen(propname
) >= ZAP_MAXNAMELEN
)
2410 return (ENAMETOOLONG
);
2412 VERIFY(nvpair_value_string(pair
, &valstr
) == 0);
2413 if (strlen(valstr
) >= ZAP_MAXVALUELEN
)
2420 props_skip(nvlist_t
*props
, nvlist_t
*skipped
, nvlist_t
**newprops
)
2424 VERIFY(nvlist_alloc(newprops
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2427 while ((pair
= nvlist_next_nvpair(props
, pair
)) != NULL
) {
2428 if (nvlist_exists(skipped
, nvpair_name(pair
)))
2431 VERIFY(nvlist_add_nvpair(*newprops
, pair
) == 0);
2436 clear_received_props(objset_t
*os
, const char *fs
, nvlist_t
*props
,
2440 nvlist_t
*cleared_props
= NULL
;
2441 props_skip(props
, skipped
, &cleared_props
);
2442 if (!nvlist_empty(cleared_props
)) {
2444 * Acts on local properties until the dataset has received
2445 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2447 zprop_source_t flags
= (ZPROP_SRC_NONE
|
2448 (dsl_prop_get_hasrecvd(os
) ? ZPROP_SRC_RECEIVED
: 0));
2449 err
= zfs_set_prop_nvlist(fs
, flags
, cleared_props
, NULL
);
2451 nvlist_free(cleared_props
);
2457 * zc_name name of filesystem
2458 * zc_value name of property to set
2459 * zc_nvlist_src{_size} nvlist of properties to apply
2460 * zc_cookie received properties flag
2463 * zc_nvlist_dst{_size} error for each unapplied received property
2466 zfs_ioc_set_prop(zfs_cmd_t
*zc
)
2469 boolean_t received
= zc
->zc_cookie
;
2470 zprop_source_t source
= (received
? ZPROP_SRC_RECEIVED
:
2472 nvlist_t
*errors
= NULL
;
2475 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2476 zc
->zc_iflags
, &nvl
)) != 0)
2480 nvlist_t
*origprops
;
2483 if (dmu_objset_hold(zc
->zc_name
, FTAG
, &os
) == 0) {
2484 if (dsl_prop_get_received(os
, &origprops
) == 0) {
2485 (void) clear_received_props(os
,
2486 zc
->zc_name
, origprops
, nvl
);
2487 nvlist_free(origprops
);
2490 dsl_prop_set_hasrecvd(os
);
2491 dmu_objset_rele(os
, FTAG
);
2495 error
= zfs_set_prop_nvlist(zc
->zc_name
, source
, nvl
, &errors
);
2497 if (zc
->zc_nvlist_dst
!= NULL
&& errors
!= NULL
) {
2498 (void) put_nvlist(zc
, errors
);
2501 nvlist_free(errors
);
2508 * zc_name name of filesystem
2509 * zc_value name of property to inherit
2510 * zc_cookie revert to received value if TRUE
2515 zfs_ioc_inherit_prop(zfs_cmd_t
*zc
)
2517 const char *propname
= zc
->zc_value
;
2518 zfs_prop_t prop
= zfs_name_to_prop(propname
);
2519 boolean_t received
= zc
->zc_cookie
;
2520 zprop_source_t source
= (received
2521 ? ZPROP_SRC_NONE
/* revert to received value, if any */
2522 : ZPROP_SRC_INHERITED
); /* explicitly inherit */
2531 * zfs_prop_set_special() expects properties in the form of an
2532 * nvpair with type info.
2534 if (prop
== ZPROP_INVAL
) {
2535 if (!zfs_prop_user(propname
))
2538 type
= PROP_TYPE_STRING
;
2539 } else if (prop
== ZFS_PROP_VOLSIZE
||
2540 prop
== ZFS_PROP_VERSION
) {
2543 type
= zfs_prop_get_type(prop
);
2546 VERIFY(nvlist_alloc(&dummy
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2549 case PROP_TYPE_STRING
:
2550 VERIFY(0 == nvlist_add_string(dummy
, propname
, ""));
2552 case PROP_TYPE_NUMBER
:
2553 case PROP_TYPE_INDEX
:
2554 VERIFY(0 == nvlist_add_uint64(dummy
, propname
, 0));
2561 pair
= nvlist_next_nvpair(dummy
, NULL
);
2562 err
= zfs_prop_set_special(zc
->zc_name
, source
, pair
);
2565 return (err
); /* special property already handled */
2568 * Only check this in the non-received case. We want to allow
2569 * 'inherit -S' to revert non-inheritable properties like quota
2570 * and reservation to the received or default values even though
2571 * they are not considered inheritable.
2573 if (prop
!= ZPROP_INVAL
&& !zfs_prop_inheritable(prop
))
2577 /* the property name has been validated by zfs_secpolicy_inherit() */
2578 return (dsl_prop_set(zc
->zc_name
, zc
->zc_value
, source
, 0, 0, NULL
));
2582 zfs_ioc_pool_set_props(zfs_cmd_t
*zc
)
2589 if (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2590 zc
->zc_iflags
, &props
))
2594 * If the only property is the configfile, then just do a spa_lookup()
2595 * to handle the faulted case.
2597 pair
= nvlist_next_nvpair(props
, NULL
);
2598 if (pair
!= NULL
&& strcmp(nvpair_name(pair
),
2599 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
)) == 0 &&
2600 nvlist_next_nvpair(props
, pair
) == NULL
) {
2601 mutex_enter(&spa_namespace_lock
);
2602 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
) {
2603 spa_configfile_set(spa
, props
, B_FALSE
);
2604 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
2606 mutex_exit(&spa_namespace_lock
);
2613 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2618 error
= spa_prop_set(spa
, props
);
2621 spa_close(spa
, FTAG
);
2627 zfs_ioc_pool_get_props(zfs_cmd_t
*zc
)
2631 nvlist_t
*nvp
= NULL
;
2633 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0) {
2635 * If the pool is faulted, there may be properties we can still
2636 * get (such as altroot and cachefile), so attempt to get them
2639 mutex_enter(&spa_namespace_lock
);
2640 if ((spa
= spa_lookup(zc
->zc_name
)) != NULL
)
2641 error
= spa_prop_get(spa
, &nvp
);
2642 mutex_exit(&spa_namespace_lock
);
2644 error
= spa_prop_get(spa
, &nvp
);
2645 spa_close(spa
, FTAG
);
2648 if (error
== 0 && zc
->zc_nvlist_dst
!= NULL
)
2649 error
= put_nvlist(zc
, nvp
);
2659 * zc_name name of filesystem
2660 * zc_nvlist_src{_size} nvlist of delegated permissions
2661 * zc_perm_action allow/unallow flag
2666 zfs_ioc_set_fsacl(zfs_cmd_t
*zc
)
2669 nvlist_t
*fsaclnv
= NULL
;
2671 if ((error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2672 zc
->zc_iflags
, &fsaclnv
)) != 0)
2676 * Verify nvlist is constructed correctly
2678 if ((error
= zfs_deleg_verify_nvlist(fsaclnv
)) != 0) {
2679 nvlist_free(fsaclnv
);
2684 * If we don't have PRIV_SYS_MOUNT, then validate
2685 * that user is allowed to hand out each permission in
2689 error
= secpolicy_zfs(CRED());
2691 if (zc
->zc_perm_action
== B_FALSE
) {
2692 error
= dsl_deleg_can_allow(zc
->zc_name
,
2695 error
= dsl_deleg_can_unallow(zc
->zc_name
,
2701 error
= dsl_deleg_set(zc
->zc_name
, fsaclnv
, zc
->zc_perm_action
);
2703 nvlist_free(fsaclnv
);
2709 * zc_name name of filesystem
2712 * zc_nvlist_src{_size} nvlist of delegated permissions
2715 zfs_ioc_get_fsacl(zfs_cmd_t
*zc
)
2720 if ((error
= dsl_deleg_get(zc
->zc_name
, &nvp
)) == 0) {
2721 error
= put_nvlist(zc
, nvp
);
2729 * Search the vfs list for a specified resource. Returns a pointer to it
2730 * or NULL if no suitable entry is found. The caller of this routine
2731 * is responsible for releasing the returned vfs pointer.
2734 zfs_get_vfs(const char *resource
)
2737 struct vfs
*vfs_found
= NULL
;
2739 vfs_list_read_lock();
2742 if (strcmp(refstr_value(vfsp
->vfs_resource
), resource
) == 0) {
2747 vfsp
= vfsp
->vfs_next
;
2748 } while (vfsp
!= rootvfs
);
2755 zfs_create_cb(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
)
2757 zfs_creat_t
*zct
= arg
;
2759 zfs_create_fs(os
, cr
, zct
->zct_zplprops
, tx
);
2762 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2766 * createprops list of properties requested by creator
2767 * default_zplver zpl version to use if unspecified in createprops
2768 * fuids_ok fuids allowed in this version of the spa?
2769 * os parent objset pointer (NULL if root fs)
2772 * zplprops values for the zplprops we attach to the master node object
2773 * is_ci true if requested file system will be purely case-insensitive
2775 * Determine the settings for utf8only, normalization and
2776 * casesensitivity. Specific values may have been requested by the
2777 * creator and/or we can inherit values from the parent dataset. If
2778 * the file system is of too early a vintage, a creator can not
2779 * request settings for these properties, even if the requested
2780 * setting is the default value. We don't actually want to create dsl
2781 * properties for these, so remove them from the source nvlist after
2785 zfs_fill_zplprops_impl(objset_t
*os
, uint64_t zplver
,
2786 boolean_t fuids_ok
, boolean_t sa_ok
, nvlist_t
*createprops
,
2787 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2789 uint64_t sense
= ZFS_PROP_UNDEFINED
;
2790 uint64_t norm
= ZFS_PROP_UNDEFINED
;
2791 uint64_t u8
= ZFS_PROP_UNDEFINED
;
2793 ASSERT(zplprops
!= NULL
);
2796 * Pull out creator prop choices, if any.
2799 (void) nvlist_lookup_uint64(createprops
,
2800 zfs_prop_to_name(ZFS_PROP_VERSION
), &zplver
);
2801 (void) nvlist_lookup_uint64(createprops
,
2802 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), &norm
);
2803 (void) nvlist_remove_all(createprops
,
2804 zfs_prop_to_name(ZFS_PROP_NORMALIZE
));
2805 (void) nvlist_lookup_uint64(createprops
,
2806 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), &u8
);
2807 (void) nvlist_remove_all(createprops
,
2808 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
2809 (void) nvlist_lookup_uint64(createprops
,
2810 zfs_prop_to_name(ZFS_PROP_CASE
), &sense
);
2811 (void) nvlist_remove_all(createprops
,
2812 zfs_prop_to_name(ZFS_PROP_CASE
));
2816 * If the zpl version requested is whacky or the file system
2817 * or pool is version is too "young" to support normalization
2818 * and the creator tried to set a value for one of the props,
2821 if ((zplver
< ZPL_VERSION_INITIAL
|| zplver
> ZPL_VERSION
) ||
2822 (zplver
>= ZPL_VERSION_FUID
&& !fuids_ok
) ||
2823 (zplver
>= ZPL_VERSION_SA
&& !sa_ok
) ||
2824 (zplver
< ZPL_VERSION_NORMALIZATION
&&
2825 (norm
!= ZFS_PROP_UNDEFINED
|| u8
!= ZFS_PROP_UNDEFINED
||
2826 sense
!= ZFS_PROP_UNDEFINED
)))
2830 * Put the version in the zplprops
2832 VERIFY(nvlist_add_uint64(zplprops
,
2833 zfs_prop_to_name(ZFS_PROP_VERSION
), zplver
) == 0);
2835 if (norm
== ZFS_PROP_UNDEFINED
)
2836 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_NORMALIZE
, &norm
) == 0);
2837 VERIFY(nvlist_add_uint64(zplprops
,
2838 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), norm
) == 0);
2841 * If we're normalizing, names must always be valid UTF-8 strings.
2845 if (u8
== ZFS_PROP_UNDEFINED
)
2846 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_UTF8ONLY
, &u8
) == 0);
2847 VERIFY(nvlist_add_uint64(zplprops
,
2848 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), u8
) == 0);
2850 if (sense
== ZFS_PROP_UNDEFINED
)
2851 VERIFY(zfs_get_zplprop(os
, ZFS_PROP_CASE
, &sense
) == 0);
2852 VERIFY(nvlist_add_uint64(zplprops
,
2853 zfs_prop_to_name(ZFS_PROP_CASE
), sense
) == 0);
2856 *is_ci
= (sense
== ZFS_CASE_INSENSITIVE
);
2862 zfs_fill_zplprops(const char *dataset
, nvlist_t
*createprops
,
2863 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2865 boolean_t fuids_ok
, sa_ok
;
2866 uint64_t zplver
= ZPL_VERSION
;
2867 objset_t
*os
= NULL
;
2868 char parentname
[MAXNAMELEN
];
2874 (void) strlcpy(parentname
, dataset
, sizeof (parentname
));
2875 cp
= strrchr(parentname
, '/');
2879 if ((error
= spa_open(dataset
, &spa
, FTAG
)) != 0)
2882 spa_vers
= spa_version(spa
);
2883 spa_close(spa
, FTAG
);
2885 zplver
= zfs_zpl_version_map(spa_vers
);
2886 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
2887 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
2890 * Open parent object set so we can inherit zplprop values.
2892 if ((error
= dmu_objset_hold(parentname
, FTAG
, &os
)) != 0)
2895 error
= zfs_fill_zplprops_impl(os
, zplver
, fuids_ok
, sa_ok
, createprops
,
2897 dmu_objset_rele(os
, FTAG
);
2902 zfs_fill_zplprops_root(uint64_t spa_vers
, nvlist_t
*createprops
,
2903 nvlist_t
*zplprops
, boolean_t
*is_ci
)
2907 uint64_t zplver
= ZPL_VERSION
;
2910 zplver
= zfs_zpl_version_map(spa_vers
);
2911 fuids_ok
= (zplver
>= ZPL_VERSION_FUID
);
2912 sa_ok
= (zplver
>= ZPL_VERSION_SA
);
2914 error
= zfs_fill_zplprops_impl(NULL
, zplver
, fuids_ok
, sa_ok
,
2915 createprops
, zplprops
, is_ci
);
2921 * zc_objset_type type of objset to create (fs vs zvol)
2922 * zc_name name of new objset
2923 * zc_value name of snapshot to clone from (may be empty)
2924 * zc_nvlist_src{_size} nvlist of properties to apply
2929 zfs_ioc_create(zfs_cmd_t
*zc
)
2934 nvlist_t
*nvprops
= NULL
;
2935 void (*cbfunc
)(objset_t
*os
, void *arg
, cred_t
*cr
, dmu_tx_t
*tx
);
2936 dmu_objset_type_t type
= zc
->zc_objset_type
;
2941 cbfunc
= zfs_create_cb
;
2945 cbfunc
= zvol_create_cb
;
2952 if (strchr(zc
->zc_name
, '@') ||
2953 strchr(zc
->zc_name
, '%'))
2956 if (zc
->zc_nvlist_src
!= NULL
&&
2957 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
2958 zc
->zc_iflags
, &nvprops
)) != 0)
2961 zct
.zct_zplprops
= NULL
;
2962 zct
.zct_props
= nvprops
;
2964 if (zc
->zc_value
[0] != '\0') {
2966 * We're creating a clone of an existing snapshot.
2968 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
2969 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0) {
2970 nvlist_free(nvprops
);
2974 error
= dmu_objset_hold(zc
->zc_value
, FTAG
, &clone
);
2976 nvlist_free(nvprops
);
2980 error
= dmu_objset_clone(zc
->zc_name
, dmu_objset_ds(clone
), 0);
2981 dmu_objset_rele(clone
, FTAG
);
2983 nvlist_free(nvprops
);
2987 boolean_t is_insensitive
= B_FALSE
;
2989 if (cbfunc
== NULL
) {
2990 nvlist_free(nvprops
);
2994 if (type
== DMU_OST_ZVOL
) {
2995 uint64_t volsize
, volblocksize
;
2997 if (nvprops
== NULL
||
2998 nvlist_lookup_uint64(nvprops
,
2999 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
3001 nvlist_free(nvprops
);
3005 if ((error
= nvlist_lookup_uint64(nvprops
,
3006 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3007 &volblocksize
)) != 0 && error
!= ENOENT
) {
3008 nvlist_free(nvprops
);
3013 volblocksize
= zfs_prop_default_numeric(
3014 ZFS_PROP_VOLBLOCKSIZE
);
3016 if ((error
= zvol_check_volblocksize(
3017 volblocksize
)) != 0 ||
3018 (error
= zvol_check_volsize(volsize
,
3019 volblocksize
)) != 0) {
3020 nvlist_free(nvprops
);
3023 } else if (type
== DMU_OST_ZFS
) {
3027 * We have to have normalization and
3028 * case-folding flags correct when we do the
3029 * file system creation, so go figure them out
3032 VERIFY(nvlist_alloc(&zct
.zct_zplprops
,
3033 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3034 error
= zfs_fill_zplprops(zc
->zc_name
, nvprops
,
3035 zct
.zct_zplprops
, &is_insensitive
);
3037 nvlist_free(nvprops
);
3038 nvlist_free(zct
.zct_zplprops
);
3042 error
= dmu_objset_create(zc
->zc_name
, type
,
3043 is_insensitive
? DS_FLAG_CI_DATASET
: 0, cbfunc
, &zct
);
3044 nvlist_free(zct
.zct_zplprops
);
3048 * It would be nice to do this atomically.
3051 error
= zfs_set_prop_nvlist(zc
->zc_name
, ZPROP_SRC_LOCAL
,
3054 (void) dmu_objset_destroy(zc
->zc_name
, B_FALSE
);
3056 nvlist_free(nvprops
);
3062 * zc_name name of filesystem
3063 * zc_value short name of snapshot
3064 * zc_cookie recursive flag
3065 * zc_nvlist_src[_size] property list
3068 * zc_value short snapname (i.e. part after the '@')
3071 zfs_ioc_snapshot(zfs_cmd_t
*zc
)
3073 nvlist_t
*nvprops
= NULL
;
3075 boolean_t recursive
= zc
->zc_cookie
;
3077 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
3080 if (zc
->zc_nvlist_src
!= NULL
&&
3081 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3082 zc
->zc_iflags
, &nvprops
)) != 0)
3085 error
= zfs_check_userprops(zc
->zc_name
, nvprops
);
3089 if (!nvlist_empty(nvprops
) &&
3090 zfs_earlier_version(zc
->zc_name
, SPA_VERSION_SNAP_PROPS
)) {
3095 error
= dmu_objset_snapshot(zc
->zc_name
, zc
->zc_value
, NULL
,
3096 nvprops
, recursive
, B_FALSE
, -1);
3099 nvlist_free(nvprops
);
3104 zfs_unmount_snap(const char *name
, void *arg
)
3109 char *snapname
= arg
;
3110 char *fullname
= kmem_asprintf("%s@%s", name
, snapname
);
3111 vfsp
= zfs_get_vfs(fullname
);
3113 } else if (strchr(name
, '@')) {
3114 vfsp
= zfs_get_vfs(name
);
3119 * Always force the unmount for snapshots.
3121 int flag
= MS_FORCE
;
3124 if ((err
= vn_vfswlock(vfsp
->vfs_vnodecovered
)) != 0) {
3129 if ((err
= dounmount(vfsp
, flag
, kcred
)) != 0)
3137 * zc_name name of filesystem, snaps must be under it
3138 * zc_nvlist_src[_size] full names of snapshots to destroy
3139 * zc_defer_destroy mark for deferred destroy
3142 * zc_name on failure, name of failed snapshot
3145 zfs_ioc_destroy_snaps_nvl(zfs_cmd_t
*zc
)
3151 if ((err
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3152 zc
->zc_iflags
, &nvl
)) != 0)
3155 len
= strlen(zc
->zc_name
);
3156 for (pair
= nvlist_next_nvpair(nvl
, NULL
); pair
!= NULL
;
3157 pair
= nvlist_next_nvpair(nvl
, pair
)) {
3158 const char *name
= nvpair_name(pair
);
3160 * The snap name must be underneath the zc_name. This ensures
3161 * that our permission checks were legitimate.
3163 if (strncmp(zc
->zc_name
, name
, len
) != 0 ||
3164 (name
[len
] != '@' && name
[len
] != '/')) {
3169 (void) zfs_unmount_snap(name
, NULL
);
3172 err
= dmu_snapshots_destroy_nvl(nvl
, zc
->zc_defer_destroy
,
3180 * zc_name name of dataset to destroy
3181 * zc_objset_type type of objset
3182 * zc_defer_destroy mark for deferred destroy
3187 zfs_ioc_destroy(zfs_cmd_t
*zc
)
3190 if (strchr(zc
->zc_name
, '@') && zc
->zc_objset_type
== DMU_OST_ZFS
) {
3191 err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
3196 err
= dmu_objset_destroy(zc
->zc_name
, zc
->zc_defer_destroy
);
3197 if (zc
->zc_objset_type
== DMU_OST_ZVOL
&& err
== 0)
3198 (void) zvol_remove_minor(zc
->zc_name
);
3204 * zc_name name of dataset to rollback (to most recent snapshot)
3209 zfs_ioc_rollback(zfs_cmd_t
*zc
)
3211 dsl_dataset_t
*ds
, *clone
;
3216 error
= dsl_dataset_hold(zc
->zc_name
, FTAG
, &ds
);
3220 /* must not be a snapshot */
3221 if (dsl_dataset_is_snapshot(ds
)) {
3222 dsl_dataset_rele(ds
, FTAG
);
3226 /* must have a most recent snapshot */
3227 if (ds
->ds_phys
->ds_prev_snap_txg
< TXG_INITIAL
) {
3228 dsl_dataset_rele(ds
, FTAG
);
3233 * Create clone of most recent snapshot.
3235 clone_name
= kmem_asprintf("%s/%%rollback", zc
->zc_name
);
3236 error
= dmu_objset_clone(clone_name
, ds
->ds_prev
, DS_FLAG_INCONSISTENT
);
3240 error
= dsl_dataset_own(clone_name
, B_TRUE
, FTAG
, &clone
);
3247 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
3248 error
= zfs_suspend_fs(zfsvfs
);
3252 if (dsl_dataset_tryown(ds
, B_FALSE
, FTAG
)) {
3253 error
= dsl_dataset_clone_swap(clone
, ds
,
3255 dsl_dataset_disown(ds
, FTAG
);
3260 resume_err
= zfs_resume_fs(zfsvfs
, zc
->zc_name
);
3261 error
= error
? error
: resume_err
;
3263 VFS_RELE(zfsvfs
->z_vfs
);
3265 if (dsl_dataset_tryown(ds
, B_FALSE
, FTAG
)) {
3266 error
= dsl_dataset_clone_swap(clone
, ds
, B_TRUE
);
3267 dsl_dataset_disown(ds
, FTAG
);
3275 * Destroy clone (which also closes it).
3277 (void) dsl_dataset_destroy(clone
, FTAG
, B_FALSE
);
3280 strfree(clone_name
);
3282 dsl_dataset_rele(ds
, FTAG
);
3288 * zc_name old name of dataset
3289 * zc_value new name of dataset
3290 * zc_cookie recursive flag (only valid for snapshots)
3295 zfs_ioc_rename(zfs_cmd_t
*zc
)
3297 boolean_t recursive
= zc
->zc_cookie
& 1;
3299 zc
->zc_value
[sizeof (zc
->zc_value
) - 1] = '\0';
3300 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3301 strchr(zc
->zc_value
, '%'))
3305 * Unmount snapshot unless we're doing a recursive rename,
3306 * in which case the dataset code figures out which snapshots
3309 if (!recursive
&& strchr(zc
->zc_name
, '@') != NULL
&&
3310 zc
->zc_objset_type
== DMU_OST_ZFS
) {
3311 int err
= zfs_unmount_snap(zc
->zc_name
, NULL
);
3315 if (zc
->zc_objset_type
== DMU_OST_ZVOL
)
3316 (void) zvol_remove_minor(zc
->zc_name
);
3317 return (dmu_objset_rename(zc
->zc_name
, zc
->zc_value
, recursive
));
3321 zfs_check_settable(const char *dsname
, nvpair_t
*pair
, cred_t
*cr
)
3323 const char *propname
= nvpair_name(pair
);
3324 boolean_t issnap
= (strchr(dsname
, '@') != NULL
);
3325 zfs_prop_t prop
= zfs_name_to_prop(propname
);
3329 if (prop
== ZPROP_INVAL
) {
3330 if (zfs_prop_user(propname
)) {
3331 if (err
= zfs_secpolicy_write_perms(dsname
,
3332 ZFS_DELEG_PERM_USERPROP
, cr
))
3337 if (!issnap
&& zfs_prop_userquota(propname
)) {
3338 const char *perm
= NULL
;
3339 const char *uq_prefix
=
3340 zfs_userquota_prop_prefixes
[ZFS_PROP_USERQUOTA
];
3341 const char *gq_prefix
=
3342 zfs_userquota_prop_prefixes
[ZFS_PROP_GROUPQUOTA
];
3344 if (strncmp(propname
, uq_prefix
,
3345 strlen(uq_prefix
)) == 0) {
3346 perm
= ZFS_DELEG_PERM_USERQUOTA
;
3347 } else if (strncmp(propname
, gq_prefix
,
3348 strlen(gq_prefix
)) == 0) {
3349 perm
= ZFS_DELEG_PERM_GROUPQUOTA
;
3351 /* USERUSED and GROUPUSED are read-only */
3355 if (err
= zfs_secpolicy_write_perms(dsname
, perm
, cr
))
3366 if (nvpair_type(pair
) == DATA_TYPE_NVLIST
) {
3368 * dsl_prop_get_all_impl() returns properties in this
3372 VERIFY(nvpair_value_nvlist(pair
, &attrs
) == 0);
3373 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3378 * Check that this value is valid for this pool version
3381 case ZFS_PROP_COMPRESSION
:
3383 * If the user specified gzip compression, make sure
3384 * the SPA supports it. We ignore any errors here since
3385 * we'll catch them later.
3387 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3388 nvpair_value_uint64(pair
, &intval
) == 0) {
3389 if (intval
>= ZIO_COMPRESS_GZIP_1
&&
3390 intval
<= ZIO_COMPRESS_GZIP_9
&&
3391 zfs_earlier_version(dsname
,
3392 SPA_VERSION_GZIP_COMPRESSION
)) {
3396 if (intval
== ZIO_COMPRESS_ZLE
&&
3397 zfs_earlier_version(dsname
,
3398 SPA_VERSION_ZLE_COMPRESSION
))
3402 * If this is a bootable dataset then
3403 * verify that the compression algorithm
3404 * is supported for booting. We must return
3405 * something other than ENOTSUP since it
3406 * implies a downrev pool version.
3408 if (zfs_is_bootfs(dsname
) &&
3409 !BOOTFS_COMPRESS_VALID(intval
)) {
3415 case ZFS_PROP_COPIES
:
3416 if (zfs_earlier_version(dsname
, SPA_VERSION_DITTO_BLOCKS
))
3420 case ZFS_PROP_DEDUP
:
3421 if (zfs_earlier_version(dsname
, SPA_VERSION_DEDUP
))
3425 case ZFS_PROP_SHARESMB
:
3426 if (zpl_earlier_version(dsname
, ZPL_VERSION_FUID
))
3430 case ZFS_PROP_ACLINHERIT
:
3431 if (nvpair_type(pair
) == DATA_TYPE_UINT64
&&
3432 nvpair_value_uint64(pair
, &intval
) == 0) {
3433 if (intval
== ZFS_ACL_PASSTHROUGH_X
&&
3434 zfs_earlier_version(dsname
,
3435 SPA_VERSION_PASSTHROUGH_X
))
3441 return (zfs_secpolicy_setprop(dsname
, prop
, pair
, CRED()));
3445 * Removes properties from the given props list that fail permission checks
3446 * needed to clear them and to restore them in case of a receive error. For each
3447 * property, make sure we have both set and inherit permissions.
3449 * Returns the first error encountered if any permission checks fail. If the
3450 * caller provides a non-NULL errlist, it also gives the complete list of names
3451 * of all the properties that failed a permission check along with the
3452 * corresponding error numbers. The caller is responsible for freeing the
3455 * If every property checks out successfully, zero is returned and the list
3456 * pointed at by errlist is NULL.
3459 zfs_check_clearable(char *dataset
, nvlist_t
*props
, nvlist_t
**errlist
)
3462 nvpair_t
*pair
, *next_pair
;
3469 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3471 zc
= kmem_alloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
3472 (void) strcpy(zc
->zc_name
, dataset
);
3473 pair
= nvlist_next_nvpair(props
, NULL
);
3474 while (pair
!= NULL
) {
3475 next_pair
= nvlist_next_nvpair(props
, pair
);
3477 (void) strcpy(zc
->zc_value
, nvpair_name(pair
));
3478 if ((err
= zfs_check_settable(dataset
, pair
, CRED())) != 0 ||
3479 (err
= zfs_secpolicy_inherit(zc
, CRED())) != 0) {
3480 VERIFY(nvlist_remove_nvpair(props
, pair
) == 0);
3481 VERIFY(nvlist_add_int32(errors
,
3482 zc
->zc_value
, err
) == 0);
3486 kmem_free(zc
, sizeof (zfs_cmd_t
));
3488 if ((pair
= nvlist_next_nvpair(errors
, NULL
)) == NULL
) {
3489 nvlist_free(errors
);
3492 VERIFY(nvpair_value_int32(pair
, &rv
) == 0);
3495 if (errlist
== NULL
)
3496 nvlist_free(errors
);
3504 propval_equals(nvpair_t
*p1
, nvpair_t
*p2
)
3506 if (nvpair_type(p1
) == DATA_TYPE_NVLIST
) {
3507 /* dsl_prop_get_all_impl() format */
3509 VERIFY(nvpair_value_nvlist(p1
, &attrs
) == 0);
3510 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3514 if (nvpair_type(p2
) == DATA_TYPE_NVLIST
) {
3516 VERIFY(nvpair_value_nvlist(p2
, &attrs
) == 0);
3517 VERIFY(nvlist_lookup_nvpair(attrs
, ZPROP_VALUE
,
3521 if (nvpair_type(p1
) != nvpair_type(p2
))
3524 if (nvpair_type(p1
) == DATA_TYPE_STRING
) {
3525 char *valstr1
, *valstr2
;
3527 VERIFY(nvpair_value_string(p1
, (char **)&valstr1
) == 0);
3528 VERIFY(nvpair_value_string(p2
, (char **)&valstr2
) == 0);
3529 return (strcmp(valstr1
, valstr2
) == 0);
3531 uint64_t intval1
, intval2
;
3533 VERIFY(nvpair_value_uint64(p1
, &intval1
) == 0);
3534 VERIFY(nvpair_value_uint64(p2
, &intval2
) == 0);
3535 return (intval1
== intval2
);
3540 * Remove properties from props if they are not going to change (as determined
3541 * by comparison with origprops). Remove them from origprops as well, since we
3542 * do not need to clear or restore properties that won't change.
3545 props_reduce(nvlist_t
*props
, nvlist_t
*origprops
)
3547 nvpair_t
*pair
, *next_pair
;
3549 if (origprops
== NULL
)
3550 return; /* all props need to be received */
3552 pair
= nvlist_next_nvpair(props
, NULL
);
3553 while (pair
!= NULL
) {
3554 const char *propname
= nvpair_name(pair
);
3557 next_pair
= nvlist_next_nvpair(props
, pair
);
3559 if ((nvlist_lookup_nvpair(origprops
, propname
,
3560 &match
) != 0) || !propval_equals(pair
, match
))
3561 goto next
; /* need to set received value */
3563 /* don't clear the existing received value */
3564 (void) nvlist_remove_nvpair(origprops
, match
);
3565 /* don't bother receiving the property */
3566 (void) nvlist_remove_nvpair(props
, pair
);
3573 static boolean_t zfs_ioc_recv_inject_err
;
3578 * zc_name name of containing filesystem
3579 * zc_nvlist_src{_size} nvlist of properties to apply
3580 * zc_value name of snapshot to create
3581 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3582 * zc_cookie file descriptor to recv from
3583 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3584 * zc_guid force flag
3585 * zc_cleanup_fd cleanup-on-exit file descriptor
3586 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3589 * zc_cookie number of bytes read
3590 * zc_nvlist_dst{_size} error for each unapplied received property
3591 * zc_obj zprop_errflags_t
3592 * zc_action_handle handle for this guid/ds mapping
3595 zfs_ioc_recv(zfs_cmd_t
*zc
)
3599 dmu_recv_cookie_t drc
;
3600 boolean_t force
= (boolean_t
)zc
->zc_guid
;
3603 int props_error
= 0;
3606 nvlist_t
*props
= NULL
; /* sent properties */
3607 nvlist_t
*origprops
= NULL
; /* existing properties */
3608 objset_t
*origin
= NULL
;
3610 char tofs
[ZFS_MAXNAMELEN
];
3611 boolean_t first_recvd_props
= B_FALSE
;
3613 if (dataset_namecheck(zc
->zc_value
, NULL
, NULL
) != 0 ||
3614 strchr(zc
->zc_value
, '@') == NULL
||
3615 strchr(zc
->zc_value
, '%'))
3618 (void) strcpy(tofs
, zc
->zc_value
);
3619 tosnap
= strchr(tofs
, '@');
3622 if (zc
->zc_nvlist_src
!= NULL
&&
3623 (error
= get_nvlist(zc
->zc_nvlist_src
, zc
->zc_nvlist_src_size
,
3624 zc
->zc_iflags
, &props
)) != 0)
3634 VERIFY(nvlist_alloc(&errors
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3636 if (props
&& dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
3637 if ((spa_version(os
->os_spa
) >= SPA_VERSION_RECVD_PROPS
) &&
3638 !dsl_prop_get_hasrecvd(os
)) {
3639 first_recvd_props
= B_TRUE
;
3643 * If new received properties are supplied, they are to
3644 * completely replace the existing received properties, so stash
3645 * away the existing ones.
3647 if (dsl_prop_get_received(os
, &origprops
) == 0) {
3648 nvlist_t
*errlist
= NULL
;
3650 * Don't bother writing a property if its value won't
3651 * change (and avoid the unnecessary security checks).
3653 * The first receive after SPA_VERSION_RECVD_PROPS is a
3654 * special case where we blow away all local properties
3657 if (!first_recvd_props
)
3658 props_reduce(props
, origprops
);
3659 if (zfs_check_clearable(tofs
, origprops
,
3661 (void) nvlist_merge(errors
, errlist
, 0);
3662 nvlist_free(errlist
);
3665 dmu_objset_rele(os
, FTAG
);
3668 if (zc
->zc_string
[0]) {
3669 error
= dmu_objset_hold(zc
->zc_string
, FTAG
, &origin
);
3674 error
= dmu_recv_begin(tofs
, tosnap
, zc
->zc_top_ds
,
3675 &zc
->zc_begin_record
, force
, origin
, &drc
);
3677 dmu_objset_rele(origin
, FTAG
);
3682 * Set properties before we receive the stream so that they are applied
3683 * to the new data. Note that we must call dmu_recv_stream() if
3684 * dmu_recv_begin() succeeds.
3689 if (dmu_objset_from_ds(drc
.drc_logical_ds
, &os
) == 0) {
3690 if (drc
.drc_newfs
) {
3691 if (spa_version(os
->os_spa
) >=
3692 SPA_VERSION_RECVD_PROPS
)
3693 first_recvd_props
= B_TRUE
;
3694 } else if (origprops
!= NULL
) {
3695 if (clear_received_props(os
, tofs
, origprops
,
3696 first_recvd_props
? NULL
: props
) != 0)
3697 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3699 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3701 dsl_prop_set_hasrecvd(os
);
3702 } else if (!drc
.drc_newfs
) {
3703 zc
->zc_obj
|= ZPROP_ERR_NOCLEAR
;
3706 (void) zfs_set_prop_nvlist(tofs
, ZPROP_SRC_RECEIVED
,
3708 (void) nvlist_merge(errors
, errlist
, 0);
3709 nvlist_free(errlist
);
3712 if (fit_error_list(zc
, &errors
) != 0 || put_nvlist(zc
, errors
) != 0) {
3714 * Caller made zc->zc_nvlist_dst less than the minimum expected
3715 * size or supplied an invalid address.
3717 props_error
= EINVAL
;
3721 error
= dmu_recv_stream(&drc
, fp
->f_vnode
, &off
, zc
->zc_cleanup_fd
,
3722 &zc
->zc_action_handle
);
3725 zfsvfs_t
*zfsvfs
= NULL
;
3727 if (getzfsvfs(tofs
, &zfsvfs
) == 0) {
3731 error
= zfs_suspend_fs(zfsvfs
);
3733 * If the suspend fails, then the recv_end will
3734 * likely also fail, and clean up after itself.
3736 end_err
= dmu_recv_end(&drc
);
3738 error
= zfs_resume_fs(zfsvfs
, tofs
);
3739 error
= error
? error
: end_err
;
3740 VFS_RELE(zfsvfs
->z_vfs
);
3742 error
= dmu_recv_end(&drc
);
3746 zc
->zc_cookie
= off
- fp
->f_offset
;
3747 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
3751 if (zfs_ioc_recv_inject_err
) {
3752 zfs_ioc_recv_inject_err
= B_FALSE
;
3757 * On error, restore the original props.
3759 if (error
&& props
) {
3760 if (dmu_objset_hold(tofs
, FTAG
, &os
) == 0) {
3761 if (clear_received_props(os
, tofs
, props
, NULL
) != 0) {
3763 * We failed to clear the received properties.
3764 * Since we may have left a $recvd value on the
3765 * system, we can't clear the $hasrecvd flag.
3767 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3768 } else if (first_recvd_props
) {
3769 dsl_prop_unset_hasrecvd(os
);
3771 dmu_objset_rele(os
, FTAG
);
3772 } else if (!drc
.drc_newfs
) {
3773 /* We failed to clear the received properties. */
3774 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3777 if (origprops
== NULL
&& !drc
.drc_newfs
) {
3778 /* We failed to stash the original properties. */
3779 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3783 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3784 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3785 * explictly if we're restoring local properties cleared in the
3786 * first new-style receive.
3788 if (origprops
!= NULL
&&
3789 zfs_set_prop_nvlist(tofs
, (first_recvd_props
?
3790 ZPROP_SRC_LOCAL
: ZPROP_SRC_RECEIVED
),
3791 origprops
, NULL
) != 0) {
3793 * We stashed the original properties but failed to
3796 zc
->zc_obj
|= ZPROP_ERR_NORESTORE
;
3801 nvlist_free(origprops
);
3802 nvlist_free(errors
);
3806 error
= props_error
;
3813 * zc_name name of snapshot to send
3814 * zc_cookie file descriptor to send stream to
3815 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3816 * zc_sendobj objsetid of snapshot to send
3817 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3818 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
3819 * output size in zc_objset_type.
3824 zfs_ioc_send(zfs_cmd_t
*zc
)
3826 objset_t
*fromsnap
= NULL
;
3831 dsl_dataset_t
*dsfrom
= NULL
;
3834 boolean_t estimate
= (zc
->zc_guid
!= 0);
3836 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
3840 dp
= spa_get_dsl(spa
);
3841 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
3842 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
3843 rw_exit(&dp
->dp_config_rwlock
);
3845 spa_close(spa
, FTAG
);
3849 error
= dmu_objset_from_ds(ds
, &tosnap
);
3851 dsl_dataset_rele(ds
, FTAG
);
3852 spa_close(spa
, FTAG
);
3856 if (zc
->zc_fromobj
!= 0) {
3857 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
3858 error
= dsl_dataset_hold_obj(dp
, zc
->zc_fromobj
, FTAG
, &dsfrom
);
3859 rw_exit(&dp
->dp_config_rwlock
);
3860 spa_close(spa
, FTAG
);
3862 dsl_dataset_rele(ds
, FTAG
);
3865 error
= dmu_objset_from_ds(dsfrom
, &fromsnap
);
3867 dsl_dataset_rele(dsfrom
, FTAG
);
3868 dsl_dataset_rele(ds
, FTAG
);
3872 spa_close(spa
, FTAG
);
3876 error
= dmu_send_estimate(tosnap
, fromsnap
, zc
->zc_obj
,
3877 &zc
->zc_objset_type
);
3879 file_t
*fp
= getf(zc
->zc_cookie
);
3881 dsl_dataset_rele(ds
, FTAG
);
3883 dsl_dataset_rele(dsfrom
, FTAG
);
3888 error
= dmu_sendbackup(tosnap
, fromsnap
, zc
->zc_obj
,
3891 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
3893 releasef(zc
->zc_cookie
);
3896 dsl_dataset_rele(dsfrom
, FTAG
);
3897 dsl_dataset_rele(ds
, FTAG
);
3902 zfs_ioc_inject_fault(zfs_cmd_t
*zc
)
3906 error
= zio_inject_fault(zc
->zc_name
, (int)zc
->zc_guid
, &id
,
3907 &zc
->zc_inject_record
);
3910 zc
->zc_guid
= (uint64_t)id
;
3916 zfs_ioc_clear_fault(zfs_cmd_t
*zc
)
3918 return (zio_clear_fault((int)zc
->zc_guid
));
3922 zfs_ioc_inject_list_next(zfs_cmd_t
*zc
)
3924 int id
= (int)zc
->zc_guid
;
3927 error
= zio_inject_list_next(&id
, zc
->zc_name
, sizeof (zc
->zc_name
),
3928 &zc
->zc_inject_record
);
3936 zfs_ioc_error_log(zfs_cmd_t
*zc
)
3940 size_t count
= (size_t)zc
->zc_nvlist_dst_size
;
3942 if ((error
= spa_open(zc
->zc_name
, &spa
, FTAG
)) != 0)
3945 error
= spa_get_errlog(spa
, (void *)(uintptr_t)zc
->zc_nvlist_dst
,
3948 zc
->zc_nvlist_dst_size
= count
;
3950 zc
->zc_nvlist_dst_size
= spa_get_errlog_size(spa
);
3952 spa_close(spa
, FTAG
);
3958 zfs_ioc_clear(zfs_cmd_t
*zc
)
3965 * On zpool clear we also fix up missing slogs
3967 mutex_enter(&spa_namespace_lock
);
3968 spa
= spa_lookup(zc
->zc_name
);
3970 mutex_exit(&spa_namespace_lock
);
3973 if (spa_get_log_state(spa
) == SPA_LOG_MISSING
) {
3974 /* we need to let spa_open/spa_load clear the chains */
3975 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3977 spa
->spa_last_open_failed
= 0;
3978 mutex_exit(&spa_namespace_lock
);
3980 if (zc
->zc_cookie
& ZPOOL_NO_REWIND
) {
3981 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
3984 nvlist_t
*config
= NULL
;
3986 if (zc
->zc_nvlist_src
== NULL
)
3989 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
3990 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &policy
)) == 0) {
3991 error
= spa_open_rewind(zc
->zc_name
, &spa
, FTAG
,
3993 if (config
!= NULL
) {
3996 if ((err
= put_nvlist(zc
, config
)) != 0)
3998 nvlist_free(config
);
4000 nvlist_free(policy
);
4007 spa_vdev_state_enter(spa
, SCL_NONE
);
4009 if (zc
->zc_guid
== 0) {
4012 vd
= spa_lookup_by_guid(spa
, zc
->zc_guid
, B_TRUE
);
4014 (void) spa_vdev_state_exit(spa
, NULL
, ENODEV
);
4015 spa_close(spa
, FTAG
);
4020 vdev_clear(spa
, vd
);
4022 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4025 * Resume any suspended I/Os.
4027 if (zio_resume(spa
) != 0)
4030 spa_close(spa
, FTAG
);
4036 zfs_ioc_pool_reopen(zfs_cmd_t
*zc
)
4041 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4045 spa_vdev_state_enter(spa
, SCL_NONE
);
4046 vdev_reopen(spa
->spa_root_vdev
);
4047 (void) spa_vdev_state_exit(spa
, NULL
, 0);
4048 spa_close(spa
, FTAG
);
4053 * zc_name name of filesystem
4054 * zc_value name of origin snapshot
4057 * zc_string name of conflicting snapshot, if there is one
4060 zfs_ioc_promote(zfs_cmd_t
*zc
)
4065 * We don't need to unmount *all* the origin fs's snapshots, but
4068 cp
= strchr(zc
->zc_value
, '@');
4071 (void) dmu_objset_find(zc
->zc_value
,
4072 zfs_unmount_snap
, NULL
, DS_FIND_SNAPSHOTS
);
4073 return (dsl_dataset_promote(zc
->zc_name
, zc
->zc_string
));
4077 * Retrieve a single {user|group}{used|quota}@... property.
4080 * zc_name name of filesystem
4081 * zc_objset_type zfs_userquota_prop_t
4082 * zc_value domain name (eg. "S-1-234-567-89")
4083 * zc_guid RID/UID/GID
4086 * zc_cookie property value
4089 zfs_ioc_userspace_one(zfs_cmd_t
*zc
)
4094 if (zc
->zc_objset_type
>= ZFS_NUM_USERQUOTA_PROPS
)
4097 error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4101 error
= zfs_userspace_one(zfsvfs
,
4102 zc
->zc_objset_type
, zc
->zc_value
, zc
->zc_guid
, &zc
->zc_cookie
);
4103 zfsvfs_rele(zfsvfs
, FTAG
);
4110 * zc_name name of filesystem
4111 * zc_cookie zap cursor
4112 * zc_objset_type zfs_userquota_prop_t
4113 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4116 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4117 * zc_cookie zap cursor
4120 zfs_ioc_userspace_many(zfs_cmd_t
*zc
)
4123 int bufsize
= zc
->zc_nvlist_dst_size
;
4128 int error
= zfsvfs_hold(zc
->zc_name
, FTAG
, &zfsvfs
, B_FALSE
);
4132 void *buf
= kmem_alloc(bufsize
, KM_SLEEP
);
4134 error
= zfs_userspace_many(zfsvfs
, zc
->zc_objset_type
, &zc
->zc_cookie
,
4135 buf
, &zc
->zc_nvlist_dst_size
);
4138 error
= xcopyout(buf
,
4139 (void *)(uintptr_t)zc
->zc_nvlist_dst
,
4140 zc
->zc_nvlist_dst_size
);
4142 kmem_free(buf
, bufsize
);
4143 zfsvfs_rele(zfsvfs
, FTAG
);
4150 * zc_name name of filesystem
4156 zfs_ioc_userspace_upgrade(zfs_cmd_t
*zc
)
4162 if (getzfsvfs(zc
->zc_name
, &zfsvfs
) == 0) {
4163 if (!dmu_objset_userused_enabled(zfsvfs
->z_os
)) {
4165 * If userused is not enabled, it may be because the
4166 * objset needs to be closed & reopened (to grow the
4167 * objset_phys_t). Suspend/resume the fs will do that.
4169 error
= zfs_suspend_fs(zfsvfs
);
4171 error
= zfs_resume_fs(zfsvfs
, zc
->zc_name
);
4174 error
= dmu_objset_userspace_upgrade(zfsvfs
->z_os
);
4175 VFS_RELE(zfsvfs
->z_vfs
);
4177 /* XXX kind of reading contents without owning */
4178 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4182 error
= dmu_objset_userspace_upgrade(os
);
4183 dmu_objset_rele(os
, FTAG
);
4190 * We don't want to have a hard dependency
4191 * against some special symbols in sharefs
4192 * nfs, and smbsrv. Determine them if needed when
4193 * the first file system is shared.
4194 * Neither sharefs, nfs or smbsrv are unloadable modules.
4196 int (*znfsexport_fs
)(void *arg
);
4197 int (*zshare_fs
)(enum sharefs_sys_op
, share_t
*, uint32_t);
4198 int (*zsmbexport_fs
)(void *arg
, boolean_t add_share
);
4200 int zfs_nfsshare_inited
;
4201 int zfs_smbshare_inited
;
4203 ddi_modhandle_t nfs_mod
;
4204 ddi_modhandle_t sharefs_mod
;
4205 ddi_modhandle_t smbsrv_mod
;
4206 kmutex_t zfs_share_lock
;
4213 ASSERT(MUTEX_HELD(&zfs_share_lock
));
4214 /* Both NFS and SMB shares also require sharetab support. */
4215 if (sharefs_mod
== NULL
&& ((sharefs_mod
=
4216 ddi_modopen("fs/sharefs",
4217 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4220 if (zshare_fs
== NULL
&& ((zshare_fs
=
4221 (int (*)(enum sharefs_sys_op
, share_t
*, uint32_t))
4222 ddi_modsym(sharefs_mod
, "sharefs_impl", &error
)) == NULL
)) {
4229 zfs_ioc_share(zfs_cmd_t
*zc
)
4234 switch (zc
->zc_share
.z_sharetype
) {
4236 case ZFS_UNSHARE_NFS
:
4237 if (zfs_nfsshare_inited
== 0) {
4238 mutex_enter(&zfs_share_lock
);
4239 if (nfs_mod
== NULL
&& ((nfs_mod
= ddi_modopen("fs/nfs",
4240 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4241 mutex_exit(&zfs_share_lock
);
4244 if (znfsexport_fs
== NULL
&&
4245 ((znfsexport_fs
= (int (*)(void *))
4247 "nfs_export", &error
)) == NULL
)) {
4248 mutex_exit(&zfs_share_lock
);
4251 error
= zfs_init_sharefs();
4253 mutex_exit(&zfs_share_lock
);
4256 zfs_nfsshare_inited
= 1;
4257 mutex_exit(&zfs_share_lock
);
4261 case ZFS_UNSHARE_SMB
:
4262 if (zfs_smbshare_inited
== 0) {
4263 mutex_enter(&zfs_share_lock
);
4264 if (smbsrv_mod
== NULL
&& ((smbsrv_mod
=
4265 ddi_modopen("drv/smbsrv",
4266 KRTLD_MODE_FIRST
, &error
)) == NULL
)) {
4267 mutex_exit(&zfs_share_lock
);
4270 if (zsmbexport_fs
== NULL
&& ((zsmbexport_fs
=
4271 (int (*)(void *, boolean_t
))ddi_modsym(smbsrv_mod
,
4272 "smb_server_share", &error
)) == NULL
)) {
4273 mutex_exit(&zfs_share_lock
);
4276 error
= zfs_init_sharefs();
4278 mutex_exit(&zfs_share_lock
);
4281 zfs_smbshare_inited
= 1;
4282 mutex_exit(&zfs_share_lock
);
4289 switch (zc
->zc_share
.z_sharetype
) {
4291 case ZFS_UNSHARE_NFS
:
4293 znfsexport_fs((void *)
4294 (uintptr_t)zc
->zc_share
.z_exportdata
))
4298 case ZFS_UNSHARE_SMB
:
4299 if (error
= zsmbexport_fs((void *)
4300 (uintptr_t)zc
->zc_share
.z_exportdata
,
4301 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
?
4308 opcode
= (zc
->zc_share
.z_sharetype
== ZFS_SHARE_NFS
||
4309 zc
->zc_share
.z_sharetype
== ZFS_SHARE_SMB
) ?
4310 SHAREFS_ADD
: SHAREFS_REMOVE
;
4313 * Add or remove share from sharetab
4315 error
= zshare_fs(opcode
,
4316 (void *)(uintptr_t)zc
->zc_share
.z_sharedata
,
4317 zc
->zc_share
.z_sharemax
);
4323 ace_t full_access
[] = {
4324 {(uid_t
)-1, ACE_ALL_PERMS
, ACE_EVERYONE
, 0}
4329 * zc_name name of containing filesystem
4330 * zc_obj object # beyond which we want next in-use object #
4333 * zc_obj next in-use object #
4336 zfs_ioc_next_obj(zfs_cmd_t
*zc
)
4338 objset_t
*os
= NULL
;
4341 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &os
);
4345 error
= dmu_object_next(os
, &zc
->zc_obj
, B_FALSE
,
4346 os
->os_dsl_dataset
->ds_phys
->ds_prev_snap_txg
);
4348 dmu_objset_rele(os
, FTAG
);
4354 * zc_name name of filesystem
4355 * zc_value prefix name for snapshot
4356 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4361 zfs_ioc_tmp_snapshot(zfs_cmd_t
*zc
)
4366 snap_name
= kmem_asprintf("%s-%016llx", zc
->zc_value
,
4367 (u_longlong_t
)ddi_get_lbolt64());
4369 if (strlen(snap_name
) >= MAXNAMELEN
) {
4374 error
= dmu_objset_snapshot(zc
->zc_name
, snap_name
, snap_name
,
4375 NULL
, B_FALSE
, B_TRUE
, zc
->zc_cleanup_fd
);
4381 (void) strcpy(zc
->zc_value
, snap_name
);
4388 * zc_name name of "to" snapshot
4389 * zc_value name of "from" snapshot
4390 * zc_cookie file descriptor to write diff data on
4393 * dmu_diff_record_t's to the file descriptor
4396 zfs_ioc_diff(zfs_cmd_t
*zc
)
4404 error
= dmu_objset_hold(zc
->zc_name
, FTAG
, &tosnap
);
4408 error
= dmu_objset_hold(zc
->zc_value
, FTAG
, &fromsnap
);
4410 dmu_objset_rele(tosnap
, FTAG
);
4414 fp
= getf(zc
->zc_cookie
);
4416 dmu_objset_rele(fromsnap
, FTAG
);
4417 dmu_objset_rele(tosnap
, FTAG
);
4423 error
= dmu_diff(tosnap
, fromsnap
, fp
->f_vnode
, &off
);
4425 if (VOP_SEEK(fp
->f_vnode
, fp
->f_offset
, &off
, NULL
) == 0)
4427 releasef(zc
->zc_cookie
);
4429 dmu_objset_rele(fromsnap
, FTAG
);
4430 dmu_objset_rele(tosnap
, FTAG
);
4435 * Remove all ACL files in shares dir
4438 zfs_smb_acl_purge(znode_t
*dzp
)
4441 zap_attribute_t zap
;
4442 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
4445 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
4446 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
4447 zap_cursor_advance(&zc
)) {
4448 if ((error
= VOP_REMOVE(ZTOV(dzp
), zap
.za_name
, kcred
,
4452 zap_cursor_fini(&zc
);
4457 zfs_ioc_smb_acl(zfs_cmd_t
*zc
)
4461 vnode_t
*resourcevp
= NULL
;
4470 if ((error
= lookupname(zc
->zc_value
, UIO_SYSSPACE
,
4471 NO_FOLLOW
, NULL
, &vp
)) != 0)
4474 /* Now make sure mntpnt and dataset are ZFS */
4476 if (vp
->v_vfsp
->vfs_fstype
!= zfsfstype
||
4477 (strcmp((char *)refstr_value(vp
->v_vfsp
->vfs_resource
),
4478 zc
->zc_name
) != 0)) {
4484 zfsvfs
= dzp
->z_zfsvfs
;
4488 * Create share dir if its missing.
4490 mutex_enter(&zfsvfs
->z_lock
);
4491 if (zfsvfs
->z_shares_dir
== 0) {
4494 tx
= dmu_tx_create(zfsvfs
->z_os
);
4495 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, TRUE
,
4497 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
4498 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4502 error
= zfs_create_share_dir(zfsvfs
, tx
);
4506 mutex_exit(&zfsvfs
->z_lock
);
4512 mutex_exit(&zfsvfs
->z_lock
);
4514 ASSERT(zfsvfs
->z_shares_dir
);
4515 if ((error
= zfs_zget(zfsvfs
, zfsvfs
->z_shares_dir
, &sharedir
)) != 0) {
4521 switch (zc
->zc_cookie
) {
4522 case ZFS_SMB_ACL_ADD
:
4523 vattr
.va_mask
= AT_MODE
|AT_UID
|AT_GID
|AT_TYPE
;
4524 vattr
.va_type
= VREG
;
4525 vattr
.va_mode
= S_IFREG
|0777;
4529 vsec
.vsa_mask
= VSA_ACE
;
4530 vsec
.vsa_aclentp
= &full_access
;
4531 vsec
.vsa_aclentsz
= sizeof (full_access
);
4532 vsec
.vsa_aclcnt
= 1;
4534 error
= VOP_CREATE(ZTOV(sharedir
), zc
->zc_string
,
4535 &vattr
, EXCL
, 0, &resourcevp
, kcred
, 0, NULL
, &vsec
);
4537 VN_RELE(resourcevp
);
4540 case ZFS_SMB_ACL_REMOVE
:
4541 error
= VOP_REMOVE(ZTOV(sharedir
), zc
->zc_string
, kcred
,
4545 case ZFS_SMB_ACL_RENAME
:
4546 if ((error
= get_nvlist(zc
->zc_nvlist_src
,
4547 zc
->zc_nvlist_src_size
, zc
->zc_iflags
, &nvlist
)) != 0) {
4552 if (nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_SRC
, &src
) ||
4553 nvlist_lookup_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4556 VN_RELE(ZTOV(sharedir
));
4558 nvlist_free(nvlist
);
4561 error
= VOP_RENAME(ZTOV(sharedir
), src
, ZTOV(sharedir
), target
,
4563 nvlist_free(nvlist
);
4566 case ZFS_SMB_ACL_PURGE
:
4567 error
= zfs_smb_acl_purge(sharedir
);
4576 VN_RELE(ZTOV(sharedir
));
4585 * zc_name name of filesystem
4586 * zc_value short name of snap
4587 * zc_string user-supplied tag for this hold
4588 * zc_cookie recursive flag
4589 * zc_temphold set if hold is temporary
4590 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4591 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4592 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4597 zfs_ioc_hold(zfs_cmd_t
*zc
)
4599 boolean_t recursive
= zc
->zc_cookie
;
4606 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
4609 if (zc
->zc_sendobj
== 0) {
4610 return (dsl_dataset_user_hold(zc
->zc_name
, zc
->zc_value
,
4611 zc
->zc_string
, recursive
, zc
->zc_temphold
,
4612 zc
->zc_cleanup_fd
));
4618 error
= spa_open(zc
->zc_name
, &spa
, FTAG
);
4622 dp
= spa_get_dsl(spa
);
4623 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
4624 error
= dsl_dataset_hold_obj(dp
, zc
->zc_sendobj
, FTAG
, &ds
);
4625 rw_exit(&dp
->dp_config_rwlock
);
4626 spa_close(spa
, FTAG
);
4631 * Until we have a hold on this snapshot, it's possible that
4632 * zc_sendobj could've been destroyed and reused as part
4633 * of a later txg. Make sure we're looking at the right object.
4635 if (zc
->zc_createtxg
!= ds
->ds_phys
->ds_creation_txg
) {
4636 dsl_dataset_rele(ds
, FTAG
);
4640 if (zc
->zc_cleanup_fd
!= -1 && zc
->zc_temphold
) {
4641 error
= zfs_onexit_fd_hold(zc
->zc_cleanup_fd
, &minor
);
4643 dsl_dataset_rele(ds
, FTAG
);
4648 error
= dsl_dataset_user_hold_for_send(ds
, zc
->zc_string
,
4652 dsl_register_onexit_hold_cleanup(ds
, zc
->zc_string
,
4655 zfs_onexit_fd_rele(zc
->zc_cleanup_fd
);
4657 dsl_dataset_rele(ds
, FTAG
);
4664 * zc_name name of dataset from which we're releasing a user hold
4665 * zc_value short name of snap
4666 * zc_string user-supplied tag for this hold
4667 * zc_cookie recursive flag
4672 zfs_ioc_release(zfs_cmd_t
*zc
)
4674 boolean_t recursive
= zc
->zc_cookie
;
4676 if (snapshot_namecheck(zc
->zc_value
, NULL
, NULL
) != 0)
4679 return (dsl_dataset_user_release(zc
->zc_name
, zc
->zc_value
,
4680 zc
->zc_string
, recursive
));
4685 * zc_name name of filesystem
4688 * zc_nvlist_src{_size} nvlist of snapshot holds
4691 zfs_ioc_get_holds(zfs_cmd_t
*zc
)
4696 if ((error
= dsl_dataset_get_holds(zc
->zc_name
, &nvp
)) == 0) {
4697 error
= put_nvlist(zc
, nvp
);
4706 * zc_name name of new filesystem or snapshot
4707 * zc_value full name of old snapshot
4710 * zc_cookie space in bytes
4711 * zc_objset_type compressed space in bytes
4712 * zc_perm_action uncompressed space in bytes
4715 zfs_ioc_space_written(zfs_cmd_t
*zc
)
4718 dsl_dataset_t
*new, *old
;
4720 error
= dsl_dataset_hold(zc
->zc_name
, FTAG
, &new);
4723 error
= dsl_dataset_hold(zc
->zc_value
, FTAG
, &old
);
4725 dsl_dataset_rele(new, FTAG
);
4729 error
= dsl_dataset_space_written(old
, new, &zc
->zc_cookie
,
4730 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
4731 dsl_dataset_rele(old
, FTAG
);
4732 dsl_dataset_rele(new, FTAG
);
4738 * zc_name full name of last snapshot
4739 * zc_value full name of first snapshot
4742 * zc_cookie space in bytes
4743 * zc_objset_type compressed space in bytes
4744 * zc_perm_action uncompressed space in bytes
4747 zfs_ioc_space_snaps(zfs_cmd_t
*zc
)
4750 dsl_dataset_t
*new, *old
;
4752 error
= dsl_dataset_hold(zc
->zc_name
, FTAG
, &new);
4755 error
= dsl_dataset_hold(zc
->zc_value
, FTAG
, &old
);
4757 dsl_dataset_rele(new, FTAG
);
4761 error
= dsl_dataset_space_wouldfree(old
, new, &zc
->zc_cookie
,
4762 &zc
->zc_objset_type
, &zc
->zc_perm_action
);
4763 dsl_dataset_rele(old
, FTAG
);
4764 dsl_dataset_rele(new, FTAG
);
4769 * pool create, destroy, and export don't log the history as part of
4770 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4771 * do the logging of those commands.
4773 static zfs_ioc_vec_t zfs_ioc_vec
[] = {
4774 { zfs_ioc_pool_create
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4776 { zfs_ioc_pool_destroy
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4778 { zfs_ioc_pool_import
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4780 { zfs_ioc_pool_export
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4782 { zfs_ioc_pool_configs
, zfs_secpolicy_none
, NO_NAME
, B_FALSE
,
4784 { zfs_ioc_pool_stats
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
,
4786 { zfs_ioc_pool_tryimport
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4788 { zfs_ioc_pool_scan
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4789 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4790 { zfs_ioc_pool_freeze
, zfs_secpolicy_config
, NO_NAME
, B_FALSE
,
4791 POOL_CHECK_READONLY
},
4792 { zfs_ioc_pool_upgrade
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4793 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4794 { zfs_ioc_pool_get_history
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4796 { zfs_ioc_vdev_add
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4797 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4798 { zfs_ioc_vdev_remove
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4799 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4800 { zfs_ioc_vdev_set_state
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4801 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4802 { zfs_ioc_vdev_attach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4803 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4804 { zfs_ioc_vdev_detach
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4805 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4806 { zfs_ioc_vdev_setpath
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4807 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4808 { zfs_ioc_vdev_setfru
, zfs_secpolicy_config
, POOL_NAME
, B_FALSE
,
4809 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4810 { zfs_ioc_objset_stats
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4811 POOL_CHECK_SUSPENDED
},
4812 { zfs_ioc_objset_zplprops
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4814 { zfs_ioc_dataset_list_next
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4815 POOL_CHECK_SUSPENDED
},
4816 { zfs_ioc_snapshot_list_next
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4817 POOL_CHECK_SUSPENDED
},
4818 { zfs_ioc_set_prop
, zfs_secpolicy_none
, DATASET_NAME
, B_TRUE
,
4819 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4820 { zfs_ioc_create
, zfs_secpolicy_create
, DATASET_NAME
, B_TRUE
,
4821 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4822 { zfs_ioc_destroy
, zfs_secpolicy_destroy
, DATASET_NAME
, B_TRUE
,
4823 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4824 { zfs_ioc_rollback
, zfs_secpolicy_rollback
, DATASET_NAME
, B_TRUE
,
4825 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4826 { zfs_ioc_rename
, zfs_secpolicy_rename
, DATASET_NAME
, B_TRUE
,
4827 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4828 { zfs_ioc_recv
, zfs_secpolicy_receive
, DATASET_NAME
, B_TRUE
,
4829 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4830 { zfs_ioc_send
, zfs_secpolicy_send
, DATASET_NAME
, B_FALSE
,
4832 { zfs_ioc_inject_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4834 { zfs_ioc_clear_fault
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4836 { zfs_ioc_inject_list_next
, zfs_secpolicy_inject
, NO_NAME
, B_FALSE
,
4838 { zfs_ioc_error_log
, zfs_secpolicy_inject
, POOL_NAME
, B_FALSE
,
4840 { zfs_ioc_clear
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4842 { zfs_ioc_promote
, zfs_secpolicy_promote
, DATASET_NAME
, B_TRUE
,
4843 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4844 { zfs_ioc_snapshot
, zfs_secpolicy_snapshot
, DATASET_NAME
, B_TRUE
,
4845 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4846 { zfs_ioc_dsobj_to_dsname
, zfs_secpolicy_diff
, POOL_NAME
, B_FALSE
,
4848 { zfs_ioc_obj_to_path
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4849 POOL_CHECK_SUSPENDED
},
4850 { zfs_ioc_pool_set_props
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4851 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4852 { zfs_ioc_pool_get_props
, zfs_secpolicy_read
, POOL_NAME
, B_FALSE
,
4854 { zfs_ioc_set_fsacl
, zfs_secpolicy_fsacl
, DATASET_NAME
, B_TRUE
,
4855 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4856 { zfs_ioc_get_fsacl
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4858 { zfs_ioc_share
, zfs_secpolicy_share
, DATASET_NAME
, B_FALSE
,
4860 { zfs_ioc_inherit_prop
, zfs_secpolicy_inherit
, DATASET_NAME
, B_TRUE
,
4861 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4862 { zfs_ioc_smb_acl
, zfs_secpolicy_smb_acl
, DATASET_NAME
, B_FALSE
,
4864 { zfs_ioc_userspace_one
, zfs_secpolicy_userspace_one
, DATASET_NAME
,
4865 B_FALSE
, POOL_CHECK_NONE
},
4866 { zfs_ioc_userspace_many
, zfs_secpolicy_userspace_many
, DATASET_NAME
,
4867 B_FALSE
, POOL_CHECK_NONE
},
4868 { zfs_ioc_userspace_upgrade
, zfs_secpolicy_userspace_upgrade
,
4869 DATASET_NAME
, B_FALSE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4870 { zfs_ioc_hold
, zfs_secpolicy_hold
, DATASET_NAME
, B_TRUE
,
4871 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4872 { zfs_ioc_release
, zfs_secpolicy_release
, DATASET_NAME
, B_TRUE
,
4873 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4874 { zfs_ioc_get_holds
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4875 POOL_CHECK_SUSPENDED
},
4876 { zfs_ioc_objset_recvd_props
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4878 { zfs_ioc_vdev_split
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4879 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4880 { zfs_ioc_next_obj
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4882 { zfs_ioc_diff
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4884 { zfs_ioc_tmp_snapshot
, zfs_secpolicy_tmp_snapshot
, DATASET_NAME
,
4885 B_FALSE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4886 { zfs_ioc_obj_to_stats
, zfs_secpolicy_diff
, DATASET_NAME
, B_FALSE
,
4887 POOL_CHECK_SUSPENDED
},
4888 { zfs_ioc_space_written
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4889 POOL_CHECK_SUSPENDED
},
4890 { zfs_ioc_space_snaps
, zfs_secpolicy_read
, DATASET_NAME
, B_FALSE
,
4891 POOL_CHECK_SUSPENDED
},
4892 { zfs_ioc_destroy_snaps_nvl
, zfs_secpolicy_destroy_recursive
,
4893 DATASET_NAME
, B_TRUE
, POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4894 { zfs_ioc_pool_reguid
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4895 POOL_CHECK_SUSPENDED
| POOL_CHECK_READONLY
},
4896 { zfs_ioc_pool_reopen
, zfs_secpolicy_config
, POOL_NAME
, B_TRUE
,
4897 POOL_CHECK_SUSPENDED
},
4901 pool_status_check(const char *name
, zfs_ioc_namecheck_t type
,
4902 zfs_ioc_poolcheck_t check
)
4907 ASSERT(type
== POOL_NAME
|| type
== DATASET_NAME
);
4909 if (check
& POOL_CHECK_NONE
)
4912 error
= spa_open(name
, &spa
, FTAG
);
4914 if ((check
& POOL_CHECK_SUSPENDED
) && spa_suspended(spa
))
4916 else if ((check
& POOL_CHECK_READONLY
) && !spa_writeable(spa
))
4918 spa_close(spa
, FTAG
);
4924 * Find a free minor number.
4927 zfsdev_minor_alloc(void)
4929 static minor_t last_minor
;
4932 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4934 for (m
= last_minor
+ 1; m
!= last_minor
; m
++) {
4935 if (m
> ZFSDEV_MAX_MINOR
)
4937 if (ddi_get_soft_state(zfsdev_state
, m
) == NULL
) {
4947 zfs_ctldev_init(dev_t
*devp
)
4950 zfs_soft_state_t
*zs
;
4952 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4953 ASSERT(getminor(*devp
) == 0);
4955 minor
= zfsdev_minor_alloc();
4959 if (ddi_soft_state_zalloc(zfsdev_state
, minor
) != DDI_SUCCESS
)
4962 *devp
= makedevice(getemajor(*devp
), minor
);
4964 zs
= ddi_get_soft_state(zfsdev_state
, minor
);
4965 zs
->zss_type
= ZSST_CTLDEV
;
4966 zfs_onexit_init((zfs_onexit_t
**)&zs
->zss_data
);
4972 zfs_ctldev_destroy(zfs_onexit_t
*zo
, minor_t minor
)
4974 ASSERT(MUTEX_HELD(&zfsdev_state_lock
));
4976 zfs_onexit_destroy(zo
);
4977 ddi_soft_state_free(zfsdev_state
, minor
);
4981 zfsdev_get_soft_state(minor_t minor
, enum zfs_soft_state_type which
)
4983 zfs_soft_state_t
*zp
;
4985 zp
= ddi_get_soft_state(zfsdev_state
, minor
);
4986 if (zp
== NULL
|| zp
->zss_type
!= which
)
4989 return (zp
->zss_data
);
4993 zfsdev_open(dev_t
*devp
, int flag
, int otyp
, cred_t
*cr
)
4997 if (getminor(*devp
) != 0)
4998 return (zvol_open(devp
, flag
, otyp
, cr
));
5000 /* This is the control device. Allocate a new minor if requested. */
5002 mutex_enter(&zfsdev_state_lock
);
5003 error
= zfs_ctldev_init(devp
);
5004 mutex_exit(&zfsdev_state_lock
);
5011 zfsdev_close(dev_t dev
, int flag
, int otyp
, cred_t
*cr
)
5014 minor_t minor
= getminor(dev
);
5019 mutex_enter(&zfsdev_state_lock
);
5020 zo
= zfsdev_get_soft_state(minor
, ZSST_CTLDEV
);
5022 mutex_exit(&zfsdev_state_lock
);
5023 return (zvol_close(dev
, flag
, otyp
, cr
));
5025 zfs_ctldev_destroy(zo
, minor
);
5026 mutex_exit(&zfsdev_state_lock
);
5032 zfsdev_ioctl(dev_t dev
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
)
5037 minor_t minor
= getminor(dev
);
5040 zfsdev_get_soft_state(minor
, ZSST_CTLDEV
) == NULL
)
5041 return (zvol_ioctl(dev
, cmd
, arg
, flag
, cr
, rvalp
));
5043 vec
= cmd
- ZFS_IOC
;
5044 ASSERT3U(getmajor(dev
), ==, ddi_driver_major(zfs_dip
));
5046 if (vec
>= sizeof (zfs_ioc_vec
) / sizeof (zfs_ioc_vec
[0]))
5049 zc
= kmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
5051 error
= ddi_copyin((void *)arg
, zc
, sizeof (zfs_cmd_t
), flag
);
5055 if ((error
== 0) && !(flag
& FKIOCTL
))
5056 error
= zfs_ioc_vec
[vec
].zvec_secpolicy(zc
, cr
);
5059 * Ensure that all pool/dataset names are valid before we pass down to
5063 zc
->zc_name
[sizeof (zc
->zc_name
) - 1] = '\0';
5064 zc
->zc_iflags
= flag
& FKIOCTL
;
5065 switch (zfs_ioc_vec
[vec
].zvec_namecheck
) {
5067 if (pool_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5069 error
= pool_status_check(zc
->zc_name
,
5070 zfs_ioc_vec
[vec
].zvec_namecheck
,
5071 zfs_ioc_vec
[vec
].zvec_pool_check
);
5075 if (dataset_namecheck(zc
->zc_name
, NULL
, NULL
) != 0)
5077 error
= pool_status_check(zc
->zc_name
,
5078 zfs_ioc_vec
[vec
].zvec_namecheck
,
5079 zfs_ioc_vec
[vec
].zvec_pool_check
);
5088 error
= zfs_ioc_vec
[vec
].zvec_func(zc
);
5090 rc
= ddi_copyout(zc
, (void *)arg
, sizeof (zfs_cmd_t
), flag
);
5094 if (zfs_ioc_vec
[vec
].zvec_his_log
)
5095 zfs_log_history(zc
);
5098 kmem_free(zc
, sizeof (zfs_cmd_t
));
5103 zfs_attach(dev_info_t
*dip
, ddi_attach_cmd_t cmd
)
5105 if (cmd
!= DDI_ATTACH
)
5106 return (DDI_FAILURE
);
5108 if (ddi_create_minor_node(dip
, "zfs", S_IFCHR
, 0,
5109 DDI_PSEUDO
, 0) == DDI_FAILURE
)
5110 return (DDI_FAILURE
);
5114 ddi_report_dev(dip
);
5116 return (DDI_SUCCESS
);
5120 zfs_detach(dev_info_t
*dip
, ddi_detach_cmd_t cmd
)
5122 if (spa_busy() || zfs_busy() || zvol_busy())
5123 return (DDI_FAILURE
);
5125 if (cmd
!= DDI_DETACH
)
5126 return (DDI_FAILURE
);
5130 ddi_prop_remove_all(dip
);
5131 ddi_remove_minor_node(dip
, NULL
);
5133 return (DDI_SUCCESS
);
5138 zfs_info(dev_info_t
*dip
, ddi_info_cmd_t infocmd
, void *arg
, void **result
)
5141 case DDI_INFO_DEVT2DEVINFO
:
5143 return (DDI_SUCCESS
);
5145 case DDI_INFO_DEVT2INSTANCE
:
5146 *result
= (void *)0;
5147 return (DDI_SUCCESS
);
5150 return (DDI_FAILURE
);
5154 * OK, so this is a little weird.
5156 * /dev/zfs is the control node, i.e. minor 0.
5157 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
5159 * /dev/zfs has basically nothing to do except serve up ioctls,
5160 * so most of the standard driver entry points are in zvol.c.
5162 static struct cb_ops zfs_cb_ops
= {
5163 zfsdev_open
, /* open */
5164 zfsdev_close
, /* close */
5165 zvol_strategy
, /* strategy */
5167 zvol_dump
, /* dump */
5168 zvol_read
, /* read */
5169 zvol_write
, /* write */
5170 zfsdev_ioctl
, /* ioctl */
5174 nochpoll
, /* poll */
5175 ddi_prop_op
, /* prop_op */
5176 NULL
, /* streamtab */
5177 D_NEW
| D_MP
| D_64BIT
, /* Driver compatibility flag */
5178 CB_REV
, /* version */
5179 nodev
, /* async read */
5180 nodev
, /* async write */
5183 static struct dev_ops zfs_dev_ops
= {
5184 DEVO_REV
, /* version */
5186 zfs_info
, /* info */
5187 nulldev
, /* identify */
5188 nulldev
, /* probe */
5189 zfs_attach
, /* attach */
5190 zfs_detach
, /* detach */
5192 &zfs_cb_ops
, /* driver operations */
5193 NULL
, /* no bus operations */
5195 ddi_quiesce_not_needed
, /* quiesce */
5198 static struct modldrv zfs_modldrv
= {
5204 static struct modlinkage modlinkage
= {
5206 (void *)&zfs_modlfs
,
5207 (void *)&zfs_modldrv
,
5212 uint_t zfs_fsyncer_key
;
5213 extern uint_t rrw_tsd_key
;
5220 spa_init(FREAD
| FWRITE
);
5224 if ((error
= mod_install(&modlinkage
)) != 0) {
5231 tsd_create(&zfs_fsyncer_key
, NULL
);
5232 tsd_create(&rrw_tsd_key
, NULL
);
5234 error
= ldi_ident_from_mod(&modlinkage
, &zfs_li
);
5236 mutex_init(&zfs_share_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5246 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled
)
5249 if ((error
= mod_remove(&modlinkage
)) != 0)
5255 if (zfs_nfsshare_inited
)
5256 (void) ddi_modclose(nfs_mod
);
5257 if (zfs_smbshare_inited
)
5258 (void) ddi_modclose(smbsrv_mod
);
5259 if (zfs_nfsshare_inited
|| zfs_smbshare_inited
)
5260 (void) ddi_modclose(sharefs_mod
);
5262 tsd_destroy(&zfs_fsyncer_key
);
5263 ldi_ident_release(zfs_li
);
5265 mutex_destroy(&zfs_share_lock
);
5271 _info(struct modinfo
*modinfop
)
5273 return (mod_info(&modlinkage
, modinfop
));