blob: ce938a954cf3e00c9d67fbe95d6ef41385e1de94 [file] [log] [blame]
Paul Crowley03f89d32017-06-16 09:37:31 -07001/*
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 Crowley14c8c072018-09-18 13:30:21 -070019#include <errno.h>
20#include <fcntl.h>
21#include <linux/fiemap.h>
22#include <linux/fs.h>
23#include <mntent.h>
Paul Crowley03f89d32017-06-16 09:37:31 -070024#include <stdio.h>
25#include <stdlib.h>
Paul Crowley03f89d32017-06-16 09:37:31 -070026#include <sys/stat.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <sys/types.h>
Paul Crowley03f89d32017-06-16 09:37:31 -070028
29#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/unique_fd.h>
32
33namespace {
34
35std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
36
37}
38
39namespace android {
40namespace vold {
41
42// Given a file path, look for the corresponding block device in /proc/mount
Paul Crowley14c8c072018-09-18 13:30:21 -070043std::string BlockDeviceForPath(const std::string& path) {
44 std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
Paul Crowley03f89d32017-06-16 09:37:31 -070045 if (!mnts) {
46 PLOG(ERROR) << "Unable to open /proc/mounts";
47 return "";
48 }
49 std::string result;
50 size_t best_length = 0;
Paul Crowley14c8c072018-09-18 13:30:21 -070051 struct mntent* mnt; // getmntent returns a thread local, so it's safe.
Paul Crowley03f89d32017-06-16 09:37:31 -070052 while ((mnt = getmntent(mnts.get())) != nullptr) {
53 auto l = strlen(mnt->mnt_dir);
Paul Crowley14c8c072018-09-18 13:30:21 -070054 if (l > best_length && path.size() > l && path[l] == '/' &&
Paul Crowley03f89d32017-06-16 09:37:31 -070055 path.compare(0, l, mnt->mnt_dir) == 0) {
Paul Crowley14c8c072018-09-18 13:30:21 -070056 result = mnt->mnt_fsname;
57 best_length = l;
Paul Crowley03f89d32017-06-16 09:37:31 -070058 }
59 }
60 if (result.empty()) {
Paul Crowley14c8c072018-09-18 13:30:21 -070061 LOG(ERROR) << "Didn't find a mountpoint to match path " << path;
Paul Crowley03f89d32017-06-16 09:37:31 -070062 return "";
63 }
64 LOG(DEBUG) << "For path " << path << " block device is " << result;
65 return result;
66}
67
Paul Crowley14c8c072018-09-18 13:30:21 -070068std::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 Crowley03f89d32017-06-16 09:37:31 -070070 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 Crowley14c8c072018-09-18 13:30:21 -070086 << " in " << path;
Paul Crowley03f89d32017-06-16 09:37:31 -070087 return nullptr;
88 }
89 return fiemap;
90}
91
92} // namespace vold
93} // namespace android
94
95namespace {
96
Paul Crowley14c8c072018-09-18 13:30:21 -070097std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) {
Paul Crowley03f89d32017-06-16 09:37:31 -070098 size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
Paul Crowley14c8c072018-09-18 13:30:21 -070099 std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap);
Paul Crowley03f89d32017-06-16 09:37:31 -0700100 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 Crowley14c8c072018-09-18 13:30:21 -0700109} // namespace