Browse Source

block: support locking on change medium

New optional argument for 'blockdev-change-medium' QAPI command to allow
the caller to specify if they wish to enable file locking.
osy 1 year ago
parent
commit
7129cfcc82
4 changed files with 46 additions and 1 deletions
  1. 22 0
      block/qapi-sysemu.c
  2. 1 0
      monitor/hmp-cmds.c
  3. 22 1
      qapi/block.json
  4. 1 0
      ui/cocoa/app_controller.m

+ 22 - 0
block/qapi-sysemu.c

@@ -321,6 +321,8 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
                                 bool has_force, bool force,
                                 bool has_read_only,
                                 BlockdevChangeReadOnlyMode read_only,
+                                bool has_file_locking_mode,
+                                BlockdevChangeFileLockingMode file_locking_mode,
                                 Error **errp)
 {
     BlockBackend *blk;
@@ -374,6 +376,26 @@ void qmp_blockdev_change_medium(bool has_device, const char *device,
         qdict_put_str(options, "driver", format);
     }
 
+    if (!has_file_locking_mode) {
+        file_locking_mode = BLOCKDEV_CHANGE_FILE_LOCKING_MODE_AUTO;
+    }
+
+    switch (file_locking_mode) {
+    case BLOCKDEV_CHANGE_FILE_LOCKING_MODE_AUTO:
+        break;
+
+    case BLOCKDEV_CHANGE_FILE_LOCKING_MODE_OFF:
+        qdict_put_str(options, "file.locking", "off");
+        break;
+
+    case BLOCKDEV_CHANGE_FILE_LOCKING_MODE_ON:
+        qdict_put_str(options, "file.locking", "on");
+        break;
+
+    default:
+        abort();
+    }
+
     medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp);
     if (!medium_bs) {
         goto fail;

+ 1 - 0
monitor/hmp-cmds.c

@@ -1499,6 +1499,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
         qmp_blockdev_change_medium(true, device, false, NULL, target,
                                    !!arg, arg, true, force,
                                    !!read_only, read_only_mode,
+                                   false, 0,
                                    &err);
     }
 

+ 22 - 1
qapi/block.json

@@ -303,6 +303,23 @@
 { 'enum': 'BlockdevChangeReadOnlyMode',
   'data': ['retain', 'read-only', 'read-write'] }
 
+##
+# @BlockdevChangeFileLockingMode:
+#
+# Specifies the new locking mode of a file image passed to the
+# @blockdev-change-medium command.
+#
+# @auto: Use locking if API is available
+#
+# @off: Disable file image locking
+#
+# @on: Enable file image locking
+#
+# Since: 9.2
+##
+{ 'enum': 'BlockdevChangeFileLockingMode',
+  'data': ['auto', 'off', 'on'] }
+
 ##
 # @blockdev-change-medium:
 #
@@ -324,6 +341,9 @@
 # @read-only-mode: change the read-only mode of the device; defaults
 #                  to 'retain'
 #
+# @file-locking-mode: change the locking mode of the file image; defaults
+#                     to 'auto'
+#
 # @force: if false (the default), an eject request through blockdev-open-tray
 #         will be sent to the guest if it has locked the tray (and the tray
 #         will not be opened immediately); if true, the tray will be opened
@@ -371,7 +391,8 @@
             'filename': 'str',
             '*format': 'str',
             '*force': 'bool',
-            '*read-only-mode': 'BlockdevChangeReadOnlyMode' } }
+            '*read-only-mode': 'BlockdevChangeReadOnlyMode',
+            '*file-locking-mode': 'BlockdevChangeFileLockingMode' } }
 
 ##
 # @DEVICE_TRAY_MOVED:

+ 1 - 0
ui/cocoa/app_controller.m

@@ -373,6 +373,7 @@ - (void)changeDeviceMedia:(id)sender
                                    "raw",
                                    true, false,
                                    false, 0,
+                                   false, 0,
                                    &err);
         qemu_mutex_unlock_iothread();
         handleAnyDeviceErrors(err);