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> |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 5 | * Copyright (c) 2001-2007 Anton Altaparmakov |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com> |
| 7 | * |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 8 | * Documentation is available at http://www.linux-ntfs.org/content/view/19/37/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 44 | #define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 47 | #define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a) |
| 48 | #define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a) |
| 49 | #define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 100 | * Return: 'true' @dest contains binary GUID |
| 101 | * 'false' @dest contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 103 | static bool ldm_parse_guid (const u8 *src, u8 *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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] != '-') |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 110 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 115 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 117 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 128 | * Return: 'true' @ph contains the PRIVHEAD data |
| 129 | * 'false' @ph contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | */ |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 131 | static bool ldm_parse_privhead(const u8 *data, struct privhead *ph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 133 | bool is_vista = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 135 | BUG_ON(!data || !ph); |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 136 | if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) { |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 137 | ldm_error("Cannot find PRIVHEAD structure. LDM database is" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | " corrupt. Aborting."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 139 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 141 | ph->ver_major = get_unaligned_be16(data + 0x000C); |
| 142 | ph->ver_minor = get_unaligned_be16(data + 0x000E); |
| 143 | ph->logical_disk_start = get_unaligned_be64(data + 0x011B); |
| 144 | ph->logical_disk_size = get_unaligned_be64(data + 0x0123); |
| 145 | ph->config_start = get_unaligned_be64(data + 0x012B); |
| 146 | ph->config_size = get_unaligned_be64(data + 0x0133); |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 153 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 155 | ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major, |
| 156 | ph->ver_minor, is_vista ? "Vista" : "2000/XP"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */ |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 158 | /* Warn the user and continue, carefully. */ |
| 159 | ldm_info("Database is normally %u bytes, it claims to " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | "be %llu bytes.", LDM_DB_SIZE, |
Jeff Garzik | 72dd9ca | 2007-05-22 00:28:11 -0400 | [diff] [blame] | 161 | (unsigned long long)ph->config_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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"); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 166 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 168 | if (!ldm_parse_guid(data + 0x0030, ph->disk_id)) { |
| 169 | ldm_error("PRIVHEAD contains an invalid GUID."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 170 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 172 | ldm_debug("Parsed PRIVHEAD successfully."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 173 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 187 | * Return: 'true' @toc contains the TOCBLOCK data |
| 188 | * 'false' @toc contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 190 | static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | BUG_ON (!data || !toc); |
| 193 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 194 | if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | ldm_crit ("Cannot find TOCBLOCK, database may be corrupt."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 196 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name)); |
| 199 | toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 200 | toc->bitmap1_start = get_unaligned_be64(data + 0x2E); |
| 201 | toc->bitmap1_size = get_unaligned_be64(data + 0x36); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 207 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name)); |
| 210 | toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 211 | toc->bitmap2_start = get_unaligned_be64(data + 0x50); |
| 212 | toc->bitmap2_size = get_unaligned_be64(data + 0x58); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 217 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | ldm_debug ("Parsed TOCBLOCK successfully."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 220 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 233 | * Return: 'true' @vm contains VMDB info |
| 234 | * 'false' @vm contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 236 | static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | BUG_ON (!data || !vm); |
| 239 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 240 | if (MAGIC_VMDB != get_unaligned_be32(data)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | ldm_crit ("Cannot find the VMDB, database may be corrupt."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 242 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 245 | vm->ver_major = get_unaligned_be16(data + 0x12); |
| 246 | vm->ver_minor = get_unaligned_be16(data + 0x14); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 250 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 253 | vm->vblk_size = get_unaligned_be32(data + 0x08); |
| 254 | vm->vblk_offset = get_unaligned_be32(data + 0x0C); |
| 255 | vm->last_vblk_seq = get_unaligned_be32(data + 0x04); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | ldm_debug ("Parsed VMDB successfully."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 258 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 268 | * Return: 'true' Identical |
| 269 | * 'false' Different |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 271 | static bool ldm_compare_privheads (const struct privhead *ph1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 292 | * Return: 'true' Identical |
| 293 | * 'false' Different |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 295 | static bool ldm_compare_tocblocks (const struct tocblock *toc1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 321 | * Return: 'true' Success |
| 322 | * 'false' Error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 324 | static bool ldm_validate_privheads (struct block_device *bdev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 331 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 391 | result = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 409 | * Return: 'true' @toc1 contains validated TOCBLOCK info |
| 410 | * 'false' @toc1 contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | */ |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 412 | static bool ldm_validate_tocblocks(struct block_device *bdev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 420 | int i, nr_tbs; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 421 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 423 | BUG_ON(!bdev || !ldb); |
| 424 | ph = &ldb->ph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | tb[0] = &ldb->toc; |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 426 | tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL); |
| 427 | if (!tb[1]) { |
| 428 | ldm_crit("Out of memory."); |
| 429 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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], §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (!data) { |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 442 | ldm_error("Disk read failed for TOCBLOCK %d.", i); |
| 443 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 445 | if (ldm_parse_tocblock(data, tb[nr_tbs])) |
| 446 | nr_tbs++; |
| 447 | put_dev_sector(sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) || |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 467 | ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 468 | result = true; |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 469 | err: |
| 470 | kfree(tb[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 483 | * Return: 'true' @ldb contains validated VBDB info |
| 484 | * 'false' @ldb contents are undefined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 486 | static bool ldm_validate_vmdb (struct block_device *bdev, unsigned long base, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | struct ldmdb *ldb) |
| 488 | { |
| 489 | Sector sect; |
| 490 | u8 *data; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 491 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 503 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (!ldm_parse_vmdb (data, vm)) |
| 507 | goto out; /* Already logged */ |
| 508 | |
| 509 | /* Are there uncommitted transactions? */ |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 510 | if (get_unaligned_be16(data + 0x10) != 0x01) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 528 | result = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 548 | * Return: 'true' @bdev is a dynamic disk |
| 549 | * 'false' @bdev is not a dynamic disk, or an error occurred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 551 | static bool ldm_validate_partition_table (struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | Sector sect; |
| 554 | u8 *data; |
| 555 | struct partition *p; |
| 556 | int i; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 557 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | BUG_ON (!bdev); |
| 560 | |
| 561 | data = read_dev_sector (bdev, 0, §); |
| 562 | if (!data) { |
| 563 | ldm_crit ("Disk read failed."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 564 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 572 | if (SYS_IND (p) == LDM_PARTITION) { |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 573 | result = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 626 | * Return: 'true' Partition created |
| 627 | * 'false' Error, probably a range checking problem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 629 | static bool ldm_create_data_partitions (struct parsed_partitions *pp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 643 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 662 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 680 | static int ldm_relative(const u8 *buffer, int buflen, int base, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
| 682 | |
| 683 | base += offset; |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 684 | if (!buffer || offset < 0 || base > buflen) { |
| 685 | if (!buffer) |
| 686 | ldm_error("!buffer"); |
| 687 | if (offset < 0) |
| 688 | ldm_error("offset (%d) < 0", offset); |
| 689 | if (base > buflen) |
| 690 | ldm_error("base (%d) > buflen (%d)", base, buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return -1; |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 692 | } |
| 693 | if (base + buffer[base] >= buflen) { |
| 694 | ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base, |
| 695 | buffer[base], buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return -1; |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 697 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | return buffer[base] + offset + 1; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order |
| 703 | * @block: Pointer to the variable-width number to convert |
| 704 | * |
| 705 | * Large numbers in the LDM Database are often stored in a packed format. Each |
| 706 | * number is prefixed by a one byte width marker. All numbers in the database |
| 707 | * are stored in big-endian byte order. This function reads one of these |
| 708 | * numbers and returns the result |
| 709 | * |
| 710 | * N.B. This function DOES NOT perform any range checking, though the most |
| 711 | * it will read is eight bytes. |
| 712 | * |
| 713 | * Return: n A number |
| 714 | * 0 Zero, or an error occurred |
| 715 | */ |
| 716 | static u64 ldm_get_vnum (const u8 *block) |
| 717 | { |
| 718 | u64 tmp = 0; |
| 719 | u8 length; |
| 720 | |
| 721 | BUG_ON (!block); |
| 722 | |
| 723 | length = *block++; |
| 724 | |
| 725 | if (length && length <= 8) |
| 726 | while (length--) |
| 727 | tmp = (tmp << 8) | *block++; |
| 728 | else |
| 729 | ldm_error ("Illegal length %d.", length); |
| 730 | |
| 731 | return tmp; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * ldm_get_vstr - Read a length-prefixed string into a buffer |
| 736 | * @block: Pointer to the length marker |
| 737 | * @buffer: Location to copy string to |
| 738 | * @buflen: Size of the output buffer |
| 739 | * |
| 740 | * Many of the strings in the LDM Database are not NULL terminated. Instead |
| 741 | * they are prefixed by a one byte length marker. This function copies one of |
| 742 | * these strings into a buffer. |
| 743 | * |
| 744 | * N.B. This function DOES NOT perform any range checking on the input. |
| 745 | * If the buffer is too small, the output will be truncated. |
| 746 | * |
| 747 | * Return: 0, Error and @buffer contents are undefined |
| 748 | * n, String length in characters (excluding NULL) |
| 749 | * buflen-1, String was truncated. |
| 750 | */ |
| 751 | static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen) |
| 752 | { |
| 753 | int length; |
| 754 | |
| 755 | BUG_ON (!block || !buffer); |
| 756 | |
| 757 | length = block[0]; |
| 758 | if (length >= buflen) { |
| 759 | ldm_error ("Truncating string %d -> %d.", length, buflen); |
| 760 | length = buflen - 1; |
| 761 | } |
| 762 | memcpy (buffer, block + 1, length); |
| 763 | buffer[length] = 0; |
| 764 | return length; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /** |
| 769 | * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure |
| 770 | * @buffer: Block of data being worked on |
| 771 | * @buflen: Size of the block of data |
| 772 | * @vb: In-memory vblk in which to return information |
| 773 | * |
| 774 | * Read a raw VBLK Component object (version 3) into a vblk structure. |
| 775 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 776 | * Return: 'true' @vb contains a Component VBLK |
| 777 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 779 | static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
| 781 | int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len; |
| 782 | struct vblk_comp *comp; |
| 783 | |
| 784 | BUG_ON (!buffer || !vb); |
| 785 | |
| 786 | r_objid = ldm_relative (buffer, buflen, 0x18, 0); |
| 787 | r_name = ldm_relative (buffer, buflen, 0x18, r_objid); |
| 788 | r_vstate = ldm_relative (buffer, buflen, 0x18, r_name); |
| 789 | r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate); |
| 790 | r_parent = ldm_relative (buffer, buflen, 0x2D, r_child); |
| 791 | |
| 792 | if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) { |
| 793 | r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent); |
| 794 | r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe); |
| 795 | len = r_cols; |
| 796 | } else { |
| 797 | r_stripe = 0; |
| 798 | r_cols = 0; |
| 799 | len = r_parent; |
| 800 | } |
| 801 | if (len < 0) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 802 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | len += VBLK_SIZE_CMP3; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 805 | if (len != get_unaligned_be32(buffer + 0x14)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 806 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
| 808 | comp = &vb->vblk.comp; |
| 809 | ldm_get_vstr (buffer + 0x18 + r_name, comp->state, |
| 810 | sizeof (comp->state)); |
| 811 | comp->type = buffer[0x18 + r_vstate]; |
| 812 | comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate); |
| 813 | comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child); |
| 814 | comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0; |
| 815 | |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 816 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
| 820 | * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure |
| 821 | * @buffer: Block of data being worked on |
| 822 | * @buflen: Size of the block of data |
| 823 | * @vb: In-memory vblk in which to return information |
| 824 | * |
| 825 | * Read a raw VBLK Disk Group object (version 3) into a vblk structure. |
| 826 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 827 | * Return: 'true' @vb contains a Disk Group VBLK |
| 828 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | */ |
| 830 | static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb) |
| 831 | { |
| 832 | int r_objid, r_name, r_diskid, r_id1, r_id2, len; |
| 833 | struct vblk_dgrp *dgrp; |
| 834 | |
| 835 | BUG_ON (!buffer || !vb); |
| 836 | |
| 837 | r_objid = ldm_relative (buffer, buflen, 0x18, 0); |
| 838 | r_name = ldm_relative (buffer, buflen, 0x18, r_objid); |
| 839 | r_diskid = ldm_relative (buffer, buflen, 0x18, r_name); |
| 840 | |
| 841 | if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) { |
| 842 | r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid); |
| 843 | r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1); |
| 844 | len = r_id2; |
| 845 | } else { |
| 846 | r_id1 = 0; |
| 847 | r_id2 = 0; |
| 848 | len = r_diskid; |
| 849 | } |
| 850 | if (len < 0) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 851 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
| 853 | len += VBLK_SIZE_DGR3; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 854 | if (len != get_unaligned_be32(buffer + 0x14)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 855 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | dgrp = &vb->vblk.dgrp; |
| 858 | ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id, |
| 859 | sizeof (dgrp->disk_id)); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 860 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
| 864 | * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure |
| 865 | * @buffer: Block of data being worked on |
| 866 | * @buflen: Size of the block of data |
| 867 | * @vb: In-memory vblk in which to return information |
| 868 | * |
| 869 | * Read a raw VBLK Disk Group object (version 4) into a vblk structure. |
| 870 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 871 | * Return: 'true' @vb contains a Disk Group VBLK |
| 872 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 874 | static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
| 876 | char buf[64]; |
| 877 | int r_objid, r_name, r_id1, r_id2, len; |
| 878 | struct vblk_dgrp *dgrp; |
| 879 | |
| 880 | BUG_ON (!buffer || !vb); |
| 881 | |
| 882 | r_objid = ldm_relative (buffer, buflen, 0x18, 0); |
| 883 | r_name = ldm_relative (buffer, buflen, 0x18, r_objid); |
| 884 | |
| 885 | if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) { |
| 886 | r_id1 = ldm_relative (buffer, buflen, 0x44, r_name); |
| 887 | r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1); |
| 888 | len = r_id2; |
| 889 | } else { |
| 890 | r_id1 = 0; |
| 891 | r_id2 = 0; |
| 892 | len = r_name; |
| 893 | } |
| 894 | if (len < 0) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 895 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
| 897 | len += VBLK_SIZE_DGR4; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 898 | if (len != get_unaligned_be32(buffer + 0x14)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 899 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | dgrp = &vb->vblk.dgrp; |
| 902 | |
| 903 | ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf)); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 904 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /** |
| 908 | * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure |
| 909 | * @buffer: Block of data being worked on |
| 910 | * @buflen: Size of the block of data |
| 911 | * @vb: In-memory vblk in which to return information |
| 912 | * |
| 913 | * Read a raw VBLK Disk object (version 3) into a vblk structure. |
| 914 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 915 | * Return: 'true' @vb contains a Disk VBLK |
| 916 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 918 | static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | { |
| 920 | int r_objid, r_name, r_diskid, r_altname, len; |
| 921 | struct vblk_disk *disk; |
| 922 | |
| 923 | BUG_ON (!buffer || !vb); |
| 924 | |
| 925 | r_objid = ldm_relative (buffer, buflen, 0x18, 0); |
| 926 | r_name = ldm_relative (buffer, buflen, 0x18, r_objid); |
| 927 | r_diskid = ldm_relative (buffer, buflen, 0x18, r_name); |
| 928 | r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid); |
| 929 | len = r_altname; |
| 930 | if (len < 0) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 931 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | len += VBLK_SIZE_DSK3; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 934 | if (len != get_unaligned_be32(buffer + 0x14)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 935 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
| 937 | disk = &vb->vblk.disk; |
| 938 | ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name, |
| 939 | sizeof (disk->alt_name)); |
| 940 | if (!ldm_parse_guid (buffer + 0x19 + r_name, disk->disk_id)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 941 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 943 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /** |
| 947 | * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure |
| 948 | * @buffer: Block of data being worked on |
| 949 | * @buflen: Size of the block of data |
| 950 | * @vb: In-memory vblk in which to return information |
| 951 | * |
| 952 | * Read a raw VBLK Disk object (version 4) into a vblk structure. |
| 953 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 954 | * Return: 'true' @vb contains a Disk VBLK |
| 955 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 957 | static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
| 959 | int r_objid, r_name, len; |
| 960 | struct vblk_disk *disk; |
| 961 | |
| 962 | BUG_ON (!buffer || !vb); |
| 963 | |
| 964 | r_objid = ldm_relative (buffer, buflen, 0x18, 0); |
| 965 | r_name = ldm_relative (buffer, buflen, 0x18, r_objid); |
| 966 | len = r_name; |
| 967 | if (len < 0) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 968 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | len += VBLK_SIZE_DSK4; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 971 | if (len != get_unaligned_be32(buffer + 0x14)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 972 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | disk = &vb->vblk.disk; |
| 975 | memcpy (disk->disk_id, buffer + 0x18 + r_name, GUID_SIZE); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 976 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /** |
| 980 | * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure |
| 981 | * @buffer: Block of data being worked on |
| 982 | * @buflen: Size of the block of data |
| 983 | * @vb: In-memory vblk in which to return information |
| 984 | * |
| 985 | * Read a raw VBLK Partition object (version 3) into a vblk structure. |
| 986 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 987 | * Return: 'true' @vb contains a Partition VBLK |
| 988 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | */ |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 990 | static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
| 992 | int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len; |
| 993 | struct vblk_part *part; |
| 994 | |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 995 | BUG_ON(!buffer || !vb); |
| 996 | r_objid = ldm_relative(buffer, buflen, 0x18, 0); |
| 997 | if (r_objid < 0) { |
| 998 | ldm_error("r_objid %d < 0", r_objid); |
| 999 | return false; |
| 1000 | } |
| 1001 | r_name = ldm_relative(buffer, buflen, 0x18, r_objid); |
| 1002 | if (r_name < 0) { |
| 1003 | ldm_error("r_name %d < 0", r_name); |
| 1004 | return false; |
| 1005 | } |
| 1006 | r_size = ldm_relative(buffer, buflen, 0x34, r_name); |
| 1007 | if (r_size < 0) { |
| 1008 | ldm_error("r_size %d < 0", r_size); |
| 1009 | return false; |
| 1010 | } |
| 1011 | r_parent = ldm_relative(buffer, buflen, 0x34, r_size); |
| 1012 | if (r_parent < 0) { |
| 1013 | ldm_error("r_parent %d < 0", r_parent); |
| 1014 | return false; |
| 1015 | } |
| 1016 | r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent); |
| 1017 | if (r_diskid < 0) { |
| 1018 | ldm_error("r_diskid %d < 0", r_diskid); |
| 1019 | return false; |
| 1020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (buffer[0x12] & VBLK_FLAG_PART_INDEX) { |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1022 | r_index = ldm_relative(buffer, buflen, 0x34, r_diskid); |
| 1023 | if (r_index < 0) { |
| 1024 | ldm_error("r_index %d < 0", r_index); |
| 1025 | return false; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | len = r_index; |
| 1028 | } else { |
| 1029 | r_index = 0; |
| 1030 | len = r_diskid; |
| 1031 | } |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1032 | if (len < 0) { |
| 1033 | ldm_error("len %d < 0", len); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1034 | return false; |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | len += VBLK_SIZE_PRT3; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1037 | if (len > get_unaligned_be32(buffer + 0x14)) { |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1038 | ldm_error("len %d > BE32(buffer + 0x14) %d", len, |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1039 | get_unaligned_be32(buffer + 0x14)); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1040 | return false; |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | part = &vb->vblk.part; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1043 | part->start = get_unaligned_be64(buffer + 0x24 + r_name); |
| 1044 | part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name); |
Anton Altaparmakov | dde3334 | 2007-05-21 09:37:42 +0100 | [diff] [blame] | 1045 | part->size = ldm_get_vnum(buffer + 0x34 + r_name); |
| 1046 | part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size); |
| 1047 | part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (vb->flags & VBLK_FLAG_PART_INDEX) |
| 1049 | part->partnum = buffer[0x35 + r_diskid]; |
| 1050 | else |
| 1051 | part->partnum = 0; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1052 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure |
| 1057 | * @buffer: Block of data being worked on |
| 1058 | * @buflen: Size of the block of data |
| 1059 | * @vb: In-memory vblk in which to return information |
| 1060 | * |
| 1061 | * Read a raw VBLK Volume object (version 5) into a vblk structure. |
| 1062 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1063 | * Return: 'true' @vb contains a Volume VBLK |
| 1064 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | */ |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1066 | static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | { |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1068 | int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size; |
| 1069 | int r_id1, r_id2, r_size2, r_drive, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | struct vblk_volu *volu; |
| 1071 | |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1072 | BUG_ON(!buffer || !vb); |
| 1073 | r_objid = ldm_relative(buffer, buflen, 0x18, 0); |
| 1074 | if (r_objid < 0) { |
| 1075 | ldm_error("r_objid %d < 0", r_objid); |
| 1076 | return false; |
| 1077 | } |
| 1078 | r_name = ldm_relative(buffer, buflen, 0x18, r_objid); |
| 1079 | if (r_name < 0) { |
| 1080 | ldm_error("r_name %d < 0", r_name); |
| 1081 | return false; |
| 1082 | } |
| 1083 | r_vtype = ldm_relative(buffer, buflen, 0x18, r_name); |
| 1084 | if (r_vtype < 0) { |
| 1085 | ldm_error("r_vtype %d < 0", r_vtype); |
| 1086 | return false; |
| 1087 | } |
| 1088 | r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype); |
| 1089 | if (r_disable_drive_letter < 0) { |
| 1090 | ldm_error("r_disable_drive_letter %d < 0", |
| 1091 | r_disable_drive_letter); |
| 1092 | return false; |
| 1093 | } |
| 1094 | r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter); |
| 1095 | if (r_child < 0) { |
| 1096 | ldm_error("r_child %d < 0", r_child); |
| 1097 | return false; |
| 1098 | } |
| 1099 | r_size = ldm_relative(buffer, buflen, 0x3D, r_child); |
| 1100 | if (r_size < 0) { |
| 1101 | ldm_error("r_size %d < 0", r_size); |
| 1102 | return false; |
| 1103 | } |
| 1104 | if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) { |
| 1105 | r_id1 = ldm_relative(buffer, buflen, 0x52, r_size); |
| 1106 | if (r_id1 < 0) { |
| 1107 | ldm_error("r_id1 %d < 0", r_id1); |
| 1108 | return false; |
| 1109 | } |
| 1110 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | r_id1 = r_size; |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1112 | if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) { |
| 1113 | r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1); |
| 1114 | if (r_id2 < 0) { |
| 1115 | ldm_error("r_id2 %d < 0", r_id2); |
| 1116 | return false; |
| 1117 | } |
| 1118 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | r_id2 = r_id1; |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1120 | if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) { |
| 1121 | r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2); |
| 1122 | if (r_size2 < 0) { |
| 1123 | ldm_error("r_size2 %d < 0", r_size2); |
| 1124 | return false; |
| 1125 | } |
| 1126 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | r_size2 = r_id2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) { |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1129 | r_drive = ldm_relative(buffer, buflen, 0x52, r_size2); |
| 1130 | if (r_drive < 0) { |
| 1131 | ldm_error("r_drive %d < 0", r_drive); |
| 1132 | return false; |
| 1133 | } |
| 1134 | } else |
| 1135 | r_drive = r_size2; |
| 1136 | len = r_drive; |
| 1137 | if (len < 0) { |
| 1138 | ldm_error("len %d < 0", len); |
| 1139 | return false; |
| 1140 | } |
| 1141 | len += VBLK_SIZE_VOL5; |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1142 | if (len > get_unaligned_be32(buffer + 0x14)) { |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1143 | ldm_error("len %d > BE32(buffer + 0x14) %d", len, |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1144 | get_unaligned_be32(buffer + 0x14)); |
Anton Altaparmakov | 959bc220 | 2007-07-16 19:39:02 +0100 | [diff] [blame] | 1145 | return false; |
| 1146 | } |
| 1147 | volu = &vb->vblk.volu; |
| 1148 | ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type, |
| 1149 | sizeof(volu->volume_type)); |
| 1150 | memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter, |
| 1151 | sizeof(volu->volume_state)); |
| 1152 | volu->size = ldm_get_vnum(buffer + 0x3D + r_child); |
| 1153 | volu->partition_type = buffer[0x41 + r_size]; |
| 1154 | memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid)); |
| 1155 | if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) { |
| 1156 | ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint, |
| 1157 | sizeof(volu->drive_hint)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1159 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * ldm_parse_vblk - Read a raw VBLK object into a vblk structure |
| 1164 | * @buf: Block of data being worked on |
| 1165 | * @len: Size of the block of data |
| 1166 | * @vb: In-memory vblk in which to return information |
| 1167 | * |
| 1168 | * Read a raw VBLK object into a vblk structure. This function just reads the |
| 1169 | * information common to all VBLK types, then delegates the rest of the work to |
| 1170 | * helper functions: ldm_parse_*. |
| 1171 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1172 | * Return: 'true' @vb contains a VBLK |
| 1173 | * 'false' @vb contents are not defined |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1175 | static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1177 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | int r_objid; |
| 1179 | |
| 1180 | BUG_ON (!buf || !vb); |
| 1181 | |
| 1182 | r_objid = ldm_relative (buf, len, 0x18, 0); |
| 1183 | if (r_objid < 0) { |
| 1184 | ldm_error ("VBLK header is corrupt."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1185 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | vb->flags = buf[0x12]; |
| 1189 | vb->type = buf[0x13]; |
| 1190 | vb->obj_id = ldm_get_vnum (buf + 0x18); |
| 1191 | ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name)); |
| 1192 | |
| 1193 | switch (vb->type) { |
| 1194 | case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break; |
| 1195 | case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break; |
| 1196 | case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break; |
| 1197 | case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break; |
| 1198 | case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break; |
| 1199 | case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break; |
| 1200 | case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break; |
| 1201 | } |
| 1202 | |
| 1203 | if (result) |
| 1204 | ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.", |
| 1205 | (unsigned long long) vb->obj_id, vb->type); |
| 1206 | else |
| 1207 | ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).", |
| 1208 | (unsigned long long) vb->obj_id, vb->type); |
| 1209 | |
| 1210 | return result; |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | /** |
| 1215 | * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database |
| 1216 | * @data: Raw VBLK to add to the database |
| 1217 | * @len: Size of the raw VBLK |
| 1218 | * @ldb: Cache of the database structures |
| 1219 | * |
| 1220 | * The VBLKs are sorted into categories. Partitions are also sorted by offset. |
| 1221 | * |
| 1222 | * N.B. This function does not check the validity of the VBLKs. |
| 1223 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1224 | * Return: 'true' The VBLK was added |
| 1225 | * 'false' An error occurred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1227 | static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | { |
| 1229 | struct vblk *vb; |
| 1230 | struct list_head *item; |
| 1231 | |
| 1232 | BUG_ON (!data || !ldb); |
| 1233 | |
| 1234 | vb = kmalloc (sizeof (*vb), GFP_KERNEL); |
| 1235 | if (!vb) { |
| 1236 | ldm_crit ("Out of memory."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1237 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | if (!ldm_parse_vblk (data, len, vb)) { |
| 1241 | kfree(vb); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1242 | return false; /* Already logged */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /* Put vblk into the correct list. */ |
| 1246 | switch (vb->type) { |
| 1247 | case VBLK_DGR3: |
| 1248 | case VBLK_DGR4: |
| 1249 | list_add (&vb->list, &ldb->v_dgrp); |
| 1250 | break; |
| 1251 | case VBLK_DSK3: |
| 1252 | case VBLK_DSK4: |
| 1253 | list_add (&vb->list, &ldb->v_disk); |
| 1254 | break; |
| 1255 | case VBLK_VOL5: |
| 1256 | list_add (&vb->list, &ldb->v_volu); |
| 1257 | break; |
| 1258 | case VBLK_CMP3: |
| 1259 | list_add (&vb->list, &ldb->v_comp); |
| 1260 | break; |
| 1261 | case VBLK_PRT3: |
| 1262 | /* Sort by the partition's start sector. */ |
| 1263 | list_for_each (item, &ldb->v_part) { |
| 1264 | struct vblk *v = list_entry (item, struct vblk, list); |
| 1265 | if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) && |
| 1266 | (v->vblk.part.start > vb->vblk.part.start)) { |
| 1267 | list_add_tail (&vb->list, &v->list); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1268 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | list_add_tail (&vb->list, &ldb->v_part); |
| 1272 | break; |
| 1273 | } |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1274 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /** |
| 1278 | * ldm_frag_add - Add a VBLK fragment to a list |
| 1279 | * @data: Raw fragment to be added to the list |
| 1280 | * @size: Size of the raw fragment |
| 1281 | * @frags: Linked list of VBLK fragments |
| 1282 | * |
| 1283 | * Fragmented VBLKs may not be consecutive in the database, so they are placed |
| 1284 | * in a list so they can be pieced together later. |
| 1285 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1286 | * Return: 'true' Success, the VBLK was added to the list |
| 1287 | * 'false' Error, a problem occurred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1289 | static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | { |
| 1291 | struct frag *f; |
| 1292 | struct list_head *item; |
| 1293 | int rec, num, group; |
| 1294 | |
| 1295 | BUG_ON (!data || !frags); |
| 1296 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1297 | group = get_unaligned_be32(data + 0x08); |
| 1298 | rec = get_unaligned_be16(data + 0x0C); |
| 1299 | num = get_unaligned_be16(data + 0x0E); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | if ((num < 1) || (num > 4)) { |
| 1301 | ldm_error ("A VBLK claims to have %d parts.", num); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1302 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | list_for_each (item, frags) { |
| 1306 | f = list_entry (item, struct frag, list); |
| 1307 | if (f->group == group) |
| 1308 | goto found; |
| 1309 | } |
| 1310 | |
| 1311 | f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL); |
| 1312 | if (!f) { |
| 1313 | ldm_crit ("Out of memory."); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1314 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | f->group = group; |
| 1318 | f->num = num; |
| 1319 | f->rec = rec; |
| 1320 | f->map = 0xFF << num; |
| 1321 | |
| 1322 | list_add_tail (&f->list, frags); |
| 1323 | found: |
| 1324 | if (f->map & (1 << rec)) { |
| 1325 | ldm_error ("Duplicate VBLK, part %d.", rec); |
| 1326 | f->map &= 0x7F; /* Mark the group as broken */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1327 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | f->map |= (1 << rec); |
| 1331 | |
| 1332 | if (num > 0) { |
| 1333 | data += VBLK_SIZE_HEAD; |
| 1334 | size -= VBLK_SIZE_HEAD; |
| 1335 | } |
| 1336 | memcpy (f->data+rec*(size-VBLK_SIZE_HEAD)+VBLK_SIZE_HEAD, data, size); |
| 1337 | |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1338 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * ldm_frag_free - Free a linked list of VBLK fragments |
| 1343 | * @list: Linked list of fragments |
| 1344 | * |
| 1345 | * Free a linked list of VBLK fragments |
| 1346 | * |
| 1347 | * Return: none |
| 1348 | */ |
| 1349 | static void ldm_frag_free (struct list_head *list) |
| 1350 | { |
| 1351 | struct list_head *item, *tmp; |
| 1352 | |
| 1353 | BUG_ON (!list); |
| 1354 | |
| 1355 | list_for_each_safe (item, tmp, list) |
| 1356 | kfree (list_entry (item, struct frag, list)); |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * ldm_frag_commit - Validate fragmented VBLKs and add them to the database |
| 1361 | * @frags: Linked list of VBLK fragments |
| 1362 | * @ldb: Cache of the database structures |
| 1363 | * |
| 1364 | * Now that all the fragmented VBLKs have been collected, they must be added to |
| 1365 | * the database for later use. |
| 1366 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1367 | * Return: 'true' All the fragments we added successfully |
| 1368 | * 'false' One or more of the fragments we invalid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1370 | static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | { |
| 1372 | struct frag *f; |
| 1373 | struct list_head *item; |
| 1374 | |
| 1375 | BUG_ON (!frags || !ldb); |
| 1376 | |
| 1377 | list_for_each (item, frags) { |
| 1378 | f = list_entry (item, struct frag, list); |
| 1379 | |
| 1380 | if (f->map != 0xFF) { |
| 1381 | ldm_error ("VBLK group %d is incomplete (0x%02x).", |
| 1382 | f->group, f->map); |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1383 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb)) |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1387 | return false; /* Already logged */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1389 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /** |
| 1393 | * ldm_get_vblks - Read the on-disk database of VBLKs into memory |
| 1394 | * @bdev: Device holding the LDM Database |
| 1395 | * @base: Offset, into @bdev, of the database |
| 1396 | * @ldb: Cache of the database structures |
| 1397 | * |
| 1398 | * To use the information from the VBLKs, they need to be read from the disk, |
| 1399 | * unpacked and validated. We cache them in @ldb according to their type. |
| 1400 | * |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1401 | * Return: 'true' All the VBLKs were read successfully |
| 1402 | * 'false' An error occurred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | */ |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1404 | static bool ldm_get_vblks (struct block_device *bdev, unsigned long base, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | struct ldmdb *ldb) |
| 1406 | { |
| 1407 | int size, perbuf, skip, finish, s, v, recs; |
| 1408 | u8 *data = NULL; |
| 1409 | Sector sect; |
Richard Knutsson | 130c6b9 | 2006-09-30 23:27:15 -0700 | [diff] [blame] | 1410 | bool result = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | LIST_HEAD (frags); |
| 1412 | |
| 1413 | BUG_ON (!bdev || !ldb); |
| 1414 | |
| 1415 | size = ldb->vm.vblk_size; |
| 1416 | perbuf = 512 / size; |
| 1417 | skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */ |
| 1418 | finish = (size * ldb->vm.last_vblk_seq) >> 9; |
| 1419 | |
| 1420 | for (s = skip; s < finish; s++) { /* For each sector */ |
| 1421 | data = read_dev_sector (bdev, base + OFF_VMDB + s, §); |
| 1422 | if (!data) { |
| 1423 | ldm_crit ("Disk read failed."); |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
| 1427 | for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */ |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1428 | if (MAGIC_VBLK != get_unaligned_be32(data)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | ldm_error ("Expected to find a VBLK."); |
| 1430 | goto out; |
| 1431 | } |
| 1432 | |
Harvey Harrison | b7bbf8f | 2008-07-25 01:45:25 -0700 | [diff] [blame] | 1433 | recs = get_unaligned_be16(data + 0x0E); /* Number of records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | if (recs == 1) { |
| 1435 | if (!ldm_ldmdb_add (data, size, ldb)) |
| 1436 | goto out; /* Already logged */ |
| 1437 | } else if (recs > 1) { |
| 1438 | if (!ldm_frag_add (data, size, &frags)) |
| 1439 | goto out; /* Already logged */ |
| 1440 | } |
| 1441 | /* else Record is not in use, ignore it. */ |
| 1442 | } |
| 1443 | put_dev_sector (sect); |
| 1444 | data = NULL; |
| 1445 | } |
| 1446 | |
| 1447 | result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */ |
| 1448 | out: |
| 1449 | if (data) |
| 1450 | put_dev_sector (sect); |
| 1451 | ldm_frag_free (&frags); |
| 1452 | |
| 1453 | return result; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * ldm_free_vblks - Free a linked list of vblk's |
| 1458 | * @lh: Head of a linked list of struct vblk |
| 1459 | * |
| 1460 | * Free a list of vblk's and free the memory used to maintain the list. |
| 1461 | * |
| 1462 | * Return: none |
| 1463 | */ |
| 1464 | static void ldm_free_vblks (struct list_head *lh) |
| 1465 | { |
| 1466 | struct list_head *item, *tmp; |
| 1467 | |
| 1468 | BUG_ON (!lh); |
| 1469 | |
| 1470 | list_for_each_safe (item, tmp, lh) |
| 1471 | kfree (list_entry (item, struct vblk, list)); |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | /** |
| 1476 | * ldm_partition - Find out whether a device is a dynamic disk and handle it |
| 1477 | * @pp: List of the partitions parsed so far |
| 1478 | * @bdev: Device holding the LDM Database |
| 1479 | * |
| 1480 | * This determines whether the device @bdev is a dynamic disk and if so creates |
| 1481 | * the partitions necessary in the gendisk structure pointed to by @hd. |
| 1482 | * |
| 1483 | * We create a dummy device 1, which contains the LDM database, and then create |
| 1484 | * each partition described by the LDM database in sequence as devices 2+. For |
| 1485 | * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3, |
| 1486 | * and so on: the actual data containing partitions. |
| 1487 | * |
| 1488 | * Return: 1 Success, @bdev is a dynamic disk and we handled it |
| 1489 | * 0 Success, @bdev is not a dynamic disk |
| 1490 | * -1 An error occurred before enough information had been read |
| 1491 | * Or @bdev is a dynamic disk, but it may be corrupted |
| 1492 | */ |
| 1493 | int ldm_partition (struct parsed_partitions *pp, struct block_device *bdev) |
| 1494 | { |
| 1495 | struct ldmdb *ldb; |
| 1496 | unsigned long base; |
| 1497 | int result = -1; |
| 1498 | |
| 1499 | BUG_ON (!pp || !bdev); |
| 1500 | |
| 1501 | /* Look for signs of a Dynamic Disk */ |
| 1502 | if (!ldm_validate_partition_table (bdev)) |
| 1503 | return 0; |
| 1504 | |
| 1505 | ldb = kmalloc (sizeof (*ldb), GFP_KERNEL); |
| 1506 | if (!ldb) { |
| 1507 | ldm_crit ("Out of memory."); |
| 1508 | goto out; |
| 1509 | } |
| 1510 | |
| 1511 | /* Parse and check privheads. */ |
| 1512 | if (!ldm_validate_privheads (bdev, &ldb->ph)) |
| 1513 | goto out; /* Already logged */ |
| 1514 | |
| 1515 | /* All further references are relative to base (database start). */ |
| 1516 | base = ldb->ph.config_start; |
| 1517 | |
| 1518 | /* Parse and check tocs and vmdb. */ |
| 1519 | if (!ldm_validate_tocblocks (bdev, base, ldb) || |
| 1520 | !ldm_validate_vmdb (bdev, base, ldb)) |
| 1521 | goto out; /* Already logged */ |
| 1522 | |
| 1523 | /* Initialize vblk lists in ldmdb struct */ |
| 1524 | INIT_LIST_HEAD (&ldb->v_dgrp); |
| 1525 | INIT_LIST_HEAD (&ldb->v_disk); |
| 1526 | INIT_LIST_HEAD (&ldb->v_volu); |
| 1527 | INIT_LIST_HEAD (&ldb->v_comp); |
| 1528 | INIT_LIST_HEAD (&ldb->v_part); |
| 1529 | |
| 1530 | if (!ldm_get_vblks (bdev, base, ldb)) { |
| 1531 | ldm_crit ("Failed to read the VBLKs from the database."); |
| 1532 | goto cleanup; |
| 1533 | } |
| 1534 | |
| 1535 | /* Finally, create the data partition devices. */ |
| 1536 | if (ldm_create_data_partitions (pp, ldb)) { |
| 1537 | ldm_debug ("Parsed LDM database successfully."); |
| 1538 | result = 1; |
| 1539 | } |
| 1540 | /* else Already logged */ |
| 1541 | |
| 1542 | cleanup: |
| 1543 | ldm_free_vblks (&ldb->v_dgrp); |
| 1544 | ldm_free_vblks (&ldb->v_disk); |
| 1545 | ldm_free_vblks (&ldb->v_volu); |
| 1546 | ldm_free_vblks (&ldb->v_comp); |
| 1547 | ldm_free_vblks (&ldb->v_part); |
| 1548 | out: |
| 1549 | kfree (ldb); |
| 1550 | return result; |
| 1551 | } |