~ubuntu-branches/ubuntu/raring/aufs/raring

« back to all changes in this revision

Viewing changes to fs/aufs25/plink.c

  • Committer: Bazaar Package Importer
  • Author(s): Julian Andres Klode
  • Date: 2008-05-06 18:35:50 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080506183550-0b6c974kkgc46oeh
Tags: 0+20080506-1
* New upstream release, supports Kernel 2.6.25 (Closes: #479717)
* Fix building with older Kernels (Closes: #475042)
* Update the patches 01, 04 and 07 to also patch fs/aufs25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-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
 * pseudo-link
 
21
 *
 
22
 * $Id: plink.c,v 1.2 2008/04/21 01:45:16 sfjro Exp $
 
23
 */
 
24
 
 
25
#include "aufs.h"
 
26
 
 
27
struct pseudo_link {
 
28
        struct list_head list;
 
29
        struct inode *inode;
 
30
};
 
31
 
 
32
#ifdef CONFIG_AUFS_DEBUG
 
33
void au_plink_list(struct super_block *sb)
 
34
{
 
35
        struct au_sbinfo *sbinfo;
 
36
        struct list_head *plink_list;
 
37
        struct pseudo_link *plink;
 
38
 
 
39
        AuTraceEnter();
 
40
        SiMustAnyLock(sb);
 
41
        sbinfo = au_sbi(sb);
 
42
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
43
 
 
44
        plink_list = &sbinfo->si_plink;
 
45
        spin_lock(&sbinfo->si_plink_lock);
 
46
        list_for_each_entry(plink, plink_list, list)
 
47
                AuDbg("%lu\n", plink->inode->i_ino);
 
48
        spin_unlock(&sbinfo->si_plink_lock);
 
49
}
 
50
#endif
 
51
 
 
52
int au_plink_test(struct super_block *sb, struct inode *inode)
 
53
{
 
54
        int found;
 
55
        struct au_sbinfo *sbinfo;
 
56
        struct list_head *plink_list;
 
57
        struct pseudo_link *plink;
 
58
 
 
59
        LKTRTrace("i%lu\n", inode->i_ino);
 
60
        SiMustAnyLock(sb);
 
61
        sbinfo = au_sbi(sb);
 
62
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
63
 
 
64
        found = 0;
 
65
        plink_list = &sbinfo->si_plink;
 
66
        spin_lock(&sbinfo->si_plink_lock);
 
67
        list_for_each_entry(plink, plink_list, list)
 
68
                if (plink->inode == inode) {
 
69
                        found = 1;
 
70
                        break;
 
71
                }
 
72
        spin_unlock(&sbinfo->si_plink_lock);
 
73
        return found;
 
74
}
 
75
 
 
76
/* 20 is max digits length of ulong 64 */
 
77
#define PLINK_NAME_LEN  ((20 + 1) * 2)
 
78
 
 
79
static int plink_name(char *name, int len, struct inode *inode,
 
80
                      aufs_bindex_t bindex)
 
81
{
 
82
        int rlen;
 
83
        struct inode *h_inode;
 
84
 
 
85
        LKTRTrace("i%lu, b%d\n", inode->i_ino, bindex);
 
86
        AuDebugOn(len != PLINK_NAME_LEN);
 
87
        h_inode = au_h_iptr(inode, bindex);
 
88
        AuDebugOn(!h_inode);
 
89
        rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
 
90
        AuDebugOn(rlen >= len);
 
91
        return rlen;
 
92
}
 
93
 
 
94
struct dentry *au_plink_lkup(struct super_block *sb, aufs_bindex_t bindex,
 
95
                             struct inode *inode)
 
96
{
 
97
        struct dentry *h_dentry, *h_parent;
 
98
        struct au_branch *br;
 
99
        struct inode *h_dir;
 
100
        char tgtname[PLINK_NAME_LEN];
 
101
        int len;
 
102
        struct au_ndx ndx = {
 
103
                .flags  = 0,
 
104
                .nd     = NULL,
 
105
                //.br   = NULL
 
106
        };
 
107
 
 
108
        LKTRTrace("b%d, i%lu\n", bindex, inode->i_ino);
 
109
        br = au_sbr(sb, bindex);
 
110
        h_parent = br->br_plink;
 
111
        AuDebugOn(!h_parent);
 
112
        h_dir = h_parent->d_inode;
 
113
        AuDebugOn(!h_dir);
 
114
 
 
115
        len = plink_name(tgtname, sizeof(tgtname), inode, bindex);
 
116
 
 
117
        /* always superio. */
 
118
        ndx.nfsmnt = au_do_nfsmnt(br->br_mnt);
 
119
#if 0
 
120
        if (unlikely(au_opt_test_dlgt(au_mntflags(sb))))
 
121
                au_fset_ndx(ndx.flags, DLGT);
 
122
#endif
 
123
        mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
 
124
        h_dentry = au_sio_lkup_one(tgtname, h_parent, len, &ndx);
 
125
        mutex_unlock(&h_dir->i_mutex);
 
126
        return h_dentry;
 
127
}
 
128
 
 
129
static int do_whplink(char *tgt, int len, struct dentry *h_parent,
 
130
                      struct dentry *h_dentry, struct vfsmount *nfsmnt,
 
131
                      struct super_block *sb)
 
132
{
 
133
        int err, dlgt;
 
134
        struct dentry *h_tgt;
 
135
        struct inode *h_dir;
 
136
        struct vfsub_args vargs;
 
137
        struct au_ndx ndx = {
 
138
                .nfsmnt = nfsmnt,
 
139
                .flags  = 0,
 
140
                .nd     = NULL,
 
141
                //.br   = NULL
 
142
        };
 
143
 
 
144
        dlgt = !!au_opt_test_dlgt(au_mntflags(sb));
 
145
        if (unlikely(dlgt))
 
146
                au_fset_ndx(ndx.flags, DLGT);
 
147
        h_tgt = au_lkup_one(tgt, h_parent, len, &ndx);
 
148
        err = PTR_ERR(h_tgt);
 
149
        if (IS_ERR(h_tgt))
 
150
                goto out;
 
151
 
 
152
        err = 0;
 
153
        vfsub_args_init(&vargs, NULL, dlgt, 0);
 
154
        h_dir = h_parent->d_inode;
 
155
        if (unlikely(h_tgt->d_inode && h_tgt->d_inode != h_dentry->d_inode))
 
156
                err = vfsub_unlink(h_dir, h_tgt, &vargs);
 
157
        if (!err && !h_tgt->d_inode) {
 
158
                err = vfsub_link(h_dentry, h_dir, h_tgt, dlgt);
 
159
                //inode->i_nlink++;
 
160
        }
 
161
        dput(h_tgt);
 
162
 
 
163
 out:
 
164
        AuTraceErr(err);
 
165
        return err;
 
166
}
 
167
 
 
168
struct do_whplink_args {
 
169
        int *errp;
 
170
        char *tgt;
 
171
        int len;
 
172
        struct dentry *h_parent;
 
173
        struct dentry *h_dentry;
 
174
        struct vfsmount *nfsmnt;
 
175
        struct super_block *sb;
 
176
};
 
177
 
 
178
static void call_do_whplink(void *args)
 
179
{
 
180
        struct do_whplink_args *a = args;
 
181
        *a->errp = do_whplink(a->tgt, a->len, a->h_parent, a->h_dentry,
 
182
                              a->nfsmnt, a->sb);
 
183
}
 
184
 
 
185
static int whplink(struct dentry *h_dentry, struct inode *inode,
 
186
                   aufs_bindex_t bindex, struct super_block *sb)
 
187
{
 
188
        int err, len, wkq_err;
 
189
        struct au_branch *br;
 
190
        struct dentry *h_parent;
 
191
        struct inode *h_dir;
 
192
        char tgtname[PLINK_NAME_LEN];
 
193
 
 
194
        LKTRTrace("%.*s\n", AuDLNPair(h_dentry));
 
195
        br = au_sbr(inode->i_sb, bindex);
 
196
        h_parent = br->br_plink;
 
197
        AuDebugOn(!h_parent);
 
198
        h_dir = h_parent->d_inode;
 
199
        AuDebugOn(!h_dir);
 
200
 
 
201
        len = plink_name(tgtname, sizeof(tgtname), inode, bindex);
 
202
 
 
203
        /* always superio. */
 
204
        mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
 
205
        if (!au_test_wkq(current)) {
 
206
                struct do_whplink_args args = {
 
207
                        .errp           = &err,
 
208
                        .tgt            = tgtname,
 
209
                        .len            = len,
 
210
                        .h_parent       = h_parent,
 
211
                        .h_dentry       = h_dentry,
 
212
                        .nfsmnt         = au_do_nfsmnt(br->br_mnt),
 
213
                        .sb             = sb
 
214
                };
 
215
                wkq_err = au_wkq_wait(call_do_whplink, &args, /*dlgt*/0);
 
216
                if (unlikely(wkq_err))
 
217
                        err = wkq_err;
 
218
        } else
 
219
                err = do_whplink(tgtname, len, h_parent, h_dentry,
 
220
                                 au_do_nfsmnt(br->br_mnt), sb);
 
221
        mutex_unlock(&h_dir->i_mutex);
 
222
 
 
223
        AuTraceErr(err);
 
224
        return err;
 
225
}
 
226
 
 
227
void au_plink_append(struct super_block *sb, struct inode *inode,
 
228
                     struct dentry *h_dentry, aufs_bindex_t bindex)
 
229
{
 
230
        struct au_sbinfo *sbinfo;
 
231
        struct list_head *plink_list;
 
232
        struct pseudo_link *plink;
 
233
        int found, err, cnt;
 
234
 
 
235
        LKTRTrace("i%lu\n", inode->i_ino);
 
236
        SiMustAnyLock(sb);
 
237
        sbinfo = au_sbi(sb);
 
238
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
239
 
 
240
        cnt = 0;
 
241
        found = 0;
 
242
        plink_list = &sbinfo->si_plink;
 
243
        spin_lock(&sbinfo->si_plink_lock);
 
244
        list_for_each_entry(plink, plink_list, list) {
 
245
                cnt++;
 
246
                if (plink->inode == inode) {
 
247
                        found = 1;
 
248
                        break;
 
249
                }
 
250
        }
 
251
 
 
252
        err = 0;
 
253
        if (!found) {
 
254
                plink = kmalloc(sizeof(*plink), GFP_ATOMIC);
 
255
                if (plink) {
 
256
                        plink->inode = igrab(inode);
 
257
                        list_add(&plink->list, plink_list);
 
258
                        cnt++;
 
259
                } else
 
260
                        err = -ENOMEM;
 
261
        }
 
262
        spin_unlock(&sbinfo->si_plink_lock);
 
263
 
 
264
        if (!err)
 
265
                err = whplink(h_dentry, inode, bindex, sb);
 
266
 
 
267
        if (unlikely(cnt > AUFS_PLINK_WARN))
 
268
                AuWarn1("unexpectedly many pseudo links, %d\n", cnt);
 
269
        if (unlikely(err))
 
270
                AuWarn("err %d, damaged pseudo link. ignored.\n", err);
 
271
}
 
272
 
 
273
static void do_put_plink(struct pseudo_link *plink, int do_del)
 
274
{
 
275
        AuTraceEnter();
 
276
 
 
277
        iput(plink->inode);
 
278
        if (do_del)
 
279
                list_del(&plink->list);
 
280
        kfree(plink);
 
281
}
 
282
 
 
283
void au_plink_put(struct super_block *sb)
 
284
{
 
285
        struct au_sbinfo *sbinfo;
 
286
        struct list_head *plink_list;
 
287
        struct pseudo_link *plink, *tmp;
 
288
 
 
289
        AuTraceEnter();
 
290
        SiMustWriteLock(sb);
 
291
        sbinfo = au_sbi(sb);
 
292
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
293
 
 
294
        plink_list = &sbinfo->si_plink;
 
295
        //spin_lock(&sbinfo->si_plink_lock);
 
296
        list_for_each_entry_safe(plink, tmp, plink_list, list)
 
297
                do_put_plink(plink, 0);
 
298
        INIT_LIST_HEAD(plink_list);
 
299
        //spin_unlock(&sbinfo->si_plink_lock);
 
300
}
 
301
 
 
302
void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
 
303
{
 
304
        struct au_sbinfo *sbinfo;
 
305
        struct list_head *plink_list;
 
306
        struct pseudo_link *plink, *tmp;
 
307
        struct inode *inode;
 
308
        aufs_bindex_t bstart, bend, bindex;
 
309
        int do_put;
 
310
 
 
311
        AuTraceEnter();
 
312
        SiMustWriteLock(sb);
 
313
        sbinfo = au_sbi(sb);
 
314
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
315
 
 
316
        plink_list = &sbinfo->si_plink;
 
317
        //spin_lock(&sbinfo->si_plink_lock);
 
318
        list_for_each_entry_safe(plink, tmp, plink_list, list) {
 
319
                do_put = 0;
 
320
                inode = igrab(plink->inode);
 
321
                ii_write_lock_child(inode);
 
322
                bstart = au_ibstart(inode);
 
323
                bend = au_ibend(inode);
 
324
                if (bstart >= 0) {
 
325
                        for (bindex = bstart; bindex <= bend; bindex++) {
 
326
                                if (!au_h_iptr(inode, bindex)
 
327
                                    || au_ii_br_id(inode, bindex) != br_id)
 
328
                                        continue;
 
329
                                au_set_h_iptr(inode, bindex, NULL, 0);
 
330
                                do_put = 1;
 
331
                                break;
 
332
                        }
 
333
                } else
 
334
                        do_put_plink(plink, 1);
 
335
 
 
336
                if (do_put) {
 
337
                        for (bindex = bstart; bindex <= bend; bindex++)
 
338
                                if (au_h_iptr(inode, bindex)) {
 
339
                                        do_put = 0;
 
340
                                        break;
 
341
                                }
 
342
                        if (do_put)
 
343
                                do_put_plink(plink, 1);
 
344
                }
 
345
                ii_write_unlock(inode);
 
346
                iput(inode);
 
347
        }
 
348
        //spin_unlock(&sbinfo->si_plink_lock);
 
349
}