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