blob: 2b0e1fa1e7f8f3841b10060ae5755d34aa1ce2e3 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
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
29static char FSCK_MSDOS_PATH[] = "/system/bin/dosfsck";
30
31int 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
39int vfat_check(blkdev_t *dev)
40{
41 int rc;
San Mehat7edc4f92009-05-18 09:55:21 -070042 boolean rw = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
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 Mehat7edc4f92009-05-18 09:55:21 -070054 do {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
San Mehat7edc4f92009-05-18 09:55:21 -070056 char *args[6];
57 args[0] = FSCK_MSDOS_PATH;
58 args[1] = "-v";
59
60 if (rw) {
61 args[2] = "-w";
62 args[3] = "-p";
63 args[4] = blkdev_get_devpath(dev);
64 args[5] = NULL;
65 rc = logwrap(5, args);
66 free(args[4]);
67 } else {
68 args[2] = "-n";
69 args[3] = blkdev_get_devpath(dev);
70 args[4] = NULL;
71 rc = logwrap(4, args);
72 free(args[3]);
73 }
74
75 if (rc == 0) {
76 LOG_VOL("Filesystem check completed OK");
77 return 0;
78 } else if (rc == 1) {
79 LOG_VOL("Filesystem check failed (general failure)");
80 return -EINVAL;
81 } else if (rc == 2) {
82 LOG_VOL("Filesystem check failed (invalid usage)");
83 return -EIO;
84 } else if (rc == 4) {
85 LOG_VOL("Filesystem check completed (errors fixed)");
86 } else if (rc == 6) {
87 LOG_VOL("Filesystem read-only - retrying check RO");
88 rw = false;
89 continue;
90 } else if (rc == 8) {
91 LOG_VOL("Filesystem check failed (not a FAT filesystem)");
92 return -ENODATA;
93 } else {
94 LOG_VOL("Filesystem check failed (unknown exit code %d)", rc);
95 return -EIO;
96 }
97 } while (0);
98
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 return 0;
100}
101
102int vfat_mount(blkdev_t *dev, volume_t *vol, boolean safe_mode)
103{
104 int flags, rc;
105 char *devpath;
106
107 devpath = blkdev_get_devpath(dev);
108
109#if VFAT_DEBUG
110 LOG_VOL("vfat_mount(%d:%d, %s, %d):", dev->major, dev->minor, vol->mount_point, safe_mode);
111#endif
112
113 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC;
114
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 if (vol->state == volstate_mounted) {
116 LOG_VOL("Remounting %d:%d on %s, safe mode %d", dev->major,
117 dev->minor, vol->mount_point, safe_mode);
118 flags |= MS_REMOUNT;
119 }
120
San Mehatb76a63b2009-05-18 12:59:13 -0700121 /*
122 * The mount masks restrict access so that:
123 * 1. The 'system' user cannot access the SD card at all -
124 * (protects system_server from grabbing file references)
125 * 2. Group users can RWX
126 * 3. Others can only RX
127 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 rc = mount(devpath, vol->mount_point, "vfat", flags,
San Mehatb76a63b2009-05-18 12:59:13 -0700129 "utf8,uid=1000,gid=1015,fmask=702,dmask=702,shortname=mixed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
131 if (rc && errno == EROFS) {
132 LOGE("vfat_mount(%d:%d, %s): Read only filesystem - retrying mount RO",
133 dev->major, dev->minor, vol->mount_point);
134 flags |= MS_RDONLY;
135 rc = mount(devpath, vol->mount_point, "vfat", flags,
San Mehatb76a63b2009-05-18 12:59:13 -0700136 "utf8,uid=1000,gid=1015,fmask=702,dmask=702,shortname=mixed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
138
139#if VFAT_DEBUG
140 LOG_VOL("vfat_mount(%s, %d:%d): mount rc = %d", dev->major,k dev->minor,
141 vol->mount_point, rc);
142#endif
143 free (devpath);
144 return rc;
145}