mkfs.c revision 1.95 1 /* $NetBSD: mkfs.c,v 1.95 2006/01/11 12:17:42 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.95 2006/01/11 12:17:42 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 #include <stddef.h>
97
98 #ifndef STANDALONE
99 #include <stdio.h>
100 #endif
101
102 #include "extern.h"
103
104 union dinode {
105 struct ufs1_dinode dp1;
106 struct ufs2_dinode dp2;
107 };
108
109 static void initcg(int, const struct timeval *);
110 static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
111 static int makedir(struct direct *, int);
112 static daddr_t alloc(int, int);
113 static void iput(union dinode *, ino_t);
114 static void rdfs(daddr_t, int, void *);
115 static void wtfs(daddr_t, int, void *);
116 static int isblock(struct fs *, unsigned char *, int);
117 static void clrblock(struct fs *, unsigned char *, int);
118 static void setblock(struct fs *, unsigned char *, int);
119 static int ilog2(int);
120 static void zap_old_sblock(int);
121 #ifdef MFS
122 static void calc_memfree(void);
123 static void *mkfs_malloc(size_t size);
124 #endif
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; /* size to end of 2nd inode block */
156 int iobuf_memsize; /* Actual buffer size */
157
158 int fsi, fso;
159
160 void
161 mkfs(struct partition *pp, const char *fsys, int fi, int fo,
162 mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
163 {
164 uint fragsperinodeblk, ncg;
165 uint cgzero;
166 uint64_t inodeblks, cgall;
167 int32_t cylno, i, csfrags;
168 struct timeval tv;
169 long long sizepb;
170 int col, delta;
171
172 #ifndef STANDALONE
173 gettimeofday(&tv, NULL);
174 #endif
175 #ifdef MFS
176 if (mfs && !Nflag) {
177 calc_memfree();
178 if (fssize * sectorsize > memleft)
179 fssize = memleft / sectorsize;
180 if ((membase = mkfs_malloc(fssize * sectorsize)) == 0)
181 exit(12);
182 }
183 #endif
184 fsi = fi;
185 fso = fo;
186 if (Oflag == 0) {
187 sblock.fs_old_inodefmt = FS_42INODEFMT;
188 sblock.fs_maxsymlinklen = 0;
189 sblock.fs_old_flags = 0;
190 } else {
191 sblock.fs_old_inodefmt = FS_44INODEFMT;
192 sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
193 MAXSYMLINKLEN_UFS2);
194 sblock.fs_old_flags = FS_FLAGS_UPDATED;
195 if (isappleufs)
196 sblock.fs_old_flags = 0;
197 sblock.fs_flags = 0;
198 }
199
200 /*
201 * collect and verify the filesystem density info
202 */
203 sblock.fs_avgfilesize = avgfilesize;
204 sblock.fs_avgfpdir = avgfpdir;
205 if (sblock.fs_avgfilesize <= 0) {
206 printf("illegal expected average file size %d\n",
207 sblock.fs_avgfilesize);
208 exit(14);
209 }
210 if (sblock.fs_avgfpdir <= 0) {
211 printf("illegal expected number of files per directory %d\n",
212 sblock.fs_avgfpdir);
213 exit(15);
214 }
215 /*
216 * collect and verify the block and fragment sizes
217 */
218 sblock.fs_bsize = bsize;
219 sblock.fs_fsize = fsize;
220 if (!POWEROF2(sblock.fs_bsize)) {
221 printf("block size must be a power of 2, not %d\n",
222 sblock.fs_bsize);
223 exit(16);
224 }
225 if (!POWEROF2(sblock.fs_fsize)) {
226 printf("fragment size must be a power of 2, not %d\n",
227 sblock.fs_fsize);
228 exit(17);
229 }
230 if (sblock.fs_fsize < sectorsize) {
231 printf("fragment size %d is too small, minimum is %d\n",
232 sblock.fs_fsize, sectorsize);
233 exit(18);
234 }
235 if (sblock.fs_bsize < MINBSIZE) {
236 printf("block size %d is too small, minimum is %d\n",
237 sblock.fs_bsize, MINBSIZE);
238 exit(19);
239 }
240 if (sblock.fs_bsize > MAXBSIZE) {
241 printf("block size %d is too large, maximum is %d\n",
242 sblock.fs_bsize, MAXBSIZE);
243 exit(19);
244 }
245 if (sblock.fs_bsize < sblock.fs_fsize) {
246 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
247 sblock.fs_bsize, sblock.fs_fsize);
248 exit(20);
249 }
250
251 if (maxbsize < bsize || !POWEROF2(maxbsize)) {
252 sblock.fs_maxbsize = sblock.fs_bsize;
253 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
254 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
255 } else {
256 sblock.fs_maxbsize = maxbsize;
257 }
258 sblock.fs_maxcontig = maxcontig;
259 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
260 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
261 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
262 }
263 if (sblock.fs_maxcontig > 1)
264 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
265
266 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
267 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
268 sblock.fs_qbmask = ~sblock.fs_bmask;
269 sblock.fs_qfmask = ~sblock.fs_fmask;
270 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
271 sblock.fs_bshift++;
272 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
273 sblock.fs_fshift++;
274 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
275 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
276 sblock.fs_fragshift++;
277 if (sblock.fs_frag > MAXFRAG) {
278 printf("fragment size %d is too small, "
279 "minimum with block size %d is %d\n",
280 sblock.fs_fsize, sblock.fs_bsize,
281 sblock.fs_bsize / MAXFRAG);
282 exit(21);
283 }
284 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
285 sblock.fs_size = dbtofsb(&sblock, fssize);
286 if (Oflag <= 1) {
287 if (sblock.fs_size >= 1ull << 31) {
288 printf("Too many fragments (0x%" PRIx64
289 ") for a UFS1 filesystem\n", sblock.fs_size);
290 exit(22);
291 }
292 sblock.fs_magic = FS_UFS1_MAGIC;
293 sblock.fs_sblockloc = SBLOCK_UFS1;
294 sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
295 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
296 sblock.fs_old_cgoffset = 0;
297 sblock.fs_old_cgmask = 0xffffffff;
298 sblock.fs_old_size = sblock.fs_size;
299 sblock.fs_old_rotdelay = 0;
300 sblock.fs_old_rps = 60;
301 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
302 sblock.fs_old_cpg = 1;
303 sblock.fs_old_interleave = 1;
304 sblock.fs_old_trackskew = 0;
305 sblock.fs_old_cpc = 0;
306 sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
307 sblock.fs_old_nrpos = 1;
308 } else {
309 sblock.fs_magic = FS_UFS2_MAGIC;
310 sblock.fs_sblockloc = SBLOCK_UFS2;
311 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
312 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
313 }
314
315 sblock.fs_sblkno =
316 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
317 sblock.fs_frag);
318 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
319 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
320 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
321 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
322 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
323 sizepb *= NINDIR(&sblock);
324 sblock.fs_maxfilesize += sizepb;
325 }
326
327 /*
328 * Calculate the number of blocks to put into each cylinder group.
329 *
330 * The cylinder group size is limited because the data structure
331 * must fit into a single block.
332 * We try to have as few cylinder groups as possible, with a proviso
333 * that we create at least MINCYLGRPS (==4) except for small
334 * filesystems.
335 *
336 * This algorithm works out how many blocks of inodes would be
337 * needed to fill the entire volume at the specified density.
338 * It then looks at how big the 'cylinder block' would have to
339 * be and, assuming that it is linearly related to the number
340 * of inodes and blocks how many cylinder groups are needed to
341 * keep the cylinder block below the filesystem block size.
342 *
343 * The cylinder groups are then all created with the average size.
344 *
345 * Space taken by the red tape on cylinder groups other than the
346 * first is ignored.
347 */
348
349 /* There must be space for 1 inode block and 2 data blocks */
350 if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
351 printf("Filesystem size %lld < minimum size of %d\n",
352 (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
353 exit(23);
354 }
355 if (num_inodes != 0)
356 inodeblks = howmany(num_inodes, INOPB(&sblock));
357 else {
358 /*
359 * Calculate 'per inode block' so we can allocate less than
360 * 1 fragment per inode - useful for /dev.
361 */
362 fragsperinodeblk = MAX(numfrags(&sblock,
363 density * INOPB(&sblock)), 1);
364 inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
365 (sblock.fs_frag + fragsperinodeblk);
366 }
367 if (inodeblks == 0)
368 inodeblks = 1;
369 /* Ensure that there are at least 2 data blocks (or we fail below) */
370 if (inodeblks > (sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
371 inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
372 /* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
373 if (inodeblks * INOPB(&sblock) >= 1ull << 31)
374 inodeblks = ((1ull << 31) - NBBY) / INOPB(&sblock);
375 /*
376 * See what would happen if we tried to use 1 cylinder group.
377 * Assume space linear, so work out number of cylinder groups needed.
378 * Subtract one from the allowed size to compensate for rounding
379 * a number of bits up to a complete byte.
380 */
381 cgzero = CGSIZE_IF(&sblock, 0, 0);
382 cgall = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock), sblock.fs_size);
383 ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero - 1);
384 if (ncg < MINCYLGRPS) {
385 /*
386 * We would like to allocate MINCLYGRPS cylinder groups,
387 * but for small file sytems (especially ones with a lot
388 * of inodes) this is not desirable (or possible).
389 */
390 i = sblock.fs_size / 2 / (sblock.fs_iblkno +
391 inodeblks * sblock.fs_frag);
392 if (i > ncg)
393 ncg = i;
394 if (ncg > MINCYLGRPS)
395 ncg = MINCYLGRPS;
396 if (ncg > inodeblks)
397 ncg = inodeblks;
398 }
399 /*
400 * Put an equal number of blocks in each cylinder group.
401 * Round up so we don't have more fragments in the last CG than
402 * the earlier ones (does that matter?), but kill a block if the
403 * CGSIZE becomes too big (only happens if there are a lot of CGs).
404 */
405 sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
406 i = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock) / ncg, sblock.fs_fpg);
407 if (i > sblock.fs_bsize)
408 sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
409 /* ... and recalculate how many cylinder groups we now need */
410 ncg = howmany(sblock.fs_size, sblock.fs_fpg);
411 inodeblks /= ncg;
412 if (inodeblks == 0)
413 inodeblks = 1;
414 sblock.fs_ipg = inodeblks * INOPB(&sblock);
415 /* Sanity check on our sums... */
416 if (CGSIZE(&sblock) > sblock.fs_bsize) {
417 printf("CGSIZE miscalculated %d > %d\n",
418 (int)CGSIZE(&sblock), sblock.fs_bsize);
419 exit(24);
420 }
421 /* Check that the last cylinder group has enough space for the inodes */
422 i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
423 if (i < sblock.fs_iblkno + inodeblks * sblock.fs_frag) {
424 /*
425 * Since we make all the cylinder groups the same size, the
426 * last will only be small if there are a large number of
427 * cylinder groups. If we pull even a fragment from each
428 * of the other groups then the last CG will be overfull.
429 * So we just kill the last CG.
430 */
431 ncg--;
432 sblock.fs_size -= i;
433 }
434 sblock.fs_ncg = ncg;
435
436 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
437 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
438 if (Oflag <= 1) {
439 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
440 sblock.fs_old_nsect = sblock.fs_old_spc;
441 sblock.fs_old_npsect = sblock.fs_old_spc;
442 sblock.fs_old_ncyl = sblock.fs_ncg;
443 }
444
445 /*
446 * Cylinder group summary information for each cylinder is written
447 * into the first cylinder group.
448 * Write this fragment by fragment, but doing the first CG last
449 * (after we've taken stuff off for the structure itself and the
450 * root directory.
451 */
452 sblock.fs_csaddr = cgdmin(&sblock, 0);
453 sblock.fs_cssize =
454 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
455 if (512 % sizeof *fscs_0)
456 errx(1, "cylinder group summary doesn't fit in sectors");
457 fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
458 MAP_ANON|MAP_PRIVATE, -1, 0);
459 if (fscs_0 == NULL)
460 exit(39);
461 memset(fscs_0, 0, 2 * sblock.fs_fsize);
462 fs_csaddr = sblock.fs_csaddr;
463 fscs_next = fscs_0;
464 fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
465 fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
466 /*
467 * fill in remaining fields of the super block
468 */
469 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
470 if (sblock.fs_sbsize > SBLOCKSIZE)
471 sblock.fs_sbsize = SBLOCKSIZE;
472 sblock.fs_minfree = minfree;
473 sblock.fs_maxcontig = maxcontig;
474 sblock.fs_maxbpg = maxbpg;
475 sblock.fs_optim = opt;
476 sblock.fs_cgrotor = 0;
477 sblock.fs_pendingblocks = 0;
478 sblock.fs_pendinginodes = 0;
479 sblock.fs_cstotal.cs_ndir = 0;
480 sblock.fs_cstotal.cs_nbfree = 0;
481 sblock.fs_cstotal.cs_nifree = 0;
482 sblock.fs_cstotal.cs_nffree = 0;
483 sblock.fs_fmod = 0;
484 sblock.fs_ronly = 0;
485 sblock.fs_state = 0;
486 sblock.fs_clean = FS_ISCLEAN;
487 sblock.fs_ronly = 0;
488 sblock.fs_id[0] = (long)tv.tv_sec; /* XXXfvdl huh? */
489 sblock.fs_id[1] = arc4random() & INT32_MAX;
490 sblock.fs_fsmnt[0] = '\0';
491 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
492 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
493 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
494 sblock.fs_cstotal.cs_nbfree =
495 fragstoblks(&sblock, sblock.fs_dsize) -
496 howmany(csfrags, sblock.fs_frag);
497 sblock.fs_cstotal.cs_nffree =
498 fragnum(&sblock, sblock.fs_size) +
499 (fragnum(&sblock, csfrags) > 0 ?
500 sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
501 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
502 sblock.fs_cstotal.cs_ndir = 0;
503 sblock.fs_dsize -= csfrags;
504 sblock.fs_time = tv.tv_sec;
505 if (Oflag <= 1) {
506 sblock.fs_old_time = tv.tv_sec;
507 sblock.fs_old_dsize = sblock.fs_dsize;
508 sblock.fs_old_csaddr = sblock.fs_csaddr;
509 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
510 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
511 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
512 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
513 }
514 /*
515 * Dump out summary information about file system.
516 */
517 if (!mfs || Nflag) {
518 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
519 printf("%s: %.1fMB (%lld sectors) block size %d, "
520 "fragment size %d\n",
521 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
522 (long long)fsbtodb(&sblock, sblock.fs_size),
523 sblock.fs_bsize, sblock.fs_fsize);
524 printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
525 "%d inodes.\n",
526 sblock.fs_ncg,
527 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
528 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
529 #undef B2MBFACTOR
530 }
531
532 /*
533 * allocate space for superblock, cylinder group map, and
534 * two sets of inode blocks.
535 */
536 if (sblock.fs_bsize < SBLOCKSIZE)
537 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
538 else
539 iobufsize = 4 * sblock.fs_bsize;
540 iobuf_memsize = iobufsize;
541 if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
542 /* A larger buffer so we can write multiple inode blks */
543 iobuf_memsize += 14 * sblock.fs_bsize;
544 }
545 for (;;) {
546 iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
547 MAP_ANON|MAP_PRIVATE, -1, 0);
548 if (iobuf != NULL)
549 break;
550 if (iobuf_memsize != iobufsize) {
551 /* Try again with the smaller size */
552 iobuf_memsize = iobufsize;
553 continue;
554 }
555 printf("Cannot allocate I/O buffer\n");
556 exit(38);
557 }
558 memset(iobuf, 0, iobuf_memsize);
559
560 /*
561 * We now start writing to the filesystem
562 */
563
564 /*
565 * Validate the given file system size.
566 * Verify that its last block can actually be accessed.
567 * Convert to file system fragment sized units.
568 */
569 if (fssize <= 0) {
570 printf("preposterous size %lld\n", (long long)fssize);
571 exit(13);
572 }
573 wtfs(fssize - 1, sectorsize, iobuf);
574
575 /*
576 * Ensure there is nothing that looks like a filesystem
577 * superbock anywhere other than where ours will be.
578 * If fsck finds the wrong one all hell breaks loose!
579 */
580 for (i = 0; ; i++) {
581 static const int sblocklist[] = SBLOCKSEARCH;
582 int sblkoff = sblocklist[i];
583 int sz;
584 if (sblkoff == -1)
585 break;
586 /* Remove main superblock */
587 zap_old_sblock(sblkoff);
588 /* and all possible locations for the first alternate */
589 sblkoff += SBLOCKSIZE;
590 for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
591 zap_old_sblock(roundup(sblkoff, sz));
592 }
593
594 if (isappleufs) {
595 struct appleufslabel appleufs;
596 ffs_appleufs_set(&appleufs, appleufs_volname, tv.tv_sec, 0);
597 wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
598 &appleufs);
599 } else {
600 struct appleufslabel appleufs;
601 /* Look for and zap any existing valid apple ufs labels */
602 rdfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
603 &appleufs);
604 if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
605 memset(&appleufs, 0, sizeof(appleufs));
606 wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
607 &appleufs);
608 }
609 }
610
611 /*
612 * Make a copy of the superblock into the buffer that we will be
613 * writing out in each cylinder group.
614 */
615 memcpy(iobuf, &sblock, sizeof sblock);
616 if (needswap)
617 ffs_sb_swap(&sblock, (struct fs *)iobuf);
618 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
619 memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
620 0xff, 256);
621
622 if (!mfs || Nflag)
623 printf("super-block backups (for fsck_ffs -b #) at:\n");
624 #define MAX_LINE 79
625 #define BASE 0x10000 /* For some fixed-point maths */
626 col = 0;
627 delta = 0;
628 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
629 int len;
630 initcg(cylno, &tv);
631 if (mfs && !Nflag)
632 continue;
633 if (delta == 0) {
634 /* Print one line of numbers (int is enough) */
635 len = printf(" %u,",
636 (int)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
637 col += len;
638 if (col + len + 1 >= MAX_LINE) {
639 /* Next one won't fit, work out dot spacing */
640 delta = sblock.fs_ncg - cylno;
641 if (delta > 1) {
642 col = 0;
643 delta = MAX_LINE * BASE / (delta - 1);
644 printf("\n");
645 }
646 }
647 } else {
648 /* Then at most one line of dots */
649 col += delta;
650 if (col < BASE)
651 continue;
652 printf(".");
653 col -= BASE;
654 }
655 fflush(stdout);
656 }
657 #undef MAX_LINE
658 #undef BASE
659 if (!mfs || Nflag)
660 printf("\n");
661 if (Nflag)
662 exit(0);
663
664 /*
665 * Now construct the initial file system,
666 */
667 if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
668 errx(1, "Error making filesystem");
669 sblock.fs_time = tv.tv_sec;
670 if (Oflag <= 1) {
671 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
672 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
673 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
674 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
675 }
676 /*
677 * Write out the super-block and zeros until the first cg info
678 */
679 i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
680 memset(iobuf, 0, i);
681 memcpy(iobuf, &sblock, sizeof sblock);
682 if (needswap)
683 ffs_sb_swap(&sblock, (struct fs *)iobuf);
684 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
685 memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
686 0xff, 256);
687 wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
688
689 /* Write out first and last cylinder summary sectors */
690 if (needswap)
691 ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
692 wtfs(fsbtodb(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
693
694 if (fscs_next > fscs_reset) {
695 if (needswap)
696 ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
697 fs_csaddr++;
698 wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
699 }
700
701 /* mfs doesn't need these permanently allocated */
702 munmap(iobuf, iobuf_memsize);
703 munmap(fscs_0, 2 * sblock.fs_fsize);
704
705 /*
706 * Update information about this partion in pack
707 * label, to that it may be updated on disk.
708 */
709 if (pp == NULL)
710 return;
711 if (isappleufs)
712 pp->p_fstype = FS_APPLEUFS;
713 else
714 pp->p_fstype = FS_BSDFFS;
715 pp->p_fsize = sblock.fs_fsize;
716 pp->p_frag = sblock.fs_frag;
717 pp->p_cpg = sblock.fs_fpg;
718 }
719
720 /*
721 * Initialize a cylinder group.
722 */
723 void
724 initcg(int cylno, const struct timeval *tv)
725 {
726 daddr_t cbase, dmax;
727 int32_t i, d, dlower, dupper, blkno;
728 struct ufs1_dinode *dp1;
729 struct ufs2_dinode *dp2;
730 int start;
731
732 /*
733 * Determine block bounds for cylinder group.
734 * Allow space for super block summary information in first
735 * cylinder group.
736 */
737 cbase = cgbase(&sblock, cylno);
738 dmax = cbase + sblock.fs_fpg;
739 if (dmax > sblock.fs_size)
740 dmax = sblock.fs_size;
741 dlower = cgsblock(&sblock, cylno) - cbase;
742 dupper = cgdmin(&sblock, cylno) - cbase;
743 if (cylno == 0) {
744 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
745 if (dupper >= cgstart(&sblock, cylno + 1)) {
746 printf("\rToo many cylinder groups to fit summary "
747 "information into first cylinder group\n");
748 exit(40);
749 }
750 }
751 memset(&acg, 0, sblock.fs_cgsize);
752 acg.cg_magic = CG_MAGIC;
753 acg.cg_cgx = cylno;
754 acg.cg_ndblk = dmax - cbase;
755 if (sblock.fs_contigsumsize > 0)
756 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
757 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
758 if (Oflag == 2) {
759 acg.cg_time = tv->tv_sec;
760 acg.cg_niblk = sblock.fs_ipg;
761 acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
762 sblock.fs_ipg : 2 * INOPB(&sblock);
763 acg.cg_iusedoff = start;
764 } else {
765 acg.cg_old_ncyl = sblock.fs_old_cpg;
766 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
767 (cylno == sblock.fs_ncg - 1))
768 acg.cg_old_ncyl =
769 sblock.fs_old_ncyl % sblock.fs_old_cpg;
770 acg.cg_old_time = tv->tv_sec;
771 acg.cg_old_niblk = sblock.fs_ipg;
772 acg.cg_old_btotoff = start;
773 acg.cg_old_boff = acg.cg_old_btotoff +
774 sblock.fs_old_cpg * sizeof(int32_t);
775 acg.cg_iusedoff = acg.cg_old_boff +
776 sblock.fs_old_cpg * sizeof(u_int16_t);
777 }
778 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
779 if (sblock.fs_contigsumsize <= 0) {
780 acg.cg_nextfreeoff = acg.cg_freeoff +
781 howmany(sblock.fs_fpg, CHAR_BIT);
782 } else {
783 acg.cg_clustersumoff = acg.cg_freeoff +
784 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
785 if (isappleufs) {
786 /* Apple PR2216969 gives rationale for this change.
787 * I believe they were mistaken, but we need to
788 * duplicate it for compatibility. -- dbj (at) NetBSD.org
789 */
790 acg.cg_clustersumoff += sizeof(int32_t);
791 }
792 acg.cg_clustersumoff =
793 roundup(acg.cg_clustersumoff, sizeof(int32_t));
794 acg.cg_clusteroff = acg.cg_clustersumoff +
795 (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
796 acg.cg_nextfreeoff = acg.cg_clusteroff +
797 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
798 }
799 if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
800 printf("Panic: cylinder group too big\n");
801 exit(37);
802 }
803 acg.cg_cs.cs_nifree += sblock.fs_ipg;
804 if (cylno == 0)
805 for (i = 0; i < ROOTINO; i++) {
806 setbit(cg_inosused(&acg, 0), i);
807 acg.cg_cs.cs_nifree--;
808 }
809 if (cylno > 0) {
810 /*
811 * In cylno 0, beginning space is reserved
812 * for boot and super blocks.
813 */
814 for (d = 0, blkno = 0; d < dlower;) {
815 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
816 if (sblock.fs_contigsumsize > 0)
817 setbit(cg_clustersfree(&acg, 0), blkno);
818 acg.cg_cs.cs_nbfree++;
819 if (Oflag <= 1) {
820 int cn = old_cbtocylno(&sblock, d);
821 old_cg_blktot(&acg, 0)[cn]++;
822 old_cg_blks(&sblock, &acg,
823 cn, 0)[old_cbtorpos(&sblock, d)]++;
824 }
825 d += sblock.fs_frag;
826 blkno++;
827 }
828 }
829 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
830 acg.cg_frsum[sblock.fs_frag - i]++;
831 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
832 setbit(cg_blksfree(&acg, 0), dupper);
833 acg.cg_cs.cs_nffree++;
834 }
835 }
836 for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
837 d + sblock.fs_frag <= acg.cg_ndblk; ) {
838 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
839 if (sblock.fs_contigsumsize > 0)
840 setbit(cg_clustersfree(&acg, 0), blkno);
841 acg.cg_cs.cs_nbfree++;
842 if (Oflag <= 1) {
843 int cn = old_cbtocylno(&sblock, d);
844 old_cg_blktot(&acg, 0)[cn]++;
845 old_cg_blks(&sblock, &acg,
846 cn, 0)[old_cbtorpos(&sblock, d)]++;
847 }
848 d += sblock.fs_frag;
849 blkno++;
850 }
851 if (d < acg.cg_ndblk) {
852 acg.cg_frsum[acg.cg_ndblk - d]++;
853 for (; d < acg.cg_ndblk; d++) {
854 setbit(cg_blksfree(&acg, 0), d);
855 acg.cg_cs.cs_nffree++;
856 }
857 }
858 if (sblock.fs_contigsumsize > 0) {
859 int32_t *sump = cg_clustersum(&acg, 0);
860 u_char *mapp = cg_clustersfree(&acg, 0);
861 int map = *mapp++;
862 int bit = 1;
863 int run = 0;
864
865 for (i = 0; i < acg.cg_nclusterblks; i++) {
866 if ((map & bit) != 0) {
867 run++;
868 } else if (run != 0) {
869 if (run > sblock.fs_contigsumsize)
870 run = sblock.fs_contigsumsize;
871 sump[run]++;
872 run = 0;
873 }
874 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
875 bit <<= 1;
876 } else {
877 map = *mapp++;
878 bit = 1;
879 }
880 }
881 if (run != 0) {
882 if (run > sblock.fs_contigsumsize)
883 run = sblock.fs_contigsumsize;
884 sump[run]++;
885 }
886 }
887 *fscs_next++ = acg.cg_cs;
888 if (fscs_next == fscs_end) {
889 /* write block of cylinder group summary info into cyl 0 */
890 if (needswap)
891 ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
892 fs_csaddr++;
893 wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
894 fscs_next = fscs_reset;
895 memset(fscs_next, 0, sblock.fs_fsize);
896 }
897 /*
898 * Write out the duplicate super block, the cylinder group map
899 * and two blocks worth of inodes in a single write.
900 */
901 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
902 memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
903 if (needswap)
904 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
905 start += sblock.fs_bsize;
906 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
907 dp2 = (struct ufs2_dinode *)(&iobuf[start]);
908 for (i = MIN(sblock.fs_ipg, 2) * INOPB(&sblock); i != 0; i--) {
909 if (sblock.fs_magic == FS_UFS1_MAGIC) {
910 /* No need to swap, it'll stay random */
911 dp1->di_gen = arc4random() & INT32_MAX;
912 dp1++;
913 } else {
914 dp2->di_gen = arc4random() & INT32_MAX;
915 dp2++;
916 }
917 }
918 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
919 /*
920 * For the old file system, we have to initialize all the inodes.
921 */
922 if (sblock.fs_magic != FS_UFS1_MAGIC)
923 return;
924
925 /* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
926 d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
927 dupper = sblock.fs_ipg / INOPF(&sblock);
928 for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
929 if (d > dupper - i)
930 d = dupper - i;
931 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
932 do
933 dp1->di_gen = arc4random() & INT32_MAX;
934 while ((char *)++dp1 < &iobuf[iobuf_memsize]);
935 wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
936 d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
937 }
938 }
939
940 /*
941 * initialize the file system
942 */
943
944 #ifdef LOSTDIR
945 #define PREDEFDIR 3
946 #else
947 #define PREDEFDIR 2
948 #endif
949
950 struct direct root_dir[] = {
951 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
952 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
953 #ifdef LOSTDIR
954 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
955 #endif
956 };
957 struct odirect {
958 u_int32_t d_ino;
959 u_int16_t d_reclen;
960 u_int16_t d_namlen;
961 u_char d_name[FFS_MAXNAMLEN + 1];
962 } oroot_dir[] = {
963 { ROOTINO, sizeof(struct direct), 1, "." },
964 { ROOTINO, sizeof(struct direct), 2, ".." },
965 #ifdef LOSTDIR
966 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
967 #endif
968 };
969 #ifdef LOSTDIR
970 struct direct lost_found_dir[] = {
971 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
972 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
973 { 0, DIRBLKSIZ, 0, 0, 0 },
974 };
975 struct odirect olost_found_dir[] = {
976 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
977 { ROOTINO, sizeof(struct direct), 2, ".." },
978 { 0, DIRBLKSIZ, 0, 0 },
979 };
980 #endif
981 char buf[MAXBSIZE];
982 static void copy_dir(struct direct *, struct direct *);
983
984 int
985 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
986 {
987 union dinode node;
988 #ifdef LOSTDIR
989 int i;
990 int dirblksiz = DIRBLKSIZ;
991 if (isappleufs)
992 dirblksiz = APPLEUFS_DIRBLKSIZ;
993 #endif
994
995 /*
996 * initialize the node
997 */
998
999 #ifdef LOSTDIR
1000 /*
1001 * create the lost+found directory
1002 */
1003 memset(&node, 0, sizeof(node));
1004 if (Oflag == 0) {
1005 (void)makedir((struct direct *)olost_found_dir, 2);
1006 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1007 copy_dir((struct direct*)&olost_found_dir[2],
1008 (struct direct*)&buf[i]);
1009 } else {
1010 (void)makedir(lost_found_dir, 2);
1011 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1012 copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
1013 }
1014 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1015 node.dp1.di_atime = tv->tv_sec;
1016 node.dp1.di_atimensec = tv->tv_usec * 1000;
1017 node.dp1.di_mtime = tv->tv_sec;
1018 node.dp1.di_mtimensec = tv->tv_usec * 1000;
1019 node.dp1.di_ctime = tv->tv_sec;
1020 node.dp1.di_ctimensec = tv->tv_usec * 1000;
1021 node.dp1.di_mode = IFDIR | UMASK;
1022 node.dp1.di_nlink = 2;
1023 node.dp1.di_size = sblock.fs_bsize;
1024 node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
1025 if (node.dp1.di_db[0] == 0)
1026 return (0);
1027 node.dp1.di_blocks = btodb(fragroundup(&sblock,
1028 node.dp1.di_size));
1029 node.dp1.di_uid = geteuid();
1030 node.dp1.di_gid = getegid();
1031 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
1032 buf);
1033 } else {
1034 node.dp2.di_atime = tv->tv_sec;
1035 node.dp2.di_atimensec = tv->tv_usec * 1000;
1036 node.dp2.di_mtime = tv->tv_sec;
1037 node.dp2.di_mtimensec = tv->tv_usec * 1000;
1038 node.dp2.di_ctime = tv->tv_sec;
1039 node.dp2.di_ctimensec = tv->tv_usec * 1000;
1040 node.dp2.di_birthtime = tv->tv_sec;
1041 node.dp2.di_birthnsec = tv->tv_usec * 1000;
1042 node.dp2.di_mode = IFDIR | UMASK;
1043 node.dp2.di_nlink = 2;
1044 node.dp2.di_size = sblock.fs_bsize;
1045 node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
1046 if (node.dp2.di_db[0] == 0)
1047 return (0);
1048 node.dp2.di_blocks = btodb(fragroundup(&sblock,
1049 node.dp2.di_size));
1050 node.dp2.di_uid = geteuid();
1051 node.dp2.di_gid = getegid();
1052 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
1053 buf);
1054 }
1055 iput(&node, LOSTFOUNDINO);
1056 #endif
1057 /*
1058 * create the root directory
1059 */
1060 memset(&node, 0, sizeof(node));
1061 if (Oflag <= 1) {
1062 if (mfs) {
1063 node.dp1.di_mode = IFDIR | mfsmode;
1064 node.dp1.di_uid = mfsuid;
1065 node.dp1.di_gid = mfsgid;
1066 } else {
1067 node.dp1.di_mode = IFDIR | UMASK;
1068 node.dp1.di_uid = geteuid();
1069 node.dp1.di_gid = getegid();
1070 }
1071 node.dp1.di_nlink = PREDEFDIR;
1072 if (Oflag == 0)
1073 node.dp1.di_size = makedir((struct direct *)oroot_dir,
1074 PREDEFDIR);
1075 else
1076 node.dp1.di_size = makedir(root_dir, PREDEFDIR);
1077 node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
1078 if (node.dp1.di_db[0] == 0)
1079 return (0);
1080 node.dp1.di_blocks = btodb(fragroundup(&sblock,
1081 node.dp1.di_size));
1082 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
1083 } else {
1084 if (mfs) {
1085 node.dp2.di_mode = IFDIR | mfsmode;
1086 node.dp2.di_uid = mfsuid;
1087 node.dp2.di_gid = mfsgid;
1088 } else {
1089 node.dp2.di_mode = IFDIR | UMASK;
1090 node.dp2.di_uid = geteuid();
1091 node.dp2.di_gid = getegid();
1092 }
1093 node.dp2.di_atime = tv->tv_sec;
1094 node.dp2.di_atimensec = tv->tv_usec * 1000;
1095 node.dp2.di_mtime = tv->tv_sec;
1096 node.dp2.di_mtimensec = tv->tv_usec * 1000;
1097 node.dp2.di_ctime = tv->tv_sec;
1098 node.dp2.di_ctimensec = tv->tv_usec * 1000;
1099 node.dp2.di_birthtime = tv->tv_sec;
1100 node.dp2.di_birthnsec = tv->tv_usec * 1000;
1101 node.dp2.di_nlink = PREDEFDIR;
1102 node.dp2.di_size = makedir(root_dir, PREDEFDIR);
1103 node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
1104 if (node.dp2.di_db[0] == 0)
1105 return (0);
1106 node.dp2.di_blocks = btodb(fragroundup(&sblock,
1107 node.dp2.di_size));
1108 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
1109 }
1110 iput(&node, ROOTINO);
1111 return (1);
1112 }
1113
1114 /*
1115 * construct a set of directory entries in "buf".
1116 * return size of directory.
1117 */
1118 int
1119 makedir(struct direct *protodir, int entries)
1120 {
1121 char *cp;
1122 int i, spcleft;
1123 int dirblksiz = DIRBLKSIZ;
1124 if (isappleufs)
1125 dirblksiz = APPLEUFS_DIRBLKSIZ;
1126
1127 memset(buf, 0, DIRBLKSIZ);
1128 spcleft = dirblksiz;
1129 for (cp = buf, i = 0; i < entries - 1; i++) {
1130 protodir[i].d_reclen = DIRSIZ(Oflag == 0, &protodir[i], 0);
1131 copy_dir(&protodir[i], (struct direct*)cp);
1132 cp += protodir[i].d_reclen;
1133 spcleft -= protodir[i].d_reclen;
1134 }
1135 protodir[i].d_reclen = spcleft;
1136 copy_dir(&protodir[i], (struct direct*)cp);
1137 return (dirblksiz);
1138 }
1139
1140 /*
1141 * allocate a block or frag
1142 */
1143 daddr_t
1144 alloc(int size, int mode)
1145 {
1146 int i, frag;
1147 daddr_t d, blkno;
1148
1149 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1150 /* fs -> host byte order */
1151 if (needswap)
1152 ffs_cg_swap(&acg, &acg, &sblock);
1153 if (acg.cg_magic != CG_MAGIC) {
1154 printf("cg 0: bad magic number\n");
1155 return (0);
1156 }
1157 if (acg.cg_cs.cs_nbfree == 0) {
1158 printf("first cylinder group ran out of space\n");
1159 return (0);
1160 }
1161 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1162 if (isblock(&sblock, cg_blksfree(&acg, 0),
1163 d >> sblock.fs_fragshift))
1164 goto goth;
1165 printf("internal error: can't find block in cyl 0\n");
1166 return (0);
1167 goth:
1168 blkno = fragstoblks(&sblock, d);
1169 clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
1170 if (sblock.fs_contigsumsize > 0)
1171 clrbit(cg_clustersfree(&acg, 0), blkno);
1172 acg.cg_cs.cs_nbfree--;
1173 sblock.fs_cstotal.cs_nbfree--;
1174 fscs_0->cs_nbfree--;
1175 if (mode & IFDIR) {
1176 acg.cg_cs.cs_ndir++;
1177 sblock.fs_cstotal.cs_ndir++;
1178 fscs_0->cs_ndir++;
1179 }
1180 if (Oflag <= 1) {
1181 int cn = old_cbtocylno(&sblock, d);
1182 old_cg_blktot(&acg, 0)[cn]--;
1183 old_cg_blks(&sblock, &acg,
1184 cn, 0)[old_cbtorpos(&sblock, d)]--;
1185 }
1186 if (size != sblock.fs_bsize) {
1187 frag = howmany(size, sblock.fs_fsize);
1188 fscs_0->cs_nffree += sblock.fs_frag - frag;
1189 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1190 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1191 acg.cg_frsum[sblock.fs_frag - frag]++;
1192 for (i = frag; i < sblock.fs_frag; i++)
1193 setbit(cg_blksfree(&acg, 0), d + i);
1194 }
1195 /* host -> fs byte order */
1196 if (needswap)
1197 ffs_cg_swap(&acg, &acg, &sblock);
1198 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1199 return (d);
1200 }
1201
1202 /*
1203 * Allocate an inode on the disk
1204 */
1205 static void
1206 iput(union dinode *ip, ino_t ino)
1207 {
1208 daddr_t d;
1209 int c, i;
1210 struct ufs1_dinode *dp1;
1211 struct ufs2_dinode *dp2;
1212
1213 c = ino_to_cg(&sblock, ino);
1214 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1215 /* fs -> host byte order */
1216 if (needswap)
1217 ffs_cg_swap(&acg, &acg, &sblock);
1218 if (acg.cg_magic != CG_MAGIC) {
1219 printf("cg 0: bad magic number\n");
1220 exit(31);
1221 }
1222 acg.cg_cs.cs_nifree--;
1223 setbit(cg_inosused(&acg, 0), ino);
1224 /* host -> fs byte order */
1225 if (needswap)
1226 ffs_cg_swap(&acg, &acg, &sblock);
1227 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1228 sblock.fs_cstotal.cs_nifree--;
1229 fscs_0->cs_nifree--;
1230 if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
1231 printf("fsinit: inode value out of range (%llu).\n",
1232 (unsigned long long)ino);
1233 exit(32);
1234 }
1235 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1236 rdfs(d, sblock.fs_bsize, (char *)iobuf);
1237 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1238 dp1 = (struct ufs1_dinode *)iobuf;
1239 dp1 += ino_to_fsbo(&sblock, ino);
1240 if (needswap) {
1241 ffs_dinode1_swap(&ip->dp1, dp1);
1242 /* ffs_dinode1_swap() doesn't swap blocks addrs */
1243 for (i=0; i<NDADDR + NIADDR; i++)
1244 dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
1245 } else
1246 *dp1 = ip->dp1;
1247 dp1->di_gen = arc4random() & INT32_MAX;
1248 } else {
1249 dp2 = (struct ufs2_dinode *)iobuf;
1250 dp2 += ino_to_fsbo(&sblock, ino);
1251 if (needswap) {
1252 ffs_dinode2_swap(&ip->dp2, dp2);
1253 for (i=0; i<NDADDR + NIADDR; i++)
1254 dp2->di_db[i] = bswap64(ip->dp2.di_db[i]);
1255 } else
1256 *dp2 = ip->dp2;
1257 dp2->di_gen = arc4random() & INT32_MAX;
1258 }
1259 wtfs(d, sblock.fs_bsize, iobuf);
1260 }
1261
1262 /*
1263 * read a block from the file system
1264 */
1265 void
1266 rdfs(daddr_t bno, int size, void *bf)
1267 {
1268 int n;
1269 off_t offset;
1270
1271 #ifdef MFS
1272 if (mfs) {
1273 if (Nflag)
1274 memset(bf, 0, size);
1275 else
1276 memmove(bf, membase + bno * sectorsize, size);
1277 return;
1278 }
1279 #endif
1280 offset = bno;
1281 n = pread(fsi, bf, size, offset * sectorsize);
1282 if (n != size) {
1283 printf("rdfs: read error for sector %lld: %s\n",
1284 (long long)bno, strerror(errno));
1285 exit(34);
1286 }
1287 }
1288
1289 /*
1290 * write a block to the file system
1291 */
1292 void
1293 wtfs(daddr_t bno, int size, void *bf)
1294 {
1295 int n;
1296 off_t offset;
1297
1298 if (Nflag)
1299 return;
1300 #ifdef MFS
1301 if (mfs) {
1302 memmove(membase + bno * sectorsize, bf, size);
1303 return;
1304 }
1305 #endif
1306 offset = bno;
1307 n = pwrite(fso, bf, size, offset * sectorsize);
1308 if (n != size) {
1309 printf("wtfs: write error for sector %lld: %s\n",
1310 (long long)bno, strerror(errno));
1311 exit(36);
1312 }
1313 }
1314
1315 /*
1316 * check if a block is available
1317 */
1318 int
1319 isblock(struct fs *fs, unsigned char *cp, int h)
1320 {
1321 unsigned char mask;
1322
1323 switch (fs->fs_fragshift) {
1324 case 3:
1325 return (cp[h] == 0xff);
1326 case 2:
1327 mask = 0x0f << ((h & 0x1) << 2);
1328 return ((cp[h >> 1] & mask) == mask);
1329 case 1:
1330 mask = 0x03 << ((h & 0x3) << 1);
1331 return ((cp[h >> 2] & mask) == mask);
1332 case 0:
1333 mask = 0x01 << (h & 0x7);
1334 return ((cp[h >> 3] & mask) == mask);
1335 default:
1336 #ifdef STANDALONE
1337 printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
1338 #else
1339 fprintf(stderr, "isblock bad fs_fragshift %d\n",
1340 fs->fs_fragshift);
1341 #endif
1342 return (0);
1343 }
1344 }
1345
1346 /*
1347 * take a block out of the map
1348 */
1349 void
1350 clrblock(struct fs *fs, unsigned char *cp, int h)
1351 {
1352 switch ((fs)->fs_fragshift) {
1353 case 3:
1354 cp[h] = 0;
1355 return;
1356 case 2:
1357 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1358 return;
1359 case 1:
1360 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1361 return;
1362 case 0:
1363 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1364 return;
1365 default:
1366 #ifdef STANDALONE
1367 printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
1368 #else
1369 fprintf(stderr, "clrblock bad fs_fragshift %d\n",
1370 fs->fs_fragshift);
1371 #endif
1372 return;
1373 }
1374 }
1375
1376 /*
1377 * put a block into the map
1378 */
1379 void
1380 setblock(struct fs *fs, unsigned char *cp, int h)
1381 {
1382 switch (fs->fs_fragshift) {
1383 case 3:
1384 cp[h] = 0xff;
1385 return;
1386 case 2:
1387 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1388 return;
1389 case 1:
1390 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1391 return;
1392 case 0:
1393 cp[h >> 3] |= (0x01 << (h & 0x7));
1394 return;
1395 default:
1396 #ifdef STANDALONE
1397 printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
1398 #else
1399 fprintf(stderr, "setblock bad fs_fragshift %d\n",
1400 fs->fs_fragshift);
1401 #endif
1402 return;
1403 }
1404 }
1405
1406 /* copy a direntry to a buffer, in fs byte order */
1407 static void
1408 copy_dir(struct direct *dir, struct direct *dbuf)
1409 {
1410 memcpy(dbuf, dir, DIRSIZ(Oflag == 0, dir, 0));
1411 if (needswap) {
1412 dbuf->d_ino = bswap32(dir->d_ino);
1413 dbuf->d_reclen = bswap16(dir->d_reclen);
1414 if (Oflag == 0)
1415 ((struct odirect*)dbuf)->d_namlen =
1416 bswap16(((struct odirect*)dir)->d_namlen);
1417 }
1418 }
1419
1420 static int
1421 ilog2(int val)
1422 {
1423 u_int n;
1424
1425 for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
1426 if (1 << n == val)
1427 return (n);
1428 errx(1, "ilog2: %d is not a power of 2\n", val);
1429 }
1430
1431 static void
1432 zap_old_sblock(int sblkoff)
1433 {
1434 static int cg0_data;
1435 uint32_t oldfs[SBLOCKSIZE / 4];
1436 static const struct fsm {
1437 uint32_t offset;
1438 uint32_t magic;
1439 uint32_t mask;
1440 } fs_magics[] = {
1441 {offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
1442 {offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
1443 {0, 0x70162, ~0u}, /* LFS_MAGIC */
1444 {14, 0xef53, 0xffff}, /* EXT2FS (little) */
1445 {14, 0xef530000, 0xffff0000}, /* EXT2FS (big) */
1446 {~0u},
1447 };
1448 const struct fsm *fsm;
1449
1450 if (Nflag)
1451 return;
1452
1453 if (sblkoff == 0) /* Why did UFS2 add support for this? sigh. */
1454 return;
1455
1456 if (cg0_data == 0)
1457 /* For FFSv1 this could include all the inodes. */
1458 cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
1459
1460 /* Ignore anything that is beyond our filesystem */
1461 if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
1462 return;
1463 /* Zero anything inside our filesystem... */
1464 if (sblkoff >= sblock.fs_sblockloc) {
1465 /* ...unless we will write that area anyway */
1466 if (sblkoff >= cg0_data)
1467 wtfs(sblkoff / sectorsize,
1468 roundup(sizeof sblock, sectorsize), iobuf);
1469 return;
1470 }
1471
1472 /* The sector might contain boot code, so we must validate it */
1473 rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1474 for (fsm = fs_magics; ; fsm++) {
1475 uint32_t v;
1476 if (fsm->mask == 0)
1477 return;
1478 v = oldfs[fsm->offset];
1479 if ((v & fsm->mask) == fsm->magic ||
1480 (bswap32(v) & fsm->mask) == fsm->magic)
1481 break;
1482 }
1483
1484 /* Just zap the magic number */
1485 oldfs[fsm->offset] = 0;
1486 wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1487 }
1488
1489
1490 #ifdef MFS
1491 /*
1492 * XXX!
1493 * Attempt to guess how much more space is available for process data. The
1494 * heuristic we use is
1495 *
1496 * max_data_limit - (sbrk(0) - etext) - 128kB
1497 *
1498 * etext approximates that start address of the data segment, and the 128kB
1499 * allows some slop for both segment gap between text and data, and for other
1500 * (libc) malloc usage.
1501 */
1502 static void
1503 calc_memfree(void)
1504 {
1505 extern char etext;
1506 struct rlimit rlp;
1507 u_long base;
1508
1509 base = (u_long)sbrk(0) - (u_long)&etext;
1510 if (getrlimit(RLIMIT_DATA, &rlp) < 0)
1511 perror("getrlimit");
1512 rlp.rlim_cur = rlp.rlim_max;
1513 if (setrlimit(RLIMIT_DATA, &rlp) < 0)
1514 perror("setrlimit");
1515 memleft = rlp.rlim_max - base - (128 * 1024);
1516 }
1517
1518 /*
1519 * Internal version of malloc that trims the requested size if not enough
1520 * memory is available.
1521 */
1522 static void *
1523 mkfs_malloc(size_t size)
1524 {
1525 u_long pgsz;
1526
1527 if (size == 0)
1528 return (NULL);
1529 if (memleft == 0)
1530 calc_memfree();
1531
1532 pgsz = getpagesize() - 1;
1533 size = (size + pgsz) &~ pgsz;
1534 if (size > memleft)
1535 size = memleft;
1536 memleft -= size;
1537 return (mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1538 -1, 0));
1539 }
1540 #endif /* MFS */
1541