Print this page
4045 zfs write throttle & i/o scheduler performance work
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/vdev_mirror.c
+++ new/usr/src/uts/common/fs/zfs/vdev_mirror.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 28 */
29 29
30 30 #include <sys/zfs_context.h>
31 31 #include <sys/spa.h>
32 32 #include <sys/vdev_impl.h>
33 33 #include <sys/zio.h>
34 34 #include <sys/fs/zfs.h>
35 35
36 36 /*
37 37 * Virtual device vector for mirroring.
38 38 */
39 39
40 40 typedef struct mirror_child {
41 41 vdev_t *mc_vd;
42 42 uint64_t mc_offset;
43 43 int mc_error;
44 44 uint8_t mc_tried;
45 45 uint8_t mc_skipped;
46 46 uint8_t mc_speculative;
47 47 } mirror_child_t;
48 48
49 49 typedef struct mirror_map {
50 50 int mm_children;
51 51 int mm_replacing;
52 52 int mm_preferred;
53 53 int mm_root;
54 54 mirror_child_t mm_child[1];
55 55 } mirror_map_t;
56 56
57 57 int vdev_mirror_shift = 21;
58 58
59 59 static void
60 60 vdev_mirror_map_free(zio_t *zio)
61 61 {
62 62 mirror_map_t *mm = zio->io_vsd;
63 63
64 64 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
65 65 }
66 66
67 67 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
68 68 vdev_mirror_map_free,
69 69 zio_vsd_default_cksum_report
70 70 };
71 71
72 72 static mirror_map_t *
73 73 vdev_mirror_map_alloc(zio_t *zio)
74 74 {
75 75 mirror_map_t *mm = NULL;
76 76 mirror_child_t *mc;
77 77 vdev_t *vd = zio->io_vd;
78 78 int c, d;
79 79
80 80 if (vd == NULL) {
81 81 dva_t *dva = zio->io_bp->blk_dva;
82 82 spa_t *spa = zio->io_spa;
83 83
84 84 c = BP_GET_NDVAS(zio->io_bp);
85 85
86 86 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
87 87 mm->mm_children = c;
88 88 mm->mm_replacing = B_FALSE;
89 89 mm->mm_preferred = spa_get_random(c);
90 90 mm->mm_root = B_TRUE;
91 91
92 92 /*
93 93 * Check the other, lower-index DVAs to see if they're on
94 94 * the same vdev as the child we picked. If they are, use
95 95 * them since they are likely to have been allocated from
96 96 * the primary metaslab in use at the time, and hence are
97 97 * more likely to have locality with single-copy data.
98 98 */
99 99 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
100 100 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
101 101 mm->mm_preferred = d;
102 102 }
103 103
104 104 for (c = 0; c < mm->mm_children; c++) {
105 105 mc = &mm->mm_child[c];
106 106
107 107 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
108 108 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
109 109 }
110 110 } else {
111 111 c = vd->vdev_children;
112 112
113 113 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
114 114 mm->mm_children = c;
115 115 mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
116 116 vd->vdev_ops == &vdev_spare_ops);
117 117 mm->mm_preferred = mm->mm_replacing ? 0 :
118 118 (zio->io_offset >> vdev_mirror_shift) % c;
119 119 mm->mm_root = B_FALSE;
120 120
121 121 for (c = 0; c < mm->mm_children; c++) {
122 122 mc = &mm->mm_child[c];
123 123 mc->mc_vd = vd->vdev_child[c];
124 124 mc->mc_offset = zio->io_offset;
125 125 }
126 126 }
127 127
128 128 zio->io_vsd = mm;
129 129 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
130 130 return (mm);
131 131 }
132 132
133 133 static int
134 134 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
135 135 uint64_t *ashift)
136 136 {
137 137 int numerrors = 0;
138 138 int lasterror = 0;
139 139
140 140 if (vd->vdev_children == 0) {
141 141 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
142 142 return (SET_ERROR(EINVAL));
143 143 }
144 144
145 145 vdev_open_children(vd);
146 146
147 147 for (int c = 0; c < vd->vdev_children; c++) {
148 148 vdev_t *cvd = vd->vdev_child[c];
149 149
150 150 if (cvd->vdev_open_error) {
151 151 lasterror = cvd->vdev_open_error;
152 152 numerrors++;
153 153 continue;
154 154 }
155 155
156 156 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
157 157 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
158 158 *ashift = MAX(*ashift, cvd->vdev_ashift);
159 159 }
160 160
161 161 if (numerrors == vd->vdev_children) {
162 162 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
163 163 return (lasterror);
164 164 }
165 165
166 166 return (0);
167 167 }
168 168
169 169 static void
170 170 vdev_mirror_close(vdev_t *vd)
171 171 {
172 172 for (int c = 0; c < vd->vdev_children; c++)
173 173 vdev_close(vd->vdev_child[c]);
174 174 }
175 175
176 176 static void
177 177 vdev_mirror_child_done(zio_t *zio)
178 178 {
179 179 mirror_child_t *mc = zio->io_private;
180 180
181 181 mc->mc_error = zio->io_error;
182 182 mc->mc_tried = 1;
183 183 mc->mc_skipped = 0;
184 184 }
185 185
186 186 static void
187 187 vdev_mirror_scrub_done(zio_t *zio)
188 188 {
189 189 mirror_child_t *mc = zio->io_private;
190 190
191 191 if (zio->io_error == 0) {
192 192 zio_t *pio;
193 193
194 194 mutex_enter(&zio->io_lock);
195 195 while ((pio = zio_walk_parents(zio)) != NULL) {
196 196 mutex_enter(&pio->io_lock);
197 197 ASSERT3U(zio->io_size, >=, pio->io_size);
198 198 bcopy(zio->io_data, pio->io_data, pio->io_size);
199 199 mutex_exit(&pio->io_lock);
200 200 }
201 201 mutex_exit(&zio->io_lock);
202 202 }
203 203
204 204 zio_buf_free(zio->io_data, zio->io_size);
205 205
206 206 mc->mc_error = zio->io_error;
207 207 mc->mc_tried = 1;
208 208 mc->mc_skipped = 0;
209 209 }
210 210
211 211 /*
212 212 * Try to find a child whose DTL doesn't contain the block we want to read.
213 213 * If we can't, try the read on any vdev we haven't already tried.
214 214 */
215 215 static int
216 216 vdev_mirror_child_select(zio_t *zio)
217 217 {
218 218 mirror_map_t *mm = zio->io_vsd;
219 219 mirror_child_t *mc;
220 220 uint64_t txg = zio->io_txg;
221 221 int i, c;
222 222
223 223 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
224 224
225 225 /*
226 226 * Try to find a child whose DTL doesn't contain the block to read.
227 227 * If a child is known to be completely inaccessible (indicated by
228 228 * vdev_readable() returning B_FALSE), don't even try.
229 229 */
230 230 for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
231 231 if (c >= mm->mm_children)
232 232 c = 0;
233 233 mc = &mm->mm_child[c];
234 234 if (mc->mc_tried || mc->mc_skipped)
235 235 continue;
236 236 if (!vdev_readable(mc->mc_vd)) {
237 237 mc->mc_error = SET_ERROR(ENXIO);
238 238 mc->mc_tried = 1; /* don't even try */
239 239 mc->mc_skipped = 1;
240 240 continue;
241 241 }
242 242 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
243 243 return (c);
244 244 mc->mc_error = SET_ERROR(ESTALE);
245 245 mc->mc_skipped = 1;
246 246 mc->mc_speculative = 1;
247 247 }
248 248
249 249 /*
250 250 * Every device is either missing or has this txg in its DTL.
251 251 * Look for any child we haven't already tried before giving up.
252 252 */
253 253 for (c = 0; c < mm->mm_children; c++)
254 254 if (!mm->mm_child[c].mc_tried)
255 255 return (c);
256 256
257 257 /*
258 258 * Every child failed. There's no place left to look.
259 259 */
260 260 return (-1);
261 261 }
262 262
263 263 static int
264 264 vdev_mirror_io_start(zio_t *zio)
265 265 {
266 266 mirror_map_t *mm;
267 267 mirror_child_t *mc;
268 268 int c, children;
269 269
270 270 mm = vdev_mirror_map_alloc(zio);
271 271
272 272 if (zio->io_type == ZIO_TYPE_READ) {
273 273 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
274 274 /*
275 275 * For scrubbing reads we need to allocate a read
276 276 * buffer for each child and issue reads to all
277 277 * children. If any child succeeds, it will copy its
278 278 * data into zio->io_data in vdev_mirror_scrub_done.
279 279 */
280 280 for (c = 0; c < mm->mm_children; c++) {
281 281 mc = &mm->mm_child[c];
282 282 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
283 283 mc->mc_vd, mc->mc_offset,
284 284 zio_buf_alloc(zio->io_size), zio->io_size,
285 285 zio->io_type, zio->io_priority, 0,
286 286 vdev_mirror_scrub_done, mc));
287 287 }
288 288 return (ZIO_PIPELINE_CONTINUE);
289 289 }
290 290 /*
291 291 * For normal reads just pick one child.
292 292 */
293 293 c = vdev_mirror_child_select(zio);
294 294 children = (c >= 0);
295 295 } else {
296 296 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
297 297
298 298 /*
299 299 * Writes go to all children.
300 300 */
301 301 c = 0;
302 302 children = mm->mm_children;
303 303 }
304 304
305 305 while (children--) {
306 306 mc = &mm->mm_child[c];
307 307 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
308 308 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
309 309 zio->io_type, zio->io_priority, 0,
310 310 vdev_mirror_child_done, mc));
311 311 c++;
312 312 }
313 313
314 314 return (ZIO_PIPELINE_CONTINUE);
315 315 }
316 316
317 317 static int
318 318 vdev_mirror_worst_error(mirror_map_t *mm)
319 319 {
320 320 int error[2] = { 0, 0 };
321 321
322 322 for (int c = 0; c < mm->mm_children; c++) {
323 323 mirror_child_t *mc = &mm->mm_child[c];
324 324 int s = mc->mc_speculative;
325 325 error[s] = zio_worst_error(error[s], mc->mc_error);
326 326 }
327 327
328 328 return (error[0] ? error[0] : error[1]);
329 329 }
330 330
331 331 static void
332 332 vdev_mirror_io_done(zio_t *zio)
333 333 {
334 334 mirror_map_t *mm = zio->io_vsd;
335 335 mirror_child_t *mc;
336 336 int c;
337 337 int good_copies = 0;
338 338 int unexpected_errors = 0;
339 339
340 340 for (c = 0; c < mm->mm_children; c++) {
341 341 mc = &mm->mm_child[c];
342 342
343 343 if (mc->mc_error) {
344 344 if (!mc->mc_skipped)
345 345 unexpected_errors++;
346 346 } else if (mc->mc_tried) {
347 347 good_copies++;
348 348 }
349 349 }
350 350
351 351 if (zio->io_type == ZIO_TYPE_WRITE) {
352 352 /*
353 353 * XXX -- for now, treat partial writes as success.
354 354 *
355 355 * Now that we support write reallocation, it would be better
356 356 * to treat partial failure as real failure unless there are
357 357 * no non-degraded top-level vdevs left, and not update DTLs
358 358 * if we intend to reallocate.
359 359 */
360 360 /* XXPOLICY */
361 361 if (good_copies != mm->mm_children) {
362 362 /*
363 363 * Always require at least one good copy.
364 364 *
365 365 * For ditto blocks (io_vd == NULL), require
366 366 * all copies to be good.
367 367 *
368 368 * XXX -- for replacing vdevs, there's no great answer.
369 369 * If the old device is really dead, we may not even
370 370 * be able to access it -- so we only want to
371 371 * require good writes to the new device. But if
372 372 * the new device turns out to be flaky, we want
373 373 * to be able to detach it -- which requires all
374 374 * writes to the old device to have succeeded.
375 375 */
376 376 if (good_copies == 0 || zio->io_vd == NULL)
377 377 zio->io_error = vdev_mirror_worst_error(mm);
378 378 }
379 379 return;
380 380 }
381 381
382 382 ASSERT(zio->io_type == ZIO_TYPE_READ);
383 383
384 384 /*
385 385 * If we don't have a good copy yet, keep trying other children.
386 386 */
387 387 /* XXPOLICY */
388 388 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
389 389 ASSERT(c >= 0 && c < mm->mm_children);
390 390 mc = &mm->mm_child[c];
391 391 zio_vdev_io_redone(zio);
392 392 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
393 393 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
394 394 ZIO_TYPE_READ, zio->io_priority, 0,
395 395 vdev_mirror_child_done, mc));
396 396 return;
397 397 }
398 398
399 399 /* XXPOLICY */
400 400 if (good_copies == 0) {
401 401 zio->io_error = vdev_mirror_worst_error(mm);
402 402 ASSERT(zio->io_error != 0);
403 403 }
404 404
405 405 if (good_copies && spa_writeable(zio->io_spa) &&
406 406 (unexpected_errors ||
407 407 (zio->io_flags & ZIO_FLAG_RESILVER) ||
408 408 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
409 409 /*
410 410 * Use the good data we have in hand to repair damaged children.
411 411 */
412 412 for (c = 0; c < mm->mm_children; c++) {
413 413 /*
414 414 * Don't rewrite known good children.
415 415 * Not only is it unnecessary, it could
416 416 * actually be harmful: if the system lost
417 417 * power while rewriting the only good copy,
418 418 * there would be no good copies left!
419 419 */
420 420 mc = &mm->mm_child[c];
421 421
422 422 if (mc->mc_error == 0) {
423 423 if (mc->mc_tried)
424 424 continue;
↓ open down ↓ |
424 lines elided |
↑ open up ↑ |
425 425 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
426 426 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
427 427 zio->io_txg, 1))
428 428 continue;
429 429 mc->mc_error = SET_ERROR(ESTALE);
430 430 }
431 431
432 432 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
433 433 mc->mc_vd, mc->mc_offset,
434 434 zio->io_data, zio->io_size,
435 - ZIO_TYPE_WRITE, zio->io_priority,
435 + ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
436 436 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
437 437 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
438 438 }
439 439 }
440 440 }
441 441
442 442 static void
443 443 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
444 444 {
445 445 if (faulted == vd->vdev_children)
446 446 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
447 447 VDEV_AUX_NO_REPLICAS);
448 448 else if (degraded + faulted != 0)
449 449 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
450 450 else
451 451 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
452 452 }
453 453
454 454 vdev_ops_t vdev_mirror_ops = {
455 455 vdev_mirror_open,
456 456 vdev_mirror_close,
457 457 vdev_default_asize,
458 458 vdev_mirror_io_start,
459 459 vdev_mirror_io_done,
460 460 vdev_mirror_state_change,
461 461 NULL,
462 462 NULL,
463 463 VDEV_TYPE_MIRROR, /* name of this vdev type */
464 464 B_FALSE /* not a leaf vdev */
465 465 };
466 466
467 467 vdev_ops_t vdev_replacing_ops = {
468 468 vdev_mirror_open,
469 469 vdev_mirror_close,
470 470 vdev_default_asize,
471 471 vdev_mirror_io_start,
472 472 vdev_mirror_io_done,
473 473 vdev_mirror_state_change,
474 474 NULL,
475 475 NULL,
476 476 VDEV_TYPE_REPLACING, /* name of this vdev type */
477 477 B_FALSE /* not a leaf vdev */
478 478 };
479 479
480 480 vdev_ops_t vdev_spare_ops = {
481 481 vdev_mirror_open,
482 482 vdev_mirror_close,
483 483 vdev_default_asize,
484 484 vdev_mirror_io_start,
485 485 vdev_mirror_io_done,
486 486 vdev_mirror_state_change,
487 487 NULL,
488 488 NULL,
489 489 VDEV_TYPE_SPARE, /* name of this vdev type */
490 490 B_FALSE /* not a leaf vdev */
491 491 };
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX