mkfs.c revision 1.119 1 /* $NetBSD: mkfs.c,v 1.119 2013/06/23 07:28:36 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 2002 Networks Associates Technology, Inc.
34 * All rights reserved.
35 *
36 * This software was developed for the FreeBSD Project by Marshall
37 * Kirk McKusick and Network Associates Laboratories, the Security
38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
40 * research program
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 #include <sys/cdefs.h>
72 #ifndef lint
73 #if 0
74 static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
75 #else
76 __RCSID("$NetBSD: mkfs.c,v 1.119 2013/06/23 07:28:36 dholland Exp $");
77 #endif
78 #endif /* not lint */
79
80 #include <sys/param.h>
81 #include <sys/mman.h>
82 #include <sys/time.h>
83 #include <sys/resource.h>
84 #include <ufs/ufs/dinode.h>
85 #include <ufs/ufs/dir.h>
86 #include <ufs/ufs/ufs_bswap.h>
87 #include <ufs/ufs/quota2.h>
88 #include <ufs/ffs/fs.h>
89 #include <ufs/ffs/ffs_extern.h>
90 #include <sys/ioctl.h>
91 #include <sys/disklabel.h>
92
93 #include <err.h>
94 #include <errno.h>
95 #include <string.h>
96 #include <unistd.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99
100 #ifndef STANDALONE
101 #include <stdio.h>
102 #endif
103
104 #include "extern.h"
105
106 union dinode {
107 struct ufs1_dinode dp1;
108 struct ufs2_dinode dp2;
109 };
110
111 static void initcg(int, const struct timeval *);
112 static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
113 static int makedir(struct direct *, int);
114 static daddr_t alloc(int, int);
115 static void iput(union dinode *, ino_t);
116 static void rdfs(daddr_t, int, void *);
117 static void wtfs(daddr_t, int, void *);
118 static int isblock(struct fs *, unsigned char *, int);
119 static void clrblock(struct fs *, unsigned char *, int);
120 static void setblock(struct fs *, unsigned char *, int);
121 static int ilog2(int);
122 static void zap_old_sblock(int);
123 #ifdef MFS
124 static void *mkfs_malloc(size_t size);
125 #endif
126
127 /*
128 * make file system for cylinder-group style file systems
129 */
130 #define UMASK 0755
131
132 union {
133 struct fs fs;
134 char pad[SBLOCKSIZE];
135 } fsun;
136 #define sblock fsun.fs
137
138 struct csum *fscs_0; /* first block of cylinder summaries */
139 struct csum *fscs_next; /* place for next summary */
140 struct csum *fscs_end; /* end of summary buffer */
141 struct csum *fscs_reset; /* place for next summary after write */
142 uint fs_csaddr; /* fragment number to write to */
143
144 union {
145 struct cg cg;
146 char pad[MAXBSIZE];
147 } cgun;
148 #define acg cgun.cg
149
150 #define DIP(dp, field) \
151 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
152 (dp)->dp1.di_##field : (dp)->dp2.di_##field)
153
154 #define EXT2FS_SBOFF 1024 /* XXX: SBOFF in <ufs/ext2fs/ext2fs.h> */
155
156 char *iobuf;
157 int iobufsize; /* size to end of 2nd inode block */
158 int iobuf_memsize; /* Actual buffer size */
159
160 int fsi, fso;
161
162 static void
163 fserr(int num)
164 {
165 #ifdef GARBAGE
166 extern int Gflag;
167
168 if (Gflag)
169 return;
170 #endif
171 exit(num);
172 }
173
174 void
175 mkfs(const char *fsys, int fi, int fo,
176 mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
177 {
178 uint fragsperinodeblk, ncg, u;
179 uint cgzero;
180 uint64_t inodeblks, cgall;
181 int32_t cylno, i, csfrags;
182 int inodes_per_cg;
183 struct timeval tv;
184 long long sizepb;
185 int len, col, delta, fld_width, max_cols;
186 struct winsize winsize;
187
188 #ifndef STANDALONE
189 gettimeofday(&tv, NULL);
190 #endif
191 #ifdef MFS
192 if (mfs && !Nflag) {
193 if ((membase = mkfs_malloc(fssize * sectorsize)) == NULL)
194 exit(12);
195 }
196 #endif
197 fsi = fi;
198 fso = fo;
199 if (Oflag == 0) {
200 sblock.fs_old_inodefmt = FS_42INODEFMT;
201 sblock.fs_maxsymlinklen = 0;
202 sblock.fs_old_flags = 0;
203 } else {
204 sblock.fs_old_inodefmt = FS_44INODEFMT;
205 sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN :
206 UFS2_MAXSYMLINKLEN);
207 sblock.fs_old_flags = FS_FLAGS_UPDATED;
208 if (isappleufs)
209 sblock.fs_old_flags = 0;
210 sblock.fs_flags = 0;
211 }
212
213 /*
214 * collect and verify the filesystem density info
215 */
216 sblock.fs_avgfilesize = avgfilesize;
217 sblock.fs_avgfpdir = avgfpdir;
218 if (sblock.fs_avgfilesize <= 0) {
219 printf("illegal expected average file size %d\n",
220 sblock.fs_avgfilesize);
221 fserr(14);
222 }
223 if (sblock.fs_avgfpdir <= 0) {
224 printf("illegal expected number of files per directory %d\n",
225 sblock.fs_avgfpdir);
226 fserr(15);
227 }
228 /*
229 * collect and verify the block and fragment sizes
230 */
231 sblock.fs_bsize = bsize;
232 sblock.fs_fsize = fsize;
233 if (!powerof2(sblock.fs_bsize)) {
234 printf("block size must be a power of 2, not %d\n",
235 sblock.fs_bsize);
236 fserr(16);
237 }
238 if (!powerof2(sblock.fs_fsize)) {
239 printf("fragment size must be a power of 2, not %d\n",
240 sblock.fs_fsize);
241 fserr(17);
242 }
243 if (sblock.fs_fsize < sectorsize) {
244 printf("fragment size %d is too small, minimum is %d\n",
245 sblock.fs_fsize, sectorsize);
246 fserr(18);
247 }
248 if (sblock.fs_bsize < MINBSIZE) {
249 printf("block size %d is too small, minimum is %d\n",
250 sblock.fs_bsize, MINBSIZE);
251 fserr(19);
252 }
253 if (sblock.fs_bsize > MAXBSIZE) {
254 printf("block size %d is too large, maximum is %d\n",
255 sblock.fs_bsize, MAXBSIZE);
256 fserr(19);
257 }
258 if (sblock.fs_bsize < sblock.fs_fsize) {
259 printf("block size (%d) cannot be smaller than fragment size (%d)\n",
260 sblock.fs_bsize, sblock.fs_fsize);
261 fserr(20);
262 }
263
264 if (maxbsize < bsize || !powerof2(maxbsize)) {
265 sblock.fs_maxbsize = sblock.fs_bsize;
266 } else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
267 sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
268 } else {
269 sblock.fs_maxbsize = maxbsize;
270 }
271 sblock.fs_maxcontig = maxcontig;
272 if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
273 sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
274 if (verbosity > 0)
275 printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
276 }
277 if (sblock.fs_maxcontig > 1)
278 sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
279
280 sblock.fs_bmask = ~(sblock.fs_bsize - 1);
281 sblock.fs_fmask = ~(sblock.fs_fsize - 1);
282 sblock.fs_qbmask = ~sblock.fs_bmask;
283 sblock.fs_qfmask = ~sblock.fs_fmask;
284 for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
285 sblock.fs_bshift++;
286 for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
287 sblock.fs_fshift++;
288 sblock.fs_frag = ffs_numfrags(&sblock, sblock.fs_bsize);
289 for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
290 sblock.fs_fragshift++;
291 if (sblock.fs_frag > MAXFRAG) {
292 printf("fragment size %d is too small, "
293 "minimum with block size %d is %d\n",
294 sblock.fs_fsize, sblock.fs_bsize,
295 sblock.fs_bsize / MAXFRAG);
296 fserr(21);
297 }
298 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
299 sblock.fs_size = FFS_DBTOFSB(&sblock, fssize);
300 if (Oflag <= 1) {
301 if ((uint64_t)sblock.fs_size >= 1ull << 31) {
302 printf("Too many fragments (0x%" PRIx64
303 ") for a FFSv1 filesystem\n", sblock.fs_size);
304 fserr(22);
305 }
306 sblock.fs_magic = FS_UFS1_MAGIC;
307 sblock.fs_sblockloc = SBLOCK_UFS1;
308 sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
309 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
310 sblock.fs_old_cgoffset = 0;
311 sblock.fs_old_cgmask = 0xffffffff;
312 sblock.fs_old_size = sblock.fs_size;
313 sblock.fs_old_rotdelay = 0;
314 sblock.fs_old_rps = 60;
315 sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
316 sblock.fs_old_cpg = 1;
317 sblock.fs_old_interleave = 1;
318 sblock.fs_old_trackskew = 0;
319 sblock.fs_old_cpc = 0;
320 sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
321 sblock.fs_old_nrpos = 1;
322 } else {
323 sblock.fs_magic = FS_UFS2_MAGIC;
324 sblock.fs_sblockloc = SBLOCK_UFS2;
325 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
326 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
327 }
328
329 sblock.fs_sblkno =
330 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
331 sblock.fs_frag);
332 sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
333 roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
334 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
335 sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
336 for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) {
337 sizepb *= FFS_NINDIR(&sblock);
338 sblock.fs_maxfilesize += sizepb;
339 }
340
341 /*
342 * Calculate the number of blocks to put into each cylinder group.
343 *
344 * The cylinder group size is limited because the data structure
345 * must fit into a single block.
346 * We try to have as few cylinder groups as possible, with a proviso
347 * that we create at least MINCYLGRPS (==4) except for small
348 * filesystems.
349 *
350 * This algorithm works out how many blocks of inodes would be
351 * needed to fill the entire volume at the specified density.
352 * It then looks at how big the 'cylinder block' would have to
353 * be and, assuming that it is linearly related to the number
354 * of inodes and blocks how many cylinder groups are needed to
355 * keep the cylinder block below the filesystem block size.
356 *
357 * The cylinder groups are then all created with the average size.
358 *
359 * Space taken by the red tape on cylinder groups other than the
360 * first is ignored.
361 */
362
363 /* There must be space for 1 inode block and 2 data blocks */
364 if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
365 printf("Filesystem size %lld < minimum size of %d\n",
366 (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
367 fserr(23);
368 }
369 if (num_inodes != 0)
370 inodeblks = howmany(num_inodes, FFS_INOPB(&sblock));
371 else {
372 /*
373 * Calculate 'per inode block' so we can allocate less than
374 * 1 fragment per inode - useful for /dev.
375 */
376 fragsperinodeblk = MAX(ffs_numfrags(&sblock,
377 (uint64_t)density * FFS_INOPB(&sblock)), 1);
378 inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
379 (sblock.fs_frag + fragsperinodeblk);
380 }
381 if (inodeblks == 0)
382 inodeblks = 1;
383 /* Ensure that there are at least 2 data blocks (or we fail below) */
384 if (inodeblks > (uint64_t)(sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
385 inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
386 /* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
387 if (inodeblks * FFS_INOPB(&sblock) >= 1ull << 31)
388 inodeblks = ((1ull << 31) - NBBY) / FFS_INOPB(&sblock);
389 /*
390 * See what would happen if we tried to use 1 cylinder group.
391 * Assume space linear, so work out number of cylinder groups needed.
392 */
393 cgzero = CGSIZE_IF(&sblock, 0, 0);
394 cgall = CGSIZE_IF(&sblock, inodeblks * FFS_INOPB(&sblock), sblock.fs_size);
395 ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero);
396 if (ncg < MINCYLGRPS) {
397 /*
398 * We would like to allocate MINCLYGRPS cylinder groups,
399 * but for small file sytems (especially ones with a lot
400 * of inodes) this is not desirable (or possible).
401 */
402 u = sblock.fs_size / 2 / (sblock.fs_iblkno +
403 inodeblks * sblock.fs_frag);
404 if (u > ncg)
405 ncg = u;
406 if (ncg > MINCYLGRPS)
407 ncg = MINCYLGRPS;
408 if (ncg > inodeblks)
409 ncg = inodeblks;
410 }
411 /*
412 * Put an equal number of blocks in each cylinder group.
413 * Round up so we don't have more fragments in the last CG than
414 * the earlier ones (does that matter?), but kill a block if the
415 * CGSIZE becomes too big (only happens if there are a lot of CGs).
416 */
417 sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
418 /* Round up the fragments/group so the bitmap bytes are full */
419 sblock.fs_fpg = roundup(sblock.fs_fpg, NBBY);
420 inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
421
422 i = CGSIZE_IF(&sblock, inodes_per_cg, sblock.fs_fpg);
423 if (i > sblock.fs_bsize) {
424 sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
425 /* ... and recalculate how many cylinder groups we now need */
426 ncg = howmany(sblock.fs_size, sblock.fs_fpg);
427 inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
428 }
429 sblock.fs_ipg = inodes_per_cg;
430 /* Sanity check on our sums... */
431 if ((int)CGSIZE(&sblock) > sblock.fs_bsize) {
432 printf("CGSIZE miscalculated %d > %d\n",
433 (int)CGSIZE(&sblock), sblock.fs_bsize);
434 fserr(24);
435 }
436
437 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / FFS_INOPF(&sblock);
438 /* Check that the last cylinder group has enough space for the inodes */
439 i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
440 if (i < sblock.fs_dblkno) {
441 /*
442 * Since we make all the cylinder groups the same size, the
443 * last will only be small if there are a large number of
444 * cylinder groups. If we pull even a fragment from each
445 * of the other groups then the last CG will be overfull.
446 * So we just kill the last CG.
447 */
448 ncg--;
449 sblock.fs_size -= i;
450 }
451 sblock.fs_ncg = ncg;
452
453 sblock.fs_cgsize = ffs_fragroundup(&sblock, CGSIZE(&sblock));
454 if (Oflag <= 1) {
455 sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
456 sblock.fs_old_nsect = sblock.fs_old_spc;
457 sblock.fs_old_npsect = sblock.fs_old_spc;
458 sblock.fs_old_ncyl = sblock.fs_ncg;
459 }
460
461 /*
462 * Cylinder group summary information for each cylinder is written
463 * into the first cylinder group.
464 * Write this fragment by fragment, but doing the first CG last
465 * (after we've taken stuff off for the structure itself and the
466 * root directory.
467 */
468 sblock.fs_csaddr = cgdmin(&sblock, 0);
469 sblock.fs_cssize =
470 ffs_fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
471 if (512 % sizeof *fscs_0)
472 errx(1, "cylinder group summary doesn't fit in sectors");
473 fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
474 MAP_ANON|MAP_PRIVATE, -1, 0);
475 if (fscs_0 == MAP_FAILED)
476 exit(39);
477 memset(fscs_0, 0, 2 * sblock.fs_fsize);
478 fs_csaddr = sblock.fs_csaddr;
479 fscs_next = fscs_0;
480 fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
481 fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
482 /*
483 * fill in remaining fields of the super block
484 */
485 sblock.fs_sbsize = ffs_fragroundup(&sblock, sizeof(struct fs));
486 if (sblock.fs_sbsize > SBLOCKSIZE)
487 sblock.fs_sbsize = SBLOCKSIZE;
488 sblock.fs_minfree = minfree;
489 sblock.fs_maxcontig = maxcontig;
490 sblock.fs_maxbpg = maxbpg;
491 sblock.fs_optim = opt;
492 sblock.fs_cgrotor = 0;
493 sblock.fs_pendingblocks = 0;
494 sblock.fs_pendinginodes = 0;
495 sblock.fs_cstotal.cs_ndir = 0;
496 sblock.fs_cstotal.cs_nbfree = 0;
497 sblock.fs_cstotal.cs_nifree = 0;
498 sblock.fs_cstotal.cs_nffree = 0;
499 sblock.fs_fmod = 0;
500 sblock.fs_ronly = 0;
501 sblock.fs_state = 0;
502 sblock.fs_clean = FS_ISCLEAN;
503 sblock.fs_ronly = 0;
504 sblock.fs_id[0] = (long)tv.tv_sec; /* XXXfvdl huh? */
505 sblock.fs_id[1] = arc4random() & INT32_MAX;
506 sblock.fs_fsmnt[0] = '\0';
507 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
508 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
509 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
510 sblock.fs_cstotal.cs_nbfree =
511 fragstoblks(&sblock, sblock.fs_dsize) -
512 howmany(csfrags, sblock.fs_frag);
513 sblock.fs_cstotal.cs_nffree =
514 fragnum(&sblock, sblock.fs_size) +
515 (fragnum(&sblock, csfrags) > 0 ?
516 sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
517 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO;
518 sblock.fs_cstotal.cs_ndir = 0;
519 sblock.fs_dsize -= csfrags;
520 sblock.fs_time = tv.tv_sec;
521 if (Oflag <= 1) {
522 sblock.fs_old_time = tv.tv_sec;
523 sblock.fs_old_dsize = sblock.fs_dsize;
524 sblock.fs_old_csaddr = sblock.fs_csaddr;
525 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
526 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
527 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
528 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
529 }
530 /* add quota data in superblock */
531 if (quotas) {
532 sblock.fs_flags |= FS_DOQUOTA2;
533 sblock.fs_quota_magic = Q2_HEAD_MAGIC;
534 sblock.fs_quota_flags = quotas;
535 }
536 /*
537 * Dump out summary information about file system.
538 */
539 if (verbosity > 0) {
540 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
541 printf("%s: %.1fMB (%lld sectors) block size %d, "
542 "fragment size %d\n",
543 fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
544 (long long)FFS_FSBTODB(&sblock, sblock.fs_size),
545 sblock.fs_bsize, sblock.fs_fsize);
546 printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
547 "%d inodes.\n",
548 sblock.fs_ncg,
549 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
550 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
551 #undef B2MBFACTOR
552 }
553
554 /*
555 * allocate space for superblock, cylinder group map, and
556 * two sets of inode blocks.
557 */
558 if (sblock.fs_bsize < SBLOCKSIZE)
559 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
560 else
561 iobufsize = 4 * sblock.fs_bsize;
562 iobuf_memsize = iobufsize;
563 if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
564 /* A larger buffer so we can write multiple inode blks */
565 iobuf_memsize += 14 * sblock.fs_bsize;
566 }
567 for (;;) {
568 iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
569 MAP_ANON|MAP_PRIVATE, -1, 0);
570 if (iobuf != MAP_FAILED)
571 break;
572 if (iobuf_memsize != iobufsize) {
573 /* Try again with the smaller size */
574 iobuf_memsize = iobufsize;
575 continue;
576 }
577 printf("Cannot allocate I/O buffer\n");
578 exit(38);
579 }
580 memset(iobuf, 0, iobuf_memsize);
581
582 /*
583 * We now start writing to the filesystem
584 */
585
586 if (!Nflag) {
587 /*
588 * Validate the given file system size.
589 * Verify that its last block can actually be accessed.
590 * Convert to file system fragment sized units.
591 */
592 if (fssize <= 0) {
593 printf("preposterous size %lld\n", (long long)fssize);
594 fserr(13);
595 }
596 wtfs(fssize - 1, sectorsize, iobuf);
597
598 /*
599 * Ensure there is nothing that looks like a filesystem
600 * superbock anywhere other than where ours will be.
601 * If fsck finds the wrong one all hell breaks loose!
602 */
603 for (i = 0; ; i++) {
604 static const int sblocklist[] = SBLOCKSEARCH;
605 int sblkoff = sblocklist[i];
606 int sz;
607 if (sblkoff == -1)
608 break;
609 /* Remove main superblock */
610 zap_old_sblock(sblkoff);
611 /* and all possible locations for the first alternate */
612 sblkoff += SBLOCKSIZE;
613 for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
614 zap_old_sblock(roundup(sblkoff, sz));
615 }
616 /*
617 * Also zap possible Ext2fs magic leftover to prevent
618 * kernel vfs_mountroot() and bootloaders from mis-recognizing
619 * this file system as Ext2fs.
620 */
621 zap_old_sblock(EXT2FS_SBOFF);
622
623 if (isappleufs) {
624 struct appleufslabel appleufs;
625 ffs_appleufs_set(&appleufs, appleufs_volname,
626 tv.tv_sec, 0);
627 wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
628 APPLEUFS_LABEL_SIZE, &appleufs);
629 } else if (APPLEUFS_LABEL_SIZE % sectorsize == 0) {
630 struct appleufslabel appleufs;
631 /* Look for & zap any existing valid apple ufs labels */
632 rdfs(APPLEUFS_LABEL_OFFSET/sectorsize,
633 APPLEUFS_LABEL_SIZE, &appleufs);
634 if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
635 memset(&appleufs, 0, sizeof(appleufs));
636 wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
637 APPLEUFS_LABEL_SIZE, &appleufs);
638 }
639 }
640 }
641
642 /*
643 * Make a copy of the superblock into the buffer that we will be
644 * writing out in each cylinder group.
645 */
646 memcpy(iobuf, &sblock, sizeof sblock);
647 if (needswap)
648 ffs_sb_swap(&sblock, (struct fs *)iobuf);
649 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
650 memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
651 0xff, 256);
652
653 if (verbosity >= 3)
654 printf("super-block backups (for fsck_ffs -b #) at:\n");
655 /* If we are printing more than one line of numbers, line up columns */
656 fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
657 (uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, sblock.fs_ncg-1)));
658 /* Get terminal width */
659 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0)
660 max_cols = winsize.ws_col;
661 else
662 max_cols = 80;
663 if (Nflag && verbosity == 3)
664 /* Leave space to add " ..." after one row of numbers */
665 max_cols -= 4;
666 #define BASE 0x10000 /* For some fixed-point maths */
667 col = 0;
668 delta = verbosity > 2 ? 0 : max_cols * BASE / sblock.fs_ncg;
669 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
670 fflush(stdout);
671 initcg(cylno, &tv);
672 if (verbosity < 2)
673 continue;
674 if (delta > 0) {
675 if (Nflag)
676 /* No point doing dots for -N */
677 break;
678 /* Print dots scaled to end near RH margin */
679 for (col += delta; col > BASE; col -= BASE)
680 printf(".");
681 continue;
682 }
683 /* Print superblock numbers */
684 len = printf("%s%*" PRIu64 ",", col ? " " : "", fld_width,
685 (uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)));
686 col += len;
687 if (col + len < max_cols)
688 /* Next number fits */
689 continue;
690 /* Next number won't fit, need a newline */
691 if (verbosity <= 3) {
692 /* Print dots for subsequent cylinder groups */
693 delta = sblock.fs_ncg - cylno - 1;
694 if (delta != 0) {
695 if (Nflag) {
696 printf(" ...");
697 break;
698 }
699 delta = max_cols * BASE / delta;
700 }
701 }
702 col = 0;
703 printf("\n");
704 }
705 #undef BASE
706 if (col > 0)
707 printf("\n");
708 if (Nflag)
709 exit(0);
710
711 /*
712 * Now construct the initial file system,
713 */
714 if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
715 errx(1, "Error making filesystem");
716 sblock.fs_time = tv.tv_sec;
717 if (Oflag <= 1) {
718 sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
719 sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
720 sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
721 sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
722 }
723 /*
724 * Write out the super-block and zeros until the first cg info
725 */
726 i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
727 memset(iobuf, 0, i);
728 memcpy(iobuf, &sblock, sizeof sblock);
729 if (needswap)
730 ffs_sb_swap(&sblock, (struct fs *)iobuf);
731 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
732 memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
733 0xff, 256);
734 wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
735
736 /* Write out first and last cylinder summary sectors */
737 if (needswap)
738 ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
739 wtfs(FFS_FSBTODB(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
740
741 if (fscs_next > fscs_reset) {
742 if (needswap)
743 ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
744 fs_csaddr++;
745 wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
746 }
747
748 /* mfs doesn't need these permanently allocated */
749 munmap(iobuf, iobuf_memsize);
750 munmap(fscs_0, 2 * sblock.fs_fsize);
751 }
752
753 /*
754 * Initialize a cylinder group.
755 */
756 void
757 initcg(int cylno, const struct timeval *tv)
758 {
759 daddr_t cbase, dmax;
760 int32_t i, d, dlower, dupper, blkno;
761 uint32_t u;
762 struct ufs1_dinode *dp1;
763 struct ufs2_dinode *dp2;
764 int start;
765
766 /*
767 * Determine block bounds for cylinder group.
768 * Allow space for super block summary information in first
769 * cylinder group.
770 */
771 cbase = cgbase(&sblock, cylno);
772 dmax = cbase + sblock.fs_fpg;
773 if (dmax > sblock.fs_size)
774 dmax = sblock.fs_size;
775 dlower = cgsblock(&sblock, cylno) - cbase;
776 dupper = cgdmin(&sblock, cylno) - cbase;
777 if (cylno == 0) {
778 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
779 if (dupper >= cgstart(&sblock, cylno + 1)) {
780 printf("\rToo many cylinder groups to fit summary "
781 "information into first cylinder group\n");
782 fserr(40);
783 }
784 }
785 memset(&acg, 0, sblock.fs_cgsize);
786 acg.cg_magic = CG_MAGIC;
787 acg.cg_cgx = cylno;
788 acg.cg_ndblk = dmax - cbase;
789 if (sblock.fs_contigsumsize > 0)
790 acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
791 start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
792 if (Oflag == 2) {
793 acg.cg_time = tv->tv_sec;
794 acg.cg_niblk = sblock.fs_ipg;
795 acg.cg_initediblk = sblock.fs_ipg < 2 * FFS_INOPB(&sblock) ?
796 sblock.fs_ipg : 2 * FFS_INOPB(&sblock);
797 acg.cg_iusedoff = start;
798 } else {
799 acg.cg_old_ncyl = sblock.fs_old_cpg;
800 if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
801 (cylno == sblock.fs_ncg - 1))
802 acg.cg_old_ncyl =
803 sblock.fs_old_ncyl % sblock.fs_old_cpg;
804 acg.cg_old_time = tv->tv_sec;
805 acg.cg_old_niblk = sblock.fs_ipg;
806 acg.cg_old_btotoff = start;
807 acg.cg_old_boff = acg.cg_old_btotoff +
808 sblock.fs_old_cpg * sizeof(int32_t);
809 acg.cg_iusedoff = acg.cg_old_boff +
810 sblock.fs_old_cpg * sizeof(u_int16_t);
811 }
812 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
813 if (sblock.fs_contigsumsize <= 0) {
814 acg.cg_nextfreeoff = acg.cg_freeoff +
815 howmany(sblock.fs_fpg, CHAR_BIT);
816 } else {
817 acg.cg_clustersumoff = acg.cg_freeoff +
818 howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
819 if (isappleufs) {
820 /* Apple PR2216969 gives rationale for this change.
821 * I believe they were mistaken, but we need to
822 * duplicate it for compatibility. -- dbj (at) NetBSD.org
823 */
824 acg.cg_clustersumoff += sizeof(int32_t);
825 }
826 acg.cg_clustersumoff =
827 roundup(acg.cg_clustersumoff, sizeof(int32_t));
828 acg.cg_clusteroff = acg.cg_clustersumoff +
829 (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
830 acg.cg_nextfreeoff = acg.cg_clusteroff +
831 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
832 }
833 if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
834 printf("Panic: cylinder group too big\n");
835 fserr(37);
836 }
837 acg.cg_cs.cs_nifree += sblock.fs_ipg;
838 if (cylno == 0)
839 for (u = 0; u < UFS_ROOTINO; u++) {
840 setbit(cg_inosused(&acg, 0), u);
841 acg.cg_cs.cs_nifree--;
842 }
843 if (cylno > 0) {
844 /*
845 * In cylno 0, beginning space is reserved
846 * for boot and super blocks.
847 */
848 for (d = 0, blkno = 0; d < dlower;) {
849 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
850 if (sblock.fs_contigsumsize > 0)
851 setbit(cg_clustersfree(&acg, 0), blkno);
852 acg.cg_cs.cs_nbfree++;
853 if (Oflag <= 1) {
854 int cn = old_cbtocylno(&sblock, d);
855 old_cg_blktot(&acg, 0)[cn]++;
856 old_cg_blks(&sblock, &acg,
857 cn, 0)[old_cbtorpos(&sblock, d)]++;
858 }
859 d += sblock.fs_frag;
860 blkno++;
861 }
862 }
863 if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
864 acg.cg_frsum[sblock.fs_frag - i]++;
865 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
866 setbit(cg_blksfree(&acg, 0), dupper);
867 acg.cg_cs.cs_nffree++;
868 }
869 }
870 for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
871 d + sblock.fs_frag <= acg.cg_ndblk; ) {
872 setblock(&sblock, cg_blksfree(&acg, 0), blkno);
873 if (sblock.fs_contigsumsize > 0)
874 setbit(cg_clustersfree(&acg, 0), blkno);
875 acg.cg_cs.cs_nbfree++;
876 if (Oflag <= 1) {
877 int cn = old_cbtocylno(&sblock, d);
878 old_cg_blktot(&acg, 0)[cn]++;
879 old_cg_blks(&sblock, &acg,
880 cn, 0)[old_cbtorpos(&sblock, d)]++;
881 }
882 d += sblock.fs_frag;
883 blkno++;
884 }
885 if (d < acg.cg_ndblk) {
886 acg.cg_frsum[acg.cg_ndblk - d]++;
887 for (; d < acg.cg_ndblk; d++) {
888 setbit(cg_blksfree(&acg, 0), d);
889 acg.cg_cs.cs_nffree++;
890 }
891 }
892 if (sblock.fs_contigsumsize > 0) {
893 int32_t *sump = cg_clustersum(&acg, 0);
894 u_char *mapp = cg_clustersfree(&acg, 0);
895 int map = *mapp++;
896 int bit = 1;
897 int run = 0;
898
899 for (i = 0; i < acg.cg_nclusterblks; i++) {
900 if ((map & bit) != 0) {
901 run++;
902 } else if (run != 0) {
903 if (run > sblock.fs_contigsumsize)
904 run = sblock.fs_contigsumsize;
905 sump[run]++;
906 run = 0;
907 }
908 if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
909 bit <<= 1;
910 } else {
911 map = *mapp++;
912 bit = 1;
913 }
914 }
915 if (run != 0) {
916 if (run > sblock.fs_contigsumsize)
917 run = sblock.fs_contigsumsize;
918 sump[run]++;
919 }
920 }
921 *fscs_next++ = acg.cg_cs;
922 if (fscs_next == fscs_end) {
923 /* write block of cylinder group summary info into cyl 0 */
924 if (needswap)
925 ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
926 fs_csaddr++;
927 wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
928 fscs_next = fscs_reset;
929 memset(fscs_next, 0, sblock.fs_fsize);
930 }
931 /*
932 * Write out the duplicate super block, the cylinder group map
933 * and two blocks worth of inodes in a single write.
934 */
935 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
936 memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
937 if (needswap)
938 ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
939 start += sblock.fs_bsize;
940 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
941 dp2 = (struct ufs2_dinode *)(&iobuf[start]);
942 for (i = MIN(sblock.fs_ipg, 2) * FFS_INOPB(&sblock); i != 0; i--) {
943 if (sblock.fs_magic == FS_UFS1_MAGIC) {
944 /* No need to swap, it'll stay random */
945 dp1->di_gen = arc4random() & INT32_MAX;
946 dp1++;
947 } else {
948 dp2->di_gen = arc4random() & INT32_MAX;
949 dp2++;
950 }
951 }
952 wtfs(FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
953 /*
954 * For the old file system, we have to initialize all the inodes.
955 */
956 if (sblock.fs_magic != FS_UFS1_MAGIC)
957 return;
958
959 /* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
960 d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
961 dupper = sblock.fs_ipg / FFS_INOPF(&sblock);
962 for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
963 if (d > dupper - i)
964 d = dupper - i;
965 dp1 = (struct ufs1_dinode *)(&iobuf[start]);
966 do
967 dp1->di_gen = arc4random() & INT32_MAX;
968 while ((char *)++dp1 < &iobuf[iobuf_memsize]);
969 wtfs(FFS_FSBTODB(&sblock, cgimin(&sblock, cylno) + i),
970 d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
971 }
972 }
973
974 /*
975 * initialize the file system
976 */
977
978 #ifdef LOSTDIR
979 #define PREDEFDIR 3
980 #else
981 #define PREDEFDIR 2
982 #endif
983
984 struct direct root_dir[] = {
985 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
986 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
987 #ifdef LOSTDIR
988 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
989 #endif
990 };
991 struct odirect {
992 u_int32_t d_ino;
993 u_int16_t d_reclen;
994 u_int16_t d_namlen;
995 u_char d_name[FFS_MAXNAMLEN + 1];
996 } oroot_dir[] = {
997 { UFS_ROOTINO, sizeof(struct direct), 1, "." },
998 { UFS_ROOTINO, sizeof(struct direct), 2, ".." },
999 #ifdef LOSTDIR
1000 { LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
1001 #endif
1002 };
1003 #ifdef LOSTDIR
1004 struct direct lost_found_dir[] = {
1005 { LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
1006 { UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
1007 { 0, DIRBLKSIZ, 0, 0, 0 },
1008 };
1009 struct odirect olost_found_dir[] = {
1010 { LOSTFOUNDINO, sizeof(struct direct), 1, "." },
1011 { UFS_ROOTINO, sizeof(struct direct), 2, ".." },
1012 { 0, DIRBLKSIZ, 0, 0 },
1013 };
1014 #endif
1015 char buf[MAXBSIZE];
1016 static void copy_dir(struct direct *, struct direct *);
1017
1018 int
1019 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
1020 {
1021 union dinode node;
1022 int i;
1023 int qblocks = 0;
1024 int qinos = 0;
1025 uint8_t q2h_hash_shift;
1026 uint16_t q2h_hash_mask;
1027 #ifdef LOSTDIR
1028 int dirblksiz = DIRBLKSIZ;
1029 if (isappleufs)
1030 dirblksiz = APPLEUFS_DIRBLKSIZ;
1031 int nextino = LOSTFOUNDINO+1;
1032 #else
1033 int nextino = UFS_ROOTINO+1;
1034 #endif
1035
1036 /*
1037 * initialize the node
1038 */
1039
1040 #ifdef LOSTDIR
1041 /*
1042 * create the lost+found directory
1043 */
1044 memset(&node, 0, sizeof(node));
1045 if (Oflag == 0) {
1046 (void)makedir((struct direct *)olost_found_dir, 2);
1047 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1048 copy_dir((struct direct*)&olost_found_dir[2],
1049 (struct direct*)&buf[i]);
1050 } else {
1051 (void)makedir(lost_found_dir, 2);
1052 for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1053 copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
1054 }
1055 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1056 node.dp1.di_atime = tv->tv_sec;
1057 node.dp1.di_atimensec = tv->tv_usec * 1000;
1058 node.dp1.di_mtime = tv->tv_sec;
1059 node.dp1.di_mtimensec = tv->tv_usec * 1000;
1060 node.dp1.di_ctime = tv->tv_sec;
1061 node.dp1.di_ctimensec = tv->tv_usec * 1000;
1062 node.dp1.di_mode = IFDIR | UMASK;
1063 node.dp1.di_nlink = 2;
1064 node.dp1.di_size = sblock.fs_bsize;
1065 node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
1066 if (node.dp1.di_db[0] == 0)
1067 return (0);
1068 node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1069 node.dp1.di_size));
1070 qblocks += node.dp1.di_blocks;
1071 node.dp1.di_uid = geteuid();
1072 node.dp1.di_gid = getegid();
1073 wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
1074 buf);
1075 } else {
1076 node.dp2.di_atime = tv->tv_sec;
1077 node.dp2.di_atimensec = tv->tv_usec * 1000;
1078 node.dp2.di_mtime = tv->tv_sec;
1079 node.dp2.di_mtimensec = tv->tv_usec * 1000;
1080 node.dp2.di_ctime = tv->tv_sec;
1081 node.dp2.di_ctimensec = tv->tv_usec * 1000;
1082 node.dp2.di_birthtime = tv->tv_sec;
1083 node.dp2.di_birthnsec = tv->tv_usec * 1000;
1084 node.dp2.di_mode = IFDIR | UMASK;
1085 node.dp2.di_nlink = 2;
1086 node.dp2.di_size = sblock.fs_bsize;
1087 node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
1088 if (node.dp2.di_db[0] == 0)
1089 return (0);
1090 node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1091 node.dp2.di_size));
1092 qblocks += node.dp2.di_blocks;
1093 node.dp2.di_uid = geteuid();
1094 node.dp2.di_gid = getegid();
1095 wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
1096 buf);
1097 }
1098 qinos++;
1099 iput(&node, LOSTFOUNDINO);
1100 #endif
1101 /*
1102 * create the root directory
1103 */
1104 memset(&node, 0, sizeof(node));
1105 if (Oflag <= 1) {
1106 if (mfs) {
1107 node.dp1.di_mode = IFDIR | mfsmode;
1108 node.dp1.di_uid = mfsuid;
1109 node.dp1.di_gid = mfsgid;
1110 } else {
1111 node.dp1.di_mode = IFDIR | UMASK;
1112 node.dp1.di_uid = geteuid();
1113 node.dp1.di_gid = getegid();
1114 }
1115 node.dp1.di_nlink = PREDEFDIR;
1116 if (Oflag == 0)
1117 node.dp1.di_size = makedir((struct direct *)oroot_dir,
1118 PREDEFDIR);
1119 else
1120 node.dp1.di_size = makedir(root_dir, PREDEFDIR);
1121 node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
1122 if (node.dp1.di_db[0] == 0)
1123 return (0);
1124 node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1125 node.dp1.di_size));
1126 qblocks += node.dp1.di_blocks;
1127 wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
1128 } else {
1129 if (mfs) {
1130 node.dp2.di_mode = IFDIR | mfsmode;
1131 node.dp2.di_uid = mfsuid;
1132 node.dp2.di_gid = mfsgid;
1133 } else {
1134 node.dp2.di_mode = IFDIR | UMASK;
1135 node.dp2.di_uid = geteuid();
1136 node.dp2.di_gid = getegid();
1137 }
1138 node.dp2.di_atime = tv->tv_sec;
1139 node.dp2.di_atimensec = tv->tv_usec * 1000;
1140 node.dp2.di_mtime = tv->tv_sec;
1141 node.dp2.di_mtimensec = tv->tv_usec * 1000;
1142 node.dp2.di_ctime = tv->tv_sec;
1143 node.dp2.di_ctimensec = tv->tv_usec * 1000;
1144 node.dp2.di_birthtime = tv->tv_sec;
1145 node.dp2.di_birthnsec = tv->tv_usec * 1000;
1146 node.dp2.di_nlink = PREDEFDIR;
1147 node.dp2.di_size = makedir(root_dir, PREDEFDIR);
1148 node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
1149 if (node.dp2.di_db[0] == 0)
1150 return (0);
1151 node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1152 node.dp2.di_size));
1153 qblocks += node.dp2.di_blocks;
1154 wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
1155 }
1156 qinos++;
1157 iput(&node, UFS_ROOTINO);
1158 /*
1159 * compute the size of the hash table
1160 * We know the smallest block size is 4k, so we can use 2k
1161 * for the hash table; as an entry is 8 bytes we can store
1162 * 256 entries. So let start q2h_hash_shift at 8
1163 */
1164 for (q2h_hash_shift = 8;
1165 q2h_hash_shift < 15;
1166 q2h_hash_shift++) {
1167 if ((sizeof(uint64_t) << (q2h_hash_shift + 1)) +
1168 sizeof(struct quota2_header) > (u_int)sblock.fs_bsize)
1169 break;
1170 }
1171 q2h_hash_mask = (1 << q2h_hash_shift) - 1;
1172 for (i = 0; i < MAXQUOTAS; i++) {
1173 struct quota2_header *q2h;
1174 struct quota2_entry *q2e;
1175 uint64_t offset;
1176 uid_t uid = (i == USRQUOTA ? geteuid() : getegid());
1177
1178 if ((quotas & FS_Q2_DO_TYPE(i)) == 0)
1179 continue;
1180 quota2_create_blk0(sblock.fs_bsize, buf, q2h_hash_shift,
1181 i, needswap);
1182 /* grab an entry from header for root dir */
1183 q2h = (void *)buf;
1184 offset = ufs_rw64(q2h->q2h_free, needswap);
1185 q2e = (void *)((char *)buf + offset);
1186 q2h->q2h_free = q2e->q2e_next;
1187 memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e));
1188 q2e->q2e_uid = ufs_rw32(uid, needswap);
1189 q2e->q2e_val[QL_BLOCK].q2v_cur = ufs_rw64(qblocks, needswap);
1190 q2e->q2e_val[QL_FILE].q2v_cur = ufs_rw64(qinos, needswap);
1191 /* add to the hash entry */
1192 q2e->q2e_next = q2h->q2h_entries[uid & q2h_hash_mask];
1193 q2h->q2h_entries[uid & q2h_hash_mask] =
1194 ufs_rw64(offset, needswap);
1195
1196 memset(&node, 0, sizeof(node));
1197 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1198 node.dp1.di_atime = tv->tv_sec;
1199 node.dp1.di_atimensec = tv->tv_usec * 1000;
1200 node.dp1.di_mtime = tv->tv_sec;
1201 node.dp1.di_mtimensec = tv->tv_usec * 1000;
1202 node.dp1.di_ctime = tv->tv_sec;
1203 node.dp1.di_ctimensec = tv->tv_usec * 1000;
1204 node.dp1.di_mode = IFREG;
1205 node.dp1.di_nlink = 1;
1206 node.dp1.di_size = sblock.fs_bsize;
1207 node.dp1.di_db[0] =
1208 alloc(node.dp1.di_size, node.dp1.di_mode);
1209 if (node.dp1.di_db[0] == 0)
1210 return (0);
1211 node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1212 node.dp1.di_size));
1213 node.dp1.di_uid = geteuid();
1214 node.dp1.di_gid = getegid();
1215 wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]),
1216 node.dp1.di_size, buf);
1217 } else {
1218 node.dp2.di_atime = tv->tv_sec;
1219 node.dp2.di_atimensec = tv->tv_usec * 1000;
1220 node.dp2.di_mtime = tv->tv_sec;
1221 node.dp2.di_mtimensec = tv->tv_usec * 1000;
1222 node.dp2.di_ctime = tv->tv_sec;
1223 node.dp2.di_ctimensec = tv->tv_usec * 1000;
1224 node.dp2.di_birthtime = tv->tv_sec;
1225 node.dp2.di_birthnsec = tv->tv_usec * 1000;
1226 node.dp2.di_mode = IFREG;
1227 node.dp2.di_nlink = 1;
1228 node.dp2.di_size = sblock.fs_bsize;
1229 node.dp2.di_db[0] =
1230 alloc(node.dp2.di_size, node.dp2.di_mode);
1231 if (node.dp2.di_db[0] == 0)
1232 return (0);
1233 node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1234 node.dp2.di_size));
1235 node.dp2.di_uid = geteuid();
1236 node.dp2.di_gid = getegid();
1237 wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]),
1238 node.dp2.di_size, buf);
1239 }
1240 iput(&node, nextino);
1241 sblock.fs_quotafile[i] = nextino;
1242 nextino++;
1243 }
1244 return (1);
1245 }
1246
1247 /*
1248 * construct a set of directory entries in "buf".
1249 * return size of directory.
1250 */
1251 int
1252 makedir(struct direct *protodir, int entries)
1253 {
1254 char *cp;
1255 int i, spcleft;
1256 int dirblksiz = UFS_DIRBLKSIZ;
1257 if (isappleufs)
1258 dirblksiz = APPLEUFS_DIRBLKSIZ;
1259
1260 memset(buf, 0, UFS_DIRBLKSIZ);
1261 spcleft = dirblksiz;
1262 for (cp = buf, i = 0; i < entries - 1; i++) {
1263 protodir[i].d_reclen = UFS_DIRSIZ(Oflag == 0, &protodir[i], 0);
1264 copy_dir(&protodir[i], (struct direct*)cp);
1265 cp += protodir[i].d_reclen;
1266 spcleft -= protodir[i].d_reclen;
1267 }
1268 protodir[i].d_reclen = spcleft;
1269 copy_dir(&protodir[i], (struct direct*)cp);
1270 return (dirblksiz);
1271 }
1272
1273 /*
1274 * allocate a block or frag
1275 */
1276 daddr_t
1277 alloc(int size, int mode)
1278 {
1279 int i, frag;
1280 daddr_t d, blkno;
1281
1282 rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1283 /* fs -> host byte order */
1284 if (needswap)
1285 ffs_cg_swap(&acg, &acg, &sblock);
1286 if (acg.cg_magic != CG_MAGIC) {
1287 printf("cg 0: bad magic number\n");
1288 return (0);
1289 }
1290 if (acg.cg_cs.cs_nbfree == 0) {
1291 printf("first cylinder group ran out of space\n");
1292 return (0);
1293 }
1294 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1295 if (isblock(&sblock, cg_blksfree(&acg, 0),
1296 d >> sblock.fs_fragshift))
1297 goto goth;
1298 printf("internal error: can't find block in cyl 0\n");
1299 return (0);
1300 goth:
1301 blkno = fragstoblks(&sblock, d);
1302 clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
1303 if (sblock.fs_contigsumsize > 0)
1304 clrbit(cg_clustersfree(&acg, 0), blkno);
1305 acg.cg_cs.cs_nbfree--;
1306 sblock.fs_cstotal.cs_nbfree--;
1307 fscs_0->cs_nbfree--;
1308 if (mode & IFDIR) {
1309 acg.cg_cs.cs_ndir++;
1310 sblock.fs_cstotal.cs_ndir++;
1311 fscs_0->cs_ndir++;
1312 }
1313 if (Oflag <= 1) {
1314 int cn = old_cbtocylno(&sblock, d);
1315 old_cg_blktot(&acg, 0)[cn]--;
1316 old_cg_blks(&sblock, &acg,
1317 cn, 0)[old_cbtorpos(&sblock, d)]--;
1318 }
1319 if (size != sblock.fs_bsize) {
1320 frag = howmany(size, sblock.fs_fsize);
1321 fscs_0->cs_nffree += sblock.fs_frag - frag;
1322 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1323 acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1324 acg.cg_frsum[sblock.fs_frag - frag]++;
1325 for (i = frag; i < sblock.fs_frag; i++)
1326 setbit(cg_blksfree(&acg, 0), d + i);
1327 }
1328 /* host -> fs byte order */
1329 if (needswap)
1330 ffs_cg_swap(&acg, &acg, &sblock);
1331 wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1332 return (d);
1333 }
1334
1335 /*
1336 * Allocate an inode on the disk
1337 */
1338 static void
1339 iput(union dinode *ip, ino_t ino)
1340 {
1341 daddr_t d;
1342 int i;
1343 struct ufs1_dinode *dp1;
1344 struct ufs2_dinode *dp2;
1345
1346 rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1347 /* fs -> host byte order */
1348 if (needswap)
1349 ffs_cg_swap(&acg, &acg, &sblock);
1350 if (acg.cg_magic != CG_MAGIC) {
1351 printf("cg 0: bad magic number\n");
1352 fserr(31);
1353 }
1354 acg.cg_cs.cs_nifree--;
1355 setbit(cg_inosused(&acg, 0), ino);
1356 /* host -> fs byte order */
1357 if (needswap)
1358 ffs_cg_swap(&acg, &acg, &sblock);
1359 wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1360 sblock.fs_cstotal.cs_nifree--;
1361 fscs_0->cs_nifree--;
1362 if (ino >= (ino_t)(sblock.fs_ipg * sblock.fs_ncg)) {
1363 printf("fsinit: inode value out of range (%llu).\n",
1364 (unsigned long long)ino);
1365 fserr(32);
1366 }
1367 d = FFS_FSBTODB(&sblock, ino_to_fsba(&sblock, ino));
1368 rdfs(d, sblock.fs_bsize, (char *)iobuf);
1369 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1370 dp1 = (struct ufs1_dinode *)iobuf;
1371 dp1 += ino_to_fsbo(&sblock, ino);
1372 if (needswap) {
1373 ffs_dinode1_swap(&ip->dp1, dp1);
1374 /* ffs_dinode1_swap() doesn't swap blocks addrs */
1375 for (i=0; i<UFS_NDADDR + UFS_NIADDR; i++)
1376 dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
1377 } else
1378 *dp1 = ip->dp1;
1379 dp1->di_gen = arc4random() & INT32_MAX;
1380 } else {
1381 dp2 = (struct ufs2_dinode *)iobuf;
1382 dp2 += ino_to_fsbo(&sblock, ino);
1383 if (needswap) {
1384 ffs_dinode2_swap(&ip->dp2, dp2);
1385 for (i=0; i<UFS_NDADDR + UFS_NIADDR; i++)
1386 dp2->di_db[i] = bswap64(ip->dp2.di_db[i]);
1387 } else
1388 *dp2 = ip->dp2;
1389 dp2->di_gen = arc4random() & INT32_MAX;
1390 }
1391 wtfs(d, sblock.fs_bsize, iobuf);
1392 }
1393
1394 /*
1395 * read a block from the file system
1396 */
1397 void
1398 rdfs(daddr_t bno, int size, void *bf)
1399 {
1400 int n;
1401 off_t offset;
1402
1403 #ifdef MFS
1404 if (mfs) {
1405 if (Nflag)
1406 memset(bf, 0, size);
1407 else
1408 memmove(bf, membase + bno * sectorsize, size);
1409 return;
1410 }
1411 #endif
1412 offset = bno;
1413 n = pread(fsi, bf, size, offset * sectorsize);
1414 if (n != size) {
1415 printf("rdfs: read error for sector %lld: %s\n",
1416 (long long)bno, strerror(errno));
1417 exit(34);
1418 }
1419 }
1420
1421 /*
1422 * write a block to the file system
1423 */
1424 void
1425 wtfs(daddr_t bno, int size, void *bf)
1426 {
1427 int n;
1428 off_t offset;
1429
1430 if (Nflag)
1431 return;
1432 #ifdef MFS
1433 if (mfs) {
1434 memmove(membase + bno * sectorsize, bf, size);
1435 return;
1436 }
1437 #endif
1438 offset = bno;
1439 n = pwrite(fso, bf, size, offset * sectorsize);
1440 if (n != size) {
1441 printf("wtfs: write error for sector %lld: %s\n",
1442 (long long)bno, strerror(errno));
1443 exit(36);
1444 }
1445 }
1446
1447 /*
1448 * check if a block is available
1449 */
1450 int
1451 isblock(struct fs *fs, unsigned char *cp, int h)
1452 {
1453 unsigned char mask;
1454
1455 switch (fs->fs_fragshift) {
1456 case 3:
1457 return (cp[h] == 0xff);
1458 case 2:
1459 mask = 0x0f << ((h & 0x1) << 2);
1460 return ((cp[h >> 1] & mask) == mask);
1461 case 1:
1462 mask = 0x03 << ((h & 0x3) << 1);
1463 return ((cp[h >> 2] & mask) == mask);
1464 case 0:
1465 mask = 0x01 << (h & 0x7);
1466 return ((cp[h >> 3] & mask) == mask);
1467 default:
1468 #ifdef STANDALONE
1469 printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
1470 #else
1471 fprintf(stderr, "isblock bad fs_fragshift %d\n",
1472 fs->fs_fragshift);
1473 #endif
1474 return (0);
1475 }
1476 }
1477
1478 /*
1479 * take a block out of the map
1480 */
1481 void
1482 clrblock(struct fs *fs, unsigned char *cp, int h)
1483 {
1484 switch ((fs)->fs_fragshift) {
1485 case 3:
1486 cp[h] = 0;
1487 return;
1488 case 2:
1489 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1490 return;
1491 case 1:
1492 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1493 return;
1494 case 0:
1495 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1496 return;
1497 default:
1498 #ifdef STANDALONE
1499 printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
1500 #else
1501 fprintf(stderr, "clrblock bad fs_fragshift %d\n",
1502 fs->fs_fragshift);
1503 #endif
1504 return;
1505 }
1506 }
1507
1508 /*
1509 * put a block into the map
1510 */
1511 void
1512 setblock(struct fs *fs, unsigned char *cp, int h)
1513 {
1514 switch (fs->fs_fragshift) {
1515 case 3:
1516 cp[h] = 0xff;
1517 return;
1518 case 2:
1519 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1520 return;
1521 case 1:
1522 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1523 return;
1524 case 0:
1525 cp[h >> 3] |= (0x01 << (h & 0x7));
1526 return;
1527 default:
1528 #ifdef STANDALONE
1529 printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
1530 #else
1531 fprintf(stderr, "setblock bad fs_fragshift %d\n",
1532 fs->fs_fragshift);
1533 #endif
1534 return;
1535 }
1536 }
1537
1538 /* copy a direntry to a buffer, in fs byte order */
1539 static void
1540 copy_dir(struct direct *dir, struct direct *dbuf)
1541 {
1542 memcpy(dbuf, dir, UFS_DIRSIZ(Oflag == 0, dir, 0));
1543 if (needswap) {
1544 dbuf->d_ino = bswap32(dir->d_ino);
1545 dbuf->d_reclen = bswap16(dir->d_reclen);
1546 if (Oflag == 0)
1547 ((struct odirect*)dbuf)->d_namlen =
1548 bswap16(((struct odirect*)dir)->d_namlen);
1549 }
1550 }
1551
1552 static int
1553 ilog2(int val)
1554 {
1555 u_int n;
1556
1557 for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
1558 if (1 << n == val)
1559 return (n);
1560 errx(1, "ilog2: %d is not a power of 2\n", val);
1561 }
1562
1563 static void
1564 zap_old_sblock(int sblkoff)
1565 {
1566 static int cg0_data;
1567 uint32_t oldfs[SBLOCKSIZE / 4];
1568 static const struct fsm {
1569 uint32_t offset;
1570 uint32_t magic;
1571 uint32_t mask;
1572 } fs_magics[] = {
1573 {offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
1574 {offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
1575 {0, 0x70162, ~0u}, /* LFS_MAGIC */
1576 {14, 0xef53, 0xffff}, /* EXT2FS (little) */
1577 {14, 0xef530000, 0xffff0000}, /* EXT2FS (big) */
1578 {.offset = ~0u},
1579 };
1580 const struct fsm *fsm;
1581
1582 if (Nflag)
1583 return;
1584
1585 if (sblkoff == 0) /* Why did UFS2 add support for this? sigh. */
1586 return;
1587
1588 if (cg0_data == 0)
1589 /* For FFSv1 this could include all the inodes. */
1590 cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
1591
1592 /* Ignore anything that is beyond our filesystem */
1593 if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
1594 return;
1595 /* Zero anything inside our filesystem... */
1596 if (sblkoff >= sblock.fs_sblockloc) {
1597 /* ...unless we will write that area anyway */
1598 if (sblkoff >= cg0_data)
1599 wtfs(sblkoff / sectorsize,
1600 roundup(sizeof sblock, sectorsize), iobuf);
1601 return;
1602 }
1603
1604 /* The sector might contain boot code, so we must validate it */
1605 rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1606 for (fsm = fs_magics; ; fsm++) {
1607 uint32_t v;
1608 if (fsm->mask == 0)
1609 return;
1610 v = oldfs[fsm->offset];
1611 if ((v & fsm->mask) == fsm->magic ||
1612 (bswap32(v) & fsm->mask) == fsm->magic)
1613 break;
1614 }
1615
1616 /* Just zap the magic number */
1617 oldfs[fsm->offset] = 0;
1618 wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1619 }
1620
1621
1622 #ifdef MFS
1623 /*
1624 * Internal version of malloc that trims the requested size if not enough
1625 * memory is available.
1626 */
1627 static void *
1628 mkfs_malloc(size_t size)
1629 {
1630 u_long pgsz;
1631 caddr_t *memory, *extra;
1632 size_t exsize = 128 * 1024;
1633
1634 if (size == 0)
1635 return (NULL);
1636
1637 pgsz = getpagesize() - 1;
1638 size = (size + pgsz) &~ pgsz;
1639
1640 /* try to map requested size */
1641 memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1642 -1, 0);
1643 if (memory == MAP_FAILED)
1644 return NULL;
1645
1646 /* try to map something extra */
1647 extra = mmap(0, exsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1648 -1, 0);
1649 munmap(extra, exsize);
1650
1651 /* if extra memory couldn't be mapped, reduce original request accordingly */
1652 if (extra == MAP_FAILED) {
1653 munmap(memory, size);
1654 size -= exsize;
1655 memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1656 -1, 0);
1657 if (memory == MAP_FAILED)
1658 return NULL;
1659 }
1660
1661 return memory;
1662 }
1663 #endif /* MFS */
1664