From af516f8875c47286a667cd7ff2e664bf59872ef1 Mon Sep 17 00:00:00 2001 From: Tom Marshall Date: Wed, 4 Nov 2015 15:44:44 -0800 Subject: [PATCH] vold: Support internal storage partitions * Support nonremovable disks and expose a nonremovable flag in the DiskCreated message. * New DiskPartition class to hold single partitions. DiskPartition is used when the fs_mgr entry has a partnum (eg. when fs_mgr_flags contains voldmanaged=label:#). Override disk partitioning methods to prevent destroying the emmc. Change-Id: Id7ec3ea409b5c96e691730604e4b1e9cc3aa9d33 vold: Correct base header paths These headers were moved to android-base Change-Id: I3eaa8316006b9017c5f5e31cd1e91efc2862106d DiskPartition.cpp: Add sysmacros.h dependency for major/minor Change-Id: I22c267c8f12b40fb3e2295becd88f12b75907b69 Signed-off-by: Adrian DC [mikeioannina] Adapt for Pie and Q Change-Id: Id7ec3ea409b5c96e691730604e4b1e9cc3aa9d33 --- Android.bp | 1 + VolumeManager.cpp | 8 +++- VolumeManager.h | 7 +++- main.cpp | 11 +++-- model/Disk.h | 18 +++++---- model/DiskPartition.cpp | 90 +++++++++++++++++++++++++++++++++++++++++ model/DiskPartition.h | 53 ++++++++++++++++++++++++ 7 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 model/DiskPartition.cpp create mode 100644 model/DiskPartition.h diff --git a/Android.bp b/Android.bp index 711b7a5..16082c7 100644 --- a/Android.bp +++ b/Android.bp @@ -143,6 +143,7 @@ cc_library_static { "fs/Ntfs.cpp", "fs/Vfat.cpp", "model/Disk.cpp", + "model/DiskPartition.cpp", "model/EmulatedVolume.cpp", "model/ObbVolume.cpp", "model/PrivateVolume.cpp", diff --git a/VolumeManager.cpp b/VolumeManager.cpp index 585d2d5..6b06703 100644 --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -240,8 +240,12 @@ void VolumeManager::handleBlockEvent(NetlinkEvent* evt) { flags |= android::vold::Disk::Flags::kUsb; } - auto disk = - new android::vold::Disk(eventPath, device, source->getNickname(), flags); + android::vold::Disk* disk = (source->getPartNum() == -1) ? + new android::vold::Disk(eventPath, device, + source->getNickname(), flags) : + new android::vold::DiskPartition(eventPath, device, + source->getNickname(), flags, + source->getPartNum()); handleDiskAdded(std::shared_ptr(disk)); break; } diff --git a/VolumeManager.h b/VolumeManager.h index 3277f75..03ca6b1 100644 --- a/VolumeManager.h +++ b/VolumeManager.h @@ -37,6 +37,7 @@ #include "android/os/IVoldListener.h" #include "model/Disk.h" +#include "model/DiskPartition.h" #include "model/VolumeBase.h" class VolumeManager { @@ -62,19 +63,21 @@ class VolumeManager { class DiskSource { public: - DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) - : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {} + DiskSource(const std::string& sysPattern, const std::string& nickname, int partnum, int flags) + : mSysPattern(sysPattern), mNickname(nickname), mPartNum(partnum), mFlags(flags) {} bool matches(const std::string& sysPath) { return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0); } const std::string& getNickname() const { return mNickname; } + int getPartNum() const { return mPartNum; } int getFlags() const { return mFlags; } private: std::string mSysPattern; std::string mNickname; + int mPartNum; int mFlags; }; diff --git a/main.cpp b/main.cpp index a6cb640..c9fc41b 100644 --- a/main.cpp +++ b/main.cpp @@ -242,13 +242,9 @@ static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quot } if (entry.fs_mgr_flags.vold_managed) { - if (entry.fs_mgr_flags.nonremovable) { - LOG(WARNING) << "nonremovable no longer supported; ignoring volume"; - continue; - } - std::string sysPattern(entry.blk_device); std::string nickname(entry.label); + int partnum = entry.partnum; int flags = 0; if (entry.is_encryptable()) { @@ -259,9 +255,12 @@ static int process_config(VolumeManager* vm, bool* has_adoptable, bool* has_quot android::base::GetBoolProperty("vold.debug.default_primary", false)) { flags |= android::vold::Disk::Flags::kDefaultPrimary; } + if (entry.fs_mgr_flags.nonremovable) { + flags |= android::vold::Disk::Flags::kNonRemovable; + } vm->addDiskSource(std::shared_ptr( - new VolumeManager::DiskSource(sysPattern, nickname, flags))); + new VolumeManager::DiskSource(sysPattern, nickname, partnum, flags))); } } return 0; diff --git a/model/Disk.h b/model/Disk.h index 7775832..b926ae2 100644 --- a/model/Disk.h +++ b/model/Disk.h @@ -56,6 +56,8 @@ class Disk { /* Flag that disk is Stub disk, i.e., disk that is managed from outside * Android (e.g., ARC++). */ kStub = 1 << 5, + /* Flag that disk is non-removable */ + kNonRemovable = 1 << 6, }; const std::string& getId() const { return mId; } @@ -73,20 +75,20 @@ class Disk { std::vector> getVolumes() const; - status_t create(); - status_t destroy(); + virtual status_t create(); + virtual status_t destroy(); - status_t readMetadata(); - status_t readPartitions(); + virtual status_t readMetadata(); + virtual status_t readPartitions(); void initializePartition(std::shared_ptr vol); status_t unmountAll(); - status_t partitionPublic(); - status_t partitionPrivate(); - status_t partitionMixed(int8_t ratio); + virtual status_t partitionPublic(); + virtual status_t partitionPrivate(); + virtual status_t partitionMixed(int8_t ratio); - private: + protected: /* ID that uniquely references this disk */ std::string mId; /* Original event path */ diff --git a/model/DiskPartition.cpp b/model/DiskPartition.cpp new file mode 100644 index 0000000..1cbcfbb --- /dev/null +++ b/model/DiskPartition.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2015 Cyanogen, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DiskPartition.h" +#include "PublicVolume.h" +#include "PrivateVolume.h" +#include "Utils.h" +#include "VolumeBase.h" +#include "VolumeManager.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using android::base::ReadFileToString; +using android::base::WriteStringToFile; +using android::base::StringPrintf; + +namespace android { +namespace vold { + +DiskPartition::DiskPartition(const std::string& eventPath, dev_t device, + const std::string& nickname, int flags, int partnum) : + Disk(eventPath, device, nickname, flags), + mPartNum(partnum) { + // Empty +} + +DiskPartition::~DiskPartition() { + // Empty +} + +status_t DiskPartition::create() { + CHECK(!mCreated); + mCreated = true; + auto listener = VolumeManager::Instance()->getListener(); + if (listener) listener->onDiskCreated(getId(), mFlags); + dev_t partDevice = makedev(major(mDevice), minor(mDevice) + mPartNum); + createPublicVolume(partDevice); + return OK; +} + +status_t DiskPartition::destroy() { + CHECK(mCreated); + destroyAllVolumes(); + mCreated = false; + auto listener = VolumeManager::Instance()->getListener(); + if (listener) listener->onDiskDestroyed(getId()); + return OK; +} + +status_t DiskPartition::partitionPublic() { + return -1; +} + +status_t DiskPartition::partitionPrivate() { + return -1; +} + +status_t DiskPartition::partitionMixed(int8_t ratio) { + return -1; +} + +} // namespace vold +} // namespace android + diff --git a/model/DiskPartition.h b/model/DiskPartition.h new file mode 100644 index 0000000..274d6be --- /dev/null +++ b/model/DiskPartition.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2015 Cyanogen, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_VOLD_DISKPARTITION_H +#define ANDROID_VOLD_DISKPARTITION_H + +#include "Disk.h" + +namespace android { +namespace vold { + +/* + * Representation of a single partition on physical media. Useful for + * single media partitions such as "internal" sdcard partitions. + */ + +class DiskPartition : public Disk { +public: + DiskPartition(const std::string& eventPath, dev_t device, + const std::string& nickname, + int flags, int partnum); + virtual ~DiskPartition(); + + virtual status_t create(); + virtual status_t destroy(); + + virtual status_t partitionPublic(); + virtual status_t partitionPrivate(); + virtual status_t partitionMixed(int8_t ratio); + +private: + /* Partition number */ + int mPartNum; +}; + +} // namespace vold +} // namespace android + +#endif +