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