blob: 99c98fc049303d0e5658f07d0287ecedc930d77c [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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
Risan82e90de2020-02-04 16:07:21 +090020#include "StubVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021#include "Utils.h"
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070022#include "VolumeBase.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023
24#include <utils/Errors.h>
25
26#include <vector>
27
28namespace android {
29namespace vold {
30
31class VolumeBase;
32
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033/*
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 */
39class Disk {
Paul Crowley14c8c072018-09-18 13:30:21 -070040 public:
Jeff Sharkey36801cc2015-03-13 16:09:20 -070041 Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042 virtual ~Disk();
43
Jeff Sharkey36801cc2015-03-13 16:09:20 -070044 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,
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070054 /* Flag that disk is EMMC internal */
55 kEmmc = 1 << 4,
Risan82e90de2020-02-04 16:07:21 +090056 /* Flag that disk is Stub disk, i.e., disk that is managed from outside
57 * Android (e.g., ARC++). */
58 kStub = 1 << 5,
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059 };
60
Greg Kaiser2bc201e2018-12-18 08:42:08 -080061 const std::string& getId() const { return mId; }
62 const std::string& getEventPath() const { return mEventPath; }
63 const std::string& getSysPath() const { return mSysPath; }
64 const std::string& getDevPath() const { return mDevPath; }
65 dev_t getDevice() const { return mDevice; }
66 uint64_t getSize() const { return mSize; }
67 const std::string& getLabel() const { return mLabel; }
68 int getFlags() const { return mFlags; }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080069
70 std::shared_ptr<VolumeBase> findVolume(const std::string& id);
71
Greg Kaiser2bc201e2018-12-18 08:42:08 -080072 void listVolumes(VolumeBase::Type type, std::list<std::string>& list) const;
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070073
Martijn Coenen0a7e9922020-01-24 16:17:32 +010074 std::vector<std::shared_ptr<VolumeBase>> getVolumes() const;
75
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 status_t create();
77 status_t destroy();
78
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079 status_t readMetadata();
80 status_t readPartitions();
Risan82e90de2020-02-04 16:07:21 +090081 void initializePartition(std::shared_ptr<StubVolume> vol);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082
Jeff Sharkey9c484982015-03-31 10:35:33 -070083 status_t unmountAll();
84
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 status_t partitionPublic();
86 status_t partitionPrivate();
87 status_t partitionMixed(int8_t ratio);
88
Paul Crowley14c8c072018-09-18 13:30:21 -070089 private:
Jeff Sharkeydeb24052015-03-02 21:01:40 -080090 /* ID that uniquely references this disk */
91 std::string mId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092 /* Original event path */
93 std::string mEventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094 /* Device path under sysfs */
95 std::string mSysPath;
96 /* Device path under dev */
97 std::string mDevPath;
98 /* Kernel device representing disk */
99 dev_t mDevice;
100 /* Size of disk, in bytes */
101 uint64_t mSize;
102 /* User-visible label, such as manufacturer */
103 std::string mLabel;
104 /* Current partitions on disk */
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700105 std::vector<std::shared_ptr<VolumeBase>> mVolumes;
106 /* Nickname for this disk */
107 std::string mNickname;
108 /* Flags applicable to this disk */
109 int mFlags;
110 /* Flag indicating object is created */
111 bool mCreated;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700112 /* Flag that we just partitioned and should format all volumes */
113 bool mJustPartitioned;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114
115 void createPublicVolume(dev_t device);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700116 void createPrivateVolume(dev_t device, const std::string& partGuid);
Risan82e90de2020-02-04 16:07:21 +0900117 void createStubVolume();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118
119 void destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120
121 int getMaxMinors();
122
Risan82e90de2020-02-04 16:07:21 +0900123 bool isStub() { return mFlags & kStub; }
124
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 DISALLOW_COPY_AND_ASSIGN(Disk);
126};
127
128} // namespace vold
129} // namespace android
130
131#endif