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_.Insert(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     return E_OK;
98 }
99 
Check(const std::string volId)100 int32_t VolumeManager::Check(const std::string volId)
101 {
102     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
103     if (info == nullptr) {
104         LOGE("the volume %{public}s does not exist.", volId.c_str());
105         return E_NON_EXIST;
106     }
107 
108     int32_t err = info->Check();
109     if (err != E_OK) {
110         LOGE("the volume %{public}s check failed.", volId.c_str());
111         return err;
112     }
113     return E_OK;
114 }
115 
Mount(const std::string volId,uint32_t flags)116 int32_t VolumeManager::Mount(const std::string volId, uint32_t flags)
117 {
118     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
119     if (info == nullptr) {
120         LOGE("the volume %{public}s does not exist.", volId.c_str());
121         return E_NON_EXIST;
122     }
123 
124     int32_t err = info->Mount(flags);
125     if (err != E_OK) {
126         LOGE("the volume %{public}s mount failed.", volId.c_str());
127         return err;
128     }
129 
130     StorageManagerClient client;
131     err = client.NotifyVolumeMounted(info);
132     if (err) {
133         LOGE("Volume Notify Mount Destroyed failed");
134     }
135     return E_OK;
136 }
137 
UMount(const std::string volId)138 int32_t VolumeManager::UMount(const std::string volId)
139 {
140     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
141     if (info == nullptr) {
142         LOGE("the volume %{public}s does not exist.", volId.c_str());
143         return E_NON_EXIST;
144     }
145 
146     int32_t err = info->UMount();
147     if (err != E_OK) {
148         LOGE("the volume %{public}s mount failed.", volId.c_str());
149         return err;
150     }
151     return E_OK;
152 }
153 
Format(const std::string volId,const std::string fsType)154 int32_t VolumeManager::Format(const std::string volId, const std::string fsType)
155 {
156     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
157     if (info == nullptr) {
158         LOGE("the volume %{public}s does not exist.", volId.c_str());
159         return E_NON_EXIST;
160     }
161 
162     int32_t err = info->Format(fsType);
163     if (err != E_OK) {
164         LOGE("the volume %{public}s format failed.", volId.c_str());
165         return err;
166     }
167 
168     return E_OK;
169 }
170 
SetVolumeDescription(const std::string volId,const std::string description)171 int32_t VolumeManager::SetVolumeDescription(const std::string volId, const std::string description)
172 {
173     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
174     if (info == nullptr) {
175         LOGE("the volume %{public}s does not exist.", volId.c_str());
176         return E_NON_EXIST;
177     }
178 
179     int32_t err = info->SetVolumeDescription(description);
180     if (err != E_OK) {
181         LOGE("the volume %{public}s setVolumeDescription failed.", volId.c_str());
182         return err;
183     }
184 
185     return E_OK;
186 }
187 } // StorageDaemon
188 } // OHOS
189