blob: c52e80e852b92d87e5ebb9bf66ba491150b289d8 [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>
Daeho Jeong0c841552024-10-21 14:05:31 -070030#include <filesystem>
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070031
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
46status_t Check(const std::string& source) {
47 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
52 // f2fs devices are currently always trusted
Paul Crowleyde2d6202018-11-30 11:43:47 -080053 return ForkExecvp(cmd, nullptr, sFsckContext);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054}
55
56status_t Mount(const std::string& source, const std::string& target) {
57 const char* c_source = source.c_str();
58 const char* c_target = target.c_str();
59 unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
60
61 int res = mount(c_source, c_target, "f2fs", flags, NULL);
62 if (res != 0) {
63 PLOG(ERROR) << "Failed to mount " << source;
64 if (errno == EROFS) {
65 res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
66 if (res != 0) {
67 PLOG(ERROR) << "Failed to mount read-only " << source;
68 }
69 }
70 }
71
72 return res;
73}
74
Jaegeuk Kim2091c872024-04-23 19:01:26 -070075status_t Format(const std::string& source, bool is_zoned,
Daeho Jeong0c841552024-10-21 14:05:31 -070076 const std::vector<std::string>& user_devices,
77 const std::vector<bool>& device_aliased, int64_t length) {
Peter Collingbourne04e04702024-11-01 14:14:45 -070078 std::vector<std::string> cmd;
Ashok Mutyala8a398782024-05-28 09:31:44 +000079 /* '-g android' parameter passed here which defaults the sector size to 4096 */
80 static constexpr int kSectorSize = 4096;
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070081 cmd.emplace_back(kMkfsPath);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070082
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070083 cmd.emplace_back("-f");
84 cmd.emplace_back("-d1");
Jaegeuk Kimc7c477b2017-11-13 17:38:39 -080085
Jaegeuk Kim2b8957d2022-02-09 13:49:53 -080086 cmd.emplace_back("-g");
87 cmd.emplace_back("android");
88
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -080089 if (android::base::GetBoolProperty("vold.has_compress", false)) {
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070090 cmd.emplace_back("-O");
91 cmd.emplace_back("compression");
92 cmd.emplace_back("-O");
93 cmd.emplace_back("extra_attr");
Jaegeuk Kimf64d30a2020-01-14 11:22:26 -080094 }
Jaegeuk Kim7db02ab2018-04-05 22:43:25 -070095
Martijn Coenen2f1c9832020-03-10 09:28:39 +010096 const bool needs_casefold =
Martijn Coenene9239f72020-04-16 10:16:29 +020097 android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Martijn Coenen2f1c9832020-03-10 09:28:39 +010098 if (needs_casefold) {
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -070099 cmd.emplace_back("-O");
100 cmd.emplace_back("casefold");
101 cmd.emplace_back("-C");
102 cmd.emplace_back("utf8");
Martijn Coenen2f1c9832020-03-10 09:28:39 +0100103 }
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700104 if (is_zoned) {
Jaegeuk Kimf6151b42020-12-15 09:02:29 -0800105 cmd.emplace_back("-m");
106 }
Daeho Jeong0c841552024-10-21 14:05:31 -0700107 for (size_t i = 0; i < user_devices.size(); i++) {
108 std::string device_name = user_devices[i];
109
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700110 cmd.emplace_back("-c");
Daeho Jeong0c841552024-10-21 14:05:31 -0700111 if (device_aliased[i]) {
112 std::filesystem::path path = device_name;
113 device_name += "@" + path.filename().string();
114 }
Peter Collingbourne04e04702024-11-01 14:14:45 -0700115 cmd.emplace_back(device_name);
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700116 }
Daniel Rosenbergfb2bf902023-11-08 16:51:55 -0800117 cmd.emplace_back("-b");
Peter Collingbourne04e04702024-11-01 14:14:45 -0700118 cmd.emplace_back(std::to_string(getpagesize()));
Daniel Rosenbergfb2bf902023-11-08 16:51:55 -0800119
Jaegeuk Kim2c1380f2021-04-14 12:02:41 -0700120 cmd.emplace_back(source.c_str());
Daniel Rosenbergfb2bf902023-11-08 16:51:55 -0800121
Ashok Mutyala8a398782024-05-28 09:31:44 +0000122 if (length) {
Peter Collingbourne04e04702024-11-01 14:14:45 -0700123 cmd.emplace_back(std::to_string(length / kSectorSize));
Ashok Mutyala8a398782024-05-28 09:31:44 +0000124 }
Peter Collingbourne04e04702024-11-01 14:14:45 -0700125
126 std::vector<char const*> cmd_cstrs;
127 for (auto& arg : cmd) {
128 cmd_cstrs.emplace_back(arg.c_str());
129 }
130 return logwrap_fork_execvp(cmd_cstrs.size(), cmd_cstrs.data(), nullptr, false, LOG_KLOG, false,
131 nullptr);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700132}
133
134} // namespace f2fs
135} // namespace vold
136} // namespace android