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> |
| 21 | |
| 22 | #include "vold.h" |
| 23 | #include "volmgr.h" |
| 24 | #include "volmgr_vfat.h" |
| 25 | #include "logwrapper.h" |
| 26 | |
| 27 | #define VFAT_DEBUG 0 |
| 28 | |
San Mehat | e7f444f | 2009-06-24 17:56:03 -0700 | [diff] [blame] | 29 | static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
| 31 | int vfat_identify(blkdev_t *dev) |
| 32 | { |
| 33 | #if VFAT_DEBUG |
| 34 | LOG_VOL("vfat_identify(%d:%d):", dev->major, dev->minor); |
| 35 | #endif |
| 36 | return 0; // XXX: Implement |
| 37 | } |
| 38 | |
| 39 | int vfat_check(blkdev_t *dev) |
| 40 | { |
| 41 | int rc; |
San Mehat | 7edc4f9 | 2009-05-18 09:55:21 -0700 | [diff] [blame] | 42 | boolean rw = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | #if VFAT_DEBUG |
| 45 | LOG_VOL("vfat_check(%d:%d):", dev->major, dev->minor); |
| 46 | #endif |
| 47 | |
| 48 | if (access(FSCK_MSDOS_PATH, X_OK)) { |
| 49 | LOGE("vfat_check(%d:%d): %s not found (skipping checks)", |
| 50 | dev->major, dev->minor, FSCK_MSDOS_PATH); |
| 51 | return 0; |
| 52 | } |
| 53 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame^] | 54 | int pass = 1; |
| 55 | do { |
| 56 | char *args[5]; |
| 57 | args[0] = FSCK_MSDOS_PATH; |
| 58 | args[1] = "-p"; |
| 59 | args[2] = "-f"; |
| 60 | args[3] = blkdev_get_devpath(dev); |
| 61 | args[4] = NULL; |
| 62 | rc = logwrap(4, args, 1); |
| 63 | free(args[3]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame^] | 65 | if (rc == 0) { |
| 66 | LOG_VOL("Filesystem check completed OK"); |
| 67 | return 0; |
| 68 | } else if (rc == 2) { |
| 69 | LOG_VOL("Filesystem check failed (not a FAT filesystem)"); |
| 70 | return -ENODATA; |
| 71 | } else if (rc == 4) { |
| 72 | if (pass++ <= 3) { |
| 73 | LOG_VOL("Filesystem modified - rechecking (pass %d)", |
| 74 | pass); |
| 75 | continue; |
| 76 | } else { |
| 77 | LOG_VOL("Failing check after too many rechecks"); |
| 78 | return -EIO; |
| 79 | } |
| 80 | } else if (rc == -11) { |
| 81 | LOG_VOL("Filesystem check crashed"); |
| 82 | return -EIO; |
| 83 | } else { |
| 84 | LOG_VOL("Filesystem check failed (unknown exit code %d)", rc); |
| 85 | return -EIO; |
| 86 | } |
| 87 | } while (0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int vfat_mount(blkdev_t *dev, volume_t *vol, boolean safe_mode) |
| 92 | { |
| 93 | int flags, rc; |
| 94 | char *devpath; |
| 95 | |
| 96 | devpath = blkdev_get_devpath(dev); |
| 97 | |
| 98 | #if VFAT_DEBUG |
| 99 | LOG_VOL("vfat_mount(%d:%d, %s, %d):", dev->major, dev->minor, vol->mount_point, safe_mode); |
| 100 | #endif |
| 101 | |
San Mehat | f754f74 | 2009-06-02 10:27:52 -0700 | [diff] [blame] | 102 | flags = MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | if (vol->state == volstate_mounted) { |
| 105 | LOG_VOL("Remounting %d:%d on %s, safe mode %d", dev->major, |
| 106 | dev->minor, vol->mount_point, safe_mode); |
| 107 | flags |= MS_REMOUNT; |
| 108 | } |
| 109 | |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 110 | /* |
| 111 | * The mount masks restrict access so that: |
| 112 | * 1. The 'system' user cannot access the SD card at all - |
| 113 | * (protects system_server from grabbing file references) |
| 114 | * 2. Group users can RWX |
| 115 | * 3. Others can only RX |
| 116 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 117 | rc = mount(devpath, vol->mount_point, "vfat", flags, |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 118 | "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] | 119 | |
| 120 | if (rc && errno == EROFS) { |
| 121 | LOGE("vfat_mount(%d:%d, %s): Read only filesystem - retrying mount RO", |
| 122 | dev->major, dev->minor, vol->mount_point); |
| 123 | flags |= MS_RDONLY; |
| 124 | rc = mount(devpath, vol->mount_point, "vfat", flags, |
San Mehat | b76a63b | 2009-05-18 12:59:13 -0700 | [diff] [blame] | 125 | "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] | 126 | } |
| 127 | |
San Mehat | d8221d9 | 2009-07-16 09:34:53 -0700 | [diff] [blame^] | 128 | if (rc == 0) { |
| 129 | char *lost_path; |
| 130 | asprintf(&lost_path, "%s/LOST.DIR", vol->mount_point); |
| 131 | if (access(lost_path, F_OK)) { |
| 132 | /* |
| 133 | * Create a LOST.DIR in the root so we have somewhere to put |
| 134 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 135 | */ |
| 136 | if (mkdir(lost_path, 0755)) { |
| 137 | LOGE("Unable to create LOST.DIR (%s)", strerror(errno)); |
| 138 | } |
| 139 | } |
| 140 | free(lost_path); |
| 141 | } |
| 142 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | #if VFAT_DEBUG |
| 144 | LOG_VOL("vfat_mount(%s, %d:%d): mount rc = %d", dev->major,k dev->minor, |
| 145 | vol->mount_point, rc); |
| 146 | #endif |
| 147 | free (devpath); |
| 148 | return rc; |
| 149 | } |