ibm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <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. /* Volume Label Type/ID Length */
  57. #define DASD_VOL_TYPE_LEN 4
  58. #define DASD_VOL_ID_LEN 6
  59. /* Volume Label Types */
  60. #define DASD_VOLLBL_TYPE_VOL1 0
  61. #define DASD_VOLLBL_TYPE_LNX1 1
  62. #define DASD_VOLLBL_TYPE_CMS1 2
  63. struct dasd_vollabel {
  64. char *type;
  65. int idx;
  66. };
  67. static struct dasd_vollabel dasd_vollabels[] = {
  68. [DASD_VOLLBL_TYPE_VOL1] = {
  69. .type = "VOL1",
  70. .idx = DASD_VOLLBL_TYPE_VOL1,
  71. },
  72. [DASD_VOLLBL_TYPE_LNX1] = {
  73. .type = "LNX1",
  74. .idx = DASD_VOLLBL_TYPE_LNX1,
  75. },
  76. [DASD_VOLLBL_TYPE_CMS1] = {
  77. .type = "CMS1",
  78. .idx = DASD_VOLLBL_TYPE_CMS1,
  79. },
  80. };
  81. static int get_label_by_type(const char *type)
  82. {
  83. int i;
  84. for (i = 0; i < ARRAY_SIZE(dasd_vollabels); i++) {
  85. if (!memcmp(type, dasd_vollabels[i].type, DASD_VOL_TYPE_LEN))
  86. return dasd_vollabels[i].idx;
  87. }
  88. return -1;
  89. }
  90. static int find_label(struct parsed_partitions *state,
  91. dasd_information2_t *info,
  92. struct hd_geometry *geo,
  93. int blocksize,
  94. sector_t *labelsect,
  95. char name[],
  96. char type[],
  97. union label_t *label)
  98. {
  99. sector_t testsect[3];
  100. int i, testcount;
  101. Sector sect;
  102. void *data;
  103. /* There a three places where we may find a valid label:
  104. * - on an ECKD disk it's block 2
  105. * - on an FBA disk it's block 1
  106. * - on an CMS formatted FBA disk it is sector 1, even if the block size
  107. * is larger than 512 bytes (possible if the DIAG discipline is used)
  108. * If we have a valid info structure, then we know exactly which case we
  109. * have, otherwise we just search through all possebilities.
  110. */
  111. if (info) {
  112. if ((info->cu_type == 0x6310 && info->dev_type == 0x9336) ||
  113. (info->cu_type == 0x3880 && info->dev_type == 0x3370))
  114. testsect[0] = info->label_block;
  115. else
  116. testsect[0] = info->label_block * (blocksize >> 9);
  117. testcount = 1;
  118. } else {
  119. testsect[0] = 1;
  120. testsect[1] = (blocksize >> 9);
  121. testsect[2] = 2 * (blocksize >> 9);
  122. testcount = 3;
  123. }
  124. for (i = 0; i < testcount; ++i) {
  125. data = read_part_sector(state, testsect[i], &sect);
  126. if (data == NULL)
  127. continue;
  128. memcpy(label, data, sizeof(*label));
  129. memcpy(type, data, DASD_VOL_TYPE_LEN);
  130. EBCASC(type, DASD_VOL_TYPE_LEN);
  131. put_dev_sector(sect);
  132. switch (get_label_by_type(type)) {
  133. case DASD_VOLLBL_TYPE_VOL1:
  134. memcpy(name, label->vol.volid, DASD_VOL_ID_LEN);
  135. EBCASC(name, DASD_VOL_ID_LEN);
  136. *labelsect = testsect[i];
  137. return 1;
  138. case DASD_VOLLBL_TYPE_LNX1:
  139. case DASD_VOLLBL_TYPE_CMS1:
  140. memcpy(name, label->lnx.volid, DASD_VOL_ID_LEN);
  141. EBCASC(name, DASD_VOL_ID_LEN);
  142. *labelsect = testsect[i];
  143. return 1;
  144. default:
  145. break;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int find_vol1_partitions(struct parsed_partitions *state,
  151. struct hd_geometry *geo,
  152. int blocksize,
  153. char name[],
  154. union label_t *label)
  155. {
  156. sector_t blk;
  157. int counter;
  158. char tmp[64];
  159. Sector sect;
  160. unsigned char *data;
  161. loff_t offset, size;
  162. struct vtoc_format1_label f1;
  163. int secperblk;
  164. snprintf(tmp, sizeof(tmp), "VOL1/%8s:", name);
  165. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  166. /*
  167. * get start of VTOC from the disk label and then search for format1
  168. * and format8 labels
  169. */
  170. secperblk = blocksize >> 9;
  171. blk = cchhb2blk(&label->vol.vtoc, geo) + 1;
  172. counter = 0;
  173. data = read_part_sector(state, blk * secperblk, &sect);
  174. while (data != NULL) {
  175. memcpy(&f1, data, sizeof(struct vtoc_format1_label));
  176. put_dev_sector(sect);
  177. /* skip FMT4 / FMT5 / FMT7 labels */
  178. if (f1.DS1FMTID == _ascebc['4']
  179. || f1.DS1FMTID == _ascebc['5']
  180. || f1.DS1FMTID == _ascebc['7']
  181. || f1.DS1FMTID == _ascebc['9']) {
  182. blk++;
  183. data = read_part_sector(state, blk * secperblk, &sect);
  184. continue;
  185. }
  186. /* only FMT1 and 8 labels valid at this point */
  187. if (f1.DS1FMTID != _ascebc['1'] &&
  188. f1.DS1FMTID != _ascebc['8'])
  189. break;
  190. /* OK, we got valid partition data */
  191. offset = cchh2blk(&f1.DS1EXT1.llimit, geo);
  192. size = cchh2blk(&f1.DS1EXT1.ulimit, geo) -
  193. offset + geo->sectors;
  194. offset *= secperblk;
  195. size *= secperblk;
  196. if (counter >= state->limit)
  197. break;
  198. put_partition(state, counter + 1, offset, size);
  199. counter++;
  200. blk++;
  201. data = read_part_sector(state, blk * secperblk, &sect);
  202. }
  203. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  204. if (!data)
  205. return -1;
  206. return 1;
  207. }
  208. static int find_lnx1_partitions(struct parsed_partitions *state,
  209. struct hd_geometry *geo,
  210. int blocksize,
  211. char name[],
  212. union label_t *label,
  213. sector_t labelsect,
  214. sector_t nr_sectors,
  215. dasd_information2_t *info)
  216. {
  217. loff_t offset, geo_size, size;
  218. char tmp[64];
  219. int secperblk;
  220. snprintf(tmp, sizeof(tmp), "LNX1/%8s:", name);
  221. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  222. secperblk = blocksize >> 9;
  223. if (label->lnx.ldl_version == 0xf2) {
  224. size = label->lnx.formatted_blocks * secperblk;
  225. } else {
  226. /*
  227. * Formated w/o large volume support. If the sanity check
  228. * 'size based on geo == size based on nr_sectors' is true, then
  229. * we can safely assume that we know the formatted size of
  230. * the disk, otherwise we need additional information
  231. * that we can only get from a real DASD device.
  232. */
  233. geo_size = geo->cylinders * geo->heads
  234. * geo->sectors * secperblk;
  235. size = nr_sectors;
  236. if (size != geo_size) {
  237. if (!info) {
  238. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  239. return 1;
  240. }
  241. if (!strcmp(info->type, "ECKD"))
  242. if (geo_size < size)
  243. size = geo_size;
  244. /* else keep size based on nr_sectors */
  245. }
  246. }
  247. /* first and only partition starts in the first block after the label */
  248. offset = labelsect + secperblk;
  249. put_partition(state, 1, offset, size - offset);
  250. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  251. return 1;
  252. }
  253. static int find_cms1_partitions(struct parsed_partitions *state,
  254. struct hd_geometry *geo,
  255. int blocksize,
  256. char name[],
  257. union label_t *label,
  258. sector_t labelsect)
  259. {
  260. loff_t offset, size;
  261. char tmp[64];
  262. int secperblk;
  263. /*
  264. * VM style CMS1 labeled disk
  265. */
  266. blocksize = label->cms.block_size;
  267. secperblk = blocksize >> 9;
  268. if (label->cms.disk_offset != 0) {
  269. snprintf(tmp, sizeof(tmp), "CMS1/%8s(MDSK):", name);
  270. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  271. /* disk is reserved minidisk */
  272. offset = label->cms.disk_offset * secperblk;
  273. size = (label->cms.block_count - 1) * secperblk;
  274. } else {
  275. snprintf(tmp, sizeof(tmp), "CMS1/%8s:", name);
  276. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  277. /*
  278. * Special case for FBA devices:
  279. * If an FBA device is CMS formatted with blocksize > 512 byte
  280. * and the DIAG discipline is used, then the CMS label is found
  281. * in sector 1 instead of block 1. However, the partition is
  282. * still supposed to start in block 2.
  283. */
  284. if (labelsect == 1)
  285. offset = 2 * secperblk;
  286. else
  287. offset = labelsect + secperblk;
  288. size = label->cms.block_count * secperblk;
  289. }
  290. put_partition(state, 1, offset, size-offset);
  291. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  292. return 1;
  293. }
  294. /*
  295. * This is the main function, called by check.c
  296. */
  297. int ibm_partition(struct parsed_partitions *state)
  298. {
  299. int (*fn)(struct gendisk *disk, dasd_information2_t *info);
  300. struct gendisk *disk = state->disk;
  301. struct block_device *bdev = disk->part0;
  302. int blocksize, res;
  303. loff_t offset, size;
  304. sector_t nr_sectors;
  305. dasd_information2_t *info;
  306. struct hd_geometry *geo;
  307. char type[DASD_VOL_TYPE_LEN + 1] = "";
  308. char name[DASD_VOL_ID_LEN + 1] = "";
  309. sector_t labelsect;
  310. union label_t *label;
  311. res = 0;
  312. if (!disk->fops->getgeo)
  313. goto out_exit;
  314. fn = symbol_get(dasd_biodasdinfo);
  315. blocksize = bdev_logical_block_size(bdev);
  316. if (blocksize <= 0)
  317. goto out_symbol;
  318. nr_sectors = bdev_nr_sectors(bdev);
  319. if (nr_sectors == 0)
  320. goto out_symbol;
  321. info = kmalloc_obj(dasd_information2_t);
  322. if (info == NULL)
  323. goto out_symbol;
  324. geo = kmalloc_obj(struct hd_geometry);
  325. if (geo == NULL)
  326. goto out_nogeo;
  327. label = kmalloc_obj(union label_t);
  328. if (label == NULL)
  329. goto out_nolab;
  330. /* set start if not filled by getgeo function e.g. virtblk */
  331. geo->start = get_start_sect(bdev);
  332. if (disk->fops->getgeo(disk, geo))
  333. goto out_freeall;
  334. if (!fn || fn(disk, info)) {
  335. kfree(info);
  336. info = NULL;
  337. }
  338. if (find_label(state, info, geo, blocksize, &labelsect, name, type, label)) {
  339. switch (get_label_by_type(type)) {
  340. case DASD_VOLLBL_TYPE_VOL1:
  341. res = find_vol1_partitions(state, geo, blocksize, name,
  342. label);
  343. break;
  344. case DASD_VOLLBL_TYPE_LNX1:
  345. res = find_lnx1_partitions(state, geo, blocksize, name,
  346. label, labelsect, nr_sectors,
  347. info);
  348. break;
  349. case DASD_VOLLBL_TYPE_CMS1:
  350. res = find_cms1_partitions(state, geo, blocksize, name,
  351. label, labelsect);
  352. break;
  353. }
  354. } else if (info) {
  355. /*
  356. * ugly but needed for backward compatibility:
  357. * If the block device is a DASD (i.e. BIODASDINFO2 works),
  358. * then we claim it in any case, even though it has no valid
  359. * label. If it has the LDL format, then we simply define a
  360. * partition as if it had an LNX1 label.
  361. */
  362. res = 1;
  363. if (info->format == DASD_FORMAT_LDL) {
  364. strlcat(state->pp_buf, "(nonl)", PAGE_SIZE);
  365. size = nr_sectors;
  366. offset = (info->label_block + 1) * (blocksize >> 9);
  367. put_partition(state, 1, offset, size-offset);
  368. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  369. }
  370. } else
  371. res = 0;
  372. out_freeall:
  373. kfree(label);
  374. out_nolab:
  375. kfree(geo);
  376. out_nogeo:
  377. kfree(info);
  378. out_symbol:
  379. if (fn)
  380. symbol_put(dasd_biodasdinfo);
  381. out_exit:
  382. return res;
  383. }