blob: 40d25887691a74aa945b0ecbc565a91d13be7d78 [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
Tuxera Inc0bf909b2021-07-27 21:17:09 +030035#define LOG_TAG "Vold::Fat"
36
Elliott Hughes7e128fb2015-12-04 15:50:53 -080037#include <android-base/logging.h>
38#include <android-base/stringprintf.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070039#include <selinux/selinux.h>
San Mehatbf041852010-01-04 10:09:16 -080040
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080041#include <logwrap/logwrap.h>
42
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070043#include "Utils.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070044#include "Vfat.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080045#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080046
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070047using android::base::StringPrintf;
48
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070049namespace android {
50namespace vold {
51namespace vfat {
52
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070053static const char* kMkfsPath = "/system/bin/newfs_msdos";
54static const char* kFsckPath = "/system/bin/fsck_msdos";
San Mehatbf041852010-01-04 10:09:16 -080055
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070056bool IsSupported() {
Paul Crowley14c8c072018-09-18 13:30:21 -070057 return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
58 IsFilesystemSupported("vfat");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070059}
60
61status_t Check(const std::string& source) {
San Mehatbf041852010-01-04 10:09:16 -080062 int pass = 1;
63 int rc = 0;
64 do {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070065 std::vector<std::string> cmd;
66 cmd.push_back(kFsckPath);
67 cmd.push_back("-p");
68 cmd.push_back("-f");
Xin Li3601f472019-06-06 11:33:51 -070069 cmd.push_back("-y");
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070070 cmd.push_back(source);
San Mehatbf041852010-01-04 10:09:16 -080071
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070072 // Fat devices are currently always untrusted
louisliua916d3f2021-04-01 09:49:55 +080073 //rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
74 rc = ForkExecvpTimeout(cmd, nullptr, 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
Paul Crowley14c8c072018-09-18 13:30:21 -070082 switch (rc) {
83 case 0:
84 LOG(INFO) << "Filesystem check completed OK";
85 return 0;
San Mehatbf041852010-01-04 10:09:16 -080086
Paul Crowley14c8c072018-09-18 13:30:21 -070087 case 2:
88 LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
89 errno = ENODATA;
90 return -1;
San Mehatbf041852010-01-04 10:09:16 -080091
Paul Crowley14c8c072018-09-18 13:30:21 -070092 case 4:
93 if (pass++ <= 3) {
94 LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
95 continue;
96 }
97 LOG(ERROR) << "Failing check after too many rechecks";
98 errno = EIO;
99 return -1;
San Mehatbf041852010-01-04 10:09:16 -0800100
Paul Crowley14c8c072018-09-18 13:30:21 -0700101 case 8:
102 LOG(ERROR) << "Filesystem check failed (no filesystem)";
103 errno = ENODATA;
104 return -1;
Mateusz Nowak3dd39302015-08-03 15:48:52 +0200105
Paul Crowley14c8c072018-09-18 13:30:21 -0700106 default:
107 LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
108 errno = EIO;
109 return -1;
San Mehatbf041852010-01-04 10:09:16 -0800110 }
111 } while (0);
112
113 return 0;
114}
115
Paul Crowley14c8c072018-09-18 13:30:21 -0700116status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
117 bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800118 int rc;
119 unsigned long flags;
120
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700121 const char* c_source = source.c_str();
122 const char* c_target = target.c_str();
123
Ravisankar Reddy4cc6baf2017-07-03 11:25:33 +0900124 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
San Mehatbf041852010-01-04 10:09:16 -0800125
Kenny Roota3e06082010-08-27 08:31:35 -0700126 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800127 flags |= (ro ? MS_RDONLY : 0);
128 flags |= (remount ? MS_REMOUNT : 0);
129
Paul Crowley14c8c072018-09-18 13:30:21 -0700130 auto mountData =
131 android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
132 ownerUid, ownerGid, permMask, permMask);
San Mehatfff0b472010-01-06 19:19:46 -0800133
Jeff Sharkey3472e522017-10-06 18:02:53 -0600134 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatfff0b472010-01-06 19:19:46 -0800135
San Mehatbf041852010-01-04 10:09:16 -0800136 if (rc && errno == EROFS) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600137 LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
San Mehatbf041852010-01-04 10:09:16 -0800138 flags |= MS_RDONLY;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600139 rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
San Mehatbf041852010-01-04 10:09:16 -0800140 }
141
San Mehatfff0b472010-01-06 19:19:46 -0800142 if (rc == 0 && createLost) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600143 auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
144 if (access(lost_path.c_str(), F_OK)) {
San Mehatbf041852010-01-04 10:09:16 -0800145 /*
146 * Create a LOST.DIR in the root so we have somewhere to put
147 * lost cluster chains (fsck_msdos doesn't currently do this)
148 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600149 if (mkdir(lost_path.c_str(), 0755)) {
150 PLOG(ERROR) << "Unable to create LOST.DIR";
San Mehatbf041852010-01-04 10:09:16 -0800151 }
152 }
San Mehatbf041852010-01-04 10:09:16 -0800153 }
154
155 return rc;
156}
157
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200158status_t Format(const std::string& source, unsigned long numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700159 std::vector<std::string> cmd;
160 cmd.push_back(kMkfsPath);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700161 cmd.push_back("-O");
162 cmd.push_back("android");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700163 cmd.push_back("-A");
San Mehatfcf24fe2010-03-03 12:37:32 -0800164
165 if (numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700166 cmd.push_back("-s");
Mateusz Nowaka4f48d02015-08-03 18:06:39 +0200167 cmd.push_back(StringPrintf("%lu", numSectors));
San Mehatfcf24fe2010-03-03 12:37:32 -0800168 }
San Mehatbf041852010-01-04 10:09:16 -0800169
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700170 cmd.push_back(source);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171
Christopher Braga2b2104f2020-09-25 18:10:50 -0400172 int rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700173 if (rc < 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600174 LOG(ERROR) << "Filesystem format failed due to logwrap error";
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800175 errno = EIO;
176 return -1;
177 }
178
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700179 if (rc == 0) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600180 LOG(INFO) << "Filesystem formatted OK";
San Mehatbf041852010-01-04 10:09:16 -0800181 return 0;
182 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600183 LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
San Mehatbf041852010-01-04 10:09:16 -0800184 errno = EIO;
185 return -1;
186 }
187 return 0;
188}
Ken Sumrall9caab762013-06-11 19:10:20 -0700189
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700190} // namespace vfat
191} // namespace vold
192} // namespace android