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 | |
| 38 | #define LOG_TAG "Vold" |
| 39 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 40 | #include <base/logging.h> |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 41 | #include <base/stringprintf.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 42 | #include <cutils/log.h> |
| 43 | #include <cutils/properties.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 44 | #include <selinux/selinux.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 45 | |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 46 | #include <logwrap/logwrap.h> |
| 47 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 48 | #include "Fat.h" |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 49 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 50 | #include "VoldUtil.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 51 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 52 | using android::base::StringPrintf; |
| 53 | |
| 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 | |
| 57 | int Fat::check(const char *fsPath) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 58 | if (access(kFsckPath, X_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 59 | SLOGW("Skipping fs checks\n"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int pass = 1; |
| 64 | int rc = 0; |
| 65 | do { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 66 | std::vector<std::string> cmd; |
| 67 | cmd.push_back(kFsckPath); |
| 68 | cmd.push_back("-p"); |
| 69 | cmd.push_back("-f"); |
| 70 | cmd.push_back(fsPath); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 71 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 72 | // Fat devices are currently always untrusted |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 73 | rc = android::vold::ForkExecvp(cmd, android::vold::sFsckUntrustedContext); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 74 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 75 | if (rc < 0) { |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 76 | SLOGE("Filesystem check failed due to logwrap error"); |
| 77 | errno = EIO; |
| 78 | return -1; |
| 79 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 80 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 81 | switch(rc) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 82 | case 0: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 83 | SLOGI("Filesystem check completed OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 84 | return 0; |
| 85 | |
| 86 | case 2: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 87 | SLOGE("Filesystem check failed (not a FAT filesystem)"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 88 | errno = ENODATA; |
| 89 | return -1; |
| 90 | |
| 91 | case 4: |
| 92 | if (pass++ <= 3) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 93 | SLOGW("Filesystem modified - rechecking (pass %d)", |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 94 | pass); |
| 95 | continue; |
| 96 | } |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 97 | SLOGE("Failing check after too many rechecks"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 98 | errno = EIO; |
| 99 | return -1; |
| 100 | |
| 101 | default: |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 102 | SLOGE("Filesystem check failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 103 | errno = EIO; |
| 104 | return -1; |
| 105 | } |
| 106 | } while (0); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 111 | int Fat::doMount(const char *fsPath, const char *mountPoint, |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 112 | bool ro, bool remount, bool executable, |
| 113 | int ownerUid, int ownerGid, int permMask, bool createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 114 | int rc; |
| 115 | unsigned long flags; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 116 | char mountData[255]; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 117 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 118 | flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 119 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 120 | flags |= (executable ? 0 : MS_NOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 121 | flags |= (ro ? MS_RDONLY : 0); |
| 122 | flags |= (remount ? MS_REMOUNT : 0); |
| 123 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 124 | /* |
| 125 | * Note: This is a temporary hack. If the sampling profiler is enabled, |
| 126 | * we make the SD card world-writable so any process can write snapshots. |
| 127 | * |
| 128 | * TODO: Remove this code once we have a drop box in system_server. |
| 129 | */ |
| 130 | char value[PROPERTY_VALUE_MAX]; |
| 131 | property_get("persist.sampling_profiler", value, ""); |
| 132 | if (value[0] == '1') { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 133 | SLOGW("The SD card is world-writable because the" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 134 | " 'persist.sampling_profiler' system property is set to '1'."); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 135 | permMask = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 136 | } |
| 137 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 138 | sprintf(mountData, |
| 139 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed", |
| 140 | ownerUid, ownerGid, permMask, permMask); |
| 141 | |
| 142 | rc = mount(fsPath, mountPoint, "vfat", flags, mountData); |
| 143 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 144 | if (rc && errno == EROFS) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 145 | SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 146 | flags |= MS_RDONLY; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 147 | rc = mount(fsPath, mountPoint, "vfat", flags, mountData); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 148 | } |
| 149 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 150 | if (rc == 0 && createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 151 | char *lost_path; |
| 152 | asprintf(&lost_path, "%s/LOST.DIR", mountPoint); |
| 153 | if (access(lost_path, F_OK)) { |
| 154 | /* |
| 155 | * Create a LOST.DIR in the root so we have somewhere to put |
| 156 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 157 | */ |
| 158 | if (mkdir(lost_path, 0755)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 159 | SLOGE("Unable to create LOST.DIR (%s)", strerror(errno)); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | free(lost_path); |
| 163 | } |
| 164 | |
| 165 | return rc; |
| 166 | } |
| 167 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 168 | int Fat::format(const char *fsPath, unsigned int numSectors, bool wipe) { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 169 | if (wipe) { |
| 170 | Fat::wipe(fsPath, numSectors); |
| 171 | } |
| 172 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 173 | std::vector<std::string> cmd; |
| 174 | cmd.push_back(kMkfsPath); |
| 175 | cmd.push_back("-F"); |
| 176 | cmd.push_back("32"); |
| 177 | cmd.push_back("-O"); |
| 178 | cmd.push_back("android"); |
| 179 | cmd.push_back("-c"); |
| 180 | cmd.push_back("64"); |
| 181 | cmd.push_back("-A"); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 182 | |
| 183 | if (numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 184 | cmd.push_back("-s"); |
| 185 | cmd.push_back(StringPrintf("%u", numSectors)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 186 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 187 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 188 | cmd.push_back(fsPath); |
| 189 | |
| 190 | int rc = android::vold::ForkExecvp(cmd); |
| 191 | if (rc < 0) { |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 192 | SLOGE("Filesystem format failed due to logwrap error"); |
| 193 | errno = EIO; |
| 194 | return -1; |
| 195 | } |
| 196 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 197 | if (rc == 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 198 | SLOGI("Filesystem formatted OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 199 | return 0; |
| 200 | } else { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 201 | SLOGE("Format failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 202 | errno = EIO; |
| 203 | return -1; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 207 | |
| 208 | void Fat::wipe(const char *fsPath, unsigned int numSectors) { |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 209 | unsigned long long range[2]; |
| 210 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 211 | int fd = open(fsPath, O_RDWR | O_CLOEXEC); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 212 | if (fd == -1) { |
| 213 | SLOGE("Fat wipe failed to open device %s", fsPath); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (numSectors == 0) { |
| 218 | unsigned long nr_sec; |
| 219 | get_blkdev_size(fd, &nr_sec); |
| 220 | if (nr_sec > UINT32_MAX) { |
| 221 | SLOGE("Too many sectors for FAT: %ld", nr_sec); |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 222 | close(fd); |
| 223 | return; |
| 224 | } |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 225 | numSectors = nr_sec; |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 226 | } |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 227 | if (numSectors == 0) { |
| 228 | SLOGE("Fat wipe failed to determine size of %s", fsPath); |
| 229 | close(fd); |
| 230 | return; |
| 231 | } |
| 232 | range[0] = 0; |
| 233 | range[1] = (unsigned long long)numSectors * 512; |
| 234 | if (ioctl(fd, BLKDISCARD, &range) < 0) { |
| 235 | SLOGE("Fat wipe failed to discard blocks on %s", fsPath); |
| 236 | } else { |
| 237 | SLOGI("Fat wipe %d sectors on %s succeeded", numSectors, fsPath); |
| 238 | } |
| 239 | close(fd); |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 240 | } |