1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/bio.h>
31 #include <sys/diskmbr.h>
32 #include <sys/endian.h>
33 #include <sys/gpt.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/uuid.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
47
48 #include "g_part_if.h"
49
50 FEATURE(geom_part_ldm, "GEOM partitioning class for LDM support");
51
52 SYSCTL_DECL(_kern_geom_part);
53 static SYSCTL_NODE(_kern_geom_part, OID_AUTO, ldm,
54 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
55 "GEOM_PART_LDM Logical Disk Manager");
56
57 static u_int ldm_debug = 0;
58 SYSCTL_UINT(_kern_geom_part_ldm, OID_AUTO, debug,
59 CTLFLAG_RWTUN, &ldm_debug, 0, "Debug level");
60
61 /*
62 * This allows access to mirrored LDM volumes. Since we do not
63 * doing mirroring here, it is not enabled by default.
64 */
65 static u_int show_mirrors = 0;
66 SYSCTL_UINT(_kern_geom_part_ldm, OID_AUTO, show_mirrors,
67 CTLFLAG_RWTUN, &show_mirrors, 0, "Show mirrored volumes");
68
69 #define LDM_DEBUG(lvl, fmt, ...) do { \
70 if (ldm_debug >= (lvl)) { \
71 printf("GEOM_PART: " fmt "\n", __VA_ARGS__); \
72 } \
73 } while (0)
74 #define LDM_DUMP(buf, size) do { \
75 if (ldm_debug > 1) { \
76 hexdump(buf, size, NULL, 0); \
77 } \
78 } while (0)
79
80 /*
81 * There are internal representations of LDM structures.
82 *
83 * We do not keep all fields of on-disk structures, only most useful.
84 * All numbers in an on-disk structures are in big-endian format.
85 */
86
87 /*
88 * Private header is 512 bytes long. There are three copies on each disk.
89 * Offset and sizes are in sectors. Location of each copy:
90 * - the first offset is relative to the disk start;
91 * - the second and third offset are relative to the LDM database start.
92 *
93 * On a disk partitioned with GPT, the LDM has not first private header.
94 */
95 #define LDM_PH_MBRINDEX 0
96 #define LDM_PH_GPTINDEX 2
97 static const uint64_t ldm_ph_off[] = {6, 1856, 2047};
98 #define LDM_VERSION_2K 0x2000b
99 #define LDM_VERSION_VISTA 0x2000c
100 #define LDM_PH_VERSION_OFF 0x00c
101 #define LDM_PH_DISKGUID_OFF 0x030
102 #define LDM_PH_DGGUID_OFF 0x0b0
103 #define LDM_PH_DGNAME_OFF 0x0f0
104 #define LDM_PH_START_OFF 0x11b
105 #define LDM_PH_SIZE_OFF 0x123
106 #define LDM_PH_DB_OFF 0x12b
107 #define LDM_PH_DBSIZE_OFF 0x133
108 #define LDM_PH_TH1_OFF 0x13b
109 #define LDM_PH_TH2_OFF 0x143
110 #define LDM_PH_CONFSIZE_OFF 0x153
111 #define LDM_PH_LOGSIZE_OFF 0x15b
112 #define LDM_PH_SIGN "PRIVHEAD"
113 struct ldm_privhdr {
114 struct uuid disk_guid;
115 struct uuid dg_guid;
116 u_char dg_name[32];
117 uint64_t start; /* logical disk start */
118 uint64_t size; /* logical disk size */
119 uint64_t db_offset; /* LDM database start */
120 #define LDM_DB_SIZE 2048
121 uint64_t db_size; /* LDM database size */
122 #define LDM_TH_COUNT 2
123 uint64_t th_offset[LDM_TH_COUNT]; /* TOC header offsets */
124 uint64_t conf_size; /* configuration size */
125 uint64_t log_size; /* size of log */
126 };
127
128 /*
129 * Table of contents header is 512 bytes long.
130 * There are two identical copies at offsets from the private header.
131 * Offsets are relative to the LDM database start.
132 */
133 #define LDM_TH_SIGN "TOCBLOCK"
134 #define LDM_TH_NAME1 "config"
135 #define LDM_TH_NAME2 "log"
136 #define LDM_TH_NAME1_OFF 0x024
137 #define LDM_TH_CONF_OFF 0x02e
138 #define LDM_TH_CONFSIZE_OFF 0x036
139 #define LDM_TH_NAME2_OFF 0x046
140 #define LDM_TH_LOG_OFF 0x050
141 #define LDM_TH_LOGSIZE_OFF 0x058
142 struct ldm_tochdr {
143 uint64_t conf_offset; /* configuration offset */
144 uint64_t log_offset; /* log offset */
145 };
146
147 /*
148 * LDM database header is 512 bytes long.
149 */
150 #define LDM_VMDB_SIGN "VMDB"
151 #define LDM_DB_LASTSEQ_OFF 0x004
152 #define LDM_DB_SIZE_OFF 0x008
153 #define LDM_DB_STATUS_OFF 0x010
154 #define LDM_DB_VERSION_OFF 0x012
155 #define LDM_DB_DGNAME_OFF 0x016
156 #define LDM_DB_DGGUID_OFF 0x035
157 struct ldm_vmdbhdr {
158 uint32_t last_seq; /* sequence number of last VBLK */
159 uint32_t size; /* size of VBLK */
160 };
161
162 /*
163 * The LDM database configuration section contains VMDB header and
164 * many VBLKs. Each VBLK represents a disk group, disk partition,
165 * component or volume.
166 *
167 * The most interesting for us are volumes, they are represents
168 * partitions in the GEOM_PART meaning. But volume VBLK does not
169 * contain all information needed to create GEOM provider. And we
170 * should get this information from the related VBLK. This is how
171 * VBLK releated:
172 * Volumes <- Components <- Partitions -> Disks
173 *
174 * One volume can contain several components. In this case LDM
175 * does mirroring of volume data to each component.
176 *
177 * Also each component can contain several partitions (spanned or
178 * striped volumes).
179 */
180
181 struct ldm_component {
182 uint64_t id; /* object id */
183 uint64_t vol_id; /* parent volume object id */
184
185 int count;
186 LIST_HEAD(, ldm_partition) partitions;
187 LIST_ENTRY(ldm_component) entry;
188 };
189
190 struct ldm_volume {
191 uint64_t id; /* object id */
192 uint64_t size; /* volume size */
193 uint8_t number; /* used for ordering */
194 uint8_t part_type; /* partition type */
195
196 int count;
197 LIST_HEAD(, ldm_component) components;
198 LIST_ENTRY(ldm_volume) entry;
199 };
200
201 struct ldm_disk {
202 uint64_t id; /* object id */
203 struct uuid guid; /* disk guid */
204
205 LIST_ENTRY(ldm_disk) entry;
206 };
207
208 #if 0
209 struct ldm_disk_group {
210 uint64_t id; /* object id */
211 struct uuid guid; /* disk group guid */
212 u_char name[32]; /* disk group name */
213
214 LIST_ENTRY(ldm_disk_group) entry;
215 };
216 #endif
217
218 struct ldm_partition {
219 uint64_t id; /* object id */
220 uint64_t disk_id; /* disk object id */
221 uint64_t comp_id; /* parent component object id */
222 uint64_t start; /* offset relative to disk start */
223 uint64_t offset; /* offset for spanned volumes */
224 uint64_t size; /* partition size */
225
226 LIST_ENTRY(ldm_partition) entry;
227 };
228
229 /*
230 * Each VBLK is 128 bytes long and has standard 16 bytes header.
231 * Some of VBLK's fields are fixed size, but others has variable size.
232 * Fields with variable size are prefixed with one byte length marker.
233 * Some fields are strings and also can have fixed size and variable.
234 * Strings with fixed size are NULL-terminated, others are not.
235 * All VBLKs have same several first fields:
236 * Offset Size Description
237 * ---------------+---------------+--------------------------
238 * 0x00 16 standard VBLK header
239 * 0x10 2 update status
240 * 0x13 1 VBLK type
241 * 0x18 PS object id
242 * 0x18+ PN object name
243 *
244 * o Offset 0x18+ means '0x18 + length of all variable-width fields'
245 * o 'P' in size column means 'prefixed' (variable-width),
246 * 'S' - string, 'N' - number.
247 */
248 #define LDM_VBLK_SIGN "VBLK"
249 #define LDM_VBLK_SEQ_OFF 0x04
250 #define LDM_VBLK_GROUP_OFF 0x08
251 #define LDM_VBLK_INDEX_OFF 0x0c
252 #define LDM_VBLK_COUNT_OFF 0x0e
253 #define LDM_VBLK_TYPE_OFF 0x13
254 #define LDM_VBLK_OID_OFF 0x18
255 struct ldm_vblkhdr {
256 uint32_t seq; /* sequence number */
257 uint32_t group; /* group number */
258 uint16_t index; /* index in the group */
259 uint16_t count; /* number of entries in the group */
260 };
261
262 #define LDM_VBLK_T_COMPONENT 0x32
263 #define LDM_VBLK_T_PARTITION 0x33
264 #define LDM_VBLK_T_DISK 0x34
265 #define LDM_VBLK_T_DISKGROUP 0x35
266 #define LDM_VBLK_T_DISK4 0x44
267 #define LDM_VBLK_T_DISKGROUP4 0x45
268 #define LDM_VBLK_T_VOLUME 0x51
269 struct ldm_vblk {
270 uint8_t type; /* VBLK type */
271 union {
272 uint64_t id;
273 struct ldm_volume vol;
274 struct ldm_component comp;
275 struct ldm_disk disk;
276 struct ldm_partition part;
277 #if 0
278 struct ldm_disk_group disk_group;
279 #endif
280 } u;
281 LIST_ENTRY(ldm_vblk) entry;
282 };
283
284 /*
285 * Some VBLKs contains a bit more data than can fit into 128 bytes. These
286 * VBLKs are called eXtended VBLK. Before parsing, the data from these VBLK
287 * should be placed into continuous memory buffer. We can determine xVBLK
288 * by the count field in the standard VBLK header (count > 1).
289 */
290 struct ldm_xvblk {
291 uint32_t group; /* xVBLK group number */
292 uint32_t size; /* the total size of xVBLK */
293 uint8_t map; /* bitmask of currently saved VBLKs */
294 u_char *data; /* xVBLK data */
295
296 LIST_ENTRY(ldm_xvblk) entry;
297 };
298
299 /* The internal representation of LDM database. */
300 struct ldm_db {
301 struct ldm_privhdr ph; /* private header */
302 struct ldm_tochdr th; /* TOC header */
303 struct ldm_vmdbhdr dh; /* VMDB header */
304
305 LIST_HEAD(, ldm_volume) volumes;
306 LIST_HEAD(, ldm_disk) disks;
307 LIST_HEAD(, ldm_vblk) vblks;
308 LIST_HEAD(, ldm_xvblk) xvblks;
309 };
310
311 static struct uuid gpt_uuid_ms_ldm_metadata = GPT_ENT_TYPE_MS_LDM_METADATA;
312
313 struct g_part_ldm_table {
314 struct g_part_table base;
315 uint64_t db_offset;
316 int is_gpt;
317 };
318 struct g_part_ldm_entry {
319 struct g_part_entry base;
320 uint8_t type;
321 };
322
323 static int g_part_ldm_add(struct g_part_table *, struct g_part_entry *,
324 struct g_part_parms *);
325 static int g_part_ldm_bootcode(struct g_part_table *, struct g_part_parms *);
326 static int g_part_ldm_create(struct g_part_table *, struct g_part_parms *);
327 static int g_part_ldm_destroy(struct g_part_table *, struct g_part_parms *);
328 static void g_part_ldm_dumpconf(struct g_part_table *, struct g_part_entry *,
329 struct sbuf *, const char *);
330 static int g_part_ldm_dumpto(struct g_part_table *, struct g_part_entry *);
331 static int g_part_ldm_modify(struct g_part_table *, struct g_part_entry *,
332 struct g_part_parms *);
333 static const char *g_part_ldm_name(struct g_part_table *, struct g_part_entry *,
334 char *, size_t);
335 static int g_part_ldm_probe(struct g_part_table *, struct g_consumer *);
336 static int g_part_ldm_read(struct g_part_table *, struct g_consumer *);
337 static const char *g_part_ldm_type(struct g_part_table *, struct g_part_entry *,
338 char *, size_t);
339 static int g_part_ldm_write(struct g_part_table *, struct g_consumer *);
340
341 static kobj_method_t g_part_ldm_methods[] = {
342 KOBJMETHOD(g_part_add, g_part_ldm_add),
343 KOBJMETHOD(g_part_bootcode, g_part_ldm_bootcode),
344 KOBJMETHOD(g_part_create, g_part_ldm_create),
345 KOBJMETHOD(g_part_destroy, g_part_ldm_destroy),
346 KOBJMETHOD(g_part_dumpconf, g_part_ldm_dumpconf),
347 KOBJMETHOD(g_part_dumpto, g_part_ldm_dumpto),
348 KOBJMETHOD(g_part_modify, g_part_ldm_modify),
349 KOBJMETHOD(g_part_name, g_part_ldm_name),
350 KOBJMETHOD(g_part_probe, g_part_ldm_probe),
351 KOBJMETHOD(g_part_read, g_part_ldm_read),
352 KOBJMETHOD(g_part_type, g_part_ldm_type),
353 KOBJMETHOD(g_part_write, g_part_ldm_write),
354 { 0, 0 }
355 };
356
357 static struct g_part_scheme g_part_ldm_scheme = {
358 "LDM",
359 g_part_ldm_methods,
360 sizeof(struct g_part_ldm_table),
361 .gps_entrysz = sizeof(struct g_part_ldm_entry)
362 };
363 G_PART_SCHEME_DECLARE(g_part_ldm);
364 MODULE_VERSION(geom_part_ldm, 0);
365
366 static struct g_part_ldm_alias {
367 u_char typ;
368 int alias;
369 } ldm_alias_match[] = {
370 { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
371 { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
372 { DOSPTYP_FAT32LBA, G_PART_ALIAS_MS_FAT32LBA },
373 { DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA },
374 { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
375 { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
376 { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
377 { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
378 { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
379 };
380
381 static u_char*
ldm_privhdr_read(struct g_consumer * cp,uint64_t off,int * error)382 ldm_privhdr_read(struct g_consumer *cp, uint64_t off, int *error)
383 {
384 struct g_provider *pp;
385 u_char *buf;
386
387 pp = cp->provider;
388 buf = g_read_data(cp, off, pp->sectorsize, error);
389 if (buf == NULL)
390 return (NULL);
391
392 if (memcmp(buf, LDM_PH_SIGN, strlen(LDM_PH_SIGN)) != 0) {
393 LDM_DEBUG(1, "%s: invalid LDM private header signature",
394 pp->name);
395 g_free(buf);
396 buf = NULL;
397 *error = EINVAL;
398 }
399 return (buf);
400 }
401
402 static int
ldm_privhdr_parse(struct g_consumer * cp,struct ldm_privhdr * hdr,const u_char * buf)403 ldm_privhdr_parse(struct g_consumer *cp, struct ldm_privhdr *hdr,
404 const u_char *buf)
405 {
406 uint32_t version;
407 int error;
408
409 memset(hdr, 0, sizeof(*hdr));
410 version = be32dec(buf + LDM_PH_VERSION_OFF);
411 if (version != LDM_VERSION_2K &&
412 version != LDM_VERSION_VISTA) {
413 LDM_DEBUG(0, "%s: unsupported LDM version %u.%u",
414 cp->provider->name, version >> 16,
415 version & 0xFFFF);
416 return (ENXIO);
417 }
418 error = parse_uuid(buf + LDM_PH_DISKGUID_OFF, &hdr->disk_guid);
419 if (error != 0)
420 return (error);
421 error = parse_uuid(buf + LDM_PH_DGGUID_OFF, &hdr->dg_guid);
422 if (error != 0)
423 return (error);
424 strncpy(hdr->dg_name, buf + LDM_PH_DGNAME_OFF, sizeof(hdr->dg_name));
425 hdr->start = be64dec(buf + LDM_PH_START_OFF);
426 hdr->size = be64dec(buf + LDM_PH_SIZE_OFF);
427 hdr->db_offset = be64dec(buf + LDM_PH_DB_OFF);
428 hdr->db_size = be64dec(buf + LDM_PH_DBSIZE_OFF);
429 hdr->th_offset[0] = be64dec(buf + LDM_PH_TH1_OFF);
430 hdr->th_offset[1] = be64dec(buf + LDM_PH_TH2_OFF);
431 hdr->conf_size = be64dec(buf + LDM_PH_CONFSIZE_OFF);
432 hdr->log_size = be64dec(buf + LDM_PH_LOGSIZE_OFF);
433 return (0);
434 }
435
436 static int
ldm_privhdr_check(struct ldm_db * db,struct g_consumer * cp,int is_gpt)437 ldm_privhdr_check(struct ldm_db *db, struct g_consumer *cp, int is_gpt)
438 {
439 struct g_consumer *cp2;
440 struct g_provider *pp;
441 struct ldm_privhdr hdr;
442 uint64_t offset, last;
443 int error, found, i;
444 u_char *buf;
445
446 pp = cp->provider;
447 if (is_gpt) {
448 /*
449 * The last LBA is used in several checks below, for the
450 * GPT case it should be calculated relative to the whole
451 * disk.
452 */
453 cp2 = LIST_FIRST(&pp->geom->consumer);
454 last =
455 cp2->provider->mediasize / cp2->provider->sectorsize - 1;
456 } else
457 last = pp->mediasize / pp->sectorsize - 1;
458 for (found = 0, i = is_gpt; i < nitems(ldm_ph_off); i++) {
459 offset = ldm_ph_off[i];
460 /*
461 * In the GPT case consumer is attached to the LDM metadata
462 * partition and we don't need add db_offset.
463 */
464 if (!is_gpt)
465 offset += db->ph.db_offset;
466 if (i == LDM_PH_MBRINDEX) {
467 /*
468 * Prepare to errors and setup new base offset
469 * to read backup private headers. Assume that LDM
470 * database is in the last 1Mbyte area.
471 */
472 db->ph.db_offset = last - LDM_DB_SIZE;
473 }
474 buf = ldm_privhdr_read(cp, offset * pp->sectorsize, &error);
475 if (buf == NULL) {
476 LDM_DEBUG(1, "%s: failed to read private header "
477 "%d at LBA %ju", pp->name, i, (uintmax_t)offset);
478 continue;
479 }
480 error = ldm_privhdr_parse(cp, &hdr, buf);
481 if (error != 0) {
482 LDM_DEBUG(1, "%s: failed to parse private "
483 "header %d", pp->name, i);
484 LDM_DUMP(buf, pp->sectorsize);
485 g_free(buf);
486 continue;
487 }
488 g_free(buf);
489 if (hdr.start > last ||
490 hdr.start + hdr.size - 1 > last ||
491 (hdr.start + hdr.size - 1 > hdr.db_offset && !is_gpt) ||
492 hdr.db_size != LDM_DB_SIZE ||
493 hdr.db_offset + LDM_DB_SIZE - 1 > last ||
494 hdr.th_offset[0] >= LDM_DB_SIZE ||
495 hdr.th_offset[1] >= LDM_DB_SIZE ||
496 hdr.conf_size + hdr.log_size >= LDM_DB_SIZE) {
497 LDM_DEBUG(1, "%s: invalid values in the "
498 "private header %d", pp->name, i);
499 LDM_DEBUG(2, "%s: start: %jd, size: %jd, "
500 "db_offset: %jd, db_size: %jd, th_offset0: %jd, "
501 "th_offset1: %jd, conf_size: %jd, log_size: %jd, "
502 "last: %jd", pp->name, hdr.start, hdr.size,
503 hdr.db_offset, hdr.db_size, hdr.th_offset[0],
504 hdr.th_offset[1], hdr.conf_size, hdr.log_size,
505 last);
506 continue;
507 }
508 if (found != 0 && memcmp(&db->ph, &hdr, sizeof(hdr)) != 0) {
509 LDM_DEBUG(0, "%s: private headers are not equal",
510 pp->name);
511 if (i > 1) {
512 /*
513 * We have different headers in the LDM.
514 * We can not trust this metadata.
515 */
516 LDM_DEBUG(0, "%s: refuse LDM metadata",
517 pp->name);
518 return (EINVAL);
519 }
520 /*
521 * We already have read primary private header
522 * and it differs from this backup one.
523 * Prefer the backup header and save it.
524 */
525 found = 0;
526 }
527 if (found == 0)
528 memcpy(&db->ph, &hdr, sizeof(hdr));
529 found = 1;
530 }
531 if (found == 0) {
532 LDM_DEBUG(1, "%s: valid LDM private header not found",
533 pp->name);
534 return (ENXIO);
535 }
536 return (0);
537 }
538
539 static int
ldm_gpt_check(struct ldm_db * db,struct g_consumer * cp)540 ldm_gpt_check(struct ldm_db *db, struct g_consumer *cp)
541 {
542 struct g_part_table *gpt;
543 struct g_part_entry *e;
544 struct g_consumer *cp2;
545 int error;
546
547 cp2 = LIST_NEXT(cp, consumer);
548 g_topology_lock();
549 gpt = cp->provider->geom->softc;
550 error = 0;
551 LIST_FOREACH(e, &gpt->gpt_entry, gpe_entry) {
552 if (cp->provider == e->gpe_pp) {
553 /* ms-ldm-metadata partition */
554 if (e->gpe_start != db->ph.db_offset ||
555 e->gpe_end != db->ph.db_offset + LDM_DB_SIZE - 1)
556 error++;
557 } else if (cp2->provider == e->gpe_pp) {
558 /* ms-ldm-data partition */
559 if (e->gpe_start != db->ph.start ||
560 e->gpe_end != db->ph.start + db->ph.size - 1)
561 error++;
562 }
563 if (error != 0) {
564 LDM_DEBUG(0, "%s: GPT partition %d boundaries "
565 "do not match with the LDM metadata",
566 e->gpe_pp->name, e->gpe_index);
567 error = ENXIO;
568 break;
569 }
570 }
571 g_topology_unlock();
572 return (error);
573 }
574
575 static int
ldm_tochdr_check(struct ldm_db * db,struct g_consumer * cp)576 ldm_tochdr_check(struct ldm_db *db, struct g_consumer *cp)
577 {
578 struct g_provider *pp;
579 struct ldm_tochdr hdr;
580 uint64_t offset, conf_size, log_size;
581 int error, found, i;
582 u_char *buf;
583
584 pp = cp->provider;
585 for (i = 0, found = 0; i < LDM_TH_COUNT; i++) {
586 offset = db->ph.db_offset + db->ph.th_offset[i];
587 buf = g_read_data(cp,
588 offset * pp->sectorsize, pp->sectorsize, &error);
589 if (buf == NULL) {
590 LDM_DEBUG(1, "%s: failed to read TOC header "
591 "at LBA %ju", pp->name, (uintmax_t)offset);
592 continue;
593 }
594 if (memcmp(buf, LDM_TH_SIGN, strlen(LDM_TH_SIGN)) != 0 ||
595 memcmp(buf + LDM_TH_NAME1_OFF, LDM_TH_NAME1,
596 strlen(LDM_TH_NAME1)) != 0 ||
597 memcmp(buf + LDM_TH_NAME2_OFF, LDM_TH_NAME2,
598 strlen(LDM_TH_NAME2)) != 0) {
599 LDM_DEBUG(1, "%s: failed to parse TOC header "
600 "at LBA %ju", pp->name, (uintmax_t)offset);
601 LDM_DUMP(buf, pp->sectorsize);
602 g_free(buf);
603 continue;
604 }
605 hdr.conf_offset = be64dec(buf + LDM_TH_CONF_OFF);
606 hdr.log_offset = be64dec(buf + LDM_TH_LOG_OFF);
607 conf_size = be64dec(buf + LDM_TH_CONFSIZE_OFF);
608 log_size = be64dec(buf + LDM_TH_LOGSIZE_OFF);
609 if (conf_size != db->ph.conf_size ||
610 hdr.conf_offset + conf_size >= LDM_DB_SIZE ||
611 log_size != db->ph.log_size ||
612 hdr.log_offset + log_size >= LDM_DB_SIZE) {
613 LDM_DEBUG(1, "%s: invalid values in the "
614 "TOC header at LBA %ju", pp->name,
615 (uintmax_t)offset);
616 LDM_DUMP(buf, pp->sectorsize);
617 g_free(buf);
618 continue;
619 }
620 g_free(buf);
621 if (found == 0)
622 memcpy(&db->th, &hdr, sizeof(hdr));
623 found = 1;
624 }
625 if (found == 0) {
626 LDM_DEBUG(0, "%s: valid LDM TOC header not found.",
627 pp->name);
628 return (ENXIO);
629 }
630 return (0);
631 }
632
633 static int
ldm_vmdbhdr_check(struct ldm_db * db,struct g_consumer * cp)634 ldm_vmdbhdr_check(struct ldm_db *db, struct g_consumer *cp)
635 {
636 struct g_provider *pp;
637 struct uuid dg_guid;
638 uint64_t offset;
639 uint32_t version;
640 int error;
641 u_char *buf;
642
643 pp = cp->provider;
644 offset = db->ph.db_offset + db->th.conf_offset;
645 buf = g_read_data(cp, offset * pp->sectorsize, pp->sectorsize,
646 &error);
647 if (buf == NULL) {
648 LDM_DEBUG(0, "%s: failed to read VMDB header at "
649 "LBA %ju", pp->name, (uintmax_t)offset);
650 return (error);
651 }
652 if (memcmp(buf, LDM_VMDB_SIGN, strlen(LDM_VMDB_SIGN)) != 0) {
653 g_free(buf);
654 LDM_DEBUG(0, "%s: failed to parse VMDB header at "
655 "LBA %ju", pp->name, (uintmax_t)offset);
656 return (ENXIO);
657 }
658 /* Check version. */
659 version = be32dec(buf + LDM_DB_VERSION_OFF);
660 if (version != 0x4000A) {
661 g_free(buf);
662 LDM_DEBUG(0, "%s: unsupported VMDB version %u.%u",
663 pp->name, version >> 16, version & 0xFFFF);
664 return (ENXIO);
665 }
666 /*
667 * Check VMDB update status:
668 * 1 - in a consistent state;
669 * 2 - in a creation phase;
670 * 3 - in a deletion phase;
671 */
672 if (be16dec(buf + LDM_DB_STATUS_OFF) != 1) {
673 g_free(buf);
674 LDM_DEBUG(0, "%s: VMDB is not in a consistent state",
675 pp->name);
676 return (ENXIO);
677 }
678 db->dh.last_seq = be32dec(buf + LDM_DB_LASTSEQ_OFF);
679 db->dh.size = be32dec(buf + LDM_DB_SIZE_OFF);
680 error = parse_uuid(buf + LDM_DB_DGGUID_OFF, &dg_guid);
681 /* Compare disk group name and guid from VMDB and private headers */
682 if (error != 0 || db->dh.size == 0 ||
683 pp->sectorsize % db->dh.size != 0 ||
684 strncmp(buf + LDM_DB_DGNAME_OFF, db->ph.dg_name, 31) != 0 ||
685 memcmp(&dg_guid, &db->ph.dg_guid, sizeof(dg_guid)) != 0 ||
686 db->dh.size * db->dh.last_seq >
687 db->ph.conf_size * pp->sectorsize) {
688 LDM_DEBUG(0, "%s: invalid values in the VMDB header",
689 pp->name);
690 LDM_DUMP(buf, pp->sectorsize);
691 g_free(buf);
692 return (EINVAL);
693 }
694 g_free(buf);
695 return (0);
696 }
697
698 static int
ldm_xvblk_handle(struct ldm_db * db,struct ldm_vblkhdr * vh,const u_char * p)699 ldm_xvblk_handle(struct ldm_db *db, struct ldm_vblkhdr *vh, const u_char *p)
700 {
701 struct ldm_xvblk *blk;
702 size_t size;
703
704 size = db->dh.size - 16;
705 LIST_FOREACH(blk, &db->xvblks, entry)
706 if (blk->group == vh->group)
707 break;
708 if (blk == NULL) {
709 blk = g_malloc(sizeof(*blk), M_WAITOK | M_ZERO);
710 blk->group = vh->group;
711 blk->size = size * vh->count + 16;
712 blk->data = g_malloc(blk->size, M_WAITOK | M_ZERO);
713 blk->map = 0xFF << vh->count;
714 LIST_INSERT_HEAD(&db->xvblks, blk, entry);
715 }
716 if ((blk->map & (1 << vh->index)) != 0) {
717 /* Block with given index has been already saved. */
718 return (EINVAL);
719 }
720 /* Copy the data block to the place related to index. */
721 memcpy(blk->data + size * vh->index + 16, p + 16, size);
722 blk->map |= 1 << vh->index;
723 return (0);
724 }
725
726 /* Read the variable-width numeric field and return new offset */
727 static int
ldm_vnum_get(const u_char * buf,int offset,uint64_t * result,size_t range)728 ldm_vnum_get(const u_char *buf, int offset, uint64_t *result, size_t range)
729 {
730 uint64_t num;
731 uint8_t len;
732
733 len = buf[offset++];
734 if (len > sizeof(uint64_t) || len + offset >= range)
735 return (-1);
736 for (num = 0; len > 0; len--)
737 num = (num << 8) | buf[offset++];
738 *result = num;
739 return (offset);
740 }
741
742 /* Read the variable-width string and return new offset */
743 static int
ldm_vstr_get(const u_char * buf,int offset,u_char * result,size_t maxlen,size_t range)744 ldm_vstr_get(const u_char *buf, int offset, u_char *result,
745 size_t maxlen, size_t range)
746 {
747 uint8_t len;
748
749 len = buf[offset++];
750 if (len >= maxlen || len + offset >= range)
751 return (-1);
752 memcpy(result, buf + offset, len);
753 result[len] = '\0';
754 return (offset + len);
755 }
756
757 /* Just skip the variable-width variable and return new offset */
758 static int
ldm_vparm_skip(const u_char * buf,int offset,size_t range)759 ldm_vparm_skip(const u_char *buf, int offset, size_t range)
760 {
761 uint8_t len;
762
763 len = buf[offset++];
764 if (offset + len >= range)
765 return (-1);
766
767 return (offset + len);
768 }
769
770 static int
ldm_vblk_handle(struct ldm_db * db,const u_char * p,size_t size)771 ldm_vblk_handle(struct ldm_db *db, const u_char *p, size_t size)
772 {
773 struct ldm_vblk *blk;
774 struct ldm_volume *volume, *last;
775 const char *errstr;
776 u_char vstr[64];
777 int error, offset;
778
779 blk = g_malloc(sizeof(*blk), M_WAITOK | M_ZERO);
780 blk->type = p[LDM_VBLK_TYPE_OFF];
781 offset = ldm_vnum_get(p, LDM_VBLK_OID_OFF, &blk->u.id, size);
782 if (offset < 0) {
783 errstr = "object id";
784 goto fail;
785 }
786 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
787 if (offset < 0) {
788 errstr = "object name";
789 goto fail;
790 }
791 switch (blk->type) {
792 /*
793 * Component VBLK fields:
794 * Offset Size Description
795 * ------------+-------+------------------------
796 * 0x18+ PS volume state
797 * 0x18+5 PN component children count
798 * 0x1D+16 PN parent's volume object id
799 * 0x2D+1 PN stripe size
800 */
801 case LDM_VBLK_T_COMPONENT:
802 offset = ldm_vparm_skip(p, offset, size);
803 if (offset < 0) {
804 errstr = "volume state";
805 goto fail;
806 }
807 offset = ldm_vparm_skip(p, offset + 5, size);
808 if (offset < 0) {
809 errstr = "children count";
810 goto fail;
811 }
812 offset = ldm_vnum_get(p, offset + 16,
813 &blk->u.comp.vol_id, size);
814 if (offset < 0) {
815 errstr = "volume id";
816 goto fail;
817 }
818 break;
819 /*
820 * Partition VBLK fields:
821 * Offset Size Description
822 * ------------+-------+------------------------
823 * 0x18+12 8 partition start offset
824 * 0x18+20 8 volume offset
825 * 0x18+28 PN partition size
826 * 0x34+ PN parent's component object id
827 * 0x34+ PN disk's object id
828 */
829 case LDM_VBLK_T_PARTITION:
830 if (offset + 28 >= size) {
831 errstr = "too small buffer";
832 goto fail;
833 }
834 blk->u.part.start = be64dec(p + offset + 12);
835 blk->u.part.offset = be64dec(p + offset + 20);
836 offset = ldm_vnum_get(p, offset + 28, &blk->u.part.size, size);
837 if (offset < 0) {
838 errstr = "partition size";
839 goto fail;
840 }
841 offset = ldm_vnum_get(p, offset, &blk->u.part.comp_id, size);
842 if (offset < 0) {
843 errstr = "component id";
844 goto fail;
845 }
846 offset = ldm_vnum_get(p, offset, &blk->u.part.disk_id, size);
847 if (offset < 0) {
848 errstr = "disk id";
849 goto fail;
850 }
851 break;
852 /*
853 * Disk VBLK fields:
854 * Offset Size Description
855 * ------------+-------+------------------------
856 * 0x18+ PS disk GUID
857 */
858 case LDM_VBLK_T_DISK:
859 errstr = "disk guid";
860 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
861 if (offset < 0)
862 goto fail;
863 error = parse_uuid(vstr, &blk->u.disk.guid);
864 if (error != 0)
865 goto fail;
866 LIST_INSERT_HEAD(&db->disks, &blk->u.disk, entry);
867 break;
868 /*
869 * Disk group VBLK fields:
870 * Offset Size Description
871 * ------------+-------+------------------------
872 * 0x18+ PS disk group GUID
873 */
874 case LDM_VBLK_T_DISKGROUP:
875 #if 0
876 strncpy(blk->u.disk_group.name, vstr,
877 sizeof(blk->u.disk_group.name));
878 offset = ldm_vstr_get(p, offset, vstr, sizeof(vstr), size);
879 if (offset < 0) {
880 errstr = "disk group guid";
881 goto fail;
882 }
883 error = parse_uuid(name, &blk->u.disk_group.guid);
884 if (error != 0) {
885 errstr = "disk group guid";
886 goto fail;
887 }
888 LIST_INSERT_HEAD(&db->groups, &blk->u.disk_group, entry);
889 #endif
890 break;
891 /*
892 * Disk VBLK fields:
893 * Offset Size Description
894 * ------------+-------+------------------------
895 * 0x18+ 16 disk GUID
896 */
897 case LDM_VBLK_T_DISK4:
898 be_uuid_dec(p + offset, &blk->u.disk.guid);
899 LIST_INSERT_HEAD(&db->disks, &blk->u.disk, entry);
900 break;
901 /*
902 * Disk group VBLK fields:
903 * Offset Size Description
904 * ------------+-------+------------------------
905 * 0x18+ 16 disk GUID
906 */
907 case LDM_VBLK_T_DISKGROUP4:
908 #if 0
909 strncpy(blk->u.disk_group.name, vstr,
910 sizeof(blk->u.disk_group.name));
911 be_uuid_dec(p + offset, &blk->u.disk.guid);
912 LIST_INSERT_HEAD(&db->groups, &blk->u.disk_group, entry);
913 #endif
914 break;
915 /*
916 * Volume VBLK fields:
917 * Offset Size Description
918 * ------------+-------+------------------------
919 * 0x18+ PS volume type
920 * 0x18+ PS unknown
921 * 0x18+ 14(S) volume state
922 * 0x18+16 1 volume number
923 * 0x18+21 PN volume children count
924 * 0x2D+16 PN volume size
925 * 0x3D+4 1 partition type
926 */
927 case LDM_VBLK_T_VOLUME:
928 offset = ldm_vparm_skip(p, offset, size);
929 if (offset < 0) {
930 errstr = "volume type";
931 goto fail;
932 }
933 offset = ldm_vparm_skip(p, offset, size);
934 if (offset < 0) {
935 errstr = "unknown param";
936 goto fail;
937 }
938 if (offset + 21 >= size) {
939 errstr = "too small buffer";
940 goto fail;
941 }
942 blk->u.vol.number = p[offset + 16];
943 offset = ldm_vparm_skip(p, offset + 21, size);
944 if (offset < 0) {
945 errstr = "children count";
946 goto fail;
947 }
948 offset = ldm_vnum_get(p, offset + 16, &blk->u.vol.size, size);
949 if (offset < 0) {
950 errstr = "volume size";
951 goto fail;
952 }
953 if (offset + 4 >= size) {
954 errstr = "too small buffer";
955 goto fail;
956 }
957 blk->u.vol.part_type = p[offset + 4];
958 /* keep volumes ordered by volume number */
959 last = NULL;
960 LIST_FOREACH(volume, &db->volumes, entry) {
961 if (volume->number > blk->u.vol.number)
962 break;
963 last = volume;
964 }
965 if (last != NULL)
966 LIST_INSERT_AFTER(last, &blk->u.vol, entry);
967 else
968 LIST_INSERT_HEAD(&db->volumes, &blk->u.vol, entry);
969 break;
970 default:
971 LDM_DEBUG(1, "unknown VBLK type 0x%02x\n", blk->type);
972 LDM_DUMP(p, size);
973 }
974 LIST_INSERT_HEAD(&db->vblks, blk, entry);
975 return (0);
976 fail:
977 LDM_DEBUG(0, "failed to parse '%s' in VBLK of type 0x%02x\n",
978 errstr, blk->type);
979 LDM_DUMP(p, size);
980 g_free(blk);
981 return (EINVAL);
982 }
983
984 static void
ldm_vmdb_free(struct ldm_db * db)985 ldm_vmdb_free(struct ldm_db *db)
986 {
987 struct ldm_vblk *vblk;
988 struct ldm_xvblk *xvblk;
989
990 while (!LIST_EMPTY(&db->xvblks)) {
991 xvblk = LIST_FIRST(&db->xvblks);
992 LIST_REMOVE(xvblk, entry);
993 g_free(xvblk->data);
994 g_free(xvblk);
995 }
996 while (!LIST_EMPTY(&db->vblks)) {
997 vblk = LIST_FIRST(&db->vblks);
998 LIST_REMOVE(vblk, entry);
999 g_free(vblk);
1000 }
1001 }
1002
1003 static int
ldm_vmdb_parse(struct ldm_db * db,struct g_consumer * cp)1004 ldm_vmdb_parse(struct ldm_db *db, struct g_consumer *cp)
1005 {
1006 struct g_provider *pp;
1007 struct ldm_vblk *vblk;
1008 struct ldm_xvblk *xvblk;
1009 struct ldm_volume *volume;
1010 struct ldm_component *comp;
1011 struct ldm_vblkhdr vh;
1012 u_char *buf, *p;
1013 size_t size, n, sectors;
1014 uint64_t offset;
1015 int error;
1016
1017 pp = cp->provider;
1018 size = howmany(db->dh.last_seq * db->dh.size, pp->sectorsize);
1019 size -= 1; /* one sector takes vmdb header */
1020 for (n = 0; n < size; n += maxphys / pp->sectorsize) {
1021 offset = db->ph.db_offset + db->th.conf_offset + n + 1;
1022 sectors = (size - n) > (maxphys / pp->sectorsize) ?
1023 maxphys / pp->sectorsize : size - n;
1024 /* read VBLKs */
1025 buf = g_read_data(cp, offset * pp->sectorsize,
1026 sectors * pp->sectorsize, &error);
1027 if (buf == NULL) {
1028 LDM_DEBUG(0, "%s: failed to read VBLK\n",
1029 pp->name);
1030 goto fail;
1031 }
1032 for (p = buf; p < buf + sectors * pp->sectorsize;
1033 p += db->dh.size) {
1034 if (memcmp(p, LDM_VBLK_SIGN,
1035 strlen(LDM_VBLK_SIGN)) != 0) {
1036 LDM_DEBUG(0, "%s: no VBLK signature\n",
1037 pp->name);
1038 LDM_DUMP(p, db->dh.size);
1039 goto fail;
1040 }
1041 vh.seq = be32dec(p + LDM_VBLK_SEQ_OFF);
1042 vh.group = be32dec(p + LDM_VBLK_GROUP_OFF);
1043 /* skip empty blocks */
1044 if (vh.seq == 0 || vh.group == 0)
1045 continue;
1046 vh.index = be16dec(p + LDM_VBLK_INDEX_OFF);
1047 vh.count = be16dec(p + LDM_VBLK_COUNT_OFF);
1048 if (vh.count == 0 || vh.count > 4 ||
1049 vh.seq > db->dh.last_seq) {
1050 LDM_DEBUG(0, "%s: invalid values "
1051 "in the VBLK header\n", pp->name);
1052 LDM_DUMP(p, db->dh.size);
1053 goto fail;
1054 }
1055 if (vh.count > 1) {
1056 error = ldm_xvblk_handle(db, &vh, p);
1057 if (error != 0) {
1058 LDM_DEBUG(0, "%s: xVBLK "
1059 "is corrupted\n", pp->name);
1060 LDM_DUMP(p, db->dh.size);
1061 goto fail;
1062 }
1063 continue;
1064 }
1065 if (be16dec(p + 16) != 0)
1066 LDM_DEBUG(1, "%s: VBLK update"
1067 " status is %u\n", pp->name,
1068 be16dec(p + 16));
1069 error = ldm_vblk_handle(db, p, db->dh.size);
1070 if (error != 0)
1071 goto fail;
1072 }
1073 g_free(buf);
1074 buf = NULL;
1075 }
1076 /* Parse xVBLKs */
1077 while (!LIST_EMPTY(&db->xvblks)) {
1078 xvblk = LIST_FIRST(&db->xvblks);
1079 if (xvblk->map == 0xFF) {
1080 error = ldm_vblk_handle(db, xvblk->data, xvblk->size);
1081 if (error != 0)
1082 goto fail;
1083 } else {
1084 LDM_DEBUG(0, "%s: incomplete or corrupt "
1085 "xVBLK found\n", pp->name);
1086 goto fail;
1087 }
1088 LIST_REMOVE(xvblk, entry);
1089 g_free(xvblk->data);
1090 g_free(xvblk);
1091 }
1092 /* construct all VBLKs relations */
1093 LIST_FOREACH(volume, &db->volumes, entry) {
1094 LIST_FOREACH(vblk, &db->vblks, entry)
1095 if (vblk->type == LDM_VBLK_T_COMPONENT &&
1096 vblk->u.comp.vol_id == volume->id) {
1097 LIST_INSERT_HEAD(&volume->components,
1098 &vblk->u.comp, entry);
1099 volume->count++;
1100 }
1101 LIST_FOREACH(comp, &volume->components, entry)
1102 LIST_FOREACH(vblk, &db->vblks, entry)
1103 if (vblk->type == LDM_VBLK_T_PARTITION &&
1104 vblk->u.part.comp_id == comp->id) {
1105 LIST_INSERT_HEAD(&comp->partitions,
1106 &vblk->u.part, entry);
1107 comp->count++;
1108 }
1109 }
1110 return (0);
1111 fail:
1112 ldm_vmdb_free(db);
1113 g_free(buf);
1114 return (ENXIO);
1115 }
1116
1117 static int
g_part_ldm_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)1118 g_part_ldm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
1119 struct g_part_parms *gpp)
1120 {
1121
1122 return (ENOSYS);
1123 }
1124
1125 static int
g_part_ldm_bootcode(struct g_part_table * basetable,struct g_part_parms * gpp)1126 g_part_ldm_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
1127 {
1128
1129 return (ENOSYS);
1130 }
1131
1132 static int
g_part_ldm_create(struct g_part_table * basetable,struct g_part_parms * gpp)1133 g_part_ldm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
1134 {
1135
1136 return (ENOSYS);
1137 }
1138
1139 static int
g_part_ldm_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)1140 g_part_ldm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
1141 {
1142 struct g_part_ldm_table *table;
1143 struct g_provider *pp;
1144
1145 table = (struct g_part_ldm_table *)basetable;
1146 /*
1147 * To destroy LDM on a disk partitioned with GPT we should delete
1148 * ms-ldm-metadata partition, but we can't do this via standard
1149 * GEOM_PART method.
1150 */
1151 if (table->is_gpt)
1152 return (ENOSYS);
1153 pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
1154 /*
1155 * To destroy LDM we should wipe MBR, first private header and
1156 * backup private headers.
1157 */
1158 basetable->gpt_smhead = (1 << ldm_ph_off[0]) | 1;
1159 /*
1160 * Don't touch last backup private header when LDM database is
1161 * not located in the last 1MByte area.
1162 * XXX: can't remove all blocks.
1163 */
1164 if (table->db_offset + LDM_DB_SIZE ==
1165 pp->mediasize / pp->sectorsize)
1166 basetable->gpt_smtail = 1;
1167 return (0);
1168 }
1169
1170 static void
g_part_ldm_dumpconf(struct g_part_table * basetable,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)1171 g_part_ldm_dumpconf(struct g_part_table *basetable,
1172 struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
1173 {
1174 struct g_part_ldm_entry *entry;
1175
1176 entry = (struct g_part_ldm_entry *)baseentry;
1177 if (indent == NULL) {
1178 /* conftxt: libdisk compatibility */
1179 sbuf_printf(sb, " xs LDM xt %u", entry->type);
1180 } else if (entry != NULL) {
1181 /* confxml: partition entry information */
1182 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
1183 entry->type);
1184 } else {
1185 /* confxml: scheme information */
1186 }
1187 }
1188
1189 static int
g_part_ldm_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)1190 g_part_ldm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
1191 {
1192
1193 return (0);
1194 }
1195
1196 static int
g_part_ldm_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)1197 g_part_ldm_modify(struct g_part_table *basetable,
1198 struct g_part_entry *baseentry, struct g_part_parms *gpp)
1199 {
1200
1201 return (ENOSYS);
1202 }
1203
1204 static const char *
g_part_ldm_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)1205 g_part_ldm_name(struct g_part_table *table, struct g_part_entry *baseentry,
1206 char *buf, size_t bufsz)
1207 {
1208
1209 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
1210 return (buf);
1211 }
1212
1213 static int
ldm_gpt_probe(struct g_part_table * basetable,struct g_consumer * cp)1214 ldm_gpt_probe(struct g_part_table *basetable, struct g_consumer *cp)
1215 {
1216 struct g_part_ldm_table *table;
1217 struct g_part_table *gpt;
1218 struct g_part_entry *entry;
1219 struct g_consumer *cp2;
1220 struct gpt_ent *part;
1221 u_char *buf;
1222 int error;
1223
1224 /*
1225 * XXX: We use some knowledge about GEOM_PART_GPT internal
1226 * structures, but it is easier than parse GPT by himself.
1227 */
1228 g_topology_lock();
1229 gpt = cp->provider->geom->softc;
1230 LIST_FOREACH(entry, &gpt->gpt_entry, gpe_entry) {
1231 part = (struct gpt_ent *)(entry + 1);
1232 /* Search ms-ldm-metadata partition */
1233 if (memcmp(&part->ent_type,
1234 &gpt_uuid_ms_ldm_metadata, sizeof(struct uuid)) != 0 ||
1235 entry->gpe_end - entry->gpe_start < LDM_DB_SIZE - 1)
1236 continue;
1237
1238 /* Create new consumer and attach it to metadata partition */
1239 cp2 = g_new_consumer(cp->geom);
1240 error = g_attach(cp2, entry->gpe_pp);
1241 if (error != 0) {
1242 g_destroy_consumer(cp2);
1243 g_topology_unlock();
1244 return (ENXIO);
1245 }
1246 error = g_access(cp2, 1, 0, 0);
1247 if (error != 0) {
1248 g_detach(cp2);
1249 g_destroy_consumer(cp2);
1250 g_topology_unlock();
1251 return (ENXIO);
1252 }
1253 g_topology_unlock();
1254
1255 LDM_DEBUG(2, "%s: LDM metadata partition %s found in the GPT",
1256 cp->provider->name, cp2->provider->name);
1257 /* Read the LDM private header */
1258 buf = ldm_privhdr_read(cp2,
1259 ldm_ph_off[LDM_PH_GPTINDEX] * cp2->provider->sectorsize,
1260 &error);
1261 if (buf != NULL) {
1262 table = (struct g_part_ldm_table *)basetable;
1263 table->is_gpt = 1;
1264 g_free(buf);
1265 return (G_PART_PROBE_PRI_HIGH);
1266 }
1267
1268 /* second consumer is no longer needed. */
1269 g_topology_lock();
1270 g_access(cp2, -1, 0, 0);
1271 g_detach(cp2);
1272 g_destroy_consumer(cp2);
1273 break;
1274 }
1275 g_topology_unlock();
1276 return (ENXIO);
1277 }
1278
1279 static int
g_part_ldm_probe(struct g_part_table * basetable,struct g_consumer * cp)1280 g_part_ldm_probe(struct g_part_table *basetable, struct g_consumer *cp)
1281 {
1282 struct g_provider *pp;
1283 u_char *buf, type[64];
1284 int error, idx;
1285
1286 pp = cp->provider;
1287 if (pp->sectorsize != 512)
1288 return (ENXIO);
1289
1290 error = g_getattr("PART::scheme", cp, &type);
1291 if (error == 0 && strcmp(type, "GPT") == 0) {
1292 if (g_getattr("PART::type", cp, &type) != 0 ||
1293 strcmp(type, "ms-ldm-data") != 0)
1294 return (ENXIO);
1295 error = ldm_gpt_probe(basetable, cp);
1296 return (error);
1297 }
1298
1299 if (basetable->gpt_depth != 0)
1300 return (ENXIO);
1301
1302 /* LDM has 1M metadata area */
1303 if (pp->mediasize <= 1024 * 1024)
1304 return (ENOSPC);
1305
1306 /* Check that there's a MBR */
1307 buf = g_read_data(cp, 0, pp->sectorsize, &error);
1308 if (buf == NULL)
1309 return (error);
1310
1311 if (le16dec(buf + DOSMAGICOFFSET) != DOSMAGIC) {
1312 g_free(buf);
1313 return (ENXIO);
1314 }
1315 error = ENXIO;
1316 /* Check that we have LDM partitions in the MBR */
1317 for (idx = 0; idx < NDOSPART && error != 0; idx++) {
1318 if (buf[DOSPARTOFF + idx * DOSPARTSIZE + 4] == DOSPTYP_LDM)
1319 error = 0;
1320 }
1321 g_free(buf);
1322 if (error == 0) {
1323 LDM_DEBUG(2, "%s: LDM data partitions found in MBR",
1324 pp->name);
1325 /* Read the LDM private header */
1326 buf = ldm_privhdr_read(cp,
1327 ldm_ph_off[LDM_PH_MBRINDEX] * pp->sectorsize, &error);
1328 if (buf == NULL)
1329 return (error);
1330 g_free(buf);
1331 return (G_PART_PROBE_PRI_HIGH);
1332 }
1333 return (error);
1334 }
1335
1336 static int
g_part_ldm_read(struct g_part_table * basetable,struct g_consumer * cp)1337 g_part_ldm_read(struct g_part_table *basetable, struct g_consumer *cp)
1338 {
1339 struct g_part_ldm_table *table;
1340 struct g_part_ldm_entry *entry;
1341 struct g_consumer *cp2;
1342 struct ldm_component *comp;
1343 struct ldm_partition *part;
1344 struct ldm_volume *vol;
1345 struct ldm_disk *disk;
1346 struct ldm_db db;
1347 int error, index, skipped;
1348
1349 table = (struct g_part_ldm_table *)basetable;
1350 memset(&db, 0, sizeof(db));
1351 cp2 = cp; /* ms-ldm-data */
1352 if (table->is_gpt)
1353 cp = LIST_FIRST(&cp->geom->consumer); /* ms-ldm-metadata */
1354 /* Read and parse LDM private headers. */
1355 error = ldm_privhdr_check(&db, cp, table->is_gpt);
1356 if (error != 0)
1357 goto gpt_cleanup;
1358 basetable->gpt_first = table->is_gpt ? 0: db.ph.start;
1359 basetable->gpt_last = basetable->gpt_first + db.ph.size - 1;
1360 table->db_offset = db.ph.db_offset;
1361 /* Make additional checks for GPT */
1362 if (table->is_gpt) {
1363 error = ldm_gpt_check(&db, cp);
1364 if (error != 0)
1365 goto gpt_cleanup;
1366 /*
1367 * Now we should reset database offset to zero, because our
1368 * consumer cp is attached to the ms-ldm-metadata partition
1369 * and we don't need add db_offset to read from it.
1370 */
1371 db.ph.db_offset = 0;
1372 }
1373 /* Read and parse LDM TOC headers. */
1374 error = ldm_tochdr_check(&db, cp);
1375 if (error != 0)
1376 goto gpt_cleanup;
1377 /* Read and parse LDM VMDB header. */
1378 error = ldm_vmdbhdr_check(&db, cp);
1379 if (error != 0)
1380 goto gpt_cleanup;
1381 error = ldm_vmdb_parse(&db, cp);
1382 /*
1383 * For the GPT case we must detach and destroy
1384 * second consumer before return.
1385 */
1386 gpt_cleanup:
1387 if (table->is_gpt) {
1388 g_topology_lock();
1389 g_access(cp, -1, 0, 0);
1390 g_detach(cp);
1391 g_destroy_consumer(cp);
1392 g_topology_unlock();
1393 cp = cp2;
1394 }
1395 if (error != 0)
1396 return (error);
1397 /* Search current disk in the disk list. */
1398 LIST_FOREACH(disk, &db.disks, entry)
1399 if (memcmp(&disk->guid, &db.ph.disk_guid,
1400 sizeof(struct uuid)) == 0)
1401 break;
1402 if (disk == NULL) {
1403 LDM_DEBUG(1, "%s: no LDM volumes on this disk",
1404 cp->provider->name);
1405 ldm_vmdb_free(&db);
1406 return (ENXIO);
1407 }
1408 index = 1;
1409 LIST_FOREACH(vol, &db.volumes, entry) {
1410 LIST_FOREACH(comp, &vol->components, entry) {
1411 /* Skip volumes from different disks. */
1412 part = LIST_FIRST(&comp->partitions);
1413 if (part->disk_id != disk->id)
1414 continue;
1415 skipped = 0;
1416 /* We don't support spanned and striped volumes. */
1417 if (comp->count > 1 || part->offset != 0) {
1418 LDM_DEBUG(1, "%s: LDM volume component "
1419 "%ju has %u partitions. Skipped",
1420 cp->provider->name, (uintmax_t)comp->id,
1421 comp->count);
1422 skipped = 1;
1423 }
1424 /*
1425 * Allow mirrored volumes only when they are explicitly
1426 * allowed with kern.geom.part.ldm.show_mirrors=1.
1427 */
1428 if (vol->count > 1 && show_mirrors == 0) {
1429 LDM_DEBUG(1, "%s: LDM volume %ju has %u "
1430 "components. Skipped",
1431 cp->provider->name, (uintmax_t)vol->id,
1432 vol->count);
1433 skipped = 1;
1434 }
1435 entry = (struct g_part_ldm_entry *)g_part_new_entry(
1436 basetable, index++,
1437 basetable->gpt_first + part->start,
1438 basetable->gpt_first + part->start +
1439 part->size - 1);
1440 /*
1441 * Mark skipped partition as ms-ldm-data partition.
1442 * We do not support them, but it is better to show
1443 * that we have something there, than just show
1444 * free space.
1445 */
1446 if (skipped == 0)
1447 entry->type = vol->part_type;
1448 else
1449 entry->type = DOSPTYP_LDM;
1450 LDM_DEBUG(1, "%s: new volume id: %ju, start: %ju,"
1451 " end: %ju, type: 0x%02x\n", cp->provider->name,
1452 (uintmax_t)part->id,(uintmax_t)part->start +
1453 basetable->gpt_first, (uintmax_t)part->start +
1454 part->size + basetable->gpt_first - 1,
1455 vol->part_type);
1456 }
1457 }
1458 ldm_vmdb_free(&db);
1459 return (error);
1460 }
1461
1462 static const char *
g_part_ldm_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)1463 g_part_ldm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
1464 char *buf, size_t bufsz)
1465 {
1466 struct g_part_ldm_entry *entry;
1467 int i;
1468
1469 entry = (struct g_part_ldm_entry *)baseentry;
1470 for (i = 0; i < nitems(ldm_alias_match); i++) {
1471 if (ldm_alias_match[i].typ == entry->type)
1472 return (g_part_alias_name(ldm_alias_match[i].alias));
1473 }
1474 snprintf(buf, bufsz, "!%d", entry->type);
1475 return (buf);
1476 }
1477
1478 static int
g_part_ldm_write(struct g_part_table * basetable,struct g_consumer * cp)1479 g_part_ldm_write(struct g_part_table *basetable, struct g_consumer *cp)
1480 {
1481
1482 return (ENOSYS);
1483 }
1484