xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_mirror.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio.h>
33 #include <sys/fs/zfs.h>
34 
35 /*
36  * Virtual device vector for mirroring.
37  */
38 
39 typedef struct mirror_map {
40 	int	mm_error;
41 	short	mm_tried;
42 	short	mm_skipped;
43 } mirror_map_t;
44 
45 static mirror_map_t *
46 vdev_mirror_map_alloc(zio_t *zio)
47 {
48 	zio->io_vsd = kmem_zalloc(zio->io_vd->vdev_children *
49 	    sizeof (mirror_map_t), KM_SLEEP);
50 	return (zio->io_vsd);
51 }
52 
53 static void
54 vdev_mirror_map_free(zio_t *zio)
55 {
56 	kmem_free(zio->io_vsd,
57 	    zio->io_vd->vdev_children * sizeof (mirror_map_t));
58 	zio->io_vsd = NULL;
59 }
60 
61 static int
62 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
63 {
64 	vdev_t *cvd;
65 	uint64_t c;
66 	int numerrors = 0;
67 	int ret, lasterror = 0;
68 
69 	if (vd->vdev_children == 0) {
70 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
71 		return (EINVAL);
72 	}
73 
74 	for (c = 0; c < vd->vdev_children; c++) {
75 		cvd = vd->vdev_child[c];
76 
77 		if ((ret = vdev_open(cvd)) != 0) {
78 			lasterror = ret;
79 			numerrors++;
80 			continue;
81 		}
82 
83 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
84 		*ashift = cvd->vdev_ashift;
85 	}
86 
87 	if (numerrors == vd->vdev_children) {
88 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
89 		return (lasterror);
90 	}
91 
92 	return (0);
93 }
94 
95 static void
96 vdev_mirror_close(vdev_t *vd)
97 {
98 	uint64_t c;
99 
100 	for (c = 0; c < vd->vdev_children; c++)
101 		vdev_close(vd->vdev_child[c]);
102 }
103 
104 static void
105 vdev_mirror_child_done(zio_t *zio)
106 {
107 	mirror_map_t *mm = zio->io_private;
108 
109 	mm->mm_error = zio->io_error;
110 	mm->mm_tried = 1;
111 	mm->mm_skipped = 0;
112 }
113 
114 static void
115 vdev_mirror_scrub_done(zio_t *zio)
116 {
117 	mirror_map_t *mm = zio->io_private;
118 
119 	if (zio->io_error == 0) {
120 		zio_t *pio = zio->io_parent;
121 		mutex_enter(&pio->io_lock);
122 		bcopy(zio->io_data, pio->io_data, pio->io_size);
123 		mutex_exit(&pio->io_lock);
124 	}
125 
126 	zio_buf_free(zio->io_data, zio->io_size);
127 
128 	mm->mm_error = zio->io_error;
129 	mm->mm_tried = 1;
130 	mm->mm_skipped = 0;
131 }
132 
133 /*
134  * Try to find a child whose DTL doesn't contain the block we want to read.
135  * If we can't, try the read on any vdev we haven't already tried.
136  */
137 static int
138 vdev_mirror_child_select(zio_t *zio)
139 {
140 	mirror_map_t *mm = zio->io_vsd;
141 	vdev_t *vd = zio->io_vd;
142 	vdev_t *cvd;
143 	uint64_t txg = zio->io_txg;
144 	int i, c;
145 
146 	ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg);
147 
148 	/*
149 	 * Select the child we'd like to read from absent any errors.
150 	 * The current policy is to alternate sides at 8M granularity.
151 	 * XXX -- investigate other policies for read distribution.
152 	 */
153 	c = (zio->io_offset >> (SPA_MAXBLOCKSHIFT + 6)) % vd->vdev_children;
154 
155 	/*
156 	 * If this is a replacing vdev, always try child 0 (the source) first.
157 	 */
158 	if (vd->vdev_ops == &vdev_replacing_ops)
159 		c = 0;
160 
161 	/*
162 	 * Try to find a child whose DTL doesn't contain the block to read.
163 	 * If a child is known to be completely inaccessible (indicated by
164 	 * vdev_is_dead() returning B_TRUE), don't even try.
165 	 */
166 	for (i = 0; i < vd->vdev_children; i++, c++) {
167 		if (c >= vd->vdev_children)
168 			c = 0;
169 		if (mm[c].mm_tried || mm[c].mm_skipped)
170 			continue;
171 		cvd = vd->vdev_child[c];
172 		if (vdev_is_dead(cvd)) {
173 			mm[c].mm_error = ENXIO;
174 			mm[c].mm_tried = 1;	/* don't even try */
175 			mm[c].mm_skipped = 1;
176 			continue;
177 		}
178 		if (!vdev_dtl_contains(&cvd->vdev_dtl_map, txg, 1))
179 			return (c);
180 		mm[c].mm_error = ESTALE;
181 		mm[c].mm_skipped = 1;
182 	}
183 
184 	/*
185 	 * Every device is either missing or has this txg in its DTL.
186 	 * If we don't have any sibling replicas to consult, look for
187 	 * any child we haven't already tried before giving up.
188 	 */
189 	if (vd == vd->vdev_top || vd->vdev_parent->vdev_children <= 1) {
190 		for (c = 0; c < vd->vdev_children; c++) {
191 			if (!mm[c].mm_tried)
192 				return (c);
193 		}
194 	}
195 
196 	/*
197 	 * Every child failed.  There's no place left to look.
198 	 */
199 	return (-1);
200 }
201 
202 static void
203 vdev_mirror_io_start(zio_t *zio)
204 {
205 	vdev_t *vd = zio->io_vd;
206 	mirror_map_t *mm;
207 	int c, children;
208 
209 	mm = vdev_mirror_map_alloc(zio);
210 
211 	if (zio->io_type == ZIO_TYPE_READ) {
212 		if (zio->io_flags & ZIO_FLAG_SCRUB) {
213 			/*
214 			 * For scrubbing reads we need to allocate a read
215 			 * buffer for each child and issue reads to all
216 			 * children.  If any child succeeds, it will copy its
217 			 * data into zio->io_data in vdev_mirror_scrub_done.
218 			 */
219 			for (c = 0; c < vd->vdev_children; c++) {
220 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
221 				    vd->vdev_child[c], zio->io_offset,
222 				    zio_buf_alloc(zio->io_size), zio->io_size,
223 				    zio->io_type, zio->io_priority,
224 				    ZIO_FLAG_CANFAIL, vdev_mirror_scrub_done,
225 				    &mm[c]));
226 			}
227 			zio_wait_children_done(zio);
228 			return;
229 		}
230 		/*
231 		 * For normal reads just pick one child.
232 		 */
233 		c = vdev_mirror_child_select(zio);
234 		children = (c >= 0);
235 	} else {
236 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
237 
238 		/*
239 		 * If this is a resilvering I/O to a replacing vdev,
240 		 * only the last child should be written -- unless the
241 		 * first child happens to have a DTL entry here as well.
242 		 * All other writes go to all children.
243 		 */
244 		if ((zio->io_flags & ZIO_FLAG_RESILVER) &&
245 		    vd->vdev_ops == &vdev_replacing_ops &&
246 		    !vdev_dtl_contains(&vd->vdev_child[0]->vdev_dtl_map,
247 		    zio->io_txg, 1)) {
248 			c = vd->vdev_children - 1;
249 			children = 1;
250 		} else {
251 			c = 0;
252 			children = vd->vdev_children;
253 		}
254 	}
255 
256 	while (children--) {
257 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
258 		    vd->vdev_child[c], zio->io_offset, zio->io_data,
259 		    zio->io_size, zio->io_type, zio->io_priority,
260 		    ZIO_FLAG_CANFAIL, vdev_mirror_child_done, &mm[c]));
261 		c++;
262 	}
263 
264 	zio_wait_children_done(zio);
265 }
266 
267 static void
268 vdev_mirror_io_done(zio_t *zio)
269 {
270 	vdev_t *vd = zio->io_vd;
271 	vdev_t *cvd;
272 	mirror_map_t *mm = zio->io_vsd;
273 	int c;
274 	int good_copies = 0;
275 	int unexpected_errors = 0;
276 
277 	ASSERT(mm != NULL);
278 
279 	zio->io_error = 0;
280 	zio->io_numerrors = 0;
281 
282 	for (c = 0; c < vd->vdev_children; c++) {
283 		if (mm[c].mm_tried && mm[c].mm_error == 0) {
284 			good_copies++;
285 			continue;
286 		}
287 
288 		/*
289 		 * We preserve any EIOs because those may be worth retrying;
290 		 * whereas ECKSUM and ENXIO are more likely to be persistent.
291 		 */
292 		if (mm[c].mm_error) {
293 			if (zio->io_error != EIO)
294 				zio->io_error = mm[c].mm_error;
295 			if (!mm[c].mm_skipped)
296 				unexpected_errors++;
297 			zio->io_numerrors++;
298 		}
299 	}
300 
301 	if (zio->io_type == ZIO_TYPE_WRITE) {
302 		/*
303 		 * XXX -- for now, treat partial writes as success.
304 		 */
305 		/* XXPOLICY */
306 		if (good_copies != 0)
307 			zio->io_error = 0;
308 		ASSERT(mm != NULL);
309 		vdev_mirror_map_free(zio);
310 		zio_next_stage(zio);
311 		return;
312 	}
313 
314 	ASSERT(zio->io_type == ZIO_TYPE_READ);
315 
316 	/*
317 	 * If we don't have a good copy yet, keep trying other children.
318 	 */
319 	/* XXPOLICY */
320 	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
321 		ASSERT(c >= 0 && c < vd->vdev_children);
322 		cvd = vd->vdev_child[c];
323 		dprintf("%s: retrying i/o (err=%d) on child %s\n",
324 		    vdev_description(zio->io_vd), zio->io_error,
325 		    vdev_description(cvd));
326 		zio->io_error = 0;
327 		zio_vdev_io_redone(zio);
328 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp, cvd,
329 		    zio->io_offset, zio->io_data, zio->io_size,
330 		    ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL,
331 		    vdev_mirror_child_done, &mm[c]));
332 		zio_wait_children_done(zio);
333 		return;
334 	}
335 
336 	/* XXPOLICY */
337 	if (good_copies)
338 		zio->io_error = 0;
339 	else
340 		ASSERT(zio->io_error != 0);
341 
342 	if (good_copies && (spa_mode & FWRITE) &&
343 	    (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) {
344 		/*
345 		 * Use the good data we have in hand to repair damaged children.
346 		 */
347 		for (c = 0; c < vd->vdev_children; c++) {
348 			/*
349 			 * Don't rewrite known good children.
350 			 * Not only is it unnecessary, it could
351 			 * actually be harmful: if the system lost
352 			 * power while rewriting the only good copy,
353 			 * there would be no good copies left!
354 			 */
355 			cvd = vd->vdev_child[c];
356 
357 			if (mm[c].mm_error == 0) {
358 				if (mm[c].mm_tried)
359 					continue;
360 				if (!vdev_dtl_contains(&cvd->vdev_dtl_map,
361 				    zio->io_txg, 1))
362 					continue;
363 				mm[c].mm_error = ESTALE;
364 			}
365 
366 			dprintf("%s resilvered %s @ 0x%llx error %d\n",
367 			    vdev_description(vd),
368 			    vdev_description(cvd),
369 			    zio->io_offset, mm[c].mm_error);
370 
371 			zio_nowait(zio_vdev_child_io(zio, zio->io_bp, cvd,
372 			    zio->io_offset, zio->io_data, zio->io_size,
373 			    ZIO_TYPE_WRITE, zio->io_priority,
374 			    ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
375 			    ZIO_FLAG_DONT_PROPAGATE, NULL, NULL));
376 		}
377 	}
378 
379 	vdev_mirror_map_free(zio);
380 	zio_next_stage(zio);
381 }
382 
383 static void
384 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
385 {
386 	if (faulted == vd->vdev_children)
387 		vdev_set_state(vd, VDEV_STATE_CANT_OPEN, VDEV_AUX_NO_REPLICAS);
388 	else if (degraded + faulted != 0)
389 		vdev_set_state(vd, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
390 	else
391 		vdev_set_state(vd, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
392 }
393 
394 vdev_ops_t vdev_mirror_ops = {
395 	vdev_mirror_open,
396 	vdev_mirror_close,
397 	vdev_default_asize,
398 	vdev_mirror_io_start,
399 	vdev_mirror_io_done,
400 	vdev_mirror_state_change,
401 	VDEV_TYPE_MIRROR,	/* name of this vdev type */
402 	B_FALSE			/* not a leaf vdev */
403 };
404 
405 vdev_ops_t vdev_replacing_ops = {
406 	vdev_mirror_open,
407 	vdev_mirror_close,
408 	vdev_default_asize,
409 	vdev_mirror_io_start,
410 	vdev_mirror_io_done,
411 	vdev_mirror_state_change,
412 	VDEV_TYPE_REPLACING,	/* name of this vdev type */
413 	B_FALSE			/* not a leaf vdev */
414 };
415