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