The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2008 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | |
| 20 | #include <sys/mount.h> |
Bob Lee | bdb005a | 2009-09-04 18:32:25 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | |
| 23 | #include "vold.h" |
| 24 | #include "volmgr.h" |
| 25 | #include "volmgr_vfat.h" |
| 26 | #include "logwrapper.h" |
| 27 | |
| 28 | #define VFAT_DEBUG 0 |
| 29 | |
San Mehat | e7f444f | 2009-06-24 17:56:03 -0700 | [diff] [blame] | 30 | static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
| 32 | int vfat_identify(blkdev_t *dev) |
| 33 | { |
| 34 | #if VFAT_DEBUG |
| 35 | LOG_VOL("vfat_identify(%d:%d):", dev->major, dev->minor); |
| 36 | #endif |
| 37 | return 0; // XXX: Implement |
| 38 | } |
| 39 | |
| 40 | int vfat_check(blkdev_t *dev) |
| 41 | { |
| 42 | int rc; |
San Mehat | 7edc4f9 | 2009-05-18 09:55:21 -0700 | [diff] [blame] | 43 | boolean rw = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | |
| 45 | #if VFAT_DEBUG |
| 46 | LOG_VOL("vfat_check(%d:%d):", dev->major, dev->minor); |
| 47 | #endif |
| 48 | |
| 49 | if (access(FSCK_MSDOS_PATH, X_OK)) { |
| 50 | LOGE("vfat_check(%d:%d): %s not found (skipping checks)", |
| 51 | dev->major, dev->minor, FSCK_MSDOS_PATH); |
| 52 | return 0; |
| 53 | } |
| 54 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame] | 55 | int pass = 1; |
| 56 | do { |
| 57 | char *args[5]; |
| 58 | args[0] = FSCK_MSDOS_PATH; |
| 59 | args[1] = "-p"; |
| 60 | args[2] = "-f"; |
| 61 | args[3] = blkdev_get_devpath(dev); |
| 62 | args[4] = NULL; |
| 63 | rc = logwrap(4, args, 1); |
| 64 | free(args[3]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame] | 66 | if (rc == 0) { |
| 67 | LOG_VOL("Filesystem check completed OK"); |
| 68 | return 0; |
| 69 | } else if (rc == 2) { |
| 70 | LOG_VOL("Filesystem check failed (not a FAT filesystem)"); |
| 71 | return -ENODATA; |
| 72 | } else if (rc == 4) { |
| 73 | if (pass++ <= 3) { |
| 74 | LOG_VOL("Filesystem modified - rechecking (pass %d)", |
| 75 | pass); |
| 76 | continue; |
| 77 | } else { |
| 78 | LOG_VOL("Failing check after too many rechecks"); |
| 79 | return -EIO; |
| 80 | } |
| 81 | } else if (rc == -11) { |
| 82 | LOG_VOL("Filesystem check crashed"); |
| 83 | return -EIO; |
| 84 | } else { |
| 85 | LOG_VOL("Filesystem check failed (unknown exit code %d)", rc); |
| 86 | return -EIO; |
| 87 | } |
| 88 | } while (0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int vfat_mount(blkdev_t *dev, volume_t *vol, boolean safe_mode) |
| 93 | { |
| 94 | int flags, rc; |
| 95 | char *devpath; |
| 96 | |
| 97 | devpath = blkdev_get_devpath(dev); |
| 98 | |
| 99 | #if VFAT_DEBUG |
| 100 | LOG_VOL("vfat_mount(%d:%d, %s, %d):", dev->major, dev->minor, vol->mount_point, safe_mode); |
| 101 | #endif |
| 102 | |
San Mehat | f754f74 | 2009-06-02 10:27:52 -0700 | [diff] [blame] | 103 | flags = MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | if (vol->state == volstate_mounted) { |
| 106 | LOG_VOL("Remounting %d:%d on %s, safe mode %d", dev->major, |
| 107 | dev->minor, vol->mount_point, safe_mode); |
| 108 | flags |= MS_REMOUNT; |
| 109 | } |
| 110 | |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 111 | /* |
Bob Lee | bdb005a | 2009-09-04 18:32:25 -0700 | [diff] [blame] | 112 | * Note: This is a temporary hack. If the sampling profiler is enabled, |
| 113 | * we make the SD card world-writable so any process can write snapshots. |
| 114 | * |
| 115 | * TODO: Remove this code once we have a drop box in system_server. |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 116 | */ |
Bob Lee | bdb005a | 2009-09-04 18:32:25 -0700 | [diff] [blame] | 117 | char value[PROPERTY_VALUE_MAX]; |
| 118 | property_get("persist.sampling_profiler", value, ""); |
| 119 | if (value[0] == '1') { |
| 120 | LOGW("The SD card is world-writable because the" |
| 121 | " 'persist.sampling_profiler' system property is set to '1'."); |
| 122 | rc = mount(devpath, vol->mount_point, "vfat", flags, |
| 123 | "utf8,uid=1000,gid=1015,fmask=000,dmask=000,shortname=mixed"); |
| 124 | } else { |
| 125 | /* |
| 126 | * The mount masks restrict access so that: |
| 127 | * 1. The 'system' user cannot access the SD card at all - |
| 128 | * (protects system_server from grabbing file references) |
| 129 | * 2. Group users can RWX |
| 130 | * 3. Others can only RX |
| 131 | */ |
| 132 | rc = mount(devpath, vol->mount_point, "vfat", flags, |
| 133 | "utf8,uid=1000,gid=1015,fmask=702,dmask=702,shortname=mixed"); |
| 134 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | |
| 136 | if (rc && errno == EROFS) { |
| 137 | LOGE("vfat_mount(%d:%d, %s): Read only filesystem - retrying mount RO", |
| 138 | dev->major, dev->minor, vol->mount_point); |
| 139 | flags |= MS_RDONLY; |
| 140 | rc = mount(devpath, vol->mount_point, "vfat", flags, |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 141 | "utf8,uid=1000,gid=1015,fmask=702,dmask=702,shortname=mixed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | } |
| 143 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame] | 144 | if (rc == 0) { |
| 145 | char *lost_path; |
| 146 | asprintf(&lost_path, "%s/LOST.DIR", vol->mount_point); |
| 147 | if (access(lost_path, F_OK)) { |
| 148 | /* |
| 149 | * Create a LOST.DIR in the root so we have somewhere to put |
| 150 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 151 | */ |
| 152 | if (mkdir(lost_path, 0755)) { |
| 153 | LOGE("Unable to create LOST.DIR (%s)", strerror(errno)); |
| 154 | } |
| 155 | } |
| 156 | free(lost_path); |
| 157 | } |
| 158 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | #if VFAT_DEBUG |
| 160 | LOG_VOL("vfat_mount(%s, %d:%d): mount rc = %d", dev->major,k dev->minor, |
| 161 | vol->mount_point, rc); |
| 162 | #endif |
| 163 | free (devpath); |
| 164 | return rc; |
| 165 | } |