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

« back to all changes in this revision

Viewing changes to fs/aufs/whout.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: whout.c,v 1.13 2007/05/07 03:46:08 sfjro Exp $ */
 
20
 
 
21
#include <linux/fs.h>
 
22
#include <linux/namei.h>
 
23
#include <linux/random.h>
 
24
#include <linux/security.h>
 
25
#include "aufs.h"
 
26
 
 
27
#define WH_MASK                 S_IRUGO
 
28
 
 
29
/* If a directory contains this file, then it is opaque.  We start with the
 
30
 * .wh. flag so that it is blocked by lookup.
 
31
 */
 
32
static struct qstr diropq_name = {
 
33
        .name = AUFS_WH_DIROPQ,
 
34
        .len = sizeof(AUFS_WH_DIROPQ) - 1
 
35
};
 
36
 
 
37
/*
 
38
 * generate whiteout name, which is NOT terminated by NULL.
 
39
 * @name: original d_name.name
 
40
 * @len: original d_name.len
 
41
 * @wh: whiteout qstr
 
42
 * returns zero when succeeds, otherwise error.
 
43
 * succeeded value as wh->name should be freed by au_free_whname().
 
44
 */
 
45
int au_alloc_whname(const char *name, int len, struct qstr *wh)
 
46
{
 
47
        char *p;
 
48
 
 
49
        DEBUG_ON(!name || !len || !wh);
 
50
 
 
51
        if (unlikely(len > PATH_MAX - AUFS_WH_LEN))
 
52
                return -ENAMETOOLONG;
 
53
 
 
54
        wh->len = len + AUFS_WH_LEN;
 
55
        wh->name = p = kmalloc(wh->len, GFP_KERNEL);
 
56
        //if (LktrCond) {kfree(p); wh->name = p = NULL;}
 
57
        if (p) {
 
58
                memcpy(p, AUFS_WH_PFX, AUFS_WH_LEN);
 
59
                memcpy(p + AUFS_WH_LEN, name, len);
 
60
                //smp_mb();
 
61
                return 0;
 
62
        }
 
63
        return -ENOMEM;
 
64
}
 
65
 
 
66
void au_free_whname(struct qstr *wh)
 
67
{
 
68
        DEBUG_ON(!wh || !wh->name);
 
69
        kfree(wh->name);
 
70
#ifdef CONFIG_AUFS_DEBUG
 
71
        wh->name = NULL;
 
72
#endif
 
73
}
 
74
 
 
75
/* ---------------------------------------------------------------------- */
 
76
 
 
77
/*
 
78
 * test if the @wh_name exists under @hidden_parent.
 
79
 * @try_sio specifies the necessary of super-io.
 
80
 */
 
81
int is_wh(struct dentry *hidden_parent, struct qstr *wh_name, int try_sio,
 
82
          struct lkup_args *lkup)
 
