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