blob: 33cc4a27ad0fa25540a12a61a1b8a868b021b1ba [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>
Eric Biggersa701c452018-10-23 13:06:55 -070023#include <fscrypt/fscrypt.h>
Jani Lusikkac8d267d2016-01-15 22:25:47 +020024#include <private/android_filesystem_config.h>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070025
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070026#include <string>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <vector>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070028
Jani Lusikkac8d267d2016-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() {
Paul Crowley14c8c072018-09-18 13:30:21 -070042 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
43 IsFilesystemSupported("f2fs");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044}
45
Michael Bestas86918bf2015-12-06 23:53:55 +020046status_t Check(const std::string& source, bool trusted) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070047 std::vector<std::string> cmd;
48 cmd.push_back(kFsckPath);
Yusuke Sato0765cf92015-07-21 15:47:29 -070049 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070050 cmd.push_back(source);
51
Michael Bestas86918bf2015-12-06 23:53:55 +020052 return ForkExecvp(cmd, nullptr, trusted ? sFsckContext : sFsckUntrustedContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070053}
54
Sam Mortimer09aef342015-11-27 15:27:03 -080055status_t Mount(const std::string& source, const std::string& target,
56 const std::string& opts /* = "" */, bool trusted, bool portable) {
Jani Lusikkac8d267d2016-01-15 22:25:47 +020057 std::string data(opts);
58
59 if (portable) {
60 if (!data.empty()) {
61 data += ",";
62 }
63 data += "context=u:object_r:sdcard_posix:s0";
64 }
65
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070066 const char* c_source = source.c_str();
67 const char* c_target = target.c_str();
Jani Lusikkac8d267d2016-01-15 22:25:47 +020068 const char* c_data = data.c_str();
Sam Mortimer09aef342015-11-27 15:27:03 -080069
70 unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
71
72 // Only use MS_DIRSYNC if we're not mounting adopted storage
73 if (!trusted) {
74 flags |= MS_DIRSYNC;
75 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070076
Jani Lusikkac8d267d2016-01-15 22:25:47 +020077 int res = mount(c_source, c_target, "f2fs", flags, c_data);
78 if (portable && res == 0) {
79 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
Jani Lusikkabf60c5f2016-03-25 20:12:15 +020080 chmod(c_target, 0775);
Jani Lusikkac8d267d2016-01-15 22:25:47 +020081 }
82
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070083 if (res != 0) {
84 PLOG(ERROR) << "Failed to mount " << source;
85 if (errno == EROFS) {
Jani Lusikkac8d267d2016-01-15 22:25:47 +020086 res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, c_data);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070087 if (res != 0) {
88 PLOG(ERROR) << "Failed to mount read-only " << source;
89 }
90 }
91 }
92
93 return res;
94}
95
96status_t Format(const std::string& source) {
97 std::vector<std::string> cmd;
98 cmd.push_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070099
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800100 cmd.push_back("-f");
101 cmd.push_back("-d1");
102
103 if (android::base::GetBoolProperty("vold.has_quota", false)) {
104 cmd.push_back("-O");
105 cmd.push_back("quota");
106 }
Eric Biggersa701c452018-10-23 13:06:55 -0700107 if (fscrypt_is_native()) {
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800108 cmd.push_back("-O");
109 cmd.push_back("encrypt");
110 }
Jaegeuk Kim7db02ab2018-04-05 22:43:25 -0700111
112 cmd.push_back("-O");
113 cmd.push_back("verity");
114
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800115 cmd.push_back(source);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700116 return ForkExecvp(cmd);
117}
118
119} // namespace f2fs
120} // namespace vold
121} // namespace android