-
Notifications
You must be signed in to change notification settings - Fork 3
/
frelink.c
320 lines (250 loc) · 7.18 KB
/
frelink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* frelink: Recover deleted files that are still open by a process
* or a loop mount.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) Pantelis Koukousoulas 2011
*
* Author: Pantelis Koukousoulas <pktoss@gmail.com>
*
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <linux/namei.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/kprobes.h>
#include <linux/loop.h>
#include <linux/workqueue.h>
#include "frelink.h"
#define MODULE_NAME "frelink"
MODULE_AUTHOR("Pantelis Koukousoulas <pktoss@gmail.com>");
MODULE_DESCRIPTION("frelink: recover deleted open files");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.5");
#define IPRINTK(_f, _a...) printk(KERN_INFO MODULE_NAME ": " _f, ## _a)
/*
* Forward decls
*/
static long jloop_get_status(struct loop_device *lo, struct loop_info64 *info);
static void do_recover(struct work_struct *work);
static int relink_file(struct file *f, char *path);
/*
* Our main data structure.
*/
struct frelink_data {
struct delayed_work work;
struct proc_dir_entry *proc_entry;
struct file *file;
char *path;
atomic_t busy;
atomic_t loidx;
};
static struct frelink_data frelink;
/*
* KProbe structure
*/
static struct jprobe loop_jprobe = {
.entry = jloop_get_status,
.kp = {
.symbol_name = "loop_get_status",
},
};
static void do_recover(struct work_struct *work)
{
struct frelink_data *rd = container_of(work,
struct frelink_data,
work.work);
struct file *file = rd->file;
char *name = rd->path;
int ret = relink_file(file, name);
if (ret == 0)
IPRINTK("File \"%s\" undelete OK!\n", name);
else
IPRINTK("Failed to undelete \"%s\": "
"ret = \"%d\"\n", name, ret);
/* We have finished with recovering, no longer busy */
atomic_set(&rd->busy,0);
}
/*
* This function is the "juice" of the module and shared by
* both code paths (fd and loop). Given a "struct file *"
* and a path, it makes a new hardlink for the deleted
* file's inode so that it doesn't get actually deleted.
*/
static int relink_file(struct file *f, char *path)
{
struct dentry *old_d, *new_d;
struct nameidata nd;
int ret;
old_d = f->f_dentry;
ret = path_lookup(path, LOOKUP_PARENT, &nd);
if (ret)
goto exit;
ret = -EXDEV;
new_d = lookup_create(&nd, 0);
ret = PTR_ERR(new_d);
if (!IS_ERR(new_d)) {
//If we don't do this, vfs_link() won't work
old_d->d_inode->i_nlink = 1;
//This is the essence of what this module does :)
ret = vfs_link(old_d, nd.path.dentry->d_inode, new_d);
//This will remove the "(deleted)" suffix from the old dentry
d_rehash(old_d);
dput(new_d);
}
mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
path_put(&nd.path);
exit:
return ret;
}
/*
* Our jprobe handler. "Steals" the struct file * and path.
*/
static long jloop_get_status(struct loop_device *lo, struct loop_info64 *info)
{
struct file *lofile;
char *lopath;
int nlinks;
int loidx = lo->lo_number;
int myloidx = atomic_read(&frelink.loidx);
/* We only care about a specific loopback device */
if (loidx != myloidx)
goto exit;
IPRINTK("\"Stealing\" backing file info from /dev/loop%d\n",loidx);
lofile = lo->lo_backing_file;
lopath = lo->lo_file_name;
nlinks = lofile->f_dentry->d_inode->i_nlink;
if (nlinks == 0) {
IPRINTK("Found deleted backing file.. Scheduling undelete ...\n");
frelink.file = lofile;
frelink.path = lopath;
schedule_delayed_work(&frelink.work, msecs_to_jiffies(1000));
}
exit:
/* Always end with a call to jprobe_return(). */
jprobe_return();
/* Control never actually reaches this point */
return 0;
}
/*
* IOCTL handler 1: recover from fd.
* Wrapper for recover_from_file()
*/
static int recover_from_fd(struct frelink_arg *p)
{
struct file *f;
int fd = p->id.fd;
char *path = getname(p->path);
int ret = PTR_ERR(path);
if (IS_ERR(path))
return ret;
IPRINTK("Recovering from fd \"%d\"to path \"%s\"", fd, path);
atomic_set(&frelink.busy,1);
f = fget(fd);
if (!f) {
ret = -EBADF;
goto exit;
}
ret = relink_file(f, path);
exit:
fput(f);
atomic_set(&frelink.busy,0);
return ret;
}
/*
* IOCTL handler 2: recover a loop-mounted file
*/
static int recover_from_loop(struct frelink_arg *p)
{
int ret = 0;
int loidx = p->id.loidx;
IPRINTK("Recovering loop-mounted file from /dev/loop%d\n",loidx);
atomic_set(&frelink.busy,1);
atomic_set(&frelink.loidx,loidx);
return 0;
}
/* IOCTL CODE ---------------------------------------------------------*/
static long frelink_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
int busy = atomic_read(&frelink.busy);
struct frelink_arg __user *p = (struct frelink_arg __user *)arg;
int ret = 0;
/* Basic access checks */
if ((_IOC_TYPE(cmd) != FRELINK_IOC_MAGIC) ||
(_IOC_NR(cmd) > FRELINK_IOC_MAXNR) ||
(_IOC_DIR(cmd) & _IOC_READ)) return -ENOTTY;
if (!access_ok(VERIFY_READ, p, _IOC_SIZE(cmd)))
return -EFAULT;
switch (cmd) {
case FRELINK_IOCRECFD:
ret = busy ? -EBUSY : recover_from_fd(p);
break;
case FRELINK_IOCRECLOOP:
ret = busy ? -EBUSY : recover_from_loop(p);
break;
case FRELINK_IOCRECTEST:
ret = p->id.fd == 1 ? 0 : -EFAULT;
break;
default:
ret = -ENOTTY;
break;
}
return ret;
}
static const struct file_operations frelink_ops = {
.owner = THIS_MODULE,
.unlocked_ioctl = frelink_ioctl,
};
/* END IOCTL CODE -----------------------------------------------------*/
static int __init frelink_init(void)
{
struct proc_dir_entry *entry;
int ret = 0;
IPRINTK ("frelink module loaded\n");
entry = create_proc_entry(MODULE_NAME, 0400, NULL);
if (!entry) {
printk (KERN_ALERT "Error creating /proc/"MODULE_NAME
" file\n");
return -EBADF;
}
entry->proc_fops = &frelink_ops;
frelink.proc_entry = entry;
atomic_set(&frelink.busy,0);
atomic_set(&frelink.loidx,-1);
ret = register_jprobe(&loop_jprobe);
if (ret < 0) {
IPRINTK("register_jprobe failed, returned %d\n", ret);
return -1;
}
IPRINTK("Planted jprobe at %p, handler addr %p\n",
loop_jprobe.kp.addr, loop_jprobe.entry);
INIT_DELAYED_WORK(&frelink.work, do_recover);
return ret;
}
module_init(frelink_init);
static void __exit frelink_exit(void)
{
IPRINTK ("unloading module...\n");
remove_proc_entry(MODULE_NAME, NULL);
unregister_jprobe(&loop_jprobe);
IPRINTK("jprobe at \"%p\" unregistered\n", loop_jprobe.kp.addr);
}
module_exit(frelink_exit);