blob: 56369d5db93e41e27824a4c7db92774db8b49a2b [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>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070024
25#include <vector>
26#include <string>
27
28#include <sys/mount.h>
29
30using android::base::StringPrintf;
31
32namespace android {
33namespace vold {
34namespace f2fs {
35
36static const char* kMkfsPath = "/system/bin/make_f2fs";
37static const char* kFsckPath = "/system/bin/fsck.f2fs";
38
39bool IsSupported() {
40 return access(kMkfsPath, X_OK) == 0
41 && access(kFsckPath, X_OK) == 0
42 && IsFilesystemSupported("f2fs");
43}
44
45status_t Check(const std::string& source) {
46 std::vector<std::string> cmd;
47 cmd.push_back(kFsckPath);
Yusuke Sato0765cf92015-07-21 15:47:29 -070048 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070049 cmd.push_back(source);
50
51 // f2fs devices are currently always trusted
52 return ForkExecvp(cmd, sFsckContext);
53}
54
55status_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
74status_t Format(const std::string& source) {
75 std::vector<std::string> cmd;
76 cmd.push_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070077
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080078 cmd.push_back("-f");
79 cmd.push_back("-d1");
80
81 if (android::base::GetBoolProperty("vold.has_quota", false)) {
82 cmd.push_back("-O");
83 cmd.push_back("quota");
84 }
85 if (e4crypt_is_native()) {
86 cmd.push_back("-O");
87 cmd.push_back("encrypt");
88 }
89 cmd.push_back(source);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070090 return ForkExecvp(cmd);
91}
92
93} // namespace f2fs
94} // namespace vold
95} // namespace android