]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_label.c
Code improvement and bug fixes for QAT support
[mirror_zfs.git] / module / zfs / vdev_label.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2012, 2016 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/uberblock_impl.h>
147 #include <sys/metaslab.h>
148 #include <sys/metaslab_impl.h>
149 #include <sys/zio.h>
150 #include <sys/dsl_scan.h>
151 #include <sys/abd.h>
152 #include <sys/fs/zfs.h>
153
154 /*
155 * Basic routines to read and write from a vdev label.
156 * Used throughout the rest of this file.
157 */
158 uint64_t
159 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
160 {
161 ASSERT(offset < sizeof (vdev_label_t));
162 ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
163
164 return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
165 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
166 }
167
168 /*
169 * Returns back the vdev label associated with the passed in offset.
170 */
171 int
172 vdev_label_number(uint64_t psize, uint64_t offset)
173 {
174 int l;
175
176 if (offset >= psize - VDEV_LABEL_END_SIZE) {
177 offset -= psize - VDEV_LABEL_END_SIZE;
178 offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
179 }
180 l = offset / sizeof (vdev_label_t);
181 return (l < VDEV_LABELS ? l : -1);
182 }
183
184 static void
185 vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
186 uint64_t size, zio_done_func_t *done, void *private, int flags)
187 {
188 ASSERT(
189 spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
190 spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
191 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
192
193 zio_nowait(zio_read_phys(zio, vd,
194 vdev_label_offset(vd->vdev_psize, l, offset),
195 size, buf, ZIO_CHECKSUM_LABEL, done, private,
196 ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
197 }
198
199 void
200 vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
201 uint64_t size, zio_done_func_t *done, void *private, int flags)
202 {
203 ASSERT(
204 spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
205 spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
206 ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
207
208 zio_nowait(zio_write_phys(zio, vd,
209 vdev_label_offset(vd->vdev_psize, l, offset),
210 size, buf, ZIO_CHECKSUM_LABEL, done, private,
211 ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
212 }
213
214 /*
215 * Generate the nvlist representing this vdev's stats
216 */
217 void
218 vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
219 {
220 nvlist_t *nvx;
221 vdev_stat_t *vs;
222 vdev_stat_ex_t *vsx;
223
224 vs = kmem_alloc(sizeof (*vs), KM_SLEEP);
225 vsx = kmem_alloc(sizeof (*vsx), KM_SLEEP);
226
227 vdev_get_stats_ex(vd, vs, vsx);
228 fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
229 (uint64_t *)vs, sizeof (*vs) / sizeof (uint64_t));
230
231 /*
232 * Add extended stats into a special extended stats nvlist. This keeps
233 * all the extended stats nicely grouped together. The extended stats
234 * nvlist is then added to the main nvlist.
235 */
236 nvx = fnvlist_alloc();
237
238 /* ZIOs in flight to disk */
239 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
240 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_READ]);
241
242 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
243 vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_WRITE]);
244
245 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
246 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_READ]);
247
248 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
249 vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_WRITE]);
250
251 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
252 vsx->vsx_active_queue[ZIO_PRIORITY_SCRUB]);
253
254 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
255 vsx->vsx_active_queue[ZIO_PRIORITY_TRIM]);
256
257 /* ZIOs pending */
258 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
259 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_READ]);
260
261 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
262 vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_WRITE]);
263
264 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
265 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_READ]);
266
267 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
268 vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_WRITE]);
269
270 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
271 vsx->vsx_pend_queue[ZIO_PRIORITY_SCRUB]);
272
273 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
274 vsx->vsx_pend_queue[ZIO_PRIORITY_TRIM]);
275
276 /* Histograms */
277 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
278 vsx->vsx_total_histo[ZIO_TYPE_READ],
279 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_READ]));
280
281 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
282 vsx->vsx_total_histo[ZIO_TYPE_WRITE],
283 ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_WRITE]));
284
285 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
286 vsx->vsx_disk_histo[ZIO_TYPE_READ],
287 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_READ]));
288
289 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
290 vsx->vsx_disk_histo[ZIO_TYPE_WRITE],
291 ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_WRITE]));
292
293 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
294 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ],
295 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ]));
296
297 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
298 vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE],
299 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE]));
300
301 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
302 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ],
303 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ]));
304
305 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
306 vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE],
307 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE]));
308
309 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
310 vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB],
311 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB]));
312
313 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
314 vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM],
315 ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM]));
316
317 /* Request sizes */
318 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
319 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ],
320 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ]));
321
322 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
323 vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE],
324 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE]));
325
326 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
327 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ],
328 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ]));
329
330 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
331 vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE],
332 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE]));
333
334 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
335 vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB],
336 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB]));
337
338 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
339 vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM],
340 ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM]));
341
342 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
343 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ],
344 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ]));
345
346 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
347 vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE],
348 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE]));
349
350 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
351 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ],
352 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ]));
353
354 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
355 vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE],
356 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE]));
357
358 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
359 vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
360 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
361
362 fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
363 vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM],
364 ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM]));
365
366 /* IO delays */
367 fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SLOW_IOS, vs->vs_slow_ios);
368
369 /* Add extended stats nvlist to main nvlist */
370 fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
371
372 fnvlist_free(nvx);
373 kmem_free(vs, sizeof (*vs));
374 kmem_free(vsx, sizeof (*vsx));
375 }
376
377 static void
378 root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
379 {
380 spa_t *spa = vd->vdev_spa;
381
382 if (vd != spa->spa_root_vdev)
383 return;
384
385 /* provide either current or previous scan information */
386 pool_scan_stat_t ps;
387 if (spa_scan_get_stats(spa, &ps) == 0) {
388 fnvlist_add_uint64_array(nvl,
389 ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
390 sizeof (pool_scan_stat_t) / sizeof (uint64_t));
391 }
392
393 pool_removal_stat_t prs;
394 if (spa_removal_get_stats(spa, &prs) == 0) {
395 fnvlist_add_uint64_array(nvl,
396 ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
397 sizeof (prs) / sizeof (uint64_t));
398 }
399
400 pool_checkpoint_stat_t pcs;
401 if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
402 fnvlist_add_uint64_array(nvl,
403 ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
404 sizeof (pcs) / sizeof (uint64_t));
405 }
406 }
407
408 /*
409 * Generate the nvlist representing this vdev's config.
410 */
411 nvlist_t *
412 vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
413 vdev_config_flag_t flags)
414 {
415 nvlist_t *nv = NULL;
416 vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
417
418 nv = fnvlist_alloc();
419
420 fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
421 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
422 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
423 fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
424
425 if (vd->vdev_path != NULL)
426 fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
427
428 if (vd->vdev_devid != NULL)
429 fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
430
431 if (vd->vdev_physpath != NULL)
432 fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
433 vd->vdev_physpath);
434
435 if (vd->vdev_enc_sysfs_path != NULL)
436 fnvlist_add_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
437 vd->vdev_enc_sysfs_path);
438
439 if (vd->vdev_fru != NULL)
440 fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
441
442 if (vd->vdev_nparity != 0) {
443 ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
444 VDEV_TYPE_RAIDZ) == 0);
445
446 /*
447 * Make sure someone hasn't managed to sneak a fancy new vdev
448 * into a crufty old storage pool.
449 */
450 ASSERT(vd->vdev_nparity == 1 ||
451 (vd->vdev_nparity <= 2 &&
452 spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
453 (vd->vdev_nparity <= 3 &&
454 spa_version(spa) >= SPA_VERSION_RAIDZ3));
455
456 /*
457 * Note that we'll add the nparity tag even on storage pools
458 * that only support a single parity device -- older software
459 * will just ignore it.
460 */
461 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
462 }
463
464 if (vd->vdev_wholedisk != -1ULL)
465 fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
466 vd->vdev_wholedisk);
467
468 if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
469 fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
470
471 if (vd->vdev_isspare)
472 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
473
474 if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
475 vd == vd->vdev_top) {
476 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
477 vd->vdev_ms_array);
478 fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
479 vd->vdev_ms_shift);
480 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
481 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
482 vd->vdev_asize);
483 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
484 if (vd->vdev_removing) {
485 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
486 vd->vdev_removing);
487 }
488
489 /* zpool command expects alloc class data */
490 if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
491 const char *bias = NULL;
492
493 switch (vd->vdev_alloc_bias) {
494 case VDEV_BIAS_LOG:
495 bias = VDEV_ALLOC_BIAS_LOG;
496 break;
497 case VDEV_BIAS_SPECIAL:
498 bias = VDEV_ALLOC_BIAS_SPECIAL;
499 break;
500 case VDEV_BIAS_DEDUP:
501 bias = VDEV_ALLOC_BIAS_DEDUP;
502 break;
503 default:
504 ASSERT3U(vd->vdev_alloc_bias, ==,
505 VDEV_BIAS_NONE);
506 }
507 fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
508 bias);
509 }
510 }
511
512 if (vd->vdev_dtl_sm != NULL) {
513 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
514 space_map_object(vd->vdev_dtl_sm));
515 }
516
517 if (vic->vic_mapping_object != 0) {
518 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
519 vic->vic_mapping_object);
520 }
521
522 if (vic->vic_births_object != 0) {
523 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
524 vic->vic_births_object);
525 }
526
527 if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
528 fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
529 vic->vic_prev_indirect_vdev);
530 }
531
532 if (vd->vdev_crtxg)
533 fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
534
535 if (vd->vdev_expansion_time)
536 fnvlist_add_uint64(nv, ZPOOL_CONFIG_EXPANSION_TIME,
537 vd->vdev_expansion_time);
538
539 if (flags & VDEV_CONFIG_MOS) {
540 if (vd->vdev_leaf_zap != 0) {
541 ASSERT(vd->vdev_ops->vdev_op_leaf);
542 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
543 vd->vdev_leaf_zap);
544 }
545
546 if (vd->vdev_top_zap != 0) {
547 ASSERT(vd == vd->vdev_top);
548 fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
549 vd->vdev_top_zap);
550 }
551
552 if (vd->vdev_resilver_deferred) {
553 ASSERT(vd->vdev_ops->vdev_op_leaf);
554 ASSERT(spa->spa_resilver_deferred);
555 fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
556 }
557 }
558
559 if (getstats) {
560 vdev_config_generate_stats(vd, nv);
561
562 root_vdev_actions_getprogress(vd, nv);
563
564 /*
565 * Note: this can be called from open context
566 * (spa_get_stats()), so we need the rwlock to prevent
567 * the mapping from being changed by condensing.
568 */
569 rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
570 if (vd->vdev_indirect_mapping != NULL) {
571 ASSERT(vd->vdev_indirect_births != NULL);
572 vdev_indirect_mapping_t *vim =
573 vd->vdev_indirect_mapping;
574 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
575 vdev_indirect_mapping_size(vim));
576 }
577 rw_exit(&vd->vdev_indirect_rwlock);
578 if (vd->vdev_mg != NULL &&
579 vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
580 /*
581 * Compute approximately how much memory would be used
582 * for the indirect mapping if this device were to
583 * be removed.
584 *
585 * Note: If the frag metric is invalid, then not
586 * enough metaslabs have been converted to have
587 * histograms.
588 */
589 uint64_t seg_count = 0;
590 uint64_t to_alloc = vd->vdev_stat.vs_alloc;
591
592 /*
593 * There are the same number of allocated segments
594 * as free segments, so we will have at least one
595 * entry per free segment. However, small free
596 * segments (smaller than vdev_removal_max_span)
597 * will be combined with adjacent allocated segments
598 * as a single mapping.
599 */
600 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
601 if (1ULL << (i + 1) < vdev_removal_max_span) {
602 to_alloc +=
603 vd->vdev_mg->mg_histogram[i] <<
604 (i + 1);
605 } else {
606 seg_count +=
607 vd->vdev_mg->mg_histogram[i];
608 }
609 }
610
611 /*
612 * The maximum length of a mapping is
613 * zfs_remove_max_segment, so we need at least one entry
614 * per zfs_remove_max_segment of allocated data.
615 */
616 seg_count += to_alloc / zfs_remove_max_segment;
617
618 fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
619 seg_count *
620 sizeof (vdev_indirect_mapping_entry_phys_t));
621 }
622 }
623
624 if (!vd->vdev_ops->vdev_op_leaf) {
625 nvlist_t **child;
626 int c, idx;
627
628 ASSERT(!vd->vdev_ishole);
629
630 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
631 KM_SLEEP);
632
633 for (c = 0, idx = 0; c < vd->vdev_children; c++) {
634 vdev_t *cvd = vd->vdev_child[c];
635
636 /*
637 * If we're generating an nvlist of removing
638 * vdevs then skip over any device which is
639 * not being removed.
640 */
641 if ((flags & VDEV_CONFIG_REMOVING) &&
642 !cvd->vdev_removing)
643 continue;
644
645 child[idx++] = vdev_config_generate(spa, cvd,
646 getstats, flags);
647 }
648
649 if (idx) {
650 fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
651 child, idx);
652 }
653
654 for (c = 0; c < idx; c++)
655 nvlist_free(child[c]);
656
657 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
658
659 } else {
660 const char *aux = NULL;
661
662 if (vd->vdev_offline && !vd->vdev_tmpoffline)
663 fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
664 if (vd->vdev_resilver_txg != 0)
665 fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
666 vd->vdev_resilver_txg);
667 if (vd->vdev_faulted)
668 fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
669 if (vd->vdev_degraded)
670 fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
671 if (vd->vdev_removed)
672 fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
673 if (vd->vdev_unspare)
674 fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
675 if (vd->vdev_ishole)
676 fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
677
678 /* Set the reason why we're FAULTED/DEGRADED. */
679 switch (vd->vdev_stat.vs_aux) {
680 case VDEV_AUX_ERR_EXCEEDED:
681 aux = "err_exceeded";
682 break;
683
684 case VDEV_AUX_EXTERNAL:
685 aux = "external";
686 break;
687 }
688
689 if (aux != NULL && !vd->vdev_tmpoffline) {
690 fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
691 } else {
692 /*
693 * We're healthy - clear any previous AUX_STATE values.
694 */
695 if (nvlist_exists(nv, ZPOOL_CONFIG_AUX_STATE))
696 nvlist_remove_all(nv, ZPOOL_CONFIG_AUX_STATE);
697 }
698
699 if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
700 fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
701 vd->vdev_orig_guid);
702 }
703 }
704
705 return (nv);
706 }
707
708 /*
709 * Generate a view of the top-level vdevs. If we currently have holes
710 * in the namespace, then generate an array which contains a list of holey
711 * vdevs. Additionally, add the number of top-level children that currently
712 * exist.
713 */
714 void
715 vdev_top_config_generate(spa_t *spa, nvlist_t *config)
716 {
717 vdev_t *rvd = spa->spa_root_vdev;
718 uint64_t *array;
719 uint_t c, idx;
720
721 array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
722
723 for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
724 vdev_t *tvd = rvd->vdev_child[c];
725
726 if (tvd->vdev_ishole) {
727 array[idx++] = c;
728 }
729 }
730
731 if (idx) {
732 VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
733 array, idx) == 0);
734 }
735
736 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
737 rvd->vdev_children) == 0);
738
739 kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
740 }
741
742 /*
743 * Returns the configuration from the label of the given vdev. For vdevs
744 * which don't have a txg value stored on their label (i.e. spares/cache)
745 * or have not been completely initialized (txg = 0) just return
746 * the configuration from the first valid label we find. Otherwise,
747 * find the most up-to-date label that does not exceed the specified
748 * 'txg' value.
749 */
750 nvlist_t *
751 vdev_label_read_config(vdev_t *vd, uint64_t txg)
752 {
753 spa_t *spa = vd->vdev_spa;
754 nvlist_t *config = NULL;
755 vdev_phys_t *vp;
756 abd_t *vp_abd;
757 zio_t *zio;
758 uint64_t best_txg = 0;
759 uint64_t label_txg = 0;
760 int error = 0;
761 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
762 ZIO_FLAG_SPECULATIVE;
763
764 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
765
766 if (!vdev_readable(vd))
767 return (NULL);
768
769 vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
770 vp = abd_to_buf(vp_abd);
771
772 retry:
773 for (int l = 0; l < VDEV_LABELS; l++) {
774 nvlist_t *label = NULL;
775
776 zio = zio_root(spa, NULL, NULL, flags);
777
778 vdev_label_read(zio, vd, l, vp_abd,
779 offsetof(vdev_label_t, vl_vdev_phys),
780 sizeof (vdev_phys_t), NULL, NULL, flags);
781
782 if (zio_wait(zio) == 0 &&
783 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
784 &label, 0) == 0) {
785 /*
786 * Auxiliary vdevs won't have txg values in their
787 * labels and newly added vdevs may not have been
788 * completely initialized so just return the
789 * configuration from the first valid label we
790 * encounter.
791 */
792 error = nvlist_lookup_uint64(label,
793 ZPOOL_CONFIG_POOL_TXG, &label_txg);
794 if ((error || label_txg == 0) && !config) {
795 config = label;
796 break;
797 } else if (label_txg <= txg && label_txg > best_txg) {
798 best_txg = label_txg;
799 nvlist_free(config);
800 config = fnvlist_dup(label);
801 }
802 }
803
804 if (label != NULL) {
805 nvlist_free(label);
806 label = NULL;
807 }
808 }
809
810 if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
811 flags |= ZIO_FLAG_TRYHARD;
812 goto retry;
813 }
814
815 /*
816 * We found a valid label but it didn't pass txg restrictions.
817 */
818 if (config == NULL && label_txg != 0) {
819 vdev_dbgmsg(vd, "label discarded as txg is too large "
820 "(%llu > %llu)", (u_longlong_t)label_txg,
821 (u_longlong_t)txg);
822 }
823
824 abd_free(vp_abd);
825
826 return (config);
827 }
828
829 /*
830 * Determine if a device is in use. The 'spare_guid' parameter will be filled
831 * in with the device guid if this spare is active elsewhere on the system.
832 */
833 static boolean_t
834 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
835 uint64_t *spare_guid, uint64_t *l2cache_guid)
836 {
837 spa_t *spa = vd->vdev_spa;
838 uint64_t state, pool_guid, device_guid, txg, spare_pool;
839 uint64_t vdtxg = 0;
840 nvlist_t *label;
841
842 if (spare_guid)
843 *spare_guid = 0ULL;
844 if (l2cache_guid)
845 *l2cache_guid = 0ULL;
846
847 /*
848 * Read the label, if any, and perform some basic sanity checks.
849 */
850 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
851 return (B_FALSE);
852
853 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
854 &vdtxg);
855
856 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
857 &state) != 0 ||
858 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
859 &device_guid) != 0) {
860 nvlist_free(label);
861 return (B_FALSE);
862 }
863
864 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
865 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
866 &pool_guid) != 0 ||
867 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
868 &txg) != 0)) {
869 nvlist_free(label);
870 return (B_FALSE);
871 }
872
873 nvlist_free(label);
874
875 /*
876 * Check to see if this device indeed belongs to the pool it claims to
877 * be a part of. The only way this is allowed is if the device is a hot
878 * spare (which we check for later on).
879 */
880 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
881 !spa_guid_exists(pool_guid, device_guid) &&
882 !spa_spare_exists(device_guid, NULL, NULL) &&
883 !spa_l2cache_exists(device_guid, NULL))
884 return (B_FALSE);
885
886 /*
887 * If the transaction group is zero, then this an initialized (but
888 * unused) label. This is only an error if the create transaction
889 * on-disk is the same as the one we're using now, in which case the
890 * user has attempted to add the same vdev multiple times in the same
891 * transaction.
892 */
893 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
894 txg == 0 && vdtxg == crtxg)
895 return (B_TRUE);
896
897 /*
898 * Check to see if this is a spare device. We do an explicit check for
899 * spa_has_spare() here because it may be on our pending list of spares
900 * to add. We also check if it is an l2cache device.
901 */
902 if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
903 spa_has_spare(spa, device_guid)) {
904 if (spare_guid)
905 *spare_guid = device_guid;
906
907 switch (reason) {
908 case VDEV_LABEL_CREATE:
909 case VDEV_LABEL_L2CACHE:
910 return (B_TRUE);
911
912 case VDEV_LABEL_REPLACE:
913 return (!spa_has_spare(spa, device_guid) ||
914 spare_pool != 0ULL);
915
916 case VDEV_LABEL_SPARE:
917 return (spa_has_spare(spa, device_guid));
918 default:
919 break;
920 }
921 }
922
923 /*
924 * Check to see if this is an l2cache device.
925 */
926 if (spa_l2cache_exists(device_guid, NULL))
927 return (B_TRUE);
928
929 /*
930 * We can't rely on a pool's state if it's been imported
931 * read-only. Instead we look to see if the pools is marked
932 * read-only in the namespace and set the state to active.
933 */
934 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
935 (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
936 spa_mode(spa) == FREAD)
937 state = POOL_STATE_ACTIVE;
938
939 /*
940 * If the device is marked ACTIVE, then this device is in use by another
941 * pool on the system.
942 */
943 return (state == POOL_STATE_ACTIVE);
944 }
945
946 /*
947 * Initialize a vdev label. We check to make sure each leaf device is not in
948 * use, and writable. We put down an initial label which we will later
949 * overwrite with a complete label. Note that it's important to do this
950 * sequentially, not in parallel, so that we catch cases of multiple use of the
951 * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
952 * itself.
953 */
954 int
955 vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
956 {
957 spa_t *spa = vd->vdev_spa;
958 nvlist_t *label;
959 vdev_phys_t *vp;
960 abd_t *vp_abd;
961 abd_t *pad2;
962 uberblock_t *ub;
963 abd_t *ub_abd;
964 zio_t *zio;
965 char *buf;
966 size_t buflen;
967 int error;
968 uint64_t spare_guid = 0, l2cache_guid = 0;
969 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
970
971 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
972
973 for (int c = 0; c < vd->vdev_children; c++)
974 if ((error = vdev_label_init(vd->vdev_child[c],
975 crtxg, reason)) != 0)
976 return (error);
977
978 /* Track the creation time for this vdev */
979 vd->vdev_crtxg = crtxg;
980
981 if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
982 return (0);
983
984 /*
985 * Dead vdevs cannot be initialized.
986 */
987 if (vdev_is_dead(vd))
988 return (SET_ERROR(EIO));
989
990 /*
991 * Determine if the vdev is in use.
992 */
993 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
994 vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
995 return (SET_ERROR(EBUSY));
996
997 /*
998 * If this is a request to add or replace a spare or l2cache device
999 * that is in use elsewhere on the system, then we must update the
1000 * guid (which was initialized to a random value) to reflect the
1001 * actual GUID (which is shared between multiple pools).
1002 */
1003 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
1004 spare_guid != 0ULL) {
1005 uint64_t guid_delta = spare_guid - vd->vdev_guid;
1006
1007 vd->vdev_guid += guid_delta;
1008
1009 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1010 pvd->vdev_guid_sum += guid_delta;
1011
1012 /*
1013 * If this is a replacement, then we want to fallthrough to the
1014 * rest of the code. If we're adding a spare, then it's already
1015 * labeled appropriately and we can just return.
1016 */
1017 if (reason == VDEV_LABEL_SPARE)
1018 return (0);
1019 ASSERT(reason == VDEV_LABEL_REPLACE ||
1020 reason == VDEV_LABEL_SPLIT);
1021 }
1022
1023 if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
1024 l2cache_guid != 0ULL) {
1025 uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
1026
1027 vd->vdev_guid += guid_delta;
1028
1029 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1030 pvd->vdev_guid_sum += guid_delta;
1031
1032 /*
1033 * If this is a replacement, then we want to fallthrough to the
1034 * rest of the code. If we're adding an l2cache, then it's
1035 * already labeled appropriately and we can just return.
1036 */
1037 if (reason == VDEV_LABEL_L2CACHE)
1038 return (0);
1039 ASSERT(reason == VDEV_LABEL_REPLACE);
1040 }
1041
1042 /*
1043 * Initialize its label.
1044 */
1045 vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1046 abd_zero(vp_abd, sizeof (vdev_phys_t));
1047 vp = abd_to_buf(vp_abd);
1048
1049 /*
1050 * Generate a label describing the pool and our top-level vdev.
1051 * We mark it as being from txg 0 to indicate that it's not
1052 * really part of an active pool just yet. The labels will
1053 * be written again with a meaningful txg by spa_sync().
1054 */
1055 if (reason == VDEV_LABEL_SPARE ||
1056 (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
1057 /*
1058 * For inactive hot spares, we generate a special label that
1059 * identifies as a mutually shared hot spare. We write the
1060 * label if we are adding a hot spare, or if we are removing an
1061 * active hot spare (in which case we want to revert the
1062 * labels).
1063 */
1064 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1065
1066 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
1067 spa_version(spa)) == 0);
1068 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1069 POOL_STATE_SPARE) == 0);
1070 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
1071 vd->vdev_guid) == 0);
1072 } else if (reason == VDEV_LABEL_L2CACHE ||
1073 (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
1074 /*
1075 * For level 2 ARC devices, add a special label.
1076 */
1077 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1078
1079 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
1080 spa_version(spa)) == 0);
1081 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1082 POOL_STATE_L2CACHE) == 0);
1083 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
1084 vd->vdev_guid) == 0);
1085 } else {
1086 uint64_t txg = 0ULL;
1087
1088 if (reason == VDEV_LABEL_SPLIT)
1089 txg = spa->spa_uberblock.ub_txg;
1090 label = spa_config_generate(spa, vd, txg, B_FALSE);
1091
1092 /*
1093 * Add our creation time. This allows us to detect multiple
1094 * vdev uses as described above, and automatically expires if we
1095 * fail.
1096 */
1097 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
1098 crtxg) == 0);
1099 }
1100
1101 buf = vp->vp_nvlist;
1102 buflen = sizeof (vp->vp_nvlist);
1103
1104 error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
1105 if (error != 0) {
1106 nvlist_free(label);
1107 abd_free(vp_abd);
1108 /* EFAULT means nvlist_pack ran out of room */
1109 return (SET_ERROR(error == EFAULT ? ENAMETOOLONG : EINVAL));
1110 }
1111
1112 /*
1113 * Initialize uberblock template.
1114 */
1115 ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
1116 abd_zero(ub_abd, VDEV_UBERBLOCK_RING);
1117 abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
1118 ub = abd_to_buf(ub_abd);
1119 ub->ub_txg = 0;
1120
1121 /* Initialize the 2nd padding area. */
1122 pad2 = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1123 abd_zero(pad2, VDEV_PAD_SIZE);
1124
1125 /*
1126 * Write everything in parallel.
1127 */
1128 retry:
1129 zio = zio_root(spa, NULL, NULL, flags);
1130
1131 for (int l = 0; l < VDEV_LABELS; l++) {
1132
1133 vdev_label_write(zio, vd, l, vp_abd,
1134 offsetof(vdev_label_t, vl_vdev_phys),
1135 sizeof (vdev_phys_t), NULL, NULL, flags);
1136
1137 /*
1138 * Skip the 1st padding area.
1139 * Zero out the 2nd padding area where it might have
1140 * left over data from previous filesystem format.
1141 */
1142 vdev_label_write(zio, vd, l, pad2,
1143 offsetof(vdev_label_t, vl_pad2),
1144 VDEV_PAD_SIZE, NULL, NULL, flags);
1145
1146 vdev_label_write(zio, vd, l, ub_abd,
1147 offsetof(vdev_label_t, vl_uberblock),
1148 VDEV_UBERBLOCK_RING, NULL, NULL, flags);
1149 }
1150
1151 error = zio_wait(zio);
1152
1153 if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
1154 flags |= ZIO_FLAG_TRYHARD;
1155 goto retry;
1156 }
1157
1158 nvlist_free(label);
1159 abd_free(pad2);
1160 abd_free(ub_abd);
1161 abd_free(vp_abd);
1162
1163 /*
1164 * If this vdev hasn't been previously identified as a spare, then we
1165 * mark it as such only if a) we are labeling it as a spare, or b) it
1166 * exists as a spare elsewhere in the system. Do the same for
1167 * level 2 ARC devices.
1168 */
1169 if (error == 0 && !vd->vdev_isspare &&
1170 (reason == VDEV_LABEL_SPARE ||
1171 spa_spare_exists(vd->vdev_guid, NULL, NULL)))
1172 spa_spare_add(vd);
1173
1174 if (error == 0 && !vd->vdev_isl2cache &&
1175 (reason == VDEV_LABEL_L2CACHE ||
1176 spa_l2cache_exists(vd->vdev_guid, NULL)))
1177 spa_l2cache_add(vd);
1178
1179 return (error);
1180 }
1181
1182 /*
1183 * ==========================================================================
1184 * uberblock load/sync
1185 * ==========================================================================
1186 */
1187
1188 /*
1189 * Consider the following situation: txg is safely synced to disk. We've
1190 * written the first uberblock for txg + 1, and then we lose power. When we
1191 * come back up, we fail to see the uberblock for txg + 1 because, say,
1192 * it was on a mirrored device and the replica to which we wrote txg + 1
1193 * is now offline. If we then make some changes and sync txg + 1, and then
1194 * the missing replica comes back, then for a few seconds we'll have two
1195 * conflicting uberblocks on disk with the same txg. The solution is simple:
1196 * among uberblocks with equal txg, choose the one with the latest timestamp.
1197 */
1198 static int
1199 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1200 {
1201 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg);
1202
1203 if (likely(cmp))
1204 return (cmp);
1205
1206 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
1207 if (likely(cmp))
1208 return (cmp);
1209
1210 /*
1211 * If MMP_VALID(ub) && MMP_SEQ_VALID(ub) then the host has an MMP-aware
1212 * ZFS, e.g. zfsonlinux >= 0.7.
1213 *
1214 * If one ub has MMP and the other does not, they were written by
1215 * different hosts, which matters for MMP. So we treat no MMP/no SEQ as
1216 * a 0 value.
1217 *
1218 * Since timestamp and txg are the same if we get this far, either is
1219 * acceptable for importing the pool.
1220 */
1221 unsigned int seq1 = 0;
1222 unsigned int seq2 = 0;
1223
1224 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
1225 seq1 = MMP_SEQ(ub1);
1226
1227 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
1228 seq2 = MMP_SEQ(ub2);
1229
1230 return (AVL_CMP(seq1, seq2));
1231 }
1232
1233 struct ubl_cbdata {
1234 uberblock_t *ubl_ubbest; /* Best uberblock */
1235 vdev_t *ubl_vd; /* vdev associated with the above */
1236 };
1237
1238 static void
1239 vdev_uberblock_load_done(zio_t *zio)
1240 {
1241 vdev_t *vd = zio->io_vd;
1242 spa_t *spa = zio->io_spa;
1243 zio_t *rio = zio->io_private;
1244 uberblock_t *ub = abd_to_buf(zio->io_abd);
1245 struct ubl_cbdata *cbp = rio->io_private;
1246
1247 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1248
1249 if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1250 mutex_enter(&rio->io_lock);
1251 if (ub->ub_txg <= spa->spa_load_max_txg &&
1252 vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1253 /*
1254 * Keep track of the vdev in which this uberblock
1255 * was found. We will use this information later
1256 * to obtain the config nvlist associated with
1257 * this uberblock.
1258 */
1259 *cbp->ubl_ubbest = *ub;
1260 cbp->ubl_vd = vd;
1261 }
1262 mutex_exit(&rio->io_lock);
1263 }
1264
1265 abd_free(zio->io_abd);
1266 }
1267
1268 static void
1269 vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1270 struct ubl_cbdata *cbp)
1271 {
1272 for (int c = 0; c < vd->vdev_children; c++)
1273 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1274
1275 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1276 for (int l = 0; l < VDEV_LABELS; l++) {
1277 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1278 vdev_label_read(zio, vd, l,
1279 abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1280 B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1281 VDEV_UBERBLOCK_SIZE(vd),
1282 vdev_uberblock_load_done, zio, flags);
1283 }
1284 }
1285 }
1286 }
1287
1288 /*
1289 * Reads the 'best' uberblock from disk along with its associated
1290 * configuration. First, we read the uberblock array of each label of each
1291 * vdev, keeping track of the uberblock with the highest txg in each array.
1292 * Then, we read the configuration from the same vdev as the best uberblock.
1293 */
1294 void
1295 vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1296 {
1297 zio_t *zio;
1298 spa_t *spa = rvd->vdev_spa;
1299 struct ubl_cbdata cb;
1300 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1301 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1302
1303 ASSERT(ub);
1304 ASSERT(config);
1305
1306 bzero(ub, sizeof (uberblock_t));
1307 *config = NULL;
1308
1309 cb.ubl_ubbest = ub;
1310 cb.ubl_vd = NULL;
1311
1312 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1313 zio = zio_root(spa, NULL, &cb, flags);
1314 vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1315 (void) zio_wait(zio);
1316
1317 /*
1318 * It's possible that the best uberblock was discovered on a label
1319 * that has a configuration which was written in a future txg.
1320 * Search all labels on this vdev to find the configuration that
1321 * matches the txg for our uberblock.
1322 */
1323 if (cb.ubl_vd != NULL) {
1324 vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
1325 "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
1326
1327 *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
1328 if (*config == NULL && spa->spa_extreme_rewind) {
1329 vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
1330 "Trying again without txg restrictions.");
1331 *config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
1332 }
1333 if (*config == NULL) {
1334 vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
1335 }
1336 }
1337 spa_config_exit(spa, SCL_ALL, FTAG);
1338 }
1339
1340 /*
1341 * For use when a leaf vdev is expanded.
1342 * The location of labels 2 and 3 changed, and at the new location the
1343 * uberblock rings are either empty or contain garbage. The sync will write
1344 * new configs there because the vdev is dirty, but expansion also needs the
1345 * uberblock rings copied. Read them from label 0 which did not move.
1346 *
1347 * Since the point is to populate labels {2,3} with valid uberblocks,
1348 * we zero uberblocks we fail to read or which are not valid.
1349 */
1350
1351 static void
1352 vdev_copy_uberblocks(vdev_t *vd)
1353 {
1354 abd_t *ub_abd;
1355 zio_t *write_zio;
1356 int locks = (SCL_L2ARC | SCL_ZIO);
1357 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1358 ZIO_FLAG_SPECULATIVE;
1359
1360 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_READER) ==
1361 SCL_STATE);
1362 ASSERT(vd->vdev_ops->vdev_op_leaf);
1363
1364 spa_config_enter(vd->vdev_spa, locks, FTAG, RW_READER);
1365
1366 ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1367
1368 write_zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1369 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1370 const int src_label = 0;
1371 zio_t *zio;
1372
1373 zio = zio_root(vd->vdev_spa, NULL, NULL, flags);
1374 vdev_label_read(zio, vd, src_label, ub_abd,
1375 VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1376 NULL, NULL, flags);
1377
1378 if (zio_wait(zio) || uberblock_verify(abd_to_buf(ub_abd)))
1379 abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1380
1381 for (int l = 2; l < VDEV_LABELS; l++)
1382 vdev_label_write(write_zio, vd, l, ub_abd,
1383 VDEV_UBERBLOCK_OFFSET(vd, n),
1384 VDEV_UBERBLOCK_SIZE(vd), NULL, NULL,
1385 flags | ZIO_FLAG_DONT_PROPAGATE);
1386 }
1387 (void) zio_wait(write_zio);
1388
1389 spa_config_exit(vd->vdev_spa, locks, FTAG);
1390
1391 abd_free(ub_abd);
1392 }
1393
1394 /*
1395 * On success, increment root zio's count of good writes.
1396 * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1397 */
1398 static void
1399 vdev_uberblock_sync_done(zio_t *zio)
1400 {
1401 uint64_t *good_writes = zio->io_private;
1402
1403 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
1404 atomic_inc_64(good_writes);
1405 }
1406
1407 /*
1408 * Write the uberblock to all labels of all leaves of the specified vdev.
1409 */
1410 static void
1411 vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1412 uberblock_t *ub, vdev_t *vd, int flags)
1413 {
1414 for (uint64_t c = 0; c < vd->vdev_children; c++) {
1415 vdev_uberblock_sync(zio, good_writes,
1416 ub, vd->vdev_child[c], flags);
1417 }
1418
1419 if (!vd->vdev_ops->vdev_op_leaf)
1420 return;
1421
1422 if (!vdev_writeable(vd))
1423 return;
1424
1425 /* If the vdev was expanded, need to copy uberblock rings. */
1426 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1427 vd->vdev_copy_uberblocks == B_TRUE) {
1428 vdev_copy_uberblocks(vd);
1429 vd->vdev_copy_uberblocks = B_FALSE;
1430 }
1431
1432 int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1433 int n = ub->ub_txg % (VDEV_UBERBLOCK_COUNT(vd) - m);
1434
1435 /* Copy the uberblock_t into the ABD */
1436 abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1437 abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1438 abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1439
1440 for (int l = 0; l < VDEV_LABELS; l++)
1441 vdev_label_write(zio, vd, l, ub_abd,
1442 VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1443 vdev_uberblock_sync_done, good_writes,
1444 flags | ZIO_FLAG_DONT_PROPAGATE);
1445
1446 abd_free(ub_abd);
1447 }
1448
1449 /* Sync the uberblocks to all vdevs in svd[] */
1450 int
1451 vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1452 {
1453 spa_t *spa = svd[0]->vdev_spa;
1454 zio_t *zio;
1455 uint64_t good_writes = 0;
1456
1457 zio = zio_root(spa, NULL, NULL, flags);
1458
1459 for (int v = 0; v < svdcount; v++)
1460 vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1461
1462 (void) zio_wait(zio);
1463
1464 /*
1465 * Flush the uberblocks to disk. This ensures that the odd labels
1466 * are no longer needed (because the new uberblocks and the even
1467 * labels are safely on disk), so it is safe to overwrite them.
1468 */
1469 zio = zio_root(spa, NULL, NULL, flags);
1470
1471 for (int v = 0; v < svdcount; v++) {
1472 if (vdev_writeable(svd[v])) {
1473 zio_flush(zio, svd[v]);
1474 }
1475 }
1476
1477 (void) zio_wait(zio);
1478
1479 return (good_writes >= 1 ? 0 : EIO);
1480 }
1481
1482 /*
1483 * On success, increment the count of good writes for our top-level vdev.
1484 */
1485 static void
1486 vdev_label_sync_done(zio_t *zio)
1487 {
1488 uint64_t *good_writes = zio->io_private;
1489
1490 if (zio->io_error == 0)
1491 atomic_inc_64(good_writes);
1492 }
1493
1494 /*
1495 * If there weren't enough good writes, indicate failure to the parent.
1496 */
1497 static void
1498 vdev_label_sync_top_done(zio_t *zio)
1499 {
1500 uint64_t *good_writes = zio->io_private;
1501
1502 if (*good_writes == 0)
1503 zio->io_error = SET_ERROR(EIO);
1504
1505 kmem_free(good_writes, sizeof (uint64_t));
1506 }
1507
1508 /*
1509 * We ignore errors for log and cache devices, simply free the private data.
1510 */
1511 static void
1512 vdev_label_sync_ignore_done(zio_t *zio)
1513 {
1514 kmem_free(zio->io_private, sizeof (uint64_t));
1515 }
1516
1517 /*
1518 * Write all even or odd labels to all leaves of the specified vdev.
1519 */
1520 static void
1521 vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1522 vdev_t *vd, int l, uint64_t txg, int flags)
1523 {
1524 nvlist_t *label;
1525 vdev_phys_t *vp;
1526 abd_t *vp_abd;
1527 char *buf;
1528 size_t buflen;
1529
1530 for (int c = 0; c < vd->vdev_children; c++) {
1531 vdev_label_sync(zio, good_writes,
1532 vd->vdev_child[c], l, txg, flags);
1533 }
1534
1535 if (!vd->vdev_ops->vdev_op_leaf)
1536 return;
1537
1538 if (!vdev_writeable(vd))
1539 return;
1540
1541 /*
1542 * Generate a label describing the top-level config to which we belong.
1543 */
1544 label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1545
1546 vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1547 abd_zero(vp_abd, sizeof (vdev_phys_t));
1548 vp = abd_to_buf(vp_abd);
1549
1550 buf = vp->vp_nvlist;
1551 buflen = sizeof (vp->vp_nvlist);
1552
1553 if (!nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP)) {
1554 for (; l < VDEV_LABELS; l += 2) {
1555 vdev_label_write(zio, vd, l, vp_abd,
1556 offsetof(vdev_label_t, vl_vdev_phys),
1557 sizeof (vdev_phys_t),
1558 vdev_label_sync_done, good_writes,
1559 flags | ZIO_FLAG_DONT_PROPAGATE);
1560 }
1561 }
1562
1563 abd_free(vp_abd);
1564 nvlist_free(label);
1565 }
1566
1567 int
1568 vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1569 {
1570 list_t *dl = &spa->spa_config_dirty_list;
1571 vdev_t *vd;
1572 zio_t *zio;
1573 int error;
1574
1575 /*
1576 * Write the new labels to disk.
1577 */
1578 zio = zio_root(spa, NULL, NULL, flags);
1579
1580 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
1581 uint64_t *good_writes;
1582
1583 ASSERT(!vd->vdev_ishole);
1584
1585 good_writes = kmem_zalloc(sizeof (uint64_t), KM_SLEEP);
1586 zio_t *vio = zio_null(zio, spa, NULL,
1587 (vd->vdev_islog || vd->vdev_aux != NULL) ?
1588 vdev_label_sync_ignore_done : vdev_label_sync_top_done,
1589 good_writes, flags);
1590 vdev_label_sync(vio, good_writes, vd, l, txg, flags);
1591 zio_nowait(vio);
1592 }
1593
1594 error = zio_wait(zio);
1595
1596 /*
1597 * Flush the new labels to disk.
1598 */
1599 zio = zio_root(spa, NULL, NULL, flags);
1600
1601 for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
1602 zio_flush(zio, vd);
1603
1604 (void) zio_wait(zio);
1605
1606 return (error);
1607 }
1608
1609 /*
1610 * Sync the uberblock and any changes to the vdev configuration.
1611 *
1612 * The order of operations is carefully crafted to ensure that
1613 * if the system panics or loses power at any time, the state on disk
1614 * is still transactionally consistent. The in-line comments below
1615 * describe the failure semantics at each stage.
1616 *
1617 * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1618 * at any time, you can just call it again, and it will resume its work.
1619 */
1620 int
1621 vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1622 {
1623 spa_t *spa = svd[0]->vdev_spa;
1624 uberblock_t *ub = &spa->spa_uberblock;
1625 int error = 0;
1626 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1627
1628 ASSERT(svdcount != 0);
1629 retry:
1630 /*
1631 * Normally, we don't want to try too hard to write every label and
1632 * uberblock. If there is a flaky disk, we don't want the rest of the
1633 * sync process to block while we retry. But if we can't write a
1634 * single label out, we should retry with ZIO_FLAG_TRYHARD before
1635 * bailing out and declaring the pool faulted.
1636 */
1637 if (error != 0) {
1638 if ((flags & ZIO_FLAG_TRYHARD) != 0)
1639 return (error);
1640 flags |= ZIO_FLAG_TRYHARD;
1641 }
1642
1643 ASSERT(ub->ub_txg <= txg);
1644
1645 /*
1646 * If this isn't a resync due to I/O errors,
1647 * and nothing changed in this transaction group,
1648 * and the vdev configuration hasn't changed,
1649 * then there's nothing to do.
1650 */
1651 if (ub->ub_txg < txg) {
1652 boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
1653 txg, spa->spa_mmp.mmp_delay);
1654
1655 if (!changed && list_is_empty(&spa->spa_config_dirty_list))
1656 return (0);
1657 }
1658
1659 if (txg > spa_freeze_txg(spa))
1660 return (0);
1661
1662 ASSERT(txg <= spa->spa_final_txg);
1663
1664 /*
1665 * Flush the write cache of every disk that's been written to
1666 * in this transaction group. This ensures that all blocks
1667 * written in this txg will be committed to stable storage
1668 * before any uberblock that references them.
1669 */
1670 zio_t *zio = zio_root(spa, NULL, NULL, flags);
1671
1672 for (vdev_t *vd =
1673 txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
1674 vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
1675 zio_flush(zio, vd);
1676
1677 (void) zio_wait(zio);
1678
1679 /*
1680 * Sync out the even labels (L0, L2) for every dirty vdev. If the
1681 * system dies in the middle of this process, that's OK: all of the
1682 * even labels that made it to disk will be newer than any uberblock,
1683 * and will therefore be considered invalid. The odd labels (L1, L3),
1684 * which have not yet been touched, will still be valid. We flush
1685 * the new labels to disk to ensure that all even-label updates
1686 * are committed to stable storage before the uberblock update.
1687 */
1688 if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
1689 if ((flags & ZIO_FLAG_TRYHARD) != 0) {
1690 zfs_dbgmsg("vdev_label_sync_list() returned error %d "
1691 "for pool '%s' when syncing out the even labels "
1692 "of dirty vdevs", error, spa_name(spa));
1693 }
1694 goto retry;
1695 }
1696
1697 /*
1698 * Sync the uberblocks to all vdevs in svd[].
1699 * If the system dies in the middle of this step, there are two cases
1700 * to consider, and the on-disk state is consistent either way:
1701 *
1702 * (1) If none of the new uberblocks made it to disk, then the
1703 * previous uberblock will be the newest, and the odd labels
1704 * (which had not yet been touched) will be valid with respect
1705 * to that uberblock.
1706 *
1707 * (2) If one or more new uberblocks made it to disk, then they
1708 * will be the newest, and the even labels (which had all
1709 * been successfully committed) will be valid with respect
1710 * to the new uberblocks.
1711 */
1712 if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
1713 if ((flags & ZIO_FLAG_TRYHARD) != 0) {
1714 zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
1715 "%d for pool '%s'", error, spa_name(spa));
1716 }
1717 goto retry;
1718 }
1719
1720 if (spa_multihost(spa))
1721 mmp_update_uberblock(spa, ub);
1722
1723 /*
1724 * Sync out odd labels for every dirty vdev. If the system dies
1725 * in the middle of this process, the even labels and the new
1726 * uberblocks will suffice to open the pool. The next time
1727 * the pool is opened, the first thing we'll do -- before any
1728 * user data is modified -- is mark every vdev dirty so that
1729 * all labels will be brought up to date. We flush the new labels
1730 * to disk to ensure that all odd-label updates are committed to
1731 * stable storage before the next transaction group begins.
1732 */
1733 if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
1734 if ((flags & ZIO_FLAG_TRYHARD) != 0) {
1735 zfs_dbgmsg("vdev_label_sync_list() returned error %d "
1736 "for pool '%s' when syncing out the odd labels of "
1737 "dirty vdevs", error, spa_name(spa));
1738 }
1739 goto retry;
1740 }
1741
1742 return (0);
1743 }