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