make_lfs.c revision 1.50 1 /* $NetBSD: make_lfs.c,v 1.50 2015/09/15 14:58:05 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*-
32 * Copyright (c) 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
60 #include <sys/cdefs.h>
61 #ifndef lint
62 #if 0
63 static char sccsid[] = "@(#)lfs.c 8.5 (Berkeley) 5/24/95";
64 #else
65 __RCSID("$NetBSD: make_lfs.c,v 1.50 2015/09/15 14:58:05 dholland Exp $");
66 #endif
67 #endif /* not lint */
68
69 #include <sys/param.h>
70 #include <sys/disk.h>
71 #include <sys/time.h>
72 #include <sys/mount.h>
73 #include <sys/stat.h>
74
75 /* Override certain things to make <ufs/lfs/lfs.h> work */
76 # undef simple_lock
77 # define simple_lock(x)
78 # undef simple_unlock
79 # define simple_unlock(x)
80 # define vnode uvnode
81 # define buf ubuf
82 # define panic call_panic
83 #include <ufs/lfs/lfs.h>
84 #include <ufs/lfs/lfs_accessors.h>
85 #include <ufs/lfs/lfs_inode.h>
86
87 #include <err.h>
88 #include <errno.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <time.h>
93 #include <unistd.h>
94
95 #include "config.h"
96 #include "extern.h"
97
98 #include "bufcache.h"
99 #include "vnode.h"
100 #include "lfs_user.h"
101 #include "segwrite.h"
102
103 extern int Nflag; /* Don't write anything */
104
105 static blkcnt_t ifibc; /* How many indirect blocks */
106
107 #ifdef MAKE_LF_DIR
108 # define HIGHEST_USED_INO LOSTFOUNDINO
109 #else
110 # define HIGHEST_USED_INO ULFS_ROOTINO
111 #endif
112
113 static const struct dlfs dlfs32_default = {
114 .dlfs_magic = LFS_MAGIC,
115 .dlfs_version = LFS_VERSION,
116 .dlfs_size = 0,
117 .dlfs_ssize = DFL_LFSSEG,
118 .dlfs_dsize = 0,
119 .dlfs_bsize = DFL_LFSBLOCK,
120 .dlfs_fsize = DFL_LFSFRAG,
121 .dlfs_frag = DFL_LFSBLOCK/DFL_LFSFRAG,
122 .dlfs_freehd = HIGHEST_USED_INO + 1,
123 .dlfs_bfree = 0,
124 .dlfs_nfiles = 0,
125 .dlfs_avail = 0,
126 .dlfs_uinodes = 0,
127 .dlfs_idaddr = 0,
128 .dlfs_ifile = LFS_IFILE_INUM,
129 .dlfs_lastseg = 0,
130 .dlfs_nextseg = 0,
131 .dlfs_curseg = 0,
132 .dlfs_offset = 0,
133 .dlfs_lastpseg = 0,
134 .dlfs_inopf = 0,
135 .dlfs_minfree = MINFREE,
136 .dlfs_maxfilesize = 0,
137 .dlfs_fsbpseg = 0,
138 .dlfs_inopb = DFL_LFSBLOCK/sizeof(struct lfs32_dinode),
139 .dlfs_ifpb = DFL_LFSBLOCK/sizeof(IFILE32),
140 .dlfs_sepb = DFL_LFSBLOCK/sizeof(SEGUSE),
141 .dlfs_nindir = DFL_LFSBLOCK/sizeof(int32_t),
142 .dlfs_nseg = 0,
143 .dlfs_nspf = 0,
144 .dlfs_cleansz = 0,
145 .dlfs_segtabsz = 0,
146 .dlfs_segmask = DFL_LFSSEG_MASK,
147 .dlfs_segshift = DFL_LFSSEG_SHIFT,
148 .dlfs_bshift = DFL_LFSBLOCK_SHIFT,
149 .dlfs_ffshift = DFL_LFS_FFSHIFT,
150 .dlfs_fbshift = DFL_LFS_FBSHIFT,
151 .dlfs_bmask = DFL_LFSBLOCK_MASK,
152 .dlfs_ffmask = DFL_LFS_FFMASK,
153 .dlfs_fbmask = DFL_LFS_FBMASK,
154 .dlfs_blktodb = 0,
155 .dlfs_sushift = 0,
156 .dlfs_maxsymlinklen = LFS32_MAXSYMLINKLEN,
157 .dlfs_sboffs = { 0 },
158 .dlfs_nclean = 0,
159 .dlfs_fsmnt = { 0 },
160 .dlfs_pflags = LFS_PF_CLEAN,
161 .dlfs_dmeta = 0,
162 .dlfs_minfreeseg = 0,
163 .dlfs_sumsize = 0,
164 .dlfs_serial = 0,
165 .dlfs_ibsize = DFL_LFSFRAG,
166 .dlfs_s0addr = 0,
167 .dlfs_tstamp = 0,
168 .dlfs_inodefmt = LFS_44INODEFMT,
169 .dlfs_interleave = 0,
170 .dlfs_ident = 0,
171 .dlfs_fsbtodb = 0,
172 .dlfs_resvseg = 0,
173
174 .dlfs_pad = { 0 },
175 .dlfs_cksum = 0
176 };
177
178 static const struct dlfs64 dlfs64_default = {
179 .dlfs_magic = LFS64_MAGIC,
180 .dlfs_version = LFS_VERSION,
181 .dlfs_size = 0,
182 .dlfs_dsize = 0,
183 .dlfs_ssize = DFL_LFSSEG,
184 .dlfs_bsize = DFL_LFSBLOCK,
185 .dlfs_fsize = DFL_LFSFRAG,
186 .dlfs_frag = DFL_LFSBLOCK/DFL_LFSFRAG,
187 .dlfs_freehd = HIGHEST_USED_INO + 1,
188 .dlfs_nfiles = 0,
189 .dlfs_bfree = 0,
190 .dlfs_avail = 0,
191 .dlfs_idaddr = 0,
192 .dlfs_uinodes = 0,
193 .dlfs_unused_0 = 0,
194 .dlfs_lastseg = 0,
195 .dlfs_nextseg = 0,
196 .dlfs_curseg = 0,
197 .dlfs_offset = 0,
198 .dlfs_lastpseg = 0,
199 .dlfs_inopf = 0,
200 .dlfs_minfree = MINFREE,
201 .dlfs_maxfilesize = 0,
202 .dlfs_fsbpseg = 0,
203 .dlfs_inopb = DFL_LFSBLOCK/sizeof(struct lfs64_dinode),
204 .dlfs_ifpb = DFL_LFSBLOCK/sizeof(IFILE64),
205 .dlfs_sepb = DFL_LFSBLOCK/sizeof(SEGUSE),
206 .dlfs_nindir = DFL_LFSBLOCK/sizeof(int64_t),
207 .dlfs_nseg = 0,
208 .dlfs_nspf = 0,
209 .dlfs_cleansz = 0,
210 .dlfs_segtabsz = 0,
211 .dlfs_bshift = DFL_LFSBLOCK_SHIFT,
212 .dlfs_ffshift = DFL_LFS_FFSHIFT,
213 .dlfs_fbshift = DFL_LFS_FBSHIFT,
214 .dlfs_bmask = DFL_LFSBLOCK_MASK,
215 .dlfs_ffmask = DFL_LFS_FFMASK,
216 .dlfs_fbmask = DFL_LFS_FBMASK,
217 .dlfs_blktodb = 0,
218 .dlfs_sushift = 0,
219 .dlfs_sboffs = { 0 },
220 .dlfs_maxsymlinklen = LFS64_MAXSYMLINKLEN,
221 .dlfs_nclean = 0,
222 .dlfs_fsmnt = { 0 },
223 .dlfs_pflags = LFS_PF_CLEAN,
224 .dlfs_dmeta = 0,
225 .dlfs_minfreeseg = 0,
226 .dlfs_sumsize = 0,
227 .dlfs_ibsize = DFL_LFSFRAG,
228 .dlfs_inodefmt = LFS_44INODEFMT,
229 .dlfs_serial = 0,
230 .dlfs_s0addr = 0,
231 .dlfs_tstamp = 0,
232 .dlfs_interleave = 0,
233 .dlfs_ident = 0,
234 .dlfs_fsbtodb = 0,
235 .dlfs_resvseg = 0,
236
237 .dlfs_pad = { 0 },
238 .dlfs_cksum = 0
239 };
240
241 static const struct lfs lfs_default;
242
243 #define UMASK 0755
244
245 struct lfs_direct lfs_root_dir[] = {
246 {
247 .d_ino = ULFS_ROOTINO,
248 .d_reclen = sizeof(struct lfs_direct),
249 .d_type = LFS_DT_DIR,
250 .d_namlen = 1,
251 .d_name = "."
252 },
253 {
254 .d_ino = ULFS_ROOTINO,
255 .d_reclen = sizeof(struct lfs_direct),
256 .d_type = LFS_DT_DIR,
257 .d_namlen = 2,
258 .d_name = ".."
259 },
260 /*
261 {
262 .d_ino = LFS_IFILE_INUM,
263 .d_reclen = sizeof(struct lfs_direct),
264 .d_type = LFS_DT_REG,
265 .d_namlen = 5,
266 .d_name = "ifile"
267 },
268 */
269 #ifdef MAKE_LF_DIR
270 {
271 .d_ino = LOSTFOUNDINO,
272 .d_reclen = sizeof(struct lfs_direct),
273 .d_type = LFS_DT_DIR,
274 .d_namlen = 10,
275 .d_name = "lost+found"
276 },
277 #endif
278 };
279
280 #ifdef MAKE_LF_DIR
281 struct lfs_direct lfs_lf_dir[] = {
282 {
283 .d_ino = LOSTFOUNDINO,
284 .d_reclen = sizeof(struct lfs_direct),
285 .d_type = LFS_DT_DIR,
286 .d_reclen = 1,
287 .d_name = "."
288 },
289 {
290 .d_ino = ULFS_ROOTINO,
291 .d_reclen = sizeof(struct lfs_direct),
292 .d_type = LFS_DT_DIR,
293 .d_reclen = 2,
294 .d_name = ".."
295 },
296 };
297 #endif
298
299 void pwarn(const char *, ...);
300 static void make_dinode(ino_t, union lfs_dinode *, int, struct lfs *);
301 static void make_dir(struct lfs *, void *, struct lfs_direct *, int);
302 static uint64_t maxfilesize(int);
303
304 /*
305 * calculate the maximum file size allowed with the specified block shift.
306 */
307 static uint64_t
308 maxfilesize(int bshift)
309 {
310 uint64_t nptr; /* number of block pointers per block */
311 uint64_t maxblock;
312
313 /* XXX ondisk32 */
314 nptr = (1 << bshift) / sizeof(uint32_t);
315 maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
316
317 return maxblock << bshift;
318 }
319
320 /*
321 * Create the root directory for this file system and the lost+found
322 * directory.
323 */
324 static void
325 make_dinode(ino_t ino, union lfs_dinode *dip, int nfrags, struct lfs *fs)
326 {
327 int i;
328 int nblocks, bb, base, factor, lvl;
329 time_t t;
330
331 nblocks = howmany(nfrags, lfs_sb_getfrag(fs));
332 if (nblocks >= ULFS_NDADDR)
333 nfrags = roundup(nfrags, lfs_sb_getfrag(fs));
334
335 lfs_dino_setnlink(fs, dip, 1);
336 lfs_dino_setblocks(fs, dip, nfrags);
337
338 lfs_dino_setsize(fs, dip, nfrags << lfs_sb_getffshift(fs));
339 t = lfs_sb_gettstamp(fs);
340 lfs_dino_setatime(fs, dip, t);
341 lfs_dino_setmtime(fs, dip, t);
342 lfs_dino_setctime(fs, dip, t);
343 lfs_dino_setatimensec(fs, dip, 0);
344 lfs_dino_setmtimensec(fs, dip, 0);
345 lfs_dino_setctimensec(fs, dip, 0);
346 lfs_dino_setinumber(fs, dip, ino);
347 lfs_dino_setgen(fs, dip, 1);
348
349 if (ULFS_NDADDR < nblocks) {
350 /* Count up how many indirect blocks we need, recursively */
351 /* XXX We are only called with nblocks > 1 for Ifile */
352 bb = nblocks - ULFS_NDADDR;
353 while (bb > 0) {
354 bb = howmany(bb, LFS_NINDIR(fs));
355 ifibc += bb;
356 --bb;
357 }
358 lfs_dino_setblocks(fs, dip,
359 lfs_dino_getblocks(fs, dip) + lfs_blkstofrags(fs, ifibc));
360 }
361
362 /* Assign the block addresses for the ifile */
363 for (i = 0; i < MIN(nblocks,ULFS_NDADDR); i++) {
364 lfs_dino_setdb(fs, dip, i, 0x0);
365 }
366 if (nblocks > ULFS_NDADDR) {
367 lfs_dino_setib(fs, dip, 0, 0x0);
368 bb = howmany(nblocks - ULFS_NDADDR, LFS_NINDIR(fs)) - 1;
369 factor = LFS_NINDIR(fs);
370 base = -ULFS_NDADDR - factor;
371 lvl = 1;
372 while (bb > 0) {
373 lfs_dino_setib(fs, dip, lvl, 0x0);
374 bb = howmany(bb, LFS_NINDIR(fs));
375 --bb;
376 factor *= LFS_NINDIR(fs);
377 base -= factor;
378 ++lvl;
379 }
380 }
381 }
382
383 /*
384 * Construct a set of directory entries in "bufp". We assume that all the
385 * entries in protodir fit in the first DIRBLKSIZ.
386 */
387 static void
388 make_dir(struct lfs *fs, void *bufp, struct lfs_direct *protodir, int entries)
389 {
390 char *cp;
391 int i, spcleft;
392 unsigned reclen;
393
394 spcleft = LFS_DIRBLKSIZ;
395 for (cp = bufp, i = 0; i < entries - 1; i++) {
396 reclen = LFS_DIRSIZ(fs, &protodir[i]);
397 lfs_dir_setreclen(fs, &protodir[i], reclen);
398 memmove(cp, &protodir[i], lfs_dir_getreclen(fs, &protodir[i]));
399 cp += reclen;
400 if ((spcleft -= reclen) < 0)
401 fatal("%s: %s", special, "directory too big");
402 }
403 lfs_dir_setreclen(fs, &protodir[i], spcleft);
404 memmove(cp, &protodir[i], LFS_DIRSIZ(fs, &protodir[i]));
405 }
406
407 int
408 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree,
409 int block_size, int frag_size, int seg_size, int minfreeseg,
410 int resvseg, int version, daddr_t start, int ibsize, int interleave,
411 u_int32_t roll_id)
412 {
413 union lfs_dinode *dip; /* Pointer to a disk inode */
414 CLEANERINFO *cip; /* Segment cleaner information table */
415 IFILE *ipall; /* Pointer to array of ifile structures */
416 IFILE64 *ip64 = NULL;
417 IFILE32 *ip32 = NULL;
418 IFILE_V1 *ip_v1 = NULL;
419 struct lfs *fs; /* Superblock */
420 SEGUSE *segp; /* Segment usage table */
421 daddr_t sb_addr; /* Address of superblocks */
422 daddr_t seg_addr; /* Address of current segment */
423 int bsize; /* Block size */
424 int fsize; /* Fragment size */
425 int db_per_blk; /* Disk blocks per file block */
426 int i, j;
427 int sb_interval; /* number of segs between super blocks */
428 int ssize; /* Segment size */
429 double fssize;
430 int warned_segtoobig=0;
431 int label_fsb, sb_fsb;
432 int curw, ww;
433 char tbuf[BUFSIZ];
434 struct ubuf *bp;
435 struct uvnode *vp, *save_devvp;
436 daddr_t bb, ubb;
437 int dmeta, labelskew;
438 u_int64_t tsepb, tnseg;
439 time_t stamp;
440 bool is64 = false; /* XXX notyet */
441 bool dobyteswap = false; /* XXX notyet */
442
443 /*
444 * Initialize buffer cache. Use a ballpark guess of the length of
445 * the segment table for the number of hash chains.
446 */
447 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize);
448 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM);
449 if (tnseg == 0)
450 fatal("zero size partition");
451 bufinit(tnseg / tsepb);
452
453 /* Initialize LFS subsystem with blank superblock and ifile. */
454 fs = lfs_init(devfd, start, 0, 1, 1/* XXX debug*/);
455 save_devvp = fs->lfs_devvp;
456 vp = fs->lfs_ivnode;
457 /* XXX this seems like a rubbish */
458 *fs = lfs_default;
459 if (is64) {
460 fs->lfs_dlfs_u.u_64 = dlfs64_default;
461 } else {
462 fs->lfs_dlfs_u.u_32 = dlfs32_default;
463 }
464 fs->lfs_is64 = is64;
465 fs->lfs_dobyteswap = dobyteswap;
466 fs->lfs_hasolddirfmt = false;
467 fs->lfs_ivnode = vp;
468 fs->lfs_devvp = save_devvp;
469
470
471 /* Set version first of all since it is used to compute other fields */
472 lfs_sb_setversion(fs, version);
473
474 /* If partition is not an LFS partition, warn that that is the case */
475 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) {
476 fatal("partition label indicated fs type \"%s\", "
477 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS);
478 }
479
480 if (!(bsize = block_size)) {
481 bsize = DFL_LFSBLOCK;
482 if (dkw->dkw_size <= SMALL_FSSIZE)
483 bsize = SMALL_LFSBLOCK;
484 }
485 if (!(fsize = frag_size)) {
486 fsize = DFL_LFSFRAG;
487 if (dkw->dkw_size <= SMALL_FSSIZE)
488 fsize = SMALL_LFSFRAG;
489 }
490 if (!(ssize = seg_size)) {
491 ssize = DFL_LFSSEG;
492 if (dkw->dkw_size <= SMALL_FSSIZE)
493 ssize = SMALL_LFSSEG;
494 }
495 if (version > 1) {
496 if (ibsize == 0)
497 ibsize = fsize;
498 if (ibsize <= 0 || ibsize % fsize)
499 fatal("illegal inode block size: %d\n", ibsize);
500 } else if (ibsize && ibsize != bsize)
501 fatal("cannot specify inode block size when version == 1\n");
502
503 /* Sanity check: fsize<=bsize<ssize */
504 if (fsize > bsize) {
505 /* Only complain if fsize was explicitly set */
506 if(frag_size)
507 fatal("fragment size must be <= block size %d", bsize);
508 fsize = bsize;
509 }
510 if (bsize >= ssize) {
511 /* Only fatal if ssize was explicitly set */
512 if(seg_size)
513 fatal("block size must be < segment size");
514 warnx("%s: disklabel segment size (%d) too small, using default (%d)",
515 progname, ssize, DFL_LFSSEG);
516 ssize = DFL_LFSSEG;
517 }
518 if (start < 0 || start >= dkw->dkw_size)
519 fatal("filesystem offset %ld out of range", (long)start);
520 if (version == 1) {
521 if (start)
522 warnx("filesystem offset ignored for version 1 filesystem");
523 start = LFS_LABELPAD / secsize;
524 }
525
526 tryagain:
527 /* Modify parts of superblock overridden by command line arguments */
528 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
529 lfs_sb_setbshift(fs, lfs_log2(bsize));
530 if (1 << lfs_sb_getbshift(fs) != bsize)
531 fatal("%d: block size not a power of 2", bsize);
532 lfs_sb_setbsize(fs, bsize);
533 lfs_sb_setfsize(fs, fsize);
534 lfs_sb_setbmask(fs, bsize - 1);
535 lfs_sb_setffmask(fs, fsize - 1);
536 lfs_sb_setffshift(fs, lfs_log2(fsize));
537 if (1 << lfs_sb_getffshift(fs) != fsize)
538 fatal("%d: frag size not a power of 2", fsize);
539 lfs_sb_setfrag(fs, lfs_numfrags(fs, bsize));
540 lfs_sb_setfbmask(fs, lfs_sb_getfrag(fs) - 1);
541 lfs_sb_setfbshift(fs, lfs_log2(lfs_sb_getfrag(fs)));
542 lfs_sb_setifpb(fs, bsize / sizeof(IFILE));
543 /* XXX ondisk32 */
544 lfs_sb_setnindir(fs, bsize / sizeof(int32_t));
545 }
546
547 if (lfs_sb_getversion(fs) == 1) {
548 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
549 if (!is64) {
550 unsigned segshift;
551
552 segshift = lfs_log2(ssize);
553 if (1 << segshift != ssize)
554 fatal("%d: segment size not power of 2",
555 ssize);
556 fs->lfs_dlfs_u.u_32.dlfs_segshift = segshift;
557 fs->lfs_dlfs_u.u_32.dlfs_segmask = ssize - 1;
558 }
559 lfs_sb_setifpb(fs, lfs_sb_getbsize(fs) / sizeof(IFILE_V1));
560 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
561 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE_V1));
562 lfs_sb_setssize(fs, ssize >> lfs_sb_getbshift(fs));
563 } else {
564 if (ssize % fsize) {
565 fprintf(stderr,
566 "Segment size %d is not a multiple of frag size; ",
567 ssize);
568 ssize = roundup(ssize, fsize);
569 fprintf(stderr, "trying size %d.\n", ssize);
570 goto tryagain;
571 }
572 lfs_sb_setsumsize(fs, fsize);
573 if (!is64) {
574 /* these do not exist in the 64-bit superblock */
575 fs->lfs_dlfs_u.u_32.dlfs_segshift = 0;
576 fs->lfs_dlfs_u.u_32.dlfs_segmask = 0;
577 }
578 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE));
579 lfs_sb_setssize(fs, ssize);
580 lfs_sb_setibsize(fs, ibsize);
581 }
582 lfs_sb_setinopb(fs, lfs_sb_getibsize(fs) / DINOSIZE(fs));
583 lfs_sb_setminfree(fs, minfree);
584
585 if (version > 1) {
586 lfs_sb_setinopf(fs, secsize/DINOSIZE(fs));
587 lfs_sb_setinterleave(fs, interleave);
588 if (roll_id == 0)
589 roll_id = arc4random();
590 lfs_sb_setident(fs, roll_id);
591 }
592
593 /*
594 * Fill in parts of superblock that can be computed from file system
595 * size, disk geometry and current time.
596 *
597 * XXX: this seems to set dlfs_size wrong for version 1... as in,
598 * sets it and then overwrites it a few lines later.
599 */
600 db_per_blk = bsize/secsize;
601 lfs_sb_setblktodb(fs, lfs_log2(db_per_blk));
602 lfs_sb_setfsbtodb(fs, lfs_log2(fsize / secsize));
603 if (version == 1) {
604 lfs_sb_setsushift(fs, lfs_log2(lfs_sb_getsepb(fs)));
605 lfs_sb_setfsbtodb(fs, 0);
606 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getblktodb(fs));
607 }
608 label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize));
609 sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize));
610 lfs_sb_setfsbpseg(fs, LFS_DBTOFSB(fs, ssize / secsize));
611 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getfsbtodb(fs));
612 lfs_sb_setdsize(fs, LFS_DBTOFSB(fs, dkw->dkw_size) -
613 MAX(label_fsb, LFS_DBTOFSB(fs, start)));
614 lfs_sb_setnseg(fs, lfs_sb_getdsize(fs) / lfs_segtod(fs, 1));
615
616 lfs_sb_setnclean(fs, lfs_sb_getnseg(fs) - 1);
617 lfs_sb_setmaxfilesize(fs, maxfilesize(lfs_sb_getbshift(fs)));
618
619 if (minfreeseg == 0)
620 lfs_sb_setminfreeseg(fs, lfs_sb_getnseg(fs) / DFL_MIN_FREE_SEGS);
621 else
622 lfs_sb_setminfreeseg(fs, minfreeseg);
623 if (lfs_sb_getminfreeseg(fs) < MIN_FREE_SEGS)
624 lfs_sb_setminfreeseg(fs, MIN_FREE_SEGS);
625
626 if (resvseg == 0)
627 lfs_sb_setresvseg(fs, lfs_sb_getminfreeseg(fs) / 2 + 1);
628 else
629 lfs_sb_setresvseg(fs, resvseg);
630 if (lfs_sb_getresvseg(fs) < MIN_RESV_SEGS)
631 lfs_sb_setresvseg(fs, MIN_RESV_SEGS);
632
633 if(lfs_sb_getnseg(fs) < (4 * lfs_sb_getminfreeseg(fs))
634 || lfs_sb_getnseg(fs) < LFS_MIN_SBINTERVAL + 1)
635 {
636 if(seg_size == 0 && ssize > (bsize<<1)) {
637 if(!warned_segtoobig) {
638 fprintf(stderr,"Segment size %d is too large; "
639 "trying smaller sizes.\n", ssize);
640 if (ssize == (bsize << 16)) {
641 fprintf(stderr, "(Did you perhaps "
642 "accidentally leave \"16\" "
643 "in the disklabel's sgs "
644 "field?)\n");
645 }
646 }
647 ++warned_segtoobig;
648 ssize >>= 1;
649 goto tryagain;
650 }
651 fatal("Could not allocate enough segments with segment "
652 "size %d and block size %d;\nplease decrease the "
653 "segment size.\n", ssize, lfs_sb_getbsize(fs));
654 }
655 if(warned_segtoobig)
656 fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize);
657
658 /*
659 * Now that we've determined what we're going to do, announce it
660 * to the user.
661 */
662 printf("Creating a version %d LFS", lfs_sb_getversion(fs));
663 if (lfs_sb_getversion(fs) > 1)
664 printf(" with roll-forward ident 0x%x", lfs_sb_getident(fs));
665 printf("\n");
666 fssize = (double)lfs_sb_getnseg(fs);
667 fssize *= (double)ssize;
668 fssize /= 1048576.0;
669 printf("%.1fMB in %d segments of size %d\n", fssize,
670 lfs_sb_getnseg(fs), ssize);
671
672 /*
673 * The number of free blocks is set from the number of segments
674 * times the segment size - lfs_minfreesegs (that we never write
675 * because we need to make sure the cleaner can run). Then
676 * we'll subtract off the room for the superblocks ifile entries
677 * and segment usage table, and half a block per segment that can't
678 * be written due to fragmentation.
679 */
680 lfs_sb_setdsize(fs,
681 lfs_segtod(fs, lfs_sb_getnseg(fs) - lfs_sb_getminfreeseg(fs)));
682 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs));
683 lfs_sb_subbfree(fs, LFS_DBTOFSB(fs, ((lfs_sb_getnseg(fs) / 2) <<
684 lfs_sb_getblktodb(fs))));
685
686 lfs_sb_setsegtabsz(fs, SEGTABSIZE_SU(fs));
687 lfs_sb_setcleansz(fs, CLEANSIZE_SU(fs));
688 if (time(&stamp) == -1)
689 fatal("time: %s", strerror(errno));
690 lfs_sb_settstamp(fs, stamp);
691 if (version == 1)
692 lfs_sb_setotstamp(fs, stamp);
693
694 if ((sb_interval = lfs_sb_getnseg(fs) / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
695 sb_interval = LFS_MIN_SBINTERVAL;
696
697 /*
698 * Figure out where the superblocks are going to live.
699 *
700 * Make segment 0 start at either zero, or LFS_LABELPAD, or
701 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
702 * from having half a superblock in it.
703 */
704 if (LFS_FSBTODB(fs, LFS_DBTOFSB(fs, start)) != start)
705 fatal("Segment 0 offset is not multiple of frag size\n");
706 if (start != 0 && dbtob(start) != LFS_LABELPAD &&
707 dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
708 fatal("Using flags \"-O %" PRId64 "\" would result in the "
709 "first segment containing only\npart of a superblock. "
710 "Please choose an offset of 0, %d, or %d or more,\n",
711 start, btodb(LFS_LABELPAD),
712 btodb(LFS_LABELPAD + LFS_SBPAD));
713 }
714 lfs_sb_setsboff(fs, 0, label_fsb);
715 if (version == 1)
716 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
717 else
718 lfs_sb_sets0addr(fs, LFS_DBTOFSB(fs, start));
719 lfs_sb_subdsize(fs, sb_fsb);
720 for (i = 1; i < LFS_MAXNUMSB; i++) {
721 sb_addr = ((i * sb_interval) * lfs_segtod(fs, 1))
722 + lfs_sb_getsboff(fs, 0);
723 /* Segment 0 eats the label, except for version 1 */
724 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < label_fsb)
725 sb_addr -= label_fsb - start;
726 if (sb_addr + sizeof(struct dlfs)
727 >= LFS_DBTOFSB(fs, dkw->dkw_size))
728 break;
729 lfs_sb_setsboff(fs, i, sb_addr);
730 lfs_sb_subdsize(fs, sb_fsb);
731 }
732
733 /* We need >= 2 superblocks */
734 if (lfs_sb_getsboff(fs, 1) == 0x0) {
735 fatal("Could not assign a disk address for the second "
736 "superblock.\nPlease decrease the segment size.\n");
737 }
738
739 lfs_sb_setlastseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 2));
740 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 1));
741 lfs_sb_setoffset(fs, lfs_sntod(fs, lfs_sb_getnseg(fs)));
742 lfs_sb_setnextseg(fs, lfs_sntod(fs, 0));
743
744 /*
745 * Initialize the Ifile inode. Do this before we do anything
746 * with the Ifile or segment tables.
747 *
748 * XXX: is there some reason this allocates a new dinode? we
749 * already have an empty one generated by vget.
750 */
751 dip = malloc(sizeof(*dip));
752 if (dip == NULL)
753 err(1, NULL);
754 memset(dip, 0, sizeof(*dip));
755
756 VTOI(fs->lfs_ivnode)->i_din = dip;
757
758 lfs_dino_setmode(fs, dip, LFS_IFREG | 0600);
759 lfs_dino_setflags(fs, dip, SF_IMMUTABLE);
760 make_dinode(LFS_IFILE_INUM, dip,
761 lfs_blkstofrags(fs, lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1), fs);
762 lfs_dino_setsize(fs, dip, (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1) << lfs_sb_getbshift(fs));
763 for (i = 0; i < ULFS_NDADDR && i < (lfs_dino_getsize(fs, dip) >> lfs_sb_getbshift(fs)); i++)
764 VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
765
766 /*
767 * Set up in-superblock segment usage cache
768 */
769 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
770 if (fs->lfs_suflags == NULL)
771 err(1, NULL);
772 fs->lfs_suflags[0] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
773 if (fs->lfs_suflags[0] == NULL)
774 err(1, NULL);
775 fs->lfs_suflags[1] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
776 if (fs->lfs_suflags[1] == NULL)
777 err(1, NULL);
778
779 /*
780 * Initialize the cleanerinfo block
781 */
782 LFS_CLEANERINFO(cip, fs, bp);
783 lfs_ci_setclean(fs, cip, lfs_sb_getnseg(fs));
784 lfs_ci_setdirty(fs, cip, 0);
785 if (version > 1) {
786 lfs_ci_setfree_head(fs, cip, HIGHEST_USED_INO + 1);
787 lfs_ci_setfree_tail(fs, cip, lfs_sb_getifpb(fs) - 1);
788 }
789 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
790
791 /*
792 * Run through segment table and initialize that
793 */
794 for (i = j = 0; i < lfs_sb_getnseg(fs); i++) {
795 LFS_SEGENTRY(segp, fs, i, bp);
796
797 if (i == 0 &&
798 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
799 segp->su_flags = SEGUSE_SUPERBLOCK;
800 lfs_sb_subbfree(fs, sb_fsb);
801 ++j;
802 }
803 if (i > 0) {
804 if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
805 segp->su_flags = SEGUSE_SUPERBLOCK;
806 lfs_sb_subbfree(fs, sb_fsb);
807 ++j;
808 } else
809 segp->su_flags = 0;
810 }
811 segp->su_lastmod = 0;
812 segp->su_nbytes = 0;
813 segp->su_ninos = 0;
814 segp->su_nsums = 0;
815
816 LFS_WRITESEGENTRY(segp, fs, i, bp);
817 }
818
819 /* Initialize root directory */
820 vp = lfs_raw_vget(fs, ULFS_ROOTINO, devfd, 0x0);
821 dip = VTOI(vp)->i_din;
822 make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)), fs);
823 lfs_dino_setmode(fs, dip, LFS_IFDIR | UMASK);
824 VTOI(vp)->i_lfs_osize = LFS_DIRBLKSIZ;
825 #ifdef MAKE_LF_DIR
826 VTOI(vp)->i_nlink = 3;
827 #else
828 VTOI(vp)->i_nlink = 2;
829 #endif
830 VTOI(vp)->i_lfs_effnblks =
831 lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)));
832 lfs_dino_setsize(fs, dip, VTOI(vp)->i_lfs_osize);
833 lfs_dino_setnlink(fs, dip, VTOI(vp)->i_nlink);
834 lfs_dino_setblocks(fs, dip, VTOI(vp)->i_lfs_effnblks);
835 for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
836 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
837 if (LFS_DIRBLKSIZ < lfs_sb_getbsize(fs))
838 VTOI(vp)->i_lfs_fragsize[i - 1] =
839 roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs));
840 bread(vp, 0, lfs_sb_getfsize(fs), 0, &bp);
841 make_dir(fs, bp->b_data, lfs_root_dir,
842 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
843 VOP_BWRITE(bp);
844
845 #ifdef MAKE_LF_DIR
846 /* Initialize lost+found directory */
847 vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
848 dip = VTOI(vp)->i_din.ffs1_din;
849 make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
850 dip->di_mode = IFDIR | UMASK;
851 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
852 VTOI(vp)->i_nlink = dip->di_nlink = 2;
853 VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
854 lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
855 for (i = 0; i < ULFS_NDADDR && i < howmany(DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
856 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
857 if (DIRBLKSIZ < lfs_sb_getbsize(fs))
858 VTOI(vp)->i_lfs_fragsize[i - 1] =
859 roundup(DIRBLKSIZ,fs->lfs_fsize);
860 bread(vp, 0, fs->lfs_fsize, 0, &bp);
861 make_dir(fs, bp->b_data, lfs_lf_dir,
862 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
863 VOP_BWRITE(bp);
864 #endif /* MAKE_LF_DIR */
865
866 /* Set their IFILE entry version numbers to 1 */
867 LFS_IENTRY(ipall, fs, 1, bp);
868 if (is64) {
869 ip64 = &ipall->u_64;
870 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
871 ip64->if_version = 1;
872 ip64->if_daddr = 0x0;
873 ip64->if_nextfree = 0;
874 ++ip64;
875 }
876 } else if (version > 1) {
877 ip32 = &ipall->u_32;
878 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
879 ip32->if_version = 1;
880 ip32->if_daddr = 0x0;
881 ip32->if_nextfree = 0;
882 ++ip32;
883 }
884 } else {
885 ip_v1 = &ipall->u_v1;
886 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
887 ip_v1->if_version = 1;
888 ip_v1->if_daddr = 0x0;
889 ip_v1->if_nextfree = 0;
890 ++ip_v1;
891 }
892 }
893 /* Link remaining IFILE entries in free list */
894 if (is64) {
895 for (;
896 i < lfs_sb_getifpb(fs); ++ip64) {
897 ip64->if_version = 1;
898 ip64->if_daddr = LFS_UNUSED_DADDR;
899 ip64->if_nextfree = ++i;
900 }
901 --ip64;
902 ip64->if_nextfree = LFS_UNUSED_INUM;
903 } else if (version > 1) {
904 for (;
905 i < lfs_sb_getifpb(fs); ++ip32) {
906 ip32->if_version = 1;
907 ip32->if_daddr = LFS_UNUSED_DADDR;
908 ip32->if_nextfree = ++i;
909 }
910 --ip32;
911 ip32->if_nextfree = LFS_UNUSED_INUM;
912 } else {
913 for (;
914 i < lfs_sb_getifpb(fs); ++ip_v1) {
915 ip_v1->if_version = 1;
916 ip_v1->if_daddr = LFS_UNUSED_DADDR;
917 ip_v1->if_nextfree = ++i;
918 }
919 --ip_v1;
920 ip_v1->if_nextfree = LFS_UNUSED_INUM;
921 }
922 VOP_BWRITE(bp);
923
924 /* Write it all to disk. */
925 if (!Nflag)
926 lfs_segwrite(fs, SEGM_CKP);
927
928 /*
929 * Now that we've written everything, look to see what's available
930 * for writing.
931 */
932 lfs_sb_setavail(fs, 0);
933 bb = ubb = dmeta = 0;
934 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
935 LFS_SEGENTRY(segp, fs, i, bp);
936 if (segp->su_flags & SEGUSE_DIRTY) {
937 bb += lfs_btofsb(fs, segp->su_nbytes +
938 segp->su_nsums * lfs_sb_getsumsize(fs));
939 ubb += lfs_btofsb(fs, segp->su_nbytes +
940 segp->su_nsums * lfs_sb_getsumsize(fs) +
941 segp->su_ninos * lfs_sb_getibsize(fs));
942 dmeta += lfs_btofsb(fs,
943 lfs_sb_getsumsize(fs) * segp->su_nsums);
944 dmeta += lfs_btofsb(fs,
945 lfs_sb_getibsize(fs) * segp->su_ninos);
946 } else {
947 lfs_sb_addavail(fs, lfs_segtod(fs, 1));
948 if (segp->su_flags & SEGUSE_SUPERBLOCK)
949 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_SBPAD));
950 if (i == 0 && lfs_sb_getversion(fs) > 1 &&
951 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
952 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_LABELPAD) -
953 lfs_sb_gets0addr(fs));
954 }
955 brelse(bp, 0);
956 }
957 /* Also may be available bytes in current seg */
958 i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
959 lfs_sb_addavail(fs, lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs));
960 /* But do not count minfreesegs */
961 lfs_sb_subavail(fs, lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) - (lfs_sb_getminfreeseg(fs) / 2))));
962
963 labelskew = 0;
964 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
965 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
966 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs) - labelskew - (ubb + bb) / 2);
967
968 /* Put that in the Ifile version too, and write it */
969 LFS_CLEANERINFO(cip, fs, bp);
970 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
971 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
972 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
973 if (!Nflag)
974 lfs_segwrite(fs, SEGM_CKP);
975
976 /*
977 * Finally write out superblocks.
978 */
979 printf("super-block backups (for fsck -b #) at:\n");
980 curw = 0;
981 for (i = 0; i < LFS_MAXNUMSB; i++) {
982 seg_addr = lfs_sb_getsboff(fs, i);
983 if (seg_addr == 0)
984 break;
985
986 if (i != 0)
987 curw += printf(", ");
988 ww = snprintf(tbuf, sizeof(tbuf), "%lld",
989 (long long)LFS_FSBTODB(fs, seg_addr));
990 curw += ww;
991 if (curw >= 78) {
992 printf("\n%s", tbuf);
993 curw = ww;
994 } else
995 printf("%s", tbuf);
996 fflush(stdout);
997
998 /* Leave the time stamp on the alt sb, zero the rest */
999 if (i == 2) {
1000 lfs_sb_settstamp(fs, 0);
1001 lfs_sb_setcksum(fs, lfs_sb_cksum(fs));
1002 }
1003 if (!Nflag)
1004 lfs_writesuper(fs, seg_addr);
1005 }
1006 printf(".\n");
1007
1008 return 0;
1009 }
1010
1011 /*
1012 * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
1013 */
1014 void
1015 pwarn(const char *fmt, ...)
1016 {
1017 va_list ap;
1018
1019 va_start(ap, fmt);
1020 vfprintf(stderr, fmt, ap);
1021 va_end(ap);
1022 }
1023