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*11727SVictor.Latushkin@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
235913Sperrin  * Use is subject to license terms.
245913Sperrin  */
255913Sperrin 
265913Sperrin /*
275913Sperrin  * This file is intended for functions that ought to be common between user
285913Sperrin  * land (libzfs) and the kernel. When many common routines need to be shared
295913Sperrin  * then a separate file should to be created.
305913Sperrin  */
315913Sperrin 
325913Sperrin #if defined(_KERNEL)
335913Sperrin #include <sys/systm.h>
3410921STim.Haley@Sun.COM #else
3510921STim.Haley@Sun.COM #include <string.h>
365913Sperrin #endif
375913Sperrin 
385913Sperrin #include <sys/types.h>
395913Sperrin #include <sys/fs/zfs.h>
4010921STim.Haley@Sun.COM #include <sys/int_limits.h>
415913Sperrin #include <sys/nvpair.h>
425913Sperrin 
435913Sperrin /*
445913Sperrin  * Are there allocatable vdevs?
455913Sperrin  */
465913Sperrin boolean_t
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
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;
78*11727SVictor.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) {
92*11727SVictor.Latushkin@Sun.COM 			if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
93*11727SVictor.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) {
98*11727SVictor.Latushkin@Sun.COM 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
9910921STim.Haley@Sun.COM 		} else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
100*11727SVictor.Latushkin@Sun.COM 			(void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
10110921STim.Haley@Sun.COM 		}
10210921STim.Haley@Sun.COM 	}
103*11727SVictor.Latushkin@Sun.COM 	if (zrpp->zrp_request == 0)
104*11727SVictor.Latushkin@Sun.COM 		zrpp->zrp_request = ZPOOL_NO_REWIND;
10510921STim.Haley@Sun.COM }
106