cephfs: refactor locks using IOCtxLock interface

Signed-off-by: Niraj Yadav <niryadav@redhat.com>
This commit is contained in:
Niraj Yadav 2024-07-26 15:25:37 +05:30 committed by mergify[bot]
parent 4445247690
commit 439a03f21b

View File

@ -23,7 +23,6 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"syscall"
"time" "time"
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors" cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
@ -34,6 +33,7 @@ import (
hc "github.com/ceph/ceph-csi/internal/health-checker" hc "github.com/ceph/ceph-csi/internal/health-checker"
"github.com/ceph/ceph-csi/internal/util" "github.com/ceph/ceph-csi/internal/util"
"github.com/ceph/ceph-csi/internal/util/fscrypt" "github.com/ceph/ceph-csi/internal/util/fscrypt"
iolock "github.com/ceph/ceph-csi/internal/util/lock"
"github.com/ceph/ceph-csi/internal/util/log" "github.com/ceph/ceph-csi/internal/util/log"
"github.com/container-storage-interface/spec/lib/go/csi" "github.com/container-storage-interface/spec/lib/go/csi"
@ -134,12 +134,12 @@ func maybeUnlockFileEncryption(
} }
// Define Mutex Lock variables // Define Mutex Lock variables
lockName := string(volID) + "-mutexLock" volIDStr := string(volID)
lockDesc := "Lock for " + string(volID) lockName := volIDStr + "-mutexLock"
lockDesc := "Lock for " + volIDStr
lockDuration := 150 * time.Second lockDuration := 150 * time.Second
// Generate a consistent lock cookie for the client using hostname and process ID // Generate a consistent lock cookie for the client using hostname and process ID
lockCookie := generateLockCookie() lockCookie := generateLockCookie()
var flags byte = 0
log.DebugLog(ctx, "Creating lock for the following volume ID %s", volID) log.DebugLog(ctx, "Creating lock for the following volume ID %s", volID)
@ -151,31 +151,15 @@ func maybeUnlockFileEncryption(
} }
defer ioctx.Destroy() defer ioctx.Destroy()
res, err := ioctx.LockExclusive(string(volID), lockName, lockCookie, lockDesc, lockDuration, &flags) lock := iolock.NewLock(ioctx, volIDStr, lockName, lockCookie, lockDesc, lockDuration)
if res != 0 { err = lock.LockExclusive(ctx)
switch res { if err != nil {
case -int(syscall.EBUSY): log.ErrorLog(ctx, "failed to create lock for volume ID %s: %v", volID, err)
return fmt.Errorf("Lock is already held by another client and cookie pair for %v volume", volID)
case -int(syscall.EEXIST):
return fmt.Errorf("Lock is already held by the same client and cookie pair for %v volume", volID)
default:
return fmt.Errorf("Failed to lock volume ID %v: %w", volID, err)
}
}
log.DebugLog(ctx, "Lock successfully created for volume ID %s", volID)
defer func() { return err
ret, unlockErr := ioctx.Unlock(string(volID), lockName, lockCookie) }
switch ret { defer lock.Unlock(ctx)
case 0: log.DebugLog(ctx, "Lock successfully created for volume ID %s", volID)
log.DebugLog(ctx, "Lock %s successfully released ", lockName)
case -int(syscall.ENOENT):
log.DebugLog(ctx, "Lock is not held by the specified %s, %s pair", lockCookie, lockName)
default:
log.ErrorLog(ctx, "Failed to release following lock, this will lead to orphan lock %s: %v",
lockName, unlockErr)
}
}()
log.DebugLog(ctx, "cephfs: unlocking fscrypt on volume %q path %s", volID, stagingTargetPath) log.DebugLog(ctx, "cephfs: unlocking fscrypt on volume %q path %s", volID, stagingTargetPath)
err = fscrypt.Unlock(ctx, volOptions.Encryption, stagingTargetPath, string(volID)) err = fscrypt.Unlock(ctx, volOptions.Encryption, stagingTargetPath, string(volID))