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