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
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 <time.h>
24 #include <unistd.h>
25
26 #include <linux/fs.h>
27 #include <sys/ioctl.h>
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>
33
34 #include <linux/kdev_t.h>
35
36 #include <android-base/logging.h>
37 #include <android-base/stringprintf.h>
38 #include <selinux/selinux.h>
39
40 #include <logwrap/logwrap.h>
41
42 #include "Utils.h"
43 #include "Vfat.h"
44 #include "VoldUtil.h"
45
46 using android::base::StringPrintf;
47
48 namespace android {
49 namespace vold {
50 namespace vfat {
51
52 static const char* kMkfsPath = "/system/bin/newfs_msdos";
53 static const char* kFsckPath = "/system/bin/fsck_msdos";
54
IsSupported()55 bool IsSupported() {
56 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
57 IsFilesystemSupported("vfat");
58 }
59
Check(const std::string & source)60 status_t Check(const std::string& source) {
61 int pass = 1;
62 int rc = 0;
63 do {
64 std::vector<std::string> cmd;
65 cmd.push_back(kFsckPath);
66 cmd.push_back("-p");
67 cmd.push_back("-f");
68 cmd.push_back("-y");
69 cmd.push_back(source);
70
71 // Fat devices are currently always untrusted
72 rc = ForkExecvpTimeout(cmd, kUntrustedFsckSleepTime, sFsckUntrustedContext);
73 if (rc < 0) {
74 LOG(ERROR) << "Filesystem check failed due to fork error";
75 errno = EIO;
76 return -1;
77 }
78
79 switch (rc) {
80 case 0:
81 LOG(INFO) << "Filesystem check completed OK";
82 return 0;
83
84 case 1:
85 LOG(INFO) << "Failed to check filesystem";
86 return -1;
87
88 case 2:
89 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
90 errno = ENODATA;
91 return -1;
92
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;
101
102 case 8:
103 LOG(ERROR) << "Filesystem check failed (no filesystem)";
104 errno = ENODATA;
105 return -1;
106
107 case ETIMEDOUT:
108 LOG(ERROR) << "Filesystem check timed out";
109 errno = ETIMEDOUT;
110 return -1;
111
112 default:
113 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
114 errno = EIO;
115 return -1;
116 }
117 } while (1);
118
119 return 0;
120 }
121
currentUtcOffsetMinutes()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
Mount(const std::string & source,const std::string & target,bool ro,bool remount,bool executable,int ownerUid,int ownerGid,int permMask,bool createLost)132 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
133 bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
134 int rc;
135 unsigned long flags;
136
137 const char* c_source = source.c_str();
138 const char* c_target = target.c_str();
139
140 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
141
142 flags |= (executable ? 0 : MS_NOEXEC);
143 flags |= (ro ? MS_RDONLY : 0);
144 flags |= (remount ? MS_REMOUNT : 0);
145
146 auto mountData =
147 android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
148 ownerUid, ownerGid, permMask, permMask);
149
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
177 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
178
179 if (rc && errno == EROFS) {
180 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
181 flags |= MS_RDONLY;
182 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
183 }
184
185 if (rc == 0 && createLost) {
186 auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
187 if (access(lost_path.c_str(), F_OK)) {
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 */
192 if (mkdir(lost_path.c_str(), 0755)) {
193 PLOG(ERROR) << "Unable to create LOST.DIR";
194 }
195 }
196 }
197
198 return rc;
199 }
200
Format(const std::string & source,unsigned long numSectors)201 status_t Format(const std::string& source, unsigned long numSectors) {
202 std::vector<std::string> cmd;
203 cmd.push_back(kMkfsPath);
204 cmd.push_back("-O");
205 cmd.push_back("android");
206 cmd.push_back("-A");
207
208 if (numSectors) {
209 cmd.push_back("-s");
210 cmd.push_back(StringPrintf("%lu", numSectors));
211 }
212
213 cmd.push_back(source);
214
215 int rc = ForkExecvp(cmd);
216 if (rc < 0) {
217 LOG(ERROR) << "Filesystem format failed due to logwrap error";
218 errno = EIO;
219 return -1;
220 }
221
222 if (rc == 0) {
223 LOG(INFO) << "Filesystem formatted OK";
224 return 0;
225 } else {
226 LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
227 errno = EIO;
228 return -1;
229 }
230 return 0;
231 }
232
233 } // namespace vfat
234 } // namespace vold
235 } // namespace android
236