summaryrefslogtreecommitdiff
path: root/libparted/labels/fdasd.c
blob: cee4d4618c0638e0ee72ef68bda18bab8333e675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
/*
 * File...........: arch/s390/tools/fdasd.c
 * Author(s)......: Volker Sameske <sameske@de.ibm.com>
 * Bugreports.to..: <Linux390@de.ibm.com>
 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 2001
 *
 * History of changes (starts March 2001)
 * 2001-04-11 possibility to change volume serial added
 *            possibility to change partition type added
 *            some changes to DS4HPCHR and DS4DSREC
 * 2001-05-03 check for invalid partition numbers added
 *            wrong free_space calculation bug fixed
 * 2001-06-26 '-a' option added, it is now possible to add a single
 *            partition in non-interactive mode
 * 2001-06-26 long parameter support added
 *
 */

#include <config.h>
#include <arch/linux.h>
#include <parted/vtoc.h>
#include <parted/device.h>
#include <parted/fdasd.h>

#include <parted/parted.h>

#include <libintl.h>
#if ENABLE_NLS
#  define _(String) dgettext (PACKAGE, String)
#else
#  define _(String) (String)
#endif /* ENABLE_NLS */

static int
getpos (fdasd_anchor_t *anc, int dsn)
{
	PDEBUG
	return anc->partno[dsn];
}

static int
getdsn (fdasd_anchor_t *anc, int pos)
{
	PDEBUG
	int i;

	for (i=0; i<USABLE_PARTITIONS; i++) {
		if (anc->partno[i] == pos)
			return i;
	}

	return -1;
}

static void
setpos (fdasd_anchor_t *anc, int dsn, int pos)
{
	PDEBUG
	anc->partno[dsn] = pos;
}

static u_int32_t
get_usable_cylinders (fdasd_anchor_t *anc)
{
	u_int32_t cyl;

	/* large volume */
	if (anc->f4->DS4DEVCT.DS4DSCYL == LV_COMPAT_CYL &&
	    anc->f4->DS4DCYL > anc->f4->DS4DEVCT.DS4DSCYL)
		return anc->f4->DS4DCYL;
	/* normal volume */
	if (anc->f4->DS4DEVCT.DS4DEVFG & ALTERNATE_CYLINDERS_USED)
		cyl = anc->f4->DS4DEVCT.DS4DSCYL -
			(u_int16_t) anc->f4->DS4DEVAC;
	else
		cyl = anc->f4->DS4DEVCT.DS4DSCYL;
	return cyl;
}

static void
get_addr_of_highest_f1_f8_label (fdasd_anchor_t *anc, cchhb_t *addr)
{

	u_int8_t record;
	/* We have to count the follwing labels:
	 * one format 4
	 * one format 5
	 * format 7 only if we have moren then BIG_DISK_SIZE tracks
	 * one for each format 1 or format 8 label == one for each partition
	 * one for each format 9 label before the last format 8
	 * We assume that all partitions use format 8 labels when
	 *  anc->formatted_cylinders > LV_COMPAT_CYL
	 * Note: Record zero is special, so block 0 on our disk is record 1!
	 */

	record = anc->used_partitions + 2;
	if (anc->big_disk)
		record++;
	if (anc->formatted_cylinders > LV_COMPAT_CYL)
		record += anc->used_partitions - 1;
	vtoc_set_cchhb(addr, VTOC_START_CC, VTOC_START_HH, record);
}

void
fdasd_cleanup (fdasd_anchor_t *anchor)
{
	PDEBUG
	int i;
	partition_info_t *part_info, *next;

	if (anchor == NULL)
		return;

	free(anchor->f4);
	free(anchor->f5);
	free(anchor->f7);
	free(anchor->vlabel);

	part_info = anchor->first;
	for (i = 1; i <= USABLE_PARTITIONS && part_info != NULL; i++) {
		next = part_info->next;
		free(part_info->f1);
		free(part_info);
		part_info = next;
	}
}

static void
fdasd_error (fdasd_anchor_t *anc, enum fdasd_failure why, char const *str)
{
	PDEBUG
	char error[2*LINE_LENGTH], *message = error;

	switch (why) {
		case unable_to_open_disk:
			sprintf(error, "fdasd: %s -- %s\n", _("open error"), str);
			break;
		case unable_to_seek_disk:
			sprintf(error, "fdasd: %s -- %s\n", _("seek error"), str);
			break;
		case unable_to_read_disk:
			sprintf(error, "fdasd: %s -- %s\n", _("read error"), str);
			break;
		case read_only_disk:
			sprintf(error, "fdasd: %s -- %s\n", _("write error"), str);
			break;
		case unable_to_ioctl:
			sprintf(error, "fdasd: %s -- %s\n", _("ioctl() error"), str);
			break;
		case api_version_mismatch:
			sprintf(error, "fdasd: %s -- %s\n",
				_("API version mismatch"), str);
			break;
		case wrong_disk_type:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Unsupported disk type"), str);
			break;
		case wrong_disk_format:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Unsupported disk format"), str);
			break;
		case disk_in_use:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Disk is in use"), str);
			break;
		case config_syntax_error:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Syntax error in config file"), str);
			break;
		case vlabel_corrupted:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Volume label is corrupted"), str);
			break;
		case dsname_corrupted:
			sprintf(error, "fdasd: %s -- %s\n",
				_("A data set name is corrupted"), str);
			break;
		case malloc_failed:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Memory allocation failed"), str);
			break;
		case device_verification_failed:
			sprintf(error, "fdasd: %s -- %s\n",
				_("Device verification failed"),
				_("The specified device is not a valid DASD device"));
			break;
		case volser_not_found:
			sprintf(error, "fdasd: %s -- %s\n", _("VOLSER not found on device"), str);
			break;
		default:
			sprintf(error, "fdasd: %s: %s\n", _("Fatal error"), str);
	}

	ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, message);
}

