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