~ubuntu-branches/ubuntu/precise/linux-ti-omap/precise

« back to all changes in this revision

Viewing changes to ubuntu/aufs/plink.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Bader, Amit Kucheria
  • Date: 2010-03-23 18:05:12 UTC
  • Revision ID: james.westby@ubuntu.com-20100323180512-iavj906ocnphdubp
Tags: 2.6.33-500.3
[ Amit Kucheria ]

* [Config] Fix the debug package name to end in -dbgsym
* SAUCE: Add the ubuntu/ drivers to omap
* SAUCE: Re-export the symbols for aufs
* [Config] Enable AUFS and COMPCACHE

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005-2009 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, 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
 
 
23
#include "aufs.h"
 
24
 
 
25
/*
 
26
 * during a user process maintains the pseudo-links,
 
27
 * prohibit adding a new plink and branch manipulation.
 
28
 */
 
29
void au_plink_block_maintain(struct super_block *sb)
 
30
{
 
31
        struct au_sbinfo *sbi = au_sbi(sb);
 
32
 
 
33
        SiMustAnyLock(sb);
 
34
 
 
35
        /* gave up wake_up_bit() */
 
36
        wait_event(sbi->si_plink_wq, !au_ftest_si(sbi, MAINTAIN_PLINK));
 
37
}
 
38
 
 
39
/* ---------------------------------------------------------------------- */
 
40
 
 
41
struct pseudo_link {
 
42
        struct list_head list;
 
43
        struct inode *inode;
 
44
};
 
45
 
 
46
#ifdef CONFIG_AUFS_DEBUG
 
47
void au_plink_list(struct super_block *sb)
 
48
{
 
49
        struct au_sbinfo *sbinfo;
 
50
        struct list_head *plink_list;
 
51
        struct pseudo_link *plink;
 
52
 
 
53
        SiMustAnyLock(sb);
 
54
 
 
55
        sbinfo = au_sbi(sb);
 
56
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
57
 
 
58
        plink_list = &sbinfo->si_plink.head;
 
59
        spin_lock(&sbinfo->si_plink.spin);
 
60
        list_for_each_entry(plink, plink_list, list)
 
61
                AuDbg("%lu\n", plink->inode->i_ino);
 
62
        spin_unlock(&sbinfo->si_plink.spin);
 
63
}
 
64
#endif
 
65
 
 
66
/* is the inode pseudo-linked? */
 
67
int au_plink_test(struct inode *inode)
 
68
{
 
69
        int found;
 
70
        struct au_sbinfo *sbinfo;
 
71
        struct list_head *plink_list;
 
72
        struct pseudo_link *plink;
 
73
 
 
74
        sbinfo = au_sbi(inode->i_sb);
 
75
        AuRwMustAnyLock(&sbinfo->si_rwsem);
 
76
        AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
 
77
 
 
78
        found = 0;
 
79
        plink_list = &sbinfo->si_plink.head;
 
80
        spin_lock(&sbinfo->si_plink.spin);
 
81
        list_for_each_entry(plink, plink_list, list)
 
82
                if (plink->inode == inode) {
 
83
                        found = 1;
 
84
                        break;
 
85
                }
 
86
        spin_unlock(&sbinfo->si_plink.spin);
 
87
        return found;
 
88
}
 
89
 
 
90
/* ---------------------------------------------------------------------- */
 
91
 
 
92
/*
 
93
 * generate a name for plink.
 
94
 * the file will be stored under AUFS_WH_PLINKDIR.
 
95
 */
 
96
/* 20 is max digits length of ulong 64 */
 
97
#define PLINK_NAME_LEN  ((20 + 1) * 2)
 
98
 
 
99
static int plink_name(char *name, int len, struct inode *inode,
 
100
                      aufs_bindex_t bindex)
 
101
{
 
102
        int rlen;
 
103
        struct inode *h_inode;
 
104
 
 
105
        h_inode = au_h_iptr(inode, bindex);
 
106
        rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
 
107
        return rlen;
 
108
}
 
109
 
 
110
/* lookup the plink-ed @inode under the branch at @bindex */
 
111
struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
 
112
{
 
113
        struct dentry *h_dentry, *h_parent;
 
114
        struct au_branch *br;
 
115
        struct inode *h_dir;
 
116
        char a[PLINK_NAME_LEN];
 
117
        struct qstr tgtname = {
 
118
                .name   = a
 
119
        };
 
120
 
 
121
        br = au_sbr(inode->i_sb, bindex);
 
122
        h_parent = br->br_wbr->wbr_plink;
 
123
        h_dir = h_parent->d_inode;
 
124
        tgtname.len = plink_name(a, sizeof(a), inode, bindex);
 
125
 
 
126
        /* always superio. */
 
127
        mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
 
128
        h_dentry = au_sio_lkup_one(&tgtname, h_parent, br);
 
129
        mutex_unlock(&h_dir->i_mutex);
 
130
        return h_dentry;
 
131
}
 
132
 
 
133
/* create a pseudo-link */
 
134
static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
 
135
                      struct dentry *h_dentry, struct au_branch *br)
 
