1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5441d80aaSlling * Common Development and Distribution License (the "License").
6441d80aaSlling * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21ad135b5dSChristopher Siden
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24c4ecba8aSPaul Dagnelie * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25663207adSDon Brady * Copyright (c) 2017, Intel Corporation.
26dd50e0ccSTony Hutter * Copyright 2020 Joyent, Inc.
27fa9e4066Sahrens */
28fa9e4066Sahrens
29fa9e4066Sahrens /*
30fa9e4066Sahrens * Virtual Device Labels
31fa9e4066Sahrens * ---------------------
32fa9e4066Sahrens *
33fa9e4066Sahrens * The vdev label serves several distinct purposes:
34fa9e4066Sahrens *
35fa9e4066Sahrens * 1. Uniquely identify this device as part of a ZFS pool and confirm its
36fa9e4066Sahrens * identity within the pool.
37fa9e4066Sahrens *
38cfd63e1bSMatthew Ahrens * 2. Verify that all the devices given in a configuration are present
39fa9e4066Sahrens * within the pool.
40fa9e4066Sahrens *
41cfd63e1bSMatthew Ahrens * 3. Determine the uberblock for the pool.
42fa9e4066Sahrens *
43cfd63e1bSMatthew Ahrens * 4. In case of an import operation, determine the configuration of the
44fa9e4066Sahrens * toplevel vdev of which it is a part.
45fa9e4066Sahrens *
46cfd63e1bSMatthew Ahrens * 5. If an import operation cannot find all the devices in the pool,
47fa9e4066Sahrens * provide enough information to the administrator to determine which
48fa9e4066Sahrens * devices are missing.
49fa9e4066Sahrens *
50fa9e4066Sahrens * It is important to note that while the kernel is responsible for writing the
51fa9e4066Sahrens * label, it only consumes the information in the first three cases. The
52fa9e4066Sahrens * latter information is only consumed in userland when determining the
53fa9e4066Sahrens * configuration to import a pool.
54fa9e4066Sahrens *
55fa9e4066Sahrens *
56fa9e4066Sahrens * Label Organization
57fa9e4066Sahrens * ------------------
58fa9e4066Sahrens *
59fa9e4066Sahrens * Before describing the contents of the label, it's important to understand how
60fa9e4066Sahrens * the labels are written and updated with respect to the uberblock.
61fa9e4066Sahrens *
62fa9e4066Sahrens * When the pool configuration is altered, either because it was newly created
63fa9e4066Sahrens * or a device was added, we want to update all the labels such that we can deal
64fa9e4066Sahrens * with fatal failure at any point. To this end, each disk has two labels which
65fa9e4066Sahrens * are updated before and after the uberblock is synced. Assuming we have
663d7072f8Seschrock * labels and an uberblock with the following transaction groups:
67fa9e4066Sahrens *
68fa9e4066Sahrens * L1 UB L2
69fa9e4066Sahrens * +------+ +------+ +------+
70fa9e4066Sahrens * | | | | | |
71fa9e4066Sahrens * | t10 | | t10 | | t10 |
72fa9e4066Sahrens * | | | | | |
73fa9e4066Sahrens * +------+ +------+ +------+
74fa9e4066Sahrens *
75fa9e4066Sahrens * In this stable state, the labels and the uberblock were all updated within
76fa9e4066Sahrens * the same transaction group (10). Each label is mirrored and checksummed, so
77fa9e4066Sahrens * that we can detect when we fail partway through writing the label.
78fa9e4066Sahrens *
79fa9e4066Sahrens * In order to identify which labels are valid, the labels are written in the
80fa9e4066Sahrens * following manner:
81fa9e4066Sahrens *
82cfd63e1bSMatthew Ahrens * 1. For each vdev, update 'L1' to the new label
83cfd63e1bSMatthew Ahrens * 2. Update the uberblock
84cfd63e1bSMatthew Ahrens * 3. For each vdev, update 'L2' to the new label
85fa9e4066Sahrens *
86fa9e4066Sahrens * Given arbitrary failure, we can determine the correct label to use based on
87fa9e4066Sahrens * the transaction group. If we fail after updating L1 but before updating the
88fa9e4066Sahrens * UB, we will notice that L1's transaction group is greater than the uberblock,
89fa9e4066Sahrens * so L2 must be valid. If we fail after writing the uberblock but before
90fa9e4066Sahrens * writing L2, we will notice that L2's transaction group is less than L1, and
91fa9e4066Sahrens * therefore L1 is valid.
92fa9e4066Sahrens *
93fa9e4066Sahrens * Another added complexity is that not every label is updated when the config
94fa9e4066Sahrens * is synced. If we add a single device, we do not want to have to re-write
95fa9e4066Sahrens * every label for every device in the pool. This means that both L1 and L2 may
96fa9e4066Sahrens * be older than the pool uberblock, because the necessary information is stored
97fa9e4066Sahrens * on another vdev.
98fa9e4066Sahrens *
99fa9e4066Sahrens *
100fa9e4066Sahrens * On-disk Format
101fa9e4066Sahrens * --------------
102fa9e4066Sahrens *
103fa9e4066Sahrens * The vdev label consists of two distinct parts, and is wrapped within the
104fa9e4066Sahrens * vdev_label_t structure. The label includes 8k of padding to permit legacy
105fa9e4066Sahrens * VTOC disk labels, but is otherwise ignored.
106fa9e4066Sahrens *
107fa9e4066Sahrens * The first half of the label is a packed nvlist which contains pool wide
108fa9e4066Sahrens * properties, per-vdev properties, and configuration information. It is
109fa9e4066Sahrens * described in more detail below.
110fa9e4066Sahrens *
111fa9e4066Sahrens * The latter half of the label consists of a redundant array of uberblocks.
112fa9e4066Sahrens * These uberblocks are updated whenever a transaction group is committed,
113fa9e4066Sahrens * or when the configuration is updated. When a pool is loaded, we scan each
114fa9e4066Sahrens * vdev for the 'best' uberblock.
115fa9e4066Sahrens *
116fa9e4066Sahrens *
117fa9e4066Sahrens * Configuration Information
118fa9e4066Sahrens * -------------------------
119fa9e4066Sahrens *
120fa9e4066Sahrens * The nvlist describing the pool and vdev contains the following elements:
121fa9e4066Sahrens *
122cfd63e1bSMatthew Ahrens * version ZFS on-disk version
123cfd63e1bSMatthew Ahrens * name Pool name
124cfd63e1bSMatthew Ahrens * state Pool state
125cfd63e1bSMatthew Ahrens * txg Transaction group in which this label was written
126cfd63e1bSMatthew Ahrens * pool_guid Unique identifier for this pool
127cfd63e1bSMatthew Ahrens * vdev_tree An nvlist describing vdev tree.
128ad135b5dSChristopher Siden * features_for_read
129ad135b5dSChristopher Siden * An nvlist of the features necessary for reading the MOS.
130fa9e4066Sahrens *
131fa9e4066Sahrens * Each leaf device label also contains the following:
132fa9e4066Sahrens *
133cfd63e1bSMatthew Ahrens * top_guid Unique ID for top-level vdev in which this is contained
134cfd63e1bSMatthew Ahrens * guid Unique ID for the leaf vdev
135fa9e4066Sahrens *
136fa9e4066Sahrens * The 'vs' configuration follows the format described in 'spa_config.c'.
137fa9e4066Sahrens */
138fa9e4066Sahrens
139fa9e4066Sahrens #include <sys/zfs_context.h>
140fa9e4066Sahrens #include <sys/spa.h>
141fa9e4066Sahrens #include <sys/spa_impl.h>
142fa9e4066Sahrens #include <sys/dmu.h>
143fa9e4066Sahrens #include <sys/zap.h>
144fa9e4066Sahrens #include <sys/vdev.h>
145fa9e4066Sahrens #include <sys/vdev_impl.h>
146fa9e4066Sahrens #include <sys/uberblock_impl.h>
147fa9e4066Sahrens #include <sys/metaslab.h>
1485cabbc6bSPrashanth Sreenivasa #include <sys/metaslab_impl.h>
149fa9e4066Sahrens #include <sys/zio.h>
1503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
151770499e1SDan Kimmel #include <sys/abd.h>
152fa9e4066Sahrens #include <sys/fs/zfs.h>
15309fcda9fSToomas Soome #include <sys/byteorder.h>
15409fcda9fSToomas Soome #include <sys/zfs_bootenv.h>
155fa9e4066Sahrens
156fa9e4066Sahrens /*
157fa9e4066Sahrens * Basic routines to read and write from a vdev label.
158fa9e4066Sahrens * Used throughout the rest of this file.
159fa9e4066Sahrens */
160fa9e4066Sahrens uint64_t
vdev_label_offset(uint64_t psize,int l,uint64_t offset)161fa9e4066Sahrens vdev_label_offset(uint64_t psize, int l, uint64_t offset)
162fa9e4066Sahrens {
163ecc2d604Sbonwick ASSERT(offset < sizeof (vdev_label_t));
164e7437265Sahrens ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
165ecc2d604Sbonwick
166fa9e4066Sahrens return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
167fa9e4066Sahrens 0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
168fa9e4066Sahrens }
169fa9e4066Sahrens
17021bf64a7Sgw /*
17121bf64a7Sgw * Returns back the vdev label associated with the passed in offset.
17221bf64a7Sgw */
17321bf64a7Sgw int
vdev_label_number(uint64_t psize,uint64_t offset)17421bf64a7Sgw vdev_label_number(uint64_t psize, uint64_t offset)
17521bf64a7Sgw {
17621bf64a7Sgw int l;
17721bf64a7Sgw
17821bf64a7Sgw if (offset >= psize - VDEV_LABEL_END_SIZE) {
17921bf64a7Sgw offset -= psize - VDEV_LABEL_END_SIZE;
18021bf64a7Sgw offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
18121bf64a7Sgw }
18221bf64a7Sgw l = offset / sizeof (vdev_label_t);
18321bf64a7Sgw return (l < VDEV_LABELS ? l : -1);
18421bf64a7Sgw }
18521bf64a7Sgw
186fa9e4066Sahrens 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)187770499e1SDan Kimmel vdev_label_read(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
1889a686fbcSPaul Dagnelie uint64_t size, zio_done_func_t *done, void *private, int flags)
189fa9e4066Sahrens {
19058447f68SOlaf Faaland ASSERT(
19158447f68SOlaf Faaland spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
19258447f68SOlaf Faaland spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
193e14bb325SJeff Bonwick ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
194fa9e4066Sahrens
195fa9e4066Sahrens zio_nowait(zio_read_phys(zio, vd,
196fa9e4066Sahrens vdev_label_offset(vd->vdev_psize, l, offset),
197fa9e4066Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private,
198e14bb325SJeff Bonwick ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
199fa9e4066Sahrens }
200fa9e4066Sahrens
201e0f1c0afSOlaf Faaland 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)202770499e1SDan Kimmel vdev_label_write(zio_t *zio, vdev_t *vd, int l, abd_t *buf, uint64_t offset,
2039a686fbcSPaul Dagnelie uint64_t size, zio_done_func_t *done, void *private, int flags)
204fa9e4066Sahrens {
20558447f68SOlaf Faaland ASSERT(
20658447f68SOlaf Faaland spa_config_held(zio->io_spa, SCL_STATE, RW_READER) == SCL_STATE ||
20758447f68SOlaf Faaland spa_config_held(zio->io_spa, SCL_STATE, RW_WRITER) == SCL_STATE);
208e14bb325SJeff Bonwick ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
209fa9e4066Sahrens
210fa9e4066Sahrens zio_nowait(zio_write_phys(zio, vd,
211fa9e4066Sahrens vdev_label_offset(vd->vdev_psize, l, offset),
212fa9e4066Sahrens size, buf, ZIO_CHECKSUM_LABEL, done, private,
21317f17c2dSbonwick ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
214fa9e4066Sahrens }
215fa9e4066Sahrens
216dd50e0ccSTony Hutter /*
217dd50e0ccSTony Hutter * Generate the nvlist representing this vdev's stats
218dd50e0ccSTony Hutter */
219dd50e0ccSTony Hutter void
vdev_config_generate_stats(vdev_t * vd,nvlist_t * nv)220dd50e0ccSTony Hutter vdev_config_generate_stats(vdev_t *vd, nvlist_t *nv)
221dd50e0ccSTony Hutter {
222dd50e0ccSTony Hutter nvlist_t *nvx;
223dd50e0ccSTony Hutter vdev_stat_t *vs;
224dd50e0ccSTony Hutter vdev_stat_ex_t *vsx;
225dd50e0ccSTony Hutter
226dd50e0ccSTony Hutter vs = kmem_alloc(sizeof (*vs), KM_SLEEP);
227dd50e0ccSTony Hutter vsx = kmem_alloc(sizeof (*vsx), KM_SLEEP);
228dd50e0ccSTony Hutter
229dd50e0ccSTony Hutter vdev_get_stats_ex(vd, vs, vsx);
230dd50e0ccSTony Hutter fnvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
231dd50e0ccSTony Hutter (uint64_t *)vs, sizeof (*vs) / sizeof (uint64_t));
232dd50e0ccSTony Hutter
233dd50e0ccSTony Hutter /*
234dd50e0ccSTony Hutter * Add extended stats into a special extended stats nvlist. This keeps
235dd50e0ccSTony Hutter * all the extended stats nicely grouped together. The extended stats
236dd50e0ccSTony Hutter * nvlist is then added to the main nvlist.
237dd50e0ccSTony Hutter */
238dd50e0ccSTony Hutter nvx = fnvlist_alloc();
239dd50e0ccSTony Hutter
240dd50e0ccSTony Hutter /* ZIOs in flight to disk */
241dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_ACTIVE_QUEUE,
242dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_READ]);
243dd50e0ccSTony Hutter
244dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_ACTIVE_QUEUE,
245dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_SYNC_WRITE]);
246dd50e0ccSTony Hutter
247dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_ACTIVE_QUEUE,
248dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_READ]);
249dd50e0ccSTony Hutter
250dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_ACTIVE_QUEUE,
251dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_ASYNC_WRITE]);
252dd50e0ccSTony Hutter
253dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_ACTIVE_QUEUE,
254dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_SCRUB]);
255dd50e0ccSTony Hutter
256dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_ACTIVE_QUEUE,
257dd50e0ccSTony Hutter vsx->vsx_active_queue[ZIO_PRIORITY_TRIM]);
258dd50e0ccSTony Hutter
259dd50e0ccSTony Hutter /* ZIOs pending */
260dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_PEND_QUEUE,
261dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_READ]);
262dd50e0ccSTony Hutter
263dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_PEND_QUEUE,
264dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_SYNC_WRITE]);
265dd50e0ccSTony Hutter
266dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_PEND_QUEUE,
267dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_READ]);
268dd50e0ccSTony Hutter
269dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_PEND_QUEUE,
270dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_ASYNC_WRITE]);
271dd50e0ccSTony Hutter
272dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SCRUB_PEND_QUEUE,
273dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_SCRUB]);
274dd50e0ccSTony Hutter
275dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_TRIM_PEND_QUEUE,
276dd50e0ccSTony Hutter vsx->vsx_pend_queue[ZIO_PRIORITY_TRIM]);
277dd50e0ccSTony Hutter
278dd50e0ccSTony Hutter /* Histograms */
279dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_R_LAT_HISTO,
280dd50e0ccSTony Hutter vsx->vsx_total_histo[ZIO_TYPE_READ],
281dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_READ]));
282dd50e0ccSTony Hutter
283dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TOT_W_LAT_HISTO,
284dd50e0ccSTony Hutter vsx->vsx_total_histo[ZIO_TYPE_WRITE],
285dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_total_histo[ZIO_TYPE_WRITE]));
286dd50e0ccSTony Hutter
287dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_R_LAT_HISTO,
288dd50e0ccSTony Hutter vsx->vsx_disk_histo[ZIO_TYPE_READ],
289dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_READ]));
290dd50e0ccSTony Hutter
291dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_DISK_W_LAT_HISTO,
292dd50e0ccSTony Hutter vsx->vsx_disk_histo[ZIO_TYPE_WRITE],
293dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_disk_histo[ZIO_TYPE_WRITE]));
294dd50e0ccSTony Hutter
295dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_R_LAT_HISTO,
296dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ],
297dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_READ]));
298dd50e0ccSTony Hutter
299dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_W_LAT_HISTO,
300dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE],
301dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SYNC_WRITE]));
302dd50e0ccSTony Hutter
303dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_R_LAT_HISTO,
304dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ],
305dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_READ]));
306dd50e0ccSTony Hutter
307dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_W_LAT_HISTO,
308dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE],
309dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_ASYNC_WRITE]));
310dd50e0ccSTony Hutter
311dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SCRUB_LAT_HISTO,
312dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB],
313dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_SCRUB]));
314dd50e0ccSTony Hutter
315dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_TRIM_LAT_HISTO,
316dd50e0ccSTony Hutter vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM],
317dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_queue_histo[ZIO_PRIORITY_TRIM]));
318dd50e0ccSTony Hutter
319dd50e0ccSTony Hutter /* Request sizes */
320dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_R_HISTO,
321dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ],
322dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_READ]));
323dd50e0ccSTony Hutter
324dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_IND_W_HISTO,
325dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE],
326dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SYNC_WRITE]));
327dd50e0ccSTony Hutter
328dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_R_HISTO,
329dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ],
330dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_READ]));
331dd50e0ccSTony Hutter
332dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_IND_W_HISTO,
333dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE],
334dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_ASYNC_WRITE]));
335dd50e0ccSTony Hutter
336dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_SCRUB_HISTO,
337dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB],
338dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_SCRUB]));
339dd50e0ccSTony Hutter
340dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_IND_TRIM_HISTO,
341dd50e0ccSTony Hutter vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM],
342dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_ind_histo[ZIO_PRIORITY_TRIM]));
343dd50e0ccSTony Hutter
344dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_R_HISTO,
345dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ],
346dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_READ]));
347dd50e0ccSTony Hutter
348dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_SYNC_AGG_W_HISTO,
349dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE],
350dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SYNC_WRITE]));
351dd50e0ccSTony Hutter
352dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_R_HISTO,
353dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ],
354dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_READ]));
355dd50e0ccSTony Hutter
356dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_ASYNC_AGG_W_HISTO,
357dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE],
358dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_ASYNC_WRITE]));
359dd50e0ccSTony Hutter
360dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_SCRUB_HISTO,
361dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB],
362dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_SCRUB]));
363dd50e0ccSTony Hutter
364dd50e0ccSTony Hutter fnvlist_add_uint64_array(nvx, ZPOOL_CONFIG_VDEV_AGG_TRIM_HISTO,
365dd50e0ccSTony Hutter vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM],
366dd50e0ccSTony Hutter ARRAY_SIZE(vsx->vsx_agg_histo[ZIO_PRIORITY_TRIM]));
367dd50e0ccSTony Hutter
368dd50e0ccSTony Hutter /* IO delays */
369dd50e0ccSTony Hutter fnvlist_add_uint64(nvx, ZPOOL_CONFIG_VDEV_SLOW_IOS, vs->vs_slow_ios);
370dd50e0ccSTony Hutter
371dd50e0ccSTony Hutter /* Add extended stats nvlist to main nvlist */
372dd50e0ccSTony Hutter fnvlist_add_nvlist(nv, ZPOOL_CONFIG_VDEV_STATS_EX, nvx);
373dd50e0ccSTony Hutter
374dd50e0ccSTony Hutter nvlist_free(nvx);
375dd50e0ccSTony Hutter kmem_free(vs, sizeof (*vs));
376dd50e0ccSTony Hutter kmem_free(vsx, sizeof (*vsx));
377dd50e0ccSTony Hutter }
378dd50e0ccSTony Hutter
37986714001SSerapheim Dimitropoulos static void
root_vdev_actions_getprogress(vdev_t * vd,nvlist_t * nvl)38086714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vdev_t *vd, nvlist_t *nvl)
38186714001SSerapheim Dimitropoulos {
38286714001SSerapheim Dimitropoulos spa_t *spa = vd->vdev_spa;
38386714001SSerapheim Dimitropoulos
38486714001SSerapheim Dimitropoulos if (vd != spa->spa_root_vdev)
38586714001SSerapheim Dimitropoulos return;
38686714001SSerapheim Dimitropoulos
38786714001SSerapheim Dimitropoulos /* provide either current or previous scan information */
38886714001SSerapheim Dimitropoulos pool_scan_stat_t ps;
38986714001SSerapheim Dimitropoulos if (spa_scan_get_stats(spa, &ps) == 0) {
39086714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl,
39186714001SSerapheim Dimitropoulos ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
39286714001SSerapheim Dimitropoulos sizeof (pool_scan_stat_t) / sizeof (uint64_t));
39386714001SSerapheim Dimitropoulos }
39486714001SSerapheim Dimitropoulos
39586714001SSerapheim Dimitropoulos pool_removal_stat_t prs;
39686714001SSerapheim Dimitropoulos if (spa_removal_get_stats(spa, &prs) == 0) {
39786714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl,
39886714001SSerapheim Dimitropoulos ZPOOL_CONFIG_REMOVAL_STATS, (uint64_t *)&prs,
39986714001SSerapheim Dimitropoulos sizeof (prs) / sizeof (uint64_t));
40086714001SSerapheim Dimitropoulos }
40186714001SSerapheim Dimitropoulos
40286714001SSerapheim Dimitropoulos pool_checkpoint_stat_t pcs;
40386714001SSerapheim Dimitropoulos if (spa_checkpoint_get_stats(spa, &pcs) == 0) {
40486714001SSerapheim Dimitropoulos fnvlist_add_uint64_array(nvl,
40586714001SSerapheim Dimitropoulos ZPOOL_CONFIG_CHECKPOINT_STATS, (uint64_t *)&pcs,
40686714001SSerapheim Dimitropoulos sizeof (pcs) / sizeof (uint64_t));
40786714001SSerapheim Dimitropoulos }
40886714001SSerapheim Dimitropoulos }
40986714001SSerapheim Dimitropoulos
410fa9e4066Sahrens /*
411fa9e4066Sahrens * Generate the nvlist representing this vdev's config.
412fa9e4066Sahrens */
413fa9e4066Sahrens nvlist_t *
vdev_config_generate(spa_t * spa,vdev_t * vd,boolean_t getstats,vdev_config_flag_t flags)41499653d4eSeschrock vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
4153f9d6ad7SLin Ling vdev_config_flag_t flags)
416fa9e4066Sahrens {
417fa9e4066Sahrens nvlist_t *nv = NULL;
4185cabbc6bSPrashanth Sreenivasa vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
419fa9e4066Sahrens
420b4952e17SGeorge Wilson nv = fnvlist_alloc();
421fa9e4066Sahrens
422b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_TYPE, vd->vdev_ops->vdev_op_type);
4233f9d6ad7SLin Ling if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
424b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id);
425b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid);
426fa9e4066Sahrens
427fa9e4066Sahrens if (vd->vdev_path != NULL)
428b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_PATH, vd->vdev_path);
429fa9e4066Sahrens
430fa9e4066Sahrens if (vd->vdev_devid != NULL)
431b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vd->vdev_devid);
432fa9e4066Sahrens
4333d7072f8Seschrock if (vd->vdev_physpath != NULL)
434b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
435b4952e17SGeorge Wilson vd->vdev_physpath);
4363d7072f8Seschrock
4376809eb4eSEric Schrock if (vd->vdev_fru != NULL)
438b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_FRU, vd->vdev_fru);
4396809eb4eSEric Schrock
44099653d4eSeschrock if (vd->vdev_nparity != 0) {
44199653d4eSeschrock ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
44299653d4eSeschrock VDEV_TYPE_RAIDZ) == 0);
44399653d4eSeschrock
44499653d4eSeschrock /*
44599653d4eSeschrock * Make sure someone hasn't managed to sneak a fancy new vdev
44699653d4eSeschrock * into a crufty old storage pool.
44799653d4eSeschrock */
44899653d4eSeschrock ASSERT(vd->vdev_nparity == 1 ||
449f94275ceSAdam Leventhal (vd->vdev_nparity <= 2 &&
450f94275ceSAdam Leventhal spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
451f94275ceSAdam Leventhal (vd->vdev_nparity <= 3 &&
452f94275ceSAdam Leventhal spa_version(spa) >= SPA_VERSION_RAIDZ3));
45399653d4eSeschrock
45499653d4eSeschrock /*
45599653d4eSeschrock * Note that we'll add the nparity tag even on storage pools
45699653d4eSeschrock * that only support a single parity device -- older software
45799653d4eSeschrock * will just ignore it.
45899653d4eSeschrock */
459b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY, vd->vdev_nparity);
46099653d4eSeschrock }
46199653d4eSeschrock
462afefbcddSeschrock if (vd->vdev_wholedisk != -1ULL)
463b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
464b4952e17SGeorge Wilson vd->vdev_wholedisk);
465afefbcddSeschrock
4666f793812SPavel Zakharov if (vd->vdev_not_present && !(flags & VDEV_CONFIG_MISSING))
467b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1);
468ea8dc4b6Seschrock
46999653d4eSeschrock if (vd->vdev_isspare)
470b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1);
47199653d4eSeschrock
4723f9d6ad7SLin Ling if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
4733f9d6ad7SLin Ling vd == vd->vdev_top) {
474b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
475b4952e17SGeorge Wilson vd->vdev_ms_array);
476b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
477b4952e17SGeorge Wilson vd->vdev_ms_shift);
478b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT, vd->vdev_ashift);
479b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
480b4952e17SGeorge Wilson vd->vdev_asize);
481b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG, vd->vdev_islog);
4825cabbc6bSPrashanth Sreenivasa if (vd->vdev_removing) {
483b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
484b4952e17SGeorge Wilson vd->vdev_removing);
4855cabbc6bSPrashanth Sreenivasa }
486663207adSDon Brady
487663207adSDon Brady /* zpool command expects alloc class data */
488663207adSDon Brady if (getstats && vd->vdev_alloc_bias != VDEV_BIAS_NONE) {
489663207adSDon Brady const char *bias = NULL;
490663207adSDon Brady
491663207adSDon Brady switch (vd->vdev_alloc_bias) {
492663207adSDon Brady case VDEV_BIAS_LOG:
493663207adSDon Brady bias = VDEV_ALLOC_BIAS_LOG;
494663207adSDon Brady break;
495663207adSDon Brady case VDEV_BIAS_SPECIAL:
496663207adSDon Brady bias = VDEV_ALLOC_BIAS_SPECIAL;
497663207adSDon Brady break;
498663207adSDon Brady case VDEV_BIAS_DEDUP:
499663207adSDon Brady bias = VDEV_ALLOC_BIAS_DEDUP;
500663207adSDon Brady break;
501663207adSDon Brady default:
502663207adSDon Brady ASSERT3U(vd->vdev_alloc_bias, ==,
503663207adSDon Brady VDEV_BIAS_NONE);
504663207adSDon Brady }
505663207adSDon Brady fnvlist_add_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
506663207adSDon Brady bias);
507663207adSDon Brady }
508fa9e4066Sahrens }
509fa9e4066Sahrens
5100713e232SGeorge Wilson if (vd->vdev_dtl_sm != NULL) {
511b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
5120713e232SGeorge Wilson space_map_object(vd->vdev_dtl_sm));
5130713e232SGeorge Wilson }
514fa9e4066Sahrens
5155cabbc6bSPrashanth Sreenivasa if (vic->vic_mapping_object != 0) {
5165cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
5175cabbc6bSPrashanth Sreenivasa vic->vic_mapping_object);
5185cabbc6bSPrashanth Sreenivasa }
5195cabbc6bSPrashanth Sreenivasa
5205cabbc6bSPrashanth Sreenivasa if (vic->vic_births_object != 0) {
5215cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
5225cabbc6bSPrashanth Sreenivasa vic->vic_births_object);
5235cabbc6bSPrashanth Sreenivasa }
5245cabbc6bSPrashanth Sreenivasa
5255cabbc6bSPrashanth Sreenivasa if (vic->vic_prev_indirect_vdev != UINT64_MAX) {
5265cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
5275cabbc6bSPrashanth Sreenivasa vic->vic_prev_indirect_vdev);
5285cabbc6bSPrashanth Sreenivasa }
5295cabbc6bSPrashanth Sreenivasa
53088ecc943SGeorge Wilson if (vd->vdev_crtxg)
531b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG, vd->vdev_crtxg);
53288ecc943SGeorge Wilson
533215198a6SJoe Stein if (flags & VDEV_CONFIG_MOS) {
534215198a6SJoe Stein if (vd->vdev_leaf_zap != 0) {
535215198a6SJoe Stein ASSERT(vd->vdev_ops->vdev_op_leaf);
536215198a6SJoe Stein fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_LEAF_ZAP,
537215198a6SJoe Stein vd->vdev_leaf_zap);
538215198a6SJoe Stein }
539215198a6SJoe Stein
540215198a6SJoe Stein if (vd->vdev_top_zap != 0) {
541215198a6SJoe Stein ASSERT(vd == vd->vdev_top);
542215198a6SJoe Stein fnvlist_add_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
543215198a6SJoe Stein vd->vdev_top_zap);
544215198a6SJoe Stein }
545e4c795beSTom Caputi
546e4c795beSTom Caputi if (vd->vdev_resilver_deferred) {
547e4c795beSTom Caputi ASSERT(vd->vdev_ops->vdev_op_leaf);
548e4c795beSTom Caputi ASSERT(spa->spa_resilver_deferred);
549e4c795beSTom Caputi fnvlist_add_boolean(nv, ZPOOL_CONFIG_RESILVER_DEFER);
550e4c795beSTom Caputi }
551215198a6SJoe Stein }
552215198a6SJoe Stein
553fa9e4066Sahrens if (getstats) {
554dd50e0ccSTony Hutter vdev_config_generate_stats(vd, nv);
5553f9d6ad7SLin Ling
55686714001SSerapheim Dimitropoulos root_vdev_actions_getprogress(vd, nv);
5575cabbc6bSPrashanth Sreenivasa
5585cabbc6bSPrashanth Sreenivasa /*
5595cabbc6bSPrashanth Sreenivasa * Note: this can be called from open context
5605cabbc6bSPrashanth Sreenivasa * (spa_get_stats()), so we need the rwlock to prevent
5615cabbc6bSPrashanth Sreenivasa * the mapping from being changed by condensing.
5625cabbc6bSPrashanth Sreenivasa */
5635cabbc6bSPrashanth Sreenivasa rw_enter(&vd->vdev_indirect_rwlock, RW_READER);
5645cabbc6bSPrashanth Sreenivasa if (vd->vdev_indirect_mapping != NULL) {
5655cabbc6bSPrashanth Sreenivasa ASSERT(vd->vdev_indirect_births != NULL);
5665cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_t *vim =
5675cabbc6bSPrashanth Sreenivasa vd->vdev_indirect_mapping;
5685cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
5695cabbc6bSPrashanth Sreenivasa vdev_indirect_mapping_size(vim));
5705cabbc6bSPrashanth Sreenivasa }
5715cabbc6bSPrashanth Sreenivasa rw_exit(&vd->vdev_indirect_rwlock);
5725cabbc6bSPrashanth Sreenivasa if (vd->vdev_mg != NULL &&
5735cabbc6bSPrashanth Sreenivasa vd->vdev_mg->mg_fragmentation != ZFS_FRAG_INVALID) {
5745cabbc6bSPrashanth Sreenivasa /*
5755cabbc6bSPrashanth Sreenivasa * Compute approximately how much memory would be used
5765cabbc6bSPrashanth Sreenivasa * for the indirect mapping if this device were to
5775cabbc6bSPrashanth Sreenivasa * be removed.
5785cabbc6bSPrashanth Sreenivasa *
5795cabbc6bSPrashanth Sreenivasa * Note: If the frag metric is invalid, then not
5805cabbc6bSPrashanth Sreenivasa * enough metaslabs have been converted to have
5815cabbc6bSPrashanth Sreenivasa * histograms.
5825cabbc6bSPrashanth Sreenivasa */
5835cabbc6bSPrashanth Sreenivasa uint64_t seg_count = 0;
584cfd63e1bSMatthew Ahrens uint64_t to_alloc = vd->vdev_stat.vs_alloc;
5855cabbc6bSPrashanth Sreenivasa
5865cabbc6bSPrashanth Sreenivasa /*
5875cabbc6bSPrashanth Sreenivasa * There are the same number of allocated segments
5885cabbc6bSPrashanth Sreenivasa * as free segments, so we will have at least one
589cfd63e1bSMatthew Ahrens * entry per free segment. However, small free
590cfd63e1bSMatthew Ahrens * segments (smaller than vdev_removal_max_span)
591cfd63e1bSMatthew Ahrens * will be combined with adjacent allocated segments
592cfd63e1bSMatthew Ahrens * as a single mapping.
5935cabbc6bSPrashanth Sreenivasa */
5945cabbc6bSPrashanth Sreenivasa for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
595cfd63e1bSMatthew Ahrens if (1ULL << (i + 1) < vdev_removal_max_span) {
596cfd63e1bSMatthew Ahrens to_alloc +=
597cfd63e1bSMatthew Ahrens vd->vdev_mg->mg_histogram[i] <<
598cfd63e1bSMatthew Ahrens i + 1;
599cfd63e1bSMatthew Ahrens } else {
600cfd63e1bSMatthew Ahrens seg_count +=
601cfd63e1bSMatthew Ahrens vd->vdev_mg->mg_histogram[i];
602cfd63e1bSMatthew Ahrens }
6035cabbc6bSPrashanth Sreenivasa }
6045cabbc6bSPrashanth Sreenivasa
6055cabbc6bSPrashanth Sreenivasa /*
606cfd63e1bSMatthew Ahrens * The maximum length of a mapping is
607cfd63e1bSMatthew Ahrens * zfs_remove_max_segment, so we need at least one entry
608cfd63e1bSMatthew Ahrens * per zfs_remove_max_segment of allocated data.
6095cabbc6bSPrashanth Sreenivasa */
610cfd63e1bSMatthew Ahrens seg_count += to_alloc / zfs_remove_max_segment;
6115cabbc6bSPrashanth Sreenivasa
6125cabbc6bSPrashanth Sreenivasa fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
6135cabbc6bSPrashanth Sreenivasa seg_count *
6145cabbc6bSPrashanth Sreenivasa sizeof (vdev_indirect_mapping_entry_phys_t));
6155cabbc6bSPrashanth Sreenivasa }
616fa9e4066Sahrens }
617fa9e4066Sahrens
618fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf) {
619fa9e4066Sahrens nvlist_t **child;
6203f9d6ad7SLin Ling int c, idx;
621fa9e4066Sahrens
62288ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole);
62388ecc943SGeorge Wilson
624fa9e4066Sahrens child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
625fa9e4066Sahrens KM_SLEEP);
626fa9e4066Sahrens
6273f9d6ad7SLin Ling for (c = 0, idx = 0; c < vd->vdev_children; c++) {
6283f9d6ad7SLin Ling vdev_t *cvd = vd->vdev_child[c];
6293f9d6ad7SLin Ling
6303f9d6ad7SLin Ling /*
6313f9d6ad7SLin Ling * If we're generating an nvlist of removing
6323f9d6ad7SLin Ling * vdevs then skip over any device which is
6333f9d6ad7SLin Ling * not being removed.
6343f9d6ad7SLin Ling */
6353f9d6ad7SLin Ling if ((flags & VDEV_CONFIG_REMOVING) &&
6363f9d6ad7SLin Ling !cvd->vdev_removing)
6373f9d6ad7SLin Ling continue;
638fa9e4066Sahrens
6393f9d6ad7SLin Ling child[idx++] = vdev_config_generate(spa, cvd,
6403f9d6ad7SLin Ling getstats, flags);
6413f9d6ad7SLin Ling }
6423f9d6ad7SLin Ling
6433f9d6ad7SLin Ling if (idx) {
644b4952e17SGeorge Wilson fnvlist_add_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
645b4952e17SGeorge Wilson child, idx);
6463f9d6ad7SLin Ling }
647fa9e4066Sahrens
6483f9d6ad7SLin Ling for (c = 0; c < idx; c++)
649fa9e4066Sahrens nvlist_free(child[c]);
650fa9e4066Sahrens
651fa9e4066Sahrens kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
652441d80aaSlling
653441d80aaSlling } else {
654069f55e2SEric Schrock const char *aux = NULL;
655069f55e2SEric Schrock
656ecc2d604Sbonwick if (vd->vdev_offline && !vd->vdev_tmpoffline)
657b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE, B_TRUE);
658b4952e17SGeorge Wilson if (vd->vdev_resilver_txg != 0)
659b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
660b4952e17SGeorge Wilson vd->vdev_resilver_txg);
6613d7072f8Seschrock if (vd->vdev_faulted)
662b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED, B_TRUE);
6633d7072f8Seschrock if (vd->vdev_degraded)
664b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED, B_TRUE);
6653d7072f8Seschrock if (vd->vdev_removed)
666b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED, B_TRUE);
6673d7072f8Seschrock if (vd->vdev_unspare)
668b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE, B_TRUE);
66988ecc943SGeorge Wilson if (vd->vdev_ishole)
670b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE, B_TRUE);
671069f55e2SEric Schrock
672069f55e2SEric Schrock switch (vd->vdev_stat.vs_aux) {
673069f55e2SEric Schrock case VDEV_AUX_ERR_EXCEEDED:
674069f55e2SEric Schrock aux = "err_exceeded";
675069f55e2SEric Schrock break;
676069f55e2SEric Schrock
677069f55e2SEric Schrock case VDEV_AUX_EXTERNAL:
678069f55e2SEric Schrock aux = "external";
679069f55e2SEric Schrock break;
680069f55e2SEric Schrock }
681069f55e2SEric Schrock
682069f55e2SEric Schrock if (aux != NULL)
683b4952e17SGeorge Wilson fnvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE, aux);
6841195e687SMark J Musante
6851195e687SMark J Musante if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
686b4952e17SGeorge Wilson fnvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
687b4952e17SGeorge Wilson vd->vdev_orig_guid);
6881195e687SMark J Musante }
689fa9e4066Sahrens }
690fa9e4066Sahrens
691fa9e4066Sahrens return (nv);
692fa9e4066Sahrens }
693fa9e4066Sahrens
69488ecc943SGeorge Wilson /*
69588ecc943SGeorge Wilson * Generate a view of the top-level vdevs. If we currently have holes
69688ecc943SGeorge Wilson * in the namespace, then generate an array which contains a list of holey
69788ecc943SGeorge Wilson * vdevs. Additionally, add the number of top-level children that currently
69888ecc943SGeorge Wilson * exist.
69988ecc943SGeorge Wilson */
70088ecc943SGeorge Wilson void
vdev_top_config_generate(spa_t * spa,nvlist_t * config)70188ecc943SGeorge Wilson vdev_top_config_generate(spa_t *spa, nvlist_t *config)
70288ecc943SGeorge Wilson {
70388ecc943SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
70488ecc943SGeorge Wilson uint64_t *array;
7053f9d6ad7SLin Ling uint_t c, idx;
70688ecc943SGeorge Wilson
70788ecc943SGeorge Wilson array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
70888ecc943SGeorge Wilson
7093f9d6ad7SLin Ling for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
71088ecc943SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
71188ecc943SGeorge Wilson
7125cabbc6bSPrashanth Sreenivasa if (tvd->vdev_ishole) {
71388ecc943SGeorge Wilson array[idx++] = c;
7145cabbc6bSPrashanth Sreenivasa }
71588ecc943SGeorge Wilson }
71688ecc943SGeorge Wilson
717312c6e15SGeorge Wilson if (idx) {
718312c6e15SGeorge Wilson VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
719312c6e15SGeorge Wilson array, idx) == 0);
720312c6e15SGeorge Wilson }
721312c6e15SGeorge Wilson
72288ecc943SGeorge Wilson VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
72388ecc943SGeorge Wilson rvd->vdev_children) == 0);
72488ecc943SGeorge Wilson
72588ecc943SGeorge Wilson kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
72688ecc943SGeorge Wilson }
72788ecc943SGeorge Wilson
728ad135b5dSChristopher Siden /*
729dfbb9432SGeorge Wilson * Returns the configuration from the label of the given vdev. For vdevs
730dfbb9432SGeorge Wilson * which don't have a txg value stored on their label (i.e. spares/cache)
731dfbb9432SGeorge Wilson * or have not been completely initialized (txg = 0) just return
732dfbb9432SGeorge Wilson * the configuration from the first valid label we find. Otherwise,
733dfbb9432SGeorge Wilson * find the most up-to-date label that does not exceed the specified
734dfbb9432SGeorge Wilson * 'txg' value.
735ad135b5dSChristopher Siden */
736fa9e4066Sahrens nvlist_t *
vdev_label_read_config(vdev_t * vd,uint64_t txg)737dfbb9432SGeorge Wilson vdev_label_read_config(vdev_t *vd, uint64_t txg)
738fa9e4066Sahrens {
7390373e76bSbonwick spa_t *spa = vd->vdev_spa;
740fa9e4066Sahrens nvlist_t *config = NULL;
741fa9e4066Sahrens vdev_phys_t *vp;
742770499e1SDan Kimmel abd_t *vp_abd;
743fa9e4066Sahrens zio_t *zio;
744dfbb9432SGeorge Wilson uint64_t best_txg = 0;
745b6bf6e15SPavel Zakharov uint64_t label_txg = 0;
746dfbb9432SGeorge Wilson int error = 0;
7478956713aSEric Schrock int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
7488956713aSEric Schrock ZIO_FLAG_SPECULATIVE;
749fa9e4066Sahrens
750e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
7510373e76bSbonwick
7520a4e9518Sgw if (!vdev_readable(vd))
753fa9e4066Sahrens return (NULL);
754fa9e4066Sahrens
755770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
756770499e1SDan Kimmel vp = abd_to_buf(vp_abd);
757fa9e4066Sahrens
7588956713aSEric Schrock retry:
759e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) {
760dfbb9432SGeorge Wilson nvlist_t *label = NULL;
761fa9e4066Sahrens
762e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags);
763fa9e4066Sahrens
764770499e1SDan Kimmel vdev_label_read(zio, vd, l, vp_abd,
765fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys),
766e14bb325SJeff Bonwick sizeof (vdev_phys_t), NULL, NULL, flags);
767fa9e4066Sahrens
768fa9e4066Sahrens if (zio_wait(zio) == 0 &&
769fa9e4066Sahrens nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
770dfbb9432SGeorge Wilson &label, 0) == 0) {
771dfbb9432SGeorge Wilson /*
772dfbb9432SGeorge Wilson * Auxiliary vdevs won't have txg values in their
773dfbb9432SGeorge Wilson * labels and newly added vdevs may not have been
774dfbb9432SGeorge Wilson * completely initialized so just return the
775dfbb9432SGeorge Wilson * configuration from the first valid label we
776dfbb9432SGeorge Wilson * encounter.
777dfbb9432SGeorge Wilson */
778dfbb9432SGeorge Wilson error = nvlist_lookup_uint64(label,
779dfbb9432SGeorge Wilson ZPOOL_CONFIG_POOL_TXG, &label_txg);
780dfbb9432SGeorge Wilson if ((error || label_txg == 0) && !config) {
781dfbb9432SGeorge Wilson config = label;
782dfbb9432SGeorge Wilson break;
783dfbb9432SGeorge Wilson } else if (label_txg <= txg && label_txg > best_txg) {
784dfbb9432SGeorge Wilson best_txg = label_txg;
785dfbb9432SGeorge Wilson nvlist_free(config);
786dfbb9432SGeorge Wilson config = fnvlist_dup(label);
787dfbb9432SGeorge Wilson }
788dfbb9432SGeorge Wilson }
789fa9e4066Sahrens
790dfbb9432SGeorge Wilson if (label != NULL) {
791dfbb9432SGeorge Wilson nvlist_free(label);
792dfbb9432SGeorge Wilson label = NULL;
793fa9e4066Sahrens }
794fa9e4066Sahrens }
795fa9e4066Sahrens
7968956713aSEric Schrock if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
7978956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD;
7988956713aSEric Schrock goto retry;
7998956713aSEric Schrock }
8008956713aSEric Schrock
801b6bf6e15SPavel Zakharov /*
802b6bf6e15SPavel Zakharov * We found a valid label but it didn't pass txg restrictions.
803b6bf6e15SPavel Zakharov */
804b6bf6e15SPavel Zakharov if (config == NULL && label_txg != 0) {
805b6bf6e15SPavel Zakharov vdev_dbgmsg(vd, "label discarded as txg is too large "
806b6bf6e15SPavel Zakharov "(%llu > %llu)", (u_longlong_t)label_txg,
807b6bf6e15SPavel Zakharov (u_longlong_t)txg);
808b6bf6e15SPavel Zakharov }
809b6bf6e15SPavel Zakharov
810770499e1SDan Kimmel abd_free(vp_abd);
811fa9e4066Sahrens
812fa9e4066Sahrens return (config);
813fa9e4066Sahrens }
814fa9e4066Sahrens
81539c23413Seschrock /*
81639c23413Seschrock * Determine if a device is in use. The 'spare_guid' parameter will be filled
81739c23413Seschrock * in with the device guid if this spare is active elsewhere on the system.
81839c23413Seschrock */
81939c23413Seschrock static boolean_t
vdev_inuse(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason,uint64_t * spare_guid,uint64_t * l2cache_guid)82039c23413Seschrock vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
821fa94a07fSbrendan uint64_t *spare_guid, uint64_t *l2cache_guid)
82239c23413Seschrock {
82339c23413Seschrock spa_t *spa = vd->vdev_spa;
82439c23413Seschrock uint64_t state, pool_guid, device_guid, txg, spare_pool;
82539c23413Seschrock uint64_t vdtxg = 0;
82639c23413Seschrock nvlist_t *label;
82739c23413Seschrock
82839c23413Seschrock if (spare_guid)
82939c23413Seschrock *spare_guid = 0ULL;
830fa94a07fSbrendan if (l2cache_guid)
831fa94a07fSbrendan *l2cache_guid = 0ULL;
83239c23413Seschrock
83339c23413Seschrock /*
83439c23413Seschrock * Read the label, if any, and perform some basic sanity checks.
83539c23413Seschrock */
836dfbb9432SGeorge Wilson if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
83739c23413Seschrock return (B_FALSE);
83839c23413Seschrock
83939c23413Seschrock (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
84039c23413Seschrock &vdtxg);
84139c23413Seschrock
84239c23413Seschrock if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
84339c23413Seschrock &state) != 0 ||
84439c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
84539c23413Seschrock &device_guid) != 0) {
84639c23413Seschrock nvlist_free(label);
84739c23413Seschrock return (B_FALSE);
84839c23413Seschrock }
84939c23413Seschrock
850fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
85139c23413Seschrock (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
85239c23413Seschrock &pool_guid) != 0 ||
85339c23413Seschrock nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
85439c23413Seschrock &txg) != 0)) {
85539c23413Seschrock nvlist_free(label);
85639c23413Seschrock return (B_FALSE);
85739c23413Seschrock }
85839c23413Seschrock
85939c23413Seschrock nvlist_free(label);
86039c23413Seschrock
86139c23413Seschrock /*
86239c23413Seschrock * Check to see if this device indeed belongs to the pool it claims to
86339c23413Seschrock * be a part of. The only way this is allowed is if the device is a hot
86439c23413Seschrock * spare (which we check for later on).
86539c23413Seschrock */
866fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
86739c23413Seschrock !spa_guid_exists(pool_guid, device_guid) &&
86889a89ebfSlling !spa_spare_exists(device_guid, NULL, NULL) &&
869fa94a07fSbrendan !spa_l2cache_exists(device_guid, NULL))
87039c23413Seschrock return (B_FALSE);
87139c23413Seschrock
87239c23413Seschrock /*
87339c23413Seschrock * If the transaction group is zero, then this an initialized (but
87439c23413Seschrock * unused) label. This is only an error if the create transaction
87539c23413Seschrock * on-disk is the same as the one we're using now, in which case the
87639c23413Seschrock * user has attempted to add the same vdev multiple times in the same
87739c23413Seschrock * transaction.
87839c23413Seschrock */
879fa94a07fSbrendan if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
880fa94a07fSbrendan txg == 0 && vdtxg == crtxg)
88139c23413Seschrock return (B_TRUE);
88239c23413Seschrock
88339c23413Seschrock /*
88439c23413Seschrock * Check to see if this is a spare device. We do an explicit check for
88539c23413Seschrock * spa_has_spare() here because it may be on our pending list of spares
886fa94a07fSbrendan * to add. We also check if it is an l2cache device.
88739c23413Seschrock */
88889a89ebfSlling if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
88939c23413Seschrock spa_has_spare(spa, device_guid)) {
89039c23413Seschrock if (spare_guid)
89139c23413Seschrock *spare_guid = device_guid;
89239c23413Seschrock
89339c23413Seschrock switch (reason) {
89439c23413Seschrock case VDEV_LABEL_CREATE:
895fa94a07fSbrendan case VDEV_LABEL_L2CACHE:
89639c23413Seschrock return (B_TRUE);
89739c23413Seschrock
89839c23413Seschrock case VDEV_LABEL_REPLACE:
89939c23413Seschrock return (!spa_has_spare(spa, device_guid) ||
90039c23413Seschrock spare_pool != 0ULL);
90139c23413Seschrock
90239c23413Seschrock case VDEV_LABEL_SPARE:
90339c23413Seschrock return (spa_has_spare(spa, device_guid));
90439c23413Seschrock }
90539c23413Seschrock }
90639c23413Seschrock
907fa94a07fSbrendan /*
908fa94a07fSbrendan * Check to see if this is an l2cache device.
909fa94a07fSbrendan */
910fa94a07fSbrendan if (spa_l2cache_exists(device_guid, NULL))
911fa94a07fSbrendan return (B_TRUE);
912fa94a07fSbrendan
913f9af39baSGeorge Wilson /*
914f9af39baSGeorge Wilson * We can't rely on a pool's state if it's been imported
915f9af39baSGeorge Wilson * read-only. Instead we look to see if the pools is marked
916f9af39baSGeorge Wilson * read-only in the namespace and set the state to active.
917f9af39baSGeorge Wilson */
9185bdd995dSRichard Yao if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
9195bdd995dSRichard Yao (spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
920f9af39baSGeorge Wilson spa_mode(spa) == FREAD)
921f9af39baSGeorge Wilson state = POOL_STATE_ACTIVE;
922f9af39baSGeorge Wilson
92339c23413Seschrock /*
92439c23413Seschrock * If the device is marked ACTIVE, then this device is in use by another
92539c23413Seschrock * pool on the system.
92639c23413Seschrock */
92739c23413Seschrock return (state == POOL_STATE_ACTIVE);
92839c23413Seschrock }
92939c23413Seschrock
93039c23413Seschrock /*
93139c23413Seschrock * Initialize a vdev label. We check to make sure each leaf device is not in
93239c23413Seschrock * use, and writable. We put down an initial label which we will later
93339c23413Seschrock * overwrite with a complete label. Note that it's important to do this
93439c23413Seschrock * sequentially, not in parallel, so that we catch cases of multiple use of the
93539c23413Seschrock * same leaf vdev in the vdev we're creating -- e.g. mirroring a disk with
93639c23413Seschrock * itself.
93739c23413Seschrock */
93839c23413Seschrock int
vdev_label_init(vdev_t * vd,uint64_t crtxg,vdev_labeltype_t reason)93939c23413Seschrock vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
940fa9e4066Sahrens {
941fa9e4066Sahrens spa_t *spa = vd->vdev_spa;
942fa9e4066Sahrens nvlist_t *label;
943fa9e4066Sahrens vdev_phys_t *vp;
944770499e1SDan Kimmel abd_t *vp_abd;
945c4ecba8aSPaul Dagnelie abd_t *bootenv;
946ecc2d604Sbonwick uberblock_t *ub;
947770499e1SDan Kimmel abd_t *ub_abd;
948fa9e4066Sahrens zio_t *zio;
949fa9e4066Sahrens char *buf;
950fa9e4066Sahrens size_t buflen;
951fa9e4066Sahrens int error;
952*74009d0fSToomas Soome uint64_t spare_guid = 0, l2cache_guid;
953e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
954fa9e4066Sahrens
955e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
9560373e76bSbonwick
957e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++)
95839c23413Seschrock if ((error = vdev_label_init(vd->vdev_child[c],
95939c23413Seschrock crtxg, reason)) != 0)
960fa9e4066Sahrens return (error);
961fa9e4066Sahrens
96288ecc943SGeorge Wilson /* Track the creation time for this vdev */
96388ecc943SGeorge Wilson vd->vdev_crtxg = crtxg;
96488ecc943SGeorge Wilson
965973c78e9SGeorge Wilson if (!vd->vdev_ops->vdev_op_leaf || !spa_writeable(spa))
966fa9e4066Sahrens return (0);
967fa9e4066Sahrens
968fa9e4066Sahrens /*
96939c23413Seschrock * Dead vdevs cannot be initialized.
970fa9e4066Sahrens */
971fa9e4066Sahrens if (vdev_is_dead(vd))
972be6fd75aSMatthew Ahrens return (SET_ERROR(EIO));
973fa9e4066Sahrens
974fa9e4066Sahrens /*
97539c23413Seschrock * Determine if the vdev is in use.
976fa9e4066Sahrens */
9771195e687SMark J Musante if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
978fa94a07fSbrendan vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
979be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY));
98039c23413Seschrock
98139c23413Seschrock /*
982fa94a07fSbrendan * If this is a request to add or replace a spare or l2cache device
983fa94a07fSbrendan * that is in use elsewhere on the system, then we must update the
984fa94a07fSbrendan * guid (which was initialized to a random value) to reflect the
985fa94a07fSbrendan * actual GUID (which is shared between multiple pools).
98639c23413Seschrock */
987fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
988fa94a07fSbrendan spare_guid != 0ULL) {
98931157203SJeff Bonwick uint64_t guid_delta = spare_guid - vd->vdev_guid;
99099653d4eSeschrock
99131157203SJeff Bonwick vd->vdev_guid += guid_delta;
99231157203SJeff Bonwick
99331157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
99431157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta;
99539c23413Seschrock
99699653d4eSeschrock /*
99739c23413Seschrock * If this is a replacement, then we want to fallthrough to the
99839c23413Seschrock * rest of the code. If we're adding a spare, then it's already
9993d7072f8Seschrock * labeled appropriately and we can just return.
100099653d4eSeschrock */
100139c23413Seschrock if (reason == VDEV_LABEL_SPARE)
100239c23413Seschrock return (0);
10031195e687SMark J Musante ASSERT(reason == VDEV_LABEL_REPLACE ||
10041195e687SMark J Musante reason == VDEV_LABEL_SPLIT);
1005fa9e4066Sahrens }
1006fa9e4066Sahrens
1007fa94a07fSbrendan if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
1008fa94a07fSbrendan l2cache_guid != 0ULL) {
100931157203SJeff Bonwick uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
101031157203SJeff Bonwick
101131157203SJeff Bonwick vd->vdev_guid += guid_delta;
1012fa94a07fSbrendan
101331157203SJeff Bonwick for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
101431157203SJeff Bonwick pvd->vdev_guid_sum += guid_delta;
1015fa94a07fSbrendan
1016fa94a07fSbrendan /*
1017fa94a07fSbrendan * If this is a replacement, then we want to fallthrough to the
1018fa94a07fSbrendan * rest of the code. If we're adding an l2cache, then it's
1019fa94a07fSbrendan * already labeled appropriately and we can just return.
1020fa94a07fSbrendan */
1021fa94a07fSbrendan if (reason == VDEV_LABEL_L2CACHE)
1022fa94a07fSbrendan return (0);
1023fa94a07fSbrendan ASSERT(reason == VDEV_LABEL_REPLACE);
1024fa94a07fSbrendan }
1025fa94a07fSbrendan
1026fa9e4066Sahrens /*
102739c23413Seschrock * Initialize its label.
1028fa9e4066Sahrens */
1029770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1030770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t));
1031770499e1SDan Kimmel vp = abd_to_buf(vp_abd);
1032fa9e4066Sahrens
1033fa9e4066Sahrens /*
1034fa9e4066Sahrens * Generate a label describing the pool and our top-level vdev.
1035fa9e4066Sahrens * We mark it as being from txg 0 to indicate that it's not
1036fa9e4066Sahrens * really part of an active pool just yet. The labels will
1037fa9e4066Sahrens * be written again with a meaningful txg by spa_sync().
1038fa9e4066Sahrens */
103939c23413Seschrock if (reason == VDEV_LABEL_SPARE ||
104039c23413Seschrock (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
104139c23413Seschrock /*
104239c23413Seschrock * For inactive hot spares, we generate a special label that
104339c23413Seschrock * identifies as a mutually shared hot spare. We write the
104439c23413Seschrock * label if we are adding a hot spare, or if we are removing an
104539c23413Seschrock * active hot spare (in which case we want to revert the
104639c23413Seschrock * labels).
104739c23413Seschrock */
104899653d4eSeschrock VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
104999653d4eSeschrock
105099653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
105199653d4eSeschrock spa_version(spa)) == 0);
105299653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
105399653d4eSeschrock POOL_STATE_SPARE) == 0);
105499653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
105599653d4eSeschrock vd->vdev_guid) == 0);
1056fa94a07fSbrendan } else if (reason == VDEV_LABEL_L2CACHE ||
1057fa94a07fSbrendan (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
1058fa94a07fSbrendan /*
1059fa94a07fSbrendan * For level 2 ARC devices, add a special label.
1060fa94a07fSbrendan */
1061fa94a07fSbrendan VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1062fa94a07fSbrendan
1063fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
1064fa94a07fSbrendan spa_version(spa)) == 0);
1065fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1066fa94a07fSbrendan POOL_STATE_L2CACHE) == 0);
1067fa94a07fSbrendan VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
1068fa94a07fSbrendan vd->vdev_guid) == 0);
106999653d4eSeschrock } else {
10701195e687SMark J Musante uint64_t txg = 0ULL;
10711195e687SMark J Musante
10721195e687SMark J Musante if (reason == VDEV_LABEL_SPLIT)
10731195e687SMark J Musante txg = spa->spa_uberblock.ub_txg;
10741195e687SMark J Musante label = spa_config_generate(spa, vd, txg, B_FALSE);
107599653d4eSeschrock
107699653d4eSeschrock /*
107799653d4eSeschrock * Add our creation time. This allows us to detect multiple
107899653d4eSeschrock * vdev uses as described above, and automatically expires if we
107999653d4eSeschrock * fail.
108099653d4eSeschrock */
108199653d4eSeschrock VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
108299653d4eSeschrock crtxg) == 0);
108399653d4eSeschrock }
1084fa9e4066Sahrens
1085fa9e4066Sahrens buf = vp->vp_nvlist;
1086fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist);
1087fa9e4066Sahrens
1088a75573b6Smmusante error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
1089a75573b6Smmusante if (error != 0) {
1090fa9e4066Sahrens nvlist_free(label);
1091770499e1SDan Kimmel abd_free(vp_abd);
1092a75573b6Smmusante /* EFAULT means nvlist_pack ran out of room */
1093a75573b6Smmusante return (error == EFAULT ? ENAMETOOLONG : EINVAL);
1094fa9e4066Sahrens }
1095fa9e4066Sahrens
1096fa9e4066Sahrens /*
1097fa9e4066Sahrens * Initialize uberblock template.
1098fa9e4066Sahrens */
1099770499e1SDan Kimmel ub_abd = abd_alloc_linear(VDEV_UBERBLOCK_RING, B_TRUE);
1100770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_RING);
1101770499e1SDan Kimmel abd_copy_from_buf(ub_abd, &spa->spa_uberblock, sizeof (uberblock_t));
1102770499e1SDan Kimmel ub = abd_to_buf(ub_abd);
1103ecc2d604Sbonwick ub->ub_txg = 0;
1104fa9e4066Sahrens
1105f83ffe1aSLin Ling /* Initialize the 2nd padding area. */
1106c4ecba8aSPaul Dagnelie bootenv = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1107c4ecba8aSPaul Dagnelie abd_zero(bootenv, VDEV_PAD_SIZE);
1108f83ffe1aSLin Ling
1109fa9e4066Sahrens /*
1110fa9e4066Sahrens * Write everything in parallel.
1111fa9e4066Sahrens */
11128956713aSEric Schrock retry:
111317f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags);
1114fa9e4066Sahrens
1115e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) {
1116fa9e4066Sahrens
1117770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd,
1118fa9e4066Sahrens offsetof(vdev_label_t, vl_vdev_phys),
111917f17c2dSbonwick sizeof (vdev_phys_t), NULL, NULL, flags);
1120fa9e4066Sahrens
1121f83ffe1aSLin Ling /*
1122f83ffe1aSLin Ling * Skip the 1st padding area.
1123f83ffe1aSLin Ling * Zero out the 2nd padding area where it might have
1124f83ffe1aSLin Ling * left over data from previous filesystem format.
1125f83ffe1aSLin Ling */
1126c4ecba8aSPaul Dagnelie vdev_label_write(zio, vd, l, bootenv,
1127c4ecba8aSPaul Dagnelie offsetof(vdev_label_t, vl_be),
1128f83ffe1aSLin Ling VDEV_PAD_SIZE, NULL, NULL, flags);
1129f83ffe1aSLin Ling
1130770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd,
1131f64c0e34SEric Taylor offsetof(vdev_label_t, vl_uberblock),
1132f64c0e34SEric Taylor VDEV_UBERBLOCK_RING, NULL, NULL, flags);
1133fa9e4066Sahrens }
1134fa9e4066Sahrens
1135fa9e4066Sahrens error = zio_wait(zio);
1136fa9e4066Sahrens
11378956713aSEric Schrock if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
11388956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD;
11398956713aSEric Schrock goto retry;
11408956713aSEric Schrock }
11418956713aSEric Schrock
1142fa9e4066Sahrens nvlist_free(label);
1143c4ecba8aSPaul Dagnelie abd_free(bootenv);
1144770499e1SDan Kimmel abd_free(ub_abd);
1145770499e1SDan Kimmel abd_free(vp_abd);
1146fa9e4066Sahrens
114739c23413Seschrock /*
114839c23413Seschrock * If this vdev hasn't been previously identified as a spare, then we
11493d7072f8Seschrock * mark it as such only if a) we are labeling it as a spare, or b) it
1150fa94a07fSbrendan * exists as a spare elsewhere in the system. Do the same for
1151fa94a07fSbrendan * level 2 ARC devices.
115239c23413Seschrock */
115339c23413Seschrock if (error == 0 && !vd->vdev_isspare &&
115439c23413Seschrock (reason == VDEV_LABEL_SPARE ||
115589a89ebfSlling spa_spare_exists(vd->vdev_guid, NULL, NULL)))
115639c23413Seschrock spa_spare_add(vd);
115799653d4eSeschrock
1158fa94a07fSbrendan if (error == 0 && !vd->vdev_isl2cache &&
1159fa94a07fSbrendan (reason == VDEV_LABEL_L2CACHE ||
1160fa94a07fSbrendan spa_l2cache_exists(vd->vdev_guid, NULL)))
1161fa94a07fSbrendan spa_l2cache_add(vd);
1162fa94a07fSbrendan
116339c23413Seschrock return (error);
116499653d4eSeschrock }
116599653d4eSeschrock
1166c4ecba8aSPaul Dagnelie /*
1167c4ecba8aSPaul Dagnelie * Done callback for vdev_label_read_bootenv_impl. If this is the first
1168c4ecba8aSPaul Dagnelie * callback to finish, store our abd in the callback pointer. Otherwise, we
1169c4ecba8aSPaul Dagnelie * just free our abd and return.
1170c4ecba8aSPaul Dagnelie */
1171c4ecba8aSPaul Dagnelie static void
vdev_label_read_bootenv_done(zio_t * zio)1172c4ecba8aSPaul Dagnelie vdev_label_read_bootenv_done(zio_t *zio)
1173c4ecba8aSPaul Dagnelie {
1174c4ecba8aSPaul Dagnelie zio_t *rio = zio->io_private;
1175c4ecba8aSPaul Dagnelie abd_t **cbp = rio->io_private;
1176c4ecba8aSPaul Dagnelie
1177c4ecba8aSPaul Dagnelie ASSERT3U(zio->io_size, ==, VDEV_PAD_SIZE);
1178c4ecba8aSPaul Dagnelie
1179c4ecba8aSPaul Dagnelie if (zio->io_error == 0) {
1180c4ecba8aSPaul Dagnelie mutex_enter(&rio->io_lock);
1181c4ecba8aSPaul Dagnelie if (*cbp == NULL) {
1182c4ecba8aSPaul Dagnelie /* Will free this buffer in vdev_label_read_bootenv. */
1183c4ecba8aSPaul Dagnelie *cbp = zio->io_abd;
1184c4ecba8aSPaul Dagnelie } else {
1185c4ecba8aSPaul Dagnelie abd_free(zio->io_abd);
1186c4ecba8aSPaul Dagnelie }
1187c4ecba8aSPaul Dagnelie mutex_exit(&rio->io_lock);
1188c4ecba8aSPaul Dagnelie } else {
1189c4ecba8aSPaul Dagnelie abd_free(zio->io_abd);
1190c4ecba8aSPaul Dagnelie }
1191c4ecba8aSPaul Dagnelie }
1192c4ecba8aSPaul Dagnelie
1193c4ecba8aSPaul Dagnelie static void
vdev_label_read_bootenv_impl(zio_t * zio,vdev_t * vd,int flags)1194c4ecba8aSPaul Dagnelie vdev_label_read_bootenv_impl(zio_t *zio, vdev_t *vd, int flags)
1195c4ecba8aSPaul Dagnelie {
1196c4ecba8aSPaul Dagnelie for (int c = 0; c < vd->vdev_children; c++)
1197c4ecba8aSPaul Dagnelie vdev_label_read_bootenv_impl(zio, vd->vdev_child[c], flags);
1198c4ecba8aSPaul Dagnelie
1199c4ecba8aSPaul Dagnelie /*
1200c4ecba8aSPaul Dagnelie * We just use the first label that has a correct checksum; the
1201c4ecba8aSPaul Dagnelie * bootloader should have rewritten them all to be the same on boot,
1202c4ecba8aSPaul Dagnelie * and any changes we made since boot have been the same across all
1203c4ecba8aSPaul Dagnelie * labels.
1204c4ecba8aSPaul Dagnelie */
1205c4ecba8aSPaul Dagnelie if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
120609fcda9fSToomas Soome for (int l = 0; l < VDEV_LABELS; l++) {
1207c4ecba8aSPaul Dagnelie vdev_label_read(zio, vd, l,
1208c4ecba8aSPaul Dagnelie abd_alloc_linear(VDEV_PAD_SIZE, B_FALSE),
1209c4ecba8aSPaul Dagnelie offsetof(vdev_label_t, vl_be), VDEV_PAD_SIZE,
1210c4ecba8aSPaul Dagnelie vdev_label_read_bootenv_done, zio, flags);
1211c4ecba8aSPaul Dagnelie }
1212c4ecba8aSPaul Dagnelie }
1213c4ecba8aSPaul Dagnelie }
1214c4ecba8aSPaul Dagnelie
1215c4ecba8aSPaul Dagnelie int
vdev_label_read_bootenv(vdev_t * rvd,nvlist_t * bootenv)121609fcda9fSToomas Soome vdev_label_read_bootenv(vdev_t *rvd, nvlist_t *bootenv)
1217c4ecba8aSPaul Dagnelie {
121809fcda9fSToomas Soome nvlist_t *config;
1219c4ecba8aSPaul Dagnelie spa_t *spa = rvd->vdev_spa;
1220c4ecba8aSPaul Dagnelie abd_t *abd = NULL;
1221c4ecba8aSPaul Dagnelie int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1222c4ecba8aSPaul Dagnelie ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1223c4ecba8aSPaul Dagnelie
122409fcda9fSToomas Soome ASSERT(bootenv);
1225c4ecba8aSPaul Dagnelie ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1226c4ecba8aSPaul Dagnelie
1227c4ecba8aSPaul Dagnelie zio_t *zio = zio_root(spa, NULL, &abd, flags);
1228c4ecba8aSPaul Dagnelie vdev_label_read_bootenv_impl(zio, rvd, flags);
1229c4ecba8aSPaul Dagnelie int err = zio_wait(zio);
1230c4ecba8aSPaul Dagnelie
1231c4ecba8aSPaul Dagnelie if (abd != NULL) {
123209fcda9fSToomas Soome char *buf;
1233c4ecba8aSPaul Dagnelie vdev_boot_envblock_t *vbe = abd_to_buf(abd);
123409fcda9fSToomas Soome
123509fcda9fSToomas Soome vbe->vbe_version = ntohll(vbe->vbe_version);
123609fcda9fSToomas Soome switch (vbe->vbe_version) {
123709fcda9fSToomas Soome case VB_RAW:
123809fcda9fSToomas Soome /*
123909fcda9fSToomas Soome * if we have textual data in vbe_bootenv, create nvlist
124009fcda9fSToomas Soome * with key "envmap".
124109fcda9fSToomas Soome */
124209fcda9fSToomas Soome fnvlist_add_uint64(bootenv, BOOTENV_VERSION, VB_RAW);
124309fcda9fSToomas Soome vbe->vbe_bootenv[sizeof (vbe->vbe_bootenv) - 1] = '\0';
124409fcda9fSToomas Soome fnvlist_add_string(bootenv, GRUB_ENVMAP,
124509fcda9fSToomas Soome vbe->vbe_bootenv);
124609fcda9fSToomas Soome break;
124709fcda9fSToomas Soome
124809fcda9fSToomas Soome case VB_NVLIST:
124909fcda9fSToomas Soome err = nvlist_unpack(vbe->vbe_bootenv,
125009fcda9fSToomas Soome sizeof (vbe->vbe_bootenv), &config, 0);
125109fcda9fSToomas Soome if (err == 0) {
125209fcda9fSToomas Soome fnvlist_merge(bootenv, config);
125309fcda9fSToomas Soome nvlist_free(config);
125409fcda9fSToomas Soome break;
125509fcda9fSToomas Soome }
125609fcda9fSToomas Soome /* FALLTHROUGH */
125709fcda9fSToomas Soome default:
125809fcda9fSToomas Soome /* Check for FreeBSD zfs bootonce command string */
125909fcda9fSToomas Soome buf = abd_to_buf(abd);
126009fcda9fSToomas Soome if (*buf == '\0') {
126109fcda9fSToomas Soome fnvlist_add_uint64(bootenv, BOOTENV_VERSION,
126209fcda9fSToomas Soome VB_NVLIST);
126309fcda9fSToomas Soome break;
126409fcda9fSToomas Soome }
126509fcda9fSToomas Soome fnvlist_add_string(bootenv, FREEBSD_BOOTONCE, buf);
1266c4ecba8aSPaul Dagnelie }
126709fcda9fSToomas Soome
126809fcda9fSToomas Soome /*
126909fcda9fSToomas Soome * abd was allocated in vdev_label_read_bootenv_impl()
127009fcda9fSToomas Soome */
1271c4ecba8aSPaul Dagnelie abd_free(abd);
127209fcda9fSToomas Soome /*
127309fcda9fSToomas Soome * If we managed to read any successfully,
127409fcda9fSToomas Soome * return success.
127509fcda9fSToomas Soome */
1276c4ecba8aSPaul Dagnelie return (0);
1277c4ecba8aSPaul Dagnelie }
1278c4ecba8aSPaul Dagnelie return (err);
1279c4ecba8aSPaul Dagnelie }
1280c4ecba8aSPaul Dagnelie
1281c4ecba8aSPaul Dagnelie int
vdev_label_write_bootenv(vdev_t * vd,nvlist_t * env)128209fcda9fSToomas Soome vdev_label_write_bootenv(vdev_t *vd, nvlist_t *env)
1283c4ecba8aSPaul Dagnelie {
1284c4ecba8aSPaul Dagnelie zio_t *zio;
1285c4ecba8aSPaul Dagnelie spa_t *spa = vd->vdev_spa;
1286c4ecba8aSPaul Dagnelie vdev_boot_envblock_t *bootenv;
1287c4ecba8aSPaul Dagnelie int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
128809fcda9fSToomas Soome int error;
128909fcda9fSToomas Soome size_t nvsize;
129009fcda9fSToomas Soome char *nvbuf;
1291c4ecba8aSPaul Dagnelie
129209fcda9fSToomas Soome error = nvlist_size(env, &nvsize, NV_ENCODE_XDR);
129309fcda9fSToomas Soome if (error != 0)
129409fcda9fSToomas Soome return (SET_ERROR(error));
129509fcda9fSToomas Soome
129609fcda9fSToomas Soome if (nvsize >= sizeof (bootenv->vbe_bootenv)) {
1297c4ecba8aSPaul Dagnelie return (SET_ERROR(E2BIG));
1298c4ecba8aSPaul Dagnelie }
1299c4ecba8aSPaul Dagnelie
1300c4ecba8aSPaul Dagnelie ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1301c4ecba8aSPaul Dagnelie
130209fcda9fSToomas Soome error = ENXIO;
1303c4ecba8aSPaul Dagnelie for (int c = 0; c < vd->vdev_children; c++) {
130409fcda9fSToomas Soome int child_err;
130509fcda9fSToomas Soome
130609fcda9fSToomas Soome child_err = vdev_label_write_bootenv(vd->vdev_child[c], env);
1307c4ecba8aSPaul Dagnelie /*
1308c4ecba8aSPaul Dagnelie * As long as any of the disks managed to write all of their
1309c4ecba8aSPaul Dagnelie * labels successfully, return success.
1310c4ecba8aSPaul Dagnelie */
1311c4ecba8aSPaul Dagnelie if (child_err == 0)
1312c4ecba8aSPaul Dagnelie error = child_err;
1313c4ecba8aSPaul Dagnelie }
1314c4ecba8aSPaul Dagnelie
1315c4ecba8aSPaul Dagnelie if (!vd->vdev_ops->vdev_op_leaf || vdev_is_dead(vd) ||
1316c4ecba8aSPaul Dagnelie !vdev_writeable(vd)) {
1317c4ecba8aSPaul Dagnelie return (error);
1318c4ecba8aSPaul Dagnelie }
1319c4ecba8aSPaul Dagnelie ASSERT3U(sizeof (*bootenv), ==, VDEV_PAD_SIZE);
1320c4ecba8aSPaul Dagnelie abd_t *abd = abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE);
1321c4ecba8aSPaul Dagnelie abd_zero(abd, VDEV_PAD_SIZE);
132209fcda9fSToomas Soome
1323c4ecba8aSPaul Dagnelie bootenv = abd_borrow_buf_copy(abd, VDEV_PAD_SIZE);
132409fcda9fSToomas Soome nvbuf = bootenv->vbe_bootenv;
132509fcda9fSToomas Soome nvsize = sizeof (bootenv->vbe_bootenv);
132609fcda9fSToomas Soome
132709fcda9fSToomas Soome bootenv->vbe_version = fnvlist_lookup_uint64(env, BOOTENV_VERSION);
132809fcda9fSToomas Soome switch (bootenv->vbe_version) {
132909fcda9fSToomas Soome case VB_RAW:
133009fcda9fSToomas Soome if (nvlist_lookup_string(env, GRUB_ENVMAP, &nvbuf) == 0) {
133109fcda9fSToomas Soome (void) strlcpy(bootenv->vbe_bootenv, nvbuf, nvsize);
133209fcda9fSToomas Soome }
133309fcda9fSToomas Soome error = 0;
133409fcda9fSToomas Soome break;
133509fcda9fSToomas Soome
133609fcda9fSToomas Soome case VB_NVLIST:
133709fcda9fSToomas Soome error = nvlist_pack(env, &nvbuf, &nvsize, NV_ENCODE_XDR,
133809fcda9fSToomas Soome KM_SLEEP);
133909fcda9fSToomas Soome break;
1340c4ecba8aSPaul Dagnelie
134109fcda9fSToomas Soome default:
134209fcda9fSToomas Soome error = EINVAL;
134309fcda9fSToomas Soome break;
134409fcda9fSToomas Soome }
134509fcda9fSToomas Soome
134609fcda9fSToomas Soome if (error == 0) {
134709fcda9fSToomas Soome bootenv->vbe_version = htonll(bootenv->vbe_version);
134809fcda9fSToomas Soome abd_return_buf_copy(abd, bootenv, VDEV_PAD_SIZE);
134909fcda9fSToomas Soome } else {
135009fcda9fSToomas Soome abd_free(abd);
135109fcda9fSToomas Soome return (SET_ERROR(error));
135209fcda9fSToomas Soome }
1353c4ecba8aSPaul Dagnelie
1354c4ecba8aSPaul Dagnelie retry:
1355c4ecba8aSPaul Dagnelie zio = zio_root(spa, NULL, NULL, flags);
135609fcda9fSToomas Soome for (int l = 0; l < VDEV_LABELS; l++) {
1357c4ecba8aSPaul Dagnelie vdev_label_write(zio, vd, l, abd,
1358c4ecba8aSPaul Dagnelie offsetof(vdev_label_t, vl_be),
1359c4ecba8aSPaul Dagnelie VDEV_PAD_SIZE, NULL, NULL, flags);
1360c4ecba8aSPaul Dagnelie }
1361c4ecba8aSPaul Dagnelie
1362c4ecba8aSPaul Dagnelie error = zio_wait(zio);
1363c4ecba8aSPaul Dagnelie if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
1364c4ecba8aSPaul Dagnelie flags |= ZIO_FLAG_TRYHARD;
1365c4ecba8aSPaul Dagnelie goto retry;
1366c4ecba8aSPaul Dagnelie }
1367c4ecba8aSPaul Dagnelie
1368c4ecba8aSPaul Dagnelie abd_free(abd);
1369c4ecba8aSPaul Dagnelie return (error);
1370c4ecba8aSPaul Dagnelie }
1371c4ecba8aSPaul Dagnelie
1372fa9e4066Sahrens /*
1373fa9e4066Sahrens * ==========================================================================
1374fa9e4066Sahrens * uberblock load/sync
1375fa9e4066Sahrens * ==========================================================================
1376fa9e4066Sahrens */
1377fa9e4066Sahrens
1378fa9e4066Sahrens /*
1379fa9e4066Sahrens * Consider the following situation: txg is safely synced to disk. We've
1380fa9e4066Sahrens * written the first uberblock for txg + 1, and then we lose power. When we
1381fa9e4066Sahrens * come back up, we fail to see the uberblock for txg + 1 because, say,
1382fa9e4066Sahrens * it was on a mirrored device and the replica to which we wrote txg + 1
1383fa9e4066Sahrens * is now offline. If we then make some changes and sync txg + 1, and then
1384ad135b5dSChristopher Siden * the missing replica comes back, then for a few seconds we'll have two
1385fa9e4066Sahrens * conflicting uberblocks on disk with the same txg. The solution is simple:
1386fa9e4066Sahrens * among uberblocks with equal txg, choose the one with the latest timestamp.
1387fa9e4066Sahrens */
1388fa9e4066Sahrens static int
vdev_uberblock_compare(const uberblock_t * ub1,const uberblock_t * ub2)1389c4ab0d3fSGvozden Neskovic vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2)
1390fa9e4066Sahrens {
13914d7988d6SPaul Dagnelie int cmp = TREE_CMP(ub1->ub_txg, ub2->ub_txg);
13924348eb90SOlaf Faaland
1393c4ab0d3fSGvozden Neskovic if (likely(cmp))
1394c4ab0d3fSGvozden Neskovic return (cmp);
1395fa9e4066Sahrens
13964d7988d6SPaul Dagnelie cmp = TREE_CMP(ub1->ub_timestamp, ub2->ub_timestamp);
13974348eb90SOlaf Faaland if (likely(cmp))
13984348eb90SOlaf Faaland return (cmp);
13994348eb90SOlaf Faaland
14004348eb90SOlaf Faaland /*
14014348eb90SOlaf Faaland * If MMP_VALID(ub) && MMP_SEQ_VALID(ub) then the host has an MMP-aware
14024348eb90SOlaf Faaland * ZFS, e.g. zfsonlinux >= 0.7.
14034348eb90SOlaf Faaland *
14044348eb90SOlaf Faaland * If one ub has MMP and the other does not, they were written by
14054348eb90SOlaf Faaland * different hosts, which matters for MMP. So we treat no MMP/no SEQ as
14064348eb90SOlaf Faaland * a 0 value.
14074348eb90SOlaf Faaland *
14084348eb90SOlaf Faaland * Since timestamp and txg are the same if we get this far, either is
14094348eb90SOlaf Faaland * acceptable for importing the pool.
14104348eb90SOlaf Faaland */
14114348eb90SOlaf Faaland unsigned int seq1 = 0;
14124348eb90SOlaf Faaland unsigned int seq2 = 0;
14134348eb90SOlaf Faaland
14144348eb90SOlaf Faaland if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1))
14154348eb90SOlaf Faaland seq1 = MMP_SEQ(ub1);
14164348eb90SOlaf Faaland
14174348eb90SOlaf Faaland if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2))
14184348eb90SOlaf Faaland seq2 = MMP_SEQ(ub2);
14194348eb90SOlaf Faaland
14204d7988d6SPaul Dagnelie return (TREE_CMP(seq1, seq2));
1421fa9e4066Sahrens }
1422fa9e4066Sahrens
1423ad135b5dSChristopher Siden struct ubl_cbdata {
1424ad135b5dSChristopher Siden uberblock_t *ubl_ubbest; /* Best uberblock */
1425ad135b5dSChristopher Siden vdev_t *ubl_vd; /* vdev associated with the above */
1426ad135b5dSChristopher Siden };
1427ad135b5dSChristopher Siden
1428fa9e4066Sahrens static void
vdev_uberblock_load_done(zio_t * zio)1429fa9e4066Sahrens vdev_uberblock_load_done(zio_t *zio)
1430fa9e4066Sahrens {
1431ad135b5dSChristopher Siden vdev_t *vd = zio->io_vd;
1432468c413aSTim Haley spa_t *spa = zio->io_spa;
1433e14bb325SJeff Bonwick zio_t *rio = zio->io_private;
1434770499e1SDan Kimmel uberblock_t *ub = abd_to_buf(zio->io_abd);
1435ad135b5dSChristopher Siden struct ubl_cbdata *cbp = rio->io_private;
1436fa9e4066Sahrens
1437ad135b5dSChristopher Siden ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
1438fa9e4066Sahrens
1439ea8dc4b6Seschrock if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
1440e14bb325SJeff Bonwick mutex_enter(&rio->io_lock);
1441468c413aSTim Haley if (ub->ub_txg <= spa->spa_load_max_txg &&
1442ad135b5dSChristopher Siden vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
1443ad135b5dSChristopher Siden /*
1444dfbb9432SGeorge Wilson * Keep track of the vdev in which this uberblock
1445dfbb9432SGeorge Wilson * was found. We will use this information later
1446dfbb9432SGeorge Wilson * to obtain the config nvlist associated with
1447ad135b5dSChristopher Siden * this uberblock.
1448ad135b5dSChristopher Siden */
1449ad135b5dSChristopher Siden *cbp->ubl_ubbest = *ub;
1450ad135b5dSChristopher Siden cbp->ubl_vd = vd;
1451ad135b5dSChristopher Siden }
1452e14bb325SJeff Bonwick mutex_exit(&rio->io_lock);
1453fa9e4066Sahrens }
1454fa9e4066Sahrens
1455770499e1SDan Kimmel abd_free(zio->io_abd);
1456fa9e4066Sahrens }
1457fa9e4066Sahrens
1458ad135b5dSChristopher Siden static void
vdev_uberblock_load_impl(zio_t * zio,vdev_t * vd,int flags,struct ubl_cbdata * cbp)1459ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
1460ad135b5dSChristopher Siden struct ubl_cbdata *cbp)
1461fa9e4066Sahrens {
1462e14bb325SJeff Bonwick for (int c = 0; c < vd->vdev_children; c++)
1463ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
1464fa9e4066Sahrens
1465e14bb325SJeff Bonwick if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1466e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++) {
1467e14bb325SJeff Bonwick for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
1468e14bb325SJeff Bonwick vdev_label_read(zio, vd, l,
1469770499e1SDan Kimmel abd_alloc_linear(VDEV_UBERBLOCK_SIZE(vd),
1470770499e1SDan Kimmel B_TRUE), VDEV_UBERBLOCK_OFFSET(vd, n),
1471e14bb325SJeff Bonwick VDEV_UBERBLOCK_SIZE(vd),
1472e14bb325SJeff Bonwick vdev_uberblock_load_done, zio, flags);
1473e14bb325SJeff Bonwick }
1474fa9e4066Sahrens }
1475fa9e4066Sahrens }
1476ad135b5dSChristopher Siden }
1477e14bb325SJeff Bonwick
1478ad135b5dSChristopher Siden /*
1479ad135b5dSChristopher Siden * Reads the 'best' uberblock from disk along with its associated
1480ad135b5dSChristopher Siden * configuration. First, we read the uberblock array of each label of each
1481ad135b5dSChristopher Siden * vdev, keeping track of the uberblock with the highest txg in each array.
1482dfbb9432SGeorge Wilson * Then, we read the configuration from the same vdev as the best uberblock.
1483ad135b5dSChristopher Siden */
1484ad135b5dSChristopher Siden void
vdev_uberblock_load(vdev_t * rvd,uberblock_t * ub,nvlist_t ** config)1485ad135b5dSChristopher Siden vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
1486ad135b5dSChristopher Siden {
1487ad135b5dSChristopher Siden zio_t *zio;
1488ad135b5dSChristopher Siden spa_t *spa = rvd->vdev_spa;
1489ad135b5dSChristopher Siden struct ubl_cbdata cb;
1490ad135b5dSChristopher Siden int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
1491ad135b5dSChristopher Siden ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
1492ad135b5dSChristopher Siden
1493ad135b5dSChristopher Siden ASSERT(ub);
1494ad135b5dSChristopher Siden ASSERT(config);
1495ad135b5dSChristopher Siden
1496ad135b5dSChristopher Siden bzero(ub, sizeof (uberblock_t));
1497ad135b5dSChristopher Siden *config = NULL;
1498ad135b5dSChristopher Siden
1499ad135b5dSChristopher Siden cb.ubl_ubbest = ub;
1500ad135b5dSChristopher Siden cb.ubl_vd = NULL;
1501ad135b5dSChristopher Siden
1502ad135b5dSChristopher Siden spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1503ad135b5dSChristopher Siden zio = zio_root(spa, NULL, &cb, flags);
1504ad135b5dSChristopher Siden vdev_uberblock_load_impl(zio, rvd, flags, &cb);
1505ad135b5dSChristopher Siden (void) zio_wait(zio);
1506dfbb9432SGeorge Wilson
1507dfbb9432SGeorge Wilson /*
1508dfbb9432SGeorge Wilson * It's possible that the best uberblock was discovered on a label
1509dfbb9432SGeorge Wilson * that has a configuration which was written in a future txg.
1510dfbb9432SGeorge Wilson * Search all labels on this vdev to find the configuration that
1511dfbb9432SGeorge Wilson * matches the txg for our uberblock.
1512dfbb9432SGeorge Wilson */
15133ee8c80cSPavel Zakharov if (cb.ubl_vd != NULL) {
15143ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "best uberblock found for spa %s. "
15153ee8c80cSPavel Zakharov "txg %llu", spa->spa_name, (u_longlong_t)ub->ub_txg);
15163ee8c80cSPavel Zakharov
1517dfbb9432SGeorge Wilson *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
15186f793812SPavel Zakharov if (*config == NULL && spa->spa_extreme_rewind) {
15196f793812SPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config. "
15206f793812SPavel Zakharov "Trying again without txg restrictions.");
15216f793812SPavel Zakharov *config = vdev_label_read_config(cb.ubl_vd, UINT64_MAX);
15226f793812SPavel Zakharov }
15233ee8c80cSPavel Zakharov if (*config == NULL) {
15243ee8c80cSPavel Zakharov vdev_dbgmsg(cb.ubl_vd, "failed to read label config");
15253ee8c80cSPavel Zakharov }
15263ee8c80cSPavel Zakharov }
1527ad135b5dSChristopher Siden spa_config_exit(spa, SCL_ALL, FTAG);
1528fa9e4066Sahrens }
1529fa9e4066Sahrens
1530fa9e4066Sahrens /*
153117f17c2dSbonwick * On success, increment root zio's count of good writes.
15320373e76bSbonwick * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
1533fa9e4066Sahrens */
1534fa9e4066Sahrens static void
vdev_uberblock_sync_done(zio_t * zio)1535fa9e4066Sahrens vdev_uberblock_sync_done(zio_t *zio)
1536fa9e4066Sahrens {
153717f17c2dSbonwick uint64_t *good_writes = zio->io_private;
1538fa9e4066Sahrens
15390373e76bSbonwick if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
15401a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes);
1541fa9e4066Sahrens }
1542fa9e4066Sahrens
154317f17c2dSbonwick /*
154417f17c2dSbonwick * Write the uberblock to all labels of all leaves of the specified vdev.
154517f17c2dSbonwick */
1546fa9e4066Sahrens static void
vdev_uberblock_sync(zio_t * zio,uint64_t * good_writes,uberblock_t * ub,vdev_t * vd,int flags)1547a3b55830SMatthew Ahrens vdev_uberblock_sync(zio_t *zio, uint64_t *good_writes,
1548a3b55830SMatthew Ahrens uberblock_t *ub, vdev_t *vd, int flags)
1549fa9e4066Sahrens {
1550a3b55830SMatthew Ahrens for (uint64_t c = 0; c < vd->vdev_children; c++) {
1551a3b55830SMatthew Ahrens vdev_uberblock_sync(zio, good_writes,
1552a3b55830SMatthew Ahrens ub, vd->vdev_child[c], flags);
1553a3b55830SMatthew Ahrens }
1554fa9e4066Sahrens
1555fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf)
1556fa9e4066Sahrens return;
1557fa9e4066Sahrens
1558e14bb325SJeff Bonwick if (!vdev_writeable(vd))
1559fa9e4066Sahrens return;
1560fa9e4066Sahrens
1561e0f1c0afSOlaf Faaland int m = spa_multihost(vd->vdev_spa) ? MMP_BLOCKS_PER_LABEL : 0;
1562e0f1c0afSOlaf Faaland int n = ub->ub_txg % (VDEV_UBERBLOCK_COUNT(vd) - m);
1563fa9e4066Sahrens
1564770499e1SDan Kimmel /* Copy the uberblock_t into the ABD */
1565770499e1SDan Kimmel abd_t *ub_abd = abd_alloc_for_io(VDEV_UBERBLOCK_SIZE(vd), B_TRUE);
1566770499e1SDan Kimmel abd_zero(ub_abd, VDEV_UBERBLOCK_SIZE(vd));
1567770499e1SDan Kimmel abd_copy_from_buf(ub_abd, ub, sizeof (uberblock_t));
1568fa9e4066Sahrens
1569e14bb325SJeff Bonwick for (int l = 0; l < VDEV_LABELS; l++)
1570770499e1SDan Kimmel vdev_label_write(zio, vd, l, ub_abd,
1571e14bb325SJeff Bonwick VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
1572a3b55830SMatthew Ahrens vdev_uberblock_sync_done, good_writes,
1573e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE);
1574fa9e4066Sahrens
1575770499e1SDan Kimmel abd_free(ub_abd);
1576fa9e4066Sahrens }
1577fa9e4066Sahrens
15783e30c24aSWill Andrews /* Sync the uberblocks to all vdevs in svd[] */
157917f17c2dSbonwick int
vdev_uberblock_sync_list(vdev_t ** svd,int svdcount,uberblock_t * ub,int flags)158017f17c2dSbonwick vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
1581fa9e4066Sahrens {
158217f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa;
1583e14bb325SJeff Bonwick zio_t *zio;
158417f17c2dSbonwick uint64_t good_writes = 0;
1585fa9e4066Sahrens
1586a3b55830SMatthew Ahrens zio = zio_root(spa, NULL, NULL, flags);
1587e14bb325SJeff Bonwick
1588e14bb325SJeff Bonwick for (int v = 0; v < svdcount; v++)
1589a3b55830SMatthew Ahrens vdev_uberblock_sync(zio, &good_writes, ub, svd[v], flags);
1590fa9e4066Sahrens
159117f17c2dSbonwick (void) zio_wait(zio);
1592fa9e4066Sahrens
1593fa9e4066Sahrens /*
159417f17c2dSbonwick * Flush the uberblocks to disk. This ensures that the odd labels
159517f17c2dSbonwick * are no longer needed (because the new uberblocks and the even
159617f17c2dSbonwick * labels are safely on disk), so it is safe to overwrite them.
1597fa9e4066Sahrens */
159817f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags);
1599fa9e4066Sahrens
16005cabbc6bSPrashanth Sreenivasa for (int v = 0; v < svdcount; v++) {
16015cabbc6bSPrashanth Sreenivasa if (vdev_writeable(svd[v])) {
16025cabbc6bSPrashanth Sreenivasa zio_flush(zio, svd[v]);
16035cabbc6bSPrashanth Sreenivasa }
16045cabbc6bSPrashanth Sreenivasa }
1605fa9e4066Sahrens
160617f17c2dSbonwick (void) zio_wait(zio);
160717f17c2dSbonwick
160817f17c2dSbonwick return (good_writes >= 1 ? 0 : EIO);
1609fa9e4066Sahrens }
1610fa9e4066Sahrens
1611fa9e4066Sahrens /*
161217f17c2dSbonwick * On success, increment the count of good writes for our top-level vdev.
1613fa9e4066Sahrens */
1614fa9e4066Sahrens static void
vdev_label_sync_done(zio_t * zio)161517f17c2dSbonwick vdev_label_sync_done(zio_t *zio)
1616fa9e4066Sahrens {
161717f17c2dSbonwick uint64_t *good_writes = zio->io_private;
1618fa9e4066Sahrens
1619fa9e4066Sahrens if (zio->io_error == 0)
16201a5e258fSJosef 'Jeff' Sipek atomic_inc_64(good_writes);
1621fa9e4066Sahrens }
1622fa9e4066Sahrens
162317f17c2dSbonwick /*
162417f17c2dSbonwick * If there weren't enough good writes, indicate failure to the parent.
162517f17c2dSbonwick */
1626fa9e4066Sahrens static void
vdev_label_sync_top_done(zio_t * zio)162717f17c2dSbonwick vdev_label_sync_top_done(zio_t *zio)
162817f17c2dSbonwick {
162917f17c2dSbonwick uint64_t *good_writes = zio->io_private;
163017f17c2dSbonwick
163117f17c2dSbonwick if (*good_writes == 0)
1632be6fd75aSMatthew Ahrens zio->io_error = SET_ERROR(EIO);
163317f17c2dSbonwick
163417f17c2dSbonwick kmem_free(good_writes, sizeof (uint64_t));
163517f17c2dSbonwick }
163617f17c2dSbonwick
163751ece835Seschrock /*
16380430f8daSeschrock * We ignore errors for log and cache devices, simply free the private data.
163951ece835Seschrock */
164051ece835Seschrock static void
vdev_label_sync_ignore_done(zio_t * zio)16410430f8daSeschrock vdev_label_sync_ignore_done(zio_t *zio)
164251ece835Seschrock {
164351ece835Seschrock kmem_free(zio->io_private, sizeof (uint64_t));
164451ece835Seschrock }
164551ece835Seschrock
164617f17c2dSbonwick /*
164717f17c2dSbonwick * Write all even or odd labels to all leaves of the specified vdev.
164817f17c2dSbonwick */
164917f17c2dSbonwick static void
vdev_label_sync(zio_t * zio,uint64_t * good_writes,vdev_t * vd,int l,uint64_t txg,int flags)1650a3b55830SMatthew Ahrens vdev_label_sync(zio_t *zio, uint64_t *good_writes,
1651a3b55830SMatthew Ahrens vdev_t *vd, int l, uint64_t txg, int flags)
1652fa9e4066Sahrens {
1653fa9e4066Sahrens nvlist_t *label;
1654fa9e4066Sahrens vdev_phys_t *vp;
1655770499e1SDan Kimmel abd_t *vp_abd;
1656fa9e4066Sahrens char *buf;
1657fa9e4066Sahrens size_t buflen;
1658fa9e4066Sahrens
1659a3b55830SMatthew Ahrens for (int c = 0; c < vd->vdev_children; c++) {
1660a3b55830SMatthew Ahrens vdev_label_sync(zio, good_writes,
1661a3b55830SMatthew Ahrens vd->vdev_child[c], l, txg, flags);
1662a3b55830SMatthew Ahrens }
1663fa9e4066Sahrens
1664fa9e4066Sahrens if (!vd->vdev_ops->vdev_op_leaf)
1665fa9e4066Sahrens return;
1666fa9e4066Sahrens
1667e14bb325SJeff Bonwick if (!vdev_writeable(vd))
1668fa9e4066Sahrens return;
1669fa9e4066Sahrens
1670fa9e4066Sahrens /*
1671fa9e4066Sahrens * Generate a label describing the top-level config to which we belong.
1672fa9e4066Sahrens */
16730373e76bSbonwick label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
1674fa9e4066Sahrens
1675770499e1SDan Kimmel vp_abd = abd_alloc_linear(sizeof (vdev_phys_t), B_TRUE);
1676770499e1SDan Kimmel abd_zero(vp_abd, sizeof (vdev_phys_t));
1677770499e1SDan Kimmel vp = abd_to_buf(vp_abd);
1678fa9e4066Sahrens
1679fa9e4066Sahrens buf = vp->vp_nvlist;
1680fa9e4066Sahrens buflen = sizeof (vp->vp_nvlist);
1681fa9e4066Sahrens
168217f17c2dSbonwick if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
168317f17c2dSbonwick for (; l < VDEV_LABELS; l += 2) {
1684770499e1SDan Kimmel vdev_label_write(zio, vd, l, vp_abd,
168517f17c2dSbonwick offsetof(vdev_label_t, vl_vdev_phys),
168617f17c2dSbonwick sizeof (vdev_phys_t),
1687a3b55830SMatthew Ahrens vdev_label_sync_done, good_writes,
1688e14bb325SJeff Bonwick flags | ZIO_FLAG_DONT_PROPAGATE);
168917f17c2dSbonwick }
169017f17c2dSbonwick }
1691fa9e4066Sahrens
1692770499e1SDan Kimmel abd_free(vp_abd);
1693fa9e4066Sahrens nvlist_free(label);
1694fa9e4066Sahrens }
1695fa9e4066Sahrens
169617f17c2dSbonwick int
vdev_label_sync_list(spa_t * spa,int l,uint64_t txg,int flags)1697e14bb325SJeff Bonwick vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
1698fa9e4066Sahrens {
1699e14bb325SJeff Bonwick list_t *dl = &spa->spa_config_dirty_list;
170017f17c2dSbonwick vdev_t *vd;
1701e14bb325SJeff Bonwick zio_t *zio;
1702fa9e4066Sahrens int error;
1703fa9e4066Sahrens
1704fa9e4066Sahrens /*
1705e14bb325SJeff Bonwick * Write the new labels to disk.
1706fa9e4066Sahrens */
1707e14bb325SJeff Bonwick zio = zio_root(spa, NULL, NULL, flags);
1708fa9e4066Sahrens
170917f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
171017f17c2dSbonwick uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
171117f17c2dSbonwick KM_SLEEP);
171288ecc943SGeorge Wilson
171388ecc943SGeorge Wilson ASSERT(!vd->vdev_ishole);
171488ecc943SGeorge Wilson
1715a3f829aeSBill Moore zio_t *vio = zio_null(zio, spa, NULL,
17160430f8daSeschrock (vd->vdev_islog || vd->vdev_aux != NULL) ?
17170430f8daSeschrock vdev_label_sync_ignore_done : vdev_label_sync_top_done,
171817f17c2dSbonwick good_writes, flags);
1719a3b55830SMatthew Ahrens vdev_label_sync(vio, good_writes, vd, l, txg, flags);
172017f17c2dSbonwick zio_nowait(vio);
1721fa9e4066Sahrens }
1722e14bb325SJeff Bonwick
1723e14bb325SJeff Bonwick error = zio_wait(zio);
1724fa9e4066Sahrens
17258654d025Sperrin /*
172617f17c2dSbonwick * Flush the new labels to disk.
17278654d025Sperrin */
172817f17c2dSbonwick zio = zio_root(spa, NULL, NULL, flags);
17298654d025Sperrin
173017f17c2dSbonwick for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
173117f17c2dSbonwick zio_flush(zio, vd);
173217f17c2dSbonwick
173317f17c2dSbonwick (void) zio_wait(zio);
1734fa9e4066Sahrens
1735fa9e4066Sahrens return (error);
1736fa9e4066Sahrens }
1737fa9e4066Sahrens
1738fa9e4066Sahrens /*
173917f17c2dSbonwick * Sync the uberblock and any changes to the vdev configuration.
1740fa9e4066Sahrens *
1741fa9e4066Sahrens * The order of operations is carefully crafted to ensure that
1742fa9e4066Sahrens * if the system panics or loses power at any time, the state on disk
1743fa9e4066Sahrens * is still transactionally consistent. The in-line comments below
1744fa9e4066Sahrens * describe the failure semantics at each stage.
1745fa9e4066Sahrens *
174617f17c2dSbonwick * Moreover, vdev_config_sync() is designed to be idempotent: if it fails
1747fa9e4066Sahrens * at any time, you can just call it again, and it will resume its work.
1748fa9e4066Sahrens */
1749e14bb325SJeff Bonwick int
vdev_config_sync(vdev_t ** svd,int svdcount,uint64_t txg)1750eb5bb584SWill Andrews vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg)
1751fa9e4066Sahrens {
175217f17c2dSbonwick spa_t *spa = svd[0]->vdev_spa;
1753fa9e4066Sahrens uberblock_t *ub = &spa->spa_uberblock;
1754eb5bb584SWill Andrews int error = 0;
1755e14bb325SJeff Bonwick int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
1756fa9e4066Sahrens
175786714001SSerapheim Dimitropoulos ASSERT(svdcount != 0);
1758eb5bb584SWill Andrews retry:
17598956713aSEric Schrock /*
17608956713aSEric Schrock * Normally, we don't want to try too hard to write every label and
17618956713aSEric Schrock * uberblock. If there is a flaky disk, we don't want the rest of the
17628956713aSEric Schrock * sync process to block while we retry. But if we can't write a
17638956713aSEric Schrock * single label out, we should retry with ZIO_FLAG_TRYHARD before
17648956713aSEric Schrock * bailing out and declaring the pool faulted.
17658956713aSEric Schrock */
1766eb5bb584SWill Andrews if (error != 0) {
1767eb5bb584SWill Andrews if ((flags & ZIO_FLAG_TRYHARD) != 0)
1768eb5bb584SWill Andrews return (error);
17698956713aSEric Schrock flags |= ZIO_FLAG_TRYHARD;
1770eb5bb584SWill Andrews }
17718956713aSEric Schrock
1772fa9e4066Sahrens ASSERT(ub->ub_txg <= txg);
1773fa9e4066Sahrens
1774fa9e4066Sahrens /*
177517f17c2dSbonwick * If this isn't a resync due to I/O errors,
177617f17c2dSbonwick * and nothing changed in this transaction group,
177717f17c2dSbonwick * and the vdev configuration hasn't changed,
17780373e76bSbonwick * then there's nothing to do.
1779fa9e4066Sahrens */
1780e0f1c0afSOlaf Faaland if (ub->ub_txg < txg) {
1781e0f1c0afSOlaf Faaland boolean_t changed = uberblock_update(ub, spa->spa_root_vdev,
1782e0f1c0afSOlaf Faaland txg, spa->spa_mmp.mmp_delay);
1783e0f1c0afSOlaf Faaland
1784e0f1c0afSOlaf Faaland if (!changed && list_is_empty(&spa->spa_config_dirty_list))
1785e0f1c0afSOlaf Faaland return (0);
1786e0f1c0afSOlaf Faaland }
1787fa9e4066Sahrens
1788fa9e4066Sahrens if (txg > spa_freeze_txg(spa))
1789e14bb325SJeff Bonwick return (0);
1790fa9e4066Sahrens
17910373e76bSbonwick ASSERT(txg <= spa->spa_final_txg);
17920373e76bSbonwick
1793fa9e4066Sahrens /*
1794fa9e4066Sahrens * Flush the write cache of every disk that's been written to
1795fa9e4066Sahrens * in this transaction group. This ensures that all blocks
1796fa9e4066Sahrens * written in this txg will be committed to stable storage
1797fa9e4066Sahrens * before any uberblock that references them.
1798fa9e4066Sahrens */
179986714001SSerapheim Dimitropoulos zio_t *zio = zio_root(spa, NULL, NULL, flags);
180017f17c2dSbonwick
180186714001SSerapheim Dimitropoulos for (vdev_t *vd =
180286714001SSerapheim Dimitropoulos txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd != NULL;
180317f17c2dSbonwick vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
180417f17c2dSbonwick zio_flush(zio, vd);
180517f17c2dSbonwick
1806fa9e4066Sahrens (void) zio_wait(zio);
1807fa9e4066Sahrens
1808fa9e4066Sahrens /*
1809fa9e4066Sahrens * Sync out the even labels (L0, L2) for every dirty vdev. If the
1810fa9e4066Sahrens * system dies in the middle of this process, that's OK: all of the
1811fa9e4066Sahrens * even labels that made it to disk will be newer than any uberblock,
1812fa9e4066Sahrens * and will therefore be considered invalid. The odd labels (L1, L3),
181317f17c2dSbonwick * which have not yet been touched, will still be valid. We flush
181417f17c2dSbonwick * the new labels to disk to ensure that all even-label updates
181517f17c2dSbonwick * are committed to stable storage before the uberblock update.
1816fa9e4066Sahrens */
181786714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0) {
181886714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) {
181986714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d "
182086714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the even labels "
182186714001SSerapheim Dimitropoulos "of dirty vdevs", error, spa_name(spa));
182286714001SSerapheim Dimitropoulos }
1823eb5bb584SWill Andrews goto retry;
182486714001SSerapheim Dimitropoulos }
1825fa9e4066Sahrens
1826fa9e4066Sahrens /*
1827e14bb325SJeff Bonwick * Sync the uberblocks to all vdevs in svd[].
18280373e76bSbonwick * If the system dies in the middle of this step, there are two cases
18290373e76bSbonwick * to consider, and the on-disk state is consistent either way:
1830fa9e4066Sahrens *
1831fa9e4066Sahrens * (1) If none of the new uberblocks made it to disk, then the
1832fa9e4066Sahrens * previous uberblock will be the newest, and the odd labels
1833fa9e4066Sahrens * (which had not yet been touched) will be valid with respect
1834fa9e4066Sahrens * to that uberblock.
1835fa9e4066Sahrens *
1836fa9e4066Sahrens * (2) If one or more new uberblocks made it to disk, then they
1837fa9e4066Sahrens * will be the newest, and the even labels (which had all
1838fa9e4066Sahrens * been successfully committed) will be valid with respect
1839fa9e4066Sahrens * to the new uberblocks.
1840fa9e4066Sahrens */
184186714001SSerapheim Dimitropoulos if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0) {
184286714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) {
184386714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_uberblock_sync_list() returned error "
184486714001SSerapheim Dimitropoulos "%d for pool '%s'", error, spa_name(spa));
184586714001SSerapheim Dimitropoulos }
1846eb5bb584SWill Andrews goto retry;
184786714001SSerapheim Dimitropoulos }
1848fa9e4066Sahrens
1849e0f1c0afSOlaf Faaland if (spa_multihost(spa))
1850e0f1c0afSOlaf Faaland mmp_update_uberblock(spa, ub);
1851e0f1c0afSOlaf Faaland
1852fa9e4066Sahrens /*
1853fa9e4066Sahrens * Sync out odd labels for every dirty vdev. If the system dies
1854fa9e4066Sahrens * in the middle of this process, the even labels and the new
1855fa9e4066Sahrens * uberblocks will suffice to open the pool. The next time
1856fa9e4066Sahrens * the pool is opened, the first thing we'll do -- before any
1857fa9e4066Sahrens * user data is modified -- is mark every vdev dirty so that
185817f17c2dSbonwick * all labels will be brought up to date. We flush the new labels
185917f17c2dSbonwick * to disk to ensure that all odd-label updates are committed to
186017f17c2dSbonwick * stable storage before the next transaction group begins.
1861fa9e4066Sahrens */
186286714001SSerapheim Dimitropoulos if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0) {
186386714001SSerapheim Dimitropoulos if ((flags & ZIO_FLAG_TRYHARD) != 0) {
186486714001SSerapheim Dimitropoulos zfs_dbgmsg("vdev_label_sync_list() returned error %d "
186586714001SSerapheim Dimitropoulos "for pool '%s' when syncing out the odd labels of "
186686714001SSerapheim Dimitropoulos "dirty vdevs", error, spa_name(spa));
186786714001SSerapheim Dimitropoulos }
1868eb5bb584SWill Andrews goto retry;
186986714001SSerapheim Dimitropoulos }
1870eb5bb584SWill Andrews
1871eb5bb584SWill Andrews return (0);
1872fa9e4066Sahrens }
1873