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