OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.24/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [mkfs.c] - Blame information for rev 211

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*      $NetBSD: mkfs.c,v 1.22 2011/10/09 21:33:43 christos 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. Neither the name of the University nor the names of its contributors
25
 *    may be used to endorse or promote products derived from this software
26
 *    without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
 * SUCH DAMAGE.
39
 */
40
 
41
#if HAVE_NBTOOL_CONFIG_H
42
#include "nbtool_config.h"
43
#endif
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
#ifdef __RCSID
51
__RCSID("$NetBSD: mkfs.c,v 1.22 2011/10/09 21:33:43 christos Exp $");
52
#endif
53
#endif
54
#endif /* not lint */
55
 
56
#include <sys/param.h>
57
#include <sys/time.h>
58
#include <sys/resource.h>
59
 
60
#include <stdio.h>
61
#include <stdlib.h>
62
#include <string.h>
63
#include <unistd.h>
64
#include <errno.h>
65
 
66
#include "common.h"
67
#include "makefs.h"
68
#include "ffs.h"
69
 
70
#include "dinode.h"
71
#include "ufs_bswap.h"
72
#include "fs.h"
73
 
74
#include "ufs_inode.h"
75
#include "ffs_extern.h"
76
#include "newfs_extern.h"
77
 
78
static void initcg(int, time_t, const fsinfo_t *);
79
static int ilog2(int);
80
 
81
static int count_digits(int);
82
 
83
/*
84
 * make file system for cylinder-group style file systems
85
 */
86
#define UMASK           0755
87
#define POWEROF2(num)   (((num) & ((num) - 1)) == 0)
88
 
89
union {
90
        struct fs fs;
91
        char pad[SBLOCKSIZE];
92
} fsun;
93
#define sblock  fsun.fs
94
struct  csum *fscs;
95
 
96
union {
97
        struct cg cg;
98
        char pad[FFS_MAXBSIZE];
99
} cgun;
100
#define acg     cgun.cg
101
 
102
char *iobuf;
103
int iobufsize;
104
 
105
char writebuf[FFS_MAXBSIZE];
106
 
107
static int     Oflag;      /* format as an 4.3BSD file system */
108
static int64_t fssize;     /* file system size */
109
static int     sectorsize;         /* bytes/sector */
110
static int     fsize;      /* fragment size */
111
static int     bsize;      /* block size */
112
static int     maxbsize;   /* maximum clustering */
113
static int     maxblkspercg;
114
static int     minfree;    /* free space threshold */
115
static int     opt;                /* optimization preference (space or time) */
116
static int     density;    /* number of bytes per inode */
117
static int     maxcontig;          /* max contiguous blocks to allocate */
118
static int     maxbpg;     /* maximum blocks per file in a cyl group */
119
static int     bbsize;     /* boot block size */
120
static int     sbsize;     /* superblock size */
121
static int     avgfilesize;        /* expected average file size */
122
static int     avgfpdir;           /* expected number of files per directory */
123
 
124
struct fs *
125
ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
126
{
127
        int fragsperinode, optimalfpg, origdensity, minfpg, lastminfpg;
128
        int32_t cylno, i, csfrags;
129
        long long sizepb;
130
        void *space;
131 167 hellwig
        int size;
132
        /* int blks; !!!!! HG: not used */
133 17 hellwig
        int nprintcols, printcolwidth;
134
        ffs_opt_t       *ffs_opts = fsopts->fs_specific;
135
 
136
        Oflag =         ffs_opts->version;
137
        fssize =        fsopts->size / fsopts->sectorsize;
138
        sectorsize =    fsopts->sectorsize;
139
        fsize =         ffs_opts->fsize;
140
        bsize =         ffs_opts->bsize;
141
        maxbsize =      ffs_opts->maxbsize;
142
        maxblkspercg =  ffs_opts->maxblkspercg;
143
        minfree =       ffs_opts->minfree;
144
        opt =           ffs_opts->optimization;
145
        density =       ffs_opts->density;
146
        maxcontig =     ffs_opts->maxcontig;
147
        maxbpg =        ffs_opts->maxbpg;
148
        avgfilesize =   ffs_opts->avgfilesize;
149
        avgfpdir =      ffs_opts->avgfpdir;
150
        bbsize =        BBSIZE;
151
        sbsize =        SBLOCKSIZE;
152
 
153
        /* !!!!! HG: */
154
        //strlcpy((char *)sblock.fs_volname, ffs_opts->label,
155
        //    sizeof(sblock.fs_volname));
156
        strncpy((char *)sblock.fs_volname, ffs_opts->label,
157
            sizeof(sblock.fs_volname));
158 174 hellwig
        /* :HG !!!!! */
159 17 hellwig
 
160
        if (Oflag == 0) {
161
                sblock.fs_old_inodefmt = FS_42INODEFMT;
162
                sblock.fs_maxsymlinklen = 0;
163
                sblock.fs_old_flags = 0;
164
        } else {
165
                sblock.fs_old_inodefmt = FS_44INODEFMT;
166
                sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
167
                    MAXSYMLINKLEN_UFS2);
168
                sblock.fs_old_flags = FS_FLAGS_UPDATED;
169
                sblock.fs_flags = 0;
170
        }
