]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/bluestore/BlockDevice.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / os / bluestore / BlockDevice.h
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"
26 #include "os/fs/aio.h"
27
28 #define SPDK_PREFIX "spdk:"
29
30 /// track in-flight io
31 struct IOContext {
32 CephContext* cct;
33 void *priv;
34 #ifdef HAVE_SPDK
35 void *nvme_task_first = nullptr;
36 void *nvme_task_last = nullptr;
37 #endif
38
39 std::mutex lock;
40 std::condition_variable cond;
41
42 std::list<aio_t> pending_aios; ///< not yet submitted
43 std::list<aio_t> running_aios; ///< submitting or submitted
44 std::atomic_int num_pending = {0};
45 std::atomic_int num_running = {0};
46
47 explicit IOContext(CephContext* cct, void *p)
48 : cct(cct), priv(p)
49 {}
50
51 // no copying
52 IOContext(const IOContext& other) = delete;
53 IOContext &operator=(const IOContext& other) = delete;
54
55 bool has_pending_aios() {
56 return num_pending.load();
57 }
58
59 void aio_wait();
60
61 void aio_wake() {
62 std::lock_guard<std::mutex> l(lock);
63 cond.notify_all();
64 --num_running;
65 assert(num_running == 0);
66 }
67 };
68
69
70 class BlockDevice {
71 public:
72 CephContext* cct;
73 private:
74 std::mutex ioc_reap_lock;
75 std::vector<IOContext*> ioc_reap_queue;
76 std::atomic_int ioc_reap_count = {0};
77
78 protected:
79 bool rotational = true;
80
81 public:
82 BlockDevice(CephContext* cct) : cct(cct) {}
83 virtual ~BlockDevice() = default;
84 typedef void (*aio_callback_t)(void *handle, void *aio);
85
86 static BlockDevice *create(
87 CephContext* cct, const std::string& path, aio_callback_t cb, void *cbpriv);
88 virtual bool supported_bdev_label() { return true; }
89 virtual bool is_rotational() { return rotational; }
90
91 virtual void aio_submit(IOContext *ioc) = 0;
92
93 virtual uint64_t get_size() const = 0;
94 virtual uint64_t get_block_size() const = 0;
95
96 virtual int collect_metadata(std::string prefix, std::map<std::string,std::string> *pm) const = 0;
97
98 virtual int read(
99 uint64_t off,
100 uint64_t len,
101 bufferlist *pbl,
102 IOContext *ioc,
103 bool buffered) = 0;
104 virtual int read_random(
105 uint64_t off,
106 uint64_t len,
107 char *buf,
108 bool buffered) = 0;
109 virtual int write(
110 uint64_t off,
111 bufferlist& bl,
112 bool buffered) = 0;
113
114 virtual int aio_read(
115 uint64_t off,
116 uint64_t len,
117 bufferlist *pbl,
118 IOContext *ioc) = 0;
119 virtual int aio_write(
120 uint64_t off,
121 bufferlist& bl,
122 IOContext *ioc,
123 bool buffered) = 0;
124 virtual int flush() = 0;
125
126 void queue_reap_ioc(IOContext *ioc);
127 void reap_ioc();
128
129 // for managing buffered readers/writers
130 virtual int invalidate_cache(uint64_t off, uint64_t len) = 0;
131 virtual int open(const std::string& path) = 0;
132 virtual void close() = 0;
133 };
134
135 #endif //CEPH_OS_BLUESTORE_BLOCKDEVICE_H