/*
 * initializes the anchor structure and allocates some
 * memory for the labels
 */
void
fdasd_initialize_anchor (fdasd_anchor_t * anc)
{
	PDEBUG
	int i;
	volume_label_t *v;
	partition_info_t *p = NULL;
	partition_info_t *q = NULL;

	bzero(anc, sizeof(fdasd_anchor_t));

	for (i=0; i<USABLE_PARTITIONS; i++)
		setpos(anc, i, -1);

	bzero(anc->confdata, sizeof(config_data_t));

	anc->f4 = malloc(sizeof(format4_label_t));
	if (anc->f4 == NULL)
		fdasd_error(anc, malloc_failed, "FMT4 DSCB.");

	anc->f5 = malloc(sizeof(format5_label_t));
	if (anc->f5 == NULL)
		fdasd_error(anc, malloc_failed, "FMT5 DSCB.");

	anc->f7 = malloc(sizeof(format7_label_t));
	if (anc->f7 == NULL)
		fdasd_error(anc, malloc_failed, "FMT7 DSCB.");

       /* template for all format 9 labels */
	anc->f9 = malloc(sizeof(format9_label_t));
	if (anc->f9 == NULL)
		fdasd_error(anc, malloc_failed, "FMT9 DSCB.");

	bzero(anc->f4, sizeof(format4_label_t));
	bzero(anc->f5, sizeof(format5_label_t));
	bzero(anc->f7, sizeof(format7_label_t));
	bzero(anc->f9, sizeof(format9_label_t));
	vtoc_init_format9_label(anc->f9);

	v = malloc(sizeof(volume_label_t));
	if (v == NULL)
		fdasd_error(anc, malloc_failed,
			    _("No room for volume label."));
	bzero(v, sizeof(volume_label_t));
	anc->vlabel = v;

	for (i=1; i<=USABLE_PARTITIONS; i++) {
        p = malloc(sizeof(partition_info_t));
		if (p == NULL)
			fdasd_error(anc, malloc_failed,
				    _("No room for partition info."));
		bzero(p, sizeof(partition_info_t));

		/* add p to double pointered list */
		if (i == 1) {
		        anc->first = p;
		} else if (i == USABLE_PARTITIONS) {
		        anc->last = p;
			p->prev = q;
			q->next = p;
		} else {
	                p->prev = q;
	                q->next = p;
		}

		p->f1 = malloc(sizeof(format1_label_t));
		if (p->f1 == NULL)
			fdasd_error(anc, malloc_failed, "FMT1 DSCB.");
		bzero(p->f1, sizeof(format1_label_t));

		q = p;
	}
	anc->hw_cylinders = 0;
	anc->formatted_cylinders = 0;
	anc->is_file = 0;
}

/*
 * writes all changes to dasd
 */
static void
fdasd_write_vtoc_labels (fdasd_anchor_t * anc, int fd)
{
	PDEBUG
	partition_info_t *p;
	unsigned long b, maxblk;
	char dsno[6], s1[VOLSER_LENGTH + 1], s2[45], *c1, *c2, *ch;
	int i = 0, k = 0;
	cchhb_t f9addr;
	format1_label_t emptyf1;

	b = (cchhb2blk (&anc->vlabel->vtoc, &anc->geo) - 1) * anc->blksize;
	if (b <= 0)
		fdasd_error (anc, vlabel_corrupted, "");
	maxblk = b + anc->blksize * 9; /* f4+f5+f7+3*f8+3*f9 */

	/* write FMT4 DSCB */
	vtoc_write_label (fd, b, NULL, anc->f4, NULL, NULL, NULL);
	b += anc->blksize;

	/* write FMT5 DSCB */
	vtoc_write_label (fd, b, NULL, NULL, anc->f5, NULL, NULL);
	b += anc->blksize;

	/* write FMT7 DSCB */
	if (anc->big_disk) {
		vtoc_write_label (fd, b, NULL, NULL, NULL, anc->f7, NULL);
		b += anc->blksize;
	}

	/* loop over all partitions (format 1 or format 8 DCB) */
	for (p = anc->first; p != NULL; p = p->next) {

		if (p->used != 0x01) {
			continue;
		}

		i++;
		strncpy (p->f1->DS1DSSN, anc->vlabel->volid, 6);

		ch = p->f1->DS1DSNAM;
		vtoc_ebcdic_dec (ch, ch, 44);
		c1 = ch + 7;

		if (getdsn (anc, i-1) > -1) {
			/* re-use the existing data set name */
			c2 = strchr (c1, '.');
			if (c2 != NULL)
				strncpy (s2, c2, 31);
			else
				fdasd_error (anc, dsname_corrupted, "");

			strncpy (s1, anc->vlabel->volid, 6);
			vtoc_ebcdic_dec (s1, s1, 6);
			s1[6] = ' ';
			strncpy (c1, s1, 7);
			c1 = strchr (ch, ' ');
			strncpy (c1, s2, 31);
		} else {
			/* create a new data set name */
			while (getpos (anc, k) > -1)
				k++;

			setpos (anc, k, i-1);

			strncpy (ch, "LINUX.V               " "                      ", 44);

			strncpy (s1, anc->vlabel->volid, 6);
			vtoc_ebcdic_dec (s1, s1, 6);
			strncpy (c1, s1, 6);

			c1 = strchr (ch, ' ');
			strncpy (c1, ".PART", 5);
			c1 += 5;

			sprintf (dsno, "%04d.", k + 1);
			strncpy (c1, dsno, 5);

			c1 += 5;
			switch(p->type) {
				case PARTITION_LINUX_LVM:
					strncpy(c1, PART_TYPE_LVM, 6);
					break;
				case PARTITION_LINUX_RAID:
					strncpy(c1, PART_TYPE_RAID, 6);
					break;
				case PARTITION_LINUX:
					strncpy(c1, PART_TYPE_NATIVE, 6);
					break;
				case PARTITION_LINUX_SWAP:
					strncpy(c1, PART_TYPE_SWAP, 6);
					break;
				default:
					strncpy(c1, PART_TYPE_NATIVE, 6);
					break;
			}
		}

		vtoc_ebcdic_enc (ch, ch, 44);

		if (p->f1->DS1FMTID == 0xf8 ) {
			/* Now as we know where which label will be written, we
			 * can add the address of the format 9 label to the
			 * format 8 label. The f9 record will be written to the
			 * block after the current blk. Remember: records are of
			 * by one, so we have to add 2 and not just one.
			 */
			vtoc_set_cchhb(&f9addr, VTOC_START_CC, VTOC_START_HH,
				((b / anc->blksize) % anc->geo.sectors) + 2);
			vtoc_update_format8_label(&f9addr, p->f1);
			vtoc_write_label(fd, b, p->f1, NULL, NULL, NULL, NULL);
			b += anc->blksize;
			vtoc_write_label(fd, b, NULL, NULL, NULL, NULL,
					 anc->f9);
			b += anc->blksize;
		} else {
			vtoc_write_label(fd, b, p->f1, NULL, NULL, NULL, NULL);
			b += anc->blksize;
		}
	}

	/* write empty labels to the rest of the blocks */
	bzero(&emptyf1, sizeof(emptyf1));
	while (b < maxblk) {
		vtoc_write_label(fd, b, &emptyf1, NULL, NULL, NULL, NULL);
		b += anc->blksize;
	}
}

