scan_ffs.c revision 1.9.4.2 1 1.9.4.2 riz /* $NetBSD: scan_ffs.c,v 1.9.4.2 2006/08/11 04:41:25 riz Exp $ */
2 1.9.4.2 riz
3 1.9.4.2 riz /*
4 1.9.4.2 riz * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.9.4.2 riz * All rights reserved.
6 1.9.4.2 riz *
7 1.9.4.2 riz * This code is derived from software contributed to The NetBSD Foundation
8 1.9.4.2 riz * by Juan Romero Pardines.
9 1.9.4.2 riz *
10 1.9.4.2 riz * Redistribution and use in source and binary forms, with or without
11 1.9.4.2 riz * modification, are permitted provided that the following conditions
12 1.9.4.2 riz * are met:
13 1.9.4.2 riz * 1. Redistributions of source code must retain the above copyright
14 1.9.4.2 riz * notice, this list of conditions and the following disclaimer.
15 1.9.4.2 riz * 2. Redistributions in binary form must reproduce the above copyright
16 1.9.4.2 riz * notice, this list of conditions and the following disclaimer in the
17 1.9.4.2 riz * documentation and/or other materials provided with the distribution.
18 1.9.4.2 riz * 3. All advertising materials mentioning features or use of this software
19 1.9.4.2 riz * must display the following acknowledgement:
20 1.9.4.2 riz * This product includes software developed by Juan Romero Pardines
21 1.9.4.2 riz * for the NetBSD Foundation, Inc. and its contributors.
22 1.9.4.2 riz * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.9.4.2 riz * contributors may be used to endorse or promote products derived
24 1.9.4.2 riz * from this software without specific prior written permission.
25 1.9.4.2 riz *
26 1.9.4.2 riz * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.9.4.2 riz * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.9.4.2 riz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.9.4.2 riz * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.9.4.2 riz * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.9.4.2 riz * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.9.4.2 riz * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.9.4.2 riz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.9.4.2 riz * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.9.4.2 riz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.9.4.2 riz * POSSIBILITY OF SUCH DAMAGE.
37 1.9.4.2 riz */
38 1.9.4.2 riz
39 1.9.4.2 riz /*
40 1.9.4.2 riz * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
41 1.9.4.2 riz * All rights reserved.
42 1.9.4.2 riz *
43 1.9.4.2 riz * Redistribution and use in source and binary forms, with or without
44 1.9.4.2 riz * modification, are permitted provided that the following conditions
45 1.9.4.2 riz * are met:
46 1.9.4.2 riz * 1. Redistributions of source code must retain the above copyright
47 1.9.4.2 riz * notice, this list of conditions and the following disclaimer.
48 1.9.4.2 riz * 2. Redistributions in binary form must reproduce the above copyright
49 1.9.4.2 riz * notice, this list of conditions and the following disclaimer in the
50 1.9.4.2 riz * documentation and/or other materials provided with the distribution.
51 1.9.4.2 riz *
52 1.9.4.2 riz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 1.9.4.2 riz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 1.9.4.2 riz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 1.9.4.2 riz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 1.9.4.2 riz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 1.9.4.2 riz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 1.9.4.2 riz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 1.9.4.2 riz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 1.9.4.2 riz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 1.9.4.2 riz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.9.4.2 riz */
63 1.9.4.2 riz
64 1.9.4.2 riz /*
65 1.9.4.2 riz * Currently it can detect:
66 1.9.4.2 riz * o FFSv1 fsize/bsize: 512/4096, 1024/8192, 2048/16384.
67 1.9.4.2 riz * o FFSv2 fsize/bsize: 512/4096, 1024/8192, 2048/16384,
68 1.9.4.2 riz * 4096/32768, 8192/65536.
69 1.9.4.2 riz * o LFSv[12] fsize/bsize: 512/4096, 1024/8192, 2048/16384,
70 1.9.4.2 riz * 4096/32768, 8192/65536.
71 1.9.4.2 riz *
72 1.9.4.2 riz * TODO:
73 1.9.4.2 riz * o Detect FFSv1 partitions with fsize/bsize > 2048/16384.
74 1.9.4.2 riz *
75 1.9.4.2 riz * -- xtraeme --
76 1.9.4.2 riz */
77 1.9.4.2 riz
78 1.9.4.2 riz #include <sys/cdefs.h>
79 1.9.4.2 riz #ifndef lint
80 1.9.4.2 riz __RCSID("$NetBSD: scan_ffs.c,v 1.9.4.2 2006/08/11 04:41:25 riz Exp $");
81 1.9.4.2 riz #endif /* not lint */
82 1.9.4.2 riz
83 1.9.4.2 riz #include <sys/types.h>
84 1.9.4.2 riz #include <sys/param.h>
85 1.9.4.2 riz #include <sys/disklabel.h>
86 1.9.4.2 riz #include <sys/dkio.h>
87 1.9.4.2 riz #include <sys/ioctl.h>
88 1.9.4.2 riz #include <sys/fcntl.h>
89 1.9.4.2 riz #include <sys/queue.h>
90 1.9.4.2 riz #include <sys/mount.h>
91 1.9.4.2 riz
92 1.9.4.2 riz #include <ufs/ufs/dinode.h>
93 1.9.4.2 riz #include <ufs/lfs/lfs.h>
94 1.9.4.2 riz
95 1.9.4.2 riz /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
96 1.9.4.2 riz #undef fsbtodb
97 1.9.4.2 riz #undef dbtofsb
98 1.9.4.2 riz #undef blkoff
99 1.9.4.2 riz #undef fragoff
100 1.9.4.2 riz #undef lblktosize
101 1.9.4.2 riz #undef lblkno
102 1.9.4.2 riz #undef numfrags
103 1.9.4.2 riz #undef blkroundup
104 1.9.4.2 riz #undef fragroundup
105 1.9.4.2 riz #undef fragstoblks
106 1.9.4.2 riz #undef blkstofrags
107 1.9.4.2 riz #undef fragnum
108 1.9.4.2 riz #undef blknum
109 1.9.4.2 riz #undef blksize
110 1.9.4.2 riz #undef INOPB
111 1.9.4.2 riz #undef INOPF
112 1.9.4.2 riz #undef NINDIR
113 1.9.4.2 riz
114 1.9.4.2 riz #include <ufs/ffs/fs.h>
115 1.9.4.2 riz
116 1.9.4.2 riz /* Undefine macros defined by both lfs/lfs.h and ffs/fs.h */
117 1.9.4.2 riz /* ...to make sure we don't later depend on their (ambigious) definition */
118 1.9.4.2 riz #undef fsbtodb
119 1.9.4.2 riz #undef dbtofsb
120 1.9.4.2 riz #undef blkoff
121 1.9.4.2 riz #undef fragoff
122 1.9.4.2 riz #undef lblktosize
123 1.9.4.2 riz #undef lblkno
124 1.9.4.2 riz #undef numfrags
125 1.9.4.2 riz #undef blkroundup
126 1.9.4.2 riz #undef fragroundup
127 1.9.4.2 riz #undef fragstoblks
128 1.9.4.2 riz #undef blkstofrags
129 1.9.4.2 riz #undef fragnum
130 1.9.4.2 riz #undef blknum
131 1.9.4.2 riz #undef blksize
132 1.9.4.2 riz #undef INOPB
133 1.9.4.2 riz #undef INOPF
134 1.9.4.2 riz #undef NINDIR
135 1.9.4.2 riz
136 1.9.4.2 riz #include <unistd.h>
137 1.9.4.2 riz #include <stdlib.h>
138 1.9.4.2 riz #include <stdio.h>
139 1.9.4.2 riz #include <string.h>
140 1.9.4.2 riz #include <err.h>
141 1.9.4.2 riz #include <util.h>
142 1.9.4.2 riz
143 1.9.4.2 riz /* common struct for FFS/LFS */
144 1.9.4.2 riz struct sblockinfo {
145 1.9.4.2 riz struct lfs *lfs;
146 1.9.4.2 riz struct fs *ffs;
147 1.9.4.2 riz u_int64_t lfs_off;
148 1.9.4.2 riz u_int64_t ffs_off;
149 1.9.4.2 riz char lfs_path[MAXMNTLEN];
150 1.9.4.2 riz char ffs_path[MAXMNTLEN];
151 1.9.4.2 riz } sbinfo;
152 1.9.4.2 riz
153 1.9.4.2 riz static daddr_t blk, lastblk;
154 1.9.4.2 riz
155 1.9.4.2 riz static int eflag = 0;
156 1.9.4.2 riz static int flags = 0;
157 1.9.4.2 riz static int sbaddr = 0; /* counter for the LFS superblocks */
158 1.9.4.2 riz
159 1.9.4.2 riz static char device[MAXPATHLEN];
160 1.9.4.2 riz static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2", "LFS" };
161 1.9.4.2 riz
162 1.9.4.2 riz #define FSTYPE_NONE 0
163 1.9.4.2 riz #define FSTYPE_FFSV1 1
164 1.9.4.2 riz #define FSTYPE_FFSV2 2
165 1.9.4.2 riz
166 1.9.4.2 riz #define SBCOUNT 64 /* may be changed */
167 1.9.4.2 riz #define SBPASS (SBCOUNT * SBLOCKSIZE / 512)
168 1.9.4.2 riz
169 1.9.4.2 riz /* This is only useful for LFS */
170 1.9.4.2 riz
171 1.9.4.2 riz /* first sblock address contains the correct offset */
172 1.9.4.2 riz #define FIRST_SBLOCK_ADDRESS 1
173 1.9.4.2 riz /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
174 1.9.4.2 riz #define SECOND_SBLOCK_ADDRESS 2
175 1.9.4.2 riz /* last sblock address in a LFS partition */
176 1.9.4.2 riz #define MAX_SBLOCK_ADDRESS 10
177 1.9.4.2 riz
178 1.9.4.2 riz enum { NADA, VERBOSE, LABELS };
179 1.9.4.2 riz
180 1.9.4.2 riz /* FFS functions */
181 1.9.4.2 riz static void ffs_printpart(int, size_t, int);
182 1.9.4.2 riz static void ffs_scan(int);
183 1.9.4.2 riz static int ffs_checkver(void);
184 1.9.4.2 riz /* LFS functions */
185 1.9.4.2 riz static void lfs_printpart(int, int, struct sblockinfo *);
186 1.9.4.2 riz static void lfs_scan(int);
187 1.9.4.2 riz /* common functions */
188 1.9.4.2 riz static void usage(void) __attribute__((__noreturn__));
189 1.9.4.2 riz static int scan_disk(int, daddr_t, daddr_t, int);
190 1.9.4.2 riz
191 1.9.4.2 riz static int
192 1.9.4.2 riz ffs_checkver(void)
193 1.9.4.2 riz {
194 1.9.4.2 riz switch (sbinfo.ffs->fs_magic) {
195 1.9.4.2 riz case FS_UFS1_MAGIC:
196 1.9.4.2 riz case FS_UFS1_MAGIC_SWAPPED:
197 1.9.4.2 riz sbinfo.ffs->fs_size = sbinfo.ffs->fs_old_size;
198 1.9.4.2 riz return FSTYPE_FFSV1;
199 1.9.4.2 riz case FS_UFS2_MAGIC:
200 1.9.4.2 riz case FS_UFS2_MAGIC_SWAPPED:
201 1.9.4.2 riz return FSTYPE_FFSV2;
202 1.9.4.2 riz default:
203 1.9.4.2 riz return FSTYPE_NONE;
204 1.9.4.2 riz }
205 1.9.4.2 riz }
206 1.9.4.2 riz
207 1.9.4.2 riz static void
208 1.9.4.2 riz ffs_printpart(int flag, size_t ffsize, int n)
209 1.9.4.2 riz {
210 1.9.4.2 riz
211 1.9.4.2 riz int fstype = ffs_checkver();
212 1.9.4.2 riz
213 1.9.4.2 riz switch (flag) {
214 1.9.4.2 riz case VERBOSE:
215 1.9.4.2 riz (void)printf("block: %" PRIu64 " id %x,%x size %" PRIu64 "\n",
216 1.9.4.2 riz blk + (n / 512),
217 1.9.4.2 riz sbinfo.ffs->fs_id[0],
218 1.9.4.2 riz sbinfo.ffs->fs_id[1], sbinfo.ffs->fs_size);
219 1.9.4.2 riz break;
220 1.9.4.2 riz case LABELS:
221 1.9.4.2 riz (void)printf("X: %9" PRIu64,
222 1.9.4.2 riz (uint64_t)((off_t)sbinfo.ffs->fs_size *
223 1.9.4.2 riz sbinfo.ffs->fs_fsize / 512));
224 1.9.4.2 riz switch (fstype) {
225 1.9.4.2 riz case FSTYPE_FFSV1:
226 1.9.4.2 riz (void)printf(" %9" PRIu64,
227 1.9.4.2 riz blk + (n / 512) - (2 * SBLOCKSIZE / 512));
228 1.9.4.2 riz break;
229 1.9.4.2 riz case FSTYPE_FFSV2:
230 1.9.4.2 riz (void)printf(" %9" PRIu64,
231 1.9.4.2 riz blk + (n / 512) -
232 1.9.4.2 riz (ffsize * SBLOCKSIZE / 512 + 128));
233 1.9.4.2 riz break;
234 1.9.4.2 riz default:
235 1.9.4.2 riz break;
236 1.9.4.2 riz }
237 1.9.4.2 riz (void)printf(" 4.2BSD %6d %5d %7d # %s [%s]\n",
238 1.9.4.2 riz sbinfo.ffs->fs_fsize, sbinfo.ffs->fs_bsize,
239 1.9.4.2 riz sbinfo.ffs->fs_old_cpg,
240 1.9.4.2 riz sbinfo.ffs_path, fstypes[fstype]);
241 1.9.4.2 riz break;
242 1.9.4.2 riz default:
243 1.9.4.2 riz (void)printf("%s ", fstypes[fstype]);
244 1.9.4.2 riz switch (fstype) {
245 1.9.4.2 riz case FSTYPE_FFSV1:
246 1.9.4.2 riz (void)printf("at %" PRIu64,
247 1.9.4.2 riz blk + (n / 512) - (2 * SBLOCKSIZE / 512));
248 1.9.4.2 riz break;
249 1.9.4.2 riz case FSTYPE_FFSV2:
250 1.9.4.2 riz (void)printf("at %" PRIu64,
251 1.9.4.2 riz blk + (n / 512) -
252 1.9.4.2 riz (ffsize * SBLOCKSIZE / 512 + 128));
253 1.9.4.2 riz break;
254 1.9.4.2 riz default:
255 1.9.4.2 riz break;
256 1.9.4.2 riz }
257 1.9.4.2 riz (void)printf(" size %" PRIu64 ", last mounted on %s\n",
258 1.9.4.2 riz (uint64_t)((off_t)sbinfo.ffs->fs_size *
259 1.9.4.2 riz sbinfo.ffs->fs_fsize / 512), sbinfo.ffs_path);
260 1.9.4.2 riz break;
261 1.9.4.2 riz }
262 1.9.4.2 riz }
263 1.9.4.2 riz
264 1.9.4.2 riz static void
265 1.9.4.2 riz ffs_scan(int n)
266 1.9.4.2 riz {
267 1.9.4.2 riz int fstype = ffs_checkver();
268 1.9.4.2 riz size_t i;
269 1.9.4.2 riz
270 1.9.4.2 riz /*
271 1.9.4.2 riz * XXX:
272 1.9.4.2 riz * It cannot find FFSv1 partitions with fsize/bsize > 2048/16384,
273 1.9.4.2 riz * same problem found in the original program that comes from
274 1.9.4.2 riz * OpenBSD (scan_ffs(8)).
275 1.9.4.2 riz */
276 1.9.4.2 riz if (flags & VERBOSE)
277 1.9.4.2 riz ffs_printpart(VERBOSE, NADA, n);
278 1.9.4.2 riz switch (fstype) {
279 1.9.4.2 riz case FSTYPE_FFSV1:
280 1.9.4.2 riz if (((blk + (n / 512)) - lastblk) == (SBLOCKSIZE / 512)) {
281 1.9.4.2 riz if (flags & LABELS)
282 1.9.4.2 riz ffs_printpart(LABELS, NADA, n);
283 1.9.4.2 riz else
284 1.9.4.2 riz ffs_printpart(NADA, NADA, n);
285 1.9.4.2 riz }
286 1.9.4.2 riz break;
287 1.9.4.2 riz case FSTYPE_FFSV2:
288 1.9.4.2 riz /*
289 1.9.4.2 riz * That checks for FFSv2 partitions with fragsize/blocksize:
290 1.9.4.2 riz * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
291 1.9.4.2 riz * Really enough for now.
292 1.9.4.2 riz */
293 1.9.4.2 riz for (i = 1; i < 16; i <<= 1)
294 1.9.4.2 riz if (((blk + (n / 512)) - lastblk) ==
295 1.9.4.2 riz (i * SBLOCKSIZE / 512)) {
296 1.9.4.2 riz if (flags & LABELS)
297 1.9.4.2 riz ffs_printpart(LABELS, i, n);
298 1.9.4.2 riz else
299 1.9.4.2 riz ffs_printpart(NADA, i, n);
300 1.9.4.2 riz }
301 1.9.4.2 riz break;
302 1.9.4.2 riz }
303 1.9.4.2 riz }
304 1.9.4.2 riz
305 1.9.4.2 riz static void
306 1.9.4.2 riz lfs_printpart(int flag, int n, struct sblockinfo *sbi)
307 1.9.4.2 riz {
308 1.9.4.2 riz if (flags & VERBOSE)
309 1.9.4.2 riz (void)printf("block: %" PRIu64 " size %" PRIu32 "\n",
310 1.9.4.2 riz blk + (n / 512), sbi->lfs->lfs_size);
311 1.9.4.2 riz switch (flag) {
312 1.9.4.2 riz case LABELS:
313 1.9.4.2 riz (void)printf("X: %9" PRIu64,
314 1.9.4.2 riz (uint64_t)((off_t)sbi->lfs->lfs_size *
315 1.9.4.2 riz sbi->lfs->lfs_fsize / 512));
316 1.9.4.2 riz (void)printf(" %9" PRIu64, sbi->lfs_off);
317 1.9.4.2 riz (void)printf(" 4.4LFS %6d %5d %7d # %s [LFSv%d]\n",
318 1.9.4.2 riz sbi->lfs->lfs_fsize, sbi->lfs->lfs_bsize,
319 1.9.4.2 riz sbi->lfs->lfs_nseg, sbi->lfs_path,
320 1.9.4.2 riz sbi->lfs->lfs_version);
321 1.9.4.2 riz break;
322 1.9.4.2 riz default:
323 1.9.4.2 riz (void)printf("LFSv%d ", sbinfo.lfs->lfs_version);
324 1.9.4.2 riz (void)printf("at %" PRIu64, sbinfo.lfs_off);
325 1.9.4.2 riz (void)printf(" size %" PRIu64 ", last mounted on %s\n",
326 1.9.4.2 riz (uint64_t)((off_t)sbinfo.lfs->lfs_size *
327 1.9.4.2 riz sbinfo.lfs->lfs_fsize / 512), sbinfo.lfs_path);
328 1.9.4.2 riz break;
329 1.9.4.2 riz }
330 1.9.4.2 riz }
331 1.9.4.2 riz
332 1.9.4.2 riz static void
333 1.9.4.2 riz lfs_scan(int n)
334 1.9.4.2 riz {
335 1.9.4.2 riz /* backup offset */
336 1.9.4.2 riz lastblk = blk + (n / 512) - (LFS_SBPAD / 512);
337 1.9.4.2 riz /* increment counter */
338 1.9.4.2 riz ++sbaddr;
339 1.9.4.2 riz
340 1.9.4.2 riz switch (sbaddr) {
341 1.9.4.2 riz /*
342 1.9.4.2 riz * first superblock contains the right offset, but lfs_fsmnt is
343 1.9.4.2 riz * empty... afortunately the next superblock address has it.
344 1.9.4.2 riz */
345 1.9.4.2 riz case FIRST_SBLOCK_ADDRESS:
346 1.9.4.2 riz /* copy partition offset */
347 1.9.4.2 riz if (sbinfo.lfs_off != lastblk)
348 1.9.4.2 riz sbinfo.lfs_off = blk + (n / 512) - (LFS_SBPAD / 512);
349 1.9.4.2 riz break;
350 1.9.4.2 riz case SECOND_SBLOCK_ADDRESS:
351 1.9.4.2 riz /* copy the path of last mount */
352 1.9.4.2 riz (void)memcpy(sbinfo.lfs_path,
353 1.9.4.2 riz sbinfo.lfs->lfs_fsmnt, MAXMNTLEN);
354 1.9.4.2 riz /* print now that we have the info */
355 1.9.4.2 riz if (flags & LABELS)
356 1.9.4.2 riz lfs_printpart(LABELS, n, &sbinfo);
357 1.9.4.2 riz else
358 1.9.4.2 riz lfs_printpart(NADA, n, &sbinfo);
359 1.9.4.2 riz /* clear our struct */
360 1.9.4.2 riz (void)memset(&sbinfo, 0, sizeof(sbinfo));
361 1.9.4.2 riz break;
362 1.9.4.2 riz case MAX_SBLOCK_ADDRESS:
363 1.9.4.2 riz /*
364 1.9.4.2 riz * reset the counter, this is the last superblock address,
365 1.9.4.2 riz * the next one will be another partition maybe.
366 1.9.4.2 riz */
367 1.9.4.2 riz sbaddr = 0;
368 1.9.4.2 riz break;
369 1.9.4.2 riz default:
370 1.9.4.2 riz break;
371 1.9.4.2 riz }
372 1.9.4.2 riz }
373 1.9.4.2 riz
374 1.9.4.2 riz static int
375 1.9.4.2 riz scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
376 1.9.4.2 riz {
377 1.9.4.2 riz u_int8_t buf[SBLOCKSIZE * SBCOUNT];
378 1.9.4.2 riz int n, fstype;
379 1.9.4.2 riz
380 1.9.4.2 riz n = fstype = 0;
381 1.9.4.2 riz lastblk = -1;
382 1.9.4.2 riz
383 1.9.4.2 riz /* clear our struct before using it */
384 1.9.4.2 riz (void)memset(&sbinfo, 0, sizeof(sbinfo));
385 1.9.4.2 riz
386 1.9.4.2 riz if (fflags & LABELS)
387 1.9.4.2 riz (void)printf(
388 1.9.4.2 riz "# size offset fstype [fsize bsize cpg/sgs]\n");
389 1.9.4.2 riz
390 1.9.4.2 riz for (blk = beg; blk <= ((end < 0) ? blk: end); blk += SBPASS) {
391 1.9.4.2 riz (void)memset(buf, 0, sizeof(buf));
392 1.9.4.2 riz
393 1.9.4.2 riz if (pread(fd, buf, sizeof(buf), (off_t)blk * 512) == (off_t)-1)
394 1.9.4.2 riz err(1, "pread");
395 1.9.4.2 riz
396 1.9.4.2 riz for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
397 1.9.4.2 riz sbinfo.ffs = (struct fs *)(void *)&buf[n];
398 1.9.4.2 riz sbinfo.lfs = (struct lfs *)(void *)&buf[n];
399 1.9.4.2 riz fstype = ffs_checkver();
400 1.9.4.2 riz switch (fstype) {
401 1.9.4.2 riz case FSTYPE_FFSV1:
402 1.9.4.2 riz case FSTYPE_FFSV2:
403 1.9.4.2 riz ffs_scan(n);
404 1.9.4.2 riz lastblk = blk + (n / 512);
405 1.9.4.2 riz (void)memcpy(sbinfo.ffs_path,
406 1.9.4.2 riz sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
407 1.9.4.2 riz break;
408 1.9.4.2 riz case FSTYPE_NONE:
409 1.9.4.2 riz /* maybe LFS? */
410 1.9.4.2 riz if (sbinfo.lfs->lfs_magic == LFS_MAGIC)
411 1.9.4.2 riz lfs_scan(n);
412 1.9.4.2 riz break;
413 1.9.4.2 riz default:
414 1.9.4.2 riz break;
415 1.9.4.2 riz }
416 1.9.4.2 riz }
417 1.9.4.2 riz }
418 1.9.4.2 riz return EXIT_SUCCESS;
419 1.9.4.2 riz }
420 1.9.4.2 riz
421 1.9.4.2 riz
422 1.9.4.2 riz static void
423 1.9.4.2 riz usage(void)
424 1.9.4.2 riz {
425 1.9.4.2 riz (void)fprintf(stderr,
426 1.9.4.2 riz "Usage: %s [-lv] [-s start] [-e end] device\n", getprogname());
427 1.9.4.2 riz exit(EXIT_FAILURE);
428 1.9.4.2 riz }
429 1.9.4.2 riz
430 1.9.4.2 riz
431 1.9.4.2 riz int
432 1.9.4.2 riz main(int argc, char **argv)
433 1.9.4.2 riz {
434 1.9.4.2 riz int ch, fd;
435 1.9.4.2 riz daddr_t end = -1, beg = 0;
436 1.9.4.2 riz struct disklabel dl;
437 1.9.4.2 riz
438 1.9.4.2 riz setprogname(*argv);
439 1.9.4.2 riz while ((ch = getopt(argc, argv, "e:ls:v")) != -1)
440 1.9.4.2 riz switch(ch) {
441 1.9.4.2 riz case 'e':
442 1.9.4.2 riz eflag = 1;
443 1.9.4.2 riz end = atoi(optarg);
444 1.9.4.2 riz break;
445 1.9.4.2 riz case 'l':
446 1.9.4.2 riz flags |= LABELS;
447 1.9.4.2 riz break;
448 1.9.4.2 riz case 's':
449 1.9.4.2 riz beg = atoi(optarg);
450 1.9.4.2 riz break;
451 1.9.4.2 riz case 'v':
452 1.9.4.2 riz flags |= VERBOSE;
453 1.9.4.2 riz break;
454 1.9.4.2 riz default:
455 1.9.4.2 riz usage();
456 1.9.4.2 riz /* NOTREACHED */
457 1.9.4.2 riz }
458 1.9.4.2 riz
459 1.9.4.2 riz argc -= optind;
460 1.9.4.2 riz argv += optind;
461 1.9.4.2 riz
462 1.9.4.2 riz if (argc != 1)
463 1.9.4.2 riz usage();
464 1.9.4.2 riz
465 1.9.4.2 riz fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
466 1.9.4.2 riz
467 1.9.4.2 riz if (fd == -1)
468 1.9.4.2 riz err(1, "Cannot open `%s'", device);
469 1.9.4.2 riz /* NOTREACHED */
470 1.9.4.2 riz
471 1.9.4.2 riz if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
472 1.9.4.2 riz warn("Couldn't retrieve disklabel");
473 1.9.4.2 riz (void)memset(&dl, 0, sizeof(dl));
474 1.9.4.2 riz dl.d_secperunit = 0x7fffffff;
475 1.9.4.2 riz } else {
476 1.9.4.2 riz (void)printf("Disk: %s\n", dl.d_typename);
477 1.9.4.2 riz (void)printf("Total sectors on disk: %" PRIu32 "\n\n",
478 1.9.4.2 riz dl.d_secperunit);
479 1.9.4.2 riz }
480 1.9.4.2 riz
481 1.9.4.2 riz if (!eflag)
482 1.9.4.2 riz end = dl.d_secperunit; /* default to max sectors */
483 1.9.4.2 riz
484 1.9.4.2 riz return scan_disk(fd, beg, end, flags);
485 1.9.4.2 riz }
486