~ubuntu-branches/ubuntu/quantal/aufs/quantal

« back to all changes in this revision

Viewing changes to fs/aufs/dinfo.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2007-05-09 15:29:28 UTC
  • Revision ID: james.westby@ubuntu.com-20070509152928-4sywrmkifvz0bq02
Tags: upstream-0+20070509
ImportĀ upstreamĀ versionĀ 0+20070509

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005, 2006, 2007 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
/* $Id: dinfo.c,v 1.23 2007/05/07 03:43:36 sfjro Exp $ */
 
20
 
 
21
#include "aufs.h"
 
22
 
 
23
int au_alloc_dinfo(struct dentry *dentry)
 
24
{
 
25
        struct aufs_dinfo *dinfo;
 
26
        struct super_block *sb;
 
27
        int nbr;
 
28
 
 
29
        LKTRTrace("%.*s\n", DLNPair(dentry));
 
30
        DEBUG_ON(dentry->d_fsdata);
 
31
 
 
32
        dinfo = cache_alloc_dinfo();
 
33
        //if (LktrCond) {cache_free_dinfo(dinfo); dinfo = NULL;}
 
34
        if (dinfo) {
 
35
                sb = dentry->d_sb;
 
36
                nbr = sbend(sb) + 1;
 
37
                if (unlikely(!nbr))
 
38
                        nbr++;
 
39
                dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry),
 
40
                                            GFP_KERNEL);
 
41
                //if (LktrCond)
 
42
                //{kfree(dinfo->di_hdentry); dinfo->di_hdentry = NULL;}
 
43
                if (dinfo->di_hdentry) {
 
44
                        rw_init_wlock_nested(&dinfo->di_rwsem, AuLsc_DI_PARENT);
 
45
                        dinfo->di_bstart = dinfo->di_bend = -1;
 
46
                        dinfo->di_bwh = dinfo->di_bdiropq = -1;
 
47
                        atomic_set(&dinfo->di_generation, au_sigen(sb));
 
48
 
 
49
                        dentry->d_fsdata = dinfo;
 
50
                        dentry->d_op = &aufs_dop;
 
51
                        return 0; /* success */
 
52
                }
 
53
                cache_free_dinfo(dinfo);
 
54
        }
 
55
        TraceErr(-ENOMEM);
 
56
        return -ENOMEM;
 
57
}
 
58
 
 
59
struct aufs_dinfo *dtodi(struct dentry *dentry)
 
60
{
 
61
        struct aufs_dinfo *dinfo = dentry->d_fsdata;
 
62
        DEBUG_ON(!dinfo
 
63
                 || !dinfo->di_hdentry
 
64
                 /* || stosi(dentry->d_sb)->si_bend < dinfo->di_bend */
 
65
                 || dinfo->di_bend < dinfo->di_bstart
 
66
                 /* dbwh can be outside of this range */
 
67
                 || (0 <= dinfo->di_bdiropq
 
68
                     && (dinfo->di_bdiropq < dinfo->di_bstart
 
69
                         /* || dinfo->di_bend < dinfo->di_bdiropq */))
 
70
                );
 
71
        return dinfo;
 
72
}
 
73
 
 
74
/* ---------------------------------------------------------------------- */
 
75
 
 
76
static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
 
77
{
 
78
        switch (lsc) {
 
79
        case AuLsc_DI_CHILD:
 
80
                ii_write_lock_child(inode);
 
81
                break;
 
82
        case AuLsc_DI_CHILD2:
 
83
                ii_write_lock_child2(inode);
 
84
                break;
 
85
        case AuLsc_DI_CHILD3:
 
86
                ii_write_lock_child3(inode);
 
87
                break;
 
88
        case AuLsc_DI_PARENT:
 
89
                ii_write_lock_parent(inode);
 
90
                break;
 
91
        case AuLsc_DI_PARENT2:
 
92
                ii_write_lock_parent2(inode);
 
93
                break;
 
94
        case AuLsc_DI_PARENT3:
 
95
                ii_write_lock_parent3(inode);
 
96
                break;
 
97
        default:
 
98
                BUG();
 
99
        }
 
100
}
 
101
 
 
102
static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
 
