blob: e0ead65ec38f9e80230b4048dde88fdb6993a1be [file] [log] [blame]
Jeff Sharkeyd0640f62015-05-21 22:35:42 -07001/*
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
Elliott Hughes7e128fb2015-12-04 15:50:53 -080020#include <android-base/logging.h>
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080021#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/stringprintf.h>
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080023#include <ext4_utils/ext4_crypt.h>
Jani Lusikka2450cd92016-01-15 22:25:47 +020024#include <private/android_filesystem_config.h>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070025
26#include <vector>
27#include <string>
28
Jani Lusikka2450cd92016-01-15 22:25:47 +020029#include <sys/stat.h>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070030#include <sys/mount.h>
31
32using android::base::StringPrintf;
33
34namespace android {
35namespace vold {
36namespace f2fs {
37
38static const char* kMkfsPath = "/system/bin/make_f2fs";
39static const char* kFsckPath = "/system/bin/fsck.f2fs";
40
41bool IsSupported() {
42 return access(kMkfsPath, X_OK) == 0
43 && access(kFsckPath, X_OK) == 0
44 && IsFilesystemSupported("f2fs");
45}
46
Michael Bestasa66df7c2015-12-06 23:53:55 +020047status_t Check(const std::string& source, bool trusted) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070048 std::vector<std::string> cmd;
49 cmd.push_back(kFsckPath);
Yusuke Sato0765cf92015-07-21 15:47:29 -070050 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070051 cmd.push_back(source);
52
Michael Bestasa66df7c2015-12-06 23:53:55 +020053 return ForkExecvp(cmd, trusted ? sFsckContext : sFsckUntrustedContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054}
55
Sam Mortimerbb0533a2015-11-27 15:27:03 -080056status_t Mount(const std::string& source, const std::string& target,
57 const std::string& opts /* = "" */, bool trusted, bool portable) {
Jani Lusikka2450cd92016-01-15 22:25:47 +020058 std::string data(opts);
59
60 if (portable) {
61 if (!data.empty()) {
62 data += ",";
63 }
64 data += "context=u:object_r:sdcard_posix:s0";
65 }
66
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070067 const char* c_source = source.c_str();
68 const char* c_target = target.c_str();
Jani Lusikka2450cd92016-01-15 22:25:47 +020069 const char* c_data = data.c_str();
Sam Mortimerbb0533a2015-11-27 15:27:03 -080070
71 unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
72
73 // Only use MS_DIRSYNC if we're not mounting adopted storage
74 if (!trusted) {
75 flags |= MS_DIRSYNC;
76 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070077
Jani Lusikka2450cd92016-01-15 22:25:47 +020078 int res = mount(c_source, c_target, "f2fs", flags, c_data);
79 if (portable && res == 0) {
80 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
Jani Lusikka52d1e602016-03-25 20:12:15 +020081 chmod(c_target, 0775);
Jani Lusikka2450cd92016-01-15 22:25:47 +020082 }
83
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070084 if (res != 0) {
85 PLOG(ERROR) << "Failed to mount " << source;
86 if (errno == EROFS) {
Jani Lusikka2450cd92016-01-15 22:25:47 +020087 res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, c_data);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070088 if (res != 0) {
89 PLOG(ERROR) << "Failed to mount read-only " << source;
90 }
91 }
92 }
93
94 return res;
95}
96
97status_t Format(const std::string& source) {
98 std::vector<std::string> cmd;
99 cmd.push_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700100
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800101 cmd.push_back("-f");
102 cmd.push_back("-d1");
103
104 if (android::base::GetBoolProperty("vold.has_quota", false)) {
105 cmd.push_back("-O");
106 cmd.push_back("quota");
107 }
108 if (e4crypt_is_native()) {
109 cmd.push_back("-O");
110 cmd.push_back("encrypt");
111 }
Jaegeuk Kim7db02ab2018-04-05 22:43:25 -0700112
113 cmd.push_back("-O");
114 cmd.push_back("verity");
115
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800116 cmd.push_back(source);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700117 return ForkExecvp(cmd);
118}
119
120} // namespace f2fs
121} // namespace vold
122} // namespace android