diff options
author | Paul Lawrence <paullawrence@google.com> | 2023-08-11 10:27:24 -0700 |
---|---|---|
committer | Paul Lawrence <paullawrence@google.com> | 2023-08-14 23:51:39 +0000 |
commit | d73dfd4ab0ff4cbf704034750b2a25832da7fb0c (patch) | |
tree | 5c114669a8b11fa6337fb9cc795617d033cc9b17 /VolumeManager.cpp | |
parent | d54caf0598da089cb4fdbb226ba83cd00c1be51b (diff) | |
download | vold-d73dfd4ab0ff4cbf704034750b2a25832da7fb0c.tar.gz |
Add vdc volume getStorageSize
StorageStatsManager.getTotalBytes currently takes the size of /data and
rounds up to known probable sizes to guess the size of internal storage.
This is not always correct.
Instead, find the device /data is on and get the size of that device.
This should give a more accurate answer.
Bug: 295358118
Test: vdc volume getStorageSize returns storage size
Change-Id: I907892041b1ce2cd72092a9877ac34c12bf3f254
Diffstat (limited to 'VolumeManager.cpp')
-rw-r--r-- | VolumeManager.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/VolumeManager.cpp b/VolumeManager.cpp index e29b9206..dc6fae98 100644 --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -36,6 +36,7 @@ #include <linux/kdev_t.h> #include <ApexProperties.sysprop.h> +#include <android-base/file.h> #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/properties.h> @@ -53,6 +54,7 @@ #include <private/android_filesystem_config.h> #include <fscrypt/fscrypt.h> +#include <libdm/dm.h> #include "AppFuseUtil.h" #include "FsCrypt.h" @@ -1196,3 +1198,68 @@ int VolumeManager::unmountAppFuse(uid_t uid, int mountId) { int VolumeManager::openAppFuseFile(uid_t uid, int mountId, int fileId, int flags) { return android::vold::OpenAppFuseFile(uid, mountId, fileId, flags); } + +android::status_t android::vold::GetStorageSize(int64_t* storageSize) { + // Start with the /data mount point from fs_mgr + auto entry = android::fs_mgr::GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT); + if (entry == nullptr) { + LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT; + return EINVAL; + } + + // Follow any symbolic links + std::string blkDevice = entry->blk_device; + std::string dataDevice; + if (!android::base::Realpath(blkDevice, &dataDevice)) { + dataDevice = blkDevice; + } + + // Handle mapped volumes. + auto& dm = android::dm::DeviceMapper::Instance(); + for (;;) { + auto parent = dm.GetParentBlockDeviceByPath(dataDevice); + if (!parent.has_value()) break; + dataDevice = *parent; + } + + // Get the potential /sys/block entry + std::size_t leaf = dataDevice.rfind('/'); + if (leaf == std::string::npos) { + LOG(ERROR) << "data device " << dataDevice << " is not a path"; + return EINVAL; + } + if (dataDevice.substr(0, leaf) != "/dev/block") { + LOG(ERROR) << "data device " << dataDevice << " is not a block device"; + return EINVAL; + } + std::string sysfs = std::string() + "/sys/block/" + dataDevice.substr(leaf + 1); + + // Look for a directory in /sys/block containing size where the name is a shortened + // version of the name we now have + // Typically we start with something like /sys/block/sda2, and we want /sys/block/sda + // Note that this directory only contains actual disks, not partitions, so this is + // not going to find anything other than the disks + std::string size; + std::string sizeFile; + for (std::string sysfsDir = sysfs;; sysfsDir = sysfsDir.substr(0, sysfsDir.size() - 1)) { + if (sysfsDir.back() == '/') { + LOG(ERROR) << "Could not find valid block device from " << sysfs; + return EINVAL; + } + sizeFile = sysfsDir + "/size"; + if (android::base::ReadFileToString(sizeFile, &size, true)) { + break; + } + } + + // Read the size file and be done + std::stringstream ssSize(size); + ssSize >> *storageSize; + if (ssSize.fail()) { + LOG(ERROR) << sizeFile << " cannot be read as an integer"; + return EINVAL; + } + + *storageSize *= 512; + return OK; +}
\ No newline at end of file |