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 /*
223f9d6ad7SLin Ling * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23*4445fffbSMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved.
24b7b97454Sperrin */
25b7b97454Sperrin
26b7b97454Sperrin /*
27b7b97454Sperrin * This file is intended for functions that ought to be common between user
28b7b97454Sperrin * land (libzfs) and the kernel. When many common routines need to be shared
29b7b97454Sperrin * then a separate file should to be created.
30b7b97454Sperrin */
31b7b97454Sperrin
32b7b97454Sperrin #if defined(_KERNEL)
33b7b97454Sperrin #include <sys/systm.h>
34468c413aSTim Haley #else
35468c413aSTim Haley #include <string.h>
36b7b97454Sperrin #endif
37b7b97454Sperrin
38b7b97454Sperrin #include <sys/types.h>
39b7b97454Sperrin #include <sys/fs/zfs.h>
40468c413aSTim Haley #include <sys/int_limits.h>
41b7b97454Sperrin #include <sys/nvpair.h>
420a586ceaSMark Shellenbaum #include "zfs_comutil.h"
43b7b97454Sperrin
44b7b97454Sperrin /*
45b7b97454Sperrin * Are there allocatable vdevs?
46b7b97454Sperrin */
47b7b97454Sperrin boolean_t
zfs_allocatable_devs(nvlist_t * nv)48b7b97454Sperrin zfs_allocatable_devs(nvlist_t *nv)
49b7b97454Sperrin {
50b7b97454Sperrin uint64_t is_log;
51b7b97454Sperrin uint_t c;
52b7b97454Sperrin nvlist_t **child;
53b7b97454Sperrin uint_t children;
54b7b97454Sperrin
55b7b97454Sperrin if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
56b7b97454Sperrin &child, &children) != 0) {
57b7b97454Sperrin return (B_FALSE);
58b7b97454Sperrin }
59b7b97454Sperrin for (c = 0; c < children; c++) {
60b7b97454Sperrin is_log = 0;
61b7b97454Sperrin (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
62b7b97454Sperrin &is_log);
63b7b97454Sperrin if (!is_log)
64b7b97454Sperrin return (B_TRUE);
65b7b97454Sperrin }
66b7b97454Sperrin return (B_FALSE);
67b7b97454Sperrin }
68468c413aSTim Haley
69468c413aSTim Haley void
zpool_get_rewind_policy(nvlist_t * nvl,zpool_rewind_policy_t * zrpp)70468c413aSTim Haley zpool_get_rewind_policy(nvlist_t *nvl, zpool_rewind_policy_t *zrpp)
71468c413aSTim Haley {
72468c413aSTim Haley nvlist_t *policy;
73468c413aSTim Haley nvpair_t *elem;
74468c413aSTim Haley char *nm;
75468c413aSTim Haley
76468c413aSTim Haley /* Defaults */
77468c413aSTim Haley zrpp->zrp_request = ZPOOL_NO_REWIND;
78468c413aSTim Haley zrpp->zrp_maxmeta = 0;
79c8ee1847SVictor Latushkin zrpp->zrp_maxdata = UINT64_MAX;
80468c413aSTim Haley zrpp->zrp_txg = UINT64_MAX;
81468c413aSTim Haley
82468c413aSTim Haley if (nvl == NULL)
83468c413aSTim Haley return;
84468c413aSTim Haley
85468c413aSTim Haley elem = NULL;
86468c413aSTim Haley while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
87468c413aSTim Haley nm = nvpair_name(elem);
88468c413aSTim Haley if (strcmp(nm, ZPOOL_REWIND_POLICY) == 0) {
89468c413aSTim Haley if (nvpair_value_nvlist(elem, &policy) == 0)
90468c413aSTim Haley zpool_get_rewind_policy(policy, zrpp);
91468c413aSTim Haley return;
92468c413aSTim Haley } else if (strcmp(nm, ZPOOL_REWIND_REQUEST) == 0) {
93c8ee1847SVictor Latushkin if (nvpair_value_uint32(elem, &zrpp->zrp_request) == 0)
94c8ee1847SVictor Latushkin if (zrpp->zrp_request & ~ZPOOL_REWIND_POLICIES)
95468c413aSTim Haley zrpp->zrp_request = ZPOOL_NO_REWIND;
96468c413aSTim Haley } else if (strcmp(nm, ZPOOL_REWIND_REQUEST_TXG) == 0) {
97468c413aSTim Haley (void) nvpair_value_uint64(elem, &zrpp->zrp_txg);
98468c413aSTim Haley } else if (strcmp(nm, ZPOOL_REWIND_META_THRESH) == 0) {
99c8ee1847SVictor Latushkin (void) nvpair_value_uint64(elem, &zrpp->zrp_maxmeta);
100468c413aSTim Haley } else if (strcmp(nm, ZPOOL_REWIND_DATA_THRESH) == 0) {
101c8ee1847SVictor Latushkin (void) nvpair_value_uint64(elem, &zrpp->zrp_maxdata);
102468c413aSTim Haley }
103468c413aSTim Haley }
104c8ee1847SVictor Latushkin if (zrpp->zrp_request == 0)
105c8ee1847SVictor Latushkin zrpp->zrp_request = ZPOOL_NO_REWIND;
106468c413aSTim Haley }
1070a586ceaSMark Shellenbaum
1080a586ceaSMark Shellenbaum typedef struct zfs_version_spa_map {
1090a586ceaSMark Shellenbaum int version_zpl;
1100a586ceaSMark Shellenbaum int version_spa;
1110a586ceaSMark Shellenbaum } zfs_version_spa_map_t;
1120a586ceaSMark Shellenbaum
1130a586ceaSMark Shellenbaum /*
1140a586ceaSMark Shellenbaum * Keep this table in monotonically increasing version number order.
1150a586ceaSMark Shellenbaum */
1160a586ceaSMark Shellenbaum static zfs_version_spa_map_t zfs_version_table[] = {
1170a586ceaSMark Shellenbaum {ZPL_VERSION_INITIAL, SPA_VERSION_INITIAL},
1180a586ceaSMark Shellenbaum {ZPL_VERSION_DIRENT_TYPE, SPA_VERSION_INITIAL},
1190a586ceaSMark Shellenbaum {ZPL_VERSION_FUID, SPA_VERSION_FUID},
1200a586ceaSMark Shellenbaum {ZPL_VERSION_USERSPACE, SPA_VERSION_USERSPACE},
1210a586ceaSMark Shellenbaum {ZPL_VERSION_SA, SPA_VERSION_SA},
1220a586ceaSMark Shellenbaum {0, 0}
1230a586ceaSMark Shellenbaum };
1240a586ceaSMark Shellenbaum
1250a586ceaSMark Shellenbaum /*
1260a586ceaSMark Shellenbaum * Return the max zpl version for a corresponding spa version
1270a586ceaSMark Shellenbaum * -1 is returned if no mapping exists.
1280a586ceaSMark Shellenbaum */
1290a586ceaSMark Shellenbaum int
zfs_zpl_version_map(int spa_version)1300a586ceaSMark Shellenbaum zfs_zpl_version_map(int spa_version)
1310a586ceaSMark Shellenbaum {
1320a586ceaSMark Shellenbaum int i;
1330a586ceaSMark Shellenbaum int version = -1;
1340a586ceaSMark Shellenbaum
1350a586ceaSMark Shellenbaum for (i = 0; zfs_version_table[i].version_spa; i++) {
1360a586ceaSMark Shellenbaum if (spa_version >= zfs_version_table[i].version_spa)
1370a586ceaSMark Shellenbaum version = zfs_version_table[i].version_zpl;
1380a586ceaSMark Shellenbaum }
1390a586ceaSMark Shellenbaum
1400a586ceaSMark Shellenbaum return (version);
1410a586ceaSMark Shellenbaum }
1420a586ceaSMark Shellenbaum
1430a586ceaSMark Shellenbaum /*
1440a586ceaSMark Shellenbaum * Return the min spa version for a corresponding spa version
1450a586ceaSMark Shellenbaum * -1 is returned if no mapping exists.
1460a586ceaSMark Shellenbaum */
1470a586ceaSMark Shellenbaum int
zfs_spa_version_map(int zpl_version)1480a586ceaSMark Shellenbaum zfs_spa_version_map(int zpl_version)
1490a586ceaSMark Shellenbaum {
1500a586ceaSMark Shellenbaum int i;
1510a586ceaSMark Shellenbaum int version = -1;
1520a586ceaSMark Shellenbaum
1530a586ceaSMark Shellenbaum for (i = 0; zfs_version_table[i].version_zpl; i++) {
1540a586ceaSMark Shellenbaum if (zfs_version_table[i].version_zpl >= zpl_version)
1550a586ceaSMark Shellenbaum return (zfs_version_table[i].version_spa);
1560a586ceaSMark Shellenbaum }
1570a586ceaSMark Shellenbaum
1580a586ceaSMark Shellenbaum return (version);
1590a586ceaSMark Shellenbaum }
1603f9d6ad7SLin Ling
161*4445fffbSMatthew Ahrens /*
162*4445fffbSMatthew Ahrens * This is the table of legacy internal event names; it should not be modified.
163*4445fffbSMatthew Ahrens * The internal events are now stored in the history log as strings.
164*4445fffbSMatthew Ahrens */
165*4445fffbSMatthew Ahrens const char *zfs_history_event_names[ZFS_NUM_LEGACY_HISTORY_EVENTS] = {
1663f9d6ad7SLin Ling "invalid event",
1673f9d6ad7SLin Ling "pool create",
1683f9d6ad7SLin Ling "vdev add",
1693f9d6ad7SLin Ling "pool remove",
1703f9d6ad7SLin Ling "pool destroy",
1713f9d6ad7SLin Ling "pool export",
1723f9d6ad7SLin Ling "pool import",
1733f9d6ad7SLin Ling "vdev attach",
1743f9d6ad7SLin Ling "vdev replace",
1753f9d6ad7SLin Ling "vdev detach",
1763f9d6ad7SLin Ling "vdev online",
1773f9d6ad7SLin Ling "vdev offline",
1783f9d6ad7SLin Ling "vdev upgrade",
1793f9d6ad7SLin Ling "pool clear",
1803f9d6ad7SLin Ling "pool scrub",
1813f9d6ad7SLin Ling "pool property set",
1823f9d6ad7SLin Ling "create",
1833f9d6ad7SLin Ling "clone",
1843f9d6ad7SLin Ling "destroy",
1853f9d6ad7SLin Ling "destroy_begin_sync",
1863f9d6ad7SLin Ling "inherit",
1873f9d6ad7SLin Ling "property set",
1883f9d6ad7SLin Ling "quota set",
1893f9d6ad7SLin Ling "permission update",
1903f9d6ad7SLin Ling "permission remove",
1913f9d6ad7SLin Ling "permission who remove",
1923f9d6ad7SLin Ling "promote",
1933f9d6ad7SLin Ling "receive",
1943f9d6ad7SLin Ling "rename",
1953f9d6ad7SLin Ling "reservation set",
1963f9d6ad7SLin Ling "replay_inc_sync",
1973f9d6ad7SLin Ling "replay_full_sync",
1983f9d6ad7SLin Ling "rollback",
1993f9d6ad7SLin Ling "snapshot",
2003f9d6ad7SLin Ling "filesystem version upgrade",
2013f9d6ad7SLin Ling "refquota set",
2023f9d6ad7SLin Ling "refreservation set",
2033f9d6ad7SLin Ling "pool scrub done",
2043f9d6ad7SLin Ling "user hold",
2053f9d6ad7SLin Ling "user release",
2063f9d6ad7SLin Ling "pool split",
2073f9d6ad7SLin Ling };
208