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

#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif // _LARGEFILE64_SOURCE

#include "librbd/migration/FileStream.h"
#include "common/dout.h"
#include "common/errno.h"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/asio/Utils.h"
#include <boost/asio/buffer.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/read.hpp>
#include <fcntl.h>
#include <unistd.h>

namespace librbd {
namespace migration {

namespace {

const std::string FILE_PATH {"file_path"};

} // anonymous namespace

#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::migration::FileStream::ReadRequest " \
                           << this << " " << __func__ << ": "

template <typename I>
struct FileStream<I>::ReadRequest {
  FileStream* file_stream;
  io::Extents byte_extents;
  bufferlist* data;
  Context* on_finish;
  size_t index = 0;

  ReadRequest(FileStream* file_stream, io::Extents&& byte_extents,
              bufferlist* data, Context* on_finish)
    : file_stream(file_stream), byte_extents(std::move(byte_extents)),
      data(data), on_finish(on_finish) {
    auto cct = file_stream->m_cct;
    ldout(cct, 20) << dendl;
  }

  void send() {
    data->clear();
    read();
  }

  void read() {
    auto cct = file_stream->m_cct;
    if (index >= byte_extents.size()) {
      finish(0);
      return;
    }

    auto& byte_extent = byte_extents[index++];
    ldout(cct, 20) << "byte_extent=" << byte_extent << dendl;

    auto ptr = buffer::ptr_node::create(buffer::create_small_page_aligned(
      byte_extent.second));
    auto buffer = boost::asio::mutable_buffer(
      ptr->c_str(), byte_extent.second);
    data->push_back(std::move(ptr));

    int r;
    auto offset = lseek64(file_stream->m_file_no, byte_extent.first, SEEK_SET);
    if (offset == -1) {
      r = -errno;
      lderr(cct) << "failed to seek file stream: " << cpp_strerror(r) << dendl;
      finish(r);
      return;
    }

    boost::system::error_code ec;
    size_t bytes_read = boost::asio::read(
      *file_stream->m_stream_descriptor, std::move(buffer), ec);
    r = -ec.value();
    if (r < 0 && r != -ENOENT) {
      lderr(cct) << "failed to read from file stream: " << cpp_strerror(r)
                 << dendl;
      finish(r);
      return;
    } else if (bytes_read < byte_extent.second) {
      lderr(cct) << "failed to read " << byte_extent.second << " bytes from "
                 << "file stream" << dendl;
      finish(-ERANGE);
      return;
    }

    // re-queue the remainder of the read requests
    boost::asio::post(file_stream->m_strand, [this]() { read(); });
  }

  void finish(int r) {
    auto cct = file_stream->m_cct;
    ldout(cct, 20) << "r=" << r << dendl;

    if (r < 0) {
      data->clear();
    }

    on_finish->complete(r);
    delete this;
  }
};

#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

#undef dout_prefix
#define dout_prefix *_dout << "librbd::migration::FileStream: " << this \
                           << " " << __func__ << ": "

template <typename I>
FileStream<I>::FileStream(I* image_ctx, const json_spirit::mObject& json_object)
  : m_cct(image_ctx->cct), m_asio_engine(image_ctx->asio_engine),
    m_json_object(json_object),
    m_strand(boost::asio::make_strand(*m_asio_engine)) {
}

template <typename I>
FileStream<I>::~FileStream() {
  if (m_file_no != -1) {
    ::close(m_file_no);
  }
}

#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

template <typename I>
void FileStream<I>::open(Context* on_finish) {
  auto& file_path_value = m_json_object[FILE_PATH];
  if (file_path_value.type() != json_spirit::str_type) {
    lderr(m_cct) << "failed to locate '" << FILE_PATH << "' key" << dendl;
    on_finish->complete(-EINVAL);
    return;
  }

  auto& file_path = file_path_value.get_str();
  ldout(m_cct, 10) << "file_path=" << file_path << dendl;

  m_file_no = ::open(file_path.c_str(), O_RDONLY);
  if (m_file_no < 0) {
    int r = -errno;
    lderr(m_cct) << "failed to open file stream '" << file_path << "': "
                 << cpp_strerror(r) << dendl;
    on_finish->complete(r);
    return;
  }

  m_stream_descriptor = std::make_optional<
    boost::asio::posix::stream_descriptor>(m_strand, m_file_no);
  on_finish->complete(0);
}

template <typename I>
void FileStream<I>::close(Context* on_finish) {
  ldout(m_cct, 10) << dendl;

  m_stream_descriptor.reset();
  on_finish->complete(0);
}

template <typename I>
void FileStream<I>::get_size(uint64_t* size, Context* on_finish) {
  ldout(m_cct, 10) << dendl;

  // execute IO operations in a single strand to prevent seek races
  boost::asio::post(
    m_strand, [this, size, on_finish]() {
      auto offset = lseek64(m_file_no, 0, SEEK_END);
      if (offset == -1) {
        int r = -errno;
        lderr(m_cct) << "failed to seek to file end: " << cpp_strerror(r)
                     << dendl;
        on_finish->complete(r);
        return;
      }

      ldout(m_cct, 10) << "size=" << offset << dendl;
      *size = offset;
      on_finish->complete(0);
    });
}

template <typename I>
void FileStream<I>::read(io::Extents&& byte_extents, bufferlist* data,
                         Context* on_finish) {
  ldout(m_cct, 20) << byte_extents << dendl;

  auto ctx = new ReadRequest(this, std::move(byte_extents), data, on_finish);

  // execute IO operations in a single strand to prevent seek races
  boost::asio::post(m_strand, [ctx]() { ctx->send(); });
}

#else  // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

template <typename I>
void FileStream<I>::open(Context* on_finish) {
  on_finish->complete(-EIO);
}

template <typename I>
void FileStream<I>::close(Context* on_finish) {
  on_finish->complete(-EIO);
}

template <typename I>
void FileStream<I>::get_size(uint64_t* size, Context* on_finish) {
  on_finish->complete(-EIO);
}

template <typename I>
void FileStream<I>::read(io::Extents&& byte_extents, bufferlist* data,
                         Context* on_finish) {
  on_finish->complete(-EIO);
}

#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR

} // namespace migration
} // namespace librbd

template class librbd::migration::FileStream<librbd::ImageCtx>;