From 8ca563e33b850b2f8adeae81b21ac58c6243edb8 Mon Sep 17 00:00:00 2001 From: "Adam C. Emerson" Date: Fri, 10 May 2019 16:59:36 -0400 Subject: [PATCH] osdc: Add error codes for Objecter error returns For errors generated /within/ the Objecter, not those returned from the OSD. Signed-off-by: Adam C. Emerson --- src/CMakeLists.txt | 1 + src/osdc/CMakeLists.txt | 1 + src/osdc/error_code.cc | 159 ++++++++++++++++++++++++++++++++++++++++ src/osdc/error_code.h | 55 ++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 src/osdc/error_code.cc create mode 100644 src/osdc/error_code.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5dbe424b504..47ff4a5dcd7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -343,6 +343,7 @@ set(libcommon_files osd/osd_op_util.cc osdc/Striper.cc osdc/Objecter.cc + osdc/error_code.cc librbd/Features.cc ${mds_files}) set_source_files_properties(ceph_ver.c diff --git a/src/osdc/CMakeLists.txt b/src/osdc/CMakeLists.txt index 6dd97b39362..551b105f893 100644 --- a/src/osdc/CMakeLists.txt +++ b/src/osdc/CMakeLists.txt @@ -2,6 +2,7 @@ set(osdc_files Filer.cc ObjectCacher.cc Objecter.cc + error_code.cc Striper.cc) add_library(osdc STATIC ${osdc_files}) if(WITH_EVENTTRACE) diff --git a/src/osdc/error_code.cc b/src/osdc/error_code.cc new file mode 100644 index 00000000000..5dc548385e8 --- /dev/null +++ b/src/osdc/error_code.cc @@ -0,0 +1,159 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2019 Red Hat + * Author: Adam C. Emerson + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include + +#include "common/error_code.h" +#include "error_code.h" + +namespace bs = boost::system; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" +class osdc_error_category : public ceph::converting_category { +public: + osdc_error_category(){} + const char* name() const noexcept override; + const char* message(int ev, char*, std::size_t) const noexcept override; + std::string message(int ev) const override; + bs::error_condition default_error_condition(int ev) const noexcept + override; + bool equivalent(int ev, const bs::error_condition& c) const + noexcept override; + using ceph::converting_category::equivalent; + int from_code(int ev) const noexcept override; +}; +#pragma GCC diagnostic pop +#pragma clang diagnostic pop + +const char* osdc_error_category::name() const noexcept { + return "osdc"; +} + +const char* osdc_error_category::message(int ev, char*, + std::size_t) const noexcept { + if (ev == 0) + return "No error"; + + switch (static_cast(ev)) { + case osdc_errc::pool_dne: + return "Pool does not exist"; + + case osdc_errc::pool_exists: + return "Pool already exists"; + + case osdc_errc::precondition_violated: + return "Precondition for operation not satisfied"; + + case osdc_errc::not_supported: + return "Operation not supported"; + + case osdc_errc::snapshot_exists: + return "Snapshot already exists"; + + case osdc_errc::snapshot_dne: + return "Snapshot does not exist"; + + case osdc_errc::timed_out: + return "Operation timed out"; + } + + return "Unknown error"; +} + +std::string osdc_error_category::message(int ev) const { + return message(ev, nullptr, 0); +} + +bs::error_condition +osdc_error_category::default_error_condition(int ev) const noexcept { + switch (static_cast(ev)) { + case osdc_errc::pool_dne: + return ceph::errc::does_not_exist; + case osdc_errc::pool_exists: + return ceph::errc::exists; + case osdc_errc::precondition_violated: + return bs::errc::invalid_argument; + case osdc_errc::not_supported: + return bs::errc::operation_not_supported; + case osdc_errc::snapshot_exists: + return ceph::errc::exists; + case osdc_errc::snapshot_dne: + return ceph::errc::does_not_exist; + case osdc_errc::timed_out: + return bs::errc::timed_out; + } + + return { ev, *this }; +} + +bool osdc_error_category::equivalent(int ev, + const bs::error_condition& c) const noexcept { + if (static_cast(ev) == osdc_errc::pool_dne) { + if (c == bs::errc::no_such_file_or_directory) { + return true; + } + if (c == ceph::errc::not_in_map) { + return true; + } + } + if (static_cast(ev) == osdc_errc::pool_exists) { + if (c == bs::errc::file_exists) { + return true; + } + } + if (static_cast(ev) == osdc_errc::snapshot_exists) { + if (c == bs::errc::file_exists) { + return true; + } + } + if (static_cast(ev) == osdc_errc::snapshot_dne) { + if (c == bs::errc::no_such_file_or_directory) { + return true; + } + if (c == ceph::errc::not_in_map) { + return true; + } + } + + return default_error_condition(ev) == c; +} + +int osdc_error_category::from_code(int ev) const noexcept { + switch (static_cast(ev)) { + case osdc_errc::pool_dne: + return -ENOENT; + case osdc_errc::pool_exists: + return -EEXIST; + case osdc_errc::precondition_violated: + return -EINVAL; + case osdc_errc::not_supported: + return -EOPNOTSUPP; + case osdc_errc::snapshot_exists: + return -EEXIST; + case osdc_errc::snapshot_dne: + return -ENOENT; + case osdc_errc::timed_out: + return -ETIMEDOUT; + } + return -EDOM; +} + +const bs::error_category& osdc_category() noexcept { + static const osdc_error_category c; + return c; +} diff --git a/src/osdc/error_code.h b/src/osdc/error_code.h new file mode 100644 index 00000000000..53c9e3c3a2a --- /dev/null +++ b/src/osdc/error_code.h @@ -0,0 +1,55 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2019 Red Hat + * Author: Adam C. Emerson + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#pragma once + +#include + +#include "include/rados.h" + +const boost::system::error_category& osdc_category() noexcept; + +enum class osdc_errc { + pool_dne = 1, + pool_exists, + // Come the revolution, we'll just kill your program. Maybe. + precondition_violated, + not_supported, + snapshot_exists, + snapshot_dne, + timed_out +}; + +namespace boost::system { +template<> +struct is_error_code_enum<::osdc_errc> { + static const bool value = true; +}; + +template<> +struct is_error_condition_enum<::osdc_errc> { + static const bool value = false; +}; +} + +// implicit conversion: +inline boost::system::error_code make_error_code(osdc_errc e) noexcept { + return { static_cast(e), osdc_category() }; +} + +// explicit conversion: +inline boost::system::error_condition make_error_condition(osdc_errc e) noexcept { + return { static_cast(e), osdc_category() }; +} -- 2.47.3