1deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey/*
2deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Copyright (C) 2008 The Android Open Source Project
3deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
4deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * you may not use this file except in compliance with the License.
6deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * You may obtain a copy of the License at
7deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
8deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
10deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Unless required by applicable law or agreed to in writing, software
11deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * See the License for the specific language governing permissions and
14deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * limitations under the License.
15deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey */
16deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
17deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#ifndef ANDROID_VOLD_VOLUME_BASE_H
18deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#define ANDROID_VOLD_VOLUME_BASE_H
19deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
20deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#include "Utils.h"
21deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
2236801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey#include <cutils/multiuser.h>
23deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#include <utils/Errors.h>
24deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
25deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#include <sys/types.h>
26deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#include <list>
27deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#include <string>
28deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
29deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeynamespace android {
30deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeynamespace vold {
31deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
32deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey/*
33deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Representation of a mounted volume ready for presentation.
34deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
35deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Various subclasses handle the different mounting prerequisites, such as
36deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * encryption details, etc.  Volumes can also be "stacked" above other
37deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * volumes to help communicate dependencies.  For example, an ASEC volume
38deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * can be stacked on a vfat volume.
39deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
40deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * Mounted volumes can be asked to manage bind mounts to present themselves
41deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * to specific users on the device.
42deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey *
43deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * When an unmount is requested, the volume recursively unmounts any stacked
44deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey * volumes and removes any bind mounts before finally unmounting itself.
45deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey */
46deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeyclass VolumeBase {
47deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeypublic:
48deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    virtual ~VolumeBase();
49deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
5036801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    enum class Type {
5136801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kPublic = 0,
5236801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kPrivate,
5336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kEmulated,
5436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kAsec,
5536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kObb,
5636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    };
5736801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
58f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    enum MountFlags {
5936801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        /* Flag that volume is primary external storage */
6036801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kPrimary = 1 << 0,
6136801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        /* Flag that volume is visible to normal apps */
6236801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kVisible = 1 << 1,
6336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    };
6436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
6536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    enum class State {
6636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kUnmounted = 0,
67f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey        kChecking,
6836801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kMounted,
69f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey        kMountedReadOnly,
7036801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey        kFormatting,
71f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey        kEjecting,
720fd9535474baf9224bc75ad6e9a63dfbfac3aaf7Jeff Sharkey        kUnmountable,
733161fb3702830b586b2e36fa9ca4519f59f951b4Jeff Sharkey        kRemoved,
74f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey        kBadRemoval,
7536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    };
7636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
77deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    const std::string& getId() { return mId; }
78f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    const std::string& getDiskId() { return mDiskId; }
79bc40cc8f07f69e0e26fc41516e2a83f0a8becbe0Jeff Sharkey    const std::string& getPartGuid() { return mPartGuid; }
8036801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    Type getType() { return mType; }
81f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    int getMountFlags() { return mMountFlags; }
82f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    userid_t getMountUserId() { return mMountUserId; }
8336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    State getState() { return mState; }
8436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    const std::string& getPath() { return mPath; }
851d6fbcc389ecb9f418076e8ab5f4c93a5d911de9Jeff Sharkey    const std::string& getInternalPath() { return mInternalPath; }
8636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
87f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    status_t setDiskId(const std::string& diskId);
88bc40cc8f07f69e0e26fc41516e2a83f0a8becbe0Jeff Sharkey    status_t setPartGuid(const std::string& partGuid);
89f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    status_t setMountFlags(int mountFlags);
90f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    status_t setMountUserId(userid_t mountUserId);
91ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    status_t setSilent(bool silent);
92deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
9336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    void addVolume(const std::shared_ptr<VolumeBase>& volume);
9436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    void removeVolume(const std::shared_ptr<VolumeBase>& volume);
9536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
9636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    std::shared_ptr<VolumeBase> findVolume(const std::string& id);
9736801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
9836801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    status_t create();
9936801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    status_t destroy();
100deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    status_t mount();
101deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    status_t unmount();
102d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    status_t format(const std::string& fsType);
103deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
104deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeyprotected:
10536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    explicit VolumeBase(Type type);
106deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
1079c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    virtual status_t doCreate();
1089c48498f4529f623650c56d03e63324c8d813032Jeff Sharkey    virtual status_t doDestroy();
109deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    virtual status_t doMount() = 0;
110deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    virtual status_t doUnmount() = 0;
111d0640f6358041f7e2657167560b357078db73526Jeff Sharkey    virtual status_t doFormat(const std::string& fsType);
112deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
11336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    status_t setId(const std::string& id);
11436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    status_t setPath(const std::string& path);
1151d6fbcc389ecb9f418076e8ab5f4c93a5d911de9Jeff Sharkey    status_t setInternalPath(const std::string& internalPath);
11636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey
117ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    void notifyEvent(int msg);
118ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    void notifyEvent(int msg, const std::string& value);
119ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey
120deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkeyprivate:
12136801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    /* ID that uniquely references volume while alive */
12236801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    std::string mId;
123f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    /* ID that uniquely references parent disk while alive */
124f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    std::string mDiskId;
125bc40cc8f07f69e0e26fc41516e2a83f0a8becbe0Jeff Sharkey    /* Partition GUID of this volume */
126bc40cc8f07f69e0e26fc41516e2a83f0a8becbe0Jeff Sharkey    std::string mPartGuid;
127deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    /* Volume type */
12836801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    Type mType;
129f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    /* Flags used when mounting this volume */
130f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    int mMountFlags;
13136801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    /* User that owns this volume, otherwise -1 */
132f1b996df6f8283aac6953b22bd9e2496d8c30c86Jeff Sharkey    userid_t mMountUserId;
13336801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    /* Flag indicating object is created */
13436801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    bool mCreated;
135deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    /* Current state of volume */
13636801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    State mState;
13736801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    /* Path to mounted volume */
13836801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    std::string mPath;
1391d6fbcc389ecb9f418076e8ab5f4c93a5d911de9Jeff Sharkey    /* Path to internal backing storage */
1401d6fbcc389ecb9f418076e8ab5f4c93a5d911de9Jeff Sharkey    std::string mInternalPath;
141ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    /* Flag indicating that volume should emit no events */
142ce6a913aeac7db94a41362c63bab74092767bb3eJeff Sharkey    bool mSilent;
143deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
144deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    /* Volumes stacked on top of this volume */
14536801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    std::list<std::shared_ptr<VolumeBase>> mVolumes;
146deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
14736801cccf27152c9eca5aab6ba3527221525110fJeff Sharkey    void setState(State state);
148deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
149deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey    DISALLOW_COPY_AND_ASSIGN(VolumeBase);
150deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey};
151deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
152deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey}  // namespace vold
153deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey}  // namespace android
154deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey
155deb240573754daf36fa8ea10a05240f9f31e7b2cJeff Sharkey#endif
156