blob: 99afc32a42f77f0193b7e6f50d6425cef41a97c1 [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 Kim2c1380f2021-04-14 12:02:41 -070023#include <logwrap/logwrap.h>
Eric Biggersa701c452018-10-23 13:06:55 -070024#include <fscrypt/fscrypt.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
29#include <sys/mount.h>
30
31using android::base::StringPrintf;
32
33namespace android {
34namespace vold {
35namespace f2fs {
36
37static const char* kMkfsPath = "/system/bin/make_f2fs";
38static const char* kFsckPath = "/system/bin/fsck.f2fs";
39
40bool IsSupported() {
Paul Crowley14c8c072018-09-18 13:30:21 -070041 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
42 IsFilesystemSupported("f2fs");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070043}
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
Paul Crowleyde2d6202018-11-30 11:43:47 -080052 return ForkExecvp(cmd, nullptr, sFsckContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070053}
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
Jaegeuk Kim2091c872024-04-23 19:01:26 -070074status_t Format(const std::string& source, bool is_zoned,
75 const std::vector<std::string>& user_devices) {
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070076 std::vector<char const*> cmd;
77 cmd.emplace_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070078
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070079 cmd.emplace_back("-f");
80 cmd.emplace_back("-d1");
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080081
Jaegeuk Kim2b8957d2022-02-09 13:49:53 -080082 cmd.emplace_back("-g");
83 cmd.emplace_back("android");
84
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -080085 if (android::base::GetBoolProperty("vold.has_compress", false)) {
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070086 cmd.emplace_back("-O");
87 cmd.emplace_back("compression");
88 cmd.emplace_back("-O");
89 cmd.emplace_back("extra_attr");
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -080090 }
Jaegeuk Kim7db02ab2018-04-05 22:43:25 -070091
Martijn Coenen2f1c9832020-03-10 09:28:39 +010092 const bool needs_casefold =
Martijn Coenene9239f72020-04-16 10:16:29 +020093 android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Martijn Coenen2f1c9832020-03-10 09:28:39 +010094 if (needs_casefold) {
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070095 cmd.emplace_back("-O");
96 cmd.emplace_back("casefold");
97 cmd.emplace_back("-C");
98 cmd.emplace_back("utf8");
Martijn Coenen2f1c9832020-03-10 09:28:39 +010099 }
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700100 if (is_zoned) {
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800101 cmd.emplace_back("-m");
102 }
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700103 for (auto& device : user_devices) {
104 cmd.emplace_back("-c");
105 cmd.emplace_back(device.c_str());
106 }
Daniel Rosenbergfb2bf902023-11-08 16:51:55 -0800107 std::string block_size = std::to_string(getpagesize());
108 cmd.emplace_back("-b");
109 cmd.emplace_back(block_size.c_str());
110
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -0700111 cmd.emplace_back(source.c_str());
Daniel Rosenbergfb2bf902023-11-08 16:51:55 -0800112
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -0700113 return logwrap_fork_execvp(cmd.size(), cmd.data(), nullptr, false, LOG_KLOG,
114 false, nullptr);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700115}
116
117} // namespace f2fs
118} // namespace vold
119} // namespace android