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

« back to all changes in this revision

Viewing changes to ubuntu/aufs/whout.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
 * whiteout for logical deletion and opaque directory
 
21
 */
 
22
 
 
23
#include <linux/fs.h>
 
24
#include "aufs.h"
 
25
 
 
26
#define WH_MASK                 S_IRUGO
 
27
 
 
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 kfree().
 
44
 */
 
45
int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
 
46
{
 
47
        char *p;
 
48
 
 
49
        if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
 
50
                return -ENAMETOOLONG;
 
51
 
 
52
        wh->len = name->len + AUFS_WH_PFX_LEN;
 
53
        p = kmalloc(wh->len, GFP_NOFS);
 
54
        wh->name = p;
 
55
        if (p) {
 
56
                memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
 
57
                memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
 
58
                /* smp_mb(); */
 
59
                return 0;
 
60
        }
 
61
        return -ENOMEM;
 
62
}
 
63
 
 
64
/* ---------------------------------------------------------------------- */
 
65
 
 
66
/*
 
67
 * test if the @wh_name exists under @h_parent.
 
68
 * @try_sio specifies the necessary of super-io.
 
69
 */
 
70
int au_wh_test(struct dentry *h_parent, struct qstr *wh_name,
 
71
               struct au_branch *br, int try_sio)
 
72
{
 
73
        int err;
 
74
        struct dentry *wh_dentry;
 
75
        struct inode *h_dir;
 
76
 
 
77
        h_dir = h_parent->d_inode;
 
78
        if (!try_sio)
 
79
                wh_dentry = au_lkup_one(wh_name, h_parent, br, /*nd*/NULL);
 
80
        else
 
81
                wh_dentry = au_sio_lkup_one(wh_name, h_parent, br);
 
82
        err = PTR_ERR(wh_dentry);
 
83
        if (IS_ERR(wh_dentry))
 
84
                goto out;
 
85
 
 
86
        err = 0;
 
87
        if (!wh_dentry->d_inode)
 
88
                goto out_wh; /* success */
 
89
 
 
90
        err = 1;
 
91
        if (S_ISREG(wh_dentry->d_inode->i_mode))
 
92
                goto out_wh; /* success */
 
93
 
 
94
        err = -EIO;
 
95
        AuIOErr("%.*s Invalid whiteout entry type 0%o.\n",
 
96
                AuDLNPair(wh_dentry), wh_dentry->d_inode->i_mode);
 
97
 
 
98
 out_wh:
 
99
        dput(wh_dentry);
 
100
 out:
 
101
        return err;
 
102
}
 
103
 
 
104
/*
 
105
 * test if the @h_dentry sets opaque or not.
 
106
 */
 
107
int au_diropq_test(struct dentry *h_dentry, struct au_branch *br)
 
108
{
 
109
        int err;
 
110
        struct inode *h_dir;
 
111
 
 
112
        h_dir = h_dentry->d_inode;
 
113
        err = au_wh_test(h_dentry, &diropq_name, br,
 
114
                         au_test_h_perm_sio(h_dir, MAY_EXEC));
 
115
        return err;
 
116
}
 
117
 
 
118
/*
 
119
 * returns a negative dentry whose name is unique and temporary.
 
120
 */
 
121
struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
 
122
                             struct qstr *prefix)
 
123
{
 
124
        struct dentry *dentry;
 
125
        int i;
 
126
        char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN_MIN + 1],
 
127
                *name, *p;
 
128
        static unsigned short cnt;
 
129
        struct qstr qs;
 
130
 
 
131
        BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
 
132
 
 
133
        name = defname;
 
134
        qs.len = sizeof(defname) - DNAME_INLINE_LEN_MIN + prefix->len - 1;
 
135
        if (unlikely(prefix->len > DNAME_INLINE_LEN_MIN)) {
 
136
                dentry = ERR_PTR(-ENAMETOOLONG);
 
137
                if (unlikely(qs.len > NAME_MAX))
 
138
                        goto out;
 
139
                dentry = ERR_PTR(-ENOMEM);
 
140
                name = kmalloc(qs.len + 1, GFP_NOFS);
 
141
                if (unlikely(!name))
 
142
                        goto out;
 
143
        }
 
144
 
 
145
        /* doubly whiteout-ed */
 
146
        memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
 
147
        p = name + AUFS_WH_PFX_LEN * 2;
 
148
        memcpy(p, prefix->name, prefix->len);
 
149
        p += prefix->len;
 
150
        *p++ = '.';
 
151
        AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
 
152
 
 
153
        qs.name = name;
 
154
        for (i = 0; i < 3; i++) {
 
155
                sprintf(p, "%.*d", AUFS_WH_TMP_LEN, cnt++);
 
156
                dentry = au_sio_lkup_one(&qs, h_parent, br);
 
157
                if (IS_ERR(dentry) || !dentry->d_inode)
 
158
                        goto out_name;
 
159
                dput(dentry);
 
160
        }
 
