tr_concat.c revision 260385
1178476Sjb/*-
2178476Sjb * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3178476Sjb * All rights reserved.
4178476Sjb *
5178476Sjb * Redistribution and use in source and binary forms, with or without
6178476Sjb * modification, are permitted provided that the following conditions
7178476Sjb * are met:
8178476Sjb * 1. Redistributions of source code must retain the above copyright
9178476Sjb *    notice, this list of conditions and the following disclaimer.
10178476Sjb * 2. Redistributions in binary form must reproduce the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer in the
12178476Sjb *    documentation and/or other materials provided with the distribution.
13178476Sjb *
14178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17178476Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24178476Sjb * SUCH DAMAGE.
25178476Sjb */
26178476Sjb
27178476Sjb#include <sys/cdefs.h>
28178476Sjb__FBSDID("$FreeBSD: stable/10/sys/geom/raid/tr_concat.c 260385 2014-01-07 01:32:23Z scottl $");
29178476Sjb
30178476Sjb#include <sys/param.h>
31178476Sjb#include <sys/bio.h>
32178476Sjb#include <sys/endian.h>
33178476Sjb#include <sys/kernel.h>
34178476Sjb#include <sys/kobj.h>
35178476Sjb#include <sys/lock.h>
36178476Sjb#include <sys/malloc.h>
37178476Sjb#include <sys/mutex.h>
38178476Sjb#include <sys/systm.h>
39178476Sjb#include <geom/geom.h>
40178476Sjb#include "geom/raid/g_raid.h"
41178476Sjb#include "g_raid_tr_if.h"
42178476Sjb
43178476Sjbstatic MALLOC_DEFINE(M_TR_CONCAT, "tr_concat_data", "GEOM_RAID CONCAT data");
44178476Sjb
45178476Sjbstruct g_raid_tr_concat_object {
46178476Sjb	struct g_raid_tr_object	 trso_base;
47178476Sjb	int			 trso_starting;
48178476Sjb	int			 trso_stopped;
49178476Sjb};
50178476Sjb
51178476Sjbstatic g_raid_tr_taste_t g_raid_tr_taste_concat;
52178476Sjbstatic g_raid_tr_event_t g_raid_tr_event_concat;
53178476Sjbstatic g_raid_tr_start_t g_raid_tr_start_concat;
54178476Sjbstatic g_raid_tr_stop_t g_raid_tr_stop_concat;
55178476Sjbstatic g_raid_tr_iostart_t g_raid_tr_iostart_concat;
56178476Sjbstatic g_raid_tr_iodone_t g_raid_tr_iodone_concat;
57178476Sjbstatic g_raid_tr_kerneldump_t g_raid_tr_kerneldump_concat;
58178476Sjbstatic g_raid_tr_free_t g_raid_tr_free_concat;
59178476Sjb
60178476Sjbstatic kobj_method_t g_raid_tr_concat_methods[] = {
61178476Sjb	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_concat),
62178476Sjb	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_concat),
63178476Sjb	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_concat),
64178476Sjb	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_concat),
65178476Sjb	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_concat),
66178476Sjb	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_concat),
67178476Sjb	KOBJMETHOD(g_raid_tr_kerneldump,	g_raid_tr_kerneldump_concat),
68178476Sjb	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_concat),
69178476Sjb	{ 0, 0 }
70178476Sjb};
71178476Sjb
72178476Sjbstatic struct g_raid_tr_class g_raid_tr_concat_class = {
73178476Sjb	"CONCAT",
74178476Sjb	g_raid_tr_concat_methods,
75178476Sjb	sizeof(struct g_raid_tr_concat_object),
76178476Sjb	.trc_enable = 1,
77178476Sjb	.trc_priority = 50,
78178476Sjb	.trc_accept_unmapped = 1
79178476Sjb};
80178476Sjb
81178476Sjbstatic int
82178476Sjbg_raid_tr_taste_concat(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
83178476Sjb{
84178476Sjb	struct g_raid_tr_concat_object *trs;
85178476Sjb
86178476Sjb	trs = (struct g_raid_tr_concat_object *)tr;
87178476Sjb	if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_SINGLE &&
88178476Sjb	    tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
89178476Sjb	    !(tr->tro_volume->v_disks_count == 1 &&
90178476Sjb	      tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_UNKNOWN))
91178476Sjb		return (G_RAID_TR_TASTE_FAIL);
92178476Sjb	trs->trso_starting = 1;
93178476Sjb	return (G_RAID_TR_TASTE_SUCCEED);
94178476Sjb}
95178476Sjb
96178476Sjbstatic int
97178476Sjbg_raid_tr_update_state_concat(struct g_raid_volume *vol)
98178476Sjb{
99178476Sjb	struct g_raid_tr_concat_object *trs;
100178476Sjb	struct g_raid_softc *sc;
101178476Sjb	off_t size;
102178476Sjb	u_int s;
103	int i, n, f;
104
105	sc = vol->v_softc;
106	trs = (struct g_raid_tr_concat_object *)vol->v_tr;
107	if (trs->trso_stopped)
108		s = G_RAID_VOLUME_S_STOPPED;
109	else if (trs->trso_starting)
110		s = G_RAID_VOLUME_S_STARTING;
111	else {
112		n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
113		f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
114		if (n + f == vol->v_disks_count) {
115			if (f == 0)
116				s = G_RAID_VOLUME_S_OPTIMAL;
117			else
118				s = G_RAID_VOLUME_S_SUBOPTIMAL;
119		} else
120			s = G_RAID_VOLUME_S_BROKEN;
121	}
122	if (s != vol->v_state) {
123
124		/*
125		 * Some metadata modules may not know CONCAT volume
126		 * mediasize until all disks connected. Recalculate.
127		 */
128		if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT &&
129		    G_RAID_VOLUME_S_ALIVE(s) &&
130		    !G_RAID_VOLUME_S_ALIVE(vol->v_state)) {
131			size = 0;
132			for (i = 0; i < vol->v_disks_count; i++) {
133				if (vol->v_subdisks[i].sd_state !=
134				    G_RAID_SUBDISK_S_NONE)
135					size += vol->v_subdisks[i].sd_size;
136			}
137			vol->v_mediasize = size;
138		}
139
140		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
141		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
142		    G_RAID_EVENT_VOLUME);
143		g_raid_change_volume_state(vol, s);
144		if (!trs->trso_starting && !trs->trso_stopped)
145			g_raid_write_metadata(sc, vol, NULL, NULL);
146	}
147	return (0);
148}
149
150static int
151g_raid_tr_event_concat(struct g_raid_tr_object *tr,
152    struct g_raid_subdisk *sd, u_int event)
153{
154	struct g_raid_tr_concat_object *trs;
155	struct g_raid_softc *sc;
156	struct g_raid_volume *vol;
157	int state;
158
159	trs = (struct g_raid_tr_concat_object *)tr;
160	vol = tr->tro_volume;
161	sc = vol->v_softc;
162
163	state = sd->sd_state;
164	if (state != G_RAID_SUBDISK_S_NONE &&
165	    state != G_RAID_SUBDISK_S_FAILED &&
166	    state != G_RAID_SUBDISK_S_ACTIVE) {
167		G_RAID_DEBUG1(1, sc,
168		    "Promote subdisk %s:%d from %s to ACTIVE.",
169		    vol->v_name, sd->sd_pos,
170		    g_raid_subdisk_state2str(sd->sd_state));
171		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
172	}
173	if (state != sd->sd_state &&
174	    !trs->trso_starting && !trs->trso_stopped)
175		g_raid_write_metadata(sc, vol, sd, NULL);
176	g_raid_tr_update_state_concat(vol);
177	return (0);
178}
179
180static int
181g_raid_tr_start_concat(struct g_raid_tr_object *tr)
182{
183	struct g_raid_tr_concat_object *trs;
184	struct g_raid_volume *vol;
185
186	trs = (struct g_raid_tr_concat_object *)tr;
187	vol = tr->tro_volume;
188	trs->trso_starting = 0;
189	g_raid_tr_update_state_concat(vol);
190	return (0);
191}
192
193static int
194g_raid_tr_stop_concat(struct g_raid_tr_object *tr)
195{
196	struct g_raid_tr_concat_object *trs;
197	struct g_raid_volume *vol;
198
199	trs = (struct g_raid_tr_concat_object *)tr;
200	vol = tr->tro_volume;
201	trs->trso_starting = 0;
202	trs->trso_stopped = 1;
203	g_raid_tr_update_state_concat(vol);
204	return (0);
205}
206
207static void
208g_raid_tr_iostart_concat(struct g_raid_tr_object *tr, struct bio *bp)
209{
210	struct g_raid_volume *vol;
211	struct g_raid_subdisk *sd;
212	struct bio_queue_head queue;
213	struct bio *cbp;
214	char *addr;
215	off_t offset, length, remain;
216	u_int no;
217
218	vol = tr->tro_volume;
219	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
220	    vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
221		g_raid_iodone(bp, EIO);
222		return;
223	}
224	if (bp->bio_cmd == BIO_FLUSH) {
225		g_raid_tr_flush_common(tr, bp);
226		return;
227	}
228
229	offset = bp->bio_offset;
230	remain = bp->bio_length;
231	if ((bp->bio_flags & BIO_UNMAPPED) != 0)
232		addr = NULL;
233	else
234		addr = bp->bio_data;
235	no = 0;
236	while (no < vol->v_disks_count &&
237	    offset >= vol->v_subdisks[no].sd_size) {
238		offset -= vol->v_subdisks[no].sd_size;
239		no++;
240	}
241	KASSERT(no < vol->v_disks_count,
242	    ("Request starts after volume end (%ju)", bp->bio_offset));
243	bioq_init(&queue);
244	do {
245		sd = &vol->v_subdisks[no];
246		length = MIN(sd->sd_size - offset, remain);
247		cbp = g_clone_bio(bp);
248		if (cbp == NULL)
249			goto failure;
250		cbp->bio_offset = offset;
251		cbp->bio_length = length;
252		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
253		    bp->bio_cmd != BIO_DELETE) {
254			cbp->bio_ma_offset += (uintptr_t)addr;
255			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
256			cbp->bio_ma_offset %= PAGE_SIZE;
257			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
258			    cbp->bio_length) / PAGE_SIZE;
259		} else
260			cbp->bio_data = addr;
261		cbp->bio_caller1 = sd;
262		bioq_insert_tail(&queue, cbp);
263		remain -= length;
264		if (bp->bio_cmd != BIO_DELETE)
265			addr += length;
266		offset = 0;
267		no++;
268		KASSERT(no < vol->v_disks_count || remain == 0,
269		    ("Request ends after volume end (%ju, %ju)",
270			bp->bio_offset, bp->bio_length));
271	} while (remain > 0);
272	while ((cbp = bioq_takefirst(&queue)) != NULL) {
273		sd = cbp->bio_caller1;
274		cbp->bio_caller1 = NULL;
275		g_raid_subdisk_iostart(sd, cbp);
276	}
277	return;
278failure:
279	while ((cbp = bioq_takefirst(&queue)) != NULL)
280		g_destroy_bio(cbp);
281	if (bp->bio_error == 0)
282		bp->bio_error = ENOMEM;
283	g_raid_iodone(bp, bp->bio_error);
284}
285
286static int
287g_raid_tr_kerneldump_concat(struct g_raid_tr_object *tr,
288    void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
289{
290	struct g_raid_volume *vol;
291	struct g_raid_subdisk *sd;
292	char *addr;
293	off_t offset, length, remain;
294	int error, no;
295
296	vol = tr->tro_volume;
297	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
298		return (ENXIO);
299
300	offset = boffset;
301	remain = blength;
302	addr = virtual;
303	no = 0;
304	while (no < vol->v_disks_count &&
305	    offset >= vol->v_subdisks[no].sd_size) {
306		offset -= vol->v_subdisks[no].sd_size;
307		no++;
308	}
309	KASSERT(no < vol->v_disks_count,
310	    ("Request starts after volume end (%ju)", boffset));
311	do {
312		sd = &vol->v_subdisks[no];
313		length = MIN(sd->sd_size - offset, remain);
314		error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
315		    addr, 0, offset, length);
316		if (error != 0)
317			return (error);
318		remain -= length;
319		addr += length;
320		offset = 0;
321		no++;
322		KASSERT(no < vol->v_disks_count || remain == 0,
323		    ("Request ends after volume end (%ju, %zu)",
324			boffset, blength));
325	} while (remain > 0);
326	return (0);
327}
328
329static void
330g_raid_tr_iodone_concat(struct g_raid_tr_object *tr,
331    struct g_raid_subdisk *sd,struct bio *bp)
332{
333	struct bio *pbp;
334
335	pbp = bp->bio_parent;
336	if (pbp->bio_error == 0)
337		pbp->bio_error = bp->bio_error;
338	g_destroy_bio(bp);
339	pbp->bio_inbed++;
340	if (pbp->bio_children == pbp->bio_inbed) {
341		pbp->bio_completed = pbp->bio_length;
342		g_raid_iodone(pbp, bp->bio_error);
343	}
344}
345
346static int
347g_raid_tr_free_concat(struct g_raid_tr_object *tr)
348{
349
350	return (0);
351}
352
353G_RAID_TR_DECLARE(concat, "CONCAT");
354