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