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