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