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