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