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