Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[deliverable/linux.git] / fs / partitions / ldm.c
1 /**
2 * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
3 *
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>
7 *
8 * Documentation is available at http://linux-ntfs.sf.net/ldm
9 *
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.
14 *
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.
19 *
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
24 */
25
26 #include <linux/slab.h>
27 #include <linux/pagemap.h>
28 #include <linux/stringify.h>
29 #include "ldm.h"
30 #include "check.h"
31 #include "msdos.h"
32
33 /**
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
37 *
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.
40 */
41 #ifndef CONFIG_LDM_DEBUG
42 #define ldm_debug(...) do {} while (0)
43 #else
44 #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __FUNCTION__, f, ##a)
45 #endif
46
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)
50
51 __attribute__ ((format (printf, 3, 4)))
52 static void _ldm_printk (const char *level, const char *function,
53 const char *fmt, ...)
54 {
55 static char buf[128];
56 va_list args;
57
58 va_start (args, fmt);
59 vsnprintf (buf, sizeof (buf), fmt, args);
60 va_end (args);
61
62 printk ("%s%s(): %s\n", level, function, buf);
63 }
64
65
66 /**
67 * ldm_parse_hexbyte - Convert a ASCII hex number to a byte
68 * @src: Pointer to at least 2 characters to convert.
69 *
70 * Convert a two character ASCII hex string to a number.
71 *
72 * Return: 0-255 Success, the byte was parsed correctly
73 * -1 Error, an invalid character was supplied
74 */
75 static int ldm_parse_hexbyte (const u8 *src)
76 {
77 unsigned int x; /* For correct wrapping */
78 int h;
79
80 /* high part */
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;
84 else return -1;
85 h <<= 4;
86
87 /* low part */
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);
91 return -1;
92 }
93
94 /**
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)
98 *
99 * N.B. The GUID need not be NULL terminated.
100 *
101 * Return: 'true' @dest contains binary GUID
102 * 'false' @dest contents are undefined
103 */
104 static bool ldm_parse_guid (const u8 *src, u8 *dest)
105 {
106 static const int size[] = { 4, 2, 2, 2, 6 };
107 int i, j, v;
108
109 if (src[8] != '-' || src[13] != '-' ||
110 src[18] != '-' || src[23] != '-')
111 return false;
112
113 for (j = 0; j < 5; j++, src++)
114 for (i = 0; i < size[j]; i++, src+=2, *dest++ = v)
115 if ((v = ldm_parse_hexbyte (src)) < 0)
116 return false;
117
118 return true;
119 }
120
121
122 /**
123 * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
124 * @data: Raw database PRIVHEAD structure loaded from the device
125 * @ph: In-memory privhead structure in which to return parsed information
126 *
127 * This parses the LDM database PRIVHEAD structure supplied in @data and
128 * sets up the in-memory privhead structure @ph with the obtained information.
129 *
130 * Return: 'true' @ph contains the PRIVHEAD data
131 * 'false' @ph contents are undefined
132 */
133 static bool ldm_parse_privhead (const u8 *data, struct privhead *ph)
134 {
135 BUG_ON (!data || !ph);
136
137 if (MAGIC_PRIVHEAD != BE64 (data)) {
138 ldm_error ("Cannot find PRIVHEAD structure. LDM database is"
139 " corrupt. Aborting.");
140 return false;
141 }
142
143 ph->ver_major = BE16 (data + 0x000C);
144 ph->ver_minor = BE16 (data + 0x000E);
145 ph->logical_disk_start = BE64 (data + 0x011B);
146 ph->logical_disk_size = BE64 (data + 0x0123);
147 ph->config_start = BE64 (data + 0x012B);
148 ph->config_size = BE64 (data + 0x0133);
149
150 if ((ph->ver_major != 2) || (ph->ver_minor != 11)) {
151 ldm_error ("Expected PRIVHEAD version %d.%d, got %d.%d."
152 " Aborting.", 2, 11, ph->ver_major, ph->ver_minor);
153 return false;
154 }
155 if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
156 /* Warn the user and continue, carefully */
157 ldm_info ("Database is normally %u bytes, it claims to "
158 "be %llu bytes.", LDM_DB_SIZE,
159 (unsigned long long)ph->config_size );
160 }
161 if ((ph->logical_disk_size == 0) ||
162 (ph->logical_disk_start + ph->logical_disk_size > ph->config_start)) {
163 ldm_error ("PRIVHEAD disk size doesn't match real disk size");
164 return false;
165 }
166
167 if (!ldm_parse_guid (data + 0x0030, ph->disk_id)) {
168 ldm_error ("PRIVHEAD contains an invalid GUID.");
169 return false;
170 }
171
172 ldm_debug ("Parsed PRIVHEAD successfully.");
173 return true;
174 }
175
176 /**
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
180 *
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.
184 *
185 * N.B. The *_start and *_size values returned in @toc are not range-checked.
186 *
187 * Return: 'true' @toc contains the TOCBLOCK data
188 * 'false' @toc contents are undefined
189 */
190 static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
191 {
192 BUG_ON (!data || !toc);
193
194 if (MAGIC_TOCBLOCK != BE64 (data)) {
195 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
196 return false;
197 }
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 = BE64 (data + 0x2E);
201 toc->bitmap1_size = BE64 (data + 0x36);
202
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;
208 }
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 = BE64 (data + 0x50);
212 toc->bitmap2_size = 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;
218 }
219 ldm_debug ("Parsed TOCBLOCK successfully.");
220 return true;
221 }
222
223 /**
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
227 *
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.
230 *
231 * N.B. The *_start, *_size and *_seq values will be range-checked later.
232 *
233 * Return: 'true' @vm contains VMDB info
234 * 'false' @vm contents are undefined
235 */
236 static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
237 {
238 BUG_ON (!data || !vm);
239
240 if (MAGIC_VMDB != BE32 (data)) {
241 ldm_crit ("Cannot find the VMDB, database may be corrupt.");
242 return false;
243 }
244
245 vm->ver_major = BE16 (data + 0x12);
246 vm->ver_minor = 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;
251 }
252
253 vm->vblk_size = BE32 (data + 0x08);
254 vm->vblk_offset = BE32 (data + 0x0C);
255 vm->last_vblk_seq = BE32 (data + 0x04);
256
257 ldm_debug ("Parsed VMDB successfully.");
258 return true;
259 }
260
261 /**
262 * ldm_compare_privheads - Compare two privhead objects
263 * @ph1: First privhead
264 * @ph2: Second privhead
265 *
266 * This compares the two privhead structures @ph1 and @ph2.
267 *
268 * Return: 'true' Identical
269 * 'false' Different
270 */
271 static bool ldm_compare_privheads (const struct privhead *ph1,
272 const struct privhead *ph2)
273 {
274 BUG_ON (!ph1 || !ph2);
275
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));
283 }
284
285 /**
286 * ldm_compare_tocblocks - Compare two tocblock objects
287 * @toc1: First toc
288 * @toc2: Second toc
289 *
290 * This compares the two tocblock structures @toc1 and @toc2.
291 *
292 * Return: 'true' Identical
293 * 'false' Different
294 */
295 static bool ldm_compare_tocblocks (const struct tocblock *toc1,
296 const struct tocblock *toc2)
297 {
298 BUG_ON (!toc1 || !toc2);
299
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)));
308 }
309
310 /**
311 * ldm_validate_privheads - Compare the primary privhead with its backups
312 * @bdev: Device holding the LDM Database
313 * @ph1: Memory struct to fill with ph contents
314 *
315 * Read and compare all three privheads from disk.
316 *
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.
320 *
321 * Return: 'true' Success
322 * 'false' Error
323 */
324 static bool ldm_validate_privheads (struct block_device *bdev,
325 struct privhead *ph1)
326 {
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;
334
335 BUG_ON (!bdev || !ph1);
336
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;
342 }
343
344 /* off[1 & 2] are relative to ph[0]->config_start */
345 ph[0]->config_start = 0;
346
347 /* Read and parse privheads */
348 for (i = 0; i < 3; i++) {
349 data = read_dev_sector (bdev,
350 ph[0]->config_start + off[i], &sect);
351 if (!data) {
352 ldm_crit ("Disk read failed.");
353 goto out;
354 }
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; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
363 }
364 }
365
366 num_sects = bdev->bd_inode->i_size >> 9;
367
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;
372 }
373
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;
379 }
380
381 if (!ldm_compare_privheads (ph[0], ph[1])) {
382 ldm_crit ("Primary and backup PRIVHEADs don't match.");
383 goto out;
384 }
385 /* FIXME ignore this for now
386 if (!ldm_compare_privheads (ph[0], ph[2])) {
387 ldm_crit ("Primary and backup PRIVHEADs don't match.");
388 goto out;
389 }*/
390 ldm_debug ("Validated PRIVHEADs successfully.");
391 result = true;
392 out:
393 kfree (ph[1]);
394 kfree (ph[2]);
395 return result;
396 }
397
398 /**
399 * ldm_validate_tocblocks - Validate the table of contents and its backups
400 * @bdev: Device holding the LDM Database
401 * @base: Offset, into @bdev, of the database
402 * @ldb: Cache of the database structures
403 *
404 * Find and compare the four tables of contents of the LDM Database stored on
405 * @bdev and return the parsed information into @toc1.
406 *
407 * The offsets and sizes of the configs are range-checked against a privhead.
408 *
409 * Return: 'true' @toc1 contains validated TOCBLOCK info
410 * 'false' @toc1 contents are undefined
411 */
412 static bool ldm_validate_tocblocks (struct block_device *bdev,
413 unsigned long base, struct ldmdb *ldb)
414 {
415 static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
416 struct tocblock *tb[4];
417 struct privhead *ph;
418 Sector sect;
419 u8 *data;
420 bool result = false;
421 int i;
422
423 BUG_ON (!bdev || !ldb);
424
425 ph = &ldb->ph;
426 tb[0] = &ldb->toc;
427 tb[1] = kmalloc (sizeof (*tb[1]), GFP_KERNEL);
428 tb[2] = kmalloc (sizeof (*tb[2]), GFP_KERNEL);
429 tb[3] = kmalloc (sizeof (*tb[3]), GFP_KERNEL);
430 if (!tb[1] || !tb[2] || !tb[3]) {
431 ldm_crit ("Out of memory.");
432 goto out;
433 }
434
435 for (i = 0; i < 4; i++) /* Read and parse all four toc's. */
436 {
437 data = read_dev_sector (bdev, base + off[i], &sect);
438 if (!data) {
439 ldm_crit ("Disk read failed.");
440 goto out;
441 }
442 result = ldm_parse_tocblock (data, tb[i]);
443 put_dev_sector (sect);
444 if (!result)
445 goto out; /* Already logged */
446 }
447
448 /* Range check the toc 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) > ph->config_size)) {
451 ldm_crit ("The bitmaps are out of range. Giving up.");
452 goto out;
453 }
454
455 if (!ldm_compare_tocblocks (tb[0], tb[1]) || /* Compare all tocs. */
456 !ldm_compare_tocblocks (tb[0], tb[2]) ||
457 !ldm_compare_tocblocks (tb[0], tb[3])) {
458 ldm_crit ("The TOCBLOCKs don't match.");
459 goto out;
460 }
461
462 ldm_debug ("Validated TOCBLOCKs successfully.");
463 result = true;
464 out:
465 kfree (tb[1]);
466 kfree (tb[2]);
467 kfree (tb[3]);
468 return result;
469 }
470
471 /**
472 * ldm_validate_vmdb - Read the VMDB and validate it
473 * @bdev: Device holding the LDM Database
474 * @base: Offset, into @bdev, of the database
475 * @ldb: Cache of the database structures
476 *
477 * Find the vmdb of the LDM Database stored on @bdev and return the parsed
478 * information in @ldb.
479 *
480 * Return: 'true' @ldb contains validated VBDB info
481 * 'false' @ldb contents are undefined
482 */
483 static bool ldm_validate_vmdb (struct block_device *bdev, unsigned long base,
484 struct ldmdb *ldb)
485 {
486 Sector sect;
487 u8 *data;
488 bool result = false;
489 struct vmdb *vm;
490 struct tocblock *toc;
491
492 BUG_ON (!bdev || !ldb);
493
494 vm = &ldb->vm;
495 toc = &ldb->toc;
496
497 data = read_dev_sector (bdev, base + OFF_VMDB, &sect);
498 if (!data) {
499 ldm_crit ("Disk read failed.");
500 return false;
501 }
502
503 if (!ldm_parse_vmdb (data, vm))
504 goto out; /* Already logged */
505
506 /* Are there uncommitted transactions? */
507 if (BE16(data + 0x10) != 0x01) {
508 ldm_crit ("Database is not in a consistent state. Aborting.");
509 goto out;
510 }
511
512 if (vm->vblk_offset != 512)
513 ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
514
515 /*
516 * The last_vblkd_seq can be before the end of the vmdb, just make sure
517 * it is not out of bounds.
518 */
519 if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
520 ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
521 "Database is corrupt. Aborting.");
522 goto out;
523 }
524
525 result = true;
526 out:
527 put_dev_sector (sect);
528 return result;
529 }
530
531
532 /**
533 * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
534 * @bdev: Device holding the LDM Database
535 *
536 * This function provides a weak test to decide whether the device is a dynamic
537 * disk or not. It looks for an MS-DOS-style partition table containing at
538 * least one partition of type 0x42 (formerly SFS, now used by Windows for
539 * dynamic disks).
540 *
541 * N.B. The only possible error can come from the read_dev_sector and that is
542 * only likely to happen if the underlying device is strange. If that IS
543 * the case we should return zero to let someone else try.
544 *
545 * Return: 'true' @bdev is a dynamic disk
546 * 'false' @bdev is not a dynamic disk, or an error occurred
547 */
548 static bool ldm_validate_partition_table (struct block_device *bdev)
549 {
550 Sector sect;
551 u8 *data;
552 struct partition *p;
553 int i;
554 bool result = false;
555
556 BUG_ON (!bdev);
557
558 data = read_dev_sector (bdev, 0, &sect);
559 if (!data) {
560 ldm_crit ("Disk read failed.");
561 return false;
562 }
563
564 if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
565 goto out;
566
567 p = (struct partition*)(data + 0x01BE);
568 for (i = 0; i < 4; i++, p++)
569 if (SYS_IND (p) == WIN2K_DYNAMIC_PARTITION) {
570 result = true;
571 break;
572 }
573
574 if (result)
575 ldm_debug ("Found W2K dynamic disk partition type.");
576
577 out:
578 put_dev_sector (sect);
579 return result;
580 }
581
582 /**
583 * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
584 * @ldb: Cache of the database structures
585 *
586 * The LDM Database contains a list of all partitions on all dynamic disks.
587 * The primary PRIVHEAD, at the beginning of the physical disk, tells us
588 * the GUID of this disk. This function searches for the GUID in a linked
589 * list of vblk's.
590 *
591 * Return: Pointer, A matching vblk was found
592 * NULL, No match, or an error
593 */
594 static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
595 {
596 struct list_head *item;
597
598 BUG_ON (!ldb);
599
600 list_for_each (item, &ldb->v_disk) {
601 struct vblk *v = list_entry (item, struct vblk, list);
602 if (!memcmp (v->vblk.disk.disk_id, ldb->ph.disk_id, GUID_SIZE))
603 return v;
604 }
605
606 return NULL;
607 }
608
609 /**
610 * ldm_create_data_partitions - Create data partitions for this device
611 * @pp: List of the partitions parsed so far
612 * @ldb: Cache of the database structures
613 *
614 * The database contains ALL the partitions for ALL disk groups, so we need to
615 * filter out this specific disk. Using the disk's object id, we can find all
616 * the partitions in the database that belong to this disk.
617 *
618 * Add each partition in our database, to the parsed_partitions structure.
619 *
620 * N.B. This function creates the partitions in the order it finds partition
621 * objects in the linked list.
622 *
623 * Return: 'true' Partition created
624 * 'false' Error, probably a range checking problem
625 */
626 static bool ldm_create_data_partitions (struct parsed_partitions *pp,
627 const struct ldmdb *ldb)
628 {
629 struct list_head *item;
630 struct vblk *vb;
631 struct vblk *disk;
632 struct vblk_part *part;
633 int part_num = 1;
634
635 BUG_ON (!pp || !ldb);
636
637 disk = ldm_get_disk_objid (ldb);
638 if (!disk) {
639 ldm_crit ("Can't find the ID of this disk in the database.");
640 return false;
641 }
642
643 printk (" [LDM]");
644
645 /* Create the data partitions */
646 list_for_each (item, &ldb->v_part) {
647 vb = list_entry (item, struct vblk, list);
648 part = &vb->vblk.part;
649
650 if (part->disk_id != disk->obj_id)
651 continue;
652
653 put_partition (pp, part_num, ldb->ph.logical_disk_start +
654 part->start, part->size);
655 part_num++;
656 }
657
658 printk ("\n");
659 return true;
660 }
661
662
663 /**
664 * ldm_relative - Calculate the next relative offset
665 * @buffer: Block of data being worked on
666 * @buflen: Size of the block of data
667 * @base: Size of the previous fixed width fields
668 * @offset: Cumulative size of the previous variable-width fields
669 *
670 * Because many of the VBLK fields are variable-width, it's necessary
671 * to calculate each offset based on the previous one and the length
672 * of the field it pointed to.
673 *
674 * Return: -1 Error, the calculated offset exceeded the size of the buffer
675 * n OK, a range-checked offset into buffer
676 */
677 static int ldm_relative (const u8 *buffer, int buflen, int base, int offset)
678 {
679
680 base += offset;
681 if ((!buffer) || (offset < 0) || (base > buflen))
682 return -1;
683 if ((base + buffer[base]) >= buflen)
684 return -1;
685
686 return buffer[base] + offset + 1;
687 }
688
689 /**
690 * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
691 * @block: Pointer to the variable-width number to convert
692 *
693 * Large numbers in the LDM Database are often stored in a packed format. Each
694 * number is prefixed by a one byte width marker. All numbers in the database
695 * are stored in big-endian byte order. This function reads one of these
696 * numbers and returns the result
697 *
698 * N.B. This function DOES NOT perform any range checking, though the most
699 * it will read is eight bytes.
700 *
701 * Return: n A number
702 * 0 Zero, or an error occurred
703 */
704 static u64 ldm_get_vnum (const u8 *block)
705 {
706 u64 tmp = 0;
707 u8 length;
708
709 BUG_ON (!block);
710
711 length = *block++;
712
713 if (length && length <= 8)
714 while (length--)
715 tmp = (tmp << 8) | *block++;
716 else
717 ldm_error ("Illegal length %d.", length);
718
719 return tmp;
720 }
721
722 /**
723 * ldm_get_vstr - Read a length-prefixed string into a buffer
724 * @block: Pointer to the length marker
725 * @buffer: Location to copy string to
726 * @buflen: Size of the output buffer
727 *
728 * Many of the strings in the LDM Database are not NULL terminated. Instead
729 * they are prefixed by a one byte length marker. This function copies one of
730 * these strings into a buffer.
731 *
732 * N.B. This function DOES NOT perform any range checking on the input.
733 * If the buffer is too small, the output will be truncated.
734 *
735 * Return: 0, Error and @buffer contents are undefined
736 * n, String length in characters (excluding NULL)
737 * buflen-1, String was truncated.
738 */
739 static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
740 {
741 int length;
742
743 BUG_ON (!block || !buffer);
744
745 length = block[0];
746 if (length >= buflen) {
747 ldm_error ("Truncating string %d -> %d.", length, buflen);
748 length = buflen - 1;
749 }
750 memcpy (buffer, block + 1, length);
751 buffer[length] = 0;
752 return length;
753 }
754
755
756 /**
757 * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
758 * @buffer: Block of data being worked on
759 * @buflen: Size of the block of data
760 * @vb: In-memory vblk in which to return information
761 *
762 * Read a raw VBLK Component object (version 3) into a vblk structure.
763 *
764 * Return: 'true' @vb contains a Component VBLK
765 * 'false' @vb contents are not defined
766 */
767 static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
768 {
769 int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
770 struct vblk_comp *comp;
771
772 BUG_ON (!buffer || !vb);
773
774 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
775 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
776 r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
777 r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
778 r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
779
780 if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
781 r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
782 r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
783 len = r_cols;
784 } else {
785 r_stripe = 0;
786 r_cols = 0;
787 len = r_parent;
788 }
789 if (len < 0)
790 return false;
791
792 len += VBLK_SIZE_CMP3;
793 if (len != BE32 (buffer + 0x14))
794 return false;
795
796 comp = &vb->vblk.comp;
797 ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
798 sizeof (comp->state));
799 comp->type = buffer[0x18 + r_vstate];
800 comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
801 comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
802 comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
803
804 return true;
805 }
806
807 /**
808 * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
809 * @buffer: Block of data being worked on
810 * @buflen: Size of the block of data
811 * @vb: In-memory vblk in which to return information
812 *
813 * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
814 *
815 * Return: 'true' @vb contains a Disk Group VBLK
816 * 'false' @vb contents are not defined
817 */
818 static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
819 {
820 int r_objid, r_name, r_diskid, r_id1, r_id2, len;
821 struct vblk_dgrp *dgrp;
822
823 BUG_ON (!buffer || !vb);
824
825 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
826 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
827 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
828
829 if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
830 r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
831 r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
832 len = r_id2;
833 } else {
834 r_id1 = 0;
835 r_id2 = 0;
836 len = r_diskid;
837 }
838 if (len < 0)
839 return false;
840
841 len += VBLK_SIZE_DGR3;
842 if (len != BE32 (buffer + 0x14))
843 return false;
844
845 dgrp = &vb->vblk.dgrp;
846 ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
847 sizeof (dgrp->disk_id));
848 return true;
849 }
850
851 /**
852 * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
853 * @buffer: Block of data being worked on
854 * @buflen: Size of the block of data
855 * @vb: In-memory vblk in which to return information
856 *
857 * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
858 *
859 * Return: 'true' @vb contains a Disk Group VBLK
860 * 'false' @vb contents are not defined
861 */
862 static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
863 {
864 char buf[64];
865 int r_objid, r_name, r_id1, r_id2, len;
866 struct vblk_dgrp *dgrp;
867
868 BUG_ON (!buffer || !vb);
869
870 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
871 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
872
873 if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
874 r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
875 r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
876 len = r_id2;
877 } else {
878 r_id1 = 0;
879 r_id2 = 0;
880 len = r_name;
881 }
882 if (len < 0)
883 return false;
884
885 len += VBLK_SIZE_DGR4;
886 if (len != BE32 (buffer + 0x14))
887 return false;
888
889 dgrp = &vb->vblk.dgrp;
890
891 ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
892 return true;
893 }
894
895 /**
896 * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
897 * @buffer: Block of data being worked on
898 * @buflen: Size of the block of data
899 * @vb: In-memory vblk in which to return information
900 *
901 * Read a raw VBLK Disk object (version 3) into a vblk structure.
902 *
903 * Return: 'true' @vb contains a Disk VBLK
904 * 'false' @vb contents are not defined
905 */
906 static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
907 {
908 int r_objid, r_name, r_diskid, r_altname, len;
909 struct vblk_disk *disk;
910
911 BUG_ON (!buffer || !vb);
912
913 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
914 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
915 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
916 r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
917 len = r_altname;
918 if (len < 0)
919 return false;
920
921 len += VBLK_SIZE_DSK3;
922 if (len != BE32 (buffer + 0x14))
923 return false;
924
925 disk = &vb->vblk.disk;
926 ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
927 sizeof (disk->alt_name));
928 if (!ldm_parse_guid (buffer + 0x19 + r_name, disk->disk_id))
929 return false;
930
931 return true;
932 }
933
934 /**
935 * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
936 * @buffer: Block of data being worked on
937 * @buflen: Size of the block of data
938 * @vb: In-memory vblk in which to return information
939 *
940 * Read a raw VBLK Disk object (version 4) into a vblk structure.
941 *
942 * Return: 'true' @vb contains a Disk VBLK
943 * 'false' @vb contents are not defined
944 */
945 static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
946 {
947 int r_objid, r_name, len;
948 struct vblk_disk *disk;
949
950 BUG_ON (!buffer || !vb);
951
952 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
953 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
954 len = r_name;
955 if (len < 0)
956 return false;
957
958 len += VBLK_SIZE_DSK4;
959 if (len != BE32 (buffer + 0x14))
960 return false;
961
962 disk = &vb->vblk.disk;
963 memcpy (disk->disk_id, buffer + 0x18 + r_name, GUID_SIZE);
964 return true;
965 }
966
967 /**
968 * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
969 * @buffer: Block of data being worked on
970 * @buflen: Size of the block of data
971 * @vb: In-memory vblk in which to return information
972 *
973 * Read a raw VBLK Partition object (version 3) into a vblk structure.
974 *
975 * Return: 'true' @vb contains a Partition VBLK
976 * 'false' @vb contents are not defined
977 */
978 static bool ldm_parse_prt3 (const u8 *buffer, int buflen, struct vblk *vb)
979 {
980 int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
981 struct vblk_part *part;
982
983 BUG_ON (!buffer || !vb);
984
985 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
986 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
987 r_size = ldm_relative (buffer, buflen, 0x34, r_name);
988 r_parent = ldm_relative (buffer, buflen, 0x34, r_size);
989 r_diskid = ldm_relative (buffer, buflen, 0x34, r_parent);
990
991 if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
992 r_index = ldm_relative (buffer, buflen, 0x34, r_diskid);
993 len = r_index;
994 } else {
995 r_index = 0;
996 len = r_diskid;
997 }
998 if (len < 0)
999 return false;
1000
1001 len += VBLK_SIZE_PRT3;
1002 if (len != BE32 (buffer + 0x14))
1003 return false;
1004
1005 part = &vb->vblk.part;
1006 part->start = BE64 (buffer + 0x24 + r_name);
1007 part->volume_offset = BE64 (buffer + 0x2C + r_name);
1008 part->size = ldm_get_vnum (buffer + 0x34 + r_name);
1009 part->parent_id = ldm_get_vnum (buffer + 0x34 + r_size);
1010 part->disk_id = ldm_get_vnum (buffer + 0x34 + r_parent);
1011 if (vb->flags & VBLK_FLAG_PART_INDEX)
1012 part->partnum = buffer[0x35 + r_diskid];
1013 else
1014 part->partnum = 0;
1015
1016 return true;
1017 }
1018
1019 /**
1020 * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
1021 * @buffer: Block of data being worked on
1022 * @buflen: Size of the block of data
1023 * @vb: In-memory vblk in which to return information
1024 *
1025 * Read a raw VBLK Volume object (version 5) into a vblk structure.
1026 *
1027 * Return: 'true' @vb contains a Volume VBLK
1028 * 'false' @vb contents are not defined
1029 */
1030 static bool ldm_parse_vol5 (const u8 *buffer, int buflen, struct vblk *vb)
1031 {
1032 int r_objid, r_name, r_vtype, r_child, r_size, r_id1, r_id2, r_size2;
1033 int r_drive, len;
1034 struct vblk_volu *volu;
1035
1036 BUG_ON (!buffer || !vb);
1037
1038 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
1039 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
1040 r_vtype = ldm_relative (buffer, buflen, 0x18, r_name);
1041 r_child = ldm_relative (buffer, buflen, 0x2E, r_vtype);
1042 r_size = ldm_relative (buffer, buflen, 0x3E, r_child);
1043
1044 if (buffer[0x12] & VBLK_FLAG_VOLU_ID1)
1045 r_id1 = ldm_relative (buffer, buflen, 0x53, r_size);
1046 else
1047 r_id1 = r_size;
1048
1049 if (buffer[0x12] & VBLK_FLAG_VOLU_ID2)
1050 r_id2 = ldm_relative (buffer, buflen, 0x53, r_id1);
1051 else
1052 r_id2 = r_id1;
1053
1054 if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE)
1055 r_size2 = ldm_relative (buffer, buflen, 0x53, r_id2);
1056 else
1057 r_size2 = r_id2;
1058
1059 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE)
1060 r_drive = ldm_relative (buffer, buflen, 0x53, r_size2);
1061 else
1062 r_drive = r_size2;
1063
1064 len = r_drive;
1065 if (len < 0)
1066 return false;
1067
1068 len += VBLK_SIZE_VOL5;
1069 if (len != BE32 (buffer + 0x14))
1070 return false;
1071
1072 volu = &vb->vblk.volu;
1073
1074 ldm_get_vstr (buffer + 0x18 + r_name, volu->volume_type,
1075 sizeof (volu->volume_type));
1076 memcpy (volu->volume_state, buffer + 0x19 + r_vtype,
1077 sizeof (volu->volume_state));
1078 volu->size = ldm_get_vnum (buffer + 0x3E + r_child);
1079 volu->partition_type = buffer[0x42 + r_size];
1080 memcpy (volu->guid, buffer + 0x43 + r_size, sizeof (volu->guid));
1081 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1082 ldm_get_vstr (buffer + 0x53 + r_size, volu->drive_hint,
1083 sizeof (volu->drive_hint));
1084 }
1085 return true;
1086 }
1087
1088 /**
1089 * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
1090 * @buf: Block of data being worked on
1091 * @len: Size of the block of data
1092 * @vb: In-memory vblk in which to return information
1093 *
1094 * Read a raw VBLK object into a vblk structure. This function just reads the
1095 * information common to all VBLK types, then delegates the rest of the work to
1096 * helper functions: ldm_parse_*.
1097 *
1098 * Return: 'true' @vb contains a VBLK
1099 * 'false' @vb contents are not defined
1100 */
1101 static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
1102 {
1103 bool result = false;
1104 int r_objid;
1105
1106 BUG_ON (!buf || !vb);
1107
1108 r_objid = ldm_relative (buf, len, 0x18, 0);
1109 if (r_objid < 0) {
1110 ldm_error ("VBLK header is corrupt.");
1111 return false;
1112 }
1113
1114 vb->flags = buf[0x12];
1115 vb->type = buf[0x13];
1116 vb->obj_id = ldm_get_vnum (buf + 0x18);
1117 ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
1118
1119 switch (vb->type) {
1120 case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
1121 case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
1122 case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
1123 case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
1124 case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
1125 case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
1126 case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
1127 }
1128
1129 if (result)
1130 ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
1131 (unsigned long long) vb->obj_id, vb->type);
1132 else
1133 ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
1134 (unsigned long long) vb->obj_id, vb->type);
1135
1136 return result;
1137 }
1138
1139
1140 /**
1141 * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
1142 * @data: Raw VBLK to add to the database
1143 * @len: Size of the raw VBLK
1144 * @ldb: Cache of the database structures
1145 *
1146 * The VBLKs are sorted into categories. Partitions are also sorted by offset.
1147 *
1148 * N.B. This function does not check the validity of the VBLKs.
1149 *
1150 * Return: 'true' The VBLK was added
1151 * 'false' An error occurred
1152 */
1153 static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
1154 {
1155 struct vblk *vb;
1156 struct list_head *item;
1157
1158 BUG_ON (!data || !ldb);
1159
1160 vb = kmalloc (sizeof (*vb), GFP_KERNEL);
1161 if (!vb) {
1162 ldm_crit ("Out of memory.");
1163 return false;
1164 }
1165
1166 if (!ldm_parse_vblk (data, len, vb)) {
1167 kfree(vb);
1168 return false; /* Already logged */
1169 }
1170
1171 /* Put vblk into the correct list. */
1172 switch (vb->type) {
1173 case VBLK_DGR3:
1174 case VBLK_DGR4:
1175 list_add (&vb->list, &ldb->v_dgrp);
1176 break;
1177 case VBLK_DSK3:
1178 case VBLK_DSK4:
1179 list_add (&vb->list, &ldb->v_disk);
1180 break;
1181 case VBLK_VOL5:
1182 list_add (&vb->list, &ldb->v_volu);
1183 break;
1184 case VBLK_CMP3:
1185 list_add (&vb->list, &ldb->v_comp);
1186 break;
1187 case VBLK_PRT3:
1188 /* Sort by the partition's start sector. */
1189 list_for_each (item, &ldb->v_part) {
1190 struct vblk *v = list_entry (item, struct vblk, list);
1191 if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
1192 (v->vblk.part.start > vb->vblk.part.start)) {
1193 list_add_tail (&vb->list, &v->list);
1194 return true;
1195 }
1196 }
1197 list_add_tail (&vb->list, &ldb->v_part);
1198 break;
1199 }
1200 return true;
1201 }
1202
1203 /**
1204 * ldm_frag_add - Add a VBLK fragment to a list
1205 * @data: Raw fragment to be added to the list
1206 * @size: Size of the raw fragment
1207 * @frags: Linked list of VBLK fragments
1208 *
1209 * Fragmented VBLKs may not be consecutive in the database, so they are placed
1210 * in a list so they can be pieced together later.
1211 *
1212 * Return: 'true' Success, the VBLK was added to the list
1213 * 'false' Error, a problem occurred
1214 */
1215 static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
1216 {
1217 struct frag *f;
1218 struct list_head *item;
1219 int rec, num, group;
1220
1221 BUG_ON (!data || !frags);
1222
1223 group = BE32 (data + 0x08);
1224 rec = BE16 (data + 0x0C);
1225 num = BE16 (data + 0x0E);
1226 if ((num < 1) || (num > 4)) {
1227 ldm_error ("A VBLK claims to have %d parts.", num);
1228 return false;
1229 }
1230
1231 list_for_each (item, frags) {
1232 f = list_entry (item, struct frag, list);
1233 if (f->group == group)
1234 goto found;
1235 }
1236
1237 f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
1238 if (!f) {
1239 ldm_crit ("Out of memory.");
1240 return false;
1241 }
1242
1243 f->group = group;
1244 f->num = num;
1245 f->rec = rec;
1246 f->map = 0xFF << num;
1247
1248 list_add_tail (&f->list, frags);
1249 found:
1250 if (f->map & (1 << rec)) {
1251 ldm_error ("Duplicate VBLK, part %d.", rec);
1252 f->map &= 0x7F; /* Mark the group as broken */
1253 return false;
1254 }
1255
1256 f->map |= (1 << rec);
1257
1258 if (num > 0) {
1259 data += VBLK_SIZE_HEAD;
1260 size -= VBLK_SIZE_HEAD;
1261 }
1262 memcpy (f->data+rec*(size-VBLK_SIZE_HEAD)+VBLK_SIZE_HEAD, data, size);
1263
1264 return true;
1265 }
1266
1267 /**
1268 * ldm_frag_free - Free a linked list of VBLK fragments
1269 * @list: Linked list of fragments
1270 *
1271 * Free a linked list of VBLK fragments
1272 *
1273 * Return: none
1274 */
1275 static void ldm_frag_free (struct list_head *list)
1276 {
1277 struct list_head *item, *tmp;
1278
1279 BUG_ON (!list);
1280
1281 list_for_each_safe (item, tmp, list)
1282 kfree (list_entry (item, struct frag, list));
1283 }
1284
1285 /**
1286 * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
1287 * @frags: Linked list of VBLK fragments
1288 * @ldb: Cache of the database structures
1289 *
1290 * Now that all the fragmented VBLKs have been collected, they must be added to
1291 * the database for later use.
1292 *
1293 * Return: 'true' All the fragments we added successfully
1294 * 'false' One or more of the fragments we invalid
1295 */
1296 static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
1297 {
1298 struct frag *f;
1299 struct list_head *item;
1300
1301 BUG_ON (!frags || !ldb);
1302
1303 list_for_each (item, frags) {
1304 f = list_entry (item, struct frag, list);
1305
1306 if (f->map != 0xFF) {
1307 ldm_error ("VBLK group %d is incomplete (0x%02x).",
1308 f->group, f->map);
1309 return false;
1310 }
1311
1312 if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
1313 return false; /* Already logged */
1314 }
1315 return true;
1316 }
1317
1318 /**
1319 * ldm_get_vblks - Read the on-disk database of VBLKs into memory
1320 * @bdev: Device holding the LDM Database
1321 * @base: Offset, into @bdev, of the database
1322 * @ldb: Cache of the database structures
1323 *
1324 * To use the information from the VBLKs, they need to be read from the disk,
1325 * unpacked and validated. We cache them in @ldb according to their type.
1326 *
1327 * Return: 'true' All the VBLKs were read successfully
1328 * 'false' An error occurred
1329 */
1330 static bool ldm_get_vblks (struct block_device *bdev, unsigned long base,
1331 struct ldmdb *ldb)
1332 {
1333 int size, perbuf, skip, finish, s, v, recs;
1334 u8 *data = NULL;
1335 Sector sect;
1336 bool result = false;
1337 LIST_HEAD (frags);
1338
1339 BUG_ON (!bdev || !ldb);
1340
1341 size = ldb->vm.vblk_size;
1342 perbuf = 512 / size;
1343 skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
1344 finish = (size * ldb->vm.last_vblk_seq) >> 9;
1345
1346 for (s = skip; s < finish; s++) { /* For each sector */
1347 data = read_dev_sector (bdev, base + OFF_VMDB + s, &sect);
1348 if (!data) {
1349 ldm_crit ("Disk read failed.");
1350 goto out;
1351 }
1352
1353 for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
1354 if (MAGIC_VBLK != BE32 (data)) {
1355 ldm_error ("Expected to find a VBLK.");
1356 goto out;
1357 }
1358
1359 recs = BE16 (data + 0x0E); /* Number of records */
1360 if (recs == 1) {
1361 if (!ldm_ldmdb_add (data, size, ldb))
1362 goto out; /* Already logged */
1363 } else if (recs > 1) {
1364 if (!ldm_frag_add (data, size, &frags))
1365 goto out; /* Already logged */
1366 }
1367 /* else Record is not in use, ignore it. */
1368 }
1369 put_dev_sector (sect);
1370 data = NULL;
1371 }
1372
1373 result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
1374 out:
1375 if (data)
1376 put_dev_sector (sect);
1377 ldm_frag_free (&frags);
1378
1379 return result;
1380 }
1381
1382 /**
1383 * ldm_free_vblks - Free a linked list of vblk's
1384 * @lh: Head of a linked list of struct vblk
1385 *
1386 * Free a list of vblk's and free the memory used to maintain the list.
1387 *
1388 * Return: none
1389 */
1390 static void ldm_free_vblks (struct list_head *lh)
1391 {
1392 struct list_head *item, *tmp;
1393
1394 BUG_ON (!lh);
1395
1396 list_for_each_safe (item, tmp, lh)
1397 kfree (list_entry (item, struct vblk, list));
1398 }
1399
1400
1401 /**
1402 * ldm_partition - Find out whether a device is a dynamic disk and handle it
1403 * @pp: List of the partitions parsed so far
1404 * @bdev: Device holding the LDM Database
1405 *
1406 * This determines whether the device @bdev is a dynamic disk and if so creates
1407 * the partitions necessary in the gendisk structure pointed to by @hd.
1408 *
1409 * We create a dummy device 1, which contains the LDM database, and then create
1410 * each partition described by the LDM database in sequence as devices 2+. For
1411 * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
1412 * and so on: the actual data containing partitions.
1413 *
1414 * Return: 1 Success, @bdev is a dynamic disk and we handled it
1415 * 0 Success, @bdev is not a dynamic disk
1416 * -1 An error occurred before enough information had been read
1417 * Or @bdev is a dynamic disk, but it may be corrupted
1418 */
1419 int ldm_partition (struct parsed_partitions *pp, struct block_device *bdev)
1420 {
1421 struct ldmdb *ldb;
1422 unsigned long base;
1423 int result = -1;
1424
1425 BUG_ON (!pp || !bdev);
1426
1427 /* Look for signs of a Dynamic Disk */
1428 if (!ldm_validate_partition_table (bdev))
1429 return 0;
1430
1431 ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
1432 if (!ldb) {
1433 ldm_crit ("Out of memory.");
1434 goto out;
1435 }
1436
1437 /* Parse and check privheads. */
1438 if (!ldm_validate_privheads (bdev, &ldb->ph))
1439 goto out; /* Already logged */
1440
1441 /* All further references are relative to base (database start). */
1442 base = ldb->ph.config_start;
1443
1444 /* Parse and check tocs and vmdb. */
1445 if (!ldm_validate_tocblocks (bdev, base, ldb) ||
1446 !ldm_validate_vmdb (bdev, base, ldb))
1447 goto out; /* Already logged */
1448
1449 /* Initialize vblk lists in ldmdb struct */
1450 INIT_LIST_HEAD (&ldb->v_dgrp);
1451 INIT_LIST_HEAD (&ldb->v_disk);
1452 INIT_LIST_HEAD (&ldb->v_volu);
1453 INIT_LIST_HEAD (&ldb->v_comp);
1454 INIT_LIST_HEAD (&ldb->v_part);
1455
1456 if (!ldm_get_vblks (bdev, base, ldb)) {
1457 ldm_crit ("Failed to read the VBLKs from the database.");
1458 goto cleanup;
1459 }
1460
1461 /* Finally, create the data partition devices. */
1462 if (ldm_create_data_partitions (pp, ldb)) {
1463 ldm_debug ("Parsed LDM database successfully.");
1464 result = 1;
1465 }
1466 /* else Already logged */
1467
1468 cleanup:
1469 ldm_free_vblks (&ldb->v_dgrp);
1470 ldm_free_vblks (&ldb->v_disk);
1471 ldm_free_vblks (&ldb->v_volu);
1472 ldm_free_vblks (&ldb->v_comp);
1473 ldm_free_vblks (&ldb->v_part);
1474 out:
1475 kfree (ldb);
1476 return result;
1477 }
1478
This page took 0.089424 seconds and 6 git commands to generate.