2 * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
4 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
5 * Copyright (c) 2001-2004 Anton Altaparmakov
6 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
8 * Documentation is available at http://linux-ntfs.sf.net/ldm
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the License, or (at your option) any later
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License along with
21 * this program (in the main directory of the source in the file COPYING); if
22 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23 * Boston, MA 02111-1307 USA
26 #include <linux/slab.h>
27 #include <linux/pagemap.h>
28 #include <linux/stringify.h>
34 * ldm_debug/info/error/crit - Output an error message
35 * @f: A printf format string containing the message
36 * @...: Variables to substitute into @f
38 * ldm_debug() writes a DEBUG level message to the syslog but only if the
39 * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
41 #ifndef CONFIG_LDM_DEBUG
42 #define ldm_debug(...) do {} while (0)
44 #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __FUNCTION__, f, ##a)
47 #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __FUNCTION__, f, ##a)
48 #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __FUNCTION__, f, ##a)
49 #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __FUNCTION__, f, ##a)
51 __attribute__ ((format (printf
, 3, 4)))
52 static void _ldm_printk (const char *level
, const char *function
,
59 vsnprintf (buf
, sizeof (buf
), fmt
, args
);
62 printk ("%s%s(): %s\n", level
, function
, buf
);
67 * ldm_parse_hexbyte - Convert a ASCII hex number to a byte
68 * @src: Pointer to at least 2 characters to convert.
70 * Convert a two character ASCII hex string to a number.
72 * Return: 0-255 Success, the byte was parsed correctly
73 * -1 Error, an invalid character was supplied
75 static int ldm_parse_hexbyte (const u8
*src
)
77 unsigned int x
; /* For correct wrapping */
81 if ((x
= src
[0] - '0') <= '9'-'0') h
= x
;
82 else if ((x
= src
[0] - 'a') <= 'f'-'a') h
= x
+10;
83 else if ((x
= src
[0] - 'A') <= 'F'-'A') h
= x
+10;
88 if ((x
= src
[1] - '0') <= '9'-'0') return h
| x
;
89 if ((x
= src
[1] - 'a') <= 'f'-'a') return h
| (x
+10);
90 if ((x
= src
[1] - 'A') <= 'F'-'A') return h
| (x
+10);
95 * ldm_parse_guid - Convert GUID from ASCII to binary
96 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
97 * @dest: Memory block to hold binary GUID (16 bytes)
99 * N.B. The GUID need not be NULL terminated.
101 * Return: 'true' @dest contains binary GUID
102 * 'false' @dest contents are undefined
104 #if 1 // add by Victor Yu. 03-07-2007
107 static bool ldm_parse_guid (const u8
*src
, u8
*dest
)
109 static const int size
[] = { 4, 2, 2, 2, 6 };
112 if (src
[8] != '-' || src
[13] != '-' ||
113 src
[18] != '-' || src
[23] != '-')
116 for (j
= 0; j
< 5; j
++, src
++)
117 for (i
= 0; i
< size
[j
]; i
++, src
+=2, *dest
++ = v
)
118 if ((v
= ldm_parse_hexbyte (src
)) < 0)
126 * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
127 * @data: Raw database PRIVHEAD structure loaded from the device
128 * @ph: In-memory privhead structure in which to return parsed information
130 * This parses the LDM database PRIVHEAD structure supplied in @data and
131 * sets up the in-memory privhead structure @ph with the obtained information.
133 * Return: 'true' @ph contains the PRIVHEAD data
134 * 'false' @ph contents are undefined
136 static bool ldm_parse_privhead (const u8
*data
, struct privhead
*ph
)
138 BUG_ON (!data
|| !ph
);
140 if (MAGIC_PRIVHEAD
!= BE64 (data
)) {
141 ldm_error ("Cannot find PRIVHEAD structure. LDM database is"
142 " corrupt. Aborting.");
146 ph
->ver_major
= BE16 (data
+ 0x000C);
147 ph
->ver_minor
= BE16 (data
+ 0x000E);
148 ph
->logical_disk_start
= BE64 (data
+ 0x011B);
149 ph
->logical_disk_size
= BE64 (data
+ 0x0123);
150 ph
->config_start
= BE64 (data
+ 0x012B);
151 ph
->config_size
= BE64 (data
+ 0x0133);
153 if ((ph
->ver_major
!= 2) || (ph
->ver_minor
!= 11)) {
154 ldm_error ("Expected PRIVHEAD version %d.%d, got %d.%d."
155 " Aborting.", 2, 11, ph
->ver_major
, ph
->ver_minor
);
158 if (ph
->config_size
!= LDM_DB_SIZE
) { /* 1 MiB in sectors. */
159 /* Warn the user and continue, carefully */
160 ldm_info ("Database is normally %u bytes, it claims to "
161 "be %llu bytes.", LDM_DB_SIZE
,
162 (unsigned long long)ph
->config_size
);
164 if ((ph
->logical_disk_size
== 0) ||
165 (ph
->logical_disk_start
+ ph
->logical_disk_size
> ph
->config_start
)) {
166 ldm_error ("PRIVHEAD disk size doesn't match real disk size");
170 if (!ldm_parse_guid (data
+ 0x0030, ph
->disk_id
)) {
171 ldm_error ("PRIVHEAD contains an invalid GUID.");
175 ldm_debug ("Parsed PRIVHEAD successfully.");
180 * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
181 * @data: Raw database TOCBLOCK structure loaded from the device
182 * @toc: In-memory toc structure in which to return parsed information
184 * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
185 * in @data and sets up the in-memory tocblock structure @toc with the obtained
188 * N.B. The *_start and *_size values returned in @toc are not range-checked.
190 * Return: 'true' @toc contains the TOCBLOCK data
191 * 'false' @toc contents are undefined
193 static bool ldm_parse_tocblock (const u8
*data
, struct tocblock
*toc
)
195 BUG_ON (!data
|| !toc
);
197 if (MAGIC_TOCBLOCK
!= BE64 (data
)) {
198 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
201 strncpy (toc
->bitmap1_name
, data
+ 0x24, sizeof (toc
->bitmap1_name
));
202 toc
->bitmap1_name
[sizeof (toc
->bitmap1_name
) - 1] = 0;
203 toc
->bitmap1_start
= BE64 (data
+ 0x2E);
204 toc
->bitmap1_size
= BE64 (data
+ 0x36);
206 if (strncmp (toc
->bitmap1_name
, TOC_BITMAP1
,
207 sizeof (toc
->bitmap1_name
)) != 0) {
208 ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
209 TOC_BITMAP1
, toc
->bitmap1_name
);
212 strncpy (toc
->bitmap2_name
, data
+ 0x46, sizeof (toc
->bitmap2_name
));
213 toc
->bitmap2_name
[sizeof (toc
->bitmap2_name
) - 1] = 0;
214 toc
->bitmap2_start
= BE64 (data
+ 0x50);
215 toc
->bitmap2_size
= BE64 (data
+ 0x58);
216 if (strncmp (toc
->bitmap2_name
, TOC_BITMAP2
,
217 sizeof (toc
->bitmap2_name
)) != 0) {
218 ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
219 TOC_BITMAP2
, toc
->bitmap2_name
);
222 ldm_debug ("Parsed TOCBLOCK successfully.");
227 * ldm_parse_vmdb - Read the LDM Database VMDB structure
228 * @data: Raw database VMDB structure loaded from the device
229 * @vm: In-memory vmdb structure in which to return parsed information
231 * This parses the LDM Database VMDB structure supplied in @data and sets up
232 * the in-memory vmdb structure @vm with the obtained information.
234 * N.B. The *_start, *_size and *_seq values will be range-checked later.
236 * Return: 'true' @vm contains VMDB info
237 * 'false' @vm contents are undefined
239 static bool ldm_parse_vmdb (const u8
*data
, struct vmdb
*vm
)
241 BUG_ON (!data
|| !vm
);
243 if (MAGIC_VMDB
!= BE32 (data
)) {
244 ldm_crit ("Cannot find the VMDB, database may be corrupt.");
248 vm
->ver_major
= BE16 (data
+ 0x12);
249 vm
->ver_minor
= BE16 (data
+ 0x14);
250 if ((vm
->ver_major
!= 4) || (vm
->ver_minor
!= 10)) {
251 ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
252 "Aborting.", 4, 10, vm
->ver_major
, vm
->ver_minor
);
256 vm
->vblk_size
= BE32 (data
+ 0x08);
257 vm
->vblk_offset
= BE32 (data
+ 0x0C);
258 vm
->last_vblk_seq
= BE32 (data
+ 0x04);
260 ldm_debug ("Parsed VMDB successfully.");
265 * ldm_compare_privheads - Compare two privhead objects
266 * @ph1: First privhead
267 * @ph2: Second privhead
269 * This compares the two privhead structures @ph1 and @ph2.
271 * Return: 'true' Identical
274 static bool ldm_compare_privheads (const struct privhead
*ph1
,
275 const struct privhead
*ph2
)
277 BUG_ON (!ph1
|| !ph2
);
279 return ((ph1
->ver_major
== ph2
->ver_major
) &&
280 (ph1
->ver_minor
== ph2
->ver_minor
) &&
281 (ph1
->logical_disk_start
== ph2
->logical_disk_start
) &&
282 (ph1
->logical_disk_size
== ph2
->logical_disk_size
) &&
283 (ph1
->config_start
== ph2
->config_start
) &&
284 (ph1
->config_size
== ph2
->config_size
) &&
285 !memcmp (ph1
->disk_id
, ph2
->disk_id
, GUID_SIZE
));
289 * ldm_compare_tocblocks - Compare two tocblock objects
293 * This compares the two tocblock structures @toc1 and @toc2.
295 * Return: 'true' Identical
298 static bool ldm_compare_tocblocks (const struct tocblock
*toc1
,
299 const struct tocblock
*toc2
)
301 BUG_ON (!toc1
|| !toc2
);
303 return ((toc1
->bitmap1_start
== toc2
->bitmap1_start
) &&
304 (toc1
->bitmap1_size
== toc2
->bitmap1_size
) &&
305 (toc1
->bitmap2_start
== toc2
->bitmap2_start
) &&
306 (toc1
->bitmap2_size
== toc2
->bitmap2_size
) &&
307 !strncmp (toc1
->bitmap1_name
, toc2
->bitmap1_name
,
308 sizeof (toc1
->bitmap1_name
)) &&
309 !strncmp (toc1
->bitmap2_name
, toc2
->bitmap2_name
,
310 sizeof (toc1
->bitmap2_name
)));
314 * ldm_validate_privheads - Compare the primary privhead with its backups
315 * @bdev: Device holding the LDM Database
316 * @ph1: Memory struct to fill with ph contents
318 * Read and compare all three privheads from disk.
320 * The privheads on disk show the size and location of the main disk area and
321 * the configuration area (the database). The values are range-checked against
322 * @hd, which contains the real size of the disk.
324 * Return: 'true' Success
327 static bool ldm_validate_privheads (struct block_device
*bdev
,
328 struct privhead
*ph1
)
330 static const int off
[3] = { OFF_PRIV1
, OFF_PRIV2
, OFF_PRIV3
};
331 struct privhead
*ph
[3] = { ph1
};
338 BUG_ON (!bdev
|| !ph1
);
340 ph
[1] = kmalloc (sizeof (*ph
[1]), GFP_KERNEL
);
341 ph
[2] = kmalloc (sizeof (*ph
[2]), GFP_KERNEL
);
342 if (!ph
[1] || !ph
[2]) {
343 ldm_crit ("Out of memory.");
347 /* off[1 & 2] are relative to ph[0]->config_start */
348 ph
[0]->config_start
= 0;
350 /* Read and parse privheads */
351 for (i
= 0; i
< 3; i
++) {
352 data
= read_dev_sector (bdev
,
353 ph
[0]->config_start
+ off
[i
], §
);
355 ldm_crit ("Disk read failed.");
358 result
= ldm_parse_privhead (data
, ph
[i
]);
359 put_dev_sector (sect
);
361 ldm_error ("Cannot find PRIVHEAD %d.", i
+1); /* Log again */
363 goto out
; /* Already logged */
365 break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
369 num_sects
= bdev
->bd_inode
->i_size
>> 9;
371 if ((ph
[0]->config_start
> num_sects
) ||
372 ((ph
[0]->config_start
+ ph
[0]->config_size
) > num_sects
)) {
373 ldm_crit ("Database extends beyond the end of the disk.");
377 if ((ph
[0]->logical_disk_start
> ph
[0]->config_start
) ||
378 ((ph
[0]->logical_disk_start
+ ph
[0]->logical_disk_size
)
379 > ph
[0]->config_start
)) {
380 ldm_crit ("Disk and database overlap.");
384 if (!ldm_compare_privheads (ph
[0], ph
[1])) {
385 ldm_crit ("Primary and backup PRIVHEADs don't match.");
388 /* FIXME ignore this for now
389 if (!ldm_compare_privheads (ph[0], ph[2])) {
390 ldm_crit ("Primary and backup PRIVHEADs don't match.");
393 ldm_debug ("Validated PRIVHEADs successfully.");
402 * ldm_validate_tocblocks - Validate the table of contents and its backups
403 * @bdev: Device holding the LDM Database
404 * @base: Offset, into @bdev, of the database
405 * @ldb: Cache of the database structures
407 * Find and compare the four tables of contents of the LDM Database stored on
408 * @bdev and return the parsed information into @toc1.
410 * The offsets and sizes of the configs are range-checked against a privhead.
412 * Return: 'true' @toc1 contains validated TOCBLOCK info
413 * 'false' @toc1 contents are undefined
415 static bool ldm_validate_tocblocks (struct block_device
*bdev
,
416 unsigned long base
, struct ldmdb
*ldb
)
418 static const int off
[4] = { OFF_TOCB1
, OFF_TOCB2
, OFF_TOCB3
, OFF_TOCB4
};
419 struct tocblock
*tb
[4];
426 BUG_ON (!bdev
|| !ldb
);
430 tb
[1] = kmalloc (sizeof (*tb
[1]), GFP_KERNEL
);
431 tb
[2] = kmalloc (sizeof (*tb
[2]), GFP_KERNEL
);
432 tb
[3] = kmalloc (sizeof (*tb
[3]), GFP_KERNEL
);
433 if (!tb
[1] || !tb
[2] || !tb
[3]) {
434 ldm_crit ("Out of memory.");
438 for (i
= 0; i
< 4; i
++) /* Read and parse all four toc's. */
440 data
= read_dev_sector (bdev
, base
+ off
[i
], §
);
442 ldm_crit ("Disk read failed.");
445 result
= ldm_parse_tocblock (data
, tb
[i
]);
446 put_dev_sector (sect
);
448 goto out
; /* Already logged */
451 /* Range check the toc against a privhead. */
452 if (((tb
[0]->bitmap1_start
+ tb
[0]->bitmap1_size
) > ph
->config_size
) ||
453 ((tb
[0]->bitmap2_start
+ tb
[0]->bitmap2_size
) > ph
->config_size
)) {
454 ldm_crit ("The bitmaps are out of range. Giving up.");
458 if (!ldm_compare_tocblocks (tb
[0], tb
[1]) || /* Compare all tocs. */
459 !ldm_compare_tocblocks (tb
[0], tb
[2]) ||
460 !ldm_compare_tocblocks (tb
[0], tb
[3])) {
461 ldm_crit ("The TOCBLOCKs don't match.");
465 ldm_debug ("Validated TOCBLOCKs successfully.");
475 * ldm_validate_vmdb - Read the VMDB and validate it
476 * @bdev: Device holding the LDM Database
477 * @base: Offset, into @bdev, of the database
478 * @ldb: Cache of the database structures
480 * Find the vmdb of the LDM Database stored on @bdev and return the parsed
481 * information in @ldb.
483 * Return: 'true' @ldb contains validated VBDB info
484 * 'false' @ldb contents are undefined
486 static bool ldm_validate_vmdb (struct block_device
*bdev
, unsigned long base
,
493 struct tocblock
*toc
;
495 BUG_ON (!bdev
|| !ldb
);
500 data
= read_dev_sector (bdev
, base
+ OFF_VMDB
, §
);
502 ldm_crit ("Disk read failed.");
506 if (!ldm_parse_vmdb (data
, vm
))
507 goto out
; /* Already logged */
509 /* Are there uncommitted transactions? */
510 if (BE16(data
+ 0x10) != 0x01) {
511 ldm_crit ("Database is not in a consistent state. Aborting.");
515 if (vm
->vblk_offset
!= 512)
516 ldm_info ("VBLKs start at offset 0x%04x.", vm
->vblk_offset
);
519 * The last_vblkd_seq can be before the end of the vmdb, just make sure
520 * it is not out of bounds.
522 if ((vm
->vblk_size
* vm
->last_vblk_seq
) > (toc
->bitmap1_size
<< 9)) {
523 ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
524 "Database is corrupt. Aborting.");
530 put_dev_sector (sect
);
536 * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
537 * @bdev: Device holding the LDM Database
539 * This function provides a weak test to decide whether the device is a dynamic
540 * disk or not. It looks for an MS-DOS-style partition table containing at
541 * least one partition of type 0x42 (formerly SFS, now used by Windows for
544 * N.B. The only possible error can come from the read_dev_sector and that is
545 * only likely to happen if the underlying device is strange. If that IS
546 * the case we should return zero to let someone else try.
548 * Return: 'true' @bdev is a dynamic disk
549 * 'false' @bdev is not a dynamic disk, or an error occurred
551 static bool ldm_validate_partition_table (struct block_device
*bdev
)
561 data
= read_dev_sector (bdev
, 0, §
);
563 ldm_crit ("Disk read failed.");
567 if (*(__le16
*) (data
+ 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC
))
570 p
= (struct partition
*)(data
+ 0x01BE);
571 for (i
= 0; i
< 4; i
++, p
++)
572 if (SYS_IND (p
) == WIN2K_DYNAMIC_PARTITION
) {
578 ldm_debug ("Found W2K dynamic disk partition type.");
581 put_dev_sector (sect
);
586 * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
587 * @ldb: Cache of the database structures
589 * The LDM Database contains a list of all partitions on all dynamic disks.
590 * The primary PRIVHEAD, at the beginning of the physical disk, tells us
591 * the GUID of this disk. This function searches for the GUID in a linked
594 * Return: Pointer, A matching vblk was found
595 * NULL, No match, or an error
597 static struct vblk
* ldm_get_disk_objid (const struct ldmdb
*ldb
)
599 struct list_head
*item
;
603 list_for_each (item
, &ldb
->v_disk
) {
604 struct vblk
*v
= list_entry (item
, struct vblk
, list
);
605 if (!memcmp (v
->vblk
.disk
.disk_id
, ldb
->ph
.disk_id
, GUID_SIZE
))
613 * ldm_create_data_partitions - Create data partitions for this device
614 * @pp: List of the partitions parsed so far
615 * @ldb: Cache of the database structures
617 * The database contains ALL the partitions for ALL disk groups, so we need to
618 * filter out this specific disk. Using the disk's object id, we can find all
619 * the partitions in the database that belong to this disk.
621 * Add each partition in our database, to the parsed_partitions structure.
623 * N.B. This function creates the partitions in the order it finds partition
624 * objects in the linked list.
626 * Return: 'true' Partition created
627 * 'false' Error, probably a range checking problem
629 static bool ldm_create_data_partitions (struct parsed_partitions
*pp
,
630 const struct ldmdb
*ldb
)
632 struct list_head
*item
;
635 struct vblk_part
*part
;
638 BUG_ON (!pp
|| !ldb
);
640 disk
= ldm_get_disk_objid (ldb
);
642 ldm_crit ("Can't find the ID of this disk in the database.");
648 /* Create the data partitions */
649 list_for_each (item
, &ldb
->v_part
) {
650 vb
= list_entry (item
, struct vblk
, list
);
651 part
= &vb
->vblk
.part
;
653 if (part
->disk_id
!= disk
->obj_id
)
656 put_partition (pp
, part_num
, ldb
->ph
.logical_disk_start
+
657 part
->start
, part
->size
);
667 * ldm_relative - Calculate the next relative offset
668 * @buffer: Block of data being worked on
669 * @buflen: Size of the block of data
670 * @base: Size of the previous fixed width fields
671 * @offset: Cumulative size of the previous variable-width fields
673 * Because many of the VBLK fields are variable-width, it's necessary
674 * to calculate each offset based on the previous one and the length
675 * of the field it pointed to.
677 * Return: -1 Error, the calculated offset exceeded the size of the buffer
678 * n OK, a range-checked offset into buffer
680 static int ldm_relative (const u8
*buffer
, int buflen
, int base
, int offset
)
684 if ((!buffer
) || (offset
< 0) || (base
> buflen
))
686 if ((base
+ buffer
[base
]) >= buflen
)
689 return buffer
[base
] + offset
+ 1;
693 * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
694 * @block: Pointer to the variable-width number to convert
696 * Large numbers in the LDM Database are often stored in a packed format. Each
697 * number is prefixed by a one byte width marker. All numbers in the database
698 * are stored in big-endian byte order. This function reads one of these
699 * numbers and returns the result
701 * N.B. This function DOES NOT perform any range checking, though the most
702 * it will read is eight bytes.
705 * 0 Zero, or an error occurred
707 static u64
ldm_get_vnum (const u8
*block
)
716 if (length
&& length
<= 8)
718 tmp
= (tmp
<< 8) | *block
++;
720 ldm_error ("Illegal length %d.", length
);
726 * ldm_get_vstr - Read a length-prefixed string into a buffer
727 * @block: Pointer to the length marker
728 * @buffer: Location to copy string to
729 * @buflen: Size of the output buffer
731 * Many of the strings in the LDM Database are not NULL terminated. Instead
732 * they are prefixed by a one byte length marker. This function copies one of
733 * these strings into a buffer.
735 * N.B. This function DOES NOT perform any range checking on the input.
736 * If the buffer is too small, the output will be truncated.
738 * Return: 0, Error and @buffer contents are undefined
739 * n, String length in characters (excluding NULL)
740 * buflen-1, String was truncated.
742 static int ldm_get_vstr (const u8
*block
, u8
*buffer
, int buflen
)
746 BUG_ON (!block
|| !buffer
);
749 if (length
>= buflen
) {
750 ldm_error ("Truncating string %d -> %d.", length
, buflen
);
753 memcpy (buffer
, block
+ 1, length
);
760 * ldm_parse_cmp3 - Read a raw VBLK Component 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 Component object (version 3) into a vblk structure.
767 * Return: 'true' @vb contains a Component VBLK
768 * 'false' @vb contents are not defined
770 static bool ldm_parse_cmp3 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
772 int r_objid
, r_name
, r_vstate
, r_child
, r_parent
, r_stripe
, r_cols
, len
;
773 struct vblk_comp
*comp
;
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_vstate
= ldm_relative (buffer
, buflen
, 0x18, r_name
);
780 r_child
= ldm_relative (buffer
, buflen
, 0x1D, r_vstate
);
781 r_parent
= ldm_relative (buffer
, buflen
, 0x2D, r_child
);
783 if (buffer
[0x12] & VBLK_FLAG_COMP_STRIPE
) {
784 r_stripe
= ldm_relative (buffer
, buflen
, 0x2E, r_parent
);
785 r_cols
= ldm_relative (buffer
, buflen
, 0x2E, r_stripe
);
795 len
+= VBLK_SIZE_CMP3
;
796 if (len
!= BE32 (buffer
+ 0x14))
799 comp
= &vb
->vblk
.comp
;
800 ldm_get_vstr (buffer
+ 0x18 + r_name
, comp
->state
,
801 sizeof (comp
->state
));
802 comp
->type
= buffer
[0x18 + r_vstate
];
803 comp
->children
= ldm_get_vnum (buffer
+ 0x1D + r_vstate
);
804 comp
->parent_id
= ldm_get_vnum (buffer
+ 0x2D + r_child
);
805 comp
->chunksize
= r_stripe
? ldm_get_vnum (buffer
+r_parent
+0x2E) : 0;
811 * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
812 * @buffer: Block of data being worked on
813 * @buflen: Size of the block of data
814 * @vb: In-memory vblk in which to return information
816 * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
818 * Return: 'true' @vb contains a Disk Group VBLK
819 * 'false' @vb contents are not defined
821 static int ldm_parse_dgr3 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
823 int r_objid
, r_name
, r_diskid
, r_id1
, r_id2
, len
;
824 struct vblk_dgrp
*dgrp
;
826 BUG_ON (!buffer
|| !vb
);
828 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
829 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
830 r_diskid
= ldm_relative (buffer
, buflen
, 0x18, r_name
);
832 if (buffer
[0x12] & VBLK_FLAG_DGR3_IDS
) {
833 r_id1
= ldm_relative (buffer
, buflen
, 0x24, r_diskid
);
834 r_id2
= ldm_relative (buffer
, buflen
, 0x24, r_id1
);
844 len
+= VBLK_SIZE_DGR3
;
845 if (len
!= BE32 (buffer
+ 0x14))
848 dgrp
= &vb
->vblk
.dgrp
;
849 ldm_get_vstr (buffer
+ 0x18 + r_name
, dgrp
->disk_id
,
850 sizeof (dgrp
->disk_id
));
855 * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
856 * @buffer: Block of data being worked on
857 * @buflen: Size of the block of data
858 * @vb: In-memory vblk in which to return information
860 * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
862 * Return: 'true' @vb contains a Disk Group VBLK
863 * 'false' @vb contents are not defined
865 static bool ldm_parse_dgr4 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
868 int r_objid
, r_name
, r_id1
, r_id2
, len
;
869 struct vblk_dgrp
*dgrp
;
871 BUG_ON (!buffer
|| !vb
);
873 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
874 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
876 if (buffer
[0x12] & VBLK_FLAG_DGR4_IDS
) {
877 r_id1
= ldm_relative (buffer
, buflen
, 0x44, r_name
);
878 r_id2
= ldm_relative (buffer
, buflen
, 0x44, r_id1
);
888 len
+= VBLK_SIZE_DGR4
;
889 if (len
!= BE32 (buffer
+ 0x14))
892 dgrp
= &vb
->vblk
.dgrp
;
894 ldm_get_vstr (buffer
+ 0x18 + r_objid
, buf
, sizeof (buf
));
899 * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
900 * @buffer: Block of data being worked on
901 * @buflen: Size of the block of data
902 * @vb: In-memory vblk in which to return information
904 * Read a raw VBLK Disk object (version 3) into a vblk structure.
906 * Return: 'true' @vb contains a Disk VBLK
907 * 'false' @vb contents are not defined
909 static bool ldm_parse_dsk3 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
911 int r_objid
, r_name
, r_diskid
, r_altname
, len
;
912 struct vblk_disk
*disk
;
914 BUG_ON (!buffer
|| !vb
);
916 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
917 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
918 r_diskid
= ldm_relative (buffer
, buflen
, 0x18, r_name
);
919 r_altname
= ldm_relative (buffer
, buflen
, 0x18, r_diskid
);
924 len
+= VBLK_SIZE_DSK3
;
925 if (len
!= BE32 (buffer
+ 0x14))
928 disk
= &vb
->vblk
.disk
;
929 ldm_get_vstr (buffer
+ 0x18 + r_diskid
, disk
->alt_name
,
930 sizeof (disk
->alt_name
));
931 if (!ldm_parse_guid (buffer
+ 0x19 + r_name
, disk
->disk_id
))
938 * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
939 * @buffer: Block of data being worked on
940 * @buflen: Size of the block of data
941 * @vb: In-memory vblk in which to return information
943 * Read a raw VBLK Disk object (version 4) into a vblk structure.
945 * Return: 'true' @vb contains a Disk VBLK
946 * 'false' @vb contents are not defined
948 static bool ldm_parse_dsk4 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
950 int r_objid
, r_name
, len
;
951 struct vblk_disk
*disk
;
953 BUG_ON (!buffer
|| !vb
);
955 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
956 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
961 len
+= VBLK_SIZE_DSK4
;
962 if (len
!= BE32 (buffer
+ 0x14))
965 disk
= &vb
->vblk
.disk
;
966 memcpy (disk
->disk_id
, buffer
+ 0x18 + r_name
, GUID_SIZE
);
971 * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
972 * @buffer: Block of data being worked on
973 * @buflen: Size of the block of data
974 * @vb: In-memory vblk in which to return information
976 * Read a raw VBLK Partition object (version 3) into a vblk structure.
978 * Return: 'true' @vb contains a Partition VBLK
979 * 'false' @vb contents are not defined
981 static bool ldm_parse_prt3 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
983 int r_objid
, r_name
, r_size
, r_parent
, r_diskid
, r_index
, len
;
984 struct vblk_part
*part
;
986 BUG_ON (!buffer
|| !vb
);
988 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
989 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
990 r_size
= ldm_relative (buffer
, buflen
, 0x34, r_name
);
991 r_parent
= ldm_relative (buffer
, buflen
, 0x34, r_size
);
992 r_diskid
= ldm_relative (buffer
, buflen
, 0x34, r_parent
);
994 if (buffer
[0x12] & VBLK_FLAG_PART_INDEX
) {
995 r_index
= ldm_relative (buffer
, buflen
, 0x34, r_diskid
);
1004 len
+= VBLK_SIZE_PRT3
;
1005 if (len
!= BE32 (buffer
+ 0x14))
1008 part
= &vb
->vblk
.part
;
1009 part
->start
= BE64 (buffer
+ 0x24 + r_name
);
1010 part
->volume_offset
= BE64 (buffer
+ 0x2C + r_name
);
1011 part
->size
= ldm_get_vnum (buffer
+ 0x34 + r_name
);
1012 part
->parent_id
= ldm_get_vnum (buffer
+ 0x34 + r_size
);
1013 part
->disk_id
= ldm_get_vnum (buffer
+ 0x34 + r_parent
);
1014 if (vb
->flags
& VBLK_FLAG_PART_INDEX
)
1015 part
->partnum
= buffer
[0x35 + r_diskid
];
1023 * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
1024 * @buffer: Block of data being worked on
1025 * @buflen: Size of the block of data
1026 * @vb: In-memory vblk in which to return information
1028 * Read a raw VBLK Volume object (version 5) into a vblk structure.
1030 * Return: 'true' @vb contains a Volume VBLK
1031 * 'false' @vb contents are not defined
1033 static bool ldm_parse_vol5 (const u8
*buffer
, int buflen
, struct vblk
*vb
)
1035 int r_objid
, r_name
, r_vtype
, r_child
, r_size
, r_id1
, r_id2
, r_size2
;
1037 struct vblk_volu
*volu
;
1039 BUG_ON (!buffer
|| !vb
);
1041 r_objid
= ldm_relative (buffer
, buflen
, 0x18, 0);
1042 r_name
= ldm_relative (buffer
, buflen
, 0x18, r_objid
);
1043 r_vtype
= ldm_relative (buffer
, buflen
, 0x18, r_name
);
1044 r_child
= ldm_relative (buffer
, buflen
, 0x2E, r_vtype
);
1045 r_size
= ldm_relative (buffer
, buflen
, 0x3E, r_child
);
1047 if (buffer
[0x12] & VBLK_FLAG_VOLU_ID1
)
1048 r_id1
= ldm_relative (buffer
, buflen
, 0x53, r_size
);
1052 if (buffer
[0x12] & VBLK_FLAG_VOLU_ID2
)
1053 r_id2
= ldm_relative (buffer
, buflen
, 0x53, r_id1
);
1057 if (buffer
[0x12] & VBLK_FLAG_VOLU_SIZE
)
1058 r_size2
= ldm_relative (buffer
, buflen
, 0x53, r_id2
);
1062 if (buffer
[0x12] & VBLK_FLAG_VOLU_DRIVE
)
1063 r_drive
= ldm_relative (buffer
, buflen
, 0x53, r_size2
);
1071 len
+= VBLK_SIZE_VOL5
;
1072 if (len
!= BE32 (buffer
+ 0x14))
1075 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
+ 0x19 + r_vtype
,
1080 sizeof (volu
->volume_state
));
1081 volu
->size
= ldm_get_vnum (buffer
+ 0x3E + r_child
);
1082 volu
->partition_type
= buffer
[0x42 + r_size
];
1083 memcpy (volu
->guid
, buffer
+ 0x43 + r_size
, sizeof (volu
->guid
));
1084 if (buffer
[0x12] & VBLK_FLAG_VOLU_DRIVE
) {
1085 ldm_get_vstr (buffer
+ 0x53 + 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 group
= BE32 (data
+ 0x08);
1227 rec
= BE16 (data
+ 0x0C);
1228 num
= BE16 (data
+ 0x0E);
1229 if ((num
< 1) || (num
> 4)) {
1230 ldm_error ("A VBLK claims to have %d parts.", num
);
1234 list_for_each (item
, frags
) {
1235 f
= list_entry (item
, struct frag
, list
);
1236 if (f
->group
== group
)
1240 f
= kmalloc (sizeof (*f
) + size
*num
, GFP_KERNEL
);
1242 ldm_crit ("Out of memory.");
1249 f
->map
= 0xFF << num
;
1251 list_add_tail (&f
->list
, frags
);
1253 if (f
->map
& (1 << rec
)) {
1254 ldm_error ("Duplicate VBLK, part %d.", rec
);
1255 f
->map
&= 0x7F; /* Mark the group as broken */
1259 f
->map
|= (1 << rec
);
1262 data
+= VBLK_SIZE_HEAD
;
1263 size
-= VBLK_SIZE_HEAD
;
1265 memcpy (f
->data
+rec
*(size
-VBLK_SIZE_HEAD
)+VBLK_SIZE_HEAD
, data
, size
);
1271 * ldm_frag_free - Free a linked list of VBLK fragments
1272 * @list: Linked list of fragments
1274 * Free a linked list of VBLK fragments
1278 static void ldm_frag_free (struct list_head
*list
)
1280 struct list_head
*item
, *tmp
;
1284 list_for_each_safe (item
, tmp
, list
)
1285 kfree (list_entry (item
, struct frag
, list
));
1289 * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
1290 * @frags: Linked list of VBLK fragments
1291 * @ldb: Cache of the database structures
1293 * Now that all the fragmented VBLKs have been collected, they must be added to
1294 * the database for later use.
1296 * Return: 'true' All the fragments we added successfully
1297 * 'false' One or more of the fragments we invalid
1299 static bool ldm_frag_commit (struct list_head
*frags
, struct ldmdb
*ldb
)
1302 struct list_head
*item
;
1304 BUG_ON (!frags
|| !ldb
);
1306 list_for_each (item
, frags
) {
1307 f
= list_entry (item
, struct frag
, list
);
1309 if (f
->map
!= 0xFF) {
1310 ldm_error ("VBLK group %d is incomplete (0x%02x).",
1315 if (!ldm_ldmdb_add (f
->data
, f
->num
*ldb
->vm
.vblk_size
, ldb
))
1316 return false; /* Already logged */
1322 * ldm_get_vblks - Read the on-disk database of VBLKs into memory
1323 * @bdev: Device holding the LDM Database
1324 * @base: Offset, into @bdev, of the database
1325 * @ldb: Cache of the database structures
1327 * To use the information from the VBLKs, they need to be read from the disk,
1328 * unpacked and validated. We cache them in @ldb according to their type.
1330 * Return: 'true' All the VBLKs were read successfully
1331 * 'false' An error occurred
1333 static bool ldm_get_vblks (struct block_device
*bdev
, unsigned long base
,
1336 int size
, perbuf
, skip
, finish
, s
, v
, recs
;
1339 bool result
= false;
1342 BUG_ON (!bdev
|| !ldb
);
1344 size
= ldb
->vm
.vblk_size
;
1345 perbuf
= 512 / size
;
1346 skip
= ldb
->vm
.vblk_offset
>> 9; /* Bytes to sectors */
1347 finish
= (size
* ldb
->vm
.last_vblk_seq
) >> 9;
1349 for (s
= skip
; s
< finish
; s
++) { /* For each sector */
1350 data
= read_dev_sector (bdev
, base
+ OFF_VMDB
+ s
, §
);
1352 ldm_crit ("Disk read failed.");
1356 for (v
= 0; v
< perbuf
; v
++, data
+=size
) { /* For each vblk */
1357 if (MAGIC_VBLK
!= BE32 (data
)) {
1358 ldm_error ("Expected to find a VBLK.");
1362 recs
= BE16 (data
+ 0x0E); /* Number of records */
1364 if (!ldm_ldmdb_add (data
, size
, ldb
))
1365 goto out
; /* Already logged */
1366 } else if (recs
> 1) {
1367 if (!ldm_frag_add (data
, size
, &frags
))
1368 goto out
; /* Already logged */
1370 /* else Record is not in use, ignore it. */
1372 put_dev_sector (sect
);
1376 result
= ldm_frag_commit (&frags
, ldb
); /* Failures, already logged */
1379 put_dev_sector (sect
);
1380 ldm_frag_free (&frags
);
1386 * ldm_free_vblks - Free a linked list of vblk's
1387 * @lh: Head of a linked list of struct vblk
1389 * Free a list of vblk's and free the memory used to maintain the list.
1393 static void ldm_free_vblks (struct list_head
*lh
)
1395 struct list_head
*item
, *tmp
;
1399 list_for_each_safe (item
, tmp
, lh
)
1400 kfree (list_entry (item
, struct vblk
, list
));
1405 * ldm_partition - Find out whether a device is a dynamic disk and handle it
1406 * @pp: List of the partitions parsed so far
1407 * @bdev: Device holding the LDM Database
1409 * This determines whether the device @bdev is a dynamic disk and if so creates
1410 * the partitions necessary in the gendisk structure pointed to by @hd.
1412 * We create a dummy device 1, which contains the LDM database, and then create
1413 * each partition described by the LDM database in sequence as devices 2+. For
1414 * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
1415 * and so on: the actual data containing partitions.
1417 * Return: 1 Success, @bdev is a dynamic disk and we handled it
1418 * 0 Success, @bdev is not a dynamic disk
1419 * -1 An error occurred before enough information had been read
1420 * Or @bdev is a dynamic disk, but it may be corrupted
1422 int ldm_partition (struct parsed_partitions
*pp
, struct block_device
*bdev
)
1428 BUG_ON (!pp
|| !bdev
);
1430 /* Look for signs of a Dynamic Disk */
1431 if (!ldm_validate_partition_table (bdev
))
1434 ldb
= kmalloc (sizeof (*ldb
), GFP_KERNEL
);
1436 ldm_crit ("Out of memory.");
1440 /* Parse and check privheads. */
1441 if (!ldm_validate_privheads (bdev
, &ldb
->ph
))
1442 goto out
; /* Already logged */
1444 /* All further references are relative to base (database start). */
1445 base
= ldb
->ph
.config_start
;
1447 /* Parse and check tocs and vmdb. */
1448 if (!ldm_validate_tocblocks (bdev
, base
, ldb
) ||
1449 !ldm_validate_vmdb (bdev
, base
, ldb
))
1450 goto out
; /* Already logged */
1452 /* Initialize vblk lists in ldmdb struct */
1453 INIT_LIST_HEAD (&ldb
->v_dgrp
);
1454 INIT_LIST_HEAD (&ldb
->v_disk
);
1455 INIT_LIST_HEAD (&ldb
->v_volu
);
1456 INIT_LIST_HEAD (&ldb
->v_comp
);
1457 INIT_LIST_HEAD (&ldb
->v_part
);
1459 if (!ldm_get_vblks (bdev
, base
, ldb
)) {
1460 ldm_crit ("Failed to read the VBLKs from the database.");
1464 /* Finally, create the data partition devices. */
1465 if (ldm_create_data_partitions (pp
, ldb
)) {
1466 ldm_debug ("Parsed LDM database successfully.");
1469 /* else Already logged */
1472 ldm_free_vblks (&ldb
->v_dgrp
);
1473 ldm_free_vblks (&ldb
->v_disk
);
1474 ldm_free_vblks (&ldb
->v_volu
);
1475 ldm_free_vblks (&ldb
->v_comp
);
1476 ldm_free_vblks (&ldb
->v_part
);