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> |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 24 | #include <private/android_filesystem_config.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 | |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
| 31 | |
| 32 | using android::base::StringPrintf; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace vold { |
| 36 | namespace f2fs { |
| 37 | |
| 38 | static const char* kMkfsPath = "/system/bin/make_f2fs"; |
| 39 | static const char* kFsckPath = "/system/bin/fsck.f2fs"; |
| 40 | |
| 41 | bool IsSupported() { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 42 | return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 && |
| 43 | IsFilesystemSupported("f2fs"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Michael Bestas | 86918bf | 2015-12-06 23:53:55 +0200 | [diff] [blame] | 46 | status_t Check(const std::string& source, bool trusted) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 47 | std::vector<std::string> cmd; |
| 48 | cmd.push_back(kFsckPath); |
Yusuke Sato | 0765cf9 | 2015-07-21 15:47:29 -0700 | [diff] [blame] | 49 | cmd.push_back("-a"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 50 | cmd.push_back(source); |
| 51 | |
Michael Bestas | 86918bf | 2015-12-06 23:53:55 +0200 | [diff] [blame] | 52 | return ForkExecvp(cmd, nullptr, trusted ? sFsckContext : sFsckUntrustedContext); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Sam Mortimer | 09aef34 | 2015-11-27 15:27:03 -0800 | [diff] [blame] | 55 | status_t Mount(const std::string& source, const std::string& target, |
| 56 | const std::string& opts /* = "" */, bool trusted, bool portable) { |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 57 | std::string data(opts); |
| 58 | |
| 59 | if (portable) { |
| 60 | if (!data.empty()) { |
| 61 | data += ","; |
| 62 | } |
| 63 | data += "context=u:object_r:sdcard_posix:s0"; |
| 64 | } |
| 65 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 66 | const char* c_source = source.c_str(); |
| 67 | const char* c_target = target.c_str(); |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 68 | const char* c_data = data.c_str(); |
Sam Mortimer | 09aef34 | 2015-11-27 15:27:03 -0800 | [diff] [blame] | 69 | |
| 70 | unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID; |
| 71 | |
| 72 | // Only use MS_DIRSYNC if we're not mounting adopted storage |
| 73 | if (!trusted) { |
| 74 | flags |= MS_DIRSYNC; |
| 75 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 76 | |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 77 | int res = mount(c_source, c_target, "f2fs", flags, c_data); |
| 78 | if (portable && res == 0) { |
| 79 | chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW); |
Jani Lusikka | bf60c5f | 2016-03-25 20:12:15 +0200 | [diff] [blame] | 80 | chmod(c_target, 0775); |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 83 | if (res != 0) { |
| 84 | PLOG(ERROR) << "Failed to mount " << source; |
| 85 | if (errno == EROFS) { |
Jani Lusikka | c8d267d | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 86 | res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, c_data); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 87 | if (res != 0) { |
| 88 | PLOG(ERROR) << "Failed to mount read-only " << source; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return res; |
| 94 | } |
| 95 | |
| 96 | status_t Format(const std::string& source) { |
| 97 | std::vector<std::string> cmd; |
| 98 | cmd.push_back(kMkfsPath); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 99 | |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 100 | cmd.push_back("-f"); |
| 101 | cmd.push_back("-d1"); |
| 102 | |
| 103 | if (android::base::GetBoolProperty("vold.has_quota", false)) { |
| 104 | cmd.push_back("-O"); |
| 105 | cmd.push_back("quota"); |
| 106 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 107 | if (fscrypt_is_native()) { |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 108 | cmd.push_back("-O"); |
| 109 | cmd.push_back("encrypt"); |
| 110 | } |
Jaegeuk Kim | 7db02ab | 2018-04-05 22:43:25 -0700 | [diff] [blame] | 111 | |
| 112 | cmd.push_back("-O"); |
| 113 | cmd.push_back("verity"); |
| 114 | |
Jaegeuk Kim | c7c477b | 2017-11-13 17:38:39 -0800 | [diff] [blame] | 115 | cmd.push_back(source); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 116 | return ForkExecvp(cmd); |
| 117 | } |
| 118 | |
| 119 | } // namespace f2fs |
| 120 | } // namespace vold |
| 121 | } // namespace android |