blob: 538178e217bc9040a00f674fc74c0949d0a0883d [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
Elliott Hughes7e128fb2015-12-04 15:50:53 -080038#include <android-base/logging.h>
39#include <android-base/stringprintf.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070040#include <selinux/selinux.h>
San Mehatbf041852010-01-04 10:09:16 -080041
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080042#include <logwrap/logwrap.h>
43
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044#include "Vfat.h"
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070045#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080046#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080047
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070048using android::base::StringPrintf;
49
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070050namespace android {
51namespace vold {
52namespace vfat {
53
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070054static const char* kMkfsPath = "/system/bin/newfs_msdos";
55static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080056
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070057bool IsSupported() {
58 return access(kMkfsPath, X_OK) == 0
59 && access(kFsckPath, X_OK) == 0
60 && IsFilesystemSupported("vfat");
61}
62
63status_t Check(const std::string& source) {
San Mehatbf041852010-01-04 10:09:16 -080064 int pass = 1;
65 int rc = 0;
66 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070067 std::vector<std::string> cmd;
68 cmd.push_back(kFsckPath);
69 cmd.push_back("-p");
70 cmd.push_back("-f");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070071 cmd.push_back(source);
San Mehatbf041852010-01-04 10:09:16 -080072
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070073 // Fat devices are currently always untrusted
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070074 rc = ForkExecvp(cmd, sFsckUntrustedContext);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070075
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070076 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060077 LOG(ERROR) << "Filesystem check failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080078 errno = EIO;
79 return -1;
80 }
San Mehatbf041852010-01-04 10:09:16 -080081
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070082 switch(rc) {
San Mehatbf041852010-01-04 10:09:16 -080083 case 0:
Jeff Sharkey3472e522017-10-06 18:02:53 -060084 LOG(INFO) << "Filesystem check completed OK";
San Mehatbf041852010-01-04 10:09:16 -080085 return 0;
86
87 case 2:
Jeff Sharkey3472e522017-10-06 18:02:53 -060088 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
San Mehatbf041852010-01-04 10:09:16 -080089 errno = ENODATA;
90 return -1;
91
92 case 4:
93 if (pass++ <= 3) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060094 LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
San Mehatbf041852010-01-04 10:09:16 -080095 continue;
96 }
Jeff Sharkey3472e522017-10-06 18:02:53 -060097 LOG(ERROR) << "Failing check after too many rechecks";
San Mehatbf041852010-01-04 10:09:16 -080098 errno = EIO;
99 return -1;
100
Mateusz Nowak3dd39302015-08-03 15:48:52 +0200101 case 8:
Jeff Sharkey3472e522017-10-06 18:02:53 -0600102 LOG(ERROR) << "Filesystem check failed (no filesystem)";
Mateusz Nowak3dd39302015-08-03 15:48:52 +0200103 errno = ENODATA;
104 return -1;
105
San Mehatbf041852010-01-04 10:09:16 -0800106 default:
Jeff Sharkey3472e522017-10-06 18:02:53 -0600107 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
San Mehatbf041852010-01-04 10:09:16 -0800108 errno = EIO;
109 return -1;
110 }
111 } while (0);
112
113 return 0;
114}
115
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700116status_t Mount(const std::string& source, const std::string& target, bool ro,
117 bool remount, bool executable, int ownerUid, int ownerGid, int permMask,
118 bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800119 int rc;
120 unsigned long flags;
121
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700122 const char* c_source = source.c_str();
123 const char* c_target = target.c_str();
124
Ravisankar Reddy4cc6baf2017-07-03 11:25:33 +0900125 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
San Mehatbf041852010-01-04 10:09:16 -0800126
Kenny Roota3e06082010-08-27 08:31:35 -0700127 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800128 flags |= (ro ? MS_RDONLY : 0);
129 flags |= (remount ? MS_REMOUNT : 0);
130
Jeff Sharkey3472e522017-10-06 18:02:53 -0600131 auto mountData = android::base::StringPrintf(
San Mehatfff0b472010-01-06 19:19:46 -0800132 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
133 ownerUid, ownerGid, permMask, permMask);
134
Jeff Sharkey3472e522017-10-06 18:02:53 -0600135 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatfff0b472010-01-06 19:19:46 -0800136
San Mehatbf041852010-01-04 10:09:16 -0800137 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600138 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
San Mehatbf041852010-01-04 10:09:16 -0800139 flags |= MS_RDONLY;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600140 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatbf041852010-01-04 10:09:16 -0800141 }
142
San Mehatfff0b472010-01-06 19:19:46 -0800143 if (rc == 0 && createLost) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600144 auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
145 if (access(lost_path.c_str(), F_OK)) {
San Mehatbf041852010-01-04 10:09:16 -0800146 /*
147 * Create a LOST.DIR in the root so we have somewhere to put
148 * lost cluster chains (fsck_msdos doesn't currently do this)
149 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600150 if (mkdir(lost_path.c_str(), 0755)) {
151 PLOG(ERROR) << "Unable to create LOST.DIR";
San Mehatbf041852010-01-04 10:09:16 -0800152 }
153 }
San Mehatbf041852010-01-04 10:09:16 -0800154 }
155
156 return rc;
157}
158
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200159status_t Format(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700160 std::vector<std::string> cmd;
161 cmd.push_back(kMkfsPath);
162 cmd.push_back("-F");
163 cmd.push_back("32");
164 cmd.push_back("-O");
165 cmd.push_back("android");
166 cmd.push_back("-c");
167 cmd.push_back("64");
168 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800169
170 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 cmd.push_back("-s");
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200172 cmd.push_back(StringPrintf("%lu", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800173 }
San Mehatbf041852010-01-04 10:09:16 -0800174
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700175 cmd.push_back(source);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700176
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700177 int rc = ForkExecvp(cmd);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700178 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600179 LOG(ERROR) << "Filesystem format failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800180 errno = EIO;
181 return -1;
182 }
183
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700184 if (rc == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600185 LOG(INFO) << "Filesystem formatted OK";
San Mehatbf041852010-01-04 10:09:16 -0800186 return 0;
187 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600188 LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
San Mehatbf041852010-01-04 10:09:16 -0800189 errno = EIO;
190 return -1;
191 }
192 return 0;
193}
Ken Sumrall9caab762013-06-11 19:10:20 -0700194
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700195} // namespace vfat
196} // namespace vold
197} // namespace android