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