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 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <linux/fiemap.h>
22 #include <linux/fs.h>
23 #include <mntent.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
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
BlockDeviceForPath(const std::string & path)43 std::string BlockDeviceForPath(const std::string& path) {
44     std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
45     if (!mnts) {
46         PLOG(ERROR) << "Unable to open /proc/mounts";
47         return "";
48     }
49     std::string result;
50     size_t best_length = 0;
51     struct mntent* mnt;  // getmntent returns a thread local, so it's safe.
52     while ((mnt = getmntent(mnts.get())) != nullptr) {
53         auto l = strlen(mnt->mnt_dir);
54         if (l > best_length && path.size() > l && path[l] == '/' &&
55             path.compare(0, l, mnt->mnt_dir) == 0) {
56             result = mnt->mnt_fsname;
57             best_length = l;
58         }
59     }
60     if (result.empty()) {
61         LOG(ERROR) << "Didn't find a mountpoint to match path " << path;
62         return "";
63     }
64     return result;
65 }
66 
PathFiemap(const std::string & path,uint32_t extent_count)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)));
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
85                    << " in " << path;
86         return nullptr;
87     }
88     return fiemap;
89 }
90 
91 }  // namespace vold
92 }  // namespace android
93 
94 namespace {
95 
alloc_fiemap(uint32_t extent_count)96 std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count) {
97     size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
98     std::unique_ptr<struct fiemap> res(new (::operator new(allocsize)) struct fiemap);
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 
108 }  // namespace
109