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