ldm.c 42 KB

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