171
        /*
172
         * Validate the given file system size.
173
         * Verify that its last block can actually be accessed.
174
         * Convert to file system fragment sized units.
175
         */
176
        if (fssize <= 0) {
177
                printf("preposterous size %lld\n", (long long)fssize);
178
                exit(13);
179
        }
180
        ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
181
 
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 > FFS_MAXBSIZE) {
219
                printf("block size %d is too large, maximum is %d\n",
220
                    sblock.fs_bsize, FFS_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
 
229
        if (maxbsize < bsize || !POWEROF2(maxbsize)) {
230
                sblock.fs_maxbsize = sblock.fs_bsize;
231
                printf("Extent size set to %d\n", sblock.fs_maxbsize);
232
        } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
233
                sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
234
                printf("Extent size reduced to %d\n", sblock.fs_maxbsize);
235
        } else {
236
                sblock.fs_maxbsize = maxbsize;
237
        }
238
        sblock.fs_maxcontig = maxcontig;
239
        if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
240
                sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
241
                printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
242
        }
243
 
244
        if (sblock.fs_maxcontig > 1)
245
                sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
246
 
247
        sblock.fs_bmask = ~(sblock.fs_bsize - 1);
248
        sblock.fs_fmask = ~(sblock.fs_fsize - 1);
249
        sblock.fs_qbmask = ~sblock.fs_bmask;
250
        sblock.fs_qfmask = ~sblock.fs_fmask;
251
        for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
252
                sblock.fs_bshift++;
253
        for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
254
                sblock.fs_fshift++;
255
        sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
256
        for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
257
                sblock.fs_fragshift++;
258
        if (sblock.fs_frag > MAXFRAG) {
259
                printf("fragment size %d is too small, "
260
                        "minimum with block size %d is %d\n",
261
                    sblock.fs_fsize, sblock.fs_bsize,
262
                    sblock.fs_bsize / MAXFRAG);
263
                exit(21);
264
        }
265
        sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
266
        sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
267
 
268
        if (Oflag <= 1) {
269
                sblock.fs_magic = FS_UFS1_MAGIC;
270
                sblock.fs_sblockloc = SBLOCK_UFS1;
271
                sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
272
                sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
273
                sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
274
                    sizeof (int32_t));
275
                sblock.fs_old_inodefmt = FS_44INODEFMT;
276
                sblock.fs_old_cgoffset = 0;
277
                sblock.fs_old_cgmask = 0xffffffff;
278
                sblock.fs_old_size = sblock.fs_size;
279
                sblock.fs_old_rotdelay = 0;
280
                sblock.fs_old_rps = 60;
281
                sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
282
                sblock.fs_old_cpg = 1;
283
                sblock.fs_old_interleave = 1;
284
                sblock.fs_old_trackskew = 0;
285
                sblock.fs_old_cpc = 0;
286
                sblock.fs_old_postblformat = 1;
287
                sblock.fs_old_nrpos = 1;
288
        } else {
289
                sblock.fs_magic = FS_UFS2_MAGIC;
290
#if 0 /* XXX makefs is used for small filesystems. */
291
                sblock.fs_sblockloc = SBLOCK_UFS2;
292
#else
293
                sblock.fs_sblockloc = SBLOCK_UFS1;
294
#endif
295
                sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
296
                sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
297
                sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
298
                    sizeof (int64_t));
299
        }
300
 
301
        sblock.fs_sblkno =