161
        /* pr_warning("could not get random name\n"); */
 
162
        dentry = ERR_PTR(-EEXIST);
 
163
        AuDbg("%.*s\n", AuLNPair(&qs));
 
164
        BUG();
 
165
 
 
166
 out_name:
 
167
        if (name != defname)
 
168
                kfree(name);
 
169
 out:
 
170
        AuTraceErrPtr(dentry);
 
171
        return dentry;
 
172
}
 
173
 
 
174
/*
 
175
 * rename the @h_dentry on @br to the whiteouted temporary name.
 
176
 */
 
177
int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
 
178
{
 
179
        int err;
 
180
        struct path h_path = {
 
181
                .mnt = br->br_mnt
 
182
        };
 
183
        struct inode *h_dir;
 
184
        struct dentry *h_parent;
 
185
 
 
186
        h_parent = h_dentry->d_parent; /* dir inode is locked */
 
187
        h_dir = h_parent->d_inode;
 
188
        IMustLock(h_dir);
 
189
 
 
190
        h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
 
191
        err = PTR_ERR(h_path.dentry);
 
192
        if (IS_ERR(h_path.dentry))
 
193
                goto out;
 
194
 
 
195
        /* under the same dir, no need to lock_rename() */
 
196
        err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path);
 
197
        AuTraceErr(err);
 
198
        dput(h_path.dentry);
 
199
 
 
200
 out:
 
201
        AuTraceErr(err);
 
202
        return err;
 
203
}
 
204
 
 
205
/* ---------------------------------------------------------------------- */
 
206
/*
 
207
 * functions for removing a whiteout
 
208
 */
 
209
 
 
210
static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
 
211
{
 
212
        int force;
 
213
 
 
214
        /*
 
215
         * forces superio when the dir has a sticky bit.
 
216
         * this may be a violation of unix fs semantics.
 
217
         */
 
218
        force = (h_dir->i_mode & S_ISVTX)
 
219
                && h_path->dentry->d_inode->i_uid != current_fsuid();
 
220
        return vfsub_unlink(h_dir, h_path, force);
 
221
}
 
222
 
 
223
int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
 
224
                        struct dentry *dentry)
 
225
{
 
226
        int err;
 
227
 
 
228
        err = do_unlink_wh(h_dir, h_path);
 
229
        if (!err && dentry)
 
230
                au_set_dbwh(dentry, -1);
 
231
 
 
232
        return err;
 
233
}
 
234
 
 
235
static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
 
236
                          struct au_branch *br)
 
237
{
 
238
        int err;
 
239
        struct path h_path = {
 
240
                .mnt = br->br_mnt
 
241
        };
 
242
 
 
243
        err = 0;
 
244
        h_path.dentry = au_lkup_one(wh, h_parent, br, /*nd*/NULL);
 
245
        if (IS_ERR(h_path.dentry))
 
246
                err = PTR_ERR(h_path.dentry);
 
247
        else {
 
248
                if (h_path.dentry->d_inode
 
249
                    && S_ISREG(h_path.dentry->d_inode->i_mode))
 
250
                        err = do_unlink_wh(h_parent->d_inode, &h_path);
 
251
                dput(h_path.dentry);
 
252
        }
 
253
 
 
254
        return err;
 
255
}
 
256
 
 
257
/* ---------------------------------------------------------------------- */
 
258
/*
 
259
 * initialize/clean whiteout for a branch
 
260
 */
 
261
 
 
262
static void au_wh_clean(struct inode *h_dir, struct path *whpath,
 
263
                        const int isdir)
 
264
{
 
265
        int err;
 
266
 
 
267
        if (!whpath->dentry->d_inode)
 
268
                return;
 
269
 
 
270
        err = mnt_want_write(whpath->mnt);
 
271
        if (!err) {
 
272
                if (isdir)
 
273
                        err = vfsub_rmdir(h_dir, whpath);
 
274
                else
 
275
                        err = vfsub_unlink(h_dir, whpath, /*force*/0);
 
276
                mnt_drop_write(whpath->mnt);
 
277
        }
 
278
        if (unlikely(err))
 
279
                pr_warning("failed removing %.*s (%d), ignored.\n",
 
280
                           AuDLNPair(whpath->dentry), err);
 
281
}
 
282
 
 
283
static int test_linkable(struct dentry *h_root)
 
284
{
 
285
        struct inode *h_dir = h_root->d_inode;
 
286
 
 
287
        if (h_dir->i_op->link)
 
288
                return 0;
 
289
 
 
290
        pr_err("%.*s (%s) doesn't support link(2), use noplink and rw+nolwh\n",
 
291
               AuDLNPair(h_root), au_sbtype(h_root->d_sb));
 
292
        return -ENOSYS;
 
293
}
 
294
 
 
295
/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
 
296
static int au_whdir(struct inode *h_dir, struct path *path)
 
