blob: 807f44027d417e4b2088ab055ba85d652fda658b [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>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080032#include <sys/wait.h>
San Mehatbf041852010-01-04 10:09:16 -080033
34#include <linux/kdev_t.h>
35
36#define LOG_TAG "Vold"
37
38#include <cutils/log.h>
39#include <cutils/properties.h>
40
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080041#include <logwrap/logwrap.h>
42
San Mehatbf041852010-01-04 10:09:16 -080043#include "Fat.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080044#include "VoldUtil.h"
San Mehatbf041852010-01-04 10:09:16 -080045
46static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos";
47static char MKDOSFS_PATH[] = "/system/bin/newfs_msdos";
San Mehatbf041852010-01-04 10:09:16 -080048extern "C" int mount(const char *, const char *, const char *, unsigned long, const void *);
49
50int Fat::check(const char *fsPath) {
51 bool rw = true;
52 if (access(FSCK_MSDOS_PATH, X_OK)) {
San Mehat97ac40e2010-03-24 10:24:19 -070053 SLOGW("Skipping fs checks\n");
San Mehatbf041852010-01-04 10:09:16 -080054 return 0;
55 }
56
57 int pass = 1;
58 int rc = 0;
59 do {
Rom Lemarchand5593c852012-12-21 12:41:43 -080060 const char *args[4];
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080061 int status;
San Mehatbf041852010-01-04 10:09:16 -080062 args[0] = FSCK_MSDOS_PATH;
63 args[1] = "-p";
64 args[2] = "-f";
65 args[3] = fsPath;
San Mehatbf041852010-01-04 10:09:16 -080066
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080067 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status,
68 false, true);
69 if (rc != 0) {
70 SLOGE("Filesystem check failed due to logwrap error");
71 errno = EIO;
72 return -1;
73 }
San Mehatbf041852010-01-04 10:09:16 -080074
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080075 if (!WIFEXITED(status)) {
76 SLOGE("Filesystem check did not exit properly");
77 errno = EIO;
78 return -1;
79 }
80
81 status = WEXITSTATUS(status);
82
83 switch(status) {
San Mehatbf041852010-01-04 10:09:16 -080084 case 0:
San Mehat97ac40e2010-03-24 10:24:19 -070085 SLOGI("Filesystem check completed OK");
San Mehatbf041852010-01-04 10:09:16 -080086 return 0;
87
88 case 2:
San Mehat97ac40e2010-03-24 10:24:19 -070089 SLOGE("Filesystem check failed (not a FAT filesystem)");
San Mehatbf041852010-01-04 10:09:16 -080090 errno = ENODATA;
91 return -1;
92
93 case 4:
94 if (pass++ <= 3) {
San Mehat97ac40e2010-03-24 10:24:19 -070095 SLOGW("Filesystem modified - rechecking (pass %d)",
San Mehatbf041852010-01-04 10:09:16 -080096 pass);
97 continue;
98 }
San Mehat97ac40e2010-03-24 10:24:19 -070099 SLOGE("Failing check after too many rechecks");
San Mehatbf041852010-01-04 10:09:16 -0800100 errno = EIO;
101 return -1;
102
103 default:
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800104 SLOGE("Filesystem check failed (unknown exit code %d)", status);
San Mehatbf041852010-01-04 10:09:16 -0800105 errno = EIO;
106 return -1;
107 }
108 } while (0);
109
110 return 0;
111}
112
San Mehatfff0b472010-01-06 19:19:46 -0800113int Fat::doMount(const char *fsPath, const char *mountPoint,
Kenny Roota3e06082010-08-27 08:31:35 -0700114 bool ro, bool remount, bool executable,
115 int ownerUid, int ownerGid, int permMask, bool createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800116 int rc;
117 unsigned long flags;
San Mehatfff0b472010-01-06 19:19:46 -0800118 char mountData[255];
San Mehatbf041852010-01-04 10:09:16 -0800119
Kenny Roota3e06082010-08-27 08:31:35 -0700120 flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC;
San Mehatbf041852010-01-04 10:09:16 -0800121
Kenny Roota3e06082010-08-27 08:31:35 -0700122 flags |= (executable ? 0 : MS_NOEXEC);
San Mehata19b2502010-01-06 10:33:53 -0800123 flags |= (ro ? MS_RDONLY : 0);
124 flags |= (remount ? MS_REMOUNT : 0);
125
San Mehatbf041852010-01-04 10:09:16 -0800126 /*
127 * Note: This is a temporary hack. If the sampling profiler is enabled,
128 * we make the SD card world-writable so any process can write snapshots.
129 *
130 * TODO: Remove this code once we have a drop box in system_server.
131 */
132 char value[PROPERTY_VALUE_MAX];
133 property_get("persist.sampling_profiler", value, "");
134 if (value[0] == '1') {
San Mehat97ac40e2010-03-24 10:24:19 -0700135 SLOGW("The SD card is world-writable because the"
San Mehatbf041852010-01-04 10:09:16 -0800136 " 'persist.sampling_profiler' system property is set to '1'.");
San Mehatfff0b472010-01-06 19:19:46 -0800137 permMask = 0;
San Mehatbf041852010-01-04 10:09:16 -0800138 }
139
San Mehatfff0b472010-01-06 19:19:46 -0800140 sprintf(mountData,
141 "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed",
142 ownerUid, ownerGid, permMask, permMask);
143
144 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
145
San Mehatbf041852010-01-04 10:09:16 -0800146 if (rc && errno == EROFS) {
San Mehat97ac40e2010-03-24 10:24:19 -0700147 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
San Mehatbf041852010-01-04 10:09:16 -0800148 flags |= MS_RDONLY;
San Mehatfff0b472010-01-06 19:19:46 -0800149 rc = mount(fsPath, mountPoint, "vfat", flags, mountData);
San Mehatbf041852010-01-04 10:09:16 -0800150 }
151
San Mehatfff0b472010-01-06 19:19:46 -0800152 if (rc == 0 && createLost) {
San Mehatbf041852010-01-04 10:09:16 -0800153 char *lost_path;
154 asprintf(&lost_path, "%s/LOST.DIR", mountPoint);
155 if (access(lost_path, F_OK)) {
156 /*
157 * Create a LOST.DIR in the root so we have somewhere to put
158 * lost cluster chains (fsck_msdos doesn't currently do this)
159 */
160 if (mkdir(lost_path, 0755)) {
San Mehat97ac40e2010-03-24 10:24:19 -0700161 SLOGE("Unable to create LOST.DIR (%s)", strerror(errno));
San Mehatbf041852010-01-04 10:09:16 -0800162 }
163 }
164 free(lost_path);
165 }
166
167 return rc;
168}
169
San Mehatfcf24fe2010-03-03 12:37:32 -0800170int Fat::format(const char *fsPath, unsigned int numSectors) {
San Mehatbf041852010-01-04 10:09:16 -0800171 int fd;
Rom Lemarchand5593c852012-12-21 12:41:43 -0800172 const char *args[10];
San Mehatbf041852010-01-04 10:09:16 -0800173 int rc;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800174 int status;
San Mehatfcf24fe2010-03-03 12:37:32 -0800175
San Mehatbf041852010-01-04 10:09:16 -0800176 args[0] = MKDOSFS_PATH;
177 args[1] = "-F";
San Mehat8d934ca2010-01-09 12:23:47 -0800178 args[2] = "32";
San Mehatbf041852010-01-04 10:09:16 -0800179 args[3] = "-O";
180 args[4] = "android";
San Mehatb9aed742010-02-04 15:07:01 -0800181 args[5] = "-c";
182 args[6] = "8";
San Mehatfcf24fe2010-03-03 12:37:32 -0800183
184 if (numSectors) {
185 char tmp[32];
186 snprintf(tmp, sizeof(tmp), "%u", numSectors);
187 const char *size = tmp;
188 args[7] = "-s";
189 args[8] = size;
190 args[9] = fsPath;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800191 rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status,
192 false, true);
San Mehatfcf24fe2010-03-03 12:37:32 -0800193 } else {
194 args[7] = fsPath;
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800195 rc = android_fork_execvp(8, (char **)args, &status, false,
196 true);
San Mehatfcf24fe2010-03-03 12:37:32 -0800197 }
San Mehatbf041852010-01-04 10:09:16 -0800198
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800199 if (rc != 0) {
200 SLOGE("Filesystem format failed due to logwrap error");
201 errno = EIO;
202 return -1;
203 }
204
205 if (!WIFEXITED(status)) {
206 SLOGE("Filesystem format did not exit properly");
207 errno = EIO;
208 return -1;
209 }
210
211 status = WEXITSTATUS(status);
212
213 if (status == 0) {
San Mehat97ac40e2010-03-24 10:24:19 -0700214 SLOGI("Filesystem formatted OK");
San Mehatbf041852010-01-04 10:09:16 -0800215 return 0;
216 } else {
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -0800217 SLOGE("Format failed (unknown exit code %d)", status);
San Mehatbf041852010-01-04 10:09:16 -0800218 errno = EIO;
219 return -1;
220 }
221 return 0;
222}