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