302
            roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
303
                sblock.fs_frag);
304
        sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
305
            roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
306
        sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
307
        sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
308
        for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
309
                sizepb *= NINDIR(&sblock);
310
                sblock.fs_maxfilesize += sizepb;
311
        }
312
 
313
        /*
314
         * Calculate the number of blocks to put into each cylinder group.
315
         *
316
         * This algorithm selects the number of blocks per cylinder
317
         * group. The first goal is to have at least enough data blocks
318
         * in each cylinder group to meet the density requirement. Once
319
         * this goal is achieved we try to expand to have at least
320
         * 1 cylinder group. Once this goal is achieved, we pack as
321
         * many blocks into each cylinder group map as will fit.
322
         *
323
         * We start by calculating the smallest number of blocks that we
324
         * can put into each cylinder group. If this is too big, we reduce
325
         * the density until it fits.
326
         */
327
        origdensity = density;
328
        for (;;) {
329
                fragsperinode = MAX(numfrags(&sblock, density), 1);
330
                minfpg = fragsperinode * INOPB(&sblock);
331
                if (minfpg > sblock.fs_size)
332
                        minfpg = sblock.fs_size;
333
                sblock.fs_ipg = INOPB(&sblock);
334
                sblock.fs_fpg = roundup(sblock.fs_iblkno +
335
                    sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
336
                if (sblock.fs_fpg < minfpg)
337
                        sblock.fs_fpg = minfpg;
338
                sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
339
                    INOPB(&sblock));
340
                sblock.fs_fpg = roundup(sblock.fs_iblkno +
341
                    sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
342
                if (sblock.fs_fpg < minfpg)
343
                        sblock.fs_fpg = minfpg;
344
                sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
345
                    INOPB(&sblock));
346
                if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
347
                        break;
348
                density -= sblock.fs_fsize;
349
        }
350
        if (density != origdensity)
351
                printf("density reduced from %d to %d\n", origdensity, density);
352
 
353
        if (maxblkspercg <= 0 || maxblkspercg >= fssize)
354
                maxblkspercg = fssize - 1;
355
        /*
356
         * Start packing more blocks into the cylinder group until
357
         * it cannot grow any larger, the number of cylinder groups
358
         * drops below 1, or we reach the size requested.
359
         */
360
        for ( ; sblock.fs_fpg < maxblkspercg; sblock.fs_fpg += sblock.fs_frag) {
361
                sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
362
                    INOPB(&sblock));
363
                if (sblock.fs_size / sblock.fs_fpg < 1)
364
                        break;
365
                if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize)
366
                        continue;
367
                if (CGSIZE(&sblock) == (unsigned long)sblock.fs_bsize)
368
                        break;
369
                sblock.fs_fpg -= sblock.fs_frag;
370
                sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
371
                    INOPB(&sblock));
372
                break;
373
        }
374
        /*
375
         * Check to be sure that the last cylinder group has enough blocks
376
         * to be viable. If it is too small, reduce the number of blocks
377
         * per cylinder group which will have the effect of moving more
378
         * blocks into the last cylinder group.
379
         */
380
        optimalfpg = sblock.fs_fpg;
381
        for (;;) {
382
                sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg);
383
                lastminfpg = roundup(sblock.fs_iblkno +
384
                    sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag);
385
                if (sblock.fs_size < lastminfpg) {
386
                        printf("Filesystem size %lld < minimum size of %d\n",
387
                            (long long)sblock.fs_size, lastminfpg);
388
                        exit(28);
389
                }
390
                if (sblock.fs_size % sblock.fs_fpg >= lastminfpg ||
391
                    sblock.fs_size % sblock.fs_fpg == 0)
392
                        break;
393
                sblock.fs_fpg -= sblock.fs_frag;
394
                sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode),
395
                    INOPB(&sblock));
396
        }
397
        if (optimalfpg != sblock.fs_fpg)
398
                printf("Reduced frags per cylinder group from %d to %d %s\n",
399
                   optimalfpg, sblock.fs_fpg, "to enlarge last cyl group");
400
        sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
401
        sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
402
        if (Oflag <= 1) {
403
                sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
404
                sblock.fs_old_nsect = sblock.fs_old_spc;
405
                sblock.fs_old_npsect = sblock.fs_old_spc;
406
                sblock.fs_old_ncyl = sblock.fs_ncg;
407
        }
