forked from scality/RestBlockDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrb.h
335 lines (289 loc) · 12.3 KB
/
srb.h
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2014 SCALITY SA - http://www.scality.com
*
* This file is part of ScalityRestBlock.
*
* ScalityRestBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ScalityRestBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __SRBLOCK_H__
# define __SRBLOCK_H__
#include <linux/inet.h>
#include <linux/socket.h>
#include <linux/net.h> // for in_aton()
#include <linux/in.h>
#include <linux/net.h>
#include <linux/scatterlist.h>
#include <net/sock.h>
#include <linux/genhd.h>
/* Constants */
#define kB 1024
#define MB (1024 * kB)
#define GB (1024 * MB)
/* Unix device constants */
#define DEV_NAME "srb"
#define DEV_REL_VERSION "0.6.1" // Set version of srb LKM
#define DEV_MINORS 256
#define DEV_DEFAULT_DISKSIZE (50 * MB)
#define DEV_MAX 64
#define DEV_SECTORSIZE 1 * MB
#define DEV_NB_PHYS_SEGS 512
/* Device state (reduce spinlock section and avoid multiple operation on same device) */
#define DEV_IN_USE 1
#define DEV_UNUSED 0
/* Dewpoint server related constants */
#define SRB_HTTP_HEADER_SIZE 1024
#define SRB_URL_SIZE 256
#define SRB_REUSE_LIMIT 100 /* Max number of requests sent to
* a single HTTP connection before
* restarting a new one.
*/
/* TODO: Retry CDMI request (Issue #22)
* Linux Kernel Module (LKM) parameters
*/
extern unsigned short srb_log;
extern unsigned short req_timeout;
extern unsigned short nb_req_retries;
extern unsigned short server_conn_timeout;
extern unsigned int thread_pool_size;
/* TODO: LKM logging (Issue #28 and Issue #3)
* Standard Kernel value for log level
*/
#define SRB_DEBUG 7
#define SRB_INFO 6
#define SRB_NOTICE 5
#define SRB_WARN 4
#define SRB_ERR 3
#define SRB_CRIT 2
#define SRB_ALERT 1
#define SRB_EMERG 0
/*
* ScalityRestBlock log function
*/
#define SRB_LOG_DEBUG(level, fmt, a...) \
if (level >= SRB_DEBUG) printk(KERN_DEBUG "srb: " fmt "\n", ##a)
#define SRB_LOG_INFO(level, fmt, a...) \
if (level >= SRB_INFO) printk(KERN_INFO "srb: " fmt "\n", ##a)
#define SRB_LOG_NOTICE(level, fmt, a...) \
if (level >= SRB_NOTICE) printk(KERN_NOTICE "srb: " fmt "\n", ##a)
#define SRB_LOG_WARN(level, fmt, a...) \
if (level >= SRB_WARN) printk(KERN_WARNING "srb: " fmt "\n", ##a)
#define SRB_LOG_ERR(level, fmt, a...) \
if (level >= SRB_ERR) printk(KERN_ERR "srb: " fmt "\n", ##a)
#define SRB_LOG_CRIT(level, fmt, a...) \
if (level >= SRB_CRIT) printk(KERN_CRIT "srb: " fmt "\n", ##a)
#define SRB_LOG_ALERT(level, fmt, a...) \
if (level >= SRB_ALERT) printk(KERN_ALERT "srb: " fmt "\n", ##a)
#define SRB_LOG_EMERG(level, fmt, a...) \
if (level >= SRB_EMERG) printk(KERN_EMERG "srb: " fmt "\n", ##a)
/*
* Default values for ScalityRestBlock LKM parameters
*/
#define SRB_REQ_TIMEOUT_DFLT 30
#define SRB_NB_REQ_RETRIES_DFLT 3
#define SRB_CONN_TIMEOUT_DFLT 30
#define SRB_LOG_LEVEL_DFLT SRB_INFO
#define SRB_THREAD_POOL_SIZE_DFLT 8
#define SRB_DEBUG_LEVEL 0 /* We do not want to be polluted
* by default */
#define SRB_XMIT_BUFFER_SIZE (SRB_HTTP_HEADER_SIZE + DEV_SECTORSIZE)
#define SRB_INTERNAL_DBG(dbg, fmt, a...) \
do { if ((dbg)->level) \
printk(KERN_NOTICE "%s @%s:%d: " fmt "\n" , \
(dbg)->name, __func__, __LINE__, ##a); \
} while (0)
//#define SRB_DEBUG(fmt, a...) SRB_INTERNAL_DBG(dbg, fmt, ##a)
#define SRB_DEV_DEBUG(fmt, a...) SRB_INTERNAL_DBG(&dev->debug, fmt, ##a)
/*
#define SRB_INFO(fmt, a...) \
printk(KERN_INFO "srb: " fmt "\n" , ##a)
#define SRB_ERROR(fmt, a...) \
printk(KERN_ERR "srb: " fmt "\n" , ##a)
*/
#define SRB_MIN(x, y) ((x) < (y) ? (x) : (y))
#define SRB_N_JSON_TOKENS 128
/*
* Status Ranges extracted from RFC 2616
*/
enum srb_http_statusrange
{
SRB_HTTP_STATUSRANGE_INFORMATIONAL = 1, // 1..
SRB_HTTP_STATUSRANGE_SUCCESS = 2, // 2..
SRB_HTTP_STATUSRANGE_REDIRECTION = 3, // 3..
SRB_HTTP_STATUSRANGE_CLIENTERROR = 4, // 4..
SRB_HTTP_STATUSRANGE_SERVERERROR = 5, // 5..
SRB_HTTP_STATUSRANGE_EXTENDED = 0
};
/*
* Codes extracted from RFC 2616
*/
enum srb_http_statuscode
{
SRB_HTTP_STATUS_CONTINUE = 100, // "100" ; Section 10.1.1: Continue
SRB_HTTP_STATUS_SWITCHPROTO = 101, // "101" ; Section 10.1.2: Switching Protocols
SRB_HTTP_STATUS_OK = 200, // "200" ; Section 10.2.1: OK
SRB_HTTP_STATUS_CREATED = 201, // "201" ; Section 10.2.2: Created
SRB_HTTP_STATUS_ACCEPTED = 202, // "202" ; Section 10.2.3: Accepted
SRB_HTTP_STATUS_NONAUTH_INFO = 203, // "203" ; Section 10.2.4: Non-Authoritative Information
SRB_HTTP_STATUS_NOCONTENT = 204, // "204" ; Section 10.2.5: No Content
SRB_HTTP_STATUS_RESETCONTENT = 205, // "205" ; Section 10.2.6: Reset Content
SRB_HTTP_STATUS_PARTIAL = 206, // "206" ; Section 10.2.7: Partial Content
SRB_HTTP_STATUS_MULTIPLE_CHOICES = 300, // "300" ; Section 10.3.1: Multiple Choices
SRB_HTTP_STATUS_MOVED = 301, // "301" ; Section 10.3.2: Moved Permanently
SRB_HTTP_STATUS_FOUND = 302, // "302" ; Section 10.3.3: Found
SRB_HTTP_STATUS_SEEOTHER = 303, // "303" ; Section 10.3.4: See Other
SRB_HTTP_STATUS_NOTMODIF = 304, // "304" ; Section 10.3.5: Not Modified
SRB_HTTP_STATUS_USE_PROXY = 305, // "305" ; Section 10.3.6: Use Proxy
SRB_HTTP_STATUS_TEMP_REDIR = 307, // "307" ; Section 10.3.8: Temporary Redirect
SRB_HTTP_STATUS_BADREQ = 400, // "400" ; Section 10.4.1: Bad Request
SRB_HTTP_STATUS_UNAUTH = 401, // "401" ; Section 10.4.2: Unauthorized
SRB_HTTP_STATUS_PAYMENT_REQ = 402, // "402" ; Section 10.4.3: Payment Required
SRB_HTTP_STATUS_FORBIDDEN = 403, // "403" ; Section 10.4.4: Forbidden
SRB_HTTP_STATUS_NOT_FOUND = 404, // "404" ; Section 10.4.5: Not Found
SRB_HTTP_STATUS_NOT_ALLOWED = 405, // "405" ; Section 10.4.6: Method Not Allowed
SRB_HTTP_STATUS_NOT_ACCEPTABLE = 406, // "406" ; Section 10.4.7: Not Acceptable
SRB_HTTP_STATUS_PROXYAUTH_REQ = 407, // "407" ; Section 10.4.8: Proxy Authentication Required
SRB_HTTP_STATUS_REQTIMEOUT = 408, // "408" ; Section 10.4.9: Request Time-out
SRB_HTTP_STATUS_CONFLICT = 409, // "409" ; Section 10.4.10: Conflict
SRB_HTTP_STATUS_GONE = 410, // "410" ; Section 10.4.11: Gone
SRB_HTTP_STATUS_LENGTH_REQ = 410, // "411" ; Section 10.4.12: Length Required
SRB_HTTP_STATUS_PRECOND_FAILED = 412, // "412" ; Section 10.4.13: Precondition Failed
SRB_HTTP_STATUS_ENTITY_TOOLARGE = 413, // "413" ; Section 10.4.14: Request Entity Too Large
SRB_HTTP_STATUS_URI_TOOLARGE = 414, // "414" ; Section 10.4.15: Request-URI Too Large
SRB_HTTP_STATUS_UNSUP_MEDIA = 415, // "415" ; Section 10.4.16: Unsupported Media Type
SRB_HTTP_STATUS_BADRANGE = 416, // "416" ; Section 10.4.17: Requested range not satisfiable
SRB_HTTP_STATUS_EXPECT_FAILED = 417, // "417" ; Section 10.4.18: Expectation Failed
SRB_HTTP_STATUS_INTERNAL_ERROR = 500, // "500" ; Section 10.5.1: Internal Server Error
SRB_HTTP_STATUS_NOTIMPL = 501, // "501" ; Section 10.5.2: Not Implemented
SRB_HTTP_STATUS_BAD_GW = 502, // "502" ; Section 10.5.3: Bad Gateway
SRB_HTTP_STATUS_SERVICE_UNAVAIL = 503, // "503" ; Section 10.5.4: Service Unavailable
SRB_HTTP_STATUS_GW_TIMEOUT = 504, // "504" ; Section 10.5.5: Gateway Time-out
SRB_HTTP_STATUS_VERSION_NOTSUPP = 505, // "505" ; Section 10.5.6: HTTP Version not supported
SRB_HTTP_STATUS_EXTENSION = 0 // ; extension-code
};
/* srb_cdmi.c */
struct srb_cdmi_desc_s {
/* For /sys/block/srb?/srb_url */
char url[SRB_URL_SIZE + 1];
uint8_t state;
char ip_addr[16];
uint16_t port;
char filename[SRB_URL_SIZE + 1];
char xmit_buff[SRB_XMIT_BUFFER_SIZE];
uint64_t nb_requests; /* Number of HTTP
* requests already sent
* through this socket */
struct scatterlist sgl[DEV_NB_PHYS_SEGS];
int sgl_size;
struct socket *socket;
struct sockaddr_in sockaddr;
struct timeval timeout;
};
/* srb device definition */
typedef struct srb_debug_s {
const char *name;
int level;
} srb_debug_t;
typedef struct srb_device_s {
/* Device subsystem related data */
int id; /* device ID */
int major; /* blkdev assigned major */
//char name[32]; /* blkdev name, e.g. srba */
//XXX: use const from ./linux/genhd.h
char name[DISK_NAME_LEN]; /* blkdev name, e.g. srba */
struct gendisk *disk;
uint64_t disk_size; /* Size in bytes */
int users; /* Number of users who
* opened dev */
int state; /* for create extend attach detach destroy purpose */
struct request_queue *q;
spinlock_t rq_lock; /* request queue lock */
/* TODO: Use a dynamic thread pool (Issue #33)
*/
//struct task_struct *thread[SRB_THREAD_POOL_SIZE_DFLT];
struct task_struct **thread; /* allow dynamic allocation during device creation */
int nb_threads;
/* Dewpoint specific data */
/* TODO: Use a dynamic CDMI pool due to the 1 <-> 1 relation with thread (Issue #33)
*/
//struct srb_cdmi_desc_s thread_cdmi_desc[SRB_THREAD_POOL_SIZE_DFLT];
struct srb_cdmi_desc_s **thread_cdmi_desc; /* allow dynamic allocation during device creation*/
/*
** List of requests received by the drivers, but still to be
** processed. This due to network latency.
*/
spinlock_t waiting_lock; /* wait_queue lock */
wait_queue_head_t waiting_wq;
struct list_head waiting_queue; /* Requests to be sent */
/* Debug traces */
srb_debug_t debug;
} srb_device_t;
typedef struct srb_server_s {
struct srb_server_s *next;
struct srb_cdmi_desc_s cdmi_desc;
} srb_server_t;
/* srb.c */
int srb_device_create(const char *filename, unsigned long long size);
int srb_device_extend(const char *filename, unsigned long long size);
int srb_device_destroy(const char *filename);
int srb_device_attach(const char *filename, const char *devname);
int srb_device_detach(const char *devname);
int srb_server_add(const char *url);
int srb_server_remove(const char *url);
ssize_t srb_servers_dump(char *buf, ssize_t max_size);
int srb_volumes_dump(char *buf, size_t max_size);
/* srb_sysfs.c*/
int srb_sysfs_init(void);
void srb_sysfs_device_init(srb_device_t *dev);
void srb_sysfs_cleanup(void);
/* srb_cdmi.c */
int srb_cdmi_init(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
const char *url);
int srb_cdmi_connect(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc);
int srb_cdmi_disconnect(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc);
int srb_cdmi_getsize(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
uint64_t *size);
int srb_cdmi_getrange(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
uint64_t offset, int size);
int srb_cdmi_putrange(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
uint64_t offset, int size);
int srb_cdmi_flush(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
unsigned long flush_size);
int srb_cdmi_truncate(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
unsigned long trunc_size);
int srb_cdmi_create(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
unsigned long long trunc_size);
int srb_cdmi_extend(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
unsigned long long trunc_size);
int srb_cdmi_delete(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc);
typedef int (*srb_cdmi_list_cb)(void * data, const char *);
int srb_cdmi_list(srb_debug_t *dbg, struct srb_cdmi_desc_s *desc,
srb_cdmi_list_cb cb, void *cb_data);
/* srb_http.c */
int srb_http_check_response_complete(char *buff, int len);
int srb_http_mklist(char *buff, int len, char *host, char *page);
int srb_http_mkhead(char *buff, int len, char *host, char *page);
int srb_http_mkrange(char *cmd, char *buff, int len, char *host, char *page,
uint64_t start, uint64_t end);
int srb_http_mkcreate(char *buff, int len, char *host, char *page);
int srb_http_mktruncate(char *buff, int len, char *host, char *page,
unsigned long long size);
int srb_http_mkdelete(char *buff, int len, char *host, char *page);
int srb_http_header_get_uint64(char *buff, int len, char *key,
uint64_t *value);
int srb_http_skipheader(char **buff, int *len);
int srb_http_mkmetadata(char *buff, int len, char *host, char *page);
int srb_http_get_status(char *buf, int len, enum srb_http_statuscode *code);
enum srb_http_statusrange srb_http_get_status_range(enum srb_http_statuscode status);
#endif