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> |
| 21 | #include <android-base/stringprintf.h> |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 22 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | #include <string> |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 25 | #include <sys/stat.h> |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 26 | #include <sys/mount.h> |
| 27 | |
| 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vold { |
| 32 | namespace f2fs { |
| 33 | |
| 34 | static const char* kMkfsPath = "/system/bin/make_f2fs"; |
| 35 | static const char* kFsckPath = "/system/bin/fsck.f2fs"; |
| 36 | |
| 37 | bool IsSupported() { |
| 38 | return access(kMkfsPath, X_OK) == 0 |
| 39 | && access(kFsckPath, X_OK) == 0 |
| 40 | && IsFilesystemSupported("f2fs"); |
| 41 | } |
| 42 | |
Michael Bestas | 7e0bc98 | 2015-12-06 23:53:55 +0200 | [diff] [blame] | 43 | status_t Check(const std::string& source, bool trusted) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 44 | std::vector<std::string> cmd; |
| 45 | cmd.push_back(kFsckPath); |
Yusuke Sato | 0765cf9 | 2015-07-21 15:47:29 -0700 | [diff] [blame] | 46 | cmd.push_back("-a"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 47 | cmd.push_back(source); |
| 48 | |
Michael Bestas | 7e0bc98 | 2015-12-06 23:53:55 +0200 | [diff] [blame] | 49 | return ForkExecvp(cmd, trusted ? sFsckContext : sFsckUntrustedContext); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Sam Mortimer | 11a9e06 | 2015-11-27 15:27:03 -0800 | [diff] [blame] | 52 | status_t Mount(const std::string& source, const std::string& target, |
| 53 | const std::string& opts /* = "" */, bool trusted, bool portable) { |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 54 | std::string data(opts); |
| 55 | |
| 56 | if (portable) { |
| 57 | if (!data.empty()) { |
| 58 | data += ","; |
| 59 | } |
| 60 | data += "context=u:object_r:sdcard_posix:s0"; |
| 61 | } |
| 62 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 63 | const char* c_source = source.c_str(); |
| 64 | const char* c_target = target.c_str(); |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 65 | const char* c_data = data.c_str(); |
Sam Mortimer | 11a9e06 | 2015-11-27 15:27:03 -0800 | [diff] [blame] | 66 | |
| 67 | unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID; |
| 68 | |
| 69 | // Only use MS_DIRSYNC if we're not mounting adopted storage |
| 70 | if (!trusted) { |
| 71 | flags |= MS_DIRSYNC; |
| 72 | } |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 73 | |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 74 | int res = mount(c_source, c_target, "f2fs", flags, c_data); |
| 75 | if (portable && res == 0) { |
| 76 | chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW); |
Jani Lusikka | 63a4f9c | 2016-03-25 20:12:15 +0200 | [diff] [blame] | 77 | chmod(c_target, 0775); |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 80 | if (res != 0) { |
| 81 | PLOG(ERROR) << "Failed to mount " << source; |
| 82 | if (errno == EROFS) { |
Jani Lusikka | f95af8c | 2016-01-15 22:25:47 +0200 | [diff] [blame] | 83 | res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, c_data); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 84 | if (res != 0) { |
| 85 | PLOG(ERROR) << "Failed to mount read-only " << source; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return res; |
| 91 | } |
| 92 | |
| 93 | status_t Format(const std::string& source) { |
| 94 | std::vector<std::string> cmd; |
| 95 | cmd.push_back(kMkfsPath); |
| 96 | cmd.push_back(source); |
| 97 | |
| 98 | return ForkExecvp(cmd); |
| 99 | } |
| 100 | |
| 101 | } // namespace f2fs |
| 102 | } // namespace vold |
| 103 | } // namespace android |