Print this page
10592 misc. metaslab and vdev related ZoL bug fixes
Portions contributed by: Jerry Jelinek <jerry.jelinek@joyent.com>
Reviewed by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed by: Giuseppe Di Natale <guss80@gmail.com>
Reviewed by: George Melikov <mail@gmelikov.ru>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: Pavel Zakharov <pavel.zakharov@delphix.com>
Reviewed by: Tony Hutter <hutter2@llnl.gov>
Reviewed by: Kody Kantor <kody.kantor@joyent.com>
Approved by: Dan McDonald <danmcd@joyent.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/vdev.c
+++ new/usr/src/uts/common/fs/zfs/vdev.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
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 24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 25 * Copyright 2017 Nexenta Systems, Inc.
26 26 * Copyright (c) 2014 Integros [integros.com]
27 27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 28 * Copyright 2017 Joyent, Inc.
29 29 * Copyright (c) 2017, Intel Corporation.
30 30 */
31 31
32 32 #include <sys/zfs_context.h>
33 33 #include <sys/fm/fs/zfs.h>
34 34 #include <sys/spa.h>
35 35 #include <sys/spa_impl.h>
36 36 #include <sys/bpobj.h>
37 37 #include <sys/dmu.h>
38 38 #include <sys/dmu_tx.h>
39 39 #include <sys/dsl_dir.h>
40 40 #include <sys/vdev_impl.h>
41 41 #include <sys/uberblock_impl.h>
42 42 #include <sys/metaslab.h>
43 43 #include <sys/metaslab_impl.h>
44 44 #include <sys/space_map.h>
45 45 #include <sys/space_reftree.h>
46 46 #include <sys/zio.h>
47 47 #include <sys/zap.h>
48 48 #include <sys/fs/zfs.h>
49 49 #include <sys/arc.h>
50 50 #include <sys/zil.h>
51 51 #include <sys/dsl_scan.h>
52 52 #include <sys/abd.h>
53 53 #include <sys/vdev_initialize.h>
54 54
55 55 /*
56 56 * Virtual device management.
57 57 */
58 58
59 59 static vdev_ops_t *vdev_ops_table[] = {
60 60 &vdev_root_ops,
61 61 &vdev_raidz_ops,
62 62 &vdev_mirror_ops,
63 63 &vdev_replacing_ops,
64 64 &vdev_spare_ops,
65 65 &vdev_disk_ops,
66 66 &vdev_file_ops,
67 67 &vdev_missing_ops,
68 68 &vdev_hole_ops,
69 69 &vdev_indirect_ops,
70 70 NULL
71 71 };
72 72
73 73 /* maximum scrub/resilver I/O queue per leaf vdev */
74 74 int zfs_scrub_limit = 10;
75 75
76 76 /* default target for number of metaslabs per top-level vdev */
77 77 int zfs_vdev_default_ms_count = 200;
78 78
79 79 /* minimum number of metaslabs per top-level vdev */
80 80 int zfs_vdev_min_ms_count = 16;
81 81
82 82 /* practical upper limit of total metaslabs per top-level vdev */
83 83 int zfs_vdev_ms_count_limit = 1ULL << 17;
84 84
85 85 /* lower limit for metaslab size (512M) */
86 86 int zfs_vdev_default_ms_shift = 29;
87 87
88 88 /* upper limit for metaslab size (16G) */
89 89 int zfs_vdev_max_ms_shift = 34;
90 90
91 91 boolean_t vdev_validate_skip = B_FALSE;
92 92
93 93 /*
94 94 * Since the DTL space map of a vdev is not expected to have a lot of
95 95 * entries, we default its block size to 4K.
96 96 */
97 97 int vdev_dtl_sm_blksz = (1 << 12);
98 98
99 99 /*
100 100 * vdev-wide space maps that have lots of entries written to them at
101 101 * the end of each transaction can benefit from a higher I/O bandwidth
102 102 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
103 103 */
104 104 int vdev_standard_sm_blksz = (1 << 17);
105 105
106 106 int zfs_ashift_min;
107 107
108 108 /*PRINTFLIKE2*/
109 109 void
110 110 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
111 111 {
112 112 va_list adx;
113 113 char buf[256];
114 114
115 115 va_start(adx, fmt);
116 116 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
117 117 va_end(adx);
118 118
119 119 if (vd->vdev_path != NULL) {
120 120 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
121 121 vd->vdev_path, buf);
122 122 } else {
123 123 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
124 124 vd->vdev_ops->vdev_op_type,
125 125 (u_longlong_t)vd->vdev_id,
126 126 (u_longlong_t)vd->vdev_guid, buf);
127 127 }
128 128 }
129 129
130 130 void
131 131 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
132 132 {
133 133 char state[20];
134 134
135 135 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
136 136 zfs_dbgmsg("%*svdev %u: %s", indent, "", vd->vdev_id,
137 137 vd->vdev_ops->vdev_op_type);
138 138 return;
139 139 }
140 140
141 141 switch (vd->vdev_state) {
142 142 case VDEV_STATE_UNKNOWN:
143 143 (void) snprintf(state, sizeof (state), "unknown");
144 144 break;
145 145 case VDEV_STATE_CLOSED:
146 146 (void) snprintf(state, sizeof (state), "closed");
147 147 break;
148 148 case VDEV_STATE_OFFLINE:
149 149 (void) snprintf(state, sizeof (state), "offline");
150 150 break;
151 151 case VDEV_STATE_REMOVED:
152 152 (void) snprintf(state, sizeof (state), "removed");
153 153 break;
154 154 case VDEV_STATE_CANT_OPEN:
155 155 (void) snprintf(state, sizeof (state), "can't open");
156 156 break;
157 157 case VDEV_STATE_FAULTED:
158 158 (void) snprintf(state, sizeof (state), "faulted");
159 159 break;
160 160 case VDEV_STATE_DEGRADED:
161 161 (void) snprintf(state, sizeof (state), "degraded");
162 162 break;
163 163 case VDEV_STATE_HEALTHY:
164 164 (void) snprintf(state, sizeof (state), "healthy");
165 165 break;
166 166 default:
167 167 (void) snprintf(state, sizeof (state), "<state %u>",
168 168 (uint_t)vd->vdev_state);
169 169 }
170 170
171 171 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
172 172 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
173 173 vd->vdev_islog ? " (log)" : "",
174 174 (u_longlong_t)vd->vdev_guid,
175 175 vd->vdev_path ? vd->vdev_path : "N/A", state);
176 176
177 177 for (uint64_t i = 0; i < vd->vdev_children; i++)
178 178 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
179 179 }
180 180
181 181 /*
182 182 * Given a vdev type, return the appropriate ops vector.
183 183 */
184 184 static vdev_ops_t *
185 185 vdev_getops(const char *type)
186 186 {
187 187 vdev_ops_t *ops, **opspp;
188 188
189 189 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
190 190 if (strcmp(ops->vdev_op_type, type) == 0)
191 191 break;
192 192
193 193 return (ops);
194 194 }
195 195
196 196 /*
197 197 * Derive the enumerated alloction bias from string input.
198 198 * String origin is either the per-vdev zap or zpool(1M).
199 199 */
200 200 static vdev_alloc_bias_t
201 201 vdev_derive_alloc_bias(const char *bias)
202 202 {
203 203 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
204 204
205 205 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
206 206 alloc_bias = VDEV_BIAS_LOG;
207 207 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
208 208 alloc_bias = VDEV_BIAS_SPECIAL;
209 209 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
210 210 alloc_bias = VDEV_BIAS_DEDUP;
211 211
212 212 return (alloc_bias);
213 213 }
214 214
215 215 /* ARGSUSED */
216 216 void
217 217 vdev_default_xlate(vdev_t *vd, const range_seg_t *in, range_seg_t *res)
218 218 {
219 219 res->rs_start = in->rs_start;
220 220 res->rs_end = in->rs_end;
221 221 }
222 222
223 223 /*
224 224 * Default asize function: return the MAX of psize with the asize of
225 225 * all children. This is what's used by anything other than RAID-Z.
226 226 */
227 227 uint64_t
228 228 vdev_default_asize(vdev_t *vd, uint64_t psize)
229 229 {
230 230 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
231 231 uint64_t csize;
232 232
233 233 for (int c = 0; c < vd->vdev_children; c++) {
234 234 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
235 235 asize = MAX(asize, csize);
236 236 }
237 237
238 238 return (asize);
239 239 }
240 240
241 241 /*
242 242 * Get the minimum allocatable size. We define the allocatable size as
243 243 * the vdev's asize rounded to the nearest metaslab. This allows us to
244 244 * replace or attach devices which don't have the same physical size but
245 245 * can still satisfy the same number of allocations.
246 246 */
247 247 uint64_t
248 248 vdev_get_min_asize(vdev_t *vd)
249 249 {
250 250 vdev_t *pvd = vd->vdev_parent;
251 251
252 252 /*
253 253 * If our parent is NULL (inactive spare or cache) or is the root,
254 254 * just return our own asize.
255 255 */
256 256 if (pvd == NULL)
257 257 return (vd->vdev_asize);
258 258
259 259 /*
260 260 * The top-level vdev just returns the allocatable size rounded
261 261 * to the nearest metaslab.
262 262 */
263 263 if (vd == vd->vdev_top)
264 264 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
265 265
266 266 /*
267 267 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
268 268 * so each child must provide at least 1/Nth of its asize.
269 269 */
270 270 if (pvd->vdev_ops == &vdev_raidz_ops)
271 271 return ((pvd->vdev_min_asize + pvd->vdev_children - 1) /
272 272 pvd->vdev_children);
273 273
274 274 return (pvd->vdev_min_asize);
275 275 }
276 276
277 277 void
278 278 vdev_set_min_asize(vdev_t *vd)
279 279 {
280 280 vd->vdev_min_asize = vdev_get_min_asize(vd);
281 281
282 282 for (int c = 0; c < vd->vdev_children; c++)
283 283 vdev_set_min_asize(vd->vdev_child[c]);
284 284 }
285 285
286 286 vdev_t *
287 287 vdev_lookup_top(spa_t *spa, uint64_t vdev)
288 288 {
289 289 vdev_t *rvd = spa->spa_root_vdev;
290 290
291 291 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
292 292
293 293 if (vdev < rvd->vdev_children) {
294 294 ASSERT(rvd->vdev_child[vdev] != NULL);
295 295 return (rvd->vdev_child[vdev]);
296 296 }
297 297
298 298 return (NULL);
299 299 }
300 300
301 301 vdev_t *
302 302 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
303 303 {
304 304 vdev_t *mvd;
305 305
306 306 if (vd->vdev_guid == guid)
307 307 return (vd);
308 308
309 309 for (int c = 0; c < vd->vdev_children; c++)
310 310 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
311 311 NULL)
312 312 return (mvd);
313 313
314 314 return (NULL);
315 315 }
316 316
317 317 static int
318 318 vdev_count_leaves_impl(vdev_t *vd)
319 319 {
320 320 int n = 0;
321 321
322 322 if (vd->vdev_ops->vdev_op_leaf)
323 323 return (1);
324 324
325 325 for (int c = 0; c < vd->vdev_children; c++)
326 326 n += vdev_count_leaves_impl(vd->vdev_child[c]);
327 327
328 328 return (n);
329 329 }
330 330
331 331 int
332 332 vdev_count_leaves(spa_t *spa)
333 333 {
334 334 return (vdev_count_leaves_impl(spa->spa_root_vdev));
335 335 }
336 336
337 337 void
338 338 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
339 339 {
340 340 size_t oldsize, newsize;
341 341 uint64_t id = cvd->vdev_id;
342 342 vdev_t **newchild;
343 343 spa_t *spa = cvd->vdev_spa;
344 344
345 345 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
346 346 ASSERT(cvd->vdev_parent == NULL);
347 347
348 348 cvd->vdev_parent = pvd;
349 349
350 350 if (pvd == NULL)
351 351 return;
352 352
353 353 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
354 354
355 355 oldsize = pvd->vdev_children * sizeof (vdev_t *);
356 356 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
357 357 newsize = pvd->vdev_children * sizeof (vdev_t *);
358 358
359 359 newchild = kmem_zalloc(newsize, KM_SLEEP);
360 360 if (pvd->vdev_child != NULL) {
361 361 bcopy(pvd->vdev_child, newchild, oldsize);
362 362 kmem_free(pvd->vdev_child, oldsize);
363 363 }
364 364
365 365 pvd->vdev_child = newchild;
366 366 pvd->vdev_child[id] = cvd;
367 367
368 368 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
369 369 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
370 370
371 371 /*
372 372 * Walk up all ancestors to update guid sum.
373 373 */
374 374 for (; pvd != NULL; pvd = pvd->vdev_parent)
375 375 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
376 376
377 377 if (cvd->vdev_ops->vdev_op_leaf) {
378 378 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
379 379 cvd->vdev_spa->spa_leaf_list_gen++;
380 380 }
381 381 }
382 382
383 383 void
384 384 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
385 385 {
386 386 int c;
387 387 uint_t id = cvd->vdev_id;
388 388
389 389 ASSERT(cvd->vdev_parent == pvd);
390 390
391 391 if (pvd == NULL)
392 392 return;
393 393
394 394 ASSERT(id < pvd->vdev_children);
395 395 ASSERT(pvd->vdev_child[id] == cvd);
396 396
397 397 pvd->vdev_child[id] = NULL;
398 398 cvd->vdev_parent = NULL;
399 399
400 400 for (c = 0; c < pvd->vdev_children; c++)
401 401 if (pvd->vdev_child[c])
402 402 break;
403 403
404 404 if (c == pvd->vdev_children) {
405 405 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
406 406 pvd->vdev_child = NULL;
407 407 pvd->vdev_children = 0;
408 408 }
409 409
410 410 if (cvd->vdev_ops->vdev_op_leaf) {
411 411 spa_t *spa = cvd->vdev_spa;
412 412 list_remove(&spa->spa_leaf_list, cvd);
413 413 spa->spa_leaf_list_gen++;
414 414 }
415 415
416 416 /*
417 417 * Walk up all ancestors to update guid sum.
418 418 */
419 419 for (; pvd != NULL; pvd = pvd->vdev_parent)
420 420 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
421 421 }
422 422
423 423 /*
424 424 * Remove any holes in the child array.
425 425 */
426 426 void
427 427 vdev_compact_children(vdev_t *pvd)
428 428 {
429 429 vdev_t **newchild, *cvd;
430 430 int oldc = pvd->vdev_children;
431 431 int newc;
432 432
433 433 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
434 434
435 435 for (int c = newc = 0; c < oldc; c++)
436 436 if (pvd->vdev_child[c])
437 437 newc++;
438 438
439 439 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
440 440
441 441 for (int c = newc = 0; c < oldc; c++) {
442 442 if ((cvd = pvd->vdev_child[c]) != NULL) {
443 443 newchild[newc] = cvd;
444 444 cvd->vdev_id = newc++;
445 445 }
446 446 }
447 447
448 448 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
449 449 pvd->vdev_child = newchild;
450 450 pvd->vdev_children = newc;
451 451 }
452 452
453 453 /*
454 454 * Allocate and minimally initialize a vdev_t.
455 455 */
456 456 vdev_t *
457 457 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
458 458 {
459 459 vdev_t *vd;
460 460 vdev_indirect_config_t *vic;
461 461
462 462 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
463 463 vic = &vd->vdev_indirect_config;
464 464
465 465 if (spa->spa_root_vdev == NULL) {
466 466 ASSERT(ops == &vdev_root_ops);
467 467 spa->spa_root_vdev = vd;
468 468 spa->spa_load_guid = spa_generate_guid(NULL);
469 469 }
470 470
471 471 if (guid == 0 && ops != &vdev_hole_ops) {
472 472 if (spa->spa_root_vdev == vd) {
473 473 /*
474 474 * The root vdev's guid will also be the pool guid,
475 475 * which must be unique among all pools.
476 476 */
477 477 guid = spa_generate_guid(NULL);
478 478 } else {
479 479 /*
480 480 * Any other vdev's guid must be unique within the pool.
481 481 */
482 482 guid = spa_generate_guid(spa);
483 483 }
484 484 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
485 485 }
486 486
487 487 vd->vdev_spa = spa;
488 488 vd->vdev_id = id;
489 489 vd->vdev_guid = guid;
490 490 vd->vdev_guid_sum = guid;
491 491 vd->vdev_ops = ops;
492 492 vd->vdev_state = VDEV_STATE_CLOSED;
493 493 vd->vdev_ishole = (ops == &vdev_hole_ops);
|
↓ open down ↓ |
493 lines elided |
↑ open up ↑ |
494 494 vic->vic_prev_indirect_vdev = UINT64_MAX;
495 495
496 496 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
497 497 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
498 498 vd->vdev_obsolete_segments = range_tree_create(NULL, NULL);
499 499
500 500 list_link_init(&vd->vdev_leaf_node);
501 501 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
502 502 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
503 503 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
504 - mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
505 504 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
506 505 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
507 506 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
508 507 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
509 508
510 509 for (int t = 0; t < DTL_TYPES; t++) {
511 510 vd->vdev_dtl[t] = range_tree_create(NULL, NULL);
512 511 }
513 512 txg_list_create(&vd->vdev_ms_list, spa,
514 513 offsetof(struct metaslab, ms_txg_node));
515 514 txg_list_create(&vd->vdev_dtl_list, spa,
516 515 offsetof(struct vdev, vdev_dtl_node));
517 516 vd->vdev_stat.vs_timestamp = gethrtime();
518 517 vdev_queue_init(vd);
519 518 vdev_cache_init(vd);
520 519
521 520 return (vd);
522 521 }
523 522
524 523 /*
525 524 * Allocate a new vdev. The 'alloctype' is used to control whether we are
526 525 * creating a new vdev or loading an existing one - the behavior is slightly
527 526 * different for each case.
528 527 */
529 528 int
530 529 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
531 530 int alloctype)
532 531 {
533 532 vdev_ops_t *ops;
534 533 char *type;
535 534 uint64_t guid = 0, islog, nparity;
536 535 vdev_t *vd;
537 536 vdev_indirect_config_t *vic;
538 537 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
539 538 boolean_t top_level = (parent && !parent->vdev_parent);
540 539
541 540 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
542 541
543 542 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
544 543 return (SET_ERROR(EINVAL));
545 544
546 545 if ((ops = vdev_getops(type)) == NULL)
547 546 return (SET_ERROR(EINVAL));
548 547
549 548 /*
550 549 * If this is a load, get the vdev guid from the nvlist.
551 550 * Otherwise, vdev_alloc_common() will generate one for us.
552 551 */
553 552 if (alloctype == VDEV_ALLOC_LOAD) {
554 553 uint64_t label_id;
555 554
556 555 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
557 556 label_id != id)
558 557 return (SET_ERROR(EINVAL));
559 558
560 559 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
561 560 return (SET_ERROR(EINVAL));
562 561 } else if (alloctype == VDEV_ALLOC_SPARE) {
563 562 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
564 563 return (SET_ERROR(EINVAL));
565 564 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
566 565 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
567 566 return (SET_ERROR(EINVAL));
568 567 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
569 568 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
570 569 return (SET_ERROR(EINVAL));
571 570 }
572 571
573 572 /*
574 573 * The first allocated vdev must be of type 'root'.
575 574 */
576 575 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
577 576 return (SET_ERROR(EINVAL));
578 577
579 578 /*
580 579 * Determine whether we're a log vdev.
581 580 */
582 581 islog = 0;
583 582 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
584 583 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
585 584 return (SET_ERROR(ENOTSUP));
586 585
587 586 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
588 587 return (SET_ERROR(ENOTSUP));
589 588
590 589 /*
591 590 * Set the nparity property for RAID-Z vdevs.
592 591 */
593 592 nparity = -1ULL;
594 593 if (ops == &vdev_raidz_ops) {
595 594 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
596 595 &nparity) == 0) {
597 596 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
598 597 return (SET_ERROR(EINVAL));
599 598 /*
600 599 * Previous versions could only support 1 or 2 parity
601 600 * device.
602 601 */
603 602 if (nparity > 1 &&
604 603 spa_version(spa) < SPA_VERSION_RAIDZ2)
605 604 return (SET_ERROR(ENOTSUP));
606 605 if (nparity > 2 &&
607 606 spa_version(spa) < SPA_VERSION_RAIDZ3)
608 607 return (SET_ERROR(ENOTSUP));
609 608 } else {
610 609 /*
611 610 * We require the parity to be specified for SPAs that
612 611 * support multiple parity levels.
613 612 */
614 613 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
615 614 return (SET_ERROR(EINVAL));
616 615 /*
617 616 * Otherwise, we default to 1 parity device for RAID-Z.
618 617 */
619 618 nparity = 1;
620 619 }
621 620 } else {
622 621 nparity = 0;
623 622 }
624 623 ASSERT(nparity != -1ULL);
625 624
626 625 /*
627 626 * If creating a top-level vdev, check for allocation classes input
628 627 */
629 628 if (top_level && alloctype == VDEV_ALLOC_ADD) {
630 629 char *bias;
631 630
632 631 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
633 632 &bias) == 0) {
634 633 alloc_bias = vdev_derive_alloc_bias(bias);
635 634
636 635 /* spa_vdev_add() expects feature to be enabled */
637 636 if (spa->spa_load_state != SPA_LOAD_CREATE &&
638 637 !spa_feature_is_enabled(spa,
639 638 SPA_FEATURE_ALLOCATION_CLASSES)) {
640 639 return (SET_ERROR(ENOTSUP));
641 640 }
642 641 }
643 642 }
644 643
645 644 vd = vdev_alloc_common(spa, id, guid, ops);
646 645 vic = &vd->vdev_indirect_config;
647 646
648 647 vd->vdev_islog = islog;
649 648 vd->vdev_nparity = nparity;
650 649 if (top_level && alloc_bias != VDEV_BIAS_NONE)
651 650 vd->vdev_alloc_bias = alloc_bias;
652 651
653 652 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
654 653 vd->vdev_path = spa_strdup(vd->vdev_path);
655 654 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
656 655 vd->vdev_devid = spa_strdup(vd->vdev_devid);
657 656 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
658 657 &vd->vdev_physpath) == 0)
659 658 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
660 659 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
661 660 vd->vdev_fru = spa_strdup(vd->vdev_fru);
662 661
663 662 /*
664 663 * Set the whole_disk property. If it's not specified, leave the value
665 664 * as -1.
666 665 */
667 666 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
668 667 &vd->vdev_wholedisk) != 0)
669 668 vd->vdev_wholedisk = -1ULL;
670 669
671 670 ASSERT0(vic->vic_mapping_object);
672 671 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
673 672 &vic->vic_mapping_object);
674 673 ASSERT0(vic->vic_births_object);
675 674 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
676 675 &vic->vic_births_object);
677 676 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
678 677 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
679 678 &vic->vic_prev_indirect_vdev);
680 679
681 680 /*
682 681 * Look for the 'not present' flag. This will only be set if the device
683 682 * was not present at the time of import.
684 683 */
685 684 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
686 685 &vd->vdev_not_present);
687 686
688 687 /*
689 688 * Get the alignment requirement.
690 689 */
691 690 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
692 691
693 692 /*
694 693 * Retrieve the vdev creation time.
695 694 */
696 695 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
697 696 &vd->vdev_crtxg);
698 697
699 698 /*
700 699 * If we're a top-level vdev, try to load the allocation parameters.
701 700 */
702 701 if (top_level &&
703 702 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
704 703 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
705 704 &vd->vdev_ms_array);
706 705 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
707 706 &vd->vdev_ms_shift);
708 707 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
709 708 &vd->vdev_asize);
710 709 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
711 710 &vd->vdev_removing);
712 711 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
713 712 &vd->vdev_top_zap);
714 713 } else {
715 714 ASSERT0(vd->vdev_top_zap);
716 715 }
717 716
718 717 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
719 718 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
720 719 alloctype == VDEV_ALLOC_ADD ||
721 720 alloctype == VDEV_ALLOC_SPLIT ||
722 721 alloctype == VDEV_ALLOC_ROOTPOOL);
723 722 /* Note: metaslab_group_create() is now deferred */
724 723 }
725 724
726 725 if (vd->vdev_ops->vdev_op_leaf &&
727 726 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
728 727 (void) nvlist_lookup_uint64(nv,
729 728 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
730 729 } else {
731 730 ASSERT0(vd->vdev_leaf_zap);
732 731 }
733 732
734 733 /*
735 734 * If we're a leaf vdev, try to load the DTL object and other state.
736 735 */
737 736
738 737 if (vd->vdev_ops->vdev_op_leaf &&
739 738 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
740 739 alloctype == VDEV_ALLOC_ROOTPOOL)) {
741 740 if (alloctype == VDEV_ALLOC_LOAD) {
742 741 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
743 742 &vd->vdev_dtl_object);
744 743 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
745 744 &vd->vdev_unspare);
746 745 }
747 746
748 747 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
749 748 uint64_t spare = 0;
750 749
751 750 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
752 751 &spare) == 0 && spare)
753 752 spa_spare_add(vd);
754 753 }
755 754
756 755 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
757 756 &vd->vdev_offline);
758 757
759 758 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
760 759 &vd->vdev_resilver_txg);
761 760
762 761 /*
763 762 * When importing a pool, we want to ignore the persistent fault
764 763 * state, as the diagnosis made on another system may not be
765 764 * valid in the current context. Local vdevs will
766 765 * remain in the faulted state.
767 766 */
768 767 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
769 768 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
770 769 &vd->vdev_faulted);
771 770 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
772 771 &vd->vdev_degraded);
773 772 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
774 773 &vd->vdev_removed);
775 774
776 775 if (vd->vdev_faulted || vd->vdev_degraded) {
777 776 char *aux;
778 777
779 778 vd->vdev_label_aux =
780 779 VDEV_AUX_ERR_EXCEEDED;
781 780 if (nvlist_lookup_string(nv,
782 781 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
783 782 strcmp(aux, "external") == 0)
784 783 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
785 784 }
786 785 }
787 786 }
788 787
789 788 /*
790 789 * Add ourselves to the parent's list of children.
791 790 */
792 791 vdev_add_child(parent, vd);
793 792
794 793 *vdp = vd;
795 794
796 795 return (0);
797 796 }
798 797
799 798 void
800 799 vdev_free(vdev_t *vd)
801 800 {
802 801 spa_t *spa = vd->vdev_spa;
803 802 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
804 803
805 804 /*
806 805 * vdev_free() implies closing the vdev first. This is simpler than
807 806 * trying to ensure complicated semantics for all callers.
808 807 */
809 808 vdev_close(vd);
810 809
811 810 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
812 811 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
813 812
814 813 /*
815 814 * Free all children.
816 815 */
817 816 for (int c = 0; c < vd->vdev_children; c++)
818 817 vdev_free(vd->vdev_child[c]);
819 818
820 819 ASSERT(vd->vdev_child == NULL);
821 820 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
822 821 ASSERT(vd->vdev_initialize_thread == NULL);
823 822
824 823 /*
825 824 * Discard allocation state.
826 825 */
827 826 if (vd->vdev_mg != NULL) {
828 827 vdev_metaslab_fini(vd);
829 828 metaslab_group_destroy(vd->vdev_mg);
830 829 }
831 830
832 831 ASSERT0(vd->vdev_stat.vs_space);
833 832 ASSERT0(vd->vdev_stat.vs_dspace);
834 833 ASSERT0(vd->vdev_stat.vs_alloc);
835 834
836 835 /*
837 836 * Remove this vdev from its parent's child list.
838 837 */
839 838 vdev_remove_child(vd->vdev_parent, vd);
840 839
841 840 ASSERT(vd->vdev_parent == NULL);
842 841 ASSERT(!list_link_active(&vd->vdev_leaf_node));
843 842
844 843 /*
845 844 * Clean up vdev structure.
846 845 */
847 846 vdev_queue_fini(vd);
848 847 vdev_cache_fini(vd);
849 848
850 849 if (vd->vdev_path)
851 850 spa_strfree(vd->vdev_path);
852 851 if (vd->vdev_devid)
853 852 spa_strfree(vd->vdev_devid);
854 853 if (vd->vdev_physpath)
855 854 spa_strfree(vd->vdev_physpath);
856 855 if (vd->vdev_fru)
857 856 spa_strfree(vd->vdev_fru);
858 857
859 858 if (vd->vdev_isspare)
860 859 spa_spare_remove(vd);
861 860 if (vd->vdev_isl2cache)
862 861 spa_l2cache_remove(vd);
863 862
864 863 txg_list_destroy(&vd->vdev_ms_list);
865 864 txg_list_destroy(&vd->vdev_dtl_list);
866 865
867 866 mutex_enter(&vd->vdev_dtl_lock);
868 867 space_map_close(vd->vdev_dtl_sm);
869 868 for (int t = 0; t < DTL_TYPES; t++) {
870 869 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
871 870 range_tree_destroy(vd->vdev_dtl[t]);
872 871 }
873 872 mutex_exit(&vd->vdev_dtl_lock);
874 873
875 874 EQUIV(vd->vdev_indirect_births != NULL,
876 875 vd->vdev_indirect_mapping != NULL);
877 876 if (vd->vdev_indirect_births != NULL) {
878 877 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
879 878 vdev_indirect_births_close(vd->vdev_indirect_births);
880 879 }
881 880
|
↓ open down ↓ |
367 lines elided |
↑ open up ↑ |
882 881 if (vd->vdev_obsolete_sm != NULL) {
883 882 ASSERT(vd->vdev_removing ||
884 883 vd->vdev_ops == &vdev_indirect_ops);
885 884 space_map_close(vd->vdev_obsolete_sm);
886 885 vd->vdev_obsolete_sm = NULL;
887 886 }
888 887 range_tree_destroy(vd->vdev_obsolete_segments);
889 888 rw_destroy(&vd->vdev_indirect_rwlock);
890 889 mutex_destroy(&vd->vdev_obsolete_lock);
891 890
892 - mutex_destroy(&vd->vdev_queue_lock);
893 891 mutex_destroy(&vd->vdev_dtl_lock);
894 892 mutex_destroy(&vd->vdev_stat_lock);
895 893 mutex_destroy(&vd->vdev_probe_lock);
896 894 mutex_destroy(&vd->vdev_initialize_lock);
897 895 mutex_destroy(&vd->vdev_initialize_io_lock);
898 896 cv_destroy(&vd->vdev_initialize_io_cv);
899 897 cv_destroy(&vd->vdev_initialize_cv);
900 898
901 899 if (vd == spa->spa_root_vdev)
902 900 spa->spa_root_vdev = NULL;
903 901
904 902 kmem_free(vd, sizeof (vdev_t));
905 903 }
906 904
907 905 /*
908 906 * Transfer top-level vdev state from svd to tvd.
909 907 */
910 908 static void
911 909 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
912 910 {
913 911 spa_t *spa = svd->vdev_spa;
914 912 metaslab_t *msp;
915 913 vdev_t *vd;
916 914 int t;
917 915
918 916 ASSERT(tvd == tvd->vdev_top);
919 917
920 918 tvd->vdev_ms_array = svd->vdev_ms_array;
921 919 tvd->vdev_ms_shift = svd->vdev_ms_shift;
922 920 tvd->vdev_ms_count = svd->vdev_ms_count;
923 921 tvd->vdev_top_zap = svd->vdev_top_zap;
924 922
925 923 svd->vdev_ms_array = 0;
926 924 svd->vdev_ms_shift = 0;
927 925 svd->vdev_ms_count = 0;
928 926 svd->vdev_top_zap = 0;
929 927
930 928 if (tvd->vdev_mg)
931 929 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
932 930 tvd->vdev_mg = svd->vdev_mg;
933 931 tvd->vdev_ms = svd->vdev_ms;
934 932
935 933 svd->vdev_mg = NULL;
936 934 svd->vdev_ms = NULL;
937 935
938 936 if (tvd->vdev_mg != NULL)
939 937 tvd->vdev_mg->mg_vd = tvd;
940 938
941 939 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
942 940 svd->vdev_checkpoint_sm = NULL;
943 941
944 942 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
945 943 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
946 944
947 945 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
948 946 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
949 947 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
950 948
951 949 svd->vdev_stat.vs_alloc = 0;
952 950 svd->vdev_stat.vs_space = 0;
953 951 svd->vdev_stat.vs_dspace = 0;
954 952
955 953 /*
956 954 * State which may be set on a top-level vdev that's in the
957 955 * process of being removed.
958 956 */
959 957 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
960 958 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
961 959 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
962 960 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
963 961 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
964 962 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
965 963 ASSERT0(tvd->vdev_removing);
966 964 tvd->vdev_removing = svd->vdev_removing;
967 965 tvd->vdev_indirect_config = svd->vdev_indirect_config;
968 966 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
969 967 tvd->vdev_indirect_births = svd->vdev_indirect_births;
970 968 range_tree_swap(&svd->vdev_obsolete_segments,
971 969 &tvd->vdev_obsolete_segments);
972 970 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
973 971 svd->vdev_indirect_config.vic_mapping_object = 0;
974 972 svd->vdev_indirect_config.vic_births_object = 0;
975 973 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
976 974 svd->vdev_indirect_mapping = NULL;
977 975 svd->vdev_indirect_births = NULL;
978 976 svd->vdev_obsolete_sm = NULL;
979 977 svd->vdev_removing = 0;
980 978
981 979 for (t = 0; t < TXG_SIZE; t++) {
982 980 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
983 981 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
984 982 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
985 983 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
986 984 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
987 985 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
988 986 }
989 987
990 988 if (list_link_active(&svd->vdev_config_dirty_node)) {
991 989 vdev_config_clean(svd);
992 990 vdev_config_dirty(tvd);
993 991 }
994 992
995 993 if (list_link_active(&svd->vdev_state_dirty_node)) {
996 994 vdev_state_clean(svd);
997 995 vdev_state_dirty(tvd);
998 996 }
999 997
1000 998 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1001 999 svd->vdev_deflate_ratio = 0;
1002 1000
1003 1001 tvd->vdev_islog = svd->vdev_islog;
1004 1002 svd->vdev_islog = 0;
1005 1003 }
1006 1004
1007 1005 static void
1008 1006 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1009 1007 {
1010 1008 if (vd == NULL)
1011 1009 return;
1012 1010
1013 1011 vd->vdev_top = tvd;
1014 1012
1015 1013 for (int c = 0; c < vd->vdev_children; c++)
1016 1014 vdev_top_update(tvd, vd->vdev_child[c]);
1017 1015 }
1018 1016
1019 1017 /*
1020 1018 * Add a mirror/replacing vdev above an existing vdev.
1021 1019 */
1022 1020 vdev_t *
1023 1021 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1024 1022 {
1025 1023 spa_t *spa = cvd->vdev_spa;
1026 1024 vdev_t *pvd = cvd->vdev_parent;
1027 1025 vdev_t *mvd;
1028 1026
1029 1027 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1030 1028
1031 1029 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1032 1030
1033 1031 mvd->vdev_asize = cvd->vdev_asize;
1034 1032 mvd->vdev_min_asize = cvd->vdev_min_asize;
1035 1033 mvd->vdev_max_asize = cvd->vdev_max_asize;
1036 1034 mvd->vdev_psize = cvd->vdev_psize;
1037 1035 mvd->vdev_ashift = cvd->vdev_ashift;
1038 1036 mvd->vdev_state = cvd->vdev_state;
1039 1037 mvd->vdev_crtxg = cvd->vdev_crtxg;
1040 1038
1041 1039 vdev_remove_child(pvd, cvd);
1042 1040 vdev_add_child(pvd, mvd);
1043 1041 cvd->vdev_id = mvd->vdev_children;
1044 1042 vdev_add_child(mvd, cvd);
1045 1043 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1046 1044
1047 1045 if (mvd == mvd->vdev_top)
1048 1046 vdev_top_transfer(cvd, mvd);
1049 1047
1050 1048 return (mvd);
1051 1049 }
1052 1050
1053 1051 /*
1054 1052 * Remove a 1-way mirror/replacing vdev from the tree.
1055 1053 */
1056 1054 void
1057 1055 vdev_remove_parent(vdev_t *cvd)
1058 1056 {
1059 1057 vdev_t *mvd = cvd->vdev_parent;
1060 1058 vdev_t *pvd = mvd->vdev_parent;
1061 1059
1062 1060 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1063 1061
1064 1062 ASSERT(mvd->vdev_children == 1);
1065 1063 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1066 1064 mvd->vdev_ops == &vdev_replacing_ops ||
1067 1065 mvd->vdev_ops == &vdev_spare_ops);
1068 1066 cvd->vdev_ashift = mvd->vdev_ashift;
1069 1067
1070 1068 vdev_remove_child(mvd, cvd);
1071 1069 vdev_remove_child(pvd, mvd);
1072 1070
1073 1071 /*
1074 1072 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1075 1073 * Otherwise, we could have detached an offline device, and when we
1076 1074 * go to import the pool we'll think we have two top-level vdevs,
1077 1075 * instead of a different version of the same top-level vdev.
1078 1076 */
1079 1077 if (mvd->vdev_top == mvd) {
1080 1078 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1081 1079 cvd->vdev_orig_guid = cvd->vdev_guid;
1082 1080 cvd->vdev_guid += guid_delta;
1083 1081 cvd->vdev_guid_sum += guid_delta;
1084 1082 }
1085 1083 cvd->vdev_id = mvd->vdev_id;
1086 1084 vdev_add_child(pvd, cvd);
1087 1085 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1088 1086
1089 1087 if (cvd == cvd->vdev_top)
1090 1088 vdev_top_transfer(mvd, cvd);
1091 1089
1092 1090 ASSERT(mvd->vdev_children == 0);
1093 1091 vdev_free(mvd);
1094 1092 }
1095 1093
1096 1094 static void
1097 1095 vdev_metaslab_group_create(vdev_t *vd)
1098 1096 {
1099 1097 spa_t *spa = vd->vdev_spa;
1100 1098
1101 1099 /*
1102 1100 * metaslab_group_create was delayed until allocation bias was available
1103 1101 */
1104 1102 if (vd->vdev_mg == NULL) {
1105 1103 metaslab_class_t *mc;
1106 1104
1107 1105 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1108 1106 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1109 1107
1110 1108 ASSERT3U(vd->vdev_islog, ==,
1111 1109 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1112 1110
1113 1111 switch (vd->vdev_alloc_bias) {
1114 1112 case VDEV_BIAS_LOG:
1115 1113 mc = spa_log_class(spa);
1116 1114 break;
1117 1115 case VDEV_BIAS_SPECIAL:
1118 1116 mc = spa_special_class(spa);
1119 1117 break;
1120 1118 case VDEV_BIAS_DEDUP:
1121 1119 mc = spa_dedup_class(spa);
1122 1120 break;
1123 1121 default:
1124 1122 mc = spa_normal_class(spa);
1125 1123 }
1126 1124
1127 1125 vd->vdev_mg = metaslab_group_create(mc, vd,
1128 1126 spa->spa_alloc_count);
1129 1127
1130 1128 /*
1131 1129 * The spa ashift values currently only reflect the
1132 1130 * general vdev classes. Class destination is late
1133 1131 * binding so ashift checking had to wait until now
1134 1132 */
1135 1133 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1136 1134 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1137 1135 if (vd->vdev_ashift > spa->spa_max_ashift)
1138 1136 spa->spa_max_ashift = vd->vdev_ashift;
1139 1137 if (vd->vdev_ashift < spa->spa_min_ashift)
1140 1138 spa->spa_min_ashift = vd->vdev_ashift;
1141 1139 }
1142 1140 }
1143 1141 }
1144 1142
1145 1143 int
1146 1144 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1147 1145 {
1148 1146 spa_t *spa = vd->vdev_spa;
1149 1147 objset_t *mos = spa->spa_meta_objset;
1150 1148 uint64_t m;
1151 1149 uint64_t oldc = vd->vdev_ms_count;
1152 1150 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1153 1151 metaslab_t **mspp;
1154 1152 int error;
1155 1153 boolean_t expanding = (oldc != 0);
1156 1154
1157 1155 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1158 1156
1159 1157 /*
1160 1158 * This vdev is not being allocated from yet or is a hole.
1161 1159 */
1162 1160 if (vd->vdev_ms_shift == 0)
1163 1161 return (0);
1164 1162
1165 1163 ASSERT(!vd->vdev_ishole);
1166 1164
1167 1165 ASSERT(oldc <= newc);
1168 1166
1169 1167 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1170 1168
1171 1169 if (expanding) {
1172 1170 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
1173 1171 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1174 1172 }
1175 1173
1176 1174 vd->vdev_ms = mspp;
1177 1175 vd->vdev_ms_count = newc;
1178 1176 for (m = oldc; m < newc; m++) {
1179 1177 uint64_t object = 0;
1180 1178
1181 1179 /*
1182 1180 * vdev_ms_array may be 0 if we are creating the "fake"
1183 1181 * metaslabs for an indirect vdev for zdb's leak detection.
1184 1182 * See zdb_leak_init().
1185 1183 */
1186 1184 if (txg == 0 && vd->vdev_ms_array != 0) {
1187 1185 error = dmu_read(mos, vd->vdev_ms_array,
1188 1186 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1189 1187 DMU_READ_PREFETCH);
1190 1188 if (error != 0) {
1191 1189 vdev_dbgmsg(vd, "unable to read the metaslab "
1192 1190 "array [error=%d]", error);
1193 1191 return (error);
1194 1192 }
1195 1193 }
1196 1194
1197 1195 #ifndef _KERNEL
1198 1196 /*
1199 1197 * To accomodate zdb_leak_init() fake indirect
1200 1198 * metaslabs, we allocate a metaslab group for
1201 1199 * indirect vdevs which normally don't have one.
1202 1200 */
1203 1201 if (vd->vdev_mg == NULL) {
1204 1202 ASSERT0(vdev_is_concrete(vd));
1205 1203 vdev_metaslab_group_create(vd);
1206 1204 }
1207 1205 #endif
1208 1206 error = metaslab_init(vd->vdev_mg, m, object, txg,
1209 1207 &(vd->vdev_ms[m]));
1210 1208 if (error != 0) {
1211 1209 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1212 1210 error);
1213 1211 return (error);
1214 1212 }
1215 1213 }
1216 1214
1217 1215 if (txg == 0)
1218 1216 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1219 1217
1220 1218 /*
1221 1219 * If the vdev is being removed we don't activate
1222 1220 * the metaslabs since we want to ensure that no new
1223 1221 * allocations are performed on this device.
1224 1222 */
1225 1223 if (!expanding && !vd->vdev_removing) {
1226 1224 metaslab_group_activate(vd->vdev_mg);
1227 1225 }
1228 1226
1229 1227 if (txg == 0)
1230 1228 spa_config_exit(spa, SCL_ALLOC, FTAG);
1231 1229
1232 1230 return (0);
1233 1231 }
1234 1232
1235 1233 void
1236 1234 vdev_metaslab_fini(vdev_t *vd)
1237 1235 {
1238 1236 if (vd->vdev_checkpoint_sm != NULL) {
1239 1237 ASSERT(spa_feature_is_active(vd->vdev_spa,
1240 1238 SPA_FEATURE_POOL_CHECKPOINT));
1241 1239 space_map_close(vd->vdev_checkpoint_sm);
1242 1240 /*
1243 1241 * Even though we close the space map, we need to set its
|
↓ open down ↓ |
341 lines elided |
↑ open up ↑ |
1244 1242 * pointer to NULL. The reason is that vdev_metaslab_fini()
1245 1243 * may be called multiple times for certain operations
1246 1244 * (i.e. when destroying a pool) so we need to ensure that
1247 1245 * this clause never executes twice. This logic is similar
1248 1246 * to the one used for the vdev_ms clause below.
1249 1247 */
1250 1248 vd->vdev_checkpoint_sm = NULL;
1251 1249 }
1252 1250
1253 1251 if (vd->vdev_ms != NULL) {
1254 - uint64_t count = vd->vdev_ms_count;
1252 + metaslab_group_t *mg = vd->vdev_mg;
1253 + metaslab_group_passivate(mg);
1255 1254
1256 - metaslab_group_passivate(vd->vdev_mg);
1255 + uint64_t count = vd->vdev_ms_count;
1257 1256 for (uint64_t m = 0; m < count; m++) {
1258 1257 metaslab_t *msp = vd->vdev_ms[m];
1259 -
1260 1258 if (msp != NULL)
1261 1259 metaslab_fini(msp);
1262 1260 }
1263 1261 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1264 1262 vd->vdev_ms = NULL;
1265 1263
1266 1264 vd->vdev_ms_count = 0;
1265 +
1266 + for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
1267 + ASSERT0(mg->mg_histogram[i]);
1267 1268 }
1268 1269 ASSERT0(vd->vdev_ms_count);
1269 1270 }
1270 1271
1271 1272 typedef struct vdev_probe_stats {
1272 1273 boolean_t vps_readable;
1273 1274 boolean_t vps_writeable;
1274 1275 int vps_flags;
1275 1276 } vdev_probe_stats_t;
1276 1277
1277 1278 static void
1278 1279 vdev_probe_done(zio_t *zio)
1279 1280 {
1280 1281 spa_t *spa = zio->io_spa;
1281 1282 vdev_t *vd = zio->io_vd;
1282 1283 vdev_probe_stats_t *vps = zio->io_private;
1283 1284
1284 1285 ASSERT(vd->vdev_probe_zio != NULL);
1285 1286
1286 1287 if (zio->io_type == ZIO_TYPE_READ) {
1287 1288 if (zio->io_error == 0)
1288 1289 vps->vps_readable = 1;
1289 1290 if (zio->io_error == 0 && spa_writeable(spa)) {
1290 1291 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1291 1292 zio->io_offset, zio->io_size, zio->io_abd,
1292 1293 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1293 1294 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1294 1295 } else {
1295 1296 abd_free(zio->io_abd);
1296 1297 }
1297 1298 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1298 1299 if (zio->io_error == 0)
1299 1300 vps->vps_writeable = 1;
1300 1301 abd_free(zio->io_abd);
1301 1302 } else if (zio->io_type == ZIO_TYPE_NULL) {
1302 1303 zio_t *pio;
1303 1304
1304 1305 vd->vdev_cant_read |= !vps->vps_readable;
1305 1306 vd->vdev_cant_write |= !vps->vps_writeable;
1306 1307
1307 1308 if (vdev_readable(vd) &&
1308 1309 (vdev_writeable(vd) || !spa_writeable(spa))) {
1309 1310 zio->io_error = 0;
1310 1311 } else {
1311 1312 ASSERT(zio->io_error != 0);
1312 1313 vdev_dbgmsg(vd, "failed probe");
1313 1314 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1314 1315 spa, vd, NULL, 0, 0);
1315 1316 zio->io_error = SET_ERROR(ENXIO);
1316 1317 }
1317 1318
1318 1319 mutex_enter(&vd->vdev_probe_lock);
1319 1320 ASSERT(vd->vdev_probe_zio == zio);
1320 1321 vd->vdev_probe_zio = NULL;
1321 1322 mutex_exit(&vd->vdev_probe_lock);
1322 1323
1323 1324 zio_link_t *zl = NULL;
1324 1325 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1325 1326 if (!vdev_accessible(vd, pio))
1326 1327 pio->io_error = SET_ERROR(ENXIO);
1327 1328
1328 1329 kmem_free(vps, sizeof (*vps));
1329 1330 }
1330 1331 }
1331 1332
1332 1333 /*
1333 1334 * Determine whether this device is accessible.
1334 1335 *
1335 1336 * Read and write to several known locations: the pad regions of each
1336 1337 * vdev label but the first, which we leave alone in case it contains
1337 1338 * a VTOC.
1338 1339 */
1339 1340 zio_t *
1340 1341 vdev_probe(vdev_t *vd, zio_t *zio)
1341 1342 {
1342 1343 spa_t *spa = vd->vdev_spa;
1343 1344 vdev_probe_stats_t *vps = NULL;
1344 1345 zio_t *pio;
1345 1346
1346 1347 ASSERT(vd->vdev_ops->vdev_op_leaf);
1347 1348
1348 1349 /*
1349 1350 * Don't probe the probe.
1350 1351 */
1351 1352 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1352 1353 return (NULL);
1353 1354
1354 1355 /*
1355 1356 * To prevent 'probe storms' when a device fails, we create
1356 1357 * just one probe i/o at a time. All zios that want to probe
1357 1358 * this vdev will become parents of the probe io.
1358 1359 */
1359 1360 mutex_enter(&vd->vdev_probe_lock);
1360 1361
1361 1362 if ((pio = vd->vdev_probe_zio) == NULL) {
1362 1363 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1363 1364
1364 1365 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1365 1366 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1366 1367 ZIO_FLAG_TRYHARD;
1367 1368
1368 1369 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1369 1370 /*
1370 1371 * vdev_cant_read and vdev_cant_write can only
1371 1372 * transition from TRUE to FALSE when we have the
1372 1373 * SCL_ZIO lock as writer; otherwise they can only
1373 1374 * transition from FALSE to TRUE. This ensures that
1374 1375 * any zio looking at these values can assume that
1375 1376 * failures persist for the life of the I/O. That's
1376 1377 * important because when a device has intermittent
1377 1378 * connectivity problems, we want to ensure that
1378 1379 * they're ascribed to the device (ENXIO) and not
1379 1380 * the zio (EIO).
1380 1381 *
1381 1382 * Since we hold SCL_ZIO as writer here, clear both
1382 1383 * values so the probe can reevaluate from first
1383 1384 * principles.
1384 1385 */
1385 1386 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1386 1387 vd->vdev_cant_read = B_FALSE;
1387 1388 vd->vdev_cant_write = B_FALSE;
1388 1389 }
1389 1390
1390 1391 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1391 1392 vdev_probe_done, vps,
1392 1393 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1393 1394
1394 1395 /*
1395 1396 * We can't change the vdev state in this context, so we
1396 1397 * kick off an async task to do it on our behalf.
1397 1398 */
1398 1399 if (zio != NULL) {
1399 1400 vd->vdev_probe_wanted = B_TRUE;
1400 1401 spa_async_request(spa, SPA_ASYNC_PROBE);
1401 1402 }
1402 1403 }
1403 1404
1404 1405 if (zio != NULL)
1405 1406 zio_add_child(zio, pio);
1406 1407
1407 1408 mutex_exit(&vd->vdev_probe_lock);
1408 1409
1409 1410 if (vps == NULL) {
1410 1411 ASSERT(zio != NULL);
1411 1412 return (NULL);
1412 1413 }
1413 1414
1414 1415 for (int l = 1; l < VDEV_LABELS; l++) {
1415 1416 zio_nowait(zio_read_phys(pio, vd,
1416 1417 vdev_label_offset(vd->vdev_psize, l,
1417 1418 offsetof(vdev_label_t, vl_pad2)), VDEV_PAD_SIZE,
1418 1419 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1419 1420 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1420 1421 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1421 1422 }
1422 1423
1423 1424 if (zio == NULL)
1424 1425 return (pio);
1425 1426
1426 1427 zio_nowait(pio);
1427 1428 return (NULL);
1428 1429 }
1429 1430
1430 1431 static void
1431 1432 vdev_open_child(void *arg)
1432 1433 {
1433 1434 vdev_t *vd = arg;
1434 1435
1435 1436 vd->vdev_open_thread = curthread;
1436 1437 vd->vdev_open_error = vdev_open(vd);
1437 1438 vd->vdev_open_thread = NULL;
1438 1439 }
1439 1440
1440 1441 boolean_t
1441 1442 vdev_uses_zvols(vdev_t *vd)
1442 1443 {
1443 1444 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1444 1445 strlen(ZVOL_DIR)) == 0)
1445 1446 return (B_TRUE);
1446 1447 for (int c = 0; c < vd->vdev_children; c++)
1447 1448 if (vdev_uses_zvols(vd->vdev_child[c]))
1448 1449 return (B_TRUE);
1449 1450 return (B_FALSE);
1450 1451 }
1451 1452
1452 1453 void
1453 1454 vdev_open_children(vdev_t *vd)
1454 1455 {
1455 1456 taskq_t *tq;
1456 1457 int children = vd->vdev_children;
1457 1458
1458 1459 /*
1459 1460 * in order to handle pools on top of zvols, do the opens
1460 1461 * in a single thread so that the same thread holds the
1461 1462 * spa_namespace_lock
1462 1463 */
1463 1464 if (vdev_uses_zvols(vd)) {
1464 1465 for (int c = 0; c < children; c++)
1465 1466 vd->vdev_child[c]->vdev_open_error =
1466 1467 vdev_open(vd->vdev_child[c]);
1467 1468 return;
1468 1469 }
1469 1470 tq = taskq_create("vdev_open", children, minclsyspri,
1470 1471 children, children, TASKQ_PREPOPULATE);
1471 1472
1472 1473 for (int c = 0; c < children; c++)
1473 1474 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1474 1475 TQ_SLEEP) != TASKQID_INVALID);
1475 1476
1476 1477 taskq_destroy(tq);
1477 1478 }
1478 1479
1479 1480 /*
1480 1481 * Compute the raidz-deflation ratio. Note, we hard-code
1481 1482 * in 128k (1 << 17) because it is the "typical" blocksize.
1482 1483 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
1483 1484 * otherwise it would inconsistently account for existing bp's.
1484 1485 */
1485 1486 static void
1486 1487 vdev_set_deflate_ratio(vdev_t *vd)
1487 1488 {
1488 1489 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1489 1490 vd->vdev_deflate_ratio = (1 << 17) /
1490 1491 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
1491 1492 }
1492 1493 }
1493 1494
1494 1495 /*
1495 1496 * Prepare a virtual device for access.
1496 1497 */
1497 1498 int
1498 1499 vdev_open(vdev_t *vd)
1499 1500 {
1500 1501 spa_t *spa = vd->vdev_spa;
1501 1502 int error;
1502 1503 uint64_t osize = 0;
1503 1504 uint64_t max_osize = 0;
1504 1505 uint64_t asize, max_asize, psize;
1505 1506 uint64_t ashift = 0;
1506 1507
1507 1508 ASSERT(vd->vdev_open_thread == curthread ||
1508 1509 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1509 1510 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1510 1511 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1511 1512 vd->vdev_state == VDEV_STATE_OFFLINE);
1512 1513
1513 1514 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1514 1515 vd->vdev_cant_read = B_FALSE;
1515 1516 vd->vdev_cant_write = B_FALSE;
1516 1517 vd->vdev_min_asize = vdev_get_min_asize(vd);
1517 1518
1518 1519 /*
1519 1520 * If this vdev is not removed, check its fault status. If it's
1520 1521 * faulted, bail out of the open.
1521 1522 */
1522 1523 if (!vd->vdev_removed && vd->vdev_faulted) {
1523 1524 ASSERT(vd->vdev_children == 0);
1524 1525 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1525 1526 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1526 1527 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1527 1528 vd->vdev_label_aux);
1528 1529 return (SET_ERROR(ENXIO));
1529 1530 } else if (vd->vdev_offline) {
1530 1531 ASSERT(vd->vdev_children == 0);
1531 1532 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1532 1533 return (SET_ERROR(ENXIO));
1533 1534 }
1534 1535
1535 1536 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1536 1537
1537 1538 /*
1538 1539 * Reset the vdev_reopening flag so that we actually close
1539 1540 * the vdev on error.
1540 1541 */
1541 1542 vd->vdev_reopening = B_FALSE;
1542 1543 if (zio_injection_enabled && error == 0)
1543 1544 error = zio_handle_device_injection(vd, NULL, ENXIO);
1544 1545
1545 1546 if (error) {
1546 1547 if (vd->vdev_removed &&
1547 1548 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1548 1549 vd->vdev_removed = B_FALSE;
1549 1550
1550 1551 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
1551 1552 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
1552 1553 vd->vdev_stat.vs_aux);
1553 1554 } else {
1554 1555 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1555 1556 vd->vdev_stat.vs_aux);
1556 1557 }
1557 1558 return (error);
1558 1559 }
1559 1560
1560 1561 vd->vdev_removed = B_FALSE;
1561 1562
1562 1563 /*
1563 1564 * Recheck the faulted flag now that we have confirmed that
1564 1565 * the vdev is accessible. If we're faulted, bail.
1565 1566 */
1566 1567 if (vd->vdev_faulted) {
1567 1568 ASSERT(vd->vdev_children == 0);
1568 1569 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1569 1570 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1570 1571 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1571 1572 vd->vdev_label_aux);
1572 1573 return (SET_ERROR(ENXIO));
1573 1574 }
1574 1575
1575 1576 if (vd->vdev_degraded) {
1576 1577 ASSERT(vd->vdev_children == 0);
1577 1578 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1578 1579 VDEV_AUX_ERR_EXCEEDED);
1579 1580 } else {
1580 1581 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1581 1582 }
1582 1583
1583 1584 /*
1584 1585 * For hole or missing vdevs we just return success.
1585 1586 */
1586 1587 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1587 1588 return (0);
1588 1589
1589 1590 for (int c = 0; c < vd->vdev_children; c++) {
1590 1591 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1591 1592 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1592 1593 VDEV_AUX_NONE);
1593 1594 break;
1594 1595 }
1595 1596 }
1596 1597
1597 1598 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1598 1599 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1599 1600
1600 1601 if (vd->vdev_children == 0) {
1601 1602 if (osize < SPA_MINDEVSIZE) {
1602 1603 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1603 1604 VDEV_AUX_TOO_SMALL);
1604 1605 return (SET_ERROR(EOVERFLOW));
1605 1606 }
1606 1607 psize = osize;
1607 1608 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1608 1609 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1609 1610 VDEV_LABEL_END_SIZE);
1610 1611 } else {
1611 1612 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1612 1613 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1613 1614 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1614 1615 VDEV_AUX_TOO_SMALL);
1615 1616 return (SET_ERROR(EOVERFLOW));
1616 1617 }
1617 1618 psize = 0;
1618 1619 asize = osize;
1619 1620 max_asize = max_osize;
1620 1621 }
1621 1622
1622 1623 vd->vdev_psize = psize;
1623 1624
1624 1625 /*
1625 1626 * Make sure the allocatable size hasn't shrunk too much.
1626 1627 */
1627 1628 if (asize < vd->vdev_min_asize) {
1628 1629 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1629 1630 VDEV_AUX_BAD_LABEL);
1630 1631 return (SET_ERROR(EINVAL));
1631 1632 }
1632 1633
1633 1634 if (vd->vdev_asize == 0) {
1634 1635 /*
1635 1636 * This is the first-ever open, so use the computed values.
1636 1637 * For testing purposes, a higher ashift can be requested.
1637 1638 */
1638 1639 vd->vdev_asize = asize;
1639 1640 vd->vdev_max_asize = max_asize;
1640 1641 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1641 1642 vd->vdev_ashift = MAX(zfs_ashift_min, vd->vdev_ashift);
1642 1643 } else {
1643 1644 /*
1644 1645 * Detect if the alignment requirement has increased.
1645 1646 * We don't want to make the pool unavailable, just
1646 1647 * issue a warning instead.
1647 1648 */
1648 1649 if (ashift > vd->vdev_top->vdev_ashift &&
1649 1650 vd->vdev_ops->vdev_op_leaf) {
1650 1651 cmn_err(CE_WARN,
1651 1652 "Disk, '%s', has a block alignment that is "
1652 1653 "larger than the pool's alignment\n",
1653 1654 vd->vdev_path);
1654 1655 }
1655 1656 vd->vdev_max_asize = max_asize;
1656 1657 }
1657 1658
1658 1659 /*
1659 1660 * If all children are healthy we update asize if either:
1660 1661 * The asize has increased, due to a device expansion caused by dynamic
1661 1662 * LUN growth or vdev replacement, and automatic expansion is enabled;
1662 1663 * making the additional space available.
1663 1664 *
1664 1665 * The asize has decreased, due to a device shrink usually caused by a
1665 1666 * vdev replace with a smaller device. This ensures that calculations
1666 1667 * based of max_asize and asize e.g. esize are always valid. It's safe
1667 1668 * to do this as we've already validated that asize is greater than
1668 1669 * vdev_min_asize.
1669 1670 */
1670 1671 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1671 1672 ((asize > vd->vdev_asize &&
1672 1673 (vd->vdev_expanding || spa->spa_autoexpand)) ||
1673 1674 (asize < vd->vdev_asize)))
1674 1675 vd->vdev_asize = asize;
1675 1676
1676 1677 vdev_set_min_asize(vd);
1677 1678
1678 1679 /*
1679 1680 * Ensure we can issue some IO before declaring the
1680 1681 * vdev open for business.
1681 1682 */
1682 1683 if (vd->vdev_ops->vdev_op_leaf &&
1683 1684 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1684 1685 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1685 1686 VDEV_AUX_ERR_EXCEEDED);
1686 1687 return (error);
1687 1688 }
1688 1689
1689 1690 /*
1690 1691 * Track the min and max ashift values for normal data devices.
1691 1692 *
1692 1693 * DJB - TBD these should perhaps be tracked per allocation class
1693 1694 * (e.g. spa_min_ashift is used to round up post compression buffers)
1694 1695 */
1695 1696 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1696 1697 vd->vdev_alloc_bias == VDEV_BIAS_NONE &&
1697 1698 vd->vdev_aux == NULL) {
1698 1699 if (vd->vdev_ashift > spa->spa_max_ashift)
1699 1700 spa->spa_max_ashift = vd->vdev_ashift;
1700 1701 if (vd->vdev_ashift < spa->spa_min_ashift)
1701 1702 spa->spa_min_ashift = vd->vdev_ashift;
1702 1703 }
1703 1704
1704 1705 /*
1705 1706 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1706 1707 * resilver. But don't do this if we are doing a reopen for a scrub,
1707 1708 * since this would just restart the scrub we are already doing.
1708 1709 */
1709 1710 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1710 1711 vdev_resilver_needed(vd, NULL, NULL))
1711 1712 spa_async_request(spa, SPA_ASYNC_RESILVER);
1712 1713
1713 1714 return (0);
1714 1715 }
1715 1716
1716 1717 /*
1717 1718 * Called once the vdevs are all opened, this routine validates the label
1718 1719 * contents. This needs to be done before vdev_load() so that we don't
1719 1720 * inadvertently do repair I/Os to the wrong device.
1720 1721 *
1721 1722 * This function will only return failure if one of the vdevs indicates that it
1722 1723 * has since been destroyed or exported. This is only possible if
1723 1724 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1724 1725 * will be updated but the function will return 0.
1725 1726 */
1726 1727 int
1727 1728 vdev_validate(vdev_t *vd)
1728 1729 {
1729 1730 spa_t *spa = vd->vdev_spa;
1730 1731 nvlist_t *label;
1731 1732 uint64_t guid = 0, aux_guid = 0, top_guid;
1732 1733 uint64_t state;
1733 1734 nvlist_t *nvl;
1734 1735 uint64_t txg;
1735 1736
1736 1737 if (vdev_validate_skip)
1737 1738 return (0);
1738 1739
1739 1740 for (uint64_t c = 0; c < vd->vdev_children; c++)
1740 1741 if (vdev_validate(vd->vdev_child[c]) != 0)
1741 1742 return (SET_ERROR(EBADF));
1742 1743
1743 1744 /*
1744 1745 * If the device has already failed, or was marked offline, don't do
1745 1746 * any further validation. Otherwise, label I/O will fail and we will
1746 1747 * overwrite the previous state.
1747 1748 */
1748 1749 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
1749 1750 return (0);
1750 1751
1751 1752 /*
1752 1753 * If we are performing an extreme rewind, we allow for a label that
1753 1754 * was modified at a point after the current txg.
1754 1755 * If config lock is not held do not check for the txg. spa_sync could
1755 1756 * be updating the vdev's label before updating spa_last_synced_txg.
1756 1757 */
1757 1758 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
1758 1759 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
1759 1760 txg = UINT64_MAX;
1760 1761 else
1761 1762 txg = spa_last_synced_txg(spa);
1762 1763
1763 1764 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1764 1765 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1765 1766 VDEV_AUX_BAD_LABEL);
1766 1767 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
1767 1768 "txg %llu", (u_longlong_t)txg);
1768 1769 return (0);
1769 1770 }
1770 1771
1771 1772 /*
1772 1773 * Determine if this vdev has been split off into another
1773 1774 * pool. If so, then refuse to open it.
1774 1775 */
1775 1776 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1776 1777 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1777 1778 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1778 1779 VDEV_AUX_SPLIT_POOL);
1779 1780 nvlist_free(label);
1780 1781 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
1781 1782 return (0);
1782 1783 }
1783 1784
1784 1785 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
1785 1786 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1786 1787 VDEV_AUX_CORRUPT_DATA);
1787 1788 nvlist_free(label);
1788 1789 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1789 1790 ZPOOL_CONFIG_POOL_GUID);
1790 1791 return (0);
1791 1792 }
1792 1793
1793 1794 /*
1794 1795 * If config is not trusted then ignore the spa guid check. This is
1795 1796 * necessary because if the machine crashed during a re-guid the new
1796 1797 * guid might have been written to all of the vdev labels, but not the
1797 1798 * cached config. The check will be performed again once we have the
1798 1799 * trusted config from the MOS.
1799 1800 */
1800 1801 if (spa->spa_trust_config && guid != spa_guid(spa)) {
1801 1802 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1802 1803 VDEV_AUX_CORRUPT_DATA);
1803 1804 nvlist_free(label);
1804 1805 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
1805 1806 "match config (%llu != %llu)", (u_longlong_t)guid,
1806 1807 (u_longlong_t)spa_guid(spa));
1807 1808 return (0);
1808 1809 }
1809 1810
1810 1811 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1811 1812 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1812 1813 &aux_guid) != 0)
1813 1814 aux_guid = 0;
1814 1815
1815 1816 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
1816 1817 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1817 1818 VDEV_AUX_CORRUPT_DATA);
1818 1819 nvlist_free(label);
1819 1820 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1820 1821 ZPOOL_CONFIG_GUID);
1821 1822 return (0);
1822 1823 }
1823 1824
1824 1825 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
1825 1826 != 0) {
1826 1827 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1827 1828 VDEV_AUX_CORRUPT_DATA);
1828 1829 nvlist_free(label);
1829 1830 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1830 1831 ZPOOL_CONFIG_TOP_GUID);
1831 1832 return (0);
1832 1833 }
1833 1834
1834 1835 /*
1835 1836 * If this vdev just became a top-level vdev because its sibling was
1836 1837 * detached, it will have adopted the parent's vdev guid -- but the
1837 1838 * label may or may not be on disk yet. Fortunately, either version
1838 1839 * of the label will have the same top guid, so if we're a top-level
1839 1840 * vdev, we can safely compare to that instead.
1840 1841 * However, if the config comes from a cachefile that failed to update
1841 1842 * after the detach, a top-level vdev will appear as a non top-level
1842 1843 * vdev in the config. Also relax the constraints if we perform an
1843 1844 * extreme rewind.
1844 1845 *
1845 1846 * If we split this vdev off instead, then we also check the
1846 1847 * original pool's guid. We don't want to consider the vdev
1847 1848 * corrupt if it is partway through a split operation.
1848 1849 */
1849 1850 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
1850 1851 boolean_t mismatch = B_FALSE;
1851 1852 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
1852 1853 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
1853 1854 mismatch = B_TRUE;
1854 1855 } else {
1855 1856 if (vd->vdev_guid != top_guid &&
1856 1857 vd->vdev_top->vdev_guid != guid)
1857 1858 mismatch = B_TRUE;
1858 1859 }
1859 1860
1860 1861 if (mismatch) {
1861 1862 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1862 1863 VDEV_AUX_CORRUPT_DATA);
1863 1864 nvlist_free(label);
1864 1865 vdev_dbgmsg(vd, "vdev_validate: config guid "
1865 1866 "doesn't match label guid");
1866 1867 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
1867 1868 (u_longlong_t)vd->vdev_guid,
1868 1869 (u_longlong_t)vd->vdev_top->vdev_guid);
1869 1870 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
1870 1871 "aux_guid %llu", (u_longlong_t)guid,
1871 1872 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
1872 1873 return (0);
1873 1874 }
1874 1875 }
1875 1876
1876 1877 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1877 1878 &state) != 0) {
1878 1879 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1879 1880 VDEV_AUX_CORRUPT_DATA);
1880 1881 nvlist_free(label);
1881 1882 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
1882 1883 ZPOOL_CONFIG_POOL_STATE);
1883 1884 return (0);
1884 1885 }
1885 1886
1886 1887 nvlist_free(label);
1887 1888
1888 1889 /*
1889 1890 * If this is a verbatim import, no need to check the
1890 1891 * state of the pool.
1891 1892 */
1892 1893 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1893 1894 spa_load_state(spa) == SPA_LOAD_OPEN &&
1894 1895 state != POOL_STATE_ACTIVE) {
1895 1896 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
1896 1897 "for spa %s", (u_longlong_t)state, spa->spa_name);
1897 1898 return (SET_ERROR(EBADF));
1898 1899 }
1899 1900
1900 1901 /*
1901 1902 * If we were able to open and validate a vdev that was
1902 1903 * previously marked permanently unavailable, clear that state
1903 1904 * now.
1904 1905 */
1905 1906 if (vd->vdev_not_present)
1906 1907 vd->vdev_not_present = 0;
1907 1908
1908 1909 return (0);
1909 1910 }
1910 1911
1911 1912 static void
1912 1913 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
1913 1914 {
1914 1915 if (svd->vdev_path != NULL && dvd->vdev_path != NULL) {
1915 1916 if (strcmp(svd->vdev_path, dvd->vdev_path) != 0) {
1916 1917 zfs_dbgmsg("vdev_copy_path: vdev %llu: path changed "
1917 1918 "from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
1918 1919 dvd->vdev_path, svd->vdev_path);
1919 1920 spa_strfree(dvd->vdev_path);
1920 1921 dvd->vdev_path = spa_strdup(svd->vdev_path);
1921 1922 }
1922 1923 } else if (svd->vdev_path != NULL) {
1923 1924 dvd->vdev_path = spa_strdup(svd->vdev_path);
1924 1925 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
1925 1926 (u_longlong_t)dvd->vdev_guid, dvd->vdev_path);
1926 1927 }
1927 1928 }
1928 1929
1929 1930 /*
1930 1931 * Recursively copy vdev paths from one vdev to another. Source and destination
1931 1932 * vdev trees must have same geometry otherwise return error. Intended to copy
1932 1933 * paths from userland config into MOS config.
1933 1934 */
1934 1935 int
1935 1936 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
1936 1937 {
1937 1938 if ((svd->vdev_ops == &vdev_missing_ops) ||
1938 1939 (svd->vdev_ishole && dvd->vdev_ishole) ||
1939 1940 (dvd->vdev_ops == &vdev_indirect_ops))
1940 1941 return (0);
1941 1942
1942 1943 if (svd->vdev_ops != dvd->vdev_ops) {
1943 1944 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
1944 1945 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
1945 1946 return (SET_ERROR(EINVAL));
1946 1947 }
1947 1948
1948 1949 if (svd->vdev_guid != dvd->vdev_guid) {
1949 1950 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
1950 1951 "%llu)", (u_longlong_t)svd->vdev_guid,
1951 1952 (u_longlong_t)dvd->vdev_guid);
1952 1953 return (SET_ERROR(EINVAL));
1953 1954 }
1954 1955
1955 1956 if (svd->vdev_children != dvd->vdev_children) {
1956 1957 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
1957 1958 "%llu != %llu", (u_longlong_t)svd->vdev_children,
1958 1959 (u_longlong_t)dvd->vdev_children);
1959 1960 return (SET_ERROR(EINVAL));
1960 1961 }
1961 1962
1962 1963 for (uint64_t i = 0; i < svd->vdev_children; i++) {
1963 1964 int error = vdev_copy_path_strict(svd->vdev_child[i],
1964 1965 dvd->vdev_child[i]);
1965 1966 if (error != 0)
1966 1967 return (error);
1967 1968 }
1968 1969
1969 1970 if (svd->vdev_ops->vdev_op_leaf)
1970 1971 vdev_copy_path_impl(svd, dvd);
1971 1972
1972 1973 return (0);
1973 1974 }
1974 1975
1975 1976 static void
1976 1977 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
1977 1978 {
1978 1979 ASSERT(stvd->vdev_top == stvd);
1979 1980 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
1980 1981
1981 1982 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
1982 1983 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
1983 1984 }
1984 1985
1985 1986 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
1986 1987 return;
1987 1988
1988 1989 /*
1989 1990 * The idea here is that while a vdev can shift positions within
1990 1991 * a top vdev (when replacing, attaching mirror, etc.) it cannot
1991 1992 * step outside of it.
1992 1993 */
1993 1994 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
1994 1995
1995 1996 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
1996 1997 return;
1997 1998
1998 1999 ASSERT(vd->vdev_ops->vdev_op_leaf);
1999 2000
2000 2001 vdev_copy_path_impl(vd, dvd);
2001 2002 }
2002 2003
2003 2004 /*
2004 2005 * Recursively copy vdev paths from one root vdev to another. Source and
2005 2006 * destination vdev trees may differ in geometry. For each destination leaf
2006 2007 * vdev, search a vdev with the same guid and top vdev id in the source.
2007 2008 * Intended to copy paths from userland config into MOS config.
2008 2009 */
2009 2010 void
2010 2011 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2011 2012 {
2012 2013 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2013 2014 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2014 2015 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2015 2016
2016 2017 for (uint64_t i = 0; i < children; i++) {
2017 2018 vdev_copy_path_search(srvd->vdev_child[i],
2018 2019 drvd->vdev_child[i]);
2019 2020 }
2020 2021 }
2021 2022
2022 2023 /*
2023 2024 * Close a virtual device.
2024 2025 */
2025 2026 void
2026 2027 vdev_close(vdev_t *vd)
2027 2028 {
2028 2029 spa_t *spa = vd->vdev_spa;
2029 2030 vdev_t *pvd = vd->vdev_parent;
2030 2031
2031 2032 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2032 2033
2033 2034 /*
2034 2035 * If our parent is reopening, then we are as well, unless we are
2035 2036 * going offline.
2036 2037 */
2037 2038 if (pvd != NULL && pvd->vdev_reopening)
2038 2039 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2039 2040
2040 2041 vd->vdev_ops->vdev_op_close(vd);
2041 2042
2042 2043 vdev_cache_purge(vd);
2043 2044
2044 2045 /*
2045 2046 * We record the previous state before we close it, so that if we are
2046 2047 * doing a reopen(), we don't generate FMA ereports if we notice that
2047 2048 * it's still faulted.
2048 2049 */
2049 2050 vd->vdev_prevstate = vd->vdev_state;
2050 2051
2051 2052 if (vd->vdev_offline)
2052 2053 vd->vdev_state = VDEV_STATE_OFFLINE;
2053 2054 else
2054 2055 vd->vdev_state = VDEV_STATE_CLOSED;
2055 2056 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2056 2057 }
2057 2058
2058 2059 void
2059 2060 vdev_hold(vdev_t *vd)
2060 2061 {
2061 2062 spa_t *spa = vd->vdev_spa;
2062 2063
2063 2064 ASSERT(spa_is_root(spa));
2064 2065 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2065 2066 return;
2066 2067
2067 2068 for (int c = 0; c < vd->vdev_children; c++)
2068 2069 vdev_hold(vd->vdev_child[c]);
2069 2070
2070 2071 if (vd->vdev_ops->vdev_op_leaf)
2071 2072 vd->vdev_ops->vdev_op_hold(vd);
2072 2073 }
2073 2074
2074 2075 void
2075 2076 vdev_rele(vdev_t *vd)
2076 2077 {
2077 2078 spa_t *spa = vd->vdev_spa;
2078 2079
2079 2080 ASSERT(spa_is_root(spa));
2080 2081 for (int c = 0; c < vd->vdev_children; c++)
2081 2082 vdev_rele(vd->vdev_child[c]);
2082 2083
2083 2084 if (vd->vdev_ops->vdev_op_leaf)
2084 2085 vd->vdev_ops->vdev_op_rele(vd);
2085 2086 }
2086 2087
2087 2088 /*
2088 2089 * Reopen all interior vdevs and any unopened leaves. We don't actually
2089 2090 * reopen leaf vdevs which had previously been opened as they might deadlock
2090 2091 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2091 2092 * If the leaf has never been opened then open it, as usual.
2092 2093 */
2093 2094 void
2094 2095 vdev_reopen(vdev_t *vd)
2095 2096 {
2096 2097 spa_t *spa = vd->vdev_spa;
2097 2098
2098 2099 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2099 2100
2100 2101 /* set the reopening flag unless we're taking the vdev offline */
2101 2102 vd->vdev_reopening = !vd->vdev_offline;
2102 2103 vdev_close(vd);
2103 2104 (void) vdev_open(vd);
2104 2105
2105 2106 /*
2106 2107 * Call vdev_validate() here to make sure we have the same device.
2107 2108 * Otherwise, a device with an invalid label could be successfully
2108 2109 * opened in response to vdev_reopen().
2109 2110 */
2110 2111 if (vd->vdev_aux) {
2111 2112 (void) vdev_validate_aux(vd);
2112 2113 if (vdev_readable(vd) && vdev_writeable(vd) &&
2113 2114 vd->vdev_aux == &spa->spa_l2cache &&
2114 2115 !l2arc_vdev_present(vd))
2115 2116 l2arc_add_vdev(spa, vd);
2116 2117 } else {
2117 2118 (void) vdev_validate(vd);
2118 2119 }
2119 2120
2120 2121 /*
2121 2122 * Reassess parent vdev's health.
2122 2123 */
2123 2124 vdev_propagate_state(vd);
2124 2125 }
2125 2126
2126 2127 int
2127 2128 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2128 2129 {
2129 2130 int error;
2130 2131
2131 2132 /*
2132 2133 * Normally, partial opens (e.g. of a mirror) are allowed.
2133 2134 * For a create, however, we want to fail the request if
2134 2135 * there are any components we can't open.
2135 2136 */
2136 2137 error = vdev_open(vd);
2137 2138
2138 2139 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2139 2140 vdev_close(vd);
2140 2141 return (error ? error : ENXIO);
2141 2142 }
2142 2143
2143 2144 /*
2144 2145 * Recursively load DTLs and initialize all labels.
2145 2146 */
2146 2147 if ((error = vdev_dtl_load(vd)) != 0 ||
2147 2148 (error = vdev_label_init(vd, txg, isreplacing ?
2148 2149 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2149 2150 vdev_close(vd);
2150 2151 return (error);
2151 2152 }
2152 2153
2153 2154 return (0);
2154 2155 }
2155 2156
2156 2157 void
2157 2158 vdev_metaslab_set_size(vdev_t *vd)
2158 2159 {
2159 2160 uint64_t asize = vd->vdev_asize;
2160 2161 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2161 2162 uint64_t ms_shift;
2162 2163
2163 2164 /*
2164 2165 * There are two dimensions to the metaslab sizing calculation:
2165 2166 * the size of the metaslab and the count of metaslabs per vdev.
2166 2167 *
2167 2168 * The default values used below are a good balance between memory
2168 2169 * usage (larger metaslab size means more memory needed for loaded
2169 2170 * metaslabs; more metaslabs means more memory needed for the
2170 2171 * metaslab_t structs), metaslab load time (larger metaslabs take
2171 2172 * longer to load), and metaslab sync time (more metaslabs means
2172 2173 * more time spent syncing all of them).
2173 2174 *
2174 2175 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2175 2176 * The range of the dimensions are as follows:
2176 2177 *
2177 2178 * 2^29 <= ms_size <= 2^34
2178 2179 * 16 <= ms_count <= 131,072
2179 2180 *
2180 2181 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2181 2182 * at least 512MB (2^29) to minimize fragmentation effects when
2182 2183 * testing with smaller devices. However, the count constraint
2183 2184 * of at least 16 metaslabs will override this minimum size goal.
2184 2185 *
2185 2186 * On the upper end of vdev sizes, we aim for a maximum metaslab
2186 2187 * size of 16GB. However, we will cap the total count to 2^17
2187 2188 * metaslabs to keep our memory footprint in check and let the
2188 2189 * metaslab size grow from there if that limit is hit.
2189 2190 *
2190 2191 * The net effect of applying above constrains is summarized below.
2191 2192 *
2192 2193 * vdev size metaslab count
2193 2194 * --------------|-----------------
2194 2195 * < 8GB ~16
2195 2196 * 8GB - 100GB one per 512MB
2196 2197 * 100GB - 3TB ~200
2197 2198 * 3TB - 2PB one per 16GB
2198 2199 * > 2PB ~131,072
2199 2200 * --------------------------------
2200 2201 *
2201 2202 * Finally, note that all of the above calculate the initial
2202 2203 * number of metaslabs. Expanding a top-level vdev will result
2203 2204 * in additional metaslabs being allocated making it possible
2204 2205 * to exceed the zfs_vdev_ms_count_limit.
2205 2206 */
2206 2207
2207 2208 if (ms_count < zfs_vdev_min_ms_count)
2208 2209 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2209 2210 else if (ms_count > zfs_vdev_default_ms_count)
2210 2211 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2211 2212 else
2212 2213 ms_shift = zfs_vdev_default_ms_shift;
2213 2214
2214 2215 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2215 2216 ms_shift = SPA_MAXBLOCKSHIFT;
2216 2217 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2217 2218 ms_shift = zfs_vdev_max_ms_shift;
2218 2219 /* cap the total count to constrain memory footprint */
2219 2220 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2220 2221 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2221 2222 }
2222 2223
2223 2224 vd->vdev_ms_shift = ms_shift;
2224 2225 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2225 2226 }
2226 2227
2227 2228 void
2228 2229 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2229 2230 {
2230 2231 ASSERT(vd == vd->vdev_top);
2231 2232 /* indirect vdevs don't have metaslabs or dtls */
2232 2233 ASSERT(vdev_is_concrete(vd) || flags == 0);
2233 2234 ASSERT(ISP2(flags));
2234 2235 ASSERT(spa_writeable(vd->vdev_spa));
2235 2236
2236 2237 if (flags & VDD_METASLAB)
2237 2238 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2238 2239
2239 2240 if (flags & VDD_DTL)
2240 2241 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2241 2242
2242 2243 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2243 2244 }
2244 2245
2245 2246 void
2246 2247 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2247 2248 {
2248 2249 for (int c = 0; c < vd->vdev_children; c++)
2249 2250 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2250 2251
2251 2252 if (vd->vdev_ops->vdev_op_leaf)
2252 2253 vdev_dirty(vd->vdev_top, flags, vd, txg);
2253 2254 }
2254 2255
2255 2256 /*
2256 2257 * DTLs.
2257 2258 *
2258 2259 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2259 2260 * the vdev has less than perfect replication. There are four kinds of DTL:
2260 2261 *
2261 2262 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2262 2263 *
2263 2264 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2264 2265 *
2265 2266 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2266 2267 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2267 2268 * txgs that was scrubbed.
2268 2269 *
2269 2270 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2270 2271 * persistent errors or just some device being offline.
2271 2272 * Unlike the other three, the DTL_OUTAGE map is not generally
2272 2273 * maintained; it's only computed when needed, typically to
2273 2274 * determine whether a device can be detached.
2274 2275 *
2275 2276 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2276 2277 * either has the data or it doesn't.
2277 2278 *
2278 2279 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2279 2280 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2280 2281 * if any child is less than fully replicated, then so is its parent.
2281 2282 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2282 2283 * comprising only those txgs which appear in 'maxfaults' or more children;
2283 2284 * those are the txgs we don't have enough replication to read. For example,
2284 2285 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2285 2286 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2286 2287 * two child DTL_MISSING maps.
2287 2288 *
2288 2289 * It should be clear from the above that to compute the DTLs and outage maps
2289 2290 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2290 2291 * Therefore, that is all we keep on disk. When loading the pool, or after
2291 2292 * a configuration change, we generate all other DTLs from first principles.
2292 2293 */
2293 2294 void
2294 2295 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2295 2296 {
2296 2297 range_tree_t *rt = vd->vdev_dtl[t];
2297 2298
2298 2299 ASSERT(t < DTL_TYPES);
2299 2300 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2300 2301 ASSERT(spa_writeable(vd->vdev_spa));
2301 2302
2302 2303 mutex_enter(&vd->vdev_dtl_lock);
2303 2304 if (!range_tree_contains(rt, txg, size))
2304 2305 range_tree_add(rt, txg, size);
2305 2306 mutex_exit(&vd->vdev_dtl_lock);
2306 2307 }
2307 2308
2308 2309 boolean_t
2309 2310 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2310 2311 {
2311 2312 range_tree_t *rt = vd->vdev_dtl[t];
2312 2313 boolean_t dirty = B_FALSE;
2313 2314
2314 2315 ASSERT(t < DTL_TYPES);
2315 2316 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2316 2317
2317 2318 /*
2318 2319 * While we are loading the pool, the DTLs have not been loaded yet.
2319 2320 * Ignore the DTLs and try all devices. This avoids a recursive
2320 2321 * mutex enter on the vdev_dtl_lock, and also makes us try hard
2321 2322 * when loading the pool (relying on the checksum to ensure that
2322 2323 * we get the right data -- note that we while loading, we are
2323 2324 * only reading the MOS, which is always checksummed).
2324 2325 */
2325 2326 if (vd->vdev_spa->spa_load_state != SPA_LOAD_NONE)
2326 2327 return (B_FALSE);
2327 2328
2328 2329 mutex_enter(&vd->vdev_dtl_lock);
2329 2330 if (!range_tree_is_empty(rt))
2330 2331 dirty = range_tree_contains(rt, txg, size);
2331 2332 mutex_exit(&vd->vdev_dtl_lock);
2332 2333
2333 2334 return (dirty);
2334 2335 }
2335 2336
2336 2337 boolean_t
2337 2338 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
2338 2339 {
2339 2340 range_tree_t *rt = vd->vdev_dtl[t];
2340 2341 boolean_t empty;
2341 2342
2342 2343 mutex_enter(&vd->vdev_dtl_lock);
2343 2344 empty = range_tree_is_empty(rt);
2344 2345 mutex_exit(&vd->vdev_dtl_lock);
2345 2346
2346 2347 return (empty);
2347 2348 }
2348 2349
2349 2350 /*
2350 2351 * Returns the lowest txg in the DTL range.
2351 2352 */
2352 2353 static uint64_t
2353 2354 vdev_dtl_min(vdev_t *vd)
2354 2355 {
2355 2356 range_seg_t *rs;
2356 2357
2357 2358 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2358 2359 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2359 2360 ASSERT0(vd->vdev_children);
2360 2361
2361 2362 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2362 2363 return (rs->rs_start - 1);
2363 2364 }
2364 2365
2365 2366 /*
2366 2367 * Returns the highest txg in the DTL.
2367 2368 */
2368 2369 static uint64_t
2369 2370 vdev_dtl_max(vdev_t *vd)
2370 2371 {
2371 2372 range_seg_t *rs;
2372 2373
2373 2374 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
2374 2375 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
2375 2376 ASSERT0(vd->vdev_children);
2376 2377
2377 2378 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
2378 2379 return (rs->rs_end);
2379 2380 }
2380 2381
2381 2382 /*
2382 2383 * Determine if a resilvering vdev should remove any DTL entries from
2383 2384 * its range. If the vdev was resilvering for the entire duration of the
2384 2385 * scan then it should excise that range from its DTLs. Otherwise, this
2385 2386 * vdev is considered partially resilvered and should leave its DTL
2386 2387 * entries intact. The comment in vdev_dtl_reassess() describes how we
2387 2388 * excise the DTLs.
2388 2389 */
2389 2390 static boolean_t
2390 2391 vdev_dtl_should_excise(vdev_t *vd)
2391 2392 {
2392 2393 spa_t *spa = vd->vdev_spa;
2393 2394 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2394 2395
2395 2396 ASSERT0(scn->scn_phys.scn_errors);
2396 2397 ASSERT0(vd->vdev_children);
2397 2398
2398 2399 if (vd->vdev_state < VDEV_STATE_DEGRADED)
2399 2400 return (B_FALSE);
2400 2401
2401 2402 if (vd->vdev_resilver_txg == 0 ||
2402 2403 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
2403 2404 return (B_TRUE);
2404 2405
2405 2406 /*
2406 2407 * When a resilver is initiated the scan will assign the scn_max_txg
2407 2408 * value to the highest txg value that exists in all DTLs. If this
2408 2409 * device's max DTL is not part of this scan (i.e. it is not in
2409 2410 * the range (scn_min_txg, scn_max_txg] then it is not eligible
2410 2411 * for excision.
2411 2412 */
2412 2413 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
2413 2414 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
2414 2415 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
2415 2416 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
2416 2417 return (B_TRUE);
2417 2418 }
2418 2419 return (B_FALSE);
2419 2420 }
2420 2421
2421 2422 /*
2422 2423 * Reassess DTLs after a config change or scrub completion.
2423 2424 */
2424 2425 void
2425 2426 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
2426 2427 {
2427 2428 spa_t *spa = vd->vdev_spa;
2428 2429 avl_tree_t reftree;
2429 2430 int minref;
2430 2431
2431 2432 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2432 2433
2433 2434 for (int c = 0; c < vd->vdev_children; c++)
2434 2435 vdev_dtl_reassess(vd->vdev_child[c], txg,
2435 2436 scrub_txg, scrub_done);
2436 2437
2437 2438 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
2438 2439 return;
2439 2440
2440 2441 if (vd->vdev_ops->vdev_op_leaf) {
2441 2442 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
2442 2443
2443 2444 mutex_enter(&vd->vdev_dtl_lock);
2444 2445
2445 2446 /*
2446 2447 * If we've completed a scan cleanly then determine
2447 2448 * if this vdev should remove any DTLs. We only want to
2448 2449 * excise regions on vdevs that were available during
2449 2450 * the entire duration of this scan.
2450 2451 */
2451 2452 if (scrub_txg != 0 &&
2452 2453 (spa->spa_scrub_started ||
2453 2454 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
2454 2455 vdev_dtl_should_excise(vd)) {
2455 2456 /*
2456 2457 * We completed a scrub up to scrub_txg. If we
2457 2458 * did it without rebooting, then the scrub dtl
2458 2459 * will be valid, so excise the old region and
2459 2460 * fold in the scrub dtl. Otherwise, leave the
2460 2461 * dtl as-is if there was an error.
2461 2462 *
2462 2463 * There's little trick here: to excise the beginning
2463 2464 * of the DTL_MISSING map, we put it into a reference
2464 2465 * tree and then add a segment with refcnt -1 that
2465 2466 * covers the range [0, scrub_txg). This means
2466 2467 * that each txg in that range has refcnt -1 or 0.
2467 2468 * We then add DTL_SCRUB with a refcnt of 2, so that
2468 2469 * entries in the range [0, scrub_txg) will have a
2469 2470 * positive refcnt -- either 1 or 2. We then convert
2470 2471 * the reference tree into the new DTL_MISSING map.
2471 2472 */
2472 2473 space_reftree_create(&reftree);
2473 2474 space_reftree_add_map(&reftree,
2474 2475 vd->vdev_dtl[DTL_MISSING], 1);
2475 2476 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
2476 2477 space_reftree_add_map(&reftree,
2477 2478 vd->vdev_dtl[DTL_SCRUB], 2);
2478 2479 space_reftree_generate_map(&reftree,
2479 2480 vd->vdev_dtl[DTL_MISSING], 1);
2480 2481 space_reftree_destroy(&reftree);
2481 2482 }
2482 2483 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
2483 2484 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2484 2485 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
2485 2486 if (scrub_done)
2486 2487 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
2487 2488 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
2488 2489 if (!vdev_readable(vd))
2489 2490 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
2490 2491 else
2491 2492 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
2492 2493 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
2493 2494
2494 2495 /*
2495 2496 * If the vdev was resilvering and no longer has any
2496 2497 * DTLs then reset its resilvering flag.
2497 2498 */
2498 2499 if (vd->vdev_resilver_txg != 0 &&
2499 2500 range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2500 2501 range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE]))
2501 2502 vd->vdev_resilver_txg = 0;
2502 2503
2503 2504 mutex_exit(&vd->vdev_dtl_lock);
2504 2505
2505 2506 if (txg != 0)
2506 2507 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2507 2508 return;
2508 2509 }
2509 2510
2510 2511 mutex_enter(&vd->vdev_dtl_lock);
2511 2512 for (int t = 0; t < DTL_TYPES; t++) {
2512 2513 /* account for child's outage in parent's missing map */
2513 2514 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
2514 2515 if (t == DTL_SCRUB)
2515 2516 continue; /* leaf vdevs only */
2516 2517 if (t == DTL_PARTIAL)
2517 2518 minref = 1; /* i.e. non-zero */
2518 2519 else if (vd->vdev_nparity != 0)
2519 2520 minref = vd->vdev_nparity + 1; /* RAID-Z */
2520 2521 else
2521 2522 minref = vd->vdev_children; /* any kind of mirror */
2522 2523 space_reftree_create(&reftree);
2523 2524 for (int c = 0; c < vd->vdev_children; c++) {
2524 2525 vdev_t *cvd = vd->vdev_child[c];
2525 2526 mutex_enter(&cvd->vdev_dtl_lock);
2526 2527 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
2527 2528 mutex_exit(&cvd->vdev_dtl_lock);
2528 2529 }
2529 2530 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
2530 2531 space_reftree_destroy(&reftree);
2531 2532 }
2532 2533 mutex_exit(&vd->vdev_dtl_lock);
2533 2534 }
2534 2535
2535 2536 int
2536 2537 vdev_dtl_load(vdev_t *vd)
2537 2538 {
2538 2539 spa_t *spa = vd->vdev_spa;
2539 2540 objset_t *mos = spa->spa_meta_objset;
2540 2541 int error = 0;
2541 2542
|
↓ open down ↓ |
1265 lines elided |
↑ open up ↑ |
2542 2543 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
2543 2544 ASSERT(vdev_is_concrete(vd));
2544 2545
2545 2546 error = space_map_open(&vd->vdev_dtl_sm, mos,
2546 2547 vd->vdev_dtl_object, 0, -1ULL, 0);
2547 2548 if (error)
2548 2549 return (error);
2549 2550 ASSERT(vd->vdev_dtl_sm != NULL);
2550 2551
2551 2552 mutex_enter(&vd->vdev_dtl_lock);
2552 -
2553 - /*
2554 - * Now that we've opened the space_map we need to update
2555 - * the in-core DTL.
2556 - */
2557 - space_map_update(vd->vdev_dtl_sm);
2558 -
2559 2553 error = space_map_load(vd->vdev_dtl_sm,
2560 2554 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
2561 2555 mutex_exit(&vd->vdev_dtl_lock);
2562 2556
2563 2557 return (error);
2564 2558 }
2565 2559
2566 2560 for (int c = 0; c < vd->vdev_children; c++) {
2567 2561 error = vdev_dtl_load(vd->vdev_child[c]);
2568 2562 if (error != 0)
2569 2563 break;
2570 2564 }
2571 2565
2572 2566 return (error);
2573 2567 }
2574 2568
2575 2569 static void
2576 2570 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
2577 2571 {
2578 2572 spa_t *spa = vd->vdev_spa;
2579 2573 objset_t *mos = spa->spa_meta_objset;
2580 2574 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
2581 2575 const char *string;
2582 2576
2583 2577 ASSERT(alloc_bias != VDEV_BIAS_NONE);
2584 2578
2585 2579 string =
2586 2580 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
2587 2581 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
2588 2582 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
2589 2583
2590 2584 ASSERT(string != NULL);
2591 2585 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
2592 2586 1, strlen(string) + 1, string, tx));
2593 2587
2594 2588 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
2595 2589 spa_activate_allocation_classes(spa, tx);
2596 2590 }
2597 2591 }
2598 2592
2599 2593 void
2600 2594 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
2601 2595 {
2602 2596 spa_t *spa = vd->vdev_spa;
2603 2597
2604 2598 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
2605 2599 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2606 2600 zapobj, tx));
2607 2601 }
2608 2602
2609 2603 uint64_t
2610 2604 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
2611 2605 {
2612 2606 spa_t *spa = vd->vdev_spa;
2613 2607 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
2614 2608 DMU_OT_NONE, 0, tx);
2615 2609
2616 2610 ASSERT(zap != 0);
2617 2611 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
2618 2612 zap, tx));
2619 2613
2620 2614 return (zap);
2621 2615 }
2622 2616
2623 2617 void
2624 2618 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
2625 2619 {
2626 2620 if (vd->vdev_ops != &vdev_hole_ops &&
2627 2621 vd->vdev_ops != &vdev_missing_ops &&
2628 2622 vd->vdev_ops != &vdev_root_ops &&
2629 2623 !vd->vdev_top->vdev_removing) {
2630 2624 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
2631 2625 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
2632 2626 }
2633 2627 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
2634 2628 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
2635 2629 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
2636 2630 vdev_zap_allocation_data(vd, tx);
2637 2631 }
2638 2632 }
2639 2633
2640 2634 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2641 2635 vdev_construct_zaps(vd->vdev_child[i], tx);
2642 2636 }
2643 2637 }
2644 2638
2645 2639 void
2646 2640 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2647 2641 {
2648 2642 spa_t *spa = vd->vdev_spa;
2649 2643 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2650 2644 objset_t *mos = spa->spa_meta_objset;
2651 2645 range_tree_t *rtsync;
2652 2646 dmu_tx_t *tx;
2653 2647 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2654 2648
2655 2649 ASSERT(vdev_is_concrete(vd));
2656 2650 ASSERT(vd->vdev_ops->vdev_op_leaf);
2657 2651
2658 2652 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2659 2653
2660 2654 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2661 2655 mutex_enter(&vd->vdev_dtl_lock);
2662 2656 space_map_free(vd->vdev_dtl_sm, tx);
2663 2657 space_map_close(vd->vdev_dtl_sm);
2664 2658 vd->vdev_dtl_sm = NULL;
2665 2659 mutex_exit(&vd->vdev_dtl_lock);
2666 2660
2667 2661 /*
2668 2662 * We only destroy the leaf ZAP for detached leaves or for
2669 2663 * removed log devices. Removed data devices handle leaf ZAP
2670 2664 * cleanup later, once cancellation is no longer possible.
2671 2665 */
2672 2666 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2673 2667 vd->vdev_top->vdev_islog)) {
2674 2668 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2675 2669 vd->vdev_leaf_zap = 0;
2676 2670 }
2677 2671
2678 2672 dmu_tx_commit(tx);
2679 2673 return;
2680 2674 }
2681 2675
2682 2676 if (vd->vdev_dtl_sm == NULL) {
2683 2677 uint64_t new_object;
2684 2678
2685 2679 new_object = space_map_alloc(mos, vdev_dtl_sm_blksz, tx);
2686 2680 VERIFY3U(new_object, !=, 0);
2687 2681
2688 2682 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2689 2683 0, -1ULL, 0));
2690 2684 ASSERT(vd->vdev_dtl_sm != NULL);
2691 2685 }
2692 2686
2693 2687 rtsync = range_tree_create(NULL, NULL);
2694 2688
2695 2689 mutex_enter(&vd->vdev_dtl_lock);
2696 2690 range_tree_walk(rt, range_tree_add, rtsync);
2697 2691 mutex_exit(&vd->vdev_dtl_lock);
2698 2692
2699 2693 space_map_truncate(vd->vdev_dtl_sm, vdev_dtl_sm_blksz, tx);
2700 2694 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
2701 2695 range_tree_vacate(rtsync, NULL, NULL);
2702 2696
2703 2697 range_tree_destroy(rtsync);
2704 2698
2705 2699 /*
2706 2700 * If the object for the space map has changed then dirty
2707 2701 * the top level so that we update the config.
|
↓ open down ↓ |
139 lines elided |
↑ open up ↑ |
2708 2702 */
2709 2703 if (object != space_map_object(vd->vdev_dtl_sm)) {
2710 2704 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
2711 2705 "new object %llu", (u_longlong_t)txg, spa_name(spa),
2712 2706 (u_longlong_t)object,
2713 2707 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
2714 2708 vdev_config_dirty(vd->vdev_top);
2715 2709 }
2716 2710
2717 2711 dmu_tx_commit(tx);
2718 -
2719 - mutex_enter(&vd->vdev_dtl_lock);
2720 - space_map_update(vd->vdev_dtl_sm);
2721 - mutex_exit(&vd->vdev_dtl_lock);
2722 2712 }
2723 2713
2724 2714 /*
2725 2715 * Determine whether the specified vdev can be offlined/detached/removed
2726 2716 * without losing data.
2727 2717 */
2728 2718 boolean_t
2729 2719 vdev_dtl_required(vdev_t *vd)
2730 2720 {
2731 2721 spa_t *spa = vd->vdev_spa;
2732 2722 vdev_t *tvd = vd->vdev_top;
2733 2723 uint8_t cant_read = vd->vdev_cant_read;
2734 2724 boolean_t required;
2735 2725
2736 2726 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2737 2727
2738 2728 if (vd == spa->spa_root_vdev || vd == tvd)
2739 2729 return (B_TRUE);
2740 2730
2741 2731 /*
2742 2732 * Temporarily mark the device as unreadable, and then determine
2743 2733 * whether this results in any DTL outages in the top-level vdev.
2744 2734 * If not, we can safely offline/detach/remove the device.
2745 2735 */
2746 2736 vd->vdev_cant_read = B_TRUE;
2747 2737 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2748 2738 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2749 2739 vd->vdev_cant_read = cant_read;
2750 2740 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2751 2741
2752 2742 if (!required && zio_injection_enabled)
2753 2743 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2754 2744
2755 2745 return (required);
2756 2746 }
2757 2747
2758 2748 /*
2759 2749 * Determine if resilver is needed, and if so the txg range.
2760 2750 */
2761 2751 boolean_t
2762 2752 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2763 2753 {
2764 2754 boolean_t needed = B_FALSE;
2765 2755 uint64_t thismin = UINT64_MAX;
2766 2756 uint64_t thismax = 0;
2767 2757
2768 2758 if (vd->vdev_children == 0) {
2769 2759 mutex_enter(&vd->vdev_dtl_lock);
2770 2760 if (!range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
2771 2761 vdev_writeable(vd)) {
2772 2762
2773 2763 thismin = vdev_dtl_min(vd);
2774 2764 thismax = vdev_dtl_max(vd);
2775 2765 needed = B_TRUE;
2776 2766 }
2777 2767 mutex_exit(&vd->vdev_dtl_lock);
2778 2768 } else {
2779 2769 for (int c = 0; c < vd->vdev_children; c++) {
2780 2770 vdev_t *cvd = vd->vdev_child[c];
2781 2771 uint64_t cmin, cmax;
2782 2772
2783 2773 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2784 2774 thismin = MIN(thismin, cmin);
2785 2775 thismax = MAX(thismax, cmax);
2786 2776 needed = B_TRUE;
2787 2777 }
2788 2778 }
2789 2779 }
2790 2780
2791 2781 if (needed && minp) {
2792 2782 *minp = thismin;
2793 2783 *maxp = thismax;
2794 2784 }
2795 2785 return (needed);
2796 2786 }
2797 2787
2798 2788 /*
2799 2789 * Gets the checkpoint space map object from the vdev's ZAP.
2800 2790 * Returns the spacemap object, or 0 if it wasn't in the ZAP
2801 2791 * or the ZAP doesn't exist yet.
2802 2792 */
2803 2793 int
2804 2794 vdev_checkpoint_sm_object(vdev_t *vd)
2805 2795 {
2806 2796 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
2807 2797 if (vd->vdev_top_zap == 0) {
2808 2798 return (0);
2809 2799 }
2810 2800
2811 2801 uint64_t sm_obj = 0;
2812 2802 int err = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
2813 2803 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, &sm_obj);
2814 2804
2815 2805 ASSERT(err == 0 || err == ENOENT);
2816 2806
2817 2807 return (sm_obj);
2818 2808 }
2819 2809
2820 2810 int
2821 2811 vdev_load(vdev_t *vd)
2822 2812 {
2823 2813 int error = 0;
2824 2814 /*
2825 2815 * Recursively load all children.
2826 2816 */
2827 2817 for (int c = 0; c < vd->vdev_children; c++) {
2828 2818 error = vdev_load(vd->vdev_child[c]);
2829 2819 if (error != 0) {
2830 2820 return (error);
2831 2821 }
2832 2822 }
2833 2823
2834 2824 vdev_set_deflate_ratio(vd);
2835 2825
2836 2826 /*
2837 2827 * On spa_load path, grab the allocation bias from our zap
2838 2828 */
2839 2829 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
2840 2830 spa_t *spa = vd->vdev_spa;
2841 2831 char bias_str[64];
2842 2832
2843 2833 if (zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
2844 2834 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
2845 2835 bias_str) == 0) {
2846 2836 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
2847 2837 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
2848 2838 }
2849 2839 }
2850 2840
2851 2841 /*
2852 2842 * If this is a top-level vdev, initialize its metaslabs.
2853 2843 */
|
↓ open down ↓ |
122 lines elided |
↑ open up ↑ |
2854 2844 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
2855 2845 vdev_metaslab_group_create(vd);
2856 2846
2857 2847 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
2858 2848 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2859 2849 VDEV_AUX_CORRUPT_DATA);
2860 2850 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
2861 2851 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
2862 2852 (u_longlong_t)vd->vdev_asize);
2863 2853 return (SET_ERROR(ENXIO));
2864 - } else if ((error = vdev_metaslab_init(vd, 0)) != 0) {
2854 + }
2855 +
2856 + error = vdev_metaslab_init(vd, 0);
2857 + if (error != 0) {
2865 2858 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
2866 2859 "[error=%d]", error);
2867 2860 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2868 2861 VDEV_AUX_CORRUPT_DATA);
2869 2862 return (error);
2870 2863 }
2871 2864
2872 2865 uint64_t checkpoint_sm_obj = vdev_checkpoint_sm_object(vd);
2873 2866 if (checkpoint_sm_obj != 0) {
2874 2867 objset_t *mos = spa_meta_objset(vd->vdev_spa);
2875 2868 ASSERT(vd->vdev_asize != 0);
2876 2869 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
2877 2870
2878 - if ((error = space_map_open(&vd->vdev_checkpoint_sm,
2871 + error = space_map_open(&vd->vdev_checkpoint_sm,
2879 2872 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
2880 - vd->vdev_ashift))) {
2873 + vd->vdev_ashift);
2874 + if (error != 0) {
2881 2875 vdev_dbgmsg(vd, "vdev_load: space_map_open "
2882 2876 "failed for checkpoint spacemap (obj %llu) "
2883 2877 "[error=%d]",
2884 2878 (u_longlong_t)checkpoint_sm_obj, error);
2885 2879 return (error);
2886 2880 }
2887 2881 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
2888 - space_map_update(vd->vdev_checkpoint_sm);
2889 2882
2890 2883 /*
2891 2884 * Since the checkpoint_sm contains free entries
2892 - * exclusively we can use sm_alloc to indicate the
2893 - * culmulative checkpointed space that has been freed.
2885 + * exclusively we can use space_map_allocated() to
2886 + * indicate the cumulative checkpointed space that
2887 + * has been freed.
2894 2888 */
2895 2889 vd->vdev_stat.vs_checkpoint_space =
2896 - -vd->vdev_checkpoint_sm->sm_alloc;
2890 + -space_map_allocated(vd->vdev_checkpoint_sm);
2897 2891 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
2898 2892 vd->vdev_stat.vs_checkpoint_space;
2899 2893 }
2900 2894 }
2901 2895
2902 2896 /*
2903 2897 * If this is a leaf vdev, load its DTL.
2904 2898 */
2905 2899 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
2906 2900 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2907 2901 VDEV_AUX_CORRUPT_DATA);
2908 2902 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
2909 2903 "[error=%d]", error);
2910 2904 return (error);
2911 2905 }
2912 2906
2913 2907 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
2914 2908 if (obsolete_sm_object != 0) {
2915 2909 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2916 2910 ASSERT(vd->vdev_asize != 0);
2917 2911 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
|
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
2918 2912
2919 2913 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
2920 2914 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
2921 2915 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2922 2916 VDEV_AUX_CORRUPT_DATA);
2923 2917 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
2924 2918 "obsolete spacemap (obj %llu) [error=%d]",
2925 2919 (u_longlong_t)obsolete_sm_object, error);
2926 2920 return (error);
2927 2921 }
2928 - space_map_update(vd->vdev_obsolete_sm);
2929 2922 }
2930 2923
2931 2924 return (0);
2932 2925 }
2933 2926
2934 2927 /*
2935 2928 * The special vdev case is used for hot spares and l2cache devices. Its
2936 2929 * sole purpose it to set the vdev state for the associated vdev. To do this,
2937 2930 * we make sure that we can open the underlying device, then try to read the
2938 2931 * label, and make sure that the label is sane and that it hasn't been
2939 2932 * repurposed to another pool.
2940 2933 */
2941 2934 int
2942 2935 vdev_validate_aux(vdev_t *vd)
2943 2936 {
2944 2937 nvlist_t *label;
2945 2938 uint64_t guid, version;
2946 2939 uint64_t state;
2947 2940
2948 2941 if (!vdev_readable(vd))
2949 2942 return (0);
2950 2943
2951 2944 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2952 2945 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2953 2946 VDEV_AUX_CORRUPT_DATA);
2954 2947 return (-1);
2955 2948 }
2956 2949
2957 2950 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2958 2951 !SPA_VERSION_IS_SUPPORTED(version) ||
2959 2952 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2960 2953 guid != vd->vdev_guid ||
2961 2954 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2962 2955 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2963 2956 VDEV_AUX_CORRUPT_DATA);
2964 2957 nvlist_free(label);
2965 2958 return (-1);
2966 2959 }
2967 2960
2968 2961 /*
2969 2962 * We don't actually check the pool state here. If it's in fact in
2970 2963 * use by another pool, we update this fact on the fly when requested.
2971 2964 */
2972 2965 nvlist_free(label);
2973 2966 return (0);
2974 2967 }
2975 2968
2976 2969 /*
2977 2970 * Free the objects used to store this vdev's spacemaps, and the array
2978 2971 * that points to them.
2979 2972 */
2980 2973 void
2981 2974 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
2982 2975 {
2983 2976 if (vd->vdev_ms_array == 0)
2984 2977 return;
2985 2978
2986 2979 objset_t *mos = vd->vdev_spa->spa_meta_objset;
2987 2980 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
2988 2981 size_t array_bytes = array_count * sizeof (uint64_t);
2989 2982 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
2990 2983 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
2991 2984 array_bytes, smobj_array, 0));
2992 2985
2993 2986 for (uint64_t i = 0; i < array_count; i++) {
2994 2987 uint64_t smobj = smobj_array[i];
2995 2988 if (smobj == 0)
2996 2989 continue;
2997 2990
2998 2991 space_map_free_obj(mos, smobj, tx);
2999 2992 }
3000 2993
3001 2994 kmem_free(smobj_array, array_bytes);
3002 2995 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
3003 2996 vd->vdev_ms_array = 0;
3004 2997 }
|
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
3005 2998
3006 2999 static void
3007 3000 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
3008 3001 {
3009 3002 spa_t *spa = vd->vdev_spa;
3010 3003
3011 3004 ASSERT(vd->vdev_islog);
3012 3005 ASSERT(vd == vd->vdev_top);
3013 3006 ASSERT3U(txg, ==, spa_syncing_txg(spa));
3014 3007
3015 - if (vd->vdev_ms != NULL) {
3016 - metaslab_group_t *mg = vd->vdev_mg;
3017 -
3018 - metaslab_group_histogram_verify(mg);
3019 - metaslab_class_histogram_verify(mg->mg_class);
3020 -
3021 - for (int m = 0; m < vd->vdev_ms_count; m++) {
3022 - metaslab_t *msp = vd->vdev_ms[m];
3023 -
3024 - if (msp == NULL || msp->ms_sm == NULL)
3025 - continue;
3026 -
3027 - mutex_enter(&msp->ms_lock);
3028 - /*
3029 - * If the metaslab was not loaded when the vdev
3030 - * was removed then the histogram accounting may
3031 - * not be accurate. Update the histogram information
3032 - * here so that we ensure that the metaslab group
3033 - * and metaslab class are up-to-date.
3034 - */
3035 - metaslab_group_histogram_remove(mg, msp);
3036 -
3037 - VERIFY0(space_map_allocated(msp->ms_sm));
3038 - space_map_close(msp->ms_sm);
3039 - msp->ms_sm = NULL;
3040 - mutex_exit(&msp->ms_lock);
3041 - }
3042 -
3043 - if (vd->vdev_checkpoint_sm != NULL) {
3044 - ASSERT(spa_has_checkpoint(spa));
3045 - space_map_close(vd->vdev_checkpoint_sm);
3046 - vd->vdev_checkpoint_sm = NULL;
3047 - }
3048 -
3049 - metaslab_group_histogram_verify(mg);
3050 - metaslab_class_histogram_verify(mg->mg_class);
3051 -
3052 - for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
3053 - ASSERT0(mg->mg_histogram[i]);
3054 - }
3055 -
3056 3008 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
3057 3009
3058 3010 vdev_destroy_spacemaps(vd, tx);
3059 3011 if (vd->vdev_top_zap != 0) {
3060 3012 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
3061 3013 vd->vdev_top_zap = 0;
3062 3014 }
3063 3015
3064 3016 dmu_tx_commit(tx);
3065 3017 }
3066 3018
3067 3019 void
3068 3020 vdev_sync_done(vdev_t *vd, uint64_t txg)
3069 3021 {
3070 3022 metaslab_t *msp;
3071 3023 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
3072 3024
3073 3025 ASSERT(vdev_is_concrete(vd));
3074 3026
3075 3027 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
3076 3028 != NULL)
3077 3029 metaslab_sync_done(msp, txg);
3078 3030
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
3079 3031 if (reassess)
3080 3032 metaslab_sync_reassess(vd->vdev_mg);
3081 3033 }
3082 3034
3083 3035 void
3084 3036 vdev_sync(vdev_t *vd, uint64_t txg)
3085 3037 {
3086 3038 spa_t *spa = vd->vdev_spa;
3087 3039 vdev_t *lvd;
3088 3040 metaslab_t *msp;
3089 - dmu_tx_t *tx;
3090 3041
3042 + ASSERT3U(txg, ==, spa->spa_syncing_txg);
3043 + dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3091 3044 if (range_tree_space(vd->vdev_obsolete_segments) > 0) {
3092 - dmu_tx_t *tx;
3093 -
3094 3045 ASSERT(vd->vdev_removing ||
3095 3046 vd->vdev_ops == &vdev_indirect_ops);
3096 3047
3097 - tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3098 3048 vdev_indirect_sync_obsolete(vd, tx);
3099 - dmu_tx_commit(tx);
3100 3049
3101 3050 /*
3102 3051 * If the vdev is indirect, it can't have dirty
3103 3052 * metaslabs or DTLs.
3104 3053 */
3105 3054 if (vd->vdev_ops == &vdev_indirect_ops) {
3106 3055 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
3107 3056 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
3057 + dmu_tx_commit(tx);
3108 3058 return;
3109 3059 }
3110 3060 }
3111 3061
3112 3062 ASSERT(vdev_is_concrete(vd));
3113 3063
3114 3064 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
3115 3065 !vd->vdev_removing) {
3116 3066 ASSERT(vd == vd->vdev_top);
3117 3067 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
3118 - tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3119 3068 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
3120 3069 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
3121 3070 ASSERT(vd->vdev_ms_array != 0);
3122 3071 vdev_config_dirty(vd);
3123 - dmu_tx_commit(tx);
3124 3072 }
3125 3073
3126 3074 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
3127 3075 metaslab_sync(msp, txg);
3128 3076 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
3129 3077 }
3130 3078
3131 3079 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
3132 3080 vdev_dtl_sync(lvd, txg);
3133 3081
3134 3082 /*
3135 3083 * If this is an empty log device being removed, destroy the
3136 3084 * metadata associated with it.
3137 3085 */
3138 3086 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
3139 3087 vdev_remove_empty_log(vd, txg);
3140 3088
3141 3089 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
3090 + dmu_tx_commit(tx);
3142 3091 }
3143 3092
3144 3093 uint64_t
3145 3094 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
3146 3095 {
3147 3096 return (vd->vdev_ops->vdev_op_asize(vd, psize));
3148 3097 }
3149 3098
3150 3099 /*
3151 3100 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
3152 3101 * not be opened, and no I/O is attempted.
3153 3102 */
3154 3103 int
3155 3104 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3156 3105 {
3157 3106 vdev_t *vd, *tvd;
3158 3107
3159 3108 spa_vdev_state_enter(spa, SCL_NONE);
3160 3109
3161 3110 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3162 3111 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3163 3112
3164 3113 if (!vd->vdev_ops->vdev_op_leaf)
3165 3114 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3166 3115
3167 3116 tvd = vd->vdev_top;
3168 3117
3169 3118 /*
3170 3119 * We don't directly use the aux state here, but if we do a
3171 3120 * vdev_reopen(), we need this value to be present to remember why we
3172 3121 * were faulted.
3173 3122 */
3174 3123 vd->vdev_label_aux = aux;
3175 3124
3176 3125 /*
3177 3126 * Faulted state takes precedence over degraded.
3178 3127 */
3179 3128 vd->vdev_delayed_close = B_FALSE;
3180 3129 vd->vdev_faulted = 1ULL;
3181 3130 vd->vdev_degraded = 0ULL;
3182 3131 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
3183 3132
3184 3133 /*
3185 3134 * If this device has the only valid copy of the data, then
3186 3135 * back off and simply mark the vdev as degraded instead.
3187 3136 */
3188 3137 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
3189 3138 vd->vdev_degraded = 1ULL;
3190 3139 vd->vdev_faulted = 0ULL;
3191 3140
3192 3141 /*
3193 3142 * If we reopen the device and it's not dead, only then do we
3194 3143 * mark it degraded.
3195 3144 */
3196 3145 vdev_reopen(tvd);
3197 3146
3198 3147 if (vdev_readable(vd))
3199 3148 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
3200 3149 }
3201 3150
3202 3151 return (spa_vdev_state_exit(spa, vd, 0));
3203 3152 }
3204 3153
3205 3154 /*
3206 3155 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
3207 3156 * user that something is wrong. The vdev continues to operate as normal as far
3208 3157 * as I/O is concerned.
3209 3158 */
3210 3159 int
3211 3160 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
3212 3161 {
3213 3162 vdev_t *vd;
3214 3163
3215 3164 spa_vdev_state_enter(spa, SCL_NONE);
3216 3165
3217 3166 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3218 3167 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3219 3168
3220 3169 if (!vd->vdev_ops->vdev_op_leaf)
3221 3170 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3222 3171
3223 3172 /*
3224 3173 * If the vdev is already faulted, then don't do anything.
3225 3174 */
3226 3175 if (vd->vdev_faulted || vd->vdev_degraded)
3227 3176 return (spa_vdev_state_exit(spa, NULL, 0));
3228 3177
3229 3178 vd->vdev_degraded = 1ULL;
3230 3179 if (!vdev_is_dead(vd))
3231 3180 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
3232 3181 aux);
3233 3182
3234 3183 return (spa_vdev_state_exit(spa, vd, 0));
3235 3184 }
3236 3185
3237 3186 /*
3238 3187 * Online the given vdev.
3239 3188 *
3240 3189 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
3241 3190 * spare device should be detached when the device finishes resilvering.
3242 3191 * Second, the online should be treated like a 'test' online case, so no FMA
3243 3192 * events are generated if the device fails to open.
3244 3193 */
3245 3194 int
3246 3195 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
3247 3196 {
3248 3197 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
3249 3198 boolean_t wasoffline;
3250 3199 vdev_state_t oldstate;
3251 3200
3252 3201 spa_vdev_state_enter(spa, SCL_NONE);
3253 3202
3254 3203 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3255 3204 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3256 3205
3257 3206 if (!vd->vdev_ops->vdev_op_leaf)
3258 3207 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3259 3208
3260 3209 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
3261 3210 oldstate = vd->vdev_state;
3262 3211
3263 3212 tvd = vd->vdev_top;
3264 3213 vd->vdev_offline = B_FALSE;
3265 3214 vd->vdev_tmpoffline = B_FALSE;
3266 3215 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
3267 3216 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
3268 3217
3269 3218 /* XXX - L2ARC 1.0 does not support expansion */
3270 3219 if (!vd->vdev_aux) {
3271 3220 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3272 3221 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
3273 3222 }
3274 3223
3275 3224 vdev_reopen(tvd);
3276 3225 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
3277 3226
3278 3227 if (!vd->vdev_aux) {
3279 3228 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3280 3229 pvd->vdev_expanding = B_FALSE;
3281 3230 }
3282 3231
3283 3232 if (newstate)
3284 3233 *newstate = vd->vdev_state;
3285 3234 if ((flags & ZFS_ONLINE_UNSPARE) &&
3286 3235 !vdev_is_dead(vd) && vd->vdev_parent &&
3287 3236 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3288 3237 vd->vdev_parent->vdev_child[0] == vd)
3289 3238 vd->vdev_unspare = B_TRUE;
3290 3239
3291 3240 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
3292 3241
3293 3242 /* XXX - L2ARC 1.0 does not support expansion */
3294 3243 if (vd->vdev_aux)
3295 3244 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
3296 3245 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3297 3246 }
3298 3247
3299 3248 /* Restart initializing if necessary */
3300 3249 mutex_enter(&vd->vdev_initialize_lock);
3301 3250 if (vdev_writeable(vd) &&
3302 3251 vd->vdev_initialize_thread == NULL &&
3303 3252 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
3304 3253 (void) vdev_initialize(vd);
3305 3254 }
3306 3255 mutex_exit(&vd->vdev_initialize_lock);
3307 3256
3308 3257 if (wasoffline ||
3309 3258 (oldstate < VDEV_STATE_DEGRADED &&
3310 3259 vd->vdev_state >= VDEV_STATE_DEGRADED))
3311 3260 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
3312 3261
3313 3262 return (spa_vdev_state_exit(spa, vd, 0));
3314 3263 }
3315 3264
3316 3265 static int
3317 3266 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
3318 3267 {
3319 3268 vdev_t *vd, *tvd;
3320 3269 int error = 0;
3321 3270 uint64_t generation;
3322 3271 metaslab_group_t *mg;
3323 3272
3324 3273 top:
3325 3274 spa_vdev_state_enter(spa, SCL_ALLOC);
3326 3275
3327 3276 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
3328 3277 return (spa_vdev_state_exit(spa, NULL, ENODEV));
3329 3278
3330 3279 if (!vd->vdev_ops->vdev_op_leaf)
3331 3280 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
3332 3281
3333 3282 tvd = vd->vdev_top;
3334 3283 mg = tvd->vdev_mg;
3335 3284 generation = spa->spa_config_generation + 1;
3336 3285
3337 3286 /*
3338 3287 * If the device isn't already offline, try to offline it.
3339 3288 */
3340 3289 if (!vd->vdev_offline) {
3341 3290 /*
3342 3291 * If this device has the only valid copy of some data,
3343 3292 * don't allow it to be offlined. Log devices are always
3344 3293 * expendable.
3345 3294 */
3346 3295 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3347 3296 vdev_dtl_required(vd))
3348 3297 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3349 3298
3350 3299 /*
3351 3300 * If the top-level is a slog and it has had allocations
3352 3301 * then proceed. We check that the vdev's metaslab group
3353 3302 * is not NULL since it's possible that we may have just
3354 3303 * added this vdev but not yet initialized its metaslabs.
3355 3304 */
3356 3305 if (tvd->vdev_islog && mg != NULL) {
3357 3306 /*
3358 3307 * Prevent any future allocations.
3359 3308 */
3360 3309 metaslab_group_passivate(mg);
|
↓ open down ↓ |
209 lines elided |
↑ open up ↑ |
3361 3310 (void) spa_vdev_state_exit(spa, vd, 0);
3362 3311
3363 3312 error = spa_reset_logs(spa);
3364 3313
3365 3314 /*
3366 3315 * If the log device was successfully reset but has
3367 3316 * checkpointed data, do not offline it.
3368 3317 */
3369 3318 if (error == 0 &&
3370 3319 tvd->vdev_checkpoint_sm != NULL) {
3371 - ASSERT3U(tvd->vdev_checkpoint_sm->sm_alloc,
3372 - !=, 0);
3373 3320 error = ZFS_ERR_CHECKPOINT_EXISTS;
3374 3321 }
3375 3322
3376 3323 spa_vdev_state_enter(spa, SCL_ALLOC);
3377 3324
3378 3325 /*
3379 3326 * Check to see if the config has changed.
3380 3327 */
3381 3328 if (error || generation != spa->spa_config_generation) {
3382 3329 metaslab_group_activate(mg);
3383 3330 if (error)
3384 3331 return (spa_vdev_state_exit(spa,
3385 3332 vd, error));
3386 3333 (void) spa_vdev_state_exit(spa, vd, 0);
3387 3334 goto top;
3388 3335 }
3389 3336 ASSERT0(tvd->vdev_stat.vs_alloc);
3390 3337 }
3391 3338
3392 3339 /*
3393 3340 * Offline this device and reopen its top-level vdev.
3394 3341 * If the top-level vdev is a log device then just offline
3395 3342 * it. Otherwise, if this action results in the top-level
3396 3343 * vdev becoming unusable, undo it and fail the request.
3397 3344 */
3398 3345 vd->vdev_offline = B_TRUE;
3399 3346 vdev_reopen(tvd);
3400 3347
3401 3348 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
3402 3349 vdev_is_dead(tvd)) {
3403 3350 vd->vdev_offline = B_FALSE;
3404 3351 vdev_reopen(tvd);
3405 3352 return (spa_vdev_state_exit(spa, NULL, EBUSY));
3406 3353 }
3407 3354
3408 3355 /*
3409 3356 * Add the device back into the metaslab rotor so that
3410 3357 * once we online the device it's open for business.
3411 3358 */
3412 3359 if (tvd->vdev_islog && mg != NULL)
3413 3360 metaslab_group_activate(mg);
3414 3361 }
3415 3362
3416 3363 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
3417 3364
3418 3365 return (spa_vdev_state_exit(spa, vd, 0));
3419 3366 }
3420 3367
3421 3368 int
3422 3369 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
3423 3370 {
3424 3371 int error;
3425 3372
3426 3373 mutex_enter(&spa->spa_vdev_top_lock);
3427 3374 error = vdev_offline_locked(spa, guid, flags);
3428 3375 mutex_exit(&spa->spa_vdev_top_lock);
3429 3376
3430 3377 return (error);
3431 3378 }
3432 3379
3433 3380 /*
3434 3381 * Clear the error counts associated with this vdev. Unlike vdev_online() and
3435 3382 * vdev_offline(), we assume the spa config is locked. We also clear all
3436 3383 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
3437 3384 */
3438 3385 void
3439 3386 vdev_clear(spa_t *spa, vdev_t *vd)
3440 3387 {
3441 3388 vdev_t *rvd = spa->spa_root_vdev;
3442 3389
3443 3390 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3444 3391
3445 3392 if (vd == NULL)
3446 3393 vd = rvd;
3447 3394
3448 3395 vd->vdev_stat.vs_read_errors = 0;
3449 3396 vd->vdev_stat.vs_write_errors = 0;
3450 3397 vd->vdev_stat.vs_checksum_errors = 0;
3451 3398
3452 3399 for (int c = 0; c < vd->vdev_children; c++)
3453 3400 vdev_clear(spa, vd->vdev_child[c]);
3454 3401
3455 3402 /*
3456 3403 * It makes no sense to "clear" an indirect vdev.
3457 3404 */
3458 3405 if (!vdev_is_concrete(vd))
3459 3406 return;
3460 3407
3461 3408 /*
3462 3409 * If we're in the FAULTED state or have experienced failed I/O, then
3463 3410 * clear the persistent state and attempt to reopen the device. We
3464 3411 * also mark the vdev config dirty, so that the new faulted state is
3465 3412 * written out to disk.
3466 3413 */
3467 3414 if (vd->vdev_faulted || vd->vdev_degraded ||
3468 3415 !vdev_readable(vd) || !vdev_writeable(vd)) {
3469 3416
3470 3417 /*
3471 3418 * When reopening in reponse to a clear event, it may be due to
3472 3419 * a fmadm repair request. In this case, if the device is
3473 3420 * still broken, we want to still post the ereport again.
3474 3421 */
3475 3422 vd->vdev_forcefault = B_TRUE;
3476 3423
3477 3424 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
3478 3425 vd->vdev_cant_read = B_FALSE;
3479 3426 vd->vdev_cant_write = B_FALSE;
3480 3427
3481 3428 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
3482 3429
3483 3430 vd->vdev_forcefault = B_FALSE;
3484 3431
3485 3432 if (vd != rvd && vdev_writeable(vd->vdev_top))
3486 3433 vdev_state_dirty(vd->vdev_top);
3487 3434
3488 3435 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
3489 3436 spa_async_request(spa, SPA_ASYNC_RESILVER);
3490 3437
3491 3438 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
3492 3439 }
3493 3440
3494 3441 /*
3495 3442 * When clearing a FMA-diagnosed fault, we always want to
3496 3443 * unspare the device, as we assume that the original spare was
3497 3444 * done in response to the FMA fault.
3498 3445 */
3499 3446 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
3500 3447 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3501 3448 vd->vdev_parent->vdev_child[0] == vd)
3502 3449 vd->vdev_unspare = B_TRUE;
3503 3450 }
3504 3451
3505 3452 boolean_t
3506 3453 vdev_is_dead(vdev_t *vd)
3507 3454 {
3508 3455 /*
3509 3456 * Holes and missing devices are always considered "dead".
3510 3457 * This simplifies the code since we don't have to check for
3511 3458 * these types of devices in the various code paths.
3512 3459 * Instead we rely on the fact that we skip over dead devices
3513 3460 * before issuing I/O to them.
3514 3461 */
3515 3462 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
3516 3463 vd->vdev_ops == &vdev_hole_ops ||
3517 3464 vd->vdev_ops == &vdev_missing_ops);
3518 3465 }
3519 3466
3520 3467 boolean_t
3521 3468 vdev_readable(vdev_t *vd)
3522 3469 {
3523 3470 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
3524 3471 }
3525 3472
3526 3473 boolean_t
3527 3474 vdev_writeable(vdev_t *vd)
3528 3475 {
3529 3476 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
3530 3477 vdev_is_concrete(vd));
3531 3478 }
3532 3479
3533 3480 boolean_t
3534 3481 vdev_allocatable(vdev_t *vd)
3535 3482 {
3536 3483 uint64_t state = vd->vdev_state;
3537 3484
3538 3485 /*
3539 3486 * We currently allow allocations from vdevs which may be in the
3540 3487 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
3541 3488 * fails to reopen then we'll catch it later when we're holding
3542 3489 * the proper locks. Note that we have to get the vdev state
3543 3490 * in a local variable because although it changes atomically,
3544 3491 * we're asking two separate questions about it.
3545 3492 */
3546 3493 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
3547 3494 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
3548 3495 vd->vdev_mg->mg_initialized);
3549 3496 }
3550 3497
3551 3498 boolean_t
3552 3499 vdev_accessible(vdev_t *vd, zio_t *zio)
3553 3500 {
3554 3501 ASSERT(zio->io_vd == vd);
3555 3502
3556 3503 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
3557 3504 return (B_FALSE);
3558 3505
3559 3506 if (zio->io_type == ZIO_TYPE_READ)
3560 3507 return (!vd->vdev_cant_read);
3561 3508
3562 3509 if (zio->io_type == ZIO_TYPE_WRITE)
3563 3510 return (!vd->vdev_cant_write);
3564 3511
3565 3512 return (B_TRUE);
3566 3513 }
3567 3514
3568 3515 boolean_t
3569 3516 vdev_is_spacemap_addressable(vdev_t *vd)
3570 3517 {
3571 3518 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
3572 3519 return (B_TRUE);
3573 3520
3574 3521 /*
3575 3522 * If double-word space map entries are not enabled we assume
3576 3523 * 47 bits of the space map entry are dedicated to the entry's
3577 3524 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
3578 3525 * to calculate the maximum address that can be described by a
3579 3526 * space map entry for the given device.
3580 3527 */
3581 3528 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
3582 3529
3583 3530 if (shift >= 63) /* detect potential overflow */
3584 3531 return (B_TRUE);
3585 3532
3586 3533 return (vd->vdev_asize < (1ULL << shift));
3587 3534 }
3588 3535
3589 3536 /*
3590 3537 * Get statistics for the given vdev.
3591 3538 */
3592 3539 void
3593 3540 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
3594 3541 {
3595 3542 spa_t *spa = vd->vdev_spa;
3596 3543 vdev_t *rvd = spa->spa_root_vdev;
3597 3544 vdev_t *tvd = vd->vdev_top;
3598 3545
3599 3546 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3600 3547
3601 3548 mutex_enter(&vd->vdev_stat_lock);
3602 3549 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
3603 3550 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
3604 3551 vs->vs_state = vd->vdev_state;
3605 3552 vs->vs_rsize = vdev_get_min_asize(vd);
3606 3553 if (vd->vdev_ops->vdev_op_leaf) {
3607 3554 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
3608 3555 /*
3609 3556 * Report intializing progress. Since we don't have the
3610 3557 * initializing locks held, this is only an estimate (although a
3611 3558 * fairly accurate one).
3612 3559 */
3613 3560 vs->vs_initialize_bytes_done = vd->vdev_initialize_bytes_done;
3614 3561 vs->vs_initialize_bytes_est = vd->vdev_initialize_bytes_est;
3615 3562 vs->vs_initialize_state = vd->vdev_initialize_state;
3616 3563 vs->vs_initialize_action_time = vd->vdev_initialize_action_time;
3617 3564 }
3618 3565 /*
3619 3566 * Report expandable space on top-level, non-auxillary devices only.
3620 3567 * The expandable space is reported in terms of metaslab sized units
3621 3568 * since that determines how much space the pool can expand.
3622 3569 */
3623 3570 if (vd->vdev_aux == NULL && tvd != NULL) {
3624 3571 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize -
3625 3572 spa->spa_bootsize, 1ULL << tvd->vdev_ms_shift);
3626 3573 }
3627 3574 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
3628 3575 vdev_is_concrete(vd)) {
3629 3576 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
3630 3577 vd->vdev_mg->mg_fragmentation : 0;
3631 3578 }
3632 3579
3633 3580 /*
3634 3581 * If we're getting stats on the root vdev, aggregate the I/O counts
3635 3582 * over all top-level vdevs (i.e. the direct children of the root).
3636 3583 */
3637 3584 if (vd == rvd) {
3638 3585 for (int c = 0; c < rvd->vdev_children; c++) {
3639 3586 vdev_t *cvd = rvd->vdev_child[c];
3640 3587 vdev_stat_t *cvs = &cvd->vdev_stat;
3641 3588
3642 3589 for (int t = 0; t < ZIO_TYPES; t++) {
3643 3590 vs->vs_ops[t] += cvs->vs_ops[t];
3644 3591 vs->vs_bytes[t] += cvs->vs_bytes[t];
3645 3592 }
3646 3593 cvs->vs_scan_removing = cvd->vdev_removing;
3647 3594 }
3648 3595 }
3649 3596 mutex_exit(&vd->vdev_stat_lock);
3650 3597 }
3651 3598
3652 3599 void
3653 3600 vdev_clear_stats(vdev_t *vd)
3654 3601 {
3655 3602 mutex_enter(&vd->vdev_stat_lock);
3656 3603 vd->vdev_stat.vs_space = 0;
3657 3604 vd->vdev_stat.vs_dspace = 0;
3658 3605 vd->vdev_stat.vs_alloc = 0;
3659 3606 mutex_exit(&vd->vdev_stat_lock);
3660 3607 }
3661 3608
3662 3609 void
3663 3610 vdev_scan_stat_init(vdev_t *vd)
3664 3611 {
3665 3612 vdev_stat_t *vs = &vd->vdev_stat;
3666 3613
3667 3614 for (int c = 0; c < vd->vdev_children; c++)
3668 3615 vdev_scan_stat_init(vd->vdev_child[c]);
3669 3616
3670 3617 mutex_enter(&vd->vdev_stat_lock);
3671 3618 vs->vs_scan_processed = 0;
3672 3619 mutex_exit(&vd->vdev_stat_lock);
3673 3620 }
3674 3621
3675 3622 void
3676 3623 vdev_stat_update(zio_t *zio, uint64_t psize)
3677 3624 {
3678 3625 spa_t *spa = zio->io_spa;
3679 3626 vdev_t *rvd = spa->spa_root_vdev;
3680 3627 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
3681 3628 vdev_t *pvd;
3682 3629 uint64_t txg = zio->io_txg;
3683 3630 vdev_stat_t *vs = &vd->vdev_stat;
3684 3631 zio_type_t type = zio->io_type;
3685 3632 int flags = zio->io_flags;
3686 3633
3687 3634 /*
3688 3635 * If this i/o is a gang leader, it didn't do any actual work.
3689 3636 */
3690 3637 if (zio->io_gang_tree)
3691 3638 return;
3692 3639
3693 3640 if (zio->io_error == 0) {
3694 3641 /*
3695 3642 * If this is a root i/o, don't count it -- we've already
3696 3643 * counted the top-level vdevs, and vdev_get_stats() will
3697 3644 * aggregate them when asked. This reduces contention on
3698 3645 * the root vdev_stat_lock and implicitly handles blocks
3699 3646 * that compress away to holes, for which there is no i/o.
3700 3647 * (Holes never create vdev children, so all the counters
3701 3648 * remain zero, which is what we want.)
3702 3649 *
3703 3650 * Note: this only applies to successful i/o (io_error == 0)
3704 3651 * because unlike i/o counts, errors are not additive.
3705 3652 * When reading a ditto block, for example, failure of
3706 3653 * one top-level vdev does not imply a root-level error.
3707 3654 */
3708 3655 if (vd == rvd)
3709 3656 return;
3710 3657
3711 3658 ASSERT(vd == zio->io_vd);
3712 3659
3713 3660 if (flags & ZIO_FLAG_IO_BYPASS)
3714 3661 return;
3715 3662
3716 3663 mutex_enter(&vd->vdev_stat_lock);
3717 3664
3718 3665 if (flags & ZIO_FLAG_IO_REPAIR) {
3719 3666 if (flags & ZIO_FLAG_SCAN_THREAD) {
3720 3667 dsl_scan_phys_t *scn_phys =
3721 3668 &spa->spa_dsl_pool->dp_scan->scn_phys;
3722 3669 uint64_t *processed = &scn_phys->scn_processed;
3723 3670
3724 3671 /* XXX cleanup? */
3725 3672 if (vd->vdev_ops->vdev_op_leaf)
3726 3673 atomic_add_64(processed, psize);
3727 3674 vs->vs_scan_processed += psize;
3728 3675 }
3729 3676
3730 3677 if (flags & ZIO_FLAG_SELF_HEAL)
3731 3678 vs->vs_self_healed += psize;
3732 3679 }
3733 3680
3734 3681 vs->vs_ops[type]++;
3735 3682 vs->vs_bytes[type] += psize;
3736 3683
3737 3684 mutex_exit(&vd->vdev_stat_lock);
3738 3685 return;
3739 3686 }
3740 3687
3741 3688 if (flags & ZIO_FLAG_SPECULATIVE)
3742 3689 return;
3743 3690
3744 3691 /*
3745 3692 * If this is an I/O error that is going to be retried, then ignore the
3746 3693 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
3747 3694 * hard errors, when in reality they can happen for any number of
3748 3695 * innocuous reasons (bus resets, MPxIO link failure, etc).
3749 3696 */
3750 3697 if (zio->io_error == EIO &&
3751 3698 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
3752 3699 return;
3753 3700
3754 3701 /*
3755 3702 * Intent logs writes won't propagate their error to the root
3756 3703 * I/O so don't mark these types of failures as pool-level
3757 3704 * errors.
3758 3705 */
3759 3706 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
3760 3707 return;
3761 3708
3762 3709 mutex_enter(&vd->vdev_stat_lock);
3763 3710 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
3764 3711 if (zio->io_error == ECKSUM)
3765 3712 vs->vs_checksum_errors++;
3766 3713 else
3767 3714 vs->vs_read_errors++;
3768 3715 }
3769 3716 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
3770 3717 vs->vs_write_errors++;
3771 3718 mutex_exit(&vd->vdev_stat_lock);
3772 3719
3773 3720 if (spa->spa_load_state == SPA_LOAD_NONE &&
3774 3721 type == ZIO_TYPE_WRITE && txg != 0 &&
3775 3722 (!(flags & ZIO_FLAG_IO_REPAIR) ||
3776 3723 (flags & ZIO_FLAG_SCAN_THREAD) ||
3777 3724 spa->spa_claiming)) {
3778 3725 /*
3779 3726 * This is either a normal write (not a repair), or it's
3780 3727 * a repair induced by the scrub thread, or it's a repair
3781 3728 * made by zil_claim() during spa_load() in the first txg.
3782 3729 * In the normal case, we commit the DTL change in the same
3783 3730 * txg as the block was born. In the scrub-induced repair
3784 3731 * case, we know that scrubs run in first-pass syncing context,
3785 3732 * so we commit the DTL change in spa_syncing_txg(spa).
3786 3733 * In the zil_claim() case, we commit in spa_first_txg(spa).
3787 3734 *
3788 3735 * We currently do not make DTL entries for failed spontaneous
3789 3736 * self-healing writes triggered by normal (non-scrubbing)
3790 3737 * reads, because we have no transactional context in which to
3791 3738 * do so -- and it's not clear that it'd be desirable anyway.
3792 3739 */
3793 3740 if (vd->vdev_ops->vdev_op_leaf) {
3794 3741 uint64_t commit_txg = txg;
3795 3742 if (flags & ZIO_FLAG_SCAN_THREAD) {
3796 3743 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3797 3744 ASSERT(spa_sync_pass(spa) == 1);
3798 3745 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
3799 3746 commit_txg = spa_syncing_txg(spa);
3800 3747 } else if (spa->spa_claiming) {
3801 3748 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
3802 3749 commit_txg = spa_first_txg(spa);
3803 3750 }
3804 3751 ASSERT(commit_txg >= spa_syncing_txg(spa));
3805 3752 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
3806 3753 return;
3807 3754 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
3808 3755 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
3809 3756 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
3810 3757 }
3811 3758 if (vd != rvd)
3812 3759 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
3813 3760 }
3814 3761 }
3815 3762
3816 3763 int64_t
3817 3764 vdev_deflated_space(vdev_t *vd, int64_t space)
3818 3765 {
3819 3766 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
3820 3767 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
3821 3768
3822 3769 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
3823 3770 }
3824 3771
3825 3772 /*
3826 3773 * Update the in-core space usage stats for this vdev and the root vdev.
3827 3774 */
3828 3775 void
3829 3776 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
3830 3777 int64_t space_delta)
3831 3778 {
3832 3779 int64_t dspace_delta;
3833 3780 spa_t *spa = vd->vdev_spa;
3834 3781 vdev_t *rvd = spa->spa_root_vdev;
3835 3782
3836 3783 ASSERT(vd == vd->vdev_top);
3837 3784
3838 3785 /*
3839 3786 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
3840 3787 * factor. We must calculate this here and not at the root vdev
3841 3788 * because the root vdev's psize-to-asize is simply the max of its
3842 3789 * childrens', thus not accurate enough for us.
3843 3790 */
3844 3791 dspace_delta = vdev_deflated_space(vd, space_delta);
3845 3792
3846 3793 mutex_enter(&vd->vdev_stat_lock);
3847 3794 vd->vdev_stat.vs_alloc += alloc_delta;
3848 3795 vd->vdev_stat.vs_space += space_delta;
3849 3796 vd->vdev_stat.vs_dspace += dspace_delta;
3850 3797 mutex_exit(&vd->vdev_stat_lock);
3851 3798
3852 3799 /* every class but log contributes to root space stats */
3853 3800 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
3854 3801 mutex_enter(&rvd->vdev_stat_lock);
3855 3802 rvd->vdev_stat.vs_alloc += alloc_delta;
3856 3803 rvd->vdev_stat.vs_space += space_delta;
3857 3804 rvd->vdev_stat.vs_dspace += dspace_delta;
3858 3805 mutex_exit(&rvd->vdev_stat_lock);
3859 3806 }
3860 3807 /* Note: metaslab_class_space_update moved to metaslab_space_update */
3861 3808 }
3862 3809
3863 3810 /*
3864 3811 * Mark a top-level vdev's config as dirty, placing it on the dirty list
3865 3812 * so that it will be written out next time the vdev configuration is synced.
3866 3813 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
3867 3814 */
3868 3815 void
3869 3816 vdev_config_dirty(vdev_t *vd)
3870 3817 {
3871 3818 spa_t *spa = vd->vdev_spa;
3872 3819 vdev_t *rvd = spa->spa_root_vdev;
3873 3820 int c;
3874 3821
3875 3822 ASSERT(spa_writeable(spa));
3876 3823
3877 3824 /*
3878 3825 * If this is an aux vdev (as with l2cache and spare devices), then we
3879 3826 * update the vdev config manually and set the sync flag.
3880 3827 */
3881 3828 if (vd->vdev_aux != NULL) {
3882 3829 spa_aux_vdev_t *sav = vd->vdev_aux;
3883 3830 nvlist_t **aux;
3884 3831 uint_t naux;
3885 3832
3886 3833 for (c = 0; c < sav->sav_count; c++) {
3887 3834 if (sav->sav_vdevs[c] == vd)
3888 3835 break;
3889 3836 }
3890 3837
3891 3838 if (c == sav->sav_count) {
3892 3839 /*
3893 3840 * We're being removed. There's nothing more to do.
3894 3841 */
3895 3842 ASSERT(sav->sav_sync == B_TRUE);
3896 3843 return;
3897 3844 }
3898 3845
3899 3846 sav->sav_sync = B_TRUE;
3900 3847
3901 3848 if (nvlist_lookup_nvlist_array(sav->sav_config,
3902 3849 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3903 3850 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3904 3851 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3905 3852 }
3906 3853
3907 3854 ASSERT(c < naux);
3908 3855
3909 3856 /*
3910 3857 * Setting the nvlist in the middle if the array is a little
3911 3858 * sketchy, but it will work.
3912 3859 */
3913 3860 nvlist_free(aux[c]);
3914 3861 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3915 3862
3916 3863 return;
3917 3864 }
3918 3865
3919 3866 /*
3920 3867 * The dirty list is protected by the SCL_CONFIG lock. The caller
3921 3868 * must either hold SCL_CONFIG as writer, or must be the sync thread
3922 3869 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3923 3870 * so this is sufficient to ensure mutual exclusion.
3924 3871 */
3925 3872 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3926 3873 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3927 3874 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3928 3875
3929 3876 if (vd == rvd) {
3930 3877 for (c = 0; c < rvd->vdev_children; c++)
3931 3878 vdev_config_dirty(rvd->vdev_child[c]);
3932 3879 } else {
3933 3880 ASSERT(vd == vd->vdev_top);
3934 3881
3935 3882 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3936 3883 vdev_is_concrete(vd)) {
3937 3884 list_insert_head(&spa->spa_config_dirty_list, vd);
3938 3885 }
3939 3886 }
3940 3887 }
3941 3888
3942 3889 void
3943 3890 vdev_config_clean(vdev_t *vd)
3944 3891 {
3945 3892 spa_t *spa = vd->vdev_spa;
3946 3893
3947 3894 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3948 3895 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3949 3896 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3950 3897
3951 3898 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3952 3899 list_remove(&spa->spa_config_dirty_list, vd);
3953 3900 }
3954 3901
3955 3902 /*
3956 3903 * Mark a top-level vdev's state as dirty, so that the next pass of
3957 3904 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3958 3905 * the state changes from larger config changes because they require
3959 3906 * much less locking, and are often needed for administrative actions.
3960 3907 */
3961 3908 void
3962 3909 vdev_state_dirty(vdev_t *vd)
3963 3910 {
3964 3911 spa_t *spa = vd->vdev_spa;
3965 3912
3966 3913 ASSERT(spa_writeable(spa));
3967 3914 ASSERT(vd == vd->vdev_top);
3968 3915
3969 3916 /*
3970 3917 * The state list is protected by the SCL_STATE lock. The caller
3971 3918 * must either hold SCL_STATE as writer, or must be the sync thread
3972 3919 * (which holds SCL_STATE as reader). There's only one sync thread,
3973 3920 * so this is sufficient to ensure mutual exclusion.
3974 3921 */
3975 3922 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3976 3923 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3977 3924 spa_config_held(spa, SCL_STATE, RW_READER)));
3978 3925
3979 3926 if (!list_link_active(&vd->vdev_state_dirty_node) &&
3980 3927 vdev_is_concrete(vd))
3981 3928 list_insert_head(&spa->spa_state_dirty_list, vd);
3982 3929 }
3983 3930
3984 3931 void
3985 3932 vdev_state_clean(vdev_t *vd)
3986 3933 {
3987 3934 spa_t *spa = vd->vdev_spa;
3988 3935
3989 3936 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3990 3937 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3991 3938 spa_config_held(spa, SCL_STATE, RW_READER)));
3992 3939
3993 3940 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3994 3941 list_remove(&spa->spa_state_dirty_list, vd);
3995 3942 }
3996 3943
3997 3944 /*
3998 3945 * Propagate vdev state up from children to parent.
3999 3946 */
4000 3947 void
4001 3948 vdev_propagate_state(vdev_t *vd)
4002 3949 {
4003 3950 spa_t *spa = vd->vdev_spa;
4004 3951 vdev_t *rvd = spa->spa_root_vdev;
4005 3952 int degraded = 0, faulted = 0;
4006 3953 int corrupted = 0;
4007 3954 vdev_t *child;
4008 3955
4009 3956 if (vd->vdev_children > 0) {
4010 3957 for (int c = 0; c < vd->vdev_children; c++) {
4011 3958 child = vd->vdev_child[c];
4012 3959
4013 3960 /*
4014 3961 * Don't factor holes or indirect vdevs into the
4015 3962 * decision.
4016 3963 */
4017 3964 if (!vdev_is_concrete(child))
4018 3965 continue;
4019 3966
4020 3967 if (!vdev_readable(child) ||
4021 3968 (!vdev_writeable(child) && spa_writeable(spa))) {
4022 3969 /*
4023 3970 * Root special: if there is a top-level log
4024 3971 * device, treat the root vdev as if it were
4025 3972 * degraded.
4026 3973 */
4027 3974 if (child->vdev_islog && vd == rvd)
4028 3975 degraded++;
4029 3976 else
4030 3977 faulted++;
4031 3978 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
4032 3979 degraded++;
4033 3980 }
4034 3981
4035 3982 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
4036 3983 corrupted++;
4037 3984 }
4038 3985
4039 3986 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
4040 3987
4041 3988 /*
4042 3989 * Root special: if there is a top-level vdev that cannot be
4043 3990 * opened due to corrupted metadata, then propagate the root
4044 3991 * vdev's aux state as 'corrupt' rather than 'insufficient
4045 3992 * replicas'.
4046 3993 */
4047 3994 if (corrupted && vd == rvd &&
4048 3995 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
4049 3996 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
4050 3997 VDEV_AUX_CORRUPT_DATA);
4051 3998 }
4052 3999
4053 4000 if (vd->vdev_parent)
4054 4001 vdev_propagate_state(vd->vdev_parent);
4055 4002 }
4056 4003
4057 4004 /*
4058 4005 * Set a vdev's state. If this is during an open, we don't update the parent
4059 4006 * state, because we're in the process of opening children depth-first.
4060 4007 * Otherwise, we propagate the change to the parent.
4061 4008 *
4062 4009 * If this routine places a device in a faulted state, an appropriate ereport is
4063 4010 * generated.
4064 4011 */
4065 4012 void
4066 4013 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
4067 4014 {
4068 4015 uint64_t save_state;
4069 4016 spa_t *spa = vd->vdev_spa;
4070 4017
4071 4018 if (state == vd->vdev_state) {
4072 4019 vd->vdev_stat.vs_aux = aux;
4073 4020 return;
4074 4021 }
4075 4022
4076 4023 save_state = vd->vdev_state;
4077 4024
4078 4025 vd->vdev_state = state;
4079 4026 vd->vdev_stat.vs_aux = aux;
4080 4027
4081 4028 /*
4082 4029 * If we are setting the vdev state to anything but an open state, then
4083 4030 * always close the underlying device unless the device has requested
4084 4031 * a delayed close (i.e. we're about to remove or fault the device).
4085 4032 * Otherwise, we keep accessible but invalid devices open forever.
4086 4033 * We don't call vdev_close() itself, because that implies some extra
4087 4034 * checks (offline, etc) that we don't want here. This is limited to
4088 4035 * leaf devices, because otherwise closing the device will affect other
4089 4036 * children.
4090 4037 */
4091 4038 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
4092 4039 vd->vdev_ops->vdev_op_leaf)
4093 4040 vd->vdev_ops->vdev_op_close(vd);
4094 4041
4095 4042 /*
4096 4043 * If we have brought this vdev back into service, we need
4097 4044 * to notify fmd so that it can gracefully repair any outstanding
4098 4045 * cases due to a missing device. We do this in all cases, even those
4099 4046 * that probably don't correlate to a repaired fault. This is sure to
4100 4047 * catch all cases, and we let the zfs-retire agent sort it out. If
4101 4048 * this is a transient state it's OK, as the retire agent will
4102 4049 * double-check the state of the vdev before repairing it.
4103 4050 */
4104 4051 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
4105 4052 vd->vdev_prevstate != state)
4106 4053 zfs_post_state_change(spa, vd);
4107 4054
4108 4055 if (vd->vdev_removed &&
4109 4056 state == VDEV_STATE_CANT_OPEN &&
4110 4057 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
4111 4058 /*
4112 4059 * If the previous state is set to VDEV_STATE_REMOVED, then this
4113 4060 * device was previously marked removed and someone attempted to
4114 4061 * reopen it. If this failed due to a nonexistent device, then
4115 4062 * keep the device in the REMOVED state. We also let this be if
4116 4063 * it is one of our special test online cases, which is only
4117 4064 * attempting to online the device and shouldn't generate an FMA
4118 4065 * fault.
4119 4066 */
4120 4067 vd->vdev_state = VDEV_STATE_REMOVED;
4121 4068 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
4122 4069 } else if (state == VDEV_STATE_REMOVED) {
4123 4070 vd->vdev_removed = B_TRUE;
4124 4071 } else if (state == VDEV_STATE_CANT_OPEN) {
4125 4072 /*
4126 4073 * If we fail to open a vdev during an import or recovery, we
4127 4074 * mark it as "not available", which signifies that it was
4128 4075 * never there to begin with. Failure to open such a device
4129 4076 * is not considered an error.
4130 4077 */
4131 4078 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
4132 4079 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
4133 4080 vd->vdev_ops->vdev_op_leaf)
4134 4081 vd->vdev_not_present = 1;
4135 4082
4136 4083 /*
4137 4084 * Post the appropriate ereport. If the 'prevstate' field is
4138 4085 * set to something other than VDEV_STATE_UNKNOWN, it indicates
4139 4086 * that this is part of a vdev_reopen(). In this case, we don't
4140 4087 * want to post the ereport if the device was already in the
4141 4088 * CANT_OPEN state beforehand.
4142 4089 *
4143 4090 * If the 'checkremove' flag is set, then this is an attempt to
4144 4091 * online the device in response to an insertion event. If we
4145 4092 * hit this case, then we have detected an insertion event for a
4146 4093 * faulted or offline device that wasn't in the removed state.
4147 4094 * In this scenario, we don't post an ereport because we are
4148 4095 * about to replace the device, or attempt an online with
4149 4096 * vdev_forcefault, which will generate the fault for us.
4150 4097 */
4151 4098 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
4152 4099 !vd->vdev_not_present && !vd->vdev_checkremove &&
4153 4100 vd != spa->spa_root_vdev) {
4154 4101 const char *class;
4155 4102
4156 4103 switch (aux) {
4157 4104 case VDEV_AUX_OPEN_FAILED:
4158 4105 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
4159 4106 break;
4160 4107 case VDEV_AUX_CORRUPT_DATA:
4161 4108 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
4162 4109 break;
4163 4110 case VDEV_AUX_NO_REPLICAS:
4164 4111 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
4165 4112 break;
4166 4113 case VDEV_AUX_BAD_GUID_SUM:
4167 4114 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
4168 4115 break;
4169 4116 case VDEV_AUX_TOO_SMALL:
4170 4117 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
4171 4118 break;
4172 4119 case VDEV_AUX_BAD_LABEL:
4173 4120 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
4174 4121 break;
4175 4122 default:
4176 4123 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
4177 4124 }
4178 4125
4179 4126 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
4180 4127 }
4181 4128
4182 4129 /* Erase any notion of persistent removed state */
4183 4130 vd->vdev_removed = B_FALSE;
4184 4131 } else {
4185 4132 vd->vdev_removed = B_FALSE;
4186 4133 }
4187 4134
4188 4135 if (!isopen && vd->vdev_parent)
4189 4136 vdev_propagate_state(vd->vdev_parent);
4190 4137 }
4191 4138
4192 4139 boolean_t
4193 4140 vdev_children_are_offline(vdev_t *vd)
4194 4141 {
4195 4142 ASSERT(!vd->vdev_ops->vdev_op_leaf);
4196 4143
4197 4144 for (uint64_t i = 0; i < vd->vdev_children; i++) {
4198 4145 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
4199 4146 return (B_FALSE);
4200 4147 }
4201 4148
4202 4149 return (B_TRUE);
4203 4150 }
4204 4151
4205 4152 /*
4206 4153 * Check the vdev configuration to ensure that it's capable of supporting
4207 4154 * a root pool. We do not support partial configuration.
4208 4155 * In addition, only a single top-level vdev is allowed.
4209 4156 */
4210 4157 boolean_t
4211 4158 vdev_is_bootable(vdev_t *vd)
4212 4159 {
4213 4160 if (!vd->vdev_ops->vdev_op_leaf) {
4214 4161 char *vdev_type = vd->vdev_ops->vdev_op_type;
4215 4162
4216 4163 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
4217 4164 vd->vdev_children > 1) {
4218 4165 return (B_FALSE);
4219 4166 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0 ||
4220 4167 strcmp(vdev_type, VDEV_TYPE_INDIRECT) == 0) {
4221 4168 return (B_FALSE);
4222 4169 }
4223 4170 }
4224 4171
4225 4172 for (int c = 0; c < vd->vdev_children; c++) {
4226 4173 if (!vdev_is_bootable(vd->vdev_child[c]))
4227 4174 return (B_FALSE);
4228 4175 }
4229 4176 return (B_TRUE);
4230 4177 }
4231 4178
4232 4179 boolean_t
4233 4180 vdev_is_concrete(vdev_t *vd)
4234 4181 {
4235 4182 vdev_ops_t *ops = vd->vdev_ops;
4236 4183 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
4237 4184 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
4238 4185 return (B_FALSE);
4239 4186 } else {
4240 4187 return (B_TRUE);
4241 4188 }
4242 4189 }
4243 4190
4244 4191 /*
4245 4192 * Determine if a log device has valid content. If the vdev was
4246 4193 * removed or faulted in the MOS config then we know that
4247 4194 * the content on the log device has already been written to the pool.
4248 4195 */
4249 4196 boolean_t
4250 4197 vdev_log_state_valid(vdev_t *vd)
4251 4198 {
4252 4199 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
4253 4200 !vd->vdev_removed)
4254 4201 return (B_TRUE);
4255 4202
4256 4203 for (int c = 0; c < vd->vdev_children; c++)
4257 4204 if (vdev_log_state_valid(vd->vdev_child[c]))
4258 4205 return (B_TRUE);
4259 4206
4260 4207 return (B_FALSE);
4261 4208 }
4262 4209
4263 4210 /*
4264 4211 * Expand a vdev if possible.
4265 4212 */
4266 4213 void
4267 4214 vdev_expand(vdev_t *vd, uint64_t txg)
4268 4215 {
4269 4216 ASSERT(vd->vdev_top == vd);
4270 4217 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4271 4218 ASSERT(vdev_is_concrete(vd));
4272 4219
4273 4220 vdev_set_deflate_ratio(vd);
4274 4221
4275 4222 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
4276 4223 vdev_is_concrete(vd)) {
4277 4224 vdev_metaslab_group_create(vd);
4278 4225 VERIFY(vdev_metaslab_init(vd, txg) == 0);
4279 4226 vdev_config_dirty(vd);
4280 4227 }
4281 4228 }
4282 4229
4283 4230 /*
4284 4231 * Split a vdev.
4285 4232 */
4286 4233 void
4287 4234 vdev_split(vdev_t *vd)
4288 4235 {
4289 4236 vdev_t *cvd, *pvd = vd->vdev_parent;
4290 4237
4291 4238 vdev_remove_child(pvd, vd);
4292 4239 vdev_compact_children(pvd);
4293 4240
4294 4241 cvd = pvd->vdev_child[0];
4295 4242 if (pvd->vdev_children == 1) {
4296 4243 vdev_remove_parent(cvd);
4297 4244 cvd->vdev_splitting = B_TRUE;
4298 4245 }
4299 4246 vdev_propagate_state(cvd);
4300 4247 }
4301 4248
4302 4249 void
4303 4250 vdev_deadman(vdev_t *vd)
4304 4251 {
4305 4252 for (int c = 0; c < vd->vdev_children; c++) {
4306 4253 vdev_t *cvd = vd->vdev_child[c];
4307 4254
4308 4255 vdev_deadman(cvd);
4309 4256 }
4310 4257
4311 4258 if (vd->vdev_ops->vdev_op_leaf) {
4312 4259 vdev_queue_t *vq = &vd->vdev_queue;
4313 4260
4314 4261 mutex_enter(&vq->vq_lock);
4315 4262 if (avl_numnodes(&vq->vq_active_tree) > 0) {
4316 4263 spa_t *spa = vd->vdev_spa;
4317 4264 zio_t *fio;
4318 4265 uint64_t delta;
4319 4266
4320 4267 /*
4321 4268 * Look at the head of all the pending queues,
4322 4269 * if any I/O has been outstanding for longer than
4323 4270 * the spa_deadman_synctime we panic the system.
4324 4271 */
4325 4272 fio = avl_first(&vq->vq_active_tree);
4326 4273 delta = gethrtime() - fio->io_timestamp;
4327 4274 if (delta > spa_deadman_synctime(spa)) {
4328 4275 vdev_dbgmsg(vd, "SLOW IO: zio timestamp "
4329 4276 "%lluns, delta %lluns, last io %lluns",
4330 4277 fio->io_timestamp, (u_longlong_t)delta,
4331 4278 vq->vq_io_complete_ts);
4332 4279 fm_panic("I/O to pool '%s' appears to be "
4333 4280 "hung.", spa_name(spa));
4334 4281 }
4335 4282 }
4336 4283 mutex_exit(&vq->vq_lock);
4337 4284 }
4338 4285 }
|
↓ open down ↓ |
956 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX