16
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
/* $Id: i_op_ren.c,v 1.68 2008/03/31 07:45:32 sfjro Exp $ */
20
* inode operation (rename entry)
23
* $Id: i_op_ren.c,v 1.70 2008/04/13 23:42:46 sfjro Exp $
30
35
#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
31
36
#define au_fset_ren(flags, name) { (flags) |= AuRen_##name; }
32
37
#define au_fclr_ren(flags, name) { (flags) &= ~AuRen_##name; }
38
#ifndef CONFIG_AUFS_DLGT
34
43
struct rename_args {
35
44
struct dentry *h_dentry[2], *parent[2], *h_parent[2], *h_trap;
36
struct aufs_nhash whlist;
45
struct au_nhash whlist;
37
46
aufs_bindex_t btgt, bstart[2];
38
47
struct super_block *sb;
39
48
unsigned int flags;
49
unsigned int mnt_flags;
43
52
static int do_rename(struct inode *src_dir, struct dentry *src_dentry,
45
54
struct rename_args *a)
47
56
int err, need_diropq, bycpup, rerr;
48
struct rmdir_whtmp_args *thargs;
49
struct dentry *wh_dentry[2], *h_dst;
57
struct au_whtmp_rmdir_args *thargs;
58
struct dentry *wh_dentry[2], *h_dst, *h_src;
50
59
struct inode *h_dir[2];
51
60
aufs_bindex_t bindex, bend;
52
struct aufs_hin_ignore ign;
61
struct au_hin_ignore ign;
53
62
struct vfsub_args vargs;
54
struct aufs_ndx ndx = {
90
99
au_fset_ndx(ndx.flags, DLGT);
91
100
/* create whiteout for src_dentry */
92
101
if (au_ftest_ren(a->flags, WHSRC)) {
93
wh_dentry[SRC] = simple_create_wh(src_dir, src_dentry, a->btgt,
94
a->h_parent[SRC], &ndx);
102
wh_dentry[SRC] = au_wh_create(src_dir, src_dentry, a->btgt,
103
a->h_parent[SRC], &ndx);
95
104
//wh_dentry[SRC] = ERR_PTR(-1);
96
105
err = PTR_ERR(wh_dentry[SRC]);
97
106
if (IS_ERR(wh_dentry[SRC]))
115
124
/* rename dentry to tmpwh */
117
err = rename_whtmp(dir, dentry, a->btgt, /*noself*/0);
126
err = au_whtmp_ren(dir, dentry, a->btgt, /*noself*/0);
119
128
if (unlikely(err))
121
set_h_dptr(dentry, a->btgt, NULL);
130
au_set_h_dptr(dentry, a->btgt, NULL);
122
131
err = au_lkup_neg(dentry, a->btgt);
124
133
if (unlikely(err))
140
149
/* rename by vfs_rename or cpup */
141
150
need_diropq = au_ftest_ren(a->flags, ISDIR)
142
151
&& (wh_dentry[DST]
143
|| dbdiropq(dentry) == a->btgt
152
|| au_dbdiropq(dentry) == a->btgt
144
153
/* hide the lower to keep xino */
145
|| a->btgt < dbend(dentry)
146
|| AuFlag(stosi(a->sb), f_always_diropq));
154
|| a->btgt < au_dbend(dentry)
155
|| au_opt_test(a->mnt_flags, ALWAYS_DIROPQ));
148
if (dbstart(src_dentry) == a->btgt) {
149
if (need_diropq && dbdiropq(src_dentry) == a->btgt)
157
if (au_dbstart(src_dentry) == a->btgt) {
158
if (need_diropq && au_dbdiropq(src_dentry) == a->btgt)
151
160
vfsub_args_init(&vargs, &ign, au_ftest_ren(a->flags, DLGT), 0);
152
if (unlikely(a->udba == AuUdba_INOTIFY
161
if (unlikely(au_opt_test(a->mnt_flags, UDBA_INOTIFY)
153
162
&& au_ftest_ren(a->flags, ISDIR)))
154
163
vfsub_ign_hinode(&vargs, IN_MOVE_SELF,
155
itohi(src_dentry->d_inode, a->btgt));
164
au_hi(src_dentry->d_inode, a->btgt));
156
165
err = vfsub_rename(h_dir[SRC], au_h_dptr(src_dentry),
157
166
h_dir[DST], a->h_dentry[DST], &vargs);
161
170
vfsub_i_lock_nested(a->h_dentry[SRC]->d_inode, AuLsc_I_CHILD);
162
set_dbstart(src_dentry, a->btgt);
163
set_h_dptr(src_dentry, a->btgt, dget(a->h_dentry[DST]));
171
au_set_dbstart(src_dentry, a->btgt);
172
au_set_h_dptr(src_dentry, a->btgt, dget(a->h_dentry[DST]));
164
173
err = au_sio_cpup_single(src_dentry, a->btgt, a->bstart[SRC],
165
174
-1, !AuCpup_DTIME);
166
175
//err = -1; // untested dir
167
176
if (unlikely(err)) {
168
set_h_dptr(src_dentry, a->btgt, NULL);
169
set_dbstart(src_dentry, a->bstart[SRC]);
177
au_set_h_dptr(src_dentry, a->btgt, NULL);
178
au_set_dbstart(src_dentry, a->bstart[SRC]);
171
180
vfsub_i_unlock(a->h_dentry[SRC]->d_inode);
179
188
struct inode *h_inode;
181
190
h_inode = au_h_dptr_i(src_dentry, a->btgt)->d_inode;
182
hdir_lock(h_inode, src_dentry->d_inode, a->btgt);
183
diropq = create_diropq(src_dentry, a->btgt,
184
au_ftest_ren(a->flags, DLGT));
191
au_hdir_lock(h_inode, src_dentry->d_inode, a->btgt);
192
diropq = au_diropq_create(src_dentry, a->btgt,
193
au_ftest_ren(a->flags, DLGT));
185
194
//diropq = ERR_PTR(-1);
186
hdir_unlock(h_inode, src_dentry->d_inode, a->btgt);
195
au_hdir_unlock(h_inode, src_dentry->d_inode, a->btgt);
187
196
err = PTR_ERR(diropq);
188
197
if (IS_ERR(diropq))
202
/* update target timestamps */
203
h_src = au_h_dptr(src_dentry);
204
au_update_fuse_h_inode(NULL, h_src); /*ignore*/
205
//src_dentry->d_inode->i_atime = h_src->d_inode->i_atime;
206
src_dentry->d_inode->i_ctime = h_src->d_inode->i_ctime;
193
208
/* remove whiteout for dentry */
194
209
if (wh_dentry[DST]) {
195
err = au_unlink_wh_dentry(h_dir[DST], wh_dentry[DST],
210
err = au_wh_unlink_dentry(h_dir[DST], wh_dentry[DST],
196
211
dentry, dir, /*dlgt*/0);
198
213
if (unlikely(err))
204
219
if (au_test_nfs(h_dst->d_sb)
205
220
|| !nhash_test_longer_wh(&a->whlist, a->btgt,
206
stosi(a->sb)->si_dirwh)) {
207
err = rmdir_whtmp(h_dst, &a->whlist, a->btgt, dir,
208
dentry->d_inode, /*noself*/0);
221
au_sbi(a->sb)->si_dirwh)) {
222
err = au_whtmp_rmdir(h_dst, &a->whlist, a->btgt, dir,
223
dentry->d_inode, /*noself*/0);
209
224
if (unlikely(err))
210
225
AuWarn("failed removing whtmp dir %.*s (%d), "
211
226
"ignored.\n", AuDLNPair(h_dst), err);
213
kick_rmdir_whtmp(h_dst, &a->whlist, a->btgt, dir,
214
dentry->d_inode, /*noself*/0, thargs);
228
au_whtmp_kick_rmdir(h_dst, &a->whlist, a->btgt, dir,
229
dentry->d_inode, /*noself*/0,
230
246
struct inode *h_inode;
232
248
h_inode = au_h_dptr_i(src_dentry, a->btgt)->d_inode;
233
//br_wh_read_lock(stobr(a->sb, a->btgt));
249
//br_wh_read_lock(au_sbr(a->sb, a->btgt));
234
250
/* i_lock simply since inotify is not set to h_inode. */
235
251
vfsub_i_lock_nested(h_inode, AuLsc_I_PARENT);
236
//hdir_lock(h_inode, src_dentry->d_inode, a->btgt);
237
rerr = remove_diropq(src_dentry, a->btgt,
238
au_ftest_ren(a->flags, DLGT));
252
//au_hdir_lock(h_inode, src_dentry->d_inode, a->btgt);
253
rerr = au_diropq_remove(src_dentry, a->btgt,
254
au_ftest_ren(a->flags, DLGT));
240
//hdir_unlock(h_inode, src_dentry->d_inode, a->btgt);
256
//au_hdir_unlock(h_inode, src_dentry->d_inode, a->btgt);
241
257
vfsub_i_unlock(h_inode);
242
//br_wh_read_unlock(stobr(a->sb, a->btgt));
258
//br_wh_read_unlock(au_sbr(a->sb, a->btgt));
244
260
RevertFailure("remove diropq %.*s",
245
261
AuDLNPair(src_dentry));
259
275
AuDebugOn(d->d_inode);
260
276
vfsub_args_init(&vargs, &ign, au_ftest_ren(a->flags, DLGT), 0);
261
if (unlikely(a->udba == AuUdba_INOTIFY
277
if (unlikely(au_opt_test(a->mnt_flags, UDBA_INOTIFY)
262
278
&& au_ftest_ren(a->flags, ISDIR)))
263
279
vfsub_ign_hinode(&vargs, IN_MOVE_SELF,
264
itohi(src_dentry->d_inode, a->btgt));
280
au_hi(src_dentry->d_inode, a->btgt));
265
281
rerr = vfsub_rename
266
282
(h_dir[DST], au_h_dptr_i(src_dentry, a->btgt),
267
283
h_dir[SRC], d, &vargs);
271
//set_h_dptr(src_dentry, a->btgt, NULL);
287
//au_set_h_dptr(src_dentry, a->btgt, NULL);
273
289
RevertFailure("rename %.*s", AuDLNPair(src_dentry));
275
291
vfsub_args_init(&vargs, NULL, au_ftest_ren(a->flags, DLGT), 0);
276
292
rerr = vfsub_unlink(h_dir[DST], a->h_dentry[DST], &vargs);
278
set_h_dptr(src_dentry, a->btgt, NULL);
279
set_dbstart(src_dentry, a->bstart[SRC]);
294
au_set_h_dptr(src_dentry, a->btgt, NULL);
295
au_set_dbstart(src_dentry, a->bstart[SRC]);
281
297
RevertFailure("unlink %.*s",
282
298
AuDLNPair(a->h_dentry[DST]));
300
316
AuDebugOn(d->d_inode);
301
317
vfsub_args_init(&vargs, &ign, au_ftest_ren(a->flags, DLGT), 0);
302
if (unlikely(0 && a->udba == AuUdba_INOTIFY
318
if (unlikely(0 && au_opt_test(a->mnt_flags, UDBA_INOTIFY)
303
319
&& au_ftest_ren(a->flags, ISDIR)))
304
320
vfsub_ign_hinode(&vargs, IN_MOVE_SELF,
305
itohi(dentry->d_inode, a->btgt));
321
au_hi(dentry->d_inode, a->btgt));
306
322
rerr = vfsub_rename(h_dir[DST], h_dst, h_dir[DST], d, &vargs);
311
327
RevertFailure("rename %.*s", AuDLNPair(h_dst));
314
set_h_dptr(dentry, a->btgt, NULL);
315
set_h_dptr(dentry, a->btgt, dget(h_dst));
330
au_set_h_dptr(dentry, a->btgt, NULL);
331
au_set_h_dptr(dentry, a->btgt, dget(h_dst));
318
334
dput(wh_dentry[DST]);
319
335
wh_dentry[DST] = NULL;
321
337
if (wh_dentry[SRC]) {
322
rerr = au_unlink_wh_dentry(h_dir[SRC], wh_dentry[SRC],
338
rerr = au_wh_unlink_dentry(h_dir[SRC], wh_dentry[SRC],
323
339
src_dentry, src_dir, /*dlgt*/0);
328
344
#undef RevertFailure
329
345
d_drop(src_dentry);
330
bend = dbend(src_dentry);
331
for (bindex = dbstart(src_dentry); bindex <= bend; bindex++) {
346
bend = au_dbend(src_dentry);
347
for (bindex = au_dbstart(src_dentry); bindex <= bend; bindex++) {
332
348
struct dentry *hd;
333
349
hd = au_h_dptr_i(src_dentry, bindex);
338
bend = dbend(dentry);
339
for (bindex = dbstart(dentry); bindex <= bend; bindex++) {
354
bend = au_dbend(dentry);
355
for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
340
356
struct dentry *hd;
341
357
hd = au_h_dptr_i(dentry, bindex);
423
439
parent = dentry->d_parent;
424
440
IMustLock(parent->d_inode); /* dir is locked */
426
bdiropq = dbdiropq(parent);
428
if (br_rdonly(stobr(dentry->d_sb, btgt))
442
bdiropq = au_dbdiropq(parent);
443
bwh = au_dbwh(dentry);
444
if (au_br_rdonly(au_sbr(dentry->d_sb, btgt))
429
445
|| (0 <= bdiropq && bdiropq < btgt)
430
446
|| (0 <= bwh && bwh < btgt))
437
453
//todo: meaningless lock if CONFIG_AUFS_DEBUG is disabled.
438
static void au_hgdirs(struct aufs_hinode **hgdir, struct rename_args *a)
454
static void au_hgdirs(struct au_hinode **hgdir, struct rename_args *a)
440
456
struct dentry *gparent[2];
441
457
struct inode *gdir;
443
if (a->udba != AuUdba_INOTIFY)
459
if (!au_opt_test(a->mnt_flags, UDBA_INOTIFY))
446
462
gparent[SRC] = NULL;
449
465
gdir = gparent[SRC]->d_inode;
450
466
if (gparent[SRC] != a->parent[DST]) {
451
467
ii_read_lock_parent3(gdir);
452
hgdir[SRC] = itohi(gdir, a->btgt);
468
hgdir[SRC] = au_hi(gdir, a->btgt);
453
469
ii_read_unlock(gdir);
455
hgdir[SRC] = itohi(gdir, a->btgt);
471
hgdir[SRC] = au_hi(gdir, a->btgt);
456
472
dput(gparent[SRC]);
463
479
gdir = gparent[DST]->d_inode;
464
480
if (gparent[DST] != a->parent[SRC]) {
465
481
ii_read_lock_parent3(gdir);
466
hgdir[DST] = itohi(gdir, a->btgt);
482
hgdir[DST] = au_hi(gdir, a->btgt);
467
483
ii_read_unlock(gdir);
469
hgdir[DST] = itohi(gdir, a->btgt);
485
hgdir[DST] = au_hi(gdir, a->btgt);
470
486
dput(gparent[DST]);
478
494
static int au_may_ren(struct inode *src_dir, struct dentry *src_dentry,
479
495
struct inode *dir, struct dentry *dentry,
480
struct rename_args *a, struct aufs_ndx *ndx)
496
struct rename_args *a, struct au_ndx *ndx)
483
499
struct inode *h_inode;
531
547
int err, do_dt_dstdir, flags;
532
548
aufs_bindex_t bend, bindex;
533
549
struct inode *inode[2], *dirs[2];
534
struct aufs_hinode *hgdir[2], *hdir;
550
struct au_hinode *hgdir[2], *hdir;
535
551
enum { PARENT, CHILD };
536
552
/* reduce stack space */
575
591
aufs_read_and_write_lock2(dentry, src_dentry, flags);
576
if (unlikely(au_need_dlgt(p->a.sb)))
592
p->a.mnt_flags = au_mntflags(p->a.sb);
593
if (unlikely(au_opt_test_dlgt(p->a.mnt_flags)))
577
594
au_fset_ren(p->a.flags, DLGT);
578
p->a.udba = AuFlag(stosi(p->a.sb), f_udba);
579
595
p->a.parent[SRC] = src_dentry->d_parent; /* dir inode is locked */
580
596
p->a.parent[DST] = dentry->d_parent; /* dir inode is locked */
581
597
if (src_dir == dir) {
588
604
/* which branch we process */
589
p->a.bstart[SRC] = dbstart(src_dentry);
590
p->a.bstart[DST] = dbstart(dentry);
605
p->a.bstart[SRC] = au_dbstart(src_dentry);
606
p->a.bstart[DST] = au_dbstart(dentry);
591
607
if (au_ftest_ren(p->a.flags, ISDIR))
592
608
au_fset_wrdir(wr_dir_args.flags, ISDIR);
593
609
wr_dir_args.force_btgt = p->a.bstart[SRC];
604
620
nhash_init(&p->a.whlist);
605
621
if (au_ftest_ren(p->a.flags, ISDIR) && inode[DST]) {
606
set_dbstart(dentry, p->a.bstart[DST]);
622
au_set_dbstart(dentry, p->a.bstart[DST]);
607
623
err = may_rename_dstdir(dentry, p->a.btgt, &p->a.whlist);
608
set_dbstart(dentry, p->a.btgt);
624
au_set_dbstart(dentry, p->a.btgt);
610
626
p->a.h_dentry[DST] = au_h_dptr(dentry);
611
627
if (unlikely(err))
648
664
AuDbgSleep_UdbaRace();
649
p->a.h_trap = hdir_lock_rename(p->a.h_parent, dirs, p->a.btgt,
650
au_ftest_ren(p->a.flags, ISSAMEDIR));
665
p->a.h_trap = au_hdir_lock_rename(p->a.h_parent, dirs, p->a.btgt,
666
au_ftest_ren(p->a.flags, ISSAMEDIR));
652
if (p->a.udba != AuUdba_NONE) {
653
struct aufs_ndx ndx = {
668
if (!au_opt_test(p->a.mnt_flags, UDBA_NONE)) {
669
struct au_ndx ndx = {
654
670
.nfsmnt = au_nfsmnt(p->a.sb, p->a.btgt),
672
688
do_dt_dstdir = 0;
673
689
if (au_ftest_ren(p->a.flags, ISDIR)) {
675
if (unlikely(p->a.udba == AuUdba_INOTIFY))
676
hdir = itohi(p->a.parent[SRC]->d_inode, p->a.btgt);
678
(p->dt[CHILD] + SRC, src_dentry, p->a.h_dentry[SRC],
691
if (unlikely(au_opt_test(p->a.mnt_flags, UDBA_INOTIFY)))
692
hdir = au_hi(p->a.parent[SRC]->d_inode, p->a.btgt);
693
au_dtime_store(p->dt[CHILD] + SRC, src_dentry,
694
p->a.h_dentry[SRC], hdir);
680
695
if (p->a.h_dentry[DST]->d_inode) {
681
696
do_dt_dstdir = 1;
682
if (unlikely(p->a.udba == AuUdba_INOTIFY))
683
hdir = itohi(p->a.parent[DST]->d_inode,
697
if (unlikely(au_opt_test(p->a.mnt_flags, UDBA_INOTIFY)))
698
hdir = au_hi(p->a.parent[DST]->d_inode,
686
(p->dt[CHILD] + DST, dentry, p->a.h_dentry[DST],
700
au_dtime_store(p->dt[CHILD] + DST, dentry,
701
p->a.h_dentry[DST], hdir);
691
705
err = do_rename(src_dir, src_dentry, dir, dentry, &p->a);
692
706
if (unlikely(err))
694
hdir_unlock_rename(p->a.h_parent, dirs, p->a.btgt,
695
au_ftest_ren(p->a.flags, ISSAMEDIR));
708
au_hdir_unlock_rename(p->a.h_parent, dirs, p->a.btgt,
709
au_ftest_ren(p->a.flags, ISSAMEDIR));
697
711
/* update dir attributes */
698
712
dir->i_version++;
708
722
au_cpup_attr_timesizes(inode[DST]);
711
if (ibstart(dir) == p->a.btgt)
725
if (au_ibstart(dir) == p->a.btgt)
712
726
au_cpup_attr_timesizes(dir);
714
728
if (!au_ftest_ren(p->a.flags, ISSAMEDIR)) {
715
729
src_dir->i_version++;
716
730
if (au_ftest_ren(p->a.flags, ISDIR))
717
731
au_cpup_attr_nlink(src_dir);
718
if (ibstart(src_dir) == p->a.btgt)
732
if (au_ibstart(src_dir) == p->a.btgt)
719
733
au_cpup_attr_timesizes(src_dir);
723
737
d_drop(src_dentry);
725
739
/* dput/iput all lower dentries */
726
set_dbwh(src_dentry, -1);
727
bend = dbend(src_dentry);
740
au_set_dbwh(src_dentry, -1);
741
bend = au_dbend(src_dentry);
728
742
for (bindex = p->a.btgt + 1; bindex <= bend; bindex++) {
729
743
struct dentry *hd;
730
744
hd = au_h_dptr_i(src_dentry, bindex);
732
set_h_dptr(src_dentry, bindex, NULL);
746
au_set_h_dptr(src_dentry, bindex, NULL);
734
set_dbend(src_dentry, p->a.btgt);
748
au_set_dbend(src_dentry, p->a.btgt);
736
bend = ibend(inode[SRC]);
750
bend = au_ibend(inode[SRC]);
737
751
for (bindex = p->a.btgt + 1; bindex <= bend; bindex++) {
738
752
struct inode *hi;
739
753
hi = au_h_iptr_i(inode[SRC], bindex);
741
755
//AuDbg("hi%lu, i%lu\n", hi->i_ino, 0LU);
742
xino_write0(p->a.sb, bindex, hi->i_ino, 0);
756
au_xino_write0(p->a.sb, bindex, hi->i_ino, 0);
743
757
/* ignore this error */
744
set_h_iptr(inode[SRC], bindex, NULL, 0);
758
au_set_h_iptr(inode[SRC], bindex, NULL, 0);
747
set_ibend(inode[SRC], p->a.btgt);
761
au_set_ibend(inode[SRC], p->a.btgt);
750
764
#if 0 // remove this
751
765
if (inode[DST]) {
752
766
struct inode *h_i;
754
bend = ibend(inode[DST]);
755
for (bindex = ibstart(inode[DST]); bindex <= bend; bindex++) {
768
bend = au_ibend(inode[DST]);
769
for (bindex = au_ibstart(inode[DST]); bindex <= bend;
756
771
h_i = au_h_iptr_i(inode[DST], bindex);
758
xino_write0(p->a.sb, bindex, h_i->i_ino, 0);
773
au_xino_write0(p->a.sb, bindex, h_i->i_ino, 0);
759
774
/* ignore this error */
760
775
/* bad action? */
786
hdir_unlock_rename(p->a.h_parent, dirs, p->a.btgt,
787
au_ftest_ren(p->a.flags, ISSAMEDIR));
801
au_hdir_unlock_rename(p->a.h_parent, dirs, p->a.btgt,
802
au_ftest_ren(p->a.flags, ISSAMEDIR));
789
804
nhash_fin(&p->a.whlist);