1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "volume/volume_manager.h"
17
18 #include <cstdlib>
19 #include <sys/sysmacros.h>
20
21 #include "ipc/storage_manager_client.h"
22 #include "storage_service_errno.h"
23 #include "storage_service_log.h"
24 #include "utils/string_utils.h"
25 #include "volume/external_volume_info.h"
26
27 using namespace std;
28
29 namespace OHOS {
30 namespace StorageDaemon {
31 VolumeManager* VolumeManager::instance_ = nullptr;
32
Instance()33 VolumeManager* VolumeManager::Instance()
34 {
35 if (instance_ == nullptr) {
36 instance_ = new VolumeManager();
37 }
38
39 return instance_;
40 }
41
GetVolume(const std::string volId)42 std::shared_ptr<VolumeInfo> VolumeManager::GetVolume(const std::string volId)
43 {
44 auto it = volumes_.find(volId);
45 if (it == volumes_.end()) {
46 return nullptr;
47 }
48 return it->second;
49 }
50
CreateVolume(const std::string diskId,dev_t device)51 std::string VolumeManager::CreateVolume(const std::string diskId, dev_t device)
52 {
53 std::string volId = StringPrintf("vol-%u-%u", major(device), minor(device));
54
55 LOGI("create volume %{public}s.", volId.c_str());
56
57 std::shared_ptr<VolumeInfo> tmp = GetVolume(volId);
58 if (tmp != nullptr) {
59 LOGE("volume %{public}s exist.", volId.c_str());
60 return "";
61 }
62
63 auto info = std::make_shared<ExternalVolumeInfo>();
64 int32_t ret = info->Create(volId, diskId, device);
65 if (ret) {
66 return "";
67 }
68
69 volumes_[volId] = info;
70
71 StorageManagerClient client;
72 ret = client.NotifyVolumeCreated(info);
73 if (ret != E_OK) {
74 LOGE("Volume Notify Created failed");
75 }
76
77 return volId;
78 }
79
DestroyVolume(const std::string volId)80 int32_t VolumeManager::DestroyVolume(const std::string volId)
81 {
82 LOGI("destroy volume %{public}s.", volId.c_str());
83
84 std::shared_ptr<VolumeInfo> destroyNode = GetVolume(volId);
85
86 if (destroyNode == nullptr) {
87 LOGE("the volume %{public}s does not exist", volId.c_str());
88 return E_NON_EXIST;
89 }
90
91 int32_t ret = destroyNode->Destroy();
92 if (ret)
93 return ret;
94 volumes_.erase(volId);
95 destroyNode.reset();
96
97 StorageManagerClient client;
98 ret = client.NotifyVolumeDestroyed(volId);
99 if (ret != E_OK) {
100 LOGE("Volume Notify Created failed");
101 }
102 return E_OK;
103 }
104
Check(const std::string volId)105 int32_t VolumeManager::Check(const std::string volId)
106 {
107 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
108 if (info == nullptr) {
109 LOGE("the volume %{public}s does not exist.", volId.c_str());
110 return E_NON_EXIST;
111 }
112
113 int32_t err = info->Check();
114 if (err != E_OK) {
115 LOGE("the volume %{public}s check failed.", volId.c_str());
116 return err;
117 }
118 return E_OK;
119 }
120
Mount(const std::string volId,uint32_t flags)121 int32_t VolumeManager::Mount(const std::string volId, uint32_t flags)
122 {
123 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
124 if (info == nullptr) {
125 LOGE("the volume %{public}s does not exist.", volId.c_str());
126 return E_NON_EXIST;
127 }
128
129 int32_t err = info->Mount(flags);
130 if (err != E_OK) {
131 LOGE("the volume %{public}s mount failed.", volId.c_str());
132 return err;
133 }
134
135 StorageManagerClient client;
136 err = client.NotifyVolumeMounted(info);
137 if (err) {
138 LOGE("Volume Notify Mount Destroyed failed");
139 }
140 return E_OK;
141 }
142
UMount(const std::string volId)143 int32_t VolumeManager::UMount(const std::string volId)
144 {
145 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
146 if (info == nullptr) {
147 LOGE("the volume %{public}s does not exist.", volId.c_str());
148 return E_NON_EXIST;
149 }
150
151 int32_t err = info->UMount();
152 if (err != E_OK) {
153 LOGE("the volume %{public}s mount failed.", volId.c_str());
154 return err;
155 }
156 return E_OK;
157 }
158
Format(const std::string volId,const std::string fsType)159 int32_t VolumeManager::Format(const std::string volId, const std::string fsType)
160 {
161 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
162 if (info == nullptr) {
163 LOGE("the volume %{public}s does not exist.", volId.c_str());
164 return E_NON_EXIST;
165 }
166
167 int32_t err = info->Format(fsType);
168 if (err != E_OK) {
169 LOGE("the volume %{public}s format failed.", volId.c_str());
170 return err;
171 }
172
173 return E_OK;
174 }
175
SetVolumeDescription(const std::string volId,const std::string description)176 int32_t VolumeManager::SetVolumeDescription(const std::string volId, const std::string description)
177 {
178 std::shared_ptr<VolumeInfo> info = GetVolume(volId);
179 if (info == nullptr) {
180 LOGE("the volume %{public}s does not exist.", volId.c_str());
181 return E_NON_EXIST;
182 }
183
184 int32_t err = info->SetVolumeDescription(description);
185 if (err != E_OK) {
186 LOGE("the volume %{public}s setVolumeDescription failed.", volId.c_str());
187 return err;
188 }
189
190 return E_OK;
191 }
192 } // StorageDaemon
193 } // OHOS
194