xref: /freebsd/sys/geom/raid/tr_concat.c (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <geom/geom.h>
42 #include <geom/geom_dbg.h>
43 #include "geom/raid/g_raid.h"
44 #include "g_raid_tr_if.h"
45 
46 static MALLOC_DEFINE(M_TR_CONCAT, "tr_concat_data", "GEOM_RAID CONCAT data");
47 
48 struct g_raid_tr_concat_object {
49 	struct g_raid_tr_object	 trso_base;
50 	int			 trso_starting;
51 	int			 trso_stopped;
52 };
53 
54 static g_raid_tr_taste_t g_raid_tr_taste_concat;
55 static g_raid_tr_event_t g_raid_tr_event_concat;
56 static g_raid_tr_start_t g_raid_tr_start_concat;
57 static g_raid_tr_stop_t g_raid_tr_stop_concat;
58 static g_raid_tr_iostart_t g_raid_tr_iostart_concat;
59 static g_raid_tr_iodone_t g_raid_tr_iodone_concat;
60 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_concat;
61 static g_raid_tr_free_t g_raid_tr_free_concat;
62 
63 static kobj_method_t g_raid_tr_concat_methods[] = {
64 	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_concat),
65 	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_concat),
66 	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_concat),
67 	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_concat),
68 	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_concat),
69 	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_concat),
70 	KOBJMETHOD(g_raid_tr_kerneldump,	g_raid_tr_kerneldump_concat),
71 	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_concat),
72 	{ 0, 0 }
73 };
74 
75 static struct g_raid_tr_class g_raid_tr_concat_class = {
76 	"CONCAT",
77 	g_raid_tr_concat_methods,
78 	sizeof(struct g_raid_tr_concat_object),
79 	.trc_enable = 1,
80 	.trc_priority = 50,
81 	.trc_accept_unmapped = 1
82 };
83 
84 static int
85 g_raid_tr_taste_concat(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
86 {
87 	struct g_raid_tr_concat_object *trs;
88 
89 	trs = (struct g_raid_tr_concat_object *)tr;
90 	if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_SINGLE &&
91 	    tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
92 	    !(tr->tro_volume->v_disks_count == 1 &&
93 	      tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_UNKNOWN))
94 		return (G_RAID_TR_TASTE_FAIL);
95 	trs->trso_starting = 1;
96 	return (G_RAID_TR_TASTE_SUCCEED);
97 }
98 
99 static int
100 g_raid_tr_update_state_concat(struct g_raid_volume *vol)
101 {
102 	struct g_raid_tr_concat_object *trs;
103 	struct g_raid_softc *sc;
104 	off_t size;
105 	u_int s;
106 	int i, n, f;
107 
108 	sc = vol->v_softc;
109 	trs = (struct g_raid_tr_concat_object *)vol->v_tr;
110 	if (trs->trso_stopped)
111 		s = G_RAID_VOLUME_S_STOPPED;
112 	else if (trs->trso_starting)
113 		s = G_RAID_VOLUME_S_STARTING;
114 	else {
115 		n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
116 		f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
117 		if (n + f == vol->v_disks_count) {
118 			if (f == 0)
119 				s = G_RAID_VOLUME_S_OPTIMAL;
120 			else
121 				s = G_RAID_VOLUME_S_SUBOPTIMAL;
122 		} else
123 			s = G_RAID_VOLUME_S_BROKEN;
124 	}
125 	if (s != vol->v_state) {
126 
127 		/*
128 		 * Some metadata modules may not know CONCAT volume
129 		 * mediasize until all disks connected. Recalculate.
130 		 */
131 		if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT &&
132 		    G_RAID_VOLUME_S_ALIVE(s) &&
133 		    !G_RAID_VOLUME_S_ALIVE(vol->v_state)) {
134 			size = 0;
135 			for (i = 0; i < vol->v_disks_count; i++) {
136 				if (vol->v_subdisks[i].sd_state !=
137 				    G_RAID_SUBDISK_S_NONE)
138 					size += vol->v_subdisks[i].sd_size;
139 			}
140 			vol->v_mediasize = size;
141 		}
142 
143 		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
144 		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
145 		    G_RAID_EVENT_VOLUME);
146 		g_raid_change_volume_state(vol, s);
147 		if (!trs->trso_starting && !trs->trso_stopped)
148 			g_raid_write_metadata(sc, vol, NULL, NULL);
149 	}
150 	return (0);
151 }
152 
153 static int
154 g_raid_tr_event_concat(struct g_raid_tr_object *tr,
155     struct g_raid_subdisk *sd, u_int event)
156 {
157 	struct g_raid_tr_concat_object *trs;
158 	struct g_raid_softc *sc;
159 	struct g_raid_volume *vol;
160 	int state;
161 
162 	trs = (struct g_raid_tr_concat_object *)tr;
163 	vol = tr->tro_volume;
164 	sc = vol->v_softc;
165 
166 	state = sd->sd_state;
167 	if (state != G_RAID_SUBDISK_S_NONE &&
168 	    state != G_RAID_SUBDISK_S_FAILED &&
169 	    state != G_RAID_SUBDISK_S_ACTIVE) {
170 		G_RAID_DEBUG1(1, sc,
171 		    "Promote subdisk %s:%d from %s to ACTIVE.",
172 		    vol->v_name, sd->sd_pos,
173 		    g_raid_subdisk_state2str(sd->sd_state));
174 		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
175 	}
176 	if (state != sd->sd_state &&
177 	    !trs->trso_starting && !trs->trso_stopped)
178 		g_raid_write_metadata(sc, vol, sd, NULL);
179 	g_raid_tr_update_state_concat(vol);
180 	return (0);
181 }
182 
183 static int
184 g_raid_tr_start_concat(struct g_raid_tr_object *tr)
185 {
186 	struct g_raid_tr_concat_object *trs;
187 	struct g_raid_volume *vol;
188 
189 	trs = (struct g_raid_tr_concat_object *)tr;
190 	vol = tr->tro_volume;
191 	trs->trso_starting = 0;
192 	g_raid_tr_update_state_concat(vol);
193 	return (0);
194 }
195 
196 static int
197 g_raid_tr_stop_concat(struct g_raid_tr_object *tr)
198 {
199 	struct g_raid_tr_concat_object *trs;
200 	struct g_raid_volume *vol;
201 
202 	trs = (struct g_raid_tr_concat_object *)tr;
203 	vol = tr->tro_volume;
204 	trs->trso_starting = 0;
205 	trs->trso_stopped = 1;
206 	g_raid_tr_update_state_concat(vol);
207 	return (0);
208 }
209 
210 static void
211 g_raid_tr_iostart_concat(struct g_raid_tr_object *tr, struct bio *bp)
212 {
213 	struct g_raid_volume *vol;
214 	struct g_raid_subdisk *sd;
215 	struct bio_queue_head queue;
216 	struct bio *cbp;
217 	char *addr;
218 	off_t offset, length, remain;
219 	u_int no;
220 
221 	vol = tr->tro_volume;
222 	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
223 	    vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
224 		g_raid_iodone(bp, EIO);
225 		return;
226 	}
227 	if (bp->bio_cmd == BIO_FLUSH || bp->bio_cmd == BIO_SPEEDUP) {
228 		g_raid_tr_flush_common(tr, bp);
229 		return;
230 	}
231 
232 	offset = bp->bio_offset;
233 	remain = bp->bio_length;
234 	if ((bp->bio_flags & BIO_UNMAPPED) != 0)
235 		addr = NULL;
236 	else
237 		addr = bp->bio_data;
238 	no = 0;
239 	while (no < vol->v_disks_count &&
240 	    offset >= vol->v_subdisks[no].sd_size) {
241 		offset -= vol->v_subdisks[no].sd_size;
242 		no++;
243 	}
244 	KASSERT(no < vol->v_disks_count,
245 	    ("Request starts after volume end (%ju)", bp->bio_offset));
246 	bioq_init(&queue);
247 	do {
248 		sd = &vol->v_subdisks[no];
249 		length = MIN(sd->sd_size - offset, remain);
250 		cbp = g_clone_bio(bp);
251 		if (cbp == NULL)
252 			goto failure;
253 		cbp->bio_offset = offset;
254 		cbp->bio_length = length;
255 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
256 		    bp->bio_cmd != BIO_DELETE) {
257 			cbp->bio_ma_offset += (uintptr_t)addr;
258 			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
259 			cbp->bio_ma_offset %= PAGE_SIZE;
260 			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
261 			    cbp->bio_length) / PAGE_SIZE;
262 		} else
263 			cbp->bio_data = addr;
264 		cbp->bio_caller1 = sd;
265 		bioq_insert_tail(&queue, cbp);
266 		remain -= length;
267 		if (bp->bio_cmd != BIO_DELETE)
268 			addr += length;
269 		offset = 0;
270 		no++;
271 		KASSERT(no < vol->v_disks_count || remain == 0,
272 		    ("Request ends after volume end (%ju, %ju)",
273 			bp->bio_offset, bp->bio_length));
274 	} while (remain > 0);
275 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
276 		sd = cbp->bio_caller1;
277 		cbp->bio_caller1 = NULL;
278 		g_raid_subdisk_iostart(sd, cbp);
279 	}
280 	return;
281 failure:
282 	while ((cbp = bioq_takefirst(&queue)) != NULL)
283 		g_destroy_bio(cbp);
284 	if (bp->bio_error == 0)
285 		bp->bio_error = ENOMEM;
286 	g_raid_iodone(bp, bp->bio_error);
287 }
288 
289 static int
290 g_raid_tr_kerneldump_concat(struct g_raid_tr_object *tr,
291     void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
292 {
293 	struct g_raid_volume *vol;
294 	struct g_raid_subdisk *sd;
295 	char *addr;
296 	off_t offset, length, remain;
297 	int error, no;
298 
299 	vol = tr->tro_volume;
300 	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
301 		return (ENXIO);
302 
303 	offset = boffset;
304 	remain = blength;
305 	addr = virtual;
306 	no = 0;
307 	while (no < vol->v_disks_count &&
308 	    offset >= vol->v_subdisks[no].sd_size) {
309 		offset -= vol->v_subdisks[no].sd_size;
310 		no++;
311 	}
312 	KASSERT(no < vol->v_disks_count,
313 	    ("Request starts after volume end (%ju)", boffset));
314 	do {
315 		sd = &vol->v_subdisks[no];
316 		length = MIN(sd->sd_size - offset, remain);
317 		error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
318 		    addr, 0, offset, length);
319 		if (error != 0)
320 			return (error);
321 		remain -= length;
322 		addr += length;
323 		offset = 0;
324 		no++;
325 		KASSERT(no < vol->v_disks_count || remain == 0,
326 		    ("Request ends after volume end (%ju, %zu)",
327 			boffset, blength));
328 	} while (remain > 0);
329 	return (0);
330 }
331 
332 static void
333 g_raid_tr_iodone_concat(struct g_raid_tr_object *tr,
334     struct g_raid_subdisk *sd,struct bio *bp)
335 {
336 	struct bio *pbp;
337 
338 	pbp = bp->bio_parent;
339 	if (pbp->bio_error == 0)
340 		pbp->bio_error = bp->bio_error;
341 	g_destroy_bio(bp);
342 	pbp->bio_inbed++;
343 	if (pbp->bio_children == pbp->bio_inbed) {
344 		pbp->bio_completed = pbp->bio_length;
345 		g_raid_iodone(pbp, pbp->bio_error);
346 	}
347 }
348 
349 static int
350 g_raid_tr_free_concat(struct g_raid_tr_object *tr)
351 {
352 
353 	return (0);
354 }
355 
356 G_RAID_TR_DECLARE(concat, "CONCAT");
357