/*
 * writes all changes to dasd
 */
int
fdasd_write_labels (fdasd_anchor_t * anc, int fd)
{
	PDEBUG
	if (anc->vlabel_changed)
		vtoc_write_volume_label (fd, anc->label_pos, anc->vlabel);

	if (anc->vtoc_changed)
		fdasd_write_vtoc_labels (anc, fd);

	return 1;
}

/*
 * writes all changes to dasd
 */
int
fdasd_prepare_labels (fdasd_anchor_t *anc, int fd)
{
	PDEBUG
	partition_info_t *p;
	char dsno[6], s1[7], s2[45], *c1, *c2, *ch;
	int i = 0, k = 0;

	/* loop over all partitions (format 1 or format 8 DCB) */
	for (p = anc->first; p != NULL; p = p->next) {

		if (p->used != 0x01) {
			continue;
		}

		i++;
		strncpy (p->f1->DS1DSSN, anc->vlabel->volid, 6);

		ch = p->f1->DS1DSNAM;
		vtoc_ebcdic_dec (ch, ch, 44);
		c1 = ch + 7;

		if (getdsn (anc, i-1) > -1) {
			/* re-use the existing data set name */
			c2 = strchr (c1, '.');
			if (c2 != NULL)
				strncpy (s2, c2, 31);
			else
				fdasd_error (anc, dsname_corrupted, "");

			strncpy (s1, anc->vlabel->volid, 6);
			vtoc_ebcdic_dec (s1, s1, 6);
			s1[6] = ' ';
			strncpy (c1, s1, 7);
			c1 = strchr (ch, ' ');
			strncpy (c1, s2, 31);
		} else {
			/* create a new data set name */
			while (getpos (anc, k) > -1)
				k++;

			setpos (anc, k, i-1);

			strncpy (ch, "LINUX.V               " "                      ", 44);

			strncpy (s1, anc->vlabel->volid, 6);
			vtoc_ebcdic_dec (s1, s1, 6);
			strncpy (c1, s1, 6);

			c1 = strchr (ch, ' ');
			strncpy (c1, ".PART", 5);
			c1 += 5;

			sprintf (dsno, "%04d.", k + 1);
			strncpy (c1, dsno, 5);

			c1 += 5;
			switch(p->type) {
				case PARTITION_LINUX_LVM:
					strncpy(c1, PART_TYPE_LVM, 6);
					break;
				case PARTITION_LINUX_RAID:
					strncpy(c1, PART_TYPE_RAID, 6);
					break;
				case PARTITION_LINUX:
					strncpy(c1, PART_TYPE_NATIVE, 6);
					break;
				case PARTITION_LINUX_SWAP:
					strncpy(c1, PART_TYPE_SWAP, 6);
					break;
				default:
					strncpy(c1, PART_TYPE_NATIVE, 6);
					break;
			}
		}

		vtoc_ebcdic_enc (ch, ch, 44);
	}

	return 1;
}

void
fdasd_recreate_vtoc (fdasd_anchor_t *anc)
{
	PDEBUG
	partition_info_t *p = anc->first;
	int i;

	vtoc_init_format4_label(anc->f4,
							anc->geo.cylinders,
						anc->formatted_cylinders,
							anc->geo.heads,
							anc->geo.sectors,
							anc->blksize,
							anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);
	vtoc_set_freespace(anc->f4, anc->f5, anc->f7,
					   '+', anc->verbose,
					   FIRST_USABLE_TRK,
				anc->formatted_cylinders * anc->geo.heads - 1,
				anc->formatted_cylinders, anc->geo.heads);

	for (i = 0; i < USABLE_PARTITIONS; i++) {
		bzero(p->f1, sizeof(format1_label_t));
		p->used       = 0x00;
		p->start_trk  = 0;
		p->end_trk    = 0;
		p->len_trk    = 0;
		p->fspace_trk = 0;
		p->type       = 0;
		p = p->next;
	}

	anc->used_partitions = 0;
	anc->fspace_trk = anc->formatted_cylinders * anc->geo.heads -
			  FIRST_USABLE_TRK;

	for (i=0; i<USABLE_PARTITIONS; i++)
		setpos(anc, i, -1);

	anc->vtoc_changed++;
}

 /*
 * initialize the VOL1 volume label
 */
