blob: 8260563c2689eb2e3ad9d9a882d2c6736fe9e307 [file] [log] [blame]
Tom Marshallbead2612019-01-04 14:37:31 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Copyright (C) 2019 The LineageOS Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "EmulatedVolume.h"
19#include <volume_manager/VolumeManager.h>
20#include "ResponseCode.h"
21#include "Utils.h"
22
23#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
25#include <cutils/fs.h>
26#include <private/android_filesystem_config.h>
27
28#include <fcntl.h>
29#include <stdlib.h>
30#include <sys/mount.h>
31#include <sys/stat.h>
32#include <sys/sysmacros.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
36using android::base::StringPrintf;
37
38namespace android {
39namespace volmgr {
40
41static const std::string kStagingPath = "/mnt/staging/emulated";
42
43EmulatedVolume::EmulatedVolume(FstabEntry* rec, const std::string& subdir)
44 : VolumeBase(Type::kEmulated),
45 mSubdir(subdir),
46 mDevPath(rec->blk_device),
47 mFsType(rec->fs_type),
48 mFlags(rec->flags),
49 mFsOptions(rec->fs_options) {
50 setId("emulated");
51 setPartLabel("internal storage");
52 setPath("/storage/emulated");
53}
54
55EmulatedVolume::~EmulatedVolume() {}
56
57status_t EmulatedVolume::doMount() {
58 if (fs_prepare_dir(kStagingPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
59 PLOG(ERROR) << getId() << " failed to create mount points";
60 return -errno;
61 }
62 if (fs_prepare_dir(getPath().c_str(), 0700, AID_ROOT, AID_ROOT)) {
63 PLOG(ERROR) << getId() << " failed to create mount points";
64 return -errno;
65 }
66
67 std::string bindPath = kStagingPath + "/" + mSubdir;
68
69 if (::mount(mDevPath.c_str(), kStagingPath.c_str(), mFsType.c_str(), mFlags,
70 mFsOptions.c_str()) != 0) {
71 PLOG(ERROR) << getId() << " failed to mount " << mDevPath << " on " << kStagingPath;
72 return -EIO;
73 }
74 if (BindMount(bindPath, getPath()) != OK) {
75 PLOG(ERROR) << getId() << " failed to bind mount " << bindPath << " on " << getPath();
76 ForceUnmount(kStagingPath);
77 return -EIO;
78 }
79
80 return OK;
81}
82
83status_t EmulatedVolume::doUnmount(bool detach /* = false */) {
84 ForceUnmount(getPath(), detach);
85 ForceUnmount(kStagingPath, detach);
86
87 rmdir(getPath().c_str());
88 rmdir(kStagingPath.c_str());
89
90 return OK;
91}
92
93} // namespace volmgr
94} // namespace android