San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Neil Fuller | 6eea031 | 2024-01-09 12:45:42 +0000 | [diff] [blame] | 23 | #include <time.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 25 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 26 | #include <linux/fs.h> |
| 27 | #include <sys/ioctl.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 28 | #include <sys/mman.h> |
| 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 33 | |
| 34 | #include <linux/kdev_t.h> |
| 35 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 36 | #include <android-base/logging.h> |
| 37 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 38 | #include <selinux/selinux.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 39 | |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 40 | #include <logwrap/logwrap.h> |
| 41 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 42 | #include "Utils.h" |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 43 | #include "Vfat.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 44 | #include "VoldUtil.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 45 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 46 | using android::base::StringPrintf; |
| 47 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 48 | namespace android { |
| 49 | namespace vold { |
| 50 | namespace vfat { |
| 51 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 52 | static const char* kMkfsPath = "/system/bin/newfs_msdos"; |
| 53 | static const char* kFsckPath = "/system/bin/fsck_msdos"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 54 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 55 | bool IsSupported() { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 56 | return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 && |
| 57 | IsFilesystemSupported("vfat"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | status_t Check(const std::string& source) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 61 | int pass = 1; |
| 62 | int rc = 0; |
| 63 | do { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 64 | std::vector<std::string> cmd; |
| 65 | cmd.push_back(kFsckPath); |
| 66 | cmd.push_back("-p"); |
| 67 | cmd.push_back("-f"); |
Xin Li | 3d3a9a7 | 2019-06-06 11:33:51 -0700 | [diff] [blame] | 68 | cmd.push_back("-y"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 69 | cmd.push_back(source); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 70 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 71 | // Fat devices are currently always untrusted |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 72 | rc = ForkExecvpTimeout(cmd, kUntrustedFsckSleepTime, sFsckUntrustedContext); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 73 | if (rc < 0) { |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 74 | LOG(ERROR) << "Filesystem check failed due to fork error"; |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 75 | errno = EIO; |
| 76 | return -1; |
| 77 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 78 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 79 | switch (rc) { |
| 80 | case 0: |
| 81 | LOG(INFO) << "Filesystem check completed OK"; |
| 82 | return 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 83 | |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 84 | case 1: |
| 85 | LOG(INFO) << "Failed to check filesystem"; |
| 86 | return -1; |
| 87 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 88 | case 2: |
| 89 | LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)"; |
| 90 | errno = ENODATA; |
| 91 | return -1; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 92 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 93 | case 4: |
| 94 | if (pass++ <= 3) { |
| 95 | LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")"; |
| 96 | continue; |
| 97 | } |
| 98 | LOG(ERROR) << "Failing check after too many rechecks"; |
| 99 | errno = EIO; |
| 100 | return -1; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 101 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 102 | case 8: |
| 103 | LOG(ERROR) << "Filesystem check failed (no filesystem)"; |
| 104 | errno = ENODATA; |
| 105 | return -1; |
Mateusz Nowak | 3dd3930 | 2015-08-03 15:48:52 +0200 | [diff] [blame] | 106 | |
Daniel Rosenberg | d9261b1 | 2021-09-14 17:32:06 -0700 | [diff] [blame] | 107 | case ETIMEDOUT: |
| 108 | LOG(ERROR) << "Filesystem check timed out"; |
| 109 | errno = ETIMEDOUT; |
| 110 | return -1; |
| 111 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 112 | default: |
| 113 | LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")"; |
| 114 | errno = EIO; |
| 115 | return -1; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 116 | } |
Daniel Rosenberg | 92d0c2e | 2024-05-13 14:33:31 -0700 | [diff] [blame] | 117 | } while (1); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Neil Fuller | 6eea031 | 2024-01-09 12:45:42 +0000 | [diff] [blame] | 122 | int16_t currentUtcOffsetMinutes() { |
| 123 | time_t now = time(NULL); |
| 124 | |
| 125 | struct tm nowTm; |
| 126 | localtime_r(&now, &nowTm); |
| 127 | |
| 128 | int32_t utcOffsetSeconds = nowTm.tm_gmtoff; |
| 129 | return (int16_t)(utcOffsetSeconds / 60); |
| 130 | } |
| 131 | |
Daniel Rosenberg | 8c5dd6e | 2024-07-18 18:10:39 -0700 | [diff] [blame] | 132 | status_t DoMount(const std::string& source, const std::string& target, bool ro, bool remount, |
| 133 | bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 134 | int rc; |
| 135 | unsigned long flags; |
| 136 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 137 | const char* c_source = source.c_str(); |
| 138 | const char* c_target = target.c_str(); |
| 139 | |
Ravisankar Reddy | 4cc6baf | 2017-07-03 11:25:33 +0900 | [diff] [blame] | 140 | flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 141 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 142 | flags |= (executable ? 0 : MS_NOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 143 | flags |= (ro ? MS_RDONLY : 0); |
| 144 | flags |= (remount ? MS_REMOUNT : 0); |
| 145 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 146 | auto mountData = |
| 147 | android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed", |
| 148 | ownerUid, ownerGid, permMask, permMask); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 149 | |
Neil Fuller | 6eea031 | 2024-01-09 12:45:42 +0000 | [diff] [blame] | 150 | // b/315058275: Set this to false if you don't want to use a fixed offset |
| 151 | // determined at mount time. When this is false, the vfat driver will fall |
| 152 | // back to using sys_tz, which Android does not set by default, then assume |
| 153 | // local time == UTC. |
| 154 | if (true) { |
| 155 | // Calculate the offset to use to adjust FAT timestamps to convert them |
| 156 | // from "local time" into unix epoch time. This assumes the current UTC |
| 157 | // offset of this device is the same as the device that wrote them. User |
| 158 | // space code, e.g. ls -l, will then apply the UTC offset for the UTC |
| 159 | // time to convert times from unix epoch time to local time for display. |
| 160 | // Before Android U (b/246256335), Android platform code informed the |
| 161 | // Linux kernel about the UTC offset under some circumstances, but not |
| 162 | // for all, e.g. DST changes. The kernel vfat driver is one of the few |
| 163 | // things in the kernel that tries to use kernel UTC offset information. |
| 164 | // Setting time zone offset in the Linux kernel is discouraged and so |
| 165 | // Android no longer informs the kernel. Instead, the offset for vfat |
| 166 | // to use is now set at volume mount time. This means that if the time |
| 167 | // zone offset changes while the device is mounted, or if files were |
| 168 | // written in opposing daylight saving time, then incorrect file times |
| 169 | // will be displayed until the volume is remounted. Even then, the vfat |
| 170 | // driver has to assume a fixed offset to apply to all files, so files |
| 171 | // written at different times of the year can have incorrect times |
| 172 | // calculated, e.g. offset incorrectly by one hour. |
| 173 | int16_t timeOffsetArg = currentUtcOffsetMinutes(); |
| 174 | mountData += android::base::StringPrintf(",time_offset=%d", timeOffsetArg); |
| 175 | } |
| 176 | |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 177 | rc = mount(c_source, c_target, "vfat", flags, mountData.c_str()); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 178 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 179 | if (rc && errno == EROFS) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 180 | LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 181 | flags |= MS_RDONLY; |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 182 | rc = mount(c_source, c_target, "vfat", flags, mountData.c_str()); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 183 | } |
| 184 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 185 | if (rc == 0 && createLost) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 186 | auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str()); |
| 187 | if (access(lost_path.c_str(), F_OK)) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 188 | /* |
| 189 | * Create a LOST.DIR in the root so we have somewhere to put |
| 190 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 191 | */ |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 192 | if (mkdir(lost_path.c_str(), 0755)) { |
| 193 | PLOG(ERROR) << "Unable to create LOST.DIR"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return rc; |
| 199 | } |
| 200 | |
Daniel Rosenberg | 8c5dd6e | 2024-07-18 18:10:39 -0700 | [diff] [blame] | 201 | struct mount_args { |
| 202 | const std::string& source; |
| 203 | const std::string& target; |
| 204 | bool ro; |
| 205 | bool remount; |
| 206 | bool executable; |
| 207 | int ownerUid; |
| 208 | int ownerGid; |
| 209 | int permMask; |
| 210 | bool createLost; |
| 211 | }; |
| 212 | |
| 213 | int DoMountWrapper(void* args) { |
| 214 | struct mount_args* m_args = (struct mount_args*)args; |
| 215 | |
| 216 | return DoMount(m_args->source, m_args->target, m_args->ro, m_args->remount, m_args->executable, |
| 217 | m_args->ownerUid, m_args->ownerGid, m_args->permMask, m_args->createLost); |
| 218 | } |
| 219 | |
| 220 | status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount, |
| 221 | bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) { |
| 222 | struct mount_args args = {source, target, ro, remount, executable, |
| 223 | ownerUid, ownerGid, permMask, createLost}; |
| 224 | return ForkTimeout(DoMountWrapper, &args, kUntrustedMountSleepTime); |
| 225 | } |
| 226 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 227 | status_t Format(const std::string& source, unsigned long numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 228 | std::vector<std::string> cmd; |
| 229 | cmd.push_back(kMkfsPath); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 230 | cmd.push_back("-O"); |
| 231 | cmd.push_back("android"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 232 | cmd.push_back("-A"); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 233 | |
| 234 | if (numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 235 | cmd.push_back("-s"); |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 236 | cmd.push_back(StringPrintf("%lu", numSectors)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 237 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 238 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 239 | cmd.push_back(source); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 240 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 241 | int rc = ForkExecvp(cmd); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 242 | if (rc < 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 243 | LOG(ERROR) << "Filesystem format failed due to logwrap error"; |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 244 | errno = EIO; |
| 245 | return -1; |
| 246 | } |
| 247 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 248 | if (rc == 0) { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 249 | LOG(INFO) << "Filesystem formatted OK"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 250 | return 0; |
| 251 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 252 | LOG(ERROR) << "Format failed (unknown exit code " << rc << ")"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 253 | errno = EIO; |
| 254 | return -1; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 258 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 259 | } // namespace vfat |
| 260 | } // namespace vold |
| 261 | } // namespace android |