OSDN Git Service

Fix ext4/f2fs can't be mounted issue
[android-x86/system-vold.git] / fs / Ext4.cpp
1 /*
2  * Copyright (C) 2012 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>
18 #include <stdlib.h>
19 #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 #include <vector>
27 #include <string>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 #include <sys/mount.h>
34 #include <sys/wait.h>
35
36 #include <linux/kdev_t.h>
37
38 #define LOG_TAG "Vold"
39 #include <private/android_filesystem_config.h>
40 #include <android-base/logging.h>
41 #include <android-base/stringprintf.h>
42 #include <cutils/log.h>
43 #include <cutils/properties.h>
44 #include <logwrap/logwrap.h>
45 #include <selinux/selinux.h>
46
47 #include "Ext4.h"
48 #include "Utils.h"
49 #include "VoldUtil.h"
50
51 using android::base::StringPrintf;
52
53 namespace android {
54 namespace vold {
55 namespace ext4 {
56
57 static const char* kResizefsPath = "/system/bin/resize2fs";
58 #ifdef TARGET_USES_MKE2FS
59 static const char* kMkfsPath = "/system/bin/mke2fs";
60 #else
61 static const char* kMkfsPath = "/system/bin/make_ext4fs";
62 #endif
63 static const char* kFsckPath = "/system/bin/e2fsck";
64
65 bool IsSupported() {
66     return access(kMkfsPath, X_OK) == 0
67             && access(kFsckPath, X_OK) == 0
68             && IsFilesystemSupported("ext4");
69 }
70
71 status_t Check(const std::string& source, const std::string& target, bool trusted) {
72     // The following is shamelessly borrowed from fs_mgr.c, so it should be
73     // kept in sync with any changes over there.
74
75     const char* c_source = source.c_str();
76     const char* c_target = target.c_str();
77
78     int status;
79     int ret;
80     long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
81     char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
82
83     /*
84      * First try to mount and unmount the filesystem.  We do this because
85      * the kernel is more efficient than e2fsck in running the journal and
86      * processing orphaned inodes, and on at least one device with a
87      * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
88      * to do what the kernel does in about a second.
89      *
90      * After mounting and unmounting the filesystem, run e2fsck, and if an
91      * error is recorded in the filesystem superblock, e2fsck will do a full
92      * check.  Otherwise, it does nothing.  If the kernel cannot mount the
93      * filesytsem due to an error, e2fsck is still run to do a full check
94      * fix the filesystem.
95      */
96     ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
97     if (!ret) {
98         int i;
99         for (i = 0; i < 5; i++) {
100             // Try to umount 5 times before continuing on.
101             // Should we try rebooting if all attempts fail?
102             int result = umount(c_target);
103             if (result == 0) {
104                 break;
105             }
106             ALOGW("%s(): umount(%s)=%d: %s\n", __func__, c_target, result, strerror(errno));
107             sleep(1);
108         }
109     }
110
111     /*
112      * Some system images do not have e2fsck for licensing reasons
113      * (e.g. recent SDK system images). Detect these and skip the check.
114      */
115     if (access(kFsckPath, X_OK)) {
116         ALOGD("Not running %s on %s (executable not in system image)\n",
117                 kFsckPath, c_source);
118     } else {
119         ALOGD("Running %s on %s\n", kFsckPath, c_source);
120
121         std::vector<std::string> cmd;
122         cmd.push_back(kFsckPath);
123         cmd.push_back("-y");
124         cmd.push_back(c_source);
125
126         return ForkExecvp(cmd, trusted ? sFsckContext : sFsckUntrustedContext);
127     }
128
129     return 0;
130 }
131
132 status_t Mount(const std::string& source, const std::string& target, bool ro,
133         bool remount, bool executable, const std::string& opts /* = "" */, bool portable) {
134     int rc;
135     unsigned long flags;
136
137     std::string data(opts);
138
139 #ifdef LINEAGE_BUILD
140     if (portable) {
141         if (!data.empty()) {
142             data += ",";
143         }
144         data += "context=u:object_r:sdcard_posix:s0";
145     }
146 #endif
147     const char* c_source = source.c_str();
148     const char* c_target = target.c_str();
149     const char* c_data = data.c_str();
150
151     flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
152
153     flags |= (executable ? 0 : MS_NOEXEC);
154     flags |= (ro ? MS_RDONLY : 0);
155     flags |= (remount ? MS_REMOUNT : 0);
156
157     rc = mount(c_source, c_target, "ext4", flags, c_data);
158     if (portable && rc == 0) {
159         chown(c_target, AID_MEDIA_RW, AID_MEDIA_RW);
160         chmod(c_target, 0755);
161     }
162
163     if (rc && errno == EROFS) {
164         SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source);
165         flags |= MS_RDONLY;
166         rc = mount(c_source, c_target, "ext4", flags, c_data);
167     }
168
169     return rc;
170 }
171
172 status_t Resize(const std::string& source, unsigned long numSectors) {
173     std::vector<std::string> cmd;
174     cmd.push_back(kResizefsPath);
175     cmd.push_back("-f");
176     cmd.push_back(source);
177     cmd.push_back(StringPrintf("%lu", numSectors));
178
179     return ForkExecvp(cmd);
180 }
181
182 status_t Format(const std::string& source, unsigned long numSectors,
183         const std::string& target) {
184     std::vector<std::string> cmd;
185     cmd.push_back(kMkfsPath);
186
187 #ifdef TARGET_USES_MKE2FS
188     cmd.push_back("-b");
189     cmd.push_back("4096");
190
191     cmd.push_back("-t");
192     cmd.push_back("ext4");
193
194     cmd.push_back("-M");
195     cmd.push_back(target);
196
197     cmd.push_back("-O");
198     cmd.push_back("^has_journal");
199
200     cmd.push_back(source);
201
202     if (numSectors)
203         cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
204 #else
205     cmd.push_back("-J");
206
207     cmd.push_back("-a");
208     cmd.push_back(target);
209
210     if (numSectors) {
211         cmd.push_back("-l");
212         cmd.push_back(StringPrintf("%lu", numSectors * 512));
213     }
214
215     // Always generate a real UUID
216     cmd.push_back("-u");
217     cmd.push_back(source);
218 #endif
219
220     return ForkExecvp(cmd);
221 }
222
223 }  // namespace ext4
224 }  // namespace vold
225 }  // namespace android