Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / os / bluestore / KernelDevice.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) 2014 Red Hat
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #ifndef CEPH_OS_BLUESTORE_KERNELDEVICE_H
16 #define CEPH_OS_BLUESTORE_KERNELDEVICE_H
17
18 #include <atomic>
19
20 #include "os/fs/FS.h"
21 #include "include/interval_set.h"
22
23 #include "aio.h"
24 #include "BlockDevice.h"
25
26 class KernelDevice : public BlockDevice {
27   int fd_direct, fd_buffered;
28   uint64_t size;
29   uint64_t block_size;
30   std::string path;
31   FS *fs;
32   bool aio, dio;
33
34   Mutex debug_lock;
35   interval_set<uint64_t> debug_inflight;
36
37   std::atomic<bool> io_since_flush = {false};
38   std::mutex flush_mutex;
39
40   aio_queue_t aio_queue;
41   aio_callback_t aio_callback;
42   void *aio_callback_priv;
43   bool aio_stop;
44
45   struct AioCompletionThread : public Thread {
46     KernelDevice *bdev;
47     explicit AioCompletionThread(KernelDevice *b) : bdev(b) {}
48     void *entry() override {
49       bdev->_aio_thread();
50       return NULL;
51     }
52   } aio_thread;
53
54   std::atomic_int injecting_crash;
55
56   void _aio_thread();
57   int _aio_start();
58   void _aio_stop();
59
60   void _aio_log_start(IOContext *ioc, uint64_t offset, uint64_t length);
61   void _aio_log_finish(IOContext *ioc, uint64_t offset, uint64_t length);
62
63   int _sync_write(uint64_t off, bufferlist& bl, bool buffered);
64
65   int _lock();
66
67   int direct_read_unaligned(uint64_t off, uint64_t len, char *buf);
68
69   // stalled aio debugging
70   aio_list_t debug_queue;
71   std::mutex debug_queue_lock;
72   aio_t *debug_oldest = nullptr;
73   utime_t debug_stall_since;
74   void debug_aio_link(aio_t& aio);
75   void debug_aio_unlink(aio_t& aio);
76
77 public:
78   KernelDevice(CephContext* cct, aio_callback_t cb, void *cbpriv);
79
80   void aio_submit(IOContext *ioc) override;
81
82   uint64_t get_size() const override {
83     return size;
84   }
85   uint64_t get_block_size() const override {
86     return block_size;
87   }
88
89   int collect_metadata(std::string prefix, map<std::string,std::string> *pm) const override;
90
91   int read(uint64_t off, uint64_t len, bufferlist *pbl,
92            IOContext *ioc,
93            bool buffered) override;
94   int aio_read(uint64_t off, uint64_t len, bufferlist *pbl,
95                IOContext *ioc) override;
96   int read_random(uint64_t off, uint64_t len, char *buf, bool buffered) override;
97
98   int write(uint64_t off, bufferlist& bl, bool buffered) override;
99   int aio_write(uint64_t off, bufferlist& bl,
100                 IOContext *ioc,
101                 bool buffered) override;
102   int flush() override;
103
104   // for managing buffered readers/writers
105   int invalidate_cache(uint64_t off, uint64_t len) override;
106   int open(const std::string& path) override;
107   void close() override;
108 };
109
110 #endif