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