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