Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1 | /* |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 2 | * lsdel.c --- routines to try to help a user recover a deleted file. |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 3 | * |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 4 | * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 |
| 5 | * Theodore Ts'o. This file may be redistributed under the terms of |
| 6 | * the GNU Public License. |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <ctype.h> |
| 13 | #include <string.h> |
| 14 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 15 | #ifdef HAVE_ERRNO_H |
| 16 | #include <errno.h> |
| 17 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | |
| 21 | #include "debugfs.h" |
| 22 | |
| 23 | struct deleted_info { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 24 | ext2_ino_t ino; |
| 25 | unsigned short mode; |
| 26 | unsigned short uid; |
| 27 | __u64 size; |
| 28 | time_t dtime; |
| 29 | int num_blocks; |
| 30 | int free_blocks; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct lsdel_struct { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 34 | ext2_ino_t inode; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 35 | int num_blocks; |
| 36 | int free_blocks; |
| 37 | int bad_blocks; |
| 38 | }; |
| 39 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 40 | static int deleted_info_compare(const void *a, const void *b) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 41 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 42 | const struct deleted_info *arg1, *arg2; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 43 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 44 | arg1 = (const struct deleted_info *) a; |
| 45 | arg2 = (const struct deleted_info *) b; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 46 | |
| 47 | return arg1->dtime - arg2->dtime; |
| 48 | } |
| 49 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 50 | static int lsdel_proc(ext2_filsys fs, |
| 51 | blk_t *block_nr, |
| 52 | int blockcnt, |
| 53 | void *private) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 54 | { |
| 55 | struct lsdel_struct *lsd = (struct lsdel_struct *) private; |
| 56 | |
| 57 | lsd->num_blocks++; |
| 58 | |
| 59 | if (*block_nr < fs->super->s_first_data_block || |
| 60 | *block_nr >= fs->super->s_blocks_count) { |
| 61 | lsd->bad_blocks++; |
| 62 | return BLOCK_ABORT; |
| 63 | } |
| 64 | |
| 65 | if (!ext2fs_test_block_bitmap(fs->block_map,*block_nr)) |
| 66 | lsd->free_blocks++; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void do_lsdel(int argc, char **argv) |
| 72 | { |
| 73 | struct lsdel_struct lsd; |
| 74 | struct deleted_info *delarray; |
| 75 | int num_delarray, max_delarray; |
| 76 | ext2_inode_scan scan = 0; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 77 | ext2_ino_t ino; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 78 | struct ext2_inode inode; |
| 79 | errcode_t retval; |
| 80 | char *block_buf; |
| 81 | int i; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 82 | long secs = 0; |
| 83 | char *tmp; |
| 84 | time_t now = time(0); |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 85 | FILE *out; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 86 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 87 | if (common_args_process(argc, argv, 1, 2, "ls_deleted_inodes", |
| 88 | "[secs]", 0)) |
| 89 | return; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 90 | if (argc > 1) { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 91 | secs = strtol(argv[1],&tmp,0); |
| 92 | if (*tmp) { |
| 93 | com_err(argv[0], 0, "Bad time - %s",argv[1]); |
| 94 | return; |
| 95 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 96 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 97 | |
| 98 | max_delarray = 100; |
| 99 | num_delarray = 0; |
| 100 | delarray = malloc(max_delarray * sizeof(struct deleted_info)); |
| 101 | if (!delarray) { |
| 102 | com_err("ls_deleted_inodes", ENOMEM, |
| 103 | "while allocating deleted information storage"); |
| 104 | exit(1); |
| 105 | } |
| 106 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 107 | block_buf = malloc(current_fs->blocksize * 3); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 108 | if (!block_buf) { |
| 109 | com_err("ls_deleted_inodes", ENOMEM, "while allocating block buffer"); |
| 110 | goto error_out; |
| 111 | } |
| 112 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 113 | retval = ext2fs_open_inode_scan(current_fs, 0, &scan); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 114 | if (retval) { |
| 115 | com_err("ls_deleted_inodes", retval, |
| 116 | "while opening inode scan"); |
| 117 | goto error_out; |
| 118 | } |
| 119 | |
Theodore Ts'o | 3e025ef | 2000-06-10 15:20:41 +0000 | [diff] [blame] | 120 | do { |
| 121 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 122 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 123 | if (retval) { |
| 124 | com_err("ls_deleted_inodes", retval, |
| 125 | "while starting inode scan"); |
| 126 | goto error_out; |
| 127 | } |
| 128 | |
| 129 | while (ino) { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 130 | if ((inode.i_dtime == 0) || |
| 131 | (secs && ((now - secs) > inode.i_dtime))) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 132 | goto next; |
| 133 | |
| 134 | lsd.inode = ino; |
| 135 | lsd.num_blocks = 0; |
| 136 | lsd.free_blocks = 0; |
| 137 | lsd.bad_blocks = 0; |
| 138 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 139 | retval = ext2fs_block_iterate(current_fs, ino, 0, block_buf, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 140 | lsdel_proc, &lsd); |
| 141 | if (retval) { |
| 142 | com_err("ls_deleted_inodes", retval, |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 143 | "while calling ext2fs_block_iterate"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 144 | goto next; |
| 145 | } |
| 146 | if (lsd.free_blocks && !lsd.bad_blocks) { |
| 147 | if (num_delarray >= max_delarray) { |
| 148 | max_delarray += 50; |
| 149 | delarray = realloc(delarray, |
| 150 | max_delarray * sizeof(struct deleted_info)); |
| 151 | if (!delarray) { |
| 152 | com_err("ls_deleted_inodes", |
| 153 | ENOMEM, |
| 154 | "while reallocating array"); |
| 155 | exit(1); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | delarray[num_delarray].ino = ino; |
| 160 | delarray[num_delarray].mode = inode.i_mode; |
| 161 | delarray[num_delarray].uid = inode.i_uid; |
| 162 | delarray[num_delarray].size = inode.i_size; |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 163 | if (!LINUX_S_ISDIR(inode.i_mode)) |
| 164 | delarray[num_delarray].size |= |
| 165 | ((__u64) inode.i_size_high << 32); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 166 | delarray[num_delarray].dtime = inode.i_dtime; |
| 167 | delarray[num_delarray].num_blocks = lsd.num_blocks; |
| 168 | delarray[num_delarray].free_blocks = lsd.free_blocks; |
| 169 | num_delarray++; |
| 170 | } |
| 171 | |
| 172 | next: |
Theodore Ts'o | 3e025ef | 2000-06-10 15:20:41 +0000 | [diff] [blame] | 173 | do { |
Theodore Ts'o | ee75309 | 2000-06-09 04:24:36 +0000 | [diff] [blame] | 174 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
Theodore Ts'o | 3e025ef | 2000-06-10 15:20:41 +0000 | [diff] [blame] | 175 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 176 | if (retval) { |
| 177 | com_err("ls_deleted_inodes", retval, |
| 178 | "while doing inode scan"); |
| 179 | goto error_out; |
| 180 | } |
| 181 | } |
| 182 | |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 183 | out = open_pager(); |
| 184 | |
| 185 | fprintf(out, " Inode Owner Mode Size Blocks Time deleted\n"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 186 | |
| 187 | qsort(delarray, num_delarray, sizeof(struct deleted_info), |
| 188 | deleted_info_compare); |
| 189 | |
| 190 | for (i = 0; i < num_delarray; i++) { |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 191 | fprintf(out, "%6u %6d %6o %6llu %4d/%4d %s", delarray[i].ino, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 192 | delarray[i].uid, delarray[i].mode, delarray[i].size, |
| 193 | delarray[i].free_blocks, delarray[i].num_blocks, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 194 | time_to_string(delarray[i].dtime)); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 195 | } |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 196 | fprintf(out, "%d deleted inodes found.\n", num_delarray); |
| 197 | close_pager(out); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 198 | |
| 199 | error_out: |
| 200 | free(block_buf); |
| 201 | free(delarray); |
| 202 | if (scan) |
| 203 | ext2fs_close_inode_scan(scan); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | |