make_lfs.c revision 1.41 1 /* $NetBSD: make_lfs.c,v 1.41 2015/08/12 18:25:03 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.41 2015/08/12 18:25:03 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 ulfs_daddr_t ifibc; /* How many indirect blocks */
105
106 #ifdef MAKE_LF_DIR
107 # define HIGHEST_USED_INO LOSTFOUNDINO
108 #else
109 # define HIGHEST_USED_INO ULFS_ROOTINO
110 #endif
111
112 static const struct dlfs dlfs32_default = {
113 .dlfs_magic = LFS_MAGIC,
114 .dlfs_version = LFS_VERSION,
115 .dlfs_size = 0,
116 .dlfs_ssize = DFL_LFSSEG,
117 .dlfs_dsize = 0,
118 .dlfs_bsize = DFL_LFSBLOCK,
119 .dlfs_fsize = DFL_LFSFRAG,
120 .dlfs_frag = DFL_LFSBLOCK/DFL_LFSFRAG,
121 .dlfs_freehd = HIGHEST_USED_INO + 1,
122 .dlfs_bfree = 0,
123 .dlfs_nfiles = 0,
124 .dlfs_avail = 0,
125 .dlfs_uinodes = 0,
126 .dlfs_idaddr = 0,
127 .dlfs_ifile = LFS_IFILE_INUM,
128 .dlfs_lastseg = 0,
129 .dlfs_nextseg = 0,
130 .dlfs_curseg = 0,
131 .dlfs_offset = 0,
132 .dlfs_lastpseg = 0,
133 .dlfs_inopf = 0,
134 .dlfs_minfree = MINFREE,
135 .dlfs_maxfilesize = 0,
136 .dlfs_fsbpseg = 0,
137 .dlfs_inopb = DFL_LFSBLOCK/sizeof(struct ulfs1_dinode),
138 .dlfs_ifpb = DFL_LFSBLOCK/sizeof(IFILE),
139 .dlfs_sepb = DFL_LFSBLOCK/sizeof(SEGUSE),
140 /* XXX ondisk32 */
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 = ULFS1_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_ifile = LFS_IFILE_INUM,
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 ulfs1_dinode),
204 .dlfs_ifpb = DFL_LFSBLOCK/sizeof(IFILE),
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 = ULFS1_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 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "."},
247 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".."},
248 /* { LFS_IFILE_INUM, sizeof(struct lfs_direct), LFS_DT_REG, 5, "ifile"}, */
249 #ifdef MAKE_LF_DIR
250 { LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 10, "lost+found"},
251 #endif
252 };
253
254 #ifdef MAKE_LF_DIR
255 struct lfs_direct lfs_lf_dir[] = {
256 { LOSTFOUNDINO, sizeof(struct lfs_direct), LFS_DT_DIR, 1, "." },
257 { ULFS_ROOTINO, sizeof(struct lfs_direct), LFS_DT_DIR, 2, ".." },
258 };
259 #endif
260
261 void pwarn(const char *, ...);
262 static void make_dinode(ino_t, struct ulfs1_dinode *, int, struct lfs *);
263 static void make_dir( void *, struct lfs_direct *, int);
264 static uint64_t maxfilesize(int);
265
266 /*
267 * calculate the maximum file size allowed with the specified block shift.
268 */
269 static uint64_t
270 maxfilesize(int bshift)
271 {
272 uint64_t nptr; /* number of block pointers per block */
273 uint64_t maxblock;
274
275 /* XXX ondisk32 */
276 nptr = (1 << bshift) / sizeof(uint32_t);
277 maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
278
279 return maxblock << bshift;
280 }
281
282 /*
283 * Create the root directory for this file system and the lost+found
284 * directory.
285 */
286 static void
287 make_dinode(ino_t ino, struct ulfs1_dinode *dip, int nfrags, struct lfs *fs)
288 {
289 int i;
290 int nblocks, bb, base, factor, lvl;
291
292 nblocks = howmany(nfrags, lfs_sb_getfrag(fs));
293 if (nblocks >= ULFS_NDADDR)
294 nfrags = roundup(nfrags, lfs_sb_getfrag(fs));
295
296 dip->di_nlink = 1;
297 dip->di_blocks = nfrags;
298
299 dip->di_size = (nfrags << lfs_sb_getffshift(fs));
300 dip->di_atime = dip->di_mtime = dip->di_ctime = lfs_sb_gettstamp(fs);
301 dip->di_atimensec = dip->di_mtimensec = dip->di_ctimensec = 0;
302 dip->di_inumber = ino;
303 dip->di_gen = 1;
304
305 if (ULFS_NDADDR < nblocks) {
306 /* Count up how many indirect blocks we need, recursively */
307 /* XXX We are only called with nblocks > 1 for Ifile */
308 bb = nblocks - ULFS_NDADDR;
309 while (bb > 0) {
310 bb = howmany(bb, LFS_NINDIR(fs));
311 ifibc += bb;
312 --bb;
313 }
314 dip->di_blocks += lfs_blkstofrags(fs, ifibc);
315 }
316
317 /* Assign the block addresses for the ifile */
318 for (i = 0; i < MIN(nblocks,ULFS_NDADDR); i++) {
319 dip->di_db[i] = 0x0;
320 }
321 if (nblocks > ULFS_NDADDR) {
322 dip->di_ib[0] = 0x0;
323 bb = howmany(nblocks - ULFS_NDADDR, LFS_NINDIR(fs)) - 1;
324 factor = LFS_NINDIR(fs);
325 base = -ULFS_NDADDR - factor;
326 lvl = 1;
327 while (bb > 0) {
328 dip->di_ib[lvl] = 0x0;
329 bb = howmany(bb, LFS_NINDIR(fs));
330 --bb;
331 factor *= LFS_NINDIR(fs);
332 base -= factor;
333 ++lvl;
334 }
335 }
336 }
337
338 /*
339 * Construct a set of directory entries in "bufp". We assume that all the
340 * entries in protodir fit in the first DIRBLKSIZ.
341 */
342 static void
343 make_dir(void *bufp, struct lfs_direct *protodir, int entries)
344 {
345 char *cp;
346 int i, spcleft;
347
348 spcleft = LFS_DIRBLKSIZ;
349 for (cp = bufp, i = 0; i < entries - 1; i++) {
350 protodir[i].d_reclen = LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0);
351 memmove(cp, &protodir[i], protodir[i].d_reclen);
352 cp += protodir[i].d_reclen;
353 if ((spcleft -= protodir[i].d_reclen) < 0)
354 fatal("%s: %s", special, "directory too big");
355 }
356 protodir[i].d_reclen = spcleft;
357 memmove(cp, &protodir[i], LFS_DIRSIZ(LFS_NEWDIRFMT, &protodir[i], 0));
358 }
359
360 int
361 make_lfs(int devfd, uint secsize, struct dkwedge_info *dkw, int minfree,
362 int block_size, int frag_size, int seg_size, int minfreeseg,
363 int resvseg, int version, daddr_t start, int ibsize, int interleave,
364 u_int32_t roll_id)
365 {
366 struct ulfs1_dinode *dip; /* Pointer to a disk inode */
367 CLEANERINFO *cip; /* Segment cleaner information table */
368 IFILE *ip; /* Pointer to array of ifile structures */
369 IFILE_V1 *ip_v1 = NULL;
370 struct lfs *fs; /* Superblock */
371 SEGUSE *segp; /* Segment usage table */
372 daddr_t sb_addr; /* Address of superblocks */
373 daddr_t seg_addr; /* Address of current segment */
374 int bsize; /* Block size */
375 int fsize; /* Fragment size */
376 int db_per_blk; /* Disk blocks per file block */
377 int i, j;
378 int sb_interval; /* number of segs between super blocks */
379 int ssize; /* Segment size */
380 double fssize;
381 int warned_segtoobig=0;
382 int label_fsb, sb_fsb;
383 int curw, ww;
384 char tbuf[BUFSIZ];
385 struct ubuf *bp;
386 struct uvnode *vp, *save_devvp;
387 daddr_t bb, ubb;
388 int dmeta, labelskew;
389 u_int64_t tsepb, tnseg;
390 time_t stamp;
391 bool is64 = false; /* XXX notyet */
392
393 /*
394 * Initialize buffer cache. Use a ballpark guess of the length of
395 * the segment table for the number of hash chains.
396 */
397 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize);
398 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM);
399 if (tnseg == 0)
400 fatal("zero size partition");
401 bufinit(tnseg / tsepb);
402
403 /* Initialize LFS subsystem with blank superblock and ifile. */
404 fs = lfs_init(devfd, start, (ulfs_daddr_t)0, 1, 1/* XXX debug*/);
405 save_devvp = fs->lfs_devvp;
406 vp = fs->lfs_ivnode;
407 /* XXX this seems like a rubbish */
408 *fs = lfs_default;
409 if (is64) {
410 fs->lfs_dlfs_u.u_64 = dlfs64_default;
411 } else {
412 fs->lfs_dlfs_u.u_32 = dlfs32_default;
413 }
414 fs->lfs_ivnode = vp;
415 fs->lfs_devvp = save_devvp;
416
417
418 /* Set version first of all since it is used to compute other fields */
419 lfs_sb_setversion(fs, version);
420
421 /* If partition is not an LFS partition, warn that that is the case */
422 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) {
423 fatal("partition label indicated fs type \"%s\", "
424 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS);
425 }
426
427 if (!(bsize = block_size)) {
428 bsize = DFL_LFSBLOCK;
429 if (dkw->dkw_size <= SMALL_FSSIZE)
430 bsize = SMALL_LFSBLOCK;
431 }
432 if (!(fsize = frag_size)) {
433 fsize = DFL_LFSFRAG;
434 if (dkw->dkw_size <= SMALL_FSSIZE)
435 fsize = SMALL_LFSFRAG;
436 }
437 if (!(ssize = seg_size)) {
438 ssize = DFL_LFSSEG;
439 if (dkw->dkw_size <= SMALL_FSSIZE)
440 ssize = SMALL_LFSSEG;
441 }
442 if (version > 1) {
443 if (ibsize == 0)
444 ibsize = fsize;
445 if (ibsize <= 0 || ibsize % fsize)
446 fatal("illegal inode block size: %d\n", ibsize);
447 } else if (ibsize && ibsize != bsize)
448 fatal("cannot specify inode block size when version == 1\n");
449
450 /* Sanity check: fsize<=bsize<ssize */
451 if (fsize > bsize) {
452 /* Only complain if fsize was explicitly set */
453 if(frag_size)
454 fatal("fragment size must be <= block size %d", bsize);
455 fsize = bsize;
456 }
457 if (bsize >= ssize) {
458 /* Only fatal if ssize was explicitly set */
459 if(seg_size)
460 fatal("block size must be < segment size");
461 warnx("%s: disklabel segment size (%d) too small, using default (%d)",
462 progname, ssize, DFL_LFSSEG);
463 ssize = DFL_LFSSEG;
464 }
465 if (start < 0 || start >= dkw->dkw_size)
466 fatal("filesystem offset %ld out of range", (long)start);
467 if (version == 1) {
468 if (start)
469 warnx("filesystem offset ignored for version 1 filesystem");
470 start = LFS_LABELPAD / secsize;
471 }
472
473 tryagain:
474 /* Modify parts of superblock overridden by command line arguments */
475 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
476 lfs_sb_setbshift(fs, lfs_log2(bsize));
477 if (1 << lfs_sb_getbshift(fs) != bsize)
478 fatal("%d: block size not a power of 2", bsize);
479 lfs_sb_setbsize(fs, bsize);
480 lfs_sb_setfsize(fs, fsize);
481 lfs_sb_setbmask(fs, bsize - 1);
482 lfs_sb_setffmask(fs, fsize - 1);
483 lfs_sb_setffshift(fs, lfs_log2(fsize));
484 if (1 << lfs_sb_getffshift(fs) != fsize)
485 fatal("%d: frag size not a power of 2", fsize);
486 lfs_sb_setfrag(fs, lfs_numfrags(fs, bsize));
487 lfs_sb_setfbmask(fs, lfs_sb_getfrag(fs) - 1);
488 lfs_sb_setfbshift(fs, lfs_log2(lfs_sb_getfrag(fs)));
489 lfs_sb_setifpb(fs, bsize / sizeof(IFILE));
490 /* XXX ondisk32 */
491 lfs_sb_setnindir(fs, bsize / sizeof(int32_t));
492 }
493
494 if (lfs_sb_getversion(fs) == 1) {
495 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
496 if (!is64) {
497 unsigned segshift;
498
499 segshift = lfs_log2(ssize);
500 if (1 << segshift != ssize)
501 fatal("%d: segment size not power of 2",
502 ssize);
503 fs->lfs_dlfs_u.u_32.dlfs_segshift = segshift;
504 fs->lfs_dlfs_u.u_32.dlfs_segmask = ssize - 1;
505 }
506 lfs_sb_setifpb(fs, lfs_sb_getbsize(fs) / sizeof(IFILE_V1));
507 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
508 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE_V1));
509 lfs_sb_setssize(fs, ssize >> lfs_sb_getbshift(fs));
510 } else {
511 if (ssize % fsize) {
512 fprintf(stderr,
513 "Segment size %d is not a multiple of frag size; ",
514 ssize);
515 ssize = roundup(ssize, fsize);
516 fprintf(stderr, "trying size %d.\n", ssize);
517 goto tryagain;
518 }
519 lfs_sb_setsumsize(fs, fsize);
520 if (!is64) {
521 /* these do not exist in the 64-bit superblock */
522 fs->lfs_dlfs_u.u_32.dlfs_segshift = 0;
523 fs->lfs_dlfs_u.u_32.dlfs_segmask = 0;
524 }
525 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE));
526 lfs_sb_setssize(fs, ssize);
527 lfs_sb_setibsize(fs, ibsize);
528 }
529 lfs_sb_setinopb(fs, lfs_sb_getibsize(fs) / sizeof(struct ulfs1_dinode));
530 lfs_sb_setminfree(fs, minfree);
531
532 if (version > 1) {
533 lfs_sb_setinopf(fs, secsize/LFS_DINODE1_SIZE);
534 lfs_sb_setinterleave(fs, interleave);
535 if (roll_id == 0)
536 roll_id = arc4random();
537 lfs_sb_setident(fs, roll_id);
538 }
539
540 /*
541 * Fill in parts of superblock that can be computed from file system
542 * size, disk geometry and current time.
543 *
544 * XXX: this seems to set dlfs_size wrong for version 1... as in,
545 * sets it and then overwrites it a few lines later.
546 */
547 db_per_blk = bsize/secsize;
548 lfs_sb_setblktodb(fs, lfs_log2(db_per_blk));
549 lfs_sb_setfsbtodb(fs, lfs_log2(fsize / secsize));
550 if (version == 1) {
551 lfs_sb_setsushift(fs, lfs_log2(lfs_sb_getsepb(fs)));
552 lfs_sb_setfsbtodb(fs, 0);
553 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getblktodb(fs));
554 }
555 label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize));
556 sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize));
557 lfs_sb_setfsbpseg(fs, LFS_DBTOFSB(fs, ssize / secsize));
558 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getfsbtodb(fs));
559 lfs_sb_setdsize(fs, LFS_DBTOFSB(fs, dkw->dkw_size) -
560 MAX(label_fsb, LFS_DBTOFSB(fs, start)));
561 lfs_sb_setnseg(fs, lfs_sb_getdsize(fs) / lfs_segtod(fs, 1));
562
563 lfs_sb_setnclean(fs, lfs_sb_getnseg(fs) - 1);
564 lfs_sb_setmaxfilesize(fs, maxfilesize(lfs_sb_getbshift(fs)));
565
566 if (minfreeseg == 0)
567 lfs_sb_setminfreeseg(fs, lfs_sb_getnseg(fs) / DFL_MIN_FREE_SEGS);
568 else
569 lfs_sb_setminfreeseg(fs, minfreeseg);
570 if (lfs_sb_getminfreeseg(fs) < MIN_FREE_SEGS)
571 lfs_sb_setminfreeseg(fs, MIN_FREE_SEGS);
572
573 if (resvseg == 0)
574 lfs_sb_setresvseg(fs, lfs_sb_getminfreeseg(fs) / 2 + 1);
575 else
576 lfs_sb_setresvseg(fs, resvseg);
577 if (lfs_sb_getresvseg(fs) < MIN_RESV_SEGS)
578 lfs_sb_setresvseg(fs, MIN_RESV_SEGS);
579
580 if(lfs_sb_getnseg(fs) < (4 * lfs_sb_getminfreeseg(fs))
581 || lfs_sb_getnseg(fs) < LFS_MIN_SBINTERVAL + 1)
582 {
583 if(seg_size == 0 && ssize > (bsize<<1)) {
584 if(!warned_segtoobig) {
585 fprintf(stderr,"Segment size %d is too large; "
586 "trying smaller sizes.\n", ssize);
587 if (ssize == (bsize << 16)) {
588 fprintf(stderr, "(Did you perhaps "
589 "accidentally leave \"16\" "
590 "in the disklabel's sgs "
591 "field?)\n");
592 }
593 }
594 ++warned_segtoobig;
595 ssize >>= 1;
596 goto tryagain;
597 }
598 fatal("Could not allocate enough segments with segment "
599 "size %d and block size %d;\nplease decrease the "
600 "segment size.\n", ssize, lfs_sb_getbsize(fs));
601 }
602 if(warned_segtoobig)
603 fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize);
604
605 /*
606 * Now that we've determined what we're going to do, announce it
607 * to the user.
608 */
609 printf("Creating a version %d LFS", lfs_sb_getversion(fs));
610 if (lfs_sb_getversion(fs) > 1)
611 printf(" with roll-forward ident 0x%x", lfs_sb_getident(fs));
612 printf("\n");
613 fssize = (double)lfs_sb_getnseg(fs);
614 fssize *= (double)ssize;
615 fssize /= 1048576.0;
616 printf("%.1fMB in %d segments of size %d\n", fssize,
617 lfs_sb_getnseg(fs), ssize);
618
619 /*
620 * The number of free blocks is set from the number of segments
621 * times the segment size - lfs_minfreesegs (that we never write
622 * because we need to make sure the cleaner can run). Then
623 * we'll subtract off the room for the superblocks ifile entries
624 * and segment usage table, and half a block per segment that can't
625 * be written due to fragmentation.
626 */
627 lfs_sb_setdsize(fs,
628 lfs_segtod(fs, lfs_sb_getnseg(fs) - lfs_sb_getminfreeseg(fs)));
629 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs));
630 lfs_sb_subbfree(fs, LFS_DBTOFSB(fs, ((lfs_sb_getnseg(fs) / 2) <<
631 lfs_sb_getblktodb(fs))));
632
633 lfs_sb_setsegtabsz(fs, SEGTABSIZE_SU(fs));
634 lfs_sb_setcleansz(fs, CLEANSIZE_SU(fs));
635 if (time(&stamp) == -1)
636 fatal("time: %s", strerror(errno));
637 lfs_sb_settstamp(fs, stamp);
638 if (version == 1)
639 lfs_sb_setotstamp(fs, stamp);
640
641 if ((sb_interval = lfs_sb_getnseg(fs) / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
642 sb_interval = LFS_MIN_SBINTERVAL;
643
644 /*
645 * Figure out where the superblocks are going to live.
646 *
647 * Make segment 0 start at either zero, or LFS_LABELPAD, or
648 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
649 * from having half a superblock in it.
650 */
651 if (LFS_FSBTODB(fs, LFS_DBTOFSB(fs, start)) != start)
652 fatal("Segment 0 offset is not multiple of frag size\n");
653 if (start != 0 && dbtob(start) != LFS_LABELPAD &&
654 dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
655 fatal("Using flags \"-O %" PRId64 "\" would result in the "
656 "first segment containing only\npart of a superblock. "
657 "Please choose an offset of 0, %d, or %d or more,\n",
658 start, btodb(LFS_LABELPAD),
659 btodb(LFS_LABELPAD + LFS_SBPAD));
660 }
661 lfs_sb_setsboff(fs, 0, label_fsb);
662 if (version == 1)
663 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
664 else
665 lfs_sb_sets0addr(fs, LFS_DBTOFSB(fs, start));
666 lfs_sb_subdsize(fs, sb_fsb);
667 for (i = 1; i < LFS_MAXNUMSB; i++) {
668 sb_addr = ((i * sb_interval) * lfs_segtod(fs, 1))
669 + lfs_sb_getsboff(fs, 0);
670 /* Segment 0 eats the label, except for version 1 */
671 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < label_fsb)
672 sb_addr -= label_fsb - start;
673 if (sb_addr + sizeof(struct dlfs)
674 >= LFS_DBTOFSB(fs, dkw->dkw_size))
675 break;
676 lfs_sb_setsboff(fs, i, sb_addr);
677 lfs_sb_subdsize(fs, sb_fsb);
678 }
679
680 /* We need >= 2 superblocks */
681 if (lfs_sb_getsboff(fs, 1) == 0x0) {
682 fatal("Could not assign a disk address for the second "
683 "superblock.\nPlease decrease the segment size.\n");
684 }
685
686 lfs_sb_setlastseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 2));
687 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 1));
688 lfs_sb_setoffset(fs, lfs_sntod(fs, lfs_sb_getnseg(fs)));
689 lfs_sb_setnextseg(fs, lfs_sntod(fs, 0));
690
691 /*
692 * Initialize the Ifile inode. Do this before we do anything
693 * with the Ifile or segment tables.
694 */
695 dip = VTOI(fs->lfs_ivnode)->i_din.ffs1_din = (struct ulfs1_dinode *)
696 malloc(sizeof(*dip));
697 if (dip == NULL)
698 err(1, NULL);
699 memset(dip, 0, sizeof(*dip));
700 dip->di_mode = LFS_IFREG | 0600;
701 dip->di_flags = SF_IMMUTABLE;
702 make_dinode(LFS_IFILE_INUM, dip,
703 lfs_blkstofrags(fs, lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1), fs);
704 dip->di_size = (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1) << lfs_sb_getbshift(fs);
705 for (i = 0; i < ULFS_NDADDR && i < (dip->di_size >> lfs_sb_getbshift(fs)); i++)
706 VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
707
708 /*
709 * Set up in-superblock segment usage cache
710 */
711 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
712 if (fs->lfs_suflags == NULL)
713 err(1, NULL);
714 fs->lfs_suflags[0] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
715 if (fs->lfs_suflags[0] == NULL)
716 err(1, NULL);
717 fs->lfs_suflags[1] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
718 if (fs->lfs_suflags[1] == NULL)
719 err(1, NULL);
720
721 /*
722 * Initialize the cleanerinfo block
723 */
724 LFS_CLEANERINFO(cip, fs, bp);
725 lfs_ci_setclean(fs, cip, lfs_sb_getnseg(fs));
726 lfs_ci_setdirty(fs, cip, 0);
727 if (version > 1) {
728 lfs_ci_setfree_head(fs, cip, HIGHEST_USED_INO + 1);
729 lfs_ci_setfree_tail(fs, cip, lfs_sb_getifpb(fs) - 1);
730 }
731 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
732
733 /*
734 * Run through segment table and initialize that
735 */
736 for (i = j = 0; i < lfs_sb_getnseg(fs); i++) {
737 LFS_SEGENTRY(segp, fs, i, bp);
738
739 if (i == 0 &&
740 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
741 segp->su_flags = SEGUSE_SUPERBLOCK;
742 lfs_sb_subbfree(fs, sb_fsb);
743 ++j;
744 }
745 if (i > 0) {
746 if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
747 segp->su_flags = SEGUSE_SUPERBLOCK;
748 lfs_sb_subbfree(fs, sb_fsb);
749 ++j;
750 } else
751 segp->su_flags = 0;
752 }
753 segp->su_lastmod = 0;
754 segp->su_nbytes = 0;
755 segp->su_ninos = 0;
756 segp->su_nsums = 0;
757
758 LFS_WRITESEGENTRY(segp, fs, i, bp);
759 }
760
761 /* Initialize root directory */
762 vp = lfs_raw_vget(fs, ULFS_ROOTINO, devfd, 0x0);
763 dip = VTOI(vp)->i_din.ffs1_din;
764 make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)), fs);
765 dip->di_mode = LFS_IFDIR | UMASK;
766 VTOI(vp)->i_lfs_osize = dip->di_size = LFS_DIRBLKSIZ;
767 #ifdef MAKE_LF_DIR
768 VTOI(vp)->i_nlink = dip->di_nlink = 3;
769 #else
770 VTOI(vp)->i_nlink = dip->di_nlink = 2;
771 #endif
772 VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
773 lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)));
774 for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
775 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
776 if (LFS_DIRBLKSIZ < lfs_sb_getbsize(fs))
777 VTOI(vp)->i_lfs_fragsize[i - 1] =
778 roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs));
779 bread(vp, 0, lfs_sb_getfsize(fs), 0, &bp);
780 make_dir(bp->b_data, lfs_root_dir,
781 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
782 VOP_BWRITE(bp);
783
784 #ifdef MAKE_LF_DIR
785 /* Initialize lost+found directory */
786 vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
787 dip = VTOI(vp)->i_din.ffs1_din;
788 make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
789 dip->di_mode = IFDIR | UMASK;
790 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
791 VTOI(vp)->i_nlink = dip->di_nlink = 2;
792 VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
793 lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
794 for (i = 0; i < ULFS_NDADDR && i < howmany(DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
795 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
796 if (DIRBLKSIZ < lfs_sb_getbsize(fs))
797 VTOI(vp)->i_lfs_fragsize[i - 1] =
798 roundup(DIRBLKSIZ,fs->lfs_fsize);
799 bread(vp, 0, fs->lfs_fsize, 0, &bp);
800 make_dir(bp->b_data, lfs_lf_dir,
801 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
802 VOP_BWRITE(bp);
803 #endif /* MAKE_LF_DIR */
804
805 /* Set their IFILE entry version numbers to 1 */
806 LFS_IENTRY(ip, fs, 1, bp);
807 if (version == 1) {
808 ip_v1 = (IFILE_V1 *)ip;
809 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
810 ip_v1->if_version = 1;
811 ip_v1->if_daddr = 0x0;
812 ip_v1->if_nextfree = 0;
813 ++ip_v1;
814 }
815 } else {
816 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
817 ip->if_version = 1;
818 ip->if_daddr = 0x0;
819 ip->if_nextfree = 0;
820 ++ip;
821 }
822 }
823 /* Link remaining IFILE entries in free list */
824 if (version == 1) {
825 for (;
826 i < lfs_sb_getifpb(fs); ++ip_v1) {
827 ip_v1->if_version = 1;
828 ip_v1->if_daddr = LFS_UNUSED_DADDR;
829 ip_v1->if_nextfree = ++i;
830 }
831 --ip_v1;
832 ip_v1->if_nextfree = LFS_UNUSED_INUM;
833 } else {
834 for (;
835 i < lfs_sb_getifpb(fs); ++ip) {
836 ip->if_version = 1;
837 ip->if_daddr = LFS_UNUSED_DADDR;
838 ip->if_nextfree = ++i;
839 }
840 --ip;
841 ip->if_nextfree = LFS_UNUSED_INUM;
842 }
843 VOP_BWRITE(bp);
844
845 /* Write it all to disk. */
846 if (!Nflag)
847 lfs_segwrite(fs, SEGM_CKP);
848
849 /*
850 * Now that we've written everything, look to see what's available
851 * for writing.
852 */
853 lfs_sb_setavail(fs, 0);
854 bb = ubb = dmeta = 0;
855 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
856 LFS_SEGENTRY(segp, fs, i, bp);
857 if (segp->su_flags & SEGUSE_DIRTY) {
858 bb += lfs_btofsb(fs, segp->su_nbytes +
859 segp->su_nsums * lfs_sb_getsumsize(fs));
860 ubb += lfs_btofsb(fs, segp->su_nbytes +
861 segp->su_nsums * lfs_sb_getsumsize(fs) +
862 segp->su_ninos * lfs_sb_getibsize(fs));
863 dmeta += lfs_btofsb(fs,
864 lfs_sb_getsumsize(fs) * segp->su_nsums);
865 dmeta += lfs_btofsb(fs,
866 lfs_sb_getibsize(fs) * segp->su_ninos);
867 } else {
868 lfs_sb_addavail(fs, lfs_segtod(fs, 1));
869 if (segp->su_flags & SEGUSE_SUPERBLOCK)
870 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_SBPAD));
871 if (i == 0 && lfs_sb_getversion(fs) > 1 &&
872 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
873 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_LABELPAD) -
874 lfs_sb_gets0addr(fs));
875 }
876 brelse(bp, 0);
877 }
878 /* Also may be available bytes in current seg */
879 i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
880 lfs_sb_addavail(fs, lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs));
881 /* But do not count minfreesegs */
882 lfs_sb_subavail(fs, lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) - (lfs_sb_getminfreeseg(fs) / 2))));
883
884 labelskew = 0;
885 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
886 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
887 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs) - labelskew - (ubb + bb) / 2);
888
889 /* Put that in the Ifile version too, and write it */
890 LFS_CLEANERINFO(cip, fs, bp);
891 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
892 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
893 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
894 if (!Nflag)
895 lfs_segwrite(fs, SEGM_CKP);
896
897 /*
898 * Finally write out superblocks.
899 */
900 printf("super-block backups (for fsck -b #) at:\n");
901 curw = 0;
902 for (i = 0; i < LFS_MAXNUMSB; i++) {
903 seg_addr = lfs_sb_getsboff(fs, i);
904 if (seg_addr == 0)
905 break;
906
907 if (i != 0)
908 curw += printf(", ");
909 ww = snprintf(tbuf, sizeof(tbuf), "%lld",
910 (long long)LFS_FSBTODB(fs, seg_addr));
911 curw += ww;
912 if (curw >= 78) {
913 printf("\n%s", tbuf);
914 curw = ww;
915 } else
916 printf("%s", tbuf);
917 fflush(stdout);
918
919 /* Leave the time stamp on the alt sb, zero the rest */
920 if (i == 2) {
921 lfs_sb_settstamp(fs, 0);
922 lfs_sb_setcksum(fs, lfs_sb_cksum(fs));
923 }
924 if (!Nflag)
925 lfs_writesuper(fs, seg_addr);
926 }
927 printf(".\n");
928
929 return 0;
930 }
931
932 /*
933 * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
934 */
935 void
936 pwarn(const char *fmt, ...)
937 {
938 va_list ap;
939
940 va_start(ap, fmt);
941 vfprintf(stderr, fmt, ap);
942 va_end(ap);
943 }
944