1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * RDMA Network Block Driver
5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
12 #include <linux/types.h>
13 #include <linux/blkdev.h>
14 #include <linux/limits.h>
15 #include <linux/inet.h>
17 #include <linux/in6.h>
20 #define RNBD_PROTO_VER_MAJOR 2
21 #define RNBD_PROTO_VER_MINOR 0
23 /* The default port number the RTRS server is listening on. */
24 #define RTRS_PORT 1234
27 * enum rnbd_msg_types - RNBD message types
28 * @RNBD_MSG_SESS_INFO: initial session info from client to server
29 * @RNBD_MSG_SESS_INFO_RSP: initial session info from server to client
30 * @RNBD_MSG_OPEN: open (map) device request
31 * @RNBD_MSG_OPEN_RSP: response to an @RNBD_MSG_OPEN
32 * @RNBD_MSG_IO: block IO request operation
33 * @RNBD_MSG_CLOSE: close (unmap) device request
37 RNBD_MSG_SESS_INFO_RSP
,
45 * struct rnbd_msg_hdr - header of RNBD messages
46 * @type: Message type, valid values see: enum rnbd_msg_types
54 * We allow to map RO many times and RW only once. We allow to map yet another
55 * time RW, if MIGRATION is provided (second RW export can be required for
56 * example for VM migration)
58 enum rnbd_access_mode
{
61 RNBD_ACCESS_MIGRATION
,
65 * struct rnbd_msg_sess_info - initial session info from client to server
66 * @hdr: message header
67 * @ver: RNBD protocol version
69 struct rnbd_msg_sess_info
{
70 struct rnbd_msg_hdr hdr
;
76 * struct rnbd_msg_sess_info_rsp - initial session info from server to client
77 * @hdr: message header
78 * @ver: RNBD protocol version
80 struct rnbd_msg_sess_info_rsp
{
81 struct rnbd_msg_hdr hdr
;
87 * struct rnbd_msg_open - request to open a remote device.
88 * @hdr: message header
89 * @access_mode: the mode to open remote device, valid values see:
90 * enum rnbd_access_mode
91 * @device_name: device path on remote side
93 struct rnbd_msg_open
{
94 struct rnbd_msg_hdr hdr
;
97 s8 dev_name
[NAME_MAX
];
102 * struct rnbd_msg_close - request to close a remote device.
103 * @hdr: message header
104 * @device_id: device_id on server side to identify the device
106 struct rnbd_msg_close
{
107 struct rnbd_msg_hdr hdr
;
111 enum rnbd_cache_policy
{
113 RNBD_WRITEBACK
= 1 << 1,
117 * struct rnbd_msg_open_rsp - response message to RNBD_MSG_OPEN
118 * @hdr: message header
119 * @device_id: device_id on server side to identify the device
120 * @nsectors: number of sectors in the usual 512b unit
121 * @max_hw_sectors: max hardware sectors in the usual 512b unit
122 * @max_write_same_sectors: max sectors for WRITE SAME in the 512b unit
123 * @max_discard_sectors: max. sectors that can be discarded at once in 512b
125 * @discard_granularity: size of the internal discard allocation unit in bytes
126 * @discard_alignment: offset from internal allocation assignment in bytes
127 * @physical_block_size: physical block size device supports in bytes
128 * @logical_block_size: logical block size device supports in bytes
129 * @max_segments: max segments hardware support in one transfer
130 * @secure_discard: supports secure discard
131 * @rotation: is a rotational disc?
132 * @cache_policy: support write-back caching or FUA?
134 struct rnbd_msg_open_rsp
{
135 struct rnbd_msg_hdr hdr
;
138 __le32 max_hw_sectors
;
139 __le32 max_write_same_sectors
;
140 __le32 max_discard_sectors
;
141 __le32 discard_granularity
;
142 __le32 discard_alignment
;
143 __le16 physical_block_size
;
144 __le16 logical_block_size
;
146 __le16 secure_discard
;
153 * struct rnbd_msg_io - message for I/O read/write
154 * @hdr: message header
155 * @device_id: device_id on server side to find the right device
156 * @sector: bi_sector attribute from struct bio
157 * @rw: valid values are defined in enum rnbd_io_flags
158 * @bi_size: number of bytes for I/O read/write
162 struct rnbd_msg_hdr hdr
;
170 #define RNBD_OP_BITS 8
171 #define RNBD_OP_MASK ((1 << RNBD_OP_BITS) - 1)
174 * enum rnbd_io_flags - RNBD request types from rq_flag_bits
175 * @RNBD_OP_READ: read sectors from the device
176 * @RNBD_OP_WRITE: write sectors to the device
177 * @RNBD_OP_FLUSH: flush the volatile write cache
178 * @RNBD_OP_DISCARD: discard sectors
179 * @RNBD_OP_SECURE_ERASE: securely erase sectors
180 * @RNBD_OP_WRITE_SAME: write the same sectors many times
182 * @RNBD_F_SYNC: request is sync (sync write or read)
183 * @RNBD_F_FUA: forced unit access
193 RNBD_OP_SECURE_ERASE
= 4,
194 RNBD_OP_WRITE_SAME
= 5,
200 RNBD_F_SYNC
= 1<<(RNBD_OP_BITS
+ 0),
201 RNBD_F_FUA
= 1<<(RNBD_OP_BITS
+ 1),
203 RNBD_F_ALL
= (RNBD_F_SYNC
| RNBD_F_FUA
)
207 static inline u32
rnbd_op(u32 flags
)
209 return flags
& RNBD_OP_MASK
;
212 static inline u32
rnbd_flags(u32 flags
)
214 return flags
& ~RNBD_OP_MASK
;
217 static inline bool rnbd_flags_supported(u32 flags
)
222 flags
= rnbd_flags(flags
);
224 if (op
>= RNBD_OP_LAST
)
226 if (flags
& ~RNBD_F_ALL
)
232 static inline u32
rnbd_to_bio_flags(u32 rnbd_opf
)
236 switch (rnbd_op(rnbd_opf
)) {
238 bio_opf
= REQ_OP_READ
;
241 bio_opf
= REQ_OP_WRITE
;
244 bio_opf
= REQ_OP_FLUSH
| REQ_PREFLUSH
;
246 case RNBD_OP_DISCARD
:
247 bio_opf
= REQ_OP_DISCARD
;
249 case RNBD_OP_SECURE_ERASE
:
250 bio_opf
= REQ_OP_SECURE_ERASE
;
252 case RNBD_OP_WRITE_SAME
:
253 bio_opf
= REQ_OP_WRITE_SAME
;
256 WARN(1, "Unknown RNBD type: %d (flags %d)\n",
257 rnbd_op(rnbd_opf
), rnbd_opf
);
261 if (rnbd_opf
& RNBD_F_SYNC
)
264 if (rnbd_opf
& RNBD_F_FUA
)
270 static inline u32
rq_to_rnbd_flags(struct request
*rq
)
274 switch (req_op(rq
)) {
276 rnbd_opf
= RNBD_OP_READ
;
279 rnbd_opf
= RNBD_OP_WRITE
;
282 rnbd_opf
= RNBD_OP_DISCARD
;
284 case REQ_OP_SECURE_ERASE
:
285 rnbd_opf
= RNBD_OP_SECURE_ERASE
;
287 case REQ_OP_WRITE_SAME
:
288 rnbd_opf
= RNBD_OP_WRITE_SAME
;
291 rnbd_opf
= RNBD_OP_FLUSH
;
294 WARN(1, "Unknown request type %d (flags %llu)\n",
295 req_op(rq
), (unsigned long long)rq
->cmd_flags
);
299 if (op_is_sync(rq
->cmd_flags
))
300 rnbd_opf
|= RNBD_F_SYNC
;
302 if (op_is_flush(rq
->cmd_flags
))
303 rnbd_opf
|= RNBD_F_FUA
;
308 const char *rnbd_access_mode_str(enum rnbd_access_mode mode
);
310 #endif /* RNBD_PROTO_H */