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