vdev_mirror.c revision 269407
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) 2013 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/fs/zfs.h>
35
36/*
37 * Virtual device vector for mirroring.
38 */
39
40typedef struct mirror_child {
41	vdev_t		*mc_vd;
42	uint64_t	mc_offset;
43	int		mc_error;
44	int		mc_load;
45	uint8_t		mc_tried;
46	uint8_t		mc_skipped;
47	uint8_t		mc_speculative;
48} mirror_child_t;
49
50typedef struct mirror_map {
51	int		*mm_preferred;
52	int		mm_preferred_cnt;
53	int		mm_children;
54	boolean_t	mm_replacing;
55	boolean_t	mm_root;
56	mirror_child_t	mm_child[];
57} mirror_map_t;
58
59static int vdev_mirror_shift = 21;
60
61SYSCTL_DECL(_vfs_zfs_vdev);
62static SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0,
63    "ZFS VDEV Mirror");
64
65/*
66 * The load configuration settings below are tuned by default for
67 * the case where all devices are of the same rotational type.
68 *
69 * If there is a mixture of rotating and non-rotating media, setting
70 * non_rotating_seek_inc to 0 may well provide better results as it
71 * will direct more reads to the non-rotating vdevs which are more
72 * likely to have a higher performance.
73 */
74
75/* Rotating media load calculation configuration. */
76static int rotating_inc = 0;
77SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN,
78    &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
79
80static int rotating_seek_inc = 5;
81SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN,
82    &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
83
84static int rotating_seek_offset = 1 * 1024 * 1024;
85SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN,
86    &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
87    "triggers a reduced rotating media seek increment");
88
89/* Non-rotating media load calculation configuration. */
90static int non_rotating_inc = 0;
91SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN,
92    &non_rotating_inc, 0,
93    "Non-rotating media load increment for non-seeking I/O's");
94
95static int non_rotating_seek_inc = 1;
96SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN,
97    &non_rotating_seek_inc, 0,
98    "Non-rotating media load increment for seeking I/O's");
99
100
101static inline size_t
102vdev_mirror_map_size(int children)
103{
104	return (offsetof(mirror_map_t, mm_child[children]) +
105	    sizeof(int) * children);
106}
107
108static inline mirror_map_t *
109vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
110{
111	mirror_map_t *mm;
112
113	mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
114	mm->mm_children = children;
115	mm->mm_replacing = replacing;
116	mm->mm_root = root;
117	mm->mm_preferred = (int *)((uintptr_t)mm +
118	    offsetof(mirror_map_t, mm_child[children]));
119
120	return mm;
121}
122
123static void
124vdev_mirror_map_free(zio_t *zio)
125{
126	mirror_map_t *mm = zio->io_vsd;
127
128	kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
129}
130
131static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
132	vdev_mirror_map_free,
133	zio_vsd_default_cksum_report
134};
135
136static int
137vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
138{
139	uint64_t lastoffset;
140	int load;
141
142	/* All DVAs have equal weight at the root. */
143	if (mm->mm_root)
144		return (INT_MAX);
145
146	/*
147	 * We don't return INT_MAX if the device is resilvering i.e.
148	 * vdev_resilver_txg != 0 as when tested performance was slightly
149	 * worse overall when resilvering with compared to without.
150	 */
151
152	/* Standard load based on pending queue length. */
153	load = vdev_queue_length(vd);
154	lastoffset = vdev_queue_lastoffset(vd);
155
156	if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
157		/* Non-rotating media. */
158		if (lastoffset == zio_offset)
159			return (load + non_rotating_inc);
160
161		/*
162		 * Apply a seek penalty even for non-rotating devices as
163		 * sequential I/O'a can be aggregated into fewer operations
164		 * on the device, thus avoiding unnecessary per-command
165		 * overhead and boosting performance.
166		 */
167		return (load + non_rotating_seek_inc);
168	}
169
170	/* Rotating media I/O's which directly follow the last I/O. */
171	if (lastoffset == zio_offset)
172		return (load + rotating_inc);
173
174	/*
175	 * Apply half the seek increment to I/O's within seek offset
176	 * of the last I/O queued to this vdev as they should incure less
177	 * of a seek increment.
178	 */
179	if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
180		return (load + (rotating_seek_inc / 2));
181
182	/* Apply the full seek increment to all other I/O's. */
183	return (load + rotating_seek_inc);
184}
185
186
187static mirror_map_t *
188vdev_mirror_map_init(zio_t *zio)
189{
190	mirror_map_t *mm = NULL;
191	mirror_child_t *mc;
192	vdev_t *vd = zio->io_vd;
193	int c;
194
195	if (vd == NULL) {
196		dva_t *dva = zio->io_bp->blk_dva;
197		spa_t *spa = zio->io_spa;
198
199		mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
200		    B_TRUE);
201		for (c = 0; c < mm->mm_children; c++) {
202			mc = &mm->mm_child[c];
203			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
204			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
205		}
206	} else {
207		mm = vdev_mirror_map_alloc(vd->vdev_children,
208		    (vd->vdev_ops == &vdev_replacing_ops ||
209                    vd->vdev_ops == &vdev_spare_ops), B_FALSE);
210		for (c = 0; c < mm->mm_children; c++) {
211			mc = &mm->mm_child[c];
212			mc->mc_vd = vd->vdev_child[c];
213			mc->mc_offset = zio->io_offset;
214		}
215	}
216
217	zio->io_vsd = mm;
218	zio->io_vsd_ops = &vdev_mirror_vsd_ops;
219	return (mm);
220}
221
222static int
223vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
224    uint64_t *logical_ashift, uint64_t *physical_ashift)
225{
226	int numerrors = 0;
227	int lasterror = 0;
228
229	if (vd->vdev_children == 0) {
230		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
231		return (SET_ERROR(EINVAL));
232	}
233
234	vdev_open_children(vd);
235
236	for (int c = 0; c < vd->vdev_children; c++) {
237		vdev_t *cvd = vd->vdev_child[c];
238
239		if (cvd->vdev_open_error) {
240			lasterror = cvd->vdev_open_error;
241			numerrors++;
242			continue;
243		}
244
245		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
246		*max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
247		*logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
248		*physical_ashift = MAX(*physical_ashift,
249		    cvd->vdev_physical_ashift);
250	}
251
252	if (numerrors == vd->vdev_children) {
253		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
254		return (lasterror);
255	}
256
257	return (0);
258}
259
260static void
261vdev_mirror_close(vdev_t *vd)
262{
263	for (int c = 0; c < vd->vdev_children; c++)
264		vdev_close(vd->vdev_child[c]);
265}
266
267static void
268vdev_mirror_child_done(zio_t *zio)
269{
270	mirror_child_t *mc = zio->io_private;
271
272	mc->mc_error = zio->io_error;
273	mc->mc_tried = 1;
274	mc->mc_skipped = 0;
275}
276
277static void
278vdev_mirror_scrub_done(zio_t *zio)
279{
280	mirror_child_t *mc = zio->io_private;
281
282	if (zio->io_error == 0) {
283		zio_t *pio;
284
285		mutex_enter(&zio->io_lock);
286		while ((pio = zio_walk_parents(zio)) != NULL) {
287			mutex_enter(&pio->io_lock);
288			ASSERT3U(zio->io_size, >=, pio->io_size);
289			bcopy(zio->io_data, pio->io_data, pio->io_size);
290			mutex_exit(&pio->io_lock);
291		}
292		mutex_exit(&zio->io_lock);
293	}
294
295	zio_buf_free(zio->io_data, zio->io_size);
296
297	mc->mc_error = zio->io_error;
298	mc->mc_tried = 1;
299	mc->mc_skipped = 0;
300}
301
302/*
303 * Check the other, lower-index DVAs to see if they're on the same
304 * vdev as the child we picked.  If they are, use them since they
305 * are likely to have been allocated from the primary metaslab in
306 * use at the time, and hence are more likely to have locality with
307 * single-copy data.
308 */
309static int
310vdev_mirror_dva_select(zio_t *zio, int p)
311{
312	dva_t *dva = zio->io_bp->blk_dva;
313	mirror_map_t *mm = zio->io_vsd;
314	int preferred;
315	int c;
316
317	preferred = mm->mm_preferred[p];
318	for (p-- ; p >= 0; p--) {
319		c = mm->mm_preferred[p];
320		if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
321			preferred = c;
322	}
323	return (preferred);
324}
325
326static int
327vdev_mirror_preferred_child_randomize(zio_t *zio)
328{
329	mirror_map_t *mm = zio->io_vsd;
330	int p;
331
332	if (mm->mm_root) {
333		p = spa_get_random(mm->mm_preferred_cnt);
334		return (vdev_mirror_dva_select(zio, p));
335	}
336
337	/*
338	 * To ensure we don't always favour the first matching vdev,
339	 * which could lead to wear leveling issues on SSD's, we
340	 * use the I/O offset as a pseudo random seed into the vdevs
341	 * which have the lowest load.
342	 */
343	p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
344	return (mm->mm_preferred[p]);
345}
346
347/*
348 * Try to find a vdev whose DTL doesn't contain the block we want to read
349 * prefering vdevs based on determined load.
350 *
351 * If we can't, try the read on any vdev we haven't already tried.
352 */
353static int
354vdev_mirror_child_select(zio_t *zio)
355{
356	mirror_map_t *mm = zio->io_vsd;
357	uint64_t txg = zio->io_txg;
358	int c, lowest_load;
359
360	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
361
362	lowest_load = INT_MAX;
363	mm->mm_preferred_cnt = 0;
364	for (c = 0; c < mm->mm_children; c++) {
365		mirror_child_t *mc;
366
367		mc = &mm->mm_child[c];
368		if (mc->mc_tried || mc->mc_skipped)
369			continue;
370
371		if (!vdev_readable(mc->mc_vd)) {
372			mc->mc_error = SET_ERROR(ENXIO);
373			mc->mc_tried = 1;	/* don't even try */
374			mc->mc_skipped = 1;
375			continue;
376		}
377
378		if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
379			mc->mc_error = SET_ERROR(ESTALE);
380			mc->mc_skipped = 1;
381			mc->mc_speculative = 1;
382			continue;
383		}
384
385		mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
386		if (mc->mc_load > lowest_load)
387			continue;
388
389		if (mc->mc_load < lowest_load) {
390			lowest_load = mc->mc_load;
391			mm->mm_preferred_cnt = 0;
392		}
393		mm->mm_preferred[mm->mm_preferred_cnt] = c;
394		mm->mm_preferred_cnt++;
395	}
396
397	if (mm->mm_preferred_cnt == 1) {
398		vdev_queue_register_lastoffset(
399		    mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
400		return (mm->mm_preferred[0]);
401	}
402
403	if (mm->mm_preferred_cnt > 1) {
404		int c = vdev_mirror_preferred_child_randomize(zio);
405
406		vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
407		return (c);
408	}
409
410	/*
411	 * Every device is either missing or has this txg in its DTL.
412	 * Look for any child we haven't already tried before giving up.
413	 */
414	for (c = 0; c < mm->mm_children; c++) {
415		if (!mm->mm_child[c].mc_tried) {
416			vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
417			    zio);
418			return (c);
419		}
420	}
421
422	/*
423	 * Every child failed.  There's no place left to look.
424	 */
425	return (-1);
426}
427
428static int
429vdev_mirror_io_start(zio_t *zio)
430{
431	mirror_map_t *mm;
432	mirror_child_t *mc;
433	int c, children;
434
435	mm = vdev_mirror_map_init(zio);
436
437	if (zio->io_type == ZIO_TYPE_READ) {
438		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
439			/*
440			 * For scrubbing reads we need to allocate a read
441			 * buffer for each child and issue reads to all
442			 * children.  If any child succeeds, it will copy its
443			 * data into zio->io_data in vdev_mirror_scrub_done.
444			 */
445			for (c = 0; c < mm->mm_children; c++) {
446				mc = &mm->mm_child[c];
447				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
448				    mc->mc_vd, mc->mc_offset,
449				    zio_buf_alloc(zio->io_size), zio->io_size,
450				    zio->io_type, zio->io_priority, 0,
451				    vdev_mirror_scrub_done, mc));
452			}
453			zio_interrupt(zio);
454			return (ZIO_PIPELINE_STOP);
455		}
456		/*
457		 * For normal reads just pick one child.
458		 */
459		c = vdev_mirror_child_select(zio);
460		children = (c >= 0);
461	} else {
462		ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
463		    zio->io_type == ZIO_TYPE_FREE);
464
465		/*
466		 * Writes and frees go to all children.
467		 */
468		c = 0;
469		children = mm->mm_children;
470	}
471
472	while (children--) {
473		mc = &mm->mm_child[c];
474		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
475		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
476		    zio->io_type, zio->io_priority, 0,
477		    vdev_mirror_child_done, mc));
478		c++;
479	}
480
481	zio_interrupt(zio);
482	return (ZIO_PIPELINE_STOP);
483}
484
485static int
486vdev_mirror_worst_error(mirror_map_t *mm)
487{
488	int error[2] = { 0, 0 };
489
490	for (int c = 0; c < mm->mm_children; c++) {
491		mirror_child_t *mc = &mm->mm_child[c];
492		int s = mc->mc_speculative;
493		error[s] = zio_worst_error(error[s], mc->mc_error);
494	}
495
496	return (error[0] ? error[0] : error[1]);
497}
498
499static void
500vdev_mirror_io_done(zio_t *zio)
501{
502	mirror_map_t *mm = zio->io_vsd;
503	mirror_child_t *mc;
504	int c;
505	int good_copies = 0;
506	int unexpected_errors = 0;
507
508	for (c = 0; c < mm->mm_children; c++) {
509		mc = &mm->mm_child[c];
510
511		if (mc->mc_error) {
512			if (!mc->mc_skipped)
513				unexpected_errors++;
514		} else if (mc->mc_tried) {
515			good_copies++;
516		}
517	}
518
519	if (zio->io_type == ZIO_TYPE_WRITE) {
520		/*
521		 * XXX -- for now, treat partial writes as success.
522		 *
523		 * Now that we support write reallocation, it would be better
524		 * to treat partial failure as real failure unless there are
525		 * no non-degraded top-level vdevs left, and not update DTLs
526		 * if we intend to reallocate.
527		 */
528		/* XXPOLICY */
529		if (good_copies != mm->mm_children) {
530			/*
531			 * Always require at least one good copy.
532			 *
533			 * For ditto blocks (io_vd == NULL), require
534			 * all copies to be good.
535			 *
536			 * XXX -- for replacing vdevs, there's no great answer.
537			 * If the old device is really dead, we may not even
538			 * be able to access it -- so we only want to
539			 * require good writes to the new device.  But if
540			 * the new device turns out to be flaky, we want
541			 * to be able to detach it -- which requires all
542			 * writes to the old device to have succeeded.
543			 */
544			if (good_copies == 0 || zio->io_vd == NULL)
545				zio->io_error = vdev_mirror_worst_error(mm);
546		}
547		return;
548	} else if (zio->io_type == ZIO_TYPE_FREE) {
549		return;
550	}
551
552	ASSERT(zio->io_type == ZIO_TYPE_READ);
553
554	/*
555	 * If we don't have a good copy yet, keep trying other children.
556	 */
557	/* XXPOLICY */
558	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
559		ASSERT(c >= 0 && c < mm->mm_children);
560		mc = &mm->mm_child[c];
561		zio_vdev_io_redone(zio);
562		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
563		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
564		    ZIO_TYPE_READ, zio->io_priority, 0,
565		    vdev_mirror_child_done, mc));
566		return;
567	}
568
569	/* XXPOLICY */
570	if (good_copies == 0) {
571		zio->io_error = vdev_mirror_worst_error(mm);
572		ASSERT(zio->io_error != 0);
573	}
574
575	if (good_copies && spa_writeable(zio->io_spa) &&
576	    (unexpected_errors ||
577	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
578	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
579		/*
580		 * Use the good data we have in hand to repair damaged children.
581		 */
582		for (c = 0; c < mm->mm_children; c++) {
583			/*
584			 * Don't rewrite known good children.
585			 * Not only is it unnecessary, it could
586			 * actually be harmful: if the system lost
587			 * power while rewriting the only good copy,
588			 * there would be no good copies left!
589			 */
590			mc = &mm->mm_child[c];
591
592			if (mc->mc_error == 0) {
593				if (mc->mc_tried)
594					continue;
595				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
596				    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
597				    zio->io_txg, 1))
598					continue;
599				mc->mc_error = SET_ERROR(ESTALE);
600			}
601
602			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
603			    mc->mc_vd, mc->mc_offset,
604			    zio->io_data, zio->io_size,
605			    ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
606			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
607			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
608		}
609	}
610}
611
612static void
613vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
614{
615	if (faulted == vd->vdev_children)
616		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
617		    VDEV_AUX_NO_REPLICAS);
618	else if (degraded + faulted != 0)
619		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
620	else
621		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
622}
623
624vdev_ops_t vdev_mirror_ops = {
625	vdev_mirror_open,
626	vdev_mirror_close,
627	vdev_default_asize,
628	vdev_mirror_io_start,
629	vdev_mirror_io_done,
630	vdev_mirror_state_change,
631	NULL,
632	NULL,
633	VDEV_TYPE_MIRROR,	/* name of this vdev type */
634	B_FALSE			/* not a leaf vdev */
635};
636
637vdev_ops_t vdev_replacing_ops = {
638	vdev_mirror_open,
639	vdev_mirror_close,
640	vdev_default_asize,
641	vdev_mirror_io_start,
642	vdev_mirror_io_done,
643	vdev_mirror_state_change,
644	NULL,
645	NULL,
646	VDEV_TYPE_REPLACING,	/* name of this vdev type */
647	B_FALSE			/* not a leaf vdev */
648};
649
650vdev_ops_t vdev_spare_ops = {
651	vdev_mirror_open,
652	vdev_mirror_close,
653	vdev_default_asize,
654	vdev_mirror_io_start,
655	vdev_mirror_io_done,
656	vdev_mirror_state_change,
657	NULL,
658	NULL,
659	VDEV_TYPE_SPARE,	/* name of this vdev type */
660	B_FALSE			/* not a leaf vdev */
661};
662