xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_mirror.c (revision f6f4cb8ada400367a1921f6b93fb9e02f53ac5e6)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/vdev_impl.h>
29 #include <sys/zio.h>
30 #include <sys/fs/zfs.h>
31 
32 /*
33  * Virtual device vector for mirroring.
34  */
35 
36 typedef struct mirror_child {
37 	vdev_t		*mc_vd;
38 	uint64_t	mc_offset;
39 	int		mc_error;
40 	short		mc_tried;
41 	short		mc_skipped;
42 } mirror_child_t;
43 
44 typedef struct mirror_map {
45 	int		mm_children;
46 	int		mm_replacing;
47 	int		mm_preferred;
48 	int		mm_root;
49 	mirror_child_t	mm_child[1];
50 } mirror_map_t;
51 
52 int vdev_mirror_shift = 21;
53 
54 static mirror_map_t *
55 vdev_mirror_map_alloc(zio_t *zio)
56 {
57 	mirror_map_t *mm = NULL;
58 	mirror_child_t *mc;
59 	vdev_t *vd = zio->io_vd;
60 	int c, d;
61 
62 	if (vd == NULL) {
63 		dva_t *dva = zio->io_bp->blk_dva;
64 		spa_t *spa = zio->io_spa;
65 
66 		c = BP_GET_NDVAS(zio->io_bp);
67 
68 		mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
69 		mm->mm_children = c;
70 		mm->mm_replacing = B_FALSE;
71 		mm->mm_preferred = spa_get_random(c);
72 		mm->mm_root = B_TRUE;
73 
74 		/*
75 		 * Check the other, lower-index DVAs to see if they're on
76 		 * the same vdev as the child we picked.  If they are, use
77 		 * them since they are likely to have been allocated from
78 		 * the primary metaslab in use at the time, and hence are
79 		 * more likely to have locality with single-copy data.
80 		 */
81 		for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
82 			if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
83 				mm->mm_preferred = d;
84 		}
85 
86 		for (c = 0; c < mm->mm_children; c++) {
87 			mc = &mm->mm_child[c];
88 
89 			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
90 			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
91 		}
92 	} else {
93 		c = vd->vdev_children;
94 
95 		mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
96 		mm->mm_children = c;
97 		mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
98 		    vd->vdev_ops == &vdev_spare_ops);
99 		mm->mm_preferred = mm->mm_replacing ? 0 :
100 		    (zio->io_offset >> vdev_mirror_shift) % c;
101 		mm->mm_root = B_FALSE;
102 
103 		for (c = 0; c < mm->mm_children; c++) {
104 			mc = &mm->mm_child[c];
105 			mc->mc_vd = vd->vdev_child[c];
106 			mc->mc_offset = zio->io_offset;
107 		}
108 	}
109 
110 	zio->io_vsd = mm;
111 	return (mm);
112 }
113 
114 static void
115 vdev_mirror_map_free(zio_t *zio)
116 {
117 	mirror_map_t *mm = zio->io_vsd;
118 
119 	kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
120 	zio->io_vsd = NULL;
121 }
122 
123 static int
124 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
125 {
126 	vdev_t *cvd;
127 	uint64_t c;
128 	int numerrors = 0;
129 	int ret, lasterror = 0;
130 
131 	if (vd->vdev_children == 0) {
132 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
133 		return (EINVAL);
134 	}
135 
136 	for (c = 0; c < vd->vdev_children; c++) {
137 		cvd = vd->vdev_child[c];
138 
139 		if ((ret = vdev_open(cvd)) != 0) {
140 			lasterror = ret;
141 			numerrors++;
142 			continue;
143 		}
144 
145 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
146 		*ashift = MAX(*ashift, cvd->vdev_ashift);
147 	}
148 
149 	if (numerrors == vd->vdev_children) {
150 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
151 		return (lasterror);
152 	}
153 
154 	return (0);
155 }
156 
157 static void
158 vdev_mirror_close(vdev_t *vd)
159 {
160 	uint64_t c;
161 
162 	for (c = 0; c < vd->vdev_children; c++)
163 		vdev_close(vd->vdev_child[c]);
164 }
165 
166 static void
167 vdev_mirror_child_done(zio_t *zio)
168 {
169 	mirror_child_t *mc = zio->io_private;
170 
171 	mc->mc_error = zio->io_error;
172 	mc->mc_tried = 1;
173 	mc->mc_skipped = 0;
174 }
175 
176 static void
177 vdev_mirror_scrub_done(zio_t *zio)
178 {
179 	mirror_child_t *mc = zio->io_private;
180 
181 	if (zio->io_error == 0) {
182 		zio_t *pio = zio->io_parent;
183 		mutex_enter(&pio->io_lock);
184 		ASSERT3U(zio->io_size, >=, pio->io_size);
185 		bcopy(zio->io_data, pio->io_data, pio->io_size);
186 		mutex_exit(&pio->io_lock);
187 	}
188 
189 	zio_buf_free(zio->io_data, zio->io_size);
190 
191 	mc->mc_error = zio->io_error;
192 	mc->mc_tried = 1;
193 	mc->mc_skipped = 0;
194 }
195 
196 static void
197 vdev_mirror_repair_done(zio_t *zio)
198 {
199 	ASSERT(zio->io_private == zio->io_parent);
200 	vdev_mirror_map_free(zio->io_private);
201 }
202 
203 /*
204  * Try to find a child whose DTL doesn't contain the block we want to read.
205  * If we can't, try the read on any vdev we haven't already tried.
206  */
207 static int
208 vdev_mirror_child_select(zio_t *zio)
209 {
210 	mirror_map_t *mm = zio->io_vsd;
211 	mirror_child_t *mc;
212 	uint64_t txg = zio->io_txg;
213 	int i, c;
214 
215 	ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg);
216 
217 	/*
218 	 * Try to find a child whose DTL doesn't contain the block to read.
219 	 * If a child is known to be completely inaccessible (indicated by
220 	 * vdev_readable() returning B_FALSE), don't even try.
221 	 */
222 	for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
223 		if (c >= mm->mm_children)
224 			c = 0;
225 		mc = &mm->mm_child[c];
226 		if (mc->mc_tried || mc->mc_skipped)
227 			continue;
228 		if (vdev_is_dead(mc->mc_vd) && !vdev_readable(mc->mc_vd)) {
229 			mc->mc_error = ENXIO;
230 			mc->mc_tried = 1;	/* don't even try */
231 			mc->mc_skipped = 1;
232 			continue;
233 		}
234 		if (!vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map, txg, 1))
235 			return (c);
236 		mc->mc_error = ESTALE;
237 		mc->mc_skipped = 1;
238 	}
239 
240 	/*
241 	 * Every device is either missing or has this txg in its DTL.
242 	 * Look for any child we haven't already tried before giving up.
243 	 */
244 	for (c = 0; c < mm->mm_children; c++)
245 		if (!mm->mm_child[c].mc_tried)
246 			return (c);
247 
248 	/*
249 	 * Every child failed.  There's no place left to look.
250 	 */
251 	return (-1);
252 }
253 
254 static int
255 vdev_mirror_io_start(zio_t *zio)
256 {
257 	mirror_map_t *mm;
258 	mirror_child_t *mc;
259 	int c, children;
260 
261 	mm = vdev_mirror_map_alloc(zio);
262 
263 	if (zio->io_type == ZIO_TYPE_READ) {
264 		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
265 			/*
266 			 * For scrubbing reads we need to allocate a read
267 			 * buffer for each child and issue reads to all
268 			 * children.  If any child succeeds, it will copy its
269 			 * data into zio->io_data in vdev_mirror_scrub_done.
270 			 */
271 			for (c = 0; c < mm->mm_children; c++) {
272 				mc = &mm->mm_child[c];
273 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
274 				    mc->mc_vd, mc->mc_offset,
275 				    zio_buf_alloc(zio->io_size), zio->io_size,
276 				    zio->io_type, zio->io_priority,
277 				    ZIO_FLAG_CANFAIL,
278 				    vdev_mirror_scrub_done, mc));
279 			}
280 			return (zio_wait_for_children_done(zio));
281 		}
282 		/*
283 		 * For normal reads just pick one child.
284 		 */
285 		c = vdev_mirror_child_select(zio);
286 		children = (c >= 0);
287 	} else {
288 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
289 
290 		/*
291 		 * If this is a resilvering I/O to a replacing vdev,
292 		 * only the last child should be written -- unless the
293 		 * first child happens to have a DTL entry here as well.
294 		 * All other writes go to all children.
295 		 */
296 		if ((zio->io_flags & ZIO_FLAG_RESILVER) && mm->mm_replacing &&
297 		    !vdev_dtl_contains(&mm->mm_child[0].mc_vd->vdev_dtl_map,
298 		    zio->io_txg, 1)) {
299 			c = mm->mm_children - 1;
300 			children = 1;
301 		} else {
302 			c = 0;
303 			children = mm->mm_children;
304 		}
305 	}
306 
307 	while (children--) {
308 		mc = &mm->mm_child[c];
309 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
310 		    mc->mc_vd, mc->mc_offset,
311 		    zio->io_data, zio->io_size, zio->io_type, zio->io_priority,
312 		    ZIO_FLAG_CANFAIL, vdev_mirror_child_done, mc));
313 		c++;
314 	}
315 
316 	return (zio_wait_for_children_done(zio));
317 }
318 
319 static int
320 vdev_mirror_io_done(zio_t *zio)
321 {
322 	mirror_map_t *mm = zio->io_vsd;
323 	mirror_child_t *mc;
324 	int c;
325 	int good_copies = 0;
326 	int unexpected_errors = 0;
327 
328 	zio->io_error = 0;
329 	zio->io_numerrors = 0;
330 
331 	for (c = 0; c < mm->mm_children; c++) {
332 		mc = &mm->mm_child[c];
333 
334 		if (mc->mc_tried && mc->mc_error == 0) {
335 			good_copies++;
336 			continue;
337 		}
338 
339 		/*
340 		 * There's a hierachy of errors:
341 		 *	EIO > other errors > ENXIO > 0
342 		 *
343 		 * We preserve any EIOs because those may be worth retrying;
344 		 * whereas ECKSUM and ENXIO are more likely to be persistent.
345 		 *
346 		 * ENXIO should only be reported as an error in a mirror
347 		 * if all children report ENXIO;
348 		 */
349 		if (mc->mc_error) {
350 			if (zio->io_error != EIO) {
351 				if (mc->mc_error != ENXIO)
352 					zio->io_error = mc->mc_error;
353 				else if (zio->io_error == 0)
354 					zio->io_error = ENXIO;
355 			}
356 			if (!mc->mc_skipped)
357 				unexpected_errors++;
358 			zio->io_numerrors++;
359 		}
360 	}
361 
362 	if (zio->io_type == ZIO_TYPE_WRITE) {
363 		/*
364 		 * XXX -- for now, treat partial writes as success.
365 		 * XXX -- For a replacing vdev, we need to make sure the
366 		 *	  new child succeeds.
367 		 */
368 		/* XXPOLICY */
369 		if (good_copies != 0)
370 			zio->io_error = 0;
371 		vdev_mirror_map_free(zio);
372 		return (ZIO_PIPELINE_CONTINUE);
373 	}
374 
375 	ASSERT(zio->io_type == ZIO_TYPE_READ);
376 
377 	/*
378 	 * If we don't have a good copy yet, keep trying other children.
379 	 */
380 	/* XXPOLICY */
381 	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
382 		ASSERT(c >= 0 && c < mm->mm_children);
383 		mc = &mm->mm_child[c];
384 		dprintf("retrying i/o (err=%d) on child %s\n",
385 		    zio->io_error, vdev_description(mc->mc_vd));
386 		zio->io_error = 0;
387 		zio_vdev_io_redone(zio);
388 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
389 		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
390 		    ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL,
391 		    vdev_mirror_child_done, mc));
392 		return (zio_wait_for_children_done(zio));
393 	}
394 
395 	/* XXPOLICY */
396 	if (good_copies)
397 		zio->io_error = 0;
398 	else
399 		ASSERT(zio->io_error != 0);
400 
401 	if (good_copies && (spa_mode & FWRITE) &&
402 	    (unexpected_errors ||
403 	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
404 	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
405 		zio_t *rio;
406 
407 		/*
408 		 * Use the good data we have in hand to repair damaged children.
409 		 *
410 		 * We issue all repair I/Os as children of 'rio' to arrange
411 		 * that vdev_mirror_map_free(zio) will be invoked after all
412 		 * repairs complete, but before we advance to the next stage.
413 		 */
414 		rio = zio_null(zio, zio->io_spa,
415 		    vdev_mirror_repair_done, zio, ZIO_FLAG_CANFAIL);
416 
417 		for (c = 0; c < mm->mm_children; c++) {
418 			/*
419 			 * Don't rewrite known good children.
420 			 * Not only is it unnecessary, it could
421 			 * actually be harmful: if the system lost
422 			 * power while rewriting the only good copy,
423 			 * there would be no good copies left!
424 			 */
425 			mc = &mm->mm_child[c];
426 
427 			if (mc->mc_error == 0) {
428 				if (mc->mc_tried)
429 					continue;
430 				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
431 				    !vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map,
432 				    zio->io_txg, 1))
433 					continue;
434 				mc->mc_error = ESTALE;
435 			}
436 
437 			dprintf("resilvered %s @ 0x%llx error %d\n",
438 			    vdev_description(mc->mc_vd), mc->mc_offset,
439 			    mc->mc_error);
440 
441 			zio_nowait(zio_vdev_child_io(rio, zio->io_bp, mc->mc_vd,
442 			    mc->mc_offset, zio->io_data, zio->io_size,
443 			    ZIO_TYPE_WRITE, zio->io_priority,
444 			    ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
445 			    ZIO_FLAG_DONT_PROPAGATE, NULL, NULL));
446 		}
447 
448 		zio_nowait(rio);
449 
450 		return (zio_wait_for_children_done(zio));
451 	}
452 
453 	vdev_mirror_map_free(zio);
454 
455 	return (ZIO_PIPELINE_CONTINUE);
456 }
457 
458 static void
459 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
460 {
461 	if (faulted == vd->vdev_children)
462 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
463 		    VDEV_AUX_NO_REPLICAS);
464 	else if (degraded + faulted != 0)
465 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
466 	else
467 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
468 }
469 
470 vdev_ops_t vdev_mirror_ops = {
471 	vdev_mirror_open,
472 	vdev_mirror_close,
473 	NULL,
474 	vdev_default_asize,
475 	vdev_mirror_io_start,
476 	vdev_mirror_io_done,
477 	vdev_mirror_state_change,
478 	VDEV_TYPE_MIRROR,	/* name of this vdev type */
479 	B_FALSE			/* not a leaf vdev */
480 };
481 
482 vdev_ops_t vdev_replacing_ops = {
483 	vdev_mirror_open,
484 	vdev_mirror_close,
485 	NULL,
486 	vdev_default_asize,
487 	vdev_mirror_io_start,
488 	vdev_mirror_io_done,
489 	vdev_mirror_state_change,
490 	VDEV_TYPE_REPLACING,	/* name of this vdev type */
491 	B_FALSE			/* not a leaf vdev */
492 };
493 
494 vdev_ops_t vdev_spare_ops = {
495 	vdev_mirror_open,
496 	vdev_mirror_close,
497 	NULL,
498 	vdev_default_asize,
499 	vdev_mirror_io_start,
500 	vdev_mirror_io_done,
501 	vdev_mirror_state_change,
502 	VDEV_TYPE_SPARE,	/* name of this vdev type */
503 	B_FALSE			/* not a leaf vdev */
504 };
505