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)
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));
245 up = gv_find_good_plex(v);
249 error = gv_access(v->provider, 1, 1, 0);
252 G_VINUM_DEBUG(0, "sync from '%s' failed to access volume: %d",
258 /* Go through the good plex, and issue BIO's to all other plexes. */
259 LIST_FOREACH(p, &v->plexes, in_volume) {
260 error = gv_sync_plex(p, up);
268 gv_rebuild_plex(struct gv_plex *p)
274 if (p->flags & GV_PLEX_SYNCING ||
275 p->flags & GV_PLEX_REBUILDING ||
276 p->flags & GV_PLEX_GROWING)
277 return (EINPROGRESS);
279 * Make sure that all subdisks have consumers. We won't allow a rebuild
280 * unless every subdisk have one.
282 LIST_FOREACH(s, &p->subdisks, in_plex) {
284 if (d == NULL || (d->flags & GV_DRIVE_REFERENCED)) {
285 G_VINUM_DEBUG(0, "unable to rebuild %s, subdisk(s) have"
286 " no drives", p->name);
290 p->flags |= GV_PLEX_REBUILDING;
293 g_topology_assert_not();
295 error = gv_access(p->vol_sc->provider, 1, 1, 0);
297 G_VINUM_DEBUG(0, "unable to access provider");
302 gv_parity_request(p, GV_BIO_REBUILD, 0);
307 gv_grow_plex(struct gv_plex *p)
311 off_t origsize, origlength;
314 KASSERT(p != NULL, ("gv_grow_plex: NULL p"));
316 KASSERT(v != NULL, ("gv_grow_plex: NULL v"));
318 if (p->flags & GV_PLEX_GROWING ||
319 p->flags & GV_PLEX_SYNCING ||
320 p->flags & GV_PLEX_REBUILDING)
321 return (EINPROGRESS);
323 error = gv_access(v->provider, 1, 1, 0);
326 G_VINUM_DEBUG(0, "unable to access provider");
330 /* XXX: This routine with finding origsize is used two other places as
331 * well, so we should create a function for it. */
332 sdcount = p->sdcount;
333 LIST_FOREACH(s, &p->subdisks, in_plex) {
334 if (s->flags & GV_SD_GROW)
337 s = LIST_FIRST(&p->subdisks);
339 G_VINUM_DEBUG(0, "error growing plex without subdisks");
340 return (GV_ERR_NOTFOUND);
342 p->flags |= GV_PLEX_GROWING;
343 origsize = (sdcount - 1) * s->size;
344 origlength = (sdcount - 1) * p->stripesize;
346 G_VINUM_DEBUG(1, "starting growing of plex %s", p->name);
347 gv_grow_request(p, 0, MIN(origlength, origsize), BIO_READ, NULL);
353 gv_init_plex(struct gv_plex *p)
361 KASSERT(p != NULL, ("gv_init_plex: NULL p"));
363 LIST_FOREACH(s, &p->subdisks, in_plex) {
364 if (s->state == GV_SD_INITIALIZING)
365 return (EINPROGRESS);
366 gv_set_sd_state(s, GV_SD_INITIALIZING, GV_SETSTATE_FORCE);
367 s->init_size = GV_DFLT_SYNCSIZE;
368 start = s->drive_offset + s->initialized;
371 G_VINUM_DEBUG(0, "subdisk %s has no drive yet", s->name);
375 * Take the lock here since we need to avoid a race in
376 * gv_init_request if the BIO is completed before the lock is
380 error = g_access(d->consumer, 0, 1, 0);
383 G_VINUM_DEBUG(0, "error accessing consumer when "
384 "initializing %s", s->name);
387 data = g_malloc(s->init_size, M_WAITOK | M_ZERO);
388 gv_init_request(s, start, data, s->init_size);