blob: 9a57a41c0629520c71402e748ab03491c686c366 [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05002 * lsdel.c --- routines to try to help a user recover a deleted file.
Theodore Ts'of3db3561997-04-26 13:34:30 +00003 *
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05004 * 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'of3db3561997-04-26 13:34:30 +00007 */
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'o50e1e101997-04-26 13:58:21 +000015#ifdef HAVE_ERRNO_H
16#include <errno.h>
17#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000018#include <sys/types.h>
19#include <sys/stat.h>
20
21#include "debugfs.h"
22
23struct deleted_info {
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000024 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'of3db3561997-04-26 13:34:30 +000031};
32
33struct lsdel_struct {
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000034 ext2_ino_t inode;
Theodore Ts'of3db3561997-04-26 13:34:30 +000035 int num_blocks;
36 int free_blocks;
37 int bad_blocks;
38};
39
Theodore Ts'o50e1e101997-04-26 13:58:21 +000040static int deleted_info_compare(const void *a, const void *b)
Theodore Ts'of3db3561997-04-26 13:34:30 +000041{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042 const struct deleted_info *arg1, *arg2;
Theodore Ts'of3db3561997-04-26 13:34:30 +000043
Theodore Ts'o50e1e101997-04-26 13:58:21 +000044 arg1 = (const struct deleted_info *) a;
45 arg2 = (const struct deleted_info *) b;
Theodore Ts'of3db3561997-04-26 13:34:30 +000046
47 return arg1->dtime - arg2->dtime;
48}
49
Theodore Ts'o50e1e101997-04-26 13:58:21 +000050static int lsdel_proc(ext2_filsys fs,
51 blk_t *block_nr,
52 int blockcnt,
53 void *private)
Theodore Ts'of3db3561997-04-26 13:34:30 +000054{
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
71void 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'ob044c2e2001-01-11 15:26:39 +000077 ext2_ino_t ino;
Theodore Ts'of3db3561997-04-26 13:34:30 +000078 struct ext2_inode inode;
79 errcode_t retval;
80 char *block_buf;
81 int i;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -050082 long secs = 0;
83 char *tmp;
84 time_t now = time(0);
Theodore Ts'o7380ac92002-03-05 01:57:53 -050085 FILE *out;
Theodore Ts'of3db3561997-04-26 13:34:30 +000086
Theodore Ts'oe1018ee2002-01-03 04:55:25 -050087 if (common_args_process(argc, argv, 1, 2, "ls_deleted_inodes",
88 "[secs]", 0))
89 return;
Theodore Ts'of3db3561997-04-26 13:34:30 +000090 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -050091 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'of3db3561997-04-26 13:34:30 +000096 }
Theodore Ts'of3db3561997-04-26 13:34:30 +000097
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'ofc6d9d51997-04-29 14:51:31 +0000107 block_buf = malloc(current_fs->blocksize * 3);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000108 if (!block_buf) {
109 com_err("ls_deleted_inodes", ENOMEM, "while allocating block buffer");
110 goto error_out;
111 }
112
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000113 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000114 if (retval) {
115 com_err("ls_deleted_inodes", retval,
116 "while opening inode scan");
117 goto error_out;
118 }
119
Theodore Ts'o3e025ef2000-06-10 15:20:41 +0000120 do {
121 retval = ext2fs_get_next_inode(scan, &ino, &inode);
122 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000123 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'oe1018ee2002-01-03 04:55:25 -0500130 if ((inode.i_dtime == 0) ||
131 (secs && ((now - secs) > inode.i_dtime)))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000132 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'ofc6d9d51997-04-29 14:51:31 +0000139 retval = ext2fs_block_iterate(current_fs, ino, 0, block_buf,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000140 lsdel_proc, &lsd);
141 if (retval) {
142 com_err("ls_deleted_inodes", retval,
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700143 "while calling ext2fs_block_iterate");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000144 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'o36a43d61998-03-24 16:17:51 +0000163 if (!LINUX_S_ISDIR(inode.i_mode))
164 delarray[num_delarray].size |=
165 ((__u64) inode.i_size_high << 32);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000166 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'o3e025ef2000-06-10 15:20:41 +0000173 do {
Theodore Ts'oee753092000-06-09 04:24:36 +0000174 retval = ext2fs_get_next_inode(scan, &ino, &inode);
Theodore Ts'o3e025ef2000-06-10 15:20:41 +0000175 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000176 if (retval) {
177 com_err("ls_deleted_inodes", retval,
178 "while doing inode scan");
179 goto error_out;
180 }
181 }
182
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500183 out = open_pager();
184
185 fprintf(out, " Inode Owner Mode Size Blocks Time deleted\n");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000186
187 qsort(delarray, num_delarray, sizeof(struct deleted_info),
188 deleted_info_compare);
189
190 for (i = 0; i < num_delarray; i++) {
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500191 fprintf(out, "%6u %6d %6o %6llu %4d/%4d %s", delarray[i].ino,
Theodore Ts'of3db3561997-04-26 13:34:30 +0000192 delarray[i].uid, delarray[i].mode, delarray[i].size,
193 delarray[i].free_blocks, delarray[i].num_blocks,
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000194 time_to_string(delarray[i].dtime));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000195 }
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500196 fprintf(out, "%d deleted inodes found.\n", num_delarray);
197 close_pager(out);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000198
199error_out:
200 free(block_buf);
201 free(delarray);
202 if (scan)
203 ext2fs_close_inode_scan(scan);
204 return;
205}
206
207
208