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