make_lfs.c revision 1.47 1 /* $NetBSD: make_lfs.c,v 1.47 2015/09/01 06:12:33 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.47 2015/09/01 06:12:33 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 = LFS32_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_unused_0 = 0,
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 = LFS64_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 bool dobyteswap = false; /* XXX notyet */
401
402 /*
403 * Initialize buffer cache. Use a ballpark guess of the length of
404 * the segment table for the number of hash chains.
405 */
406 tnseg = dkw->dkw_size / ((seg_size ? seg_size : DFL_LFSSEG) / secsize);
407 tsepb = (block_size ? block_size : DFL_LFSBLOCK) / sizeof(SEGSUM);
408 if (tnseg == 0)
409 fatal("zero size partition");
410 bufinit(tnseg / tsepb);
411
412 /* Initialize LFS subsystem with blank superblock and ifile. */
413 fs = lfs_init(devfd, start, (ulfs_daddr_t)0, 1, 1/* XXX debug*/);
414 save_devvp = fs->lfs_devvp;
415 vp = fs->lfs_ivnode;
416 /* XXX this seems like a rubbish */
417 *fs = lfs_default;
418 if (is64) {
419 fs->lfs_dlfs_u.u_64 = dlfs64_default;
420 } else {
421 fs->lfs_dlfs_u.u_32 = dlfs32_default;
422 }
423 fs->lfs_is64 = is64;
424 fs->lfs_dobyteswap = dobyteswap;
425 fs->lfs_ivnode = vp;
426 fs->lfs_devvp = save_devvp;
427
428
429 /* Set version first of all since it is used to compute other fields */
430 lfs_sb_setversion(fs, version);
431
432 /* If partition is not an LFS partition, warn that that is the case */
433 if (strcmp(dkw->dkw_ptype, DKW_PTYPE_LFS) != 0) {
434 fatal("partition label indicated fs type \"%s\", "
435 "expected \"%s\"", dkw->dkw_ptype, DKW_PTYPE_LFS);
436 }
437
438 if (!(bsize = block_size)) {
439 bsize = DFL_LFSBLOCK;
440 if (dkw->dkw_size <= SMALL_FSSIZE)
441 bsize = SMALL_LFSBLOCK;
442 }
443 if (!(fsize = frag_size)) {
444 fsize = DFL_LFSFRAG;
445 if (dkw->dkw_size <= SMALL_FSSIZE)
446 fsize = SMALL_LFSFRAG;
447 }
448 if (!(ssize = seg_size)) {
449 ssize = DFL_LFSSEG;
450 if (dkw->dkw_size <= SMALL_FSSIZE)
451 ssize = SMALL_LFSSEG;
452 }
453 if (version > 1) {
454 if (ibsize == 0)
455 ibsize = fsize;
456 if (ibsize <= 0 || ibsize % fsize)
457 fatal("illegal inode block size: %d\n", ibsize);
458 } else if (ibsize && ibsize != bsize)
459 fatal("cannot specify inode block size when version == 1\n");
460
461 /* Sanity check: fsize<=bsize<ssize */
462 if (fsize > bsize) {
463 /* Only complain if fsize was explicitly set */
464 if(frag_size)
465 fatal("fragment size must be <= block size %d", bsize);
466 fsize = bsize;
467 }
468 if (bsize >= ssize) {
469 /* Only fatal if ssize was explicitly set */
470 if(seg_size)
471 fatal("block size must be < segment size");
472 warnx("%s: disklabel segment size (%d) too small, using default (%d)",
473 progname, ssize, DFL_LFSSEG);
474 ssize = DFL_LFSSEG;
475 }
476 if (start < 0 || start >= dkw->dkw_size)
477 fatal("filesystem offset %ld out of range", (long)start);
478 if (version == 1) {
479 if (start)
480 warnx("filesystem offset ignored for version 1 filesystem");
481 start = LFS_LABELPAD / secsize;
482 }
483
484 tryagain:
485 /* Modify parts of superblock overridden by command line arguments */
486 if (bsize != DFL_LFSBLOCK || fsize != DFL_LFSFRAG) {
487 lfs_sb_setbshift(fs, lfs_log2(bsize));
488 if (1 << lfs_sb_getbshift(fs) != bsize)
489 fatal("%d: block size not a power of 2", bsize);
490 lfs_sb_setbsize(fs, bsize);
491 lfs_sb_setfsize(fs, fsize);
492 lfs_sb_setbmask(fs, bsize - 1);
493 lfs_sb_setffmask(fs, fsize - 1);
494 lfs_sb_setffshift(fs, lfs_log2(fsize));
495 if (1 << lfs_sb_getffshift(fs) != fsize)
496 fatal("%d: frag size not a power of 2", fsize);
497 lfs_sb_setfrag(fs, lfs_numfrags(fs, bsize));
498 lfs_sb_setfbmask(fs, lfs_sb_getfrag(fs) - 1);
499 lfs_sb_setfbshift(fs, lfs_log2(lfs_sb_getfrag(fs)));
500 lfs_sb_setifpb(fs, bsize / sizeof(IFILE));
501 /* XXX ondisk32 */
502 lfs_sb_setnindir(fs, bsize / sizeof(int32_t));
503 }
504
505 if (lfs_sb_getversion(fs) == 1) {
506 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
507 if (!is64) {
508 unsigned segshift;
509
510 segshift = lfs_log2(ssize);
511 if (1 << segshift != ssize)
512 fatal("%d: segment size not power of 2",
513 ssize);
514 fs->lfs_dlfs_u.u_32.dlfs_segshift = segshift;
515 fs->lfs_dlfs_u.u_32.dlfs_segmask = ssize - 1;
516 }
517 lfs_sb_setifpb(fs, lfs_sb_getbsize(fs) / sizeof(IFILE_V1));
518 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
519 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE_V1));
520 lfs_sb_setssize(fs, ssize >> lfs_sb_getbshift(fs));
521 } else {
522 if (ssize % fsize) {
523 fprintf(stderr,
524 "Segment size %d is not a multiple of frag size; ",
525 ssize);
526 ssize = roundup(ssize, fsize);
527 fprintf(stderr, "trying size %d.\n", ssize);
528 goto tryagain;
529 }
530 lfs_sb_setsumsize(fs, fsize);
531 if (!is64) {
532 /* these do not exist in the 64-bit superblock */
533 fs->lfs_dlfs_u.u_32.dlfs_segshift = 0;
534 fs->lfs_dlfs_u.u_32.dlfs_segmask = 0;
535 }
536 lfs_sb_setsepb(fs, bsize / sizeof(SEGUSE));
537 lfs_sb_setssize(fs, ssize);
538 lfs_sb_setibsize(fs, ibsize);
539 }
540 lfs_sb_setinopb(fs, lfs_sb_getibsize(fs) / DINOSIZE(fs));
541 lfs_sb_setminfree(fs, minfree);
542
543 if (version > 1) {
544 lfs_sb_setinopf(fs, secsize/DINOSIZE(fs));
545 lfs_sb_setinterleave(fs, interleave);
546 if (roll_id == 0)
547 roll_id = arc4random();
548 lfs_sb_setident(fs, roll_id);
549 }
550
551 /*
552 * Fill in parts of superblock that can be computed from file system
553 * size, disk geometry and current time.
554 *
555 * XXX: this seems to set dlfs_size wrong for version 1... as in,
556 * sets it and then overwrites it a few lines later.
557 */
558 db_per_blk = bsize/secsize;
559 lfs_sb_setblktodb(fs, lfs_log2(db_per_blk));
560 lfs_sb_setfsbtodb(fs, lfs_log2(fsize / secsize));
561 if (version == 1) {
562 lfs_sb_setsushift(fs, lfs_log2(lfs_sb_getsepb(fs)));
563 lfs_sb_setfsbtodb(fs, 0);
564 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getblktodb(fs));
565 }
566 label_fsb = lfs_btofsb(fs, roundup(LFS_LABELPAD, fsize));
567 sb_fsb = lfs_btofsb(fs, roundup(LFS_SBPAD, fsize));
568 lfs_sb_setfsbpseg(fs, LFS_DBTOFSB(fs, ssize / secsize));
569 lfs_sb_setsize(fs, dkw->dkw_size >> lfs_sb_getfsbtodb(fs));
570 lfs_sb_setdsize(fs, LFS_DBTOFSB(fs, dkw->dkw_size) -
571 MAX(label_fsb, LFS_DBTOFSB(fs, start)));
572 lfs_sb_setnseg(fs, lfs_sb_getdsize(fs) / lfs_segtod(fs, 1));
573
574 lfs_sb_setnclean(fs, lfs_sb_getnseg(fs) - 1);
575 lfs_sb_setmaxfilesize(fs, maxfilesize(lfs_sb_getbshift(fs)));
576
577 if (minfreeseg == 0)
578 lfs_sb_setminfreeseg(fs, lfs_sb_getnseg(fs) / DFL_MIN_FREE_SEGS);
579 else
580 lfs_sb_setminfreeseg(fs, minfreeseg);
581 if (lfs_sb_getminfreeseg(fs) < MIN_FREE_SEGS)
582 lfs_sb_setminfreeseg(fs, MIN_FREE_SEGS);
583
584 if (resvseg == 0)
585 lfs_sb_setresvseg(fs, lfs_sb_getminfreeseg(fs) / 2 + 1);
586 else
587 lfs_sb_setresvseg(fs, resvseg);
588 if (lfs_sb_getresvseg(fs) < MIN_RESV_SEGS)
589 lfs_sb_setresvseg(fs, MIN_RESV_SEGS);
590
591 if(lfs_sb_getnseg(fs) < (4 * lfs_sb_getminfreeseg(fs))
592 || lfs_sb_getnseg(fs) < LFS_MIN_SBINTERVAL + 1)
593 {
594 if(seg_size == 0 && ssize > (bsize<<1)) {
595 if(!warned_segtoobig) {
596 fprintf(stderr,"Segment size %d is too large; "
597 "trying smaller sizes.\n", ssize);
598 if (ssize == (bsize << 16)) {
599 fprintf(stderr, "(Did you perhaps "
600 "accidentally leave \"16\" "
601 "in the disklabel's sgs "
602 "field?)\n");
603 }
604 }
605 ++warned_segtoobig;
606 ssize >>= 1;
607 goto tryagain;
608 }
609 fatal("Could not allocate enough segments with segment "
610 "size %d and block size %d;\nplease decrease the "
611 "segment size.\n", ssize, lfs_sb_getbsize(fs));
612 }
613 if(warned_segtoobig)
614 fprintf(stderr,"Using segment size %d, block size %d, frag size %d.\n", ssize, bsize, fsize);
615
616 /*
617 * Now that we've determined what we're going to do, announce it
618 * to the user.
619 */
620 printf("Creating a version %d LFS", lfs_sb_getversion(fs));
621 if (lfs_sb_getversion(fs) > 1)
622 printf(" with roll-forward ident 0x%x", lfs_sb_getident(fs));
623 printf("\n");
624 fssize = (double)lfs_sb_getnseg(fs);
625 fssize *= (double)ssize;
626 fssize /= 1048576.0;
627 printf("%.1fMB in %d segments of size %d\n", fssize,
628 lfs_sb_getnseg(fs), ssize);
629
630 /*
631 * The number of free blocks is set from the number of segments
632 * times the segment size - lfs_minfreesegs (that we never write
633 * because we need to make sure the cleaner can run). Then
634 * we'll subtract off the room for the superblocks ifile entries
635 * and segment usage table, and half a block per segment that can't
636 * be written due to fragmentation.
637 */
638 lfs_sb_setdsize(fs,
639 lfs_segtod(fs, lfs_sb_getnseg(fs) - lfs_sb_getminfreeseg(fs)));
640 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs));
641 lfs_sb_subbfree(fs, LFS_DBTOFSB(fs, ((lfs_sb_getnseg(fs) / 2) <<
642 lfs_sb_getblktodb(fs))));
643
644 lfs_sb_setsegtabsz(fs, SEGTABSIZE_SU(fs));
645 lfs_sb_setcleansz(fs, CLEANSIZE_SU(fs));
646 if (time(&stamp) == -1)
647 fatal("time: %s", strerror(errno));
648 lfs_sb_settstamp(fs, stamp);
649 if (version == 1)
650 lfs_sb_setotstamp(fs, stamp);
651
652 if ((sb_interval = lfs_sb_getnseg(fs) / LFS_MAXNUMSB) < LFS_MIN_SBINTERVAL)
653 sb_interval = LFS_MIN_SBINTERVAL;
654
655 /*
656 * Figure out where the superblocks are going to live.
657 *
658 * Make segment 0 start at either zero, or LFS_LABELPAD, or
659 * >= LFS_SBPAD+LFS_LABELPAD, in order to prevent segment 0
660 * from having half a superblock in it.
661 */
662 if (LFS_FSBTODB(fs, LFS_DBTOFSB(fs, start)) != start)
663 fatal("Segment 0 offset is not multiple of frag size\n");
664 if (start != 0 && dbtob(start) != LFS_LABELPAD &&
665 dbtob(start) < LFS_SBPAD + LFS_LABELPAD) {
666 fatal("Using flags \"-O %" PRId64 "\" would result in the "
667 "first segment containing only\npart of a superblock. "
668 "Please choose an offset of 0, %d, or %d or more,\n",
669 start, btodb(LFS_LABELPAD),
670 btodb(LFS_LABELPAD + LFS_SBPAD));
671 }
672 lfs_sb_setsboff(fs, 0, label_fsb);
673 if (version == 1)
674 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
675 else
676 lfs_sb_sets0addr(fs, LFS_DBTOFSB(fs, start));
677 lfs_sb_subdsize(fs, sb_fsb);
678 for (i = 1; i < LFS_MAXNUMSB; i++) {
679 sb_addr = ((i * sb_interval) * lfs_segtod(fs, 1))
680 + lfs_sb_getsboff(fs, 0);
681 /* Segment 0 eats the label, except for version 1 */
682 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < label_fsb)
683 sb_addr -= label_fsb - start;
684 if (sb_addr + sizeof(struct dlfs)
685 >= LFS_DBTOFSB(fs, dkw->dkw_size))
686 break;
687 lfs_sb_setsboff(fs, i, sb_addr);
688 lfs_sb_subdsize(fs, sb_fsb);
689 }
690
691 /* We need >= 2 superblocks */
692 if (lfs_sb_getsboff(fs, 1) == 0x0) {
693 fatal("Could not assign a disk address for the second "
694 "superblock.\nPlease decrease the segment size.\n");
695 }
696
697 lfs_sb_setlastseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 2));
698 lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_sb_getnseg(fs) - 1));
699 lfs_sb_setoffset(fs, lfs_sntod(fs, lfs_sb_getnseg(fs)));
700 lfs_sb_setnextseg(fs, lfs_sntod(fs, 0));
701
702 /*
703 * Initialize the Ifile inode. Do this before we do anything
704 * with the Ifile or segment tables.
705 *
706 * XXX: is there some reason this allocates a new dinode? we
707 * already have an empty one generated by vget.
708 */
709 dip = malloc(sizeof(*dip));
710 if (dip == NULL)
711 err(1, NULL);
712 memset(dip, 0, sizeof(*dip));
713
714 VTOI(fs->lfs_ivnode)->i_din = dip;
715
716 lfs_dino_setmode(fs, dip, LFS_IFREG | 0600);
717 lfs_dino_setflags(fs, dip, SF_IMMUTABLE);
718 make_dinode(LFS_IFILE_INUM, dip,
719 lfs_blkstofrags(fs, lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1), fs);
720 lfs_dino_setsize(fs, dip, (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs) + 1) << lfs_sb_getbshift(fs));
721 for (i = 0; i < ULFS_NDADDR && i < (lfs_dino_getsize(fs, dip) >> lfs_sb_getbshift(fs)); i++)
722 VTOI(fs->lfs_ivnode)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
723
724 /*
725 * Set up in-superblock segment usage cache
726 */
727 fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
728 if (fs->lfs_suflags == NULL)
729 err(1, NULL);
730 fs->lfs_suflags[0] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
731 if (fs->lfs_suflags[0] == NULL)
732 err(1, NULL);
733 fs->lfs_suflags[1] = (u_int32_t *) malloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
734 if (fs->lfs_suflags[1] == NULL)
735 err(1, NULL);
736
737 /*
738 * Initialize the cleanerinfo block
739 */
740 LFS_CLEANERINFO(cip, fs, bp);
741 lfs_ci_setclean(fs, cip, lfs_sb_getnseg(fs));
742 lfs_ci_setdirty(fs, cip, 0);
743 if (version > 1) {
744 lfs_ci_setfree_head(fs, cip, HIGHEST_USED_INO + 1);
745 lfs_ci_setfree_tail(fs, cip, lfs_sb_getifpb(fs) - 1);
746 }
747 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
748
749 /*
750 * Run through segment table and initialize that
751 */
752 for (i = j = 0; i < lfs_sb_getnseg(fs); i++) {
753 LFS_SEGENTRY(segp, fs, i, bp);
754
755 if (i == 0 &&
756 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD + LFS_SBPAD)) {
757 segp->su_flags = SEGUSE_SUPERBLOCK;
758 lfs_sb_subbfree(fs, sb_fsb);
759 ++j;
760 }
761 if (i > 0) {
762 if ((i % sb_interval) == 0 && j < LFS_MAXNUMSB) {
763 segp->su_flags = SEGUSE_SUPERBLOCK;
764 lfs_sb_subbfree(fs, sb_fsb);
765 ++j;
766 } else
767 segp->su_flags = 0;
768 }
769 segp->su_lastmod = 0;
770 segp->su_nbytes = 0;
771 segp->su_ninos = 0;
772 segp->su_nsums = 0;
773
774 LFS_WRITESEGENTRY(segp, fs, i, bp);
775 }
776
777 /* Initialize root directory */
778 vp = lfs_raw_vget(fs, ULFS_ROOTINO, devfd, 0x0);
779 dip = VTOI(vp)->i_din;
780 make_dinode(ULFS_ROOTINO, dip, howmany(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)), fs);
781 lfs_dino_setmode(fs, dip, LFS_IFDIR | UMASK);
782 VTOI(vp)->i_lfs_osize = LFS_DIRBLKSIZ;
783 #ifdef MAKE_LF_DIR
784 VTOI(vp)->i_nlink = 3;
785 #else
786 VTOI(vp)->i_nlink = 2;
787 #endif
788 VTOI(vp)->i_lfs_effnblks =
789 lfs_btofsb(fs, roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs)));
790 lfs_dino_setsize(fs, dip, VTOI(vp)->i_lfs_osize);
791 lfs_dino_setnlink(fs, dip, VTOI(vp)->i_nlink);
792 lfs_dino_setblocks(fs, dip, VTOI(vp)->i_lfs_effnblks);
793 for (i = 0; i < ULFS_NDADDR && i < howmany(LFS_DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
794 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
795 if (LFS_DIRBLKSIZ < lfs_sb_getbsize(fs))
796 VTOI(vp)->i_lfs_fragsize[i - 1] =
797 roundup(LFS_DIRBLKSIZ, lfs_sb_getfsize(fs));
798 bread(vp, 0, lfs_sb_getfsize(fs), 0, &bp);
799 make_dir(bp->b_data, lfs_root_dir,
800 sizeof(lfs_root_dir) / sizeof(struct lfs_direct));
801 VOP_BWRITE(bp);
802
803 #ifdef MAKE_LF_DIR
804 /* Initialize lost+found directory */
805 vp = lfs_raw_vget(fs, LOSTFOUNDINO, devfd, 0x0);
806 dip = VTOI(vp)->i_din.ffs1_din;
807 make_dinode(LOSTFOUNDINO, dip, howmany(DIRBLKSIZ,fs->lfs_fsize), fs);
808 dip->di_mode = IFDIR | UMASK;
809 VTOI(vp)->i_lfs_osize = dip->di_size = DIRBLKSIZ;
810 VTOI(vp)->i_nlink = dip->di_nlink = 2;
811 VTOI(vp)->i_lfs_effnblks = dip->di_blocks =
812 lfs_btofsb(fs, roundup(DIRBLKSIZ,fs->lfs_fsize));
813 for (i = 0; i < ULFS_NDADDR && i < howmany(DIRBLKSIZ, lfs_sb_getbsize(fs)); i++)
814 VTOI(vp)->i_lfs_fragsize[i] = lfs_sb_getbsize(fs);
815 if (DIRBLKSIZ < lfs_sb_getbsize(fs))
816 VTOI(vp)->i_lfs_fragsize[i - 1] =
817 roundup(DIRBLKSIZ,fs->lfs_fsize);
818 bread(vp, 0, fs->lfs_fsize, 0, &bp);
819 make_dir(bp->b_data, lfs_lf_dir,
820 sizeof(lfs_lf_dir) / sizeof(struct lfs_direct));
821 VOP_BWRITE(bp);
822 #endif /* MAKE_LF_DIR */
823
824 /* Set their IFILE entry version numbers to 1 */
825 LFS_IENTRY(ipall, fs, 1, bp);
826 if (is64) {
827 ip64 = &ipall->u_64;
828 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
829 ip64->if_version = 1;
830 ip64->if_daddr = 0x0;
831 ip64->if_nextfree = 0;
832 ++ip64;
833 }
834 } else if (version > 1) {
835 ip32 = &ipall->u_32;
836 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
837 ip32->if_version = 1;
838 ip32->if_daddr = 0x0;
839 ip32->if_nextfree = 0;
840 ++ip32;
841 }
842 } else {
843 ip_v1 = &ipall->u_v1;
844 for (i = LFS_IFILE_INUM; i <= HIGHEST_USED_INO; i++) {
845 ip_v1->if_version = 1;
846 ip_v1->if_daddr = 0x0;
847 ip_v1->if_nextfree = 0;
848 ++ip_v1;
849 }
850 }
851 /* Link remaining IFILE entries in free list */
852 if (is64) {
853 for (;
854 i < lfs_sb_getifpb(fs); ++ip64) {
855 ip64->if_version = 1;
856 ip64->if_daddr = LFS_UNUSED_DADDR;
857 ip64->if_nextfree = ++i;
858 }
859 --ip64;
860 ip64->if_nextfree = LFS_UNUSED_INUM;
861 } else if (version > 1) {
862 for (;
863 i < lfs_sb_getifpb(fs); ++ip32) {
864 ip32->if_version = 1;
865 ip32->if_daddr = LFS_UNUSED_DADDR;
866 ip32->if_nextfree = ++i;
867 }
868 --ip32;
869 ip32->if_nextfree = LFS_UNUSED_INUM;
870 } else {
871 for (;
872 i < lfs_sb_getifpb(fs); ++ip_v1) {
873 ip_v1->if_version = 1;
874 ip_v1->if_daddr = LFS_UNUSED_DADDR;
875 ip_v1->if_nextfree = ++i;
876 }
877 --ip_v1;
878 ip_v1->if_nextfree = LFS_UNUSED_INUM;
879 }
880 VOP_BWRITE(bp);
881
882 /* Write it all to disk. */
883 if (!Nflag)
884 lfs_segwrite(fs, SEGM_CKP);
885
886 /*
887 * Now that we've written everything, look to see what's available
888 * for writing.
889 */
890 lfs_sb_setavail(fs, 0);
891 bb = ubb = dmeta = 0;
892 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
893 LFS_SEGENTRY(segp, fs, i, bp);
894 if (segp->su_flags & SEGUSE_DIRTY) {
895 bb += lfs_btofsb(fs, segp->su_nbytes +
896 segp->su_nsums * lfs_sb_getsumsize(fs));
897 ubb += lfs_btofsb(fs, segp->su_nbytes +
898 segp->su_nsums * lfs_sb_getsumsize(fs) +
899 segp->su_ninos * lfs_sb_getibsize(fs));
900 dmeta += lfs_btofsb(fs,
901 lfs_sb_getsumsize(fs) * segp->su_nsums);
902 dmeta += lfs_btofsb(fs,
903 lfs_sb_getibsize(fs) * segp->su_ninos);
904 } else {
905 lfs_sb_addavail(fs, lfs_segtod(fs, 1));
906 if (segp->su_flags & SEGUSE_SUPERBLOCK)
907 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_SBPAD));
908 if (i == 0 && lfs_sb_getversion(fs) > 1 &&
909 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
910 lfs_sb_subavail(fs, lfs_btofsb(fs, LFS_LABELPAD) -
911 lfs_sb_gets0addr(fs));
912 }
913 brelse(bp, 0);
914 }
915 /* Also may be available bytes in current seg */
916 i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
917 lfs_sb_addavail(fs, lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs));
918 /* But do not count minfreesegs */
919 lfs_sb_subavail(fs, lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) - (lfs_sb_getminfreeseg(fs) / 2))));
920
921 labelskew = 0;
922 if (lfs_sb_getversion(fs) > 1 && lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
923 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
924 lfs_sb_setbfree(fs, lfs_sb_getdsize(fs) - labelskew - (ubb + bb) / 2);
925
926 /* Put that in the Ifile version too, and write it */
927 LFS_CLEANERINFO(cip, fs, bp);
928 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs));
929 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs));
930 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
931 if (!Nflag)
932 lfs_segwrite(fs, SEGM_CKP);
933
934 /*
935 * Finally write out superblocks.
936 */
937 printf("super-block backups (for fsck -b #) at:\n");
938 curw = 0;
939 for (i = 0; i < LFS_MAXNUMSB; i++) {
940 seg_addr = lfs_sb_getsboff(fs, i);
941 if (seg_addr == 0)
942 break;
943
944 if (i != 0)
945 curw += printf(", ");
946 ww = snprintf(tbuf, sizeof(tbuf), "%lld",
947 (long long)LFS_FSBTODB(fs, seg_addr));
948 curw += ww;
949 if (curw >= 78) {
950 printf("\n%s", tbuf);
951 curw = ww;
952 } else
953 printf("%s", tbuf);
954 fflush(stdout);
955
956 /* Leave the time stamp on the alt sb, zero the rest */
957 if (i == 2) {
958 lfs_sb_settstamp(fs, 0);
959 lfs_sb_setcksum(fs, lfs_sb_cksum(fs));
960 }
961 if (!Nflag)
962 lfs_writesuper(fs, seg_addr);
963 }
964 printf(".\n");
965
966 return 0;
967 }
968
969 /*
970 * Compatibility with fsck_lfs, since the "generic" LFS userland code uses it.
971 */
972 void
973 pwarn(const char *fmt, ...)
974 {
975 va_list ap;
976
977 va_start(ap, fmt);
978 vfprintf(stderr, fmt, ap);
979 va_end(ap);
980 }
981