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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2015 XSky <haomai@xsky.com>
*
* Author: Haomai Wang <haomaiwang@gmail.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef CEPH_OS_BLUESTORE_BLOCKDEVICE_H
#define CEPH_OS_BLUESTORE_BLOCKDEVICE_H
#include <atomic>
#include <condition_variable>
#include <list>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <vector>
#include "acconfig.h"
#ifdef HAVE_LIBAIO
#include "aio.h"
#endif
#include "include/assert.h"
#include "include/buffer.h"
#define SPDK_PREFIX "spdk:"
class CephContext;
/// track in-flight io
struct IOContext {
private:
std::mutex lock;
std::condition_variable cond;
int r = 0;
public:
CephContext* cct;
void *priv;
#ifdef HAVE_SPDK
void *nvme_task_first = nullptr;
void *nvme_task_last = nullptr;
std::atomic_int total_nseg = {0};
#endif
#ifdef HAVE_LIBAIO
std::list<aio_t> pending_aios; ///< not yet submitted
std::list<aio_t> running_aios; ///< submitting or submitted
#endif
std::atomic_int num_pending = {0};
std::atomic_int num_running = {0};
bool allow_eio;
explicit IOContext(CephContext* cct, void *p, bool allow_eio = false)
: cct(cct), priv(p), allow_eio(allow_eio)
{}
// no copying
IOContext(const IOContext& other) = delete;
IOContext &operator=(const IOContext& other) = delete;
bool has_pending_aios() {
return num_pending.load();
}
void aio_wait();
void try_aio_wake() {
if (num_running == 1) {
// we might have some pending IOs submitted after the check
// as there is no lock protection for aio_submit.
// Hence we might have false conditional trigger.
// aio_wait has to handle that hence do not care here.
std::lock_guard<std::mutex> l(lock);
cond.notify_all();
--num_running;
assert(num_running >= 0);
} else {
--num_running;
}
}
void set_return_value(int _r) {
r = _r;
}
int get_return_value() const {
return r;
}
};
class BlockDevice {
public:
CephContext* cct;
typedef void (*aio_callback_t)(void *handle, void *aio);
private:
std::mutex ioc_reap_lock;
std::vector<IOContext*> ioc_reap_queue;
std::atomic_int ioc_reap_count = {0};
protected:
uint64_t size;
uint64_t block_size;
bool rotational = true;
public:
aio_callback_t aio_callback;
void *aio_callback_priv;
BlockDevice(CephContext* cct, aio_callback_t cb, void *cbpriv)
: cct(cct),
size(0),
block_size(0),
aio_callback(cb),
aio_callback_priv(cbpriv)
{}
virtual ~BlockDevice() = default;
static BlockDevice *create(
CephContext* cct, const std::string& path, aio_callback_t cb, void *cbpriv);
virtual bool supported_bdev_label() { return true; }
virtual bool is_rotational() { return rotational; }
virtual void aio_submit(IOContext *ioc) = 0;
uint64_t get_size() const { return size; }
uint64_t get_block_size() const { return block_size; }
virtual int collect_metadata(const std::string& prefix, std::map<std::string,std::string> *pm) const = 0;
virtual int get_devname(std::string *out) {
return -ENOENT;
}
virtual int get_devices(std::set<std::string> *ls) {
std::string s;
if (get_devname(&s) == 0) {
ls->insert(s);
}
return 0;
}
virtual int read(
uint64_t off,
uint64_t len,
bufferlist *pbl,
IOContext *ioc,
bool buffered) = 0;
virtual int read_random(
uint64_t off,
uint64_t len,
char *buf,
bool buffered) = 0;
virtual int write(
uint64_t off,
bufferlist& bl,
bool buffered) = 0;
virtual int aio_read(
uint64_t off,
uint64_t len,
bufferlist *pbl,
IOContext *ioc) = 0;
virtual int aio_write(
uint64_t off,
bufferlist& bl,
IOContext *ioc,
bool buffered) = 0;
virtual int flush() = 0;
void queue_reap_ioc(IOContext *ioc);
void reap_ioc();
// for managing buffered readers/writers
virtual int invalidate_cache(uint64_t off, uint64_t len) = 0;
virtual int open(const std::string& path) = 0;
virtual void close() = 0;
protected:
bool is_valid_io(uint64_t off, uint64_t len) const {
return (off % block_size == 0 &&
len % block_size == 0 &&
len > 0 &&
off < size &&
off + len <= size);
}
};
#endif //CEPH_OS_BLUESTORE_BLOCKDEVICE_H
|