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 $
44 extern int atoi(char *);
45 extern char * getenv(char *);
48 extern long random(void);
49 extern void srandomdev(void);
52 #endif /* STANDALONE */
55 * make file system for cylinder-group style file systems
59 * We limit the size of the inode map to be no more than a
60 * third of the cylinder group space, since we must leave at
61 * least an equal amount of space for the block map.
63 * N.B.: MAXIPG must be a multiple of INOPB(fs).
65 #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
68 #define MAXINOPB (MAXBSIZE / sizeof(struct ufs1_dinode))
69 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
72 * variables set up by front end.
74 extern int mfs
; /* run as the memory based filesystem */
75 extern char *mfs_mtpt
; /* mount point for mfs */
76 extern struct stat mfs_mtstat
; /* stat prior to mount */
77 extern int Nflag
; /* run mkfs without writing file system */
78 extern int Oflag
; /* format as an 4.3BSD file system */
79 extern int Uflag
; /* enable soft updates for file system */
80 extern u_long fssize
; /* file system size */
81 extern int ntracks
; /* # tracks/cylinder */
82 extern int nsectors
; /* # sectors/track */
83 extern int nphyssectors
; /* # sectors/track including spares */
84 extern int secpercyl
; /* sectors per cylinder */
85 extern int sectorsize
; /* bytes/sector */
86 extern int realsectorsize
; /* bytes/sector in hardware*/
87 extern int rpm
; /* revolutions/minute of drive */
88 extern int interleave
; /* hardware sector interleave */
89 extern int trackskew
; /* sector 0 skew, per track */
90 extern int fsize
; /* fragment size */
91 extern int bsize
; /* block size */
92 extern int cpg
; /* cylinders/cylinder group */
93 extern int cpgflg
; /* cylinders/cylinder group flag was given */
94 extern int minfree
; /* free space threshold */
95 extern int opt
; /* optimization preference (space or time) */
96 extern int density
; /* number of bytes per inode */
97 extern int maxcontig
; /* max contiguous blocks to allocate */
98 extern int rotdelay
; /* rotational delay between blocks */
99 extern int maxbpg
; /* maximum blocks per file in a cyl group */
100 extern int nrpos
; /* # of distinguished rotational positions */
101 extern int bbsize
; /* boot block size */
102 extern int sbsize
; /* superblock size */
103 extern int avgfilesize
; /* expected average file size */
104 extern int avgfilesperdir
; /* expected number of files per directory */
105 extern u_long memleft
; /* virtual memory available */
106 extern caddr_t membase
; /* start address of memory based filesystem */
107 extern char * filename
;
108 extern struct disktab geom
;
110 extern void fatal(const char *fmt
, ...);
116 #define sblock fsun.fs
125 struct ufs1_dinode zino
[MAXBSIZE
/ sizeof(struct ufs1_dinode
)];
128 static fsnode_t copyroot
;
129 static fsnode_t copyhlinks
;
133 daddr_t
alloc(int, int);
134 long calcipg(long, long, off_t
*);
135 static int charsperline(void);
136 void clrblock(struct fs
*, unsigned char *, int);
138 void initcg(int, time_t);
139 int isblock(struct fs
*, unsigned char *, int);
140 void iput(struct ufs1_dinode
*, ino_t
);
141 int makedir(struct direct
*, int);
142 void parentready(int);
143 void rdfs(daddr_t
, int, char *);
144 void setblock(struct fs
*, unsigned char *, int);
146 void wtfs(daddr_t
, int, char *);
147 void wtfsflush(void);
150 void get_memleft(void);
151 void raise_data_limit(void);
154 char * calloc(u_long
, u_long
);
155 caddr_t
malloc(u_long
);
156 caddr_t
realloc(char *, u_long
);
160 int parentready_signalled
;
163 mkfs(char *fsys
, int fi
, int fo
, const char *mfscopy
)
165 long i
, mincpc
, mincpg
, inospercg
;
166 long cylno
, rpos
, blk
, j
, emitwarn
= 0;
167 long used
, mincpgcnt
, bpcg
;
169 long mapcramped
, inodecramped
;
170 long postblsize
, rotblsize
, totalsbsize
;
175 char tmpbuf
[100]; /* XXX this will break in about 2,500 years */
191 signal(SIGUSR1
, parentready
);
192 if ((child
= fork()) != 0) {
199 copyroot
= FSCopy(©hlinks
, mfscopy
);
200 signal(SIGUSR1
, started
);
201 kill(child
, SIGUSR1
);
202 while (waitpid(child
, &status
, 0) != child
)
204 exit(WEXITSTATUS(status
));
211 omask
= sigblock(sigmask(SIGUSR1
));
212 while (parentready_signalled
== 0)
220 if (filename
!= NULL
) {
221 unsigned char buf
[BUFSIZ
];
225 fd
= open(filename
, O_RDWR
|O_TRUNC
|O_CREAT
, 0644);
227 err(12, "%s", filename
);
228 l1
= fssize
* sectorsize
;
231 for (l
= 0; l
< fssize
* (u_long
)sectorsize
; l
+= l1
) {
232 w
= write(fd
, buf
, l1
);
233 if (w
< 0 || (u_long
)w
!= l1
)
234 err(12, "%s", filename
);
239 PROT_READ
|PROT_WRITE
,
243 if(membase
== MAP_FAILED
)
250 if (fssize
* (u_long
)sectorsize
> (memleft
- 131072))
251 fssize
= (memleft
- 131072) / sectorsize
;
252 if ((membase
= malloc(fssize
* sectorsize
)) == NULL
)
253 errx(13, "malloc failed");
259 sblock
.fs_inodefmt
= FS_42INODEFMT
;
260 sblock
.fs_maxsymlinklen
= 0;
262 sblock
.fs_inodefmt
= FS_44INODEFMT
;
263 sblock
.fs_maxsymlinklen
= MAXSYMLINKLEN
;
266 sblock
.fs_flags
|= FS_DOSOFTDEP
;
268 * Validate the given file system size.
269 * Verify that its last block can actually be accessed.
272 printf("preposterous size %lu\n", fssize
), exit(13);
273 wtfs(fssize
- (realsectorsize
/ DEV_BSIZE
), realsectorsize
,
276 * collect and verify the sector and track info
278 sblock
.fs_nsect
= nsectors
;
279 sblock
.fs_ntrak
= ntracks
;
280 if (sblock
.fs_ntrak
<= 0)
281 printf("preposterous ntrak %d\n", sblock
.fs_ntrak
), exit(14);
282 if (sblock
.fs_nsect
<= 0)
283 printf("preposterous nsect %d\n", sblock
.fs_nsect
), exit(15);
285 * collect and verify the filesystem density info
287 sblock
.fs_avgfilesize
= avgfilesize
;
288 sblock
.fs_avgfpdir
= avgfilesperdir
;
289 if (sblock
.fs_avgfilesize
<= 0)
290 printf("illegal expected average file size %d\n",
291 sblock
.fs_avgfilesize
), exit(14);
292 if (sblock
.fs_avgfpdir
<= 0)
293 printf("illegal expected number of files per directory %d\n",
294 sblock
.fs_avgfpdir
), exit(15);
296 * collect and verify the block and fragment sizes
298 sblock
.fs_bsize
= bsize
;
299 sblock
.fs_fsize
= fsize
;
300 if (!POWEROF2(sblock
.fs_bsize
)) {
301 printf("block size must be a power of 2, not %d\n",
305 if (!POWEROF2(sblock
.fs_fsize
)) {
306 printf("fragment size must be a power of 2, not %d\n",
310 if (sblock
.fs_fsize
< sectorsize
) {
311 printf("fragment size %d is too small, minimum is %d\n",
312 sblock
.fs_fsize
, sectorsize
);
315 if (sblock
.fs_bsize
< MINBSIZE
) {
316 printf("block size %d is too small, minimum is %d\n",
317 sblock
.fs_bsize
, MINBSIZE
);
320 if (sblock
.fs_bsize
< sblock
.fs_fsize
) {
321 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
322 sblock
.fs_bsize
, sblock
.fs_fsize
);
325 sblock
.fs_bmask
= ~(sblock
.fs_bsize
- 1);
326 sblock
.fs_fmask
= ~(sblock
.fs_fsize
- 1);
327 sblock
.fs_qbmask
= ~sblock
.fs_bmask
;
328 sblock
.fs_qfmask
= ~sblock
.fs_fmask
;
329 for (sblock
.fs_bshift
= 0, i
= sblock
.fs_bsize
; i
> 1; i
>>= 1)
331 for (sblock
.fs_fshift
= 0, i
= sblock
.fs_fsize
; i
> 1; i
>>= 1)
333 sblock
.fs_frag
= numfrags(&sblock
, sblock
.fs_bsize
);
334 for (sblock
.fs_fragshift
= 0, i
= sblock
.fs_frag
; i
> 1; i
>>= 1)
335 sblock
.fs_fragshift
++;
336 if (sblock
.fs_frag
> MAXFRAG
) {
337 printf("fragment size %d is too small, minimum with block size %d is %d\n",
338 sblock
.fs_fsize
, sblock
.fs_bsize
,
339 sblock
.fs_bsize
/ MAXFRAG
);
342 sblock
.fs_nrpos
= nrpos
;
343 sblock
.fs_nindir
= sblock
.fs_bsize
/ sizeof(daddr_t
);
344 sblock
.fs_inopb
= sblock
.fs_bsize
/ sizeof(struct ufs1_dinode
);
345 sblock
.fs_nspf
= sblock
.fs_fsize
/ sectorsize
;
346 for (sblock
.fs_fsbtodb
= 0, i
= NSPF(&sblock
); i
> 1; i
>>= 1)
349 roundup(howmany(bbsize
+ sbsize
, sblock
.fs_fsize
), sblock
.fs_frag
);
350 sblock
.fs_cblkno
= (daddr_t
)(sblock
.fs_sblkno
+
351 roundup(howmany(sbsize
, sblock
.fs_fsize
), sblock
.fs_frag
));
352 sblock
.fs_iblkno
= sblock
.fs_cblkno
+ sblock
.fs_frag
;
353 sblock
.fs_cgoffset
= roundup(
354 howmany(sblock
.fs_nsect
, NSPF(&sblock
)), sblock
.fs_frag
);
355 for (sblock
.fs_cgmask
= 0xffffffff, i
= sblock
.fs_ntrak
; i
> 1; i
>>= 1)
356 sblock
.fs_cgmask
<<= 1;
357 if (!POWEROF2(sblock
.fs_ntrak
))
358 sblock
.fs_cgmask
<<= 1;
359 sblock
.fs_maxfilesize
= sblock
.fs_bsize
* NDADDR
- 1;
360 for (sizepb
= sblock
.fs_bsize
, i
= 0; i
< NIADDR
; i
++) {
361 sizepb
*= NINDIR(&sblock
);
362 sblock
.fs_maxfilesize
+= sizepb
;
365 * Validate specified/determined secpercyl
366 * and calculate minimum cylinders per group.
368 sblock
.fs_spc
= secpercyl
;
369 for (sblock
.fs_cpc
= NSPB(&sblock
), i
= sblock
.fs_spc
;
370 sblock
.fs_cpc
> 1 && (i
& 1) == 0;
371 sblock
.fs_cpc
>>= 1, i
>>= 1)
373 mincpc
= sblock
.fs_cpc
;
374 bpcg
= sblock
.fs_spc
* sectorsize
;
375 inospercg
= roundup(bpcg
/ sizeof(struct ufs1_dinode
), INOPB(&sblock
));
376 if (inospercg
> MAXIPG(&sblock
))
377 inospercg
= MAXIPG(&sblock
);
378 used
= (sblock
.fs_iblkno
+ inospercg
/ INOPF(&sblock
)) * NSPF(&sblock
);
379 mincpgcnt
= howmany(sblock
.fs_cgoffset
* (~sblock
.fs_cgmask
) + used
,
381 mincpg
= roundup(mincpgcnt
, mincpc
);
383 * Ensure that cylinder group with mincpg has enough space
386 sblock
.fs_cpg
= mincpg
;
387 sblock
.fs_ipg
= inospercg
;
389 sblock
.fs_contigsumsize
= MIN(maxcontig
, FS_MAXCONTIG
);
391 while (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
393 if (sblock
.fs_bsize
< MAXBSIZE
) {
394 sblock
.fs_bsize
<<= 1;
400 mincpg
= roundup(mincpgcnt
, mincpc
);
401 sblock
.fs_cpg
= mincpg
;
403 sblock
.fs_frag
<<= 1;
404 sblock
.fs_fragshift
+= 1;
405 if (sblock
.fs_frag
<= MAXFRAG
)
408 if (sblock
.fs_fsize
== sblock
.fs_bsize
) {
409 printf("There is no block size that");
410 printf(" can support this disk\n");
413 sblock
.fs_frag
>>= 1;
414 sblock
.fs_fragshift
-= 1;
415 sblock
.fs_fsize
<<= 1;
416 sblock
.fs_nspf
<<= 1;
419 * Ensure that cylinder group with mincpg has enough space for inodes.
422 inospercg
= calcipg(mincpg
, bpcg
, &usedb
);
423 sblock
.fs_ipg
= inospercg
;
424 while (inospercg
> MAXIPG(&sblock
)) {
426 if (mincpc
== 1 || sblock
.fs_frag
== 1 ||
427 sblock
.fs_bsize
== MINBSIZE
)
429 printf("With a block size of %d %s %d\n", sblock
.fs_bsize
,
430 "minimum bytes per inode is",
431 (int)((mincpg
* (off_t
)bpcg
- usedb
)
432 / MAXIPG(&sblock
) + 1));
433 sblock
.fs_bsize
>>= 1;
434 sblock
.fs_frag
>>= 1;
435 sblock
.fs_fragshift
-= 1;
437 sblock
.fs_cpg
= roundup(mincpgcnt
, mincpc
);
438 if (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
439 sblock
.fs_bsize
<<= 1;
442 mincpg
= sblock
.fs_cpg
;
443 inospercg
= calcipg(mincpg
, bpcg
, &usedb
);
444 sblock
.fs_ipg
= inospercg
;
447 if (inospercg
> MAXIPG(&sblock
)) {
448 printf("Minimum bytes per inode is %d\n",
449 (int)((mincpg
* (off_t
)bpcg
- usedb
)
450 / MAXIPG(&sblock
) + 1));
451 } else if (!mapcramped
) {
452 printf("With %d bytes per inode, ", density
);
453 printf("minimum cylinders per group is %ld\n", mincpg
);
457 printf("With %d sectors per cylinder, ", sblock
.fs_spc
);
458 printf("minimum cylinders per group is %ld\n", mincpg
);
460 if (inodecramped
|| mapcramped
) {
461 if (sblock
.fs_bsize
!= bsize
)
462 printf("%s to be changed from %d to %d\n",
463 "This requires the block size",
464 bsize
, sblock
.fs_bsize
);
465 if (sblock
.fs_fsize
!= fsize
)
466 printf("\t%s to be changed from %d to %d\n",
467 "and the fragment size",
468 fsize
, sblock
.fs_fsize
);
472 * Calculate the number of cylinders per group
475 if (sblock
.fs_cpg
% mincpc
!= 0) {
476 printf("%s groups must have a multiple of %ld cylinders\n",
477 cpgflg
? "Cylinder" : "Warning: cylinder", mincpc
);
478 sblock
.fs_cpg
= roundup(sblock
.fs_cpg
, mincpc
);
483 * Must ensure there is enough space for inodes.
485 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
486 while (sblock
.fs_ipg
> MAXIPG(&sblock
)) {
488 sblock
.fs_cpg
-= mincpc
;
489 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
492 * Must ensure there is enough space to hold block map.
494 while (CGSIZE(&sblock
) > (uint32_t)sblock
.fs_bsize
) {
496 sblock
.fs_cpg
-= mincpc
;
497 sblock
.fs_ipg
= calcipg(sblock
.fs_cpg
, bpcg
, &usedb
);
499 sblock
.fs_fpg
= (sblock
.fs_cpg
* sblock
.fs_spc
) / NSPF(&sblock
);
500 if ((sblock
.fs_cpg
* sblock
.fs_spc
) % NSPB(&sblock
) != 0) {
501 printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
504 if (sblock
.fs_cpg
< mincpg
) {
505 printf("cylinder groups must have at least %ld cylinders\n",
508 } else if (sblock
.fs_cpg
!= cpg
) {
511 else if (!mapcramped
&& !inodecramped
)
514 if (mapcramped
&& inodecramped
)
515 printf("Block size and bytes per inode restrict");
517 printf("Block size restricts");
519 printf("Bytes per inode restrict");
520 printf(" cylinders per group to %d.\n", sblock
.fs_cpg
);
525 sblock
.fs_cgsize
= fragroundup(&sblock
, CGSIZE(&sblock
));
527 * Now have size for file system and nsect and ntrak.
528 * Determine number of cylinders and blocks in the file system.
530 sblock
.fs_size
= fssize
= dbtofsb(&sblock
, fssize
);
531 sblock
.fs_ncyl
= fssize
* NSPF(&sblock
) / sblock
.fs_spc
;
532 if ((long)fssize
* NSPF(&sblock
) > sblock
.fs_ncyl
* sblock
.fs_spc
) {
536 if (sblock
.fs_ncyl
< 1) {
537 printf("file systems must have at least one cylinder\n");
541 * Determine feasability/values of rotational layout tables.
543 * The size of the rotational layout tables is limited by the
544 * size of the superblock, SBSIZE. The amount of space available
545 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
546 * The size of these tables is inversely proportional to the block
547 * size of the file system. The size increases if sectors per track
548 * are not powers of two, because more cylinders must be described
549 * by the tables before the rotational pattern repeats (fs_cpc).
551 sblock
.fs_interleave
= interleave
;
552 sblock
.fs_trackskew
= trackskew
;
553 sblock
.fs_npsect
= nphyssectors
;
554 sblock
.fs_postblformat
= FS_DYNAMICPOSTBLFMT
;
555 sblock
.fs_sbsize
= fragroundup(&sblock
, sizeof(struct fs
));
556 if (sblock
.fs_sbsize
> SBSIZE
)
557 sblock
.fs_sbsize
= SBSIZE
;
558 if (sblock
.fs_ntrak
== 1) {
562 postblsize
= sblock
.fs_nrpos
* sblock
.fs_cpc
* sizeof(int16_t);
563 rotblsize
= sblock
.fs_cpc
* sblock
.fs_spc
/ NSPB(&sblock
);
564 totalsbsize
= sizeof(struct fs
) + rotblsize
;
565 if (sblock
.fs_nrpos
== 8 && sblock
.fs_cpc
<= 16) {
566 /* use old static table space */
567 sblock
.fs_postbloff
= (char *)(&sblock
.fs_opostbl
[0][0]) -
568 (char *)(&sblock
.fs_firstfield
);
569 sblock
.fs_rotbloff
= &sblock
.fs_space
[0] -
570 (u_char
*)(&sblock
.fs_firstfield
);
572 /* use dynamic table space */
573 sblock
.fs_postbloff
= &sblock
.fs_space
[0] -
574 (u_char
*)(&sblock
.fs_firstfield
);
575 sblock
.fs_rotbloff
= sblock
.fs_postbloff
+ postblsize
;
576 totalsbsize
+= postblsize
;
578 if (totalsbsize
> SBSIZE
||
579 sblock
.fs_nsect
> (1 << NBBY
) * NSPB(&sblock
)) {
580 printf("%s %s %d %s %d.%s",
581 "Warning: insufficient space in super block for\n",
582 "rotational layout tables with nsect", sblock
.fs_nsect
,
583 "and ntrak", sblock
.fs_ntrak
,
584 "\nFile system performance may be impaired.\n");
588 sblock
.fs_sbsize
= fragroundup(&sblock
, totalsbsize
);
589 if (sblock
.fs_sbsize
> SBSIZE
)
590 sblock
.fs_sbsize
= SBSIZE
;
592 * calculate the available blocks for each rotational position
594 for (cylno
= 0; cylno
< sblock
.fs_cpc
; cylno
++)
595 for (rpos
= 0; rpos
< sblock
.fs_nrpos
; rpos
++)
596 fs_postbl(&sblock
, cylno
)[rpos
] = -1;
597 for (i
= (rotblsize
- 1) * sblock
.fs_frag
;
598 i
>= 0; i
-= sblock
.fs_frag
) {
599 cylno
= cbtocylno(&sblock
, i
);
600 rpos
= cbtorpos(&sblock
, i
);
601 blk
= fragstoblks(&sblock
, i
);
602 if (fs_postbl(&sblock
, cylno
)[rpos
] == -1)
603 fs_rotbl(&sblock
)[blk
] = 0;
605 fs_rotbl(&sblock
)[blk
] =
606 fs_postbl(&sblock
, cylno
)[rpos
] - blk
;
607 fs_postbl(&sblock
, cylno
)[rpos
] = blk
;
611 * Compute/validate number of cylinder groups.
613 sblock
.fs_ncg
= sblock
.fs_ncyl
/ sblock
.fs_cpg
;
614 if (sblock
.fs_ncyl
% sblock
.fs_cpg
)
616 sblock
.fs_dblkno
= sblock
.fs_iblkno
+ sblock
.fs_ipg
/ INOPF(&sblock
);
617 i
= MIN(~sblock
.fs_cgmask
, sblock
.fs_ncg
- 1);
618 if (cgdmin(&sblock
, i
) - cgbase(&sblock
, i
) >= sblock
.fs_fpg
) {
619 printf("inode blocks/cyl group (%ld) >= data blocks (%ld)\n",
620 cgdmin(&sblock
, i
) - cgbase(&sblock
, i
) / sblock
.fs_frag
,
621 (long)(sblock
.fs_fpg
/ sblock
.fs_frag
));
622 printf("number of cylinders per cylinder group (%d) %s.\n",
623 sblock
.fs_cpg
, "must be increased");
626 j
= sblock
.fs_ncg
- 1;
627 if ((i
= fssize
- j
* sblock
.fs_fpg
) < sblock
.fs_fpg
&&
628 cgdmin(&sblock
, j
) - cgbase(&sblock
, j
) > i
) {
630 printf("Filesystem must have at least %d sectors\n",
632 (cgdmin(&sblock
, 0) + 3 * sblock
.fs_frag
));
636 "Warning: inode blocks/cyl group (%ld) >= data blocks (%ld) in last\n",
637 (cgdmin(&sblock
, j
) - cgbase(&sblock
, j
)) / sblock
.fs_frag
,
640 " cylinder group. This implies %ld sector(s) cannot be allocated.\n",
643 sblock
.fs_ncyl
-= sblock
.fs_ncyl
% sblock
.fs_cpg
;
644 sblock
.fs_size
= fssize
= sblock
.fs_ncyl
* sblock
.fs_spc
/
648 if (emitwarn
&& !mfs
) {
649 printf("Warning: %lu sector(s) in last cylinder unallocated\n",
651 (fssize
* NSPF(&sblock
) - (sblock
.fs_ncyl
- 1)
655 * fill in remaining fields of the super block
657 sblock
.fs_csaddr
= cgdmin(&sblock
, 0);
659 fragroundup(&sblock
, sblock
.fs_ncg
* sizeof(struct csum
));
661 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
662 * longer used. However, we still initialise them so that the
663 * filesystem remains compatible with old kernels.
665 i
= sblock
.fs_bsize
/ sizeof(struct csum
);
666 sblock
.fs_csmask
= ~(i
- 1);
667 for (sblock
.fs_csshift
= 0; i
> 1; i
>>= 1)
669 fscs
= (struct csum
*)calloc(1, sblock
.fs_cssize
);
671 errx(31, "calloc failed");
672 sblock
.fs_magic
= FS_MAGIC
;
673 sblock
.fs_rotdelay
= rotdelay
;
674 sblock
.fs_minfree
= minfree
;
675 sblock
.fs_maxcontig
= maxcontig
;
676 sblock
.fs_maxbpg
= maxbpg
;
677 sblock
.fs_rps
= rpm
/ 60;
678 sblock
.fs_optim
= opt
;
679 sblock
.fs_cgrotor
= 0;
680 sblock
.fs_cstotal
.cs_ndir
= 0;
681 sblock
.fs_cstotal
.cs_nbfree
= 0;
682 sblock
.fs_cstotal
.cs_nifree
= 0;
683 sblock
.fs_cstotal
.cs_nffree
= 0;
688 sblock
.fs_id
[0] = (long)utime
;
689 sblock
.fs_id
[1] = random();
693 * Dump out summary information about file system.
696 printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
697 fsys
, sblock
.fs_size
* NSPF(&sblock
), sblock
.fs_ncyl
,
698 "cylinders", sblock
.fs_ntrak
, sblock
.fs_nsect
);
699 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
700 printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)%s\n",
701 (float)sblock
.fs_size
* sblock
.fs_fsize
* B2MBFACTOR
,
702 sblock
.fs_ncg
, sblock
.fs_cpg
,
703 (float)sblock
.fs_fpg
* sblock
.fs_fsize
* B2MBFACTOR
,
705 sblock
.fs_flags
& FS_DOSOFTDEP
? " SOFTUPDATES" : "");
709 * Now build the cylinders group blocks and
710 * then print out indices of cylinder groups.
713 printf("super-block backups (for fsck -b #) at:\n");
715 width
= charsperline();
716 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++) {
717 initcg(cylno
, utime
);
720 j
= snprintf(tmpbuf
, sizeof(tmpbuf
), " %ld%s",
721 fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
722 cylno
< (sblock
.fs_ncg
-1) ? "," : "" );
723 if (i
+ j
>= width
) {
728 printf("%s", tmpbuf
);
736 * Now construct the initial file system,
737 * then write out the super-block.
740 sblock
.fs_time
= utime
;
741 wtfs((int)SBOFF
/ sectorsize
, sbsize
, (char *)&sblock
);
742 for (i
= 0; i
< sblock
.fs_cssize
; i
+= sblock
.fs_bsize
)
743 wtfs(fsbtodb(&sblock
, sblock
.fs_csaddr
+ numfrags(&sblock
, i
)),
744 sblock
.fs_cssize
- i
< sblock
.fs_bsize
?
745 sblock
.fs_cssize
- i
: sblock
.fs_bsize
,
748 * Write out the duplicate super blocks
750 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++)
751 wtfs(fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)),
752 sbsize
, (char *)&sblock
);
756 * NOTE: we no longer update information in the disklabel
760 * Notify parent process of success.
761 * Dissociate from session and tty.
763 * NOTE: We are the child and may receive a SIGINT due
764 * to losing the tty session? XXX
768 kill(mfs_ppid
, SIGUSR1
);
774 /* returns to mount_mfs (newfs) and issues the mount */
779 * Initialize a cylinder group.
782 initcg(int cylno
, time_t utime
)
784 daddr_t cbase
, d
, dlower
, dupper
, dmax
, blkno
;
793 * Determine block bounds for cylinder group.
794 * Allow space for super block summary information in first
797 cbase
= cgbase(&sblock
, cylno
);
798 dmax
= cbase
+ sblock
.fs_fpg
;
799 if (dmax
> sblock
.fs_size
)
800 dmax
= sblock
.fs_size
;
801 dlower
= cgsblock(&sblock
, cylno
) - cbase
;
802 dupper
= cgdmin(&sblock
, cylno
) - cbase
;
804 dupper
+= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
806 memset(&acg
, 0, sblock
.fs_cgsize
);
808 acg
.cg_magic
= CG_MAGIC
;
810 if (cylno
== sblock
.fs_ncg
- 1)
811 acg
.cg_ncyl
= sblock
.fs_ncyl
% sblock
.fs_cpg
;
813 acg
.cg_ncyl
= sblock
.fs_cpg
;
814 acg
.cg_niblk
= sblock
.fs_ipg
;
815 acg
.cg_ndblk
= dmax
- cbase
;
816 if (sblock
.fs_contigsumsize
> 0)
817 acg
.cg_nclusterblks
= acg
.cg_ndblk
/ sblock
.fs_frag
;
818 acg
.cg_btotoff
= &acg
.cg_space
[0] - (u_char
*)(&acg
.cg_firstfield
);
819 acg
.cg_boff
= acg
.cg_btotoff
+ sblock
.fs_cpg
* sizeof(int32_t);
820 acg
.cg_iusedoff
= acg
.cg_boff
+
821 sblock
.fs_cpg
* sblock
.fs_nrpos
* sizeof(u_int16_t
);
822 acg
.cg_freeoff
= acg
.cg_iusedoff
+ howmany(sblock
.fs_ipg
, NBBY
);
823 if (sblock
.fs_contigsumsize
<= 0) {
824 acg
.cg_nextfreeoff
= acg
.cg_freeoff
+
825 howmany(sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
);
827 acg
.cg_clustersumoff
= acg
.cg_freeoff
+ howmany
828 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPF(&sblock
), NBBY
) -
830 acg
.cg_clustersumoff
=
831 roundup(acg
.cg_clustersumoff
, sizeof(u_int32_t
));
832 acg
.cg_clusteroff
= acg
.cg_clustersumoff
+
833 (sblock
.fs_contigsumsize
+ 1) * sizeof(u_int32_t
);
834 acg
.cg_nextfreeoff
= acg
.cg_clusteroff
+ howmany
835 (sblock
.fs_cpg
* sblock
.fs_spc
/ NSPB(&sblock
), NBBY
);
837 if (acg
.cg_nextfreeoff
- (long)(&acg
.cg_firstfield
) > sblock
.fs_cgsize
) {
838 printf("Panic: cylinder group too big\n");
841 acg
.cg_cs
.cs_nifree
+= sblock
.fs_ipg
;
843 for (k
= 0; k
< ROOTINO
; k
++) {
844 setbit(cg_inosused(&acg
), k
);
845 acg
.cg_cs
.cs_nifree
--;
848 for (i
= 0; i
< sblock
.fs_ipg
/ INOPF(&sblock
); i
+= sblock
.fs_frag
) {
851 j
< sblock
.fs_bsize
/ sizeof(struct ufs1_dinode
);
853 zino
[j
].di_gen
= random();
856 wtfs(fsbtodb(&sblock
, cgimin(&sblock
, cylno
) + i
),
857 sblock
.fs_bsize
, (char *)zino
);
861 * In cylno 0, beginning space is reserved
862 * for boot and super blocks.
864 for (d
= 0; d
< dlower
; d
+= sblock
.fs_frag
) {
865 blkno
= d
/ sblock
.fs_frag
;
866 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
867 if (sblock
.fs_contigsumsize
> 0)
868 setbit(cg_clustersfree(&acg
), blkno
);
869 acg
.cg_cs
.cs_nbfree
++;
870 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
871 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
872 [cbtorpos(&sblock
, d
)]++;
874 sblock
.fs_dsize
+= dlower
;
876 sblock
.fs_dsize
+= acg
.cg_ndblk
- dupper
;
877 if ((i
= dupper
% sblock
.fs_frag
)) {
878 acg
.cg_frsum
[sblock
.fs_frag
- i
]++;
879 for (d
= dupper
+ sblock
.fs_frag
- i
; dupper
< d
; dupper
++) {
880 setbit(cg_blksfree(&acg
), dupper
);
881 acg
.cg_cs
.cs_nffree
++;
884 for (d
= dupper
; d
+ sblock
.fs_frag
<= dmax
- cbase
; ) {
885 blkno
= d
/ sblock
.fs_frag
;
886 setblock(&sblock
, cg_blksfree(&acg
), blkno
);
887 if (sblock
.fs_contigsumsize
> 0)
888 setbit(cg_clustersfree(&acg
), blkno
);
889 acg
.cg_cs
.cs_nbfree
++;
890 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]++;
891 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))
892 [cbtorpos(&sblock
, d
)]++;
895 if (d
< dmax
- cbase
) {
896 acg
.cg_frsum
[dmax
- cbase
- d
]++;
897 for (; d
< dmax
- cbase
; d
++) {
898 setbit(cg_blksfree(&acg
), d
);
899 acg
.cg_cs
.cs_nffree
++;
902 if (sblock
.fs_contigsumsize
> 0) {
903 int32_t *sump
= cg_clustersum(&acg
);
904 u_char
*mapp
= cg_clustersfree(&acg
);
909 for (i
= 0; i
< acg
.cg_nclusterblks
; i
++) {
910 if ((map
& bit
) != 0) {
912 } else if (run
!= 0) {
913 if (run
> sblock
.fs_contigsumsize
)
914 run
= sblock
.fs_contigsumsize
;
918 if ((i
& (NBBY
- 1)) != (NBBY
- 1)) {
926 if (run
> sblock
.fs_contigsumsize
)
927 run
= sblock
.fs_contigsumsize
;
931 sblock
.fs_cstotal
.cs_ndir
+= acg
.cg_cs
.cs_ndir
;
932 sblock
.fs_cstotal
.cs_nffree
+= acg
.cg_cs
.cs_nffree
;
933 sblock
.fs_cstotal
.cs_nbfree
+= acg
.cg_cs
.cs_nbfree
;
934 sblock
.fs_cstotal
.cs_nifree
+= acg
.cg_cs
.cs_nifree
;
936 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, cylno
)),
937 sblock
.fs_bsize
, (char *)&acg
);
941 * initialize the file system
943 struct ufs1_dinode node
;
951 struct direct root_dir
[] = {
952 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
953 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
955 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 10, "lost+found" },
962 u_char d_name
[MAXNAMLEN
+ 1];
964 { ROOTINO
, sizeof(struct direct
), 1, "." },
965 { ROOTINO
, sizeof(struct direct
), 2, ".." },
967 { LOSTFOUNDINO
, sizeof(struct direct
), 10, "lost+found" },
971 struct direct lost_found_dir
[] = {
972 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
973 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
974 { 0, DIRBLKSIZ
, 0, 0, 0 },
976 struct odirect olost_found_dir
[] = {
977 { LOSTFOUNDINO
, sizeof(struct direct
), 1, "." },
978 { ROOTINO
, sizeof(struct direct
), 2, ".." },
979 { 0, DIRBLKSIZ
, 0, 0 },
992 * initialize the node
994 node
.di_atime
= utime
;
995 node
.di_mtime
= utime
;
996 node
.di_ctime
= utime
;
999 * create the lost+found directory
1002 makedir((struct direct
*)olost_found_dir
, 2);
1003 for (i
= DIRBLKSIZ
; i
< sblock
.fs_bsize
; i
+= DIRBLKSIZ
)
1004 memmove(&buf
[i
], &olost_found_dir
[2],
1005 DIRSIZ(0, &olost_found_dir
[2]));
1007 makedir(lost_found_dir
, 2);
1008 for (i
= DIRBLKSIZ
; i
< sblock
.fs_bsize
; i
+= DIRBLKSIZ
)
1009 memmove(&buf
[i
], &lost_found_dir
[2],
1010 DIRSIZ(0, &lost_found_dir
[2]));
1012 node
.di_mode
= IFDIR
| UMASK
;
1014 node
.di_size
= sblock
.fs_bsize
;
1015 node
.di_db
[0] = alloc(node
.di_size
, node
.di_mode
);
1016 node
.di_blocks
= btodb(fragroundup(&sblock
, node
.di_size
));
1017 wtfs(fsbtodb(&sblock
, node
.di_db
[0]), node
.di_size
, buf
);
1018 iput(&node
, LOSTFOUNDINO
);
1021 * create the root directory
1024 node
.di_mode
= IFDIR
| 01777;
1026 node
.di_mode
= IFDIR
| UMASK
;
1027 node
.di_nlink
= PREDEFDIR
;
1029 node
.di_size
= makedir((struct direct
*)oroot_dir
, PREDEFDIR
);
1031 node
.di_size
= makedir(root_dir
, PREDEFDIR
);
1032 node
.di_db
[0] = alloc(sblock
.fs_fsize
, node
.di_mode
);
1033 node
.di_blocks
= btodb(fragroundup(&sblock
, node
.di_size
));
1034 wtfs(fsbtodb(&sblock
, node
.di_db
[0]), sblock
.fs_fsize
, buf
);
1035 iput(&node
, ROOTINO
);
1039 * construct a set of directory entries in "buf".
1040 * return size of directory.
1043 makedir(struct direct
*protodir
, int entries
)
1048 spcleft
= DIRBLKSIZ
;
1049 for (cp
= buf
, i
= 0; i
< entries
- 1; i
++) {
1050 protodir
[i
].d_reclen
= DIRSIZ(0, &protodir
[i
]);
1051 memmove(cp
, &protodir
[i
], protodir
[i
].d_reclen
);
1052 cp
+= protodir
[i
].d_reclen
;
1053 spcleft
-= protodir
[i
].d_reclen
;
1055 protodir
[i
].d_reclen
= spcleft
;
1056 memmove(cp
, &protodir
[i
], DIRSIZ(0, &protodir
[i
]));
1061 * allocate a block or frag
1064 alloc(int size
, int mode
)
1069 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1071 if (acg
.cg_magic
!= CG_MAGIC
) {
1072 printf("cg 0: bad magic number\n");
1075 if (acg
.cg_cs
.cs_nbfree
== 0) {
1076 printf("first cylinder group ran out of space\n");
1079 for (d
= 0; d
< acg
.cg_ndblk
; d
+= sblock
.fs_frag
)
1080 if (isblock(&sblock
, cg_blksfree(&acg
), d
/ sblock
.fs_frag
))
1082 printf("internal error: can't find block in cyl 0\n");
1085 blkno
= fragstoblks(&sblock
, d
);
1086 clrblock(&sblock
, cg_blksfree(&acg
), blkno
);
1087 if (sblock
.fs_contigsumsize
> 0)
1088 clrbit(cg_clustersfree(&acg
), blkno
);
1089 acg
.cg_cs
.cs_nbfree
--;
1090 sblock
.fs_cstotal
.cs_nbfree
--;
1091 fscs
[0].cs_nbfree
--;
1093 acg
.cg_cs
.cs_ndir
++;
1094 sblock
.fs_cstotal
.cs_ndir
++;
1097 cg_blktot(&acg
)[cbtocylno(&sblock
, d
)]--;
1098 cg_blks(&sblock
, &acg
, cbtocylno(&sblock
, d
))[cbtorpos(&sblock
, d
)]--;
1099 if (size
!= sblock
.fs_bsize
) {
1100 frag
= howmany(size
, sblock
.fs_fsize
);
1101 fscs
[0].cs_nffree
+= sblock
.fs_frag
- frag
;
1102 sblock
.fs_cstotal
.cs_nffree
+= sblock
.fs_frag
- frag
;
1103 acg
.cg_cs
.cs_nffree
+= sblock
.fs_frag
- frag
;
1104 acg
.cg_frsum
[sblock
.fs_frag
- frag
]++;
1105 for (i
= frag
; i
< sblock
.fs_frag
; i
++)
1106 setbit(cg_blksfree(&acg
), d
+ i
);
1108 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1114 * Calculate number of inodes per group.
1117 calcipg(long cylspg
, long bpcg
, off_t
*usedbp
)
1120 long ipg
, new_ipg
, ncg
, ncyl
;
1124 * Prepare to scale by fssize / (number of sectors in cylinder groups).
1125 * Note that fssize is still in sectors, not filesystem blocks.
1127 ncyl
= howmany(fssize
, (u_int
)secpercyl
);
1128 ncg
= howmany(ncyl
, cylspg
);
1130 * Iterate a few times to allow for ipg depending on itself.
1133 for (i
= 0; i
< 10; i
++) {
1134 usedb
= (sblock
.fs_iblkno
+ ipg
/ INOPF(&sblock
))
1135 * NSPF(&sblock
) * (off_t
)sectorsize
;
1136 new_ipg
= (cylspg
* (quad_t
)bpcg
- usedb
) / density
* fssize
1137 / ncg
/ secpercyl
/ cylspg
;
1138 new_ipg
= roundup(new_ipg
, INOPB(&sblock
));
1148 * Allocate an inode on the disk
1151 iput(struct ufs1_dinode
*ip
, ino_t ino
)
1153 struct ufs1_dinode inobuf
[MAXINOPB
];
1158 ip
->di_gen
= random();
1160 c
= ino_to_cg(&sblock
, ino
);
1161 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1163 if (acg
.cg_magic
!= CG_MAGIC
) {
1164 printf("cg 0: bad magic number\n");
1167 acg
.cg_cs
.cs_nifree
--;
1168 setbit(cg_inosused(&acg
), ino
);
1169 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
,
1171 sblock
.fs_cstotal
.cs_nifree
--;
1172 fscs
[0].cs_nifree
--;
1173 if (ino
>= (uint32_t)sblock
.fs_ipg
* (uint32_t)sblock
.fs_ncg
) {
1174 printf("fsinit: inode value out of range (%ju).\n",
1178 d
= fsbtodb(&sblock
, ino_to_fsba(&sblock
, ino
));
1179 rdfs(d
, sblock
.fs_bsize
, (char *)inobuf
);
1180 inobuf
[ino_to_fsbo(&sblock
, ino
)] = *ip
;
1181 wtfs(d
, sblock
.fs_bsize
, (char *)inobuf
);
1185 * Parent notifies child that it can proceed with the newfs and mount
1186 * operation (occurs after parent has copied the underlying filesystem
1187 * if the -C option was specified (for MFS), or immediately after the
1188 * parent forked the child otherwise).
1191 parentready(__unused
int signo
)
1193 parentready_signalled
= 1;
1197 * Notify parent process that the filesystem has created itself successfully.
1199 * We have to wait until the mount has actually completed!
1202 started(__unused
int signo
)
1204 int retry
= 100; /* 10 seconds, 100ms */
1206 while (mfs_ppid
&& retry
) {
1210 stat(mfs_mtpt
, &st
) < 0 ||
1211 st
.st_dev
!= mfs_mtstat
.st_dev
1219 fatal("mfs mount failed waiting for mount to go active");
1220 } else if (copyroot
) {
1221 FSPaste(mfs_mtpt
, copyroot
, copyhlinks
);
1228 * Replace libc function with one suited to our needs.
1239 pgsz
= getpagesize() - 1;
1240 i
= (char *)((u_long
)(base
+ pgsz
) &~ pgsz
);
1241 base
= sbrk(i
- base
);
1242 if (getrlimit(RLIMIT_DATA
, &rlp
) < 0)
1244 rlp
.rlim_cur
= rlp
.rlim_max
;
1245 if (setrlimit(RLIMIT_DATA
, &rlp
) < 0)
1247 memleft
= rlp
.rlim_max
- (u_long
)base
;
1249 size
= (size
+ pgsz
) &~ pgsz
;
1255 return ((caddr_t
)sbrk(size
));
1259 * Replace libc function with one suited to our needs.
1262 realloc(char *ptr
, u_long size
)
1266 if ((p
= malloc(size
)) == NULL
)
1268 memmove(p
, ptr
, size
);
1274 * Replace libc function with one suited to our needs.
1277 calloc(u_long size
, u_long numelm
)
1282 if ((base
= malloc(size
)) == NULL
)
1284 memset(base
, 0, size
);
1289 * Replace libc function with one suited to our needs.
1295 /* do not worry about it for now */
1298 #else /* !STANDALONE */
1301 raise_data_limit(void)
1305 if (getrlimit(RLIMIT_DATA
, &rlp
) < 0)
1307 rlp
.rlim_cur
= rlp
.rlim_max
;
1308 if (setrlimit(RLIMIT_DATA
, &rlp
) < 0)
1313 extern char *_etext
;
1314 #define etext _etext
1328 pgsz
= getpagesize() - 1;
1329 dstart
= ((u_long
)&etext
) &~ pgsz
;
1330 freestart
= ((u_long
)((char *)sbrk(0) + pgsz
) &~ pgsz
);
1331 if (getrlimit(RLIMIT_DATA
, &rlp
) < 0)
1333 memused
= freestart
- dstart
;
1334 memleft
= rlp
.rlim_cur
- memused
;
1336 #endif /* STANDALONE */
1339 * read a block from the file system
1342 rdfs(daddr_t bno
, int size
, char *bf
)
1348 memmove(bf
, membase
+ bno
* sectorsize
, size
);
1351 if (lseek(fsi
, (off_t
)bno
* sectorsize
, 0) < 0) {
1352 printf("seek error: %ld\n", (long)bno
);
1355 n
= read(fsi
, bf
, size
);
1357 printf("read error: %ld\n", (long)bno
);
1362 #define WCSIZE (128 * 1024)
1363 daddr_t wc_sect
; /* units of sectorsize */
1364 int wc_end
; /* bytes */
1365 static char wc
[WCSIZE
]; /* bytes */
1368 * Flush dirty write behind buffer.
1375 if (lseek(fso
, (off_t
)wc_sect
* sectorsize
, SEEK_SET
) < 0) {
1376 printf("seek error: %ld\n", (long)wc_sect
);
1377 err(35, "wtfs - writecombine");
1379 n
= write(fso
, wc
, wc_end
);
1381 printf("write error: %ld\n", (long)wc_sect
);
1382 err(36, "wtfs - writecombine");
1389 * write a block to the file system
1392 wtfs(daddr_t bno
, int size
, char *bf
)
1398 memmove(membase
+ bno
* sectorsize
, bf
, size
);
1404 if (wc_end
== 0 && size
<= WCSIZE
) {
1406 bcopy(bf
, wc
, size
);
1408 if (wc_end
< WCSIZE
)
1412 if ((off_t
)wc_sect
* sectorsize
+ wc_end
== (off_t
)bno
* sectorsize
&&
1413 wc_end
+ size
<= WCSIZE
) {
1414 bcopy(bf
, wc
+ wc_end
, size
);
1416 if (wc_end
< WCSIZE
)
1423 if (lseek(fso
, (off_t
)bno
* sectorsize
, SEEK_SET
) < 0) {
1424 printf("seek error: %ld\n", (long)bno
);
1427 n
= write(fso
, bf
, size
);
1429 printf("write error: fso %d blk %ld %d/%d\n",
1430 fso
, (long)bno
, n
, size
);
1436 * check if a block is available
1439 isblock(struct fs
*fs
, unsigned char *cp
, int h
)
1443 switch (fs
->fs_frag
) {
1445 return (cp
[h
] == 0xff);
1447 mask
= 0x0f << ((h
& 0x1) << 2);
1448 return ((cp
[h
>> 1] & mask
) == mask
);
1450 mask
= 0x03 << ((h
& 0x3) << 1);
1451 return ((cp
[h
>> 2] & mask
) == mask
);
1453 mask
= 0x01 << (h
& 0x7);
1454 return ((cp
[h
>> 3] & mask
) == mask
);
1457 printf("isblock bad fs_frag %d\n", fs
->fs_frag
);
1459 fprintf(stderr
, "isblock bad fs_frag %d\n", fs
->fs_frag
);
1466 * take a block out of the map
1469 clrblock(struct fs
*fs
, unsigned char *cp
, int h
)
1471 switch ((fs
)->fs_frag
) {
1476 cp
[h
>> 1] &= ~(0x0f << ((h
& 0x1) << 2));
1479 cp
[h
>> 2] &= ~(0x03 << ((h
& 0x3) << 1));
1482 cp
[h
>> 3] &= ~(0x01 << (h
& 0x7));
1486 printf("clrblock bad fs_frag %d\n", fs
->fs_frag
);
1488 fprintf(stderr
, "clrblock bad fs_frag %d\n", fs
->fs_frag
);
1495 * put a block into the map
1498 setblock(struct fs
*fs
, unsigned char *cp
, int h
)
1500 switch (fs
->fs_frag
) {
1505 cp
[h
>> 1] |= (0x0f << ((h
& 0x1) << 2));
1508 cp
[h
>> 2] |= (0x03 << ((h
& 0x3) << 1));
1511 cp
[h
>> 3] |= (0x01 << (h
& 0x7));
1515 printf("setblock bad fs_frag %d\n", fs
->fs_frag
);
1517 fprintf(stderr
, "setblock bad fs_frag %d\n", fs
->fs_frag
);
1524 * Determine the number of characters in a
1536 if (ioctl(0, TIOCGWINSZ
, &ws
) != -1)
1537 columns
= ws
.ws_col
;
1538 if (columns
== 0 && (cp
= getenv("COLUMNS")))
1541 columns
= 80; /* last resort */