103
{
 
104
        switch (lsc) {
 
105
        case AuLsc_DI_CHILD:
 
106
                ii_read_lock_child(inode);
 
107
                break;
 
108
        case AuLsc_DI_CHILD2:
 
109
                ii_read_lock_child2(inode);
 
110
                break;
 
111
        case AuLsc_DI_CHILD3:
 
112
                ii_read_lock_child3(inode);
 
113
                break;
 
114
        case AuLsc_DI_PARENT:
 
115
                ii_read_lock_parent(inode);
 
116
                break;
 
117
        case AuLsc_DI_PARENT2:
 
118
                ii_read_lock_parent2(inode);
 
119
                break;
 
120
        case AuLsc_DI_PARENT3:
 
121
                ii_read_lock_parent3(inode);
 
122
                break;
 
123
        default:
 
124
                BUG();
 
125
        }
 
126
}
 
127
 
 
128
void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
 
129
{
 
130
        SiMustAnyLock(d->d_sb);
 
131
        // todo: always nested?
 
132
        rw_read_lock_nested(&dtodi(d)->di_rwsem, lsc);
 
133
        if (d->d_inode) {
 
134
                if (flags & AUFS_I_WLOCK)
 
135
                        do_ii_write_lock(d->d_inode, lsc);
 
136
                else if (flags & AUFS_I_RLOCK)
 
137
                        do_ii_read_lock(d->d_inode, lsc);
 
138
        }
 
139
}
 
140
 
 
141
void di_read_unlock(struct dentry *d, int flags)
 
142
{
 
143
        SiMustAnyLock(d->d_sb);
 
144
        if (d->d_inode) {
 
145
                if (flags & AUFS_I_WLOCK)
 
146
                        ii_write_unlock(d->d_inode);
 
147
                else if (flags & AUFS_I_RLOCK)
 
148
                        ii_read_unlock(d->d_inode);
 
149
        }
 
150
        rw_read_unlock(&dtodi(d)->di_rwsem);
 
151
}
 
152
 
 
153
void di_downgrade_lock(struct dentry *d, int flags)
 
154
{
 
155
        SiMustAnyLock(d->d_sb);
 
156
        rw_dgrade_lock(&dtodi(d)->di_rwsem);
 
157
        if (d->d_inode && (flags & AUFS_I_RLOCK))
 
158
                ii_downgrade_lock(d->d_inode);
 
159
}
 
160
 
 
161
void di_write_lock(struct dentry *d, unsigned int lsc)
 
162
{
 
163
        SiMustAnyLock(d->d_sb);
 
164
        // todo: always nested?
 
165
        rw_write_lock_nested(&dtodi(d)->di_rwsem, lsc);
 
166
        if (d->d_inode)
 
167
                do_ii_write_lock(d->d_inode, lsc);
 
168
}
 
169
 
 
170
void di_write_unlock(struct dentry *d)
 
171
{
 
172
        SiMustAnyLock(d->d_sb);
 
173
        if (d->d_inode)
 
174
                ii_write_unlock(d->d_inode);
 
175
        rw_write_unlock(&dtodi(d)->di_rwsem);
 
176
}
 
177
 
 
178
void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
 
179
{
 
180
        struct dentry *d;
 
181
 
 
182
        TraceEnter();
 
183
        DEBUG_ON(d1 == d2
 
184
                 || d1->d_inode == d2->d_inode
 
185
                 || d1->d_sb != d2->d_sb);
 
186
 
 
187
        if (isdir)
 
188
                for (d = d1; d->d_parent != d; d = d->d_parent) // dget_parent()
 
189
                        if (d->d_parent == d2) {
 
190
                                di_write_lock_child(d1);
 
191
                                di_write_lock_child2(d2);
 
192
                                return;
 
193
                        }
 
194
 
 
195
        di_write_lock_child(d2);
 
196
        di_write_lock_child2(d1);
 
197
}
 
198
 
 
199
void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
 
200
{
 
201
        struct dentry *d;
 
202
 
 
203
        TraceEnter();
 
204
        DEBUG_ON(d1 == d2
 
205
                 || d1->d_inode == d2->d_inode
 
206
                 || d1->d_sb != d2->d_sb);
 
207
 
 
208
        if (isdir)
 
209
                for (d = d1; d->d_parent != d; d = d->d_parent) // dget_parent()
 
210
                        if (d->d_parent == d2) {
 
211
                                di_write_lock_parent(d1);
 
212
                                di_write_lock_parent2(d2);
 
213
                                return;
 
214
                        }
 
215
 
 
216
        di_write_lock_parent(d2);
 
217
        di_write_lock_parent2(d1);
 
218
}
 
