Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/geom/linux_lvm/g_linux_lvm.c
39476 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2008 Andrew Thompson <[email protected]>
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
#include <sys/cdefs.h>
30
#include <sys/ctype.h>
31
#include <sys/param.h>
32
#include <sys/bio.h>
33
#include <sys/kernel.h>
34
#include <sys/limits.h>
35
#include <sys/malloc.h>
36
#include <sys/queue.h>
37
#include <sys/sysctl.h>
38
#include <sys/systm.h>
39
40
#include <geom/geom.h>
41
#include <geom/geom_dbg.h>
42
#include <sys/endian.h>
43
44
#include <geom/linux_lvm/g_linux_lvm.h>
45
46
FEATURE(geom_linux_lvm, "GEOM Linux LVM partitioning support");
47
48
/* Declare malloc(9) label */
49
static MALLOC_DEFINE(M_GLLVM, "gllvm", "GEOM_LINUX_LVM Data");
50
51
/* GEOM class methods */
52
static g_access_t g_llvm_access;
53
static g_init_t g_llvm_init;
54
static g_orphan_t g_llvm_orphan;
55
static g_orphan_t g_llvm_taste_orphan;
56
static g_start_t g_llvm_start;
57
static g_taste_t g_llvm_taste;
58
static g_ctl_destroy_geom_t g_llvm_destroy_geom;
59
60
static void g_llvm_done(struct bio *);
61
static void g_llvm_remove_disk(struct g_llvm_vg *, struct g_consumer *);
62
static int g_llvm_activate_lv(struct g_llvm_vg *, struct g_llvm_lv *);
63
static int g_llvm_add_disk(struct g_llvm_vg *, struct g_provider *, char *);
64
static void g_llvm_free_vg(struct g_llvm_vg *);
65
static int g_llvm_destroy(struct g_llvm_vg *, int);
66
static int g_llvm_read_label(struct g_consumer *, struct g_llvm_label *);
67
static int g_llvm_read_md(struct g_consumer *, struct g_llvm_metadata *,
68
struct g_llvm_label *);
69
70
static int llvm_label_decode(const u_char *, struct g_llvm_label *,
71
int, u_int);
72
static int llvm_md_decode(const u_char *, struct g_llvm_metadata *,
73
struct g_llvm_label *);
74
static int llvm_textconf_decode(u_char *, int,
75
struct g_llvm_metadata *);
76
static int llvm_textconf_decode_pv(char **, char *, struct g_llvm_vg *);
77
static int llvm_textconf_decode_lv(char **, char *, struct g_llvm_vg *);
78
static int llvm_textconf_decode_sg(char **, char *, struct g_llvm_lv *);
79
80
SYSCTL_DECL(_kern_geom);
81
SYSCTL_NODE(_kern_geom, OID_AUTO, linux_lvm, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
82
"GEOM_LINUX_LVM stuff");
83
static u_int g_llvm_debug = 0;
84
SYSCTL_UINT(_kern_geom_linux_lvm, OID_AUTO, debug, CTLFLAG_RWTUN, &g_llvm_debug, 0,
85
"Debug level");
86
87
LIST_HEAD(, g_llvm_vg) vg_list;
88
89
/*
90
* Called to notify geom when it's been opened, and for what intent
91
*/
92
static int
93
g_llvm_access(struct g_provider *pp, int dr, int dw, int de)
94
{
95
struct g_consumer *c;
96
struct g_llvm_vg *vg;
97
struct g_geom *gp;
98
int error;
99
100
KASSERT(pp != NULL, ("%s: NULL provider", __func__));
101
gp = pp->geom;
102
KASSERT(gp != NULL, ("%s: NULL geom", __func__));
103
vg = gp->softc;
104
105
if (vg == NULL) {
106
/* It seems that .access can be called with negative dr,dw,dx
107
* in this case but I want to check for myself */
108
G_LLVM_DEBUG(0, "access(%d, %d, %d) for %s",
109
dr, dw, de, pp->name);
110
111
/* This should only happen when geom is withered so
112
* allow only negative requests */
113
KASSERT(dr <= 0 && dw <= 0 && de <= 0,
114
("%s: Positive access for %s", __func__, pp->name));
115
if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
116
G_LLVM_DEBUG(0,
117
"Device %s definitely destroyed", pp->name);
118
return (0);
119
}
120
121
/* Grab an exclusive bit to propagate on our consumers on first open */
122
if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
123
de++;
124
/* ... drop it on close */
125
if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
126
de--;
127
128
error = ENXIO;
129
LIST_FOREACH(c, &gp->consumer, consumer) {
130
KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
131
error = g_access(c, dr, dw, de);
132
if (error != 0) {
133
struct g_consumer *c2;
134
135
/* Backout earlier changes */
136
LIST_FOREACH(c2, &gp->consumer, consumer) {
137
if (c2 == c) /* all eariler components fixed */
138
return (error);
139
g_access(c2, -dr, -dw, -de);
140
}
141
}
142
}
143
144
return (error);
145
}
146
147
/*
148
* Dismantle bio_queue and destroy its components
149
*/
150
static void
151
bioq_dismantle(struct bio_queue_head *bq)
152
{
153
struct bio *b;
154
155
for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
156
bioq_remove(bq, b);
157
g_destroy_bio(b);
158
}
159
}
160
161
/*
162
* GEOM .done handler
163
* Can't use standard handler because one requested IO may
164
* fork into additional data IOs
165
*/
166
static void
167
g_llvm_done(struct bio *b)
168
{
169
struct bio *parent_b;
170
171
parent_b = b->bio_parent;
172
173
if (b->bio_error != 0) {
174
G_LLVM_DEBUG(0, "Error %d for offset=%ju, length=%ju on %s",
175
b->bio_error, b->bio_offset, b->bio_length,
176
b->bio_to->name);
177
if (parent_b->bio_error == 0)
178
parent_b->bio_error = b->bio_error;
179
}
180
181
parent_b->bio_inbed++;
182
parent_b->bio_completed += b->bio_completed;
183
184
if (parent_b->bio_children == parent_b->bio_inbed) {
185
parent_b->bio_completed = parent_b->bio_length;
186
g_io_deliver(parent_b, parent_b->bio_error);
187
}
188
g_destroy_bio(b);
189
}
190
191
static void
192
g_llvm_start(struct bio *bp)
193
{
194
struct g_provider *pp;
195
struct g_llvm_vg *vg;
196
struct g_llvm_pv *pv;
197
struct g_llvm_lv *lv;
198
struct g_llvm_segment *sg;
199
struct bio *cb;
200
struct bio_queue_head bq;
201
size_t chunk_size;
202
off_t offset, length;
203
char *addr;
204
u_int count;
205
206
pp = bp->bio_to;
207
lv = pp->private;
208
vg = pp->geom->softc;
209
210
switch (bp->bio_cmd) {
211
case BIO_READ:
212
case BIO_WRITE:
213
case BIO_DELETE:
214
/* XXX BIO_GETATTR allowed? */
215
break;
216
default:
217
/*
218
* BIO_SPEEDUP and BIO_FLUSH should pass through to all sg
219
* elements, but aren't.
220
*/
221
g_io_deliver(bp, EOPNOTSUPP);
222
return;
223
}
224
225
bioq_init(&bq);
226
227
chunk_size = vg->vg_extentsize;
228
addr = bp->bio_data;
229
offset = bp->bio_offset; /* virtual offset and length */
230
length = bp->bio_length;
231
232
while (length > 0) {
233
size_t chunk_index, in_chunk_offset, in_chunk_length;
234
235
pv = NULL;
236
cb = g_clone_bio(bp);
237
if (cb == NULL) {
238
bioq_dismantle(&bq);
239
if (bp->bio_error == 0)
240
bp->bio_error = ENOMEM;
241
g_io_deliver(bp, bp->bio_error);
242
return;
243
}
244
245
/* get the segment and the pv */
246
if (lv->lv_sgcount == 1) {
247
/* skip much of the calculations for a single sg */
248
chunk_index = 0;
249
in_chunk_offset = 0;
250
in_chunk_length = length;
251
sg = lv->lv_firstsg;
252
pv = sg->sg_pv;
253
cb->bio_offset = offset + sg->sg_pvoffset;
254
} else {
255
chunk_index = offset / chunk_size; /* round downwards */
256
in_chunk_offset = offset % chunk_size;
257
in_chunk_length =
258
min(length, chunk_size - in_chunk_offset);
259
260
/* XXX could be faster */
261
LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
262
if (chunk_index >= sg->sg_start &&
263
chunk_index <= sg->sg_end) {
264
/* adjust chunk index for sg start */
265
chunk_index -= sg->sg_start;
266
pv = sg->sg_pv;
267
break;
268
}
269
}
270
cb->bio_offset =
271
(off_t)chunk_index * (off_t)chunk_size
272
+ in_chunk_offset + sg->sg_pvoffset;
273
}
274
275
KASSERT(pv != NULL, ("Can't find PV for chunk %zu",
276
chunk_index));
277
278
cb->bio_to = pv->pv_gprov;
279
cb->bio_done = g_llvm_done;
280
cb->bio_length = in_chunk_length;
281
cb->bio_data = addr;
282
cb->bio_caller1 = pv;
283
bioq_disksort(&bq, cb);
284
285
G_LLVM_DEBUG(5,
286
"Mapped %s(%ju, %ju) on %s to %zu(%zu,%zu) @ %s:%ju",
287
bp->bio_cmd == BIO_READ ? "R" : "W",
288
offset, length, lv->lv_name,
289
chunk_index, in_chunk_offset, in_chunk_length,
290
pv->pv_name, cb->bio_offset);
291
292
addr += in_chunk_length;
293
length -= in_chunk_length;
294
offset += in_chunk_length;
295
}
296
297
/* Fire off bio's here */
298
count = 0;
299
for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
300
bioq_remove(&bq, cb);
301
pv = cb->bio_caller1;
302
cb->bio_caller1 = NULL;
303
G_LLVM_DEBUG(6, "firing bio to %s, offset=%ju, length=%ju",
304
cb->bio_to->name, cb->bio_offset, cb->bio_length);
305
g_io_request(cb, pv->pv_gcons);
306
count++;
307
}
308
if (count == 0) { /* We handled everything locally */
309
bp->bio_completed = bp->bio_length;
310
g_io_deliver(bp, 0);
311
}
312
}
313
314
static void
315
g_llvm_remove_disk(struct g_llvm_vg *vg, struct g_consumer *cp)
316
{
317
struct g_llvm_pv *pv;
318
struct g_llvm_lv *lv;
319
struct g_llvm_segment *sg;
320
int found;
321
322
KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
323
pv = (struct g_llvm_pv *)cp->private;
324
325
G_LLVM_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
326
pv->pv_name);
327
328
LIST_FOREACH(lv, &vg->vg_lvs, lv_next) {
329
/* Find segments that map to this disk */
330
found = 0;
331
LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
332
if (sg->sg_pv == pv) {
333
sg->sg_pv = NULL;
334
lv->lv_sgactive--;
335
found = 1;
336
break;
337
}
338
}
339
if (found) {
340
G_LLVM_DEBUG(0, "Device %s removed.",
341
lv->lv_gprov->name);
342
g_wither_provider(lv->lv_gprov, ENXIO);
343
lv->lv_gprov = NULL;
344
}
345
}
346
347
if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
348
g_access(cp, -cp->acr, -cp->acw, -cp->ace);
349
g_detach(cp);
350
g_destroy_consumer(cp);
351
}
352
353
static void
354
g_llvm_orphan(struct g_consumer *cp)
355
{
356
struct g_llvm_vg *vg;
357
struct g_geom *gp;
358
359
g_topology_assert();
360
gp = cp->geom;
361
vg = gp->softc;
362
if (vg == NULL)
363
return;
364
365
g_llvm_remove_disk(vg, cp);
366
g_llvm_destroy(vg, 1);
367
}
368
369
static int
370
g_llvm_activate_lv(struct g_llvm_vg *vg, struct g_llvm_lv *lv)
371
{
372
struct g_geom *gp;
373
struct g_provider *pp;
374
375
g_topology_assert();
376
377
KASSERT(lv->lv_sgactive == lv->lv_sgcount, ("segment missing"));
378
379
gp = vg->vg_geom;
380
pp = g_new_providerf(gp, "linux_lvm/%s-%s", vg->vg_name, lv->lv_name);
381
pp->mediasize = vg->vg_extentsize * (off_t)lv->lv_extentcount;
382
pp->sectorsize = vg->vg_sectorsize;
383
g_error_provider(pp, 0);
384
lv->lv_gprov = pp;
385
pp->private = lv;
386
387
G_LLVM_DEBUG(1, "Created %s, %juM", pp->name,
388
pp->mediasize / (1024*1024));
389
390
return (0);
391
}
392
393
static int
394
g_llvm_add_disk(struct g_llvm_vg *vg, struct g_provider *pp, char *uuid)
395
{
396
struct g_geom *gp;
397
struct g_consumer *cp, *fcp;
398
struct g_llvm_pv *pv;
399
struct g_llvm_lv *lv;
400
struct g_llvm_segment *sg;
401
int error;
402
403
g_topology_assert();
404
405
LIST_FOREACH(pv, &vg->vg_pvs, pv_next) {
406
if (strcmp(pv->pv_uuid, uuid) == 0)
407
break; /* found it */
408
}
409
if (pv == NULL) {
410
G_LLVM_DEBUG(3, "uuid %s not found in pv list", uuid);
411
return (ENOENT);
412
}
413
if (pv->pv_gprov != NULL) {
414
G_LLVM_DEBUG(0, "disk %s already initialised in %s",
415
pv->pv_name, vg->vg_name);
416
return (EEXIST);
417
}
418
419
pv->pv_start *= vg->vg_sectorsize;
420
gp = vg->vg_geom;
421
fcp = LIST_FIRST(&gp->consumer);
422
423
cp = g_new_consumer(gp);
424
error = g_attach(cp, pp);
425
G_LLVM_DEBUG(1, "Attached %s to %s at offset %ju",
426
pp->name, pv->pv_name, pv->pv_start);
427
428
if (error != 0) {
429
G_LLVM_DEBUG(0, "cannot attach %s to %s",
430
pp->name, vg->vg_name);
431
g_destroy_consumer(cp);
432
return (error);
433
}
434
435
if (fcp != NULL) {
436
if (fcp->provider->sectorsize != pp->sectorsize) {
437
G_LLVM_DEBUG(0, "Provider %s of %s has invalid "
438
"sector size (%d)", pp->name, vg->vg_name,
439
pp->sectorsize);
440
return (EINVAL);
441
}
442
if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
443
/* Replicate access permissions from first "live"
444
* consumer to the new one */
445
error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
446
if (error != 0) {
447
g_detach(cp);
448
g_destroy_consumer(cp);
449
return (error);
450
}
451
}
452
}
453
454
cp->private = pv;
455
pv->pv_gcons = cp;
456
pv->pv_gprov = pp;
457
458
LIST_FOREACH(lv, &vg->vg_lvs, lv_next) {
459
/* Find segments that map to this disk */
460
LIST_FOREACH(sg, &lv->lv_segs, sg_next) {
461
if (strcmp(sg->sg_pvname, pv->pv_name) == 0) {
462
/* avtivate the segment */
463
KASSERT(sg->sg_pv == NULL,
464
("segment already mapped"));
465
sg->sg_pvoffset =
466
(off_t)sg->sg_pvstart * vg->vg_extentsize
467
+ pv->pv_start;
468
sg->sg_pv = pv;
469
lv->lv_sgactive++;
470
471
G_LLVM_DEBUG(2, "%s: %d to %d @ %s:%d"
472
" offset %ju sector %ju",
473
lv->lv_name, sg->sg_start, sg->sg_end,
474
sg->sg_pvname, sg->sg_pvstart,
475
sg->sg_pvoffset,
476
sg->sg_pvoffset / vg->vg_sectorsize);
477
}
478
}
479
/* Activate any lvs waiting on this disk */
480
if (lv->lv_gprov == NULL && lv->lv_sgactive == lv->lv_sgcount) {
481
error = g_llvm_activate_lv(vg, lv);
482
if (error)
483
break;
484
}
485
}
486
return (error);
487
}
488
489
static void
490
g_llvm_init(struct g_class *mp)
491
{
492
LIST_INIT(&vg_list);
493
}
494
495
static void
496
g_llvm_free_vg(struct g_llvm_vg *vg)
497
{
498
struct g_llvm_pv *pv;
499
struct g_llvm_lv *lv;
500
struct g_llvm_segment *sg;
501
502
/* Free all the structures */
503
while ((pv = LIST_FIRST(&vg->vg_pvs)) != NULL) {
504
LIST_REMOVE(pv, pv_next);
505
free(pv, M_GLLVM);
506
}
507
while ((lv = LIST_FIRST(&vg->vg_lvs)) != NULL) {
508
while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
509
LIST_REMOVE(sg, sg_next);
510
free(sg, M_GLLVM);
511
}
512
LIST_REMOVE(lv, lv_next);
513
free(lv, M_GLLVM);
514
}
515
free(vg, M_GLLVM);
516
}
517
518
static void
519
g_llvm_taste_orphan(struct g_consumer *cp)
520
{
521
522
KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
523
cp->provider->name));
524
}
525
526
static struct g_geom *
527
g_llvm_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
528
{
529
struct g_consumer *cp;
530
struct g_geom *gp;
531
struct g_llvm_label ll;
532
struct g_llvm_metadata md;
533
struct g_llvm_vg *vg;
534
int error;
535
536
bzero(&md, sizeof(md));
537
538
g_topology_assert();
539
g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
540
gp = g_new_geom(mp, "linux_lvm:taste");
541
/* This orphan function should be never called. */
542
gp->orphan = g_llvm_taste_orphan;
543
cp = g_new_consumer(gp);
544
cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
545
error = g_attach(cp, pp);
546
if (error == 0) {
547
error = g_llvm_read_label(cp, &ll);
548
if (error == 0)
549
error = g_llvm_read_md(cp, &md, &ll);
550
g_detach(cp);
551
}
552
g_destroy_consumer(cp);
553
g_destroy_geom(gp);
554
if (error != 0)
555
return (NULL);
556
557
vg = md.md_vg;
558
if (vg->vg_geom == NULL) {
559
/* new volume group */
560
gp = g_new_geom(mp, vg->vg_name);
561
gp->start = g_llvm_start;
562
gp->spoiled = g_llvm_orphan;
563
gp->orphan = g_llvm_orphan;
564
gp->access = g_llvm_access;
565
vg->vg_sectorsize = pp->sectorsize;
566
vg->vg_extentsize *= vg->vg_sectorsize;
567
vg->vg_geom = gp;
568
gp->softc = vg;
569
G_LLVM_DEBUG(1, "Created volume %s, extent size %zuK",
570
vg->vg_name, vg->vg_extentsize / 1024);
571
}
572
573
/* initialise this disk in the volume group */
574
g_llvm_add_disk(vg, pp, ll.ll_uuid);
575
return (vg->vg_geom);
576
}
577
578
static int
579
g_llvm_destroy(struct g_llvm_vg *vg, int force)
580
{
581
struct g_provider *pp;
582
struct g_geom *gp;
583
584
g_topology_assert();
585
if (vg == NULL)
586
return (ENXIO);
587
gp = vg->vg_geom;
588
589
LIST_FOREACH(pp, &gp->provider, provider) {
590
if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
591
G_LLVM_DEBUG(1, "Device %s is still open (r%dw%de%d)",
592
pp->name, pp->acr, pp->acw, pp->ace);
593
if (!force)
594
return (EBUSY);
595
}
596
}
597
598
LIST_REMOVE(vg, vg_next);
599
g_llvm_free_vg(vg);
600
gp->softc = NULL;
601
g_wither_geom(gp, ENXIO);
602
return (0);
603
}
604
605
static int
606
g_llvm_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
607
struct g_geom *gp)
608
{
609
struct g_llvm_vg *vg;
610
611
vg = gp->softc;
612
return (g_llvm_destroy(vg, 0));
613
}
614
615
int
616
g_llvm_read_label(struct g_consumer *cp, struct g_llvm_label *ll)
617
{
618
struct g_provider *pp;
619
u_char *buf;
620
int i, error = 0;
621
622
g_topology_assert();
623
624
/* The LVM label is stored on the first four sectors */
625
error = g_access(cp, 1, 0, 0);
626
if (error != 0)
627
return (error);
628
pp = cp->provider;
629
g_topology_unlock();
630
buf = g_read_data(cp, 0, pp->sectorsize * 4, &error);
631
g_topology_lock();
632
g_access(cp, -1, 0, 0);
633
if (buf == NULL) {
634
G_LLVM_DEBUG(1, "Cannot read metadata from %s (error=%d)",
635
pp->name, error);
636
return (error);
637
}
638
639
/* Search the four sectors for the LVM label. */
640
for (i = 0; i < 4; i++) {
641
error = llvm_label_decode(&buf[i * pp->sectorsize], ll, i,
642
pp->sectorsize);
643
if (error == 0)
644
break; /* found it */
645
}
646
g_free(buf);
647
return (error);
648
}
649
650
int
651
g_llvm_read_md(struct g_consumer *cp, struct g_llvm_metadata *md,
652
struct g_llvm_label *ll)
653
{
654
struct g_provider *pp;
655
u_char *buf;
656
int error;
657
int size;
658
659
g_topology_assert();
660
661
error = g_access(cp, 1, 0, 0);
662
if (error != 0)
663
return (error);
664
pp = cp->provider;
665
g_topology_unlock();
666
buf = g_read_data(cp, ll->ll_md_offset, pp->sectorsize, &error);
667
g_topology_lock();
668
g_access(cp, -1, 0, 0);
669
if (buf == NULL) {
670
G_LLVM_DEBUG(0, "Cannot read metadata from %s (error=%d)",
671
cp->provider->name, error);
672
return (error);
673
}
674
675
error = llvm_md_decode(buf, md, ll);
676
g_free(buf);
677
if (error != 0) {
678
return (error);
679
}
680
681
G_LLVM_DEBUG(1, "reading LVM2 config @ %s:%ju", pp->name,
682
ll->ll_md_offset + md->md_reloffset);
683
error = g_access(cp, 1, 0, 0);
684
if (error != 0)
685
return (error);
686
pp = cp->provider;
687
g_topology_unlock();
688
/* round up to the nearest sector */
689
size = md->md_relsize +
690
(pp->sectorsize - md->md_relsize % pp->sectorsize);
691
buf = g_read_data(cp, ll->ll_md_offset + md->md_reloffset, size, &error);
692
g_topology_lock();
693
g_access(cp, -1, 0, 0);
694
if (buf == NULL) {
695
G_LLVM_DEBUG(0, "Cannot read LVM2 config from %s (error=%d)",
696
pp->name, error);
697
return (error);
698
}
699
buf[md->md_relsize] = '\0';
700
G_LLVM_DEBUG(10, "LVM config:\n%s\n", buf);
701
error = llvm_textconf_decode(buf, md->md_relsize, md);
702
g_free(buf);
703
704
return (error);
705
}
706
707
static int
708
llvm_label_decode(const u_char *data, struct g_llvm_label *ll, int sector,
709
u_int sectorsize)
710
{
711
uint64_t off;
712
char *uuid;
713
714
/* Magic string */
715
if (bcmp("LABELONE", data , 8) != 0)
716
return (EINVAL);
717
718
/* We only support LVM2 text format */
719
if (bcmp("LVM2 001", data + 24, 8) != 0) {
720
G_LLVM_DEBUG(0, "Unsupported LVM format");
721
return (EINVAL);
722
}
723
724
ll->ll_sector = le64dec(data + 8);
725
ll->ll_crc = le32dec(data + 16);
726
ll->ll_offset = le32dec(data + 20);
727
728
if (ll->ll_sector != sector) {
729
G_LLVM_DEBUG(0, "Expected sector %ju, found at %d",
730
ll->ll_sector, sector);
731
return (EINVAL);
732
}
733
734
/* XXX The minimal possible size of physical volume header is 88 */
735
if (ll->ll_offset < 32 || ll->ll_offset > sectorsize - 88) {
736
G_LLVM_DEBUG(0, "Invalid physical volume header offset %u",
737
ll->ll_offset);
738
return (EINVAL);
739
}
740
741
off = ll->ll_offset;
742
/*
743
* convert the binary uuid to string format, the format is
744
* xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx (6-4-4-4-4-4-6)
745
*/
746
uuid = ll->ll_uuid;
747
bcopy(data + off, uuid, 6);
748
off += 6;
749
uuid += 6;
750
*uuid++ = '-';
751
for (int i = 0; i < 5; i++) {
752
bcopy(data + off, uuid, 4);
753
off += 4;
754
uuid += 4;
755
*uuid++ = '-';
756
}
757
bcopy(data + off, uuid, 6);
758
off += 6;
759
uuid += 6;
760
*uuid++ = '\0';
761
762
ll->ll_size = le64dec(data + off);
763
off += 8;
764
ll->ll_pestart = le64dec(data + off);
765
off += 16;
766
767
/* Only one data section is supported */
768
if (le64dec(data + off) != 0) {
769
G_LLVM_DEBUG(0, "Only one data section supported");
770
return (EINVAL);
771
}
772
773
off += 16;
774
ll->ll_md_offset = le64dec(data + off);
775
off += 8;
776
ll->ll_md_size = le64dec(data + off);
777
off += 8;
778
779
G_LLVM_DEBUG(1, "LVM metadata: offset=%ju, size=%ju", ll->ll_md_offset,
780
ll->ll_md_size);
781
782
/* Only one data section is supported */
783
if (le64dec(data + off) != 0) {
784
G_LLVM_DEBUG(0, "Only one metadata section supported");
785
return (EINVAL);
786
}
787
788
G_LLVM_DEBUG(2, "label uuid=%s", ll->ll_uuid);
789
G_LLVM_DEBUG(2, "sector=%ju, crc=%u, offset=%u, size=%ju, pestart=%ju",
790
ll->ll_sector, ll->ll_crc, ll->ll_offset, ll->ll_size,
791
ll->ll_pestart);
792
793
return (0);
794
}
795
796
static int
797
llvm_md_decode(const u_char *data, struct g_llvm_metadata *md,
798
struct g_llvm_label *ll)
799
{
800
uint64_t off;
801
char magic[16];
802
803
off = 0;
804
md->md_csum = le32dec(data + off);
805
off += 4;
806
bcopy(data + off, magic, 16);
807
off += 16;
808
md->md_version = le32dec(data + off);
809
off += 4;
810
md->md_start = le64dec(data + off);
811
off += 8;
812
md->md_size = le64dec(data + off);
813
off += 8;
814
815
if (bcmp(G_LLVM_MAGIC, magic, 16) != 0) {
816
G_LLVM_DEBUG(0, "Incorrect md magic number");
817
return (EINVAL);
818
}
819
if (md->md_version != 1) {
820
G_LLVM_DEBUG(0, "Incorrect md version number (%u)",
821
md->md_version);
822
return (EINVAL);
823
}
824
if (md->md_start != ll->ll_md_offset) {
825
G_LLVM_DEBUG(0, "Incorrect md offset (%ju)", md->md_start);
826
return (EINVAL);
827
}
828
829
/* Aparently only one is ever returned */
830
md->md_reloffset = le64dec(data + off);
831
off += 8;
832
md->md_relsize = le64dec(data + off);
833
off += 16; /* XXX skipped checksum */
834
835
if (le64dec(data + off) != 0) {
836
G_LLVM_DEBUG(0, "Only one reloc supported");
837
return (EINVAL);
838
}
839
840
G_LLVM_DEBUG(3, "reloc: offset=%ju, size=%ju",
841
md->md_reloffset, md->md_relsize);
842
G_LLVM_DEBUG(3, "md: version=%u, start=%ju, size=%ju",
843
md->md_version, md->md_start, md->md_size);
844
845
return (0);
846
}
847
848
#define GRAB_INT(key, tok1, tok2, v) \
849
if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) { \
850
v = strtol(tok2, &tok1, 10); \
851
if (tok1 == tok2) \
852
/* strtol did not eat any of the buffer */ \
853
goto bad; \
854
continue; \
855
}
856
857
#define GRAB_STR(key, tok1, tok2, v, len) \
858
if (tok1 && tok2 && strncmp(tok1, key, sizeof(key)) == 0) { \
859
strsep(&tok2, "\""); \
860
if (tok2 == NULL) \
861
continue; \
862
tok1 = strsep(&tok2, "\""); \
863
if (tok2 == NULL) \
864
continue; \
865
strncpy(v, tok1, len); \
866
continue; \
867
}
868
869
#define SPLIT(key, value, str) \
870
key = strsep(&value, str); \
871
/* strip trailing whitespace on the key */ \
872
for (char *t = key; *t != '\0'; t++) \
873
if (isspace(*t)) { \
874
*t = '\0'; \
875
break; \
876
}
877
878
static size_t
879
llvm_grab_name(char *name, const char *tok)
880
{
881
size_t len;
882
883
len = 0;
884
if (tok == NULL)
885
return (0);
886
if (tok[0] == '-')
887
return (0);
888
if (strcmp(tok, ".") == 0 || strcmp(tok, "..") == 0)
889
return (0);
890
while (tok[len] && (isalpha(tok[len]) || isdigit(tok[len]) ||
891
tok[len] == '.' || tok[len] == '_' || tok[len] == '-' ||
892
tok[len] == '+') && len < G_LLVM_NAMELEN - 1)
893
len++;
894
bcopy(tok, name, len);
895
name[len] = '\0';
896
return (len);
897
}
898
899
static int
900
llvm_textconf_decode(u_char *data, int buflen, struct g_llvm_metadata *md)
901
{
902
struct g_llvm_vg *vg;
903
char *buf = data;
904
char *tok, *v;
905
char name[G_LLVM_NAMELEN];
906
char uuid[G_LLVM_UUIDLEN];
907
size_t len;
908
909
if (buf == NULL || *buf == '\0')
910
return (EINVAL);
911
912
tok = strsep(&buf, "\n");
913
if (tok == NULL)
914
return (EINVAL);
915
len = llvm_grab_name(name, tok);
916
if (len == 0)
917
return (EINVAL);
918
919
/* check too see if the vg has already been loaded off another disk */
920
LIST_FOREACH(vg, &vg_list, vg_next) {
921
if (strcmp(vg->vg_name, name) == 0) {
922
uuid[0] = '\0';
923
/* grab the volume group uuid */
924
while ((tok = strsep(&buf, "\n")) != NULL) {
925
if (strstr(tok, "{"))
926
break;
927
if (strstr(tok, "=")) {
928
SPLIT(v, tok, "=");
929
GRAB_STR("id", v, tok, uuid,
930
sizeof(uuid));
931
}
932
}
933
if (strcmp(vg->vg_uuid, uuid) == 0) {
934
/* existing vg */
935
md->md_vg = vg;
936
return (0);
937
}
938
/* XXX different volume group with name clash! */
939
G_LLVM_DEBUG(0,
940
"%s already exists, volume group not loaded", name);
941
return (EINVAL);
942
}
943
}
944
945
vg = malloc(sizeof(*vg), M_GLLVM, M_NOWAIT|M_ZERO);
946
if (vg == NULL)
947
return (ENOMEM);
948
949
strncpy(vg->vg_name, name, sizeof(vg->vg_name));
950
LIST_INIT(&vg->vg_pvs);
951
LIST_INIT(&vg->vg_lvs);
952
953
#define VOL_FOREACH(func, tok, buf, p) \
954
while ((tok = strsep(buf, "\n")) != NULL) { \
955
if (strstr(tok, "{")) { \
956
func(buf, tok, p); \
957
continue; \
958
} \
959
if (strstr(tok, "}")) \
960
break; \
961
}
962
963
while ((tok = strsep(&buf, "\n")) != NULL) {
964
if (strcmp(tok, "physical_volumes {") == 0) {
965
VOL_FOREACH(llvm_textconf_decode_pv, tok, &buf, vg);
966
continue;
967
}
968
if (strcmp(tok, "logical_volumes {") == 0) {
969
VOL_FOREACH(llvm_textconf_decode_lv, tok, &buf, vg);
970
continue;
971
}
972
if (strstr(tok, "{")) {
973
G_LLVM_DEBUG(2, "unknown section %s", tok);
974
continue;
975
}
976
977
/* parse 'key = value' lines */
978
if (strstr(tok, "=")) {
979
SPLIT(v, tok, "=");
980
GRAB_STR("id", v, tok, vg->vg_uuid, sizeof(vg->vg_uuid));
981
GRAB_INT("extent_size", v, tok, vg->vg_extentsize);
982
continue;
983
}
984
}
985
/* basic checking */
986
if (vg->vg_extentsize == 0)
987
goto bad;
988
989
md->md_vg = vg;
990
LIST_INSERT_HEAD(&vg_list, vg, vg_next);
991
G_LLVM_DEBUG(3, "vg: name=%s uuid=%s", vg->vg_name, vg->vg_uuid);
992
return(0);
993
994
bad:
995
g_llvm_free_vg(vg);
996
return (-1);
997
}
998
#undef VOL_FOREACH
999
1000
static int
1001
llvm_textconf_decode_pv(char **buf, char *tok, struct g_llvm_vg *vg)
1002
{
1003
struct g_llvm_pv *pv;
1004
char *v;
1005
size_t len;
1006
1007
if (*buf == NULL || **buf == '\0')
1008
return (EINVAL);
1009
1010
pv = malloc(sizeof(*pv), M_GLLVM, M_NOWAIT|M_ZERO);
1011
if (pv == NULL)
1012
return (ENOMEM);
1013
1014
pv->pv_vg = vg;
1015
len = 0;
1016
if (tok == NULL)
1017
goto bad;
1018
len = llvm_grab_name(pv->pv_name, tok);
1019
if (len == 0)
1020
goto bad;
1021
1022
while ((tok = strsep(buf, "\n")) != NULL) {
1023
if (strstr(tok, "{"))
1024
goto bad;
1025
1026
if (strstr(tok, "}"))
1027
break;
1028
1029
/* parse 'key = value' lines */
1030
if (strstr(tok, "=")) {
1031
SPLIT(v, tok, "=");
1032
GRAB_STR("id", v, tok, pv->pv_uuid, sizeof(pv->pv_uuid));
1033
GRAB_INT("pe_start", v, tok, pv->pv_start);
1034
GRAB_INT("pe_count", v, tok, pv->pv_count);
1035
continue;
1036
}
1037
}
1038
if (tok == NULL)
1039
goto bad;
1040
/* basic checking */
1041
if (pv->pv_count == 0)
1042
goto bad;
1043
1044
LIST_INSERT_HEAD(&vg->vg_pvs, pv, pv_next);
1045
G_LLVM_DEBUG(3, "pv: name=%s uuid=%s", pv->pv_name, pv->pv_uuid);
1046
1047
return (0);
1048
bad:
1049
free(pv, M_GLLVM);
1050
return (-1);
1051
}
1052
1053
static int
1054
llvm_textconf_decode_lv(char **buf, char *tok, struct g_llvm_vg *vg)
1055
{
1056
struct g_llvm_lv *lv;
1057
struct g_llvm_segment *sg;
1058
char *v;
1059
size_t len;
1060
1061
if (*buf == NULL || **buf == '\0')
1062
return (EINVAL);
1063
1064
lv = malloc(sizeof(*lv), M_GLLVM, M_NOWAIT|M_ZERO);
1065
if (lv == NULL)
1066
return (ENOMEM);
1067
1068
lv->lv_vg = vg;
1069
LIST_INIT(&lv->lv_segs);
1070
1071
if (tok == NULL)
1072
goto bad;
1073
len = llvm_grab_name(lv->lv_name, tok);
1074
if (len == 0)
1075
goto bad;
1076
1077
while ((tok = strsep(buf, "\n")) != NULL) {
1078
if (strstr(tok, "{")) {
1079
if (strstr(tok, "segment")) {
1080
llvm_textconf_decode_sg(buf, tok, lv);
1081
continue;
1082
} else
1083
/* unexpected section */
1084
goto bad;
1085
}
1086
1087
if (strstr(tok, "}"))
1088
break;
1089
1090
/* parse 'key = value' lines */
1091
if (strstr(tok, "=")) {
1092
SPLIT(v, tok, "=");
1093
GRAB_STR("id", v, tok, lv->lv_uuid, sizeof(lv->lv_uuid));
1094
GRAB_INT("segment_count", v, tok, lv->lv_sgcount);
1095
continue;
1096
}
1097
}
1098
if (tok == NULL)
1099
goto bad;
1100
if (lv->lv_sgcount == 0 || lv->lv_sgcount != lv->lv_numsegs)
1101
/* zero or incomplete segment list */
1102
goto bad;
1103
1104
/* Optimize for only one segment on the pv */
1105
lv->lv_firstsg = LIST_FIRST(&lv->lv_segs);
1106
LIST_INSERT_HEAD(&vg->vg_lvs, lv, lv_next);
1107
G_LLVM_DEBUG(3, "lv: name=%s uuid=%s", lv->lv_name, lv->lv_uuid);
1108
1109
return (0);
1110
bad:
1111
while ((sg = LIST_FIRST(&lv->lv_segs)) != NULL) {
1112
LIST_REMOVE(sg, sg_next);
1113
free(sg, M_GLLVM);
1114
}
1115
free(lv, M_GLLVM);
1116
return (-1);
1117
}
1118
1119
static int
1120
llvm_textconf_decode_sg(char **buf, char *tok, struct g_llvm_lv *lv)
1121
{
1122
struct g_llvm_segment *sg;
1123
char *v;
1124
int count = 0;
1125
1126
if (*buf == NULL || **buf == '\0')
1127
return (EINVAL);
1128
1129
sg = malloc(sizeof(*sg), M_GLLVM, M_NOWAIT|M_ZERO);
1130
if (sg == NULL)
1131
return (ENOMEM);
1132
1133
while ((tok = strsep(buf, "\n")) != NULL) {
1134
/* only a single linear stripe is supported */
1135
if (strstr(tok, "stripe_count")) {
1136
SPLIT(v, tok, "=");
1137
GRAB_INT("stripe_count", v, tok, count);
1138
if (count != 1)
1139
goto bad;
1140
}
1141
1142
if (strstr(tok, "{"))
1143
goto bad;
1144
1145
if (strstr(tok, "}"))
1146
break;
1147
1148
if (strcmp(tok, "stripes = [") == 0) {
1149
tok = strsep(buf, "\n");
1150
if (tok == NULL)
1151
goto bad;
1152
1153
strsep(&tok, "\"");
1154
if (tok == NULL)
1155
goto bad; /* missing open quotes */
1156
v = strsep(&tok, "\"");
1157
if (tok == NULL)
1158
goto bad; /* missing close quotes */
1159
strncpy(sg->sg_pvname, v, sizeof(sg->sg_pvname));
1160
if (*tok != ',')
1161
goto bad; /* missing comma for stripe */
1162
tok++;
1163
1164
sg->sg_pvstart = strtol(tok, &v, 10);
1165
if (v == tok)
1166
/* strtol did not eat any of the buffer */
1167
goto bad;
1168
1169
continue;
1170
}
1171
1172
/* parse 'key = value' lines */
1173
if (strstr(tok, "=")) {
1174
SPLIT(v, tok, "=");
1175
GRAB_INT("start_extent", v, tok, sg->sg_start);
1176
GRAB_INT("extent_count", v, tok, sg->sg_count);
1177
continue;
1178
}
1179
}
1180
if (tok == NULL)
1181
goto bad;
1182
/* basic checking */
1183
if (count != 1 || sg->sg_count == 0)
1184
goto bad;
1185
1186
sg->sg_end = sg->sg_start + sg->sg_count - 1;
1187
lv->lv_numsegs++;
1188
lv->lv_extentcount += sg->sg_count;
1189
LIST_INSERT_HEAD(&lv->lv_segs, sg, sg_next);
1190
1191
return (0);
1192
bad:
1193
free(sg, M_GLLVM);
1194
return (-1);
1195
}
1196
#undef GRAB_INT
1197
#undef GRAB_STR
1198
#undef SPLIT
1199
1200
static struct g_class g_llvm_class = {
1201
.name = G_LLVM_CLASS_NAME,
1202
.version = G_VERSION,
1203
.init = g_llvm_init,
1204
.taste = g_llvm_taste,
1205
.destroy_geom = g_llvm_destroy_geom
1206
};
1207
1208
DECLARE_GEOM_CLASS(g_llvm_class, g_linux_lvm);
1209
MODULE_VERSION(geom_linux_lvm, 0);
1210
1211