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