ldm.c 41 KB

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