diff --git a/hw/atapi-pt.c b/hw/atapi-pt.c
-index 09cf6bb..aed333d 100644
+index 09cf6bb..53519cd 100644
--- a/hw/atapi-pt.c
+++ b/hw/atapi-pt.c
@@ -29,7 +29,6 @@ static int debug_enabled=-1;
#define IDE_ATAPI_PT_DEBUG_FILE_TEMPLATE "/var/log/cdrom-%d.log"
-@@ -386,6 +385,90 @@ static const struct {
+@@ -386,6 +385,96 @@ static const struct {
{ 0x000000, 0x000000, "Unrecognised sense data"}
};
+#define ATAPI_PT_LOCK_STATE_LOCKED_BY_ME 1
+#define ATAPI_PT_LOCK_STATE_LOCKED_BY_OTHER 2
+
-+static int lock_fd = -1, we_have_lock = 0;
++static int we_have_lock = 0;
+
-+static int open_lock_file(void)
++static int get_lock_fd(void)
+{
-+ lock_fd = open( IDE_ATAPI_PT_EXCLUSIVE_CD_FILE, O_RDWR | O_CREAT, 0666);
-+ return (lock_fd<0) ? 1:0;
++ static int lock_fd=-1;
++ if (lock_fd <0) {
++ lock_fd = open( IDE_ATAPI_PT_EXCLUSIVE_CD_FILE, O_RDWR | O_CREAT, 0666);
++ }
++ return lock_fd;
+}
+
+
+static int get_atapi_pt_lock_state(void)
+{
+ struct flock lock = {0};
++ int lock_fd=get_lock_fd();
++
++ if (lock_fd<0) return ATAPI_PT_LOCK_STATE_UNLOCKED;
+
+ if (we_have_lock)
+ return ATAPI_PT_LOCK_STATE_LOCKED_BY_ME;
+
-+ if (open_lock_file()) return ATAPI_PT_LOCK_STATE_UNLOCKED;
-+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+static int get_atapi_pt_lock(void)
+{
+ struct flock lock = {0};
-+
-+ if (open_lock_file()) return -1;
++ int lock_fd=get_lock_fd();
++
++ if (lock_fd<0) return -1;
+
+ if (we_have_lock)
+ return 0;
+static int release_atapi_pt_lock(void)
+{
+ struct flock lock = {0};
-+
-+ if (open_lock_file()) return -1;
++ int lock_fd=get_lock_fd();
++
++ if (lock_fd<0) return -1;
+
+ if (!we_have_lock)
+ return 0;
static const char *atapi_sense_to_str(int key, int asc, int ascq)
{
int i;
-@@ -1007,8 +1090,7 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer)
+@@ -1007,8 +1096,7 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer)
static void ide_atapi_pt_ejected(void)
{
}
-@@ -1016,7 +1098,6 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -1016,7 +1104,6 @@ static void ide_atapi_pt_cmd(IDEState *s)
{
uint8_t cmd_code;
FILE *fp;
if(pthread_mutex_trylock(&s->atapi_pt.sgio_mutex)) {
DEBUG_PRINTF("ide_atapi_pt_cmd() called with existing request processing - ignored!\n");
-@@ -1069,14 +1150,9 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -1069,14 +1156,9 @@ static void ide_atapi_pt_cmd(IDEState *s)
s->atapi_pt.din_xfer_len = ide_atapi_pt_get_data_size(ide_atapi_pt_size_buffer,
cmd_code, s->atapi_pt.request);
{
uint8_t sense[18] = {0x70, 0, 2, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x3a, 1, 0, 0, 0, 0};
-@@ -1118,17 +1194,17 @@ static void ide_atapi_pt_cmd(IDEState *s)
+@@ -1118,17 +1200,17 @@ static void ide_atapi_pt_cmd(IDEState *s)
cmd_code == GPCMD_WRITE_10 || cmd_code == GPCMD_WRITE_12 || cmd_code == GPCMD_WRITE_AND_VERIFY_10 ||
cmd_code == GPCMD_WRITE_BUFFER)
{