2 * Copyright (c) 2004, 2007 Lukas Ertl
3 * Copyright (c) 2007, 2009 Ulf Lilleengen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/libkern.h>
33 #include <sys/malloc.h>
35 #include <geom/geom.h>
36 #include <geom/vinum/geom_vinum_var.h>
37 #include <geom/vinum/geom_vinum.h>
39 static int gv_sync(struct gv_volume *);
40 static int gv_rebuild_plex(struct gv_plex *);
41 static int gv_init_plex(struct gv_plex *);
42 static int gv_grow_plex(struct gv_plex *);
43 static int gv_sync_plex(struct gv_plex *, struct gv_plex *);
44 static struct gv_plex *gv_find_good_plex(struct gv_volume *);
47 gv_start_obj(struct g_geom *gp, struct gctl_req *req)
56 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
57 initsize = gctl_get_paraml(req, "initsize", sizeof(*initsize));
59 if (argc == NULL || *argc == 0) {
60 gctl_error(req, "no arguments given");
66 for (i = 0; i < *argc; i++) {
67 snprintf(buf, sizeof(buf), "argv%d", i);
68 argv = gctl_get_param(req, buf, NULL);
71 type = gv_object_type(sc, argv);
74 v = gv_find_vol(sc, argv);
76 gv_post_event(sc, GV_EVENT_START_VOLUME, v,
81 p = gv_find_plex(sc, argv);
83 gv_post_event(sc, GV_EVENT_START_PLEX, p, NULL,
89 /* XXX Not implemented, but what is the use? */
90 gctl_error(req, "unable to start '%s' - not yet supported",
94 gctl_error(req, "unknown object '%s'", argv);
101 gv_start_plex(struct gv_plex *p)
108 KASSERT(p != NULL, ("gv_start_plex: NULL p"));
113 /* RAID5 plexes can either be init, rebuilt or grown. */
114 if (p->org == GV_PLEX_RAID5) {
115 if (p->state > GV_PLEX_DEGRADED) {
116 LIST_FOREACH(s, &p->subdisks, in_plex) {
117 if (s->flags & GV_SD_GROW) {
118 error = gv_grow_plex(p);
122 } else if (p->state == GV_PLEX_DEGRADED) {
123 error = gv_rebuild_plex(p);
125 error = gv_init_plex(p);
127 /* We want to sync from the other plex if we're down. */
128 if (p->state == GV_PLEX_DOWN && v->plexcount > 1) {
129 up = gv_find_good_plex(v);
131 G_VINUM_DEBUG(1, "unable to find a good plex");
135 error = gv_access(v->provider, 1, 1, 0);
138 G_VINUM_DEBUG(0, "sync from '%s' failed to "
139 "access volume: %d", up->name, error);
143 error = gv_sync_plex(p, up);
147 * In case we have a stripe that is up, check whether it can be
150 } else if (p->org == GV_PLEX_STRIPED &&
151 p->state != GV_PLEX_DOWN) {
152 LIST_FOREACH(s, &p->subdisks, in_plex) {
153 if (s->flags & GV_SD_GROW) {
154 error = gv_grow_plex(p);
164 gv_start_vol(struct gv_volume *v)
169 KASSERT(v != NULL, ("gv_start_vol: NULL v"));
173 if (v->plexcount == 0)
176 else if (v->plexcount == 1) {
177 p = LIST_FIRST(&v->plexes);
178 KASSERT(p != NULL, ("gv_start_vol: NULL p on %s", v->name));
179 error = gv_start_plex(p);
186 /* Sync a plex p from the plex up. */
188 gv_sync_plex(struct gv_plex *p, struct gv_plex *up)
192 KASSERT(p != NULL, ("%s: NULL p", __func__));
193 KASSERT(up != NULL, ("%s: NULL up", __func__));
194 if ((p == up) || (p->state == GV_PLEX_UP))
196 if (p->flags & GV_PLEX_SYNCING ||
197 p->flags & GV_PLEX_REBUILDING ||
198 p->flags & GV_PLEX_GROWING) {
199 return (EINPROGRESS);
202 p->flags |= GV_PLEX_SYNCING;
203 G_VINUM_DEBUG(1, "starting sync of plex %s", p->name);
204 error = gv_sync_request(up, p, p->synced,
205 MIN(GV_DFLT_SYNCSIZE, up->size - p->synced),
208 G_VINUM_DEBUG(0, "error syncing plex %s", p->name);
214 /* Return a good plex from volume v. */
215 static struct gv_plex *
216 gv_find_good_plex(struct gv_volume *v)
220 /* Find the plex that's up. */
222 LIST_FOREACH(up, &v->plexes, in_volume) {
223 if (up->state == GV_PLEX_UP)
226 /* Didn't find a good plex. */
231 gv_sync(struct gv_volume *v)
234 struct gv_plex *p, *up;
237 KASSERT(v != NULL, ("gv_sync: NULL v"));
239 KASSERT(sc != NULL, ("gv_sync: NULL sc on %s", v->name));
242 up = gv_find_good_plex(v);
246 error = gv_access(v->provider, 1, 1, 0);
249 G_VINUM_DEBUG(0, "sync from '%s' failed to access volume: %d",
255 /* Go through the good plex, and issue BIO's to all other plexes. */
256 LIST_FOREACH(p, &v->plexes, in_volume) {
257 error = gv_sync_plex(p, up);
265 gv_rebuild_plex(struct gv_plex *p)
271 if (p->flags & GV_PLEX_SYNCING ||
272 p->flags & GV_PLEX_REBUILDING ||
273 p->flags & GV_PLEX_GROWING)
274 return (EINPROGRESS);
276 * Make sure that all subdisks have consumers. We won't allow a rebuild
277 * unless every subdisk have one.
279 LIST_FOREACH(s, &p->subdisks, in_plex) {
281 if (d == NULL || (d->flags & GV_DRIVE_REFERENCED)) {
282 G_VINUM_DEBUG(0, "unable to rebuild %s, subdisk(s) have"
283 " no drives", p->name);
287 p->flags |= GV_PLEX_REBUILDING;
290 g_topology_assert_not();
292 error = gv_access(p->vol_sc->provider, 1, 1, 0);
294 G_VINUM_DEBUG(0, "unable to access provider");
299 gv_parity_request(p, GV_BIO_REBUILD, 0);
304 gv_grow_plex(struct gv_plex *p)
308 off_t origsize, origlength;
311 KASSERT(p != NULL, ("gv_grow_plex: NULL p"));
313 KASSERT(v != NULL, ("gv_grow_plex: NULL v"));
315 if (p->flags & GV_PLEX_GROWING ||
316 p->flags & GV_PLEX_SYNCING ||
317 p->flags & GV_PLEX_REBUILDING)
318 return (EINPROGRESS);
320 error = gv_access(v->provider, 1, 1, 0);
323 G_VINUM_DEBUG(0, "unable to access provider");
327 /* XXX: This routine with finding origsize is used two other places as
328 * well, so we should create a function for it. */
329 sdcount = p->sdcount;
330 LIST_FOREACH(s, &p->subdisks, in_plex) {
331 if (s->flags & GV_SD_GROW)
334 s = LIST_FIRST(&p->subdisks);
336 G_VINUM_DEBUG(0, "error growing plex without subdisks");
337 return (GV_ERR_NOTFOUND);
339 p->flags |= GV_PLEX_GROWING;
340 origsize = (sdcount - 1) * s->size;
341 origlength = (sdcount - 1) * p->stripesize;
343 G_VINUM_DEBUG(1, "starting growing of plex %s", p->name);
344 gv_grow_request(p, 0, MIN(origlength, origsize), BIO_READ, NULL);
350 gv_init_plex(struct gv_plex *p)
358 KASSERT(p != NULL, ("gv_init_plex: NULL p"));
360 LIST_FOREACH(s, &p->subdisks, in_plex) {
361 if (s->state == GV_SD_INITIALIZING)
362 return (EINPROGRESS);
363 gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
364 s->init_size = GV_DFLT_SYNCSIZE;
365 start = s->drive_offset + s->initialized;
368 G_VINUM_DEBUG(0, "subdisk %s has no drive yet", s->name);
372 * Take the lock here since we need to avoid a race in
373 * gv_init_request if the BIO is completed before the lock is
377 error = g_access(d->consumer, 0, 1, 0);
380 G_VINUM_DEBUG(0, "error accessing consumer when "
381 "initializing %s", s->name);
384 data = g_malloc(s->init_size, M_WAITOK | M_ZERO);
385 gv_init_request(s, start, data, s->init_size);