Blame view

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

57881dd9d   Suzuki K P   [PATCH] Fix check...
297
  	res = 0;
e1defc4ff   Martin K. Petersen   block: Do away wi...
298
  	blocksize = bdev_logical_block_size(bdev);
90f0094dc   Horst Hummel   [PATCH] s390: das...
299
  	if (blocksize <= 0)
57881dd9d   Suzuki K P   [PATCH] Fix check...
300
  		goto out_exit;
90f0094dc   Horst Hummel   [PATCH] s390: das...
301
302
  	i_size = i_size_read(bdev->bd_inode);
  	if (i_size == 0)
57881dd9d   Suzuki K P   [PATCH] Fix check...
303
  		goto out_exit;
bf1a95a22   Stefan Haberland   [S390] fixed cdl-...
304
305
  	info = kmalloc(sizeof(dasd_information2_t), GFP_KERNEL);
  	if (info == NULL)
57881dd9d   Suzuki K P   [PATCH] Fix check...
306
  		goto out_exit;
bf1a95a22   Stefan Haberland   [S390] fixed cdl-...
307
308
  	geo = kmalloc(sizeof(struct hd_geometry), GFP_KERNEL);
  	if (geo == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
  		goto out_nogeo;
bf1a95a22   Stefan Haberland   [S390] fixed cdl-...
310
311
  	label = kmalloc(sizeof(union label_t), GFP_KERNEL);
  	if (label == NULL)
56dc6a88e   Peter Oberparleiter   [PATCH] s390: cms...
312
  		goto out_nolab;
46e889478   Stefan Weinhuber   s390/partitions: ...
313
  	if (ioctl_by_bdev(bdev, HDIO_GETGEO, (unsigned long)geo) != 0)
57881dd9d   Suzuki K P   [PATCH] Fix check...
314
  		goto out_freeall;
46e889478   Stefan Weinhuber   s390/partitions: ...
315
316
317
  	if (ioctl_by_bdev(bdev, BIODASDINFO2, (unsigned long)info) != 0) {
  		kfree(info);
  		info = NULL;
b44b0ab3b   Stefan Weinhuber   [S390] dasd: add ...
318
  	}
57881dd9d   Suzuki K P   [PATCH] Fix check...
319

46e889478   Stefan Weinhuber   s390/partitions: ...
320
321
322
323
324
325
326
327
328
329
330
331
  	if (find_label(state, info, geo, blocksize, &labelsect, name, type,
  		       label)) {
  		if (!strncmp(type, "VOL1", 4)) {
  			res = find_vol1_partitions(state, geo, blocksize, name,
  						   label);
  		} else if (!strncmp(type, "LNX1", 4)) {
  			res = find_lnx1_partitions(state, geo, blocksize, name,
  						   label, labelsect, i_size,
  						   info);
  		} else if (!strncmp(type, "CMS1", 4)) {
  			res = find_cms1_partitions(state, geo, blocksize, name,
  						   label, labelsect);
b44b0ab3b   Stefan Weinhuber   [S390] dasd: add ...
332
  		}
46e889478   Stefan Weinhuber   s390/partitions: ...
333
  	} else if (info) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  		/*
46e889478   Stefan Weinhuber   s390/partitions: ...
335
336
337
338
339
  		 * ugly but needed for backward compatibility:
  		 * If the block device is a DASD (i.e. BIODASDINFO2 works),
  		 * then we claim it in any case, even though it has no valid
  		 * label. If it has the LDL format, then we simply define a
  		 * partition as if it had an LNX1 label.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
  		 */
46e889478   Stefan Weinhuber   s390/partitions: ...
341
342
343
344
345
346
347
348
349
350
351
  		res = 1;
  		if (info->format == DASD_FORMAT_LDL) {
  			strlcat(state->pp_buf, "(nonl)", PAGE_SIZE);
  			size = i_size >> 9;
  			offset = (info->label_block + 1) * (blocksize >> 9);
  			put_partition(state, 1, offset, size-offset);
  			strlcat(state->pp_buf, "
  ", PAGE_SIZE);
  		}
  	} else
  		res = 0;
ef1597d52   Horst Hummel   [PATCH] s390: Rem...
352

57881dd9d   Suzuki K P   [PATCH] Fix check...
353
  out_freeall:
56dc6a88e   Peter Oberparleiter   [PATCH] s390: cms...
354
355
  	kfree(label);
  out_nolab:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
  	kfree(geo);
  out_nogeo:
  	kfree(info);
57881dd9d   Suzuki K P   [PATCH] Fix check...
359
360
  out_exit:
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
  }