297
{
 
298
        int err;
 
299
 
 
300
        err = -EEXIST;
 
301
        if (!path->dentry->d_inode) {
 
302
                int mode = S_IRWXU;
 
303
 
 
304
                if (au_test_nfs(path->dentry->d_sb))
 
305
                        mode |= S_IXUGO;
 
306
                err = mnt_want_write(path->mnt);
 
307
                if (!err) {
 
308
                        err = vfsub_mkdir(h_dir, path, mode);
 
309
                        mnt_drop_write(path->mnt);
 
310
                }
 
311
        } else if (S_ISDIR(path->dentry->d_inode->i_mode))
 
312
                err = 0;
 
313
        else
 
314
                pr_err("unknown %.*s exists\n", AuDLNPair(path->dentry));
 
315
 
 
316
        return err;
 
317
}
 
318
 
 
319
struct au_wh_base {
 
320
        const struct qstr *name;
 
321
        struct dentry *dentry;
 
322
};
 
323
 
 
324
static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
 
325
                          struct path *h_path)
 
326
{
 
327
        h_path->dentry = base[AuBrWh_BASE].dentry;
 
328
        au_wh_clean(h_dir, h_path, /*isdir*/0);
 
329
        h_path->dentry = base[AuBrWh_PLINK].dentry;
 
330
        au_wh_clean(h_dir, h_path, /*isdir*/1);
 
331
        h_path->dentry = base[AuBrWh_ORPH].dentry;
 
332
        au_wh_clean(h_dir, h_path, /*isdir*/1);
 
333
}
 
334
 
 
335
/*
 
336
 * returns tri-state,
 
337
 * minus: error, caller should print the mesage
 
338
 * zero: succuess
 
339
 * plus: error, caller should NOT print the mesage
 
340
 */
 
341
static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
 
342
                                int do_plink, struct au_wh_base base[],
 
343
                                struct path *h_path)
 
344
{
 
345
        int err;
 
346
        struct inode *h_dir;
 
347
 
 
348
        h_dir = h_root->d_inode;
 
349
        h_path->dentry = base[AuBrWh_BASE].dentry;
 
350
        au_wh_clean(h_dir, h_path, /*isdir*/0);
 
351
        h_path->dentry = base[AuBrWh_PLINK].dentry;
 
352
        if (do_plink) {
 
353
                err = test_linkable(h_root);
 
354
                if (unlikely(err)) {
 
355
                        err = 1;
 
356
                        goto out;
 
357
                }
 
358
 
 
359
                err = au_whdir(h_dir, h_path);
 
360
                if (unlikely(err))
 
361
                        goto out;
 
362
                wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
 
363
        } else
 
364
                au_wh_clean(h_dir, h_path, /*isdir*/1);
 
365
        h_path->dentry = base[AuBrWh_ORPH].dentry;
 
366
        err = au_whdir(h_dir, h_path);
 
367
        if (unlikely(err))
 
368
                goto out;
 
369
        wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
 
370
 
 
371
 out:
 
372
        return err;
 
373
}
 
374
 
 
375
/*
 
376
 * for the moment, aufs supports the branch filesystem which does not support
 
377
 * link(2). testing on FAT which does not support i_op->setattr() fully either,
 
378
 * copyup failed. finally, such filesystem will not be used as the writable
 
379
 * branch.
 
380
 *
 
381
 * returns tri-state, see above.
 
382
 */
 
383
static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
 
384
                         int do_plink, struct au_wh_base base[],
 
385
                         struct path *h_path)
 
386
{
 
387
        int err;
 
388
        struct inode *h_dir;
 
389
 
 
390
        WbrWhMustWriteLock(wbr);
 
391
 
 
392
        err = test_linkable(h_root);
 
393
        if (unlikely(err)) {
 
394
                err = 1;
 
395
                goto out;
 
396
        }
 
397
 
 
398
        /*
 
399
         * todo: should this create be done in /sbin/mount.aufs helper?
 
400
         */
 
401
        err = -EEXIST;
 
402
        h_dir = h_root->d_inode;
 
403
        if (!base[AuBrWh_BASE].dentry->d_inode) {
 
404
                err = mnt_want_write(h_path->mnt);
 
405
                if (!err) {
 
406
                        h_path->dentry = base[AuBrWh_BASE].dentry;
 
407
                        err = vfsub_create(h_dir, h_path, WH_MASK);
 
408
                        mnt_drop_write(h_path->mnt);
 
409
                }
 
410
        } else if (S_ISREG(base[AuBrWh_BASE].dentry->d_inode->i_mode))
 
411
                err = 0;
 
412
        else
 
413
                pr_err("unknown %.*s/%.*s exists\n",
 
414
                       AuDLNPair(h_root), AuDLNPair(base[AuBrWh_BASE].dentry));
 
415
        if (unlikely(err))
 
416
                goto out;
 
417
 
 
418
        h_path->dentry = base[AuBrWh_PLINK].dentry;
 
419
        if (do_plink) {
 
420
                err = au_whdir(h_dir, h_path);
 
421
                if (unlikely(err))
 
422
                        goto out;
 
423
                wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
 
424
        } else
 
425
                au_wh_clean(h_dir, h_path, /*isdir*/1);
 
426
        wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
 
427
 
 
428
        h_path->dentry = base[AuBrWh_ORPH].dentry;
 
429
        err = au_whdir(h_dir, h_path);
 
430
        if (unlikely(err))
 
431
                goto out;
 
432
        wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
 
433
 
 
434
 out:
 
435
        return err;
 
436
}
 
