~ubuntu-branches/ubuntu/vivid/aufs/vivid

« back to all changes in this revision

Viewing changes to fs/aufs25/dinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2008-08-21 14:58:54 UTC
  • mfrom: (1.2.1 upstream) (11 intrepid)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20080821145854-6erljbzg007r476y
01_vserver_apparmor.dpatch: [UPDATE] Disable vserver patches on kernel 
2.6.26, because they are not needed anymore. (Closes: #495921)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005-2008 Junjiro 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
 * dentry private data
 
21
 *
 
22
 * $Id: dinfo.c,v 1.6 2008/07/14 00:14:33 sfjro Exp $
 
23
 */
 
24
 
 
25
#include "aufs.h"
 
26
 
 
27
int au_alloc_dinfo(struct dentry *dentry)
 
28
{
 
29
        struct au_dinfo *dinfo;
 
30
        struct super_block *sb;
 
31
        int nbr;
 
32
 
 
33
        LKTRTrace("%.*s\n", AuDLNPair(dentry));
 
34
        AuDebugOn(dentry->d_fsdata);
 
35
 
 
36
        dinfo = au_cache_alloc_dinfo();
 
37
        if (dinfo) {
 
38
                sb = dentry->d_sb;
 
39
                nbr = au_sbend(sb) + 1;
 
40
                if (unlikely(nbr <= 0))
 
41
                        nbr = 1;
 
42
                dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry),
 
43
                                            GFP_NOFS);
 
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,
 
49
                                                AuLsc_DI_CHILD);
 
50
                        dinfo->di_bstart = -1;
 
51
                        dinfo->di_bend = -1;
 
52
                        dinfo->di_bwh = -1;
 
53
                        dinfo->di_bdiropq = -1;
 
54
 
 
55
                        dentry->d_fsdata = dinfo;
 
56
                        dentry->d_op = &aufs_dop;
 
57
                        return 0; /* success */
 
58
                }
 
59
                au_cache_free_dinfo(dinfo);
 
60
        }
 
61
        AuTraceErr(-ENOMEM);
 
62
        return -ENOMEM;
 
63
}
 
64
 
 
65
struct au_dinfo *au_di(struct dentry *dentry)
 
66
{
 
67
        struct au_dinfo *dinfo = dentry->d_fsdata;
 
68
        AuDebugOn(!dinfo
 
69
                 || !dinfo->di_hdentry
 
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 */))
 
76
                );
 
77
        return dinfo;
 
78
}
 
79
 
 
80
/* ---------------------------------------------------------------------- */
 
81
 
 
82
static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
 
83
{
 
84
        switch (lsc) {
 
85
        case AuLsc_DI_CHILD:
 
86
                ii_write_lock_child(inode);
 
87
                break;
 
88
        case AuLsc_DI_CHILD2:
 
89
                ii_write_lock_child2(inode);
 
90
                break;
 
91
        case AuLsc_DI_CHILD3:
 
92
                ii_write_lock_child3(inode);
 
93
                break;
 
94
        case AuLsc_DI_PARENT:
 
95
                ii_write_lock_parent(inode);
 
96
                break;
 
97
        case AuLsc_DI_PARENT2:
 
98
                ii_write_lock_parent2(inode);
 
99
                break;
 
100
        case AuLsc_DI_PARENT3:
 
101
                ii_write_lock_parent3(inode);
 
102
                break;
 
103
        case AuLsc_DI_PARENT4:
 
104
                ii_write_lock_parent4(inode);
 
105
                break;
 
106
        default:
 
107
                BUG();
 
108
        }
 
109
}
 
110
 
 
111
static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
 
112
{
 
113
        switch (lsc) {
 
114
        case AuLsc_DI_CHILD:
 
115
                ii_read_lock_child(inode);
 
116
                break;
 
117
        case AuLsc_DI_CHILD2:
 
118
                ii_read_lock_child2(inode);
 
119
                break;
 
120
        case AuLsc_DI_CHILD3:
 
121
                ii_read_lock_child3(inode);
 
122
                break;
 
123
        case AuLsc_DI_PARENT:
 
124
                ii_read_lock_parent(inode);
 
125
                break;
 
126
        case AuLsc_DI_PARENT2:
 
127
                ii_read_lock_parent2(inode);
 
128
                break;
 
129
        case AuLsc_DI_PARENT3:
 
130
                ii_read_lock_parent3(inode);
 
131
                break;
 
132
        case AuLsc_DI_PARENT4:
 
133
                ii_read_lock_parent4(inode);
 
134
                break;
 
135
        default:
 
136
                BUG();
 
137
        }
 
138
}
 
139
 
 
140
void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
 
141
{
 
142
        LKTRTrace("%.*s, %u\n", AuDLNPair(d), lsc);
 
143
 
 
144
        SiMustAnyLock(d->d_sb);
 
145
        /* todo: always nested? */
 
146
        au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
 
147
        if (d->d_inode) {
 
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);
 
152
        }
 
