~ubuntu-branches/ubuntu/vivid/aufs/vivid

« back to all changes in this revision

Viewing changes to fs/aufs25/i_op_add.c

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005-2008 Junjiro Okajima
 
3
 *
 
4
 * This program, aufs is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 */
 
18
 
 
19
/*
 
20
 * inode operations (add entry)
 
21
 *
 
22
 * $Id: i_op_add.c,v 1.4 2008/05/04 23:54:53 sfjro Exp $
 
23
 */
 
24
 
 
25
#include "aufs.h"
 
26
 
 
27
/*
 
28
 * final procedure of adding a new entry, except link(2).
 
29
 * remove whiteout, instantiate, copyup the parent dir's times and size
 
30
 * and update version.
 
31
 * if it failed, re-create the removed whiteout.
 
32
 */
 
33
static int epilog(struct inode *dir, struct dentry *wh_dentry,
 
34
                  struct dentry *dentry)
 
35
{
 
36
        int err, rerr;
 
37
        aufs_bindex_t bwh;
 
38
        struct inode *inode, *h_dir;
 
39
        struct dentry *wh;
 
40
        struct au_ndx ndx;
 
41
        struct super_block *sb;
 
42
 
 
43
        LKTRTrace("wh %p, %.*s\n", wh_dentry, AuDLNPair(dentry));
 
44
 
 
45
        bwh = -1;
 
46
        if (wh_dentry) {
 
47
                h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
 
48
                IMustLock(h_dir);
 
49
                bwh = au_dbwh(dentry);
 
50
                err = au_wh_unlink_dentry(h_dir, wh_dentry, dentry, dir,
 
51
                                          /*dlgt*/0);
 
52
                //err = -1;
 
53
                if (unlikely(err))
 
54
                        goto out;
 
55
        }
 
56
 
 
57
        inode = au_new_inode(dentry);
 
58
        //inode = ERR_PTR(-1);
 
59
        if (!IS_ERR(inode)) {
 
60
                d_instantiate(dentry, inode);
 
61
                dir = dentry->d_parent->d_inode; /* dir inode is locked */
 
62
                IMustLock(dir);
 
63
                /* or always cpup dir mtime? */
 
64
                if (au_ibstart(dir) == au_dbstart(dentry))
 
65
                        au_cpup_attr_timesizes(dir);
 
66
                dir->i_version++;
 
67
                return 0; /* success */
 
68
        }
 
69
 
 
70
        err = PTR_ERR(inode);
 
71
        if (!wh_dentry)
 
72
                goto out;
 
73
 
 
74
        /* revert */
 
75
        sb = dentry->d_sb;
 
76
        ndx.flags = 0;
 
77
        if (unlikely(au_opt_test_dlgt(au_mntflags(sb))))
 
78
                au_fset_ndx(ndx.flags, DLGT);
 
79
        ndx.nfsmnt = au_nfsmnt(sb, bwh);
 
80
        ndx.nd = NULL;
 
81
        //ndx.br = NULL;
 
82
        /* dir inode is locked */
 
83
        wh = au_wh_create(dir, dentry, bwh, wh_dentry->d_parent, &ndx);
 
84
        //wh = ERR_PTR(-1);
 
85
        rerr = PTR_ERR(wh);
 
86
        if (!IS_ERR(wh)) {
 
87
                dput(wh);
 
88
                goto out;
 
89
        }
 
90
        AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
 
91
                AuDLNPair(dentry), err, rerr);
 
92
        err = -EIO;
 
93
 
 
94
 out:
 
95
        AuTraceErr(err);
 
96
        return err;
 
97
}
 
98
 
 
99
/*
 
100
 * simple tests for the adding inode operations.
 
101
 * following the checks in vfs, plus the parent-child relationship.
 
102
 */
 
103
int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
 
104
               struct dentry *h_parent, int isdir, struct au_ndx *ndx)
 