437
 
 
438
/*
 
439
 * initialize the whiteout base file/dir for @br.
 
440
 */
 
441
int au_wh_init(struct dentry *h_root, struct au_branch *br,
 
442
               struct super_block *sb)
 
443
{
 
444
        int err, i;
 
445
        const unsigned char do_plink
 
446
                = !!au_opt_test(au_mntflags(sb), PLINK);
 
447
        struct path path = {
 
448
                .mnt = br->br_mnt
 
449
        };
 
450
        struct inode *h_dir;
 
451
        struct au_wbr *wbr = br->br_wbr;
 
452
        static const struct qstr base_name[] = {
 
453
                [AuBrWh_BASE] = {
 
454
                        .name   = AUFS_BASE_NAME,
 
455
                        .len    = sizeof(AUFS_BASE_NAME) - 1
 
456
                },
 
457
                [AuBrWh_PLINK] = {
 
458
                        .name   = AUFS_PLINKDIR_NAME,
 
459
                        .len    = sizeof(AUFS_PLINKDIR_NAME) - 1
 
460
                },
 
461
                [AuBrWh_ORPH] = {
 
462
                        .name   = AUFS_ORPHDIR_NAME,
 
463
                        .len    = sizeof(AUFS_ORPHDIR_NAME) - 1
 
464
                }
 
465
        };
 
466
        struct au_wh_base base[] = {
 
467
                [AuBrWh_BASE] = {
 
468
                        .name   = base_name + AuBrWh_BASE,
 
469
                        .dentry = NULL
 
470
                },
 
471
                [AuBrWh_PLINK] = {
 
472
                        .name   = base_name + AuBrWh_PLINK,
 
473
                        .dentry = NULL
 
474
                },
 
475
                [AuBrWh_ORPH] = {
 
476
                        .name   = base_name + AuBrWh_ORPH,
 
477
                        .dentry = NULL
 
478
                }
 
479
        };
 
480
 
 
481
        if (wbr)
 
482
                WbrWhMustWriteLock(wbr);
 
483
 
 
484
        h_dir = h_root->d_inode;
 
485
        for (i = 0; i < AuBrWh_Last; i++) {
 
486
                /* doubly whiteouted */
 
487
                struct dentry *d;
 
488
 
 
489
                d = au_wh_lkup(h_root, (void *)base[i].name, br);
 
490
                err = PTR_ERR(d);
 
491
                if (IS_ERR(d))
 
492
                        goto out;
 
493
 
 
494
                base[i].dentry = d;
 
495
                AuDebugOn(wbr
 
496
                          && wbr->wbr_wh[i]
 
497
                          && wbr->wbr_wh[i] != base[i].dentry);
 
498
        }
 
499
 
 
500
        if (wbr)
 
501
                for (i = 0; i < AuBrWh_Last; i++) {
 
502
                        dput(wbr->wbr_wh[i]);
 
503
                        wbr->wbr_wh[i] = NULL;
 
504
                }
 
505
 
 
506
        err = 0;
 
507
 
 
508
        switch (br->br_perm) {
 
509
        case AuBrPerm_RO:
 
510
        case AuBrPerm_ROWH:
 
511
        case AuBrPerm_RR:
 
512
        case AuBrPerm_RRWH:
 
513
                au_wh_init_ro(h_dir, base, &path);
 
514
                break;
 
515
 
 
516
        case AuBrPerm_RWNoLinkWH:
 
517
                err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
 
518
                if (err > 0)
 
519
                        goto out;
 
520
                else if (err)
 
521
                        goto out_err;
 
522
                break;
 
523
 
 
524
        case AuBrPerm_RW:
 
525
                err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
 
526
                if (err > 0)
 
527
                        goto out;
 
528
                else if (err)
 
529
                        goto out_err;
 
530
                break;
 
531
 
 
532
        default:
 
533
                BUG();
 
534
        }
 
535
        goto out; /* success */
 
536
 
 
537
 out_err:
 
538
        pr_err("an error(%d) on the writable branch %.*s(%s)\n",
 
539
               err, AuDLNPair(h_root), au_sbtype(h_root->d_sb));
 
540
 out:
 
541
        for (i = 0; i < AuBrWh_Last; i++)
 
542
                dput(base[i].dentry);
 
543
        return err;
 
544
}
 
545
 
 
546
/* ---------------------------------------------------------------------- */
 
