]> git.proxmox.com Git - ceph.git/blame - ceph/src/blk/zoned/HMSMRDevice.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / blk / zoned / HMSMRDevice.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 Red Hat
f67539c2 7 * Copyright (C) 2020 Abutalib Aghayev
7c673cae
FG
8 *
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
13 *
14 */
15
f67539c2 16#include "HMSMRDevice.h"
f67539c2
TL
17extern "C" {
18#include <libzbd/zbd.h>
19}
20effc67
TL
20#include "common/debug.h"
21#include "common/errno.h"
7c673cae
FG
22
23#define dout_context cct
24#define dout_subsys ceph_subsys_bdev
25#undef dout_prefix
f67539c2 26#define dout_prefix *_dout << "smrbdev(" << this << " " << path << ") "
7c673cae 27
20effc67 28using namespace std;
9f95a23c 29
20effc67
TL
30HMSMRDevice::HMSMRDevice(CephContext* cct,
31 aio_callback_t cb,
32 void *cbpriv,
33 aio_callback_t d_cb,
34 void *d_cbpriv)
35 : KernelDevice(cct, cb, cbpriv, d_cb, d_cbpriv)
36{
7c673cae
FG
37}
38
f67539c2
TL
39bool HMSMRDevice::support(const std::string& path)
40{
41 return zbd_device_is_zoned(path.c_str()) == 1;
42}
43
20effc67 44int HMSMRDevice::_post_open()
7c673cae 45{
20effc67 46 dout(10) << __func__ << dendl;
f67539c2 47
20effc67
TL
48 zbd_fd = zbd_open(path.c_str(), O_RDWR | O_DIRECT | O_LARGEFILE, nullptr);
49 int r;
50 if (zbd_fd < 0) {
51 r = errno;
52 derr << __func__ << " zbd_open failed on " << path << ": "
53 << cpp_strerror(r) << dendl;
54 return -r;
f67539c2 55 }
f67539c2
TL
56
57 unsigned int nr_zones = 0;
20effc67
TL
58 std::vector<zbd_zone> zones;
59 if (zbd_report_nr_zones(zbd_fd, 0, 0, ZBD_RO_NOT_WP, &nr_zones) != 0) {
60 r = -errno;
61 derr << __func__ << " zbd_report_nr_zones failed on " << path << ": "
62 << cpp_strerror(r) << dendl;
63 goto fail;
f67539c2
TL
64 }
65
20effc67
TL
66 zones.resize(nr_zones);
67 if (zbd_report_zones(zbd_fd, 0, 0, ZBD_RO_NOT_WP, zones.data(), &nr_zones) != 0) {
68 r = -errno;
69 derr << __func__ << " zbd_report_zones failed on " << path << dendl;
70 goto fail;
11fdf7f2 71 }
f67539c2
TL
72
73 zone_size = zbd_zone_len(&zones[0]);
74 conventional_region_size = nr_zones * zone_size;
75
76 dout(10) << __func__ << " setting zone size to " << zone_size
77 << " and conventional region size to " << conventional_region_size
78 << dendl;
79
11fdf7f2 80 return 0;
11fdf7f2 81
20effc67
TL
82fail:
83 zbd_close(zbd_fd);
84 zbd_fd = -1;
7c673cae
FG
85 return r;
86}
87
7c673cae 88
20effc67 89void HMSMRDevice::_pre_close()
7c673cae 90{
20effc67
TL
91 if (zbd_fd >= 0) {
92 zbd_close(zbd_fd);
93 zbd_fd = -1;
7c673cae
FG
94 }
95}
96
20effc67 97void HMSMRDevice::reset_all_zones()
11fdf7f2
TL
98{
99 dout(10) << __func__ << dendl;
20effc67 100 zbd_reset_zones(zbd_fd, conventional_region_size, 0);
11fdf7f2
TL
101}
102
20effc67 103void HMSMRDevice::reset_zone(uint64_t zone)
11fdf7f2 104{
20effc67
TL
105 dout(10) << __func__ << " zone 0x" << std::hex << zone << std::dec << dendl;
106 if (zbd_reset_zones(zbd_fd, zone * zone_size, zone_size) != 0) {
107 derr << __func__ << " resetting zone failed for zone 0x" << std::hex
108 << zone << std::dec << dendl;
109 ceph_abort("zbd_reset_zones failed");
11fdf7f2
TL
110 }
111}
112
20effc67 113std::vector<uint64_t> HMSMRDevice::get_zones()
28e407b8 114{
20effc67
TL
115 std::vector<zbd_zone> zones;
116 unsigned int num_zones = size / zone_size;
117 zones.resize(num_zones);
b32b8144 118
20effc67
TL
119 int r = zbd_report_zones(zbd_fd, 0, 0, ZBD_RO_ALL, zones.data(), &num_zones);
120 if (r != 0) {
121 derr << __func__ << " zbd_report_zones failed on " << path << ": "
122 << cpp_strerror(errno) << dendl;
123 ceph_abort("zbd_report_zones failed");
7c673cae 124 }
7c673cae 125
20effc67
TL
126 std::vector<uint64_t> wp(num_zones);
127 for (unsigned i = 0; i < num_zones; ++i) {
128 wp[i] = zones[i].wp;
7c673cae 129 }
20effc67 130 return wp;
7c673cae 131}