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