blob: 018fbeb44838a09d921626bcb4e6c36f4b7f0ae7 [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
17#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080018#include <stdlib.h>
San Mehatbf041852010-01-04 10:09:16 -080019#include <fcntl.h>
20#include <unistd.h>
21#include <errno.h>
22#include <string.h>
23#include <dirent.h>
24#include <errno.h>
25#include <fcntl.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/mman.h>
31#include <sys/mount.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080032#include <sys/wait.h>
Ken Sumrall9caab762013-06-11 19:10:20 -070033#include <linux/fs.h>
34#include <sys/ioctl.h>
San Mehatbf041852010-01-04 10:09:16 -080035
36#include <linux/kdev_t.h>
37
38#define LOG_TAG "Vold"
39
40#include <cutils/log.h>
41#include <cutils/properties.h>
42
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080043#include <logwrap/logwrap.h>
44
San Mehatbf041852010-01-04 10:09:16 -080045#include "Fat.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080046#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080047
48static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos";
49static char MKDOSFS_PATH[] = "/system/bin/newfs_msdos";
San Mehatbf041852010-01-04 10:09:16 -080050extern "C" int mount(const char *, const char *, const char *, unsigned long, const void *);
51
52int Fat::check(const char *fsPath) {
San Mehatbf041852010-01-04 10:09:16 -080053 if (access(FSCK_MSDOS_PATH, X_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -070054 SLOGW("Skipping fs checks\n");
San Mehatbf041852010-01-04 10:09:16 -080055 return 0;
56 }
57
58 int pass = 1;
59 int rc = 0;
60 do {
Rom Lemarchand5593c852012-12-21 12:41:43 -080061 const char *args[4];
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080062 int status;
San Mehatbf041852010-01-04 10:09:16 -080063 args[0] = FSCK_MSDOS_PATH;
64 args[1] = "-p";
65 args[2] = "-f";
66 args[3] = fsPath;
San Mehatbf041852010-01-04 10:09:16 -080067
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080068 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status,
69 false, true);
70 if (rc != 0) {
71 SLOGE("Filesystem check failed due to logwrap error");
72 errno = EIO;
73 return -1;
74 }
San Mehatbf041852010-01-04 10:09:16 -080075
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080076 if (!WIFEXITED(status)) {
77 SLOGE("Filesystem check did not exit properly");
78 errno = EIO;
79 return -1;
80 }
81
82 status = WEXITSTATUS(status);
83
84 switch(status) {
San Mehatbf041852010-01-04 10:09:16 -080085 case 0:
San Mehat97ac40e2010-03-24 10:24:19 -070086 SLOGI("Filesystem check completed OK");
San Mehatbf041852010-01-04 10:09:16 -080087 return 0;
88
89 case 2:
San Mehat97ac40e2010-03-24 10:24:19 -070090 SLOGE("Filesystem check failed (not a FAT filesystem)");
San Mehatbf041852010-01-04 10:09:16 -080091 errno = ENODATA;
92 return -1;
93
94 case 4:
95 if (pass++ <= 3) {
San Mehat97ac40e2010-03-24 10:24:19 -070096 SLOGW("Filesystem modified - rechecking (pass %d)",
San Mehatbf041852010-01-04 10:09:16 -080097 pass);
98 continue;
99 }
San Mehat97ac40e2010-03-24 10:24:19 -0700100 SLOGE("Failing check after too many rechecks");
San Mehatbf041852010-01-04 10:09:16 -0800101 errno = EIO;
102 return -1;
103
104 default:
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800105 SLOGE("Filesystem check failed (unknown exit code %d)", status);
San Mehatbf041852010-01-04 10:09:16 -0800106 errno = EIO;
107 return -1;
108 }
109 } while (0);
110
111 return 0;
112}
113
San Mehatfff0b472010-01-06 19:19:46 -0800114int Fat::doMount(const char *fsPath, const char *mountPoint,
Kenny Roota3e06082010-08-27 08:31:35 -0700115 bool ro, bool remount, bool executable,
116 int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800117 int rc;
118 unsigned long flags;
San Mehatfff0b472010-01-06 19:19:46 -0800119 char mountData[255];
San Mehatbf041852010-01-04 10:09:16 -0800120
Kenny Roota3e06082010-08-27 08:31:35 -0700121 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
San Mehatbf041852010-01-04 10:09:16 -0800122
Kenny Roota3e06082010-08-27 08:31:35 -0700123 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800124 flags |= (ro ? MS_RDONLY : 0);
125 flags |= (remount ? MS_REMOUNT : 0);
126
San Mehatbf041852010-01-04 10:09:16 -0800127 /*
128 * Note: This is a temporary hack. If the sampling profiler is enabled,
129 * we make the SD card world-writable so any process can write snapshots.
130 *
131 * TODO: Remove this code once we have a drop box in system_server.
132 */
133 char value[PROPERTY_VALUE_MAX];
134 property_get("persist.sampling_profiler", value, "");
135 if (value[0] == '1') {
San Mehat97ac40e2010-03-24 10:24:19 -0700136 SLOGW("The SD card is world-writable because the"
San Mehatbf041852010-01-04 10:09:16 -0800137 " 'persist.sampling_profiler' system property is set to '1'.");
San Mehatfff0b472010-01-06 19:19:46 -0800138 permMask = 0;
San Mehatbf041852010-01-04 10:09:16 -0800139 }
140
San Mehatfff0b472010-01-06 19:19:46 -0800141 sprintf(mountData,
142 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
143 ownerUid, ownerGid, permMask, permMask);
144
145 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
146
San Mehatbf041852010-01-04 10:09:16 -0800147 if (rc && errno == EROFS) {
San Mehat97ac40e2010-03-24 10:24:19 -0700148 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
San Mehatbf041852010-01-04 10:09:16 -0800149 flags |= MS_RDONLY;
San Mehatfff0b472010-01-06 19:19:46 -0800150 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
San Mehatbf041852010-01-04 10:09:16 -0800151 }
152
San Mehatfff0b472010-01-06 19:19:46 -0800153 if (rc == 0 && createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800154 char *lost_path;
155 asprintf(&lost_path, "%s/LOST.DIR", mountPoint);
156 if (access(lost_path, F_OK)) {
157 /*
158 * Create a LOST.DIR in the root so we have somewhere to put
159 * lost cluster chains (fsck_msdos doesn't currently do this)
160 */
161 if (mkdir(lost_path, 0755)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700162 SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
San Mehatbf041852010-01-04 10:09:16 -0800163 }
164 }
165 free(lost_path);
166 }
167
168 return rc;
169}
170
Ken Sumrall9caab762013-06-11 19:10:20 -0700171int Fat::format(const char *fsPath, unsigned int numSectors, bool wipe) {
Daniel Rosenberge068a492014-06-26 15:35:24 -0700172 const char *args[11];
San Mehatbf041852010-01-04 10:09:16 -0800173 int rc;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800174 int status;
San Mehatfcf24fe2010-03-03 12:37:32 -0800175
Ken Sumrall9caab762013-06-11 19:10:20 -0700176 if (wipe) {
177 Fat::wipe(fsPath, numSectors);
178 }
179
San Mehatbf041852010-01-04 10:09:16 -0800180 args[0] = MKDOSFS_PATH;
181 args[1] = "-F";
San Mehat8d934ca2010-01-09 12:23:47 -0800182 args[2] = "32";
San Mehatbf041852010-01-04 10:09:16 -0800183 args[3] = "-O";
184 args[4] = "android";
San Mehatb9aed742010-02-04 15:07:01 -0800185 args[5] = "-c";
Daniel Rosenberge068a492014-06-26 15:35:24 -0700186 args[6] = "64";
187 args[7] = "-A";
San Mehatfcf24fe2010-03-03 12:37:32 -0800188
189 if (numSectors) {
190 char tmp[32];
191 snprintf(tmp, sizeof(tmp), "%u", numSectors);
192 const char *size = tmp;
Daniel Rosenberge068a492014-06-26 15:35:24 -0700193 args[8] = "-s";
194 args[9] = size;
195 args[10] = fsPath;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800196 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status,
197 false, true);
San Mehatfcf24fe2010-03-03 12:37:32 -0800198 } else {
Daniel Rosenberge068a492014-06-26 15:35:24 -0700199 args[8] = fsPath;
200 rc = android_fork_execvp(9, (char **)args, &status, false,
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800201 true);
San Mehatfcf24fe2010-03-03 12:37:32 -0800202 }
San Mehatbf041852010-01-04 10:09:16 -0800203
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800204 if (rc != 0) {
205 SLOGE("Filesystem format failed due to logwrap error");
206 errno = EIO;
207 return -1;
208 }
209
210 if (!WIFEXITED(status)) {
211 SLOGE("Filesystem format did not exit properly");
212 errno = EIO;
213 return -1;
214 }
215
216 status = WEXITSTATUS(status);
217
218 if (status == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700219 SLOGI("Filesystem formatted OK");
San Mehatbf041852010-01-04 10:09:16 -0800220 return 0;
221 } else {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800222 SLOGE("Format failed (unknown exit code %d)", status);
San Mehatbf041852010-01-04 10:09:16 -0800223 errno = EIO;
224 return -1;
225 }
226 return 0;
227}
Ken Sumrall9caab762013-06-11 19:10:20 -0700228
229void Fat::wipe(const char *fsPath, unsigned int numSectors) {
Ken Sumrall9caab762013-06-11 19:10:20 -0700230 unsigned long long range[2];
231
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900232 int fd = open(fsPath, O_RDWR);
233 if (fd == -1) {
234 SLOGE("Fat wipe failed to open device %s", fsPath);
235 return;
236 }
237
238 if (numSectors == 0) {
239 unsigned long nr_sec;
240 get_blkdev_size(fd, &nr_sec);
241 if (nr_sec > UINT32_MAX) {
242 SLOGE("Too many sectors for FAT: %ld", nr_sec);
Ken Sumrall9caab762013-06-11 19:10:20 -0700243 close(fd);
244 return;
245 }
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900246 numSectors = nr_sec;
Ken Sumrall9caab762013-06-11 19:10:20 -0700247 }
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900248 if (numSectors == 0) {
249 SLOGE("Fat wipe failed to determine size of %s", fsPath);
250 close(fd);
251 return;
252 }
253 range[0] = 0;
254 range[1] = (unsigned long long)numSectors * 512;
255 if (ioctl(fd, BLKDISCARD, &range) < 0) {
256 SLOGE("Fat wipe failed to discard blocks on %s", fsPath);
257 } else {
258 SLOGI("Fat wipe %d sectors on %s succeeded", numSectors, fsPath);
259 }
260 close(fd);
Ken Sumrall9caab762013-06-11 19:10:20 -0700261}