219
 
 
220
void di_write_unlock2(struct dentry *d1, struct dentry *d2)
 
221
{
 
222
        di_write_unlock(d1);
 
223
        if (d1->d_inode == d2->d_inode)
 
224
                rw_write_unlock(&dtodi(d2)->di_rwsem);
 
225
        else
 
226
                di_write_unlock(d2);
 
227
}
 
228
 
 
229
/* ---------------------------------------------------------------------- */
 
230
 
 
231
aufs_bindex_t dbstart(struct dentry *dentry)
 
232
{
 
233
        DiMustAnyLock(dentry);
 
234
        return dtodi(dentry)->di_bstart;
 
235
}
 
236
 
 
237
aufs_bindex_t dbend(struct dentry *dentry)
 
238
{
 
239
        DiMustAnyLock(dentry);
 
240
        return dtodi(dentry)->di_bend;
 
241
}
 
242
 
 
243
aufs_bindex_t dbwh(struct dentry *dentry)
 
244
{
 
245
        DiMustAnyLock(dentry);
 
246
        return dtodi(dentry)->di_bwh;
 
247
}
 
248
 
 
249
aufs_bindex_t dbdiropq(struct dentry *dentry)
 
250
{
 
251
        DiMustAnyLock(dentry);
 
252
        DEBUG_ON(dentry->d_inode
 
253
                 && dentry->d_inode->i_mode
 
254
                 && !S_ISDIR(dentry->d_inode->i_mode));
 
255
        return dtodi(dentry)->di_bdiropq;
 
256
}
 
257
 
 
258
struct dentry *au_h_dptr_i(struct dentry *dentry, aufs_bindex_t bindex)
 
259
{
 
260
        struct dentry *d;
 
261
 
 
262
        DiMustAnyLock(dentry);
 
263
        if (dbstart(dentry) < 0 || bindex < dbstart(dentry))
 
264
                return NULL;
 
265
        DEBUG_ON(bindex < 0
 
266
                 /* || bindex > sbend(dentry->d_sb) */);
 
267
        d = dtodi(dentry)->di_hdentry[0 + bindex].hd_dentry;
 
268
        DEBUG_ON(d && (atomic_read(&d->d_count) <= 0));
 
269
        return d;
 
270
}
 
271
 
 
272
struct dentry *au_h_dptr(struct dentry *dentry)
 
273
{
 
274
        return au_h_dptr_i(dentry, dbstart(dentry));
 
275
}
 
276
 
 
277
aufs_bindex_t dbtail(struct dentry *dentry)
 
278
{
 
279
        aufs_bindex_t bend, bwh;
 
280
 
 
281
        bend = dbend(dentry);
 
282
        if (0 <= bend) {
 
283
                bwh = dbwh(dentry);
 
284
                //DEBUG_ON(bend < bwh);
 
285
                if (!bwh)
 
286
                        return bwh;
 
287
                if (0 < bwh && bwh < bend)
 
288
                        return bwh - 1;
 
289
        }
 
290
        return bend;
 
291
}
 
292
 
 
293
aufs_bindex_t dbtaildir(struct dentry *dentry)
 
294
{
 
295
        aufs_bindex_t bend, bopq;
 
296
 
 
297
        DEBUG_ON(dentry->d_inode
 
298
                 && dentry->d_inode->i_mode
 
299
                 && !S_ISDIR(dentry->d_inode->i_mode));
 
300
 
 
301
        bend = dbtail(dentry);
 
302
        if (0 <= bend) {
 
303
                bopq = dbdiropq(dentry);
 
304
                DEBUG_ON(bend < bopq);
 
305
                if (0 <= bopq && bopq < bend)
 
306
                        bend = bopq;
 
307
        }
 
308
        return bend;
 
309
}
 
310
 
 
311
aufs_bindex_t dbtail_generic(struct dentry *dentry)
 
312
{
 
313
        struct inode *inode;
 
314
 
 
315
        inode = dentry->d_inode;
 
316
        if (inode && S_ISDIR(inode->i_mode))
 
317
                return dbtaildir(dentry);
 
318
        else
 
319
                return dbtail(dentry);
 
320
}
 