static void
fdasd_init_volume_label(fdasd_anchor_t *anc, int fd)
{
	volume_label_t *vlabel = anc->vlabel;

	vtoc_volume_label_init(vlabel);
	vtoc_volume_label_set_key(vlabel, "VOL1");
	vtoc_volume_label_set_label(vlabel, "VOL1");

	vtoc_set_cchhb(&vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);
}


/*
 * sets some important partition data
 * (like used, start_trk, end_trk, len_trk)
 * by calculating these values with the
 * information provided in the labels
 */
static void
fdasd_update_partition_info (fdasd_anchor_t *anc)
{
	PDEBUG
	partition_info_t *q = NULL, *p = anc->first;
	unsigned long max = anc->formatted_cylinders * anc->geo.heads - 1;
	int i;
	char *ch;

	anc->used_partitions = anc->geo.sectors - 2 - anc->f4->DS4DSREC;

	for (i = 1; i <= USABLE_PARTITIONS; i++) {
		if (p->f1->DS1FMTID != 0xf1 &&
		    p->f1->DS1FMTID != 0xf8) {
			if (i == 1)
				/* there is no partition at all */
				anc->fspace_trk = max - FIRST_USABLE_TRK + 1;
			else
				/* previous partition was the last one */
				q->fspace_trk = max - q->end_trk;
			break;
		}

		/* this is a valid format 1 label */
		p->used = 0x01;
		p->start_trk = cchh2trk(&p->f1->DS1EXT1.llimit,	&anc->geo);
		p->end_trk = cchh2trk(&p->f1->DS1EXT1.ulimit, &anc->geo);
		p->len_trk   = p->end_trk - p->start_trk + 1;

		if (i == 1) {
			/* first partition, there is at least one */
			anc->fspace_trk = p->start_trk - FIRST_USABLE_TRK;
		} else {
			if (i == USABLE_PARTITIONS)
				/* last possible partition */
				p->fspace_trk = max - p->end_trk;

			/* set free space values of previous partition */
			q->fspace_trk = p->start_trk - q->end_trk - 1;
		}

		ch = p->f1->DS1DSNAM;
		vtoc_ebcdic_dec (ch, ch, 44);
		if (strstr(ch, PART_TYPE_LVM))
			p->type = PARTITION_LINUX_LVM;
		else if (strstr(ch, PART_TYPE_RAID))
			p->type = PARTITION_LINUX_RAID;
		else if (strstr(ch, PART_TYPE_NATIVE))
			p->type = PARTITION_LINUX;
		else if (strstr(ch, PART_TYPE_SWAP))
			p->type = PARTITION_LINUX_SWAP;
		else
			p->type = PARTITION_LINUX;
		vtoc_ebcdic_enc (ch, ch, 44);

		q = p;
		p = p->next;
	}
}

/*
 * reorganizes all FMT1s, after that all used FMT1s should be right in
 * front of all unused FMT1s
 */
static void
fdasd_reorganize_FMT1s (fdasd_anchor_t *anc)
{
	PDEBUG
	int i, j;
	format1_label_t *ltmp;
	partition_info_t *ptmp;

	for (i=1; i<=USABLE_PARTITIONS - 1; i++) {
		ptmp = anc->first;

		for (j=1; j<=USABLE_PARTITIONS - i; j++) {
			if (ptmp->f1->DS1FMTID < ptmp->next->f1->DS1FMTID) {
				ltmp = ptmp->f1;
				ptmp->f1 = ptmp->next->f1;
				ptmp->next->f1 = ltmp;
			}

			ptmp=ptmp->next;
		}
	}
}

static void
fdasd_process_valid_vtoc (fdasd_anchor_t * anc, unsigned long b, int fd)
{
	PDEBUG
	int f5_counter = 0, f7_counter = 0, f1_counter = 0, oldfmt = 0;
	int i, n, f1size = sizeof (format1_label_t);
	partition_info_t *p = anc->first;
	format1_label_t q;
	char s[5], *ch;

	if (anc->f4->DS4DEVCT.DS4DSCYL == LV_COMPAT_CYL &&
	    anc->f4->DS4DCYL > anc->f4->DS4DEVCT.DS4DSCYL)
		anc->formatted_cylinders = anc->f4->DS4DCYL;
	else
		anc->formatted_cylinders = anc->f4->DS4DEVCT.DS4DSCYL;
	anc->fspace_trk = anc->formatted_cylinders * anc->geo.heads -
			  FIRST_USABLE_TRK;
	b += anc->blksize;

	for (i = 1; i < anc->geo.sectors; i++) {
		bzero (&q, f1size);
		vtoc_read_label (fd, b, &q, NULL, NULL, NULL);

		switch (q.DS1FMTID) {
			case 0xf1:
			case 0xf8:
				if (p == NULL)
					break;
				memcpy (p->f1, &q, f1size);

				n = -1;
				vtoc_ebcdic_dec (p->f1->DS1DSNAM, p->f1->DS1DSNAM, 44);
				ch = strstr (p->f1->DS1DSNAM, "PART");
				if (ch != NULL) {
					strncpy (s, ch + 4, 4);
					s[4] = '\0';
					n = atoi (s) - 1;
				}

				vtoc_ebcdic_enc (p->f1->DS1DSNAM, p->f1->DS1DSNAM, 44);

				/* this dasd has data set names 0000-0002
				but we use now 0001-0003 */
				if (n == -1)
					oldfmt++;

				if (((oldfmt == 0) && (n < 0)) || (n >= USABLE_PARTITIONS)) {
					/* no op */
				} else {
					if (oldfmt) {
						/* correct +1 */
						setpos (anc, n + 1, f1_counter);
					} else {
						setpos (anc, n, f1_counter);
					}
				}

				p = p->next;
				f1_counter++;
				break;
			case 0xf5:
				memcpy (anc->f5, &q, f1size);
				f5_counter++;
				break;
			case 0xf7:
				if (f7_counter == 0)
					memcpy (anc->f7, &q, f1size);
				f7_counter++;
				break;
			case 0xf9:
				/* each format 8 lable has an associated
				 * format 9 lable, but they are of no further
				 * use to us.
				 */
				break;
		}

		b += anc->blksize;
	}

	if (oldfmt > 0) {
		/* this is the old format PART0000 - PART0002 */
		anc->vtoc_changed++;
	}

	if ((f5_counter == 0) || (anc->big_disk))
		vtoc_init_format5_label (anc->f5);

	if (f7_counter == 0)
		vtoc_init_format7_label (anc->f7);

	fdasd_reorganize_FMT1s (anc);
	fdasd_update_partition_info (anc);
}

