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