F2fs.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2015 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 "F2fs.h"
  17. #include "Utils.h"
  18. #include <android-base/logging.h>
  19. #include <android-base/properties.h>
  20. #include <android-base/stringprintf.h>
  21. #include <fscrypt/fscrypt.h>
  22. #include <string>
  23. #include <vector>
  24. #include <sys/mount.h>
  25. using android::base::StringPrintf;
  26. namespace android {
  27. namespace vold {
  28. namespace f2fs {
  29. static const char* kMkfsPath = "/system/bin/make_f2fs";
  30. static const char* kFsckPath = "/system/bin/fsck.f2fs";
  31. bool IsSupported() {
  32. return access(kMkfsPath, X_OK) == 0 && access(kFsckPath, X_OK) == 0 &&
  33. IsFilesystemSupported("f2fs");
  34. }
  35. status_t Check(const std::string& source) {
  36. std::vector<std::string> cmd;
  37. cmd.push_back(kFsckPath);
  38. cmd.push_back("-a");
  39. cmd.push_back(source);
  40. // f2fs devices are currently always trusted
  41. return ForkExecvp(cmd, nullptr, sFsckContext);
  42. }
  43. status_t Mount(const std::string& source, const std::string& target) {
  44. const char* c_source = source.c_str();
  45. const char* c_target = target.c_str();
  46. unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
  47. int res = mount(c_source, c_target, "f2fs", flags, NULL);
  48. if (res != 0) {
  49. PLOG(ERROR) << "Failed to mount " << source;
  50. if (errno == EROFS) {
  51. res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
  52. if (res != 0) {
  53. PLOG(ERROR) << "Failed to mount read-only " << source;
  54. }
  55. }
  56. }
  57. return res;
  58. }
  59. status_t Format(const std::string& source) {
  60. std::vector<std::string> cmd;
  61. cmd.push_back(kMkfsPath);
  62. cmd.push_back("-f");
  63. cmd.push_back("-d1");
  64. if (android::base::GetBoolProperty("vold.has_quota", false)) {
  65. cmd.push_back("-O");
  66. cmd.push_back("quota");
  67. }
  68. if (fscrypt_is_native()) {
  69. cmd.push_back("-O");
  70. cmd.push_back("encrypt");
  71. }
  72. cmd.push_back("-O");
  73. cmd.push_back("verity");
  74. cmd.push_back(source);
  75. return ForkExecvp(cmd);
  76. }
  77. } // namespace f2fs
  78. } // namespace vold
  79. } // namespace android