xref: /titanic_52/usr/src/common/zfs/zfs_comutil.c (revision 3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5)
1b7b97454Sperrin /*
2b7b97454Sperrin  * CDDL HEADER START
3b7b97454Sperrin  *
4b7b97454Sperrin  * The contents of this file are subject to the terms of the
5b7b97454Sperrin  * Common Development and Distribution License (the "License").
6b7b97454Sperrin  * You may not use this file except in compliance with the License.
7b7b97454Sperrin  *
8b7b97454Sperrin  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b7b97454Sperrin  * or http://www.opensolaris.org/os/licensing.
10b7b97454Sperrin  * See the License for the specific language governing permissions
11b7b97454Sperrin  * and limitations under the License.
12b7b97454Sperrin  *
13b7b97454Sperrin  * When distributing Covered Code, include this CDDL HEADER in each
14b7b97454Sperrin  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b7b97454Sperrin  * If applicable, add the following below this CDDL HEADER, with the
16b7b97454Sperrin  * fields enclosed by brackets "[]" replaced with your own identifying
17b7b97454Sperrin  * information: Portions Copyright [yyyy] [name of copyright owner]
18b7b97454Sperrin  *
19b7b97454Sperrin  * CDDL HEADER END
20b7b97454Sperrin  */
21b7b97454Sperrin /*
22*3f9d6ad7SLin Ling  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23b7b97454Sperrin  */
24b7b97454Sperrin 
25b7b97454Sperrin /*
26b7b97454Sperrin  * This file is intended for functions that ought to be common between user
27b7b97454Sperrin  * land (libzfs) and the kernel. When many common routines need to be shared
28b7b97454Sperrin  * then a separate file should to be created.
29b7b97454Sperrin  */
30b7b97454Sperrin 
31b7b97454Sperrin #if defined(_KERNEL)
32b7b97454Sperrin #include <sys/systm.h>
33468c413aSTim Haley #else
34468c413aSTim Haley #include <string.h>
35b7b97454Sperrin #endif
36b7b97454Sperrin 
37b7b97454Sperrin #include <sys/types.h>
38b7b97454Sperrin #include <sys/fs/zfs.h>
39468c413aSTim Haley #include <sys/int_limits.h>
40b7b97454Sperrin #include <sys/nvpair.h>
410a586ceaSMark Shellenbaum #include "zfs_comutil.h"
42b7b97454Sperrin 
43b7b97454Sperrin /*
44b7b97454Sperrin  * Are there allocatable vdevs?
45b7b97454Sperrin  */
46b7b97454Sperrin boolean_t
47b7b97454Sperrin zfs_allocatable_devs(nvlist_t *nv)
48b7b97454Sperrin {
49b7b97454Sperrin 	uint64_t is_log;
50b7b97454Sperrin 	uint_t c;
51b7b97454Sperrin 	nvlist_t **child;
52b7b97454Sperrin 	uint_t children;
53b7b97454Sperrin 
54b7b97454Sperrin 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
55b7b97454Sperrin 	    &child, &children) != 0) {
56b7b97454Sperrin 		return (B_FALSE);
57b7b97454Sperrin 	}
58b7b97454Sperrin 	for (c = 0; c < children; c++) {
59b7b97454Sperrin 		is_log = 0;
60b7b97454Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
61b7b97454Sperrin 		    &is_log);
62b7b97454Sperrin 		if (!is_log)
63b7b97454Sperrin 			return (B_TRUE);
64b7b97454Sperrin 	}
65b7b97454Sperrin 	return (B_FALSE);
66b7b97454Sperrin }
67468c413aSTim Haley 
68468c413aSTim Haley void
69468c413aSTim Haley zpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp)
70468c413aSTim Haley {
71468c413aSTim Haley 	nvlist_t *policy;
72468c413aSTim Haley 	nvpair_t *elem;
73468c413aSTim Haley 	char *nm;
74468c413aSTim Haley 
75468c413aSTim Haley 	/* Defaults */
76468c413aSTim Haley 	zrpp->zrp_request = ZPOOL_NO_REWIND;
77468c413aSTim Haley 	zrpp->zrp_maxmeta = 0;
78c8ee1847SVictor Latushkin 	zrpp->zrp_maxdata = UINT64_MAX;
79468c413aSTim Haley 	zrpp->zrp_txg = UINT64_MAX;
80468c413aSTim Haley 
81468c413aSTim Haley 	if (nvl == NULL)
82468c413aSTim Haley 		return;
83468c413aSTim Haley 
84468c413aSTim Haley 	elem = NULL;
85468c413aSTim Haley 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
86468c413aSTim Haley 		nm = nvpair_name(elem);
87468c413aSTim Haley 		if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) {
88468c413aSTim Haley 			if (nvpair_value_nvlist(elem, &policy) == 0)
89468c413aSTim Haley 				zpool_get_rewind_policy(policy, zrpp);
90468c413aSTim Haley 			return;
91468c413aSTim Haley 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) {
92c8ee1847SVictor Latushkin 			if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
93c8ee1847SVictor Latushkin 				if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES)
94468c413aSTim Haley 					zrpp->zrp_request = ZPOOL_NO_REWIND;
95468c413aSTim Haley 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) {
96468c413aSTim Haley 			(void) nvpair_value_uint64(elem, &zrpp->zrp_txg);
97468c413aSTim Haley 		} else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) {
98c8ee1847SVictor Latushkin 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
99468c413aSTim Haley 		} else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
100c8ee1847SVictor Latushkin 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
101468c413aSTim Haley 		}
102468c413aSTim Haley 	}
103c8ee1847SVictor Latushkin 	if (zrpp->zrp_request == 0)
104c8ee1847SVictor Latushkin 		zrpp->zrp_request = ZPOOL_NO_REWIND;
105468c413aSTim Haley }
1060a586ceaSMark Shellenbaum 
1070a586ceaSMark Shellenbaum typedef struct zfs_version_spa_map {
1080a586ceaSMark Shellenbaum 	int	version_zpl;
1090a586ceaSMark Shellenbaum 	int	version_spa;
1100a586ceaSMark Shellenbaum } zfs_version_spa_map_t;
1110a586ceaSMark Shellenbaum 
1120a586ceaSMark Shellenbaum /*
1130a586ceaSMark Shellenbaum  * Keep this table in monotonically increasing version number order.
1140a586ceaSMark Shellenbaum  */
1150a586ceaSMark Shellenbaum static zfs_version_spa_map_t zfs_version_table[] = {
1160a586ceaSMark Shellenbaum 	{ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
1170a586ceaSMark Shellenbaum 	{ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
1180a586ceaSMark Shellenbaum 	{ZPL_VERSION_FUID, SPA_VERSION_FUID},
1190a586ceaSMark Shellenbaum 	{ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
1200a586ceaSMark Shellenbaum 	{ZPL_VERSION_SA, SPA_VERSION_SA},
1210a586ceaSMark Shellenbaum 	{0, 0}
1220a586ceaSMark Shellenbaum };
1230a586ceaSMark Shellenbaum 
1240a586ceaSMark Shellenbaum /*
1250a586ceaSMark Shellenbaum  * Return the max zpl version for a corresponding spa version
1260a586ceaSMark Shellenbaum  * -1 is returned if no mapping exists.
1270a586ceaSMark Shellenbaum  */
1280a586ceaSMark Shellenbaum int
1290a586ceaSMark Shellenbaum zfs_zpl_version_map(int spa_version)
1300a586ceaSMark Shellenbaum {
1310a586ceaSMark Shellenbaum 	int i;
1320a586ceaSMark Shellenbaum 	int version = -1;
1330a586ceaSMark Shellenbaum 
1340a586ceaSMark Shellenbaum 	for (i = 0; zfs_version_table[i].version_spa; i++) {
1350a586ceaSMark Shellenbaum 		if (spa_version >= zfs_version_table[i].version_spa)
1360a586ceaSMark Shellenbaum 			version = zfs_version_table[i].version_zpl;
1370a586ceaSMark Shellenbaum 	}
1380a586ceaSMark Shellenbaum 
1390a586ceaSMark Shellenbaum 	return (version);
1400a586ceaSMark Shellenbaum }
1410a586ceaSMark Shellenbaum 
1420a586ceaSMark Shellenbaum /*
1430a586ceaSMark Shellenbaum  * Return the min spa version for a corresponding spa version
1440a586ceaSMark Shellenbaum  * -1 is returned if no mapping exists.
1450a586ceaSMark Shellenbaum  */
1460a586ceaSMark Shellenbaum int
1470a586ceaSMark Shellenbaum zfs_spa_version_map(int zpl_version)
1480a586ceaSMark Shellenbaum {
1490a586ceaSMark Shellenbaum 	int i;
1500a586ceaSMark Shellenbaum 	int version = -1;
1510a586ceaSMark Shellenbaum 
1520a586ceaSMark Shellenbaum 	for (i = 0; zfs_version_table[i].version_zpl; i++) {
1530a586ceaSMark Shellenbaum 		if (zfs_version_table[i].version_zpl >= zpl_version)
1540a586ceaSMark Shellenbaum 			return (zfs_version_table[i].version_spa);
1550a586ceaSMark Shellenbaum 	}
1560a586ceaSMark Shellenbaum 
1570a586ceaSMark Shellenbaum 	return (version);
1580a586ceaSMark Shellenbaum }
159*3f9d6ad7SLin Ling 
160*3f9d6ad7SLin Ling const char *zfs_history_event_names[LOG_END] = {
161*3f9d6ad7SLin Ling 	"invalid event",
162*3f9d6ad7SLin Ling 	"pool create",
163*3f9d6ad7SLin Ling 	"vdev add",
164*3f9d6ad7SLin Ling 	"pool remove",
165*3f9d6ad7SLin Ling 	"pool destroy",
166*3f9d6ad7SLin Ling 	"pool export",
167*3f9d6ad7SLin Ling 	"pool import",
168*3f9d6ad7SLin Ling 	"vdev attach",
169*3f9d6ad7SLin Ling 	"vdev replace",
170*3f9d6ad7SLin Ling 	"vdev detach",
171*3f9d6ad7SLin Ling 	"vdev online",
172*3f9d6ad7SLin Ling 	"vdev offline",
173*3f9d6ad7SLin Ling 	"vdev upgrade",
174*3f9d6ad7SLin Ling 	"pool clear",
175*3f9d6ad7SLin Ling 	"pool scrub",
176*3f9d6ad7SLin Ling 	"pool property set",
177*3f9d6ad7SLin Ling 	"create",
178*3f9d6ad7SLin Ling 	"clone",
179*3f9d6ad7SLin Ling 	"destroy",
180*3f9d6ad7SLin Ling 	"destroy_begin_sync",
181*3f9d6ad7SLin Ling 	"inherit",
182*3f9d6ad7SLin Ling 	"property set",
183*3f9d6ad7SLin Ling 	"quota set",
184*3f9d6ad7SLin Ling 	"permission update",
185*3f9d6ad7SLin Ling 	"permission remove",
186*3f9d6ad7SLin Ling 	"permission who remove",
187*3f9d6ad7SLin Ling 	"promote",
188*3f9d6ad7SLin Ling 	"receive",
189*3f9d6ad7SLin Ling 	"rename",
190*3f9d6ad7SLin Ling 	"reservation set",
191*3f9d6ad7SLin Ling 	"replay_inc_sync",
192*3f9d6ad7SLin Ling 	"replay_full_sync",
193*3f9d6ad7SLin Ling 	"rollback",
194*3f9d6ad7SLin Ling 	"snapshot",
195*3f9d6ad7SLin Ling 	"filesystem version upgrade",
196*3f9d6ad7SLin Ling 	"refquota set",
197*3f9d6ad7SLin Ling 	"refreservation set",
198*3f9d6ad7SLin Ling 	"pool scrub done",
199*3f9d6ad7SLin Ling 	"user hold",
200*3f9d6ad7SLin Ling 	"user release",
201*3f9d6ad7SLin Ling 	"pool split",
202*3f9d6ad7SLin Ling };
203