blob: a208eb594ee9bfd538775b8031a9bc45159bff55 [file] [log] [blame]
Kenny Root344ca102012-04-03 17:23:01 -07001/*
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>
Jeff Sharkey9c484982015-03-31 10:35:33 -070026#include <vector>
27#include <string>
Kenny Root344ca102012-04-03 17:23:01 -070028
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>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080034#include <sys/wait.h>
Kenny Root344ca102012-04-03 17:23:01 -070035
36#include <linux/kdev_t.h>
Kenny Root344ca102012-04-03 17:23:01 -070037
38#define LOG_TAG "Vold"
39
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070040#include <base/logging.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070041#include <base/stringprintf.h>
Kenny Root344ca102012-04-03 17:23:01 -070042#include <cutils/log.h>
43#include <cutils/properties.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080044#include <logwrap/logwrap.h>
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070045#include <selinux/selinux.h>
Rom Lemarchand2ba45aa2013-01-16 12:29:28 -080046
Kenny Root344ca102012-04-03 17:23:01 -070047#include "Ext4.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070048#include "Utils.h"
Rom Lemarchand5593c852012-12-21 12:41:43 -080049#include "VoldUtil.h"
Kenny Root344ca102012-04-03 17:23:01 -070050
Jeff Sharkey9c484982015-03-31 10:35:33 -070051using android::base::StringPrintf;
52
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070053static const char* kResizefsPath = "/system/bin/resize2fs";
Jeff Sharkey9c484982015-03-31 10:35:33 -070054static const char* kMkfsPath = "/system/bin/make_ext4fs";
Jeff Sharkey9c484982015-03-31 10:35:33 -070055static const char* kFsckPath = "/system/bin/e2fsck";
Jeff Sharkey9c484982015-03-31 10:35:33 -070056
57int Ext4::check(const char *fsPath, const char *mountPoint) {
58 // The following is shamelessly borrowed from fs_mgr.c, so it should be
59 // kept in sync with any changes over there.
60
61 char* blk_device = (char*) fsPath;
62 char* target = (char*) mountPoint;
63
64 int status;
65 int ret;
66 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
67 char *tmpmnt_opts = (char*) "nomblk_io_submit,errors=remount-ro";
Jeff Sharkey9c484982015-03-31 10:35:33 -070068
69 /*
70 * First try to mount and unmount the filesystem. We do this because
71 * the kernel is more efficient than e2fsck in running the journal and
72 * processing orphaned inodes, and on at least one device with a
73 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
74 * to do what the kernel does in about a second.
75 *
76 * After mounting and unmounting the filesystem, run e2fsck, and if an
77 * error is recorded in the filesystem superblock, e2fsck will do a full
78 * check. Otherwise, it does nothing. If the kernel cannot mount the
79 * filesytsem due to an error, e2fsck is still run to do a full check
80 * fix the filesystem.
81 */
82 ret = mount(blk_device, target, "ext4", tmpmnt_flags, tmpmnt_opts);
83 if (!ret) {
84 int i;
85 for (i = 0; i < 5; i++) {
86 // Try to umount 5 times before continuing on.
87 // Should we try rebooting if all attempts fail?
88 int result = umount(target);
89 if (result == 0) {
90 break;
91 }
92 ALOGW("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
93 sleep(1);
94 }
95 }
96
97 /*
98 * Some system images do not have e2fsck for licensing reasons
99 * (e.g. recent SDK system images). Detect these and skip the check.
100 */
101 if (access(kFsckPath, X_OK)) {
102 ALOGD("Not running %s on %s (executable not in system image)\n",
103 kFsckPath, blk_device);
104 } else {
105 ALOGD("Running %s on %s\n", kFsckPath, blk_device);
106
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700107 std::vector<std::string> cmd;
108 cmd.push_back(kFsckPath);
109 cmd.push_back("-y");
110 cmd.push_back(blk_device);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700111
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700112 // Ext4 devices are currently always trusted
113 return android::vold::ForkExecvp(cmd, android::vold::sFsckContext);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700114 }
115
116 return 0;
117}
118
Kenny Root344ca102012-04-03 17:23:01 -0700119int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
120 bool executable) {
121 int rc;
122 unsigned long flags;
123
124 flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
125
126 flags |= (executable ? 0 : MS_NOEXEC);
127 flags |= (ro ? MS_RDONLY : 0);
128 flags |= (remount ? MS_REMOUNT : 0);
129
130 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
131
132 if (rc && errno == EROFS) {
133 SLOGE("%s appears to be a read only filesystem - retrying mount RO", fsPath);
134 flags |= MS_RDONLY;
135 rc = mount(fsPath, mountPoint, "ext4", flags, NULL);
136 }
137
138 return rc;
139}
140
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700141int Ext4::resize(const char *fspath, unsigned int numSectors) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700142 std::vector<std::string> cmd;
143 cmd.push_back(kResizefsPath);
144 cmd.push_back("-f");
145 cmd.push_back(fspath);
146 cmd.push_back(StringPrintf("%u", numSectors));
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700147
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700148 return android::vold::ForkExecvp(cmd);
Daniel Rosenbergfcd34a02014-05-22 11:23:56 -0700149}
150
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700151int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700152 std::vector<std::string> cmd;
153 cmd.push_back(kMkfsPath);
154 cmd.push_back("-J");
155
156 cmd.push_back("-a");
157 cmd.push_back(mountpoint);
158
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700159 if (numSectors) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700160 cmd.push_back("-l");
161 cmd.push_back(StringPrintf("%u", numSectors * 512));
Daniel Rosenberg6a74dca2014-05-23 13:47:00 -0700162 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700163
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700164 // Always generate a real UUID
165 cmd.push_back("-u");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700166 cmd.push_back(fsPath);
167
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700168 return android::vold::ForkExecvp(cmd);
Kenny Root344ca102012-04-03 17:23:01 -0700169}