105
{
 
106
        int err, exist;
 
107
        struct dentry *h_dentry;
 
108
        struct inode *h_inode;
 
109
        umode_t h_mode;
 
110
 
 
111
        LKTRTrace("%.*s/%.*s, b%d, dir %d\n",
 
112
                  AuDLNPair(h_parent), AuDLNPair(dentry), bindex, isdir);
 
113
 
 
114
        exist = !!dentry->d_inode;
 
115
        h_dentry = au_h_dptr(dentry, bindex);
 
116
        h_inode = h_dentry->d_inode;
 
117
        if (!exist) {
 
118
                err = -EEXIST;
 
119
                if (unlikely(h_inode))
 
120
                        goto out;
 
121
        } else {
 
122
                /* rename(2) case */
 
123
                err = -EIO;
 
124
                if (unlikely(!h_inode || !h_inode->i_nlink))
 
125
                        goto out;
 
126
 
 
127
                h_mode = h_inode->i_mode;
 
128
                if (!isdir) {
 
129
                        err = -EISDIR;
 
130
                        if (unlikely(S_ISDIR(h_mode)))
 
131
                                goto out;
 
132
                } else if (unlikely(!S_ISDIR(h_mode))) {
 
133
                        err = -ENOTDIR;
 
134
                        goto out;
 
135
                }
 
136
        }
 
137
 
 
138
        err = -EIO;
 
139
        /* expected parent dir is locked */
 
140
        if (unlikely(h_parent != h_dentry->d_parent))
 
141
                goto out;
 
142
        err = 0;
 
143
 
 
144
        if (unlikely(au_opt_test(au_mntflags(dentry->d_sb), UDBA_INOTIFY))) {
 
145
                struct dentry *h_latest;
 
146
                struct qstr *qstr = &dentry->d_name;
 
147
 
 
148
                err = -EACCES;
 
149
                if (unlikely(au_test_h_perm
 
150
                             (h_parent->d_inode, MAY_EXEC | MAY_WRITE,
 
151
                              au_ftest_ndx(ndx->flags, DLGT))))
 
152
                        goto out;
 
153
 
 
154
                err = -EIO;
 
155
                h_latest = au_sio_lkup_one(qstr->name, h_parent, qstr->len,
 
156
                                           ndx);
 
157
                err = PTR_ERR(h_latest);
 
158
                if (IS_ERR(h_latest))
 
159
                        goto out;
 
160
                err = -EIO;
 
161
                dput(h_latest);
 
162
                if (h_latest == h_dentry)
 
163
                        err = 0;
 
164
        }
 
165
 
 
166
 out:
 
167
        AuTraceErr(err);
 
168
        return err;
 
169
}
 
170
 
 
171
/*
 
172
 * initial procedure of adding a new entry.
 
173
 * prepare writable branch and the parent dir, lock it,
 
174
 * lookup whiteout for the new entry.
 
175
 */
 
176
static struct dentry *
 
177
lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
 
178
                  struct dentry *src_dentry, struct au_wr_dir_args *wr_dir_args)
 
179
{
 
180
        struct dentry *wh_dentry, *parent, *h_parent, *gparent;
 
181
        int err;
 
182
        aufs_bindex_t bstart, bcpup;
 
183
        struct inode *dir, *h_dir, *gdir;
 
184
        struct au_ndx ndx;
 
185
        struct super_block *sb;
 
186
        struct au_hinode *hgdir;
 
187
        unsigned int mnt_flags;
 
188
 
 
189
        LKTRTrace("%.*s, src %p\n", AuDLNPair(dentry), src_dentry);
 
190
 
 
191
        parent = dentry->d_parent; /* dir inode is locked */
 
192
        IMustLock(parent->d_inode);
 
193
        bstart = au_dbstart(dentry);
 
194
        err = au_wr_dir(dentry, src_dentry, wr_dir_args);
 
195
        bcpup = err;
 
196
        //err = -1;
 
197
        wh_dentry = ERR_PTR(err);
 
198
        if (unlikely(err < 0))
 
199
                goto out;
 
200
 
 
201
        sb = parent->d_sb;
 
202
        mnt_flags = au_mntflags(sb);
 
203
        //todo: meaningless lock if CONFIG_AUFS_DEBUG is disabled.
 
204
        hgdir = NULL;
 
205
        if (unlikely(dt && au_opt_test(mnt_flags, UDBA_INOTIFY)
 
206
                     && !IS_ROOT(parent))) {
 
207
                gparent = dget_parent(parent);
 
208
                gdir = gparent->d_inode;
 
209
                ii_read_lock_parent2(gdir);
 
210
                hgdir = au_hi(gdir, bcpup);
 
211
                ii_read_unlock(gdir);
 
212
                dput(gparent);
 
213
        }
 
214
        dir = parent->d_inode;
 
215
        h_parent = au_h_dptr(parent, bcpup);
 
216
        h_dir = h_parent->d_inode;
 
217
 
 
218
        AuDbgSleep_UdbaRace();
 
219
        au_hdir_lock(h_dir, dir, bcpup);
 
220
        //todo: revalidate the lower dentry?
 
221
 
 
222
        ndx.nfsmnt = au_nfsmnt(sb, bcpup);
 
223
        ndx.flags = 0;
 
224
        if (unlikely(au_opt_test_dlgt(mnt_flags)))
 
225
                au_fset_ndx(ndx.flags, DLGT);
 
226
        ndx.nd = NULL;
 
227
        //ndx.br = NULL;
 
228
        //ndx.nd_file = NULL;
 
229
 
 
230
        if (!au_opt_test(mnt_flags, UDBA_NONE) && au_dbstart(dentry) == bcpup) {
 
231
                struct nameidata nd;
 
232
 
 
233
                if (unlikely(ndx.nfsmnt)) {
 
234
                        //todo: dirty
 
235
                        ndx.nd = &nd;
 
236
                        ndx.br = au_sbr(sb, bcpup);
 
237
                        memset(&nd, 0, sizeof(nd));
 
238
                        nd.flags = LOOKUP_CREATE;
 
239
                        nd.intent.open.flags = O_EXCL;
 
240
                }
 
241
                err = au_may_add(dentry, bcpup, h_parent,
 
242
                                 au_ftest_wrdir(wr_dir_args->flags, ISDIR),
 
243
                                 &ndx);
 
244
                wh_dentry = ERR_PTR(err);
 
245
                if (unlikely(err))
 
246
                        goto out_dir;
 
247
                ndx.nd = NULL;
 
248
                ndx.br = NULL;
 
249
        }
 
250
 
 
251
        if (dt)
 
252
                au_dtime_store(dt, parent, h_parent, hgdir);
 
253
        wh_dentry = NULL;
 
254
        if (/* bcpup != bstart || */ bcpup != au_dbwh(dentry))
 
255
                goto out; /* success */
 
256
 
 
257
        wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, &ndx);
 
