• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dirent.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <string>
25 #include <vector>
26 
27 #include <sys/mman.h>
28 #include <sys/mount.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 
33 #include <linux/kdev_t.h>
34 
35 #include <android-base/logging.h>
36 #include <android-base/properties.h>
37 #include <android-base/stringprintf.h>
38 #include <cutils/properties.h>
39 #include <fscrypt/fscrypt.h>
40 #include <logwrap/logwrap.h>
41 #include <selinux/selinux.h>
42 
43 #include "Ext4.h"
44 #include "FsCrypt.h"
45 #include "Utils.h"
46 #include "VoldUtil.h"
47 
48 using android::base::StringPrintf;
49 
50 namespace android {
51 namespace vold {
52 namespace ext4 {
53 
54 static const char* kResizefsPath = "/system/bin/resize2fs";
55 static const char* kMkfsPath = "/system/bin/mke2fs";
56 static const char* kFsckPath = "/system/bin/e2fsck";
57 
IsSupported()58 bool IsSupported() {
59     return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
60            IsFilesystemSupported("ext4");
61 }
62 
Check(const std::string & source,const std::string & target)63 status_t Check(const std::string& source, const std::string& target) {
64     // The following is shamelessly borrowed from fs_mgr.c, so it should be
65     // kept in sync with any changes over there.
66 
67     const char* c_source = source.c_str();
68     const char* c_target = target.c_str();
69 
70     int status;
71     int ret;
72     long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
73     char* tmpmnt_opts = (char*)"nomblk_io_submit,errors=remount-ro";
74 
75     /*
76      * First try to mount and unmount the filesystem.  We do this because
77      * the kernel is more efficient than e2fsck in running the journal and
78      * processing orphaned inodes, and on at least one device with a
79      * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
80      * to do what the kernel does in about a second.
81      *
82      * After mounting and unmounting the filesystem, run e2fsck, and if an
83      * error is recorded in the filesystem superblock, e2fsck will do a full
84      * check.  Otherwise, it does nothing.  If the kernel cannot mount the
85      * filesytsem due to an error, e2fsck is still run to do a full check
86      * fix the filesystem.
87      */
88     ret = mount(c_source, c_target, "ext4", tmpmnt_flags, tmpmnt_opts);
89     if (!ret) {
90         int i;
91         for (i = 0; i < 5; i++) {
92             // Try to umount 5 times before continuing on.
93             // Should we try rebooting if all attempts fail?
94             int result = umount(c_target);
95             if (result == 0) {
96                 break;
97             }
98             LOG(WARNING) << __func__ << "(): umount(" << c_target << ")=" << result << ": "
99                          << strerror(errno);
100             sleep(1);
101         }
102     }
103 
104     /*
105      * Some system images do not have e2fsck for licensing reasons
106      * (e.g. recent SDK system images). Detect these and skip the check.
107      */
108     if (access(kFsckPath, X_OK)) {
109         LOG(DEBUG) << "Not running " << kFsckPath << " on " << c_source
110                    << " (executable not in system image)";
111     } else {
112         LOG(DEBUG) << "Running " << kFsckPath << " on " << c_source;
113 
114         std::vector<std::string> cmd;
115         cmd.push_back(kFsckPath);
116         cmd.push_back("-y");
117         cmd.push_back(c_source);
118 
119         // ext4 devices are currently always trusted
120         return ForkExecvp(cmd, nullptr, sFsckContext);
121     }
122 
123     return 0;
124 }
125 
Mount(const std::string & source,const std::string & target,bool ro,bool remount,bool executable)126 status_t Mount(const std::string& source, const std::string& target, bool ro, bool remount,
127                bool executable) {
128     int rc;
129     unsigned long flags;
130 
131     const char* c_source = source.c_str();
132     const char* c_target = target.c_str();
133 
134     flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
135 
136     flags |= (executable ? 0 : MS_NOEXEC);
137     flags |= (ro ? MS_RDONLY : 0);
138     flags |= (remount ? MS_REMOUNT : 0);
139 
140     rc = mount(c_source, c_target, "ext4", flags, NULL);
141 
142     if (rc && errno == EROFS) {
143         LOG(ERROR) << source << " appears to be a read only filesystem - retrying mount RO";
144         flags |= MS_RDONLY;
145         rc = mount(c_source, c_target, "ext4", flags, NULL);
146     }
147 
148     return rc;
149 }
150 
Resize(const std::string & source,unsigned long numSectors)151 status_t Resize(const std::string& source, unsigned long numSectors) {
152     std::vector<std::string> cmd;
153     cmd.push_back(kResizefsPath);
154     cmd.push_back("-f");
155     cmd.push_back(source);
156     cmd.push_back(StringPrintf("%lu", numSectors));
157 
158     return ForkExecvp(cmd);
159 }
160 
Format(const std::string & source,unsigned long numSectors,const std::string & target)161 status_t Format(const std::string& source, unsigned long numSectors, const std::string& target) {
162     std::vector<std::string> cmd;
163     cmd.push_back(kMkfsPath);
164 
165     cmd.push_back("-b");
166     cmd.push_back("4096");
167 
168     cmd.push_back("-t");
169     cmd.push_back("ext4");
170 
171     cmd.push_back("-M");
172     cmd.push_back(target);
173 
174     bool needs_casefold =
175             android::base::GetBoolProperty("external_storage.casefold.enabled", false);
176     bool needs_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false);
177 
178     if (needs_projid) {
179         cmd.push_back("-I");
180         cmd.push_back("512");
181     }
182 
183     std::string options("has_journal");
184     if (android::base::GetBoolProperty("vold.has_quota", false)) {
185         options += ",quota";
186     }
187     if (fscrypt_is_native()) {
188         options += ",encrypt";
189     }
190     if (needs_casefold) {
191         options += ",casefold";
192     }
193 
194     cmd.push_back("-O");
195     cmd.push_back(options);
196 
197     if (needs_casefold || needs_projid) {
198         cmd.push_back("-E");
199         std::string extopts = "";
200         if (needs_casefold) extopts += "encoding=utf8,";
201         if (needs_projid) extopts += "quotatype=prjquota,";
202         cmd.push_back(extopts);
203     }
204 
205     cmd.push_back(source);
206 
207     if (numSectors) {
208         cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
209     }
210 
211     return ForkExecvp(cmd);
212 }
213 
214 }  // namespace ext4
215 }  // namespace vold
216 }  // namespace android
217