1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/reboot.h>
42 #include <sys/sbuf.h>
43 #include <sys/sched.h>
44 #include <sys/sysctl.h>
45
46 #include <vm/uma.h>
47
48 #include <geom/geom.h>
49 #include <geom/geom_dbg.h>
50 #include <geom/geom_disk.h>
51 #include <geom/raid3/g_raid3.h>
52
53 FEATURE(geom_raid3, "GEOM RAID-3 functionality");
54
55 static MALLOC_DEFINE(M_RAID3, "raid3_data", "GEOM_RAID3 Data");
56
57 SYSCTL_DECL(_kern_geom);
58 static SYSCTL_NODE(_kern_geom, OID_AUTO, raid3, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
59 "GEOM_RAID3 stuff");
60 u_int g_raid3_debug = 0;
61 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, debug, CTLFLAG_RWTUN, &g_raid3_debug, 0,
62 "Debug level");
63 static u_int g_raid3_timeout = 4;
64 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_raid3_timeout,
65 0, "Time to wait on all raid3 components");
66 static u_int g_raid3_idletime = 5;
67 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, idletime, CTLFLAG_RWTUN,
68 &g_raid3_idletime, 0, "Mark components as clean when idling");
69 static u_int g_raid3_disconnect_on_failure = 1;
70 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
71 &g_raid3_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
72 static u_int g_raid3_syncreqs = 2;
73 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
74 &g_raid3_syncreqs, 0, "Parallel synchronization I/O requests.");
75 static u_int g_raid3_use_malloc = 0;
76 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, use_malloc, CTLFLAG_RDTUN,
77 &g_raid3_use_malloc, 0, "Use malloc(9) instead of uma(9).");
78
79 static u_int g_raid3_n64k = 50;
80 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n64k, CTLFLAG_RDTUN, &g_raid3_n64k, 0,
81 "Maximum number of 64kB allocations");
82 static u_int g_raid3_n16k = 200;
83 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n16k, CTLFLAG_RDTUN, &g_raid3_n16k, 0,
84 "Maximum number of 16kB allocations");
85 static u_int g_raid3_n4k = 1200;
86 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n4k, CTLFLAG_RDTUN, &g_raid3_n4k, 0,
87 "Maximum number of 4kB allocations");
88
89 static SYSCTL_NODE(_kern_geom_raid3, OID_AUTO, stat,
90 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
91 "GEOM_RAID3 statistics");
92 static u_int g_raid3_parity_mismatch = 0;
93 SYSCTL_UINT(_kern_geom_raid3_stat, OID_AUTO, parity_mismatch, CTLFLAG_RD,
94 &g_raid3_parity_mismatch, 0, "Number of failures in VERIFY mode");
95
96 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
97 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
98 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
99 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
100 } while (0)
101
102 static eventhandler_tag g_raid3_post_sync = NULL;
103 static int g_raid3_shutdown = 0;
104
105 static int g_raid3_destroy_geom(struct gctl_req *req, struct g_class *mp,
106 struct g_geom *gp);
107 static g_taste_t g_raid3_taste;
108 static void g_raid3_init(struct g_class *mp);
109 static void g_raid3_fini(struct g_class *mp);
110 static void g_raid3_providergone(struct g_provider *pp);
111
112 struct g_class g_raid3_class = {
113 .name = G_RAID3_CLASS_NAME,
114 .version = G_VERSION,
115 .ctlreq = g_raid3_config,
116 .taste = g_raid3_taste,
117 .destroy_geom = g_raid3_destroy_geom,
118 .init = g_raid3_init,
119 .fini = g_raid3_fini,
120 .providergone = g_raid3_providergone,
121 };
122
123 static void g_raid3_destroy_provider(struct g_raid3_softc *sc);
124 static int g_raid3_update_disk(struct g_raid3_disk *disk, u_int state);
125 static void g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force);
126 static void g_raid3_dumpconf(struct sbuf *sb, const char *indent,
127 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
128 static void g_raid3_sync_stop(struct g_raid3_softc *sc, int type);
129 static int g_raid3_register_request(struct bio *pbp);
130 static void g_raid3_sync_release(struct g_raid3_softc *sc);
131 static void g_raid3_timeout_drain(struct g_raid3_softc *sc);
132
133 static const char *
g_raid3_disk_state2str(int state)134 g_raid3_disk_state2str(int state)
135 {
136
137 switch (state) {
138 case G_RAID3_DISK_STATE_NODISK:
139 return ("NODISK");
140 case G_RAID3_DISK_STATE_NONE:
141 return ("NONE");
142 case G_RAID3_DISK_STATE_NEW:
143 return ("NEW");
144 case G_RAID3_DISK_STATE_ACTIVE:
145 return ("ACTIVE");
146 case G_RAID3_DISK_STATE_STALE:
147 return ("STALE");
148 case G_RAID3_DISK_STATE_SYNCHRONIZING:
149 return ("SYNCHRONIZING");
150 case G_RAID3_DISK_STATE_DISCONNECTED:
151 return ("DISCONNECTED");
152 default:
153 return ("INVALID");
154 }
155 }
156
157 static const char *
g_raid3_device_state2str(int state)158 g_raid3_device_state2str(int state)
159 {
160
161 switch (state) {
162 case G_RAID3_DEVICE_STATE_STARTING:
163 return ("STARTING");
164 case G_RAID3_DEVICE_STATE_DEGRADED:
165 return ("DEGRADED");
166 case G_RAID3_DEVICE_STATE_COMPLETE:
167 return ("COMPLETE");
168 default:
169 return ("INVALID");
170 }
171 }
172
173 const char *
g_raid3_get_diskname(struct g_raid3_disk * disk)174 g_raid3_get_diskname(struct g_raid3_disk *disk)
175 {
176
177 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
178 return ("[unknown]");
179 return (disk->d_name);
180 }
181
182 static void *
g_raid3_alloc(struct g_raid3_softc * sc,size_t size,int flags)183 g_raid3_alloc(struct g_raid3_softc *sc, size_t size, int flags)
184 {
185 void *ptr;
186 enum g_raid3_zones zone;
187
188 if (g_raid3_use_malloc ||
189 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
190 ptr = malloc(size, M_RAID3, flags);
191 else {
192 ptr = uma_zalloc_arg(sc->sc_zones[zone].sz_zone,
193 &sc->sc_zones[zone], flags);
194 sc->sc_zones[zone].sz_requested++;
195 if (ptr == NULL)
196 sc->sc_zones[zone].sz_failed++;
197 }
198 return (ptr);
199 }
200
201 static void
g_raid3_free(struct g_raid3_softc * sc,void * ptr,size_t size)202 g_raid3_free(struct g_raid3_softc *sc, void *ptr, size_t size)
203 {
204 enum g_raid3_zones zone;
205
206 if (g_raid3_use_malloc ||
207 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
208 free(ptr, M_RAID3);
209 else {
210 uma_zfree_arg(sc->sc_zones[zone].sz_zone,
211 ptr, &sc->sc_zones[zone]);
212 }
213 }
214
215 static int
g_raid3_uma_ctor(void * mem,int size,void * arg,int flags)216 g_raid3_uma_ctor(void *mem, int size, void *arg, int flags)
217 {
218 struct g_raid3_zone *sz = arg;
219
220 if (sz->sz_max > 0 && sz->sz_inuse == sz->sz_max)
221 return (ENOMEM);
222 sz->sz_inuse++;
223 return (0);
224 }
225
226 static void
g_raid3_uma_dtor(void * mem,int size,void * arg)227 g_raid3_uma_dtor(void *mem, int size, void *arg)
228 {
229 struct g_raid3_zone *sz = arg;
230
231 sz->sz_inuse--;
232 }
233
234 #define g_raid3_xor(src, dst, size) \
235 _g_raid3_xor((uint64_t *)(src), \
236 (uint64_t *)(dst), (size_t)size)
237 static void
_g_raid3_xor(uint64_t * src,uint64_t * dst,size_t size)238 _g_raid3_xor(uint64_t *src, uint64_t *dst, size_t size)
239 {
240
241 KASSERT((size % 128) == 0, ("Invalid size: %zu.", size));
242 for (; size > 0; size -= 128) {
243 *dst++ ^= (*src++);
244 *dst++ ^= (*src++);
245 *dst++ ^= (*src++);
246 *dst++ ^= (*src++);
247 *dst++ ^= (*src++);
248 *dst++ ^= (*src++);
249 *dst++ ^= (*src++);
250 *dst++ ^= (*src++);
251 *dst++ ^= (*src++);
252 *dst++ ^= (*src++);
253 *dst++ ^= (*src++);
254 *dst++ ^= (*src++);
255 *dst++ ^= (*src++);
256 *dst++ ^= (*src++);
257 *dst++ ^= (*src++);
258 *dst++ ^= (*src++);
259 }
260 }
261
262 static int
g_raid3_is_zero(struct bio * bp)263 g_raid3_is_zero(struct bio *bp)
264 {
265 static const uint64_t zeros[] = {
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
267 };
268 u_char *addr;
269 ssize_t size;
270
271 size = bp->bio_length;
272 addr = (u_char *)bp->bio_data;
273 for (; size > 0; size -= sizeof(zeros), addr += sizeof(zeros)) {
274 if (bcmp(addr, zeros, sizeof(zeros)) != 0)
275 return (0);
276 }
277 return (1);
278 }
279
280 /*
281 * --- Events handling functions ---
282 * Events in geom_raid3 are used to maintain disks and device status
283 * from one thread to simplify locking.
284 */
285 static void
g_raid3_event_free(struct g_raid3_event * ep)286 g_raid3_event_free(struct g_raid3_event *ep)
287 {
288
289 free(ep, M_RAID3);
290 }
291
292 static int
g_raid3_event_dispatch(struct g_raid3_event * ep,void * arg,int state,int flags)293 g_raid3_event_dispatch(struct g_raid3_event *ep, void *arg, int state,
294 int flags)
295 {
296 struct g_raid3_softc *sc;
297 struct g_raid3_disk *disk;
298 int error;
299
300 G_RAID3_DEBUG(4, "%s: Sending event %p.", __func__, ep);
301 if ((flags & G_RAID3_EVENT_DEVICE) != 0) {
302 disk = NULL;
303 sc = arg;
304 } else {
305 disk = arg;
306 sc = disk->d_softc;
307 }
308 ep->e_disk = disk;
309 ep->e_state = state;
310 ep->e_flags = flags;
311 ep->e_error = 0;
312 mtx_lock(&sc->sc_events_mtx);
313 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
314 mtx_unlock(&sc->sc_events_mtx);
315 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
316 mtx_lock(&sc->sc_queue_mtx);
317 wakeup(sc);
318 wakeup(&sc->sc_queue);
319 mtx_unlock(&sc->sc_queue_mtx);
320 if ((flags & G_RAID3_EVENT_DONTWAIT) != 0)
321 return (0);
322 sx_assert(&sc->sc_lock, SX_XLOCKED);
323 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
324 sx_xunlock(&sc->sc_lock);
325 while ((ep->e_flags & G_RAID3_EVENT_DONE) == 0) {
326 mtx_lock(&sc->sc_events_mtx);
327 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "r3:event",
328 hz * 5);
329 }
330 error = ep->e_error;
331 g_raid3_event_free(ep);
332 sx_xlock(&sc->sc_lock);
333 return (error);
334 }
335
336 int
g_raid3_event_send(void * arg,int state,int flags)337 g_raid3_event_send(void *arg, int state, int flags)
338 {
339 struct g_raid3_event *ep;
340
341 ep = malloc(sizeof(*ep), M_RAID3, M_WAITOK);
342 return (g_raid3_event_dispatch(ep, arg, state, flags));
343 }
344
345 static struct g_raid3_event *
g_raid3_event_get(struct g_raid3_softc * sc)346 g_raid3_event_get(struct g_raid3_softc *sc)
347 {
348 struct g_raid3_event *ep;
349
350 mtx_lock(&sc->sc_events_mtx);
351 ep = TAILQ_FIRST(&sc->sc_events);
352 mtx_unlock(&sc->sc_events_mtx);
353 return (ep);
354 }
355
356 static void
g_raid3_event_remove(struct g_raid3_softc * sc,struct g_raid3_event * ep)357 g_raid3_event_remove(struct g_raid3_softc *sc, struct g_raid3_event *ep)
358 {
359
360 mtx_lock(&sc->sc_events_mtx);
361 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
362 mtx_unlock(&sc->sc_events_mtx);
363 }
364
365 static void
g_raid3_event_cancel(struct g_raid3_disk * disk)366 g_raid3_event_cancel(struct g_raid3_disk *disk)
367 {
368 struct g_raid3_softc *sc;
369 struct g_raid3_event *ep, *tmpep;
370
371 sc = disk->d_softc;
372 sx_assert(&sc->sc_lock, SX_XLOCKED);
373
374 mtx_lock(&sc->sc_events_mtx);
375 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
376 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0)
377 continue;
378 if (ep->e_disk != disk)
379 continue;
380 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
381 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
382 g_raid3_event_free(ep);
383 else {
384 ep->e_error = ECANCELED;
385 wakeup(ep);
386 }
387 }
388 mtx_unlock(&sc->sc_events_mtx);
389 }
390
391 /*
392 * Return the number of disks in the given state.
393 * If state is equal to -1, count all connected disks.
394 */
395 u_int
g_raid3_ndisks(struct g_raid3_softc * sc,int state)396 g_raid3_ndisks(struct g_raid3_softc *sc, int state)
397 {
398 struct g_raid3_disk *disk;
399 u_int n, ndisks;
400
401 sx_assert(&sc->sc_lock, SX_LOCKED);
402
403 for (n = ndisks = 0; n < sc->sc_ndisks; n++) {
404 disk = &sc->sc_disks[n];
405 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
406 continue;
407 if (state == -1 || disk->d_state == state)
408 ndisks++;
409 }
410 return (ndisks);
411 }
412
413 static u_int
g_raid3_nrequests(struct g_raid3_softc * sc,struct g_consumer * cp)414 g_raid3_nrequests(struct g_raid3_softc *sc, struct g_consumer *cp)
415 {
416 struct bio *bp;
417 u_int nreqs = 0;
418
419 mtx_lock(&sc->sc_queue_mtx);
420 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
421 if (bp->bio_from == cp)
422 nreqs++;
423 }
424 mtx_unlock(&sc->sc_queue_mtx);
425 return (nreqs);
426 }
427
428 static int
g_raid3_is_busy(struct g_raid3_softc * sc,struct g_consumer * cp)429 g_raid3_is_busy(struct g_raid3_softc *sc, struct g_consumer *cp)
430 {
431
432 if (cp->index > 0) {
433 G_RAID3_DEBUG(2,
434 "I/O requests for %s exist, can't destroy it now.",
435 cp->provider->name);
436 return (1);
437 }
438 if (g_raid3_nrequests(sc, cp) > 0) {
439 G_RAID3_DEBUG(2,
440 "I/O requests for %s in queue, can't destroy it now.",
441 cp->provider->name);
442 return (1);
443 }
444 return (0);
445 }
446
447 static void
g_raid3_destroy_consumer(void * arg,int flags __unused)448 g_raid3_destroy_consumer(void *arg, int flags __unused)
449 {
450 struct g_consumer *cp;
451
452 g_topology_assert();
453
454 cp = arg;
455 G_RAID3_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
456 g_detach(cp);
457 g_destroy_consumer(cp);
458 }
459
460 static void
g_raid3_kill_consumer(struct g_raid3_softc * sc,struct g_consumer * cp)461 g_raid3_kill_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
462 {
463 struct g_provider *pp;
464 int retaste_wait;
465
466 g_topology_assert();
467
468 cp->private = NULL;
469 if (g_raid3_is_busy(sc, cp))
470 return;
471 G_RAID3_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
472 pp = cp->provider;
473 retaste_wait = 0;
474 if (cp->acw == 1) {
475 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
476 retaste_wait = 1;
477 }
478 G_RAID3_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
479 -cp->acw, -cp->ace, 0);
480 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
481 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
482 if (retaste_wait) {
483 /*
484 * After retaste event was send (inside g_access()), we can send
485 * event to detach and destroy consumer.
486 * A class, which has consumer to the given provider connected
487 * will not receive retaste event for the provider.
488 * This is the way how I ignore retaste events when I close
489 * consumers opened for write: I detach and destroy consumer
490 * after retaste event is sent.
491 */
492 g_post_event(g_raid3_destroy_consumer, cp, M_WAITOK, NULL);
493 return;
494 }
495 G_RAID3_DEBUG(1, "Consumer %s destroyed.", pp->name);
496 g_detach(cp);
497 g_destroy_consumer(cp);
498 }
499
500 static int
g_raid3_connect_disk(struct g_raid3_disk * disk,struct g_provider * pp)501 g_raid3_connect_disk(struct g_raid3_disk *disk, struct g_provider *pp)
502 {
503 struct g_consumer *cp;
504 int error;
505
506 g_topology_assert_not();
507 KASSERT(disk->d_consumer == NULL,
508 ("Disk already connected (device %s).", disk->d_softc->sc_name));
509
510 g_topology_lock();
511 cp = g_new_consumer(disk->d_softc->sc_geom);
512 error = g_attach(cp, pp);
513 if (error != 0) {
514 g_destroy_consumer(cp);
515 g_topology_unlock();
516 return (error);
517 }
518 error = g_access(cp, 1, 1, 1);
519 g_topology_unlock();
520 if (error != 0) {
521 g_detach(cp);
522 g_destroy_consumer(cp);
523 G_RAID3_DEBUG(0, "Cannot open consumer %s (error=%d).",
524 pp->name, error);
525 return (error);
526 }
527 disk->d_consumer = cp;
528 disk->d_consumer->private = disk;
529 disk->d_consumer->index = 0;
530 G_RAID3_DEBUG(2, "Disk %s connected.", g_raid3_get_diskname(disk));
531 return (0);
532 }
533
534 static void
g_raid3_disconnect_consumer(struct g_raid3_softc * sc,struct g_consumer * cp)535 g_raid3_disconnect_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
536 {
537
538 g_topology_assert();
539
540 if (cp == NULL)
541 return;
542 if (cp->provider != NULL)
543 g_raid3_kill_consumer(sc, cp);
544 else
545 g_destroy_consumer(cp);
546 }
547
548 /*
549 * Initialize disk. This means allocate memory, create consumer, attach it
550 * to the provider and open access (r1w1e1) to it.
551 */
552 static struct g_raid3_disk *
g_raid3_init_disk(struct g_raid3_softc * sc,struct g_provider * pp,struct g_raid3_metadata * md,int * errorp)553 g_raid3_init_disk(struct g_raid3_softc *sc, struct g_provider *pp,
554 struct g_raid3_metadata *md, int *errorp)
555 {
556 struct g_raid3_disk *disk;
557 int error;
558
559 disk = &sc->sc_disks[md->md_no];
560 error = g_raid3_connect_disk(disk, pp);
561 if (error != 0) {
562 if (errorp != NULL)
563 *errorp = error;
564 return (NULL);
565 }
566 disk->d_state = G_RAID3_DISK_STATE_NONE;
567 disk->d_flags = md->md_dflags;
568 error = g_getattr("GEOM::rotation_rate", disk->d_consumer,
569 &disk->d_rotation_rate);
570 if (error != 0)
571 disk->d_rotation_rate = DISK_RR_UNKNOWN;
572 if (md->md_provider[0] != '\0')
573 disk->d_flags |= G_RAID3_DISK_FLAG_HARDCODED;
574 disk->d_sync.ds_consumer = NULL;
575 disk->d_sync.ds_offset = md->md_sync_offset;
576 disk->d_sync.ds_offset_done = md->md_sync_offset;
577 disk->d_genid = md->md_genid;
578 disk->d_sync.ds_syncid = md->md_syncid;
579 if (errorp != NULL)
580 *errorp = 0;
581 return (disk);
582 }
583
584 static void
g_raid3_destroy_disk(struct g_raid3_disk * disk)585 g_raid3_destroy_disk(struct g_raid3_disk *disk)
586 {
587 struct g_raid3_softc *sc;
588
589 g_topology_assert_not();
590 sc = disk->d_softc;
591 sx_assert(&sc->sc_lock, SX_XLOCKED);
592
593 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
594 return;
595 g_raid3_event_cancel(disk);
596 switch (disk->d_state) {
597 case G_RAID3_DISK_STATE_SYNCHRONIZING:
598 if (sc->sc_syncdisk != NULL)
599 g_raid3_sync_stop(sc, 1);
600 /* FALLTHROUGH */
601 case G_RAID3_DISK_STATE_NEW:
602 case G_RAID3_DISK_STATE_STALE:
603 case G_RAID3_DISK_STATE_ACTIVE:
604 g_topology_lock();
605 g_raid3_disconnect_consumer(sc, disk->d_consumer);
606 g_topology_unlock();
607 disk->d_consumer = NULL;
608 break;
609 default:
610 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
611 g_raid3_get_diskname(disk),
612 g_raid3_disk_state2str(disk->d_state)));
613 }
614 disk->d_state = G_RAID3_DISK_STATE_NODISK;
615 }
616
617 static void
g_raid3_free_device(struct g_raid3_softc * sc)618 g_raid3_free_device(struct g_raid3_softc *sc)
619 {
620 KASSERT(sc->sc_refcnt == 0,
621 ("%s: non-zero refcount %u", __func__, sc->sc_refcnt));
622
623 if (!g_raid3_use_malloc) {
624 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
625 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
626 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
627 }
628 mtx_destroy(&sc->sc_queue_mtx);
629 mtx_destroy(&sc->sc_events_mtx);
630 sx_xunlock(&sc->sc_lock);
631 sx_destroy(&sc->sc_lock);
632 free(sc->sc_disks, M_RAID3);
633 free(sc, M_RAID3);
634 }
635
636 static void
g_raid3_providergone(struct g_provider * pp)637 g_raid3_providergone(struct g_provider *pp)
638 {
639 struct g_raid3_softc *sc = pp->private;
640
641 if (--sc->sc_refcnt == 0)
642 g_raid3_free_device(sc);
643 }
644
645 static void
g_raid3_destroy_device(struct g_raid3_softc * sc)646 g_raid3_destroy_device(struct g_raid3_softc *sc)
647 {
648 struct g_raid3_event *ep;
649 struct g_raid3_disk *disk;
650 struct g_geom *gp;
651 struct g_consumer *cp;
652 u_int n;
653
654 g_topology_assert_not();
655 sx_assert(&sc->sc_lock, SX_XLOCKED);
656
657 gp = sc->sc_geom;
658 if (sc->sc_provider != NULL)
659 g_raid3_destroy_provider(sc);
660 for (n = 0; n < sc->sc_ndisks; n++) {
661 disk = &sc->sc_disks[n];
662 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
663 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
664 g_raid3_update_metadata(disk);
665 g_raid3_destroy_disk(disk);
666 }
667 }
668 while ((ep = g_raid3_event_get(sc)) != NULL) {
669 g_raid3_event_remove(sc, ep);
670 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
671 g_raid3_event_free(ep);
672 else {
673 ep->e_error = ECANCELED;
674 ep->e_flags |= G_RAID3_EVENT_DONE;
675 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, ep);
676 mtx_lock(&sc->sc_events_mtx);
677 wakeup(ep);
678 mtx_unlock(&sc->sc_events_mtx);
679 }
680 }
681 g_raid3_timeout_drain(sc);
682 cp = LIST_FIRST(&sc->sc_sync.ds_geom->consumer);
683 g_topology_lock();
684 if (cp != NULL)
685 g_raid3_disconnect_consumer(sc, cp);
686 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
687 G_RAID3_DEBUG(0, "Device %s destroyed.", gp->name);
688 g_wither_geom(gp, ENXIO);
689 if (--sc->sc_refcnt == 0)
690 g_raid3_free_device(sc);
691 g_topology_unlock();
692 }
693
694 static void
g_raid3_orphan(struct g_consumer * cp)695 g_raid3_orphan(struct g_consumer *cp)
696 {
697 struct g_raid3_disk *disk;
698
699 g_topology_assert();
700
701 disk = cp->private;
702 if (disk == NULL)
703 return;
704 disk->d_softc->sc_bump_id = G_RAID3_BUMP_SYNCID;
705 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
706 G_RAID3_EVENT_DONTWAIT);
707 }
708
709 static int
g_raid3_write_metadata(struct g_raid3_disk * disk,struct g_raid3_metadata * md)710 g_raid3_write_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
711 {
712 struct g_raid3_softc *sc;
713 struct g_consumer *cp;
714 off_t offset, length;
715 u_char *sector;
716 int error = 0;
717
718 g_topology_assert_not();
719 sc = disk->d_softc;
720 sx_assert(&sc->sc_lock, SX_LOCKED);
721
722 cp = disk->d_consumer;
723 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
724 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
725 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
726 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
727 cp->acw, cp->ace));
728 length = cp->provider->sectorsize;
729 offset = cp->provider->mediasize - length;
730 sector = malloc((size_t)length, M_RAID3, M_WAITOK | M_ZERO);
731 if (md != NULL)
732 raid3_metadata_encode(md, sector);
733 error = g_write_data(cp, offset, sector, length);
734 free(sector, M_RAID3);
735 if (error != 0) {
736 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
737 G_RAID3_DEBUG(0, "Cannot write metadata on %s "
738 "(device=%s, error=%d).",
739 g_raid3_get_diskname(disk), sc->sc_name, error);
740 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
741 } else {
742 G_RAID3_DEBUG(1, "Cannot write metadata on %s "
743 "(device=%s, error=%d).",
744 g_raid3_get_diskname(disk), sc->sc_name, error);
745 }
746 if (g_raid3_disconnect_on_failure &&
747 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
748 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
749 g_raid3_event_send(disk,
750 G_RAID3_DISK_STATE_DISCONNECTED,
751 G_RAID3_EVENT_DONTWAIT);
752 }
753 }
754 return (error);
755 }
756
757 int
g_raid3_clear_metadata(struct g_raid3_disk * disk)758 g_raid3_clear_metadata(struct g_raid3_disk *disk)
759 {
760 int error;
761
762 g_topology_assert_not();
763 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
764
765 error = g_raid3_write_metadata(disk, NULL);
766 if (error == 0) {
767 G_RAID3_DEBUG(2, "Metadata on %s cleared.",
768 g_raid3_get_diskname(disk));
769 } else {
770 G_RAID3_DEBUG(0,
771 "Cannot clear metadata on disk %s (error=%d).",
772 g_raid3_get_diskname(disk), error);
773 }
774 return (error);
775 }
776
777 void
g_raid3_fill_metadata(struct g_raid3_disk * disk,struct g_raid3_metadata * md)778 g_raid3_fill_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
779 {
780 struct g_raid3_softc *sc;
781 struct g_provider *pp;
782
783 bzero(md, sizeof(*md));
784 sc = disk->d_softc;
785 strlcpy(md->md_magic, G_RAID3_MAGIC, sizeof(md->md_magic));
786 md->md_version = G_RAID3_VERSION;
787 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
788 md->md_id = sc->sc_id;
789 md->md_all = sc->sc_ndisks;
790 md->md_genid = sc->sc_genid;
791 md->md_mediasize = sc->sc_mediasize;
792 md->md_sectorsize = sc->sc_sectorsize;
793 md->md_mflags = (sc->sc_flags & G_RAID3_DEVICE_FLAG_MASK);
794 md->md_no = disk->d_no;
795 md->md_syncid = disk->d_sync.ds_syncid;
796 md->md_dflags = (disk->d_flags & G_RAID3_DISK_FLAG_MASK);
797 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
798 md->md_sync_offset =
799 disk->d_sync.ds_offset_done / (sc->sc_ndisks - 1);
800 }
801 if (disk->d_consumer != NULL && disk->d_consumer->provider != NULL)
802 pp = disk->d_consumer->provider;
803 else
804 pp = NULL;
805 if ((disk->d_flags & G_RAID3_DISK_FLAG_HARDCODED) != 0 && pp != NULL)
806 strlcpy(md->md_provider, pp->name, sizeof(md->md_provider));
807 if (pp != NULL)
808 md->md_provsize = pp->mediasize;
809 }
810
811 void
g_raid3_update_metadata(struct g_raid3_disk * disk)812 g_raid3_update_metadata(struct g_raid3_disk *disk)
813 {
814 struct g_raid3_softc *sc __diagused;
815 struct g_raid3_metadata md;
816 int error;
817
818 g_topology_assert_not();
819 sc = disk->d_softc;
820 sx_assert(&sc->sc_lock, SX_LOCKED);
821
822 g_raid3_fill_metadata(disk, &md);
823 error = g_raid3_write_metadata(disk, &md);
824 if (error == 0) {
825 G_RAID3_DEBUG(2, "Metadata on %s updated.",
826 g_raid3_get_diskname(disk));
827 } else {
828 G_RAID3_DEBUG(0,
829 "Cannot update metadata on disk %s (error=%d).",
830 g_raid3_get_diskname(disk), error);
831 }
832 }
833
834 static void
g_raid3_bump_syncid(struct g_raid3_softc * sc)835 g_raid3_bump_syncid(struct g_raid3_softc *sc)
836 {
837 struct g_raid3_disk *disk;
838 u_int n;
839
840 g_topology_assert_not();
841 sx_assert(&sc->sc_lock, SX_XLOCKED);
842 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
843 ("%s called with no active disks (device=%s).", __func__,
844 sc->sc_name));
845
846 sc->sc_syncid++;
847 G_RAID3_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
848 sc->sc_syncid);
849 for (n = 0; n < sc->sc_ndisks; n++) {
850 disk = &sc->sc_disks[n];
851 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
852 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
853 disk->d_sync.ds_syncid = sc->sc_syncid;
854 g_raid3_update_metadata(disk);
855 }
856 }
857 }
858
859 static void
g_raid3_bump_genid(struct g_raid3_softc * sc)860 g_raid3_bump_genid(struct g_raid3_softc *sc)
861 {
862 struct g_raid3_disk *disk;
863 u_int n;
864
865 g_topology_assert_not();
866 sx_assert(&sc->sc_lock, SX_XLOCKED);
867 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
868 ("%s called with no active disks (device=%s).", __func__,
869 sc->sc_name));
870
871 sc->sc_genid++;
872 G_RAID3_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
873 sc->sc_genid);
874 for (n = 0; n < sc->sc_ndisks; n++) {
875 disk = &sc->sc_disks[n];
876 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
877 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
878 disk->d_genid = sc->sc_genid;
879 g_raid3_update_metadata(disk);
880 }
881 }
882 }
883
884 static int
g_raid3_idle(struct g_raid3_softc * sc,int acw)885 g_raid3_idle(struct g_raid3_softc *sc, int acw)
886 {
887 struct g_raid3_disk *disk;
888 u_int i;
889 int timeout;
890
891 g_topology_assert_not();
892 sx_assert(&sc->sc_lock, SX_XLOCKED);
893
894 if (sc->sc_provider == NULL)
895 return (0);
896 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
897 return (0);
898 if (sc->sc_idle)
899 return (0);
900 if (sc->sc_writes > 0)
901 return (0);
902 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
903 timeout = g_raid3_idletime - (time_uptime - sc->sc_last_write);
904 if (!g_raid3_shutdown && timeout > 0)
905 return (timeout);
906 }
907 sc->sc_idle = 1;
908 for (i = 0; i < sc->sc_ndisks; i++) {
909 disk = &sc->sc_disks[i];
910 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
911 continue;
912 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
913 g_raid3_get_diskname(disk), sc->sc_name);
914 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
915 g_raid3_update_metadata(disk);
916 }
917 return (0);
918 }
919
920 static void
g_raid3_unidle(struct g_raid3_softc * sc)921 g_raid3_unidle(struct g_raid3_softc *sc)
922 {
923 struct g_raid3_disk *disk;
924 u_int i;
925
926 g_topology_assert_not();
927 sx_assert(&sc->sc_lock, SX_XLOCKED);
928
929 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
930 return;
931 sc->sc_idle = 0;
932 sc->sc_last_write = time_uptime;
933 for (i = 0; i < sc->sc_ndisks; i++) {
934 disk = &sc->sc_disks[i];
935 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
936 continue;
937 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
938 g_raid3_get_diskname(disk), sc->sc_name);
939 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
940 g_raid3_update_metadata(disk);
941 }
942 }
943
944 /*
945 * Treat bio_driver1 field in parent bio as list head and field bio_caller1
946 * in child bio as pointer to the next element on the list.
947 */
948 #define G_RAID3_HEAD_BIO(pbp) (pbp)->bio_driver1
949
950 #define G_RAID3_NEXT_BIO(cbp) (cbp)->bio_caller1
951
952 #define G_RAID3_FOREACH_BIO(pbp, bp) \
953 for ((bp) = G_RAID3_HEAD_BIO(pbp); (bp) != NULL; \
954 (bp) = G_RAID3_NEXT_BIO(bp))
955
956 #define G_RAID3_FOREACH_SAFE_BIO(pbp, bp, tmpbp) \
957 for ((bp) = G_RAID3_HEAD_BIO(pbp); \
958 (bp) != NULL && ((tmpbp) = G_RAID3_NEXT_BIO(bp), 1); \
959 (bp) = (tmpbp))
960
961 static void
g_raid3_init_bio(struct bio * pbp)962 g_raid3_init_bio(struct bio *pbp)
963 {
964
965 G_RAID3_HEAD_BIO(pbp) = NULL;
966 }
967
968 static void
g_raid3_remove_bio(struct bio * cbp)969 g_raid3_remove_bio(struct bio *cbp)
970 {
971 struct bio *pbp, *bp;
972
973 pbp = cbp->bio_parent;
974 if (G_RAID3_HEAD_BIO(pbp) == cbp)
975 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
976 else {
977 G_RAID3_FOREACH_BIO(pbp, bp) {
978 if (G_RAID3_NEXT_BIO(bp) == cbp) {
979 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
980 break;
981 }
982 }
983 }
984 G_RAID3_NEXT_BIO(cbp) = NULL;
985 }
986
987 static void
g_raid3_replace_bio(struct bio * sbp,struct bio * dbp)988 g_raid3_replace_bio(struct bio *sbp, struct bio *dbp)
989 {
990 struct bio *pbp, *bp;
991
992 g_raid3_remove_bio(sbp);
993 pbp = dbp->bio_parent;
994 G_RAID3_NEXT_BIO(sbp) = G_RAID3_NEXT_BIO(dbp);
995 if (G_RAID3_HEAD_BIO(pbp) == dbp)
996 G_RAID3_HEAD_BIO(pbp) = sbp;
997 else {
998 G_RAID3_FOREACH_BIO(pbp, bp) {
999 if (G_RAID3_NEXT_BIO(bp) == dbp) {
1000 G_RAID3_NEXT_BIO(bp) = sbp;
1001 break;
1002 }
1003 }
1004 }
1005 G_RAID3_NEXT_BIO(dbp) = NULL;
1006 }
1007
1008 static void
g_raid3_destroy_bio(struct g_raid3_softc * sc,struct bio * cbp)1009 g_raid3_destroy_bio(struct g_raid3_softc *sc, struct bio *cbp)
1010 {
1011 struct bio *bp, *pbp;
1012 size_t size;
1013
1014 pbp = cbp->bio_parent;
1015 pbp->bio_children--;
1016 KASSERT(cbp->bio_data != NULL, ("NULL bio_data"));
1017 size = pbp->bio_length / (sc->sc_ndisks - 1);
1018 g_raid3_free(sc, cbp->bio_data, size);
1019 if (G_RAID3_HEAD_BIO(pbp) == cbp) {
1020 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
1021 G_RAID3_NEXT_BIO(cbp) = NULL;
1022 g_destroy_bio(cbp);
1023 } else {
1024 G_RAID3_FOREACH_BIO(pbp, bp) {
1025 if (G_RAID3_NEXT_BIO(bp) == cbp)
1026 break;
1027 }
1028 if (bp != NULL) {
1029 KASSERT(G_RAID3_NEXT_BIO(bp) != NULL,
1030 ("NULL bp->bio_driver1"));
1031 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
1032 G_RAID3_NEXT_BIO(cbp) = NULL;
1033 }
1034 g_destroy_bio(cbp);
1035 }
1036 }
1037
1038 static struct bio *
g_raid3_clone_bio(struct g_raid3_softc * sc,struct bio * pbp)1039 g_raid3_clone_bio(struct g_raid3_softc *sc, struct bio *pbp)
1040 {
1041 struct bio *bp, *cbp;
1042 size_t size;
1043 int memflag;
1044
1045 cbp = g_clone_bio(pbp);
1046 if (cbp == NULL)
1047 return (NULL);
1048 size = pbp->bio_length / (sc->sc_ndisks - 1);
1049 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
1050 memflag = M_WAITOK;
1051 else
1052 memflag = M_NOWAIT;
1053 cbp->bio_data = g_raid3_alloc(sc, size, memflag);
1054 if (cbp->bio_data == NULL) {
1055 pbp->bio_children--;
1056 g_destroy_bio(cbp);
1057 return (NULL);
1058 }
1059 G_RAID3_NEXT_BIO(cbp) = NULL;
1060 if (G_RAID3_HEAD_BIO(pbp) == NULL)
1061 G_RAID3_HEAD_BIO(pbp) = cbp;
1062 else {
1063 G_RAID3_FOREACH_BIO(pbp, bp) {
1064 if (G_RAID3_NEXT_BIO(bp) == NULL) {
1065 G_RAID3_NEXT_BIO(bp) = cbp;
1066 break;
1067 }
1068 }
1069 }
1070 return (cbp);
1071 }
1072
1073 static void
g_raid3_scatter(struct bio * pbp)1074 g_raid3_scatter(struct bio *pbp)
1075 {
1076 struct g_raid3_softc *sc;
1077 struct g_raid3_disk *disk;
1078 struct bio *bp, *cbp, *tmpbp;
1079 off_t atom, cadd, padd, left;
1080 int first;
1081
1082 sc = pbp->bio_to->private;
1083 bp = NULL;
1084 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1085 /*
1086 * Find bio for which we should calculate data.
1087 */
1088 G_RAID3_FOREACH_BIO(pbp, cbp) {
1089 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1090 bp = cbp;
1091 break;
1092 }
1093 }
1094 KASSERT(bp != NULL, ("NULL parity bio."));
1095 }
1096 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1097 cadd = padd = 0;
1098 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1099 G_RAID3_FOREACH_BIO(pbp, cbp) {
1100 if (cbp == bp)
1101 continue;
1102 bcopy(pbp->bio_data + padd, cbp->bio_data + cadd, atom);
1103 padd += atom;
1104 }
1105 cadd += atom;
1106 }
1107 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1108 /*
1109 * Calculate parity.
1110 */
1111 first = 1;
1112 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1113 if (cbp == bp)
1114 continue;
1115 if (first) {
1116 bcopy(cbp->bio_data, bp->bio_data,
1117 bp->bio_length);
1118 first = 0;
1119 } else {
1120 g_raid3_xor(cbp->bio_data, bp->bio_data,
1121 bp->bio_length);
1122 }
1123 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_NODISK) != 0)
1124 g_raid3_destroy_bio(sc, cbp);
1125 }
1126 }
1127 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1128 struct g_consumer *cp;
1129
1130 disk = cbp->bio_caller2;
1131 cp = disk->d_consumer;
1132 cbp->bio_to = cp->provider;
1133 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1134 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1135 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1136 cp->acr, cp->acw, cp->ace));
1137 cp->index++;
1138 sc->sc_writes++;
1139 g_io_request(cbp, cp);
1140 }
1141 }
1142
1143 static void
g_raid3_gather(struct bio * pbp)1144 g_raid3_gather(struct bio *pbp)
1145 {
1146 struct g_raid3_softc *sc;
1147 struct g_raid3_disk *disk;
1148 struct bio *xbp, *fbp, *cbp;
1149 off_t atom, cadd, padd, left;
1150
1151 sc = pbp->bio_to->private;
1152 /*
1153 * Find bio for which we have to calculate data.
1154 * While going through this path, check if all requests
1155 * succeeded, if not, deny whole request.
1156 * If we're in COMPLETE mode, we allow one request to fail,
1157 * so if we find one, we're sending it to the parity consumer.
1158 * If there are more failed requests, we deny whole request.
1159 */
1160 xbp = fbp = NULL;
1161 G_RAID3_FOREACH_BIO(pbp, cbp) {
1162 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1163 KASSERT(xbp == NULL, ("More than one parity bio."));
1164 xbp = cbp;
1165 }
1166 if (cbp->bio_error == 0)
1167 continue;
1168 /*
1169 * Found failed request.
1170 */
1171 if (fbp == NULL) {
1172 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_DEGRADED) != 0) {
1173 /*
1174 * We are already in degraded mode, so we can't
1175 * accept any failures.
1176 */
1177 if (pbp->bio_error == 0)
1178 pbp->bio_error = cbp->bio_error;
1179 } else {
1180 fbp = cbp;
1181 }
1182 } else {
1183 /*
1184 * Next failed request, that's too many.
1185 */
1186 if (pbp->bio_error == 0)
1187 pbp->bio_error = fbp->bio_error;
1188 }
1189 disk = cbp->bio_caller2;
1190 if (disk == NULL)
1191 continue;
1192 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1193 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1194 G_RAID3_LOGREQ(0, cbp, "Request failed (error=%d).",
1195 cbp->bio_error);
1196 } else {
1197 G_RAID3_LOGREQ(1, cbp, "Request failed (error=%d).",
1198 cbp->bio_error);
1199 }
1200 if (g_raid3_disconnect_on_failure &&
1201 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1202 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1203 g_raid3_event_send(disk,
1204 G_RAID3_DISK_STATE_DISCONNECTED,
1205 G_RAID3_EVENT_DONTWAIT);
1206 }
1207 }
1208 if (pbp->bio_error != 0)
1209 goto finish;
1210 if (fbp != NULL && (pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1211 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_VERIFY;
1212 if (xbp != fbp)
1213 g_raid3_replace_bio(xbp, fbp);
1214 g_raid3_destroy_bio(sc, fbp);
1215 } else if (fbp != NULL) {
1216 struct g_consumer *cp;
1217
1218 /*
1219 * One request failed, so send the same request to
1220 * the parity consumer.
1221 */
1222 disk = pbp->bio_driver2;
1223 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1224 pbp->bio_error = fbp->bio_error;
1225 goto finish;
1226 }
1227 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1228 pbp->bio_inbed--;
1229 fbp->bio_flags &= ~(BIO_DONE | BIO_ERROR);
1230 if (disk->d_no == sc->sc_ndisks - 1)
1231 fbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1232 fbp->bio_error = 0;
1233 fbp->bio_completed = 0;
1234 fbp->bio_children = 0;
1235 fbp->bio_inbed = 0;
1236 cp = disk->d_consumer;
1237 fbp->bio_caller2 = disk;
1238 fbp->bio_to = cp->provider;
1239 G_RAID3_LOGREQ(3, fbp, "Sending request (recover).");
1240 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1241 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1242 cp->acr, cp->acw, cp->ace));
1243 cp->index++;
1244 g_io_request(fbp, cp);
1245 return;
1246 }
1247 if (xbp != NULL) {
1248 /*
1249 * Calculate parity.
1250 */
1251 G_RAID3_FOREACH_BIO(pbp, cbp) {
1252 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0)
1253 continue;
1254 g_raid3_xor(cbp->bio_data, xbp->bio_data,
1255 xbp->bio_length);
1256 }
1257 xbp->bio_cflags &= ~G_RAID3_BIO_CFLAG_PARITY;
1258 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1259 if (!g_raid3_is_zero(xbp)) {
1260 g_raid3_parity_mismatch++;
1261 pbp->bio_error = EIO;
1262 goto finish;
1263 }
1264 g_raid3_destroy_bio(sc, xbp);
1265 }
1266 }
1267 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1268 cadd = padd = 0;
1269 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1270 G_RAID3_FOREACH_BIO(pbp, cbp) {
1271 bcopy(cbp->bio_data + cadd, pbp->bio_data + padd, atom);
1272 pbp->bio_completed += atom;
1273 padd += atom;
1274 }
1275 cadd += atom;
1276 }
1277 finish:
1278 if (pbp->bio_error == 0)
1279 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1280 else {
1281 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0)
1282 G_RAID3_LOGREQ(1, pbp, "Verification error.");
1283 else
1284 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1285 }
1286 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_MASK;
1287 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1288 g_raid3_destroy_bio(sc, cbp);
1289 g_io_deliver(pbp, pbp->bio_error);
1290 }
1291
1292 static void
g_raid3_done(struct bio * bp)1293 g_raid3_done(struct bio *bp)
1294 {
1295 struct g_raid3_softc *sc;
1296
1297 sc = bp->bio_from->geom->softc;
1298 bp->bio_cflags |= G_RAID3_BIO_CFLAG_REGULAR;
1299 G_RAID3_LOGREQ(3, bp, "Regular request done (error=%d).", bp->bio_error);
1300 mtx_lock(&sc->sc_queue_mtx);
1301 bioq_insert_head(&sc->sc_queue, bp);
1302 mtx_unlock(&sc->sc_queue_mtx);
1303 wakeup(sc);
1304 wakeup(&sc->sc_queue);
1305 }
1306
1307 static void
g_raid3_regular_request(struct bio * cbp)1308 g_raid3_regular_request(struct bio *cbp)
1309 {
1310 struct g_raid3_softc *sc;
1311 struct g_raid3_disk *disk;
1312 struct bio *pbp;
1313
1314 g_topology_assert_not();
1315
1316 pbp = cbp->bio_parent;
1317 sc = pbp->bio_to->private;
1318 cbp->bio_from->index--;
1319 if (cbp->bio_cmd == BIO_WRITE)
1320 sc->sc_writes--;
1321 disk = cbp->bio_from->private;
1322 if (disk == NULL) {
1323 g_topology_lock();
1324 g_raid3_kill_consumer(sc, cbp->bio_from);
1325 g_topology_unlock();
1326 }
1327
1328 G_RAID3_LOGREQ(3, cbp, "Request finished.");
1329 pbp->bio_inbed++;
1330 KASSERT(pbp->bio_inbed <= pbp->bio_children,
1331 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1332 pbp->bio_children));
1333 if (pbp->bio_inbed != pbp->bio_children)
1334 return;
1335 switch (pbp->bio_cmd) {
1336 case BIO_READ:
1337 g_raid3_gather(pbp);
1338 break;
1339 case BIO_WRITE:
1340 case BIO_DELETE:
1341 {
1342 int error = 0;
1343
1344 pbp->bio_completed = pbp->bio_length;
1345 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL) {
1346 if (cbp->bio_error == 0) {
1347 g_raid3_destroy_bio(sc, cbp);
1348 continue;
1349 }
1350
1351 if (error == 0)
1352 error = cbp->bio_error;
1353 else if (pbp->bio_error == 0) {
1354 /*
1355 * Next failed request, that's too many.
1356 */
1357 pbp->bio_error = error;
1358 }
1359
1360 disk = cbp->bio_caller2;
1361 if (disk == NULL) {
1362 g_raid3_destroy_bio(sc, cbp);
1363 continue;
1364 }
1365
1366 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1367 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1368 G_RAID3_LOGREQ(0, cbp,
1369 "Request failed (error=%d).",
1370 cbp->bio_error);
1371 } else {
1372 G_RAID3_LOGREQ(1, cbp,
1373 "Request failed (error=%d).",
1374 cbp->bio_error);
1375 }
1376 if (g_raid3_disconnect_on_failure &&
1377 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1378 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1379 g_raid3_event_send(disk,
1380 G_RAID3_DISK_STATE_DISCONNECTED,
1381 G_RAID3_EVENT_DONTWAIT);
1382 }
1383 g_raid3_destroy_bio(sc, cbp);
1384 }
1385 if (pbp->bio_error == 0)
1386 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1387 else
1388 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1389 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_DEGRADED;
1390 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_NOPARITY;
1391 bioq_remove(&sc->sc_inflight, pbp);
1392 /* Release delayed sync requests if possible. */
1393 g_raid3_sync_release(sc);
1394 g_io_deliver(pbp, pbp->bio_error);
1395 break;
1396 }
1397 }
1398 }
1399
1400 static void
g_raid3_sync_done(struct bio * bp)1401 g_raid3_sync_done(struct bio *bp)
1402 {
1403 struct g_raid3_softc *sc;
1404
1405 G_RAID3_LOGREQ(3, bp, "Synchronization request delivered.");
1406 sc = bp->bio_from->geom->softc;
1407 bp->bio_cflags |= G_RAID3_BIO_CFLAG_SYNC;
1408 mtx_lock(&sc->sc_queue_mtx);
1409 bioq_insert_head(&sc->sc_queue, bp);
1410 mtx_unlock(&sc->sc_queue_mtx);
1411 wakeup(sc);
1412 wakeup(&sc->sc_queue);
1413 }
1414
1415 static void
g_raid3_flush(struct g_raid3_softc * sc,struct bio * bp)1416 g_raid3_flush(struct g_raid3_softc *sc, struct bio *bp)
1417 {
1418 struct bio_queue_head queue;
1419 struct g_raid3_disk *disk;
1420 struct g_consumer *cp __diagused;
1421 struct bio *cbp;
1422 u_int i;
1423
1424 bioq_init(&queue);
1425 for (i = 0; i < sc->sc_ndisks; i++) {
1426 disk = &sc->sc_disks[i];
1427 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
1428 continue;
1429 cbp = g_clone_bio(bp);
1430 if (cbp == NULL) {
1431 for (cbp = bioq_first(&queue); cbp != NULL;
1432 cbp = bioq_first(&queue)) {
1433 bioq_remove(&queue, cbp);
1434 g_destroy_bio(cbp);
1435 }
1436 if (bp->bio_error == 0)
1437 bp->bio_error = ENOMEM;
1438 g_io_deliver(bp, bp->bio_error);
1439 return;
1440 }
1441 bioq_insert_tail(&queue, cbp);
1442 cbp->bio_done = g_std_done;
1443 cbp->bio_caller1 = disk;
1444 cbp->bio_to = disk->d_consumer->provider;
1445 }
1446 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1447 bioq_remove(&queue, cbp);
1448 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1449 disk = cbp->bio_caller1;
1450 cbp->bio_caller1 = NULL;
1451 cp = disk->d_consumer;
1452 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1453 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1454 cp->acr, cp->acw, cp->ace));
1455 g_io_request(cbp, disk->d_consumer);
1456 }
1457 }
1458
1459 static void
g_raid3_rotation_rate(struct g_raid3_softc * sc,struct bio * bp)1460 g_raid3_rotation_rate(struct g_raid3_softc *sc, struct bio *bp)
1461 {
1462 struct g_raid3_disk *disk;
1463 bool first = true;
1464 uint16_t rr = DISK_RR_UNKNOWN;
1465 u_int n;
1466
1467 for (n = 0; n < sc->sc_ndisks; n++) {
1468 disk = &sc->sc_disks[n];
1469 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
1470 continue;
1471 if (first)
1472 rr = disk->d_rotation_rate;
1473 else if (rr != disk->d_rotation_rate) {
1474 rr = DISK_RR_UNKNOWN;
1475 break;
1476 }
1477 first = false;
1478 }
1479 g_handleattr(bp, "GEOM::rotation_rate", &rr, sizeof(rr));
1480 }
1481
1482 static void
g_raid3_start(struct bio * bp)1483 g_raid3_start(struct bio *bp)
1484 {
1485 struct g_raid3_softc *sc;
1486
1487 sc = bp->bio_to->private;
1488 /*
1489 * If sc == NULL or there are no valid disks, provider's error
1490 * should be set and g_raid3_start() should not be called at all.
1491 */
1492 KASSERT(sc != NULL && (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
1493 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE),
1494 ("Provider's error should be set (error=%d)(device=%s).",
1495 bp->bio_to->error, bp->bio_to->name));
1496 G_RAID3_LOGREQ(3, bp, "Request received.");
1497
1498 switch (bp->bio_cmd) {
1499 case BIO_READ:
1500 case BIO_WRITE:
1501 case BIO_DELETE:
1502 break;
1503 case BIO_SPEEDUP:
1504 case BIO_FLUSH:
1505 g_raid3_flush(sc, bp);
1506 return;
1507 case BIO_GETATTR:
1508 if (!strcmp(bp->bio_attribute, "GEOM::rotation_rate")) {
1509 g_raid3_rotation_rate(sc, bp);
1510 return;
1511 }
1512 /* FALLTHROUGH */
1513 default:
1514 g_io_deliver(bp, EOPNOTSUPP);
1515 return;
1516 }
1517 mtx_lock(&sc->sc_queue_mtx);
1518 bioq_insert_tail(&sc->sc_queue, bp);
1519 mtx_unlock(&sc->sc_queue_mtx);
1520 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1521 wakeup(sc);
1522 }
1523
1524 /*
1525 * Return TRUE if the given request is colliding with a in-progress
1526 * synchronization request.
1527 */
1528 static int
g_raid3_sync_collision(struct g_raid3_softc * sc,struct bio * bp)1529 g_raid3_sync_collision(struct g_raid3_softc *sc, struct bio *bp)
1530 {
1531 struct g_raid3_disk *disk;
1532 struct bio *sbp;
1533 off_t rstart, rend, sstart, send;
1534 int i;
1535
1536 disk = sc->sc_syncdisk;
1537 if (disk == NULL)
1538 return (0);
1539 rstart = bp->bio_offset;
1540 rend = bp->bio_offset + bp->bio_length;
1541 for (i = 0; i < g_raid3_syncreqs; i++) {
1542 sbp = disk->d_sync.ds_bios[i];
1543 if (sbp == NULL)
1544 continue;
1545 sstart = sbp->bio_offset;
1546 send = sbp->bio_length;
1547 if (sbp->bio_cmd == BIO_WRITE) {
1548 sstart *= sc->sc_ndisks - 1;
1549 send *= sc->sc_ndisks - 1;
1550 }
1551 send += sstart;
1552 if (rend > sstart && rstart < send)
1553 return (1);
1554 }
1555 return (0);
1556 }
1557
1558 /*
1559 * Return TRUE if the given sync request is colliding with a in-progress regular
1560 * request.
1561 */
1562 static int
g_raid3_regular_collision(struct g_raid3_softc * sc,struct bio * sbp)1563 g_raid3_regular_collision(struct g_raid3_softc *sc, struct bio *sbp)
1564 {
1565 off_t rstart, rend, sstart, send;
1566 struct bio *bp;
1567
1568 if (sc->sc_syncdisk == NULL)
1569 return (0);
1570 sstart = sbp->bio_offset;
1571 send = sstart + sbp->bio_length;
1572 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1573 rstart = bp->bio_offset;
1574 rend = bp->bio_offset + bp->bio_length;
1575 if (rend > sstart && rstart < send)
1576 return (1);
1577 }
1578 return (0);
1579 }
1580
1581 /*
1582 * Puts request onto delayed queue.
1583 */
1584 static void
g_raid3_regular_delay(struct g_raid3_softc * sc,struct bio * bp)1585 g_raid3_regular_delay(struct g_raid3_softc *sc, struct bio *bp)
1586 {
1587
1588 G_RAID3_LOGREQ(2, bp, "Delaying request.");
1589 bioq_insert_head(&sc->sc_regular_delayed, bp);
1590 }
1591
1592 /*
1593 * Puts synchronization request onto delayed queue.
1594 */
1595 static void
g_raid3_sync_delay(struct g_raid3_softc * sc,struct bio * bp)1596 g_raid3_sync_delay(struct g_raid3_softc *sc, struct bio *bp)
1597 {
1598
1599 G_RAID3_LOGREQ(2, bp, "Delaying synchronization request.");
1600 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1601 }
1602
1603 /*
1604 * Releases delayed regular requests which don't collide anymore with sync
1605 * requests.
1606 */
1607 static void
g_raid3_regular_release(struct g_raid3_softc * sc)1608 g_raid3_regular_release(struct g_raid3_softc *sc)
1609 {
1610 struct bio *bp, *bp2;
1611
1612 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1613 if (g_raid3_sync_collision(sc, bp))
1614 continue;
1615 bioq_remove(&sc->sc_regular_delayed, bp);
1616 G_RAID3_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1617 mtx_lock(&sc->sc_queue_mtx);
1618 bioq_insert_head(&sc->sc_queue, bp);
1619 #if 0
1620 /*
1621 * wakeup() is not needed, because this function is called from
1622 * the worker thread.
1623 */
1624 wakeup(&sc->sc_queue);
1625 #endif
1626 mtx_unlock(&sc->sc_queue_mtx);
1627 }
1628 }
1629
1630 /*
1631 * Releases delayed sync requests which don't collide anymore with regular
1632 * requests.
1633 */
1634 static void
g_raid3_sync_release(struct g_raid3_softc * sc)1635 g_raid3_sync_release(struct g_raid3_softc *sc)
1636 {
1637 struct bio *bp, *bp2;
1638
1639 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1640 if (g_raid3_regular_collision(sc, bp))
1641 continue;
1642 bioq_remove(&sc->sc_sync_delayed, bp);
1643 G_RAID3_LOGREQ(2, bp,
1644 "Releasing delayed synchronization request.");
1645 g_io_request(bp, bp->bio_from);
1646 }
1647 }
1648
1649 /*
1650 * Handle synchronization requests.
1651 * Every synchronization request is two-steps process: first, READ request is
1652 * send to active provider and then WRITE request (with read data) to the provider
1653 * being synchronized. When WRITE is finished, new synchronization request is
1654 * send.
1655 */
1656 static void
g_raid3_sync_request(struct bio * bp)1657 g_raid3_sync_request(struct bio *bp)
1658 {
1659 struct g_raid3_softc *sc;
1660 struct g_raid3_disk *disk;
1661
1662 bp->bio_from->index--;
1663 sc = bp->bio_from->geom->softc;
1664 disk = bp->bio_from->private;
1665 if (disk == NULL) {
1666 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1667 g_topology_lock();
1668 g_raid3_kill_consumer(sc, bp->bio_from);
1669 g_topology_unlock();
1670 free(bp->bio_data, M_RAID3);
1671 g_destroy_bio(bp);
1672 sx_xlock(&sc->sc_lock);
1673 return;
1674 }
1675
1676 /*
1677 * Synchronization request.
1678 */
1679 switch (bp->bio_cmd) {
1680 case BIO_READ:
1681 {
1682 struct g_consumer *cp;
1683 u_char *dst, *src;
1684 off_t left;
1685 u_int atom;
1686
1687 if (bp->bio_error != 0) {
1688 G_RAID3_LOGREQ(0, bp,
1689 "Synchronization request failed (error=%d).",
1690 bp->bio_error);
1691 g_destroy_bio(bp);
1692 return;
1693 }
1694 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1695 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1696 dst = src = bp->bio_data;
1697 if (disk->d_no == sc->sc_ndisks - 1) {
1698 u_int n;
1699
1700 /* Parity component. */
1701 for (left = bp->bio_length; left > 0;
1702 left -= sc->sc_sectorsize) {
1703 bcopy(src, dst, atom);
1704 src += atom;
1705 for (n = 1; n < sc->sc_ndisks - 1; n++) {
1706 g_raid3_xor(src, dst, atom);
1707 src += atom;
1708 }
1709 dst += atom;
1710 }
1711 } else {
1712 /* Regular component. */
1713 src += atom * disk->d_no;
1714 for (left = bp->bio_length; left > 0;
1715 left -= sc->sc_sectorsize) {
1716 bcopy(src, dst, atom);
1717 src += sc->sc_sectorsize;
1718 dst += atom;
1719 }
1720 }
1721 bp->bio_driver1 = bp->bio_driver2 = NULL;
1722 bp->bio_pflags = 0;
1723 bp->bio_offset /= sc->sc_ndisks - 1;
1724 bp->bio_length /= sc->sc_ndisks - 1;
1725 bp->bio_cmd = BIO_WRITE;
1726 bp->bio_cflags = 0;
1727 bp->bio_children = bp->bio_inbed = 0;
1728 cp = disk->d_consumer;
1729 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1730 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1731 cp->acr, cp->acw, cp->ace));
1732 cp->index++;
1733 g_io_request(bp, cp);
1734 return;
1735 }
1736 case BIO_WRITE:
1737 {
1738 struct g_raid3_disk_sync *sync;
1739 off_t boffset, moffset;
1740 void *data;
1741 int i;
1742
1743 if (bp->bio_error != 0) {
1744 G_RAID3_LOGREQ(0, bp,
1745 "Synchronization request failed (error=%d).",
1746 bp->bio_error);
1747 g_destroy_bio(bp);
1748 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1749 g_raid3_event_send(disk,
1750 G_RAID3_DISK_STATE_DISCONNECTED,
1751 G_RAID3_EVENT_DONTWAIT);
1752 return;
1753 }
1754 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1755 sync = &disk->d_sync;
1756 if (sync->ds_offset == sc->sc_mediasize / (sc->sc_ndisks - 1) ||
1757 sync->ds_consumer == NULL ||
1758 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1759 /* Don't send more synchronization requests. */
1760 sync->ds_inflight--;
1761 if (sync->ds_bios != NULL) {
1762 i = (int)(uintptr_t)bp->bio_caller1;
1763 sync->ds_bios[i] = NULL;
1764 }
1765 free(bp->bio_data, M_RAID3);
1766 g_destroy_bio(bp);
1767 if (sync->ds_inflight > 0)
1768 return;
1769 if (sync->ds_consumer == NULL ||
1770 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1771 return;
1772 }
1773 /*
1774 * Disk up-to-date, activate it.
1775 */
1776 g_raid3_event_send(disk, G_RAID3_DISK_STATE_ACTIVE,
1777 G_RAID3_EVENT_DONTWAIT);
1778 return;
1779 }
1780
1781 /* Send next synchronization request. */
1782 data = bp->bio_data;
1783 g_reset_bio(bp);
1784 bp->bio_cmd = BIO_READ;
1785 bp->bio_offset = sync->ds_offset * (sc->sc_ndisks - 1);
1786 bp->bio_length = MIN(maxphys, sc->sc_mediasize - bp->bio_offset);
1787 sync->ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
1788 bp->bio_done = g_raid3_sync_done;
1789 bp->bio_data = data;
1790 bp->bio_from = sync->ds_consumer;
1791 bp->bio_to = sc->sc_provider;
1792 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
1793 sync->ds_consumer->index++;
1794 /*
1795 * Delay the request if it is colliding with a regular request.
1796 */
1797 if (g_raid3_regular_collision(sc, bp))
1798 g_raid3_sync_delay(sc, bp);
1799 else
1800 g_io_request(bp, sync->ds_consumer);
1801
1802 /* Release delayed requests if possible. */
1803 g_raid3_regular_release(sc);
1804
1805 /* Find the smallest offset. */
1806 moffset = sc->sc_mediasize;
1807 for (i = 0; i < g_raid3_syncreqs; i++) {
1808 bp = sync->ds_bios[i];
1809 boffset = bp->bio_offset;
1810 if (bp->bio_cmd == BIO_WRITE)
1811 boffset *= sc->sc_ndisks - 1;
1812 if (boffset < moffset)
1813 moffset = boffset;
1814 }
1815 if (sync->ds_offset_done + maxphys * 100 < moffset) {
1816 /* Update offset_done on every 100 blocks. */
1817 sync->ds_offset_done = moffset;
1818 g_raid3_update_metadata(disk);
1819 }
1820 return;
1821 }
1822 default:
1823 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1824 bp->bio_cmd, sc->sc_name));
1825 break;
1826 }
1827 }
1828
1829 static int
g_raid3_register_request(struct bio * pbp)1830 g_raid3_register_request(struct bio *pbp)
1831 {
1832 struct g_raid3_softc *sc;
1833 struct g_raid3_disk *disk;
1834 struct g_consumer *cp;
1835 struct bio *cbp, *tmpbp;
1836 off_t offset, length;
1837 u_int n, ndisks;
1838 int round_robin, verify;
1839
1840 ndisks = 0;
1841 sc = pbp->bio_to->private;
1842 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGSYNC) != 0 &&
1843 sc->sc_syncdisk == NULL) {
1844 g_io_deliver(pbp, EIO);
1845 return (0);
1846 }
1847 g_raid3_init_bio(pbp);
1848 length = pbp->bio_length / (sc->sc_ndisks - 1);
1849 offset = pbp->bio_offset / (sc->sc_ndisks - 1);
1850 round_robin = verify = 0;
1851 switch (pbp->bio_cmd) {
1852 case BIO_READ:
1853 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
1854 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1855 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_VERIFY;
1856 verify = 1;
1857 ndisks = sc->sc_ndisks;
1858 } else {
1859 verify = 0;
1860 ndisks = sc->sc_ndisks - 1;
1861 }
1862 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0 &&
1863 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1864 round_robin = 1;
1865 } else {
1866 round_robin = 0;
1867 }
1868 KASSERT(!round_robin || !verify,
1869 ("ROUND-ROBIN and VERIFY are mutually exclusive."));
1870 pbp->bio_driver2 = &sc->sc_disks[sc->sc_ndisks - 1];
1871 break;
1872 case BIO_WRITE:
1873 case BIO_DELETE:
1874 /*
1875 * Delay the request if it is colliding with a synchronization
1876 * request.
1877 */
1878 if (g_raid3_sync_collision(sc, pbp)) {
1879 g_raid3_regular_delay(sc, pbp);
1880 return (0);
1881 }
1882
1883 if (sc->sc_idle)
1884 g_raid3_unidle(sc);
1885 else
1886 sc->sc_last_write = time_uptime;
1887
1888 ndisks = sc->sc_ndisks;
1889 break;
1890 }
1891 for (n = 0; n < ndisks; n++) {
1892 disk = &sc->sc_disks[n];
1893 cbp = g_raid3_clone_bio(sc, pbp);
1894 if (cbp == NULL) {
1895 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1896 g_raid3_destroy_bio(sc, cbp);
1897 /*
1898 * To prevent deadlock, we must run back up
1899 * with the ENOMEM for failed requests of any
1900 * of our consumers. Our own sync requests
1901 * can stick around, as they are finite.
1902 */
1903 if ((pbp->bio_cflags &
1904 G_RAID3_BIO_CFLAG_REGULAR) != 0) {
1905 g_io_deliver(pbp, ENOMEM);
1906 return (0);
1907 }
1908 return (ENOMEM);
1909 }
1910 cbp->bio_offset = offset;
1911 cbp->bio_length = length;
1912 cbp->bio_done = g_raid3_done;
1913 switch (pbp->bio_cmd) {
1914 case BIO_READ:
1915 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1916 /*
1917 * Replace invalid component with the parity
1918 * component.
1919 */
1920 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1921 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1922 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1923 } else if (round_robin &&
1924 disk->d_no == sc->sc_round_robin) {
1925 /*
1926 * In round-robin mode skip one data component
1927 * and use parity component when reading.
1928 */
1929 pbp->bio_driver2 = disk;
1930 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1931 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1932 sc->sc_round_robin++;
1933 round_robin = 0;
1934 } else if (verify && disk->d_no == sc->sc_ndisks - 1) {
1935 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1936 }
1937 break;
1938 case BIO_WRITE:
1939 case BIO_DELETE:
1940 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
1941 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
1942 if (n == ndisks - 1) {
1943 /*
1944 * Active parity component, mark it as such.
1945 */
1946 cbp->bio_cflags |=
1947 G_RAID3_BIO_CFLAG_PARITY;
1948 }
1949 } else {
1950 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1951 if (n == ndisks - 1) {
1952 /*
1953 * Parity component is not connected,
1954 * so destroy its request.
1955 */
1956 pbp->bio_pflags |=
1957 G_RAID3_BIO_PFLAG_NOPARITY;
1958 g_raid3_destroy_bio(sc, cbp);
1959 cbp = NULL;
1960 } else {
1961 cbp->bio_cflags |=
1962 G_RAID3_BIO_CFLAG_NODISK;
1963 disk = NULL;
1964 }
1965 }
1966 break;
1967 }
1968 if (cbp != NULL)
1969 cbp->bio_caller2 = disk;
1970 }
1971 switch (pbp->bio_cmd) {
1972 case BIO_READ:
1973 if (round_robin) {
1974 /*
1975 * If we are in round-robin mode and 'round_robin' is
1976 * still 1, it means, that we skipped parity component
1977 * for this read and must reset sc_round_robin field.
1978 */
1979 sc->sc_round_robin = 0;
1980 }
1981 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1982 disk = cbp->bio_caller2;
1983 cp = disk->d_consumer;
1984 cbp->bio_to = cp->provider;
1985 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1986 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1987 ("Consumer %s not opened (r%dw%de%d).",
1988 cp->provider->name, cp->acr, cp->acw, cp->ace));
1989 cp->index++;
1990 g_io_request(cbp, cp);
1991 }
1992 break;
1993 case BIO_WRITE:
1994 case BIO_DELETE:
1995 /*
1996 * Put request onto inflight queue, so we can check if new
1997 * synchronization requests don't collide with it.
1998 */
1999 bioq_insert_tail(&sc->sc_inflight, pbp);
2000
2001 /*
2002 * Bump syncid on first write.
2003 */
2004 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0) {
2005 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2006 g_raid3_bump_syncid(sc);
2007 }
2008 g_raid3_scatter(pbp);
2009 break;
2010 }
2011 return (0);
2012 }
2013
2014 static int
g_raid3_can_destroy(struct g_raid3_softc * sc)2015 g_raid3_can_destroy(struct g_raid3_softc *sc)
2016 {
2017 struct g_geom *gp;
2018 struct g_consumer *cp;
2019
2020 g_topology_assert();
2021 gp = sc->sc_geom;
2022 if (gp->softc == NULL)
2023 return (1);
2024 LIST_FOREACH(cp, &gp->consumer, consumer) {
2025 if (g_raid3_is_busy(sc, cp))
2026 return (0);
2027 }
2028 gp = sc->sc_sync.ds_geom;
2029 LIST_FOREACH(cp, &gp->consumer, consumer) {
2030 if (g_raid3_is_busy(sc, cp))
2031 return (0);
2032 }
2033 G_RAID3_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
2034 sc->sc_name);
2035 return (1);
2036 }
2037
2038 static int
g_raid3_try_destroy(struct g_raid3_softc * sc)2039 g_raid3_try_destroy(struct g_raid3_softc *sc)
2040 {
2041
2042 g_topology_assert_not();
2043 sx_assert(&sc->sc_lock, SX_XLOCKED);
2044
2045 if (sc->sc_rootmount != NULL) {
2046 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2047 sc->sc_rootmount);
2048 root_mount_rel(sc->sc_rootmount);
2049 sc->sc_rootmount = NULL;
2050 }
2051
2052 g_topology_lock();
2053 if (!g_raid3_can_destroy(sc)) {
2054 g_topology_unlock();
2055 return (0);
2056 }
2057 sc->sc_geom->softc = NULL;
2058 sc->sc_sync.ds_geom->softc = NULL;
2059 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_WAIT) != 0) {
2060 g_topology_unlock();
2061 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2062 &sc->sc_worker);
2063 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
2064 sx_xunlock(&sc->sc_lock);
2065 wakeup(&sc->sc_worker);
2066 sc->sc_worker = NULL;
2067 } else {
2068 g_topology_unlock();
2069 g_raid3_destroy_device(sc);
2070 }
2071 return (1);
2072 }
2073
2074 /*
2075 * Worker thread.
2076 */
2077 static void
g_raid3_worker(void * arg)2078 g_raid3_worker(void *arg)
2079 {
2080 struct g_raid3_softc *sc;
2081 struct g_raid3_event *ep;
2082 struct bio *bp;
2083 int timeout;
2084
2085 sc = arg;
2086 thread_lock(curthread);
2087 sched_prio(curthread, PRIBIO);
2088 thread_unlock(curthread);
2089
2090 sx_xlock(&sc->sc_lock);
2091 for (;;) {
2092 G_RAID3_DEBUG(5, "%s: Let's see...", __func__);
2093 /*
2094 * First take a look at events.
2095 * This is important to handle events before any I/O requests.
2096 */
2097 ep = g_raid3_event_get(sc);
2098 if (ep != NULL) {
2099 g_raid3_event_remove(sc, ep);
2100 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0) {
2101 /* Update only device status. */
2102 G_RAID3_DEBUG(3,
2103 "Running event for device %s.",
2104 sc->sc_name);
2105 ep->e_error = 0;
2106 g_raid3_update_device(sc, 1);
2107 } else {
2108 /* Update disk status. */
2109 G_RAID3_DEBUG(3, "Running event for disk %s.",
2110 g_raid3_get_diskname(ep->e_disk));
2111 ep->e_error = g_raid3_update_disk(ep->e_disk,
2112 ep->e_state);
2113 if (ep->e_error == 0)
2114 g_raid3_update_device(sc, 0);
2115 }
2116 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0) {
2117 KASSERT(ep->e_error == 0,
2118 ("Error cannot be handled."));
2119 g_raid3_event_free(ep);
2120 } else {
2121 ep->e_flags |= G_RAID3_EVENT_DONE;
2122 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2123 ep);
2124 mtx_lock(&sc->sc_events_mtx);
2125 wakeup(ep);
2126 mtx_unlock(&sc->sc_events_mtx);
2127 }
2128 if ((sc->sc_flags &
2129 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2130 if (g_raid3_try_destroy(sc)) {
2131 curthread->td_pflags &= ~TDP_GEOM;
2132 G_RAID3_DEBUG(1, "Thread exiting.");
2133 kproc_exit(0);
2134 }
2135 }
2136 G_RAID3_DEBUG(5, "%s: I'm here 1.", __func__);
2137 continue;
2138 }
2139 /*
2140 * Check if we can mark array as CLEAN and if we can't take
2141 * how much seconds should we wait.
2142 */
2143 timeout = g_raid3_idle(sc, -1);
2144 /*
2145 * Now I/O requests.
2146 */
2147 /* Get first request from the queue. */
2148 mtx_lock(&sc->sc_queue_mtx);
2149 bp = bioq_first(&sc->sc_queue);
2150 if (bp == NULL) {
2151 if ((sc->sc_flags &
2152 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2153 mtx_unlock(&sc->sc_queue_mtx);
2154 if (g_raid3_try_destroy(sc)) {
2155 curthread->td_pflags &= ~TDP_GEOM;
2156 G_RAID3_DEBUG(1, "Thread exiting.");
2157 kproc_exit(0);
2158 }
2159 mtx_lock(&sc->sc_queue_mtx);
2160 }
2161 sx_xunlock(&sc->sc_lock);
2162 /*
2163 * XXX: We can miss an event here, because an event
2164 * can be added without sx-device-lock and without
2165 * mtx-queue-lock. Maybe I should just stop using
2166 * dedicated mutex for events synchronization and
2167 * stick with the queue lock?
2168 * The event will hang here until next I/O request
2169 * or next event is received.
2170 */
2171 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "r3:w1",
2172 timeout * hz);
2173 sx_xlock(&sc->sc_lock);
2174 G_RAID3_DEBUG(5, "%s: I'm here 4.", __func__);
2175 continue;
2176 }
2177 process:
2178 bioq_remove(&sc->sc_queue, bp);
2179 mtx_unlock(&sc->sc_queue_mtx);
2180
2181 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2182 (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0) {
2183 g_raid3_sync_request(bp); /* READ */
2184 } else if (bp->bio_to != sc->sc_provider) {
2185 if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
2186 g_raid3_regular_request(bp);
2187 else if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0)
2188 g_raid3_sync_request(bp); /* WRITE */
2189 else {
2190 KASSERT(0,
2191 ("Invalid request cflags=0x%hx to=%s.",
2192 bp->bio_cflags, bp->bio_to->name));
2193 }
2194 } else if (g_raid3_register_request(bp) != 0) {
2195 mtx_lock(&sc->sc_queue_mtx);
2196 bioq_insert_head(&sc->sc_queue, bp);
2197 /*
2198 * We are short in memory, let see if there are finished
2199 * request we can free.
2200 */
2201 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2202 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR)
2203 goto process;
2204 }
2205 /*
2206 * No finished regular request, so at least keep
2207 * synchronization running.
2208 */
2209 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2210 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC)
2211 goto process;
2212 }
2213 sx_xunlock(&sc->sc_lock);
2214 MSLEEP(&sc->sc_queue, &sc->sc_queue_mtx, PRIBIO | PDROP,
2215 "r3:lowmem", hz / 10);
2216 sx_xlock(&sc->sc_lock);
2217 }
2218 G_RAID3_DEBUG(5, "%s: I'm here 9.", __func__);
2219 }
2220 }
2221
2222 static void
g_raid3_update_idle(struct g_raid3_softc * sc,struct g_raid3_disk * disk)2223 g_raid3_update_idle(struct g_raid3_softc *sc, struct g_raid3_disk *disk)
2224 {
2225
2226 sx_assert(&sc->sc_lock, SX_LOCKED);
2227 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
2228 return;
2229 if (!sc->sc_idle && (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) == 0) {
2230 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
2231 g_raid3_get_diskname(disk), sc->sc_name);
2232 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2233 } else if (sc->sc_idle &&
2234 (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0) {
2235 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
2236 g_raid3_get_diskname(disk), sc->sc_name);
2237 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2238 }
2239 }
2240
2241 static void
g_raid3_sync_start(struct g_raid3_softc * sc)2242 g_raid3_sync_start(struct g_raid3_softc *sc)
2243 {
2244 struct g_raid3_disk *disk;
2245 struct g_consumer *cp;
2246 struct bio *bp;
2247 int error __diagused;
2248 u_int n;
2249
2250 g_topology_assert_not();
2251 sx_assert(&sc->sc_lock, SX_XLOCKED);
2252
2253 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2254 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2255 sc->sc_state));
2256 KASSERT(sc->sc_syncdisk == NULL, ("Syncdisk is not NULL (%s, %u).",
2257 sc->sc_name, sc->sc_state));
2258 disk = NULL;
2259 for (n = 0; n < sc->sc_ndisks; n++) {
2260 if (sc->sc_disks[n].d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
2261 continue;
2262 disk = &sc->sc_disks[n];
2263 break;
2264 }
2265 if (disk == NULL)
2266 return;
2267
2268 sx_xunlock(&sc->sc_lock);
2269 g_topology_lock();
2270 cp = g_new_consumer(sc->sc_sync.ds_geom);
2271 error = g_attach(cp, sc->sc_provider);
2272 KASSERT(error == 0,
2273 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2274 error = g_access(cp, 1, 0, 0);
2275 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2276 g_topology_unlock();
2277 sx_xlock(&sc->sc_lock);
2278
2279 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2280 g_raid3_get_diskname(disk));
2281 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) == 0)
2282 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2283 KASSERT(disk->d_sync.ds_consumer == NULL,
2284 ("Sync consumer already exists (device=%s, disk=%s).",
2285 sc->sc_name, g_raid3_get_diskname(disk)));
2286
2287 disk->d_sync.ds_consumer = cp;
2288 disk->d_sync.ds_consumer->private = disk;
2289 disk->d_sync.ds_consumer->index = 0;
2290 sc->sc_syncdisk = disk;
2291
2292 /*
2293 * Allocate memory for synchronization bios and initialize them.
2294 */
2295 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_raid3_syncreqs,
2296 M_RAID3, M_WAITOK);
2297 for (n = 0; n < g_raid3_syncreqs; n++) {
2298 bp = g_alloc_bio();
2299 disk->d_sync.ds_bios[n] = bp;
2300 bp->bio_parent = NULL;
2301 bp->bio_cmd = BIO_READ;
2302 bp->bio_data = malloc(maxphys, M_RAID3, M_WAITOK);
2303 bp->bio_cflags = 0;
2304 bp->bio_offset = disk->d_sync.ds_offset * (sc->sc_ndisks - 1);
2305 bp->bio_length = MIN(maxphys, sc->sc_mediasize - bp->bio_offset);
2306 disk->d_sync.ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
2307 bp->bio_done = g_raid3_sync_done;
2308 bp->bio_from = disk->d_sync.ds_consumer;
2309 bp->bio_to = sc->sc_provider;
2310 bp->bio_caller1 = (void *)(uintptr_t)n;
2311 }
2312
2313 /* Set the number of in-flight synchronization requests. */
2314 disk->d_sync.ds_inflight = g_raid3_syncreqs;
2315
2316 /*
2317 * Fire off first synchronization requests.
2318 */
2319 for (n = 0; n < g_raid3_syncreqs; n++) {
2320 bp = disk->d_sync.ds_bios[n];
2321 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
2322 disk->d_sync.ds_consumer->index++;
2323 /*
2324 * Delay the request if it is colliding with a regular request.
2325 */
2326 if (g_raid3_regular_collision(sc, bp))
2327 g_raid3_sync_delay(sc, bp);
2328 else
2329 g_io_request(bp, disk->d_sync.ds_consumer);
2330 }
2331 }
2332
2333 /*
2334 * Stop synchronization process.
2335 * type: 0 - synchronization finished
2336 * 1 - synchronization stopped
2337 */
2338 static void
g_raid3_sync_stop(struct g_raid3_softc * sc,int type)2339 g_raid3_sync_stop(struct g_raid3_softc *sc, int type)
2340 {
2341 struct g_raid3_disk *disk;
2342 struct g_consumer *cp;
2343
2344 g_topology_assert_not();
2345 sx_assert(&sc->sc_lock, SX_LOCKED);
2346
2347 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2348 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2349 sc->sc_state));
2350 disk = sc->sc_syncdisk;
2351 sc->sc_syncdisk = NULL;
2352 KASSERT(disk != NULL, ("No disk was synchronized (%s).", sc->sc_name));
2353 KASSERT(disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2354 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2355 g_raid3_disk_state2str(disk->d_state)));
2356 if (disk->d_sync.ds_consumer == NULL)
2357 return;
2358
2359 if (type == 0) {
2360 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2361 sc->sc_name, g_raid3_get_diskname(disk));
2362 } else /* if (type == 1) */ {
2363 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2364 sc->sc_name, g_raid3_get_diskname(disk));
2365 }
2366 free(disk->d_sync.ds_bios, M_RAID3);
2367 disk->d_sync.ds_bios = NULL;
2368 cp = disk->d_sync.ds_consumer;
2369 disk->d_sync.ds_consumer = NULL;
2370 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2371 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2372 g_topology_lock();
2373 g_raid3_kill_consumer(sc, cp);
2374 g_topology_unlock();
2375 sx_xlock(&sc->sc_lock);
2376 }
2377
2378 static void
g_raid3_launch_provider(struct g_raid3_softc * sc)2379 g_raid3_launch_provider(struct g_raid3_softc *sc)
2380 {
2381 struct g_provider *pp;
2382 struct g_raid3_disk *disk;
2383 int n;
2384
2385 sx_assert(&sc->sc_lock, SX_LOCKED);
2386
2387 g_topology_lock();
2388 pp = g_new_providerf(sc->sc_geom, "raid3/%s", sc->sc_name);
2389 pp->mediasize = sc->sc_mediasize;
2390 pp->sectorsize = sc->sc_sectorsize;
2391 pp->stripesize = 0;
2392 pp->stripeoffset = 0;
2393 for (n = 0; n < sc->sc_ndisks; n++) {
2394 disk = &sc->sc_disks[n];
2395 if (disk->d_consumer && disk->d_consumer->provider &&
2396 disk->d_consumer->provider->stripesize > pp->stripesize) {
2397 pp->stripesize = disk->d_consumer->provider->stripesize;
2398 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2399 }
2400 }
2401 pp->stripesize *= sc->sc_ndisks - 1;
2402 pp->stripeoffset *= sc->sc_ndisks - 1;
2403 pp->private = sc;
2404 sc->sc_refcnt++;
2405 sc->sc_provider = pp;
2406 g_error_provider(pp, 0);
2407 g_topology_unlock();
2408 G_RAID3_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2409 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE), sc->sc_ndisks);
2410
2411 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED)
2412 g_raid3_sync_start(sc);
2413 }
2414
2415 static void
g_raid3_destroy_provider(struct g_raid3_softc * sc)2416 g_raid3_destroy_provider(struct g_raid3_softc *sc)
2417 {
2418 struct bio *bp;
2419
2420 g_topology_assert_not();
2421 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2422 sc->sc_name));
2423
2424 g_topology_lock();
2425 g_error_provider(sc->sc_provider, ENXIO);
2426 mtx_lock(&sc->sc_queue_mtx);
2427 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2428 bioq_remove(&sc->sc_queue, bp);
2429 g_io_deliver(bp, ENXIO);
2430 }
2431 mtx_unlock(&sc->sc_queue_mtx);
2432 G_RAID3_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2433 sc->sc_provider->name);
2434 g_wither_provider(sc->sc_provider, ENXIO);
2435 g_topology_unlock();
2436 sc->sc_provider = NULL;
2437 if (sc->sc_syncdisk != NULL)
2438 g_raid3_sync_stop(sc, 1);
2439 }
2440
2441 static void
g_raid3_go(void * arg)2442 g_raid3_go(void *arg)
2443 {
2444 struct g_raid3_softc *sc;
2445 struct g_raid3_event *ep;
2446
2447 sc = arg;
2448 G_RAID3_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2449 ep = sc->sc_timeout_event;
2450 sc->sc_timeout_event = NULL;
2451 g_raid3_event_dispatch(ep, sc, 0,
2452 G_RAID3_EVENT_DONTWAIT | G_RAID3_EVENT_DEVICE);
2453 }
2454
2455 static void
g_raid3_timeout_drain(struct g_raid3_softc * sc)2456 g_raid3_timeout_drain(struct g_raid3_softc *sc)
2457 {
2458 sx_assert(&sc->sc_lock, SX_XLOCKED);
2459
2460 callout_drain(&sc->sc_callout);
2461 g_raid3_event_free(sc->sc_timeout_event);
2462 sc->sc_timeout_event = NULL;
2463 }
2464
2465 static u_int
g_raid3_determine_state(struct g_raid3_disk * disk)2466 g_raid3_determine_state(struct g_raid3_disk *disk)
2467 {
2468 struct g_raid3_softc *sc;
2469 u_int state;
2470
2471 sc = disk->d_softc;
2472 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2473 if ((disk->d_flags &
2474 G_RAID3_DISK_FLAG_SYNCHRONIZING) == 0) {
2475 /* Disk does not need synchronization. */
2476 state = G_RAID3_DISK_STATE_ACTIVE;
2477 } else {
2478 if ((sc->sc_flags &
2479 G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2480 (disk->d_flags &
2481 G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2482 /*
2483 * We can start synchronization from
2484 * the stored offset.
2485 */
2486 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2487 } else {
2488 state = G_RAID3_DISK_STATE_STALE;
2489 }
2490 }
2491 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2492 /*
2493 * Reset all synchronization data for this disk,
2494 * because if it even was synchronized, it was
2495 * synchronized to disks with different syncid.
2496 */
2497 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2498 disk->d_sync.ds_offset = 0;
2499 disk->d_sync.ds_offset_done = 0;
2500 disk->d_sync.ds_syncid = sc->sc_syncid;
2501 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2502 (disk->d_flags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2503 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2504 } else {
2505 state = G_RAID3_DISK_STATE_STALE;
2506 }
2507 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2508 /*
2509 * Not good, NOT GOOD!
2510 * It means that device was started on stale disks
2511 * and more fresh disk just arrive.
2512 * If there were writes, device is broken, sorry.
2513 * I think the best choice here is don't touch
2514 * this disk and inform the user loudly.
2515 */
2516 G_RAID3_DEBUG(0, "Device %s was started before the freshest "
2517 "disk (%s) arrives!! It will not be connected to the "
2518 "running device.", sc->sc_name,
2519 g_raid3_get_diskname(disk));
2520 g_raid3_destroy_disk(disk);
2521 state = G_RAID3_DISK_STATE_NONE;
2522 /* Return immediately, because disk was destroyed. */
2523 return (state);
2524 }
2525 G_RAID3_DEBUG(3, "State for %s disk: %s.",
2526 g_raid3_get_diskname(disk), g_raid3_disk_state2str(state));
2527 return (state);
2528 }
2529
2530 /*
2531 * Update device state.
2532 */
2533 static void
g_raid3_update_device(struct g_raid3_softc * sc,boolean_t force)2534 g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force)
2535 {
2536 struct g_raid3_disk *disk;
2537 u_int state;
2538
2539 sx_assert(&sc->sc_lock, SX_XLOCKED);
2540
2541 switch (sc->sc_state) {
2542 case G_RAID3_DEVICE_STATE_STARTING:
2543 {
2544 u_int n, ndirty, ndisks, genid, syncid;
2545
2546 KASSERT(sc->sc_provider == NULL,
2547 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2548 /*
2549 * Are we ready? We are, if all disks are connected or
2550 * one disk is missing and 'force' is true.
2551 */
2552 if (g_raid3_ndisks(sc, -1) + force == sc->sc_ndisks) {
2553 if (!force)
2554 g_raid3_timeout_drain(sc);
2555 } else {
2556 if (force) {
2557 /*
2558 * Timeout expired, so destroy device.
2559 */
2560 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2561 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p",
2562 __LINE__, sc->sc_rootmount);
2563 root_mount_rel(sc->sc_rootmount);
2564 sc->sc_rootmount = NULL;
2565 }
2566 return;
2567 }
2568
2569 /*
2570 * Find the biggest genid.
2571 */
2572 genid = 0;
2573 for (n = 0; n < sc->sc_ndisks; n++) {
2574 disk = &sc->sc_disks[n];
2575 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2576 continue;
2577 if (disk->d_genid > genid)
2578 genid = disk->d_genid;
2579 }
2580 sc->sc_genid = genid;
2581 /*
2582 * Remove all disks without the biggest genid.
2583 */
2584 for (n = 0; n < sc->sc_ndisks; n++) {
2585 disk = &sc->sc_disks[n];
2586 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2587 continue;
2588 if (disk->d_genid < genid) {
2589 G_RAID3_DEBUG(0,
2590 "Component %s (device %s) broken, skipping.",
2591 g_raid3_get_diskname(disk), sc->sc_name);
2592 g_raid3_destroy_disk(disk);
2593 }
2594 }
2595
2596 /*
2597 * There must be at least 'sc->sc_ndisks - 1' components
2598 * with the same syncid and without SYNCHRONIZING flag.
2599 */
2600
2601 /*
2602 * Find the biggest syncid, number of valid components and
2603 * number of dirty components.
2604 */
2605 ndirty = ndisks = syncid = 0;
2606 for (n = 0; n < sc->sc_ndisks; n++) {
2607 disk = &sc->sc_disks[n];
2608 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2609 continue;
2610 if ((disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0)
2611 ndirty++;
2612 if (disk->d_sync.ds_syncid > syncid) {
2613 syncid = disk->d_sync.ds_syncid;
2614 ndisks = 0;
2615 } else if (disk->d_sync.ds_syncid < syncid) {
2616 continue;
2617 }
2618 if ((disk->d_flags &
2619 G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) {
2620 continue;
2621 }
2622 ndisks++;
2623 }
2624 /*
2625 * Do we have enough valid components?
2626 */
2627 if (ndisks + 1 < sc->sc_ndisks) {
2628 G_RAID3_DEBUG(0,
2629 "Device %s is broken, too few valid components.",
2630 sc->sc_name);
2631 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2632 return;
2633 }
2634 /*
2635 * If there is one DIRTY component and all disks are present,
2636 * mark it for synchronization. If there is more than one DIRTY
2637 * component, mark parity component for synchronization.
2638 */
2639 if (ndisks == sc->sc_ndisks && ndirty == 1) {
2640 for (n = 0; n < sc->sc_ndisks; n++) {
2641 disk = &sc->sc_disks[n];
2642 if ((disk->d_flags &
2643 G_RAID3_DISK_FLAG_DIRTY) == 0) {
2644 continue;
2645 }
2646 disk->d_flags |=
2647 G_RAID3_DISK_FLAG_SYNCHRONIZING;
2648 }
2649 } else if (ndisks == sc->sc_ndisks && ndirty > 1) {
2650 disk = &sc->sc_disks[sc->sc_ndisks - 1];
2651 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2652 }
2653
2654 sc->sc_syncid = syncid;
2655 if (force) {
2656 /* Remember to bump syncid on first write. */
2657 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2658 }
2659 if (ndisks == sc->sc_ndisks)
2660 state = G_RAID3_DEVICE_STATE_COMPLETE;
2661 else /* if (ndisks == sc->sc_ndisks - 1) */
2662 state = G_RAID3_DEVICE_STATE_DEGRADED;
2663 G_RAID3_DEBUG(1, "Device %s state changed from %s to %s.",
2664 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2665 g_raid3_device_state2str(state));
2666 sc->sc_state = state;
2667 for (n = 0; n < sc->sc_ndisks; n++) {
2668 disk = &sc->sc_disks[n];
2669 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2670 continue;
2671 state = g_raid3_determine_state(disk);
2672 g_raid3_event_send(disk, state, G_RAID3_EVENT_DONTWAIT);
2673 if (state == G_RAID3_DISK_STATE_STALE)
2674 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2675 }
2676 break;
2677 }
2678 case G_RAID3_DEVICE_STATE_DEGRADED:
2679 /*
2680 * Genid need to be bumped immediately, so do it here.
2681 */
2682 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2683 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2684 g_raid3_bump_genid(sc);
2685 }
2686
2687 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2688 return;
2689 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
2690 sc->sc_ndisks - 1) {
2691 if (sc->sc_provider != NULL)
2692 g_raid3_destroy_provider(sc);
2693 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2694 return;
2695 }
2696 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2697 sc->sc_ndisks) {
2698 state = G_RAID3_DEVICE_STATE_COMPLETE;
2699 G_RAID3_DEBUG(1,
2700 "Device %s state changed from %s to %s.",
2701 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2702 g_raid3_device_state2str(state));
2703 sc->sc_state = state;
2704 }
2705 if (sc->sc_provider == NULL)
2706 g_raid3_launch_provider(sc);
2707 if (sc->sc_rootmount != NULL) {
2708 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2709 sc->sc_rootmount);
2710 root_mount_rel(sc->sc_rootmount);
2711 sc->sc_rootmount = NULL;
2712 }
2713 break;
2714 case G_RAID3_DEVICE_STATE_COMPLETE:
2715 /*
2716 * Genid need to be bumped immediately, so do it here.
2717 */
2718 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2719 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2720 g_raid3_bump_genid(sc);
2721 }
2722
2723 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2724 return;
2725 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) >=
2726 sc->sc_ndisks - 1,
2727 ("Too few ACTIVE components in COMPLETE state (device %s).",
2728 sc->sc_name));
2729 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2730 sc->sc_ndisks - 1) {
2731 state = G_RAID3_DEVICE_STATE_DEGRADED;
2732 G_RAID3_DEBUG(1,
2733 "Device %s state changed from %s to %s.",
2734 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2735 g_raid3_device_state2str(state));
2736 sc->sc_state = state;
2737 }
2738 if (sc->sc_provider == NULL)
2739 g_raid3_launch_provider(sc);
2740 if (sc->sc_rootmount != NULL) {
2741 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2742 sc->sc_rootmount);
2743 root_mount_rel(sc->sc_rootmount);
2744 sc->sc_rootmount = NULL;
2745 }
2746 break;
2747 default:
2748 KASSERT(1 == 0, ("Wrong device state (%s, %s).", sc->sc_name,
2749 g_raid3_device_state2str(sc->sc_state)));
2750 break;
2751 }
2752 }
2753
2754 /*
2755 * Update disk state and device state if needed.
2756 */
2757 #define DISK_STATE_CHANGED() G_RAID3_DEBUG(1, \
2758 "Disk %s state changed from %s to %s (device %s).", \
2759 g_raid3_get_diskname(disk), \
2760 g_raid3_disk_state2str(disk->d_state), \
2761 g_raid3_disk_state2str(state), sc->sc_name)
2762 static int
g_raid3_update_disk(struct g_raid3_disk * disk,u_int state)2763 g_raid3_update_disk(struct g_raid3_disk *disk, u_int state)
2764 {
2765 struct g_raid3_softc *sc;
2766
2767 sc = disk->d_softc;
2768 sx_assert(&sc->sc_lock, SX_XLOCKED);
2769
2770 again:
2771 G_RAID3_DEBUG(3, "Changing disk %s state from %s to %s.",
2772 g_raid3_get_diskname(disk), g_raid3_disk_state2str(disk->d_state),
2773 g_raid3_disk_state2str(state));
2774 switch (state) {
2775 case G_RAID3_DISK_STATE_NEW:
2776 /*
2777 * Possible scenarios:
2778 * 1. New disk arrive.
2779 */
2780 /* Previous state should be NONE. */
2781 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NONE,
2782 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2783 g_raid3_disk_state2str(disk->d_state)));
2784 DISK_STATE_CHANGED();
2785
2786 disk->d_state = state;
2787 G_RAID3_DEBUG(1, "Device %s: provider %s detected.",
2788 sc->sc_name, g_raid3_get_diskname(disk));
2789 if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING)
2790 break;
2791 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2792 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2793 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2794 g_raid3_device_state2str(sc->sc_state),
2795 g_raid3_get_diskname(disk),
2796 g_raid3_disk_state2str(disk->d_state)));
2797 state = g_raid3_determine_state(disk);
2798 if (state != G_RAID3_DISK_STATE_NONE)
2799 goto again;
2800 break;
2801 case G_RAID3_DISK_STATE_ACTIVE:
2802 /*
2803 * Possible scenarios:
2804 * 1. New disk does not need synchronization.
2805 * 2. Synchronization process finished successfully.
2806 */
2807 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2808 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2809 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2810 g_raid3_device_state2str(sc->sc_state),
2811 g_raid3_get_diskname(disk),
2812 g_raid3_disk_state2str(disk->d_state)));
2813 /* Previous state should be NEW or SYNCHRONIZING. */
2814 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW ||
2815 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2816 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2817 g_raid3_disk_state2str(disk->d_state)));
2818 DISK_STATE_CHANGED();
2819
2820 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
2821 disk->d_flags &= ~G_RAID3_DISK_FLAG_SYNCHRONIZING;
2822 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
2823 g_raid3_sync_stop(sc, 0);
2824 }
2825 disk->d_state = state;
2826 disk->d_sync.ds_offset = 0;
2827 disk->d_sync.ds_offset_done = 0;
2828 g_raid3_update_idle(sc, disk);
2829 g_raid3_update_metadata(disk);
2830 G_RAID3_DEBUG(1, "Device %s: provider %s activated.",
2831 sc->sc_name, g_raid3_get_diskname(disk));
2832 break;
2833 case G_RAID3_DISK_STATE_STALE:
2834 /*
2835 * Possible scenarios:
2836 * 1. Stale disk was connected.
2837 */
2838 /* Previous state should be NEW. */
2839 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2840 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2841 g_raid3_disk_state2str(disk->d_state)));
2842 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2843 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2844 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2845 g_raid3_device_state2str(sc->sc_state),
2846 g_raid3_get_diskname(disk),
2847 g_raid3_disk_state2str(disk->d_state)));
2848 /*
2849 * STALE state is only possible if device is marked
2850 * NOAUTOSYNC.
2851 */
2852 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0,
2853 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2854 g_raid3_device_state2str(sc->sc_state),
2855 g_raid3_get_diskname(disk),
2856 g_raid3_disk_state2str(disk->d_state)));
2857 DISK_STATE_CHANGED();
2858
2859 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2860 disk->d_state = state;
2861 g_raid3_update_metadata(disk);
2862 G_RAID3_DEBUG(0, "Device %s: provider %s is stale.",
2863 sc->sc_name, g_raid3_get_diskname(disk));
2864 break;
2865 case G_RAID3_DISK_STATE_SYNCHRONIZING:
2866 /*
2867 * Possible scenarios:
2868 * 1. Disk which needs synchronization was connected.
2869 */
2870 /* Previous state should be NEW. */
2871 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2872 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2873 g_raid3_disk_state2str(disk->d_state)));
2874 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2875 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2876 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2877 g_raid3_device_state2str(sc->sc_state),
2878 g_raid3_get_diskname(disk),
2879 g_raid3_disk_state2str(disk->d_state)));
2880 DISK_STATE_CHANGED();
2881
2882 if (disk->d_state == G_RAID3_DISK_STATE_NEW)
2883 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2884 disk->d_state = state;
2885 if (sc->sc_provider != NULL) {
2886 g_raid3_sync_start(sc);
2887 g_raid3_update_metadata(disk);
2888 }
2889 break;
2890 case G_RAID3_DISK_STATE_DISCONNECTED:
2891 /*
2892 * Possible scenarios:
2893 * 1. Device wasn't running yet, but disk disappear.
2894 * 2. Disk was active and disapppear.
2895 * 3. Disk disappear during synchronization process.
2896 */
2897 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2898 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
2899 /*
2900 * Previous state should be ACTIVE, STALE or
2901 * SYNCHRONIZING.
2902 */
2903 KASSERT(disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
2904 disk->d_state == G_RAID3_DISK_STATE_STALE ||
2905 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2906 ("Wrong disk state (%s, %s).",
2907 g_raid3_get_diskname(disk),
2908 g_raid3_disk_state2str(disk->d_state)));
2909 } else if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING) {
2910 /* Previous state should be NEW. */
2911 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2912 ("Wrong disk state (%s, %s).",
2913 g_raid3_get_diskname(disk),
2914 g_raid3_disk_state2str(disk->d_state)));
2915 /*
2916 * Reset bumping syncid if disk disappeared in STARTING
2917 * state.
2918 */
2919 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0)
2920 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2921 #ifdef INVARIANTS
2922 } else {
2923 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2924 sc->sc_name,
2925 g_raid3_device_state2str(sc->sc_state),
2926 g_raid3_get_diskname(disk),
2927 g_raid3_disk_state2str(disk->d_state)));
2928 #endif
2929 }
2930 DISK_STATE_CHANGED();
2931 G_RAID3_DEBUG(0, "Device %s: provider %s disconnected.",
2932 sc->sc_name, g_raid3_get_diskname(disk));
2933
2934 g_raid3_destroy_disk(disk);
2935 break;
2936 default:
2937 KASSERT(1 == 0, ("Unknown state (%u).", state));
2938 break;
2939 }
2940 return (0);
2941 }
2942 #undef DISK_STATE_CHANGED
2943
2944 int
g_raid3_read_metadata(struct g_consumer * cp,struct g_raid3_metadata * md)2945 g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md)
2946 {
2947 struct g_provider *pp;
2948 u_char *buf;
2949 int error;
2950
2951 g_topology_assert();
2952
2953 error = g_access(cp, 1, 0, 0);
2954 if (error != 0)
2955 return (error);
2956 pp = cp->provider;
2957 g_topology_unlock();
2958 /* Metadata are stored on last sector. */
2959 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2960 &error);
2961 g_topology_lock();
2962 g_access(cp, -1, 0, 0);
2963 if (buf == NULL) {
2964 G_RAID3_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2965 cp->provider->name, error);
2966 return (error);
2967 }
2968
2969 /* Decode metadata. */
2970 error = raid3_metadata_decode(buf, md);
2971 g_free(buf);
2972 if (strcmp(md->md_magic, G_RAID3_MAGIC) != 0)
2973 return (EINVAL);
2974 if (md->md_version > G_RAID3_VERSION) {
2975 G_RAID3_DEBUG(0,
2976 "Kernel module is too old to handle metadata from %s.",
2977 cp->provider->name);
2978 return (EINVAL);
2979 }
2980 if (error != 0) {
2981 G_RAID3_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2982 cp->provider->name);
2983 return (error);
2984 }
2985 if (md->md_sectorsize > maxphys) {
2986 G_RAID3_DEBUG(0, "The blocksize is too big.");
2987 return (EINVAL);
2988 }
2989
2990 return (0);
2991 }
2992
2993 static int
g_raid3_check_metadata(struct g_raid3_softc * sc,struct g_provider * pp,struct g_raid3_metadata * md)2994 g_raid3_check_metadata(struct g_raid3_softc *sc, struct g_provider *pp,
2995 struct g_raid3_metadata *md)
2996 {
2997
2998 if (md->md_no >= sc->sc_ndisks) {
2999 G_RAID3_DEBUG(1, "Invalid disk %s number (no=%u), skipping.",
3000 pp->name, md->md_no);
3001 return (EINVAL);
3002 }
3003 if (sc->sc_disks[md->md_no].d_state != G_RAID3_DISK_STATE_NODISK) {
3004 G_RAID3_DEBUG(1, "Disk %s (no=%u) already exists, skipping.",
3005 pp->name, md->md_no);
3006 return (EEXIST);
3007 }
3008 if (md->md_all != sc->sc_ndisks) {
3009 G_RAID3_DEBUG(1,
3010 "Invalid '%s' field on disk %s (device %s), skipping.",
3011 "md_all", pp->name, sc->sc_name);
3012 return (EINVAL);
3013 }
3014 if ((md->md_mediasize % md->md_sectorsize) != 0) {
3015 G_RAID3_DEBUG(1, "Invalid metadata (mediasize %% sectorsize != "
3016 "0) on disk %s (device %s), skipping.", pp->name,
3017 sc->sc_name);
3018 return (EINVAL);
3019 }
3020 if (md->md_mediasize != sc->sc_mediasize) {
3021 G_RAID3_DEBUG(1,
3022 "Invalid '%s' field on disk %s (device %s), skipping.",
3023 "md_mediasize", pp->name, sc->sc_name);
3024 return (EINVAL);
3025 }
3026 if ((md->md_mediasize % (sc->sc_ndisks - 1)) != 0) {
3027 G_RAID3_DEBUG(1,
3028 "Invalid '%s' field on disk %s (device %s), skipping.",
3029 "md_mediasize", pp->name, sc->sc_name);
3030 return (EINVAL);
3031 }
3032 if ((sc->sc_mediasize / (sc->sc_ndisks - 1)) > pp->mediasize) {
3033 G_RAID3_DEBUG(1,
3034 "Invalid size of disk %s (device %s), skipping.", pp->name,
3035 sc->sc_name);
3036 return (EINVAL);
3037 }
3038 if ((md->md_sectorsize / pp->sectorsize) < sc->sc_ndisks - 1) {
3039 G_RAID3_DEBUG(1,
3040 "Invalid '%s' field on disk %s (device %s), skipping.",
3041 "md_sectorsize", pp->name, sc->sc_name);
3042 return (EINVAL);
3043 }
3044 if (md->md_sectorsize != sc->sc_sectorsize) {
3045 G_RAID3_DEBUG(1,
3046 "Invalid '%s' field on disk %s (device %s), skipping.",
3047 "md_sectorsize", pp->name, sc->sc_name);
3048 return (EINVAL);
3049 }
3050 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
3051 G_RAID3_DEBUG(1,
3052 "Invalid sector size of disk %s (device %s), skipping.",
3053 pp->name, sc->sc_name);
3054 return (EINVAL);
3055 }
3056 if ((md->md_mflags & ~G_RAID3_DEVICE_FLAG_MASK) != 0) {
3057 G_RAID3_DEBUG(1,
3058 "Invalid device flags on disk %s (device %s), skipping.",
3059 pp->name, sc->sc_name);
3060 return (EINVAL);
3061 }
3062 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
3063 (md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
3064 /*
3065 * VERIFY and ROUND-ROBIN options are mutally exclusive.
3066 */
3067 G_RAID3_DEBUG(1, "Both VERIFY and ROUND-ROBIN flags exist on "
3068 "disk %s (device %s), skipping.", pp->name, sc->sc_name);
3069 return (EINVAL);
3070 }
3071 if ((md->md_dflags & ~G_RAID3_DISK_FLAG_MASK) != 0) {
3072 G_RAID3_DEBUG(1,
3073 "Invalid disk flags on disk %s (device %s), skipping.",
3074 pp->name, sc->sc_name);
3075 return (EINVAL);
3076 }
3077 return (0);
3078 }
3079
3080 int
g_raid3_add_disk(struct g_raid3_softc * sc,struct g_provider * pp,struct g_raid3_metadata * md)3081 g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
3082 struct g_raid3_metadata *md)
3083 {
3084 struct g_raid3_disk *disk;
3085 int error;
3086
3087 g_topology_assert_not();
3088 G_RAID3_DEBUG(2, "Adding disk %s.", pp->name);
3089
3090 error = g_raid3_check_metadata(sc, pp, md);
3091 if (error != 0)
3092 return (error);
3093 if (sc->sc_state != G_RAID3_DEVICE_STATE_STARTING &&
3094 md->md_genid < sc->sc_genid) {
3095 G_RAID3_DEBUG(0, "Component %s (device %s) broken, skipping.",
3096 pp->name, sc->sc_name);
3097 return (EINVAL);
3098 }
3099 disk = g_raid3_init_disk(sc, pp, md, &error);
3100 if (disk == NULL)
3101 return (error);
3102 error = g_raid3_event_send(disk, G_RAID3_DISK_STATE_NEW,
3103 G_RAID3_EVENT_WAIT);
3104 if (error != 0)
3105 return (error);
3106 if (md->md_version < G_RAID3_VERSION) {
3107 G_RAID3_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3108 pp->name, md->md_version, G_RAID3_VERSION);
3109 g_raid3_update_metadata(disk);
3110 }
3111 return (0);
3112 }
3113
3114 static void
g_raid3_destroy_delayed(void * arg,int flag)3115 g_raid3_destroy_delayed(void *arg, int flag)
3116 {
3117 struct g_raid3_softc *sc;
3118 int error;
3119
3120 if (flag == EV_CANCEL) {
3121 G_RAID3_DEBUG(1, "Destroying canceled.");
3122 return;
3123 }
3124 sc = arg;
3125 g_topology_unlock();
3126 sx_xlock(&sc->sc_lock);
3127 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) == 0,
3128 ("DESTROY flag set on %s.", sc->sc_name));
3129 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0,
3130 ("DESTROYING flag not set on %s.", sc->sc_name));
3131 G_RAID3_DEBUG(0, "Destroying %s (delayed).", sc->sc_name);
3132 error = g_raid3_destroy(sc, G_RAID3_DESTROY_SOFT);
3133 if (error != 0) {
3134 G_RAID3_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
3135 sx_xunlock(&sc->sc_lock);
3136 }
3137 g_topology_lock();
3138 }
3139
3140 static int
g_raid3_access(struct g_provider * pp,int acr,int acw,int ace)3141 g_raid3_access(struct g_provider *pp, int acr, int acw, int ace)
3142 {
3143 struct g_raid3_softc *sc;
3144 int dcr, dcw, dce, error = 0;
3145
3146 g_topology_assert();
3147 G_RAID3_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3148 acw, ace);
3149
3150 sc = pp->private;
3151 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3152
3153 dcr = pp->acr + acr;
3154 dcw = pp->acw + acw;
3155 dce = pp->ace + ace;
3156
3157 g_topology_unlock();
3158 sx_xlock(&sc->sc_lock);
3159 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0 ||
3160 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks - 1) {
3161 if (acr > 0 || acw > 0 || ace > 0)
3162 error = ENXIO;
3163 goto end;
3164 }
3165 if (dcw == 0)
3166 g_raid3_idle(sc, dcw);
3167 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0) {
3168 if (acr > 0 || acw > 0 || ace > 0) {
3169 error = ENXIO;
3170 goto end;
3171 }
3172 if (dcr == 0 && dcw == 0 && dce == 0) {
3173 g_post_event(g_raid3_destroy_delayed, sc, M_WAITOK,
3174 sc, NULL);
3175 }
3176 }
3177 end:
3178 sx_xunlock(&sc->sc_lock);
3179 g_topology_lock();
3180 return (error);
3181 }
3182
3183 static struct g_geom *
g_raid3_create(struct g_class * mp,const struct g_raid3_metadata * md)3184 g_raid3_create(struct g_class *mp, const struct g_raid3_metadata *md)
3185 {
3186 struct g_raid3_softc *sc;
3187 struct g_geom *gp;
3188 int error, timeout;
3189 u_int n;
3190
3191 g_topology_assert();
3192 G_RAID3_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
3193
3194 /* One disk is minimum. */
3195 if (md->md_all < 1)
3196 return (NULL);
3197 /*
3198 * Action geom.
3199 */
3200 gp = g_new_geom(mp, md->md_name);
3201 sc = malloc(sizeof(*sc), M_RAID3, M_WAITOK | M_ZERO);
3202 sc->sc_disks = malloc(sizeof(struct g_raid3_disk) * md->md_all, M_RAID3,
3203 M_WAITOK | M_ZERO);
3204 gp->start = g_raid3_start;
3205 gp->orphan = g_raid3_orphan;
3206 gp->access = g_raid3_access;
3207 gp->dumpconf = g_raid3_dumpconf;
3208
3209 sc->sc_id = md->md_id;
3210 sc->sc_mediasize = md->md_mediasize;
3211 sc->sc_sectorsize = md->md_sectorsize;
3212 sc->sc_ndisks = md->md_all;
3213 sc->sc_round_robin = 0;
3214 sc->sc_flags = md->md_mflags;
3215 sc->sc_bump_id = 0;
3216 sc->sc_idle = 1;
3217 sc->sc_last_write = time_uptime;
3218 sc->sc_writes = 0;
3219 sc->sc_refcnt = 1;
3220 for (n = 0; n < sc->sc_ndisks; n++) {
3221 sc->sc_disks[n].d_softc = sc;
3222 sc->sc_disks[n].d_no = n;
3223 sc->sc_disks[n].d_state = G_RAID3_DISK_STATE_NODISK;
3224 }
3225 sx_init(&sc->sc_lock, "graid3:lock");
3226 bioq_init(&sc->sc_queue);
3227 mtx_init(&sc->sc_queue_mtx, "graid3:queue", NULL, MTX_DEF);
3228 bioq_init(&sc->sc_regular_delayed);
3229 bioq_init(&sc->sc_inflight);
3230 bioq_init(&sc->sc_sync_delayed);
3231 TAILQ_INIT(&sc->sc_events);
3232 mtx_init(&sc->sc_events_mtx, "graid3:events", NULL, MTX_DEF);
3233 callout_init(&sc->sc_callout, 1);
3234 sc->sc_state = G_RAID3_DEVICE_STATE_STARTING;
3235 gp->softc = sc;
3236 sc->sc_geom = gp;
3237 sc->sc_provider = NULL;
3238 /*
3239 * Synchronization geom.
3240 */
3241 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3242 gp->softc = sc;
3243 gp->orphan = g_raid3_orphan;
3244 sc->sc_sync.ds_geom = gp;
3245
3246 if (!g_raid3_use_malloc) {
3247 sc->sc_zones[G_RAID3_ZONE_64K].sz_zone = uma_zcreate("gr3:64k",
3248 65536, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3249 UMA_ALIGN_PTR, 0);
3250 sc->sc_zones[G_RAID3_ZONE_64K].sz_inuse = 0;
3251 sc->sc_zones[G_RAID3_ZONE_64K].sz_max = g_raid3_n64k;
3252 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested =
3253 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed = 0;
3254 sc->sc_zones[G_RAID3_ZONE_16K].sz_zone = uma_zcreate("gr3:16k",
3255 16384, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3256 UMA_ALIGN_PTR, 0);
3257 sc->sc_zones[G_RAID3_ZONE_16K].sz_inuse = 0;
3258 sc->sc_zones[G_RAID3_ZONE_16K].sz_max = g_raid3_n16k;
3259 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested =
3260 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed = 0;
3261 sc->sc_zones[G_RAID3_ZONE_4K].sz_zone = uma_zcreate("gr3:4k",
3262 4096, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3263 UMA_ALIGN_PTR, 0);
3264 sc->sc_zones[G_RAID3_ZONE_4K].sz_inuse = 0;
3265 sc->sc_zones[G_RAID3_ZONE_4K].sz_max = g_raid3_n4k;
3266 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested =
3267 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed = 0;
3268 }
3269
3270 error = kproc_create(g_raid3_worker, sc, &sc->sc_worker, 0, 0,
3271 "g_raid3 %s", md->md_name);
3272 if (error != 0) {
3273 G_RAID3_DEBUG(1, "Cannot create kernel thread for %s.",
3274 sc->sc_name);
3275 g_destroy_geom(sc->sc_geom);
3276 g_raid3_free_device(sc);
3277 return (NULL);
3278 }
3279
3280 G_RAID3_DEBUG(1, "Device %s created (%u components, id=%u).",
3281 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3282
3283 sc->sc_rootmount = root_mount_hold("GRAID3");
3284 G_RAID3_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3285
3286 /*
3287 * Schedule startup timeout.
3288 */
3289 timeout = atomic_load_acq_int(&g_raid3_timeout);
3290 sc->sc_timeout_event = malloc(sizeof(struct g_raid3_event), M_RAID3,
3291 M_WAITOK);
3292 callout_reset(&sc->sc_callout, timeout * hz, g_raid3_go, sc);
3293 return (sc->sc_geom);
3294 }
3295
3296 int
g_raid3_destroy(struct g_raid3_softc * sc,int how)3297 g_raid3_destroy(struct g_raid3_softc *sc, int how)
3298 {
3299 struct g_provider *pp;
3300
3301 g_topology_assert_not();
3302 sx_assert(&sc->sc_lock, SX_XLOCKED);
3303
3304 pp = sc->sc_provider;
3305 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
3306 switch (how) {
3307 case G_RAID3_DESTROY_SOFT:
3308 G_RAID3_DEBUG(1,
3309 "Device %s is still open (r%dw%de%d).", pp->name,
3310 pp->acr, pp->acw, pp->ace);
3311 return (EBUSY);
3312 case G_RAID3_DESTROY_DELAYED:
3313 G_RAID3_DEBUG(1,
3314 "Device %s will be destroyed on last close.",
3315 pp->name);
3316 if (sc->sc_syncdisk != NULL)
3317 g_raid3_sync_stop(sc, 1);
3318 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROYING;
3319 return (EBUSY);
3320 case G_RAID3_DESTROY_HARD:
3321 G_RAID3_DEBUG(1, "Device %s is still open, so it "
3322 "can't be definitely removed.", pp->name);
3323 break;
3324 }
3325 }
3326
3327 g_topology_lock();
3328 if (sc->sc_geom->softc == NULL) {
3329 g_topology_unlock();
3330 return (0);
3331 }
3332 sc->sc_geom->softc = NULL;
3333 sc->sc_sync.ds_geom->softc = NULL;
3334 g_topology_unlock();
3335
3336 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
3337 sc->sc_flags |= G_RAID3_DEVICE_FLAG_WAIT;
3338 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3339 sx_xunlock(&sc->sc_lock);
3340 mtx_lock(&sc->sc_queue_mtx);
3341 wakeup(sc);
3342 wakeup(&sc->sc_queue);
3343 mtx_unlock(&sc->sc_queue_mtx);
3344 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3345 while (sc->sc_worker != NULL)
3346 tsleep(&sc->sc_worker, PRIBIO, "r3:destroy", hz / 5);
3347 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3348 sx_xlock(&sc->sc_lock);
3349 g_raid3_destroy_device(sc);
3350 return (0);
3351 }
3352
3353 static void
g_raid3_taste_orphan(struct g_consumer * cp)3354 g_raid3_taste_orphan(struct g_consumer *cp)
3355 {
3356
3357 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3358 cp->provider->name));
3359 }
3360
3361 static struct g_geom *
g_raid3_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)3362 g_raid3_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3363 {
3364 struct g_raid3_metadata md;
3365 struct g_raid3_softc *sc;
3366 struct g_consumer *cp;
3367 struct g_geom *gp;
3368 int error;
3369
3370 g_topology_assert();
3371 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3372 G_RAID3_DEBUG(2, "Tasting %s.", pp->name);
3373
3374 gp = g_new_geom(mp, "raid3:taste");
3375 /* This orphan function should be never called. */
3376 gp->orphan = g_raid3_taste_orphan;
3377 cp = g_new_consumer(gp);
3378 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
3379 error = g_attach(cp, pp);
3380 if (error == 0) {
3381 error = g_raid3_read_metadata(cp, &md);
3382 g_detach(cp);
3383 }
3384 g_destroy_consumer(cp);
3385 g_destroy_geom(gp);
3386 if (error != 0)
3387 return (NULL);
3388 gp = NULL;
3389
3390 if (md.md_provider[0] != '\0' &&
3391 !g_compare_names(md.md_provider, pp->name))
3392 return (NULL);
3393 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3394 return (NULL);
3395 if (g_raid3_debug >= 2)
3396 raid3_metadata_dump(&md);
3397
3398 /*
3399 * Let's check if device already exists.
3400 */
3401 sc = NULL;
3402 LIST_FOREACH(gp, &mp->geom, geom) {
3403 sc = gp->softc;
3404 if (sc == NULL)
3405 continue;
3406 if (sc->sc_sync.ds_geom == gp)
3407 continue;
3408 if (strcmp(md.md_name, sc->sc_name) != 0)
3409 continue;
3410 if (md.md_id != sc->sc_id) {
3411 G_RAID3_DEBUG(0, "Device %s already configured.",
3412 sc->sc_name);
3413 return (NULL);
3414 }
3415 break;
3416 }
3417 if (gp == NULL) {
3418 gp = g_raid3_create(mp, &md);
3419 if (gp == NULL) {
3420 G_RAID3_DEBUG(0, "Cannot create device %s.",
3421 md.md_name);
3422 return (NULL);
3423 }
3424 sc = gp->softc;
3425 }
3426 G_RAID3_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3427 g_topology_unlock();
3428 sx_xlock(&sc->sc_lock);
3429 error = g_raid3_add_disk(sc, pp, &md);
3430 if (error != 0) {
3431 G_RAID3_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3432 pp->name, gp->name, error);
3433 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NODISK) ==
3434 sc->sc_ndisks) {
3435 g_cancel_event(sc);
3436 g_raid3_destroy(sc, G_RAID3_DESTROY_HARD);
3437 g_topology_lock();
3438 return (NULL);
3439 }
3440 gp = NULL;
3441 }
3442 sx_xunlock(&sc->sc_lock);
3443 g_topology_lock();
3444 return (gp);
3445 }
3446
3447 static int
g_raid3_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)3448 g_raid3_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
3449 struct g_geom *gp)
3450 {
3451 struct g_raid3_softc *sc;
3452 int error;
3453
3454 g_topology_unlock();
3455 sc = gp->softc;
3456 sx_xlock(&sc->sc_lock);
3457 g_cancel_event(sc);
3458 error = g_raid3_destroy(gp->softc, G_RAID3_DESTROY_SOFT);
3459 if (error != 0)
3460 sx_xunlock(&sc->sc_lock);
3461 g_topology_lock();
3462 return (error);
3463 }
3464
3465 static void
g_raid3_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)3466 g_raid3_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3467 struct g_consumer *cp, struct g_provider *pp)
3468 {
3469 struct g_raid3_softc *sc;
3470
3471 g_topology_assert();
3472
3473 sc = gp->softc;
3474 if (sc == NULL)
3475 return;
3476 /* Skip synchronization geom. */
3477 if (gp == sc->sc_sync.ds_geom)
3478 return;
3479 if (pp != NULL) {
3480 /* Nothing here. */
3481 } else if (cp != NULL) {
3482 struct g_raid3_disk *disk;
3483
3484 disk = cp->private;
3485 if (disk == NULL)
3486 return;
3487 g_topology_unlock();
3488 sx_xlock(&sc->sc_lock);
3489 sbuf_printf(sb, "%s<Type>", indent);
3490 if (disk->d_no == sc->sc_ndisks - 1)
3491 sbuf_cat(sb, "PARITY");
3492 else
3493 sbuf_cat(sb, "DATA");
3494 sbuf_cat(sb, "</Type>\n");
3495 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
3496 (u_int)disk->d_no);
3497 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
3498 sbuf_printf(sb, "%s<Synchronized>", indent);
3499 if (disk->d_sync.ds_offset == 0)
3500 sbuf_cat(sb, "0%");
3501 else {
3502 sbuf_printf(sb, "%u%%",
3503 (u_int)((disk->d_sync.ds_offset * 100) /
3504 (sc->sc_mediasize / (sc->sc_ndisks - 1))));
3505 }
3506 sbuf_cat(sb, "</Synchronized>\n");
3507 if (disk->d_sync.ds_offset > 0) {
3508 sbuf_printf(sb, "%s<BytesSynced>%jd"
3509 "</BytesSynced>\n", indent,
3510 (intmax_t)disk->d_sync.ds_offset);
3511 }
3512 }
3513 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3514 disk->d_sync.ds_syncid);
3515 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, disk->d_genid);
3516 sbuf_printf(sb, "%s<Flags>", indent);
3517 if (disk->d_flags == 0)
3518 sbuf_cat(sb, "NONE");
3519 else {
3520 int first = 1;
3521
3522 #define ADD_FLAG(flag, name) do { \
3523 if ((disk->d_flags & (flag)) != 0) { \
3524 if (!first) \
3525 sbuf_cat(sb, ", "); \
3526 else \
3527 first = 0; \
3528 sbuf_cat(sb, name); \
3529 } \
3530 } while (0)
3531 ADD_FLAG(G_RAID3_DISK_FLAG_DIRTY, "DIRTY");
3532 ADD_FLAG(G_RAID3_DISK_FLAG_HARDCODED, "HARDCODED");
3533 ADD_FLAG(G_RAID3_DISK_FLAG_SYNCHRONIZING,
3534 "SYNCHRONIZING");
3535 ADD_FLAG(G_RAID3_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3536 ADD_FLAG(G_RAID3_DISK_FLAG_BROKEN, "BROKEN");
3537 #undef ADD_FLAG
3538 }
3539 sbuf_cat(sb, "</Flags>\n");
3540 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3541 g_raid3_disk_state2str(disk->d_state));
3542 sx_xunlock(&sc->sc_lock);
3543 g_topology_lock();
3544 } else {
3545 g_topology_unlock();
3546 sx_xlock(&sc->sc_lock);
3547 if (!g_raid3_use_malloc) {
3548 sbuf_printf(sb,
3549 "%s<Zone4kRequested>%u</Zone4kRequested>\n", indent,
3550 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested);
3551 sbuf_printf(sb,
3552 "%s<Zone4kFailed>%u</Zone4kFailed>\n", indent,
3553 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed);
3554 sbuf_printf(sb,
3555 "%s<Zone16kRequested>%u</Zone16kRequested>\n", indent,
3556 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested);
3557 sbuf_printf(sb,
3558 "%s<Zone16kFailed>%u</Zone16kFailed>\n", indent,
3559 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed);
3560 sbuf_printf(sb,
3561 "%s<Zone64kRequested>%u</Zone64kRequested>\n", indent,
3562 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested);
3563 sbuf_printf(sb,
3564 "%s<Zone64kFailed>%u</Zone64kFailed>\n", indent,
3565 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed);
3566 }
3567 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3568 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3569 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3570 sbuf_printf(sb, "%s<Flags>", indent);
3571 if (sc->sc_flags == 0)
3572 sbuf_cat(sb, "NONE");
3573 else {
3574 int first = 1;
3575
3576 #define ADD_FLAG(flag, name) do { \
3577 if ((sc->sc_flags & (flag)) != 0) { \
3578 if (!first) \
3579 sbuf_cat(sb, ", "); \
3580 else \
3581 first = 0; \
3582 sbuf_cat(sb, name); \
3583 } \
3584 } while (0)
3585 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3586 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3587 ADD_FLAG(G_RAID3_DEVICE_FLAG_ROUND_ROBIN,
3588 "ROUND-ROBIN");
3589 ADD_FLAG(G_RAID3_DEVICE_FLAG_VERIFY, "VERIFY");
3590 #undef ADD_FLAG
3591 }
3592 sbuf_cat(sb, "</Flags>\n");
3593 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3594 sc->sc_ndisks);
3595 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3596 g_raid3_device_state2str(sc->sc_state));
3597 sx_xunlock(&sc->sc_lock);
3598 g_topology_lock();
3599 }
3600 }
3601
3602 static void
g_raid3_shutdown_post_sync(void * arg,int howto)3603 g_raid3_shutdown_post_sync(void *arg, int howto)
3604 {
3605 struct g_class *mp;
3606 struct g_geom *gp, *gp2;
3607 struct g_raid3_softc *sc;
3608 int error;
3609
3610 if ((howto & RB_NOSYNC) != 0)
3611 return;
3612
3613 mp = arg;
3614 g_topology_lock();
3615 g_raid3_shutdown = 1;
3616 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3617 if ((sc = gp->softc) == NULL)
3618 continue;
3619 /* Skip synchronization geom. */
3620 if (gp == sc->sc_sync.ds_geom)
3621 continue;
3622 g_topology_unlock();
3623 sx_xlock(&sc->sc_lock);
3624 g_raid3_idle(sc, -1);
3625 g_cancel_event(sc);
3626 error = g_raid3_destroy(sc, G_RAID3_DESTROY_DELAYED);
3627 if (error != 0)
3628 sx_xunlock(&sc->sc_lock);
3629 g_topology_lock();
3630 }
3631 g_topology_unlock();
3632 }
3633
3634 static void
g_raid3_init(struct g_class * mp)3635 g_raid3_init(struct g_class *mp)
3636 {
3637
3638 g_raid3_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3639 g_raid3_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3640 if (g_raid3_post_sync == NULL)
3641 G_RAID3_DEBUG(0, "Warning! Cannot register shutdown event.");
3642 }
3643
3644 static void
g_raid3_fini(struct g_class * mp)3645 g_raid3_fini(struct g_class *mp)
3646 {
3647
3648 if (g_raid3_post_sync != NULL)
3649 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid3_post_sync);
3650 }
3651
3652 DECLARE_GEOM_CLASS(g_raid3_class, g_raid3);
3653 MODULE_VERSION(geom_raid3, 0);
3654