83
{
 
84
        int err;
 
85
        struct dentry *wh_dentry;
 
86
        struct inode *hidden_dir;
 
87
 
 
88
        LKTRTrace("%.*s/%.*s, lkup{%p, %d}\n", DLNPair(hidden_parent),
 
89
                  wh_name->len, wh_name->name, lkup->nfsmnt, lkup->dlgt);
 
90
        hidden_dir = hidden_parent->d_inode;
 
91
        DEBUG_ON(!S_ISDIR(hidden_dir->i_mode));
 
92
        IMustLock(hidden_dir);
 
93
 
 
94
        if (!try_sio)
 
95
                wh_dentry = lkup_one(wh_name->name, hidden_parent,
 
96
                                     wh_name->len, lkup);
 
97
        else
 
98
                wh_dentry = sio_lkup_one(wh_name->name, hidden_parent,
 
99
                                         wh_name->len, lkup);
 
100
        //if (LktrCond) {dput(wh_dentry); wh_dentry = ERR_PTR(-1);}
 
101
        err = PTR_ERR(wh_dentry);
 
102
        if (IS_ERR(wh_dentry))
 
103
                goto out;
 
104
 
 
105
        err = 0;
 
106
        if (!wh_dentry->d_inode)
 
107
                goto out_wh; /* success */
 
108
 
 
109
        err = 1;
 
110
        if (S_ISREG(wh_dentry->d_inode->i_mode))
 
111
                goto out_wh; /* success */
 
112
 
 
113
        err = -EIO;
 
114
        IOErr("%.*s Invalid whiteout entry type 0%o.\n",
 
115
              DLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
 
116
 
 
117
 out_wh:
 
118
        dput(wh_dentry);
 
119
 out:
 
120
        TraceErr(err);
 
121
        return err;
 
122
}
 
123
 
 
124
/*
 
125
 * test if the @hidden_dentry sets opaque or not.
 
126
 */
 
127
int is_diropq(struct dentry *hidden_dentry, struct lkup_args *lkup)
 
128
{
 
129
        int err;
 
130
        struct inode *hidden_dir;
 
131
 
 
132
        LKTRTrace("dentry %.*s\n", DLNPair(hidden_dentry));
 
133
        hidden_dir = hidden_dentry->d_inode;
 
134
        DEBUG_ON(!S_ISDIR(hidden_dir->i_mode));
 
135
        IMustLock(hidden_dir);
 
136
 
 
137
        err = is_wh(hidden_dentry, &diropq_name, /*try_sio*/1, lkup);
 
138
        TraceErr(err);
 
139
        return err;
 
140
}
 
141
 
 
142
/*
 
143
 * returns a negative dentry whose name is unique and temporary.
 
144
 */
 
145
struct dentry *lkup_whtmp(struct dentry *hidden_parent, struct qstr *prefix,
 
146
                          struct lkup_args *lkup)
 
147
{
 
148
#define HEX_LEN 4
 
149
        struct dentry *dentry;
 
150
        int len, i;
 
151
        char defname[AUFS_WH_LEN * 2 + DNAME_INLINE_LEN_MIN + 1 + HEX_LEN + 1],
 
152
                *name, *p;
 
153
        static unsigned char cnt;
 
154
 
 
155
        LKTRTrace("hp %.*s, prefix %.*s\n",
 
156
                  DLNPair(hidden_parent), prefix->len, prefix->name);
 
157
        DEBUG_ON(!hidden_parent->d_inode);
 
158
        IMustLock(hidden_parent->d_inode);
 
159
 
 
160
        name = defname;
 
161
        len = sizeof(defname) - DNAME_INLINE_LEN_MIN + prefix->len - 1;
 
162
        if (unlikely(prefix->len > DNAME_INLINE_LEN_MIN)) {
 
163
                dentry = ERR_PTR(-ENAMETOOLONG);
 
164
                if (unlikely(len >= PATH_MAX))
 
165
                        goto out;
 
166
                dentry = ERR_PTR(-ENOMEM);
 
167
                name = kmalloc(len + 1, GFP_KERNEL);
 
168
                //if (LktrCond) {kfree(name); name = NULL;}
 
169
                if (unlikely(!name))
 
170
                        goto out;
 
171
        }
 
172
 
 
173
        // doubly whiteout-ed
 
174
        memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_LEN * 2);
 
175
        p = name + AUFS_WH_LEN * 2;
 
176
        memcpy(p, prefix->name, prefix->len);
 
177
        p += prefix->len;
 
178
        *p++ = '.';
 
179
        DEBUG_ON(name + len + 1 - p <= HEX_LEN);
 
180
 
 
181
        for (i = 0; i < 3; i++) {
 
182
                sprintf(p, "%.*d", HEX_LEN, cnt++);
 
183
                dentry = sio_lkup_one(name, hidden_parent, len, lkup);
 
184
                //if (LktrCond) {dput(dentry); dentry = ERR_PTR(-1);}
 
185
                if (unlikely(IS_ERR(dentry) || !dentry->d_inode))
 
186
                        goto out_name;
 
187
                dput(dentry);
 
188
        }
 
189
        //Warn("could not get random name\n");
 
190
        dentry = ERR_PTR(-EEXIST);
 
191
        Dbg("%.*s\n", len, name);
 
192
        BUG();
 
193
 
 
194
 out_name:
 
195
        if (unlikely(name != defname))
 
196
                kfree(name);
 
197
 out:
 
198
        TraceErrPtr(dentry);
 
199
        return dentry;
 
200
#undef HEX_LEN
 
201
}
 
202
 
 
203
/*
 
204
 * rename the @dentry of @bindex to the whiteouted temporary name.
 
205
 */
 
206
int rename_whtmp(struct dentry *dentry, aufs_bindex_t bindex)
 
207
{
 
208
        int err;
 
209
        struct inode *hidden_dir;
 
210
        struct dentry *hidden_dentry, *hidden_parent, *tmp_dentry;
 
211
        struct super_block *sb;
 
212
        struct lkup_args lkup;
 
213
 
 
214
        LKTRTrace("%.*s, b%d\n", DLNPair(dentry), bindex);
 
215
        hidden_dentry = au_h_dptr_i(dentry, bindex);
 
216
        DEBUG_ON(!hidden_dentry || !hidden_dentry->d_inode);
 
217
        hidden_parent = hidden_dentry->d_parent;
 
218
        hidden_dir = hidden_parent->d_inode;
 
219
        IMustLock(hidden_dir);
 
220
 
 
221
        sb = dentry->d_sb;
 
222
        lkup.nfsmnt = au_nfsmnt(sb, bindex);
 
223
        lkup.dlgt = need_dlgt(sb);
 
224
        tmp_dentry = lkup_whtmp(hidden_parent, &hidden_dentry->d_name, &lkup);
 
225
        //if (LktrCond) {dput(tmp_dentry); tmp_dentry = ERR_PTR(-1);}
 
226
        err = PTR_ERR(tmp_dentry);
 
227
        if (!IS_ERR(tmp_dentry)) {
 
228
                /* under the same dir, no need to lock_rename() */
 
229
                err = vfsub_rename(hidden_dir, hidden_dentry,
 
230
                                   hidden_dir, tmp_dentry, lkup.dlgt);
 
231
                //if (LktrCond) err = -1; //unavailable
 
232
                TraceErr(err);
 
233
                dput(tmp_dentry);
 
234
        }
 
235
 
 
236
        TraceErr(err);
 
237
        return err;
 
238
}
 