547
/*
 
548
 * whiteouts are all hard-linked usually.
 
549
 * when its link count reaches a ceiling, we create a new whiteout base
 
550
 * asynchronously.
 
551
 */
 
552
 
 
553
struct reinit_br_wh {
 
554
        struct super_block *sb;
 
555
        struct au_branch *br;
 
556
};
 
557
 
 
558
static void reinit_br_wh(void *arg)
 
559
{
 
560
        int err;
 
561
        aufs_bindex_t bindex;
 
562
        struct path h_path;
 
563
        struct reinit_br_wh *a = arg;
 
564
        struct au_wbr *wbr;
 
565
        struct inode *dir;
 
566
        struct dentry *h_root;
 
567
        struct au_hinode *hdir;
 
568
 
 
569
        err = 0;
 
570
        wbr = a->br->br_wbr;
 
571
        /* big aufs lock */
 
572
        si_noflush_write_lock(a->sb);
 
573
        if (!au_br_writable(a->br->br_perm))
 
574
                goto out;
 
575
        bindex = au_br_index(a->sb, a->br->br_id);
 
576
        if (unlikely(bindex < 0))
 
577
                goto out;
 
578
 
 
579
        di_read_lock_parent(a->sb->s_root, AuLock_IR);
 
580
        dir = a->sb->s_root->d_inode;
 
581
        hdir = au_hi(dir, bindex);
 
582
        h_root = au_h_dptr(a->sb->s_root, bindex);
 
583
 
 
584
        au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
 
585
        wbr_wh_write_lock(wbr);
 
586
        err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
 
587
                          h_root, a->br);
 
588
        if (!err) {
 
589
                err = mnt_want_write(a->br->br_mnt);
 
590
                if (!err) {
 
591
                        h_path.dentry = wbr->wbr_whbase;
 
592
                        h_path.mnt = a->br->br_mnt;
 
593
                        err = vfsub_unlink(hdir->hi_inode, &h_path, /*force*/0);
 
594
                        mnt_drop_write(a->br->br_mnt);
 
595
                }
 
596
        } else {
 
597
                pr_warning("%.*s is moved, ignored\n",
 
598
                           AuDLNPair(wbr->wbr_whbase));
 
599
                err = 0;
 
600
        }
 
601
        dput(wbr->wbr_whbase);
 
602
        wbr->wbr_whbase = NULL;
 
603
        if (!err)
 
604
                err = au_wh_init(h_root, a->br, a->sb);
 
605
        wbr_wh_write_unlock(wbr);
 
606
        au_hin_imtx_unlock(hdir);
 
607
        di_read_unlock(a->sb->s_root, AuLock_IR);
 
608
 
 
609
 out:
 
610
        if (wbr)
 
611
                atomic_dec(&wbr->wbr_wh_running);
 
612
        atomic_dec(&a->br->br_count);
 
613
        au_nwt_done(&au_sbi(a->sb)->si_nowait);
 
614
        si_write_unlock(a->sb);
 
615
        kfree(arg);
 
616
        if (unlikely(err))
 
617
                AuIOErr("err %d\n", err);
 
618
}
 
619
 
 
620
static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
 
621
{
 
622
        int do_dec, wkq_err;
 
623
        struct reinit_br_wh *arg;
 
624
 
 
625
        do_dec = 1;
 
626
        if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
 
627
                goto out;
 
628
 
 
629
        /* ignore ENOMEM */
 
630
        arg = kmalloc(sizeof(*arg), GFP_NOFS);
 
631
        if (arg) {
 
632
                /*
 
633
                 * dec(wh_running), kfree(arg) and dec(br_count)
 
634
                 * in reinit function
 
635
                 */
 
636
                arg->sb = sb;
 
637
                arg->br = br;
 
638
                atomic_inc(&br->br_count);
 
639
                wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb);
 
640
                if (unlikely(wkq_err)) {
 
641
                        atomic_dec(&br->br_wbr->wbr_wh_running);
 
642
                        atomic_dec(&br->br_count);
 
643
                        kfree(arg);
 
644
                }
 
645
                do_dec = 0;
 
646
        }
 
647
 
 
648
 out:
 
649
        if (do_dec)
 
650
                atomic_dec(&br->br_wbr->wbr_wh_running);
 
651
}
 
652
 
 
653
/* ---------------------------------------------------------------------- */
 
654
 
 
655
/*
 
656
 * create the whiteout @wh.
 
657
 */
 
658
static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
 
659
                             struct dentry *wh)
 
