GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / partitions / ldm.c
blob4d770d8e37632fa27a63e9bf642142346c541d9c
1 /**
2 * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
4 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
5 * Copyright (c) 2001-2007 Anton Altaparmakov
6 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
8 * Documentation is available at http://www.linux-ntfs.org/content/view/19/37/
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
13 * version.
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
18 * details.
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>
29 #include <linux/kernel.h>
30 #include "ldm.h"
31 #include "check.h"
32 #include "msdos.h"
34 /**
35 * ldm_debug/info/error/crit - Output an error message
36 * @f: A printf format string containing the message
37 * @...: Variables to substitute into @f
39 * ldm_debug() writes a DEBUG level message to the syslog but only if the
40 * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
42 #ifndef CONFIG_LDM_DEBUG
43 #define ldm_debug(...) do {} while (0)
44 #else
45 #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
46 #endif
48 #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
49 #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
50 #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
52 __attribute__ ((format (printf, 3, 4)))
53 static void _ldm_printk (const char *level, const char *function,
54 const char *fmt, ...)
56 static char buf[128];
57 va_list args;
59 va_start (args, fmt);
60 vsnprintf (buf, sizeof (buf), fmt, args);
61 va_end (args);
63 printk ("%s%s(): %s\n", level, function, buf);
66 /**
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 */
78 int h;
80 /* high part */
81 x = h = hex_to_bin(src[0]);
82 if (h < 0)
83 return -1;
85 /* low part */
86 h = hex_to_bin(src[1]);
87 if (h < 0)
88 return -1;
90 return (x << 4) + h;
93 /**
94 * ldm_parse_guid - Convert GUID from ASCII to binary
95 * @src: 36 char string of the form fa50ff2b-f2e8-45de-83fa-65417f2f49ba
96 * @dest: Memory block to hold binary GUID (16 bytes)
98 * N.B. The GUID need not be NULL terminated.
100 * Return: 'true' @dest contains binary GUID
101 * 'false' @dest contents are undefined
103 static bool ldm_parse_guid (const u8 *src, u8 *dest)
105 static const int size[] = { 4, 2, 2, 2, 6 };
106 int i, j, v;
108 if (src[8] != '-' || src[13] != '-' ||
109 src[18] != '-' || src[23] != '-')
110 return false;
112 for (j = 0; j < 5; j++, src++)
113 for (i = 0; i < size[j]; i++, src+=2, *dest++ = v)
114 if ((v = ldm_parse_hexbyte (src)) < 0)
115 return false;
117 return true;
121 * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
122 * @data: Raw database PRIVHEAD structure loaded from the device
123 * @ph: In-memory privhead structure in which to return parsed information
125 * This parses the LDM database PRIVHEAD structure supplied in @data and
126 * sets up the in-memory privhead structure @ph with the obtained information.
128 * Return: 'true' @ph contains the PRIVHEAD data
129 * 'false' @ph contents are undefined
131 static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
133 bool is_vista = false;
135 BUG_ON(!data || !ph);
136 if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
137 ldm_error("Cannot find PRIVHEAD structure. LDM database is"
138 " corrupt. Aborting.");
139 return false;
141 ph->ver_major = get_unaligned_be16(data + 0x000C);
142 ph->ver_minor = get_unaligned_be16(data + 0x000E);
143 ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
144 ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
145 ph->config_start = get_unaligned_be64(data + 0x012B);
146 ph->config_size = get_unaligned_be64(data + 0x0133);
147 /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
148 if (ph->ver_major == 2 && ph->ver_minor == 12)
149 is_vista = true;
150 if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
151 ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
152 " Aborting.", ph->ver_major, ph->ver_minor);
153 return false;
155 ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
156 ph->ver_minor, is_vista ? "Vista" : "2000/XP");
157 if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
158 /* Warn the user and continue, carefully. */
159 ldm_info("Database is normally %u bytes, it claims to "
160 "be %llu bytes.", LDM_DB_SIZE,
161 (unsigned long long)ph->config_size);
163 if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
164 ph->logical_disk_size > ph->config_start)) {
165 ldm_error("PRIVHEAD disk size doesn't match real disk size");
166 return false;
168 if (!ldm_parse_guid(data + 0x0030, ph->disk_id)) {
169 ldm_error("PRIVHEAD contains an invalid GUID.");
170 return false;
172 ldm_debug("Parsed PRIVHEAD successfully.");
173 return true;
177 * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
178 * @data: Raw database TOCBLOCK structure loaded from the device
179 * @toc: In-memory toc structure in which to return parsed information
181 * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
182 * in @data and sets up the in-memory tocblock structure @toc with the obtained
183 * information.
185 * N.B. The *_start and *_size values returned in @toc are not range-checked.
187 * Return: 'true' @toc contains the TOCBLOCK data
188 * 'false' @toc contents are undefined
190 static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
192 BUG_ON (!data || !toc);
194 if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
195 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
196 return false;
198 strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
199 toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
200 toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
201 toc->bitmap1_size = get_unaligned_be64(data + 0x36);
203 if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
204 sizeof (toc->bitmap1_name)) != 0) {
205 ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
206 TOC_BITMAP1, toc->bitmap1_name);
207 return false;
209 strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
210 toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
211 toc->bitmap2_start = get_unaligned_be64(data + 0x50);
212 toc->bitmap2_size = get_unaligned_be64(data + 0x58);
213 if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
214 sizeof (toc->bitmap2_name)) != 0) {
215 ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
216 TOC_BITMAP2, toc->bitmap2_name);
217 return false;
219 ldm_debug ("Parsed TOCBLOCK successfully.");
220 return true;
224 * ldm_parse_vmdb - Read the LDM Database VMDB structure
225 * @data: Raw database VMDB structure loaded from the device
226 * @vm: In-memory vmdb structure in which to return parsed information
228 * This parses the LDM Database VMDB structure supplied in @data and sets up
229 * the in-memory vmdb structure @vm with the obtained information.
231 * N.B. The *_start, *_size and *_seq values will be range-checked later.
233 * Return: 'true' @vm contains VMDB info
234 * 'false' @vm contents are undefined
236 static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
238 BUG_ON (!data || !vm);
240 if (MAGIC_VMDB != get_unaligned_be32(data)) {
241 ldm_crit ("Cannot find the VMDB, database may be corrupt.");
242 return false;
245 vm->ver_major = get_unaligned_be16(data + 0x12);
246 vm->ver_minor = get_unaligned_be16(data + 0x14);
247 if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
248 ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
249 "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
250 return false;
253 vm->vblk_size = get_unaligned_be32(data + 0x08);
254 vm->vblk_offset = get_unaligned_be32(data + 0x0C);
255 vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
257 ldm_debug ("Parsed VMDB successfully.");
258 return true;
262 * ldm_compare_privheads - Compare two privhead objects
263 * @ph1: First privhead
264 * @ph2: Second privhead
266 * This compares the two privhead structures @ph1 and @ph2.
268 * Return: 'true' Identical
269 * 'false' Different
271 static bool ldm_compare_privheads (const struct privhead *ph1,
272 const struct privhead *ph2)
274 BUG_ON (!ph1 || !ph2);
276 return ((ph1->ver_major == ph2->ver_major) &&
277 (ph1->ver_minor == ph2->ver_minor) &&
278 (ph1->logical_disk_start == ph2->logical_disk_start) &&
279 (ph1->logical_disk_size == ph2->logical_disk_size) &&
280 (ph1->config_start == ph2->config_start) &&
281 (ph1->config_size == ph2->config_size) &&
282 !memcmp (ph1->disk_id, ph2->disk_id, GUID_SIZE));
286 * ldm_compare_tocblocks - Compare two tocblock objects
287 * @toc1: First toc
288 * @toc2: Second toc
290 * This compares the two tocblock structures @toc1 and @toc2.
292 * Return: 'true' Identical
293 * 'false' Different
295 static bool ldm_compare_tocblocks (const struct tocblock *toc1,
296 const struct tocblock *toc2)
298 BUG_ON (!toc1 || !toc2);
300 return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
301 (toc1->bitmap1_size == toc2->bitmap1_size) &&
302 (toc1->bitmap2_start == toc2->bitmap2_start) &&
303 (toc1->bitmap2_size == toc2->bitmap2_size) &&
304 !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
305 sizeof (toc1->bitmap1_name)) &&
306 !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
307 sizeof (toc1->bitmap2_name)));
311 * ldm_validate_privheads - Compare the primary privhead with its backups
312 * @state: Partition check state including device holding the LDM Database
313 * @ph1: Memory struct to fill with ph contents
315 * Read and compare all three privheads from disk.
317 * The privheads on disk show the size and location of the main disk area and
318 * the configuration area (the database). The values are range-checked against
319 * @hd, which contains the real size of the disk.
321 * Return: 'true' Success
322 * 'false' Error
324 static bool ldm_validate_privheads(struct parsed_partitions *state,
325 struct privhead *ph1)
327 static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
328 struct privhead *ph[3] = { ph1 };
329 Sector sect;
330 u8 *data;
331 bool result = false;
332 long num_sects;
333 int i;
335 BUG_ON (!state || !ph1);
337 ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
338 ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
339 if (!ph[1] || !ph[2]) {
340 ldm_crit ("Out of memory.");
341 goto out;
344 /* off[1 & 2] are relative to ph[0]->config_start */
345 ph[0]->config_start = 0;
347 /* Read and parse privheads */
348 for (i = 0; i < 3; i++) {
349 data = read_part_sector(state, ph[0]->config_start + off[i],
350 &sect);
351 if (!data) {
352 ldm_crit ("Disk read failed.");
353 goto out;
355 result = ldm_parse_privhead (data, ph[i]);
356 put_dev_sector (sect);
357 if (!result) {
358 ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
359 if (i < 2)
360 goto out; /* Already logged */
361 else
362 break;
366 num_sects = state->bdev->bd_inode->i_size >> 9;
368 if ((ph[0]->config_start > num_sects) ||
369 ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
370 ldm_crit ("Database extends beyond the end of the disk.");
371 goto out;
374 if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
375 ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
376 > ph[0]->config_start)) {
377 ldm_crit ("Disk and database overlap.");
378 goto out;
381 if (!ldm_compare_privheads (ph[0], ph[1])) {
382 ldm_crit ("Primary and backup PRIVHEADs don't match.");
383 goto out;
385 ldm_debug ("Validated PRIVHEADs successfully.");
386 result = true;
387 out:
388 kfree (ph[1]);
389 kfree (ph[2]);
390 return result;
394 * ldm_validate_tocblocks - Validate the table of contents and its backups
395 * @state: Partition check state including device holding the LDM Database
396 * @base: Offset, into @state->bdev, of the database
397 * @ldb: Cache of the database structures
399 * Find and compare the four tables of contents of the LDM Database stored on
400 * @state->bdev and return the parsed information into @toc1.
402 * The offsets and sizes of the configs are range-checked against a privhead.
404 * Return: 'true' @toc1 contains validated TOCBLOCK info
405 * 'false' @toc1 contents are undefined
407 static bool ldm_validate_tocblocks(struct parsed_partitions *state,
408 unsigned long base, struct ldmdb *ldb)
410 static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
411 struct tocblock *tb[4];
412 struct privhead *ph;
413 Sector sect;
414 u8 *data;
415 int i, nr_tbs;
416 bool result = false;
418 BUG_ON(!state || !ldb);
419 ph = &ldb->ph;
420 tb[0] = &ldb->toc;
421 tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL);
422 if (!tb[1]) {
423 ldm_crit("Out of memory.");
424 goto err;
426 tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
427 tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
429 * Try to read and parse all four TOCBLOCKs.
431 * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
432 * skip any that fail as long as we get at least one valid TOCBLOCK.
434 for (nr_tbs = i = 0; i < 4; i++) {
435 data = read_part_sector(state, base + off[i], &sect);
436 if (!data) {
437 ldm_error("Disk read failed for TOCBLOCK %d.", i);
438 continue;
440 if (ldm_parse_tocblock(data, tb[nr_tbs]))
441 nr_tbs++;
442 put_dev_sector(sect);
444 if (!nr_tbs) {
445 ldm_crit("Failed to find a valid TOCBLOCK.");
446 goto err;
448 /* Range check the TOCBLOCK against a privhead. */
449 if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
450 ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
451 ph->config_size)) {
452 ldm_crit("The bitmaps are out of range. Giving up.");
453 goto err;
455 /* Compare all loaded TOCBLOCKs. */
456 for (i = 1; i < nr_tbs; i++) {
457 if (!ldm_compare_tocblocks(tb[0], tb[i])) {
458 ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
459 goto err;
462 ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
463 result = true;
464 err:
465 kfree(tb[1]);
466 return result;
470 * ldm_validate_vmdb - Read the VMDB and validate it
471 * @state: Partition check state including device holding the LDM Database
472 * @base: Offset, into @bdev, of the database
473 * @ldb: Cache of the database structures
475 * Find the vmdb of the LDM Database stored on @bdev and return the parsed
476 * information in @ldb.
478 * Return: 'true' @ldb contains validated VBDB info
479 * 'false' @ldb contents are undefined
481 static bool ldm_validate_vmdb(struct parsed_partitions *state,
482 unsigned long base, struct ldmdb *ldb)
484 Sector sect;
485 u8 *data;
486 bool result = false;
487 struct vmdb *vm;
488 struct tocblock *toc;
490 BUG_ON (!state || !ldb);
492 vm = &ldb->vm;
493 toc = &ldb->toc;
495 data = read_part_sector(state, base + OFF_VMDB, &sect);
496 if (!data) {
497 ldm_crit ("Disk read failed.");
498 return false;
501 if (!ldm_parse_vmdb (data, vm))
502 goto out; /* Already logged */
504 /* Are there uncommitted transactions? */
505 if (get_unaligned_be16(data + 0x10) != 0x01) {
506 ldm_crit ("Database is not in a consistent state. Aborting.");
507 goto out;
510 if (vm->vblk_offset != 512)
511 ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
514 * The last_vblkd_seq can be before the end of the vmdb, just make sure
515 * it is not out of bounds.
517 if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
518 ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
519 "Database is corrupt. Aborting.");
520 goto out;
523 result = true;
524 out:
525 put_dev_sector (sect);
526 return result;
531 * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
532 * @state: Partition check state including device holding the LDM Database
534 * This function provides a weak test to decide whether the device is a dynamic
535 * disk or not. It looks for an MS-DOS-style partition table containing at
536 * least one partition of type 0x42 (formerly SFS, now used by Windows for
537 * dynamic disks).
539 * N.B. The only possible error can come from the read_part_sector and that is
540 * only likely to happen if the underlying device is strange. If that IS
541 * the case we should return zero to let someone else try.
543 * Return: 'true' @state->bdev is a dynamic disk
544 * 'false' @state->bdev is not a dynamic disk, or an error occurred
546 static bool ldm_validate_partition_table(struct parsed_partitions *state)
548 Sector sect;
549 u8 *data;
550 struct partition *p;
551 int i;
552 bool result = false;
554 BUG_ON(!state);
556 data = read_part_sector(state, 0, &sect);
557 if (!data) {
558 ldm_crit ("Disk read failed.");
559 return false;
562 if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
563 goto out;
565 p = (struct partition*)(data + 0x01BE);
566 for (i = 0; i < 4; i++, p++)
567 if (SYS_IND (p) == LDM_PARTITION) {
568 result = true;
569 break;
572 if (result)
573 ldm_debug ("Found W2K dynamic disk partition type.");
575 out:
576 put_dev_sector (sect);
577 return result;
581 * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
582 * @ldb: Cache of the database structures
584 * The LDM Database contains a list of all partitions on all dynamic disks.
585 * The primary PRIVHEAD, at the beginning of the physical disk, tells us
586 * the GUID of this disk. This function searches for the GUID in a linked
587 * list of vblk's.
589 * Return: Pointer, A matching vblk was found
590 * NULL, No match, or an error
592 static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
594 struct list_head *item;
596 BUG_ON (!ldb);
598 list_for_each (item, &ldb->v_disk) {
599 struct vblk *v = list_entry (item, struct vblk, list);
600 if (!memcmp (v->vblk.disk.disk_id, ldb->ph.disk_id, GUID_SIZE))
601 return v;
604 return NULL;
608 * ldm_create_data_partitions - Create data partitions for this device
609 * @pp: List of the partitions parsed so far
610 * @ldb: Cache of the database structures
612 * The database contains ALL the partitions for ALL disk groups, so we need to
613 * filter out this specific disk. Using the disk's object id, we can find all
614 * the partitions in the database that belong to this disk.
616 * Add each partition in our database, to the parsed_partitions structure.
618 * N.B. This function creates the partitions in the order it finds partition
619 * objects in the linked list.
621 * Return: 'true' Partition created
622 * 'false' Error, probably a range checking problem
624 static bool ldm_create_data_partitions (struct parsed_partitions *pp,
625 const struct ldmdb *ldb)
627 struct list_head *item;
628 struct vblk *vb;
629 struct vblk *disk;
630 struct vblk_part *part;
631 int part_num = 1;
633 BUG_ON (!pp || !ldb);
635 disk = ldm_get_disk_objid (ldb);
636 if (!disk) {
637 ldm_crit ("Can't find the ID of this disk in the database.");
638 return false;
641 strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
643 /* Create the data partitions */
644 list_for_each (item, &ldb->v_part) {
645 vb = list_entry (item, struct vblk, list);
646 part = &vb->vblk.part;
648 if (part->disk_id != disk->obj_id)
649 continue;
651 put_partition (pp, part_num, ldb->ph.logical_disk_start +
652 part->start, part->size);
653 part_num++;
656 strlcat(pp->pp_buf, "\n", PAGE_SIZE);
657 return true;
662 * ldm_relative - Calculate the next relative offset
663 * @buffer: Block of data being worked on
664 * @buflen: Size of the block of data
665 * @base: Size of the previous fixed width fields
666 * @offset: Cumulative size of the previous variable-width fields
668 * Because many of the VBLK fields are variable-width, it's necessary
669 * to calculate each offset based on the previous one and the length
670 * of the field it pointed to.
672 * Return: -1 Error, the calculated offset exceeded the size of the buffer
673 * n OK, a range-checked offset into buffer
675 static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
678 base += offset;
679 if (!buffer || offset < 0 || base > buflen) {
680 if (!buffer)
681 ldm_error("!buffer");
682 if (offset < 0)
683 ldm_error("offset (%d) < 0", offset);
684 if (base > buflen)
685 ldm_error("base (%d) > buflen (%d)", base, buflen);
686 return -1;
688 if (base + buffer[base] >= buflen) {
689 ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
690 buffer[base], buflen);
691 return -1;
693 return buffer[base] + offset + 1;
697 * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
698 * @block: Pointer to the variable-width number to convert
700 * Large numbers in the LDM Database are often stored in a packed format. Each
701 * number is prefixed by a one byte width marker. All numbers in the database
702 * are stored in big-endian byte order. This function reads one of these
703 * numbers and returns the result
705 * N.B. This function DOES NOT perform any range checking, though the most
706 * it will read is eight bytes.
708 * Return: n A number
709 * 0 Zero, or an error occurred
711 static u64 ldm_get_vnum (const u8 *block)
713 u64 tmp = 0;
714 u8 length;
716 BUG_ON (!block);
718 length = *block++;
720 if (length && length <= 8)
721 while (length--)
722 tmp = (tmp << 8) | *block++;
723 else
724 ldm_error ("Illegal length %d.", length);
726 return tmp;
730 * ldm_get_vstr - Read a length-prefixed string into a buffer
731 * @block: Pointer to the length marker
732 * @buffer: Location to copy string to
733 * @buflen: Size of the output buffer
735 * Many of the strings in the LDM Database are not NULL terminated. Instead
736 * they are prefixed by a one byte length marker. This function copies one of
737 * these strings into a buffer.
739 * N.B. This function DOES NOT perform any range checking on the input.
740 * If the buffer is too small, the output will be truncated.
742 * Return: 0, Error and @buffer contents are undefined
743 * n, String length in characters (excluding NULL)
744 * buflen-1, String was truncated.
746 static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
748 int length;
750 BUG_ON (!block || !buffer);
752 length = block[0];
753 if (length >= buflen) {
754 ldm_error ("Truncating string %d -> %d.", length, buflen);
755 length = buflen - 1;
757 memcpy (buffer, block + 1, length);
758 buffer[length] = 0;
759 return length;
764 * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
765 * @buffer: Block of data being worked on
766 * @buflen: Size of the block of data
767 * @vb: In-memory vblk in which to return information
769 * Read a raw VBLK Component object (version 3) into a vblk structure.
771 * Return: 'true' @vb contains a Component VBLK
772 * 'false' @vb contents are not defined
774 static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
776 int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
777 struct vblk_comp *comp;
779 BUG_ON (!buffer || !vb);
781 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
782 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
783 r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
784 r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
785 r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
787 if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
788 r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
789 r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
790 len = r_cols;
791 } else {
792 r_stripe = 0;
793 r_cols = 0;
794 len = r_parent;
796 if (len < 0)
797 return false;
799 len += VBLK_SIZE_CMP3;
800 if (len != get_unaligned_be32(buffer + 0x14))
801 return false;
803 comp = &vb->vblk.comp;
804 ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
805 sizeof (comp->state));
806 comp->type = buffer[0x18 + r_vstate];
807 comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
808 comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
809 comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
811 return true;
815 * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
816 * @buffer: Block of data being worked on
817 * @buflen: Size of the block of data
818 * @vb: In-memory vblk in which to return information
820 * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
822 * Return: 'true' @vb contains a Disk Group VBLK
823 * 'false' @vb contents are not defined
825 static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
827 int r_objid, r_name, r_diskid, r_id1, r_id2, len;
828 struct vblk_dgrp *dgrp;
830 BUG_ON (!buffer || !vb);
832 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
833 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
834 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
836 if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
837 r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
838 r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
839 len = r_id2;
840 } else {
841 r_id1 = 0;
842 r_id2 = 0;
843 len = r_diskid;
845 if (len < 0)
846 return false;
848 len += VBLK_SIZE_DGR3;
849 if (len != get_unaligned_be32(buffer + 0x14))
850 return false;
852 dgrp = &vb->vblk.dgrp;
853 ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
854 sizeof (dgrp->disk_id));
855 return true;
859 * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
860 * @buffer: Block of data being worked on
861 * @buflen: Size of the block of data
862 * @vb: In-memory vblk in which to return information
864 * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
866 * Return: 'true' @vb contains a Disk Group VBLK
867 * 'false' @vb contents are not defined
869 static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
871 char buf[64];
872 int r_objid, r_name, r_id1, r_id2, len;
873 struct vblk_dgrp *dgrp;
875 BUG_ON (!buffer || !vb);
877 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
878 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
880 if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
881 r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
882 r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
883 len = r_id2;
884 } else {
885 r_id1 = 0;
886 r_id2 = 0;
887 len = r_name;
889 if (len < 0)
890 return false;
892 len += VBLK_SIZE_DGR4;
893 if (len != get_unaligned_be32(buffer + 0x14))
894 return false;
896 dgrp = &vb->vblk.dgrp;
898 ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
899 return true;
903 * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
904 * @buffer: Block of data being worked on
905 * @buflen: Size of the block of data
906 * @vb: In-memory vblk in which to return information
908 * Read a raw VBLK Disk object (version 3) into a vblk structure.
910 * Return: 'true' @vb contains a Disk VBLK
911 * 'false' @vb contents are not defined
913 static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
915 int r_objid, r_name, r_diskid, r_altname, len;
916 struct vblk_disk *disk;
918 BUG_ON (!buffer || !vb);
920 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
921 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
922 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
923 r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
924 len = r_altname;
925 if (len < 0)
926 return false;
928 len += VBLK_SIZE_DSK3;
929 if (len != get_unaligned_be32(buffer + 0x14))
930 return false;
932 disk = &vb->vblk.disk;
933 ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
934 sizeof (disk->alt_name));
935 if (!ldm_parse_guid (buffer + 0x19 + r_name, disk->disk_id))
936 return false;
938 return true;
942 * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
943 * @buffer: Block of data being worked on
944 * @buflen: Size of the block of data
945 * @vb: In-memory vblk in which to return information
947 * Read a raw VBLK Disk object (version 4) into a vblk structure.
949 * Return: 'true' @vb contains a Disk VBLK
950 * 'false' @vb contents are not defined
952 static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
954 int r_objid, r_name, len;
955 struct vblk_disk *disk;
957 BUG_ON (!buffer || !vb);
959 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
960 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
961 len = r_name;
962 if (len < 0)
963 return false;
965 len += VBLK_SIZE_DSK4;
966 if (len != get_unaligned_be32(buffer + 0x14))
967 return false;
969 disk = &vb->vblk.disk;
970 memcpy (disk->disk_id, buffer + 0x18 + r_name, GUID_SIZE);
971 return true;
975 * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
976 * @buffer: Block of data being worked on
977 * @buflen: Size of the block of data
978 * @vb: In-memory vblk in which to return information
980 * Read a raw VBLK Partition object (version 3) into a vblk structure.
982 * Return: 'true' @vb contains a Partition VBLK
983 * 'false' @vb contents are not defined
985 static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
987 int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
988 struct vblk_part *part;
990 BUG_ON(!buffer || !vb);
991 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
992 if (r_objid < 0) {
993 ldm_error("r_objid %d < 0", r_objid);
994 return false;
996 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
997 if (r_name < 0) {
998 ldm_error("r_name %d < 0", r_name);
999 return false;
1001 r_size = ldm_relative(buffer, buflen, 0x34, r_name);
1002 if (r_size < 0) {
1003 ldm_error("r_size %d < 0", r_size);
1004 return false;
1006 r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
1007 if (r_parent < 0) {
1008 ldm_error("r_parent %d < 0", r_parent);
1009 return false;
1011 r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
1012 if (r_diskid < 0) {
1013 ldm_error("r_diskid %d < 0", r_diskid);
1014 return false;
1016 if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
1017 r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
1018 if (r_index < 0) {
1019 ldm_error("r_index %d < 0", r_index);
1020 return false;
1022 len = r_index;
1023 } else {
1024 r_index = 0;
1025 len = r_diskid;
1027 if (len < 0) {
1028 ldm_error("len %d < 0", len);
1029 return false;
1031 len += VBLK_SIZE_PRT3;
1032 if (len > get_unaligned_be32(buffer + 0x14)) {
1033 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
1034 get_unaligned_be32(buffer + 0x14));
1035 return false;
1037 part = &vb->vblk.part;
1038 part->start = get_unaligned_be64(buffer + 0x24 + r_name);
1039 part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
1040 part->size = ldm_get_vnum(buffer + 0x34 + r_name);
1041 part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
1042 part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
1043 if (vb->flags & VBLK_FLAG_PART_INDEX)
1044 part->partnum = buffer[0x35 + r_diskid];
1045 else
1046 part->partnum = 0;
1047 return true;
1051 * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
1052 * @buffer: Block of data being worked on
1053 * @buflen: Size of the block of data
1054 * @vb: In-memory vblk in which to return information
1056 * Read a raw VBLK Volume object (version 5) into a vblk structure.
1058 * Return: 'true' @vb contains a Volume VBLK
1059 * 'false' @vb contents are not defined
1061 static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
1063 int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
1064 int r_id1, r_id2, r_size2, r_drive, len;
1065 struct vblk_volu *volu;
1067 BUG_ON(!buffer || !vb);
1068 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
1069 if (r_objid < 0) {
1070 ldm_error("r_objid %d < 0", r_objid);
1071 return false;
1073 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
1074 if (r_name < 0) {
1075 ldm_error("r_name %d < 0", r_name);
1076 return false;
1078 r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
1079 if (r_vtype < 0) {
1080 ldm_error("r_vtype %d < 0", r_vtype);
1081 return false;
1083 r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
1084 if (r_disable_drive_letter < 0) {
1085 ldm_error("r_disable_drive_letter %d < 0",
1086 r_disable_drive_letter);
1087 return false;
1089 r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
1090 if (r_child < 0) {
1091 ldm_error("r_child %d < 0", r_child);
1092 return false;
1094 r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
1095 if (r_size < 0) {
1096 ldm_error("r_size %d < 0", r_size);
1097 return false;
1099 if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
1100 r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
1101 if (r_id1 < 0) {
1102 ldm_error("r_id1 %d < 0", r_id1);
1103 return false;
1105 } else
1106 r_id1 = r_size;
1107 if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
1108 r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
1109 if (r_id2 < 0) {
1110 ldm_error("r_id2 %d < 0", r_id2);
1111 return false;
1113 } else
1114 r_id2 = r_id1;
1115 if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
1116 r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
1117 if (r_size2 < 0) {
1118 ldm_error("r_size2 %d < 0", r_size2);
1119 return false;
1121 } else
1122 r_size2 = r_id2;
1123 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1124 r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
1125 if (r_drive < 0) {
1126 ldm_error("r_drive %d < 0", r_drive);
1127 return false;
1129 } else
1130 r_drive = r_size2;
1131 len = r_drive;
1132 if (len < 0) {
1133 ldm_error("len %d < 0", len);
1134 return false;
1136 len += VBLK_SIZE_VOL5;
1137 if (len > get_unaligned_be32(buffer + 0x14)) {
1138 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
1139 get_unaligned_be32(buffer + 0x14));
1140 return false;
1142 volu = &vb->vblk.volu;
1143 ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
1144 sizeof(volu->volume_type));
1145 memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
1146 sizeof(volu->volume_state));
1147 volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
1148 volu->partition_type = buffer[0x41 + r_size];
1149 memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
1150 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1151 ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
1152 sizeof(volu->drive_hint));
1154 return true;
1158 * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
1159 * @buf: Block of data being worked on
1160 * @len: Size of the block of data
1161 * @vb: In-memory vblk in which to return information
1163 * Read a raw VBLK object into a vblk structure. This function just reads the
1164 * information common to all VBLK types, then delegates the rest of the work to
1165 * helper functions: ldm_parse_*.
1167 * Return: 'true' @vb contains a VBLK
1168 * 'false' @vb contents are not defined
1170 static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
1172 bool result = false;
1173 int r_objid;
1175 BUG_ON (!buf || !vb);
1177 r_objid = ldm_relative (buf, len, 0x18, 0);
1178 if (r_objid < 0) {
1179 ldm_error ("VBLK header is corrupt.");
1180 return false;
1183 vb->flags = buf[0x12];
1184 vb->type = buf[0x13];
1185 vb->obj_id = ldm_get_vnum (buf + 0x18);
1186 ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
1188 switch (vb->type) {
1189 case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
1190 case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
1191 case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
1192 case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
1193 case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
1194 case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
1195 case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
1198 if (result)
1199 ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
1200 (unsigned long long) vb->obj_id, vb->type);
1201 else
1202 ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
1203 (unsigned long long) vb->obj_id, vb->type);
1205 return result;
1210 * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
1211 * @data: Raw VBLK to add to the database
1212 * @len: Size of the raw VBLK
1213 * @ldb: Cache of the database structures
1215 * The VBLKs are sorted into categories. Partitions are also sorted by offset.
1217 * N.B. This function does not check the validity of the VBLKs.
1219 * Return: 'true' The VBLK was added
1220 * 'false' An error occurred
1222 static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
1224 struct vblk *vb;
1225 struct list_head *item;
1227 BUG_ON (!data || !ldb);
1229 vb = kmalloc (sizeof (*vb), GFP_KERNEL);
1230 if (!vb) {
1231 ldm_crit ("Out of memory.");
1232 return false;
1235 if (!ldm_parse_vblk (data, len, vb)) {
1236 kfree(vb);
1237 return false; /* Already logged */
1240 /* Put vblk into the correct list. */
1241 switch (vb->type) {
1242 case VBLK_DGR3:
1243 case VBLK_DGR4:
1244 list_add (&vb->list, &ldb->v_dgrp);
1245 break;
1246 case VBLK_DSK3:
1247 case VBLK_DSK4:
1248 list_add (&vb->list, &ldb->v_disk);
1249 break;
1250 case VBLK_VOL5:
1251 list_add (&vb->list, &ldb->v_volu);
1252 break;
1253 case VBLK_CMP3:
1254 list_add (&vb->list, &ldb->v_comp);
1255 break;
1256 case VBLK_PRT3:
1257 /* Sort by the partition's start sector. */
1258 list_for_each (item, &ldb->v_part) {
1259 struct vblk *v = list_entry (item, struct vblk, list);
1260 if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
1261 (v->vblk.part.start > vb->vblk.part.start)) {
1262 list_add_tail (&vb->list, &v->list);
1263 return true;
1266 list_add_tail (&vb->list, &ldb->v_part);
1267 break;
1269 return true;
1273 * ldm_frag_add - Add a VBLK fragment to a list
1274 * @data: Raw fragment to be added to the list
1275 * @size: Size of the raw fragment
1276 * @frags: Linked list of VBLK fragments
1278 * Fragmented VBLKs may not be consecutive in the database, so they are placed
1279 * in a list so they can be pieced together later.
1281 * Return: 'true' Success, the VBLK was added to the list
1282 * 'false' Error, a problem occurred
1284 static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
1286 struct frag *f;
1287 struct list_head *item;
1288 int rec, num, group;
1290 BUG_ON (!data || !frags);
1292 group = get_unaligned_be32(data + 0x08);
1293 rec = get_unaligned_be16(data + 0x0C);
1294 num = get_unaligned_be16(data + 0x0E);
1295 if ((num < 1) || (num > 4)) {
1296 ldm_error ("A VBLK claims to have %d parts.", num);
1297 return false;
1300 list_for_each (item, frags) {
1301 f = list_entry (item, struct frag, list);
1302 if (f->group == group)
1303 goto found;
1306 f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
1307 if (!f) {
1308 ldm_crit ("Out of memory.");
1309 return false;
1312 f->group = group;
1313 f->num = num;
1314 f->rec = rec;
1315 f->map = 0xFF << num;
1317 list_add_tail (&f->list, frags);
1318 found:
1319 if (f->map & (1 << rec)) {
1320 ldm_error ("Duplicate VBLK, part %d.", rec);
1321 f->map &= 0x7F; /* Mark the group as broken */
1322 return false;
1325 f->map |= (1 << rec);
1327 if (num > 0) {
1328 data += VBLK_SIZE_HEAD;
1329 size -= VBLK_SIZE_HEAD;
1331 memcpy (f->data+rec*(size-VBLK_SIZE_HEAD)+VBLK_SIZE_HEAD, data, size);
1333 return true;
1337 * ldm_frag_free - Free a linked list of VBLK fragments
1338 * @list: Linked list of fragments
1340 * Free a linked list of VBLK fragments
1342 * Return: none
1344 static void ldm_frag_free (struct list_head *list)
1346 struct list_head *item, *tmp;
1348 BUG_ON (!list);
1350 list_for_each_safe (item, tmp, list)
1351 kfree (list_entry (item, struct frag, list));
1355 * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
1356 * @frags: Linked list of VBLK fragments
1357 * @ldb: Cache of the database structures
1359 * Now that all the fragmented VBLKs have been collected, they must be added to
1360 * the database for later use.
1362 * Return: 'true' All the fragments we added successfully
1363 * 'false' One or more of the fragments we invalid
1365 static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
1367 struct frag *f;
1368 struct list_head *item;
1370 BUG_ON (!frags || !ldb);
1372 list_for_each (item, frags) {
1373 f = list_entry (item, struct frag, list);
1375 if (f->map != 0xFF) {
1376 ldm_error ("VBLK group %d is incomplete (0x%02x).",
1377 f->group, f->map);
1378 return false;
1381 if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
1382 return false; /* Already logged */
1384 return true;
1388 * ldm_get_vblks - Read the on-disk database of VBLKs into memory
1389 * @state: Partition check state including device holding the LDM Database
1390 * @base: Offset, into @state->bdev, of the database
1391 * @ldb: Cache of the database structures
1393 * To use the information from the VBLKs, they need to be read from the disk,
1394 * unpacked and validated. We cache them in @ldb according to their type.
1396 * Return: 'true' All the VBLKs were read successfully
1397 * 'false' An error occurred
1399 static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
1400 struct ldmdb *ldb)
1402 int size, perbuf, skip, finish, s, v, recs;
1403 u8 *data = NULL;
1404 Sector sect;
1405 bool result = false;
1406 LIST_HEAD (frags);
1408 BUG_ON(!state || !ldb);
1410 size = ldb->vm.vblk_size;
1411 perbuf = 512 / size;
1412 skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
1413 finish = (size * ldb->vm.last_vblk_seq) >> 9;
1415 for (s = skip; s < finish; s++) { /* For each sector */
1416 data = read_part_sector(state, base + OFF_VMDB + s, &sect);
1417 if (!data) {
1418 ldm_crit ("Disk read failed.");
1419 goto out;
1422 for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
1423 if (MAGIC_VBLK != get_unaligned_be32(data)) {
1424 ldm_error ("Expected to find a VBLK.");
1425 goto out;
1428 recs = get_unaligned_be16(data + 0x0E); /* Number of records */
1429 if (recs == 1) {
1430 if (!ldm_ldmdb_add (data, size, ldb))
1431 goto out; /* Already logged */
1432 } else if (recs > 1) {
1433 if (!ldm_frag_add (data, size, &frags))
1434 goto out; /* Already logged */
1436 /* else Record is not in use, ignore it. */
1438 put_dev_sector (sect);
1439 data = NULL;
1442 result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
1443 out:
1444 if (data)
1445 put_dev_sector (sect);
1446 ldm_frag_free (&frags);
1448 return result;
1452 * ldm_free_vblks - Free a linked list of vblk's
1453 * @lh: Head of a linked list of struct vblk
1455 * Free a list of vblk's and free the memory used to maintain the list.
1457 * Return: none
1459 static void ldm_free_vblks (struct list_head *lh)
1461 struct list_head *item, *tmp;
1463 BUG_ON (!lh);
1465 list_for_each_safe (item, tmp, lh)
1466 kfree (list_entry (item, struct vblk, list));
1471 * ldm_partition - Find out whether a device is a dynamic disk and handle it
1472 * @state: Partition check state including device holding the LDM Database
1474 * This determines whether the device @bdev is a dynamic disk and if so creates
1475 * the partitions necessary in the gendisk structure pointed to by @hd.
1477 * We create a dummy device 1, which contains the LDM database, and then create
1478 * each partition described by the LDM database in sequence as devices 2+. For
1479 * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
1480 * and so on: the actual data containing partitions.
1482 * Return: 1 Success, @state->bdev is a dynamic disk and we handled it
1483 * 0 Success, @state->bdev is not a dynamic disk
1484 * -1 An error occurred before enough information had been read
1485 * Or @state->bdev is a dynamic disk, but it may be corrupted
1487 int ldm_partition(struct parsed_partitions *state)
1489 struct ldmdb *ldb;
1490 unsigned long base;
1491 int result = -1;
1493 BUG_ON(!state);
1495 /* Look for signs of a Dynamic Disk */
1496 if (!ldm_validate_partition_table(state))
1497 return 0;
1499 ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
1500 if (!ldb) {
1501 ldm_crit ("Out of memory.");
1502 goto out;
1505 /* Parse and check privheads. */
1506 if (!ldm_validate_privheads(state, &ldb->ph))
1507 goto out; /* Already logged */
1509 /* All further references are relative to base (database start). */
1510 base = ldb->ph.config_start;
1512 /* Parse and check tocs and vmdb. */
1513 if (!ldm_validate_tocblocks(state, base, ldb) ||
1514 !ldm_validate_vmdb(state, base, ldb))
1515 goto out; /* Already logged */
1517 /* Initialize vblk lists in ldmdb struct */
1518 INIT_LIST_HEAD (&ldb->v_dgrp);
1519 INIT_LIST_HEAD (&ldb->v_disk);
1520 INIT_LIST_HEAD (&ldb->v_volu);
1521 INIT_LIST_HEAD (&ldb->v_comp);
1522 INIT_LIST_HEAD (&ldb->v_part);
1524 if (!ldm_get_vblks(state, base, ldb)) {
1525 ldm_crit ("Failed to read the VBLKs from the database.");
1526 goto cleanup;
1529 /* Finally, create the data partition devices. */
1530 if (ldm_create_data_partitions(state, ldb)) {
1531 ldm_debug ("Parsed LDM database successfully.");
1532 result = 1;
1534 /* else Already logged */
1536 cleanup:
1537 ldm_free_vblks (&ldb->v_dgrp);
1538 ldm_free_vblks (&ldb->v_disk);
1539 ldm_free_vblks (&ldb->v_volu);
1540 ldm_free_vblks (&ldb->v_comp);
1541 ldm_free_vblks (&ldb->v_part);
1542 out:
1543 kfree (ldb);
1544 return result;