blob: a7fcc28f58c9ecfb6f18f029fe1190a9e64aace9 [file] [log] [blame]
San Mehatbf041852010-01-04 10:09:16 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
Olivier Bailly37dcda62010-11-16 10:41:53 -080018#include <stdlib.h>
San Mehatbf041852010-01-04 10:09:16 -080019#include <fcntl.h>
20#include <unistd.h>
21#include <errno.h>
22#include <string.h>
23#include <dirent.h>
24#include <errno.h>
25#include <fcntl.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/mman.h>
31#include <sys/mount.h>
32
33#include <linux/kdev_t.h>
34
35#define LOG_TAG "Vold"
36
37#include <cutils/log.h>
38#include <cutils/properties.h>
39
40#include "Fat.h"
41
42static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos";
43static char MKDOSFS_PATH[] = "/system/bin/newfs_msdos";
44extern "C" int logwrap(int argc, const char **argv, int background);
45extern "C" int mount(const char *, const char *, const char *, unsigned long, const void *);
46
47int Fat::check(const char *fsPath) {
48 bool rw = true;
49 if (access(FSCK_MSDOS_PATH, X_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -070050 SLOGW("Skipping fs checks\n");
San Mehatbf041852010-01-04 10:09:16 -080051 return 0;
52 }
53
54 int pass = 1;
55 int rc = 0;
56 do {
57 const char *args[5];
58 args[0] = FSCK_MSDOS_PATH;
59 args[1] = "-p";
60 args[2] = "-f";
61 args[3] = fsPath;
62 args[4] = NULL;
63
64 rc = logwrap(4, args, 1);
65
66 switch(rc) {
67 case 0:
San Mehat97ac40e2010-03-24 10:24:19 -070068 SLOGI("Filesystem check completed OK");
San Mehatbf041852010-01-04 10:09:16 -080069 return 0;
70
71 case 2:
San Mehat97ac40e2010-03-24 10:24:19 -070072 SLOGE("Filesystem check failed (not a FAT filesystem)");
San Mehatbf041852010-01-04 10:09:16 -080073 errno = ENODATA;
74 return -1;
75
76 case 4:
77 if (pass++ <= 3) {
San Mehat97ac40e2010-03-24 10:24:19 -070078 SLOGW("Filesystem modified - rechecking (pass %d)",
San Mehatbf041852010-01-04 10:09:16 -080079 pass);
80 continue;
81 }
San Mehat97ac40e2010-03-24 10:24:19 -070082 SLOGE("Failing check after too many rechecks");
San Mehatbf041852010-01-04 10:09:16 -080083 errno = EIO;
84 return -1;
85
86 default:
San Mehat97ac40e2010-03-24 10:24:19 -070087 SLOGE("Filesystem check failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -080088 errno = EIO;
89 return -1;
90 }
91 } while (0);
92
93 return 0;
94}
95
San Mehatfff0b472010-01-06 19:19:46 -080096int Fat::doMount(const char *fsPath, const char *mountPoint,
Kenny Roota3e06082010-08-27 08:31:35 -070097 bool ro, bool remount, bool executable,
98 int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -080099 int rc;
100 unsigned long flags;
San Mehatfff0b472010-01-06 19:19:46 -0800101 char mountData[255];
San Mehatbf041852010-01-04 10:09:16 -0800102
Kenny Roota3e06082010-08-27 08:31:35 -0700103 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
San Mehatbf041852010-01-04 10:09:16 -0800104
Kenny Roota3e06082010-08-27 08:31:35 -0700105 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800106 flags |= (ro ? MS_RDONLY : 0);
107 flags |= (remount ? MS_REMOUNT : 0);
108
San Mehatbf041852010-01-04 10:09:16 -0800109 /*
110 * Note: This is a temporary hack. If the sampling profiler is enabled,
111 * we make the SD card world-writable so any process can write snapshots.
112 *
113 * TODO: Remove this code once we have a drop box in system_server.
114 */
115 char value[PROPERTY_VALUE_MAX];
116 property_get("persist.sampling_profiler", value, "");
117 if (value[0] == '1') {
San Mehat97ac40e2010-03-24 10:24:19 -0700118 SLOGW("The SD card is world-writable because the"
San Mehatbf041852010-01-04 10:09:16 -0800119 " 'persist.sampling_profiler' system property is set to '1'.");
San Mehatfff0b472010-01-06 19:19:46 -0800120 permMask = 0;
San Mehatbf041852010-01-04 10:09:16 -0800121 }
122
San Mehatfff0b472010-01-06 19:19:46 -0800123 sprintf(mountData,
124 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
125 ownerUid, ownerGid, permMask, permMask);
126
127 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
128
San Mehatbf041852010-01-04 10:09:16 -0800129 if (rc && errno == EROFS) {
San Mehat97ac40e2010-03-24 10:24:19 -0700130 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
San Mehatbf041852010-01-04 10:09:16 -0800131 flags |= MS_RDONLY;
San Mehatfff0b472010-01-06 19:19:46 -0800132 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
San Mehatbf041852010-01-04 10:09:16 -0800133 }
134
San Mehatfff0b472010-01-06 19:19:46 -0800135 if (rc == 0 && createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800136 char *lost_path;
137 asprintf(&lost_path, "%s/LOST.DIR", mountPoint);
138 if (access(lost_path, F_OK)) {
139 /*
140 * Create a LOST.DIR in the root so we have somewhere to put
141 * lost cluster chains (fsck_msdos doesn't currently do this)
142 */
143 if (mkdir(lost_path, 0755)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700144 SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
San Mehatbf041852010-01-04 10:09:16 -0800145 }
146 }
147 free(lost_path);
148 }
149
150 return rc;
151}
152
San Mehatfcf24fe2010-03-03 12:37:32 -0800153int Fat::format(const char *fsPath, unsigned int numSectors) {
San Mehatbf041852010-01-04 10:09:16 -0800154 int fd;
San Mehatfcf24fe2010-03-03 12:37:32 -0800155 const char *args[11];
San Mehatbf041852010-01-04 10:09:16 -0800156 int rc;
San Mehatfcf24fe2010-03-03 12:37:32 -0800157
San Mehatbf041852010-01-04 10:09:16 -0800158 args[0] = MKDOSFS_PATH;
159 args[1] = "-F";
San Mehat8d934ca2010-01-09 12:23:47 -0800160 args[2] = "32";
San Mehatbf041852010-01-04 10:09:16 -0800161 args[3] = "-O";
162 args[4] = "android";
San Mehatb9aed742010-02-04 15:07:01 -0800163 args[5] = "-c";
164 args[6] = "8";
San Mehatfcf24fe2010-03-03 12:37:32 -0800165
166 if (numSectors) {
167 char tmp[32];
168 snprintf(tmp, sizeof(tmp), "%u", numSectors);
169 const char *size = tmp;
170 args[7] = "-s";
171 args[8] = size;
172 args[9] = fsPath;
173 args[10] = NULL;
174 rc = logwrap(11, args, 1);
175 } else {
176 args[7] = fsPath;
177 args[8] = NULL;
178 rc = logwrap(9, args, 1);
179 }
San Mehatbf041852010-01-04 10:09:16 -0800180
181 if (rc == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700182 SLOGI("Filesystem formatted OK");
San Mehatbf041852010-01-04 10:09:16 -0800183 return 0;
184 } else {
San Mehat97ac40e2010-03-24 10:24:19 -0700185 SLOGE("Format failed (unknown exit code %d)", rc);
San Mehatbf041852010-01-04 10:09:16 -0800186 errno = EIO;
187 return -1;
188 }
189 return 0;
190}