1 /* $NetBSD: ffs.c,v 1.43 2009/04/16 18:54:16 dyoung 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 #if HAVE_NBTOOL_CONFIG_H
69 #include "nbtool_config.h"
72 #include <sys/cdefs.h>
73 #if defined(__RCSID) && !defined(__lint)
74 __RCSID("$NetBSD: ffs.c,v 1.43 2009/04/16 18:54:16 dyoung Exp $");
77 #include <sys/param.h>
79 #if !HAVE_NBTOOL_CONFIG_H
80 #include <sys/mount.h>
95 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
96 #include <sys/statvfs.h>
99 #include <ufs/ufs/dinode.h>
100 #include <ufs/ufs/dir.h>
101 #include <ufs/ffs/fs.h>
102 #include <ufs/ufs/ufs_bswap.h>
104 #include "ffs/ufs_inode.h"
105 #include "ffs/newfs_extern.h"
106 #include "ffs/ffs_extern.h"
109 #define DIP(dp, field) \
110 ((ffs_opts->version == 1) ? \
111 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
114 * Various file system defaults (cribbed from newfs(8)).
116 #define DFL_FRAGSIZE 1024 /* fragment size */
117 #define DFL_BLKSIZE 8192 /* block size */
118 #define DFL_SECSIZE 512 /* sector size */
119 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */
120 #define DFL_FRAGSPERINODE 4 /* fragments per inode */
121 #define DFL_ROTDELAY 0 /* rotational delay */
122 #define DFL_NRPOS 1 /* rotational positions */
123 #define DFL_RPM 3600 /* rpm of disk */
124 #define DFL_NSECTORS 64 /* # of sectors */
125 #define DFL_NTRACKS 16 /* # of tracks */
129 u_char
*buf
; /* buf for directory */
130 doff_t size
; /* full size of buf */
131 doff_t cur
; /* offset of current entry */
135 static int ffs_create_image(const char *, fsinfo_t
*);
136 static void ffs_dump_fsinfo(fsinfo_t
*);
137 static void ffs_dump_dirbuf(dirbuf_t
*, const char *, int);
138 static void ffs_make_dirbuf(dirbuf_t
*, const char *, fsnode
*, int);
139 static int ffs_populate_dir(const char *, fsnode
*, fsinfo_t
*);
140 static void ffs_size_dir(fsnode
*, fsinfo_t
*);
141 static void ffs_validate(const char *, fsnode
*, fsinfo_t
*);
142 static void ffs_write_file(union dinode
*, uint32_t, void *, fsinfo_t
*);
143 static void ffs_write_inode(union dinode
*, uint32_t, const fsinfo_t
*);
144 static void *ffs_build_dinode1(struct ufs1_dinode
*, dirbuf_t
*, fsnode
*,
145 fsnode
*, fsinfo_t
*);
146 static void *ffs_build_dinode2(struct ufs2_dinode
*, dirbuf_t
*, fsnode
*,
147 fsnode
*, fsinfo_t
*);
151 int sectorsize
; /* XXX: for buf.c::getblk() */
153 /* publically visible functions */
156 ffs_prep_opts(fsinfo_t
*fsopts
)
160 if ((ffs_opts
= calloc(1, sizeof(ffs_opt_t
))) == NULL
)
161 err(1, "Allocating memory for ffs_options");
163 fsopts
->fs_specific
= ffs_opts
;
168 ffs_opts
->density
= -1;
169 ffs_opts
->minfree
= -1;
170 ffs_opts
->optimization
= -1;
171 ffs_opts
->maxcontig
= -1;
172 ffs_opts
->maxbpg
= -1;
173 ffs_opts
->avgfilesize
= -1;
174 ffs_opts
->avgfpdir
= -1;
175 ffs_opts
->version
= 1;
179 ffs_cleanup_opts(fsinfo_t
*fsopts
)
181 if (fsopts
->fs_specific
)
182 free(fsopts
->fs_specific
);
186 ffs_parse_opts(const char *option
, fsinfo_t
*fsopts
)
188 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
190 option_t ffs_options
[] = {
191 { "bsize", &ffs_opts
->bsize
, 1, INT_MAX
,
193 { "fsize", &ffs_opts
->fsize
, 1, INT_MAX
,
195 { "density", &ffs_opts
->density
, 1, INT_MAX
,
197 { "minfree", &ffs_opts
->minfree
, 0, 99,
199 { "maxbpf", &ffs_opts
->maxbpg
, 1, INT_MAX
,
200 "max blocks per file in a cg" },
201 { "avgfilesize", &ffs_opts
->avgfilesize
,1, INT_MAX
,
202 "expected average file size" },
203 { "avgfpdir", &ffs_opts
->avgfpdir
, 1, INT_MAX
,
204 "expected # of files per directory" },
205 { "extent", &ffs_opts
->maxbsize
, 1, INT_MAX
,
206 "maximum # extent size" },
207 { "maxbpcg", &ffs_opts
->maxblkspercg
,1, INT_MAX
,
208 "max # of blocks per group" },
209 { "version", &ffs_opts
->version
, 1, 2,
217 assert(option
!= NULL
);
218 assert(fsopts
!= NULL
);
219 assert(ffs_opts
!= NULL
);
221 if (debug
& DEBUG_FS_PARSE_OPTS
)
222 printf("ffs_parse_opts: got `%s'\n", option
);
224 if ((var
= strdup(option
)) == NULL
)
225 err(1, "Allocating memory for copy of option string");
228 if ((val
= strchr(var
, '=')) == NULL
) {
229 warnx("Option `%s' doesn't contain a value", var
);
230 goto leave_ffs_parse_opts
;
234 if (strcmp(var
, "optimization") == 0) {
235 if (strcmp(val
, "time") == 0) {
236 ffs_opts
->optimization
= FS_OPTTIME
;
237 } else if (strcmp(val
, "space") == 0) {
238 ffs_opts
->optimization
= FS_OPTSPACE
;
240 warnx("Invalid optimization `%s'", val
);
241 goto leave_ffs_parse_opts
;
245 rv
= set_option(ffs_options
, var
, val
);
247 leave_ffs_parse_opts
:
255 ffs_makefs(const char *image
, const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
257 struct fs
*superblock
;
258 struct timeval start
;
260 assert(image
!= NULL
);
262 assert(root
!= NULL
);
263 assert(fsopts
!= NULL
);
265 if (debug
& DEBUG_FS_MAKEFS
)
266 printf("ffs_makefs: image %s directory %s root %p\n",
269 /* validate tree and options */
271 ffs_validate(dir
, root
, fsopts
);
272 TIMER_RESULTS(start
, "ffs_validate");
274 printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
275 image
, (long long)fsopts
->size
, (long long)fsopts
->inodes
);
279 if (ffs_create_image(image
, fsopts
) == -1)
280 errx(1, "Image file `%s' not created.", image
);
281 TIMER_RESULTS(start
, "ffs_create_image");
283 fsopts
->curinode
= ROOTINO
;
285 if (debug
& DEBUG_FS_MAKEFS
)
289 printf("Populating `%s'\n", image
);
291 if (! ffs_populate_dir(dir
, root
, fsopts
))
292 errx(1, "Image file `%s' not populated.", image
);
293 TIMER_RESULTS(start
, "ffs_populate_dir");
295 /* ensure no outstanding buffers remain */
296 if (debug
& DEBUG_FS_MAKEFS
)
299 /* update various superblock parameters */
300 superblock
= fsopts
->superblock
;
301 superblock
->fs_fmod
= 0;
302 superblock
->fs_old_cstotal
.cs_ndir
= superblock
->fs_cstotal
.cs_ndir
;
303 superblock
->fs_old_cstotal
.cs_nbfree
= superblock
->fs_cstotal
.cs_nbfree
;
304 superblock
->fs_old_cstotal
.cs_nifree
= superblock
->fs_cstotal
.cs_nifree
;
305 superblock
->fs_old_cstotal
.cs_nffree
= superblock
->fs_cstotal
.cs_nffree
;
307 /* write out superblock; image is now complete */
308 ffs_write_superblock(fsopts
->superblock
, fsopts
);
309 if (close(fsopts
->fd
) == -1)
310 err(1, "Closing `%s'", image
);
312 printf("Image `%s' complete\n", image
);
315 /* end of public functions */
319 ffs_validate(const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
323 int32_t spc
, nspf
, ncyl
, fssize
;
325 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
328 assert(root
!= NULL
);
329 assert(fsopts
!= NULL
);
330 assert(ffs_opts
!= NULL
);
332 if (debug
& DEBUG_FS_VALIDATE
) {
333 printf("ffs_validate: before defaults set:\n");
334 ffs_dump_fsinfo(fsopts
);
337 /* set FFS defaults */
338 if (fsopts
->sectorsize
== -1)
339 fsopts
->sectorsize
= DFL_SECSIZE
;
340 if (ffs_opts
->fsize
== -1)
341 ffs_opts
->fsize
= MAX(DFL_FRAGSIZE
, fsopts
->sectorsize
);
342 if (ffs_opts
->bsize
== -1)
343 ffs_opts
->bsize
= MIN(DFL_BLKSIZE
, 8 * ffs_opts
->fsize
);
344 if (ffs_opts
->cpg
== -1)
345 ffs_opts
->cpg
= DFL_CYLSPERGROUP
;
347 ffs_opts
->cpgflg
= 1;
348 /* fsopts->density is set below */
349 if (ffs_opts
->nsectors
== -1)
350 ffs_opts
->nsectors
= DFL_NSECTORS
;
351 if (ffs_opts
->minfree
== -1)
352 ffs_opts
->minfree
= MINFREE
;
353 if (ffs_opts
->optimization
== -1)
354 ffs_opts
->optimization
= DEFAULTOPT
;
355 if (ffs_opts
->maxcontig
== -1)
356 ffs_opts
->maxcontig
=
357 MAX(1, MIN(MAXPHYS
, FFS_MAXBSIZE
) / ffs_opts
->bsize
);
359 if (ffs_opts
->maxbpg
== -1)
360 ffs_opts
->maxbpg
= ffs_opts
->bsize
/ sizeof(int32_t);
361 if (ffs_opts
->avgfilesize
== -1)
362 ffs_opts
->avgfilesize
= AVFILESIZ
;
363 if (ffs_opts
->avgfpdir
== -1)
364 ffs_opts
->avgfpdir
= AFPDIR
;
366 /* calculate size of tree */
367 ffs_size_dir(root
, fsopts
);
368 fsopts
->inodes
+= ROOTINO
; /* include first two inodes */
370 if (debug
& DEBUG_FS_VALIDATE
)
371 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
372 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
374 /* add requested slop */
375 fsopts
->size
+= fsopts
->freeblocks
;
376 fsopts
->inodes
+= fsopts
->freefiles
;
377 if (fsopts
->freefilepc
> 0)
379 fsopts
->inodes
* (100 + fsopts
->freefilepc
) / 100;
380 if (fsopts
->freeblockpc
> 0)
382 fsopts
->size
* (100 + fsopts
->freeblockpc
) / 100;
384 /* add space needed for superblocks */
386 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
387 * typically used for small filesystems where space matters.
388 * XXX make this an option.
390 fsopts
->size
+= (SBLOCK_UFS1
+ SBLOCKSIZE
) * ncg
;
391 /* add space needed to store inodes, x3 for blockmaps, etc */
392 if (ffs_opts
->version
== 1)
393 fsopts
->size
+= ncg
* DINODE1_SIZE
*
394 roundup(fsopts
->inodes
/ ncg
,
395 ffs_opts
->bsize
/ DINODE1_SIZE
);
397 fsopts
->size
+= ncg
* DINODE2_SIZE
*
398 roundup(fsopts
->inodes
/ ncg
,
399 ffs_opts
->bsize
/ DINODE2_SIZE
);
402 if (ffs_opts
->minfree
> 0)
404 fsopts
->size
* (100 + ffs_opts
->minfree
) / 100;
406 * XXX any other fs slop to add, such as csum's, bitmaps, etc ??
409 if (fsopts
->size
< fsopts
->minsize
) /* ensure meets minimum size */
410 fsopts
->size
= fsopts
->minsize
;
412 /* round up to the next block */
413 fsopts
->size
= roundup(fsopts
->size
, ffs_opts
->bsize
);
415 /* calculate density if necessary */
416 if (ffs_opts
->density
== -1)
417 ffs_opts
->density
= fsopts
->size
/ fsopts
->inodes
+ 1;
419 if (debug
& DEBUG_FS_VALIDATE
) {
420 printf("ffs_validate: after defaults set:\n");
421 ffs_dump_fsinfo(fsopts
);
422 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
423 dir
, (long long)fsopts
->size
, (long long)fsopts
->inodes
);
425 sectorsize
= fsopts
->sectorsize
; /* XXX - see earlier */
427 /* now check calculated sizes vs requested sizes */
428 if (fsopts
->maxsize
> 0 && fsopts
->size
> fsopts
->maxsize
) {
429 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
430 dir
, (long long)fsopts
->size
, (long long)fsopts
->maxsize
);
436 ffs_dump_fsinfo(fsinfo_t
*f
)
439 ffs_opt_t
*fs
= f
->fs_specific
;
441 printf("fsopts at %p\n", f
);
443 printf("\tsize %lld, inodes %lld, curinode %u\n",
444 (long long)f
->size
, (long long)f
->inodes
, f
->curinode
);
446 printf("\tminsize %lld, maxsize %lld\n",
447 (long long)f
->minsize
, (long long)f
->maxsize
);
448 printf("\tfree files %lld, freefile %% %d\n",
449 (long long)f
->freefiles
, f
->freefilepc
);
450 printf("\tfree blocks %lld, freeblock %% %d\n",
451 (long long)f
->freeblocks
, f
->freeblockpc
);
452 printf("\tneedswap %d, sectorsize %d\n", f
->needswap
, f
->sectorsize
);
454 printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
455 fs
->bsize
, fs
->fsize
, fs
->cpg
, fs
->density
);
456 printf("\tnsectors %d, rpm %d, minfree %d\n",
457 fs
->nsectors
, fs
->rpm
, fs
->minfree
);
458 printf("\tmaxcontig %d, maxbpg %d\n",
459 fs
->maxcontig
, fs
->maxbpg
);
460 printf("\toptimization %s\n",
461 fs
->optimization
== FS_OPTSPACE
? "space" : "time");
466 ffs_create_image(const char *image
, fsinfo_t
*fsopts
)
468 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
476 assert (image
!= NULL
);
477 assert (fsopts
!= NULL
);
480 if ((fsopts
->fd
= open(image
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666))
482 warn("Can't open `%s' for writing", image
);
487 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
488 if (fstatvfs(fsopts
->fd
, &sfs
) == -1) {
491 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
492 warn("can't fstatvfs `%s', using default %d byte chunk",
495 bufsize
= sfs
.f_iosize
;
497 bufrem
= fsopts
->size
;
498 if (debug
& DEBUG_FS_CREATE_IMAGE
)
500 "zero-ing image `%s', %lld sectors, using %d byte chunks\n",
501 image
, (long long)bufrem
, bufsize
);
502 if ((buf
= calloc(1, bufsize
)) == NULL
) {
503 warn("Can't create buffer for sector");
507 i
= write(fsopts
->fd
, buf
, MIN(bufsize
, bufrem
));
509 warn("zeroing image, %lld bytes to go",
518 /* make the file system */
519 if (debug
& DEBUG_FS_CREATE_IMAGE
)
520 printf("calling mkfs(\"%s\", ...)\n", image
);
521 fs
= ffs_mkfs(image
, fsopts
);
522 fsopts
->superblock
= (void *)fs
;
523 if (debug
& DEBUG_FS_CREATE_IMAGE
) {
526 t
= (time_t)((struct fs
*)fsopts
->superblock
)->fs_time
;
527 printf("mkfs returned %p; fs_time %s",
528 fsopts
->superblock
, ctime(&t
));
529 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
530 (long long)fs
->fs_cstotal
.cs_nbfree
,
531 (long long)fs
->fs_cstotal
.cs_nffree
,
532 (long long)fs
->fs_cstotal
.cs_nifree
,
533 (long long)fs
->fs_cstotal
.cs_ndir
);
536 if (fs
->fs_cstotal
.cs_nifree
+ ROOTINO
< fsopts
->inodes
) {
538 "Image file `%s' has %lld free inodes; %lld are required.",
540 (long long)(fs
->fs_cstotal
.cs_nifree
+ ROOTINO
),
541 (long long)fsopts
->inodes
);
549 ffs_size_dir(fsnode
*root
, fsinfo_t
*fsopts
)
551 struct direct tmpdir
;
553 int curdirsize
, this;
554 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
556 /* node may be NULL (empty directory) */
557 assert(fsopts
!= NULL
);
558 assert(ffs_opts
!= NULL
);
560 if (debug
& DEBUG_FS_SIZE_DIR
)
561 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
562 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
564 #define ADDDIRENT(e) do { \
565 tmpdir.d_namlen = strlen((e)); \
566 this = DIRSIZ(0, &tmpdir, 0); \
567 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
568 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \
569 e, tmpdir.d_namlen, this, curdirsize); \
570 if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ)) \
571 curdirsize = roundup(curdirsize, DIRBLKSIZ); \
572 curdirsize += this; \
573 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
574 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \
575 e, tmpdir.d_namlen, this, curdirsize); \
579 * XXX this needs to take into account extra space consumed
580 * by indirect blocks, etc.
582 #define ADDSIZE(x) do { \
583 fsopts->size += roundup((x), ffs_opts->fsize); \
587 for (node
= root
; node
!= NULL
; node
= node
->next
) {
588 ADDDIRENT(node
->name
);
589 if (node
== root
) { /* we're at "." */
590 assert(strcmp(node
->name
, ".") == 0);
592 } else if ((node
->inode
->flags
& FI_SIZED
) == 0) {
593 /* don't count duplicate names */
594 node
->inode
->flags
|= FI_SIZED
;
595 if (debug
& DEBUG_FS_SIZE_DIR_NODE
)
596 printf("ffs_size_dir: `%s' size %lld\n",
598 (long long)node
->inode
->st
.st_size
);
600 if (node
->type
== S_IFREG
)
601 ADDSIZE(node
->inode
->st
.st_size
);
602 if (node
->type
== S_IFLNK
) {
605 slen
= strlen(node
->symlink
) + 1;
606 if (slen
>= (ffs_opts
->version
== 1 ?
612 if (node
->type
== S_IFDIR
)
613 ffs_size_dir(node
->child
, fsopts
);
617 if (debug
& DEBUG_FS_SIZE_DIR
)
618 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
619 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
623 ffs_build_dinode1(struct ufs1_dinode
*dinp
, dirbuf_t
*dbufp
, fsnode
*cur
,
624 fsnode
*root
, fsinfo_t
*fsopts
)
629 memset(dinp
, 0, sizeof(*dinp
));
630 dinp
->di_mode
= cur
->inode
->st
.st_mode
;
631 dinp
->di_nlink
= cur
->inode
->nlink
;
632 dinp
->di_size
= cur
->inode
->st
.st_size
;
633 dinp
->di_atime
= cur
->inode
->st
.st_atime
;
634 dinp
->di_mtime
= cur
->inode
->st
.st_mtime
;
635 dinp
->di_ctime
= cur
->inode
->st
.st_ctime
;
636 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
637 dinp
->di_atimensec
= cur
->inode
->st
.st_atimensec
;
638 dinp
->di_mtimensec
= cur
->inode
->st
.st_mtimensec
;
639 dinp
->di_ctimensec
= cur
->inode
->st
.st_ctimensec
;
641 #if HAVE_STRUCT_STAT_ST_FLAGS
642 dinp
->di_flags
= cur
->inode
->st
.st_flags
;
644 #if HAVE_STRUCT_STAT_ST_GEN
645 dinp
->di_gen
= cur
->inode
->st
.st_gen
;
647 dinp
->di_uid
= cur
->inode
->st
.st_uid
;
648 dinp
->di_gid
= cur
->inode
->st
.st_gid
;
649 /* not set: di_db, di_ib, di_blocks, di_spare */
652 if (cur
== root
) { /* "."; write dirbuf */
654 dinp
->di_size
= dbufp
->size
;
655 } else if (S_ISBLK(cur
->type
) || S_ISCHR(cur
->type
)) {
656 dinp
->di_size
= 0; /* a device */
658 ufs_rw32(cur
->inode
->st
.st_rdev
, fsopts
->needswap
);
659 } else if (S_ISLNK(cur
->type
)) { /* symlink */
660 slen
= strlen(cur
->symlink
);
661 if (slen
< MAXSYMLINKLEN_UFS1
) { /* short link */
662 memcpy(dinp
->di_db
, cur
->symlink
, slen
);
664 membuf
= cur
->symlink
;
665 dinp
->di_size
= slen
;
671 ffs_build_dinode2(struct ufs2_dinode
*dinp
, dirbuf_t
*dbufp
, fsnode
*cur
,
672 fsnode
*root
, fsinfo_t
*fsopts
)
677 memset(dinp
, 0, sizeof(*dinp
));
678 dinp
->di_mode
= cur
->inode
->st
.st_mode
;
679 dinp
->di_nlink
= cur
->inode
->nlink
;
680 dinp
->di_size
= cur
->inode
->st
.st_size
;
681 dinp
->di_atime
= cur
->inode
->st
.st_atime
;
682 dinp
->di_mtime
= cur
->inode
->st
.st_mtime
;
683 dinp
->di_ctime
= cur
->inode
->st
.st_ctime
;
684 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
685 dinp
->di_atimensec
= cur
->inode
->st
.st_atimensec
;
686 dinp
->di_mtimensec
= cur
->inode
->st
.st_mtimensec
;
687 dinp
->di_ctimensec
= cur
->inode
->st
.st_ctimensec
;
689 #if HAVE_STRUCT_STAT_ST_FLAGS
690 dinp
->di_flags
= cur
->inode
->st
.st_flags
;
692 #if HAVE_STRUCT_STAT_ST_GEN
693 dinp
->di_gen
= cur
->inode
->st
.st_gen
;
695 #if HAVE_STRUCT_STAT_BIRTHTIME
696 dinp
->di_birthtime
= cur
->inode
->st
.st_birthtime
;
697 dinp
->di_birthnsec
= cur
->inode
->st
.st_birthtimensec
;
699 dinp
->di_uid
= cur
->inode
->st
.st_uid
;
700 dinp
->di_gid
= cur
->inode
->st
.st_gid
;
701 /* not set: di_db, di_ib, di_blocks, di_spare */
704 if (cur
== root
) { /* "."; write dirbuf */
706 dinp
->di_size
= dbufp
->size
;
707 } else if (S_ISBLK(cur
->type
) || S_ISCHR(cur
->type
)) {
708 dinp
->di_size
= 0; /* a device */
710 ufs_rw64(cur
->inode
->st
.st_rdev
, fsopts
->needswap
);
711 } else if (S_ISLNK(cur
->type
)) { /* symlink */
712 slen
= strlen(cur
->symlink
);
713 if (slen
< MAXSYMLINKLEN_UFS2
) { /* short link */
714 memcpy(dinp
->di_db
, cur
->symlink
, slen
);
716 membuf
= cur
->symlink
;
717 dinp
->di_size
= slen
;
723 ffs_populate_dir(const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
729 char path
[MAXPATHLEN
+ 1];
730 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
733 assert(root
!= NULL
);
734 assert(fsopts
!= NULL
);
735 assert(ffs_opts
!= NULL
);
737 (void)memset(&dirbuf
, 0, sizeof(dirbuf
));
739 if (debug
& DEBUG_FS_POPULATE
)
740 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir
, root
);
743 * pass 1: allocate inode numbers, build directory `file'
745 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
746 if ((cur
->inode
->flags
& FI_ALLOCATED
) == 0) {
747 cur
->inode
->flags
|= FI_ALLOCATED
;
748 if (cur
== root
&& cur
->parent
!= NULL
)
749 cur
->inode
->ino
= cur
->parent
->inode
->ino
;
751 cur
->inode
->ino
= fsopts
->curinode
;
755 ffs_make_dirbuf(&dirbuf
, cur
->name
, cur
, fsopts
->needswap
);
756 if (cur
== root
) { /* we're at "."; add ".." */
757 ffs_make_dirbuf(&dirbuf
, "..",
758 cur
->parent
== NULL
? cur
: cur
->parent
->first
,
760 root
->inode
->nlink
++; /* count my parent's link */
761 } else if (cur
->child
!= NULL
)
762 root
->inode
->nlink
++; /* count my child's link */
765 * XXX possibly write file and long symlinks here,
766 * ensuring that blocks get written before inodes?
767 * otoh, this isn't a real filesystem, so who
768 * cares about ordering? :-)
771 if (debug
& DEBUG_FS_POPULATE_DIRBUF
)
772 ffs_dump_dirbuf(&dirbuf
, dir
, fsopts
->needswap
);
775 * pass 2: write out dirbuf, then non-directories at this level
777 if (debug
& DEBUG_FS_POPULATE
)
778 printf("ffs_populate_dir: PASS 2 dir %s\n", dir
);
779 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
780 if (cur
->inode
->flags
& FI_WRITTEN
)
781 continue; /* skip hard-linked entries */
782 cur
->inode
->flags
|= FI_WRITTEN
;
784 if (snprintf(path
, sizeof(path
), "%s/%s", dir
, cur
->name
)
786 errx(1, "Pathname too long.");
788 if (cur
->child
!= NULL
)
789 continue; /* child creates own inode */
791 /* build on-disk inode */
792 if (ffs_opts
->version
== 1)
793 membuf
= ffs_build_dinode1(&din
.ffs1_din
, &dirbuf
, cur
,
796 membuf
= ffs_build_dinode2(&din
.ffs2_din
, &dirbuf
, cur
,
799 if (debug
& DEBUG_FS_POPULATE_NODE
) {
800 printf("ffs_populate_dir: writing ino %d, %s",
801 cur
->inode
->ino
, inode_type(cur
->type
));
802 if (cur
->inode
->nlink
> 1)
803 printf(", nlink %d", cur
->inode
->nlink
);
807 if (membuf
!= NULL
) {
808 ffs_write_file(&din
, cur
->inode
->ino
, membuf
, fsopts
);
809 } else if (S_ISREG(cur
->type
)) {
810 ffs_write_file(&din
, cur
->inode
->ino
, path
, fsopts
);
812 assert (! S_ISDIR(cur
->type
));
813 ffs_write_inode(&din
, cur
->inode
->ino
, fsopts
);
818 * pass 3: write out sub-directories
820 if (debug
& DEBUG_FS_POPULATE
)
821 printf("ffs_populate_dir: PASS 3 dir %s\n", dir
);
822 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
823 if (cur
->child
== NULL
)
825 if (snprintf(path
, sizeof(path
), "%s/%s", dir
, cur
->name
)
827 errx(1, "Pathname too long.");
828 if (! ffs_populate_dir(path
, cur
->child
, fsopts
))
832 if (debug
& DEBUG_FS_POPULATE
)
833 printf("ffs_populate_dir: DONE dir %s\n", dir
);
836 if (dirbuf
.buf
!= NULL
)
843 ffs_write_file(union dinode
*din
, uint32_t ino
, void *buf
, fsinfo_t
*fsopts
)
847 off_t bufleft
, chunk
, offset
;
851 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
853 assert (din
!= NULL
);
854 assert (buf
!= NULL
);
855 assert (fsopts
!= NULL
);
856 assert (ffs_opts
!= NULL
);
858 isfile
= S_ISREG(DIP(din
, mode
));
863 in
.i_fs
= (struct fs
*)fsopts
->superblock
;
865 if (debug
& DEBUG_FS_WRITE_FILE
) {
867 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
868 ino
, din
, isfile
, inode_type(DIP(din
, mode
) & S_IFMT
),
869 (long long)DIP(din
, size
));
871 printf(", file '%s'\n", (char *)buf
);
873 printf(", buffer %p\n", buf
);
877 in
.i_size
= DIP(din
, size
);
878 if (ffs_opts
->version
== 1)
879 memcpy(&in
.i_din
.ffs1_din
, &din
->ffs1_din
,
880 sizeof(in
.i_din
.ffs1_din
));
882 memcpy(&in
.i_din
.ffs2_din
, &din
->ffs2_din
,
883 sizeof(in
.i_din
.ffs2_din
));
884 in
.i_fd
= fsopts
->fd
;
886 if (DIP(din
, size
) == 0)
887 goto write_inode_and_leave
; /* mmm, cheating */
890 if ((fbuf
= malloc(ffs_opts
->bsize
)) == NULL
)
891 err(1, "Allocating memory for write buffer");
892 if ((ffd
= open((char *)buf
, O_RDONLY
, 0444)) == -1) {
893 warn("Can't open `%s' for reading", (char *)buf
);
894 goto leave_ffs_write_file
;
901 for (bufleft
= DIP(din
, size
); bufleft
> 0; bufleft
-= chunk
) {
902 chunk
= MIN(bufleft
, ffs_opts
->bsize
);
905 else if ((nread
= read(ffd
, fbuf
, chunk
)) == -1)
906 err(EXIT_FAILURE
, "Reading `%s', %lld bytes to go",
907 (char *)buf
, (long long)bufleft
);
908 else if (nread
!= chunk
)
909 errx(EXIT_FAILURE
, "Reading `%s', %lld bytes to go, "
910 "read %zd bytes, expected %ju bytes, does "
911 "metalog size= attribute mismatch source size?",
912 (char *)buf
, (long long)bufleft
, nread
,
916 offset
= DIP(din
, size
) - bufleft
;
917 if (debug
& DEBUG_FS_WRITE_FILE_BLOCK
)
919 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
920 p
, (long long)offset
,
921 (long long)chunk
, (long long)bufleft
);
923 * XXX if holey support is desired, do the check here
925 * XXX might need to write out last bit in fragroundup
926 * sized chunk. however, ffs_balloc() handles this for us
928 errno
= ffs_balloc(&in
, offset
, chunk
, &bp
);
932 "Writing inode %d (%s), bytes %lld + %lld",
934 isfile
? (char *)buf
:
935 inode_type(DIP(din
, mode
) & S_IFMT
),
936 (long long)offset
, (long long)chunk
);
937 memcpy(bp
->b_data
, p
, chunk
);
940 goto bad_ffs_write_file
;
946 write_inode_and_leave
:
947 ffs_write_inode(&in
.i_din
, in
.i_number
, fsopts
);
949 leave_ffs_write_file
:
958 ffs_dump_dirbuf(dirbuf_t
*dbuf
, const char *dir
, int needswap
)
964 assert (dbuf
!= NULL
);
965 assert (dir
!= NULL
);
966 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
967 dir
, dbuf
->size
, dbuf
->cur
);
969 for (i
= 0; i
< dbuf
->size
; ) {
970 de
= (struct direct
*)(dbuf
->buf
+ i
);
971 reclen
= ufs_rw16(de
->d_reclen
, needswap
);
973 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
974 ufs_rw32(de
->d_fileno
, needswap
),
975 inode_type(DTTOIF(de
->d_type
)), i
, reclen
,
976 de
->d_namlen
, de
->d_name
);
983 ffs_make_dirbuf(dirbuf_t
*dbuf
, const char *name
, fsnode
*node
, int needswap
)
985 struct direct de
, *dp
;
986 uint16_t llen
, reclen
;
989 assert (dbuf
!= NULL
);
990 assert (name
!= NULL
);
991 assert (node
!= NULL
);
992 /* create direct entry */
993 (void)memset(&de
, 0, sizeof(de
));
994 de
.d_fileno
= ufs_rw32(node
->inode
->ino
, needswap
);
995 de
.d_type
= IFTODT(node
->type
);
996 de
.d_namlen
= (uint8_t)strlen(name
);
997 strcpy(de
.d_name
, name
);
998 reclen
= DIRSIZ(0, &de
, needswap
);
999 de
.d_reclen
= ufs_rw16(reclen
, needswap
);
1001 dp
= (struct direct
*)(dbuf
->buf
+ dbuf
->cur
);
1004 llen
= DIRSIZ(0, dp
, needswap
);
1006 if (debug
& DEBUG_FS_MAKE_DIRBUF
)
1008 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1009 " ino %d type %d reclen %d namlen %d name %.30s\n",
1010 dbuf
->size
, dbuf
->cur
, llen
,
1011 ufs_rw32(de
.d_fileno
, needswap
), de
.d_type
, reclen
,
1012 de
.d_namlen
, de
.d_name
);
1014 if (reclen
+ dbuf
->cur
+ llen
> roundup(dbuf
->size
, DIRBLKSIZ
)) {
1015 if (debug
& DEBUG_FS_MAKE_DIRBUF
)
1016 printf("ffs_make_dirbuf: growing buf to %d\n",
1017 dbuf
->size
+ DIRBLKSIZ
);
1018 if ((newbuf
= realloc(dbuf
->buf
, dbuf
->size
+ DIRBLKSIZ
)) == NULL
)
1019 err(1, "Allocating memory for directory buffer");
1021 dbuf
->size
+= DIRBLKSIZ
;
1022 memset(dbuf
->buf
+ dbuf
->size
- DIRBLKSIZ
, 0, DIRBLKSIZ
);
1023 dbuf
->cur
= dbuf
->size
- DIRBLKSIZ
;
1024 } else if (dp
) { /* shrink end of previous */
1025 dp
->d_reclen
= ufs_rw16(llen
,needswap
);
1028 dp
= (struct direct
*)(dbuf
->buf
+ dbuf
->cur
);
1029 memcpy(dp
, &de
, reclen
);
1030 dp
->d_reclen
= ufs_rw16(dbuf
->size
- dbuf
->cur
, needswap
);
1034 * cribbed from sys/ufs/ffs/ffs_alloc.c
1037 ffs_write_inode(union dinode
*dp
, uint32_t ino
, const fsinfo_t
*fsopts
)
1040 struct ufs1_dinode
*dp1
;
1041 struct ufs2_dinode
*dp2
, *dip
;
1046 char sbbuf
[FFS_MAXBSIZE
];
1048 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
1050 assert (dp
!= NULL
);
1052 assert (fsopts
!= NULL
);
1053 assert (ffs_opts
!= NULL
);
1055 fs
= (struct fs
*)fsopts
->superblock
;
1056 cg
= ino_to_cg(fs
, ino
);
1057 cgino
= ino
% fs
->fs_ipg
;
1058 if (debug
& DEBUG_FS_WRITE_INODE
)
1059 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1060 dp
, ino
, cg
, cgino
);
1062 ffs_rdfs(fsbtodb(fs
, cgtod(fs
, cg
)), (int)fs
->fs_cgsize
, &sbbuf
,
1064 cgp
= (struct cg
*)sbbuf
;
1065 if (!cg_chkmagic(cgp
, fsopts
->needswap
))
1066 errx(1, "ffs_write_inode: cg %d: bad magic number", cg
);
1068 assert (isclr(cg_inosused(cgp
, fsopts
->needswap
), cgino
));
1070 buf
= malloc(fs
->fs_bsize
);
1072 errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg
);
1074 dp1
= (struct ufs1_dinode
*)buf
;
1075 dp2
= (struct ufs2_dinode
*)buf
;
1077 if (fs
->fs_cstotal
.cs_nifree
== 0)
1078 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1080 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1082 "ffs_write_inode: cg %d out of inodes for ino %u",
1084 setbit(cg_inosused(cgp
, fsopts
->needswap
), cgino
);
1085 ufs_add32(cgp
->cg_cs
.cs_nifree
, -1, fsopts
->needswap
);
1086 fs
->fs_cstotal
.cs_nifree
--;
1087 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1088 if (S_ISDIR(DIP(dp
, mode
))) {
1089 ufs_add32(cgp
->cg_cs
.cs_ndir
, 1, fsopts
->needswap
);
1090 fs
->fs_cstotal
.cs_ndir
++;
1091 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1095 * Initialize inode blocks on the fly for UFS2.
1097 initediblk
= ufs_rw32(cgp
->cg_initediblk
, fsopts
->needswap
);
1098 if (ffs_opts
->version
== 2 && cgino
+ INOPB(fs
) > initediblk
&&
1099 initediblk
< ufs_rw32(cgp
->cg_niblk
, fsopts
->needswap
)) {
1100 memset(buf
, 0, fs
->fs_bsize
);
1101 dip
= (struct ufs2_dinode
*)buf
;
1102 srandom(time(NULL
));
1103 for (i
= 0; i
< INOPB(fs
); i
++) {
1104 dip
->di_gen
= random() / 2 + 1;
1107 ffs_wtfs(fsbtodb(fs
, ino_to_fsba(fs
,
1108 cg
* fs
->fs_ipg
+ initediblk
)),
1109 fs
->fs_bsize
, buf
, fsopts
);
1110 initediblk
+= INOPB(fs
);
1111 cgp
->cg_initediblk
= ufs_rw32(initediblk
, fsopts
->needswap
);
1115 ffs_wtfs(fsbtodb(fs
, cgtod(fs
, cg
)), (int)fs
->fs_cgsize
, &sbbuf
,
1118 /* now write inode */
1119 d
= fsbtodb(fs
, ino_to_fsba(fs
, ino
));
1120 ffs_rdfs(d
, fs
->fs_bsize
, buf
, fsopts
);
1121 if (fsopts
->needswap
) {
1122 if (ffs_opts
->version
== 1)
1123 ffs_dinode1_swap(&dp
->ffs1_din
,
1124 &dp1
[ino_to_fsbo(fs
, ino
)]);
1126 ffs_dinode2_swap(&dp
->ffs2_din
,
1127 &dp2
[ino_to_fsbo(fs
, ino
)]);
1129 if (ffs_opts
->version
== 1)
1130 dp1
[ino_to_fsbo(fs
, ino
)] = dp
->ffs1_din
;
1132 dp2
[ino_to_fsbo(fs
, ino
)] = dp
->ffs2_din
;
1134 ffs_wtfs(d
, fs
->fs_bsize
, buf
, fsopts
);
1139 panic(const char *fmt
, ...)