258
        //wh_dentry = ERR_PTR(-1);
 
259
 
 
260
 out_dir:
 
261
        if (IS_ERR(wh_dentry))
 
262
                au_hdir_unlock(h_dir, dir, bcpup);
 
263
 out:
 
264
        AuTraceErrPtr(wh_dentry);
 
265
        return wh_dentry;
 
266
}
 
267
 
 
268
/* ---------------------------------------------------------------------- */
 
269
 
 
270
enum { Mknod, Symlink, Creat };
 
271
struct simple_arg {
 
272
        int type;
 
273
        union {
 
274
                struct {
 
275
                        int mode;
 
276
                        struct nameidata *nd;
 
277
                } c;
 
278
                struct {
 
279
                        const char *symname;
 
280
                } s;
 
281
                struct {
 
282
                        int mode;
 
283
                        dev_t dev;
 
284
                } m;
 
285
        } u;
 
286
};
 
287
 
 
288
static int add_simple(struct inode *dir, struct dentry *dentry,
 
289
                      struct simple_arg *arg)
 
290
{
 
291
        int err, dlgt, created;
 
292
        struct dentry *h_dentry, *h_parent, *wh_dentry, *parent;
 
293
        struct inode *h_dir;
 
294
        struct au_dtime dt;
 
295
        struct vfsub_args vargs;
 
296
        struct super_block *sb;
 
297
        aufs_bindex_t bstart;
 
298
        struct au_wr_dir_args wr_dir_args = {
 
299
                .force_btgt     = -1,
 
300
                .flags          = AuWrDir_ADD_ENTRY
 
301
        };
 
302
 
 
303
        LKTRTrace("type %d, %.*s\n", arg->type, AuDLNPair(dentry));
 
304
        IMustLock(dir);
 
305
 
 
306
        aufs_read_lock(dentry, AuLock_DW);
 
307
        parent = dentry->d_parent; /* dir inode is locked */
 
308
        di_write_lock_parent(parent);
 
309
        wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL,
 
310
                                      &wr_dir_args);
 
311
        //wh_dentry = ERR_PTR(-1);
 
312
        err = PTR_ERR(wh_dentry);
 
313
        if (IS_ERR(wh_dentry))
 
314
                goto out;
 
315
 
 
316
        bstart = au_dbstart(dentry);
 
317
        h_dentry = au_h_dptr(dentry, bstart);
 
318
        h_parent = h_dentry->d_parent; /* dir inode is locked */
 
319
        h_dir = h_parent->d_inode;
 
320
        IMustLock(h_dir);
 
321
        sb = dir->i_sb;
 
322
        dlgt = !!au_opt_test_dlgt(au_mntflags(sb));
 
323
 
 
324
#if 1 // partial testing
 
