1 /* $OpenBSD: scan_ffs.c,v 1.14 2007/06/22 19:04:13 otto Exp $ */ 2 3 /* 4 * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/fcntl.h> 31 #include <ufs/ffs/fs.h> 32 #include <unistd.h> 33 #include <stdlib.h> 34 #include <stdio.h> 35 #include <string.h> 36 #include <time.h> 37 #include <err.h> 38 #include <util.h> 39 40 #define SBCOUNT 64 /* XXX - Should be configurable */ 41 42 /* Flags to control ourselves... */ 43 #define FLAG_VERBOSE 1 44 #define FLAG_SMART 2 45 #define FLAG_LABELS 4 46 47 static void usage(void); 48 49 static int 50 ufsscan(int fd, daddr64_t beg, daddr64_t end, int flags) 51 { 52 static char lastmount[MAXMNTLEN]; 53 static u_int8_t buf[SBSIZE * SBCOUNT]; 54 struct fs *sb; 55 daddr64_t blk, lastblk; 56 int n; 57 58 lastblk = -1; 59 memset(lastmount, 0, MAXMNTLEN); 60 61 for (blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){ 62 memset(buf, 0, SBSIZE * SBCOUNT); 63 if (lseek(fd, (off_t)blk * 512, SEEK_SET) < 0) 64 err(1, "lseek"); 65 if (read(fd, buf, SBSIZE * SBCOUNT) < 0) 66 err(1, "read"); 67 68 for (n = 0; n < (SBSIZE * SBCOUNT); n += 512){ 69 sb = (struct fs*)(&buf[n]); 70 if (sb->fs_magic == FS_MAGIC) { 71 if (flags & FLAG_VERBOSE) 72 printf("block %lld id %x,%x size %d\n", 73 blk + (n/512), sb->fs_id[0], 74 sb->fs_id[1], sb->fs_ffs1_size); 75 76 if (((blk+(n/512)) - lastblk) == (SBSIZE/512)) { 77 if (flags & FLAG_LABELS ) { 78 printf("X: %lld %lld 4.2BSD %d %d %d # %s\n", 79 ((off_t)sb->fs_ffs1_size * 80 sb->fs_fsize / 512), 81 blk+(n/512)-(2*SBSIZE/512), 82 sb->fs_fsize, sb->fs_bsize, 83 sb->fs_cpg, lastmount); 84 } else { 85 printf("ffs at %lld size %lld " 86 "mount %s time %s", 87 blk+(n/512)-(2*SBSIZE/512), 88 (long long)(off_t)sb->fs_ffs1_size * 89 sb->fs_fsize, 90 lastmount, ctime(&sb->fs_ffs1_time)); 91 } 92 93 if (flags & FLAG_SMART) { 94 off_t size = (off_t)sb->fs_ffs1_size * 95 sb->fs_fsize; 96 97 if ((n + size) < (SBSIZE * SBCOUNT)) 98 n += size; 99 else { 100 blk += (size/512 - 101 (SBCOUNT*SBCOUNT)); 102 break; 103 } 104 } 105 } 106 107 /* Update last potential FS SBs seen */ 108 lastblk = blk + (n/512); 109 memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN); 110 } 111 } 112 } 113 return(0); 114 } 115 116 117 static void 118 usage(void) 119 { 120 extern char *__progname; 121 122 fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n", 123 __progname); 124 exit(1); 125 } 126 127 128 int 129 main(int argc, char *argv[]) 130 { 131 int ch, fd, flags = 0; 132 daddr64_t beg = 0, end = -1; 133 const char *errstr; 134 135 while ((ch = getopt(argc, argv, "lsvb:e:")) != -1) 136 switch(ch) { 137 case 'b': 138 beg = strtonum(optarg, 0, LLONG_MAX, &errstr); 139 if (errstr) 140 errx(1, "%s: %s", optarg, errstr); 141 break; 142 case 'e': 143 end = strtonum(optarg, 0, LLONG_MAX, &errstr); 144 if (errstr) 145 errx(1, "%s: %s", optarg, errstr); 146 break; 147 case 'v': 148 flags |= FLAG_VERBOSE; 149 break; 150 case 's': 151 flags |= FLAG_SMART; 152 break; 153 case 'l': 154 flags |= FLAG_LABELS; 155 break; 156 default: 157 usage(); 158 /* NOTREACHED */ 159 } 160 argc -= optind; 161 argv += optind; 162 163 if (argc != 1) 164 usage(); 165 166 fd = opendev(argv[0], O_RDONLY, OPENDEV_PART, NULL); 167 if (fd < 0) 168 err(1, "%s", argv[0]); 169 170 return (ufsscan(fd, beg, end, flags)); 171 } 172