blob: d6f3dab100da8c8c384a7005bfefac1ea6132298 [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>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070024
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070025#include <string>
Paul Crowley14c8c072018-09-18 13:30:21 -070026#include <vector>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070027
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() {
Paul Crowley14c8c072018-09-18 13:30:21 -070040 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
41 IsFilesystemSupported("f2fs");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070042}
43
44status_t Check(const std::string& source) {
45 std::vector<std::string> cmd;
46 cmd.push_back(kFsckPath);
Yusuke Sato0765cf92015-07-21 15:47:29 -070047 cmd.push_back("-a");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070048 cmd.push_back(source);
49
50 // f2fs devices are currently always trusted
Paul Crowleyde2d6202018-11-30 11:43:47 -080051 return ForkExecvp(cmd, nullptr, sFsckContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070052}
53
54status_t Mount(const std::string& source, const std::string& target) {
55 const char* c_source = source.c_str();
56 const char* c_target = target.c_str();
57 unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
58
59 int res = mount(c_source, c_target, "f2fs", flags, NULL);
60 if (res != 0) {
61 PLOG(ERROR) << "Failed to mount " << source;
62 if (errno == EROFS) {
63 res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
64 if (res != 0) {
65 PLOG(ERROR) << "Failed to mount read-only " << source;
66 }
67 }
68 }
69
70 return res;
71}
72
73status_t Format(const std::string& source) {
74 std::vector<std::string> cmd;
75 cmd.push_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070076
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080077 cmd.push_back("-f");
78 cmd.push_back("-d1");
79
80 if (android::base::GetBoolProperty("vold.has_quota", false)) {
81 cmd.push_back("-O");
82 cmd.push_back("quota");
83 }
Eric Biggersa701c452018-10-23 13:06:55 -070084 if (fscrypt_is_native()) {
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080085 cmd.push_back("-O");
86 cmd.push_back("encrypt");
87 }
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -080088 if (android::base::GetBoolProperty("vold.has_compress", false)) {
89 cmd.push_back("-O");
90 cmd.push_back("compression");
91 cmd.push_back("-O");
92 cmd.push_back("extra_attr");
93 }
Jaegeuk Kim7db02ab2018-04-05 22:43:25 -070094 cmd.push_back("-O");
95 cmd.push_back("verity");
96
Martijn Coenen2f1c9832020-03-10 09:28:39 +010097 const bool needs_casefold =
Martijn Coenene9239f72020-04-16 10:16:29 +020098 android::base::GetBoolProperty("external_storage.casefold.enabled", false);
99 const bool needs_projid =
100 android::base::GetBoolProperty("external_storage.projid.enabled", false);
Martijn Coenen2f1c9832020-03-10 09:28:39 +0100101 if (needs_projid) {
102 cmd.push_back("-O");
103 cmd.push_back("project_quota,extra_attr");
104 }
105 if (needs_casefold) {
106 cmd.push_back("-O");
107 cmd.push_back("casefold");
108 cmd.push_back("-C");
109 cmd.push_back("utf8");
110 }
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -0800111 cmd.push_back(source);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700112 return ForkExecvp(cmd);
113}
114
115} // namespace f2fs
116} // namespace vold
117} // namespace android