325
        switch (arg->type) {
 
326
        case Creat:
 
327
                AuDebugOn(au_test_nfs(h_dir->i_sb) && !arg->u.c.nd);
 
328
                err = au_h_create(h_dir, h_dentry, arg->u.c.mode, dlgt,
 
329
                                  arg->u.c.nd, au_nfsmnt(sb, bstart));
 
330
                break;
 
331
        case Symlink:
 
332
                err = vfsub_symlink(h_dir, h_dentry, arg->u.s.symname,
 
333
                                    S_IALLUGO, dlgt);
 
334
                break;
 
335
        case Mknod:
 
336
                err = vfsub_mknod(h_dir, h_dentry, arg->u.m.mode, arg->u.m.dev,
 
337
                                  dlgt);
 
338
                break;
 
339
        default:
 
340
                BUG();
 
341
        }
 
342
#else
 
343
        err = -1;
 
344
#endif
 
345
        created = !err;
 
346
        if (!err)
 
347
                err = epilog(dir, wh_dentry, dentry);
 
348
        //err = -1;
 
349
 
 
350
        /* revert */
 
351
        if (unlikely(created && err && h_dentry->d_inode)) {
 
352
                int rerr;
 
353
                vfsub_args_init(&vargs, NULL, dlgt, 0);
 
354
                rerr = vfsub_unlink(h_dir, h_dentry, &vargs);
 
355
                //rerr = -1;
 
356
                if (rerr) {
 
357
                        AuIOErr("%.*s revert failure(%d, %d)\n",
 
358
                                AuDLNPair(dentry), err, rerr);
 
359
                        err = -EIO;
 
360
                }
 
361
                //todo: inotify will be fired to the grand parent dir
 
362
                au_dtime_revert(&dt);
 
363
                d_drop(dentry);
 
364
        }
 
365
 
 
366
        au_hdir_unlock(h_dir, dir, bstart);
 
367
        dput(wh_dentry);
 
368
 
 
369
 out:
 
370
        if (unlikely(err)) {
 
371
                au_update_dbstart(dentry);
 
372
                d_drop(dentry);
 
373
        }
 
374
        di_write_unlock(parent);
 
375
        aufs_read_unlock(dentry, AuLock_DW);
 
376
        AuTraceErr(err);
 
377
        return err;
 
378
}
 
379
 
 
380
int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 
381
{
 
382
        struct simple_arg arg = {
 
383
                .type = Mknod,
 
384
                .u.m = {
 
385
                        .mode   = mode,
 
386
                        .dev    = dev
 
387
                }
 
388
        };
 
389
        return add_simple(dir, dentry, &arg);
 
390
}
 
391
 
 
392
int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
 
393
{
 
394
        struct simple_arg arg = {
 
395
                .type = Symlink,
 
396
                .u.s.symname = symname
 
397
        };
 
398
        return add_simple(dir, dentry, &arg);
 
399
}
 
400
 
 
401
int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
 
402
                struct nameidata *nd)
 
403
{
 
404
        struct simple_arg arg = {
 
405
                .type = Creat,
 
406
                .u.c = {
 
407
                        .mode   = mode,
 
408
                        .nd     = nd
 
409
                }
 
410
        };
 
411
        return add_simple(dir, dentry, &arg);
 
412
}
 
413
 
 
414
/* ---------------------------------------------------------------------- */
 
415
 
 
416
struct link_arg {
 
417
        aufs_bindex_t bdst, bsrc;
 
418
        int issamedir, dlgt;
 
419
        struct dentry *src_parent, *parent, *h_dentry;
 
420
        struct inode *h_dir, *inode, *dir;
 
421
};
 
422
 
 
423
static int cpup_before_link(struct dentry *src_dentry, struct inode *dir,
 
424
                            struct link_arg *a)
 
