mkfs.c revision 1.72 1 /* $NetBSD: mkfs.c,v 1.72 2003/08/15 15:07:16 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
35 *
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
40 * research program
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
57 *
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
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 #ifndef lint
73 #if 0
74 static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
75 #else
76 __RCSID("$NetBSD: mkfs.c,v 1.72 2003/08/15 15:07:16 dsl Exp $");
77 #endif
78 #endif /* not lint */
79
80 #include <sys/param.h>
81 #include <sys/mman.h>
82 #include <sys/time.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/disklabel.h>
90
91 #include <err.h>
92 #include <errno.h>
93 #include <string.h>
94 #include <unistd.h>
95 #include <stdlib.h>
96
97 #ifndef STANDALONE
98 #include <stdio.h>
99 #endif
100
101 #include "extern.h"
102
103 union dinode {
104 struct ufs1_dinode dp1;
105 struct ufs2_dinode dp2;
106 };
107
108 static void initcg(int, const struct timeval *);
109 static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
110 static int makedir(struct direct *, int);
111 static daddr_t alloc(int, int);
112 static void iput(union dinode *, ino_t);
113 static void rdfs(daddr_t, int, void *);
114 static void wtfs(daddr_t, int, void *);
115 static int isblock(struct fs *, unsigned char *, int);
116 static void clrblock(struct fs *, unsigned char *, int);
117 static void setblock(struct fs *, unsigned char *, int);
118 static int ilog2(int);
119 #ifdef MFS
120 static void calc_memfree(void);
121 static void *mkfs_malloc(size_t size);
122 #endif
123
124 static int count_digits(uint64_t);
125
126 /*
127 * make file system for cylinder-group style file systems
128 */
129 #define UMASK 0755
130 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
131
132 union {
133 struct fs fs;
134 char pad[SBLOCKSIZE];
135 } fsun;
136 #define sblock fsun.fs
137 struct csum *fscs;
138
139 union {
140 struct cg cg;
141 char pad[MAXBSIZE];
142 } cgun;
143 #define acg cgun.cg
144
145 #define DIP(dp, field) \
146 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
147 (dp)->dp1.di_##field : (dp)->dp2.di_##field)
148
149 char *iobuf;
150 int iobufsize;
151
152 char writebuf[MAXBSIZE];
153
154 int fsi, fso;
155
156 void
157 mkfs(struct partition *pp, const char *fsys, int fi, int fo,
158 mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
159 {
160 int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg;
161 int32_t cylno, i, csfrags;
162 struct timeval tv;
163 long long sizepb;
164 char *writebuf2; /* dynamic buffer */
165 int nprintcols, printcolwidth;
166
167 #ifndef STANDALONE
168 gettimeofday(&tv, NULL);
169 #endif
170 #ifdef MFS
171 if (mfs) {
172 calc_memfree();
173 if (fssize * sectorsize > memleft)
174 fssize = memleft / sectorsize;
175 if ((membase = mkfs_malloc(fssize * sectorsize)) == 0)
176 exit(12);
177 }
178 #endif
179 fsi = fi;
180 fso = fo;
181 if (Oflag == 0) {
182 sblock.fs_old_inodefmt = FS_42INODEFMT;
183 sblock.fs_maxsymlinklen = 0;
184 sblock.fs_old_flags = 0;
185 } else {
186 sblock.fs_old_inodefmt = FS_44INODEFMT;
187 sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
188 MAXSYMLINKLEN_UFS2);
189 sblock.fs_old_flags = FS_FLAGS_UPDATED;
190 sblock.fs_flags = 0;
191 }
192 /*
193 * Validate the given file system size.
194 * Verify that its last block can actually be accessed.
195 * Convert to file system fragment sized units.
196 */
197 if (fssize <= 0) {
198 printf("preposterous size %lld\n", (long long)fssize);
199 exit(13);
200 }
201 wtfs(fssize - 1, sectorsize, &sblock);
202
203 if (isappleufs) {
204 struct appleufslabel appleufs;
205 ffs_appleufs_set(&appleufs,appleufs_volname,tv.tv_sec);
206 wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,APPLEUFS_LABEL_SIZE,&appleufs);
207 }
208
209 /*
210 * collect and verify the filesystem density info
211 */
212 sblock.fs_avgfilesize = avgfilesize;
213 sblock.fs_avgfpdir = avgfpdir;
214 if (sblock.fs_avgfilesize <= 0) {
215 printf("illegal expected average file size %d\n",
216 sblock.fs_avgfilesize);
217 exit(14);
218 }
219 if (sblock.fs_avgfpdir <= 0) {
220 printf("illegal expected number of files per directory %d\n",
221 sblock.fs_avgfpdir);
222 exit(15);
223 }
224 /*
225 * collect and verify the block and fragment sizes
226 */
227 sblock.fs_bsize = bsize;
228 sblock.fs_fsize = fsize;
229 if (!POWEROF2(sblock.fs_bsize)) {
230 printf("block size must be a power of 2, not %d\n",
231 sblock.fs_bsize);
232 exit(16);
233 }
234 if (!POWEROF2(sblock.fs_fsize)) {
235 printf("fragment size must be a power of 2, not %d\n",
236 sblock.fs_fsize);
237 exit(17);
238 }
239 if (sblock.fs_fsize < sectorsize) {
240 printf("fragment size %d is too small, minimum is %d\n",
241 sblock.fs_fsize, sectorsize);
242 exit(18);
243 }
244 if (sblock.fs_bsize < MINBSIZE) {
245 printf("block size %d is too small, minimum is %d\n",
246 sblock.fs_bsize, MINBSIZE);
247 exit(19);
248 }
249 if (sblock.fs_bsize > MAXBSIZE) {
250 printf("block size %d is too large, maximum is %d\n",
251 sblock.fs_bsize, MAXBSIZE);
252 exit(19);
253 }
254 if (sblock.fs_bsize < sblock.fs_fsize) {
255 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
256 sblock.fs_bsize, sblock.fs_fsize);
257 exit(20);
258 }
259
260 if (maxbsize < bsize || !POWEROF2(maxbsize)) {
261 sblock.fs_maxbsize = sblock.fs_bsize;
262 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
263 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
264 } else {
265 sblock.fs_maxbsize = maxbsize;
266 }
267 sblock.fs_maxcontig = maxcontig;
268 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
269 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
270 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
271 }
272 if (sblock.fs_maxcontig > 1)
273 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
274
275 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
276 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
277 sblock.fs_qbmask = ~sblock.fs_bmask;
278 sblock.fs_qfmask = ~sblock.fs_fmask;
279 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
280 sblock.fs_bshift++;
281 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
282 sblock.fs_fshift++;
283 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
284 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
285 sblock.fs_fragshift++;
286 if (sblock.fs_frag > MAXFRAG) {
287 printf("fragment size %d is too small, "
288 "minimum with block size %d is %d\n",
289 sblock.fs_fsize, sblock.fs_bsize,
290 sblock.fs_bsize / MAXFRAG);
291 exit(21);
292 }
293 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
294 sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
295 if (Oflag <= 1) {
296 if (sblock.fs_size >= 1ull << 31) {
297 printf("Too many fragments (0x%" PRIx64
298 ") for a UFS1 filesystem\n", sblock.fs_size);
299 exit(22);
300 }
301 sblock.fs_magic = FS_UFS1_MAGIC;
302 sblock.fs_sblockloc = SBLOCK_UFS1;
303 sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
304 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
305 sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
306 sizeof (int32_t));
307 sblock.fs_old_inodefmt = FS_44INODEFMT;
308 sblock.fs_old_cgoffset = 0;
309 sblock.fs_old_cgmask = 0xffffffff;
310 sblock.fs_old_size = sblock.fs_size;
311 sblock.fs_old_rotdelay = 0;
312 sblock.fs_old_rps = 60;
313 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
314 sblock.fs_old_cpg = 1;
315 sblock.fs_old_interleave = 1;
316 sblock.fs_old_trackskew = 0;
317 sblock.fs_old_cpc = 0;
318 sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
319 sblock.fs_old_nrpos = 1;
320 } else {
321 sblock.fs_magic = FS_UFS2_MAGIC;
322 sblock.fs_sblockloc = SBLOCK_UFS2;
323 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
324 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
325 sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
326 sizeof (int64_t));
327 }
328
329 sblock.fs_sblkno =
330 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
331 sblock.fs_frag);
332 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
333 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
334 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
335 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
336 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
337 sizepb *= NINDIR(&sblock);
338 sblock.fs_maxfilesize += sizepb;
339 }
340
341 /*
342 * Calculate the number of blocks to put into each cylinder group.
343 *
344 * This algorithm selects the number of blocks per cylinder
345 * group. The first goal is to have at least enough data blocks
346 * in each cylinder group to meet the density requirement. Once
347 * this goal is achieved we try to expand to have at least
348 * MINCYLGRPS cylinder groups. Once this goal is achieved, we
349 * pack as many blocks into each cylinder group map as will fit.
350 *
351 * We start by calculating the smallest number of blocks that we
352 * can put into each cylinder group. If this is too big, we reduce
353 * the density until it fits.
354 */
355 origdensity = density;
356 for (;;) {
357 fragsperinode = MAX(numfrags(&sblock, density), 1);
358 minfpg = fragsperinode * INOPB(&sblock);
359 if (minfpg > sblock.fs_size)
360 minfpg = sblock.fs_size;
361 sblock.fs_ipg = INOPB(&sblock);
362 sblock.fs_fpg = roundup(sblock.fs_iblkno +
363 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
364 if (sblock.fs_fpg < minfpg)
365 sblock.fs_fpg = minfpg;
366 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
367 INOPB(&sblock));
368 sblock.fs_fpg = roundup(sblock.fs_iblkno +
369 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
370 if (sblock.fs_fpg < minfpg)
371 sblock.fs_fpg = minfpg;
372 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
373 INOPB(&sblock));
374 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
375 break;
376 density -= sblock.fs_fsize;
377 }
378 if (density != origdensity)
379 printf("density reduced from %d to %d\n", origdensity, density);
380 /*
381 * Start packing more blocks into the cylinder group until
382 * it cannot grow any larger, the number of cylinder groups
383 * drops below MINCYLGRPS, or we reach the size requested.
384 */
385 for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) {
386 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
387 INOPB(&sblock));
388 if (sblock.fs_size / sblock.fs_fpg < MINCYLGRPS)
389 break;
390 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
391 continue;
392 if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize)
393 break;
394 sblock.fs_fpg -= sblock.fs_frag;
395 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
396 INOPB(&sblock));
397 break;
398 }
399 /*
400 * Check to be sure that the last cylinder group has enough blocks
401 * to be viable. If it is too small, reduce the number of blocks
402 * per cylinder group which will have the effect of moving more
403 * blocks into the last cylinder group.
404 */
405 optimalfpg = sblock.fs_fpg;
406 for (;;) {
407 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
408 lastminfpg = roundup(sblock.fs_iblkno +
409 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
410 if (sblock.fs_size < lastminfpg) {
411 printf("Filesystem size %lld < minimum size of %d\n",
412 (long long)sblock.fs_size, lastminfpg);
413 exit(28);
414 }
415 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg ||
416 sblock.fs_size % sblock.fs_fpg == 0)
417 break;
418 sblock.fs_fpg -= sblock.fs_frag;
419 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
420 INOPB(&sblock));
421 }
422 if (optimalfpg != sblock.fs_fpg)
423 printf("Reduced frags per cylinder group from %d to %d %s\n",
424 optimalfpg, sblock.fs_fpg, "to enlarge last cyl group");
425 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
426 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
427 if (Oflag <= 1) {
428 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
429 sblock.fs_old_nsect = sblock.fs_old_spc;
430 sblock.fs_old_npsect = sblock.fs_old_spc;
431 sblock.fs_old_ncyl = sblock.fs_ncg;
432 }
433
434 /*
435 * fill in remaining fields of the super block
436 */
437 sblock.fs_csaddr = cgdmin(&sblock, 0);
438 sblock.fs_cssize =
439 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
440 fscs = (struct csum *)calloc(1, sblock.fs_cssize);
441 if (fscs == NULL)
442 exit(39);
443 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
444 if (sblock.fs_sbsize > SBLOCKSIZE)
445 sblock.fs_sbsize = SBLOCKSIZE;
446 sblock.fs_minfree = minfree;
447 sblock.fs_maxcontig = maxcontig;
448 sblock.fs_maxbpg = maxbpg;
449 sblock.fs_optim = opt;
450 sblock.fs_cgrotor = 0;
451 sblock.fs_pendingblocks = 0;
452 sblock.fs_pendinginodes = 0;
453 sblock.fs_cstotal.cs_ndir = 0;
454 sblock.fs_cstotal.cs_nbfree = 0;
455 sblock.fs_cstotal.cs_nifree = 0;
456 sblock.fs_cstotal.cs_nffree = 0;
457 sblock.fs_fmod = 0;
458 sblock.fs_ronly = 0;
459 sblock.fs_state = 0;
460 sblock.fs_clean = FS_ISCLEAN;
461 sblock.fs_ronly = 0;
462 sblock.fs_id[0] = (long)tv.tv_sec; /* XXXfvdl huh? */
463 sblock.fs_id[1] = random();
464 sblock.fs_fsmnt[0] = '\0';
465 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
466 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
467 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
468 sblock.fs_cstotal.cs_nbfree =
469 fragstoblks(&sblock, sblock.fs_dsize) -
470 howmany(csfrags, sblock.fs_frag);
471 sblock.fs_cstotal.cs_nffree =
472 fragnum(&sblock, sblock.fs_size) +
473 (fragnum(&sblock, csfrags) > 0 ?
474 sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
475 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
476 sblock.fs_cstotal.cs_ndir = 0;
477 sblock.fs_dsize -= csfrags;
478 sblock.fs_time = tv.tv_sec;
479 if (Oflag <= 1) {
480 sblock.fs_old_time = tv.tv_sec;
481 sblock.fs_old_dsize = sblock.fs_dsize;
482 sblock.fs_old_csaddr = sblock.fs_csaddr;
483 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
484 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
485 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
486 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
487 }
488 /*
489 * Dump out summary information about file system.
490 */
491 if (!mfs) {
492 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
493 printf("%s: %.1fMB (%lld sectors) block size %d, "
494 "fragment size %d\n",
495 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
496 (long long)fsbtodb(&sblock, sblock.fs_size),
497 sblock.fs_bsize, sblock.fs_fsize);
498 printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
499 "%d inodes.\n",
500 sblock.fs_ncg,
501 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
502 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
503 #undef B2MBFACTOR
504 }
505 /*
506 * Now determine how wide each column will be, and calculate how
507 * many columns will fit in a 80 char line.
508 */
509 printcolwidth = count_digits(
510 fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
511 nprintcols = 80 / (printcolwidth + 2);
512
513 /*
514 * allocate space for superblock, cylinder group map, and
515 * two sets of inode blocks.
516 */
517 if (sblock.fs_bsize < SBLOCKSIZE)
518 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
519 else
520 iobufsize = 4 * sblock.fs_bsize;
521 if ((iobuf = malloc(iobufsize)) == 0) {
522 printf("Cannot allocate I/O buffer\n");
523 exit(38);
524 }
525 memset(iobuf, 0, iobufsize);
526 /*
527 * Make a copy of the superblock into the buffer that we will be
528 * writing out in each cylinder group.
529 */
530 memcpy(writebuf, &sblock, sbsize);
531 if (needswap)
532 ffs_sb_swap(&sblock, (struct fs*)writebuf);
533 memcpy(iobuf, writebuf, SBLOCKSIZE);
534
535 if (!mfs)
536 printf("super-block backups (for fsck -b #) at:");
537 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
538 initcg(cylno, &tv);
539 if (mfs)
540 continue;
541 if (cylno % nprintcols == 0)
542 printf("\n");
543 printf(" %*lld,", printcolwidth,
544 (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
545 fflush(stdout);
546 }
547 if (!mfs)
548 printf("\n");
549 if (Nflag && !mfs)
550 exit(0);
551
552 /*
553 * Now construct the initial file system,
554 * then write out the super-block.
555 */
556 if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
557 errx(1, "Error making filesystem");
558 sblock.fs_time = tv.tv_sec;
559 if (Oflag <= 1) {
560 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
561 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
562 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
563 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
564 }
565 memcpy(writebuf, &sblock, sbsize);
566 if (needswap)
567 ffs_sb_swap(&sblock, (struct fs*)writebuf);
568 wtfs(sblock.fs_sblockloc / sectorsize, sbsize, writebuf);
569
570 /*
571 * if we need to swap, create a buffer for the cylinder summaries
572 * to get swapped to.
573 */
574 if (needswap) {
575 if ((writebuf2 = malloc(sblock.fs_cssize)) == NULL)
576 exit(12);
577 ffs_csum_swap(fscs, (struct csum*)writebuf2, sblock.fs_cssize);
578 } else
579 writebuf2 = (char *)fscs;
580
581 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize)
582 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)),
583 sblock.fs_cssize - i < sblock.fs_bsize ?
584 sblock.fs_cssize - i : sblock.fs_bsize,
585 ((char *)writebuf2) + i);
586 if (writebuf2 != (char *)fscs)
587 free(writebuf2);
588
589 /*
590 * Update information about this partion in pack
591 * label, to that it may be updated on disk.
592 */
593 if (isappleufs)
594 pp->p_fstype = FS_APPLEUFS;
595 else
596 pp->p_fstype = FS_BSDFFS;
597 pp->p_fsize = sblock.fs_fsize;
598 pp->p_frag = sblock.fs_frag;
599 pp->p_cpg = sblock.fs_fpg;
600 }
601
602 /*
603 * Initialize a cylinder group.
604 */
605 void
606 initcg(int cylno, const struct timeval *tv)
607 {
608 daddr_t cbase, dmax;
609 int32_t i, j, d, dlower, dupper, blkno;
610 struct csum *cs;
611 struct ufs1_dinode *dp1;
612 struct ufs2_dinode *dp2;
613 int start;
614
615 /*
616 * Determine block bounds for cylinder group.
617 * Allow space for super block summary information in first
618 * cylinder group.
619 */
620 cbase = cgbase(&sblock, cylno);
621 dmax = cbase + sblock.fs_fpg;
622 if (dmax > sblock.fs_size)
623 dmax = sblock.fs_size;
624 dlower = cgsblock(&sblock, cylno) - cbase;
625 dupper = cgdmin(&sblock, cylno) - cbase;
626 if (cylno == 0) {
627 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
628 if (dupper >= cgstart(&sblock, cylno + 1)) {
629 printf("\rToo many cylinder groups to fit summary "
630 "information into first cylinder group\n");
631 exit(40);
632 }
633 }
634 cs = fscs + cylno;
635 memset(&acg, 0, sblock.fs_cgsize);
636 acg.cg_time = tv->tv_sec;
637 acg.cg_magic = CG_MAGIC;
638 acg.cg_cgx = cylno;
639 acg.cg_niblk = sblock.fs_ipg;
640 acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
641 sblock.fs_ipg : 2 * INOPB(&sblock);
642 acg.cg_ndblk = dmax - cbase;
643 if (sblock.fs_contigsumsize > 0)
644 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
645 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
646 if (Oflag == 2) {
647 acg.cg_iusedoff = start;
648 } else {
649 acg.cg_old_ncyl = sblock.fs_old_cpg;
650 acg.cg_old_time = acg.cg_time;
651 acg.cg_time = 0;
652 acg.cg_old_niblk = acg.cg_niblk;
653 acg.cg_niblk = 0;
654 acg.cg_initediblk = 0;
655 acg.cg_old_btotoff = start;
656 acg.cg_old_boff = acg.cg_old_btotoff +
657 sblock.fs_old_cpg * sizeof(int32_t);
658 acg.cg_iusedoff = acg.cg_old_boff +
659 sblock.fs_old_cpg * sizeof(u_int16_t);
660 }
661 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
662 if (sblock.fs_contigsumsize <= 0) {
663 acg.cg_nextfreeoff = acg.cg_freeoff +
664 howmany(sblock.fs_fpg, CHAR_BIT);
665 } else {
666 acg.cg_clustersumoff = acg.cg_freeoff +
667 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
668 if (isappleufs) {
669 /* Apple PR2216969 gives rationale for this change.
670 * I believe they were mistaken, but we need to
671 * duplicate it for compatibility. -- dbj (at) NetBSD.org
672 */
673 acg.cg_clustersumoff += sizeof(int32_t);
674 }
675 acg.cg_clustersumoff =
676 roundup(acg.cg_clustersumoff, sizeof(int32_t));
677 acg.cg_clusteroff = acg.cg_clustersumoff +
678 (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
679 acg.cg_nextfreeoff = acg.cg_clusteroff +
680 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
681 }
682 if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
683 printf("Panic: cylinder group too big\n");
684 exit(37);
685 }
686 acg.cg_cs.cs_nifree += sblock.fs_ipg;
687 if (cylno == 0)
688 for (i = 0; i < ROOTINO; i++) {
689 setbit(cg_inosused(&acg, 0), i);
690 acg.cg_cs.cs_nifree--;
691 }
692 if (cylno > 0) {
693 /*
694 * In cylno 0, beginning space is reserved
695 * for boot and super blocks.
696 */
697 for (d = 0, blkno = 0; d < dlower;) {
698 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
699 if (sblock.fs_contigsumsize > 0)
700 setbit(cg_clustersfree(&acg, 0), blkno);
701 acg.cg_cs.cs_nbfree++;
702 d += sblock.fs_frag;
703 blkno++;
704 }
705 }
706 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
707 acg.cg_frsum[sblock.fs_frag - i]++;
708 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
709 setbit(cg_blksfree(&acg, 0), dupper);
710 acg.cg_cs.cs_nffree++;
711 }
712 }
713 for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
714 d + sblock.fs_frag <= acg.cg_ndblk; ) {
715 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
716 if (sblock.fs_contigsumsize > 0)
717 setbit(cg_clustersfree(&acg, 0), blkno);
718 acg.cg_cs.cs_nbfree++;
719 d += sblock.fs_frag;
720 blkno++;
721 }
722 if (d < acg.cg_ndblk) {
723 acg.cg_frsum[acg.cg_ndblk - d]++;
724 for (; d < acg.cg_ndblk; d++) {
725 setbit(cg_blksfree(&acg, 0), d);
726 acg.cg_cs.cs_nffree++;
727 }
728 }
729 if (sblock.fs_contigsumsize > 0) {
730 int32_t *sump = cg_clustersum(&acg, 0);
731 u_char *mapp = cg_clustersfree(&acg, 0);
732 int map = *mapp++;
733 int bit = 1;
734 int run = 0;
735
736 for (i = 0; i < acg.cg_nclusterblks; i++) {
737 if ((map & bit) != 0) {
738 run++;
739 } else if (run != 0) {
740 if (run > sblock.fs_contigsumsize)
741 run = sblock.fs_contigsumsize;
742 sump[run]++;
743 run = 0;
744 }
745 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
746 bit <<= 1;
747 } else {
748 map = *mapp++;
749 bit = 1;
750 }
751 }
752 if (run != 0) {
753 if (run > sblock.fs_contigsumsize)
754 run = sblock.fs_contigsumsize;
755 sump[run]++;
756 }
757 }
758 *cs = acg.cg_cs;
759 /*
760 * Write out the duplicate super block, the cylinder group map
761 * and two blocks worth of inodes in a single write.
762 */
763 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
764 memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
765 if (needswap)
766 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
767 start += sblock.fs_bsize;
768 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
769 dp2 = (struct ufs2_dinode *)(&iobuf[start]);
770 for (i = 0; i < acg.cg_initediblk; i++) {
771 if (sblock.fs_magic == FS_UFS1_MAGIC) {
772 /* No need to swap, it'll stay random */
773 dp1->di_gen = random();
774 dp1++;
775 } else {
776 dp2->di_gen = random();
777 dp2++;
778 }
779 }
780 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
781 /*
782 * For the old file system, we have to initialize all the inodes.
783 */
784 if (Oflag <= 1) {
785 for (i = 2 * sblock.fs_frag;
786 i < sblock.fs_ipg / INOPF(&sblock);
787 i += sblock.fs_frag) {
788 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
789 for (j = 0; j < INOPB(&sblock); j++) {
790 dp1->di_gen = random();
791 dp1++;
792 }
793 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
794 sblock.fs_bsize, &iobuf[start]);
795 }
796 }
797 }
798
799 /*
800 * initialize the file system
801 */
802 union dinode node;
803
804 #ifdef LOSTDIR
805 #define PREDEFDIR 3
806 #else
807 #define PREDEFDIR 2
808 #endif
809
810 struct direct root_dir[] = {
811 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
812 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
813 #ifdef LOSTDIR
814 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
815 #endif
816 };
817 struct odirect {
818 u_int32_t d_ino;
819 u_int16_t d_reclen;
820 u_int16_t d_namlen;
821 u_char d_name[MAXNAMLEN + 1];
822 } oroot_dir[] = {
823 { ROOTINO, sizeof(struct direct), 1, "." },
824 { ROOTINO, sizeof(struct direct), 2, ".." },
825 #ifdef LOSTDIR
826 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
827 #endif
828 };
829 #ifdef LOSTDIR
830 struct direct lost_found_dir[] = {
831 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
832 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
833 { 0, DIRBLKSIZ, 0, 0, 0 },
834 };
835 struct odirect olost_found_dir[] = {
836 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
837 { ROOTINO, sizeof(struct direct), 2, ".." },
838 { 0, DIRBLKSIZ, 0, 0 },
839 };
840 #endif
841 char buf[MAXBSIZE];
842 static void copy_dir(struct direct *, struct direct *);
843
844 int
845 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
846 {
847 #ifdef LOSTDIR
848 int i;
849 int dirblksiz = DIRBLKSIZ;
850 if (isappleufs)
851 dirblksiz = APPLEUFS_DIRBLKSIZ;
852 #endif
853
854 /*
855 * initialize the node
856 */
857 memset(&node, 0, sizeof(node));
858
859 #ifdef LOSTDIR
860 /*
861 * create the lost+found directory
862 */
863 if (Oflag == 0) {
864 (void)makedir((struct direct *)olost_found_dir, 2);
865 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
866 copy_dir((struct direct*)&olost_found_dir[2],
867 (struct direct*)&buf[i]);
868 } else {
869 (void)makedir(lost_found_dir, 2);
870 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
871 copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
872 }
873 if (sblock.fs_magic == FS_UFS1_MAGIC) {
874 node.dp1.di_atime = tv->tv_sec;
875 node.dp1.di_atimensec = tv->tv_usec * 1000;
876 node.dp1.di_mtime = tv->tv_sec;
877 node.dp1.di_mtimensec = tv->tv_usec * 1000;
878 node.dp1.di_ctime = tv->tv_sec;
879 node.dp1.di_ctimensec = tv->tv_usec * 1000;
880 node.dp1.di_mode = IFDIR | UMASK;
881 node.dp1.di_nlink = 2;
882 node.dp1.di_size = sblock.fs_bsize;
883 node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
884 if (node.dp1.di_db[0] == 0)
885 return (0);
886 node.dp1.di_blocks = btodb(fragroundup(&sblock,
887 node.dp1.di_size));
888 node.dp1.di_uid = geteuid();
889 node.dp1.di_gid = getegid();
890 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
891 buf);
892 } else {
893 node.dp2.di_atime = tv->tv_sec;
894 node.dp2.di_atimensec = tv->tv_usec * 1000;
895 node.dp2.di_mtime = tv->tv_sec;
896 node.dp2.di_mtimensec = tv->tv_usec * 1000;
897 node.dp2.di_ctime = tv->tv_sec;
898 node.dp2.di_ctimensec = tv->tv_usec * 1000;
899 node.dp2.di_birthtime = tv->tv_sec;
900 node.dp2.di_birthnsec = tv->tv_usec * 1000;
901 node.dp2.di_mode = IFDIR | UMASK;
902 node.dp2.di_nlink = 2;
903 node.dp2.di_size = sblock.fs_bsize;
904 node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
905 if (node.dp2.di_db[0] == 0)
906 return (0);
907 node.dp2.di_blocks = btodb(fragroundup(&sblock,
908 node.dp2.di_size));
909 node.dp2.di_uid = geteuid();
910 node.dp2.di_gid = getegid();
911 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
912 buf);
913 }
914 iput(&node, LOSTFOUNDINO);
915 #endif
916 /*
917 * create the root directory
918 */
919 if (Oflag <= 1) {
920 if (mfs) {
921 node.dp1.di_mode = IFDIR | mfsmode;
922 node.dp1.di_uid = mfsuid;
923 node.dp1.di_gid = mfsgid;
924 } else {
925 node.dp1.di_mode = IFDIR | UMASK;
926 node.dp1.di_uid = geteuid();
927 node.dp1.di_gid = getegid();
928 }
929 node.dp1.di_nlink = PREDEFDIR;
930 if (Oflag == 0)
931 node.dp1.di_size = makedir((struct direct *)oroot_dir,
932 PREDEFDIR);
933 else
934 node.dp1.di_size = makedir(root_dir, PREDEFDIR);
935 node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
936 if (node.dp1.di_db[0] == 0)
937 return (0);
938 node.dp1.di_blocks = btodb(fragroundup(&sblock,
939 node.dp1.di_size));
940 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
941 } else {
942 if (mfs) {
943 node.dp2.di_mode = IFDIR | mfsmode;
944 node.dp2.di_uid = mfsuid;
945 node.dp2.di_gid = mfsgid;
946 } else {
947 node.dp2.di_mode = IFDIR | UMASK;
948 node.dp2.di_uid = geteuid();
949 node.dp2.di_gid = getegid();
950 }
951 node.dp2.di_atime = tv->tv_sec;
952 node.dp2.di_atimensec = tv->tv_usec * 1000;
953 node.dp2.di_mtime = tv->tv_sec;
954 node.dp2.di_mtimensec = tv->tv_usec * 1000;
955 node.dp2.di_ctime = tv->tv_sec;
956 node.dp2.di_ctimensec = tv->tv_usec * 1000;
957 node.dp2.di_birthtime = tv->tv_sec;
958 node.dp2.di_birthnsec = tv->tv_usec * 1000;
959 node.dp2.di_nlink = PREDEFDIR;
960 node.dp2.di_size = makedir(root_dir, PREDEFDIR);
961 node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
962 if (node.dp2.di_db[0] == 0)
963 return (0);
964 node.dp2.di_blocks = btodb(fragroundup(&sblock,
965 node.dp2.di_size));
966 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
967 }
968 iput(&node, ROOTINO);
969 return (1);
970 }
971
972 /*
973 * construct a set of directory entries in "buf".
974 * return size of directory.
975 */
976 int
977 makedir(struct direct *protodir, int entries)
978 {
979 char *cp;
980 int i, spcleft;
981 int dirblksiz = DIRBLKSIZ;
982 if (isappleufs)
983 dirblksiz = APPLEUFS_DIRBLKSIZ;
984
985 memset(buf, 0, DIRBLKSIZ);
986 spcleft = dirblksiz;
987 for (cp = buf, i = 0; i < entries - 1; i++) {
988 protodir[i].d_reclen = DIRSIZ(Oflag == 0, &protodir[i], 0);
989 copy_dir(&protodir[i], (struct direct*)cp);
990 cp += protodir[i].d_reclen;
991 spcleft -= protodir[i].d_reclen;
992 }
993 protodir[i].d_reclen = spcleft;
994 copy_dir(&protodir[i], (struct direct*)cp);
995 return (dirblksiz);
996 }
997
998 /*
999 * allocate a block or frag
1000 */
1001 daddr_t
1002 alloc(int size, int mode)
1003 {
1004 int i, frag;
1005 daddr_t d, blkno;
1006
1007 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1008 /* fs -> host byte order */
1009 if (needswap)
1010 ffs_cg_swap(&acg, &acg, &sblock);
1011 if (acg.cg_magic != CG_MAGIC) {
1012 printf("cg 0: bad magic number\n");
1013 return (0);
1014 }
1015 if (acg.cg_cs.cs_nbfree == 0) {
1016 printf("first cylinder group ran out of space\n");
1017 return (0);
1018 }
1019 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1020 if (isblock(&sblock, cg_blksfree(&acg, 0),
1021 d >> sblock.fs_fragshift))
1022 goto goth;
1023 printf("internal error: can't find block in cyl 0\n");
1024 return (0);
1025 goth:
1026 blkno = fragstoblks(&sblock, d);
1027 clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
1028 if (sblock.fs_contigsumsize > 0)
1029 clrbit(cg_clustersfree(&acg, 0), blkno);
1030 acg.cg_cs.cs_nbfree--;
1031 sblock.fs_cstotal.cs_nbfree--;
1032 fscs[0].cs_nbfree--;
1033 if (mode & IFDIR) {
1034 acg.cg_cs.cs_ndir++;
1035 sblock.fs_cstotal.cs_ndir++;
1036 fscs[0].cs_ndir++;
1037 }
1038 if (size != sblock.fs_bsize) {
1039 frag = howmany(size, sblock.fs_fsize);
1040 fscs[0].cs_nffree += sblock.fs_frag - frag;
1041 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1042 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1043 acg.cg_frsum[sblock.fs_frag - frag]++;
1044 for (i = frag; i < sblock.fs_frag; i++)
1045 setbit(cg_blksfree(&acg, 0), d + i);
1046 }
1047 /* host -> fs byte order */
1048 if (needswap)
1049 ffs_cg_swap(&acg, &acg, &sblock);
1050 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1051 return (d);
1052 }
1053
1054 /*
1055 * Allocate an inode on the disk
1056 */
1057 static void
1058 iput(union dinode *ip, ino_t ino)
1059 {
1060 daddr_t d;
1061 int c, i;
1062 struct ufs1_dinode *dp1;
1063 struct ufs2_dinode *dp2;
1064
1065 c = ino_to_cg(&sblock, ino);
1066 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1067 /* fs -> host byte order */
1068 if (needswap)
1069 ffs_cg_swap(&acg, &acg, &sblock);
1070 if (acg.cg_magic != CG_MAGIC) {
1071 printf("cg 0: bad magic number\n");
1072 exit(31);
1073 }
1074 acg.cg_cs.cs_nifree--;
1075 setbit(cg_inosused(&acg, 0), ino);
1076 /* host -> fs byte order */
1077 if (needswap)
1078 ffs_cg_swap(&acg, &acg, &sblock);
1079 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1080 sblock.fs_cstotal.cs_nifree--;
1081 fscs[0].cs_nifree--;
1082 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
1083 printf("fsinit: inode value out of range (%d).\n", ino);
1084 exit(32);
1085 }
1086 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1087 rdfs(d, sblock.fs_bsize, (char *)iobuf);
1088 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1089 dp1 = (struct ufs1_dinode *)iobuf;
1090 if (needswap) {
1091 ffs_dinode1_swap(&ip->dp1,
1092 &dp1[ino_to_fsbo(&sblock, ino)]);
1093 /* ffs_dinode1_swap() doesn't swap blocks addrs */
1094 for (i=0; i<NDADDR + NIADDR; i++)
1095 (&dp1[ino_to_fsbo(&sblock, ino)])->di_db[i] =
1096 bswap32(ip->dp1.di_db[i]);
1097 } else
1098 dp1[ino_to_fsbo(&sblock, ino)] = ip->dp1;
1099 } else {
1100 dp2 = (struct ufs2_dinode *)iobuf;
1101 if (needswap) {
1102 ffs_dinode2_swap(&ip->dp2,
1103 &dp2[ino_to_fsbo(&sblock, ino)]);
1104 for (i=0; i<NDADDR + NIADDR; i++)
1105 (&dp2[ino_to_fsbo(&sblock, ino)])->di_db[i] =
1106 bswap32(ip->dp2.di_db[i]);
1107 } else
1108 dp2[ino_to_fsbo(&sblock, ino)] = ip->dp2;
1109 }
1110 wtfs(d, sblock.fs_bsize, iobuf);
1111 }
1112
1113 /*
1114 * read a block from the file system
1115 */
1116 void
1117 rdfs(daddr_t bno, int size, void *bf)
1118 {
1119 int n;
1120 off_t offset;
1121
1122 #ifdef MFS
1123 if (mfs) {
1124 memmove(bf, membase + bno * sectorsize, size);
1125 return;
1126 }
1127 #endif
1128 offset = bno;
1129 n = pread(fsi, bf, size, offset * sectorsize);
1130 if (n != size) {
1131 printf("rdfs: read error for sector %lld: %s\n",
1132 (long long)bno, strerror(errno));
1133 exit(34);
1134 }
1135 }
1136
1137 /*
1138 * write a block to the file system
1139 */
1140 void
1141 wtfs(daddr_t bno, int size, void *bf)
1142 {
1143 int n;
1144 off_t offset;
1145
1146 #ifdef MFS
1147 if (mfs) {
1148 memmove(membase + bno * sectorsize, bf, size);
1149 return;
1150 }
1151 #endif
1152 if (Nflag)
1153 return;
1154 offset = bno;
1155 n = pwrite(fso, bf, size, offset * sectorsize);
1156 if (n != size) {
1157 printf("wtfs: write error for sector %lld: %s\n",
1158 (long long)bno, strerror(errno));
1159 exit(36);
1160 }
1161 }
1162
1163 /*
1164 * check if a block is available
1165 */
1166 int
1167 isblock(struct fs *fs, unsigned char *cp, int h)
1168 {
1169 unsigned char mask;
1170
1171 switch (fs->fs_fragshift) {
1172 case 3:
1173 return (cp[h] == 0xff);
1174 case 2:
1175 mask = 0x0f << ((h & 0x1) << 2);
1176 return ((cp[h >> 1] & mask) == mask);
1177 case 1:
1178 mask = 0x03 << ((h & 0x3) << 1);
1179 return ((cp[h >> 2] & mask) == mask);
1180 case 0:
1181 mask = 0x01 << (h & 0x7);
1182 return ((cp[h >> 3] & mask) == mask);
1183 default:
1184 #ifdef STANDALONE
1185 printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
1186 #else
1187 fprintf(stderr, "isblock bad fs_fragshift %d\n",
1188 fs->fs_fragshift);
1189 #endif
1190 return (0);
1191 }
1192 }
1193
1194 /*
1195 * take a block out of the map
1196 */
1197 void
1198 clrblock(struct fs *fs, unsigned char *cp, int h)
1199 {
1200 switch ((fs)->fs_fragshift) {
1201 case 3:
1202 cp[h] = 0;
1203 return;
1204 case 2:
1205 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1206 return;
1207 case 1:
1208 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1209 return;
1210 case 0:
1211 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1212 return;
1213 default:
1214 #ifdef STANDALONE
1215 printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
1216 #else
1217 fprintf(stderr, "clrblock bad fs_fragshift %d\n",
1218 fs->fs_fragshift);
1219 #endif
1220 return;
1221 }
1222 }
1223
1224 /*
1225 * put a block into the map
1226 */
1227 void
1228 setblock(struct fs *fs, unsigned char *cp, int h)
1229 {
1230 switch (fs->fs_fragshift) {
1231 case 3:
1232 cp[h] = 0xff;
1233 return;
1234 case 2:
1235 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1236 return;
1237 case 1:
1238 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1239 return;
1240 case 0:
1241 cp[h >> 3] |= (0x01 << (h & 0x7));
1242 return;
1243 default:
1244 #ifdef STANDALONE
1245 printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
1246 #else
1247 fprintf(stderr, "setblock bad fs_fragshift %d\n",
1248 fs->fs_fragshift);
1249 #endif
1250 return;
1251 }
1252 }
1253
1254 /* copy a direntry to a buffer, in fs byte order */
1255 static void
1256 copy_dir(struct direct *dir, struct direct *dbuf)
1257 {
1258 memcpy(dbuf, dir, DIRSIZ(Oflag == 0, dir, 0));
1259 if (needswap) {
1260 dbuf->d_ino = bswap32(dir->d_ino);
1261 dbuf->d_reclen = bswap16(dir->d_reclen);
1262 if (Oflag == 0)
1263 ((struct odirect*)dbuf)->d_namlen =
1264 bswap16(((struct odirect*)dir)->d_namlen);
1265 }
1266 }
1267
1268 /* Determine how many digits are needed to print a given integer */
1269 static int
1270 count_digits(uint64_t num)
1271 {
1272 int ndig;
1273
1274 for (ndig = 1; num > 9; num /= 10, ndig++);
1275
1276 return (ndig);
1277 }
1278
1279 static int
1280 ilog2(int val)
1281 {
1282 u_int n;
1283
1284 for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
1285 if (1 << n == val)
1286 return (n);
1287 errx(1, "ilog2: %d is not a power of 2\n", val);
1288 }
1289
1290
1291 #ifdef MFS
1292 /*
1293 * XXX!
1294 * Attempt to guess how much more space is available for process data. The
1295 * heuristic we use is
1296 *
1297 * max_data_limit - (sbrk(0) - etext) - 128kB
1298 *
1299 * etext approximates that start address of the data segment, and the 128kB
1300 * allows some slop for both segment gap between text and data, and for other
1301 * (libc) malloc usage.
1302 */
1303 static void
1304 calc_memfree(void)
1305 {
1306 extern char etext;
1307 struct rlimit rlp;
1308 u_long base;
1309
1310 base = (u_long)sbrk(0) - (u_long)&etext;
1311 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1312 perror("getrlimit");
1313 rlp.rlim_cur = rlp.rlim_max;
1314 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1315 perror("setrlimit");
1316 memleft = rlp.rlim_max - base - (128 * 1024);
1317 }
1318
1319 /*
1320 * Internal version of malloc that trims the requested size if not enough
1321 * memory is available.
1322 */
1323 static void *
1324 mkfs_malloc(size_t size)
1325 {
1326 u_long pgsz;
1327
1328 if (size == 0)
1329 return (NULL);
1330 if (memleft == 0)
1331 calc_memfree();
1332
1333 pgsz = getpagesize() - 1;
1334 size = (size + pgsz) &~ pgsz;
1335 if (size > memleft)
1336 size = memleft;
1337 memleft -= size;
1338 return (mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1339 -1, 0));
1340 }
1341 #endif /* MFS */
1342