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