Alfred Piccioni | 564f6c6 | 2022-09-21 17:32:04 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <linux/fs.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/mount.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
| 32 | |
| 33 | #include <linux/kdev_t.h> |
| 34 | |
| 35 | #define LOG_TAG "Vold" |
| 36 | |
| 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/stringprintf.h> |
| 39 | #include <cutils/log.h> |
| 40 | #include <cutils/properties.h> |
| 41 | #include <selinux/selinux.h> |
| 42 | |
| 43 | #include <logwrap/logwrap.h> |
| 44 | |
| 45 | #include "Ntfs.h" |
| 46 | #include "Utils.h" |
| 47 | #include "VoldUtil.h" |
| 48 | |
| 49 | using android::base::StringPrintf; |
| 50 | |
| 51 | namespace android { |
| 52 | namespace vold { |
| 53 | namespace ntfs { |
| 54 | |
| 55 | static const char* kFsckPath = "/system/bin/ntfsfix"; |
| 56 | static const char* kMkfsPath = "/system/bin/mkntfs"; |
| 57 | |
| 58 | static const char* fsName = "ntfs3"; |
| 59 | |
| 60 | bool IsSupported() { |
| 61 | return access(kFsckPath, X_OK) == 0 && access(kMkfsPath, X_OK) == 0 && |
| 62 | IsFilesystemSupported(fsName); |
| 63 | } |
| 64 | |
| 65 | status_t Check(const std::string& source) { |
| 66 | std::vector<std::string> cmd; |
| 67 | cmd.push_back(kFsckPath); |
| 68 | |
| 69 | // ntfsfix sets the dirty bit by default, which prevents mounting the drive. |
| 70 | // -d tells it to instead reset the dirty bit. Technically, this could be dangerous, |
| 71 | // but since ntfsfix should report any errors with the drive and separately return |
| 72 | // a failed check, this should be relatively safe. |
| 73 | cmd.push_back("-d"); |
| 74 | |
| 75 | cmd.push_back(source); |
| 76 | |
| 77 | int rc = ForkExecvpTimeout(cmd, kUntrustedFsckSleepTime, sFsckUntrustedContext); |
| 78 | if (rc == 0) { |
| 79 | LOG(INFO) << "Check NTFS OK"; |
| 80 | return 0; |
| 81 | } else { |
| 82 | LOG(ERROR) << "Check NTFS failed (code " << rc << ")"; |
| 83 | errno = EIO; |
| 84 | return -1; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount, |
| 89 | bool executable, int ownerUid, int ownerGid, int permMask) { |
| 90 | unsigned long flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME; |
| 91 | |
| 92 | flags |= (executable ? 0 : MS_NOEXEC); |
| 93 | flags |= (ro ? MS_RDONLY : 0); |
| 94 | flags |= (remount ? MS_REMOUNT : 0); |
| 95 | |
| 96 | // Android mount does not support "utf8" as an option. We use the deprecated iocharset instead. |
| 97 | auto mountData = android::base::StringPrintf("uid=%d,gid=%d,fmask=%o,dmask=%o,iocharset=%s", |
| 98 | ownerUid, ownerGid, permMask, permMask, "utf8"); |
| 99 | |
| 100 | int rc = mount(source.c_str(), target.c_str(), fsName, flags, mountData.c_str()); |
| 101 | |
| 102 | if (rc && errno == EROFS) { |
| 103 | LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO"; |
| 104 | flags |= MS_RDONLY; |
| 105 | rc = mount(source.c_str(), target.c_str(), fsName, flags, mountData.c_str()); |
| 106 | } |
| 107 | |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | status_t Format(const std::string& source, unsigned int numSectors) { |
| 112 | std::vector<std::string> cmd; |
| 113 | cmd.push_back(kMkfsPath); |
| 114 | cmd.push_back(source); |
| 115 | |
| 116 | if (numSectors) { |
| 117 | cmd.push_back(StringPrintf("%u", numSectors)); |
| 118 | } |
| 119 | |
| 120 | int rc = ForkExecvp(cmd); |
| 121 | if (rc == 0) { |
| 122 | LOG(INFO) << "Filesystem formatted OK"; |
| 123 | return 0; |
| 124 | } else { |
| 125 | LOG(ERROR) << "Format failed with error code: " << rc; |
| 126 | errno = EIO; |
| 127 | return -1; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | } // namespace ntfs |
| 133 | } // namespace vold |
| 134 | } // namespace android |