1 /* $NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $ */
4 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
38 * Copyright (c) 1982, 1986, 1989, 1993
39 * The Regents of the University of California. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include <sys/param.h>
73 #include <sys/mount.h>
90 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
91 #include <sys/statvfs.h>
94 #include <ufs/ufs/dinode.h>
95 #include <ufs/ufs/dir.h>
96 #include <ufs/ffs/fs.h>
99 #include "ffs/ufs_bswap.h"
100 #include "ffs/ufs_inode.h"
101 #include "ffs/newfs_extern.h"
102 #include "ffs/ffs_extern.h"
105 #define DIP(dp, field) \
106 ((ffs_opts->version == 1) ? \
107 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
110 * Various file system defaults (cribbed from newfs(8)).
112 #define DFL_FRAGSIZE 4096 /* fragment size */
113 #define DFL_BLKSIZE 32768 /* block size */
114 #define DFL_SECSIZE 512 /* sector size */
115 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */
116 #define DFL_FRAGSPERINODE 4 /* fragments per inode */
117 #define DFL_ROTDELAY 0 /* rotational delay */
118 #define DFL_NRPOS 1 /* rotational positions */
119 #define DFL_RPM 3600 /* rpm of disk */
120 #define DFL_NSECTORS 64 /* # of sectors */
121 #define DFL_NTRACKS 16 /* # of tracks */
125 u_char *buf; /* buf for directory */
126 doff_t size; /* full size of buf */
127 doff_t cur; /* offset of current entry */
131 static int ffs_create_image(const char *, fsinfo_t *);
132 static void ffs_dump_fsinfo(fsinfo_t *);
133 static void ffs_dump_dirbuf(dirbuf_t *, const char *, int);
134 static void ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
135 static int ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
136 static void ffs_size_dir(fsnode *, fsinfo_t *);
137 static void ffs_validate(const char *, fsnode *, fsinfo_t *);
138 static void ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
139 static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
140 static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
141 fsnode *, fsinfo_t *);
142 static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
143 fsnode *, fsinfo_t *);
147 int sectorsize; /* XXX: for buf.c::getblk() */
148 /* publicly visible functions */
151 ffs_prep_opts(fsinfo_t *fsopts)
153 ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
155 const option_t ffs_options[] = {
156 { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
157 1, INT_MAX, "block size" },
158 { 'f', "fsize", &ffs_opts->fsize, OPT_INT32,
159 1, INT_MAX, "fragment size" },
160 { 'd', "density", &ffs_opts->density, OPT_INT32,
161 1, INT_MAX, "bytes per inode" },
162 { 'm', "minfree", &ffs_opts->minfree, OPT_INT32,
164 { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32,
165 1, INT_MAX, "max blocks per file in a cg" },
166 { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32,
167 1, INT_MAX, "expected average file size" },
168 { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32,
169 1, INT_MAX, "expected # of files per directory" },
170 { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32,
171 1, INT_MAX, "maximum # extent size" },
172 { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32,
173 1, INT_MAX, "max # of blocks per group" },
174 { 'v', "version", &ffs_opts->version, OPT_INT32,
175 1, 2, "UFS version" },
176 { 'o', "optimization", NULL, OPT_STRBUF,
177 0, 0, "Optimization (time|space)" },
178 { 'l', "label", ffs_opts->label, OPT_STRARRAY,
179 1, sizeof(ffs_opts->label), "UFS label" },
186 ffs_opts->density= -1;
187 ffs_opts->minfree= -1;
188 ffs_opts->optimization= -1;
189 ffs_opts->maxcontig= -1;
190 ffs_opts->maxbpg= -1;
191 ffs_opts->avgfilesize= -1;
192 ffs_opts->avgfpdir= -1;
193 ffs_opts->version = 1;
195 fsopts->fs_specific = ffs_opts;
196 fsopts->fs_options = copy_opts(ffs_options);
200 ffs_cleanup_opts(fsinfo_t *fsopts)
202 free(fsopts->fs_specific);
203 free(fsopts->fs_options);
207 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
209 ffs_opt_t *ffs_opts = fsopts->fs_specific;
210 option_t *ffs_options = fsopts->fs_options;
215 assert(option != NULL);
216 assert(fsopts != NULL);
217 assert(ffs_opts != NULL);
219 if (debug & DEBUG_FS_PARSE_OPTS)
220 printf("ffs_parse_opts: got `%s'\n", option);
222 rv = set_option(ffs_options, option, buf, sizeof(buf));
226 if (ffs_options[rv].name == NULL)
229 switch (ffs_options[rv].letter) {
231 if (strcmp(buf, "time") == 0) {
232 ffs_opts->optimization = FS_OPTTIME;
233 } else if (strcmp(buf, "space") == 0) {
234 ffs_opts->optimization = FS_OPTSPACE;
236 warnx("Invalid optimization `%s'", buf);
248 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
250 struct fs *superblock;
251 struct timeval start;
253 assert(image != NULL);
255 assert(root != NULL);
256 assert(fsopts != NULL);
258 if (debug & DEBUG_FS_MAKEFS)
259 printf("ffs_makefs: image %s directory %s root %p\n",
262 /* validate tree and options */
264 ffs_validate(dir, root, fsopts);
265 TIMER_RESULTS(start, "ffs_validate");
267 printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
268 image, (long long)fsopts->size, (long long)fsopts->inodes);
272 if (ffs_create_image(image, fsopts) == -1)
273 errx(1, "Image file `%s' not created.", image);
274 TIMER_RESULTS(start, "ffs_create_image");
276 fsopts->curinode = ROOTINO;
278 if (debug & DEBUG_FS_MAKEFS)
282 printf("Populating `%s'\n", image);
284 if (! ffs_populate_dir(dir, root, fsopts))
285 errx(1, "Image file `%s' not populated.", image);
286 TIMER_RESULTS(start, "ffs_populate_dir");
288 /* ensure no outstanding buffers remain */
289 if (debug & DEBUG_FS_MAKEFS)
292 /* update various superblock parameters */
293 superblock = fsopts->superblock;
294 superblock->fs_fmod = 0;
295 superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir;
296 superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
297 superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
298 superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
300 /* write out superblock; image is now complete */
301 ffs_write_superblock(fsopts->superblock, fsopts);
302 if (close(fsopts->fd) == -1)
303 err(1, "Closing `%s'", image);
305 printf("Image `%s' complete\n", image);
308 /* end of public functions */
312 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
316 int32_t spc, nspf, ncyl, fssize;
318 ffs_opt_t *ffs_opts = fsopts->fs_specific;
321 assert(root != NULL);
322 assert(fsopts != NULL);
323 assert(ffs_opts != NULL);
325 if (debug & DEBUG_FS_VALIDATE) {
326 printf("ffs_validate: before defaults set:\n");
327 ffs_dump_fsinfo(fsopts);
330 /* set FFS defaults */
331 if (fsopts->sectorsize == -1)
332 fsopts->sectorsize = DFL_SECSIZE;
333 if (ffs_opts->fsize == -1)
334 ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
335 if (ffs_opts->bsize == -1)
336 ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
337 if (ffs_opts->cpg == -1)
338 ffs_opts->cpg = DFL_CYLSPERGROUP;
340 ffs_opts->cpgflg = 1;
341 /* fsopts->density is set below */
342 if (ffs_opts->nsectors == -1)
343 ffs_opts->nsectors = DFL_NSECTORS;
344 if (ffs_opts->minfree == -1)
345 ffs_opts->minfree = MINFREE;
346 if (ffs_opts->optimization == -1)
347 ffs_opts->optimization = DEFAULTOPT;
348 if (ffs_opts->maxcontig == -1)
349 ffs_opts->maxcontig =
350 MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
352 if (ffs_opts->maxbpg == -1)
353 ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
354 if (ffs_opts->avgfilesize == -1)
355 ffs_opts->avgfilesize = AVFILESIZ;
356 if (ffs_opts->avgfpdir == -1)
357 ffs_opts->avgfpdir = AFPDIR;
359 if (fsopts->maxsize > 0 &&
360 roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize)
361 errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d "
362 "exceeds maxsize %lld. Lower bsize, or round the minimum "
363 "and maximum sizes to bsize.", dir,
364 (long long)fsopts->minsize, ffs_opts->bsize,
365 (long long)fsopts->maxsize);
367 /* calculate size of tree */
368 ffs_size_dir(root, fsopts);
369 fsopts->inodes += ROOTINO; /* include first two inodes */
371 if (debug & DEBUG_FS_VALIDATE)
372 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
373 (long long)fsopts->size, (long long)fsopts->inodes);
375 /* add requested slop */
376 fsopts->size += fsopts->freeblocks;
377 fsopts->inodes += fsopts->freefiles;
378 if (fsopts->freefilepc > 0)
380 fsopts->inodes * (100 + fsopts->freefilepc) / 100;
381 if (fsopts->freeblockpc > 0)
383 fsopts->size * (100 + fsopts->freeblockpc) / 100;
385 /* add space needed for superblocks */
387 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
388 * typically used for small filesystems where space matters.
389 * XXX make this an option.
391 fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
392 /* add space needed to store inodes, x3 for blockmaps, etc */
393 if (ffs_opts->version == 1)
394 fsopts->size += ncg * DINODE1_SIZE *
395 roundup(fsopts->inodes / ncg,
396 ffs_opts->bsize / DINODE1_SIZE);
398 fsopts->size += ncg * DINODE2_SIZE *
399 roundup(fsopts->inodes / ncg,
400 ffs_opts->bsize / DINODE2_SIZE);
403 if (ffs_opts->minfree > 0)
405 fsopts->size * (100 + ffs_opts->minfree) / 100;
407 * XXX any other fs slop to add, such as csum's, bitmaps, etc ??
410 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */
411 fsopts->size = fsopts->minsize;
413 /* round up to the next block */
414 fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
416 /* round up to requested block size, if any */
417 if (fsopts->roundup > 0)
418 fsopts->size = roundup(fsopts->size, fsopts->roundup);
420 /* calculate density if necessary */
421 if (ffs_opts->density == -1)
422 ffs_opts->density = fsopts->size / fsopts->inodes + 1;
424 if (debug & DEBUG_FS_VALIDATE) {
425 printf("ffs_validate: after defaults set:\n");
426 ffs_dump_fsinfo(fsopts);
427 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
428 dir, (long long)fsopts->size, (long long)fsopts->inodes);
430 sectorsize = fsopts->sectorsize; /* XXX - see earlier */
432 /* now check calculated sizes vs requested sizes */
433 if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
434 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
435 dir, (long long)fsopts->size, (long long)fsopts->maxsize);
441 ffs_dump_fsinfo(fsinfo_t *f)
444 ffs_opt_t *fs = f->fs_specific;
446 printf("fsopts at %p\n", f);
448 printf("\tsize %lld, inodes %lld, curinode %u\n",
449 (long long)f->size, (long long)f->inodes, f->curinode);
451 printf("\tminsize %lld, maxsize %lld\n",
452 (long long)f->minsize, (long long)f->maxsize);
453 printf("\tfree files %lld, freefile %% %d\n",
454 (long long)f->freefiles, f->freefilepc);
455 printf("\tfree blocks %lld, freeblock %% %d\n",
456 (long long)f->freeblocks, f->freeblockpc);
457 printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
459 printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
460 fs->bsize, fs->fsize, fs->cpg, fs->density);
461 printf("\tnsectors %d, rpm %d, minfree %d\n",
462 fs->nsectors, fs->rpm, fs->minfree);
463 printf("\tmaxcontig %d, maxbpg %d\n",
464 fs->maxcontig, fs->maxbpg);
465 printf("\toptimization %s\n",
466 fs->optimization == FS_OPTSPACE ? "space" : "time");
471 ffs_create_image(const char *image, fsinfo_t *fsopts)
473 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
482 assert (image != NULL);
483 assert (fsopts != NULL);
486 if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666))
488 warn("Can't open `%s' for writing", image);
493 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
494 if (fstatvfs(fsopts->fd, &sfs) == -1) {
497 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
498 warn("can't fstatvfs `%s', using default %d byte chunk",
501 bufsize = sfs.f_iosize;
503 bufrem = fsopts->size;
504 if (fsopts->sparse) {
505 if (ftruncate(fsopts->fd, bufrem) == -1) {
506 warn("sparse option disabled.\n");
510 if (fsopts->sparse) {
511 /* File truncated at bufrem. Remaining is 0 */
515 if (debug & DEBUG_FS_CREATE_IMAGE)
516 printf("zero-ing image `%s', %lld sectors, "
517 "using %d byte chunks\n", image, (long long)bufrem,
519 buf = ecalloc(1, bufsize);
522 i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
524 warn("zeroing image, %lld bytes to go",
534 /* make the file system */
535 if (debug & DEBUG_FS_CREATE_IMAGE)
536 printf("calling mkfs(\"%s\", ...)\n", image);
538 if (stampst.st_ino != 0)
539 tstamp = stampst.st_ctime;
541 tstamp = start_time.tv_sec;
545 fs = ffs_mkfs(image, fsopts, tstamp);
546 fsopts->superblock = (void *)fs;
547 if (debug & DEBUG_FS_CREATE_IMAGE) {
550 t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
551 printf("mkfs returned %p; fs_time %s",
552 fsopts->superblock, ctime(&t));
553 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
554 (long long)fs->fs_cstotal.cs_nbfree,
555 (long long)fs->fs_cstotal.cs_nffree,
556 (long long)fs->fs_cstotal.cs_nifree,
557 (long long)fs->fs_cstotal.cs_ndir);
560 if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) {
562 "Image file `%s' has %lld free inodes; %lld are required.",
564 (long long)(fs->fs_cstotal.cs_nifree + ROOTINO),
565 (long long)fsopts->inodes);
573 ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
575 struct direct tmpdir;
577 int curdirsize, this;
578 ffs_opt_t *ffs_opts = fsopts->fs_specific;
580 /* node may be NULL (empty directory) */
581 assert(fsopts != NULL);
582 assert(ffs_opts != NULL);
584 if (debug & DEBUG_FS_SIZE_DIR)
585 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
586 (long long)fsopts->size, (long long)fsopts->inodes);
588 #define ADDDIRENT(e) do { \
589 tmpdir.d_namlen = strlen((e)); \
590 this = DIRSIZ_SWAP(0, &tmpdir, 0); \
591 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
592 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \
593 e, tmpdir.d_namlen, this, curdirsize); \
594 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \
595 curdirsize = roundup(curdirsize, DIRBLKSIZ); \
596 curdirsize += this; \
597 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
598 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \
599 e, tmpdir.d_namlen, this, curdirsize); \
603 * XXX this needs to take into account extra space consumed
604 * by indirect blocks, etc.
606 #define ADDSIZE(x) do { \
607 fsopts->size += roundup((x), ffs_opts->fsize); \
611 for (node = root; node != NULL; node = node->next) {
612 ADDDIRENT(node->name);
613 if (node == root) { /* we're at "." */
614 assert(strcmp(node->name, ".") == 0);
616 } else if ((node->inode->flags & FI_SIZED) == 0) {
617 /* don't count duplicate names */
618 node->inode->flags |= FI_SIZED;
619 if (debug & DEBUG_FS_SIZE_DIR_NODE)
620 printf("ffs_size_dir: `%s' size %lld\n",
622 (long long)node->inode->st.st_size);
624 if (node->type == S_IFREG)
625 ADDSIZE(node->inode->st.st_size);
626 if (node->type == S_IFLNK) {
629 slen = strlen(node->symlink) + 1;
630 if (slen >= (ffs_opts->version == 1 ?
636 if (node->type == S_IFDIR)
637 ffs_size_dir(node->child, fsopts);
641 if (debug & DEBUG_FS_SIZE_DIR)
642 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
643 (long long)fsopts->size, (long long)fsopts->inodes);
647 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
648 fsnode *root, fsinfo_t *fsopts)
652 struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
654 memset(dinp, 0, sizeof(*dinp));
655 dinp->di_mode = cur->inode->st.st_mode;
656 dinp->di_nlink = cur->inode->nlink;
657 dinp->di_size = cur->inode->st.st_size;
658 #if HAVE_STRUCT_STAT_ST_FLAGS
659 dinp->di_flags = cur->inode->st.st_flags;
661 #if HAVE_STRUCT_STAT_ST_GEN
662 dinp->di_gen = cur->inode->st.st_gen;
664 dinp->di_uid = cur->inode->st.st_uid;
665 dinp->di_gid = cur->inode->st.st_gid;
667 dinp->di_atime = st->st_atime;
668 dinp->di_mtime = st->st_mtime;
669 dinp->di_ctime = st->st_ctime;
670 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
671 dinp->di_atimensec = st->st_atimensec;
672 dinp->di_mtimensec = st->st_mtimensec;
673 dinp->di_ctimensec = st->st_ctimensec;
675 /* not set: di_db, di_ib, di_blocks, di_spare */
678 if (cur == root) { /* "."; write dirbuf */
680 dinp->di_size = dbufp->size;
681 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
682 dinp->di_size = 0; /* a device */
684 ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
685 } else if (S_ISLNK(cur->type)) { /* symlink */
686 slen = strlen(cur->symlink);
687 if (slen < MAXSYMLINKLEN_UFS1) { /* short link */
688 memcpy(dinp->di_db, cur->symlink, slen);
690 membuf = cur->symlink;
691 dinp->di_size = slen;
697 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
698 fsnode *root, fsinfo_t *fsopts)
702 struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
704 memset(dinp, 0, sizeof(*dinp));
705 dinp->di_mode = cur->inode->st.st_mode;
706 dinp->di_nlink = cur->inode->nlink;
707 dinp->di_size = cur->inode->st.st_size;
708 #if HAVE_STRUCT_STAT_ST_FLAGS
709 dinp->di_flags = cur->inode->st.st_flags;
711 #if HAVE_STRUCT_STAT_ST_GEN
712 dinp->di_gen = cur->inode->st.st_gen;
714 dinp->di_uid = cur->inode->st.st_uid;
715 dinp->di_gid = cur->inode->st.st_gid;
717 dinp->di_atime = st->st_atime;
718 dinp->di_mtime = st->st_mtime;
719 dinp->di_ctime = st->st_ctime;
720 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
721 dinp->di_atimensec = st->st_atimensec;
722 dinp->di_mtimensec = st->st_mtimensec;
723 dinp->di_ctimensec = st->st_ctimensec;
725 #if HAVE_STRUCT_STAT_BIRTHTIME
726 dinp->di_birthtime = st->st_birthtime;
727 dinp->di_birthnsec = st->st_birthtimensec;
729 /* not set: di_db, di_ib, di_blocks, di_spare */
732 if (cur == root) { /* "."; write dirbuf */
734 dinp->di_size = dbufp->size;
735 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
736 dinp->di_size = 0; /* a device */
738 ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
739 } else if (S_ISLNK(cur->type)) { /* symlink */
740 slen = strlen(cur->symlink);
741 if (slen < MAXSYMLINKLEN_UFS2) { /* short link */
742 memcpy(dinp->di_db, cur->symlink, slen);
744 membuf = cur->symlink;
745 dinp->di_size = slen;
751 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
757 char path[MAXPATHLEN + 1];
758 ffs_opt_t *ffs_opts = fsopts->fs_specific;
761 assert(root != NULL);
762 assert(fsopts != NULL);
763 assert(ffs_opts != NULL);
765 (void)memset(&dirbuf, 0, sizeof(dirbuf));
767 if (debug & DEBUG_FS_POPULATE)
768 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root);
771 * pass 1: allocate inode numbers, build directory `file'
773 for (cur = root; cur != NULL; cur = cur->next) {
774 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
775 cur->inode->flags |= FI_ALLOCATED;
776 if (cur == root && cur->parent != NULL)
777 cur->inode->ino = cur->parent->inode->ino;
779 cur->inode->ino = fsopts->curinode;
783 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
784 if (cur == root) { /* we're at "."; add ".." */
785 ffs_make_dirbuf(&dirbuf, "..",
786 cur->parent == NULL ? cur : cur->parent->first,
788 root->inode->nlink++; /* count my parent's link */
789 } else if (cur->child != NULL)
790 root->inode->nlink++; /* count my child's link */
793 * XXX possibly write file and long symlinks here,
794 * ensuring that blocks get written before inodes?
795 * otoh, this isn't a real filesystem, so who
796 * cares about ordering? :-)
799 if (debug & DEBUG_FS_POPULATE_DIRBUF)
800 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
803 * pass 2: write out dirbuf, then non-directories at this level
805 if (debug & DEBUG_FS_POPULATE)
806 printf("ffs_populate_dir: PASS 2 dir %s\n", dir);
807 for (cur = root; cur != NULL; cur = cur->next) {
808 if (cur->inode->flags & FI_WRITTEN)
809 continue; /* skip hard-linked entries */
810 cur->inode->flags |= FI_WRITTEN;
812 if (cur->contents == NULL) {
813 if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
814 cur->path, cur->name) >= (int)sizeof(path))
815 errx(1, "Pathname too long.");
818 if (cur->child != NULL)
819 continue; /* child creates own inode */
821 /* build on-disk inode */
822 if (ffs_opts->version == 1)
823 membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
826 membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
829 if (debug & DEBUG_FS_POPULATE_NODE) {
830 printf("ffs_populate_dir: writing ino %d, %s",
831 cur->inode->ino, inode_type(cur->type));
832 if (cur->inode->nlink > 1)
833 printf(", nlink %d", cur->inode->nlink);
837 if (membuf != NULL) {
838 ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
839 } else if (S_ISREG(cur->type)) {
840 ffs_write_file(&din, cur->inode->ino,
841 (cur->contents) ? cur->contents : path, fsopts);
843 assert (! S_ISDIR(cur->type));
844 ffs_write_inode(&din, cur->inode->ino, fsopts);
849 * pass 3: write out sub-directories
851 if (debug & DEBUG_FS_POPULATE)
852 printf("ffs_populate_dir: PASS 3 dir %s\n", dir);
853 for (cur = root; cur != NULL; cur = cur->next) {
854 if (cur->child == NULL)
856 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
858 errx(1, "Pathname too long.");
859 if (! ffs_populate_dir(path, cur->child, fsopts))
863 if (debug & DEBUG_FS_POPULATE)
864 printf("ffs_populate_dir: DONE dir %s\n", dir);
867 if (dirbuf.buf != NULL)
874 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
878 off_t bufleft, chunk, offset;
882 ffs_opt_t *ffs_opts = fsopts->fs_specific;
884 assert (din != NULL);
885 assert (buf != NULL);
886 assert (fsopts != NULL);
887 assert (ffs_opts != NULL);
889 isfile = S_ISREG(DIP(din, mode));
894 in.i_fs = (struct fs *)fsopts->superblock;
896 if (debug & DEBUG_FS_WRITE_FILE) {
898 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
899 ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
900 (long long)DIP(din, size));
902 printf(", file '%s'\n", (char *)buf);
904 printf(", buffer %p\n", buf);
908 in.i_size = DIP(din, size);
909 if (ffs_opts->version == 1)
910 memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
911 sizeof(in.i_din.ffs1_din));
913 memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
914 sizeof(in.i_din.ffs2_din));
915 in.i_fd = fsopts->fd;
917 if (DIP(din, size) == 0)
918 goto write_inode_and_leave; /* mmm, cheating */
921 fbuf = emalloc(ffs_opts->bsize);
922 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
923 warn("Can't open `%s' for reading", (char *)buf);
924 goto leave_ffs_write_file;
931 for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
932 chunk = MIN(bufleft, ffs_opts->bsize);
935 else if ((nread = read(ffd, fbuf, chunk)) == -1)
936 err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
937 (char *)buf, (long long)bufleft);
938 else if (nread != chunk)
939 errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
940 "read %zd bytes, expected %ju bytes, does "
941 "metalog size= attribute mismatch source size?",
942 (char *)buf, (long long)bufleft, nread,
946 offset = DIP(din, size) - bufleft;
947 if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
949 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
950 p, (long long)offset,
951 (long long)chunk, (long long)bufleft);
953 * XXX if holey support is desired, do the check here
955 * XXX might need to write out last bit in fragroundup
956 * sized chunk. however, ffs_balloc() handles this for us
958 errno = ffs_balloc(&in, offset, chunk, &bp);
962 "Writing inode %d (%s), bytes %lld + %lld",
964 isfile ? (char *)buf :
965 inode_type(DIP(din, mode) & S_IFMT),
966 (long long)offset, (long long)chunk);
967 memcpy(bp->b_data, p, chunk);
970 goto bad_ffs_write_file;
976 write_inode_and_leave:
977 ffs_write_inode(&in.i_din, in.i_number, fsopts);
979 leave_ffs_write_file:
988 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
994 assert (dbuf != NULL);
995 assert (dir != NULL);
996 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
997 dir, dbuf->size, dbuf->cur);
999 for (i = 0; i < dbuf->size; ) {
1000 de = (struct direct *)(dbuf->buf + i);
1001 reclen = ufs_rw16(de->d_reclen, needswap);
1003 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
1004 ufs_rw32(de->d_ino, needswap),
1005 inode_type(DTTOIF(de->d_type)), i, reclen,
1006 de->d_namlen, de->d_name);
1013 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
1015 struct direct de, *dp;
1016 uint16_t llen, reclen;
1019 assert (dbuf != NULL);
1020 assert (name != NULL);
1021 assert (node != NULL);
1022 /* create direct entry */
1023 (void)memset(&de, 0, sizeof(de));
1024 de.d_ino = ufs_rw32(node->inode->ino, needswap);
1025 de.d_type = IFTODT(node->type);
1026 de.d_namlen = (uint8_t)strlen(name);
1027 strcpy(de.d_name, name);
1028 reclen = DIRSIZ_SWAP(0, &de, needswap);
1029 de.d_reclen = ufs_rw16(reclen, needswap);
1031 dp = (struct direct *)(dbuf->buf + dbuf->cur);
1034 llen = DIRSIZ_SWAP(0, dp, needswap);
1036 if (debug & DEBUG_FS_MAKE_DIRBUF)
1038 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1039 " ino %d type %d reclen %d namlen %d name %.30s\n",
1040 dbuf->size, dbuf->cur, llen,
1041 ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1042 de.d_namlen, de.d_name);
1044 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1045 if (debug & DEBUG_FS_MAKE_DIRBUF)
1046 printf("ffs_make_dirbuf: growing buf to %d\n",
1047 dbuf->size + DIRBLKSIZ);
1048 newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ);
1050 dbuf->size += DIRBLKSIZ;
1051 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1052 dbuf->cur = dbuf->size - DIRBLKSIZ;
1053 } else if (dp) { /* shrink end of previous */
1054 dp->d_reclen = ufs_rw16(llen,needswap);
1057 dp = (struct direct *)(dbuf->buf + dbuf->cur);
1058 memcpy(dp, &de, reclen);
1059 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1063 * cribbed from sys/ufs/ffs/ffs_alloc.c
1066 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1069 struct ufs1_dinode *dp1;
1070 struct ufs2_dinode *dp2, *dip;
1075 char sbbuf[FFS_MAXBSIZE];
1077 ffs_opt_t *ffs_opts = fsopts->fs_specific;
1079 assert (dp != NULL);
1081 assert (fsopts != NULL);
1082 assert (ffs_opts != NULL);
1084 fs = (struct fs *)fsopts->superblock;
1085 cg = ino_to_cg(fs, ino);
1086 cgino = ino % fs->fs_ipg;
1087 if (debug & DEBUG_FS_WRITE_INODE)
1088 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1089 dp, ino, cg, cgino);
1091 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1093 cgp = (struct cg *)sbbuf;
1094 if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1095 errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1097 assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1099 buf = emalloc(fs->fs_bsize);
1100 dp1 = (struct ufs1_dinode *)buf;
1101 dp2 = (struct ufs2_dinode *)buf;
1103 if (fs->fs_cstotal.cs_nifree == 0)
1104 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1106 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1108 "ffs_write_inode: cg %d out of inodes for ino %u",
1110 setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1111 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1112 fs->fs_cstotal.cs_nifree--;
1113 fs->fs_cs(fs, cg).cs_nifree--;
1114 if (S_ISDIR(DIP(dp, mode))) {
1115 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1116 fs->fs_cstotal.cs_ndir++;
1117 fs->fs_cs(fs, cg).cs_ndir++;
1121 * Initialize inode blocks on the fly for UFS2.
1123 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1124 while (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1125 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1126 memset(buf, 0, fs->fs_bsize);
1127 dip = (struct ufs2_dinode *)buf;
1128 for (i = 0; i < INOPB(fs); i++) {
1129 dip->di_gen = random();
1132 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1133 cg * fs->fs_ipg + initediblk)),
1134 fs->fs_bsize, buf, fsopts);
1135 initediblk += INOPB(fs);
1136 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1140 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1143 /* now write inode */
1144 d = fsbtodb(fs, ino_to_fsba(fs, ino));
1145 ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1146 if (fsopts->needswap) {
1147 if (ffs_opts->version == 1)
1148 ffs_dinode1_swap(&dp->ffs1_din,
1149 &dp1[ino_to_fsbo(fs, ino)]);
1151 ffs_dinode2_swap(&dp->ffs2_din,
1152 &dp2[ino_to_fsbo(fs, ino)]);
1154 if (ffs_opts->version == 1)
1155 dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1157 dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1159 ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1164 panic(const char *fmt, ...)