1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
5 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
6 * Copyright (c) 2001-2012 Anton Altaparmakov
7 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
9 * Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <linux/stringify.h>
15 #include <linux/kernel.h>
16 #include <linux/uuid.h>
17 #include <linux/msdos_partition.h>
23 * ldm_debug/info/error/crit - Output an error message
24 * @f: A printf format string containing the message
25 * @...: Variables to substitute into @f
27 * ldm_debug() writes a DEBUG level message to the syslog but only if the
28 * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
30 #ifndef CONFIG_LDM_DEBUG
31 #define ldm_debug(...) do {} while (0)
33 #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
36 #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
37 #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
38 #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
41 void _ldm_printk(const char *level, const char *function, const char *fmt, ...)
51 printk("%s%s(): %pV\n", level, function, &vaf);
57 * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
58 * @data: Raw database PRIVHEAD structure loaded from the device
59 * @ph: In-memory privhead structure in which to return parsed information
61 * This parses the LDM database PRIVHEAD structure supplied in @data and
62 * sets up the in-memory privhead structure @ph with the obtained information.
64 * Return: 'true' @ph contains the PRIVHEAD data
65 * 'false' @ph contents are undefined
67 static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
69 bool is_vista = false;
72 if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
73 ldm_error("Cannot find PRIVHEAD structure. LDM database is"
74 " corrupt. Aborting.");
77 ph->ver_major = get_unaligned_be16(data + 0x000C);
78 ph->ver_minor = get_unaligned_be16(data + 0x000E);
79 ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
80 ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
81 ph->config_start = get_unaligned_be64(data + 0x012B);
82 ph->config_size = get_unaligned_be64(data + 0x0133);
83 /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
84 if (ph->ver_major == 2 && ph->ver_minor == 12)
86 if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
87 ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
88 " Aborting.", ph->ver_major, ph->ver_minor);
91 ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
92 ph->ver_minor, is_vista ? "Vista" : "2000/XP");
93 if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
94 /* Warn the user and continue, carefully. */
95 ldm_info("Database is normally %u bytes, it claims to "
96 "be %llu bytes.", LDM_DB_SIZE,
97 (unsigned long long)ph->config_size);
99 if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
100 ph->logical_disk_size > ph->config_start)) {
101 ldm_error("PRIVHEAD disk size doesn't match real disk size");
104 if (uuid_parse(data + 0x0030, &ph->disk_id)) {
105 ldm_error("PRIVHEAD contains an invalid GUID.");
108 ldm_debug("Parsed PRIVHEAD successfully.");
113 * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
114 * @data: Raw database TOCBLOCK structure loaded from the device
115 * @toc: In-memory toc structure in which to return parsed information
117 * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
118 * in @data and sets up the in-memory tocblock structure @toc with the obtained
121 * N.B. The *_start and *_size values returned in @toc are not range-checked.
123 * Return: 'true' @toc contains the TOCBLOCK data
124 * 'false' @toc contents are undefined
126 static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
128 BUG_ON (!data || !toc);
130 if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
131 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
134 strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
135 toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
136 toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
137 toc->bitmap1_size = get_unaligned_be64(data + 0x36);
139 if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
140 sizeof (toc->bitmap1_name)) != 0) {
141 ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
142 TOC_BITMAP1, toc->bitmap1_name);
145 strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
146 toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
147 toc->bitmap2_start = get_unaligned_be64(data + 0x50);
148 toc->bitmap2_size = get_unaligned_be64(data + 0x58);
149 if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
150 sizeof (toc->bitmap2_name)) != 0) {
151 ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
152 TOC_BITMAP2, toc->bitmap2_name);
155 ldm_debug ("Parsed TOCBLOCK successfully.");
160 * ldm_parse_vmdb - Read the LDM Database VMDB structure
161 * @data: Raw database VMDB structure loaded from the device
162 * @vm: In-memory vmdb structure in which to return parsed information
164 * This parses the LDM Database VMDB structure supplied in @data and sets up
165 * the in-memory vmdb structure @vm with the obtained information.
167 * N.B. The *_start, *_size and *_seq values will be range-checked later.
169 * Return: 'true' @vm contains VMDB info
170 * 'false' @vm contents are undefined
172 static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
174 BUG_ON (!data || !vm);
176 if (MAGIC_VMDB != get_unaligned_be32(data)) {
177 ldm_crit ("Cannot find the VMDB, database may be corrupt.");
181 vm->ver_major = get_unaligned_be16(data + 0x12);
182 vm->ver_minor = get_unaligned_be16(data + 0x14);
183 if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
184 ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
185 "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
189 vm->vblk_size = get_unaligned_be32(data + 0x08);
190 if (vm->vblk_size == 0) {
191 ldm_error ("Illegal VBLK size");
195 vm->vblk_offset = get_unaligned_be32(data + 0x0C);
196 vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
198 ldm_debug ("Parsed VMDB successfully.");
203 * ldm_compare_privheads - Compare two privhead objects
204 * @ph1: First privhead
205 * @ph2: Second privhead
207 * This compares the two privhead structures @ph1 and @ph2.
209 * Return: 'true' Identical
212 static bool ldm_compare_privheads (const struct privhead *ph1,
213 const struct privhead *ph2)
215 BUG_ON (!ph1 || !ph2);
217 return ((ph1->ver_major == ph2->ver_major) &&
218 (ph1->ver_minor == ph2->ver_minor) &&
219 (ph1->logical_disk_start == ph2->logical_disk_start) &&
220 (ph1->logical_disk_size == ph2->logical_disk_size) &&
221 (ph1->config_start == ph2->config_start) &&
222 (ph1->config_size == ph2->config_size) &&
223 uuid_equal(&ph1->disk_id, &ph2->disk_id));
227 * ldm_compare_tocblocks - Compare two tocblock objects
231 * This compares the two tocblock structures @toc1 and @toc2.
233 * Return: 'true' Identical
236 static bool ldm_compare_tocblocks (const struct tocblock *toc1,
237 const struct tocblock *toc2)
239 BUG_ON (!toc1 || !toc2);
241 return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
242 (toc1->bitmap1_size == toc2->bitmap1_size) &&
243 (toc1->bitmap2_start == toc2->bitmap2_start) &&
244 (toc1->bitmap2_size == toc2->bitmap2_size) &&
245 !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
246 sizeof (toc1->bitmap1_name)) &&
247 !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
248 sizeof (toc1->bitmap2_name)));
252 * ldm_validate_privheads - Compare the primary privhead with its backups
253 * @state: Partition check state including device holding the LDM Database
254 * @ph1: Memory struct to fill with ph contents
256 * Read and compare all three privheads from disk.
258 * The privheads on disk show the size and location of the main disk area and
259 * the configuration area (the database). The values are range-checked against
260 * @hd, which contains the real size of the disk.
262 * Return: 'true' Success
265 static bool ldm_validate_privheads(struct parsed_partitions *state,
266 struct privhead *ph1)
268 static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
269 struct privhead *ph[3] = { ph1 };
276 BUG_ON (!state || !ph1);
278 ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
279 ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
280 if (!ph[1] || !ph[2]) {
281 ldm_crit ("Out of memory.");
285 /* off[1 & 2] are relative to ph[0]->config_start */
286 ph[0]->config_start = 0;
288 /* Read and parse privheads */
289 for (i = 0; i < 3; i++) {
290 data = read_part_sector(state, ph[0]->config_start + off[i],
293 ldm_crit ("Disk read failed.");
296 result = ldm_parse_privhead (data, ph[i]);
297 put_dev_sector (sect);
299 ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
301 goto out; /* Already logged */
303 break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
307 num_sects = get_capacity(state->disk);
309 if ((ph[0]->config_start > num_sects) ||
310 ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
311 ldm_crit ("Database extends beyond the end of the disk.");
315 if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
316 ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
317 > ph[0]->config_start)) {
318 ldm_crit ("Disk and database overlap.");
322 if (!ldm_compare_privheads (ph[0], ph[1])) {
323 ldm_crit ("Primary and backup PRIVHEADs don't match.");
326 /* FIXME ignore this for now
327 if (!ldm_compare_privheads (ph[0], ph[2])) {
328 ldm_crit ("Primary and backup PRIVHEADs don't match.");
331 ldm_debug ("Validated PRIVHEADs successfully.");
340 * ldm_validate_tocblocks - Validate the table of contents and its backups
341 * @state: Partition check state including device holding the LDM Database
342 * @base: Offset, into @state->disk, of the database
343 * @ldb: Cache of the database structures
345 * Find and compare the four tables of contents of the LDM Database stored on
346 * @state->disk and return the parsed information into @toc1.
348 * The offsets and sizes of the configs are range-checked against a privhead.
350 * Return: 'true' @toc1 contains validated TOCBLOCK info
351 * 'false' @toc1 contents are undefined
353 static bool ldm_validate_tocblocks(struct parsed_partitions *state,
354 unsigned long base, struct ldmdb *ldb)
356 static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
357 struct tocblock *tb[4];
364 BUG_ON(!state || !ldb);
367 tb[1] = kmalloc_array(3, sizeof(*tb[1]), GFP_KERNEL);
369 ldm_crit("Out of memory.");
372 tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
373 tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
375 * Try to read and parse all four TOCBLOCKs.
377 * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
378 * skip any that fail as long as we get at least one valid TOCBLOCK.
380 for (nr_tbs = i = 0; i < 4; i++) {
381 data = read_part_sector(state, base + off[i], §);
383 ldm_error("Disk read failed for TOCBLOCK %d.", i);
386 if (ldm_parse_tocblock(data, tb[nr_tbs]))
388 put_dev_sector(sect);
391 ldm_crit("Failed to find a valid TOCBLOCK.");
394 /* Range check the TOCBLOCK against a privhead. */
395 if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
396 ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
398 ldm_crit("The bitmaps are out of range. Giving up.");
401 /* Compare all loaded TOCBLOCKs. */
402 for (i = 1; i < nr_tbs; i++) {
403 if (!ldm_compare_tocblocks(tb[0], tb[i])) {
404 ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
408 ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
416 * ldm_validate_vmdb - Read the VMDB and validate it
417 * @state: Partition check state including device holding the LDM Database
418 * @base: Offset, into @bdev, of the database
419 * @ldb: Cache of the database structures
421 * Find the vmdb of the LDM Database stored on @bdev and return the parsed
422 * information in @ldb.
424 * Return: 'true' @ldb contains validated VBDB info
425 * 'false' @ldb contents are undefined
427 static bool ldm_validate_vmdb(struct parsed_partitions *state,
428 unsigned long base, struct ldmdb *ldb)
434 struct tocblock *toc;
436 BUG_ON (!state || !ldb);
441 data = read_part_sector(state, base + OFF_VMDB, §);
443 ldm_crit ("Disk read failed.");
447 if (!ldm_parse_vmdb (data, vm))
448 goto out; /* Already logged */
450 /* Are there uncommitted transactions? */
451 if (get_unaligned_be16(data + 0x10) != 0x01) {
452 ldm_crit ("Database is not in a consistent state. Aborting.");
456 if (vm->vblk_offset != 512)
457 ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
460 * The last_vblkd_seq can be before the end of the vmdb, just make sure
461 * it is not out of bounds.
463 if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
464 ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
465 "Database is corrupt. Aborting.");
471 put_dev_sector (sect);
477 * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
478 * @state: Partition check state including device holding the LDM Database
480 * This function provides a weak test to decide whether the device is a dynamic
481 * disk or not. It looks for an MS-DOS-style partition table containing at
482 * least one partition of type 0x42 (formerly SFS, now used by Windows for
485 * N.B. The only possible error can come from the read_part_sector and that is
486 * only likely to happen if the underlying device is strange. If that IS
487 * the case we should return zero to let someone else try.
489 * Return: 'true' @state->disk is a dynamic disk
490 * 'false' @state->disk is not a dynamic disk, or an error occurred
492 static bool ldm_validate_partition_table(struct parsed_partitions *state)
496 struct msdos_partition *p;
502 data = read_part_sector(state, 0, §);
504 ldm_info ("Disk read failed.");
508 if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
511 p = (struct msdos_partition *)(data + 0x01BE);
512 for (i = 0; i < 4; i++, p++)
513 if (p->sys_ind == LDM_PARTITION) {
519 ldm_debug ("Found W2K dynamic disk partition type.");
522 put_dev_sector (sect);
527 * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
528 * @ldb: Cache of the database structures
530 * The LDM Database contains a list of all partitions on all dynamic disks.
531 * The primary PRIVHEAD, at the beginning of the physical disk, tells us
532 * the GUID of this disk. This function searches for the GUID in a linked
535 * Return: Pointer, A matching vblk was found
536 * NULL, No match, or an error
538 static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
540 struct list_head *item;
544 list_for_each (item, &ldb->v_disk) {
545 struct vblk *v = list_entry (item, struct vblk, list);
546 if (uuid_equal(&v->vblk.disk.disk_id, &ldb->ph.disk_id))
554 * ldm_create_data_partitions - Create data partitions for this device
555 * @pp: List of the partitions parsed so far
556 * @ldb: Cache of the database structures
558 * The database contains ALL the partitions for ALL disk groups, so we need to
559 * filter out this specific disk. Using the disk's object id, we can find all
560 * the partitions in the database that belong to this disk.
562 * Add each partition in our database, to the parsed_partitions structure.
564 * N.B. This function creates the partitions in the order it finds partition
565 * objects in the linked list.
567 * Return: 'true' Partition created
568 * 'false' Error, probably a range checking problem
570 static bool ldm_create_data_partitions (struct parsed_partitions *pp,
571 const struct ldmdb *ldb)
573 struct list_head *item;
576 struct vblk_part *part;
579 BUG_ON (!pp || !ldb);
581 disk = ldm_get_disk_objid (ldb);
583 ldm_crit ("Can't find the ID of this disk in the database.");
587 strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
589 /* Create the data partitions */
590 list_for_each (item, &ldb->v_part) {
591 vb = list_entry (item, struct vblk, list);
592 part = &vb->vblk.part;
594 if (part->disk_id != disk->obj_id)
597 put_partition (pp, part_num, ldb->ph.logical_disk_start +
598 part->start, part->size);
602 strlcat(pp->pp_buf, "\n", PAGE_SIZE);
608 * ldm_relative - Calculate the next relative offset
609 * @buffer: Block of data being worked on
610 * @buflen: Size of the block of data
611 * @base: Size of the previous fixed width fields
612 * @offset: Cumulative size of the previous variable-width fields
614 * Because many of the VBLK fields are variable-width, it's necessary
615 * to calculate each offset based on the previous one and the length
616 * of the field it pointed to.
618 * Return: -1 Error, the calculated offset exceeded the size of the buffer
619 * n OK, a range-checked offset into buffer
621 static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
625 if (!buffer || offset < 0 || base > buflen) {
627 ldm_error("!buffer");
629 ldm_error("offset (%d) < 0", offset);
631 ldm_error("base (%d) > buflen (%d)", base, buflen);
634 if (base + buffer[base] >= buflen) {
635 ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
636 buffer[base], buflen);
639 return buffer[base] + offset + 1;
643 * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
644 * @block: Pointer to the variable-width number to convert
646 * Large numbers in the LDM Database are often stored in a packed format. Each
647 * number is prefixed by a one byte width marker. All numbers in the database
648 * are stored in big-endian byte order. This function reads one of these
649 * numbers and returns the result
651 * N.B. This function DOES NOT perform any range checking, though the most
652 * it will read is eight bytes.
655 * 0 Zero, or an error occurred
657 static u64 ldm_get_vnum (const u8 *block)
666 if (length && length <= 8)
668 tmp = (tmp << 8) | *block++;
670 ldm_error ("Illegal length %d.", length);
676 * ldm_get_vstr - Read a length-prefixed string into a buffer
677 * @block: Pointer to the length marker
678 * @buffer: Location to copy string to
679 * @buflen: Size of the output buffer
681 * Many of the strings in the LDM Database are not NULL terminated. Instead
682 * they are prefixed by a one byte length marker. This function copies one of
683 * these strings into a buffer.
685 * N.B. This function DOES NOT perform any range checking on the input.
686 * If the buffer is too small, the output will be truncated.
688 * Return: 0, Error and @buffer contents are undefined
689 * n, String length in characters (excluding NULL)
690 * buflen-1, String was truncated.
692 static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
696 BUG_ON (!block || !buffer);
699 if (length >= buflen) {
700 ldm_error ("Truncating string %d -> %d.", length, buflen);
703 memcpy (buffer, block + 1, length);
710 * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
711 * @buffer: Block of data being worked on
712 * @buflen: Size of the block of data
713 * @vb: In-memory vblk in which to return information
715 * Read a raw VBLK Component object (version 3) into a vblk structure.
717 * Return: 'true' @vb contains a Component VBLK
718 * 'false' @vb contents are not defined
720 static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
722 int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
723 struct vblk_comp *comp;
725 BUG_ON (!buffer || !vb);
727 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
728 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
729 r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
730 r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
731 r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
733 if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
734 r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
735 r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
744 len += VBLK_SIZE_CMP3;
745 if (len != get_unaligned_be32(buffer + 0x14))
748 comp = &vb->vblk.comp;
749 ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
750 sizeof (comp->state));
751 comp->type = buffer[0x18 + r_vstate];
752 comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
753 comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
754 comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
760 * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
761 * @buffer: Block of data being worked on
762 * @buflen: Size of the block of data
763 * @vb: In-memory vblk in which to return information
765 * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
767 * Return: 'true' @vb contains a Disk Group VBLK
768 * 'false' @vb contents are not defined
770 static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
772 int r_objid, r_name, r_diskid, r_id1, r_id2, len;
773 struct vblk_dgrp *dgrp;
775 BUG_ON (!buffer || !vb);
777 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
778 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
779 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
781 if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
782 r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
783 r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
790 len += VBLK_SIZE_DGR3;
791 if (len != get_unaligned_be32(buffer + 0x14))
794 dgrp = &vb->vblk.dgrp;
795 ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
796 sizeof (dgrp->disk_id));
801 * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
802 * @buffer: Block of data being worked on
803 * @buflen: Size of the block of data
804 * @vb: In-memory vblk in which to return information
806 * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
808 * Return: 'true' @vb contains a Disk Group VBLK
809 * 'false' @vb contents are not defined
811 static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
814 int r_objid, r_name, r_id1, r_id2, len;
816 BUG_ON (!buffer || !vb);
818 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
819 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
821 if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
822 r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
823 r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
830 len += VBLK_SIZE_DGR4;
831 if (len != get_unaligned_be32(buffer + 0x14))
834 ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
839 * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
840 * @buffer: Block of data being worked on
841 * @buflen: Size of the block of data
842 * @vb: In-memory vblk in which to return information
844 * Read a raw VBLK Disk object (version 3) into a vblk structure.
846 * Return: 'true' @vb contains a Disk VBLK
847 * 'false' @vb contents are not defined
849 static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
851 int r_objid, r_name, r_diskid, r_altname, len;
852 struct vblk_disk *disk;
854 BUG_ON (!buffer || !vb);
856 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
857 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
858 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
859 r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
864 len += VBLK_SIZE_DSK3;
865 if (len != get_unaligned_be32(buffer + 0x14))
868 disk = &vb->vblk.disk;
869 ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
870 sizeof (disk->alt_name));
871 if (uuid_parse(buffer + 0x19 + r_name, &disk->disk_id))
878 * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
879 * @buffer: Block of data being worked on
880 * @buflen: Size of the block of data
881 * @vb: In-memory vblk in which to return information
883 * Read a raw VBLK Disk object (version 4) into a vblk structure.
885 * Return: 'true' @vb contains a Disk VBLK
886 * 'false' @vb contents are not defined
888 static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
890 int r_objid, r_name, len;
891 struct vblk_disk *disk;
893 BUG_ON (!buffer || !vb);
895 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
896 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
901 len += VBLK_SIZE_DSK4;
902 if (len != get_unaligned_be32(buffer + 0x14))
905 disk = &vb->vblk.disk;
906 import_uuid(&disk->disk_id, buffer + 0x18 + r_name);
911 * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
912 * @buffer: Block of data being worked on
913 * @buflen: Size of the block of data
914 * @vb: In-memory vblk in which to return information
916 * Read a raw VBLK Partition object (version 3) into a vblk structure.
918 * Return: 'true' @vb contains a Partition VBLK
919 * 'false' @vb contents are not defined
921 static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
923 int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
924 struct vblk_part *part;
926 BUG_ON(!buffer || !vb);
927 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
929 ldm_error("r_objid %d < 0", r_objid);
932 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
934 ldm_error("r_name %d < 0", r_name);
937 r_size = ldm_relative(buffer, buflen, 0x34, r_name);
939 ldm_error("r_size %d < 0", r_size);
942 r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
944 ldm_error("r_parent %d < 0", r_parent);
947 r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
949 ldm_error("r_diskid %d < 0", r_diskid);
952 if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
953 r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
955 ldm_error("r_index %d < 0", r_index);
962 ldm_error("len %d < 0", len);
965 len += VBLK_SIZE_PRT3;
966 if (len > get_unaligned_be32(buffer + 0x14)) {
967 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
968 get_unaligned_be32(buffer + 0x14));
971 part = &vb->vblk.part;
972 part->start = get_unaligned_be64(buffer + 0x24 + r_name);
973 part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
974 part->size = ldm_get_vnum(buffer + 0x34 + r_name);
975 part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
976 part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
977 if (vb->flags & VBLK_FLAG_PART_INDEX)
978 part->partnum = buffer[0x35 + r_diskid];
985 * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
986 * @buffer: Block of data being worked on
987 * @buflen: Size of the block of data
988 * @vb: In-memory vblk in which to return information
990 * Read a raw VBLK Volume object (version 5) into a vblk structure.
992 * Return: 'true' @vb contains a Volume VBLK
993 * 'false' @vb contents are not defined
995 static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
997 int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
998 int r_id1, r_id2, r_size2, r_drive, len;
999 struct vblk_volu *volu;
1001 BUG_ON(!buffer || !vb);
1002 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
1004 ldm_error("r_objid %d < 0", r_objid);
1007 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
1009 ldm_error("r_name %d < 0", r_name);
1012 r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
1014 ldm_error("r_vtype %d < 0", r_vtype);
1017 r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
1018 if (r_disable_drive_letter < 0) {
1019 ldm_error("r_disable_drive_letter %d < 0",
1020 r_disable_drive_letter);
1023 r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
1025 ldm_error("r_child %d < 0", r_child);
1028 r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
1030 ldm_error("r_size %d < 0", r_size);
1033 if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
1034 r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
1036 ldm_error("r_id1 %d < 0", r_id1);
1041 if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
1042 r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
1044 ldm_error("r_id2 %d < 0", r_id2);
1049 if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
1050 r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
1052 ldm_error("r_size2 %d < 0", r_size2);
1057 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1058 r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
1060 ldm_error("r_drive %d < 0", r_drive);
1067 ldm_error("len %d < 0", len);
1070 len += VBLK_SIZE_VOL5;
1071 if (len > get_unaligned_be32(buffer + 0x14)) {
1072 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
1073 get_unaligned_be32(buffer + 0x14));
1076 volu = &vb->vblk.volu;
1077 ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
1078 sizeof(volu->volume_type));
1079 memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
1080 sizeof(volu->volume_state));
1081 volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
1082 volu->partition_type = buffer[0x41 + r_size];
1083 memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
1084 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1085 ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
1086 sizeof(volu->drive_hint));
1092 * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
1093 * @buf: Block of data being worked on
1094 * @len: Size of the block of data
1095 * @vb: In-memory vblk in which to return information
1097 * Read a raw VBLK object into a vblk structure. This function just reads the
1098 * information common to all VBLK types, then delegates the rest of the work to
1099 * helper functions: ldm_parse_*.
1101 * Return: 'true' @vb contains a VBLK
1102 * 'false' @vb contents are not defined
1104 static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
1106 bool result = false;
1109 BUG_ON (!buf || !vb);
1111 r_objid = ldm_relative (buf, len, 0x18, 0);
1113 ldm_error ("VBLK header is corrupt.");
1117 vb->flags = buf[0x12];
1118 vb->type = buf[0x13];
1119 vb->obj_id = ldm_get_vnum (buf + 0x18);
1120 ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
1123 case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
1124 case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
1125 case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
1126 case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
1127 case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
1128 case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
1129 case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
1133 ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
1134 (unsigned long long) vb->obj_id, vb->type);
1136 ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
1137 (unsigned long long) vb->obj_id, vb->type);
1144 * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
1145 * @data: Raw VBLK to add to the database
1146 * @len: Size of the raw VBLK
1147 * @ldb: Cache of the database structures
1149 * The VBLKs are sorted into categories. Partitions are also sorted by offset.
1151 * N.B. This function does not check the validity of the VBLKs.
1153 * Return: 'true' The VBLK was added
1154 * 'false' An error occurred
1156 static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
1159 struct list_head *item;
1161 BUG_ON (!data || !ldb);
1163 vb = kmalloc (sizeof (*vb), GFP_KERNEL);
1165 ldm_crit ("Out of memory.");
1169 if (!ldm_parse_vblk (data, len, vb)) {
1171 return false; /* Already logged */
1174 /* Put vblk into the correct list. */
1178 list_add (&vb->list, &ldb->v_dgrp);
1182 list_add (&vb->list, &ldb->v_disk);
1185 list_add (&vb->list, &ldb->v_volu);
1188 list_add (&vb->list, &ldb->v_comp);
1191 /* Sort by the partition's start sector. */
1192 list_for_each (item, &ldb->v_part) {
1193 struct vblk *v = list_entry (item, struct vblk, list);
1194 if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
1195 (v->vblk.part.start > vb->vblk.part.start)) {
1196 list_add_tail (&vb->list, &v->list);
1200 list_add_tail (&vb->list, &ldb->v_part);
1207 * ldm_frag_add - Add a VBLK fragment to a list
1208 * @data: Raw fragment to be added to the list
1209 * @size: Size of the raw fragment
1210 * @frags: Linked list of VBLK fragments
1212 * Fragmented VBLKs may not be consecutive in the database, so they are placed
1213 * in a list so they can be pieced together later.
1215 * Return: 'true' Success, the VBLK was added to the list
1216 * 'false' Error, a problem occurred
1218 static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
1221 struct list_head *item;
1222 int rec, num, group;
1224 BUG_ON (!data || !frags);
1226 if (size < 2 * VBLK_SIZE_HEAD) {
1227 ldm_error("Value of size is too small.");
1231 group = get_unaligned_be32(data + 0x08);
1232 rec = get_unaligned_be16(data + 0x0C);
1233 num = get_unaligned_be16(data + 0x0E);
1234 if ((num < 1) || (num > 4)) {
1235 ldm_error ("A VBLK claims to have %d parts.", num);
1239 ldm_error("REC value (%d) exceeds NUM value (%d)", rec, num);
1243 list_for_each (item, frags) {
1244 f = list_entry (item, struct frag, list);
1245 if (f->group == group)
1249 f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
1251 ldm_crit ("Out of memory.");
1258 f->map = 0xFF << num;
1260 list_add_tail (&f->list, frags);
1262 if (rec >= f->num) {
1263 ldm_error("REC value (%d) exceeds NUM value (%d)", rec, f->num);
1266 if (f->map & (1 << rec)) {
1267 ldm_error ("Duplicate VBLK, part %d.", rec);
1268 f->map &= 0x7F; /* Mark the group as broken */
1271 f->map |= (1 << rec);
1273 memcpy(f->data, data, VBLK_SIZE_HEAD);
1274 data += VBLK_SIZE_HEAD;
1275 size -= VBLK_SIZE_HEAD;
1276 memcpy(f->data + VBLK_SIZE_HEAD + rec * size, data, size);
1281 * ldm_frag_free - Free a linked list of VBLK fragments
1282 * @list: Linked list of fragments
1284 * Free a linked list of VBLK fragments
1288 static void ldm_frag_free (struct list_head *list)
1290 struct list_head *item, *tmp;
1294 list_for_each_safe (item, tmp, list)
1295 kfree (list_entry (item, struct frag, list));
1299 * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
1300 * @frags: Linked list of VBLK fragments
1301 * @ldb: Cache of the database structures
1303 * Now that all the fragmented VBLKs have been collected, they must be added to
1304 * the database for later use.
1306 * Return: 'true' All the fragments we added successfully
1307 * 'false' One or more of the fragments we invalid
1309 static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
1312 struct list_head *item;
1314 BUG_ON (!frags || !ldb);
1316 list_for_each (item, frags) {
1317 f = list_entry (item, struct frag, list);
1319 if (f->map != 0xFF) {
1320 ldm_error ("VBLK group %d is incomplete (0x%02x).",
1325 if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
1326 return false; /* Already logged */
1332 * ldm_get_vblks - Read the on-disk database of VBLKs into memory
1333 * @state: Partition check state including device holding the LDM Database
1334 * @base: Offset, into @state->disk, of the database
1335 * @ldb: Cache of the database structures
1337 * To use the information from the VBLKs, they need to be read from the disk,
1338 * unpacked and validated. We cache them in @ldb according to their type.
1340 * Return: 'true' All the VBLKs were read successfully
1341 * 'false' An error occurred
1343 static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
1346 int size, perbuf, skip, finish, s, v, recs;
1349 bool result = false;
1352 BUG_ON(!state || !ldb);
1354 size = ldb->vm.vblk_size;
1355 perbuf = 512 / size;
1356 skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
1357 finish = (size * ldb->vm.last_vblk_seq) >> 9;
1359 for (s = skip; s < finish; s++) { /* For each sector */
1360 data = read_part_sector(state, base + OFF_VMDB + s, §);
1362 ldm_crit ("Disk read failed.");
1366 for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
1367 if (MAGIC_VBLK != get_unaligned_be32(data)) {
1368 ldm_error ("Expected to find a VBLK.");
1372 recs = get_unaligned_be16(data + 0x0E); /* Number of records */
1374 if (!ldm_ldmdb_add (data, size, ldb))
1375 goto out; /* Already logged */
1376 } else if (recs > 1) {
1377 if (!ldm_frag_add (data, size, &frags))
1378 goto out; /* Already logged */
1380 /* else Record is not in use, ignore it. */
1382 put_dev_sector (sect);
1386 result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
1389 put_dev_sector (sect);
1390 ldm_frag_free (&frags);
1396 * ldm_free_vblks - Free a linked list of vblk's
1397 * @lh: Head of a linked list of struct vblk
1399 * Free a list of vblk's and free the memory used to maintain the list.
1403 static void ldm_free_vblks (struct list_head *lh)
1405 struct list_head *item, *tmp;
1409 list_for_each_safe (item, tmp, lh)
1410 kfree (list_entry (item, struct vblk, list));
1415 * ldm_partition - Find out whether a device is a dynamic disk and handle it
1416 * @state: Partition check state including device holding the LDM Database
1418 * This determines whether the device @bdev is a dynamic disk and if so creates
1419 * the partitions necessary in the gendisk structure pointed to by @hd.
1421 * We create a dummy device 1, which contains the LDM database, and then create
1422 * each partition described by the LDM database in sequence as devices 2+. For
1423 * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
1424 * and so on: the actual data containing partitions.
1426 * Return: 1 Success, @state->disk is a dynamic disk and we handled it
1427 * 0 Success, @state->disk is not a dynamic disk
1428 * -1 An error occurred before enough information had been read
1429 * Or @state->disk is a dynamic disk, but it may be corrupted
1431 int ldm_partition(struct parsed_partitions *state)
1439 /* Look for signs of a Dynamic Disk */
1440 if (!ldm_validate_partition_table(state))
1443 ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
1445 ldm_crit ("Out of memory.");
1449 /* Parse and check privheads. */
1450 if (!ldm_validate_privheads(state, &ldb->ph))
1451 goto out; /* Already logged */
1453 /* All further references are relative to base (database start). */
1454 base = ldb->ph.config_start;
1456 /* Parse and check tocs and vmdb. */
1457 if (!ldm_validate_tocblocks(state, base, ldb) ||
1458 !ldm_validate_vmdb(state, base, ldb))
1459 goto out; /* Already logged */
1461 /* Initialize vblk lists in ldmdb struct */
1462 INIT_LIST_HEAD (&ldb->v_dgrp);
1463 INIT_LIST_HEAD (&ldb->v_disk);
1464 INIT_LIST_HEAD (&ldb->v_volu);
1465 INIT_LIST_HEAD (&ldb->v_comp);
1466 INIT_LIST_HEAD (&ldb->v_part);
1468 if (!ldm_get_vblks(state, base, ldb)) {
1469 ldm_crit ("Failed to read the VBLKs from the database.");
1473 /* Finally, create the data partition devices. */
1474 if (ldm_create_data_partitions(state, ldb)) {
1475 ldm_debug ("Parsed LDM database successfully.");
1478 /* else Already logged */
1481 ldm_free_vblks (&ldb->v_dgrp);
1482 ldm_free_vblks (&ldb->v_disk);
1483 ldm_free_vblks (&ldb->v_volu);
1484 ldm_free_vblks (&ldb->v_comp);
1485 ldm_free_vblks (&ldb->v_part);