xref: /aosp_15_r20/system/vold/fs/F2fs.cpp (revision f40fafd4c6c2594924d919feffc1a1fd6e3b30f3)
1 /*
2  * Copyright (C) 2015 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 "F2fs.h"
18 #include "Utils.h"
19 
20 #include <android-base/logging.h>
21 #include <android-base/properties.h>
22 #include <android-base/stringprintf.h>
23 #include <logwrap/logwrap.h>
24 #include <fscrypt/fscrypt.h>
25 
26 #include <string>
27 #include <vector>
28 
29 #include <sys/mount.h>
30 #include <filesystem>
31 
32 using android::base::StringPrintf;
33 
34 namespace android {
35 namespace vold {
36 namespace f2fs {
37 
38 static const char* kMkfsPath = "/system/bin/make_f2fs";
39 static const char* kFsckPath = "/system/bin/fsck.f2fs";
40 
IsSupported()41 bool IsSupported() {
42     return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
43            IsFilesystemSupported("f2fs");
44 }
45 
Check(const std::string & source)46 status_t Check(const std::string& source) {
47     std::vector<std::string> cmd;
48     cmd.push_back(kFsckPath);
49     cmd.push_back("-a");
50     cmd.push_back(source);
51 
52     // f2fs devices are currently always trusted
53     return ForkExecvp(cmd, nullptr, sFsckContext);
54 }
55 
Mount(const std::string & source,const std::string & target)56 status_t Mount(const std::string& source, const std::string& target) {
57     const char* c_source = source.c_str();
58     const char* c_target = target.c_str();
59     unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
60 
61     int res = mount(c_source, c_target, "f2fs", flags, NULL);
62     if (res != 0) {
63         PLOG(ERROR) << "Failed to mount " << source;
64         if (errno == EROFS) {
65             res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
66             if (res != 0) {
67                 PLOG(ERROR) << "Failed to mount read-only " << source;
68             }
69         }
70     }
71 
72     return res;
73 }
74 
Format(const std::string & source,bool is_zoned,const std::vector<std::string> & user_devices,const std::vector<bool> & device_aliased,int64_t length)75 status_t Format(const std::string& source, bool is_zoned,
76                 const std::vector<std::string>& user_devices,
77                 const std::vector<bool>& device_aliased, int64_t length) {
78     std::vector<std::string> cmd;
79     /* '-g android' parameter passed here which defaults the sector size to 4096 */
80     static constexpr int kSectorSize = 4096;
81     cmd.emplace_back(kMkfsPath);
82 
83     cmd.emplace_back("-f");
84     cmd.emplace_back("-d1");
85 
86     cmd.emplace_back("-g");
87     cmd.emplace_back("android");
88 
89     if (android::base::GetBoolProperty("vold.has_compress", false)) {
90         cmd.emplace_back("-O");
91         cmd.emplace_back("compression");
92         cmd.emplace_back("-O");
93         cmd.emplace_back("extra_attr");
94     }
95 
96     const bool needs_casefold =
97             android::base::GetBoolProperty("external_storage.casefold.enabled", false);
98     if (needs_casefold) {
99         cmd.emplace_back("-O");
100         cmd.emplace_back("casefold");
101         cmd.emplace_back("-C");
102         cmd.emplace_back("utf8");
103     }
104     if (is_zoned) {
105         cmd.emplace_back("-m");
106     }
107     for (size_t i = 0; i < user_devices.size(); i++) {
108         std::string device_name = user_devices[i];
109 
110         cmd.emplace_back("-c");
111         if (device_aliased[i]) {
112             std::filesystem::path path = device_name;
113             device_name += "@" + path.filename().string();
114         }
115         cmd.emplace_back(device_name);
116     }
117     cmd.emplace_back("-b");
118     cmd.emplace_back(std::to_string(getpagesize()));
119 
120     cmd.emplace_back(source.c_str());
121 
122     if (length) {
123         cmd.emplace_back(std::to_string(length / kSectorSize));
124     }
125 
126     std::vector<char const*> cmd_cstrs;
127     for (auto& arg : cmd) {
128         cmd_cstrs.emplace_back(arg.c_str());
129     }
130     return logwrap_fork_execvp(cmd_cstrs.size(), cmd_cstrs.data(), nullptr, false, LOG_KLOG, false,
131                                nullptr);
132 }
133 
134 }  // namespace f2fs
135 }  // namespace vold
136 }  // namespace android
137