1 /* $NetBSD: ffs.c,v 1.14 2002/02/15 04:04:57 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 #if defined(__RCSID) && !defined(__lint) 74 __RCSID("$NetBSD: ffs.c,v 1.14 2002/02/15 04:04:57 lukem 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 */ 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 256 ((struct fs *)fsopts->superblock)->fs_fmod = 0; 257 ffs_write_superblock(fsopts->superblock, fsopts); 258 if (close(fsopts->fd) == -1) 259 err(1, "Closing `%s'", image); 260 fsopts->fd = -1; 261 printf("Image `%s' complete\n", image); 262 } 263 264 /* end of public functions */ 265 266 267 static void 268 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts) 269 { 270 int32_t ncg = 1; 271 #if notyet 272 int32_t spc, nspf, ncyl, fssize; 273 #endif 274 275 assert(dir != NULL); 276 assert(root != NULL); 277 assert(fsopts != NULL); 278 279 if (debug & DEBUG_FS_VALIDATE) { 280 printf("ffs_validate: before defaults set:\n"); 281 ffs_dump_fsinfo(fsopts); 282 } 283 284 /* set FFS defaults */ 285 if (fsopts->sectorsize == -1) 286 fsopts->sectorsize = DFL_SECSIZE; 287 if (fsopts->fsize == -1) 288 fsopts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize); 289 if (fsopts->bsize == -1) 290 fsopts->bsize = MIN(DFL_BLKSIZE, 8 * fsopts->fsize); 291 if (fsopts->cpg == -1) 292 fsopts->cpg = DFL_CYLSPERGROUP; 293 else 294 fsopts->cpgflg = 1; 295 /* fsopts->density is set below */ 296 if (fsopts->ntracks == -1) 297 fsopts->ntracks = DFL_NTRACKS; 298 if (fsopts->nsectors == -1) 299 fsopts->nsectors = DFL_NSECTORS; 300 if (fsopts->rpm == -1) 301 fsopts->rpm = DFL_RPM; 302 if (fsopts->minfree == -1) 303 fsopts->minfree = MINFREE; 304 if (fsopts->optimization == -1) 305 fsopts->optimization = DEFAULTOPT; 306 if (fsopts->maxcontig == -1) 307 fsopts->maxcontig = 308 MAX(1, MIN(MAXPHYS, MAXBSIZE) / fsopts->bsize); 309 if (fsopts->rotdelay == -1) 310 fsopts->rotdelay = DFL_ROTDELAY; 311 if (fsopts->maxbpg == -1) 312 fsopts->maxbpg = fsopts->bsize / sizeof(ufs_daddr_t); 313 if (fsopts->nrpos == -1) 314 fsopts->nrpos = DFL_NRPOS; 315 if (fsopts->avgfilesize == -1) 316 fsopts->avgfilesize = AVFILESIZ; 317 if (fsopts->avgfpdir == -1) 318 fsopts->avgfpdir = AFPDIR; 319 320 /* calculate size of tree */ 321 ffs_size_dir(root, fsopts); 322 fsopts->inodes += ROOTINO; /* include first two inodes */ 323 324 if (debug & DEBUG_FS_VALIDATE) 325 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n", 326 (long long)fsopts->size, (long long)fsopts->inodes); 327 328 /* add requested slop */ 329 fsopts->size += fsopts->freeblocks; 330 fsopts->inodes += fsopts->freefiles; 331 if (fsopts->freefilepc > 0) 332 fsopts->inodes = 333 fsopts->inodes * (100 + fsopts->freefilepc) / 100; 334 if (fsopts->freeblockpc > 0) 335 fsopts->size = 336 fsopts->size * (100 + fsopts->freeblockpc) / 100; 337 338 #if notyet 339 /* 340 * estimate number of cylinder groups 341 */ 342 spc = fsopts->nsectors * fsopts->ntracks; 343 nspf = fsopts->fsize / fsopts->sectorsize; 344 fssize = fsopts->size / fsopts->sectorsize; 345 ncyl = fssize * nspf / spc; 346 if (fssize * nspf > ncyl * spc) 347 ncyl++; 348 ncg = ncyl / fsopts->cpg; 349 if (ncg == 0) 350 ncg = 1; 351 if (debug & DEBUG_FS_VALIDATE) 352 printf( 353 "ffs_validate: spc %d nspf %d fssize %d ncyl %d ncg %d\n", 354 spc, nspf, fssize, ncyl, ncg); 355 #endif /* notyet */ 356 357 /* add space needed for superblocks */ 358 fsopts->size += (SBOFF + SBSIZE) * ncg; 359 /* add space needed to store inodes, x3 for blockmaps, etc */ 360 fsopts->size += ncg * DINODE_SIZE * 3 * 361 roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE_SIZE); 362 363 /* add minfree */ 364 if (fsopts->minfree > 0) 365 fsopts->size = 366 fsopts->size * (100 + fsopts->minfree) / 100; 367 /* 368 * XXX any other fs slop to add, such as csum's, etc ?? 369 */ 370 371 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */ 372 fsopts->size = fsopts->minsize; 373 374 /* round up to the next block */ 375 fsopts->size = roundup(fsopts->size, fsopts->bsize); 376 377 /* calculate density if necessary */ 378 if (fsopts->density == -1) 379 fsopts->density = fsopts->size / fsopts->inodes + 1; 380 381 if (debug & DEBUG_FS_VALIDATE) { 382 printf("ffs_validate: after defaults set:\n"); 383 ffs_dump_fsinfo(fsopts); 384 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n", 385 dir, (long long)fsopts->size, (long long)fsopts->inodes); 386 } 387 sectorsize = fsopts->sectorsize; /* XXX - see earlier */ 388 389 /* now check calculated sizes vs requested sizes */ 390 if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) { 391 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.", 392 dir, (long long)fsopts->size, (long long)fsopts->maxsize); 393 } 394 } 395 396 397 static void 398 ffs_dump_fsinfo(fsinfo_t *f) 399 { 400 401 printf("fsopts at %p\n", f); 402 403 printf("\tsize %lld, inodes %lld, curinode %u\n", 404 (long long)f->size, (long long)f->inodes, f->curinode); 405 406 printf("\tminsize %lld, maxsize %lld\n", 407 (long long)f->minsize, (long long)f->maxsize); 408 printf("\tfree files %lld, freefile %% %d\n", 409 (long long)f->freefiles, f->freefilepc); 410 printf("\tfree blocks %lld, freeblock %% %d\n", 411 (long long)f->freeblocks, f->freeblockpc); 412 printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize); 413 414 printf("\tbsize %d, fsize %d, cpg %d, density %d\n", 415 f->bsize, f->fsize, f->cpg, f->density); 416 printf("\tntracks %d, nsectors %d, rpm %d, minfree %d\n", 417 f->ntracks, f->nsectors, f->rpm, f->minfree); 418 printf("\tmaxcontig %d, rotdelay %d, maxbpg %d, nrpos %d\n", 419 f->maxcontig, f->rotdelay, f->maxbpg, f->nrpos); 420 printf("\toptimization %s\n", 421 f->optimization == FS_OPTSPACE ? "space" : "time"); 422 } 423 424 425 static int 426 ffs_create_image(const char *image, fsinfo_t *fsopts) 427 { 428 #if HAVE_STRUCT_STATFS_F_IOSIZE 429 struct statfs sfs; 430 #endif 431 struct fs *fs; 432 char *buf; 433 int i, bufsize; 434 off_t bufrem; 435 436 assert (image != NULL); 437 assert (fsopts != NULL); 438 439 /* create image */ 440 if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0777)) 441 == -1) { 442 warn("Can't open `%s' for writing", image); 443 return (-1); 444 } 445 446 /* zero image */ 447 #if HAVE_STRUCT_STATFS_F_IOSIZE 448 if (fstatfs(fsopts->fd, &sfs) == -1) { 449 #endif 450 bufsize = 8192; 451 #if HAVE_STRUCT_STATFS_F_IOSIZE 452 warn("can't fstatfs `%s', using default %d byte chunk", 453 image, bufsize); 454 } else 455 bufsize = sfs.f_iosize; 456 #endif 457 bufrem = fsopts->size; 458 if (debug & DEBUG_FS_CREATE_IMAGE) 459 printf( 460 "zero-ing image `%s', %lld sectors, using %d byte chunks\n", 461 image, (long long)bufrem, bufsize); 462 if ((buf = calloc(1, bufsize)) == NULL) { 463 warn("Can't create buffer for sector"); 464 return (-1); 465 } 466 while (bufrem > 0) { 467 i = write(fsopts->fd, buf, MIN(bufsize, bufrem)); 468 if (i == -1) { 469 warn("zeroing image, %lld bytes to go", 470 (long long)bufrem); 471 return (-1); 472 } 473 bufrem -= i; 474 } 475 476 /* make the file system */ 477 if (debug & DEBUG_FS_CREATE_IMAGE) 478 printf("calling mkfs(\"%s\", ...)\n", image); 479 fs = ffs_mkfs(image, fsopts); 480 fsopts->superblock = (void *)fs; 481 if (debug & DEBUG_FS_CREATE_IMAGE) { 482 time_t t; 483 484 t = ((struct fs *)fsopts->superblock)->fs_time; 485 printf("mkfs returned %p; fs_time %s", 486 fsopts->superblock, ctime(&t)); 487 printf("fs totals: nbfree %d, nffree %d, nifree %d, ndir %d\n", 488 fs->fs_cstotal.cs_nbfree, fs->fs_cstotal.cs_nffree, 489 fs->fs_cstotal.cs_nifree, fs->fs_cstotal.cs_ndir); 490 } 491 492 if (fs->fs_cstotal.cs_nifree < fsopts->inodes) { 493 warnx( 494 "Image file `%s' has %lld free inodes; %lld are required.", 495 image, 496 (long long)fs->fs_cstotal.cs_nifree, 497 (long long)fsopts->inodes); 498 return (-1); 499 } 500 return (fsopts->fd); 501 } 502 503 504 static void 505 ffs_size_dir(fsnode *root, fsinfo_t *fsopts) 506 { 507 struct direct tmpdir; 508 fsnode * node; 509 int curdirsize, this; 510 511 /* node may be NULL (empty directory) */ 512 assert(fsopts != NULL); 513 514 if (debug & DEBUG_FS_SIZE_DIR) 515 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n", 516 (long long)fsopts->size, (long long)fsopts->inodes); 517 518 #define ADDDIRENT(e) do { \ 519 tmpdir.d_namlen = strlen((e)); \ 520 this = DIRSIZ(0, &tmpdir, 0); \ 521 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 522 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \ 523 e, tmpdir.d_namlen, this, curdirsize); \ 524 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \ 525 curdirsize = roundup(curdirsize, DIRBLKSIZ); \ 526 curdirsize += this; \ 527 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \ 528 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \ 529 e, tmpdir.d_namlen, this, curdirsize); \ 530 } while (0); 531 532 /* 533 * XXX this needs to take into account extra space consumed 534 * by indirect blocks, etc. 535 */ 536 #define ADDSIZE(x) do { \ 537 fsopts->size += roundup((x), fsopts->fsize); \ 538 } while (0); 539 540 curdirsize = 0; 541 for (node = root; node != NULL; node = node->next) { 542 ADDDIRENT(node->name); 543 if (node == root) { /* we're at "." */ 544 assert(strcmp(node->name, ".") == 0); 545 ADDDIRENT(".."); 546 } else if ((node->inode->flags & FI_SIZED) == 0) { 547 /* don't count duplicate names */ 548 node->inode->flags |= FI_SIZED; 549 if (debug & DEBUG_FS_SIZE_DIR_NODE) 550 printf("ffs_size_dir: `%s' size %lld\n", 551 node->name, 552 (long long)node->inode->st.st_size); 553 fsopts->inodes++; 554 if (node->type == S_IFREG) 555 ADDSIZE(node->inode->st.st_size); 556 if (node->type == S_IFLNK) { 557 int slen; 558 559 slen = strlen(node->symlink) + 1; 560 if (slen >= MAXSYMLINKLEN) 561 ADDSIZE(slen); 562 } 563 } 564 if (node->type == S_IFDIR) 565 ffs_size_dir(node->child, fsopts); 566 } 567 ADDSIZE(curdirsize); 568 569 if (debug & DEBUG_FS_SIZE_DIR) 570 printf("ffs_size_dir: exit: size %lld inodes %lld\n", 571 (long long)fsopts->size, (long long)fsopts->inodes); 572 } 573 574 575 static int 576 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts) 577 { 578 fsnode *cur; 579 dirbuf_t dirbuf; 580 struct dinode din; 581 void *membuf; 582 char path[MAXPATHLEN + 1]; 583 584 assert(dir != NULL); 585 assert(root != NULL); 586 assert(fsopts != NULL); 587 588 (void)memset(&dirbuf, 0, sizeof(dirbuf)); 589 590 if (debug & DEBUG_FS_POPULATE) 591 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root); 592 593 /* 594 * pass 1: allocate inode numbers, build directory `file' 595 */ 596 for (cur = root; cur != NULL; cur = cur->next) { 597 if ((cur->inode->flags & FI_ALLOCATED) == 0) { 598 cur->inode->flags |= FI_ALLOCATED; 599 if (cur == root && cur->parent != NULL) 600 cur->inode->ino = cur->parent->inode->ino; 601 else { 602 cur->inode->ino = fsopts->curinode; 603 fsopts->curinode++; 604 } 605 } 606 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap); 607 if (cur == root) { /* we're at "."; add ".." */ 608 ffs_make_dirbuf(&dirbuf, "..", 609 cur->parent == NULL ? cur : cur->parent->first, 610 fsopts->needswap); 611 root->inode->nlink++; /* count my parent's link */ 612 } else if (cur->child != NULL) 613 root->inode->nlink++; /* count my child's link */ 614 615 /* 616 * XXX possibly write file and long symlinks here, 617 * ensuring that blocks get written before inodes? 618 * otoh, this isn't a real filesystem, so who 619 * cares about ordering? :-) 620 */ 621 } 622 if (debug & DEBUG_FS_POPULATE_DIRBUF) 623 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap); 624 625 /* 626 * pass 2: write out dirbuf, then non-directories at this level 627 */ 628 if (debug & DEBUG_FS_POPULATE) 629 printf("ffs_populate_dir: PASS 2 dir %s\n", dir); 630 for (cur = root; cur != NULL; cur = cur->next) { 631 if (cur->inode->flags & FI_WRITTEN) 632 continue; /* skip hard-linked entries */ 633 cur->inode->flags |= FI_WRITTEN; 634 635 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 636 >= sizeof(path)) 637 errx(1, "Pathname too long."); 638 639 if (cur->child != NULL) 640 continue; /* child creates own inode */ 641 642 /* build on-disk inode */ 643 memset(&din, 0, sizeof(din)); 644 din.di_mode = cur->inode->st.st_mode; 645 din.di_nlink = cur->inode->nlink; 646 din.di_size = cur->inode->st.st_size; 647 din.di_atime = cur->inode->st.st_atime; 648 din.di_mtime = cur->inode->st.st_mtime; 649 din.di_ctime = cur->inode->st.st_ctime; 650 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 651 din.di_atimensec = cur->inode->st.st_atimensec; 652 din.di_mtimensec = cur->inode->st.st_mtimensec; 653 din.di_ctimensec = cur->inode->st.st_ctimensec; 654 #endif 655 #if HAVE_STRUCT_STAT_ST_FLAGS 656 din.di_flags = cur->inode->st.st_flags; 657 #endif 658 #if HAVE_STRUCT_STAT_ST_GEN 659 din.di_gen = cur->inode->st.st_gen; 660 #endif 661 din.di_uid = cur->inode->st.st_uid; 662 din.di_gid = cur->inode->st.st_gid; 663 /* not set: di_db, di_ib, di_blocks, di_spare */ 664 665 membuf = NULL; 666 if (cur == root) { /* "."; write dirbuf */ 667 membuf = dirbuf.buf; 668 din.di_size = dirbuf.size; 669 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) { 670 din.di_size = 0; /* a device */ 671 din.di_rdev = 672 ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap); 673 } else if (S_ISLNK(cur->type)) { /* symlink */ 674 int slen; 675 676 slen = strlen(cur->symlink); 677 if (slen < MAXSYMLINKLEN) { /* short link */ 678 memcpy(din.di_shortlink, cur->symlink, slen); 679 } else 680 membuf = cur->symlink; 681 din.di_size = slen; 682 } 683 684 if (debug & DEBUG_FS_POPULATE_NODE) { 685 printf("ffs_populate_dir: writing ino %d, %s", 686 cur->inode->ino, inode_type(cur->type)); 687 if (cur->inode->nlink > 1) 688 printf(", nlink %d", cur->inode->nlink); 689 putchar('\n'); 690 } 691 692 if (membuf != NULL) { 693 ffs_write_file(&din, cur->inode->ino, membuf, fsopts); 694 } else if (S_ISREG(cur->type)) { 695 ffs_write_file(&din, cur->inode->ino, path, fsopts); 696 } else { 697 assert (! S_ISDIR(cur->type)); 698 ffs_write_inode(&din, cur->inode->ino, fsopts); 699 } 700 } 701 702 /* 703 * pass 3: write out sub-directories 704 */ 705 if (debug & DEBUG_FS_POPULATE) 706 printf("ffs_populate_dir: PASS 3 dir %s\n", dir); 707 for (cur = root; cur != NULL; cur = cur->next) { 708 if (cur->child == NULL) 709 continue; 710 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 711 >= sizeof(path)) 712 errx(1, "Pathname too long."); 713 if (! ffs_populate_dir(path, cur->child, fsopts)) 714 return (0); 715 } 716 717 if (debug & DEBUG_FS_POPULATE) 718 printf("ffs_populate_dir: DONE dir %s\n", dir); 719 720 /* cleanup */ 721 if (dirbuf.buf != NULL) 722 free(dirbuf.buf); 723 return (1); 724 } 725 726 727 static void 728 ffs_write_file(struct dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts) 729 { 730 int isfile, ffd; 731 char *fbuf, *p; 732 off_t bufleft, chunk, offset; 733 struct inode in; 734 struct buf * bp; 735 736 assert (din != NULL); 737 assert (buf != NULL); 738 assert (fsopts != NULL); 739 740 isfile = S_ISREG(din->di_mode); 741 fbuf = NULL; 742 ffd = -1; 743 744 if (debug & DEBUG_FS_WRITE_FILE) { 745 printf( 746 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld", 747 ino, din, isfile, inode_type(din->di_mode & S_IFMT), 748 (long long)din->di_size); 749 if (isfile) 750 printf(", file '%s'\n", (char *)buf); 751 else 752 printf(", buffer %p\n", buf); 753 } 754 755 in.i_number = ino; 756 in.i_fs = (struct fs *)fsopts->superblock; 757 memcpy(&in.i_din.ffs_din, din, sizeof(in.i_din.ffs_din)); 758 in.i_fd = fsopts->fd; 759 760 if (din->di_size == 0) 761 goto write_inode_and_leave; /* mmm, cheating */ 762 763 if (isfile) { 764 if ((fbuf = malloc(fsopts->bsize)) == NULL) 765 err(1, "Allocating memory for write buffer"); 766 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) { 767 warn("Can't open `%s' for reading", (char *)buf); 768 goto leave_ffs_write_file; 769 } 770 } else { 771 p = buf; 772 } 773 774 chunk = 0; 775 for (bufleft = din->di_size; bufleft > 0; bufleft -= chunk) { 776 chunk = MIN(bufleft, fsopts->bsize); 777 if (isfile) { 778 if (read(ffd, fbuf, chunk) != chunk) 779 err(1, "Reading `%s', %lld bytes to go", 780 (char *)buf, (long long)bufleft); 781 p = fbuf; 782 } 783 offset = din->di_size - bufleft; 784 if (debug & DEBUG_FS_WRITE_FILE_BLOCK) 785 printf( 786 "ffs_write_file: write %p offset %lld size %lld left %lld\n", 787 p, (long long)offset, 788 (long long)chunk, (long long)bufleft); 789 /* 790 * XXX if holey support is desired, do the check here 791 * 792 * XXX might need to write out last bit in fragroundup 793 * sized chunk. however, ffs_balloc() handles this for us 794 */ 795 errno = ffs_balloc(&in, offset, chunk, &bp); 796 bad_ffs_write_file: 797 if (errno != 0) 798 err(1, 799 "Writing inode %d (%s), bytes %lld + %lld", 800 ino, 801 isfile ? (char *)buf : 802 inode_type(din->di_mode & S_IFMT), 803 (long long)offset, (long long)chunk); 804 memcpy(bp->b_data, p, chunk); 805 errno = bwrite(bp); 806 if (errno != 0) 807 goto bad_ffs_write_file; 808 brelse(bp); 809 if (!isfile) 810 p += chunk; 811 } 812 813 write_inode_and_leave: 814 ffs_write_inode(&in.i_din.ffs_din, in.i_number, fsopts); 815 816 leave_ffs_write_file: 817 if (fbuf) 818 free(fbuf); 819 if (ffd != -1) 820 close(ffd); 821 } 822 823 824 static void 825 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap) 826 { 827 doff_t i; 828 struct direct *de; 829 uint16_t reclen; 830 831 assert (dbuf != NULL); 832 assert (dir != NULL); 833 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n", 834 dir, dbuf->size, dbuf->cur); 835 836 for (i = 0; i < dbuf->size; ) { 837 de = (struct direct *)(dbuf->buf + i); 838 reclen = ufs_rw16(de->d_reclen, needswap); 839 printf( 840 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n", 841 ufs_rw32(de->d_ino, needswap), 842 inode_type(DTTOIF(de->d_type)), i, reclen, 843 de->d_namlen, de->d_name); 844 i += reclen; 845 assert(reclen > 0); 846 } 847 } 848 849 static void 850 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap) 851 { 852 struct direct de, *dp; 853 uint16_t llen, reclen; 854 855 assert (dbuf != NULL); 856 assert (name != NULL); 857 assert (node != NULL); 858 /* create direct entry */ 859 (void)memset(&de, 0, sizeof(de)); 860 de.d_ino = ufs_rw32(node->inode->ino, needswap); 861 de.d_type = IFTODT(node->type); 862 de.d_namlen = (uint8_t)strlen(name); 863 assert (de.d_namlen < (sizeof(de.d_name))); 864 strcpy(de.d_name, name); 865 reclen = DIRSIZ(0, &de, needswap); 866 de.d_reclen = ufs_rw16(reclen, needswap); 867 868 dp = (struct direct *)(dbuf->buf + dbuf->cur); 869 llen = 0; 870 if (dp != NULL) 871 llen = DIRSIZ(0, dp, needswap); 872 873 if (debug & DEBUG_FS_MAKE_DIRBUF) 874 printf( 875 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n" 876 " ino %d type %d reclen %d namlen %d name %.30s\n", 877 dbuf->size, dbuf->cur, llen, 878 ufs_rw32(de.d_ino, needswap), de.d_type, reclen, 879 de.d_namlen, de.d_name); 880 881 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) { 882 dbuf->size += DIRBLKSIZ; /* need another chunk */ 883 if (debug & DEBUG_FS_MAKE_DIRBUF) 884 printf("ffs_make_dirbuf: growing buf to %d\n", 885 dbuf->size); 886 if ((dbuf->buf = realloc(dbuf->buf, dbuf->size)) == NULL) 887 err(1, "Allocating memory for directory buffer"); 888 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ); 889 dbuf->cur = dbuf->size - DIRBLKSIZ; 890 } else { /* shrink end of previous */ 891 dp->d_reclen = ufs_rw16(llen,needswap); 892 dbuf->cur += llen; 893 } 894 dp = (struct direct *)(dbuf->buf + dbuf->cur); 895 memcpy(dp, &de, reclen); 896 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap); 897 } 898 899 /* 900 * cribbed from sys/ufs/ffs/ffs_alloc.c 901 */ 902 static void 903 ffs_write_inode(struct dinode *ip, uint32_t ino, const fsinfo_t *fsopts) 904 { 905 struct dinode ibuf[MAXINOPB]; 906 struct cg *cgp; 907 struct fs *fs; 908 int cg, cgino; 909 daddr_t d; 910 char sbbuf[MAXBSIZE]; 911 912 assert (ip != NULL); 913 assert (ino > 0); 914 assert (fsopts != NULL); 915 916 fs = (struct fs *)fsopts->superblock; 917 cg = ino_to_cg(fs, ino); 918 cgino = ino % fs->fs_ipg; 919 if (debug & DEBUG_FS_WRITE_INODE) 920 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n", 921 ip, ino, cg, cgino); 922 923 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 924 fsopts); 925 cgp = (struct cg *)sbbuf; 926 if (!cg_chkmagic(cgp, fsopts->needswap)) 927 errx(1, "ffs_write_inode: cg %d: bad magic number", cg); 928 929 assert (isclr(cg_inosused(cgp, fsopts->needswap), cgino)); 930 931 if (fs->fs_cstotal.cs_nifree == 0) 932 errx(1, "ffs_write_inode: fs out of inodes for ino %u", 933 ino); 934 if (fs->fs_cs(fs, cg).cs_nifree == 0) 935 errx(1, 936 "ffs_write_inode: cg %d out of inodes for ino %u", 937 cg, ino); 938 setbit(cg_inosused(cgp, fsopts->needswap), cgino); 939 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap); 940 fs->fs_cstotal.cs_nifree--; 941 fs->fs_cs(fs, cg).cs_nifree--; 942 if (S_ISDIR(ip->di_mode)) { 943 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap); 944 fs->fs_cstotal.cs_ndir++; 945 fs->fs_cs(fs, cg).cs_ndir++; 946 } 947 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf, 948 fsopts); 949 950 /* now write inode */ 951 d = fsbtodb(fs, ino_to_fsba(fs, ino)); 952 ffs_rdfs(d, fs->fs_bsize, ibuf, fsopts); 953 if (fsopts->needswap) 954 ffs_dinode_swap(ip, &ibuf[ino_to_fsbo(fs, ino)]); 955 else 956 ibuf[ino_to_fsbo(fs, ino)] = *ip; 957 ffs_wtfs(d, fs->fs_bsize, ibuf, fsopts); 958 } 959 960 void 961 panic(const char *fmt, ...) 962 { 963 va_list ap; 964 965 va_start(ap, fmt); 966 vwarnx(fmt, ap); 967 va_end(ap); 968 exit(1); 969 } 970