Print this page
7938 Port ZOL #3712 disable LBA weighting on files and SSDs
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, 2015 by Delphix. All rights reserved.
25 25 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 26 * Copyright (c) 2014 Integros [integros.com]
27 27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 28 */
29 29
30 30 #include <sys/zfs_context.h>
31 31 #include <sys/fm/fs/zfs.h>
32 32 #include <sys/spa.h>
33 33 #include <sys/spa_impl.h>
34 34 #include <sys/dmu.h>
35 35 #include <sys/dmu_tx.h>
36 36 #include <sys/vdev_impl.h>
37 37 #include <sys/uberblock_impl.h>
38 38 #include <sys/metaslab.h>
39 39 #include <sys/metaslab_impl.h>
40 40 #include <sys/space_map.h>
41 41 #include <sys/space_reftree.h>
42 42 #include <sys/zio.h>
43 43 #include <sys/zap.h>
44 44 #include <sys/fs/zfs.h>
45 45 #include <sys/arc.h>
46 46 #include <sys/zil.h>
47 47 #include <sys/dsl_scan.h>
48 48
49 49 /*
50 50 * Virtual device management.
51 51 */
52 52
53 53 static vdev_ops_t *vdev_ops_table[] = {
54 54 &vdev_root_ops,
55 55 &vdev_raidz_ops,
56 56 &vdev_mirror_ops,
57 57 &vdev_replacing_ops,
58 58 &vdev_spare_ops,
59 59 &vdev_disk_ops,
60 60 &vdev_file_ops,
61 61 &vdev_missing_ops,
62 62 &vdev_hole_ops,
63 63 NULL
64 64 };
65 65
66 66 /* maximum scrub/resilver I/O queue per leaf vdev */
67 67 int zfs_scrub_limit = 10;
68 68
69 69 /*
70 70 * When a vdev is added, it will be divided into approximately (but no
71 71 * more than) this number of metaslabs.
72 72 */
73 73 int metaslabs_per_vdev = 200;
74 74
75 75 /*
76 76 * Given a vdev type, return the appropriate ops vector.
77 77 */
78 78 static vdev_ops_t *
79 79 vdev_getops(const char *type)
80 80 {
81 81 vdev_ops_t *ops, **opspp;
82 82
83 83 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
84 84 if (strcmp(ops->vdev_op_type, type) == 0)
85 85 break;
86 86
87 87 return (ops);
88 88 }
89 89
90 90 /*
91 91 * Default asize function: return the MAX of psize with the asize of
92 92 * all children. This is what's used by anything other than RAID-Z.
93 93 */
94 94 uint64_t
95 95 vdev_default_asize(vdev_t *vd, uint64_t psize)
96 96 {
97 97 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
98 98 uint64_t csize;
99 99
100 100 for (int c = 0; c < vd->vdev_children; c++) {
101 101 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
102 102 asize = MAX(asize, csize);
103 103 }
104 104
105 105 return (asize);
106 106 }
107 107
108 108 /*
109 109 * Get the minimum allocatable size. We define the allocatable size as
110 110 * the vdev's asize rounded to the nearest metaslab. This allows us to
111 111 * replace or attach devices which don't have the same physical size but
112 112 * can still satisfy the same number of allocations.
113 113 */
114 114 uint64_t
115 115 vdev_get_min_asize(vdev_t *vd)
116 116 {
117 117 vdev_t *pvd = vd->vdev_parent;
118 118
119 119 /*
120 120 * If our parent is NULL (inactive spare or cache) or is the root,
121 121 * just return our own asize.
122 122 */
123 123 if (pvd == NULL)
124 124 return (vd->vdev_asize);
125 125
126 126 /*
127 127 * The top-level vdev just returns the allocatable size rounded
128 128 * to the nearest metaslab.
129 129 */
130 130 if (vd == vd->vdev_top)
131 131 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
132 132
133 133 /*
134 134 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
135 135 * so each child must provide at least 1/Nth of its asize.
136 136 */
137 137 if (pvd->vdev_ops == &vdev_raidz_ops)
138 138 return (pvd->vdev_min_asize / pvd->vdev_children);
139 139
140 140 return (pvd->vdev_min_asize);
141 141 }
142 142
143 143 void
144 144 vdev_set_min_asize(vdev_t *vd)
145 145 {
146 146 vd->vdev_min_asize = vdev_get_min_asize(vd);
147 147
148 148 for (int c = 0; c < vd->vdev_children; c++)
149 149 vdev_set_min_asize(vd->vdev_child[c]);
150 150 }
151 151
152 152 vdev_t *
153 153 vdev_lookup_top(spa_t *spa, uint64_t vdev)
154 154 {
155 155 vdev_t *rvd = spa->spa_root_vdev;
156 156
157 157 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
158 158
159 159 if (vdev < rvd->vdev_children) {
160 160 ASSERT(rvd->vdev_child[vdev] != NULL);
161 161 return (rvd->vdev_child[vdev]);
162 162 }
163 163
164 164 return (NULL);
165 165 }
166 166
167 167 vdev_t *
168 168 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
169 169 {
170 170 vdev_t *mvd;
171 171
172 172 if (vd->vdev_guid == guid)
173 173 return (vd);
174 174
175 175 for (int c = 0; c < vd->vdev_children; c++)
176 176 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
177 177 NULL)
178 178 return (mvd);
179 179
180 180 return (NULL);
181 181 }
182 182
183 183 static int
184 184 vdev_count_leaves_impl(vdev_t *vd)
185 185 {
186 186 int n = 0;
187 187
188 188 if (vd->vdev_ops->vdev_op_leaf)
189 189 return (1);
190 190
191 191 for (int c = 0; c < vd->vdev_children; c++)
192 192 n += vdev_count_leaves_impl(vd->vdev_child[c]);
193 193
194 194 return (n);
195 195 }
196 196
197 197 int
198 198 vdev_count_leaves(spa_t *spa)
199 199 {
200 200 return (vdev_count_leaves_impl(spa->spa_root_vdev));
201 201 }
202 202
203 203 void
204 204 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
205 205 {
206 206 size_t oldsize, newsize;
207 207 uint64_t id = cvd->vdev_id;
208 208 vdev_t **newchild;
209 209 spa_t *spa = cvd->vdev_spa;
210 210
211 211 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
212 212 ASSERT(cvd->vdev_parent == NULL);
213 213
214 214 cvd->vdev_parent = pvd;
215 215
216 216 if (pvd == NULL)
217 217 return;
218 218
219 219 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
220 220
221 221 oldsize = pvd->vdev_children * sizeof (vdev_t *);
222 222 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
223 223 newsize = pvd->vdev_children * sizeof (vdev_t *);
224 224
225 225 newchild = kmem_zalloc(newsize, KM_SLEEP);
226 226 if (pvd->vdev_child != NULL) {
227 227 bcopy(pvd->vdev_child, newchild, oldsize);
228 228 kmem_free(pvd->vdev_child, oldsize);
229 229 }
230 230
231 231 pvd->vdev_child = newchild;
232 232 pvd->vdev_child[id] = cvd;
233 233
234 234 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
235 235 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
236 236
237 237 /*
238 238 * Walk up all ancestors to update guid sum.
239 239 */
240 240 for (; pvd != NULL; pvd = pvd->vdev_parent)
241 241 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
242 242 }
243 243
244 244 void
245 245 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
246 246 {
247 247 int c;
248 248 uint_t id = cvd->vdev_id;
249 249
250 250 ASSERT(cvd->vdev_parent == pvd);
251 251
252 252 if (pvd == NULL)
253 253 return;
254 254
255 255 ASSERT(id < pvd->vdev_children);
256 256 ASSERT(pvd->vdev_child[id] == cvd);
257 257
258 258 pvd->vdev_child[id] = NULL;
259 259 cvd->vdev_parent = NULL;
260 260
261 261 for (c = 0; c < pvd->vdev_children; c++)
262 262 if (pvd->vdev_child[c])
263 263 break;
264 264
265 265 if (c == pvd->vdev_children) {
266 266 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
267 267 pvd->vdev_child = NULL;
268 268 pvd->vdev_children = 0;
269 269 }
270 270
271 271 /*
272 272 * Walk up all ancestors to update guid sum.
273 273 */
274 274 for (; pvd != NULL; pvd = pvd->vdev_parent)
275 275 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
276 276 }
277 277
278 278 /*
279 279 * Remove any holes in the child array.
280 280 */
281 281 void
282 282 vdev_compact_children(vdev_t *pvd)
283 283 {
284 284 vdev_t **newchild, *cvd;
285 285 int oldc = pvd->vdev_children;
286 286 int newc;
287 287
288 288 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
289 289
290 290 for (int c = newc = 0; c < oldc; c++)
291 291 if (pvd->vdev_child[c])
292 292 newc++;
293 293
294 294 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
295 295
296 296 for (int c = newc = 0; c < oldc; c++) {
297 297 if ((cvd = pvd->vdev_child[c]) != NULL) {
298 298 newchild[newc] = cvd;
299 299 cvd->vdev_id = newc++;
300 300 }
301 301 }
302 302
303 303 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
304 304 pvd->vdev_child = newchild;
305 305 pvd->vdev_children = newc;
306 306 }
307 307
308 308 /*
309 309 * Allocate and minimally initialize a vdev_t.
310 310 */
311 311 vdev_t *
312 312 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
313 313 {
314 314 vdev_t *vd;
315 315
316 316 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
317 317
318 318 if (spa->spa_root_vdev == NULL) {
319 319 ASSERT(ops == &vdev_root_ops);
320 320 spa->spa_root_vdev = vd;
321 321 spa->spa_load_guid = spa_generate_guid(NULL);
322 322 }
323 323
324 324 if (guid == 0 && ops != &vdev_hole_ops) {
325 325 if (spa->spa_root_vdev == vd) {
326 326 /*
327 327 * The root vdev's guid will also be the pool guid,
328 328 * which must be unique among all pools.
329 329 */
330 330 guid = spa_generate_guid(NULL);
331 331 } else {
332 332 /*
333 333 * Any other vdev's guid must be unique within the pool.
334 334 */
335 335 guid = spa_generate_guid(spa);
336 336 }
337 337 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
338 338 }
339 339
340 340 vd->vdev_spa = spa;
341 341 vd->vdev_id = id;
342 342 vd->vdev_guid = guid;
343 343 vd->vdev_guid_sum = guid;
344 344 vd->vdev_ops = ops;
345 345 vd->vdev_state = VDEV_STATE_CLOSED;
346 346 vd->vdev_ishole = (ops == &vdev_hole_ops);
347 347
348 348 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
349 349 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
350 350 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
351 351 mutex_init(&vd->vdev_queue_lock, NULL, MUTEX_DEFAULT, NULL);
352 352 for (int t = 0; t < DTL_TYPES; t++) {
353 353 vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
354 354 &vd->vdev_dtl_lock);
355 355 }
356 356 txg_list_create(&vd->vdev_ms_list,
357 357 offsetof(struct metaslab, ms_txg_node));
358 358 txg_list_create(&vd->vdev_dtl_list,
359 359 offsetof(struct vdev, vdev_dtl_node));
360 360 vd->vdev_stat.vs_timestamp = gethrtime();
361 361 vdev_queue_init(vd);
362 362 vdev_cache_init(vd);
363 363
364 364 return (vd);
365 365 }
366 366
367 367 /*
368 368 * Allocate a new vdev. The 'alloctype' is used to control whether we are
369 369 * creating a new vdev or loading an existing one - the behavior is slightly
370 370 * different for each case.
371 371 */
372 372 int
373 373 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
374 374 int alloctype)
375 375 {
376 376 vdev_ops_t *ops;
377 377 char *type;
378 378 uint64_t guid = 0, islog, nparity;
379 379 vdev_t *vd;
380 380
381 381 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
382 382
383 383 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
384 384 return (SET_ERROR(EINVAL));
385 385
386 386 if ((ops = vdev_getops(type)) == NULL)
387 387 return (SET_ERROR(EINVAL));
388 388
389 389 /*
390 390 * If this is a load, get the vdev guid from the nvlist.
391 391 * Otherwise, vdev_alloc_common() will generate one for us.
392 392 */
393 393 if (alloctype == VDEV_ALLOC_LOAD) {
394 394 uint64_t label_id;
395 395
396 396 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
397 397 label_id != id)
398 398 return (SET_ERROR(EINVAL));
399 399
400 400 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
401 401 return (SET_ERROR(EINVAL));
402 402 } else if (alloctype == VDEV_ALLOC_SPARE) {
403 403 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
404 404 return (SET_ERROR(EINVAL));
405 405 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
406 406 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
407 407 return (SET_ERROR(EINVAL));
408 408 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
409 409 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
410 410 return (SET_ERROR(EINVAL));
411 411 }
412 412
413 413 /*
414 414 * The first allocated vdev must be of type 'root'.
415 415 */
416 416 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
417 417 return (SET_ERROR(EINVAL));
418 418
419 419 /*
420 420 * Determine whether we're a log vdev.
421 421 */
422 422 islog = 0;
423 423 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
424 424 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
425 425 return (SET_ERROR(ENOTSUP));
426 426
427 427 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
428 428 return (SET_ERROR(ENOTSUP));
429 429
430 430 /*
431 431 * Set the nparity property for RAID-Z vdevs.
432 432 */
433 433 nparity = -1ULL;
434 434 if (ops == &vdev_raidz_ops) {
435 435 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
436 436 &nparity) == 0) {
437 437 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
438 438 return (SET_ERROR(EINVAL));
439 439 /*
440 440 * Previous versions could only support 1 or 2 parity
441 441 * device.
442 442 */
443 443 if (nparity > 1 &&
444 444 spa_version(spa) < SPA_VERSION_RAIDZ2)
445 445 return (SET_ERROR(ENOTSUP));
446 446 if (nparity > 2 &&
447 447 spa_version(spa) < SPA_VERSION_RAIDZ3)
448 448 return (SET_ERROR(ENOTSUP));
449 449 } else {
450 450 /*
451 451 * We require the parity to be specified for SPAs that
452 452 * support multiple parity levels.
453 453 */
454 454 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
455 455 return (SET_ERROR(EINVAL));
456 456 /*
457 457 * Otherwise, we default to 1 parity device for RAID-Z.
458 458 */
459 459 nparity = 1;
460 460 }
461 461 } else {
462 462 nparity = 0;
463 463 }
464 464 ASSERT(nparity != -1ULL);
465 465
466 466 vd = vdev_alloc_common(spa, id, guid, ops);
467 467
468 468 vd->vdev_islog = islog;
469 469 vd->vdev_nparity = nparity;
470 470
471 471 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
472 472 vd->vdev_path = spa_strdup(vd->vdev_path);
473 473 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
474 474 vd->vdev_devid = spa_strdup(vd->vdev_devid);
475 475 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
476 476 &vd->vdev_physpath) == 0)
477 477 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
478 478 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
479 479 vd->vdev_fru = spa_strdup(vd->vdev_fru);
480 480
481 481 /*
482 482 * Set the whole_disk property. If it's not specified, leave the value
483 483 * as -1.
484 484 */
485 485 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
486 486 &vd->vdev_wholedisk) != 0)
487 487 vd->vdev_wholedisk = -1ULL;
488 488
489 489 /*
490 490 * Look for the 'not present' flag. This will only be set if the device
491 491 * was not present at the time of import.
492 492 */
493 493 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
494 494 &vd->vdev_not_present);
495 495
496 496 /*
497 497 * Get the alignment requirement.
498 498 */
499 499 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
500 500
501 501 /*
502 502 * Retrieve the vdev creation time.
503 503 */
504 504 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
505 505 &vd->vdev_crtxg);
506 506
507 507 /*
508 508 * If we're a top-level vdev, try to load the allocation parameters.
509 509 */
510 510 if (parent && !parent->vdev_parent &&
511 511 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
512 512 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
513 513 &vd->vdev_ms_array);
514 514 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
515 515 &vd->vdev_ms_shift);
516 516 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
517 517 &vd->vdev_asize);
518 518 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
519 519 &vd->vdev_removing);
520 520 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
521 521 &vd->vdev_top_zap);
522 522 } else {
523 523 ASSERT0(vd->vdev_top_zap);
524 524 }
525 525
526 526 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
527 527 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
528 528 alloctype == VDEV_ALLOC_ADD ||
529 529 alloctype == VDEV_ALLOC_SPLIT ||
530 530 alloctype == VDEV_ALLOC_ROOTPOOL);
531 531 vd->vdev_mg = metaslab_group_create(islog ?
532 532 spa_log_class(spa) : spa_normal_class(spa), vd);
533 533 }
534 534
535 535 if (vd->vdev_ops->vdev_op_leaf &&
536 536 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
537 537 (void) nvlist_lookup_uint64(nv,
538 538 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
539 539 } else {
540 540 ASSERT0(vd->vdev_leaf_zap);
541 541 }
542 542
543 543 /*
544 544 * If we're a leaf vdev, try to load the DTL object and other state.
545 545 */
546 546
547 547 if (vd->vdev_ops->vdev_op_leaf &&
548 548 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
549 549 alloctype == VDEV_ALLOC_ROOTPOOL)) {
550 550 if (alloctype == VDEV_ALLOC_LOAD) {
551 551 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
552 552 &vd->vdev_dtl_object);
553 553 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
554 554 &vd->vdev_unspare);
555 555 }
556 556
557 557 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
558 558 uint64_t spare = 0;
559 559
560 560 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
561 561 &spare) == 0 && spare)
562 562 spa_spare_add(vd);
563 563 }
564 564
565 565 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
566 566 &vd->vdev_offline);
567 567
568 568 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
569 569 &vd->vdev_resilver_txg);
570 570
571 571 /*
572 572 * When importing a pool, we want to ignore the persistent fault
573 573 * state, as the diagnosis made on another system may not be
574 574 * valid in the current context. Local vdevs will
575 575 * remain in the faulted state.
576 576 */
577 577 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
578 578 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
579 579 &vd->vdev_faulted);
580 580 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
581 581 &vd->vdev_degraded);
582 582 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
583 583 &vd->vdev_removed);
584 584
585 585 if (vd->vdev_faulted || vd->vdev_degraded) {
586 586 char *aux;
587 587
588 588 vd->vdev_label_aux =
589 589 VDEV_AUX_ERR_EXCEEDED;
590 590 if (nvlist_lookup_string(nv,
591 591 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
592 592 strcmp(aux, "external") == 0)
593 593 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
594 594 }
595 595 }
596 596 }
597 597
598 598 /*
599 599 * Add ourselves to the parent's list of children.
600 600 */
601 601 vdev_add_child(parent, vd);
602 602
603 603 *vdp = vd;
604 604
605 605 return (0);
606 606 }
607 607
608 608 void
609 609 vdev_free(vdev_t *vd)
610 610 {
611 611 spa_t *spa = vd->vdev_spa;
612 612
613 613 /*
614 614 * vdev_free() implies closing the vdev first. This is simpler than
615 615 * trying to ensure complicated semantics for all callers.
616 616 */
617 617 vdev_close(vd);
618 618
619 619 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
620 620 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
621 621
622 622 /*
623 623 * Free all children.
624 624 */
625 625 for (int c = 0; c < vd->vdev_children; c++)
626 626 vdev_free(vd->vdev_child[c]);
627 627
628 628 ASSERT(vd->vdev_child == NULL);
629 629 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
630 630
631 631 /*
632 632 * Discard allocation state.
633 633 */
634 634 if (vd->vdev_mg != NULL) {
635 635 vdev_metaslab_fini(vd);
636 636 metaslab_group_destroy(vd->vdev_mg);
637 637 }
638 638
639 639 ASSERT0(vd->vdev_stat.vs_space);
640 640 ASSERT0(vd->vdev_stat.vs_dspace);
641 641 ASSERT0(vd->vdev_stat.vs_alloc);
642 642
643 643 /*
644 644 * Remove this vdev from its parent's child list.
645 645 */
646 646 vdev_remove_child(vd->vdev_parent, vd);
647 647
648 648 ASSERT(vd->vdev_parent == NULL);
649 649
650 650 /*
651 651 * Clean up vdev structure.
652 652 */
653 653 vdev_queue_fini(vd);
654 654 vdev_cache_fini(vd);
655 655
656 656 if (vd->vdev_path)
657 657 spa_strfree(vd->vdev_path);
658 658 if (vd->vdev_devid)
659 659 spa_strfree(vd->vdev_devid);
660 660 if (vd->vdev_physpath)
661 661 spa_strfree(vd->vdev_physpath);
662 662 if (vd->vdev_fru)
663 663 spa_strfree(vd->vdev_fru);
664 664
665 665 if (vd->vdev_isspare)
666 666 spa_spare_remove(vd);
667 667 if (vd->vdev_isl2cache)
668 668 spa_l2cache_remove(vd);
669 669
670 670 txg_list_destroy(&vd->vdev_ms_list);
671 671 txg_list_destroy(&vd->vdev_dtl_list);
672 672
673 673 mutex_enter(&vd->vdev_dtl_lock);
674 674 space_map_close(vd->vdev_dtl_sm);
675 675 for (int t = 0; t < DTL_TYPES; t++) {
676 676 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
677 677 range_tree_destroy(vd->vdev_dtl[t]);
678 678 }
679 679 mutex_exit(&vd->vdev_dtl_lock);
680 680
681 681 mutex_destroy(&vd->vdev_queue_lock);
682 682 mutex_destroy(&vd->vdev_dtl_lock);
683 683 mutex_destroy(&vd->vdev_stat_lock);
684 684 mutex_destroy(&vd->vdev_probe_lock);
685 685
686 686 if (vd == spa->spa_root_vdev)
687 687 spa->spa_root_vdev = NULL;
688 688
689 689 kmem_free(vd, sizeof (vdev_t));
690 690 }
691 691
692 692 /*
693 693 * Transfer top-level vdev state from svd to tvd.
694 694 */
695 695 static void
696 696 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
697 697 {
698 698 spa_t *spa = svd->vdev_spa;
699 699 metaslab_t *msp;
700 700 vdev_t *vd;
701 701 int t;
702 702
703 703 ASSERT(tvd == tvd->vdev_top);
704 704
705 705 tvd->vdev_ms_array = svd->vdev_ms_array;
706 706 tvd->vdev_ms_shift = svd->vdev_ms_shift;
707 707 tvd->vdev_ms_count = svd->vdev_ms_count;
708 708 tvd->vdev_top_zap = svd->vdev_top_zap;
709 709
710 710 svd->vdev_ms_array = 0;
711 711 svd->vdev_ms_shift = 0;
712 712 svd->vdev_ms_count = 0;
713 713 svd->vdev_top_zap = 0;
714 714
715 715 if (tvd->vdev_mg)
716 716 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
717 717 tvd->vdev_mg = svd->vdev_mg;
718 718 tvd->vdev_ms = svd->vdev_ms;
719 719
720 720 svd->vdev_mg = NULL;
721 721 svd->vdev_ms = NULL;
722 722
723 723 if (tvd->vdev_mg != NULL)
724 724 tvd->vdev_mg->mg_vd = tvd;
725 725
726 726 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
727 727 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
728 728 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
729 729
730 730 svd->vdev_stat.vs_alloc = 0;
731 731 svd->vdev_stat.vs_space = 0;
732 732 svd->vdev_stat.vs_dspace = 0;
733 733
734 734 for (t = 0; t < TXG_SIZE; t++) {
735 735 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
736 736 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
737 737 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
738 738 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
739 739 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
740 740 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
741 741 }
742 742
743 743 if (list_link_active(&svd->vdev_config_dirty_node)) {
744 744 vdev_config_clean(svd);
745 745 vdev_config_dirty(tvd);
746 746 }
747 747
748 748 if (list_link_active(&svd->vdev_state_dirty_node)) {
749 749 vdev_state_clean(svd);
750 750 vdev_state_dirty(tvd);
751 751 }
752 752
753 753 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
754 754 svd->vdev_deflate_ratio = 0;
755 755
756 756 tvd->vdev_islog = svd->vdev_islog;
757 757 svd->vdev_islog = 0;
758 758 }
759 759
760 760 static void
761 761 vdev_top_update(vdev_t *tvd, vdev_t *vd)
762 762 {
763 763 if (vd == NULL)
764 764 return;
765 765
766 766 vd->vdev_top = tvd;
767 767
768 768 for (int c = 0; c < vd->vdev_children; c++)
769 769 vdev_top_update(tvd, vd->vdev_child[c]);
770 770 }
771 771
772 772 /*
773 773 * Add a mirror/replacing vdev above an existing vdev.
774 774 */
775 775 vdev_t *
776 776 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
777 777 {
778 778 spa_t *spa = cvd->vdev_spa;
779 779 vdev_t *pvd = cvd->vdev_parent;
780 780 vdev_t *mvd;
781 781
782 782 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
783 783
784 784 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
785 785
786 786 mvd->vdev_asize = cvd->vdev_asize;
787 787 mvd->vdev_min_asize = cvd->vdev_min_asize;
788 788 mvd->vdev_max_asize = cvd->vdev_max_asize;
789 789 mvd->vdev_ashift = cvd->vdev_ashift;
790 790 mvd->vdev_state = cvd->vdev_state;
791 791 mvd->vdev_crtxg = cvd->vdev_crtxg;
792 792
793 793 vdev_remove_child(pvd, cvd);
794 794 vdev_add_child(pvd, mvd);
795 795 cvd->vdev_id = mvd->vdev_children;
796 796 vdev_add_child(mvd, cvd);
797 797 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
798 798
799 799 if (mvd == mvd->vdev_top)
800 800 vdev_top_transfer(cvd, mvd);
801 801
802 802 return (mvd);
803 803 }
804 804
805 805 /*
806 806 * Remove a 1-way mirror/replacing vdev from the tree.
807 807 */
808 808 void
809 809 vdev_remove_parent(vdev_t *cvd)
810 810 {
811 811 vdev_t *mvd = cvd->vdev_parent;
812 812 vdev_t *pvd = mvd->vdev_parent;
813 813
814 814 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
815 815
816 816 ASSERT(mvd->vdev_children == 1);
817 817 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
818 818 mvd->vdev_ops == &vdev_replacing_ops ||
819 819 mvd->vdev_ops == &vdev_spare_ops);
820 820 cvd->vdev_ashift = mvd->vdev_ashift;
821 821
822 822 vdev_remove_child(mvd, cvd);
823 823 vdev_remove_child(pvd, mvd);
824 824
825 825 /*
826 826 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
827 827 * Otherwise, we could have detached an offline device, and when we
828 828 * go to import the pool we'll think we have two top-level vdevs,
829 829 * instead of a different version of the same top-level vdev.
830 830 */
831 831 if (mvd->vdev_top == mvd) {
832 832 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
833 833 cvd->vdev_orig_guid = cvd->vdev_guid;
834 834 cvd->vdev_guid += guid_delta;
835 835 cvd->vdev_guid_sum += guid_delta;
836 836 }
837 837 cvd->vdev_id = mvd->vdev_id;
838 838 vdev_add_child(pvd, cvd);
839 839 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
840 840
841 841 if (cvd == cvd->vdev_top)
842 842 vdev_top_transfer(mvd, cvd);
843 843
844 844 ASSERT(mvd->vdev_children == 0);
845 845 vdev_free(mvd);
846 846 }
847 847
848 848 int
849 849 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
850 850 {
851 851 spa_t *spa = vd->vdev_spa;
852 852 objset_t *mos = spa->spa_meta_objset;
853 853 uint64_t m;
854 854 uint64_t oldc = vd->vdev_ms_count;
855 855 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
856 856 metaslab_t **mspp;
857 857 int error;
858 858
859 859 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
860 860
861 861 /*
862 862 * This vdev is not being allocated from yet or is a hole.
863 863 */
864 864 if (vd->vdev_ms_shift == 0)
865 865 return (0);
866 866
867 867 ASSERT(!vd->vdev_ishole);
868 868
869 869 /*
870 870 * Compute the raidz-deflation ratio. Note, we hard-code
871 871 * in 128k (1 << 17) because it is the "typical" blocksize.
872 872 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
873 873 * otherwise it would inconsistently account for existing bp's.
874 874 */
875 875 vd->vdev_deflate_ratio = (1 << 17) /
876 876 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
877 877
878 878 ASSERT(oldc <= newc);
879 879
880 880 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
881 881
882 882 if (oldc != 0) {
883 883 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
884 884 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
885 885 }
886 886
887 887 vd->vdev_ms = mspp;
888 888 vd->vdev_ms_count = newc;
889 889
890 890 for (m = oldc; m < newc; m++) {
891 891 uint64_t object = 0;
892 892
893 893 if (txg == 0) {
894 894 error = dmu_read(mos, vd->vdev_ms_array,
895 895 m * sizeof (uint64_t), sizeof (uint64_t), &object,
896 896 DMU_READ_PREFETCH);
897 897 if (error)
898 898 return (error);
899 899 }
900 900
901 901 error = metaslab_init(vd->vdev_mg, m, object, txg,
902 902 &(vd->vdev_ms[m]));
903 903 if (error)
904 904 return (error);
905 905 }
906 906
907 907 if (txg == 0)
908 908 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
909 909
910 910 /*
911 911 * If the vdev is being removed we don't activate
912 912 * the metaslabs since we want to ensure that no new
913 913 * allocations are performed on this device.
914 914 */
915 915 if (oldc == 0 && !vd->vdev_removing)
916 916 metaslab_group_activate(vd->vdev_mg);
917 917
918 918 if (txg == 0)
919 919 spa_config_exit(spa, SCL_ALLOC, FTAG);
920 920
921 921 return (0);
922 922 }
923 923
924 924 void
925 925 vdev_metaslab_fini(vdev_t *vd)
926 926 {
927 927 uint64_t m;
928 928 uint64_t count = vd->vdev_ms_count;
929 929
930 930 if (vd->vdev_ms != NULL) {
931 931 metaslab_group_passivate(vd->vdev_mg);
932 932 for (m = 0; m < count; m++) {
933 933 metaslab_t *msp = vd->vdev_ms[m];
934 934
935 935 if (msp != NULL)
936 936 metaslab_fini(msp);
937 937 }
938 938 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
939 939 vd->vdev_ms = NULL;
940 940 }
941 941 }
942 942
943 943 typedef struct vdev_probe_stats {
944 944 boolean_t vps_readable;
945 945 boolean_t vps_writeable;
946 946 int vps_flags;
947 947 } vdev_probe_stats_t;
948 948
949 949 static void
950 950 vdev_probe_done(zio_t *zio)
951 951 {
952 952 spa_t *spa = zio->io_spa;
953 953 vdev_t *vd = zio->io_vd;
954 954 vdev_probe_stats_t *vps = zio->io_private;
955 955
956 956 ASSERT(vd->vdev_probe_zio != NULL);
957 957
958 958 if (zio->io_type == ZIO_TYPE_READ) {
959 959 if (zio->io_error == 0)
960 960 vps->vps_readable = 1;
961 961 if (zio->io_error == 0 && spa_writeable(spa)) {
962 962 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
963 963 zio->io_offset, zio->io_size, zio->io_data,
964 964 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
965 965 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
966 966 } else {
967 967 zio_buf_free(zio->io_data, zio->io_size);
968 968 }
969 969 } else if (zio->io_type == ZIO_TYPE_WRITE) {
970 970 if (zio->io_error == 0)
971 971 vps->vps_writeable = 1;
972 972 zio_buf_free(zio->io_data, zio->io_size);
973 973 } else if (zio->io_type == ZIO_TYPE_NULL) {
974 974 zio_t *pio;
975 975
976 976 vd->vdev_cant_read |= !vps->vps_readable;
977 977 vd->vdev_cant_write |= !vps->vps_writeable;
978 978
979 979 if (vdev_readable(vd) &&
980 980 (vdev_writeable(vd) || !spa_writeable(spa))) {
981 981 zio->io_error = 0;
982 982 } else {
983 983 ASSERT(zio->io_error != 0);
984 984 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
985 985 spa, vd, NULL, 0, 0);
986 986 zio->io_error = SET_ERROR(ENXIO);
987 987 }
988 988
989 989 mutex_enter(&vd->vdev_probe_lock);
990 990 ASSERT(vd->vdev_probe_zio == zio);
991 991 vd->vdev_probe_zio = NULL;
992 992 mutex_exit(&vd->vdev_probe_lock);
993 993
994 994 zio_link_t *zl = NULL;
995 995 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
996 996 if (!vdev_accessible(vd, pio))
997 997 pio->io_error = SET_ERROR(ENXIO);
998 998
999 999 kmem_free(vps, sizeof (*vps));
1000 1000 }
1001 1001 }
1002 1002
1003 1003 /*
1004 1004 * Determine whether this device is accessible.
1005 1005 *
1006 1006 * Read and write to several known locations: the pad regions of each
1007 1007 * vdev label but the first, which we leave alone in case it contains
1008 1008 * a VTOC.
1009 1009 */
1010 1010 zio_t *
1011 1011 vdev_probe(vdev_t *vd, zio_t *zio)
1012 1012 {
1013 1013 spa_t *spa = vd->vdev_spa;
1014 1014 vdev_probe_stats_t *vps = NULL;
1015 1015 zio_t *pio;
1016 1016
1017 1017 ASSERT(vd->vdev_ops->vdev_op_leaf);
1018 1018
1019 1019 /*
1020 1020 * Don't probe the probe.
1021 1021 */
1022 1022 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1023 1023 return (NULL);
1024 1024
1025 1025 /*
1026 1026 * To prevent 'probe storms' when a device fails, we create
1027 1027 * just one probe i/o at a time. All zios that want to probe
1028 1028 * this vdev will become parents of the probe io.
1029 1029 */
1030 1030 mutex_enter(&vd->vdev_probe_lock);
1031 1031
1032 1032 if ((pio = vd->vdev_probe_zio) == NULL) {
1033 1033 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1034 1034
1035 1035 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1036 1036 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1037 1037 ZIO_FLAG_TRYHARD;
1038 1038
1039 1039 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1040 1040 /*
1041 1041 * vdev_cant_read and vdev_cant_write can only
1042 1042 * transition from TRUE to FALSE when we have the
1043 1043 * SCL_ZIO lock as writer; otherwise they can only
1044 1044 * transition from FALSE to TRUE. This ensures that
1045 1045 * any zio looking at these values can assume that
1046 1046 * failures persist for the life of the I/O. That's
1047 1047 * important because when a device has intermittent
1048 1048 * connectivity problems, we want to ensure that
1049 1049 * they're ascribed to the device (ENXIO) and not
1050 1050 * the zio (EIO).
1051 1051 *
1052 1052 * Since we hold SCL_ZIO as writer here, clear both
1053 1053 * values so the probe can reevaluate from first
1054 1054 * principles.
1055 1055 */
1056 1056 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1057 1057 vd->vdev_cant_read = B_FALSE;
1058 1058 vd->vdev_cant_write = B_FALSE;
1059 1059 }
1060 1060
1061 1061 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1062 1062 vdev_probe_done, vps,
1063 1063 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1064 1064
1065 1065 /*
1066 1066 * We can't change the vdev state in this context, so we
1067 1067 * kick off an async task to do it on our behalf.
1068 1068 */
1069 1069 if (zio != NULL) {
1070 1070 vd->vdev_probe_wanted = B_TRUE;
1071 1071 spa_async_request(spa, SPA_ASYNC_PROBE);
1072 1072 }
1073 1073 }
1074 1074
1075 1075 if (zio != NULL)
1076 1076 zio_add_child(zio, pio);
1077 1077
1078 1078 mutex_exit(&vd->vdev_probe_lock);
1079 1079
1080 1080 if (vps == NULL) {
1081 1081 ASSERT(zio != NULL);
1082 1082 return (NULL);
1083 1083 }
1084 1084
1085 1085 for (int l = 1; l < VDEV_LABELS; l++) {
1086 1086 zio_nowait(zio_read_phys(pio, vd,
1087 1087 vdev_label_offset(vd->vdev_psize, l,
1088 1088 offsetof(vdev_label_t, vl_pad2)),
1089 1089 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1090 1090 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1091 1091 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1092 1092 }
1093 1093
1094 1094 if (zio == NULL)
1095 1095 return (pio);
1096 1096
1097 1097 zio_nowait(pio);
1098 1098 return (NULL);
↓ open down ↓ |
1098 lines elided |
↑ open up ↑ |
1099 1099 }
1100 1100
1101 1101 static void
1102 1102 vdev_open_child(void *arg)
1103 1103 {
1104 1104 vdev_t *vd = arg;
1105 1105
1106 1106 vd->vdev_open_thread = curthread;
1107 1107 vd->vdev_open_error = vdev_open(vd);
1108 1108 vd->vdev_open_thread = NULL;
1109 + vd->vdev_parent->vdev_nonrot &= vd->vdev_nonrot;
1109 1110 }
1110 1111
1111 1112 boolean_t
1112 1113 vdev_uses_zvols(vdev_t *vd)
1113 1114 {
1114 1115 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1115 1116 strlen(ZVOL_DIR)) == 0)
1116 1117 return (B_TRUE);
1117 1118 for (int c = 0; c < vd->vdev_children; c++)
1118 1119 if (vdev_uses_zvols(vd->vdev_child[c]))
1119 1120 return (B_TRUE);
1120 1121 return (B_FALSE);
1121 1122 }
1122 1123
1123 1124 void
1124 1125 vdev_open_children(vdev_t *vd)
1125 1126 {
1126 1127 taskq_t *tq;
1127 1128 int children = vd->vdev_children;
1128 1129
1130 + vd->vdev_nonrot = B_TRUE;
1131 +
1129 1132 /*
1130 1133 * in order to handle pools on top of zvols, do the opens
1131 1134 * in a single thread so that the same thread holds the
1132 1135 * spa_namespace_lock
1133 1136 */
1134 1137 if (vdev_uses_zvols(vd)) {
1135 - for (int c = 0; c < children; c++)
1138 + for (int c = 0; c < children; c++) {
1136 1139 vd->vdev_child[c]->vdev_open_error =
1137 1140 vdev_open(vd->vdev_child[c]);
1141 + vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
1142 + }
1138 1143 return;
1139 1144 }
1140 1145 tq = taskq_create("vdev_open", children, minclsyspri,
1141 1146 children, children, TASKQ_PREPOPULATE);
1142 1147
1143 1148 for (int c = 0; c < children; c++)
1144 1149 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1145 1150 TQ_SLEEP) != NULL);
1146 1151
1147 1152 taskq_destroy(tq);
1153 +
1154 + for (int c = 0; c < children; c++)
1155 + vd->vdev_nonrot &= vd->vdev_child[c]->vdev_nonrot;
1148 1156 }
1149 1157
1150 1158 /*
1151 1159 * Prepare a virtual device for access.
1152 1160 */
1153 1161 int
1154 1162 vdev_open(vdev_t *vd)
1155 1163 {
1156 1164 spa_t *spa = vd->vdev_spa;
1157 1165 int error;
1158 1166 uint64_t osize = 0;
1159 1167 uint64_t max_osize = 0;
1160 1168 uint64_t asize, max_asize, psize;
1161 1169 uint64_t ashift = 0;
1162 1170
1163 1171 ASSERT(vd->vdev_open_thread == curthread ||
1164 1172 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1165 1173 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1166 1174 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1167 1175 vd->vdev_state == VDEV_STATE_OFFLINE);
1168 1176
1169 1177 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1170 1178 vd->vdev_cant_read = B_FALSE;
1171 1179 vd->vdev_cant_write = B_FALSE;
1172 1180 vd->vdev_min_asize = vdev_get_min_asize(vd);
1173 1181
1174 1182 /*
1175 1183 * If this vdev is not removed, check its fault status. If it's
1176 1184 * faulted, bail out of the open.
1177 1185 */
1178 1186 if (!vd->vdev_removed && vd->vdev_faulted) {
1179 1187 ASSERT(vd->vdev_children == 0);
1180 1188 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1181 1189 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1182 1190 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1183 1191 vd->vdev_label_aux);
1184 1192 return (SET_ERROR(ENXIO));
1185 1193 } else if (vd->vdev_offline) {
1186 1194 ASSERT(vd->vdev_children == 0);
1187 1195 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1188 1196 return (SET_ERROR(ENXIO));
1189 1197 }
1190 1198
1191 1199 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1192 1200
1193 1201 /*
1194 1202 * Reset the vdev_reopening flag so that we actually close
1195 1203 * the vdev on error.
1196 1204 */
1197 1205 vd->vdev_reopening = B_FALSE;
1198 1206 if (zio_injection_enabled && error == 0)
1199 1207 error = zio_handle_device_injection(vd, NULL, ENXIO);
1200 1208
1201 1209 if (error) {
1202 1210 if (vd->vdev_removed &&
1203 1211 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1204 1212 vd->vdev_removed = B_FALSE;
1205 1213
1206 1214 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1207 1215 vd->vdev_stat.vs_aux);
1208 1216 return (error);
1209 1217 }
1210 1218
1211 1219 vd->vdev_removed = B_FALSE;
1212 1220
1213 1221 /*
1214 1222 * Recheck the faulted flag now that we have confirmed that
1215 1223 * the vdev is accessible. If we're faulted, bail.
1216 1224 */
1217 1225 if (vd->vdev_faulted) {
1218 1226 ASSERT(vd->vdev_children == 0);
1219 1227 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1220 1228 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1221 1229 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1222 1230 vd->vdev_label_aux);
1223 1231 return (SET_ERROR(ENXIO));
1224 1232 }
1225 1233
1226 1234 if (vd->vdev_degraded) {
1227 1235 ASSERT(vd->vdev_children == 0);
1228 1236 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1229 1237 VDEV_AUX_ERR_EXCEEDED);
1230 1238 } else {
1231 1239 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1232 1240 }
1233 1241
1234 1242 /*
1235 1243 * For hole or missing vdevs we just return success.
1236 1244 */
1237 1245 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1238 1246 return (0);
1239 1247
1240 1248 for (int c = 0; c < vd->vdev_children; c++) {
1241 1249 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1242 1250 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1243 1251 VDEV_AUX_NONE);
1244 1252 break;
1245 1253 }
1246 1254 }
1247 1255
1248 1256 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1249 1257 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1250 1258
1251 1259 if (vd->vdev_children == 0) {
1252 1260 if (osize < SPA_MINDEVSIZE) {
1253 1261 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1254 1262 VDEV_AUX_TOO_SMALL);
1255 1263 return (SET_ERROR(EOVERFLOW));
1256 1264 }
1257 1265 psize = osize;
1258 1266 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1259 1267 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1260 1268 VDEV_LABEL_END_SIZE);
1261 1269 } else {
1262 1270 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1263 1271 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1264 1272 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1265 1273 VDEV_AUX_TOO_SMALL);
1266 1274 return (SET_ERROR(EOVERFLOW));
1267 1275 }
1268 1276 psize = 0;
1269 1277 asize = osize;
1270 1278 max_asize = max_osize;
1271 1279 }
1272 1280
1273 1281 vd->vdev_psize = psize;
1274 1282
1275 1283 /*
1276 1284 * Make sure the allocatable size hasn't shrunk.
1277 1285 */
1278 1286 if (asize < vd->vdev_min_asize) {
1279 1287 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1280 1288 VDEV_AUX_BAD_LABEL);
1281 1289 return (SET_ERROR(EINVAL));
1282 1290 }
1283 1291
1284 1292 if (vd->vdev_asize == 0) {
1285 1293 /*
1286 1294 * This is the first-ever open, so use the computed values.
1287 1295 * For testing purposes, a higher ashift can be requested.
1288 1296 */
1289 1297 vd->vdev_asize = asize;
1290 1298 vd->vdev_max_asize = max_asize;
1291 1299 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1292 1300 } else {
1293 1301 /*
1294 1302 * Detect if the alignment requirement has increased.
1295 1303 * We don't want to make the pool unavailable, just
1296 1304 * issue a warning instead.
1297 1305 */
1298 1306 if (ashift > vd->vdev_top->vdev_ashift &&
1299 1307 vd->vdev_ops->vdev_op_leaf) {
1300 1308 cmn_err(CE_WARN,
1301 1309 "Disk, '%s', has a block alignment that is "
1302 1310 "larger than the pool's alignment\n",
1303 1311 vd->vdev_path);
1304 1312 }
1305 1313 vd->vdev_max_asize = max_asize;
1306 1314 }
1307 1315
1308 1316 /*
1309 1317 * If all children are healthy and the asize has increased,
1310 1318 * then we've experienced dynamic LUN growth. If automatic
1311 1319 * expansion is enabled then use the additional space.
1312 1320 */
1313 1321 if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1314 1322 (vd->vdev_expanding || spa->spa_autoexpand))
1315 1323 vd->vdev_asize = asize;
1316 1324
1317 1325 vdev_set_min_asize(vd);
1318 1326
1319 1327 /*
1320 1328 * Ensure we can issue some IO before declaring the
1321 1329 * vdev open for business.
1322 1330 */
1323 1331 if (vd->vdev_ops->vdev_op_leaf &&
1324 1332 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1325 1333 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1326 1334 VDEV_AUX_ERR_EXCEEDED);
1327 1335 return (error);
1328 1336 }
1329 1337
1330 1338 /*
1331 1339 * Track the min and max ashift values for normal data devices.
1332 1340 */
1333 1341 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1334 1342 !vd->vdev_islog && vd->vdev_aux == NULL) {
1335 1343 if (vd->vdev_ashift > spa->spa_max_ashift)
1336 1344 spa->spa_max_ashift = vd->vdev_ashift;
1337 1345 if (vd->vdev_ashift < spa->spa_min_ashift)
1338 1346 spa->spa_min_ashift = vd->vdev_ashift;
1339 1347 }
1340 1348
1341 1349 /*
1342 1350 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1343 1351 * resilver. But don't do this if we are doing a reopen for a scrub,
1344 1352 * since this would just restart the scrub we are already doing.
1345 1353 */
1346 1354 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1347 1355 vdev_resilver_needed(vd, NULL, NULL))
1348 1356 spa_async_request(spa, SPA_ASYNC_RESILVER);
1349 1357
1350 1358 return (0);
1351 1359 }
1352 1360
1353 1361 /*
1354 1362 * Called once the vdevs are all opened, this routine validates the label
1355 1363 * contents. This needs to be done before vdev_load() so that we don't
1356 1364 * inadvertently do repair I/Os to the wrong device.
1357 1365 *
1358 1366 * If 'strict' is false ignore the spa guid check. This is necessary because
1359 1367 * if the machine crashed during a re-guid the new guid might have been written
1360 1368 * to all of the vdev labels, but not the cached config. The strict check
1361 1369 * will be performed when the pool is opened again using the mos config.
1362 1370 *
1363 1371 * This function will only return failure if one of the vdevs indicates that it
1364 1372 * has since been destroyed or exported. This is only possible if
1365 1373 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1366 1374 * will be updated but the function will return 0.
1367 1375 */
1368 1376 int
1369 1377 vdev_validate(vdev_t *vd, boolean_t strict)
1370 1378 {
1371 1379 spa_t *spa = vd->vdev_spa;
1372 1380 nvlist_t *label;
1373 1381 uint64_t guid = 0, top_guid;
1374 1382 uint64_t state;
1375 1383
1376 1384 for (int c = 0; c < vd->vdev_children; c++)
1377 1385 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1378 1386 return (SET_ERROR(EBADF));
1379 1387
1380 1388 /*
1381 1389 * If the device has already failed, or was marked offline, don't do
1382 1390 * any further validation. Otherwise, label I/O will fail and we will
1383 1391 * overwrite the previous state.
1384 1392 */
1385 1393 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1386 1394 uint64_t aux_guid = 0;
1387 1395 nvlist_t *nvl;
1388 1396 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1389 1397 spa_last_synced_txg(spa) : -1ULL;
1390 1398
1391 1399 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1392 1400 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1393 1401 VDEV_AUX_BAD_LABEL);
1394 1402 return (0);
1395 1403 }
1396 1404
1397 1405 /*
1398 1406 * Determine if this vdev has been split off into another
1399 1407 * pool. If so, then refuse to open it.
1400 1408 */
1401 1409 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1402 1410 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1403 1411 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1404 1412 VDEV_AUX_SPLIT_POOL);
1405 1413 nvlist_free(label);
1406 1414 return (0);
1407 1415 }
1408 1416
1409 1417 if (strict && (nvlist_lookup_uint64(label,
1410 1418 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1411 1419 guid != spa_guid(spa))) {
1412 1420 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1413 1421 VDEV_AUX_CORRUPT_DATA);
1414 1422 nvlist_free(label);
1415 1423 return (0);
1416 1424 }
1417 1425
1418 1426 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1419 1427 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1420 1428 &aux_guid) != 0)
1421 1429 aux_guid = 0;
1422 1430
1423 1431 /*
1424 1432 * If this vdev just became a top-level vdev because its
1425 1433 * sibling was detached, it will have adopted the parent's
1426 1434 * vdev guid -- but the label may or may not be on disk yet.
1427 1435 * Fortunately, either version of the label will have the
1428 1436 * same top guid, so if we're a top-level vdev, we can
1429 1437 * safely compare to that instead.
1430 1438 *
1431 1439 * If we split this vdev off instead, then we also check the
1432 1440 * original pool's guid. We don't want to consider the vdev
1433 1441 * corrupt if it is partway through a split operation.
1434 1442 */
1435 1443 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1436 1444 &guid) != 0 ||
1437 1445 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1438 1446 &top_guid) != 0 ||
1439 1447 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1440 1448 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1441 1449 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1442 1450 VDEV_AUX_CORRUPT_DATA);
1443 1451 nvlist_free(label);
1444 1452 return (0);
1445 1453 }
1446 1454
1447 1455 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1448 1456 &state) != 0) {
1449 1457 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1450 1458 VDEV_AUX_CORRUPT_DATA);
1451 1459 nvlist_free(label);
1452 1460 return (0);
1453 1461 }
1454 1462
1455 1463 nvlist_free(label);
1456 1464
1457 1465 /*
1458 1466 * If this is a verbatim import, no need to check the
1459 1467 * state of the pool.
1460 1468 */
1461 1469 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1462 1470 spa_load_state(spa) == SPA_LOAD_OPEN &&
1463 1471 state != POOL_STATE_ACTIVE)
1464 1472 return (SET_ERROR(EBADF));
1465 1473
1466 1474 /*
1467 1475 * If we were able to open and validate a vdev that was
1468 1476 * previously marked permanently unavailable, clear that state
1469 1477 * now.
1470 1478 */
1471 1479 if (vd->vdev_not_present)
1472 1480 vd->vdev_not_present = 0;
1473 1481 }
1474 1482
1475 1483 return (0);
1476 1484 }
1477 1485
1478 1486 /*
1479 1487 * Close a virtual device.
1480 1488 */
1481 1489 void
1482 1490 vdev_close(vdev_t *vd)
1483 1491 {
1484 1492 spa_t *spa = vd->vdev_spa;
1485 1493 vdev_t *pvd = vd->vdev_parent;
1486 1494
1487 1495 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1488 1496
1489 1497 /*
1490 1498 * If our parent is reopening, then we are as well, unless we are
1491 1499 * going offline.
1492 1500 */
1493 1501 if (pvd != NULL && pvd->vdev_reopening)
1494 1502 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1495 1503
1496 1504 vd->vdev_ops->vdev_op_close(vd);
1497 1505
1498 1506 vdev_cache_purge(vd);
1499 1507
1500 1508 /*
1501 1509 * We record the previous state before we close it, so that if we are
1502 1510 * doing a reopen(), we don't generate FMA ereports if we notice that
1503 1511 * it's still faulted.
1504 1512 */
1505 1513 vd->vdev_prevstate = vd->vdev_state;
1506 1514
1507 1515 if (vd->vdev_offline)
1508 1516 vd->vdev_state = VDEV_STATE_OFFLINE;
1509 1517 else
1510 1518 vd->vdev_state = VDEV_STATE_CLOSED;
1511 1519 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1512 1520 }
1513 1521
1514 1522 void
1515 1523 vdev_hold(vdev_t *vd)
1516 1524 {
1517 1525 spa_t *spa = vd->vdev_spa;
1518 1526
1519 1527 ASSERT(spa_is_root(spa));
1520 1528 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1521 1529 return;
1522 1530
1523 1531 for (int c = 0; c < vd->vdev_children; c++)
1524 1532 vdev_hold(vd->vdev_child[c]);
1525 1533
1526 1534 if (vd->vdev_ops->vdev_op_leaf)
1527 1535 vd->vdev_ops->vdev_op_hold(vd);
1528 1536 }
1529 1537
1530 1538 void
1531 1539 vdev_rele(vdev_t *vd)
1532 1540 {
1533 1541 spa_t *spa = vd->vdev_spa;
1534 1542
1535 1543 ASSERT(spa_is_root(spa));
1536 1544 for (int c = 0; c < vd->vdev_children; c++)
1537 1545 vdev_rele(vd->vdev_child[c]);
1538 1546
1539 1547 if (vd->vdev_ops->vdev_op_leaf)
1540 1548 vd->vdev_ops->vdev_op_rele(vd);
1541 1549 }
1542 1550
1543 1551 /*
1544 1552 * Reopen all interior vdevs and any unopened leaves. We don't actually
1545 1553 * reopen leaf vdevs which had previously been opened as they might deadlock
1546 1554 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1547 1555 * If the leaf has never been opened then open it, as usual.
1548 1556 */
1549 1557 void
1550 1558 vdev_reopen(vdev_t *vd)
1551 1559 {
1552 1560 spa_t *spa = vd->vdev_spa;
1553 1561
1554 1562 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1555 1563
1556 1564 /* set the reopening flag unless we're taking the vdev offline */
1557 1565 vd->vdev_reopening = !vd->vdev_offline;
1558 1566 vdev_close(vd);
1559 1567 (void) vdev_open(vd);
1560 1568
1561 1569 /*
1562 1570 * Call vdev_validate() here to make sure we have the same device.
1563 1571 * Otherwise, a device with an invalid label could be successfully
1564 1572 * opened in response to vdev_reopen().
1565 1573 */
1566 1574 if (vd->vdev_aux) {
1567 1575 (void) vdev_validate_aux(vd);
1568 1576 if (vdev_readable(vd) && vdev_writeable(vd) &&
1569 1577 vd->vdev_aux == &spa->spa_l2cache &&
1570 1578 !l2arc_vdev_present(vd))
1571 1579 l2arc_add_vdev(spa, vd);
1572 1580 } else {
1573 1581 (void) vdev_validate(vd, B_TRUE);
1574 1582 }
1575 1583
1576 1584 /*
1577 1585 * Reassess parent vdev's health.
1578 1586 */
1579 1587 vdev_propagate_state(vd);
1580 1588 }
1581 1589
1582 1590 int
1583 1591 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1584 1592 {
1585 1593 int error;
1586 1594
1587 1595 /*
1588 1596 * Normally, partial opens (e.g. of a mirror) are allowed.
1589 1597 * For a create, however, we want to fail the request if
1590 1598 * there are any components we can't open.
1591 1599 */
1592 1600 error = vdev_open(vd);
1593 1601
1594 1602 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1595 1603 vdev_close(vd);
1596 1604 return (error ? error : ENXIO);
1597 1605 }
1598 1606
1599 1607 /*
1600 1608 * Recursively load DTLs and initialize all labels.
1601 1609 */
1602 1610 if ((error = vdev_dtl_load(vd)) != 0 ||
1603 1611 (error = vdev_label_init(vd, txg, isreplacing ?
1604 1612 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1605 1613 vdev_close(vd);
1606 1614 return (error);
1607 1615 }
1608 1616
1609 1617 return (0);
1610 1618 }
1611 1619
1612 1620 void
1613 1621 vdev_metaslab_set_size(vdev_t *vd)
1614 1622 {
1615 1623 /*
1616 1624 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1617 1625 */
1618 1626 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1619 1627 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1620 1628 }
1621 1629
1622 1630 void
1623 1631 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1624 1632 {
1625 1633 ASSERT(vd == vd->vdev_top);
1626 1634 ASSERT(!vd->vdev_ishole);
1627 1635 ASSERT(ISP2(flags));
1628 1636 ASSERT(spa_writeable(vd->vdev_spa));
1629 1637
1630 1638 if (flags & VDD_METASLAB)
1631 1639 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1632 1640
1633 1641 if (flags & VDD_DTL)
1634 1642 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1635 1643
1636 1644 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1637 1645 }
1638 1646
1639 1647 void
1640 1648 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1641 1649 {
1642 1650 for (int c = 0; c < vd->vdev_children; c++)
1643 1651 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1644 1652
1645 1653 if (vd->vdev_ops->vdev_op_leaf)
1646 1654 vdev_dirty(vd->vdev_top, flags, vd, txg);
1647 1655 }
1648 1656
1649 1657 /*
1650 1658 * DTLs.
1651 1659 *
1652 1660 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1653 1661 * the vdev has less than perfect replication. There are four kinds of DTL:
1654 1662 *
1655 1663 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1656 1664 *
1657 1665 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1658 1666 *
1659 1667 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1660 1668 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1661 1669 * txgs that was scrubbed.
1662 1670 *
1663 1671 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1664 1672 * persistent errors or just some device being offline.
1665 1673 * Unlike the other three, the DTL_OUTAGE map is not generally
1666 1674 * maintained; it's only computed when needed, typically to
1667 1675 * determine whether a device can be detached.
1668 1676 *
1669 1677 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1670 1678 * either has the data or it doesn't.
1671 1679 *
1672 1680 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1673 1681 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1674 1682 * if any child is less than fully replicated, then so is its parent.
1675 1683 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1676 1684 * comprising only those txgs which appear in 'maxfaults' or more children;
1677 1685 * those are the txgs we don't have enough replication to read. For example,
1678 1686 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1679 1687 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1680 1688 * two child DTL_MISSING maps.
1681 1689 *
1682 1690 * It should be clear from the above that to compute the DTLs and outage maps
1683 1691 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1684 1692 * Therefore, that is all we keep on disk. When loading the pool, or after
1685 1693 * a configuration change, we generate all other DTLs from first principles.
1686 1694 */
1687 1695 void
1688 1696 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1689 1697 {
1690 1698 range_tree_t *rt = vd->vdev_dtl[t];
1691 1699
1692 1700 ASSERT(t < DTL_TYPES);
1693 1701 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1694 1702 ASSERT(spa_writeable(vd->vdev_spa));
1695 1703
1696 1704 mutex_enter(rt->rt_lock);
1697 1705 if (!range_tree_contains(rt, txg, size))
1698 1706 range_tree_add(rt, txg, size);
1699 1707 mutex_exit(rt->rt_lock);
1700 1708 }
1701 1709
1702 1710 boolean_t
1703 1711 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1704 1712 {
1705 1713 range_tree_t *rt = vd->vdev_dtl[t];
1706 1714 boolean_t dirty = B_FALSE;
1707 1715
1708 1716 ASSERT(t < DTL_TYPES);
1709 1717 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1710 1718
1711 1719 mutex_enter(rt->rt_lock);
1712 1720 if (range_tree_space(rt) != 0)
1713 1721 dirty = range_tree_contains(rt, txg, size);
1714 1722 mutex_exit(rt->rt_lock);
1715 1723
1716 1724 return (dirty);
1717 1725 }
1718 1726
1719 1727 boolean_t
1720 1728 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1721 1729 {
1722 1730 range_tree_t *rt = vd->vdev_dtl[t];
1723 1731 boolean_t empty;
1724 1732
1725 1733 mutex_enter(rt->rt_lock);
1726 1734 empty = (range_tree_space(rt) == 0);
1727 1735 mutex_exit(rt->rt_lock);
1728 1736
1729 1737 return (empty);
1730 1738 }
1731 1739
1732 1740 /*
1733 1741 * Returns the lowest txg in the DTL range.
1734 1742 */
1735 1743 static uint64_t
1736 1744 vdev_dtl_min(vdev_t *vd)
1737 1745 {
1738 1746 range_seg_t *rs;
1739 1747
1740 1748 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1741 1749 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1742 1750 ASSERT0(vd->vdev_children);
1743 1751
1744 1752 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1745 1753 return (rs->rs_start - 1);
1746 1754 }
1747 1755
1748 1756 /*
1749 1757 * Returns the highest txg in the DTL.
1750 1758 */
1751 1759 static uint64_t
1752 1760 vdev_dtl_max(vdev_t *vd)
1753 1761 {
1754 1762 range_seg_t *rs;
1755 1763
1756 1764 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1757 1765 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1758 1766 ASSERT0(vd->vdev_children);
1759 1767
1760 1768 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1761 1769 return (rs->rs_end);
1762 1770 }
1763 1771
1764 1772 /*
1765 1773 * Determine if a resilvering vdev should remove any DTL entries from
1766 1774 * its range. If the vdev was resilvering for the entire duration of the
1767 1775 * scan then it should excise that range from its DTLs. Otherwise, this
1768 1776 * vdev is considered partially resilvered and should leave its DTL
1769 1777 * entries intact. The comment in vdev_dtl_reassess() describes how we
1770 1778 * excise the DTLs.
1771 1779 */
1772 1780 static boolean_t
1773 1781 vdev_dtl_should_excise(vdev_t *vd)
1774 1782 {
1775 1783 spa_t *spa = vd->vdev_spa;
1776 1784 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1777 1785
1778 1786 ASSERT0(scn->scn_phys.scn_errors);
1779 1787 ASSERT0(vd->vdev_children);
1780 1788
1781 1789 if (vd->vdev_resilver_txg == 0 ||
1782 1790 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1783 1791 return (B_TRUE);
1784 1792
1785 1793 /*
1786 1794 * When a resilver is initiated the scan will assign the scn_max_txg
1787 1795 * value to the highest txg value that exists in all DTLs. If this
1788 1796 * device's max DTL is not part of this scan (i.e. it is not in
1789 1797 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1790 1798 * for excision.
1791 1799 */
1792 1800 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1793 1801 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1794 1802 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1795 1803 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1796 1804 return (B_TRUE);
1797 1805 }
1798 1806 return (B_FALSE);
1799 1807 }
1800 1808
1801 1809 /*
1802 1810 * Reassess DTLs after a config change or scrub completion.
1803 1811 */
1804 1812 void
1805 1813 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1806 1814 {
1807 1815 spa_t *spa = vd->vdev_spa;
1808 1816 avl_tree_t reftree;
1809 1817 int minref;
1810 1818
1811 1819 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1812 1820
1813 1821 for (int c = 0; c < vd->vdev_children; c++)
1814 1822 vdev_dtl_reassess(vd->vdev_child[c], txg,
1815 1823 scrub_txg, scrub_done);
1816 1824
1817 1825 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1818 1826 return;
1819 1827
1820 1828 if (vd->vdev_ops->vdev_op_leaf) {
1821 1829 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1822 1830
1823 1831 mutex_enter(&vd->vdev_dtl_lock);
1824 1832
1825 1833 /*
1826 1834 * If we've completed a scan cleanly then determine
1827 1835 * if this vdev should remove any DTLs. We only want to
1828 1836 * excise regions on vdevs that were available during
1829 1837 * the entire duration of this scan.
1830 1838 */
1831 1839 if (scrub_txg != 0 &&
1832 1840 (spa->spa_scrub_started ||
1833 1841 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1834 1842 vdev_dtl_should_excise(vd)) {
1835 1843 /*
1836 1844 * We completed a scrub up to scrub_txg. If we
1837 1845 * did it without rebooting, then the scrub dtl
1838 1846 * will be valid, so excise the old region and
1839 1847 * fold in the scrub dtl. Otherwise, leave the
1840 1848 * dtl as-is if there was an error.
1841 1849 *
1842 1850 * There's little trick here: to excise the beginning
1843 1851 * of the DTL_MISSING map, we put it into a reference
1844 1852 * tree and then add a segment with refcnt -1 that
1845 1853 * covers the range [0, scrub_txg). This means
1846 1854 * that each txg in that range has refcnt -1 or 0.
1847 1855 * We then add DTL_SCRUB with a refcnt of 2, so that
1848 1856 * entries in the range [0, scrub_txg) will have a
1849 1857 * positive refcnt -- either 1 or 2. We then convert
1850 1858 * the reference tree into the new DTL_MISSING map.
1851 1859 */
1852 1860 space_reftree_create(&reftree);
1853 1861 space_reftree_add_map(&reftree,
1854 1862 vd->vdev_dtl[DTL_MISSING], 1);
1855 1863 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
1856 1864 space_reftree_add_map(&reftree,
1857 1865 vd->vdev_dtl[DTL_SCRUB], 2);
1858 1866 space_reftree_generate_map(&reftree,
1859 1867 vd->vdev_dtl[DTL_MISSING], 1);
1860 1868 space_reftree_destroy(&reftree);
1861 1869 }
1862 1870 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1863 1871 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1864 1872 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1865 1873 if (scrub_done)
1866 1874 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1867 1875 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1868 1876 if (!vdev_readable(vd))
1869 1877 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1870 1878 else
1871 1879 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1872 1880 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1873 1881
1874 1882 /*
1875 1883 * If the vdev was resilvering and no longer has any
1876 1884 * DTLs then reset its resilvering flag.
1877 1885 */
1878 1886 if (vd->vdev_resilver_txg != 0 &&
1879 1887 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
1880 1888 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1881 1889 vd->vdev_resilver_txg = 0;
1882 1890
1883 1891 mutex_exit(&vd->vdev_dtl_lock);
1884 1892
1885 1893 if (txg != 0)
1886 1894 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1887 1895 return;
1888 1896 }
1889 1897
1890 1898 mutex_enter(&vd->vdev_dtl_lock);
1891 1899 for (int t = 0; t < DTL_TYPES; t++) {
1892 1900 /* account for child's outage in parent's missing map */
1893 1901 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1894 1902 if (t == DTL_SCRUB)
1895 1903 continue; /* leaf vdevs only */
1896 1904 if (t == DTL_PARTIAL)
1897 1905 minref = 1; /* i.e. non-zero */
1898 1906 else if (vd->vdev_nparity != 0)
1899 1907 minref = vd->vdev_nparity + 1; /* RAID-Z */
1900 1908 else
1901 1909 minref = vd->vdev_children; /* any kind of mirror */
1902 1910 space_reftree_create(&reftree);
1903 1911 for (int c = 0; c < vd->vdev_children; c++) {
1904 1912 vdev_t *cvd = vd->vdev_child[c];
1905 1913 mutex_enter(&cvd->vdev_dtl_lock);
1906 1914 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1907 1915 mutex_exit(&cvd->vdev_dtl_lock);
1908 1916 }
1909 1917 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
1910 1918 space_reftree_destroy(&reftree);
1911 1919 }
1912 1920 mutex_exit(&vd->vdev_dtl_lock);
1913 1921 }
1914 1922
1915 1923 int
1916 1924 vdev_dtl_load(vdev_t *vd)
1917 1925 {
1918 1926 spa_t *spa = vd->vdev_spa;
1919 1927 objset_t *mos = spa->spa_meta_objset;
1920 1928 int error = 0;
1921 1929
1922 1930 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
1923 1931 ASSERT(!vd->vdev_ishole);
1924 1932
1925 1933 error = space_map_open(&vd->vdev_dtl_sm, mos,
1926 1934 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
1927 1935 if (error)
1928 1936 return (error);
1929 1937 ASSERT(vd->vdev_dtl_sm != NULL);
1930 1938
1931 1939 mutex_enter(&vd->vdev_dtl_lock);
1932 1940
1933 1941 /*
1934 1942 * Now that we've opened the space_map we need to update
1935 1943 * the in-core DTL.
1936 1944 */
1937 1945 space_map_update(vd->vdev_dtl_sm);
1938 1946
1939 1947 error = space_map_load(vd->vdev_dtl_sm,
1940 1948 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
1941 1949 mutex_exit(&vd->vdev_dtl_lock);
1942 1950
1943 1951 return (error);
1944 1952 }
1945 1953
1946 1954 for (int c = 0; c < vd->vdev_children; c++) {
1947 1955 error = vdev_dtl_load(vd->vdev_child[c]);
1948 1956 if (error != 0)
1949 1957 break;
1950 1958 }
1951 1959
1952 1960 return (error);
1953 1961 }
1954 1962
1955 1963 void
1956 1964 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
1957 1965 {
1958 1966 spa_t *spa = vd->vdev_spa;
1959 1967
1960 1968 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
1961 1969 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1962 1970 zapobj, tx));
1963 1971 }
1964 1972
1965 1973 uint64_t
1966 1974 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
1967 1975 {
1968 1976 spa_t *spa = vd->vdev_spa;
1969 1977 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
1970 1978 DMU_OT_NONE, 0, tx);
1971 1979
1972 1980 ASSERT(zap != 0);
1973 1981 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1974 1982 zap, tx));
1975 1983
1976 1984 return (zap);
1977 1985 }
1978 1986
1979 1987 void
1980 1988 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
1981 1989 {
1982 1990 if (vd->vdev_ops != &vdev_hole_ops &&
1983 1991 vd->vdev_ops != &vdev_missing_ops &&
1984 1992 vd->vdev_ops != &vdev_root_ops &&
1985 1993 !vd->vdev_top->vdev_removing) {
1986 1994 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
1987 1995 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
1988 1996 }
1989 1997 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
1990 1998 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
1991 1999 }
1992 2000 }
1993 2001 for (uint64_t i = 0; i < vd->vdev_children; i++) {
1994 2002 vdev_construct_zaps(vd->vdev_child[i], tx);
1995 2003 }
1996 2004 }
1997 2005
1998 2006 void
1999 2007 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
2000 2008 {
2001 2009 spa_t *spa = vd->vdev_spa;
2002 2010 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2003 2011 objset_t *mos = spa->spa_meta_objset;
2004 2012 range_tree_t *rtsync;
2005 2013 kmutex_t rtlock;
2006 2014 dmu_tx_t *tx;
2007 2015 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2008 2016
2009 2017 ASSERT(!vd->vdev_ishole);
2010 2018 ASSERT(vd->vdev_ops->vdev_op_leaf);
2011 2019
2012 2020 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2013 2021
2014 2022 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2015 2023 mutex_enter(&vd->vdev_dtl_lock);
2016 2024 space_map_free(vd->vdev_dtl_sm, tx);
2017 2025 space_map_close(vd->vdev_dtl_sm);
2018 2026 vd->vdev_dtl_sm = NULL;
2019 2027 mutex_exit(&vd->vdev_dtl_lock);
2020 2028
2021 2029 /*
2022 2030 * We only destroy the leaf ZAP for detached leaves or for
2023 2031 * removed log devices. Removed data devices handle leaf ZAP
2024 2032 * cleanup later, once cancellation is no longer possible.
2025 2033 */
2026 2034 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2027 2035 vd->vdev_top->vdev_islog)) {
2028 2036 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2029 2037 vd->vdev_leaf_zap = 0;
2030 2038 }
2031 2039
2032 2040 dmu_tx_commit(tx);
2033 2041 return;
2034 2042 }
2035 2043
2036 2044 if (vd->vdev_dtl_sm == NULL) {
2037 2045 uint64_t new_object;
2038 2046
2039 2047 new_object = space_map_alloc(mos, tx);
2040 2048 VERIFY3U(new_object, !=, 0);
2041 2049
2042 2050 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2043 2051 0, -1ULL, 0, &vd->vdev_dtl_lock));
2044 2052 ASSERT(vd->vdev_dtl_sm != NULL);
2045 2053 }
2046 2054
2047 2055 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2048 2056
2049 2057 rtsync = range_tree_create(NULL, NULL, &rtlock);
2050 2058
2051 2059 mutex_enter(&rtlock);
2052 2060
2053 2061 mutex_enter(&vd->vdev_dtl_lock);
2054 2062 range_tree_walk(rt, range_tree_add, rtsync);
2055 2063 mutex_exit(&vd->vdev_dtl_lock);
2056 2064
2057 2065 space_map_truncate(vd->vdev_dtl_sm, tx);
2058 2066 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2059 2067 range_tree_vacate(rtsync, NULL, NULL);
2060 2068
2061 2069 range_tree_destroy(rtsync);
2062 2070
2063 2071 mutex_exit(&rtlock);
2064 2072 mutex_destroy(&rtlock);
2065 2073
2066 2074 /*
2067 2075 * If the object for the space map has changed then dirty
2068 2076 * the top level so that we update the config.
2069 2077 */
2070 2078 if (object != space_map_object(vd->vdev_dtl_sm)) {
2071 2079 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2072 2080 "new object %llu", txg, spa_name(spa), object,
2073 2081 space_map_object(vd->vdev_dtl_sm));
2074 2082 vdev_config_dirty(vd->vdev_top);
2075 2083 }
2076 2084
2077 2085 dmu_tx_commit(tx);
2078 2086
2079 2087 mutex_enter(&vd->vdev_dtl_lock);
2080 2088 space_map_update(vd->vdev_dtl_sm);
2081 2089 mutex_exit(&vd->vdev_dtl_lock);
2082 2090 }
2083 2091
2084 2092 /*
2085 2093 * Determine whether the specified vdev can be offlined/detached/removed
2086 2094 * without losing data.
2087 2095 */
2088 2096 boolean_t
2089 2097 vdev_dtl_required(vdev_t *vd)
2090 2098 {
2091 2099 spa_t *spa = vd->vdev_spa;
2092 2100 vdev_t *tvd = vd->vdev_top;
2093 2101 uint8_t cant_read = vd->vdev_cant_read;
2094 2102 boolean_t required;
2095 2103
2096 2104 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2097 2105
2098 2106 if (vd == spa->spa_root_vdev || vd == tvd)
2099 2107 return (B_TRUE);
2100 2108
2101 2109 /*
2102 2110 * Temporarily mark the device as unreadable, and then determine
2103 2111 * whether this results in any DTL outages in the top-level vdev.
2104 2112 * If not, we can safely offline/detach/remove the device.
2105 2113 */
2106 2114 vd->vdev_cant_read = B_TRUE;
2107 2115 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2108 2116 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2109 2117 vd->vdev_cant_read = cant_read;
2110 2118 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2111 2119
2112 2120 if (!required && zio_injection_enabled)
2113 2121 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2114 2122
2115 2123 return (required);
2116 2124 }
2117 2125
2118 2126 /*
2119 2127 * Determine if resilver is needed, and if so the txg range.
2120 2128 */
2121 2129 boolean_t
2122 2130 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2123 2131 {
2124 2132 boolean_t needed = B_FALSE;
2125 2133 uint64_t thismin = UINT64_MAX;
2126 2134 uint64_t thismax = 0;
2127 2135
2128 2136 if (vd->vdev_children == 0) {
2129 2137 mutex_enter(&vd->vdev_dtl_lock);
2130 2138 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2131 2139 vdev_writeable(vd)) {
2132 2140
2133 2141 thismin = vdev_dtl_min(vd);
2134 2142 thismax = vdev_dtl_max(vd);
2135 2143 needed = B_TRUE;
2136 2144 }
2137 2145 mutex_exit(&vd->vdev_dtl_lock);
2138 2146 } else {
2139 2147 for (int c = 0; c < vd->vdev_children; c++) {
2140 2148 vdev_t *cvd = vd->vdev_child[c];
2141 2149 uint64_t cmin, cmax;
2142 2150
2143 2151 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2144 2152 thismin = MIN(thismin, cmin);
2145 2153 thismax = MAX(thismax, cmax);
2146 2154 needed = B_TRUE;
2147 2155 }
2148 2156 }
2149 2157 }
2150 2158
2151 2159 if (needed && minp) {
2152 2160 *minp = thismin;
2153 2161 *maxp = thismax;
2154 2162 }
2155 2163 return (needed);
2156 2164 }
2157 2165
2158 2166 void
2159 2167 vdev_load(vdev_t *vd)
2160 2168 {
2161 2169 /*
2162 2170 * Recursively load all children.
2163 2171 */
2164 2172 for (int c = 0; c < vd->vdev_children; c++)
2165 2173 vdev_load(vd->vdev_child[c]);
2166 2174
2167 2175 /*
2168 2176 * If this is a top-level vdev, initialize its metaslabs.
2169 2177 */
2170 2178 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2171 2179 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2172 2180 vdev_metaslab_init(vd, 0) != 0))
2173 2181 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2174 2182 VDEV_AUX_CORRUPT_DATA);
2175 2183
2176 2184 /*
2177 2185 * If this is a leaf vdev, load its DTL.
2178 2186 */
2179 2187 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2180 2188 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2181 2189 VDEV_AUX_CORRUPT_DATA);
2182 2190 }
2183 2191
2184 2192 /*
2185 2193 * The special vdev case is used for hot spares and l2cache devices. Its
2186 2194 * sole purpose it to set the vdev state for the associated vdev. To do this,
2187 2195 * we make sure that we can open the underlying device, then try to read the
2188 2196 * label, and make sure that the label is sane and that it hasn't been
2189 2197 * repurposed to another pool.
2190 2198 */
2191 2199 int
2192 2200 vdev_validate_aux(vdev_t *vd)
2193 2201 {
2194 2202 nvlist_t *label;
2195 2203 uint64_t guid, version;
2196 2204 uint64_t state;
2197 2205
2198 2206 if (!vdev_readable(vd))
2199 2207 return (0);
2200 2208
2201 2209 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2202 2210 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2203 2211 VDEV_AUX_CORRUPT_DATA);
2204 2212 return (-1);
2205 2213 }
2206 2214
2207 2215 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2208 2216 !SPA_VERSION_IS_SUPPORTED(version) ||
2209 2217 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2210 2218 guid != vd->vdev_guid ||
2211 2219 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2212 2220 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2213 2221 VDEV_AUX_CORRUPT_DATA);
2214 2222 nvlist_free(label);
2215 2223 return (-1);
2216 2224 }
2217 2225
2218 2226 /*
2219 2227 * We don't actually check the pool state here. If it's in fact in
2220 2228 * use by another pool, we update this fact on the fly when requested.
2221 2229 */
2222 2230 nvlist_free(label);
2223 2231 return (0);
2224 2232 }
2225 2233
2226 2234 void
2227 2235 vdev_remove(vdev_t *vd, uint64_t txg)
2228 2236 {
2229 2237 spa_t *spa = vd->vdev_spa;
2230 2238 objset_t *mos = spa->spa_meta_objset;
2231 2239 dmu_tx_t *tx;
2232 2240
2233 2241 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2234 2242 ASSERT(vd == vd->vdev_top);
2235 2243 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2236 2244
2237 2245 if (vd->vdev_ms != NULL) {
2238 2246 metaslab_group_t *mg = vd->vdev_mg;
2239 2247
2240 2248 metaslab_group_histogram_verify(mg);
2241 2249 metaslab_class_histogram_verify(mg->mg_class);
2242 2250
2243 2251 for (int m = 0; m < vd->vdev_ms_count; m++) {
2244 2252 metaslab_t *msp = vd->vdev_ms[m];
2245 2253
2246 2254 if (msp == NULL || msp->ms_sm == NULL)
2247 2255 continue;
2248 2256
2249 2257 mutex_enter(&msp->ms_lock);
2250 2258 /*
2251 2259 * If the metaslab was not loaded when the vdev
2252 2260 * was removed then the histogram accounting may
2253 2261 * not be accurate. Update the histogram information
2254 2262 * here so that we ensure that the metaslab group
2255 2263 * and metaslab class are up-to-date.
2256 2264 */
2257 2265 metaslab_group_histogram_remove(mg, msp);
2258 2266
2259 2267 VERIFY0(space_map_allocated(msp->ms_sm));
2260 2268 space_map_free(msp->ms_sm, tx);
2261 2269 space_map_close(msp->ms_sm);
2262 2270 msp->ms_sm = NULL;
2263 2271 mutex_exit(&msp->ms_lock);
2264 2272 }
2265 2273
2266 2274 metaslab_group_histogram_verify(mg);
2267 2275 metaslab_class_histogram_verify(mg->mg_class);
2268 2276 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2269 2277 ASSERT0(mg->mg_histogram[i]);
2270 2278
2271 2279 }
2272 2280
2273 2281 if (vd->vdev_ms_array) {
2274 2282 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2275 2283 vd->vdev_ms_array = 0;
2276 2284 }
2277 2285
2278 2286 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2279 2287 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2280 2288 vd->vdev_top_zap = 0;
2281 2289 }
2282 2290 dmu_tx_commit(tx);
2283 2291 }
2284 2292
2285 2293 void
2286 2294 vdev_sync_done(vdev_t *vd, uint64_t txg)
2287 2295 {
2288 2296 metaslab_t *msp;
2289 2297 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2290 2298
2291 2299 ASSERT(!vd->vdev_ishole);
2292 2300
2293 2301 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2294 2302 metaslab_sync_done(msp, txg);
2295 2303
2296 2304 if (reassess)
2297 2305 metaslab_sync_reassess(vd->vdev_mg);
2298 2306 }
2299 2307
2300 2308 void
2301 2309 vdev_sync(vdev_t *vd, uint64_t txg)
2302 2310 {
2303 2311 spa_t *spa = vd->vdev_spa;
2304 2312 vdev_t *lvd;
2305 2313 metaslab_t *msp;
2306 2314 dmu_tx_t *tx;
2307 2315
2308 2316 ASSERT(!vd->vdev_ishole);
2309 2317
2310 2318 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2311 2319 ASSERT(vd == vd->vdev_top);
2312 2320 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2313 2321 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2314 2322 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2315 2323 ASSERT(vd->vdev_ms_array != 0);
2316 2324 vdev_config_dirty(vd);
2317 2325 dmu_tx_commit(tx);
2318 2326 }
2319 2327
2320 2328 /*
2321 2329 * Remove the metadata associated with this vdev once it's empty.
2322 2330 */
2323 2331 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2324 2332 vdev_remove(vd, txg);
2325 2333
2326 2334 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2327 2335 metaslab_sync(msp, txg);
2328 2336 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2329 2337 }
2330 2338
2331 2339 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2332 2340 vdev_dtl_sync(lvd, txg);
2333 2341
2334 2342 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2335 2343 }
2336 2344
2337 2345 uint64_t
2338 2346 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2339 2347 {
2340 2348 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2341 2349 }
2342 2350
2343 2351 /*
2344 2352 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2345 2353 * not be opened, and no I/O is attempted.
2346 2354 */
2347 2355 int
2348 2356 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2349 2357 {
2350 2358 vdev_t *vd, *tvd;
2351 2359
2352 2360 spa_vdev_state_enter(spa, SCL_NONE);
2353 2361
2354 2362 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2355 2363 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2356 2364
2357 2365 if (!vd->vdev_ops->vdev_op_leaf)
2358 2366 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2359 2367
2360 2368 tvd = vd->vdev_top;
2361 2369
2362 2370 /*
2363 2371 * We don't directly use the aux state here, but if we do a
2364 2372 * vdev_reopen(), we need this value to be present to remember why we
2365 2373 * were faulted.
2366 2374 */
2367 2375 vd->vdev_label_aux = aux;
2368 2376
2369 2377 /*
2370 2378 * Faulted state takes precedence over degraded.
2371 2379 */
2372 2380 vd->vdev_delayed_close = B_FALSE;
2373 2381 vd->vdev_faulted = 1ULL;
2374 2382 vd->vdev_degraded = 0ULL;
2375 2383 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2376 2384
2377 2385 /*
2378 2386 * If this device has the only valid copy of the data, then
2379 2387 * back off and simply mark the vdev as degraded instead.
2380 2388 */
2381 2389 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2382 2390 vd->vdev_degraded = 1ULL;
2383 2391 vd->vdev_faulted = 0ULL;
2384 2392
2385 2393 /*
2386 2394 * If we reopen the device and it's not dead, only then do we
2387 2395 * mark it degraded.
2388 2396 */
2389 2397 vdev_reopen(tvd);
2390 2398
2391 2399 if (vdev_readable(vd))
2392 2400 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2393 2401 }
2394 2402
2395 2403 return (spa_vdev_state_exit(spa, vd, 0));
2396 2404 }
2397 2405
2398 2406 /*
2399 2407 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2400 2408 * user that something is wrong. The vdev continues to operate as normal as far
2401 2409 * as I/O is concerned.
2402 2410 */
2403 2411 int
2404 2412 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2405 2413 {
2406 2414 vdev_t *vd;
2407 2415
2408 2416 spa_vdev_state_enter(spa, SCL_NONE);
2409 2417
2410 2418 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2411 2419 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2412 2420
2413 2421 if (!vd->vdev_ops->vdev_op_leaf)
2414 2422 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2415 2423
2416 2424 /*
2417 2425 * If the vdev is already faulted, then don't do anything.
2418 2426 */
2419 2427 if (vd->vdev_faulted || vd->vdev_degraded)
2420 2428 return (spa_vdev_state_exit(spa, NULL, 0));
2421 2429
2422 2430 vd->vdev_degraded = 1ULL;
2423 2431 if (!vdev_is_dead(vd))
2424 2432 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2425 2433 aux);
2426 2434
2427 2435 return (spa_vdev_state_exit(spa, vd, 0));
2428 2436 }
2429 2437
2430 2438 /*
2431 2439 * Online the given vdev.
2432 2440 *
2433 2441 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2434 2442 * spare device should be detached when the device finishes resilvering.
2435 2443 * Second, the online should be treated like a 'test' online case, so no FMA
2436 2444 * events are generated if the device fails to open.
2437 2445 */
2438 2446 int
2439 2447 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2440 2448 {
2441 2449 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2442 2450 boolean_t postevent = B_FALSE;
2443 2451
2444 2452 spa_vdev_state_enter(spa, SCL_NONE);
2445 2453
2446 2454 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2447 2455 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2448 2456
2449 2457 if (!vd->vdev_ops->vdev_op_leaf)
2450 2458 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2451 2459
2452 2460 postevent =
2453 2461 (vd->vdev_offline == B_TRUE || vd->vdev_tmpoffline == B_TRUE) ?
2454 2462 B_TRUE : B_FALSE;
2455 2463
2456 2464 tvd = vd->vdev_top;
2457 2465 vd->vdev_offline = B_FALSE;
2458 2466 vd->vdev_tmpoffline = B_FALSE;
2459 2467 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2460 2468 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2461 2469
2462 2470 /* XXX - L2ARC 1.0 does not support expansion */
2463 2471 if (!vd->vdev_aux) {
2464 2472 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2465 2473 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2466 2474 }
2467 2475
2468 2476 vdev_reopen(tvd);
2469 2477 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2470 2478
2471 2479 if (!vd->vdev_aux) {
2472 2480 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2473 2481 pvd->vdev_expanding = B_FALSE;
2474 2482 }
2475 2483
2476 2484 if (newstate)
2477 2485 *newstate = vd->vdev_state;
2478 2486 if ((flags & ZFS_ONLINE_UNSPARE) &&
2479 2487 !vdev_is_dead(vd) && vd->vdev_parent &&
2480 2488 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2481 2489 vd->vdev_parent->vdev_child[0] == vd)
2482 2490 vd->vdev_unspare = B_TRUE;
2483 2491
2484 2492 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2485 2493
2486 2494 /* XXX - L2ARC 1.0 does not support expansion */
2487 2495 if (vd->vdev_aux)
2488 2496 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2489 2497 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2490 2498 }
2491 2499
2492 2500 if (postevent)
2493 2501 spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
2494 2502
2495 2503 return (spa_vdev_state_exit(spa, vd, 0));
2496 2504 }
2497 2505
2498 2506 static int
2499 2507 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2500 2508 {
2501 2509 vdev_t *vd, *tvd;
2502 2510 int error = 0;
2503 2511 uint64_t generation;
2504 2512 metaslab_group_t *mg;
2505 2513
2506 2514 top:
2507 2515 spa_vdev_state_enter(spa, SCL_ALLOC);
2508 2516
2509 2517 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2510 2518 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2511 2519
2512 2520 if (!vd->vdev_ops->vdev_op_leaf)
2513 2521 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2514 2522
2515 2523 tvd = vd->vdev_top;
2516 2524 mg = tvd->vdev_mg;
2517 2525 generation = spa->spa_config_generation + 1;
2518 2526
2519 2527 /*
2520 2528 * If the device isn't already offline, try to offline it.
2521 2529 */
2522 2530 if (!vd->vdev_offline) {
2523 2531 /*
2524 2532 * If this device has the only valid copy of some data,
2525 2533 * don't allow it to be offlined. Log devices are always
2526 2534 * expendable.
2527 2535 */
2528 2536 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2529 2537 vdev_dtl_required(vd))
2530 2538 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2531 2539
2532 2540 /*
2533 2541 * If the top-level is a slog and it has had allocations
2534 2542 * then proceed. We check that the vdev's metaslab group
2535 2543 * is not NULL since it's possible that we may have just
2536 2544 * added this vdev but not yet initialized its metaslabs.
2537 2545 */
2538 2546 if (tvd->vdev_islog && mg != NULL) {
2539 2547 /*
2540 2548 * Prevent any future allocations.
2541 2549 */
2542 2550 metaslab_group_passivate(mg);
2543 2551 (void) spa_vdev_state_exit(spa, vd, 0);
2544 2552
2545 2553 error = spa_offline_log(spa);
2546 2554
2547 2555 spa_vdev_state_enter(spa, SCL_ALLOC);
2548 2556
2549 2557 /*
2550 2558 * Check to see if the config has changed.
2551 2559 */
2552 2560 if (error || generation != spa->spa_config_generation) {
2553 2561 metaslab_group_activate(mg);
2554 2562 if (error)
2555 2563 return (spa_vdev_state_exit(spa,
2556 2564 vd, error));
2557 2565 (void) spa_vdev_state_exit(spa, vd, 0);
2558 2566 goto top;
2559 2567 }
2560 2568 ASSERT0(tvd->vdev_stat.vs_alloc);
2561 2569 }
2562 2570
2563 2571 /*
2564 2572 * Offline this device and reopen its top-level vdev.
2565 2573 * If the top-level vdev is a log device then just offline
2566 2574 * it. Otherwise, if this action results in the top-level
2567 2575 * vdev becoming unusable, undo it and fail the request.
2568 2576 */
2569 2577 vd->vdev_offline = B_TRUE;
2570 2578 vdev_reopen(tvd);
2571 2579
2572 2580 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2573 2581 vdev_is_dead(tvd)) {
2574 2582 vd->vdev_offline = B_FALSE;
2575 2583 vdev_reopen(tvd);
2576 2584 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2577 2585 }
2578 2586
2579 2587 /*
2580 2588 * Add the device back into the metaslab rotor so that
2581 2589 * once we online the device it's open for business.
2582 2590 */
2583 2591 if (tvd->vdev_islog && mg != NULL)
2584 2592 metaslab_group_activate(mg);
2585 2593 }
2586 2594
2587 2595 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2588 2596
2589 2597 return (spa_vdev_state_exit(spa, vd, 0));
2590 2598 }
2591 2599
2592 2600 int
2593 2601 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2594 2602 {
2595 2603 int error;
2596 2604
2597 2605 mutex_enter(&spa->spa_vdev_top_lock);
2598 2606 error = vdev_offline_locked(spa, guid, flags);
2599 2607 mutex_exit(&spa->spa_vdev_top_lock);
2600 2608
2601 2609 return (error);
2602 2610 }
2603 2611
2604 2612 /*
2605 2613 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2606 2614 * vdev_offline(), we assume the spa config is locked. We also clear all
2607 2615 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2608 2616 */
2609 2617 void
2610 2618 vdev_clear(spa_t *spa, vdev_t *vd)
2611 2619 {
2612 2620 vdev_t *rvd = spa->spa_root_vdev;
2613 2621
2614 2622 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2615 2623
2616 2624 if (vd == NULL)
2617 2625 vd = rvd;
2618 2626
2619 2627 vd->vdev_stat.vs_read_errors = 0;
2620 2628 vd->vdev_stat.vs_write_errors = 0;
2621 2629 vd->vdev_stat.vs_checksum_errors = 0;
2622 2630
2623 2631 for (int c = 0; c < vd->vdev_children; c++)
2624 2632 vdev_clear(spa, vd->vdev_child[c]);
2625 2633
2626 2634 /*
2627 2635 * If we're in the FAULTED state or have experienced failed I/O, then
2628 2636 * clear the persistent state and attempt to reopen the device. We
2629 2637 * also mark the vdev config dirty, so that the new faulted state is
2630 2638 * written out to disk.
2631 2639 */
2632 2640 if (vd->vdev_faulted || vd->vdev_degraded ||
2633 2641 !vdev_readable(vd) || !vdev_writeable(vd)) {
2634 2642
2635 2643 /*
2636 2644 * When reopening in reponse to a clear event, it may be due to
2637 2645 * a fmadm repair request. In this case, if the device is
2638 2646 * still broken, we want to still post the ereport again.
2639 2647 */
2640 2648 vd->vdev_forcefault = B_TRUE;
2641 2649
2642 2650 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2643 2651 vd->vdev_cant_read = B_FALSE;
2644 2652 vd->vdev_cant_write = B_FALSE;
2645 2653
2646 2654 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2647 2655
2648 2656 vd->vdev_forcefault = B_FALSE;
2649 2657
2650 2658 if (vd != rvd && vdev_writeable(vd->vdev_top))
2651 2659 vdev_state_dirty(vd->vdev_top);
2652 2660
2653 2661 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2654 2662 spa_async_request(spa, SPA_ASYNC_RESILVER);
2655 2663
2656 2664 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2657 2665 }
2658 2666
2659 2667 /*
2660 2668 * When clearing a FMA-diagnosed fault, we always want to
2661 2669 * unspare the device, as we assume that the original spare was
2662 2670 * done in response to the FMA fault.
2663 2671 */
2664 2672 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2665 2673 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2666 2674 vd->vdev_parent->vdev_child[0] == vd)
2667 2675 vd->vdev_unspare = B_TRUE;
2668 2676 }
2669 2677
2670 2678 boolean_t
2671 2679 vdev_is_dead(vdev_t *vd)
2672 2680 {
2673 2681 /*
2674 2682 * Holes and missing devices are always considered "dead".
2675 2683 * This simplifies the code since we don't have to check for
2676 2684 * these types of devices in the various code paths.
2677 2685 * Instead we rely on the fact that we skip over dead devices
2678 2686 * before issuing I/O to them.
2679 2687 */
2680 2688 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2681 2689 vd->vdev_ops == &vdev_missing_ops);
2682 2690 }
2683 2691
2684 2692 boolean_t
2685 2693 vdev_readable(vdev_t *vd)
2686 2694 {
2687 2695 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2688 2696 }
2689 2697
2690 2698 boolean_t
2691 2699 vdev_writeable(vdev_t *vd)
2692 2700 {
2693 2701 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2694 2702 }
2695 2703
2696 2704 boolean_t
2697 2705 vdev_allocatable(vdev_t *vd)
2698 2706 {
2699 2707 uint64_t state = vd->vdev_state;
2700 2708
2701 2709 /*
2702 2710 * We currently allow allocations from vdevs which may be in the
2703 2711 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2704 2712 * fails to reopen then we'll catch it later when we're holding
2705 2713 * the proper locks. Note that we have to get the vdev state
2706 2714 * in a local variable because although it changes atomically,
2707 2715 * we're asking two separate questions about it.
2708 2716 */
2709 2717 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2710 2718 !vd->vdev_cant_write && !vd->vdev_ishole &&
2711 2719 vd->vdev_mg->mg_initialized);
2712 2720 }
2713 2721
2714 2722 boolean_t
2715 2723 vdev_accessible(vdev_t *vd, zio_t *zio)
2716 2724 {
2717 2725 ASSERT(zio->io_vd == vd);
2718 2726
2719 2727 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2720 2728 return (B_FALSE);
2721 2729
2722 2730 if (zio->io_type == ZIO_TYPE_READ)
2723 2731 return (!vd->vdev_cant_read);
2724 2732
2725 2733 if (zio->io_type == ZIO_TYPE_WRITE)
2726 2734 return (!vd->vdev_cant_write);
2727 2735
2728 2736 return (B_TRUE);
2729 2737 }
2730 2738
2731 2739 /*
2732 2740 * Get statistics for the given vdev.
2733 2741 */
2734 2742 void
2735 2743 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2736 2744 {
2737 2745 spa_t *spa = vd->vdev_spa;
2738 2746 vdev_t *rvd = spa->spa_root_vdev;
2739 2747 vdev_t *tvd = vd->vdev_top;
2740 2748
2741 2749 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2742 2750
2743 2751 mutex_enter(&vd->vdev_stat_lock);
2744 2752 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2745 2753 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2746 2754 vs->vs_state = vd->vdev_state;
2747 2755 vs->vs_rsize = vdev_get_min_asize(vd);
2748 2756 if (vd->vdev_ops->vdev_op_leaf)
2749 2757 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2750 2758 /*
2751 2759 * Report expandable space on top-level, non-auxillary devices only.
2752 2760 * The expandable space is reported in terms of metaslab sized units
2753 2761 * since that determines how much space the pool can expand.
2754 2762 */
2755 2763 if (vd->vdev_aux == NULL && tvd != NULL) {
2756 2764 vs->vs_esize = P2ALIGN(vd->vdev_max_asize - vd->vdev_asize,
2757 2765 1ULL << tvd->vdev_ms_shift);
2758 2766 }
2759 2767 if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
2760 2768 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
2761 2769 }
2762 2770
2763 2771 /*
2764 2772 * If we're getting stats on the root vdev, aggregate the I/O counts
2765 2773 * over all top-level vdevs (i.e. the direct children of the root).
2766 2774 */
2767 2775 if (vd == rvd) {
2768 2776 for (int c = 0; c < rvd->vdev_children; c++) {
2769 2777 vdev_t *cvd = rvd->vdev_child[c];
2770 2778 vdev_stat_t *cvs = &cvd->vdev_stat;
2771 2779
2772 2780 for (int t = 0; t < ZIO_TYPES; t++) {
2773 2781 vs->vs_ops[t] += cvs->vs_ops[t];
2774 2782 vs->vs_bytes[t] += cvs->vs_bytes[t];
2775 2783 }
2776 2784 cvs->vs_scan_removing = cvd->vdev_removing;
2777 2785 }
2778 2786 }
2779 2787 mutex_exit(&vd->vdev_stat_lock);
2780 2788 }
2781 2789
2782 2790 void
2783 2791 vdev_clear_stats(vdev_t *vd)
2784 2792 {
2785 2793 mutex_enter(&vd->vdev_stat_lock);
2786 2794 vd->vdev_stat.vs_space = 0;
2787 2795 vd->vdev_stat.vs_dspace = 0;
2788 2796 vd->vdev_stat.vs_alloc = 0;
2789 2797 mutex_exit(&vd->vdev_stat_lock);
2790 2798 }
2791 2799
2792 2800 void
2793 2801 vdev_scan_stat_init(vdev_t *vd)
2794 2802 {
2795 2803 vdev_stat_t *vs = &vd->vdev_stat;
2796 2804
2797 2805 for (int c = 0; c < vd->vdev_children; c++)
2798 2806 vdev_scan_stat_init(vd->vdev_child[c]);
2799 2807
2800 2808 mutex_enter(&vd->vdev_stat_lock);
2801 2809 vs->vs_scan_processed = 0;
2802 2810 mutex_exit(&vd->vdev_stat_lock);
2803 2811 }
2804 2812
2805 2813 void
2806 2814 vdev_stat_update(zio_t *zio, uint64_t psize)
2807 2815 {
2808 2816 spa_t *spa = zio->io_spa;
2809 2817 vdev_t *rvd = spa->spa_root_vdev;
2810 2818 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2811 2819 vdev_t *pvd;
2812 2820 uint64_t txg = zio->io_txg;
2813 2821 vdev_stat_t *vs = &vd->vdev_stat;
2814 2822 zio_type_t type = zio->io_type;
2815 2823 int flags = zio->io_flags;
2816 2824
2817 2825 /*
2818 2826 * If this i/o is a gang leader, it didn't do any actual work.
2819 2827 */
2820 2828 if (zio->io_gang_tree)
2821 2829 return;
2822 2830
2823 2831 if (zio->io_error == 0) {
2824 2832 /*
2825 2833 * If this is a root i/o, don't count it -- we've already
2826 2834 * counted the top-level vdevs, and vdev_get_stats() will
2827 2835 * aggregate them when asked. This reduces contention on
2828 2836 * the root vdev_stat_lock and implicitly handles blocks
2829 2837 * that compress away to holes, for which there is no i/o.
2830 2838 * (Holes never create vdev children, so all the counters
2831 2839 * remain zero, which is what we want.)
2832 2840 *
2833 2841 * Note: this only applies to successful i/o (io_error == 0)
2834 2842 * because unlike i/o counts, errors are not additive.
2835 2843 * When reading a ditto block, for example, failure of
2836 2844 * one top-level vdev does not imply a root-level error.
2837 2845 */
2838 2846 if (vd == rvd)
2839 2847 return;
2840 2848
2841 2849 ASSERT(vd == zio->io_vd);
2842 2850
2843 2851 if (flags & ZIO_FLAG_IO_BYPASS)
2844 2852 return;
2845 2853
2846 2854 mutex_enter(&vd->vdev_stat_lock);
2847 2855
2848 2856 if (flags & ZIO_FLAG_IO_REPAIR) {
2849 2857 if (flags & ZIO_FLAG_SCAN_THREAD) {
2850 2858 dsl_scan_phys_t *scn_phys =
2851 2859 &spa->spa_dsl_pool->dp_scan->scn_phys;
2852 2860 uint64_t *processed = &scn_phys->scn_processed;
2853 2861
2854 2862 /* XXX cleanup? */
2855 2863 if (vd->vdev_ops->vdev_op_leaf)
2856 2864 atomic_add_64(processed, psize);
2857 2865 vs->vs_scan_processed += psize;
2858 2866 }
2859 2867
2860 2868 if (flags & ZIO_FLAG_SELF_HEAL)
2861 2869 vs->vs_self_healed += psize;
2862 2870 }
2863 2871
2864 2872 vs->vs_ops[type]++;
2865 2873 vs->vs_bytes[type] += psize;
2866 2874
2867 2875 mutex_exit(&vd->vdev_stat_lock);
2868 2876 return;
2869 2877 }
2870 2878
2871 2879 if (flags & ZIO_FLAG_SPECULATIVE)
2872 2880 return;
2873 2881
2874 2882 /*
2875 2883 * If this is an I/O error that is going to be retried, then ignore the
2876 2884 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
2877 2885 * hard errors, when in reality they can happen for any number of
2878 2886 * innocuous reasons (bus resets, MPxIO link failure, etc).
2879 2887 */
2880 2888 if (zio->io_error == EIO &&
2881 2889 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2882 2890 return;
2883 2891
2884 2892 /*
2885 2893 * Intent logs writes won't propagate their error to the root
2886 2894 * I/O so don't mark these types of failures as pool-level
2887 2895 * errors.
2888 2896 */
2889 2897 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
2890 2898 return;
2891 2899
2892 2900 mutex_enter(&vd->vdev_stat_lock);
2893 2901 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2894 2902 if (zio->io_error == ECKSUM)
2895 2903 vs->vs_checksum_errors++;
2896 2904 else
2897 2905 vs->vs_read_errors++;
2898 2906 }
2899 2907 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2900 2908 vs->vs_write_errors++;
2901 2909 mutex_exit(&vd->vdev_stat_lock);
2902 2910
2903 2911 if (type == ZIO_TYPE_WRITE && txg != 0 &&
2904 2912 (!(flags & ZIO_FLAG_IO_REPAIR) ||
2905 2913 (flags & ZIO_FLAG_SCAN_THREAD) ||
2906 2914 spa->spa_claiming)) {
2907 2915 /*
2908 2916 * This is either a normal write (not a repair), or it's
2909 2917 * a repair induced by the scrub thread, or it's a repair
2910 2918 * made by zil_claim() during spa_load() in the first txg.
2911 2919 * In the normal case, we commit the DTL change in the same
2912 2920 * txg as the block was born. In the scrub-induced repair
2913 2921 * case, we know that scrubs run in first-pass syncing context,
2914 2922 * so we commit the DTL change in spa_syncing_txg(spa).
2915 2923 * In the zil_claim() case, we commit in spa_first_txg(spa).
2916 2924 *
2917 2925 * We currently do not make DTL entries for failed spontaneous
2918 2926 * self-healing writes triggered by normal (non-scrubbing)
2919 2927 * reads, because we have no transactional context in which to
2920 2928 * do so -- and it's not clear that it'd be desirable anyway.
2921 2929 */
2922 2930 if (vd->vdev_ops->vdev_op_leaf) {
2923 2931 uint64_t commit_txg = txg;
2924 2932 if (flags & ZIO_FLAG_SCAN_THREAD) {
2925 2933 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2926 2934 ASSERT(spa_sync_pass(spa) == 1);
2927 2935 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2928 2936 commit_txg = spa_syncing_txg(spa);
2929 2937 } else if (spa->spa_claiming) {
2930 2938 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2931 2939 commit_txg = spa_first_txg(spa);
2932 2940 }
2933 2941 ASSERT(commit_txg >= spa_syncing_txg(spa));
2934 2942 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2935 2943 return;
2936 2944 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2937 2945 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
2938 2946 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2939 2947 }
2940 2948 if (vd != rvd)
2941 2949 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2942 2950 }
2943 2951 }
2944 2952
2945 2953 /*
2946 2954 * Update the in-core space usage stats for this vdev, its metaslab class,
2947 2955 * and the root vdev.
2948 2956 */
2949 2957 void
2950 2958 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2951 2959 int64_t space_delta)
2952 2960 {
2953 2961 int64_t dspace_delta = space_delta;
2954 2962 spa_t *spa = vd->vdev_spa;
2955 2963 vdev_t *rvd = spa->spa_root_vdev;
2956 2964 metaslab_group_t *mg = vd->vdev_mg;
2957 2965 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2958 2966
2959 2967 ASSERT(vd == vd->vdev_top);
2960 2968
2961 2969 /*
2962 2970 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2963 2971 * factor. We must calculate this here and not at the root vdev
2964 2972 * because the root vdev's psize-to-asize is simply the max of its
2965 2973 * childrens', thus not accurate enough for us.
2966 2974 */
2967 2975 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2968 2976 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
2969 2977 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2970 2978 vd->vdev_deflate_ratio;
2971 2979
2972 2980 mutex_enter(&vd->vdev_stat_lock);
2973 2981 vd->vdev_stat.vs_alloc += alloc_delta;
2974 2982 vd->vdev_stat.vs_space += space_delta;
2975 2983 vd->vdev_stat.vs_dspace += dspace_delta;
2976 2984 mutex_exit(&vd->vdev_stat_lock);
2977 2985
2978 2986 if (mc == spa_normal_class(spa)) {
2979 2987 mutex_enter(&rvd->vdev_stat_lock);
2980 2988 rvd->vdev_stat.vs_alloc += alloc_delta;
2981 2989 rvd->vdev_stat.vs_space += space_delta;
2982 2990 rvd->vdev_stat.vs_dspace += dspace_delta;
2983 2991 mutex_exit(&rvd->vdev_stat_lock);
2984 2992 }
2985 2993
2986 2994 if (mc != NULL) {
2987 2995 ASSERT(rvd == vd->vdev_parent);
2988 2996 ASSERT(vd->vdev_ms_count != 0);
2989 2997
2990 2998 metaslab_class_space_update(mc,
2991 2999 alloc_delta, defer_delta, space_delta, dspace_delta);
2992 3000 }
2993 3001 }
2994 3002
2995 3003 /*
2996 3004 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2997 3005 * so that it will be written out next time the vdev configuration is synced.
2998 3006 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2999 3007 */
3000 3008 void
3001 3009 vdev_config_dirty(vdev_t *vd)
3002 3010 {
3003 3011 spa_t *spa = vd->vdev_spa;
3004 3012 vdev_t *rvd = spa->spa_root_vdev;
3005 3013 int c;
3006 3014
3007 3015 ASSERT(spa_writeable(spa));
3008 3016
3009 3017 /*
3010 3018 * If this is an aux vdev (as with l2cache and spare devices), then we
3011 3019 * update the vdev config manually and set the sync flag.
3012 3020 */
3013 3021 if (vd->vdev_aux != NULL) {
3014 3022 spa_aux_vdev_t *sav = vd->vdev_aux;
3015 3023 nvlist_t **aux;
3016 3024 uint_t naux;
3017 3025
3018 3026 for (c = 0; c < sav->sav_count; c++) {
3019 3027 if (sav->sav_vdevs[c] == vd)
3020 3028 break;
3021 3029 }
3022 3030
3023 3031 if (c == sav->sav_count) {
3024 3032 /*
3025 3033 * We're being removed. There's nothing more to do.
3026 3034 */
3027 3035 ASSERT(sav->sav_sync == B_TRUE);
3028 3036 return;
3029 3037 }
3030 3038
3031 3039 sav->sav_sync = B_TRUE;
3032 3040
3033 3041 if (nvlist_lookup_nvlist_array(sav->sav_config,
3034 3042 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3035 3043 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3036 3044 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3037 3045 }
3038 3046
3039 3047 ASSERT(c < naux);
3040 3048
3041 3049 /*
3042 3050 * Setting the nvlist in the middle if the array is a little
3043 3051 * sketchy, but it will work.
3044 3052 */
3045 3053 nvlist_free(aux[c]);
3046 3054 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3047 3055
3048 3056 return;
3049 3057 }
3050 3058
3051 3059 /*
3052 3060 * The dirty list is protected by the SCL_CONFIG lock. The caller
3053 3061 * must either hold SCL_CONFIG as writer, or must be the sync thread
3054 3062 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3055 3063 * so this is sufficient to ensure mutual exclusion.
3056 3064 */
3057 3065 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3058 3066 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3059 3067 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3060 3068
3061 3069 if (vd == rvd) {
3062 3070 for (c = 0; c < rvd->vdev_children; c++)
3063 3071 vdev_config_dirty(rvd->vdev_child[c]);
3064 3072 } else {
3065 3073 ASSERT(vd == vd->vdev_top);
3066 3074
3067 3075 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3068 3076 !vd->vdev_ishole)
3069 3077 list_insert_head(&spa->spa_config_dirty_list, vd);
3070 3078 }
3071 3079 }
3072 3080
3073 3081 void
3074 3082 vdev_config_clean(vdev_t *vd)
3075 3083 {
3076 3084 spa_t *spa = vd->vdev_spa;
3077 3085
3078 3086 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3079 3087 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3080 3088 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3081 3089
3082 3090 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3083 3091 list_remove(&spa->spa_config_dirty_list, vd);
3084 3092 }
3085 3093
3086 3094 /*
3087 3095 * Mark a top-level vdev's state as dirty, so that the next pass of
3088 3096 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3089 3097 * the state changes from larger config changes because they require
3090 3098 * much less locking, and are often needed for administrative actions.
3091 3099 */
3092 3100 void
3093 3101 vdev_state_dirty(vdev_t *vd)
3094 3102 {
3095 3103 spa_t *spa = vd->vdev_spa;
3096 3104
3097 3105 ASSERT(spa_writeable(spa));
3098 3106 ASSERT(vd == vd->vdev_top);
3099 3107
3100 3108 /*
3101 3109 * The state list is protected by the SCL_STATE lock. The caller
3102 3110 * must either hold SCL_STATE as writer, or must be the sync thread
3103 3111 * (which holds SCL_STATE as reader). There's only one sync thread,
3104 3112 * so this is sufficient to ensure mutual exclusion.
3105 3113 */
3106 3114 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3107 3115 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3108 3116 spa_config_held(spa, SCL_STATE, RW_READER)));
3109 3117
3110 3118 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3111 3119 list_insert_head(&spa->spa_state_dirty_list, vd);
3112 3120 }
3113 3121
3114 3122 void
3115 3123 vdev_state_clean(vdev_t *vd)
3116 3124 {
3117 3125 spa_t *spa = vd->vdev_spa;
3118 3126
3119 3127 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3120 3128 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3121 3129 spa_config_held(spa, SCL_STATE, RW_READER)));
3122 3130
3123 3131 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3124 3132 list_remove(&spa->spa_state_dirty_list, vd);
3125 3133 }
3126 3134
3127 3135 /*
3128 3136 * Propagate vdev state up from children to parent.
3129 3137 */
3130 3138 void
3131 3139 vdev_propagate_state(vdev_t *vd)
3132 3140 {
3133 3141 spa_t *spa = vd->vdev_spa;
3134 3142 vdev_t *rvd = spa->spa_root_vdev;
3135 3143 int degraded = 0, faulted = 0;
3136 3144 int corrupted = 0;
3137 3145 vdev_t *child;
3138 3146
3139 3147 if (vd->vdev_children > 0) {
3140 3148 for (int c = 0; c < vd->vdev_children; c++) {
3141 3149 child = vd->vdev_child[c];
3142 3150
3143 3151 /*
3144 3152 * Don't factor holes into the decision.
3145 3153 */
3146 3154 if (child->vdev_ishole)
3147 3155 continue;
3148 3156
3149 3157 if (!vdev_readable(child) ||
3150 3158 (!vdev_writeable(child) && spa_writeable(spa))) {
3151 3159 /*
3152 3160 * Root special: if there is a top-level log
3153 3161 * device, treat the root vdev as if it were
3154 3162 * degraded.
3155 3163 */
3156 3164 if (child->vdev_islog && vd == rvd)
3157 3165 degraded++;
3158 3166 else
3159 3167 faulted++;
3160 3168 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3161 3169 degraded++;
3162 3170 }
3163 3171
3164 3172 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3165 3173 corrupted++;
3166 3174 }
3167 3175
3168 3176 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3169 3177
3170 3178 /*
3171 3179 * Root special: if there is a top-level vdev that cannot be
3172 3180 * opened due to corrupted metadata, then propagate the root
3173 3181 * vdev's aux state as 'corrupt' rather than 'insufficient
3174 3182 * replicas'.
3175 3183 */
3176 3184 if (corrupted && vd == rvd &&
3177 3185 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3178 3186 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3179 3187 VDEV_AUX_CORRUPT_DATA);
3180 3188 }
3181 3189
3182 3190 if (vd->vdev_parent)
3183 3191 vdev_propagate_state(vd->vdev_parent);
3184 3192 }
3185 3193
3186 3194 /*
3187 3195 * Set a vdev's state. If this is during an open, we don't update the parent
3188 3196 * state, because we're in the process of opening children depth-first.
3189 3197 * Otherwise, we propagate the change to the parent.
3190 3198 *
3191 3199 * If this routine places a device in a faulted state, an appropriate ereport is
3192 3200 * generated.
3193 3201 */
3194 3202 void
3195 3203 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3196 3204 {
3197 3205 uint64_t save_state;
3198 3206 spa_t *spa = vd->vdev_spa;
3199 3207
3200 3208 if (state == vd->vdev_state) {
3201 3209 vd->vdev_stat.vs_aux = aux;
3202 3210 return;
3203 3211 }
3204 3212
3205 3213 save_state = vd->vdev_state;
3206 3214
3207 3215 vd->vdev_state = state;
3208 3216 vd->vdev_stat.vs_aux = aux;
3209 3217
3210 3218 /*
3211 3219 * If we are setting the vdev state to anything but an open state, then
3212 3220 * always close the underlying device unless the device has requested
3213 3221 * a delayed close (i.e. we're about to remove or fault the device).
3214 3222 * Otherwise, we keep accessible but invalid devices open forever.
3215 3223 * We don't call vdev_close() itself, because that implies some extra
3216 3224 * checks (offline, etc) that we don't want here. This is limited to
3217 3225 * leaf devices, because otherwise closing the device will affect other
3218 3226 * children.
3219 3227 */
3220 3228 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3221 3229 vd->vdev_ops->vdev_op_leaf)
3222 3230 vd->vdev_ops->vdev_op_close(vd);
3223 3231
3224 3232 /*
3225 3233 * If we have brought this vdev back into service, we need
3226 3234 * to notify fmd so that it can gracefully repair any outstanding
3227 3235 * cases due to a missing device. We do this in all cases, even those
3228 3236 * that probably don't correlate to a repaired fault. This is sure to
3229 3237 * catch all cases, and we let the zfs-retire agent sort it out. If
3230 3238 * this is a transient state it's OK, as the retire agent will
3231 3239 * double-check the state of the vdev before repairing it.
3232 3240 */
3233 3241 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3234 3242 vd->vdev_prevstate != state)
3235 3243 zfs_post_state_change(spa, vd);
3236 3244
3237 3245 if (vd->vdev_removed &&
3238 3246 state == VDEV_STATE_CANT_OPEN &&
3239 3247 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3240 3248 /*
3241 3249 * If the previous state is set to VDEV_STATE_REMOVED, then this
3242 3250 * device was previously marked removed and someone attempted to
3243 3251 * reopen it. If this failed due to a nonexistent device, then
3244 3252 * keep the device in the REMOVED state. We also let this be if
3245 3253 * it is one of our special test online cases, which is only
3246 3254 * attempting to online the device and shouldn't generate an FMA
3247 3255 * fault.
3248 3256 */
3249 3257 vd->vdev_state = VDEV_STATE_REMOVED;
3250 3258 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3251 3259 } else if (state == VDEV_STATE_REMOVED) {
3252 3260 vd->vdev_removed = B_TRUE;
3253 3261 } else if (state == VDEV_STATE_CANT_OPEN) {
3254 3262 /*
3255 3263 * If we fail to open a vdev during an import or recovery, we
3256 3264 * mark it as "not available", which signifies that it was
3257 3265 * never there to begin with. Failure to open such a device
3258 3266 * is not considered an error.
3259 3267 */
3260 3268 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3261 3269 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3262 3270 vd->vdev_ops->vdev_op_leaf)
3263 3271 vd->vdev_not_present = 1;
3264 3272
3265 3273 /*
3266 3274 * Post the appropriate ereport. If the 'prevstate' field is
3267 3275 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3268 3276 * that this is part of a vdev_reopen(). In this case, we don't
3269 3277 * want to post the ereport if the device was already in the
3270 3278 * CANT_OPEN state beforehand.
3271 3279 *
3272 3280 * If the 'checkremove' flag is set, then this is an attempt to
3273 3281 * online the device in response to an insertion event. If we
3274 3282 * hit this case, then we have detected an insertion event for a
3275 3283 * faulted or offline device that wasn't in the removed state.
3276 3284 * In this scenario, we don't post an ereport because we are
3277 3285 * about to replace the device, or attempt an online with
3278 3286 * vdev_forcefault, which will generate the fault for us.
3279 3287 */
3280 3288 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3281 3289 !vd->vdev_not_present && !vd->vdev_checkremove &&
3282 3290 vd != spa->spa_root_vdev) {
3283 3291 const char *class;
3284 3292
3285 3293 switch (aux) {
3286 3294 case VDEV_AUX_OPEN_FAILED:
3287 3295 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3288 3296 break;
3289 3297 case VDEV_AUX_CORRUPT_DATA:
3290 3298 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3291 3299 break;
3292 3300 case VDEV_AUX_NO_REPLICAS:
3293 3301 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3294 3302 break;
3295 3303 case VDEV_AUX_BAD_GUID_SUM:
3296 3304 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3297 3305 break;
3298 3306 case VDEV_AUX_TOO_SMALL:
3299 3307 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3300 3308 break;
3301 3309 case VDEV_AUX_BAD_LABEL:
3302 3310 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3303 3311 break;
3304 3312 default:
3305 3313 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3306 3314 }
3307 3315
3308 3316 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3309 3317 }
3310 3318
3311 3319 /* Erase any notion of persistent removed state */
3312 3320 vd->vdev_removed = B_FALSE;
3313 3321 } else {
3314 3322 vd->vdev_removed = B_FALSE;
3315 3323 }
3316 3324
3317 3325 if (!isopen && vd->vdev_parent)
3318 3326 vdev_propagate_state(vd->vdev_parent);
3319 3327 }
3320 3328
3321 3329 /*
3322 3330 * Check the vdev configuration to ensure that it's capable of supporting
3323 3331 * a root pool. We do not support partial configuration.
3324 3332 * In addition, only a single top-level vdev is allowed.
3325 3333 */
3326 3334 boolean_t
3327 3335 vdev_is_bootable(vdev_t *vd)
3328 3336 {
3329 3337 if (!vd->vdev_ops->vdev_op_leaf) {
3330 3338 char *vdev_type = vd->vdev_ops->vdev_op_type;
3331 3339
3332 3340 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3333 3341 vd->vdev_children > 1) {
3334 3342 return (B_FALSE);
3335 3343 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3336 3344 return (B_FALSE);
3337 3345 }
3338 3346 }
3339 3347
3340 3348 for (int c = 0; c < vd->vdev_children; c++) {
3341 3349 if (!vdev_is_bootable(vd->vdev_child[c]))
3342 3350 return (B_FALSE);
3343 3351 }
3344 3352 return (B_TRUE);
3345 3353 }
3346 3354
3347 3355 /*
3348 3356 * Load the state from the original vdev tree (ovd) which
3349 3357 * we've retrieved from the MOS config object. If the original
3350 3358 * vdev was offline or faulted then we transfer that state to the
3351 3359 * device in the current vdev tree (nvd).
3352 3360 */
3353 3361 void
3354 3362 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3355 3363 {
3356 3364 spa_t *spa = nvd->vdev_spa;
3357 3365
3358 3366 ASSERT(nvd->vdev_top->vdev_islog);
3359 3367 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3360 3368 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3361 3369
3362 3370 for (int c = 0; c < nvd->vdev_children; c++)
3363 3371 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3364 3372
3365 3373 if (nvd->vdev_ops->vdev_op_leaf) {
3366 3374 /*
3367 3375 * Restore the persistent vdev state
3368 3376 */
3369 3377 nvd->vdev_offline = ovd->vdev_offline;
3370 3378 nvd->vdev_faulted = ovd->vdev_faulted;
3371 3379 nvd->vdev_degraded = ovd->vdev_degraded;
3372 3380 nvd->vdev_removed = ovd->vdev_removed;
3373 3381 }
3374 3382 }
3375 3383
3376 3384 /*
3377 3385 * Determine if a log device has valid content. If the vdev was
3378 3386 * removed or faulted in the MOS config then we know that
3379 3387 * the content on the log device has already been written to the pool.
3380 3388 */
3381 3389 boolean_t
3382 3390 vdev_log_state_valid(vdev_t *vd)
3383 3391 {
3384 3392 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3385 3393 !vd->vdev_removed)
3386 3394 return (B_TRUE);
3387 3395
3388 3396 for (int c = 0; c < vd->vdev_children; c++)
3389 3397 if (vdev_log_state_valid(vd->vdev_child[c]))
3390 3398 return (B_TRUE);
3391 3399
3392 3400 return (B_FALSE);
3393 3401 }
3394 3402
3395 3403 /*
3396 3404 * Expand a vdev if possible.
3397 3405 */
3398 3406 void
3399 3407 vdev_expand(vdev_t *vd, uint64_t txg)
3400 3408 {
3401 3409 ASSERT(vd->vdev_top == vd);
3402 3410 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3403 3411
3404 3412 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3405 3413 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3406 3414 vdev_config_dirty(vd);
3407 3415 }
3408 3416 }
3409 3417
3410 3418 /*
3411 3419 * Split a vdev.
3412 3420 */
3413 3421 void
3414 3422 vdev_split(vdev_t *vd)
3415 3423 {
3416 3424 vdev_t *cvd, *pvd = vd->vdev_parent;
3417 3425
3418 3426 vdev_remove_child(pvd, vd);
3419 3427 vdev_compact_children(pvd);
3420 3428
3421 3429 cvd = pvd->vdev_child[0];
3422 3430 if (pvd->vdev_children == 1) {
3423 3431 vdev_remove_parent(cvd);
3424 3432 cvd->vdev_splitting = B_TRUE;
3425 3433 }
3426 3434 vdev_propagate_state(cvd);
3427 3435 }
3428 3436
3429 3437 void
3430 3438 vdev_deadman(vdev_t *vd)
3431 3439 {
3432 3440 for (int c = 0; c < vd->vdev_children; c++) {
3433 3441 vdev_t *cvd = vd->vdev_child[c];
3434 3442
3435 3443 vdev_deadman(cvd);
3436 3444 }
3437 3445
3438 3446 if (vd->vdev_ops->vdev_op_leaf) {
3439 3447 vdev_queue_t *vq = &vd->vdev_queue;
3440 3448
3441 3449 mutex_enter(&vq->vq_lock);
3442 3450 if (avl_numnodes(&vq->vq_active_tree) > 0) {
3443 3451 spa_t *spa = vd->vdev_spa;
3444 3452 zio_t *fio;
3445 3453 uint64_t delta;
3446 3454
3447 3455 /*
3448 3456 * Look at the head of all the pending queues,
3449 3457 * if any I/O has been outstanding for longer than
3450 3458 * the spa_deadman_synctime we panic the system.
3451 3459 */
3452 3460 fio = avl_first(&vq->vq_active_tree);
3453 3461 delta = gethrtime() - fio->io_timestamp;
3454 3462 if (delta > spa_deadman_synctime(spa)) {
3455 3463 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3456 3464 "delta %lluns, last io %lluns",
3457 3465 fio->io_timestamp, delta,
3458 3466 vq->vq_io_complete_ts);
3459 3467 fm_panic("I/O to pool '%s' appears to be "
3460 3468 "hung.", spa_name(spa));
3461 3469 }
3462 3470 }
3463 3471 mutex_exit(&vq->vq_lock);
3464 3472 }
3465 3473 }
↓ open down ↓ |
2308 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX