San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 18 | #include <stdlib.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 19 | #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 Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 32 | #include <sys/wait.h> |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 33 | #include <linux/fs.h> |
| 34 | #include <sys/ioctl.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 35 | |
| 36 | #include <linux/kdev_t.h> |
| 37 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 38 | #include <android-base/logging.h> |
| 39 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 40 | #include <selinux/selinux.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 41 | |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 42 | #include <logwrap/logwrap.h> |
| 43 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 44 | #include "Vfat.h" |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 45 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 46 | #include "VoldUtil.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 47 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 48 | using android::base::StringPrintf; |
| 49 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 50 | namespace android { |
| 51 | namespace vold { |
| 52 | namespace vfat { |
| 53 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 54 | static const char* kMkfsPath = "/system/bin/newfs_msdos"; |
| 55 | static const char* kFsckPath = "/system/bin/fsck_msdos"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 56 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 57 | bool IsSupported() { |
| 58 | return access(kMkfsPath, X_OK) == 0 |
| 59 | && access(kFsckPath, X_OK) == 0 |
| 60 | && IsFilesystemSupported("vfat"); |
| 61 | } |
| 62 | |
| 63 | status_t Check(const std::string& source) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 64 | if (access(kFsckPath, X_OK)) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 65 | LOG(WARNING) << "Skipping fs checks"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int pass = 1; |
| 70 | int rc = 0; |
| 71 | do { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 72 | std::vector<std::string> cmd; |
| 73 | cmd.push_back(kFsckPath); |
| 74 | cmd.push_back("-p"); |
| 75 | cmd.push_back("-f"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 76 | cmd.push_back(source); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 77 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 78 | // Fat devices are currently always untrusted |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 79 | rc = ForkExecvp(cmd, sFsckUntrustedContext); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 80 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 81 | if (rc < 0) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 82 | LOG(ERROR) << "Filesystem check failed due to logwrap error"; |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 83 | errno = EIO; |
| 84 | return -1; |
| 85 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 86 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 87 | switch(rc) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 88 | case 0: |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 89 | LOG(INFO) << "Filesystem check completed OK"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 90 | return 0; |
| 91 | |
| 92 | case 2: |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 93 | LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 94 | errno = ENODATA; |
| 95 | return -1; |
| 96 | |
| 97 | case 4: |
| 98 | if (pass++ <= 3) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 99 | LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 100 | continue; |
| 101 | } |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 102 | LOG(ERROR) << "Failing check after too many rechecks"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 103 | errno = EIO; |
| 104 | return -1; |
| 105 | |
Mateusz Nowak | 3dd3930 | 2015-08-03 15:48:52 +0200 | [diff] [blame] | 106 | case 8: |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 107 | LOG(ERROR) << "Filesystem check failed (no filesystem)"; |
Mateusz Nowak | 3dd3930 | 2015-08-03 15:48:52 +0200 | [diff] [blame] | 108 | errno = ENODATA; |
| 109 | return -1; |
| 110 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 111 | default: |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 112 | LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 113 | errno = EIO; |
| 114 | return -1; |
| 115 | } |
| 116 | } while (0); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 121 | status_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 Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 124 | int rc; |
| 125 | unsigned long flags; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 126 | char mountData[255]; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 127 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 128 | const char* c_source = source.c_str(); |
| 129 | const char* c_target = target.c_str(); |
| 130 | |
Ravisankar Reddy | 4cc6baf | 2017-07-03 11:25:33 +0900 | [diff] [blame] | 131 | flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC | MS_NOATIME; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 132 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 133 | flags |= (executable ? 0 : MS_NOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 134 | flags |= (ro ? MS_RDONLY : 0); |
| 135 | flags |= (remount ? MS_REMOUNT : 0); |
| 136 | |
George Burgess IV | 605d7ae | 2016-02-29 13:39:17 -0800 | [diff] [blame] | 137 | snprintf(mountData, sizeof(mountData), |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 138 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed", |
| 139 | ownerUid, ownerGid, permMask, permMask); |
| 140 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 141 | rc = mount(c_source, c_target, "vfat", flags, mountData); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 142 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 143 | if (rc && errno == EROFS) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 144 | LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 145 | flags |= MS_RDONLY; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 146 | rc = mount(c_source, c_target, "vfat", flags, mountData); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 147 | } |
| 148 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 149 | if (rc == 0 && createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 150 | char *lost_path; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 151 | asprintf(&lost_path, "%s/LOST.DIR", c_target); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 152 | if (access(lost_path, F_OK)) { |
| 153 | /* |
| 154 | * Create a LOST.DIR in the root so we have somewhere to put |
| 155 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 156 | */ |
| 157 | if (mkdir(lost_path, 0755)) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 158 | PLOG(ERROR) << "Unable to create LOST.DIR"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | free(lost_path); |
| 162 | } |
| 163 | |
| 164 | return rc; |
| 165 | } |
| 166 | |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 167 | status_t Format(const std::string& source, unsigned long numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 168 | std::vector<std::string> cmd; |
| 169 | cmd.push_back(kMkfsPath); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 170 | cmd.push_back("-O"); |
| 171 | cmd.push_back("android"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 172 | cmd.push_back("-A"); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 173 | |
| 174 | if (numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 175 | cmd.push_back("-s"); |
Mateusz Nowak | a4f48d0 | 2015-08-03 18:06:39 +0200 | [diff] [blame] | 176 | cmd.push_back(StringPrintf("%lu", numSectors)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 177 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 178 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 179 | cmd.push_back(source); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 181 | int rc = ForkExecvp(cmd); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 182 | if (rc < 0) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 183 | LOG(ERROR) << "Filesystem format failed due to logwrap error"; |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 184 | errno = EIO; |
| 185 | return -1; |
| 186 | } |
| 187 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 188 | if (rc == 0) { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 189 | LOG(INFO) << "Filesystem formatted OK"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 190 | return 0; |
| 191 | } else { |
Logan Chien | bf319c4 | 2018-06-25 10:18:42 +0800 | [diff] [blame] | 192 | LOG(ERROR) << "Format failed (unknown exit code " << rc << ")"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 193 | errno = EIO; |
| 194 | return -1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 199 | } // namespace vfat |
| 200 | } // namespace vold |
| 201 | } // namespace android |