OSDN Git Service

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