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> |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 23 | #include <fscrypt/fscrypt.h> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 24 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 25 | #include <string> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 26 | #include <vector> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 27 | |
| 28 | #include <sys/mount.h> |
| 29 | |
| 30 | using android::base::StringPrintf; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace vold { |
| 34 | namespace f2fs { |
| 35 | |
| 36 | static const char* kMkfsPath = "/system/bin/make_f2fs"; |
| 37 | static const char* kFsckPath = "/system/bin/fsck.f2fs"; |
| 38 | |
| 39 | bool IsSupported() { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 40 | return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 && |
| 41 | IsFilesystemSupported("f2fs"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | status_t Check(const std::string& source) { |
| 45 | std::vector<std::string> cmd; |
| 46 | cmd.push_back(kFsckPath); |
Yusuke Sato | 0765cf9 | 2015-07-21 15:47:29 -0700 | [diff] [blame] | 47 | cmd.push_back("-a"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 48 | cmd.push_back(source); |
| 49 | |
| 50 | // f2fs devices are currently always trusted |
Paul Crowley | de2d620 | 2018-11-30 11:43:47 -0800 | [diff] [blame] | 51 | return ForkExecvp(cmd, nullptr, sFsckContext); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | status_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 | |
| 73 | status_t Format(const std::string& source) { |
| 74 | std::vector<std::string> cmd; |
| 75 | cmd.push_back(kMkfsPath); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 76 | |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 77 | 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 Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 84 | if (fscrypt_is_native()) { |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 85 | cmd.push_back("-O"); |
| 86 | cmd.push_back("encrypt"); |
| 87 | } |
Jaegeuk Kim | 7db02ab | 2018-04-05 22:43:25 -0700 | [diff] [blame] | 88 | |
| 89 | cmd.push_back("-O"); |
| 90 | cmd.push_back("verity"); |
| 91 | |
Martijn Coenen | 2f1c983 | 2020-03-10 09:28:39 +0100 | [diff] [blame] | 92 | const bool needs_casefold = |
Martijn Coenen | e9239f7 | 2020-04-16 10:16:29 +0200 | [diff] [blame] | 93 | android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
| 94 | const bool needs_projid = |
| 95 | android::base::GetBoolProperty("external_storage.projid.enabled", false); |
Martijn Coenen | 2f1c983 | 2020-03-10 09:28:39 +0100 | [diff] [blame] | 96 | if (needs_projid) { |
| 97 | cmd.push_back("-O"); |
| 98 | cmd.push_back("project_quota,extra_attr"); |
| 99 | } |
| 100 | if (needs_casefold) { |
| 101 | cmd.push_back("-O"); |
| 102 | cmd.push_back("casefold"); |
| 103 | cmd.push_back("-C"); |
| 104 | cmd.push_back("utf8"); |
| 105 | } |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 106 | cmd.push_back(source); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 107 | return ForkExecvp(cmd); |
| 108 | } |
| 109 | |
| 110 | } // namespace f2fs |
| 111 | } // namespace vold |
| 112 | } // namespace android |