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