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