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