1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006-2007 Ivan Voras <ivoras@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 /* Implementation notes:
30 * - "Components" are wrappers around providers that make up the
31 * virtual storage (i.e. a virstor has "physical" components)
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/sx.h>
44 #include <sys/bio.h>
45 #include <sys/sbuf.h>
46 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #include <sys/kthread.h>
51 #include <sys/mutex.h>
52 #include <vm/uma.h>
53 #include <geom/geom.h>
54
55 #include <geom/virstor/g_virstor.h>
56 #include <geom/virstor/g_virstor_md.h>
57
58 FEATURE(g_virstor, "GEOM virtual storage support");
59
60 /* Declare malloc(9) label */
61 static MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
62
63 /* GEOM class methods */
64 static g_init_t g_virstor_init;
65 static g_fini_t g_virstor_fini;
66 static g_taste_t g_virstor_taste;
67 static g_ctl_req_t g_virstor_config;
68 static g_ctl_destroy_geom_t g_virstor_destroy_geom;
69
70 /* Declare & initialize class structure ("geom class") */
71 struct g_class g_virstor_class = {
72 .name = G_VIRSTOR_CLASS_NAME,
73 .version = G_VERSION,
74 .init = g_virstor_init,
75 .fini = g_virstor_fini,
76 .taste = g_virstor_taste,
77 .ctlreq = g_virstor_config,
78 .destroy_geom = g_virstor_destroy_geom
79 /* The .dumpconf and the rest are only usable for a geom instance, so
80 * they will be set when such instance is created. */
81 };
82
83 /* Declare sysctl's and loader tunables */
84 SYSCTL_DECL(_kern_geom);
85 static SYSCTL_NODE(_kern_geom, OID_AUTO, virstor, CTLFLAG_RW, 0,
86 "GEOM_GVIRSTOR information");
87
88 static u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */
89 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RWTUN, &g_virstor_debug,
90 0, "Debug level (2=production, 5=normal, 15=excessive)");
91
92 static u_int g_virstor_chunk_watermark = 100;
93 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RWTUN,
94 &g_virstor_chunk_watermark, 0,
95 "Minimum number of free chunks before issuing administrative warning");
96
97 static u_int g_virstor_component_watermark = 1;
98 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RWTUN,
99 &g_virstor_component_watermark, 0,
100 "Minimum number of free components before issuing administrative warning");
101
102 static int read_metadata(struct g_consumer *, struct g_virstor_metadata *);
103 static void write_metadata(struct g_consumer *, struct g_virstor_metadata *);
104 static int clear_metadata(struct g_virstor_component *);
105 static int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *,
106 struct g_virstor_metadata *);
107 static struct g_geom *create_virstor_geom(struct g_class *,
108 struct g_virstor_metadata *);
109 static void virstor_check_and_run(struct g_virstor_softc *);
110 static u_int virstor_valid_components(struct g_virstor_softc *);
111 static int virstor_geom_destroy(struct g_virstor_softc *, boolean_t,
112 boolean_t);
113 static void remove_component(struct g_virstor_softc *,
114 struct g_virstor_component *, boolean_t);
115 static void bioq_dismantle(struct bio_queue_head *);
116 static int allocate_chunk(struct g_virstor_softc *,
117 struct g_virstor_component **, u_int *, u_int *);
118 static void delay_destroy_consumer(void *, int);
119 static void dump_component(struct g_virstor_component *comp);
120 #if 0
121 static void dump_me(struct virstor_map_entry *me, unsigned int nr);
122 #endif
123
124 static void virstor_ctl_stop(struct gctl_req *, struct g_class *);
125 static void virstor_ctl_add(struct gctl_req *, struct g_class *);
126 static void virstor_ctl_remove(struct gctl_req *, struct g_class *);
127 static struct g_virstor_softc * virstor_find_geom(const struct g_class *,
128 const char *);
129 static void update_metadata(struct g_virstor_softc *);
130 static void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
131 u_int, u_int);
132
133 static void g_virstor_orphan(struct g_consumer *);
134 static int g_virstor_access(struct g_provider *, int, int, int);
135 static void g_virstor_start(struct bio *);
136 static void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *,
137 struct g_consumer *, struct g_provider *);
138 static void g_virstor_done(struct bio *);
139
140 static void invalid_call(void);
141 /*
142 * Initialise GEOM class (per-class callback)
143 */
144 static void
145 g_virstor_init(struct g_class *mp __unused)
146 {
147
148 /* Catch map struct size mismatch at compile time; Map entries must
149 * fit into MAXPHYS exactly, with no wasted space. */
150 CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
151
152 /* Init UMA zones, TAILQ's, other global vars */
153 }
154
155 /*
156 * Finalise GEOM class (per-class callback)
157 */
158 static void
159 g_virstor_fini(struct g_class *mp __unused)
160 {
161
162 /* Deinit UMA zones & global vars */
163 }
164
165 /*
166 * Config (per-class callback)
167 */
168 static void
169 g_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
170 {
171 uint32_t *version;
172
173 g_topology_assert();
174
175 version = gctl_get_paraml(req, "version", sizeof(*version));
176 if (version == NULL) {
177 gctl_error(req, "Failed to get 'version' argument");
178 return;
179 }
180 if (*version != G_VIRSTOR_VERSION) {
181 gctl_error(req, "Userland and kernel versions out of sync");
182 return;
183 }
184
185 g_topology_unlock();
186 if (strcmp(verb, "add") == 0)
187 virstor_ctl_add(req, cp);
188 else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0)
189 virstor_ctl_stop(req, cp);
190 else if (strcmp(verb, "remove") == 0)
191 virstor_ctl_remove(req, cp);
192 else
193 gctl_error(req, "unknown verb: '%s'", verb);
194 g_topology_lock();
195 }
196
197 /*
198 * "stop" verb from userland
199 */
200 static void
201 virstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
202 {
203 int *force, *nargs;
204 int i;
205
206 nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
207 if (nargs == NULL) {
208 gctl_error(req, "Error fetching argument '%s'", "nargs");
209 return;
210 }
211 if (*nargs < 1) {
212 gctl_error(req, "Invalid number of arguments");
213 return;
214 }
215 force = gctl_get_paraml(req, "force", sizeof *force);
216 if (force == NULL) {
217 gctl_error(req, "Error fetching argument '%s'", "force");
218 return;
219 }
220
221 g_topology_lock();
222 for (i = 0; i < *nargs; i++) {
223 char param[8];
224 const char *name;
225 struct g_virstor_softc *sc;
226 int error;
227
228 sprintf(param, "arg%d", i);
229 name = gctl_get_asciiparam(req, param);
230 if (name == NULL) {
231 gctl_error(req, "No 'arg%d' argument", i);
232 g_topology_unlock();
233 return;
234 }
235 sc = virstor_find_geom(cp, name);
236 if (sc == NULL) {
237 gctl_error(req, "Don't know anything about '%s'", name);
238 g_topology_unlock();
239 return;
240 }
241
242 LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
243 sc->geom->name);
244 update_metadata(sc);
245 if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
246 LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
247 sc->geom->name, error);
248 }
249 }
250 g_topology_unlock();
251 }
252
253 /*
254 * "add" verb from userland - add new component(s) to the structure.
255 * This will be done all at once in here, without going through the
256 * .taste function for new components.
257 */
258 static void
259 virstor_ctl_add(struct gctl_req *req, struct g_class *cp)
260 {
261 /* Note: while this is going on, I/O is being done on
262 * the g_up and g_down threads. The idea is to make changes
263 * to softc members in a way that can atomically activate
264 * them all at once. */
265 struct g_virstor_softc *sc;
266 int *hardcode, *nargs;
267 const char *geom_name; /* geom to add a component to */
268 struct g_consumer *fcp;
269 struct g_virstor_bio_q *bq;
270 u_int added;
271 int error;
272 int i;
273
274 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
275 if (nargs == NULL) {
276 gctl_error(req, "Error fetching argument '%s'", "nargs");
277 return;
278 }
279 if (*nargs < 2) {
280 gctl_error(req, "Invalid number of arguments");
281 return;
282 }
283 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
284 if (hardcode == NULL) {
285 gctl_error(req, "Error fetching argument '%s'", "hardcode");
286 return;
287 }
288
289 /* Find "our" geom */
290 geom_name = gctl_get_asciiparam(req, "arg0");
291 if (geom_name == NULL) {
292 gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
293 return;
294 }
295 sc = virstor_find_geom(cp, geom_name);
296 if (sc == NULL) {
297 gctl_error(req, "Don't know anything about '%s'", geom_name);
298 return;
299 }
300
301 if (virstor_valid_components(sc) != sc->n_components) {
302 LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
303 "virstor %s", sc->geom->name);
304 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
305 return;
306 }
307
308 fcp = sc->components[0].gcons;
309 added = 0;
310 g_topology_lock();
311 for (i = 1; i < *nargs; i++) {
312 struct g_virstor_metadata md;
313 char aname[8];
314 const char *prov_name;
315 struct g_provider *pp;
316 struct g_consumer *cp;
317 u_int nc;
318 u_int j;
319
320 snprintf(aname, sizeof aname, "arg%d", i);
321 prov_name = gctl_get_asciiparam(req, aname);
322 if (prov_name == NULL) {
323 gctl_error(req, "Error fetching argument '%s'", aname);
324 g_topology_unlock();
325 return;
326 }
327 if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
328 prov_name += sizeof(_PATH_DEV) - 1;
329
330 pp = g_provider_by_name(prov_name);
331 if (pp == NULL) {
332 /* This is the most common error so be verbose about it */
333 if (added != 0) {
334 gctl_error(req, "Invalid provider: '%s' (added"
335 " %u components)", prov_name, added);
336 update_metadata(sc);
337 } else {
338 gctl_error(req, "Invalid provider: '%s'",
339 prov_name);
340 }
341 g_topology_unlock();
342 return;
343 }
344 cp = g_new_consumer(sc->geom);
345 if (cp == NULL) {
346 gctl_error(req, "Cannot create consumer");
347 g_topology_unlock();
348 return;
349 }
350 error = g_attach(cp, pp);
351 if (error != 0) {
352 gctl_error(req, "Cannot attach a consumer to %s",
353 pp->name);
354 g_destroy_consumer(cp);
355 g_topology_unlock();
356 return;
357 }
358 if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
359 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
360 if (error != 0) {
361 gctl_error(req, "Access request failed for %s",
362 pp->name);
363 g_destroy_consumer(cp);
364 g_topology_unlock();
365 return;
366 }
367 }
368 if (fcp->provider->sectorsize != pp->sectorsize) {
369 gctl_error(req, "Sector size doesn't fit for %s",
370 pp->name);
371 g_destroy_consumer(cp);
372 g_topology_unlock();
373 return;
374 }
375 for (j = 0; j < sc->n_components; j++) {
376 if (strcmp(sc->components[j].gcons->provider->name,
377 pp->name) == 0) {
378 gctl_error(req, "Component %s already in %s",
379 pp->name, sc->geom->name);
380 g_destroy_consumer(cp);
381 g_topology_unlock();
382 return;
383 }
384 }
385 sc->components = realloc(sc->components,
386 sizeof(*sc->components) * (sc->n_components + 1),
387 M_GVIRSTOR, M_WAITOK);
388
389 nc = sc->n_components;
390 sc->components[nc].gcons = cp;
391 sc->components[nc].sc = sc;
392 sc->components[nc].index = nc;
393 sc->components[nc].chunk_count = cp->provider->mediasize /
394 sc->chunk_size;
395 sc->components[nc].chunk_next = 0;
396 sc->components[nc].chunk_reserved = 0;
397
398 if (sc->components[nc].chunk_count < 4) {
399 gctl_error(req, "Provider too small: %s",
400 cp->provider->name);
401 g_destroy_consumer(cp);
402 g_topology_unlock();
403 return;
404 }
405 fill_metadata(sc, &md, nc, *hardcode);
406 write_metadata(cp, &md);
407 /* The new component becomes visible when n_components is
408 * incremented */
409 sc->n_components++;
410 added++;
411
412 }
413 /* This call to update_metadata() is critical. In case there's a
414 * power failure in the middle of it and some components are updated
415 * while others are not, there will be trouble on next .taste() iff
416 * a non-updated component is detected first */
417 update_metadata(sc);
418 g_topology_unlock();
419 LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
420 sc->geom->name);
421 /* Fire off BIOs previously queued because there wasn't any
422 * physical space left. If the BIOs still can't be satisfied
423 * they will again be added to the end of the queue (during
424 * which the mutex will be recursed) */
425 bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
426 bq->bio = NULL;
427 mtx_lock(&sc->delayed_bio_q_mtx);
428 /* First, insert a sentinel to the queue end, so we don't
429 * end up in an infinite loop if there's still no free
430 * space available. */
431 STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
432 while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
433 bq = STAILQ_FIRST(&sc->delayed_bio_q);
434 if (bq->bio != NULL) {
435 g_virstor_start(bq->bio);
436 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
437 free(bq, M_GVIRSTOR);
438 } else {
439 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
440 free(bq, M_GVIRSTOR);
441 break;
442 }
443 }
444 mtx_unlock(&sc->delayed_bio_q_mtx);
445
446 }
447
448 /*
449 * Find a geom handled by the class
450 */
451 static struct g_virstor_softc *
452 virstor_find_geom(const struct g_class *cp, const char *name)
453 {
454 struct g_geom *gp;
455
456 LIST_FOREACH(gp, &cp->geom, geom) {
457 if (strcmp(name, gp->name) == 0)
458 return (gp->softc);
459 }
460 return (NULL);
461 }
462
463 /*
464 * Update metadata on all components to reflect the current state
465 * of these fields:
466 * - chunk_next
467 * - flags
468 * - md_count
469 * Expects things to be set up so write_metadata() can work, i.e.
470 * the topology lock must be held.
471 */
472 static void
473 update_metadata(struct g_virstor_softc *sc)
474 {
475 struct g_virstor_metadata md;
476 u_int n;
477
478 if (virstor_valid_components(sc) != sc->n_components)
479 return; /* Incomplete device */
480 LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
481 sc->geom->name);
482 /* Update metadata on components */
483 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
484 sc->geom->class->name, sc->geom->name);
485 g_topology_assert();
486 for (n = 0; n < sc->n_components; n++) {
487 read_metadata(sc->components[n].gcons, &md);
488 md.chunk_next = sc->components[n].chunk_next;
489 md.flags = sc->components[n].flags;
490 md.md_count = sc->n_components;
491 write_metadata(sc->components[n].gcons, &md);
492 }
493 }
494
495 /*
496 * Fills metadata (struct md) from information stored in softc and the nc'th
497 * component of virstor
498 */
499 static void
500 fill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
501 u_int nc, u_int hardcode)
502 {
503 struct g_virstor_component *c;
504
505 bzero(md, sizeof *md);
506 c = &sc->components[nc];
507
508 strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
509 md->md_version = G_VIRSTOR_VERSION;
510 strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
511 md->md_id = sc->id;
512 md->md_virsize = sc->virsize;
513 md->md_chunk_size = sc->chunk_size;
514 md->md_count = sc->n_components;
515
516 if (hardcode) {
517 strncpy(md->provider, c->gcons->provider->name,
518 sizeof md->provider);
519 }
520 md->no = nc;
521 md->provsize = c->gcons->provider->mediasize;
522 md->chunk_count = c->chunk_count;
523 md->chunk_next = c->chunk_next;
524 md->chunk_reserved = c->chunk_reserved;
525 md->flags = c->flags;
526 }
527
528 /*
529 * Remove a component from virstor device.
530 * Can only be done if the component is unallocated.
531 */
532 static void
533 virstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
534 {
535 /* As this is executed in parallel to I/O, operations on virstor
536 * structures must be as atomic as possible. */
537 struct g_virstor_softc *sc;
538 int *nargs;
539 const char *geom_name;
540 u_int removed;
541 int i;
542
543 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
544 if (nargs == NULL) {
545 gctl_error(req, "Error fetching argument '%s'", "nargs");
546 return;
547 }
548 if (*nargs < 2) {
549 gctl_error(req, "Invalid number of arguments");
550 return;
551 }
552 /* Find "our" geom */
553 geom_name = gctl_get_asciiparam(req, "arg0");
554 if (geom_name == NULL) {
555 gctl_error(req, "Error fetching argument '%s'",
556 "geom_name (arg0)");
557 return;
558 }
559 sc = virstor_find_geom(cp, geom_name);
560 if (sc == NULL) {
561 gctl_error(req, "Don't know anything about '%s'", geom_name);
562 return;
563 }
564
565 if (virstor_valid_components(sc) != sc->n_components) {
566 LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
567 "virstor %s", sc->geom->name);
568 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
569 return;
570 }
571
572 removed = 0;
573 for (i = 1; i < *nargs; i++) {
574 char param[8];
575 const char *prov_name;
576 int j, found;
577 struct g_virstor_component *newcomp, *compbak;
578
579 sprintf(param, "arg%d", i);
580 prov_name = gctl_get_asciiparam(req, param);
581 if (prov_name == NULL) {
582 gctl_error(req, "Error fetching argument '%s'", param);
583 return;
584 }
585 if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
586 prov_name += sizeof(_PATH_DEV) - 1;
587
588 found = -1;
589 for (j = 0; j < sc->n_components; j++) {
590 if (strcmp(sc->components[j].gcons->provider->name,
591 prov_name) == 0) {
592 found = j;
593 break;
594 }
595 }
596 if (found == -1) {
597 LOG_MSG(LVL_ERROR, "No %s component in %s",
598 prov_name, sc->geom->name);
599 continue;
600 }
601
602 compbak = sc->components;
603 newcomp = malloc(sc->n_components * sizeof(*sc->components),
604 M_GVIRSTOR, M_WAITOK | M_ZERO);
605 bcopy(sc->components, newcomp, found * sizeof(*sc->components));
606 bcopy(&sc->components[found + 1], newcomp + found,
607 found * sizeof(*sc->components));
608 if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
609 LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
610 "removed from %s",
611 prov_name, sc->geom->name);
612 free(newcomp, M_GVIRSTOR);
613 /* We'll consider this non-fatal error */
614 continue;
615 }
616 /* Renumerate unallocated components */
617 for (j = 0; j < sc->n_components-1; j++) {
618 if ((sc->components[j].flags &
619 VIRSTOR_PROVIDER_ALLOCATED) == 0) {
620 sc->components[j].index = j;
621 }
622 }
623 /* This is the critical section. If a component allocation
624 * event happens while both variables are not yet set,
625 * there will be trouble. Something will panic on encountering
626 * NULL sc->components[x].gcomp member.
627 * Luckily, component allocation happens very rarely and
628 * removing components is an abnormal action in any case. */
629 sc->components = newcomp;
630 sc->n_components--;
631 /* End critical section */
632
633 g_topology_lock();
634 if (clear_metadata(&compbak[found]) != 0) {
635 LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
636 "metadata on %s", prov_name);
637 }
638 g_detach(compbak[found].gcons);
639 g_destroy_consumer(compbak[found].gcons);
640 g_topology_unlock();
641
642 free(compbak, M_GVIRSTOR);
643
644 removed++;
645 }
646
647 /* This call to update_metadata() is critical. In case there's a
648 * power failure in the middle of it and some components are updated
649 * while others are not, there will be trouble on next .taste() iff
650 * a non-updated component is detected first */
651 g_topology_lock();
652 update_metadata(sc);
653 g_topology_unlock();
654 LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
655 sc->geom->name);
656 }
657
658 /*
659 * Clear metadata sector on component
660 */
661 static int
662 clear_metadata(struct g_virstor_component *comp)
663 {
664 char *buf;
665 int error;
666
667 LOG_MSG(LVL_INFO, "Clearing metadata on %s",
668 comp->gcons->provider->name);
669 g_topology_assert();
670 error = g_access(comp->gcons, 0, 1, 0);
671 if (error != 0)
672 return (error);
673 buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
674 M_WAITOK | M_ZERO);
675 error = g_write_data(comp->gcons,
676 comp->gcons->provider->mediasize -
677 comp->gcons->provider->sectorsize,
678 buf,
679 comp->gcons->provider->sectorsize);
680 free(buf, M_GVIRSTOR);
681 g_access(comp->gcons, 0, -1, 0);
682 return (error);
683 }
684
685 /*
686 * Destroy geom forcibly.
687 */
688 static int
689 g_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
690 struct g_geom *gp)
691 {
692 struct g_virstor_softc *sc;
693 int exitval;
694
695 sc = gp->softc;
696 KASSERT(sc != NULL, ("%s: NULL sc", __func__));
697
698 exitval = 0;
699 LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
700 gp->softc);
701
702 if (sc != NULL) {
703 #ifdef INVARIANTS
704 char *buf;
705 int error;
706 off_t off;
707 int isclean, count;
708 int n;
709
710 LOG_MSG(LVL_INFO, "INVARIANTS detected");
711 LOG_MSG(LVL_INFO, "Verifying allocation "
712 "table for %s", sc->geom->name);
713 count = 0;
714 for (n = 0; n < sc->chunk_count; n++) {
715 if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
716 count++;
717 }
718 LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
719 sc->geom->name, count);
720 n = off = count = 0;
721 isclean = 1;
722 if (virstor_valid_components(sc) != sc->n_components) {
723 /* This is a incomplete virstor device (not all
724 * components have been found) */
725 LOG_MSG(LVL_ERROR, "Device %s is incomplete",
726 sc->geom->name);
727 goto bailout;
728 }
729 error = g_access(sc->components[0].gcons, 1, 0, 0);
730 KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
731 error));
732 /* Compare the whole on-disk allocation table with what's
733 * currently in memory */
734 while (n < sc->chunk_count) {
735 buf = g_read_data(sc->components[0].gcons, off,
736 sc->sectorsize, &error);
737 KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
738 "for read at %jd", error, off));
739 if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
740 LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
741 "entry %d, offset %jd", n, off);
742 isclean = 0;
743 count++;
744 }
745 n += sc->me_per_sector;
746 off += sc->sectorsize;
747 g_free(buf);
748 }
749 error = g_access(sc->components[0].gcons, -1, 0, 0);
750 KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
751 __func__, error));
752 if (isclean != 1) {
753 LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
754 "(%d sectors don't match, max %zu allocations)",
755 sc->geom->name, count,
756 count * sc->me_per_sector);
757 } else {
758 LOG_MSG(LVL_INFO, "Allocation table ok for %s",
759 sc->geom->name);
760 }
761 bailout:
762 #endif
763 update_metadata(sc);
764 virstor_geom_destroy(sc, FALSE, FALSE);
765 exitval = EAGAIN;
766 } else
767 exitval = 0;
768 return (exitval);
769 }
770
771 /*
772 * Taste event (per-class callback)
773 * Examines a provider and creates geom instances if needed
774 */
775 static struct g_geom *
776 g_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
777 {
778 struct g_virstor_metadata md;
779 struct g_geom *gp;
780 struct g_consumer *cp;
781 struct g_virstor_softc *sc;
782 int error;
783
784 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
785 g_topology_assert();
786 LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
787
788 /* We need a dummy geom to attach a consumer to the given provider */
789 gp = g_new_geomf(mp, "virstor:taste.helper");
790 gp->start = (void *)invalid_call; /* XXX: hacked up so the */
791 gp->access = (void *)invalid_call; /* compiler doesn't complain. */
792 gp->orphan = (void *)invalid_call; /* I really want these to fail. */
793
794 cp = g_new_consumer(gp);
795 g_attach(cp, pp);
796 error = read_metadata(cp, &md);
797 g_detach(cp);
798 g_destroy_consumer(cp);
799 g_destroy_geom(gp);
800
801 if (error != 0)
802 return (NULL);
803
804 if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
805 return (NULL);
806 if (md.md_version != G_VIRSTOR_VERSION) {
807 LOG_MSG(LVL_ERROR, "Kernel module version invalid "
808 "to handle %s (%s) : %d should be %d",
809 md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
810 return (NULL);
811 }
812 if (md.provsize != pp->mediasize)
813 return (NULL);
814
815 /* If the provider name is hardcoded, use the offered provider only
816 * if it's been offered with its proper name (the one used in
817 * the label command). */
818 if (md.provider[0] != '\0' &&
819 !g_compare_names(md.provider, pp->name))
820 return (NULL);
821
822 /* Iterate all geoms this class already knows about to see if a new
823 * geom instance of this class needs to be created (in case the provider
824 * is first from a (possibly) multi-consumer geom) or it just needs
825 * to be added to an existing instance. */
826 sc = NULL;
827 gp = NULL;
828 LIST_FOREACH(gp, &mp->geom, geom) {
829 sc = gp->softc;
830 if (sc == NULL)
831 continue;
832 if (strcmp(md.md_name, sc->geom->name) != 0)
833 continue;
834 if (md.md_id != sc->id)
835 continue;
836 break;
837 }
838 if (gp != NULL) { /* We found an existing geom instance; add to it */
839 LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
840 error = add_provider_to_geom(sc, pp, &md);
841 if (error != 0) {
842 LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
843 pp->name, md.md_name, error);
844 return (NULL);
845 }
846 } else { /* New geom instance needs to be created */
847 gp = create_virstor_geom(mp, &md);
848 if (gp == NULL) {
849 LOG_MSG(LVL_ERROR, "Error creating new instance of "
850 "class %s: %s", mp->name, md.md_name);
851 LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
852 md.md_name, pp->name);
853 return (NULL);
854 }
855 sc = gp->softc;
856 LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
857 md.md_name);
858 error = add_provider_to_geom(sc, pp, &md);
859 if (error != 0) {
860 LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
861 pp->name, md.md_name, error);
862 virstor_geom_destroy(sc, TRUE, FALSE);
863 return (NULL);
864 }
865 }
866
867 return (gp);
868 }
869
870 /*
871 * Destroyes consumer passed to it in arguments. Used as a callback
872 * on g_event queue.
873 */
874 static void
875 delay_destroy_consumer(void *arg, int flags __unused)
876 {
877 struct g_consumer *c = arg;
878 KASSERT(c != NULL, ("%s: invalid consumer", __func__));
879 LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
880 c->provider->name);
881 g_detach(c);
882 g_destroy_consumer(c);
883 }
884
885 /*
886 * Remove a component (consumer) from geom instance; If it's the first
887 * component being removed, orphan the provider to announce geom's being
888 * dismantled
889 */
890 static void
891 remove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
892 boolean_t delay)
893 {
894 struct g_consumer *c;
895
896 KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
897 sc->geom->name));
898 c = comp->gcons;
899
900 comp->gcons = NULL;
901 KASSERT(c->provider != NULL, ("%s: no provider", __func__));
902 LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
903 sc->geom->name);
904 if (sc->provider != NULL) {
905 LOG_MSG(LVL_INFO, "Removing provider %s", sc->provider->name);
906 g_wither_provider(sc->provider, ENXIO);
907 sc->provider = NULL;
908 }
909
910 if (c->acr > 0 || c->acw > 0 || c->ace > 0)
911 return;
912 if (delay) {
913 /* Destroy consumer after it's tasted */
914 g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
915 } else {
916 g_detach(c);
917 g_destroy_consumer(c);
918 }
919 }
920
921 /*
922 * Destroy geom - called internally
923 * See g_virstor_destroy_geom for the other one
924 */
925 static int
926 virstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
927 boolean_t delay)
928 {
929 struct g_provider *pp;
930 struct g_geom *gp;
931 u_int n;
932
933 g_topology_assert();
934
935 if (sc == NULL)
936 return (ENXIO);
937
938 pp = sc->provider;
939 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
940 LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
941 "Device %s is still open.", pp->name);
942 if (!force)
943 return (EBUSY);
944 }
945
946 for (n = 0; n < sc->n_components; n++) {
947 if (sc->components[n].gcons != NULL)
948 remove_component(sc, &sc->components[n], delay);
949 }
950
951 gp = sc->geom;
952 gp->softc = NULL;
953
954 KASSERT(sc->provider == NULL, ("Provider still exists for %s",
955 gp->name));
956
957 /* XXX: This might or might not work, since we're called with
958 * the topology lock held. Also, it might panic the kernel if
959 * the error'd BIO is in softupdates code. */
960 mtx_lock(&sc->delayed_bio_q_mtx);
961 while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
962 struct g_virstor_bio_q *bq;
963 bq = STAILQ_FIRST(&sc->delayed_bio_q);
964 bq->bio->bio_error = ENOSPC;
965 g_io_deliver(bq->bio, EIO);
966 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
967 free(bq, M_GVIRSTOR);
968 }
969 mtx_unlock(&sc->delayed_bio_q_mtx);
970 mtx_destroy(&sc->delayed_bio_q_mtx);
971
972 free(sc->map, M_GVIRSTOR);
973 free(sc->components, M_GVIRSTOR);
974 bzero(sc, sizeof *sc);
975 free(sc, M_GVIRSTOR);
976
977 pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
978 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
979 LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
980
981 g_wither_geom(gp, ENXIO);
982
983 return (0);
984 }
985
986 /*
987 * Utility function: read metadata & decode. Wants topology lock to be
988 * held.
989 */
990 static int
991 read_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
992 {
993 struct g_provider *pp;
994 char *buf;
995 int error;
996
997 g_topology_assert();
998 error = g_access(cp, 1, 0, 0);
999 if (error != 0)
1000 return (error);
1001 pp = cp->provider;
1002 g_topology_unlock();
1003 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
1004 &error);
1005 g_topology_lock();
1006 g_access(cp, -1, 0, 0);
1007 if (buf == NULL)
1008 return (error);
1009
1010 virstor_metadata_decode(buf, md);
1011 g_free(buf);
1012
1013 return (0);
1014 }
1015
1016 /**
1017 * Utility function: encode & write metadata. Assumes topology lock is
1018 * held.
1019 *
1020 * There is no useful way of recovering from errors in this function,
1021 * not involving panicking the kernel. If the metadata cannot be written
1022 * the most we can do is notify the operator and hope he spots it and
1023 * replaces the broken drive.
1024 */
1025 static void
1026 write_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
1027 {
1028 struct g_provider *pp;
1029 char *buf;
1030 int error;
1031
1032 KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
1033 ("Something's fishy in %s", __func__));
1034 LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
1035 g_topology_assert();
1036 error = g_access(cp, 0, 1, 0);
1037 if (error != 0) {
1038 LOG_MSG(LVL_ERROR, "g_access(0,1,0) failed for %s: %d",
1039 cp->provider->name, error);
1040 return;
1041 }
1042 pp = cp->provider;
1043
1044 buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
1045 bzero(buf, pp->sectorsize);
1046 virstor_metadata_encode(md, buf);
1047 g_topology_unlock();
1048 error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
1049 pp->sectorsize);
1050 g_topology_lock();
1051 g_access(cp, 0, -1, 0);
1052 free(buf, M_GVIRSTOR);
1053
1054 if (error != 0)
1055 LOG_MSG(LVL_ERROR, "Error %d writing metadata to %s",
1056 error, cp->provider->name);
1057 }
1058
1059 /*
1060 * Creates a new instance of this GEOM class, initialise softc
1061 */
1062 static struct g_geom *
1063 create_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
1064 {
1065 struct g_geom *gp;
1066 struct g_virstor_softc *sc;
1067
1068 LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
1069 md->md_name, md->md_id);
1070
1071 if (md->md_count < 1 || md->md_chunk_size < 1 ||
1072 md->md_virsize < md->md_chunk_size) {
1073 /* This is bogus configuration, and probably means data is
1074 * somehow corrupted. Panic, maybe? */
1075 LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
1076 md->md_name);
1077 return (NULL);
1078 }
1079
1080 /* Check if it's already created */
1081 LIST_FOREACH(gp, &mp->geom, geom) {
1082 sc = gp->softc;
1083 if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
1084 LOG_MSG(LVL_WARNING, "Geom %s already exists",
1085 md->md_name);
1086 if (sc->id != md->md_id) {
1087 LOG_MSG(LVL_ERROR,
1088 "Some stale or invalid components "
1089 "exist for virstor device named %s. "
1090 "You will need to <CLEAR> all stale "
1091 "components and maybe reconfigure "
1092 "the virstor device. Tune "
1093 "kern.geom.virstor.debug sysctl up "
1094 "for more information.",
1095 sc->geom->name);
1096 }
1097 return (NULL);
1098 }
1099 }
1100 gp = g_new_geomf(mp, "%s", md->md_name);
1101 gp->softc = NULL; /* to circumevent races that test softc */
1102
1103 gp->start = g_virstor_start;
1104 gp->spoiled = g_virstor_orphan;
1105 gp->orphan = g_virstor_orphan;
1106 gp->access = g_virstor_access;
1107 gp->dumpconf = g_virstor_dumpconf;
1108
1109 sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
1110 sc->id = md->md_id;
1111 sc->n_components = md->md_count;
1112 sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
1113 M_GVIRSTOR, M_WAITOK | M_ZERO);
1114 sc->chunk_size = md->md_chunk_size;
1115 sc->virsize = md->md_virsize;
1116 STAILQ_INIT(&sc->delayed_bio_q);
1117 mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
1118 "gvirstor", MTX_DEF | MTX_RECURSE);
1119
1120 sc->geom = gp;
1121 sc->provider = NULL; /* virstor_check_and_run will create it */
1122 gp->softc = sc;
1123
1124 LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
1125
1126 return (gp);
1127 }
1128
1129 /*
1130 * Add provider to a GEOM class instance
1131 */
1132 static int
1133 add_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
1134 struct g_virstor_metadata *md)
1135 {
1136 struct g_virstor_component *component;
1137 struct g_consumer *cp, *fcp;
1138 struct g_geom *gp;
1139 int error;
1140
1141 if (md->no >= sc->n_components)
1142 return (EINVAL);
1143
1144 /* "Current" compontent */
1145 component = &(sc->components[md->no]);
1146 if (component->gcons != NULL)
1147 return (EEXIST);
1148
1149 gp = sc->geom;
1150 fcp = LIST_FIRST(&gp->consumer);
1151
1152 cp = g_new_consumer(gp);
1153 error = g_attach(cp, pp);
1154
1155 if (error != 0) {
1156 g_destroy_consumer(cp);
1157 return (error);
1158 }
1159
1160 if (fcp != NULL) {
1161 if (fcp->provider->sectorsize != pp->sectorsize) {
1162 /* TODO: this can be made to work */
1163 LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
1164 "sector size (%d)", pp->name, sc->geom->name,
1165 pp->sectorsize);
1166 return (EINVAL);
1167 }
1168 if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
1169 /* Replicate access permissions from first "live" consumer
1170 * to the new one */
1171 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1172 if (error != 0) {
1173 g_detach(cp);
1174 g_destroy_consumer(cp);
1175 return (error);
1176 }
1177 }
1178 }
1179
1180 /* Bring up a new component */
1181 cp->private = component;
1182 component->gcons = cp;
1183 component->sc = sc;
1184 component->index = md->no;
1185 component->chunk_count = md->chunk_count;
1186 component->chunk_next = md->chunk_next;
1187 component->chunk_reserved = md->chunk_reserved;
1188 component->flags = md->flags;
1189
1190 LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
1191
1192 virstor_check_and_run(sc);
1193 return (0);
1194 }
1195
1196 /*
1197 * Check if everything's ready to create the geom provider & device entry,
1198 * create and start provider.
1199 * Called ultimately by .taste, from g_event thread
1200 */
1201 static void
1202 virstor_check_and_run(struct g_virstor_softc *sc)
1203 {
1204 off_t off;
1205 size_t n, count;
1206 int index;
1207 int error;
1208
1209 if (virstor_valid_components(sc) != sc->n_components)
1210 return;
1211
1212 if (virstor_valid_components(sc) == 0) {
1213 /* This is actually a candidate for panic() */
1214 LOG_MSG(LVL_ERROR, "No valid components for %s?",
1215 sc->provider->name);
1216 return;
1217 }
1218
1219 sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
1220
1221 /* Initialise allocation map from the first consumer */
1222 sc->chunk_count = sc->virsize / sc->chunk_size;
1223 if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
1224 LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
1225 sc->provider->name,
1226 sc->chunk_count * (off_t)sc->chunk_size);
1227 }
1228 sc->map_size = sc->chunk_count * sizeof *(sc->map);
1229 /* The following allocation is in order of 4MB - 8MB */
1230 sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
1231 KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
1232 __func__, sc->map_size, sc->provider->name));
1233 sc->map_sectors = sc->map_size / sc->sectorsize;
1234
1235 count = 0;
1236 for (n = 0; n < sc->n_components; n++)
1237 count += sc->components[n].chunk_count;
1238 LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
1239 "(%zu KB chunks)",
1240 sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
1241
1242 error = g_access(sc->components[0].gcons, 1, 0, 0);
1243 if (error != 0) {
1244 LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
1245 "read allocation map for %s",
1246 sc->components[0].gcons->provider->name,
1247 sc->geom->name);
1248 return;
1249 }
1250 /* Read in the allocation map */
1251 LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
1252 sc->components[0].gcons->provider->name);
1253 off = count = n = 0;
1254 while (count < sc->map_size) {
1255 struct g_virstor_map_entry *mapbuf;
1256 size_t bs;
1257
1258 bs = MIN(MAXPHYS, sc->map_size - count);
1259 if (bs % sc->sectorsize != 0) {
1260 /* Check for alignment errors */
1261 bs = rounddown(bs, sc->sectorsize);
1262 if (bs == 0)
1263 break;
1264 LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
1265 "for %s on %s", sc->geom->name,
1266 sc->components[0].gcons->provider->name);
1267 }
1268 mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
1269 if (mapbuf == NULL) {
1270 free(sc->map, M_GVIRSTOR);
1271 LOG_MSG(LVL_ERROR, "Error reading allocation map "
1272 "for %s from %s (offset %ju) (error %d)",
1273 sc->geom->name,
1274 sc->components[0].gcons->provider->name,
1275 off, error);
1276 return;
1277 }
1278
1279 bcopy(mapbuf, &sc->map[n], bs);
1280 off += bs;
1281 count += bs;
1282 n += bs / sizeof *(sc->map);
1283 g_free(mapbuf);
1284 }
1285 g_access(sc->components[0].gcons, -1, 0, 0);
1286 LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
1287
1288 /* find first component with allocatable chunks */
1289 index = -1;
1290 for (n = 0; n < sc->n_components; n++) {
1291 if (sc->components[n].chunk_next <
1292 sc->components[n].chunk_count) {
1293 index = n;
1294 break;
1295 }
1296 }
1297 if (index == -1)
1298 /* not found? set it to the last component and handle it
1299 * later */
1300 index = sc->n_components - 1;
1301
1302 if (index >= sc->n_components - g_virstor_component_watermark - 1) {
1303 LOG_MSG(LVL_WARNING, "Device %s running out of components "
1304 "(%d/%u: %s)", sc->geom->name,
1305 index+1,
1306 sc->n_components,
1307 sc->components[index].gcons->provider->name);
1308 }
1309 sc->curr_component = index;
1310
1311 if (sc->components[index].chunk_next >=
1312 sc->components[index].chunk_count - g_virstor_chunk_watermark) {
1313 LOG_MSG(LVL_WARNING,
1314 "Component %s of %s is running out of free space "
1315 "(%u chunks left)",
1316 sc->components[index].gcons->provider->name,
1317 sc->geom->name, sc->components[index].chunk_count -
1318 sc->components[index].chunk_next);
1319 }
1320
1321 sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
1322 if (sc->sectorsize % sizeof *(sc->map) != 0) {
1323 LOG_MSG(LVL_ERROR,
1324 "%s: Map entries don't fit exactly in a sector (%s)",
1325 __func__, sc->geom->name);
1326 return;
1327 }
1328
1329 /* Recalculate allocated chunks in components & at the same time
1330 * verify map data is sane. We could trust metadata on this, but
1331 * we want to make sure. */
1332 for (n = 0; n < sc->n_components; n++)
1333 sc->components[n].chunk_next = sc->components[n].chunk_reserved;
1334
1335 for (n = 0; n < sc->chunk_count; n++) {
1336 if (sc->map[n].provider_no >= sc->n_components ||
1337 sc->map[n].provider_chunk >=
1338 sc->components[sc->map[n].provider_no].chunk_count) {
1339 LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
1340 __func__, (u_int)n, sc->geom->name);
1341 LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
1342 " provider_chunk: %u, chunk_count: %u", __func__,
1343 sc->map[n].provider_no, sc->n_components,
1344 sc->map[n].provider_chunk,
1345 sc->components[sc->map[n].provider_no].chunk_count);
1346 return;
1347 }
1348 if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
1349 sc->components[sc->map[n].provider_no].chunk_next++;
1350 }
1351
1352 sc->provider = g_new_providerf(sc->geom, "virstor/%s",
1353 sc->geom->name);
1354
1355 sc->provider->sectorsize = sc->sectorsize;
1356 sc->provider->mediasize = sc->virsize;
1357 g_error_provider(sc->provider, 0);
1358
1359 LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
1360 LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
1361 "chunk %u", sc->provider->name, sc->curr_component,
1362 sc->components[sc->curr_component].chunk_next);
1363 }
1364
1365 /*
1366 * Returns count of active providers in this geom instance
1367 */
1368 static u_int
1369 virstor_valid_components(struct g_virstor_softc *sc)
1370 {
1371 unsigned int nc, i;
1372
1373 nc = 0;
1374 KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
1375 KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
1376 for (i = 0; i < sc->n_components; i++)
1377 if (sc->components[i].gcons != NULL)
1378 nc++;
1379 return (nc);
1380 }
1381
1382 /*
1383 * Called when the consumer gets orphaned (?)
1384 */
1385 static void
1386 g_virstor_orphan(struct g_consumer *cp)
1387 {
1388 struct g_virstor_softc *sc;
1389 struct g_virstor_component *comp;
1390 struct g_geom *gp;
1391
1392 g_topology_assert();
1393 gp = cp->geom;
1394 sc = gp->softc;
1395 if (sc == NULL)
1396 return;
1397
1398 comp = cp->private;
1399 KASSERT(comp != NULL, ("%s: No component in private part of consumer",
1400 __func__));
1401 remove_component(sc, comp, FALSE);
1402 if (LIST_EMPTY(&gp->consumer))
1403 virstor_geom_destroy(sc, TRUE, FALSE);
1404 }
1405
1406 /*
1407 * Called to notify geom when it's been opened, and for what intent
1408 */
1409 static int
1410 g_virstor_access(struct g_provider *pp, int dr, int dw, int de)
1411 {
1412 struct g_consumer *c, *c2, *tmp;
1413 struct g_virstor_softc *sc;
1414 struct g_geom *gp;
1415 int error;
1416
1417 KASSERT(pp != NULL, ("%s: NULL provider", __func__));
1418 gp = pp->geom;
1419 KASSERT(gp != NULL, ("%s: NULL geom", __func__));
1420 sc = gp->softc;
1421
1422 /* Grab an exclusive bit to propagate on our consumers on first open */
1423 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1424 de++;
1425 /* ... drop it on close */
1426 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
1427 de--;
1428 if (sc != NULL)
1429 update_metadata(sc);
1430 }
1431
1432 error = ENXIO;
1433 LIST_FOREACH_SAFE(c, &gp->consumer, consumer, tmp) {
1434 error = g_access(c, dr, dw, de);
1435 if (error != 0)
1436 goto fail;
1437 if (c->acr == 0 && c->acw == 0 && c->ace == 0 &&
1438 c->flags & G_CF_ORPHAN) {
1439 g_detach(c);
1440 g_destroy_consumer(c);
1441 }
1442 }
1443
1444 if (sc != NULL && LIST_EMPTY(&gp->consumer))
1445 virstor_geom_destroy(sc, TRUE, FALSE);
1446
1447 return (error);
1448
1449 fail:
1450 /* Backout earlier changes */
1451 LIST_FOREACH(c2, &gp->consumer, consumer) {
1452 if (c2 == c)
1453 break;
1454 g_access(c2, -dr, -dw, -de);
1455 }
1456 return (error);
1457 }
1458
1459 /*
1460 * Generate XML dump of current state
1461 */
1462 static void
1463 g_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1464 struct g_consumer *cp, struct g_provider *pp)
1465 {
1466 struct g_virstor_softc *sc;
1467
1468 g_topology_assert();
1469 sc = gp->softc;
1470
1471 if (sc == NULL || pp != NULL)
1472 return;
1473
1474 if (cp != NULL) {
1475 /* For each component */
1476 struct g_virstor_component *comp;
1477
1478 comp = cp->private;
1479 if (comp == NULL)
1480 return;
1481 sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
1482 indent, comp->index);
1483 sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
1484 indent, comp->chunk_count);
1485 sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
1486 indent, comp->chunk_next);
1487 sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
1488 indent, comp->chunk_reserved);
1489 sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
1490 indent,
1491 comp->chunk_next > 0 ? 100 -
1492 ((comp->chunk_next + comp->chunk_reserved) * 100) /
1493 comp->chunk_count : 100);
1494 } else {
1495 /* For the whole thing */
1496 u_int count, used, i;
1497 off_t size;
1498
1499 count = used = size = 0;
1500 for (i = 0; i < sc->n_components; i++) {
1501 if (sc->components[i].gcons != NULL) {
1502 count += sc->components[i].chunk_count;
1503 used += sc->components[i].chunk_next +
1504 sc->components[i].chunk_reserved;
1505 size += sc->components[i].gcons->
1506 provider->mediasize;
1507 }
1508 }
1509
1510 sbuf_printf(sb, "%s<Status>"
1511 "Components=%u, Online=%u</Status>\n", indent,
1512 sc->n_components, virstor_valid_components(sc));
1513 sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
1514 indent, 100-(used * 100) / count);
1515 sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
1516 sc->chunk_size);
1517 sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
1518 indent, used > 0 ? 100 - (used * 100) / count : 100);
1519 sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
1520 indent, count);
1521 sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
1522 indent, sc->chunk_count);
1523 sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
1524 indent,
1525 (count * 100) / sc->chunk_count);
1526 sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
1527 indent, size);
1528 sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
1529 sc->virsize);
1530 }
1531 }
1532
1533 /*
1534 * GEOM .done handler
1535 * Can't use standard handler because one requested IO may
1536 * fork into additional data IOs
1537 */
1538 static void
1539 g_virstor_done(struct bio *b)
1540 {
1541 struct g_virstor_softc *sc;
1542 struct bio *parent_b;
1543
1544 parent_b = b->bio_parent;
1545 sc = parent_b->bio_to->geom->softc;
1546
1547 if (b->bio_error != 0) {
1548 LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
1549 b->bio_error, b->bio_offset, b->bio_length,
1550 b->bio_to->name);
1551 if (parent_b->bio_error == 0)
1552 parent_b->bio_error = b->bio_error;
1553 }
1554
1555 parent_b->bio_inbed++;
1556 parent_b->bio_completed += b->bio_completed;
1557
1558 if (parent_b->bio_children == parent_b->bio_inbed) {
1559 parent_b->bio_completed = parent_b->bio_length;
1560 g_io_deliver(parent_b, parent_b->bio_error);
1561 }
1562 g_destroy_bio(b);
1563 }
1564
1565 /*
1566 * I/O starts here
1567 * Called in g_down thread
1568 */
1569 static void
1570 g_virstor_start(struct bio *b)
1571 {
1572 struct g_virstor_softc *sc;
1573 struct g_virstor_component *comp;
1574 struct bio *cb;
1575 struct g_provider *pp;
1576 char *addr;
1577 off_t offset, length;
1578 struct bio_queue_head bq;
1579 size_t chunk_size; /* cached for convenience */
1580 u_int count;
1581
1582 pp = b->bio_to;
1583 sc = pp->geom->softc;
1584 KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
1585 b->bio_to->error, b->bio_to->name));
1586
1587 LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
1588
1589 switch (b->bio_cmd) {
1590 case BIO_READ:
1591 case BIO_WRITE:
1592 case BIO_DELETE:
1593 break;
1594 default:
1595 g_io_deliver(b, EOPNOTSUPP);
1596 return;
1597 }
1598
1599 LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
1600 bioq_init(&bq);
1601
1602 chunk_size = sc->chunk_size;
1603 addr = b->bio_data;
1604 offset = b->bio_offset; /* virtual offset and length */
1605 length = b->bio_length;
1606
1607 while (length > 0) {
1608 size_t chunk_index, in_chunk_offset, in_chunk_length;
1609 struct virstor_map_entry *me;
1610
1611 chunk_index = offset / chunk_size; /* round downwards */
1612 in_chunk_offset = offset % chunk_size;
1613 in_chunk_length = min(length, chunk_size - in_chunk_offset);
1614 LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
1615 b->bio_cmd == BIO_READ ? "R" : "W",
1616 offset, length,
1617 chunk_index, in_chunk_offset, in_chunk_length);
1618 me = &sc->map[chunk_index];
1619
1620 if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
1621 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1622 /* Reads from unallocated chunks return zeroed
1623 * buffers */
1624 if (b->bio_cmd == BIO_READ)
1625 bzero(addr, in_chunk_length);
1626 } else {
1627 comp = &sc->components[me->provider_no];
1628
1629 cb = g_clone_bio(b);
1630 if (cb == NULL) {
1631 bioq_dismantle(&bq);
1632 if (b->bio_error == 0)
1633 b->bio_error = ENOMEM;
1634 g_io_deliver(b, b->bio_error);
1635 return;
1636 }
1637 cb->bio_to = comp->gcons->provider;
1638 cb->bio_done = g_virstor_done;
1639 cb->bio_offset =
1640 (off_t)me->provider_chunk * (off_t)chunk_size
1641 + in_chunk_offset;
1642 cb->bio_length = in_chunk_length;
1643 cb->bio_data = addr;
1644 cb->bio_caller1 = comp;
1645 bioq_disksort(&bq, cb);
1646 }
1647 } else { /* handle BIO_WRITE */
1648 KASSERT(b->bio_cmd == BIO_WRITE,
1649 ("%s: Unknown command %d", __func__,
1650 b->bio_cmd));
1651
1652 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1653 /* We have a virtual chunk, represented by
1654 * the "me" entry, but it's not yet allocated
1655 * (tied to) a physical chunk. So do it now. */
1656 struct virstor_map_entry *data_me;
1657 u_int phys_chunk, comp_no;
1658 off_t s_offset;
1659 int error;
1660
1661 error = allocate_chunk(sc, &comp, &comp_no,
1662 &phys_chunk);
1663 if (error != 0) {
1664 /* We cannot allocate a physical chunk
1665 * to satisfy this request, so we'll
1666 * delay it to when we can...
1667 * XXX: this will prevent the fs from
1668 * being umounted! */
1669 struct g_virstor_bio_q *biq;
1670 biq = malloc(sizeof *biq, M_GVIRSTOR,
1671 M_NOWAIT);
1672 if (biq == NULL) {
1673 bioq_dismantle(&bq);
1674 if (b->bio_error == 0)
1675 b->bio_error = ENOMEM;
1676 g_io_deliver(b, b->bio_error);
1677 return;
1678 }
1679 biq->bio = b;
1680 mtx_lock(&sc->delayed_bio_q_mtx);
1681 STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
1682 biq, linkage);
1683 mtx_unlock(&sc->delayed_bio_q_mtx);
1684 LOG_MSG(LVL_WARNING, "Delaying BIO "
1685 "(size=%ju) until free physical "
1686 "space can be found on %s",
1687 b->bio_length,
1688 sc->provider->name);
1689 return;
1690 }
1691 LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
1692 "for %s",
1693 phys_chunk,
1694 comp->gcons->provider->name,
1695 sc->provider->name);
1696
1697 me->provider_no = comp_no;
1698 me->provider_chunk = phys_chunk;
1699 me->flags |= VIRSTOR_MAP_ALLOCATED;
1700
1701 cb = g_clone_bio(b);
1702 if (cb == NULL) {
1703 me->flags &= ~VIRSTOR_MAP_ALLOCATED;
1704 me->provider_no = 0;
1705 me->provider_chunk = 0;
1706 bioq_dismantle(&bq);
1707 if (b->bio_error == 0)
1708 b->bio_error = ENOMEM;
1709 g_io_deliver(b, b->bio_error);
1710 return;
1711 }
1712
1713 /* The allocation table is stored continuously
1714 * at the start of the drive. We need to
1715 * calculate the offset of the sector that holds
1716 * this map entry both on the drive and in the
1717 * map array.
1718 * sc_offset will end up pointing to the drive
1719 * sector. */
1720 s_offset = chunk_index * sizeof *me;
1721 s_offset = rounddown(s_offset, sc->sectorsize);
1722
1723 /* data_me points to map entry sector
1724 * in memory (analogous to offset) */
1725 data_me = &sc->map[rounddown(chunk_index,
1726 sc->me_per_sector)];
1727
1728 /* Commit sector with map entry to storage */
1729 cb->bio_to = sc->components[0].gcons->provider;
1730 cb->bio_done = g_virstor_done;
1731 cb->bio_offset = s_offset;
1732 cb->bio_data = (char *)data_me;
1733 cb->bio_length = sc->sectorsize;
1734 cb->bio_caller1 = &sc->components[0];
1735 bioq_disksort(&bq, cb);
1736 }
1737
1738 comp = &sc->components[me->provider_no];
1739 cb = g_clone_bio(b);
1740 if (cb == NULL) {
1741 bioq_dismantle(&bq);
1742 if (b->bio_error == 0)
1743 b->bio_error = ENOMEM;
1744 g_io_deliver(b, b->bio_error);
1745 return;
1746 }
1747 /* Finally, handle the data */
1748 cb->bio_to = comp->gcons->provider;
1749 cb->bio_done = g_virstor_done;
1750 cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
1751 in_chunk_offset;
1752 cb->bio_length = in_chunk_length;
1753 cb->bio_data = addr;
1754 cb->bio_caller1 = comp;
1755 bioq_disksort(&bq, cb);
1756 }
1757 addr += in_chunk_length;
1758 length -= in_chunk_length;
1759 offset += in_chunk_length;
1760 }
1761
1762 /* Fire off bio's here */
1763 count = 0;
1764 for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
1765 bioq_remove(&bq, cb);
1766 LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
1767 comp = cb->bio_caller1;
1768 cb->bio_caller1 = NULL;
1769 LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
1770 cb->bio_offset, cb->bio_length);
1771 g_io_request(cb, comp->gcons);
1772 count++;
1773 }
1774 if (count == 0) { /* We handled everything locally */
1775 b->bio_completed = b->bio_length;
1776 g_io_deliver(b, 0);
1777 }
1778
1779 }
1780
1781 /*
1782 * Allocate a chunk from a physical provider. Returns physical component,
1783 * chunk index relative to the component and the component's index.
1784 */
1785 static int
1786 allocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
1787 u_int *comp_no_p, u_int *chunk)
1788 {
1789 u_int comp_no;
1790
1791 KASSERT(sc->curr_component < sc->n_components,
1792 ("%s: Invalid curr_component: %u", __func__, sc->curr_component));
1793
1794 comp_no = sc->curr_component;
1795 *comp = &sc->components[comp_no];
1796 dump_component(*comp);
1797 if ((*comp)->chunk_next >= (*comp)->chunk_count) {
1798 /* This component is full. Allocate next component */
1799 if (comp_no >= sc->n_components-1) {
1800 LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
1801 sc->geom->name);
1802 return (-1);
1803 }
1804 (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
1805 sc->curr_component = ++comp_no;
1806
1807 *comp = &sc->components[comp_no];
1808 if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
1809 LOG_MSG(LVL_WARNING, "Device %s running out of components "
1810 "(switching to %u/%u: %s)", sc->geom->name,
1811 comp_no+1, sc->n_components,
1812 (*comp)->gcons->provider->name);
1813 /* Take care not to overwrite reserved chunks */
1814 if ( (*comp)->chunk_reserved > 0 &&
1815 (*comp)->chunk_next < (*comp)->chunk_reserved)
1816 (*comp)->chunk_next = (*comp)->chunk_reserved;
1817
1818 (*comp)->flags |=
1819 VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
1820 dump_component(*comp);
1821 *comp_no_p = comp_no;
1822 *chunk = (*comp)->chunk_next++;
1823 } else {
1824 *comp_no_p = comp_no;
1825 *chunk = (*comp)->chunk_next++;
1826 }
1827 return (0);
1828 }
1829
1830 /* Dump a component */
1831 static void
1832 dump_component(struct g_virstor_component *comp)
1833 {
1834
1835 if (g_virstor_debug < LVL_DEBUG2)
1836 return;
1837 printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
1838 printf(" chunk_count: %u\n", comp->chunk_count);
1839 printf(" chunk_next: %u\n", comp->chunk_next);
1840 printf(" flags: %u\n", comp->flags);
1841 }
1842
1843 #if 0
1844 /* Dump a map entry */
1845 static void
1846 dump_me(struct virstor_map_entry *me, unsigned int nr)
1847 {
1848 if (g_virstor_debug < LVL_DEBUG)
1849 return;
1850 printf("VIRT. CHUNK #%d: ", nr);
1851 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
1852 printf("(unallocated)\n");
1853 else
1854 printf("allocated at provider %u, provider_chunk %u\n",
1855 me->provider_no, me->provider_chunk);
1856 }
1857 #endif
1858
1859 /*
1860 * Dismantle bio_queue and destroy its components
1861 */
1862 static void
1863 bioq_dismantle(struct bio_queue_head *bq)
1864 {
1865 struct bio *b;
1866
1867 for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
1868 bioq_remove(bq, b);
1869 g_destroy_bio(b);
1870 }
1871 }
1872
1873 /*
1874 * The function that shouldn't be called.
1875 * When this is called, the stack is already garbled because of
1876 * argument mismatch. There's nothing to do now but panic, which is
1877 * accidentally the whole purpose of this function.
1878 * Motivation: to guard from accidentally calling geom methods when
1879 * they shouldn't be called. (see g_..._taste)
1880 */
1881 static void
1882 invalid_call(void)
1883 {
1884 panic("invalid_call() has just been called. Something's fishy here.");
1885 }
1886
1887 DECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */
1888 MODULE_VERSION(geom_virstor, 0);
Cache object: 8f260bfb027e7b16bd3428088366f133
|