2010-01-04 19:09:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/ioctl.h>
|
2010-01-04 19:09:16 +01:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/mount.h>
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2013-01-16 21:29:28 +01:00
|
|
|
#include <sys/wait.h>
|
2010-01-04 19:09:16 +01:00
|
|
|
|
|
|
|
#include <linux/kdev_t.h>
|
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2015-04-01 20:54:32 +02:00
|
|
|
#include <selinux/selinux.h>
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2013-01-16 21:29:28 +01:00
|
|
|
#include <logwrap/logwrap.h>
|
|
|
|
|
2015-04-01 20:54:32 +02:00
|
|
|
#include "Utils.h"
|
2018-09-18 22:30:21 +02:00
|
|
|
#include "Vfat.h"
|
2012-12-21 21:41:43 +01:00
|
|
|
#include "VoldUtil.h"
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
namespace vfat {
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
static const char* kMkfsPath = "/system/bin/newfs_msdos";
|
|
|
|
static const char* kFsckPath = "/system/bin/fsck_msdos";
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
bool IsSupported() {
|
2018-09-18 22:30:21 +02:00
|
|
|
return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
|
|
|
|
IsFilesystemSupported("vfat");
|
2015-05-22 07:35:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t Check(const std::string& source) {
|
2010-01-04 19:09:16 +01:00
|
|
|
int pass = 1;
|
|
|
|
int rc = 0;
|
|
|
|
do {
|
2015-04-09 06:07:21 +02:00
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kFsckPath);
|
|
|
|
cmd.push_back("-p");
|
|
|
|
cmd.push_back("-f");
|
2019-06-06 20:33:51 +02:00
|
|
|
cmd.push_back("-y");
|
2015-05-22 07:35:42 +02:00
|
|
|
cmd.push_back(source);
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2015-04-01 20:54:32 +02:00
|
|
|
// Fat devices are currently always untrusted
|
2018-11-30 20:43:47 +01:00
|
|
|
rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext);
|
2015-04-01 20:54:32 +02:00
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if (rc < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(ERROR) << "Filesystem check failed due to logwrap error";
|
2013-01-16 21:29:28 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
LOG(INFO) << "Filesystem check completed OK";
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
|
|
|
|
errno = ENODATA;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
if (pass++ <= 3) {
|
|
|
|
LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Failing check after too many rechecks";
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
LOG(ERROR) << "Filesystem check failed (no filesystem)";
|
|
|
|
errno = ENODATA;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
2010-01-04 19:09:16 +01:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
|
|
|
|
bool executable, int ownerUid, int ownerGid, int permMask, bool createLost) {
|
2010-01-04 19:09:16 +01:00
|
|
|
int rc;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
const char* c_source = source.c_str();
|
|
|
|
const char* c_target = target.c_str();
|
|
|
|
|
2017-07-03 04:25:33 +02:00
|
|
|
flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME;
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2010-08-27 17:31:35 +02:00
|
|
|
flags |= (executable ? 0 : MS_NOEXEC);
|
2010-01-06 19:33:53 +01:00
|
|
|
flags |= (ro ? MS_RDONLY : 0);
|
|
|
|
flags |= (remount ? MS_REMOUNT : 0);
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
auto mountData =
|
|
|
|
android::base::StringPrintf("utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
|
|
|
|
ownerUid, ownerGid, permMask, permMask);
|
2010-01-07 04:19:46 +01:00
|
|
|
|
2017-10-07 02:02:53 +02:00
|
|
|
rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
|
2010-01-07 04:19:46 +01:00
|
|
|
|
2010-01-04 19:09:16 +01:00
|
|
|
if (rc && errno == EROFS) {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
|
2010-01-04 19:09:16 +01:00
|
|
|
flags |= MS_RDONLY;
|
2017-10-07 02:02:53 +02:00
|
|
|
rc = mount(c_source, c_target, "vfat", flags, mountData.c_str());
|
2010-01-04 19:09:16 +01:00
|
|
|
}
|
|
|
|
|
2010-01-07 04:19:46 +01:00
|
|
|
if (rc == 0 && createLost) {
|
2017-10-07 02:02:53 +02:00
|
|
|
auto lost_path = android::base::StringPrintf("%s/LOST.DIR", target.c_str());
|
|
|
|
if (access(lost_path.c_str(), F_OK)) {
|
2010-01-04 19:09:16 +01:00
|
|
|
/*
|
|
|
|
* Create a LOST.DIR in the root so we have somewhere to put
|
|
|
|
* lost cluster chains (fsck_msdos doesn't currently do this)
|
|
|
|
*/
|
2017-10-07 02:02:53 +02:00
|
|
|
if (mkdir(lost_path.c_str(), 0755)) {
|
|
|
|
PLOG(ERROR) << "Unable to create LOST.DIR";
|
2010-01-04 19:09:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-03 18:06:39 +02:00
|
|
|
status_t Format(const std::string& source, unsigned long numSectors) {
|
2015-04-09 06:07:21 +02:00
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kMkfsPath);
|
|
|
|
cmd.push_back("-O");
|
|
|
|
cmd.push_back("android");
|
|
|
|
cmd.push_back("-A");
|
2010-03-03 21:37:32 +01:00
|
|
|
|
|
|
|
if (numSectors) {
|
2015-04-09 06:07:21 +02:00
|
|
|
cmd.push_back("-s");
|
2015-08-03 18:06:39 +02:00
|
|
|
cmd.push_back(StringPrintf("%lu", numSectors));
|
2010-03-03 21:37:32 +01:00
|
|
|
}
|
2010-01-04 19:09:16 +01:00
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
cmd.push_back(source);
|
2013-01-16 21:29:28 +01:00
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
int rc = ForkExecvp(cmd);
|
2015-04-09 06:07:21 +02:00
|
|
|
if (rc < 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(ERROR) << "Filesystem format failed due to logwrap error";
|
2013-01-16 21:29:28 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
if (rc == 0) {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(INFO) << "Filesystem formatted OK";
|
2010-01-04 19:09:16 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
2017-10-07 02:02:53 +02:00
|
|
|
LOG(ERROR) << "Format failed (unknown exit code " << rc << ")";
|
2010-01-04 19:09:16 +01:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-12 04:10:20 +02:00
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
} // namespace vfat
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|