From 488111ce390218806fca933b89279b766f7ff49c Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 12 Apr 2020 17:15:03 +0200 Subject: inomap: split into separate package inomap will also be used by fusefrontend_reverse in the future. Split if off openfiletable to make it independent. --- internal/fusefrontend/file.go | 7 +-- internal/fusefrontend/fs.go | 10 ++-- internal/inomap/inomap.go | 63 ++++++++++++++++++++++++ internal/inomap/inomap_test.go | 81 +++++++++++++++++++++++++++++++ internal/inomap/qino.go | 25 ++++++++++ internal/openfiletable/inummap.go | 63 ------------------------ internal/openfiletable/inummap_test.go | 81 ------------------------------- internal/openfiletable/open_file_table.go | 31 +++--------- 8 files changed, 184 insertions(+), 177 deletions(-) create mode 100644 internal/inomap/inomap.go create mode 100644 internal/inomap/inomap_test.go create mode 100644 internal/inomap/qino.go delete mode 100644 internal/openfiletable/inummap.go delete mode 100644 internal/openfiletable/inummap_test.go diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go index 83e39be..8c2bae0 100644 --- a/internal/fusefrontend/file.go +++ b/internal/fusefrontend/file.go @@ -17,6 +17,7 @@ import ( "github.com/hanwen/go-fuse/fuse/nodefs" "github.com/rfjakob/gocryptfs/internal/contentenc" + "github.com/rfjakob/gocryptfs/internal/inomap" "github.com/rfjakob/gocryptfs/internal/openfiletable" "github.com/rfjakob/gocryptfs/internal/serialize_reads" "github.com/rfjakob/gocryptfs/internal/stupidgcm" @@ -42,7 +43,7 @@ type File struct { // Content encryption helper contentEnc *contentenc.ContentEnc // Device and inode number uniquely identify the backing file - qIno openfiletable.QIno + qIno inomap.QIno // Entry in the open file table fileTableEntry *openfiletable.Entry // Store where the last byte was written @@ -66,7 +67,7 @@ func NewFile(fd *os.File, fs *FS) (*File, fuse.Status) { tlog.Warn.Printf("NewFile: Fstat on fd %d failed: %v\n", fd.Fd(), err) return nil, fuse.ToStatus(err) } - qi := openfiletable.QInoFromStat(&st) + qi := inomap.QInoFromStat(&st) e := openfiletable.Register(qi) return &File{ @@ -462,7 +463,7 @@ func (f *File) GetAttr(a *fuse.Attr) fuse.Status { if err != nil { return fuse.ToStatus(err) } - f.fs.inumMap.TranslateStat(&st) + f.fs.inoMap.TranslateStat(&st) a.FromStat(&st) a.Size = f.contentEnc.CipherSizeToPlainSize(a.Size) if f.fs.args.ForceOwner != nil { diff --git a/internal/fusefrontend/fs.go b/internal/fusefrontend/fs.go index d03fc77..cfd17e3 100644 --- a/internal/fusefrontend/fs.go +++ b/internal/fusefrontend/fs.go @@ -18,8 +18,8 @@ import ( "github.com/rfjakob/gocryptfs/internal/configfile" "github.com/rfjakob/gocryptfs/internal/contentenc" + "github.com/rfjakob/gocryptfs/internal/inomap" "github.com/rfjakob/gocryptfs/internal/nametransform" - "github.com/rfjakob/gocryptfs/internal/openfiletable" "github.com/rfjakob/gocryptfs/internal/serialize_reads" "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/internal/tlog" @@ -59,9 +59,9 @@ type FS struct { IsIdle uint32 // dirCache caches directory fds dirCache dirCacheStruct - // inumMap translates inode numbers from different devices to unique inode + // inoMap translates inode numbers from different devices to unique inode // numbers. - inumMap *openfiletable.InumMap + inoMap *inomap.InoMap } //var _ pathfs.FileSystem = &FS{} // Verify that interface is implemented. @@ -85,7 +85,7 @@ func NewFS(args Args, c *contentenc.ContentEnc, n nametransform.NameTransformer) args: args, nameTransform: n, contentEnc: c, - inumMap: openfiletable.NewInumMap(uint64(st.Dev)), // cast is needed for Darwin + inoMap: inomap.New(uint64(st.Dev)), // cast is needed for Darwin } } @@ -109,7 +109,7 @@ func (fs *FS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.S } a := &fuse.Attr{} st2 := syscallcompat.Unix2syscall(st) - fs.inumMap.TranslateStat(&st2) + fs.inoMap.TranslateStat(&st2) a.FromStat(&st2) if a.IsRegular() { a.Size = fs.contentEnc.CipherSizeToPlainSize(a.Size) diff --git a/internal/inomap/inomap.go b/internal/inomap/inomap.go new file mode 100644 index 0000000..f8909c7 --- /dev/null +++ b/internal/inomap/inomap.go @@ -0,0 +1,63 @@ +package inomap + +import ( + "sync" + "syscall" +) + +// UINT64_MAX = 18446744073709551615 +const inumTranslateBase = 10000000000000000000 + +// InoMap ... see New() for description. +type InoMap struct { + sync.Mutex + baseDev uint64 + translate map[QIno]uint64 + translateNext uint64 +} + +// New returns a new InoMap. +// +// InoMap translates (device uint64, inode uint64) pairs to unique uint64 +// inode numbers. +// Inode numbers on the "baseDev" are passed through unchanged (as long as they +// are not higher than inumTranslateBase). +// Inode numbers on other devices are remapped to the number space above +// 10000000000000000000. The mapping is stored in a simple Go map. Entries +// can only be added and are never removed. +func New(baseDev uint64) *InoMap { + return &InoMap{ + baseDev: baseDev, + translate: make(map[QIno]uint64), + translateNext: inumTranslateBase, + } +} + +// Translate maps the passed-in (device, inode) pair to a unique inode number. +func (m *InoMap) Translate(in QIno) (out uint64) { + if in.Dev == m.baseDev && in.Ino < inumTranslateBase { + return in.Ino + } + m.Lock() + defer m.Unlock() + out = m.translate[in] + if out != 0 { + return out + } + out = m.translateNext + m.translate[in] = m.translateNext + m.translateNext++ + return out +} + +// TranslateStat translates the inode number contained in "st" if neccessary. +// Convience wrapper around Translate(). +func (m *InoMap) TranslateStat(st *syscall.Stat_t) { + in := QInoFromStat(st) + st.Ino = m.Translate(in) +} + +// Count returns the number of entries in the translation table. +func (m *InoMap) Count() int { + return len(m.translate) +} diff --git a/internal/inomap/inomap_test.go b/internal/inomap/inomap_test.go new file mode 100644 index 0000000..3c0ea7d --- /dev/null +++ b/internal/inomap/inomap_test.go @@ -0,0 +1,81 @@ +package inomap + +import ( + "sync" + "testing" +) + +func TestTranslate(t *testing.T) { + const baseDev = 12345 + m := New(baseDev) + + q := QIno{Dev: baseDev, Ino: 1} + out := m.Translate(q) + if out != 1 { + t.Errorf("expected 1, got %d", out) + } + q.Ino = inumTranslateBase + out = m.Translate(q) + if out < inumTranslateBase { + t.Errorf("got %d", out) + } + out2 := m.Translate(q) + if out2 != out { + t.Errorf("unstable mapping: %d %d", out2, out) + } +} + +func TestTranslateStress(t *testing.T) { + const baseDev = 12345 + m := New(baseDev) + var wg sync.WaitGroup + wg.Add(4) + go func() { + q := QIno{Dev: baseDev} + for i := uint64(1); i <= 10000; i++ { + q.Ino = i + out := m.Translate(q) + if out != i { + t.Fail() + } + } + wg.Done() + }() + go func() { + q := QIno{Dev: baseDev} + for i := uint64(1); i <= 10000; i++ { + q.Ino = inumTranslateBase + i + out := m.Translate(q) + if out < inumTranslateBase { + t.Fail() + } + } + wg.Done() + }() + go func() { + q := QIno{Dev: 9999999} + for i := uint64(1); i <= 10000; i++ { + q.Ino = i + out := m.Translate(q) + if out < inumTranslateBase { + t.Fail() + } + } + wg.Done() + }() + go func() { + q := QIno{Dev: 4444444} + for i := uint64(1); i <= 10000; i++ { + q.Ino = i + out := m.Translate(q) + if out < inumTranslateBase { + t.Fail() + } + } + wg.Done() + }() + wg.Wait() + if m.Count() != 30000 { + t.Fail() + } +} diff --git a/internal/inomap/qino.go b/internal/inomap/qino.go new file mode 100644 index 0000000..8f99004 --- /dev/null +++ b/internal/inomap/qino.go @@ -0,0 +1,25 @@ +package inomap + +import ( + "syscall" +) + +// QIno = Qualified Inode number. +// Uniquely identifies a backing file through the device number, +// inode number pair. +type QIno struct { + // Stat_t.{Dev,Ino} is uint64 on 32- and 64-bit Linux + Dev uint64 + Ino uint64 +} + +// QInoFromStat fills a new QIno struct with the passed Stat_t info. +func QInoFromStat(st *syscall.Stat_t) QIno { + return QIno{ + // There are some architectures that use 32-bit values here + // (darwin, freebsd-32, maybe others). Add and explicit cast to make + // this function work everywhere. + Dev: uint64(st.Dev), + Ino: uint64(st.Ino), + } +} diff --git a/internal/openfiletable/inummap.go b/internal/openfiletable/inummap.go deleted file mode 100644 index d9f6862..0000000 --- a/internal/openfiletable/inummap.go +++ /dev/null @@ -1,63 +0,0 @@ -package openfiletable - -import ( - "sync" - "syscall" -) - -// UINT64_MAX = 18446744073709551615 -const inumTranslateBase = 10000000000000000000 - -// InumMap ... see NewInumMap() for description. -type InumMap struct { - sync.Mutex - baseDev uint64 - translate map[QIno]uint64 - translateNext uint64 -} - -// NewInumMap returns a new inumMap. -// -// inumMap translates (device uint64, inode uint64) pairs to unique uint64 -// inode numbers. -// Inode numbers on the "baseDev" are passed through unchanged (as long as they -// are not higher than inumTranslateBase). -// Inode numbers on other devices are remapped to the number space above -// 10000000000000000000. The mapping is stored in a simple Go map. Entries -// can only be added and are never removed. -func NewInumMap(baseDev uint64) *InumMap { - return &InumMap{ - baseDev: baseDev, - translate: make(map[QIno]uint64), - translateNext: inumTranslateBase, - } -} - -// Translate maps the passed-in (device, inode) pair to a unique inode number. -func (m *InumMap) Translate(in QIno) (out uint64) { - if in.Dev == m.baseDev && in.Ino < inumTranslateBase { - return in.Ino - } - m.Lock() - defer m.Unlock() - out = m.translate[in] - if out != 0 { - return out - } - out = m.translateNext - m.translate[in] = m.translateNext - m.translateNext++ - return out -} - -// TranslateStat translates the inode number contained in "st" if neccessary. -// Convience wrapper around Translate(). -func (m *InumMap) TranslateStat(st *syscall.Stat_t) { - in := QInoFromStat(st) - st.Ino = m.Translate(in) -} - -// Count returns the number of entries in the translation table. -func (m *InumMap) Count() int { - return len(m.translate) -} diff --git a/internal/openfiletable/inummap_test.go b/internal/openfiletable/inummap_test.go deleted file mode 100644 index 85438bd..0000000 --- a/internal/openfiletable/inummap_test.go +++ /dev/null @@ -1,81 +0,0 @@ -package openfiletable - -import ( - "sync" - "testing" -) - -func TestTranslate(t *testing.T) { - const baseDev = 12345 - m := NewInumMap(baseDev) - - q := QIno{Dev: baseDev, Ino: 1} - out := m.Translate(q) - if out != 1 { - t.Errorf("expected 1, got %d", out) - } - q.Ino = inumTranslateBase - out = m.Translate(q) - if out < inumTranslateBase { - t.Errorf("got %d", out) - } - out2 := m.Translate(q) - if out2 != out { - t.Errorf("unstable mapping: %d %d", out2, out) - } -} - -func TestTranslateStress(t *testing.T) { - const baseDev = 12345 - m := NewInumMap(baseDev) - var wg sync.WaitGroup - wg.Add(4) - go func() { - q := QIno{Dev: baseDev} - for i := uint64(1); i <= 10000; i++ { - q.Ino = i - out := m.Translate(q) - if out != i { - t.Fail() - } - } - wg.Done() - }() - go func() { - q := QIno{Dev: baseDev} - for i := uint64(1); i <= 10000; i++ { - q.Ino = inumTranslateBase + i - out := m.Translate(q) - if out < inumTranslateBase { - t.Fail() - } - } - wg.Done() - }() - go func() { - q := QIno{Dev: 9999999} - for i := uint64(1); i <= 10000; i++ { - q.Ino = i - out := m.Translate(q) - if out < inumTranslateBase { - t.Fail() - } - } - wg.Done() - }() - go func() { - q := QIno{Dev: 4444444} - for i := uint64(1); i <= 10000; i++ { - q.Ino = i - out := m.Translate(q) - if out < inumTranslateBase { - t.Fail() - } - } - wg.Done() - }() - wg.Wait() - if m.Count() != 30000 { - t.Fail() - } -} diff --git a/internal/openfiletable/open_file_table.go b/internal/openfiletable/open_file_table.go index 4a8ce28..dfd9637 100644 --- a/internal/openfiletable/open_file_table.go +++ b/internal/openfiletable/open_file_table.go @@ -7,28 +7,9 @@ package openfiletable import ( "sync" "sync/atomic" - "syscall" -) - -// QIno = Qualified Inode number. -// Uniquely identifies a backing file through the device number, -// inode number pair. -type QIno struct { - // Stat_t.{Dev,Ino} is uint64 on 32- and 64-bit Linux - Dev uint64 - Ino uint64 -} -// QInoFromStat fills a new QIno struct with the passed Stat_t info. -func QInoFromStat(st *syscall.Stat_t) QIno { - return QIno{ - // There are some architectures that use 32-bit values here - // (darwin, freebsd-32, maybe others). Add and explicit cast to make - // this function work everywhere. - Dev: uint64(st.Dev), - Ino: uint64(st.Ino), - } -} + "github.com/rfjakob/gocryptfs/internal/inomap" +) // wlock - serializes write accesses to each file (identified by inode number) // Writing partial blocks means we have to do read-modify-write cycles. We @@ -38,7 +19,7 @@ func QInoFromStat(st *syscall.Stat_t) QIno { var t table func init() { - t.entries = make(map[QIno]*Entry) + t.entries = make(map[inomap.QIno]*Entry) } type table struct { @@ -52,7 +33,7 @@ type table struct { // Protects map access sync.Mutex // Table entries - entries map[QIno]*Entry + entries map[inomap.QIno]*Entry } // Entry is an entry in the open file table @@ -71,7 +52,7 @@ type Entry struct { // Register creates an open file table entry for "qi" (or incrementes the // reference count if the entry already exists) and returns the entry. -func Register(qi QIno) *Entry { +func Register(qi inomap.QIno) *Entry { t.Lock() defer t.Unlock() @@ -86,7 +67,7 @@ func Register(qi QIno) *Entry { // Unregister decrements the reference count for "qi" and deletes the entry from // the open file table if the reference count reaches 0. -func Unregister(qi QIno) { +func Unregister(qi inomap.QIno) { t.Lock() defer t.Unlock() -- cgit v1.2.3