1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef __DST_H
17#define __DST_H
18
19#include <linux/types.h>
20#include <linux/connector.h>
21
22#define DST_NAMELEN 32
23#define DST_NAME "dst"
24
25enum {
26
27 DST_DEL_NODE = 0,
28
29 DST_ADD_REMOTE,
30
31 DST_ADD_EXPORT,
32
33 DST_CRYPTO,
34
35 DST_SECURITY,
36
37 DST_START,
38 DST_CMD_MAX
39};
40
41struct dst_ctl
42{
43
44 char name[DST_NAMELEN];
45
46 __u32 flags;
47
48 __u32 cmd;
49
50 __u32 max_pages;
51
52 __u32 trans_scan_timeout;
53
54 __u32 trans_max_retries;
55
56 __u64 size;
57};
58
59
60struct dst_ctl_ack
61{
62 struct cn_msg msg;
63 int error;
64 int unused[3];
65};
66
67
68
69
70
71#define SADDR_MAX_DATA 128
72
73struct saddr {
74
75 unsigned short sa_family;
76
77 char sa_data[SADDR_MAX_DATA];
78
79 unsigned short sa_data_len;
80};
81
82
83struct dst_network_ctl
84{
85
86 unsigned int type;
87
88 unsigned int proto;
89
90 struct saddr addr;
91};
92
93struct dst_crypto_ctl
94{
95
96 char cipher_algo[DST_NAMELEN];
97 char hash_algo[DST_NAMELEN];
98
99
100 unsigned int cipher_keysize, hash_keysize;
101
102 unsigned int crypto_attached_size;
103
104 int thread_num;
105};
106
107
108#define DST_PERM_READ (1<<0)
109#define DST_PERM_WRITE (1<<1)
110
111
112
113
114
115
116
117struct dst_secure_user
118{
119 unsigned int permissions;
120 struct saddr addr;
121};
122
123
124
125
126
127struct dst_export_ctl
128{
129 char device[DST_NAMELEN];
130 struct dst_network_ctl ctl;
131};
132
133enum {
134 DST_CFG = 1,
135 DST_IO,
136 DST_IO_RESPONSE,
137 DST_PING,
138 DST_NCMD_MAX,
139};
140
141struct dst_cmd
142{
143
144 __u32 cmd;
145
146
147
148
149 __u32 size;
150
151 __u32 csize;
152
153 __u32 reserved;
154
155 __u64 rw;
156
157 __u64 flags;
158
159 __u64 id;
160
161 __u64 sector;
162
163 __u8 hash[0];
164};
165
166
167
168
169
170
171static inline void dst_convert_cmd(struct dst_cmd *c)
172{
173 c->cmd = __cpu_to_be32(c->cmd);
174 c->csize = __cpu_to_be32(c->csize);
175 c->size = __cpu_to_be32(c->size);
176 c->sector = __cpu_to_be64(c->sector);
177 c->id = __cpu_to_be64(c->id);
178 c->flags = __cpu_to_be64(c->flags);
179 c->rw = __cpu_to_be64(c->rw);
180}
181
182
183typedef __u64 dst_gen_t;
184
185#ifdef __KERNEL__
186
187#include <linux/blkdev.h>
188#include <linux/bio.h>
189#include <linux/device.h>
190#include <linux/mempool.h>
191#include <linux/net.h>
192#include <linux/poll.h>
193#include <linux/rbtree.h>
194
195#ifdef CONFIG_DST_DEBUG
196#define dprintk(f, a...) printk(KERN_NOTICE f, ##a)
197#else
198static inline void __attribute__ ((format (printf, 1, 2)))
199 dprintk(const char *fmt, ...) {}
200#endif
201
202struct dst_node;
203
204struct dst_trans
205{
206
207 struct dst_node *n;
208
209
210 struct rb_node trans_entry;
211
212
213 atomic_t refcnt;
214
215
216 short enc;
217
218 short retries;
219
220 int error;
221
222
223 long send_time;
224
225
226
227 dst_gen_t gen;
228
229
230 struct bio *bio;
231
232
233 struct dst_cmd cmd;
234};
235
236struct dst_crypto_engine
237{
238
239 struct crypto_hash *hash;
240 struct crypto_ablkcipher *cipher;
241
242
243 int page_num;
244 struct page **pages;
245
246
247 int enc;
248
249 struct scatterlist *src, *dst;
250
251
252 long timeout;
253
254 u64 iv;
255
256
257 void *private;
258
259
260 int size;
261 void *data;
262};
263
264struct dst_state
265{
266
267 struct mutex state_lock;
268
269
270 wait_queue_t wait;
271 wait_queue_head_t *whead;
272
273 wait_queue_head_t thread_wait;
274
275
276 struct dst_node *node;
277
278
279 struct dst_network_ctl ctl;
280
281
282 u32 permissions;
283
284
285 void (* cleanup)(struct dst_state *st);
286
287
288 struct list_head request_list;
289 spinlock_t request_lock;
290
291
292 atomic_t refcnt;
293
294
295 int need_exit;
296
297
298
299
300
301
302 struct socket *socket, *read_socket;
303
304
305 void *data;
306 unsigned int size;
307
308
309 struct dst_cmd cmd;
310};
311
312struct dst_info
313{
314
315 u64 size;
316
317
318 char local[DST_NAMELEN];
319
320
321 struct dst_network_ctl net;
322
323
324 struct device device;
325};
326
327struct dst_node
328{
329 struct list_head node_entry;
330
331
332 char name[DST_NAMELEN];
333
334 char cache_name[DST_NAMELEN];
335
336
337
338 struct block_device *bdev;
339
340 struct dst_state *state;
341
342
343 struct request_queue *queue;
344 struct gendisk *disk;
345
346
347 int thread_num;
348
349 int max_pages;
350
351
352 loff_t size;
353
354
355 struct dst_info *info;
356
357
358
359
360
361 struct list_head security_list;
362 struct mutex security_lock;
363
364
365
366
367
368
369 atomic_t refcnt;
370
371
372 int (*start)(struct dst_node *);
373
374
375 struct dst_crypto_ctl crypto;
376 u8 *hash_key;
377 u8 *cipher_key;
378
379
380 struct thread_pool *pool;
381
382
383 atomic_long_t gen;
384
385
386
387
388
389 long trans_scan_timeout;
390 int trans_max_retries;
391
392
393 struct rb_root trans_root;
394 struct mutex trans_lock;
395
396
397
398
399
400
401 struct kmem_cache *trans_cache;
402 mempool_t *trans_pool;
403
404
405 struct delayed_work trans_work;
406
407 wait_queue_head_t wait;
408};
409
410
411struct dst_secure
412{
413 struct list_head sec_entry;
414 struct dst_secure_user sec;
415};
416
417int dst_process_bio(struct dst_node *n, struct bio *bio);
418
419int dst_node_init_connected(struct dst_node *n, struct dst_network_ctl *r);
420int dst_node_init_listened(struct dst_node *n, struct dst_export_ctl *le);
421
422static inline struct dst_state *dst_state_get(struct dst_state *st)
423{
424 BUG_ON(atomic_read(&st->refcnt) == 0);
425 atomic_inc(&st->refcnt);
426 return st;
427}
428
429void dst_state_put(struct dst_state *st);
430
431struct dst_state *dst_state_alloc(struct dst_node *n);
432int dst_state_socket_create(struct dst_state *st);
433void dst_state_socket_release(struct dst_state *st);
434
435void dst_state_exit_connected(struct dst_state *st);
436
437int dst_state_schedule_receiver(struct dst_state *st);
438
439void dst_dump_addr(struct socket *sk, struct sockaddr *sa, char *str);
440
441static inline void dst_state_lock(struct dst_state *st)
442{
443 mutex_lock(&st->state_lock);
444}
445
446static inline void dst_state_unlock(struct dst_state *st)
447{
448 mutex_unlock(&st->state_lock);
449}
450
451void dst_poll_exit(struct dst_state *st);
452int dst_poll_init(struct dst_state *st);
453
454static inline unsigned int dst_state_poll(struct dst_state *st)
455{
456 unsigned int revents = POLLHUP | POLLERR;
457
458 dst_state_lock(st);
459 if (st->socket)
460 revents = st->socket->ops->poll(NULL, st->socket, NULL);
461 dst_state_unlock(st);
462
463 return revents;
464}
465
466static inline int dst_thread_setup(void *private, void *data)
467{
468 return 0;
469}
470
471void dst_node_put(struct dst_node *n);
472
473static inline struct dst_node *dst_node_get(struct dst_node *n)
474{
475 atomic_inc(&n->refcnt);
476 return n;
477}
478
479int dst_data_recv(struct dst_state *st, void *data, unsigned int size);
480int dst_recv_cdata(struct dst_state *st, void *cdata);
481int dst_data_send_header(struct socket *sock,
482 void *data, unsigned int size, int more);
483
484int dst_send_bio(struct dst_state *st, struct dst_cmd *cmd, struct bio *bio);
485
486int dst_process_io(struct dst_state *st);
487int dst_export_crypto(struct dst_node *n, struct bio *bio);
488int dst_export_send_bio(struct bio *bio);
489int dst_start_export(struct dst_node *n);
490
491int __init dst_export_init(void);
492void dst_export_exit(void);
493
494
495struct dst_export_priv
496{
497 struct list_head request_entry;
498 struct dst_state *state;
499 struct bio *bio;
500 struct dst_cmd cmd;
501};
502
503static inline void dst_trans_get(struct dst_trans *t)
504{
505 atomic_inc(&t->refcnt);
506}
507
508struct dst_trans *dst_trans_search(struct dst_node *node, dst_gen_t gen);
509int dst_trans_remove(struct dst_trans *t);
510int dst_trans_remove_nolock(struct dst_trans *t);
511void dst_trans_put(struct dst_trans *t);
512
513
514
515
516static inline void dst_bio_to_cmd(struct bio *bio, struct dst_cmd *cmd,
517 u32 command, u64 id)
518{
519 cmd->cmd = command;
520 cmd->flags = (bio->bi_flags << BIO_POOL_BITS) >> BIO_POOL_BITS;
521 cmd->rw = bio->bi_rw;
522 cmd->size = bio->bi_size;
523 cmd->csize = 0;
524 cmd->id = id;
525 cmd->sector = bio->bi_sector;
526};
527
528int dst_trans_send(struct dst_trans *t);
529int dst_trans_crypto(struct dst_trans *t);
530
531int dst_node_crypto_init(struct dst_node *n, struct dst_crypto_ctl *ctl);
532void dst_node_crypto_exit(struct dst_node *n);
533
534static inline int dst_need_crypto(struct dst_node *n)
535{
536 struct dst_crypto_ctl *c = &n->crypto;
537
538
539
540
541 return (c->hash_algo[0] | c->cipher_algo[0]);
542}
543
544int dst_node_trans_init(struct dst_node *n, unsigned int size);
545void dst_node_trans_exit(struct dst_node *n);
546
547
548
549
550
551
552
553struct thread_pool
554{
555 int thread_num;
556 struct mutex thread_lock;
557 struct list_head ready_list, active_list;
558
559 wait_queue_head_t wait;
560};
561
562void thread_pool_del_worker(struct thread_pool *p);
563void thread_pool_del_worker_id(struct thread_pool *p, unsigned int id);
564int thread_pool_add_worker(struct thread_pool *p,
565 char *name,
566 unsigned int id,
567 void *(* init)(void *data),
568 void (* cleanup)(void *data),
569 void *data);
570
571void thread_pool_destroy(struct thread_pool *p);
572struct thread_pool *thread_pool_create(int num, char *name,
573 void *(* init)(void *data),
574 void (* cleanup)(void *data),
575 void *data);
576
577int thread_pool_schedule(struct thread_pool *p,
578 int (* setup)(void *stored_private, void *setup_data),
579 int (* action)(void *stored_private, void *setup_data),
580 void *setup_data, long timeout);
581int thread_pool_schedule_private(struct thread_pool *p,
582 int (* setup)(void *private, void *data),
583 int (* action)(void *private, void *data),
584 void *data, long timeout, void *id);
585
586#endif
587#endif
588