Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 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 | |
| 17 | #include "FileDeviceUtils.h" |
| 18 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <linux/fiemap.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <mntent.h> |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 28 | |
| 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/unique_fd.h> |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | namespace android { |
| 40 | namespace vold { |
| 41 | |
| 42 | // Given a file path, look for the corresponding block device in /proc/mount |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 43 | std::string BlockDeviceForPath(const std::string& path) { |
| 44 | std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent); |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 45 | if (!mnts) { |
| 46 | PLOG(ERROR) << "Unable to open /proc/mounts"; |
| 47 | return ""; |
| 48 | } |
| 49 | std::string result; |
| 50 | size_t best_length = 0; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 51 | struct mntent* mnt; // getmntent returns a thread local, so it's safe. |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 52 | while ((mnt = getmntent(mnts.get())) != nullptr) { |
| 53 | auto l = strlen(mnt->mnt_dir); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 54 | if (l > best_length && path.size() > l && path[l] == '/' && |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 55 | path.compare(0, l, mnt->mnt_dir) == 0) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 56 | result = mnt->mnt_fsname; |
| 57 | best_length = l; |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | if (result.empty()) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 61 | LOG(ERROR) << "Didn't find a mountpoint to match path " << path; |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 62 | return ""; |
| 63 | } |
| 64 | LOG(DEBUG) << "For path " << path << " block device is " << result; |
| 65 | return result; |
| 66 | } |
| 67 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 68 | std::unique_ptr<struct fiemap> PathFiemap(const std::string& path, uint32_t extent_count) { |
| 69 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC, 0))); |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 70 | if (fd == -1) { |
| 71 | if (errno == ENOENT) { |
| 72 | PLOG(DEBUG) << "Unable to open " << path; |
| 73 | } else { |
| 74 | PLOG(ERROR) << "Unable to open " << path; |
| 75 | } |
| 76 | return nullptr; |
| 77 | } |
| 78 | auto fiemap = alloc_fiemap(extent_count); |
| 79 | if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { |
| 80 | PLOG(ERROR) << "Unable to FIEMAP " << path; |
| 81 | return nullptr; |
| 82 | } |
| 83 | auto mapped = fiemap->fm_mapped_extents; |
| 84 | if (mapped < 1 || mapped > extent_count) { |
| 85 | LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 86 | << " in " << path; |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
| 89 | return fiemap; |
| 90 | } |
| 91 | |
| 92 | } // namespace vold |
| 93 | } // namespace android |
| 94 | |
| 95 | namespace { |
| 96 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 97 | std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) { |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 98 | size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 99 | std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap); |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 100 | memset(res.get(), 0, allocsize); |
| 101 | res->fm_start = 0; |
| 102 | res->fm_length = UINT64_MAX; |
| 103 | res->fm_flags = 0; |
| 104 | res->fm_extent_count = extent_count; |
| 105 | res->fm_mapped_extents = 0; |
| 106 | return res; |
| 107 | } |
| 108 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 109 | } // namespace |