blob: 04240b7ed87d7f4827dffe9fe0f91f322c1408af [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2008 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#ifndef ANDROID_VOLD_VOLUME_BASE_H
18#define ANDROID_VOLD_VOLUME_BASE_H
19
20#include "Utils.h"
21
22#include <utils/Errors.h>
23
24#include <sys/types.h>
25#include <list>
26#include <string>
27
28namespace android {
29namespace vold {
30
31enum class VolumeState {
32 kUnmounted,
33 kMounting,
34 kMounted,
35 kCorrupt,
36 kFormatting,
37 kUnmounting,
38};
39
40enum class VolumeType {
41 kPublic,
42 kPrivate,
43 kEmulated,
44 kAsec,
45 kObb,
46};
47
48// events:
49// volume_created private:127:4
50// volume_state private:127:4 mounted
51// volume_meta private:127:4 [fsGuid] [label]
52// volume_destroyed public:127:4
53
54// commands:
55// volume mount public:127:4 [primary]
56// volume unmount public:127:4
57// volume bind_user public:127:4 [userId]
58// volume unbind_user public:127:4 [userId]
59// volume bind_package private:4:1 [userId] [package]
60// volume unbind_package private:4:1 [userId] [package]
61
62/*
63 * Representation of a mounted volume ready for presentation.
64 *
65 * Various subclasses handle the different mounting prerequisites, such as
66 * encryption details, etc. Volumes can also be "stacked" above other
67 * volumes to help communicate dependencies. For example, an ASEC volume
68 * can be stacked on a vfat volume.
69 *
70 * Mounted volumes can be asked to manage bind mounts to present themselves
71 * to specific users on the device.
72 *
73 * When an unmount is requested, the volume recursively unmounts any stacked
74 * volumes and removes any bind mounts before finally unmounting itself.
75 */
76class VolumeBase {
77public:
78 virtual ~VolumeBase();
79
80 VolumeType getType() { return mType; }
81 const std::string& getId() { return mId; }
82 VolumeState getState() { return mState; }
83
84 void stackVolume(const std::shared_ptr<VolumeBase>& volume);
85 void unstackVolume(const std::shared_ptr<VolumeBase>& volume);
86
87 status_t mount();
88 status_t unmount();
89 status_t format();
90
91protected:
92 explicit VolumeBase(VolumeType type);
93
94 /* ID that uniquely references this disk */
95 std::string mId;
96
97 /* Manage bind mounts for this volume */
98 status_t mountBind(const std::string& source, const std::string& target);
99 status_t unmountBind(const std::string& target);
100
101 virtual status_t doMount() = 0;
102 virtual status_t doUnmount() = 0;
103 virtual status_t doFormat();
104
105private:
106 /* Volume type */
107 VolumeType mType;
108 /* Current state of volume */
109 VolumeState mState;
110
111 /* Volumes stacked on top of this volume */
112 std::list<std::shared_ptr<VolumeBase>> mStacked;
113 /* Currently active bind mounts */
114 std::list<std::string> mBindTargets;
115
116 void setState(VolumeState state);
117
118 DISALLOW_COPY_AND_ASSIGN(VolumeBase);
119};
120
121} // namespace vold
122} // namespace android
123
124#endif