summaryrefslogtreecommitdiffstats
path: root/src/librbd/trash
diff options
context:
space:
mode:
Diffstat (limited to 'src/librbd/trash')
-rw-r--r--src/librbd/trash/MoveRequest.cc126
-rw-r--r--src/librbd/trash/MoveRequest.h87
-rw-r--r--src/librbd/trash/RemoveRequest.cc170
-rw-r--r--src/librbd/trash/RemoveRequest.h118
4 files changed, 501 insertions, 0 deletions
diff --git a/src/librbd/trash/MoveRequest.cc b/src/librbd/trash/MoveRequest.cc
new file mode 100644
index 000000000..7b7abe452
--- /dev/null
+++ b/src/librbd/trash/MoveRequest.cc
@@ -0,0 +1,126 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/trash/MoveRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/Utils.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::trash::MoveRequest: " << this \
+ << " " << __func__ << ": "
+
+namespace librbd {
+namespace trash {
+
+using util::create_context_callback;
+using util::create_rados_callback;
+
+template <typename I>
+void MoveRequest<I>::send() {
+ trash_add();
+}
+
+template <typename I>
+void MoveRequest<I>::trash_add() {
+ ldout(m_cct, 10) << dendl;
+
+ librados::ObjectWriteOperation op;
+ librbd::cls_client::trash_add(&op, m_image_id, m_trash_image_spec);
+
+ auto aio_comp = create_rados_callback<
+ MoveRequest<I>, &MoveRequest<I>::handle_trash_add>(this);
+ int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op);
+ ceph_assert(r == 0);
+ aio_comp->release();
+}
+
+template <typename I>
+void MoveRequest<I>::handle_trash_add(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r == -EEXIST) {
+ ldout(m_cct, 10) << "previous unfinished deferred remove for image: "
+ << m_image_id << dendl;
+ } else if (r < 0) {
+ lderr(m_cct) << "failed to add image to trash: " << cpp_strerror(r)
+ << dendl;
+ finish(r);
+ return;
+ }
+
+ remove_id();
+}
+
+template <typename I>
+void MoveRequest<I>::remove_id() {
+ ldout(m_cct, 10) << dendl;
+
+ auto aio_comp = create_rados_callback<
+ MoveRequest<I>, &MoveRequest<I>::handle_remove_id>(this);
+ int r = m_io_ctx.aio_remove(util::id_obj_name(m_trash_image_spec.name),
+ aio_comp);
+ ceph_assert(r == 0);
+ aio_comp->release();
+}
+
+template <typename I>
+void MoveRequest<I>::handle_remove_id(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r < 0 && r != -ENOENT) {
+ lderr(m_cct) << "failed to remove image id object: " << cpp_strerror(r)
+ << dendl;
+ finish(r);
+ return;
+ }
+
+ directory_remove();
+}
+
+template <typename I>
+void MoveRequest<I>::directory_remove() {
+ ldout(m_cct, 10) << dendl;
+
+ librados::ObjectWriteOperation op;
+ librbd::cls_client::dir_remove_image(&op, m_trash_image_spec.name,
+ m_image_id);
+
+ auto aio_comp = create_rados_callback<
+ MoveRequest<I>, &MoveRequest<I>::handle_directory_remove>(this);
+ int r = m_io_ctx.aio_operate(RBD_DIRECTORY, aio_comp, &op);
+ ceph_assert(r == 0);
+ aio_comp->release();
+}
+
+template <typename I>
+void MoveRequest<I>::handle_directory_remove(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r == -ENOENT) {
+ r = 0;
+ }
+ if (r < 0) {
+ lderr(m_cct) << "failed to remove image from directory: " << cpp_strerror(r)
+ << dendl;
+ }
+
+ finish(r);
+}
+
+template <typename I>
+void MoveRequest<I>::finish(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ m_on_finish->complete(r);
+ delete this;
+}
+
+} // namespace trash
+} // namespace librbd
+
+template class librbd::trash::MoveRequest<librbd::ImageCtx>;
diff --git a/src/librbd/trash/MoveRequest.h b/src/librbd/trash/MoveRequest.h
new file mode 100644
index 000000000..d08011e85
--- /dev/null
+++ b/src/librbd/trash/MoveRequest.h
@@ -0,0 +1,87 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_TRASH_MOVE_REQUEST_H
+#define CEPH_LIBRBD_TRASH_MOVE_REQUEST_H
+
+#include "include/common_fwd.h"
+#include "include/utime.h"
+#include "include/rados/librados.hpp"
+#include "cls/rbd/cls_rbd_types.h"
+#include <string>
+
+struct Context;
+
+namespace librbd {
+
+struct ImageCtx;
+
+namespace trash {
+
+template <typename ImageCtxT = librbd::ImageCtx>
+class MoveRequest {
+public:
+ static MoveRequest* create(librados::IoCtx& io_ctx,
+ const std::string& image_id,
+ const cls::rbd::TrashImageSpec& trash_image_spec,
+ Context* on_finish) {
+ return new MoveRequest(io_ctx, image_id, trash_image_spec, on_finish);
+ }
+
+ MoveRequest(librados::IoCtx& io_ctx, const std::string& image_id,
+ const cls::rbd::TrashImageSpec& trash_image_spec,
+ Context* on_finish)
+ : m_io_ctx(io_ctx), m_image_id(image_id),
+ m_trash_image_spec(trash_image_spec), m_on_finish(on_finish),
+ m_cct(reinterpret_cast<CephContext *>(io_ctx.cct())) {
+ }
+
+ void send();
+
+private:
+ /*
+ * @verbatim
+ *
+ * <start>
+ * |
+ * v
+ * TRASH_ADD
+ * |
+ * v
+ * REMOVE_ID
+ * |
+ * v
+ * DIRECTORY_REMOVE
+ * |
+ * v
+ * <finish>
+ *
+ * @endverbatim
+ */
+
+ librados::IoCtx &m_io_ctx;
+ std::string m_image_id;
+ cls::rbd::TrashImageSpec m_trash_image_spec;
+ Context *m_on_finish;
+
+ CephContext *m_cct;
+
+ void trash_add();
+ void handle_trash_add(int r);
+
+ void remove_id();
+ void handle_remove_id(int r);
+
+ void directory_remove();
+ void handle_directory_remove(int r);
+
+ void finish(int r);
+
+};
+
+} // namespace trash
+} // namespace librbd
+
+extern template class librbd::trash::MoveRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_TRASH_MOVE_REQUEST_H
diff --git a/src/librbd/trash/RemoveRequest.cc b/src/librbd/trash/RemoveRequest.cc
new file mode 100644
index 000000000..1149d1d80
--- /dev/null
+++ b/src/librbd/trash/RemoveRequest.cc
@@ -0,0 +1,170 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/trash/RemoveRequest.h"
+#include "common/dout.h"
+#include "common/errno.h"
+#include "cls/rbd/cls_rbd_client.h"
+#include "librbd/ExclusiveLock.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/ImageState.h"
+#include "librbd/Utils.h"
+#include "librbd/asio/ContextWQ.h"
+#include "librbd/image/RemoveRequest.h"
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::trash::RemoveRequest: " << this \
+ << " " << __func__ << ": "
+
+namespace librbd {
+namespace trash {
+
+using util::create_context_callback;
+using util::create_rados_callback;
+
+template <typename I>
+void RemoveRequest<I>::send() {
+ set_state();
+}
+
+template <typename I>
+void RemoveRequest<I>::set_state() {
+ ldout(m_cct, 10) << dendl;
+
+ librados::ObjectWriteOperation op;
+ cls_client::trash_state_set(&op, m_image_id, m_trash_set_state,
+ m_trash_expect_state);
+
+ auto aio_comp = create_rados_callback<
+ RemoveRequest<I>, &RemoveRequest<I>::handle_set_state>(this);
+ int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op);
+ ceph_assert(r == 0);
+ aio_comp->release();
+}
+
+template <typename I>
+void RemoveRequest<I>::handle_set_state(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r < 0 && r != -EOPNOTSUPP) {
+ lderr(m_cct) << "error setting trash image state: " << cpp_strerror(r)
+ << dendl;
+ if (m_ret_val == 0) {
+ m_ret_val = r;
+ }
+ if (m_trash_set_state == cls::rbd::TRASH_IMAGE_STATE_REMOVING) {
+ close_image();
+ } else {
+ finish(m_ret_val);
+ }
+ return;
+ }
+
+ if (m_trash_set_state == cls::rbd::TRASH_IMAGE_STATE_REMOVING) {
+ remove_image();
+ } else {
+ ceph_assert(m_trash_set_state == cls::rbd::TRASH_IMAGE_STATE_NORMAL);
+ finish(m_ret_val < 0 ? m_ret_val : r);
+ };
+}
+
+template <typename I>
+void RemoveRequest<I>::close_image() {
+ if (m_image_ctx == nullptr) {
+ finish(m_ret_val);
+ return;
+ }
+
+ ldout(m_cct, 10) << dendl;
+
+ auto ctx = create_context_callback<
+ RemoveRequest<I>, &RemoveRequest<I>::handle_close_image>(this);
+ m_image_ctx->state->close(ctx);
+}
+
+template <typename I>
+void RemoveRequest<I>::handle_close_image(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r < 0) {
+ ldout(m_cct, 5) << "failed to close image:" << cpp_strerror(r) << dendl;
+ }
+
+ m_image_ctx = nullptr;
+ finish(m_ret_val);
+}
+
+template <typename I>
+void RemoveRequest<I>::remove_image() {
+ ldout(m_cct, 10) << dendl;
+
+ auto ctx = create_context_callback<
+ RemoveRequest<I>, &RemoveRequest<I>::handle_remove_image>(this);
+ if (m_image_ctx != nullptr) {
+ auto req = librbd::image::RemoveRequest<I>::create(
+ m_io_ctx, m_image_ctx, m_force, true, m_prog_ctx, m_op_work_queue, ctx);
+ req->send();
+ } else {
+ auto req = librbd::image::RemoveRequest<I>::create(
+ m_io_ctx, "", m_image_id, m_force, true, m_prog_ctx, m_op_work_queue,
+ ctx);
+ req->send();
+ }
+}
+
+template <typename I>
+void RemoveRequest<I>::handle_remove_image(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r < 0) {
+ ldout(m_cct, 5) << "failed to remove image:" << cpp_strerror(r) << dendl;
+
+ m_ret_val = r;
+ m_trash_set_state = cls::rbd::TRASH_IMAGE_STATE_NORMAL;
+ m_trash_expect_state = cls::rbd::TRASH_IMAGE_STATE_REMOVING;
+ set_state();
+ return;
+ }
+
+ m_image_ctx = nullptr;
+ remove_trash_entry();
+}
+
+template <typename I>
+void RemoveRequest<I>::remove_trash_entry() {
+ ldout(m_cct, 10) << dendl;
+
+ librados::ObjectWriteOperation op;
+ cls_client::trash_remove(&op, m_image_id);
+
+ auto aio_comp = create_rados_callback<
+ RemoveRequest<I>, &RemoveRequest<I>::handle_remove_trash_entry>(this);
+ int r = m_io_ctx.aio_operate(RBD_TRASH, aio_comp, &op);
+ ceph_assert(r == 0);
+ aio_comp->release();
+}
+
+template <typename I>
+void RemoveRequest<I>::handle_remove_trash_entry(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ if (r < 0 && r != -ENOENT) {
+ lderr(m_cct) << "error removing trash entry: " << cpp_strerror(r) << dendl;
+ }
+
+ finish(0);
+}
+
+template <typename I>
+void RemoveRequest<I>::finish(int r) {
+ ldout(m_cct, 10) << "r=" << r << dendl;
+
+ m_on_finish->complete(r);
+ delete this;
+}
+
+} // namespace trash
+} // namespace librbd
+
+template class librbd::trash::RemoveRequest<librbd::ImageCtx>;
diff --git a/src/librbd/trash/RemoveRequest.h b/src/librbd/trash/RemoveRequest.h
new file mode 100644
index 000000000..86082ca49
--- /dev/null
+++ b/src/librbd/trash/RemoveRequest.h
@@ -0,0 +1,118 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_TRASH_REMOVE_REQUEST_H
+#define CEPH_LIBRBD_TRASH_REMOVE_REQUEST_H
+
+#include "include/common_fwd.h"
+#include "include/utime.h"
+#include "include/rados/librados.hpp"
+#include "cls/rbd/cls_rbd_types.h"
+#include <string>
+
+class Context;
+
+namespace librbd {
+
+struct ImageCtx;
+class ProgressContext;
+namespace asio { struct ContextWQ; }
+
+namespace trash {
+
+template <typename ImageCtxT = librbd::ImageCtx>
+class RemoveRequest {
+public:
+ static RemoveRequest* create(librados::IoCtx &io_ctx,
+ const std::string &image_id,
+ asio::ContextWQ *op_work_queue, bool force,
+ ProgressContext &prog_ctx, Context *on_finish) {
+ return new RemoveRequest(io_ctx, image_id, op_work_queue, force, prog_ctx,
+ on_finish);
+ }
+
+ static RemoveRequest* create(librados::IoCtx &io_ctx, ImageCtxT *image_ctx,
+ asio::ContextWQ *op_work_queue, bool force,
+ ProgressContext &prog_ctx, Context *on_finish) {
+ return new RemoveRequest(io_ctx, image_ctx, op_work_queue, force, prog_ctx,
+ on_finish);
+ }
+
+
+ RemoveRequest(librados::IoCtx &io_ctx, const std::string &image_id,
+ asio::ContextWQ *op_work_queue, bool force,
+ ProgressContext &prog_ctx, Context *on_finish)
+ : m_io_ctx(io_ctx), m_image_id(image_id), m_op_work_queue(op_work_queue),
+ m_force(force), m_prog_ctx(prog_ctx), m_on_finish(on_finish),
+ m_cct(reinterpret_cast<CephContext *>(io_ctx.cct())) {
+ }
+
+ RemoveRequest(librados::IoCtx &io_ctx, ImageCtxT *image_ctx,
+ asio::ContextWQ *op_work_queue, bool force,
+ ProgressContext &prog_ctx, Context *on_finish)
+ : m_io_ctx(io_ctx), m_image_ctx(image_ctx), m_image_id(m_image_ctx->id),
+ m_op_work_queue(op_work_queue), m_force(force), m_prog_ctx(prog_ctx),
+ m_on_finish(on_finish),
+ m_cct(reinterpret_cast<CephContext *>(io_ctx.cct())) {
+ }
+
+ void send();
+
+private:
+ /*
+ * @verbatim
+ *
+ * <start>
+ * |
+ * v
+ * SET_STATE (removing) * * * * * * *> CLOSE_IMAGE
+ * | |
+ * v |
+ * REMOVE_IMAGE * * *> SET_STATE (normal) |
+ * | | |
+ * v | |
+ * REMOVE_TRASH_ENTRY | |
+ * | | |
+ * v | |
+ * <finish> <-------------/<---------------/
+ *
+ * @endverbatim
+ */
+
+ librados::IoCtx &m_io_ctx;
+ ImageCtxT *m_image_ctx = nullptr;
+ std::string m_image_id;
+ asio::ContextWQ *m_op_work_queue;
+ bool m_force;
+ ProgressContext &m_prog_ctx;
+ Context *m_on_finish;
+
+ CephContext *m_cct;
+
+ cls::rbd::TrashImageState m_trash_set_state =
+ cls::rbd::TRASH_IMAGE_STATE_REMOVING;
+ cls::rbd::TrashImageState m_trash_expect_state =
+ cls::rbd::TRASH_IMAGE_STATE_NORMAL;
+ int m_ret_val = 0;
+
+ void set_state();
+ void handle_set_state(int r);
+
+ void close_image();
+ void handle_close_image(int r);
+
+ void remove_image();
+ void handle_remove_image(int r);
+
+ void remove_trash_entry();
+ void handle_remove_trash_entry(int r);
+
+ void finish(int r);
+};
+
+} // namespace trash
+} // namespace librbd
+
+extern template class librbd::trash::RemoveRequest<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_TRASH_REMOVE_REQUEST_H