Skip to content

Commit

Permalink
Alternate implementation of GetLock
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Oct 24, 2023
1 parent 466469a commit 000e7a7
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pkg/storage/fs/cephfs/cephfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,22 @@ func (fs *cephfs) GetLock(ctx context.Context, ref *provider.Reference) (*provid

var l *provider.Lock
user.op(func(cv *cacheVal) {
buf, errXattr := cv.mount.GetXattr(path, xattrLock)
if errXattr == nil {
if l, err = decodeLock(string(buf)); err != nil {
err = errors.Wrap(err, "malformed lock payload")
return
}
}

var file *goceph.File
defer closeFile(file)
if file, err = cv.mount.Open(path, os.O_RDWR, fs.conf.FilePerms); err != nil {
// TODO(lopresti) if user has read-only permissions, here we fail because
// we want to try and grab a lock to probe if a lock existed. Alternatively,
// we could just return the metadata if present.
// try and open with read-only permissions: if this succeeds, we just return
// the metadata as is, otherwise we return the error on Open()
if file, err = cv.mount.Open(path, os.O_RDONLY, fs.conf.FilePerms); err != nil {
l = nil
}
return
}

Expand All @@ -718,21 +728,14 @@ func (fs *cephfs) GetLock(ctx context.Context, ref *provider.Reference) (*provid
return
}

buf, err := cv.mount.GetXattr(path, xattrLock)
if err != nil {
if errXattr != nil {
// error here means we have a "foreign" flock with no CS3 metadata
err = nil
l = new(provider.Lock)
l.AppName = "External"
return
}

if l, err = decodeLock(string(buf)); err != nil {
l = nil
err = errors.Wrap(err, "cephfs: malformed lock payload")
return
}

if time.Unix(int64(l.Expiration.Seconds), 0).After(time.Now()) {
// the lock expired, drop
fs.UnsetArbitraryMetadata(ctx, ref, []string{xattrLock})
Expand Down Expand Up @@ -775,7 +778,7 @@ func (fs *cephfs) RefreshLock(ctx context.Context, ref *provider.Reference, newL
}

if existingLockID != "" && oldLock.LockId != existingLockID {
return errtypes.BadRequest("mismatching existing lock id")
return errtypes.BadRequest("lock id does not match")
}

return fs.SetLock(ctx, ref, newLock)
Expand Down

0 comments on commit 000e7a7

Please sign in to comment.