Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / sbinfo.c
1 /*
2  * Copyright (C) 2005-2012 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  * superblock private data
21  */
22
23 #include "aufs.h"
24
25 /*
26  * they are necessary regardless sysfs is disabled.
27  */
28 void au_si_free(struct kobject *kobj)
29 {
30         struct au_sbinfo *sbinfo;
31         char *locked __maybe_unused; /* debug only */
32
33         sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
34         AuDebugOn(!list_empty(&sbinfo->si_plink.head));
35         AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
36
37         au_rw_write_lock(&sbinfo->si_rwsem);
38         au_br_free(sbinfo);
39         au_rw_write_unlock(&sbinfo->si_rwsem);
40
41         AuDebugOn(radix_tree_gang_lookup
42                   (&sbinfo->au_si_pid.tree, (void **)&locked,
43                    /*first_index*/PID_MAX_DEFAULT - 1,
44                    /*max_items*/sizeof(locked)/sizeof(*locked)));
45
46         kfree(sbinfo->si_branch);
47         kfree(sbinfo->au_si_pid.bitmap);
48         mutex_destroy(&sbinfo->si_xib_mtx);
49         AuRwDestroy(&sbinfo->si_rwsem);
50
51         kfree(sbinfo);
52 }
53
54 int au_si_alloc(struct super_block *sb)
55 {
56         int err;
57         struct au_sbinfo *sbinfo;
58         static struct lock_class_key aufs_si;
59
60         err = -ENOMEM;
61         sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
62         if (unlikely(!sbinfo))
63                 goto out;
64
65         BUILD_BUG_ON(sizeof(unsigned long) !=
66                      sizeof(*sbinfo->au_si_pid.bitmap));
67         sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
68                                         sizeof(*sbinfo->au_si_pid.bitmap),
69                                         GFP_NOFS);
70         if (unlikely(!sbinfo->au_si_pid.bitmap))
71                 goto out_sbinfo;
72
73         /* will be reallocated separately */
74         sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
75         if (unlikely(!sbinfo->si_branch))
76                 goto out_pidmap;
77
78         err = sysaufs_si_init(sbinfo);
79         if (unlikely(err))
80                 goto out_br;
81
82         au_nwt_init(&sbinfo->si_nowait);
83         au_rw_init_wlock(&sbinfo->si_rwsem);
84         au_rw_class(&sbinfo->si_rwsem, &aufs_si);
85         spin_lock_init(&sbinfo->au_si_pid.tree_lock);
86         INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
87
88         atomic_long_set(&sbinfo->si_ninodes, 0);
89         atomic_long_set(&sbinfo->si_nfiles, 0);
90
91         sbinfo->si_bend = -1;
92
93         sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
94         sbinfo->si_wbr_create = AuWbrCreate_Def;
95         sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
96         sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
97
98         sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
99
100         mutex_init(&sbinfo->si_xib_mtx);
101         sbinfo->si_xino_brid = -1;
102         /* leave si_xib_last_pindex and si_xib_next_bit */
103
104         sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
105         sbinfo->si_rdblk = AUFS_RDBLK_DEF;
106         sbinfo->si_rdhash = AUFS_RDHASH_DEF;
107         sbinfo->si_dirwh = AUFS_DIRWH_DEF;
108
109         au_spl_init(&sbinfo->si_plink);
110         init_waitqueue_head(&sbinfo->si_plink_wq);
111         spin_lock_init(&sbinfo->si_plink_maint_lock);
112
113         /* leave other members for sysaufs and si_mnt. */
114         sbinfo->si_sb = sb;
115         sb->s_fs_info = sbinfo;
116         si_pid_set(sb);
117         au_debug_sbinfo_init(sbinfo);
118         return 0; /* success */
119
120 out_br:
121         kfree(sbinfo->si_branch);
122 out_pidmap:
123         kfree(sbinfo->au_si_pid.bitmap);
124 out_sbinfo:
125         kfree(sbinfo);
126 out:
127         return err;
128 }
129
130 int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
131 {
132         int err, sz;
133         struct au_branch **brp;
134
135         AuRwMustWriteLock(&sbinfo->si_rwsem);
136
137         err = -ENOMEM;
138         sz = sizeof(*brp) * (sbinfo->si_bend + 1);
139         if (unlikely(!sz))
140                 sz = sizeof(*brp);
141         brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
142         if (brp) {
143                 sbinfo->si_branch = brp;
144                 err = 0;
145         }
146
147         return err;
148 }
149
150 /* ---------------------------------------------------------------------- */
151
152 unsigned int au_sigen_inc(struct super_block *sb)
153 {
154         unsigned int gen;
155
156         SiMustWriteLock(sb);
157
158         gen = ++au_sbi(sb)->si_generation;
159         au_update_digen(sb->s_root);
160         au_update_iigen(sb->s_root->d_inode);
161         sb->s_root->d_inode->i_version++;
162         return gen;
163 }
164
165 aufs_bindex_t au_new_br_id(struct super_block *sb)
166 {
167         aufs_bindex_t br_id;
168         int i;
169         struct au_sbinfo *sbinfo;
170
171         SiMustWriteLock(sb);
172
173         sbinfo = au_sbi(sb);
174         for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
175                 br_id = ++sbinfo->si_last_br_id;
176                 AuDebugOn(br_id < 0);
177                 if (br_id && au_br_index(sb, br_id) < 0)
178                         return br_id;
179         }
180
181         return -1;
182 }
183
184 /* ---------------------------------------------------------------------- */
185
186 /* it is ok that new 'nwt' tasks are appended while we are sleeping */
187 int si_read_lock(struct super_block *sb, int flags)
188 {
189         int err;
190
191         err = 0;
192         if (au_ftest_lock(flags, FLUSH))
193                 au_nwt_flush(&au_sbi(sb)->si_nowait);
194
195         si_noflush_read_lock(sb);
196         err = au_plink_maint(sb, flags);
197         if (unlikely(err))
198                 si_read_unlock(sb);
199
200         return err;
201 }
202
203 int si_write_lock(struct super_block *sb, int flags)
204 {
205         int err;
206
207         if (au_ftest_lock(flags, FLUSH))
208                 au_nwt_flush(&au_sbi(sb)->si_nowait);
209
210         si_noflush_write_lock(sb);
211         err = au_plink_maint(sb, flags);
212         if (unlikely(err))
213                 si_write_unlock(sb);
214
215         return err;
216 }
217
218 /* dentry and super_block lock. call at entry point */
219 int aufs_read_lock(struct dentry *dentry, int flags)
220 {
221         int err;
222         struct super_block *sb;
223
224         sb = dentry->d_sb;
225         err = si_read_lock(sb, flags);
226         if (unlikely(err))
227                 goto out;
228
229         if (au_ftest_lock(flags, DW))
230                 di_write_lock_child(dentry);
231         else
232                 di_read_lock_child(dentry, flags);
233
234         if (au_ftest_lock(flags, GEN)) {
235                 err = au_digen_test(dentry, au_sigen(sb));
236                 AuDebugOn(!err && au_dbrange_test(dentry));
237                 if (unlikely(err))
238                         aufs_read_unlock(dentry, flags);
239         }
240
241 out:
242         return err;
243 }
244
245 void aufs_read_unlock(struct dentry *dentry, int flags)
246 {
247         if (au_ftest_lock(flags, DW))
248                 di_write_unlock(dentry);
249         else
250                 di_read_unlock(dentry, flags);
251         si_read_unlock(dentry->d_sb);
252 }
253
254 void aufs_write_lock(struct dentry *dentry)
255 {
256         si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
257         di_write_lock_child(dentry);
258 }
259
260 void aufs_write_unlock(struct dentry *dentry)
261 {
262         di_write_unlock(dentry);
263         si_write_unlock(dentry->d_sb);
264 }
265
266 int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
267 {
268         int err;
269         unsigned int sigen;
270         struct super_block *sb;
271
272         sb = d1->d_sb;
273         err = si_read_lock(sb, flags);
274         if (unlikely(err))
275                 goto out;
276
277         di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
278
279         if (au_ftest_lock(flags, GEN)) {
280                 sigen = au_sigen(sb);
281                 err = au_digen_test(d1, sigen);
282                 AuDebugOn(!err && au_dbrange_test(d1));
283                 if (!err) {
284                         err = au_digen_test(d2, sigen);
285                         AuDebugOn(!err && au_dbrange_test(d2));
286                 }
287                 if (unlikely(err))
288                         aufs_read_and_write_unlock2(d1, d2);
289         }
290
291 out:
292         return err;
293 }
294
295 void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
296 {
297         di_write_unlock2(d1, d2);
298         si_read_unlock(d1->d_sb);
299 }
300
301 /* ---------------------------------------------------------------------- */
302
303 int si_pid_test_slow(struct super_block *sb)
304 {
305         void *p;
306
307         rcu_read_lock();
308         p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
309         rcu_read_unlock();
310
311         return (long)!!p;
312 }
313
314 void si_pid_set_slow(struct super_block *sb)
315 {
316         int err;
317         struct au_sbinfo *sbinfo;
318
319         AuDebugOn(si_pid_test_slow(sb));
320
321         sbinfo = au_sbi(sb);
322         err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
323         AuDebugOn(err);
324         spin_lock(&sbinfo->au_si_pid.tree_lock);
325         err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
326                                 /*any valid ptr*/sb);
327         spin_unlock(&sbinfo->au_si_pid.tree_lock);
328         AuDebugOn(err);
329         radix_tree_preload_end();
330 }
331
332 void si_pid_clr_slow(struct super_block *sb)
333 {
334         void *p;
335         struct au_sbinfo *sbinfo;
336
337         AuDebugOn(!si_pid_test_slow(sb));
338
339         sbinfo = au_sbi(sb);
340         spin_lock(&sbinfo->au_si_pid.tree_lock);
341         p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
342         spin_unlock(&sbinfo->au_si_pid.tree_lock);
343 }