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> |
| 32 | |
| 33 | #include <linux/kdev_t.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 34 | #include <linux/fs.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 35 | |
| 36 | #define LOG_TAG "Vold" |
| 37 | |
| 38 | #include <cutils/log.h> |
| 39 | #include <cutils/properties.h> |
| 40 | |
| 41 | #include "Fat.h" |
| 42 | |
| 43 | static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos"; |
| 44 | static char MKDOSFS_PATH[] = "/system/bin/newfs_msdos"; |
| 45 | extern "C" int logwrap(int argc, const char **argv, int background); |
| 46 | extern "C" int mount(const char *, const char *, const char *, unsigned long, const void *); |
| 47 | |
| 48 | int Fat::check(const char *fsPath) { |
| 49 | bool rw = true; |
| 50 | if (access(FSCK_MSDOS_PATH, X_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 51 | SLOGW("Skipping fs checks\n"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int pass = 1; |
| 56 | int rc = 0; |
| 57 | do { |
| 58 | const char *args[5]; |
| 59 | args[0] = FSCK_MSDOS_PATH; |
| 60 | args[1] = "-p"; |
| 61 | args[2] = "-f"; |
| 62 | args[3] = fsPath; |
| 63 | args[4] = NULL; |
| 64 | |
| 65 | rc = logwrap(4, args, 1); |
| 66 | |
| 67 | switch(rc) { |
| 68 | case 0: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 69 | SLOGI("Filesystem check completed OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 70 | return 0; |
| 71 | |
| 72 | case 2: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 73 | SLOGE("Filesystem check failed (not a FAT filesystem)"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 74 | errno = ENODATA; |
| 75 | return -1; |
| 76 | |
| 77 | case 4: |
| 78 | if (pass++ <= 3) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 79 | SLOGW("Filesystem modified - rechecking (pass %d)", |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 80 | pass); |
| 81 | continue; |
| 82 | } |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 83 | SLOGE("Failing check after too many rechecks"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 84 | errno = EIO; |
| 85 | return -1; |
| 86 | |
| 87 | default: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 88 | SLOGE("Filesystem check failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 89 | errno = EIO; |
| 90 | return -1; |
| 91 | } |
| 92 | } while (0); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 97 | int Fat::doMount(const char *fsPath, const char *mountPoint, |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 98 | bool ro, bool remount, bool executable, |
| 99 | int ownerUid, int ownerGid, int permMask, bool createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 100 | int rc; |
| 101 | unsigned long flags; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 102 | char mountData[255]; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 103 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 104 | flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 105 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 106 | flags |= (executable ? 0 : MS_NOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 107 | flags |= (ro ? MS_RDONLY : 0); |
| 108 | flags |= (remount ? MS_REMOUNT : 0); |
| 109 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 110 | /* |
| 111 | * Note: This is a temporary hack. If the sampling profiler is enabled, |
| 112 | * we make the SD card world-writable so any process can write snapshots. |
| 113 | * |
| 114 | * TODO: Remove this code once we have a drop box in system_server. |
| 115 | */ |
| 116 | char value[PROPERTY_VALUE_MAX]; |
| 117 | property_get("persist.sampling_profiler", value, ""); |
| 118 | if (value[0] == '1') { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 119 | SLOGW("The SD card is world-writable because the" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 120 | " 'persist.sampling_profiler' system property is set to '1'."); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 121 | permMask = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 122 | } |
| 123 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 124 | sprintf(mountData, |
| 125 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed", |
| 126 | ownerUid, ownerGid, permMask, permMask); |
| 127 | |
| 128 | rc = mount(fsPath, mountPoint, "vfat", flags, mountData); |
| 129 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 130 | if (rc && errno == EROFS) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 131 | 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] | 132 | flags |= MS_RDONLY; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 133 | rc = mount(fsPath, mountPoint, "vfat", flags, mountData); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 134 | } |
| 135 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 136 | if (rc == 0 && createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 137 | char *lost_path; |
| 138 | asprintf(&lost_path, "%s/LOST.DIR", mountPoint); |
| 139 | if (access(lost_path, F_OK)) { |
| 140 | /* |
| 141 | * Create a LOST.DIR in the root so we have somewhere to put |
| 142 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 143 | */ |
| 144 | if (mkdir(lost_path, 0755)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 145 | SLOGE("Unable to create LOST.DIR (%s)", strerror(errno)); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | free(lost_path); |
| 149 | } |
| 150 | |
| 151 | return rc; |
| 152 | } |
| 153 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 154 | int Fat::format(const char *fsPath, unsigned int numSectors) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 155 | int fd; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 156 | const char *args[11]; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 157 | int rc; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 158 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 159 | args[0] = MKDOSFS_PATH; |
| 160 | args[1] = "-F"; |
San Mehat | 8d934ca | 2010-01-09 12:23:47 -0800 | [diff] [blame] | 161 | args[2] = "32"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 162 | args[3] = "-O"; |
| 163 | args[4] = "android"; |
San Mehat | b9aed74 | 2010-02-04 15:07:01 -0800 | [diff] [blame] | 164 | args[5] = "-c"; |
| 165 | args[6] = "8"; |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 166 | |
| 167 | if (numSectors) { |
| 168 | char tmp[32]; |
| 169 | snprintf(tmp, sizeof(tmp), "%u", numSectors); |
| 170 | const char *size = tmp; |
| 171 | args[7] = "-s"; |
| 172 | args[8] = size; |
| 173 | args[9] = fsPath; |
| 174 | args[10] = NULL; |
| 175 | rc = logwrap(11, args, 1); |
| 176 | } else { |
| 177 | args[7] = fsPath; |
| 178 | args[8] = NULL; |
| 179 | rc = logwrap(9, args, 1); |
| 180 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 181 | |
| 182 | if (rc == 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 183 | SLOGI("Filesystem formatted OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 186 | SLOGE("Format failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 187 | errno = EIO; |
| 188 | return -1; |
| 189 | } |
| 190 | return 0; |
| 191 | } |