1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011 by Delphix. All rights reserved.
25  * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26  */
27 
28 
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
31 #include <sys/spa_impl.h>
32 #include <sys/zio.h>
33 #include <sys/zio_checksum.h>
34 #include <sys/dmu.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/zap.h>
37 #include <sys/zil.h>
38 #include <sys/ddt.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/vdev_os.h>
41 #include <sys/vdev_removal.h>
42 #include <sys/vdev_indirect_mapping.h>
43 #include <sys/vdev_indirect_births.h>
44 #include <sys/metaslab.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/txg.h>
48 #include <sys/avl.h>
49 #include <sys/bpobj.h>
50 #include <sys/dmu_traverse.h>
51 #include <sys/dmu_objset.h>
52 #include <sys/unique.h>
53 #include <sys/dsl_pool.h>
54 #include <sys/dsl_dataset.h>
55 #include <sys/dsl_dir.h>
56 #include <sys/dsl_prop.h>
57 #include <sys/dsl_synctask.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/arc.h>
60 #include <sys/callb.h>
61 #include <sys/spa_boot.h>
62 #include <sys/zfs_ioctl.h>
63 #include <sys/dsl_scan.h>
64 #include <sys/dmu_send.h>
65 #include <sys/dsl_destroy.h>
66 #include <sys/dsl_userhold.h>
67 #include <sys/zfeature.h>
68 #include <sys/zvol.h>
69 #include <sys/abd.h>
70 #include <sys/callb.h>
71 #include <sys/zone.h>
72 
73 #include "zfs_prop.h"
74 #include "zfs_comutil.h"
75 
76 static nvlist_t *
77 spa_generate_rootconf(const char *name)
78 {
79 	nvlist_t **configs, **tops;
80 	nvlist_t *config;
81 	nvlist_t *best_cfg, *nvtop, *nvroot;
82 	uint64_t *holes;
83 	uint64_t best_txg;
84 	uint64_t nchildren;
85 	uint64_t pgid;
86 	uint64_t count;
87 	uint64_t i;
88 	uint_t   nholes;
89 
90 	if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
91 		return (NULL);
92 
93 	ASSERT3U(count, !=, 0);
94 	best_txg = 0;
95 	for (i = 0; i < count; i++) {
96 		uint64_t txg;
97 
98 		txg = fnvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG);
99 		if (txg > best_txg) {
100 			best_txg = txg;
101 			best_cfg = configs[i];
102 		}
103 	}
104 
105 	nchildren = 1;
106 	nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
107 	holes = NULL;
108 	nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
109 	    &holes, &nholes);
110 
111 	tops = kmem_zalloc(nchildren * sizeof (void *), KM_SLEEP);
112 	for (i = 0; i < nchildren; i++) {
113 		if (i >= count)
114 			break;
115 		if (configs[i] == NULL)
116 			continue;
117 		nvtop = fnvlist_lookup_nvlist(configs[i],
118 		    ZPOOL_CONFIG_VDEV_TREE);
119 		tops[i] = fnvlist_dup(nvtop);
120 	}
121 	for (i = 0; holes != NULL && i < nholes; i++) {
122 		if (i >= nchildren)
123 			continue;
124 		if (tops[holes[i]] != NULL)
125 			continue;
126 		tops[holes[i]] = fnvlist_alloc();
127 		fnvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
128 		    VDEV_TYPE_HOLE);
129 		fnvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID, holes[i]);
130 		fnvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID, 0);
131 	}
132 	for (i = 0; i < nchildren; i++) {
133 		if (tops[i] != NULL)
134 			continue;
135 		tops[i] = fnvlist_alloc();
136 		fnvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
137 		    VDEV_TYPE_MISSING);
138 		fnvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID, i);
139 		fnvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID, 0);
140 	}
141 
142 	/*
143 	 * Create pool config based on the best vdev config.
144 	 */
145 	config = fnvlist_dup(best_cfg);
146 
147 	/*
148 	 * Put this pool's top-level vdevs into a root vdev.
149 	 */
150 	pgid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID);
151 	nvroot = fnvlist_alloc();
152 	fnvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT);
153 	fnvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL);
154 	fnvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid);
155 	fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, tops,
156 	    nchildren);
157 
158 	/*
159 	 * Replace the existing vdev_tree with the new root vdev in
160 	 * this pool's configuration (remove the old, add the new).
161 	 */
162 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot);
163 
164 	/*
165 	 * Drop vdev config elements that should not be present at pool level.
166 	 */
167 	fnvlist_remove(config, ZPOOL_CONFIG_GUID);
168 	fnvlist_remove(config, ZPOOL_CONFIG_TOP_GUID);
169 
170 	for (i = 0; i < count; i++)
171 		fnvlist_free(configs[i]);
172 	kmem_free(configs, count * sizeof (void *));
173 	for (i = 0; i < nchildren; i++)
174 		fnvlist_free(tops[i]);
175 	kmem_free(tops, nchildren * sizeof (void *));
176 	fnvlist_free(nvroot);
177 	return (config);
178 }
179 
180 int
181 spa_import_rootpool(const char *name, bool checkpointrewind)
182 {
183 	spa_t *spa;
184 	vdev_t *rvd;
185 	nvlist_t *config, *nvtop;
186 	uint64_t txg;
187 	char *pname;
188 	int error;
189 
190 	/*
191 	 * Read the label from the boot device and generate a configuration.
192 	 */
193 	config = spa_generate_rootconf(name);
194 
195 	mutex_enter(&spa_namespace_lock);
196 	if (config != NULL) {
197 		pname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME);
198 		VERIFY0(strcmp(name, pname));
199 		txg = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG);
200 
201 		if ((spa = spa_lookup(pname)) != NULL) {
202 			/*
203 			 * The pool could already be imported,
204 			 * e.g., after reboot -r.
205 			 */
206 			if (spa->spa_state == POOL_STATE_ACTIVE) {
207 				mutex_exit(&spa_namespace_lock);
208 				fnvlist_free(config);
209 				return (0);
210 			}
211 
212 			/*
213 			 * Remove the existing root pool from the namespace so
214 			 * that we can replace it with the correct config
215 			 * we just read in.
216 			 */
217 			spa_remove(spa);
218 		}
219 		spa = spa_add(pname, config, NULL);
220 
221 		/*
222 		 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
223 		 * via spa_version().
224 		 */
225 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
226 		    &spa->spa_ubsync.ub_version) != 0)
227 			spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
228 	} else if ((spa = spa_lookup(name)) == NULL) {
229 		mutex_exit(&spa_namespace_lock);
230 		fnvlist_free(config);
231 		cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
232 		    name);
233 		return (EIO);
234 	} else {
235 		config = fnvlist_dup(spa->spa_config);
236 	}
237 	spa->spa_is_root = B_TRUE;
238 	spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
239 	if (checkpointrewind) {
240 		spa->spa_import_flags |= ZFS_IMPORT_CHECKPOINT;
241 	}
242 
243 	/*
244 	 * Build up a vdev tree based on the boot device's label config.
245 	 */
246 	nvtop = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE);
247 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
248 	error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
249 	    VDEV_ALLOC_ROOTPOOL);
250 	spa_config_exit(spa, SCL_ALL, FTAG);
251 	if (error) {
252 		mutex_exit(&spa_namespace_lock);
253 		fnvlist_free(config);
254 		cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
255 		    pname);
256 		return (error);
257 	}
258 
259 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
260 	vdev_free(rvd);
261 	spa_config_exit(spa, SCL_ALL, FTAG);
262 	mutex_exit(&spa_namespace_lock);
263 
264 	fnvlist_free(config);
265 	return (0);
266 }
267 
268 const char *
269 spa_history_zone(void)
270 {
271 	return ("freebsd");
272 }
273