Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 23 | #include <logwrap/logwrap.h> |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 24 | #include <fscrypt/fscrypt.h> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 25 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 26 | #include <string> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 27 | #include <vector> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 28 | |
| 29 | #include <sys/mount.h> |
| 30 | |
| 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace vold { |
| 35 | namespace f2fs { |
| 36 | |
| 37 | static const char* kMkfsPath = "/system/bin/make_f2fs"; |
| 38 | static const char* kFsckPath = "/system/bin/fsck.f2fs"; |
| 39 | |
| 40 | bool IsSupported() { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 41 | return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 && |
| 42 | IsFilesystemSupported("f2fs"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 45 | status_t Check(const std::string& source) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 46 | std::vector<std::string> cmd; |
| 47 | cmd.push_back(kFsckPath); |
Yusuke Sato | 0765cf9 | 2015-07-21 15:47:29 -0700 | [diff] [blame] | 48 | cmd.push_back("-a"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 49 | cmd.push_back(source); |
| 50 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 51 | // f2fs devices are currently always trusted |
| 52 | return ForkExecvp(cmd, nullptr, sFsckContext); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 55 | status_t Mount(const std::string& source, const std::string& target) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 56 | const char* c_source = source.c_str(); |
| 57 | const char* c_target = target.c_str(); |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 58 | unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
Sam Mortimer | c087137 | 2015-11-27 15:27:03 -0800 | [diff] [blame] | 59 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 60 | int res = mount(c_source, c_target, "f2fs", flags, NULL); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 61 | if (res != 0) { |
| 62 | PLOG(ERROR) << "Failed to mount " << source; |
| 63 | if (errno == EROFS) { |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame] | 64 | res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 65 | if (res != 0) { |
| 66 | PLOG(ERROR) << "Failed to mount read-only " << source; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return res; |
| 72 | } |
| 73 | |
| 74 | status_t Format(const std::string& source) { |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 75 | std::vector<char const*> cmd; |
| 76 | cmd.emplace_back(kMkfsPath); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 77 | |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 78 | cmd.emplace_back("-f"); |
| 79 | cmd.emplace_back("-d1"); |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 80 | |
Jaegeuk Kim | 2b8957d | 2022-02-09 13:49:53 -0800 | [diff] [blame] | 81 | cmd.emplace_back("-g"); |
| 82 | cmd.emplace_back("android"); |
| 83 | |
Jaegeuk Kim | f64d30a | 2020-01-14 11:22:26 -0800 | [diff] [blame] | 84 | if (android::base::GetBoolProperty("vold.has_compress", false)) { |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 85 | cmd.emplace_back("-O"); |
| 86 | cmd.emplace_back("compression"); |
| 87 | cmd.emplace_back("-O"); |
| 88 | cmd.emplace_back("extra_attr"); |
Jaegeuk Kim | f64d30a | 2020-01-14 11:22:26 -0800 | [diff] [blame] | 89 | } |
Jaegeuk Kim | 7db02ab | 2018-04-05 22:43:25 -0700 | [diff] [blame] | 90 | |
Martijn Coenen | 2f1c983 | 2020-03-10 09:28:39 +0100 | [diff] [blame] | 91 | const bool needs_casefold = |
Martijn Coenen | e9239f7 | 2020-04-16 10:16:29 +0200 | [diff] [blame] | 92 | android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
Martijn Coenen | 2f1c983 | 2020-03-10 09:28:39 +0100 | [diff] [blame] | 93 | if (needs_casefold) { |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 94 | cmd.emplace_back("-O"); |
| 95 | cmd.emplace_back("casefold"); |
| 96 | cmd.emplace_back("-C"); |
| 97 | cmd.emplace_back("utf8"); |
Martijn Coenen | 2f1c983 | 2020-03-10 09:28:39 +0100 | [diff] [blame] | 98 | } |
Jaegeuk Kim | 2c1380f | 2021-04-14 12:02:41 -0700 | [diff] [blame] | 99 | cmd.emplace_back(source.c_str()); |
| 100 | return logwrap_fork_execvp(cmd.size(), cmd.data(), nullptr, false, LOG_KLOG, |
| 101 | false, nullptr); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace f2fs |
| 105 | } // namespace vold |
| 106 | } // namespace android |