2
* Copyright (C) 2005, 2006, 2007 Junjiro Okajima
4
* This program, aufs is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
/* $Id: i_op_add.c,v 1.37 2007/05/07 03:46:08 sfjro Exp $ */
21
//#include <linux/fs.h>
22
//#include <linux/namei.h>
26
* final procedure of adding a new entry, except link(2).
27
* remove whiteout, instantiate, copyup the parent dir's times and size
29
* if it failed, re-create the removed whiteout.
31
static int epilog(struct dentry *wh_dentry, struct dentry *dentry)
35
struct inode *inode, *dir;
37
struct lkup_args lkup;
39
LKTRTrace("wh %p, %.*s\n", wh_dentry, DLNPair(dentry));
41
lkup.dlgt = need_dlgt(dentry->d_sb);
45
err = au_unlink_wh_dentry(wh_dentry->d_parent->d_inode,
46
wh_dentry, dentry, lkup.dlgt);
52
inode = au_new_inode(dentry);
53
//inode = ERR_PTR(-1);
55
d_instantiate(dentry, inode);
56
dir = dentry->d_parent->d_inode;
57
/* or always cpup dir mtime? */
58
if (ibstart(dir) == dbstart(dentry))
59
au_cpup_attr_timesizes(dir);
61
return 0; /* success */
69
lkup.nfsmnt = au_nfsmnt(dentry->d_sb, bwh);
70
wh = simple_create_wh(dentry, bwh, wh_dentry->d_parent, &lkup);
77
IOErr("%.*s reverting whiteout failed(%d, %d)\n",
78
DLNPair(dentry), err, rerr);
87
* initial procedure of adding a new entry.
88
* prepare writable branch and the parent dir, lock it,
89
* lookup whiteout for the new entry.
91
static struct dentry *
92
lock_hdir_lkup_wh(struct dentry *dentry, struct dtime *dt,
93
struct dentry *src_dentry, int do_lock_srcdir)
95
struct dentry *wh_dentry, *parent, *hidden_parent;
97
aufs_bindex_t bstart, bcpup;
98
struct inode *dir, *h_dir;
99
struct lkup_args lkup;
101
LKTRTrace("%.*s, src %p\n", DLNPair(dentry), src_dentry);
103
parent = dentry->d_parent;
104
bstart = dbstart(dentry);
105
bcpup = err = wr_dir(dentry, 1, src_dentry, -1, do_lock_srcdir);
107
wh_dentry = ERR_PTR(err);
108
if (unlikely(err < 0))
111
dir = parent->d_inode;
112
hidden_parent = au_h_dptr_i(parent, bcpup);
113
h_dir = hidden_parent->d_inode;
114
hdir_lock(h_dir, dir, bcpup);
116
dtime_store(dt, parent, hidden_parent);
117
if (/* bcpup != bstart || */ bcpup != dbwh(dentry))
118
return NULL; /* success */
120
lkup.nfsmnt = au_nfsmnt(parent->d_sb, bcpup);
121
lkup.dlgt = need_dlgt(parent->d_sb);
122
wh_dentry = lkup_wh(hidden_parent, &dentry->d_name, &lkup);
123
//wh_dentry = ERR_PTR(-1);
124
if (IS_ERR(wh_dentry))
125
hdir_unlock(h_dir, dir, bcpup);
128
TraceErrPtr(wh_dentry);
132
/* ---------------------------------------------------------------------- */
134
enum {Mknod, Symlink, Creat};
140
struct nameidata *nd;
152
static int add_simple(struct inode *dir, struct dentry *dentry,
153
struct simple_arg *arg)
156
struct dentry *hidden_dentry, *hidden_parent, *wh_dentry, *parent;
157
struct inode *hidden_dir;
160
LKTRTrace("type %d, %.*s\n", arg->type, DLNPair(dentry));
163
aufs_read_lock(dentry, AUFS_D_WLOCK);
164
parent = dentry->d_parent;
165
di_write_lock_parent(parent);
166
wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL,
167
/*do_lock_srcdir*/0);
168
//wh_dentry = ERR_PTR(-1);
169
err = PTR_ERR(wh_dentry);
170
if (IS_ERR(wh_dentry))
173
hidden_dentry = au_h_dptr(dentry);
174
hidden_parent = hidden_dentry->d_parent;
175
hidden_dir = hidden_parent->d_inode;
176
IMustLock(hidden_dir);
177
dlgt = need_dlgt(dir->i_sb);
179
#if 1 // partial testing
184
struct nameidata fake_nd;
185
fake_nd = *arg->u.c.nd;
186
fake_nd.dentry = dget(hidden_parent);
187
fake_nd.mnt = sbr_mnt(dentry->d_sb, dbstart(dentry));
189
err = vfsub_create(hidden_dir, hidden_dentry,
190
arg->u.c.mode, &fake_nd, dlgt);
191
path_release(&fake_nd);
194
err = vfsub_create(hidden_dir, hidden_dentry,
195
arg->u.c.mode, NULL, dlgt);
198
err = vfsub_symlink(hidden_dir, hidden_dentry,
199
arg->u.s.symname, S_IALLUGO, dlgt);
202
err = vfsub_mknod(hidden_dir, hidden_dentry,
203
arg->u.m.mode, arg->u.m.dev, dlgt);
212
err = epilog(wh_dentry, dentry);
216
if (unlikely(err && hidden_dentry->d_inode)) {
218
rerr = vfsub_unlink(hidden_dir, hidden_dentry, dlgt);
221
IOErr("%.*s revert failure(%d, %d)\n",
222
DLNPair(dentry), err, rerr);
225
dtime_revert(&dt, !CPUP_LOCKED_GHDIR);
229
hdir_unlock(hidden_dir, dir, dbstart(dentry));
234
au_update_dbstart(dentry);
237
di_write_unlock(parent);
238
aufs_read_unlock(dentry, AUFS_D_WLOCK);
243
int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
245
struct simple_arg arg = {
247
.u.m = {.mode = mode, .dev = dev}
249
return add_simple(dir, dentry, &arg);
252
int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
254
struct simple_arg arg = {
256
.u.s.symname = symname
258
return add_simple(dir, dentry, &arg);
261
int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
262
struct nameidata *nd)
264
struct simple_arg arg = {
266
.u.c = {.mode = mode, .nd = nd}
268
return add_simple(dir, dentry, &arg);
271
/* ---------------------------------------------------------------------- */
274
aufs_bindex_t bdst, bsrc;
276
struct dentry *src_parent, *parent, *hidden_dentry;
277
struct inode *hidden_dir, *inode;
280
static int cpup_before_link(struct dentry *src_dentry, struct inode *dir,
285
struct inode *hi, *hdir = NULL, *src_dir;
290
flags = au_flags_cpup(CPUP_DTIME, a->parent);
291
src_dir = a->src_parent->d_inode;
294
di_read_lock_parent2(a->src_parent, AUFS_I_RLOCK);
295
// this temporary unlock/lock is safe
296
hdir_unlock(a->hidden_dir, dir, a->bdst);
297
err = test_and_cpup_dirs(src_dentry, a->bdst, a->parent);
300
hdir = au_h_iptr_i(src_dir, a->bdst);
301
hdir_lock(hdir, src_dir, a->bdst);
302
flags = au_flags_cpup(CPUP_DTIME, a->src_parent);
307
hi = au_h_dptr(src_dentry)->d_inode;
309
err = sio_cpup_simple(src_dentry, a->bdst, -1, flags);
316
hdir_unlock(hdir, src_dir, a->bdst);
317
hdir_lock(a->hidden_dir, dir, a->bdst);
318
di_read_unlock(a->src_parent, AUFS_I_RLOCK);
325
static int cpup_or_link(struct dentry *src_dentry, struct link_arg *a)
328
struct inode *inode, *h_inode, *h_dst_inode;
329
struct dentry *h_dentry;
330
aufs_bindex_t bstart;
331
struct super_block *sb;
335
sb = src_dentry->d_sb;
336
inode = src_dentry->d_inode;
337
h_dentry = au_h_dptr(src_dentry);
338
h_inode = h_dentry->d_inode;
339
bstart = ibstart(inode);
341
if (bstart <= a->bdst)
342
h_dst_inode = au_h_iptr_i(inode, a->bdst);
345
/* copyup src_dentry as the name of dentry. */
346
set_dbstart(src_dentry, a->bdst);
347
set_h_dptr(src_dentry, a->bdst, dget(a->hidden_dentry));
348
hi_lock_child(h_inode);
349
err = sio_cpup_single(src_dentry, a->bdst, a->bsrc, -1,
350
au_flags_cpup(!CPUP_DTIME, a->parent));
353
set_h_dptr(src_dentry, a->bdst, NULL);
354
set_dbstart(src_dentry, a->bsrc);
356
/* the inode of src_dentry already exists on a.bdst branch */
357
h_dentry = d_find_alias(h_dst_inode);
359
err = vfsub_link(h_dentry, a->hidden_dir,
360
a->hidden_dentry, a->dlgt);
363
IOErr("no dentry found for i%lu on b%d\n",
364
h_dst_inode->i_ino, a->bdst);
370
append_plink(sb, a->inode, a->hidden_dentry, a->bdst);
376
int aufs_link(struct dentry *src_dentry, struct inode *dir,
377
struct dentry *dentry)
380
struct dentry *hidden_parent, *wh_dentry, *hidden_src_dentry;
383
struct super_block *sb;
385
LKTRTrace("src %.*s, i%lu, dst %.*s\n",
386
DLNPair(src_dentry), dir->i_ino, DLNPair(dentry));
388
IMustLock(src_dentry->d_inode);
390
aufs_read_and_write_lock2(dentry, src_dentry, /*isdir*/0);
391
a.src_parent = src_dentry->d_parent;
392
a.parent = dentry->d_parent;
393
a.issamedir = (a.src_parent == a.parent);
394
di_write_lock_parent(a.parent);
395
wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, !a.issamedir);
396
//wh_dentry = ERR_PTR(-1);
397
err = PTR_ERR(wh_dentry);
398
if (IS_ERR(wh_dentry))
401
a.inode = src_dentry->d_inode;
402
a.hidden_dentry = au_h_dptr(dentry);
403
hidden_parent = a.hidden_dentry->d_parent;
404
a.hidden_dir = hidden_parent->d_inode;
405
IMustLock(a.hidden_dir);
409
a.dlgt = need_dlgt(sb);
411
//todo: minor optimize, their sb may be same while their bindex differs.
412
a.bsrc = dbstart(src_dentry);
413
a.bdst = dbstart(dentry);
414
hidden_src_dentry = au_h_dptr(src_dentry);
415
if (unlikely(!au_flag_test(sb, AuFlag_PLINK))) {
417
* copyup src_dentry to the branch we process,
418
* and then link(2) to it.
419
* gave up 'pseudo link by cpup' approach,
420
* since nlink may be one and some applications will not work.
423
/* && hidden_src_dentry->d_sb != a.hidden_dentry->d_sb */)
424
err = cpup_before_link(src_dentry, dir, &a);
426
hidden_src_dentry = au_h_dptr(src_dentry);
427
err = vfsub_link(hidden_src_dentry, a.hidden_dir,
428
a.hidden_dentry, a.dlgt);
433
/* && hidden_src_dentry->d_sb != a.hidden_dentry->d_sb */)
434
err = cpup_or_link(src_dentry, &a);
436
hidden_src_dentry = au_h_dptr(src_dentry);
437
err = vfsub_link(hidden_src_dentry, a.hidden_dir,
438
a.hidden_dentry, a.dlgt);
445
err = au_unlink_wh_dentry(a.hidden_dir, wh_dentry, dentry,
453
if (ibstart(dir) == dbstart(dentry))
454
au_cpup_attr_timesizes(dir);
455
if (!d_unhashed(a.hidden_dentry)
456
/* || hidden_old_inode->i_nlink <= nlink */
457
/* || SB_NFS(hidden_src_dentry->d_sb) */) {
458
dentry->d_inode = igrab(a.inode);
459
d_instantiate(dentry, a.inode);
461
a.inode->i_ctime = dir->i_ctime;
463
/* nfs case (< 2.6.15) */
471
for (i = ibstart(a.inode); i <= ibend(a.inode); i++) {
474
hi = au_h_iptr_i(a.inode, i);
476
xino_read(sb, i, hi->i_ino, &xino);
477
Dbg("hi%lu, i%lu\n", hi->i_ino, xino.ino);
482
goto out_unlock; /* success */
486
if (d_unhashed(a.hidden_dentry)) {
487
/* hardlink on nfs (< 2.6.15) */
489
const struct qstr *name = &a.hidden_dentry->d_name;
490
DEBUG_ON(a.hidden_dentry->d_parent->d_inode != a.hidden_dir);
492
d = lkup_one(name->name, a.hidden_dentry->d_parent, name->len,
493
au_nfsmnt(sb, a.bdst)??, need_dlgt(sb));
497
dput(a.hidden_dentry);
499
DEBUG_ON(!d->d_inode);
502
rerr = vfsub_unlink(a.hidden_dir, a.hidden_dentry, a.dlgt);
507
IOErr("%.*s reverting failed(%d, %d)\n", DLNPair(dentry), err, rerr);
511
dtime_revert(&dt, !CPUP_LOCKED_GHDIR);
513
hdir_unlock(a.hidden_dir, dir, a.bdst);
517
au_update_dbstart(dentry);
520
di_write_unlock(a.parent);
521
aufs_read_and_write_unlock2(dentry, src_dentry);
526
int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
528
int err, rerr, diropq, dlgt;
529
struct dentry *hidden_dentry, *hidden_parent, *wh_dentry, *parent,
531
struct inode *hidden_dir, *hidden_inode;
533
aufs_bindex_t bindex;
534
struct super_block *sb;
536
LKTRTrace("i%lu, %.*s, mode 0%o\n", dir->i_ino, DLNPair(dentry), mode);
539
aufs_read_lock(dentry, AUFS_D_WLOCK);
540
parent = dentry->d_parent;
541
di_write_lock_parent(parent);
542
wh_dentry = lock_hdir_lkup_wh(dentry, &dt, /*src_dentry*/NULL,
543
/*do_lock_srcdir*/0);
544
//wh_dentry = ERR_PTR(-1);
545
err = PTR_ERR(wh_dentry);
546
if (IS_ERR(wh_dentry))
550
bindex = dbstart(dentry);
551
hidden_dentry = au_h_dptr(dentry);
552
hidden_parent = hidden_dentry->d_parent;
553
hidden_dir = hidden_parent->d_inode;
554
IMustLock(hidden_dir);
555
dlgt = need_dlgt(sb);
557
err = vfsub_mkdir(hidden_dir, hidden_dentry, mode, dlgt);
561
hidden_inode = hidden_dentry->d_inode;
563
/* make the dir opaque */
565
if (unlikely(wh_dentry || au_flag_test(sb, AuFlag_ALWAYS_DIROPQ))) {
566
hi_lock_child(hidden_inode);
567
opq_dentry = create_diropq(dentry, bindex, dlgt);
568
//opq_dentry = ERR_PTR(-1);
569
i_unlock(hidden_inode);
570
err = PTR_ERR(opq_dentry);
571
if (IS_ERR(opq_dentry))
577
err = epilog(wh_dentry, dentry);
581
goto out_unlock; /* success */
585
if (unlikely(diropq)) {
586
LKTRLabel(revert opq);
587
hi_lock_child(hidden_inode);
588
rerr = remove_diropq(dentry, bindex, dlgt);
590
i_unlock(hidden_inode);
592
IOErr("%.*s reverting diropq failed(%d, %d)\n",
593
DLNPair(dentry), err, rerr);
599
LKTRLabel(revert dir);
600
rerr = vfsub_rmdir(hidden_dir, hidden_dentry, dlgt);
603
IOErr("%.*s reverting dir failed(%d, %d)\n",
604
DLNPair(dentry), err, rerr);
608
dtime_revert(&dt, /*fake flag*/CPUP_LOCKED_GHDIR);
610
hdir_unlock(hidden_dir, dir, bindex);
614
au_update_dbstart(dentry);
617
di_write_unlock(parent);
618
aufs_read_unlock(dentry, AUFS_D_WLOCK);