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