239
 
 
240
/* ---------------------------------------------------------------------- */
 
241
 
 
242
int au_unlink_wh_dentry(struct inode *hidden_dir, struct dentry *wh_dentry,
 
243
                        struct dentry *dentry, int dlgt)
 
244
{
 
245
        int err;
 
246
 
 
247
        LKTRTrace("hi%lu, wh %.*s, d %p\n", hidden_dir->i_ino,
 
248
                  DLNPair(wh_dentry), dentry);
 
249
        DEBUG_ON((dentry && dbwh(dentry) == -1)
 
250
                 || !wh_dentry->d_inode
 
251
                 || !S_ISREG(wh_dentry->d_inode->i_mode));
 
252
        IMustLock(hidden_dir);
 
253
 
 
254
        err = vfsub_unlink(hidden_dir, wh_dentry, dlgt);
 
255
        //if (LktrCond) err = -1; // unavailable
 
256
        if (!err && dentry)
 
257
                set_dbwh(dentry, -1);
 
258
 
 
259
        TraceErr(err);
 
260
        return err;
 
261
}
 
262
 
 
263
static int unlink_wh_name(struct dentry *hidden_parent, struct qstr *wh,
 
264
                          struct lkup_args *lkup)
 
265
{
 
266
        int err;
 
267
        struct inode *hidden_dir;
 
268
        struct dentry *hidden_dentry;
 
269
 
 
270
        LKTRTrace("%.*s/%.*s\n", DLNPair(hidden_parent), LNPair(wh));
 
271
        hidden_dir = hidden_parent->d_inode;
 
272
        IMustLock(hidden_dir);
 
273
 
 
274
        // au_test_perm() is already done
 
275
        hidden_dentry = lkup_one(wh->name, hidden_parent, wh->len, lkup);
 
276
        //if (LktrCond) {dput(hidden_dentry); hidden_dentry = ERR_PTR(-1);}
 
277
        if (!IS_ERR(hidden_dentry)) {
 
278
                err = 0;
 
279
                if (hidden_dentry->d_inode)
 
280
                        err = vfsub_unlink(hidden_dir, hidden_dentry,
 
281
                                           lkup->dlgt);
 
282
                dput(hidden_dentry);
 
283
        } else
 
284
                err = PTR_ERR(hidden_dentry);
 
285
 
 
286
        TraceErr(err);
 
287
        return err;
 
288
}
 
289
 
 
290
/* ---------------------------------------------------------------------- */
 
291
 
 
292
static void clean_wh(struct inode *h_dir, struct dentry *wh)
 
293
{
 
294
        TraceEnter();
 
295
        if (wh->d_inode) {
 
296
                int err = vfsub_unlink(h_dir, wh, /*dlgt*/0);
 
297
                if (unlikely(err))
 
298
                        Warn("failed unlink %.*s (%d), ignored.\n",
 
299
                             DLNPair(wh), err);
 
300
        }
 
301
}
 
302
 
 
303
static void clean_plink(struct inode *h_dir, struct dentry *plink)
 
304
{
 
305
        TraceEnter();
 
306
        if (plink->d_inode) {
 
307
                int err = vfsub_rmdir(h_dir, plink, /*dlgt*/0);
 
308
                if (unlikely(err))
 
309
                        Warn("failed rmdir %.*s (%d), ignored.\n",
 
310
                             DLNPair(plink), err);
 
311
        }
 
312
}
 
313
 
 
314
static int test_linkable(struct inode *h_dir)
 
315
{
 
316
        if (h_dir->i_op && h_dir->i_op->link)
 
317
                return 0;
 
318
        return -ENOSYS;
 
319
}
 
320
 
 
321
static int plink_dir(struct inode *h_dir, struct dentry *plink)
 
322
{
 
323
        int err;
 
324
 
 
325
        err = -EEXIST;
 
326
        if (!plink->d_inode) {
 
327
                int mode = S_IRWXU;
 
328
                if (unlikely(au_is_nfs(plink->d_sb)))
 
329
                        mode |= S_IXUGO;
 
330
                err = vfsub_mkdir(h_dir, plink, mode, /*dlgt*/0);
 
331
        } else if (S_ISDIR(plink->d_inode->i_mode))
 
332
                err = 0;
 
333
        else
 
334
                Err("unknown %.*s exists\n", DLNPair(plink));
 
335
 
 
336
        return err;
 
337
}
 
338
 
 
339
/*
 
340
 * initialize the whiteout base file/dir for @br.
 
341
 */
 
342
int init_wh(struct dentry *h_root, struct aufs_branch *br,
 
343
            struct vfsmount *nfsmnt, struct super_block *sb)
 
