update aufs to it's latest standalone 3.2 branch
[pandora-kernel.git] / fs / aufs / loop.c
1 /*
2  * Copyright (C) 2005-2013 Junjiro R. Okajima
3  *
4  * This program, aufs is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * support for loopback block device as a branch
21  */
22
23 #include <linux/loop.h>
24 #include "aufs.h"
25
26 /*
27  * test if two lower dentries have overlapping branches.
28  */
29 int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
30 {
31         struct super_block *h_sb;
32         struct loop_device *l;
33
34         h_sb = h_adding->d_sb;
35         if (MAJOR(h_sb->s_dev) != LOOP_MAJOR)
36                 return 0;
37
38         l = h_sb->s_bdev->bd_disk->private_data;
39         h_adding = l->lo_backing_file->f_dentry;
40         /*
41          * h_adding can be local NFS.
42          * in this case aufs cannot detect the loop.
43          */
44         if (unlikely(h_adding->d_sb == sb))
45                 return 1;
46         return !!au_test_subdir(h_adding, sb->s_root);
47 }
48
49 /* true if a kernel thread named 'loop[0-9].*' accesses a file */
50 int au_test_loopback_kthread(void)
51 {
52         int ret;
53         struct task_struct *tsk = current;
54         char c, comm[sizeof(tsk->comm)];
55
56         ret = 0;
57         if (tsk->flags & PF_KTHREAD) {
58                 get_task_comm(comm, tsk);
59                 c = comm[4];
60                 ret = ('0' <= c && c <= '9'
61                        && !strncmp(comm, "loop", 4));
62         }
63
64         return ret;
65 }
66
67 /* ---------------------------------------------------------------------- */
68
69 #define au_warn_loopback_step   16
70 static int au_warn_loopback_nelem = au_warn_loopback_step;
71 static unsigned long *au_warn_loopback_array;
72
73 void au_warn_loopback(struct super_block *h_sb)
74 {
75         int i, new_nelem;
76         unsigned long *a, magic;
77         static DEFINE_SPINLOCK(spin);
78
79         magic = h_sb->s_magic;
80         spin_lock(&spin);
81         a = au_warn_loopback_array;
82         for (i = 0; i < au_warn_loopback_nelem && *a; i++)
83                 if (a[i] == magic) {
84                         spin_unlock(&spin);
85                         return;
86                 }
87
88         /* h_sb is new to us, print it */
89         if (i < au_warn_loopback_nelem) {
90                 a[i] = magic;
91                 goto pr;
92         }
93
94         /* expand the array */
95         new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
96         a = au_kzrealloc(au_warn_loopback_array,
97                          au_warn_loopback_nelem * sizeof(unsigned long),
98                          new_nelem * sizeof(unsigned long), GFP_ATOMIC);
99         if (a) {
100                 au_warn_loopback_nelem = new_nelem;
101                 au_warn_loopback_array = a;
102                 a[i] = magic;
103                 goto pr;
104         }
105
106         spin_unlock(&spin);
107         AuWarn1("realloc failed, ignored\n");
108         return;
109
110 pr:
111         spin_unlock(&spin);
112         pr_warn("you may want to try another patch for loopback file "
113                 "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
114 }
115
116 int au_loopback_init(void)
117 {
118         int err;
119         struct super_block *sb __maybe_unused;
120
121         AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
122
123         err = 0;
124         au_warn_loopback_array = kcalloc(au_warn_loopback_step,
125                                          sizeof(unsigned long), GFP_NOFS);
126         if (unlikely(!au_warn_loopback_array))
127                 err = -ENOMEM;
128
129         return err;
130 }
131
132 void au_loopback_fin(void)
133 {
134         kfree(au_warn_loopback_array);
135 }