Skip to content

Commit

Permalink
cephfs:Set object lock for volumes for cephfs encryption
Browse files Browse the repository at this point in the history
Signed-off-by: Sunnatillo <sunnat.samadov@est.tech>
  • Loading branch information
Sunnatillo committed Jul 1, 2024
1 parent 29dde7a commit d1aef8f
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions internal/cephfs/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path"
"strings"
"time"

cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
"github.com/ceph/ceph-csi/internal/cephfs/mounter"
Expand Down Expand Up @@ -127,13 +128,47 @@ func maybeUnlockFileEncryption(
stagingTargetPath string,
volID fsutil.VolumeID,
) error {
if volOptions.IsEncrypted() {
log.DebugLog(ctx, "cephfs: unlocking fscrypt on volume %q path %s", volID, stagingTargetPath)

if volOptions.IsEncrypted() == false {
return nil
}

// Define Mutex Lock variables
lockName := string(volID) + "-mutexLock"
lockCookie := lockName + "-coockie"
lockDesc := "Lock for " + string(volID)
lockDuration := 40 * time.Second
var flags byte = 0

log.DebugLog(ctx, "Creating lock for the following volume ID %s", volID)

ioctx, err := volOptions.GetConnection().GetIoctx(volOptions.MetadataPool)
if err != nil {
}
defer ioctx.Destroy()

res, err := ioctx.LockExclusive(volOptions.VolID, lockName, string(lockCookie), lockDesc, lockDuration, &flags)
if err != nil {
return fmt.Errorf("Failed to lock volume ID %v: %s", volID, err)
}
log.DebugLog(ctx, "Lock successfully created for the volume ID %s", volID)

if res == 0 {
log.DebugLog(ctx, "cephfs: unlocking fscrypt on volume %q path %s", volID, stagingTargetPath)
return fscrypt.Unlock(ctx, volOptions.Encryption, stagingTargetPath, string(volID))
}
defer func() {
log.DebugLog(ctx, "Releasing following lock %s", lockName)

return nil
_, err = ioctx.Unlock(string(volID), lockName, lockCookie)

if err != nil {
log.ErrorLog(ctx, "failed to release following lock, this will lead to orphan lock %s: %v",
lockName, err)
}
}()

return fmt.Errorf("There is already one file system with name %s", string(volID))
}

// maybeInitializeFileEncryption initializes KMS and node specifics, if volContext enables encryption.
Expand Down

0 comments on commit d1aef8f

Please sign in to comment.