1 /* $NetBSD: ffs.c,v 1.64 2015/01/12 19:50:25 christos Exp $ */
4 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1982, 1986, 1989, 1993
39 * The Regents of the University of California. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
68 #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.64 2015/01/12 19:50:25 christos Exp $");
77 #include <sys/param.h>
79 #if !HAVE_NBTOOL_CONFIG_H
80 #include <sys/mount.h>
96 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
97 #include <sys/statvfs.h>
100 #include <ufs/ufs/dinode.h>
101 #include <ufs/ufs/dir.h>
102 #include <ufs/ffs/fs.h>
103 #include <ufs/ufs/ufs_bswap.h>
105 #include "ffs/ufs_inode.h"
106 #include "ffs/newfs_extern.h"
107 #include "ffs/ffs_extern.h"
110 #define DIP(dp, field) \
111 ((ffs_opts->version == 1) ? \
112 (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
115 * Various file system defaults (cribbed from newfs(8)).
117 #define DFL_FRAGSIZE 1024 /* fragment size */
118 #define DFL_BLKSIZE 8192 /* block size */
119 #define DFL_SECSIZE 512 /* sector size */
120 #define DFL_CYLSPERGROUP 65536 /* cylinders per group */
121 #define DFL_FRAGSPERINODE 4 /* fragments per inode */
122 #define DFL_ROTDELAY 0 /* rotational delay */
123 #define DFL_NRPOS 1 /* rotational positions */
124 #define DFL_RPM 3600 /* rpm of disk */
125 #define DFL_NSECTORS 64 /* # of sectors */
126 #define DFL_NTRACKS 16 /* # of tracks */
130 u_char
*buf
; /* buf for directory */
131 doff_t size
; /* full size of buf */
132 doff_t cur
; /* offset of current entry */
136 static int ffs_create_image(const char *, fsinfo_t
*);
137 static void ffs_dump_fsinfo(fsinfo_t
*);
138 static void ffs_dump_dirbuf(dirbuf_t
*, const char *, int);
139 static void ffs_make_dirbuf(dirbuf_t
*, const char *, fsnode
*, int);
140 static int ffs_populate_dir(const char *, fsnode
*, fsinfo_t
*);
141 static void ffs_size_dir(fsnode
*, fsinfo_t
*);
142 static void ffs_validate(const char *, fsnode
*, fsinfo_t
*);
143 static void ffs_write_file(union dinode
*, uint32_t, void *, fsinfo_t
*);
144 static void ffs_write_inode(union dinode
*, uint32_t, const fsinfo_t
*);
145 static void *ffs_build_dinode1(struct ufs1_dinode
*, dirbuf_t
*, fsnode
*,
146 fsnode
*, fsinfo_t
*);
147 static void *ffs_build_dinode2(struct ufs2_dinode
*, dirbuf_t
*, fsnode
*,
148 fsnode
*, fsinfo_t
*);
152 /* publically visible functions */
154 ffs_prep_opts(fsinfo_t
*fsopts
)
156 ffs_opt_t
*ffs_opts
= ecalloc(1, sizeof(*ffs_opts
));
158 const option_t ffs_options
[] = {
159 { 'b', "bsize", &ffs_opts
->bsize
, OPT_INT32
,
160 1, INT_MAX
, "block size" },
161 { 'f', "fsize", &ffs_opts
->fsize
, OPT_INT32
,
162 1, INT_MAX
, "fragment size" },
163 { 'd', "density", &ffs_opts
->density
, OPT_INT32
,
164 1, INT_MAX
, "bytes per inode" },
165 { 'm', "minfree", &ffs_opts
->minfree
, OPT_INT32
,
167 { 'M', "maxbpg", &ffs_opts
->maxbpg
, OPT_INT32
,
168 1, INT_MAX
, "max blocks per file in a cg" },
169 { 'a', "avgfilesize", &ffs_opts
->avgfilesize
, OPT_INT32
,
170 1, INT_MAX
, "expected average file size" },
171 { 'n', "avgfpdir", &ffs_opts
->avgfpdir
, OPT_INT32
,
172 1, INT_MAX
, "expected # of files per directory" },
173 { 'x', "extent", &ffs_opts
->maxbsize
, OPT_INT32
,
174 1, INT_MAX
, "maximum # extent size" },
175 { 'g', "maxbpcg", &ffs_opts
->maxblkspercg
, OPT_INT32
,
176 1, INT_MAX
, "max # of blocks per group" },
177 { 'v', "version", &ffs_opts
->version
, OPT_INT32
,
178 1, 2, "UFS version" },
179 { 'o', "optimization", NULL
, OPT_STRBUF
,
180 0, 0, "Optimization (time|space)" },
181 { 'l', "label", ffs_opts
->label
, OPT_STRARRAY
,
182 1, sizeof(ffs_opts
->label
), "UFS label" },
189 ffs_opts
->density
= -1;
190 ffs_opts
->minfree
= -1;
191 ffs_opts
->optimization
= -1;
192 ffs_opts
->maxcontig
= -1;
193 ffs_opts
->maxbpg
= -1;
194 ffs_opts
->avgfilesize
= -1;
195 ffs_opts
->avgfpdir
= -1;
196 ffs_opts
->version
= 1;
198 fsopts
->fs_specific
= ffs_opts
;
199 fsopts
->fs_options
= copy_opts(ffs_options
);
203 ffs_cleanup_opts(fsinfo_t
*fsopts
)
205 free(fsopts
->fs_specific
);
206 free(fsopts
->fs_options
);
210 ffs_parse_opts(const char *option
, fsinfo_t
*fsopts
)
212 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
213 option_t
*ffs_options
= fsopts
->fs_options
;
218 assert(option
!= NULL
);
219 assert(fsopts
!= NULL
);
220 assert(ffs_opts
!= NULL
);
222 if (debug
& DEBUG_FS_PARSE_OPTS
)
223 printf("ffs_parse_opts: got `%s'\n", option
);
225 rv
= set_option(ffs_options
, option
, buf
, sizeof(buf
));
229 if (ffs_options
[rv
].name
== NULL
)
232 switch (ffs_options
[rv
].letter
) {
234 if (strcmp(buf
, "time") == 0) {
235 ffs_opts
->optimization
= FS_OPTTIME
;
236 } else if (strcmp(buf
, "space") == 0) {
237 ffs_opts
->optimization
= FS_OPTSPACE
;
239 warnx("Invalid optimization `%s'", buf
);
251 ffs_makefs(const char *image
, const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
253 struct fs
*superblock
;
254 struct timeval start
;
256 assert(image
!= NULL
);
258 assert(root
!= NULL
);
259 assert(fsopts
!= NULL
);
261 if (debug
& DEBUG_FS_MAKEFS
)
262 printf("ffs_makefs: image %s directory %s root %p\n",
265 /* validate tree and options */
267 ffs_validate(dir
, root
, fsopts
);
268 TIMER_RESULTS(start
, "ffs_validate");
270 printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
271 image
, (long long)fsopts
->size
, (long long)fsopts
->inodes
);
275 if (ffs_create_image(image
, fsopts
) == -1)
276 errx(1, "Image file `%s' not created.", image
);
277 TIMER_RESULTS(start
, "ffs_create_image");
279 fsopts
->curinode
= UFS_ROOTINO
;
281 if (debug
& DEBUG_FS_MAKEFS
)
285 printf("Populating `%s'\n", image
);
287 if (! ffs_populate_dir(dir
, root
, fsopts
))
288 errx(1, "Image file `%s' not populated.", image
);
289 TIMER_RESULTS(start
, "ffs_populate_dir");
291 /* ensure no outstanding buffers remain */
292 if (debug
& DEBUG_FS_MAKEFS
)
295 /* update various superblock parameters */
296 superblock
= fsopts
->superblock
;
297 superblock
->fs_fmod
= 0;
298 superblock
->fs_old_cstotal
.cs_ndir
= superblock
->fs_cstotal
.cs_ndir
;
299 superblock
->fs_old_cstotal
.cs_nbfree
= superblock
->fs_cstotal
.cs_nbfree
;
300 superblock
->fs_old_cstotal
.cs_nifree
= superblock
->fs_cstotal
.cs_nifree
;
301 superblock
->fs_old_cstotal
.cs_nffree
= superblock
->fs_cstotal
.cs_nffree
;
303 /* write out superblock; image is now complete */
304 ffs_write_superblock(fsopts
->superblock
, fsopts
);
305 if (close(fsopts
->fd
) == -1)
306 err(1, "Closing `%s'", image
);
308 printf("Image `%s' complete\n", image
);
311 /* end of public functions */
315 ffs_validate(const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
319 int32_t spc
, nspf
, ncyl
, fssize
;
321 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
324 assert(root
!= NULL
);
325 assert(fsopts
!= NULL
);
326 assert(ffs_opts
!= NULL
);
328 if (debug
& DEBUG_FS_VALIDATE
) {
329 printf("ffs_validate: before defaults set:\n");
330 ffs_dump_fsinfo(fsopts
);
333 /* set FFS defaults */
334 if (fsopts
->sectorsize
== -1)
335 fsopts
->sectorsize
= DFL_SECSIZE
;
336 if (ffs_opts
->fsize
== -1)
337 ffs_opts
->fsize
= MAX(DFL_FRAGSIZE
, fsopts
->sectorsize
);
338 if (ffs_opts
->bsize
== -1)
339 ffs_opts
->bsize
= MIN(DFL_BLKSIZE
, 8 * ffs_opts
->fsize
);
340 if (ffs_opts
->cpg
== -1)
341 ffs_opts
->cpg
= DFL_CYLSPERGROUP
;
343 ffs_opts
->cpgflg
= 1;
344 /* fsopts->density is set below */
345 if (ffs_opts
->nsectors
== -1)
346 ffs_opts
->nsectors
= DFL_NSECTORS
;
347 if (ffs_opts
->minfree
== -1)
348 ffs_opts
->minfree
= MINFREE
;
349 if (ffs_opts
->optimization
== -1)
350 ffs_opts
->optimization
= DEFAULTOPT
;
351 if (ffs_opts
->maxcontig
== -1)
352 ffs_opts
->maxcontig
=
353 MAX(1, MIN(MAXPHYS
, FFS_MAXBSIZE
) / ffs_opts
->bsize
);
355 if (ffs_opts
->maxbpg
== -1)
356 ffs_opts
->maxbpg
= ffs_opts
->bsize
/ sizeof(int32_t);
357 if (ffs_opts
->avgfilesize
== -1)
358 ffs_opts
->avgfilesize
= AVFILESIZ
;
359 if (ffs_opts
->avgfpdir
== -1)
360 ffs_opts
->avgfpdir
= AFPDIR
;
362 /* calculate size of tree */
363 ffs_size_dir(root
, fsopts
);
364 fsopts
->inodes
+= UFS_ROOTINO
; /* include first two inodes */
366 if (debug
& DEBUG_FS_VALIDATE
)
367 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
368 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
370 /* add requested slop */
371 fsopts
->size
+= fsopts
->freeblocks
;
372 fsopts
->inodes
+= fsopts
->freefiles
;
373 if (fsopts
->freefilepc
> 0)
375 fsopts
->inodes
* (100 + fsopts
->freefilepc
) / 100;
376 if (fsopts
->freeblockpc
> 0)
378 fsopts
->size
* (100 + fsopts
->freeblockpc
) / 100;
380 /* add space needed for superblocks */
382 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
383 * typically used for small filesystems where space matters.
384 * XXX make this an option.
386 fsopts
->size
+= (SBLOCK_UFS1
+ SBLOCKSIZE
) * ncg
;
387 /* add space needed to store inodes, x3 for blockmaps, etc */
388 if (ffs_opts
->version
== 1)
389 fsopts
->size
+= ncg
* DINODE1_SIZE
*
390 roundup(fsopts
->inodes
/ ncg
,
391 ffs_opts
->bsize
/ DINODE1_SIZE
);
393 fsopts
->size
+= ncg
* DINODE2_SIZE
*
394 roundup(fsopts
->inodes
/ ncg
,
395 ffs_opts
->bsize
/ DINODE2_SIZE
);
398 if (ffs_opts
->minfree
> 0)
400 fsopts
->size
* (100 + ffs_opts
->minfree
) / 100;
402 * XXX any other fs slop to add, such as csum's, bitmaps, etc ??
405 if (fsopts
->size
< fsopts
->minsize
) /* ensure meets minimum size */
406 fsopts
->size
= fsopts
->minsize
;
408 /* round up to the next block */
409 fsopts
->size
= roundup(fsopts
->size
, ffs_opts
->bsize
);
411 /* calculate density if necessary */
412 if (ffs_opts
->density
== -1)
413 ffs_opts
->density
= fsopts
->size
/ fsopts
->inodes
+ 1;
415 if (debug
& DEBUG_FS_VALIDATE
) {
416 printf("ffs_validate: after defaults set:\n");
417 ffs_dump_fsinfo(fsopts
);
418 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
419 dir
, (long long)fsopts
->size
, (long long)fsopts
->inodes
);
421 /* now check calculated sizes vs requested sizes */
422 if (fsopts
->maxsize
> 0 && fsopts
->size
> fsopts
->maxsize
) {
423 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
424 dir
, (long long)fsopts
->size
, (long long)fsopts
->maxsize
);
430 ffs_dump_fsinfo(fsinfo_t
*f
)
433 ffs_opt_t
*fs
= f
->fs_specific
;
435 printf("fsopts at %p\n", f
);
437 printf("\tsize %lld, inodes %lld, curinode %u\n",
438 (long long)f
->size
, (long long)f
->inodes
, f
->curinode
);
440 printf("\tminsize %lld, maxsize %lld\n",
441 (long long)f
->minsize
, (long long)f
->maxsize
);
442 printf("\tfree files %lld, freefile %% %d\n",
443 (long long)f
->freefiles
, f
->freefilepc
);
444 printf("\tfree blocks %lld, freeblock %% %d\n",
445 (long long)f
->freeblocks
, f
->freeblockpc
);
446 printf("\tneedswap %d, sectorsize %d\n", f
->needswap
, f
->sectorsize
);
448 printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
449 fs
->bsize
, fs
->fsize
, fs
->cpg
, fs
->density
);
450 printf("\tnsectors %d, rpm %d, minfree %d\n",
451 fs
->nsectors
, fs
->rpm
, fs
->minfree
);
452 printf("\tmaxcontig %d, maxbpg %d\n",
453 fs
->maxcontig
, fs
->maxbpg
);
454 printf("\toptimization %s\n",
455 fs
->optimization
== FS_OPTSPACE
? "space" : "time");
460 ffs_create_image(const char *image
, fsinfo_t
*fsopts
)
462 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
469 int oflags
= O_RDWR
| O_CREAT
;
471 assert (image
!= NULL
);
472 assert (fsopts
!= NULL
);
475 if (fsopts
->offset
== 0)
477 if ((fsopts
->fd
= open(image
, oflags
, 0666)) == -1) {
478 warn("Can't open `%s' for writing", image
);
483 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
484 if (fstatvfs(fsopts
->fd
, &sfs
) == -1) {
487 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
488 warn("can't fstatvfs `%s', using default %d byte chunk",
491 bufsize
= sfs
.f_iosize
;
493 bufrem
= fsopts
->size
;
495 if (fsopts
->sparse
) {
496 if (ftruncate(fsopts
->fd
, bufrem
) == -1) {
497 printf ("ERROR in truncate. Sparse option disabled\n");
500 bufrem
= 0; /* File truncated at bufrem. Remaining is 0 */
505 if (fsopts
->offset
!= 0)
506 if (lseek(fsopts
->fd
, fsopts
->offset
, SEEK_SET
) == -1) {
511 if ((debug
& DEBUG_FS_CREATE_IMAGE
) && fsopts
->sparse
== 0)
513 "zero-ing image `%s', %lld sectors, using %d byte chunks\n",
514 image
, (long long)bufrem
, bufsize
);
516 buf
= ecalloc(1, bufsize
);
518 i
= write(fsopts
->fd
, buf
, MIN(bufsize
, bufrem
));
520 warn("zeroing image, %lld bytes to go",
530 /* make the file system */
531 if (debug
& DEBUG_FS_CREATE_IMAGE
)
532 printf("calling mkfs(\"%s\", ...)\n", image
);
533 fs
= ffs_mkfs(image
, fsopts
);
534 fsopts
->superblock
= (void *)fs
;
535 if (debug
& DEBUG_FS_CREATE_IMAGE
) {
538 t
= (time_t)((struct fs
*)fsopts
->superblock
)->fs_time
;
539 printf("mkfs returned %p; fs_time %s",
540 fsopts
->superblock
, ctime(&t
));
541 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
542 (long long)fs
->fs_cstotal
.cs_nbfree
,
543 (long long)fs
->fs_cstotal
.cs_nffree
,
544 (long long)fs
->fs_cstotal
.cs_nifree
,
545 (long long)fs
->fs_cstotal
.cs_ndir
);
548 if ((off_t
)(fs
->fs_cstotal
.cs_nifree
+ UFS_ROOTINO
) < fsopts
->inodes
) {
550 "Image file `%s' has %lld free inodes; %lld are required.",
552 (long long)(fs
->fs_cstotal
.cs_nifree
+ UFS_ROOTINO
),
553 (long long)fsopts
->inodes
);
561 ffs_size_dir(fsnode
*root
, fsinfo_t
*fsopts
)
563 struct direct tmpdir
;
565 int curdirsize
, this;
566 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
568 /* node may be NULL (empty directory) */
569 assert(fsopts
!= NULL
);
570 assert(ffs_opts
!= NULL
);
572 if (debug
& DEBUG_FS_SIZE_DIR
)
573 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
574 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
576 #define ADDDIRENT(e) do { \
577 tmpdir.d_namlen = strlen((e)); \
578 this = UFS_DIRSIZ(0, &tmpdir, 0); \
579 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
580 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n", \
581 e, tmpdir.d_namlen, this, curdirsize); \
582 if (this + curdirsize > roundup(curdirsize, UFS_DIRBLKSIZ)) \
583 curdirsize = roundup(curdirsize, UFS_DIRBLKSIZ); \
584 curdirsize += this; \
585 if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT) \
586 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n", \
587 e, tmpdir.d_namlen, this, curdirsize); \
591 * XXX this needs to take into account extra space consumed
592 * by indirect blocks, etc.
594 #define ADDSIZE(x) do { \
595 fsopts->size += roundup((x), ffs_opts->fsize); \
599 for (node
= root
; node
!= NULL
; node
= node
->next
) {
600 ADDDIRENT(node
->name
);
601 if (node
== root
) { /* we're at "." */
602 assert(strcmp(node
->name
, ".") == 0);
604 } else if ((node
->inode
->flags
& FI_SIZED
) == 0) {
605 /* don't count duplicate names */
606 node
->inode
->flags
|= FI_SIZED
;
607 if (debug
& DEBUG_FS_SIZE_DIR_NODE
)
608 printf("ffs_size_dir: `%s' size %lld\n",
610 (long long)node
->inode
->st
.st_size
);
612 if (node
->type
== S_IFREG
)
613 ADDSIZE(node
->inode
->st
.st_size
);
614 if (node
->type
== S_IFLNK
) {
617 slen
= strlen(node
->symlink
) + 1;
618 if (slen
>= (ffs_opts
->version
== 1 ?
624 if (node
->type
== S_IFDIR
)
625 ffs_size_dir(node
->child
, fsopts
);
629 if (debug
& DEBUG_FS_SIZE_DIR
)
630 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
631 (long long)fsopts
->size
, (long long)fsopts
->inodes
);
635 ffs_build_dinode1(struct ufs1_dinode
*dinp
, dirbuf_t
*dbufp
, fsnode
*cur
,
636 fsnode
*root
, fsinfo_t
*fsopts
)
641 memset(dinp
, 0, sizeof(*dinp
));
642 dinp
->di_mode
= cur
->inode
->st
.st_mode
;
643 dinp
->di_nlink
= cur
->inode
->nlink
;
644 dinp
->di_size
= cur
->inode
->st
.st_size
;
645 dinp
->di_atime
= cur
->inode
->st
.st_atime
;
646 dinp
->di_mtime
= cur
->inode
->st
.st_mtime
;
647 dinp
->di_ctime
= cur
->inode
->st
.st_ctime
;
648 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
649 dinp
->di_atimensec
= cur
->inode
->st
.st_atimensec
;
650 dinp
->di_mtimensec
= cur
->inode
->st
.st_mtimensec
;
651 dinp
->di_ctimensec
= cur
->inode
->st
.st_ctimensec
;
653 #if HAVE_STRUCT_STAT_ST_FLAGS
654 dinp
->di_flags
= cur
->inode
->st
.st_flags
;
656 #if HAVE_STRUCT_STAT_ST_GEN
657 dinp
->di_gen
= cur
->inode
->st
.st_gen
;
659 dinp
->di_uid
= cur
->inode
->st
.st_uid
;
660 dinp
->di_gid
= cur
->inode
->st
.st_gid
;
661 /* not set: di_db, di_ib, di_blocks, di_spare */
664 if (cur
== root
) { /* "."; write dirbuf */
666 dinp
->di_size
= dbufp
->size
;
667 } else if (S_ISBLK(cur
->type
) || S_ISCHR(cur
->type
)) {
668 dinp
->di_size
= 0; /* a device */
670 ufs_rw32(cur
->inode
->st
.st_rdev
, fsopts
->needswap
);
671 } else if (S_ISLNK(cur
->type
)) { /* symlink */
672 slen
= strlen(cur
->symlink
);
673 if (slen
< UFS1_MAXSYMLINKLEN
) { /* short link */
674 memcpy(dinp
->di_db
, cur
->symlink
, slen
);
676 membuf
= cur
->symlink
;
677 dinp
->di_size
= slen
;
683 ffs_build_dinode2(struct ufs2_dinode
*dinp
, dirbuf_t
*dbufp
, fsnode
*cur
,
684 fsnode
*root
, fsinfo_t
*fsopts
)
689 memset(dinp
, 0, sizeof(*dinp
));
690 dinp
->di_mode
= cur
->inode
->st
.st_mode
;
691 dinp
->di_nlink
= cur
->inode
->nlink
;
692 dinp
->di_size
= cur
->inode
->st
.st_size
;
693 dinp
->di_atime
= cur
->inode
->st
.st_atime
;
694 dinp
->di_mtime
= cur
->inode
->st
.st_mtime
;
695 dinp
->di_ctime
= cur
->inode
->st
.st_ctime
;
696 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
697 dinp
->di_atimensec
= cur
->inode
->st
.st_atimensec
;
698 dinp
->di_mtimensec
= cur
->inode
->st
.st_mtimensec
;
699 dinp
->di_ctimensec
= cur
->inode
->st
.st_ctimensec
;
701 #if HAVE_STRUCT_STAT_ST_FLAGS
702 dinp
->di_flags
= cur
->inode
->st
.st_flags
;
704 #if HAVE_STRUCT_STAT_ST_GEN
705 dinp
->di_gen
= cur
->inode
->st
.st_gen
;
707 #if HAVE_STRUCT_STAT_BIRTHTIME
708 dinp
->di_birthtime
= cur
->inode
->st
.st_birthtime
;
709 dinp
->di_birthnsec
= cur
->inode
->st
.st_birthtimensec
;
711 dinp
->di_uid
= cur
->inode
->st
.st_uid
;
712 dinp
->di_gid
= cur
->inode
->st
.st_gid
;
713 /* not set: di_db, di_ib, di_blocks, di_spare */
716 if (cur
== root
) { /* "."; write dirbuf */
718 dinp
->di_size
= dbufp
->size
;
719 } else if (S_ISBLK(cur
->type
) || S_ISCHR(cur
->type
)) {
720 dinp
->di_size
= 0; /* a device */
722 ufs_rw64(cur
->inode
->st
.st_rdev
, fsopts
->needswap
);
723 } else if (S_ISLNK(cur
->type
)) { /* symlink */
724 slen
= strlen(cur
->symlink
);
725 if (slen
< UFS2_MAXSYMLINKLEN
) { /* short link */
726 memcpy(dinp
->di_db
, cur
->symlink
, slen
);
728 membuf
= cur
->symlink
;
729 dinp
->di_size
= slen
;
735 ffs_populate_dir(const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
741 char path
[MAXPATHLEN
+ 1];
742 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
745 assert(root
!= NULL
);
746 assert(fsopts
!= NULL
);
747 assert(ffs_opts
!= NULL
);
749 (void)memset(&dirbuf
, 0, sizeof(dirbuf
));
751 if (debug
& DEBUG_FS_POPULATE
)
752 printf("ffs_populate_dir: PASS 1 dir %s node %p\n", dir
, root
);
755 * pass 1: allocate inode numbers, build directory `file'
757 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
758 if ((cur
->inode
->flags
& FI_ALLOCATED
) == 0) {
759 cur
->inode
->flags
|= FI_ALLOCATED
;
760 if (cur
== root
&& cur
->parent
!= NULL
)
761 cur
->inode
->ino
= cur
->parent
->inode
->ino
;
763 cur
->inode
->ino
= fsopts
->curinode
;
767 ffs_make_dirbuf(&dirbuf
, cur
->name
, cur
, fsopts
->needswap
);
768 if (cur
== root
) { /* we're at "."; add ".." */
769 ffs_make_dirbuf(&dirbuf
, "..",
770 cur
->parent
== NULL
? cur
: cur
->parent
->first
,
772 root
->inode
->nlink
++; /* count my parent's link */
773 } else if (cur
->child
!= NULL
)
774 root
->inode
->nlink
++; /* count my child's link */
777 * XXX possibly write file and long symlinks here,
778 * ensuring that blocks get written before inodes?
779 * otoh, this isn't a real filesystem, so who
780 * cares about ordering? :-)
783 if (debug
& DEBUG_FS_POPULATE_DIRBUF
)
784 ffs_dump_dirbuf(&dirbuf
, dir
, fsopts
->needswap
);
787 * pass 2: write out dirbuf, then non-directories at this level
789 if (debug
& DEBUG_FS_POPULATE
)
790 printf("ffs_populate_dir: PASS 2 dir %s\n", dir
);
791 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
792 if (cur
->inode
->flags
& FI_WRITTEN
)
793 continue; /* skip hard-linked entries */
794 cur
->inode
->flags
|= FI_WRITTEN
;
796 if ((size_t)snprintf(path
, sizeof(path
), "%s/%s/%s", cur
->root
,
797 cur
->path
, cur
->name
) >= sizeof(path
))
798 errx(1, "Pathname too long.");
800 if (cur
->child
!= NULL
)
801 continue; /* child creates own inode */
803 /* build on-disk inode */
804 if (ffs_opts
->version
== 1)
805 membuf
= ffs_build_dinode1(&din
.ffs1_din
, &dirbuf
, cur
,
808 membuf
= ffs_build_dinode2(&din
.ffs2_din
, &dirbuf
, cur
,
811 if (debug
& DEBUG_FS_POPULATE_NODE
) {
812 printf("ffs_populate_dir: writing ino %d, %s",
813 cur
->inode
->ino
, inode_type(cur
->type
));
814 if (cur
->inode
->nlink
> 1)
815 printf(", nlink %d", cur
->inode
->nlink
);
819 if (membuf
!= NULL
) {
820 ffs_write_file(&din
, cur
->inode
->ino
, membuf
, fsopts
);
821 } else if (S_ISREG(cur
->type
)) {
822 ffs_write_file(&din
, cur
->inode
->ino
, path
, fsopts
);
824 assert (! S_ISDIR(cur
->type
));
825 ffs_write_inode(&din
, cur
->inode
->ino
, fsopts
);
830 * pass 3: write out sub-directories
832 if (debug
& DEBUG_FS_POPULATE
)
833 printf("ffs_populate_dir: PASS 3 dir %s\n", dir
);
834 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
835 if (cur
->child
== NULL
)
837 if ((size_t)snprintf(path
, sizeof(path
), "%s/%s", dir
,
838 cur
->name
) >= sizeof(path
))
839 errx(1, "Pathname too long.");
840 if (! ffs_populate_dir(path
, cur
->child
, fsopts
))
844 if (debug
& DEBUG_FS_POPULATE
)
845 printf("ffs_populate_dir: DONE dir %s\n", dir
);
848 if (dirbuf
.buf
!= NULL
)
855 ffs_write_file(union dinode
*din
, uint32_t ino
, void *buf
, fsinfo_t
*fsopts
)
859 off_t bufleft
, chunk
, offset
;
863 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
864 struct vnode vp
= { fsopts
, NULL
};
866 assert (din
!= NULL
);
867 assert (buf
!= NULL
);
868 assert (fsopts
!= NULL
);
869 assert (ffs_opts
!= NULL
);
871 isfile
= S_ISREG(DIP(din
, mode
));
876 in
.i_fs
= (struct fs
*)fsopts
->superblock
;
879 if (debug
& DEBUG_FS_WRITE_FILE
) {
881 "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
882 ino
, din
, isfile
, inode_type(DIP(din
, mode
) & S_IFMT
),
883 (long long)DIP(din
, size
));
885 printf(", file '%s'\n", (char *)buf
);
887 printf(", buffer %p\n", buf
);
891 in
.i_size
= DIP(din
, size
);
892 if (ffs_opts
->version
== 1)
893 memcpy(&in
.i_din
.ffs1_din
, &din
->ffs1_din
,
894 sizeof(in
.i_din
.ffs1_din
));
896 memcpy(&in
.i_din
.ffs2_din
, &din
->ffs2_din
,
897 sizeof(in
.i_din
.ffs2_din
));
899 if (DIP(din
, size
) == 0)
900 goto write_inode_and_leave
; /* mmm, cheating */
903 fbuf
= emalloc(ffs_opts
->bsize
);
904 if ((ffd
= open((char *)buf
, O_RDONLY
, 0444)) == -1) {
905 warn("Can't open `%s' for reading", (char *)buf
);
906 goto leave_ffs_write_file
;
913 for (bufleft
= DIP(din
, size
); bufleft
> 0; bufleft
-= chunk
) {
914 chunk
= MIN(bufleft
, ffs_opts
->bsize
);
917 else if ((nread
= read(ffd
, fbuf
, chunk
)) == -1)
918 err(EXIT_FAILURE
, "Reading `%s', %lld bytes to go",
919 (char *)buf
, (long long)bufleft
);
920 else if (nread
!= chunk
)
921 errx(EXIT_FAILURE
, "Reading `%s', %lld bytes to go, "
922 "read %zd bytes, expected %ju bytes, does "
923 "metalog size= attribute mismatch source size?",
924 (char *)buf
, (long long)bufleft
, nread
,
928 offset
= DIP(din
, size
) - bufleft
;
929 if (debug
& DEBUG_FS_WRITE_FILE_BLOCK
)
931 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
932 p
, (long long)offset
,
933 (long long)chunk
, (long long)bufleft
);
935 * XXX if holey support is desired, do the check here
937 * XXX might need to write out last bit in fragroundup
938 * sized chunk. however, ffs_balloc() handles this for us
940 errno
= ffs_balloc(&in
, offset
, chunk
, &bp
);
944 "Writing inode %d (%s), bytes %lld + %lld",
946 isfile
? (char *)buf
:
947 inode_type(DIP(din
, mode
) & S_IFMT
),
948 (long long)offset
, (long long)chunk
);
949 memcpy(bp
->b_data
, p
, chunk
);
952 goto bad_ffs_write_file
;
957 write_inode_and_leave
:
958 ffs_write_inode(&in
.i_din
, in
.i_number
, fsopts
);
960 leave_ffs_write_file
:
969 ffs_dump_dirbuf(dirbuf_t
*dbuf
, const char *dir
, int needswap
)
975 assert (dbuf
!= NULL
);
976 assert (dir
!= NULL
);
977 printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
978 dir
, dbuf
->size
, dbuf
->cur
);
980 for (i
= 0; i
< dbuf
->size
; ) {
981 de
= (struct direct
*)(dbuf
->buf
+ i
);
982 reclen
= ufs_rw16(de
->d_reclen
, needswap
);
984 " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
985 ufs_rw32(de
->d_fileno
, needswap
),
986 inode_type(DTTOIF(de
->d_type
)), i
, reclen
,
987 de
->d_namlen
, de
->d_name
);
994 ffs_make_dirbuf(dirbuf_t
*dbuf
, const char *name
, fsnode
*node
, int needswap
)
996 struct direct de
, *dp
;
997 uint16_t llen
, reclen
;
1000 assert (dbuf
!= NULL
);
1001 assert (name
!= NULL
);
1002 assert (node
!= NULL
);
1003 /* create direct entry */
1004 (void)memset(&de
, 0, sizeof(de
));
1005 de
.d_fileno
= ufs_rw32(node
->inode
->ino
, needswap
);
1006 de
.d_type
= IFTODT(node
->type
);
1007 de
.d_namlen
= (uint8_t)strlen(name
);
1008 strcpy(de
.d_name
, name
);
1009 reclen
= UFS_DIRSIZ(0, &de
, needswap
);
1010 de
.d_reclen
= ufs_rw16(reclen
, needswap
);
1012 dp
= (struct direct
*)(dbuf
->buf
+ dbuf
->cur
);
1015 llen
= UFS_DIRSIZ(0, dp
, needswap
);
1017 if (debug
& DEBUG_FS_MAKE_DIRBUF
)
1019 "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1020 " ino %d type %d reclen %d namlen %d name %.30s\n",
1021 dbuf
->size
, dbuf
->cur
, llen
,
1022 ufs_rw32(de
.d_fileno
, needswap
), de
.d_type
, reclen
,
1023 de
.d_namlen
, de
.d_name
);
1025 if (reclen
+ dbuf
->cur
+ llen
> roundup(dbuf
->size
, UFS_DIRBLKSIZ
)) {
1026 if (debug
& DEBUG_FS_MAKE_DIRBUF
)
1027 printf("ffs_make_dirbuf: growing buf to %d\n",
1028 dbuf
->size
+ UFS_DIRBLKSIZ
);
1029 newbuf
= erealloc(dbuf
->buf
, dbuf
->size
+ UFS_DIRBLKSIZ
);
1031 dbuf
->size
+= UFS_DIRBLKSIZ
;
1032 memset(dbuf
->buf
+ dbuf
->size
- UFS_DIRBLKSIZ
, 0, UFS_DIRBLKSIZ
);
1033 dbuf
->cur
= dbuf
->size
- UFS_DIRBLKSIZ
;
1034 } else if (dp
) { /* shrink end of previous */
1035 dp
->d_reclen
= ufs_rw16(llen
,needswap
);
1038 dp
= (struct direct
*)(dbuf
->buf
+ dbuf
->cur
);
1039 memcpy(dp
, &de
, reclen
);
1040 dp
->d_reclen
= ufs_rw16(dbuf
->size
- dbuf
->cur
, needswap
);
1044 * cribbed from sys/ufs/ffs/ffs_alloc.c
1047 ffs_write_inode(union dinode
*dp
, uint32_t ino
, const fsinfo_t
*fsopts
)
1050 struct ufs1_dinode
*dp1
;
1051 struct ufs2_dinode
*dp2
, *dip
;
1056 char sbbuf
[FFS_MAXBSIZE
];
1057 uint32_t initediblk
;
1058 ffs_opt_t
*ffs_opts
= fsopts
->fs_specific
;
1060 assert (dp
!= NULL
);
1062 assert (fsopts
!= NULL
);
1063 assert (ffs_opts
!= NULL
);
1065 fs
= (struct fs
*)fsopts
->superblock
;
1066 cg
= ino_to_cg(fs
, ino
);
1067 cgino
= ino
% fs
->fs_ipg
;
1068 if (debug
& DEBUG_FS_WRITE_INODE
)
1069 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1070 dp
, ino
, cg
, cgino
);
1072 ffs_rdfs(FFS_FSBTODB(fs
, cgtod(fs
, cg
)), (int)fs
->fs_cgsize
, &sbbuf
,
1074 cgp
= (struct cg
*)sbbuf
;
1075 if (!cg_chkmagic(cgp
, fsopts
->needswap
))
1076 errx(1, "ffs_write_inode: cg %d: bad magic number", cg
);
1078 assert (isclr(cg_inosused(cgp
, fsopts
->needswap
), cgino
));
1080 buf
= emalloc(fs
->fs_bsize
);
1081 dp1
= (struct ufs1_dinode
*)buf
;
1082 dp2
= (struct ufs2_dinode
*)buf
;
1084 if (fs
->fs_cstotal
.cs_nifree
== 0)
1085 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1087 if (fs
->fs_cs(fs
, cg
).cs_nifree
== 0)
1089 "ffs_write_inode: cg %d out of inodes for ino %u",
1091 setbit(cg_inosused(cgp
, fsopts
->needswap
), cgino
);
1092 ufs_add32(cgp
->cg_cs
.cs_nifree
, -1, fsopts
->needswap
);
1093 fs
->fs_cstotal
.cs_nifree
--;
1094 fs
->fs_cs(fs
, cg
).cs_nifree
--;
1095 if (S_ISDIR(DIP(dp
, mode
))) {
1096 ufs_add32(cgp
->cg_cs
.cs_ndir
, 1, fsopts
->needswap
);
1097 fs
->fs_cstotal
.cs_ndir
++;
1098 fs
->fs_cs(fs
, cg
).cs_ndir
++;
1102 * Initialize inode blocks on the fly for UFS2.
1104 initediblk
= ufs_rw32(cgp
->cg_initediblk
, fsopts
->needswap
);
1105 if (ffs_opts
->version
== 2 &&
1106 (uint32_t)(cgino
+ FFS_INOPB(fs
)) > initediblk
&&
1107 initediblk
< ufs_rw32(cgp
->cg_niblk
, fsopts
->needswap
)) {
1108 memset(buf
, 0, fs
->fs_bsize
);
1109 dip
= (struct ufs2_dinode
*)buf
;
1110 srandom(time(NULL
));
1111 for (i
= 0; i
< FFS_INOPB(fs
); i
++) {
1112 dip
->di_gen
= random() / 2 + 1;
1115 ffs_wtfs(FFS_FSBTODB(fs
, ino_to_fsba(fs
,
1116 cg
* fs
->fs_ipg
+ initediblk
)),
1117 fs
->fs_bsize
, buf
, fsopts
);
1118 initediblk
+= FFS_INOPB(fs
);
1119 cgp
->cg_initediblk
= ufs_rw32(initediblk
, fsopts
->needswap
);
1123 ffs_wtfs(FFS_FSBTODB(fs
, cgtod(fs
, cg
)), (int)fs
->fs_cgsize
, &sbbuf
,
1126 /* now write inode */
1127 d
= FFS_FSBTODB(fs
, ino_to_fsba(fs
, ino
));
1128 ffs_rdfs(d
, fs
->fs_bsize
, buf
, fsopts
);
1129 if (fsopts
->needswap
) {
1130 if (ffs_opts
->version
== 1)
1131 ffs_dinode1_swap(&dp
->ffs1_din
,
1132 &dp1
[ino_to_fsbo(fs
, ino
)]);
1134 ffs_dinode2_swap(&dp
->ffs2_din
,
1135 &dp2
[ino_to_fsbo(fs
, ino
)]);
1137 if (ffs_opts
->version
== 1)
1138 dp1
[ino_to_fsbo(fs
, ino
)] = dp
->ffs1_din
;
1140 dp2
[ino_to_fsbo(fs
, ino
)] = dp
->ffs2_din
;
1142 ffs_wtfs(d
, fs
->fs_bsize
, buf
, fsopts
);
1147 panic(const char *fmt
, ...)