blob: 4ce98719be6efe919934a401bd453976b88725b0 [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>
21#include <android-base/stringprintf.h>
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020022#include <private/android_filesystem_config.h>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070023#include <vector>
24#include <string>
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020025#include <sys/stat.h>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070026#include <sys/mount.h>
27
28using android::base::StringPrintf;
29
30namespace android {
31namespace vold {
32namespace f2fs {
33
34static const char* kMkfsPath = "/system/bin/make_f2fs";
35static const char* kFsckPath = "/system/bin/fsck.f2fs";
36
37bool IsSupported() {
38 return access(kMkfsPath, X_OK) == 0
39 && access(kFsckPath, X_OK) == 0
40 && IsFilesystemSupported("f2fs");
41}
42
Michael Bestas7e0bc982015-12-06 23:53:55 +020043status_t Check(const std::string& source, bool trusted) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044 std::vector<std::string> cmd;
45 cmd.push_back(kFsckPath);
Yusuke Sato0765cf92015-07-21 15:47:29 -070046 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070047 cmd.push_back(source);
48
Michael Bestas7e0bc982015-12-06 23:53:55 +020049 return ForkExecvp(cmd, trusted ? sFsckContext : sFsckUntrustedContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070050}
51
Sam Mortimer11a9e062015-11-27 15:27:03 -080052status_t Mount(const std::string& source, const std::string& target,
53 const std::string& opts /* = "" */, bool trusted, bool portable) {
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020054 std::string data(opts);
55
56 if (portable) {
57 if (!data.empty()) {
58 data += ",";
59 }
60 data += "context=u:object_r:sdcard_posix:s0";
61 }
62
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070063 const char* c_source = source.c_str();
64 const char* c_target = target.c_str();
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020065 const char* c_data = data.c_str();
Sam Mortimer11a9e062015-11-27 15:27:03 -080066
67 unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID;
68
69 // Only use MS_DIRSYNC if we're not mounting adopted storage
70 if (!trusted) {
71 flags |= MS_DIRSYNC;
72 }
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070073
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020074 int res = mount(c_source, c_target, "f2fs", flags, c_data);
75 if (portable && res == 0) {
76 chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
Jani Lusikka63a4f9c2016-03-25 20:12:15 +020077 chmod(c_target, 0775);
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020078 }
79
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070080 if (res != 0) {
81 PLOG(ERROR) << "Failed to mount " << source;
82 if (errno == EROFS) {
Jani Lusikkaf95af8c2016-01-15 22:25:47 +020083 res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, c_data);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070084 if (res != 0) {
85 PLOG(ERROR) << "Failed to mount read-only " << source;
86 }
87 }
88 }
89
90 return res;
91}
92
93status_t Format(const std::string& source) {
94 std::vector<std::string> cmd;
95 cmd.push_back(kMkfsPath);
96 cmd.push_back(source);
97
98 return ForkExecvp(cmd);
99}
100
101} // namespace f2fs
102} // namespace vold
103} // namespace android