blob: 1dc4c8d54ec928630c1052c27e09d9719bbe4780 [file] [log] [blame]
The Android Open Source Project8ac3a132009-01-20 14:04:01 -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
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080034 LOG_VOL("vfat_identify(%d:%d):", dev->major, dev->minor);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080035#endif
36 return 0; // XXX: Implement
37}
38
39int vfat_check(blkdev_t *dev)
40{
41 int rc;
42
43#if VFAT_DEBUG
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080044 LOG_VOL("vfat_check(%d:%d):", dev->major, dev->minor);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080045#endif
46
47 if (access(FSCK_MSDOS_PATH, X_OK)) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080048 LOGE("vfat_check(%d:%d): %s not found (skipping checks)",
The Android Open Source Project13f797d2009-02-10 15:44:07 -080049 dev->major, dev->minor, FSCK_MSDOS_PATH);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080050 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";
The Android Open Source Project13f797d2009-02-10 15:44:07 -080060 args[5] = blkdev_get_devpath(dev);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080061 args[6] = NULL;
62 rc = logwrap(6, args);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080063 free(args[5]);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080064#else
65 char *args[6];
66 args[0] = FSCK_MSDOS_PATH;
67 args[1] = "-v";
68 args[2] = "-w";
69 args[3] = "-p";
The Android Open Source Project13f797d2009-02-10 15:44:07 -080070 args[4] = blkdev_get_devpath(dev);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080071 args[5] = NULL;
72 rc = logwrap(5, args);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080073 free(args[4]);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080074#endif
75
76 if (rc == 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080077 LOG_VOL("Filesystem check completed OK");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080078 return 0;
79 } else if (rc == 1) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080080 LOG_VOL("Filesystem check failed (general failure)");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080081 return -EINVAL;
82 } else if (rc == 2) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080083 LOG_VOL("Filesystem check failed (invalid usage)");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080084 return -EIO;
85 } else if (rc == 4) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080086 LOG_VOL("Filesystem check completed (errors fixed)");
The Android Open Source Project13f797d2009-02-10 15:44:07 -080087 } else if (rc == 8) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080088 LOG_VOL("Filesystem check failed (not a FAT filesystem)");
The Android Open Source Project13f797d2009-02-10 15:44:07 -080089 return -ENODATA;
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080090 } else {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080091 LOG_VOL("Filesystem check failed (unknown exit code %d)", rc);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080092 return -EIO;
93 }
94 return 0;
95}
96
The Android Open Source Project13f797d2009-02-10 15:44:07 -080097int vfat_mount(blkdev_t *dev, volume_t *vol, boolean safe_mode)
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080098{
99 int flags, rc;
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800100 char *devpath;
101
102 devpath = blkdev_get_devpath(dev);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800103
104#if VFAT_DEBUG
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800105 LOG_VOL("vfat_mount(%d:%d, %s, %d):", dev->major, dev->minor, vol->mount_point, safe_mode);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800106#endif
107
108 flags = MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_DIRSYNC;
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800109
110 if (safe_mode)
111 flags |= MS_SYNCHRONOUS;
112 if (vol->state == volstate_mounted) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800113 LOG_VOL("Remounting %d:%d on %s, safe mode %d", dev->major,
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800114 dev->minor, vol->mount_point, safe_mode);
115 flags |= MS_REMOUNT;
116 }
117
118 rc = mount(devpath, vol->mount_point, "vfat", flags,
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800119 "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
120
121 if (rc && errno == EROFS) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800122 LOGE("vfat_mount(%d:%d, %s): Read only filesystem - retrying mount RO",
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800123 dev->major, dev->minor, vol->mount_point);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800124 flags |= MS_RDONLY;
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800125 rc = mount(devpath, vol->mount_point, "vfat", flags,
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800126 "utf8,uid=1000,gid=1000,fmask=711,dmask=700");
127 }
128
129#if VFAT_DEBUG
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800130 LOG_VOL("vfat_mount(%s, %d:%d): mount rc = %d", dev->major,k dev->minor,
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800131 vol->mount_point, rc);
132#endif
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800133 free (devpath);
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800134 return rc;
135}