660
{
 
661
        int err;
 
662
        struct path h_path = {
 
663
                .dentry = wh
 
664
        };
 
665
        struct au_branch *br;
 
666
        struct au_wbr *wbr;
 
667
        struct dentry *h_parent;
 
668
        struct inode *h_dir;
 
669
 
 
670
        h_parent = wh->d_parent; /* dir inode is locked */
 
671
        h_dir = h_parent->d_inode;
 
672
        IMustLock(h_dir);
 
673
 
 
674
        br = au_sbr(sb, bindex);
 
675
        h_path.mnt = br->br_mnt;
 
676
        wbr = br->br_wbr;
 
677
        wbr_wh_read_lock(wbr);
 
678
        if (wbr->wbr_whbase) {
 
679
                err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path);
 
680
                if (!err || err != -EMLINK)
 
681
                        goto out;
 
682
 
 
683
                /* link count full. re-initialize br_whbase. */
 
684
                kick_reinit_br_wh(sb, br);
 
685
        }
 
686
 
 
687
        /* return this error in this context */
 
688
        err = vfsub_create(h_dir, &h_path, WH_MASK);
 
689
 
 
690
 out:
 
691
        wbr_wh_read_unlock(wbr);
 
692
        return err;
 
693
}
 
694
 
 
695
/* ---------------------------------------------------------------------- */
 
696
 
 
697
/*
 
698
 * create or remove the diropq.
 
699
 */
 
700
static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
 
701
                                unsigned int flags)
 
702
{
 
703
        struct dentry *opq_dentry, *h_dentry;
 
704
        struct super_block *sb;
 
705
        struct au_branch *br;
 
706
        int err;
 
707
 
 
708
        sb = dentry->d_sb;
 
709
        br = au_sbr(sb, bindex);
 
710
        h_dentry = au_h_dptr(dentry, bindex);
 
711
        opq_dentry = au_lkup_one(&diropq_name, h_dentry, br, /*nd*/NULL);
 
712
        if (IS_ERR(opq_dentry))
 
713
                goto out;
 
714
 
 
715
        if (au_ftest_diropq(flags, CREATE)) {
 
716
                err = link_or_create_wh(sb, bindex, opq_dentry);
 
717
                if (!err) {
 
718
                        au_set_dbdiropq(dentry, bindex);
 
719
                        goto out; /* success */
 
720
                }
 
721
        } else {
 
722
                struct path tmp = {
 
723
                        .dentry = opq_dentry,
 
724
                        .mnt    = br->br_mnt
 
725
                };
 
726
                err = do_unlink_wh(au_h_iptr(dentry->d_inode, bindex), &tmp);
 
727
                if (!err)
 
728
                        au_set_dbdiropq(dentry, -1);
 
729
        }
 
730
        dput(opq_dentry);
 
731
        opq_dentry = ERR_PTR(err);
 
732
 
 
733
 out:
 
734
        return opq_dentry;
 
735
}
 
736
 
 
737
struct do_diropq_args {
 
738
        struct dentry **errp;
 
739
        struct dentry *dentry;
 
740
        aufs_bindex_t bindex;
 
741
        unsigned int flags;
 
742
};
 
743
 
 
744
static void call_do_diropq(void *args)
 
745
{
 
746
        struct do_diropq_args *a = args;
 
747
        *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
 
748
}
 
749
 
 
750
struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
 
751
                             unsigned int flags)
 
752
{
 
753
        struct dentry *diropq, *h_dentry;
 
754
 
 
755
        h_dentry = au_h_dptr(dentry, bindex);
 
756
        if (!au_test_h_perm_sio(h_dentry->d_inode, MAY_EXEC | MAY_WRITE))
 
757
                diropq = do_diropq(dentry, bindex, flags);
 
758
        else {
 
759
                int wkq_err;
 
760
                struct do_diropq_args args = {
 
761
                        .errp           = &diropq,
 
762
                        .dentry         = dentry,
 
763
                        .bindex         = bindex,
 
764
                        .flags          = flags
 
765
                };
 
766
 
 
767
                wkq_err = au_wkq_wait(call_do_diropq, &args);
 
768
                if (unlikely(wkq_err))
 
769
                        diropq = ERR_PTR(wkq_err);
 
770
        }
 
771
 
 
772
        return diropq;
 
773
}
 
774
 
 
775
/* ---------------------------------------------------------------------- */
 
776
 
 
777
/*
 
778
 * lookup whiteout dentry.
 
779
 * @h_parent: lower parent dentry which must exist and be locked
 
780
 * @base_name: name of dentry which will be whiteouted
 
781
 * returns dentry for whiteout.
 
782
 */
 
783
struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
 
784
                          struct au_branch *br)
 
785
{
 
786
        int err;
 
787
        struct qstr wh_name;
 
788
        struct dentry *wh_dentry;
 
789
 
 
790
        err = au_wh_name_alloc(&wh_name, base_name);
 
791
        wh_dentry = ERR_PTR(err);
 
792
        if (!err) {
 
793
                wh_dentry = au_lkup_one(&wh_name, h_parent, br, /*nd*/NULL);
 
794
                kfree(wh_name.name);
 
795
        }
 
796
        return wh_dentry;
 
797
}
 
798
 
 
799
/*
 
800
 * link/create a whiteout for @dentry on @bindex.
 
801
 */
 
802
struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
 