136
{
 
137
        int err;
 
138
        struct path h_path = {
 
139
                .mnt = br->br_mnt
 
140
        };
 
141
        struct inode *h_dir;
 
142
 
 
143
        h_dir = h_parent->d_inode;
 
144
 again:
 
145
        h_path.dentry = au_lkup_one(tgt, h_parent, br, /*nd*/NULL);
 
146
        err = PTR_ERR(h_path.dentry);
 
147
        if (IS_ERR(h_path.dentry))
 
148
                goto out;
 
149
 
 
150
        err = 0;
 
151
        /* wh.plink dir is not monitored */
 
152
        if (h_path.dentry->d_inode
 
153
            && h_path.dentry->d_inode != h_dentry->d_inode) {
 
154
                err = vfsub_unlink(h_dir, &h_path, /*force*/0);
 
155
                dput(h_path.dentry);
 
156
                h_path.dentry = NULL;
 
157
                if (!err)
 
158
                        goto again;
 
159
        }
 
160
        if (!err && !h_path.dentry->d_inode)
 
161
                err = vfsub_link(h_dentry, h_dir, &h_path);
 
162
        dput(h_path.dentry);
 
163
 
 
164
 out:
 
165
        return err;
 
166
}
 
167
 
 
168
struct do_whplink_args {
 
169
        int *errp;
 
170
        struct qstr *tgt;
 
171
        struct dentry *h_parent;
 
172
        struct dentry *h_dentry;
 
173
        struct au_branch *br;
 
174
};
 
175
 
 
176
static void call_do_whplink(void *args)
 
177
{
 
178
        struct do_whplink_args *a = args;
 
179
        *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
 
180
}
 
181
 
 
182
static int whplink(struct dentry *h_dentry, struct inode *inode,
 
183
                   aufs_bindex_t bindex, struct au_branch *br)
 
184
{
 
185
        int err, wkq_err;
 
186
        struct au_wbr *wbr;
 
187
        struct dentry *h_parent;
 
188
        struct inode *h_dir;
 
189
        char a[PLINK_NAME_LEN];
 
190
        struct qstr tgtname = {
 
191
                .name = a
 
192
        };
 
193
 
 
194
        wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
 
195
        h_parent = wbr->wbr_plink;
 
196
        h_dir = h_parent->d_inode;
 
197
        tgtname.len = plink_name(a, sizeof(a), inode, bindex);
 
198
 
 
199
        /* always superio. */
 
200
        mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
 
201
        if (!au_test_wkq(current)) {
 
202
                struct do_whplink_args args = {
 
203
                        .errp           = &err,
 
204
                        .tgt            = &tgtname,
 
205
                        .h_parent       = h_parent,
 
206
                        .h_dentry       = h_dentry,
 
207
                        .br             = br
 
208
                };
 
209
                wkq_err = au_wkq_wait(call_do_whplink, &args);
 
210
                if (unlikely(wkq_err))
 
211
                        err = wkq_err;
 
212
        } else
 
213
                err = do_whplink(&tgtname, h_parent, h_dentry, br);
 
214
        mutex_unlock(&h_dir->i_mutex);
 
215
 
 
216
        return err;
 
217
}
 
218
 
 
219
/* free a single plink */
 
220
static void do_put_plink(struct pseudo_link *plink, int do_del)
 
221
{
 
222
        iput(plink->inode);
 
223
        if (do_del)
 
224
                list_del(&plink->list);
 
225
        kfree(plink);
 
226
}
 
227
 
 
228
/*
 
229
 * create a new pseudo-link for @h_dentry on @bindex.
 
230
 * the linked inode is held in aufs @inode.
 
231
 */
 
232
void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
 
233
                     struct dentry *h_dentry)
 
234
{
 
235
        struct super_block *sb;
 
236
        struct au_sbinfo *sbinfo;
 
237
        struct list_head *plink_list;
 
238
        struct pseudo_link *plink;
 
239
        int found, err, cnt;
 
240
 
 
241
        sb = inode->i_sb;
 
242
        sbinfo = au_sbi(sb);
 
243
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
244
 
 
245
        err = 0;
 
246
        cnt = 0;
 
247
        found = 0;
 
248
        plink_list = &sbinfo->si_plink.head;
 
249
        spin_lock(&sbinfo->si_plink.spin);
 
250
        list_for_each_entry(plink, plink_list, list) {
 
251
                cnt++;
 
252
                if (plink->inode == inode) {
 
253
                        found = 1;
 
254
                        break;
 
255
                }
 
256
        }
 
257
        if (found) {
 
258
                spin_unlock(&sbinfo->si_plink.spin);
 
259
                return;
 
260
        }
 
261
 
 
262
        plink = NULL;
 
263
        if (!found) {
 
264
                plink = kmalloc(sizeof(*plink), GFP_ATOMIC);
 
265
                if (plink) {
 
266
                        plink->inode = au_igrab(inode);
 
267
                        list_add(&plink->list, plink_list);
 
268
                        cnt++;
 
269
                } else
 
270
                        err = -ENOMEM;
 
271
        }
 
272
        spin_unlock(&sbinfo->si_plink.spin);
 
273
 
 
274
        if (!err) {
 
275
                au_plink_block_maintain(sb);
 
276
                err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
 
277
        }
 
278
 
 
279
        if (unlikely(cnt > AUFS_PLINK_WARN))
 
280
                AuWarn1("unexpectedly many pseudo links, %d\n", cnt);
 
281
        if (unlikely(err)) {
 
282
                pr_warning("err %d, damaged pseudo link.\n", err);
 
283
                if (!found && plink)
 
284
                        do_put_plink(plink, /*do_del*/1);
 
285
        }
 
286
}
 
