• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <cstdlib>
18 #include <sys/sysmacros.h>
19 #include "storage_service_log.h"
20 #include "storage_service_errno.h"
21 #include "utils/string_utils.h"
22 #include "volume/external_volume_info.h"
23 #include "ipc/storage_manager_client.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace StorageDaemon {
29 VolumeManager* VolumeManager::instance_ = nullptr;
30 
Instance()31 VolumeManager* VolumeManager::Instance()
32 {
33     if (instance_ == nullptr) {
34         instance_ = new VolumeManager();
35     }
36 
37     return instance_;
38 }
39 
GetVolume(const std::string volId)40 std::shared_ptr<VolumeInfo> VolumeManager::GetVolume(const std::string volId)
41 {
42     auto it = volumes_.find(volId);
43     if (it == volumes_.end()) {
44         return nullptr;
45     }
46     return it->second;
47 }
48 
CreateVolume(const std::string diskId,dev_t device)49 std::string VolumeManager::CreateVolume(const std::string diskId, dev_t device)
50 {
51     std::string volId = StringPrintf("vol-%u-%u", major(device), minor(device));
52 
53     LOGI("create volume %{public}s.", volId.c_str());
54 
55     std::shared_ptr<VolumeInfo> tmp = GetVolume(volId);
56     if (tmp != nullptr) {
57         LOGE("volume %{public}s exist.", volId.c_str());
58         return "";
59     }
60 
61     auto info = std::make_shared<ExternalVolumeInfo>();
62     int32_t ret = info->Create(volId, diskId, device);
63     if (ret) {
64         return "";
65     }
66 
67     volumes_[volId] = info;
68 
69     StorageManagerClient client;
70     ret = client.NotifyVolumeCreated(info);
71     if (ret != E_OK) {
72         LOGE("Volume Notify Created failed");
73     }
74 
75     return volId;
76 }
77 
DestroyVolume(const std::string volId)78 int32_t VolumeManager::DestroyVolume(const std::string volId)
79 {
80     LOGI("destroy volume %{public}s.", volId.c_str());
81 
82     std::shared_ptr<VolumeInfo> destroyNode = GetVolume(volId);
83 
84     if (destroyNode == nullptr) {
85         LOGE("the volume %{public}s does not exist", volId.c_str());
86         return E_NON_EXIST;
87     }
88 
89     int32_t ret = destroyNode->Destroy();
90     if (ret)
91         return ret;
92     volumes_.erase(volId);
93     destroyNode.reset();
94 
95     StorageManagerClient client;
96     ret = client.NotifyVolumeDestroyed(volId);
97     if (ret != E_OK) {
98         LOGE("Volume Notify Created failed");
99     }
100     return E_OK;
101 }
102 
Check(const std::string volId)103 int32_t VolumeManager::Check(const std::string volId)
104 {
105     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
106     if (info == nullptr) {
107         LOGE("the volume %{public}s does not exist.", volId.c_str());
108         return E_NON_EXIST;
109     }
110 
111     int32_t err = info->Check();
112     if (err != E_OK) {
113         LOGE("the volume %{public}s check failed.", volId.c_str());
114         return err;
115     }
116     return E_OK;
117 }
118 
Mount(const std::string volId,uint32_t flags)119 int32_t VolumeManager::Mount(const std::string volId, uint32_t flags)
120 {
121     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
122     if (info == nullptr) {
123         LOGE("the volume %{public}s does not exist.", volId.c_str());
124         return E_NON_EXIST;
125     }
126 
127     int32_t err = info->Mount(flags);
128     if (err != E_OK) {
129         LOGE("the volume %{public}s mount failed.", volId.c_str());
130         return err;
131     }
132 
133     StorageManagerClient client;
134     err = client.NotifyVolumeMounted(info);
135     if (err) {
136         LOGE("Volume Notify Mount Destroyed failed");
137     }
138     return E_OK;
139 }
140 
UMount(const std::string volId)141 int32_t VolumeManager::UMount(const std::string volId)
142 {
143     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
144     if (info == nullptr) {
145         LOGE("the volume %{public}s does not exist.", volId.c_str());
146         return E_NON_EXIST;
147     }
148 
149     int32_t err = info->UMount();
150     if (err != E_OK) {
151         LOGE("the volume %{public}s mount failed.", volId.c_str());
152         return err;
153     }
154     return E_OK;
155 }
156 
Format(const std::string volId,const std::string fsType)157 int32_t VolumeManager::Format(const std::string volId, const std::string fsType)
158 {
159     std::shared_ptr<VolumeInfo> info = GetVolume(volId);
160     if (info == nullptr) {
161         LOGE("the volume %{public}s does not exist.", volId.c_str());
162         return E_NON_EXIST;
163     }
164 
165     int32_t err = info->Format(fsType);
166     if (err != E_OK) {
167         LOGE("the volume %{public}s format failed.", volId.c_str());
168         return err;
169     }
170 
171     return E_OK;
172 }
173 } // StorageDaemon
174 } // OHOS
175