209
211
int err, npositive;
210
212
struct inode *inode, *h_inode;
211
213
struct nameidata tmp_nd, *ndp;
214
aufs_bindex_t bstart;
216
struct super_block *sb;
213
218
LKTRTrace("dir %lu, %.*s, nd{0x%x}\n",
214
219
dir->i_ino, AuDLNPair(dentry), nd ? nd->flags : 0);
215
220
AuDebugOn(IS_ROOT(dentry));
224
si_read_lock(sb, AuLock_FLUSH);
225
err = au_alloc_dinfo(dentry);
218
230
/* nd can be NULL */
231
ndp = au_dup_nd(au_sbi(sb), &tmp_nd, nd);
219
232
parent = dentry->d_parent; /* dir inode is locked */
220
aufs_read_lock(parent, AuLock_FLUSH);
221
err = au_alloc_dinfo(dentry);
226
ndp = au_dup_nd(au_sbi(dir->i_sb), &tmp_nd, nd);
233
di_read_lock_parent(parent, AuLock_IR);
227
234
npositive = au_lkup_dentry(dentry, au_dbstart(parent), /*type*/0, ndp);
235
di_read_unlock(parent, AuLock_IR);
229
237
ret = ERR_PTR(err);
230
238
if (unlikely(err < 0))
235
* stop 'race'-ing between hardlinks under different parents.
237
h_inode = au_h_dptr(dentry, au_dbstart(dentry))->d_inode;
243
bstart = au_dbstart(dentry);
244
h_inode = au_h_dptr(dentry, bstart)->d_inode;
238
245
AuDebugOn(!h_inode);
239
if (h_inode->i_nlink == 1 || S_ISDIR(h_inode->i_mode))
240
inode = au_new_inode(dentry);
242
/* todo: this lock is too large, try br_xino_nondir mutex */
243
static DEFINE_MUTEX(mtx);
245
inode = au_new_inode(dentry);
246
if (!S_ISDIR(h_inode->i_mode)) {
248
* stop 'race'-ing between hardlinks under different
251
mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
253
inode = au_new_inode(dentry);
256
inode = au_new_inode(dentry);
248
257
ret = (void *)inode;
250
259
if (!IS_ERR(inode)) {
384
393
/* ---------------------------------------------------------------------- */
386
static void au_hi_lock(struct inode *h_inode, int isdir, struct inode *inode,
387
aufs_bindex_t bindex)
390
mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
392
au_hdir2_lock(h_inode, inode, bindex);
395
static void au_hi_unlock(struct inode *h_inode, int isdir, struct inode *inode,
396
aufs_bindex_t bindex)
399
mutex_unlock(&h_inode->i_mutex);
401
au_hdir_unlock(h_inode, inode, bindex);
395
struct dentry *au_do_pinned_h_parent(struct au_pin1 *pin, aufs_bindex_t bindex)
397
if (pin && pin->parent)
398
return au_h_dptr(pin->parent, bindex);
402
void au_do_unpin(struct au_pin1 *p, struct au_pin1 *gp)
404
LKTRTrace("%p, %p\n", p, gp);
407
if (unlikely(!p->h_dir))
410
LKTRTrace("p{%.*s, %d, %d, %d, %d}\n",
411
AuDLNPair(p->dentry), p->lsc_di, p->lsc_hi,
412
!!p->parent, !!p->h_dir);
414
mutex_unlock(&p->h_dir->i_mutex);
416
au_do_unpin(gp, NULL);
418
di_read_unlock(p->parent, AuLock_IR);
424
void au_do_pin(struct au_pin1 *p, struct au_pin1 *gp, const aufs_bindex_t bindex,
427
struct dentry *h_dentry;
429
LKTRTrace("%.*s, %d, b%d, %d\n",
430
AuDLNPair(p->dentry), !!gp, bindex, do_gp);
431
AuDebugOn(do_gp && !gp);
434
if (bindex <= au_dbend(p->dentry))
435
h_dentry = au_h_dptr(p->dentry, bindex);
437
while (!IS_ROOT(p->dentry)) {
438
p->parent = dget_parent(p->dentry);
440
di_read_lock(p->parent, AuLock_IR, p->lsc_di);
442
DiMustAnyLock(p->parent);
443
AuDebugOn(!p->parent->d_inode);
444
p->h_dir = au_h_iptr(p->parent->d_inode, bindex);
445
AuDebugOn(!p->h_dir);
446
if (unlikely(do_gp)) {
447
gp->dentry = p->parent;
448
au_do_pin(gp, NULL, bindex, 0);
450
mutex_lock_nested(&p->h_dir->i_mutex, p->lsc_hi);
451
/* todo: call d_revalidate() here? */
452
if (!h_dentry || !au_verify_parent(h_dentry, p->h_dir))
456
AuWarn1("bypassed %.*s/%.*s?\n",
457
AuDLNPair(p->parent), AuDLNPair(p->dentry));
459
AuDbgDentry(p->dentry);
460
AuDbgDentry(h_dentry);
461
AuDbgDentry(p->parent);
462
AuDbgInode(p->h_dir);
464
AuDbgDentry(h_dentry->d_parent);
475
void au_pin_init(struct au_pin *args, struct dentry *dentry, int di_locked,
476
int lsc_di, int lsc_hi, int do_gp)
482
memset(args, 0, sizeof(*args));
483
p = args->pin + AuPin_PARENT;
485
p->di_locked = di_locked;
493
p->lsc_di = lsc_di + 1; /* child first */
494
p->lsc_hi = lsc_hi - 1; /* parent first */
498
void au_pin(struct au_pin *args, struct dentry *dentry, aufs_bindex_t bindex,
499
int di_locked, int do_gp)
501
LKTRTrace("%.*s, b%d, di_locked %d, do_gp %d\n",
502
AuDLNPair(dentry), bindex, di_locked, do_gp);
504
au_pin_init(args, dentry, di_locked, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
506
au_do_pin(args->pin + AuPin_PARENT, au_pin_gp(args), bindex, do_gp);
509
/* ---------------------------------------------------------------------- */
404
511
struct au_icpup_args {
405
512
aufs_bindex_t btgt;
406
unsigned char isdir, did_cpup; /* flags */
407
unsigned char hinotify;
408
struct dentry *parent, *gparent, *h_dentry;
409
struct inode *dir, *gdir, *h_inode, *h_dir;
513
unsigned char isdir, hinotify; /* flags */
514
struct dentry *h_dentry;
515
struct inode *h_inode;
517
struct au_hin_ignore ign[2];
518
struct vfsub_args vargs;
412
521
static int au_lock_and_icpup(struct dentry *dentry, loff_t sz,
413
struct au_icpup_args *rargs)
522
struct au_icpup_args *a)
416
525
aufs_bindex_t bstart;
526
unsigned char did_cpup;
417
527
struct super_block *sb;
418
528
struct dentry *hi_wh;
419
529
struct inode *inode;
428
538
bstart = au_dbstart(dentry);
429
539
sb = dentry->d_sb;
430
540
inode = dentry->d_inode;
431
rargs->isdir = S_ISDIR(inode->i_mode);
541
a->isdir = !!S_ISDIR(inode->i_mode);
542
if (unlikely(a->isdir))
433
543
au_fset_wrdir(wr_dir_args.flags, ISDIR);
544
#if 0 /* todo: fix this */
545
if (unlikely(bstart != au_ibstart(inode))) {
546
AuDebugOn(!au_plink_test(sb, inode));
547
wr_dir_args.force_btgt = au_ibstart(inode);
434
550
err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
435
551
if (unlikely(err < 0))
438
rargs->did_cpup = (err != bstart);
554
did_cpup = (err != bstart);
441
557
/* crazy udba locks */
442
rargs->hinotify = !!au_opt_test(au_mntflags(sb), UDBA_INOTIFY);
443
if (unlikely(!IS_ROOT(dentry))) {
444
rargs->parent = dget_parent(dentry);
445
rargs->dir = rargs->parent->d_inode;
446
di_read_lock_parent(rargs->parent, AuLock_IR);
448
rargs->h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
449
rargs->h_inode = rargs->h_dentry->d_inode;
450
AuDebugOn(!rargs->h_inode);
452
if (!rargs->did_cpup) {
453
au_hi_lock(rargs->h_inode, rargs->isdir, inode, rargs->btgt);
454
/* todo: revalidate the lower dentry? */
558
a->hinotify = !!au_opt_test(au_mntflags(sb), UDBA_INOTIFY);
559
au_pin(&a->pin, dentry, a->btgt, /*di_locked*/0, /*dp_gp*/a->hinotify);
560
a->h_dentry = au_h_dptr(dentry, bstart);
561
a->h_inode = a->h_dentry->d_inode;
562
AuDebugOn(!a->h_inode);
563
mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
565
au_unpin_gp(&a->pin);
455
566
goto out; /* success */
458
if (unlikely(rargs->hinotify
460
&& !IS_ROOT(rargs->parent))) {
461
rargs->gparent = dget_parent(rargs->parent);
462
rargs->gdir = rargs->gparent->d_inode;
463
ii_read_lock_parent2(rargs->gdir);
467
rargs->h_dir = au_h_iptr(rargs->dir, rargs->btgt);
468
au_hdir_lock(rargs->h_dir, rargs->dir, rargs->btgt);
469
/* todo: revalidate the lower dentry? */
470
au_hi_lock(rargs->h_inode, rargs->isdir, inode, bstart);
471
570
if (!d_unhashed(dentry)) {
472
err = au_sio_cpup_simple(dentry, rargs->btgt, sz, AuCpup_DTIME);
571
err = au_sio_cpup_simple(dentry, a->btgt, sz, AuCpup_DTIME);
474
rargs->h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
573
a->h_dentry = au_h_dptr(dentry, a->btgt);
476
hi_wh = au_hi_wh(inode, rargs->btgt);
575
hi_wh = au_hi_wh(inode, a->btgt);
478
err = au_sio_cpup_wh(dentry, rargs->btgt, sz,
577
err = au_sio_cpup_wh(dentry, a->btgt, sz,
481
hi_wh = au_hi_wh(inode, rargs->btgt);
580
hi_wh = au_hi_wh(inode, a->btgt);
482
581
/* todo: revalidate hi_wh? */
485
rargs->h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
584
a->h_dentry = au_h_dptr(dentry, a->btgt);
487
rargs->h_dentry = hi_wh; /* do not dget here */
586
a->h_dentry = hi_wh; /* do not dget here */
490
au_hi_unlock(rargs->h_inode, rargs->isdir, inode, bstart);
491
rargs->h_inode = rargs->h_dentry->d_inode;
492
AuDebugOn(!rargs->h_inode);
494
au_hi_lock(rargs->h_inode, rargs->isdir, inode, rargs->btgt);
495
au_hdir_unlock(rargs->h_dir, rargs->dir, rargs->btgt);
589
mutex_unlock(&a->h_inode->i_mutex);
590
a->h_inode = a->h_dentry->d_inode;
591
AuDebugOn(!a->h_inode);
593
mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
594
au_unpin_gp(&a->pin);
497
595
goto out; /* success */
499
au_hi_unlock(rargs->h_inode, rargs->isdir, inode, rargs->btgt);
500
if (unlikely(rargs->gdir)) {
501
ii_read_unlock(rargs->gdir);
502
dput(rargs->gparent);
504
if (unlikely(rargs->dir)) {
505
di_read_unlock(rargs->parent, AuLock_IR);
510
601
di_write_unlock(dentry);
560
654
ia->ia_valid &= ~ATTR_MODE;
563
vfsub_args_init(&vargs, &ign, au_test_dlgt(au_mntflags(sb)), 0);
564
if (unlikely(rargs.hinotify && rargs.dir)) {
657
if (unlikely(a->hinotify)) {
565
658
events = vfsub_events_notify_change(ia);
567
vfsub_ign_hinode(&vargs, events,
568
au_hi(rargs.dir, rargs.btgt));
660
if (unlikely(a->isdir))
661
vfsub_ign_hinode(&a->vargs, events,
662
au_hi(inode, a->btgt));
663
vfsub_ign_hinode(&a->vargs, events,
664
au_pinned_hdir(&a->pin, a->btgt));
570
err = vfsub_notify_change(rargs.h_dentry, ia, &vargs);
667
err = vfsub_notify_change(a->h_dentry, ia, &a->vargs);
572
669
au_cpup_attr_changeable(inode);
575
au_hi_unlock(rargs.h_inode, rargs.isdir, inode, rargs.btgt);
576
if (unlikely(rargs.gdir)) {
577
ii_read_unlock(rargs.gdir);
580
if (unlikely(rargs.dir)) {
581
di_read_unlock(rargs.parent, AuLock_IR);
672
mutex_unlock(&a->h_inode->i_mutex);
584
674
di_write_unlock(dentry);
587
677
ia->ia_file = file;
588
678
/* ia->ia_valid |= ATTR_FILE; */
589
679
fi_write_unlock(file);
591
681
si_read_unlock(sb);