425
{
 
426
        int err;
 
427
        struct inode *hi, *h_dir, *src_dir, *gdir;
 
428
        struct dentry *gparent;
 
429
 
 
430
        AuTraceEnter();
 
431
 
 
432
        gparent = NULL;
 
433
        gdir = NULL;
 
434
        if (unlikely(au_opt_test(au_mntflags(src_dentry->d_sb), UDBA_INOTIFY)
 
435
                     && !IS_ROOT(a->src_parent))) {
 
436
                gparent = dget_parent(a->src_parent);
 
437
                gdir = gparent->d_inode;
 
438
                if (gdir == dir) {
 
439
                        dput(gparent);
 
440
                        gparent = NULL;
 
441
                }
 
442
        }
 
443
        src_dir = a->src_parent->d_inode;
 
444
        h_dir = NULL;
 
445
 
 
446
        if (!a->issamedir) {
 
447
                /* this temporary unlock/lock is safe */
 
448
                au_hdir_unlock(a->h_dir, dir, a->bdst);
 
449
                di_read_lock_parent2(a->src_parent, AuLock_IR);
 
450
                err = au_test_and_cpup_dirs(src_dentry, a->bdst, a->parent);
 
451
                //err = -1;
 
452
                if (unlikely(err)) {
 
453
                        au_hdir_lock(a->h_dir, dir, a->bdst);
 
454
                        goto out;
 
455
                }
 
456
 
 
457
                //todo: meaningless lock if CONFIG_AUFS_DEBUG is disabled.
 
458
                if (unlikely(gparent))
 
459
                        ii_read_lock_parent3(gdir);
 
460
                h_dir = au_h_iptr(src_dir, a->bdst);
 
461
                au_hdir_lock(h_dir, src_dir, a->bdst);
 
462
        } else if (unlikely(gparent)) {
 
463
                /* this temporary unlock/lock is safe */
 
464
                au_hdir_unlock(a->h_dir, dir, a->bdst);
 
465
                ii_read_lock_parent3(gdir);
 
466
                au_hdir_lock(a->h_dir, dir, a->bdst);
 
467
        }
 
468
        //todo: test parent-gparent relationship
 
469
 
 
470
        AuDebugOn(au_dbstart(src_dentry) != a->bsrc);
 
471
        hi = au_h_dptr(src_dentry, a->bsrc)->d_inode;
 
472
        mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD);
 
473
        err = au_sio_cpup_simple(src_dentry, a->bdst, -1, AuCpup_DTIME);
 
474
        //err = -1;
 
475
        mutex_unlock(&hi->i_mutex);
 
476
 
 
477
        if (unlikely(gparent)) {
 
478
                ii_read_unlock(gdir);
 
479
                dput(gparent);
 
480
        }
 
481
 
 
482
 out:
 
483
        if (h_dir) {
 
484
                au_hdir_unlock(h_dir, src_dir, a->bdst);
 
485
                au_hdir_lock(a->h_dir, dir, a->bdst);
 
486
        }
 
487
        if (!a->issamedir)
 
488
                di_read_unlock(a->src_parent, AuLock_IR);
 
489
 
 
490
        AuTraceErr(err);
 
491
        return err;
 
492
}
 
493
 
 
494
static int cpup_or_link(struct dentry *src_dentry, struct link_arg *a)
 
495
{
 
496
        int err;
 
497
        struct inode *inode, *h_inode, *h_dst_inode;
 
498
        struct dentry *h_dentry;
 
499
        aufs_bindex_t bstart;
 
500
        struct super_block *sb;
 
501
 
 
502
        AuTraceEnter();
 
503
 
 
504
        sb = src_dentry->d_sb;
 
505
        inode = src_dentry->d_inode;
 
506
        AuDebugOn(au_dbstart(src_dentry) != a->bsrc);
 
507
        h_dentry = au_h_dptr(src_dentry, a->bsrc);
 
508
        h_inode = h_dentry->d_inode;
 
509
        bstart = au_ibstart(inode);
 
510
        h_dst_inode = NULL;
 
511
        if (bstart <= a->bdst)
 
512
                h_dst_inode = au_h_iptr(inode, a->bdst);
 
513
 
 
514
        if (!h_dst_inode || !h_dst_inode->i_nlink) {
 
515
                /* copyup src_dentry as the name of dentry. */
 
516
                au_set_dbstart(src_dentry, a->bdst);
 
517
                au_set_h_dptr(src_dentry, a->bdst, dget(a->h_dentry));
 
518
                mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
 
519
                err = au_sio_cpup_single(src_dentry, a->bdst, a->bsrc, -1,
 
520
                                         !AuCpup_DTIME);
 
521
                //err = -1;
 
522
                mutex_unlock(&h_inode->i_mutex);
 
523
                au_set_h_dptr(src_dentry, a->bdst, NULL);
 
524
                au_set_dbstart(src_dentry, a->bsrc);
 
525
        } else {
 
526
                /* the inode of src_dentry already exists on a.bdst branch */
 
527
                h_dentry = d_find_alias(h_dst_inode);
 
528
                if (h_dentry) {
 
529
                        err = vfsub_link(h_dentry, a->h_dir,
 
530
                                         a->h_dentry, a->dlgt);
 
531
                        dput(h_dentry);
 
532
                } else {
 
533
                        AuIOErr("no dentry found for i%lu on b%d\n",
 
534
                                h_dst_inode->i_ino, a->bdst);
 
535
                        err = -EIO;
 
536
                }
 
537
        }
 
538
 
 
539
        if (!err)
 
540
                au_plink_append(sb, a->inode, a->h_dentry, a->bdst);
 
541
 
 
542
        AuTraceErr(err);
 
543
        return err;
 
544
}
 
