~aleteoryx/muditaos

ref: ef556d43f24756643effcb43ee3866421c08a52d muditaos/module-vfs/src/purefs/blkdev/disk_manager.cpp -rw-r--r-- 15.1 KiB
ef556d43 — Pawel Olejniczak [CP-1423] Update backup and restore endpoint API test 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <purefs/blkdev/disk_manager.hpp>
#include <purefs/blkdev/disk.hpp>
#include <log/log.hpp>
#include <mutex.hpp>
#include <errno.h>
#include <charconv>
#include <tuple>
#include <purefs/blkdev/disk_handle.hpp>
#include <purefs/blkdev/partition_parser.hpp>

/** NOTE: Device manager implementation without sector cache
 */
namespace purefs::blkdev
{
    namespace
    {
        using namespace std::literals;
        static constexpr auto part_suffix    = "part"sv;
        static constexpr auto syspart_suffix = "sys"sv;
    } // namespace

    disk_manager::disk_manager() : m_lock(std::make_unique<cpp_freertos::MutexRecursive>())
    {}

    disk_manager::~disk_manager()
    {}

    auto disk_manager::register_device(std::shared_ptr<disk> disk, std::string_view device_name, unsigned flags) -> int
    {
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -EINVAL;
        }
        cpp_freertos::LockGuard _lck(*m_lock);
        const auto ret = m_dev_map.find(std::string(device_name));
        if (ret != std::end(m_dev_map)) {
            LOG_ERROR("Disc with same name already registered");
            return -EEXIST;
        }
        else {
            auto ret = disk->probe(flags);
            if (ret < 0) {
                LOG_ERROR("Unable to probe the disc errno %i", ret);
                return ret;
            }
            const auto it = m_dev_map.emplace(std::make_pair(device_name, disk));
            if (flags & flags::no_parts_scan) {
                disk->clear_partitions();
                return 0;
            }
            else {
                ret = reread_partitions(std::make_shared<internal::disk_handle>(disk, it.first->first));
                return ret;
            }
        }
    }
    auto disk_manager::unregister_device(std::string_view device_name) -> int
    {
        cpp_freertos::LockGuard _lck(*m_lock);
        auto it = m_dev_map.find(std::string(device_name));
        if (it == std::end(m_dev_map)) {
            LOG_ERROR("Disc with given name doesn't exists in manager");
            return -ENOENT;
        }
        auto ret = it->second->cleanup();
        m_dev_map.erase(it);
        if (ret < 0) {
            LOG_ERROR("Disk cleanup failed code %i", ret);
        }
        return ret;
    }

    auto disk_manager::device_handle(std::string_view device_name) const -> disk_fd
    {
        const auto [dev, part] = parse_device_name(device_name);
        disk_fd ret{};
        if (dev.empty()) {
            ret = nullptr;
        }
        else {
            cpp_freertos::LockGuard _lck(*m_lock);
            const auto it = m_dev_map.find(std::string(dev));
            if (it == std::end(m_dev_map)) {
                ret = nullptr;
            }
            else {
                ret = std::make_shared<internal::disk_handle>(it->second, device_name, part);
                if (internal::disk_handle::is_user_partition(part)) {
                    if (part >= int(partitions(ret).size())) {
                        LOG_ERROR("Partition %i doesn't exists", part);
                        ret = nullptr;
                    }
                }
            }
        }
        return ret;
    }

    auto disk_manager::parse_device_name(std::string_view device) -> std::tuple<std::string_view, part_t>
    {
        const auto parti    = device.rfind(part_suffix);
        const auto sysparti = device.rfind(syspart_suffix);
        if ((parti != std::string::npos) != (sysparti != std::string::npos)) {
            const auto si        = (parti != std::string::npos) ? (parti) : (sysparti);
            const auto sl        = (parti != std::string::npos) ? (part_suffix.length()) : (syspart_suffix.length());
            const auto part_name = device.substr(0, si);
            const auto part_num  = device.substr(si + sl);
            part_t part_inum{-1};
            if (!part_num.empty()) {
                const auto ires = std::from_chars(std::begin(part_num), std::end(part_num), part_inum);
                if (ires.ec == std::errc()) {
                    if (sysparti != std::string::npos)
                        part_inum = internal::disk_handle::to_syspart_num(part_inum);
                    return std::make_tuple(part_name, part_inum);
                }
                else {
                    return std::make_tuple(""sv, -1);
                }
            }
            else {
                return std::make_tuple(part_name, part_inum);
            }
        }
        else {
            return std::make_tuple(device, -1);
        }
    }
    auto disk_manager::part_lba_to_disk_lba(disk_fd disk, sector_t part_lba, size_t count) -> scount_t
    {
        if (disk->is_user_partition()) {
            auto pdisc = disk->disk();
            if (!pdisc) {
                LOG_ERROR("Unable to lock disk");
                return -EIO;
            }
            const auto part = pdisc->partitions()[disk->partition()];
            if (part_lba + count > part.num_sectors) {
                LOG_ERROR("Partition sector req out of range");
                return -ERANGE;
            }
            else {
                return part_lba + part.start_sector;
            }
        }
        else {
            if (part_lba + count > disk->sectors()) {
                LOG_ERROR("Disk sector req out of range");
                return -ERANGE;
            }
            else {
                return part_lba;
            }
        }
    }
    auto disk_manager::write(disk_fd dfd, const void *buf, sector_t lba, std::size_t count) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        const auto calc_lba = part_lba_to_disk_lba(dfd, lba, count);
        if (calc_lba < 0) {
            return calc_lba;
        }
        else {
            return disk->write(buf, calc_lba, count, dfd->system_partition());
        }
    }
    auto disk_manager::read(disk_fd dfd, void *buf, sector_t lba, std::size_t count) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        const auto calc_lba = part_lba_to_disk_lba(dfd, lba, count);
        if (calc_lba < 0) {
            return calc_lba;
        }
        else {
            return disk->read(buf, calc_lba, count, dfd->system_partition());
        }
    }
    auto disk_manager::erase(disk_fd dfd, sector_t lba, std::size_t count) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        const auto calc_lba = part_lba_to_disk_lba(dfd, lba, count);
        if (calc_lba < 0) {
            return calc_lba;
        }
        else {
            return disk->erase(calc_lba, count, dfd->system_partition());
        }
    }
    auto disk_manager::sync(disk_fd dfd) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        return disk->sync();
    }
    auto disk_manager::pm_control(disk_fd dfd, pm_state target_state) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        return disk->pm_control(target_state);
    }
    auto disk_manager::pm_control(pm_state target_state) -> int
    {
        cpp_freertos::LockGuard _lck(*m_lock);
        int last_err{};
        for (const auto &disk : m_dev_map) {
            auto err = disk.second->pm_control(target_state);
            if (err) {
                LOG_ERROR("Unable to change PM state for specified device. Errno: %i", err);
                last_err = err;
            }
        }
        return last_err;
    }
    auto disk_manager::pm_read(disk_fd dfd, pm_state &current_state) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return -ENOENT;
        }
        return disk->pm_read(current_state);
    }
    auto disk_manager::status(disk_fd dfd) const -> media_status
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return media_status::error;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return media_status::error;
        }
        return disk->status();
    }
    auto disk_manager::partitions(disk_fd dfd) const -> std::vector<partition>
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return {};
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return {};
        }
        return disk->partitions();
    }
    auto disk_manager::partition_info(disk_fd dfd) const -> std::optional<partition>
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return std::nullopt;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return std::nullopt;
        }
        if (dfd->is_user_partition()) {
            auto parts = disk->partitions();
            if (size_t(dfd->partition()) >= parts.size()) {
                LOG_ERROR("Partition num out of range");
                return std::nullopt;
            }
            else {
                return {parts[dfd->partition()]};
            }
        }
        else {
            LOG_ERROR("No paritions on disc");
            return std::nullopt;
        }
    }
    auto disk_manager::get_info(disk_fd dfd, info_type what) const -> scount_t
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return {};
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return {};
        }
        //! When it is partition ask for partition info
        if ((what == info_type::sector_count || what == info_type::start_sector) && dfd->is_user_partition()) {
            if (unsigned(dfd->partition()) >= disk->partitions().size()) {
                LOG_ERROR("Partition number out of range");
                return -ERANGE;
            }
            const auto part = disk->partitions()[dfd->partition()];
            if (what == info_type::sector_count) {
                return part.num_sectors;
            }
            if (what == info_type::start_sector) {
                return part.start_sector;
            }
            return {};
        }
        else {
            return disk->get_info(what, dfd->system_partition());
        }
    }
    auto disk_manager::reread_partitions(disk_fd dfd) -> int
    {
        if (!dfd) {
            LOG_ERROR("Disk handle doesn't exists");
            return -EINVAL;
        }
        auto disk = dfd->disk();
        if (!disk) {
            LOG_ERROR("Disk doesn't exists");
            return {};
        }
        disk->clear_partitions();
        internal::partition_parser pparser(disk, disk->partitions());
        auto ret = pparser.partition_search();
        // Fill the partition name
        if (!ret) {
            int no{};
            for (auto &parts : disk->partitions()) {
                parts.name = dfd->name();
                parts.name += part_suffix;
                parts.name += std::to_string(no++);
            }
        }
        return ret;
    }

    auto disk_manager::write(std::string_view device_name, const void *buf, sector_t lba, std::size_t count) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return write(dfd, buf, lba, count);
        else
            return -ENOENT;
    }
    auto disk_manager::read(std::string_view device_name, void *buf, sector_t lba, std::size_t count) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return read(dfd, buf, lba, count);
        else
            return -ENOENT;
    }
    auto disk_manager::erase(std::string_view device_name, sector_t lba, std::size_t count) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return erase(dfd, lba, count);
        else
            return -ENOENT;
    }
    auto disk_manager::sync(std::string_view device_name) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return sync(dfd);
        else
            return -ENOENT;
    }
    auto disk_manager::pm_control(std::string_view device_name, pm_state target_state) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return pm_control(dfd, target_state);
        else
            return -ENOENT;
    }
    auto disk_manager::pm_read(std::string_view device_name, pm_state &current_state) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return pm_read(dfd, current_state);
        else
            return -ENOENT;
    }
    auto disk_manager::status(std::string_view device_name) const -> media_status
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return status(dfd);
        else
            return media_status::error;
    }
    auto disk_manager::partitions(std::string_view device_name) const -> std::vector<partition>
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return partitions(dfd);
        else
            return {};
    }
    auto disk_manager::partition_info(std::string_view device_name) const -> std::optional<partition>
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return partition_info(dfd);
        else
            return std::nullopt;
    }
    auto disk_manager::get_info(std::string_view device_name, info_type what) const -> scount_t
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return get_info(dfd, what);
        else
            return -ENOENT;
    }
    auto disk_manager::reread_partitions(std::string_view device_name) -> int
    {
        auto dfd = device_handle(device_name);
        if (dfd)
            return reread_partitions(dfd);
        else
            return -ENOENT;
    }
    auto disk_manager::disk_handle_from_partition_handle(disk_fd disk) -> disk_fd
    {
        const auto new_name = std::get<0>(parse_device_name(disk->name()));
        return std::make_shared<internal::disk_handle>(disk->disk(), new_name);
    }

} // namespace purefs::blkdev