blob: 3bab02f90b1f3152e030bd4a97afc5c39c96093a [file] [log] [blame]
San Mehatbf041852010-01-04 10:09:16 -08001/*
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 Mehatbf041852010-01-04 10:09:16 -080017#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
Neil Fuller6eea0312024-01-09 12:45:42 +000023#include <time.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070024#include <unistd.h>
San Mehatbf041852010-01-04 10:09:16 -080025
Ken Sumrall9caab762013-06-11 19:10:20 -070026#include <linux/fs.h>
27#include <sys/ioctl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070028#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 Mehatbf041852010-01-04 10:09:16 -080033
34#include <linux/kdev_t.h>
35
Elliott Hughes7e128fb2015-12-04 15:50:53 -080036#include <android-base/logging.h>
37#include <android-base/stringprintf.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070038#include <selinux/selinux.h>
San Mehatbf041852010-01-04 10:09:16 -080039
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080040#include <logwrap/logwrap.h>
41
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070042#include "Utils.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070043#include "Vfat.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080044#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080045
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070046using android::base::StringPrintf;
47
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070048namespace android {
49namespace vold {
50namespace vfat {
51
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070052static const char* kMkfsPath = "/system/bin/newfs_msdos";
53static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080054
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070055bool IsSupported() {
Paul Crowley14c8c072018-09-18 13:30:21 -070056 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
57 IsFilesystemSupported("vfat");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070058}
59
60status_t Check(const std::string& source) {
San Mehatbf041852010-01-04 10:09:16 -080061 int pass = 1;
62 int rc = 0;
63 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070064 std::vector<std::string> cmd;
65 cmd.push_back(kFsckPath);
66 cmd.push_back("-p");
67 cmd.push_back("-f");
Xin Li3d3a9a72019-06-06 11:33:51 -070068 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070069 cmd.push_back(source);
San Mehatbf041852010-01-04 10:09:16 -080070
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070071 // Fat devices are currently always untrusted
Daniel Rosenbergd9261b12021-09-14 17:32:06 -070072 rc = ForkExecvpTimeout(cmd, kUntrustedFsckSleepTime, sFsckUntrustedContext);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073 if (rc < 0) {
Daniel Rosenbergd9261b12021-09-14 17:32:06 -070074 LOG(ERROR) << "Filesystem check failed due to fork error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080075 errno = EIO;
76 return -1;
77 }
San Mehatbf041852010-01-04 10:09:16 -080078
Paul Crowley14c8c072018-09-18 13:30:21 -070079 switch (rc) {
80 case 0:
81 LOG(INFO) << "Filesystem check completed OK";
82 return 0;
San Mehatbf041852010-01-04 10:09:16 -080083
Daniel Rosenbergd9261b12021-09-14 17:32:06 -070084 case 1:
85 LOG(INFO) << "Failed to check filesystem";
86 return -1;
87
Paul Crowley14c8c072018-09-18 13:30:21 -070088 case 2:
89 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
90 errno = ENODATA;
91 return -1;
San Mehatbf041852010-01-04 10:09:16 -080092
Paul Crowley14c8c072018-09-18 13:30:21 -070093 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 Mehatbf041852010-01-04 10:09:16 -0800101
Paul Crowley14c8c072018-09-18 13:30:21 -0700102 case 8:
103 LOG(ERROR) << "Filesystem check failed (no filesystem)";
104 errno = ENODATA;
105 return -1;
Mateusz Nowak3dd39302015-08-03 15:48:52 +0200106
Daniel Rosenbergd9261b12021-09-14 17:32:06 -0700107 case ETIMEDOUT:
108 LOG(ERROR) << "Filesystem check timed out";
109 errno = ETIMEDOUT;
110 return -1;
111
Paul Crowley14c8c072018-09-18 13:30:21 -0700112 default:
113 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
114 errno = EIO;
115 return -1;
San Mehatbf041852010-01-04 10:09:16 -0800116 }
Daniel Rosenberg92d0c2e2024-05-13 14:33:31 -0700117 } while (1);
San Mehatbf041852010-01-04 10:09:16 -0800118
119 return 0;
120}
121
Neil Fuller6eea0312024-01-09 12:45:42 +0000122int16_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 Rosenberg8c5dd6e2024-07-18 18:10:39 -0700132status_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 Mehatbf041852010-01-04 10:09:16 -0800134 int rc;
135 unsigned long flags;
136
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700137 const char* c_source = source.c_str();
138 const char* c_target = target.c_str();
139
Ravisankar Reddy4cc6baf2017-07-03 11:25:33 +0900140 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
San Mehatbf041852010-01-04 10:09:16 -0800141
Kenny Roota3e06082010-08-27 08:31:35 -0700142 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800143 flags |= (ro ? MS_RDONLY : 0);
144 flags |= (remount ? MS_REMOUNT : 0);
145
Paul Crowley14c8c072018-09-18 13:30:21 -0700146 auto mountData =
147 android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
148 ownerUid, ownerGid, permMask, permMask);
San Mehatfff0b472010-01-06 19:19:46 -0800149
Neil Fuller6eea0312024-01-09 12:45:42 +0000150 // 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 Sharkey3472e522017-10-06 18:02:53 -0600177 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatfff0b472010-01-06 19:19:46 -0800178
San Mehatbf041852010-01-04 10:09:16 -0800179 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600180 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
San Mehatbf041852010-01-04 10:09:16 -0800181 flags |= MS_RDONLY;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600182 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatbf041852010-01-04 10:09:16 -0800183 }
184
San Mehatfff0b472010-01-06 19:19:46 -0800185 if (rc == 0 && createLost) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600186 auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
187 if (access(lost_path.c_str(), F_OK)) {
San Mehatbf041852010-01-04 10:09:16 -0800188 /*
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 Sharkey3472e522017-10-06 18:02:53 -0600192 if (mkdir(lost_path.c_str(), 0755)) {
193 PLOG(ERROR) << "Unable to create LOST.DIR";
San Mehatbf041852010-01-04 10:09:16 -0800194 }
195 }
San Mehatbf041852010-01-04 10:09:16 -0800196 }
197
198 return rc;
199}
200
Daniel Rosenberg8c5dd6e2024-07-18 18:10:39 -0700201struct 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
213int 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
220status_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 Nowaka4f48d02015-08-03 18:06:39 +0200227status_t Format(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700228 std::vector<std::string> cmd;
229 cmd.push_back(kMkfsPath);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700230 cmd.push_back("-O");
231 cmd.push_back("android");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700232 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800233
234 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700235 cmd.push_back("-s");
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200236 cmd.push_back(StringPrintf("%lu", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800237 }
San Mehatbf041852010-01-04 10:09:16 -0800238
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700239 cmd.push_back(source);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700240
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700241 int rc = ForkExecvp(cmd);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700242 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600243 LOG(ERROR) << "Filesystem format failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800244 errno = EIO;
245 return -1;
246 }
247
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700248 if (rc == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600249 LOG(INFO) << "Filesystem formatted OK";
San Mehatbf041852010-01-04 10:09:16 -0800250 return 0;
251 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600252 LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
San Mehatbf041852010-01-04 10:09:16 -0800253 errno = EIO;
254 return -1;
255 }
256 return 0;
257}
Ken Sumrall9caab762013-06-11 19:10:20 -0700258
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700259} // namespace vfat
260} // namespace vold
261} // namespace android