Home | History | Annotate | Line # | Download | only in scan_ffs
scan_ffs.c revision 1.6
      1 /* $NetBSD: scan_ffs.c,v 1.6 2005/07/31 20:19:40 christos Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2005 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.6 2005/07/31 20:19:40 christos 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 #include <ufs/ffs/fs.h>
     95 
     96 #include <unistd.h>
     97 #include <stdlib.h>
     98 #include <stdio.h>
     99 #include <string.h>
    100 #include <err.h>
    101 #include <util.h>
    102 
    103 /* common struct for FFS/LFS */
    104 struct sblockinfo {
    105 	struct lfs	*lfs;
    106 	struct fs	*ffs;
    107 	u_int64_t	lfs_off;
    108 	u_int64_t	ffs_off;
    109 	char		lfs_path[MAXMNTLEN];
    110 	char		ffs_path[MAXMNTLEN];
    111 } sbinfo;
    112 
    113 static daddr_t	blk, lastblk;
    114 
    115 static int	eflag = 0;
    116 static int	flags = 0;
    117 static int 	sbaddr = 0; /* counter for the LFS superblocks */
    118 
    119 static char	device[MAXPATHLEN];
    120 static const char *fstypes[] = { "NONE", "FFSv1", "FFSv2", "LFS" };
    121 
    122 #define FSTYPE_NONE	0
    123 #define FSTYPE_FFSV1	1
    124 #define FSTYPE_FFSV2	2
    125 
    126 #define SBCOUNT		64 /* may be changed */
    127 #define SBPASS		(SBCOUNT * SBLOCKSIZE / 512)
    128 
    129 /* This is only useful for LFS */
    130 
    131 /* first sblock address contains the correct offset */
    132 #define FIRST_SBLOCK_ADDRESS    1
    133 /* second and third sblock address contain lfs_fsmnt[MAXMNTLEN] */
    134 #define SECOND_SBLOCK_ADDRESS   2
    135 /* last sblock address in a LFS partition */
    136 #define MAX_SBLOCK_ADDRESS      10
    137 
    138 enum { NADA, VERBOSE, LABELS };
    139 
    140 /* FFS functions */
    141 static void	ffs_printpart(int, size_t, int);
    142 static void	ffs_scan(int);
    143 static int	ffs_checkver(void);
    144 /* LFS functions */
    145 static void	lfs_printpart(int, int, struct sblockinfo *);
    146 static void	lfs_scan(int);
    147 /* common functions */
    148 static void	usage(void) __attribute__((__noreturn__));
    149 static int	scan_disk(int, daddr_t, daddr_t, int);
    150 
    151 static int
    152 ffs_checkver(void)
    153 {
    154 	switch (sbinfo.ffs->fs_magic) {
    155 		case FS_UFS1_MAGIC:
    156 		case FS_UFS1_MAGIC_SWAPPED:
    157 			sbinfo.ffs->fs_size = sbinfo.ffs->fs_old_size;
    158 			return FSTYPE_FFSV1;
    159 		case FS_UFS2_MAGIC:
    160 		case FS_UFS2_MAGIC_SWAPPED:
    161 			return FSTYPE_FFSV2;
    162 		default:
    163 			return FSTYPE_NONE;
    164 	}
    165 }
    166 
    167 static void
    168 ffs_printpart(int flag, size_t ffsize, int n)
    169 {
    170 
    171 	int fstype = ffs_checkver();
    172 
    173 	switch (flag) {
    174 	case VERBOSE:
    175 		(void)printf("block: %" PRIu64 " id %x,%x size %" PRIu64 "\n",
    176 			blk + (n / 512),
    177 			sbinfo.ffs->fs_id[0],
    178 			sbinfo.ffs->fs_id[1], sbinfo.ffs->fs_size);
    179 		break;
    180 	case LABELS:
    181 		(void)printf("X:  %9" PRIu64,
    182 			(uint64_t)((off_t)sbinfo.ffs->fs_size *
    183 			sbinfo.ffs->fs_fsize / 512));
    184 		switch (fstype) {
    185 		case FSTYPE_FFSV1:
    186 			(void)printf(" %9" PRIu64,
    187 			    blk + (n / 512) - (2 * SBLOCKSIZE / 512));
    188 			break;
    189 		case FSTYPE_FFSV2:
    190 			(void)printf(" %9" PRIu64,
    191 			    blk + (n / 512) -
    192 			    (ffsize * SBLOCKSIZE / 512 + 128));
    193 			break;
    194 		default:
    195 			break;
    196 		}
    197 		(void)printf(" 4.2BSD %6d %5d %8d # %s [%s]\n",
    198 			sbinfo.ffs->fs_fsize, sbinfo.ffs->fs_bsize,
    199 			sbinfo.ffs->fs_old_cpg,
    200 			sbinfo.ffs_path, fstypes[fstype]);
    201 		break;
    202 	default:
    203 		(void)printf("%s ", fstypes[fstype]);
    204 		switch (fstype) {
    205 		case FSTYPE_FFSV1:
    206 			(void)printf("at %" PRIu64,
    207 			    blk + (n / 512) - (2 * SBLOCKSIZE / 512));
    208 			break;
    209 		case FSTYPE_FFSV2:
    210 			(void)printf("at %" PRIu64,
    211 			    blk + (n / 512) -
    212 			    (ffsize * SBLOCKSIZE / 512 + 128));
    213 			break;
    214 		default:
    215 			break;
    216 		}
    217 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    218 			(uint64_t)((off_t)sbinfo.ffs->fs_size *
    219 			sbinfo.ffs->fs_fsize / 512), sbinfo.ffs_path);
    220 		break;
    221 	}
    222 }
    223 
    224 static void
    225 ffs_scan(int n)
    226 {
    227 	int fstype = ffs_checkver();
    228 	size_t i;
    229 
    230 	/*
    231 	 * XXX:
    232 	 * It cannot find FFSv1 partitions with fsize/bsize > 2048/16384,
    233 	 * same problem found in the original program that comes from
    234 	 * OpenBSD (scan_ffs(8)).
    235 	 */
    236 	if (flags & VERBOSE)
    237 		ffs_printpart(VERBOSE, NADA, n);
    238 	switch (fstype) {
    239 	case FSTYPE_FFSV1:
    240 		if (((blk + (n / 512)) - lastblk) == (SBLOCKSIZE / 512)) {
    241 			if (flags & LABELS)
    242 				ffs_printpart(LABELS, NADA, n);
    243 			else
    244 				ffs_printpart(NADA, NADA, n);
    245 		}
    246 		break;
    247 	case FSTYPE_FFSV2:
    248 		/*
    249 		 * That checks for FFSv2 partitions with fragsize/blocksize:
    250 		 * 512/4096, 1024/8192, 2048/16384, 4096/32768 and 8192/65536.
    251 		 * Really enough for now.
    252 		 */
    253 		for (i = 1; i < 16; i <<= 1)
    254 			if (((blk + (n / 512)) - lastblk) ==
    255 			    (i * SBLOCKSIZE / 512)) {
    256 				if (flags & LABELS)
    257 					ffs_printpart(LABELS, i, n);
    258 				else
    259 					ffs_printpart(NADA, i, n);
    260 			}
    261 		break;
    262 	}
    263 }
    264 
    265 static void
    266 lfs_printpart(int flag, int n, struct sblockinfo *sbi)
    267 {
    268 	if (flags & VERBOSE)
    269                	(void)printf("block: %" PRIu64 " size %" PRIu32 "\n",
    270                 	blk + (n / 512), sbi->lfs->lfs_size);
    271 	switch (flag) {
    272 	case LABELS:
    273 		(void)printf("X:  %9" PRIu64,
    274                		(uint64_t)((off_t)sbi->lfs->lfs_size *
    275                		sbi->lfs->lfs_fsize / 512));
    276 		(void)printf(" %9" PRIu64, sbi->lfs_off);
    277 		(void)printf(" 4.4LFS %6d %5d %8d # %s [LFSv%d]\n",
    278 			sbi->lfs->lfs_fsize, sbi->lfs->lfs_bsize,
    279 			sbi->lfs->lfs_nseg, sbi->lfs_path,
    280 			sbi->lfs->lfs_version);
    281 		break;
    282 	default:
    283 		(void)printf("LFSv%d ", sbinfo.lfs->lfs_version);
    284 		(void)printf("at %" PRIu64, sbinfo.lfs_off);
    285 		(void)printf(" size %" PRIu64 ", last mounted on %s\n",
    286 			(uint64_t)((off_t)sbinfo.lfs->lfs_size *
    287 			sbinfo.lfs->lfs_fsize / 512), sbinfo.lfs_path);
    288 		break;
    289 	}
    290 }
    291 
    292 static void
    293 lfs_scan(int n)
    294 {
    295 	/* backup offset */
    296 	lastblk = blk + (n / 512) - (LFS_SBPAD / 512);
    297 	/* increment counter */
    298         ++sbaddr;
    299 
    300 	switch (sbaddr) {
    301 	/*
    302 	 * first superblock contains the right offset, but lfs_fsmnt is
    303 	 * empty... afortunately the next superblock address has it.
    304 	 */
    305 	case FIRST_SBLOCK_ADDRESS:
    306 		/* copy partition offset */
    307 		if (sbinfo.lfs_off != lastblk)
    308 			sbinfo.lfs_off = blk + (n / 512) - (LFS_SBPAD / 512);
    309 		break;
    310 	case SECOND_SBLOCK_ADDRESS:
    311 		/* copy the path of last mount */
    312 		(void)memcpy(sbinfo.lfs_path,
    313 			sbinfo.lfs->lfs_fsmnt, MAXMNTLEN);
    314 		/* print now that we have the info */
    315 		if (flags & LABELS)
    316 			lfs_printpart(LABELS, n, &sbinfo);
    317 		else
    318 			lfs_printpart(NADA, n, &sbinfo);
    319 		/* clear our struct */
    320 		(void)memset(&sbinfo, 0, sizeof(sbinfo));
    321 		break;
    322 	case MAX_SBLOCK_ADDRESS:
    323 		/*
    324 		 * reset the counter, this is the last superblock address,
    325 		 * the next one will be another partition maybe.
    326 		 */
    327 		sbaddr = 0;
    328 		break;
    329 	default:
    330 		break;
    331 	}
    332 }
    333 
    334 static int
    335 scan_disk(int fd, daddr_t beg, daddr_t end, int fflags)
    336 {
    337 	u_int8_t buf[SBLOCKSIZE * SBCOUNT];
    338 	int n, fstype;
    339 
    340 	n = fstype = 0;
    341 	lastblk = -1;
    342 
    343 	/* clear our struct before using it */
    344 	(void)memset(&sbinfo, 0, sizeof(sbinfo));
    345 
    346 	if (fflags & LABELS)
    347 		(void)printf(
    348 		    "#        size    offset fstype [fsize bsize cpg/sgs]\n");
    349 
    350 	for (blk = beg; blk <= ((end < 0) ? blk: end); blk += SBPASS) {
    351 		(void)memset(buf, 0, sizeof(buf));
    352 
    353 		if (lseek(fd, (off_t)blk * 512, SEEK_SET) == (off_t)-1)
    354 			err(1, "lseek");
    355 
    356 		if (read(fd, buf, sizeof(buf)) == -1)
    357 			err(1, "read");
    358 
    359 		for (n = 0; n < (SBLOCKSIZE * SBCOUNT); n += 512) {
    360 			sbinfo.ffs = (struct fs *)(void *)&buf[n];
    361 			sbinfo.lfs = (struct lfs *)(void *)&buf[n];
    362 			fstype = ffs_checkver();
    363 			switch (fstype) {
    364 			case FSTYPE_FFSV1:
    365 			case FSTYPE_FFSV2:
    366 				ffs_scan(n);
    367 				lastblk = blk + (n / 512);
    368 				(void)memcpy(sbinfo.ffs_path,
    369 					sbinfo.ffs->fs_fsmnt, MAXMNTLEN);
    370 				break;
    371 			case FSTYPE_NONE:
    372 				/* maybe LFS? */
    373 				if (sbinfo.lfs->lfs_magic != LFS_MAGIC)
    374 					break;
    375 				if (sbinfo.lfs->lfs_magic == LFS_MAGIC)
    376 					lfs_scan(n);
    377 				break;
    378 			default:
    379 				break;
    380 			}
    381 		}
    382 	}
    383 	return EXIT_SUCCESS;
    384 }
    385 
    386 
    387 static void
    388 usage(void)
    389 {
    390 	(void)fprintf(stderr,
    391 		"Usage: %s [-lv] [-s start] [-e end] device\n", getprogname());
    392 	exit(EXIT_FAILURE);
    393 }
    394 
    395 
    396 int
    397 main(int argc, char **argv)
    398 {
    399 	int ch, fd;
    400 	daddr_t end = -1, beg = 0;
    401 	struct disklabel dl;
    402 
    403 	setprogname(*argv);
    404 	while ((ch = getopt(argc, argv, "e:ls:v")) != -1)
    405 		switch(ch) {
    406 		case 'e':
    407 			eflag = 1;
    408 			end = atoi(optarg);
    409 			break;
    410 		case 'l':
    411 			flags |= LABELS;
    412 			break;
    413 		case 's':
    414 			beg = atoi(optarg);
    415 			break;
    416 		case 'v':
    417 			flags |= VERBOSE;
    418 			break;
    419 		default:
    420 			usage();
    421 			/* NOTREACHED */
    422 		}
    423 
    424 	argc -= optind;
    425 	argv += optind;
    426 
    427 	if (argc != 1)
    428 		usage();
    429 
    430 	fd = opendisk(argv[0], O_RDONLY, device, sizeof(device), 0);
    431 
    432 	if (fd == -1)
    433 		err(1, "Cannot open `%s'", device);
    434 		/* NOTREACHED */
    435 
    436 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    437 		warn("Couldn't retrieve disklabel");
    438 		(void)memset(&dl, 0, sizeof(dl));
    439 		dl.d_secperunit = 0x7fffffff;
    440 	} else {
    441 		(void)printf("Disk: %s\n", dl.d_typename);
    442 		(void)printf("Total sectors on disk: %" PRIu32 "\n\n",
    443 		    dl.d_secperunit);
    444 	}
    445 
    446 	if (!eflag)
    447 		end = dl.d_secperunit; /* default to max sectors */
    448 
    449 	return scan_disk(fd, beg, end, flags);
    450 }
    451