803
                            struct dentry *h_parent)
 
804
{
 
805
        struct dentry *wh_dentry;
 
806
        struct super_block *sb;
 
807
        int err;
 
808
 
 
809
        sb = dentry->d_sb;
 
810
        wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
 
811
        if (!IS_ERR(wh_dentry) && !wh_dentry->d_inode) {
 
812
                err = link_or_create_wh(sb, bindex, wh_dentry);
 
813
                if (!err)
 
814
                        au_set_dbwh(dentry, bindex);
 
815
                else {
 
816
                        dput(wh_dentry);
 
817
                        wh_dentry = ERR_PTR(err);
 
818
                }
 
819
        }
 
820
 
 
821
        return wh_dentry;
 
822
}
 
823
 
 
824
/* ---------------------------------------------------------------------- */
 
825
 
 
826
/* Delete all whiteouts in this directory on branch bindex. */
 
827
static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
 
828
                           aufs_bindex_t bindex, struct au_branch *br)
 
829
{
 
830
        int err;
 
831
        unsigned long ul, n;
 
832
        struct qstr wh_name;
 
833
        char *p;
 
834
        struct hlist_head *head;
 
835
        struct au_vdir_wh *tpos;
 
836
        struct hlist_node *pos;
 
837
        struct au_vdir_destr *str;
 
838
 
 
839
        err = -ENOMEM;
 
840
        p = __getname();
 
841
        wh_name.name = p;
 
842
        if (unlikely(!wh_name.name))
 
843
                goto out;
 
844
 
 
845
        err = 0;
 
846
        memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
 
847
        p += AUFS_WH_PFX_LEN;
 
848
        n = whlist->nh_num;
 
849
        head = whlist->nh_head;
 
850
        for (ul = 0; !err && ul < n; ul++, head++) {
 
851
                hlist_for_each_entry(tpos, pos, head, wh_hash) {
 
852
                        if (tpos->wh_bindex != bindex)
 
853
                                continue;
 
854
 
 
855
                        str = &tpos->wh_str;
 
856
                        if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
 
857
                                memcpy(p, str->name, str->len);
 
858
                                wh_name.len = AUFS_WH_PFX_LEN + str->len;
 
859
                                err = unlink_wh_name(h_dentry, &wh_name, br);
 
860
                                if (!err)
 
861
                                        continue;
 
862
                                break;
 
863
                        }
 
864
                        AuIOErr("whiteout name too long %.*s\n",
 
865
                                str->len, str->name);
 
866
                        err = -EIO;
 
867
                        break;
 
868
                }
 
869
        }
 
870
        __putname(wh_name.name);
 
871
 
 
872
 out:
 
873
        return err;
 
874
}
 
875
 
 
876
struct del_wh_children_args {
 
877
        int *errp;
 
878
        struct dentry *h_dentry;
 
879
        struct au_nhash *whlist;
 
880
        aufs_bindex_t bindex;
 
881
        struct au_branch *br;
 
882
};
 
883
 
 
884
static void call_del_wh_children(void *args)
 
885
{
 
886
        struct del_wh_children_args *a = args;
 
887
        *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
 
888
}
 
889
 
 
890
/* ---------------------------------------------------------------------- */
 
891
 
 
892
struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
 
893
{
 
894
        struct au_whtmp_rmdir *whtmp;
 
895
        int err;
 
896
        unsigned int rdhash;
 
897
 
 
898
        SiMustAnyLock(sb);
 
899
 
 
900
        whtmp = kmalloc(sizeof(*whtmp), gfp);
 
901
        if (unlikely(!whtmp)) {
 
902
                whtmp = ERR_PTR(-ENOMEM);
 
903
                goto out;
 
904
        }
 
905
 
 
906
        whtmp->dir = NULL;
 
907
        whtmp->wh_dentry = NULL;
 
908
        /* no estimation for dir size */
 
909
        rdhash = au_sbi(sb)->si_rdhash;
 
910
        if (!rdhash)
 
911
                rdhash = AUFS_RDHASH_DEF;
 
912
        err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
 
913
        if (unlikely(err)) {
 
914
                kfree(whtmp);
 
915
                whtmp = ERR_PTR(err);
 
916
        }
 
917
 
 
918
 out:
 
919
        return whtmp;
 
920
}
 
921
 
 
922
void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
 
923
{
 
924
        dput(whtmp->wh_dentry);
 
925
        iput(whtmp->dir);
 
926
        au_nhash_wh_free(&whtmp->whlist);
 
927
        kfree(whtmp);
 
928
}
 
929
 
 
930
/*
 
931
 * rmdir the whiteouted temporary named dir @h_dentry.
 
932
 * @whlist: whiteouted children.
 
933
 */
 
934
int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
 
935
                   struct dentry *wh_dentry, struct au_nhash *whlist)
 
