blob: 4f1e982eb32023d0c9e35faefa80b7f04cb0af4f [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
San Mehatbf041852010-01-04 10:09:16 -080017#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
San Mehatbf041852010-01-04 10:09:16 -080024
Ken Sumrall9caab762013-06-11 19:10:20 -070025#include <linux/fs.h>
26#include <sys/ioctl.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <sys/mman.h>
28#include <sys/mount.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
San Mehatbf041852010-01-04 10:09:16 -080032
33#include <linux/kdev_t.h>
34
Elliott Hughes7e128fb2015-12-04 15:50:53 -080035#include <android-base/logging.h>
36#include <android-base/stringprintf.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070037#include <selinux/selinux.h>
San Mehatbf041852010-01-04 10:09:16 -080038
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080039#include <logwrap/logwrap.h>
40
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070041#include "Utils.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070042#include "Vfat.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080043#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080044
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070045using android::base::StringPrintf;
46
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070047namespace android {
48namespace vold {
49namespace vfat {
50
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070051static const char* kMkfsPath = "/system/bin/newfs_msdos";
52static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080053
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070054bool IsSupported() {
Paul Crowley14c8c072018-09-18 13:30:21 -070055 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
56 IsFilesystemSupported("vfat");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070057}
58
59status_t Check(const std::string& source) {
San Mehatbf041852010-01-04 10:09:16 -080060 int pass = 1;
61 int rc = 0;
62 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070063 std::vector<std::string> cmd;
64 cmd.push_back(kFsckPath);
65 cmd.push_back("-p");
66 cmd.push_back("-f");
Xin Li3d3a9a72019-06-06 11:33:51 -070067 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070068 cmd.push_back(source);
San Mehatbf041852010-01-04 10:09:16 -080069
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070070 // Fat devices are currently always untrusted
Paul Crowleyde2d6202018-11-30 11:43:47 -080071 rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -060074 LOG(ERROR) << "Filesystem check failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080075 errno = EIO;
76 return -1;
77 }
San Mehatbf041852010-01-04 10:09:16 -080078
Paul Crowley14c8c072018-09-18 13:30:21 -070079 switch (rc) {
80 case 0:
81 LOG(INFO) << "Filesystem check completed OK";
82 return 0;
San Mehatbf041852010-01-04 10:09:16 -080083
Paul Crowley14c8c072018-09-18 13:30:21 -070084 case 2:
85 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
86 errno = ENODATA;
87 return -1;
San Mehatbf041852010-01-04 10:09:16 -080088
Paul Crowley14c8c072018-09-18 13:30:21 -070089 case 4:
90 if (pass++ <= 3) {
91 LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
92 continue;
93 }
94 LOG(ERROR) << "Failing check after too many rechecks";
95 errno = EIO;
96 return -1;
San Mehatbf041852010-01-04 10:09:16 -080097
Paul Crowley14c8c072018-09-18 13:30:21 -070098 case 8:
99 LOG(ERROR) << "Filesystem check failed (no filesystem)";
100 errno = ENODATA;
101 return -1;
Mateusz Nowak3dd39302015-08-03 15:48:52 +0200102
Paul Crowley14c8c072018-09-18 13:30:21 -0700103 default:
104 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
105 errno = EIO;
106 return -1;
San Mehatbf041852010-01-04 10:09:16 -0800107 }
108 } while (0);
109
110 return 0;
111}
112
Paul Crowley14c8c072018-09-18 13:30:21 -0700113status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
114 bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800115 int rc;
116 unsigned long flags;
117
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700118 const char* c_source = source.c_str();
119 const char* c_target = target.c_str();
120
Ravisankar Reddy4cc6baf2017-07-03 11:25:33 +0900121 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
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
Paul Crowley14c8c072018-09-18 13:30:21 -0700127 auto mountData =
128 android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
129 ownerUid, ownerGid, permMask, permMask);
San Mehatfff0b472010-01-06 19:19:46 -0800130
Jeff Sharkey3472e522017-10-06 18:02:53 -0600131 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatfff0b472010-01-06 19:19:46 -0800132
San Mehatbf041852010-01-04 10:09:16 -0800133 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600134 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
San Mehatbf041852010-01-04 10:09:16 -0800135 flags |= MS_RDONLY;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600136 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatbf041852010-01-04 10:09:16 -0800137 }
138
San Mehatfff0b472010-01-06 19:19:46 -0800139 if (rc == 0 && createLost) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600140 auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
141 if (access(lost_path.c_str(), F_OK)) {
San Mehatbf041852010-01-04 10:09:16 -0800142 /*
143 * Create a LOST.DIR in the root so we have somewhere to put
144 * lost cluster chains (fsck_msdos doesn't currently do this)
145 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600146 if (mkdir(lost_path.c_str(), 0755)) {
147 PLOG(ERROR) << "Unable to create LOST.DIR";
San Mehatbf041852010-01-04 10:09:16 -0800148 }
149 }
San Mehatbf041852010-01-04 10:09:16 -0800150 }
151
152 return rc;
153}
154
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200155status_t Format(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700156 std::vector<std::string> cmd;
157 cmd.push_back(kMkfsPath);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700158 cmd.push_back("-O");
159 cmd.push_back("android");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700160 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800161
162 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700163 cmd.push_back("-s");
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200164 cmd.push_back(StringPrintf("%lu", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800165 }
San Mehatbf041852010-01-04 10:09:16 -0800166
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700167 cmd.push_back(source);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700168
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700169 int rc = ForkExecvp(cmd);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700170 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600171 LOG(ERROR) << "Filesystem format failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800172 errno = EIO;
173 return -1;
174 }
175
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700176 if (rc == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600177 LOG(INFO) << "Filesystem formatted OK";
San Mehatbf041852010-01-04 10:09:16 -0800178 return 0;
179 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600180 LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
San Mehatbf041852010-01-04 10:09:16 -0800181 errno = EIO;
182 return -1;
183 }
184 return 0;
185}
Ken Sumrall9caab762013-06-11 19:10:20 -0700186
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700187} // namespace vfat
188} // namespace vold
189} // namespace android