static void
fdasd_invalid_vtoc_pointer(fdasd_anchor_t *anc)
{
	PDEBUG
	anc->formatted_cylinders = anc->hw_cylinders;
	anc->fspace_trk = anc->formatted_cylinders * anc->geo.heads
			- FIRST_USABLE_TRK;
	vtoc_init_format4_label(anc->f4,
			anc->geo.cylinders, anc->formatted_cylinders,
			anc->geo.heads, anc->geo.sectors,
			anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);

	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', anc->verbose,
			FIRST_USABLE_TRK,
			anc->formatted_cylinders * anc->geo.heads - 1,
			anc->formatted_cylinders, anc->geo.heads);

	vtoc_set_cchhb(&anc->vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);
	anc->vtoc_changed++;
	anc->vlabel_changed++;
}

/*
 * we have a invalid FMT4 DSCB and therefore we will re-create the VTOC
 */
static void
fdasd_process_invalid_vtoc(fdasd_anchor_t *anc)
{
	anc->formatted_cylinders = anc->hw_cylinders;
	anc->fspace_trk = anc->formatted_cylinders * anc->geo.heads
			- FIRST_USABLE_TRK;
	vtoc_init_format4_label(anc->f4,
			anc->geo.cylinders, anc->formatted_cylinders,
			anc->geo.heads, anc->geo.sectors,
			anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);
	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', anc->verbose,
			FIRST_USABLE_TRK,
			anc->formatted_cylinders * anc->geo.heads - 1,
			anc->formatted_cylinders, anc->geo.heads);

	anc->vtoc_changed++;
}


static int
fdasd_valid_vtoc_pointer(fdasd_anchor_t *anc, unsigned long b, int fd)
{
	PDEBUG
	char str[LINE_LENGTH];

	/* VOL1 label contains valid VTOC pointer */
	vtoc_read_label (fd, b, NULL, anc->f4, NULL, NULL);

	if (anc->f4->DS4IDFMT == 0xf4) {
		fdasd_process_valid_vtoc (anc, b, fd);
		return 0;
	} else {
		fdasd_process_invalid_vtoc(anc);
	}
	if (strncmp(anc->vlabel->volkey, vtoc_ebcdic_enc("LNX1",str,4),4) == 0 ||
	    strncmp(anc->vlabel->volkey, vtoc_ebcdic_enc("CMS1",str,4),4) == 0)
		return 0;

	fdasd_error(anc, wrong_disk_format, _("Invalid VTOC."));
	return 1;
}

/*
 * check the dasd for a volume label
 */
int
fdasd_check_volume (fdasd_anchor_t *anc, int fd)
{
	PDEBUG
	volume_label_t *v = anc->vlabel;
	long long b = -1;
	char str[LINE_LENGTH];

	memset(v, 0, sizeof(volume_label_t));
	vtoc_read_volume_label (fd, anc->label_pos, v);

	if (strncmp(v->vollbl, vtoc_ebcdic_enc ("VOL1", str, 4), 4) == 0) {
		if (anc->FBA_layout != 1 ) {
			/* found VOL1 volume label */
			b = (cchhb2blk (&v->vtoc, &anc->geo) - 1) * anc->blksize;

			if (b > 0) {
				int rc;
				rc = fdasd_valid_vtoc_pointer (anc, b, fd);

				if (rc < 0)
					return 1;
				else
					return 0;
			} else {
				fdasd_invalid_vtoc_pointer(anc);
			}
		}
	} else if (strncmp (v->volkey, vtoc_ebcdic_enc ("LNX1", str, 4), 4) == 0 ||
	           strncmp (v->volkey, vtoc_ebcdic_enc ("CMS1", str, 4), 4) == 0) {
		return 0;
	} else if (anc->FBA_layout == 1) {
		/* Some times LDL formatted disks does not
		   contain any volume label */
		return 1;
	} else if (! anc->is_file) {
	/* didn't find VOL1 volume label */
		anc->formatted_cylinders = anc->hw_cylinders;
		anc->fspace_trk = anc->formatted_cylinders * anc->geo.heads
					- FIRST_USABLE_TRK;

		fdasd_init_volume_label(anc, fd);

		vtoc_init_format4_label(anc->f4,
				anc->geo.cylinders, anc->formatted_cylinders,
				anc->geo.heads, anc->geo.sectors,
				anc->blksize, anc->dev_type);

		vtoc_init_format5_label(anc->f5);
		vtoc_init_format7_label(anc->f7);

		vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+',
			anc->verbose, FIRST_USABLE_TRK,
			anc->formatted_cylinders * anc->geo.heads - 1,
			anc->formatted_cylinders, anc->geo.heads);
		return 0;
	}
	return 1;
}

