]> git.proxmox.com Git - ceph.git/blame - ceph/src/os/bluestore/BlockDevice.h
update sources to v12.2.3
[ceph.git] / ceph / src / os / bluestore / BlockDevice.h
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) 2015 XSky <haomai@xsky.com>
7 *
8 * Author: Haomai Wang <haomaiwang@gmail.com>
9 *
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
14 *
15 */
16
17#ifndef CEPH_OS_BLUESTORE_BLOCKDEVICE_H
18#define CEPH_OS_BLUESTORE_BLOCKDEVICE_H
19
20#include <atomic>
21#include <condition_variable>
22#include <mutex>
23#include <list>
24
25#include "acconfig.h"
c07f9fc5 26#include "aio.h"
7c673cae
FG
27
28#define SPDK_PREFIX "spdk:"
29
30/// track in-flight io
31struct IOContext {
31f18b77
FG
32private:
33 std::mutex lock;
34 std::condition_variable cond;
b32b8144 35 int r = 0;
31f18b77
FG
36
37public:
7c673cae
FG
38 CephContext* cct;
39 void *priv;
40#ifdef HAVE_SPDK
41 void *nvme_task_first = nullptr;
42 void *nvme_task_last = nullptr;
43#endif
44
7c673cae
FG
45
46 std::list<aio_t> pending_aios; ///< not yet submitted
47 std::list<aio_t> running_aios; ///< submitting or submitted
48 std::atomic_int num_pending = {0};
49 std::atomic_int num_running = {0};
b32b8144 50 bool allow_eio;
7c673cae 51
b32b8144
FG
52 explicit IOContext(CephContext* cct, void *p, bool allow_eio = false)
53 : cct(cct), priv(p), allow_eio(allow_eio)
7c673cae
FG
54 {}
55
56 // no copying
57 IOContext(const IOContext& other) = delete;
58 IOContext &operator=(const IOContext& other) = delete;
59
60 bool has_pending_aios() {
61 return num_pending.load();
62 }
63
64 void aio_wait();
65
31f18b77
FG
66 void try_aio_wake() {
67 if (num_running == 1) {
68
69 // we might have some pending IOs submitted after the check
70 // as there is no lock protection for aio_submit.
71 // Hence we might have false conditional trigger.
72 // aio_wait has to handle that hence do not care here.
73 std::lock_guard<std::mutex> l(lock);
74 cond.notify_all();
75 --num_running;
76 assert(num_running >= 0);
77 } else {
78 --num_running;
79 }
7c673cae 80 }
b32b8144
FG
81
82 void set_return_value(int _r) {
83 r = _r;
84 }
85
86 int get_return_value() const {
87 return r;
88 }
7c673cae
FG
89};
90
91
92class BlockDevice {
93public:
94 CephContext* cct;
95private:
96 std::mutex ioc_reap_lock;
97 std::vector<IOContext*> ioc_reap_queue;
98 std::atomic_int ioc_reap_count = {0};
99
100protected:
101 bool rotational = true;
102
103public:
104 BlockDevice(CephContext* cct) : cct(cct) {}
105 virtual ~BlockDevice() = default;
106 typedef void (*aio_callback_t)(void *handle, void *aio);
107
108 static BlockDevice *create(
109 CephContext* cct, const std::string& path, aio_callback_t cb, void *cbpriv);
110 virtual bool supported_bdev_label() { return true; }
111 virtual bool is_rotational() { return rotational; }
112
113 virtual void aio_submit(IOContext *ioc) = 0;
114
115 virtual uint64_t get_size() const = 0;
116 virtual uint64_t get_block_size() const = 0;
117
118 virtual int collect_metadata(std::string prefix, std::map<std::string,std::string> *pm) const = 0;
119
120 virtual int read(
121 uint64_t off,
122 uint64_t len,
123 bufferlist *pbl,
124 IOContext *ioc,
125 bool buffered) = 0;
126 virtual int read_random(
127 uint64_t off,
128 uint64_t len,
129 char *buf,
130 bool buffered) = 0;
131 virtual int write(
132 uint64_t off,
133 bufferlist& bl,
134 bool buffered) = 0;
135
136 virtual int aio_read(
137 uint64_t off,
138 uint64_t len,
139 bufferlist *pbl,
140 IOContext *ioc) = 0;
141 virtual int aio_write(
142 uint64_t off,
143 bufferlist& bl,
144 IOContext *ioc,
145 bool buffered) = 0;
146 virtual int flush() = 0;
147
148 void queue_reap_ioc(IOContext *ioc);
149 void reap_ioc();
150
151 // for managing buffered readers/writers
152 virtual int invalidate_cache(uint64_t off, uint64_t len) = 0;
153 virtual int open(const std::string& path) = 0;
154 virtual void close() = 0;
155};
156
157#endif //CEPH_OS_BLUESTORE_BLOCKDEVICE_H