Skip to content

Commit

Permalink
fix locks
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Feb 28, 2022
1 parent 69b0f36 commit b55bae1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
42 changes: 32 additions & 10 deletions pkg/storage/utils/decomposedfs/node/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"io/fs"
"os"
"path/filepath"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand All @@ -36,7 +37,7 @@ import (

// SetLock sets a lock on the node
func (n *Node) SetLock(ctx context.Context, lock *provider.Lock) error {
nodepath := n.LockFilePath()
lockFilePath := n.LockFilePath()
// check existing lock

if l, _ := n.ReadLock(ctx); l != nil {
Expand All @@ -45,13 +46,17 @@ func (n *Node) SetLock(ctx context.Context, lock *provider.Lock) error {
return errtypes.Locked(l.LockId)
}

err := os.Remove(n.LockFilePath())
err := os.Remove(lockFilePath)
if err != nil {
return err
}
}

fileLock, err := filelocks.AcquireWriteLock(n.LockFilePath())
// ensure parent path exists
if err := os.MkdirAll(filepath.Dir(lockFilePath), 0700); err != nil {
return errors.Wrap(err, "Decomposedfs: error creating parent folder for lock")
}
fileLock, err := filelocks.AcquireWriteLock(n.InternalPath())

if err != nil {
return err
Expand All @@ -67,7 +72,7 @@ func (n *Node) SetLock(ctx context.Context, lock *provider.Lock) error {
}()

// O_EXCL to make open fail when the file already exists
f, err := os.OpenFile(nodepath, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600)
f, err := os.OpenFile(lockFilePath, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return errors.Wrap(err, "Decomposedfs: could not create lock file")
}
Expand All @@ -83,7 +88,11 @@ func (n *Node) SetLock(ctx context.Context, lock *provider.Lock) error {
// ReadLock reads the lock id for a node
func (n Node) ReadLock(ctx context.Context) (*provider.Lock, error) {

fileLock, err := filelocks.AcquireReadLock(n.LockFilePath())
// ensure parent path exists
if err := os.MkdirAll(filepath.Dir(n.InternalPath()), 0700); err != nil {
return nil, errors.Wrap(err, "Decomposedfs: error creating parent folder for lock")
}
fileLock, err := filelocks.AcquireReadLock(n.InternalPath())

if err != nil {
return nil, err
Expand Down Expand Up @@ -118,7 +127,11 @@ func (n Node) ReadLock(ctx context.Context) (*provider.Lock, error) {
// RefreshLock refreshes the node's lock
func (n *Node) RefreshLock(ctx context.Context, lock *provider.Lock) error {

fileLock, err := filelocks.AcquireWriteLock(n.LockFilePath())
// ensure parent path exists
if err := os.MkdirAll(filepath.Dir(n.InternalPath()), 0700); err != nil {
return errors.Wrap(err, "Decomposedfs: error creating parent folder for lock")
}
fileLock, err := filelocks.AcquireWriteLock(n.InternalPath())

if err != nil {
return err
Expand Down Expand Up @@ -171,7 +184,11 @@ func (n *Node) RefreshLock(ctx context.Context, lock *provider.Lock) error {
// Unlock unlocks the node
func (n *Node) Unlock(ctx context.Context, lock *provider.Lock) error {

fileLock, err := filelocks.AcquireWriteLock(n.LockFilePath())
// ensure parent path exists
if err := os.MkdirAll(filepath.Dir(n.InternalPath()), 0700); err != nil {
return errors.Wrap(err, "Decomposedfs: error creating parent folder for lock")
}
fileLock, err := filelocks.AcquireWriteLock(n.InternalPath())

if err != nil {
return err
Expand Down Expand Up @@ -236,8 +253,13 @@ func (n *Node) CheckLock(ctx context.Context) error {
return nil // ok
}

func readLocksIntoOpaque(ctx context.Context, lockPath string, ri *provider.ResourceInfo) error {
fileLock, err := filelocks.AcquireReadLock(lockPath)
func readLocksIntoOpaque(ctx context.Context, n *Node, ri *provider.ResourceInfo) error {

// ensure parent path exists
if err := os.MkdirAll(filepath.Dir(n.InternalPath()), 0700); err != nil {
return errors.Wrap(err, "Decomposedfs: error creating parent folder for lock")
}
fileLock, err := filelocks.AcquireReadLock(n.InternalPath())

if err != nil {
return err
Expand All @@ -252,7 +274,7 @@ func readLocksIntoOpaque(ctx context.Context, lockPath string, ri *provider.Reso
}
}()

f, err := os.Open(lockPath)
f, err := os.Open(n.LockFilePath())
if err != nil {
appctx.GetLogger(ctx).Error().Err(err).Msg("Decomposedfs: could not open lock file")
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/utils/decomposedfs/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func (n *Node) AsResourceInfo(ctx context.Context, rp *provider.ResourcePermissi
// read locks
if _, ok := mdKeysMap[LockdiscoveryKey]; returnAllKeys || ok {
if n.hasLocks(ctx) {
err = readLocksIntoOpaque(ctx, n.LockFilePath(), ri)
err = readLocksIntoOpaque(ctx, n, ri)
if err != nil {
sublog.Debug().Err(errtypes.InternalError("lockfail"))
}
Expand Down

0 comments on commit b55bae1

Please sign in to comment.