aboutsummaryrefslogtreecommitdiff
path: root/sys/geom/vinum/geom_vinum_init.c
blob: e076ccd48a97812b40d45dc0fa048350aba51ded (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*-
 * Copyright (c) 2004, 2007 Lukas Ertl
 * Copyright (c) 2007, 2009 Ulf Lilleengen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/sys/geom/vinum/geom_vinum_init.c,v 1.14.2.1.6.1 2010/12/21 17:09:25 kensmith Exp $");
#include <sys/param.h>
#include <sys/bio.h>
#include <sys/libkern.h>
#include <sys/malloc.h>

#include <geom/geom.h>
#include <geom/vinum/geom_vinum_var.h>
#include <geom/vinum/geom_vinum.h>

static int		 gv_sync(struct gv_volume *);
static int		 gv_rebuild_plex(struct gv_plex *);
static int		 gv_init_plex(struct gv_plex *);
static int		 gv_grow_plex(struct gv_plex *);
static int		 gv_sync_plex(struct gv_plex *, struct gv_plex *);
static struct gv_plex	*gv_find_good_plex(struct gv_volume *);

void
gv_start_obj(struct g_geom *gp, struct gctl_req *req)
{
	struct gv_softc *sc;
	struct gv_volume *v;
	struct gv_plex *p;
	int *argc, *initsize;
	char *argv, buf[20];
	int i, type;

	argc = gctl_get_paraml(req, "argc", sizeof(*argc));
	initsize = gctl_get_paraml(req, "initsize", sizeof(*initsize));

	if (argc == NULL || *argc == 0) {
		gctl_error(req, "no arguments given");
		return;
	}

	sc = gp->softc;

	for (i = 0; i < *argc; i++) {
		snprintf(buf, sizeof(buf), "argv%d", i);
		argv = gctl_get_param(req, buf, NULL);
		if (argv == NULL)
			continue;
		type = gv_object_type(sc, argv);
		switch (type) {
		case GV_TYPE_VOL:
			v = gv_find_vol(sc, argv);
			if (v != NULL)
				gv_post_event(sc, GV_EVENT_START_VOLUME, v,
				    NULL, *initsize, 0);
			break;

		case GV_TYPE_PLEX:
			p = gv_find_plex(sc, argv);
			if (p != NULL)
				gv_post_event(sc, GV_EVENT_START_PLEX, p, NULL,
				    *initsize, 0);
			break;

		case GV_TYPE_SD:
		case GV_TYPE_DRIVE:
			/* XXX Not implemented, but what is the use? */
			gctl_error(req, "cannot start '%s' - not yet supported",
			    argv);
			return;
		default:
			gctl_error(req, "unknown object '%s'", argv);
			return;
		}
	}
}

int
gv_start_plex(struct gv_plex *p)
{
	struct gv_volume *v;
	struct gv_plex *up;
	struct gv_sd *s;
	int error;

	KASSERT(p != NULL, ("gv_start_plex: NULL p"));

	error = 0;
	v = p->vol_sc;

	/* RAID5 plexes can either be init, rebuilt or grown. */
	if (p->org == GV_PLEX_RAID5) {
		if (p->state > GV_PLEX_DEGRADED) {
			LIST_FOREACH(s, &p->subdisks, in_plex) {
				if (s->flags & GV_SD_GROW) {
					error = gv_grow_plex(p);
					return (error);
				}
			}
		} else if (p->state == GV_PLEX_DEGRADED) {
			error = gv_rebuild_plex(p);
		} else
			error = gv_init_plex(p);
	} else {
		/* We want to sync from the other plex if we're down. */
		if (p->state == GV_PLEX_DOWN && v->plexcount > 1) {
			up = gv_find_good_plex(v);
			if (up == NULL) {
				G_VINUM_DEBUG(1, "unable to find a good plex");
				return (ENXIO);
			}
			g_topology_lock();
			error = gv_access(v->provider, 1, 1, 0);
			if (error) {
				g_topology_unlock();
				G_VINUM_DEBUG(0, "sync from '%s' failed to "
				    "access volume: %d", up->name, error);
				return (error);
			}
			g_topology_unlock();
			error = gv_sync_plex(p, up);
			if (error)
				return (error);
		/*
		 * In case we have a stripe that is up, check whether it can be
		 * grown.
		 */
		} else if (p->org == GV_PLEX_STRIPED &&
		    p->state != GV_PLEX_DOWN) {
			LIST_FOREACH(s, &p->subdisks, in_plex) {
				if (s->flags & GV_SD_GROW) {
					error = gv_grow_plex(p);
					break;
				}
			}
		}
	}
	return (error);
}

int
gv_start_vol(struct gv_volume *v)
{
	struct gv_plex *p;
	int error;

	KASSERT(v != NULL, ("gv_start_vol: NULL v"));

	error = 0;

	if (v->plexcount == 0)
		return (ENXIO);

	else if (v->plexcount == 1) {
		p = LIST_FIRST(&v->plexes);
		KASSERT(p != NULL, ("gv_start_vol: NULL p on %s", v->name));
		error = gv_start_plex(p);
	} else
		error = gv_sync(v);

	return (error);
}

/* Sync a plex p from the plex up.  */
static int
gv_sync_plex(struct gv_plex *p, struct gv_plex *up)
{
	int error;

	KASSERT(p != NULL, ("%s: NULL p", __func__));
	KASSERT(up != NULL, ("%s: NULL up", __func__));
	if ((p == up) || (p->state == GV_PLEX_UP))
		return (0);
	if (p->flags & GV_PLEX_SYNCING ||
	    p->flags & GV_PLEX_REBUILDING ||
	    p->flags & GV_PLEX_GROWING) {
		return (EINPROGRESS);
	}
	p->synced = 0;
	p->flags |= GV_PLEX_SYNCING;
	G_VINUM_DEBUG(1, "starting sync of plex %s", p->name);
	error = gv_sync_request(up, p, p->synced, 
	    MIN(GV_DFLT_SYNCSIZE, up->size - p->synced), 
	    BIO_READ, NULL);
	if (error) {
		G_VINUM_DEBUG(0, "error syncing plex %s", p->name);
		return (error);
	}
	return (0);
}

/* Return a good plex from volume v. */
static struct gv_plex *
gv_find_good_plex(struct gv_volume *v)
{
	struct gv_plex *up;

	/* Find the plex that's up. */
	up = NULL;
	LIST_FOREACH(up, &v->plexes, in_volume) {
		if (up->state == GV_PLEX_UP)
			break;
	}
	/* Didn't find a good plex. */
	return (up);
}

static int
gv_sync(struct gv_volume *v)
{
	struct gv_softc *sc;
	struct gv_plex *p, *up;
	int error;

	KASSERT(v != NULL, ("gv_sync: NULL v"));
	sc = v->vinumconf;
	KASSERT(sc != NULL, ("gv_sync: NULL sc on %s", v->name));


	up = gv_find_good_plex(v);
	if (up == NULL)
		return (ENXIO);
	g_topology_lock();
	error = gv_access(v->provider, 1, 1, 0);
	if (error) {
		g_topology_unlock();
		G_VINUM_DEBUG(0, "sync from '%s' failed to access volume: %d",
		    up->name, error);
		return (error);
	}
	g_topology_unlock();

	/* Go through the good plex, and issue BIO's to all other plexes. */
	LIST_FOREACH(p, &v->plexes, in_volume) {
		error = gv_sync_plex(p, up);
		if (error)
			break;
	}
	return (0);
}

static int
gv_rebuild_plex(struct gv_plex *p)
{
	struct gv_drive *d;
	struct gv_sd *s;
	int error;

	if (p->flags & GV_PLEX_SYNCING ||
	    p->flags & GV_PLEX_REBUILDING ||
	    p->flags & GV_PLEX_GROWING)
		return (EINPROGRESS);
	/*
	 * Make sure that all subdisks have consumers. We won't allow a rebuild
	 * unless every subdisk have one.
	 */
	LIST_FOREACH(s, &p->subdisks, in_plex) {
		d = s->drive_sc;
		if (d == NULL || (d->flags & GV_DRIVE_REFERENCED)) {
			G_VINUM_DEBUG(0, "can't rebuild %s, subdisk(s) have no "
			    "drives", p->name);
			return (ENXIO);
		}
	}
	p->flags |= GV_PLEX_REBUILDING;
	p->synced = 0;

	g_topology_assert_not();
	g_topology_lock();
	error = gv_access(p->vol_sc->provider, 1, 1, 0);
	if (error) {
		G_VINUM_DEBUG(0, "unable to access provider");
		return (0);
	}
	g_topology_unlock();

	gv_parity_request(p, GV_BIO_REBUILD, 0);
	return (0);
}

static int
gv_grow_plex(struct gv_plex *p)
{
	struct gv_volume *v;
	struct gv_sd *s;
	off_t origsize, origlength;
	int error, sdcount;

	KASSERT(p != NULL, ("gv_grow_plex: NULL p"));
	v = p->vol_sc;
	KASSERT(v != NULL, ("gv_grow_plex: NULL v"));

	if (p->flags & GV_PLEX_GROWING || 
	    p->flags & GV_PLEX_SYNCING ||
	    p->flags & GV_PLEX_REBUILDING)
		return (EINPROGRESS);
	g_topology_lock();
	error = gv_access(v->provider, 1, 1, 0);
	g_topology_unlock();
	if (error) {
		G_VINUM_DEBUG(0, "unable to access provider");
		return (error);
	}

	/* XXX: This routine with finding origsize is used two other places as
	 * well, so we should create a function for it. */
	sdcount = p->sdcount;
	LIST_FOREACH(s, &p->subdisks, in_plex) {
		if (s->flags & GV_SD_GROW)
			sdcount--;
	}
	s = LIST_FIRST(&p->subdisks);
	if (s == NULL) {
		G_VINUM_DEBUG(0, "error growing plex without subdisks");
		return (GV_ERR_NOTFOUND);
	}
	p->flags |= GV_PLEX_GROWING;
	origsize = (sdcount - 1) * s->size;
	origlength = (sdcount - 1) * p->stripesize;
	p->synced = 0;
	G_VINUM_DEBUG(1, "starting growing of plex %s", p->name);
	gv_grow_request(p, 0, MIN(origlength, origsize), BIO_READ, NULL);

	return (0);
}

static int
gv_init_plex(struct gv_plex *p)
{
	struct gv_drive *d;
	struct gv_sd *s;
	int error;
	off_t start;
	caddr_t data;

	KASSERT(p != NULL, ("gv_init_plex: NULL p"));

	LIST_FOREACH(s, &p->subdisks, in_plex) {
		if (s->state == GV_SD_INITIALIZING)
			return (EINPROGRESS);
		gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
		s->init_size = GV_DFLT_SYNCSIZE;
		start = s->drive_offset + s->initialized;
		d = s->drive_sc;
		if (d == NULL) {
			G_VINUM_DEBUG(0, "subdisk %s has no drive yet", s->name);
			break;
		}
		/*
		 * Take the lock here since we need to avoid a race in
		 * gv_init_request if the BIO is completed before the lock is
		 * released.
		 */
		g_topology_lock();
		error = g_access(d->consumer, 0, 1, 0);
		g_topology_unlock();
		if (error) {
			G_VINUM_DEBUG(0, "error accessing consumer when "
			    "initializing %s", s->name);
			break;
		}
		data = g_malloc(s->init_size, M_WAITOK | M_ZERO);
		gv_init_request(s, start, data, s->init_size);
	}
	return (0);
}