mkfs.c
32.5 KB
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
/*
* EFS version of mkfs.
*
* $Revision: 1.1.1.1 $
*/
#include "efs.h"
#include <sys/stat.h>
#include <sys/dkio.h>
#include <diskinfo.h>
#include <sys/ustat.h>
#include <sys/major.h>
#define HP_DISK_MODEL
#ifdef HP_DISK_MODEL
#include "diskmodel.param"
#include "diskdevices.h"
#endif
extern char *malloc();
extern char *calloc();
void cgsizepick();
void efs_newregfile();
#define GOOD_INODE_TOTAL(b) (b / 10 + (b > 20000 ? 1000 : b / 20))
char *progname; /* name of this program */
/* user supplied information */
char *special; /* name of special device */
char *rawpath; /* equivalent raw device (may be the same) */
long fs_blocks; /* total blocks in filesystem */
long dev_blocks; /* total blocks on device */
ino_t fs_isize = 0; /* total minimum inodes in partition */
short fs_sectors; /* # of sectors per track */
int user_inodes = 0;
/* internally supplied information */
long cg_align; /* alignment for cylinder groups */
long i_align; /* alignment for inodes */
/* random variables */
long firstcg; /* block offset to first cg */
long cg_fsize = 0; /* data size for a cylinder group */
long cg_isize; /* inode size for a cylinder group */
char is_ittybitty; /* non-zero for itty bitty filesystems */
int fs_fd; /* fd to read/write on for special */
long ncg; /* number of cylinder groups */
struct efs *fs; /* pointer to sblock.fs */
char *bitmap; /* base of incore copy of bitmap */
long bitmap_blocks; /* number of bb's in bitmap */
long bitmap_bytes; /* number of bytes in bitmap (exact) */
char *proto; /* name of proto file */
char *charp;
FILE *fproto; /* FILE proto is open on during reading */
char string[1000]; /* temp buffer for parsing proto */
int end_blocks; /* blocks used by replsb at end */
int start_blocks; /* blocks used by boot, superblock & bitmap
* (rounded up to a cg_align boundary) */
int cyl_align = 0; /* in auto mode, align inodes and data to
* cylinder boundaries */
int debug = 0;
/*
* This union declares a single BB, assuming that the minimum for raw i/o
* is at least a BB.
*/
union {
struct efs fs;
char block[BBTOB(BTOBB(sizeof(struct efs)))];
} sblock;
void getstr();
void clear_other_sb();
int quiet = 0;
int interact = 0;
int use_writeb = 0;
int short_form = 1;
int recover_option = 0;
int
good_cg_size(blocks, iblocks, align)
int blocks, iblocks, align;
{
int size;
size = (blocks / 100) + 20000;
if (size < ((blocks + iblocks - 1) / iblocks) * align)
size = ((blocks + iblocks - 1) / iblocks) * align;
return size;
}
/* Given an (extended) dev_t, put it into the on-disk
* union. If its components are within the range understood by earlier
* kernels, use the old format; if not, put -1 in the old field &
* store it in the new one. Duplicates logic in fs/efs/efs_inode.c
*
* XXXdh mkfs should really use the efs_putdev() in efs/cmd/nlib/libefs.a,
* but that is missing several functions needed by the proto file stuff,
* so until those are moved into nlib & tested, we have to use the old
* lib/libefs.a
*/
static void
efs_putdev(dev_t dev, union di_addr *di)
{
major_t maj;
minor_t min;
if ( ((maj = (dev >> L_BITSMINOR)) > O_MAXMAJ) ||
((min = (dev & L_MAXMIN)) > O_MAXMIN)) {
di->di_dev.odev = -1;
di->di_dev.ndev = dev;
}
else
di->di_dev.odev = (maj << O_BITSMINOR) | min;
}
void main(argc, argv)
int argc;
char **argv;
{
int cgblocks;
int iblocks;
int add_inodes = 0;
int i, nargs;
int fp;
char **ap;
progname = argv[0];
/* The (-q) option is not really intended to be documented or used
by users. It is here for use of programs like the
vadmin disk tool. Olson, 10/88
Another change (daveh 10/26/88): the default is now to just
go ahead. If you want confirmation, give the -i flag.
(Note: ALL - flags MUST come before any other arguments,
for backward compatibility with any scripts which use the
long forms).
*/
nargs = (argc > 4) ? 4 : argc;
ap = argv;
for (i = 1; i < nargs; i++)
{
if (ap[i][0] != '-') break;
switch (ap[i][1])
{
case 'd' : debug = 1;
break;
case 'q' : quiet++;
break;
case 'i' : interact++;
break;
case 'r' : recover_option++;
break;
case 'a' : cyl_align++;
break;
case 'n' : i++;
user_inodes = atoi(ap[i]);
argc--;
argv++;
break;
default : userr();
}
argc--;
argv++;
}
/* If interactive use is specified, clobber 'quiet' so fs params
get printed for the user. (-q -i is pretty stupid, but just
in case...)
*/
if (interact) quiet = 0;
if (argc != 3) {
fprintf(stderr, "Usage: mkfs diskname protofile\n");
exit(-1);
}
proto = argv[2];
special = argv[1];
fproto = fopen(proto, "r");
if (!fproto) {
fprintf(stderr, "%s: can't open proto file %s\n",
progname, proto);
exit(-1);
}
getstr(); /* ignore get boot image */
fs_blocks = getnum();
fs_isize = GOOD_INODE_TOTAL(fs_blocks);
add_inodes = (fs_blocks - 0x8000) / 4;
if (add_inodes < 0)
add_inodes = 0;
if (add_inodes > 4096)
add_inodes = 4096;
fs_isize += add_inodes;
fs_sectors = getnum();
#ifdef HP_DISK_MODEL
fs_sectors = NSECTORS;
#endif
cg_align = 1; /* No alignment */
i_align = 1; /* No alignment */
/* protect user against himself. sigh... */
if ((fs_blocks == 0) ||
(fs_isize == 0) ||
(fs_sectors == 0) ||
(cg_align == 0) ||
(i_align == 0))
{
fprintf(stderr,"%s: Illegal zero numeric parameter.\n",
progname);
exit(-1);
}
/* NOTE (daveh 12/13/89): if fs_blocks is greater than the 2 gig
* limit, we must use the new writeb system call (and cannot
* proceed if this is not present, ie on a 3.2 kernel).
* This works only on a char device, so we need in that case to find
* the equivalent char device even if invoked on block.
* Also, the structure of efs extent descriptors imposes, for the
* moment, an 8 gig absolute upper limit. (24 bit offsets).
*/
if (fs_blocks > 0xffffff)
{
fprintf(stderr,"%s: filesystems may not exceed 8 gigabytes!\n",
progname);
exit(-1);
}
if ((fs_fd = open(special, O_RDWR|O_CREAT, 0664)) < 0)
{
fprintf(stderr, "%s: can't open %s\n",
progname, special);
exit(-1);
}
/*
* Now seek to the end of the file and then back.
*/
fp = lseek(fs_fd, (fs_blocks * 512) - 1, SEEK_SET);
if (fp != (fs_blocks * 512 -1)) {
perror(progname);
exit(-1);
}
if (write(fs_fd, "\000", 1) != 1) {
perror(progname);
exit(-1);
}
/*
* Compute the number of blocks used by the bootblock, superblock
* & bitmap, rounded up to a cg_align boundary. The first cylinder
* group starts there.
* The replicated superblock at the end effectively consumes cg_align
* blocks.
*/
bitmap_bytes = (fs_blocks + BITSPERBYTE - 1) / BITSPERBYTE;
bitmap_blocks = BTOBB(bitmap_bytes);
start_blocks = 2 + bitmap_blocks;
if (i = (start_blocks % cg_align))
start_blocks += (cg_align - i);
firstcg = start_blocks;
end_blocks = cg_align;
cgblocks = fs_blocks - (start_blocks + end_blocks);
iblocks = (fs_isize + EFS_INOPBB - 1) / EFS_INOPBB;
/*
* If file system is itty bitty, handle it specially
*/
if (short_form &&
fs_blocks < 2 * good_cg_size(cgblocks, iblocks, cg_align))
{
is_ittybitty = 1;
ncg = 1;
firstcg = start_blocks = 2 + bitmap_blocks;
end_blocks = 1;
cg_fsize = fs_blocks - (start_blocks + end_blocks);
cg_align = i_align = 1;
if (cg_fsize <= 0)
{
fprintf(stderr,
"%s: not enough space to make filesystem\n",
progname);
exit(-1);
}
cg_isize = (fs_isize + EFS_INOPBB - 1) / EFS_INOPBB;
if (cg_isize >= cg_fsize)
{
fprintf(stderr,
"%s: invalid filesystem size - inodes larger than partition\n",
progname);
exit(-1);
}
buildfs();
exit(0);
}
/*
* If size of cylinder groups not yet specified, compute a good
* size to efficiently fit the available space.
* Compute number of cylinder groups and blocks of inodes per cylinder
* group.
*/
if (!cg_fsize)
{
if (cyl_align)
cgsizepick(cgblocks, iblocks, cg_align,
&ncg, &cg_fsize, &cg_isize);
else
{
cg_fsize = good_cg_size(cgblocks, iblocks, cg_align);
ncg = cgblocks / cg_fsize;
cg_fsize = cgblocks / ncg;
cg_isize = cg_fsize / (cgblocks / iblocks);
}
}
else
{
/* round cg_fsize down to a cg_align multiple. Sanity check:
* if given cg_fsize is < align, complain & exit */
if (cg_fsize < cg_align)
{
fprintf(stderr,
"%s: invalid cgsize - smaller than specified alignment.\n",
progname);
exit(-1);
}
cg_fsize -= cg_fsize % cg_align;
if (cg_fsize > cgblocks)
{
fprintf(stderr,
"%s: invalid cgsize - larger than available space.\n",
progname);
exit(-1);
}
ncg = cgblocks / cg_fsize;
/* Now the inode computations:
* Adjust cg_isize to be a multiple of i_align. Round up or
* down, whichever is closest, unless this would make it zero!
*/
cg_isize = (iblocks + ncg - 1) / ncg;
if (i = (cg_isize % i_align))
{
if (cg_isize < i_align)
cg_isize = i_align;
else if (i < (i_align / 2))
cg_isize -= i;
else
cg_isize += (i_align - i);
}
}
if (cg_isize >= cg_fsize)
{
fprintf(stderr,
"%s: Ridiculous number of inodes; no space for data.\n",
progname);
exit(-1);
}
fs_isize = cg_isize * ncg * EFS_INOPBB;
buildfs();
fprintf(stderr, "%s: SUCCESSFUL COMPLETION\n", progname);
exit(0);
}
/*
* Get info about device size & geometry.
*/
getdevinfo()
{
int heads;
int add_inodes = 0;
if ((dev_blocks = findsize(special)) <= 0)
{
fprintf(stderr,"Can't determine size of %s\n", special);
blather();
exit(-1);
}
fs_blocks = dev_blocks; /* by default, use whole device */
if ((fs_sectors = findtrksize(special)) <= 0)
{
fprintf(stderr,"Can't determine tracksize of %s\n", special);
blather();
exit(-1);
}
if ((heads = findnumhead(special)) <= 0)
{
fprintf(stderr,"Can't get number of heads on %s\n", special);
blather();
exit(-1);
}
fs_isize = GOOD_INODE_TOTAL(fs_blocks);
/*
* On root partitions > 16MB, add 512 inodes for each additional MB.
*/
if (is_rootpart(special))
{
add_inodes = (fs_blocks - 0x8000) / 4;
if (add_inodes < 0)
add_inodes = 0;
if (add_inodes > 4096)
add_inodes = 4096;
}
fs_isize += add_inodes;
if (user_inodes)
{
if (user_inodes < fs_isize)
fprintf(stderr,"Warning: specified number of inodes %d is smaller than normal default %d\n", user_inodes, fs_isize);
fs_isize = user_inodes;
}
if (cyl_align)
{
cg_align = fs_sectors * heads;
i_align = fs_sectors * heads;
}
else
{
cg_align = 1;
i_align = 1;
}
}
blather()
{
fprintf(stderr,"Check that device exists and is writeable.\n");
fprintf(stderr,"(Note that it is possible for a special file\n");
fprintf(stderr,"to exist and have writeable permissions, but\n");
fprintf(stderr,"for there to be no real device corresponding\n");
fprintf(stderr,"to it).\n");
}
/*
* cgsizepick() chooses cylinder group size, inodes per cylinder group,
* and number of cylinder groups for a filesystem. It holds inodes per
* cylinder group to a constant and varies the data blocks per cylinder
* group by up to 20 percent either way, looking for the least amount
* of space wastage. This algorithm is optimized for large align values.
*/
void
cgsizepick(blocks, iblocks, align, addr_ncg, addr_cg_fsize, addr_cg_isize)
int blocks, iblocks, align;
long *addr_ncg, *addr_cg_fsize, *addr_cg_isize;
{
int cgs, cguess, i, waste;
int savecgs, savecgsize, savecgisize;
int prevwaste = blocks;
int mincgsize, mult;
mult = (blocks + iblocks - 1) / iblocks;
/* initial guess for cgsize, in blocks */
mincgsize = mult * align;
cguess = (good_cg_size(blocks, iblocks, align) / mincgsize) * mincgsize;
if (cguess == 0)
cguess = mincgsize;
savecgisize = align * (cguess / mincgsize);
for (i = 0; i < cguess/5; i += align)
{
cgs = blocks / (cguess + i);
waste = blocks % (cguess + i);
if (waste < prevwaste)
{
prevwaste = waste;
savecgs = cgs;
savecgsize = cguess + i;
}
cgs = blocks / (cguess - i);
waste = blocks % (cguess - i);
if (waste < prevwaste)
{
prevwaste = waste;
savecgs = cgs;
savecgsize = cguess - i;
}
}
*addr_ncg = savecgs;
*addr_cg_fsize = savecgsize;
*addr_cg_isize = savecgisize;
}
/*
* Initialize the superblock
*/
void
init_sb()
{
fs = &sblock.fs;
bzero(fs, sizeof(*fs));
fs->fs_size = start_blocks + (ncg * cg_fsize);
fs->fs_firstcg = firstcg;
fs->fs_cgfsize = cg_fsize;
fs->fs_cgisize = cg_isize;
fs->fs_sectors = fs_sectors;
fs->fs_ncg = ncg;
time(&fs->fs_time);
fs->fs_magic = EFS_MAGIC;
fs->fs_bmblock = 0; /* to force 3.2 defaults... */
fs->fs_heads = 10; /* BOGUS: but 3.2 mount expects nonzero... */
if (short_form)
fs->fs_replsb = dev_blocks - 1;
else
{
/* If long form invocation, put in a replicate superblock only if
* the given size parameter is "close" to the actual device size.
* "Close" is arbitrarily defined as less than cgfsize...
* Of course, if we're working on a file not a device (eg miniroot
* construction) we don't have a dev size so never put in a replsb! */
if (!dev_blocks || ((dev_blocks - fs_blocks) >= cg_fsize))
fs->fs_replsb = 0;
else
fs->fs_replsb = dev_blocks - 1;
}
fs->fs_bmsize = (fs->fs_size + BITSPERBYTE - 1) / BITSPERBYTE;
fs->fs_tfree = ncg * (fs->fs_cgfsize - fs->fs_cgisize);
strncpy(fs->fs_fpack, "nopack", sizeof(fs->fs_fpack));
strncpy(fs->fs_fname, "noname", sizeof(fs->fs_fname));
/* subtract 2 for inodes 0 & 1 which are never used */
fs->fs_tinode = ncg * fs->fs_cgisize * EFS_INOPBB - 2;
/* If this is recovery-option, mark the superblock dirty to
* force an fsck before it can be mounted.
*/
if (recover_option)
fs->fs_dirty = EFS_DIRTY;
EFS_SETUP_SUPERB(fs);
}
/*
* Clear out the inodes
*/
void
clear_inodes()
{
register struct efs_dinode *di;
register char *cp;
register int ilist_bytes;
register int i;
unsigned long igen;
ilist_bytes = BBTOB(cg_isize);
cp = calloc(1, ilist_bytes);
if (!cp) {
fprintf(stderr,
"%s: can't initialize inodes - out of memory\n",
progname);
exit(-1);
}
/*
* Construct generation number based on current time to avoid
* generation number collisions when a server recreates an
* exported file system.
*/
time(&igen);
di = (struct efs_dinode *)cp;
for (i = EFS_INOPBB * cg_isize; i > 0; i-- ) {
di->di_gen = igen;
di++;
}
for (i = 0; i < ncg; i++) {
if (c_writeb(fs_fd, cp, EFS_CGIMIN(fs, i), cg_isize) != cg_isize)
{
fprintf(stderr, "%s: inode write failed\n", progname);
exit(-1);
}
}
free(cp);
}
/*
* Fill bootblock with zeros
*/
void
clear_bootblock()
{
long buf[BBSIZE / sizeof(long)];
bzero(buf, sizeof(buf));
if (c_writeb(fs_fd, buf, 0, 1) != 1)
{
fprintf(stderr, "%s: boot block write failed\n", progname);
exit(-1);
}
}
/*
* Initialize the bitmap
*/
void
init_bitmap()
{
register int i, j;
register daddr_t bn;
bitmap = malloc(BBTOB(bitmap_blocks));
if (!bitmap)
{
fprintf(stderr, "%s: can't initialize bitmap - out of memory\n",
progname);
exit(-1);
}
/*
* Start the bitmap out as entirely used. Then clear out
* the regions that are allocatable.
*/
bzero(bitmap, BBTOB(bitmap_blocks));
for (i = 0; i < ncg; i++) {
bn = EFS_CGIMIN(fs, i) + fs->fs_cgisize;
for (j = fs->fs_cgisize; j < fs->fs_cgfsize; j++) {
bset(bitmap, bn);
bn++;
}
}
}
error()
{
int old_errno;
old_errno = errno;
fprintf(stderr, "%s: i/o error\n", progname);
errno = old_errno;
perror(progname);
exit(-1);
}
/*
* Get a character from the canned string or from the input file
*/
getch()
{
if (charp)
return (*charp++);
return (getc(fproto));
}
/*
* Get a string from the input file. Gleefully stolen from system V mkfs.
*/
void
getstr(f)
FILE *f;
{
register int i, c;
loop:
switch(c = getch()) {
case ' ':
case '\t':
case '\n':
goto loop;
case '\0':
fprintf(stderr, "%s: premature EOF on %s\n", progname, proto);
exit(-1);
case ':':
while (getch() != '\n')
;
goto loop;
}
i = 0;
do {
string[i++] = c;
c = getch();
}
while((c != ' ') && (c != '\t') && (c != '\n') && (c != '\0'))
;
string[i] = '\0';
/* printf("getstr: %s\n", string); */
}
/*
* Convert a string into a decimal number.
*/
long
getnum()
{
register int i, c;
register long n;
getstr();
n = 0;
for (i = 0; c = string[i]; i++) {
if ((c < '0') || (c > '9')) {
fprintf(stderr, "%s: %s is a bad number\n",
progname, string);
exit(-1);
}
n = n * 10 + (c - '0');
}
return n;
}
/*
* Parse the proto file
*/
parseproto(parent, child)
ino_t parent, child;
{
struct efs_dinode *di;
int val;
int i;
int fd;
int nb;
int majdev, mindev;
char buf[16384];
int fmt, mode, uid, gid;
ino_t newino;
int islostfound;
/* get first word */
getstr();
/* decode mode spec */
switch (string[0]) {
case '-': fmt = IFREG; break;
case 'b': fmt = IFBLK; break;
case 'c': fmt = IFCHR; break;
case 'd': fmt = IFDIR; break;
case 'p': fmt = IFIFO; break;
case 'l': fmt = IFLNK; break;
default:
fprintf(stderr, "%s: got unknown mode spec %c\n",
progname, string[0]);
fprintf(stderr, "ABORTING!\n");
exit(-1);
}
mode = 0;
if (string[1] == 'u')
mode |= ISUID;
if (string[2] == 'g')
mode |= ISGID;
val = 0;
for (i = 3; i < 6; i++) {
if ((string[i] < '0') || (string[i] > '7')) {
fprintf(stderr,
"%s: %c/%s: bad octal mode digit\n",
progname, string[i], string);
exit(-1);
}
val = (val * 8) + (string[i] - '0');
}
mode |= val;
/* decode uid & gid */
uid = getnum();
gid = getnum();
/* initialize an inode */
efs_mknod(child, mode | fmt, uid, gid);
/* now create the requested file */
switch (fmt) {
case IFREG: /* regular file */
getstr();
if (debug)
printf("Creating %s\n", string);
efs_newregfile(child, string);
break;
case IFBLK: /* block device special */
case IFCHR: /* character device special */
majdev = getnum() & L_MAXMAJ;
mindev = getnum() & L_MAXMIN;
di = efs_iget(child);
efs_putdev(makedev(majdev, mindev), &di->di_u);
efs_iput(di, child);
break;
case IFIFO: /* fifo */
break;
case IFLNK: /* symbolic link */
/*
* Get contents of link from proto and write it to the
* link file.
*/
getstr();
if (debug)
printf("Creating link to %s\n", string);
efs_write(child, string, strlen(string));
break;
case IFDIR: /* directory */
if (child == EFS_ROOTINO) {
/*
* For the root inode, we just need to set its
* link count to two to get things going.
*/
di = efs_iget(parent);
di->di_nlink = 2;
efs_iput(di, parent);
} else {
/* increment link count of parent inode */
di = efs_iget(parent);
di->di_nlink++;
efs_iput(di, parent);
/* increment link count of new directory */
di = efs_iget(child);
di->di_nlink++;
efs_iput(di, child);
}
/* put "." and ".." entries into new directory */
efs_enter(child, child, ".");
efs_enter(child, parent, "..");
/* now read in the directories contents and install them */
for (;;) {
getstr();
if ((string[0] == '$') && (string[1] == '\0')) {
break;
}
/*
* Allocate a new inode for the file in the current
* directory. Then parse its attributes by recursing.
*/
newino = efs_allocino();
efs_enter(child, newino, string);
islostfound = strcmp(string, "lost+found") == 0;
parseproto(child, newino);
/*
* If inode is the lost+found inode, make it a big
* file so that fsck will work well. We do this
* after recursing to allow for the lost+found "."
* and ".." directories to be entered first.
*/
if (islostfound) {
efs_mklostandfound(newino);
}
}
break;
}
}
be_verbose()
{
printf("%s: %s: blocks=%d inodes=%d\n",
progname, special, fs_blocks, ncg * cg_isize * EFS_INOPBB);
printf("%s: %s: sectors=%d cgfsize=%d\n",
progname, special, fs_sectors, cg_fsize);
printf("%s: %s: cgalign=%d ialign=%d ncg=%d\n",
progname, special, cg_align, i_align, ncg);
printf("%s: %s: firstcg=%d cgisize=%d\n",
progname, special, firstcg, cg_isize);
printf("%s: %s: bitmap blocks=%d\n",
progname, special, bitmap_blocks);
}
buildfs()
{
char *realpath;
if (rawpath)
realpath = rawpath;
else
realpath = special;
if(interact || !quiet)
be_verbose();
if (interact) ask_confirm();
/* Added 2/14/90: the "recover" option just puts the superblock
* on disk without touching the rest of the space. This allows
* a last-ditch attempt to salvage a filesystem with fsck.
*/
if (!recover_option)
clear_other_sb();
init_sb();
/* allocate root inode */
fs->fs_tinode--;
if (!recover_option)
{
clear_inodes();
clear_bootblock();
init_bitmap();
/* finally, parse proto file and build fs */
parseproto(EFS_ROOTINO, EFS_ROOTINO);
/* sync to disk: updated superblock, its replica, & the bitmap. */
printf("Updating superblock, etc...\n");
if (c_writeb(fs_fd, bitmap, EFS_BITMAPBB, bitmap_blocks) !=
bitmap_blocks)
{
fprintf(stderr,"%s: Can't write to %s\n", progname, realpath);
exit(-1);
}
}
printf("Computing checksum.\n");
efs_checksum(); /* libefs relies on global fs, yucko!! */
printf("Writing to superblock again.\n");
if (c_writeb(fs_fd, &sblock, EFS_SUPERBB , 1) != 1)
{
fprintf(stderr,"%s: Can't write to %s\n", progname, realpath);
exit(-1);
}
/* We write the replicated superblock only if its pointer is
* present in the superblock.
* Also not written if it's recover_option.
*/
printf("May write to replicated superblock.\n");
if (!recover_option && fs->fs_replsb &&
(c_writeb(fs_fd, &sblock, fs->fs_replsb, 1) != 1))
{
fprintf(stderr,"%s: Can't write to %s\n", progname, realpath);
exit(-1);
}
}
ask_confirm()
{
char rb[100];
#ifndef SVR0
if (!isatty(0)) return; /* don't hang if stdin isn't a tty! */
printf("%s: is %s correct? (y/n?) ", progname, special);
fflush(stdout);
gets(rb);
if ((*rb != 'y') && (*rb != 'Y')) exit(0);
#endif
}
ismounted(name)
char *name;
{
struct ustat ustatarea;
struct stat sb;
if (stat(name, &sb) < 0) return (0);
if (((sb.st_mode & S_IFMT) != S_IFCHR) &&
((sb.st_mode & S_IFMT) != S_IFBLK)) return (0);
if (ustat(sb.st_rdev,&ustatarea) >= 0) return (1);
else return (0);
}
userr()
{
fprintf(stderr, "Usage: %s [-q] [-a] [-i] [-r] [-n inodes] special [proto]\n", progname);
fprintf(stderr, " %s [-q] [-i] [-r] special blocks inodes heads sectors cgfsize cgalign ialign [proto]\n", progname);
exit(-1);
}
/* clear out any potential superblocks in partitions which have their
superblock overlapped by the one we are making.
This is primarily for the benefit of the
disk tools, so that we don't tell the user one of the overlapped
partitions is a valid mount point. It's only an issue when the
other partition had a filesystem, and none of the info we write
happens to clobber it.
If we can't do it for some reason, be silent about it, since this
is just icing on the cake...
Also, only try on regular disks, not floppies & logical volumes.
*/
void
clear_other_sb()
{
struct stat sb;
unsigned token;
struct ptinfo *pt;
struct ustat ustatarea;
/* libdisk.a code needs to have name starting with /dev/rdsk */
if(stat(special, &sb) == -1)
return;
#ifdef notdef
This does not seem to be necessary. If there are no complaints,
the next person to see this should go ahead and remove it.
jh - Oct 1992
/* this is gross, in that new disk drivers need to be added.
* the extra dksc and jag got missed. someday... */
switch (major( sb.st_rdev))
{
case DKIP_MAJOR :
case DKSC_MAJOR :
case DKSC1_MAJOR :
case DKSC2_MAJOR :
case DKSC3_MAJOR :
case JAG0_MAJOR :
case JAG1_MAJOR :
case JAG2_MAJOR :
case JAG3_MAJOR :
case JAG4_MAJOR :
case JAG5_MAJOR :
case JAG6_MAJOR :
case JAG7_MAJOR :
case XYL_MAJOR :
case IPI_MAJOR :
case IPI1_MAJOR :
case IPI2_MAJOR :
case IPI3_MAJOR : break;
default : return;
};
#endif
/* libdisk.a code needs to have name starting with /dev/rdsk */
if (!rawpath)
rawpath = findrawpath(special);
if (!rawpath) return; /* too bad... */
if((token=setdiskinfo(rawpath, "/etc/fstab", 0)) == 0)
return;
while(pt = getpartition(token)) {
struct stat sb2;
if(stat(partname(rawpath, pt->ptnum), &sb2) == 0 &&
sb2.st_rdev == sb.st_rdev) {
register struct ptinfo **pover;
for(pover=pt->overlaps; pover && *pover; pover++) {
char *pn;
if(stat((pn=partname(rawpath, (*pover)->ptnum)), &sb2) == 0) {
if(ustat(sb2.st_rdev,&ustatarea) == 0) {
fprintf(stderr,
"%s overlaps a mounted filesystem on partition %d\n",
special, (*pover)->ptnum);
exit(1);
}
if(((*pover)->pstartblk+EFS_SUPERBOFF) >= pt->pstartblk &&
((*pover)->pstartblk+EFS_SUPERBOFF) <= (pt->pstartblk+pt->psize))
zap_sb(pn);
}
}
enddiskinfo(token);
return;
}
}
enddiskinfo(token);
}
/* zap a superblock on an overlapped partition; see comments at
clear_other_sb().
*/
zap_sb(name)
char *name;
{
struct efs e;
int fd;
bzero(&e, sizeof(e));
fd = open(name, O_WRONLY);
if(fd == -1) { /* shouldn't happen, but perhaps done by non-super user */
return;
}
/* it's conceivable that this could fail if a vh is bad, since the
sb isn't necessarily inside the fs we are making */
if(lseek(fd, (long) EFS_SUPERBOFF, 0) == EFS_SUPERBOFF)
(void)write(fd, (char *) &e, BBTOB(BTOBB(sizeof(e))));
close(fd);
}
/* To retain backward compatibility of the 3.3 executable on a 3.2 kernel,
* we'll use regular writes unless we know we have to go over 2 gig.
*/
c_writeb(fd, buf, block, nblocks)
int fd;
char *buf;
int block, nblocks;
{
int bytes;
if (use_writeb)
{
if (writeb(fd, buf, block, nblocks) == nblocks)
return (nblocks);
}
else
{
lseek(fd, block * BBSIZE, 0);
bytes = nblocks * BBSIZE;
if (write(fd, buf, bytes) == bytes)
return (nblocks);
}
return (-1);
}
/* Now a new function efs_newregfile(). This creates a new regular file
* of the given size; this file may have indirect extents.
* This is done to avoid rehacking the
* whole of libefs to know about indirect extents!
* Basically to get round the size limitations when a mkfs proto specifies
* a humongo file! Rather than successive extends, we allocate the space
* all at once (since we're working from a regular file we already
* know the size).
*
* Assumptions:
*
* efs_mknod has been called to allocate & initialize the dinode.
*
* The bitmap is in core and is correctly representative of blocks
* used so far.
*/
struct extent * findex();
char *malloc();
void busyout();
void
efs_newregfile(ino, name)
ino_t ino;
char *name;
{
struct stat sb;
int len;
daddr_t blocks, allocblocks;
int fd;
register struct efs_dinode *di;
register daddr_t bn;
register struct extent *exbase = NULL;
register struct extent *ex, *foundex;
int extents, exbufsize;
int copysize, copyoffset, copied;
char *copybuf = NULL;
static daddr_t curblock = 2; /* starting place to search for
* free blocks */
int largestextent;
register int i;
int numindirs = 0;
int indirblocks;
if ((fd = open(name, O_RDONLY)) < 0)
{
fprintf(stderr,"%s: can't open %s\n", progname, name);
exit(-1);
}
if (fstat(fd, &sb) < 0)
{
fprintf(stderr,"%s: cannot stat %s\n",progname, name);
exit(-1);
}
if ((sb.st_mode & S_IFMT) != S_IFREG)
{
fprintf(stderr,"%s: %s is not a regular file\n",progname, name);
exit(-1);
}
len = sb.st_size;
blocks = (len + (BBSIZE - 1)) / BBSIZE;
di = efs_iget(ino);
/* Guess at number of extents & allocate space for them. We will
* realloc later if it turns out we need more; however since it's
* assumed we're creating in a virgin fs that is unlikely.
*/
exbufsize = blocks / 64;
exbase = (struct extent *)malloc(BBSIZE + exbufsize * sizeof (struct extent));
/* now allocate extent space from the bitmap until we've got enough
* extents to hold the file.
*/
allocblocks = 0;
extents = 0;
largestextent = 0;
while (allocblocks < blocks)
{
if ((foundex = findex(curblock, (blocks - allocblocks))) == NULL)
{
fprintf(stderr,"%s: cannot allocate space for file: %s\n", progname, name);
exit(-1);
}
if (foundex->ex_length > largestextent)
largestextent = foundex->ex_length;
curblock = foundex->ex_bn + foundex->ex_length;
ex = (exbase + extents);
ex->ex_magic = 0;
ex->ex_bn = foundex->ex_bn;
ex->ex_length = foundex->ex_length;
ex->ex_offset = allocblocks;
allocblocks += foundex->ex_length;
if (++extents == exbufsize)
{
exbufsize += 10;
if ((exbase = (struct extent *)realloc((char *)exbase, (BBSIZE + exbufsize * sizeof (struct extent)))) == NULL)
{
fprintf(stderr,"%s: cannot allocate space for file: %s\n", progname, name);
exit(-1);
}
}
}
if (extents > EFS_DIRECTEXTENTS)
{
indirblocks = ((BBSIZE - 1 + (extents * sizeof(struct extent))) / BBSIZE);
allocblocks = 0;
while (allocblocks < indirblocks)
{
if ((foundex = findex(curblock, (indirblocks - allocblocks))) == NULL)
{
fprintf(stderr,"%s: cannot allocate space for file: %s\n", progname, name);
exit(-1);
}
curblock = foundex->ex_bn + foundex->ex_length;
ex = &di->di_u.di_extents[numindirs];
ex->ex_magic = 0;
ex->ex_bn = foundex->ex_bn;
ex->ex_length = foundex->ex_length;
allocblocks += foundex->ex_length;
if (++numindirs == EFS_DIRECTEXTENTS)
{
fprintf(stderr,"%s: cannot allocate space for file: %s\n", progname, name);
exit(-1);
}
}
di->di_u.di_extents[0].ex_offset = numindirs;
}
/* Hokay. Now we've allocated all the extents needed to hold the new
* file's data (including indirect ones if any). Copy the data to the
* appropriate places.
*/
if ((copybuf = malloc(largestextent * BBSIZE)) == NULL)
{
fprintf(stderr,"%s: can't get buffer memory for file copy\n",
progname);
exit(-1);
}
for (i = 0, ex = exbase, copied = 0; i < extents; i++)
{
copysize = ex->ex_length * BBSIZE;
copyoffset = ex->ex_bn * BBSIZE;
bzero(copybuf, copysize);
if ((len - copied) < copysize) /* partial last block */
copysize = len - copied;
if (read(fd, copybuf, copysize) != copysize)
{
fprintf(stderr, "%s: error reading %s\n",
progname, name);
exit(-1);
}
/* set copysize back to BBSIZE multiple: we may be working
* on a raw device!
*/
copysize = ex->ex_length * BBSIZE;
lseek(fs_fd, copyoffset, 0);
if (write(fs_fd, copybuf, copysize) != copysize)
{
fprintf(stderr, "%s: error writing %s\n",
progname, name);
exit(-1);
}
copied += copysize;
ex++;
}
free (copybuf);
copybuf = NULL;
/* Data copied. Now the extents: if < EFS_DIRECTEXTENTS, they go
* in the dinode. If greater, they must be written to the indirect
* extents allocated for them; pointers to these are already in
* the dinode in that case.
*/
if (extents <= EFS_DIRECTEXTENTS)
{
for (i = 0, foundex = exbase, ex = di->di_u.di_extents; i < extents; i++, ex++, foundex++)
{
ex->ex_bn = foundex->ex_bn;
ex->ex_length = foundex->ex_length;
ex->ex_offset = foundex->ex_offset;
ex->ex_magic = 0;
}
}
else
{
copybuf = (char *)exbase;
for (i = 0, ex = di->di_u.di_extents; i < numindirs; i++, ex++)
{
copysize = ex->ex_length * BBSIZE;
copyoffset = ex->ex_bn * BBSIZE;
lseek(fs_fd, copyoffset, 0);
if (write(fs_fd, copybuf, copysize) != copysize)
{
fprintf(stderr, "%s: error writing %s\n",
progname, name);
exit(-1);
}
copybuf += copysize;
}
copybuf = NULL;
}
/* Busy out the appropriate parts of the bitmap. */
for (i = 0, ex = exbase; i < extents; i++, ex++)
busyout(ex->ex_bn, ex->ex_length);
if (numindirs)
for (i = 0, ex = di->di_u.di_extents; i < numindirs; i++, ex++)
busyout(ex->ex_bn, ex->ex_length);
di->di_size = len;
di->di_numextents = extents;
efs_iput(di, ino);
close (fd);
if (exbase)
free ((char *)exbase);
if (copybuf)
free ((char *)copybuf);
return;
}
static struct extent *
findex(block, nblocks)
register daddr_t block;
register int nblocks;
{
static struct extent ex;
register daddr_t nextblock = block;
register int foundblocks = 0;
if (nblocks > EFS_MAXEXTENTLEN)
nblocks = EFS_MAXEXTENTLEN;
/* first skip any nonfree blocks */
while (!btst(bitmap, nextblock))
{
nextblock++; /* side effect warning: btst is a macro! */
if (nextblock == (fs_blocks - 1))
return (NULL);
}
block = nextblock;
while ((foundblocks < nblocks) &&
(nextblock < fs_blocks) &&
btst(bitmap, nextblock))
{
foundblocks++;
nextblock++;
}
if (!foundblocks)
return (NULL);
ex.ex_bn = block;
ex.ex_length = foundblocks;
return (&ex);
}
static void
busyout(bn, len)
register daddr_t bn;
register int len;
{
while (len--)
{
bclr(bitmap, bn);
bn++; /* can't do it inside bclr: side effects!! */
fs->fs_tfree--;
}
}