blob: edcea70674c9de501b6d26b9082ddd780ecc5bea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * ldm - Support for Windows Logical Disk Manager (Dynamic Disks)
3 *
4 * Copyright (C) 2001,2002 Richard Russon <ldm@flatcap.org>
Anton Altaparmakov97387e32012-02-24 09:37:42 +00005 * Copyright (c) 2001-2012 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2001,2002 Jakob Kemi <jakob.kemi@telia.com>
7 *
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02008 * Documentation is available at http://www.linux-ntfs.org/doku.php?id=downloads
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 Shevchenko91f06e62010-05-24 14:33:27 -070029#include <linux/kernel.h>
Andy Shevchenko7244ad62016-05-20 17:01:27 -070030#include <linux/uuid.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "ldm.h"
33#include "check.h"
34#include "msdos.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/**
37 * ldm_debug/info/error/crit - Output an error message
38 * @f: A printf format string containing the message
39 * @...: Variables to substitute into @f
40 *
41 * ldm_debug() writes a DEBUG level message to the syslog but only if the
42 * driver was compiled with debug enabled. Otherwise, the call turns into a NOP.
43 */
44#ifndef CONFIG_LDM_DEBUG
45#define ldm_debug(...) do {} while (0)
46#else
Harvey Harrison8e24eea2008-04-30 00:55:09 -070047#define ldm_debug(f, a...) _ldm_printk (KERN_DEBUG, __func__, f, ##a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
Harvey Harrison8e24eea2008-04-30 00:55:09 -070050#define ldm_crit(f, a...) _ldm_printk (KERN_CRIT, __func__, f, ##a)
51#define ldm_error(f, a...) _ldm_printk (KERN_ERR, __func__, f, ##a)
52#define ldm_info(f, a...) _ldm_printk (KERN_INFO, __func__, f, ##a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Joe Perchesb9075fa2011-10-31 17:11:33 -070054static __printf(3, 4)
55void _ldm_printk(const char *level, const char *function, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Joe Perchesb9075fa2011-10-31 17:11:33 -070057 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 va_list args;
59
60 va_start (args, fmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Joe Perchesb9075fa2011-10-31 17:11:33 -070062 vaf.fmt = fmt;
63 vaf.va = &args;
64
65 printk("%s%s(): %pV\n", level, function, &vaf);
66
67 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * ldm_parse_privhead - Read the LDM Database PRIVHEAD structure
72 * @data: Raw database PRIVHEAD structure loaded from the device
73 * @ph: In-memory privhead structure in which to return parsed information
74 *
75 * This parses the LDM database PRIVHEAD structure supplied in @data and
76 * sets up the in-memory privhead structure @ph with the obtained information.
77 *
Richard Knutsson130c6b92006-09-30 23:27:15 -070078 * Return: 'true' @ph contains the PRIVHEAD data
79 * 'false' @ph contents are undefined
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Anton Altaparmakovdde33342007-05-21 09:37:42 +010081static bool ldm_parse_privhead(const u8 *data, struct privhead *ph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Anton Altaparmakovdde33342007-05-21 09:37:42 +010083 bool is_vista = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Anton Altaparmakovdde33342007-05-21 09:37:42 +010085 BUG_ON(!data || !ph);
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -070086 if (MAGIC_PRIVHEAD != get_unaligned_be64(data)) {
Anton Altaparmakovdde33342007-05-21 09:37:42 +010087 ldm_error("Cannot find PRIVHEAD structure. LDM database is"
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 " corrupt. Aborting.");
Richard Knutsson130c6b92006-09-30 23:27:15 -070089 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -070091 ph->ver_major = get_unaligned_be16(data + 0x000C);
92 ph->ver_minor = get_unaligned_be16(data + 0x000E);
93 ph->logical_disk_start = get_unaligned_be64(data + 0x011B);
94 ph->logical_disk_size = get_unaligned_be64(data + 0x0123);
95 ph->config_start = get_unaligned_be64(data + 0x012B);
96 ph->config_size = get_unaligned_be64(data + 0x0133);
Anton Altaparmakovdde33342007-05-21 09:37:42 +010097 /* Version 2.11 is Win2k/XP and version 2.12 is Vista. */
98 if (ph->ver_major == 2 && ph->ver_minor == 12)
99 is_vista = true;
100 if (!is_vista && (ph->ver_major != 2 || ph->ver_minor != 11)) {
101 ldm_error("Expected PRIVHEAD version 2.11 or 2.12, got %d.%d."
102 " Aborting.", ph->ver_major, ph->ver_minor);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700103 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100105 ldm_debug("PRIVHEAD version %d.%d (Windows %s).", ph->ver_major,
106 ph->ver_minor, is_vista ? "Vista" : "2000/XP");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (ph->config_size != LDM_DB_SIZE) { /* 1 MiB in sectors. */
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100108 /* Warn the user and continue, carefully. */
109 ldm_info("Database is normally %u bytes, it claims to "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 "be %llu bytes.", LDM_DB_SIZE,
Jeff Garzik72dd9ca2007-05-22 00:28:11 -0400111 (unsigned long long)ph->config_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100113 if ((ph->logical_disk_size == 0) || (ph->logical_disk_start +
114 ph->logical_disk_size > ph->config_start)) {
115 ldm_error("PRIVHEAD disk size doesn't match real disk size");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700116 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Andy Shevchenko7244ad62016-05-20 17:01:27 -0700118 if (uuid_be_to_bin(data + 0x0030, (uuid_be *)ph->disk_id)) {
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100119 ldm_error("PRIVHEAD contains an invalid GUID.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700120 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100122 ldm_debug("Parsed PRIVHEAD successfully.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700123 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/**
127 * ldm_parse_tocblock - Read the LDM Database TOCBLOCK structure
128 * @data: Raw database TOCBLOCK structure loaded from the device
129 * @toc: In-memory toc structure in which to return parsed information
130 *
131 * This parses the LDM Database TOCBLOCK (table of contents) structure supplied
132 * in @data and sets up the in-memory tocblock structure @toc with the obtained
133 * information.
134 *
135 * N.B. The *_start and *_size values returned in @toc are not range-checked.
136 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700137 * Return: 'true' @toc contains the TOCBLOCK data
138 * 'false' @toc contents are undefined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700140static bool ldm_parse_tocblock (const u8 *data, struct tocblock *toc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 BUG_ON (!data || !toc);
143
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700144 if (MAGIC_TOCBLOCK != get_unaligned_be64(data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ldm_crit ("Cannot find TOCBLOCK, database may be corrupt.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700146 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 strncpy (toc->bitmap1_name, data + 0x24, sizeof (toc->bitmap1_name));
149 toc->bitmap1_name[sizeof (toc->bitmap1_name) - 1] = 0;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700150 toc->bitmap1_start = get_unaligned_be64(data + 0x2E);
151 toc->bitmap1_size = get_unaligned_be64(data + 0x36);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (strncmp (toc->bitmap1_name, TOC_BITMAP1,
154 sizeof (toc->bitmap1_name)) != 0) {
155 ldm_crit ("TOCBLOCK's first bitmap is '%s', should be '%s'.",
156 TOC_BITMAP1, toc->bitmap1_name);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700157 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 strncpy (toc->bitmap2_name, data + 0x46, sizeof (toc->bitmap2_name));
160 toc->bitmap2_name[sizeof (toc->bitmap2_name) - 1] = 0;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700161 toc->bitmap2_start = get_unaligned_be64(data + 0x50);
162 toc->bitmap2_size = get_unaligned_be64(data + 0x58);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (strncmp (toc->bitmap2_name, TOC_BITMAP2,
164 sizeof (toc->bitmap2_name)) != 0) {
165 ldm_crit ("TOCBLOCK's second bitmap is '%s', should be '%s'.",
166 TOC_BITMAP2, toc->bitmap2_name);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700167 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 ldm_debug ("Parsed TOCBLOCK successfully.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700170 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
174 * ldm_parse_vmdb - Read the LDM Database VMDB structure
175 * @data: Raw database VMDB structure loaded from the device
176 * @vm: In-memory vmdb structure in which to return parsed information
177 *
178 * This parses the LDM Database VMDB structure supplied in @data and sets up
179 * the in-memory vmdb structure @vm with the obtained information.
180 *
181 * N.B. The *_start, *_size and *_seq values will be range-checked later.
182 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700183 * Return: 'true' @vm contains VMDB info
184 * 'false' @vm contents are undefined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700186static bool ldm_parse_vmdb (const u8 *data, struct vmdb *vm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 BUG_ON (!data || !vm);
189
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700190 if (MAGIC_VMDB != get_unaligned_be32(data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ldm_crit ("Cannot find the VMDB, database may be corrupt.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700192 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700195 vm->ver_major = get_unaligned_be16(data + 0x12);
196 vm->ver_minor = get_unaligned_be16(data + 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if ((vm->ver_major != 4) || (vm->ver_minor != 10)) {
198 ldm_error ("Expected VMDB version %d.%d, got %d.%d. "
199 "Aborting.", 4, 10, vm->ver_major, vm->ver_minor);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700200 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700203 vm->vblk_size = get_unaligned_be32(data + 0x08);
Timo Warns294f6cf2011-02-25 14:44:21 -0800204 if (vm->vblk_size == 0) {
205 ldm_error ("Illegal VBLK size");
206 return false;
207 }
208
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700209 vm->vblk_offset = get_unaligned_be32(data + 0x0C);
210 vm->last_vblk_seq = get_unaligned_be32(data + 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 ldm_debug ("Parsed VMDB successfully.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700213 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/**
217 * ldm_compare_privheads - Compare two privhead objects
218 * @ph1: First privhead
219 * @ph2: Second privhead
220 *
221 * This compares the two privhead structures @ph1 and @ph2.
222 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700223 * Return: 'true' Identical
224 * 'false' Different
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700226static bool ldm_compare_privheads (const struct privhead *ph1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 const struct privhead *ph2)
228{
229 BUG_ON (!ph1 || !ph2);
230
231 return ((ph1->ver_major == ph2->ver_major) &&
232 (ph1->ver_minor == ph2->ver_minor) &&
233 (ph1->logical_disk_start == ph2->logical_disk_start) &&
234 (ph1->logical_disk_size == ph2->logical_disk_size) &&
235 (ph1->config_start == ph2->config_start) &&
236 (ph1->config_size == ph2->config_size) &&
237 !memcmp (ph1->disk_id, ph2->disk_id, GUID_SIZE));
238}
239
240/**
241 * ldm_compare_tocblocks - Compare two tocblock objects
242 * @toc1: First toc
243 * @toc2: Second toc
244 *
245 * This compares the two tocblock structures @toc1 and @toc2.
246 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700247 * Return: 'true' Identical
248 * 'false' Different
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700250static bool ldm_compare_tocblocks (const struct tocblock *toc1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 const struct tocblock *toc2)
252{
253 BUG_ON (!toc1 || !toc2);
254
255 return ((toc1->bitmap1_start == toc2->bitmap1_start) &&
256 (toc1->bitmap1_size == toc2->bitmap1_size) &&
257 (toc1->bitmap2_start == toc2->bitmap2_start) &&
258 (toc1->bitmap2_size == toc2->bitmap2_size) &&
259 !strncmp (toc1->bitmap1_name, toc2->bitmap1_name,
260 sizeof (toc1->bitmap1_name)) &&
261 !strncmp (toc1->bitmap2_name, toc2->bitmap2_name,
262 sizeof (toc1->bitmap2_name)));
263}
264
265/**
266 * ldm_validate_privheads - Compare the primary privhead with its backups
Tejun Heo1493bf22010-05-15 20:09:30 +0200267 * @state: Partition check state including device holding the LDM Database
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @ph1: Memory struct to fill with ph contents
269 *
270 * Read and compare all three privheads from disk.
271 *
272 * The privheads on disk show the size and location of the main disk area and
273 * the configuration area (the database). The values are range-checked against
274 * @hd, which contains the real size of the disk.
275 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700276 * Return: 'true' Success
277 * 'false' Error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200279static bool ldm_validate_privheads(struct parsed_partitions *state,
280 struct privhead *ph1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 static const int off[3] = { OFF_PRIV1, OFF_PRIV2, OFF_PRIV3 };
283 struct privhead *ph[3] = { ph1 };
284 Sector sect;
285 u8 *data;
Richard Knutsson130c6b92006-09-30 23:27:15 -0700286 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 long num_sects;
288 int i;
289
Tejun Heo1493bf22010-05-15 20:09:30 +0200290 BUG_ON (!state || !ph1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 ph[1] = kmalloc (sizeof (*ph[1]), GFP_KERNEL);
293 ph[2] = kmalloc (sizeof (*ph[2]), GFP_KERNEL);
294 if (!ph[1] || !ph[2]) {
295 ldm_crit ("Out of memory.");
296 goto out;
297 }
298
299 /* off[1 & 2] are relative to ph[0]->config_start */
300 ph[0]->config_start = 0;
301
302 /* Read and parse privheads */
303 for (i = 0; i < 3; i++) {
Tejun Heo1493bf22010-05-15 20:09:30 +0200304 data = read_part_sector(state, ph[0]->config_start + off[i],
305 &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!data) {
307 ldm_crit ("Disk read failed.");
308 goto out;
309 }
310 result = ldm_parse_privhead (data, ph[i]);
311 put_dev_sector (sect);
312 if (!result) {
313 ldm_error ("Cannot find PRIVHEAD %d.", i+1); /* Log again */
314 if (i < 2)
315 goto out; /* Already logged */
316 else
317 break; /* FIXME ignore for now, 3rd PH can fail on odd-sized disks */
318 }
319 }
320
Tejun Heo1493bf22010-05-15 20:09:30 +0200321 num_sects = state->bdev->bd_inode->i_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if ((ph[0]->config_start > num_sects) ||
324 ((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
325 ldm_crit ("Database extends beyond the end of the disk.");
326 goto out;
327 }
328
329 if ((ph[0]->logical_disk_start > ph[0]->config_start) ||
330 ((ph[0]->logical_disk_start + ph[0]->logical_disk_size)
331 > ph[0]->config_start)) {
332 ldm_crit ("Disk and database overlap.");
333 goto out;
334 }
335
336 if (!ldm_compare_privheads (ph[0], ph[1])) {
337 ldm_crit ("Primary and backup PRIVHEADs don't match.");
338 goto out;
339 }
340 /* FIXME ignore this for now
341 if (!ldm_compare_privheads (ph[0], ph[2])) {
342 ldm_crit ("Primary and backup PRIVHEADs don't match.");
343 goto out;
344 }*/
345 ldm_debug ("Validated PRIVHEADs successfully.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700346 result = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347out:
348 kfree (ph[1]);
349 kfree (ph[2]);
350 return result;
351}
352
353/**
354 * ldm_validate_tocblocks - Validate the table of contents and its backups
Tejun Heo1493bf22010-05-15 20:09:30 +0200355 * @state: Partition check state including device holding the LDM Database
356 * @base: Offset, into @state->bdev, of the database
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * @ldb: Cache of the database structures
358 *
359 * Find and compare the four tables of contents of the LDM Database stored on
Tejun Heo1493bf22010-05-15 20:09:30 +0200360 * @state->bdev and return the parsed information into @toc1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * The offsets and sizes of the configs are range-checked against a privhead.
363 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700364 * Return: 'true' @toc1 contains validated TOCBLOCK info
365 * 'false' @toc1 contents are undefined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200367static bool ldm_validate_tocblocks(struct parsed_partitions *state,
368 unsigned long base, struct ldmdb *ldb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 static const int off[4] = { OFF_TOCB1, OFF_TOCB2, OFF_TOCB3, OFF_TOCB4};
371 struct tocblock *tb[4];
372 struct privhead *ph;
373 Sector sect;
374 u8 *data;
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100375 int i, nr_tbs;
Richard Knutsson130c6b92006-09-30 23:27:15 -0700376 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Tejun Heo1493bf22010-05-15 20:09:30 +0200378 BUG_ON(!state || !ldb);
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100379 ph = &ldb->ph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 tb[0] = &ldb->toc;
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100381 tb[1] = kmalloc(sizeof(*tb[1]) * 3, GFP_KERNEL);
382 if (!tb[1]) {
383 ldm_crit("Out of memory.");
384 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100386 tb[2] = (struct tocblock*)((u8*)tb[1] + sizeof(*tb[1]));
387 tb[3] = (struct tocblock*)((u8*)tb[2] + sizeof(*tb[2]));
388 /*
389 * Try to read and parse all four TOCBLOCKs.
390 *
391 * Windows Vista LDM v2.12 does not always have all four TOCBLOCKs so
392 * skip any that fail as long as we get at least one valid TOCBLOCK.
393 */
394 for (nr_tbs = i = 0; i < 4; i++) {
Tejun Heo1493bf22010-05-15 20:09:30 +0200395 data = read_part_sector(state, base + off[i], &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!data) {
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100397 ldm_error("Disk read failed for TOCBLOCK %d.", i);
398 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100400 if (ldm_parse_tocblock(data, tb[nr_tbs]))
401 nr_tbs++;
402 put_dev_sector(sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100404 if (!nr_tbs) {
405 ldm_crit("Failed to find a valid TOCBLOCK.");
406 goto err;
407 }
408 /* Range check the TOCBLOCK against a privhead. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (((tb[0]->bitmap1_start + tb[0]->bitmap1_size) > ph->config_size) ||
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100410 ((tb[0]->bitmap2_start + tb[0]->bitmap2_size) >
411 ph->config_size)) {
412 ldm_crit("The bitmaps are out of range. Giving up.");
413 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100415 /* Compare all loaded TOCBLOCKs. */
416 for (i = 1; i < nr_tbs; i++) {
417 if (!ldm_compare_tocblocks(tb[0], tb[i])) {
418 ldm_crit("TOCBLOCKs 0 and %d do not match.", i);
419 goto err;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100422 ldm_debug("Validated %d TOCBLOCKs successfully.", nr_tbs);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700423 result = true;
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100424err:
425 kfree(tb[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return result;
427}
428
429/**
430 * ldm_validate_vmdb - Read the VMDB and validate it
Tejun Heo1493bf22010-05-15 20:09:30 +0200431 * @state: Partition check state including device holding the LDM Database
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * @base: Offset, into @bdev, of the database
433 * @ldb: Cache of the database structures
434 *
435 * Find the vmdb of the LDM Database stored on @bdev and return the parsed
436 * information in @ldb.
437 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700438 * Return: 'true' @ldb contains validated VBDB info
439 * 'false' @ldb contents are undefined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200441static bool ldm_validate_vmdb(struct parsed_partitions *state,
442 unsigned long base, struct ldmdb *ldb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 Sector sect;
445 u8 *data;
Richard Knutsson130c6b92006-09-30 23:27:15 -0700446 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct vmdb *vm;
448 struct tocblock *toc;
449
Tejun Heo1493bf22010-05-15 20:09:30 +0200450 BUG_ON (!state || !ldb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 vm = &ldb->vm;
453 toc = &ldb->toc;
454
Tejun Heo1493bf22010-05-15 20:09:30 +0200455 data = read_part_sector(state, base + OFF_VMDB, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!data) {
457 ldm_crit ("Disk read failed.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700458 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 if (!ldm_parse_vmdb (data, vm))
462 goto out; /* Already logged */
463
464 /* Are there uncommitted transactions? */
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700465 if (get_unaligned_be16(data + 0x10) != 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ldm_crit ("Database is not in a consistent state. Aborting.");
467 goto out;
468 }
469
470 if (vm->vblk_offset != 512)
471 ldm_info ("VBLKs start at offset 0x%04x.", vm->vblk_offset);
472
473 /*
474 * The last_vblkd_seq can be before the end of the vmdb, just make sure
475 * it is not out of bounds.
476 */
477 if ((vm->vblk_size * vm->last_vblk_seq) > (toc->bitmap1_size << 9)) {
478 ldm_crit ("VMDB exceeds allowed size specified by TOCBLOCK. "
479 "Database is corrupt. Aborting.");
480 goto out;
481 }
482
Richard Knutsson130c6b92006-09-30 23:27:15 -0700483 result = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484out:
485 put_dev_sector (sect);
486 return result;
487}
488
489
490/**
491 * ldm_validate_partition_table - Determine whether bdev might be a dynamic disk
Tejun Heo1493bf22010-05-15 20:09:30 +0200492 * @state: Partition check state including device holding the LDM Database
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *
494 * This function provides a weak test to decide whether the device is a dynamic
495 * disk or not. It looks for an MS-DOS-style partition table containing at
496 * least one partition of type 0x42 (formerly SFS, now used by Windows for
497 * dynamic disks).
498 *
Tejun Heo1493bf22010-05-15 20:09:30 +0200499 * N.B. The only possible error can come from the read_part_sector and that is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * only likely to happen if the underlying device is strange. If that IS
501 * the case we should return zero to let someone else try.
502 *
Tejun Heo1493bf22010-05-15 20:09:30 +0200503 * Return: 'true' @state->bdev is a dynamic disk
504 * 'false' @state->bdev is not a dynamic disk, or an error occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200506static bool ldm_validate_partition_table(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 Sector sect;
509 u8 *data;
510 struct partition *p;
511 int i;
Richard Knutsson130c6b92006-09-30 23:27:15 -0700512 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Tejun Heo1493bf22010-05-15 20:09:30 +0200514 BUG_ON(!state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Tejun Heo1493bf22010-05-15 20:09:30 +0200516 data = read_part_sector(state, 0, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!data) {
Nikanth Karthikesan1dd45aa2011-03-17 11:04:14 +0530518 ldm_info ("Disk read failed.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700519 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 if (*(__le16*) (data + 0x01FE) != cpu_to_le16 (MSDOS_LABEL_MAGIC))
523 goto out;
524
525 p = (struct partition*)(data + 0x01BE);
526 for (i = 0; i < 4; i++, p++)
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100527 if (SYS_IND (p) == LDM_PARTITION) {
Richard Knutsson130c6b92006-09-30 23:27:15 -0700528 result = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 }
531
532 if (result)
533 ldm_debug ("Found W2K dynamic disk partition type.");
534
535out:
536 put_dev_sector (sect);
537 return result;
538}
539
540/**
541 * ldm_get_disk_objid - Search a linked list of vblk's for a given Disk Id
542 * @ldb: Cache of the database structures
543 *
544 * The LDM Database contains a list of all partitions on all dynamic disks.
545 * The primary PRIVHEAD, at the beginning of the physical disk, tells us
546 * the GUID of this disk. This function searches for the GUID in a linked
547 * list of vblk's.
548 *
549 * Return: Pointer, A matching vblk was found
550 * NULL, No match, or an error
551 */
552static struct vblk * ldm_get_disk_objid (const struct ldmdb *ldb)
553{
554 struct list_head *item;
555
556 BUG_ON (!ldb);
557
558 list_for_each (item, &ldb->v_disk) {
559 struct vblk *v = list_entry (item, struct vblk, list);
560 if (!memcmp (v->vblk.disk.disk_id, ldb->ph.disk_id, GUID_SIZE))
561 return v;
562 }
563
564 return NULL;
565}
566
567/**
568 * ldm_create_data_partitions - Create data partitions for this device
569 * @pp: List of the partitions parsed so far
570 * @ldb: Cache of the database structures
571 *
572 * The database contains ALL the partitions for ALL disk groups, so we need to
573 * filter out this specific disk. Using the disk's object id, we can find all
574 * the partitions in the database that belong to this disk.
575 *
576 * Add each partition in our database, to the parsed_partitions structure.
577 *
578 * N.B. This function creates the partitions in the order it finds partition
579 * objects in the linked list.
580 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700581 * Return: 'true' Partition created
582 * 'false' Error, probably a range checking problem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700584static bool ldm_create_data_partitions (struct parsed_partitions *pp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 const struct ldmdb *ldb)
586{
587 struct list_head *item;
588 struct vblk *vb;
589 struct vblk *disk;
590 struct vblk_part *part;
591 int part_num = 1;
592
593 BUG_ON (!pp || !ldb);
594
595 disk = ldm_get_disk_objid (ldb);
596 if (!disk) {
597 ldm_crit ("Can't find the ID of this disk in the database.");
Richard Knutsson130c6b92006-09-30 23:27:15 -0700598 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700601 strlcat(pp->pp_buf, " [LDM]", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Create the data partitions */
604 list_for_each (item, &ldb->v_part) {
605 vb = list_entry (item, struct vblk, list);
606 part = &vb->vblk.part;
607
608 if (part->disk_id != disk->obj_id)
609 continue;
610
611 put_partition (pp, part_num, ldb->ph.logical_disk_start +
612 part->start, part->size);
613 part_num++;
614 }
615
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700616 strlcat(pp->pp_buf, "\n", PAGE_SIZE);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700617 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620
621/**
622 * ldm_relative - Calculate the next relative offset
623 * @buffer: Block of data being worked on
624 * @buflen: Size of the block of data
625 * @base: Size of the previous fixed width fields
626 * @offset: Cumulative size of the previous variable-width fields
627 *
628 * Because many of the VBLK fields are variable-width, it's necessary
629 * to calculate each offset based on the previous one and the length
630 * of the field it pointed to.
631 *
632 * Return: -1 Error, the calculated offset exceeded the size of the buffer
633 * n OK, a range-checked offset into buffer
634 */
Anton Altaparmakov959bc2202007-07-16 19:39:02 +0100635static int ldm_relative(const u8 *buffer, int buflen, int base, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637
638 base += offset;
Anton Altaparmakov959bc2202007-07-16 19:39:02 +0100639 if (!buffer || offset < 0 || base > buflen) {
640 if (!buffer)
641 ldm_error("!buffer");
642 if (offset < 0)
643 ldm_error("offset (%d) < 0", offset);
644 if (base > buflen)
645 ldm_error("base (%d) > buflen (%d)", base, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return -1;
Anton Altaparmakov959bc2202007-07-16 19:39:02 +0100647 }
648 if (base + buffer[base] >= buflen) {
649 ldm_error("base (%d) + buffer[base] (%d) >= buflen (%d)", base,
650 buffer[base], buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -1;
Anton Altaparmakov959bc2202007-07-16 19:39:02 +0100652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return buffer[base] + offset + 1;
654}
655
656/**
657 * ldm_get_vnum - Convert a variable-width, big endian number, into cpu order
658 * @block: Pointer to the variable-width number to convert
659 *
660 * Large numbers in the LDM Database are often stored in a packed format. Each
661 * number is prefixed by a one byte width marker. All numbers in the database
662 * are stored in big-endian byte order. This function reads one of these
663 * numbers and returns the result
664 *
665 * N.B. This function DOES NOT perform any range checking, though the most
666 * it will read is eight bytes.
667 *
668 * Return: n A number
669 * 0 Zero, or an error occurred
670 */
671static u64 ldm_get_vnum (const u8 *block)
672{
673 u64 tmp = 0;
674 u8 length;
675
676 BUG_ON (!block);
677
678 length = *block++;
679
680 if (length && length <= 8)
681 while (length--)
682 tmp = (tmp << 8) | *block++;
683 else
684 ldm_error ("Illegal length %d.", length);
685
686 return tmp;
687}
688
689/**
690 * ldm_get_vstr - Read a length-prefixed string into a buffer
691 * @block: Pointer to the length marker
692 * @buffer: Location to copy string to
693 * @buflen: Size of the output buffer
694 *
695 * Many of the strings in the LDM Database are not NULL terminated. Instead
696 * they are prefixed by a one byte length marker. This function copies one of
697 * these strings into a buffer.
698 *
699 * N.B. This function DOES NOT perform any range checking on the input.
700 * If the buffer is too small, the output will be truncated.
701 *
702 * Return: 0, Error and @buffer contents are undefined
703 * n, String length in characters (excluding NULL)
704 * buflen-1, String was truncated.
705 */
706static int ldm_get_vstr (const u8 *block, u8 *buffer, int buflen)
707{
708 int length;
709
710 BUG_ON (!block || !buffer);
711
712 length = block[0];
713 if (length >= buflen) {
714 ldm_error ("Truncating string %d -> %d.", length, buflen);
715 length = buflen - 1;
716 }
717 memcpy (buffer, block + 1, length);
718 buffer[length] = 0;
719 return length;
720}
721
722
723/**
724 * ldm_parse_cmp3 - Read a raw VBLK Component object into a vblk structure
725 * @buffer: Block of data being worked on
726 * @buflen: Size of the block of data
727 * @vb: In-memory vblk in which to return information
728 *
729 * Read a raw VBLK Component object (version 3) into a vblk structure.
730 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700731 * Return: 'true' @vb contains a Component VBLK
732 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700734static bool ldm_parse_cmp3 (const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 int r_objid, r_name, r_vstate, r_child, r_parent, r_stripe, r_cols, len;
737 struct vblk_comp *comp;
738
739 BUG_ON (!buffer || !vb);
740
741 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
742 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
743 r_vstate = ldm_relative (buffer, buflen, 0x18, r_name);
744 r_child = ldm_relative (buffer, buflen, 0x1D, r_vstate);
745 r_parent = ldm_relative (buffer, buflen, 0x2D, r_child);
746
747 if (buffer[0x12] & VBLK_FLAG_COMP_STRIPE) {
748 r_stripe = ldm_relative (buffer, buflen, 0x2E, r_parent);
749 r_cols = ldm_relative (buffer, buflen, 0x2E, r_stripe);
750 len = r_cols;
751 } else {
752 r_stripe = 0;
753 r_cols = 0;
754 len = r_parent;
755 }
756 if (len < 0)
Richard Knutsson130c6b92006-09-30 23:27:15 -0700757 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 len += VBLK_SIZE_CMP3;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700760 if (len != get_unaligned_be32(buffer + 0x14))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700761 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 comp = &vb->vblk.comp;
764 ldm_get_vstr (buffer + 0x18 + r_name, comp->state,
765 sizeof (comp->state));
766 comp->type = buffer[0x18 + r_vstate];
767 comp->children = ldm_get_vnum (buffer + 0x1D + r_vstate);
768 comp->parent_id = ldm_get_vnum (buffer + 0x2D + r_child);
769 comp->chunksize = r_stripe ? ldm_get_vnum (buffer+r_parent+0x2E) : 0;
770
Richard Knutsson130c6b92006-09-30 23:27:15 -0700771 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/**
775 * ldm_parse_dgr3 - Read a raw VBLK Disk Group object into a vblk structure
776 * @buffer: Block of data being worked on
777 * @buflen: Size of the block of data
778 * @vb: In-memory vblk in which to return information
779 *
780 * Read a raw VBLK Disk Group object (version 3) into a vblk structure.
781 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700782 * Return: 'true' @vb contains a Disk Group VBLK
783 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785static int ldm_parse_dgr3 (const u8 *buffer, int buflen, struct vblk *vb)
786{
787 int r_objid, r_name, r_diskid, r_id1, r_id2, len;
788 struct vblk_dgrp *dgrp;
789
790 BUG_ON (!buffer || !vb);
791
792 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
793 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
794 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
795
796 if (buffer[0x12] & VBLK_FLAG_DGR3_IDS) {
797 r_id1 = ldm_relative (buffer, buflen, 0x24, r_diskid);
798 r_id2 = ldm_relative (buffer, buflen, 0x24, r_id1);
799 len = r_id2;
800 } else {
801 r_id1 = 0;
802 r_id2 = 0;
803 len = r_diskid;
804 }
805 if (len < 0)
Richard Knutsson130c6b92006-09-30 23:27:15 -0700806 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 len += VBLK_SIZE_DGR3;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700809 if (len != get_unaligned_be32(buffer + 0x14))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700810 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 dgrp = &vb->vblk.dgrp;
813 ldm_get_vstr (buffer + 0x18 + r_name, dgrp->disk_id,
814 sizeof (dgrp->disk_id));
Richard Knutsson130c6b92006-09-30 23:27:15 -0700815 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/**
819 * ldm_parse_dgr4 - Read a raw VBLK Disk Group object into a vblk structure
820 * @buffer: Block of data being worked on
821 * @buflen: Size of the block of data
822 * @vb: In-memory vblk in which to return information
823 *
824 * Read a raw VBLK Disk Group object (version 4) into a vblk structure.
825 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700826 * Return: 'true' @vb contains a Disk Group VBLK
827 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700829static bool ldm_parse_dgr4 (const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 char buf[64];
832 int r_objid, r_name, r_id1, r_id2, len;
833 struct vblk_dgrp *dgrp;
834
835 BUG_ON (!buffer || !vb);
836
837 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
838 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
839
840 if (buffer[0x12] & VBLK_FLAG_DGR4_IDS) {
841 r_id1 = ldm_relative (buffer, buflen, 0x44, r_name);
842 r_id2 = ldm_relative (buffer, buflen, 0x44, r_id1);
843 len = r_id2;
844 } else {
845 r_id1 = 0;
846 r_id2 = 0;
847 len = r_name;
848 }
849 if (len < 0)
Richard Knutsson130c6b92006-09-30 23:27:15 -0700850 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 len += VBLK_SIZE_DGR4;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700853 if (len != get_unaligned_be32(buffer + 0x14))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700854 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 dgrp = &vb->vblk.dgrp;
857
858 ldm_get_vstr (buffer + 0x18 + r_objid, buf, sizeof (buf));
Richard Knutsson130c6b92006-09-30 23:27:15 -0700859 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/**
863 * ldm_parse_dsk3 - Read a raw VBLK Disk object into a vblk structure
864 * @buffer: Block of data being worked on
865 * @buflen: Size of the block of data
866 * @vb: In-memory vblk in which to return information
867 *
868 * Read a raw VBLK Disk object (version 3) into a vblk structure.
869 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700870 * Return: 'true' @vb contains a Disk VBLK
871 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700873static bool ldm_parse_dsk3 (const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 int r_objid, r_name, r_diskid, r_altname, len;
876 struct vblk_disk *disk;
877
878 BUG_ON (!buffer || !vb);
879
880 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
881 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
882 r_diskid = ldm_relative (buffer, buflen, 0x18, r_name);
883 r_altname = ldm_relative (buffer, buflen, 0x18, r_diskid);
884 len = r_altname;
885 if (len < 0)
Richard Knutsson130c6b92006-09-30 23:27:15 -0700886 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 len += VBLK_SIZE_DSK3;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700889 if (len != get_unaligned_be32(buffer + 0x14))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700890 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 disk = &vb->vblk.disk;
893 ldm_get_vstr (buffer + 0x18 + r_diskid, disk->alt_name,
894 sizeof (disk->alt_name));
Andy Shevchenko7244ad62016-05-20 17:01:27 -0700895 if (uuid_be_to_bin(buffer + 0x19 + r_name, (uuid_be *)disk->disk_id))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700896 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Richard Knutsson130c6b92006-09-30 23:27:15 -0700898 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/**
902 * ldm_parse_dsk4 - Read a raw VBLK Disk object into a vblk structure
903 * @buffer: Block of data being worked on
904 * @buflen: Size of the block of data
905 * @vb: In-memory vblk in which to return information
906 *
907 * Read a raw VBLK Disk object (version 4) into a vblk structure.
908 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700909 * Return: 'true' @vb contains a Disk VBLK
910 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Richard Knutsson130c6b92006-09-30 23:27:15 -0700912static bool ldm_parse_dsk4 (const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 int r_objid, r_name, len;
915 struct vblk_disk *disk;
916
917 BUG_ON (!buffer || !vb);
918
919 r_objid = ldm_relative (buffer, buflen, 0x18, 0);
920 r_name = ldm_relative (buffer, buflen, 0x18, r_objid);
921 len = r_name;
922 if (len < 0)
Richard Knutsson130c6b92006-09-30 23:27:15 -0700923 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 len += VBLK_SIZE_DSK4;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700926 if (len != get_unaligned_be32(buffer + 0x14))
Richard Knutsson130c6b92006-09-30 23:27:15 -0700927 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 disk = &vb->vblk.disk;
930 memcpy (disk->disk_id, buffer + 0x18 + r_name, GUID_SIZE);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700931 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934/**
935 * ldm_parse_prt3 - Read a raw VBLK Partition object into a vblk structure
936 * @buffer: Block of data being worked on
937 * @buflen: Size of the block of data
938 * @vb: In-memory vblk in which to return information
939 *
940 * Read a raw VBLK Partition object (version 3) into a vblk structure.
941 *
Richard Knutsson130c6b92006-09-30 23:27:15 -0700942 * Return: 'true' @vb contains a Partition VBLK
943 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100945static bool ldm_parse_prt3(const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 int r_objid, r_name, r_size, r_parent, r_diskid, r_index, len;
948 struct vblk_part *part;
949
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100950 BUG_ON(!buffer || !vb);
951 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
952 if (r_objid < 0) {
953 ldm_error("r_objid %d < 0", r_objid);
954 return false;
955 }
956 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
957 if (r_name < 0) {
958 ldm_error("r_name %d < 0", r_name);
959 return false;
960 }
961 r_size = ldm_relative(buffer, buflen, 0x34, r_name);
962 if (r_size < 0) {
963 ldm_error("r_size %d < 0", r_size);
964 return false;
965 }
966 r_parent = ldm_relative(buffer, buflen, 0x34, r_size);
967 if (r_parent < 0) {
968 ldm_error("r_parent %d < 0", r_parent);
969 return false;
970 }
971 r_diskid = ldm_relative(buffer, buflen, 0x34, r_parent);
972 if (r_diskid < 0) {
973 ldm_error("r_diskid %d < 0", r_diskid);
974 return false;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (buffer[0x12] & VBLK_FLAG_PART_INDEX) {
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100977 r_index = ldm_relative(buffer, buflen, 0x34, r_diskid);
978 if (r_index < 0) {
979 ldm_error("r_index %d < 0", r_index);
980 return false;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 len = r_index;
983 } else {
984 r_index = 0;
985 len = r_diskid;
986 }
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100987 if (len < 0) {
988 ldm_error("len %d < 0", len);
Richard Knutsson130c6b92006-09-30 23:27:15 -0700989 return false;
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 len += VBLK_SIZE_PRT3;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700992 if (len > get_unaligned_be32(buffer + 0x14)) {
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100993 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700994 get_unaligned_be32(buffer + 0x14));
Richard Knutsson130c6b92006-09-30 23:27:15 -0700995 return false;
Anton Altaparmakovdde33342007-05-21 09:37:42 +0100996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 part = &vb->vblk.part;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -0700998 part->start = get_unaligned_be64(buffer + 0x24 + r_name);
999 part->volume_offset = get_unaligned_be64(buffer + 0x2C + r_name);
Anton Altaparmakovdde33342007-05-21 09:37:42 +01001000 part->size = ldm_get_vnum(buffer + 0x34 + r_name);
1001 part->parent_id = ldm_get_vnum(buffer + 0x34 + r_size);
1002 part->disk_id = ldm_get_vnum(buffer + 0x34 + r_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (vb->flags & VBLK_FLAG_PART_INDEX)
1004 part->partnum = buffer[0x35 + r_diskid];
1005 else
1006 part->partnum = 0;
Richard Knutsson130c6b92006-09-30 23:27:15 -07001007 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010/**
1011 * ldm_parse_vol5 - Read a raw VBLK Volume object into a vblk structure
1012 * @buffer: Block of data being worked on
1013 * @buflen: Size of the block of data
1014 * @vb: In-memory vblk in which to return information
1015 *
1016 * Read a raw VBLK Volume object (version 5) into a vblk structure.
1017 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001018 * Return: 'true' @vb contains a Volume VBLK
1019 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001021static bool ldm_parse_vol5(const u8 *buffer, int buflen, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001023 int r_objid, r_name, r_vtype, r_disable_drive_letter, r_child, r_size;
1024 int r_id1, r_id2, r_size2, r_drive, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct vblk_volu *volu;
1026
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001027 BUG_ON(!buffer || !vb);
1028 r_objid = ldm_relative(buffer, buflen, 0x18, 0);
1029 if (r_objid < 0) {
1030 ldm_error("r_objid %d < 0", r_objid);
1031 return false;
1032 }
1033 r_name = ldm_relative(buffer, buflen, 0x18, r_objid);
1034 if (r_name < 0) {
1035 ldm_error("r_name %d < 0", r_name);
1036 return false;
1037 }
1038 r_vtype = ldm_relative(buffer, buflen, 0x18, r_name);
1039 if (r_vtype < 0) {
1040 ldm_error("r_vtype %d < 0", r_vtype);
1041 return false;
1042 }
1043 r_disable_drive_letter = ldm_relative(buffer, buflen, 0x18, r_vtype);
1044 if (r_disable_drive_letter < 0) {
1045 ldm_error("r_disable_drive_letter %d < 0",
1046 r_disable_drive_letter);
1047 return false;
1048 }
1049 r_child = ldm_relative(buffer, buflen, 0x2D, r_disable_drive_letter);
1050 if (r_child < 0) {
1051 ldm_error("r_child %d < 0", r_child);
1052 return false;
1053 }
1054 r_size = ldm_relative(buffer, buflen, 0x3D, r_child);
1055 if (r_size < 0) {
1056 ldm_error("r_size %d < 0", r_size);
1057 return false;
1058 }
1059 if (buffer[0x12] & VBLK_FLAG_VOLU_ID1) {
1060 r_id1 = ldm_relative(buffer, buflen, 0x52, r_size);
1061 if (r_id1 < 0) {
1062 ldm_error("r_id1 %d < 0", r_id1);
1063 return false;
1064 }
1065 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 r_id1 = r_size;
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001067 if (buffer[0x12] & VBLK_FLAG_VOLU_ID2) {
1068 r_id2 = ldm_relative(buffer, buflen, 0x52, r_id1);
1069 if (r_id2 < 0) {
1070 ldm_error("r_id2 %d < 0", r_id2);
1071 return false;
1072 }
1073 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 r_id2 = r_id1;
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001075 if (buffer[0x12] & VBLK_FLAG_VOLU_SIZE) {
1076 r_size2 = ldm_relative(buffer, buflen, 0x52, r_id2);
1077 if (r_size2 < 0) {
1078 ldm_error("r_size2 %d < 0", r_size2);
1079 return false;
1080 }
1081 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 r_size2 = r_id2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001084 r_drive = ldm_relative(buffer, buflen, 0x52, r_size2);
1085 if (r_drive < 0) {
1086 ldm_error("r_drive %d < 0", r_drive);
1087 return false;
1088 }
1089 } else
1090 r_drive = r_size2;
1091 len = r_drive;
1092 if (len < 0) {
1093 ldm_error("len %d < 0", len);
1094 return false;
1095 }
1096 len += VBLK_SIZE_VOL5;
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -07001097 if (len > get_unaligned_be32(buffer + 0x14)) {
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001098 ldm_error("len %d > BE32(buffer + 0x14) %d", len,
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -07001099 get_unaligned_be32(buffer + 0x14));
Anton Altaparmakov959bc2202007-07-16 19:39:02 +01001100 return false;
1101 }
1102 volu = &vb->vblk.volu;
1103 ldm_get_vstr(buffer + 0x18 + r_name, volu->volume_type,
1104 sizeof(volu->volume_type));
1105 memcpy(volu->volume_state, buffer + 0x18 + r_disable_drive_letter,
1106 sizeof(volu->volume_state));
1107 volu->size = ldm_get_vnum(buffer + 0x3D + r_child);
1108 volu->partition_type = buffer[0x41 + r_size];
1109 memcpy(volu->guid, buffer + 0x42 + r_size, sizeof(volu->guid));
1110 if (buffer[0x12] & VBLK_FLAG_VOLU_DRIVE) {
1111 ldm_get_vstr(buffer + 0x52 + r_size, volu->drive_hint,
1112 sizeof(volu->drive_hint));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Richard Knutsson130c6b92006-09-30 23:27:15 -07001114 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/**
1118 * ldm_parse_vblk - Read a raw VBLK object into a vblk structure
1119 * @buf: Block of data being worked on
1120 * @len: Size of the block of data
1121 * @vb: In-memory vblk in which to return information
1122 *
1123 * Read a raw VBLK object into a vblk structure. This function just reads the
1124 * information common to all VBLK types, then delegates the rest of the work to
1125 * helper functions: ldm_parse_*.
1126 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001127 * Return: 'true' @vb contains a VBLK
1128 * 'false' @vb contents are not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
Richard Knutsson130c6b92006-09-30 23:27:15 -07001130static bool ldm_parse_vblk (const u8 *buf, int len, struct vblk *vb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Richard Knutsson130c6b92006-09-30 23:27:15 -07001132 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int r_objid;
1134
1135 BUG_ON (!buf || !vb);
1136
1137 r_objid = ldm_relative (buf, len, 0x18, 0);
1138 if (r_objid < 0) {
1139 ldm_error ("VBLK header is corrupt.");
Richard Knutsson130c6b92006-09-30 23:27:15 -07001140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
1143 vb->flags = buf[0x12];
1144 vb->type = buf[0x13];
1145 vb->obj_id = ldm_get_vnum (buf + 0x18);
1146 ldm_get_vstr (buf+0x18+r_objid, vb->name, sizeof (vb->name));
1147
1148 switch (vb->type) {
1149 case VBLK_CMP3: result = ldm_parse_cmp3 (buf, len, vb); break;
1150 case VBLK_DSK3: result = ldm_parse_dsk3 (buf, len, vb); break;
1151 case VBLK_DSK4: result = ldm_parse_dsk4 (buf, len, vb); break;
1152 case VBLK_DGR3: result = ldm_parse_dgr3 (buf, len, vb); break;
1153 case VBLK_DGR4: result = ldm_parse_dgr4 (buf, len, vb); break;
1154 case VBLK_PRT3: result = ldm_parse_prt3 (buf, len, vb); break;
1155 case VBLK_VOL5: result = ldm_parse_vol5 (buf, len, vb); break;
1156 }
1157
1158 if (result)
1159 ldm_debug ("Parsed VBLK 0x%llx (type: 0x%02x) ok.",
1160 (unsigned long long) vb->obj_id, vb->type);
1161 else
1162 ldm_error ("Failed to parse VBLK 0x%llx (type: 0x%02x).",
1163 (unsigned long long) vb->obj_id, vb->type);
1164
1165 return result;
1166}
1167
1168
1169/**
1170 * ldm_ldmdb_add - Adds a raw VBLK entry to the ldmdb database
1171 * @data: Raw VBLK to add to the database
1172 * @len: Size of the raw VBLK
1173 * @ldb: Cache of the database structures
1174 *
1175 * The VBLKs are sorted into categories. Partitions are also sorted by offset.
1176 *
1177 * N.B. This function does not check the validity of the VBLKs.
1178 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001179 * Return: 'true' The VBLK was added
1180 * 'false' An error occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
Richard Knutsson130c6b92006-09-30 23:27:15 -07001182static bool ldm_ldmdb_add (u8 *data, int len, struct ldmdb *ldb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct vblk *vb;
1185 struct list_head *item;
1186
1187 BUG_ON (!data || !ldb);
1188
1189 vb = kmalloc (sizeof (*vb), GFP_KERNEL);
1190 if (!vb) {
1191 ldm_crit ("Out of memory.");
Richard Knutsson130c6b92006-09-30 23:27:15 -07001192 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
1195 if (!ldm_parse_vblk (data, len, vb)) {
1196 kfree(vb);
Richard Knutsson130c6b92006-09-30 23:27:15 -07001197 return false; /* Already logged */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
1200 /* Put vblk into the correct list. */
1201 switch (vb->type) {
1202 case VBLK_DGR3:
1203 case VBLK_DGR4:
1204 list_add (&vb->list, &ldb->v_dgrp);
1205 break;
1206 case VBLK_DSK3:
1207 case VBLK_DSK4:
1208 list_add (&vb->list, &ldb->v_disk);
1209 break;
1210 case VBLK_VOL5:
1211 list_add (&vb->list, &ldb->v_volu);
1212 break;
1213 case VBLK_CMP3:
1214 list_add (&vb->list, &ldb->v_comp);
1215 break;
1216 case VBLK_PRT3:
1217 /* Sort by the partition's start sector. */
1218 list_for_each (item, &ldb->v_part) {
1219 struct vblk *v = list_entry (item, struct vblk, list);
1220 if ((v->vblk.part.disk_id == vb->vblk.part.disk_id) &&
1221 (v->vblk.part.start > vb->vblk.part.start)) {
1222 list_add_tail (&vb->list, &v->list);
Richard Knutsson130c6b92006-09-30 23:27:15 -07001223 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 }
1226 list_add_tail (&vb->list, &ldb->v_part);
1227 break;
1228 }
Richard Knutsson130c6b92006-09-30 23:27:15 -07001229 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/**
1233 * ldm_frag_add - Add a VBLK fragment to a list
1234 * @data: Raw fragment to be added to the list
1235 * @size: Size of the raw fragment
1236 * @frags: Linked list of VBLK fragments
1237 *
1238 * Fragmented VBLKs may not be consecutive in the database, so they are placed
1239 * in a list so they can be pieced together later.
1240 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001241 * Return: 'true' Success, the VBLK was added to the list
1242 * 'false' Error, a problem occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Richard Knutsson130c6b92006-09-30 23:27:15 -07001244static bool ldm_frag_add (const u8 *data, int size, struct list_head *frags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct frag *f;
1247 struct list_head *item;
1248 int rec, num, group;
1249
1250 BUG_ON (!data || !frags);
1251
Timo Warnsc340b1d2011-04-14 15:21:56 -07001252 if (size < 2 * VBLK_SIZE_HEAD) {
1253 ldm_error("Value of size is to small.");
1254 return false;
1255 }
1256
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -07001257 group = get_unaligned_be32(data + 0x08);
1258 rec = get_unaligned_be16(data + 0x0C);
1259 num = get_unaligned_be16(data + 0x0E);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if ((num < 1) || (num > 4)) {
1261 ldm_error ("A VBLK claims to have %d parts.", num);
Richard Knutsson130c6b92006-09-30 23:27:15 -07001262 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Timo Warnsc340b1d2011-04-14 15:21:56 -07001264 if (rec >= num) {
1265 ldm_error("REC value (%d) exceeds NUM value (%d)", rec, num);
1266 return false;
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 list_for_each (item, frags) {
1270 f = list_entry (item, struct frag, list);
1271 if (f->group == group)
1272 goto found;
1273 }
1274
1275 f = kmalloc (sizeof (*f) + size*num, GFP_KERNEL);
1276 if (!f) {
1277 ldm_crit ("Out of memory.");
Richard Knutsson130c6b92006-09-30 23:27:15 -07001278 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 f->group = group;
1282 f->num = num;
1283 f->rec = rec;
1284 f->map = 0xFF << num;
1285
1286 list_add_tail (&f->list, frags);
1287found:
Timo Warnscae13fe2011-05-19 09:24:17 +02001288 if (rec >= f->num) {
1289 ldm_error("REC value (%d) exceeds NUM value (%d)", rec, f->num);
1290 return false;
1291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (f->map & (1 << rec)) {
1293 ldm_error ("Duplicate VBLK, part %d.", rec);
1294 f->map &= 0x7F; /* Mark the group as broken */
Richard Knutsson130c6b92006-09-30 23:27:15 -07001295 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 f->map |= (1 << rec);
Anton Altaparmakov97387e32012-02-24 09:37:42 +00001298 if (!rec)
1299 memcpy(f->data, data, VBLK_SIZE_HEAD);
Timo Warnsc340b1d2011-04-14 15:21:56 -07001300 data += VBLK_SIZE_HEAD;
1301 size -= VBLK_SIZE_HEAD;
Anton Altaparmakov97387e32012-02-24 09:37:42 +00001302 memcpy(f->data + VBLK_SIZE_HEAD + rec * size, data, size);
Richard Knutsson130c6b92006-09-30 23:27:15 -07001303 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/**
1307 * ldm_frag_free - Free a linked list of VBLK fragments
1308 * @list: Linked list of fragments
1309 *
1310 * Free a linked list of VBLK fragments
1311 *
1312 * Return: none
1313 */
1314static void ldm_frag_free (struct list_head *list)
1315{
1316 struct list_head *item, *tmp;
1317
1318 BUG_ON (!list);
1319
1320 list_for_each_safe (item, tmp, list)
1321 kfree (list_entry (item, struct frag, list));
1322}
1323
1324/**
1325 * ldm_frag_commit - Validate fragmented VBLKs and add them to the database
1326 * @frags: Linked list of VBLK fragments
1327 * @ldb: Cache of the database structures
1328 *
1329 * Now that all the fragmented VBLKs have been collected, they must be added to
1330 * the database for later use.
1331 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001332 * Return: 'true' All the fragments we added successfully
1333 * 'false' One or more of the fragments we invalid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Richard Knutsson130c6b92006-09-30 23:27:15 -07001335static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct frag *f;
1338 struct list_head *item;
1339
1340 BUG_ON (!frags || !ldb);
1341
1342 list_for_each (item, frags) {
1343 f = list_entry (item, struct frag, list);
1344
1345 if (f->map != 0xFF) {
1346 ldm_error ("VBLK group %d is incomplete (0x%02x).",
1347 f->group, f->map);
Richard Knutsson130c6b92006-09-30 23:27:15 -07001348 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 if (!ldm_ldmdb_add (f->data, f->num*ldb->vm.vblk_size, ldb))
Richard Knutsson130c6b92006-09-30 23:27:15 -07001352 return false; /* Already logged */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Richard Knutsson130c6b92006-09-30 23:27:15 -07001354 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357/**
1358 * ldm_get_vblks - Read the on-disk database of VBLKs into memory
Tejun Heo1493bf22010-05-15 20:09:30 +02001359 * @state: Partition check state including device holding the LDM Database
1360 * @base: Offset, into @state->bdev, of the database
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * @ldb: Cache of the database structures
1362 *
1363 * To use the information from the VBLKs, they need to be read from the disk,
1364 * unpacked and validated. We cache them in @ldb according to their type.
1365 *
Richard Knutsson130c6b92006-09-30 23:27:15 -07001366 * Return: 'true' All the VBLKs were read successfully
1367 * 'false' An error occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
Tejun Heo1493bf22010-05-15 20:09:30 +02001369static bool ldm_get_vblks(struct parsed_partitions *state, unsigned long base,
1370 struct ldmdb *ldb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 int size, perbuf, skip, finish, s, v, recs;
1373 u8 *data = NULL;
1374 Sector sect;
Richard Knutsson130c6b92006-09-30 23:27:15 -07001375 bool result = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 LIST_HEAD (frags);
1377
Tejun Heo1493bf22010-05-15 20:09:30 +02001378 BUG_ON(!state || !ldb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 size = ldb->vm.vblk_size;
1381 perbuf = 512 / size;
1382 skip = ldb->vm.vblk_offset >> 9; /* Bytes to sectors */
1383 finish = (size * ldb->vm.last_vblk_seq) >> 9;
1384
1385 for (s = skip; s < finish; s++) { /* For each sector */
Tejun Heo1493bf22010-05-15 20:09:30 +02001386 data = read_part_sector(state, base + OFF_VMDB + s, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (!data) {
1388 ldm_crit ("Disk read failed.");
1389 goto out;
1390 }
1391
1392 for (v = 0; v < perbuf; v++, data+=size) { /* For each vblk */
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -07001393 if (MAGIC_VBLK != get_unaligned_be32(data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 ldm_error ("Expected to find a VBLK.");
1395 goto out;
1396 }
1397
Harvey Harrisonb7bbf8f2008-07-25 01:45:25 -07001398 recs = get_unaligned_be16(data + 0x0E); /* Number of records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (recs == 1) {
1400 if (!ldm_ldmdb_add (data, size, ldb))
1401 goto out; /* Already logged */
1402 } else if (recs > 1) {
1403 if (!ldm_frag_add (data, size, &frags))
1404 goto out; /* Already logged */
1405 }
1406 /* else Record is not in use, ignore it. */
1407 }
1408 put_dev_sector (sect);
1409 data = NULL;
1410 }
1411
1412 result = ldm_frag_commit (&frags, ldb); /* Failures, already logged */
1413out:
1414 if (data)
1415 put_dev_sector (sect);
1416 ldm_frag_free (&frags);
1417
1418 return result;
1419}
1420
1421/**
1422 * ldm_free_vblks - Free a linked list of vblk's
1423 * @lh: Head of a linked list of struct vblk
1424 *
1425 * Free a list of vblk's and free the memory used to maintain the list.
1426 *
1427 * Return: none
1428 */
1429static void ldm_free_vblks (struct list_head *lh)
1430{
1431 struct list_head *item, *tmp;
1432
1433 BUG_ON (!lh);
1434
1435 list_for_each_safe (item, tmp, lh)
1436 kfree (list_entry (item, struct vblk, list));
1437}
1438
1439
1440/**
1441 * ldm_partition - Find out whether a device is a dynamic disk and handle it
Tejun Heo1493bf22010-05-15 20:09:30 +02001442 * @state: Partition check state including device holding the LDM Database
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *
1444 * This determines whether the device @bdev is a dynamic disk and if so creates
1445 * the partitions necessary in the gendisk structure pointed to by @hd.
1446 *
1447 * We create a dummy device 1, which contains the LDM database, and then create
1448 * each partition described by the LDM database in sequence as devices 2+. For
1449 * example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
1450 * and so on: the actual data containing partitions.
1451 *
Tejun Heo1493bf22010-05-15 20:09:30 +02001452 * Return: 1 Success, @state->bdev is a dynamic disk and we handled it
1453 * 0 Success, @state->bdev is not a dynamic disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 * -1 An error occurred before enough information had been read
Tejun Heo1493bf22010-05-15 20:09:30 +02001455 * Or @state->bdev is a dynamic disk, but it may be corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
Tejun Heo1493bf22010-05-15 20:09:30 +02001457int ldm_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct ldmdb *ldb;
1460 unsigned long base;
1461 int result = -1;
1462
Tejun Heo1493bf22010-05-15 20:09:30 +02001463 BUG_ON(!state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* Look for signs of a Dynamic Disk */
Tejun Heo1493bf22010-05-15 20:09:30 +02001466 if (!ldm_validate_partition_table(state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468
1469 ldb = kmalloc (sizeof (*ldb), GFP_KERNEL);
1470 if (!ldb) {
1471 ldm_crit ("Out of memory.");
1472 goto out;
1473 }
1474
1475 /* Parse and check privheads. */
Tejun Heo1493bf22010-05-15 20:09:30 +02001476 if (!ldm_validate_privheads(state, &ldb->ph))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto out; /* Already logged */
1478
1479 /* All further references are relative to base (database start). */
1480 base = ldb->ph.config_start;
1481
1482 /* Parse and check tocs and vmdb. */
Tejun Heo1493bf22010-05-15 20:09:30 +02001483 if (!ldm_validate_tocblocks(state, base, ldb) ||
1484 !ldm_validate_vmdb(state, base, ldb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out; /* Already logged */
1486
1487 /* Initialize vblk lists in ldmdb struct */
1488 INIT_LIST_HEAD (&ldb->v_dgrp);
1489 INIT_LIST_HEAD (&ldb->v_disk);
1490 INIT_LIST_HEAD (&ldb->v_volu);
1491 INIT_LIST_HEAD (&ldb->v_comp);
1492 INIT_LIST_HEAD (&ldb->v_part);
1493
Tejun Heo1493bf22010-05-15 20:09:30 +02001494 if (!ldm_get_vblks(state, base, ldb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ldm_crit ("Failed to read the VBLKs from the database.");
1496 goto cleanup;
1497 }
1498
1499 /* Finally, create the data partition devices. */
Tejun Heo1493bf22010-05-15 20:09:30 +02001500 if (ldm_create_data_partitions(state, ldb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 ldm_debug ("Parsed LDM database successfully.");
1502 result = 1;
1503 }
1504 /* else Already logged */
1505
1506cleanup:
1507 ldm_free_vblks (&ldb->v_dgrp);
1508 ldm_free_vblks (&ldb->v_disk);
1509 ldm_free_vblks (&ldb->v_volu);
1510 ldm_free_vblks (&ldb->v_comp);
1511 ldm_free_vblks (&ldb->v_part);
1512out:
1513 kfree (ldb);
1514 return result;
1515}