ldm.c 43 KB

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