153
}
 
154
 
 
155
void di_read_unlock(struct dentry *d, int flags)
 
156
{
 
157
        LKTRTrace("%.*s\n", AuDLNPair(d));
 
158
 
 
159
        SiMustAnyLock(d->d_sb);
 
160
        if (d->d_inode) {
 
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);
 
165
        }
 
166
        au_rw_read_unlock(&au_di(d)->di_rwsem);
 
167
}
 
168
 
 
169
void di_downgrade_lock(struct dentry *d, int flags)
 
170
{
 
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);
 
175
}
 
176
 
 
177
void di_write_lock(struct dentry *d, unsigned int lsc)
 
178
{
 
179
        LKTRTrace("%.*s, %u\n", AuDLNPair(d), lsc);
 
180
 
 
181
        SiMustAnyLock(d->d_sb);
 
182
        /* todo: always nested? */
 
183
        au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
 
184
        if (d->d_inode)
 
185
                do_ii_write_lock(d->d_inode, lsc);
 
186
}
 
187
 
 
188
void di_write_unlock(struct dentry *d)
 
189
{
 
190
        LKTRTrace("%.*s\n", AuDLNPair(d));
 
191
 
 
192
        SiMustAnyLock(d->d_sb);
 
193
        if (d->d_inode)
 
194
                ii_write_unlock(d->d_inode);
 
195
        au_rw_write_unlock(&au_di(d)->di_rwsem);
 
196
}
 
197
 
 
198
void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
 
199
{
 
200
        AuTraceEnter();
 
201
        AuDebugOn(d1 == d2
 
202
                  || d1->d_inode == d2->d_inode
 
203
                  || d1->d_sb != d2->d_sb);
 
204
 
 
205
        if (isdir && au_test_subdir(d1, d2)) {
 
206
                di_write_lock_child(d1);
 
207
                di_write_lock_child2(d2);
 
208
        } else {
 
209
                /* there should be no races */
 
210
                di_write_lock_child(d2);
 
211
                di_write_lock_child2(d1);
 
212
        }
 
213
}
 
214
 
 
215
void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
 
216
{
 
217
        AuTraceEnter();
 
218
        AuDebugOn(d1 == d2
 
219
                  || d1->d_inode == d2->d_inode
 
220
                  || d1->d_sb != d2->d_sb);
 
221
 
 
222
        if (isdir && au_test_subdir(d1, d2)) {
 
223
                di_write_lock_parent(d1);
 
224
                di_write_lock_parent2(d2);
 
225
        } else {
 
226
                /* there should be no races */
 
227
                di_write_lock_parent(d2);
 
228
                di_write_lock_parent2(d1);
 
229
        }
 
230
}
 
231
 
 
232
void di_write_unlock2(struct dentry *d1, struct dentry *d2)
 
233
{
 
234
        di_write_unlock(d1);
 
235
        if (d1->d_inode == d2->d_inode)
 
236
                au_rw_write_unlock(&au_di(d2)->di_rwsem);
 
237
        else
 
238
                di_write_unlock(d2);
 
239
}
 
240
 
 
241
/* ---------------------------------------------------------------------- */
 
242
 
 
243
struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
 
244
{
 
245
        struct dentry *d;
 
246
 
 
247
        DiMustAnyLock(dentry);
 
248
        if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
 
249
                return NULL;
 
250
        AuDebugOn(bindex < 0
 
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));
 
254
        return d;
 
255
}
 
256
 
 
257
aufs_bindex_t au_dbtail(struct dentry *dentry)
 
258
{
 
259
        aufs_bindex_t bend, bwh;
 
260
 
 
261
        bend = au_dbend(dentry);
 
262
        if (0 <= bend) {
 
263
                bwh = au_dbwh(dentry);
 
264
                if (!bwh)
 
265
                        return bwh;
 
266
                if (0 < bwh && bwh < bend)
 
267
                        return bwh - 1;
 
268
        }
 
269
        return bend;
 
270
}
 
271
 
 
272
aufs_bindex_t au_dbtaildir(struct dentry *dentry)
 
273
{
 
274
        aufs_bindex_t bend, bopq;
 
275
 
 
276
        AuDebugOn(dentry->d_inode
 
277
                  && dentry->d_inode->i_mode
 
278
                  && !S_ISDIR(dentry->d_inode->i_mode));
 
279
 
 
280
        bend = au_dbtail(dentry);
 
281
        if (0 <= bend) {
 
282
                bopq = au_dbdiropq(dentry);
 
283
                AuDebugOn(bend < bopq);
 
284
                if (0 <= bopq && bopq < bend)
 
285
                        bend = bopq;
 
286
        }
 
287
        return bend;
 
288
}
 
