Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 | |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 20 | #include "StubVolume.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 21 | #include "Utils.h" |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 22 | #include "VolumeBase.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | |
| 24 | #include <utils/Errors.h> |
| 25 | |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace android { |
| 29 | namespace vold { |
| 30 | |
| 31 | class VolumeBase; |
| 32 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 40 | public: |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 41 | Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | virtual ~Disk(); |
| 43 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 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, |
Jeff Sharkey | 5a6bfca | 2015-05-14 20:33:55 -0700 | [diff] [blame] | 54 | /* Flag that disk is EMMC internal */ |
| 55 | kEmmc = 1 << 4, |
Youkichi Hosoi | defc045 | 2020-07-08 06:08:48 +0900 | [diff] [blame] | 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, |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 64 | const std::string& getId() const { return mId; } |
| 65 | const std::string& getEventPath() const { return mEventPath; } |
| 66 | const std::string& getSysPath() const { return mSysPath; } |
| 67 | const std::string& getDevPath() const { return mDevPath; } |
| 68 | dev_t getDevice() const { return mDevice; } |
| 69 | uint64_t getSize() const { return mSize; } |
| 70 | const std::string& getLabel() const { return mLabel; } |
| 71 | int getFlags() const { return mFlags; } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 72 | |
Youkichi Hosoi | dfaff1d | 2021-11-25 16:24:35 +0900 | [diff] [blame] | 73 | bool isStub() const { return (mFlags & kStubInvisible) || (mFlags & kStubVisible); } |
| 74 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 75 | std::shared_ptr<VolumeBase> findVolume(const std::string& id); |
| 76 | |
Greg Kaiser | 2bc201e | 2018-12-18 08:42:08 -0800 | [diff] [blame] | 77 | void listVolumes(VolumeBase::Type type, std::list<std::string>& list) const; |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 78 | |
Martijn Coenen | 0a7e992 | 2020-01-24 16:17:32 +0100 | [diff] [blame] | 79 | std::vector<std::shared_ptr<VolumeBase>> getVolumes() const; |
| 80 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame^] | 81 | status_t create(); |
| 82 | status_t destroy(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 83 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame^] | 84 | status_t readMetadata(); |
| 85 | status_t readPartitions(); |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 86 | void initializePartition(std::shared_ptr<StubVolume> vol); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 87 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 88 | status_t unmountAll(); |
| 89 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame^] | 90 | status_t partitionPublic(); |
| 91 | status_t partitionPrivate(); |
| 92 | status_t partitionMixed(int8_t ratio); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 93 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame^] | 94 | private: |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 95 | /* ID that uniquely references this disk */ |
| 96 | std::string mId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 97 | /* Original event path */ |
| 98 | std::string mEventPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 99 | /* Device path under sysfs */ |
| 100 | std::string mSysPath; |
| 101 | /* Device path under dev */ |
| 102 | std::string mDevPath; |
| 103 | /* Kernel device representing disk */ |
| 104 | dev_t mDevice; |
| 105 | /* Size of disk, in bytes */ |
| 106 | uint64_t mSize; |
| 107 | /* User-visible label, such as manufacturer */ |
| 108 | std::string mLabel; |
| 109 | /* Current partitions on disk */ |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 110 | std::vector<std::shared_ptr<VolumeBase>> mVolumes; |
| 111 | /* Nickname for this disk */ |
| 112 | std::string mNickname; |
| 113 | /* Flags applicable to this disk */ |
| 114 | int mFlags; |
| 115 | /* Flag indicating object is created */ |
| 116 | bool mCreated; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 117 | /* Flag that we just partitioned and should format all volumes */ |
| 118 | bool mJustPartitioned; |
Scott Mertz | ad5b4f9 | 2016-03-31 13:09:56 -0700 | [diff] [blame] | 119 | /* Flag that we need to skip first disk change events after partitioning*/ |
| 120 | bool mSkipChange; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 121 | |
Alexander Martinz | 7226888 | 2023-09-28 14:43:16 +0200 | [diff] [blame^] | 122 | void createPublicVolume(dev_t device); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 123 | void createPrivateVolume(dev_t device, const std::string& partGuid); |
Risan | 82e90de | 2020-02-04 16:07:21 +0900 | [diff] [blame] | 124 | void createStubVolume(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 125 | |
| 126 | void destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 127 | |
| 128 | int getMaxMinors(); |
| 129 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(Disk); |
| 131 | }; |
| 132 | |
| 133 | } // namespace vold |
| 134 | } // namespace android |
| 135 | |
| 136 | #endif |