Bug Summary

File:src/sbin/fsck_ffs/setup.c
Warning:line 651, column 49
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name setup.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sbin/fsck_ffs/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/sbin/fsck_ffs/../fsck -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/sbin/fsck_ffs/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/sbin/fsck_ffs/setup.c
1/* $OpenBSD: setup.c,v 1.68 2021/07/12 15:09:18 beck Exp $ */
2/* $NetBSD: setup.c,v 1.27 1996/09/27 22:45:19 christos Exp $ */
3
4/*
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h> /* MAXBSIZE DEV_BSIZE roundup */
34#define DKTYPENAMES
35#include <sys/time.h>
36#include <ufs/ufs/dinode.h>
37#include <ufs/ffs/fs.h>
38#include <sys/stat.h>
39#include <sys/ioctl.h>
40#include <sys/dkio.h>
41#include <sys/disklabel.h>
42
43#include <errno(*__errno()).h>
44#include <fcntl.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <unistd.h>
48#include <string.h>
49#include <util.h>
50#include <limits.h>
51#include <ctype.h>
52#include <err.h>
53
54#include "fsck.h"
55#include "extern.h"
56#include "fsutil.h"
57
58#define MAXIMUM(a, b)(((a) > (b)) ? (a) : (b)) (((a) > (b)) ? (a) : (b))
59
60#define altsblock(*asblk.b_un.b_fs) (*asblk.b_un.b_fs)
61#define POWEROF2(num)(((num) & ((num) - 1)) == 0) (((num) & ((num) - 1)) == 0)
62
63void badsb(int, char *);
64int calcsb(char *, int, struct fs *, struct disklabel *, uint32_t);
65static struct disklabel *getdisklabel(char *, int);
66static int readsb(int);
67static int cmpsb(struct fs *, struct fs *);
68static char rdevname[PATH_MAX1024];
69
70long numdirs, listmax, inplast;
71
72/*
73 * Possible locations for the superblock.
74 */
75static const int sbtry[] = SBLOCKSEARCH{ 65536, 8192, 262144, -1 };
76/* locations the 1st alternate sb can be at */
77static const int altsbtry[] = { 32, 64, 128, 144, 160, 192, 256 };
78
79int
80setup(char *dev, int isfsdb)
81{
82 long size, asked, i, j;
83 size_t bmapsize;
84 struct disklabel *lp;
85 off_t sizepb;
86 struct stat statb;
87 struct fs proto;
88 int doskipclean;
89 int32_t maxsymlinklen, nindir;
90 uint32_t cg, inopb;
91 u_int64_t maxfilesize;
92 char *realdev;
93
94 havesb = 0;
95 fswritefd = fsreadfd = -1;
96 doskipclean = skipclean;
97 if ((fsreadfd = opendev(dev, O_RDONLY0x0000, 0, &realdev)) == -1) {
1
Assuming the condition is false
2
Taking false branch
98 printf("Can't open %s: %s\n", dev, strerror(errno(*__errno())));
99 return (0);
100 }
101 if (strncmp(dev, realdev, PATH_MAX1024) != 0) {
3
Assuming the condition is false
4
Taking false branch
102 blockcheck(unrawname(realdev));
103 strlcpy(rdevname, realdev, sizeof(rdevname));
104 setcdevname(rdevname, dev, preen);
105
106 if (isfsdb || !hotroot()) {
107 if (unveil("/dev", "rw") == -1)
108 err(1, "unveil /dev");
109 if (pledge("stdio rpath wpath getpw tty disklabel",
110 NULL((void *)0)) == -1)
111 err(1, "pledge");
112 }
113 }
114
115 if (fstat(fsreadfd, &statb) == -1) {
5
Assuming the condition is false
6
Taking false branch
116 printf("Can't stat %s: %s\n", realdev, strerror(errno(*__errno())));
117 close(fsreadfd);
118 return (0);
119 }
120 if (!S_ISCHR(statb.st_mode)((statb.st_mode & 0170000) == 0020000)) {
7
Assuming the condition is false
8
Taking true branch
121 pfatal("%s is not a character device", realdev);
122 if (reply("CONTINUE") == 0) {
9
Assuming the condition is false
10
Taking false branch
123 close(fsreadfd);
124 return (0);
125 }
126 }
127 if (preen == 0) {
11
Assuming 'preen' is not equal to 0
12
Taking false branch
128 printf("** %s", realdev);
129 if (strncmp(dev, realdev, PATH_MAX1024) != 0)
130 printf(" (%s)", dev);
131 }
132 if (nflag || (fswritefd = opendev(dev, O_WRONLY0x0001, 0, NULL((void *)0))) == -1) {
13
Assuming 'nflag' is 0
14
Assuming the condition is false
15
Taking false branch
133 fswritefd = -1;
134 if (preen)
135 pfatal("NO WRITE ACCESS");
136 printf(" (NO WRITE)");
137 }
138 if (preen
15.1
'preen' is not equal to 0
== 0)
16
Taking false branch
139 printf("\n");
140 fsmodified = 0;
141 lfdir = 0;
142 initbarea(&sblk)(&sblk)->b_dirty = 0; (&sblk)->b_bno = -1; (&
sblk)->b_flags = 0;
;
143 initbarea(&asblk)(&asblk)->b_dirty = 0; (&asblk)->b_bno = -1; (&
asblk)->b_flags = 0;
;
144 sblk.b_un.b_buf = malloc(SBSIZE8192);
145 asblk.b_un.b_buf = malloc(SBSIZE8192);
146 if (sblk.b_un.b_buf == NULL((void *)0) || asblk.b_un.b_buf == NULL((void *)0))
17
Assuming field 'b_buf' is not equal to NULL
18
Assuming field 'b_buf' is not equal to NULL
19
Taking false branch
147 errexit("cannot allocate space for superblock\n");
148 if ((lp = getdisklabel(NULL((void *)0), fsreadfd)) != NULL((void *)0))
20
Taking true branch
149 secsize = lp->d_secsize;
150 else
151 secsize = DEV_BSIZE(1 << 9);
152
153 if (isfsdb) {
21
Assuming 'isfsdb' is 0
22
Taking false branch
154 if (pledge("stdio rpath getpw tty", NULL((void *)0)) == -1)
155 err(1, "pledge");
156 } else if (!hotroot()) {
23
Assuming the condition is false
24
Taking false branch
157#ifndef SMALL
158 if (pledge("stdio getpw", NULL((void *)0)) == -1)
159 err(1, "pledge");
160#else
161 if (pledge("stdio", NULL((void *)0)) == -1)
162 err(1, "pledge");
163#endif
164 }
165
166 /*
167 * Read in the superblock, looking for alternates if necessary
168 */
169 if (readsb(1) == 0) {
25
Taking true branch
170 if (bflag || preen ||
26
Assuming 'bflag' is 0
27
Assuming 'preen' is 0
171 calcsb(realdev, fsreadfd, &proto, lp, SBLOCK_UFS18192) == 0)
28
Calling 'calcsb'
172 return(0);
173 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
174 return (0);
175 for (i = 0; i < sizeof(altsbtry) / sizeof(altsbtry[0]); i++) {
176 bflag = altsbtry[i];
177 /* proto partially setup by calcsb */
178 if (readsb(0) != 0 &&
179 proto.fs_fsize == sblock(*sblk.b_un.b_fs).fs_fsize &&
180 proto.fs_bsize == sblock(*sblk.b_un.b_fs).fs_bsize)
181 goto found;
182 }
183 for (cg = 0; cg < proto.fs_ncg; cg++) {
184 bflag = fsbtodb(&proto, cgsblock(&proto, cg))((((((daddr_t)(&proto)->fs_fpg * (cg)) + (&proto)->
fs_cgoffset * ((cg) & ~((&proto)->fs_cgmask))) + (
&proto)->fs_sblkno)) << (&proto)->fs_fsbtodb
)
;
185 if (readsb(0) != 0 &&
186 proto.fs_fsize == sblock(*sblk.b_un.b_fs).fs_fsize &&
187 proto.fs_bsize == sblock(*sblk.b_un.b_fs).fs_bsize)
188 goto found;
189 }
190 calcsb(realdev, fsreadfd, &proto, lp, SBLOCK_UFS265536);
191 for (cg = 0; cg < proto.fs_ncg; cg++) {
192 bflag = fsbtodb(&proto, cgsblock(&proto, cg))((((((daddr_t)(&proto)->fs_fpg * (cg)) + (&proto)->
fs_cgoffset * ((cg) & ~((&proto)->fs_cgmask))) + (
&proto)->fs_sblkno)) << (&proto)->fs_fsbtodb
)
;
193 if (readsb(0) != 0 &&
194 proto.fs_fsize == sblock(*sblk.b_un.b_fs).fs_fsize &&
195 proto.fs_bsize == sblock(*sblk.b_un.b_fs).fs_bsize)
196 goto found;
197 }
198 if (cg >= proto.fs_ncg) {
199 printf("%s %s\n%s %s\n%s %s\n",
200 "SEARCH FOR ALTERNATE SUPER-BLOCK",
201 "FAILED. YOU MUST USE THE",
202 "-b OPTION TO FSCK_FFS TO SPECIFY THE",
203 "LOCATION OF AN ALTERNATE",
204 "SUPER-BLOCK TO SUPPLY NEEDED",
205 "INFORMATION; SEE fsck_ffs(8).");
206 return(0);
207 }
208found:
209 doskipclean = 0;
210 pwarn("USING ALTERNATE SUPERBLOCK AT %lld\n", (long long)bflag);
211 }
212 if (debug)
213 printf("clean = %d\n", sblock(*sblk.b_un.b_fs).fs_clean);
214 if (sblock(*sblk.b_un.b_fs).fs_clean & FS_ISCLEAN0x01) {
215 if (doskipclean) {
216 pwarn("%sile system is clean; not checking\n",
217 preen ? "f" : "** F");
218 return (-1);
219 }
220 if (!preen)
221 pwarn("** File system is already clean\n");
222 }
223 maxfsblock = sblock(*sblk.b_un.b_fs).fs_size;
224 maxino = sblock(*sblk.b_un.b_fs).fs_ncg * sblock(*sblk.b_un.b_fs).fs_ipg;
225 sizepb = sblock(*sblk.b_un.b_fs).fs_bsize;
226 maxfilesize = sblock(*sblk.b_un.b_fs).fs_bsize * NDADDR12 - 1;
227 for (i = 0; i < NIADDR3; i++) {
228 sizepb *= NINDIR(&sblock)((&(*sblk.b_un.b_fs))->fs_nindir);
229 maxfilesize += sizepb;
230 }
231 /*
232 * Check and potentially fix certain fields in the super block.
233 */
234 if (sblock(*sblk.b_un.b_fs).fs_optim != FS_OPTTIME0 && sblock(*sblk.b_un.b_fs).fs_optim != FS_OPTSPACE1) {
235 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
236 if (reply("SET TO DEFAULT") == 1) {
237 sblock(*sblk.b_un.b_fs).fs_optim = FS_OPTTIME0;
238 sbdirty()sblk.b_dirty = 1;
239 }
240 }
241 if ((sblock(*sblk.b_un.b_fs).fs_minfree < 0 || sblock(*sblk.b_un.b_fs).fs_minfree > 99)) {
242 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
243 sblock(*sblk.b_un.b_fs).fs_minfree);
244 if (reply("SET TO DEFAULT") == 1) {
245 sblock(*sblk.b_un.b_fs).fs_minfree = 10;
246 sbdirty()sblk.b_dirty = 1;
247 }
248 }
249 if (sblock(*sblk.b_un.b_fs).fs_npsect < sblock(*sblk.b_un.b_fs).fs_nsect ||
250 sblock(*sblk.b_un.b_fs).fs_npsect > sblock(*sblk.b_un.b_fs).fs_nsect*2) {
251 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
252 sblock(*sblk.b_un.b_fs).fs_npsect);
253 sblock(*sblk.b_un.b_fs).fs_npsect = sblock(*sblk.b_un.b_fs).fs_nsect;
254 if (preen)
255 printf(" (FIXED)\n");
256 if (preen || reply("SET TO DEFAULT") == 1) {
257 sbdirty()sblk.b_dirty = 1;
258 dirty(&asblk)(&asblk)->b_dirty = 1;
259 }
260 }
261 if (sblock(*sblk.b_un.b_fs).fs_bmask != ~(sblock(*sblk.b_un.b_fs).fs_bsize - 1)) {
262 pwarn("INCORRECT BMASK=%x IN SUPERBLOCK",
263 sblock(*sblk.b_un.b_fs).fs_bmask);
264 sblock(*sblk.b_un.b_fs).fs_bmask = ~(sblock(*sblk.b_un.b_fs).fs_bsize - 1);
265 if (preen)
266 printf(" (FIXED)\n");
267 if (preen || reply("FIX") == 1) {
268 sbdirty()sblk.b_dirty = 1;
269 dirty(&asblk)(&asblk)->b_dirty = 1;
270 }
271 }
272 if (sblock(*sblk.b_un.b_fs).fs_fmask != ~(sblock(*sblk.b_un.b_fs).fs_fsize - 1)) {
273 pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
274 sblock(*sblk.b_un.b_fs).fs_fmask);
275 sblock(*sblk.b_un.b_fs).fs_fmask = ~(sblock(*sblk.b_un.b_fs).fs_fsize - 1);
276 if (preen)
277 printf(" (FIXED)\n");
278 if (preen || reply("FIX") == 1) {
279 sbdirty()sblk.b_dirty = 1;
280 dirty(&asblk)(&asblk)->b_dirty = 1;
281 }
282 }
283 if (1 << sblock(*sblk.b_un.b_fs).fs_bshift != sblock(*sblk.b_un.b_fs).fs_bsize) {
284 pwarn("INCORRECT BSHIFT=%d IN SUPERBLOCK", sblock(*sblk.b_un.b_fs).fs_bshift);
285 sblock(*sblk.b_un.b_fs).fs_bshift = ffs(sblock(*sblk.b_un.b_fs).fs_bsize) - 1;
286 if (preen)
287 printf(" (FIXED)\n");
288 if (preen || reply("FIX") == 1) {
289 sbdirty()sblk.b_dirty = 1;
290 dirty(&asblk)(&asblk)->b_dirty = 1;
291 }
292 }
293 if (1 << sblock(*sblk.b_un.b_fs).fs_fshift != sblock(*sblk.b_un.b_fs).fs_fsize) {
294 pwarn("INCORRECT FSHIFT=%d IN SUPERBLOCK", sblock(*sblk.b_un.b_fs).fs_fshift);
295 sblock(*sblk.b_un.b_fs).fs_fshift = ffs(sblock(*sblk.b_un.b_fs).fs_fsize) - 1;
296 if (preen)
297 printf(" (FIXED)\n");
298 if (preen || reply("FIX") == 1) {
299 sbdirty()sblk.b_dirty = 1;
300 dirty(&asblk)(&asblk)->b_dirty = 1;
301 }
302 }
303 if (sblock(*sblk.b_un.b_fs).fs_inodefmt < FS_44INODEFMT2) {
304 pwarn("Format of filesystem is too old.\n");
305 pwarn("Must update to modern format using a version of fsck\n");
306 pfatal("from before release 5.0 with the command ``fsck -c 2''\n");
307 exit(8);
308 }
309 if (sblock(*sblk.b_un.b_fs).fs_maxfilesize != maxfilesize) {
310 pwarn("INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK",
311 (unsigned long long)sblock(*sblk.b_un.b_fs).fs_maxfilesize);
312 sblock(*sblk.b_un.b_fs).fs_maxfilesize = maxfilesize;
313 if (preen)
314 printf(" (FIXED)\n");
315 if (preen || reply("FIX") == 1) {
316 sbdirty()sblk.b_dirty = 1;
317 dirty(&asblk)(&asblk)->b_dirty = 1;
318 }
319 }
320 maxsymlinklen = sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS1_MAGIC0x011954 ?
321 MAXSYMLINKLEN_UFS1((12 + 3) * sizeof(int32_t)) : MAXSYMLINKLEN_UFS2((12 + 3) * sizeof(int64_t));
322 if (sblock(*sblk.b_un.b_fs).fs_maxsymlinklen != maxsymlinklen) {
323 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
324 sblock(*sblk.b_un.b_fs).fs_maxsymlinklen);
325 sblock(*sblk.b_un.b_fs).fs_maxsymlinklen = maxsymlinklen;
326 if (preen)
327 printf(" (FIXED)\n");
328 if (preen || reply("FIX") == 1) {
329 sbdirty()sblk.b_dirty = 1;
330 dirty(&asblk)(&asblk)->b_dirty = 1;
331 }
332 }
333 if (sblock(*sblk.b_un.b_fs).fs_qbmask != ~sblock(*sblk.b_un.b_fs).fs_bmask) {
334 pwarn("INCORRECT QBMASK=%lx IN SUPERBLOCK",
335 (unsigned long)sblock(*sblk.b_un.b_fs).fs_qbmask);
336 sblock(*sblk.b_un.b_fs).fs_qbmask = ~sblock(*sblk.b_un.b_fs).fs_bmask;
337 if (preen)
338 printf(" (FIXED)\n");
339 if (preen || reply("FIX") == 1) {
340 sbdirty()sblk.b_dirty = 1;
341 dirty(&asblk)(&asblk)->b_dirty = 1;
342 }
343 }
344 if (sblock(*sblk.b_un.b_fs).fs_qfmask != ~sblock(*sblk.b_un.b_fs).fs_fmask) {
345 pwarn("INCORRECT QFMASK=%lx IN SUPERBLOCK",
346 (unsigned long)sblock(*sblk.b_un.b_fs).fs_qfmask);
347 sblock(*sblk.b_un.b_fs).fs_qfmask = ~sblock(*sblk.b_un.b_fs).fs_fmask;
348 if (preen)
349 printf(" (FIXED)\n");
350 if (preen || reply("FIX") == 1) {
351 sbdirty()sblk.b_dirty = 1;
352 dirty(&asblk)(&asblk)->b_dirty = 1;
353 }
354 }
355 if (sblock(*sblk.b_un.b_fs).fs_cgsize != fragroundup(&sblock, CGSIZE(&sblock))((((sizeof(struct cg) + sizeof(int32_t) + (&(*sblk.b_un.b_fs
))->fs_cpg * sizeof(int32_t) + (&(*sblk.b_un.b_fs))->
fs_cpg * (&(*sblk.b_un.b_fs))->fs_nrpos * sizeof(int16_t
) + ((((&(*sblk.b_un.b_fs))->fs_ipg) + ((8) - 1)) / (8
)) + ((((&(*sblk.b_un.b_fs))->fs_fpg) + ((8) - 1)) / (
8)) + ((&(*sblk.b_un.b_fs))->fs_contigsumsize <= 0 ?
0 : (&(*sblk.b_un.b_fs))->fs_contigsumsize * sizeof(int32_t
) + ((((((&(*sblk.b_un.b_fs))->fs_fpg) >> (&
(*sblk.b_un.b_fs))->fs_fragshift)) + ((8) - 1)) / (8))))) +
(&(*sblk.b_un.b_fs))->fs_qfmask) & (&(*sblk.b_un
.b_fs))->fs_fmask)
) {
356 pwarn("INCONSISTENT CGSIZE=%d\n", sblock(*sblk.b_un.b_fs).fs_cgsize);
357 sblock(*sblk.b_un.b_fs).fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock))((((sizeof(struct cg) + sizeof(int32_t) + (&(*sblk.b_un.b_fs
))->fs_cpg * sizeof(int32_t) + (&(*sblk.b_un.b_fs))->
fs_cpg * (&(*sblk.b_un.b_fs))->fs_nrpos * sizeof(int16_t
) + ((((&(*sblk.b_un.b_fs))->fs_ipg) + ((8) - 1)) / (8
)) + ((((&(*sblk.b_un.b_fs))->fs_fpg) + ((8) - 1)) / (
8)) + ((&(*sblk.b_un.b_fs))->fs_contigsumsize <= 0 ?
0 : (&(*sblk.b_un.b_fs))->fs_contigsumsize * sizeof(int32_t
) + ((((((&(*sblk.b_un.b_fs))->fs_fpg) >> (&
(*sblk.b_un.b_fs))->fs_fragshift)) + ((8) - 1)) / (8))))) +
(&(*sblk.b_un.b_fs))->fs_qfmask) & (&(*sblk.b_un
.b_fs))->fs_fmask)
;
358 if (preen)
359 printf(" (FIXED)\n");
360 if (preen || reply("FIX") == 1) {
361 sbdirty()sblk.b_dirty = 1;
362 dirty(&asblk)(&asblk)->b_dirty = 1;
363 }
364 }
365 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS2_MAGIC0x19540119)
366 inopb = sblock(*sblk.b_un.b_fs).fs_bsize / sizeof(struct ufs2_dinode);
367 else
368 inopb = sblock(*sblk.b_un.b_fs).fs_bsize / sizeof(struct ufs1_dinode);
369 if (INOPB(&sblock)((&(*sblk.b_un.b_fs))->fs_inopb) != inopb) {
370 pwarn("INCONSISTENT INOPB=%u\n", INOPB(&sblock)((&(*sblk.b_un.b_fs))->fs_inopb));
371 sblock(*sblk.b_un.b_fs).fs_inopb = inopb;
372 if (preen)
373 printf(" (FIXED)\n");
374 if (preen || reply("FIX") == 1) {
375 sbdirty()sblk.b_dirty = 1;
376 dirty(&asblk)(&asblk)->b_dirty = 1;
377 }
378 }
379 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS2_MAGIC0x19540119)
380 nindir = sblock(*sblk.b_un.b_fs).fs_bsize / sizeof(int64_t);
381 else
382 nindir = sblock(*sblk.b_un.b_fs).fs_bsize / sizeof(int32_t);
383 if (NINDIR(&sblock)((&(*sblk.b_un.b_fs))->fs_nindir) != nindir) {
384 pwarn("INCONSISTENT NINDIR=%d\n", NINDIR(&sblock)((&(*sblk.b_un.b_fs))->fs_nindir));
385 sblock(*sblk.b_un.b_fs).fs_nindir = nindir;
386 if (preen)
387 printf(" (FIXED)\n");
388 if (preen || reply("FIX") == 1) {
389 sbdirty()sblk.b_dirty = 1;
390 dirty(&asblk)(&asblk)->b_dirty = 1;
391 }
392 }
393 if (asblk.b_dirty && !bflag) {
394 memcpy(&altsblock(*asblk.b_un.b_fs), &sblock(*sblk.b_un.b_fs), (size_t)sblock(*sblk.b_un.b_fs).fs_sbsize);
395 flush(fswritefd, &asblk);
396 }
397 /*
398 * read in the summary info.
399 */
400 asked = 0;
401 sblock(*sblk.b_un.b_fs).fs_csp = calloc(1, sblock(*sblk.b_un.b_fs).fs_cssize);
402 if (sblock(*sblk.b_un.b_fs).fs_csp == NULL((void *)0)) {
403 printf("cannot alloc %u bytes for cylinder group summary area\n",
404 (unsigned)sblock(*sblk.b_un.b_fs).fs_cssize);
405 goto badsblabel;
406 }
407 for (i = 0, j = 0; i < sblock(*sblk.b_un.b_fs).fs_cssize; i += sblock(*sblk.b_un.b_fs).fs_bsize, j++) {
408 size = sblock(*sblk.b_un.b_fs).fs_cssize - i < sblock(*sblk.b_un.b_fs).fs_bsize ?
409 sblock(*sblk.b_un.b_fs).fs_cssize - i : sblock(*sblk.b_un.b_fs).fs_bsize;
410 if (bread(fsreadfd, (char *)sblock(*sblk.b_un.b_fs).fs_csp + i,
411 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag)(((*sblk.b_un.b_fs).fs_csaddr + j * (*sblk.b_un.b_fs).fs_frag
) << (&(*sblk.b_un.b_fs))->fs_fsbtodb)
,
412 size) != 0 && !asked) {
413 pfatal("BAD SUMMARY INFORMATION");
414 if (reply("CONTINUE") == 0) {
415 ckfini(0);
416 errexit("%s", "");
417 }
418 asked++;
419 }
420 }
421 /*
422 * allocate and initialize the necessary maps
423 */
424 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t))(((((((maxfsblock) + ((8) - 1)) / (8)))+((sizeof(int16_t))-1)
)/(sizeof(int16_t)))*(sizeof(int16_t)))
;
425 blockmap = calloc(bmapsize, sizeof(char));
426 if (blockmap == NULL((void *)0)) {
427 printf("cannot alloc %zu bytes for blockmap\n", bmapsize);
428 goto badsblabel;
429 }
430 inostathead = calloc((unsigned)(sblock(*sblk.b_un.b_fs).fs_ncg),
431 sizeof(struct inostatlist));
432 if (inostathead == NULL((void *)0)) {
433 printf("cannot alloc %zu bytes for inostathead\n",
434 (unsigned)sblock(*sblk.b_un.b_fs).fs_ncg * sizeof(struct inostatlist));
435 goto badsblabel;
436 }
437 numdirs = MAXIMUM(sblock.fs_cstotal.cs_ndir, 128)((((*sblk.b_un.b_fs).fs_cstotal.cs_ndir) > (128)) ? ((*sblk
.b_un.b_fs).fs_cstotal.cs_ndir) : (128))
;
438 inplast = 0;
439 listmax = numdirs + 10;
440 inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *));
441 if (inpsort == NULL((void *)0)) {
442 printf("cannot alloc %zu bytes for inpsort\n",
443 (unsigned)listmax * sizeof(struct inoinfo *));
444 goto badsblabel;
445 }
446 inphead = calloc((unsigned)numdirs, sizeof(struct inoinfo *));
447 if (inphead == NULL((void *)0)) {
448 printf("cannot alloc %zu bytes for inphead\n",
449 (unsigned)numdirs * sizeof(struct inoinfo *));
450 goto badsblabel;
451 }
452 bufinit();
453 if (sblock(*sblk.b_un.b_fs).fs_flags & FS_DOSOFTDEP0x02)
454 usedsoftdep = 1;
455 else
456 usedsoftdep = 0;
457 return (1);
458
459badsblabel:
460 ckfini(0);
461 return (0);
462}
463
464/*
465 * Read in the super block and its summary info.
466 */
467static int
468readsb(int listerr)
469{
470 daddr_t super = 0;
471 int i;
472
473 if (bflag) {
474 super = bflag;
475
476 if (bread(fsreadfd, (char *)&sblock(*sblk.b_un.b_fs), super, (long)SBSIZE8192) != 0)
477 return (0);
478
479 if (sblock(*sblk.b_un.b_fs).fs_magic != FS_UFS1_MAGIC0x011954 &&
480 sblock(*sblk.b_un.b_fs).fs_magic != FS_UFS2_MAGIC0x19540119) {
481 badsb(listerr, "MAGIC NUMBER WRONG");
482 return (0);
483 }
484 } else {
485 for (i = 0; sbtry[i] != -1; i++) {
486 super = sbtry[i] / DEV_BSIZE(1 << 9);
487
488 if (bread(fsreadfd, (char *)&sblock(*sblk.b_un.b_fs), super,
489 (long)SBSIZE8192) != 0)
490 return (0);
491
492 if (sblock(*sblk.b_un.b_fs).fs_magic != FS_UFS1_MAGIC0x011954 &&
493 sblock(*sblk.b_un.b_fs).fs_magic != FS_UFS2_MAGIC0x19540119)
494 continue; /* Not a superblock */
495
496 /*
497 * Do not look for an FFS1 file system at SBLOCK_UFS2.
498 * Doing so will find the wrong super-block for file
499 * systems with 64k block size.
500 */
501 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS1_MAGIC0x011954 &&
502 sbtry[i] == SBLOCK_UFS265536)
503 continue;
504
505 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS2_MAGIC0x19540119 &&
506 sblock(*sblk.b_un.b_fs).fs_sblockloc != sbtry[i])
507 continue; /* Not a superblock */
508
509 break;
510 }
511
512 if (sbtry[i] == -1) {
513 badsb(listerr, "MAGIC NUMBER WRONG");
514 return (0);
515 }
516 }
517
518 sblk.b_bno = super;
519 sblk.b_size = SBSIZE8192;
520
521 /*
522 * run a few consistency checks of the super block
523 */
524 if (sblock(*sblk.b_un.b_fs).fs_ncg < 1) {
525 badsb(listerr, "NCG OUT OF RANGE");
526 return (0);
527 }
528 if (sblock(*sblk.b_un.b_fs).fs_cpg < 1) {
529 badsb(listerr, "CPG OUT OF RANGE");
530 return (0);
531 }
532 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS1_MAGIC0x011954) {
533 if (sblock(*sblk.b_un.b_fs).fs_ncg * sblock(*sblk.b_un.b_fs).fs_cpg < sblock(*sblk.b_un.b_fs).fs_ncyl ||
534 (sblock(*sblk.b_un.b_fs).fs_ncg - 1) * sblock(*sblk.b_un.b_fs).fs_cpg >= sblock(*sblk.b_un.b_fs).fs_ncyl) {
535 badsb(listerr, "NCYL LESS THAN NCG*CPG");
536 return (0);
537 }
538 }
539 if (sblock(*sblk.b_un.b_fs).fs_sbsize > SBSIZE8192) {
540 badsb(listerr, "SBSIZE PREPOSTEROUSLY LARGE");
541 return (0);
542 }
543
544 if (!POWEROF2(sblock.fs_bsize)((((*sblk.b_un.b_fs).fs_bsize) & (((*sblk.b_un.b_fs).fs_bsize
) - 1)) == 0)
|| sblock(*sblk.b_un.b_fs).fs_bsize < MINBSIZE4096 ||
545 sblock(*sblk.b_un.b_fs).fs_bsize > MAXBSIZE(64 * 1024)) {
546 badsb(listerr, "ILLEGAL BLOCK SIZE IN SUPERBLOCK");
547 return (0);
548 }
549
550 if (!POWEROF2(sblock.fs_fsize)((((*sblk.b_un.b_fs).fs_fsize) & (((*sblk.b_un.b_fs).fs_fsize
) - 1)) == 0)
|| sblock(*sblk.b_un.b_fs).fs_fsize > sblock(*sblk.b_un.b_fs).fs_bsize ||
551 sblock(*sblk.b_un.b_fs).fs_fsize < sblock(*sblk.b_un.b_fs).fs_bsize / MAXFRAG8) {
552 badsb(listerr, "ILLEGAL FRAGMENT SIZE IN SUPERBLOCK");
553 return (0);
554 }
555
556 if (bflag)
557 goto out;
558 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1)((((daddr_t)(&(*sblk.b_un.b_fs))->fs_fpg * ((*sblk.b_un
.b_fs).fs_ncg - 1)) + (&(*sblk.b_un.b_fs))->fs_cgoffset
* (((*sblk.b_un.b_fs).fs_ncg - 1) & ~((&(*sblk.b_un.
b_fs))->fs_cgmask))) + (&(*sblk.b_un.b_fs))->fs_sblkno
)
, sblock(*sblk.b_un.b_fs).fs_sbsize);
559 if (asblk.b_errs)
560 return (0);
561 if (cmpsb(&sblock(*sblk.b_un.b_fs), &altsblock(*asblk.b_un.b_fs))) {
562 if (debug) {
563 long *nlp, *olp, *endlp;
564
565 printf("superblock mismatches\n");
566 nlp = (long *)&altsblock(*asblk.b_un.b_fs);
567 olp = (long *)&sblock(*sblk.b_un.b_fs);
568 endlp = olp + (sblock(*sblk.b_un.b_fs).fs_sbsize / sizeof *olp);
569 for ( ; olp < endlp; olp++, nlp++) {
570 if (*olp == *nlp)
571 continue;
572 printf("offset %d, original %ld, alternate %ld\n",
573 (int)(olp - (long *)&sblock(*sblk.b_un.b_fs)), *olp, *nlp);
574 }
575 }
576 badsb(listerr,
577 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN LAST ALTERNATE");
578 return (0);
579 }
580out:
581 if (sblock(*sblk.b_un.b_fs).fs_magic == FS_UFS1_MAGIC0x011954) {
582 sblock(*sblk.b_un.b_fs).fs_time = sblock(*sblk.b_un.b_fs).fs_ffs1_time;
583 sblock(*sblk.b_un.b_fs).fs_size = sblock(*sblk.b_un.b_fs).fs_ffs1_size;
584 sblock(*sblk.b_un.b_fs).fs_dsize = sblock(*sblk.b_un.b_fs).fs_ffs1_dsize;
585 sblock(*sblk.b_un.b_fs).fs_csaddr = sblock(*sblk.b_un.b_fs).fs_ffs1_csaddr;
586 sblock(*sblk.b_un.b_fs).fs_cstotal.cs_ndir = sblock(*sblk.b_un.b_fs).fs_ffs1_cstotal.cs_ndir;
587 sblock(*sblk.b_un.b_fs).fs_cstotal.cs_nbfree = sblock(*sblk.b_un.b_fs).fs_ffs1_cstotal.cs_nbfree;
588 sblock(*sblk.b_un.b_fs).fs_cstotal.cs_nifree = sblock(*sblk.b_un.b_fs).fs_ffs1_cstotal.cs_nifree;
589 sblock(*sblk.b_un.b_fs).fs_cstotal.cs_nffree = sblock(*sblk.b_un.b_fs).fs_ffs1_cstotal.cs_nffree;
590 }
591 havesb = 1;
592 return (1);
593}
594
595void
596badsb(int listerr, char *s)
597{
598
599 if (!listerr)
600 return;
601 if (preen)
602 printf("%s: ", cdevname());
603 pfatal("BAD SUPER BLOCK: %s\n", s);
604}
605
606/*
607 * Calculate a prototype superblock based on information in the disk label.
608 * When done the cgsblock macro can be calculated and the fs_ncg field
609 * can be used. Do NOT attempt to use other macros without verifying that
610 * their needed information is available!
611 */
612int
613calcsb(char *dev, int devfd, struct fs *fs, struct disklabel *lp,
614 uint32_t sblockloc)
615{
616 struct partition *pp;
617 char *cp;
618 int i;
619
620 cp = strchr(dev, '\0');
621 if ((cp == NULL((void *)0) || (cp[-1] < 'a' || cp[-1] >= 'a' + MAXPARTITIONS16)) &&
29
Assuming 'cp' is not equal to NULL
30
Assuming the condition is false
31
Assuming the condition is false
622 !isdigit((unsigned char)cp[-1])) {
623 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
624 return (0);
625 }
626 cp--;
627 if (lp
31.1
'lp' is not equal to NULL
== NULL((void *)0))
32
Taking false branch
628 pfatal("%s: CANNOT READ DISKLABEL\n", dev);
629 if (isdigit((unsigned char)*cp))
33
Taking false branch
630 pp = &lp->d_partitions[0];
631 else
632 pp = &lp->d_partitions[*cp - 'a'];
633 if (pp->p_fstype != FS_BSDFFS7) {
34
Assuming field 'p_fstype' is equal to FS_BSDFFS
35
Taking false branch
634 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
635 dev, pp->p_fstype < FSMAXTYPES(sizeof(fstypenames) / sizeof(fstypenames[0]) - 1) ?
636 fstypenames[pp->p_fstype] : "unknown");
637 return (0);
638 }
639 memset(fs, 0, sizeof(struct fs));
640 fs->fs_fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock)(((pp->p_fragblock) == 0 ? 0 : (1 << (((pp->p_fragblock
) & 0x07) - 1))) == 0 ? 0 : (((pp->p_fragblock) == 0 ?
0 : (1 << (((pp->p_fragblock) >> 3) + 12))) /
((pp->p_fragblock) == 0 ? 0 : (1 << (((pp->p_fragblock
) & 0x07) - 1)))))
;
36
Assuming field 'p_fragblock' is equal to 0
37
'?' condition is true
38
'?' condition is true
641 fs->fs_frag = DISKLABELV1_FFS_FRAG(pp->p_fragblock)((pp->p_fragblock) == 0 ? 0 : (1 << (((pp->p_fragblock
) & 0x07) - 1)))
;
39
'?' condition is true
642 fs->fs_fpg = pp->p_cpg * fs->fs_frag;
643 fs->fs_bsize = fs->fs_fsize * fs->fs_frag;
644 fs->fs_nspf = DL_SECTOBLK(lp, fs->fs_fsize / lp->d_secsize)((fs->fs_fsize / lp->d_secsize) * ((lp)->d_secsize /
(1 << 9)))
;
40
The value 0 is assigned to 'proto.fs_nspf'
645 for (fs->fs_fsbtodb = 0, i = NSPF(fs)((fs)->fs_nspf); i > 1; i >>= 1)
41
Loop condition is false. Execution continues on line 651
646 fs->fs_fsbtodb++;
647 /*
648 * fs->fs_size is in fragments, DL_GETPSIZE() is in disk sectors
649 * and fs_nspf is in DEV_BSIZE blocks. Shake well.
650 */
651 fs->fs_size = DL_SECTOBLK(lp, DL_GETPSIZE(pp))(((((u_int64_t)(pp)->p_sizeh << 32) + (pp)->p_size
)) * ((lp)->d_secsize / (1 << 9)))
/ fs->fs_nspf
;
42
Division by zero
652 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg)(((fs->fs_size) + ((fs->fs_fpg) - 1)) / (fs->fs_fpg)
)
;
653 /* we can't use lp->d_sbsize, it is the max sb size */
654 fs->fs_sblkno = roundup((((((((sblockloc + 8192) + ((fs->fs_fsize) - 1)) / (fs->
fs_fsize)))+((fs->fs_frag)-1))/(fs->fs_frag))*(fs->fs_frag
))
655 howmany(sblockloc + SBSIZE, fs->fs_fsize),(((((((sblockloc + 8192) + ((fs->fs_fsize) - 1)) / (fs->
fs_fsize)))+((fs->fs_frag)-1))/(fs->fs_frag))*(fs->fs_frag
))
656 fs->fs_frag)(((((((sblockloc + 8192) + ((fs->fs_fsize) - 1)) / (fs->
fs_fsize)))+((fs->fs_frag)-1))/(fs->fs_frag))*(fs->fs_frag
))
;
657
658 return (1);
659}
660
661static struct disklabel *
662getdisklabel(char *s, int fd)
663{
664 static struct disklabel lab;
665
666 if (ioctl(fd, DIOCGDINFO((unsigned long)0x40000000 | ((sizeof(struct disklabel) &
0x1fff) << 16) | ((('d')) << 8) | ((101)))
, (char *)&lab) == -1) {
667 if (s == NULL((void *)0))
668 return (NULL((void *)0));
669 pwarn("ioctl (GCINFO): %s\n", strerror(errno(*__errno())));
670 errexit("%s: can't read disk label\n", s);
671 }
672 return (&lab);
673}
674
675/*
676 * Compare two superblocks
677 */
678static int
679cmpsb(struct fs *sb, struct fs *asb)
680{
681 /*
682 * Only compare fields which should be the same, and ignore ones
683 * likely to change to ensure future compatibility.
684 */
685 if (asb->fs_sblkno != sb->fs_sblkno ||
686 asb->fs_cblkno != sb->fs_cblkno ||
687 asb->fs_iblkno != sb->fs_iblkno ||
688 asb->fs_dblkno != sb->fs_dblkno ||
689 asb->fs_cgoffset != sb->fs_cgoffset ||
690 asb->fs_cgmask != sb->fs_cgmask ||
691 asb->fs_ncg != sb->fs_ncg ||
692 asb->fs_bsize != sb->fs_bsize ||
693 asb->fs_fsize != sb->fs_fsize ||
694 asb->fs_frag != sb->fs_frag ||
695 asb->fs_bmask != sb->fs_bmask ||
696 asb->fs_fmask != sb->fs_fmask ||
697 asb->fs_bshift != sb->fs_bshift ||
698 asb->fs_fshift != sb->fs_fshift ||
699 asb->fs_fragshift != sb->fs_fragshift ||
700 asb->fs_fsbtodb != sb->fs_fsbtodb ||
701 asb->fs_sbsize != sb->fs_sbsize ||
702 asb->fs_nindir != sb->fs_nindir ||
703 asb->fs_inopb != sb->fs_inopb ||
704 asb->fs_cssize != sb->fs_cssize ||
705 asb->fs_cpg != sb->fs_cpg ||
706 asb->fs_ipg != sb->fs_ipg ||
707 asb->fs_fpg != sb->fs_fpg ||
708 asb->fs_magic != sb->fs_magic)
709 return (1);
710 /* they're the same */
711 return (0);
712}