]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/io/ReadResult.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / io / ReadResult.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "librbd/io/ReadResult.h"
5 #include "include/buffer.h"
6 #include "common/dout.h"
7 #include "librbd/io/AioCompletion.h"
8 #include <boost/variant/apply_visitor.hpp>
9 #include <boost/variant/static_visitor.hpp>
10
11 #define dout_subsys ceph_subsys_rbd
12 #undef dout_prefix
13 #define dout_prefix *_dout << "librbd::io::ReadResult: " << this \
14 << " " << __func__ << ": "
15
16 namespace librbd {
17 namespace io {
18
19 struct ReadResult::SetClipLengthVisitor : public boost::static_visitor<void> {
20 size_t length;
21
22 explicit SetClipLengthVisitor(size_t length) : length(length) {
23 }
24
25 void operator()(Linear &linear) const {
26 ceph_assert(length <= linear.buf_len);
27 linear.buf_len = length;
28 }
29
30 template <typename T>
31 void operator()(T &t) const {
32 }
33 };
34
35 struct ReadResult::AssembleResultVisitor : public boost::static_visitor<void> {
36 CephContext *cct;
37 Striper::StripedReadResult &destriper;
38
39 AssembleResultVisitor(CephContext *cct, Striper::StripedReadResult &destriper)
40 : cct(cct), destriper(destriper) {
41 }
42
43 void operator()(Empty &empty) const {
44 ldout(cct, 20) << "dropping read result" << dendl;
45 }
46
47 void operator()(Linear &linear) const {
48 ldout(cct, 20) << "copying resulting bytes to "
49 << reinterpret_cast<void*>(linear.buf) << dendl;
50 destriper.assemble_result(cct, linear.buf, linear.buf_len);
51 }
52
53 void operator()(Vector &vector) const {
54 bufferlist bl;
55 destriper.assemble_result(cct, bl, true);
56
57 ldout(cct, 20) << "copying resulting " << bl.length() << " bytes to iovec "
58 << reinterpret_cast<const void*>(vector.iov) << dendl;
59
60 bufferlist::iterator it = bl.begin();
61 size_t length = bl.length();
62 size_t offset = 0;
63 int idx = 0;
64 for (; offset < length && idx < vector.iov_count; idx++) {
65 size_t len = std::min(vector.iov[idx].iov_len, length - offset);
66 it.copy(len, static_cast<char *>(vector.iov[idx].iov_base));
67 offset += len;
68 }
69 ceph_assert(offset == bl.length());
70 }
71
72 void operator()(Bufferlist &bufferlist) const {
73 bufferlist.bl->clear();
74 destriper.assemble_result(cct, *bufferlist.bl, true);
75
76 ldout(cct, 20) << "moved resulting " << bufferlist.bl->length() << " "
77 << "bytes to bl " << reinterpret_cast<void*>(bufferlist.bl)
78 << dendl;
79 }
80 };
81
82 ReadResult::C_ImageReadRequest::C_ImageReadRequest(
83 AioCompletion *aio_completion, const Extents image_extents)
84 : aio_completion(aio_completion), image_extents(image_extents) {
85 aio_completion->add_request();
86 }
87
88 void ReadResult::C_ImageReadRequest::finish(int r) {
89 CephContext *cct = aio_completion->ictx->cct;
90 ldout(cct, 10) << "C_ImageReadRequest: r=" << r
91 << dendl;
92 if (r >= 0) {
93 size_t length = 0;
94 for (auto &image_extent : image_extents) {
95 length += image_extent.second;
96 }
97 ceph_assert(length == bl.length());
98
99 aio_completion->lock.Lock();
100 aio_completion->read_result.m_destriper.add_partial_result(
101 cct, bl, image_extents);
102 aio_completion->lock.Unlock();
103 r = length;
104 }
105
106 aio_completion->complete_request(r);
107 }
108
109 ReadResult::C_ObjectReadRequest::C_ObjectReadRequest(
110 AioCompletion *aio_completion, uint64_t object_off, uint64_t object_len,
111 Extents&& buffer_extents)
112 : aio_completion(aio_completion), object_off(object_off),
113 object_len(object_len), buffer_extents(std::move(buffer_extents)) {
114 aio_completion->add_request();
115 }
116
117 void ReadResult::C_ObjectReadRequest::finish(int r) {
118 CephContext *cct = aio_completion->ictx->cct;
119 ldout(cct, 10) << "C_ObjectReadRequest: r=" << r
120 << dendl;
121
122 if (r == -ENOENT) {
123 r = 0;
124 }
125 if (r >= 0) {
126 ldout(cct, 10) << " got " << extent_map
127 << " for " << buffer_extents
128 << " bl " << bl.length() << dendl;
129 // handle the case where a sparse-read wasn't issued
130 if (extent_map.empty()) {
131 extent_map[object_off] = bl.length();
132 }
133
134 aio_completion->lock.Lock();
135 aio_completion->read_result.m_destriper.add_partial_sparse_result(
136 cct, bl, extent_map, object_off, buffer_extents);
137 aio_completion->lock.Unlock();
138
139 r = object_len;
140 }
141
142 aio_completion->complete_request(r);
143 }
144
145 ReadResult::ReadResult() : m_buffer(Empty()) {
146 }
147
148 ReadResult::ReadResult(char *buf, size_t buf_len)
149 : m_buffer(Linear(buf, buf_len)) {
150 }
151
152 ReadResult::ReadResult(const struct iovec *iov, int iov_count)
153 : m_buffer(Vector(iov, iov_count)) {
154 }
155
156 ReadResult::ReadResult(ceph::bufferlist *bl)
157 : m_buffer(Bufferlist(bl)) {
158 }
159
160 void ReadResult::set_clip_length(size_t length) {
161 boost::apply_visitor(SetClipLengthVisitor(length), m_buffer);
162 }
163
164 void ReadResult::assemble_result(CephContext *cct) {
165 boost::apply_visitor(AssembleResultVisitor(cct, m_destriper), m_buffer);
166 }
167
168 } // namespace io
169 } // namespace librbd
170