1 /* $NetBSD: ffs.c,v 1.30 2004/06/24 22:30:13 lukem 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>
86 #include <ufs/ufs/dinode.h>
87 #include <ufs/ufs/dir.h>
88 #include <ufs/ffs/fs.h>
90 #include "ffs/ufs_bswap.h"
91 #include "ffs/ufs_inode.h"
92 #include "ffs/newfs_extern.h"
93 #include "ffs/ffs_extern.h"
96 #define DIP(dp, field) \
97 ((fsopts->version == 1) ? \
98 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
101 * Various file system defaults (cribbed from newfs(8)).
103 #define DFL_FRAGSIZE 1024 /* fragment size */
104 #define DFL_BLKSIZE 8192 /* block size */
105 #define DFL_SECSIZE 512 /* sector size */
106 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */
107 #define DFL_FRAGSPERINODE 4 /* fragments per inode */
108 #define DFL_ROTDELAY 0 /* rotational delay */
109 #define DFL_NRPOS 1 /* rotational positions */
110 #define DFL_RPM 3600 /* rpm of disk */
111 #define DFL_NSECTORS 64 /* # of sectors */
112 #define DFL_NTRACKS 16 /* # of tracks */
116 u_char *buf; /* buf for directory */
117 doff_t size; /* full size of buf */
118 doff_t cur; /* offset of current entry */
122 static int ffs_create_image(const char *, fsinfo_t *);
123 static void ffs_dump_fsinfo(fsinfo_t *);
124 static void ffs_dump_dirbuf(dirbuf_t *, const char *, int);
125 static void ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
126 static int ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
127 static void ffs_size_dir(fsnode *, fsinfo_t *);
128 static void ffs_validate(const char *, fsnode *, fsinfo_t *);
129 static void ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
130 static void ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
131 static void *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
132 fsnode *, fsinfo_t *);
133 static void *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
134 fsnode *, fsinfo_t *);
138 int sectorsize; /* XXX: for buf.c::getblk() */
140 /* publically visible functions */
143 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
145 option_t ffs_options[] = {
146 { "bsize", &fsopts->bsize, 1, INT_MAX,
148 { "fsize", &fsopts->fsize, 1, INT_MAX,
150 { "density", &fsopts->density, 1, INT_MAX,
152 { "minfree", &fsopts->minfree, 0, 99,
154 { "maxbpf", &fsopts->maxbpg, 1, INT_MAX,
155 "max blocks per file in a cg" },
156 { "avgfilesize", &fsopts->avgfilesize, 1, INT_MAX,
157 "expected average file size" },
158 { "avgfpdir", &fsopts->avgfpdir, 1, INT_MAX,
159 "expected # of files per directory" },
160 { "extent", &fsopts->maxbsize, 1, INT_MAX,
161 "maximum # extent size" },
162 { "maxbpcg", &fsopts->maxblkspercg, 1, INT_MAX,
163 "max # of blocks per group" },
164 { "version", &fsopts->version, 1, 2,
173 assert(option != NULL);
174 assert(fsopts != NULL);
176 if (debug & DEBUG_FS_PARSE_OPTS)
177 printf("ffs_parse_opts: got `%s'\n", option);
179 if ((var = strdup(option)) == NULL)
180 err(1, "Allocating memory for copy of option string");
183 if ((val = strchr(var, '=')) == NULL) {
184 warnx("Option `%s' doesn't contain a value", var);
185 goto leave_ffs_parse_opts;
189 if (strcmp(var, "optimization") == 0) {
190 if (strcmp(val, "time") == 0) {
191 fsopts->optimization = FS_OPTTIME;
192 } else if (strcmp(val, "space") == 0) {
193 fsopts->optimization = FS_OPTSPACE;
195 warnx("Invalid optimization `%s'", val);
196 goto leave_ffs_parse_opts;
200 rv = set_option(ffs_options, var, val);
202 leave_ffs_parse_opts:
210 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
212 struct fs *superblock;
213 struct timeval start;
215 assert(image != NULL);
217 assert(root != NULL);
218 assert(fsopts != NULL);
220 if (debug & DEBUG_FS_MAKEFS)
221 printf("ffs_makefs: image %s directory %s root %p\n",
224 /* validate tree and options */
226 ffs_validate(dir, root, fsopts);
227 TIMER_RESULTS(start, "ffs_validate");
229 printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
230 image, (long long)fsopts->size, (long long)fsopts->inodes);
234 if (ffs_create_image(image, fsopts) == -1)
235 errx(1, "Image file `%s' not created.", image);
236 TIMER_RESULTS(start, "ffs_create_image");
238 fsopts->curinode = ROOTINO;
240 if (debug & DEBUG_FS_MAKEFS)
244 printf("Populating `%s'\n", image);
246 if (! ffs_populate_dir(dir, root, fsopts))
247 errx(1, "Image file `%s' not populated.", image);
248 TIMER_RESULTS(start, "ffs_populate_dir");
250 /* ensure no outstanding buffers remain */
251 if (debug & DEBUG_FS_MAKEFS)
254 /* update various superblock parameters */
255 superblock = fsopts->superblock;
256 superblock->fs_fmod = 0;
257 superblock->fs_old_cstotal.cs_ndir = superblock->fs_cstotal.cs_ndir;
258 superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
259 superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
260 superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
262 /* write out superblock; image is now complete */
263 ffs_write_superblock(fsopts->superblock, fsopts);
264 if (close(fsopts->fd) == -1)
265 err(1, "Closing `%s'", image);
267 printf("Image `%s' complete\n", image);
270 /* end of public functions */
274 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
278 int32_t spc, nspf, ncyl, fssize;
283 assert(root != NULL);
284 assert(fsopts != NULL);
286 if (debug & DEBUG_FS_VALIDATE) {
287 printf("ffs_validate: before defaults set:\n");
288 ffs_dump_fsinfo(fsopts);
291 /* set FFS defaults */
292 if (fsopts->sectorsize == -1)
293 fsopts->sectorsize = DFL_SECSIZE;
294 if (fsopts->fsize == -1)
295 fsopts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
296 if (fsopts->bsize == -1)
297 fsopts->bsize = MIN(DFL_BLKSIZE, 8 * fsopts->fsize);
298 if (fsopts->cpg == -1)
299 fsopts->cpg = DFL_CYLSPERGROUP;
302 /* fsopts->density is set below */
303 if (fsopts->nsectors == -1)
304 fsopts->nsectors = DFL_NSECTORS;
305 if (fsopts->minfree == -1)
306 fsopts->minfree = MINFREE;
307 if (fsopts->optimization == -1)
308 fsopts->optimization = DEFAULTOPT;
309 if (fsopts->maxcontig == -1)
311 MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / fsopts->bsize);
313 if (fsopts->maxbpg == -1)
314 fsopts->maxbpg = fsopts->bsize / sizeof(int32_t);
315 if (fsopts->avgfilesize == -1)
316 fsopts->avgfilesize = AVFILESIZ;
317 if (fsopts->avgfpdir == -1)
318 fsopts->avgfpdir = AFPDIR;
320 /* calculate size of tree */
321 ffs_size_dir(root, fsopts);
322 fsopts->inodes += ROOTINO; /* include first two inodes */
324 if (debug & DEBUG_FS_VALIDATE)
325 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
326 (long long)fsopts->size, (long long)fsopts->inodes);
328 /* add requested slop */
329 fsopts->size += fsopts->freeblocks;
330 fsopts->inodes += fsopts->freefiles;
331 if (fsopts->freefilepc > 0)
333 fsopts->inodes * (100 + fsopts->freefilepc) / 100;
334 if (fsopts->freeblockpc > 0)
336 fsopts->size * (100 + fsopts->freeblockpc) / 100;
338 /* add space needed for superblocks */
340 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
341 * typically used for small filesystems where space matters.
342 * XXX make this an option.
344 fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
345 /* add space needed to store inodes, x3 for blockmaps, etc */
346 if (fsopts->version == 1)
347 fsopts->size += ncg * DINODE1_SIZE *
348 roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE1_SIZE);
350 fsopts->size += ncg * DINODE2_SIZE *
351 roundup(fsopts->inodes / ncg, fsopts->bsize / DINODE2_SIZE);
354 if (fsopts->minfree > 0)
356 fsopts->size * (100 + fsopts->minfree) / 100;
358 * XXX any other fs slop to add, such as csum's, bitmaps, etc ??
361 if (fsopts->size < fsopts->minsize) /* ensure meets minimum size */
362 fsopts->size = fsopts->minsize;
364 /* round up to the next block */
365 size = roundup(fsopts->size, fsopts->bsize);
367 /* now check calculated sizes vs requested sizes */
368 if (fsopts->maxsize > 0 && size > fsopts->maxsize) {
369 if (debug & DEBUG_FS_VALIDATE) {
370 printf("%s: `%s' size of %lld is larger than the "
371 "maxsize of %lld; rounding down to %lld.",
372 __func__, dir, (long long)size,
373 (long long)fsopts->maxsize,
374 (long long) rounddown(fsopts->size, fsopts->bsize));
376 size = rounddown(fsopts->size, fsopts->bsize);
380 /* calculate density if necessary */
381 if (fsopts->density == -1)
382 fsopts->density = fsopts->size / fsopts->inodes + 1;
384 if (debug & DEBUG_FS_VALIDATE) {
385 printf("ffs_validate: after defaults set:\n");
386 ffs_dump_fsinfo(fsopts);
387 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
388 dir, (long long)fsopts->size, (long long)fsopts->inodes);
390 sectorsize = fsopts->sectorsize; /* XXX - see earlier */
395 ffs_dump_fsinfo(fsinfo_t *f)
398 printf("fsopts at %p\n", f);
400 printf("\tsize %lld, inodes %lld, curinode %u\n",
401 (long long)f->size, (long long)f->inodes, f->curinode);
403 printf("\tminsize %lld, maxsize %lld\n",
404 (long long)f->minsize, (long long)f->maxsize);
405 printf("\tfree files %lld, freefile %% %d\n",
406 (long long)f->freefiles, f->freefilepc);
407 printf("\tfree blocks %lld, freeblock %% %d\n",
408 (long long)f->freeblocks, f->freeblockpc);
409 printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
411 printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
412 f->bsize, f->fsize, f->cpg, f->density);
413 printf("\tnsectors %d, rpm %d, minfree %d\n",
414 f->nsectors, f->rpm, f->minfree);
415 printf("\tmaxcontig %d, maxbpg %d\n",
416 f->maxcontig, f->maxbpg);
417 printf("\toptimization %s\n",
418 f->optimization == FS_OPTSPACE ? "space" : "time");
423 ffs_create_image(const char *image, fsinfo_t *fsopts)
425 #if HAVE_STRUCT_STATVFS_F_IOSIZE
433 assert (image != NULL);
434 assert (fsopts != NULL);
437 if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0777))
439 warn("Can't open `%s' for writing", image);
444 #if HAVE_STRUCT_STATVFS_F_IOSIZE
445 if (fstatvfs(fsopts->fd, &sfs) == -1) {
448 #if HAVE_STRUCT_STATVFS_F_IOSIZE
449 warn("can't fstatvfs `%s', using default %d byte chunk",
452 bufsize = sfs.f_iosize;
454 bufrem = fsopts->size;
455 if (debug & DEBUG_FS_CREATE_IMAGE)
457 "zero-ing image `%s', %lld sectors, using %d byte chunks\n",
458 image, (long long)bufrem, bufsize);
459 if ((buf = calloc(1, bufsize)) == NULL) {
460 warn("Can't create buffer for sector");
464 i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
466 warn("zeroing image, %lld bytes to go",
473 /* make the file system */
474 if (debug & DEBUG_FS_CREATE_IMAGE)
475 printf("calling mkfs(\"%s\", ...)\n", image);
476 fs = ffs_mkfs(image, fsopts);
477 fsopts->superblock = (void *)fs;
478 if (debug & DEBUG_FS_CREATE_IMAGE) {
481 t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
482 printf("mkfs returned %p; fs_time %s",
483 fsopts->superblock, ctime(&t));
484 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
485 (long long)fs->fs_cstotal.cs_nbfree,
486 (long long)fs->fs_cstotal.cs_nffree,
487 (long long)fs->fs_cstotal.cs_nifree,
488 (long long)fs->fs_cstotal.cs_ndir);
491 if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) {
493 "Image file `%s' has %lld free inodes; %lld are required.",
495 (long long)fs->fs_cstotal.cs_nifree + ROOTINO,
496 (long long)fsopts->inodes);
504 ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
506 struct direct tmpdir;
508 int curdirsize, this;
510 /* node may be NULL (empty directory) */
511 assert(fsopts != NULL);
513 if (debug & DEBUG_FS_SIZE_DIR)
514 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
515 (long long)fsopts->size, (long long)fsopts->inodes);
517 #define ADDDIRENT(e) do { \
518 tmpdir.d_namlen = strlen((e)); \
519 this = DIRSIZ_SWAP(0, &tmpdir, 0); \
520 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
521 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \
522 e, tmpdir.d_namlen, this, curdirsize); \
523 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \
524 curdirsize = roundup(curdirsize, DIRBLKSIZ); \
525 curdirsize += this; \
526 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
527 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \
528 e, tmpdir.d_namlen, this, curdirsize); \
532 * XXX this needs to take into account extra space consumed
533 * by indirect blocks, etc.
535 #define ADDSIZE(x) do { \
536 fsopts->size += roundup((x), fsopts->fsize); \
540 for (node = root; node != NULL; node = node->next) {
541 ADDDIRENT(node->name);
542 if (FSNODE_EXCLUDE_P(fsopts, node))
544 if (node == root) { /* we're at "." */
545 assert(strcmp(node->name, ".") == 0);
547 } else if ((node->inode->flags & FI_SIZED) == 0) {
548 /* don't count duplicate names */
549 node->inode->flags |= FI_SIZED;
550 if (debug & DEBUG_FS_SIZE_DIR_NODE)
551 printf("ffs_size_dir: `%s' size %lld\n",
553 (long long)node->inode->st.st_size);
555 if (node->type == S_IFREG)
556 ADDSIZE(node->inode->st.st_size);
557 if (node->type == S_IFLNK) {
560 slen = strlen(node->symlink) + 1;
561 if (slen >= (fsopts->version == 1 ?
567 if (node->type == S_IFDIR)
568 ffs_size_dir(node->child, fsopts);
572 if (debug & DEBUG_FS_SIZE_DIR)
573 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
574 (long long)fsopts->size, (long long)fsopts->inodes);
578 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
579 fsnode *root, fsinfo_t *fsopts)
584 memset(dinp, 0, sizeof(*dinp));
585 dinp->di_mode = cur->inode->st.st_mode;
586 dinp->di_nlink = cur->inode->nlink;
587 dinp->di_size = cur->inode->st.st_size;
588 dinp->di_atime = cur->inode->st.st_atime;
589 dinp->di_mtime = cur->inode->st.st_mtime;
590 dinp->di_ctime = cur->inode->st.st_ctime;
591 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
592 dinp->di_atimensec = cur->inode->st.st_atimensec;
593 dinp->di_mtimensec = cur->inode->st.st_mtimensec;
594 dinp->di_ctimensec = cur->inode->st.st_ctimensec;
596 #if HAVE_STRUCT_STAT_ST_FLAGS
597 dinp->di_flags = cur->inode->st.st_flags;
599 #if HAVE_STRUCT_STAT_ST_GEN
600 dinp->di_gen = cur->inode->st.st_gen;
602 dinp->di_uid = cur->inode->st.st_uid;
603 dinp->di_gid = cur->inode->st.st_gid;
604 /* not set: di_db, di_ib, di_blocks, di_spare */
607 if (cur == root) { /* "."; write dirbuf */
609 dinp->di_size = dbufp->size;
610 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
611 dinp->di_size = 0; /* a device */
613 ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
614 } else if (S_ISLNK(cur->type)) { /* symlink */
615 slen = strlen(cur->symlink);
616 if (slen < MAXSYMLINKLEN_UFS1) { /* short link */
617 memcpy(dinp->di_db, cur->symlink, slen);
619 membuf = cur->symlink;
620 dinp->di_size = slen;
626 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
627 fsnode *root, fsinfo_t *fsopts)
632 memset(dinp, 0, sizeof(*dinp));
633 dinp->di_mode = cur->inode->st.st_mode;
634 dinp->di_nlink = cur->inode->nlink;
635 dinp->di_size = cur->inode->st.st_size;
636 dinp->di_atime = cur->inode->st.st_atime;
637 dinp->di_mtime = cur->inode->st.st_mtime;
638 dinp->di_ctime = cur->inode->st.st_ctime;
639 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
640 dinp->di_atimensec = cur->inode->st.st_atimensec;
641 dinp->di_mtimensec = cur->inode->st.st_mtimensec;
642 dinp->di_ctimensec = cur->inode->st.st_ctimensec;
644 #if HAVE_STRUCT_STAT_ST_FLAGS
645 dinp->di_flags = cur->inode->st.st_flags;
647 #if HAVE_STRUCT_STAT_ST_GEN
648 dinp->di_gen = cur->inode->st.st_gen;
650 #if HAVE_STRUCT_STAT_BIRTHTIME
651 dinp->di_birthtime = cur->inode->st.st_birthtime;
652 dinp->di_birthnsec = cur->inode->st.st_birthtimensec;
654 dinp->di_uid = cur->inode->st.st_uid;
655 dinp->di_gid = cur->inode->st.st_gid;
656 /* not set: di_db, di_ib, di_blocks, di_spare */
659 if (cur == root) { /* "."; write dirbuf */
661 dinp->di_size = dbufp->size;
662 } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
663 dinp->di_size = 0; /* a device */
665 ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
666 } else if (S_ISLNK(cur->type)) { /* symlink */
667 slen = strlen(cur->symlink);
668 if (slen < MAXSYMLINKLEN_UFS2) { /* short link */
669 memcpy(dinp->di_db, cur->symlink, slen);
671 membuf = cur->symlink;
672 dinp->di_size = slen;
678 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
684 char path[MAXPATHLEN + 1];
687 assert(root != NULL);
688 assert(fsopts != NULL);
690 (void)memset(&dirbuf, 0, sizeof(dirbuf));
692 if (debug & DEBUG_FS_POPULATE)
693 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir, root);
696 * pass 1: allocate inode numbers, build directory `file'
698 for (cur = root; cur != NULL; cur = cur->next) {
699 if (FSNODE_EXCLUDE_P(fsopts, cur))
701 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
702 cur->inode->flags |= FI_ALLOCATED;
703 if (cur == root && cur->parent != NULL)
704 cur->inode->ino = cur->parent->inode->ino;
706 cur->inode->ino = fsopts->curinode;
710 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
711 if (cur == root) { /* we're at "."; add ".." */
712 ffs_make_dirbuf(&dirbuf, "..",
713 cur->parent == NULL ? cur : cur->parent->first,
715 root->inode->nlink++; /* count my parent's link */
716 } else if (cur->child != NULL)
717 root->inode->nlink++; /* count my child's link */
720 * XXX possibly write file and long symlinks here,
721 * ensuring that blocks get written before inodes?
722 * otoh, this isn't a real filesystem, so who
723 * cares about ordering? :-)
726 if (debug & DEBUG_FS_POPULATE_DIRBUF)
727 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
730 * pass 2: write out dirbuf, then non-directories at this level
732 if (debug & DEBUG_FS_POPULATE)
733 printf("ffs_populate_dir: PASS 2 dir %s\n", dir);
734 for (cur = root; cur != NULL; cur = cur->next) {
735 if (FSNODE_EXCLUDE_P(fsopts, cur))
737 if (cur->inode->flags & FI_WRITTEN)
738 continue; /* skip hard-linked entries */
739 cur->inode->flags |= FI_WRITTEN;
741 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
743 errx(1, "Pathname too long.");
745 if (cur->child != NULL)
746 continue; /* child creates own inode */
748 /* build on-disk inode */
749 if (fsopts->version == 1)
750 membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
753 membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
756 if (debug & DEBUG_FS_POPULATE_NODE) {
757 printf("ffs_populate_dir: writing ino %d, %s",
758 cur->inode->ino, inode_type(cur->type));
759 if (cur->inode->nlink > 1)
760 printf(", nlink %d", cur->inode->nlink);
764 if (membuf != NULL) {
765 ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
766 } else if (S_ISREG(cur->type)) {
767 ffs_write_file(&din, cur->inode->ino, path, fsopts);
769 assert (! S_ISDIR(cur->type));
770 ffs_write_inode(&din, cur->inode->ino, fsopts);
775 * pass 3: write out sub-directories
777 if (debug & DEBUG_FS_POPULATE)
778 printf("ffs_populate_dir: PASS 3 dir %s\n", dir);
779 for (cur = root; cur != NULL; cur = cur->next) {
780 if (FSNODE_EXCLUDE_P(fsopts, cur))
782 if (cur->child == NULL)
784 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
786 errx(1, "Pathname too long.");
787 if (! ffs_populate_dir(path, cur->child, fsopts))
791 if (debug & DEBUG_FS_POPULATE)
792 printf("ffs_populate_dir: DONE dir %s\n", dir);
795 if (dirbuf.buf != NULL)
802 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
806 off_t bufleft, chunk, offset;
810 assert (din != NULL);
811 assert (buf != NULL);
812 assert (fsopts != NULL);
814 isfile = S_ISREG(DIP(din, mode));
818 in.i_fs = (struct fs *)fsopts->superblock;
820 if (debug & DEBUG_FS_WRITE_FILE) {
822 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
823 ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
824 (long long)DIP(din, size));
826 printf(", file '%s'\n", (char *)buf);
828 printf(", buffer %p\n", buf);
832 in.i_size = DIP(din, size);
833 if (fsopts->version == 1)
834 memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
835 sizeof(in.i_din.ffs1_din));
837 memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
838 sizeof(in.i_din.ffs2_din));
839 in.i_fd = fsopts->fd;
841 if (DIP(din, size) == 0)
842 goto write_inode_and_leave; /* mmm, cheating */
845 if ((fbuf = malloc(fsopts->bsize)) == NULL)
846 err(1, "Allocating memory for write buffer");
847 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
848 warn("Can't open `%s' for reading", (char *)buf);
849 goto leave_ffs_write_file;
856 for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
857 chunk = MIN(bufleft, fsopts->bsize);
859 if (read(ffd, fbuf, chunk) != chunk)
860 err(1, "Reading `%s', %lld bytes to go",
861 (char *)buf, (long long)bufleft);
864 offset = DIP(din, size) - bufleft;
865 if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
867 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
868 p, (long long)offset,
869 (long long)chunk, (long long)bufleft);
871 * XXX if holey support is desired, do the check here
873 * XXX might need to write out last bit in fragroundup
874 * sized chunk. however, ffs_balloc() handles this for us
876 errno = ffs_balloc(&in, offset, chunk, &bp);
880 "Writing inode %d (%s), bytes %lld + %lld",
882 isfile ? (char *)buf :
883 inode_type(DIP(din, mode) & S_IFMT),
884 (long long)offset, (long long)chunk);
885 memcpy(bp->b_data, p, chunk);
888 goto bad_ffs_write_file;
894 write_inode_and_leave:
895 ffs_write_inode(&in.i_din, in.i_number, fsopts);
897 leave_ffs_write_file:
906 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
912 assert (dbuf != NULL);
913 assert (dir != NULL);
914 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
915 dir, dbuf->size, dbuf->cur);
917 for (i = 0; i < dbuf->size; ) {
918 de = (struct direct *)(dbuf->buf + i);
919 reclen = ufs_rw16(de->d_reclen, needswap);
921 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
922 ufs_rw32(de->d_ino, needswap),
923 inode_type(DTTOIF(de->d_type)), i, reclen,
924 de->d_namlen, de->d_name);
931 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
933 struct direct de, *dp;
934 uint16_t llen, reclen;
937 assert (dbuf != NULL);
938 assert (name != NULL);
939 assert (node != NULL);
940 /* create direct entry */
941 (void)memset(&de, 0, sizeof(de));
942 de.d_ino = ufs_rw32(node->inode->ino, needswap);
943 de.d_type = IFTODT(node->type);
944 de.d_namlen = (uint8_t)strlen(name);
945 strcpy(de.d_name, name);
946 reclen = DIRSIZ_SWAP(0, &de, needswap);
947 de.d_reclen = ufs_rw16(reclen, needswap);
949 dp = (struct direct *)(dbuf->buf + dbuf->cur);
952 llen = DIRSIZ_SWAP(0, dp, needswap);
954 if (debug & DEBUG_FS_MAKE_DIRBUF)
956 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
957 " ino %d type %d reclen %d namlen %d name %.30s\n",
958 dbuf->size, dbuf->cur, llen,
959 ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
960 de.d_namlen, de.d_name);
962 if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
963 if (debug & DEBUG_FS_MAKE_DIRBUF)
964 printf("ffs_make_dirbuf: growing buf to %d\n",
965 dbuf->size + DIRBLKSIZ);
966 if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL)
967 err(1, "Allocating memory for directory buffer");
969 dbuf->size += DIRBLKSIZ;
970 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
971 dbuf->cur = dbuf->size - DIRBLKSIZ;
972 } else { /* shrink end of previous */
973 dp->d_reclen = ufs_rw16(llen,needswap);
976 dp = (struct direct *)(dbuf->buf + dbuf->cur);
977 memcpy(dp, &de, reclen);
978 dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
982 * cribbed from sys/ufs/ffs/ffs_alloc.c
985 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
988 struct ufs1_dinode *dp1;
989 struct ufs2_dinode *dp2, *dip;
994 char sbbuf[FFS_MAXBSIZE];
999 assert (fsopts != NULL);
1001 fs = (struct fs *)fsopts->superblock;
1002 cg = ino_to_cg(fs, ino);
1003 cgino = ino % fs->fs_ipg;
1004 if (debug & DEBUG_FS_WRITE_INODE)
1005 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1006 dp, ino, cg, cgino);
1008 ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1010 cgp = (struct cg *)sbbuf;
1011 if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1012 errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1014 assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1016 buf = malloc(fs->fs_bsize);
1018 errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg);
1020 dp1 = (struct ufs1_dinode *)buf;
1021 dp2 = (struct ufs2_dinode *)buf;
1023 if (fs->fs_cstotal.cs_nifree == 0)
1024 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1026 if (fs->fs_cs(fs, cg).cs_nifree == 0)
1028 "ffs_write_inode: cg %d out of inodes for ino %u",
1030 setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1031 ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1032 fs->fs_cstotal.cs_nifree--;
1033 fs->fs_cs(fs, cg).cs_nifree--;
1034 if (S_ISDIR(DIP(dp, mode))) {
1035 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1036 fs->fs_cstotal.cs_ndir++;
1037 fs->fs_cs(fs, cg).cs_ndir++;
1041 * Initialize inode blocks on the fly for UFS2.
1043 initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1044 if (fsopts->version == 2 && cgino + INOPB(fs) > initediblk &&
1045 initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1046 memset(buf, 0, fs->fs_bsize);
1047 dip = (struct ufs2_dinode *)buf;
1048 srandom(time(NULL));
1049 for (i = 0; i < INOPB(fs); i++) {
1050 dip->di_gen = random() / 2 + 1;
1053 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1054 cg * fs->fs_ipg + initediblk)),
1055 fs->fs_bsize, buf, fsopts);
1056 initediblk += INOPB(fs);
1057 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1061 ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1064 /* now write inode */
1065 d = fsbtodb(fs, ino_to_fsba(fs, ino));
1066 ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1067 if (fsopts->needswap) {
1068 if (fsopts->version == 1)
1069 ffs_dinode1_swap(&dp->ffs1_din,
1070 &dp1[ino_to_fsbo(fs, ino)]);
1072 ffs_dinode2_swap(&dp->ffs2_din,
1073 &dp2[ino_to_fsbo(fs, ino)]);
1075 if (fsopts->version == 1)
1076 dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1078 dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1080 ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1085 panic(const char *fmt, ...)