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