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