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 #include <sys/cdefs.h> 69 __FBSDID("$FreeBSD$"); 70 71 #include <sys/param.h> 72 73 #include <sys/mount.h> 74 75 #include <assert.h> 76 #include <errno.h> 77 #include <fcntl.h> 78 #include <stdarg.h> 79 #include <stdio.h> 80 #include <stdlib.h> 81 #include <string.h> 82 #include <unistd.h> 83 84 #include "makefs.h" 85 86 #include <ufs/ufs/dinode.h> 87 #include <ufs/ufs/dir.h> 88 #include <ufs/ffs/fs.h> 89 90 #include "ffs/ufs_bswap.h" 91 #include "ffs/ufs_inode.h" 92 #include "ffs/newfs_extern.h" 93 #include "ffs/ffs_extern.h" 94 95 #undef DIP 96 #define DIP(dp, field) \ 97 ((fsopts->version == 1) ? \ 98 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field) 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(union dinode *, uint32_t, void *, fsinfo_t *); 130 static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *); 131 static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *, 132 fsnode *, fsinfo_t *); 133 static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *, 134 fsnode *, fsinfo_t *); 135 136 137 138 int sectorsize; /* XXX: for buf.c::getblk() */ 139 140 /* publically visible functions */ 141 142 int 143 ffs_parse_opts(const char *option, fsinfo_t *fsopts) 144 { 145 option_t ffs_options[] = { 146 { "bsize", &fsopts->bsize, 1, INT_MAX, 147 "block size" }, 148 { "fsize", &fsopts->fsize, 1, INT_MAX, 149 "fragment size" }, 150 { "density", &fsopts->density, 1, INT_MAX, 151 "bytes per inode" }, 152 { "minfree", &fsopts->minfree, 0, 99, 153 "minfree" }, 154 { "maxbpf", &fsopts->maxbpg, 1, INT_MAX, 155 "max blocks per file in a cg" }, 156 { "avgfilesize", &fsopts->avgfilesize, 1, INT_MAX, 157 "expected average file size" }, 158 { "avgfpdir", &fsopts->avgfpdir, 1, INT_MAX, 159 "expected # of files per directory" }, 160 { "extent", &fsopts->maxbsize, 1, INT_MAX, 161 "maximum # extent size" }, 162 { "maxbpcg", &fsopts->maxblkspercg, 1, INT_MAX, 163 "max # of blocks per group" }, 164 { "version", &fsopts->version, 1, 2, 165 "UFS version" }, 166 { NULL } 167 }; 168 169 char *var, *val; 170 int rv; 171 172 (void)&ffs_options; 173 assert(option != NULL); 174 assert(fsopts != NULL); 175 176 if (debug & DEBUG_FS_PARSE_OPTS) 177 printf("ffs_parse_opts: got `%s'\n", option); 178 179 if ((var = strdup(option)) == NULL) 180 err(1, "Allocating memory for copy of option string"); 181 rv = 0; 182 183 if ((val = strchr(var, '=')) == NULL) { 184 warnx("Option `%s' doesn't contain a value", var); 185 goto leave_ffs_parse_opts; 186 } 187 *val++ = '\0'; 188 189 if (strcmp(var, "optimization") == 0) { 190 if (strcmp(val, "time") == 0) { 191 fsopts->optimization = FS_OPTTIME; 192 } else if (strcmp(val, "space") == 0) { 193 fsopts->optimization = FS_OPTSPACE; 194 } else { 195 warnx("Invalid optimization `%s'", val); 196 goto leave_ffs_parse_opts; 197 } 198 rv = 1; 199 } else 200 rv = set_option(ffs_options, var, val); 201 202 leave_ffs_parse_opts: 203 if (var) 204 free(var); 205 return (rv); 206 } 207 208 209 void 210 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) 211 { 212 struct fs *superblock; 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 /* update various superblock parameters */ 255 superblock = fsopts->superblock; 256 superblock->fs_fmod = 0; 257 superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir; 258 superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree; 259 superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree; 260 superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree; 261 262 /* write out superblock; image is now complete */ 263 ffs_write_superblock(fsopts->superblock, fsopts); 264 if (close(fsopts->fd) == -1) 265 err(1, "Closing `%s'", image); 266 fsopts->fd = -1; 267 printf("Image `%s' complete\n", image); 268 } 269 270 /* end of public functions */ 271 272 273 static void 274 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts) 275 { 276 int32_t ncg = 1; 277 #if notyet 278 int32_t spc, nspf, ncyl, fssize; 279 #endif 280 off_t size; 281 282 assert(dir != NULL); 283 assert(root != NULL); 284 assert(fsopts != NULL); 285 286 if (debug & DEBUG_FS_VALIDATE) { 287 printf("ffs_validate: before defaults set:\n"); 288 ffs_dump_fsinfo(fsopts); 289 } 290 291 /* set FFS defaults */ 292 if (fsopts->sectorsize == -1) 293 fsopts->sectorsize = DFL_SECSIZE; 294 if (fsopts->fsize == -1) 295 fsopts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize); 296 if (fsopts->bsize == -1) 297 fsopts->bsize = MIN(DFL_BLKSIZE, 8 * fsopts->fsize); 298 if (fsopts->cpg == -1) 299 fsopts->cpg = DFL_CYLSPERGROUP; 300 else 301 fsopts->cpgflg = 1; 302 /* fsopts->density is set below */ 303 if (fsopts->nsectors == -1) 304 fsopts->nsectors = DFL_NSECTORS; 305 if (fsopts->minfree == -1) 306 fsopts->minfree = MINFREE; 307 if (fsopts->optimization == -1) 308 fsopts->optimization = DEFAULTOPT; 309 if (fsopts->maxcontig == -1) 310 fsopts->maxcontig = 311 MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / fsopts->bsize); 312 /* XXX ondisk32 */ 313 if (fsopts->maxbpg == -1) 314 fsopts->maxbpg = fsopts->bsize / sizeof(int32_t); 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 /* add space needed for superblocks */ 339 /* 340 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is 341 * typically used for small filesystems where space matters. 342 * XXX make this an option. 343 */ 344 fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg; 345 /* add space needed to store inodes, x3 for blockmaps, etc */ 346 if (fsopts->version == 1) 347 fsopts->size += ncg * DINODE1_SIZE * 348 roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE1_SIZE); 349 else 350 fsopts->size += ncg * DINODE2_SIZE * 351 roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE2_SIZE); 352 353 /* add minfree */ 354 if (fsopts->minfree > 0) 355 fsopts->size = 356 fsopts->size * (100 + fsopts->minfree) / 100; 357 /* 358 * XXX any other fs slop to add, such as csum's, bitmaps, etc ?? 359 */ 360 361 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */ 362 fsopts->size = fsopts->minsize; 363 364 /* round up to the next block */ 365 size = roundup(fsopts->size, fsopts->bsize); 366 367 /* now check calculated sizes vs requested sizes */ 368 if (fsopts->maxsize > 0 && size > fsopts->maxsize) { 369 if (debug & DEBUG_FS_VALIDATE) { 370 printf("%s: `%s' size of %lld is larger than the " 371 "maxsize of %lld; rounding down to %lld.", 372 __func__, dir, (long long)size, 373 (long long)fsopts->maxsize, 374 (long long) rounddown(fsopts->size, fsopts->bsize)); 375 } 376 size = rounddown(fsopts->size, fsopts->bsize); 377 } 378 fsopts->size = size; 379 380 /* calculate density if necessary */ 381 if (fsopts->density == -1) 382 fsopts->density = fsopts->size / fsopts->inodes + 1; 383 384 if (debug & DEBUG_FS_VALIDATE) { 385 printf("ffs_validate: after defaults set:\n"); 386 ffs_dump_fsinfo(fsopts); 387 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n", 388 dir, (long long)fsopts->size, (long long)fsopts->inodes); 389 } 390 sectorsize = fsopts->sectorsize; /* XXX - see earlier */ 391 } 392 393 394 static void 395 ffs_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 422 static int 423 ffs_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 503 static void 504 ffs_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_SWAP(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 577 static void * 578 ffs_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 625 static void * 626 ffs_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 677 static int 678 ffs_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 801 static void 802 ffs_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 905 static void 906 ffs_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 930 static void 931 ffs_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_SWAP(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_SWAP(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 */ 984 static void 985 ffs_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_swap(cgp, fsopts->needswap)) 1012 errx(1, "ffs_write_inode: cg %d: bad magic number", cg); 1013 1014 assert (isclr(cg_inosused_swap(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_swap(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 1084 void 1085 panic(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