/*
 * checks the current API version with the API version of the dasd driver
 */
void
fdasd_check_api_version (fdasd_anchor_t *anc, int f)
{
	PDEBUG
	int api;
	char s[2*LINE_LENGTH];

        struct stat st;
        if (fstat (f, &st) == 0 && S_ISREG (st.st_mode)) {
		/* skip these tests when F is a regular file.  */
	}
	else {
		if (ioctl(f, DASDAPIVER, &api) != 0)
			fdasd_error(anc, unable_to_ioctl,
				    _("Could not retrieve API version."));

		if (api != DASD_MIN_API_VERSION) {
			sprintf(s, _("The current API version '%d' doesn't " \
					"match dasd driver API version " \
					"'%d'!"), api, DASD_MIN_API_VERSION);
			fdasd_error(anc, api_version_mismatch, s);
		}
	}
}

/*
 * The following two functions match those in the DASD ECKD device driver.
 * They are used to compute how many records of a given size can be stored
 * in one track.
 */
static unsigned int ceil_quot(unsigned int d1, unsigned int d2)
{
	return (d1 + (d2 - 1)) / d2;
}

/* kl: key length, dl: data length */
static unsigned int recs_per_track(unsigned short dev_type, unsigned int kl,
				   unsigned int dl)
{
	unsigned int dn, kn;

	switch (dev_type) {
	case DASD_3380_TYPE:
		if (kl)
			return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
				       ceil_quot(dl + 12, 32));
		else
			return 1499 / (15 + ceil_quot(dl + 12, 32));
	case DASD_3390_TYPE:
		dn = ceil_quot(dl + 6, 232) + 1;
		if (kl) {
			kn = ceil_quot(kl + 6, 232) + 1;
			return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
				       9 + ceil_quot(dl + 6 * dn, 34));
		} else
			return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
	case DASD_9345_TYPE:
		dn = ceil_quot(dl + 6, 232) + 1;
		if (kl) {
			kn = ceil_quot(kl + 6, 232) + 1;
			return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
				       ceil_quot(dl + 6 * dn, 34));
		} else
			return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
	}
	return 0;
}

/*
 * Verify that number of tracks (heads) per cylinder and number of
 * sectors per track match the expected values for a given device type
 * and block size.
 * Returns 1 for a valid match and 0 otherwise.
 */
static int fdasd_verify_geometry(unsigned short dev_type, int blksize,
				 struct fdasd_hd_geometry *geometry)
{
	unsigned int expected_sectors;
	if (geometry->heads != 15)
		return 0;
	expected_sectors = recs_per_track(dev_type, 0, blksize);
	if (geometry->sectors == expected_sectors)
		return 1;
	return 0;
}

/*
 * reads dasd geometry data
 */
int
fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int f)
{
	PDEBUG
	int blksize = 0;
	dasd_information_t dasd_info;
	struct dasd_eckd_characteristics *characteristics;
	unsigned long long size_in_bytes;

	/* We can't get geometry from a regular file,
	   so simulate something usable, for the sake of testing.  */
	struct stat st;
	if (fstat (f, &st) == 0 && S_ISREG (st.st_mode)) {
	    PedSector n_sectors = st.st_size / dev->sector_size;
	    anc->geo.heads = 15;
	    anc->geo.sectors = 12;
	    anc->geo.cylinders
	      = (n_sectors / (anc->geo.heads * anc->geo.sectors
			      * (dev->sector_size / dev->phys_sector_size)));
	    anc->geo.start = 0;
	    blksize = 4096;
	    memcpy (dasd_info.type, "ECKD", 4);
	    dasd_info.dev_type = 13200;
	    dasd_info.label_block = 2;
	    dasd_info.devno = 513;
	    dasd_info.label_block = 2;
	    dasd_info.FBA_layout = 0;
	    anc->hw_cylinders = ((st.st_size / blksize) / anc->geo.sectors) /
				anc->geo.heads;
	    anc->is_file = 1;
	} else {
	        if (ioctl(f, BLKGETSIZE64, &size_in_bytes) != 0) {
		        fdasd_error(anc, unable_to_ioctl,
				    _("Could not retrieve disk size."));
			goto error;
		}

		if (ioctl(f, HDIO_GETGEO, &anc->geo) != 0 ||
		        anc->geo.heads == 0 ||
		        anc->geo.sectors == 0 ||
		        anc->geo.cylinders == 0 ) {
			fdasd_error(anc, unable_to_ioctl,
			    _("Could not retrieve disk geometry information."));
			goto error;
		}

		if (ioctl(f, BLKSSZGET, &blksize) != 0) {
			fdasd_error(anc, unable_to_ioctl,
			    _("Could not retrieve blocksize information."));
			goto error;
		}

		/* get disk type */
		if (ioctl(f, BIODASDINFO, &dasd_info) != 0) {
		        /* verify that the geometry matches a 3390 DASD */
		        if (!fdasd_verify_geometry(DASD_3390_TYPE, blksize,
						   &anc->geo)) {
			        fdasd_error(anc, wrong_disk_type,
					    _("Disk geometry does not match a " \
					      "DASD device of type 3390."));
				goto error;
			}
			anc->dev_type = DASD_3390_TYPE;
			anc->hw_cylinders =
			        size_in_bytes / (blksize * anc->geo.heads * anc->geo.sectors);
			/* The VOL1 label on a CDL formatted ECKD DASD is in block 2
			 * It will be verified later, if this position actually holds a
			 * valid label record.
			 */
			anc->label_pos = 2 * blksize;
			/* A devno 0 is actually a valid devno, which could exist
			 * in the system. Since we use this number only to create
			 * a default volume serial, there is no serious conflict.
			 */
			anc->devno = 0;
		} else {
		        characteristics = (struct dasd_eckd_characteristics *)
			        &dasd_info.characteristics;
			if (characteristics->no_cyl == LV_COMPAT_CYL &&
			        characteristics->long_no_cyl)
		                anc->hw_cylinders = characteristics->long_no_cyl;
			else
		                anc->hw_cylinders = characteristics->no_cyl;
			anc->dev_type = dasd_info.dev_type;
			anc->label_pos = dasd_info.label_block * blksize;
			anc->devno = dasd_info.devno;
			anc->label_block = dasd_info.label_block;
			anc->FBA_layout = dasd_info.FBA_layout;
		}

		anc->is_file = 0;
	}

	anc->blksize = blksize;
	return 1;

 error:
	return 0;
}

