]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_label.c
Performance optimization of AVL tree comparator functions
[mirror_zfs.git] / module / zfs / vdev_label.c
CommitLineData
34dc7c2f
BB
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 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2e528b49 24 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27/*
28 * Virtual Device Labels
29 * ---------------------
30 *
31 * The vdev label serves several distinct purposes:
32 *
33 * 1. Uniquely identify this device as part of a ZFS pool and confirm its
34 * identity within the pool.
35 *
36 * 2. Verify that all the devices given in a configuration are present
37 * within the pool.
38 *
39 * 3. Determine the uberblock for the pool.
40 *
41 * 4. In case of an import operation, determine the configuration of the
42 * toplevel vdev of which it is a part.
43 *
44 * 5. If an import operation cannot find all the devices in the pool,
45 * provide enough information to the administrator to determine which
46 * devices are missing.
47 *
48 * It is important to note that while the kernel is responsible for writing the
49 * label, it only consumes the information in the first three cases. The
50 * latter information is only consumed in userland when determining the
51 * configuration to import a pool.
52 *
53 *
54 * Label Organization
55 * ------------------
56 *
57 * Before describing the contents of the label, it's important to understand how
58 * the labels are written and updated with respect to the uberblock.
59 *
60 * When the pool configuration is altered, either because it was newly created
61 * or a device was added, we want to update all the labels such that we can deal
62 * with fatal failure at any point. To this end, each disk has two labels which
63 * are updated before and after the uberblock is synced. Assuming we have
64 * labels and an uberblock with the following transaction groups:
65 *
66 * L1 UB L2
67 * +------+ +------+ +------+
68 * | | | | | |
69 * | t10 | | t10 | | t10 |
70 * | | | | | |
71 * +------+ +------+ +------+
72 *
73 * In this stable state, the labels and the uberblock were all updated within
74 * the same transaction group (10). Each label is mirrored and checksummed, so
75 * that we can detect when we fail partway through writing the label.
76 *
77 * In order to identify which labels are valid, the labels are written in the
78 * following manner:
79 *
80 * 1. For each vdev, update 'L1' to the new label
81 * 2. Update the uberblock
82 * 3. For each vdev, update 'L2' to the new label
83 *
84 * Given arbitrary failure, we can determine the correct label to use based on
85 * the transaction group. If we fail after updating L1 but before updating the
86 * UB, we will notice that L1's transaction group is greater than the uberblock,
87 * so L2 must be valid. If we fail after writing the uberblock but before
88 * writing L2, we will notice that L2's transaction group is less than L1, and
89 * therefore L1 is valid.
90 *
91 * Another added complexity is that not every label is updated when the config
92 * is synced. If we add a single device, we do not want to have to re-write
93 * every label for every device in the pool. This means that both L1 and L2 may
94 * be older than the pool uberblock, because the necessary information is stored
95 * on another vdev.
96 *
97 *
98 * On-disk Format
99 * --------------
100 *
101 * The vdev label consists of two distinct parts, and is wrapped within the
102 * vdev_label_t structure. The label includes 8k of padding to permit legacy
103 * VTOC disk labels, but is otherwise ignored.
104 *
105 * The first half of the label is a packed nvlist which contains pool wide
106 * properties, per-vdev properties, and configuration information. It is
107 * described in more detail below.
108 *
109 * The latter half of the label consists of a redundant array of uberblocks.
110 * These uberblocks are updated whenever a transaction group is committed,
111 * or when the configuration is updated. When a pool is loaded, we scan each
112 * vdev for the 'best' uberblock.
113 *
114 *
115 * Configuration Information
116 * -------------------------
117 *
118 * The nvlist describing the pool and vdev contains the following elements:
119 *
120 * version ZFS on-disk version
121 * name Pool name
122 * state Pool state
123 * txg Transaction group in which this label was written
124 * pool_guid Unique identifier for this pool
125 * vdev_tree An nvlist describing vdev tree.
9ae529ec
CS
126 * features_for_read
127 * An nvlist of the features necessary for reading the MOS.
34dc7c2f
BB
128 *
129 * Each leaf device label also contains the following:
130 *
131 * top_guid Unique ID for top-level vdev in which this is contained
132 * guid Unique ID for the leaf vdev
133 *
134 * The 'vs' configuration follows the format described in 'spa_config.c'.
135 */
136
137#include <sys/zfs_context.h>
138#include <sys/spa.h>
139#include <sys/spa_impl.h>
140#include <sys/dmu.h>
141#include <sys/zap.h>
142#include <sys/vdev.h>
143#include <sys/vdev_impl.h>
144#include <sys/uberblock_impl.h>
145#include <sys/metaslab.h>
146#include <sys/zio.h>
428870ff 147#include <sys/dsl_scan.h>
34dc7c2f
BB
148#include <sys/fs/zfs.h>
149
150/*
151 * Basic routines to read and write from a vdev label.
152 * Used throughout the rest of this file.
153 */
154uint64_t
155vdev_label_offset(uint64_t psize, int l, uint64_t offset)
156{
157 ASSERT(offset < sizeof (vdev_label_t));
158 ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
159
160 return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
161 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
162}
163
b128c09f
BB
164/*
165 * Returns back the vdev label associated with the passed in offset.
166 */
167int
168vdev_label_number(uint64_t psize, uint64_t offset)
169{
170 int l;
171
172 if (offset >= psize - VDEV_LABEL_END_SIZE) {
173 offset -= psize - VDEV_LABEL_END_SIZE;
174 offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
175 }
176 l = offset / sizeof (vdev_label_t);
177 return (l < VDEV_LABELS ? l : -1);
178}
179
34dc7c2f
BB
180static void
181vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
b128c09f 182 uint64_t size, zio_done_func_t *done, void *private, int flags)
34dc7c2f 183{
b128c09f
BB
184 ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
185 SCL_STATE_ALL);
186 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
34dc7c2f
BB
187
188 zio_nowait(zio_read_phys(zio, vd,
189 vdev_label_offset(vd->vdev_psize, l, offset),
190 size, buf, ZIO_CHECKSUM_LABEL, done, private,
b128c09f 191 ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
34dc7c2f
BB
192}
193
194static void
195vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
196 uint64_t size, zio_done_func_t *done, void *private, int flags)
197{
b128c09f
BB
198 ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
199 (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
200 (SCL_CONFIG | SCL_STATE) &&
201 dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
202 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
34dc7c2f
BB
203
204 zio_nowait(zio_write_phys(zio, vd,
205 vdev_label_offset(vd->vdev_psize, l, offset),
206 size, buf, ZIO_CHECKSUM_LABEL, done, private,
207 ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
208}
209
193a37cb
TH
210/*
211 * Generate the nvlist representing this vdev's stats
212 */
213void
214vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
215{
216 nvlist_t *nvx;
217 vdev_stat_t *vs;
218 vdev_stat_ex_t *vsx;
219
220 vs = kmem_alloc(sizeof (*vs), KM_SLEEP);
221 vsx = kmem_alloc(sizeof (*vsx), KM_SLEEP);
222
223 vdev_get_stats_ex(vd, vs, vsx);
224 fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
225 (uint64_t *)vs, sizeof (*vs) / sizeof (uint64_t));
226
227 kmem_free(vs, sizeof (*vs));
228
229 /*
230 * Add extended stats into a special extended stats nvlist. This keeps
231 * all the extended stats nicely grouped together. The extended stats
232 * nvlist is then added to the main nvlist.
233 */
234 nvx = fnvlist_alloc();
235
236 /* ZIOs in flight to disk */
237 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
238 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_READ]);
239
240 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
241 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_WRITE]);
242
243 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
244 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_READ]);
245
246 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
247 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_WRITE]);
248
249 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
250 vsx->vsx_active_queue[ZIO_PRIORITY_SCRUB]);
251
252 /* ZIOs pending */
253 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
254 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_READ]);
255
256 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
257 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_WRITE]);
258
259 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
260 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_READ]);
261
262 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
263 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_WRITE]);
264
265 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
266 vsx->vsx_pend_queue[ZIO_PRIORITY_SCRUB]);
267
268 /* Histograms */
269 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
270 vsx->vsx_total_histo[ZIO_TYPE_READ],
271 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_READ]));
272
273 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
274 vsx->vsx_total_histo[ZIO_TYPE_WRITE],
275 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_WRITE]));
276
277 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
278 vsx->vsx_disk_histo[ZIO_TYPE_READ],
279 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_READ]));
280
281 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
282 vsx->vsx_disk_histo[ZIO_TYPE_WRITE],
283 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_WRITE]));
284
285 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
286 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ],
287 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ]));
288
289 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
290 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE],
291 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE]));
292
293 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
294 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ],
295 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ]));
296
297 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
298 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE],
299 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE]));
300
301 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
302 vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB],
303 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB]));
304
7e945072
TH
305 /* Request sizes */
306 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
307 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ],
308 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ]));
309
310 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
311 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE],
312 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE]));
313
314 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
315 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ],
316 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ]));
317
318 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
319 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE],
320 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE]));
321
322 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
323 vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB],
324 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB]));
325
326 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
327 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ],
328 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ]));
329
330 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
331 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE],
332 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE]));
333
334 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
335 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ],
336 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ]));
337
338 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
339 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE],
340 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE]));
341
342 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
343 vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
344 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
345
193a37cb
TH
346 /* Add extended stats nvlist to main nvlist */
347 fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
348
6a796725 349 fnvlist_free(nvx);
193a37cb
TH
350 kmem_free(vsx, sizeof (*vsx));
351}
352
34dc7c2f
BB
353/*
354 * Generate the nvlist representing this vdev's config.
355 */
356nvlist_t *
357vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
428870ff 358 vdev_config_flag_t flags)
34dc7c2f
BB
359{
360 nvlist_t *nv = NULL;
79c76d5b 361 nv = fnvlist_alloc();
34dc7c2f 362
5d1f7fb6 363 fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
428870ff 364 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
5d1f7fb6
GW
365 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
366 fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
34dc7c2f
BB
367
368 if (vd->vdev_path != NULL)
5d1f7fb6 369 fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
34dc7c2f
BB
370
371 if (vd->vdev_devid != NULL)
5d1f7fb6 372 fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
34dc7c2f
BB
373
374 if (vd->vdev_physpath != NULL)
5d1f7fb6
GW
375 fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
376 vd->vdev_physpath);
34dc7c2f 377
9babb374 378 if (vd->vdev_fru != NULL)
5d1f7fb6 379 fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
9babb374 380
34dc7c2f
BB
381 if (vd->vdev_nparity != 0) {
382 ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
383 VDEV_TYPE_RAIDZ) == 0);
384
385 /*
386 * Make sure someone hasn't managed to sneak a fancy new vdev
387 * into a crufty old storage pool.
388 */
389 ASSERT(vd->vdev_nparity == 1 ||
45d1cae3
BB
390 (vd->vdev_nparity <= 2 &&
391 spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
392 (vd->vdev_nparity <= 3 &&
393 spa_version(spa) >= SPA_VERSION_RAIDZ3));
34dc7c2f
BB
394
395 /*
396 * Note that we'll add the nparity tag even on storage pools
397 * that only support a single parity device -- older software
398 * will just ignore it.
399 */
5d1f7fb6 400 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
34dc7c2f
BB
401 }
402
403 if (vd->vdev_wholedisk != -1ULL)
5d1f7fb6
GW
404 fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
405 vd->vdev_wholedisk);
34dc7c2f
BB
406
407 if (vd->vdev_not_present)
5d1f7fb6 408 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
34dc7c2f
BB
409
410 if (vd->vdev_isspare)
5d1f7fb6 411 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
34dc7c2f 412
428870ff
BB
413 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
414 vd == vd->vdev_top) {
5d1f7fb6
GW
415 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
416 vd->vdev_ms_array);
417 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
418 vd->vdev_ms_shift);
419 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
420 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
421 vd->vdev_asize);
422 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
428870ff 423 if (vd->vdev_removing)
5d1f7fb6
GW
424 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
425 vd->vdev_removing);
34dc7c2f
BB
426 }
427
93cf2076 428 if (vd->vdev_dtl_sm != NULL) {
5d1f7fb6 429 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
93cf2076
GW
430 space_map_object(vd->vdev_dtl_sm));
431 }
34dc7c2f 432
428870ff 433 if (vd->vdev_crtxg)
5d1f7fb6 434 fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
428870ff 435
e0ab3ab5
JS
436 if (flags & VDEV_CONFIG_MOS) {
437 if (vd->vdev_leaf_zap != 0) {
438 ASSERT(vd->vdev_ops->vdev_op_leaf);
439 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
440 vd->vdev_leaf_zap);
441 }
442
443 if (vd->vdev_top_zap != 0) {
444 ASSERT(vd == vd->vdev_top);
445 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
446 vd->vdev_top_zap);
447 }
448 }
449
34dc7c2f 450 if (getstats) {
428870ff
BB
451 pool_scan_stat_t ps;
452
193a37cb 453 vdev_config_generate_stats(vd, nv);
428870ff
BB
454
455 /* provide either current or previous scan information */
456 if (spa_scan_get_stats(spa, &ps) == 0) {
5d1f7fb6 457 fnvlist_add_uint64_array(nv,
428870ff 458 ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
5d1f7fb6 459 sizeof (pool_scan_stat_t) / sizeof (uint64_t));
428870ff 460 }
34dc7c2f
BB
461 }
462
463 if (!vd->vdev_ops->vdev_op_leaf) {
464 nvlist_t **child;
428870ff
BB
465 int c, idx;
466
467 ASSERT(!vd->vdev_ishole);
34dc7c2f
BB
468
469 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
79c76d5b 470 KM_SLEEP);
34dc7c2f 471
428870ff
BB
472 for (c = 0, idx = 0; c < vd->vdev_children; c++) {
473 vdev_t *cvd = vd->vdev_child[c];
34dc7c2f 474
428870ff
BB
475 /*
476 * If we're generating an nvlist of removing
477 * vdevs then skip over any device which is
478 * not being removed.
479 */
480 if ((flags & VDEV_CONFIG_REMOVING) &&
481 !cvd->vdev_removing)
482 continue;
34dc7c2f 483
428870ff
BB
484 child[idx++] = vdev_config_generate(spa, cvd,
485 getstats, flags);
486 }
487
488 if (idx) {
5d1f7fb6
GW
489 fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
490 child, idx);
428870ff
BB
491 }
492
493 for (c = 0; c < idx; c++)
34dc7c2f
BB
494 nvlist_free(child[c]);
495
496 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
497
498 } else {
428870ff
BB
499 const char *aux = NULL;
500
34dc7c2f 501 if (vd->vdev_offline && !vd->vdev_tmpoffline)
5d1f7fb6
GW
502 fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
503 if (vd->vdev_resilver_txg != 0)
504 fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
505 vd->vdev_resilver_txg);
34dc7c2f 506 if (vd->vdev_faulted)
5d1f7fb6 507 fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
34dc7c2f 508 if (vd->vdev_degraded)
5d1f7fb6 509 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
34dc7c2f 510 if (vd->vdev_removed)
5d1f7fb6 511 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
34dc7c2f 512 if (vd->vdev_unspare)
5d1f7fb6 513 fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
428870ff 514 if (vd->vdev_ishole)
5d1f7fb6 515 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
428870ff
BB
516
517 switch (vd->vdev_stat.vs_aux) {
518 case VDEV_AUX_ERR_EXCEEDED:
519 aux = "err_exceeded";
520 break;
521
522 case VDEV_AUX_EXTERNAL:
523 aux = "external";
524 break;
525 }
526
527 if (aux != NULL)
5d1f7fb6 528 fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
428870ff
BB
529
530 if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
5d1f7fb6
GW
531 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
532 vd->vdev_orig_guid);
428870ff 533 }
34dc7c2f
BB
534 }
535
536 return (nv);
537}
538
428870ff
BB
539/*
540 * Generate a view of the top-level vdevs. If we currently have holes
541 * in the namespace, then generate an array which contains a list of holey
542 * vdevs. Additionally, add the number of top-level children that currently
543 * exist.
544 */
545void
546vdev_top_config_generate(spa_t *spa, nvlist_t *config)
547{
548 vdev_t *rvd = spa->spa_root_vdev;
549 uint64_t *array;
550 uint_t c, idx;
551
79c76d5b 552 array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
428870ff
BB
553
554 for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
555 vdev_t *tvd = rvd->vdev_child[c];
556
557 if (tvd->vdev_ishole)
558 array[idx++] = c;
559 }
560
561 if (idx) {
562 VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
563 array, idx) == 0);
564 }
565
566 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
567 rvd->vdev_children) == 0);
568
569 kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
570}
571
9ae529ec 572/*
3bc7e0fb
GW
573 * Returns the configuration from the label of the given vdev. For vdevs
574 * which don't have a txg value stored on their label (i.e. spares/cache)
575 * or have not been completely initialized (txg = 0) just return
576 * the configuration from the first valid label we find. Otherwise,
577 * find the most up-to-date label that does not exceed the specified
578 * 'txg' value.
9ae529ec 579 */
34dc7c2f 580nvlist_t *
3bc7e0fb 581vdev_label_read_config(vdev_t *vd, uint64_t txg)
34dc7c2f
BB
582{
583 spa_t *spa = vd->vdev_spa;
584 nvlist_t *config = NULL;
585 vdev_phys_t *vp;
586 zio_t *zio;
3bc7e0fb
GW
587 uint64_t best_txg = 0;
588 int error = 0;
9babb374
BB
589 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
590 ZIO_FLAG_SPECULATIVE;
d6320ddb 591 int l;
34dc7c2f 592
b128c09f 593 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
34dc7c2f
BB
594
595 if (!vdev_readable(vd))
596 return (NULL);
597
598 vp = zio_buf_alloc(sizeof (vdev_phys_t));
599
9babb374 600retry:
d6320ddb 601 for (l = 0; l < VDEV_LABELS; l++) {
3bc7e0fb 602 nvlist_t *label = NULL;
34dc7c2f 603
b128c09f 604 zio = zio_root(spa, NULL, NULL, flags);
34dc7c2f
BB
605
606 vdev_label_read(zio, vd, l, vp,
607 offsetof(vdev_label_t, vl_vdev_phys),
b128c09f 608 sizeof (vdev_phys_t), NULL, NULL, flags);
34dc7c2f
BB
609
610 if (zio_wait(zio) == 0 &&
611 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
3bc7e0fb
GW
612 &label, 0) == 0) {
613 uint64_t label_txg = 0;
614
615 /*
616 * Auxiliary vdevs won't have txg values in their
617 * labels and newly added vdevs may not have been
618 * completely initialized so just return the
619 * configuration from the first valid label we
620 * encounter.
621 */
622 error = nvlist_lookup_uint64(label,
623 ZPOOL_CONFIG_POOL_TXG, &label_txg);
624 if ((error || label_txg == 0) && !config) {
625 config = label;
626 break;
627 } else if (label_txg <= txg && label_txg > best_txg) {
628 best_txg = label_txg;
629 nvlist_free(config);
630 config = fnvlist_dup(label);
631 }
632 }
34dc7c2f 633
3bc7e0fb
GW
634 if (label != NULL) {
635 nvlist_free(label);
636 label = NULL;
34dc7c2f
BB
637 }
638 }
639
9babb374
BB
640 if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
641 flags |= ZIO_FLAG_TRYHARD;
642 goto retry;
643 }
644
34dc7c2f
BB
645 zio_buf_free(vp, sizeof (vdev_phys_t));
646
647 return (config);
648}
649
650/*
651 * Determine if a device is in use. The 'spare_guid' parameter will be filled
652 * in with the device guid if this spare is active elsewhere on the system.
653 */
654static boolean_t
655vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
656 uint64_t *spare_guid, uint64_t *l2cache_guid)
657{
658 spa_t *spa = vd->vdev_spa;
659 uint64_t state, pool_guid, device_guid, txg, spare_pool;
660 uint64_t vdtxg = 0;
661 nvlist_t *label;
662
663 if (spare_guid)
664 *spare_guid = 0ULL;
665 if (l2cache_guid)
666 *l2cache_guid = 0ULL;
667
668 /*
669 * Read the label, if any, and perform some basic sanity checks.
670 */
3bc7e0fb 671 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
34dc7c2f
BB
672 return (B_FALSE);
673
674 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
675 &vdtxg);
676
677 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
678 &state) != 0 ||
679 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
680 &device_guid) != 0) {
681 nvlist_free(label);
682 return (B_FALSE);
683 }
684
685 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
686 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
687 &pool_guid) != 0 ||
688 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
689 &txg) != 0)) {
690 nvlist_free(label);
691 return (B_FALSE);
692 }
693
694 nvlist_free(label);
695
696 /*
697 * Check to see if this device indeed belongs to the pool it claims to
698 * be a part of. The only way this is allowed is if the device is a hot
699 * spare (which we check for later on).
700 */
701 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
702 !spa_guid_exists(pool_guid, device_guid) &&
b128c09f 703 !spa_spare_exists(device_guid, NULL, NULL) &&
34dc7c2f
BB
704 !spa_l2cache_exists(device_guid, NULL))
705 return (B_FALSE);
706
707 /*
708 * If the transaction group is zero, then this an initialized (but
709 * unused) label. This is only an error if the create transaction
710 * on-disk is the same as the one we're using now, in which case the
711 * user has attempted to add the same vdev multiple times in the same
712 * transaction.
713 */
714 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
715 txg == 0 && vdtxg == crtxg)
716 return (B_TRUE);
717
718 /*
719 * Check to see if this is a spare device. We do an explicit check for
720 * spa_has_spare() here because it may be on our pending list of spares
721 * to add. We also check if it is an l2cache device.
722 */
b128c09f 723 if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
34dc7c2f
BB
724 spa_has_spare(spa, device_guid)) {
725 if (spare_guid)
726 *spare_guid = device_guid;
727
728 switch (reason) {
729 case VDEV_LABEL_CREATE:
730 case VDEV_LABEL_L2CACHE:
731 return (B_TRUE);
732
733 case VDEV_LABEL_REPLACE:
734 return (!spa_has_spare(spa, device_guid) ||
735 spare_pool != 0ULL);
736
737 case VDEV_LABEL_SPARE:
738 return (spa_has_spare(spa, device_guid));
e75c13c3
BB
739 default:
740 break;
34dc7c2f
BB
741 }
742 }
743
744 /*
745 * Check to see if this is an l2cache device.
746 */
747 if (spa_l2cache_exists(device_guid, NULL))
748 return (B_TRUE);
749
572e2857
BB
750 /*
751 * We can't rely on a pool's state if it's been imported
752 * read-only. Instead we look to see if the pools is marked
753 * read-only in the namespace and set the state to active.
754 */
485c581c
RY
755 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
756 (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
572e2857
BB
757 spa_mode(spa) == FREAD)
758 state = POOL_STATE_ACTIVE;
759
34dc7c2f
BB
760 /*
761 * If the device is marked ACTIVE, then this device is in use by another
762 * pool on the system.
763 */
764 return (state == POOL_STATE_ACTIVE);
765}
766
767/*
768 * Initialize a vdev label. We check to make sure each leaf device is not in
769 * use, and writable. We put down an initial label which we will later
770 * overwrite with a complete label. Note that it's important to do this
771 * sequentially, not in parallel, so that we catch cases of multiple use of the
772 * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
773 * itself.
774 */
775int
776vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
777{
778 spa_t *spa = vd->vdev_spa;
779 nvlist_t *label;
780 vdev_phys_t *vp;
9babb374 781 char *pad2;
34dc7c2f
BB
782 uberblock_t *ub;
783 zio_t *zio;
34dc7c2f
BB
784 char *buf;
785 size_t buflen;
786 int error;
d4ed6673 787 uint64_t spare_guid = 0, l2cache_guid = 0;
b128c09f 788 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
d6320ddb
BB
789 int c, l;
790 vdev_t *pvd;
34dc7c2f 791
b128c09f 792 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
34dc7c2f 793
d6320ddb 794 for (c = 0; c < vd->vdev_children; c++)
34dc7c2f
BB
795 if ((error = vdev_label_init(vd->vdev_child[c],
796 crtxg, reason)) != 0)
797 return (error);
798
428870ff
BB
799 /* Track the creation time for this vdev */
800 vd->vdev_crtxg = crtxg;
801
dda12da9 802 if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
34dc7c2f
BB
803 return (0);
804
805 /*
806 * Dead vdevs cannot be initialized.
807 */
808 if (vdev_is_dead(vd))
2e528b49 809 return (SET_ERROR(EIO));
34dc7c2f
BB
810
811 /*
812 * Determine if the vdev is in use.
813 */
428870ff 814 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
34dc7c2f 815 vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
2e528b49 816 return (SET_ERROR(EBUSY));
34dc7c2f 817
34dc7c2f
BB
818 /*
819 * If this is a request to add or replace a spare or l2cache device
820 * that is in use elsewhere on the system, then we must update the
821 * guid (which was initialized to a random value) to reflect the
822 * actual GUID (which is shared between multiple pools).
823 */
824 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
825 spare_guid != 0ULL) {
b128c09f 826 uint64_t guid_delta = spare_guid - vd->vdev_guid;
34dc7c2f 827
b128c09f 828 vd->vdev_guid += guid_delta;
34dc7c2f 829
d6320ddb 830 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
b128c09f 831 pvd->vdev_guid_sum += guid_delta;
34dc7c2f
BB
832
833 /*
834 * If this is a replacement, then we want to fallthrough to the
835 * rest of the code. If we're adding a spare, then it's already
836 * labeled appropriately and we can just return.
837 */
838 if (reason == VDEV_LABEL_SPARE)
839 return (0);
428870ff
BB
840 ASSERT(reason == VDEV_LABEL_REPLACE ||
841 reason == VDEV_LABEL_SPLIT);
34dc7c2f
BB
842 }
843
844 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
845 l2cache_guid != 0ULL) {
b128c09f 846 uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
34dc7c2f 847
b128c09f 848 vd->vdev_guid += guid_delta;
34dc7c2f 849
d6320ddb 850 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
b128c09f 851 pvd->vdev_guid_sum += guid_delta;
34dc7c2f
BB
852
853 /*
854 * If this is a replacement, then we want to fallthrough to the
855 * rest of the code. If we're adding an l2cache, then it's
856 * already labeled appropriately and we can just return.
857 */
858 if (reason == VDEV_LABEL_L2CACHE)
859 return (0);
860 ASSERT(reason == VDEV_LABEL_REPLACE);
861 }
862
863 /*
864 * Initialize its label.
865 */
866 vp = zio_buf_alloc(sizeof (vdev_phys_t));
867 bzero(vp, sizeof (vdev_phys_t));
868
869 /*
870 * Generate a label describing the pool and our top-level vdev.
871 * We mark it as being from txg 0 to indicate that it's not
872 * really part of an active pool just yet. The labels will
873 * be written again with a meaningful txg by spa_sync().
874 */
875 if (reason == VDEV_LABEL_SPARE ||
876 (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
877 /*
878 * For inactive hot spares, we generate a special label that
879 * identifies as a mutually shared hot spare. We write the
880 * label if we are adding a hot spare, or if we are removing an
881 * active hot spare (in which case we want to revert the
882 * labels).
883 */
79c76d5b 884 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
885
886 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
887 spa_version(spa)) == 0);
888 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
889 POOL_STATE_SPARE) == 0);
890 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
891 vd->vdev_guid) == 0);
892 } else if (reason == VDEV_LABEL_L2CACHE ||
893 (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
894 /*
895 * For level 2 ARC devices, add a special label.
896 */
79c76d5b 897 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
34dc7c2f
BB
898
899 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
900 spa_version(spa)) == 0);
901 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
902 POOL_STATE_L2CACHE) == 0);
903 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
904 vd->vdev_guid) == 0);
905 } else {
428870ff
BB
906 uint64_t txg = 0ULL;
907
908 if (reason == VDEV_LABEL_SPLIT)
909 txg = spa->spa_uberblock.ub_txg;
910 label = spa_config_generate(spa, vd, txg, B_FALSE);
34dc7c2f
BB
911
912 /*
913 * Add our creation time. This allows us to detect multiple
914 * vdev uses as described above, and automatically expires if we
915 * fail.
916 */
917 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
918 crtxg) == 0);
919 }
920
921 buf = vp->vp_nvlist;
922 buflen = sizeof (vp->vp_nvlist);
923
79c76d5b 924 error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
34dc7c2f
BB
925 if (error != 0) {
926 nvlist_free(label);
927 zio_buf_free(vp, sizeof (vdev_phys_t));
928 /* EFAULT means nvlist_pack ran out of room */
929 return (error == EFAULT ? ENAMETOOLONG : EINVAL);
930 }
931
34dc7c2f
BB
932 /*
933 * Initialize uberblock template.
934 */
45d1cae3
BB
935 ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
936 bzero(ub, VDEV_UBERBLOCK_RING);
34dc7c2f
BB
937 *ub = spa->spa_uberblock;
938 ub->ub_txg = 0;
939
9babb374
BB
940 /* Initialize the 2nd padding area. */
941 pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
942 bzero(pad2, VDEV_PAD_SIZE);
943
34dc7c2f
BB
944 /*
945 * Write everything in parallel.
946 */
9babb374 947retry:
34dc7c2f
BB
948 zio = zio_root(spa, NULL, NULL, flags);
949
d6320ddb 950 for (l = 0; l < VDEV_LABELS; l++) {
34dc7c2f
BB
951
952 vdev_label_write(zio, vd, l, vp,
953 offsetof(vdev_label_t, vl_vdev_phys),
954 sizeof (vdev_phys_t), NULL, NULL, flags);
955
9babb374
BB
956 /*
957 * Skip the 1st padding area.
958 * Zero out the 2nd padding area where it might have
959 * left over data from previous filesystem format.
960 */
961 vdev_label_write(zio, vd, l, pad2,
962 offsetof(vdev_label_t, vl_pad2),
963 VDEV_PAD_SIZE, NULL, NULL, flags);
34dc7c2f 964
45d1cae3
BB
965 vdev_label_write(zio, vd, l, ub,
966 offsetof(vdev_label_t, vl_uberblock),
967 VDEV_UBERBLOCK_RING, NULL, NULL, flags);
34dc7c2f
BB
968 }
969
970 error = zio_wait(zio);
971
9babb374
BB
972 if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
973 flags |= ZIO_FLAG_TRYHARD;
974 goto retry;
975 }
976
34dc7c2f 977 nvlist_free(label);
9babb374 978 zio_buf_free(pad2, VDEV_PAD_SIZE);
45d1cae3 979 zio_buf_free(ub, VDEV_UBERBLOCK_RING);
34dc7c2f
BB
980 zio_buf_free(vp, sizeof (vdev_phys_t));
981
982 /*
983 * If this vdev hasn't been previously identified as a spare, then we
984 * mark it as such only if a) we are labeling it as a spare, or b) it
985 * exists as a spare elsewhere in the system. Do the same for
986 * level 2 ARC devices.
987 */
988 if (error == 0 && !vd->vdev_isspare &&
989 (reason == VDEV_LABEL_SPARE ||
b128c09f 990 spa_spare_exists(vd->vdev_guid, NULL, NULL)))
34dc7c2f
BB
991 spa_spare_add(vd);
992
993 if (error == 0 && !vd->vdev_isl2cache &&
994 (reason == VDEV_LABEL_L2CACHE ||
995 spa_l2cache_exists(vd->vdev_guid, NULL)))
996 spa_l2cache_add(vd);
997
998 return (error);
999}
1000
1001/*
1002 * ==========================================================================
1003 * uberblock load/sync
1004 * ==========================================================================
1005 */
1006
1007/*
1008 * Consider the following situation: txg is safely synced to disk. We've
1009 * written the first uberblock for txg + 1, and then we lose power. When we
1010 * come back up, we fail to see the uberblock for txg + 1 because, say,
1011 * it was on a mirrored device and the replica to which we wrote txg + 1
1012 * is now offline. If we then make some changes and sync txg + 1, and then
9ae529ec 1013 * the missing replica comes back, then for a few seconds we'll have two
34dc7c2f
BB
1014 * conflicting uberblocks on disk with the same txg. The solution is simple:
1015 * among uberblocks with equal txg, choose the one with the latest timestamp.
1016 */
1017static int
ee36c709 1018vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
34dc7c2f 1019{
ee36c709
GN
1020 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1021 if (likely(cmp))
1022 return (cmp);
34dc7c2f 1023
ee36c709 1024 return (AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp));
34dc7c2f
BB
1025}
1026
9ae529ec
CS
1027struct ubl_cbdata {
1028 uberblock_t *ubl_ubbest; /* Best uberblock */
1029 vdev_t *ubl_vd; /* vdev associated with the above */
9ae529ec
CS
1030};
1031
34dc7c2f
BB
1032static void
1033vdev_uberblock_load_done(zio_t *zio)
1034{
9ae529ec 1035 vdev_t *vd = zio->io_vd;
428870ff 1036 spa_t *spa = zio->io_spa;
b128c09f 1037 zio_t *rio = zio->io_private;
34dc7c2f 1038 uberblock_t *ub = zio->io_data;
9ae529ec 1039 struct ubl_cbdata *cbp = rio->io_private;
34dc7c2f 1040
9ae529ec 1041 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
34dc7c2f
BB
1042
1043 if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
b128c09f 1044 mutex_enter(&rio->io_lock);
428870ff 1045 if (ub->ub_txg <= spa->spa_load_max_txg &&
9ae529ec
CS
1046 vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1047 /*
3bc7e0fb
GW
1048 * Keep track of the vdev in which this uberblock
1049 * was found. We will use this information later
1050 * to obtain the config nvlist associated with
9ae529ec
CS
1051 * this uberblock.
1052 */
1053 *cbp->ubl_ubbest = *ub;
1054 cbp->ubl_vd = vd;
9ae529ec 1055 }
b128c09f 1056 mutex_exit(&rio->io_lock);
34dc7c2f
BB
1057 }
1058
1059 zio_buf_free(zio->io_data, zio->io_size);
1060}
1061
9ae529ec
CS
1062static void
1063vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1064 struct ubl_cbdata *cbp)
34dc7c2f 1065{
d6320ddb 1066 int c, l, n;
b128c09f 1067
d6320ddb 1068 for (c = 0; c < vd->vdev_children; c++)
9ae529ec 1069 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
34dc7c2f 1070
b128c09f 1071 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
d6320ddb
BB
1072 for (l = 0; l < VDEV_LABELS; l++) {
1073 for (n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
b128c09f
BB
1074 vdev_label_read(zio, vd, l,
1075 zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
1076 VDEV_UBERBLOCK_OFFSET(vd, n),
1077 VDEV_UBERBLOCK_SIZE(vd),
1078 vdev_uberblock_load_done, zio, flags);
1079 }
34dc7c2f
BB
1080 }
1081 }
9ae529ec
CS
1082}
1083
1084/*
1085 * Reads the 'best' uberblock from disk along with its associated
1086 * configuration. First, we read the uberblock array of each label of each
1087 * vdev, keeping track of the uberblock with the highest txg in each array.
3bc7e0fb 1088 * Then, we read the configuration from the same vdev as the best uberblock.
9ae529ec
CS
1089 */
1090void
1091vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1092{
9ae529ec
CS
1093 zio_t *zio;
1094 spa_t *spa = rvd->vdev_spa;
1095 struct ubl_cbdata cb;
1096 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1097 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1098
1099 ASSERT(ub);
1100 ASSERT(config);
b128c09f 1101
9ae529ec
CS
1102 bzero(ub, sizeof (uberblock_t));
1103 *config = NULL;
1104
1105 cb.ubl_ubbest = ub;
1106 cb.ubl_vd = NULL;
1107
1108 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1109 zio = zio_root(spa, NULL, &cb, flags);
1110 vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1111 (void) zio_wait(zio);
3bc7e0fb
GW
1112
1113 /*
1114 * It's possible that the best uberblock was discovered on a label
1115 * that has a configuration which was written in a future txg.
1116 * Search all labels on this vdev to find the configuration that
1117 * matches the txg for our uberblock.
1118 */
1119 if (cb.ubl_vd != NULL)
1120 *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
9ae529ec 1121 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
1122}
1123
1124/*
1125 * On success, increment root zio's count of good writes.
1126 * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1127 */
1128static void
1129vdev_uberblock_sync_done(zio_t *zio)
1130{
1131 uint64_t *good_writes = zio->io_private;
1132
1133 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
bc89ac84 1134 atomic_inc_64(good_writes);
34dc7c2f
BB
1135}
1136
1137/*
1138 * Write the uberblock to all labels of all leaves of the specified vdev.
1139 */
1140static void
b128c09f 1141vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
34dc7c2f 1142{
34dc7c2f 1143 uberblock_t *ubbuf;
d6320ddb 1144 int c, l, n;
34dc7c2f 1145
d6320ddb 1146 for (c = 0; c < vd->vdev_children; c++)
b128c09f 1147 vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
34dc7c2f
BB
1148
1149 if (!vd->vdev_ops->vdev_op_leaf)
1150 return;
1151
b128c09f 1152 if (!vdev_writeable(vd))
34dc7c2f
BB
1153 return;
1154
1155 n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
1156
1157 ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
1158 bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
1159 *ubbuf = *ub;
1160
d6320ddb 1161 for (l = 0; l < VDEV_LABELS; l++)
34dc7c2f 1162 vdev_label_write(zio, vd, l, ubbuf,
b128c09f 1163 VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
34dc7c2f 1164 vdev_uberblock_sync_done, zio->io_private,
b128c09f 1165 flags | ZIO_FLAG_DONT_PROPAGATE);
34dc7c2f
BB
1166
1167 zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
1168}
1169
e49f1e20 1170/* Sync the uberblocks to all vdevs in svd[] */
34dc7c2f
BB
1171int
1172vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1173{
1174 spa_t *spa = svd[0]->vdev_spa;
34dc7c2f
BB
1175 zio_t *zio;
1176 uint64_t good_writes = 0;
d6320ddb 1177 int v;
34dc7c2f
BB
1178
1179 zio = zio_root(spa, NULL, &good_writes, flags);
1180
d6320ddb 1181 for (v = 0; v < svdcount; v++)
b128c09f 1182 vdev_uberblock_sync(zio, ub, svd[v], flags);
34dc7c2f
BB
1183
1184 (void) zio_wait(zio);
1185
1186 /*
1187 * Flush the uberblocks to disk. This ensures that the odd labels
1188 * are no longer needed (because the new uberblocks and the even
1189 * labels are safely on disk), so it is safe to overwrite them.
1190 */
1191 zio = zio_root(spa, NULL, NULL, flags);
1192
d6320ddb 1193 for (v = 0; v < svdcount; v++)
34dc7c2f
BB
1194 zio_flush(zio, svd[v]);
1195
1196 (void) zio_wait(zio);
1197
1198 return (good_writes >= 1 ? 0 : EIO);
1199}
1200
1201/*
1202 * On success, increment the count of good writes for our top-level vdev.
1203 */
1204static void
1205vdev_label_sync_done(zio_t *zio)
1206{
1207 uint64_t *good_writes = zio->io_private;
1208
1209 if (zio->io_error == 0)
bc89ac84 1210 atomic_inc_64(good_writes);
34dc7c2f
BB
1211}
1212
1213/*
1214 * If there weren't enough good writes, indicate failure to the parent.
1215 */
1216static void
1217vdev_label_sync_top_done(zio_t *zio)
1218{
1219 uint64_t *good_writes = zio->io_private;
1220
1221 if (*good_writes == 0)
2e528b49 1222 zio->io_error = SET_ERROR(EIO);
34dc7c2f
BB
1223
1224 kmem_free(good_writes, sizeof (uint64_t));
1225}
1226
b128c09f
BB
1227/*
1228 * We ignore errors for log and cache devices, simply free the private data.
1229 */
1230static void
1231vdev_label_sync_ignore_done(zio_t *zio)
1232{
1233 kmem_free(zio->io_private, sizeof (uint64_t));
1234}
1235
34dc7c2f
BB
1236/*
1237 * Write all even or odd labels to all leaves of the specified vdev.
1238 */
1239static void
b128c09f 1240vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
34dc7c2f
BB
1241{
1242 nvlist_t *label;
1243 vdev_phys_t *vp;
1244 char *buf;
1245 size_t buflen;
d6320ddb 1246 int c;
34dc7c2f 1247
d6320ddb 1248 for (c = 0; c < vd->vdev_children; c++)
b128c09f 1249 vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
34dc7c2f
BB
1250
1251 if (!vd->vdev_ops->vdev_op_leaf)
1252 return;
1253
b128c09f 1254 if (!vdev_writeable(vd))
34dc7c2f
BB
1255 return;
1256
1257 /*
1258 * Generate a label describing the top-level config to which we belong.
1259 */
1260 label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1261
1262 vp = zio_buf_alloc(sizeof (vdev_phys_t));
1263 bzero(vp, sizeof (vdev_phys_t));
1264
1265 buf = vp->vp_nvlist;
1266 buflen = sizeof (vp->vp_nvlist);
1267
79c76d5b 1268 if (!nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP)) {
34dc7c2f
BB
1269 for (; l < VDEV_LABELS; l += 2) {
1270 vdev_label_write(zio, vd, l, vp,
1271 offsetof(vdev_label_t, vl_vdev_phys),
1272 sizeof (vdev_phys_t),
1273 vdev_label_sync_done, zio->io_private,
b128c09f 1274 flags | ZIO_FLAG_DONT_PROPAGATE);
34dc7c2f
BB
1275 }
1276 }
1277
1278 zio_buf_free(vp, sizeof (vdev_phys_t));
1279 nvlist_free(label);
1280}
1281
1282int
b128c09f 1283vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
34dc7c2f 1284{
b128c09f 1285 list_t *dl = &spa->spa_config_dirty_list;
34dc7c2f
BB
1286 vdev_t *vd;
1287 zio_t *zio;
1288 int error;
1289
1290 /*
1291 * Write the new labels to disk.
1292 */
1293 zio = zio_root(spa, NULL, NULL, flags);
1294
1295 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
d6320ddb
BB
1296 uint64_t *good_writes;
1297 zio_t *vio;
428870ff
BB
1298
1299 ASSERT(!vd->vdev_ishole);
1300
79c76d5b 1301 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
d6320ddb 1302 vio = zio_null(zio, spa, NULL,
b128c09f
BB
1303 (vd->vdev_islog || vd->vdev_aux != NULL) ?
1304 vdev_label_sync_ignore_done : vdev_label_sync_top_done,
34dc7c2f 1305 good_writes, flags);
b128c09f 1306 vdev_label_sync(vio, vd, l, txg, flags);
34dc7c2f
BB
1307 zio_nowait(vio);
1308 }
1309
1310 error = zio_wait(zio);
1311
1312 /*
1313 * Flush the new labels to disk.
1314 */
1315 zio = zio_root(spa, NULL, NULL, flags);
1316
1317 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
1318 zio_flush(zio, vd);
1319
1320 (void) zio_wait(zio);
1321
1322 return (error);
1323}
1324
1325/*
1326 * Sync the uberblock and any changes to the vdev configuration.
1327 *
1328 * The order of operations is carefully crafted to ensure that
1329 * if the system panics or loses power at any time, the state on disk
1330 * is still transactionally consistent. The in-line comments below
1331 * describe the failure semantics at each stage.
1332 *
1333 * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1334 * at any time, you can just call it again, and it will resume its work.
1335 */
1336int
b6fcb792 1337vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
34dc7c2f
BB
1338{
1339 spa_t *spa = svd[0]->vdev_spa;
1340 uberblock_t *ub = &spa->spa_uberblock;
1341 vdev_t *vd;
1342 zio_t *zio;
b6fcb792 1343 int error = 0;
b128c09f 1344 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
34dc7c2f 1345
b6fcb792 1346retry:
9babb374
BB
1347 /*
1348 * Normally, we don't want to try too hard to write every label and
1349 * uberblock. If there is a flaky disk, we don't want the rest of the
1350 * sync process to block while we retry. But if we can't write a
1351 * single label out, we should retry with ZIO_FLAG_TRYHARD before
1352 * bailing out and declaring the pool faulted.
1353 */
b6fcb792
BB
1354 if (error != 0) {
1355 if ((flags & ZIO_FLAG_TRYHARD) != 0)
1356 return (error);
9babb374 1357 flags |= ZIO_FLAG_TRYHARD;
b6fcb792 1358 }
9babb374 1359
34dc7c2f
BB
1360 ASSERT(ub->ub_txg <= txg);
1361
1362 /*
1363 * If this isn't a resync due to I/O errors,
1364 * and nothing changed in this transaction group,
1365 * and the vdev configuration hasn't changed,
1366 * then there's nothing to do.
1367 */
1368 if (ub->ub_txg < txg &&
1369 uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
b128c09f 1370 list_is_empty(&spa->spa_config_dirty_list))
34dc7c2f
BB
1371 return (0);
1372
1373 if (txg > spa_freeze_txg(spa))
1374 return (0);
1375
1376 ASSERT(txg <= spa->spa_final_txg);
1377
1378 /*
1379 * Flush the write cache of every disk that's been written to
1380 * in this transaction group. This ensures that all blocks
1381 * written in this txg will be committed to stable storage
1382 * before any uberblock that references them.
1383 */
1384 zio = zio_root(spa, NULL, NULL, flags);
1385
1386 for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
1387 vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
1388 zio_flush(zio, vd);
1389
1390 (void) zio_wait(zio);
1391
1392 /*
1393 * Sync out the even labels (L0, L2) for every dirty vdev. If the
1394 * system dies in the middle of this process, that's OK: all of the
1395 * even labels that made it to disk will be newer than any uberblock,
1396 * and will therefore be considered invalid. The odd labels (L1, L3),
1397 * which have not yet been touched, will still be valid. We flush
1398 * the new labels to disk to ensure that all even-label updates
1399 * are committed to stable storage before the uberblock update.
1400 */
b128c09f 1401 if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
b6fcb792 1402 goto retry;
34dc7c2f
BB
1403
1404 /*
1405 * Sync the uberblocks to all vdevs in svd[].
1406 * If the system dies in the middle of this step, there are two cases
1407 * to consider, and the on-disk state is consistent either way:
1408 *
1409 * (1) If none of the new uberblocks made it to disk, then the
1410 * previous uberblock will be the newest, and the odd labels
1411 * (which had not yet been touched) will be valid with respect
1412 * to that uberblock.
1413 *
1414 * (2) If one or more new uberblocks made it to disk, then they
1415 * will be the newest, and the even labels (which had all
1416 * been successfully committed) will be valid with respect
1417 * to the new uberblocks.
1418 */
1419 if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
b6fcb792 1420 goto retry;
34dc7c2f
BB
1421
1422 /*
1423 * Sync out odd labels for every dirty vdev. If the system dies
1424 * in the middle of this process, the even labels and the new
1425 * uberblocks will suffice to open the pool. The next time
1426 * the pool is opened, the first thing we'll do -- before any
1427 * user data is modified -- is mark every vdev dirty so that
1428 * all labels will be brought up to date. We flush the new labels
1429 * to disk to ensure that all odd-label updates are committed to
1430 * stable storage before the next transaction group begins.
1431 */
b6fcb792
BB
1432 if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0)
1433 goto retry;
1434
1435 return (0);
34dc7c2f 1436}