ibm.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  3. * Volker Sameske <sameske@de.ibm.com>
  4. * Bugreports.to..: <Linux390@de.ibm.com>
  5. * Copyright IBM Corp. 1999, 2012
  6. */
  7. #include <linux/buffer_head.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/slab.h>
  10. #include <asm/dasd.h>
  11. #include <asm/ebcdic.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/vtoc.h>
  14. #include "check.h"
  15. #include "ibm.h"
  16. union label_t {
  17. struct vtoc_volume_label_cdl vol;
  18. struct vtoc_volume_label_ldl lnx;
  19. struct vtoc_cms_label cms;
  20. };
  21. /*
  22. * compute the block number from a
  23. * cyl-cyl-head-head structure
  24. */
  25. static sector_t cchh2blk(struct vtoc_cchh *ptr, struct hd_geometry *geo)
  26. {
  27. sector_t cyl;
  28. __u16 head;
  29. /* decode cylinder and heads for large volumes */
  30. cyl = ptr->hh & 0xFFF0;
  31. cyl <<= 12;
  32. cyl |= ptr->cc;
  33. head = ptr->hh & 0x000F;
  34. return cyl * geo->heads * geo->sectors +
  35. head * geo->sectors;
  36. }
  37. /*
  38. * compute the block number from a
  39. * cyl-cyl-head-head-block structure
  40. */
  41. static sector_t cchhb2blk(struct vtoc_cchhb *ptr, struct hd_geometry *geo)
  42. {
  43. sector_t cyl;
  44. __u16 head;
  45. /* decode cylinder and heads for large volumes */
  46. cyl = ptr->hh & 0xFFF0;
  47. cyl <<= 12;
  48. cyl |= ptr->cc;
  49. head = ptr->hh & 0x000F;
  50. return cyl * geo->heads * geo->sectors +
  51. head * geo->sectors +
  52. ptr->b;
  53. }
  54. static int find_label(struct parsed_partitions *state,
  55. dasd_information2_t *info,
  56. struct hd_geometry *geo,
  57. int blocksize,
  58. sector_t *labelsect,
  59. char name[],
  60. char type[],
  61. union label_t *label)
  62. {
  63. Sector sect;
  64. unsigned char *data;
  65. sector_t testsect[3];
  66. unsigned char temp[5];
  67. int found = 0;
  68. int i, testcount;
  69. /* There a three places where we may find a valid label:
  70. * - on an ECKD disk it's block 2
  71. * - on an FBA disk it's block 1
  72. * - on an CMS formatted FBA disk it is sector 1, even if the block size
  73. * is larger than 512 bytes (possible if the DIAG discipline is used)
  74. * If we have a valid info structure, then we know exactly which case we
  75. * have, otherwise we just search through all possebilities.
  76. */
  77. if (info) {
  78. if ((info->cu_type == 0x6310 && info->dev_type == 0x9336) ||
  79. (info->cu_type == 0x3880 && info->dev_type == 0x3370))
  80. testsect[0] = info->label_block;
  81. else
  82. testsect[0] = info->label_block * (blocksize >> 9);
  83. testcount = 1;
  84. } else {
  85. testsect[0] = 1;
  86. testsect[1] = (blocksize >> 9);
  87. testsect[2] = 2 * (blocksize >> 9);
  88. testcount = 3;
  89. }
  90. for (i = 0; i < testcount; ++i) {
  91. data = read_part_sector(state, testsect[i], &sect);
  92. if (data == NULL)
  93. continue;
  94. memcpy(label, data, sizeof(*label));
  95. memcpy(temp, data, 4);
  96. temp[4] = 0;
  97. EBCASC(temp, 4);
  98. put_dev_sector(sect);
  99. if (!strcmp(temp, "VOL1") ||
  100. !strcmp(temp, "LNX1") ||
  101. !strcmp(temp, "CMS1")) {
  102. if (!strcmp(temp, "VOL1")) {
  103. strncpy(type, label->vol.vollbl, 4);
  104. strncpy(name, label->vol.volid, 6);
  105. } else {
  106. strncpy(type, label->lnx.vollbl, 4);
  107. strncpy(name, label->lnx.volid, 6);
  108. }
  109. EBCASC(type, 4);
  110. EBCASC(name, 6);
  111. *labelsect = testsect[i];
  112. found = 1;
  113. break;
  114. }
  115. }
  116. if (!found)
  117. memset(label, 0, sizeof(*label));
  118. return found;
  119. }
  120. static int find_vol1_partitions(struct parsed_partitions *state,
  121. struct hd_geometry *geo,
  122. int blocksize,
  123. char name[],
  124. union label_t *label)
  125. {
  126. sector_t blk;
  127. int counter;
  128. char tmp[64];
  129. Sector sect;
  130. unsigned char *data;
  131. loff_t offset, size;
  132. struct vtoc_format1_label f1;
  133. int secperblk;
  134. snprintf(tmp, sizeof(tmp), "VOL1/%8s:", name);
  135. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  136. /*
  137. * get start of VTOC from the disk label and then search for format1
  138. * and format8 labels
  139. */
  140. secperblk = blocksize >> 9;
  141. blk = cchhb2blk(&label->vol.vtoc, geo) + 1;
  142. counter = 0;
  143. data = read_part_sector(state, blk * secperblk, &sect);
  144. while (data != NULL) {
  145. memcpy(&f1, data, sizeof(struct vtoc_format1_label));
  146. put_dev_sector(sect);
  147. /* skip FMT4 / FMT5 / FMT7 labels */
  148. if (f1.DS1FMTID == _ascebc['4']
  149. || f1.DS1FMTID == _ascebc['5']
  150. || f1.DS1FMTID == _ascebc['7']
  151. || f1.DS1FMTID == _ascebc['9']) {
  152. blk++;
  153. data = read_part_sector(state, blk * secperblk, &sect);
  154. continue;
  155. }
  156. /* only FMT1 and 8 labels valid at this point */
  157. if (f1.DS1FMTID != _ascebc['1'] &&
  158. f1.DS1FMTID != _ascebc['8'])
  159. break;
  160. /* OK, we got valid partition data */
  161. offset = cchh2blk(&f1.DS1EXT1.llimit, geo);
  162. size = cchh2blk(&f1.DS1EXT1.ulimit, geo) -
  163. offset + geo->sectors;
  164. offset *= secperblk;
  165. size *= secperblk;
  166. if (counter >= state->limit)
  167. break;
  168. put_partition(state, counter + 1, offset, size);
  169. counter++;
  170. blk++;
  171. data = read_part_sector(state, blk * secperblk, &sect);
  172. }
  173. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  174. if (!data)
  175. return -1;
  176. return 1;
  177. }
  178. static int find_lnx1_partitions(struct parsed_partitions *state,
  179. struct hd_geometry *geo,
  180. int blocksize,
  181. char name[],
  182. union label_t *label,
  183. sector_t labelsect,
  184. loff_t i_size,
  185. dasd_information2_t *info)
  186. {
  187. loff_t offset, geo_size, size;
  188. char tmp[64];
  189. int secperblk;
  190. snprintf(tmp, sizeof(tmp), "LNX1/%8s:", name);
  191. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  192. secperblk = blocksize >> 9;
  193. if (label->lnx.ldl_version == 0xf2) {
  194. size = label->lnx.formatted_blocks * secperblk;
  195. } else {
  196. /*
  197. * Formated w/o large volume support. If the sanity check
  198. * 'size based on geo == size based on i_size' is true, then
  199. * we can safely assume that we know the formatted size of
  200. * the disk, otherwise we need additional information
  201. * that we can only get from a real DASD device.
  202. */
  203. geo_size = geo->cylinders * geo->heads
  204. * geo->sectors * secperblk;
  205. size = i_size >> 9;
  206. if (size != geo_size) {
  207. if (!info) {
  208. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  209. return 1;
  210. }
  211. if (!strcmp(info->type, "ECKD"))
  212. if (geo_size < size)
  213. size = geo_size;
  214. /* else keep size based on i_size */
  215. }
  216. }
  217. /* first and only partition starts in the first block after the label */
  218. offset = labelsect + secperblk;
  219. put_partition(state, 1, offset, size - offset);
  220. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  221. return 1;
  222. }
  223. static int find_cms1_partitions(struct parsed_partitions *state,
  224. struct hd_geometry *geo,
  225. int blocksize,
  226. char name[],
  227. union label_t *label,
  228. sector_t labelsect)
  229. {
  230. loff_t offset, size;
  231. char tmp[64];
  232. int secperblk;
  233. /*
  234. * VM style CMS1 labeled disk
  235. */
  236. blocksize = label->cms.block_size;
  237. secperblk = blocksize >> 9;
  238. if (label->cms.disk_offset != 0) {
  239. snprintf(tmp, sizeof(tmp), "CMS1/%8s(MDSK):", name);
  240. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  241. /* disk is reserved minidisk */
  242. offset = label->cms.disk_offset * secperblk;
  243. size = (label->cms.block_count - 1) * secperblk;
  244. } else {
  245. snprintf(tmp, sizeof(tmp), "CMS1/%8s:", name);
  246. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  247. /*
  248. * Special case for FBA devices:
  249. * If an FBA device is CMS formatted with blocksize > 512 byte
  250. * and the DIAG discipline is used, then the CMS label is found
  251. * in sector 1 instead of block 1. However, the partition is
  252. * still supposed to start in block 2.
  253. */
  254. if (labelsect == 1)
  255. offset = 2 * secperblk;
  256. else
  257. offset = labelsect + secperblk;
  258. size = label->cms.block_count * secperblk;
  259. }
  260. put_partition(state, 1, offset, size-offset);
  261. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  262. return 1;
  263. }
  264. /*
  265. * This is the main function, called by check.c
  266. */
  267. int ibm_partition(struct parsed_partitions *state)
  268. {
  269. struct block_device *bdev = state->bdev;
  270. int blocksize, res;
  271. loff_t i_size, offset, size;
  272. dasd_information2_t *info;
  273. struct hd_geometry *geo;
  274. char type[5] = {0,};
  275. char name[7] = {0,};
  276. sector_t labelsect;
  277. union label_t *label;
  278. res = 0;
  279. blocksize = bdev_logical_block_size(bdev);
  280. if (blocksize <= 0)
  281. goto out_exit;
  282. i_size = i_size_read(bdev->bd_inode);
  283. if (i_size == 0)
  284. goto out_exit;
  285. info = kmalloc(sizeof(dasd_information2_t), GFP_KERNEL);
  286. if (info == NULL)
  287. goto out_exit;
  288. geo = kmalloc(sizeof(struct hd_geometry), GFP_KERNEL);
  289. if (geo == NULL)
  290. goto out_nogeo;
  291. label = kmalloc(sizeof(union label_t), GFP_KERNEL);
  292. if (label == NULL)
  293. goto out_nolab;
  294. if (ioctl_by_bdev(bdev, HDIO_GETGEO, (unsigned long)geo) != 0)
  295. goto out_freeall;
  296. if (ioctl_by_bdev(bdev, BIODASDINFO2, (unsigned long)info) != 0) {
  297. kfree(info);
  298. info = NULL;
  299. }
  300. if (find_label(state, info, geo, blocksize, &labelsect, name, type,
  301. label)) {
  302. if (!strncmp(type, "VOL1", 4)) {
  303. res = find_vol1_partitions(state, geo, blocksize, name,
  304. label);
  305. } else if (!strncmp(type, "LNX1", 4)) {
  306. res = find_lnx1_partitions(state, geo, blocksize, name,
  307. label, labelsect, i_size,
  308. info);
  309. } else if (!strncmp(type, "CMS1", 4)) {
  310. res = find_cms1_partitions(state, geo, blocksize, name,
  311. label, labelsect);
  312. }
  313. } else if (info) {
  314. /*
  315. * ugly but needed for backward compatibility:
  316. * If the block device is a DASD (i.e. BIODASDINFO2 works),
  317. * then we claim it in any case, even though it has no valid
  318. * label. If it has the LDL format, then we simply define a
  319. * partition as if it had an LNX1 label.
  320. */
  321. res = 1;
  322. if (info->format == DASD_FORMAT_LDL) {
  323. strlcat(state->pp_buf, "(nonl)", PAGE_SIZE);
  324. size = i_size >> 9;
  325. offset = (info->label_block + 1) * (blocksize >> 9);
  326. put_partition(state, 1, offset, size-offset);
  327. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  328. }
  329. } else
  330. res = 0;
  331. out_freeall:
  332. kfree(label);
  333. out_nolab:
  334. kfree(geo);
  335. out_nogeo:
  336. kfree(info);
  337. out_exit:
  338. return res;
  339. }