]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/io/IoOperations.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / librbd / io / IoOperations.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 <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string.hpp>
6
7 #include "librbd/io/Types.h"
8 #include "librbd/io/IoOperations.h"
9
10 #include <map>
11 #include <vector>
12
13 namespace librbd {
14 namespace io {
15
16 #define RBD_IO_OPERATION_NAME_READ "read"
17 #define RBD_IO_OPERATION_NAME_WRITE "write"
18 #define RBD_IO_OPERATION_NAME_DISCARD "discard"
19 #define RBD_IO_OPERATION_NAME_WRITE_SAME "write_same"
20 #define RBD_IO_OPERATION_NAME_COMPARE_AND_WRITE "compare_and_write"
21
22 static const std::map<std::string, uint64_t> RBD_IO_OPERATION_MAP = {
23 {RBD_IO_OPERATION_NAME_READ, RBD_IO_OPERATION_READ},
24 {RBD_IO_OPERATION_NAME_WRITE, RBD_IO_OPERATION_WRITE},
25 {RBD_IO_OPERATION_NAME_DISCARD, RBD_IO_OPERATION_DISCARD},
26 {RBD_IO_OPERATION_NAME_WRITE_SAME, RBD_IO_OPERATION_WRITE_SAME},
27 {RBD_IO_OPERATION_NAME_COMPARE_AND_WRITE, RBD_IO_OPERATION_COMPARE_AND_WRITE},
28 };
29 static_assert((RBD_IO_OPERATION_COMPARE_AND_WRITE << 1) > RBD_IO_OPERATIONS_ALL,
30 "new RBD io operation added");
31
32 std::string rbd_io_operations_to_string(uint64_t operations,
33 std::ostream *err)
34 {
35 std::string r;
36 for (auto& i : RBD_IO_OPERATION_MAP) {
37 if (operations & i.second) {
38 if (!r.empty()) {
39 r += ",";
40 }
41 r += i.first;
42 operations &= ~i.second;
43 }
44 }
45 if (err && operations) {
46 *err << "ignoring unknown io operation mask 0x"
47 << std::hex << operations << std::dec;
48 }
49 return r;
50 }
51
52 uint64_t rbd_io_operations_from_string(const std::string& orig_value,
53 std::ostream *err)
54 {
55 uint64_t operations = 0;
56 std::string value = orig_value;
57 boost::trim(value);
58
59 // empty string means default operations
60 if (!value.size()) {
61 return RBD_IO_OPERATIONS_DEFAULT;
62 }
63
64 try {
65 // numeric?
66 operations = boost::lexical_cast<uint64_t>(value);
67
68 // drop unrecognized bits
69 uint64_t unsupported_operations = (operations & ~RBD_IO_OPERATIONS_ALL);
70 if (unsupported_operations != 0ull) {
71 operations &= RBD_IO_OPERATIONS_ALL;
72 if (err) {
73 *err << "ignoring unknown operation mask 0x"
74 << std::hex << unsupported_operations << std::dec;
75 }
76 }
77 } catch (boost::bad_lexical_cast&) {
78 // operation name list?
79 bool errors = false;
80 std::vector<std::string> operation_names;
81 boost::split(operation_names, value, boost::is_any_of(","));
82 for (auto operation_name: operation_names) {
83 boost::trim(operation_name);
84 auto operation_it = RBD_IO_OPERATION_MAP.find(operation_name);
85 if (operation_it != RBD_IO_OPERATION_MAP.end()) {
86 operations += operation_it->second;
87 } else if (err) {
88 if (errors) {
89 *err << ", ";
90 } else {
91 errors = true;
92 }
93 *err << "ignoring unknown operation " << operation_name;
94 }
95 }
96 }
97 return operations;
98 }
99
100 } // namespace io
101 } // namespace librbd