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