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