321
 
 
322
/* ---------------------------------------------------------------------- */
 
323
 
 
324
// hard/soft set
 
325
void set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
 
326
{
 
327
        DiMustWriteLock(dentry);
 
328
        DEBUG_ON(sbend(dentry->d_sb) < bindex);
 
329
        /* */
 
330
        dtodi(dentry)->di_bstart = bindex;
 
331
}
 
332
 
 
333
void set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
 
334
{
 
335
        DiMustWriteLock(dentry);
 
336
        DEBUG_ON(sbend(dentry->d_sb) < bindex
 
337
                 || bindex < dbstart(dentry));
 
338
        dtodi(dentry)->di_bend = bindex;
 
339
}
 
340
 
 
341
void set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
 
342
{
 
343
        DiMustWriteLock(dentry);
 
344
        DEBUG_ON(sbend(dentry->d_sb) < bindex);
 
345
        /* dbwh can be outside of bstart - bend range */
 
346
        dtodi(dentry)->di_bwh = bindex;
 
347
}
 
348
 
 
349
void set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
 
350
{
 
351
        DiMustWriteLock(dentry);
 
352
        DEBUG_ON(sbend(dentry->d_sb) < bindex);
 
353
        DEBUG_ON((bindex != -1
 
354
                  && (bindex < dbstart(dentry) || dbend(dentry) < bindex))
 
355
                 || (dentry->d_inode
 
356
                     && dentry->d_inode->i_mode
 
357
                     && !S_ISDIR(dentry->d_inode->i_mode)));
 
358
        dtodi(dentry)->di_bdiropq = bindex;
 
359
}
 
360
 
 
361
void hdput(struct aufs_hdentry *hd)
 
362
{
 
363
        dput(hd->hd_dentry);
 
364
}
 
365
 
 
366
void set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
 
367
                struct dentry *h_dentry)
 
368
{
 
369
        struct aufs_hdentry *hd = dtodi(dentry)->di_hdentry + bindex;
 
370
        DiMustWriteLock(dentry);
 
371
        DEBUG_ON(bindex < dtodi(dentry)->di_bstart
 
372
                 || bindex > dtodi(dentry)->di_bend
 
373
                 || (h_dentry && atomic_read(&h_dentry->d_count) <= 0)
 
374
                 || (h_dentry && hd->hd_dentry)
 
375
                );
 
376
        if (hd->hd_dentry)
 
377
                hdput(hd);
 
378
        hd->hd_dentry = h_dentry;
 
379
}
 
380
 
 
381
/* ---------------------------------------------------------------------- */
 
382
 
 
383
void au_update_digen(struct dentry *dentry)
 
384
{
 
385
        //DiMustWriteLock(dentry);
 
386
        DEBUG_ON(!dentry->d_sb);
 
387
        atomic_set(&dtodi(dentry)->di_generation, au_sigen(dentry->d_sb));
 
388
}
 
389
 
 
390
void au_update_dbstart(struct dentry *dentry)
 
391
{
 
392
        aufs_bindex_t bindex, bstart = dbstart(dentry), bend = dbend(dentry);
 
393
        struct dentry *hidden_dentry;
 
394
 
 
395
        DiMustWriteLock(dentry);
 
396
        for (bindex = bstart; bindex <= bend; bindex++) {
 
397
                hidden_dentry = au_h_dptr_i(dentry, bindex);
 
398
                if (!hidden_dentry)
 
399
                        continue;
 
400
                if (hidden_dentry->d_inode) {
 
401
                        set_dbstart(dentry, bindex);
 
402
                        return;
 
403
                }
 
404
                set_h_dptr(dentry, bindex, NULL);
 
405
        }
 
406
        //set_dbstart(dentry, -1);
 
407
        //set_dbend(dentry, -1);
 
408
}
 
409
 
 
410
int au_find_dbindex(struct dentry *dentry, struct dentry *hidden_dentry)
 
411
{
 
412
        aufs_bindex_t bindex, bend;
 
413
 
 
414
        bend = dbend(dentry);
 
415
        for (bindex = dbstart(dentry); bindex <= bend; bindex++)
 
416
                if (au_h_dptr_i(dentry, bindex) == hidden_dentry)
 
417
                        return bindex;
 
418
        return -1;
 
419
}