summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/vm/0002-bmcweb-handle-device-or-resource-busy-exception.patch
blob: e364fd8872b01ac733c6d54eb9161b3cf87e09e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
From d951041df370dd4c7068b3883d5cc8ef39d044da Mon Sep 17 00:00:00 2001
From: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Date: Fri, 23 Jul 2021 12:07:02 +0000
Subject: [PATCH] bmcweb handle device or resource busy exception

Use async_method_call_timed() for mount/unmount dbus oprations.
Long mount/unmount times are supported by VirtualMedia service,
this works because of settable timeout property, available for each block
device.
Default dbus calls will timeout when mount/unmount timeout is long enough.

Get mount/unmount timeout property and use it for mount/unmount calls.
Add handling of device or resource busy exception (EBUSY) that
can be thrown by VirtualMedia service during Mount/Unmount dbus operations.

Tested: Verified that after mounting non-existing HTTPS resource
        in proxy mode, VirtualMedia recovers restoring ready state
        and returns EBUSY during that transition.
        Verfied that resources can be mounted/unmounted in both legacy
        and proxy mode.
Signed-off-by: Karol Wachowski <karol.wachowski@intel.com>
Change-Id: Ica62c34db0cce24c4c6169fc661edfde49e948d0
---
 redfish-core/lib/virtual_media.hpp | 143 +++++++++++++++++++++--------
 1 file changed, 106 insertions(+), 37 deletions(-)

diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index b0e3b38..779e948 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -26,6 +26,8 @@
 #include <boost/url/url_view.hpp>
 #include <registries/privilege_registry.hpp>
 
+#include <chrono>
+
 namespace redfish
 {
 /**
@@ -143,6 +145,26 @@ inline void
     }
 }
 
+/**
+ * @brief parses Timeout property and converts to microseconds
+ */
+static std::optional<uint64_t>
+    vmParseTimeoutProperty(const std::variant<int>& timeoutProperty)
+{
+    const int* timeoutValue = std::get_if<int>(&timeoutProperty);
+    if (timeoutValue)
+    {
+        constexpr int timeoutMarginSeconds = 10;
+        return std::chrono::duration_cast<std::chrono::microseconds>(
+                   std::chrono::seconds(*timeoutValue + timeoutMarginSeconds))
+            .count();
+    }
+    else
+    {
+        return std::nullopt;
+    }
+}
+
 /**
  * @brief Fill template for Virtual Media Item.
  */
@@ -702,22 +724,58 @@ inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
     }
 
     crow::connections::systemBus->async_method_call(
-        [asyncResp, secretPipe](const boost::system::error_code ec,
-                                bool success) {
+        [asyncResp, service, name, imageUrl, rw, unixFd,
+         secretPipe](const boost::system::error_code ec,
+                     const std::variant<int> timeoutProperty) {
             if (ec)
             {
                 BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
                 messages::internalError(asyncResp->res);
+                return;
             }
-            else if (!success)
+
+            auto timeout = vmParseTimeoutProperty(timeoutProperty);
+            if (timeout == std::nullopt)
             {
-                BMCWEB_LOG_ERROR << "Service responded with error";
-                messages::generalError(asyncResp->res);
+                BMCWEB_LOG_ERROR << "Timeout property is empty.";
+                messages::internalError(asyncResp->res);
+                return;
             }
+
+            crow::connections::systemBus->async_method_call_timed(
+                [asyncResp, secretPipe](const boost::system::error_code ec,
+                                        bool success) {
+                    if (ec)
+                    {
+                        BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
+                        if (ec == boost::system::errc::device_or_resource_busy)
+                        {
+                            messages::resourceInUse(asyncResp->res);
+                        }
+                        else if (ec == boost::system::errc::permission_denied)
+                        {
+                            messages::accessDenied(asyncResp->res,
+                                                   crow::utility::urlFromPieces(
+                                                       "VirtualMedia.Insert"));
+                        }
+                        else
+                        {
+                            messages::internalError(asyncResp->res);
+                        }
+                    }
+                    else if (!success)
+                    {
+                        BMCWEB_LOG_ERROR << "Service responded with error ";
+                        messages::generalError(asyncResp->res);
+                    }
+                },
+                service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
+                "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", *timeout,
+                imageUrl, rw, unixFd);
         },
         service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
-        "xyz.openbmc_project.VirtualMedia.Legacy", "Mount", imageUrl, rw,
-        unixFd);
+        "org.freedesktop.DBus.Properties", "Get",
+        "xyz.openbmc_project.VirtualMedia.MountPoint", "Timeout");
 }
 
 /**
@@ -729,38 +787,49 @@ inline void doVmAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                        const std::string& service, const std::string& name,
                        bool legacy)
 {
+    const std::string vmMode = legacy ? "Legacy" : "Proxy";
+    const std::string objectPath =
+        "/xyz/openbmc_project/VirtualMedia/" + vmMode + "/" + name;
+    const std::string ifaceName = "xyz.openbmc_project.VirtualMedia." + vmMode;
 
-    // Legacy mount requires parameter with image
-    if (legacy)
-    {
-        crow::connections::systemBus->async_method_call(
-            [asyncResp](const boost::system::error_code ec) {
-                if (ec)
-                {
-                    BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
-
-                    messages::internalError(asyncResp->res);
-                    return;
-                }
-            },
-            service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
-            "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
-    }
-    else // proxy
-    {
-        crow::connections::systemBus->async_method_call(
-            [asyncResp](const boost::system::error_code ec) {
-                if (ec)
-                {
-                    BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
+    crow::connections::systemBus->async_method_call(
+        [asyncResp, service, name, objectPath,
+         ifaceName](const boost::system::error_code ec,
+                    const std::variant<int> timeoutProperty) {
+            if (ec)
+            {
+                BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
+                messages::internalError(asyncResp->res);
+                return;
+            }
 
-                    messages::internalError(asyncResp->res);
-                    return;
-                }
-            },
-            service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
-            "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
-    }
+            auto timeout = vmParseTimeoutProperty(timeoutProperty);
+            if (timeout == std::nullopt)
+            {
+                BMCWEB_LOG_ERROR << "Timeout property is empty.";
+                messages::internalError(asyncResp->res);
+                return;
+            }
+            crow::connections::systemBus->async_method_call_timed(
+                [asyncResp](const boost::system::error_code ec) {
+                    if (ec)
+                    {
+                        BMCWEB_LOG_ERROR << "Bad D-Bus request error: " << ec;
+                        if (ec == boost::system::errc::device_or_resource_busy)
+                        {
+                            messages::resourceInUse(asyncResp->res);
+                        }
+                        else
+                        {
+                            messages::internalError(asyncResp->res);
+                        }
+                        return;
+                    }
+                },
+                service, objectPath, ifaceName, "Unmount", *timeout);
+        },
+        service, objectPath, "org.freedesktop.DBus.Properties", "Get",
+        "xyz.openbmc_project.VirtualMedia.MountPoint", "Timeout");
 }
 
 struct InsertMediaActionParams
-- 
2.25.1