Ext4.cpp 5.7 KB

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