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 #ifndef ANDROID_VOLD_DISK_H
18 #define ANDROID_VOLD_DISK_H
19 
20 #include "StubVolume.h"
21 #include "Utils.h"
22 #include "VolumeBase.h"
23 
24 #include <utils/Errors.h>
25 
26 #include <vector>
27 
28 namespace android {
29 namespace vold {
30 
31 class VolumeBase;
32 
33 /*
34  * Representation of detected physical media.
35  *
36  * Knows how to create volumes based on the partition tables found, and also
37  * how to repartition itself.
38  */
39 class Disk {
40   public:
41     Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags);
42     virtual ~Disk();
43 
44     enum Flags {
45         /* Flag that disk is adoptable */
46         kAdoptable = 1 << 0,
47         /* Flag that disk is considered primary when the user hasn't
48          * explicitly picked a primary storage location */
49         kDefaultPrimary = 1 << 1,
50         /* Flag that disk is SD card */
51         kSd = 1 << 2,
52         /* Flag that disk is USB disk */
53         kUsb = 1 << 3,
54         /* Flag that disk is EMMC internal */
55         kEmmc = 1 << 4,
56         /* Flag that disk is an invisible Stub disk, i.e., disk that is managed from outside
57          * Android (e.g., ARC++) and invisible to apps. */
58         kStubInvisible = 1 << 5,
59         /* Flag that disk is a visible Stub disk, i.e., disk that is managed from outside
60          * Android (e.g., ARC++) and visible to apps. */
61         kStubVisible = 1 << 6,
62     };
63 
getId()64     const std::string& getId() const { return mId; }
getEventPath()65     const std::string& getEventPath() const { return mEventPath; }
getSysPath()66     const std::string& getSysPath() const { return mSysPath; }
getDevPath()67     const std::string& getDevPath() const { return mDevPath; }
getDevice()68     dev_t getDevice() const { return mDevice; }
getSize()69     uint64_t getSize() const { return mSize; }
getLabel()70     const std::string& getLabel() const { return mLabel; }
getFlags()71     int getFlags() const { return mFlags; }
72 
73     std::shared_ptr<VolumeBase> findVolume(const std::string& id);
74 
75     void listVolumes(VolumeBase::Type type, std::list<std::string>& list) const;
76 
77     std::vector<std::shared_ptr<VolumeBase>> getVolumes() const;
78 
79     status_t create();
80     status_t destroy();
81 
82     status_t readMetadata();
83     status_t readPartitions();
84     void initializePartition(std::shared_ptr<StubVolume> vol);
85 
86     status_t unmountAll();
87 
88     status_t partitionPublic();
89     status_t partitionPrivate();
90     status_t partitionMixed(int8_t ratio);
91 
92   private:
93     /* ID that uniquely references this disk */
94     std::string mId;
95     /* Original event path */
96     std::string mEventPath;
97     /* Device path under sysfs */
98     std::string mSysPath;
99     /* Device path under dev */
100     std::string mDevPath;
101     /* Kernel device representing disk */
102     dev_t mDevice;
103     /* Size of disk, in bytes */
104     uint64_t mSize;
105     /* User-visible label, such as manufacturer */
106     std::string mLabel;
107     /* Current partitions on disk */
108     std::vector<std::shared_ptr<VolumeBase>> mVolumes;
109     /* Nickname for this disk */
110     std::string mNickname;
111     /* Flags applicable to this disk */
112     int mFlags;
113     /* Flag indicating object is created */
114     bool mCreated;
115     /* Flag that we just partitioned and should format all volumes */
116     bool mJustPartitioned;
117 
118     void createPublicVolume(dev_t device);
119     void createPrivateVolume(dev_t device, const std::string& partGuid);
120     void createStubVolume();
121 
122     void destroyAllVolumes();
123 
124     int getMaxMinors();
125 
isStub()126     bool isStub() { return (mFlags & kStubInvisible) || (mFlags & kStubVisible); }
127 
128     DISALLOW_COPY_AND_ASSIGN(Disk);
129 };
130 
131 }  // namespace vold
132 }  // namespace android
133 
134 #endif
135