FileDeviceUtils.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "FileDeviceUtils.h"
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/fs.h>
  21. #include <mntent.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <android-base/file.h>
  27. #include <android-base/logging.h>
  28. #include <android-base/unique_fd.h>
  29. namespace {
  30. std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
  31. }
  32. namespace android {
  33. namespace vold {
  34. // Given a file path, look for the corresponding block device in /proc/mount
  35. std::string BlockDeviceForPath(const std::string& path) {
  36. std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
  37. if (!mnts) {
  38. PLOG(ERROR) << "Unable to open /proc/mounts";
  39. return "";
  40. }
  41. std::string result;
  42. size_t best_length = 0;
  43. struct mntent* mnt; // getmntent returns a thread local, so it's safe.
  44. while ((mnt = getmntent(mnts.get())) != nullptr) {
  45. auto l = strlen(mnt->mnt_dir);
  46. if (l > best_length && path.size() > l && path[l] == '/' &&
  47. path.compare(0, l, mnt->mnt_dir) == 0) {
  48. result = mnt->mnt_fsname;
  49. best_length = l;
  50. }
  51. }
  52. if (result.empty()) {
  53. LOG(ERROR) << "Didn't find a mountpoint to match path " << path;
  54. return "";
  55. }
  56. return result;
  57. }
  58. std::unique_ptr<struct fiemap> PathFiemap(const std::string& path, uint32_t extent_count) {
  59. android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC, 0)));
  60. if (fd == -1) {
  61. if (errno == ENOENT) {
  62. PLOG(DEBUG) << "Unable to open " << path;
  63. } else {
  64. PLOG(ERROR) << "Unable to open " << path;
  65. }
  66. return nullptr;
  67. }
  68. auto fiemap = alloc_fiemap(extent_count);
  69. if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
  70. PLOG(ERROR) << "Unable to FIEMAP " << path;
  71. return nullptr;
  72. }
  73. auto mapped = fiemap->fm_mapped_extents;
  74. if (mapped < 1 || mapped > extent_count) {
  75. LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
  76. << " in " << path;
  77. return nullptr;
  78. }
  79. return fiemap;
  80. }
  81. } // namespace vold
  82. } // namespace android
  83. namespace {
  84. std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) {
  85. size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
  86. std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap);
  87. memset(res.get(), 0, allocsize);
  88. res->fm_start = 0;
  89. res->fm_length = UINT64_MAX;
  90. res->fm_flags = 0;
  91. res->fm_extent_count = extent_count;
  92. res->fm_mapped_extents = 0;
  93. return res;
  94. }
  95. } // namespace