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