1 /* $NetBSD: mkfs.c,v 1.105 2009/04/11 07:20:09 lukem Exp $ */
4 * Copyright (c) 1980, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 #include <sys/cdefs.h>
74 static char sccsid
[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
76 __RCSID("$NetBSD: mkfs.c,v 1.105 2009/04/11 07:20:09 lukem Exp $");
80 #include <sys/param.h>
83 #include <sys/resource.h>
84 #include <ufs/ufs/dinode.h>
85 #include <ufs/ufs/dir.h>
86 #include <ufs/ufs/ufs_bswap.h>
87 #include <ufs/ffs/fs.h>
88 #include <ufs/ffs/ffs_extern.h>
89 #include <sys/ioctl.h>
90 #include <sys/disklabel.h>
106 struct ufs1_dinode dp1
;
107 struct ufs2_dinode dp2
;
110 static void initcg(int, const struct timeval
*);
111 static int fsinit(const struct timeval
*, mode_t
, uid_t
, gid_t
);
112 static int makedir(struct direct
*, int);
113 static daddr_t
alloc(int, int);
114 static void iput(union dinode
*, ino_t
);
115 static void rdfs(daddr_t
, int, void *);
116 static void wtfs(daddr_t
, int, void *);
117 static int isblock(struct fs
*, unsigned char *, int);
118 static void clrblock(struct fs
*, unsigned char *, int);
119 static void setblock(struct fs
*, unsigned char *, int);
120 static int ilog2(int);
121 static void zap_old_sblock(int);
123 static void calc_memfree(void);
124 static void *mkfs_malloc(size_t size
);
128 * make file system for cylinder-group style file systems
134 char pad
[SBLOCKSIZE
];
136 #define sblock fsun.fs
138 struct csum
*fscs_0
; /* first block of cylinder summaries */
139 struct csum
*fscs_next
; /* place for next summary */
140 struct csum
*fscs_end
; /* end of summary buffer */
141 struct csum
*fscs_reset
; /* place for next summary after write */
142 uint fs_csaddr
; /* fragment number to write to */
150 #define DIP(dp, field) \
151 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
152 (dp)->dp1.di_##field : (dp)->dp2.di_##field)
155 int iobufsize
; /* size to end of 2nd inode block */
156 int iobuf_memsize
; /* Actual buffer size */
161 mkfs(const char *fsys
, int fi
, int fo
,
162 mode_t mfsmode
, uid_t mfsuid
, gid_t mfsgid
)
164 uint fragsperinodeblk
, ncg
, u
;
166 uint64_t inodeblks
, cgall
;
167 int32_t cylno
, i
, csfrags
;
171 int len
, col
, delta
, fld_width
, max_cols
;
172 struct winsize winsize
;
175 gettimeofday(&tv
, NULL
);
180 if ((uint64_t)fssize
* sectorsize
> memleft
)
181 fssize
= memleft
/ sectorsize
;
182 if ((membase
= mkfs_malloc(fssize
* sectorsize
)) == NULL
)
189 sblock
.fs_old_inodefmt
= FS_42INODEFMT
;
190 sblock
.fs_maxsymlinklen
= 0;
191 sblock
.fs_old_flags
= 0;
193 sblock
.fs_old_inodefmt
= FS_44INODEFMT
;
194 sblock
.fs_maxsymlinklen
= (Oflag
== 1 ? MAXSYMLINKLEN_UFS1
:
196 sblock
.fs_old_flags
= FS_FLAGS_UPDATED
;
198 sblock
.fs_old_flags
= 0;
203 * collect and verify the filesystem density info
205 sblock
.fs_avgfilesize
= avgfilesize
;
206 sblock
.fs_avgfpdir
= avgfpdir
;
207 if (sblock
.fs_avgfilesize
<= 0) {
208 printf("illegal expected average file size %d\n",
209 sblock
.fs_avgfilesize
);
212 if (sblock
.fs_avgfpdir
<= 0) {
213 printf("illegal expected number of files per directory %d\n",
218 * collect and verify the block and fragment sizes
220 sblock
.fs_bsize
= bsize
;
221 sblock
.fs_fsize
= fsize
;
222 if (!powerof2(sblock
.fs_bsize
)) {
223 printf("block size must be a power of 2, not %d\n",
227 if (!powerof2(sblock
.fs_fsize
)) {
228 printf("fragment size must be a power of 2, not %d\n",
232 if (sblock
.fs_fsize
< sectorsize
) {
233 printf("fragment size %d is too small, minimum is %d\n",
234 sblock
.fs_fsize
, sectorsize
);
237 if (sblock
.fs_bsize
< MINBSIZE
) {
238 printf("block size %d is too small, minimum is %d\n",
239 sblock
.fs_bsize
, MINBSIZE
);
242 if (sblock
.fs_bsize
> MAXBSIZE
) {
243 printf("block size %d is too large, maximum is %d\n",
244 sblock
.fs_bsize
, MAXBSIZE
);
247 if (sblock
.fs_bsize
< sblock
.fs_fsize
) {
248 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
249 sblock
.fs_bsize
, sblock
.fs_fsize
);
253 if (maxbsize
< bsize
|| !powerof2(maxbsize
)) {
254 sblock
.fs_maxbsize
= sblock
.fs_bsize
;
255 } else if (sblock
.fs_maxbsize
> FS_MAXCONTIG
* sblock
.fs_bsize
) {
256 sblock
.fs_maxbsize
= FS_MAXCONTIG
* sblock
.fs_bsize
;
258 sblock
.fs_maxbsize
= maxbsize
;
260 sblock
.fs_maxcontig
= maxcontig
;
261 if (sblock
.fs_maxcontig
< sblock
.fs_maxbsize
/ sblock
.fs_bsize
) {
262 sblock
.fs_maxcontig
= sblock
.fs_maxbsize
/ sblock
.fs_bsize
;
264 printf("Maxcontig raised to %d\n", sblock
.fs_maxbsize
);
266 if (sblock
.fs_maxcontig
> 1)
267 sblock
.fs_contigsumsize
= MIN(sblock
.fs_maxcontig
,FS_MAXCONTIG
);
269 sblock
.fs_bmask
= ~(sblock
.fs_bsize
- 1);
270 sblock
.fs_fmask
= ~(sblock
.fs_fsize
- 1);
271 sblock
.fs_qbmask
= ~sblock
.fs_bmask
;
272 sblock
.fs_qfmask
= ~sblock
.fs_fmask
;
273 for (sblock
.fs_bshift
= 0, i
= sblock
.fs_bsize
; i
> 1; i
>>= 1)
275 for (sblock
.fs_fshift
= 0, i
= sblock
.fs_fsize
; i
> 1; i
>>= 1)
277 sblock
.fs_frag
= numfrags(&sblock
, sblock
.fs_bsize
);
278 for (sblock
.fs_fragshift
= 0, i
= sblock
.fs_frag
; i
> 1; i
>>= 1)
279 sblock
.fs_fragshift
++;
280 if (sblock
.fs_frag
> MAXFRAG
) {
281 printf("fragment size %d is too small, "
282 "minimum with block size %d is %d\n",
283 sblock
.fs_fsize
, sblock
.fs_bsize
,
284 sblock
.fs_bsize
/ MAXFRAG
);
287 sblock
.fs_fsbtodb
= ilog2(sblock
.fs_fsize
/ sectorsize
);
288 sblock
.fs_size
= dbtofsb(&sblock
, fssize
);
290 if ((uint64_t)sblock
.fs_size
>= 1ull << 31) {
291 printf("Too many fragments (0x%" PRIx64
292 ") for a FFSv1 filesystem\n", sblock
.fs_size
);
295 sblock
.fs_magic
= FS_UFS1_MAGIC
;
296 sblock
.fs_sblockloc
= SBLOCK_UFS1
;
297 sblock
.fs_nindir
= sblock
.fs_bsize
/ sizeof(int32_t);
298 sblock
.fs_inopb
= sblock
.fs_bsize
/ sizeof(struct ufs1_dinode
);
299 sblock
.fs_old_cgoffset
= 0;
300 sblock
.fs_old_cgmask
= 0xffffffff;
301 sblock
.fs_old_size
= sblock
.fs_size
;
302 sblock
.fs_old_rotdelay
= 0;
303 sblock
.fs_old_rps
= 60;
304 sblock
.fs_old_nspf
= sblock
.fs_fsize
/ sectorsize
;
305 sblock
.fs_old_cpg
= 1;
306 sblock
.fs_old_interleave
= 1;
307 sblock
.fs_old_trackskew
= 0;
308 sblock
.fs_old_cpc
= 0;
309 sblock
.fs_old_postblformat
= FS_DYNAMICPOSTBLFMT
;
310 sblock
.fs_old_nrpos
= 1;
312 sblock
.fs_magic
= FS_UFS2_MAGIC
;
313 sblock
.fs_sblockloc
= SBLOCK_UFS2
;
314 sblock
.fs_nindir
= sblock
.fs_bsize
/ sizeof(int64_t);
315 sblock
.fs_inopb
= sblock
.fs_bsize
/ sizeof(struct ufs2_dinode
);
319 roundup(howmany(sblock
.fs_sblockloc
+ SBLOCKSIZE
, sblock
.fs_fsize
),
321 sblock
.fs_cblkno
= (daddr_t
)(sblock
.fs_sblkno
+
322 roundup(howmany(SBLOCKSIZE
, sblock
.fs_fsize
), sblock
.fs_frag
));
323 sblock
.fs_iblkno
= sblock
.fs_cblkno
+ sblock
.fs_frag
;
324 sblock
.fs_maxfilesize
= sblock
.fs_bsize
* NDADDR
- 1;
325 for (sizepb
= sblock
.fs_bsize
, i
= 0; i
< NIADDR
; i
++) {
326 sizepb
*= NINDIR(&sblock
);
327 sblock
.fs_maxfilesize
+= sizepb
;
331 * Calculate the number of blocks to put into each cylinder group.
333 * The cylinder group size is limited because the data structure
334 * must fit into a single block.
335 * We try to have as few cylinder groups as possible, with a proviso
336 * that we create at least MINCYLGRPS (==4) except for small
339 * This algorithm works out how many blocks of inodes would be
340 * needed to fill the entire volume at the specified density.
341 * It then looks at how big the 'cylinder block' would have to
342 * be and, assuming that it is linearly related to the number
343 * of inodes and blocks how many cylinder groups are needed to
344 * keep the cylinder block below the filesystem block size.
346 * The cylinder groups are then all created with the average size.
348 * Space taken by the red tape on cylinder groups other than the
352 /* There must be space for 1 inode block and 2 data blocks */
353 if (sblock
.fs_size
< sblock
.fs_iblkno
+ 3 * sblock
.fs_frag
) {
354 printf("Filesystem size %lld < minimum size of %d\n",
355 (long long)sblock
.fs_size
, sblock
.fs_iblkno
+ 3 * sblock
.fs_frag
);
359 inodeblks
= howmany(num_inodes
, INOPB(&sblock
));
362 * Calculate 'per inode block' so we can allocate less than
363 * 1 fragment per inode - useful for /dev.
365 fragsperinodeblk
= MAX(numfrags(&sblock
,
366 (uint64_t)density
* INOPB(&sblock
)), 1);
367 inodeblks
= (sblock
.fs_size
- sblock
.fs_iblkno
) /
368 (sblock
.fs_frag
+ fragsperinodeblk
);
372 /* Ensure that there are at least 2 data blocks (or we fail below) */
373 if (inodeblks
> (uint64_t)(sblock
.fs_size
- sblock
.fs_iblkno
)/sblock
.fs_frag
- 2)
374 inodeblks
= (sblock
.fs_size
-sblock
.fs_iblkno
)/sblock
.fs_frag
-2;
375 /* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
376 if (inodeblks
* INOPB(&sblock
) >= 1ull << 31)
377 inodeblks
= ((1ull << 31) - NBBY
) / INOPB(&sblock
);
379 * See what would happen if we tried to use 1 cylinder group.
380 * Assume space linear, so work out number of cylinder groups needed.
382 cgzero
= CGSIZE_IF(&sblock
, 0, 0);
383 cgall
= CGSIZE_IF(&sblock
, inodeblks
* INOPB(&sblock
), sblock
.fs_size
);
384 ncg
= howmany(cgall
- cgzero
, sblock
.fs_bsize
- cgzero
);
385 if (ncg
< MINCYLGRPS
) {
387 * We would like to allocate MINCLYGRPS cylinder groups,
388 * but for small file sytems (especially ones with a lot
389 * of inodes) this is not desirable (or possible).
391 u
= sblock
.fs_size
/ 2 / (sblock
.fs_iblkno
+
392 inodeblks
* sblock
.fs_frag
);
395 if (ncg
> MINCYLGRPS
)
401 * Put an equal number of blocks in each cylinder group.
402 * Round up so we don't have more fragments in the last CG than
403 * the earlier ones (does that matter?), but kill a block if the
404 * CGSIZE becomes too big (only happens if there are a lot of CGs).
406 sblock
.fs_fpg
= roundup(howmany(sblock
.fs_size
, ncg
), sblock
.fs_frag
);
407 /* Round up the fragments/group so the bitmap bytes are full */
408 sblock
.fs_fpg
= roundup(sblock
.fs_fpg
, NBBY
);
409 inodes_per_cg
= ((inodeblks
- 1) / ncg
+ 1) * INOPB(&sblock
);
411 i
= CGSIZE_IF(&sblock
, inodes_per_cg
, sblock
.fs_fpg
);
412 if (i
> sblock
.fs_bsize
) {
413 sblock
.fs_fpg
-= (i
- sblock
.fs_bsize
) * NBBY
;
414 /* ... and recalculate how many cylinder groups we now need */
415 ncg
= howmany(sblock
.fs_size
, sblock
.fs_fpg
);
416 inodes_per_cg
= ((inodeblks
- 1) / ncg
+ 1) * INOPB(&sblock
);
418 sblock
.fs_ipg
= inodes_per_cg
;
419 /* Sanity check on our sums... */
420 if ((int)CGSIZE(&sblock
) > sblock
.fs_bsize
) {
421 printf("CGSIZE miscalculated %d > %d\n",
422 (int)CGSIZE(&sblock
), sblock
.fs_bsize
);
426 sblock
.fs_dblkno
= sblock
.fs_iblkno
+ sblock
.fs_ipg
/ INOPF(&sblock
);
427 /* Check that the last cylinder group has enough space for the inodes */
428 i
= sblock
.fs_size
- sblock
.fs_fpg
* (ncg
- 1ull);
429 if (i
< sblock
.fs_dblkno
) {
431 * Since we make all the cylinder groups the same size, the
432 * last will only be small if there are a large number of
433 * cylinder groups. If we pull even a fragment from each
434 * of the other groups then the last CG will be overfull.
435 * So we just kill the last CG.
442 sblock
.fs_cgsize
= fragroundup(&sblock
, CGSIZE(&sblock
));
444 sblock
.fs_old_spc
= sblock
.fs_fpg
* sblock
.fs_old_nspf
;
445 sblock
.fs_old_nsect
= sblock
.fs_old_spc
;
446 sblock
.fs_old_npsect
= sblock
.fs_old_spc
;
447 sblock
.fs_old_ncyl
= sblock
.fs_ncg
;
451 * Cylinder group summary information for each cylinder is written
452 * into the first cylinder group.
453 * Write this fragment by fragment, but doing the first CG last
454 * (after we've taken stuff off for the structure itself and the
457 sblock
.fs_csaddr
= cgdmin(&sblock
, 0);
459 fragroundup(&sblock
, sblock
.fs_ncg
* sizeof(struct csum
));
460 if (512 % sizeof *fscs_0
)
461 errx(1, "cylinder group summary doesn't fit in sectors");
462 fscs_0
= mmap(0, 2 * sblock
.fs_fsize
, PROT_READ
|PROT_WRITE
,
463 MAP_ANON
|MAP_PRIVATE
, -1, 0);
464 if (fscs_0
== MAP_FAILED
)
466 memset(fscs_0
, 0, 2 * sblock
.fs_fsize
);
467 fs_csaddr
= sblock
.fs_csaddr
;
469 fscs_end
= (void *)((char *)fscs_0
+ 2 * sblock
.fs_fsize
);
470 fscs_reset
= (void *)((char *)fscs_0
+ sblock
.fs_fsize
);
472 * fill in remaining fields of the super block
474 sblock
.fs_sbsize
= fragroundup(&sblock
, sizeof(struct fs
));
475 if (sblock
.fs_sbsize
> SBLOCKSIZE
)
476 sblock
.fs_sbsize
= SBLOCKSIZE
;
477 sblock
.fs_minfree
= minfree
;
478 sblock
.fs_maxcontig
= maxcontig
;
479 sblock
.fs_maxbpg
= maxbpg
;
480 sblock
.fs_optim
= opt
;
481 sblock
.fs_cgrotor
= 0;
482 sblock
.fs_pendingblocks
= 0;
483 sblock
.fs_pendinginodes
= 0;
484 sblock
.fs_cstotal
.cs_ndir
= 0;
485 sblock
.fs_cstotal
.cs_nbfree
= 0;
486 sblock
.fs_cstotal
.cs_nifree
= 0;
487 sblock
.fs_cstotal
.cs_nffree
= 0;
491 sblock
.fs_clean
= FS_ISCLEAN
;
493 sblock
.fs_id
[0] = (long)tv
.tv_sec
; /* XXXfvdl huh? */
494 sblock
.fs_id
[1] = arc4random() & INT32_MAX
;
495 sblock
.fs_fsmnt
[0] = '\0';
496 csfrags
= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
497 sblock
.fs_dsize
= sblock
.fs_size
- sblock
.fs_sblkno
-
498 sblock
.fs_ncg
* (sblock
.fs_dblkno
- sblock
.fs_sblkno
);
499 sblock
.fs_cstotal
.cs_nbfree
=
500 fragstoblks(&sblock
, sblock
.fs_dsize
) -
501 howmany(csfrags
, sblock
.fs_frag
);
502 sblock
.fs_cstotal
.cs_nffree
=
503 fragnum(&sblock
, sblock
.fs_size
) +
504 (fragnum(&sblock
, csfrags
) > 0 ?
505 sblock
.fs_frag
- fragnum(&sblock
, csfrags
) : 0);
506 sblock
.fs_cstotal
.cs_nifree
= sblock
.fs_ncg
* sblock
.fs_ipg
- ROOTINO
;
507 sblock
.fs_cstotal
.cs_ndir
= 0;
508 sblock
.fs_dsize
-= csfrags
;
509 sblock
.fs_time
= tv
.tv_sec
;
511 sblock
.fs_old_time
= tv
.tv_sec
;
512 sblock
.fs_old_dsize
= sblock
.fs_dsize
;
513 sblock
.fs_old_csaddr
= sblock
.fs_csaddr
;
514 sblock
.fs_old_cstotal
.cs_ndir
= sblock
.fs_cstotal
.cs_ndir
;
515 sblock
.fs_old_cstotal
.cs_nbfree
= sblock
.fs_cstotal
.cs_nbfree
;
516 sblock
.fs_old_cstotal
.cs_nifree
= sblock
.fs_cstotal
.cs_nifree
;
517 sblock
.fs_old_cstotal
.cs_nffree
= sblock
.fs_cstotal
.cs_nffree
;
520 * Dump out summary information about file system.
523 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
524 printf("%s: %.1fMB (%lld sectors) block size %d, "
525 "fragment size %d\n",
526 fsys
, (float)sblock
.fs_size
* sblock
.fs_fsize
* B2MBFACTOR
,
527 (long long)fsbtodb(&sblock
, sblock
.fs_size
),
528 sblock
.fs_bsize
, sblock
.fs_fsize
);
529 printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
532 (float)sblock
.fs_fpg
* sblock
.fs_fsize
* B2MBFACTOR
,
533 sblock
.fs_fpg
/ sblock
.fs_frag
, sblock
.fs_ipg
);
538 * allocate space for superblock, cylinder group map, and
539 * two sets of inode blocks.
541 if (sblock
.fs_bsize
< SBLOCKSIZE
)
542 iobufsize
= SBLOCKSIZE
+ 3 * sblock
.fs_bsize
;
544 iobufsize
= 4 * sblock
.fs_bsize
;
545 iobuf_memsize
= iobufsize
;
546 if (!mfs
&& sblock
.fs_magic
== FS_UFS1_MAGIC
) {
547 /* A larger buffer so we can write multiple inode blks */
548 iobuf_memsize
+= 14 * sblock
.fs_bsize
;
551 iobuf
= mmap(0, iobuf_memsize
, PROT_READ
|PROT_WRITE
,
552 MAP_ANON
|MAP_PRIVATE
, -1, 0);
553 if (iobuf
!= MAP_FAILED
)
555 if (iobuf_memsize
!= iobufsize
) {
556 /* Try again with the smaller size */
557 iobuf_memsize
= iobufsize
;
560 printf("Cannot allocate I/O buffer\n");
563 memset(iobuf
, 0, iobuf_memsize
);
566 * We now start writing to the filesystem
571 * Validate the given file system size.
572 * Verify that its last block can actually be accessed.
573 * Convert to file system fragment sized units.
576 printf("preposterous size %lld\n", (long long)fssize
);
579 wtfs(fssize
- 1, sectorsize
, iobuf
);
582 * Ensure there is nothing that looks like a filesystem
583 * superbock anywhere other than where ours will be.
584 * If fsck finds the wrong one all hell breaks loose!
587 static const int sblocklist
[] = SBLOCKSEARCH
;
588 int sblkoff
= sblocklist
[i
];
592 /* Remove main superblock */
593 zap_old_sblock(sblkoff
);
594 /* and all possible locations for the first alternate */
595 sblkoff
+= SBLOCKSIZE
;
596 for (sz
= SBLOCKSIZE
; sz
<= 0x10000; sz
<<= 1)
597 zap_old_sblock(roundup(sblkoff
, sz
));
601 struct appleufslabel appleufs
;
602 ffs_appleufs_set(&appleufs
, appleufs_volname
,
604 wtfs(APPLEUFS_LABEL_OFFSET
/sectorsize
,
605 APPLEUFS_LABEL_SIZE
, &appleufs
);
607 struct appleufslabel appleufs
;
608 /* Look for & zap any existing valid apple ufs labels */
609 rdfs(APPLEUFS_LABEL_OFFSET
/sectorsize
,
610 APPLEUFS_LABEL_SIZE
, &appleufs
);
611 if (ffs_appleufs_validate(fsys
, &appleufs
, NULL
) == 0) {
612 memset(&appleufs
, 0, sizeof(appleufs
));
613 wtfs(APPLEUFS_LABEL_OFFSET
/sectorsize
,
614 APPLEUFS_LABEL_SIZE
, &appleufs
);
620 * Make a copy of the superblock into the buffer that we will be
621 * writing out in each cylinder group.
623 memcpy(iobuf
, &sblock
, sizeof sblock
);
625 ffs_sb_swap(&sblock
, (struct fs
*)iobuf
);
626 if ((sblock
.fs_old_flags
& FS_FLAGS_UPDATED
) == 0)
627 memset(iobuf
+ offsetof(struct fs
, fs_old_postbl_start
),
631 printf("super-block backups (for fsck_ffs -b #) at:\n");
632 /* If we are printing more than one line of numbers, line up columns */
633 fld_width
= verbosity
< 4 ? 1 : snprintf(NULL
, 0, "%" PRIu64
,
634 (uint64_t)fsbtodb(&sblock
, cgsblock(&sblock
, sblock
.fs_ncg
-1)));
635 /* Get terminal width */
636 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &winsize
) == 0)
637 max_cols
= winsize
.ws_col
;
640 if (Nflag
&& verbosity
== 3)
641 /* Leave space to add " ..." after one row of numbers */
643 #define BASE 0x10000 /* For some fixed-point maths */
645 delta
= verbosity
> 2 ? 0 : max_cols
* BASE
/ sblock
.fs_ncg
;
646 for (cylno
= 0; cylno
< sblock
.fs_ncg
; cylno
++) {
653 /* No point doing dots for -N */
655 /* Print dots scaled to end near RH margin */
656 for (col
+= delta
; col
> BASE
; col
-= BASE
)
660 /* Print superblock numbers */
661 len
= printf(" %*" PRIu64
"," + !col
, fld_width
,
662 (uint64_t)fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)));
664 if (col
+ len
< max_cols
)
665 /* Next number fits */
667 /* Next number won't fit, need a newline */
668 if (verbosity
<= 3) {
669 /* Print dots for subsequent cylinder groups */
670 delta
= sblock
.fs_ncg
- cylno
- 1;
676 delta
= max_cols
* BASE
/ delta
;
689 * Now construct the initial file system,
691 if (fsinit(&tv
, mfsmode
, mfsuid
, mfsgid
) == 0 && mfs
)
692 errx(1, "Error making filesystem");
693 sblock
.fs_time
= tv
.tv_sec
;
695 sblock
.fs_old_cstotal
.cs_ndir
= sblock
.fs_cstotal
.cs_ndir
;
696 sblock
.fs_old_cstotal
.cs_nbfree
= sblock
.fs_cstotal
.cs_nbfree
;
697 sblock
.fs_old_cstotal
.cs_nifree
= sblock
.fs_cstotal
.cs_nifree
;
698 sblock
.fs_old_cstotal
.cs_nffree
= sblock
.fs_cstotal
.cs_nffree
;
701 * Write out the super-block and zeros until the first cg info
703 i
= cgsblock(&sblock
, 0) * sblock
.fs_fsize
- sblock
.fs_sblockloc
,
705 memcpy(iobuf
, &sblock
, sizeof sblock
);
707 ffs_sb_swap(&sblock
, (struct fs
*)iobuf
);
708 if ((sblock
.fs_old_flags
& FS_FLAGS_UPDATED
) == 0)
709 memset(iobuf
+ offsetof(struct fs
, fs_old_postbl_start
),
711 wtfs(sblock
.fs_sblockloc
/ sectorsize
, i
, iobuf
);
713 /* Write out first and last cylinder summary sectors */
715 ffs_csum_swap(fscs_0
, fscs_0
, sblock
.fs_fsize
);
716 wtfs(fsbtodb(&sblock
, sblock
.fs_csaddr
), sblock
.fs_fsize
, fscs_0
);
718 if (fscs_next
> fscs_reset
) {
720 ffs_csum_swap(fscs_reset
, fscs_reset
, sblock
.fs_fsize
);
722 wtfs(fsbtodb(&sblock
, fs_csaddr
), sblock
.fs_fsize
, fscs_reset
);
725 /* mfs doesn't need these permanently allocated */
726 munmap(iobuf
, iobuf_memsize
);
727 munmap(fscs_0
, 2 * sblock
.fs_fsize
);
731 * Initialize a cylinder group.
734 initcg(int cylno
, const struct timeval
*tv
)
737 int32_t i
, d
, dlower
, dupper
, blkno
;
739 struct ufs1_dinode
*dp1
;
740 struct ufs2_dinode
*dp2
;
744 * Determine block bounds for cylinder group.
745 * Allow space for super block summary information in first
748 cbase
= cgbase(&sblock
, cylno
);
749 dmax
= cbase
+ sblock
.fs_fpg
;
750 if (dmax
> sblock
.fs_size
)
751 dmax
= sblock
.fs_size
;
752 dlower
= cgsblock(&sblock
, cylno
) - cbase
;
753 dupper
= cgdmin(&sblock
, cylno
) - cbase
;
755 dupper
+= howmany(sblock
.fs_cssize
, sblock
.fs_fsize
);
756 if (dupper
>= cgstart(&sblock
, cylno
+ 1)) {
757 printf("\rToo many cylinder groups to fit summary "
758 "information into first cylinder group\n");
762 memset(&acg
, 0, sblock
.fs_cgsize
);
763 acg
.cg_magic
= CG_MAGIC
;
765 acg
.cg_ndblk
= dmax
- cbase
;
766 if (sblock
.fs_contigsumsize
> 0)
767 acg
.cg_nclusterblks
= acg
.cg_ndblk
>> sblock
.fs_fragshift
;
768 start
= &acg
.cg_space
[0] - (u_char
*)(&acg
.cg_firstfield
);
770 acg
.cg_time
= tv
->tv_sec
;
771 acg
.cg_niblk
= sblock
.fs_ipg
;
772 acg
.cg_initediblk
= sblock
.fs_ipg
< 2 * INOPB(&sblock
) ?
773 sblock
.fs_ipg
: 2 * INOPB(&sblock
);
774 acg
.cg_iusedoff
= start
;
776 acg
.cg_old_ncyl
= sblock
.fs_old_cpg
;
777 if ((sblock
.fs_old_flags
& FS_FLAGS_UPDATED
) == 0 &&
778 (cylno
== sblock
.fs_ncg
- 1))
780 sblock
.fs_old_ncyl
% sblock
.fs_old_cpg
;
781 acg
.cg_old_time
= tv
->tv_sec
;
782 acg
.cg_old_niblk
= sblock
.fs_ipg
;
783 acg
.cg_old_btotoff
= start
;
784 acg
.cg_old_boff
= acg
.cg_old_btotoff
+
785 sblock
.fs_old_cpg
* sizeof(int32_t);
786 acg
.cg_iusedoff
= acg
.cg_old_boff
+
787 sblock
.fs_old_cpg
* sizeof(u_int16_t
);
789 acg
.cg_freeoff
= acg
.cg_iusedoff
+ howmany(sblock
.fs_ipg
, CHAR_BIT
);
790 if (sblock
.fs_contigsumsize
<= 0) {
791 acg
.cg_nextfreeoff
= acg
.cg_freeoff
+
792 howmany(sblock
.fs_fpg
, CHAR_BIT
);
794 acg
.cg_clustersumoff
= acg
.cg_freeoff
+
795 howmany(sblock
.fs_fpg
, CHAR_BIT
) - sizeof(int32_t);
797 /* Apple PR2216969 gives rationale for this change.
798 * I believe they were mistaken, but we need to
799 * duplicate it for compatibility. -- dbj@NetBSD.org
801 acg
.cg_clustersumoff
+= sizeof(int32_t);
803 acg
.cg_clustersumoff
=
804 roundup(acg
.cg_clustersumoff
, sizeof(int32_t));
805 acg
.cg_clusteroff
= acg
.cg_clustersumoff
+
806 (sblock
.fs_contigsumsize
+ 1) * sizeof(int32_t);
807 acg
.cg_nextfreeoff
= acg
.cg_clusteroff
+
808 howmany(fragstoblks(&sblock
, sblock
.fs_fpg
), CHAR_BIT
);
810 if (acg
.cg_nextfreeoff
> sblock
.fs_cgsize
) {
811 printf("Panic: cylinder group too big\n");
814 acg
.cg_cs
.cs_nifree
+= sblock
.fs_ipg
;
816 for (u
= 0; u
< ROOTINO
; u
++) {
817 setbit(cg_inosused(&acg
, 0), u
);
818 acg
.cg_cs
.cs_nifree
--;
822 * In cylno 0, beginning space is reserved
823 * for boot and super blocks.
825 for (d
= 0, blkno
= 0; d
< dlower
;) {
826 setblock(&sblock
, cg_blksfree(&acg
, 0), blkno
);
827 if (sblock
.fs_contigsumsize
> 0)
828 setbit(cg_clustersfree(&acg
, 0), blkno
);
829 acg
.cg_cs
.cs_nbfree
++;
831 int cn
= old_cbtocylno(&sblock
, d
);
832 old_cg_blktot(&acg
, 0)[cn
]++;
833 old_cg_blks(&sblock
, &acg
,
834 cn
, 0)[old_cbtorpos(&sblock
, d
)]++;
840 if ((i
= (dupper
& (sblock
.fs_frag
- 1))) != 0) {
841 acg
.cg_frsum
[sblock
.fs_frag
- i
]++;
842 for (d
= dupper
+ sblock
.fs_frag
- i
; dupper
< d
; dupper
++) {
843 setbit(cg_blksfree(&acg
, 0), dupper
);
844 acg
.cg_cs
.cs_nffree
++;
847 for (d
= dupper
, blkno
= dupper
>> sblock
.fs_fragshift
;
848 d
+ sblock
.fs_frag
<= acg
.cg_ndblk
; ) {
849 setblock(&sblock
, cg_blksfree(&acg
, 0), blkno
);
850 if (sblock
.fs_contigsumsize
> 0)
851 setbit(cg_clustersfree(&acg
, 0), blkno
);
852 acg
.cg_cs
.cs_nbfree
++;
854 int cn
= old_cbtocylno(&sblock
, d
);
855 old_cg_blktot(&acg
, 0)[cn
]++;
856 old_cg_blks(&sblock
, &acg
,
857 cn
, 0)[old_cbtorpos(&sblock
, d
)]++;
862 if (d
< acg
.cg_ndblk
) {
863 acg
.cg_frsum
[acg
.cg_ndblk
- d
]++;
864 for (; d
< acg
.cg_ndblk
; d
++) {
865 setbit(cg_blksfree(&acg
, 0), d
);
866 acg
.cg_cs
.cs_nffree
++;
869 if (sblock
.fs_contigsumsize
> 0) {
870 int32_t *sump
= cg_clustersum(&acg
, 0);
871 u_char
*mapp
= cg_clustersfree(&acg
, 0);
876 for (i
= 0; i
< acg
.cg_nclusterblks
; i
++) {
877 if ((map
& bit
) != 0) {
879 } else if (run
!= 0) {
880 if (run
> sblock
.fs_contigsumsize
)
881 run
= sblock
.fs_contigsumsize
;
885 if ((i
& (CHAR_BIT
- 1)) != (CHAR_BIT
- 1)) {
893 if (run
> sblock
.fs_contigsumsize
)
894 run
= sblock
.fs_contigsumsize
;
898 *fscs_next
++ = acg
.cg_cs
;
899 if (fscs_next
== fscs_end
) {
900 /* write block of cylinder group summary info into cyl 0 */
902 ffs_csum_swap(fscs_reset
, fscs_reset
, sblock
.fs_fsize
);
904 wtfs(fsbtodb(&sblock
, fs_csaddr
), sblock
.fs_fsize
, fscs_reset
);
905 fscs_next
= fscs_reset
;
906 memset(fscs_next
, 0, sblock
.fs_fsize
);
909 * Write out the duplicate super block, the cylinder group map
910 * and two blocks worth of inodes in a single write.
912 start
= sblock
.fs_bsize
> SBLOCKSIZE
? sblock
.fs_bsize
: SBLOCKSIZE
;
913 memcpy(&iobuf
[start
], &acg
, sblock
.fs_cgsize
);
915 ffs_cg_swap(&acg
, (struct cg
*)&iobuf
[start
], &sblock
);
916 start
+= sblock
.fs_bsize
;
917 dp1
= (struct ufs1_dinode
*)(&iobuf
[start
]);
918 dp2
= (struct ufs2_dinode
*)(&iobuf
[start
]);
919 for (i
= MIN(sblock
.fs_ipg
, 2) * INOPB(&sblock
); i
!= 0; i
--) {
920 if (sblock
.fs_magic
== FS_UFS1_MAGIC
) {
921 /* No need to swap, it'll stay random */
922 dp1
->di_gen
= arc4random() & INT32_MAX
;
925 dp2
->di_gen
= arc4random() & INT32_MAX
;
929 wtfs(fsbtodb(&sblock
, cgsblock(&sblock
, cylno
)), iobufsize
, iobuf
);
931 * For the old file system, we have to initialize all the inodes.
933 if (sblock
.fs_magic
!= FS_UFS1_MAGIC
)
936 /* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
937 d
= (iobuf_memsize
- start
) / sblock
.fs_bsize
* sblock
.fs_frag
;
938 dupper
= sblock
.fs_ipg
/ INOPF(&sblock
);
939 for (i
= 2 * sblock
.fs_frag
; i
< dupper
; i
+= d
) {
942 dp1
= (struct ufs1_dinode
*)(&iobuf
[start
]);
944 dp1
->di_gen
= arc4random() & INT32_MAX
;
945 while ((char *)++dp1
< &iobuf
[iobuf_memsize
]);
946 wtfs(fsbtodb(&sblock
, cgimin(&sblock
, cylno
) + i
),
947 d
* sblock
.fs_bsize
/ sblock
.fs_frag
, &iobuf
[start
]);
952 * initialize the file system
961 struct direct root_dir
[] = {
962 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
963 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
965 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 10, "lost+found" },
972 u_char d_name
[FFS_MAXNAMLEN
+ 1];
974 { ROOTINO
, sizeof(struct direct
), 1, "." },
975 { ROOTINO
, sizeof(struct direct
), 2, ".." },
977 { LOSTFOUNDINO
, sizeof(struct direct
), 10, "lost+found" },
981 struct direct lost_found_dir
[] = {
982 { LOSTFOUNDINO
, sizeof(struct direct
), DT_DIR
, 1, "." },
983 { ROOTINO
, sizeof(struct direct
), DT_DIR
, 2, ".." },
984 { 0, DIRBLKSIZ
, 0, 0, 0 },
986 struct odirect olost_found_dir
[] = {
987 { LOSTFOUNDINO
, sizeof(struct direct
), 1, "." },
988 { ROOTINO
, sizeof(struct direct
), 2, ".." },
989 { 0, DIRBLKSIZ
, 0, 0 },
993 static void copy_dir(struct direct
*, struct direct
*);
996 fsinit(const struct timeval
*tv
, mode_t mfsmode
, uid_t mfsuid
, gid_t mfsgid
)
1001 int dirblksiz
= DIRBLKSIZ
;
1003 dirblksiz
= APPLEUFS_DIRBLKSIZ
;
1007 * initialize the node
1012 * create the lost+found directory
1014 memset(&node
, 0, sizeof(node
));
1016 (void)makedir((struct direct
*)olost_found_dir
, 2);
1017 for (i
= dirblksiz
; i
< sblock
.fs_bsize
; i
+= dirblksiz
)
1018 copy_dir((struct direct
*)&olost_found_dir
[2],
1019 (struct direct
*)&buf
[i
]);
1021 (void)makedir(lost_found_dir
, 2);
1022 for (i
= dirblksiz
; i
< sblock
.fs_bsize
; i
+= dirblksiz
)
1023 copy_dir(&lost_found_dir
[2], (struct direct
*)&buf
[i
]);
1025 if (sblock
.fs_magic
== FS_UFS1_MAGIC
) {
1026 node
.dp1
.di_atime
= tv
->tv_sec
;
1027 node
.dp1
.di_atimensec
= tv
->tv_usec
* 1000;
1028 node
.dp1
.di_mtime
= tv
->tv_sec
;
1029 node
.dp1
.di_mtimensec
= tv
->tv_usec
* 1000;
1030 node
.dp1
.di_ctime
= tv
->tv_sec
;
1031 node
.dp1
.di_ctimensec
= tv
->tv_usec
* 1000;
1032 node
.dp1
.di_mode
= IFDIR
| UMASK
;
1033 node
.dp1
.di_nlink
= 2;
1034 node
.dp1
.di_size
= sblock
.fs_bsize
;
1035 node
.dp1
.di_db
[0] = alloc(node
.dp1
.di_size
, node
.dp1
.di_mode
);
1036 if (node
.dp1
.di_db
[0] == 0)
1038 node
.dp1
.di_blocks
= btodb(fragroundup(&sblock
,
1040 node
.dp1
.di_uid
= geteuid();
1041 node
.dp1
.di_gid
= getegid();
1042 wtfs(fsbtodb(&sblock
, node
.dp1
.di_db
[0]), node
.dp1
.di_size
,
1045 node
.dp2
.di_atime
= tv
->tv_sec
;
1046 node
.dp2
.di_atimensec
= tv
->tv_usec
* 1000;
1047 node
.dp2
.di_mtime
= tv
->tv_sec
;
1048 node
.dp2
.di_mtimensec
= tv
->tv_usec
* 1000;
1049 node
.dp2
.di_ctime
= tv
->tv_sec
;
1050 node
.dp2
.di_ctimensec
= tv
->tv_usec
* 1000;
1051 node
.dp2
.di_birthtime
= tv
->tv_sec
;
1052 node
.dp2
.di_birthnsec
= tv
->tv_usec
* 1000;
1053 node
.dp2
.di_mode
= IFDIR
| UMASK
;
1054 node
.dp2
.di_nlink
= 2;
1055 node
.dp2
.di_size
= sblock
.fs_bsize
;
1056 node
.dp2
.di_db
[0] = alloc(node
.dp2
.di_size
, node
.dp2
.di_mode
);
1057 if (node
.dp2
.di_db
[0] == 0)
1059 node
.dp2
.di_blocks
= btodb(fragroundup(&sblock
,
1061 node
.dp2
.di_uid
= geteuid();
1062 node
.dp2
.di_gid
= getegid();
1063 wtfs(fsbtodb(&sblock
, node
.dp2
.di_db
[0]), node
.dp2
.di_size
,
1066 iput(&node
, LOSTFOUNDINO
);
1069 * create the root directory
1071 memset(&node
, 0, sizeof(node
));
1074 node
.dp1
.di_mode
= IFDIR
| mfsmode
;
1075 node
.dp1
.di_uid
= mfsuid
;
1076 node
.dp1
.di_gid
= mfsgid
;
1078 node
.dp1
.di_mode
= IFDIR
| UMASK
;
1079 node
.dp1
.di_uid
= geteuid();
1080 node
.dp1
.di_gid
= getegid();
1082 node
.dp1
.di_nlink
= PREDEFDIR
;
1084 node
.dp1
.di_size
= makedir((struct direct
*)oroot_dir
,
1087 node
.dp1
.di_size
= makedir(root_dir
, PREDEFDIR
);
1088 node
.dp1
.di_db
[0] = alloc(sblock
.fs_fsize
, node
.dp1
.di_mode
);
1089 if (node
.dp1
.di_db
[0] == 0)
1091 node
.dp1
.di_blocks
= btodb(fragroundup(&sblock
,
1093 wtfs(fsbtodb(&sblock
, node
.dp1
.di_db
[0]), sblock
.fs_fsize
, buf
);
1096 node
.dp2
.di_mode
= IFDIR
| mfsmode
;
1097 node
.dp2
.di_uid
= mfsuid
;
1098 node
.dp2
.di_gid
= mfsgid
;
1100 node
.dp2
.di_mode
= IFDIR
| UMASK
;
1101 node
.dp2
.di_uid
= geteuid();
1102 node
.dp2
.di_gid
= getegid();
1104 node
.dp2
.di_atime
= tv
->tv_sec
;
1105 node
.dp2
.di_atimensec
= tv
->tv_usec
* 1000;
1106 node
.dp2
.di_mtime
= tv
->tv_sec
;
1107 node
.dp2
.di_mtimensec
= tv
->tv_usec
* 1000;
1108 node
.dp2
.di_ctime
= tv
->tv_sec
;
1109 node
.dp2
.di_ctimensec
= tv
->tv_usec
* 1000;
1110 node
.dp2
.di_birthtime
= tv
->tv_sec
;
1111 node
.dp2
.di_birthnsec
= tv
->tv_usec
* 1000;
1112 node
.dp2
.di_nlink
= PREDEFDIR
;
1113 node
.dp2
.di_size
= makedir(root_dir
, PREDEFDIR
);
1114 node
.dp2
.di_db
[0] = alloc(sblock
.fs_fsize
, node
.dp2
.di_mode
);
1115 if (node
.dp2
.di_db
[0] == 0)
1117 node
.dp2
.di_blocks
= btodb(fragroundup(&sblock
,
1119 wtfs(fsbtodb(&sblock
, node
.dp2
.di_db
[0]), sblock
.fs_fsize
, buf
);
1121 iput(&node
, ROOTINO
);
1126 * construct a set of directory entries in "buf".
1127 * return size of directory.
1130 makedir(struct direct
*protodir
, int entries
)
1134 int dirblksiz
= DIRBLKSIZ
;
1136 dirblksiz
= APPLEUFS_DIRBLKSIZ
;
1138 memset(buf
, 0, DIRBLKSIZ
);
1139 spcleft
= dirblksiz
;
1140 for (cp
= buf
, i
= 0; i
< entries
- 1; i
++) {
1141 protodir
[i
].d_reclen
= DIRSIZ(Oflag
== 0, &protodir
[i
], 0);
1142 copy_dir(&protodir
[i
], (struct direct
*)cp
);
1143 cp
+= protodir
[i
].d_reclen
;
1144 spcleft
-= protodir
[i
].d_reclen
;
1146 protodir
[i
].d_reclen
= spcleft
;
1147 copy_dir(&protodir
[i
], (struct direct
*)cp
);
1152 * allocate a block or frag
1155 alloc(int size
, int mode
)
1160 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
, &acg
);
1161 /* fs -> host byte order */
1163 ffs_cg_swap(&acg
, &acg
, &sblock
);
1164 if (acg
.cg_magic
!= CG_MAGIC
) {
1165 printf("cg 0: bad magic number\n");
1168 if (acg
.cg_cs
.cs_nbfree
== 0) {
1169 printf("first cylinder group ran out of space\n");
1172 for (d
= 0; d
< acg
.cg_ndblk
; d
+= sblock
.fs_frag
)
1173 if (isblock(&sblock
, cg_blksfree(&acg
, 0),
1174 d
>> sblock
.fs_fragshift
))
1176 printf("internal error: can't find block in cyl 0\n");
1179 blkno
= fragstoblks(&sblock
, d
);
1180 clrblock(&sblock
, cg_blksfree(&acg
, 0), blkno
);
1181 if (sblock
.fs_contigsumsize
> 0)
1182 clrbit(cg_clustersfree(&acg
, 0), blkno
);
1183 acg
.cg_cs
.cs_nbfree
--;
1184 sblock
.fs_cstotal
.cs_nbfree
--;
1185 fscs_0
->cs_nbfree
--;
1187 acg
.cg_cs
.cs_ndir
++;
1188 sblock
.fs_cstotal
.cs_ndir
++;
1192 int cn
= old_cbtocylno(&sblock
, d
);
1193 old_cg_blktot(&acg
, 0)[cn
]--;
1194 old_cg_blks(&sblock
, &acg
,
1195 cn
, 0)[old_cbtorpos(&sblock
, d
)]--;
1197 if (size
!= sblock
.fs_bsize
) {
1198 frag
= howmany(size
, sblock
.fs_fsize
);
1199 fscs_0
->cs_nffree
+= sblock
.fs_frag
- frag
;
1200 sblock
.fs_cstotal
.cs_nffree
+= sblock
.fs_frag
- frag
;
1201 acg
.cg_cs
.cs_nffree
+= sblock
.fs_frag
- frag
;
1202 acg
.cg_frsum
[sblock
.fs_frag
- frag
]++;
1203 for (i
= frag
; i
< sblock
.fs_frag
; i
++)
1204 setbit(cg_blksfree(&acg
, 0), d
+ i
);
1206 /* host -> fs byte order */
1208 ffs_cg_swap(&acg
, &acg
, &sblock
);
1209 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
, &acg
);
1214 * Allocate an inode on the disk
1217 iput(union dinode
*ip
, ino_t ino
)
1221 struct ufs1_dinode
*dp1
;
1222 struct ufs2_dinode
*dp2
;
1224 c
= ino_to_cg(&sblock
, ino
);
1225 rdfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
, &acg
);
1226 /* fs -> host byte order */
1228 ffs_cg_swap(&acg
, &acg
, &sblock
);
1229 if (acg
.cg_magic
!= CG_MAGIC
) {
1230 printf("cg 0: bad magic number\n");
1233 acg
.cg_cs
.cs_nifree
--;
1234 setbit(cg_inosused(&acg
, 0), ino
);
1235 /* host -> fs byte order */
1237 ffs_cg_swap(&acg
, &acg
, &sblock
);
1238 wtfs(fsbtodb(&sblock
, cgtod(&sblock
, 0)), sblock
.fs_cgsize
, &acg
);
1239 sblock
.fs_cstotal
.cs_nifree
--;
1240 fscs_0
->cs_nifree
--;
1241 if (ino
>= (ino_t
)(sblock
.fs_ipg
* sblock
.fs_ncg
)) {
1242 printf("fsinit: inode value out of range (%llu).\n",
1243 (unsigned long long)ino
);
1246 d
= fsbtodb(&sblock
, ino_to_fsba(&sblock
, ino
));
1247 rdfs(d
, sblock
.fs_bsize
, (char *)iobuf
);
1248 if (sblock
.fs_magic
== FS_UFS1_MAGIC
) {
1249 dp1
= (struct ufs1_dinode
*)iobuf
;
1250 dp1
+= ino_to_fsbo(&sblock
, ino
);
1252 ffs_dinode1_swap(&ip
->dp1
, dp1
);
1253 /* ffs_dinode1_swap() doesn't swap blocks addrs */
1254 for (i
=0; i
<NDADDR
+ NIADDR
; i
++)
1255 dp1
->di_db
[i
] = bswap32(ip
->dp1
.di_db
[i
]);
1258 dp1
->di_gen
= arc4random() & INT32_MAX
;
1260 dp2
= (struct ufs2_dinode
*)iobuf
;
1261 dp2
+= ino_to_fsbo(&sblock
, ino
);
1263 ffs_dinode2_swap(&ip
->dp2
, dp2
);
1264 for (i
=0; i
<NDADDR
+ NIADDR
; i
++)
1265 dp2
->di_db
[i
] = bswap64(ip
->dp2
.di_db
[i
]);
1268 dp2
->di_gen
= arc4random() & INT32_MAX
;
1270 wtfs(d
, sblock
.fs_bsize
, iobuf
);
1274 * read a block from the file system
1277 rdfs(daddr_t bno
, int size
, void *bf
)
1285 memset(bf
, 0, size
);
1287 memmove(bf
, membase
+ bno
* sectorsize
, size
);
1292 n
= pread(fsi
, bf
, size
, offset
* sectorsize
);
1294 printf("rdfs: read error for sector %lld: %s\n",
1295 (long long)bno
, strerror(errno
));
1301 * write a block to the file system
1304 wtfs(daddr_t bno
, int size
, void *bf
)
1313 memmove(membase
+ bno
* sectorsize
, bf
, size
);
1318 n
= pwrite(fso
, bf
, size
, offset
* sectorsize
);
1320 printf("wtfs: write error for sector %lld: %s\n",
1321 (long long)bno
, strerror(errno
));
1327 * check if a block is available
1330 isblock(struct fs
*fs
, unsigned char *cp
, int h
)
1334 switch (fs
->fs_fragshift
) {
1336 return (cp
[h
] == 0xff);
1338 mask
= 0x0f << ((h
& 0x1) << 2);
1339 return ((cp
[h
>> 1] & mask
) == mask
);
1341 mask
= 0x03 << ((h
& 0x3) << 1);
1342 return ((cp
[h
>> 2] & mask
) == mask
);
1344 mask
= 0x01 << (h
& 0x7);
1345 return ((cp
[h
>> 3] & mask
) == mask
);
1348 printf("isblock bad fs_fragshift %d\n", fs
->fs_fragshift
);
1350 fprintf(stderr
, "isblock bad fs_fragshift %d\n",
1358 * take a block out of the map
1361 clrblock(struct fs
*fs
, unsigned char *cp
, int h
)
1363 switch ((fs
)->fs_fragshift
) {
1368 cp
[h
>> 1] &= ~(0x0f << ((h
& 0x1) << 2));
1371 cp
[h
>> 2] &= ~(0x03 << ((h
& 0x3) << 1));
1374 cp
[h
>> 3] &= ~(0x01 << (h
& 0x7));
1378 printf("clrblock bad fs_fragshift %d\n", fs
->fs_fragshift
);
1380 fprintf(stderr
, "clrblock bad fs_fragshift %d\n",
1388 * put a block into the map
1391 setblock(struct fs
*fs
, unsigned char *cp
, int h
)
1393 switch (fs
->fs_fragshift
) {
1398 cp
[h
>> 1] |= (0x0f << ((h
& 0x1) << 2));
1401 cp
[h
>> 2] |= (0x03 << ((h
& 0x3) << 1));
1404 cp
[h
>> 3] |= (0x01 << (h
& 0x7));
1408 printf("setblock bad fs_frag %d\n", fs
->fs_fragshift
);
1410 fprintf(stderr
, "setblock bad fs_fragshift %d\n",
1417 /* copy a direntry to a buffer, in fs byte order */
1419 copy_dir(struct direct
*dir
, struct direct
*dbuf
)
1421 memcpy(dbuf
, dir
, DIRSIZ(Oflag
== 0, dir
, 0));
1423 dbuf
->d_ino
= bswap32(dir
->d_ino
);
1424 dbuf
->d_reclen
= bswap16(dir
->d_reclen
);
1426 ((struct odirect
*)dbuf
)->d_namlen
=
1427 bswap16(((struct odirect
*)dir
)->d_namlen
);
1436 for (n
= 0; n
< sizeof(n
) * CHAR_BIT
; n
++)
1439 errx(1, "ilog2: %d is not a power of 2\n", val
);
1443 zap_old_sblock(int sblkoff
)
1445 static int cg0_data
;
1446 uint32_t oldfs
[SBLOCKSIZE
/ 4];
1447 static const struct fsm
{
1452 {offsetof(struct fs
, fs_magic
)/4, FS_UFS1_MAGIC
, ~0u},
1453 {offsetof(struct fs
, fs_magic
)/4, FS_UFS2_MAGIC
, ~0u},
1454 {0, 0x70162, ~0u}, /* LFS_MAGIC */
1455 {14, 0xef53, 0xffff}, /* EXT2FS (little) */
1456 {14, 0xef530000, 0xffff0000}, /* EXT2FS (big) */
1459 const struct fsm
*fsm
;
1464 if (sblkoff
== 0) /* Why did UFS2 add support for this? sigh. */
1468 /* For FFSv1 this could include all the inodes. */
1469 cg0_data
= cgsblock(&sblock
, 0) * sblock
.fs_fsize
+ iobufsize
;
1471 /* Ignore anything that is beyond our filesystem */
1472 if ((sblkoff
+ SBLOCKSIZE
)/sectorsize
>= fssize
)
1474 /* Zero anything inside our filesystem... */
1475 if (sblkoff
>= sblock
.fs_sblockloc
) {
1476 /* ...unless we will write that area anyway */
1477 if (sblkoff
>= cg0_data
)
1478 wtfs(sblkoff
/ sectorsize
,
1479 roundup(sizeof sblock
, sectorsize
), iobuf
);
1483 /* The sector might contain boot code, so we must validate it */
1484 rdfs(sblkoff
/sectorsize
, sizeof oldfs
, &oldfs
);
1485 for (fsm
= fs_magics
; ; fsm
++) {
1489 v
= oldfs
[fsm
->offset
];
1490 if ((v
& fsm
->mask
) == fsm
->magic
||
1491 (bswap32(v
) & fsm
->mask
) == fsm
->magic
)
1495 /* Just zap the magic number */
1496 oldfs
[fsm
->offset
] = 0;
1497 wtfs(sblkoff
/sectorsize
, sizeof oldfs
, &oldfs
);
1504 * Attempt to guess how much more space is available for process data. The
1505 * heuristic we use is
1507 * max_data_limit - (sbrk(0) - etext) - 128kB
1509 * etext approximates that start address of the data segment, and the 128kB
1510 * allows some slop for both segment gap between text and data, and for other
1511 * (libc) malloc usage.
1520 base
= (u_long
)sbrk(0) - (u_long
)&etext
;
1521 if (getrlimit(RLIMIT_DATA
, &rlp
) < 0)
1522 perror("getrlimit");
1523 rlp
.rlim_cur
= rlp
.rlim_max
;
1524 if (setrlimit(RLIMIT_DATA
, &rlp
) < 0)
1525 perror("setrlimit");
1526 memleft
= rlp
.rlim_max
- base
- (128 * 1024);
1530 * Internal version of malloc that trims the requested size if not enough
1531 * memory is available.
1534 mkfs_malloc(size_t size
)
1544 pgsz
= getpagesize() - 1;
1545 size
= (size
+ pgsz
) &~ pgsz
;
1549 memory
= mmap(0, size
, PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_PRIVATE
,
1551 return memory
!= MAP_FAILED
? memory
: NULL
;