Annotation of embedaddon/scan_ffs/scan_ffs.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (c) 1998 Niklas Hallqvist, Tobias Weingartner
                      3:  * Copyright (c) 2002, 2003 Robert Watson, Michael Ranner
                      4:  * Copyright (c) 2004 Michael Ranner
                      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/ufs/dinode.h>
                     32: #include <ufs/ffs/fs.h>
                     33: #include <unistd.h>
                     34: #include <stdlib.h>
                     35: #include <stdio.h>
                     36: #include <string.h>
                     37: #include <time.h>
                     38: #include <err.h>
                     39: #if 0
                     40: #include <util.h>
                     41: #endif
                     42: 
                     43: #if __FreeBSD_version < 500037
                     44: #include "scan_ffs_freebsd5.h"
                     45: #endif
                     46: 
                     47: #if __FreeBSD_version >= 500037
                     48: #define SBSIZE SBLOCKSIZE
                     49: #endif
                     50: #define SBCOUNT 64             /* XXX - Should be configurable */
                     51: 
                     52: /* Flags to control ourselves... */
                     53: #define FLAG_VERBOSE           1
                     54: #define FLAG_SMART             2
                     55: #define FLAG_LABELS            4
                     56: 
                     57: static void usage(void);
                     58: 
                     59: static int
                     60: ufsscan(int fd, daddr_t beg, daddr_t end, int flags)
                     61: {
                     62:        static char lastmount[MAXMNTLEN];
                     63:        static u_int8_t buf[SBSIZE * SBCOUNT];
                     64:        struct fs *sb;
                     65:        daddr_t blk, lastblk;
                     66:        int n;
                     67:        int ufs;
                     68: 
                     69:        lastblk = -1;
                     70:        memset(lastmount, 0, MAXMNTLEN);
                     71: 
                     72:        for (blk = beg; blk <= ((end<0)?blk:end); blk += (SBCOUNT*SBSIZE/512)){
                     73:                memset(buf, 0, SBSIZE * SBCOUNT);
                     74:                if (lseek(fd, (off_t)blk * 512, SEEK_SET) < 0)
                     75:                    err(1, "lseek");
                     76:                if (read(fd, buf, SBSIZE * SBCOUNT) < 0)
                     77:                        err(1, "read");
                     78: 
                     79:                for (n = 0; n < (SBSIZE * SBCOUNT); n += 512){
                     80:                        ufs = 0;
                     81:                        sb = (struct fs*)(&buf[n]);
                     82: 
                     83:                        if (sb->fs_magic == FS_UFS1_MAGIC)
                     84:                                ufs = 1;
                     85: #if __FreeBSD_version >= 500037
                     86:                        if (sb->fs_magic == FS_UFS2_MAGIC)
                     87:                                ufs = 2;
                     88: #endif
                     89: 
                     90:                        if (ufs > 0) {
                     91: #if __FreeBSD_version >= 500037
                     92:                                if (ufs == 1) sb->fs_size = sb->fs_old_size;
                     93: #endif
                     94:                                if (flags & FLAG_VERBOSE)
                     95: #if __FreeBSD_version >= 500037
                     96:                                        printf("block %lld id %x,%x size %lld\n",
                     97: #else
                     98:                                        printf("block %d id %x,%x size %d\n",
                     99: #endif
                    100:                                            blk + (n/512), sb->fs_id[0],
                    101:                                            sb->fs_id[1], sb->fs_size);
                    102: 
                    103:                                if (((blk+(n/512)) - lastblk) == (SBSIZE*ufs/512)) {
                    104:                                        if (flags & FLAG_LABELS ) {
                    105: #if __FreeBSD_version >= 500037
                    106:                                                printf("X: %lld %lld 4.2BSD %ld %ld %ld # %s\n",
                    107: #else
                    108:                                                printf("X: %d %d 4.2BSD %d %d %d # %s\n",
                    109: #endif
                    110:                                                    (daddr_t)((off_t)sb->fs_size *
                    111:                                                        sb->fs_fsize / 512),
                    112:                                                    blk+(n/(512))-(2*SBSIZE/512)-((ufs-1)*SBLOCK_UFS2/512),
                    113:                                                    sb->fs_fsize, sb->fs_bsize,
                    114: #if __FreeBSD_version < 500037
                    115:                                                    sb->fs_cpg, lastmount);
                    116: #else
                    117:                                                    sb->fs_old_cpg, lastmount);
                    118: #endif
                    119:                                        } else {
                    120: #if __FreeBSD_version >= 500037
                    121:                                                printf("ufs%d at %lld size %lld mount %s time %s\n",
                    122: #else
                    123:                                                printf("ufs%d at %d size %d mount %s time %s\n",
                    124: #endif
                    125:                                                        ufs,
                    126:                                                    blk+(n/(512))-(2*SBSIZE/512)-((ufs-1)*SBLOCK_UFS2/512),
                    127:                                                    sb->fs_size,
                    128:                                                    lastmount, ctime((time_t *)&sb->fs_time));
                    129:                                        }
                    130: 
                    131:                                        if (flags & FLAG_SMART) {
                    132:                                                off_t size = (off_t)sb->fs_size *       
                    133:                                                        sb->fs_fsize;
                    134: 
                    135:                                                if ((n + size) < (SBSIZE * SBCOUNT)) 
                    136:                                                        n += size;
                    137:                                                else {
                    138:                                                        blk += (size/512) - (SBCOUNT * SBCOUNT);
                    139:                                                        break;
                    140:                                                }
                    141:                                        }
                    142:                                }
                    143: 
                    144:                                /* Update last potential FS SBs seen */
                    145:                                lastblk = blk + (n/512);
                    146:                                memcpy(lastmount, sb->fs_fsmnt, MAXMNTLEN);
                    147:                        }
                    148:                }
                    149:        }
                    150:        return(0);
                    151: }
                    152: 
                    153: 
                    154: static void
                    155: usage(void)
                    156: {
                    157:        extern char *__progname;
                    158: 
                    159:        fprintf(stderr, "usage: %s [-lsv] [-b begin] [-e end] device\n",
                    160:            __progname);
                    161:        exit(1);
                    162: }
                    163: 
                    164: 
                    165: int
                    166: main(int argc, char *argv[])
                    167: {
                    168:        char *name;
                    169:        int ch, fd, flags = 0;
                    170:        daddr_t beg = 0, end = -1;
                    171: 
                    172:        while ((ch = getopt(argc, argv, "lsvb:e:")) != -1)
                    173:                switch(ch) {
                    174:                case 'b':
                    175:                        beg = atoi(optarg);
                    176:                        break;
                    177:                case 'e':
                    178:                        end = atoi(optarg);
                    179:                        break;
                    180:                case 'v':
                    181:                        flags |= FLAG_VERBOSE;
                    182:                        break;
                    183:                case 's':
                    184:                        flags |= FLAG_SMART;
                    185:                        break;
                    186:                case 'l':
                    187:                        flags |= FLAG_LABELS;
                    188:                        break;
                    189:                default:
                    190:                        usage();
                    191:                        /* NOTREACHED */
                    192:        }
                    193:        argc -= optind;
                    194:        argv += optind;
                    195: 
                    196:        if (argc != 1)
                    197:                usage();
                    198: 
                    199:        if (argv[0][0] != '/') {
                    200:                if (asprintf(&name, "/dev/%s", argv[0]) == -1)
                    201:                        err(1, "%s", argv[1]);
                    202:        } else
                    203:                name = argv[0];
                    204:        fd = open(name, O_RDONLY);
                    205:        if (fd < 0)
                    206:                err(1, "%s", argv[0]);
                    207:        if (name != argv[0])
                    208:                free(name);
                    209: 
                    210:        return (ufsscan(fd, beg, end, flags));
                    211: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>