summaryrefslogtreecommitdiffstats
path: root/src/librbd/operation/Request.h
blob: e32b49644cebbfd25a4b7c66787154dbd2522a2a (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_OPERATION_REQUEST_H
#define CEPH_LIBRBD_OPERATION_REQUEST_H

#include "librbd/AsyncRequest.h"
#include "include/Context.h"
#include "common/RWLock.h"
#include "librbd/Utils.h"
#include "librbd/Journal.h"

namespace librbd {

class ImageCtx;

namespace operation {

template <typename ImageCtxT = ImageCtx>
class Request : public AsyncRequest<ImageCtxT> {
public:
  Request(ImageCtxT &image_ctx, Context *on_finish,
          uint64_t journal_op_tid = 0);

  void send();

protected:
  void finish(int r) override;
  virtual void send_op() = 0;

  virtual bool can_affect_io() const {
    return false;
  }
  virtual journal::Event create_event(uint64_t op_tid) const = 0;

  template <typename T, Context*(T::*MF)(int*)>
  bool append_op_event(T *request) {
    ImageCtxT &image_ctx = this->m_image_ctx;

    ceph_assert(can_affect_io());
    std::scoped_lock locker{image_ctx.owner_lock, image_ctx.image_lock};
    if (image_ctx.journal != nullptr) {
      if (image_ctx.journal->is_journal_replaying()) {
        Context *ctx = util::create_context_callback<T, MF>(request);
        replay_op_ready(ctx);
        return true;
      } else if (image_ctx.journal->is_journal_appending()) {
        Context *ctx = util::create_context_callback<T, MF>(request);
        append_op_event(ctx);
        return true;
      }
    }
    return false;
  }

  bool append_op_event();

  // NOTE: temporary until converted to new state machine format
  Context *create_context_finisher(int r);
  void finish_and_destroy(int r) override;

private:
  struct C_AppendOpEvent : public Context {
    Request *request;
    Context *on_safe;
    C_AppendOpEvent(Request *request, Context *on_safe)
      : request(request), on_safe(on_safe) {
    }
    void finish(int r) override {
      if (r >= 0) {
        request->m_appended_op_event = true;
      }
      on_safe->complete(r);
    }
  };

  struct C_CommitOpEvent : public Context {
    Request *request;
    int ret_val;
    C_CommitOpEvent(Request *request, int ret_val)
      : request(request), ret_val(ret_val) {
    }
    void finish(int r) override {
      request->handle_commit_op_event(r, ret_val);
      delete request;
    }
  };

  uint64_t m_op_tid = 0;
  bool m_appended_op_event = false;
  bool m_committed_op_event = false;

  void replay_op_ready(Context *on_safe);
  void append_op_event(Context *on_safe);
  void handle_op_event_safe(int r);

  bool commit_op_event(int r);
  void handle_commit_op_event(int r, int original_ret_val);

};

} // namespace operation
} // namespace librbd

extern template class librbd::operation::Request<librbd::ImageCtx>;

#endif // CEPH_LIBRBD_OPERATION_REQUEST_H