blob: 1dc4c8d54ec928630c1052c27e09d9719bbe4780 [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;
42
43#if VFAT_DEBUG
44 LOG_VOL("vfat_check(%d:%d):", dev->major, dev->minor);
45#endif
46
47 if (access(FSCK_MSDOS_PATH, X_OK)) {
48 LOGE("vfat_check(%d:%d): %s not found (skipping checks)",
49 dev->major, dev->minor, FSCK_MSDOS_PATH);
50 return 0;
51 }
52
53#ifdef VERIFY_PASS
54 char *args[7];
55 args[0] = FSCK_MSDOS_PATH;
56 args[1] = "-v";
57 args[2] = "-V";
58 args[3] = "-w";
59 args[4] = "-p";
60 args[5] = blkdev_get_devpath(dev);
61 args[6] = NULL;
62 rc = logwrap(6, args);
63 free(args[5]);
64#else
65 char *args[6];
66 args[0] = FSCK_MSDOS_PATH;
67 args[1] = "-v";
68 args[2] = "-w";
69 args[3] = "-p";
70 args[4] = blkdev_get_devpath(dev);
71 args[5] = NULL;
72 rc = logwrap(5, args);
73 free(args[4]);
74#endif
75
76 if (rc == 0) {
77 LOG_VOL("Filesystem check completed OK");
78 return 0;
79 } else if (rc == 1) {
80 LOG_VOL("Filesystem check failed (general failure)");
81 return -EINVAL;
82 } else if (rc == 2) {
83 LOG_VOL("Filesystem check failed (invalid usage)");
84 return -EIO;
85 } else if (rc == 4) {
86 LOG_VOL("Filesystem check completed (errors fixed)");
87 } else if (rc == 8) {
88 LOG_VOL("Filesystem check failed (not a FAT filesystem)");
89 return -ENODATA;
90 } else {
91 LOG_VOL("Filesystem check failed (unknown exit code %d)", rc);
92 return -EIO;
93 }
94 return 0;
95}
96
97int vfat_mount(blkdev_t *dev, volume_t *vol, boolean safe_mode)
98{
99 int flags, rc;
100 char *devpath;
101
102 devpath = blkdev_get_devpath(dev);
103
104#if VFAT_DEBUG
105 LOG_VOL("vfat_mount(%d:%d, %s, %d):", dev->major, dev->minor, vol->mount_point, safe_mode);
106#endif
107
108 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC;
109
110 if (safe_mode)
111 flags |= MS_SYNCHRONOUS;
112 if (vol->state == volstate_mounted) {
113 LOG_VOL("Remounting %d:%d on %s, safe mode %d", dev->major,
114 dev->minor, vol->mount_point, safe_mode);
115 flags |= MS_REMOUNT;
116 }
117
118 rc = mount(devpath, vol->mount_point, "vfat", flags,
119 "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
120
121 if (rc && errno == EROFS) {
122 LOGE("vfat_mount(%d:%d, %s): Read only filesystem - retrying mount RO",
123 dev->major, dev->minor, vol->mount_point);
124 flags |= MS_RDONLY;
125 rc = mount(devpath, vol->mount_point, "vfat", flags,
126 "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
127 }
128
129#if VFAT_DEBUG
130 LOG_VOL("vfat_mount(%s, %d:%d): mount rc = %d", dev->major,k dev->minor,
131 vol->mount_point, rc);
132#endif
133 free (devpath);
134 return rc;
135}