1 /* $NetBSD: ffs.c,v 1.39 2006/04/22 17:40:49 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.39 2006/04/22 17:40:49 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 { 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 free(buf); 513 return (-1); 514 } 515 bufrem -= i; 516 } 517 free(buf); 518 519 /* make the file system */ 520 if (debug & DEBUG_FS_CREATE_IMAGE) 521 printf("calling mkfs(\"%s\", ...)\n", image); 522 fs = ffs_mkfs(image, fsopts); 523 fsopts->superblock = (void *)fs; 524 if (debug & DEBUG_FS_CREATE_IMAGE) { 525 time_t t; 526 527 t = (time_t)((struct fs *)fsopts->superblock)->fs_time; 528 printf("mkfs returned %p; fs_time %s", 529 fsopts->superblock, ctime(&t)); 530 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n", 531 (long long)fs->fs_cstotal.cs_nbfree, 532 (long long)fs->fs_cstotal.cs_nffree, 533 (long long)fs->fs_cstotal.cs_nifree, 534 (long long)fs->fs_cstotal.cs_ndir); 535 } 536 537 if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) { 538 warnx( 539 "Image file `%s' has %lld free inodes; %lld are required.", 540 image, 541 (long long)(fs->fs_cstotal.cs_nifree + ROOTINO), 542 (long long)fsopts->inodes); 543 return (-1); 544 } 545 return (fsopts->fd); 546 } 547 548 549 static void 550 ffs_size_dir(fsnode *root, fsinfo_t *fsopts) 551 { 552 struct direct tmpdir; 553 fsnode * node; 554 int curdirsize, this; 555 ffs_opt_t *ffs_opts = fsopts->fs_specific; 556 557 /* node may be NULL (empty directory) */ 558 assert(fsopts != NULL); 559 assert(ffs_opts != NULL); 560 561 if (debug & DEBUG_FS_SIZE_DIR) 562 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n", 563 (long long)fsopts->size, (long long)fsopts->inodes); 564 565 #define ADDDIRENT(e) do { \ 566 tmpdir.d_namlen = strlen((e)); \ 567 this = DIRSIZ(0, &tmpdir, 0); \ 568 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 569 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \ 570 e, tmpdir.d_namlen, this, curdirsize); \ 571 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \ 572 curdirsize = roundup(curdirsize, DIRBLKSIZ); \ 573 curdirsize += this; \ 574 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 575 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \ 576 e, tmpdir.d_namlen, this, curdirsize); \ 577 } while (0); 578 579 /* 580 * XXX this needs to take into account extra space consumed 581 * by indirect blocks, etc. 582 */ 583 #define ADDSIZE(x) do { \ 584 fsopts->size += roundup((x), ffs_opts->fsize); \ 585 } while (0); 586 587 curdirsize = 0; 588 for (node = root; node != NULL; node = node->next) { 589 ADDDIRENT(node->name); 590 if (FSNODE_EXCLUDE_P(fsopts, node)) 591 continue; 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 (FSNODE_EXCLUDE_P(fsopts, cur)) 750 continue; 751 if ((cur->inode->flags & FI_ALLOCATED) == 0) { 752 cur->inode->flags |= FI_ALLOCATED; 753 if (cur == root && cur->parent != NULL) 754 cur->inode->ino = cur->parent->inode->ino; 755 else { 756 cur->inode->ino = fsopts->curinode; 757 fsopts->curinode++; 758 } 759 } 760 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap); 761 if (cur == root) { /* we're at "."; add ".." */ 762 ffs_make_dirbuf(&dirbuf, "..", 763 cur->parent == NULL ? cur : cur->parent->first, 764 fsopts->needswap); 765 root->inode->nlink++; /* count my parent's link */ 766 } else if (cur->child != NULL) 767 root->inode->nlink++; /* count my child's link */ 768 769 /* 770 * XXX possibly write file and long symlinks here, 771 * ensuring that blocks get written before inodes? 772 * otoh, this isn't a real filesystem, so who 773 * cares about ordering? :-) 774 */ 775 } 776 if (debug & DEBUG_FS_POPULATE_DIRBUF) 777 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap); 778 779 /* 780 * pass 2: write out dirbuf, then non-directories at this level 781 */ 782 if (debug & DEBUG_FS_POPULATE) 783 printf("ffs_populate_dir: PASS 2 dir %s\n", dir); 784 for (cur = root; cur != NULL; cur = cur->next) { 785 if (FSNODE_EXCLUDE_P(fsopts, cur)) 786 continue; 787 if (cur->inode->flags & FI_WRITTEN) 788 continue; /* skip hard-linked entries */ 789 cur->inode->flags |= FI_WRITTEN; 790 791 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 792 >= sizeof(path)) 793 errx(1, "Pathname too long."); 794 795 if (cur->child != NULL) 796 continue; /* child creates own inode */ 797 798 /* build on-disk inode */ 799 if (ffs_opts->version == 1) 800 membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur, 801 root, fsopts); 802 else 803 membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur, 804 root, fsopts); 805 806 if (debug & DEBUG_FS_POPULATE_NODE) { 807 printf("ffs_populate_dir: writing ino %d, %s", 808 cur->inode->ino, inode_type(cur->type)); 809 if (cur->inode->nlink > 1) 810 printf(", nlink %d", cur->inode->nlink); 811 putchar('\n'); 812 } 813 814 if (membuf != NULL) { 815 ffs_write_file(&din, cur->inode->ino, membuf, fsopts); 816 } else if (S_ISREG(cur->type)) { 817 ffs_write_file(&din, cur->inode->ino, path, fsopts); 818 } else { 819 assert (! S_ISDIR(cur->type)); 820 ffs_write_inode(&din, cur->inode->ino, fsopts); 821 } 822 } 823 824 /* 825 * pass 3: write out sub-directories 826 */ 827 if (debug & DEBUG_FS_POPULATE) 828 printf("ffs_populate_dir: PASS 3 dir %s\n", dir); 829 for (cur = root; cur != NULL; cur = cur->next) { 830 if (FSNODE_EXCLUDE_P(fsopts, cur)) 831 continue; 832 if (cur->child == NULL) 833 continue; 834 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 835 >= sizeof(path)) 836 errx(1, "Pathname too long."); 837 if (! ffs_populate_dir(path, cur->child, fsopts)) 838 return (0); 839 } 840 841 if (debug & DEBUG_FS_POPULATE) 842 printf("ffs_populate_dir: DONE dir %s\n", dir); 843 844 /* cleanup */ 845 if (dirbuf.buf != NULL) 846 free(dirbuf.buf); 847 return (1); 848 } 849 850 851 static void 852 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts) 853 { 854 int isfile, ffd; 855 char *fbuf, *p; 856 off_t bufleft, chunk, offset; 857 struct inode in; 858 struct buf * bp; 859 ffs_opt_t *ffs_opts = fsopts->fs_specific; 860 861 assert (din != NULL); 862 assert (buf != NULL); 863 assert (fsopts != NULL); 864 assert (ffs_opts != NULL); 865 866 isfile = S_ISREG(DIP(din, mode)); 867 fbuf = NULL; 868 ffd = -1; 869 p = NULL; 870 871 in.i_fs = (struct fs *)fsopts->superblock; 872 873 if (debug & DEBUG_FS_WRITE_FILE) { 874 printf( 875 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld", 876 ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT), 877 (long long)DIP(din, size)); 878 if (isfile) 879 printf(", file '%s'\n", (char *)buf); 880 else 881 printf(", buffer %p\n", buf); 882 } 883 884 in.i_number = ino; 885 in.i_size = DIP(din, size); 886 if (ffs_opts->version == 1) 887 memcpy(&in.i_din.ffs1_din, &din->ffs1_din, 888 sizeof(in.i_din.ffs1_din)); 889 else 890 memcpy(&in.i_din.ffs2_din, &din->ffs2_din, 891 sizeof(in.i_din.ffs2_din)); 892 in.i_fd = fsopts->fd; 893 894 if (DIP(din, size) == 0) 895 goto write_inode_and_leave; /* mmm, cheating */ 896 897 if (isfile) { 898 if ((fbuf = malloc(ffs_opts->bsize)) == NULL) 899 err(1, "Allocating memory for write buffer"); 900 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) { 901 warn("Can't open `%s' for reading", (char *)buf); 902 goto leave_ffs_write_file; 903 } 904 } else { 905 p = buf; 906 } 907 908 chunk = 0; 909 for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) { 910 chunk = MIN(bufleft, ffs_opts->bsize); 911 if (isfile) { 912 if (read(ffd, fbuf, chunk) != chunk) 913 err(1, "Reading `%s', %lld bytes to go", 914 (char *)buf, (long long)bufleft); 915 p = fbuf; 916 } 917 offset = DIP(din, size) - bufleft; 918 if (debug & DEBUG_FS_WRITE_FILE_BLOCK) 919 printf( 920 "ffs_write_file: write %p offset %lld size %lld left %lld\n", 921 p, (long long)offset, 922 (long long)chunk, (long long)bufleft); 923 /* 924 * XXX if holey support is desired, do the check here 925 * 926 * XXX might need to write out last bit in fragroundup 927 * sized chunk. however, ffs_balloc() handles this for us 928 */ 929 errno = ffs_balloc(&in, offset, chunk, &bp); 930 bad_ffs_write_file: 931 if (errno != 0) 932 err(1, 933 "Writing inode %d (%s), bytes %lld + %lld", 934 ino, 935 isfile ? (char *)buf : 936 inode_type(DIP(din, mode) & S_IFMT), 937 (long long)offset, (long long)chunk); 938 memcpy(bp->b_data, p, chunk); 939 errno = bwrite(bp); 940 if (errno != 0) 941 goto bad_ffs_write_file; 942 brelse(bp); 943 if (!isfile) 944 p += chunk; 945 } 946 947 write_inode_and_leave: 948 ffs_write_inode(&in.i_din, in.i_number, fsopts); 949 950 leave_ffs_write_file: 951 if (fbuf) 952 free(fbuf); 953 if (ffd != -1) 954 close(ffd); 955 } 956 957 958 static void 959 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap) 960 { 961 doff_t i; 962 struct direct *de; 963 uint16_t reclen; 964 965 assert (dbuf != NULL); 966 assert (dir != NULL); 967 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n", 968 dir, dbuf->size, dbuf->cur); 969 970 for (i = 0; i < dbuf->size; ) { 971 de = (struct direct *)(dbuf->buf + i); 972 reclen = ufs_rw16(de->d_reclen, needswap); 973 printf( 974 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n", 975 ufs_rw32(de->d_fileno, needswap), 976 inode_type(DTTOIF(de->d_type)), i, reclen, 977 de->d_namlen, de->d_name); 978 i += reclen; 979 assert(reclen > 0); 980 } 981 } 982 983 static void 984 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) 985 { 986 struct direct de, *dp; 987 uint16_t llen, reclen; 988 u_char *newbuf; 989 990 assert (dbuf != NULL); 991 assert (name != NULL); 992 assert (node != NULL); 993 /* create direct entry */ 994 (void)memset(&de, 0, sizeof(de)); 995 de.d_fileno = ufs_rw32(node->inode->ino, needswap); 996 de.d_type = IFTODT(node->type); 997 de.d_namlen = (uint8_t)strlen(name); 998 strcpy(de.d_name, name); 999 reclen = DIRSIZ(0, &de, needswap); 1000 de.d_reclen = ufs_rw16(reclen, needswap); 1001 1002 dp = (struct direct *)(dbuf->buf + dbuf->cur); 1003 llen = 0; 1004 if (dp != NULL) 1005 llen = DIRSIZ(0, dp, needswap); 1006 1007 if (debug & DEBUG_FS_MAKE_DIRBUF) 1008 printf( 1009 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n" 1010 " ino %d type %d reclen %d namlen %d name %.30s\n", 1011 dbuf->size, dbuf->cur, llen, 1012 ufs_rw32(de.d_fileno, needswap), de.d_type, reclen, 1013 de.d_namlen, de.d_name); 1014 1015 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { 1016 if (debug & DEBUG_FS_MAKE_DIRBUF) 1017 printf("ffs_make_dirbuf: growing buf to %d\n", 1018 dbuf->size + DIRBLKSIZ); 1019 if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL) 1020 err(1, "Allocating memory for directory buffer"); 1021 dbuf->buf = newbuf; 1022 dbuf->size += DIRBLKSIZ; 1023 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); 1024 dbuf->cur = dbuf->size - DIRBLKSIZ; 1025 } else if (dp) { /* shrink end of previous */ 1026 dp->d_reclen = ufs_rw16(llen,needswap); 1027 dbuf->cur += llen; 1028 } 1029 dp = (struct direct *)(dbuf->buf + dbuf->cur); 1030 memcpy(dp, &de, reclen); 1031 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap); 1032 } 1033 1034 /* 1035 * cribbed from sys/ufs/ffs/ffs_alloc.c 1036 */ 1037 static void 1038 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts) 1039 { 1040 char *buf; 1041 struct ufs1_dinode *dp1; 1042 struct ufs2_dinode *dp2, *dip; 1043 struct cg *cgp; 1044 struct fs *fs; 1045 int cg, cgino, i; 1046 daddr_t d; 1047 char sbbuf[FFS_MAXBSIZE]; 1048 int32_t initediblk; 1049 ffs_opt_t *ffs_opts = fsopts->fs_specific; 1050 1051 assert (dp != NULL); 1052 assert (ino > 0); 1053 assert (fsopts != NULL); 1054 assert (ffs_opts != NULL); 1055 1056 fs = (struct fs *)fsopts->superblock; 1057 cg = ino_to_cg(fs, ino); 1058 cgino = ino % fs->fs_ipg; 1059 if (debug & DEBUG_FS_WRITE_INODE) 1060 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n", 1061 dp, ino, cg, cgino); 1062 1063 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 1064 fsopts); 1065 cgp = (struct cg *)sbbuf; 1066 if (!cg_chkmagic(cgp, fsopts->needswap)) 1067 errx(1, "ffs_write_inode: cg %d: bad magic number", cg); 1068 1069 assert (isclr(cg_inosused(cgp, fsopts->needswap), cgino)); 1070 1071 buf = malloc(fs->fs_bsize); 1072 if (buf == NULL) 1073 errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg); 1074 1075 dp1 = (struct ufs1_dinode *)buf; 1076 dp2 = (struct ufs2_dinode *)buf; 1077 1078 if (fs->fs_cstotal.cs_nifree == 0) 1079 errx(1, "ffs_write_inode: fs out of inodes for ino %u", 1080 ino); 1081 if (fs->fs_cs(fs, cg).cs_nifree == 0) 1082 errx(1, 1083 "ffs_write_inode: cg %d out of inodes for ino %u", 1084 cg, ino); 1085 setbit(cg_inosused(cgp, fsopts->needswap), cgino); 1086 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); 1087 fs->fs_cstotal.cs_nifree--; 1088 fs->fs_cs(fs, cg).cs_nifree--; 1089 if (S_ISDIR(DIP(dp, mode))) { 1090 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); 1091 fs->fs_cstotal.cs_ndir++; 1092 fs->fs_cs(fs, cg).cs_ndir++; 1093 } 1094 1095 /* 1096 * Initialize inode blocks on the fly for UFS2. 1097 */ 1098 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap); 1099 if (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk && 1100 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) { 1101 memset(buf, 0, fs->fs_bsize); 1102 dip = (struct ufs2_dinode *)buf; 1103 srandom(time(NULL)); 1104 for (i = 0; i < INOPB(fs); i++) { 1105 dip->di_gen = random() / 2 + 1; 1106 dip++; 1107 } 1108 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs, 1109 cg * fs->fs_ipg + initediblk)), 1110 fs->fs_bsize, buf, fsopts); 1111 initediblk += INOPB(fs); 1112 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap); 1113 } 1114 1115 1116 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 1117 fsopts); 1118 1119 /* now write inode */ 1120 d = fsbtodb(fs, ino_to_fsba(fs, ino)); 1121 ffs_rdfs(d, fs->fs_bsize, buf, fsopts); 1122 if (fsopts->needswap) { 1123 if (ffs_opts->version == 1) 1124 ffs_dinode1_swap(&dp->ffs1_din, 1125 &dp1[ino_to_fsbo(fs, ino)]); 1126 else 1127 ffs_dinode2_swap(&dp->ffs2_din, 1128 &dp2[ino_to_fsbo(fs, ino)]); 1129 } else { 1130 if (ffs_opts->version == 1) 1131 dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din; 1132 else 1133 dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din; 1134 } 1135 ffs_wtfs(d, fs->fs_bsize, buf, fsopts); 1136 free(buf); 1137 } 1138 1139 void 1140 panic(const char *fmt, ...) 1141 { 1142 va_list ap; 1143 1144 va_start(ap, fmt); 1145 vwarnx(fmt, ap); 1146 va_end(ap); 1147 exit(1); 1148 } 1149