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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/zio.h>
32 #include <sys/fs/zfs.h>
33 
34 /*
35  * Virtual device vector for mirroring.
36  */
37 
38 typedef struct mirror_child {
39 	vdev_t		*mc_vd;
40 	uint64_t	mc_offset;
41 	int		mc_error;
42 	short		mc_tried;
43 	short		mc_skipped;
44 } mirror_child_t;
45 
46 typedef struct mirror_map {
47 	int		mm_children;
48 	int		mm_replacing;
49 	int		mm_preferred;
50 	int		mm_root;
51 	mirror_child_t	mm_child[1];
52 } mirror_map_t;
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 : spa_get_random(c);
100 		mm->mm_root = B_FALSE;
101 
102 		for (c = 0; c < mm->mm_children; c++) {
103 			mc = &mm->mm_child[c];
104 			mc->mc_vd = vd->vdev_child[c];
105 			mc->mc_offset = zio->io_offset;
106 		}
107 	}
108 
109 	zio->io_vsd = mm;
110 	return (mm);
111 }
112 
113 static void
114 vdev_mirror_map_free(zio_t *zio)
115 {
116 	mirror_map_t *mm = zio->io_vsd;
117 
118 	kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
119 	zio->io_vsd = NULL;
120 }
121 
122 static int
123 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
124 {
125 	vdev_t *cvd;
126 	uint64_t c;
127 	int numerrors = 0;
128 	int ret, lasterror = 0;
129 
130 	if (vd->vdev_children == 0) {
131 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
132 		return (EINVAL);
133 	}
134 
135 	for (c = 0; c < vd->vdev_children; c++) {
136 		cvd = vd->vdev_child[c];
137 
138 		if ((ret = vdev_open(cvd)) != 0) {
139 			lasterror = ret;
140 			numerrors++;
141 			continue;
142 		}
143 
144 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
145 		*ashift = MAX(*ashift, cvd->vdev_ashift);
146 	}
147 
148 	if (numerrors == vd->vdev_children) {
149 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
150 		return (lasterror);
151 	}
152 
153 	return (0);
154 }
155 
156 static void
157 vdev_mirror_close(vdev_t *vd)
158 {
159 	uint64_t c;
160 
161 	for (c = 0; c < vd->vdev_children; c++)
162 		vdev_close(vd->vdev_child[c]);
163 }
164 
165 static void
166 vdev_mirror_child_done(zio_t *zio)
167 {
168 	mirror_child_t *mc = zio->io_private;
169 
170 	mc->mc_error = zio->io_error;
171 	mc->mc_tried = 1;
172 	mc->mc_skipped = 0;
173 }
174 
175 static void
176 vdev_mirror_scrub_done(zio_t *zio)
177 {
178 	mirror_child_t *mc = zio->io_private;
179 
180 	if (zio->io_error == 0) {
181 		zio_t *pio = zio->io_parent;
182 		mutex_enter(&pio->io_lock);
183 		ASSERT3U(zio->io_size, >=, pio->io_size);
184 		bcopy(zio->io_data, pio->io_data, pio->io_size);
185 		mutex_exit(&pio->io_lock);
186 	}
187 
188 	zio_buf_free(zio->io_data, zio->io_size);
189 
190 	mc->mc_error = zio->io_error;
191 	mc->mc_tried = 1;
192 	mc->mc_skipped = 0;
193 }
194 
195 static void
196 vdev_mirror_repair_done(zio_t *zio)
197 {
198 	ASSERT(zio->io_private == zio->io_parent);
199 	vdev_mirror_map_free(zio->io_private);
200 }
201 
202 /*
203  * Try to find a child whose DTL doesn't contain the block we want to read.
204  * If we can't, try the read on any vdev we haven't already tried.
205  */
206 static int
207 vdev_mirror_child_select(zio_t *zio)
208 {
209 	mirror_map_t *mm = zio->io_vsd;
210 	mirror_child_t *mc;
211 	uint64_t txg = zio->io_txg;
212 	int i, c;
213 
214 	ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg);
215 
216 	/*
217 	 * Try to find a child whose DTL doesn't contain the block to read.
218 	 * If a child is known to be completely inaccessible (indicated by
219 	 * vdev_is_dead() returning B_TRUE), don't even try.
220 	 */
221 	for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
222 		if (c >= mm->mm_children)
223 			c = 0;
224 		mc = &mm->mm_child[c];
225 		if (mc->mc_tried || mc->mc_skipped)
226 			continue;
227 		if (vdev_is_dead(mc->mc_vd)) {
228 			mc->mc_error = ENXIO;
229 			mc->mc_tried = 1;	/* don't even try */
230 			mc->mc_skipped = 1;
231 			continue;
232 		}
233 		if (!vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map, txg, 1))
234 			return (c);
235 		mc->mc_error = ESTALE;
236 		mc->mc_skipped = 1;
237 	}
238 
239 	/*
240 	 * Every device is either missing or has this txg in its DTL.
241 	 * Look for any child we haven't already tried before giving up.
242 	 */
243 	for (c = 0; c < mm->mm_children; c++)
244 		if (!mm->mm_child[c].mc_tried)
245 			return (c);
246 
247 	/*
248 	 * Every child failed.  There's no place left to look.
249 	 */
250 	return (-1);
251 }
252 
253 static void
254 vdev_mirror_io_start(zio_t *zio)
255 {
256 	mirror_map_t *mm;
257 	mirror_child_t *mc;
258 	int c, children;
259 
260 	mm = vdev_mirror_map_alloc(zio);
261 
262 	if (zio->io_type == ZIO_TYPE_READ) {
263 		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
264 			/*
265 			 * For scrubbing reads we need to allocate a read
266 			 * buffer for each child and issue reads to all
267 			 * children.  If any child succeeds, it will copy its
268 			 * data into zio->io_data in vdev_mirror_scrub_done.
269 			 */
270 			for (c = 0; c < mm->mm_children; c++) {
271 				mc = &mm->mm_child[c];
272 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
273 				    mc->mc_vd, mc->mc_offset,
274 				    zio_buf_alloc(zio->io_size), zio->io_size,
275 				    zio->io_type, zio->io_priority,
276 				    ZIO_FLAG_CANFAIL,
277 				    vdev_mirror_scrub_done, mc));
278 			}
279 			zio_wait_children_done(zio);
280 			return;
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 	zio_wait_children_done(zio);
317 }
318 
319 static void
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 		 * We preserve any EIOs because those may be worth retrying;
341 		 * whereas ECKSUM and ENXIO are more likely to be persistent.
342 		 */
343 		if (mc->mc_error) {
344 			if (zio->io_error != EIO)
345 				zio->io_error = mc->mc_error;
346 			if (!mc->mc_skipped)
347 				unexpected_errors++;
348 			zio->io_numerrors++;
349 		}
350 	}
351 
352 	if (zio->io_type == ZIO_TYPE_WRITE) {
353 		/*
354 		 * XXX -- for now, treat partial writes as success.
355 		 * XXX -- For a replacing vdev, we need to make sure the
356 		 *	  new child succeeds.
357 		 */
358 		/* XXPOLICY */
359 		if (good_copies != 0)
360 			zio->io_error = 0;
361 		vdev_mirror_map_free(zio);
362 		zio_next_stage(zio);
363 		return;
364 	}
365 
366 	ASSERT(zio->io_type == ZIO_TYPE_READ);
367 
368 	/*
369 	 * If we don't have a good copy yet, keep trying other children.
370 	 */
371 	/* XXPOLICY */
372 	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
373 		ASSERT(c >= 0 && c < mm->mm_children);
374 		mc = &mm->mm_child[c];
375 		dprintf("retrying i/o (err=%d) on child %s\n",
376 		    zio->io_error, vdev_description(mc->mc_vd));
377 		zio->io_error = 0;
378 		zio_vdev_io_redone(zio);
379 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
380 		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
381 		    ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL,
382 		    vdev_mirror_child_done, mc));
383 		zio_wait_children_done(zio);
384 		return;
385 	}
386 
387 	/* XXPOLICY */
388 	if (good_copies)
389 		zio->io_error = 0;
390 	else
391 		ASSERT(zio->io_error != 0);
392 
393 	if (good_copies && (spa_mode & FWRITE) &&
394 	    (unexpected_errors ||
395 	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
396 	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
397 		zio_t *rio;
398 
399 		/*
400 		 * Use the good data we have in hand to repair damaged children.
401 		 *
402 		 * We issue all repair I/Os as children of 'rio' to arrange
403 		 * that vdev_mirror_map_free(zio) will be invoked after all
404 		 * repairs complete, but before we advance to the next stage.
405 		 */
406 		rio = zio_null(zio, zio->io_spa,
407 		    vdev_mirror_repair_done, zio, ZIO_FLAG_CANFAIL);
408 
409 		for (c = 0; c < mm->mm_children; c++) {
410 			/*
411 			 * Don't rewrite known good children.
412 			 * Not only is it unnecessary, it could
413 			 * actually be harmful: if the system lost
414 			 * power while rewriting the only good copy,
415 			 * there would be no good copies left!
416 			 */
417 			mc = &mm->mm_child[c];
418 
419 			if (mc->mc_error == 0) {
420 				if (mc->mc_tried)
421 					continue;
422 				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
423 				    !vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map,
424 				    zio->io_txg, 1))
425 					continue;
426 				mc->mc_error = ESTALE;
427 			}
428 
429 			dprintf("resilvered %s @ 0x%llx error %d\n",
430 			    vdev_description(mc->mc_vd), mc->mc_offset,
431 			    mc->mc_error);
432 
433 			zio_nowait(zio_vdev_child_io(rio, zio->io_bp, mc->mc_vd,
434 			    mc->mc_offset, zio->io_data, zio->io_size,
435 			    ZIO_TYPE_WRITE, zio->io_priority,
436 			    ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
437 			    ZIO_FLAG_DONT_PROPAGATE, NULL, NULL));
438 		}
439 
440 		zio_nowait(rio);
441 		zio_wait_children_done(zio);
442 		return;
443 	}
444 
445 	vdev_mirror_map_free(zio);
446 	zio_next_stage(zio);
447 }
448 
449 static void
450 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
451 {
452 	if (faulted == vd->vdev_children)
453 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
454 		    VDEV_AUX_NO_REPLICAS);
455 	else if (degraded + faulted != 0)
456 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
457 	else
458 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
459 }
460 
461 vdev_ops_t vdev_mirror_ops = {
462 	vdev_mirror_open,
463 	vdev_mirror_close,
464 	vdev_default_asize,
465 	vdev_mirror_io_start,
466 	vdev_mirror_io_done,
467 	vdev_mirror_state_change,
468 	VDEV_TYPE_MIRROR,	/* name of this vdev type */
469 	B_FALSE			/* not a leaf vdev */
470 };
471 
472 vdev_ops_t vdev_replacing_ops = {
473 	vdev_mirror_open,
474 	vdev_mirror_close,
475 	vdev_default_asize,
476 	vdev_mirror_io_start,
477 	vdev_mirror_io_done,
478 	vdev_mirror_state_change,
479 	VDEV_TYPE_REPLACING,	/* name of this vdev type */
480 	B_FALSE			/* not a leaf vdev */
481 };
482 
483 vdev_ops_t vdev_spare_ops = {
484 	vdev_mirror_open,
485 	vdev_mirror_close,
486 	vdev_default_asize,
487 	vdev_mirror_io_start,
488 	vdev_mirror_io_done,
489 	vdev_mirror_state_change,
490 	VDEV_TYPE_SPARE,	/* name of this vdev type */
491 	B_FALSE			/* not a leaf vdev */
492 };
493