2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Copyright (c) 2007, 2009 Ulf Lilleengen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
37 #include <geom/geom.h>
38 #include <geom/geom_dbg.h>
39 #include <geom/vinum/geom_vinum_var.h>
40 #include <geom/vinum/geom_vinum.h>
42 static int gv_sync(struct gv_volume *);
43 static int gv_rebuild_plex(struct gv_plex *);
44 static int gv_init_plex(struct gv_plex *);
45 static int gv_grow_plex(struct gv_plex *);
46 static int gv_sync_plex(struct gv_plex *, struct gv_plex *);
47 static struct gv_plex *gv_find_good_plex(struct gv_volume *);
50 gv_start_obj(struct g_geom *gp, struct gctl_req *req)
59 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
60 initsize = gctl_get_paraml(req, "initsize", sizeof(*initsize));
62 if (argc == NULL || *argc == 0) {
63 gctl_error(req, "no arguments given");
69 for (i = 0; i < *argc; i++) {
70 snprintf(buf, sizeof(buf), "argv%d", i);
71 argv = gctl_get_param(req, buf, NULL);
74 type = gv_object_type(sc, argv);
77 v = gv_find_vol(sc, argv);
79 gv_post_event(sc, GV_EVENT_START_VOLUME, v,
84 p = gv_find_plex(sc, argv);
86 gv_post_event(sc, GV_EVENT_START_PLEX, p, NULL,
92 /* XXX Not implemented, but what is the use? */
93 gctl_error(req, "unable to start '%s' - not yet supported",
97 gctl_error(req, "unknown object '%s'", argv);
104 gv_start_plex(struct gv_plex *p)
111 KASSERT(p != NULL, ("gv_start_plex: NULL p"));
116 /* RAID5 plexes can either be init, rebuilt or grown. */
117 if (p->org == GV_PLEX_RAID5) {
118 if (p->state > GV_PLEX_DEGRADED) {
119 LIST_FOREACH(s, &p->subdisks, in_plex) {
120 if (s->flags & GV_SD_GROW) {
121 error = gv_grow_plex(p);
125 } else if (p->state == GV_PLEX_DEGRADED) {
126 error = gv_rebuild_plex(p);
128 error = gv_init_plex(p);
130 /* We want to sync from the other plex if we're down. */
131 if (p->state == GV_PLEX_DOWN && v->plexcount > 1) {
132 up = gv_find_good_plex(v);
134 G_VINUM_DEBUG(1, "unable to find a good plex");
138 error = gv_access(v->provider, 1, 1, 0);
141 G_VINUM_DEBUG(0, "sync from '%s' failed to "
142 "access volume: %d", up->name, error);
146 error = gv_sync_plex(p, up);
150 * In case we have a stripe that is up, check whether it can be
153 } else if (p->org == GV_PLEX_STRIPED &&
154 p->state != GV_PLEX_DOWN) {
155 LIST_FOREACH(s, &p->subdisks, in_plex) {
156 if (s->flags & GV_SD_GROW) {
157 error = gv_grow_plex(p);
167 gv_start_vol(struct gv_volume *v)
172 KASSERT(v != NULL, ("gv_start_vol: NULL v"));
176 if (v->plexcount == 0)
179 else if (v->plexcount == 1) {
180 p = LIST_FIRST(&v->plexes);
181 KASSERT(p != NULL, ("gv_start_vol: NULL p on %s", v->name));
182 error = gv_start_plex(p);
189 /* Sync a plex p from the plex up. */
191 gv_sync_plex(struct gv_plex *p, struct gv_plex *up)
195 KASSERT(p != NULL, ("%s: NULL p", __func__));
196 KASSERT(up != NULL, ("%s: NULL up", __func__));
197 if ((p == up) || (p->state == GV_PLEX_UP))
199 if (p->flags & GV_PLEX_SYNCING ||
200 p->flags & GV_PLEX_REBUILDING ||
201 p->flags & GV_PLEX_GROWING) {
202 return (EINPROGRESS);
205 p->flags |= GV_PLEX_SYNCING;
206 G_VINUM_DEBUG(1, "starting sync of plex %s", p->name);
207 error = gv_sync_request(up, p, p->synced,
208 MIN(GV_DFLT_SYNCSIZE, up->size - p->synced),
211 G_VINUM_DEBUG(0, "error syncing plex %s", p->name);
217 /* Return a good plex from volume v. */
218 static struct gv_plex *
219 gv_find_good_plex(struct gv_volume *v)
223 /* Find the plex that's up. */
225 LIST_FOREACH(up, &v->plexes, in_volume) {
226 if (up->state == GV_PLEX_UP)
229 /* Didn't find a good plex. */
234 gv_sync(struct gv_volume *v)
236 struct gv_softc *sc __diagused;
237 struct gv_plex *p, *up;
240 KASSERT(v != NULL, ("gv_sync: NULL v"));
242 KASSERT(sc != NULL, ("gv_sync: NULL sc on %s", v->name));
244 up = gv_find_good_plex(v);
248 error = gv_access(v->provider, 1, 1, 0);
251 G_VINUM_DEBUG(0, "sync from '%s' failed to access volume: %d",
257 /* Go through the good plex, and issue BIO's to all other plexes. */
258 LIST_FOREACH(p, &v->plexes, in_volume) {
259 error = gv_sync_plex(p, up);
267 gv_rebuild_plex(struct gv_plex *p)
273 if (p->flags & GV_PLEX_SYNCING ||
274 p->flags & GV_PLEX_REBUILDING ||
275 p->flags & GV_PLEX_GROWING)
276 return (EINPROGRESS);
278 * Make sure that all subdisks have consumers. We won't allow a rebuild
279 * unless every subdisk have one.
281 LIST_FOREACH(s, &p->subdisks, in_plex) {
283 if (d == NULL || (d->flags & GV_DRIVE_REFERENCED)) {
284 G_VINUM_DEBUG(0, "unable to rebuild %s, subdisk(s) have"
285 " no drives", p->name);
289 p->flags |= GV_PLEX_REBUILDING;
292 g_topology_assert_not();
294 error = gv_access(p->vol_sc->provider, 1, 1, 0);
296 G_VINUM_DEBUG(0, "unable to access provider");
301 gv_parity_request(p, GV_BIO_REBUILD, 0);
306 gv_grow_plex(struct gv_plex *p)
310 off_t origsize, origlength;
313 KASSERT(p != NULL, ("gv_grow_plex: NULL p"));
315 KASSERT(v != NULL, ("gv_grow_plex: NULL v"));
317 if (p->flags & GV_PLEX_GROWING ||
318 p->flags & GV_PLEX_SYNCING ||
319 p->flags & GV_PLEX_REBUILDING)
320 return (EINPROGRESS);
322 error = gv_access(v->provider, 1, 1, 0);
325 G_VINUM_DEBUG(0, "unable to access provider");
329 /* XXX: This routine with finding origsize is used two other places as
330 * well, so we should create a function for it. */
331 sdcount = p->sdcount;
332 LIST_FOREACH(s, &p->subdisks, in_plex) {
333 if (s->flags & GV_SD_GROW)
336 s = LIST_FIRST(&p->subdisks);
338 G_VINUM_DEBUG(0, "error growing plex without subdisks");
339 return (GV_ERR_NOTFOUND);
341 p->flags |= GV_PLEX_GROWING;
342 origsize = (sdcount - 1) * s->size;
343 origlength = (sdcount - 1) * p->stripesize;
345 G_VINUM_DEBUG(1, "starting growing of plex %s", p->name);
346 gv_grow_request(p, 0, MIN(origlength, origsize), BIO_READ, NULL);
352 gv_init_plex(struct gv_plex *p)
360 KASSERT(p != NULL, ("gv_init_plex: NULL p"));
362 LIST_FOREACH(s, &p->subdisks, in_plex) {
363 if (s->state == GV_SD_INITIALIZING)
364 return (EINPROGRESS);
365 gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
366 s->init_size = GV_DFLT_SYNCSIZE;
367 start = s->drive_offset + s->initialized;
370 G_VINUM_DEBUG(0, "subdisk %s has no drive yet", s->name);
374 * Take the lock here since we need to avoid a race in
375 * gv_init_request if the BIO is completed before the lock is
379 error = g_access(d->consumer, 0, 1, 0);
382 G_VINUM_DEBUG(0, "error accessing consumer when "
383 "initializing %s", s->name);
386 data = g_malloc(s->init_size, M_WAITOK | M_ZERO);
387 gv_init_request(s, start, data, s->init_size);