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