344
{
 
345
        int err;
 
346
        struct dentry *wh, *plink;
 
347
        struct inode *h_dir;
 
348
        static struct qstr base_name[] = {
 
349
                {.name = AUFS_WH_BASENAME, .len = sizeof(AUFS_WH_BASENAME) - 1},
 
350
                {.name = AUFS_WH_PLINKDIR, .len = sizeof(AUFS_WH_PLINKDIR) - 1}
 
351
        };
 
352
        struct lkup_args lkup = {
 
353
                .nfsmnt = nfsmnt,
 
354
                .dlgt   = 0 // always no dlgt
 
355
        };
 
356
        const int do_plink = au_flag_test(sb, AuFlag_PLINK);
 
357
 
 
358
        LKTRTrace("nfsmnt %p\n", nfsmnt);
 
359
        BrWhMustWriteLock(br);
 
360
        SiMustWriteLock(sb);
 
361
        h_dir = h_root->d_inode;
 
362
        IMustLock(h_dir);
 
363
 
 
364
        // doubly whiteouted
 
365
        wh = lkup_wh(h_root, base_name + 0, &lkup);
 
366
        //if (LktrCond) {dput(wh); wh = ERR_PTR(-1);}
 
367
        err = PTR_ERR(wh);
 
368
        if (IS_ERR(wh))
 
369
                goto out;
 
370
        DEBUG_ON(br->br_wh && br->br_wh != wh);
 
371
 
 
372
        plink = lkup_wh(h_root, base_name + 1, &lkup);
 
373
        err = PTR_ERR(plink);
 
374
        if (IS_ERR(plink))
 
375
                goto out_dput_wh;
 
376
        DEBUG_ON(br->br_plink && br->br_plink != plink);
 
377
 
 
378
        dput(br->br_wh);
 
379
        dput(br->br_plink);
 
380
        br->br_wh = br->br_plink = NULL;
 
381
 
 
382
        err = 0;
 
383
        switch (br->br_perm) {
 
384
        case AuBr_RR:
 
385
        case AuBr_RO:
 
386
        case AuBr_RRWH:
 
387
        case AuBr_ROWH:
 
388
                clean_wh(h_dir, wh);
 
389
                clean_plink(h_dir, plink);
 
390
                break;
 
391
 
 
392
        case AuBr_RWNoLinkWH:
 
393
                clean_wh(h_dir, wh);
 
394
                if (do_plink) {
 
395
                        err = test_linkable(h_dir);
 
396
                        if (unlikely(err))
 
397
                                goto out_nolink;
 
398
 
 
399
                        err = plink_dir(h_dir, plink);
 
400
                        if (unlikely(err))
 
401
                                goto out_err;
 
402
                        br->br_plink = dget(plink);
 
403
                } else
 
404
                        clean_plink(h_dir, plink);
 
405
                break;
 
406
 
 
407
        case AuBr_RW:
 
408
                /*
 
409
                 * for the moment, aufs supports the branch filesystem
 
410
                 * which does not support link(2).
 
411
                 * testing on FAT which does not support i_op->setattr() fully either,
 
412
                 * copyup failed.
 
413
                 * finally, such filesystem will not be used as the writable branch.
 
414
                 */
 
415
                err = test_linkable(h_dir);
 
416
                if (unlikely(err))
 
417
                        goto out_nolink;
 
418
 
 
419
                err = -EEXIST;
 
420
                if (!wh->d_inode)
 
421
                        err = vfsub_create(h_dir, wh, WH_MASK, NULL, /*dlgt*/0);
 
422
                else if (S_ISREG(wh->d_inode->i_mode))
 
423
                        err = 0;
 
424
                else
 
425
                        Err("unknown %.*s/%.*s exists\n",
 
426
                            DLNPair(h_root), DLNPair(wh));
 
427
                if (unlikely(err))
 
428
                        goto out_err;
 
429
 
 
430
                if (do_plink) {
 
431
                        err = plink_dir(h_dir, plink);
 
432
                        if (unlikely(err))
 
433
                                goto out_err;
 
434
                        br->br_plink = dget(plink);
 
435
                } else
 
436
                        clean_plink(h_dir, plink);
 
437
                br->br_wh = dget(wh);
 
438
                break;
 
439
 
 
440
        default:
 
441
                BUG();
 
442
        }
 
443
 
 
444
 out_dput:
 
445
        dput(plink);
 
446
 out_dput_wh:
 
447
        dput(wh);
 
448
 out:
 
449
        TraceErr(err);
 
450
        return err;
 
451
 out_nolink:
 
452
        Err("%.*s doesn't support link(2), use noplink and rw+nolwh\n",
 
453
            DLNPair(h_root));
 
454
        goto out_dput;
 
455
 out_err:
 
456
        Err("an error(%d) on the writable branch %.*s(%s)\n",
 
457
            err, DLNPair(h_root), au_sbtype(h_root->d_sb));
 
458
        goto out_dput;
 
459
}
 
460
 
 
461
struct reinit_br_wh {
 
462
        struct super_block *sb;
 
463
        struct aufs_branch *br;
 
464
};
 
465
 
 
466
static void reinit_br_wh(void *arg)
 
