blob: 8f94af3f83204dab19b86e5929b45995f18890d7 [file] [log] [blame]
[email protected]2321d282012-01-31 23:06:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4ae73292011-11-15 05:20:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4cbead42012-08-26 12:02:395#include "chromeos/disks/disk_mount_manager.h"
[email protected]a66a23cb2012-06-19 23:15:336
avi6e1a22d2015-12-21 03:43:207#include <stddef.h>
8#include <stdint.h>
9
[email protected]4ae73292011-11-15 05:20:1810#include <set>
11
[email protected]4ae73292011-11-15 05:20:1812#include "base/bind.h"
avi6e1a22d2015-12-21 03:43:2013#include "base/macros.h"
avi8194ad62016-09-20 16:58:3614#include "base/memory/ptr_util.h"
[email protected]c6944272012-01-06 22:12:2815#include "base/memory/weak_ptr.h"
[email protected]3fc40c142011-12-01 13:09:0416#include "base/observer_list.h"
[email protected]afa339d72013-06-11 06:32:5117#include "base/strings/string_util.h"
[email protected]64e199252012-04-06 01:54:3618#include "chromeos/dbus/dbus_thread_manager.h"
hirono9f5eae542015-06-22 04:28:4119#include "chromeos/disks/suspend_unmount_manager.h"
[email protected]4ae73292011-11-15 05:20:1820
21namespace chromeos {
22namespace disks {
23
24namespace {
25
26const char kDeviceNotFound[] = "Device could not be found";
27
28DiskMountManager* g_disk_mount_manager = NULL;
29
30// The DiskMountManager implementation.
31class DiskMountManagerImpl : public DiskMountManager {
32 public:
[email protected]a2e4ee22014-07-11 05:16:3533 DiskMountManagerImpl() :
34 already_refreshed_(false),
35 weak_ptr_factory_(this) {
[email protected]4ae73292011-11-15 05:20:1836 DBusThreadManager* dbus_thread_manager = DBusThreadManager::Get();
[email protected]4ae73292011-11-15 05:20:1837 cros_disks_client_ = dbus_thread_manager->GetCrosDisksClient();
hirono9f5eae542015-06-22 04:28:4138 PowerManagerClient* power_manager_client =
39 dbus_thread_manager->GetPowerManagerClient();
40 suspend_unmount_manager_.reset(
41 new SuspendUnmountManager(this, power_manager_client));
[email protected]a0278d52014-05-06 03:36:1542 cros_disks_client_->SetMountEventHandler(
[email protected]4ae73292011-11-15 05:20:1843 base::Bind(&DiskMountManagerImpl::OnMountEvent,
[email protected]a0278d52014-05-06 03:36:1544 weak_ptr_factory_.GetWeakPtr()));
45 cros_disks_client_->SetMountCompletedHandler(
[email protected]4ae73292011-11-15 05:20:1846 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
47 weak_ptr_factory_.GetWeakPtr()));
[email protected]a0278d52014-05-06 03:36:1548 cros_disks_client_->SetFormatCompletedHandler(
49 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
50 weak_ptr_factory_.GetWeakPtr()));
[email protected]4ae73292011-11-15 05:20:1851 }
52
dchengae98daa2015-01-21 20:30:4953 ~DiskMountManagerImpl() override {
[email protected]4ae73292011-11-15 05:20:1854 }
55
56 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4957 void AddObserver(Observer* observer) override {
[email protected]4ae73292011-11-15 05:20:1858 observers_.AddObserver(observer);
59 }
60
61 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4962 void RemoveObserver(Observer* observer) override {
[email protected]4ae73292011-11-15 05:20:1863 observers_.RemoveObserver(observer);
64 }
65
66 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4967 void MountPath(const std::string& source_path,
68 const std::string& source_format,
69 const std::string& mount_label,
yamaguchicc8186b2016-08-08 06:38:5470 MountType type,
71 MountAccessMode access_mode) override {
[email protected]4ae73292011-11-15 05:20:1872 // Hidden and non-existent devices should not be mounted.
73 if (type == MOUNT_TYPE_DEVICE) {
74 DiskMap::const_iterator it = disks_.find(source_path);
75 if (it == disks_.end() || it->second->is_hidden()) {
[email protected]d7760592014-05-16 07:57:5276 OnMountCompleted(MountEntry(MOUNT_ERROR_INTERNAL, source_path, type,
77 ""));
[email protected]4ae73292011-11-15 05:20:1878 return;
79 }
80 }
[email protected]4ae73292011-11-15 05:20:1881 cros_disks_client_->Mount(
yamaguchicc8186b2016-08-08 06:38:5482 source_path, source_format, mount_label, access_mode,
[email protected]4ae73292011-11-15 05:20:1883 // When succeeds, OnMountCompleted will be called by
84 // "MountCompleted" signal instead.
[email protected]c6944272012-01-06 22:12:2885 base::Bind(&base::DoNothing),
[email protected]4ae73292011-11-15 05:20:1886 base::Bind(&DiskMountManagerImpl::OnMountCompleted,
87 weak_ptr_factory_.GetWeakPtr(),
[email protected]d7760592014-05-16 07:57:5288 MountEntry(MOUNT_ERROR_INTERNAL, source_path, type, "")));
yamaguchi6594bf7e12016-08-24 22:16:1189
90 // Record the access mode option passed to CrosDisks.
91 // This is needed because CrosDisks service methods doesn't return the info
92 // via DBus.
93 access_modes_.insert(std::make_pair(source_path, access_mode));
[email protected]4ae73292011-11-15 05:20:1894 }
95
96 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:4997 void UnmountPath(const std::string& mount_path,
98 UnmountOptions options,
99 const UnmountPathCallback& callback) override {
[email protected]3af8e1b2012-09-15 20:46:05100 UnmountChildMounts(mount_path);
[email protected]10795ae2012-10-10 07:33:49101 cros_disks_client_->Unmount(mount_path, options,
[email protected]4ae73292011-11-15 05:20:18102 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
[email protected]10795ae2012-10-10 07:33:49103 weak_ptr_factory_.GetWeakPtr(),
[email protected]8f919ee2013-03-14 19:53:29104 callback,
[email protected]ffdcc7a9c2013-07-02 06:59:39105 true,
106 mount_path),
[email protected]10795ae2012-10-10 07:33:49107 base::Bind(&DiskMountManagerImpl::OnUnmountPath,
108 weak_ptr_factory_.GetWeakPtr(),
[email protected]8f919ee2013-03-14 19:53:29109 callback,
[email protected]ffdcc7a9c2013-07-02 06:59:39110 false,
111 mount_path));
[email protected]4ae73292011-11-15 05:20:18112 }
113
114 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49115 void FormatMountedDevice(const std::string& mount_path) override {
[email protected]e3c1fc92012-11-15 00:56:46116 MountPointMap::const_iterator mount_point = mount_points_.find(mount_path);
117 if (mount_point == mount_points_.end()) {
118 LOG(ERROR) << "Mount point with path \"" << mount_path << "\" not found.";
[email protected]f026c0f2014-05-06 21:52:35119 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, mount_path);
[email protected]4ae73292011-11-15 05:20:18120 return;
121 }
[email protected]e3c1fc92012-11-15 00:56:46122
123 std::string device_path = mount_point->second.source_path;
124 DiskMap::const_iterator disk = disks_.find(device_path);
125 if (disk == disks_.end()) {
126 LOG(ERROR) << "Device with path \"" << device_path << "\" not found.";
[email protected]f026c0f2014-05-06 21:52:35127 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
[email protected]e3c1fc92012-11-15 00:56:46128 return;
129 }
yamaguchibffc32eb2016-08-17 06:35:39130 if (disk->second->is_read_only()) {
131 LOG(ERROR) << "Mount point with path \"" << mount_path
132 << "\" is read-only.";
133 OnFormatCompleted(FORMAT_ERROR_DEVICE_NOT_ALLOWED, mount_path);
134 return;
135 }
[email protected]e3c1fc92012-11-15 00:56:46136
[email protected]8f919ee2013-03-14 19:53:29137 UnmountPath(disk->second->mount_path(),
138 UNMOUNT_OPTIONS_NONE,
139 base::Bind(&DiskMountManagerImpl::OnUnmountPathForFormat,
140 weak_ptr_factory_.GetWeakPtr(),
141 device_path));
[email protected]4ae73292011-11-15 05:20:18142 }
143
144 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49145 void UnmountDeviceRecursively(
[email protected]4ae73292011-11-15 05:20:18146 const std::string& device_path,
mostynb4f4cf142014-10-06 13:57:52147 const UnmountDeviceRecursivelyCallbackType& callback) override {
[email protected]4ae73292011-11-15 05:20:18148 std::vector<std::string> devices_to_unmount;
149
150 // Get list of all devices to unmount.
151 int device_path_len = device_path.length();
152 for (DiskMap::iterator it = disks_.begin(); it != disks_.end(); ++it) {
153 if (!it->second->mount_path().empty() &&
154 strncmp(device_path.c_str(), it->second->device_path().c_str(),
155 device_path_len) == 0) {
156 devices_to_unmount.push_back(it->second->mount_path());
157 }
158 }
[email protected]e008d4fa2013-02-21 06:38:01159
[email protected]4ae73292011-11-15 05:20:18160 // We should detect at least original device.
161 if (devices_to_unmount.empty()) {
162 if (disks_.find(device_path) == disks_.end()) {
[email protected]e008d4fa2013-02-21 06:38:01163 LOG(WARNING) << "Unmount recursive request failed for device "
164 << device_path << ", with error: " << kDeviceNotFound;
165 callback.Run(false);
[email protected]4ae73292011-11-15 05:20:18166 return;
167 }
[email protected]e008d4fa2013-02-21 06:38:01168
169 // Nothing to unmount.
170 callback.Run(true);
171 return;
[email protected]4ae73292011-11-15 05:20:18172 }
[email protected]e008d4fa2013-02-21 06:38:01173
174 // We will send the same callback data object to all Unmount calls and use
[email protected]a2e4ee22014-07-11 05:16:35175 // it to synchronize callbacks.
[email protected]e008d4fa2013-02-21 06:38:01176 // Note: this implementation has a potential memory leak issue. For
177 // example if this instance is destructed before all the callbacks for
178 // Unmount are invoked, the memory pointed by |cb_data| will be leaked.
179 // It is because the UnmountDeviceRecursivelyCallbackData keeps how
180 // many times OnUnmountDeviceRecursively callback is called and when
181 // all the callbacks are called, |cb_data| will be deleted in the method.
182 // However destructing the instance before all callback invocations will
183 // cancel all pending callbacks, so that the |cb_data| would never be
184 // deleted.
185 // Fortunately, in the real scenario, the instance will be destructed
186 // only for ShutDown. So, probably the memory would rarely be leaked.
187 // TODO(hidehiko): Fix the issue.
188 UnmountDeviceRecursivelyCallbackData* cb_data =
189 new UnmountDeviceRecursivelyCallbackData(
190 callback, devices_to_unmount.size());
191 for (size_t i = 0; i < devices_to_unmount.size(); ++i) {
192 cros_disks_client_->Unmount(
193 devices_to_unmount[i],
194 UNMOUNT_OPTIONS_NONE,
195 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
[email protected]ffdcc7a9c2013-07-02 06:59:39196 weak_ptr_factory_.GetWeakPtr(),
197 cb_data,
198 true,
199 devices_to_unmount[i]),
[email protected]e008d4fa2013-02-21 06:38:01200 base::Bind(&DiskMountManagerImpl::OnUnmountDeviceRecursively,
[email protected]ffdcc7a9c2013-07-02 06:59:39201 weak_ptr_factory_.GetWeakPtr(),
202 cb_data,
203 false,
204 devices_to_unmount[i]));
[email protected]4ae73292011-11-15 05:20:18205 }
206 }
207
208 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49209 void EnsureMountInfoRefreshed(
hironoa4b675d2015-07-29 01:13:37210 const EnsureMountInfoRefreshedCallback& callback,
211 bool force) override {
212 if (!force && already_refreshed_) {
[email protected]a2e4ee22014-07-11 05:16:35213 callback.Run(true);
214 return;
215 }
216
217 refresh_callbacks_.push_back(callback);
218 if (refresh_callbacks_.size() == 1) {
219 // If there's no in-flight refreshing task, start it.
220 cros_disks_client_->EnumerateAutoMountableDevices(
221 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateDevices,
222 weak_ptr_factory_.GetWeakPtr()),
223 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
224 weak_ptr_factory_.GetWeakPtr(), false));
225 }
[email protected]4ae73292011-11-15 05:20:18226 }
227
228 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49229 const DiskMap& disks() const override { return disks_; }
[email protected]4ae73292011-11-15 05:20:18230
[email protected]bcfa0072012-08-07 01:08:57231 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49232 const Disk* FindDiskBySourcePath(
233 const std::string& source_path) const override {
[email protected]bcfa0072012-08-07 01:08:57234 DiskMap::const_iterator disk_it = disks_.find(source_path);
avi8194ad62016-09-20 16:58:36235 return disk_it == disks_.end() ? NULL : disk_it->second.get();
[email protected]bcfa0072012-08-07 01:08:57236 }
[email protected]4ae73292011-11-15 05:20:18237
238 // DiskMountManager override.
dchengae98daa2015-01-21 20:30:49239 const MountPointMap& mount_points() const override { return mount_points_; }
[email protected]4ae73292011-11-15 05:20:18240
[email protected]e3c1fc92012-11-15 00:56:46241 // DiskMountManager override.
avi8194ad62016-09-20 16:58:36242 bool AddDiskForTest(std::unique_ptr<Disk> disk) override {
[email protected]e3c1fc92012-11-15 00:56:46243 if (disks_.find(disk->device_path()) != disks_.end()) {
244 LOG(ERROR) << "Attempt to add a duplicate disk";
245 return false;
246 }
247
avi8194ad62016-09-20 16:58:36248 disks_.insert(std::make_pair(disk->device_path(), std::move(disk)));
[email protected]e3c1fc92012-11-15 00:56:46249 return true;
250 }
251
252 // DiskMountManager override.
253 // Corresponding disk should be added to the manager before this is called.
dchengae98daa2015-01-21 20:30:49254 bool AddMountPointForTest(const MountPointInfo& mount_point) override {
[email protected]e3c1fc92012-11-15 00:56:46255 if (mount_points_.find(mount_point.mount_path) != mount_points_.end()) {
256 LOG(ERROR) << "Attempt to add a duplicate mount point";
257 return false;
258 }
259 if (mount_point.mount_type == chromeos::MOUNT_TYPE_DEVICE &&
260 disks_.find(mount_point.source_path) == disks_.end()) {
261 LOG(ERROR) << "Device mount points must have a disk entry.";
262 return false;
263 }
264
265 mount_points_.insert(std::make_pair(mount_point.mount_path, mount_point));
266 return true;
267 }
268
[email protected]4ae73292011-11-15 05:20:18269 private:
[email protected]e008d4fa2013-02-21 06:38:01270 struct UnmountDeviceRecursivelyCallbackData {
271 UnmountDeviceRecursivelyCallbackData(
272 const UnmountDeviceRecursivelyCallbackType& in_callback,
273 int in_num_pending_callbacks)
274 : callback(in_callback),
275 num_pending_callbacks(in_num_pending_callbacks) {
[email protected]4ae73292011-11-15 05:20:18276 }
[email protected]e008d4fa2013-02-21 06:38:01277
278 const UnmountDeviceRecursivelyCallbackType callback;
279 size_t num_pending_callbacks;
[email protected]4ae73292011-11-15 05:20:18280 };
281
[email protected]3af8e1b2012-09-15 20:46:05282 // Unmounts all mount points whose source path is transitively parented by
283 // |mount_path|.
284 void UnmountChildMounts(const std::string& mount_path_in) {
285 std::string mount_path = mount_path_in;
286 // Let's make sure mount path has trailing slash.
pkasting9022cb42016-02-05 00:08:56287 if (mount_path.back() != '/')
[email protected]3af8e1b2012-09-15 20:46:05288 mount_path += '/';
289
290 for (MountPointMap::iterator it = mount_points_.begin();
291 it != mount_points_.end();
292 ++it) {
brettw95509312015-07-16 23:57:33293 if (base::StartsWith(it->second.source_path, mount_path,
294 base::CompareCase::SENSITIVE)) {
[email protected]8f919ee2013-03-14 19:53:29295 // TODO(tbarzic): Handle the case where this fails.
296 UnmountPath(it->second.mount_path,
297 UNMOUNT_OPTIONS_NONE,
298 UnmountPathCallback());
[email protected]3af8e1b2012-09-15 20:46:05299 }
300 }
301 }
302
[email protected]e008d4fa2013-02-21 06:38:01303 // Callback for UnmountDeviceRecursively.
304 void OnUnmountDeviceRecursively(
305 UnmountDeviceRecursivelyCallbackData* cb_data,
306 bool success,
307 const std::string& mount_path) {
[email protected]4ae73292011-11-15 05:20:18308 if (success) {
309 // Do standard processing for Unmount event.
[email protected]8f919ee2013-03-14 19:53:29310 OnUnmountPath(UnmountPathCallback(), true, mount_path);
[email protected]a0278d52014-05-06 03:36:15311 VLOG(1) << mount_path << " unmounted.";
[email protected]4ae73292011-11-15 05:20:18312 }
313 // This is safe as long as all callbacks are called on the same thread as
[email protected]e008d4fa2013-02-21 06:38:01314 // UnmountDeviceRecursively.
315 cb_data->num_pending_callbacks--;
[email protected]4ae73292011-11-15 05:20:18316
[email protected]e008d4fa2013-02-21 06:38:01317 if (cb_data->num_pending_callbacks == 0) {
318 // This code has a problem that the |success| status used here is for the
319 // last "unmount" callback, but not whether all unmounting is succeeded.
320 // TODO(hidehiko): Fix the issue.
321 cb_data->callback.Run(success);
[email protected]4ae73292011-11-15 05:20:18322 delete cb_data;
323 }
324 }
325
326 // Callback to handle MountCompleted signal and Mount method call failure.
[email protected]d7760592014-05-16 07:57:52327 void OnMountCompleted(const MountEntry& entry) {
[email protected]4ae73292011-11-15 05:20:18328 MountCondition mount_condition = MOUNT_CONDITION_NONE;
[email protected]d7760592014-05-16 07:57:52329 if (entry.mount_type() == MOUNT_TYPE_DEVICE) {
330 if (entry.error_code() == MOUNT_ERROR_UNKNOWN_FILESYSTEM) {
[email protected]4ae73292011-11-15 05:20:18331 mount_condition = MOUNT_CONDITION_UNKNOWN_FILESYSTEM;
[email protected]a66a23cb2012-06-19 23:15:33332 }
[email protected]d7760592014-05-16 07:57:52333 if (entry.error_code() == MOUNT_ERROR_UNSUPPORTED_FILESYSTEM) {
[email protected]4ae73292011-11-15 05:20:18334 mount_condition = MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM;
[email protected]a66a23cb2012-06-19 23:15:33335 }
[email protected]4ae73292011-11-15 05:20:18336 }
[email protected]d7760592014-05-16 07:57:52337 const MountPointInfo mount_info(entry.source_path(),
338 entry.mount_path(),
339 entry.mount_type(),
[email protected]4ae73292011-11-15 05:20:18340 mount_condition);
341
[email protected]4ae73292011-11-15 05:20:18342 // If the device is corrupted but it's still possible to format it, it will
343 // be fake mounted.
[email protected]d7760592014-05-16 07:57:52344 if ((entry.error_code() == MOUNT_ERROR_NONE ||
345 mount_info.mount_condition) &&
[email protected]4ae73292011-11-15 05:20:18346 mount_points_.find(mount_info.mount_path) == mount_points_.end()) {
347 mount_points_.insert(MountPointMap::value_type(mount_info.mount_path,
348 mount_info));
349 }
yamaguchi6594bf7e12016-08-24 22:16:11350
[email protected]d7760592014-05-16 07:57:52351 if ((entry.error_code() == MOUNT_ERROR_NONE ||
352 mount_info.mount_condition) &&
[email protected]4ae73292011-11-15 05:20:18353 mount_info.mount_type == MOUNT_TYPE_DEVICE &&
354 !mount_info.source_path.empty() &&
355 !mount_info.mount_path.empty()) {
356 DiskMap::iterator iter = disks_.find(mount_info.source_path);
yamaguchi6594bf7e12016-08-24 22:16:11357 if (iter != disks_.end()) { // disk might have been removed by now?
avi8194ad62016-09-20 16:58:36358 Disk* disk = iter->second.get();
yamaguchi6594bf7e12016-08-24 22:16:11359 DCHECK(disk);
360 // The is_read_only field in *disk may be incorrect when this is called
361 // from CrosDisksClientImpl::OnMountCompleted.
yamaguchi21448d5b2016-09-06 02:04:56362 // The disk should be treated as read-only when:
363 // - the read-only option was passed when issuing mount command
364 // - or the device hardware is read-only.
yamaguchi6594bf7e12016-08-24 22:16:11365 // |source_path| should be same as |disk->device_path| because
366 // |VolumeManager::OnDiskEvent()| passes the latter to cros-disks as a
367 // source path when mounting a device.
368 AccessModeMap::iterator it = access_modes_.find(entry.source_path());
yamaguchi21448d5b2016-09-06 02:04:56369 if (it != access_modes_.end() &&
370 it->second == chromeos::MOUNT_ACCESS_MODE_READ_ONLY) {
371 disk->set_read_only(true);
yamaguchi6594bf7e12016-08-24 22:16:11372 }
373 disk->set_mount_path(mount_info.mount_path);
[email protected]4ae73292011-11-15 05:20:18374 }
[email protected]4ae73292011-11-15 05:20:18375 }
yamaguchi6594bf7e12016-08-24 22:16:11376 // Observers may read the values of disks_. So notify them after tweaking
377 // values of disks_.
378 NotifyMountStatusUpdate(MOUNTING, entry.error_code(), mount_info);
[email protected]4ae73292011-11-15 05:20:18379 }
380
381 // Callback for UnmountPath.
[email protected]8f919ee2013-03-14 19:53:29382 void OnUnmountPath(const UnmountPathCallback& callback,
383 bool success,
384 const std::string& mount_path) {
[email protected]4ae73292011-11-15 05:20:18385 MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
[email protected]8f919ee2013-03-14 19:53:29386 if (mount_points_it == mount_points_.end()) {
387 // The path was unmounted, but not as a result of this unmount request,
388 // so return error.
389 if (!callback.is_null())
390 callback.Run(MOUNT_ERROR_INTERNAL);
[email protected]4ae73292011-11-15 05:20:18391 return;
[email protected]8f919ee2013-03-14 19:53:29392 }
[email protected]e3c1fc92012-11-15 00:56:46393
394 NotifyMountStatusUpdate(
[email protected]10795ae2012-10-10 07:33:49395 UNMOUNTING,
396 success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
[email protected]a66a23cb2012-06-19 23:15:33397 MountPointInfo(mount_points_it->second.source_path,
398 mount_points_it->second.mount_path,
399 mount_points_it->second.mount_type,
400 mount_points_it->second.mount_condition));
[email protected]10795ae2012-10-10 07:33:49401
[email protected]4ae73292011-11-15 05:20:18402 std::string path(mount_points_it->second.source_path);
[email protected]10795ae2012-10-10 07:33:49403 if (success)
404 mount_points_.erase(mount_points_it);
405
[email protected]e3c1fc92012-11-15 00:56:46406 DiskMap::iterator disk_iter = disks_.find(path);
407 if (disk_iter != disks_.end()) {
408 DCHECK(disk_iter->second);
409 if (success)
410 disk_iter->second->clear_mount_path();
[email protected]4ae73292011-11-15 05:20:18411 }
[email protected]10795ae2012-10-10 07:33:49412
[email protected]8f919ee2013-03-14 19:53:29413 if (!callback.is_null())
414 callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL);
415 }
416
417 void OnUnmountPathForFormat(const std::string& device_path,
418 MountError error_code) {
419 if (error_code == MOUNT_ERROR_NONE &&
420 disks_.find(device_path) != disks_.end()) {
421 FormatUnmountedDevice(device_path);
422 } else {
[email protected]f026c0f2014-05-06 21:52:35423 OnFormatCompleted(FORMAT_ERROR_UNKNOWN, device_path);
[email protected]4ae73292011-11-15 05:20:18424 }
425 }
426
[email protected]e3c1fc92012-11-15 00:56:46427 // Starts device formatting.
428 void FormatUnmountedDevice(const std::string& device_path) {
429 DiskMap::const_iterator disk = disks_.find(device_path);
430 DCHECK(disk != disks_.end() && disk->second->mount_path().empty());
431
432 const char kFormatVFAT[] = "vfat";
[email protected]f026c0f2014-05-06 21:52:35433 cros_disks_client_->Format(
[email protected]e3c1fc92012-11-15 00:56:46434 device_path,
435 kFormatVFAT,
[email protected]f026c0f2014-05-06 21:52:35436 base::Bind(&DiskMountManagerImpl::OnFormatStarted,
[email protected]15a2c282013-07-03 08:39:49437 weak_ptr_factory_.GetWeakPtr(),
438 device_path),
[email protected]f026c0f2014-05-06 21:52:35439 base::Bind(&DiskMountManagerImpl::OnFormatCompleted,
[email protected]e3c1fc92012-11-15 00:56:46440 weak_ptr_factory_.GetWeakPtr(),
[email protected]f026c0f2014-05-06 21:52:35441 FORMAT_ERROR_UNKNOWN,
442 device_path));
[email protected]e3c1fc92012-11-15 00:56:46443 }
444
[email protected]f026c0f2014-05-06 21:52:35445 // Callback for Format.
446 void OnFormatStarted(const std::string& device_path) {
447 NotifyFormatStatusUpdate(FORMAT_STARTED, FORMAT_ERROR_NONE, device_path);
[email protected]4ae73292011-11-15 05:20:18448 }
449
[email protected]a0278d52014-05-06 03:36:15450 // Callback to handle FormatCompleted signal and Format method call failure.
451 void OnFormatCompleted(FormatError error_code,
[email protected]f026c0f2014-05-06 21:52:35452 const std::string& device_path) {
453 NotifyFormatStatusUpdate(FORMAT_COMPLETED, error_code, device_path);
[email protected]a0278d52014-05-06 03:36:15454 }
455
[email protected]a2e4ee22014-07-11 05:16:35456 // Callback for GetDeviceProperties.
[email protected]4ae73292011-11-15 05:20:18457 void OnGetDeviceProperties(const DiskInfo& disk_info) {
458 // TODO(zelidrag): Find a better way to filter these out before we
459 // fetch the properties:
460 // Ignore disks coming from the device we booted the system from.
461 if (disk_info.on_boot_device())
462 return;
463
464 LOG(WARNING) << "Found disk " << disk_info.device_path();
465 // Delete previous disk info for this path:
466 bool is_new = true;
467 DiskMap::iterator iter = disks_.find(disk_info.device_path());
468 if (iter != disks_.end()) {
[email protected]4ae73292011-11-15 05:20:18469 disks_.erase(iter);
470 is_new = false;
471 }
472 Disk* disk = new Disk(disk_info.device_path(),
473 disk_info.mount_path(),
474 disk_info.system_path(),
475 disk_info.file_path(),
476 disk_info.label(),
477 disk_info.drive_label(),
[email protected]202e9fee2012-09-13 20:21:29478 disk_info.vendor_id(),
479 disk_info.vendor_name(),
480 disk_info.product_id(),
481 disk_info.product_name(),
[email protected]9c5620d32012-07-31 01:00:38482 disk_info.uuid(),
[email protected]4ae73292011-11-15 05:20:18483 FindSystemPathPrefix(disk_info.system_path()),
484 disk_info.device_type(),
485 disk_info.total_size_in_bytes(),
486 disk_info.is_drive(),
487 disk_info.is_read_only(),
488 disk_info.has_media(),
489 disk_info.on_boot_device(),
[email protected]79ed457b2014-07-22 04:07:26490 disk_info.on_removable_device(),
[email protected]4ae73292011-11-15 05:20:18491 disk_info.is_hidden());
yamaguchi6594bf7e12016-08-24 22:16:11492 // If the device was mounted by the instance, apply recorded parameter.
493 // Lookup by |device_path| which we pass to cros-disks when mounting a
494 // device in |VolumeManager::OnDiskEvent()|.
495 AccessModeMap::iterator access_mode =
496 access_modes_.find(disk->device_path());
497 if (access_mode != access_modes_.end()) {
498 disk->set_read_only(access_mode->second ==
499 chromeos::MOUNT_ACCESS_MODE_READ_ONLY);
500 }
avi8194ad62016-09-20 16:58:36501 disks_.insert(
502 std::make_pair(disk_info.device_path(), base::WrapUnique(disk)));
[email protected]e3c1fc92012-11-15 00:56:46503 NotifyDiskStatusUpdate(is_new ? DISK_ADDED : DISK_CHANGED, disk);
[email protected]4ae73292011-11-15 05:20:18504 }
505
[email protected]a2e4ee22014-07-11 05:16:35506 // Part of EnsureMountInfoRefreshed(). Called after the list of devices are
507 // enumerated.
508 void RefreshAfterEnumerateDevices(const std::vector<std::string>& devices) {
509 std::set<std::string> current_device_set(devices.begin(), devices.end());
[email protected]4ae73292011-11-15 05:20:18510 for (DiskMap::iterator iter = disks_.begin(); iter != disks_.end(); ) {
511 if (current_device_set.find(iter->first) == current_device_set.end()) {
[email protected]4ae73292011-11-15 05:20:18512 disks_.erase(iter++);
513 } else {
514 ++iter;
515 }
516 }
[email protected]a2e4ee22014-07-11 05:16:35517 RefreshDeviceAtIndex(devices, 0);
518 }
519
520 // Part of EnsureMountInfoRefreshed(). Called for each device to refresh info.
521 void RefreshDeviceAtIndex(const std::vector<std::string>& devices,
522 size_t index) {
523 if (index == devices.size()) {
524 // All devices info retrieved. Proceed to enumerate mount point info.
525 cros_disks_client_->EnumerateMountEntries(
526 base::Bind(&DiskMountManagerImpl::RefreshAfterEnumerateMountEntries,
527 weak_ptr_factory_.GetWeakPtr()),
528 base::Bind(&DiskMountManagerImpl::RefreshCompleted,
529 weak_ptr_factory_.GetWeakPtr(), false));
530 return;
531 }
532
533 cros_disks_client_->GetDeviceProperties(
534 devices[index],
535 base::Bind(&DiskMountManagerImpl::RefreshAfterGetDeviceProperties,
536 weak_ptr_factory_.GetWeakPtr(), devices, index + 1),
hirono8067bf02015-08-10 03:12:25537 base::Bind(&DiskMountManagerImpl::RefreshDeviceAtIndex,
538 weak_ptr_factory_.GetWeakPtr(), devices, index + 1));
[email protected]a2e4ee22014-07-11 05:16:35539 }
540
541 // Part of EnsureMountInfoRefreshed().
542 void RefreshAfterGetDeviceProperties(const std::vector<std::string>& devices,
543 size_t next_index,
544 const DiskInfo& disk_info) {
545 OnGetDeviceProperties(disk_info);
546 RefreshDeviceAtIndex(devices, next_index);
547 }
548
549 // Part of EnsureMountInfoRefreshed(). Called after mount entries are listed.
550 void RefreshAfterEnumerateMountEntries(
551 const std::vector<MountEntry>& entries) {
552 for (size_t i = 0; i < entries.size(); ++i)
553 OnMountCompleted(entries[i]);
554 RefreshCompleted(true);
555 }
556
557 // Part of EnsureMountInfoRefreshed(). Called when the refreshing is done.
558 void RefreshCompleted(bool success) {
559 already_refreshed_ = true;
560 for (size_t i = 0; i < refresh_callbacks_.size(); ++i)
561 refresh_callbacks_[i].Run(success);
562 refresh_callbacks_.clear();
[email protected]4ae73292011-11-15 05:20:18563 }
564
565 // Callback to handle mount event signals.
[email protected]e24f8762011-12-20 00:10:04566 void OnMountEvent(MountEventType event, const std::string& device_path_arg) {
567 // Take a copy of the argument so we can modify it below.
568 std::string device_path = device_path_arg;
[email protected]4ae73292011-11-15 05:20:18569 switch (event) {
[email protected]e3c1fc92012-11-15 00:56:46570 case CROS_DISKS_DISK_ADDED: {
[email protected]4ae73292011-11-15 05:20:18571 cros_disks_client_->GetDeviceProperties(
572 device_path,
573 base::Bind(&DiskMountManagerImpl::OnGetDeviceProperties,
574 weak_ptr_factory_.GetWeakPtr()),
[email protected]c6944272012-01-06 22:12:28575 base::Bind(&base::DoNothing));
[email protected]e3c1fc92012-11-15 00:56:46576 break;
[email protected]4ae73292011-11-15 05:20:18577 }
[email protected]e3c1fc92012-11-15 00:56:46578 case CROS_DISKS_DISK_REMOVED: {
[email protected]4ae73292011-11-15 05:20:18579 // Search and remove disks that are no longer present.
580 DiskMountManager::DiskMap::iterator iter = disks_.find(device_path);
581 if (iter != disks_.end()) {
avi8194ad62016-09-20 16:58:36582 Disk* disk = iter->second.get();
[email protected]e3c1fc92012-11-15 00:56:46583 NotifyDiskStatusUpdate(DISK_REMOVED, disk);
[email protected]4ae73292011-11-15 05:20:18584 disks_.erase(iter);
585 }
[email protected]e3c1fc92012-11-15 00:56:46586 break;
[email protected]4ae73292011-11-15 05:20:18587 }
[email protected]e3c1fc92012-11-15 00:56:46588 case CROS_DISKS_DEVICE_ADDED: {
[email protected]4ae73292011-11-15 05:20:18589 system_path_prefixes_.insert(device_path);
[email protected]e3c1fc92012-11-15 00:56:46590 NotifyDeviceStatusUpdate(DEVICE_ADDED, device_path);
[email protected]4ae73292011-11-15 05:20:18591 break;
592 }
[email protected]e3c1fc92012-11-15 00:56:46593 case CROS_DISKS_DEVICE_REMOVED: {
[email protected]4ae73292011-11-15 05:20:18594 system_path_prefixes_.erase(device_path);
[email protected]e3c1fc92012-11-15 00:56:46595 NotifyDeviceStatusUpdate(DEVICE_REMOVED, device_path);
[email protected]4ae73292011-11-15 05:20:18596 break;
597 }
[email protected]e3c1fc92012-11-15 00:56:46598 case CROS_DISKS_DEVICE_SCANNED: {
599 NotifyDeviceStatusUpdate(DEVICE_SCANNED, device_path);
[email protected]4ae73292011-11-15 05:20:18600 break;
601 }
[email protected]4ae73292011-11-15 05:20:18602 default: {
603 LOG(ERROR) << "Unknown event: " << event;
[email protected]4ae73292011-11-15 05:20:18604 }
605 }
[email protected]4ae73292011-11-15 05:20:18606 }
607
608 // Notifies all observers about disk status update.
[email protected]e3c1fc92012-11-15 00:56:46609 void NotifyDiskStatusUpdate(DiskEvent event,
[email protected]4ae73292011-11-15 05:20:18610 const Disk* disk) {
[email protected]e3c1fc92012-11-15 00:56:46611 FOR_EACH_OBSERVER(Observer, observers_, OnDiskEvent(event, disk));
[email protected]4ae73292011-11-15 05:20:18612 }
613
614 // Notifies all observers about device status update.
[email protected]e3c1fc92012-11-15 00:56:46615 void NotifyDeviceStatusUpdate(DeviceEvent event,
[email protected]4ae73292011-11-15 05:20:18616 const std::string& device_path) {
[email protected]e3c1fc92012-11-15 00:56:46617 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceEvent(event, device_path));
[email protected]4ae73292011-11-15 05:20:18618 }
619
620 // Notifies all observers about mount completion.
[email protected]e3c1fc92012-11-15 00:56:46621 void NotifyMountStatusUpdate(MountEvent event,
622 MountError error_code,
623 const MountPointInfo& mount_info) {
[email protected]4ae73292011-11-15 05:20:18624 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]e3c1fc92012-11-15 00:56:46625 OnMountEvent(event, error_code, mount_info));
626 }
627
628 void NotifyFormatStatusUpdate(FormatEvent event,
629 FormatError error_code,
630 const std::string& device_path) {
631 FOR_EACH_OBSERVER(Observer, observers_,
632 OnFormatEvent(event, error_code, device_path));
[email protected]4ae73292011-11-15 05:20:18633 }
634
[email protected]4ae73292011-11-15 05:20:18635 // Finds system path prefix from |system_path|.
636 const std::string& FindSystemPathPrefix(const std::string& system_path) {
637 if (system_path.empty())
[email protected]8790210c2013-12-02 05:29:53638 return base::EmptyString();
[email protected]4ae73292011-11-15 05:20:18639 for (SystemPathPrefixSet::const_iterator it = system_path_prefixes_.begin();
640 it != system_path_prefixes_.end();
641 ++it) {
642 const std::string& prefix = *it;
brettw95509312015-07-16 23:57:33643 if (base::StartsWith(system_path, prefix, base::CompareCase::SENSITIVE))
[email protected]4ae73292011-11-15 05:20:18644 return prefix;
645 }
[email protected]8790210c2013-12-02 05:29:53646 return base::EmptyString();
[email protected]4ae73292011-11-15 05:20:18647 }
648
[email protected]4ae73292011-11-15 05:20:18649 // Mount event change observers.
brettw236d3172015-06-03 16:31:43650 base::ObserverList<Observer> observers_;
[email protected]4ae73292011-11-15 05:20:18651
652 CrosDisksClient* cros_disks_client_;
653
654 // The list of disks found.
655 DiskMountManager::DiskMap disks_;
656
657 DiskMountManager::MountPointMap mount_points_;
658
659 typedef std::set<std::string> SystemPathPrefixSet;
660 SystemPathPrefixSet system_path_prefixes_;
661
[email protected]a2e4ee22014-07-11 05:16:35662 bool already_refreshed_;
663 std::vector<EnsureMountInfoRefreshedCallback> refresh_callbacks_;
664
dcheng0a6e80c2016-04-08 18:37:38665 std::unique_ptr<SuspendUnmountManager> suspend_unmount_manager_;
hirono9f5eae542015-06-22 04:28:41666
yamaguchi6594bf7e12016-08-24 22:16:11667 // Whether the instance attempted to mount a device in read-only mode for
668 // each source path.
669 typedef std::map<std::string, chromeos::MountAccessMode> AccessModeMap;
670 AccessModeMap access_modes_;
671
[email protected]4ae73292011-11-15 05:20:18672 base::WeakPtrFactory<DiskMountManagerImpl> weak_ptr_factory_;
673
674 DISALLOW_COPY_AND_ASSIGN(DiskMountManagerImpl);
675};
676
[email protected]a66a23cb2012-06-19 23:15:33677} // namespace
[email protected]4ae73292011-11-15 05:20:18678
679DiskMountManager::Disk::Disk(const std::string& device_path,
680 const std::string& mount_path,
681 const std::string& system_path,
682 const std::string& file_path,
683 const std::string& device_label,
684 const std::string& drive_label,
[email protected]202e9fee2012-09-13 20:21:29685 const std::string& vendor_id,
686 const std::string& vendor_name,
687 const std::string& product_id,
688 const std::string& product_name,
[email protected]9c5620d32012-07-31 01:00:38689 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:18690 const std::string& system_path_prefix,
691 DeviceType device_type,
avi6e1a22d2015-12-21 03:43:20692 uint64_t total_size_in_bytes,
[email protected]4ae73292011-11-15 05:20:18693 bool is_parent,
694 bool is_read_only,
695 bool has_media,
696 bool on_boot_device,
[email protected]79ed457b2014-07-22 04:07:26697 bool on_removable_device,
[email protected]4ae73292011-11-15 05:20:18698 bool is_hidden)
699 : device_path_(device_path),
700 mount_path_(mount_path),
701 system_path_(system_path),
702 file_path_(file_path),
703 device_label_(device_label),
704 drive_label_(drive_label),
[email protected]202e9fee2012-09-13 20:21:29705 vendor_id_(vendor_id),
706 vendor_name_(vendor_name),
707 product_id_(product_id),
708 product_name_(product_name),
[email protected]9c5620d32012-07-31 01:00:38709 fs_uuid_(fs_uuid),
[email protected]4ae73292011-11-15 05:20:18710 system_path_prefix_(system_path_prefix),
711 device_type_(device_type),
712 total_size_in_bytes_(total_size_in_bytes),
713 is_parent_(is_parent),
714 is_read_only_(is_read_only),
715 has_media_(has_media),
716 on_boot_device_(on_boot_device),
[email protected]79ed457b2014-07-22 04:07:26717 on_removable_device_(on_removable_device),
avi6e1a22d2015-12-21 03:43:20718 is_hidden_(is_hidden) {}
[email protected]4ae73292011-11-15 05:20:18719
vmpstr2a0c10402016-04-08 21:28:07720DiskMountManager::Disk::Disk(const Disk& other) = default;
721
[email protected]4ae73292011-11-15 05:20:18722DiskMountManager::Disk::~Disk() {}
723
avi8194ad62016-09-20 16:58:36724bool DiskMountManager::AddDiskForTest(std::unique_ptr<Disk> disk) {
[email protected]e3c1fc92012-11-15 00:56:46725 return false;
726}
727
728bool DiskMountManager::AddMountPointForTest(const MountPointInfo& mount_point) {
729 return false;
730}
731
[email protected]4ae73292011-11-15 05:20:18732// static
[email protected]4ae73292011-11-15 05:20:18733std::string DiskMountManager::MountConditionToString(MountCondition condition) {
734 switch (condition) {
735 case MOUNT_CONDITION_NONE:
736 return "";
737 case MOUNT_CONDITION_UNKNOWN_FILESYSTEM:
738 return "unknown_filesystem";
739 case MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM:
740 return "unsupported_filesystem";
741 default:
742 NOTREACHED();
743 }
744 return "";
745}
746
747// static
[email protected]2321d282012-01-31 23:06:59748std::string DiskMountManager::DeviceTypeToString(DeviceType type) {
749 switch (type) {
750 case DEVICE_TYPE_USB:
751 return "usb";
752 case DEVICE_TYPE_SD:
753 return "sd";
754 case DEVICE_TYPE_OPTICAL_DISC:
755 return "optical";
756 case DEVICE_TYPE_MOBILE:
757 return "mobile";
758 default:
759 return "unknown";
760 }
761}
762
763// static
[email protected]4ae73292011-11-15 05:20:18764void DiskMountManager::Initialize() {
[email protected]b307bceb2011-11-17 07:49:55765 if (g_disk_mount_manager) {
766 LOG(WARNING) << "DiskMountManager was already initialized";
767 return;
768 }
[email protected]4ae73292011-11-15 05:20:18769 g_disk_mount_manager = new DiskMountManagerImpl();
[email protected]b307bceb2011-11-17 07:49:55770 VLOG(1) << "DiskMountManager initialized";
771}
772
773// static
774void DiskMountManager::InitializeForTesting(
775 DiskMountManager* disk_mount_manager) {
776 if (g_disk_mount_manager) {
777 LOG(WARNING) << "DiskMountManager was already initialized";
778 return;
779 }
780 g_disk_mount_manager = disk_mount_manager;
781 VLOG(1) << "DiskMountManager initialized";
[email protected]4ae73292011-11-15 05:20:18782}
783
784// static
785void DiskMountManager::Shutdown() {
[email protected]b307bceb2011-11-17 07:49:55786 if (!g_disk_mount_manager) {
787 LOG(WARNING) << "DiskMountManager::Shutdown() called with NULL manager";
788 return;
[email protected]4ae73292011-11-15 05:20:18789 }
[email protected]b307bceb2011-11-17 07:49:55790 delete g_disk_mount_manager;
791 g_disk_mount_manager = NULL;
792 VLOG(1) << "DiskMountManager Shutdown completed";
[email protected]4ae73292011-11-15 05:20:18793}
794
795// static
796DiskMountManager* DiskMountManager::GetInstance() {
797 return g_disk_mount_manager;
798}
799
800} // namespace disks
801} // namespace chromeos