287
 
 
288
/* free all plinks */
 
289
void au_plink_put(struct super_block *sb)
 
290
{
 
291
        struct au_sbinfo *sbinfo;
 
292
        struct list_head *plink_list;
 
293
        struct pseudo_link *plink, *tmp;
 
294
 
 
295
        SiMustWriteLock(sb);
 
296
 
 
297
        sbinfo = au_sbi(sb);
 
298
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
299
 
 
300
        plink_list = &sbinfo->si_plink.head;
 
301
        /* no spin_lock since sbinfo is write-locked */
 
302
        list_for_each_entry_safe(plink, tmp, plink_list, list)
 
303
                do_put_plink(plink, 0);
 
304
        INIT_LIST_HEAD(plink_list);
 
305
}
 
306
 
 
307
/* free the plinks on a branch specified by @br_id */
 
308
void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
 
309
{
 
310
        struct au_sbinfo *sbinfo;
 
311
        struct list_head *plink_list;
 
312
        struct pseudo_link *plink, *tmp;
 
313
        struct inode *inode;
 
314
        aufs_bindex_t bstart, bend, bindex;
 
315
        unsigned char do_put;
 
316
 
 
317
        SiMustWriteLock(sb);
 
318
 
 
319
        sbinfo = au_sbi(sb);
 
320
        AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
 
321
 
 
322
        plink_list = &sbinfo->si_plink.head;
 
323
        /* no spin_lock since sbinfo is write-locked */
 
324
        list_for_each_entry_safe(plink, tmp, plink_list, list) {
 
325
                do_put = 0;
 
326
                inode = au_igrab(plink->inode);
 
327
                ii_write_lock_child(inode);
 
328
                bstart = au_ibstart(inode);
 
329
                bend = au_ibend(inode);
 
330
                if (bstart >= 0) {
 
331
                        for (bindex = bstart; bindex <= bend; bindex++) {
 
332
                                if (!au_h_iptr(inode, bindex)
 
333
                                    || au_ii_br_id(inode, bindex) != br_id)
 
334
                                        continue;
 
335
                                au_set_h_iptr(inode, bindex, NULL, 0);
 
336
                                do_put = 1;
 
337
                                break;
 
338
                        }
 
339
                } else
 
340
                        do_put_plink(plink, 1);
 
341
 
 
342
                if (do_put) {
 
343
                        for (bindex = bstart; bindex <= bend; bindex++)
 
344
                                if (au_h_iptr(inode, bindex)) {
 
345
                                        do_put = 0;
 
346
                                        break;
 
347
                                }
 
348
                        if (do_put)
 
349
                                do_put_plink(plink, 1);
 
350
                }
 
351
                ii_write_unlock(inode);
 
352
                iput(inode);
 
353
        }
 
354
}
 
355
 
 
356
/* ---------------------------------------------------------------------- */
 
357
 
 
358
long au_plink_ioctl(struct file *file, unsigned int cmd)
 
359
{
 
360
        long err;
 
361
        struct super_block *sb;
 
362
        struct au_sbinfo *sbinfo;
 
363
 
 
364
        err = -EACCES;
 
365
        if (!capable(CAP_SYS_ADMIN))
 
366
                goto out;
 
367
 
 
368
        err = 0;
 
369
        sb = file->f_dentry->d_sb;
 
370
        sbinfo = au_sbi(sb);
 
371
        switch (cmd) {
 
372
        case AUFS_CTL_PLINK_MAINT:
 
373
                /*
 
374
                 * pseudo-link maintenance mode,
 
375
                 * cleared by aufs_release_dir()
 
376
                 */
 
377
                si_write_lock(sb);
 
378
                if (!au_ftest_si(sbinfo, MAINTAIN_PLINK)) {
 
379
                        au_fset_si(sbinfo, MAINTAIN_PLINK);
 
380
                        au_fi(file)->fi_maintain_plink = 1;
 
381
                } else
 
382
                        err = -EBUSY;
 
383
                si_write_unlock(sb);
 
384
                break;
 
385
        case AUFS_CTL_PLINK_CLEAN:
 
386
                aufs_write_lock(sb->s_root);
 
387
                if (au_opt_test(sbinfo->si_mntflags, PLINK))
 
388
                        au_plink_put(sb);
 
389
                aufs_write_unlock(sb->s_root);
 
390
                break;
 
391
        default:
 
392
                err = -EINVAL;
 
393
        }
 
394
 out:
 
395
        return err;
 
396
}