Driver Core: devtmpfs - kernel-maintained tmpfs-based /dev
[linux/fpc-iii.git] / drivers / staging / dst / state.c
blobd057e52f3b64921262d3a14610f060953126ee2c
1 /*
2 * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/buffer_head.h>
17 #include <linux/blkdev.h>
18 #include <linux/bio.h>
19 #include <linux/connector.h>
20 #include <linux/dst.h>
21 #include <linux/device.h>
22 #include <linux/in.h>
23 #include <linux/in6.h>
24 #include <linux/socket.h>
25 #include <linux/slab.h>
27 #include <net/sock.h>
30 * Polling machinery.
33 struct dst_poll_helper
35 poll_table pt;
36 struct dst_state *st;
39 static int dst_queue_wake(wait_queue_t *wait, unsigned mode, int sync, void *key)
41 struct dst_state *st = container_of(wait, struct dst_state, wait);
43 wake_up(&st->thread_wait);
44 return 1;
47 static void dst_queue_func(struct file *file, wait_queue_head_t *whead,
48 poll_table *pt)
50 struct dst_state *st = container_of(pt, struct dst_poll_helper, pt)->st;
52 st->whead = whead;
53 init_waitqueue_func_entry(&st->wait, dst_queue_wake);
54 add_wait_queue(whead, &st->wait);
57 void dst_poll_exit(struct dst_state *st)
59 if (st->whead) {
60 remove_wait_queue(st->whead, &st->wait);
61 st->whead = NULL;
65 int dst_poll_init(struct dst_state *st)
67 struct dst_poll_helper ph;
69 ph.st = st;
70 init_poll_funcptr(&ph.pt, &dst_queue_func);
72 st->socket->ops->poll(NULL, st->socket, &ph.pt);
73 return 0;
77 * Header receiving function - may block.
79 static int dst_data_recv_header(struct socket *sock,
80 void *data, unsigned int size, int block)
82 struct msghdr msg;
83 struct kvec iov;
84 int err;
86 iov.iov_base = data;
87 iov.iov_len = size;
89 msg.msg_iov = (struct iovec *)&iov;
90 msg.msg_iovlen = 1;
91 msg.msg_name = NULL;
92 msg.msg_namelen = 0;
93 msg.msg_control = NULL;
94 msg.msg_controllen = 0;
95 msg.msg_flags = (block)?MSG_WAITALL:MSG_DONTWAIT;
97 err = kernel_recvmsg(sock, &msg, &iov, 1, iov.iov_len,
98 msg.msg_flags);
99 if (err != size)
100 return -1;
102 return 0;
106 * Header sending function - may block.
108 int dst_data_send_header(struct socket *sock,
109 void *data, unsigned int size, int more)
111 struct msghdr msg;
112 struct kvec iov;
113 int err;
115 iov.iov_base = data;
116 iov.iov_len = size;
118 msg.msg_iov = (struct iovec *)&iov;
119 msg.msg_iovlen = 1;
120 msg.msg_name = NULL;
121 msg.msg_namelen = 0;
122 msg.msg_control = NULL;
123 msg.msg_controllen = 0;
124 msg.msg_flags = MSG_WAITALL | (more)?MSG_MORE:0;
126 err = kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
127 if (err != size) {
128 dprintk("%s: size: %u, more: %d, err: %d.\n",
129 __func__, size, more, err);
130 return -1;
133 return 0;
137 * Block autoconfiguration: request size of the storage and permissions.
139 static int dst_request_remote_config(struct dst_state *st)
141 struct dst_node *n = st->node;
142 int err = -EINVAL;
143 struct dst_cmd *cmd = st->data;
145 memset(cmd, 0, sizeof(struct dst_cmd));
146 cmd->cmd = DST_CFG;
148 dst_convert_cmd(cmd);
150 err = dst_data_send_header(st->socket, cmd, sizeof(struct dst_cmd), 0);
151 if (err)
152 goto out;
154 err = dst_data_recv_header(st->socket, cmd, sizeof(struct dst_cmd), 1);
155 if (err)
156 goto out;
158 dst_convert_cmd(cmd);
160 if (cmd->cmd != DST_CFG) {
161 err = -EINVAL;
162 dprintk("%s: checking result: cmd: %d, size reported: %llu.\n",
163 __func__, cmd->cmd, cmd->sector);
164 goto out;
167 if (n->size != 0)
168 n->size = min_t(loff_t, n->size, cmd->sector);
169 else
170 n->size = cmd->sector;
172 n->info->size = n->size;
173 st->permissions = cmd->rw;
175 out:
176 dprintk("%s: n: %p, err: %d, size: %llu, permission: %x.\n",
177 __func__, n, err, n->size, st->permissions);
178 return err;
182 * Socket machinery.
185 #define DST_DEFAULT_TIMEO 20000
187 int dst_state_socket_create(struct dst_state *st)
189 int err;
190 struct socket *sock;
191 struct dst_network_ctl *ctl = &st->ctl;
193 err = sock_create(ctl->addr.sa_family, ctl->type, ctl->proto, &sock);
194 if (err < 0)
195 return err;
197 sock->sk->sk_sndtimeo = sock->sk->sk_rcvtimeo =
198 msecs_to_jiffies(DST_DEFAULT_TIMEO);
199 sock->sk->sk_allocation = GFP_NOIO;
201 st->socket = st->read_socket = sock;
202 return 0;
205 void dst_state_socket_release(struct dst_state *st)
207 dprintk("%s: st: %p, socket: %p, n: %p.\n",
208 __func__, st, st->socket, st->node);
209 if (st->socket) {
210 sock_release(st->socket);
211 st->socket = NULL;
212 st->read_socket = NULL;
216 void dst_dump_addr(struct socket *sk, struct sockaddr *sa, char *str)
218 if (sk->ops->family == AF_INET) {
219 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
220 printk(KERN_INFO "%s %u.%u.%u.%u:%d.\n",
221 str, NIPQUAD(sin->sin_addr.s_addr), ntohs(sin->sin_port));
222 } else if (sk->ops->family == AF_INET6) {
223 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
224 printk(KERN_INFO "%s %pi6:%d",
225 str, &sin->sin6_addr, ntohs(sin->sin6_port));
229 void dst_state_exit_connected(struct dst_state *st)
231 if (st->socket) {
232 dst_poll_exit(st);
233 st->socket->ops->shutdown(st->socket, 2);
235 dst_dump_addr(st->socket, (struct sockaddr *)&st->ctl.addr,
236 "Disconnected peer");
237 dst_state_socket_release(st);
241 static int dst_state_init_connected(struct dst_state *st)
243 int err;
244 struct dst_network_ctl *ctl = &st->ctl;
246 err = dst_state_socket_create(st);
247 if (err)
248 goto err_out_exit;
250 err = kernel_connect(st->socket, (struct sockaddr *)&st->ctl.addr,
251 st->ctl.addr.sa_data_len, 0);
252 if (err)
253 goto err_out_release;
255 err = dst_poll_init(st);
256 if (err)
257 goto err_out_release;
259 dst_dump_addr(st->socket, (struct sockaddr *)&ctl->addr,
260 "Connected to peer");
262 return 0;
264 err_out_release:
265 dst_state_socket_release(st);
266 err_out_exit:
267 return err;
271 * State reset is used to reconnect to the remote peer.
272 * May fail, but who cares, we will try again later.
274 static void inline dst_state_reset_nolock(struct dst_state *st)
276 dst_state_exit_connected(st);
277 dst_state_init_connected(st);
280 static void inline dst_state_reset(struct dst_state *st)
282 dst_state_lock(st);
283 dst_state_reset_nolock(st);
284 dst_state_unlock(st);
288 * Basic network sending/receiving functions.
289 * Blocked mode is used.
291 static int dst_data_recv_raw(struct dst_state *st, void *buf, u64 size)
293 struct msghdr msg;
294 struct kvec iov;
295 int err;
297 BUG_ON(!size);
299 iov.iov_base = buf;
300 iov.iov_len = size;
302 msg.msg_iov = (struct iovec *)&iov;
303 msg.msg_iovlen = 1;
304 msg.msg_name = NULL;
305 msg.msg_namelen = 0;
306 msg.msg_control = NULL;
307 msg.msg_controllen = 0;
308 msg.msg_flags = MSG_DONTWAIT;
310 err = kernel_recvmsg(st->socket, &msg, &iov, 1, iov.iov_len,
311 msg.msg_flags);
312 if (err <= 0) {
313 dprintk("%s: failed to recv data: size: %llu, err: %d.\n",
314 __func__, size, err);
315 if (err == 0)
316 err = -ECONNRESET;
318 dst_state_exit_connected(st);
321 return err;
325 * Ping command to early detect failed nodes.
327 static int dst_send_ping(struct dst_state *st)
329 struct dst_cmd *cmd = st->data;
330 int err = -ECONNRESET;
332 dst_state_lock(st);
333 if (st->socket) {
334 memset(cmd, 0, sizeof(struct dst_cmd));
336 cmd->cmd = __cpu_to_be32(DST_PING);
338 err = dst_data_send_header(st->socket, cmd, sizeof(struct dst_cmd), 0);
340 dprintk("%s: st: %p, socket: %p, err: %d.\n", __func__, st, st->socket, err);
341 dst_state_unlock(st);
343 return err;
347 * Receiving function, which should either return error or read
348 * whole block request. If there was no traffic for a one second,
349 * send a ping, since remote node may die.
351 int dst_data_recv(struct dst_state *st, void *data, unsigned int size)
353 unsigned int revents = 0;
354 unsigned int err_mask = POLLERR | POLLHUP | POLLRDHUP;
355 unsigned int mask = err_mask | POLLIN;
356 struct dst_node *n = st->node;
357 int err = 0;
359 while (size && !err) {
360 revents = dst_state_poll(st);
362 if (!(revents & mask)) {
363 DEFINE_WAIT(wait);
365 for (;;) {
366 prepare_to_wait(&st->thread_wait, &wait,
367 TASK_INTERRUPTIBLE);
368 if (!n->trans_scan_timeout || st->need_exit)
369 break;
371 revents = dst_state_poll(st);
373 if (revents & mask)
374 break;
376 if (signal_pending(current))
377 break;
379 if (!schedule_timeout(HZ)) {
380 err = dst_send_ping(st);
381 if (err)
382 return err;
385 continue;
387 finish_wait(&st->thread_wait, &wait);
390 err = -ECONNRESET;
391 dst_state_lock(st);
393 if ( st->socket &&
394 (st->read_socket == st->socket) &&
395 (revents & POLLIN)) {
396 err = dst_data_recv_raw(st, data, size);
397 if (err > 0) {
398 data += err;
399 size -= err;
400 err = 0;
404 if (revents & err_mask || !st->socket) {
405 dprintk("%s: revents: %x, socket: %p, size: %u, err: %d.\n",
406 __func__, revents, st->socket, size, err);
407 err = -ECONNRESET;
410 dst_state_unlock(st);
412 if (!n->trans_scan_timeout)
413 err = -ENODEV;
416 return err;
420 * Send block autoconf reply.
422 static int dst_process_cfg(struct dst_state *st)
424 struct dst_node *n = st->node;
425 struct dst_cmd *cmd = st->data;
426 int err;
428 cmd->sector = n->size;
429 cmd->rw = st->permissions;
431 dst_convert_cmd(cmd);
433 dst_state_lock(st);
434 err = dst_data_send_header(st->socket, cmd, sizeof(struct dst_cmd), 0);
435 dst_state_unlock(st);
437 return err;
441 * Receive block IO from the network.
443 static int dst_recv_bio(struct dst_state *st, struct bio *bio, unsigned int total_size)
445 struct bio_vec *bv;
446 int i, err;
447 void *data;
448 unsigned int sz;
450 bio_for_each_segment(bv, bio, i) {
451 sz = min(total_size, bv->bv_len);
453 dprintk("%s: bio: %llu/%u, total: %u, len: %u, sz: %u, off: %u.\n",
454 __func__, (u64)bio->bi_sector, bio->bi_size, total_size,
455 bv->bv_len, sz, bv->bv_offset);
457 data = kmap(bv->bv_page) + bv->bv_offset;
458 err = dst_data_recv(st, data, sz);
459 kunmap(bv->bv_page);
461 bv->bv_len = sz;
463 if (err)
464 return err;
466 total_size -= sz;
467 if (total_size == 0)
468 break;
471 return 0;
475 * Our block IO has just completed and arrived: get it.
477 static int dst_process_io_response(struct dst_state *st)
479 struct dst_node *n = st->node;
480 struct dst_cmd *cmd = st->data;
481 struct dst_trans *t;
482 int err = 0;
483 struct bio *bio;
485 mutex_lock(&n->trans_lock);
486 t = dst_trans_search(n, cmd->id);
487 mutex_unlock(&n->trans_lock);
489 if (!t)
490 goto err_out_exit;
492 bio = t->bio;
494 dprintk("%s: bio: %llu/%u, cmd_size: %u, csize: %u, dir: %lu.\n",
495 __func__, (u64)bio->bi_sector, bio->bi_size, cmd->size,
496 cmd->csize, bio_data_dir(bio));
498 if (bio_data_dir(bio) == READ) {
499 if (bio->bi_size != cmd->size - cmd->csize)
500 goto err_out_exit;
502 if (dst_need_crypto(n)) {
503 err = dst_recv_cdata(st, t->cmd.hash);
504 if (err)
505 goto err_out_exit;
508 err = dst_recv_bio(st, t->bio, bio->bi_size);
509 if (err)
510 goto err_out_exit;
512 if (dst_need_crypto(n))
513 return dst_trans_crypto(t);
514 } else {
515 err = -EBADMSG;
516 if (cmd->size || cmd->csize)
517 goto err_out_exit;
520 dst_trans_remove(t);
521 dst_trans_put(t);
523 return 0;
525 err_out_exit:
526 return err;
530 * Receive crypto data.
532 int dst_recv_cdata(struct dst_state *st, void *cdata)
534 struct dst_cmd *cmd = st->data;
535 struct dst_node *n = st->node;
536 struct dst_crypto_ctl *c = &n->crypto;
537 int err;
539 if (cmd->csize != c->crypto_attached_size) {
540 dprintk("%s: cmd: cmd: %u, sector: %llu, size: %u, "
541 "csize: %u != digest size %u.\n",
542 __func__, cmd->cmd, cmd->sector, cmd->size,
543 cmd->csize, c->crypto_attached_size);
544 err = -EINVAL;
545 goto err_out_exit;
548 err = dst_data_recv(st, cdata, cmd->csize);
549 if (err)
550 goto err_out_exit;
552 cmd->size -= cmd->csize;
553 return 0;
555 err_out_exit:
556 return err;
560 * Receive the command and start its processing.
562 static int dst_recv_processing(struct dst_state *st)
564 int err = -EINTR;
565 struct dst_cmd *cmd = st->data;
568 * If socket will be reset after this statement, then
569 * dst_data_recv() will just fail and loop will
570 * start again, so it can be done without any locks.
572 * st->read_socket is needed to prevents state machine
573 * breaking between this data reading and subsequent one
574 * in protocol specific functions during connection reset.
575 * In case of reset we have to read next command and do
576 * not expect data for old command to magically appear in
577 * new connection.
579 st->read_socket = st->socket;
580 err = dst_data_recv(st, cmd, sizeof(struct dst_cmd));
581 if (err)
582 goto out_exit;
584 dst_convert_cmd(cmd);
586 dprintk("%s: cmd: %u, size: %u, csize: %u, id: %llu, "
587 "sector: %llu, flags: %llx, rw: %llx.\n",
588 __func__, cmd->cmd, cmd->size,
589 cmd->csize, cmd->id, cmd->sector,
590 cmd->flags, cmd->rw);
593 * This should catch protocol breakage and random garbage instead of commands.
595 if (unlikely(cmd->csize > st->size - sizeof(struct dst_cmd))) {
596 err = -EBADMSG;
597 goto out_exit;
600 err = -EPROTO;
601 switch (cmd->cmd) {
602 case DST_IO_RESPONSE:
603 err = dst_process_io_response(st);
604 break;
605 case DST_IO:
606 err = dst_process_io(st);
607 break;
608 case DST_CFG:
609 err = dst_process_cfg(st);
610 break;
611 case DST_PING:
612 err = 0;
613 break;
614 default:
615 break;
618 out_exit:
619 return err;
623 * Receiving thread. For the client node we should try to reconnect,
624 * for accepted client we just drop the state and expect it to reconnect.
626 static int dst_recv(void *init_data, void *schedule_data)
628 struct dst_state *st = schedule_data;
629 struct dst_node *n = init_data;
630 int err = 0;
632 dprintk("%s: start st: %p, n: %p, scan: %lu, need_exit: %d.\n",
633 __func__, st, n, n->trans_scan_timeout, st->need_exit);
635 while (n->trans_scan_timeout && !st->need_exit) {
636 err = dst_recv_processing(st);
637 if (err < 0) {
638 if (!st->ctl.type)
639 break;
641 if (!n->trans_scan_timeout || st->need_exit)
642 break;
644 dst_state_reset(st);
645 msleep(1000);
649 st->need_exit = 1;
650 wake_up(&st->thread_wait);
652 dprintk("%s: freeing receiving socket st: %p.\n", __func__, st);
653 dst_state_lock(st);
654 dst_state_exit_connected(st);
655 dst_state_unlock(st);
656 dst_state_put(st);
658 dprintk("%s: freed receiving socket st: %p.\n", __func__, st);
660 return err;
664 * Network state dies here and borns couple of lines below.
665 * This object is the main network state processing engine:
666 * sending, receiving, reconnections, all network related
667 * tasks are handled on behalf of the state.
669 static void dst_state_free(struct dst_state *st)
671 dprintk("%s: st: %p.\n", __func__, st);
672 if (st->cleanup)
673 st->cleanup(st);
674 kfree(st->data);
675 kfree(st);
678 struct dst_state *dst_state_alloc(struct dst_node *n)
680 struct dst_state *st;
681 int err = -ENOMEM;
683 st = kzalloc(sizeof(struct dst_state), GFP_KERNEL);
684 if (!st)
685 goto err_out_exit;
687 st->node = n;
688 st->need_exit = 0;
690 st->size = PAGE_SIZE;
691 st->data = kmalloc(st->size, GFP_KERNEL);
692 if (!st->data)
693 goto err_out_free;
695 spin_lock_init(&st->request_lock);
696 INIT_LIST_HEAD(&st->request_list);
698 mutex_init(&st->state_lock);
699 init_waitqueue_head(&st->thread_wait);
702 * One for processing thread, another one for node itself.
704 atomic_set(&st->refcnt, 2);
706 dprintk("%s: st: %p, n: %p.\n", __func__, st, st->node);
708 return st;
710 err_out_free:
711 kfree(st);
712 err_out_exit:
713 return ERR_PTR(err);
716 int dst_state_schedule_receiver(struct dst_state *st)
718 return thread_pool_schedule_private(st->node->pool, dst_thread_setup,
719 dst_recv, st, MAX_SCHEDULE_TIMEOUT, st->node);
723 * Initialize client's connection to the remote peer: allocate state,
724 * connect and perform block IO autoconfiguration.
726 int dst_node_init_connected(struct dst_node *n, struct dst_network_ctl *r)
728 struct dst_state *st;
729 int err = -ENOMEM;
731 st = dst_state_alloc(n);
732 if (IS_ERR(st)) {
733 err = PTR_ERR(st);
734 goto err_out_exit;
736 memcpy(&st->ctl, r, sizeof(struct dst_network_ctl));
738 err = dst_state_init_connected(st);
739 if (err)
740 goto err_out_free_data;
742 err = dst_request_remote_config(st);
743 if (err)
744 goto err_out_exit_connected;
745 n->state = st;
747 err = dst_state_schedule_receiver(st);
748 if (err)
749 goto err_out_exit_connected;
751 return 0;
753 err_out_exit_connected:
754 dst_state_exit_connected(st);
755 err_out_free_data:
756 dst_state_free(st);
757 err_out_exit:
758 n->state = NULL;
759 return err;
762 void dst_state_put(struct dst_state *st)
764 dprintk("%s: st: %p, refcnt: %d.\n",
765 __func__, st, atomic_read(&st->refcnt));
766 if (atomic_dec_and_test(&st->refcnt))
767 dst_state_free(st);
771 * Send block IO to the network one by one using zero-copy ->sendpage().
773 int dst_send_bio(struct dst_state *st, struct dst_cmd *cmd, struct bio *bio)
775 struct bio_vec *bv;
776 struct dst_crypto_ctl *c = &st->node->crypto;
777 int err, i = 0;
778 int flags = MSG_WAITALL;
780 err = dst_data_send_header(st->socket, cmd,
781 sizeof(struct dst_cmd) + c->crypto_attached_size, bio->bi_vcnt);
782 if (err)
783 goto err_out_exit;
785 bio_for_each_segment(bv, bio, i) {
786 if (i < bio->bi_vcnt - 1)
787 flags |= MSG_MORE;
789 err = kernel_sendpage(st->socket, bv->bv_page, bv->bv_offset,
790 bv->bv_len, flags);
791 if (err <= 0)
792 goto err_out_exit;
795 return 0;
797 err_out_exit:
798 dprintk("%s: %d/%d, flags: %x, err: %d.\n",
799 __func__, i, bio->bi_vcnt, flags, err);
800 return err;
804 * Send transaction to the remote peer.
806 int dst_trans_send(struct dst_trans *t)
808 int err;
809 struct dst_state *st = t->n->state;
810 struct bio *bio = t->bio;
812 dst_convert_cmd(&t->cmd);
814 dst_state_lock(st);
815 if (!st->socket) {
816 err = dst_state_init_connected(st);
817 if (err)
818 goto err_out_unlock;
821 if (bio_data_dir(bio) == WRITE) {
822 err = dst_send_bio(st, &t->cmd, t->bio);
823 } else {
824 err = dst_data_send_header(st->socket, &t->cmd,
825 sizeof(struct dst_cmd), 0);
827 if (err)
828 goto err_out_reset;
830 dst_state_unlock(st);
831 return 0;
833 err_out_reset:
834 dst_state_reset_nolock(st);
835 err_out_unlock:
836 dst_state_unlock(st);
838 return err;