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