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