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