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