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