2
* Copyright (C) 2005-2009 Junjiro R. 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
26
* during a user process maintains the pseudo-links,
27
* prohibit adding a new plink and branch manipulation.
29
void au_plink_block_maintain(struct super_block *sb)
31
struct au_sbinfo *sbi = au_sbi(sb);
35
/* gave up wake_up_bit() */
36
wait_event(sbi->si_plink_wq, !au_ftest_si(sbi, MAINTAIN_PLINK));
39
/* ---------------------------------------------------------------------- */
42
struct list_head list;
46
#ifdef CONFIG_AUFS_DEBUG
47
void au_plink_list(struct super_block *sb)
49
struct au_sbinfo *sbinfo;
50
struct list_head *plink_list;
51
struct pseudo_link *plink;
56
AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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);
66
/* is the inode pseudo-linked? */
67
int au_plink_test(struct inode *inode)
70
struct au_sbinfo *sbinfo;
71
struct list_head *plink_list;
72
struct pseudo_link *plink;
74
sbinfo = au_sbi(inode->i_sb);
75
AuRwMustAnyLock(&sbinfo->si_rwsem);
76
AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
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) {
86
spin_unlock(&sbinfo->si_plink.spin);
90
/* ---------------------------------------------------------------------- */
93
* generate a name for plink.
94
* the file will be stored under AUFS_WH_PLINKDIR.
96
/* 20 is max digits length of ulong 64 */
97
#define PLINK_NAME_LEN ((20 + 1) * 2)
99
static int plink_name(char *name, int len, struct inode *inode,
100
aufs_bindex_t bindex)
103
struct inode *h_inode;
105
h_inode = au_h_iptr(inode, bindex);
106
rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
110
/* lookup the plink-ed @inode under the branch at @bindex */
111
struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
113
struct dentry *h_dentry, *h_parent;
114
struct au_branch *br;
116
char a[PLINK_NAME_LEN];
117
struct qstr tgtname = {
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);
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);
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)
138
struct path h_path = {
143
h_dir = h_parent->d_inode;
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))
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);
156
h_path.dentry = NULL;
160
if (!err && !h_path.dentry->d_inode)
161
err = vfsub_link(h_dentry, h_dir, &h_path);
168
struct do_whplink_args {
171
struct dentry *h_parent;
172
struct dentry *h_dentry;
173
struct au_branch *br;
176
static void call_do_whplink(void *args)
178
struct do_whplink_args *a = args;
179
*a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
182
static int whplink(struct dentry *h_dentry, struct inode *inode,
183
aufs_bindex_t bindex, struct au_branch *br)
187
struct dentry *h_parent;
189
char a[PLINK_NAME_LEN];
190
struct qstr tgtname = {
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);
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 = {
205
.h_parent = h_parent,
206
.h_dentry = h_dentry,
209
wkq_err = au_wkq_wait(call_do_whplink, &args);
210
if (unlikely(wkq_err))
213
err = do_whplink(&tgtname, h_parent, h_dentry, br);
214
mutex_unlock(&h_dir->i_mutex);
219
/* free a single plink */
220
static void do_put_plink(struct pseudo_link *plink, int do_del)
224
list_del(&plink->list);
229
* create a new pseudo-link for @h_dentry on @bindex.
230
* the linked inode is held in aufs @inode.
232
void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
233
struct dentry *h_dentry)
235
struct super_block *sb;
236
struct au_sbinfo *sbinfo;
237
struct list_head *plink_list;
238
struct pseudo_link *plink;
243
AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
248
plink_list = &sbinfo->si_plink.head;
249
spin_lock(&sbinfo->si_plink.spin);
250
list_for_each_entry(plink, plink_list, list) {
252
if (plink->inode == inode) {
258
spin_unlock(&sbinfo->si_plink.spin);
264
plink = kmalloc(sizeof(*plink), GFP_ATOMIC);
266
plink->inode = au_igrab(inode);
267
list_add(&plink->list, plink_list);
272
spin_unlock(&sbinfo->si_plink.spin);
275
au_plink_block_maintain(sb);
276
err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
279
if (unlikely(cnt > AUFS_PLINK_WARN))
280
AuWarn1("unexpectedly many pseudo links, %d\n", cnt);
282
pr_warning("err %d, damaged pseudo link.\n", err);
284
do_put_plink(plink, /*do_del*/1);
288
/* free all plinks */
289
void au_plink_put(struct super_block *sb)
291
struct au_sbinfo *sbinfo;
292
struct list_head *plink_list;
293
struct pseudo_link *plink, *tmp;
298
AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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);
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)
310
struct au_sbinfo *sbinfo;
311
struct list_head *plink_list;
312
struct pseudo_link *plink, *tmp;
314
aufs_bindex_t bstart, bend, bindex;
315
unsigned char do_put;
320
AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
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) {
326
inode = au_igrab(plink->inode);
327
ii_write_lock_child(inode);
328
bstart = au_ibstart(inode);
329
bend = au_ibend(inode);
331
for (bindex = bstart; bindex <= bend; bindex++) {
332
if (!au_h_iptr(inode, bindex)
333
|| au_ii_br_id(inode, bindex) != br_id)
335
au_set_h_iptr(inode, bindex, NULL, 0);
340
do_put_plink(plink, 1);
343
for (bindex = bstart; bindex <= bend; bindex++)
344
if (au_h_iptr(inode, bindex)) {
349
do_put_plink(plink, 1);
351
ii_write_unlock(inode);
356
/* ---------------------------------------------------------------------- */
358
long au_plink_ioctl(struct file *file, unsigned int cmd)
361
struct super_block *sb;
362
struct au_sbinfo *sbinfo;
365
if (!capable(CAP_SYS_ADMIN))
369
sb = file->f_dentry->d_sb;
372
case AUFS_CTL_PLINK_MAINT:
374
* pseudo-link maintenance mode,
375
* cleared by aufs_release_dir()
378
if (!au_ftest_si(sbinfo, MAINTAIN_PLINK)) {
379
au_fset_si(sbinfo, MAINTAIN_PLINK);
380
au_fi(file)->fi_maintain_plink = 1;
385
case AUFS_CTL_PLINK_CLEAN:
386
aufs_write_lock(sb->s_root);
387
if (au_opt_test(sbinfo->si_mntflags, PLINK))
389
aufs_write_unlock(sb->s_root);