]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/aufs/sbinfo.c
UBUNTU: SAUCE: Import aufs driver
[mirror_ubuntu-zesty-kernel.git] / fs / aufs / sbinfo.c
1 /*
2 * Copyright (C) 2005-2016 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * superblock private data
20 */
21
22 #include "aufs.h"
23
24 /*
25 * they are necessary regardless sysfs is disabled.
26 */
27 void au_si_free(struct kobject *kobj)
28 {
29 int i;
30 struct au_sbinfo *sbinfo;
31 char *locked __maybe_unused; /* debug only */
32
33 sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
34 for (i = 0; i < AuPlink_NHASH; i++)
35 AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
36 AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
37
38 AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
39 percpu_counter_destroy(&sbinfo->si_ninodes);
40 AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
41 percpu_counter_destroy(&sbinfo->si_nfiles);
42
43 au_rw_write_lock(&sbinfo->si_rwsem);
44 au_br_free(sbinfo);
45 au_rw_write_unlock(&sbinfo->si_rwsem);
46
47 au_delayed_kfree(sbinfo->si_branch);
48 for (i = 0; i < AU_NPIDMAP; i++)
49 if (sbinfo->au_si_pid.pid_bitmap[i])
50 au_delayed_kfree(sbinfo->au_si_pid.pid_bitmap[i]);
51 mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
52 mutex_destroy(&sbinfo->si_xib_mtx);
53 AuRwDestroy(&sbinfo->si_rwsem);
54
55 au_delayed_kfree(sbinfo);
56 }
57
58 int au_si_alloc(struct super_block *sb)
59 {
60 int err, i;
61 struct au_sbinfo *sbinfo;
62
63 err = -ENOMEM;
64 sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
65 if (unlikely(!sbinfo))
66 goto out;
67
68 /* will be reallocated separately */
69 sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
70 if (unlikely(!sbinfo->si_branch))
71 goto out_sbinfo;
72
73 err = sysaufs_si_init(sbinfo);
74 if (unlikely(err))
75 goto out_br;
76
77 au_nwt_init(&sbinfo->si_nowait);
78 au_rw_init_wlock(&sbinfo->si_rwsem);
79 mutex_init(&sbinfo->au_si_pid.pid_mtx);
80
81 percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
82 percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
83
84 sbinfo->si_bbot = -1;
85 sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
86
87 sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
88 sbinfo->si_wbr_create = AuWbrCreate_Def;
89 sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
90 sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
91
92 au_fhsm_init(sbinfo);
93
94 sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
95
96 sbinfo->si_xino_jiffy = jiffies;
97 sbinfo->si_xino_expire
98 = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
99 mutex_init(&sbinfo->si_xib_mtx);
100 sbinfo->si_xino_brid = -1;
101 /* leave si_xib_last_pindex and si_xib_next_bit */
102
103 au_sphl_init(&sbinfo->si_aopen);
104
105 sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
106 sbinfo->si_rdblk = AUFS_RDBLK_DEF;
107 sbinfo->si_rdhash = AUFS_RDHASH_DEF;
108 sbinfo->si_dirwh = AUFS_DIRWH_DEF;
109
110 for (i = 0; i < AuPlink_NHASH; i++)
111 au_sphl_init(sbinfo->si_plink + i);
112 init_waitqueue_head(&sbinfo->si_plink_wq);
113 spin_lock_init(&sbinfo->si_plink_maint_lock);
114
115 au_sphl_init(&sbinfo->si_files);
116
117 /* with getattr by default */
118 sbinfo->si_iop_array = aufs_iop;
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 return 0; /* success */
125
126 out_br:
127 au_delayed_kfree(sbinfo->si_branch);
128 out_sbinfo:
129 au_delayed_kfree(sbinfo);
130 out:
131 return err;
132 }
133
134 int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
135 {
136 int err, sz;
137 struct au_branch **brp;
138
139 AuRwMustWriteLock(&sbinfo->si_rwsem);
140
141 err = -ENOMEM;
142 sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
143 if (unlikely(!sz))
144 sz = sizeof(*brp);
145 brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
146 may_shrink);
147 if (brp) {
148 sbinfo->si_branch = brp;
149 err = 0;
150 }
151
152 return err;
153 }
154
155 /* ---------------------------------------------------------------------- */
156
157 unsigned int au_sigen_inc(struct super_block *sb)
158 {
159 unsigned int gen;
160 struct inode *inode;
161
162 SiMustWriteLock(sb);
163
164 gen = ++au_sbi(sb)->si_generation;
165 au_update_digen(sb->s_root);
166 inode = d_inode(sb->s_root);
167 au_update_iigen(inode, /*half*/0);
168 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 if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
244 AuDebugOn(!err && au_dbrange_test(dentry));
245 else if (!err)
246 err = au_dbrange_test(dentry);
247 if (unlikely(err))
248 aufs_read_unlock(dentry, flags);
249 }
250
251 out:
252 return err;
253 }
254
255 void aufs_read_unlock(struct dentry *dentry, int flags)
256 {
257 if (au_ftest_lock(flags, DW))
258 di_write_unlock(dentry);
259 else
260 di_read_unlock(dentry, flags);
261 si_read_unlock(dentry->d_sb);
262 }
263
264 void aufs_write_lock(struct dentry *dentry)
265 {
266 si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
267 di_write_lock_child(dentry);
268 }
269
270 void aufs_write_unlock(struct dentry *dentry)
271 {
272 di_write_unlock(dentry);
273 si_write_unlock(dentry->d_sb);
274 }
275
276 int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
277 {
278 int err;
279 unsigned int sigen;
280 struct super_block *sb;
281
282 sb = d1->d_sb;
283 err = si_read_lock(sb, flags);
284 if (unlikely(err))
285 goto out;
286
287 di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
288
289 if (au_ftest_lock(flags, GEN)) {
290 sigen = au_sigen(sb);
291 err = au_digen_test(d1, sigen);
292 AuDebugOn(!err && au_dbrange_test(d1));
293 if (!err) {
294 err = au_digen_test(d2, sigen);
295 AuDebugOn(!err && au_dbrange_test(d2));
296 }
297 if (unlikely(err))
298 aufs_read_and_write_unlock2(d1, d2);
299 }
300
301 out:
302 return err;
303 }
304
305 void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
306 {
307 di_write_unlock2(d1, d2);
308 si_read_unlock(d1->d_sb);
309 }
310
311 /* ---------------------------------------------------------------------- */
312
313 static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
314 {
315 unsigned long *p;
316
317 BUILD_BUG_ON(sizeof(unsigned long) !=
318 sizeof(*au_si_pid->pid_bitmap));
319
320 mutex_lock(&au_si_pid->pid_mtx);
321 p = au_si_pid->pid_bitmap[idx];
322 while (!p) {
323 /*
324 * bad approach.
325 * but keeping 'si_pid_set()' void is more important.
326 */
327 p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
328 sizeof(*au_si_pid->pid_bitmap),
329 GFP_NOFS);
330 if (p)
331 break;
332 cond_resched();
333 }
334 au_si_pid->pid_bitmap[idx] = p;
335 mutex_unlock(&au_si_pid->pid_mtx);
336 }
337
338 void si_pid_set(struct super_block *sb)
339 {
340 pid_t bit;
341 int idx;
342 unsigned long *bitmap;
343 struct au_si_pid *au_si_pid;
344
345 si_pid_idx_bit(&idx, &bit);
346 au_si_pid = &au_sbi(sb)->au_si_pid;
347 bitmap = au_si_pid->pid_bitmap[idx];
348 if (!bitmap) {
349 si_pid_alloc(au_si_pid, idx);
350 bitmap = au_si_pid->pid_bitmap[idx];
351 }
352 AuDebugOn(test_bit(bit, bitmap));
353 set_bit(bit, bitmap);
354 /* smp_mb(); */
355 }