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 | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 23 | #include <ext4_utils/ext4_crypt.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 |
| 51 | return ForkExecvp(cmd, sFsckContext); |
| 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 | } |
| 84 | if (e4crypt_is_native()) { |
| 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 | |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 92 | cmd.push_back(source); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 93 | return ForkExecvp(cmd); |
| 94 | } |
| 95 | |
| 96 | } // namespace f2fs |
| 97 | } // namespace vold |
| 98 | } // namespace android |