ldm.c 42 KB

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