408
 
409
        /*
410
         * fill in remaining fields of the super block
411
         */
412
        sblock.fs_csaddr = cgdmin(&sblock, 0);
413
        sblock.fs_cssize =
414
            fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
415
 
416
        /*
417
         * Setup memory for temporary in-core cylgroup summaries.
418
         * Cribbed from ffs_mountfs().
419
         */
420
        size = sblock.fs_cssize;
421 167 hellwig
        /* blks = howmany(size, sblock.fs_fsize); !!!!! HG: not used */
422 17 hellwig
        if (sblock.fs_contigsumsize > 0)
423
                size += sblock.fs_ncg * sizeof(int32_t);
424
        if ((space = (char *)calloc(1, size)) == NULL)
425
                err(1, "memory allocation error for cg summaries");
426
        sblock.fs_csp = space;
427
        space = (char *)space + sblock.fs_cssize;
428
        if (sblock.fs_contigsumsize > 0) {
429
                int32_t *lp;
430
 
431
                sblock.fs_maxcluster = lp = space;
432
                for (i = 0; i < sblock.fs_ncg; i++)
433
                *lp++ = sblock.fs_contigsumsize;
434
        }
435
 
436
        sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
437
        if (sblock.fs_sbsize > SBLOCKSIZE)
438
                sblock.fs_sbsize = SBLOCKSIZE;
439
        sblock.fs_minfree = minfree;
440
        sblock.fs_maxcontig = maxcontig;
441
        sblock.fs_maxbpg = maxbpg;
442
        sblock.fs_optim = opt;
443
        sblock.fs_cgrotor = 0;
444
        sblock.fs_pendingblocks = 0;
445
        sblock.fs_pendinginodes = 0;
446
        sblock.fs_cstotal.cs_ndir = 0;
447
        sblock.fs_cstotal.cs_nbfree = 0;
448
        sblock.fs_cstotal.cs_nifree = 0;
449
        sblock.fs_cstotal.cs_nffree = 0;
450
        sblock.fs_fmod = 0;
451
        sblock.fs_ronly = 0;
452
        sblock.fs_state = 0;
453
        sblock.fs_clean = FS_ISCLEAN;
454
        sblock.fs_ronly = 0;
455
        sblock.fs_id[0] = start_time.tv_sec;
456
        sblock.fs_id[1] = random();
457
        sblock.fs_fsmnt[0] = '\0';
458
        csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
459
        sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
460
            sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
461
        sblock.fs_cstotal.cs_nbfree =
462
            fragstoblks(&sblock, sblock.fs_dsize) -
463
            howmany(csfrags, sblock.fs_frag);
464
        sblock.fs_cstotal.cs_nffree =
465
            fragnum(&sblock, sblock.fs_size) +
