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