]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/makefs/ffs.c
Merge CK as of commit 255a47553aa5e8d0bb5f8eec63acac7f4c25a6d8, mostly
[FreeBSD/FreeBSD.git] / usr.sbin / makefs / ffs.c
1 /*      $NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $        */
2
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Luke Mewburn for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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
23  *    written permission.
24  *
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.
36  */
37 /*
38  * Copyright (c) 1982, 1986, 1989, 1993
39  *      The Regents of the University of California.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
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.
52  *
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
63  * SUCH DAMAGE.
64  *
65  *      @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
66  */
67
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
70
71 #include <sys/param.h>
72
73 #include <sys/mount.h>
74
75 #include <assert.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <stdarg.h>
79 #include <stdint.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <time.h>
84 #include <unistd.h>
85
86 #include "makefs.h"
87 #include "ffs.h"
88
89 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
90 #include <sys/statvfs.h>
91 #endif
92
93 #include <ufs/ufs/dinode.h>
94 #include <ufs/ufs/dir.h>
95 #include <ufs/ffs/fs.h>
96
97
98 #include "ffs/ufs_bswap.h"
99 #include "ffs/ufs_inode.h"
100 #include "ffs/newfs_extern.h"
101 #include "ffs/ffs_extern.h"
102
103 #undef DIP
104 #define DIP(dp, field) \
105         ((ffs_opts->version == 1) ? \
106         (dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
107
108 /*
109  * Various file system defaults (cribbed from newfs(8)).
110  */
111 #define DFL_FRAGSIZE            1024            /* fragment size */
112 #define DFL_BLKSIZE             8192            /* block size */
113 #define DFL_SECSIZE             512             /* sector size */
114 #define DFL_CYLSPERGROUP        65536           /* cylinders per group */
115 #define DFL_FRAGSPERINODE       4               /* fragments per inode */
116 #define DFL_ROTDELAY            0               /* rotational delay */
117 #define DFL_NRPOS               1               /* rotational positions */
118 #define DFL_RPM                 3600            /* rpm of disk */
119 #define DFL_NSECTORS            64              /* # of sectors */
120 #define DFL_NTRACKS             16              /* # of tracks */
121
122
123 typedef struct {
124         u_char          *buf;           /* buf for directory */
125         doff_t          size;           /* full size of buf */
126         doff_t          cur;            /* offset of current entry */
127 } dirbuf_t;
128
129
130 static  int     ffs_create_image(const char *, fsinfo_t *);
131 static  void    ffs_dump_fsinfo(fsinfo_t *);
132 static  void    ffs_dump_dirbuf(dirbuf_t *, const char *, int);
133 static  void    ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
134 static  int     ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
135 static  void    ffs_size_dir(fsnode *, fsinfo_t *);
136 static  void    ffs_validate(const char *, fsnode *, fsinfo_t *);
137 static  void    ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
138 static  void    ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
139 static  void    *ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
140                                  fsnode *, fsinfo_t *);
141 static  void    *ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
142                                  fsnode *, fsinfo_t *);
143
144
145
146 int     sectorsize;             /* XXX: for buf.c::getblk() */
147
148         /* publicly visible functions */
149
150 void
151 ffs_prep_opts(fsinfo_t *fsopts)
152 {
153         ffs_opt_t *ffs_opts;
154
155         if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
156                 err(1, "Allocating memory for ffs_options");
157
158         fsopts->fs_specific = ffs_opts;
159
160         ffs_opts->bsize= -1;
161         ffs_opts->fsize= -1;
162         ffs_opts->cpg= -1;
163         ffs_opts->density= -1;
164         ffs_opts->minfree= -1;
165         ffs_opts->optimization= -1;
166         ffs_opts->maxcontig= -1;
167         ffs_opts->maxbpg= -1;
168         ffs_opts->avgfilesize= -1;
169         ffs_opts->avgfpdir= -1;
170         ffs_opts->version = 1;
171 }
172
173 void
174 ffs_cleanup_opts(fsinfo_t *fsopts)
175 {
176         if (fsopts->fs_specific)
177                 free(fsopts->fs_specific);
178 }
179
180 int
181 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
182 {
183         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
184
185         option_t ffs_options[] = {
186                 { "bsize",      &ffs_opts->bsize,       1,      INT_MAX,
187                                         "block size" },
188                 { "fsize",      &ffs_opts->fsize,       1,      INT_MAX,
189                                         "fragment size" },
190                 { "density",    &ffs_opts->density,     1,      INT_MAX,
191                                         "bytes per inode" },
192                 { "minfree",    &ffs_opts->minfree,     0,      99,
193                                         "minfree" },
194                 { "maxbpg",     &ffs_opts->maxbpg,      1,      INT_MAX,
195                                         "max blocks per file in a cg" },
196                 { "avgfilesize", &ffs_opts->avgfilesize,1,      INT_MAX,
197                                         "expected average file size" },
198                 { "avgfpdir",   &ffs_opts->avgfpdir,    1,      INT_MAX,
199                                         "expected # of files per directory" },
200                 { "extent",     &ffs_opts->maxbsize,    1,      INT_MAX,
201                                         "maximum # extent size" },
202                 { "maxbpcg",    &ffs_opts->maxblkspercg,1,      INT_MAX,
203                                         "max # of blocks per group" },
204                 { "version",    &ffs_opts->version,     1,      2,
205                                         "UFS version" },
206                 { .name = NULL }
207         };
208
209         char    *var, *val;
210         int     rv;
211
212         assert(option != NULL);
213         assert(fsopts != NULL);
214         assert(ffs_opts != NULL);
215
216         if (debug & DEBUG_FS_PARSE_OPTS)
217                 printf("ffs_parse_opts: got `%s'\n", option);
218
219         if ((var = strdup(option)) == NULL)
220                 err(1, "Allocating memory for copy of option string");
221         rv = 0;
222
223         if ((val = strchr(var, '=')) == NULL) {
224                 warnx("Option `%s' doesn't contain a value", var);
225                 goto leave_ffs_parse_opts;
226         }
227         *val++ = '\0';
228
229         if (strcmp(var, "optimization") == 0) {
230                 if (strcmp(val, "time") == 0) {
231                         ffs_opts->optimization = FS_OPTTIME;
232                 } else if (strcmp(val, "space") == 0) {
233                         ffs_opts->optimization = FS_OPTSPACE;
234                 } else {
235                         warnx("Invalid optimization `%s'", val);
236                         goto leave_ffs_parse_opts;
237                 }
238                 rv = 1;
239         } else if (strcmp(var, "label") == 0) {
240                 strlcpy(ffs_opts->label, val, sizeof(ffs_opts->label));
241                 rv = 1;
242         } else
243                 rv = set_option(ffs_options, var, val);
244
245  leave_ffs_parse_opts:
246         if (var)
247                 free(var);
248         return (rv);
249 }
250
251
252 void
253 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
254 {
255         struct fs       *superblock;
256         struct timeval  start;
257
258         assert(image != NULL);
259         assert(dir != NULL);
260         assert(root != NULL);
261         assert(fsopts != NULL);
262
263         if (debug & DEBUG_FS_MAKEFS)
264                 printf("ffs_makefs: image %s directory %s root %p\n",
265                     image, dir, root);
266
267                 /* validate tree and options */
268         TIMER_START(start);
269         ffs_validate(dir, root, fsopts);
270         TIMER_RESULTS(start, "ffs_validate");
271
272         printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
273             image, (long long)fsopts->size, (long long)fsopts->inodes);
274
275                 /* create image */
276         TIMER_START(start);
277         if (ffs_create_image(image, fsopts) == -1)
278                 errx(1, "Image file `%s' not created.", image);
279         TIMER_RESULTS(start, "ffs_create_image");
280
281         fsopts->curinode = ROOTINO;
282
283         if (debug & DEBUG_FS_MAKEFS)
284                 putchar('\n');
285
286                 /* populate image */
287         printf("Populating `%s'\n", image);
288         TIMER_START(start);
289         if (! ffs_populate_dir(dir, root, fsopts))
290                 errx(1, "Image file `%s' not populated.", image);
291         TIMER_RESULTS(start, "ffs_populate_dir");
292
293                 /* ensure no outstanding buffers remain */
294         if (debug & DEBUG_FS_MAKEFS)
295                 bcleanup();
296
297                 /* update various superblock parameters */
298         superblock = fsopts->superblock;
299         superblock->fs_fmod = 0;
300         superblock->fs_old_cstotal.cs_ndir   = superblock->fs_cstotal.cs_ndir;
301         superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
302         superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
303         superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
304
305                 /* write out superblock; image is now complete */
306         ffs_write_superblock(fsopts->superblock, fsopts);
307         if (close(fsopts->fd) == -1)
308                 err(1, "Closing `%s'", image);
309         fsopts->fd = -1;
310         printf("Image `%s' complete\n", image);
311 }
312
313         /* end of public functions */
314
315
316 static void
317 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
318 {
319         int32_t ncg = 1;
320 #if notyet
321         int32_t spc, nspf, ncyl, fssize;
322 #endif
323         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
324
325         assert(dir != NULL);
326         assert(root != NULL);
327         assert(fsopts != NULL);
328         assert(ffs_opts != NULL);
329
330         if (debug & DEBUG_FS_VALIDATE) {
331                 printf("ffs_validate: before defaults set:\n");
332                 ffs_dump_fsinfo(fsopts);
333         }
334
335                 /* set FFS defaults */
336         if (fsopts->sectorsize == -1)
337                 fsopts->sectorsize = DFL_SECSIZE;
338         if (ffs_opts->fsize == -1)
339                 ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
340         if (ffs_opts->bsize == -1)
341                 ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
342         if (ffs_opts->cpg == -1)
343                 ffs_opts->cpg = DFL_CYLSPERGROUP;
344         else
345                 ffs_opts->cpgflg = 1;
346                                 /* fsopts->density is set below */
347         if (ffs_opts->nsectors == -1)
348                 ffs_opts->nsectors = DFL_NSECTORS;
349         if (ffs_opts->minfree == -1)
350                 ffs_opts->minfree = MINFREE;
351         if (ffs_opts->optimization == -1)
352                 ffs_opts->optimization = DEFAULTOPT;
353         if (ffs_opts->maxcontig == -1)
354                 ffs_opts->maxcontig =
355                     MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
356         /* XXX ondisk32 */
357         if (ffs_opts->maxbpg == -1)
358                 ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
359         if (ffs_opts->avgfilesize == -1)
360                 ffs_opts->avgfilesize = AVFILESIZ;
361         if (ffs_opts->avgfpdir == -1)
362                 ffs_opts->avgfpdir = AFPDIR;
363
364         if (fsopts->maxsize > 0 &&
365             roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize)
366                 errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d "
367                     "exceeds maxsize %lld.  Lower bsize, or round the minimum "
368                     "and maximum sizes to bsize.", dir,
369                     (long long)fsopts->minsize, ffs_opts->bsize,
370                     (long long)fsopts->maxsize);
371
372                 /* calculate size of tree */
373         ffs_size_dir(root, fsopts);
374         fsopts->inodes += ROOTINO;              /* include first two inodes */
375
376         if (debug & DEBUG_FS_VALIDATE)
377                 printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
378                     (long long)fsopts->size, (long long)fsopts->inodes);
379
380                 /* add requested slop */
381         fsopts->size += fsopts->freeblocks;
382         fsopts->inodes += fsopts->freefiles;
383         if (fsopts->freefilepc > 0)
384                 fsopts->inodes =
385                     fsopts->inodes * (100 + fsopts->freefilepc) / 100;
386         if (fsopts->freeblockpc > 0)
387                 fsopts->size =
388                     fsopts->size * (100 + fsopts->freeblockpc) / 100;
389
390                 /* add space needed for superblocks */
391         /*
392          * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
393          * typically used for small filesystems where space matters.
394          * XXX make this an option.
395          */
396         fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
397                 /* add space needed to store inodes, x3 for blockmaps, etc */
398         if (ffs_opts->version == 1)
399                 fsopts->size += ncg * DINODE1_SIZE *
400                     roundup(fsopts->inodes / ncg, 
401                         ffs_opts->bsize / DINODE1_SIZE);
402         else
403                 fsopts->size += ncg * DINODE2_SIZE *
404                     roundup(fsopts->inodes / ncg, 
405                         ffs_opts->bsize / DINODE2_SIZE);
406
407                 /* add minfree */
408         if (ffs_opts->minfree > 0)
409                 fsopts->size =
410                     fsopts->size * (100 + ffs_opts->minfree) / 100;
411         /*
412          * XXX  any other fs slop to add, such as csum's, bitmaps, etc ??
413          */
414
415         if (fsopts->size < fsopts->minsize)     /* ensure meets minimum size */
416                 fsopts->size = fsopts->minsize;
417
418                 /* round up to the next block */
419         fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
420
421                 /* round up to requested block size, if any */
422         if (fsopts->roundup > 0)
423                 fsopts->size = roundup(fsopts->size, fsopts->roundup);
424
425                 /* calculate density if necessary */
426         if (ffs_opts->density == -1)
427                 ffs_opts->density = fsopts->size / fsopts->inodes + 1;
428
429         if (debug & DEBUG_FS_VALIDATE) {
430                 printf("ffs_validate: after defaults set:\n");
431                 ffs_dump_fsinfo(fsopts);
432                 printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
433                     dir, (long long)fsopts->size, (long long)fsopts->inodes);
434         }
435         sectorsize = fsopts->sectorsize;        /* XXX - see earlier */
436
437                 /* now check calculated sizes vs requested sizes */
438         if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
439                 errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
440                     dir, (long long)fsopts->size, (long long)fsopts->maxsize);
441         }
442 }
443
444
445 static void
446 ffs_dump_fsinfo(fsinfo_t *f)
447 {
448
449         ffs_opt_t       *fs = f->fs_specific;
450
451         printf("fsopts at %p\n", f);
452
453         printf("\tsize %lld, inodes %lld, curinode %u\n",
454             (long long)f->size, (long long)f->inodes, f->curinode);
455
456         printf("\tminsize %lld, maxsize %lld\n",
457             (long long)f->minsize, (long long)f->maxsize);
458         printf("\tfree files %lld, freefile %% %d\n",
459             (long long)f->freefiles, f->freefilepc);
460         printf("\tfree blocks %lld, freeblock %% %d\n",
461             (long long)f->freeblocks, f->freeblockpc);
462         printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
463
464         printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
465             fs->bsize, fs->fsize, fs->cpg, fs->density);
466         printf("\tnsectors %d, rpm %d, minfree %d\n",
467             fs->nsectors, fs->rpm, fs->minfree);
468         printf("\tmaxcontig %d, maxbpg %d\n",
469             fs->maxcontig, fs->maxbpg);
470         printf("\toptimization %s\n",
471             fs->optimization == FS_OPTSPACE ? "space" : "time");
472 }
473
474
475 static int
476 ffs_create_image(const char *image, fsinfo_t *fsopts)
477 {
478 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
479         struct statvfs  sfs;
480 #endif
481         struct fs       *fs;
482         char    *buf;
483         int     i, bufsize;
484         off_t   bufrem;
485         time_t  tstamp;
486
487         assert (image != NULL);
488         assert (fsopts != NULL);
489
490                 /* create image */
491         if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666))
492             == -1) {
493                 warn("Can't open `%s' for writing", image);
494                 return (-1);
495         }
496
497                 /* zero image */
498 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
499         if (fstatvfs(fsopts->fd, &sfs) == -1) {
500 #endif
501                 bufsize = 8192;
502 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
503                 warn("can't fstatvfs `%s', using default %d byte chunk",
504                     image, bufsize);
505         } else
506                 bufsize = sfs.f_iosize;
507 #endif
508         bufrem = fsopts->size;
509         if (fsopts->sparse) {
510                 if (ftruncate(fsopts->fd, bufrem) == -1) {
511                         warn("sparse option disabled.\n");
512                         fsopts->sparse = 0;
513                 }
514         }
515         if (fsopts->sparse) {
516                 /* File truncated at bufrem. Remaining is 0 */
517                 bufrem = 0;
518                 buf = NULL;
519         } else {
520                 if (debug & DEBUG_FS_CREATE_IMAGE)
521                         printf("zero-ing image `%s', %lld sectors, "
522                             "using %d byte chunks\n", image, (long long)bufrem,
523                             bufsize);
524                 if ((buf = calloc(1, bufsize)) == NULL) {
525                         warn("Can't create buffer for sector");
526                         return (-1);
527                 }
528         }
529         while (bufrem > 0) {
530                 i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
531                 if (i == -1) {
532                         warn("zeroing image, %lld bytes to go",
533                             (long long)bufrem);
534                         free(buf);
535                         return (-1);
536                 }
537                 bufrem -= i;
538         }
539         if (buf)
540                 free(buf);
541
542                 /* make the file system */
543         if (debug & DEBUG_FS_CREATE_IMAGE)
544                 printf("calling mkfs(\"%s\", ...)\n", image);
545
546         if (stampst.st_ino != 0)
547                 tstamp = stampst.st_ctime;
548         else
549                 tstamp = start_time.tv_sec;
550
551         srandom(tstamp);
552
553         fs = ffs_mkfs(image, fsopts, tstamp);
554         fsopts->superblock = (void *)fs;
555         if (debug & DEBUG_FS_CREATE_IMAGE) {
556                 time_t t;
557
558                 t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
559                 printf("mkfs returned %p; fs_time %s",
560                     fsopts->superblock, ctime(&t));
561                 printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
562                     (long long)fs->fs_cstotal.cs_nbfree,
563                     (long long)fs->fs_cstotal.cs_nffree,
564                     (long long)fs->fs_cstotal.cs_nifree,
565                     (long long)fs->fs_cstotal.cs_ndir);
566         }
567
568         if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) {
569                 warnx(
570                 "Image file `%s' has %lld free inodes; %lld are required.",
571                     image,
572                     (long long)(fs->fs_cstotal.cs_nifree + ROOTINO),
573                     (long long)fsopts->inodes);
574                 return (-1);
575         }
576         return (fsopts->fd);
577 }
578
579
580 static void
581 ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
582 {
583         struct direct   tmpdir;
584         fsnode *        node;
585         int             curdirsize, this;
586         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
587
588         /* node may be NULL (empty directory) */
589         assert(fsopts != NULL);
590         assert(ffs_opts != NULL);
591
592         if (debug & DEBUG_FS_SIZE_DIR)
593                 printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
594                     (long long)fsopts->size, (long long)fsopts->inodes);
595
596 #define ADDDIRENT(e) do {                                               \
597         tmpdir.d_namlen = strlen((e));                                  \
598         this = DIRSIZ_SWAP(0, &tmpdir, 0);                                      \
599         if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)                       \
600                 printf("ADDDIRENT: was: %s (%d) this %d cur %d\n",      \
601                     e, tmpdir.d_namlen, this, curdirsize);              \
602         if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ))         \
603                 curdirsize = roundup(curdirsize, DIRBLKSIZ);            \
604         curdirsize += this;                                             \
605         if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)                       \
606                 printf("ADDDIRENT: now: %s (%d) this %d cur %d\n",      \
607                     e, tmpdir.d_namlen, this, curdirsize);              \
608 } while (0);
609
610         /*
611          * XXX  this needs to take into account extra space consumed
612          *      by indirect blocks, etc.
613          */
614 #define ADDSIZE(x) do {                                                 \
615         fsopts->size += roundup((x), ffs_opts->fsize);                  \
616 } while (0);
617
618         curdirsize = 0;
619         for (node = root; node != NULL; node = node->next) {
620                 ADDDIRENT(node->name);
621                 if (node == root) {                     /* we're at "." */
622                         assert(strcmp(node->name, ".") == 0);
623                         ADDDIRENT("..");
624                 } else if ((node->inode->flags & FI_SIZED) == 0) {
625                                 /* don't count duplicate names */
626                         node->inode->flags |= FI_SIZED;
627                         if (debug & DEBUG_FS_SIZE_DIR_NODE)
628                                 printf("ffs_size_dir: `%s' size %lld\n",
629                                     node->name,
630                                     (long long)node->inode->st.st_size);
631                         fsopts->inodes++;
632                         if (node->type == S_IFREG)
633                                 ADDSIZE(node->inode->st.st_size);
634                         if (node->type == S_IFLNK) {
635                                 int     slen;
636
637                                 slen = strlen(node->symlink) + 1;
638                                 if (slen >= (ffs_opts->version == 1 ?
639                                                 MAXSYMLINKLEN_UFS1 :
640                                                 MAXSYMLINKLEN_UFS2))
641                                         ADDSIZE(slen);
642                         }
643                 }
644                 if (node->type == S_IFDIR)
645                         ffs_size_dir(node->child, fsopts);
646         }
647         ADDSIZE(curdirsize);
648
649         if (debug & DEBUG_FS_SIZE_DIR)
650                 printf("ffs_size_dir: exit: size %lld inodes %lld\n",
651                     (long long)fsopts->size, (long long)fsopts->inodes);
652 }
653
654 static void *
655 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
656                  fsnode *root, fsinfo_t *fsopts)
657 {
658         int slen;
659         void *membuf;
660         struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
661
662         memset(dinp, 0, sizeof(*dinp));
663         dinp->di_mode = cur->inode->st.st_mode;
664         dinp->di_nlink = cur->inode->nlink;
665         dinp->di_size = cur->inode->st.st_size;
666 #if HAVE_STRUCT_STAT_ST_FLAGS
667         dinp->di_flags = cur->inode->st.st_flags;
668 #endif
669         dinp->di_gen = random();
670         dinp->di_uid = cur->inode->st.st_uid;
671         dinp->di_gid = cur->inode->st.st_gid;
672
673         dinp->di_atime = st->st_atime;
674         dinp->di_mtime = st->st_mtime;
675         dinp->di_ctime = st->st_ctime;
676 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
677         dinp->di_atimensec = st->st_atimensec;
678         dinp->di_mtimensec = st->st_mtimensec;
679         dinp->di_ctimensec = st->st_ctimensec;
680 #endif
681                 /* not set: di_db, di_ib, di_blocks, di_spare */
682
683         membuf = NULL;
684         if (cur == root) {                      /* "."; write dirbuf */
685                 membuf = dbufp->buf;
686                 dinp->di_size = dbufp->size;
687         } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
688                 dinp->di_size = 0;      /* a device */
689                 dinp->di_rdev =
690                     ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
691         } else if (S_ISLNK(cur->type)) {        /* symlink */
692                 slen = strlen(cur->symlink);
693                 if (slen < MAXSYMLINKLEN_UFS1) {        /* short link */
694                         memcpy(dinp->di_db, cur->symlink, slen);
695                 } else
696                         membuf = cur->symlink;
697                 dinp->di_size = slen;
698         }
699         return membuf;
700 }
701
702 static void *
703 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
704                  fsnode *root, fsinfo_t *fsopts)
705 {
706         int slen;
707         void *membuf;
708         struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
709
710         memset(dinp, 0, sizeof(*dinp));
711         dinp->di_mode = cur->inode->st.st_mode;
712         dinp->di_nlink = cur->inode->nlink;
713         dinp->di_size = cur->inode->st.st_size;
714 #if HAVE_STRUCT_STAT_ST_FLAGS
715         dinp->di_flags = cur->inode->st.st_flags;
716 #endif
717         dinp->di_gen = random();
718         dinp->di_uid = cur->inode->st.st_uid;
719         dinp->di_gid = cur->inode->st.st_gid;
720
721         dinp->di_atime = st->st_atime;
722         dinp->di_mtime = st->st_mtime;
723         dinp->di_ctime = st->st_ctime;
724 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
725         dinp->di_atimensec = st->st_atimensec;
726         dinp->di_mtimensec = st->st_mtimensec;
727         dinp->di_ctimensec = st->st_ctimensec;
728 #endif
729 #if HAVE_STRUCT_STAT_BIRTHTIME
730         dinp->di_birthtime = st->st_birthtime;
731         dinp->di_birthnsec = st->st_birthtimensec;
732 #endif
733                 /* not set: di_db, di_ib, di_blocks, di_spare */
734
735         membuf = NULL;
736         if (cur == root) {                      /* "."; write dirbuf */
737                 membuf = dbufp->buf;
738                 dinp->di_size = dbufp->size;
739         } else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
740                 dinp->di_size = 0;      /* a device */
741                 dinp->di_rdev =
742                     ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
743         } else if (S_ISLNK(cur->type)) {        /* symlink */
744                 slen = strlen(cur->symlink);
745                 if (slen < MAXSYMLINKLEN_UFS2) {        /* short link */
746                         memcpy(dinp->di_db, cur->symlink, slen);
747                 } else
748                         membuf = cur->symlink;
749                 dinp->di_size = slen;
750         }
751         return membuf;
752 }
753
754 static int
755 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
756 {
757         fsnode          *cur;
758         dirbuf_t        dirbuf;
759         union dinode    din;
760         void            *membuf;
761         char            path[MAXPATHLEN + 1];
762         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
763
764         assert(dir != NULL);
765         assert(root != NULL);
766         assert(fsopts != NULL);
767         assert(ffs_opts != NULL);
768
769         (void)memset(&dirbuf, 0, sizeof(dirbuf));
770
771         if (debug & DEBUG_FS_POPULATE)
772                 printf("ffs_populate_dir: PASS 1  dir %s node %p\n", dir, root);
773
774                 /*
775                  * pass 1: allocate inode numbers, build directory `file'
776                  */
777         for (cur = root; cur != NULL; cur = cur->next) {
778                 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
779                         cur->inode->flags |= FI_ALLOCATED;
780                         if (cur == root && cur->parent != NULL)
781                                 cur->inode->ino = cur->parent->inode->ino;
782                         else {
783                                 cur->inode->ino = fsopts->curinode;
784                                 fsopts->curinode++;
785                         }
786                 }
787                 ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
788                 if (cur == root) {              /* we're at "."; add ".." */
789                         ffs_make_dirbuf(&dirbuf, "..",
790                             cur->parent == NULL ? cur : cur->parent->first,
791                             fsopts->needswap);
792                         root->inode->nlink++;   /* count my parent's link */
793                 } else if (cur->child != NULL)
794                         root->inode->nlink++;   /* count my child's link */
795
796                 /*
797                  * XXX  possibly write file and long symlinks here,
798                  *      ensuring that blocks get written before inodes?
799                  *      otoh, this isn't a real filesystem, so who
800                  *      cares about ordering? :-)
801                  */
802         }
803         if (debug & DEBUG_FS_POPULATE_DIRBUF)
804                 ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
805
806                 /*
807                  * pass 2: write out dirbuf, then non-directories at this level
808                  */
809         if (debug & DEBUG_FS_POPULATE)
810                 printf("ffs_populate_dir: PASS 2  dir %s\n", dir);
811         for (cur = root; cur != NULL; cur = cur->next) {
812                 if (cur->inode->flags & FI_WRITTEN)
813                         continue;               /* skip hard-linked entries */
814                 cur->inode->flags |= FI_WRITTEN;
815
816                 if (cur->contents == NULL) {
817                         if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
818                             cur->path, cur->name) >= (int)sizeof(path))
819                                 errx(1, "Pathname too long.");
820                 }
821
822                 if (cur->child != NULL)
823                         continue;               /* child creates own inode */
824
825                                 /* build on-disk inode */
826                 if (ffs_opts->version == 1)
827                         membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
828                             root, fsopts);
829                 else
830                         membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
831                             root, fsopts);
832
833                 if (debug & DEBUG_FS_POPULATE_NODE) {
834                         printf("ffs_populate_dir: writing ino %d, %s",
835                             cur->inode->ino, inode_type(cur->type));
836                         if (cur->inode->nlink > 1)
837                                 printf(", nlink %d", cur->inode->nlink);
838                         putchar('\n');
839                 }
840
841                 if (membuf != NULL) {
842                         ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
843                 } else if (S_ISREG(cur->type)) {
844                         ffs_write_file(&din, cur->inode->ino,
845                             (cur->contents) ?  cur->contents : path, fsopts);
846                 } else {
847                         assert (! S_ISDIR(cur->type));
848                         ffs_write_inode(&din, cur->inode->ino, fsopts);
849                 }
850         }
851
852                 /*
853                  * pass 3: write out sub-directories
854                  */
855         if (debug & DEBUG_FS_POPULATE)
856                 printf("ffs_populate_dir: PASS 3  dir %s\n", dir);
857         for (cur = root; cur != NULL; cur = cur->next) {
858                 if (cur->child == NULL)
859                         continue;
860                 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
861                     >= sizeof(path))
862                         errx(1, "Pathname too long.");
863                 if (! ffs_populate_dir(path, cur->child, fsopts))
864                         return (0);
865         }
866
867         if (debug & DEBUG_FS_POPULATE)
868                 printf("ffs_populate_dir: DONE dir %s\n", dir);
869
870                 /* cleanup */
871         if (dirbuf.buf != NULL)
872                 free(dirbuf.buf);
873         return (1);
874 }
875
876
877 static void
878 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
879 {
880         int     isfile, ffd;
881         char    *fbuf, *p;
882         off_t   bufleft, chunk, offset;
883         ssize_t nread;
884         struct inode    in;
885         struct buf *    bp;
886         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
887
888         assert (din != NULL);
889         assert (buf != NULL);
890         assert (fsopts != NULL);
891         assert (ffs_opts != NULL);
892
893         isfile = S_ISREG(DIP(din, mode));
894         fbuf = NULL;
895         ffd = -1;
896         p = NULL;
897
898         in.i_fs = (struct fs *)fsopts->superblock;
899
900         if (debug & DEBUG_FS_WRITE_FILE) {
901                 printf(
902                     "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
903                     ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
904                     (long long)DIP(din, size));
905                 if (isfile)
906                         printf(", file '%s'\n", (char *)buf);
907                 else
908                         printf(", buffer %p\n", buf);
909         }
910
911         in.i_number = ino;
912         in.i_size = DIP(din, size);
913         if (ffs_opts->version == 1)
914                 memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
915                     sizeof(in.i_din.ffs1_din));
916         else
917                 memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
918                     sizeof(in.i_din.ffs2_din));
919         in.i_fd = fsopts->fd;
920
921         if (DIP(din, size) == 0)
922                 goto write_inode_and_leave;             /* mmm, cheating */
923
924         if (isfile) {
925                 if ((fbuf = malloc(ffs_opts->bsize)) == NULL)
926                         err(1, "Allocating memory for write buffer");
927                 if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
928                         warn("Can't open `%s' for reading", (char *)buf);
929                         goto leave_ffs_write_file;
930                 }
931         } else {
932                 p = buf;
933         }
934
935         chunk = 0;
936         for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
937                 chunk = MIN(bufleft, ffs_opts->bsize);
938                 if (!isfile)
939                         ;
940                 else if ((nread = read(ffd, fbuf, chunk)) == -1)
941                         err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
942                             (char *)buf, (long long)bufleft);
943                 else if (nread != chunk)
944                         errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
945                             "read %zd bytes, expected %ju bytes, does "
946                             "metalog size= attribute mismatch source size?",
947                             (char *)buf, (long long)bufleft, nread,
948                             (uintmax_t)chunk);
949                 else
950                         p = fbuf;
951                 offset = DIP(din, size) - bufleft;
952                 if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
953                         printf(
954                 "ffs_write_file: write %p offset %lld size %lld left %lld\n",
955                             p, (long long)offset,
956                             (long long)chunk, (long long)bufleft);
957         /*
958          * XXX  if holey support is desired, do the check here
959          *
960          * XXX  might need to write out last bit in fragroundup
961          *      sized chunk. however, ffs_balloc() handles this for us
962          */
963                 errno = ffs_balloc(&in, offset, chunk, &bp);
964  bad_ffs_write_file:
965                 if (errno != 0)
966                         err(1,
967                             "Writing inode %d (%s), bytes %lld + %lld",
968                             ino,
969                             isfile ? (char *)buf :
970                               inode_type(DIP(din, mode) & S_IFMT),
971                             (long long)offset, (long long)chunk);
972                 memcpy(bp->b_data, p, chunk);
973                 errno = bwrite(bp);
974                 if (errno != 0)
975                         goto bad_ffs_write_file;
976                 brelse(bp);
977                 if (!isfile)
978                         p += chunk;
979         }
980   
981  write_inode_and_leave:
982         ffs_write_inode(&in.i_din, in.i_number, fsopts);
983
984  leave_ffs_write_file:
985         if (fbuf)
986                 free(fbuf);
987         if (ffd != -1)
988                 close(ffd);
989 }
990
991
992 static void
993 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
994 {
995         doff_t          i;
996         struct direct   *de;
997         uint16_t        reclen;
998
999         assert (dbuf != NULL);
1000         assert (dir != NULL);
1001         printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
1002             dir, dbuf->size, dbuf->cur);
1003
1004         for (i = 0; i < dbuf->size; ) {
1005                 de = (struct direct *)(dbuf->buf + i);
1006                 reclen = ufs_rw16(de->d_reclen, needswap);
1007                 printf(
1008             " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
1009                     ufs_rw32(de->d_ino, needswap),
1010                     inode_type(DTTOIF(de->d_type)), i, reclen,
1011                     de->d_namlen, de->d_name);
1012                 i += reclen;
1013                 assert(reclen > 0);
1014         }
1015 }
1016
1017 static void
1018 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
1019 {
1020         struct direct   de, *dp;
1021         uint16_t        llen, reclen;
1022         u_char          *newbuf;
1023
1024         assert (dbuf != NULL);
1025         assert (name != NULL);
1026         assert (node != NULL);
1027                                         /* create direct entry */
1028         (void)memset(&de, 0, sizeof(de));
1029         de.d_ino = ufs_rw32(node->inode->ino, needswap);
1030         de.d_type = IFTODT(node->type);
1031         de.d_namlen = (uint8_t)strlen(name);
1032         strcpy(de.d_name, name);
1033         reclen = DIRSIZ_SWAP(0, &de, needswap);
1034         de.d_reclen = ufs_rw16(reclen, needswap);
1035
1036         dp = (struct direct *)(dbuf->buf + dbuf->cur);
1037         llen = 0;
1038         if (dp != NULL)
1039                 llen = DIRSIZ_SWAP(0, dp, needswap);
1040
1041         if (debug & DEBUG_FS_MAKE_DIRBUF)
1042                 printf(
1043                     "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1044                     "  ino %d type %d reclen %d namlen %d name %.30s\n",
1045                     dbuf->size, dbuf->cur, llen,
1046                     ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1047                     de.d_namlen, de.d_name);
1048
1049         if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1050                 if (debug & DEBUG_FS_MAKE_DIRBUF)
1051                         printf("ffs_make_dirbuf: growing buf to %d\n",
1052                             dbuf->size + DIRBLKSIZ);
1053                 if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL)
1054                         err(1, "Allocating memory for directory buffer");
1055                 dbuf->buf = newbuf;
1056                 dbuf->size += DIRBLKSIZ;
1057                 memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1058                 dbuf->cur = dbuf->size - DIRBLKSIZ;
1059         } else if (dp) {                        /* shrink end of previous */
1060                 dp->d_reclen = ufs_rw16(llen,needswap);
1061                 dbuf->cur += llen;
1062         }
1063         dp = (struct direct *)(dbuf->buf + dbuf->cur);
1064         memcpy(dp, &de, reclen);
1065         dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1066 }
1067
1068 /*
1069  * cribbed from sys/ufs/ffs/ffs_alloc.c
1070  */
1071 static void
1072 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1073 {
1074         char            *buf;
1075         struct ufs1_dinode *dp1;
1076         struct ufs2_dinode *dp2, *dip;
1077         struct cg       *cgp;
1078         struct fs       *fs;
1079         int             cg, cgino, i;
1080         daddr_t         d;
1081         char            sbbuf[FFS_MAXBSIZE];
1082         int32_t         initediblk;
1083         ffs_opt_t       *ffs_opts = fsopts->fs_specific;
1084
1085         assert (dp != NULL);
1086         assert (ino > 0);
1087         assert (fsopts != NULL);
1088         assert (ffs_opts != NULL);
1089
1090         fs = (struct fs *)fsopts->superblock;
1091         cg = ino_to_cg(fs, ino);
1092         cgino = ino % fs->fs_ipg;
1093         if (debug & DEBUG_FS_WRITE_INODE)
1094                 printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1095                     dp, ino, cg, cgino);
1096
1097         ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1098             fsopts);
1099         cgp = (struct cg *)sbbuf;
1100         if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1101                 errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1102
1103         assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1104
1105         buf = malloc(fs->fs_bsize);
1106         if (buf == NULL)
1107                 errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg);
1108
1109         dp1 = (struct ufs1_dinode *)buf;
1110         dp2 = (struct ufs2_dinode *)buf;
1111
1112         if (fs->fs_cstotal.cs_nifree == 0)
1113                 errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1114                     ino);
1115         if (fs->fs_cs(fs, cg).cs_nifree == 0)
1116                 errx(1,
1117                     "ffs_write_inode: cg %d out of inodes for ino %u",
1118                     cg, ino);
1119         setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1120         ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1121         fs->fs_cstotal.cs_nifree--;
1122         fs->fs_cs(fs, cg).cs_nifree--;
1123         if (S_ISDIR(DIP(dp, mode))) {
1124                 ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1125                 fs->fs_cstotal.cs_ndir++;
1126                 fs->fs_cs(fs, cg).cs_ndir++; 
1127         }
1128
1129         /*
1130          * Initialize inode blocks on the fly for UFS2.
1131          */
1132         initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1133         if (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1134             initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1135                 memset(buf, 0, fs->fs_bsize);
1136                 dip = (struct ufs2_dinode *)buf;
1137                 for (i = 0; i < INOPB(fs); i++) {
1138                         dip->di_gen = random();
1139                         dip++;
1140                 }
1141                 ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1142                                   cg * fs->fs_ipg + initediblk)),
1143                     fs->fs_bsize, buf, fsopts);
1144                 initediblk += INOPB(fs);
1145                 cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1146         }
1147
1148
1149         ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1150             fsopts);
1151
1152                                         /* now write inode */
1153         d = fsbtodb(fs, ino_to_fsba(fs, ino));
1154         ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1155         if (fsopts->needswap) {
1156                 if (ffs_opts->version == 1)
1157                         ffs_dinode1_swap(&dp->ffs1_din,
1158                             &dp1[ino_to_fsbo(fs, ino)]);
1159                 else
1160                         ffs_dinode2_swap(&dp->ffs2_din,
1161                             &dp2[ino_to_fsbo(fs, ino)]);
1162         } else {
1163                 if (ffs_opts->version == 1)
1164                         dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1165                 else
1166                         dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1167         }
1168         ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1169         free(buf);
1170 }
1171
1172 void
1173 panic(const char *fmt, ...)
1174 {
1175         va_list ap;
1176
1177         va_start(ap, fmt);
1178         vwarnx(fmt, ap);
1179         va_end(ap);
1180         exit(1);
1181 }