xref: /freebsd/sys/geom/raid/tr_raid5.c (revision 6486b015fc84e96725fef22b0e3363351399ae83)
1 /*-
2  * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <geom/geom.h>
42 #include "geom/raid/g_raid.h"
43 #include "g_raid_tr_if.h"
44 
45 SYSCTL_DECL(_kern_geom_raid);
46 
47 static MALLOC_DEFINE(M_TR_RAID5, "tr_raid5_data", "GEOM_RAID RAID5 data");
48 
49 #define TR_RAID5_NONE 0
50 #define TR_RAID5_REBUILD 1
51 #define TR_RAID5_RESYNC 2
52 
53 #define TR_RAID5_F_DOING_SOME	0x1
54 #define TR_RAID5_F_LOCKED	0x2
55 #define TR_RAID5_F_ABORT	0x4
56 
57 struct g_raid_tr_raid5_object {
58 	struct g_raid_tr_object	 trso_base;
59 	int			 trso_starting;
60 	int			 trso_stopping;
61 	int			 trso_type;
62 	int			 trso_recover_slabs; /* slabs before rest */
63 	int			 trso_fair_io;
64 	int			 trso_meta_update;
65 	int			 trso_flags;
66 	struct g_raid_subdisk	*trso_failed_sd; /* like per volume */
67 	void			*trso_buffer;	 /* Buffer space */
68 	struct bio		 trso_bio;
69 };
70 
71 static g_raid_tr_taste_t g_raid_tr_taste_raid5;
72 static g_raid_tr_event_t g_raid_tr_event_raid5;
73 static g_raid_tr_start_t g_raid_tr_start_raid5;
74 static g_raid_tr_stop_t g_raid_tr_stop_raid5;
75 static g_raid_tr_iostart_t g_raid_tr_iostart_raid5;
76 static g_raid_tr_iodone_t g_raid_tr_iodone_raid5;
77 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid5;
78 static g_raid_tr_locked_t g_raid_tr_locked_raid5;
79 static g_raid_tr_free_t g_raid_tr_free_raid5;
80 
81 static kobj_method_t g_raid_tr_raid5_methods[] = {
82 	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_raid5),
83 	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_raid5),
84 	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_raid5),
85 	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_raid5),
86 	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_raid5),
87 	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_raid5),
88 	KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid5),
89 	KOBJMETHOD(g_raid_tr_locked,	g_raid_tr_locked_raid5),
90 	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_raid5),
91 	{ 0, 0 }
92 };
93 
94 static struct g_raid_tr_class g_raid_tr_raid5_class = {
95 	"RAID5",
96 	g_raid_tr_raid5_methods,
97 	sizeof(struct g_raid_tr_raid5_object),
98 	.trc_priority = 100
99 };
100 
101 static int
102 g_raid_tr_taste_raid5(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
103 {
104 	struct g_raid_tr_raid5_object *trs;
105 	u_int qual;
106 
107 	trs = (struct g_raid_tr_raid5_object *)tr;
108 	qual = tr->tro_volume->v_raid_level_qualifier;
109 	if (tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5 &&
110 	    qual >= 0 && qual <= 3) {
111 		/* RAID5 */
112 	} else
113 		return (G_RAID_TR_TASTE_FAIL);
114 	trs->trso_starting = 1;
115 	return (G_RAID_TR_TASTE_SUCCEED);
116 }
117 
118 static int
119 g_raid_tr_update_state_raid5(struct g_raid_volume *vol,
120     struct g_raid_subdisk *sd)
121 {
122 	struct g_raid_tr_raid5_object *trs;
123 	struct g_raid_softc *sc;
124 	u_int s;
125 	int na, ns, nu;
126 
127 	sc = vol->v_softc;
128 	trs = (struct g_raid_tr_raid5_object *)vol->v_tr;
129 	if (trs->trso_stopping &&
130 	    (trs->trso_flags & TR_RAID5_F_DOING_SOME) == 0)
131 		s = G_RAID_VOLUME_S_STOPPED;
132 	else if (trs->trso_starting)
133 		s = G_RAID_VOLUME_S_STARTING;
134 	else {
135 		na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
136 		ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
137 		     g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
138 		nu = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
139 		if (na == vol->v_disks_count)
140 			s = G_RAID_VOLUME_S_OPTIMAL;
141 		else if (na + ns == vol->v_disks_count ||
142 		    na + ns + nu == vol->v_disks_count /* XXX: Temporary. */)
143 			s = G_RAID_VOLUME_S_SUBOPTIMAL;
144 		else if (na == vol->v_disks_count - 1 ||
145 		    na + ns + nu == vol->v_disks_count)
146 			s = G_RAID_VOLUME_S_DEGRADED;
147 		else
148 			s = G_RAID_VOLUME_S_BROKEN;
149 	}
150 	if (s != vol->v_state) {
151 		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
152 		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
153 		    G_RAID_EVENT_VOLUME);
154 		g_raid_change_volume_state(vol, s);
155 		if (!trs->trso_starting && !trs->trso_stopping)
156 			g_raid_write_metadata(sc, vol, NULL, NULL);
157 	}
158 	return (0);
159 }
160 
161 static int
162 g_raid_tr_event_raid5(struct g_raid_tr_object *tr,
163     struct g_raid_subdisk *sd, u_int event)
164 {
165 
166 	g_raid_tr_update_state_raid5(tr->tro_volume, sd);
167 	return (0);
168 }
169 
170 static int
171 g_raid_tr_start_raid5(struct g_raid_tr_object *tr)
172 {
173 	struct g_raid_tr_raid5_object *trs;
174 	struct g_raid_volume *vol;
175 
176 	trs = (struct g_raid_tr_raid5_object *)tr;
177 	vol = tr->tro_volume;
178 	trs->trso_starting = 0;
179 	g_raid_tr_update_state_raid5(vol, NULL);
180 	return (0);
181 }
182 
183 static int
184 g_raid_tr_stop_raid5(struct g_raid_tr_object *tr)
185 {
186 	struct g_raid_tr_raid5_object *trs;
187 	struct g_raid_volume *vol;
188 
189 	trs = (struct g_raid_tr_raid5_object *)tr;
190 	vol = tr->tro_volume;
191 	trs->trso_starting = 0;
192 	trs->trso_stopping = 1;
193 	g_raid_tr_update_state_raid5(vol, NULL);
194 	return (0);
195 }
196 
197 static void
198 g_raid_tr_iostart_raid5_read(struct g_raid_tr_object *tr, struct bio *bp)
199 {
200 	struct g_raid_volume *vol;
201 	struct g_raid_subdisk *sd;
202 	struct bio_queue_head queue;
203 	struct bio *cbp;
204 	char *addr;
205 	off_t offset, start, length, nstripe, remain;
206 	int no, pno;
207 	u_int strip_size, qual;
208 
209 	vol = tr->tro_volume;
210 	addr = bp->bio_data;
211 	strip_size = vol->v_strip_size;
212 	qual = tr->tro_volume->v_raid_level_qualifier;
213 
214 	/* Stripe number. */
215 	nstripe = bp->bio_offset / strip_size;
216 	/* Start position in stripe. */
217 	start = bp->bio_offset % strip_size;
218 	/* Parity disk number. */
219 	pno = nstripe / (vol->v_disks_count - 1) % vol->v_disks_count;
220 	if (qual >= 2)
221 		pno = (vol->v_disks_count - 1) - pno;
222 	/* Disk number. */
223 	no = nstripe % (vol->v_disks_count - 1);
224 	if (qual & 1) {
225 		no = (pno + no + 1) % vol->v_disks_count;
226 	} else if (no >= pno)
227 		no++;
228 	/* Stripe start position in disk. */
229 	offset = (nstripe / (vol->v_disks_count - 1)) * strip_size;
230 	/* Length of data to operate. */
231 	remain = bp->bio_length;
232 
233 	bioq_init(&queue);
234 	do {
235 		length = MIN(strip_size - start, remain);
236 		cbp = g_clone_bio(bp);
237 		if (cbp == NULL)
238 			goto failure;
239 		cbp->bio_offset = offset + start;
240 		cbp->bio_data = addr;
241 		cbp->bio_length = length;
242 		cbp->bio_caller1 = &vol->v_subdisks[no];
243 		bioq_insert_tail(&queue, cbp);
244 		no++;
245 		if (qual & 1) {
246 			no %= vol->v_disks_count;
247 			if (no == pno) {
248 				if (qual < 2) {
249 					pno = (pno + 1) % vol->v_disks_count;
250 					no = (no + 2) % vol->v_disks_count;
251 				} else if (pno == 0)
252 					pno = vol->v_disks_count - 1;
253 				else
254 					pno--;
255 				offset += strip_size;
256 			}
257 		} else {
258 			if (no == pno)
259 				no++;
260 			if (no >= vol->v_disks_count) {
261 				no %= vol->v_disks_count;
262 				if (qual < 2)
263 					pno = (pno + 1) % vol->v_disks_count;
264 				else if (pno == 0)
265 					pno = vol->v_disks_count - 1;
266 				else
267 					pno--;
268 				offset += strip_size;
269 			}
270 			if (no == pno)
271 				no++;
272 		}
273 		remain -= length;
274 		addr += length;
275 		start = 0;
276 	} while (remain > 0);
277 	for (cbp = bioq_first(&queue); cbp != NULL;
278 	    cbp = bioq_first(&queue)) {
279 		bioq_remove(&queue, cbp);
280 		sd = cbp->bio_caller1;
281 		cbp->bio_caller1 = NULL;
282 		g_raid_subdisk_iostart(sd, cbp);
283 	}
284 	return;
285 failure:
286 	for (cbp = bioq_first(&queue); cbp != NULL;
287 	    cbp = bioq_first(&queue)) {
288 		bioq_remove(&queue, cbp);
289 		g_destroy_bio(cbp);
290 	}
291 	if (bp->bio_error == 0)
292 		bp->bio_error = ENOMEM;
293 	g_raid_iodone(bp, bp->bio_error);
294 }
295 
296 static void
297 g_raid_tr_iostart_raid5(struct g_raid_tr_object *tr, struct bio *bp)
298 {
299 	struct g_raid_volume *vol;
300 	struct g_raid_tr_raid5_object *trs;
301 
302 	vol = tr->tro_volume;
303 	trs = (struct g_raid_tr_raid5_object *)tr;
304 	if (vol->v_state < G_RAID_VOLUME_S_SUBOPTIMAL) {
305 		g_raid_iodone(bp, EIO);
306 		return;
307 	}
308 	switch (bp->bio_cmd) {
309 	case BIO_READ:
310 		g_raid_tr_iostart_raid5_read(tr, bp);
311 		break;
312 	case BIO_WRITE:
313 	case BIO_DELETE:
314 	case BIO_FLUSH:
315 		g_raid_iodone(bp, ENODEV);
316 		break;
317 	default:
318 		KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
319 		    bp->bio_cmd, vol->v_name));
320 		break;
321 	}
322 }
323 
324 static void
325 g_raid_tr_iodone_raid5(struct g_raid_tr_object *tr,
326     struct g_raid_subdisk *sd, struct bio *bp)
327 {
328 	struct bio *pbp;
329 	int error;
330 
331 	pbp = bp->bio_parent;
332 	pbp->bio_inbed++;
333 	error = bp->bio_error;
334 	g_destroy_bio(bp);
335 	if (pbp->bio_children == pbp->bio_inbed) {
336 		pbp->bio_completed = pbp->bio_length;
337 		g_raid_iodone(pbp, error);
338 	}
339 }
340 
341 static int
342 g_raid_tr_kerneldump_raid5(struct g_raid_tr_object *tr,
343     void *virtual, vm_offset_t physical, off_t offset, size_t length)
344 {
345 
346 	return (ENODEV);
347 }
348 
349 static int
350 g_raid_tr_locked_raid5(struct g_raid_tr_object *tr, void *argp)
351 {
352 	struct bio *bp;
353 	struct g_raid_subdisk *sd;
354 
355 	bp = (struct bio *)argp;
356 	sd = (struct g_raid_subdisk *)bp->bio_caller1;
357 	g_raid_subdisk_iostart(sd, bp);
358 
359 	return (0);
360 }
361 
362 static int
363 g_raid_tr_free_raid5(struct g_raid_tr_object *tr)
364 {
365 	struct g_raid_tr_raid5_object *trs;
366 
367 	trs = (struct g_raid_tr_raid5_object *)tr;
368 
369 	if (trs->trso_buffer != NULL) {
370 		free(trs->trso_buffer, M_TR_RAID5);
371 		trs->trso_buffer = NULL;
372 	}
373 	return (0);
374 }
375 
376 G_RAID_TR_DECLARE(g_raid_tr_raid5);
377