936
{
 
937
        int err;
 
938
        struct path h_tmp;
 
939
        struct inode *wh_inode, *h_dir;
 
940
        struct au_branch *br;
 
941
 
 
942
        h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
 
943
        IMustLock(h_dir);
 
944
 
 
945
        br = au_sbr(dir->i_sb, bindex);
 
946
        wh_inode = wh_dentry->d_inode;
 
947
        mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
 
948
 
 
949
        /*
 
950
         * someone else might change some whiteouts while we were sleeping.
 
951
         * it means this whlist may have an obsoleted entry.
 
952
         */
 
953
        if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
 
954
                err = del_wh_children(wh_dentry, whlist, bindex, br);
 
955
        else {
 
956
                int wkq_err;
 
957
                struct del_wh_children_args args = {
 
958
                        .errp           = &err,
 
959
                        .h_dentry       = wh_dentry,
 
960
                        .whlist         = whlist,
 
961
                        .bindex         = bindex,
 
962
                        .br             = br
 
963
                };
 
964
 
 
965
                wkq_err = au_wkq_wait(call_del_wh_children, &args);
 
966
                if (unlikely(wkq_err))
 
967
                        err = wkq_err;
 
968
        }
 
969
        mutex_unlock(&wh_inode->i_mutex);
 
970
 
 
971
        if (!err) {
 
972
                h_tmp.dentry = wh_dentry;
 
973
                h_tmp.mnt = br->br_mnt;
 
974
                err = vfsub_rmdir(h_dir, &h_tmp);
 
975
                /* d_drop(h_dentry); */
 
976
        }
 
977
 
 
978
        if (!err) {
 
979
                if (au_ibstart(dir) == bindex) {
 
980
                        au_cpup_attr_timesizes(dir);
 
981
                        drop_nlink(dir);
 
982
                }
 
983
                return 0; /* success */
 
984
        }
 
985
 
 
986
        pr_warning("failed removing %.*s(%d), ignored\n",
 
987
                   AuDLNPair(wh_dentry), err);
 
988
        return err;
 
989
}
 
990
 
 
991
static void call_rmdir_whtmp(void *args)
 
992
{
 
993
        int err;
 
994
        struct au_whtmp_rmdir *a = args;
 
995
        struct super_block *sb;
 
996
        struct dentry *h_parent;
 
997
        struct inode *h_dir;
 
998
        struct au_branch *br;
 
999
        struct au_hinode *hdir;
 
1000
 
 
1001
        /* rmdir by nfsd may cause deadlock with this i_mutex */
 
1002
        /* mutex_lock(&a->dir->i_mutex); */
 
1003
        sb = a->dir->i_sb;
 
1004
        si_noflush_read_lock(sb);
 
1005
        err = au_test_ro(sb, a->bindex, NULL);
 
1006
        if (unlikely(err))
 
1007
                goto out;
 
1008
 
 
1009
        err = -EIO;
 
1010
        br = au_sbr(sb, a->bindex);
 
1011
        ii_write_lock_parent(a->dir);
 
1012
        h_parent = dget_parent(a->wh_dentry);
 
1013
        h_dir = h_parent->d_inode;
 
1014
        hdir = au_hi(a->dir, a->bindex);
 
1015
        au_hin_imtx_lock_nested(hdir, AuLsc_I_PARENT);
 
1016
        err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent, br);
 
1017
        if (!err) {
 
1018
                err = mnt_want_write(br->br_mnt);
 
1019
                if (!err) {
 
1020
                        err = au_whtmp_rmdir(a->dir, a->bindex, a->wh_dentry,
 
1021
                                             &a->whlist);
 
1022
                        mnt_drop_write(br->br_mnt);
 
1023
                }
 
1024
        }
 
1025
        au_hin_imtx_unlock(hdir);
 
1026
        dput(h_parent);
 
1027
        ii_write_unlock(a->dir);
 
1028
 
 
1029
 out:
 
1030
        /* mutex_unlock(&a->dir->i_mutex); */
 
1031
        au_nwt_done(&au_sbi(sb)->si_nowait);
 
1032
        si_read_unlock(sb);
 
1033
        au_whtmp_rmdir_free(a);
 
1034
        if (unlikely(err))
 
1035
                AuIOErr("err %d\n", err);
 
1036
}
 
1037
 
 
1038
void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
 
1039
                         struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
 
1040
{
 
1041
        int wkq_err;
 
1042
 
 
1043
        IMustLock(dir);
 
1044
 
 
1045
        /* all post-process will be done in do_rmdir_whtmp(). */
 
1046
        args->dir = au_igrab(dir);
 
1047
        args->bindex = bindex;
 
1048
        args->wh_dentry = dget(wh_dentry);
 
1049
        wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, dir->i_sb);
 
1050
        if (unlikely(wkq_err)) {
 
1051
                pr_warning("rmdir error %.*s (%d), ignored\n",
 
1052
                           AuDLNPair(wh_dentry), wkq_err);
 
1053
                au_whtmp_rmdir_free(args);
 
1054
        }
 
1055
}