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