1d0640f6358041f7e2657167560b357078db73526Jeff Sharkey/*
2d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * Copyright (C) 2015 The Android Open Source Project
3d0640f6358041f7e2657167560b357078db73526Jeff Sharkey *
4d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * you may not use this file except in compliance with the License.
6d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * You may obtain a copy of the License at
7d0640f6358041f7e2657167560b357078db73526Jeff Sharkey *
8d0640f6358041f7e2657167560b357078db73526Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9d0640f6358041f7e2657167560b357078db73526Jeff Sharkey *
10d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
11d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * See the License for the specific language governing permissions and
14d0640f6358041f7e2657167560b357078db73526Jeff Sharkey * limitations under the License.
15d0640f6358041f7e2657167560b357078db73526Jeff Sharkey */
16d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
17d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include "F2fs.h"
18d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include "Utils.h"
19d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
20d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include <base/logging.h>
21d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include <base/stringprintf.h>
22d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
23d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include <vector>
24d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include <string>
25d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
26d0640f6358041f7e2657167560b357078db73526Jeff Sharkey#include <sys/mount.h>
27d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
28d0640f6358041f7e2657167560b357078db73526Jeff Sharkeyusing android::base::StringPrintf;
29d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
30d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace android {
31d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace vold {
32d0640f6358041f7e2657167560b357078db73526Jeff Sharkeynamespace f2fs {
33d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
34d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatic const char* kMkfsPath = "/system/bin/make_f2fs";
35d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatic const char* kFsckPath = "/system/bin/fsck.f2fs";
36d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
37d0640f6358041f7e2657167560b357078db73526Jeff Sharkeybool IsSupported() {
38d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return access(kMkfsPath, X_OK) == 0
39d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            && access(kFsckPath, X_OK) == 0
40d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            && IsFilesystemSupported("f2fs");
41d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}
42d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
43d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Check(const std::string& source) {
44d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    std::vector<std::string> cmd;
45d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(kFsckPath);
46d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back("-f");
47d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(source);
48d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
49d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    // f2fs devices are currently always trusted
50d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return ForkExecvp(cmd, sFsckContext);
51d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}
52d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
53d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Mount(const std::string& source, const std::string& target) {
54d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_source = source.c_str();
55d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    const char* c_target = target.c_str();
56d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    unsigned long flags = MS_NOATIME | MS_NODEV | MS_NOSUID | MS_DIRSYNC;
57d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
58d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    int res = mount(c_source, c_target, "f2fs", flags, NULL);
59d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    if (res != 0) {
60d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        PLOG(ERROR) << "Failed to mount " << source;
61d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        if (errno == EROFS) {
62d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            res = mount(c_source, c_target, "f2fs", flags | MS_RDONLY, NULL);
63d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            if (res != 0) {
64d0640f6358041f7e2657167560b357078db73526Jeff Sharkey                PLOG(ERROR) << "Failed to mount read-only " << source;
65d0640f6358041f7e2657167560b357078db73526Jeff Sharkey            }
66d0640f6358041f7e2657167560b357078db73526Jeff Sharkey        }
67d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    }
68d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
69d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return res;
70d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}
71d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
72d0640f6358041f7e2657167560b357078db73526Jeff Sharkeystatus_t Format(const std::string& source) {
73d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    std::vector<std::string> cmd;
74d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(kMkfsPath);
75d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    cmd.push_back(source);
76d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
77d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    return ForkExecvp(cmd);
78d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}
79d0640f6358041f7e2657167560b357078db73526Jeff Sharkey
80d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace f2fs
81d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace vold
82d0640f6358041f7e2657167560b357078db73526Jeff Sharkey}  // namespace android
83