blob: c745b54d700cafcbbf280f677546acda7dc96b6c [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 }
Paul Crowley03f89d32017-06-16 09:37:31 -070064 return result;
65}
66
Paul Crowley14c8c072018-09-18 13:30:21 -070067std::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 Crowley03f89d32017-06-16 09:37:31 -070069 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 Crowley14c8c072018-09-18 13:30:21 -070085 << " in " << path;
Paul Crowley03f89d32017-06-16 09:37:31 -070086 return nullptr;
87 }
88 return fiemap;
89}
90
91} // namespace vold
92} // namespace android
93
94namespace {
95
Paul Crowley14c8c072018-09-18 13:30:21 -070096std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) {
Paul Crowley03f89d32017-06-16 09:37:31 -070097 size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
Paul Crowley14c8c072018-09-18 13:30:21 -070098 std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap);
Paul Crowley03f89d32017-06-16 09:37:31 -070099 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 Crowley14c8c072018-09-18 13:30:21 -0700108} // namespace