xref: /onnv-gate/usr/src/common/zfs/zfs_comutil.c (revision 12296:7cf402a7f374)
15913Sperrin /*
25913Sperrin  * CDDL HEADER START
35913Sperrin  *
45913Sperrin  * The contents of this file are subject to the terms of the
55913Sperrin  * Common Development and Distribution License (the "License").
65913Sperrin  * You may not use this file except in compliance with the License.
75913Sperrin  *
85913Sperrin  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95913Sperrin  * or http://www.opensolaris.org/os/licensing.
105913Sperrin  * See the License for the specific language governing permissions
115913Sperrin  * and limitations under the License.
125913Sperrin  *
135913Sperrin  * When distributing Covered Code, include this CDDL HEADER in each
145913Sperrin  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155913Sperrin  * If applicable, add the following below this CDDL HEADER, with the
165913Sperrin  * fields enclosed by brackets "[]" replaced with your own identifying
175913Sperrin  * information: Portions Copyright [yyyy] [name of copyright owner]
185913Sperrin  *
195913Sperrin  * CDDL HEADER END
205913Sperrin  */
215913Sperrin /*
22*12296SLin.Ling@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
235913Sperrin  */
245913Sperrin 
255913Sperrin /*
265913Sperrin  * This file is intended for functions that ought to be common between user
275913Sperrin  * land (libzfs) and the kernel. When many common routines need to be shared
285913Sperrin  * then a separate file should to be created.
295913Sperrin  */
305913Sperrin 
315913Sperrin #if defined(_KERNEL)
325913Sperrin #include <sys/systm.h>
3310921STim.Haley@Sun.COM #else
3410921STim.Haley@Sun.COM #include <string.h>
355913Sperrin #endif
365913Sperrin 
375913Sperrin #include <sys/types.h>
385913Sperrin #include <sys/fs/zfs.h>
3910921STim.Haley@Sun.COM #include <sys/int_limits.h>
405913Sperrin #include <sys/nvpair.h>
4111935SMark.Shellenbaum@Sun.COM #include "zfs_comutil.h"
425913Sperrin 
435913Sperrin /*
445913Sperrin  * Are there allocatable vdevs?
455913Sperrin  */
465913Sperrin boolean_t
zfs_allocatable_devs(nvlist_t * nv)475913Sperrin zfs_allocatable_devs(nvlist_t *nv)
485913Sperrin {
495913Sperrin 	uint64_t is_log;
505913Sperrin 	uint_t c;
515913Sperrin 	nvlist_t **child;
525913Sperrin 	uint_t children;
535913Sperrin 
545913Sperrin 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
555913Sperrin 	    &child, &children) != 0) {
565913Sperrin 		return (B_FALSE);
575913Sperrin 	}
585913Sperrin 	for (c = 0; c < children; c++) {
595913Sperrin 		is_log = 0;
605913Sperrin 		(void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
615913Sperrin 		    &is_log);
625913Sperrin 		if (!is_log)
635913Sperrin 			return (B_TRUE);
645913Sperrin 	}
655913Sperrin 	return (B_FALSE);
665913Sperrin }
6710921STim.Haley@Sun.COM 
6810921STim.Haley@Sun.COM void
zpool_get_rewind_policy(nvlist_t * nvl,zpool_rewind_policy_t * zrpp)6910921STim.Haley@Sun.COM zpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp)
7010921STim.Haley@Sun.COM {
7110921STim.Haley@Sun.COM 	nvlist_t *policy;
7210921STim.Haley@Sun.COM 	nvpair_t *elem;
7310921STim.Haley@Sun.COM 	char *nm;
7410921STim.Haley@Sun.COM 
7510921STim.Haley@Sun.COM 	/* Defaults */
7610921STim.Haley@Sun.COM 	zrpp->zrp_request = ZPOOL_NO_REWIND;
7710921STim.Haley@Sun.COM 	zrpp->zrp_maxmeta = 0;
7811727SVictor.Latushkin@Sun.COM 	zrpp->zrp_maxdata = UINT64_MAX;
7910921STim.Haley@Sun.COM 	zrpp->zrp_txg = UINT64_MAX;
8010921STim.Haley@Sun.COM 
8110921STim.Haley@Sun.COM 	if (nvl == NULL)
8210921STim.Haley@Sun.COM 		return;
8310921STim.Haley@Sun.COM 
8410921STim.Haley@Sun.COM 	elem = NULL;
8510921STim.Haley@Sun.COM 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
8610921STim.Haley@Sun.COM 		nm = nvpair_name(elem);
8710921STim.Haley@Sun.COM 		if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) {
8810921STim.Haley@Sun.COM 			if (nvpair_value_nvlist(elem, &policy) == 0)
8910921STim.Haley@Sun.COM 				zpool_get_rewind_policy(policy, zrpp);
9010921STim.Haley@Sun.COM 			return;
9110921STim.Haley@Sun.COM 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) {
9211727SVictor.Latushkin@Sun.COM 			if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
9311727SVictor.Latushkin@Sun.COM 				if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES)
9410921STim.Haley@Sun.COM 					zrpp->zrp_request = ZPOOL_NO_REWIND;
9510921STim.Haley@Sun.COM 		} else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) {
9610921STim.Haley@Sun.COM 			(void) nvpair_value_uint64(elem, &zrpp->zrp_txg);
9710921STim.Haley@Sun.COM 		} else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) {
9811727SVictor.Latushkin@Sun.COM 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
9910921STim.Haley@Sun.COM 		} else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
10011727SVictor.Latushkin@Sun.COM 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
10110921STim.Haley@Sun.COM 		}
10210921STim.Haley@Sun.COM 	}
10311727SVictor.Latushkin@Sun.COM 	if (zrpp->zrp_request == 0)
10411727SVictor.Latushkin@Sun.COM 		zrpp->zrp_request = ZPOOL_NO_REWIND;
10510921STim.Haley@Sun.COM }
10611935SMark.Shellenbaum@Sun.COM 
10711935SMark.Shellenbaum@Sun.COM typedef struct zfs_version_spa_map {
10811935SMark.Shellenbaum@Sun.COM 	int	version_zpl;
10911935SMark.Shellenbaum@Sun.COM 	int	version_spa;
11011935SMark.Shellenbaum@Sun.COM } zfs_version_spa_map_t;
11111935SMark.Shellenbaum@Sun.COM 
11211935SMark.Shellenbaum@Sun.COM /*
11311935SMark.Shellenbaum@Sun.COM  * Keep this table in monotonically increasing version number order.
11411935SMark.Shellenbaum@Sun.COM  */
11511935SMark.Shellenbaum@Sun.COM static zfs_version_spa_map_t zfs_version_table[] = {
11611935SMark.Shellenbaum@Sun.COM 	{ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
11711935SMark.Shellenbaum@Sun.COM 	{ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
11811935SMark.Shellenbaum@Sun.COM 	{ZPL_VERSION_FUID, SPA_VERSION_FUID},
11911935SMark.Shellenbaum@Sun.COM 	{ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
12011935SMark.Shellenbaum@Sun.COM 	{ZPL_VERSION_SA, SPA_VERSION_SA},
12111935SMark.Shellenbaum@Sun.COM 	{0, 0}
12211935SMark.Shellenbaum@Sun.COM };
12311935SMark.Shellenbaum@Sun.COM 
12411935SMark.Shellenbaum@Sun.COM /*
12511935SMark.Shellenbaum@Sun.COM  * Return the max zpl version for a corresponding spa version
12611935SMark.Shellenbaum@Sun.COM  * -1 is returned if no mapping exists.
12711935SMark.Shellenbaum@Sun.COM  */
12811935SMark.Shellenbaum@Sun.COM int
zfs_zpl_version_map(int spa_version)12911935SMark.Shellenbaum@Sun.COM zfs_zpl_version_map(int spa_version)
13011935SMark.Shellenbaum@Sun.COM {
13111935SMark.Shellenbaum@Sun.COM 	int i;
13211935SMark.Shellenbaum@Sun.COM 	int version = -1;
13311935SMark.Shellenbaum@Sun.COM 
13411935SMark.Shellenbaum@Sun.COM 	for (i = 0; zfs_version_table[i].version_spa; i++) {
13511935SMark.Shellenbaum@Sun.COM 		if (spa_version >= zfs_version_table[i].version_spa)
13611935SMark.Shellenbaum@Sun.COM 			version = zfs_version_table[i].version_zpl;
13711935SMark.Shellenbaum@Sun.COM 	}
13811935SMark.Shellenbaum@Sun.COM 
13911935SMark.Shellenbaum@Sun.COM 	return (version);
14011935SMark.Shellenbaum@Sun.COM }
14111935SMark.Shellenbaum@Sun.COM 
14211935SMark.Shellenbaum@Sun.COM /*
14311935SMark.Shellenbaum@Sun.COM  * Return the min spa version for a corresponding spa version
14411935SMark.Shellenbaum@Sun.COM  * -1 is returned if no mapping exists.
14511935SMark.Shellenbaum@Sun.COM  */
14611935SMark.Shellenbaum@Sun.COM int
zfs_spa_version_map(int zpl_version)14711935SMark.Shellenbaum@Sun.COM zfs_spa_version_map(int zpl_version)
14811935SMark.Shellenbaum@Sun.COM {
14911935SMark.Shellenbaum@Sun.COM 	int i;
15011935SMark.Shellenbaum@Sun.COM 	int version = -1;
15111935SMark.Shellenbaum@Sun.COM 
15211935SMark.Shellenbaum@Sun.COM 	for (i = 0; zfs_version_table[i].version_zpl; i++) {
15311935SMark.Shellenbaum@Sun.COM 		if (zfs_version_table[i].version_zpl >= zpl_version)
15411935SMark.Shellenbaum@Sun.COM 			return (zfs_version_table[i].version_spa);
15511935SMark.Shellenbaum@Sun.COM 	}
15611935SMark.Shellenbaum@Sun.COM 
15711935SMark.Shellenbaum@Sun.COM 	return (version);
15811935SMark.Shellenbaum@Sun.COM }
159*12296SLin.Ling@Sun.COM 
160*12296SLin.Ling@Sun.COM const char *zfs_history_event_names[LOG_END] = {
161*12296SLin.Ling@Sun.COM 	"invalid event",
162*12296SLin.Ling@Sun.COM 	"pool create",
163*12296SLin.Ling@Sun.COM 	"vdev add",
164*12296SLin.Ling@Sun.COM 	"pool remove",
165*12296SLin.Ling@Sun.COM 	"pool destroy",
166*12296SLin.Ling@Sun.COM 	"pool export",
167*12296SLin.Ling@Sun.COM 	"pool import",
168*12296SLin.Ling@Sun.COM 	"vdev attach",
169*12296SLin.Ling@Sun.COM 	"vdev replace",
170*12296SLin.Ling@Sun.COM 	"vdev detach",
171*12296SLin.Ling@Sun.COM 	"vdev online",
172*12296SLin.Ling@Sun.COM 	"vdev offline",
173*12296SLin.Ling@Sun.COM 	"vdev upgrade",
174*12296SLin.Ling@Sun.COM 	"pool clear",
175*12296SLin.Ling@Sun.COM 	"pool scrub",
176*12296SLin.Ling@Sun.COM 	"pool property set",
177*12296SLin.Ling@Sun.COM 	"create",
178*12296SLin.Ling@Sun.COM 	"clone",
179*12296SLin.Ling@Sun.COM 	"destroy",
180*12296SLin.Ling@Sun.COM 	"destroy_begin_sync",
181*12296SLin.Ling@Sun.COM 	"inherit",
182*12296SLin.Ling@Sun.COM 	"property set",
183*12296SLin.Ling@Sun.COM 	"quota set",
184*12296SLin.Ling@Sun.COM 	"permission update",
185*12296SLin.Ling@Sun.COM 	"permission remove",
186*12296SLin.Ling@Sun.COM 	"permission who remove",
187*12296SLin.Ling@Sun.COM 	"promote",
188*12296SLin.Ling@Sun.COM 	"receive",
189*12296SLin.Ling@Sun.COM 	"rename",
190*12296SLin.Ling@Sun.COM 	"reservation set",
191*12296SLin.Ling@Sun.COM 	"replay_inc_sync",
192*12296SLin.Ling@Sun.COM 	"replay_full_sync",
193*12296SLin.Ling@Sun.COM 	"rollback",
194*12296SLin.Ling@Sun.COM 	"snapshot",
195*12296SLin.Ling@Sun.COM 	"filesystem version upgrade",
196*12296SLin.Ling@Sun.COM 	"refquota set",
197*12296SLin.Ling@Sun.COM 	"refreservation set",
198*12296SLin.Ling@Sun.COM 	"pool scrub done",
199*12296SLin.Ling@Sun.COM 	"user hold",
200*12296SLin.Ling@Sun.COM 	"user release",
201*12296SLin.Ling@Sun.COM 	"pool split",
202*12296SLin.Ling@Sun.COM };
203