467
{
 
468
        int err;
 
469
        struct reinit_br_wh *a = arg;
 
470
        struct inode *hidden_dir, *dir;
 
471
        struct dentry *hidden_root;
 
472
        aufs_bindex_t bindex;
 
473
 
 
474
        TraceEnter();
 
475
        DEBUG_ON(!a->br->br_wh || !a->br->br_wh->d_inode || current->fsuid);
 
476
 
 
477
        err = 0;
 
478
        /* big lock */
 
479
        si_write_lock(a->sb);
 
480
        if (unlikely(!br_writable(a->br->br_perm)))
 
481
                goto out;
 
482
        bindex = find_brindex(a->sb, a->br->br_id);
 
483
        if (unlikely(bindex < 0))
 
484
                goto out;
 
485
 
 
486
        dir = a->sb->s_root->d_inode;
 
487
        hidden_root = a->br->br_wh->d_parent;
 
488
        hidden_dir = hidden_root->d_inode;
 
489
        DEBUG_ON(!hidden_dir->i_op || !hidden_dir->i_op->link);
 
490
        hdir_lock(hidden_dir, dir, bindex);
 
491
        br_wh_write_lock(a->br);
 
492
        err = vfsub_unlink(hidden_dir, a->br->br_wh, /*dlgt*/0);
 
493
        //if (LktrCond) err = -1;
 
494
        dput(a->br->br_wh);
 
495
        a->br->br_wh = NULL;
 
496
        if (!err)
 
497
                err = init_wh(hidden_root, a->br, au_do_nfsmnt(a->br->br_mnt),
 
498
                              a->sb);
 
499
        br_wh_write_unlock(a->br);
 
500
        hdir_unlock(hidden_dir, dir, bindex);
 
501
 
 
502
 out:
 
503
        atomic_dec(&a->br->br_wh_running);
 
504
        br_put(a->br);
 
505
        si_write_unlock(a->sb);
 
506
        kfree(arg);
 
507
        if (unlikely(err))
 
508
                IOErr("err %d\n", err);
 
509
}
 
510
 
 
511
static void kick_reinit_br_wh(struct super_block *sb, struct aufs_branch *br)
 
512
{
 
513
        int do_dec;
 
514
        struct reinit_br_wh *arg;
 
515
 
 
516
        do_dec = 1;
 
517
        if (atomic_inc_return(&br->br_wh_running) != 1)
 
518
                goto out;
 
519
 
 
520
        // ignore ENOMEM
 
521
        arg = kmalloc(sizeof(*arg), GFP_KERNEL);
 
522
        if (arg) {
 
523
                // dec(wh_running), kfree(arg) and br_put() in reinit function
 
524
                arg->sb = sb;
 
525
                arg->br = br;
 
526
                br_get(br);
 
527
                au_wkq_nowait(reinit_br_wh, arg, /*dlgt*/0);
 
528
                do_dec = 0;
 
529
        }
 
530
 
 
531
 out:
 
532
        if (do_dec)
 
533
                atomic_dec(&br->br_wh_running);
 
534
}
 
535
 
 
536
/*
 
537
 * create the whiteoute @wh.
 
538
 */
 
539
static int link_or_create_wh(struct dentry *wh, struct super_block *sb,
 
540
                             aufs_bindex_t bindex)
 
541
{
 
542
        int err, dlgt;
 
543
        struct aufs_branch *br;
 
544
        struct dentry *hidden_parent;
 
545
        struct inode *hidden_dir;
 
546
 
 
547
        LKTRTrace("%.*s\n", DLNPair(wh));
 
548
        SiMustReadLock(sb);
 
549
        hidden_parent = wh->d_parent;
 
550
        hidden_dir = hidden_parent->d_inode;
 
551
        IMustLock(hidden_dir);
 
552
 
 
553
        dlgt = need_dlgt(sb);
 
554
        br = stobr(sb, bindex);
 
555
        br_wh_read_lock(br);
 
556
        if (br->br_wh) {
 
557
                err = vfsub_link(br->br_wh, hidden_dir, wh, dlgt);
 
558
                if (!err || err != -EMLINK)
 
559
                        goto out;
 
560
 
 
561
                // link count full. re-initialize br_wh.
 
562
                kick_reinit_br_wh(sb, br);
 
563
        }
 
564
 
 
565
        // return this error in this context
 
566
        err = vfsub_create(hidden_dir, wh, WH_MASK, NULL, dlgt);
 
567
 
 
568
 out:
 
569
        br_wh_read_unlock(br);
 
570
        TraceErr(err);
 
571
        return err;
 
572
}
 
573
 
 
574
/* ---------------------------------------------------------------------- */
 
575
 
 
576
/*
 
577
 * create or remove the diropq.
 
578
 */
 
579
static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
 
580
                                int do_create, int dlgt)
 
