1 /* $NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2001 Wasabi Systems, Inc. 7 * All rights reserved. 8 * 9 * Written by Luke Mewburn for Wasabi Systems, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed for the NetBSD Project by 22 * Wasabi Systems, Inc. 23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 24 * or promote products derived from this software without specific prior 25 * written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 /* 40 * Copyright (c) 1982, 1986, 1989, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 * 67 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95 68 */ 69 70 #include <sys/cdefs.h> 71 __FBSDID("$FreeBSD$"); 72 73 #if HAVE_NBTOOL_CONFIG_H 74 #include "nbtool_config.h" 75 #endif 76 77 #include <sys/param.h> 78 79 #include <sys/mount.h> 80 81 #include <assert.h> 82 #include <errno.h> 83 #include <fcntl.h> 84 #include <stdarg.h> 85 #include <stdint.h> 86 #include <stdio.h> 87 #include <stdlib.h> 88 #include <string.h> 89 #include <time.h> 90 #include <unistd.h> 91 #include <util.h> 92 93 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS 94 #include <sys/statvfs.h> 95 #endif 96 97 #include <ufs/ufs/dinode.h> 98 #include <ufs/ufs/dir.h> 99 #include <ufs/ffs/fs.h> 100 101 #include "ffs/ufs_bswap.h" 102 #include "ffs/ufs_inode.h" 103 #include "ffs/newfs_extern.h" 104 #include "ffs/ffs_extern.h" 105 106 #undef clrbuf 107 #include "makefs.h" 108 #include "ffs.h" 109 110 #undef DIP 111 #define DIP(dp, field) \ 112 ((ffs_opts->version == 1) ? \ 113 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field) 114 115 /* 116 * Various file system defaults (cribbed from newfs(8)). 117 */ 118 #define DFL_FRAGSIZE 4096 /* fragment size */ 119 #define DFL_BLKSIZE 32768 /* block size */ 120 #define DFL_SECSIZE 512 /* sector size */ 121 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */ 122 #define DFL_FRAGSPERINODE 4 /* fragments per inode */ 123 #define DFL_ROTDELAY 0 /* rotational delay */ 124 #define DFL_NRPOS 1 /* rotational positions */ 125 #define DFL_RPM 3600 /* rpm of disk */ 126 #define DFL_NSECTORS 64 /* # of sectors */ 127 #define DFL_NTRACKS 16 /* # of tracks */ 128 129 130 typedef struct { 131 u_char *buf; /* buf for directory */ 132 doff_t size; /* full size of buf */ 133 doff_t cur; /* offset of current entry */ 134 } dirbuf_t; 135 136 137 static int ffs_create_image(const char *, fsinfo_t *); 138 static void ffs_dump_fsinfo(fsinfo_t *); 139 static void ffs_dump_dirbuf(dirbuf_t *, const char *, int); 140 static void ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int); 141 static int ffs_populate_dir(const char *, fsnode *, fsinfo_t *); 142 static void ffs_size_dir(fsnode *, fsinfo_t *); 143 static void ffs_validate(const char *, fsnode *, fsinfo_t *); 144 static void ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *); 145 static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *); 146 static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *, 147 fsnode *, fsinfo_t *); 148 static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *, 149 fsnode *, fsinfo_t *); 150 151 152 /* publicly visible functions */ 153 154 void 155 ffs_prep_opts(fsinfo_t *fsopts) 156 { 157 ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts)); 158 159 const option_t ffs_options[] = { 160 { 'b', "bsize", &ffs_opts->bsize, OPT_INT32, 161 1, INT_MAX, "block size" }, 162 { 'f', "fsize", &ffs_opts->fsize, OPT_INT32, 163 1, INT_MAX, "fragment size" }, 164 { 'd', "density", &ffs_opts->density, OPT_INT32, 165 1, INT_MAX, "bytes per inode" }, 166 { 'm', "minfree", &ffs_opts->minfree, OPT_INT32, 167 0, 99, "minfree" }, 168 { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32, 169 1, INT_MAX, "max blocks per file in a cg" }, 170 { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32, 171 1, INT_MAX, "expected average file size" }, 172 { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32, 173 1, INT_MAX, "expected # of files per directory" }, 174 { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32, 175 1, INT_MAX, "maximum # extent size" }, 176 { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32, 177 1, INT_MAX, "max # of blocks per group" }, 178 { 'v', "version", &ffs_opts->version, OPT_INT32, 179 1, 2, "UFS version" }, 180 { 'o', "optimization", NULL, OPT_STRBUF, 181 0, 0, "Optimization (time|space)" }, 182 { 'l', "label", ffs_opts->label, OPT_STRARRAY, 183 1, sizeof(ffs_opts->label), "UFS label" }, 184 { 's', "softupdates", &ffs_opts->softupdates, OPT_INT32, 185 0, 1, "enable softupdates" }, 186 { .name = NULL } 187 }; 188 189 ffs_opts->bsize= -1; 190 ffs_opts->fsize= -1; 191 ffs_opts->cpg= -1; 192 ffs_opts->density= -1; 193 ffs_opts->min_inodes= false; 194 ffs_opts->minfree= -1; 195 ffs_opts->optimization= -1; 196 ffs_opts->maxcontig= -1; 197 ffs_opts->maxbpg= -1; 198 ffs_opts->avgfilesize= -1; 199 ffs_opts->avgfpdir= -1; 200 ffs_opts->version = 1; 201 ffs_opts->softupdates = 0; 202 203 fsopts->fs_specific = ffs_opts; 204 fsopts->fs_options = copy_opts(ffs_options); 205 } 206 207 void 208 ffs_cleanup_opts(fsinfo_t *fsopts) 209 { 210 free(fsopts->fs_specific); 211 free(fsopts->fs_options); 212 } 213 214 int 215 ffs_parse_opts(const char *option, fsinfo_t *fsopts) 216 { 217 ffs_opt_t *ffs_opts = fsopts->fs_specific; 218 option_t *ffs_options = fsopts->fs_options; 219 char buf[1024]; 220 221 int rv; 222 223 assert(option != NULL); 224 assert(fsopts != NULL); 225 assert(ffs_opts != NULL); 226 227 if (debug & DEBUG_FS_PARSE_OPTS) 228 printf("ffs_parse_opts: got `%s'\n", option); 229 230 rv = set_option(ffs_options, option, buf, sizeof(buf)); 231 if (rv == -1) 232 return 0; 233 234 if (ffs_options[rv].name == NULL) 235 abort(); 236 237 switch (ffs_options[rv].letter) { 238 case 'o': 239 if (strcmp(buf, "time") == 0) { 240 ffs_opts->optimization = FS_OPTTIME; 241 } else if (strcmp(buf, "space") == 0) { 242 ffs_opts->optimization = FS_OPTSPACE; 243 } else { 244 warnx("Invalid optimization `%s'", buf); 245 return 0; 246 } 247 break; 248 default: 249 break; 250 } 251 return 1; 252 } 253 254 255 void 256 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) 257 { 258 struct fs *superblock; 259 struct timeval start; 260 261 assert(image != NULL); 262 assert(dir != NULL); 263 assert(root != NULL); 264 assert(fsopts != NULL); 265 266 if (debug & DEBUG_FS_MAKEFS) 267 printf("ffs_makefs: image %s directory %s root %p\n", 268 image, dir, root); 269 270 /* if user wants no free space, use minimum number of inodes */ 271 if (fsopts->minsize == 0 && fsopts->freeblockpc == 0 && 272 fsopts->freeblocks == 0) 273 ((ffs_opt_t *)fsopts->fs_specific)->min_inodes = true; 274 275 /* validate tree and options */ 276 TIMER_START(start); 277 ffs_validate(dir, root, fsopts); 278 TIMER_RESULTS(start, "ffs_validate"); 279 280 printf("Calculated size of `%s': %lld bytes, %lld inodes\n", 281 image, (long long)fsopts->size, (long long)fsopts->inodes); 282 283 /* create image */ 284 TIMER_START(start); 285 if (ffs_create_image(image, fsopts) == -1) 286 errx(1, "Image file `%s' not created.", image); 287 TIMER_RESULTS(start, "ffs_create_image"); 288 289 fsopts->curinode = UFS_ROOTINO; 290 291 if (debug & DEBUG_FS_MAKEFS) 292 putchar('\n'); 293 294 /* populate image */ 295 printf("Populating `%s'\n", image); 296 TIMER_START(start); 297 if (! ffs_populate_dir(dir, root, fsopts)) 298 errx(1, "Image file `%s' not populated.", image); 299 TIMER_RESULTS(start, "ffs_populate_dir"); 300 301 /* ensure no outstanding buffers remain */ 302 if (debug & DEBUG_FS_MAKEFS) 303 bcleanup(); 304 305 /* update various superblock parameters */ 306 superblock = fsopts->superblock; 307 superblock->fs_fmod = 0; 308 superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir; 309 superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree; 310 superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree; 311 superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree; 312 313 /* write out superblock; image is now complete */ 314 ffs_write_superblock(fsopts->superblock, fsopts); 315 if (close(fsopts->fd) == -1) 316 err(1, "Closing `%s'", image); 317 fsopts->fd = -1; 318 printf("Image `%s' complete\n", image); 319 } 320 321 /* end of public functions */ 322 323 324 static void 325 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts) 326 { 327 #ifdef notyet 328 int32_t spc, nspf, ncyl, fssize; 329 #endif 330 ffs_opt_t *ffs_opts = fsopts->fs_specific; 331 332 assert(dir != NULL); 333 assert(root != NULL); 334 assert(fsopts != NULL); 335 assert(ffs_opts != NULL); 336 337 if (debug & DEBUG_FS_VALIDATE) { 338 printf("ffs_validate: before defaults set:\n"); 339 ffs_dump_fsinfo(fsopts); 340 } 341 342 /* set FFS defaults */ 343 if (fsopts->sectorsize == -1) 344 fsopts->sectorsize = DFL_SECSIZE; 345 if (ffs_opts->fsize == -1) 346 ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize); 347 if (ffs_opts->bsize == -1) 348 ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize); 349 if (ffs_opts->cpg == -1) 350 ffs_opts->cpg = DFL_CYLSPERGROUP; 351 else 352 ffs_opts->cpgflg = 1; 353 /* fsopts->density is set below */ 354 if (ffs_opts->nsectors == -1) 355 ffs_opts->nsectors = DFL_NSECTORS; 356 if (ffs_opts->minfree == -1) 357 ffs_opts->minfree = MINFREE; 358 if (ffs_opts->optimization == -1) 359 ffs_opts->optimization = DEFAULTOPT; 360 if (ffs_opts->maxcontig == -1) 361 ffs_opts->maxcontig = 362 MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize); 363 /* XXX ondisk32 */ 364 if (ffs_opts->maxbpg == -1) 365 ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t); 366 if (ffs_opts->avgfilesize == -1) 367 ffs_opts->avgfilesize = AVFILESIZ; 368 if (ffs_opts->avgfpdir == -1) 369 ffs_opts->avgfpdir = AFPDIR; 370 371 if (fsopts->maxsize > 0 && 372 roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize) 373 errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d " 374 "exceeds maxsize %lld. Lower bsize, or round the minimum " 375 "and maximum sizes to bsize.", dir, 376 (long long)fsopts->minsize, ffs_opts->bsize, 377 (long long)fsopts->maxsize); 378 379 /* calculate size of tree */ 380 ffs_size_dir(root, fsopts); 381 fsopts->inodes += UFS_ROOTINO; /* include first two inodes */ 382 383 if (debug & DEBUG_FS_VALIDATE) 384 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n", 385 (long long)fsopts->size, (long long)fsopts->inodes); 386 387 /* add requested slop */ 388 fsopts->size += fsopts->freeblocks; 389 fsopts->inodes += fsopts->freefiles; 390 if (fsopts->freefilepc > 0) 391 fsopts->inodes = 392 fsopts->inodes * (100 + fsopts->freefilepc) / 100; 393 if (fsopts->freeblockpc > 0) 394 fsopts->size = 395 fsopts->size * (100 + fsopts->freeblockpc) / 100; 396 397 /* 398 * Add space needed for superblock, cylblock and to store inodes. 399 * All of those segments are aligned to the block size. 400 * XXX: This has to match calculations done in ffs_mkfs. 401 */ 402 if (ffs_opts->version == 1) { 403 fsopts->size += 404 roundup(SBLOCK_UFS1 + SBLOCKSIZE, ffs_opts->bsize); 405 fsopts->size += roundup(SBLOCKSIZE, ffs_opts->bsize); 406 fsopts->size += ffs_opts->bsize; 407 fsopts->size += DINODE1_SIZE * 408 roundup(fsopts->inodes, ffs_opts->bsize / DINODE1_SIZE); 409 } else { 410 fsopts->size += 411 roundup(SBLOCK_UFS2 + SBLOCKSIZE, ffs_opts->bsize); 412 fsopts->size += roundup(SBLOCKSIZE, ffs_opts->bsize); 413 fsopts->size += ffs_opts->bsize; 414 fsopts->size += DINODE2_SIZE * 415 roundup(fsopts->inodes, ffs_opts->bsize / DINODE2_SIZE); 416 } 417 418 /* add minfree */ 419 if (ffs_opts->minfree > 0) 420 fsopts->size = 421 fsopts->size * (100 + ffs_opts->minfree) / 100; 422 /* 423 * XXX any other fs slop to add, such as csum's, bitmaps, etc ?? 424 */ 425 426 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */ 427 fsopts->size = fsopts->minsize; 428 429 /* round up to the next block */ 430 fsopts->size = roundup(fsopts->size, ffs_opts->bsize); 431 432 /* round up to requested block size, if any */ 433 if (fsopts->roundup > 0) 434 fsopts->size = roundup(fsopts->size, fsopts->roundup); 435 436 /* calculate density to just fit inodes if no free space */ 437 if (ffs_opts->density == -1) 438 ffs_opts->density = fsopts->size / fsopts->inodes + 1; 439 440 if (debug & DEBUG_FS_VALIDATE) { 441 printf("ffs_validate: after defaults set:\n"); 442 ffs_dump_fsinfo(fsopts); 443 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n", 444 dir, (long long)fsopts->size, (long long)fsopts->inodes); 445 } 446 /* now check calculated sizes vs requested sizes */ 447 if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) { 448 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.", 449 dir, (long long)fsopts->size, (long long)fsopts->maxsize); 450 } 451 } 452 453 454 static void 455 ffs_dump_fsinfo(fsinfo_t *f) 456 { 457 458 ffs_opt_t *fs = f->fs_specific; 459 460 printf("fsopts at %p\n", f); 461 462 printf("\tsize %lld, inodes %lld, curinode %u\n", 463 (long long)f->size, (long long)f->inodes, f->curinode); 464 465 printf("\tminsize %lld, maxsize %lld\n", 466 (long long)f->minsize, (long long)f->maxsize); 467 printf("\tfree files %lld, freefile %% %d\n", 468 (long long)f->freefiles, f->freefilepc); 469 printf("\tfree blocks %lld, freeblock %% %d\n", 470 (long long)f->freeblocks, f->freeblockpc); 471 printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize); 472 473 printf("\tbsize %d, fsize %d, cpg %d, density %d\n", 474 fs->bsize, fs->fsize, fs->cpg, fs->density); 475 printf("\tnsectors %d, rpm %d, minfree %d\n", 476 fs->nsectors, fs->rpm, fs->minfree); 477 printf("\tmaxcontig %d, maxbpg %d\n", 478 fs->maxcontig, fs->maxbpg); 479 printf("\toptimization %s\n", 480 fs->optimization == FS_OPTSPACE ? "space" : "time"); 481 } 482 483 484 static int 485 ffs_create_image(const char *image, fsinfo_t *fsopts) 486 { 487 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS 488 struct statvfs sfs; 489 #endif 490 struct fs *fs; 491 char *buf; 492 int i, bufsize; 493 off_t bufrem; 494 int oflags = O_RDWR | O_CREAT; 495 time_t tstamp; 496 497 assert (image != NULL); 498 assert (fsopts != NULL); 499 500 /* create image */ 501 if (fsopts->offset == 0) 502 oflags |= O_TRUNC; 503 if ((fsopts->fd = open(image, oflags, 0666)) == -1) { 504 warn("Can't open `%s' for writing", image); 505 return (-1); 506 } 507 508 /* zero image */ 509 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS 510 if (fstatvfs(fsopts->fd, &sfs) == -1) { 511 #endif 512 bufsize = 8192; 513 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS 514 warn("can't fstatvfs `%s', using default %d byte chunk", 515 image, bufsize); 516 } else 517 bufsize = sfs.f_iosize; 518 #endif 519 bufrem = fsopts->size; 520 if (fsopts->sparse) { 521 if (ftruncate(fsopts->fd, bufrem) == -1) { 522 warn("sparse option disabled."); 523 fsopts->sparse = 0; 524 } 525 } 526 if (fsopts->sparse) { 527 /* File truncated at bufrem. Remaining is 0 */ 528 bufrem = 0; 529 buf = NULL; 530 } else { 531 if (debug & DEBUG_FS_CREATE_IMAGE) 532 printf("zero-ing image `%s', %lld sectors, " 533 "using %d byte chunks\n", image, (long long)bufrem, 534 bufsize); 535 buf = ecalloc(1, bufsize); 536 } 537 538 if (fsopts->offset != 0) 539 if (lseek(fsopts->fd, fsopts->offset, SEEK_SET) == -1) { 540 warn("can't seek"); 541 free(buf); 542 return -1; 543 } 544 545 while (bufrem > 0) { 546 i = write(fsopts->fd, buf, MIN(bufsize, bufrem)); 547 if (i == -1) { 548 warn("zeroing image, %lld bytes to go", 549 (long long)bufrem); 550 free(buf); 551 return (-1); 552 } 553 bufrem -= i; 554 } 555 if (buf) 556 free(buf); 557 558 /* make the file system */ 559 if (debug & DEBUG_FS_CREATE_IMAGE) 560 printf("calling mkfs(\"%s\", ...)\n", image); 561 562 if (stampst.st_ino != 0) 563 tstamp = stampst.st_ctime; 564 else 565 tstamp = start_time.tv_sec; 566 567 srandom(tstamp); 568 569 fs = ffs_mkfs(image, fsopts, tstamp); 570 fsopts->superblock = (void *)fs; 571 if (debug & DEBUG_FS_CREATE_IMAGE) { 572 time_t t; 573 574 t = (time_t)((struct fs *)fsopts->superblock)->fs_time; 575 printf("mkfs returned %p; fs_time %s", 576 fsopts->superblock, ctime(&t)); 577 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n", 578 (long long)fs->fs_cstotal.cs_nbfree, 579 (long long)fs->fs_cstotal.cs_nffree, 580 (long long)fs->fs_cstotal.cs_nifree, 581 (long long)fs->fs_cstotal.cs_ndir); 582 } 583 584 if (fs->fs_cstotal.cs_nifree + (off_t)UFS_ROOTINO < fsopts->inodes) { 585 warnx( 586 "Image file `%s' has %lld free inodes; %lld are required.", 587 image, 588 (long long)(fs->fs_cstotal.cs_nifree + UFS_ROOTINO), 589 (long long)fsopts->inodes); 590 return (-1); 591 } 592 return (fsopts->fd); 593 } 594 595 596 static void 597 ffs_size_dir(fsnode *root, fsinfo_t *fsopts) 598 { 599 struct direct tmpdir; 600 fsnode * node; 601 int curdirsize, this; 602 ffs_opt_t *ffs_opts = fsopts->fs_specific; 603 604 /* node may be NULL (empty directory) */ 605 assert(fsopts != NULL); 606 assert(ffs_opts != NULL); 607 608 if (debug & DEBUG_FS_SIZE_DIR) 609 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n", 610 (long long)fsopts->size, (long long)fsopts->inodes); 611 612 #define ADDDIRENT(e) do { \ 613 tmpdir.d_namlen = strlen((e)); \ 614 this = DIRSIZ_SWAP(0, &tmpdir, 0); \ 615 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 616 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \ 617 e, tmpdir.d_namlen, this, curdirsize); \ 618 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \ 619 curdirsize = roundup(curdirsize, DIRBLKSIZ); \ 620 curdirsize += this; \ 621 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 622 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \ 623 e, tmpdir.d_namlen, this, curdirsize); \ 624 } while (0); 625 626 #define ADDSIZE(x) do { \ 627 if ((size_t)(x) < UFS_NDADDR * (size_t)ffs_opts->bsize) { \ 628 fsopts->size += roundup((x), ffs_opts->fsize); \ 629 } else { \ 630 /* Count space consumed by indirecttion blocks. */ \ 631 fsopts->size += ffs_opts->bsize * \ 632 (howmany((x), UFS_NDADDR * ffs_opts->bsize) - 1); \ 633 /* \ 634 * If the file is big enough to use indirect blocks, \ 635 * we allocate bsize block for trailing data. \ 636 */ \ 637 fsopts->size += roundup((x), ffs_opts->bsize); \ 638 } \ 639 } while (0); 640 641 curdirsize = 0; 642 for (node = root; node != NULL; node = node->next) { 643 ADDDIRENT(node->name); 644 if (node == root) { /* we're at "." */ 645 assert(strcmp(node->name, ".") == 0); 646 ADDDIRENT(".."); 647 } else if ((node->inode->flags & FI_SIZED) == 0) { 648 /* don't count duplicate names */ 649 node->inode->flags |= FI_SIZED; 650 if (debug & DEBUG_FS_SIZE_DIR_NODE) 651 printf("ffs_size_dir: `%s' size %lld\n", 652 node->name, 653 (long long)node->inode->st.st_size); 654 fsopts->inodes++; 655 if (node->type == S_IFREG) 656 ADDSIZE(node->inode->st.st_size); 657 if (node->type == S_IFLNK) { 658 size_t slen; 659 660 slen = strlen(node->symlink) + 1; 661 if (slen >= (ffs_opts->version == 1 ? 662 UFS1_MAXSYMLINKLEN : 663 UFS2_MAXSYMLINKLEN)) 664 ADDSIZE(slen); 665 } 666 } 667 if (node->type == S_IFDIR) 668 ffs_size_dir(node->child, fsopts); 669 } 670 ADDSIZE(curdirsize); 671 672 if (debug & DEBUG_FS_SIZE_DIR) 673 printf("ffs_size_dir: exit: size %lld inodes %lld\n", 674 (long long)fsopts->size, (long long)fsopts->inodes); 675 } 676 677 static void * 678 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur, 679 fsnode *root, fsinfo_t *fsopts) 680 { 681 size_t slen; 682 void *membuf; 683 struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st; 684 685 memset(dinp, 0, sizeof(*dinp)); 686 dinp->di_mode = cur->inode->st.st_mode; 687 dinp->di_nlink = cur->inode->nlink; 688 dinp->di_size = cur->inode->st.st_size; 689 #if HAVE_STRUCT_STAT_ST_FLAGS 690 dinp->di_flags = cur->inode->st.st_flags; 691 #endif 692 dinp->di_gen = random(); 693 dinp->di_uid = cur->inode->st.st_uid; 694 dinp->di_gid = cur->inode->st.st_gid; 695 696 dinp->di_atime = st->st_atime; 697 dinp->di_mtime = st->st_mtime; 698 dinp->di_ctime = st->st_ctime; 699 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 700 dinp->di_atimensec = st->st_atimensec; 701 dinp->di_mtimensec = st->st_mtimensec; 702 dinp->di_ctimensec = st->st_ctimensec; 703 #endif 704 /* not set: di_db, di_ib, di_blocks, di_spare */ 705 706 membuf = NULL; 707 if (cur == root) { /* "."; write dirbuf */ 708 membuf = dbufp->buf; 709 dinp->di_size = dbufp->size; 710 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { 711 dinp->di_size = 0; /* a device */ 712 dinp->di_rdev = 713 ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap); 714 } else if (S_ISLNK(cur->type)) { /* symlink */ 715 slen = strlen(cur->symlink); 716 if (slen < UFS1_MAXSYMLINKLEN) { /* short link */ 717 memcpy(dinp->di_shortlink, cur->symlink, slen); 718 } else 719 membuf = cur->symlink; 720 dinp->di_size = slen; 721 } 722 return membuf; 723 } 724 725 static void * 726 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur, 727 fsnode *root, fsinfo_t *fsopts) 728 { 729 size_t slen; 730 void *membuf; 731 struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st; 732 733 memset(dinp, 0, sizeof(*dinp)); 734 dinp->di_mode = cur->inode->st.st_mode; 735 dinp->di_nlink = cur->inode->nlink; 736 dinp->di_size = cur->inode->st.st_size; 737 #if HAVE_STRUCT_STAT_ST_FLAGS 738 dinp->di_flags = cur->inode->st.st_flags; 739 #endif 740 dinp->di_gen = random(); 741 dinp->di_uid = cur->inode->st.st_uid; 742 dinp->di_gid = cur->inode->st.st_gid; 743 744 dinp->di_atime = st->st_atime; 745 dinp->di_mtime = st->st_mtime; 746 dinp->di_ctime = st->st_ctime; 747 #if HAVE_STRUCT_STAT_BIRTHTIME 748 dinp->di_birthtime = st->st_birthtime; 749 #else 750 dinp->di_birthtime = st->st_ctime; 751 #endif 752 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 753 dinp->di_atimensec = st->st_atimensec; 754 dinp->di_mtimensec = st->st_mtimensec; 755 dinp->di_ctimensec = st->st_ctimensec; 756 #if HAVE_STRUCT_STAT_BIRTHTIME 757 dinp->di_birthnsec = st->st_birthtimensec; 758 #else 759 dinp->di_birthnsec = st->st_ctimensec; 760 #endif 761 #endif 762 763 /* not set: di_db, di_ib, di_blocks, di_spare */ 764 765 membuf = NULL; 766 if (cur == root) { /* "."; write dirbuf */ 767 membuf = dbufp->buf; 768 dinp->di_size = dbufp->size; 769 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { 770 dinp->di_size = 0; /* a device */ 771 dinp->di_rdev = 772 ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap); 773 } else if (S_ISLNK(cur->type)) { /* symlink */ 774 slen = strlen(cur->symlink); 775 if (slen < UFS2_MAXSYMLINKLEN) { /* short link */ 776 memcpy(dinp->di_shortlink, cur->symlink, slen); 777 } else 778 membuf = cur->symlink; 779 dinp->di_size = slen; 780 } 781 return membuf; 782 } 783 784 static int 785 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts) 786 { 787 fsnode *cur; 788 dirbuf_t dirbuf; 789 union dinode din; 790 void *membuf; 791 char path[MAXPATHLEN + 1]; 792 ffs_opt_t *ffs_opts = fsopts->fs_specific; 793 794 assert(dir != NULL); 795 assert(root != NULL); 796 assert(fsopts != NULL); 797 assert(ffs_opts != NULL); 798 799 (void)memset(&dirbuf, 0, sizeof(dirbuf)); 800 801 if (debug & DEBUG_FS_POPULATE) 802 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root); 803 804 /* 805 * pass 1: allocate inode numbers, build directory `file' 806 */ 807 for (cur = root; cur != NULL; cur = cur->next) { 808 if ((cur->inode->flags & FI_ALLOCATED) == 0) { 809 cur->inode->flags |= FI_ALLOCATED; 810 if (cur == root && cur->parent != NULL) 811 cur->inode->ino = cur->parent->inode->ino; 812 else { 813 cur->inode->ino = fsopts->curinode; 814 fsopts->curinode++; 815 } 816 } 817 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap); 818 if (cur == root) { /* we're at "."; add ".." */ 819 ffs_make_dirbuf(&dirbuf, "..", 820 cur->parent == NULL ? cur : cur->parent->first, 821 fsopts->needswap); 822 root->inode->nlink++; /* count my parent's link */ 823 } else if (cur->child != NULL) 824 root->inode->nlink++; /* count my child's link */ 825 826 /* 827 * XXX possibly write file and long symlinks here, 828 * ensuring that blocks get written before inodes? 829 * otoh, this isn't a real filesystem, so who 830 * cares about ordering? :-) 831 */ 832 } 833 if (debug & DEBUG_FS_POPULATE_DIRBUF) 834 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap); 835 836 /* 837 * pass 2: write out dirbuf, then non-directories at this level 838 */ 839 if (debug & DEBUG_FS_POPULATE) 840 printf("ffs_populate_dir: PASS 2 dir %s\n", dir); 841 for (cur = root; cur != NULL; cur = cur->next) { 842 if (cur->inode->flags & FI_WRITTEN) 843 continue; /* skip hard-linked entries */ 844 cur->inode->flags |= FI_WRITTEN; 845 846 if (cur->contents == NULL) { 847 if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root, 848 cur->path, cur->name) >= (int)sizeof(path)) 849 errx(1, "Pathname too long."); 850 } 851 852 if (cur->child != NULL) 853 continue; /* child creates own inode */ 854 855 /* build on-disk inode */ 856 if (ffs_opts->version == 1) 857 membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur, 858 root, fsopts); 859 else 860 membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur, 861 root, fsopts); 862 863 if (debug & DEBUG_FS_POPULATE_NODE) { 864 printf("ffs_populate_dir: writing ino %d, %s", 865 cur->inode->ino, inode_type(cur->type)); 866 if (cur->inode->nlink > 1) 867 printf(", nlink %d", cur->inode->nlink); 868 putchar('\n'); 869 } 870 871 if (membuf != NULL) { 872 ffs_write_file(&din, cur->inode->ino, membuf, fsopts); 873 } else if (S_ISREG(cur->type)) { 874 ffs_write_file(&din, cur->inode->ino, 875 (cur->contents) ? cur->contents : path, fsopts); 876 } else { 877 assert (! S_ISDIR(cur->type)); 878 ffs_write_inode(&din, cur->inode->ino, fsopts); 879 } 880 } 881 882 /* 883 * pass 3: write out sub-directories 884 */ 885 if (debug & DEBUG_FS_POPULATE) 886 printf("ffs_populate_dir: PASS 3 dir %s\n", dir); 887 for (cur = root; cur != NULL; cur = cur->next) { 888 if (cur->child == NULL) 889 continue; 890 if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir, 891 cur->name) >= sizeof(path)) 892 errx(1, "Pathname too long."); 893 if (! ffs_populate_dir(path, cur->child, fsopts)) 894 return (0); 895 } 896 897 if (debug & DEBUG_FS_POPULATE) 898 printf("ffs_populate_dir: DONE dir %s\n", dir); 899 900 /* cleanup */ 901 if (dirbuf.buf != NULL) 902 free(dirbuf.buf); 903 return (1); 904 } 905 906 907 static void 908 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts) 909 { 910 int isfile, ffd; 911 char *fbuf, *p; 912 off_t bufleft, chunk, offset; 913 ssize_t nread; 914 struct inode in; 915 struct m_buf * bp; 916 ffs_opt_t *ffs_opts = fsopts->fs_specific; 917 struct m_vnode vp = { fsopts, NULL }; 918 919 assert (din != NULL); 920 assert (buf != NULL); 921 assert (fsopts != NULL); 922 assert (ffs_opts != NULL); 923 924 isfile = S_ISREG(DIP(din, mode)); 925 fbuf = NULL; 926 ffd = -1; 927 p = NULL; 928 929 in.i_fs = (struct fs *)fsopts->superblock; 930 in.i_devvp = (void *)&vp; 931 932 if (debug & DEBUG_FS_WRITE_FILE) { 933 printf( 934 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld", 935 ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT), 936 (long long)DIP(din, size)); 937 if (isfile) 938 printf(", file '%s'\n", (char *)buf); 939 else 940 printf(", buffer %p\n", buf); 941 } 942 943 in.i_number = ino; 944 in.i_size = DIP(din, size); 945 if (ffs_opts->version == 1) 946 memcpy(&in.i_din.ffs1_din, &din->ffs1_din, 947 sizeof(in.i_din.ffs1_din)); 948 else 949 memcpy(&in.i_din.ffs2_din, &din->ffs2_din, 950 sizeof(in.i_din.ffs2_din)); 951 952 if (DIP(din, size) == 0) 953 goto write_inode_and_leave; /* mmm, cheating */ 954 955 if (isfile) { 956 fbuf = emalloc(ffs_opts->bsize); 957 if ((ffd = open((char *)buf, O_RDONLY)) == -1) { 958 err(EXIT_FAILURE, "Can't open `%s' for reading", (char *)buf); 959 } 960 } else { 961 p = buf; 962 } 963 964 chunk = 0; 965 for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) { 966 chunk = MIN(bufleft, ffs_opts->bsize); 967 if (!isfile) 968 ; 969 else if ((nread = read(ffd, fbuf, chunk)) == -1) 970 err(EXIT_FAILURE, "Reading `%s', %lld bytes to go", 971 (char *)buf, (long long)bufleft); 972 else if (nread != chunk) 973 errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, " 974 "read %zd bytes, expected %ju bytes, does " 975 "metalog size= attribute mismatch source size?", 976 (char *)buf, (long long)bufleft, nread, 977 (uintmax_t)chunk); 978 else 979 p = fbuf; 980 offset = DIP(din, size) - bufleft; 981 if (debug & DEBUG_FS_WRITE_FILE_BLOCK) 982 printf( 983 "ffs_write_file: write %p offset %lld size %lld left %lld\n", 984 p, (long long)offset, 985 (long long)chunk, (long long)bufleft); 986 /* 987 * XXX if holey support is desired, do the check here 988 * 989 * XXX might need to write out last bit in fragroundup 990 * sized chunk. however, ffs_balloc() handles this for us 991 */ 992 errno = ffs_balloc(&in, offset, chunk, &bp); 993 bad_ffs_write_file: 994 if (errno != 0) 995 err(1, 996 "Writing inode %d (%s), bytes %lld + %lld", 997 ino, 998 isfile ? (char *)buf : 999 inode_type(DIP(din, mode) & S_IFMT), 1000 (long long)offset, (long long)chunk); 1001 memcpy(bp->b_data, p, chunk); 1002 errno = bwrite(bp); 1003 if (errno != 0) 1004 goto bad_ffs_write_file; 1005 if (!isfile) 1006 p += chunk; 1007 } 1008 1009 write_inode_and_leave: 1010 ffs_write_inode(&in.i_din, in.i_number, fsopts); 1011 if (fbuf) 1012 free(fbuf); 1013 if (ffd != -1) 1014 close(ffd); 1015 } 1016 1017 1018 static void 1019 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap) 1020 { 1021 doff_t i; 1022 struct direct *de; 1023 uint16_t reclen; 1024 1025 assert (dbuf != NULL); 1026 assert (dir != NULL); 1027 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n", 1028 dir, dbuf->size, dbuf->cur); 1029 1030 for (i = 0; i < dbuf->size; ) { 1031 de = (struct direct *)(dbuf->buf + i); 1032 reclen = ufs_rw16(de->d_reclen, needswap); 1033 printf( 1034 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n", 1035 ufs_rw32(de->d_ino, needswap), 1036 inode_type(DTTOIF(de->d_type)), i, reclen, 1037 de->d_namlen, de->d_name); 1038 i += reclen; 1039 assert(reclen > 0); 1040 } 1041 } 1042 1043 static void 1044 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) 1045 { 1046 struct direct de, *dp; 1047 uint16_t llen, reclen; 1048 u_char *newbuf; 1049 1050 assert (dbuf != NULL); 1051 assert (name != NULL); 1052 assert (node != NULL); 1053 /* create direct entry */ 1054 (void)memset(&de, 0, sizeof(de)); 1055 de.d_ino = ufs_rw32(node->inode->ino, needswap); 1056 de.d_type = IFTODT(node->type); 1057 de.d_namlen = (uint8_t)strlen(name); 1058 strcpy(de.d_name, name); 1059 reclen = DIRSIZ_SWAP(0, &de, needswap); 1060 de.d_reclen = ufs_rw16(reclen, needswap); 1061 1062 dp = (struct direct *)(dbuf->buf + dbuf->cur); 1063 llen = 0; 1064 if (dp != NULL) 1065 llen = DIRSIZ_SWAP(0, dp, needswap); 1066 1067 if (debug & DEBUG_FS_MAKE_DIRBUF) 1068 printf( 1069 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n" 1070 " ino %d type %d reclen %d namlen %d name %.30s\n", 1071 dbuf->size, dbuf->cur, llen, 1072 ufs_rw32(de.d_ino, needswap), de.d_type, reclen, 1073 de.d_namlen, de.d_name); 1074 1075 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { 1076 if (debug & DEBUG_FS_MAKE_DIRBUF) 1077 printf("ffs_make_dirbuf: growing buf to %d\n", 1078 dbuf->size + DIRBLKSIZ); 1079 newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ); 1080 dbuf->buf = newbuf; 1081 dbuf->size += DIRBLKSIZ; 1082 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); 1083 dbuf->cur = dbuf->size - DIRBLKSIZ; 1084 } else if (dp) { /* shrink end of previous */ 1085 dp->d_reclen = ufs_rw16(llen,needswap); 1086 dbuf->cur += llen; 1087 } 1088 dp = (struct direct *)(dbuf->buf + dbuf->cur); 1089 memcpy(dp, &de, reclen); 1090 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap); 1091 } 1092 1093 /* 1094 * cribbed from sys/ufs/ffs/ffs_alloc.c 1095 */ 1096 static void 1097 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts) 1098 { 1099 char *buf; 1100 struct ufs1_dinode *dp1; 1101 struct ufs2_dinode *dp2, *dip; 1102 struct cg *cgp; 1103 struct fs *fs; 1104 int cg, cgino; 1105 uint32_t i; 1106 daddr_t d; 1107 char sbbuf[FFS_MAXBSIZE]; 1108 uint32_t initediblk; 1109 ffs_opt_t *ffs_opts = fsopts->fs_specific; 1110 1111 assert (dp != NULL); 1112 assert (ino > 0); 1113 assert (fsopts != NULL); 1114 assert (ffs_opts != NULL); 1115 1116 fs = (struct fs *)fsopts->superblock; 1117 cg = ino_to_cg(fs, ino); 1118 cgino = ino % fs->fs_ipg; 1119 if (debug & DEBUG_FS_WRITE_INODE) 1120 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n", 1121 dp, ino, cg, cgino); 1122 1123 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 1124 fsopts); 1125 cgp = (struct cg *)sbbuf; 1126 if (!cg_chkmagic_swap(cgp, fsopts->needswap)) 1127 errx(1, "ffs_write_inode: cg %d: bad magic number", cg); 1128 1129 assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino)); 1130 1131 buf = emalloc(fs->fs_bsize); 1132 dp1 = (struct ufs1_dinode *)buf; 1133 dp2 = (struct ufs2_dinode *)buf; 1134 1135 if (fs->fs_cstotal.cs_nifree == 0) 1136 errx(1, "ffs_write_inode: fs out of inodes for ino %u", 1137 ino); 1138 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1139 errx(1, 1140 "ffs_write_inode: cg %d out of inodes for ino %u", 1141 cg, ino); 1142 setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino); 1143 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); 1144 fs->fs_cstotal.cs_nifree--; 1145 fs->fs_cs(fs, cg).cs_nifree--; 1146 if (S_ISDIR(DIP(dp, mode))) { 1147 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); 1148 fs->fs_cstotal.cs_ndir++; 1149 fs->fs_cs(fs, cg).cs_ndir++; 1150 } 1151 1152 /* 1153 * Initialize inode blocks on the fly for UFS2. 1154 */ 1155 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap); 1156 while (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk && 1157 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) { 1158 memset(buf, 0, fs->fs_bsize); 1159 dip = (struct ufs2_dinode *)buf; 1160 for (i = 0; i < INOPB(fs); i++) { 1161 dip->di_gen = random(); 1162 dip++; 1163 } 1164 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs, 1165 cg * fs->fs_ipg + initediblk)), 1166 fs->fs_bsize, buf, fsopts); 1167 initediblk += INOPB(fs); 1168 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap); 1169 } 1170 1171 1172 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 1173 fsopts); 1174 1175 /* now write inode */ 1176 d = fsbtodb(fs, ino_to_fsba(fs, ino)); 1177 ffs_rdfs(d, fs->fs_bsize, buf, fsopts); 1178 if (fsopts->needswap) { 1179 if (ffs_opts->version == 1) 1180 ffs_dinode1_swap(&dp->ffs1_din, 1181 &dp1[ino_to_fsbo(fs, ino)]); 1182 else 1183 ffs_dinode2_swap(&dp->ffs2_din, 1184 &dp2[ino_to_fsbo(fs, ino)]); 1185 } else { 1186 if (ffs_opts->version == 1) 1187 dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din; 1188 else 1189 dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din; 1190 } 1191 ffs_wtfs(d, fs->fs_bsize, buf, fsopts); 1192 free(buf); 1193 } 1194 1195 void 1196 panic(const char *fmt, ...) 1197 { 1198 va_list ap; 1199 1200 va_start(ap, fmt); 1201 vwarnx(fmt, ap); 1202 va_end(ap); 1203 exit(1); 1204 } 1205