545
 
 
546
int aufs_link(struct dentry *src_dentry, struct inode *dir,
 
547
              struct dentry *dentry)
 
548
{
 
549
        int err, rerr;
 
550
        struct dentry *h_parent, *wh_dentry, *h_src_dentry;
 
551
        struct au_dtime dt;
 
552
        struct link_arg a;
 
553
        struct super_block *sb;
 
554
        unsigned int mnt_flags;
 
555
        struct vfsub_args vargs;
 
556
        struct au_wr_dir_args wr_dir_args = {
 
557
                //.force_btgt   = -1,
 
558
                .flags          = AuWrDir_ADD_ENTRY
 
559
        };
 
560
 
 
561
        LKTRTrace("src %.*s, i%lu, dst %.*s\n",
 
562
                  AuDLNPair(src_dentry), dir->i_ino, AuDLNPair(dentry));
 
563
        IMustLock(dir);
 
564
        IMustLock(src_dentry->d_inode);
 
565
        AuDebugOn(S_ISDIR(src_dentry->d_inode->i_mode));
 
566
 
 
567
        aufs_read_and_write_lock2(dentry, src_dentry, /*flags*/0);
 
568
        sb = dentry->d_sb;
 
569
        a.dir = dir;
 
570
        a.src_parent = dget_parent(src_dentry);
 
571
        a.parent = dentry->d_parent; /* dir inode is locked */
 
572
        a.issamedir = (a.src_parent == a.parent);
 
573
        if (!a.issamedir)
 
574
                au_fset_wrdir(wr_dir_args.flags, LOCK_SRCDIR);
 
575
        wr_dir_args.force_btgt = au_dbstart(src_dentry);
 
576
        di_write_lock_parent(a.parent);
 
577
        wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
 
578
        wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &wr_dir_args);
 
579
        //wh_dentry = ERR_PTR(-1);
 
580
        err = PTR_ERR(wh_dentry);
 
581
        if (IS_ERR(wh_dentry))
 
582
                goto out;
 
583
 
 
584
        a.inode = src_dentry->d_inode;
 
585
        a.bdst = au_dbstart(dentry);
 
586
        a.h_dentry = au_h_dptr(dentry, a.bdst);
 
587
        h_parent = a.h_dentry->d_parent; /* dir inode is locked */
 
588
        a.h_dir = h_parent->d_inode;
 
589
        IMustLock(a.h_dir);
 
590
 
 
591
        err = 0;
 
592
        mnt_flags = au_mntflags(sb);
 
593
        a.dlgt = !!au_opt_test_dlgt(mnt_flags);
 
594
 
 
595
        //todo: minor optimize, their sb may be same while their bindex differs.
 
596
        a.bsrc = au_dbstart(src_dentry);
 
597
        h_src_dentry = au_h_dptr(src_dentry, a.bsrc);
 
598
        if (unlikely(!au_opt_test(mnt_flags, PLINK))) {
 
599
                /*
 
600
                 * copyup src_dentry to the branch we process,
 
601
                 * and then link(2) to it.
 
602
                 * gave up 'pseudo link by cpup' approach,
 
603
                 * since nlink may be one and some applications will not work.
 
604
                 */
 
605
                if (a.bdst < a.bsrc
 
606
                    /* && h_src_dentry->d_sb != a.h_dentry->d_sb */)
 
607
                        err = cpup_before_link(src_dentry, dir, &a);
 
608
                if (!err) {
 
609
                        h_src_dentry = au_h_dptr(src_dentry, a.bdst);
 
610
                        err = vfsub_link(h_src_dentry, a.h_dir, a.h_dentry,
 
611
                                         a.dlgt);
 
612
                        //err = -1;
 
613
                }
 
614
        } else {
 
615
                if (a.bdst < a.bsrc
 
616
                    /* && h_src_dentry->d_sb != a.h_dentry->d_sb */)
 
617
                        err = cpup_or_link(src_dentry, &a);
 
618
                else {
 
619
                        h_src_dentry = au_h_dptr(src_dentry, a.bdst);
 
620
                        err = vfsub_link(h_src_dentry, a.h_dir, a.h_dentry,
 
621
                                         a.dlgt);
 
622
                        //err = -1;
 
623
                }
 
624
        }
 
625
        if (unlikely(err))
 
626
                goto out_unlock;
 