581
{
 
582
        struct dentry *opq_dentry, *hidden_dentry;
 
583
        struct inode *hidden_dir;
 
584
        int err;
 
585
        struct super_block *sb;
 
586
        struct lkup_args lkup;
 
587
 
 
588
        LKTRTrace("%.*s, bindex %d, do_create %d\n", DLNPair(dentry),
 
589
                  bindex, do_create);
 
590
        hidden_dentry = au_h_dptr_i(dentry, bindex);
 
591
        DEBUG_ON(!hidden_dentry);
 
592
        hidden_dir = hidden_dentry->d_inode;
 
593
        DEBUG_ON(!hidden_dir || !S_ISDIR(hidden_dir->i_mode));
 
594
        IMustLock(hidden_dir);
 
595
 
 
596
        // already checked by au_test_perm().
 
597
        sb = dentry->d_sb;
 
598
        lkup.nfsmnt = au_nfsmnt(sb, bindex);
 
599
        lkup.dlgt = dlgt;
 
600
        opq_dentry = lkup_one(diropq_name.name, hidden_dentry, diropq_name.len,
 
601
                              &lkup);
 
602
        //if (LktrCond) {dput(opq_dentry); opq_dentry = ERR_PTR(-1);}
 
603
        if (IS_ERR(opq_dentry))
 
604
                goto out;
 
605
 
 
606
        if (do_create) {
 
607
                DEBUG_ON(opq_dentry->d_inode);
 
608
                err = link_or_create_wh(opq_dentry, sb, bindex);
 
609
                //if (LktrCond) {vfs_unlink(hidden_dir, opq_dentry); err = -1;}
 
610
                if (!err) {
 
611
                        set_dbdiropq(dentry, bindex);
 
612
                        goto out; /* success */
 
613
                }
 
614
        } else {
 
615
                DEBUG_ON(/* !S_ISDIR(dentry->d_inode->i_mode)
 
616
                          * ||  */!opq_dentry->d_inode);
 
617
                err = vfsub_unlink(hidden_dir, opq_dentry, lkup.dlgt);
 
618
                //if (LktrCond) err = -1;
 
619
                if (!err)
 
620
                        set_dbdiropq(dentry, -1);
 
621
        }
 
622
        dput(opq_dentry);
 
623
        opq_dentry = ERR_PTR(err);
 
624
 
 
625
 out:
 
626
        TraceErrPtr(opq_dentry);
 
627
        return opq_dentry;
 
628
}
 
629
 
 
630
struct do_diropq_args {
 
631
        struct dentry **errp;
 
632
        struct dentry *dentry;
 
633
        aufs_bindex_t bindex;
 
634
        int do_create, dlgt;
 
635
};
 
636
 
 
637
static void call_do_diropq(void *args)
 
638
{
 
639
        struct do_diropq_args *a = args;
 
640
        *a->errp = do_diropq(a->dentry, a->bindex, a->do_create, a->dlgt);
 
641
}
 
642
 
 
643
struct dentry *sio_diropq(struct dentry *dentry, aufs_bindex_t bindex,
 
644
                          int do_create, int dlgt)
 
645
{
 
646
        struct dentry *diropq, *hidden_dentry;
 
647
 
 
648
        LKTRTrace("%.*s, bindex %d, do_create %d\n",
 
649
                  DLNPair(dentry), bindex, do_create);
 
650
 
 
651
        hidden_dentry = au_h_dptr_i(dentry, bindex);
 
652
        if (!au_test_perm(hidden_dentry->d_inode, MAY_EXEC | MAY_WRITE, dlgt))
 
653
                diropq = do_diropq(dentry, bindex, do_create, dlgt);
 
654
        else {
 
655
                struct do_diropq_args args = {
 
656
                        .errp           = &diropq,
 
657
                        .dentry         = dentry,
 
658
                        .bindex         = bindex,
 
659
                        .do_create      = do_create,
 
660
                        .dlgt           = dlgt
 
661
                };
 
662
                au_wkq_wait(call_do_diropq, &args, /*dlgt*/0);
 
663
        }
 
664
 
 
665
        TraceErrPtr(diropq);
 
666
        return diropq;
 
667
}
 
668
 
 
669
/* ---------------------------------------------------------------------- */
 
670
 
 
671
/*
 
672
 * lookup whiteout dentry.
 
673
 * @hidden_parent: hidden parent dentry which must exist and be locked
 
674
 * @base_name: name of dentry which will be whiteouted
 
675
 * returns dentry for whiteout.
 
676
 */
 
677
struct dentry *lkup_wh(struct dentry *hidden_parent, struct qstr *base_name,
 
678
                       struct lkup_args *lkup)
 
679
{
 
680
        int err;
 
681
        struct qstr wh_name;
 
682
        struct dentry *wh_dentry;
 
683
 
 
684
        LKTRTrace("%.*s/%.*s\n", DLNPair(hidden_parent), LNPair(base_name));
 
685
        IMustLock(hidden_parent->d_inode);
 
686
 
 
687
        err = au_alloc_whname(base_name->name, base_name->len, &wh_name);
 
688
        //if (LktrCond) {au_free_whname(&wh_name); err = -1;}
 
689
        wh_dentry = ERR_PTR(err);
 
690
        if (!err) {
 
691
                // do not superio.
 
692
                wh_dentry = lkup_one(wh_name.name, hidden_parent, wh_name.len,
 
693
                                     lkup);
 
694
                au_free_whname(&wh_name);
 
695
        }
 
696
        TraceErrPtr(wh_dentry);
 
697
        return wh_dentry;
 
698
}
 
699
 
 
700
/*
 
701
 * link/create a whiteout for @dentry on @bindex.
 
702
 */
 
703
struct dentry *simple_create_wh(struct dentry *dentry, aufs_bindex_t bindex,
 
704
                                struct dentry *hidden_parent,
 
705
                                struct lkup_args *lkup)
 
