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