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