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