ibm.c 9.4 KB

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