summaryrefslogtreecommitdiffstats
path: root/src/blk/BlockDevice.h
blob: 191eb8ec908af8ebd0c32a5dbf8d96c0e8545add (plain)
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
// -*- 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_BLK_BLOCKDEVICE_H
#define CEPH_BLK_BLOCKDEVICE_H

#include <atomic>
#include <condition_variable>
#include <list>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <vector>

#include "acconfig.h"
#include "common/ceph_mutex.h"
#include "include/common_fwd.h"

#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
#include "aio/aio.h"
#endif
#include "include/ceph_assert.h"
#include "include/buffer.h"
#include "include/interval_set.h"
#define SPDK_PREFIX "spdk:"

#if defined(__linux__)
#if !defined(F_SET_FILE_RW_HINT)
#define F_LINUX_SPECIFIC_BASE 1024
#define F_SET_FILE_RW_HINT         (F_LINUX_SPECIFIC_BASE + 14)
#endif
// These values match Linux definition
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/fcntl.h#n56
#define  WRITE_LIFE_NOT_SET  	0 	// No hint information set
#define  WRITE_LIFE_NONE  	1       // No hints about write life time
#define  WRITE_LIFE_SHORT  	2       // Data written has a short life time
#define  WRITE_LIFE_MEDIUM  	3    	// Data written has a medium life time
#define  WRITE_LIFE_LONG  	4       // Data written has a long life time
#define  WRITE_LIFE_EXTREME  	5     	// Data written has an extremely long life time
#define  WRITE_LIFE_MAX  	6
#else
// On systems don't have WRITE_LIFE_* only use one FD 
// And all files are created equal
#define  WRITE_LIFE_NOT_SET  	0 	// No hint information set
#define  WRITE_LIFE_NONE  	0       // No hints about write life time
#define  WRITE_LIFE_SHORT  	0       // Data written has a short life time
#define  WRITE_LIFE_MEDIUM  	0    	// Data written has a medium life time
#define  WRITE_LIFE_LONG  	0       // Data written has a long life time
#define  WRITE_LIFE_EXTREME  	0    	// Data written has an extremely long life time
#define  WRITE_LIFE_MAX  	1
#endif


/// track in-flight io
struct IOContext {
private:
  ceph::mutex lock = ceph::make_mutex("IOContext::lock");
  ceph::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

#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
  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 release_running_aios();
  void aio_wait();
  uint64_t get_num_ios() const;

  void try_aio_wake() {
    assert(num_running >= 1);

    std::lock_guard l(lock);
    if (num_running.fetch_sub(1) == 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.
      cond.notify_all();
    }
  }

  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:
  ceph::mutex ioc_reap_lock = ceph::make_mutex("BlockDevice::ioc_reap_lock");
  std::vector<IOContext*> ioc_reap_queue;
  std::atomic_int ioc_reap_count = {0};
  enum class block_device_t {
    unknown,
#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
    aio,
#if defined(HAVE_LIBZBD)
    hm_smr,
#endif
#endif
#if defined(HAVE_SPDK)
    spdk,
#endif
#if defined(HAVE_BLUESTORE_PMEM)
    pmem,
#endif
  };
  static block_device_t detect_device_type(const std::string& path);
  static block_device_t device_type_from_name(const std::string& blk_dev_name);
  static BlockDevice *create_with_type(block_device_t device_type,
    CephContext* cct, const std::string& path, aio_callback_t cb,
    void *cbpriv, aio_callback_t d_cb, void *d_cbpriv);

protected:
  uint64_t size = 0;
  uint64_t block_size = 0;
  bool support_discard = false;
  bool rotational = true;
  bool lock_exclusive = true;

  // HM-SMR specific properties.  In HM-SMR drives the LBA space is divided into
  // fixed-size zones.  Typically, the first few zones are randomly writable;
  // they form a conventional region of the drive.  The remaining zones must be
  // written sequentially and they must be reset before rewritten.  For example,
  // a 14 TB HGST HSH721414AL drive has 52156 zones each of size is 256 MiB.
  // The zones 0-523 are randomly writable and they form the conventional region
  // of the drive.  The zones 524-52155 are sequential zones.
  uint64_t conventional_region_size = 0;
  uint64_t zone_size = 0;

public:
  aio_callback_t aio_callback;
  void *aio_callback_priv;
  BlockDevice(CephContext* cct, aio_callback_t cb, void *cbpriv)
  : cct(cct),
    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, aio_callback_t d_cb, void *d_cbpriv);
  virtual bool supported_bdev_label() { return true; }
  virtual bool is_rotational() { return rotational; }

  // HM-SMR-specific calls
  virtual bool is_smr() const { return false; }
  virtual uint64_t get_zone_size() const {
    ceph_assert(is_smr());
    return zone_size;
  }
  virtual uint64_t get_conventional_region_size() const {
    ceph_assert(is_smr());
    return conventional_region_size;
  }

  virtual void aio_submit(IOContext *ioc) = 0;

  void set_no_exclusive_lock() {
    lock_exclusive = false;
  }
  
  uint64_t get_size() const { return size; }
  uint64_t get_block_size() const { return block_size; }

  /// hook to provide utilization of thinly-provisioned device
  virtual bool get_thin_utilization(uint64_t *total, uint64_t *avail) const {
    return false;
  }

  virtual int collect_metadata(const std::string& prefix, std::map<std::string,std::string> *pm) const = 0;

  virtual int get_devname(std::string *out) const {
    return -ENOENT;
  }
  virtual int get_devices(std::set<std::string> *ls) const {
    std::string s;
    if (get_devname(&s) == 0) {
      ls->insert(s);
    }
    return 0;
  }
  virtual int get_numa_node(int *node) const {
    return -EOPNOTSUPP;
  }

  virtual int read(
    uint64_t off,
    uint64_t len,
    ceph::buffer::list *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,
    ceph::buffer::list& bl,
    bool buffered,
    int write_hint = WRITE_LIFE_NOT_SET) = 0;

  virtual int aio_read(
    uint64_t off,
    uint64_t len,
    ceph::buffer::list *pbl,
    IOContext *ioc) = 0;
  virtual int aio_write(
    uint64_t off,
    ceph::buffer::list& bl,
    IOContext *ioc,
    bool buffered,
    int write_hint = WRITE_LIFE_NOT_SET) = 0;
  virtual int flush() = 0;
  virtual int discard(uint64_t offset, uint64_t len) { return 0; }
  virtual int queue_discard(interval_set<uint64_t> &to_release) { return -1; }
  virtual void discard_drain() { return; }

  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;
};

#endif //CEPH_BLK_BLOCKDEVICE_H