summaryrefslogtreecommitdiff
path: root/src/librbd/AioRequest.h
blob: f97f840705957b72e91624a12ba39d92f364b57f (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_LIBRBD_AIOREQUEST_H
#define CEPH_LIBRBD_AIOREQUEST_H

#include "inttypes.h"
#include <map>

#include "include/buffer.h"
#include "include/Context.h"
#include "include/rados/librados.hpp"
#include "common/snap_types.h"

namespace librbd {

  class AioCompletion;
  class ImageCtx;

  /**
   * This class represents an I/O operation to a single RBD data object.
   * Its subclasses encapsulate logic for dealing with special cases
   * for I/O due to layering.
   */
  class AioRequest
  {
  public:
    AioRequest();
    AioRequest(ImageCtx *ictx, const std::string &oid,
	       uint64_t objectno, uint64_t off, uint64_t len,
	       librados::snap_t snap_id, Context *completion,
	       bool hide_enoent);
    virtual ~AioRequest();

    void complete(int r)
    {
      if (should_complete(r)) {
	if (m_hide_enoent && r == -ENOENT)
	  r = 0;
	m_completion->complete(r);
	delete this;
      }
    }

    virtual bool should_complete(int r) = 0;
    virtual int send() = 0;

  protected:
    void read_from_parent(vector<pair<uint64_t,uint64_t> >& image_extents);

    ImageCtx *m_ictx;
    librados::IoCtx m_ioctx;
    std::string m_oid;
    uint64_t m_object_no, m_object_off, m_object_len;
    librados::snap_t m_snap_id;
    Context *m_completion;
    AioCompletion *m_parent_completion;
    ceph::bufferlist m_read_data;
    bool m_hide_enoent;
  };

  class AioRead : public AioRequest {
  public:
    AioRead(ImageCtx *ictx, const std::string &oid,
	    uint64_t objectno, uint64_t offset, uint64_t len,
	    vector<pair<uint64_t,uint64_t> >& be,
	    librados::snap_t snap_id, bool sparse,
	    Context *completion)
      : AioRequest(ictx, oid, objectno, offset, len, snap_id, completion, false),
	m_buffer_extents(be),
	m_tried_parent(false), m_sparse(sparse) {
      m_ioctx.snap_set_read(m_snap_id);
    }
    virtual ~AioRead() {}
    virtual bool should_complete(int r);
    virtual int send();

    ceph::bufferlist &data() {
      return m_read_data;
    }
    std::map<uint64_t, uint64_t> m_ext_map;

    friend class C_AioRead;

  private:
    vector<pair<uint64_t,uint64_t> > m_buffer_extents;
    bool m_tried_parent;
    bool m_sparse;
  };

  class AbstractWrite : public AioRequest {
  public:
    AbstractWrite();
    AbstractWrite(ImageCtx *ictx, const std::string &oid,
		  uint64_t object_no, uint64_t object_off, uint64_t len,
		  vector<pair<uint64_t,uint64_t> >& objectx, uint64_t object_overlap,
		  const ::SnapContext &snapc,
		  librados::snap_t snap_id,
		  Context *completion,
		  bool hide_enoent);
    virtual ~AbstractWrite() {}
    virtual bool should_complete(int r);
    virtual int send();
    void guard_write();

    bool has_parent() const {
      return !m_object_image_extents.empty();
    }

  private:
    /**
     * Writes go through the following state machine to deal with
     * layering:
     *
     *                           need copyup
     * LIBRBD_AIO_WRITE_GUARD ---------------> LIBRBD_AIO_WRITE_COPYUP
     *           |        ^                              |
     *           v        \------------------------------/
     *         done
     *           ^
     *           |
     * LIBRBD_AIO_WRITE_FLAT
     *
     * Writes start in LIBRBD_AIO_WRITE_GUARD or _FLAT, depending on whether
     * there is a parent or not.
     */
    enum write_state_d {
      LIBRBD_AIO_WRITE_GUARD,
      LIBRBD_AIO_WRITE_COPYUP,
      LIBRBD_AIO_WRITE_FLAT
    };

  protected:
    virtual void add_copyup_ops() = 0;

    write_state_d m_state;
    vector<pair<uint64_t,uint64_t> > m_object_image_extents;
    uint64_t m_parent_overlap;
    librados::ObjectWriteOperation m_write;
    librados::ObjectWriteOperation m_copyup;

  private:
    void send_copyup();
  };

  class AioWrite : public AbstractWrite {
  public:
    AioWrite(ImageCtx *ictx, const std::string &oid,
	     uint64_t object_no, uint64_t object_off,
	     vector<pair<uint64_t,uint64_t> >& objectx, uint64_t object_overlap,
	     const ceph::bufferlist &data, const ::SnapContext &snapc,
	     librados::snap_t snap_id,
	     Context *completion)
      : AbstractWrite(ictx, oid,
		      object_no, object_off, data.length(),
		      objectx, object_overlap,
		      snapc, snap_id,
		      completion, false),
	m_write_data(data) {
      guard_write();
      m_write.write(m_object_off, data);
    }
    virtual ~AioWrite() {}

  protected:
    virtual void add_copyup_ops() {
      m_copyup.write(m_object_off, m_write_data);
    }

  private:
    ceph::bufferlist m_write_data;
  };

  class AioRemove : public AbstractWrite {
  public:
    AioRemove(ImageCtx *ictx, const std::string &oid,
	      uint64_t object_no,
	      vector<pair<uint64_t,uint64_t> >& objectx, uint64_t object_overlap,
	      const ::SnapContext &snapc, librados::snap_t snap_id,
	      Context *completion)
      : AbstractWrite(ictx, oid,
		      object_no, 0, 0,
		      objectx, object_overlap,
		      snapc, snap_id, completion,
		      true) {
      if (has_parent())
	m_write.truncate(0);
      else
	m_write.remove();
    }
    virtual ~AioRemove() {}

  protected:
    virtual void add_copyup_ops() {
      // removing an object never needs to copyup
      assert(0);
    }
  };

  class AioTruncate : public AbstractWrite {
  public:
    AioTruncate(ImageCtx *ictx, const std::string &oid,
		uint64_t object_no, uint64_t object_off,
		vector<pair<uint64_t,uint64_t> >& objectx, uint64_t object_overlap,
		const ::SnapContext &snapc, librados::snap_t snap_id,
		Context *completion)
      : AbstractWrite(ictx, oid,
		      object_no, object_off, 0,
		      objectx, object_overlap,
		      snapc, snap_id, completion,
		      true) {
      guard_write();
      m_write.truncate(object_off);
    }
    virtual ~AioTruncate() {}

  protected:
    virtual void add_copyup_ops() {
      m_copyup.truncate(m_object_off);
    }
  };

  class AioZero : public AbstractWrite {
  public:
    AioZero(ImageCtx *ictx, const std::string &oid,
	    uint64_t object_no, uint64_t object_off, uint64_t object_len,
	    vector<pair<uint64_t,uint64_t> >& objectx, uint64_t object_overlap,
	    const ::SnapContext &snapc, librados::snap_t snap_id,
	    Context *completion)
      : AbstractWrite(ictx, oid,
		      object_no, object_off, object_len,
		      objectx, object_overlap,
		      snapc, snap_id, completion,
		      true) {
      guard_write();
      m_write.zero(object_off, object_len);
    }
    virtual ~AioZero() {}

  protected:
    virtual void add_copyup_ops() {
      m_copyup.zero(m_object_off, m_object_len);
    }
  };

}

#endif