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