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