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