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