2
* Copyright (C) 2005-2008 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
20
* inode operations (add entry)
22
* $Id: i_op_add.c,v 1.4 2008/05/04 23:54:53 sfjro Exp $
28
* final procedure of adding a new entry, except link(2).
29
* remove whiteout, instantiate, copyup the parent dir's times and size
31
* if it failed, re-create the removed whiteout.
33
static int epilog(struct inode *dir, struct dentry *wh_dentry,
34
struct dentry *dentry)
38
struct inode *inode, *h_dir;
41
struct super_block *sb;
43
LKTRTrace("wh %p, %.*s\n", wh_dentry, AuDLNPair(dentry));
47
h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
49
bwh = au_dbwh(dentry);
50
err = au_wh_unlink_dentry(h_dir, wh_dentry, dentry, dir,
57
inode = au_new_inode(dentry);
58
//inode = ERR_PTR(-1);
60
d_instantiate(dentry, inode);
61
dir = dentry->d_parent->d_inode; /* dir inode is locked */
63
/* or always cpup dir mtime? */
64
if (au_ibstart(dir) == au_dbstart(dentry))
65
au_cpup_attr_timesizes(dir);
67
return 0; /* success */
77
if (unlikely(au_opt_test_dlgt(au_mntflags(sb))))
78
au_fset_ndx(ndx.flags, DLGT);
79
ndx.nfsmnt = au_nfsmnt(sb, bwh);
82
/* dir inode is locked */
83
wh = au_wh_create(dir, dentry, bwh, wh_dentry->d_parent, &ndx);
90
AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
91
AuDLNPair(dentry), err, rerr);
100
* simple tests for the adding inode operations.
101
* following the checks in vfs, plus the parent-child relationship.
103
int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
104
struct dentry *h_parent, int isdir, struct au_ndx *ndx)
107
struct dentry *h_dentry;
108
struct inode *h_inode;
111
LKTRTrace("%.*s/%.*s, b%d, dir %d\n",
112
AuDLNPair(h_parent), AuDLNPair(dentry), bindex, isdir);
114
exist = !!dentry->d_inode;
115
h_dentry = au_h_dptr(dentry, bindex);
116
h_inode = h_dentry->d_inode;
119
if (unlikely(h_inode))
124
if (unlikely(!h_inode || !h_inode->i_nlink))
127
h_mode = h_inode->i_mode;
130
if (unlikely(S_ISDIR(h_mode)))
132
} else if (unlikely(!S_ISDIR(h_mode))) {
139
/* expected parent dir is locked */
140
if (unlikely(h_parent != h_dentry->d_parent))
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;
149
if (unlikely(au_test_h_perm
150
(h_parent->d_inode, MAY_EXEC | MAY_WRITE,
151
au_ftest_ndx(ndx->flags, DLGT))))
155
h_latest = au_sio_lkup_one(qstr->name, h_parent, qstr->len,
157
err = PTR_ERR(h_latest);
158
if (IS_ERR(h_latest))
162
if (h_latest == h_dentry)
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.
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)
180
struct dentry *wh_dentry, *parent, *h_parent, *gparent;
182
aufs_bindex_t bstart, bcpup;
183
struct inode *dir, *h_dir, *gdir;
185
struct super_block *sb;
186
struct au_hinode *hgdir;
187
unsigned int mnt_flags;
189
LKTRTrace("%.*s, src %p\n", AuDLNPair(dentry), src_dentry);
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);
197
wh_dentry = ERR_PTR(err);
198
if (unlikely(err < 0))
202
mnt_flags = au_mntflags(sb);
203
//todo: meaningless lock if CONFIG_AUFS_DEBUG is disabled.
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);
214
dir = parent->d_inode;
215
h_parent = au_h_dptr(parent, bcpup);
216
h_dir = h_parent->d_inode;
218
AuDbgSleep_UdbaRace();
219
au_hdir_lock(h_dir, dir, bcpup);
220
//todo: revalidate the lower dentry?
222
ndx.nfsmnt = au_nfsmnt(sb, bcpup);
224
if (unlikely(au_opt_test_dlgt(mnt_flags)))
225
au_fset_ndx(ndx.flags, DLGT);
228
//ndx.nd_file = NULL;
230
if (!au_opt_test(mnt_flags, UDBA_NONE) && au_dbstart(dentry) == bcpup) {
233
if (unlikely(ndx.nfsmnt)) {
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;
241
err = au_may_add(dentry, bcpup, h_parent,
242
au_ftest_wrdir(wr_dir_args->flags, ISDIR),
244
wh_dentry = ERR_PTR(err);
252
au_dtime_store(dt, parent, h_parent, hgdir);
254
if (/* bcpup != bstart || */ bcpup != au_dbwh(dentry))
255
goto out; /* success */
257
wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, &ndx);
258
//wh_dentry = ERR_PTR(-1);
261
if (IS_ERR(wh_dentry))
262
au_hdir_unlock(h_dir, dir, bcpup);
264
AuTraceErrPtr(wh_dentry);
268
/* ---------------------------------------------------------------------- */
270
enum { Mknod, Symlink, Creat };
276
struct nameidata *nd;
288
static int add_simple(struct inode *dir, struct dentry *dentry,
289
struct simple_arg *arg)
291
int err, dlgt, created;
292
struct dentry *h_dentry, *h_parent, *wh_dentry, *parent;
295
struct vfsub_args vargs;
296
struct super_block *sb;
297
aufs_bindex_t bstart;
298
struct au_wr_dir_args wr_dir_args = {
300
.flags = AuWrDir_ADD_ENTRY
303
LKTRTrace("type %d, %.*s\n", arg->type, AuDLNPair(dentry));
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,
311
//wh_dentry = ERR_PTR(-1);
312
err = PTR_ERR(wh_dentry);
313
if (IS_ERR(wh_dentry))
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;
322
dlgt = !!au_opt_test_dlgt(au_mntflags(sb));
324
#if 1 // partial testing
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));
332
err = vfsub_symlink(h_dir, h_dentry, arg->u.s.symname,
336
err = vfsub_mknod(h_dir, h_dentry, arg->u.m.mode, arg->u.m.dev,
347
err = epilog(dir, wh_dentry, dentry);
351
if (unlikely(created && err && h_dentry->d_inode)) {
353
vfsub_args_init(&vargs, NULL, dlgt, 0);
354
rerr = vfsub_unlink(h_dir, h_dentry, &vargs);
357
AuIOErr("%.*s revert failure(%d, %d)\n",
358
AuDLNPair(dentry), err, rerr);
361
//todo: inotify will be fired to the grand parent dir
362
au_dtime_revert(&dt);
366
au_hdir_unlock(h_dir, dir, bstart);
371
au_update_dbstart(dentry);
374
di_write_unlock(parent);
375
aufs_read_unlock(dentry, AuLock_DW);
380
int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
382
struct simple_arg arg = {
389
return add_simple(dir, dentry, &arg);
392
int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
394
struct simple_arg arg = {
396
.u.s.symname = symname
398
return add_simple(dir, dentry, &arg);
401
int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
402
struct nameidata *nd)
404
struct simple_arg arg = {
411
return add_simple(dir, dentry, &arg);
414
/* ---------------------------------------------------------------------- */
417
aufs_bindex_t bdst, bsrc;
419
struct dentry *src_parent, *parent, *h_dentry;
420
struct inode *h_dir, *inode, *dir;
423
static int cpup_before_link(struct dentry *src_dentry, struct inode *dir,
427
struct inode *hi, *h_dir, *src_dir, *gdir;
428
struct dentry *gparent;
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;
443
src_dir = a->src_parent->d_inode;
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);
453
au_hdir_lock(a->h_dir, dir, a->bdst);
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);
468
//todo: test parent-gparent relationship
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);
475
mutex_unlock(&hi->i_mutex);
477
if (unlikely(gparent)) {
478
ii_read_unlock(gdir);
484
au_hdir_unlock(h_dir, src_dir, a->bdst);
485
au_hdir_lock(a->h_dir, dir, a->bdst);
488
di_read_unlock(a->src_parent, AuLock_IR);
494
static int cpup_or_link(struct dentry *src_dentry, struct link_arg *a)
497
struct inode *inode, *h_inode, *h_dst_inode;
498
struct dentry *h_dentry;
499
aufs_bindex_t bstart;
500
struct super_block *sb;
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);
511
if (bstart <= a->bdst)
512
h_dst_inode = au_h_iptr(inode, a->bdst);
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,
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);
526
/* the inode of src_dentry already exists on a.bdst branch */
527
h_dentry = d_find_alias(h_dst_inode);
529
err = vfsub_link(h_dentry, a->h_dir,
530
a->h_dentry, a->dlgt);
533
AuIOErr("no dentry found for i%lu on b%d\n",
534
h_dst_inode->i_ino, a->bdst);
540
au_plink_append(sb, a->inode, a->h_dentry, a->bdst);
546
int aufs_link(struct dentry *src_dentry, struct inode *dir,
547
struct dentry *dentry)
550
struct dentry *h_parent, *wh_dentry, *h_src_dentry;
553
struct super_block *sb;
554
unsigned int mnt_flags;
555
struct vfsub_args vargs;
556
struct au_wr_dir_args wr_dir_args = {
558
.flags = AuWrDir_ADD_ENTRY
561
LKTRTrace("src %.*s, i%lu, dst %.*s\n",
562
AuDLNPair(src_dentry), dir->i_ino, AuDLNPair(dentry));
564
IMustLock(src_dentry->d_inode);
565
AuDebugOn(S_ISDIR(src_dentry->d_inode->i_mode));
567
aufs_read_and_write_lock2(dentry, src_dentry, /*flags*/0);
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);
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))
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;
592
mnt_flags = au_mntflags(sb);
593
a.dlgt = !!au_opt_test_dlgt(mnt_flags);
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))) {
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.
606
/* && h_src_dentry->d_sb != a.h_dentry->d_sb */)
607
err = cpup_before_link(src_dentry, dir, &a);
609
h_src_dentry = au_h_dptr(src_dentry, a.bdst);
610
err = vfsub_link(h_src_dentry, a.h_dir, a.h_dentry,
616
/* && h_src_dentry->d_sb != a.h_dentry->d_sb */)
617
err = cpup_or_link(src_dentry, &a);
619
h_src_dentry = au_h_dptr(src_dentry, a.bdst);
620
err = vfsub_link(h_src_dentry, a.h_dir, a.h_dentry,
628
err = au_wh_unlink_dentry(a.h_dir, wh_dentry, dentry,
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))) {
640
//d_drop(h_src_dentry);
641
//d_drop(src_dentry);
643
a.inode->i_ctime = dir->i_ctime;
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);
656
a.inode->i_ctime = dir->i_ctime;
658
/* nfs case (< 2.6.15) */
666
for (i = au_ibstart(a.inode); i <= au_ibend(a.inode); i++) {
667
struct au_xino_entry xinoe;
669
hi = au_h_iptr(a.inode, i);
671
au_xino_read(sb, i, hi->i_ino, &xinoe);
672
AuDbg("hi%lu, i%lu\n", hi->i_ino, xinoe.ino);
677
goto out_unlock; /* success */
681
if (d_unhashed(a.h_dentry)) {
682
/* hardlink on nfs (< 2.6.15) */
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));
695
AuDebugOn(!d->d_inode);
698
vfsub_args_init(&vargs, NULL, a.dlgt, 0);
699
rerr = vfsub_unlink(a.h_dir, a.h_dentry, &vargs);
704
AuIOErr("%.*s reverting failed(%d, %d)\n",
705
AuDLNPair(dentry), err, rerr);
709
au_dtime_revert(&dt);
711
au_hdir_unlock(a.h_dir, dir, a.bdst);
715
au_update_dbstart(dentry);
718
di_write_unlock(a.parent);
720
aufs_read_and_write_unlock2(dentry, src_dentry);
725
int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
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;
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 = {
737
.flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
740
LKTRTrace("i%lu, %.*s, mode 0%o\n",
741
dir->i_ino, AuDLNPair(dentry), mode);
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,
749
//wh_dentry = ERR_PTR(-1);
750
err = PTR_ERR(wh_dentry);
751
if (IS_ERR(wh_dentry))
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;
760
mnt_flags = au_mntflags(sb);
761
dlgt = !!au_opt_test_dlgt(mnt_flags);
763
err = vfsub_mkdir(h_dir, h_dentry, mode, dlgt);
767
h_inode = h_dentry->d_inode;
769
/* make the dir opaque */
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))
783
err = epilog(dir, wh_dentry, dentry);
787
goto out_unlock; /* success */
792
LKTRLabel(revert opq);
793
mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
794
rerr = au_diropq_remove(dentry, bindex, dlgt);
796
mutex_unlock(&h_inode->i_mutex);
798
AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
799
AuDLNPair(dentry), err, rerr);
805
LKTRLabel(revert dir);
806
vfsub_args_init(&vargs, NULL, dlgt, 0);
807
rerr = vfsub_rmdir(h_dir, h_dentry, &vargs);
810
AuIOErr("%.*s reverting dir failed(%d, %d)\n",
811
AuDLNPair(dentry), err, rerr);
815
au_dtime_revert(&dt);
817
au_hdir_unlock(h_dir, dir, bindex);
821
au_update_dbstart(dentry);
824
di_write_unlock(parent);
825
aufs_read_unlock(dentry, AuLock_DW);