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 | } |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 64 | return result; |
| 65 | } |
| 66 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 67 | std::unique_ptr<struct fiemap> PathFiemap(const std::string& path, uint32_t extent_count) { |
| 68 | 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] | 69 | if (fd == -1) { |
| 70 | if (errno == ENOENT) { |
| 71 | PLOG(DEBUG) << "Unable to open " << path; |
| 72 | } else { |
| 73 | PLOG(ERROR) << "Unable to open " << path; |
| 74 | } |
| 75 | return nullptr; |
| 76 | } |
| 77 | auto fiemap = alloc_fiemap(extent_count); |
| 78 | if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) { |
| 79 | PLOG(ERROR) << "Unable to FIEMAP " << path; |
| 80 | return nullptr; |
| 81 | } |
| 82 | auto mapped = fiemap->fm_mapped_extents; |
| 83 | if (mapped < 1 || mapped > extent_count) { |
| 84 | LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 85 | << " in " << path; |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 86 | return nullptr; |
| 87 | } |
| 88 | return fiemap; |
| 89 | } |
| 90 | |
| 91 | } // namespace vold |
| 92 | } // namespace android |
| 93 | |
| 94 | namespace { |
| 95 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 96 | std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) { |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 97 | size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 98 | std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap); |
Paul Crowley | 03f89d3 | 2017-06-16 09:37:31 -0700 | [diff] [blame] | 99 | memset(res.get(), 0, allocsize); |
| 100 | res->fm_start = 0; |
| 101 | res->fm_length = UINT64_MAX; |
| 102 | res->fm_flags = 0; |
| 103 | res->fm_extent_count = extent_count; |
| 104 | res->fm_mapped_extents = 0; |
| 105 | return res; |
| 106 | } |
| 107 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 108 | } // namespace |