466
            (fragnum(&sblock, csfrags) > 0 ?
467
            sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
468
        sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
469
        sblock.fs_cstotal.cs_ndir = 0;
470
        sblock.fs_dsize -= csfrags;
471
        sblock.fs_time = start_time.tv_sec;
472
        if (Oflag <= 1) {
473
                sblock.fs_old_time = start_time.tv_sec;
474
                sblock.fs_old_dsize = sblock.fs_dsize;
475
                sblock.fs_old_csaddr = sblock.fs_csaddr;
476
                sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
477
                sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
478
                sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
479
                sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
480
        }
481
        /*
482
         * Dump out summary information about file system.
483
         */
484
#define B2MBFACTOR (1 / (1024.0 * 1024.0))
485
        printf("%s: %.1fMB (%lld sectors) block size %d, "
486
               "fragment size %d\n",
487
            fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
488
            (long long)fsbtodb(&sblock, sblock.fs_size),
489
            sblock.fs_bsize, sblock.fs_fsize);
490
        printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
491
               "%d inodes.\n",
492
            sblock.fs_ncg,
493
            (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
494
            sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
495
#undef B2MBFACTOR
496
        /*
497
         * Now determine how wide each column will be, and calculate how
498
         * many columns will fit in a 76 char line. 76 is the width of the
499
         * subwindows in sysinst.
500
         */
501
        printcolwidth = count_digits(
502
                        fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
503
        nprintcols = 76 / (printcolwidth + 2);
504
 
505
        /*
506
         * allocate space for superblock, cylinder group map, and
507
         * two sets of inode blocks.
508
         */
509
        if (sblock.fs_bsize < SBLOCKSIZE)
510
                iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
511
        else
512
                iobufsize = 4 * sblock.fs_bsize;
513
        if ((iobuf = malloc(iobufsize)) == 0) {
514
                printf("Cannot allocate I/O buffer\n");
515
                exit(38);
516
        }
517
        memset(iobuf, 0, iobufsize);
518
        /*
519
         * Make a copy of the superblock into the buffer that we will be
520
         * writing out in each cylinder group.
521
         */
522
        memcpy(writebuf, &sblock, sbsize);
523
        if (fsopts->needswap)
524
                ffs_sb_swap(&sblock, (struct fs*)writebuf);
525
        memcpy(iobuf, writebuf, SBLOCKSIZE);
526
 
527
        printf("super-block backups (for fsck -b #) at:");
528
        for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
529
                initcg(cylno, start_time.tv_sec, fsopts);
530
                if (cylno % nprintcols == 0)
531
                        printf("\n");
532
                printf(" %*lld,", printcolwidth,
533
                        (long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
534
                fflush(stdout);
535
        }
536
        printf("\n");
537
 
538
        /*
539
         * Now construct the initial file system,
540
         * then write out the super-block.
541
         */
542
        sblock.fs_time = start_time.tv_sec;
543
        if (Oflag <= 1) {
544
                sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
545
                sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
546
                sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
547
                sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
548
        }
549
        if (fsopts->needswap)
550
                sblock.fs_flags |= FS_SWAPPED;
551
        ffs_write_superblock(&sblock, fsopts);
552
        return (&sblock);
553
}
554
 
555
/*
556
 * Write out the superblock and its duplicates,
557
 * and the cylinder group summaries
558
 */
559
void
560
ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
561
{
562
        int cylno, size, blks, i, saveflag;
563
        void *space;
564
        char *wrbuf;
565
 
566
        saveflag = fs->fs_flags & FS_INTERNAL;
567
        fs->fs_flags &= ~FS_INTERNAL;
568
 
569
        memcpy(writebuf, &sblock, sbsize);
570
        if (fsopts->needswap)
571
                ffs_sb_swap(fs, (struct fs*)writebuf);
572
        ffs_wtfs(fs->fs_sblockloc / sectorsize, sbsize, writebuf, fsopts);
573
 
574
        /* Write out the duplicate super blocks */
575
        for (cylno = 0; cylno < fs->fs_ncg; cylno++)
576
                ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
577
                    sbsize, writebuf, fsopts);
578
 
579
        /* Write out the cylinder group summaries */
580
        size = fs->fs_cssize;
581
        blks = howmany(size, fs->fs_fsize);
582
        space = (void *)fs->fs_csp;
583
        if ((wrbuf = malloc(size)) == NULL)
584
                err(1, "ffs_write_superblock: malloc %d", size);
585
        for (i = 0; i < blks; i+= fs->fs_frag) {
586
                size = fs->fs_bsize;
587
                if (i + fs->fs_frag > blks)
588
                        size = (blks - i) * fs->fs_fsize;
589
                if (fsopts->needswap)
590
                        ffs_csum_swap((struct csum *)space,
591
                            (struct csum *)wrbuf, size);
592
                else
593
                        memcpy(wrbuf, space, (u_int)size);
594
                ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
595
                space = (char *)space + size;
596
        }
597
        free(wrbuf);
598
        fs->fs_flags |= saveflag;
599
}
600
 
601
/*
602
 * Initialize a cylinder group.
603
 */
604
static void
605
initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
606
{
607
        daddr_t cbase, dmax;
608
        int32_t i, j, d, dlower, dupper, blkno;
609
        struct ufs1_dinode *dp1;
610
        struct ufs2_dinode *dp2;
611
        int start;
612
 
613
        /*
614
         * Determine block bounds for cylinder group.
615
         * Allow space for super block summary information in first
616
         * cylinder group.
617
         */
618
        cbase = cgbase(&sblock, cylno);
619
        dmax = cbase + sblock.fs_fpg;
620
        if (dmax > sblock.fs_size)
621
                dmax = sblock.fs_size;
622
        dlower = cgsblock(&sblock, cylno) - cbase;
623
        dupper = cgdmin(&sblock, cylno) - cbase;
624
        if (cylno == 0)
625
                dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
626
        memset(&acg, 0, sblock.fs_cgsize);
627
        acg.cg_time = utime;
628
        acg.cg_magic = CG_MAGIC;
629
        acg.cg_cgx = cylno;
630
        acg.cg_niblk = sblock.fs_ipg;
631
        acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
632
            sblock.fs_ipg : 2 * INOPB(&sblock);
633
        acg.cg_ndblk = dmax - cbase;
634
        if (sblock.fs_contigsumsize > 0)
635
                acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
636
        start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
637
        if (Oflag == 2) {
638
                acg.cg_iusedoff = start;
639
        } else {
640
                if (cylno == sblock.fs_ncg - 1)
641
                        acg.cg_old_ncyl = howmany(acg.cg_ndblk,
642
                            sblock.fs_fpg / sblock.fs_old_cpg);
643
                else
644
                        acg.cg_old_ncyl = sblock.fs_old_cpg;
645
                acg.cg_old_time = acg.cg_time;
646
                acg.cg_time = 0;
647
                acg.cg_old_niblk = acg.cg_niblk;
648
                acg.cg_niblk = 0;
649
                acg.cg_initediblk = 0;
650
                acg.cg_old_btotoff = start;
651
                acg.cg_old_boff = acg.cg_old_btotoff +
652
                    sblock.fs_old_cpg * sizeof(int32_t);
653
                acg.cg_iusedoff = acg.cg_old_boff +
654
                    sblock.fs_old_cpg * sizeof(u_int16_t);
655
        }
656
        acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
657
        if (sblock.fs_contigsumsize <= 0) {
658
                acg.cg_nextfreeoff = acg.cg_freeoff +
659
                   howmany(sblock.fs_fpg, CHAR_BIT);
660
        } else {
661
                acg.cg_clustersumoff = acg.cg_freeoff +
662
                    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
663
                acg.cg_clustersumoff =
664
                    roundup(acg.cg_clustersumoff, sizeof(int32_t));
665
                acg.cg_clusteroff = acg.cg_clustersumoff +
666
                    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
667
                acg.cg_nextfreeoff = acg.cg_clusteroff +
668
                    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
669
        }
670
        if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
671
                printf("Panic: cylinder group too big\n");
672
                exit(37);
673
        }
674
        acg.cg_cs.cs_nifree += sblock.fs_ipg;
675
        if (cylno == 0)
676
                for (i = 0; i < ROOTINO; i++) {
677
                        setbit(cg_inosused(&acg, 0), i);
678
                        acg.cg_cs.cs_nifree--;
679
                }
680
        if (cylno > 0) {
681
                /*
682
                 * In cylno 0, beginning space is reserved
683
                 * for boot and super blocks.
684
                 */
685
                for (d = 0, blkno = 0; d < dlower;) {
686
                        ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
687
                        if (sblock.fs_contigsumsize > 0)
688
                                setbit(cg_clustersfree(&acg, 0), blkno);
689
                        acg.cg_cs.cs_nbfree++;
690
                        d += sblock.fs_frag;
691
                        blkno++;
692
                }
693
        }
694
        if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
695
                acg.cg_frsum[sblock.fs_frag - i]++;
696
                for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
697
                        setbit(cg_blksfree(&acg, 0), dupper);
698
                        acg.cg_cs.cs_nffree++;
699
                }
700
        }
