Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 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> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <string> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 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> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 34 | #include <sys/wait.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 35 | |
| 36 | #include <linux/kdev_t.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 37 | |
| 38 | #define LOG_TAG "Vold" |
| 39 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 40 | #include <base/logging.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 41 | #include <base/stringprintf.h> |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 42 | #include <cutils/log.h> |
| 43 | #include <cutils/properties.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 44 | #include <logwrap/logwrap.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 45 | #include <selinux/selinux.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 46 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 47 | #include "Ext4.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 48 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 49 | #include "VoldUtil.h" |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 51 | using android::base::StringPrintf; |
| 52 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 53 | static const char* kResizefsPath = "/system/bin/resize2fs"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 54 | static const char* kMkfsPath = "/system/bin/make_ext4fs"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 55 | static const char* kFsckPath = "/system/bin/e2fsck"; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 56 | |
| 57 | int 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 Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 68 | |
| 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 Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 107 | std::vector<std::string> cmd; |
| 108 | cmd.push_back(kFsckPath); |
| 109 | cmd.push_back("-y"); |
| 110 | cmd.push_back(blk_device); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 111 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 112 | // Ext4 devices are currently always trusted |
| 113 | return android::vold::ForkExecvp(cmd, android::vold::sFsckContext); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 119 | int 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 Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 141 | int Ext4::resize(const char *fspath, unsigned int numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 142 | 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 Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 147 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 148 | return android::vold::ForkExecvp(cmd); |
Daniel Rosenberg | fcd34a0 | 2014-05-22 11:23:56 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 151 | int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 152 | 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 Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 159 | if (numSectors) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 160 | cmd.push_back("-l"); |
| 161 | cmd.push_back(StringPrintf("%u", numSectors * 512)); |
Daniel Rosenberg | 6a74dca | 2014-05-23 13:47:00 -0700 | [diff] [blame] | 162 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 163 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 164 | // Always generate a real UUID |
| 165 | cmd.push_back("-u"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 166 | cmd.push_back(fsPath); |
| 167 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 168 | return android::vold::ForkExecvp(cmd); |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 169 | } |