mkfs.c revision 1.3 1 1.3 lukem /* $NetBSD: mkfs.c,v 1.3 2002/01/07 05:07:51 lukem Exp $ */
2 1.1 lukem /* From NetBSD: mkfs.c,v 1.55 2001/09/06 02:16:01 lukem Exp $ */
3 1.1 lukem
4 1.1 lukem /*
5 1.1 lukem * Copyright (c) 1980, 1989, 1993
6 1.1 lukem * The Regents of the University of California. All rights reserved.
7 1.1 lukem *
8 1.1 lukem * Redistribution and use in source and binary forms, with or without
9 1.1 lukem * modification, are permitted provided that the following conditions
10 1.1 lukem * are met:
11 1.1 lukem * 1. Redistributions of source code must retain the above copyright
12 1.1 lukem * notice, this list of conditions and the following disclaimer.
13 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer in the
15 1.1 lukem * documentation and/or other materials provided with the distribution.
16 1.1 lukem * 3. All advertising materials mentioning features or use of this software
17 1.1 lukem * must display the following acknowledgement:
18 1.1 lukem * This product includes software developed by the University of
19 1.1 lukem * California, Berkeley and its contributors.
20 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
21 1.1 lukem * may be used to endorse or promote products derived from this software
22 1.1 lukem * without specific prior written permission.
23 1.1 lukem *
24 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 lukem * SUCH DAMAGE.
35 1.1 lukem */
36 1.1 lukem
37 1.1 lukem #include <sys/cdefs.h>
38 1.1 lukem #ifndef lint
39 1.1 lukem #if 0
40 1.1 lukem static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
41 1.1 lukem #else
42 1.3 lukem __RCSID("$NetBSD: mkfs.c,v 1.3 2002/01/07 05:07:51 lukem Exp $");
43 1.1 lukem #endif
44 1.1 lukem #endif /* not lint */
45 1.1 lukem
46 1.1 lukem #include <sys/param.h>
47 1.1 lukem #include <sys/time.h>
48 1.1 lukem #include <sys/resource.h>
49 1.1 lukem
50 1.1 lukem #include <err.h>
51 1.1 lukem #include <stdio.h>
52 1.1 lukem #include <stdlib.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <unistd.h>
55 1.1 lukem
56 1.3 lukem #include "makefs.h"
57 1.3 lukem
58 1.2 lukem #include "ufs/ufs/dir.h"
59 1.2 lukem #include "ufs/ufs/inode.h"
60 1.2 lukem #include "ufs/ufs/ufs_bswap.h"
61 1.2 lukem #include "ufs/ffs/fs.h"
62 1.1 lukem
63 1.1 lukem #include "ffs/ffs_extern.h"
64 1.1 lukem #include "ffs/newfs_extern.h"
65 1.1 lukem
66 1.1 lukem static void initcg(int, time_t, const fsinfo_t *);
67 1.1 lukem static int32_t calcipg(int32_t, int32_t, off_t *);
68 1.1 lukem static void swap_cg(struct cg *, struct cg *);
69 1.1 lukem
70 1.1 lukem static int count_digits(int);
71 1.1 lukem
72 1.1 lukem /*
73 1.1 lukem * make file system for cylinder-group style file systems
74 1.1 lukem */
75 1.1 lukem
76 1.1 lukem /*
77 1.1 lukem * We limit the size of the inode map to be no more than a
78 1.1 lukem * third of the cylinder group space, since we must leave at
79 1.1 lukem * least an equal amount of space for the block map.
80 1.1 lukem *
81 1.1 lukem * N.B.: MAXIPG must be a multiple of INOPB(fs).
82 1.1 lukem */
83 1.1 lukem #define MAXIPG(fs) roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
84 1.1 lukem
85 1.1 lukem #define UMASK 0755
86 1.1 lukem #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
87 1.1 lukem
88 1.1 lukem union {
89 1.1 lukem struct fs fs;
90 1.1 lukem char pad[SBSIZE];
91 1.1 lukem } fsun;
92 1.1 lukem #define sblock fsun.fs
93 1.1 lukem
94 1.1 lukem union {
95 1.1 lukem struct cg cg;
96 1.1 lukem char pad[MAXBSIZE];
97 1.1 lukem } cgun;
98 1.1 lukem #define acg cgun.cg
99 1.1 lukem
100 1.1 lukem struct dinode zino[MAXBSIZE / DINODE_SIZE];
101 1.1 lukem
102 1.1 lukem char writebuf[MAXBSIZE];
103 1.1 lukem
104 1.1 lukem static int Oflag; /* format as an 4.3BSD file system */
105 1.1 lukem static int fssize; /* file system size */
106 1.1 lukem static int ntracks; /* # tracks/cylinder */
107 1.1 lukem static int nsectors; /* # sectors/track */
108 1.1 lukem static int nphyssectors; /* # sectors/track including spares */
109 1.1 lukem static int secpercyl; /* sectors per cylinder */
110 1.1 lukem static int sectorsize; /* bytes/sector */
111 1.1 lukem static int rpm; /* revolutions/minute of drive */
112 1.1 lukem static int interleave; /* hardware sector interleave */
113 1.1 lukem static int trackskew; /* sector 0 skew, per track */
114 1.1 lukem static int fsize; /* fragment size */
115 1.1 lukem static int bsize; /* block size */
116 1.1 lukem static int cpg; /* cylinders/cylinder group */
117 1.1 lukem static int cpgflg; /* cylinders/cylinder group flag was given */
118 1.1 lukem static int minfree; /* free space threshold */
119 1.1 lukem static int opt; /* optimization preference (space or time) */
120 1.1 lukem static int density; /* number of bytes per inode */
121 1.1 lukem static int maxcontig; /* max contiguous blocks to allocate */
122 1.1 lukem static int rotdelay; /* rotational delay between blocks */
123 1.1 lukem static int maxbpg; /* maximum blocks per file in a cyl group */
124 1.1 lukem static int nrpos; /* # of distinguished rotational positions */
125 1.1 lukem static int bbsize; /* boot block size */
126 1.1 lukem static int sbsize; /* superblock size */
127 1.1 lukem static int avgfilesize; /* expected average file size */
128 1.1 lukem static int avgfpdir; /* expected number of files per directory */
129 1.1 lukem
130 1.1 lukem
131 1.1 lukem struct fs *
132 1.1 lukem ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
133 1.1 lukem {
134 1.1 lukem int32_t i, mincpc, mincpg, inospercg;
135 1.1 lukem int32_t cylno, rpos, blk, j, warned = 0;
136 1.1 lukem int32_t used, mincpgcnt, bpcg;
137 1.1 lukem off_t usedb;
138 1.1 lukem int32_t mapcramped, inodecramped;
139 1.1 lukem int32_t postblsize, rotblsize, totalsbsize;
140 1.1 lukem long long sizepb;
141 1.1 lukem void *space;
142 1.1 lukem int size, blks;
143 1.1 lukem int nprintcols, printcolwidth;
144 1.1 lukem
145 1.1 lukem Oflag = 0;
146 1.1 lukem fssize = fsopts->size / fsopts->sectorsize;
147 1.1 lukem ntracks = fsopts->ntracks;
148 1.1 lukem nsectors = fsopts->nsectors;
149 1.1 lukem nphyssectors = fsopts->nsectors; /* XXX: no trackspares */
150 1.1 lukem secpercyl = nsectors * ntracks;
151 1.1 lukem sectorsize = fsopts->sectorsize;
152 1.1 lukem rpm = fsopts->rpm;
153 1.1 lukem interleave = 1; /* XXX: HCD */
154 1.1 lukem trackskew = 0; /* XXX: HCD */
155 1.1 lukem fsize = fsopts->fsize;
156 1.1 lukem bsize = fsopts->bsize;
157 1.1 lukem cpg = fsopts->cpg;
158 1.1 lukem cpgflg = 1;
159 1.1 lukem minfree = fsopts->minfree;
160 1.1 lukem opt = fsopts->optimization;
161 1.1 lukem density = fsopts->density;
162 1.1 lukem maxcontig = fsopts->maxcontig;
163 1.1 lukem rotdelay = fsopts->rotdelay;
164 1.1 lukem maxbpg = fsopts->maxbpg;
165 1.1 lukem nrpos = fsopts->nrpos;
166 1.1 lukem bbsize = BBSIZE;
167 1.1 lukem sbsize = SBSIZE;
168 1.1 lukem avgfilesize = fsopts->avgfilesize;
169 1.1 lukem avgfpdir = fsopts->avgfpdir;
170 1.1 lukem
171 1.1 lukem if (Oflag) {
172 1.1 lukem sblock.fs_inodefmt = FS_42INODEFMT;
173 1.1 lukem sblock.fs_maxsymlinklen = 0;
174 1.1 lukem } else {
175 1.1 lukem sblock.fs_inodefmt = FS_44INODEFMT;
176 1.1 lukem sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
177 1.1 lukem }
178 1.1 lukem /*
179 1.1 lukem * Validate the given file system size.
180 1.1 lukem * Verify that its last block can actually be accessed.
181 1.1 lukem */
182 1.1 lukem if (fssize <= 0)
183 1.1 lukem printf("preposterous size %d\n", fssize), exit(13);
184 1.1 lukem ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
185 1.1 lukem
186 1.1 lukem /*
187 1.1 lukem * collect and verify the sector and track info
188 1.1 lukem */
189 1.1 lukem sblock.fs_nsect = nsectors;
190 1.1 lukem sblock.fs_ntrak = ntracks;
191 1.1 lukem if (sblock.fs_ntrak <= 0)
192 1.1 lukem printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
193 1.1 lukem if (sblock.fs_nsect <= 0)
194 1.1 lukem printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
195 1.1 lukem /*
196 1.1 lukem * collect and verify the filesystem density info
197 1.1 lukem */
198 1.1 lukem sblock.fs_avgfilesize = avgfilesize;
199 1.1 lukem sblock.fs_avgfpdir = avgfpdir;
200 1.1 lukem if (sblock.fs_avgfilesize <= 0)
201 1.1 lukem printf("illegal expected average file size %d\n",
202 1.1 lukem sblock.fs_avgfilesize), exit(14);
203 1.1 lukem if (sblock.fs_avgfpdir <= 0)
204 1.1 lukem printf("illegal expected number of files per directory %d\n",
205 1.1 lukem sblock.fs_avgfpdir), exit(15);
206 1.1 lukem /*
207 1.1 lukem * collect and verify the block and fragment sizes
208 1.1 lukem */
209 1.1 lukem sblock.fs_bsize = bsize;
210 1.1 lukem sblock.fs_fsize = fsize;
211 1.1 lukem if (!POWEROF2(sblock.fs_bsize)) {
212 1.1 lukem printf("block size must be a power of 2, not %d\n",
213 1.1 lukem sblock.fs_bsize);
214 1.1 lukem exit(16);
215 1.1 lukem }
216 1.1 lukem if (!POWEROF2(sblock.fs_fsize)) {
217 1.1 lukem printf("fragment size must be a power of 2, not %d\n",
218 1.1 lukem sblock.fs_fsize);
219 1.1 lukem exit(17);
220 1.1 lukem }
221 1.1 lukem if (sblock.fs_fsize < sectorsize) {
222 1.1 lukem printf("fragment size %d is too small, minimum is %d\n",
223 1.1 lukem sblock.fs_fsize, sectorsize);
224 1.1 lukem exit(18);
225 1.1 lukem }
226 1.1 lukem if (sblock.fs_bsize < MINBSIZE) {
227 1.1 lukem printf("block size %d is too small, minimum is %d\n",
228 1.1 lukem sblock.fs_bsize, MINBSIZE);
229 1.1 lukem exit(19);
230 1.1 lukem }
231 1.1 lukem if (sblock.fs_bsize < sblock.fs_fsize) {
232 1.1 lukem printf("block size (%d) cannot be smaller than fragment size (%d)\n",
233 1.1 lukem sblock.fs_bsize, sblock.fs_fsize);
234 1.1 lukem exit(20);
235 1.1 lukem }
236 1.1 lukem sblock.fs_bmask = ~(sblock.fs_bsize - 1);
237 1.1 lukem sblock.fs_fmask = ~(sblock.fs_fsize - 1);
238 1.1 lukem sblock.fs_qbmask = ~sblock.fs_bmask;
239 1.1 lukem sblock.fs_qfmask = ~sblock.fs_fmask;
240 1.1 lukem for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
241 1.1 lukem sblock.fs_bshift++;
242 1.1 lukem for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
243 1.1 lukem sblock.fs_fshift++;
244 1.1 lukem sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
245 1.1 lukem for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
246 1.1 lukem sblock.fs_fragshift++;
247 1.1 lukem if (sblock.fs_frag > MAXFRAG) {
248 1.1 lukem printf("fragment size %d is too small, "
249 1.1 lukem "minimum with block size %d is %d\n",
250 1.1 lukem sblock.fs_fsize, sblock.fs_bsize,
251 1.1 lukem sblock.fs_bsize / MAXFRAG);
252 1.1 lukem exit(21);
253 1.1 lukem }
254 1.1 lukem sblock.fs_nrpos = nrpos;
255 1.1 lukem sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
256 1.1 lukem sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
257 1.1 lukem sblock.fs_nspf = sblock.fs_fsize / sectorsize;
258 1.1 lukem for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
259 1.1 lukem sblock.fs_fsbtodb++;
260 1.1 lukem sblock.fs_sblkno =
261 1.1 lukem roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
262 1.1 lukem sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
263 1.1 lukem roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
264 1.1 lukem sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
265 1.1 lukem sblock.fs_cgoffset = roundup(
266 1.1 lukem howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
267 1.1 lukem for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
268 1.1 lukem sblock.fs_cgmask <<= 1;
269 1.1 lukem if (!POWEROF2(sblock.fs_ntrak))
270 1.1 lukem sblock.fs_cgmask <<= 1;
271 1.1 lukem sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
272 1.1 lukem for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
273 1.1 lukem sizepb *= NINDIR(&sblock);
274 1.1 lukem sblock.fs_maxfilesize += sizepb;
275 1.1 lukem }
276 1.1 lukem /*
277 1.1 lukem * Validate specified/determined secpercyl
278 1.1 lukem * and calculate minimum cylinders per group.
279 1.1 lukem */
280 1.1 lukem sblock.fs_spc = secpercyl;
281 1.1 lukem for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
282 1.1 lukem sblock.fs_cpc > 1 && (i & 1) == 0;
283 1.1 lukem sblock.fs_cpc >>= 1, i >>= 1)
284 1.1 lukem /* void */;
285 1.1 lukem mincpc = sblock.fs_cpc;
286 1.1 lukem bpcg = sblock.fs_spc * sectorsize;
287 1.1 lukem inospercg = roundup(bpcg / DINODE_SIZE, INOPB(&sblock));
288 1.1 lukem if (inospercg > MAXIPG(&sblock))
289 1.1 lukem inospercg = MAXIPG(&sblock);
290 1.1 lukem used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
291 1.1 lukem mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
292 1.1 lukem sblock.fs_spc);
293 1.1 lukem mincpg = roundup(mincpgcnt, mincpc);
294 1.1 lukem /*
295 1.1 lukem * Ensure that cylinder group with mincpg has enough space
296 1.1 lukem * for block maps.
297 1.1 lukem */
298 1.1 lukem sblock.fs_cpg = mincpg;
299 1.1 lukem sblock.fs_ipg = inospercg;
300 1.1 lukem if (maxcontig > 1)
301 1.1 lukem sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
302 1.1 lukem mapcramped = 0;
303 1.1 lukem while (CGSIZE(&sblock) > sblock.fs_bsize) {
304 1.1 lukem mapcramped = 1;
305 1.1 lukem if (sblock.fs_bsize < MAXBSIZE) {
306 1.1 lukem sblock.fs_bsize <<= 1;
307 1.1 lukem if ((i & 1) == 0) {
308 1.1 lukem i >>= 1;
309 1.1 lukem } else {
310 1.1 lukem sblock.fs_cpc <<= 1;
311 1.1 lukem mincpc <<= 1;
312 1.1 lukem mincpg = roundup(mincpgcnt, mincpc);
313 1.1 lukem sblock.fs_cpg = mincpg;
314 1.1 lukem }
315 1.1 lukem sblock.fs_frag <<= 1;
316 1.1 lukem sblock.fs_fragshift += 1;
317 1.1 lukem if (sblock.fs_frag <= MAXFRAG)
318 1.1 lukem continue;
319 1.1 lukem }
320 1.1 lukem if (sblock.fs_fsize == sblock.fs_bsize) {
321 1.1 lukem printf("There is no block size that");
322 1.1 lukem printf(" can support this disk\n");
323 1.1 lukem exit(22);
324 1.1 lukem }
325 1.1 lukem sblock.fs_frag >>= 1;
326 1.1 lukem sblock.fs_fragshift -= 1;
327 1.1 lukem sblock.fs_fsize <<= 1;
328 1.1 lukem sblock.fs_nspf <<= 1;
329 1.1 lukem }
330 1.1 lukem /*
331 1.1 lukem * Ensure that cylinder group with mincpg has enough space for inodes.
332 1.1 lukem */
333 1.1 lukem inodecramped = 0;
334 1.1 lukem inospercg = calcipg(mincpg, bpcg, &usedb);
335 1.1 lukem sblock.fs_ipg = inospercg;
336 1.1 lukem while (inospercg > MAXIPG(&sblock)) {
337 1.1 lukem inodecramped = 1;
338 1.1 lukem if (mincpc == 1 || sblock.fs_frag == 1 ||
339 1.1 lukem sblock.fs_bsize == MINBSIZE)
340 1.1 lukem break;
341 1.1 lukem printf("With a block size of %d %s %d\n", sblock.fs_bsize,
342 1.1 lukem "minimum bytes per inode is",
343 1.1 lukem (int)((mincpg * (off_t)bpcg - usedb)
344 1.1 lukem / MAXIPG(&sblock) + 1));
345 1.1 lukem sblock.fs_bsize >>= 1;
346 1.1 lukem sblock.fs_frag >>= 1;
347 1.1 lukem sblock.fs_fragshift -= 1;
348 1.1 lukem mincpc >>= 1;
349 1.1 lukem sblock.fs_cpg = roundup(mincpgcnt, mincpc);
350 1.1 lukem if (CGSIZE(&sblock) > sblock.fs_bsize) {
351 1.1 lukem sblock.fs_bsize <<= 1;
352 1.1 lukem break;
353 1.1 lukem }
354 1.1 lukem mincpg = sblock.fs_cpg;
355 1.1 lukem inospercg = calcipg(mincpg, bpcg, &usedb);
356 1.1 lukem sblock.fs_ipg = inospercg;
357 1.1 lukem }
358 1.1 lukem if (inodecramped) {
359 1.1 lukem if (inospercg > MAXIPG(&sblock)) {
360 1.1 lukem printf("Minimum bytes per inode is %d\n",
361 1.1 lukem (int)((mincpg * (off_t)bpcg - usedb)
362 1.1 lukem / MAXIPG(&sblock) + 1));
363 1.1 lukem } else if (!mapcramped) {
364 1.1 lukem printf("With %d bytes per inode, ", density);
365 1.1 lukem printf("minimum cylinders per group is %d\n", mincpg);
366 1.1 lukem }
367 1.1 lukem }
368 1.1 lukem if (mapcramped) {
369 1.1 lukem printf("With %d sectors per cylinder, ", sblock.fs_spc);
370 1.1 lukem printf("minimum cylinders per group is %d\n", mincpg);
371 1.1 lukem }
372 1.1 lukem if (inodecramped || mapcramped) {
373 1.1 lukem if (sblock.fs_bsize != bsize)
374 1.1 lukem printf("%s to be changed from %d to %d\n",
375 1.1 lukem "This requires the block size",
376 1.1 lukem bsize, sblock.fs_bsize);
377 1.1 lukem if (sblock.fs_fsize != fsize)
378 1.1 lukem printf("\t%s to be changed from %d to %d\n",
379 1.1 lukem "and the fragment size",
380 1.1 lukem fsize, sblock.fs_fsize);
381 1.1 lukem exit(23);
382 1.1 lukem }
383 1.1 lukem /*
384 1.1 lukem * Calculate the number of cylinders per group
385 1.1 lukem */
386 1.1 lukem sblock.fs_cpg = cpg;
387 1.1 lukem if (sblock.fs_cpg % mincpc != 0) {
388 1.1 lukem printf("%s groups must have a multiple of %d cylinders\n",
389 1.1 lukem cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
390 1.1 lukem sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
391 1.1 lukem if (!cpgflg)
392 1.1 lukem cpg = sblock.fs_cpg;
393 1.1 lukem }
394 1.1 lukem /*
395 1.1 lukem * Must ensure there is enough space for inodes.
396 1.1 lukem */
397 1.1 lukem sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
398 1.1 lukem while (sblock.fs_ipg > MAXIPG(&sblock)) {
399 1.1 lukem inodecramped = 1;
400 1.1 lukem sblock.fs_cpg -= mincpc;
401 1.1 lukem sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
402 1.1 lukem }
403 1.1 lukem /*
404 1.1 lukem * Must ensure there is enough space to hold block map.
405 1.1 lukem */
406 1.1 lukem while (CGSIZE(&sblock) > sblock.fs_bsize) {
407 1.1 lukem mapcramped = 1;
408 1.1 lukem sblock.fs_cpg -= mincpc;
409 1.1 lukem sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
410 1.1 lukem }
411 1.1 lukem sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
412 1.1 lukem if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
413 1.1 lukem printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
414 1.1 lukem exit(24);
415 1.1 lukem }
416 1.1 lukem if (sblock.fs_cpg < mincpg) {
417 1.1 lukem printf("cylinder groups must have at least %d cylinders\n",
418 1.1 lukem mincpg);
419 1.1 lukem exit(25);
420 1.1 lukem } else if (sblock.fs_cpg != cpg) {
421 1.1 lukem if (!cpgflg)
422 1.1 lukem printf("Warning: ");
423 1.1 lukem else if (!mapcramped && !inodecramped)
424 1.1 lukem exit(26);
425 1.1 lukem if (mapcramped && inodecramped)
426 1.1 lukem printf("Block size and bytes per inode restrict");
427 1.1 lukem else if (mapcramped)
428 1.1 lukem printf("Block size restricts");
429 1.1 lukem else
430 1.1 lukem printf("Bytes per inode restrict");
431 1.1 lukem printf(" cylinders per group to %d.\n", sblock.fs_cpg);
432 1.1 lukem if (cpgflg)
433 1.1 lukem exit(27);
434 1.1 lukem }
435 1.1 lukem sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
436 1.1 lukem /*
437 1.1 lukem * Now have size for file system and nsect and ntrak.
438 1.1 lukem * Determine number of cylinders and blocks in the file system.
439 1.1 lukem */
440 1.1 lukem sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
441 1.1 lukem sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
442 1.1 lukem if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
443 1.1 lukem sblock.fs_ncyl++;
444 1.1 lukem warned = 1;
445 1.1 lukem }
446 1.1 lukem if (sblock.fs_ncyl < 1) {
447 1.1 lukem printf("file systems must have at least one cylinder\n");
448 1.1 lukem exit(28);
449 1.1 lukem }
450 1.1 lukem /*
451 1.1 lukem * Determine feasability/values of rotational layout tables.
452 1.1 lukem *
453 1.1 lukem * The size of the rotational layout tables is limited by the
454 1.1 lukem * size of the superblock, SBSIZE. The amount of space available
455 1.1 lukem * for tables is calculated as (SBSIZE - sizeof (struct fs)).
456 1.1 lukem * The size of these tables is inversely proportional to the block
457 1.1 lukem * size of the file system. The size increases if sectors per track
458 1.1 lukem * are not powers of two, because more cylinders must be described
459 1.1 lukem * by the tables before the rotational pattern repeats (fs_cpc).
460 1.1 lukem */
461 1.1 lukem sblock.fs_interleave = interleave;
462 1.1 lukem sblock.fs_trackskew = trackskew;
463 1.1 lukem sblock.fs_npsect = nphyssectors;
464 1.1 lukem sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
465 1.1 lukem sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
466 1.1 lukem if (sblock.fs_ntrak == 1) {
467 1.1 lukem sblock.fs_cpc = 0;
468 1.1 lukem goto next;
469 1.1 lukem }
470 1.1 lukem postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
471 1.1 lukem rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
472 1.1 lukem totalsbsize = sizeof(struct fs) + rotblsize;
473 1.1 lukem if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
474 1.1 lukem /* use old static table space */
475 1.1 lukem sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
476 1.1 lukem (char *)(&sblock.fs_firstfield);
477 1.1 lukem sblock.fs_rotbloff = &sblock.fs_space[0] -
478 1.1 lukem (u_char *)(&sblock.fs_firstfield);
479 1.1 lukem } else {
480 1.1 lukem /* use dynamic table space */
481 1.1 lukem sblock.fs_postbloff = &sblock.fs_space[0] -
482 1.1 lukem (u_char *)(&sblock.fs_firstfield);
483 1.1 lukem sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
484 1.1 lukem totalsbsize += postblsize;
485 1.1 lukem }
486 1.1 lukem if (totalsbsize > SBSIZE ||
487 1.1 lukem sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
488 1.1 lukem printf("%s %s %d %s %d.%s",
489 1.1 lukem "Warning: insufficient space in super block for\n",
490 1.1 lukem "rotational layout tables with nsect", sblock.fs_nsect,
491 1.1 lukem "and ntrak", sblock.fs_ntrak,
492 1.1 lukem "\nFile system performance may be impaired.\n");
493 1.1 lukem sblock.fs_cpc = 0;
494 1.1 lukem goto next;
495 1.1 lukem }
496 1.1 lukem sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
497 1.1 lukem /*
498 1.1 lukem * calculate the available blocks for each rotational position
499 1.1 lukem */
500 1.1 lukem for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
501 1.1 lukem for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
502 1.1 lukem fs_postbl(&sblock, cylno)[rpos] = -1;
503 1.1 lukem for (i = (rotblsize - 1) * sblock.fs_frag;
504 1.1 lukem i >= 0; i -= sblock.fs_frag) {
505 1.1 lukem cylno = cbtocylno(&sblock, i);
506 1.1 lukem rpos = cbtorpos(&sblock, i);
507 1.1 lukem blk = fragstoblks(&sblock, i);
508 1.1 lukem if (fs_postbl(&sblock, cylno)[rpos] == -1)
509 1.1 lukem fs_rotbl(&sblock)[blk] = 0;
510 1.1 lukem else
511 1.1 lukem fs_rotbl(&sblock)[blk] = fs_postbl(&sblock, cylno)[rpos] - blk;
512 1.1 lukem fs_postbl(&sblock, cylno)[rpos] = blk;
513 1.1 lukem }
514 1.1 lukem next:
515 1.1 lukem /*
516 1.1 lukem * Compute/validate number of cylinder groups.
517 1.1 lukem */
518 1.1 lukem sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
519 1.1 lukem if (sblock.fs_ncyl % sblock.fs_cpg)
520 1.1 lukem sblock.fs_ncg++;
521 1.1 lukem sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
522 1.1 lukem i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
523 1.1 lukem if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
524 1.1 lukem printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
525 1.1 lukem cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
526 1.1 lukem sblock.fs_fpg / sblock.fs_frag);
527 1.1 lukem printf("number of cylinders per cylinder group (%d) %s.\n",
528 1.1 lukem sblock.fs_cpg, "must be increased");
529 1.1 lukem exit(29);
530 1.1 lukem }
531 1.1 lukem j = sblock.fs_ncg - 1;
532 1.1 lukem if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
533 1.1 lukem cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
534 1.1 lukem if (j == 0) {
535 1.1 lukem printf("File system must have at least %d sectors\n",
536 1.1 lukem NSPF(&sblock) *
537 1.1 lukem (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
538 1.1 lukem exit(30);
539 1.1 lukem }
540 1.1 lukem printf("Warning: inode blocks/cyl group (%d) >= "
541 1.1 lukem "data blocks (%d) in last\n",
542 1.1 lukem (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
543 1.1 lukem i / sblock.fs_frag);
544 1.1 lukem printf(" cylinder group. This implies %d sector(s) "
545 1.1 lukem "cannot be allocated.\n",
546 1.1 lukem i * NSPF(&sblock));
547 1.1 lukem sblock.fs_ncg--;
548 1.1 lukem sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
549 1.1 lukem sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
550 1.1 lukem NSPF(&sblock);
551 1.1 lukem warned = 0;
552 1.1 lukem }
553 1.1 lukem if (warned) {
554 1.1 lukem printf("Warning: %d sector(s) in last cylinder unallocated\n",
555 1.1 lukem sblock.fs_spc -
556 1.1 lukem (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
557 1.1 lukem * sblock.fs_spc));
558 1.1 lukem }
559 1.1 lukem /*
560 1.1 lukem * fill in remaining fields of the super block
561 1.1 lukem */
562 1.1 lukem sblock.fs_csaddr = cgdmin(&sblock, 0);
563 1.1 lukem sblock.fs_cssize =
564 1.1 lukem fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
565 1.1 lukem /*
566 1.1 lukem * The superblock fields 'fs_csmask' and 'fs_csshift' are no
567 1.1 lukem * longer used. However, we still initialise them so that the
568 1.1 lukem * filesystem remains compatible with old kernels.
569 1.1 lukem */
570 1.1 lukem i = sblock.fs_bsize / sizeof(struct csum);
571 1.1 lukem sblock.fs_csmask = ~(i - 1);
572 1.1 lukem for (sblock.fs_csshift = 0; i > 1; i >>= 1)
573 1.1 lukem sblock.fs_csshift++;
574 1.1 lukem
575 1.1 lukem /*
576 1.1 lukem * Setup memory for temporary in-core cylgroup summaries.
577 1.1 lukem * Cribbed from ffs_mountfs().
578 1.1 lukem */
579 1.1 lukem size = sblock.fs_cssize;
580 1.1 lukem blks = howmany(size, sblock.fs_fsize);
581 1.1 lukem if (sblock.fs_contigsumsize > 0)
582 1.1 lukem size += sblock.fs_ncg * sizeof(int32_t);
583 1.1 lukem if ((space = (char *)calloc(1, size)) == NULL)
584 1.1 lukem err(1, "memory allocation error for cg summaries");
585 1.1 lukem sblock.fs_csp = space;
586 1.1 lukem space = (char *)space + sblock.fs_cssize;
587 1.1 lukem if (sblock.fs_contigsumsize > 0) {
588 1.1 lukem int32_t *lp;
589 1.1 lukem
590 1.1 lukem sblock.fs_maxcluster = lp = space;
591 1.1 lukem for (i = 0; i < sblock.fs_ncg; i++)
592 1.1 lukem *lp++ = sblock.fs_contigsumsize;
593 1.1 lukem }
594 1.1 lukem
595 1.1 lukem sblock.fs_magic = FS_MAGIC;
596 1.1 lukem sblock.fs_rotdelay = rotdelay;
597 1.1 lukem sblock.fs_minfree = minfree;
598 1.1 lukem sblock.fs_maxcontig = maxcontig;
599 1.1 lukem sblock.fs_maxbpg = maxbpg;
600 1.1 lukem sblock.fs_rps = rpm / 60;
601 1.1 lukem sblock.fs_optim = opt;
602 1.1 lukem sblock.fs_cgrotor = 0;
603 1.1 lukem sblock.fs_cstotal.cs_ndir = 0;
604 1.1 lukem sblock.fs_cstotal.cs_nbfree = 0;
605 1.1 lukem sblock.fs_cstotal.cs_nifree = 0;
606 1.1 lukem sblock.fs_cstotal.cs_nffree = 0;
607 1.1 lukem sblock.fs_fmod = 0;
608 1.1 lukem sblock.fs_clean = FS_ISCLEAN;
609 1.1 lukem sblock.fs_ronly = 0;
610 1.1 lukem
611 1.1 lukem /*
612 1.1 lukem * Dump out summary information about file system.
613 1.1 lukem */
614 1.1 lukem printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
615 1.1 lukem fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
616 1.1 lukem "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
617 1.1 lukem #define B2MBFACTOR (1 / (1024.0 * 1024.0))
618 1.1 lukem printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
619 1.1 lukem (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
620 1.1 lukem sblock.fs_ncg, sblock.fs_cpg,
621 1.1 lukem (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
622 1.1 lukem sblock.fs_ipg);
623 1.1 lukem #undef B2MBFACTOR
624 1.1 lukem /*
625 1.1 lukem * Now determine how wide each column will be, and calculate how
626 1.1 lukem * many columns will fit in a 76 char line. 76 is the width of the
627 1.1 lukem * subwindows in sysinst.
628 1.1 lukem */
629 1.1 lukem printcolwidth = count_digits(
630 1.1 lukem fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
631 1.1 lukem nprintcols = 76 / (printcolwidth + 2);
632 1.1 lukem /*
633 1.1 lukem * Now build the cylinders group blocks and
634 1.1 lukem * then print out indices of cylinder groups.
635 1.1 lukem */
636 1.1 lukem printf("super-block backups (for fsck -b #) at:");
637 1.1 lukem for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
638 1.1 lukem initcg(cylno, start_time.tv_sec, fsopts);
639 1.1 lukem if (cylno % nprintcols == 0)
640 1.1 lukem printf("\n");
641 1.1 lukem printf(" %*d,", printcolwidth,
642 1.1 lukem fsbtodb(&sblock, cgsblock(&sblock, cylno)));
643 1.1 lukem fflush(stdout);
644 1.1 lukem }
645 1.1 lukem printf("\n");
646 1.1 lukem
647 1.1 lukem /*
648 1.1 lukem * Now construct the initial file system,
649 1.1 lukem * then write out the super-block.
650 1.1 lukem */
651 1.1 lukem sblock.fs_time = start_time.tv_sec;
652 1.1 lukem if (fsopts->needswap)
653 1.1 lukem sblock.fs_flags |= FS_SWAPPED;
654 1.1 lukem ffs_write_superblock(&sblock, fsopts);
655 1.1 lukem return (&sblock);
656 1.1 lukem }
657 1.1 lukem
658 1.1 lukem /*
659 1.1 lukem * Write out the superblock and its duplicates,
660 1.1 lukem * and the cylinder group summaries
661 1.1 lukem */
662 1.1 lukem void
663 1.1 lukem ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
664 1.1 lukem {
665 1.1 lukem int cylno, size, blks, i, saveflag;
666 1.1 lukem void *space;
667 1.1 lukem char *wrbuf;
668 1.1 lukem
669 1.1 lukem saveflag = fs->fs_flags & FS_INTERNAL;
670 1.1 lukem fs->fs_flags &= ~FS_INTERNAL;
671 1.1 lukem
672 1.1 lukem /* Write out the master super block */
673 1.1 lukem memcpy(writebuf, fs, sbsize);
674 1.1 lukem if (fsopts->needswap)
675 1.1 lukem ffs_sb_swap(fs, (struct fs*)writebuf);
676 1.1 lukem ffs_wtfs((int)SBOFF / sectorsize, sbsize, writebuf, fsopts);
677 1.1 lukem
678 1.1 lukem /* Write out the duplicate super blocks */
679 1.1 lukem for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
680 1.1 lukem ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
681 1.1 lukem sbsize, writebuf, fsopts);
682 1.1 lukem
683 1.1 lukem /* Write out the cylinder group summaries */
684 1.1 lukem size = fs->fs_cssize;
685 1.1 lukem blks = howmany(size, fs->fs_fsize);
686 1.1 lukem space = (void *)fs->fs_csp;
687 1.1 lukem if ((wrbuf = malloc(size)) == NULL)
688 1.1 lukem err(1, "ffs_write_superblock: malloc %d", size);
689 1.1 lukem for (i = 0; i < blks; i+= fs->fs_frag) {
690 1.1 lukem size = fs->fs_bsize;
691 1.1 lukem if (i + fs->fs_frag > blks)
692 1.1 lukem size = (blks - i) * fs->fs_fsize;
693 1.1 lukem if (fsopts->needswap)
694 1.1 lukem ffs_csum_swap((struct csum *)space,
695 1.1 lukem (struct csum *)wrbuf, size);
696 1.1 lukem else
697 1.1 lukem memcpy(wrbuf, space, (u_int)size);
698 1.1 lukem ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
699 1.1 lukem space = (char *)space + size;
700 1.1 lukem }
701 1.1 lukem free(wrbuf);
702 1.1 lukem fs->fs_flags |= saveflag;
703 1.1 lukem }
704 1.1 lukem
705 1.1 lukem
706 1.1 lukem /*
707 1.1 lukem * Initialize a cylinder group.
708 1.1 lukem */
709 1.1 lukem static void
710 1.1 lukem initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
711 1.1 lukem {
712 1.1 lukem daddr_t cbase, d, dlower, dupper, dmax, blkno;
713 1.1 lukem int32_t i;
714 1.1 lukem
715 1.1 lukem /*
716 1.1 lukem * Determine block bounds for cylinder group.
717 1.1 lukem * Allow space for super block summary information in first
718 1.1 lukem * cylinder group.
719 1.1 lukem */
720 1.1 lukem cbase = cgbase(&sblock, cylno);
721 1.1 lukem dmax = cbase + sblock.fs_fpg;
722 1.1 lukem if (dmax > sblock.fs_size)
723 1.1 lukem dmax = sblock.fs_size;
724 1.1 lukem dlower = cgsblock(&sblock, cylno) - cbase;
725 1.1 lukem dupper = cgdmin(&sblock, cylno) - cbase;
726 1.1 lukem if (cylno == 0)
727 1.1 lukem dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
728 1.1 lukem memset(&acg, 0, sblock.fs_cgsize);
729 1.1 lukem acg.cg_time = utime;
730 1.1 lukem acg.cg_magic = CG_MAGIC;
731 1.1 lukem acg.cg_cgx = cylno;
732 1.1 lukem if (cylno == sblock.fs_ncg - 1)
733 1.1 lukem acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
734 1.1 lukem else
735 1.1 lukem acg.cg_ncyl = sblock.fs_cpg;
736 1.1 lukem acg.cg_niblk = sblock.fs_ipg;
737 1.1 lukem acg.cg_ndblk = dmax - cbase;
738 1.1 lukem if (sblock.fs_contigsumsize > 0)
739 1.1 lukem acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
740 1.1 lukem acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
741 1.1 lukem acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
742 1.1 lukem acg.cg_iusedoff = acg.cg_boff +
743 1.1 lukem sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
744 1.1 lukem acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
745 1.1 lukem if (sblock.fs_contigsumsize <= 0) {
746 1.1 lukem acg.cg_nextfreeoff = acg.cg_freeoff +
747 1.1 lukem howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
748 1.1 lukem } else {
749 1.1 lukem acg.cg_clustersumoff = acg.cg_freeoff + howmany
750 1.1 lukem (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
751 1.1 lukem sizeof(int32_t);
752 1.1 lukem acg.cg_clustersumoff =
753 1.1 lukem roundup(acg.cg_clustersumoff, sizeof(int32_t));
754 1.1 lukem acg.cg_clusteroff = acg.cg_clustersumoff +
755 1.1 lukem (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
756 1.1 lukem acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
757 1.1 lukem (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
758 1.1 lukem }
759 1.1 lukem if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
760 1.1 lukem printf("Panic: cylinder group too big\n");
761 1.1 lukem exit(37);
762 1.1 lukem }
763 1.1 lukem acg.cg_cs.cs_nifree += sblock.fs_ipg;
764 1.1 lukem if (cylno == 0)
765 1.1 lukem for (i = 0; i < ROOTINO; i++) {
766 1.1 lukem setbit(cg_inosused(&acg, 0), i);
767 1.1 lukem acg.cg_cs.cs_nifree--;
768 1.1 lukem }
769 1.1 lukem for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
770 1.1 lukem ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
771 1.1 lukem sblock.fs_bsize, (char *)zino, fsopts);
772 1.1 lukem if (cylno > 0) {
773 1.1 lukem /*
774 1.1 lukem * In cylno 0, beginning space is reserved
775 1.1 lukem * for boot and super blocks.
776 1.1 lukem */
777 1.1 lukem for (d = 0; d < dlower; d += sblock.fs_frag) {
778 1.1 lukem blkno = d / sblock.fs_frag;
779 1.1 lukem ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
780 1.1 lukem if (sblock.fs_contigsumsize > 0)
781 1.1 lukem setbit(cg_clustersfree(&acg, 0), blkno);
782 1.1 lukem acg.cg_cs.cs_nbfree++;
783 1.1 lukem cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
784 1.1 lukem cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
785 1.1 lukem [cbtorpos(&sblock, d)]++;
786 1.1 lukem }
787 1.1 lukem sblock.fs_dsize += dlower;
788 1.1 lukem }
789 1.1 lukem sblock.fs_dsize += acg.cg_ndblk - dupper;
790 1.1 lukem if ((i = (dupper % sblock.fs_frag)) != 0) {
791 1.1 lukem acg.cg_frsum[sblock.fs_frag - i]++;
792 1.1 lukem for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
793 1.1 lukem setbit(cg_blksfree(&acg, 0), dupper);
794 1.1 lukem acg.cg_cs.cs_nffree++;
795 1.1 lukem }
796 1.1 lukem }
797 1.1 lukem for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
798 1.1 lukem blkno = d / sblock.fs_frag;
799 1.1 lukem ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
800 1.1 lukem if (sblock.fs_contigsumsize > 0)
801 1.1 lukem setbit(cg_clustersfree(&acg, 0), blkno);
802 1.1 lukem acg.cg_cs.cs_nbfree++;
803 1.1 lukem cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
804 1.1 lukem cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
805 1.1 lukem [cbtorpos(&sblock, d)]++;
806 1.1 lukem d += sblock.fs_frag;
807 1.1 lukem }
808 1.1 lukem if (d < dmax - cbase) {
809 1.1 lukem acg.cg_frsum[dmax - cbase - d]++;
810 1.1 lukem for (; d < dmax - cbase; d++) {
811 1.1 lukem setbit(cg_blksfree(&acg, 0), d);
812 1.1 lukem acg.cg_cs.cs_nffree++;
813 1.1 lukem }
814 1.1 lukem }
815 1.1 lukem if (sblock.fs_contigsumsize > 0) {
816 1.1 lukem int32_t *sump = cg_clustersum(&acg, 0);
817 1.1 lukem u_char *mapp = cg_clustersfree(&acg, 0);
818 1.1 lukem int map = *mapp++;
819 1.1 lukem int bit = 1;
820 1.1 lukem int run = 0;
821 1.1 lukem
822 1.1 lukem for (i = 0; i < acg.cg_nclusterblks; i++) {
823 1.1 lukem if ((map & bit) != 0) {
824 1.1 lukem run++;
825 1.1 lukem } else if (run != 0) {
826 1.1 lukem if (run > sblock.fs_contigsumsize)
827 1.1 lukem run = sblock.fs_contigsumsize;
828 1.1 lukem sump[run]++;
829 1.1 lukem run = 0;
830 1.1 lukem }
831 1.1 lukem if ((i & (NBBY - 1)) != (NBBY - 1)) {
832 1.1 lukem bit <<= 1;
833 1.1 lukem } else {
834 1.1 lukem map = *mapp++;
835 1.1 lukem bit = 1;
836 1.1 lukem }
837 1.1 lukem }
838 1.1 lukem if (run != 0) {
839 1.1 lukem if (run > sblock.fs_contigsumsize)
840 1.1 lukem run = sblock.fs_contigsumsize;
841 1.1 lukem sump[run]++;
842 1.1 lukem }
843 1.1 lukem }
844 1.1 lukem sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
845 1.1 lukem sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
846 1.1 lukem sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
847 1.1 lukem sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
848 1.1 lukem sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
849 1.1 lukem memcpy(writebuf, &acg, sblock.fs_bsize);
850 1.1 lukem if (fsopts->needswap)
851 1.1 lukem swap_cg(&acg, (struct cg*)writebuf);
852 1.1 lukem ffs_wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
853 1.1 lukem sblock.fs_bsize,
854 1.1 lukem writebuf, fsopts);
855 1.1 lukem }
856 1.1 lukem
857 1.1 lukem /*
858 1.1 lukem * Calculate number of inodes per group.
859 1.1 lukem */
860 1.1 lukem static int32_t
861 1.1 lukem calcipg(int32_t cylpg, int32_t bpcg, off_t *usedbp)
862 1.1 lukem {
863 1.1 lukem int i;
864 1.1 lukem int32_t ipg, new_ipg, ncg, ncyl;
865 1.1 lukem off_t usedb;
866 1.1 lukem
867 1.1 lukem /*
868 1.1 lukem * Prepare to scale by fssize / (number of sectors in cylinder groups).
869 1.1 lukem * Note that fssize is still in sectors, not file system blocks.
870 1.1 lukem */
871 1.1 lukem ncyl = howmany(fssize, secpercyl);
872 1.1 lukem ncg = howmany(ncyl, cylpg);
873 1.1 lukem /*
874 1.1 lukem * Iterate a few times to allow for ipg depending on itself.
875 1.1 lukem */
876 1.1 lukem ipg = 0;
877 1.1 lukem for (i = 0; i < 10; i++) {
878 1.1 lukem usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
879 1.1 lukem * NSPF(&sblock) * (off_t)sectorsize;
880 1.1 lukem if (cylpg * (long long)bpcg < usedb) {
881 1.1 lukem warnx("Too many inodes per cyl group!");
882 1.1 lukem return (MAXIPG(&sblock)+1);
883 1.1 lukem }
884 1.1 lukem new_ipg = (cylpg * (long long)bpcg - usedb) /
885 1.1 lukem ((long long)density * fssize / (ncg * secpercyl * cylpg));
886 1.1 lukem if (new_ipg <= 0)
887 1.1 lukem new_ipg = 1; /* ensure ipg > 0 */
888 1.1 lukem new_ipg = roundup(new_ipg, INOPB(&sblock));
889 1.1 lukem if (new_ipg == ipg)
890 1.1 lukem break;
891 1.1 lukem ipg = new_ipg;
892 1.1 lukem }
893 1.1 lukem *usedbp = usedb;
894 1.1 lukem return (ipg);
895 1.1 lukem }
896 1.1 lukem
897 1.1 lukem
898 1.1 lukem /*
899 1.1 lukem * read a block from the file system
900 1.1 lukem */
901 1.1 lukem void
902 1.1 lukem ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
903 1.1 lukem {
904 1.1 lukem int n;
905 1.1 lukem off_t offset;
906 1.1 lukem
907 1.1 lukem offset = bno;
908 1.1 lukem offset *= fsopts->sectorsize;
909 1.1 lukem if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
910 1.1 lukem err(1, "ffs_rdfs: seek error: %d\n", bno);
911 1.1 lukem n = read(fsopts->fd, bf, size);
912 1.1 lukem if (n == -1)
913 1.1 lukem err(1, "ffs_rdfs: read error bno %d size %d\n", bno, size);
914 1.1 lukem else if (n != size)
915 1.1 lukem errx(1,
916 1.1 lukem "ffs_rdfs: read error bno %d size %d: short read of %d\n",
917 1.1 lukem bno, size, n);
918 1.1 lukem }
919 1.1 lukem
920 1.1 lukem /*
921 1.1 lukem * write a block to the file system
922 1.1 lukem */
923 1.1 lukem void
924 1.1 lukem ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
925 1.1 lukem {
926 1.1 lukem int n;
927 1.1 lukem off_t offset;
928 1.1 lukem
929 1.1 lukem offset = bno;
930 1.1 lukem offset *= fsopts->sectorsize;
931 1.1 lukem if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
932 1.1 lukem err(1, "ffs_wtfs: seek error: %d\n", bno);
933 1.1 lukem n = write(fsopts->fd, bf, size);
934 1.1 lukem if (n == -1)
935 1.1 lukem err(1, "ffs_wtfs: write error bno %d size %d\n", bno, size);
936 1.1 lukem else if (n != size)
937 1.1 lukem errx(1,
938 1.1 lukem "ffs_wtfs: write error bno %d size %d: short write of %d\n",
939 1.1 lukem bno, size, n);
940 1.1 lukem }
941 1.1 lukem
942 1.1 lukem /* swap byte order of cylinder group */
943 1.1 lukem static void
944 1.1 lukem swap_cg(struct cg *o, struct cg *n)
945 1.1 lukem {
946 1.1 lukem int i, btotsize, fbsize;
947 1.1 lukem u_int32_t *n32, *o32;
948 1.1 lukem u_int16_t *n16, *o16;
949 1.1 lukem
950 1.1 lukem n->cg_firstfield = bswap32(o->cg_firstfield);
951 1.1 lukem n->cg_magic = bswap32(o->cg_magic);
952 1.1 lukem n->cg_time = bswap32(o->cg_time);
953 1.1 lukem n->cg_cgx = bswap32(o->cg_cgx);
954 1.1 lukem n->cg_ncyl = bswap16(o->cg_ncyl);
955 1.1 lukem n->cg_niblk = bswap16(o->cg_niblk);
956 1.1 lukem n->cg_ndblk = bswap32(o->cg_ndblk);
957 1.1 lukem n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
958 1.1 lukem n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
959 1.1 lukem n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
960 1.1 lukem n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
961 1.1 lukem n->cg_rotor = bswap32(o->cg_rotor);
962 1.1 lukem n->cg_frotor = bswap32(o->cg_frotor);
963 1.1 lukem n->cg_irotor = bswap32(o->cg_irotor);
964 1.1 lukem n->cg_btotoff = bswap32(o->cg_btotoff);
965 1.1 lukem n->cg_boff = bswap32(o->cg_boff);
966 1.1 lukem n->cg_iusedoff = bswap32(o->cg_iusedoff);
967 1.1 lukem n->cg_freeoff = bswap32(o->cg_freeoff);
968 1.1 lukem n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
969 1.1 lukem n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
970 1.1 lukem n->cg_clusteroff = bswap32(o->cg_clusteroff);
971 1.1 lukem n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
972 1.1 lukem for (i=0; i < MAXFRAG; i++)
973 1.1 lukem n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
974 1.1 lukem
975 1.1 lukem /* alays new format */
976 1.1 lukem if (n->cg_magic == CG_MAGIC) {
977 1.1 lukem btotsize = n->cg_boff - n->cg_btotoff;
978 1.1 lukem fbsize = n->cg_iusedoff - n->cg_boff;
979 1.1 lukem n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
980 1.1 lukem o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
981 1.1 lukem n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
982 1.1 lukem o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
983 1.1 lukem } else {
984 1.1 lukem btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
985 1.1 lukem fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
986 1.1 lukem n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
987 1.1 lukem o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
988 1.1 lukem n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
989 1.1 lukem o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
990 1.1 lukem }
991 1.1 lukem for (i=0; i < btotsize / sizeof(u_int32_t); i++)
992 1.1 lukem n32[i] = bswap32(o32[i]);
993 1.1 lukem
994 1.1 lukem for (i=0; i < fbsize/sizeof(u_int16_t); i++)
995 1.1 lukem n16[i] = bswap16(o16[i]);
996 1.1 lukem
997 1.1 lukem if (n->cg_magic == CG_MAGIC) {
998 1.1 lukem n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
999 1.1 lukem o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
1000 1.1 lukem } else {
1001 1.1 lukem n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
1002 1.1 lukem o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
1003 1.1 lukem }
1004 1.1 lukem for (i = 1; i < sblock.fs_contigsumsize + 1; i++)
1005 1.1 lukem n32[i] = bswap32(o32[i]);
1006 1.1 lukem }
1007 1.1 lukem
1008 1.1 lukem /* Determine how many digits are needed to print a given integer */
1009 1.1 lukem static int
1010 1.1 lukem count_digits(int num)
1011 1.1 lukem {
1012 1.1 lukem int ndig;
1013 1.1 lukem
1014 1.1 lukem for(ndig = 1; num > 9; num /=10, ndig++);
1015 1.1 lukem
1016 1.1 lukem return (ndig);
1017 1.1 lukem }
1018