1 /* $NetBSD: ffs.c,v 1.44 2009/04/28 22:49:26 joerg 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>
89 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
90 #include <sys/statvfs.h>
93 #include <ufs/ufs/dinode.h>
94 #include <ufs/ufs/dir.h>
95 #include <ufs/ffs/fs.h>
98 #include "ffs/ufs_bswap.h"
99 #include "ffs/ufs_inode.h"
100 #include "ffs/newfs_extern.h"
101 #include "ffs/ffs_extern.h"
104 #define DIP(dp, field) \
105 ((ffs_opts->version == 1) ? \
106 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
109 * Various file system defaults (cribbed from newfs(8)).
111 #define DFL_FRAGSIZE 1024 /* fragment size */
112 #define DFL_BLKSIZE 8192 /* block size */
113 #define DFL_SECSIZE 512 /* sector size */
114 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */
115 #define DFL_FRAGSPERINODE 4 /* fragments per inode */
116 #define DFL_ROTDELAY 0 /* rotational delay */
117 #define DFL_NRPOS 1 /* rotational positions */
118 #define DFL_RPM 3600 /* rpm of disk */
119 #define DFL_NSECTORS 64 /* # of sectors */
120 #define DFL_NTRACKS 16 /* # of tracks */
124 u_char *buf; /* buf for directory */
125 doff_t size; /* full size of buf */
126 doff_t cur; /* offset of current entry */
130 static int ffs_create_image(const char *, fsinfo_t *);
131 static void ffs_dump_fsinfo(fsinfo_t *);
132 static void ffs_dump_dirbuf(dirbuf_t *, const char *, int);
133 static void ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
134 static int ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
135 static void ffs_size_dir(fsnode *, fsinfo_t *);
136 static void ffs_validate(const char *, fsnode *, fsinfo_t *);
137 static void ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
138 static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
139 static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
140 fsnode *, fsinfo_t *);
141 static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
142 fsnode *, fsinfo_t *);
146 int sectorsize; /* XXX: for buf.c::getblk() */
148 /* publicly visible functions */
151 ffs_prep_opts(fsinfo_t *fsopts)
155 if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
156 err(1, "Allocating memory for ffs_options");
158 fsopts->fs_specific = ffs_opts;
163 ffs_opts->density= -1;
164 ffs_opts->minfree= -1;
165 ffs_opts->optimization= -1;
166 ffs_opts->maxcontig= -1;
167 ffs_opts->maxbpg= -1;
168 ffs_opts->avgfilesize= -1;
169 ffs_opts->avgfpdir= -1;
170 ffs_opts->version = 1;
174 ffs_cleanup_opts(fsinfo_t *fsopts)
176 if (fsopts->fs_specific)
177 free(fsopts->fs_specific);
181 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
183 ffs_opt_t *ffs_opts = fsopts->fs_specific;
185 option_t ffs_options[] = {
186 { "bsize", &ffs_opts->bsize, 1, INT_MAX,
188 { "fsize", &ffs_opts->fsize, 1, INT_MAX,
190 { "density", &ffs_opts->density, 1, INT_MAX,
192 { "minfree", &ffs_opts->minfree, 0, 99,
194 { "maxbpf", &ffs_opts->maxbpg, 1, INT_MAX,
195 "max blocks per file in a cg" },
196 { "avgfilesize", &ffs_opts->avgfilesize,1, INT_MAX,
197 "expected average file size" },
198 { "avgfpdir", &ffs_opts->avgfpdir, 1, INT_MAX,
199 "expected # of files per directory" },
200 { "extent", &ffs_opts->maxbsize, 1, INT_MAX,
201 "maximum # extent size" },
202 { "maxbpcg", &ffs_opts->maxblkspercg,1, INT_MAX,
203 "max # of blocks per group" },
204 { "version", &ffs_opts->version, 1, 2,
212 assert(option != NULL);
213 assert(fsopts != NULL);
214 assert(ffs_opts != NULL);
216 if (debug & DEBUG_FS_PARSE_OPTS)
217 printf("ffs_parse_opts: got `%s'\n", option);
219 if ((var = strdup(option)) == NULL)
220 err(1, "Allocating memory for copy of option string");
223 if ((val = strchr(var, '=')) == NULL) {
224 warnx("Option `%s' doesn't contain a value", var);
225 goto leave_ffs_parse_opts;
229 if (strcmp(var, "optimization") == 0) {
230 if (strcmp(val, "time") == 0) {
231 ffs_opts->optimization = FS_OPTTIME;
232 } else if (strcmp(val, "space") == 0) {
233 ffs_opts->optimization = FS_OPTSPACE;
235 warnx("Invalid optimization `%s'", val);
236 goto leave_ffs_parse_opts;
239 } else if (strcmp(var, "label") == 0) {
240 strlcpy(ffs_opts->label, val, sizeof(ffs_opts->label));
243 rv = set_option(ffs_options, var, val);
245 leave_ffs_parse_opts:
253 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
255 struct fs *superblock;
256 struct timeval start;
258 assert(image != NULL);
260 assert(root != NULL);
261 assert(fsopts != NULL);
263 if (debug & DEBUG_FS_MAKEFS)
264 printf("ffs_makefs: image %s directory %s root %p\n",
267 /* validate tree and options */
269 ffs_validate(dir, root, fsopts);
270 TIMER_RESULTS(start, "ffs_validate");
272 printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
273 image, (long long)fsopts->size, (long long)fsopts->inodes);
277 if (ffs_create_image(image, fsopts) == -1)
278 errx(1, "Image file `%s' not created.", image);
279 TIMER_RESULTS(start, "ffs_create_image");
281 fsopts->curinode = ROOTINO;
283 if (debug & DEBUG_FS_MAKEFS)
287 printf("Populating `%s'\n", image);
289 if (! ffs_populate_dir(dir, root, fsopts))
290 errx(1, "Image file `%s' not populated.", image);
291 TIMER_RESULTS(start, "ffs_populate_dir");
293 /* ensure no outstanding buffers remain */
294 if (debug & DEBUG_FS_MAKEFS)
297 /* update various superblock parameters */
298 superblock = fsopts->superblock;
299 superblock->fs_fmod = 0;
300 superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir;
301 superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
302 superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
303 superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
305 /* write out superblock; image is now complete */
306 ffs_write_superblock(fsopts->superblock, fsopts);
307 if (close(fsopts->fd) == -1)
308 err(1, "Closing `%s'", image);
310 printf("Image `%s' complete\n", image);
313 /* end of public functions */
317 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
321 int32_t spc, nspf, ncyl, fssize;
323 ffs_opt_t *ffs_opts = fsopts->fs_specific;
326 assert(root != NULL);
327 assert(fsopts != NULL);
328 assert(ffs_opts != NULL);
330 if (debug & DEBUG_FS_VALIDATE) {
331 printf("ffs_validate: before defaults set:\n");
332 ffs_dump_fsinfo(fsopts);
335 /* set FFS defaults */
336 if (fsopts->sectorsize == -1)
337 fsopts->sectorsize = DFL_SECSIZE;
338 if (ffs_opts->fsize == -1)
339 ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
340 if (ffs_opts->bsize == -1)
341 ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
342 if (ffs_opts->cpg == -1)
343 ffs_opts->cpg = DFL_CYLSPERGROUP;
345 ffs_opts->cpgflg = 1;
346 /* fsopts->density is set below */
347 if (ffs_opts->nsectors == -1)
348 ffs_opts->nsectors = DFL_NSECTORS;
349 if (ffs_opts->minfree == -1)
350 ffs_opts->minfree = MINFREE;
351 if (ffs_opts->optimization == -1)
352 ffs_opts->optimization = DEFAULTOPT;
353 if (ffs_opts->maxcontig == -1)
354 ffs_opts->maxcontig =
355 MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
357 if (ffs_opts->maxbpg == -1)
358 ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
359 if (ffs_opts->avgfilesize == -1)
360 ffs_opts->avgfilesize = AVFILESIZ;
361 if (ffs_opts->avgfpdir == -1)
362 ffs_opts->avgfpdir = AFPDIR;
364 if (roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize)
365 errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d "
366 "exceeds maxsize %lld. Lower bsize, or round the minimum "
367 "and maximum sizes to bsize.", dir,
368 (long long)fsopts->minsize, ffs_opts->bsize,
369 (long long)fsopts->maxsize);
371 /* calculate size of tree */
372 ffs_size_dir(root, fsopts);
373 fsopts->inodes += ROOTINO; /* include first two inodes */
375 if (debug & DEBUG_FS_VALIDATE)
376 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
377 (long long)fsopts->size, (long long)fsopts->inodes);
379 /* add requested slop */
380 fsopts->size += fsopts->freeblocks;
381 fsopts->inodes += fsopts->freefiles;
382 if (fsopts->freefilepc > 0)
384 fsopts->inodes * (100 + fsopts->freefilepc) / 100;
385 if (fsopts->freeblockpc > 0)
387 fsopts->size * (100 + fsopts->freeblockpc) / 100;
389 /* add space needed for superblocks */
391 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
392 * typically used for small filesystems where space matters.
393 * XXX make this an option.
395 fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
396 /* add space needed to store inodes, x3 for blockmaps, etc */
397 if (ffs_opts->version == 1)
398 fsopts->size += ncg * DINODE1_SIZE *
399 roundup(fsopts->inodes / ncg,
400 ffs_opts->bsize / DINODE1_SIZE);
402 fsopts->size += ncg * DINODE2_SIZE *
403 roundup(fsopts->inodes / ncg,
404 ffs_opts->bsize / DINODE2_SIZE);
407 if (ffs_opts->minfree > 0)
409 fsopts->size * (100 + ffs_opts->minfree) / 100;
411 * XXX any other fs slop to add, such as csum's, bitmaps, etc ??
414 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */
415 fsopts->size = fsopts->minsize;
417 /* round up to the next block */
418 fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
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
481 assert (image != NULL);
482 assert (fsopts != NULL);
485 if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666))
487 warn("Can't open `%s' for writing", image);
492 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
493 if (fstatvfs(fsopts->fd, &sfs) == -1) {
496 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
497 warn("can't fstatvfs `%s', using default %d byte chunk",
500 bufsize = sfs.f_iosize;
502 bufrem = fsopts->size;
503 if (fsopts->sparse) {
504 if (ftruncate(fsopts->fd, bufrem) == -1) {
505 warn("sparse option disabled.\n");
509 if (fsopts->sparse) {
510 /* File truncated at bufrem. Remaining is 0 */
514 if (debug & DEBUG_FS_CREATE_IMAGE)
515 printf("zero-ing image `%s', %lld sectors, "
516 "using %d byte chunks\n", image, (long long)bufrem,
518 if ((buf = calloc(1, bufsize)) == NULL) {
519 warn("Can't create buffer for sector");
524 i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
526 warn("zeroing image, %lld bytes to go",
536 /* make the file system */
537 if (debug & DEBUG_FS_CREATE_IMAGE)
538 printf("calling mkfs(\"%s\", ...)\n", image);
539 fs = ffs_mkfs(image, fsopts);
540 fsopts->superblock = (void *)fs;
541 if (debug & DEBUG_FS_CREATE_IMAGE) {
544 t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
545 printf("mkfs returned %p; fs_time %s",
546 fsopts->superblock, ctime(&t));
547 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
548 (long long)fs->fs_cstotal.cs_nbfree,
549 (long long)fs->fs_cstotal.cs_nffree,
550 (long long)fs->fs_cstotal.cs_nifree,
551 (long long)fs->fs_cstotal.cs_ndir);
554 if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) {
556 "Image file `%s' has %lld free inodes; %lld are required.",
558 (long long)(fs->fs_cstotal.cs_nifree + ROOTINO),
559 (long long)fsopts->inodes);
567 ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
569 struct direct tmpdir;
571 int curdirsize, this;
572 ffs_opt_t *ffs_opts = fsopts->fs_specific;
574 /* node may be NULL (empty directory) */
575 assert(fsopts != NULL);
576 assert(ffs_opts != NULL);
578 if (debug & DEBUG_FS_SIZE_DIR)
579 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
580 (long long)fsopts->size, (long long)fsopts->inodes);
582 #define ADDDIRENT(e) do { \
583 tmpdir.d_namlen = strlen((e)); \
584 this = DIRSIZ_SWAP(0, &tmpdir, 0); \
585 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
586 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \
587 e, tmpdir.d_namlen, this, curdirsize); \
588 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \
589 curdirsize = roundup(curdirsize, DIRBLKSIZ); \
590 curdirsize += this; \
591 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
592 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \
593 e, tmpdir.d_namlen, this, curdirsize); \
597 * XXX this needs to take into account extra space consumed
598 * by indirect blocks, etc.
600 #define ADDSIZE(x) do { \
601 fsopts->size += roundup((x), ffs_opts->fsize); \
605 for (node = root; node != NULL; node = node->next) {
606 ADDDIRENT(node->name);
607 if (node == root) { /* we're at "." */
608 assert(strcmp(node->name, ".") == 0);
610 } else if ((node->inode->flags & FI_SIZED) == 0) {
611 /* don't count duplicate names */
612 node->inode->flags |= FI_SIZED;
613 if (debug & DEBUG_FS_SIZE_DIR_NODE)
614 printf("ffs_size_dir: `%s' size %lld\n",
616 (long long)node->inode->st.st_size);
618 if (node->type == S_IFREG)
619 ADDSIZE(node->inode->st.st_size);
620 if (node->type == S_IFLNK) {
623 slen = strlen(node->symlink) + 1;
624 if (slen >= (ffs_opts->version == 1 ?
630 if (node->type == S_IFDIR)
631 ffs_size_dir(node->child, fsopts);
635 if (debug & DEBUG_FS_SIZE_DIR)
636 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
637 (long long)fsopts->size, (long long)fsopts->inodes);
641 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
642 fsnode *root, fsinfo_t *fsopts)
647 memset(dinp, 0, sizeof(*dinp));
648 dinp->di_mode = cur->inode->st.st_mode;
649 dinp->di_nlink = cur->inode->nlink;
650 dinp->di_size = cur->inode->st.st_size;
651 dinp->di_atime = cur->inode->st.st_atime;
652 dinp->di_mtime = cur->inode->st.st_mtime;
653 dinp->di_ctime = cur->inode->st.st_ctime;
654 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
655 dinp->di_atimensec = cur->inode->st.st_atimensec;
656 dinp->di_mtimensec = cur->inode->st.st_mtimensec;
657 dinp->di_ctimensec = cur->inode->st.st_ctimensec;
659 #if HAVE_STRUCT_STAT_ST_FLAGS
660 dinp->di_flags = cur->inode->st.st_flags;
662 #if HAVE_STRUCT_STAT_ST_GEN
663 dinp->di_gen = cur->inode->st.st_gen;
665 dinp->di_uid = cur->inode->st.st_uid;
666 dinp->di_gid = cur->inode->st.st_gid;
667 /* not set: di_db, di_ib, di_blocks, di_spare */
670 if (cur == root) { /* "."; write dirbuf */
672 dinp->di_size = dbufp->size;
673 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
674 dinp->di_size = 0; /* a device */
676 ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
677 } else if (S_ISLNK(cur->type)) { /* symlink */
678 slen = strlen(cur->symlink);
679 if (slen < MAXSYMLINKLEN_UFS1) { /* short link */
680 memcpy(dinp->di_db, cur->symlink, slen);
682 membuf = cur->symlink;
683 dinp->di_size = slen;
689 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
690 fsnode *root, fsinfo_t *fsopts)
695 memset(dinp, 0, sizeof(*dinp));
696 dinp->di_mode = cur->inode->st.st_mode;
697 dinp->di_nlink = cur->inode->nlink;
698 dinp->di_size = cur->inode->st.st_size;
699 dinp->di_atime = cur->inode->st.st_atime;
700 dinp->di_mtime = cur->inode->st.st_mtime;
701 dinp->di_ctime = cur->inode->st.st_ctime;
702 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
703 dinp->di_atimensec = cur->inode->st.st_atimensec;
704 dinp->di_mtimensec = cur->inode->st.st_mtimensec;
705 dinp->di_ctimensec = cur->inode->st.st_ctimensec;
707 #if HAVE_STRUCT_STAT_ST_FLAGS
708 dinp->di_flags = cur->inode->st.st_flags;
710 #if HAVE_STRUCT_STAT_ST_GEN
711 dinp->di_gen = cur->inode->st.st_gen;
713 #if HAVE_STRUCT_STAT_BIRTHTIME
714 dinp->di_birthtime = cur->inode->st.st_birthtime;
715 dinp->di_birthnsec = cur->inode->st.st_birthtimensec;
717 dinp->di_uid = cur->inode->st.st_uid;
718 dinp->di_gid = cur->inode->st.st_gid;
719 /* not set: di_db, di_ib, di_blocks, di_spare */
722 if (cur == root) { /* "."; write dirbuf */
724 dinp->di_size = dbufp->size;
725 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
726 dinp->di_size = 0; /* a device */
728 ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
729 } else if (S_ISLNK(cur->type)) { /* symlink */
730 slen = strlen(cur->symlink);
731 if (slen < MAXSYMLINKLEN_UFS2) { /* short link */
732 memcpy(dinp->di_db, cur->symlink, slen);
734 membuf = cur->symlink;
735 dinp->di_size = slen;
741 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
747 char path[MAXPATHLEN + 1];
748 ffs_opt_t *ffs_opts = fsopts->fs_specific;
751 assert(root != NULL);
752 assert(fsopts != NULL);
753 assert(ffs_opts != NULL);
755 (void)memset(&dirbuf, 0, sizeof(dirbuf));
757 if (debug & DEBUG_FS_POPULATE)
758 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root);
761 * pass 1: allocate inode numbers, build directory `file'
763 for (cur = root; cur != NULL; cur = cur->next) {
764 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
765 cur->inode->flags |= FI_ALLOCATED;
766 if (cur == root && cur->parent != NULL)
767 cur->inode->ino = cur->parent->inode->ino;
769 cur->inode->ino = fsopts->curinode;
773 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
774 if (cur == root) { /* we're at "."; add ".." */
775 ffs_make_dirbuf(&dirbuf, "..",
776 cur->parent == NULL ? cur : cur->parent->first,
778 root->inode->nlink++; /* count my parent's link */
779 } else if (cur->child != NULL)
780 root->inode->nlink++; /* count my child's link */
783 * XXX possibly write file and long symlinks here,
784 * ensuring that blocks get written before inodes?
785 * otoh, this isn't a real filesystem, so who
786 * cares about ordering? :-)
789 if (debug & DEBUG_FS_POPULATE_DIRBUF)
790 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
793 * pass 2: write out dirbuf, then non-directories at this level
795 if (debug & DEBUG_FS_POPULATE)
796 printf("ffs_populate_dir: PASS 2 dir %s\n", dir);
797 for (cur = root; cur != NULL; cur = cur->next) {
798 if (cur->inode->flags & FI_WRITTEN)
799 continue; /* skip hard-linked entries */
800 cur->inode->flags |= FI_WRITTEN;
802 if (cur->contents == NULL) {
803 if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
804 cur->path, cur->name) >= (int)sizeof(path))
805 errx(1, "Pathname too long.");
808 if (cur->child != NULL)
809 continue; /* child creates own inode */
811 /* build on-disk inode */
812 if (ffs_opts->version == 1)
813 membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
816 membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
819 if (debug & DEBUG_FS_POPULATE_NODE) {
820 printf("ffs_populate_dir: writing ino %d, %s",
821 cur->inode->ino, inode_type(cur->type));
822 if (cur->inode->nlink > 1)
823 printf(", nlink %d", cur->inode->nlink);
827 if (membuf != NULL) {
828 ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
829 } else if (S_ISREG(cur->type)) {
830 ffs_write_file(&din, cur->inode->ino,
831 (cur->contents) ? cur->contents : path, fsopts);
833 assert (! S_ISDIR(cur->type));
834 ffs_write_inode(&din, cur->inode->ino, fsopts);
839 * pass 3: write out sub-directories
841 if (debug & DEBUG_FS_POPULATE)
842 printf("ffs_populate_dir: PASS 3 dir %s\n", dir);
843 for (cur = root; cur != NULL; cur = cur->next) {
844 if (cur->child == NULL)
846 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
848 errx(1, "Pathname too long.");
849 if (! ffs_populate_dir(path, cur->child, fsopts))
853 if (debug & DEBUG_FS_POPULATE)
854 printf("ffs_populate_dir: DONE dir %s\n", dir);
857 if (dirbuf.buf != NULL)
864 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
868 off_t bufleft, chunk, offset;
872 ffs_opt_t *ffs_opts = fsopts->fs_specific;
874 assert (din != NULL);
875 assert (buf != NULL);
876 assert (fsopts != NULL);
877 assert (ffs_opts != NULL);
879 isfile = S_ISREG(DIP(din, mode));
884 in.i_fs = (struct fs *)fsopts->superblock;
886 if (debug & DEBUG_FS_WRITE_FILE) {
888 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
889 ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
890 (long long)DIP(din, size));
892 printf(", file '%s'\n", (char *)buf);
894 printf(", buffer %p\n", buf);
898 in.i_size = DIP(din, size);
899 if (ffs_opts->version == 1)
900 memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
901 sizeof(in.i_din.ffs1_din));
903 memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
904 sizeof(in.i_din.ffs2_din));
905 in.i_fd = fsopts->fd;
907 if (DIP(din, size) == 0)
908 goto write_inode_and_leave; /* mmm, cheating */
911 if ((fbuf = malloc(ffs_opts->bsize)) == NULL)
912 err(1, "Allocating memory for write buffer");
913 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
914 warn("Can't open `%s' for reading", (char *)buf);
915 goto leave_ffs_write_file;
922 for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
923 chunk = MIN(bufleft, ffs_opts->bsize);
926 else if ((nread = read(ffd, fbuf, chunk)) == -1)
927 err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
928 (char *)buf, (long long)bufleft);
929 else if (nread != chunk)
930 errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
931 "read %zd bytes, expected %ju bytes, does "
932 "metalog size= attribute mismatch source size?",
933 (char *)buf, (long long)bufleft, nread,
937 offset = DIP(din, size) - bufleft;
938 if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
940 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
941 p, (long long)offset,
942 (long long)chunk, (long long)bufleft);
944 * XXX if holey support is desired, do the check here
946 * XXX might need to write out last bit in fragroundup
947 * sized chunk. however, ffs_balloc() handles this for us
949 errno = ffs_balloc(&in, offset, chunk, &bp);
953 "Writing inode %d (%s), bytes %lld + %lld",
955 isfile ? (char *)buf :
956 inode_type(DIP(din, mode) & S_IFMT),
957 (long long)offset, (long long)chunk);
958 memcpy(bp->b_data, p, chunk);
961 goto bad_ffs_write_file;
967 write_inode_and_leave:
968 ffs_write_inode(&in.i_din, in.i_number, fsopts);
970 leave_ffs_write_file:
979 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
985 assert (dbuf != NULL);
986 assert (dir != NULL);
987 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
988 dir, dbuf->size, dbuf->cur);
990 for (i = 0; i < dbuf->size; ) {
991 de = (struct direct *)(dbuf->buf + i);
992 reclen = ufs_rw16(de->d_reclen, needswap);
994 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
995 ufs_rw32(de->d_ino, needswap),
996 inode_type(DTTOIF(de->d_type)), i, reclen,
997 de->d_namlen, de->d_name);
1004 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
1006 struct direct de, *dp;
1007 uint16_t llen, reclen;
1010 assert (dbuf != NULL);
1011 assert (name != NULL);
1012 assert (node != NULL);
1013 /* create direct entry */
1014 (void)memset(&de, 0, sizeof(de));
1015 de.d_ino = ufs_rw32(node->inode->ino, needswap);
1016 de.d_type = IFTODT(node->type);
1017 de.d_namlen = (uint8_t)strlen(name);
1018 strcpy(de.d_name, name);
1019 reclen = DIRSIZ_SWAP(0, &de, needswap);
1020 de.d_reclen = ufs_rw16(reclen, needswap);
1022 dp = (struct direct *)(dbuf->buf + dbuf->cur);
1025 llen = DIRSIZ_SWAP(0, dp, needswap);
1027 if (debug & DEBUG_FS_MAKE_DIRBUF)
1029 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1030 " ino %d type %d reclen %d namlen %d name %.30s\n",
1031 dbuf->size, dbuf->cur, llen,
1032 ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1033 de.d_namlen, de.d_name);
1035 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1036 if (debug & DEBUG_FS_MAKE_DIRBUF)
1037 printf("ffs_make_dirbuf: growing buf to %d\n",
1038 dbuf->size + DIRBLKSIZ);
1039 if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL)
1040 err(1, "Allocating memory for directory buffer");
1042 dbuf->size += DIRBLKSIZ;
1043 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1044 dbuf->cur = dbuf->size - DIRBLKSIZ;
1045 } else if (dp) { /* shrink end of previous */
1046 dp->d_reclen = ufs_rw16(llen,needswap);
1049 dp = (struct direct *)(dbuf->buf + dbuf->cur);
1050 memcpy(dp, &de, reclen);
1051 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1055 * cribbed from sys/ufs/ffs/ffs_alloc.c
1058 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1061 struct ufs1_dinode *dp1;
1062 struct ufs2_dinode *dp2, *dip;
1067 char sbbuf[FFS_MAXBSIZE];
1069 ffs_opt_t *ffs_opts = fsopts->fs_specific;
1071 assert (dp != NULL);
1073 assert (fsopts != NULL);
1074 assert (ffs_opts != NULL);
1076 fs = (struct fs *)fsopts->superblock;
1077 cg = ino_to_cg(fs, ino);
1078 cgino = ino % fs->fs_ipg;
1079 if (debug & DEBUG_FS_WRITE_INODE)
1080 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1081 dp, ino, cg, cgino);
1083 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1085 cgp = (struct cg *)sbbuf;
1086 if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1087 errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1089 assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1091 buf = malloc(fs->fs_bsize);
1093 errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg);
1095 dp1 = (struct ufs1_dinode *)buf;
1096 dp2 = (struct ufs2_dinode *)buf;
1098 if (fs->fs_cstotal.cs_nifree == 0)
1099 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1101 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1103 "ffs_write_inode: cg %d out of inodes for ino %u",
1105 setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1106 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1107 fs->fs_cstotal.cs_nifree--;
1108 fs->fs_cs(fs, cg).cs_nifree--;
1109 if (S_ISDIR(DIP(dp, mode))) {
1110 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1111 fs->fs_cstotal.cs_ndir++;
1112 fs->fs_cs(fs, cg).cs_ndir++;
1116 * Initialize inode blocks on the fly for UFS2.
1118 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1119 if (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1120 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1121 memset(buf, 0, fs->fs_bsize);
1122 dip = (struct ufs2_dinode *)buf;
1123 srandom(time(NULL));
1124 for (i = 0; i < INOPB(fs); i++) {
1125 dip->di_gen = random() / 2 + 1;
1128 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1129 cg * fs->fs_ipg + initediblk)),
1130 fs->fs_bsize, buf, fsopts);
1131 initediblk += INOPB(fs);
1132 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1136 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1139 /* now write inode */
1140 d = fsbtodb(fs, ino_to_fsba(fs, ino));
1141 ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1142 if (fsopts->needswap) {
1143 if (ffs_opts->version == 1)
1144 ffs_dinode1_swap(&dp->ffs1_din,
1145 &dp1[ino_to_fsbo(fs, ino)]);
1147 ffs_dinode2_swap(&dp->ffs2_din,
1148 &dp2[ino_to_fsbo(fs, ino)]);
1150 if (ffs_opts->version == 1)
1151 dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1153 dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1155 ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1160 panic(const char *fmt, ...)