1 /* $NetBSD: ffs.c,v 1.3 2001/11/02 03:12:48 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.3 2001/11/02 03:12:48 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/ffs/fs.h> 93 #include <ufs/ufs/dir.h> 94 #include <ufs/ufs/dinode.h> 95 #include <ufs/ufs/inode.h> 96 #include <ufs/ufs/ufs_bswap.h> 97 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 == 0) 274 fsopts->sectorsize = DFL_SECSIZE; 275 if (fsopts->fsize == 0) 276 fsopts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize); 277 if (fsopts->bsize == 0) 278 fsopts->bsize = MIN(DFL_BLKSIZE, 8 * fsopts->fsize); 279 if (fsopts->cpg == 0) 280 fsopts->cpg = DFL_CYLSPERGROUP; 281 /* fsopts->density is set below */ 282 if (fsopts->ntracks == 0) 283 fsopts->ntracks = DFL_NTRACKS; 284 if (fsopts->nsectors == 0) 285 fsopts->nsectors = DFL_NSECTORS; 286 if (fsopts->rpm == 0) 287 fsopts->rpm = DFL_RPM; 288 if (fsopts->minfree == 0) 289 fsopts->minfree = MINFREE; 290 if (fsopts->optimization == 0) 291 fsopts->optimization = DEFAULTOPT; 292 if (fsopts->maxcontig == 0) 293 fsopts->maxcontig = 294 MAX(1, MIN(MAXPHYS, MAXBSIZE) / fsopts->bsize); 295 if (fsopts->rotdelay == 0) 296 fsopts->rotdelay = DFL_ROTDELAY; 297 if (fsopts->maxbpg == 0) 298 fsopts->maxbpg = fsopts->bsize / sizeof(ufs_daddr_t); 299 if (fsopts->nrpos == 0) 300 fsopts->nrpos = DFL_NRPOS; 301 if (fsopts->avgfilesize == 0) 302 fsopts->avgfilesize = AVFILESIZ; 303 if (fsopts->avgfpdir == 0) 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 sector */ 358 fsopts->size = roundup(fsopts->size, fsopts->sectorsize); 359 360 /* calculate density if necessary */ 361 if (fsopts->density == 0) 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->bsize); \ 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->dup == NULL) { /* don't count duplicate names */ 522 if (debug & DEBUG_FS_SIZE_DIR_NODE) 523 printf("ffs_size_dir: %s size %lld\n", 524 node->name, 525 (long long)node->statbuf.st_size); 526 fsopts->inodes++; 527 if (node->type == S_IFREG) 528 ADDSIZE(node->statbuf.st_size); 529 if (node->type == S_IFLNK) { 530 int slen; 531 532 slen = strlen(node->symlink) + 1; 533 if (slen >= MAXSYMLINKLEN) 534 ADDSIZE(slen); 535 } 536 } 537 if (node->type == S_IFDIR) 538 ffs_size_dir(node->child, fsopts); 539 } 540 ADDSIZE(curdirsize); 541 542 if (debug & DEBUG_FS_SIZE_DIR) 543 printf("ffs_size_dir: exit: size %lld inodes %lld\n", 544 (long long)fsopts->size, (long long)fsopts->inodes); 545 } 546 547 548 static int 549 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts) 550 { 551 fsnode *cur; 552 dirbuf_t dirbuf; 553 struct dinode din; 554 void *membuf; 555 char path[MAXPATHLEN + 1]; 556 557 assert(dir != NULL); 558 assert(root != NULL); 559 assert(fsopts != NULL); 560 561 (void)memset(&dirbuf, 0, sizeof(dirbuf)); 562 563 if (debug & DEBUG_FS_POPULATE) 564 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root); 565 566 /* 567 * pass 1: allocate inode numbers, build directory `file' 568 */ 569 for (cur = root; cur != NULL; cur = cur->next) { 570 if (cur->dup == NULL) { 571 if (cur == root && cur->parent) 572 cur->ino = cur->parent->ino; 573 else { 574 cur->ino = fsopts->curinode; 575 fsopts->curinode++; 576 } 577 } else 578 cur->ino = cur->dup->ino; 579 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap); 580 if (cur == root) { /* we're at "."; add ".." */ 581 ffs_make_dirbuf(&dirbuf, "..", 582 cur->parent == NULL ? cur : cur->parent->first, 583 fsopts->needswap); 584 root->nlink++; /* count my parent's link */ 585 } else if (cur->child != NULL) 586 root->nlink++; /* count my child's link */ 587 588 /* 589 * XXX possibly write file and long symlinks here, 590 * ensuring that blocks get written before inodes? 591 * otoh, this isn't a real filesystem, so who 592 * cares about ordering? :-) 593 */ 594 } 595 if (debug & DEBUG_FS_POPULATE_DIRBUF) 596 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap); 597 598 /* 599 * pass 2: write out dirbuf, then non-directories at this level 600 */ 601 if (debug & DEBUG_FS_POPULATE) 602 printf("ffs_populate_dir: PASS 2 dir %s\n", dir); 603 for (cur = root; cur != NULL; cur = cur->next) { 604 if (cur->dup != NULL) 605 continue; /* skip hard-linked entries */ 606 607 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 608 >= sizeof(path)) 609 errx(1, "Pathname too long."); 610 611 if (cur->child != NULL) 612 continue; /* child creates own inode */ 613 614 /* build on-disk inode */ 615 memset(&din, 0, sizeof(din)); 616 din.di_mode = cur->statbuf.st_mode; 617 din.di_nlink = cur->nlink; 618 din.di_size = cur->statbuf.st_size; 619 din.di_atime = cur->statbuf.st_atime; 620 din.di_atimensec = cur->statbuf.st_atimensec; 621 din.di_mtime = cur->statbuf.st_mtime; 622 din.di_mtimensec = cur->statbuf.st_mtimensec; 623 din.di_ctime = cur->statbuf.st_ctime; 624 din.di_ctimensec = cur->statbuf.st_ctimensec; 625 din.di_flags = cur->statbuf.st_flags; 626 din.di_gen = cur->statbuf.st_gen; 627 din.di_uid = cur->statbuf.st_uid; 628 din.di_gid = cur->statbuf.st_gid; 629 /* not set: di_db, di_ib, di_blocks, di_spare */ 630 631 membuf = NULL; 632 if (cur == root) { /* "."; write dirbuf */ 633 membuf = dirbuf.buf; 634 din.di_size = dirbuf.size; 635 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { 636 din.di_size = 0; /* a device */ 637 din.di_rdev = 638 ufs_rw32(cur->statbuf.st_rdev, fsopts->needswap); 639 } else if (S_ISLNK(cur->type)) { /* symlink */ 640 int slen; 641 642 slen = strlen(cur->symlink); 643 if (slen < MAXSYMLINKLEN) { /* short link */ 644 memcpy(din.di_shortlink, cur->symlink, slen); 645 } else 646 membuf = cur->symlink; 647 din.di_size = slen; 648 } 649 650 if (debug & DEBUG_FS_POPULATE_NODE) { 651 printf("ffs_populate_dir: writing ino %d, %s", 652 cur->ino, inode_type(cur->type)); 653 if (cur->nlink > 1) 654 printf(", nlink %d", cur->nlink); 655 putchar('\n'); 656 } 657 658 if (membuf != NULL) { 659 ffs_write_file(&din, cur->ino, membuf, fsopts); 660 } else if (S_ISREG(cur->type)) { 661 ffs_write_file(&din, cur->ino, path, fsopts); 662 } else { 663 assert (! S_ISDIR(cur->type)); 664 ffs_write_inode(&din, cur->ino, fsopts); 665 } 666 } 667 668 /* 669 * pass 3: write out sub-directories 670 */ 671 if (debug & DEBUG_FS_POPULATE) 672 printf("ffs_populate_dir: PASS 3 dir %s\n", dir); 673 for (cur = root; cur != NULL; cur = cur->next) { 674 if (cur->child == NULL) 675 continue; 676 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 677 >= sizeof(path)) 678 errx(1, "Pathname too long."); 679 if (! ffs_populate_dir(path, cur->child, fsopts)) 680 return (0); 681 } 682 683 if (debug & DEBUG_FS_POPULATE) 684 printf("ffs_populate_dir: DONE dir %s\n", dir); 685 686 /* cleanup */ 687 if (dirbuf.buf != NULL) 688 free(dirbuf.buf); 689 return (1); 690 } 691 692 693 static void 694 ffs_write_file(struct dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts) 695 { 696 int isfile, ffd; 697 char *fbuf, *p; 698 off_t bufleft, chunk, offset; 699 struct inode in; 700 struct buf * bp; 701 702 assert (din != NULL); 703 assert (buf != NULL); 704 assert (fsopts != NULL); 705 706 isfile = S_ISREG(din->di_mode); 707 fbuf = NULL; 708 ffd = -1; 709 710 if (debug & DEBUG_FS_WRITE_FILE) { 711 printf( 712 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld", 713 ino, din, isfile, inode_type(din->di_mode & S_IFMT), 714 (long long)din->di_size); 715 if (isfile) 716 printf(", file '%s'\n", (char *)buf); 717 else 718 printf(", buffer %p\n", buf); 719 } 720 721 in.i_number = ino; 722 in.i_fs = (struct fs *)fsopts->superblock; 723 memcpy(&in.i_din.ffs_din, din, sizeof(in.i_din.ffs_din)); 724 in.i_fd = fsopts->fd; 725 726 if (din->di_size == 0) 727 goto write_inode_and_leave; /* mmm, cheating */ 728 729 if (isfile) { 730 if ((fbuf = malloc(fsopts->bsize)) == NULL) 731 err(1, "Allocating memory for write buffer"); 732 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) { 733 warn("Can't open `%s' for reading", (char *)buf); 734 goto leave_ffs_write_file; 735 } 736 } else { 737 p = buf; 738 } 739 740 chunk = 0; 741 for (bufleft = din->di_size; bufleft > 0; bufleft -= chunk) { 742 chunk = MIN(bufleft, fsopts->bsize); 743 if (isfile) { 744 if (read(ffd, fbuf, chunk) != chunk) 745 err(1, "reading %s, %lld bytes to go", 746 (char *)buf, (long long)bufleft); 747 p = fbuf; 748 } 749 offset = din->di_size - bufleft; 750 if (debug & DEBUG_FS_WRITE_FILE_BLOCK) 751 printf( 752 "ffs_write_file: write %p offset %lld size %lld left %lld\n", 753 p, (long long)offset, 754 (long long)chunk, (long long)bufleft); 755 /* 756 * XXX if holey support is desired, do the check here 757 * 758 * XXX might need to write out last bit in fragroundup 759 * sized chunk. however, ffs_balloc() handles this for us 760 */ 761 errno = ffs_balloc(&in, offset, chunk, &bp); 762 if (errno != 0) 763 err(1, 764 "ffs_write_file: ffs_balloc %lld %lld", 765 (long long)offset, (long long)chunk); 766 memcpy(bp->b_data, p, chunk); 767 errno = bwrite(bp); 768 if (errno != 0) 769 err(1, "ffs_write_file: bwrite"); 770 brelse(bp); 771 if (!isfile) 772 p += chunk; 773 } 774 775 write_inode_and_leave: 776 ffs_write_inode(&in.i_din.ffs_din, in.i_number, fsopts); 777 778 leave_ffs_write_file: 779 if (fbuf) 780 free(fbuf); 781 if (ffd != -1) 782 close(ffd); 783 } 784 785 786 static void 787 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap) 788 { 789 doff_t i; 790 struct direct *de; 791 u_int16_t reclen; 792 793 assert (dbuf != NULL); 794 assert (dir != NULL); 795 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n", 796 dir, dbuf->size, dbuf->cur); 797 798 for (i = 0; i < dbuf->size; ) { 799 de = (struct direct *)(dbuf->buf + i); 800 reclen = ufs_rw16(de->d_reclen, needswap); 801 printf( 802 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n", 803 ufs_rw32(de->d_ino, needswap), 804 inode_type(DTTOIF(de->d_type)), i, reclen, 805 de->d_namlen, de->d_name); 806 i += reclen; 807 assert(reclen > 0); 808 } 809 } 810 811 static void 812 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) 813 { 814 struct direct de, *dp; 815 u_int16_t llen, reclen; 816 817 assert (dbuf != NULL); 818 assert (name != NULL); 819 assert (node != NULL); 820 /* create direct entry */ 821 (void)memset(&de, 0, sizeof(de)); 822 de.d_ino = ufs_rw32(node->ino, needswap); 823 de.d_type = IFTODT(node->type); 824 de.d_namlen = (u_int8_t)strlen(name); 825 assert (de.d_namlen < (sizeof(de.d_name))); 826 strcpy(de.d_name, name); 827 reclen = DIRSIZ(0, &de, needswap); 828 de.d_reclen = ufs_rw16(reclen, needswap); 829 830 dp = (struct direct *)(dbuf->buf + dbuf->cur); 831 llen = 0; 832 if (dp != NULL) 833 llen = DIRSIZ(0, dp, needswap); 834 835 if (debug & DEBUG_FS_MAKE_DIRBUF) 836 printf( 837 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n" 838 " ino %d type %d reclen %d namlen %d name %.30s\n", 839 dbuf->size, dbuf->cur, llen, 840 ufs_rw32(de.d_ino, needswap), de.d_type, reclen, 841 de.d_namlen, de.d_name); 842 843 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { 844 dbuf->size += DIRBLKSIZ; /* need another chunk */ 845 if (debug & DEBUG_FS_MAKE_DIRBUF) 846 printf("ffs_make_dirbuf: growing buf to %d\n", 847 dbuf->size); 848 if ((dbuf->buf = realloc(dbuf->buf, dbuf->size)) == NULL) 849 err(1, "Allocating memory for directory buffer"); 850 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); 851 dbuf->cur = dbuf->size - DIRBLKSIZ; 852 } else { /* shrink end of previous */ 853 dp->d_reclen = ufs_rw16(llen,needswap); 854 dbuf->cur += llen; 855 } 856 dp = (struct direct *)(dbuf->buf + dbuf->cur); 857 memcpy(dp, &de, reclen); 858 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap); 859 } 860 861 /* 862 * cribbed from sys/ufs/ffs/ffs_alloc.c 863 */ 864 static void 865 ffs_write_inode(struct dinode *ip, uint32_t ino, const fsinfo_t *fsopts) 866 { 867 struct dinode ibuf[MAXINOPB]; 868 struct cg *cgp; 869 struct fs *fs; 870 int cg, cgino; 871 daddr_t d; 872 char sbbuf[MAXBSIZE]; 873 874 assert (ip != NULL); 875 assert (ino > 0); 876 assert (fsopts != NULL); 877 878 fs = (struct fs *)fsopts->superblock; 879 cg = ino_to_cg(fs, ino); 880 cgino = ino % fs->fs_ipg; 881 if (debug & DEBUG_FS_WRITE_INODE) 882 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n", 883 ip, ino, cg, cgino); 884 885 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 886 fsopts); 887 cgp = (struct cg *)sbbuf; 888 if (!cg_chkmagic(cgp, fsopts->needswap)) 889 errx(1, "ffs_write_inode: cg %d: bad magic number", cg); 890 891 assert (isclr(cg_inosused(cgp, fsopts->needswap), cgino)); 892 893 if (fs->fs_cstotal.cs_nifree == 0) 894 errx(1, "ffs_write_inode: fs out of inodes for ino %u", 895 ino); 896 if (fs->fs_cs(fs, cg).cs_nifree == 0) 897 errx(1, 898 "ffs_write_inode: cg %d out of inodes for ino %u", 899 cg, ino); 900 setbit(cg_inosused(cgp, fsopts->needswap), cgino); 901 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); 902 fs->fs_cstotal.cs_nifree--; 903 fs->fs_cs(fs, cg).cs_nifree--; 904 if (S_ISDIR(ip->di_mode)) { 905 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); 906 fs->fs_cstotal.cs_ndir++; 907 fs->fs_cs(fs, cg).cs_ndir++; 908 } 909 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 910 fsopts); 911 912 /* now write inode */ 913 d = fsbtodb(fs, ino_to_fsba(fs, ino)); 914 ffs_rdfs(d, fs->fs_bsize, ibuf, fsopts); 915 if (fsopts->needswap) 916 ffs_dinode_swap(ip, &ibuf[ino_to_fsbo(fs, ino)]); 917 else 918 ibuf[ino_to_fsbo(fs, ino)] = *ip; 919 ffs_wtfs(d, fs->fs_bsize, ibuf, fsopts); 920 } 921 922 void 923 panic(const char *fmt, ...) 924 { 925 va_list ap; 926 927 va_start(ap, fmt); 928 vwarnx(fmt, ap); 929 va_end(ap); 930 exit(1); 931 } 932