blob: 873088da163e031c06761164348690f865026ac3 [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
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070040#include <base/logging.h>
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070041#include <base/stringprintf.h>
San Mehatbf041852010-01-04 10:09:16 -080042#include <cutils/log.h>
43#include <cutils/properties.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070044#include <selinux/selinux.h>
San Mehatbf041852010-01-04 10:09:16 -080045
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080046#include <logwrap/logwrap.h>
47
San Mehatbf041852010-01-04 10:09:16 -080048#include "Fat.h"
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070049#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080050#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080051
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070052using android::base::StringPrintf;
53
54static const char* kMkfsPath = "/system/bin/newfs_msdos";
55static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080056
57int Fat::check(const char *fsPath) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070058 if (access(kFsckPath, X_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -070059 SLOGW("Skipping fs checks\n");
San Mehatbf041852010-01-04 10:09:16 -080060 return 0;
61 }
62
63 int pass = 1;
64 int rc = 0;
65 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070066 std::vector<std::string> cmd;
67 cmd.push_back(kFsckPath);
68 cmd.push_back("-p");
69 cmd.push_back("-f");
70 cmd.push_back(fsPath);
San Mehatbf041852010-01-04 10:09:16 -080071
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072 // Fat devices are currently always untrusted
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073 rc = android::vold::ForkExecvp(cmd, android::vold::sFsckUntrustedContext);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070074
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070075 if (rc < 0) {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080076 SLOGE("Filesystem check failed due to logwrap error");
77 errno = EIO;
78 return -1;
79 }
San Mehatbf041852010-01-04 10:09:16 -080080
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070081 switch(rc) {
San Mehatbf041852010-01-04 10:09:16 -080082 case 0:
San Mehat97ac40e2010-03-24 10:24:19 -070083 SLOGI("Filesystem check completed OK");
San Mehatbf041852010-01-04 10:09:16 -080084 return 0;
85
86 case 2:
San Mehat97ac40e2010-03-24 10:24:19 -070087 SLOGE("Filesystem check failed (not a FAT filesystem)");
San Mehatbf041852010-01-04 10:09:16 -080088 errno = ENODATA;
89 return -1;
90
91 case 4:
92 if (pass++ <= 3) {
San Mehat97ac40e2010-03-24 10:24:19 -070093 SLOGW("Filesystem modified - rechecking (pass %d)",
San Mehatbf041852010-01-04 10:09:16 -080094 pass);
95 continue;
96 }
San Mehat97ac40e2010-03-24 10:24:19 -070097 SLOGE("Failing check after too many rechecks");
San Mehatbf041852010-01-04 10:09:16 -080098 errno = EIO;
99 return -1;
100
101 default:
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700102 SLOGE("Filesystem check failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -0800103 errno = EIO;
104 return -1;
105 }
106 } while (0);
107
108 return 0;
109}
110
San Mehatfff0b472010-01-06 19:19:46 -0800111int Fat::doMount(const char *fsPath, const char *mountPoint,
Kenny Roota3e06082010-08-27 08:31:35 -0700112 bool ro, bool remount, bool executable,
113 int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800114 int rc;
115 unsigned long flags;
San Mehatfff0b472010-01-06 19:19:46 -0800116 char mountData[255];
San Mehatbf041852010-01-04 10:09:16 -0800117
Kenny Roota3e06082010-08-27 08:31:35 -0700118 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
San Mehatbf041852010-01-04 10:09:16 -0800119
Kenny Roota3e06082010-08-27 08:31:35 -0700120 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800121 flags |= (ro ? MS_RDONLY : 0);
122 flags |= (remount ? MS_REMOUNT : 0);
123
San Mehatbf041852010-01-04 10:09:16 -0800124 /*
125 * Note: This is a temporary hack. If the sampling profiler is enabled,
126 * we make the SD card world-writable so any process can write snapshots.
127 *
128 * TODO: Remove this code once we have a drop box in system_server.
129 */
130 char value[PROPERTY_VALUE_MAX];
131 property_get("persist.sampling_profiler", value, "");
132 if (value[0] == '1') {
San Mehat97ac40e2010-03-24 10:24:19 -0700133 SLOGW("The SD card is world-writable because the"
San Mehatbf041852010-01-04 10:09:16 -0800134 " 'persist.sampling_profiler' system property is set to '1'.");
San Mehatfff0b472010-01-06 19:19:46 -0800135 permMask = 0;
San Mehatbf041852010-01-04 10:09:16 -0800136 }
137
San Mehatfff0b472010-01-06 19:19:46 -0800138 sprintf(mountData,
139 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
140 ownerUid, ownerGid, permMask, permMask);
141
142 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
143
San Mehatbf041852010-01-04 10:09:16 -0800144 if (rc && errno == EROFS) {
San Mehat97ac40e2010-03-24 10:24:19 -0700145 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
San Mehatbf041852010-01-04 10:09:16 -0800146 flags |= MS_RDONLY;
San Mehatfff0b472010-01-06 19:19:46 -0800147 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
San Mehatbf041852010-01-04 10:09:16 -0800148 }
149
San Mehatfff0b472010-01-06 19:19:46 -0800150 if (rc == 0 && createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800151 char *lost_path;
152 asprintf(&lost_path, "%s/LOST.DIR", mountPoint);
153 if (access(lost_path, F_OK)) {
154 /*
155 * Create a LOST.DIR in the root so we have somewhere to put
156 * lost cluster chains (fsck_msdos doesn't currently do this)
157 */
158 if (mkdir(lost_path, 0755)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700159 SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
San Mehatbf041852010-01-04 10:09:16 -0800160 }
161 }
162 free(lost_path);
163 }
164
165 return rc;
166}
167
Ken Sumrall9caab762013-06-11 19:10:20 -0700168int Fat::format(const char *fsPath, unsigned int numSectors, bool wipe) {
Ken Sumrall9caab762013-06-11 19:10:20 -0700169 if (wipe) {
170 Fat::wipe(fsPath, numSectors);
171 }
172
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700173 std::vector<std::string> cmd;
174 cmd.push_back(kMkfsPath);
175 cmd.push_back("-F");
176 cmd.push_back("32");
177 cmd.push_back("-O");
178 cmd.push_back("android");
179 cmd.push_back("-c");
180 cmd.push_back("64");
181 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800182
183 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700184 cmd.push_back("-s");
185 cmd.push_back(StringPrintf("%u", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800186 }
San Mehatbf041852010-01-04 10:09:16 -0800187
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700188 cmd.push_back(fsPath);
189
190 int rc = android::vold::ForkExecvp(cmd);
191 if (rc < 0) {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800192 SLOGE("Filesystem format failed due to logwrap error");
193 errno = EIO;
194 return -1;
195 }
196
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700197 if (rc == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700198 SLOGI("Filesystem formatted OK");
San Mehatbf041852010-01-04 10:09:16 -0800199 return 0;
200 } else {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700201 SLOGE("Format failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -0800202 errno = EIO;
203 return -1;
204 }
205 return 0;
206}
Ken Sumrall9caab762013-06-11 19:10:20 -0700207
208void Fat::wipe(const char *fsPath, unsigned int numSectors) {
Ken Sumrall9caab762013-06-11 19:10:20 -0700209 unsigned long long range[2];
210
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700211 int fd = open(fsPath, O_RDWR | O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900212 if (fd == -1) {
213 SLOGE("Fat wipe failed to open device %s", fsPath);
214 return;
215 }
216
217 if (numSectors == 0) {
218 unsigned long nr_sec;
219 get_blkdev_size(fd, &nr_sec);
220 if (nr_sec > UINT32_MAX) {
221 SLOGE("Too many sectors for FAT: %ld", nr_sec);
Ken Sumrall9caab762013-06-11 19:10:20 -0700222 close(fd);
223 return;
224 }
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900225 numSectors = nr_sec;
Ken Sumrall9caab762013-06-11 19:10:20 -0700226 }
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900227 if (numSectors == 0) {
228 SLOGE("Fat wipe failed to determine size of %s", fsPath);
229 close(fd);
230 return;
231 }
232 range[0] = 0;
233 range[1] = (unsigned long long)numSectors * 512;
234 if (ioctl(fd, BLKDISCARD, &range) < 0) {
235 SLOGE("Fat wipe failed to discard blocks on %s", fsPath);
236 } else {
237 SLOGI("Fat wipe %d sectors on %s succeeded", numSectors, fsPath);
238 }
239 close(fd);
Ken Sumrall9caab762013-06-11 19:10:20 -0700240}