ldm.c 41 KB

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