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