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