Debian Codegraph @

glassfs/redir2.c

raw
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include <linux/namei.h>
4 #include <linux/mount.h>
5 #include <linux/string.h>
6 #include <linux/init.h>
7 #include <linux/proc_fs.h>
8 #include <asm/uaccess.h>
9
10 #define REDIR2_VERSION "0.0"
11
12 #define AVFS_MAGIC_CHAR '#'
13 #define OVERLAY_DIR "/overlay"
14 #define OVERLAY_DIR_LEN 8
15
16 #define FUSE_MAGIC 0x65735546
17
18 static struct dentry *(*orig_lookup)(struct inode *, struct dentry *,
19 struct nameidata *);
20
21 static struct dentry *(**orig_lookup_ptr)(struct inode *, struct dentry *,
22 struct nameidata *);
23
24
25 static struct vfsmount *orig_mount;
26 static struct semaphore redir2_sem;
27 static int mount_pid = -1;
28 static int lookup_pid = -1;
29 static LIST_HEAD(redir2_mounts);
30 static struct proc_dir_entry *redir2_proc_dir;
31
32
33
34 struct redir2_mount {
35 struct list_head list;
36 struct vfsmount *mnt;
37 };
38
39 static struct super_operations redir2_dummy_super_operations;
40 static struct super_block redir2_dummy_sb = {
41 .s_op = &redir2_dummy_super_operations,
42 };
43
44
45 static int is_avfs(const unsigned char *name, unsigned int len)
46 {
47 for (; len--; name++)
48 if (*name == AVFS_MAGIC_CHAR)
49 return 1;
50 return 0;
51 }
52
53 static char * my_d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
54 struct dentry *root, struct vfsmount *rootmnt,
55 char *buffer, int buflen)
56 {
57 char * end = buffer+buflen;
58 char * retval;
59 int namelen;
60
61 *--end = '\0';
62 buflen--;
63 if (!IS_ROOT(dentry) && d_unhashed(dentry))
64 return ERR_PTR(-ENOENT);
65
66 if (buflen < 1)
67 goto Elong;
68 /* Get '/' right */
69 retval = end-1;
70 *retval = '/';
71
72 for (;;) {
73 struct dentry * parent;
74
75 if (dentry == root && vfsmnt == rootmnt)
76 break;
77 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
78 /* Global root? */
79 spin_lock(&vfsmount_lock);
80 if (vfsmnt->mnt_parent == vfsmnt) {
81 spin_unlock(&vfsmount_lock);
82 goto global_root;
83 }
84 dentry = vfsmnt->mnt_mountpoint;
85 vfsmnt = vfsmnt->mnt_parent;
86 spin_unlock(&vfsmount_lock);
87 continue;
88 }
89 parent = dentry->d_parent;
90 prefetch(parent);
91 namelen = dentry->d_name.len;
92 buflen -= namelen + 1;
93 if (buflen < 0)
94 goto Elong;
95 end -= namelen;
96 memcpy(end, dentry->d_name.name, namelen);
97 *--end = '/';
98 retval = end;
99 dentry = parent;
100 }
101
102 return retval;
103
104 global_root:
105 namelen = dentry->d_name.len;
106 buflen -= namelen;
107 if (buflen < 0)
108 goto Elong;
109 retval -= namelen-1; /* hit the slash */
110 memcpy(retval, dentry->d_name.name, namelen);
111 return retval;
112 Elong:
113 return ERR_PTR(-ENAMETOOLONG);
114 }
115
116 static int redir2_permission(struct inode *inode, int mask,
117 struct nameidata *nd)
118 {
119 return -ENOENT;
120 }
121
122 static int redir2_getattr(struct vfsmount *mnt, struct dentry *entry,
123 struct kstat *stat)
124 {
125 return -ENOENT;
126 }
127
128 static struct dentry *redir2_dummy_lookup(struct inode *dir, struct dentry *entry,
129 struct nameidata *nd)
130 {
131 return ERR_PTR(-ENOENT);
132 }
133
134
135 static struct inode_operations redir2_inode_operations = {
136 .permission = redir2_permission,
137 .getattr = redir2_getattr,
138 .lookup = redir2_dummy_lookup,
139 };
140
141 static int mount_avfs(struct dentry *dentry, struct vfsmount *mnt,
142 char *path, int mode)
143 {
144 struct inode *inode;
145 struct redir2_mount *newmnt;
146
147 newmnt = kmalloc(sizeof(struct redir2_mount), GFP_KERNEL);
148 if (!newmnt)
149 return -ENOMEM;
150
151 inode = new_inode(&redir2_dummy_sb);
152 if (!inode) {
153 kfree(newmnt);
154 return -ENOMEM;
155 }
156
157 inode->i_mode = mode;
158 inode->i_op = &redir2_inode_operations;
159 d_instantiate(dentry, inode);
160
161 char *argv[] = { "/usr/local/bin/redir2mount",
162 path, path + OVERLAY_DIR_LEN, NULL };
163 char *envp[] = { NULL };
164 int ret;
165 ret = call_usermodehelper(argv[0], argv, envp, 1);
166 printk("mount ret: %i\n", ret);
167 if (ret) {
168 kfree(newmnt);
169 return -EINVAL;
170 }
171 newmnt->mnt = lookup_mnt(mnt, dentry);
172 if (!newmnt->mnt) {
173 printk("not mounted\n");
174 kfree(newmnt);
175 return -EINVAL;
176 }
177
178 __module_get(THIS_MODULE);
179 list_add(&newmnt->list, &redir2_mounts);
180 printk("new mount: %p\n", newmnt->mnt);
181
182 return 0;
183 }
184
185 static int exists_avfs(char *path, int *modep)
186 {
187 int err;
188 struct nameidata avfsnd;
189
190 printk("lookup_avfs: '%s'\n", path);
191
192 avfsnd.last_type = LAST_ROOT;
193 avfsnd.flags = 0;
194 avfsnd.mnt = mntget(orig_mount);
195 avfsnd.dentry = dget(orig_mount->mnt_sb->s_root);
196 err = path_walk(path, &avfsnd);
197 if (err)
198 return 0;
199
200 if(!avfsnd.dentry->d_inode) {
201 path_release(&avfsnd);
202 return 0;
203 }
204 *modep = avfsnd.dentry->d_inode->i_mode;
205 path_release(&avfsnd);
206 return 1;
207 }
208
209 static int lookup_avfs(struct dentry *dentry, struct vfsmount *mnt)
210 {
211 char *page;
212 char *path;
213 int err;
214
215 err = -ENOMEM;
216 page = (char *) __get_free_page(GFP_KERNEL);
217 if (page) {
218 spin_lock(&dcache_lock);
219 path = my_d_path(dentry, mnt, mnt->mnt_sb->s_root, mnt, page, PAGE_SIZE);
220 spin_unlock(&dcache_lock);
221 err = -ENAMETOOLONG;
222 if (!IS_ERR(path) && page + OVERLAY_DIR_LEN < path) {
223 int mode;
224 path -= OVERLAY_DIR_LEN;
225 memcpy(path, OVERLAY_DIR, OVERLAY_DIR_LEN);
226
227 if (exists_avfs(path, &mode))
228 err = mount_avfs(dentry, mnt, path, mode);
229 else
230 err = -ENOENT;
231 }
232 free_page((unsigned long) page);
233 }
234 return err;
235 }
236
237 static int redir2_dentry_revalidate(struct dentry *dentry,
238 struct nameidata *nd)
239 {
240 //printk("redir2_dentry_revalidate\n");
241 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
242 if (current->pid == mount_pid)
243 return 1;
244
245 printk("redir2_dentry_revalidate: still pending\n");
246 down(&redir2_sem);
247 printk("redir2_dentry_revalidate: OK\n");
248 up(&redir2_sem);
249 }
250 if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
251 BUG();
252 if (!dentry->d_inode || d_unhashed(dentry))
253 return 0;
254 return 1;
255 }
256
257 static int redir2_dentry_delete(struct dentry *dentry)
258 {
259 printk("redir2_dentry_delete %p '%.*s'\n", dentry,
260 dentry->d_name.len, dentry->d_name.name);
261
262 module_put(THIS_MODULE);
263 return 1;
264 }
265
266 static struct dentry_operations redir2_dentry_operations = {
267 .d_revalidate = redir2_dentry_revalidate,
268 .d_delete = redir2_dentry_delete,
269 };
270
271 static inline int is_create(struct nameidata *nd)
272 {
273 if (!nd)
274 return 1;
275 if ((nd->flags & LOOKUP_CREATE) && !(nd->flags & LOOKUP_CONTINUE))
276 return 1;
277 return 0;
278 }
279
280 static int lookup_maybeavfs(struct inode *dir, struct dentry *dentry,
281 struct nameidata *nd)
282 {
283 int err;
284
285 if (!try_module_get(THIS_MODULE))
286 return -EBUSY;
287
288 down(&redir2_sem);
289 lookup_pid = current->pid;
290 printk("redir2_dentry_add %p '%.*s'\n", dentry,
291 dentry->d_name.len, dentry->d_name.name);
292 mount_pid = -1;
293 dentry->d_op = &redir2_dentry_operations;
294 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
295 d_add(dentry, NULL);
296 up(&dir->i_sem);
297 err = lookup_avfs(dentry, nd->mnt);
298 if (err)
299 d_drop(dentry);
300 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
301 lookup_pid = -1;
302 up(&redir2_sem);
303 down(&dir->i_sem);
304 return err;
305 }
306
307 static struct dentry *redir2_lookup(struct inode *dir, struct dentry *dentry,
308 struct nameidata *nd)
309 {
310 int err;
311 //printk("lookup %.*s\n", dentry->d_name.len, dentry->d_name.name);
312
313 if (current->pid == lookup_pid || is_create(nd) ||
314 !is_avfs(dentry->d_name.name, dentry->d_name.len))
315 return orig_lookup(dir, dentry, nd);
316
317 err = lookup_maybeavfs(dir, dentry, nd);
318 if (err)
319 return ERR_PTR(err);
320 return NULL;
321 }
322
323 static void redir2_release_mount(struct redir2_mount *mnt)
324 {
325 printk("releasing mount: %p\n", mnt->mnt);
326 mntput(mnt->mnt);
327 list_del(&mnt->list);
328 kfree(mnt);
329 module_put(THIS_MODULE);
330 }
331
332 static int umount_avfs(struct redir2_mount *mnt, char *path)
333 {
334 char *argv[] = { "/usr/local/bin/redir2mount", "-", path, NULL };
335 char *envp[] = { NULL };
336 int ret;
337 redir2_release_mount(mnt);
338 printk("umount\n");
339 ret = call_usermodehelper(argv[0], argv, envp, 1);
340 printk("umount ret: %i\n", ret);
341 if (ret)
342 return -EINVAL;
343
344 return 0;
345 }
346
347 static void redir2_try_umount(struct redir2_mount *mnt)
348 {
349 char *page;
350 char *path;
351 struct dentry *dentry;
352 struct vfsmount *pmnt;
353
354 page = (char *) __get_free_page(GFP_KERNEL);
355 if (!page)
356 return;
357
358 spin_lock(&vfsmount_lock);
359 if (mnt->mnt->mnt_parent == mnt->mnt) {
360 /* Already unmounted */
361 spin_unlock(&vfsmount_lock);
362 redir2_release_mount(mnt);
363 free_page((unsigned long) page);
364 return;
365 }
366 pmnt = mntget(mnt->mnt->mnt_parent);
367 dentry = dget(mnt->mnt->mnt_mountpoint);
368 spin_unlock(&vfsmount_lock);
369
370 spin_lock(&dcache_lock);
371 path = my_d_path(dentry, pmnt, pmnt->mnt_sb->s_root, pmnt, page, PAGE_SIZE);
372 spin_unlock(&dcache_lock);
373 if (!IS_ERR(path))
374 umount_avfs(mnt, path);
375 free_page((unsigned long) page);
376 dput(dentry);
377 mntput(pmnt);
378 }
379
380 static int redir2_flush(struct file *file, const char __user *buffer,
381 unsigned long count, void *data)
382 {
383 struct redir2_mount *mnt;
384 struct redir2_mount *next;
385 printk("redir2_flush (%i)\n", current->pid);
386 down(&redir2_sem);
387 list_for_each_entry_safe (mnt, next, &redir2_mounts, list) {
388 int cnt = atomic_read(&mnt->mnt->mnt_count);
389 printk("mount %p has count %u\n", mnt->mnt, cnt);
390 if (cnt <= 2) {
391 redir2_try_umount(mnt);
392 // break;
393 }
394 }
395 up(&redir2_sem);
396 return count;
397 }
398
399 static int mount_pid_write(struct file *file, const char __user *buffer,
400 unsigned long count, void *data)
401 {
402 char buf[32];
403 if(count > sizeof(buf))
404 return -EINVAL;
405 if(copy_from_user(buf, buffer, count))
406 return -EFAULT;
407 mount_pid = simple_strtol(buf, NULL, 10);
408 return count;
409
410 }
411
412 static void redir2_init_proc(void)
413 {
414 redir2_proc_dir = proc_mkdir("redir2", proc_root_fs);
415 if (redir2_proc_dir) {
416 struct proc_dir_entry *e;
417 redir2_proc_dir->owner = THIS_MODULE;
418 e = create_proc_entry("mount_pid", S_IFREG | 0200, redir2_proc_dir);
419 if (e) {
420 e->owner = THIS_MODULE;
421 e->write_proc = mount_pid_write;
422 }
423 e = create_proc_entry("flush", S_IFREG | 0200, redir2_proc_dir);
424 if (e) {
425 e->owner = THIS_MODULE;
426 e->write_proc = redir2_flush;
427 }
428 }
429 }
430
431 static int __init init_redir2(void)
432 {
433 printk(KERN_INFO "redir2 init (version %s)\n", REDIR2_VERSION);
434
435 sema_init(&redir2_sem, 1);
436 redir2_init_proc();
437 read_lock(&current->fs->lock);
438 orig_mount = mntget(current->fs->rootmnt);
439 orig_lookup_ptr = &current->fs->root->d_inode->i_op->lookup;
440 orig_lookup = *orig_lookup_ptr;
441 *orig_lookup_ptr = redir2_lookup;
442 read_unlock(&current->fs->lock);
443
444 /* FIXME: This is a bit too brutal approach */
445 printk("shrinking dcache...\n");
446 shrink_dcache_sb(orig_mount->mnt_sb);
447 printk("done\n");
448
449 return 0;
450 }
451
452 static void __exit exit_redir2(void)
453 {
454 printk(KERN_INFO "redir2 cleanup\n");
455
456 if (orig_lookup_ptr)
457 *orig_lookup_ptr = orig_lookup;
458 mntput(orig_mount);
459 if (redir2_proc_dir) {
460 remove_proc_entry("mount_pid", redir2_proc_dir);
461 remove_proc_entry("flush", redir2_proc_dir);
462 remove_proc_entry("redir2", proc_root_fs);
463 }
464 }
465
466
467 module_init(init_redir2)
468 module_exit(exit_redir2)
469
470
471 MODULE_LICENSE("GPL");
472
473 /*
474 * Local Variables:
475 * indent-tabs-mode: t
476 * c-basic-offset: 8
477 * End:
478 */