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