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