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