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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/spa.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/zio.h>
37 #include <sys/abd.h>
38 #include <sys/fs/zfs.h>
39
40 /*
41 * Virtual device vector for mirroring.
42 */
43
44 typedef struct mirror_child {
45 vdev_t *mc_vd;
46 uint64_t mc_offset;
47 int mc_error;
48 uint8_t mc_tried;
49 uint8_t mc_skipped;
50 uint8_t mc_speculative;
51 } mirror_child_t;
52
53 typedef struct mirror_map {
54 int mm_children;
55 int mm_resilvering;
56 int mm_preferred;
57 int mm_root;
58 mirror_child_t mm_child[1];
59 } mirror_map_t;
60
61 int vdev_mirror_shift = 21;
62
63 static void
64 vdev_mirror_map_free(zio_t *zio)
65 {
66 mirror_map_t *mm = zio->io_vsd;
67
68 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
69 }
70
71 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
72 vdev_mirror_map_free,
73 zio_vsd_default_cksum_report
74 };
75
76 static mirror_map_t *
77 vdev_mirror_map_alloc(zio_t *zio)
78 {
79 mirror_map_t *mm = NULL;
80 mirror_child_t *mc;
81 vdev_t *vd = zio->io_vd;
82 int c, d;
83
84 if (vd == NULL) {
85 dva_t *dva = zio->io_bp->blk_dva;
86 spa_t *spa = zio->io_spa;
87 dva_t dva_copy[SPA_DVAS_PER_BP];
88
89 c = BP_GET_NDVAS(zio->io_bp);
90
91 /*
92 * If we do not trust the pool config, some DVAs might be
93 * invalid or point to vdevs that do not exist. We skip them.
94 */
95 if (!spa_trust_config(spa)) {
96 ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
97 int j = 0;
98 for (int i = 0; i < c; i++) {
99 if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
100 dva_copy[j++] = dva[i];
101 }
102 if (j == 0) {
103 zio->io_vsd = NULL;
104 zio->io_error = ENXIO;
105 return (NULL);
106 }
107 if (j < c) {
108 dva = dva_copy;
109 c = j;
110 }
111 }
112
113 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
114 mm->mm_children = c;
115 mm->mm_resilvering = B_FALSE;
116 mm->mm_preferred = spa_get_random(c);
117 mm->mm_root = B_TRUE;
118
119 /*
120 * Check the other, lower-index DVAs to see if they're on
121 * the same vdev as the child we picked. If they are, use
122 * them since they are likely to have been allocated from
123 * the primary metaslab in use at the time, and hence are
124 * more likely to have locality with single-copy data.
125 */
126 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
127 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
128 mm->mm_preferred = d;
129 }
130
131 for (c = 0; c < mm->mm_children; c++) {
132 mc = &mm->mm_child[c];
133
134 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
135 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
136 }
137 } else {
138 int replacing;
139
140 c = vd->vdev_children;
141
142 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
143 mm->mm_children = c;
144 /*
145 * If we are resilvering, then we should handle scrub reads
146 * differently; we shouldn't issue them to the resilvering
147 * device because it might not have those blocks.
148 *
149 * We are resilvering iff:
150 * 1) We are a replacing vdev (ie our name is "replacing-1" or
151 * "spare-1" or something like that), and
152 * 2) The pool is currently being resilvered.
153 *
154 * We cannot simply check vd->vdev_resilver_txg, because it's
155 * not set in this path.
170 * If a spa load is in progress, then spa_dsl_pool may be
171 * uninitialized. But we shouldn't be resilvering during a spa
172 * load anyway.
173 */
174 if (replacing &&
175 (spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE) &&
176 dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool)) {
177 mm->mm_resilvering = B_TRUE;
178 } else {
179 mm->mm_resilvering = B_FALSE;
180 }
181
182 mm->mm_preferred = mm->mm_resilvering ? 0 :
183 (zio->io_offset >> vdev_mirror_shift) % c;
184 mm->mm_root = B_FALSE;
185
186 for (c = 0; c < mm->mm_children; c++) {
187 mc = &mm->mm_child[c];
188 mc->mc_vd = vd->vdev_child[c];
189 mc->mc_offset = zio->io_offset;
190 }
191 }
192
193 zio->io_vsd = mm;
194 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
195 return (mm);
196 }
197
198 static int
199 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
200 uint64_t *ashift)
201 {
202 int numerrors = 0;
203 int lasterror = 0;
204
205 if (vd->vdev_children == 0) {
206 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
207 return (SET_ERROR(EINVAL));
208 }
209
210 vdev_open_children(vd);
211
212 for (int c = 0; c < vd->vdev_children; c++) {
213 vdev_t *cvd = vd->vdev_child[c];
214
215 if (cvd->vdev_open_error) {
216 lasterror = cvd->vdev_open_error;
217 numerrors++;
218 continue;
219 }
220
221 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
222 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
223 *ashift = MAX(*ashift, cvd->vdev_ashift);
224 }
225
226 if (numerrors == vd->vdev_children) {
227 if (vdev_children_are_offline(vd))
228 vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
229 else
230 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
231 return (lasterror);
232 }
233
234 return (0);
235 }
236
237 static void
238 vdev_mirror_close(vdev_t *vd)
239 {
240 for (int c = 0; c < vd->vdev_children; c++)
241 vdev_close(vd->vdev_child[c]);
242 }
243
244 static void
245 vdev_mirror_child_done(zio_t *zio)
246 {
247 mirror_child_t *mc = zio->io_private;
248
249 mc->mc_error = zio->io_error;
259 if (zio->io_error == 0) {
260 zio_t *pio;
261 zio_link_t *zl = NULL;
262
263 mutex_enter(&zio->io_lock);
264 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
265 mutex_enter(&pio->io_lock);
266 ASSERT3U(zio->io_size, >=, pio->io_size);
267 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
268 mutex_exit(&pio->io_lock);
269 }
270 mutex_exit(&zio->io_lock);
271 }
272 abd_free(zio->io_abd);
273
274 mc->mc_error = zio->io_error;
275 mc->mc_tried = 1;
276 mc->mc_skipped = 0;
277 }
278
279 /*
280 * Try to find a child whose DTL doesn't contain the block we want to read.
281 * If we can't, try the read on any vdev we haven't already tried.
282 */
283 static int
284 vdev_mirror_child_select(zio_t *zio)
285 {
286 mirror_map_t *mm = zio->io_vsd;
287 mirror_child_t *mc;
288 uint64_t txg = zio->io_txg;
289 int i, c;
290
291 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
292
293 /*
294 * Try to find a child whose DTL doesn't contain the block to read.
295 * If a child is known to be completely inaccessible (indicated by
296 * vdev_readable() returning B_FALSE), don't even try.
297 */
298 for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
299 if (c >= mm->mm_children)
300 c = 0;
301 mc = &mm->mm_child[c];
302 if (mc->mc_tried || mc->mc_skipped)
303 continue;
304 if (!vdev_readable(mc->mc_vd)) {
305 mc->mc_error = SET_ERROR(ENXIO);
306 mc->mc_tried = 1; /* don't even try */
307 mc->mc_skipped = 1;
308 continue;
309 }
310 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
311 return (c);
312 mc->mc_error = SET_ERROR(ESTALE);
313 mc->mc_skipped = 1;
314 mc->mc_speculative = 1;
315 }
316
317 /*
318 * Every device is either missing or has this txg in its DTL.
319 * Look for any child we haven't already tried before giving up.
320 */
321 for (c = 0; c < mm->mm_children; c++)
322 if (!mm->mm_child[c].mc_tried)
323 return (c);
324
325 /*
326 * Every child failed. There's no place left to look.
327 */
328 return (-1);
329 }
330
331 static void
332 vdev_mirror_io_start(zio_t *zio)
333 {
334 mirror_map_t *mm;
335 mirror_child_t *mc;
336 int c, children;
337
338 mm = vdev_mirror_map_alloc(zio);
339
340 if (mm == NULL) {
341 ASSERT(!spa_trust_config(zio->io_spa));
342 ASSERT(zio->io_type == ZIO_TYPE_READ);
343 zio_execute(zio);
344 return;
345 }
346
347 if (zio->io_type == ZIO_TYPE_READ) {
348 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
349 /*
350 * For scrubbing reads we need to allocate a read
351 * buffer for each child and issue reads to all
352 * children. If any child succeeds, it will copy its
353 * data into zio->io_data in vdev_mirror_scrub_done.
354 */
355 for (c = 0; c < mm->mm_children; c++) {
356 mc = &mm->mm_child[c];
357 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
358 mc->mc_vd, mc->mc_offset,
359 abd_alloc_sametype(zio->io_abd,
360 zio->io_size), zio->io_size,
361 zio->io_type, zio->io_priority, 0,
362 vdev_mirror_scrub_done, mc));
363 }
364 zio_execute(zio);
365 return;
366 }
367 /*
368 * For normal reads just pick one child.
369 */
370 c = vdev_mirror_child_select(zio);
371 children = (c >= 0);
372 } else {
373 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
374
375 /*
376 * Writes go to all children.
377 */
378 c = 0;
379 children = mm->mm_children;
380 }
381
382 while (children--) {
383 mc = &mm->mm_child[c];
384 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
385 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
386 zio->io_type, zio->io_priority, 0,
387 vdev_mirror_child_done, mc));
388 c++;
389 }
390
391 zio_execute(zio);
392 }
393
394 static int
395 vdev_mirror_worst_error(mirror_map_t *mm)
396 {
397 int error[2] = { 0, 0 };
398
399 for (int c = 0; c < mm->mm_children; c++) {
400 mirror_child_t *mc = &mm->mm_child[c];
401 int s = mc->mc_speculative;
402 error[s] = zio_worst_error(error[s], mc->mc_error);
403 }
404
405 return (error[0] ? error[0] : error[1]);
406 }
407
408 static void
409 vdev_mirror_io_done(zio_t *zio)
410 {
411 mirror_map_t *mm = zio->io_vsd;
412 mirror_child_t *mc;
413 int c;
414 int good_copies = 0;
415 int unexpected_errors = 0;
416
417 if (mm == NULL)
418 return;
419
420 for (c = 0; c < mm->mm_children; c++) {
421 mc = &mm->mm_child[c];
422
423 if (mc->mc_error) {
424 if (!mc->mc_skipped)
425 unexpected_errors++;
426 } else if (mc->mc_tried) {
427 good_copies++;
428 }
429 }
430
431 if (zio->io_type == ZIO_TYPE_WRITE) {
432 /*
433 * XXX -- for now, treat partial writes as success.
434 *
435 * Now that we support write reallocation, it would be better
436 * to treat partial failure as real failure unless there are
437 * no non-degraded top-level vdevs left, and not update DTLs
438 * if we intend to reallocate.
439 */
505 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
506 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
507 zio->io_txg, 1))
508 continue;
509 mc->mc_error = SET_ERROR(ESTALE);
510 }
511
512 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
513 mc->mc_vd, mc->mc_offset,
514 zio->io_abd, zio->io_size,
515 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
516 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
517 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
518 }
519 }
520 }
521
522 static void
523 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
524 {
525 if (faulted == vd->vdev_children) {
526 if (vdev_children_are_offline(vd)) {
527 vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
528 VDEV_AUX_CHILDREN_OFFLINE);
529 } else {
530 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
531 VDEV_AUX_NO_REPLICAS);
532 }
533 } else if (degraded + faulted != 0) {
534 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
535 } else {
536 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
537 }
538 }
539
540 vdev_ops_t vdev_mirror_ops = {
541 vdev_mirror_open,
542 vdev_mirror_close,
543 vdev_default_asize,
544 vdev_mirror_io_start,
545 vdev_mirror_io_done,
546 vdev_mirror_state_change,
547 NULL,
548 NULL,
549 NULL,
550 VDEV_TYPE_MIRROR, /* name of this vdev type */
551 B_FALSE /* not a leaf vdev */
552 };
553
554 vdev_ops_t vdev_replacing_ops = {
555 vdev_mirror_open,
556 vdev_mirror_close,
557 vdev_default_asize,
|
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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
29 */
30
31 #include <sys/zfs_context.h>
32 #include <sys/spa.h>
33 #include <sys/spa_impl.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/dsl_scan.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/zio.h>
38 #include <sys/abd.h>
39 #include <sys/wbc.h>
40 #include <sys/fs/zfs.h>
41
42 /*
43 * Virtual device vector for mirroring.
44 */
45
46 typedef struct mirror_child {
47 vdev_t *mc_vd;
48 uint64_t mc_offset;
49 int mc_error;
50 uint8_t mc_tried;
51 uint8_t mc_skipped;
52 uint8_t mc_speculative;
53 int mc_index; /* index in mirror_map_t */
54 avl_node_t mc_node; /* used for sorting based on weight */
55 int64_t mc_weight; /* thread-local copy of vdev_weight */
56 } mirror_child_t;
57
58 typedef struct mirror_map {
59 int mm_children;
60 int mm_resilvering;
61 int mm_preferred;
62 int mm_root;
63 mirror_child_t mm_child[1];
64 } mirror_map_t;
65
66 int vdev_mirror_shift = 21;
67
68 static void
69 vdev_mirror_map_free(zio_t *zio)
70 {
71 mirror_map_t *mm = zio->io_vsd;
72
73 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
74 }
75
76 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
77 vdev_mirror_map_free,
78 zio_vsd_default_cksum_report
79 };
80
81 static mirror_map_t *
82 vdev_mirror_map_alloc(zio_t *zio)
83 {
84 mirror_map_t *mm = NULL;
85 mirror_child_t *mc;
86 vdev_t *vd = zio->io_vd;
87 int c, d;
88
89 if (vd == NULL) {
90 dva_t *dva = zio->io_bp->blk_dva;
91 spa_t *spa = zio->io_spa;
92
93 c = BP_GET_NDVAS(zio->io_bp);
94
95 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
96 mm->mm_children = c;
97 mm->mm_resilvering = B_FALSE;
98 mm->mm_preferred = spa_get_random(c);
99 mm->mm_root = B_TRUE;
100
101 /*
102 * Check the other, lower-index DVAs to see if they're on
103 * the same vdev as the child we picked. If they are, use
104 * them since they are likely to have been allocated from
105 * the primary metaslab in use at the time, and hence are
106 * more likely to have locality with single-copy data.
107 */
108 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
109 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
110 mm->mm_preferred = d;
111 }
112
113 for (c = 0; c < mm->mm_children; c++) {
114 mc = &mm->mm_child[c];
115
116 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
117 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
118 mc->mc_index = c;
119 mc->mc_weight = (mc->mc_vd != NULL ?
120 mc->mc_vd->vdev_weight : 0);
121 }
122 } else {
123 int replacing;
124
125 c = vd->vdev_children;
126
127 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
128 mm->mm_children = c;
129 /*
130 * If we are resilvering, then we should handle scrub reads
131 * differently; we shouldn't issue them to the resilvering
132 * device because it might not have those blocks.
133 *
134 * We are resilvering iff:
135 * 1) We are a replacing vdev (ie our name is "replacing-1" or
136 * "spare-1" or something like that), and
137 * 2) The pool is currently being resilvered.
138 *
139 * We cannot simply check vd->vdev_resilver_txg, because it's
140 * not set in this path.
155 * If a spa load is in progress, then spa_dsl_pool may be
156 * uninitialized. But we shouldn't be resilvering during a spa
157 * load anyway.
158 */
159 if (replacing &&
160 (spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE) &&
161 dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool)) {
162 mm->mm_resilvering = B_TRUE;
163 } else {
164 mm->mm_resilvering = B_FALSE;
165 }
166
167 mm->mm_preferred = mm->mm_resilvering ? 0 :
168 (zio->io_offset >> vdev_mirror_shift) % c;
169 mm->mm_root = B_FALSE;
170
171 for (c = 0; c < mm->mm_children; c++) {
172 mc = &mm->mm_child[c];
173 mc->mc_vd = vd->vdev_child[c];
174 mc->mc_offset = zio->io_offset;
175 mc->mc_index = c;
176 mc->mc_weight = (mc->mc_vd != NULL ?
177 mc->mc_vd->vdev_weight : 0);
178 }
179 }
180
181 zio->io_vsd = mm;
182 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
183 return (mm);
184 }
185
186 static int
187 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
188 uint64_t *ashift)
189 {
190 int numerrors = 0;
191 int lasterror = 0;
192
193 if (vd->vdev_children == 0) {
194 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
195 return (SET_ERROR(EINVAL));
196 }
197
198 vdev_open_children(vd);
199
200 for (int c = 0; c < vd->vdev_children; c++) {
201 vdev_t *cvd = vd->vdev_child[c];
202
203 if (cvd->vdev_open_error) {
204 lasterror = cvd->vdev_open_error;
205 numerrors++;
206 continue;
207 }
208
209 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
210 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
211 *ashift = MAX(*ashift, cvd->vdev_ashift);
212 }
213
214 if (numerrors == vd->vdev_children) {
215 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
216 return (lasterror);
217 }
218
219 return (0);
220 }
221
222 static void
223 vdev_mirror_close(vdev_t *vd)
224 {
225 for (int c = 0; c < vd->vdev_children; c++)
226 vdev_close(vd->vdev_child[c]);
227 }
228
229 static void
230 vdev_mirror_child_done(zio_t *zio)
231 {
232 mirror_child_t *mc = zio->io_private;
233
234 mc->mc_error = zio->io_error;
244 if (zio->io_error == 0) {
245 zio_t *pio;
246 zio_link_t *zl = NULL;
247
248 mutex_enter(&zio->io_lock);
249 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
250 mutex_enter(&pio->io_lock);
251 ASSERT3U(zio->io_size, >=, pio->io_size);
252 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
253 mutex_exit(&pio->io_lock);
254 }
255 mutex_exit(&zio->io_lock);
256 }
257 abd_free(zio->io_abd);
258
259 mc->mc_error = zio->io_error;
260 mc->mc_tried = 1;
261 mc->mc_skipped = 0;
262 }
263
264 static int
265 vdev_weight_compar(const void *mc_a, const void *mc_b)
266 {
267 const mirror_child_t *a = mc_a, *b = mc_b;
268
269 /*
270 * 1) if a's weight is less than b's, a goes right in the tree
271 * 2) if a's weight is greater than b's, a goes left
272 * 3) if a's and b's weights are equal, lower map index goes left
273 * 4) if weight and map index are equal, it's the same object
274 */
275 if (a->mc_weight < b->mc_weight)
276 return (1);
277 if (a->mc_weight > b->mc_weight)
278 return (-1);
279 if (a->mc_index > b->mc_index)
280 return (1);
281 if (a->mc_index < b->mc_index)
282 return (-1);
283 ASSERT3P(a->mc_vd, ==, b->mc_vd);
284 return (0);
285 }
286
287 static boolean_t
288 child_select_mc(mirror_child_t *mc, uint64_t txg)
289 {
290 if (mc->mc_tried || mc->mc_skipped)
291 return (B_FALSE);
292 if (!vdev_readable(mc->mc_vd)) {
293 mc->mc_error = SET_ERROR(ENXIO);
294 mc->mc_tried = 1; /* don't even try */
295 mc->mc_skipped = 1;
296 return (B_FALSE);
297 }
298 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
299 mc->mc_weight--;
300 mc->mc_vd->vdev_weight = mc->mc_weight;
301 return (B_TRUE);
302 }
303 mc->mc_error = SET_ERROR(ESTALE);
304 mc->mc_skipped = 1;
305 mc->mc_speculative = 1;
306 return (B_FALSE);
307 }
308
309 static void
310 child_select_cleanup(mirror_map_t *mm, avl_tree_t *vdevs_by_weight)
311 {
312 for (int i = 0; i < mm->mm_children; i++)
313 avl_remove(vdevs_by_weight, &mm->mm_child[i]);
314 avl_destroy(vdevs_by_weight);
315 }
316
317 /*
318 * Try to find a child whose DTL doesn't contain the block we want to read.
319 * If we can't, try the read on any vdev we haven't already tried.
320 */
321 static int
322 vdev_mirror_child_select(zio_t *zio)
323 {
324 mirror_map_t *mm = zio->io_vsd;
325 uint64_t txg = zio->io_txg;
326 /*
327 * Look at the weights of the vdevs in the mirror; the weights help
328 * decide which vdev to read from; the highest-weight suitable child
329 * index is returned, and its weight is decremented in order to avoid
330 * creating "hot" devices; once all the vdevs' weights are zero, the
331 * weights are set back to the ones configured in vdev props
332 */
333 int64_t max_weight = 0;
334
335 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
336
337 for (int c = 0; c < mm->mm_children; c++) {
338 mirror_child_t *mc = &mm->mm_child[c];
339 if (mc->mc_vd == NULL)
340 continue;
341 max_weight = MAX(max_weight, mc->mc_weight);
342 }
343
344 /*
345 * Recalculate weights
346 */
347 if (max_weight == 0) {
348 for (int c = 0; c < mm->mm_children; c++) {
349 mirror_child_t *mc = &mm->mm_child[c];
350 if (mc->mc_vd == NULL)
351 continue;
352 mc->mc_weight =
353 vdev_queue_get_prop_uint64(&mc->mc_vd->vdev_queue,
354 VDEV_PROP_PREFERRED_READ) + 1;
355 mc->mc_vd->vdev_weight = mc->mc_weight;
356 }
357 }
358
359 if (mm->mm_children > 1) {
360 avl_tree_t vdevs_by_weight;
361
362 avl_create(&vdevs_by_weight, vdev_weight_compar,
363 sizeof (mirror_child_t), offsetof(mirror_child_t, mc_node));
364
365 /*
366 * Sort the weighted list
367 */
368 for (int i = 0; i < mm->mm_children; i++)
369 avl_add(&vdevs_by_weight, &mm->mm_child[i]);
370
371 /*
372 * Try to find a child whose DTL doesn't contain the block to
373 * read. If a child is known to be completely inaccessible
374 * (vdev_readable() returning B_FALSE), don't even try.
375 */
376 for (mirror_child_t *mc = avl_first(&vdevs_by_weight);
377 mc != NULL; mc = AVL_NEXT(&vdevs_by_weight, mc)) {
378 if (child_select_mc(mc, txg)) {
379 child_select_cleanup(mm, &vdevs_by_weight);
380 return (mc->mc_index);
381 }
382 }
383 child_select_cleanup(mm, &vdevs_by_weight);
384 } else {
385 if (child_select_mc(&mm->mm_child[0], txg))
386 return (0);
387 }
388
389 /*
390 * Every device is either missing or has this txg in its DTL.
391 * Look for any child we haven't already tried before giving up.
392 */
393 for (int c = 0; c < mm->mm_children; c++)
394 if (!mm->mm_child[c].mc_tried && mm->mm_child[c].mc_vd != NULL)
395 return (c);
396
397 /*
398 * Every child failed. There's no place left to look.
399 */
400 return (-1);
401 }
402
403 static void
404 vdev_mirror_io_start(zio_t *zio)
405 {
406 mirror_map_t *mm;
407 mirror_child_t *mc;
408 int c, children;
409 boolean_t spec_case = B_FALSE;
410 spa_t *spa = zio->io_spa;
411
412 mm = vdev_mirror_map_alloc(zio);
413
414 if (zio->io_child_type != ZIO_CHILD_VDEV &&
415 BP_IS_SPECIAL(zio->io_bp))
416 spec_case = B_TRUE;
417
418 if (zio->io_type == ZIO_TYPE_READ) {
419 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
420 int target = 0;
421
422 /*
423 * Scrub of special BPs should take into
424 * account the state of WBC-Window
425 */
426 if (spec_case) {
427 target = wbc_select_dva(
428 spa_get_wbc_data(spa), zio);
429 }
430
431 /*
432 * For scrubbing reads we need to allocate a read
433 * buffer for each child and issue reads to all
434 * children. If any child succeeds, it will copy its
435 * data into zio->io_data in vdev_mirror_scrub_done.
436 */
437 for (c = 0; c < mm->mm_children; c++) {
438 mc = &mm->mm_child[c];
439 if (mc->mc_vd == NULL) {
440 /*
441 * Invalid vdev id in blkptr caused
442 * mc_vd to be NULL here.
443 * Just skip this vdev.
444 */
445 continue;
446 }
447
448 if (spec_case && c != target)
449 continue;
450
451 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
452 mc->mc_vd, mc->mc_offset,
453 abd_alloc_sametype(zio->io_abd,
454 zio->io_size), zio->io_size,
455 zio->io_type, zio->io_priority, 0,
456 vdev_mirror_scrub_done, mc));
457 }
458 zio_execute(zio);
459 return;
460 }
461 /*
462 * For normal reads just pick one child.
463 */
464
465 if (spec_case)
466 c = wbc_select_dva(spa_get_wbc_data(spa), zio);
467 else
468 c = vdev_mirror_child_select(zio);
469
470 children = (c >= 0);
471 } else {
472 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
473
474 /*
475 * Writes go to all children.
476 */
477 c = 0;
478 children = mm->mm_children;
479 }
480
481 for (; children--; c++) {
482 mc = &mm->mm_child[c];
483 if (mc->mc_vd == NULL) {
484 /*
485 * Invalid vdev in blkptr caused mc_vd to be NULL here.
486 * Just skip this vdev.
487 */
488 continue;
489 }
490
491 if (spec_case) {
492 if (zio->io_type == ZIO_TYPE_WRITE &&
493 !vdev_is_special(mc->mc_vd))
494 continue;
495 }
496
497 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
498 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
499 zio->io_type, zio->io_priority, 0,
500 vdev_mirror_child_done, mc));
501
502 if (spec_case)
503 break;
504 }
505
506 zio_execute(zio);
507 }
508
509 static int
510 vdev_mirror_worst_error(mirror_map_t *mm)
511 {
512 int error[2] = { 0, 0 };
513
514 for (int c = 0; c < mm->mm_children; c++) {
515 mirror_child_t *mc = &mm->mm_child[c];
516 int s = mc->mc_speculative;
517 error[s] = zio_worst_error(error[s], mc->mc_error);
518 }
519
520 return (error[0] ? error[0] : error[1]);
521 }
522
523 static void
524 vdev_mirror_io_done(zio_t *zio)
525 {
526 mirror_map_t *mm = zio->io_vsd;
527 mirror_child_t *mc;
528 int c;
529 int good_copies = 0;
530 int unexpected_errors = 0;
531
532 for (c = 0; c < mm->mm_children; c++) {
533 mc = &mm->mm_child[c];
534
535 if (mc->mc_error) {
536 if (!mc->mc_skipped)
537 unexpected_errors++;
538 } else if (mc->mc_tried) {
539 good_copies++;
540 }
541 }
542
543 if (zio->io_type == ZIO_TYPE_WRITE) {
544 /*
545 * XXX -- for now, treat partial writes as success.
546 *
547 * Now that we support write reallocation, it would be better
548 * to treat partial failure as real failure unless there are
549 * no non-degraded top-level vdevs left, and not update DTLs
550 * if we intend to reallocate.
551 */
617 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
618 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
619 zio->io_txg, 1))
620 continue;
621 mc->mc_error = SET_ERROR(ESTALE);
622 }
623
624 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
625 mc->mc_vd, mc->mc_offset,
626 zio->io_abd, zio->io_size,
627 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
628 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
629 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
630 }
631 }
632 }
633
634 static void
635 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
636 {
637 if (faulted == vd->vdev_children)
638 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
639 VDEV_AUX_NO_REPLICAS);
640 else if (degraded + faulted != 0)
641 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
642 else
643 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
644 }
645
646 vdev_ops_t vdev_mirror_ops = {
647 vdev_mirror_open,
648 vdev_mirror_close,
649 vdev_default_asize,
650 vdev_mirror_io_start,
651 vdev_mirror_io_done,
652 vdev_mirror_state_change,
653 NULL,
654 NULL,
655 NULL,
656 VDEV_TYPE_MIRROR, /* name of this vdev type */
657 B_FALSE /* not a leaf vdev */
658 };
659
660 vdev_ops_t vdev_replacing_ops = {
661 vdev_mirror_open,
662 vdev_mirror_close,
663 vdev_default_asize,
|