289
 
 
290
#if 0 /* reserved for future use */
 
291
aufs_bindex_t au_dbtail_generic(struct dentry *dentry)
 
292
{
 
293
        struct inode *inode;
 
294
 
 
295
        inode = dentry->d_inode;
 
296
        if (inode && S_ISDIR(inode->i_mode))
 
297
                return au_dbtaildir(dentry);
 
298
        else
 
299
                return au_dbtail(dentry);
 
300
}
 
301
#endif
 
302
 
 
303
/* ---------------------------------------------------------------------- */
 
304
 
 
305
void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
 
306
{
 
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))
 
312
                  || (dentry->d_inode
 
313
                      && dentry->d_inode->i_mode
 
314
                      && !S_ISDIR(dentry->d_inode->i_mode)));
 
315
        au_di(dentry)->di_bdiropq = bindex;
 
316
}
 
317
 
 
318
void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
 
319
                   struct dentry *h_dentry)
 
320
{
 
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)
 
327
                );
 
328
        if (hd->hd_dentry)
 
329
                au_hdput(hd, /*do_free*/0);
 
330
        hd->hd_dentry = h_dentry;
 
331
}
 
332
 
 
333
/* ---------------------------------------------------------------------- */
 
334
 
 
335
void au_update_dbrange(struct dentry *dentry, int do_put_zero)
 
336
{
 
337
        struct au_dinfo *dinfo;
 
338
        aufs_bindex_t bindex;
 
339
        struct dentry *h_d;
 
340
 
 
341
        LKTRTrace("%.*s, %d\n", AuDLNPair(dentry), do_put_zero);
 
342
        DiMustWriteLock(dentry);
 
343
 
 
344
        dinfo = au_di(dentry);
 
345
        if (unlikely(!dinfo) || dinfo->di_bstart < 0)
 
346
                return;
 
347
 
 
348
        if (do_put_zero) {
 
349
                for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend;
 
350
                     bindex++) {
 
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);
 
354
                }
 
355
        }
 
356
 
 
357
        dinfo->di_bstart = -1;
 
358
        while (++dinfo->di_bstart <= dinfo->di_bend)
 
359
                if (dinfo->di_hdentry[0 + dinfo->di_bstart].hd_dentry)
 
360
                        break;
 
361
        if (dinfo->di_bstart > dinfo->di_bend) {
 
362
                dinfo->di_bstart = -1;
 
363
                dinfo->di_bend = -1;
 
364
                return;
 
365
        }
 
366
 
 
367
        dinfo->di_bend++;
 
368
        while (0 <= --dinfo->di_bend)
 
369
                if (dinfo->di_hdentry[0 + dinfo->di_bend].hd_dentry)
 
370
                        break;
 
371
        AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
 
372
}
 
373
 
 
374
void au_update_dbstart(struct dentry *dentry)
 
375
{
 
376
        aufs_bindex_t bindex,
 
377
                bstart = au_dbstart(dentry),
 
378
                bend = au_dbend(dentry);
 
379
        struct dentry *h_dentry;
 
380
 
 
381
        LKTRTrace("%.*s\n", AuDLNPair(dentry));
 
382
        DiMustWriteLock(dentry);
 
383
 
 
384
        for (bindex = bstart; bindex <= bend; bindex++) {
 
385
                h_dentry = au_h_dptr(dentry, bindex);
 
386
                if (!h_dentry)
 
387
                        continue;
 
388
                if (h_dentry->d_inode) {
 
389
                        au_set_dbstart(dentry, bindex);
 
390
                        return;
 
391
                }
 
392
                au_set_h_dptr(dentry, bindex, NULL);
 
393
        }
 
394
}
 
395
 
 
396
void au_update_dbend(struct dentry *dentry)
 
397
{
 
398
        aufs_bindex_t bindex,
 
399
                bstart = au_dbstart(dentry),
 
400
                bend = au_dbend(dentry);
 
401
        struct dentry *h_dentry;
 
402
 
 
403
        DiMustWriteLock(dentry);
 
404
        for (bindex = bend; bindex <= bstart; bindex--) {
 
405
                h_dentry = au_h_dptr(dentry, bindex);
 
406
                if (!h_dentry)
 
407
                        continue;
 
408
                if (h_dentry->d_inode) {
 
409
                        au_set_dbend(dentry, bindex);
 
410
                        return;
 
411
                }
 
412
                au_set_h_dptr(dentry, bindex, NULL);
 
413
        }
 
414
}
 
415
 
 
416
int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
 
417
{
 
418
        aufs_bindex_t bindex, bend;
 
419
 
 
420
        bend = au_dbend(dentry);
 
421
        for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
 
422
                if (au_h_dptr(dentry, bindex) == h_dentry)
 
423
                        return bindex;
 
424
        return -1;
 
425
}