701
        for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
702
             d + sblock.fs_frag <= acg.cg_ndblk; ) {
703
                ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
704
                if (sblock.fs_contigsumsize > 0)
705
                        setbit(cg_clustersfree(&acg, 0), blkno);
706
                acg.cg_cs.cs_nbfree++;
707
                d += sblock.fs_frag;
708
                blkno++;
709
        }
710
        if (d < acg.cg_ndblk) {
711
                acg.cg_frsum[acg.cg_ndblk - d]++;
712
                for (; d < acg.cg_ndblk; d++) {
713
                        setbit(cg_blksfree(&acg, 0), d);
714
                        acg.cg_cs.cs_nffree++;
715
                }
716
        }
717
        if (sblock.fs_contigsumsize > 0) {
718
                int32_t *sump = cg_clustersum(&acg, 0);
719
                u_char *mapp = cg_clustersfree(&acg, 0);
720
                int map = *mapp++;
721
                int bit = 1;
722
                int run = 0;
723
 
724
                for (i = 0; i < acg.cg_nclusterblks; i++) {
725
                        if ((map & bit) != 0) {
726
                                run++;
727
                        } else if (run != 0) {
728
                                if (run > sblock.fs_contigsumsize)
729
                                        run = sblock.fs_contigsumsize;
730
                                sump[run]++;
731
                                run = 0;
732
                        }
733
                        if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
734
                                bit <<= 1;
735
                        } else {
736
                                map = *mapp++;
737
                                bit = 1;
738
                        }
739
                }