706
{
 
707
        struct dentry *wh_dentry;
 
708
        int err;
 
709
        struct super_block *sb;
 
710
 
 
711
        LKTRTrace("%.*s/%.*s on b%d\n", DLNPair(hidden_parent),
 
712
                  DLNPair(dentry), bindex);
 
713
 
 
714
        sb = dentry->d_sb;
 
715
        wh_dentry = lkup_wh(hidden_parent, &dentry->d_name, lkup);
 
716
        //au_nfsmnt(sb, bindex), need_dlgt(sb));
 
717
        //if (LktrCond) {dput(wh_dentry); wh_dentry = ERR_PTR(-1);}
 
718
        if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
 
719
                IMustLock(hidden_parent->d_inode);
 
720
                err = link_or_create_wh(wh_dentry, sb, bindex);
 
721
                if (!err)
 
722
                        set_dbwh(dentry, bindex);
 
723
                else {
 
724
                        dput(wh_dentry);
 
725
                        wh_dentry = ERR_PTR(err);
 
726
                }
 
727
        }
 
728
 
 
729
        TraceErrPtr(wh_dentry);
 
730
        return wh_dentry;
 
731
}
 
732
 
 
733
/* ---------------------------------------------------------------------- */
 
734
 
 
735
/* Delete all whiteouts in this directory in branch bindex. */
 
736
static int del_wh_children(struct aufs_nhash *whlist,
 
737
                           struct dentry *hidden_parent, aufs_bindex_t bindex,
 
738
                           struct lkup_args *lkup)
 
739
{
 
740
        int err, i;
 
741
        struct qstr wh_name;
 
742
        char *p;
 
743
        struct inode *hidden_dir;
 
744
        struct hlist_head *head;
 
745
        struct aufs_wh *tpos;
 
746
        struct hlist_node *pos;
 
747
        struct aufs_destr *str;
 
748
 
 
749
        LKTRTrace("%.*s\n", DLNPair(hidden_parent));
 
750
        hidden_dir = hidden_parent->d_inode;
 
751
        IMustLock(hidden_dir);
 
752
        DEBUG_ON(IS_RDONLY(hidden_dir));
 
753
        //SiMustReadLock(??);
 
754
 
 
755
        err = -ENOMEM;
 
756
        wh_name.name = p = __getname();
 
757
        //if (LktrCond) {__putname(p); wh_name.name = p = NULL;}
 
758
        if (unlikely(!wh_name.name))
 
759
                goto out;
 
760
        memcpy(p, AUFS_WH_PFX, AUFS_WH_LEN);
 
761
        p += AUFS_WH_LEN;
 
762
 
 
763
        // already checked by au_test_perm().
 
764
        err = 0;
 
765
        for (i = 0; !err && i < AUFS_NHASH_SIZE; i++) {
 
766
                head = whlist->heads + i;
 
767
                hlist_for_each_entry(tpos, pos, head, wh_hash) {
 
768
                        if (tpos->wh_bindex != bindex)
 
769
                                continue;
 
770
                        str = &tpos->wh_str;
 
771
                        if (str->len + AUFS_WH_LEN <= PATH_MAX) {
 
772
                                memcpy(p, str->name, str->len);
 
773
                                wh_name.len = AUFS_WH_LEN + str->len;
 
774
                                err = unlink_wh_name(hidden_parent, &wh_name,
 
775
                                                     lkup);
 
776
                                //if (LktrCond) err = -1;
 
777
                                if (!err)
 
778
                                        continue;
 
779
                                break;
 
780
                        }
 
781
                        IOErr("whiteout name too long %.*s\n",
 
782
                              str->len, str->name);
 
783
                        err = -EIO;
 
784
                        break;
 
785
                }
 
786
        }
 
787
        __putname(wh_name.name);
 
788
 
 
789
 out:
 
790
        TraceErr(err);
 
791
        return err;
 
792
}
 
793
 
 
794
struct del_wh_children_args {
 
795
        int *errp;
 
796
        struct aufs_nhash *whlist;
 
797
        struct dentry *hidden_parent;
 
798
        aufs_bindex_t bindex;
 
799
        struct lkup_args *lkup;
 
800
};
 
801
 
 
802
static void call_del_wh_children(void *args)
 
803
{
 
804
        struct del_wh_children_args *a = args;
 
805
        *a->errp = del_wh_children(a->whlist, a->hidden_parent, a->bindex,
 
806
                                   a->lkup);
 
807
}
 
808
 
 
809
/* ---------------------------------------------------------------------- */
 
810
 
 
811
/*
 
812
 * rmdir the whiteouted temporary named dir @hidden_dentry.
 
813
 * @whlist: whiteouted children.
 
814
 */
 
815
int rmdir_whtmp(struct dentry *hidden_dentry, struct aufs_nhash *whlist,
 
816
                aufs_bindex_t bindex, struct inode *dir, struct inode *inode)
 
