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