740
                if (run != 0) {
741
                        if (run > sblock.fs_contigsumsize)
742
                                run = sblock.fs_contigsumsize;
743
                        sump[run]++;
744
                }
745
        }
746
        sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
747
        /*
748
         * Write out the duplicate super block, the cylinder group map
749
         * and two blocks worth of inodes in a single write.
750
         */
751
        start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
752
        memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
753
        if (fsopts->needswap)
754
                ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
755
        start += sblock.fs_bsize;
756
        dp1 = (struct ufs1_dinode *)(&iobuf[start]);
757
        dp2 = (struct ufs2_dinode *)(&iobuf[start]);
758
        for (i = 0; i < acg.cg_initediblk; i++) {
759
                if (sblock.fs_magic == FS_UFS1_MAGIC) {
760
                        /* No need to swap, it'll stay random */
761
                        dp1->di_gen = random();
762
                        dp1++;
763
                } else {
764
                        dp2->di_gen = random();
765
                        dp2++;
766
                }
767
        }
768
        ffs_wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf,
769
            fsopts);
770
        /*
771
         * For the old file system, we have to initialize all the inodes.
772
         */
773
        if (Oflag <= 1) {
774
                for (i = 2 * sblock.fs_frag;
775
                     i < sblock.fs_ipg / INOPF(&sblock);
776
                     i += sblock.fs_frag) {
777
                        dp1 = (struct ufs1_dinode *)(&iobuf[start]);
778
                        for (j = 0; j < INOPB(&sblock); j++) {
779
                                dp1->di_gen = random();
780
                                dp1++;
781
                        }
782
                        ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
783
                            sblock.fs_bsize, &iobuf[start], fsopts);
784
                }
785
        }
786
}
787
 
788
/*
789
 * read a block from the file system
790
 */
791
void
792
ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
793
{
794
        int n;
795
        off_t offset;
796
 
797
        offset = bno;
798
        offset *= fsopts->sectorsize;
799
        if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
800
                err(1, "ffs_rdfs: seek error for sector %lld: %s\n",
801
                    (long long)bno, strerror(errno));
802
        n = read(fsopts->fd, bf, size);
803
        if (n == -1) {
804
                abort();
805
                err(1, "ffs_rdfs: read error bno %lld size %d", (long long)bno,
806
                    size);
807
        }
808
        else if (n != size)
809
                errx(1, "ffs_rdfs: read error for sector %lld: %s\n",
810
                    (long long)bno, strerror(errno));
811
}
812
 
813
/*
814
 * write a block to the file system
815
 */
816
void
817
ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
818
{
819
        int n;
820
        off_t offset;
821
 
822
        offset = bno;
823
        offset *= fsopts->sectorsize;
824
        if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
825
                err(1, "wtfs: seek error for sector %lld: %s\n",
826
                    (long long)bno, strerror(errno));
827
        n = write(fsopts->fd, bf, size);
828
        if (n == -1)
829
                err(1, "wtfs: write error for sector %lld: %s\n",
830
                    (long long)bno, strerror(errno));
831
        else if (n != size)
832
                errx(1, "wtfs: write error for sector %lld: %s\n",
833
                    (long long)bno, strerror(errno));
834
}
835
 
836
 
837
/* Determine how many digits are needed to print a given integer */
838
static int
839
count_digits(int num)
840
{
841
        int ndig;
842
 
843
        for(ndig = 1; num > 9; num /=10, ndig++);
844
 
845
        return (ndig);
846
}
847
 
848
static int
849
ilog2(int val)
850
{
851
        u_int n;
852
 
853
        for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
854
                if (1 << n == val)
855
                        return (n);
856
        errx(1, "ilog2: %d is not a power of 2\n", val);
857
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.