From d1e001605a36ad13fe77fdc079d983c9d5e57215 Mon Sep 17 00:00:00 2001 From: Eryu Guan Date: Mon, 8 May 2023 00:58:47 +0800 Subject: [PATCH] meta: update atime on Open Signed-off-by: Eryu Guan --- pkg/meta/base.go | 11 ++++++++++- pkg/vfs/vfs_test.go | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/meta/base.go b/pkg/meta/base.go index 402811fd98f16..516a60cc12acf 100644 --- a/pkg/meta/base.go +++ b/pkg/meta/base.go @@ -1495,10 +1495,19 @@ func (m *baseMeta) Rename(ctx Context, parentSrc Ino, nameSrc string, parentDst return st } -func (m *baseMeta) Open(ctx Context, inode Ino, flags uint32, attr *Attr) syscall.Errno { +func (m *baseMeta) Open(ctx Context, inode Ino, flags uint32, attr *Attr) (rerr syscall.Errno) { if m.conf.ReadOnly && flags&(syscall.O_WRONLY|syscall.O_RDWR|syscall.O_TRUNC|syscall.O_APPEND) != 0 { return syscall.EROFS } + + defer func() { + if rerr == 0 { + if err := m.en.touchAtime(ctx, inode, attr); err != 0 { + logger.Warnf("open %v update atime: %s", inode, err) + } + } + }() + if m.conf.OpenCache > 0 && m.of.OpenCheck(inode, attr) { return 0 } diff --git a/pkg/vfs/vfs_test.go b/pkg/vfs/vfs_test.go index e0686aa71d159..d0f260e5e930e 100644 --- a/pkg/vfs/vfs_test.go +++ b/pkg/vfs/vfs_test.go @@ -875,6 +875,9 @@ func TestAtime(t *testing.T) { if fe, e := v.GetAttr(ctx, de.Inode, 0); e != 0 || fe.Attr.Atime != 1234 || fe.Attr.Atimensec != 5678 { t.Fatalf("getattr after readdir d1: %s atime: %v, atimensec %v", e, fe.Attr.Atime, fe.Attr.Atimensec) } + if fe, _, e := v.Open(ctx, fe.Inode, syscall.O_RDWR); e != 0 || fe.Attr.Atime != 1234 || fe.Attr.Atimensec != 5678 { + t.Fatalf("open f1: %s atime: %v, atimensec %v", e, fe.Attr.Atime, fe.Attr.Atimensec) + } // set relatime v.Conf.Meta.AtimeMode = meta.RelAtime @@ -994,4 +997,10 @@ func TestAtime(t *testing.T) { if fe, e := v.GetAttr(ctx, de.Inode, 0); e != 0 || fe.Attr.Atime < now.Unix() { t.Fatalf("getattr after readdir d2: %s atime: %v, now %v", e, fe.Attr.Atime, now.Unix()) } + now = time.Now() + time.Sleep(time.Second) + // update atime on open + if fe, _, e := v.Open(ctx, fe.Inode, syscall.O_RDWR); e != 0 || fe.Attr.Atime < now.Unix() { + t.Fatalf("open f1: %s atime: %v, atimensec %v", e, fe.Attr.Atime, fe.Attr.Atimensec) + } }