1*cca4ee03Skevlo /* $OpenBSD: mke2fs.c,v 1.18 2019/07/01 07:17:26 kevlo Exp $ */
2b2753aa5Sotto /* $NetBSD: mke2fs.c,v 1.13 2009/10/19 18:41:08 bouyer Exp $ */
3b2753aa5Sotto
4b2753aa5Sotto /*-
5b2753aa5Sotto * Copyright (c) 2007 Izumi Tsutsui. All rights reserved.
6b2753aa5Sotto *
7b2753aa5Sotto * Redistribution and use in source and binary forms, with or without
8b2753aa5Sotto * modification, are permitted provided that the following conditions
9b2753aa5Sotto * are met:
10b2753aa5Sotto * 1. Redistributions of source code must retain the above copyright
11b2753aa5Sotto * notice, this list of conditions and the following disclaimer.
12b2753aa5Sotto * 2. Redistributions in binary form must reproduce the above copyright
13b2753aa5Sotto * notice, this list of conditions and the following disclaimer in the
14b2753aa5Sotto * documentation and/or other materials provided with the distribution.
15b2753aa5Sotto *
16b2753aa5Sotto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17b2753aa5Sotto * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18b2753aa5Sotto * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19b2753aa5Sotto * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20b2753aa5Sotto * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21b2753aa5Sotto * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22b2753aa5Sotto * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23b2753aa5Sotto * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24b2753aa5Sotto * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25b2753aa5Sotto * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b2753aa5Sotto */
27b2753aa5Sotto
28b2753aa5Sotto /*
29b2753aa5Sotto * Copyright (c) 1980, 1989, 1993
30b2753aa5Sotto * The Regents of the University of California. All rights reserved.
31b2753aa5Sotto *
32b2753aa5Sotto * Redistribution and use in source and binary forms, with or without
33b2753aa5Sotto * modification, are permitted provided that the following conditions
34b2753aa5Sotto * are met:
35b2753aa5Sotto * 1. Redistributions of source code must retain the above copyright
36b2753aa5Sotto * notice, this list of conditions and the following disclaimer.
37b2753aa5Sotto * 2. Redistributions in binary form must reproduce the above copyright
38b2753aa5Sotto * notice, this list of conditions and the following disclaimer in the
39b2753aa5Sotto * documentation and/or other materials provided with the distribution.
40b2753aa5Sotto * 3. Neither the name of the University nor the names of its contributors
41b2753aa5Sotto * may be used to endorse or promote products derived from this software
42b2753aa5Sotto * without specific prior written permission.
43b2753aa5Sotto *
44b2753aa5Sotto * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45b2753aa5Sotto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46b2753aa5Sotto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47b2753aa5Sotto * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48b2753aa5Sotto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49b2753aa5Sotto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50b2753aa5Sotto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51b2753aa5Sotto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52b2753aa5Sotto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53b2753aa5Sotto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54b2753aa5Sotto * SUCH DAMAGE.
55b2753aa5Sotto */
56b2753aa5Sotto
57b2753aa5Sotto /*
58b2753aa5Sotto * Copyright (c) 1997 Manuel Bouyer.
59b2753aa5Sotto *
60b2753aa5Sotto * Redistribution and use in source and binary forms, with or without
61b2753aa5Sotto * modification, are permitted provided that the following conditions
62b2753aa5Sotto * are met:
63b2753aa5Sotto * 1. Redistributions of source code must retain the above copyright
64b2753aa5Sotto * notice, this list of conditions and the following disclaimer.
65b2753aa5Sotto * 2. Redistributions in binary form must reproduce the above copyright
66b2753aa5Sotto * notice, this list of conditions and the following disclaimer in the
67b2753aa5Sotto * documentation and/or other materials provided with the distribution.
68b2753aa5Sotto *
69b2753aa5Sotto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
70b2753aa5Sotto * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
71b2753aa5Sotto * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
72b2753aa5Sotto * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
73b2753aa5Sotto * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
74b2753aa5Sotto * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
75b2753aa5Sotto * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
76b2753aa5Sotto * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
77b2753aa5Sotto * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
78b2753aa5Sotto * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79b2753aa5Sotto */
80b2753aa5Sotto
81b2753aa5Sotto /*
82b2753aa5Sotto * mke2fs.c: "re-invent (dumb but non-GPLed) wheel as a fun project"
83b2753aa5Sotto *
84b2753aa5Sotto * In spite of this name, there is no piece of code
85b2753aa5Sotto * derived from GPLed e2fsprogs written for Linux.
86b2753aa5Sotto * I referred them only to see how each structure
87b2753aa5Sotto * member should be initialized.
88b2753aa5Sotto *
89b2753aa5Sotto * Reference:
90b2753aa5Sotto * - All NetBSD sources under src/sys/ufs/ext2fs and src/sbin/fsck_ext2fs
91b2753aa5Sotto * - Ext2fs Home Page
92b2753aa5Sotto * http://e2fsprogs.sourceforge.net/ext2.html
93b2753aa5Sotto * - Design and Implementation of the Second Extended Filesystem
94b2753aa5Sotto * http://e2fsprogs.sourceforge.net/ext2intro.html
95b2753aa5Sotto * - Linux Documentation "The Second Extended Filesystem"
96b2753aa5Sotto * src/linux/Documentation/filesystems/ext2.txt
97b2753aa5Sotto * in the Linux kernel distribution
98b2753aa5Sotto */
99b2753aa5Sotto
100b9fc9a72Sderaadt #include <sys/param.h> /* MAXBSIZE powerof2 roundup setbit isset MIN */
101b9fc9a72Sderaadt #include <sys/types.h>
102b2753aa5Sotto #include <sys/mman.h>
103b2753aa5Sotto #include <sys/time.h>
104b2753aa5Sotto #include <ufs/ext2fs/ext2fs_dinode.h>
105b2753aa5Sotto #include <ufs/ext2fs/ext2fs_dir.h>
106b2753aa5Sotto #include <ufs/ext2fs/ext2fs.h>
107b2753aa5Sotto #include <sys/ioctl.h>
108b2753aa5Sotto
109b2753aa5Sotto #include <err.h>
110b2753aa5Sotto #include <errno.h>
111b2753aa5Sotto #include <inttypes.h>
112b2753aa5Sotto #include <string.h>
113b2753aa5Sotto #include <unistd.h>
114b2753aa5Sotto #include <stdint.h>
115b2753aa5Sotto #include <stdlib.h>
116b2753aa5Sotto #include <stddef.h>
117b2753aa5Sotto #include <stdio.h>
118b2753aa5Sotto
119b2753aa5Sotto #include "extern.h"
120b2753aa5Sotto
121b2753aa5Sotto static void initcg(uint);
122b6d2e2d5Sderaadt static void zap_old_sblock(daddr32_t);
123b2753aa5Sotto static uint cgoverhead(uint);
124b2753aa5Sotto static int fsinit(const struct timeval *);
125b2753aa5Sotto static int makedir(struct ext2fs_direct *, int);
126b2753aa5Sotto static void copy_dir(struct ext2fs_direct *, struct ext2fs_direct *);
127b2753aa5Sotto static void init_resizeino(const struct timeval *);
128b2753aa5Sotto static uint32_t alloc(uint32_t, uint16_t);
129b2753aa5Sotto static void iput(struct ext2fs_dinode *, ino_t);
130b6d2e2d5Sderaadt static void rdfs(daddr32_t, int, void *);
131b6d2e2d5Sderaadt static void wtfs(daddr32_t, int, void *);
132b2753aa5Sotto static int ilog2(uint);
133b2753aa5Sotto static int skpc(int, size_t, uint8_t *);
134b2753aa5Sotto static void uuid_get(struct m_ext2fs *);
135b2753aa5Sotto
136b2753aa5Sotto /* XXX: some of these macro should be into <ufs/ext2fs/ext2fs.h>? */
137b2753aa5Sotto #define EXT2_DEF_MAX_MNT_COUNT 20
138b2753aa5Sotto #define EXT2_DEF_FSCKINTV (180 * 24 * 60 * 60) /* 180 days */
139b2753aa5Sotto #define EXT2_RESERVED_INODES (EXT2_FIRSTINO - 1)
140b2753aa5Sotto #define EXT2_UMASK 0755
141b2753aa5Sotto
142b2753aa5Sotto #define EXT2_INO_INDEX(ino) ((ino) - 1) /* no inode zero */
143b2753aa5Sotto
144b2753aa5Sotto #define EXT2_LOSTFOUNDSIZE 16384
145b2753aa5Sotto #define EXT2_LOSTFOUNDINO EXT2_FIRSTINO /* XXX: not quite */
146b2753aa5Sotto #define EXT2_LOSTFOUNDUMASK 0700
147b2753aa5Sotto
148b2753aa5Sotto #define EXT2_RESIZEINOUMASK 0600
149b2753aa5Sotto
150b2753aa5Sotto #define NBLOCK_SUPERBLOCK 1
151b2753aa5Sotto #define NBLOCK_BLOCK_BITMAP 1
152b2753aa5Sotto #define NBLOCK_INODE_BITMAP 1
153b2753aa5Sotto
154b2753aa5Sotto #define cgbase(fs, c) \
155b2753aa5Sotto ((fs)->e2fs.e2fs_first_dblock + (fs)->e2fs.e2fs_bpg * (c))
156b2753aa5Sotto
157b2753aa5Sotto #define rounddown(x,y) (((x)/(y))*(y))
158b2753aa5Sotto
159b2753aa5Sotto /*
160b2753aa5Sotto * ext2fs super block and group descriptor structures
161b2753aa5Sotto *
162b2753aa5Sotto * We don't have to use or setup whole in-memory m_ext2fs structure,
163b2753aa5Sotto * but prepare it to use several macro defined in kernel headers.
164b2753aa5Sotto */
165b2753aa5Sotto union {
166b2753aa5Sotto struct m_ext2fs m_ext2fs;
167b2753aa5Sotto char pad[SBSIZE];
168b2753aa5Sotto } ext2fsun;
169b2753aa5Sotto #define sblock ext2fsun.m_ext2fs
170b2753aa5Sotto #define gd ext2fsun.m_ext2fs.e2fs_gd
171b2753aa5Sotto
172b2753aa5Sotto static uint8_t *iobuf; /* for superblock and group descriptors */
173b2753aa5Sotto static int iobufsize;
174b2753aa5Sotto
175b2753aa5Sotto static uint8_t buf[MAXBSIZE]; /* for initcg() and makedir() ops */
176b2753aa5Sotto
177bed75cd5Snatano static int fd;
178b2753aa5Sotto
1794a93462bSderaadt extern int max_cols;
1804a93462bSderaadt
181b2753aa5Sotto void
mke2fs(const char * fsys,int f)182bed75cd5Snatano mke2fs(const char *fsys, int f)
183b2753aa5Sotto {
184b2753aa5Sotto struct timeval tv;
185b2753aa5Sotto int64_t minfssize;
186b2753aa5Sotto uint bcount, fbcount, ficount;
187b2753aa5Sotto uint blocks_gd, blocks_per_cg, inodes_per_cg, iblocks_per_cg;
188b2753aa5Sotto uint minblocks_per_cg, blocks_lastcg;
189b2753aa5Sotto uint ncg, cylno, sboff;
1904a93462bSderaadt int i, len, col, delta, fld_width;
191b2753aa5Sotto
192b2753aa5Sotto gettimeofday(&tv, NULL);
193bed75cd5Snatano fd = f;
194b2753aa5Sotto
195b2753aa5Sotto /*
196b2753aa5Sotto * collect and verify the block and fragment sizes
197b2753aa5Sotto */
198b2753aa5Sotto if (!powerof2(bsize)) {
199b2753aa5Sotto errx(EXIT_FAILURE,
200b2753aa5Sotto "block size must be a power of 2, not %u\n",
201b2753aa5Sotto bsize);
202b2753aa5Sotto }
203b2753aa5Sotto if (!powerof2(fsize)) {
204b2753aa5Sotto errx(EXIT_FAILURE,
205b2753aa5Sotto "fragment size must be a power of 2, not %u\n",
206b2753aa5Sotto fsize);
207b2753aa5Sotto }
208b2753aa5Sotto if (fsize < sectorsize) {
209b2753aa5Sotto errx(EXIT_FAILURE,
210b2753aa5Sotto "fragment size %u is too small, minimum is %u\n",
211b2753aa5Sotto fsize, sectorsize);
212b2753aa5Sotto }
213b2753aa5Sotto if (bsize < MINBSIZE) {
214b2753aa5Sotto errx(EXIT_FAILURE,
215b2753aa5Sotto "block size %u is too small, minimum is %u\n",
216b2753aa5Sotto bsize, MINBSIZE);
217b2753aa5Sotto }
218b2753aa5Sotto if (bsize > EXT2_MAXBSIZE) {
219b2753aa5Sotto errx(EXIT_FAILURE,
220b2753aa5Sotto "block size %u is too large, maximum is %u\n",
221b2753aa5Sotto bsize, MAXBSIZE);
222b2753aa5Sotto }
223b2753aa5Sotto if (bsize != fsize) {
224b2753aa5Sotto /*
225b2753aa5Sotto * There is no fragment support on current ext2fs (yet?),
226b2753aa5Sotto * but some kernel code refers fsize or fpg as bsize or bpg
227b2753aa5Sotto * and Linux seems to set the same values to them.
228b2753aa5Sotto */
229b2753aa5Sotto errx(EXIT_FAILURE,
230b2753aa5Sotto "block size (%u) can't be different from "
231b2753aa5Sotto "fragment size (%u)\n",
232b2753aa5Sotto bsize, fsize);
233b2753aa5Sotto }
234b2753aa5Sotto
235b2753aa5Sotto /* variable inodesize is REV1 feature */
236b2753aa5Sotto if (Oflag == 0 && inodesize != EXT2_REV0_DINODE_SIZE) {
237b2753aa5Sotto errx(EXIT_FAILURE, "GOOD_OLD_REV file system format"
238b2753aa5Sotto " doesn't support %d byte inode\n", inodesize);
239b2753aa5Sotto }
240b2753aa5Sotto
241b2753aa5Sotto sblock.e2fs.e2fs_log_bsize = ilog2(bsize) - LOG_MINBSIZE;
242fdcf9035Spelikan sblock.e2fs.e2fs_log_fsize = ilog2(fsize) - LOG_MINFSIZE;
243b2753aa5Sotto
244b2753aa5Sotto sblock.e2fs_bsize = bsize;
245fdcf9035Spelikan sblock.e2fs_fsize = fsize;
246b2753aa5Sotto sblock.e2fs_bshift = sblock.e2fs.e2fs_log_bsize + LOG_MINBSIZE;
247b2753aa5Sotto sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
248b2753aa5Sotto sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
249b2753aa5Sotto sblock.e2fs_fsbtodb = ilog2(sblock.e2fs_bsize) - ilog2(sectorsize);
250b2753aa5Sotto sblock.e2fs_ipb = sblock.e2fs_bsize / inodesize;
251b2753aa5Sotto
252b2753aa5Sotto /*
253b2753aa5Sotto * Ext2fs preserves BBSIZE (1024 bytes) space at the top for
254b2753aa5Sotto * bootloader (though it is not enough at all for our bootloader).
255b2753aa5Sotto * If bsize == BBSIZE we have to preserve one block.
256b2753aa5Sotto * If bsize > BBSIZE, the first block already contains BBSIZE space
257b2753aa5Sotto * before superblock because superblock is allocated at SBOFF and
258b2753aa5Sotto * bsize is a power of two (i.e. 2048 bytes or more).
259b2753aa5Sotto */
260b2753aa5Sotto sblock.e2fs.e2fs_first_dblock = (sblock.e2fs_bsize > BBSIZE) ? 0 : 1;
261b2753aa5Sotto minfssize = fsbtodb(&sblock,
262b2753aa5Sotto sblock.e2fs.e2fs_first_dblock +
263b2753aa5Sotto NBLOCK_SUPERBLOCK +
264b2753aa5Sotto 1 /* at least one group descriptor */ +
265b2753aa5Sotto NBLOCK_BLOCK_BITMAP +
266b2753aa5Sotto NBLOCK_INODE_BITMAP +
267b2753aa5Sotto 1 /* at least one inode table block */ +
268b2753aa5Sotto 1 /* at least one data block for rootdir */ +
269b2753aa5Sotto 1 /* at least one data block for data */
270b2753aa5Sotto ); /* XXX and more? */
271b2753aa5Sotto
272b2753aa5Sotto if (fssize < minfssize)
273b2753aa5Sotto errx(EXIT_FAILURE, "Filesystem size %" PRId64
274b2753aa5Sotto " < minimum size of %" PRId64 "\n", fssize, minfssize);
275b2753aa5Sotto
276b2753aa5Sotto bcount = dbtofsb(&sblock, fssize);
277b2753aa5Sotto
278b2753aa5Sotto /*
279b2753aa5Sotto * While many people claim that ext2fs is a (bad) clone of ufs/ffs,
280b2753aa5Sotto * it isn't actual ffs so maybe we should call it "block group"
281b2753aa5Sotto * as their native name rather than ffs derived "cylinder group."
282b2753aa5Sotto * But we'll use the latter here since other kernel sources use it.
283b2753aa5Sotto * (I also agree "cylinder" based allocation is obsolete though)
284b2753aa5Sotto */
285b2753aa5Sotto
286b2753aa5Sotto /* maybe "simple is the best" */
287b2753aa5Sotto blocks_per_cg = sblock.e2fs_bsize * NBBY;
288b2753aa5Sotto
289b2753aa5Sotto ncg = howmany(bcount - sblock.e2fs.e2fs_first_dblock, blocks_per_cg);
290b2753aa5Sotto blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
291b2753aa5Sotto
292b2753aa5Sotto /* check range of inode number */
293b2753aa5Sotto if (num_inodes < EXT2_FIRSTINO)
294b2753aa5Sotto num_inodes = EXT2_FIRSTINO; /* needs reserved inodes + 1 */
295b2753aa5Sotto if (num_inodes > UINT16_MAX * ncg)
296b2753aa5Sotto num_inodes = UINT16_MAX * ncg; /* ext2bgd_nifree is uint16_t */
297b2753aa5Sotto
298b2753aa5Sotto inodes_per_cg = num_inodes / ncg;
299b2753aa5Sotto iblocks_per_cg = howmany(inodesize * inodes_per_cg, bsize);
300b2753aa5Sotto
301b2753aa5Sotto /* Check that the last cylinder group has enough space for inodes */
302b2753aa5Sotto minblocks_per_cg =
303b2753aa5Sotto NBLOCK_BLOCK_BITMAP +
304b2753aa5Sotto NBLOCK_INODE_BITMAP +
305b2753aa5Sotto iblocks_per_cg +
306b2753aa5Sotto 1; /* at least one data block */
307b2753aa5Sotto if (Oflag == 0 || cg_has_sb(ncg - 1) != 0)
308b2753aa5Sotto minblocks_per_cg += NBLOCK_SUPERBLOCK + blocks_gd;
309b2753aa5Sotto
310b2753aa5Sotto blocks_lastcg = bcount - sblock.e2fs.e2fs_first_dblock -
311b2753aa5Sotto blocks_per_cg * (ncg - 1);
312b2753aa5Sotto if (blocks_lastcg < minblocks_per_cg) {
313b2753aa5Sotto /*
314b2753aa5Sotto * Since we make all the cylinder groups the same size, the
315b2753aa5Sotto * last will only be small if there are more than one
316b2753aa5Sotto * cylinder groups. If the last one is too small to store
317b2753aa5Sotto * filesystem data, just kill it.
318b2753aa5Sotto *
319b2753aa5Sotto * XXX: Does fsck_ext2fs(8) properly handle this case?
320b2753aa5Sotto */
321b2753aa5Sotto bcount -= blocks_lastcg;
322b2753aa5Sotto ncg--;
323b2753aa5Sotto blocks_lastcg = blocks_per_cg;
324b2753aa5Sotto blocks_gd = howmany(sizeof(struct ext2_gd) * ncg, bsize);
325b2753aa5Sotto inodes_per_cg = num_inodes / ncg;
326b2753aa5Sotto }
327b2753aa5Sotto /* roundup inodes_per_cg to make it use whole inode table blocks */
328b2753aa5Sotto inodes_per_cg = roundup(inodes_per_cg, sblock.e2fs_ipb);
329b2753aa5Sotto num_inodes = inodes_per_cg * ncg;
330b2753aa5Sotto iblocks_per_cg = inodes_per_cg / sblock.e2fs_ipb;
331b2753aa5Sotto
332b2753aa5Sotto /* XXX: probably we should check these adjusted values again */
333b2753aa5Sotto
334b2753aa5Sotto sblock.e2fs.e2fs_bcount = bcount;
335b2753aa5Sotto sblock.e2fs.e2fs_icount = num_inodes;
336b2753aa5Sotto
337b2753aa5Sotto sblock.e2fs_ncg = ncg;
338b2753aa5Sotto sblock.e2fs_ngdb = blocks_gd;
339b2753aa5Sotto sblock.e2fs_itpg = iblocks_per_cg;
340b2753aa5Sotto
341b2753aa5Sotto sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * minfree / 100;
342b2753aa5Sotto /* e2fs_fbcount will be accounted later */
343b2753aa5Sotto /* e2fs_ficount will be accounted later */
344b2753aa5Sotto
345b2753aa5Sotto sblock.e2fs.e2fs_bpg = blocks_per_cg;
346b2753aa5Sotto sblock.e2fs.e2fs_fpg = blocks_per_cg;
347b2753aa5Sotto
348b2753aa5Sotto sblock.e2fs.e2fs_ipg = inodes_per_cg;
349b2753aa5Sotto
350b2753aa5Sotto sblock.e2fs.e2fs_mtime = 0;
3513917bbadSderaadt sblock.e2fs.e2fs_wtime = (u_int32_t)tv.tv_sec;
352b2753aa5Sotto sblock.e2fs.e2fs_mnt_count = 0;
353b2753aa5Sotto /* XXX: should add some entropy to avoid checking all fs at once? */
354b2753aa5Sotto sblock.e2fs.e2fs_max_mnt_count = EXT2_DEF_MAX_MNT_COUNT;
355b2753aa5Sotto
356b2753aa5Sotto sblock.e2fs.e2fs_magic = E2FS_MAGIC;
357b2753aa5Sotto sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
358b2753aa5Sotto sblock.e2fs.e2fs_beh = E2FS_BEH_DEFAULT;
359b2753aa5Sotto sblock.e2fs.e2fs_minrev = 0;
3603917bbadSderaadt sblock.e2fs.e2fs_lastfsck = (u_int32_t)tv.tv_sec;
361b2753aa5Sotto sblock.e2fs.e2fs_fsckintv = EXT2_DEF_FSCKINTV;
362b2753aa5Sotto
363b2753aa5Sotto /*
364b2753aa5Sotto * Maybe we can use E2FS_OS_FREEBSD here and it would be more proper,
365b2753aa5Sotto * but the purpose of this newfs_ext2fs(8) command is to provide
366b2753aa5Sotto * a filesystem which can be recognized by firmware on some
367b2753aa5Sotto * Linux based appliances that can load bootstrap files only from
368b2753aa5Sotto * (their native) ext2fs, and anyway we will (and should) try to
369b2753aa5Sotto * act like them as much as possible.
370b2753aa5Sotto *
371b2753aa5Sotto * Anyway, I hope that all newer such boxes will keep their support
372b2753aa5Sotto * for the "GOOD_OLD_REV" ext2fs.
373b2753aa5Sotto */
374b2753aa5Sotto sblock.e2fs.e2fs_creator = E2FS_OS_LINUX;
375b2753aa5Sotto
376b2753aa5Sotto if (Oflag == 0) {
377b2753aa5Sotto sblock.e2fs.e2fs_rev = E2FS_REV0;
378b2753aa5Sotto sblock.e2fs.e2fs_features_compat = 0;
379b2753aa5Sotto sblock.e2fs.e2fs_features_incompat = 0;
380b2753aa5Sotto sblock.e2fs.e2fs_features_rocompat = 0;
381b2753aa5Sotto } else {
382b2753aa5Sotto sblock.e2fs.e2fs_rev = E2FS_REV1;
383b2753aa5Sotto /*
384b2753aa5Sotto * e2fsprogs say "REV1" is "dynamic" so
385b2753aa5Sotto * it isn't quite a version and maybe it means
386b2753aa5Sotto * "extended from REV0 so check compat features."
387b2753aa5Sotto *
388b2753aa5Sotto * XXX: We don't have any native tool to activate
389b2753aa5Sotto * the EXT2F_COMPAT_RESIZE feature and
390b2753aa5Sotto * fsck_ext2fs(8) might not fix structures for it.
391b2753aa5Sotto */
392b2753aa5Sotto sblock.e2fs.e2fs_features_compat = EXT2F_COMPAT_RESIZE;
393b2753aa5Sotto sblock.e2fs.e2fs_features_incompat = EXT2F_INCOMPAT_FTYPE;
394b2753aa5Sotto sblock.e2fs.e2fs_features_rocompat =
395*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER | EXT2F_ROCOMPAT_LARGE_FILE;
396b2753aa5Sotto }
397b2753aa5Sotto
398b2753aa5Sotto sblock.e2fs.e2fs_ruid = geteuid();
399b2753aa5Sotto sblock.e2fs.e2fs_rgid = getegid();
400b2753aa5Sotto
401b2753aa5Sotto sblock.e2fs.e2fs_first_ino = EXT2_FIRSTINO;
402b2753aa5Sotto sblock.e2fs.e2fs_inode_size = inodesize;
403b2753aa5Sotto
404b2753aa5Sotto /* e2fs_block_group_nr is set on writing superblock to each group */
405b2753aa5Sotto
406b2753aa5Sotto uuid_get(&sblock);
407b2753aa5Sotto if (volname != NULL) {
408b2753aa5Sotto if (strlen(volname) > sizeof(sblock.e2fs.e2fs_vname))
409b2753aa5Sotto errx(EXIT_FAILURE, "Volume name is too long");
410b2753aa5Sotto strlcpy(sblock.e2fs.e2fs_vname, volname,
411b2753aa5Sotto sizeof(sblock.e2fs.e2fs_vname));
412b2753aa5Sotto }
413b2753aa5Sotto
414b2753aa5Sotto sblock.e2fs.e2fs_fsmnt[0] = '\0';
415b2753aa5Sotto sblock.e2fs_fsmnt[0] = '\0';
416b2753aa5Sotto
417b2753aa5Sotto sblock.e2fs.e2fs_algo = 0; /* XXX unsupported? */
418b2753aa5Sotto sblock.e2fs.e2fs_prealloc = 0; /* XXX unsupported? */
419b2753aa5Sotto sblock.e2fs.e2fs_dir_prealloc = 0; /* XXX unsupported? */
420b2753aa5Sotto
421b2753aa5Sotto /* calculate blocks for reserved group descriptors for resize */
422b2753aa5Sotto sblock.e2fs.e2fs_reserved_ngdb = 0;
423b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
424b2753aa5Sotto (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0) {
425b2753aa5Sotto uint64_t target_blocks;
426b2753aa5Sotto uint target_ncg, target_ngdb, reserved_ngdb;
427b2753aa5Sotto
428b2753aa5Sotto /* reserve descriptors for size as 1024 times as current */
429b2753aa5Sotto target_blocks =
430b2753aa5Sotto (sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock)
431b2753aa5Sotto * 1024ULL;
432b2753aa5Sotto /* number of blocks must be in uint32_t */
433b2753aa5Sotto if (target_blocks > UINT32_MAX)
434b2753aa5Sotto target_blocks = UINT32_MAX;
435b2753aa5Sotto target_ncg = howmany(target_blocks, sblock.e2fs.e2fs_bpg);
436b2753aa5Sotto target_ngdb = howmany(sizeof(struct ext2_gd) * target_ncg,
437b2753aa5Sotto sblock.e2fs_bsize);
438b2753aa5Sotto /*
439b2753aa5Sotto * Reserved group descriptor blocks are preserved as
440b2753aa5Sotto * the second level double indirect reference blocks in
441b2753aa5Sotto * the EXT2_RESIZEINO inode, so the maximum number of
442b2753aa5Sotto * the blocks is NINDIR(fs).
443b2753aa5Sotto * (see also descriptions in init_resizeino() function)
444b2753aa5Sotto *
445b2753aa5Sotto * We check a number including current e2fs_ngdb here
446b2753aa5Sotto * because they will be moved into reserved gdb on
447b2753aa5Sotto * possible future size shrink, though e2fsprogs don't
448b2753aa5Sotto * seem to care about it.
449b2753aa5Sotto */
450b2753aa5Sotto if (target_ngdb > NINDIR(&sblock))
451b2753aa5Sotto target_ngdb = NINDIR(&sblock);
452b2753aa5Sotto
453b2753aa5Sotto reserved_ngdb = target_ngdb - sblock.e2fs_ngdb;
454b2753aa5Sotto
455b2753aa5Sotto /* make sure reserved_ngdb fits in the last cg */
456b2753aa5Sotto if (reserved_ngdb >= blocks_lastcg - cgoverhead(ncg - 1))
457b2753aa5Sotto reserved_ngdb = blocks_lastcg - cgoverhead(ncg - 1);
458b2753aa5Sotto if (reserved_ngdb == 0) {
459b2753aa5Sotto /* if no space for reserved gdb, disable the feature */
460b2753aa5Sotto sblock.e2fs.e2fs_features_compat &=
461b2753aa5Sotto ~EXT2F_COMPAT_RESIZE;
462b2753aa5Sotto }
463b2753aa5Sotto sblock.e2fs.e2fs_reserved_ngdb = reserved_ngdb;
464b2753aa5Sotto }
465b2753aa5Sotto
466b2753aa5Sotto /*
467b2753aa5Sotto * Initialize group descriptors
468b2753aa5Sotto */
469b6eef8d7Sguenther gd = calloc(sblock.e2fs_ngdb, bsize);
470b2753aa5Sotto if (gd == NULL)
471b2753aa5Sotto errx(EXIT_FAILURE, "Can't allocate descriptors buffer");
472b2753aa5Sotto
473b2753aa5Sotto fbcount = 0;
474b2753aa5Sotto ficount = 0;
475b2753aa5Sotto for (cylno = 0; cylno < ncg; cylno++) {
476b2753aa5Sotto uint boffset;
477b2753aa5Sotto
478b2753aa5Sotto boffset = cgbase(&sblock, cylno);
479b2753aa5Sotto if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
480b2753aa5Sotto (sblock.e2fs.e2fs_features_rocompat &
481*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
482b2753aa5Sotto cg_has_sb(cylno)) {
483b2753aa5Sotto boffset += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
484b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
485b2753aa5Sotto (sblock.e2fs.e2fs_features_compat &
486b2753aa5Sotto EXT2F_COMPAT_RESIZE) != 0)
487b2753aa5Sotto boffset += sblock.e2fs.e2fs_reserved_ngdb;
488b2753aa5Sotto }
489b2753aa5Sotto gd[cylno].ext2bgd_b_bitmap = boffset;
490b2753aa5Sotto boffset += NBLOCK_BLOCK_BITMAP;
491b2753aa5Sotto gd[cylno].ext2bgd_i_bitmap = boffset;
492b2753aa5Sotto boffset += NBLOCK_INODE_BITMAP;
493b2753aa5Sotto gd[cylno].ext2bgd_i_tables = boffset;
494b2753aa5Sotto if (cylno == (ncg - 1))
495b2753aa5Sotto gd[cylno].ext2bgd_nbfree =
496b2753aa5Sotto blocks_lastcg - cgoverhead(cylno);
497b2753aa5Sotto else
498b2753aa5Sotto gd[cylno].ext2bgd_nbfree =
499b2753aa5Sotto sblock.e2fs.e2fs_bpg - cgoverhead(cylno);
500b2753aa5Sotto fbcount += gd[cylno].ext2bgd_nbfree;
501b2753aa5Sotto gd[cylno].ext2bgd_nifree = sblock.e2fs.e2fs_ipg;
502b2753aa5Sotto if (cylno == 0) {
503b2753aa5Sotto /* take reserved inodes off nifree */
504b2753aa5Sotto gd[cylno].ext2bgd_nifree -= EXT2_RESERVED_INODES;
505b2753aa5Sotto }
506b2753aa5Sotto ficount += gd[cylno].ext2bgd_nifree;
507b2753aa5Sotto gd[cylno].ext2bgd_ndirs = 0;
508b2753aa5Sotto }
509b2753aa5Sotto sblock.e2fs.e2fs_fbcount = fbcount;
510b2753aa5Sotto sblock.e2fs.e2fs_ficount = ficount;
511b2753aa5Sotto
512b2753aa5Sotto /*
513b2753aa5Sotto * Dump out summary information about file system.
514b2753aa5Sotto */
515b2753aa5Sotto if (verbosity > 0) {
516b2753aa5Sotto printf("%s: %u.%1uMB (%" PRId64 " sectors) "
517b2753aa5Sotto "block size %u, fragment size %u\n",
518b2753aa5Sotto fsys,
519b2753aa5Sotto (uint)(((uint64_t)bcount * bsize) / (1024 * 1024)),
520b2753aa5Sotto (uint)((uint64_t)bcount * bsize -
521b2753aa5Sotto rounddown((uint64_t)bcount * bsize, 1024 * 1024))
522b2753aa5Sotto / 1024 / 100,
523b2753aa5Sotto fssize, bsize, fsize);
524b2753aa5Sotto printf("\tusing %u block groups of %u.0MB, %u blks, "
525b2753aa5Sotto "%u inodes.\n",
526b2753aa5Sotto ncg, bsize * sblock.e2fs.e2fs_bpg / (1024 * 1024),
527b2753aa5Sotto sblock.e2fs.e2fs_bpg, sblock.e2fs.e2fs_ipg);
528b2753aa5Sotto }
529b2753aa5Sotto
530b2753aa5Sotto /*
531b2753aa5Sotto * allocate space for superblock and group descriptors
532b2753aa5Sotto */
533b2753aa5Sotto iobufsize = (NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb) * sblock.e2fs_bsize;
534b2753aa5Sotto iobuf = mmap(0, iobufsize, PROT_READ|PROT_WRITE,
535b2753aa5Sotto MAP_ANON|MAP_PRIVATE, -1, 0);
53666ac7e0fSdoug if (iobuf == MAP_FAILED)
537b2753aa5Sotto errx(EXIT_FAILURE, "Cannot allocate I/O buffer\n");
538b2753aa5Sotto
539b2753aa5Sotto /*
540b2753aa5Sotto * We now start writing to the filesystem
541b2753aa5Sotto */
542b2753aa5Sotto
543b2753aa5Sotto if (!Nflag) {
544b2753aa5Sotto static const uint pbsize[] = { 1024, 2048, 4096, 0 };
545b2753aa5Sotto uint pblock, epblock;
546b2753aa5Sotto /*
547b2753aa5Sotto * Validate the given file system size.
548b2753aa5Sotto * Verify that its last block can actually be accessed.
549b2753aa5Sotto * Convert to file system fragment sized units.
550b2753aa5Sotto */
551b2753aa5Sotto if (fssize <= 0)
552b2753aa5Sotto errx(EXIT_FAILURE, "Preposterous size %" PRId64 "\n",
553b2753aa5Sotto fssize);
554b2753aa5Sotto wtfs(fssize - 1, sectorsize, iobuf);
555b2753aa5Sotto
556b2753aa5Sotto /*
557b2753aa5Sotto * Ensure there is nothing that looks like a filesystem
558b2753aa5Sotto * superblock anywhere other than where ours will be.
559b2753aa5Sotto * If fsck_ext2fs finds the wrong one all hell breaks loose!
560b2753aa5Sotto *
561b2753aa5Sotto * XXX: needs to check how fsck_ext2fs programs even
562b2753aa5Sotto * on other OSes determine alternate superblocks
563b2753aa5Sotto */
564b2753aa5Sotto for (i = 0; pbsize[i] != 0; i++) {
565b2753aa5Sotto epblock = (uint64_t)bcount * bsize / pbsize[i];
566b2753aa5Sotto for (pblock = ((pbsize[i] == SBSIZE) ? 1 : 0);
567b2753aa5Sotto pblock < epblock;
568b2753aa5Sotto pblock += pbsize[i] * NBBY /* bpg */)
569b6d2e2d5Sderaadt zap_old_sblock((daddr32_t)pblock *
570b2753aa5Sotto pbsize[i] / sectorsize);
571b2753aa5Sotto }
572b2753aa5Sotto }
573b2753aa5Sotto
574b2753aa5Sotto if (verbosity >= 3)
575b2753aa5Sotto printf("super-block backups (for fsck_ext2fs -b #) at:\n");
576b2753aa5Sotto /* If we are printing more than one line of numbers, line up columns */
577b2753aa5Sotto fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
578b2753aa5Sotto (uint64_t)cgbase(&sblock, ncg - 1));
579b2753aa5Sotto if (Nflag && verbosity == 3)
580b2753aa5Sotto /* Leave space to add " ..." after one row of numbers */
581b2753aa5Sotto max_cols -= 4;
582b2753aa5Sotto #define BASE 0x10000 /* For some fixed-point maths */
583b2753aa5Sotto col = 0;
584b2753aa5Sotto delta = verbosity > 2 ? 0 : max_cols * BASE / ncg;
585b2753aa5Sotto for (cylno = 0; cylno < ncg; cylno++) {
586b2753aa5Sotto fflush(stdout);
587b2753aa5Sotto initcg(cylno);
588b2753aa5Sotto if (verbosity < 2)
589b2753aa5Sotto continue;
590b2753aa5Sotto /* the first one is a master, not backup */
591b2753aa5Sotto if (cylno == 0)
592b2753aa5Sotto continue;
593b2753aa5Sotto /* skip if this cylinder doesn't have a backup */
594b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
595b2753aa5Sotto (sblock.e2fs.e2fs_features_rocompat &
596*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) != 0 &&
597b2753aa5Sotto cg_has_sb(cylno) == 0)
598b2753aa5Sotto continue;
599b2753aa5Sotto
600b2753aa5Sotto if (delta > 0) {
601b2753aa5Sotto if (Nflag)
602b2753aa5Sotto /* No point doing dots for -N */
603b2753aa5Sotto break;
604b2753aa5Sotto /* Print dots scaled to end near RH margin */
605b2753aa5Sotto for (col += delta; col > BASE; col -= BASE)
606b2753aa5Sotto printf(".");
607b2753aa5Sotto continue;
608b2753aa5Sotto }
609b2753aa5Sotto /* Print superblock numbers */
6105161919aSotto len = printf("%s%*" PRIu64 ",", (col ? " " : ""), fld_width,
611b2753aa5Sotto (uint64_t)cgbase(&sblock, cylno));
612b2753aa5Sotto col += len;
613b2753aa5Sotto if (col + len < max_cols)
614b2753aa5Sotto /* Next number fits */
615b2753aa5Sotto continue;
616b2753aa5Sotto /* Next number won't fit, need a newline */
617b2753aa5Sotto if (verbosity <= 3) {
618b2753aa5Sotto /* Print dots for subsequent cylinder groups */
619b2753aa5Sotto delta = sblock.e2fs_ncg - cylno - 1;
620b2753aa5Sotto if (delta != 0) {
621b2753aa5Sotto if (Nflag) {
622b2753aa5Sotto printf(" ...");
623b2753aa5Sotto break;
624b2753aa5Sotto }
625b2753aa5Sotto delta = max_cols * BASE / delta;
626b2753aa5Sotto }
627b2753aa5Sotto }
628b2753aa5Sotto col = 0;
629b2753aa5Sotto printf("\n");
630b2753aa5Sotto }
631b2753aa5Sotto #undef BASE
632b2753aa5Sotto if (col > 0)
633b2753aa5Sotto printf("\n");
634b2753aa5Sotto if (Nflag)
635b2753aa5Sotto return;
636b2753aa5Sotto
637b2753aa5Sotto /*
638b2753aa5Sotto * Now construct the initial file system,
639b2753aa5Sotto */
640b2753aa5Sotto if (fsinit(&tv) == 0)
641b2753aa5Sotto errx(EXIT_FAILURE, "Error making filesystem");
642b2753aa5Sotto /*
643b2753aa5Sotto * Write out the superblock and group descriptors
644b2753aa5Sotto */
645b2753aa5Sotto sblock.e2fs.e2fs_block_group_nr = 0;
646b2753aa5Sotto sboff = 0;
647b2753aa5Sotto if (cgbase(&sblock, 0) == 0) {
648b2753aa5Sotto /*
649b2753aa5Sotto * If the first block contains the boot block sectors,
650b2753aa5Sotto * (i.e. in case of sblock.e2fs.e2fs_bsize > BBSIZE)
651b2753aa5Sotto * we have to preserve data in it.
652b2753aa5Sotto */
653b2753aa5Sotto sboff = SBOFF;
654b2753aa5Sotto }
655b2753aa5Sotto e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
656b2753aa5Sotto e2fs_cgsave(gd, (struct ext2_gd *)(iobuf + sblock.e2fs_bsize),
657b2753aa5Sotto sizeof(struct ext2_gd) * sblock.e2fs_ncg);
658b2753aa5Sotto wtfs(fsbtodb(&sblock, cgbase(&sblock, 0)) + sboff / sectorsize,
659b2753aa5Sotto iobufsize - sboff, iobuf + sboff);
660b2753aa5Sotto
661b2753aa5Sotto munmap(iobuf, iobufsize);
662b2753aa5Sotto }
663b2753aa5Sotto
664b2753aa5Sotto /*
665b2753aa5Sotto * Initialize a cylinder (block) group.
666b2753aa5Sotto */
667b2753aa5Sotto void
initcg(uint cylno)668b2753aa5Sotto initcg(uint cylno)
669b2753aa5Sotto {
670b2753aa5Sotto uint nblcg, i, j, sboff;
671b2753aa5Sotto struct ext2fs_dinode *dp;
672b2753aa5Sotto
673b2753aa5Sotto /*
674b2753aa5Sotto * Make a copy of the superblock and group descriptors.
675b2753aa5Sotto */
676b2753aa5Sotto if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
677b2753aa5Sotto (sblock.e2fs.e2fs_features_rocompat &
678*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
679b2753aa5Sotto cg_has_sb(cylno)) {
680b2753aa5Sotto sblock.e2fs.e2fs_block_group_nr = cylno;
681b2753aa5Sotto sboff = 0;
682b2753aa5Sotto if (cgbase(&sblock, cylno) == 0) {
683b2753aa5Sotto /* preserve data in bootblock in cg0 */
684b2753aa5Sotto sboff = SBOFF;
685b2753aa5Sotto }
686b2753aa5Sotto e2fs_sbsave(&sblock.e2fs, (struct ext2fs *)(iobuf + sboff));
687b2753aa5Sotto e2fs_cgsave(gd, (struct ext2_gd *)(iobuf +
688b2753aa5Sotto sblock.e2fs_bsize * NBLOCK_SUPERBLOCK),
689b2753aa5Sotto sizeof(struct ext2_gd) * sblock.e2fs_ncg);
690b2753aa5Sotto /* write superblock and group descriptor backups */
691b2753aa5Sotto wtfs(fsbtodb(&sblock, cgbase(&sblock, cylno)) +
692b2753aa5Sotto sboff / sectorsize, iobufsize - sboff, iobuf + sboff);
693b2753aa5Sotto }
694b2753aa5Sotto
695b2753aa5Sotto /*
696b2753aa5Sotto * Initialize block bitmap.
697b2753aa5Sotto */
698b2753aa5Sotto memset(buf, 0, sblock.e2fs_bsize);
699b2753aa5Sotto if (cylno == (sblock.e2fs_ncg - 1)) {
700b2753aa5Sotto /* The last group could have less blocks than e2fs_bpg. */
701b2753aa5Sotto nblcg = sblock.e2fs.e2fs_bcount -
702b2753aa5Sotto cgbase(&sblock, sblock.e2fs_ncg - 1);
703b2753aa5Sotto for (i = nblcg; i < roundup(nblcg, NBBY); i++)
704b2753aa5Sotto setbit(buf, i);
705b2753aa5Sotto memset(&buf[i / NBBY], ~0U, sblock.e2fs.e2fs_bpg - i);
706b2753aa5Sotto }
707b2753aa5Sotto /* set overhead (superblock, group descriptor etc.) blocks used */
708b2753aa5Sotto for (i = 0; i < cgoverhead(cylno) / NBBY; i++)
709b2753aa5Sotto buf[i] = ~0;
710b2753aa5Sotto i = i * NBBY;
711b2753aa5Sotto for (; i < cgoverhead(cylno); i++)
712b2753aa5Sotto setbit(buf, i);
713b2753aa5Sotto wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_b_bitmap), sblock.e2fs_bsize,
714b2753aa5Sotto buf);
715b2753aa5Sotto
716b2753aa5Sotto /*
717b2753aa5Sotto * Initialize inode bitmap.
718b2753aa5Sotto *
719b2753aa5Sotto * Assume e2fs_ipg is a multiple of NBBY since
720b2753aa5Sotto * it's a multiple of e2fs_ipb (as we did above).
721b2753aa5Sotto * Note even (possibly smaller) the last group has the same e2fs_ipg.
722b2753aa5Sotto */
723b2753aa5Sotto i = sblock.e2fs.e2fs_ipg / NBBY;
724b2753aa5Sotto memset(buf, 0, i);
725b2753aa5Sotto memset(buf + i, ~0U, sblock.e2fs_bsize - i);
726b2753aa5Sotto if (cylno == 0) {
727b2753aa5Sotto /* mark reserved inodes */
728b2753aa5Sotto for (i = 1; i < EXT2_FIRSTINO; i++)
729b2753aa5Sotto setbit(buf, EXT2_INO_INDEX(i));
730b2753aa5Sotto }
731b2753aa5Sotto wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_i_bitmap), sblock.e2fs_bsize,
732b2753aa5Sotto buf);
733b2753aa5Sotto
734b2753aa5Sotto /*
735b2753aa5Sotto * Initialize inode tables.
736b2753aa5Sotto *
737b2753aa5Sotto * Just initialize generation numbers for NFS security.
738b2753aa5Sotto * XXX: sys/ufs/ext2fs/ext2fs_alloc.c:ext2fs_valloc() seems
739b2753aa5Sotto * to override these generated numbers.
740b2753aa5Sotto */
741b2753aa5Sotto memset(buf, 0, sblock.e2fs_bsize);
742b2753aa5Sotto for (i = 0; i < sblock.e2fs_itpg; i++) {
743b2753aa5Sotto for (j = 0; j < sblock.e2fs_ipb; j++) {
744b2753aa5Sotto dp = (struct ext2fs_dinode *)(buf + inodesize * j);
74526270cbeSpelikan /* If there is some bias in arc4random(), keep it. */
74626270cbeSpelikan dp->e2di_gen = htole32(arc4random());
747b2753aa5Sotto }
748b2753aa5Sotto wtfs(fsbtodb(&sblock, gd[cylno].ext2bgd_i_tables + i),
749b2753aa5Sotto sblock.e2fs_bsize, buf);
750b2753aa5Sotto }
751b2753aa5Sotto }
752b2753aa5Sotto
753b2753aa5Sotto /*
754b2753aa5Sotto * Zap possible lingering old superblock data
755b2753aa5Sotto */
756b2753aa5Sotto static void
zap_old_sblock(daddr32_t sec)757b6d2e2d5Sderaadt zap_old_sblock(daddr32_t sec)
758b2753aa5Sotto {
759b6d2e2d5Sderaadt static daddr32_t cg0_data;
760b2753aa5Sotto uint32_t oldfs[SBSIZE / sizeof(uint32_t)];
761b2753aa5Sotto static const struct fsm {
762b2753aa5Sotto uint32_t offset;
763b2753aa5Sotto uint32_t magic;
764b2753aa5Sotto uint32_t mask;
765b2753aa5Sotto } fs_magics[] = {
766b2753aa5Sotto {offsetof(struct ext2fs, e2fs_magic) / 4, E2FS_MAGIC, 0xffff},
767b2753aa5Sotto {offsetof(struct ext2fs, e2fs_magic) / 4,
768b2753aa5Sotto E2FS_MAGIC << 16, 0xffff0000},
769b2753aa5Sotto {14, 0xef530000, 0xffff0000}, /* EXT2FS (big) */
770b2753aa5Sotto {0x55c / 4, 0x00011954, ~0U}, /* FS_UFS1_MAGIC */
771b2753aa5Sotto {0x55c / 4, 0x19540119, ~0U}, /* FS_UFS2_MAGIC */
772b2753aa5Sotto {0, 0x70162, ~0U}, /* LFS_MAGIC */
773b2753aa5Sotto {.offset = ~0U},
774b2753aa5Sotto };
775b2753aa5Sotto const struct fsm *fsm;
776b2753aa5Sotto
777b2753aa5Sotto if (Nflag)
778b2753aa5Sotto return;
779b2753aa5Sotto
780b2753aa5Sotto /* don't override data before superblock */
781b2753aa5Sotto if (sec < SBOFF / sectorsize)
782b2753aa5Sotto return;
783b2753aa5Sotto
784b2753aa5Sotto if (cg0_data == 0) {
785b2753aa5Sotto cg0_data =
786b6d2e2d5Sderaadt ((daddr32_t)sblock.e2fs.e2fs_first_dblock + cgoverhead(0)) *
787b2753aa5Sotto sblock.e2fs_bsize / sectorsize;
788b2753aa5Sotto }
789b2753aa5Sotto
790b2753aa5Sotto /* Ignore anything that is beyond our filesystem */
791b2753aa5Sotto if (sec >= fssize)
792b2753aa5Sotto return;
793b2753aa5Sotto /* Zero anything inside our filesystem... */
794b2753aa5Sotto if (sec >= sblock.e2fs.e2fs_first_dblock * bsize / sectorsize) {
795b2753aa5Sotto /* ...unless we will write that area anyway */
796b2753aa5Sotto if (sec >= cg0_data)
797b2753aa5Sotto /* assume iobuf is zero'ed here */
798b2753aa5Sotto wtfs(sec, roundup(SBSIZE, sectorsize), iobuf);
799b2753aa5Sotto return;
800b2753aa5Sotto }
801b2753aa5Sotto
802b2753aa5Sotto /*
803b2753aa5Sotto * The sector might contain boot code, so we must validate it
804b2753aa5Sotto *
805b2753aa5Sotto * XXX: ext2fs won't preserve data after SBOFF,
806b2753aa5Sotto * but first_dblock could have a different value.
807b2753aa5Sotto */
808b2753aa5Sotto rdfs(sec, sizeof(oldfs), &oldfs);
809b2753aa5Sotto for (fsm = fs_magics;; fsm++) {
810b2753aa5Sotto uint32_t v;
811b2753aa5Sotto if (fsm->mask == 0)
812b2753aa5Sotto return;
813b2753aa5Sotto v = oldfs[fsm->offset];
814b2753aa5Sotto if ((v & fsm->mask) == fsm->magic ||
815b2753aa5Sotto (swap32(v) & fsm->mask) == fsm->magic)
816b2753aa5Sotto break;
817b2753aa5Sotto }
818b2753aa5Sotto
819b2753aa5Sotto /* Just zap the magic number */
820b2753aa5Sotto oldfs[fsm->offset] = 0;
821b2753aa5Sotto wtfs(sec, sizeof(oldfs), &oldfs);
822b2753aa5Sotto }
823b2753aa5Sotto
824b2753aa5Sotto /*
825b2753aa5Sotto * uint cgoverhead(uint c)
826b2753aa5Sotto *
827b2753aa5Sotto * Return a number of reserved blocks on the specified group.
828b2753aa5Sotto * XXX: should be shared with src/sbin/fsck_ext2fs/setup.c
829b2753aa5Sotto */
830b2753aa5Sotto uint
cgoverhead(uint c)831b2753aa5Sotto cgoverhead(uint c)
832b2753aa5Sotto {
833b2753aa5Sotto uint overh;
834b2753aa5Sotto
835b2753aa5Sotto overh = NBLOCK_BLOCK_BITMAP + NBLOCK_INODE_BITMAP + sblock.e2fs_itpg;
836b2753aa5Sotto
837b2753aa5Sotto if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
838b2753aa5Sotto (sblock.e2fs.e2fs_features_rocompat &
839*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) == 0 ||
840b2753aa5Sotto cg_has_sb(c) != 0) {
841b2753aa5Sotto overh += NBLOCK_SUPERBLOCK + sblock.e2fs_ngdb;
842b2753aa5Sotto
843b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
844b2753aa5Sotto (sblock.e2fs.e2fs_features_compat &
845b2753aa5Sotto EXT2F_COMPAT_RESIZE) != 0)
846b2753aa5Sotto overh += sblock.e2fs.e2fs_reserved_ngdb;
847b2753aa5Sotto }
848b2753aa5Sotto
849b2753aa5Sotto return overh;
850b2753aa5Sotto }
851b2753aa5Sotto
852b2753aa5Sotto /*
853b2753aa5Sotto * Initialize the file system
854b2753aa5Sotto */
855b2753aa5Sotto
856b2753aa5Sotto #define LOSTDIR /* e2fsck complains if there is no lost+found */
857b2753aa5Sotto
858b2753aa5Sotto #define PREDEFDIR 2
859b2753aa5Sotto
860b2753aa5Sotto #ifdef LOSTDIR
861b2753aa5Sotto #define PREDEFROOTDIR (PREDEFDIR + 1)
862b2753aa5Sotto #else
863b2753aa5Sotto #define PREDEFROOTDIR PREDEFDIR
864b2753aa5Sotto #endif
865b2753aa5Sotto
866b2753aa5Sotto struct ext2fs_direct root_dir[] = {
867b2753aa5Sotto { EXT2_ROOTINO, 0, 1, 0, "." },
868b2753aa5Sotto { EXT2_ROOTINO, 0, 2, 0, ".." },
869b2753aa5Sotto #ifdef LOSTDIR
870b2753aa5Sotto { EXT2_LOSTFOUNDINO, 0, 10, 0, "lost+found" },
871b2753aa5Sotto #endif
872b2753aa5Sotto };
873b2753aa5Sotto
874b2753aa5Sotto #ifdef LOSTDIR
875b2753aa5Sotto struct ext2fs_direct lost_found_dir[] = {
876b2753aa5Sotto { EXT2_LOSTFOUNDINO, 0, 1, 0, "." },
877b2753aa5Sotto { EXT2_ROOTINO, 0, 2, 0, ".." },
878b2753aa5Sotto };
879b2753aa5Sotto struct ext2fs_direct pad_dir = { 0, sizeof(struct ext2fs_direct), 0, 0, "" };
880b2753aa5Sotto #endif
881b2753aa5Sotto
882b2753aa5Sotto int
fsinit(const struct timeval * tv)883b2753aa5Sotto fsinit(const struct timeval *tv)
884b2753aa5Sotto {
885b2753aa5Sotto struct ext2fs_dinode node;
886b2753aa5Sotto #ifdef LOSTDIR
887b2753aa5Sotto uint i, nblks_lostfound, blk;
888b2753aa5Sotto #endif
889b2753aa5Sotto
890b2753aa5Sotto /*
891b2753aa5Sotto * Initialize the inode for the resizefs feature
892b2753aa5Sotto */
893b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
894b2753aa5Sotto (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0)
895b2753aa5Sotto init_resizeino(tv);
896b2753aa5Sotto
897b2753aa5Sotto /*
898b2753aa5Sotto * Initialize the node
899b2753aa5Sotto */
900b2753aa5Sotto
901b2753aa5Sotto #ifdef LOSTDIR
902b2753aa5Sotto /*
903b2753aa5Sotto * Create the lost+found directory
904b2753aa5Sotto */
905b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
906b2753aa5Sotto sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
907b2753aa5Sotto lost_found_dir[0].e2d_type = EXT2_FT_DIR;
908b2753aa5Sotto lost_found_dir[1].e2d_type = EXT2_FT_DIR;
909b2753aa5Sotto }
910b2753aa5Sotto (void)makedir(lost_found_dir, nitems(lost_found_dir));
911b2753aa5Sotto
912b2753aa5Sotto /* prepare a bit large directory for preserved files */
913b2753aa5Sotto nblks_lostfound = EXT2_LOSTFOUNDSIZE / sblock.e2fs_bsize;
914b2753aa5Sotto /* ...but only with direct blocks */
915b2753aa5Sotto if (nblks_lostfound > NDADDR)
916b2753aa5Sotto nblks_lostfound = NDADDR;
917b2753aa5Sotto
918b2753aa5Sotto memset(&node, 0, sizeof(node));
919b2753aa5Sotto node.e2di_mode = EXT2_IFDIR | EXT2_LOSTFOUNDUMASK;
920b2753aa5Sotto node.e2di_uid_low = geteuid();
921b2753aa5Sotto node.e2di_size = sblock.e2fs_bsize * nblks_lostfound;
9223917bbadSderaadt node.e2di_atime = (u_int32_t)tv->tv_sec;
9233917bbadSderaadt node.e2di_ctime = (u_int32_t)tv->tv_sec;
9243917bbadSderaadt node.e2di_mtime = (u_int32_t)tv->tv_sec;
925b2753aa5Sotto node.e2di_gid_low = getegid();
926b2753aa5Sotto node.e2di_nlink = PREDEFDIR;
927b2753aa5Sotto /* e2di_nblock is a number of disk blocks, not ext2fs blocks */
928b2753aa5Sotto node.e2di_nblock = fsbtodb(&sblock, nblks_lostfound);
929b2753aa5Sotto node.e2di_blocks[0] = alloc(sblock.e2fs_bsize, node.e2di_mode);
930b2753aa5Sotto if (node.e2di_blocks[0] == 0) {
931b2753aa5Sotto printf("%s: can't allocate block for lost+found\n", __func__);
932b2753aa5Sotto return 0;
933b2753aa5Sotto }
934b2753aa5Sotto for (i = 1; i < nblks_lostfound; i++) {
935b2753aa5Sotto blk = alloc(sblock.e2fs_bsize, 0);
936b2753aa5Sotto if (blk == 0) {
937b2753aa5Sotto printf("%s: can't allocate blocks for lost+found\n",
938b2753aa5Sotto __func__);
939b2753aa5Sotto return 0;
940b2753aa5Sotto }
941b2753aa5Sotto node.e2di_blocks[i] = blk;
942b2753aa5Sotto }
943b2753aa5Sotto wtfs(fsbtodb(&sblock, node.e2di_blocks[0]), sblock.e2fs_bsize, buf);
944b2753aa5Sotto pad_dir.e2d_reclen = sblock.e2fs_bsize;
945b2753aa5Sotto for (i = 1; i < nblks_lostfound; i++) {
946b2753aa5Sotto memset(buf, 0, sblock.e2fs_bsize);
947b2753aa5Sotto copy_dir(&pad_dir, (struct ext2fs_direct *)buf);
948b2753aa5Sotto wtfs(fsbtodb(&sblock, node.e2di_blocks[i]), sblock.e2fs_bsize,
949b2753aa5Sotto buf);
950b2753aa5Sotto }
951b2753aa5Sotto iput(&node, EXT2_LOSTFOUNDINO);
952b2753aa5Sotto #endif
953b2753aa5Sotto /*
954b2753aa5Sotto * create the root directory
955b2753aa5Sotto */
956b2753aa5Sotto memset(&node, 0, sizeof(node));
957b2753aa5Sotto if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
958b2753aa5Sotto sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) {
959b2753aa5Sotto root_dir[0].e2d_type = EXT2_FT_DIR;
960b2753aa5Sotto root_dir[1].e2d_type = EXT2_FT_DIR;
961b2753aa5Sotto #ifdef LOSTDIR
962b2753aa5Sotto root_dir[2].e2d_type = EXT2_FT_DIR;
963b2753aa5Sotto #endif
964b2753aa5Sotto }
965b2753aa5Sotto node.e2di_mode = EXT2_IFDIR | EXT2_UMASK;
966b2753aa5Sotto node.e2di_uid_low = geteuid();
967b2753aa5Sotto node.e2di_size = makedir(root_dir, nitems(root_dir));
9683917bbadSderaadt node.e2di_atime = (u_int32_t)tv->tv_sec;
9693917bbadSderaadt node.e2di_ctime = (u_int32_t)tv->tv_sec;
9703917bbadSderaadt node.e2di_mtime = (u_int32_t)tv->tv_sec;
971b2753aa5Sotto node.e2di_gid_low = getegid();
972b2753aa5Sotto node.e2di_nlink = PREDEFROOTDIR;
973b2753aa5Sotto /* e2di_nblock is a number of disk block, not ext2fs block */
974b2753aa5Sotto node.e2di_nblock = fsbtodb(&sblock, 1);
975b2753aa5Sotto node.e2di_blocks[0] = alloc(node.e2di_size, node.e2di_mode);
976b2753aa5Sotto if (node.e2di_blocks[0] == 0) {
977b2753aa5Sotto printf("%s: can't allocate block for root dir\n", __func__);
978b2753aa5Sotto return 0;
979b2753aa5Sotto }
980b2753aa5Sotto wtfs(fsbtodb(&sblock, node.e2di_blocks[0]), sblock.e2fs_bsize, buf);
981b2753aa5Sotto iput(&node, EXT2_ROOTINO);
982b2753aa5Sotto return 1;
983b2753aa5Sotto }
984b2753aa5Sotto
985b2753aa5Sotto /*
986b2753aa5Sotto * Construct a set of directory entries in "buf".
987b2753aa5Sotto * return size of directory.
988b2753aa5Sotto */
989b2753aa5Sotto int
makedir(struct ext2fs_direct * protodir,int entries)990b2753aa5Sotto makedir(struct ext2fs_direct *protodir, int entries)
991b2753aa5Sotto {
992b2753aa5Sotto uint8_t *cp;
993b2753aa5Sotto uint i, spcleft;
994b2753aa5Sotto uint dirblksiz;
995b2753aa5Sotto
996b2753aa5Sotto dirblksiz = sblock.e2fs_bsize;
997b2753aa5Sotto memset(buf, 0, dirblksiz);
998b2753aa5Sotto spcleft = dirblksiz;
999b2753aa5Sotto for (cp = buf, i = 0; i < entries - 1; i++) {
1000b2753aa5Sotto protodir[i].e2d_reclen = EXT2FS_DIRSIZ(protodir[i].e2d_namlen);
1001b2753aa5Sotto copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
1002b2753aa5Sotto cp += protodir[i].e2d_reclen;
1003b2753aa5Sotto spcleft -= protodir[i].e2d_reclen;
1004b2753aa5Sotto }
1005b2753aa5Sotto protodir[i].e2d_reclen = spcleft;
1006b2753aa5Sotto copy_dir(&protodir[i], (struct ext2fs_direct *)cp);
1007b2753aa5Sotto return dirblksiz;
1008b2753aa5Sotto }
1009b2753aa5Sotto
1010b2753aa5Sotto /*
1011b2753aa5Sotto * Copy a direntry to a buffer, in fs byte order
1012b2753aa5Sotto */
1013b2753aa5Sotto static void
copy_dir(struct ext2fs_direct * dir,struct ext2fs_direct * dbuf)1014b2753aa5Sotto copy_dir(struct ext2fs_direct *dir, struct ext2fs_direct *dbuf)
1015b2753aa5Sotto {
1016b2753aa5Sotto
1017b2753aa5Sotto memcpy(dbuf, dir, EXT2FS_DIRSIZ(dir->e2d_namlen));
101826270cbeSpelikan dbuf->e2d_ino = htole32(dir->e2d_ino);
101926270cbeSpelikan dbuf->e2d_reclen = htole16(dir->e2d_reclen);
1020b2753aa5Sotto }
1021b2753aa5Sotto
1022b2753aa5Sotto /*
1023b2753aa5Sotto * void init_resizeino(const struct timeval *tv);
1024b2753aa5Sotto *
1025b2753aa5Sotto * Initialize the EXT2_RESEIZE_INO inode to preserve
1026b2753aa5Sotto * reserved group descriptor blocks for future growth of this ext2fs.
1027b2753aa5Sotto */
1028b2753aa5Sotto void
init_resizeino(const struct timeval * tv)1029b2753aa5Sotto init_resizeino(const struct timeval *tv)
1030b2753aa5Sotto {
1031b2753aa5Sotto struct ext2fs_dinode node;
1032b2753aa5Sotto uint64_t isize;
1033b2753aa5Sotto uint32_t *dindir_block, *reserved_gdb;
1034b2753aa5Sotto uint nblock, i, cylno, n;
1035b2753aa5Sotto
1036b2753aa5Sotto memset(&node, 0, sizeof(node));
1037b2753aa5Sotto
1038b2753aa5Sotto /*
1039b2753aa5Sotto * Note this function only prepares required structures for
1040b2753aa5Sotto * future resize. It's a quite different work to implement
1041b2753aa5Sotto * a utility like resize_ext2fs(8) which handles actual
1042b2753aa5Sotto * resize ops even on offline.
1043b2753aa5Sotto *
1044b2753aa5Sotto * Anyway, I'm not sure if there is any documentation about
1045b2753aa5Sotto * this resize ext2fs feature and related data structures,
1046b2753aa5Sotto * and I've written this function based on things what I see
1047b2753aa5Sotto * on some existing implementation and real file system data
1048b2753aa5Sotto * created by existing tools. To be honest, they are not
1049b2753aa5Sotto * so easy to read, so I will try to implement it here without
1050b2753aa5Sotto * any dumb optimization for people who would eventually
1051b2753aa5Sotto * work on "yet another wheel" like resize_ext2fs(8).
1052b2753aa5Sotto */
1053b2753aa5Sotto
1054b2753aa5Sotto /*
1055b2753aa5Sotto * I'm not sure what type is appropriate for this inode.
1056b2753aa5Sotto * The release notes of e2fsprogs says they changed e2fsck to allow
1057b2753aa5Sotto * IFREG for RESIZEINO since a certain resize tool used it. Hmm.
1058b2753aa5Sotto */
1059b2753aa5Sotto node.e2di_mode = EXT2_IFREG | EXT2_RESIZEINOUMASK;
1060b2753aa5Sotto node.e2di_uid_low = geteuid();
10613917bbadSderaadt node.e2di_atime = (u_int32_t)tv->tv_sec;
10623917bbadSderaadt node.e2di_ctime = (u_int32_t)tv->tv_sec;
10633917bbadSderaadt node.e2di_mtime = (u_int32_t)tv->tv_sec;
1064b2753aa5Sotto node.e2di_gid_low = getegid();
1065b2753aa5Sotto node.e2di_nlink = 1;
1066b2753aa5Sotto
1067b2753aa5Sotto /*
1068b2753aa5Sotto * To preserve the reserved group descriptor blocks,
1069b2753aa5Sotto * EXT2_RESIZEINO uses only double indirect reference
1070b2753aa5Sotto * blocks in its inode entries.
1071b2753aa5Sotto *
1072b2753aa5Sotto * All entries for direct, single indirect and triple
1073b2753aa5Sotto * indirect references are left zero'ed. Maybe it's safe
1074b2753aa5Sotto * because no write operation will happen with this inode.
1075b2753aa5Sotto *
1076b2753aa5Sotto * We have to allocate a block for the first level double
1077b2753aa5Sotto * indirect reference block. Indexes of inode entries in
1078b2753aa5Sotto * this first level dindirect block are corresponding to
1079b2753aa5Sotto * indexes of group descriptors including both used (e2fs_ngdb)
1080b2753aa5Sotto * and reserved (e2fs_reserved_ngdb) group descriptor blocks.
1081b2753aa5Sotto *
1082b2753aa5Sotto * Inode entries of indexes for used (e2fs_ngdb) descriptors are
1083b2753aa5Sotto * left zero'ed. Entries for reserved (e2fs_reserved_ngdb) ones
1084b2753aa5Sotto * have block numbers of actual reserved group descriptors
1085b2753aa5Sotto * allocated at block group zero. This means e2fs_reserved_ngdb
1086b2753aa5Sotto * blocks are reserved as the second level dindirect reference
1087b2753aa5Sotto * blocks, and they actually contain block numbers of indirect
1088b2753aa5Sotto * references. It may be safe since they don't have to keep any
1089b2753aa5Sotto * data yet.
1090b2753aa5Sotto *
1091b2753aa5Sotto * Each these second dindirect blocks (i.e. reserved group
1092b2753aa5Sotto * descriptor blocks in the first block group) should have
1093b2753aa5Sotto * block numbers of its backups in all other block groups.
1094b2753aa5Sotto * I.e. reserved_ngdb[0] block in block group 0 contains block
1095b2753aa5Sotto * numbers of resreved_ngdb[0] from group 1 through (e2fs_ncg - 1).
1096b2753aa5Sotto * The number of backups can be determined by the
1097b2753aa5Sotto * EXT2_ROCOMPAT_SPARSESUPER feature and cg_has_sb() macro
1098b2753aa5Sotto * as done in the above initcg() function.
1099b2753aa5Sotto */
1100b2753aa5Sotto
1101b2753aa5Sotto /* set e2di_size which occupies whole blocks through DINDIR blocks */
1102b2753aa5Sotto isize = (uint64_t)sblock.e2fs_bsize * NDADDR +
1103b2753aa5Sotto (uint64_t)sblock.e2fs_bsize * NINDIR(&sblock) +
1104b2753aa5Sotto (uint64_t)sblock.e2fs_bsize * NINDIR(&sblock) * NINDIR(&sblock);
1105b2753aa5Sotto if (isize > UINT32_MAX &&
1106b2753aa5Sotto (sblock.e2fs.e2fs_features_rocompat &
1107*cca4ee03Skevlo EXT2F_ROCOMPAT_LARGE_FILE) == 0) {
1108b2753aa5Sotto /* XXX should enable it here and update all backups? */
1109b2753aa5Sotto errx(EXIT_FAILURE, "%s: large_file rocompat feature is "
1110b2753aa5Sotto "required to enable resize feature for this filesystem\n",
1111b2753aa5Sotto __func__);
1112b2753aa5Sotto }
1113f8894c3dSpelikan /* upper 32bit is stored into e2di_size_hi on REV1 feature */
1114b2753aa5Sotto node.e2di_size = isize & UINT32_MAX;
1115f8894c3dSpelikan node.e2di_size_hi = isize >> 32;
1116b2753aa5Sotto
1117b2753aa5Sotto #define SINGLE 0 /* index of single indirect block */
1118b2753aa5Sotto #define DOUBLE 1 /* index of double indirect block */
1119b2753aa5Sotto #define TRIPLE 2 /* index of triple indirect block */
1120b2753aa5Sotto
1121b2753aa5Sotto /* zero out entries for direct references */
1122b2753aa5Sotto for (i = 0; i < NDADDR; i++)
1123b2753aa5Sotto node.e2di_blocks[i] = 0;
1124b2753aa5Sotto /* also zero out entries for single and triple indirect references */
1125b2753aa5Sotto node.e2di_blocks[NDADDR + SINGLE] = 0;
1126b2753aa5Sotto node.e2di_blocks[NDADDR + TRIPLE] = 0;
1127b2753aa5Sotto
1128b2753aa5Sotto /* allocate a block for the first level double indirect reference */
1129b2753aa5Sotto node.e2di_blocks[NDADDR + DOUBLE] =
1130b2753aa5Sotto alloc(sblock.e2fs_bsize, node.e2di_mode);
1131b2753aa5Sotto if (node.e2di_blocks[NDADDR + DOUBLE] == 0)
1132b2753aa5Sotto errx(EXIT_FAILURE, "%s: Can't allocate a dindirect block",
1133b2753aa5Sotto __func__);
1134b2753aa5Sotto
1135b2753aa5Sotto /* account this first block */
1136b2753aa5Sotto nblock = fsbtodb(&sblock, 1);
1137b2753aa5Sotto
1138b2753aa5Sotto /* allocate buffer to set data in the dindirect block */
1139b2753aa5Sotto dindir_block = malloc(sblock.e2fs_bsize);
1140b2753aa5Sotto if (dindir_block == NULL)
1141b2753aa5Sotto errx(EXIT_FAILURE,
1142b2753aa5Sotto "%s: Can't allocate buffer for a dindirect block",
1143b2753aa5Sotto __func__);
1144b2753aa5Sotto
1145b2753aa5Sotto /* allocate buffer to set data in the group descriptor blocks */
1146b2753aa5Sotto reserved_gdb = malloc(sblock.e2fs_bsize);
1147b2753aa5Sotto if (reserved_gdb == NULL)
1148b2753aa5Sotto errx(EXIT_FAILURE,
1149b2753aa5Sotto "%s: Can't allocate buffer for group descriptor blocks",
1150b2753aa5Sotto __func__);
1151b2753aa5Sotto
1152b2753aa5Sotto /*
1153b2753aa5Sotto * Setup block entries in the first level dindirect blocks
1154b2753aa5Sotto */
1155b2753aa5Sotto for (i = 0; i < sblock.e2fs_ngdb; i++) {
1156b2753aa5Sotto /* no need to handle used group descriptor blocks */
1157b2753aa5Sotto dindir_block[i] = 0;
1158b2753aa5Sotto }
1159b2753aa5Sotto for (; i < sblock.e2fs_ngdb + sblock.e2fs.e2fs_reserved_ngdb; i++) {
1160b2753aa5Sotto /*
1161b2753aa5Sotto * point reserved group descriptor block in the first
1162b2753aa5Sotto * (i.e. master) block group
1163b2753aa5Sotto *
1164b2753aa5Sotto * XXX: e2fsprogs seem to use "(i % NINDIR(&sblock))" here
1165b2753aa5Sotto * to store maximum NINDIR(&sblock) reserved gdbs.
1166b2753aa5Sotto * I'm not sure what will be done on future filesystem
1167b2753aa5Sotto * shrink in that case on their way.
1168b2753aa5Sotto */
1169b2753aa5Sotto if (i >= NINDIR(&sblock))
1170b2753aa5Sotto errx(EXIT_FAILURE, "%s: too many reserved "
1171b2753aa5Sotto "group descriptors (%u) for resize inode",
1172b2753aa5Sotto __func__, sblock.e2fs.e2fs_reserved_ngdb);
1173b2753aa5Sotto dindir_block[i] =
117426270cbeSpelikan htole32(cgbase(&sblock, 0) + NBLOCK_SUPERBLOCK + i);
1175b2753aa5Sotto
1176b2753aa5Sotto /*
1177b2753aa5Sotto * Setup block entries in the second dindirect blocks
1178b2753aa5Sotto * (which are primary reserved group descriptor blocks)
1179b2753aa5Sotto * to point their backups.
1180b2753aa5Sotto */
1181b2753aa5Sotto for (n = 0, cylno = 1; cylno < sblock.e2fs_ncg; cylno++) {
1182b2753aa5Sotto /* skip block groups without backup */
1183b2753aa5Sotto if ((sblock.e2fs.e2fs_features_rocompat &
1184*cca4ee03Skevlo EXT2F_ROCOMPAT_SPARSE_SUPER) != 0 &&
1185b2753aa5Sotto cg_has_sb(cylno) == 0)
1186b2753aa5Sotto continue;
1187b2753aa5Sotto
1188b2753aa5Sotto if (n >= NINDIR(&sblock))
1189b2753aa5Sotto errx(EXIT_FAILURE, "%s: too many block groups "
1190b2753aa5Sotto "for the resize feature", __func__);
1191b2753aa5Sotto /*
1192b2753aa5Sotto * These blocks are already reserved in
1193b2753aa5Sotto * initcg() so no need to use alloc() here.
1194b2753aa5Sotto */
119526270cbeSpelikan reserved_gdb[n++] = htole32(cgbase(&sblock, cylno) +
1196b2753aa5Sotto NBLOCK_SUPERBLOCK + i);
1197b2753aa5Sotto nblock += fsbtodb(&sblock, 1);
1198b2753aa5Sotto }
1199b2753aa5Sotto for (; n < NINDIR(&sblock); n++)
1200b2753aa5Sotto reserved_gdb[n] = 0;
1201b2753aa5Sotto
1202b2753aa5Sotto /* write group descriptor block as the second dindirect refs */
120326270cbeSpelikan wtfs(fsbtodb(&sblock, letoh32(dindir_block[i])),
1204b2753aa5Sotto sblock.e2fs_bsize, reserved_gdb);
1205b2753aa5Sotto nblock += fsbtodb(&sblock, 1);
1206b2753aa5Sotto }
1207b2753aa5Sotto for (; i < NINDIR(&sblock); i++) {
1208b2753aa5Sotto /* leave trailing entries unallocated */
1209b2753aa5Sotto dindir_block[i] = 0;
1210b2753aa5Sotto }
1211b2753aa5Sotto free(reserved_gdb);
1212b2753aa5Sotto
1213b2753aa5Sotto /* finally write the first level dindirect block */
1214b2753aa5Sotto wtfs(fsbtodb(&sblock, node.e2di_blocks[NDADDR + DOUBLE]),
1215b2753aa5Sotto sblock.e2fs_bsize, dindir_block);
1216b2753aa5Sotto free(dindir_block);
1217b2753aa5Sotto
1218b2753aa5Sotto node.e2di_nblock = nblock;
1219b2753aa5Sotto iput(&node, EXT2_RESIZEINO);
1220b2753aa5Sotto }
1221b2753aa5Sotto
1222b2753aa5Sotto /*
1223b2753aa5Sotto * uint32_t alloc(uint32_t size, uint16_t mode)
1224b2753aa5Sotto *
1225b2753aa5Sotto * Allocate a block (from cylinder group 0)
1226b2753aa5Sotto * Reference: src/sys/ufs/ext2fs/ext2fs_alloc.c:ext2fs_alloccg()
1227b2753aa5Sotto */
1228b2753aa5Sotto uint32_t
alloc(uint32_t size,uint16_t mode)1229b2753aa5Sotto alloc(uint32_t size, uint16_t mode)
1230b2753aa5Sotto {
1231b2753aa5Sotto uint32_t loc, bno;
1232b2753aa5Sotto uint8_t *bbp;
1233b2753aa5Sotto uint len, map, i;
1234b2753aa5Sotto
1235b2753aa5Sotto if (gd[0].ext2bgd_nbfree == 0)
1236b2753aa5Sotto return 0;
1237b2753aa5Sotto
1238b2753aa5Sotto if (size > sblock.e2fs_bsize)
1239b2753aa5Sotto return 0;
1240b2753aa5Sotto
1241b2753aa5Sotto bbp = malloc(sblock.e2fs_bsize);
1242b2753aa5Sotto if (bbp == NULL)
1243b2753aa5Sotto return 0;
1244b2753aa5Sotto rdfs(fsbtodb(&sblock, gd[0].ext2bgd_b_bitmap), sblock.e2fs_bsize, bbp);
1245b2753aa5Sotto
1246b2753aa5Sotto /* XXX: kernel uses e2fs_fpg here */
1247b2753aa5Sotto len = sblock.e2fs.e2fs_bpg / NBBY;
1248b2753aa5Sotto
1249b2753aa5Sotto #if 0 /* no need block allocation for root or lost+found dir */
1250b2753aa5Sotto for (loc = 0; loc < len; loc++) {
1251b2753aa5Sotto if (bbp[loc] == 0) {
1252b2753aa5Sotto bno = loc * NBBY;
1253b2753aa5Sotto goto gotit;
1254b2753aa5Sotto }
1255b2753aa5Sotto }
1256b2753aa5Sotto #endif
1257b2753aa5Sotto
1258b2753aa5Sotto loc = skpc(~0U, len, bbp);
12596645c9ffSderaadt if (loc == 0) {
12606645c9ffSderaadt free(bbp);
1261b2753aa5Sotto return 0;
12626645c9ffSderaadt }
1263b2753aa5Sotto loc = len - loc;
1264b2753aa5Sotto map = bbp[loc];
1265b2753aa5Sotto bno = loc * NBBY;
1266b2753aa5Sotto for (i = 0; i < NBBY; i++, bno++) {
1267b2753aa5Sotto if ((map & (1 << i)) == 0)
1268b2753aa5Sotto goto gotit;
1269b2753aa5Sotto }
12706645c9ffSderaadt free(bbp);
1271b2753aa5Sotto return 0;
1272b2753aa5Sotto
1273b2753aa5Sotto gotit:
1274b2753aa5Sotto if (isset(bbp, bno))
1275b2753aa5Sotto errx(EXIT_FAILURE, "%s: inconsistent bitmap\n", __func__);
1276b2753aa5Sotto
1277b2753aa5Sotto setbit(bbp, bno);
1278b2753aa5Sotto wtfs(fsbtodb(&sblock, gd[0].ext2bgd_b_bitmap), sblock.e2fs_bsize, bbp);
1279b2753aa5Sotto free(bbp);
1280b2753aa5Sotto /* XXX: modified group descriptors won't be written into backups */
1281b2753aa5Sotto gd[0].ext2bgd_nbfree--;
1282b2753aa5Sotto if ((mode & EXT2_IFDIR) != 0)
1283b2753aa5Sotto gd[0].ext2bgd_ndirs++;
1284b2753aa5Sotto sblock.e2fs.e2fs_fbcount--;
1285b2753aa5Sotto
1286b2753aa5Sotto return sblock.e2fs.e2fs_first_dblock + bno;
1287b2753aa5Sotto }
1288b2753aa5Sotto
1289b2753aa5Sotto /*
1290b2753aa5Sotto * void iput(struct ext2fs_dinode *ip, ino_t ino)
1291b2753aa5Sotto *
1292b2753aa5Sotto * Put an inode entry into the corresponding table.
1293b2753aa5Sotto */
1294b2753aa5Sotto static void
iput(struct ext2fs_dinode * ip,ino_t ino)1295b2753aa5Sotto iput(struct ext2fs_dinode *ip, ino_t ino)
1296b2753aa5Sotto {
1297b6d2e2d5Sderaadt daddr32_t d;
1298b2753aa5Sotto uint c, i;
1299b2753aa5Sotto struct ext2fs_dinode *dp;
1300b2753aa5Sotto uint8_t *bp;
1301b2753aa5Sotto
1302b2753aa5Sotto bp = malloc(sblock.e2fs_bsize);
1303b2753aa5Sotto if (bp == NULL)
1304b2753aa5Sotto errx(EXIT_FAILURE, "%s: can't allocate buffer for inode\n",
1305b2753aa5Sotto __func__);
1306b2753aa5Sotto
1307b2753aa5Sotto /*
1308b2753aa5Sotto * Reserved inodes are allocated and accounted in initcg()
1309b2753aa5Sotto * so skip checks of the bitmap and allocation for them.
1310b2753aa5Sotto */
1311b2753aa5Sotto if (ino >= EXT2_FIRSTINO) {
1312b2753aa5Sotto c = ino_to_cg(&sblock, ino);
1313b2753aa5Sotto
1314b2753aa5Sotto /* sanity check */
1315b2753aa5Sotto if (gd[c].ext2bgd_nifree == 0)
1316b2753aa5Sotto errx(EXIT_FAILURE,
1317b2753aa5Sotto "%s: no free inode %" PRIu64 " in block group %u\n",
1318b2753aa5Sotto __func__, (uint64_t)ino, c);
1319b2753aa5Sotto
1320b2753aa5Sotto /* update inode bitmap */
1321b2753aa5Sotto rdfs(fsbtodb(&sblock, gd[0].ext2bgd_i_bitmap),
1322b2753aa5Sotto sblock.e2fs_bsize, bp);
1323b2753aa5Sotto
1324b2753aa5Sotto /* more sanity */
1325b2753aa5Sotto if (isset(bp, EXT2_INO_INDEX(ino)))
1326b2753aa5Sotto errx(EXIT_FAILURE, "%s: inode %" PRIu64
1327b2753aa5Sotto " already in use\n", __func__, (uint64_t)ino);
1328b2753aa5Sotto setbit(bp, EXT2_INO_INDEX(ino));
1329b2753aa5Sotto wtfs(fsbtodb(&sblock, gd[0].ext2bgd_i_bitmap),
1330b2753aa5Sotto sblock.e2fs_bsize, bp);
1331b2753aa5Sotto gd[c].ext2bgd_nifree--;
1332b2753aa5Sotto sblock.e2fs.e2fs_ficount--;
1333b2753aa5Sotto }
1334b2753aa5Sotto
1335b2753aa5Sotto if (ino >= sblock.e2fs.e2fs_ipg * sblock.e2fs_ncg)
1336b2753aa5Sotto errx(EXIT_FAILURE, "%s: inode value out of range (%" PRIu64
1337b2753aa5Sotto ").\n", __func__, (uint64_t)ino);
1338b2753aa5Sotto
1339b2753aa5Sotto /* update an inode entry in the table */
1340b2753aa5Sotto d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
1341b2753aa5Sotto rdfs(d, sblock.e2fs_bsize, bp);
1342b2753aa5Sotto
1343b2753aa5Sotto dp = (struct ext2fs_dinode *)(bp +
1344b2753aa5Sotto inodesize * ino_to_fsbo(&sblock, ino));
1345207ff64bSpelikan e2fs_isave(&sblock, ip, dp);
1346b2753aa5Sotto /* e2fs_i_bswap() doesn't swap e2di_blocks addrs */
1347b2753aa5Sotto if ((ip->e2di_mode & EXT2_IFMT) != EXT2_IFLNK) {
1348b2753aa5Sotto for (i = 0; i < NDADDR + NIADDR; i++)
134926270cbeSpelikan dp->e2di_blocks[i] = htole32(ip->e2di_blocks[i]);
1350b2753aa5Sotto }
135126270cbeSpelikan /* If there is some bias in arc4random(), keep it. */
135226270cbeSpelikan dp->e2di_gen = htole32(arc4random());
1353b2753aa5Sotto
1354b2753aa5Sotto wtfs(d, sblock.e2fs_bsize, bp);
1355b2753aa5Sotto free(bp);
1356b2753aa5Sotto }
1357b2753aa5Sotto
1358b2753aa5Sotto /*
1359b2753aa5Sotto * Read a block from the file system
1360b2753aa5Sotto */
1361b2753aa5Sotto void
rdfs(daddr32_t bno,int size,void * bf)1362b6d2e2d5Sderaadt rdfs(daddr32_t bno, int size, void *bf)
1363b2753aa5Sotto {
1364b2753aa5Sotto int n;
1365b2753aa5Sotto off_t offset;
1366b2753aa5Sotto
1367b2753aa5Sotto offset = bno;
1368bed75cd5Snatano n = pread(fd, bf, size, offset * sectorsize);
1369b2753aa5Sotto if (n != size)
1370b2753aa5Sotto err(EXIT_FAILURE, "%s: read error for sector %" PRId64,
1371b2753aa5Sotto __func__, (int64_t)bno);
1372b2753aa5Sotto }
1373b2753aa5Sotto
1374b2753aa5Sotto /*
1375b2753aa5Sotto * Write a block to the file system
1376b2753aa5Sotto */
1377b2753aa5Sotto void
wtfs(daddr32_t bno,int size,void * bf)1378b6d2e2d5Sderaadt wtfs(daddr32_t bno, int size, void *bf)
1379b2753aa5Sotto {
1380b2753aa5Sotto int n;
1381b2753aa5Sotto off_t offset;
1382b2753aa5Sotto
1383b2753aa5Sotto if (Nflag)
1384b2753aa5Sotto return;
1385b2753aa5Sotto offset = bno;
1386bed75cd5Snatano n = pwrite(fd, bf, size, offset * sectorsize);
1387b2753aa5Sotto if (n != size)
1388b2753aa5Sotto err(EXIT_FAILURE, "%s: write error for sector %" PRId64,
1389b2753aa5Sotto __func__, (int64_t)bno);
1390b2753aa5Sotto }
1391b2753aa5Sotto
1392b2753aa5Sotto int
ilog2(uint val)1393b2753aa5Sotto ilog2(uint val)
1394b2753aa5Sotto {
1395b2753aa5Sotto
1396b2753aa5Sotto if (val == 0 || !powerof2(val))
1397b2753aa5Sotto errx(EXIT_FAILURE, "%s: %u is not a power of 2\n",
1398b2753aa5Sotto __func__, val);
1399b2753aa5Sotto
1400b2753aa5Sotto return ffs(val) - 1;
1401b2753aa5Sotto }
1402b2753aa5Sotto
1403b2753aa5Sotto /*
1404b2753aa5Sotto * int skpc(int mask, size_t size, uint8_t *cp)
1405b2753aa5Sotto *
1406b2753aa5Sotto * Locate an unsigned character of value mask inside cp[].
1407b2753aa5Sotto * (from src/sys/lib/libkern/skpc.c)
1408b2753aa5Sotto */
1409b2753aa5Sotto int
skpc(int mask,size_t size,uint8_t * cp)1410b2753aa5Sotto skpc(int mask, size_t size, uint8_t *cp)
1411b2753aa5Sotto {
1412b2753aa5Sotto uint8_t *end;
1413b2753aa5Sotto
1414b2753aa5Sotto end = &cp[size];
1415b2753aa5Sotto while (cp < end && *cp == (uint8_t)mask)
1416b2753aa5Sotto cp++;
1417b2753aa5Sotto
1418b2753aa5Sotto return end - cp;
1419b2753aa5Sotto }
1420b2753aa5Sotto
1421b2753aa5Sotto static void
uuid_get(struct m_ext2fs * sb)1422b2753aa5Sotto uuid_get(struct m_ext2fs *sb)
1423b2753aa5Sotto {
1424b2753aa5Sotto unsigned char buf[sizeof(sb->e2fs.e2fs_uuid)];
1425b2753aa5Sotto
1426b2753aa5Sotto arc4random_buf(buf, sizeof(buf));
1427b2753aa5Sotto /* UUID version 4: random */
1428b2753aa5Sotto buf[6] &= 0x0f;
1429b2753aa5Sotto buf[6] |= 0x40;
1430b2753aa5Sotto /* RFC4122 variant */
1431b2753aa5Sotto buf[8] &= 0x3f;
1432b2753aa5Sotto buf[8] |= 0x80;
1433b2753aa5Sotto memcpy(sb->e2fs.e2fs_uuid, buf, sizeof(sb->e2fs.e2fs_uuid));
1434b2753aa5Sotto }
1435