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