2
* Copyright (C) 2005-2008 Junjiro Okajima
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.
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.
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
22
* $Id: dinfo.c,v 1.6 2008/07/14 00:14:33 sfjro Exp $
27
int au_alloc_dinfo(struct dentry *dentry)
29
struct au_dinfo *dinfo;
30
struct super_block *sb;
33
LKTRTrace("%.*s\n", AuDLNPair(dentry));
34
AuDebugOn(dentry->d_fsdata);
36
dinfo = au_cache_alloc_dinfo();
39
nbr = au_sbend(sb) + 1;
40
if (unlikely(nbr <= 0))
42
dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry),
44
if (dinfo->di_hdentry) {
45
au_h_dentry_init_all(dinfo->di_hdentry, nbr);
46
atomic_set(&dinfo->di_generation, au_sigen(sb));
47
/* smp_mb(); */ /* atomic_set */
48
au_rw_init_wlock_nested(&dinfo->di_rwsem,
50
dinfo->di_bstart = -1;
53
dinfo->di_bdiropq = -1;
55
dentry->d_fsdata = dinfo;
56
dentry->d_op = &aufs_dop;
57
return 0; /* success */
59
au_cache_free_dinfo(dinfo);
65
struct au_dinfo *au_di(struct dentry *dentry)
67
struct au_dinfo *dinfo = dentry->d_fsdata;
70
/* || au_sbi(dentry->d_sb)->si_bend < dinfo->di_bend */
71
|| dinfo->di_bend < dinfo->di_bstart
72
/* dbwh can be outside of this range */
73
|| (0 <= dinfo->di_bdiropq
74
&& (dinfo->di_bdiropq < dinfo->di_bstart
75
/* || dinfo->di_bend < dinfo->di_bdiropq */))
80
/* ---------------------------------------------------------------------- */
82
static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
86
ii_write_lock_child(inode);
89
ii_write_lock_child2(inode);
92
ii_write_lock_child3(inode);
95
ii_write_lock_parent(inode);
97
case AuLsc_DI_PARENT2:
98
ii_write_lock_parent2(inode);
100
case AuLsc_DI_PARENT3:
101
ii_write_lock_parent3(inode);
103
case AuLsc_DI_PARENT4:
104
ii_write_lock_parent4(inode);
111
static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
115
ii_read_lock_child(inode);
117
case AuLsc_DI_CHILD2:
118
ii_read_lock_child2(inode);
120
case AuLsc_DI_CHILD3:
121
ii_read_lock_child3(inode);
123
case AuLsc_DI_PARENT:
124
ii_read_lock_parent(inode);
126
case AuLsc_DI_PARENT2:
127
ii_read_lock_parent2(inode);
129
case AuLsc_DI_PARENT3:
130
ii_read_lock_parent3(inode);
132
case AuLsc_DI_PARENT4:
133
ii_read_lock_parent4(inode);
140
void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
142
LKTRTrace("%.*s, %u\n", AuDLNPair(d), lsc);
144
SiMustAnyLock(d->d_sb);
145
/* todo: always nested? */
146
au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
148
if (au_ftest_lock(flags, IW))
149
do_ii_write_lock(d->d_inode, lsc);
150
else if (au_ftest_lock(flags, IR))
151
do_ii_read_lock(d->d_inode, lsc);
155
void di_read_unlock(struct dentry *d, int flags)
157
LKTRTrace("%.*s\n", AuDLNPair(d));
159
SiMustAnyLock(d->d_sb);
161
if (au_ftest_lock(flags, IW))
162
ii_write_unlock(d->d_inode);
163
else if (au_ftest_lock(flags, IR))
164
ii_read_unlock(d->d_inode);
166
au_rw_read_unlock(&au_di(d)->di_rwsem);
169
void di_downgrade_lock(struct dentry *d, int flags)
171
SiMustAnyLock(d->d_sb);
172
au_rw_dgrade_lock(&au_di(d)->di_rwsem);
173
if (d->d_inode && au_ftest_lock(flags, IR))
174
ii_downgrade_lock(d->d_inode);
177
void di_write_lock(struct dentry *d, unsigned int lsc)
179
LKTRTrace("%.*s, %u\n", AuDLNPair(d), lsc);
181
SiMustAnyLock(d->d_sb);
182
/* todo: always nested? */
183
au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
185
do_ii_write_lock(d->d_inode, lsc);
188
void di_write_unlock(struct dentry *d)
190
LKTRTrace("%.*s\n", AuDLNPair(d));
192
SiMustAnyLock(d->d_sb);
194
ii_write_unlock(d->d_inode);
195
au_rw_write_unlock(&au_di(d)->di_rwsem);
198
void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
202
|| d1->d_inode == d2->d_inode
203
|| d1->d_sb != d2->d_sb);
205
if (isdir && au_test_subdir(d1, d2)) {
206
di_write_lock_child(d1);
207
di_write_lock_child2(d2);
209
/* there should be no races */
210
di_write_lock_child(d2);
211
di_write_lock_child2(d1);
215
void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
219
|| d1->d_inode == d2->d_inode
220
|| d1->d_sb != d2->d_sb);
222
if (isdir && au_test_subdir(d1, d2)) {
223
di_write_lock_parent(d1);
224
di_write_lock_parent2(d2);
226
/* there should be no races */
227
di_write_lock_parent(d2);
228
di_write_lock_parent2(d1);
232
void di_write_unlock2(struct dentry *d1, struct dentry *d2)
235
if (d1->d_inode == d2->d_inode)
236
au_rw_write_unlock(&au_di(d2)->di_rwsem);
241
/* ---------------------------------------------------------------------- */
243
struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
247
DiMustAnyLock(dentry);
248
if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
251
/* || bindex > au_sbend(dentry->d_sb) */);
252
d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
253
AuDebugOn(d && (atomic_read(&d->d_count) <= 0));
257
aufs_bindex_t au_dbtail(struct dentry *dentry)
259
aufs_bindex_t bend, bwh;
261
bend = au_dbend(dentry);
263
bwh = au_dbwh(dentry);
266
if (0 < bwh && bwh < bend)
272
aufs_bindex_t au_dbtaildir(struct dentry *dentry)
274
aufs_bindex_t bend, bopq;
276
AuDebugOn(dentry->d_inode
277
&& dentry->d_inode->i_mode
278
&& !S_ISDIR(dentry->d_inode->i_mode));
280
bend = au_dbtail(dentry);
282
bopq = au_dbdiropq(dentry);
283
AuDebugOn(bend < bopq);
284
if (0 <= bopq && bopq < bend)
290
#if 0 /* reserved for future use */
291
aufs_bindex_t au_dbtail_generic(struct dentry *dentry)
295
inode = dentry->d_inode;
296
if (inode && S_ISDIR(inode->i_mode))
297
return au_dbtaildir(dentry);
299
return au_dbtail(dentry);
303
/* ---------------------------------------------------------------------- */
305
void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
307
DiMustWriteLock(dentry);
308
AuDebugOn(au_sbend(dentry->d_sb) < bindex);
309
AuDebugOn((bindex >= 0
310
&& (bindex < au_dbstart(dentry)
311
|| au_dbend(dentry) < bindex))
313
&& dentry->d_inode->i_mode
314
&& !S_ISDIR(dentry->d_inode->i_mode)));
315
au_di(dentry)->di_bdiropq = bindex;
318
void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
319
struct dentry *h_dentry)
321
struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
322
DiMustWriteLock(dentry);
323
AuDebugOn(bindex < au_di(dentry)->di_bstart
324
|| bindex > au_di(dentry)->di_bend
325
|| (h_dentry && atomic_read(&h_dentry->d_count) <= 0)
326
|| (h_dentry && hd->hd_dentry)
329
au_hdput(hd, /*do_free*/0);
330
hd->hd_dentry = h_dentry;
333
/* ---------------------------------------------------------------------- */
335
void au_update_dbrange(struct dentry *dentry, int do_put_zero)
337
struct au_dinfo *dinfo;
338
aufs_bindex_t bindex;
341
LKTRTrace("%.*s, %d\n", AuDLNPair(dentry), do_put_zero);
342
DiMustWriteLock(dentry);
344
dinfo = au_di(dentry);
345
if (unlikely(!dinfo) || dinfo->di_bstart < 0)
349
for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend;
351
h_d = dinfo->di_hdentry[0 + bindex].hd_dentry;
352
if (h_d && !h_d->d_inode)
353
au_set_h_dptr(dentry, bindex, NULL);
357
dinfo->di_bstart = -1;
358
while (++dinfo->di_bstart <= dinfo->di_bend)
359
if (dinfo->di_hdentry[0 + dinfo->di_bstart].hd_dentry)
361
if (dinfo->di_bstart > dinfo->di_bend) {
362
dinfo->di_bstart = -1;
368
while (0 <= --dinfo->di_bend)
369
if (dinfo->di_hdentry[0 + dinfo->di_bend].hd_dentry)
371
AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
374
void au_update_dbstart(struct dentry *dentry)
376
aufs_bindex_t bindex,
377
bstart = au_dbstart(dentry),
378
bend = au_dbend(dentry);
379
struct dentry *h_dentry;
381
LKTRTrace("%.*s\n", AuDLNPair(dentry));
382
DiMustWriteLock(dentry);
384
for (bindex = bstart; bindex <= bend; bindex++) {
385
h_dentry = au_h_dptr(dentry, bindex);
388
if (h_dentry->d_inode) {
389
au_set_dbstart(dentry, bindex);
392
au_set_h_dptr(dentry, bindex, NULL);
396
void au_update_dbend(struct dentry *dentry)
398
aufs_bindex_t bindex,
399
bstart = au_dbstart(dentry),
400
bend = au_dbend(dentry);
401
struct dentry *h_dentry;
403
DiMustWriteLock(dentry);
404
for (bindex = bend; bindex <= bstart; bindex--) {
405
h_dentry = au_h_dptr(dentry, bindex);
408
if (h_dentry->d_inode) {
409
au_set_dbend(dentry, bindex);
412
au_set_h_dptr(dentry, bindex, NULL);
416
int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
418
aufs_bindex_t bindex, bend;
420
bend = au_dbend(dentry);
421
for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
422
if (au_h_dptr(dentry, bindex) == h_dentry)