1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
16 * Copyright (c) 2017, Intel Corporation.
17 * Copyright (c) 2024-2026, Klara, Inc.
18 * Copyright (c) 2026, TrueNAS.
19 */
20
21 /*
22 * Virtual Device Labels
23 * ---------------------
24 *
25 * The vdev label serves several distinct purposes:
26 *
27 * 1. Uniquely identify this device as part of a ZFS pool and confirm its
28 * identity within the pool.
29 *
30 * 2. Verify that all the devices given in a configuration are present
31 * within the pool.
32 *
33 * 3. Determine the uberblock for the pool.
34 *
35 * 4. In case of an import operation, determine the configuration of the
36 * toplevel vdev of which it is a part.
37 *
38 * 5. If an import operation cannot find all the devices in the pool,
39 * provide enough information to the administrator to determine which
40 * devices are missing.
41 *
42 * It is important to note that while the kernel is responsible for writing the
43 * label, it only consumes the information in the first three cases. The
44 * latter information is only consumed in userland when determining the
45 * configuration to import a pool.
46 *
47 *
48 * Label Organization
49 * ------------------
50 *
51 * Before describing the contents of the label, it's important to understand how
52 * the labels are written and updated with respect to the uberblock.
53 *
54 * When the pool configuration is altered, either because it was newly created
55 * or a device was added, we want to update all the labels such that we can deal
56 * with fatal failure at any point. To this end, each disk has two labels which
57 * are updated before and after the uberblock is synced. Assuming we have
58 * labels and an uberblock with the following transaction groups:
59 *
60 * L1 UB L2
61 * +------+ +------+ +------+
62 * | | | | | |
63 * | t10 | | t10 | | t10 |
64 * | | | | | |
65 * +------+ +------+ +------+
66 *
67 * In this stable state, the labels and the uberblock were all updated within
68 * the same transaction group (10). Each label is mirrored and checksummed, so
69 * that we can detect when we fail partway through writing the label.
70 *
71 * In order to identify which labels are valid, the labels are written in the
72 * following manner:
73 *
74 * 1. For each vdev, update 'L1' to the new label
75 * 2. Update the uberblock
76 * 3. For each vdev, update 'L2' to the new label
77 *
78 * Given arbitrary failure, we can determine the correct label to use based on
79 * the transaction group. If we fail after updating L1 but before updating the
80 * UB, we will notice that L1's transaction group is greater than the uberblock,
81 * so L2 must be valid. If we fail after writing the uberblock but before
82 * writing L2, we will notice that L2's transaction group is less than L1, and
83 * therefore L1 is valid.
84 *
85 * Another added complexity is that not every label is updated when the config
86 * is synced. If we add a single device, we do not want to have to re-write
87 * every label for every device in the pool. This means that both L1 and L2 may
88 * be older than the pool uberblock, because the necessary information is stored
89 * on another vdev.
90 *
91 *
92 * On-disk Format
93 * --------------
94 *
95 * The vdev label consists of two distinct parts, and is wrapped within the
96 * vdev_label_t structure. The label includes 8k of padding to permit legacy
97 * VTOC disk labels, but is otherwise ignored.
98 *
99 * The first half of the label is a packed nvlist which contains pool wide
100 * properties, per-vdev properties, and configuration information. It is
101 * described in more detail below.
102 *
103 * The latter half of the label consists of a redundant array of uberblocks.
104 * These uberblocks are updated whenever a transaction group is committed,
105 * or when the configuration is updated. When a pool is loaded, we scan each
106 * vdev for the 'best' uberblock.
107 *
108 *
109 * Configuration Information
110 * -------------------------
111 *
112 * The nvlist describing the pool and vdev contains the following elements:
113 *
114 * version ZFS on-disk version
115 * name Pool name
116 * state Pool state
117 * txg Transaction group in which this label was written
118 * pool_guid Unique identifier for this pool
119 * vdev_tree An nvlist describing vdev tree.
120 * features_for_read
121 * An nvlist of the features necessary for reading the MOS.
122 *
123 * Each leaf device label also contains the following:
124 *
125 * top_guid Unique ID for top-level vdev in which this is contained
126 * guid Unique ID for the leaf vdev
127 *
128 * The 'vs' configuration follows the format described in 'spa_config.c'.
129 */
130
131 #include <sys/zfs_context.h>
132 #include <sys/spa.h>
133 #include <sys/spa_impl.h>
134 #include <sys/dmu.h>
135 #include <sys/zap.h>
136 #include <sys/vdev.h>
137 #include <sys/vdev_impl.h>
138 #include <sys/vdev_raidz.h>
139 #include <sys/vdev_draid.h>
140 #include <sys/uberblock_impl.h>
141 #include <sys/metaslab.h>
142 #include <sys/metaslab_impl.h>
143 #include <sys/zio.h>
144 #include <sys/dsl_scan.h>
145 #include <sys/abd.h>
146 #include <sys/fs/zfs.h>
147 #include <sys/byteorder.h>
148 #include <sys/zfs_bootenv.h>
149
150 /*
151 * Basic routines to read and write from a vdev label.
152 * Used throughout the rest of this file.
153 */
154 uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)155 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
156 {
157 ASSERT(offset < sizeof (vdev_label_t));
158 ASSERT0(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t));
159
160 return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
161 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
162 }
163
164 /*
165 * Returns back the vdev label associated with the passed in offset.
166 */
167 int
vdev_label_number(uint64_t psize,uint64_t offset)168 vdev_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
180 static void
vdev_label_read(zio_t * zio,vdev_t * vd,int l,abd_t * buf,uint64_t offset,uint64_t size,zio_done_func_t * done,void * private,int flags)181 vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
182 uint64_t size, zio_done_func_t *done, void *private, int flags)
183 {
184 ASSERT(
185 spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
186 spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
187 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
188
189 zio_nowait(zio_read_phys(zio, vd,
190 vdev_label_offset(vd->vdev_psize, l, offset),
191 size, buf, ZIO_CHECKSUM_LABEL, done, private,
192 ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
193 }
194
195 void
vdev_label_write(zio_t * zio,vdev_t * vd,int l,abd_t * buf,uint64_t offset,uint64_t size,zio_done_func_t * done,void * private,int flags)196 vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
197 uint64_t size, zio_done_func_t *done, void *private, int flags)
198 {
199 ASSERT(
200 spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
201 spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
202 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
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
210 /*
211 * Generate the nvlist representing this vdev's stats
212 */
213 void
vdev_config_generate_stats(vdev_t * vd,nvlist_t * nv)214 vdev_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 /*
228 * Add extended stats into a special extended stats nvlist. This keeps
229 * all the extended stats nicely grouped together. The extended stats
230 * nvlist is then added to the main nvlist.
231 */
232 nvx = fnvlist_alloc();
233
234 /* ZIOs in flight to disk */
235 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
236 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_READ]);
237
238 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
239 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_WRITE]);
240
241 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
242 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_READ]);
243
244 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
245 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_WRITE]);
246
247 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
248 vsx->vsx_active_queue[ZIO_PRIORITY_SCRUB]);
249
250 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
251 vsx->vsx_active_queue[ZIO_PRIORITY_TRIM]);
252
253 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_REBUILD_ACTIVE_QUEUE,
254 vsx->vsx_active_queue[ZIO_PRIORITY_REBUILD]);
255
256 /* ZIOs pending */
257 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
258 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_READ]);
259
260 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
261 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_WRITE]);
262
263 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
264 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_READ]);
265
266 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
267 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_WRITE]);
268
269 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
270 vsx->vsx_pend_queue[ZIO_PRIORITY_SCRUB]);
271
272 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
273 vsx->vsx_pend_queue[ZIO_PRIORITY_TRIM]);
274
275 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_REBUILD_PEND_QUEUE,
276 vsx->vsx_pend_queue[ZIO_PRIORITY_REBUILD]);
277
278 /* Histograms */
279 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
280 vsx->vsx_total_histo[ZIO_TYPE_READ],
281 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_READ]));
282
283 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
284 vsx->vsx_total_histo[ZIO_TYPE_WRITE],
285 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_WRITE]));
286
287 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
288 vsx->vsx_disk_histo[ZIO_TYPE_READ],
289 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_READ]));
290
291 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
292 vsx->vsx_disk_histo[ZIO_TYPE_WRITE],
293 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_WRITE]));
294
295 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
296 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ],
297 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ]));
298
299 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
300 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE],
301 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE]));
302
303 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
304 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ],
305 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ]));
306
307 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
308 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE],
309 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE]));
310
311 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
312 vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB],
313 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB]));
314
315 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
316 vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM],
317 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM]));
318
319 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_REBUILD_LAT_HISTO,
320 vsx->vsx_queue_histo[ZIO_PRIORITY_REBUILD],
321 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_REBUILD]));
322
323 /* Request sizes */
324 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
325 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ],
326 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ]));
327
328 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
329 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE],
330 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE]));
331
332 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
333 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ],
334 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ]));
335
336 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
337 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE],
338 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE]));
339
340 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
341 vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB],
342 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB]));
343
344 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
345 vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM],
346 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM]));
347
348 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_REBUILD_HISTO,
349 vsx->vsx_ind_histo[ZIO_PRIORITY_REBUILD],
350 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_REBUILD]));
351
352 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
353 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ],
354 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ]));
355
356 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
357 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE],
358 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE]));
359
360 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
361 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ],
362 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ]));
363
364 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
365 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE],
366 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE]));
367
368 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
369 vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
370 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
371
372 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
373 vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM],
374 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM]));
375
376 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_REBUILD_HISTO,
377 vsx->vsx_agg_histo[ZIO_PRIORITY_REBUILD],
378 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_REBUILD]));
379
380 /* IO delays */
381 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SLOW_IOS, vs->vs_slow_ios);
382
383 /* Direct I/O write verify errors */
384 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_DIO_VERIFY_ERRORS,
385 vs->vs_dio_verify_errors);
386
387 /* Add extended stats nvlist to main nvlist */
388 fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
389
390 fnvlist_free(nvx);
391 kmem_free(vs, sizeof (*vs));
392 kmem_free(vsx, sizeof (*vsx));
393 }
394
395 static const char *condense_type_keys[] = {
396 POOL_CONDENSE_LOG_SPACEMAP,
397 #ifdef ZFS_DEBUG
398 "debug",
399 #endif
400 NULL,
401 };
402
403 static void
root_vdev_actions_getprogress(vdev_t * vd,nvlist_t * nvl)404 root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
405 {
406 spa_t *spa = vd->vdev_spa;
407
408 if (vd != spa->spa_root_vdev)
409 return;
410
411 /* provide either current or previous scan information */
412 pool_scan_stat_t ps;
413 if (spa_scan_get_stats(spa, &ps) == 0) {
414 fnvlist_add_uint64_array(nvl,
415 ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
416 sizeof (pool_scan_stat_t) / sizeof (uint64_t));
417 }
418
419 pool_removal_stat_t prs;
420 if (spa_removal_get_stats(spa, &prs) == 0) {
421 fnvlist_add_uint64_array(nvl,
422 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
423 sizeof (prs) / sizeof (uint64_t));
424 }
425
426 pool_checkpoint_stat_t pcs;
427 if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
428 fnvlist_add_uint64_array(nvl,
429 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
430 sizeof (pcs) / sizeof (uint64_t));
431 }
432
433 pool_raidz_expand_stat_t pres;
434 if (spa_raidz_expand_get_stats(spa, &pres) == 0) {
435 fnvlist_add_uint64_array(nvl,
436 ZPOOL_CONFIG_RAIDZ_EXPAND_STATS, (uint64_t *)&pres,
437 sizeof (pres) / sizeof (uint64_t));
438 }
439
440 nvlist_t *cnv = fnvlist_alloc();
441 for (spa_condense_type_t type = 0; type < SPA_CONDENSE_TYPES; type++) {
442 const spa_condense_stat_t *scns =
443 &spa->spa_condense_stats[type];
444 if (scns->scns_start_time == 0)
445 continue;
446
447 nvlist_t *tnv = fnvlist_alloc();
448 mutex_enter(&spa->spa_condense_stats_lock);
449
450 if (scns->scns_start_time == 0) {
451 /* It was cleared before we could get the lock, skip. */
452 mutex_exit(&spa->spa_condense_stats_lock);
453 fnvlist_free(tnv);
454 continue;
455 }
456
457 fnvlist_add_uint64(tnv, "start_time", scns->scns_start_time);
458 fnvlist_add_uint64(tnv, "end_time", scns->scns_end_time);
459 fnvlist_add_uint64(tnv, "processed", scns->scns_processed);
460 fnvlist_add_uint64(tnv, "total", scns->scns_total);
461
462 mutex_exit(&spa->spa_condense_stats_lock);
463
464 fnvlist_add_nvlist(cnv, condense_type_keys[type], tnv);
465 fnvlist_free(tnv);
466 }
467 fnvlist_add_nvlist(nvl, ZPOOL_CONFIG_CONDENSE_STATS, cnv);
468 fnvlist_free(cnv);
469 }
470
471 static void
top_vdev_actions_getprogress(vdev_t * vd,nvlist_t * nvl)472 top_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
473 {
474 if (vd == vd->vdev_top) {
475 vdev_rebuild_stat_t vrs;
476 if (vdev_rebuild_get_stats(vd, &vrs) == 0) {
477 fnvlist_add_uint64_array(nvl,
478 ZPOOL_CONFIG_REBUILD_STATS, (uint64_t *)&vrs,
479 sizeof (vrs) / sizeof (uint64_t));
480 }
481 }
482 }
483
484 /*
485 * Generate the nvlist representing this vdev's config.
486 */
487 nvlist_t *
vdev_config_generate(spa_t * spa,vdev_t * vd,boolean_t getstats,vdev_config_flag_t flags)488 vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
489 vdev_config_flag_t flags)
490 {
491 nvlist_t *nv = NULL;
492 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
493
494 nv = fnvlist_alloc();
495
496 fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
497 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
498 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
499 fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
500 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
501 vd->vdev_top != NULL) {
502 fnvlist_add_uint64(nv, ZPOOL_CONFIG_TOP_GUID,
503 vd->vdev_top->vdev_guid);
504 }
505
506 if (vd->vdev_path != NULL)
507 fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
508
509 if (vd->vdev_devid != NULL)
510 fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
511
512 if (vd->vdev_physpath != NULL)
513 fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
514 vd->vdev_physpath);
515
516 if (vd->vdev_enc_sysfs_path != NULL)
517 fnvlist_add_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
518 vd->vdev_enc_sysfs_path);
519
520 if (vd->vdev_fru != NULL)
521 fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
522
523 if (vd->vdev_ops->vdev_op_config_generate != NULL)
524 vd->vdev_ops->vdev_op_config_generate(vd, nv);
525
526 if (vd->vdev_wholedisk != -1ULL) {
527 fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
528 vd->vdev_wholedisk);
529 }
530
531 if (vd->vdev_ops->vdev_op_leaf) {
532 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_ROTATIONAL,
533 !vd->vdev_nonrot);
534 }
535
536 if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
537 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
538
539 if (vd->vdev_isspare)
540 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
541
542 if (flags & VDEV_CONFIG_L2CACHE)
543 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
544
545 if ((flags & VDEV_CONFIG_SPARE) && vd->vdev_asize != 0)
546 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE, vd->vdev_asize);
547
548 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
549 vd == vd->vdev_top) {
550 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
551 vd->vdev_ms_array);
552 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
553 vd->vdev_ms_shift);
554 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
555 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
556 vd->vdev_asize);
557 fnvlist_add_uint64(nv, ZPOOL_CONFIG_MIN_ALLOC,
558 vdev_get_min_alloc(vd));
559 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
560 if (vd->vdev_noalloc) {
561 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
562 vd->vdev_noalloc);
563 }
564
565 /*
566 * Slog devices are removed synchronously so don't
567 * persist the vdev_removing flag to the label.
568 */
569 if (vd->vdev_removing && !vd->vdev_islog) {
570 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
571 vd->vdev_removing);
572 }
573
574 /* zpool command expects alloc class data */
575 if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
576 const char *bias = NULL;
577
578 switch (vd->vdev_alloc_bias) {
579 case VDEV_BIAS_LOG:
580 bias = VDEV_ALLOC_BIAS_LOG;
581 break;
582 case VDEV_BIAS_SPECIAL:
583 bias = VDEV_ALLOC_BIAS_SPECIAL;
584 break;
585 case VDEV_BIAS_DEDUP:
586 bias = VDEV_ALLOC_BIAS_DEDUP;
587 break;
588 default:
589 ASSERT3U(vd->vdev_alloc_bias, ==,
590 VDEV_BIAS_NONE);
591 }
592 fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
593 bias);
594 }
595 }
596
597 if (vd->vdev_dtl_sm != NULL) {
598 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
599 space_map_object(vd->vdev_dtl_sm));
600 }
601
602 if (vic->vic_mapping_object != 0) {
603 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
604 vic->vic_mapping_object);
605 }
606
607 if (vic->vic_births_object != 0) {
608 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
609 vic->vic_births_object);
610 }
611
612 if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
613 fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
614 vic->vic_prev_indirect_vdev);
615 }
616
617 if (vd->vdev_crtxg)
618 fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
619
620 if (vd->vdev_expansion_time)
621 fnvlist_add_uint64(nv, ZPOOL_CONFIG_EXPANSION_TIME,
622 vd->vdev_expansion_time);
623
624 if (flags & VDEV_CONFIG_MOS) {
625 if (vd->vdev_leaf_zap != 0) {
626 ASSERT(vd->vdev_ops->vdev_op_leaf);
627 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
628 vd->vdev_leaf_zap);
629 }
630
631 if (vd->vdev_top_zap != 0) {
632 ASSERT(vd == vd->vdev_top);
633 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
634 vd->vdev_top_zap);
635 }
636
637 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap != 0 &&
638 spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
639 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_ROOT_ZAP,
640 vd->vdev_root_zap);
641 }
642
643 if (vd->vdev_resilver_deferred) {
644 ASSERT(vd->vdev_ops->vdev_op_leaf);
645 ASSERT(spa->spa_resilver_deferred);
646 fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
647 }
648 }
649
650 if (getstats) {
651 vdev_config_generate_stats(vd, nv);
652
653 root_vdev_actions_getprogress(vd, nv);
654 top_vdev_actions_getprogress(vd, nv);
655
656 /*
657 * Note: this can be called from open context
658 * (spa_get_stats()), so we need the rwlock to prevent
659 * the mapping from being changed by condensing.
660 */
661 rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
662 if (vd->vdev_indirect_mapping != NULL) {
663 ASSERT(vd->vdev_indirect_births != NULL);
664 vdev_indirect_mapping_t *vim =
665 vd->vdev_indirect_mapping;
666 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
667 vdev_indirect_mapping_size(vim));
668 }
669 rw_exit(&vd->vdev_indirect_rwlock);
670 if (vd->vdev_mg != NULL &&
671 vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
672 /*
673 * Compute approximately how much memory would be used
674 * for the indirect mapping if this device were to
675 * be removed.
676 *
677 * Note: If the frag metric is invalid, then not
678 * enough metaslabs have been converted to have
679 * histograms.
680 */
681 uint64_t seg_count = 0;
682 uint64_t to_alloc = vd->vdev_stat.vs_alloc;
683
684 /*
685 * There are the same number of allocated segments
686 * as free segments, so we will have at least one
687 * entry per free segment. However, small free
688 * segments (smaller than vdev_removal_max_span)
689 * will be combined with adjacent allocated segments
690 * as a single mapping.
691 */
692 for (int i = 0; i < ZFS_RANGE_TREE_HISTOGRAM_SIZE;
693 i++) {
694 if (i + 1 < highbit64(vdev_removal_max_span)
695 - 1) {
696 to_alloc +=
697 vd->vdev_mg->mg_histogram[i] <<
698 (i + 1);
699 } else {
700 seg_count +=
701 vd->vdev_mg->mg_histogram[i];
702 }
703 }
704
705 /*
706 * The maximum length of a mapping is
707 * zfs_remove_max_segment, so we need at least one entry
708 * per zfs_remove_max_segment of allocated data.
709 */
710 seg_count += to_alloc / spa_remove_max_segment(spa);
711
712 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
713 seg_count *
714 sizeof (vdev_indirect_mapping_entry_phys_t));
715 }
716 }
717
718 if (!vd->vdev_ops->vdev_op_leaf) {
719 nvlist_t **child;
720 uint64_t c;
721
722 ASSERT(!vd->vdev_ishole);
723
724 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
725 KM_SLEEP);
726
727 for (c = 0; c < vd->vdev_children; c++) {
728 child[c] = vdev_config_generate(spa, vd->vdev_child[c],
729 getstats, flags);
730 }
731
732 fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
733 (const nvlist_t * const *)child, vd->vdev_children);
734
735 for (c = 0; c < vd->vdev_children; c++)
736 nvlist_free(child[c]);
737
738 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
739
740 } else {
741 const char *aux = NULL;
742
743 if (vd->vdev_offline && !vd->vdev_tmpoffline)
744 fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
745 if (vd->vdev_resilver_txg != 0)
746 fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
747 vd->vdev_resilver_txg);
748 if (vd->vdev_rebuild_txg != 0)
749 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
750 vd->vdev_rebuild_txg);
751 if (vd->vdev_faulted)
752 fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
753 if (vd->vdev_degraded)
754 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
755 if (vd->vdev_removed)
756 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
757 if (vd->vdev_unspare)
758 fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
759 if (vd->vdev_ishole)
760 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
761
762 /* Set the reason why we're FAULTED/DEGRADED. */
763 switch (vd->vdev_stat.vs_aux) {
764 case VDEV_AUX_ERR_EXCEEDED:
765 aux = "err_exceeded";
766 break;
767
768 case VDEV_AUX_EXTERNAL:
769 aux = "external";
770 break;
771 }
772
773 if (aux != NULL && !vd->vdev_tmpoffline) {
774 fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
775 } else {
776 /*
777 * We're healthy - clear any previous AUX_STATE values.
778 */
779 if (nvlist_exists(nv, ZPOOL_CONFIG_AUX_STATE))
780 nvlist_remove_all(nv, ZPOOL_CONFIG_AUX_STATE);
781 }
782
783 if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
784 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
785 vd->vdev_orig_guid);
786 }
787 }
788
789 return (nv);
790 }
791
792 /*
793 * Generate a view of the top-level vdevs. If we currently have holes
794 * in the namespace, then generate an array which contains a list of holey
795 * vdevs. Additionally, add the number of top-level children that currently
796 * exist.
797 */
798 void
vdev_top_config_generate(spa_t * spa,nvlist_t * config)799 vdev_top_config_generate(spa_t *spa, nvlist_t *config)
800 {
801 vdev_t *rvd = spa->spa_root_vdev;
802 uint64_t *array;
803 uint_t c, idx;
804
805 array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
806
807 for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
808 vdev_t *tvd = rvd->vdev_child[c];
809
810 if (tvd->vdev_ishole) {
811 array[idx++] = c;
812 }
813 }
814
815 if (idx) {
816 VERIFY0(nvlist_add_uint64_array(config,
817 ZPOOL_CONFIG_HOLE_ARRAY, array, idx));
818 }
819
820 VERIFY0(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
821 rvd->vdev_children));
822
823 kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
824 }
825
826 /*
827 * Returns the configuration from the label of the given vdev. For vdevs
828 * which don't have a txg value stored on their label (i.e. spares/cache)
829 * or have not been completely initialized (txg = 0) just return
830 * the configuration from the first valid label we find. Otherwise,
831 * find the most up-to-date label that does not exceed the specified
832 * 'txg' value.
833 *
834 * Only the labels named by the 'labels' mask are consulted, so that a caller
835 * which cannot vouch for the space some of them occupy is able to leave them
836 * out. See VDEV_LABELS_HEAD and friends.
837 */
838 nvlist_t *
vdev_label_read_config(vdev_t * vd,uint64_t txg,int labels)839 vdev_label_read_config(vdev_t *vd, uint64_t txg, int labels)
840 {
841 spa_t *spa = vd->vdev_spa;
842 nvlist_t *config = NULL;
843 vdev_phys_t *vp[VDEV_LABELS];
844 abd_t *vp_abd[VDEV_LABELS];
845 zio_t *zio[VDEV_LABELS];
846 uint64_t best_txg = 0;
847 uint64_t label_txg = 0;
848 int error = 0;
849 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
850 ZIO_FLAG_SPECULATIVE;
851
852 ASSERT(vd->vdev_validate_thread == curthread ||
853 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
854
855 if (!vdev_readable(vd))
856 return (NULL);
857
858 /*
859 * The label for a dRAID distributed spare is not stored on disk.
860 * Instead it is generated when needed which allows us to bypass
861 * the pipeline when reading the config from the label.
862 */
863 if (vd->vdev_ops == &vdev_draid_spare_ops)
864 return (vdev_draid_read_config_spare(vd));
865
866 for (int l = 0; l < VDEV_LABELS; l++) {
867 if (!(labels & (1 << l))) {
868 vp_abd[l] = NULL;
869 vp[l] = NULL;
870 continue;
871 }
872
873 vp_abd[l] = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
874 vp[l] = abd_to_buf(vp_abd[l]);
875 }
876
877 retry:
878 for (int l = 0; l < VDEV_LABELS; l++) {
879 if (!(labels & (1 << l)))
880 continue;
881
882 zio[l] = zio_root(spa, NULL, NULL, flags);
883
884 vdev_label_read(zio[l], vd, l, vp_abd[l],
885 offsetof(vdev_label_t, vl_vdev_phys), sizeof (vdev_phys_t),
886 NULL, NULL, flags);
887 }
888 for (int l = 0; l < VDEV_LABELS; l++) {
889 nvlist_t *label = NULL;
890
891 if (!(labels & (1 << l)))
892 continue;
893
894 if (zio_wait(zio[l]) == 0 &&
895 nvlist_unpack(vp[l]->vp_nvlist, sizeof (vp[l]->vp_nvlist),
896 &label, 0) == 0) {
897 /*
898 * Auxiliary vdevs won't have txg values in their
899 * labels and newly added vdevs may not have been
900 * completely initialized so just return the
901 * configuration from the first valid label we
902 * encounter.
903 */
904 error = nvlist_lookup_uint64(label,
905 ZPOOL_CONFIG_POOL_TXG, &label_txg);
906 if ((error || label_txg == 0) && !config) {
907 config = label;
908 for (l++; l < VDEV_LABELS; l++)
909 if (labels & (1 << l))
910 zio_wait(zio[l]);
911 break;
912 } else if (label_txg <= txg && label_txg > best_txg) {
913 best_txg = label_txg;
914 nvlist_free(config);
915 config = fnvlist_dup(label);
916 }
917 }
918
919 if (label != NULL) {
920 nvlist_free(label);
921 label = NULL;
922 }
923 }
924
925 if (config == NULL && !(flags & ZIO_FLAG_IO_RETRY)) {
926 flags |= ZIO_FLAG_IO_RETRY;
927 goto retry;
928 }
929
930 /*
931 * We found a valid label but it didn't pass txg restrictions.
932 */
933 if (config == NULL && label_txg != 0) {
934 vdev_dbgmsg(vd, "label discarded as txg is too large "
935 "(%llu > %llu)", (u_longlong_t)label_txg,
936 (u_longlong_t)txg);
937 }
938
939 for (int l = 0; l < VDEV_LABELS; l++) {
940 if (vp_abd[l] != NULL)
941 abd_free(vp_abd[l]);
942 }
943
944 return (config);
945 }
946
947 /*
948 * Determine if a device is in use. The 'spare_guid' parameter will be filled
949 * in with the device guid if this spare is active elsewhere on the system.
950 */
951 static boolean_t
vdev_inuse(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason,uint64_t * spare_guid,uint64_t * l2cache_guid)952 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
953 uint64_t *spare_guid, uint64_t *l2cache_guid)
954 {
955 spa_t *spa = vd->vdev_spa;
956 uint64_t state, pool_guid, device_guid, txg, spare_pool;
957 uint64_t vdtxg = 0;
958 nvlist_t *label;
959
960 if (spare_guid)
961 *spare_guid = 0ULL;
962 if (l2cache_guid)
963 *l2cache_guid = 0ULL;
964
965 /*
966 * Read the label, if any, and perform some basic sanity checks.
967 */
968 if ((label = vdev_label_read_config(vd, -1ULL,
969 VDEV_LABELS_ALL)) == NULL)
970 return (B_FALSE);
971
972 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
973 &vdtxg);
974
975 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
976 &state) != 0 ||
977 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
978 &device_guid) != 0) {
979 nvlist_free(label);
980 return (B_FALSE);
981 }
982
983 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
984 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
985 &pool_guid) != 0 ||
986 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
987 &txg) != 0)) {
988 nvlist_free(label);
989 return (B_FALSE);
990 }
991
992 nvlist_free(label);
993
994 /*
995 * Check to see if this device indeed belongs to the pool it claims to
996 * be a part of. The only way this is allowed is if the device is a hot
997 * spare (which we check for later on).
998 */
999 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1000 !spa_guid_exists(pool_guid, device_guid) &&
1001 !spa_spare_exists(device_guid, NULL, NULL) &&
1002 !spa_l2cache_exists(device_guid, NULL))
1003 return (B_FALSE);
1004
1005 /*
1006 * If the transaction group is zero, then this an initialized (but
1007 * unused) label. This is only an error if the create transaction
1008 * on-disk is the same as the one we're using now, in which case the
1009 * user has attempted to add the same vdev multiple times in the same
1010 * transaction.
1011 */
1012 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1013 txg == 0 && vdtxg == crtxg)
1014 return (B_TRUE);
1015
1016 /*
1017 * Check to see if this is a spare device. We do an explicit check for
1018 * spa_has_spare() here because it may be on our pending list of spares
1019 * to add.
1020 */
1021 if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
1022 spa_has_spare(spa, device_guid)) {
1023 if (spare_guid)
1024 *spare_guid = device_guid;
1025
1026 switch (reason) {
1027 case VDEV_LABEL_CREATE:
1028 return (B_TRUE);
1029
1030 case VDEV_LABEL_REPLACE:
1031 return (!spa_has_spare(spa, device_guid) ||
1032 spare_pool != 0ULL);
1033
1034 case VDEV_LABEL_SPARE:
1035 return (spa_has_spare(spa, device_guid));
1036 default:
1037 break;
1038 }
1039 }
1040
1041 /*
1042 * Check to see if this is an l2cache device.
1043 */
1044 if (spa_l2cache_exists(device_guid, NULL) ||
1045 spa_has_l2cache(spa, device_guid)) {
1046 if (l2cache_guid)
1047 *l2cache_guid = device_guid;
1048
1049 switch (reason) {
1050 case VDEV_LABEL_CREATE:
1051 return (B_TRUE);
1052
1053 case VDEV_LABEL_REPLACE:
1054 return (!spa_has_l2cache(spa, device_guid));
1055
1056 case VDEV_LABEL_L2CACHE:
1057 return (spa_has_l2cache(spa, device_guid));
1058 default:
1059 break;
1060 }
1061 }
1062
1063 /*
1064 * We can't rely on a pool's state if it's been imported
1065 * read-only. Instead we look to see if the pools is marked
1066 * read-only in the namespace and set the state to active.
1067 */
1068 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
1069 (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
1070 spa_mode(spa) == SPA_MODE_READ)
1071 state = POOL_STATE_ACTIVE;
1072
1073 /*
1074 * If the device is marked ACTIVE, then this device is in use by another
1075 * pool on the system.
1076 */
1077 return (state == POOL_STATE_ACTIVE);
1078 }
1079
1080 static nvlist_t *
vdev_aux_label_generate(vdev_t * vd,boolean_t reason_spare)1081 vdev_aux_label_generate(vdev_t *vd, boolean_t reason_spare)
1082 {
1083 /*
1084 * For inactive hot spares and level 2 ARC devices, we generate
1085 * a special label that identifies as a mutually shared hot
1086 * spare or l2cache device. We write the label in case of
1087 * addition or removal of hot spare or l2cache vdev (in which
1088 * case we want to revert the labels).
1089 */
1090 nvlist_t *label = fnvlist_alloc();
1091 fnvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
1092 spa_version(vd->vdev_spa));
1093 fnvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE, reason_spare ?
1094 POOL_STATE_SPARE : POOL_STATE_L2CACHE);
1095 fnvlist_add_uint64(label, ZPOOL_CONFIG_GUID, vd->vdev_guid);
1096
1097 /*
1098 * This is merely to facilitate reporting the ashift of the
1099 * cache device through zdb. The actual retrieval of the
1100 * ashift (in vdev_alloc()) uses the nvlist
1101 * spa->spa_l2cache->sav_config (populated in
1102 * spa_ld_open_aux_vdevs()).
1103 */
1104 if (!reason_spare)
1105 fnvlist_add_uint64(label, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
1106
1107 /*
1108 * Add path information to help find it during pool import
1109 */
1110 if (vd->vdev_path != NULL)
1111 fnvlist_add_string(label, ZPOOL_CONFIG_PATH, vd->vdev_path);
1112 if (vd->vdev_devid != NULL)
1113 fnvlist_add_string(label, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
1114 if (vd->vdev_physpath != NULL) {
1115 fnvlist_add_string(label, ZPOOL_CONFIG_PHYS_PATH,
1116 vd->vdev_physpath);
1117 }
1118 return (label);
1119 }
1120
1121 /*
1122 * Initialize a vdev label. We check to make sure each leaf device is not in
1123 * use, and writable. We put down an initial label which we will later
1124 * overwrite with a complete label. Note that it's important to do this
1125 * sequentially, not in parallel, so that we catch cases of multiple use of the
1126 * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
1127 * itself.
1128 */
1129 int
vdev_label_init(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason)1130 vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
1131 {
1132 spa_t *spa = vd->vdev_spa;
1133 nvlist_t *label;
1134 vdev_phys_t *vp;
1135 abd_t *vp_abd;
1136 abd_t *bootenv;
1137 uberblock_t *ub;
1138 abd_t *ub_abd;
1139 zio_t *zio;
1140 char *buf;
1141 size_t buflen;
1142 int error;
1143 uint64_t spare_guid = 0, l2cache_guid = 0;
1144 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1145 ZIO_FLAG_TRYHARD;
1146 boolean_t reason_spare = (reason == VDEV_LABEL_SPARE || (reason ==
1147 VDEV_LABEL_REMOVE && vd->vdev_isspare));
1148 boolean_t reason_l2cache = (reason == VDEV_LABEL_L2CACHE || (reason ==
1149 VDEV_LABEL_REMOVE && vd->vdev_isl2cache));
1150
1151 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1152
1153 for (int c = 0; c < vd->vdev_children; c++)
1154 if ((error = vdev_label_init(vd->vdev_child[c],
1155 crtxg, reason)) != 0)
1156 return (error);
1157
1158 /* Track the creation time for this vdev */
1159 vd->vdev_crtxg = crtxg;
1160
1161 if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
1162 return (0);
1163
1164 /*
1165 * Dead vdevs cannot be initialized.
1166 */
1167 if (vdev_is_dead(vd))
1168 return (SET_ERROR(EIO));
1169
1170 /*
1171 * Determine if the vdev is in use.
1172 */
1173 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
1174 vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid)) {
1175 if (spa->spa_create_info == NULL) {
1176 nvlist_t *nv = fnvlist_alloc();
1177 nvlist_t *cfg;
1178
1179 if (vd->vdev_path != NULL)
1180 fnvlist_add_string(nv,
1181 ZPOOL_CREATE_INFO_VDEV, vd->vdev_path);
1182
1183 cfg = vdev_label_read_config(vd, -1ULL,
1184 VDEV_LABELS_ALL);
1185 if (cfg != NULL) {
1186 const char *pname;
1187 if (nvlist_lookup_string(cfg,
1188 ZPOOL_CONFIG_POOL_NAME, &pname) == 0)
1189 fnvlist_add_string(nv,
1190 ZPOOL_CREATE_INFO_POOL, pname);
1191 nvlist_free(cfg);
1192 }
1193
1194 spa->spa_create_info = nv;
1195 }
1196 return (SET_ERROR(EBUSY));
1197 }
1198
1199 /*
1200 * If this is a request to add or replace a spare or l2cache device
1201 * that is in use elsewhere on the system, then we must update the
1202 * guid (which was initialized to a random value) to reflect the
1203 * actual GUID (which is shared between multiple pools).
1204 */
1205 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
1206 spare_guid != 0ULL) {
1207 uint64_t guid_delta = spare_guid - vd->vdev_guid;
1208
1209 vd->vdev_guid += guid_delta;
1210
1211 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1212 pvd->vdev_guid_sum += guid_delta;
1213
1214 /*
1215 * If this is a replacement, then we want to fallthrough to the
1216 * rest of the code. If we're adding a spare, then it's already
1217 * labeled appropriately and we can just return.
1218 */
1219 if (reason == VDEV_LABEL_SPARE)
1220 return (0);
1221 ASSERT(reason == VDEV_LABEL_REPLACE ||
1222 reason == VDEV_LABEL_SPLIT);
1223 }
1224
1225 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
1226 l2cache_guid != 0ULL) {
1227 uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
1228
1229 vd->vdev_guid += guid_delta;
1230
1231 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1232 pvd->vdev_guid_sum += guid_delta;
1233
1234 /*
1235 * If this is a replacement, then we want to fallthrough to the
1236 * rest of the code. If we're adding an l2cache, then it's
1237 * already labeled appropriately and we can just return.
1238 */
1239 if (reason == VDEV_LABEL_L2CACHE)
1240 return (0);
1241 ASSERT(reason == VDEV_LABEL_REPLACE);
1242 }
1243
1244 /*
1245 * Initialize its label.
1246 */
1247 vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1248 abd_zero(vp_abd, sizeof (vdev_phys_t));
1249 vp = abd_to_buf(vp_abd);
1250
1251 /*
1252 * Generate a label describing the pool and our top-level vdev.
1253 * We mark it as being from txg 0 to indicate that it's not
1254 * really part of an active pool just yet. The labels will
1255 * be written again with a meaningful txg by spa_sync().
1256 */
1257 if (reason_spare || reason_l2cache) {
1258 label = vdev_aux_label_generate(vd, reason_spare);
1259
1260 /*
1261 * When spare or l2cache (aux) vdev is added during pool
1262 * creation, spa->spa_uberblock is not written until this
1263 * point. Write it on next config sync.
1264 */
1265 if (uberblock_verify(&spa->spa_uberblock))
1266 spa->spa_aux_sync_uber = B_TRUE;
1267 } else {
1268 uint64_t txg = 0ULL;
1269
1270 if (reason == VDEV_LABEL_SPLIT)
1271 txg = spa->spa_uberblock.ub_txg;
1272 label = spa_config_generate(spa, vd, txg, B_FALSE);
1273
1274 /*
1275 * Add our creation time. This allows us to detect multiple
1276 * vdev uses as described above, and automatically expires if we
1277 * fail.
1278 */
1279 VERIFY0(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
1280 crtxg));
1281 }
1282
1283 buf = vp->vp_nvlist;
1284 buflen = sizeof (vp->vp_nvlist);
1285
1286 error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
1287 if (error != 0) {
1288 nvlist_free(label);
1289 abd_free(vp_abd);
1290 /* EFAULT means nvlist_pack ran out of room */
1291 return (SET_ERROR(error == EFAULT ? ENAMETOOLONG : EINVAL));
1292 }
1293
1294 /*
1295 * Initialize uberblock template.
1296 */
1297 ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
1298 abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
1299 abd_zero_off(ub_abd, sizeof (uberblock_t),
1300 VDEV_UBERBLOCK_RING - sizeof (uberblock_t));
1301 ub = abd_to_buf(ub_abd);
1302 ub->ub_txg = 0;
1303
1304 /* Initialize the 2nd padding area. */
1305 bootenv = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1306 abd_zero(bootenv, VDEV_PAD_SIZE);
1307
1308 /*
1309 * Write everything in parallel.
1310 */
1311 zio = zio_root(spa, NULL, NULL, flags);
1312
1313 for (int l = 0; l < VDEV_LABELS; l++) {
1314
1315 vdev_label_write(zio, vd, l, vp_abd,
1316 offsetof(vdev_label_t, vl_vdev_phys),
1317 sizeof (vdev_phys_t), NULL, NULL, flags);
1318
1319 /*
1320 * Skip the 1st padding area.
1321 * Zero out the 2nd padding area where it might have
1322 * left over data from previous filesystem format.
1323 */
1324 vdev_label_write(zio, vd, l, bootenv,
1325 offsetof(vdev_label_t, vl_be),
1326 VDEV_PAD_SIZE, NULL, NULL, flags);
1327
1328 vdev_label_write(zio, vd, l, ub_abd,
1329 offsetof(vdev_label_t, vl_uberblock),
1330 VDEV_UBERBLOCK_RING, NULL, NULL, flags);
1331 }
1332
1333 error = zio_wait(zio);
1334
1335 nvlist_free(label);
1336 abd_free(bootenv);
1337 abd_free(ub_abd);
1338 abd_free(vp_abd);
1339
1340 /*
1341 * If this vdev hasn't been previously identified as a spare, then we
1342 * mark it as such only if a) we are labeling it as a spare, or b) it
1343 * exists as a spare elsewhere in the system. Do the same for
1344 * level 2 ARC devices.
1345 */
1346 if (error == 0 && !vd->vdev_isspare &&
1347 (reason == VDEV_LABEL_SPARE ||
1348 spa_spare_exists(vd->vdev_guid, NULL, NULL)))
1349 spa_spare_add(vd);
1350
1351 if (error == 0 && !vd->vdev_isl2cache &&
1352 (reason == VDEV_LABEL_L2CACHE ||
1353 spa_l2cache_exists(vd->vdev_guid, NULL)))
1354 spa_l2cache_add(vd);
1355
1356 return (error);
1357 }
1358
1359 /*
1360 * Done callback for vdev_label_read_bootenv_impl. If this is the first
1361 * callback to finish, store our abd in the callback pointer. Otherwise, we
1362 * just free our abd and return.
1363 */
1364 static void
vdev_label_read_bootenv_done(zio_t * zio)1365 vdev_label_read_bootenv_done(zio_t *zio)
1366 {
1367 zio_t *rio = zio->io_private;
1368 abd_t **cbp = rio->io_private;
1369
1370 ASSERT3U(zio->io_size, ==, VDEV_PAD_SIZE);
1371
1372 if (zio->io_error == 0) {
1373 mutex_enter(&rio->io_lock);
1374 if (*cbp == NULL) {
1375 /* Will free this buffer in vdev_label_read_bootenv. */
1376 *cbp = zio->io_abd;
1377 } else {
1378 abd_free(zio->io_abd);
1379 }
1380 mutex_exit(&rio->io_lock);
1381 } else {
1382 abd_free(zio->io_abd);
1383 }
1384 }
1385
1386 static void
vdev_label_read_bootenv_impl(zio_t * zio,vdev_t * vd,int flags)1387 vdev_label_read_bootenv_impl(zio_t *zio, vdev_t *vd, int flags)
1388 {
1389 for (int c = 0; c < vd->vdev_children; c++)
1390 vdev_label_read_bootenv_impl(zio, vd->vdev_child[c], flags);
1391
1392 /*
1393 * We just use the first label that has a correct checksum; the
1394 * bootloader should have rewritten them all to be the same on boot,
1395 * and any changes we made since boot have been the same across all
1396 * labels.
1397 */
1398 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1399 for (int l = 0; l < VDEV_LABELS; l++) {
1400 vdev_label_read(zio, vd, l,
1401 abd_alloc_linear(VDEV_PAD_SIZE, B_FALSE),
1402 offsetof(vdev_label_t, vl_be), VDEV_PAD_SIZE,
1403 vdev_label_read_bootenv_done, zio, flags);
1404 }
1405 }
1406 }
1407
1408 int
vdev_label_read_bootenv(vdev_t * rvd,nvlist_t * bootenv)1409 vdev_label_read_bootenv(vdev_t *rvd, nvlist_t *bootenv)
1410 {
1411 nvlist_t *config;
1412 spa_t *spa = rvd->vdev_spa;
1413 abd_t *abd = NULL;
1414 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1415 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1416
1417 ASSERT(bootenv);
1418 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1419
1420 zio_t *zio = zio_root(spa, NULL, &abd, flags);
1421 vdev_label_read_bootenv_impl(zio, rvd, flags);
1422 int err = zio_wait(zio);
1423
1424 if (abd != NULL) {
1425 char *buf;
1426 vdev_boot_envblock_t *vbe = abd_to_buf(abd);
1427
1428 vbe->vbe_version = ntohll(vbe->vbe_version);
1429 switch (vbe->vbe_version) {
1430 case VB_RAW:
1431 /*
1432 * if we have textual data in vbe_bootenv, create nvlist
1433 * with key "envmap".
1434 */
1435 fnvlist_add_uint64(bootenv, BOOTENV_VERSION, VB_RAW);
1436 vbe->vbe_bootenv[sizeof (vbe->vbe_bootenv) - 1] = '\0';
1437 fnvlist_add_string(bootenv, GRUB_ENVMAP,
1438 vbe->vbe_bootenv);
1439 break;
1440
1441 case VB_NVLIST:
1442 err = nvlist_unpack(vbe->vbe_bootenv,
1443 sizeof (vbe->vbe_bootenv), &config, 0);
1444 if (err == 0) {
1445 fnvlist_merge(bootenv, config);
1446 nvlist_free(config);
1447 break;
1448 }
1449 zfs_fallthrough;
1450 default:
1451 /* Check for FreeBSD zfs bootonce command string */
1452 buf = abd_to_buf(abd);
1453 if (*buf == '\0') {
1454 fnvlist_add_uint64(bootenv, BOOTENV_VERSION,
1455 VB_NVLIST);
1456 break;
1457 }
1458 vbe->vbe_bootenv[sizeof (vbe->vbe_bootenv) - 1] = '\0';
1459 fnvlist_add_string(bootenv, FREEBSD_BOOTONCE, buf);
1460 }
1461
1462 /*
1463 * abd was allocated in vdev_label_read_bootenv_impl()
1464 */
1465 abd_free(abd);
1466 /*
1467 * If we managed to read any successfully,
1468 * return success.
1469 */
1470 return (0);
1471 }
1472 return (err);
1473 }
1474
1475 int
vdev_label_write_bootenv(vdev_t * vd,nvlist_t * env)1476 vdev_label_write_bootenv(vdev_t *vd, nvlist_t *env)
1477 {
1478 zio_t *zio;
1479 spa_t *spa = vd->vdev_spa;
1480 vdev_boot_envblock_t *bootenv;
1481 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1482 ZIO_FLAG_TRYHARD;
1483 int error;
1484 size_t nvsize;
1485 char *nvbuf;
1486 const char *tmp;
1487
1488 error = nvlist_size(env, &nvsize, NV_ENCODE_XDR);
1489 if (error != 0)
1490 return (SET_ERROR(error));
1491
1492 if (nvsize >= sizeof (bootenv->vbe_bootenv)) {
1493 return (SET_ERROR(E2BIG));
1494 }
1495
1496 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1497
1498 error = ENXIO;
1499 for (int c = 0; c < vd->vdev_children; c++) {
1500 int child_err;
1501
1502 child_err = vdev_label_write_bootenv(vd->vdev_child[c], env);
1503 /*
1504 * As long as any of the disks managed to write all of their
1505 * labels successfully, return success.
1506 */
1507 if (child_err == 0)
1508 error = child_err;
1509 }
1510
1511 if (!vd->vdev_ops->vdev_op_leaf || vdev_is_dead(vd) ||
1512 !vdev_writeable(vd)) {
1513 return (error);
1514 }
1515 ASSERT3U(sizeof (*bootenv), ==, VDEV_PAD_SIZE);
1516 abd_t *abd = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1517 abd_zero(abd, VDEV_PAD_SIZE);
1518
1519 bootenv = abd_borrow_buf_copy(abd, VDEV_PAD_SIZE);
1520 nvbuf = bootenv->vbe_bootenv;
1521 nvsize = sizeof (bootenv->vbe_bootenv);
1522
1523 bootenv->vbe_version = fnvlist_lookup_uint64(env, BOOTENV_VERSION);
1524 switch (bootenv->vbe_version) {
1525 case VB_RAW:
1526 if (nvlist_lookup_string(env, GRUB_ENVMAP, &tmp) == 0) {
1527 (void) strlcpy(bootenv->vbe_bootenv, tmp, nvsize);
1528 }
1529 error = 0;
1530 break;
1531
1532 case VB_NVLIST:
1533 error = nvlist_pack(env, &nvbuf, &nvsize, NV_ENCODE_XDR,
1534 KM_SLEEP);
1535 break;
1536
1537 default:
1538 error = EINVAL;
1539 break;
1540 }
1541
1542 if (error == 0) {
1543 bootenv->vbe_version = htonll(bootenv->vbe_version);
1544 abd_return_buf_copy(abd, bootenv, VDEV_PAD_SIZE);
1545 } else {
1546 abd_free(abd);
1547 return (SET_ERROR(error));
1548 }
1549
1550 zio = zio_root(spa, NULL, NULL, flags);
1551 for (int l = 0; l < VDEV_LABELS; l++) {
1552 vdev_label_write(zio, vd, l, abd,
1553 offsetof(vdev_label_t, vl_be),
1554 VDEV_PAD_SIZE, NULL, NULL, flags);
1555 }
1556
1557 error = zio_wait(zio);
1558
1559 abd_free(abd);
1560 return (error);
1561 }
1562
1563 /*
1564 * ==========================================================================
1565 * uberblock load/sync
1566 * ==========================================================================
1567 */
1568
1569 /*
1570 * Consider the following situation: txg is safely synced to disk. We've
1571 * written the first uberblock for txg + 1, and then we lose power. When we
1572 * come back up, we fail to see the uberblock for txg + 1 because, say,
1573 * it was on a mirrored device and the replica to which we wrote txg + 1
1574 * is now offline. If we then make some changes and sync txg + 1, and then
1575 * the missing replica comes back, then for a few seconds we'll have two
1576 * conflicting uberblocks on disk with the same txg. The solution is simple:
1577 * among uberblocks with equal txg, choose the one with the latest timestamp.
1578 */
1579 int
vdev_uberblock_compare(const uberblock_t * ub1,const uberblock_t * ub2)1580 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1581 {
1582 int cmp = TREE_CMP(ub1->ub_txg, ub2->ub_txg);
1583
1584 if (likely(cmp))
1585 return (cmp);
1586
1587 cmp = TREE_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1588 if (likely(cmp))
1589 return (cmp);
1590
1591 /*
1592 * If MMP_VALID(ub) && MMP_SEQ_VALID(ub) then the host has an MMP-aware
1593 * ZFS, e.g. OpenZFS >= 0.7.
1594 *
1595 * If one ub has MMP and the other does not, they were written by
1596 * different hosts, which matters for MMP. So we treat no MMP/no SEQ as
1597 * a 0 value.
1598 *
1599 * Since timestamp and txg are the same if we get this far, either is
1600 * acceptable for importing the pool.
1601 */
1602 unsigned int seq1 = 0;
1603 unsigned int seq2 = 0;
1604
1605 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1606 seq1 = MMP_SEQ(ub1);
1607
1608 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1609 seq2 = MMP_SEQ(ub2);
1610
1611 return (TREE_CMP(seq1, seq2));
1612 }
1613
1614 struct ubl_cbdata {
1615 uberblock_t ubl_latest; /* Most recent uberblock */
1616 uberblock_t *ubl_ubbest; /* Best uberblock (w/r/t max_txg) */
1617 vdev_t *ubl_vd; /* vdev associated with the above */
1618 };
1619
1620 static void
vdev_uberblock_load_done(zio_t * zio)1621 vdev_uberblock_load_done(zio_t *zio)
1622 {
1623 vdev_t *vd = zio->io_vd;
1624 spa_t *spa = zio->io_spa;
1625 zio_t *rio = zio->io_private;
1626 uberblock_t *ub = abd_to_buf(zio->io_abd);
1627 struct ubl_cbdata *cbp = rio->io_private;
1628
1629 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1630
1631 if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1632 mutex_enter(&rio->io_lock);
1633 if (vdev_uberblock_compare(ub, &cbp->ubl_latest) > 0) {
1634 cbp->ubl_latest = *ub;
1635 }
1636 if (ub->ub_txg <= spa->spa_load_max_txg &&
1637 vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1638 /*
1639 * Keep track of the vdev in which this uberblock
1640 * was found. We will use this information later
1641 * to obtain the config nvlist associated with
1642 * this uberblock.
1643 */
1644 *cbp->ubl_ubbest = *ub;
1645 cbp->ubl_vd = vd;
1646 }
1647 mutex_exit(&rio->io_lock);
1648 }
1649
1650 abd_free(zio->io_abd);
1651 }
1652
1653 static void
vdev_uberblock_load_impl(zio_t * zio,vdev_t * vd,int flags,struct ubl_cbdata * cbp)1654 vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1655 struct ubl_cbdata *cbp)
1656 {
1657 for (int c = 0; c < vd->vdev_children; c++)
1658 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1659
1660 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd) &&
1661 vd->vdev_ops != &vdev_draid_spare_ops) {
1662 int labels = VDEV_TRUSTED_LABELS(vd);
1663
1664 for (int l = 0; l < VDEV_LABELS; l++) {
1665 if (!(labels & (1 << l)))
1666 continue;
1667
1668 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1669 vdev_label_read(zio, vd, l,
1670 abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1671 B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1672 VDEV_UBERBLOCK_SIZE(vd),
1673 vdev_uberblock_load_done, zio, flags);
1674 }
1675 }
1676 }
1677 }
1678
1679 /*
1680 * Reads the 'best' uberblock from disk along with its associated
1681 * configuration. First, we read the uberblock array of each label of each
1682 * vdev, keeping track of the uberblock with the highest txg in each array.
1683 * Then, we read the configuration from the same vdev as the best uberblock.
1684 */
1685 void
vdev_uberblock_load(vdev_t * rvd,uberblock_t * ub,nvlist_t ** config)1686 vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1687 {
1688 zio_t *zio;
1689 spa_t *spa = rvd->vdev_spa;
1690 struct ubl_cbdata cb;
1691 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1692 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1693
1694 ASSERT(ub);
1695 ASSERT(config);
1696
1697 memset(ub, 0, sizeof (uberblock_t));
1698 memset(&cb, 0, sizeof (cb));
1699 *config = NULL;
1700
1701 cb.ubl_ubbest = ub;
1702
1703 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1704 zio = zio_root(spa, NULL, &cb, flags);
1705 vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1706 (void) zio_wait(zio);
1707
1708 /*
1709 * It's possible that the best uberblock was discovered on a label
1710 * that has a configuration which was written in a future txg.
1711 * Search all labels on this vdev to find the configuration that
1712 * matches the txg for our uberblock.
1713 */
1714 if (cb.ubl_vd != NULL) {
1715 vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s, "
1716 "txg=%llu seq=%llu", spa_load_name(spa),
1717 (u_longlong_t)ub->ub_txg,
1718 (u_longlong_t)(MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0));
1719
1720 if (ub->ub_raidz_reflow_info !=
1721 cb.ubl_latest.ub_raidz_reflow_info) {
1722 vdev_dbgmsg(cb.ubl_vd,
1723 "spa=%s best uberblock (txg=%llu info=0x%llx) "
1724 "has different raidz_reflow_info than latest "
1725 "uberblock (txg=%llu info=0x%llx)",
1726 spa_load_name(spa),
1727 (u_longlong_t)ub->ub_txg,
1728 (u_longlong_t)ub->ub_raidz_reflow_info,
1729 (u_longlong_t)cb.ubl_latest.ub_txg,
1730 (u_longlong_t)cb.ubl_latest.ub_raidz_reflow_info);
1731 memset(ub, 0, sizeof (uberblock_t));
1732 spa_config_exit(spa, SCL_ALL, FTAG);
1733 return;
1734 }
1735
1736 *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg,
1737 VDEV_TRUSTED_LABELS(cb.ubl_vd));
1738 if (*config == NULL && spa->spa_extreme_rewind) {
1739 vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
1740 "Trying again without txg restrictions.");
1741 *config = vdev_label_read_config(cb.ubl_vd,
1742 UINT64_MAX, VDEV_TRUSTED_LABELS(cb.ubl_vd));
1743 }
1744 if (*config == NULL) {
1745 vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
1746 }
1747 }
1748 spa_config_exit(spa, SCL_ALL, FTAG);
1749 }
1750
1751 /*
1752 * For use when a leaf vdev is expanded.
1753 * The location of labels 2 and 3 changed, and at the new location the
1754 * uberblock rings are either empty or contain garbage. The sync will write
1755 * new configs there because the vdev is dirty, but expansion also needs the
1756 * uberblock rings copied. Read them from label 0 which did not move.
1757 *
1758 * Since the point is to populate labels {2,3} with valid uberblocks,
1759 * we zero uberblocks we fail to read or which are not valid.
1760 */
1761
1762 static void
vdev_copy_uberblocks(vdev_t * vd)1763 vdev_copy_uberblocks(vdev_t *vd)
1764 {
1765 abd_t *ub_abd;
1766 zio_t *write_zio;
1767 int locks = (SCL_L2ARC | SCL_ZIO);
1768 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1769 ZIO_FLAG_SPECULATIVE;
1770
1771 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_READER) ==
1772 SCL_STATE);
1773 ASSERT(vd->vdev_ops->vdev_op_leaf);
1774
1775 /*
1776 * No uberblocks are stored on distributed spares, they may be
1777 * safely skipped when expanding a leaf vdev.
1778 */
1779 if (vd->vdev_ops == &vdev_draid_spare_ops)
1780 return;
1781
1782 spa_config_enter(vd->vdev_spa, locks, FTAG, RW_READER);
1783
1784 ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1785
1786 write_zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1787 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1788 const int src_label = 0;
1789 zio_t *zio;
1790
1791 zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1792 vdev_label_read(zio, vd, src_label, ub_abd,
1793 VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1794 NULL, NULL, flags);
1795
1796 if (zio_wait(zio) || uberblock_verify(abd_to_buf(ub_abd)))
1797 abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1798
1799 for (int l = 2; l < VDEV_LABELS; l++)
1800 vdev_label_write(write_zio, vd, l, ub_abd,
1801 VDEV_UBERBLOCK_OFFSET(vd, n),
1802 VDEV_UBERBLOCK_SIZE(vd), NULL, NULL,
1803 flags | ZIO_FLAG_DONT_PROPAGATE);
1804 }
1805 (void) zio_wait(write_zio);
1806
1807 spa_config_exit(vd->vdev_spa, locks, FTAG);
1808
1809 abd_free(ub_abd);
1810 }
1811
1812 /*
1813 * On success, increment root zio's count of good writes.
1814 * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1815 */
1816 static void
vdev_uberblock_sync_done(zio_t * zio)1817 vdev_uberblock_sync_done(zio_t *zio)
1818 {
1819 uint64_t *good_writes = zio->io_private;
1820
1821 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
1822 atomic_inc_64(good_writes);
1823 }
1824
1825 /*
1826 * Write the uberblock to all labels of all leaves of the specified vdev.
1827 */
1828 static void
vdev_uberblock_sync(zio_t * zio,uint64_t * good_writes,uberblock_t * ub,vdev_t * vd,int flags)1829 vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1830 uberblock_t *ub, vdev_t *vd, int flags)
1831 {
1832 for (uint64_t c = 0; c < vd->vdev_children; c++) {
1833 vdev_uberblock_sync(zio, good_writes,
1834 ub, vd->vdev_child[c], flags);
1835 }
1836
1837 if (!vd->vdev_ops->vdev_op_leaf)
1838 return;
1839
1840 if (!vdev_writeable(vd))
1841 return;
1842
1843 /*
1844 * There's no need to write uberblocks to a distributed spare, they
1845 * are already stored on all the leaves of the parent dRAID. For
1846 * this same reason vdev_uberblock_load_impl() skips distributed
1847 * spares when reading uberblocks.
1848 */
1849 if (vd->vdev_ops == &vdev_draid_spare_ops)
1850 return;
1851
1852 /*
1853 * If the vdev was expanded, or its trailing labels were found to hold
1854 * an older pool's, the rings there have to be rebuilt in full: the
1855 * write below only lands in one slot of each, and every slot it does
1856 * not touch keeps whatever was there, which for a foreign ring is a
1857 * set of uberblocks that can outrank our own for as long as it takes
1858 * the txg to come round again. Only once the copy has been made are
1859 * the trailing labels ours to read from.
1860 */
1861 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1862 (vd->vdev_copy_uberblocks || vd->vdev_tail_labels_foreign)) {
1863 vdev_copy_uberblocks(vd);
1864 vd->vdev_copy_uberblocks = B_FALSE;
1865 vd->vdev_tail_labels_foreign = B_FALSE;
1866 }
1867
1868 /*
1869 * We chose a slot based on the txg. If this uberblock has a special
1870 * RAIDZ expansion state, then it is essentially an update of the
1871 * current uberblock (it has the same txg). However, the current
1872 * state is committed, so we want to write it to a different slot. If
1873 * we overwrote the same slot, and we lose power during the uberblock
1874 * write, and the disk does not do single-sector overwrites
1875 * atomically (even though it is required to - i.e. we should see
1876 * either the old or the new uberblock), then we could lose this
1877 * txg's uberblock. Rewinding to the previous txg's uberblock may not
1878 * be possible because RAIDZ expansion may have already overwritten
1879 * some of the data, so we need the progress indicator in the
1880 * uberblock.
1881 */
1882 int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1883 int n = (ub->ub_txg - (RRSS_GET_STATE(ub) == RRSS_SCRATCH_VALID)) %
1884 (VDEV_UBERBLOCK_COUNT(vd) - m);
1885
1886 /* Copy the uberblock_t into the ABD */
1887 abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1888 abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1889 abd_zero_off(ub_abd, sizeof (uberblock_t),
1890 VDEV_UBERBLOCK_SIZE(vd) - sizeof (uberblock_t));
1891
1892 for (int l = 0; l < VDEV_LABELS; l++)
1893 vdev_label_write(zio, vd, l, ub_abd,
1894 VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1895 vdev_uberblock_sync_done, good_writes,
1896 flags | ZIO_FLAG_DONT_PROPAGATE);
1897
1898 abd_free(ub_abd);
1899 }
1900
1901 /* Sync the uberblocks to all vdevs in svd[] */
1902 int
vdev_uberblock_sync_list(spa_t * spa,vdev_t ** svd,int svdcount,uberblock_t * ub,int flags)1903 vdev_uberblock_sync_list(spa_t *spa, vdev_t **svd, int svdcount,
1904 uberblock_t *ub, int flags)
1905 {
1906 zio_t *zio;
1907 uint64_t good_writes = 0;
1908
1909 zio = zio_root(spa, NULL, NULL, flags);
1910
1911 for (int v = 0; v < svdcount; v++)
1912 vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1913
1914 if (spa->spa_aux_sync_uber) {
1915 for (int v = 0; v < spa->spa_spares.sav_count; v++) {
1916 vdev_uberblock_sync(zio, &good_writes, ub,
1917 spa->spa_spares.sav_vdevs[v], flags);
1918 }
1919 for (int v = 0; v < spa->spa_l2cache.sav_count; v++) {
1920 vdev_uberblock_sync(zio, &good_writes, ub,
1921 spa->spa_l2cache.sav_vdevs[v], flags);
1922 }
1923 }
1924 (void) zio_wait(zio);
1925
1926 /*
1927 * Flush the uberblocks to disk. This ensures that the odd labels
1928 * are no longer needed (because the new uberblocks and the even
1929 * labels are safely on disk), so it is safe to overwrite them.
1930 */
1931 zio = zio_root(spa, NULL, NULL, flags);
1932
1933 for (int v = 0; v < svdcount; v++) {
1934 if (vdev_writeable(svd[v])) {
1935 zio_flush(zio, svd[v]);
1936 }
1937 }
1938 if (spa->spa_aux_sync_uber) {
1939 spa->spa_aux_sync_uber = B_FALSE;
1940 for (int v = 0; v < spa->spa_spares.sav_count; v++) {
1941 if (vdev_writeable(spa->spa_spares.sav_vdevs[v])) {
1942 zio_flush(zio, spa->spa_spares.sav_vdevs[v]);
1943 }
1944 }
1945 for (int v = 0; v < spa->spa_l2cache.sav_count; v++) {
1946 if (vdev_writeable(spa->spa_l2cache.sav_vdevs[v])) {
1947 zio_flush(zio, spa->spa_l2cache.sav_vdevs[v]);
1948 }
1949 }
1950 }
1951
1952 (void) zio_wait(zio);
1953
1954 return (good_writes >= 1 ? 0 : EIO);
1955 }
1956
1957 /*
1958 * On success, increment the count of good writes for our top-level vdev.
1959 */
1960 static void
vdev_label_sync_done(zio_t * zio)1961 vdev_label_sync_done(zio_t *zio)
1962 {
1963 uint64_t *good_writes = zio->io_private;
1964
1965 if (zio->io_error == 0)
1966 atomic_inc_64(good_writes);
1967 }
1968
1969 /*
1970 * If there weren't enough good writes, indicate failure to the parent.
1971 */
1972 static void
vdev_label_sync_top_done(zio_t * zio)1973 vdev_label_sync_top_done(zio_t *zio)
1974 {
1975 uint64_t *good_writes = zio->io_private;
1976
1977 if (*good_writes == 0)
1978 zio->io_error = SET_ERROR(EIO);
1979
1980 kmem_free(good_writes, sizeof (uint64_t));
1981 }
1982
1983 /*
1984 * We ignore errors for log and cache devices, simply free the private data.
1985 */
1986 static void
vdev_label_sync_ignore_done(zio_t * zio)1987 vdev_label_sync_ignore_done(zio_t *zio)
1988 {
1989 kmem_free(zio->io_private, sizeof (uint64_t));
1990 }
1991
1992 /*
1993 * Write all even or odd labels to all leaves of the specified vdev.
1994 */
1995 static void
vdev_label_sync(zio_t * zio,uint64_t * good_writes,vdev_t * vd,int l,uint64_t txg,int flags)1996 vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1997 vdev_t *vd, int l, uint64_t txg, int flags)
1998 {
1999 nvlist_t *label;
2000 vdev_phys_t *vp;
2001 abd_t *vp_abd;
2002 char *buf;
2003 size_t buflen;
2004 vdev_t *pvd = vd->vdev_parent;
2005 boolean_t spare_in_use = B_FALSE;
2006
2007 for (int c = 0; c < vd->vdev_children; c++) {
2008 vdev_label_sync(zio, good_writes,
2009 vd->vdev_child[c], l, txg, flags);
2010 }
2011
2012 if (!vd->vdev_ops->vdev_op_leaf)
2013 return;
2014
2015 if (!vdev_writeable(vd))
2016 return;
2017
2018 /*
2019 * The top-level config never needs to be written to a distributed
2020 * spare. When read vdev_dspare_label_read_config() will generate
2021 * the config for the vdev_label_read_config().
2022 */
2023 if (vd->vdev_ops == &vdev_draid_spare_ops)
2024 return;
2025
2026 if (pvd && pvd->vdev_ops == &vdev_spare_ops)
2027 spare_in_use = B_TRUE;
2028
2029 /*
2030 * Generate a label describing the top-level config to which we belong.
2031 */
2032 if ((vd->vdev_isspare && !spare_in_use) || vd->vdev_isl2cache) {
2033 label = vdev_aux_label_generate(vd, vd->vdev_isspare);
2034 } else {
2035 label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
2036 }
2037
2038 vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
2039 abd_zero(vp_abd, sizeof (vdev_phys_t));
2040 vp = abd_to_buf(vp_abd);
2041
2042 buf = vp->vp_nvlist;
2043 buflen = sizeof (vp->vp_nvlist);
2044
2045 if (!nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP)) {
2046 for (; l < VDEV_LABELS; l += 2) {
2047 vdev_label_write(zio, vd, l, vp_abd,
2048 offsetof(vdev_label_t, vl_vdev_phys),
2049 sizeof (vdev_phys_t),
2050 vdev_label_sync_done, good_writes,
2051 flags | ZIO_FLAG_DONT_PROPAGATE);
2052 }
2053 }
2054
2055 abd_free(vp_abd);
2056 nvlist_free(label);
2057 }
2058
2059 static int
vdev_label_sync_list(spa_t * spa,int l,uint64_t txg,int flags)2060 vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
2061 {
2062 list_t *dl = &spa->spa_config_dirty_list;
2063 vdev_t *vd;
2064 zio_t *zio;
2065 int error;
2066
2067 /*
2068 * Write the new labels to disk.
2069 */
2070 zio = zio_root(spa, NULL, NULL, flags);
2071
2072 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
2073 uint64_t *good_writes;
2074
2075 ASSERT(!vd->vdev_ishole);
2076
2077 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
2078 zio_t *vio = zio_null(zio, spa, NULL,
2079 (vd->vdev_islog || vd->vdev_aux != NULL) ?
2080 vdev_label_sync_ignore_done : vdev_label_sync_top_done,
2081 good_writes, flags);
2082 vdev_label_sync(vio, good_writes, vd, l, txg, flags);
2083 zio_nowait(vio);
2084 }
2085
2086 /*
2087 * AUX path may have changed during import
2088 */
2089 spa_aux_vdev_t *sav[2] = {&spa->spa_spares, &spa->spa_l2cache};
2090 for (int i = 0; i < 2; i++) {
2091 for (int v = 0; v < sav[i]->sav_count; v++) {
2092 uint64_t *good_writes;
2093 if (!sav[i]->sav_label_sync)
2094 continue;
2095 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
2096 zio_t *vio = zio_null(zio, spa, NULL,
2097 vdev_label_sync_ignore_done, good_writes, flags);
2098 vdev_label_sync(vio, good_writes, sav[i]->sav_vdevs[v],
2099 l, txg, flags);
2100 zio_nowait(vio);
2101 }
2102 }
2103
2104 error = zio_wait(zio);
2105
2106 /*
2107 * Flush the new labels to disk.
2108 */
2109 zio = zio_root(spa, NULL, NULL, flags);
2110
2111 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
2112 zio_flush(zio, vd);
2113
2114 for (int i = 0; i < 2; i++) {
2115 if (!sav[i]->sav_label_sync)
2116 continue;
2117 for (int v = 0; v < sav[i]->sav_count; v++)
2118 zio_flush(zio, sav[i]->sav_vdevs[v]);
2119 if (l == 1)
2120 sav[i]->sav_label_sync = B_FALSE;
2121 }
2122
2123 (void) zio_wait(zio);
2124
2125 return (error);
2126 }
2127
2128 /*
2129 * Sync the uberblock and any changes to the vdev configuration.
2130 *
2131 * The order of operations is carefully crafted to ensure that
2132 * if the system panics or loses power at any time, the state on disk
2133 * is still transactionally consistent. The in-line comments below
2134 * describe the failure semantics at each stage.
2135 *
2136 * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
2137 * at any time, you can just call it again, and it will resume its work.
2138 */
2139 int
vdev_config_sync(spa_t * spa,vdev_t ** svd,int svdcount,uint64_t txg)2140 vdev_config_sync(spa_t *spa, vdev_t **svd, int svdcount, uint64_t txg)
2141 {
2142 uberblock_t *ub = &spa->spa_uberblock;
2143 int error = 0;
2144 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
2145
2146 ASSERT(svdcount != 0);
2147 retry:
2148 /*
2149 * Normally, we don't want to try too hard to write every label and
2150 * uberblock. If there is a flaky disk, we don't want the rest of the
2151 * sync process to block while we retry. But if we can't write a
2152 * single label out, we should retry with ZIO_FLAG_IO_RETRY before
2153 * bailing out and declaring the pool faulted.
2154 */
2155 if (error != 0) {
2156 if ((flags & ZIO_FLAG_IO_RETRY) != 0)
2157 return (error);
2158 flags |= ZIO_FLAG_IO_RETRY;
2159 }
2160
2161 ASSERT(ub->ub_txg <= txg);
2162
2163 /*
2164 * If this isn't a resync due to I/O errors,
2165 * and nothing changed in this transaction group,
2166 * and multihost protection isn't enabled,
2167 * and the vdev configuration hasn't changed,
2168 * then there's nothing to do.
2169 */
2170 if (ub->ub_txg < txg) {
2171 boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
2172 txg, spa->spa_mmp.mmp_delay);
2173
2174 if (!changed && list_is_empty(&spa->spa_config_dirty_list) &&
2175 !spa_multihost(spa))
2176 return (0);
2177 }
2178
2179 if (txg > spa_freeze_txg(spa))
2180 return (0);
2181
2182 ASSERT(txg <= spa->spa_final_txg);
2183
2184 /*
2185 * Flush the write cache of every disk that's been written to
2186 * in this transaction group. This ensures that all blocks
2187 * written in this txg will be committed to stable storage
2188 * before any uberblock that references them.
2189 */
2190 zio_t *zio = zio_root(spa, NULL, NULL, flags);
2191
2192 for (vdev_t *vd =
2193 txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
2194 vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
2195 zio_flush(zio, vd);
2196
2197 (void) zio_wait(zio);
2198
2199 /*
2200 * Sync out the even labels (L0, L2) for every dirty vdev. If the
2201 * system dies in the middle of this process, that's OK: all of the
2202 * even labels that made it to disk will be newer than any uberblock,
2203 * and will therefore be considered invalid. The odd labels (L1, L3),
2204 * which have not yet been touched, will still be valid. We flush
2205 * the new labels to disk to ensure that all even-label updates
2206 * are committed to stable storage before the uberblock update.
2207 */
2208 if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
2209 if ((flags & ZIO_FLAG_IO_RETRY) != 0) {
2210 zfs_dbgmsg("vdev_label_sync_list() returned error %d "
2211 "for pool '%s' when syncing out the even labels "
2212 "of dirty vdevs", error, spa_name(spa));
2213 }
2214 goto retry;
2215 }
2216
2217 /*
2218 * Sync the uberblocks to all vdevs in svd[].
2219 * If the system dies in the middle of this step, there are two cases
2220 * to consider, and the on-disk state is consistent either way:
2221 *
2222 * (1) If none of the new uberblocks made it to disk, then the
2223 * previous uberblock will be the newest, and the odd labels
2224 * (which had not yet been touched) will be valid with respect
2225 * to that uberblock.
2226 *
2227 * (2) If one or more new uberblocks made it to disk, then they
2228 * will be the newest, and the even labels (which had all
2229 * been successfully committed) will be valid with respect
2230 * to the new uberblocks.
2231 */
2232 if ((error = vdev_uberblock_sync_list(spa, svd, svdcount, ub,
2233 flags)) != 0) {
2234 if ((flags & ZIO_FLAG_IO_RETRY) != 0) {
2235 zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
2236 "%d for pool '%s'", error, spa_name(spa));
2237 }
2238 goto retry;
2239 }
2240
2241 if (spa_multihost(spa))
2242 mmp_update_uberblock(spa, ub);
2243
2244 /*
2245 * Sync out odd labels for every dirty vdev. If the system dies
2246 * in the middle of this process, the even labels and the new
2247 * uberblocks will suffice to open the pool. The next time
2248 * the pool is opened, the first thing we'll do -- before any
2249 * user data is modified -- is mark every vdev dirty so that
2250 * all labels will be brought up to date. We flush the new labels
2251 * to disk to ensure that all odd-label updates are committed to
2252 * stable storage before the next transaction group begins.
2253 */
2254 if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
2255 if ((flags & ZIO_FLAG_IO_RETRY) != 0) {
2256 zfs_dbgmsg("vdev_label_sync_list() returned error %d "
2257 "for pool '%s' when syncing out the odd labels of "
2258 "dirty vdevs", error, spa_name(spa));
2259 }
2260 goto retry;
2261 }
2262
2263 return (0);
2264 }
2265