627
        if (wh_dentry) {
 
628
                err = au_wh_unlink_dentry(a.h_dir, wh_dentry, dentry,
 
629
                                          dir, /*dlgt*/0);
 
630
                //err = -1;
 
631
                if (unlikely(err))
 
632
                        goto out_revert;
 
633
        }
 
634
 
 
635
#if 0 // cannot support it
 
636
        /* fuse has different memory inode for the same inode number */
 
637
        if (unlikely(au_test_fuse(a.h_dentry->d_sb))) {
 
638
                LKTRLabel(here);
 
639
                d_drop(a.h_dentry);
 
640
                //d_drop(h_src_dentry);
 
641
                //d_drop(src_dentry);
 
642
                inc_nlink(a.inode);
 
643
                a.inode->i_ctime = dir->i_ctime;
 
644
        }
 
645
#endif
 
646
 
 
647
        dir->i_version++;
 
648
        if (au_ibstart(dir) == au_dbstart(dentry))
 
649
                au_cpup_attr_timesizes(dir);
 
650
        if (!d_unhashed(a.h_dentry)
 
651
            /* || h_old_inode->i_nlink <= nlink */
 
652
            /* || SB_NFS(h_src_dentry->d_sb) */) {
 
653
                dentry->d_inode = igrab(a.inode);
 
654
                d_instantiate(dentry, a.inode);
 
655
                inc_nlink(a.inode);
 
656
                a.inode->i_ctime = dir->i_ctime;
 
657
        } else
 
658
                /* nfs case (< 2.6.15) */
 
659
                d_drop(dentry);
 
660
#if 0 // debug
 
661
        //au_debug_on();
 
662
        AuDbgInode(a.inode);
 
663
        //au_debug_off();
 
664
        {
 
665
                aufs_bindex_t i;
 
666
                for (i = au_ibstart(a.inode); i <= au_ibend(a.inode); i++) {
 
667
                        struct au_xino_entry xinoe;
 
668
                        struct inode *hi;
 
669
                        hi = au_h_iptr(a.inode, i);
 
670
                        if (hi) {
 
671
                                au_xino_read(sb, i, hi->i_ino, &xinoe);
 
672
                                AuDbg("hi%lu, i%lu\n", hi->i_ino, xinoe.ino);
 
673
                        }
 
674
                }
 
675
        }
 
676
#endif
 
677
        goto out_unlock; /* success */
 
678
 
 
679
 out_revert:
 
680
#if 0 // remove
 
681
        if (d_unhashed(a.h_dentry)) {
 
682
                /* hardlink on nfs (< 2.6.15) */
 
683
                struct dentry *d;
 
684
                const struct qstr *name = &a.h_dentry->d_name;
 
685
                AuDebugOn(a.h_dentry->d_parent->d_inode != a.h_dir);
 
686
                /* do not superio. */
 
687
                d = au_lkup_one(name->name, a.h_dentry->d_parent,
 
688
                                name->len, au_nfsmnt(sb, a.bdst)??,
 
689
                                au_opt_test_dlgt(mnt_flags));
 
690
                rerr = PTR_ERR(d);
 
691
                if (IS_ERR(d))
 
692
                        goto out_rerr;
 
693
                dput(a.h_dentry);
 
694
                a.h_dentry = d;
 
695
                AuDebugOn(!d->d_inode);
 
696
        }
 
697
#endif
 
698
        vfsub_args_init(&vargs, NULL, a.dlgt, 0);
 
699
        rerr = vfsub_unlink(a.h_dir, a.h_dentry, &vargs);
 
700
        //rerr = -1;
 
701
        if (!rerr)
 
702
                goto out_dt;
 
703
// out_rerr:
 
704
        AuIOErr("%.*s reverting failed(%d, %d)\n",
 
705
                AuDLNPair(dentry), err, rerr);
 
706
        err = -EIO;
 
707
 out_dt:
 
708
        d_drop(dentry);
 
709
        au_dtime_revert(&dt);
 
710
 out_unlock:
 
711
        au_hdir_unlock(a.h_dir, dir, a.bdst);
 
712
        dput(wh_dentry);
 
713
 out:
 
714
        if (unlikely(err)) {
 
715
                au_update_dbstart(dentry);
 
716
                d_drop(dentry);
 
717
        }
 
718
        di_write_unlock(a.parent);
 
719
        dput(a.src_parent);
 
720
        aufs_read_and_write_unlock2(dentry, src_dentry);
 
721
        AuTraceErr(err);
 
722
        return err;
 
723
}
 
