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