/*
 * returns unused partition info pointer if there
 * is a free partition, otherwise NULL
 */
static partition_info_t *
fdasd_get_empty_f1_label (fdasd_anchor_t * anc)
{
	PDEBUG
	if (anc->used_partitions < USABLE_PARTITIONS)
		return anc->last;
	else
		return NULL;
}

/*
 * asks for and sets some important partition data
 */
static int
fdasd_get_partition_data (fdasd_anchor_t *anc, extent_t *part_extent,
                          partition_info_t *p, unsigned int *start_ptr,
                          unsigned int *stop_ptr)
{
	PDEBUG
	unsigned int limit;
	u_int32_t cc, c;
	u_int16_t hh, h;
	cchh_t llimit, ulimit;
	partition_info_t *q;
	u_int8_t b1, b2;
	unsigned int start = *start_ptr, stop = *stop_ptr;
	int i;
	char *ch;

	c = get_usable_cylinders(anc);
	h = anc->f4->DS4DEVCT.DS4DSTRK;
	limit = (h * c - 1);

	/* check start value from user */
	q = anc->first;
	for (i = 0; i < USABLE_PARTITIONS; i++) {
		if ( q->next == NULL )
			break;

		if (start >= q->start_trk && start <= q->end_trk) {
			/* start is within another partition */
			start = q->end_trk + 1;

			if (start > limit) {
				start = FIRST_USABLE_TRK;
				q = anc->first;
			}
		}

		if (start < q->start_trk) {
			limit = q->start_trk - 1;
			break;
		}

		q = q->next;
	}

	if (start == limit)
		stop = start;

	/* update partition info */
	p->len_trk    = stop - start + 1;
	p->start_trk  = start;
	p->end_trk    = stop;

	cc = start / anc->geo.heads;
	hh = start - (cc * anc->geo.heads);
	vtoc_set_cchh(&llimit, cc, hh);

	/* check for cylinder boundary */
	if (hh == 0)
		b1 = 0x81;
	else
		b1 = 0x01;

	cc = stop / anc->geo.heads;
	hh = stop - cc * anc->geo.heads;
	vtoc_set_cchh(&ulimit, cc, hh);

	/* it is always the 1st extent */
	b2 = 0x00;

	vtoc_set_extent(part_extent, b1, b2, &llimit, &ulimit);

	*start_ptr = start;
	*stop_ptr = stop;

	ch = p->f1->DS1DSNAM;
	vtoc_ebcdic_dec (ch, ch, 44);

	if (strstr(ch, PART_TYPE_LVM))
		p->type = PARTITION_LINUX_LVM;
	else if (strstr(ch, PART_TYPE_RAID))
		p->type = PARTITION_LINUX_RAID;
	else if (strstr(ch, PART_TYPE_NATIVE))
		p->type = PARTITION_LINUX;
	else if (strstr(ch, PART_TYPE_SWAP))
		p->type = PARTITION_LINUX_SWAP;
	else
		p->type = PARTITION_LINUX;

	vtoc_ebcdic_enc (ch, ch, 44);

	return 0;
}

static void
fdasd_enqueue_new_partition (fdasd_anchor_t *anc)
{
	PDEBUG
	partition_info_t *q = anc->first, *p = anc->last;
	int i, k=0;

	for (i=1; i<USABLE_PARTITIONS; i++) {
		if ((q->end_trk == 0) || (p->start_trk < q->start_trk)) {
			break;
		} else {
			q = q->next;
			k++;
		}
	}

	if (anc->first == q)
		anc->first = p;

	if (p != q) {
		anc->last->prev->next = NULL;
		anc->last = anc->last->prev;

		p->next = q;
		p->prev = q->prev;
		q->prev = p;

		if (p->prev != NULL)
			p->prev->next = p;
	}

	p->used = 0x01;
	p->type = PARTITION_LINUX;

	for (i=0; i<USABLE_PARTITIONS; i++) {
		int j = getpos(anc, i);
		if (j >= k)
			setpos(anc, i, j + 1);
	}

	/* update free-space counters */
	if (anc->first == p) {
		/* partition is the first used partition */
		if (p->start_trk == FIRST_USABLE_TRK) {
			/* partition starts right behind VTOC */
			p->fspace_trk = anc->fspace_trk - p->len_trk;
			anc->fspace_trk = 0;
		} else {
			/* there is some space between VTOC and partition */
			p->fspace_trk = anc->fspace_trk - p->len_trk - p->start_trk
							+ FIRST_USABLE_TRK;
			anc->fspace_trk = p->start_trk - FIRST_USABLE_TRK;
		}
	} else {
		/* there are partitions in front of the new one */
		if (p->start_trk == p->prev->end_trk + 1) {
			/* new partition is right behind the previous one */
			p->fspace_trk = p->prev->fspace_trk - p->len_trk;
			p->prev->fspace_trk = 0;
		} else {
			/* there is some space between new and prev. part. */
			p->fspace_trk = p->prev->fspace_trk - p->len_trk
							- p->start_trk + p->prev->end_trk + 1;
			p->prev->fspace_trk = p->start_trk - p->prev->end_trk - 1;
		}
	}
}

