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