817
{
 
818
        int err;
 
819
        struct inode *hidden_inode, *hidden_dir;
 
820
        struct lkup_args lkup;
 
821
        struct super_block *sb;
 
822
 
 
823
        LKTRTrace("hd %.*s, b%d, i%lu\n",
 
824
                  DLNPair(hidden_dentry), bindex, dir->i_ino);
 
825
        IMustLock(dir);
 
826
        IiMustAnyLock(dir);
 
827
        hidden_dir = hidden_dentry->d_parent->d_inode;
 
828
        IMustLock(hidden_dir);
 
829
 
 
830
        sb = inode->i_sb;
 
831
        lkup.nfsmnt = au_nfsmnt(sb, bindex);
 
832
        lkup.dlgt = need_dlgt(sb);
 
833
        hidden_inode = hidden_dentry->d_inode;
 
834
        DEBUG_ON(hidden_inode != au_h_iptr_i(inode, bindex));
 
835
        hdir2_lock(hidden_inode, inode, bindex);
 
836
        if (!au_test_perm(hidden_inode, MAY_EXEC | MAY_WRITE, lkup.dlgt))
 
837
                err = del_wh_children(whlist, hidden_dentry, bindex, &lkup);
 
838
        else {
 
839
                // ugly
 
840
                int dlgt = lkup.dlgt;
 
841
                struct del_wh_children_args args = {
 
842
                        .errp           = &err,
 
843
                        .whlist         = whlist,
 
844
                        .hidden_parent  = hidden_dentry,
 
845
                        .bindex         = bindex,
 
846
                        .lkup           = &lkup
 
847
                };
 
848
 
 
849
                lkup.dlgt = 0;
 
850
                au_wkq_wait(call_del_wh_children, &args, /*dlgt*/0);
 
851
                lkup.dlgt = dlgt;
 
852
        }
 
853
        hdir_unlock(hidden_inode, inode, bindex);
 
854
 
 
855
        if (!err) {
 
856
                err = vfsub_rmdir(hidden_dir, hidden_dentry, lkup.dlgt);
 
857
                //d_drop(hidden_dentry);
 
858
                //if (LktrCond) err = -1;
 
859
        }
 
860
 
 
861
        if (!err) {
 
862
                if (ibstart(dir) == bindex) {
 
863
                        au_cpup_attr_timesizes(dir);
 
864
                        //au_cpup_attr_nlink(dir);
 
865
                        dir->i_nlink--;
 
866
                }
 
867
                return 0; /* success */
 
868
        }
 
869
 
 
870
        Warn("failed removing %.*s(%d), ignored\n",
 
871
             DLNPair(hidden_dentry), err);
 
872
        return err;
 
873
}
 
874
 
 
875
static void do_rmdir_whtmp(void *arg)
 
876
{
 
877
        int err;
 
878
        struct rmdir_whtmp_arg *a = arg;
 
879
        struct super_block *sb;
 
880
 
 
881
        LKTRTrace("%.*s, b%d, dir i%lu\n",
 
882
                  DLNPair(a->h_dentry), a->bindex, a->dir->i_ino);
 
883
 
 
884
        i_lock(a->dir);
 
885
        sb = a->dir->i_sb;
 
886
        si_read_lock(sb);
 
887
        err = test_ro(sb, a->bindex, NULL);
 
888
        if (!err) {
 
889
                struct inode *hidden_dir = a->h_dentry->d_parent->d_inode;
 
890
 
 
891
                ii_write_lock_child(a->inode);
 
892
                ii_write_lock_parent(a->dir);
 
893
                hdir_lock(hidden_dir, a->dir, a->bindex);
 
894
                err = rmdir_whtmp(a->h_dentry, &a->whlist, a->bindex,
 
895
                                  a->dir, a->inode);
 
896
                hdir_unlock(hidden_dir, a->dir, a->bindex);
 
897
                ii_write_unlock(a->dir);
 
898
                ii_write_unlock(a->inode);
 
899
        }
 
900
        dput(a->h_dentry);
 
901
        nhash_fin(&a->whlist);
 
902
        iput(a->inode);
 
903
        si_read_unlock(sb);
 
904
        i_unlock(a->dir);
 
905
        iput(a->dir);
 
906
        kfree(arg);
 
907
        if (unlikely(err))
 
908
                IOErr("err %d\n", err);
 
909
}
 
910
 
 
911
void kick_rmdir_whtmp(struct dentry *hidden_dentry, struct aufs_nhash *whlist,
 
912
                      aufs_bindex_t bindex, struct inode *dir,
 
913
                      struct inode *inode, struct rmdir_whtmp_arg *arg)
 
914
{
 
915
        LKTRTrace("%.*s\n", DLNPair(hidden_dentry));
 
916
        IMustLock(dir);
 
917
 
 
918
        // all post-process will be done in do_rmdir_whtmp().
 
919
        arg->h_dentry = dget(hidden_dentry);
 
920
        nhash_init(&arg->whlist);
 
921
        nhash_move(&arg->whlist, whlist);
 
922
        arg->bindex = bindex;
 
923
        arg->dir = igrab(dir);
 
924
        arg->inode = igrab(inode);
 
925
 
 
926
        au_wkq_nowait(do_rmdir_whtmp, arg, /*dlgt*/0);
 
927
}