/*
 * adds a new partition to the 'partition table'
 */
partition_info_t *
fdasd_add_partition (fdasd_anchor_t *anc, unsigned int start,
                     unsigned int stop)
{
	PDEBUG
	cchhb_t hf1;
	partition_info_t *p;
	extent_t ext;

	PDEBUG;

	if ((p = fdasd_get_empty_f1_label(anc)) == NULL) {
		PDEBUG;
		return 0;
	}

	PDEBUG;
	if (fdasd_get_partition_data(anc, &ext, p, &start, &stop) != 0)
		return 0;

	if (anc->formatted_cylinders > LV_COMPAT_CYL) {
		vtoc_init_format8_label(anc->blksize, &ext, p->f1);
	} else {
		PDEBUG;
		vtoc_init_format1_label(anc->blksize, &ext, p->f1);
	}

	PDEBUG;
	fdasd_enqueue_new_partition(anc);

	PDEBUG;
	anc->used_partitions += 1;

	get_addr_of_highest_f1_f8_label(anc, &hf1);
	vtoc_update_format4_label(anc->f4, &hf1, anc->f4->DS4DSREC - 1);

	PDEBUG;

	start = cchh2trk(&ext.llimit, &anc->geo);
	stop = cchh2trk(&ext.ulimit, &anc->geo);

	PDEBUG;
	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '-', anc->verbose,
			start, stop, anc->formatted_cylinders, anc->geo.heads);

	anc->vtoc_changed++;

	PDEBUG;
	return p;
}

/*
 * Check for valid volume serial characters (max. 6) - remove invalid.
 * If volser is empty, fill with default volser.
 */
void fdasd_check_volser (char *volser, int devno)
{
	int from, to;

	for (from = 0, to = 0; volser[from] && from < VOLSER_LENGTH; from++) {

			if ((volser[from] >= 0x23 &&
			     volser[from] <= 0x25) ||
			    (volser[from] >= 0x30 &&
			     volser[from] <= 0x39) ||
			    (volser[from] >= 0x40 &&
			     volser[from] <= 0x5a) ||
			    (volser[from] >= 0x61 &&
			     volser[from] <= 0x7a))
				volser[to++] = toupper(volser[from]);
	}

	volser[to] = 0x00;

	if (volser[0] == 0x00)
		sprintf(volser, "0X%04x", devno);
}

/*
 * get volser from vtoc
 */
int fdasd_get_volser (fdasd_anchor_t *anc, char *volser, int fd)
{
	volume_label_t vlabel;

	vtoc_read_volume_label(fd, anc->label_pos, &vlabel);
	vtoc_volume_label_get_volser(&vlabel, volser);
	return 0;
}

/* Changes the volume serial (menu option)
 *
 */
void fdasd_change_volser (fdasd_anchor_t *anc, char *str)
{
	fdasd_check_volser(str, anc->devno);
	vtoc_volume_label_set_volser(anc->vlabel, str);

	vtoc_set_cchhb(&anc->vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);
	anc->vlabel_changed++;
	anc->vtoc_changed++;
}

/*
 * re-create all VTOC labels, but use the partition information
 * from existing VTOC
 */
void fdasd_reuse_vtoc (fdasd_anchor_t *anc)
{
	partition_info_t *part_info = anc->first;
	struct fdasd_hd_geometry geo = anc->geo;
	format1_label_t f1;
	format4_label_t f4;
	format5_label_t f5;
	format7_label_t f7;

	vtoc_init_format4_label(&f4, geo.cylinders, anc->formatted_cylinders,
				geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	/* reuse some FMT4 values */
	f4.DS4HPCHR = anc->f4->DS4HPCHR;
	f4.DS4DSREC = anc->f4->DS4DSREC;

	/* re-initialize both free-space labels */
	vtoc_init_format5_label(&f5);
	vtoc_init_format7_label(&f7);

	if (anc->fspace_trk > 0)
		vtoc_set_freespace(&f4, &f5, &f7, '+', anc->verbose,
				   FIRST_USABLE_TRK,
				   FIRST_USABLE_TRK + anc->fspace_trk - 1,
				   anc->formatted_cylinders, geo.heads);

	while (part_info != NULL) {
		if (part_info->used != 0x01) {
			part_info = part_info->next;
			continue;
		}

		if (anc->formatted_cylinders > LV_COMPAT_CYL)
			vtoc_init_format8_label(anc->blksize,
						&part_info->f1->DS1EXT1, &f1);
		else
			vtoc_init_format1_label(anc->blksize,
						&part_info->f1->DS1EXT1, &f1);


		strncpy(f1.DS1DSNAM, part_info->f1->DS1DSNAM, 44);
		strncpy((char *)f1.DS1DSSN, (char *)part_info->f1->DS1DSSN, 6);
		f1.DS1CREDT = part_info->f1->DS1CREDT;

		memcpy(part_info->f1, &f1, sizeof(format1_label_t));

		if (part_info->fspace_trk > 0)
			vtoc_set_freespace(&f4, &f5, &f7, '+', anc->verbose,
					   part_info->end_trk + 1,
					   part_info->end_trk +
					   part_info->fspace_trk,
					   anc->formatted_cylinders, geo.heads);

		part_info = part_info->next;
	}

	/* over-write old labels with new ones */
	memcpy(anc->f4, &f4, sizeof(format4_label_t));
	memcpy(anc->f5, &f5, sizeof(format5_label_t));
	memcpy(anc->f7, &f7, sizeof(format7_label_t));

	anc->vtoc_changed++;

	return;
}

/* vim:set tabstop=4 shiftwidth=4 softtabstop=4: */