2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/diskmbr.h>
33 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
46 #include <geom/geom.h>
47 #include <geom/part/g_part.h>
49 #include "g_part_if.h"
51 FEATURE(geom_part_ldm, "GEOM partitioning class for LDM support");
53 SYSCTL_DECL(_kern_geom_part);
54 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, ldm, CTLFLAG_RW, 0,
55 "GEOM_PART_LDM Logical Disk Manager");
57 static u_int ldm_debug = 0;
58 TUNABLE_INT("kern.geom.part.ldm.debug", &ldm_debug);
59 SYSCTL_UINT(_kern_geom_part_ldm, OID_AUTO, debug,
60 CTLFLAG_RW | CTLFLAG_TUN, &ldm_debug, 0, "Debug level");
63 * This allows access to mirrored LDM volumes. Since we do not
64 * doing mirroring here, it is not enabled by default.
66 static u_int show_mirrors = 0;
67 TUNABLE_INT("kern.geom.part.ldm.show_mirrors", &show_mirrors);
68 SYSCTL_UINT(_kern_geom_part_ldm, OID_AUTO, show_mirrors,
69 CTLFLAG_RW | CTLFLAG_TUN, &show_mirrors, 0, "Show mirrored volumes");
71 #define LDM_DEBUG(lvl, fmt, ...) do { \
72 if (ldm_debug >= (lvl)) { \
73 printf("GEOM_PART: " fmt "\n", __VA_ARGS__); \
76 #define LDM_DUMP(buf, size) do { \
77 if (ldm_debug > 1) { \
78 hexdump(buf, size, NULL, 0); \
83 * There are internal representations of LDM structures.
85 * We do not keep all fields of on-disk structures, only most useful.
86 * All numbers in an on-disk structures are in big-endian format.
90 * Private header is 512 bytes long. There are three copies on each disk.
91 * Offset and sizes are in sectors. Location of each copy:
92 * - the first offset is relative to the disk start;
93 * - the second and third offset are relative to the LDM database start.
95 * On a disk partitioned with GPT, the LDM has not first private header.
97 #define LDM_PH_MBRINDEX 0
98 #define LDM_PH_GPTINDEX 2
99 static const uint64_t ldm_ph_off[] = {6, 1856, 2047};
100 #define LDM_VERSION_2K 0x2000b
101 #define LDM_VERSION_VISTA 0x2000c
102 #define LDM_PH_VERSION_OFF 0x00c
103 #define LDM_PH_DISKGUID_OFF 0x030
104 #define LDM_PH_DGGUID_OFF 0x0b0
105 #define LDM_PH_DGNAME_OFF 0x0f0
106 #define LDM_PH_START_OFF 0x11b
107 #define LDM_PH_SIZE_OFF 0x123
108 #define LDM_PH_DB_OFF 0x12b
109 #define LDM_PH_DBSIZE_OFF 0x133
110 #define LDM_PH_TH1_OFF 0x13b
111 #define LDM_PH_TH2_OFF 0x143
112 #define LDM_PH_CONFSIZE_OFF 0x153
113 #define LDM_PH_LOGSIZE_OFF 0x15b
114 #define LDM_PH_SIGN "PRIVHEAD"
116 struct uuid disk_guid;
119 uint64_t start; /* logical disk start */
120 uint64_t size; /* logical disk size */
121 uint64_t db_offset; /* LDM database start */
122 #define LDM_DB_SIZE 2048
123 uint64_t db_size; /* LDM database size */
124 #define LDM_TH_COUNT 2
125 uint64_t th_offset[LDM_TH_COUNT]; /* TOC header offsets */
126 uint64_t conf_size; /* configuration size */
127 uint64_t log_size; /* size of log */
131 * Table of contents header is 512 bytes long.
132 * There are two identical copies at offsets from the private header.
133 * Offsets are relative to the LDM database start.
135 #define LDM_TH_SIGN "TOCBLOCK"
136 #define LDM_TH_NAME1 "config"
137 #define LDM_TH_NAME2 "log"
138 #define LDM_TH_NAME1_OFF 0x024
139 #define LDM_TH_CONF_OFF 0x02e
140 #define LDM_TH_CONFSIZE_OFF 0x036
141 #define LDM_TH_NAME2_OFF 0x046
142 #define LDM_TH_LOG_OFF 0x050
143 #define LDM_TH_LOGSIZE_OFF 0x058
145 uint64_t conf_offset; /* configuration offset */
146 uint64_t log_offset; /* log offset */
150 * LDM database header is 512 bytes long.
152 #define LDM_VMDB_SIGN "VMDB"
153 #define LDM_DB_LASTSEQ_OFF 0x004
154 #define LDM_DB_SIZE_OFF 0x008
155 #define LDM_DB_STATUS_OFF 0x010
156 #define LDM_DB_VERSION_OFF 0x012
157 #define LDM_DB_DGNAME_OFF 0x016
158 #define LDM_DB_DGGUID_OFF 0x035
160 uint32_t last_seq; /* sequence number of last VBLK */
161 uint32_t size; /* size of VBLK */
165 * The LDM database configuration section contains VMDB header and
166 * many VBLKs. Each VBLK represents a disk group, disk partition,
167 * component or volume.
169 * The most interesting for us are volumes, they are represents
170 * partitions in the GEOM_PART meaning. But volume VBLK does not
171 * contain all information needed to create GEOM provider. And we
172 * should get this information from the related VBLK. This is how
174 * Volumes <- Components <- Partitions -> Disks
176 * One volume can contain several components. In this case LDM
177 * does mirroring of volume data to each component.
179 * Also each component can contain several partitions (spanned or
183 struct ldm_component {
184 uint64_t id; /* object id */
185 uint64_t vol_id; /* parent volume object id */
188 LIST_HEAD(, ldm_partition) partitions;
189 LIST_ENTRY(ldm_component) entry;
193 uint64_t id; /* object id */
194 uint64_t size; /* volume size */
195 uint8_t number; /* used for ordering */
196 uint8_t part_type; /* partition type */
199 LIST_HEAD(, ldm_component) components;
200 LIST_ENTRY(ldm_volume) entry;
204 uint64_t id; /* object id */
205 struct uuid guid; /* disk guid */
207 LIST_ENTRY(ldm_disk) entry;
211 struct ldm_disk_group {
212 uint64_t id; /* object id */
213 struct uuid guid; /* disk group guid */
214 u_char name[32]; /* disk group name */
216 LIST_ENTRY(ldm_disk_group) entry;
220 struct ldm_partition {
221 uint64_t id; /* object id */
222 uint64_t disk_id; /* disk object id */
223 uint64_t comp_id; /* parent component object id */
224 uint64_t start; /* offset relative to disk start */
225 uint64_t offset; /* offset for spanned volumes */
226 uint64_t size; /* partition size */
228 LIST_ENTRY(ldm_partition) entry;
232 * Each VBLK is 128 bytes long and has standard 16 bytes header.
233 * Some of VBLK's fields are fixed size, but others has variable size.
234 * Fields with variable size are prefixed with one byte length marker.
235 * Some fields are strings and also can have fixed size and variable.
236 * Strings with fixed size are NULL-terminated, others are not.
237 * All VBLKs have same several first fields:
238 * Offset Size Description
239 * ---------------+---------------+--------------------------
240 * 0x00 16 standard VBLK header
241 * 0x10 2 update status
244 * 0x18+ PN object name
246 * o Offset 0x18+ means '0x18 + length of all variable-width fields'
247 * o 'P' in size column means 'prefixed' (variable-width),
248 * 'S' - string, 'N' - number.
250 #define LDM_VBLK_SIGN "VBLK"
251 #define LDM_VBLK_SEQ_OFF 0x04
252 #define LDM_VBLK_GROUP_OFF 0x08
253 #define LDM_VBLK_INDEX_OFF 0x0c
254 #define LDM_VBLK_COUNT_OFF 0x0e
255 #define LDM_VBLK_TYPE_OFF 0x13
256 #define LDM_VBLK_OID_OFF 0x18
258 uint32_t seq; /* sequence number */
259 uint32_t group; /* group number */
260 uint16_t index; /* index in the group */
261 uint16_t count; /* number of entries in the group */
264 #define LDM_VBLK_T_COMPONENT 0x32
265 #define LDM_VBLK_T_PARTITION 0x33
266 #define LDM_VBLK_T_DISK 0x34
267 #define LDM_VBLK_T_DISKGROUP 0x35
268 #define LDM_VBLK_T_DISK4 0x44
269 #define LDM_VBLK_T_DISKGROUP4 0x45
270 #define LDM_VBLK_T_VOLUME 0x51
272 uint8_t type; /* VBLK type */
275 struct ldm_volume vol;
276 struct ldm_component comp;
277 struct ldm_disk disk;
278 struct ldm_partition part;
280 struct ldm_disk_group disk_group;
283 LIST_ENTRY(ldm_vblk) entry;
287 * Some VBLKs contains a bit more data than can fit into 128 bytes. These
288 * VBLKs are called eXtended VBLK. Before parsing, the data from these VBLK
289 * should be placed into continuous memory buffer. We can determine xVBLK
290 * by the count field in the standard VBLK header (count > 1).
293 uint32_t group; /* xVBLK group number */
294 uint32_t size; /* the total size of xVBLK */
295 uint8_t map; /* bitmask of currently saved VBLKs */
296 u_char *data; /* xVBLK data */
298 LIST_ENTRY(ldm_xvblk) entry;
301 /* The internal representation of LDM database. */
303 struct ldm_privhdr ph; /* private header */
304 struct ldm_tochdr th; /* TOC header */
305 struct ldm_vmdbhdr dh; /* VMDB header */
307 LIST_HEAD(, ldm_volume) volumes;
308 LIST_HEAD(, ldm_disk) disks;
309 LIST_HEAD(, ldm_vblk) vblks;
310 LIST_HEAD(, ldm_xvblk) xvblks;
313 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
315 struct g_part_ldm_table {
316 struct g_part_table base;
320 struct g_part_ldm_entry {
321 struct g_part_entry base;
325 static int g_part_ldm_add(struct g_part_table *, struct g_part_entry *,
326 struct g_part_parms *);
327 static int g_part_ldm_bootcode(struct g_part_table *, struct g_part_parms *);
328 static int g_part_ldm_create(struct g_part_table *, struct g_part_parms *);
329 static int g_part_ldm_destroy(struct g_part_table *, struct g_part_parms *);
330 static void g_part_ldm_dumpconf(struct g_part_table *, struct g_part_entry *,
331 struct sbuf *, const char *);
332 static int g_part_ldm_dumpto(struct g_part_table *, struct g_part_entry *);
333 static int g_part_ldm_modify(struct g_part_table *, struct g_part_entry *,
334 struct g_part_parms *);
335 static const char *g_part_ldm_name(struct g_part_table *, struct g_part_entry *,
337 static int g_part_ldm_probe(struct g_part_table *, struct g_consumer *);
338 static int g_part_ldm_read(struct g_part_table *, struct g_consumer *);
339 static const char *g_part_ldm_type(struct g_part_table *, struct g_part_entry *,
341 static int g_part_ldm_write(struct g_part_table *, struct g_consumer *);
342 static int g_part_ldm_resize(struct g_part_table *, struct g_part_entry *,
343 struct g_part_parms *);
345 static kobj_method_t g_part_ldm_methods[] = {
346 KOBJMETHOD(g_part_add, g_part_ldm_add),
347 KOBJMETHOD(g_part_bootcode, g_part_ldm_bootcode),
348 KOBJMETHOD(g_part_create, g_part_ldm_create),
349 KOBJMETHOD(g_part_destroy, g_part_ldm_destroy),
350 KOBJMETHOD(g_part_dumpconf, g_part_ldm_dumpconf),
351 KOBJMETHOD(g_part_dumpto, g_part_ldm_dumpto),
352 KOBJMETHOD(g_part_modify, g_part_ldm_modify),
353 KOBJMETHOD(g_part_resize, g_part_ldm_resize),
354 KOBJMETHOD(g_part_name, g_part_ldm_name),
355 KOBJMETHOD(g_part_probe, g_part_ldm_probe),
356 KOBJMETHOD(g_part_read, g_part_ldm_read),
357 KOBJMETHOD(g_part_type, g_part_ldm_type),
358 KOBJMETHOD(g_part_write, g_part_ldm_write),
362 static struct g_part_scheme g_part_ldm_scheme = {
365 sizeof(struct g_part_ldm_table),
366 .gps_entrysz = sizeof(struct g_part_ldm_entry)
368 G_PART_SCHEME_DECLARE(g_part_ldm);
370 static struct g_part_ldm_alias {
373 } ldm_alias_match[] = {
374 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
375 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
376 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
377 { DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA },
378 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
379 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
380 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
381 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
385 ldm_privhdr_read(struct g_consumer *cp, uint64_t off, int *error)
387 struct g_provider *pp;
391 buf = g_read_data(cp, off, pp->sectorsize, error);
395 if (memcmp(buf, LDM_PH_SIGN, strlen(LDM_PH_SIGN)) != 0) {
396 LDM_DEBUG(1, "%s: invalid LDM private header signature",
406 ldm_privhdr_parse(struct g_consumer *cp, struct ldm_privhdr *hdr,
412 memset(hdr, 0, sizeof(*hdr));
413 version = be32dec(buf + LDM_PH_VERSION_OFF);
414 if (version != LDM_VERSION_2K &&
415 version != LDM_VERSION_VISTA) {
416 LDM_DEBUG(0, "%s: unsupported LDM version %u.%u",
417 cp->provider->name, version >> 16,
421 error = parse_uuid(buf + LDM_PH_DISKGUID_OFF, &hdr->disk_guid);
424 error = parse_uuid(buf + LDM_PH_DGGUID_OFF, &hdr->dg_guid);
427 strncpy(hdr->dg_name, buf + LDM_PH_DGNAME_OFF, sizeof(hdr->dg_name));
428 hdr->start = be64dec(buf + LDM_PH_START_OFF);
429 hdr->size = be64dec(buf + LDM_PH_SIZE_OFF);
430 hdr->db_offset = be64dec(buf + LDM_PH_DB_OFF);
431 hdr->db_size = be64dec(buf + LDM_PH_DBSIZE_OFF);
432 hdr->th_offset[0] = be64dec(buf + LDM_PH_TH1_OFF);
433 hdr->th_offset[1] = be64dec(buf + LDM_PH_TH2_OFF);
434 hdr->conf_size = be64dec(buf + LDM_PH_CONFSIZE_OFF);
435 hdr->log_size = be64dec(buf + LDM_PH_LOGSIZE_OFF);
440 ldm_privhdr_check(struct ldm_db *db, struct g_consumer *cp, int is_gpt)
442 struct g_consumer *cp2;
443 struct g_provider *pp;
444 struct ldm_privhdr hdr;
445 uint64_t offset, last;
452 * The last LBA is used in several checks below, for the
453 * GPT case it should be calculated relative to the whole
456 cp2 = LIST_FIRST(&pp->geom->consumer);
458 cp2->provider->mediasize / cp2->provider->sectorsize - 1;
460 last = pp->mediasize / pp->sectorsize - 1;
461 for (found = 0, i = is_gpt;
462 i < sizeof(ldm_ph_off) / sizeof(ldm_ph_off[0]); i++) {
463 offset = ldm_ph_off[i];
465 * In the GPT case consumer is attached to the LDM metadata
466 * partition and we don't need add db_offset.
469 offset += db->ph.db_offset;
470 if (i == LDM_PH_MBRINDEX) {
472 * Prepare to errors and setup new base offset
473 * to read backup private headers. Assume that LDM
474 * database is in the last 1Mbyte area.
476 db->ph.db_offset = last - LDM_DB_SIZE;
478 buf = ldm_privhdr_read(cp, offset * pp->sectorsize, &error);
480 LDM_DEBUG(1, "%s: failed to read private header "
481 "%d at LBA %ju", pp->name, i, (uintmax_t)offset);
484 error = ldm_privhdr_parse(cp, &hdr, buf);
486 LDM_DEBUG(1, "%s: failed to parse private "
487 "header %d", pp->name, i);
488 LDM_DUMP(buf, pp->sectorsize);
493 if (hdr.start > last ||
494 hdr.start + hdr.size - 1 > last ||
495 (hdr.start + hdr.size - 1 > hdr.db_offset && !is_gpt) ||
496 hdr.db_size != LDM_DB_SIZE ||
497 hdr.db_offset + LDM_DB_SIZE - 1 > last ||
498 hdr.th_offset[0] >= LDM_DB_SIZE ||
499 hdr.th_offset[1] >= LDM_DB_SIZE ||
500 hdr.conf_size + hdr.log_size >= LDM_DB_SIZE) {
501 LDM_DEBUG(1, "%s: invalid values in the "
502 "private header %d", pp->name, i);
503 LDM_DEBUG(2, "%s: start: %jd, size: %jd, "
504 "db_offset: %jd, db_size: %jd, th_offset0: %jd, "
505 "th_offset1: %jd, conf_size: %jd, log_size: %jd, "
506 "last: %jd", pp->name, hdr.start, hdr.size,
507 hdr.db_offset, hdr.db_size, hdr.th_offset[0],
508 hdr.th_offset[1], hdr.conf_size, hdr.log_size,
512 if (found != 0 && memcmp(&db->ph, &hdr, sizeof(hdr)) != 0) {
513 LDM_DEBUG(0, "%s: private headers are not equal",
517 * We have different headers in the LDM.
518 * We can not trust this metadata.
520 LDM_DEBUG(0, "%s: refuse LDM metadata",
525 * We already have read primary private header
526 * and it differs from this backup one.
527 * Prefer the backup header and save it.
532 memcpy(&db->ph, &hdr, sizeof(hdr));
536 LDM_DEBUG(1, "%s: valid LDM private header not found",
544 ldm_gpt_check(struct ldm_db *db, struct g_consumer *cp)
546 struct g_part_table *gpt;
547 struct g_part_entry *e;
548 struct g_consumer *cp2;
551 cp2 = LIST_NEXT(cp, consumer);
553 gpt = cp->provider->geom->softc;
555 LIST_FOREACH(e, &gpt->gpt_entry, gpe_entry) {
556 if (cp->provider == e->gpe_pp) {
557 /* ms-ldm-metadata partition */
558 if (e->gpe_start != db->ph.db_offset ||
559 e->gpe_end != db->ph.db_offset + LDM_DB_SIZE - 1)
561 } else if (cp2->provider == e->gpe_pp) {
562 /* ms-ldm-data partition */
563 if (e->gpe_start != db->ph.start ||
564 e->gpe_end != db->ph.start + db->ph.size - 1)
568 LDM_DEBUG(0, "%s: GPT partition %d boundaries "
569 "do not match with the LDM metadata",
570 e->gpe_pp->name, e->gpe_index);
580 ldm_tochdr_check(struct ldm_db *db, struct g_consumer *cp)
582 struct g_provider *pp;
583 struct ldm_tochdr hdr;
584 uint64_t offset, conf_size, log_size;
589 for (i = 0, found = 0; i < LDM_TH_COUNT; i++) {
590 offset = db->ph.db_offset + db->ph.th_offset[i];
591 buf = g_read_data(cp,
592 offset * pp->sectorsize, pp->sectorsize, &error);
594 LDM_DEBUG(1, "%s: failed to read TOC header "
595 "at LBA %ju", pp->name, (uintmax_t)offset);
598 if (memcmp(buf, LDM_TH_SIGN, strlen(LDM_TH_SIGN)) != 0 ||
599 memcmp(buf + LDM_TH_NAME1_OFF, LDM_TH_NAME1,
600 strlen(LDM_TH_NAME1)) != 0 ||
601 memcmp(buf + LDM_TH_NAME2_OFF, LDM_TH_NAME2,
602 strlen(LDM_TH_NAME2)) != 0) {
603 LDM_DEBUG(1, "%s: failed to parse TOC header "
604 "at LBA %ju", pp->name, (uintmax_t)offset);
605 LDM_DUMP(buf, pp->sectorsize);
609 hdr.conf_offset = be64dec(buf + LDM_TH_CONF_OFF);
610 hdr.log_offset = be64dec(buf + LDM_TH_LOG_OFF);
611 conf_size = be64dec(buf + LDM_TH_CONFSIZE_OFF);
612 log_size = be64dec(buf + LDM_TH_LOGSIZE_OFF);
613 if (conf_size != db->ph.conf_size ||
614 hdr.conf_offset + conf_size >= LDM_DB_SIZE ||
615 log_size != db->ph.log_size ||
616 hdr.log_offset + log_size >= LDM_DB_SIZE) {
617 LDM_DEBUG(1, "%s: invalid values in the "
618 "TOC header at LBA %ju", pp->name,
620 LDM_DUMP(buf, pp->sectorsize);
626 memcpy(&db->th, &hdr, sizeof(hdr));
630 LDM_DEBUG(0, "%s: valid LDM TOC header not found.",
638 ldm_vmdbhdr_check(struct ldm_db *db, struct g_consumer *cp)
640 struct g_provider *pp;
648 offset = db->ph.db_offset + db->th.conf_offset;
649 buf = g_read_data(cp, offset * pp->sectorsize, pp->sectorsize,
652 LDM_DEBUG(0, "%s: failed to read VMDB header at "
653 "LBA %ju", pp->name, (uintmax_t)offset);
656 if (memcmp(buf, LDM_VMDB_SIGN, strlen(LDM_VMDB_SIGN)) != 0) {
658 LDM_DEBUG(0, "%s: failed to parse VMDB header at "
659 "LBA %ju", pp->name, (uintmax_t)offset);
663 version = be32dec(buf + LDM_DB_VERSION_OFF);
664 if (version != 0x4000A) {
666 LDM_DEBUG(0, "%s: unsupported VMDB version %u.%u",
667 pp->name, version >> 16, version & 0xFFFF);
671 * Check VMDB update status:
672 * 1 - in a consistent state;
673 * 2 - in a creation phase;
674 * 3 - in a deletion phase;
676 if (be16dec(buf + LDM_DB_STATUS_OFF) != 1) {
678 LDM_DEBUG(0, "%s: VMDB is not in a consistent state",
682 db->dh.last_seq = be32dec(buf + LDM_DB_LASTSEQ_OFF);
683 db->dh.size = be32dec(buf + LDM_DB_SIZE_OFF);
684 error = parse_uuid(buf + LDM_DB_DGGUID_OFF, &dg_guid);
685 /* Compare disk group name and guid from VMDB and private headers */
686 if (error != 0 || db->dh.size == 0 ||
687 pp->sectorsize % db->dh.size != 0 ||
688 strncmp(buf + LDM_DB_DGNAME_OFF, db->ph.dg_name, 31) != 0 ||
689 memcmp(&dg_guid, &db->ph.dg_guid, sizeof(dg_guid)) != 0 ||
690 db->dh.size * db->dh.last_seq >
691 db->ph.conf_size * pp->sectorsize) {
692 LDM_DEBUG(0, "%s: invalid values in the VMDB header",
694 LDM_DUMP(buf, pp->sectorsize);
703 ldm_xvblk_handle(struct ldm_db *db, struct ldm_vblkhdr *vh, const u_char *p)
705 struct ldm_xvblk *blk;
708 size = db->dh.size - 16;
709 LIST_FOREACH(blk, &db->xvblks, entry)
710 if (blk->group == vh->group)
713 blk = g_malloc(sizeof(*blk), M_WAITOK | M_ZERO);
714 blk->group = vh->group;
715 blk->size = size * vh->count + 16;
716 blk->data = g_malloc(blk->size, M_WAITOK | M_ZERO);
717 blk->map = 0xFF << vh->count;
718 LIST_INSERT_HEAD(&db->xvblks, blk, entry);
720 if ((blk->map & (1 << vh->index)) != 0) {
721 /* Block with given index has been already saved. */
724 /* Copy the data block to the place related to index. */
725 memcpy(blk->data + size * vh->index + 16, p + 16, size);
726 blk->map |= 1 << vh->index;
730 /* Read the variable-width numeric field and return new offset */
732 ldm_vnum_get(const u_char *buf, int offset, uint64_t *result, size_t range)
738 if (len > sizeof(uint64_t) || len + offset >= range)
740 for (num = 0; len > 0; len--)
741 num = (num << 8) | buf[offset++];
746 /* Read the variable-width string and return new offset */
748 ldm_vstr_get(const u_char *buf, int offset, u_char *result,
749 size_t maxlen, size_t range)
754 if (len >= maxlen || len + offset >= range)
756 memcpy(result, buf + offset, len);
758 return (offset + len);
761 /* Just skip the variable-width variable and return new offset */
763 ldm_vparm_skip(const u_char *buf, int offset, size_t range)
768 if (offset + len >= range)
771 return (offset + len);
775 ldm_vblk_handle(struct ldm_db *db, const u_char *p, size_t size)
777 struct ldm_vblk *blk;
778 struct ldm_volume *volume, *last;
783 blk = g_malloc(sizeof(*blk), M_WAITOK | M_ZERO);
784 blk->type = p[LDM_VBLK_TYPE_OFF];
785 offset = ldm_vnum_get(p, LDM_VBLK_OID_OFF, &blk->u.id, size);
787 errstr = "object id";
790 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
792 errstr = "object name";
797 * Component VBLK fields:
798 * Offset Size Description
799 * ------------+-------+------------------------
800 * 0x18+ PS volume state
801 * 0x18+5 PN component children count
802 * 0x1D+16 PN parent's volume object id
803 * 0x2D+1 PN stripe size
805 case LDM_VBLK_T_COMPONENT:
806 offset = ldm_vparm_skip(p, offset, size);
808 errstr = "volume state";
811 offset = ldm_vparm_skip(p, offset + 5, size);
813 errstr = "children count";
816 offset = ldm_vnum_get(p, offset + 16,
817 &blk->u.comp.vol_id, size);
819 errstr = "volume id";
824 * Partition VBLK fields:
825 * Offset Size Description
826 * ------------+-------+------------------------
827 * 0x18+12 8 partition start offset
828 * 0x18+20 8 volume offset
829 * 0x18+28 PN partition size
830 * 0x34+ PN parent's component object id
831 * 0x34+ PN disk's object id
833 case LDM_VBLK_T_PARTITION:
834 if (offset + 28 >= size) {
835 errstr = "too small buffer";
838 blk->u.part.start = be64dec(p + offset + 12);
839 blk->u.part.offset = be64dec(p + offset + 20);
840 offset = ldm_vnum_get(p, offset + 28, &blk->u.part.size, size);
842 errstr = "partition size";
845 offset = ldm_vnum_get(p, offset, &blk->u.part.comp_id, size);
847 errstr = "component id";
850 offset = ldm_vnum_get(p, offset, &blk->u.part.disk_id, size);
858 * Offset Size Description
859 * ------------+-------+------------------------
862 case LDM_VBLK_T_DISK:
863 errstr = "disk guid";
864 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
867 error = parse_uuid(vstr, &blk->u.disk.guid);
870 LIST_INSERT_HEAD(&db->disks, &blk->u.disk, entry);
873 * Disk group VBLK fields:
874 * Offset Size Description
875 * ------------+-------+------------------------
876 * 0x18+ PS disk group GUID
878 case LDM_VBLK_T_DISKGROUP:
880 strncpy(blk->u.disk_group.name, vstr,
881 sizeof(blk->u.disk_group.name));
882 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
884 errstr = "disk group guid";
887 error = parse_uuid(name, &blk->u.disk_group.guid);
889 errstr = "disk group guid";
892 LIST_INSERT_HEAD(&db->groups, &blk->u.disk_group, entry);
897 * Offset Size Description
898 * ------------+-------+------------------------
901 case LDM_VBLK_T_DISK4:
902 be_uuid_dec(p + offset, &blk->u.disk.guid);
903 LIST_INSERT_HEAD(&db->disks, &blk->u.disk, entry);
906 * Disk group VBLK fields:
907 * Offset Size Description
908 * ------------+-------+------------------------
911 case LDM_VBLK_T_DISKGROUP4:
913 strncpy(blk->u.disk_group.name, vstr,
914 sizeof(blk->u.disk_group.name));
915 be_uuid_dec(p + offset, &blk->u.disk.guid);
916 LIST_INSERT_HEAD(&db->groups, &blk->u.disk_group, entry);
920 * Volume VBLK fields:
921 * Offset Size Description
922 * ------------+-------+------------------------
923 * 0x18+ PS volume type
925 * 0x18+ 14(S) volume state
926 * 0x18+16 1 volume number
927 * 0x18+21 PN volume children count
928 * 0x2D+16 PN volume size
929 * 0x3D+4 1 partition type
931 case LDM_VBLK_T_VOLUME:
932 offset = ldm_vparm_skip(p, offset, size);
934 errstr = "volume type";
937 offset = ldm_vparm_skip(p, offset, size);
939 errstr = "unknown param";
942 if (offset + 21 >= size) {
943 errstr = "too small buffer";
946 blk->u.vol.number = p[offset + 16];
947 offset = ldm_vparm_skip(p, offset + 21, size);
949 errstr = "children count";
952 offset = ldm_vnum_get(p, offset + 16, &blk->u.vol.size, size);
954 errstr = "volume size";
957 if (offset + 4 >= size) {
958 errstr = "too small buffer";
961 blk->u.vol.part_type = p[offset + 4];
962 /* keep volumes ordered by volume number */
964 LIST_FOREACH(volume, &db->volumes, entry) {
965 if (volume->number > blk->u.vol.number)
970 LIST_INSERT_AFTER(last, &blk->u.vol, entry);
972 LIST_INSERT_HEAD(&db->volumes, &blk->u.vol, entry);
975 LDM_DEBUG(1, "unknown VBLK type 0x%02x\n", blk->type);
978 LIST_INSERT_HEAD(&db->vblks, blk, entry);
981 LDM_DEBUG(0, "failed to parse '%s' in VBLK of type 0x%02x\n",
989 ldm_vmdb_free(struct ldm_db *db)
991 struct ldm_vblk *vblk;
992 struct ldm_xvblk *xvblk;
994 while (!LIST_EMPTY(&db->xvblks)) {
995 xvblk = LIST_FIRST(&db->xvblks);
996 LIST_REMOVE(xvblk, entry);
1000 while (!LIST_EMPTY(&db->vblks)) {
1001 vblk = LIST_FIRST(&db->vblks);
1002 LIST_REMOVE(vblk, entry);
1008 ldm_vmdb_parse(struct ldm_db *db, struct g_consumer *cp)
1010 struct g_provider *pp;
1011 struct ldm_vblk *vblk;
1012 struct ldm_xvblk *xvblk;
1013 struct ldm_volume *volume;
1014 struct ldm_component *comp;
1015 struct ldm_vblkhdr vh;
1017 size_t size, n, sectors;
1022 size = (db->dh.last_seq * db->dh.size +
1023 pp->sectorsize - 1) / pp->sectorsize;
1024 size -= 1; /* one sector takes vmdb header */
1025 for (n = 0; n < size; n += MAXPHYS / pp->sectorsize) {
1026 offset = db->ph.db_offset + db->th.conf_offset + n + 1;
1027 sectors = (size - n) > (MAXPHYS / pp->sectorsize) ?
1028 MAXPHYS / pp->sectorsize: size - n;
1030 buf = g_read_data(cp, offset * pp->sectorsize,
1031 sectors * pp->sectorsize, &error);
1033 LDM_DEBUG(0, "%s: failed to read VBLK\n",
1037 for (p = buf; p < buf + sectors * pp->sectorsize;
1039 if (memcmp(p, LDM_VBLK_SIGN,
1040 strlen(LDM_VBLK_SIGN)) != 0) {
1041 LDM_DEBUG(0, "%s: no VBLK signature\n",
1043 LDM_DUMP(p, db->dh.size);
1046 vh.seq = be32dec(p + LDM_VBLK_SEQ_OFF);
1047 vh.group = be32dec(p + LDM_VBLK_GROUP_OFF);
1048 /* skip empty blocks */
1049 if (vh.seq == 0 || vh.group == 0)
1051 vh.index = be16dec(p + LDM_VBLK_INDEX_OFF);
1052 vh.count = be16dec(p + LDM_VBLK_COUNT_OFF);
1053 if (vh.count == 0 || vh.count > 4 ||
1054 vh.seq > db->dh.last_seq) {
1055 LDM_DEBUG(0, "%s: invalid values "
1056 "in the VBLK header\n", pp->name);
1057 LDM_DUMP(p, db->dh.size);
1061 error = ldm_xvblk_handle(db, &vh, p);
1063 LDM_DEBUG(0, "%s: xVBLK "
1064 "is corrupted\n", pp->name);
1065 LDM_DUMP(p, db->dh.size);
1070 if (be16dec(p + 16) != 0)
1071 LDM_DEBUG(1, "%s: VBLK update"
1072 " status is %u\n", pp->name,
1074 error = ldm_vblk_handle(db, p, db->dh.size);
1082 while (!LIST_EMPTY(&db->xvblks)) {
1083 xvblk = LIST_FIRST(&db->xvblks);
1084 if (xvblk->map == 0xFF) {
1085 error = ldm_vblk_handle(db, xvblk->data, xvblk->size);
1089 LDM_DEBUG(0, "%s: incomplete or corrupt "
1090 "xVBLK found\n", pp->name);
1093 LIST_REMOVE(xvblk, entry);
1094 g_free(xvblk->data);
1097 /* construct all VBLKs relations */
1098 LIST_FOREACH(volume, &db->volumes, entry) {
1099 LIST_FOREACH(vblk, &db->vblks, entry)
1100 if (vblk->type == LDM_VBLK_T_COMPONENT &&
1101 vblk->u.comp.vol_id == volume->id) {
1102 LIST_INSERT_HEAD(&volume->components,
1103 &vblk->u.comp, entry);
1106 LIST_FOREACH(comp, &volume->components, entry)
1107 LIST_FOREACH(vblk, &db->vblks, entry)
1108 if (vblk->type == LDM_VBLK_T_PARTITION &&
1109 vblk->u.part.comp_id == comp->id) {
1110 LIST_INSERT_HEAD(&comp->partitions,
1111 &vblk->u.part, entry);
1123 g_part_ldm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
1124 struct g_part_parms *gpp)
1131 g_part_ldm_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
1138 g_part_ldm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
1145 g_part_ldm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
1147 struct g_part_ldm_table *table;
1148 struct g_provider *pp;
1150 table = (struct g_part_ldm_table *)basetable;
1152 * To destroy LDM on a disk partitioned with GPT we should delete
1153 * ms-ldm-metadata partition, but we can't do this via standard
1158 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1160 * To destroy LDM we should wipe MBR, first private header and
1161 * backup private headers.
1163 basetable->gpt_smhead = (1 << ldm_ph_off[0]) | 1;
1165 * Don't touch last backup private header when LDM database is
1166 * not located in the last 1MByte area.
1167 * XXX: can't remove all blocks.
1169 if (table->db_offset + LDM_DB_SIZE ==
1170 pp->mediasize / pp->sectorsize)
1171 basetable->gpt_smtail = 1;
1176 g_part_ldm_dumpconf(struct g_part_table *basetable,
1177 struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
1179 struct g_part_ldm_entry *entry;
1181 entry = (struct g_part_ldm_entry *)baseentry;
1182 if (indent == NULL) {
1183 /* conftxt: libdisk compatibility */
1184 sbuf_printf(sb, " xs LDM xt %u", entry->type);
1185 } else if (entry != NULL) {
1186 /* confxml: partition entry information */
1187 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
1190 /* confxml: scheme information */
1195 g_part_ldm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
1202 g_part_ldm_modify(struct g_part_table *basetable,
1203 struct g_part_entry *baseentry, struct g_part_parms *gpp)
1210 g_part_ldm_resize(struct g_part_table *basetable,
1211 struct g_part_entry *baseentry, struct g_part_parms *gpp)
1218 g_part_ldm_name(struct g_part_table *table, struct g_part_entry *baseentry,
1219 char *buf, size_t bufsz)
1222 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
1227 ldm_gpt_probe(struct g_part_table *basetable, struct g_consumer *cp)
1229 struct g_part_ldm_table *table;
1230 struct g_part_table *gpt;
1231 struct g_part_entry *entry;
1232 struct g_consumer *cp2;
1233 struct gpt_ent *part;
1238 * XXX: We use some knowlege about GEOM_PART_GPT internal
1239 * structures, but it is easier than parse GPT by himself.
1242 gpt = cp->provider->geom->softc;
1243 LIST_FOREACH(entry, &gpt->gpt_entry, gpe_entry) {
1244 part = (struct gpt_ent *)(entry + 1);
1245 /* Search ms-ldm-metadata partition */
1246 if (memcmp(&part->ent_type,
1247 &gpt_uuid_ms_ldm_metadata, sizeof(struct uuid)) != 0 ||
1248 entry->gpe_end - entry->gpe_start < LDM_DB_SIZE - 1)
1251 /* Create new consumer and attach it to metadata partition */
1252 cp2 = g_new_consumer(cp->geom);
1253 error = g_attach(cp2, entry->gpe_pp);
1255 g_destroy_consumer(cp2);
1256 g_topology_unlock();
1259 error = g_access(cp2, 1, 0, 0);
1262 g_destroy_consumer(cp2);
1263 g_topology_unlock();
1266 g_topology_unlock();
1268 LDM_DEBUG(2, "%s: LDM metadata partition %s found in the GPT",
1269 cp->provider->name, cp2->provider->name);
1270 /* Read the LDM private header */
1271 buf = ldm_privhdr_read(cp2,
1272 ldm_ph_off[LDM_PH_GPTINDEX] * cp2->provider->sectorsize,
1275 table = (struct g_part_ldm_table *)basetable;
1278 return (G_PART_PROBE_PRI_HIGH);
1281 /* second consumer is no longer needed. */
1283 g_access(cp2, -1, 0, 0);
1285 g_destroy_consumer(cp2);
1288 g_topology_unlock();
1293 g_part_ldm_probe(struct g_part_table *basetable, struct g_consumer *cp)
1295 struct g_provider *pp;
1296 u_char *buf, type[64];
1301 if (pp->sectorsize != 512)
1304 error = g_getattr("PART::scheme", cp, &type);
1305 if (error == 0 && strcmp(type, "GPT") == 0) {
1306 if (g_getattr("PART::type", cp, &type) != 0 ||
1307 strcmp(type, "ms-ldm-data") != 0)
1309 error = ldm_gpt_probe(basetable, cp);
1313 if (basetable->gpt_depth != 0)
1316 /* LDM has 1M metadata area */
1317 if (pp->mediasize <= 1024 * 1024)
1320 /* Check that there's a MBR */
1321 buf = g_read_data(cp, 0, pp->sectorsize, &error);
1325 if (le16dec(buf + DOSMAGICOFFSET) != DOSMAGIC) {
1330 /* Check that we have LDM partitions in the MBR */
1331 for (idx = 0; idx < NDOSPART && error != 0; idx++) {
1332 if (buf[DOSPARTOFF + idx * DOSPARTSIZE + 4] == DOSPTYP_LDM)
1337 LDM_DEBUG(2, "%s: LDM data partitions found in MBR",
1339 /* Read the LDM private header */
1340 buf = ldm_privhdr_read(cp,
1341 ldm_ph_off[LDM_PH_MBRINDEX] * pp->sectorsize, &error);
1345 return (G_PART_PROBE_PRI_HIGH);
1351 g_part_ldm_read(struct g_part_table *basetable, struct g_consumer *cp)
1353 struct g_part_ldm_table *table;
1354 struct g_part_ldm_entry *entry;
1355 struct g_consumer *cp2;
1356 struct ldm_component *comp;
1357 struct ldm_partition *part;
1358 struct ldm_volume *vol;
1359 struct ldm_disk *disk;
1361 int error, index, skipped;
1363 table = (struct g_part_ldm_table *)basetable;
1364 memset(&db, 0, sizeof(db));
1365 cp2 = cp; /* ms-ldm-data */
1367 cp = LIST_FIRST(&cp->geom->consumer); /* ms-ldm-metadata */
1368 /* Read and parse LDM private headers. */
1369 error = ldm_privhdr_check(&db, cp, table->is_gpt);
1372 basetable->gpt_first = table->is_gpt ? 0: db.ph.start;
1373 basetable->gpt_last = basetable->gpt_first + db.ph.size - 1;
1374 table->db_offset = db.ph.db_offset;
1375 /* Make additional checks for GPT */
1376 if (table->is_gpt) {
1377 error = ldm_gpt_check(&db, cp);
1381 * Now we should reset database offset to zero, because our
1382 * consumer cp is attached to the ms-ldm-metadata partition
1383 * and we don't need add db_offset to read from it.
1385 db.ph.db_offset = 0;
1387 /* Read and parse LDM TOC headers. */
1388 error = ldm_tochdr_check(&db, cp);
1391 /* Read and parse LDM VMDB header. */
1392 error = ldm_vmdbhdr_check(&db, cp);
1395 error = ldm_vmdb_parse(&db, cp);
1397 * For the GPT case we must detach and destroy
1398 * second consumer before return.
1401 if (table->is_gpt) {
1403 g_access(cp, -1, 0, 0);
1405 g_destroy_consumer(cp);
1406 g_topology_unlock();
1411 /* Search current disk in the disk list. */
1412 LIST_FOREACH(disk, &db.disks, entry)
1413 if (memcmp(&disk->guid, &db.ph.disk_guid,
1414 sizeof(struct uuid)) == 0)
1417 LDM_DEBUG(1, "%s: no LDM volumes on this disk",
1418 cp->provider->name);
1423 LIST_FOREACH(vol, &db.volumes, entry) {
1424 LIST_FOREACH(comp, &vol->components, entry) {
1425 /* Skip volumes from different disks. */
1426 part = LIST_FIRST(&comp->partitions);
1427 if (part->disk_id != disk->id)
1430 /* We don't support spanned and striped volumes. */
1431 if (comp->count > 1 || part->offset != 0) {
1432 LDM_DEBUG(1, "%s: LDM volume component "
1433 "%ju has %u partitions. Skipped",
1434 cp->provider->name, (uintmax_t)comp->id,
1439 * Allow mirrored volumes only when they are explicitly
1440 * allowed with kern.geom.part.ldm.show_mirrors=1.
1442 if (vol->count > 1 && show_mirrors == 0) {
1443 LDM_DEBUG(1, "%s: LDM volume %ju has %u "
1444 "components. Skipped",
1445 cp->provider->name, (uintmax_t)vol->id,
1449 entry = (struct g_part_ldm_entry *)g_part_new_entry(
1451 basetable->gpt_first + part->start,
1452 basetable->gpt_first + part->start +
1455 * Mark skipped partition as ms-ldm-data partition.
1456 * We do not support them, but it is better to show
1457 * that we have something there, than just show
1461 entry->type = vol->part_type;
1463 entry->type = DOSPTYP_LDM;
1464 LDM_DEBUG(1, "%s: new volume id: %ju, start: %ju,"
1465 " end: %ju, type: 0x%02x\n", cp->provider->name,
1466 (uintmax_t)part->id,(uintmax_t)part->start +
1467 basetable->gpt_first, (uintmax_t)part->start +
1468 part->size + basetable->gpt_first - 1,
1477 g_part_ldm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1478 char *buf, size_t bufsz)
1480 struct g_part_ldm_entry *entry;
1483 entry = (struct g_part_ldm_entry *)baseentry;
1485 i < sizeof(ldm_alias_match) / sizeof(ldm_alias_match[0]); i++) {
1486 if (ldm_alias_match[i].typ == entry->type)
1487 return (g_part_alias_name(ldm_alias_match[i].alias));
1489 snprintf(buf, bufsz, "!%d", entry->type);
1494 g_part_ldm_write(struct g_part_table *basetable, struct g_consumer *cp)