Removed <lib>_pic.a file rules - libs are now built directly from shared
[unix-history] / sbin / newfs / mkfs.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1980, 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)mkfs.c 6.18 (Berkeley) 7/3/91";
36#endif /* not lint */
37
38#ifndef STANDALONE
39#include <stdio.h>
40#include <a.out.h>
41#endif
42
43#include <sys/param.h>
44#include <sys/time.h>
45#include <sys/wait.h>
46#include <sys/resource.h>
47#include <ufs/dinode.h>
48#include <ufs/fs.h>
49#include <ufs/dir.h>
50#include <sys/disklabel.h>
51#include <machine/endian.h>
52
53/*
54 * make file system for cylinder-group style file systems
55 */
56
57/*
58 * The size of a cylinder group is calculated by CGSIZE. The maximum size
59 * is limited by the fact that cylinder groups are at most one block.
60 * Its size is derived from the size of the maps maintained in the
61 * cylinder group and the (struct cg) size.
62 */
63#define CGSIZE(fs) \
64 /* base cg */ (sizeof(struct cg) + \
65 /* blktot size */ (fs)->fs_cpg * sizeof(long) + \
66 /* blks size */ (fs)->fs_cpg * (fs)->fs_nrpos * sizeof(short) + \
67 /* inode map */ howmany((fs)->fs_ipg, NBBY) + \
68 /* block map */ howmany((fs)->fs_cpg * (fs)->fs_spc / NSPF(fs), NBBY))
69
70/*
71 * We limit the size of the inode map to be no more than a
72 * third of the cylinder group space, since we must leave at
73 * least an equal amount of space for the block map.
74 *
75 * N.B.: MAXIPG must be a multiple of INOPB(fs).
76 */
77#define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
78
79#define UMASK 0755
80#define MAXINOPB (MAXBSIZE / sizeof(struct dinode))
81#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
82
83/*
84 * variables set up by front end.
85 */
86extern int mfs; /* run as the memory based filesystem */
87extern int Nflag; /* run mkfs without writing file system */
88extern int fssize; /* file system size */
89extern int ntracks; /* # tracks/cylinder */
90extern int nsectors; /* # sectors/track */
91extern int nphyssectors; /* # sectors/track including spares */
92extern int secpercyl; /* sectors per cylinder */
93extern int sectorsize; /* bytes/sector */
94extern int rpm; /* revolutions/minute of drive */
95extern int interleave; /* hardware sector interleave */
96extern int trackskew; /* sector 0 skew, per track */
97extern int headswitch; /* head switch time, usec */
98extern int trackseek; /* track-to-track seek, usec */
99extern int fsize; /* fragment size */
100extern int bsize; /* block size */
101extern int cpg; /* cylinders/cylinder group */
102extern int cpgflg; /* cylinders/cylinder group flag was given */
103extern int minfree; /* free space threshold */
104extern int opt; /* optimization preference (space or time) */
105extern int density; /* number of bytes per inode */
106extern int maxcontig; /* max contiguous blocks to allocate */
107extern int rotdelay; /* rotational delay between blocks */
108extern int maxbpg; /* maximum blocks per file in a cyl group */
109extern int nrpos; /* # of distinguished rotational positions */
110extern int bbsize; /* boot block size */
111extern int sbsize; /* superblock size */
15637ed4 112extern caddr_t membase; /* start address of memory based filesystem */
15637ed4
RG
113
114union {
115 struct fs fs;
116 char pad[SBSIZE];
117} fsun;
118#define sblock fsun.fs
119struct csum *fscs;
120
121union {
122 struct cg cg;
123 char pad[MAXBSIZE];
124} cgun;
125#define acg cgun.cg
126
127struct dinode zino[MAXBSIZE / sizeof(struct dinode)];
128
129int fsi, fso;
130daddr_t alloc();
131
132mkfs(pp, fsys, fi, fo)
133 struct partition *pp;
134 char *fsys;
135 int fi, fo;
136{
137 register long i, mincpc, mincpg, inospercg;
138 long cylno, rpos, blk, j, warn = 0;
139 long used, mincpgcnt, bpcg;
140 long mapcramped, inodecramped;
141 long postblsize, rotblsize, totalsbsize;
15637ed4 142 time_t utime;
15637ed4
RG
143
144#ifndef STANDALONE
145 time(&utime);
146#endif
15637ed4
RG
147 fsi = fi;
148 fso = fo;
149 /*
150 * Validate the given file system size.
151 * Verify that its last block can actually be accessed.
152 */
153 if (fssize <= 0)
154 printf("preposterous size %d\n", fssize), exit(13);
155 wtfs(fssize - 1, sectorsize, (char *)&sblock);
156 /*
157 * collect and verify the sector and track info
158 */
159 sblock.fs_nsect = nsectors;
160 sblock.fs_ntrak = ntracks;
161 if (sblock.fs_ntrak <= 0)
162 printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
163 if (sblock.fs_nsect <= 0)
164 printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
165 /*
166 * collect and verify the block and fragment sizes
167 */
168 sblock.fs_bsize = bsize;
169 sblock.fs_fsize = fsize;
170 if (!POWEROF2(sblock.fs_bsize)) {
171 printf("block size must be a power of 2, not %d\n",
172 sblock.fs_bsize);
173 exit(16);
174 }
175 if (!POWEROF2(sblock.fs_fsize)) {
176 printf("fragment size must be a power of 2, not %d\n",
177 sblock.fs_fsize);
178 exit(17);
179 }
180 if (sblock.fs_fsize < sectorsize) {
181 printf("fragment size %d is too small, minimum is %d\n",
182 sblock.fs_fsize, sectorsize);
183 exit(18);
184 }
185 if (sblock.fs_bsize < MINBSIZE) {
186 printf("block size %d is too small, minimum is %d\n",
187 sblock.fs_bsize, MINBSIZE);
188 exit(19);
189 }
190 if (sblock.fs_bsize < sblock.fs_fsize) {
191 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
192 sblock.fs_bsize, sblock.fs_fsize);
193 exit(20);
194 }
195 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
196 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
197 /*
198 * Planning now for future expansion.
199 */
200# if (BYTE_ORDER == BIG_ENDIAN)
201 sblock.fs_qbmask.val[0] = 0;
202 sblock.fs_qbmask.val[1] = ~sblock.fs_bmask;
203 sblock.fs_qfmask.val[0] = 0;
204 sblock.fs_qfmask.val[1] = ~sblock.fs_fmask;
205# endif /* BIG_ENDIAN */
206# if (BYTE_ORDER == LITTLE_ENDIAN)
207 sblock.fs_qbmask.val[0] = ~sblock.fs_bmask;
208 sblock.fs_qbmask.val[1] = 0;
209 sblock.fs_qfmask.val[0] = ~sblock.fs_fmask;
210 sblock.fs_qfmask.val[1] = 0;
211# endif /* LITTLE_ENDIAN */
212 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
213 sblock.fs_bshift++;
214 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
215 sblock.fs_fshift++;
216 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
217 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
218 sblock.fs_fragshift++;
219 if (sblock.fs_frag > MAXFRAG) {
220 printf("fragment size %d is too small, minimum with block size %d is %d\n",
221 sblock.fs_fsize, sblock.fs_bsize,
222 sblock.fs_bsize / MAXFRAG);
223 exit(21);
224 }
225 sblock.fs_nrpos = nrpos;
226 sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
227 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct dinode);
228 sblock.fs_nspf = sblock.fs_fsize / sectorsize;
229 for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
230 sblock.fs_fsbtodb++;
231 sblock.fs_sblkno =
232 roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
233 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
234 roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
235 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
236 sblock.fs_cgoffset = roundup(
237 howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
238 for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
239 sblock.fs_cgmask <<= 1;
240 if (!POWEROF2(sblock.fs_ntrak))
241 sblock.fs_cgmask <<= 1;
242 /*
243 * Validate specified/determined secpercyl
244 * and calculate minimum cylinders per group.
245 */
246 sblock.fs_spc = secpercyl;
247 for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
248 sblock.fs_cpc > 1 && (i & 1) == 0;
249 sblock.fs_cpc >>= 1, i >>= 1)
250 /* void */;
251 mincpc = sblock.fs_cpc;
252 bpcg = sblock.fs_spc * sectorsize;
253 inospercg = roundup(bpcg / sizeof(struct dinode), INOPB(&sblock));
254 if (inospercg > MAXIPG(&sblock))
255 inospercg = MAXIPG(&sblock);
256 used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
257 mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
258 sblock.fs_spc);
259 mincpg = roundup(mincpgcnt, mincpc);
260 /*
261 * Insure that cylinder group with mincpg has enough space
262 * for block maps
263 */
264 sblock.fs_cpg = mincpg;
265 sblock.fs_ipg = inospercg;
266 mapcramped = 0;
267 while (CGSIZE(&sblock) > sblock.fs_bsize) {
268 mapcramped = 1;
269 if (sblock.fs_bsize < MAXBSIZE) {
270 sblock.fs_bsize <<= 1;
271 if ((i & 1) == 0) {
272 i >>= 1;
273 } else {
274 sblock.fs_cpc <<= 1;
275 mincpc <<= 1;
276 mincpg = roundup(mincpgcnt, mincpc);
277 sblock.fs_cpg = mincpg;
278 }
279 sblock.fs_frag <<= 1;
280 sblock.fs_fragshift += 1;
281 if (sblock.fs_frag <= MAXFRAG)
282 continue;
283 }
284 if (sblock.fs_fsize == sblock.fs_bsize) {
285 printf("There is no block size that");
286 printf(" can support this disk\n");
287 exit(22);
288 }
289 sblock.fs_frag >>= 1;
290 sblock.fs_fragshift -= 1;
291 sblock.fs_fsize <<= 1;
292 sblock.fs_nspf <<= 1;
293 }
294 /*
295 * Insure that cylinder group with mincpg has enough space for inodes
296 */
297 inodecramped = 0;
298 used *= sectorsize;
299 inospercg = roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
300 sblock.fs_ipg = inospercg;
301 while (inospercg > MAXIPG(&sblock)) {
302 inodecramped = 1;
303 if (mincpc == 1 || sblock.fs_frag == 1 ||
304 sblock.fs_bsize == MINBSIZE)
305 break;
306 printf("With a block size of %d %s %d\n", sblock.fs_bsize,
307 "minimum bytes per inode is",
308 (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
309 sblock.fs_bsize >>= 1;
310 sblock.fs_frag >>= 1;
311 sblock.fs_fragshift -= 1;
312 mincpc >>= 1;
313 sblock.fs_cpg = roundup(mincpgcnt, mincpc);
314 if (CGSIZE(&sblock) > sblock.fs_bsize) {
315 sblock.fs_bsize <<= 1;
316 break;
317 }
318 mincpg = sblock.fs_cpg;
319 inospercg =
320 roundup((mincpg * bpcg - used) / density, INOPB(&sblock));
321 sblock.fs_ipg = inospercg;
322 }
323 if (inodecramped) {
324 if (inospercg > MAXIPG(&sblock)) {
325 printf("Minimum bytes per inode is %d\n",
326 (mincpg * bpcg - used) / MAXIPG(&sblock) + 1);
327 } else if (!mapcramped) {
328 printf("With %d bytes per inode, ", density);
329 printf("minimum cylinders per group is %d\n", mincpg);
330 }
331 }
332 if (mapcramped) {
333 printf("With %d sectors per cylinder, ", sblock.fs_spc);
334 printf("minimum cylinders per group is %d\n", mincpg);
335 }
336 if (inodecramped || mapcramped) {
337 if (sblock.fs_bsize != bsize)
338 printf("%s to be changed from %d to %d\n",
339 "This requires the block size",
340 bsize, sblock.fs_bsize);
341 if (sblock.fs_fsize != fsize)
342 printf("\t%s to be changed from %d to %d\n",
343 "and the fragment size",
344 fsize, sblock.fs_fsize);
345 exit(23);
346 }
347 /*
348 * Calculate the number of cylinders per group
349 */
350 sblock.fs_cpg = cpg;
351 if (sblock.fs_cpg % mincpc != 0) {
352 printf("%s groups must have a multiple of %d cylinders\n",
353 cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
354 sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
355 if (!cpgflg)
356 cpg = sblock.fs_cpg;
357 }
358 /*
359 * Must insure there is enough space for inodes
360 */
361 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
362 INOPB(&sblock));
363 while (sblock.fs_ipg > MAXIPG(&sblock)) {
364 inodecramped = 1;
365 sblock.fs_cpg -= mincpc;
366 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
367 INOPB(&sblock));
368 }
369 /*
370 * Must insure there is enough space to hold block map
371 */
372 while (CGSIZE(&sblock) > sblock.fs_bsize) {
373 mapcramped = 1;
374 sblock.fs_cpg -= mincpc;
375 sblock.fs_ipg = roundup((sblock.fs_cpg * bpcg - used) / density,
376 INOPB(&sblock));
377 }
378 sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
379 if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
380 printf("panic (fs_cpg * fs_spc) % NSPF != 0");
381 exit(24);
382 }
383 if (sblock.fs_cpg < mincpg) {
384 printf("cylinder groups must have at least %d cylinders\n",
385 mincpg);
386 exit(25);
387 } else if (sblock.fs_cpg != cpg) {
388 if (!cpgflg)
389 printf("Warning: ");
390 else if (!mapcramped && !inodecramped)
391 exit(26);
392 if (mapcramped && inodecramped)
393 printf("Block size and bytes per inode restrict");
394 else if (mapcramped)
395 printf("Block size restricts");
396 else
397 printf("Bytes per inode restrict");
398 printf(" cylinders per group to %d.\n", sblock.fs_cpg);
399 if (cpgflg)
400 exit(27);
401 }
402 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
403 /*
404 * Now have size for file system and nsect and ntrak.
405 * Determine number of cylinders and blocks in the file system.
406 */
407 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
408 sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
409 if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
410 sblock.fs_ncyl++;
411 warn = 1;
412 }
413 if (sblock.fs_ncyl < 1) {
414 printf("file systems must have at least one cylinder\n");
415 exit(28);
416 }
417 /*
418 * Determine feasability/values of rotational layout tables.
419 *
420 * The size of the rotational layout tables is limited by the
421 * size of the superblock, SBSIZE. The amount of space available
422 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
423 * The size of these tables is inversely proportional to the block
424 * size of the file system. The size increases if sectors per track
425 * are not powers of two, because more cylinders must be described
426 * by the tables before the rotational pattern repeats (fs_cpc).
427 */
428 sblock.fs_interleave = interleave;
429 sblock.fs_trackskew = trackskew;
430 sblock.fs_npsect = nphyssectors;
431 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
432 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
433 if (sblock.fs_ntrak == 1) {
434 sblock.fs_cpc = 0;
435 goto next;
436 }
437 postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(short);
438 rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
439 totalsbsize = sizeof(struct fs) + rotblsize;
440 if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
441 /* use old static table space */
442 sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
443 (char *)(&sblock.fs_link);
444 sblock.fs_rotbloff = &sblock.fs_space[0] -
445 (u_char *)(&sblock.fs_link);
446 } else {
447 /* use dynamic table space */
448 sblock.fs_postbloff = &sblock.fs_space[0] -
449 (u_char *)(&sblock.fs_link);
450 sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
451 totalsbsize += postblsize;
452 }
453 if (totalsbsize > SBSIZE ||
454 sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
455 printf("%s %s %d %s %d.%s",
456 "Warning: insufficient space in super block for\n",
457 "rotational layout tables with nsect", sblock.fs_nsect,
458 "and ntrak", sblock.fs_ntrak,
459 "\nFile system performance may be impaired.\n");
460 sblock.fs_cpc = 0;
461 goto next;
462 }
463 sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
464 /*
465 * calculate the available blocks for each rotational position
466 */
467 for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
468 for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
469 fs_postbl(&sblock, cylno)[rpos] = -1;
470 for (i = (rotblsize - 1) * sblock.fs_frag;
471 i >= 0; i -= sblock.fs_frag) {
472 cylno = cbtocylno(&sblock, i);
473 rpos = cbtorpos(&sblock, i);
474 blk = fragstoblks(&sblock, i);
475 if (fs_postbl(&sblock, cylno)[rpos] == -1)
476 fs_rotbl(&sblock)[blk] = 0;
477 else
478 fs_rotbl(&sblock)[blk] =
479 fs_postbl(&sblock, cylno)[rpos] - blk;
480 fs_postbl(&sblock, cylno)[rpos] = blk;
481 }
482next:
483 /*
484 * Compute/validate number of cylinder groups.
485 */
486 sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
487 if (sblock.fs_ncyl % sblock.fs_cpg)
488 sblock.fs_ncg++;
489 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
490 i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
491 if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
492 printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
493 cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
494 sblock.fs_fpg / sblock.fs_frag);
495 printf("number of cylinders per cylinder group (%d) %s.\n",
496 sblock.fs_cpg, "must be increased");
497 exit(29);
498 }
499 j = sblock.fs_ncg - 1;
500 if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
501 cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
502 if (j == 0) {
503 printf("Filesystem must have at least %d sectors\n",
504 NSPF(&sblock) *
505 (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
506 exit(30);
507 }
508 printf("Warning: inode blocks/cyl group (%d) >= data blocks (%d) in last\n",
509 (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
510 i / sblock.fs_frag);
511 printf(" cylinder group. This implies %d sector(s) cannot be allocated.\n",
512 i * NSPF(&sblock));
513 sblock.fs_ncg--;
514 sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
515 sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
516 NSPF(&sblock);
517 warn = 0;
518 }
519 if (warn && !mfs) {
520 printf("Warning: %d sector(s) in last cylinder unallocated\n",
521 sblock.fs_spc -
522 (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
523 * sblock.fs_spc));
524 }
525 /*
526 * fill in remaining fields of the super block
527 */
528 sblock.fs_csaddr = cgdmin(&sblock, 0);
529 sblock.fs_cssize =
530 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
531 i = sblock.fs_bsize / sizeof(struct csum);
532 sblock.fs_csmask = ~(i - 1);
533 for (sblock.fs_csshift = 0; i > 1; i >>= 1)
534 sblock.fs_csshift++;
535 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
536 sblock.fs_magic = FS_MAGIC;
537 sblock.fs_rotdelay = rotdelay;
538 sblock.fs_minfree = minfree;
539 sblock.fs_maxcontig = maxcontig;
540 sblock.fs_headswitch = headswitch;
541 sblock.fs_trkseek = trackseek;
542 sblock.fs_maxbpg = maxbpg;
543 sblock.fs_rps = rpm / 60;
544 sblock.fs_optim = opt;
545 sblock.fs_cgrotor = 0;
546 sblock.fs_cstotal.cs_ndir = 0;
547 sblock.fs_cstotal.cs_nbfree = 0;
548 sblock.fs_cstotal.cs_nifree = 0;
549 sblock.fs_cstotal.cs_nffree = 0;
550 sblock.fs_fmod = 0;
551 sblock.fs_ronly = 0;
552 /*
553 * Dump out summary information about file system.
554 */
555 if (!mfs) {
556 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
557 fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
558 "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
559 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
560 (float)sblock.fs_size * sblock.fs_fsize * 1e-6,
561 sblock.fs_ncg, sblock.fs_cpg,
562 (float)sblock.fs_fpg * sblock.fs_fsize * 1e-6,
563 sblock.fs_ipg);
564 }
565 /*
566 * Now build the cylinders group blocks and
567 * then print out indices of cylinder groups.
568 */
569 if (!mfs)
570 printf("super-block backups (for fsck -b #) at:");
571 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
572 initcg(cylno, utime);
573 if (mfs)
574 continue;
575 if (cylno % 9 == 0)
576 printf("\n");
577 printf(" %d,", fsbtodb(&sblock, cgsblock(&sblock, cylno)));
578 }
579 if (!mfs)
580 printf("\n");
581 if (Nflag && !mfs)
582 exit(0);
583 /*
584 * Now construct the initial file system,
585 * then write out the super-block.
586 */
587 fsinit(utime);
588 sblock.fs_time = utime;
589 wtfs(SBOFF / sectorsize, sbsize, (char *)&sblock);
590 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
591 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
592 sblock.fs_cssize - i < sblock.fs_bsize ?
593 sblock.fs_cssize - i : sblock.fs_bsize,
594 ((char *)fscs) + i);
595 /*
596 * Write out the duplicate super blocks
597 */
598 for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
599 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)),
600 sbsize, (char *)&sblock);
601 /*
602 * Update information about this partion in pack
603 * label, to that it may be updated on disk.
604 */
605 pp->p_fstype = FS_BSDFFS;
606 pp->p_fsize = sblock.fs_fsize;
607 pp->p_frag = sblock.fs_frag;
608 pp->p_cpg = sblock.fs_cpg;
15637ed4
RG
609}
610
611/*
612 * Initialize a cylinder group.
613 */
614initcg(cylno, utime)
615 int cylno;
616 time_t utime;
617{
618 daddr_t cbase, d, dlower, dupper, dmax;
619 long i, j, s;
620 register struct csum *cs;
621
622 /*
623 * Determine block bounds for cylinder group.
624 * Allow space for super block summary information in first
625 * cylinder group.
626 */
627 cbase = cgbase(&sblock, cylno);
628 dmax = cbase + sblock.fs_fpg;
629 if (dmax > sblock.fs_size)
630 dmax = sblock.fs_size;
631 dlower = cgsblock(&sblock, cylno) - cbase;
632 dupper = cgdmin(&sblock, cylno) - cbase;
633 if (cylno == 0)
634 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
635 cs = fscs + cylno;
636 acg.cg_time = utime;
637 acg.cg_magic = CG_MAGIC;
638 acg.cg_cgx = cylno;
639 if (cylno == sblock.fs_ncg - 1)
640 acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
641 else
642 acg.cg_ncyl = sblock.fs_cpg;
643 acg.cg_niblk = sblock.fs_ipg;
644 acg.cg_ndblk = dmax - cbase;
645 acg.cg_cs.cs_ndir = 0;
646 acg.cg_cs.cs_nffree = 0;
647 acg.cg_cs.cs_nbfree = 0;
648 acg.cg_cs.cs_nifree = 0;
649 acg.cg_rotor = 0;
650 acg.cg_frotor = 0;
651 acg.cg_irotor = 0;
652 acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_link);
653 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(long);
654 acg.cg_iusedoff = acg.cg_boff +
655 sblock.fs_cpg * sblock.fs_nrpos * sizeof(short);
656 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
657 acg.cg_nextfreeoff = acg.cg_freeoff +
658 howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
659 for (i = 0; i < sblock.fs_frag; i++) {
660 acg.cg_frsum[i] = 0;
661 }
662 bzero((caddr_t)cg_inosused(&acg), acg.cg_freeoff - acg.cg_iusedoff);
663 acg.cg_cs.cs_nifree += sblock.fs_ipg;
664 if (cylno == 0)
665 for (i = 0; i < ROOTINO; i++) {
666 setbit(cg_inosused(&acg), i);
667 acg.cg_cs.cs_nifree--;
668 }
669 for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
670 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
671 sblock.fs_bsize, (char *)zino);
672 bzero((caddr_t)cg_blktot(&acg), acg.cg_boff - acg.cg_btotoff);
673 bzero((caddr_t)cg_blks(&sblock, &acg, 0),
674 acg.cg_iusedoff - acg.cg_boff);
675 bzero((caddr_t)cg_blksfree(&acg), acg.cg_nextfreeoff - acg.cg_freeoff);
676 if (cylno > 0) {
677 /*
678 * In cylno 0, beginning space is reserved
679 * for boot and super blocks.
680 */
681 for (d = 0; d < dlower; d += sblock.fs_frag) {
682 setblock(&sblock, cg_blksfree(&acg), d/sblock.fs_frag);
683 acg.cg_cs.cs_nbfree++;
684 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
685 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
686 [cbtorpos(&sblock, d)]++;
687 }
688 sblock.fs_dsize += dlower;
689 }
690 sblock.fs_dsize += acg.cg_ndblk - dupper;
691 if (i = dupper % sblock.fs_frag) {
692 acg.cg_frsum[sblock.fs_frag - i]++;
693 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
694 setbit(cg_blksfree(&acg), dupper);
695 acg.cg_cs.cs_nffree++;
696 }
697 }
698 for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
699 setblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
700 acg.cg_cs.cs_nbfree++;
701 cg_blktot(&acg)[cbtocylno(&sblock, d)]++;
702 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))
703 [cbtorpos(&sblock, d)]++;
704 d += sblock.fs_frag;
705 }
706 if (d < dmax - cbase) {
707 acg.cg_frsum[dmax - cbase - d]++;
708 for (; d < dmax - cbase; d++) {
709 setbit(cg_blksfree(&acg), d);
710 acg.cg_cs.cs_nffree++;
711 }
712 }
713 sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
714 sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
715 sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
716 sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
717 *cs = acg.cg_cs;
718 wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
719 sblock.fs_bsize, (char *)&acg);
720}
721
722/*
723 * initialize the file system
724 */
725struct dinode node;
726
727#ifdef LOSTDIR
728#define PREDEFDIR 3
729#else
730#define PREDEFDIR 2
731#endif
732
733struct direct root_dir[] = {
734 { ROOTINO, sizeof(struct direct), 1, "." },
735 { ROOTINO, sizeof(struct direct), 2, ".." },
736#ifdef LOSTDIR
737 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
738#endif
739};
740#ifdef LOSTDIR
741struct direct lost_found_dir[] = {
742 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
743 { ROOTINO, sizeof(struct direct), 2, ".." },
744 { 0, DIRBLKSIZ, 0, 0 },
745};
746#endif
747char buf[MAXBSIZE];
748
749fsinit(utime)
750 time_t utime;
751{
752 int i;
753
754 /*
755 * initialize the node
756 */
757 node.di_atime = utime;
758 node.di_mtime = utime;
759 node.di_ctime = utime;
760#ifdef LOSTDIR
761 /*
762 * create the lost+found directory
763 */
764 (void)makedir(lost_found_dir, 2);
765 for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ)
766 bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(&lost_found_dir[2]));
767 node.di_mode = IFDIR | UMASK;
768 node.di_nlink = 2;
769 node.di_size = sblock.fs_bsize;
770 node.di_db[0] = alloc(node.di_size, node.di_mode);
771 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
772 wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf);
773 iput(&node, LOSTFOUNDINO);
774#endif
775 /*
776 * create the root directory
777 */
778 if (mfs)
779 node.di_mode = IFDIR | 01777;
780 else
781 node.di_mode = IFDIR | UMASK;
782 node.di_nlink = PREDEFDIR;
783 node.di_size = makedir(root_dir, PREDEFDIR);
784 node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode);
785 node.di_blocks = btodb(fragroundup(&sblock, node.di_size));
786 wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf);
787 iput(&node, ROOTINO);
788}
789
790/*
791 * construct a set of directory entries in "buf".
792 * return size of directory.
793 */
794makedir(protodir, entries)
795 register struct direct *protodir;
796 int entries;
797{
798 char *cp;
799 int i, spcleft;
800
801 spcleft = DIRBLKSIZ;
802 for (cp = buf, i = 0; i < entries - 1; i++) {
803 protodir[i].d_reclen = DIRSIZ(&protodir[i]);
804 bcopy(&protodir[i], cp, protodir[i].d_reclen);
805 cp += protodir[i].d_reclen;
806 spcleft -= protodir[i].d_reclen;
807 }
808 protodir[i].d_reclen = spcleft;
809 bcopy(&protodir[i], cp, DIRSIZ(&protodir[i]));
810 return (DIRBLKSIZ);
811}
812
813/*
814 * allocate a block or frag
815 */
816daddr_t
817alloc(size, mode)
818 int size;
819 int mode;
820{
821 int i, frag;
822 daddr_t d;
823
824 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
825 (char *)&acg);
826 if (acg.cg_magic != CG_MAGIC) {
827 printf("cg 0: bad magic number\n");
828 return (0);
829 }
830 if (acg.cg_cs.cs_nbfree == 0) {
831 printf("first cylinder group ran out of space\n");
832 return (0);
833 }
834 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
835 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag))
836 goto goth;
837 printf("internal error: can't find block in cyl 0\n");
838 return (0);
839goth:
840 clrblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag);
841 acg.cg_cs.cs_nbfree--;
842 sblock.fs_cstotal.cs_nbfree--;
843 fscs[0].cs_nbfree--;
844 if (mode & IFDIR) {
845 acg.cg_cs.cs_ndir++;
846 sblock.fs_cstotal.cs_ndir++;
847 fscs[0].cs_ndir++;
848 }
849 cg_blktot(&acg)[cbtocylno(&sblock, d)]--;
850 cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--;
851 if (size != sblock.fs_bsize) {
852 frag = howmany(size, sblock.fs_fsize);
853 fscs[0].cs_nffree += sblock.fs_frag - frag;
854 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
855 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
856 acg.cg_frsum[sblock.fs_frag - frag]++;
857 for (i = frag; i < sblock.fs_frag; i++)
858 setbit(cg_blksfree(&acg), d + i);
859 }
860 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
861 (char *)&acg);
862 return (d);
863}
864
865/*
866 * Allocate an inode on the disk
867 */
868iput(ip, ino)
869 register struct dinode *ip;
870 register ino_t ino;
871{
872 struct dinode buf[MAXINOPB];
873 daddr_t d;
874 int c;
875
876 c = itog(&sblock, ino);
877 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
878 (char *)&acg);
879 if (acg.cg_magic != CG_MAGIC) {
880 printf("cg 0: bad magic number\n");
881 exit(31);
882 }
883 acg.cg_cs.cs_nifree--;
884 setbit(cg_inosused(&acg), ino);
885 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize,
886 (char *)&acg);
887 sblock.fs_cstotal.cs_nifree--;
888 fscs[0].cs_nifree--;
889 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
890 printf("fsinit: inode value out of range (%d).\n", ino);
891 exit(32);
892 }
893 d = fsbtodb(&sblock, itod(&sblock, ino));
894 rdfs(d, sblock.fs_bsize, buf);
895 buf[itoo(&sblock, ino)] = *ip;
896 wtfs(d, sblock.fs_bsize, buf);
897}
898
15637ed4
RG
899/*
900 * read a block from the file system
901 */
902rdfs(bno, size, bf)
903 daddr_t bno;
904 int size;
905 char *bf;
906{
907 int n;
908
909 if (mfs) {
910 bcopy(membase + bno * sectorsize, bf, size);
911 return;
912 }
913 if (lseek(fsi, bno * sectorsize, 0) < 0) {
914 printf("seek error: %ld\n", bno);
915 perror("rdfs");
916 exit(33);
917 }
918 n = read(fsi, bf, size);
919 if(n != size) {
920 printf("read error: %ld\n", bno);
921 perror("rdfs");
922 exit(34);
923 }
924}
925
926/*
927 * write a block to the file system
928 */
929wtfs(bno, size, bf)
930 daddr_t bno;
931 int size;
932 char *bf;
933{
934 int n;
935
936 if (mfs) {
937 bcopy(bf, membase + bno * sectorsize, size);
938 return;
939 }
940 if (Nflag)
941 return;
942 if (lseek(fso, bno * sectorsize, 0) < 0) {
943 printf("seek error: %ld\n", bno);
944 perror("wtfs");
945 exit(35);
946 }
947 n = write(fso, bf, size);
948 if(n != size) {
949 printf("write error: %ld\n", bno);
950 perror("wtfs");
951 exit(36);
952 }
953}
954
955/*
956 * check if a block is available
957 */
958isblock(fs, cp, h)
959 struct fs *fs;
960 unsigned char *cp;
961 int h;
962{
963 unsigned char mask;
964
965 switch (fs->fs_frag) {
966 case 8:
967 return (cp[h] == 0xff);
968 case 4:
969 mask = 0x0f << ((h & 0x1) << 2);
970 return ((cp[h >> 1] & mask) == mask);
971 case 2:
972 mask = 0x03 << ((h & 0x3) << 1);
973 return ((cp[h >> 2] & mask) == mask);
974 case 1:
975 mask = 0x01 << (h & 0x7);
976 return ((cp[h >> 3] & mask) == mask);
977 default:
978#ifdef STANDALONE
979 printf("isblock bad fs_frag %d\n", fs->fs_frag);
980#else
981 fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);
982#endif
983 return (0);
984 }
985}
986
987/*
988 * take a block out of the map
989 */
990clrblock(fs, cp, h)
991 struct fs *fs;
992 unsigned char *cp;
993 int h;
994{
995 switch ((fs)->fs_frag) {
996 case 8:
997 cp[h] = 0;
998 return;
999 case 4:
1000 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1001 return;
1002 case 2:
1003 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1004 return;
1005 case 1:
1006 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1007 return;
1008 default:
1009#ifdef STANDALONE
1010 printf("clrblock bad fs_frag %d\n", fs->fs_frag);
1011#else
1012 fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);
1013#endif
1014 return;
1015 }
1016}
1017
1018/*
1019 * put a block into the map
1020 */
1021setblock(fs, cp, h)
1022 struct fs *fs;
1023 unsigned char *cp;
1024 int h;
1025{
1026 switch (fs->fs_frag) {
1027 case 8:
1028 cp[h] = 0xff;
1029 return;
1030 case 4:
1031 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1032 return;
1033 case 2:
1034 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1035 return;
1036 case 1:
1037 cp[h >> 3] |= (0x01 << (h & 0x7));
1038 return;
1039 default:
1040#ifdef STANDALONE
1041 printf("setblock bad fs_frag %d\n", fs->fs_frag);
1042#else
1043 fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);
1044#endif
1045 return;
1046 }
1047}