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 
31 using android::base::StringPrintf;
32 
33 namespace android {
34 namespace vold {
35 namespace f2fs {
36 
37 static const char* kMkfsPath = "/system/bin/make_f2fs";
38 static const char* kFsckPath = "/system/bin/fsck.f2fs";
39 
IsSupported()40 bool IsSupported() {
41     return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
42            IsFilesystemSupported("f2fs");
43 }
44 
Check(const std::string & source)45 status_t Check(const std::string& source) {
46     std::vector<std::string> cmd;
47     cmd.push_back(kFsckPath);
48     cmd.push_back("-a");
49     cmd.push_back(source);
50 
51     // f2fs devices are currently always trusted
52     return ForkExecvp(cmd, nullptr, sFsckContext);
53 }
54 
Mount(const std::string & source,const std::string & target)55 status_t Mount(const std::string& source, const std::string& target) {
56     const char* c_source = source.c_str();
57     const char* c_target = target.c_str();
58     unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
59 
60     int res = mount(c_source, c_target, "f2fs", flags, NULL);
61     if (res != 0) {
62         PLOG(ERROR) << "Failed to mount " << source;
63         if (errno == EROFS) {
64             res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
65             if (res != 0) {
66                 PLOG(ERROR) << "Failed to mount read-only " << source;
67             }
68         }
69     }
70 
71     return res;
72 }
73 
Format(const std::string & source)74 status_t Format(const std::string& source) {
75     std::vector<char const*> cmd;
76     cmd.emplace_back(kMkfsPath);
77 
78     cmd.emplace_back("-f");
79     cmd.emplace_back("-d1");
80 
81     if (android::base::GetBoolProperty("vold.has_quota", false)) {
82         cmd.emplace_back("-O");
83         cmd.emplace_back("quota");
84     }
85     if (fscrypt_is_native()) {
86         cmd.emplace_back("-O");
87         cmd.emplace_back("encrypt");
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     cmd.emplace_back("-O");
96     cmd.emplace_back("verity");
97 
98     const bool needs_casefold =
99             android::base::GetBoolProperty("external_storage.casefold.enabled", false);
100     const bool needs_projid =
101             android::base::GetBoolProperty("external_storage.projid.enabled", false);
102     if (needs_projid) {
103         cmd.emplace_back("-O");
104         cmd.emplace_back("project_quota,extra_attr");
105     }
106     if (needs_casefold) {
107         cmd.emplace_back("-O");
108         cmd.emplace_back("casefold");
109         cmd.emplace_back("-C");
110         cmd.emplace_back("utf8");
111     }
112     cmd.emplace_back(source.c_str());
113     return logwrap_fork_execvp(cmd.size(), cmd.data(), nullptr, false, LOG_KLOG,
114                              false, nullptr);
115 }
116 
117 }  // namespace f2fs
118 }  // namespace vold
119 }  // namespace android
120