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