2 * Copyright (c) 1980, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)mkfs.c 8.11 (Berkeley) 5/3/95
34 * $FreeBSD: src/sbin/newfs/mkfs.c,v 1.29.2.6 2001/09/21 19:15:21 dillon Exp $
35 * $DragonFly: src/sbin/newfs/mkfs.c,v 1.14 2007/05/20 19:29:21 dillon Exp $
43 * make file system for cylinder-group style file systems
47 * We limit the size of the inode map to be no more than a
48 * third of the cylinder group space, since we must leave at
49 * least an equal amount of space for the block map.
51 * N.B.: MAXIPG must be a multiple of INOPB(fs).
53 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
56 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
57 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
60 #error "mkfs.c: STANDALONE compilation no longer supported"
64 * variables set up by front end.
66 extern int mfs
; /* run as the memory based filesystem */
67 extern char *mfs_mtpt
; /* mount point for mfs */
68 extern struct stat mfs_mtstat
; /* stat prior to mount */
69 extern int Nflag
; /* run mkfs without writing file system */
70 extern int Oflag
; /* format as an 4.3BSD file system */
71 extern int Uflag
; /* enable soft updates for file system */
72 extern u_long fssize
; /* file system size */
73 extern int ntracks
; /* # tracks/cylinder */
74 extern int nsectors
; /* # sectors/track */
75 extern int nphyssectors
; /* # sectors/track including spares */
76 extern int secpercyl
; /* sectors per cylinder */
77 extern int sectorsize
; /* bytes/sector */
78 extern int realsectorsize
; /* bytes/sector in hardware*/
79 extern int rpm
; /* revolutions/minute of drive */
80 extern int interleave
; /* hardware sector interleave */
81 extern int trackskew
; /* sector 0 skew, per track */
82 extern int fsize
; /* fragment size */
83 extern int bsize
; /* block size */
84 extern int cpg
; /* cylinders/cylinder group */
85 extern int cpgflg
; /* cylinders/cylinder group flag was given */
86 extern int minfree
; /* free space threshold */
87 extern int opt
; /* optimization preference (space or time) */
88 extern int density
; /* number of bytes per inode */
89 extern int maxcontig
; /* max contiguous blocks to allocate */
90 extern int rotdelay
; /* rotational delay between blocks */
91 extern int maxbpg
; /* maximum blocks per file in a cyl group */
92 extern int nrpos
; /* # of distinguished rotational positions */
93 extern int bbsize
; /* boot block size */
94 extern int sbsize
; /* superblock size */
95 extern int avgfilesize
; /* expected average file size */
96 extern int avgfilesperdir
; /* expected number of files per directory */
97 extern caddr_t membase
; /* start address of memory based filesystem */
98 extern char * filename
;
99 extern struct disktab geom
;
101 extern void fatal(const char *fmt
, ...);
107 #define sblock fsun.fs
116 struct ufs1_dinode zino
[MAXBSIZE
/ sizeof(struct ufs1_dinode
)];
119 static fsnode_t copyroot
;
120 static fsnode_t copyhlinks
;
124 daddr_t
alloc(int, int);
125 long calcipg(long, long, off_t
*);
126 static int charsperline(void);
127 void clrblock(struct fs
*, unsigned char *, int);
129 void initcg(int, time_t);
130 int isblock(struct fs
*, unsigned char *, int);
131 void iput(struct ufs1_dinode
*, ino_t
);
132 int makedir(struct direct
*, int);
133 void parentready(int);
134 void rdfs(daddr_t
, int, char *);
135 void setblock(struct fs
*, unsigned char *, int);
137 void wtfs(daddr_t
, int, char *);
138 void wtfsflush(void);
141 int parentready_signalled
;
144 mkfs(char *fsys
, int fi
, int fo
, const char *mfscopy
)
146 long i
, mincpc
, mincpg
, inospercg
;
147 long cylno
, rpos
, blk
, j
, emitwarn
= 0;
148 long used
, mincpgcnt
, bpcg
;
150 long mapcramped
, inodecramped
;
151 long postblsize
, rotblsize
, totalsbsize
;
156 char tmpbuf
[100]; /* XXX this will break in about 2,500 years */
170 signal(SIGUSR1
, parentready
);
171 if ((child
= fork()) != 0) {
178 copyroot
= FSCopy(©hlinks
, mfscopy
);
179 signal(SIGUSR1
, started
);
180 kill(child
, SIGUSR1
);
181 while (waitpid(child
, &status
, 0) != child
)
183 exit(WEXITSTATUS(status
));
190 omask
= sigblock(sigmask(SIGUSR1
));
191 while (parentready_signalled
== 0)
194 if (filename
!= NULL
) {
195 unsigned char buf
[BUFSIZ
];
199 fd
= open(filename
, O_RDWR
|O_TRUNC
|O_CREAT
, 0644);
201 err(12, "%s", filename
);
202 l1
= fssize
* sectorsize
;
205 for (l
= 0; l
< fssize
* (u_long
)sectorsize
; l
+= l1
) {
206 w
= write(fd
, buf
, l1
);
207 if (w
< 0 || (u_long
)w
!= l1
)
208 err(12, "%s", filename
);
210 membase
= mmap(NULL
, fssize
* sectorsize
,
211 PROT_READ
|PROT_WRITE
,
213 if (membase
== MAP_FAILED
)
217 membase
= mmap(NULL
, fssize
* sectorsize
,
218 PROT_READ
|PROT_WRITE
,
219 MAP_SHARED
|MAP_ANON
, -1, 0);
220 if (membase
== MAP_FAILED
)
221 errx(13, "mmap (anonymous memory) failed");
227 sblock
.fs_inodefmt
= FS_42INODEFMT
;
228 sblock
.fs_maxsymlinklen
= 0;
230 sblock
.fs_inodefmt
= FS_44INODEFMT
;
231 sblock
.fs_maxsymlinklen
= MAXSYMLINKLEN
;
234 sblock
.fs_flags
|= FS_DOSOFTDEP
;
236 * Validate the given file system size.
237 * Verify that its last block can actually be accessed.
240 printf("preposterous size %lu\n", fssize
), exit(13);
241 wtfs(fssize
- (realsectorsize
/ DEV_BSIZE
), realsectorsize
,
244 * collect and verify the sector and track info
246 sblock
.fs_nsect
= nsectors
;
247 sblock
.fs_ntrak
= ntracks
;
248 if (sblock
.fs_ntrak
<= 0)
249 printf("preposterous ntrak %d\n", sblock
.fs_ntrak
), exit(14);
250 if (sblock
.fs_nsect
<= 0)
251 printf("preposterous nsect %d\n", sblock
.fs_nsect
), exit(15);
253 * collect and verify the filesystem density info
255 sblock
.fs_avgfilesize
= avgfilesize
;
256 sblock
.fs_avgfpdir
= avgfilesperdir
;
257 if (sblock
.fs_avgfilesize
<= 0)
258 printf("illegal expected average file size %d\n",
259 sblock
.fs_avgfilesize
), exit(14);
260 if (sblock
.fs_avgfpdir
<= 0)
261 printf("illegal expected number of files per directory %d\n",
262 sblock
.fs_avgfpdir
), exit(15);
264 * collect and verify the block and fragment sizes
266 sblock
.fs_bsize
= bsize
;
267 sblock
.fs_fsize
= fsize
;
268 if (!POWEROF2(sblock
.fs_bsize
)) {
269 printf("block size must be a power of 2, not %d\n",
273 if (!POWEROF2(sblock
.fs_fsize
)) {
274 printf("fragment size must be a power of 2, not %d\n",
278 if (sblock
.fs_fsize
< sectorsize
) {
279 printf("fragment size %d is too small, minimum is %d\n",
280 sblock
.fs_fsize
, sectorsize
);
283 if (sblock
.fs_bsize
< MINBSIZE
) {
284 printf("block size %d is too small, minimum is %d\n",
285 sblock
.fs_bsize
, MINBSIZE
);
288 if (sblock
.fs_bsize
< sblock
.fs_fsize
) {
289 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
290 sblock
.fs_bsize
, sblock
.fs_fsize
);
293 sblock
.fs_bmask
= ~(sblock
.fs_bsize
- 1);
294 sblock
.fs_fmask
= ~(sblock
.fs_fsize
- 1);
295 sblock
.fs_qbmask
= ~sblock
.fs_bmask
;
296 sblock
.fs_qfmask
= ~sblock
.fs_fmask
;
297 for (sblock
.fs_bshift
= 0, i
= sblock
.fs_bsize
; i
> 1; i
>>= 1)
299 for (sblock
.fs_fshift
= 0, i
= sblock
.fs_fsize
; i
> 1; i
>>= 1)
301 sblock
.fs_frag
= numfrags(&sblock
, sblock
.fs_bsize
);
302 for (sblock
.fs_fragshift
= 0, i
= sblock
.fs_frag
; i
> 1; i
>>= 1)
303 sblock
.fs_fragshift
++;
304 if (sblock
.fs_frag
> MAXFRAG
) {
305 printf("fragment size %d is too small, minimum with block size %d is %d\n",
306 sblock
.fs_fsize
, sblock
.fs_bsize
,
307 sblock
.fs_bsize
/ MAXFRAG
);
310 sblock
.fs_nrpos
= nrpos
;
311 sblock
.fs_nindir
= sblock
.fs_bsize
/ sizeof(daddr_t
);
312 sblock
.fs_inopb
= sblock
.fs_bsize
/ sizeof(struct ufs1_dinode
);
313 sblock
.fs_nspf
= sblock
.fs_fsize
/ sectorsize
;
314 for (sblock
.fs_fsbtodb
= 0, i
= NSPF(&sblock
); i
> 1; i
>>= 1)
317 roundup(howmany(bbsize
+ sbsize
, sblock
.fs_fsize
), sblock
.fs_frag
);
318 sblock
.fs_cblkno
= (daddr_t
)(sblock
.fs_sblkno
+
319 roundup(howmany(sbsize
, sblock
.fs_fsize
), sblock
.fs_frag
));
320 sblock
.fs_iblkno
= sblock
.fs_cblkno
+ sblock
.fs_frag
;
321 sblock
.fs_cgoffset
= roundup(
322 howmany(sblock
.fs_nsect
, NSPF(&sblock
)), sblock
.fs_frag
);
323 for (sblock
.fs_cgmask
= 0xffffffff, i
= sblock
.fs_ntrak
; i
> 1; i
>>= 1)
324 sblock
.fs_cgmask
<<= 1;
325 if (!POWEROF2(sblock
.fs_ntrak
))
326 sblock
.fs_cgmask
<<= 1;
327 sblock
.fs_maxfilesize
= sblock
.fs_bsize
* NDADDR
- 1;
328 for (sizepb
= sblock
.fs_bsize
, i
= 0; i
< NIADDR
; i
++) {
329 sizepb
*= NINDIR(&sblock
);
330 sblock
.fs_maxfilesize
+= sizepb
;
333 * Validate specified/determined secpercyl
334 * and calculate minimum cylinders per group.
336 sblock
.fs_spc
= secpercyl
;
337 for (sblock
.fs_cpc
= NSPB(&sblock
), i
= sblock
.fs_spc
;
338 sblock
.fs_cpc
> 1 && (i
& 1) == 0;
339 sblock
.fs_cpc
>>= 1, i
>>= 1)
341 mincpc
= sblock
.fs_cpc
;
342 bpcg
= sblock
.fs_spc
* sectorsize
;
343 inospercg
= roundup(bpcg
/ sizeof(struct ufs1_dinode
), INOPB(&sblock
));
344 if (inospercg
> MAXIPG(&sblock
))
345 inospercg
= MAXIPG(&sblock
);
346 used
= (sblock
.fs_iblkno
+ inospercg
/ INOPF(&sblock
)) * NSPF(&sblock
);
347 mincpgcnt
= howmany(sblock
.fs_cgoffset
* (~sblock
.fs_cgmask
) + used
,
349 mincpg
= roundup(mincpgcnt
, mincpc
);
351 * Ensure that cylinder group with mincpg has enough space
354 sblock
.fs_cpg
= mincpg
;
355 sblock
.fs_ipg
= inospercg
;
357 sblock
.fs_contigsumsize
= MIN(maxcontig
, FS_MAXCONTIG
);
359 while (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
361 if (sblock
.fs_bsize
< MAXBSIZE
) {
362 sblock
.fs_bsize
<<= 1;
368 mincpg
= roundup(mincpgcnt
, mincpc
);
369 sblock
.fs_cpg
= mincpg
;
371 sblock
.fs_frag
<<= 1;
372 sblock
.fs_fragshift
+= 1;
373 if (sblock
.fs_frag
<= MAXFRAG
)
376 if (sblock
.fs_fsize
== sblock
.fs_bsize
) {
377 printf("There is no block size that");
378 printf(" can support this disk\n");
381 sblock
.fs_frag
>>= 1;
382 sblock
.fs_fragshift
-= 1;
383 sblock
.fs_fsize
<<= 1;
384 sblock
.fs_nspf
<<= 1;
387 * Ensure that cylinder group with mincpg has enough space for inodes.
390 inospercg
= calcipg(mincpg
, bpcg
, &usedb
);
391 sblock
.fs_ipg
= inospercg
;
392 while (inospercg
> MAXIPG(&sblock
)) {
394 if (mincpc
== 1 || sblock
.fs_frag
== 1 ||
395 sblock
.fs_bsize
== MINBSIZE
)
397 printf("With a block size of %d %s %d\n", sblock
.fs_bsize
,
398 "minimum bytes per inode is",
399 (int)((mincpg
* (off_t
)bpcg
- usedb
)
400 / MAXIPG(&sblock
) + 1));
401 sblock
.fs_bsize
>>= 1;
402 sblock
.fs_frag
>>= 1;
403 sblock
.fs_fragshift
-= 1;
405 sblock
.fs_cpg
= roundup(mincpgcnt
, mincpc
);
406 if (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
407 sblock
.fs_bsize
<<= 1;
410 mincpg
= sblock
.fs_cpg
;
411 inospercg
= calcipg(mincpg
, bpcg
, &usedb
);
412 sblock
.fs_ipg
= inospercg
;
415 if (inospercg
> MAXIPG(&sblock
)) {
416 printf("Minimum bytes per inode is %d\n",
417 (int)((mincpg
* (off_t
)bpcg
- usedb
)
418 / MAXIPG(&sblock
) + 1));
419 } else if (!mapcramped
) {
420 printf("With %d bytes per inode, ", density
);
421 printf("minimum cylinders per group is %ld\n", mincpg
);
425 printf("With %d sectors per cylinder, ", sblock
.fs_spc
);
426 printf("minimum cylinders per group is %ld\n", mincpg
);
428 if (inodecramped
|| mapcramped
) {
429 if (sblock
.fs_bsize
!= bsize
)
430 printf("%s to be changed from %d to %d\n",
431 "This requires the block size",
432 bsize
, sblock
.fs_bsize
);
433 if (sblock
.fs_fsize
!= fsize
)
434 printf("\t%s to be changed from %d to %d\n",
435 "and the fragment size",
436 fsize
, sblock
.fs_fsize
);
440 * Calculate the number of cylinders per group
443 if (sblock
.fs_cpg
% mincpc
!= 0) {
444 printf("%s groups must have a multiple of %ld cylinders\n",
445 cpgflg
? "Cylinder" : "Warning: cylinder", mincpc
);
446 sblock
.fs_cpg
= roundup(sblock
.fs_cpg
, mincpc
);
451 * Must ensure there is enough space for inodes.
453 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
454 while (sblock
.fs_ipg
> MAXIPG(&sblock
)) {
456 sblock
.fs_cpg
-= mincpc
;
457 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
460 * Must ensure there is enough space to hold block map.
462 while (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
464 sblock
.fs_cpg
-= mincpc
;
465 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
467 sblock
.fs_fpg
= (sblock
.fs_cpg
* sblock
.fs_spc
) / NSPF(&sblock
);
468 if ((sblock
.fs_cpg
* sblock
.fs_spc
) % NSPB(&sblock
) != 0) {
469 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
472 if (sblock
.fs_cpg
< mincpg
) {
473 printf("cylinder groups must have at least %ld cylinders\n",
476 } else if (sblock
.fs_cpg
!= cpg
) {
479 else if (!mapcramped
&& !inodecramped
)
482 if (mapcramped
&& inodecramped
)
483 printf("Block size and bytes per inode restrict");
485 printf("Block size restricts");
487 printf("Bytes per inode restrict");
488 printf(" cylinders per group to %d.\n", sblock
.fs_cpg
);
493 sblock
.fs_cgsize
= fragroundup(&sblock
, CGSIZE(&sblock
));
495 * Now have size for file system and nsect and ntrak.
496 * Determine number of cylinders and blocks in the file system.
498 sblock
.fs_size
= fssize
= dbtofsb(&sblock
, fssize
);
499 sblock
.fs_ncyl
= fssize
* NSPF(&sblock
) / sblock
.fs_spc
;
500 if ((long)fssize
* NSPF(&sblock
) > sblock
.fs_ncyl
* sblock
.fs_spc
) {
504 if (sblock
.fs_ncyl
< 1) {
505 printf("file systems must have at least one cylinder\n");
509 * Determine feasability/values of rotational layout tables.
511 * The size of the rotational layout tables is limited by the
512 * size of the superblock, SBSIZE. The amount of space available
513 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
514 * The size of these tables is inversely proportional to the block
515 * size of the file system. The size increases if sectors per track
516 * are not powers of two, because more cylinders must be described
517 * by the tables before the rotational pattern repeats (fs_cpc).
519 sblock
.fs_interleave
= interleave
;
520 sblock
.fs_trackskew
= trackskew
;
521 sblock
.fs_npsect
= nphyssectors
;
522 sblock
.fs_postblformat
= FS_DYNAMICPOSTBLFMT
;
523 sblock
.fs_sbsize
= fragroundup(&sblock
, sizeof(struct fs
));
524 if (sblock
.fs_sbsize
> SBSIZE
)
525 sblock
.fs_sbsize
= SBSIZE
;
526 if (sblock
.fs_ntrak
== 1) {
530 postblsize
= sblock
.fs_nrpos
* sblock
.fs_cpc
* sizeof(int16_t);
531 rotblsize
= sblock
.fs_cpc
* sblock
.fs_spc
/ NSPB(&sblock
);
532 totalsbsize
= sizeof(struct fs
) + rotblsize
;
533 if (sblock
.fs_nrpos
== 8 && sblock
.fs_cpc
<= 16) {
534 /* use old static table space */
535 sblock
.fs_postbloff
= (char *)(&sblock
.fs_opostbl
[0][0]) -
536 (char *)(&sblock
.fs_firstfield
);
537 sblock
.fs_rotbloff
= &sblock
.fs_space
[0] -
538 (u_char
*)(&sblock
.fs_firstfield
);
540 /* use dynamic table space */
541 sblock
.fs_postbloff
= &sblock
.fs_space
[0] -
542 (u_char
*)(&sblock
.fs_firstfield
);
543 sblock
.fs_rotbloff
= sblock
.fs_postbloff
+ postblsize
;
544 totalsbsize
+= postblsize
;
546 if (totalsbsize
> SBSIZE
||
547 sblock
.fs_nsect
> (1 << NBBY
) * NSPB(&sblock
)) {
548 printf("%s %s %d %s %d.%s",
549 "Warning: insufficient space in super block for\n",
550 "rotational layout tables with nsect", sblock
.fs_nsect
,
551 "and ntrak", sblock
.fs_ntrak
,
552 "\nFile system performance may be impaired.\n");
556 sblock
.fs_sbsize
= fragroundup(&sblock
, totalsbsize
);
557 if (sblock
.fs_sbsize
> SBSIZE
)
558 sblock
.fs_sbsize
= SBSIZE
;
560 * calculate the available blocks for each rotational position
562 for (cylno
= 0; cylno
< sblock
.fs_cpc
; cylno
++)
563 for (rpos
= 0; rpos
< sblock
.fs_nrpos
; rpos
++)
564 fs_postbl(&sblock
, cylno
)[rpos
] = -1;
565 for (i
= (rotblsize
- 1) * sblock
.fs_frag
;
566 i
>= 0; i
-= sblock
.fs_frag
) {
567 cylno
= cbtocylno(&sblock
, i
);
568 rpos
= cbtorpos(&sblock
, i
);
569 blk
= fragstoblks(&sblock
, i
);
570 if (fs_postbl(&sblock
, cylno
)[rpos
] == -1)
571 fs_rotbl(&sblock
)[blk
] = 0;
573 fs_rotbl(&sblock
)[blk
] =
574 fs_postbl(&sblock
, cylno
)[rpos
] - blk
;
575 fs_postbl(&sblock
, cylno
)[rpos
] = blk
;
579 * Compute/validate number of cylinder groups.
581 sblock
.fs_ncg
= sblock
.fs_ncyl
/ sblock
.fs_cpg
;
582 if (sblock
.fs_ncyl
% sblock
.fs_cpg
)
584 sblock
.fs_dblkno
= sblock
.fs_iblkno
+ sblock
.fs_ipg
/ INOPF(&sblock
);
585 i
= MIN(~sblock
.fs_cgmask
, sblock
.fs_ncg
- 1);
586 if (cgdmin(&sblock
, i
) - cgbase(&sblock
, i
) >= sblock
.fs_fpg
) {
587 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
588 cgdmin(&sblock
, i
) - cgbase(&sblock
, i
) / sblock
.fs_frag
,
589 (long)(sblock
.fs_fpg
/ sblock
.fs_frag
));
590 printf("number of cylinders per cylinder group (%d) %s.\n",
591 sblock
.fs_cpg
, "must be increased");
594 j
= sblock
.fs_ncg
- 1;
595 if ((i
= fssize
- j
* sblock
.fs_fpg
) < sblock
.fs_fpg
&&
596 cgdmin(&sblock
, j
) - cgbase(&sblock
, j
) > i
) {
598 printf("Filesystem must have at least %d sectors\n",
600 (cgdmin(&sblock
, 0) + 3 * sblock
.fs_frag
));
604 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
605 (cgdmin(&sblock
, j
) - cgbase(&sblock
, j
)) / sblock
.fs_frag
,
608 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
611 sblock
.fs_ncyl
-= sblock
.fs_ncyl
% sblock
.fs_cpg
;
612 sblock
.fs_size
= fssize
= sblock
.fs_ncyl
* sblock
.fs_spc
/
616 if (emitwarn
&& !mfs
) {
617 printf("Warning: %lu sector(s) in last cylinder unallocated\n",
619 (fssize
* NSPF(&sblock
) - (sblock
.fs_ncyl
- 1)
623 * fill in remaining fields of the super block
625 sblock
.fs_csaddr
= cgdmin(&sblock
, 0);
627 fragroundup(&sblock
, sblock
.fs_ncg
* sizeof(struct csum
));
629 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
630 * longer used. However, we still initialise them so that the
631 * filesystem remains compatible with old kernels.
633 i
= sblock
.fs_bsize
/ sizeof(struct csum
);
634 sblock
.fs_csmask
= ~(i
- 1);
635 for (sblock
.fs_csshift
= 0; i
> 1; i
>>= 1)
637 fscs
= (struct csum
*)calloc(1, sblock
.fs_cssize
);
639 errx(31, "calloc failed");
640 sblock
.fs_magic
= FS_MAGIC
;
641 sblock
.fs_rotdelay
= rotdelay
;
642 sblock
.fs_minfree
= minfree
;
643 sblock
.fs_maxcontig
= maxcontig
;
644 sblock
.fs_maxbpg
= maxbpg
;
645 sblock
.fs_rps
= rpm
/ 60;
646 sblock
.fs_optim
= opt
;
647 sblock
.fs_cgrotor
= 0;
648 sblock
.fs_cstotal
.cs_ndir
= 0;
649 sblock
.fs_cstotal
.cs_nbfree
= 0;
650 sblock
.fs_cstotal
.cs_nifree
= 0;
651 sblock
.fs_cstotal
.cs_nffree
= 0;
656 sblock
.fs_id
[0] = (long)utime
;
657 sblock
.fs_id
[1] = random();
661 * Dump out summary information about file system.
664 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
665 fsys
, sblock
.fs_size
* NSPF(&sblock
), sblock
.fs_ncyl
,
666 "cylinders", sblock
.fs_ntrak
, sblock
.fs_nsect
);
667 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
668 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
669 (float)sblock
.fs_size
* sblock
.fs_fsize
* B2MBFACTOR
,
670 sblock
.fs_ncg
, sblock
.fs_cpg
,
671 (float)sblock
.fs_fpg
* sblock
.fs_fsize
* B2MBFACTOR
,
673 sblock
.fs_flags
& FS_DOSOFTDEP
? " SOFTUPDATES" : "");
677 * Now build the cylinders group blocks and
678 * then print out indices of cylinder groups.
681 printf("super-block backups (for fsck -b #) at:\n");
683 width
= charsperline();
684 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++) {
685 initcg(cylno
, utime
);
688 j
= snprintf(tmpbuf
, sizeof(tmpbuf
), " %ld%s",
689 fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
690 cylno
< (sblock
.fs_ncg
-1) ? "," : "" );
691 if (i
+ j
>= width
) {
696 printf("%s", tmpbuf
);
704 * Now construct the initial file system,
705 * then write out the super-block.
708 sblock
.fs_time
= utime
;
709 wtfs((int)SBOFF
/ sectorsize
, sbsize
, (char *)&sblock
);
710 for (i
= 0; i
< sblock
.fs_cssize
; i
+= sblock
.fs_bsize
)
711 wtfs(fsbtodb(&sblock
, sblock
.fs_csaddr
+ numfrags(&sblock
, i
)),
712 sblock
.fs_cssize
- i
< sblock
.fs_bsize
?
713 sblock
.fs_cssize
- i
: sblock
.fs_bsize
,
716 * Write out the duplicate super blocks
718 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++)
719 wtfs(fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
720 sbsize
, (char *)&sblock
);
724 * NOTE: we no longer update information in the disklabel
728 * Notify parent process of success.
729 * Dissociate from session and tty.
731 * NOTE: We are the child and may receive a SIGINT due
732 * to losing the tty session? XXX
736 kill(mfs_ppid
, SIGUSR1
);
742 /* returns to mount_mfs (newfs) and issues the mount */
747 * Initialize a cylinder group.
750 initcg(int cylno
, time_t utime
)
752 daddr_t cbase
, d
, dlower
, dupper
, dmax
, blkno
;
761 * Determine block bounds for cylinder group.
762 * Allow space for super block summary information in first
765 cbase
= cgbase(&sblock
, cylno
);
766 dmax
= cbase
+ sblock
.fs_fpg
;
767 if (dmax
> sblock
.fs_size
)
768 dmax
= sblock
.fs_size
;
769 dlower
= cgsblock(&sblock
, cylno
) - cbase
;
770 dupper
= cgdmin(&sblock
, cylno
) - cbase
;
772 dupper
+= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
774 memset(&acg
, 0, sblock
.fs_cgsize
);
776 acg
.cg_magic
= CG_MAGIC
;
778 if (cylno
== sblock
.fs_ncg
- 1)
779 acg
.cg_ncyl
= sblock
.fs_ncyl
% sblock
.fs_cpg
;
781 acg
.cg_ncyl
= sblock
.fs_cpg
;
782 acg
.cg_niblk
= sblock
.fs_ipg
;
783 acg
.cg_ndblk
= dmax
- cbase
;
784 if (sblock
.fs_contigsumsize
> 0)
785 acg
.cg_nclusterblks
= acg
.cg_ndblk
/ sblock
.fs_frag
;
786 acg
.cg_btotoff
= &acg
.cg_space
[0] - (u_char
*)(&acg
.cg_firstfield
);
787 acg
.cg_boff
= acg
.cg_btotoff
+ sblock
.fs_cpg
* sizeof(int32_t);
788 acg
.cg_iusedoff
= acg
.cg_boff
+
789 sblock
.fs_cpg
* sblock
.fs_nrpos
* sizeof(u_int16_t
);
790 acg
.cg_freeoff
= acg
.cg_iusedoff
+ howmany(sblock
.fs_ipg
, NBBY
);
791 if (sblock
.fs_contigsumsize
<= 0) {
792 acg
.cg_nextfreeoff
= acg
.cg_freeoff
+
793 howmany(sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
);
795 acg
.cg_clustersumoff
= acg
.cg_freeoff
+ howmany
796 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
) -
798 acg
.cg_clustersumoff
=
799 roundup(acg
.cg_clustersumoff
, sizeof(u_int32_t
));
800 acg
.cg_clusteroff
= acg
.cg_clustersumoff
+
801 (sblock
.fs_contigsumsize
+ 1) * sizeof(u_int32_t
);
802 acg
.cg_nextfreeoff
= acg
.cg_clusteroff
+ howmany
803 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPB(&sblock
), NBBY
);
805 if (acg
.cg_nextfreeoff
- (long)(&acg
.cg_firstfield
) > sblock
.fs_cgsize
) {
806 printf("Panic: cylinder group too big\n");
809 acg
.cg_cs
.cs_nifree
+= sblock
.fs_ipg
;
811 for (k
= 0; k
< ROOTINO
; k
++) {
812 setbit(cg_inosused(&acg
), k
);
813 acg
.cg_cs
.cs_nifree
--;
816 for (i
= 0; i
< sblock
.fs_ipg
/ INOPF(&sblock
); i
+= sblock
.fs_frag
) {
819 j
< sblock
.fs_bsize
/ sizeof(struct ufs1_dinode
);
821 zino
[j
].di_gen
= random();
824 wtfs(fsbtodb(&sblock
, cgimin(&sblock
, cylno
) + i
),
825 sblock
.fs_bsize
, (char *)zino
);
829 * In cylno 0, beginning space is reserved
830 * for boot and super blocks.
832 for (d
= 0; d
< dlower
; d
+= sblock
.fs_frag
) {
833 blkno
= d
/ sblock
.fs_frag
;
834 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
835 if (sblock
.fs_contigsumsize
> 0)
836 setbit(cg_clustersfree(&acg
), blkno
);
837 acg
.cg_cs
.cs_nbfree
++;
838 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
839 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
840 [cbtorpos(&sblock
, d
)]++;
842 sblock
.fs_dsize
+= dlower
;
844 sblock
.fs_dsize
+= acg
.cg_ndblk
- dupper
;
845 if ((i
= dupper
% sblock
.fs_frag
)) {
846 acg
.cg_frsum
[sblock
.fs_frag
- i
]++;
847 for (d
= dupper
+ sblock
.fs_frag
- i
; dupper
< d
; dupper
++) {
848 setbit(cg_blksfree(&acg
), dupper
);
849 acg
.cg_cs
.cs_nffree
++;
852 for (d
= dupper
; d
+ sblock
.fs_frag
<= dmax
- cbase
; ) {
853 blkno
= d
/ sblock
.fs_frag
;
854 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
855 if (sblock
.fs_contigsumsize
> 0)
856 setbit(cg_clustersfree(&acg
), blkno
);
857 acg
.cg_cs
.cs_nbfree
++;
858 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
859 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
860 [cbtorpos(&sblock
, d
)]++;
863 if (d
< dmax
- cbase
) {
864 acg
.cg_frsum
[dmax
- cbase
- d
]++;
865 for (; d
< dmax
- cbase
; d
++) {
866 setbit(cg_blksfree(&acg
), d
);
867 acg
.cg_cs
.cs_nffree
++;
870 if (sblock
.fs_contigsumsize
> 0) {
871 int32_t *sump
= cg_clustersum(&acg
);
872 u_char
*mapp
= cg_clustersfree(&acg
);
877 for (i
= 0; i
< acg
.cg_nclusterblks
; i
++) {
878 if ((map
& bit
) != 0) {
880 } else if (run
!= 0) {
881 if (run
> sblock
.fs_contigsumsize
)
882 run
= sblock
.fs_contigsumsize
;
886 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
894 if (run
> sblock
.fs_contigsumsize
)
895 run
= sblock
.fs_contigsumsize
;
899 sblock
.fs_cstotal
.cs_ndir
+= acg
.cg_cs
.cs_ndir
;
900 sblock
.fs_cstotal
.cs_nffree
+= acg
.cg_cs
.cs_nffree
;
901 sblock
.fs_cstotal
.cs_nbfree
+= acg
.cg_cs
.cs_nbfree
;
902 sblock
.fs_cstotal
.cs_nifree
+= acg
.cg_cs
.cs_nifree
;
904 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, cylno
)),
905 sblock
.fs_bsize
, (char *)&acg
);
909 * initialize the file system
911 struct ufs1_dinode node
;
919 struct direct root_dir
[] = {
920 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
921 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
923 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 10, "lost+found" },
930 u_char d_name
[MAXNAMLEN
+ 1];
932 { ROOTINO
, sizeof(struct direct
), 1, "." },
933 { ROOTINO
, sizeof(struct direct
), 2, ".." },
935 { LOSTFOUNDINO
, sizeof(struct direct
), 10, "lost+found" },
939 struct direct lost_found_dir
[] = {
940 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
941 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
942 { 0, DIRBLKSIZ
, 0, 0, 0 },
944 struct odirect olost_found_dir
[] = {
945 { LOSTFOUNDINO
, sizeof(struct direct
), 1, "." },
946 { ROOTINO
, sizeof(struct direct
), 2, ".." },
947 { 0, DIRBLKSIZ
, 0, 0 },
960 * initialize the node
962 node
.di_atime
= utime
;
963 node
.di_mtime
= utime
;
964 node
.di_ctime
= utime
;
967 * create the lost+found directory
970 makedir((struct direct
*)olost_found_dir
, 2);
971 for (i
= DIRBLKSIZ
; i
< sblock
.fs_bsize
; i
+= DIRBLKSIZ
)
972 memmove(&buf
[i
], &olost_found_dir
[2],
973 DIRSIZ(0, &olost_found_dir
[2]));
975 makedir(lost_found_dir
, 2);
976 for (i
= DIRBLKSIZ
; i
< sblock
.fs_bsize
; i
+= DIRBLKSIZ
)
977 memmove(&buf
[i
], &lost_found_dir
[2],
978 DIRSIZ(0, &lost_found_dir
[2]));
980 node
.di_mode
= IFDIR
| UMASK
;
982 node
.di_size
= sblock
.fs_bsize
;
983 node
.di_db
[0] = alloc(node
.di_size
, node
.di_mode
);
984 node
.di_blocks
= btodb(fragroundup(&sblock
, node
.di_size
));
985 wtfs(fsbtodb(&sblock
, node
.di_db
[0]), node
.di_size
, buf
);
986 iput(&node
, LOSTFOUNDINO
);
989 * create the root directory
992 node
.di_mode
= IFDIR
| 01777;
994 node
.di_mode
= IFDIR
| UMASK
;
995 node
.di_nlink
= PREDEFDIR
;
997 node
.di_size
= makedir((struct direct
*)oroot_dir
, PREDEFDIR
);
999 node
.di_size
= makedir(root_dir
, PREDEFDIR
);
1000 node
.di_db
[0] = alloc(sblock
.fs_fsize
, node
.di_mode
);
1001 node
.di_blocks
= btodb(fragroundup(&sblock
, node
.di_size
));
1002 wtfs(fsbtodb(&sblock
, node
.di_db
[0]), sblock
.fs_fsize
, buf
);
1003 iput(&node
, ROOTINO
);
1007 * construct a set of directory entries in "buf".
1008 * return size of directory.
1011 makedir(struct direct
*protodir
, int entries
)
1016 spcleft
= DIRBLKSIZ
;
1017 for (cp
= buf
, i
= 0; i
< entries
- 1; i
++) {
1018 protodir
[i
].d_reclen
= DIRSIZ(0, &protodir
[i
]);
1019 memmove(cp
, &protodir
[i
], protodir
[i
].d_reclen
);
1020 cp
+= protodir
[i
].d_reclen
;
1021 spcleft
-= protodir
[i
].d_reclen
;
1023 protodir
[i
].d_reclen
= spcleft
;
1024 memmove(cp
, &protodir
[i
], DIRSIZ(0, &protodir
[i
]));
1029 * allocate a block or frag
1032 alloc(int size
, int mode
)
1037 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1039 if (acg
.cg_magic
!= CG_MAGIC
) {
1040 printf("cg 0: bad magic number\n");
1043 if (acg
.cg_cs
.cs_nbfree
== 0) {
1044 printf("first cylinder group ran out of space\n");
1047 for (d
= 0; d
< acg
.cg_ndblk
; d
+= sblock
.fs_frag
)
1048 if (isblock(&sblock
, cg_blksfree(&acg
), d
/ sblock
.fs_frag
))
1050 printf("internal error: can't find block in cyl 0\n");
1053 blkno
= fragstoblks(&sblock
, d
);
1054 clrblock(&sblock
, cg_blksfree(&acg
), blkno
);
1055 if (sblock
.fs_contigsumsize
> 0)
1056 clrbit(cg_clustersfree(&acg
), blkno
);
1057 acg
.cg_cs
.cs_nbfree
--;
1058 sblock
.fs_cstotal
.cs_nbfree
--;
1059 fscs
[0].cs_nbfree
--;
1061 acg
.cg_cs
.cs_ndir
++;
1062 sblock
.fs_cstotal
.cs_ndir
++;
1065 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]--;
1066 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))[cbtorpos(&sblock
, d
)]--;
1067 if (size
!= sblock
.fs_bsize
) {
1068 frag
= howmany(size
, sblock
.fs_fsize
);
1069 fscs
[0].cs_nffree
+= sblock
.fs_frag
- frag
;
1070 sblock
.fs_cstotal
.cs_nffree
+= sblock
.fs_frag
- frag
;
1071 acg
.cg_cs
.cs_nffree
+= sblock
.fs_frag
- frag
;
1072 acg
.cg_frsum
[sblock
.fs_frag
- frag
]++;
1073 for (i
= frag
; i
< sblock
.fs_frag
; i
++)
1074 setbit(cg_blksfree(&acg
), d
+ i
);
1076 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1082 * Calculate number of inodes per group.
1085 calcipg(long cylspg
, long bpcg
, off_t
*usedbp
)
1088 long ipg
, new_ipg
, ncg
, ncyl
;
1092 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1093 * Note that fssize is still in sectors, not filesystem blocks.
1095 ncyl
= howmany(fssize
, (u_int
)secpercyl
);
1096 ncg
= howmany(ncyl
, cylspg
);
1098 * Iterate a few times to allow for ipg depending on itself.
1101 for (i
= 0; i
< 10; i
++) {
1102 usedb
= (sblock
.fs_iblkno
+ ipg
/ INOPF(&sblock
))
1103 * NSPF(&sblock
) * (off_t
)sectorsize
;
1104 new_ipg
= (cylspg
* (quad_t
)bpcg
- usedb
) / density
* fssize
1105 / ncg
/ secpercyl
/ cylspg
;
1106 new_ipg
= roundup(new_ipg
, INOPB(&sblock
));
1116 * Allocate an inode on the disk
1119 iput(struct ufs1_dinode
*ip
, ino_t ino
)
1121 struct ufs1_dinode inobuf
[MAXINOPB
];
1126 ip
->di_gen
= random();
1128 c
= ino_to_cg(&sblock
, ino
);
1129 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1131 if (acg
.cg_magic
!= CG_MAGIC
) {
1132 printf("cg 0: bad magic number\n");
1135 acg
.cg_cs
.cs_nifree
--;
1136 setbit(cg_inosused(&acg
), ino
);
1137 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1139 sblock
.fs_cstotal
.cs_nifree
--;
1140 fscs
[0].cs_nifree
--;
1141 if (ino
>= (uint32_t)sblock
.fs_ipg
* (uint32_t)sblock
.fs_ncg
) {
1142 printf("fsinit: inode value out of range (%ju).\n",
1146 d
= fsbtodb(&sblock
, ino_to_fsba(&sblock
, ino
));
1147 rdfs(d
, sblock
.fs_bsize
, (char *)inobuf
);
1148 inobuf
[ino_to_fsbo(&sblock
, ino
)] = *ip
;
1149 wtfs(d
, sblock
.fs_bsize
, (char *)inobuf
);
1153 * Parent notifies child that it can proceed with the newfs and mount
1154 * operation (occurs after parent has copied the underlying filesystem
1155 * if the -C option was specified (for MFS), or immediately after the
1156 * parent forked the child otherwise).
1159 parentready(__unused
int signo
)
1161 parentready_signalled
= 1;
1165 * Notify parent process that the filesystem has created itself successfully.
1167 * We have to wait until the mount has actually completed!
1170 started(__unused
int signo
)
1172 int retry
= 100; /* 10 seconds, 100ms */
1174 while (mfs_ppid
&& retry
) {
1178 stat(mfs_mtpt
, &st
) < 0 ||
1179 st
.st_dev
!= mfs_mtstat
.st_dev
1187 fatal("mfs mount failed waiting for mount to go active");
1188 } else if (copyroot
) {
1189 FSPaste(mfs_mtpt
, copyroot
, copyhlinks
);
1195 extern char *_etext
;
1196 #define etext _etext
1202 * read a block from the file system
1205 rdfs(daddr_t bno
, int size
, char *bf
)
1211 memmove(bf
, membase
+ bno
* sectorsize
, size
);
1214 if (lseek(fsi
, (off_t
)bno
* sectorsize
, 0) < 0) {
1215 printf("seek error: %ld\n", (long)bno
);
1218 n
= read(fsi
, bf
, size
);
1220 printf("read error: %ld\n", (long)bno
);
1225 #define WCSIZE (128 * 1024)
1226 daddr_t wc_sect
; /* units of sectorsize */
1227 int wc_end
; /* bytes */
1228 static char wc
[WCSIZE
]; /* bytes */
1231 * Flush dirty write behind buffer.
1238 if (lseek(fso
, (off_t
)wc_sect
* sectorsize
, SEEK_SET
) < 0) {
1239 printf("seek error: %ld\n", (long)wc_sect
);
1240 err(35, "wtfs - writecombine");
1242 n
= write(fso
, wc
, wc_end
);
1244 printf("write error: %ld\n", (long)wc_sect
);
1245 err(36, "wtfs - writecombine");
1252 * write a block to the file system
1255 wtfs(daddr_t bno
, int size
, char *bf
)
1261 memmove(membase
+ bno
* sectorsize
, bf
, size
);
1267 if (wc_end
== 0 && size
<= WCSIZE
) {
1269 bcopy(bf
, wc
, size
);
1271 if (wc_end
< WCSIZE
)
1275 if ((off_t
)wc_sect
* sectorsize
+ wc_end
== (off_t
)bno
* sectorsize
&&
1276 wc_end
+ size
<= WCSIZE
) {
1277 bcopy(bf
, wc
+ wc_end
, size
);
1279 if (wc_end
< WCSIZE
)
1286 if (lseek(fso
, (off_t
)bno
* sectorsize
, SEEK_SET
) < 0) {
1287 printf("seek error: %ld\n", (long)bno
);
1290 n
= write(fso
, bf
, size
);
1292 printf("write error: fso %d blk %ld %d/%d\n",
1293 fso
, (long)bno
, n
, size
);
1299 * check if a block is available
1302 isblock(struct fs
*fs
, unsigned char *cp
, int h
)
1306 switch (fs
->fs_frag
) {
1308 return (cp
[h
] == 0xff);
1310 mask
= 0x0f << ((h
& 0x1) << 2);
1311 return ((cp
[h
>> 1] & mask
) == mask
);
1313 mask
= 0x03 << ((h
& 0x3) << 1);
1314 return ((cp
[h
>> 2] & mask
) == mask
);
1316 mask
= 0x01 << (h
& 0x7);
1317 return ((cp
[h
>> 3] & mask
) == mask
);
1319 fprintf(stderr
, "isblock bad fs_frag %d\n", fs
->fs_frag
);
1325 * take a block out of the map
1328 clrblock(struct fs
*fs
, unsigned char *cp
, int h
)
1330 switch ((fs
)->fs_frag
) {
1335 cp
[h
>> 1] &= ~(0x0f << ((h
& 0x1) << 2));
1338 cp
[h
>> 2] &= ~(0x03 << ((h
& 0x3) << 1));
1341 cp
[h
>> 3] &= ~(0x01 << (h
& 0x7));
1344 fprintf(stderr
, "clrblock bad fs_frag %d\n", fs
->fs_frag
);
1350 * put a block into the map
1353 setblock(struct fs
*fs
, unsigned char *cp
, int h
)
1355 switch (fs
->fs_frag
) {
1360 cp
[h
>> 1] |= (0x0f << ((h
& 0x1) << 2));
1363 cp
[h
>> 2] |= (0x03 << ((h
& 0x3) << 1));
1366 cp
[h
>> 3] |= (0x01 << (h
& 0x7));
1369 fprintf(stderr
, "setblock bad fs_frag %d\n", fs
->fs_frag
);
1375 * Determine the number of characters in a
1387 if (ioctl(0, TIOCGWINSZ
, &ws
) != -1)
1388 columns
= ws
.ws_col
;
1389 if (columns
== 0 && (cp
= getenv("COLUMNS")))
1392 columns
= 80; /* last resort */