724
 
 
725
int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 
726
{
 
727
        int err, rerr, diropq, dlgt;
 
728
        struct dentry *h_dentry, *h_parent, *wh_dentry, *parent, *opq_dentry;
 
729
        struct inode *h_dir, *h_inode;
 
730
        struct au_dtime dt;
 
731
        aufs_bindex_t bindex;
 
732
        struct super_block *sb;
 
733
        unsigned int mnt_flags;
 
734
        struct vfsub_args vargs;
 
735
        struct au_wr_dir_args wr_dir_args = {
 
736
                .force_btgt     = -1,
 
737
                .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
 
738
        };
 
739
 
 
740
        LKTRTrace("i%lu, %.*s, mode 0%o\n",
 
741
                  dir->i_ino, AuDLNPair(dentry), mode);
 
742
        IMustLock(dir);
 
743
 
 
744
        aufs_read_lock(dentry, AuLock_DW);
 
745
        parent = dentry->d_parent; /* dir inode is locked */
 
746
        di_write_lock_parent(parent);
 
747
        wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL,
 
748
                                      &wr_dir_args);
 
749
        //wh_dentry = ERR_PTR(-1);
 
750
        err = PTR_ERR(wh_dentry);
 
751
        if (IS_ERR(wh_dentry))
 
752
                goto out;
 
753
 
 
754
        sb = dentry->d_sb;
 
755
        bindex = au_dbstart(dentry);
 
756
        h_dentry = au_h_dptr(dentry, bindex);
 
757
        h_parent = h_dentry->d_parent; /* dir inode is locked */
 
758
        h_dir = h_parent->d_inode;
 
759
        IMustLock(h_dir);
 
760
        mnt_flags = au_mntflags(sb);
 
761
        dlgt = !!au_opt_test_dlgt(mnt_flags);
 
762
 
 
763
        err = vfsub_mkdir(h_dir, h_dentry, mode, dlgt);
 
764
        //err = -1;
 
765
        if (unlikely(err))
 
766
                goto out_unlock;
 
767
        h_inode = h_dentry->d_inode;
 
768
 
 
769
        /* make the dir opaque */
 
770
        diropq = 0;
 
771
        if (wh_dentry || au_opt_test(mnt_flags, ALWAYS_DIROPQ)) {
 
772
                mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
 
773
                opq_dentry = au_diropq_create(dentry, bindex, /*dlgt*/0);
 
774
                //opq_dentry = ERR_PTR(-1);
 
775
                mutex_unlock(&h_inode->i_mutex);
 
776
                err = PTR_ERR(opq_dentry);
 
777
                if (IS_ERR(opq_dentry))
 
778
                        goto out_dir;
 
779
                dput(opq_dentry);
 
780
                diropq = 1;
 
781
        }
 
782
 
 
783
        err = epilog(dir, wh_dentry, dentry);
 
784
        //err = -1;
 
785
        if (!err) {
 
786
                inc_nlink(dir);
 
787
                goto out_unlock; /* success */
 
788
        }
 
789
 
 
790
        /* revert */
 
791
        if (diropq) {
 
792
                LKTRLabel(revert opq);
 
793
                mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
 
794
                rerr = au_diropq_remove(dentry, bindex, dlgt);
 
795
                //rerr = -1;
 
796
                mutex_unlock(&h_inode->i_mutex);
 
797
                if (rerr) {
 
798
                        AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
 
799
                                AuDLNPair(dentry), err, rerr);
 
800
                        err = -EIO;
 
801
                }
 
802
        }
 
803
 
 
804
 out_dir:
 
805
        LKTRLabel(revert dir);
 
806
        vfsub_args_init(&vargs, NULL, dlgt, 0);
 
807
        rerr = vfsub_rmdir(h_dir, h_dentry, &vargs);
 
808
        //rerr = -1;
 
809
        if (rerr) {
 
810
                AuIOErr("%.*s reverting dir failed(%d, %d)\n",
 
811
                        AuDLNPair(dentry), err, rerr);
 
812
                err = -EIO;
 
813
        }
 
814
        d_drop(dentry);
 
815
        au_dtime_revert(&dt);
 
816
 out_unlock:
 
817
        au_hdir_unlock(h_dir, dir, bindex);
 
818
        dput(wh_dentry);
 
819
 out:
 
820
        if (unlikely(err)) {
 
821
                au_update_dbstart(dentry);
 
822
                d_drop(dentry);
 
823
        }
 
824
        di_write_unlock(parent);
 
825
        aufs_read_unlock(dentry, AuLock_DW);
 
826
        AuTraceErr(err);
 
827
        return err;
 
828
}