ibm.c 9.7 KB

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