blob: faff7638a493b7acf291f6467e298c7dee627da1 [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
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070048#include "Vfat.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
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054namespace android {
55namespace vold {
56namespace vfat {
57
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070058static const char* kMkfsPath = "/system/bin/newfs_msdos";
59static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080060
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070061bool IsSupported() {
62 return access(kMkfsPath, X_OK) == 0
63 && access(kFsckPath, X_OK) == 0
64 && IsFilesystemSupported("vfat");
65}
66
67status_t Check(const std::string& source) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070068 if (access(kFsckPath, X_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -070069 SLOGW("Skipping fs checks\n");
San Mehatbf041852010-01-04 10:09:16 -080070 return 0;
71 }
72
73 int pass = 1;
74 int rc = 0;
75 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070076 std::vector<std::string> cmd;
77 cmd.push_back(kFsckPath);
78 cmd.push_back("-p");
79 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070080 cmd.push_back(source);
San Mehatbf041852010-01-04 10:09:16 -080081
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070082 // Fat devices are currently always untrusted
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070083 rc = ForkExecvp(cmd, sFsckUntrustedContext);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070084
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070085 if (rc < 0) {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080086 SLOGE("Filesystem check failed due to logwrap error");
87 errno = EIO;
88 return -1;
89 }
San Mehatbf041852010-01-04 10:09:16 -080090
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070091 switch(rc) {
San Mehatbf041852010-01-04 10:09:16 -080092 case 0:
San Mehat97ac40e2010-03-24 10:24:19 -070093 SLOGI("Filesystem check completed OK");
San Mehatbf041852010-01-04 10:09:16 -080094 return 0;
95
96 case 2:
San Mehat97ac40e2010-03-24 10:24:19 -070097 SLOGE("Filesystem check failed (not a FAT filesystem)");
San Mehatbf041852010-01-04 10:09:16 -080098 errno = ENODATA;
99 return -1;
100
101 case 4:
102 if (pass++ <= 3) {
San Mehat97ac40e2010-03-24 10:24:19 -0700103 SLOGW("Filesystem modified - rechecking (pass %d)",
San Mehatbf041852010-01-04 10:09:16 -0800104 pass);
105 continue;
106 }
San Mehat97ac40e2010-03-24 10:24:19 -0700107 SLOGE("Failing check after too many rechecks");
San Mehatbf041852010-01-04 10:09:16 -0800108 errno = EIO;
109 return -1;
110
111 default:
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700112 SLOGE("Filesystem check failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -0800113 errno = EIO;
114 return -1;
115 }
116 } while (0);
117
118 return 0;
119}
120
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700121status_t Mount(const std::string& source, const std::string& target, bool ro,
122 bool remount, bool executable, int ownerUid, int ownerGid, int permMask,
123 bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800124 int rc;
125 unsigned long flags;
San Mehatfff0b472010-01-06 19:19:46 -0800126 char mountData[255];
San Mehatbf041852010-01-04 10:09:16 -0800127
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700128 const char* c_source = source.c_str();
129 const char* c_target = target.c_str();
130
Kenny Roota3e06082010-08-27 08:31:35 -0700131 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
San Mehatbf041852010-01-04 10:09:16 -0800132
Kenny Roota3e06082010-08-27 08:31:35 -0700133 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800134 flags |= (ro ? MS_RDONLY : 0);
135 flags |= (remount ? MS_REMOUNT : 0);
136
San Mehatbf041852010-01-04 10:09:16 -0800137 /*
138 * Note: This is a temporary hack. If the sampling profiler is enabled,
139 * we make the SD card world-writable so any process can write snapshots.
140 *
141 * TODO: Remove this code once we have a drop box in system_server.
142 */
143 char value[PROPERTY_VALUE_MAX];
144 property_get("persist.sampling_profiler", value, "");
145 if (value[0] == '1') {
San Mehat97ac40e2010-03-24 10:24:19 -0700146 SLOGW("The SD card is world-writable because the"
San Mehatbf041852010-01-04 10:09:16 -0800147 " 'persist.sampling_profiler' system property is set to '1'.");
San Mehatfff0b472010-01-06 19:19:46 -0800148 permMask = 0;
San Mehatbf041852010-01-04 10:09:16 -0800149 }
150
San Mehatfff0b472010-01-06 19:19:46 -0800151 sprintf(mountData,
152 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
153 ownerUid, ownerGid, permMask, permMask);
154
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700155 rc = mount(c_source, c_target, "vfat", flags, mountData);
San Mehatfff0b472010-01-06 19:19:46 -0800156
San Mehatbf041852010-01-04 10:09:16 -0800157 if (rc && errno == EROFS) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700158 SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
San Mehatbf041852010-01-04 10:09:16 -0800159 flags |= MS_RDONLY;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700160 rc = mount(c_source, c_target, "vfat", flags, mountData);
San Mehatbf041852010-01-04 10:09:16 -0800161 }
162
San Mehatfff0b472010-01-06 19:19:46 -0800163 if (rc == 0 && createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800164 char *lost_path;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700165 asprintf(&lost_path, "%s/LOST.DIR", c_target);
San Mehatbf041852010-01-04 10:09:16 -0800166 if (access(lost_path, F_OK)) {
167 /*
168 * Create a LOST.DIR in the root so we have somewhere to put
169 * lost cluster chains (fsck_msdos doesn't currently do this)
170 */
171 if (mkdir(lost_path, 0755)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700172 SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
San Mehatbf041852010-01-04 10:09:16 -0800173 }
174 }
175 free(lost_path);
176 }
177
178 return rc;
179}
180
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700181status_t Format(const std::string& source, unsigned int numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700182 std::vector<std::string> cmd;
183 cmd.push_back(kMkfsPath);
184 cmd.push_back("-F");
185 cmd.push_back("32");
186 cmd.push_back("-O");
187 cmd.push_back("android");
188 cmd.push_back("-c");
189 cmd.push_back("64");
190 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800191
192 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700193 cmd.push_back("-s");
194 cmd.push_back(StringPrintf("%u", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800195 }
San Mehatbf041852010-01-04 10:09:16 -0800196
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700197 cmd.push_back(source);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700198
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700199 int rc = ForkExecvp(cmd);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700200 if (rc < 0) {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800201 SLOGE("Filesystem format failed due to logwrap error");
202 errno = EIO;
203 return -1;
204 }
205
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700206 if (rc == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700207 SLOGI("Filesystem formatted OK");
San Mehatbf041852010-01-04 10:09:16 -0800208 return 0;
209 } else {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 SLOGE("Format failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -0800211 errno = EIO;
212 return -1;
213 }
214 return 0;
215}
Ken Sumrall9caab762013-06-11 19:10:20 -0700216
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700217} // namespace vfat
218} // namespace vold
219} // namespace android