4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
28 #include <asm/uaccess.h>
31 #include <linux/drbd.h>
33 #include <linux/file.h>
36 #include <linux/memcontrol.h>
37 #include <linux/mm_inline.h>
38 #include <linux/slab.h>
39 #include <linux/pkt_sched.h>
40 #define __KERNEL_SYSCALLS__
41 #include <linux/unistd.h>
42 #include <linux/vmalloc.h>
43 #include <linux/random.h>
44 #include <linux/string.h>
45 #include <linux/scatterlist.h>
64 static int drbd_do_features(struct drbd_tconn *tconn);
65 static int drbd_do_auth(struct drbd_tconn *tconn);
66 static int drbd_disconnected(struct drbd_conf *mdev);
68 static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *, struct drbd_epoch *, enum epoch_event);
69 static int e_end_block(struct drbd_work *, int);
72 #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
75 * some helper functions to deal with single linked page lists,
76 * page->private being our "next" pointer.
79 /* If at least n pages are linked at head, get n pages off.
80 * Otherwise, don't modify head, and return NULL.
81 * Locking is the responsibility of the caller.
83 static struct page *page_chain_del(struct page **head, int n)
97 tmp = page_chain_next(page);
99 break; /* found sufficient pages */
101 /* insufficient pages, don't use any of them. */
106 /* add end of list marker for the returned list */
107 set_page_private(page, 0);
108 /* actual return value, and adjustment of head */
114 /* may be used outside of locks to find the tail of a (usually short)
115 * "private" page chain, before adding it back to a global chain head
116 * with page_chain_add() under a spinlock. */
117 static struct page *page_chain_tail(struct page *page, int *len)
121 while ((tmp = page_chain_next(page)))
128 static int page_chain_free(struct page *page)
132 page_chain_for_each_safe(page, tmp) {
139 static void page_chain_add(struct page **head,
140 struct page *chain_first, struct page *chain_last)
144 tmp = page_chain_tail(chain_first, NULL);
145 BUG_ON(tmp != chain_last);
148 /* add chain to head */
149 set_page_private(chain_last, (unsigned long)*head);
153 static struct page *__drbd_alloc_pages(struct drbd_conf *mdev,
156 struct page *page = NULL;
157 struct page *tmp = NULL;
160 /* Yes, testing drbd_pp_vacant outside the lock is racy.
161 * So what. It saves a spin_lock. */
162 if (drbd_pp_vacant >= number) {
163 spin_lock(&drbd_pp_lock);
164 page = page_chain_del(&drbd_pp_pool, number);
166 drbd_pp_vacant -= number;
167 spin_unlock(&drbd_pp_lock);
172 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
173 * "criss-cross" setup, that might cause write-out on some other DRBD,
174 * which in turn might block on the other node at this very place. */
175 for (i = 0; i < number; i++) {
176 tmp = alloc_page(GFP_TRY);
179 set_page_private(tmp, (unsigned long)page);
186 /* Not enough pages immediately available this time.
187 * No need to jump around here, drbd_alloc_pages will retry this
188 * function "soon". */
190 tmp = page_chain_tail(page, NULL);
191 spin_lock(&drbd_pp_lock);
192 page_chain_add(&drbd_pp_pool, page, tmp);
194 spin_unlock(&drbd_pp_lock);
199 static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev,
200 struct list_head *to_be_freed)
202 struct drbd_peer_request *peer_req;
203 struct list_head *le, *tle;
205 /* The EEs are always appended to the end of the list. Since
206 they are sent in order over the wire, they have to finish
207 in order. As soon as we see the first not finished we can
208 stop to examine the list... */
210 list_for_each_safe(le, tle, &mdev->net_ee) {
211 peer_req = list_entry(le, struct drbd_peer_request, w.list);
212 if (drbd_peer_req_has_active_page(peer_req))
214 list_move(le, to_be_freed);
218 static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
220 LIST_HEAD(reclaimed);
221 struct drbd_peer_request *peer_req, *t;
223 spin_lock_irq(&mdev->tconn->req_lock);
224 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
225 spin_unlock_irq(&mdev->tconn->req_lock);
227 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
228 drbd_free_net_peer_req(mdev, peer_req);
232 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
233 * @mdev: DRBD device.
234 * @number: number of pages requested
235 * @retry: whether to retry, if not enough pages are available right now
237 * Tries to allocate number pages, first from our own page pool, then from
238 * the kernel, unless this allocation would exceed the max_buffers setting.
239 * Possibly retry until DRBD frees sufficient pages somewhere else.
241 * Returns a page chain linked via page->private.
243 struct page *drbd_alloc_pages(struct drbd_conf *mdev, unsigned int number,
246 struct page *page = NULL;
251 /* Yes, we may run up to @number over max_buffers. If we
252 * follow it strictly, the admin will get it wrong anyways. */
254 nc = rcu_dereference(mdev->tconn->net_conf);
255 mxb = nc ? nc->max_buffers : 1000000;
258 if (atomic_read(&mdev->pp_in_use) < mxb)
259 page = __drbd_alloc_pages(mdev, number);
261 while (page == NULL) {
262 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
264 drbd_kick_lo_and_reclaim_net(mdev);
266 if (atomic_read(&mdev->pp_in_use) < mxb) {
267 page = __drbd_alloc_pages(mdev, number);
275 if (signal_pending(current)) {
276 dev_warn(DEV, "drbd_alloc_pages interrupted!\n");
282 finish_wait(&drbd_pp_wait, &wait);
285 atomic_add(number, &mdev->pp_in_use);
289 /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
290 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
291 * Either links the page chain back to the global pool,
292 * or returns all pages to the system. */
293 static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net)
295 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
301 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
302 i = page_chain_free(page);
305 tmp = page_chain_tail(page, &i);
306 spin_lock(&drbd_pp_lock);
307 page_chain_add(&drbd_pp_pool, page, tmp);
309 spin_unlock(&drbd_pp_lock);
311 i = atomic_sub_return(i, a);
313 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
314 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
315 wake_up(&drbd_pp_wait);
319 You need to hold the req_lock:
320 _drbd_wait_ee_list_empty()
322 You must not have the req_lock:
324 drbd_alloc_peer_req()
325 drbd_free_peer_reqs()
327 drbd_finish_peer_reqs()
329 drbd_wait_ee_list_empty()
332 struct drbd_peer_request *
333 drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
334 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
336 struct drbd_peer_request *peer_req;
337 struct page *page = NULL;
338 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
340 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
343 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
345 if (!(gfp_mask & __GFP_NOWARN))
346 dev_err(DEV, "%s: allocation failed\n", __func__);
351 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
356 drbd_clear_interval(&peer_req->i);
357 peer_req->i.size = data_size;
358 peer_req->i.sector = sector;
359 peer_req->i.local = false;
360 peer_req->i.waiting = false;
362 peer_req->epoch = NULL;
363 peer_req->w.mdev = mdev;
364 peer_req->pages = page;
365 atomic_set(&peer_req->pending_bios, 0);
368 * The block_id is opaque to the receiver. It is not endianness
369 * converted, and sent back to the sender unchanged.
371 peer_req->block_id = id;
376 mempool_free(peer_req, drbd_ee_mempool);
380 void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
383 if (peer_req->flags & EE_HAS_DIGEST)
384 kfree(peer_req->digest);
385 drbd_free_pages(mdev, peer_req->pages, is_net);
386 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
387 D_ASSERT(drbd_interval_empty(&peer_req->i));
388 mempool_free(peer_req, drbd_ee_mempool);
391 int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
393 LIST_HEAD(work_list);
394 struct drbd_peer_request *peer_req, *t;
396 int is_net = list == &mdev->net_ee;
398 spin_lock_irq(&mdev->tconn->req_lock);
399 list_splice_init(list, &work_list);
400 spin_unlock_irq(&mdev->tconn->req_lock);
402 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
403 __drbd_free_peer_req(mdev, peer_req, is_net);
410 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
412 static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
414 LIST_HEAD(work_list);
415 LIST_HEAD(reclaimed);
416 struct drbd_peer_request *peer_req, *t;
419 spin_lock_irq(&mdev->tconn->req_lock);
420 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
421 list_splice_init(&mdev->done_ee, &work_list);
422 spin_unlock_irq(&mdev->tconn->req_lock);
424 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
425 drbd_free_net_peer_req(mdev, peer_req);
427 /* possible callbacks here:
428 * e_end_block, and e_end_resync_block, e_send_superseded.
429 * all ignore the last argument.
431 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
434 /* list_del not necessary, next/prev members not touched */
435 err2 = peer_req->w.cb(&peer_req->w, !!err);
438 drbd_free_peer_req(mdev, peer_req);
440 wake_up(&mdev->ee_wait);
445 static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
446 struct list_head *head)
450 /* avoids spin_lock/unlock
451 * and calling prepare_to_wait in the fast path */
452 while (!list_empty(head)) {
453 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
454 spin_unlock_irq(&mdev->tconn->req_lock);
456 finish_wait(&mdev->ee_wait, &wait);
457 spin_lock_irq(&mdev->tconn->req_lock);
461 static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
462 struct list_head *head)
464 spin_lock_irq(&mdev->tconn->req_lock);
465 _drbd_wait_ee_list_empty(mdev, head);
466 spin_unlock_irq(&mdev->tconn->req_lock);
469 static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
476 struct msghdr msg = {
478 .msg_iov = (struct iovec *)&iov,
479 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
485 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
491 static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
495 rv = drbd_recv_short(tconn->data.socket, buf, size, 0);
498 if (rv == -ECONNRESET)
499 conn_info(tconn, "sock was reset by peer\n");
500 else if (rv != -ERESTARTSYS)
501 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
502 } else if (rv == 0) {
503 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
506 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
509 t = wait_event_timeout(tconn->ping_wait, tconn->cstate < C_WF_REPORT_PARAMS, t);
514 conn_info(tconn, "sock was shut down by peer\n");
518 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
524 static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
528 err = drbd_recv(tconn, buf, size);
537 static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
541 err = drbd_recv_all(tconn, buf, size);
542 if (err && !signal_pending(current))
543 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
548 * On individual connections, the socket buffer size must be set prior to the
549 * listen(2) or connect(2) calls in order to have it take effect.
550 * This is our wrapper to do so.
552 static void drbd_setbufsize(struct socket *sock, unsigned int snd,
555 /* open coded SO_SNDBUF, SO_RCVBUF */
557 sock->sk->sk_sndbuf = snd;
558 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
561 sock->sk->sk_rcvbuf = rcv;
562 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
566 static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
570 struct sockaddr_in6 src_in6;
571 struct sockaddr_in6 peer_in6;
573 int err, peer_addr_len, my_addr_len;
574 int sndbuf_size, rcvbuf_size, connect_int;
575 int disconnect_on_error = 1;
578 nc = rcu_dereference(tconn->net_conf);
583 sndbuf_size = nc->sndbuf_size;
584 rcvbuf_size = nc->rcvbuf_size;
585 connect_int = nc->connect_int;
588 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
589 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
591 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
592 src_in6.sin6_port = 0;
594 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
596 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
597 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
599 what = "sock_create_kern";
600 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
601 SOCK_STREAM, IPPROTO_TCP, &sock);
607 sock->sk->sk_rcvtimeo =
608 sock->sk->sk_sndtimeo = connect_int * HZ;
609 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
611 /* explicitly bind to the configured IP as source IP
612 * for the outgoing connections.
613 * This is needed for multihomed hosts and to be
614 * able to use lo: interfaces for drbd.
615 * Make sure to use 0 as port number, so linux selects
616 * a free one dynamically.
618 what = "bind before connect";
619 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
623 /* connect may fail, peer not yet available.
624 * stay C_WF_CONNECTION, don't go Disconnecting! */
625 disconnect_on_error = 0;
627 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
636 /* timeout, busy, signal pending */
637 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
638 case EINTR: case ERESTARTSYS:
639 /* peer not (yet) available, network problem */
640 case ECONNREFUSED: case ENETUNREACH:
641 case EHOSTDOWN: case EHOSTUNREACH:
642 disconnect_on_error = 0;
645 conn_err(tconn, "%s failed, err = %d\n", what, err);
647 if (disconnect_on_error)
648 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
654 struct accept_wait_data {
655 struct drbd_tconn *tconn;
656 struct socket *s_listen;
657 struct completion door_bell;
658 void (*original_sk_state_change)(struct sock *sk);
662 static void drbd_incoming_connection(struct sock *sk)
664 struct accept_wait_data *ad = sk->sk_user_data;
665 void (*state_change)(struct sock *sk);
667 state_change = ad->original_sk_state_change;
668 if (sk->sk_state == TCP_ESTABLISHED)
669 complete(&ad->door_bell);
673 static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
675 int err, sndbuf_size, rcvbuf_size, my_addr_len;
676 struct sockaddr_in6 my_addr;
677 struct socket *s_listen;
682 nc = rcu_dereference(tconn->net_conf);
687 sndbuf_size = nc->sndbuf_size;
688 rcvbuf_size = nc->rcvbuf_size;
691 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
692 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
694 what = "sock_create_kern";
695 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
696 SOCK_STREAM, IPPROTO_TCP, &s_listen);
702 s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
703 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
705 what = "bind before listen";
706 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
710 ad->s_listen = s_listen;
711 write_lock_bh(&s_listen->sk->sk_callback_lock);
712 ad->original_sk_state_change = s_listen->sk->sk_state_change;
713 s_listen->sk->sk_state_change = drbd_incoming_connection;
714 s_listen->sk->sk_user_data = ad;
715 write_unlock_bh(&s_listen->sk->sk_callback_lock);
718 err = s_listen->ops->listen(s_listen, 5);
725 sock_release(s_listen);
727 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
728 conn_err(tconn, "%s failed, err = %d\n", what, err);
729 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
736 static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
738 write_lock_bh(&sk->sk_callback_lock);
739 sk->sk_state_change = ad->original_sk_state_change;
740 sk->sk_user_data = NULL;
741 write_unlock_bh(&sk->sk_callback_lock);
744 static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
746 int timeo, connect_int, err = 0;
747 struct socket *s_estab = NULL;
751 nc = rcu_dereference(tconn->net_conf);
756 connect_int = nc->connect_int;
759 timeo = connect_int * HZ;
760 /* 28.5% random jitter */
761 timeo += (prandom_u32() & 1) ? timeo / 7 : -timeo / 7;
763 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
767 err = kernel_accept(ad->s_listen, &s_estab, 0);
769 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
770 conn_err(tconn, "accept failed, err = %d\n", err);
771 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
776 unregister_state_change(s_estab->sk, ad);
781 static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
783 static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
784 enum drbd_packet cmd)
786 if (!conn_prepare_command(tconn, sock))
788 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
791 static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
793 unsigned int header_size = drbd_header_size(tconn);
794 struct packet_info pi;
797 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
798 if (err != header_size) {
803 err = decode_header(tconn, tconn->data.rbuf, &pi);
810 * drbd_socket_okay() - Free the socket if its connection is not okay
811 * @sock: pointer to the pointer to the socket.
813 static int drbd_socket_okay(struct socket **sock)
821 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
823 if (rr > 0 || rr == -EAGAIN) {
831 /* Gets called if a connection is established, or if a new minor gets created
833 int drbd_connected(struct drbd_conf *mdev)
837 atomic_set(&mdev->packet_seq, 0);
840 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
841 &mdev->tconn->cstate_mutex :
842 &mdev->own_state_mutex;
844 err = drbd_send_sync_param(mdev);
846 err = drbd_send_sizes(mdev, 0, 0);
848 err = drbd_send_uuids(mdev);
850 err = drbd_send_current_state(mdev);
851 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
852 clear_bit(RESIZE_PENDING, &mdev->flags);
853 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
859 * 1 yes, we have a valid connection
860 * 0 oops, did not work out, please try again
861 * -1 peer talks different language,
862 * no point in trying again, please go standalone.
863 * -2 We do not have a network config...
865 static int conn_connect(struct drbd_tconn *tconn)
867 struct drbd_socket sock, msock;
868 struct drbd_conf *mdev;
870 int vnr, timeout, h, ok;
871 bool discard_my_data;
872 enum drbd_state_rv rv;
873 struct accept_wait_data ad = {
875 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
878 clear_bit(DISCONNECT_SENT, &tconn->flags);
879 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
882 mutex_init(&sock.mutex);
883 sock.sbuf = tconn->data.sbuf;
884 sock.rbuf = tconn->data.rbuf;
886 mutex_init(&msock.mutex);
887 msock.sbuf = tconn->meta.sbuf;
888 msock.rbuf = tconn->meta.rbuf;
891 /* Assume that the peer only understands protocol 80 until we know better. */
892 tconn->agreed_pro_version = 80;
894 if (prepare_listen_socket(tconn, &ad))
900 s = drbd_try_connect(tconn);
904 send_first_packet(tconn, &sock, P_INITIAL_DATA);
905 } else if (!msock.socket) {
906 clear_bit(RESOLVE_CONFLICTS, &tconn->flags);
908 send_first_packet(tconn, &msock, P_INITIAL_META);
910 conn_err(tconn, "Logic error in conn_connect()\n");
911 goto out_release_sockets;
915 if (sock.socket && msock.socket) {
917 nc = rcu_dereference(tconn->net_conf);
918 timeout = nc->ping_timeo * HZ / 10;
920 schedule_timeout_interruptible(timeout);
921 ok = drbd_socket_okay(&sock.socket);
922 ok = drbd_socket_okay(&msock.socket) && ok;
928 s = drbd_wait_for_connect(tconn, &ad);
930 int fp = receive_first_packet(tconn, s);
931 drbd_socket_okay(&sock.socket);
932 drbd_socket_okay(&msock.socket);
936 conn_warn(tconn, "initial packet S crossed\n");
937 sock_release(sock.socket);
944 set_bit(RESOLVE_CONFLICTS, &tconn->flags);
946 conn_warn(tconn, "initial packet M crossed\n");
947 sock_release(msock.socket);
954 conn_warn(tconn, "Error receiving initial packet\n");
957 if (prandom_u32() & 1)
962 if (tconn->cstate <= C_DISCONNECTING)
963 goto out_release_sockets;
964 if (signal_pending(current)) {
965 flush_signals(current);
967 if (get_t_state(&tconn->receiver) == EXITING)
968 goto out_release_sockets;
971 ok = drbd_socket_okay(&sock.socket);
972 ok = drbd_socket_okay(&msock.socket) && ok;
976 sock_release(ad.s_listen);
978 sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
979 msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
981 sock.socket->sk->sk_allocation = GFP_NOIO;
982 msock.socket->sk->sk_allocation = GFP_NOIO;
984 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
985 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
988 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
989 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
990 * first set it to the P_CONNECTION_FEATURES timeout,
991 * which we set to 4x the configured ping_timeout. */
993 nc = rcu_dereference(tconn->net_conf);
995 sock.socket->sk->sk_sndtimeo =
996 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
998 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
999 timeout = nc->timeout * HZ / 10;
1000 discard_my_data = nc->discard_my_data;
1003 msock.socket->sk->sk_sndtimeo = timeout;
1005 /* we don't want delays.
1006 * we use TCP_CORK where appropriate, though */
1007 drbd_tcp_nodelay(sock.socket);
1008 drbd_tcp_nodelay(msock.socket);
1010 tconn->data.socket = sock.socket;
1011 tconn->meta.socket = msock.socket;
1012 tconn->last_received = jiffies;
1014 h = drbd_do_features(tconn);
1018 if (tconn->cram_hmac_tfm) {
1019 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
1020 switch (drbd_do_auth(tconn)) {
1022 conn_err(tconn, "Authentication of peer failed\n");
1025 conn_err(tconn, "Authentication of peer failed, trying again.\n");
1030 tconn->data.socket->sk->sk_sndtimeo = timeout;
1031 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
1033 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
1036 set_bit(STATE_SENT, &tconn->flags);
1039 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1040 kref_get(&mdev->kref);
1041 /* Prevent a race between resync-handshake and
1042 * being promoted to Primary.
1044 * Grab and release the state mutex, so we know that any current
1045 * drbd_set_role() is finished, and any incoming drbd_set_role
1046 * will see the STATE_SENT flag, and wait for it to be cleared.
1048 mutex_lock(mdev->state_mutex);
1049 mutex_unlock(mdev->state_mutex);
1053 if (discard_my_data)
1054 set_bit(DISCARD_MY_DATA, &mdev->flags);
1056 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1058 drbd_connected(mdev);
1059 kref_put(&mdev->kref, &drbd_minor_destroy);
1064 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
1065 if (rv < SS_SUCCESS || tconn->cstate != C_WF_REPORT_PARAMS) {
1066 clear_bit(STATE_SENT, &tconn->flags);
1070 drbd_thread_start(&tconn->asender);
1072 mutex_lock(&tconn->conf_update);
1073 /* The discard_my_data flag is a single-shot modifier to the next
1074 * connection attempt, the handshake of which is now well underway.
1075 * No need for rcu style copying of the whole struct
1076 * just to clear a single value. */
1077 tconn->net_conf->discard_my_data = 0;
1078 mutex_unlock(&tconn->conf_update);
1082 out_release_sockets:
1084 sock_release(ad.s_listen);
1086 sock_release(sock.socket);
1088 sock_release(msock.socket);
1092 static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
1094 unsigned int header_size = drbd_header_size(tconn);
1096 if (header_size == sizeof(struct p_header100) &&
1097 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1098 struct p_header100 *h = header;
1100 conn_err(tconn, "Header padding is not zero\n");
1103 pi->vnr = be16_to_cpu(h->volume);
1104 pi->cmd = be16_to_cpu(h->command);
1105 pi->size = be32_to_cpu(h->length);
1106 } else if (header_size == sizeof(struct p_header95) &&
1107 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
1108 struct p_header95 *h = header;
1109 pi->cmd = be16_to_cpu(h->command);
1110 pi->size = be32_to_cpu(h->length);
1112 } else if (header_size == sizeof(struct p_header80) &&
1113 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1114 struct p_header80 *h = header;
1115 pi->cmd = be16_to_cpu(h->command);
1116 pi->size = be16_to_cpu(h->length);
1119 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1120 be32_to_cpu(*(__be32 *)header),
1121 tconn->agreed_pro_version);
1124 pi->data = header + header_size;
1128 static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
1130 void *buffer = tconn->data.rbuf;
1133 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
1137 err = decode_header(tconn, buffer, pi);
1138 tconn->last_received = jiffies;
1143 static void drbd_flush(struct drbd_tconn *tconn)
1146 struct drbd_conf *mdev;
1149 if (tconn->write_ordering >= WO_bdev_flush) {
1151 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1152 if (!get_ldev(mdev))
1154 kref_get(&mdev->kref);
1157 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1160 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1161 /* would rather check on EOPNOTSUPP, but that is not reliable.
1162 * don't try again for ANY return value != 0
1163 * if (rv == -EOPNOTSUPP) */
1164 drbd_bump_write_ordering(tconn, WO_drain_io);
1167 kref_put(&mdev->kref, &drbd_minor_destroy);
1178 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1179 * @mdev: DRBD device.
1180 * @epoch: Epoch object.
1183 static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
1184 struct drbd_epoch *epoch,
1185 enum epoch_event ev)
1188 struct drbd_epoch *next_epoch;
1189 enum finish_epoch rv = FE_STILL_LIVE;
1191 spin_lock(&tconn->epoch_lock);
1195 epoch_size = atomic_read(&epoch->epoch_size);
1197 switch (ev & ~EV_CLEANUP) {
1199 atomic_dec(&epoch->active);
1201 case EV_GOT_BARRIER_NR:
1202 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
1204 case EV_BECAME_LAST:
1209 if (epoch_size != 0 &&
1210 atomic_read(&epoch->active) == 0 &&
1211 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
1212 if (!(ev & EV_CLEANUP)) {
1213 spin_unlock(&tconn->epoch_lock);
1214 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
1215 spin_lock(&tconn->epoch_lock);
1218 /* FIXME: dec unacked on connection, once we have
1219 * something to count pending connection packets in. */
1220 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
1221 dec_unacked(epoch->tconn);
1224 if (tconn->current_epoch != epoch) {
1225 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1226 list_del(&epoch->list);
1227 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1231 if (rv == FE_STILL_LIVE)
1235 atomic_set(&epoch->epoch_size, 0);
1236 /* atomic_set(&epoch->active, 0); is already zero */
1237 if (rv == FE_STILL_LIVE)
1248 spin_unlock(&tconn->epoch_lock);
1254 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1255 * @tconn: DRBD connection.
1256 * @wo: Write ordering method to try.
1258 void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
1260 struct disk_conf *dc;
1261 struct drbd_conf *mdev;
1262 enum write_ordering_e pwo;
1264 static char *write_ordering_str[] = {
1266 [WO_drain_io] = "drain",
1267 [WO_bdev_flush] = "flush",
1270 pwo = tconn->write_ordering;
1273 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1274 if (!get_ldev_if_state(mdev, D_ATTACHING))
1276 dc = rcu_dereference(mdev->ldev->disk_conf);
1278 if (wo == WO_bdev_flush && !dc->disk_flushes)
1280 if (wo == WO_drain_io && !dc->disk_drain)
1285 tconn->write_ordering = wo;
1286 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1287 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
1291 * drbd_submit_peer_request()
1292 * @mdev: DRBD device.
1293 * @peer_req: peer request
1294 * @rw: flag field, see bio->bi_rw
1296 * May spread the pages to multiple bios,
1297 * depending on bio_add_page restrictions.
1299 * Returns 0 if all bios have been submitted,
1300 * -ENOMEM if we could not allocate enough bios,
1301 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1302 * single page to an empty bio (which should never happen and likely indicates
1303 * that the lower level IO stack is in some way broken). This has been observed
1304 * on certain Xen deployments.
1306 /* TODO allocate from our own bio_set. */
1307 int drbd_submit_peer_request(struct drbd_conf *mdev,
1308 struct drbd_peer_request *peer_req,
1309 const unsigned rw, const int fault_type)
1311 struct bio *bios = NULL;
1313 struct page *page = peer_req->pages;
1314 sector_t sector = peer_req->i.sector;
1315 unsigned ds = peer_req->i.size;
1316 unsigned n_bios = 0;
1317 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
1320 /* In most cases, we will only need one bio. But in case the lower
1321 * level restrictions happen to be different at this offset on this
1322 * side than those of the sending peer, we may need to submit the
1323 * request in more than one bio.
1325 * Plain bio_alloc is good enough here, this is no DRBD internally
1326 * generated bio, but a bio allocated on behalf of the peer.
1329 bio = bio_alloc(GFP_NOIO, nr_pages);
1331 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1334 /* > peer_req->i.sector, unless this is the first bio */
1335 bio->bi_sector = sector;
1336 bio->bi_bdev = mdev->ldev->backing_bdev;
1338 bio->bi_private = peer_req;
1339 bio->bi_end_io = drbd_peer_request_endio;
1341 bio->bi_next = bios;
1345 page_chain_for_each(page) {
1346 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1347 if (!bio_add_page(bio, page, len, 0)) {
1348 /* A single page must always be possible!
1349 * But in case it fails anyways,
1350 * we deal with it, and complain (below). */
1351 if (bio->bi_vcnt == 0) {
1353 "bio_add_page failed for len=%u, "
1354 "bi_vcnt=0 (bi_sector=%llu)\n",
1355 len, (unsigned long long)bio->bi_sector);
1365 D_ASSERT(page == NULL);
1368 atomic_set(&peer_req->pending_bios, n_bios);
1371 bios = bios->bi_next;
1372 bio->bi_next = NULL;
1374 drbd_generic_make_request(mdev, fault_type, bio);
1381 bios = bios->bi_next;
1387 static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
1388 struct drbd_peer_request *peer_req)
1390 struct drbd_interval *i = &peer_req->i;
1392 drbd_remove_interval(&mdev->write_requests, i);
1393 drbd_clear_interval(i);
1395 /* Wake up any processes waiting for this peer request to complete. */
1397 wake_up(&mdev->misc_wait);
1400 void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1402 struct drbd_conf *mdev;
1406 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1407 kref_get(&mdev->kref);
1409 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1410 kref_put(&mdev->kref, &drbd_minor_destroy);
1416 static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
1419 struct p_barrier *p = pi->data;
1420 struct drbd_epoch *epoch;
1422 /* FIXME these are unacked on connection,
1423 * not a specific (peer)device.
1425 tconn->current_epoch->barrier_nr = p->barrier;
1426 tconn->current_epoch->tconn = tconn;
1427 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
1429 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1430 * the activity log, which means it would not be resynced in case the
1431 * R_PRIMARY crashes now.
1432 * Therefore we must send the barrier_ack after the barrier request was
1434 switch (tconn->write_ordering) {
1436 if (rv == FE_RECYCLED)
1439 /* receiver context, in the writeout path of the other node.
1440 * avoid potential distributed deadlock */
1441 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1445 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
1450 conn_wait_active_ee_empty(tconn);
1453 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1454 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1461 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
1466 atomic_set(&epoch->epoch_size, 0);
1467 atomic_set(&epoch->active, 0);
1469 spin_lock(&tconn->epoch_lock);
1470 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1471 list_add(&epoch->list, &tconn->current_epoch->list);
1472 tconn->current_epoch = epoch;
1475 /* The current_epoch got recycled while we allocated this one... */
1478 spin_unlock(&tconn->epoch_lock);
1483 /* used from receive_RSDataReply (recv_resync_read)
1484 * and from receive_Data */
1485 static struct drbd_peer_request *
1486 read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1487 int data_size) __must_hold(local)
1489 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
1490 struct drbd_peer_request *peer_req;
1493 void *dig_in = mdev->tconn->int_dig_in;
1494 void *dig_vv = mdev->tconn->int_dig_vv;
1495 unsigned long *data;
1498 if (mdev->tconn->peer_integrity_tfm) {
1499 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
1501 * FIXME: Receive the incoming digest into the receive buffer
1502 * here, together with its struct p_data?
1504 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1510 if (!expect(IS_ALIGNED(data_size, 512)))
1512 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1515 /* even though we trust out peer,
1516 * we sometimes have to double check. */
1517 if (sector + (data_size>>9) > capacity) {
1518 dev_err(DEV, "request from peer beyond end of local disk: "
1519 "capacity: %llus < sector: %llus + size: %u\n",
1520 (unsigned long long)capacity,
1521 (unsigned long long)sector, data_size);
1525 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1526 * "criss-cross" setup, that might cause write-out on some other DRBD,
1527 * which in turn might block on the other node at this very place. */
1528 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
1536 page = peer_req->pages;
1537 page_chain_for_each(page) {
1538 unsigned len = min_t(int, ds, PAGE_SIZE);
1540 err = drbd_recv_all_warn(mdev->tconn, data, len);
1541 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
1542 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1543 data[0] = data[0] ^ (unsigned long)-1;
1547 drbd_free_peer_req(mdev, peer_req);
1554 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
1555 if (memcmp(dig_in, dig_vv, dgs)) {
1556 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1557 (unsigned long long)sector, data_size);
1558 drbd_free_peer_req(mdev, peer_req);
1562 mdev->recv_cnt += data_size>>9;
1566 /* drbd_drain_block() just takes a data block
1567 * out of the socket input buffer, and discards it.
1569 static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1578 page = drbd_alloc_pages(mdev, 1, 1);
1582 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1584 err = drbd_recv_all_warn(mdev->tconn, data, len);
1590 drbd_free_pages(mdev, page, 0);
1594 static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1595 sector_t sector, int data_size)
1597 struct bio_vec *bvec;
1599 int dgs, err, i, expect;
1600 void *dig_in = mdev->tconn->int_dig_in;
1601 void *dig_vv = mdev->tconn->int_dig_vv;
1604 if (mdev->tconn->peer_integrity_tfm) {
1605 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
1606 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1612 /* optimistically update recv_cnt. if receiving fails below,
1613 * we disconnect anyways, and counters will be reset. */
1614 mdev->recv_cnt += data_size>>9;
1616 bio = req->master_bio;
1617 D_ASSERT(sector == bio->bi_sector);
1619 bio_for_each_segment(bvec, bio, i) {
1620 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
1621 expect = min_t(int, data_size, bvec->bv_len);
1622 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
1623 kunmap(bvec->bv_page);
1626 data_size -= expect;
1630 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
1631 if (memcmp(dig_in, dig_vv, dgs)) {
1632 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1637 D_ASSERT(data_size == 0);
1642 * e_end_resync_block() is called in asender context via
1643 * drbd_finish_peer_reqs().
1645 static int e_end_resync_block(struct drbd_work *w, int unused)
1647 struct drbd_peer_request *peer_req =
1648 container_of(w, struct drbd_peer_request, w);
1649 struct drbd_conf *mdev = w->mdev;
1650 sector_t sector = peer_req->i.sector;
1653 D_ASSERT(drbd_interval_empty(&peer_req->i));
1655 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1656 drbd_set_in_sync(mdev, sector, peer_req->i.size);
1657 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
1659 /* Record failure to sync */
1660 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
1662 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
1669 static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1671 struct drbd_peer_request *peer_req;
1673 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1677 dec_rs_pending(mdev);
1680 /* corresponding dec_unacked() in e_end_resync_block()
1681 * respective _drbd_clear_done_ee */
1683 peer_req->w.cb = e_end_resync_block;
1685 spin_lock_irq(&mdev->tconn->req_lock);
1686 list_add(&peer_req->w.list, &mdev->sync_ee);
1687 spin_unlock_irq(&mdev->tconn->req_lock);
1689 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
1690 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
1693 /* don't care for the reason here */
1694 dev_err(DEV, "submit failed, triggering re-connect\n");
1695 spin_lock_irq(&mdev->tconn->req_lock);
1696 list_del(&peer_req->w.list);
1697 spin_unlock_irq(&mdev->tconn->req_lock);
1699 drbd_free_peer_req(mdev, peer_req);
1705 static struct drbd_request *
1706 find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1707 sector_t sector, bool missing_ok, const char *func)
1709 struct drbd_request *req;
1711 /* Request object according to our peer */
1712 req = (struct drbd_request *)(unsigned long)id;
1713 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
1716 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
1717 (unsigned long)id, (unsigned long long)sector);
1722 static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
1724 struct drbd_conf *mdev;
1725 struct drbd_request *req;
1728 struct p_data *p = pi->data;
1730 mdev = vnr_to_mdev(tconn, pi->vnr);
1734 sector = be64_to_cpu(p->sector);
1736 spin_lock_irq(&mdev->tconn->req_lock);
1737 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
1738 spin_unlock_irq(&mdev->tconn->req_lock);
1742 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
1743 * special casing it there for the various failure cases.
1744 * still no race with drbd_fail_pending_reads */
1745 err = recv_dless_read(mdev, req, sector, pi->size);
1747 req_mod(req, DATA_RECEIVED);
1748 /* else: nothing. handled from drbd_disconnect...
1749 * I don't think we may complete this just yet
1750 * in case we are "on-disconnect: freeze" */
1755 static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
1757 struct drbd_conf *mdev;
1760 struct p_data *p = pi->data;
1762 mdev = vnr_to_mdev(tconn, pi->vnr);
1766 sector = be64_to_cpu(p->sector);
1767 D_ASSERT(p->block_id == ID_SYNCER);
1769 if (get_ldev(mdev)) {
1770 /* data is submitted to disk within recv_resync_read.
1771 * corresponding put_ldev done below on error,
1772 * or in drbd_peer_request_endio. */
1773 err = recv_resync_read(mdev, sector, pi->size);
1775 if (__ratelimit(&drbd_ratelimit_state))
1776 dev_err(DEV, "Can not write resync data to local disk.\n");
1778 err = drbd_drain_block(mdev, pi->size);
1780 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
1783 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
1788 static void restart_conflicting_writes(struct drbd_conf *mdev,
1789 sector_t sector, int size)
1791 struct drbd_interval *i;
1792 struct drbd_request *req;
1794 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1797 req = container_of(i, struct drbd_request, i);
1798 if (req->rq_state & RQ_LOCAL_PENDING ||
1799 !(req->rq_state & RQ_POSTPONED))
1801 /* as it is RQ_POSTPONED, this will cause it to
1802 * be queued on the retry workqueue. */
1803 __req_mod(req, CONFLICT_RESOLVED, NULL);
1808 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
1810 static int e_end_block(struct drbd_work *w, int cancel)
1812 struct drbd_peer_request *peer_req =
1813 container_of(w, struct drbd_peer_request, w);
1814 struct drbd_conf *mdev = w->mdev;
1815 sector_t sector = peer_req->i.sector;
1818 if (peer_req->flags & EE_SEND_WRITE_ACK) {
1819 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1820 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1821 mdev->state.conn <= C_PAUSED_SYNC_T &&
1822 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
1823 P_RS_WRITE_ACK : P_WRITE_ACK;
1824 err = drbd_send_ack(mdev, pcmd, peer_req);
1825 if (pcmd == P_RS_WRITE_ACK)
1826 drbd_set_in_sync(mdev, sector, peer_req->i.size);
1828 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
1829 /* we expect it to be marked out of sync anyways...
1830 * maybe assert this? */
1834 /* we delete from the conflict detection hash _after_ we sent out the
1835 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
1836 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
1837 spin_lock_irq(&mdev->tconn->req_lock);
1838 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1839 drbd_remove_epoch_entry_interval(mdev, peer_req);
1840 if (peer_req->flags & EE_RESTART_REQUESTS)
1841 restart_conflicting_writes(mdev, sector, peer_req->i.size);
1842 spin_unlock_irq(&mdev->tconn->req_lock);
1844 D_ASSERT(drbd_interval_empty(&peer_req->i));
1846 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1851 static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
1853 struct drbd_conf *mdev = w->mdev;
1854 struct drbd_peer_request *peer_req =
1855 container_of(w, struct drbd_peer_request, w);
1858 err = drbd_send_ack(mdev, ack, peer_req);
1864 static int e_send_superseded(struct drbd_work *w, int unused)
1866 return e_send_ack(w, P_SUPERSEDED);
1869 static int e_send_retry_write(struct drbd_work *w, int unused)
1871 struct drbd_tconn *tconn = w->mdev->tconn;
1873 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1874 P_RETRY_WRITE : P_SUPERSEDED);
1877 static bool seq_greater(u32 a, u32 b)
1880 * We assume 32-bit wrap-around here.
1881 * For 24-bit wrap-around, we would have to shift:
1884 return (s32)a - (s32)b > 0;
1887 static u32 seq_max(u32 a, u32 b)
1889 return seq_greater(a, b) ? a : b;
1892 static bool need_peer_seq(struct drbd_conf *mdev)
1894 struct drbd_tconn *tconn = mdev->tconn;
1898 * We only need to keep track of the last packet_seq number of our peer
1899 * if we are in dual-primary mode and we have the resolve-conflicts flag set; see
1900 * handle_write_conflicts().
1904 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1907 return tp && test_bit(RESOLVE_CONFLICTS, &tconn->flags);
1910 static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
1912 unsigned int newest_peer_seq;
1914 if (need_peer_seq(mdev)) {
1915 spin_lock(&mdev->peer_seq_lock);
1916 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1917 mdev->peer_seq = newest_peer_seq;
1918 spin_unlock(&mdev->peer_seq_lock);
1919 /* wake up only if we actually changed mdev->peer_seq */
1920 if (peer_seq == newest_peer_seq)
1921 wake_up(&mdev->seq_wait);
1925 static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1927 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1930 /* maybe change sync_ee into interval trees as well? */
1931 static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
1933 struct drbd_peer_request *rs_req;
1936 spin_lock_irq(&mdev->tconn->req_lock);
1937 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1938 if (overlaps(peer_req->i.sector, peer_req->i.size,
1939 rs_req->i.sector, rs_req->i.size)) {
1944 spin_unlock_irq(&mdev->tconn->req_lock);
1949 /* Called from receive_Data.
1950 * Synchronize packets on sock with packets on msock.
1952 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1953 * packet traveling on msock, they are still processed in the order they have
1956 * Note: we don't care for Ack packets overtaking P_DATA packets.
1958 * In case packet_seq is larger than mdev->peer_seq number, there are
1959 * outstanding packets on the msock. We wait for them to arrive.
1960 * In case we are the logically next packet, we update mdev->peer_seq
1961 * ourselves. Correctly handles 32bit wrap around.
1963 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1964 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1965 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1966 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1968 * returns 0 if we may process the packet,
1969 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1970 static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
1976 if (!need_peer_seq(mdev))
1979 spin_lock(&mdev->peer_seq_lock);
1981 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1982 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1986 if (signal_pending(current)) {
1990 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1991 spin_unlock(&mdev->peer_seq_lock);
1993 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1995 timeout = schedule_timeout(timeout);
1996 spin_lock(&mdev->peer_seq_lock);
1999 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
2003 spin_unlock(&mdev->peer_seq_lock);
2004 finish_wait(&mdev->seq_wait, &wait);
2008 /* see also bio_flags_to_wire()
2009 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2010 * flags and back. We may replicate to other kernel versions. */
2011 static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
2013 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2014 (dpf & DP_FUA ? REQ_FUA : 0) |
2015 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2016 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
2019 static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2022 struct drbd_interval *i;
2025 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2026 struct drbd_request *req;
2027 struct bio_and_error m;
2031 req = container_of(i, struct drbd_request, i);
2032 if (!(req->rq_state & RQ_POSTPONED))
2034 req->rq_state &= ~RQ_POSTPONED;
2035 __req_mod(req, NEG_ACKED, &m);
2036 spin_unlock_irq(&mdev->tconn->req_lock);
2038 complete_master_bio(mdev, &m);
2039 spin_lock_irq(&mdev->tconn->req_lock);
2044 static int handle_write_conflicts(struct drbd_conf *mdev,
2045 struct drbd_peer_request *peer_req)
2047 struct drbd_tconn *tconn = mdev->tconn;
2048 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &tconn->flags);
2049 sector_t sector = peer_req->i.sector;
2050 const unsigned int size = peer_req->i.size;
2051 struct drbd_interval *i;
2056 * Inserting the peer request into the write_requests tree will prevent
2057 * new conflicting local requests from being added.
2059 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2062 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2063 if (i == &peer_req->i)
2068 * Our peer has sent a conflicting remote request; this
2069 * should not happen in a two-node setup. Wait for the
2070 * earlier peer request to complete.
2072 err = drbd_wait_misc(mdev, i);
2078 equal = i->sector == sector && i->size == size;
2079 if (resolve_conflicts) {
2081 * If the peer request is fully contained within the
2082 * overlapping request, it can be considered overwritten
2083 * and thus superseded; otherwise, it will be retried
2084 * once all overlapping requests have completed.
2086 bool superseded = i->sector <= sector && i->sector +
2087 (i->size >> 9) >= sector + (size >> 9);
2090 dev_alert(DEV, "Concurrent writes detected: "
2091 "local=%llus +%u, remote=%llus +%u, "
2092 "assuming %s came first\n",
2093 (unsigned long long)i->sector, i->size,
2094 (unsigned long long)sector, size,
2095 superseded ? "local" : "remote");
2098 peer_req->w.cb = superseded ? e_send_superseded :
2100 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2101 wake_asender(mdev->tconn);
2106 struct drbd_request *req =
2107 container_of(i, struct drbd_request, i);
2110 dev_alert(DEV, "Concurrent writes detected: "
2111 "local=%llus +%u, remote=%llus +%u\n",
2112 (unsigned long long)i->sector, i->size,
2113 (unsigned long long)sector, size);
2115 if (req->rq_state & RQ_LOCAL_PENDING ||
2116 !(req->rq_state & RQ_POSTPONED)) {
2118 * Wait for the node with the discard flag to
2119 * decide if this request has been superseded
2120 * or needs to be retried.
2121 * Requests that have been superseded will
2122 * disappear from the write_requests tree.
2124 * In addition, wait for the conflicting
2125 * request to finish locally before submitting
2126 * the conflicting peer request.
2128 err = drbd_wait_misc(mdev, &req->i);
2130 _conn_request_state(mdev->tconn,
2131 NS(conn, C_TIMEOUT),
2133 fail_postponed_requests(mdev, sector, size);
2139 * Remember to restart the conflicting requests after
2140 * the new peer request has completed.
2142 peer_req->flags |= EE_RESTART_REQUESTS;
2149 drbd_remove_epoch_entry_interval(mdev, peer_req);
2153 /* mirrored write */
2154 static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
2156 struct drbd_conf *mdev;
2158 struct drbd_peer_request *peer_req;
2159 struct p_data *p = pi->data;
2160 u32 peer_seq = be32_to_cpu(p->seq_num);
2165 mdev = vnr_to_mdev(tconn, pi->vnr);
2169 if (!get_ldev(mdev)) {
2172 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2173 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
2174 atomic_inc(&tconn->current_epoch->epoch_size);
2175 err2 = drbd_drain_block(mdev, pi->size);
2182 * Corresponding put_ldev done either below (on various errors), or in
2183 * drbd_peer_request_endio, if we successfully submit the data at the
2184 * end of this function.
2187 sector = be64_to_cpu(p->sector);
2188 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
2194 peer_req->w.cb = e_end_block;
2196 dp_flags = be32_to_cpu(p->dp_flags);
2197 rw |= wire_flags_to_bio(mdev, dp_flags);
2198 if (peer_req->pages == NULL) {
2199 D_ASSERT(peer_req->i.size == 0);
2200 D_ASSERT(dp_flags & DP_FLUSH);
2203 if (dp_flags & DP_MAY_SET_IN_SYNC)
2204 peer_req->flags |= EE_MAY_SET_IN_SYNC;
2206 spin_lock(&tconn->epoch_lock);
2207 peer_req->epoch = tconn->current_epoch;
2208 atomic_inc(&peer_req->epoch->epoch_size);
2209 atomic_inc(&peer_req->epoch->active);
2210 spin_unlock(&tconn->epoch_lock);
2213 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2216 peer_req->flags |= EE_IN_INTERVAL_TREE;
2217 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2219 goto out_interrupted;
2220 spin_lock_irq(&mdev->tconn->req_lock);
2221 err = handle_write_conflicts(mdev, peer_req);
2223 spin_unlock_irq(&mdev->tconn->req_lock);
2224 if (err == -ENOENT) {
2228 goto out_interrupted;
2231 spin_lock_irq(&mdev->tconn->req_lock);
2232 list_add(&peer_req->w.list, &mdev->active_ee);
2233 spin_unlock_irq(&mdev->tconn->req_lock);
2235 if (mdev->state.conn == C_SYNC_TARGET)
2236 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
2238 if (mdev->tconn->agreed_pro_version < 100) {
2240 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
2242 dp_flags |= DP_SEND_WRITE_ACK;
2245 dp_flags |= DP_SEND_RECEIVE_ACK;
2251 if (dp_flags & DP_SEND_WRITE_ACK) {
2252 peer_req->flags |= EE_SEND_WRITE_ACK;
2254 /* corresponding dec_unacked() in e_end_block()
2255 * respective _drbd_clear_done_ee */
2258 if (dp_flags & DP_SEND_RECEIVE_ACK) {
2259 /* I really don't like it that the receiver thread
2260 * sends on the msock, but anyways */
2261 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
2264 if (mdev->state.pdsk < D_INCONSISTENT) {
2265 /* In case we have the only disk of the cluster, */
2266 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2267 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2268 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
2269 drbd_al_begin_io(mdev, &peer_req->i);
2272 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2276 /* don't care for the reason here */
2277 dev_err(DEV, "submit failed, triggering re-connect\n");
2278 spin_lock_irq(&mdev->tconn->req_lock);
2279 list_del(&peer_req->w.list);
2280 drbd_remove_epoch_entry_interval(mdev, peer_req);
2281 spin_unlock_irq(&mdev->tconn->req_lock);
2282 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
2283 drbd_al_complete_io(mdev, &peer_req->i);
2286 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
2288 drbd_free_peer_req(mdev, peer_req);
2292 /* We may throttle resync, if the lower device seems to be busy,
2293 * and current sync rate is above c_min_rate.
2295 * To decide whether or not the lower device is busy, we use a scheme similar
2296 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2297 * (more than 64 sectors) of activity we cannot account for with our own resync
2298 * activity, it obviously is "busy".
2300 * The current sync rate used here uses only the most recent two step marks,
2301 * to have a short time average so we can react faster.
2303 int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
2305 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2306 unsigned long db, dt, dbdt;
2307 struct lc_element *tmp;
2310 unsigned int c_min_rate;
2313 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2316 /* feature disabled? */
2317 if (c_min_rate == 0)
2320 spin_lock_irq(&mdev->al_lock);
2321 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2323 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2324 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2325 spin_unlock_irq(&mdev->al_lock);
2328 /* Do not slow down if app IO is already waiting for this extent */
2330 spin_unlock_irq(&mdev->al_lock);
2332 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2333 (int)part_stat_read(&disk->part0, sectors[1]) -
2334 atomic_read(&mdev->rs_sect_ev);
2336 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2337 unsigned long rs_left;
2340 mdev->rs_last_events = curr_events;
2342 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2344 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2346 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2347 rs_left = mdev->ov_left;
2349 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
2351 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2354 db = mdev->rs_mark_left[i] - rs_left;
2355 dbdt = Bit2KB(db/dt);
2357 if (dbdt > c_min_rate)
2364 static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
2366 struct drbd_conf *mdev;
2369 struct drbd_peer_request *peer_req;
2370 struct digest_info *di = NULL;
2372 unsigned int fault_type;
2373 struct p_block_req *p = pi->data;
2375 mdev = vnr_to_mdev(tconn, pi->vnr);
2378 capacity = drbd_get_capacity(mdev->this_bdev);
2380 sector = be64_to_cpu(p->sector);
2381 size = be32_to_cpu(p->blksize);
2383 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
2384 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2385 (unsigned long long)sector, size);
2388 if (sector + (size>>9) > capacity) {
2389 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2390 (unsigned long long)sector, size);
2394 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
2397 case P_DATA_REQUEST:
2398 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2400 case P_RS_DATA_REQUEST:
2401 case P_CSUM_RS_REQUEST:
2403 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2407 dec_rs_pending(mdev);
2408 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2413 if (verb && __ratelimit(&drbd_ratelimit_state))
2414 dev_err(DEV, "Can not satisfy peer's read request, "
2415 "no local data.\n");
2417 /* drain possibly payload */
2418 return drbd_drain_block(mdev, pi->size);
2421 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2422 * "criss-cross" setup, that might cause write-out on some other DRBD,
2423 * which in turn might block on the other node at this very place. */
2424 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
2431 case P_DATA_REQUEST:
2432 peer_req->w.cb = w_e_end_data_req;
2433 fault_type = DRBD_FAULT_DT_RD;
2434 /* application IO, don't drbd_rs_begin_io */
2437 case P_RS_DATA_REQUEST:
2438 peer_req->w.cb = w_e_end_rsdata_req;
2439 fault_type = DRBD_FAULT_RS_RD;
2440 /* used in the sector offset progress display */
2441 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
2445 case P_CSUM_RS_REQUEST:
2446 fault_type = DRBD_FAULT_RS_RD;
2447 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
2451 di->digest_size = pi->size;
2452 di->digest = (((char *)di)+sizeof(struct digest_info));
2454 peer_req->digest = di;
2455 peer_req->flags |= EE_HAS_DIGEST;
2457 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
2460 if (pi->cmd == P_CSUM_RS_REQUEST) {
2461 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
2462 peer_req->w.cb = w_e_end_csum_rs_req;
2463 /* used in the sector offset progress display */
2464 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
2465 } else if (pi->cmd == P_OV_REPLY) {
2466 /* track progress, we may need to throttle */
2467 atomic_add(size >> 9, &mdev->rs_sect_in);
2468 peer_req->w.cb = w_e_end_ov_reply;
2469 dec_rs_pending(mdev);
2470 /* drbd_rs_begin_io done when we sent this request,
2471 * but accounting still needs to be done. */
2472 goto submit_for_resync;
2477 if (mdev->ov_start_sector == ~(sector_t)0 &&
2478 mdev->tconn->agreed_pro_version >= 90) {
2479 unsigned long now = jiffies;
2481 mdev->ov_start_sector = sector;
2482 mdev->ov_position = sector;
2483 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2484 mdev->rs_total = mdev->ov_left;
2485 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2486 mdev->rs_mark_left[i] = mdev->ov_left;
2487 mdev->rs_mark_time[i] = now;
2489 dev_info(DEV, "Online Verify start sector: %llu\n",
2490 (unsigned long long)sector);
2492 peer_req->w.cb = w_e_end_ov_req;
2493 fault_type = DRBD_FAULT_RS_RD;
2500 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2501 * wrt the receiver, but it is not as straightforward as it may seem.
2502 * Various places in the resync start and stop logic assume resync
2503 * requests are processed in order, requeuing this on the worker thread
2504 * introduces a bunch of new code for synchronization between threads.
2506 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2507 * "forever", throttling after drbd_rs_begin_io will lock that extent
2508 * for application writes for the same time. For now, just throttle
2509 * here, where the rest of the code expects the receiver to sleep for
2513 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2514 * this defers syncer requests for some time, before letting at least
2515 * on request through. The resync controller on the receiving side
2516 * will adapt to the incoming rate accordingly.
2518 * We cannot throttle here if remote is Primary/SyncTarget:
2519 * we would also throttle its application reads.
2520 * In that case, throttling is done on the SyncTarget only.
2522 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2523 schedule_timeout_uninterruptible(HZ/10);
2524 if (drbd_rs_begin_io(mdev, sector))
2528 atomic_add(size >> 9, &mdev->rs_sect_ev);
2532 spin_lock_irq(&mdev->tconn->req_lock);
2533 list_add_tail(&peer_req->w.list, &mdev->read_ee);
2534 spin_unlock_irq(&mdev->tconn->req_lock);
2536 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
2539 /* don't care for the reason here */
2540 dev_err(DEV, "submit failed, triggering re-connect\n");
2541 spin_lock_irq(&mdev->tconn->req_lock);
2542 list_del(&peer_req->w.list);
2543 spin_unlock_irq(&mdev->tconn->req_lock);
2544 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2548 drbd_free_peer_req(mdev, peer_req);
2552 static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2554 int self, peer, rv = -100;
2555 unsigned long ch_self, ch_peer;
2556 enum drbd_after_sb_p after_sb_0p;
2558 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2559 peer = mdev->p_uuid[UI_BITMAP] & 1;
2561 ch_peer = mdev->p_uuid[UI_SIZE];
2562 ch_self = mdev->comm_bm_set;
2565 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2567 switch (after_sb_0p) {
2569 case ASB_DISCARD_SECONDARY:
2570 case ASB_CALL_HELPER:
2572 dev_err(DEV, "Configuration error.\n");
2574 case ASB_DISCONNECT:
2576 case ASB_DISCARD_YOUNGER_PRI:
2577 if (self == 0 && peer == 1) {
2581 if (self == 1 && peer == 0) {
2585 /* Else fall through to one of the other strategies... */
2586 case ASB_DISCARD_OLDER_PRI:
2587 if (self == 0 && peer == 1) {
2591 if (self == 1 && peer == 0) {
2595 /* Else fall through to one of the other strategies... */
2596 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
2597 "Using discard-least-changes instead\n");
2598 case ASB_DISCARD_ZERO_CHG:
2599 if (ch_peer == 0 && ch_self == 0) {
2600 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
2604 if (ch_peer == 0) { rv = 1; break; }
2605 if (ch_self == 0) { rv = -1; break; }
2607 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
2609 case ASB_DISCARD_LEAST_CHG:
2610 if (ch_self < ch_peer)
2612 else if (ch_self > ch_peer)
2614 else /* ( ch_self == ch_peer ) */
2615 /* Well, then use something else. */
2616 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
2619 case ASB_DISCARD_LOCAL:
2622 case ASB_DISCARD_REMOTE:
2629 static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2632 enum drbd_after_sb_p after_sb_1p;
2635 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2637 switch (after_sb_1p) {
2638 case ASB_DISCARD_YOUNGER_PRI:
2639 case ASB_DISCARD_OLDER_PRI:
2640 case ASB_DISCARD_LEAST_CHG:
2641 case ASB_DISCARD_LOCAL:
2642 case ASB_DISCARD_REMOTE:
2643 case ASB_DISCARD_ZERO_CHG:
2644 dev_err(DEV, "Configuration error.\n");
2646 case ASB_DISCONNECT:
2649 hg = drbd_asb_recover_0p(mdev);
2650 if (hg == -1 && mdev->state.role == R_SECONDARY)
2652 if (hg == 1 && mdev->state.role == R_PRIMARY)
2656 rv = drbd_asb_recover_0p(mdev);
2658 case ASB_DISCARD_SECONDARY:
2659 return mdev->state.role == R_PRIMARY ? 1 : -1;
2660 case ASB_CALL_HELPER:
2661 hg = drbd_asb_recover_0p(mdev);
2662 if (hg == -1 && mdev->state.role == R_PRIMARY) {
2663 enum drbd_state_rv rv2;
2665 drbd_set_role(mdev, R_SECONDARY, 0);
2666 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2667 * we might be here in C_WF_REPORT_PARAMS which is transient.
2668 * we do not need to wait for the after state change work either. */
2669 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2670 if (rv2 != SS_SUCCESS) {
2671 drbd_khelper(mdev, "pri-lost-after-sb");
2673 dev_warn(DEV, "Successfully gave up primary role.\n");
2683 static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2686 enum drbd_after_sb_p after_sb_2p;
2689 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2691 switch (after_sb_2p) {
2692 case ASB_DISCARD_YOUNGER_PRI:
2693 case ASB_DISCARD_OLDER_PRI:
2694 case ASB_DISCARD_LEAST_CHG:
2695 case ASB_DISCARD_LOCAL:
2696 case ASB_DISCARD_REMOTE:
2698 case ASB_DISCARD_SECONDARY:
2699 case ASB_DISCARD_ZERO_CHG:
2700 dev_err(DEV, "Configuration error.\n");
2703 rv = drbd_asb_recover_0p(mdev);
2705 case ASB_DISCONNECT:
2707 case ASB_CALL_HELPER:
2708 hg = drbd_asb_recover_0p(mdev);
2710 enum drbd_state_rv rv2;
2712 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2713 * we might be here in C_WF_REPORT_PARAMS which is transient.
2714 * we do not need to wait for the after state change work either. */
2715 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2716 if (rv2 != SS_SUCCESS) {
2717 drbd_khelper(mdev, "pri-lost-after-sb");
2719 dev_warn(DEV, "Successfully gave up primary role.\n");
2729 static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2730 u64 bits, u64 flags)
2733 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2736 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2738 (unsigned long long)uuid[UI_CURRENT],
2739 (unsigned long long)uuid[UI_BITMAP],
2740 (unsigned long long)uuid[UI_HISTORY_START],
2741 (unsigned long long)uuid[UI_HISTORY_END],
2742 (unsigned long long)bits,
2743 (unsigned long long)flags);
2747 100 after split brain try auto recover
2748 2 C_SYNC_SOURCE set BitMap
2749 1 C_SYNC_SOURCE use BitMap
2751 -1 C_SYNC_TARGET use BitMap
2752 -2 C_SYNC_TARGET set BitMap
2753 -100 after split brain, disconnect
2754 -1000 unrelated data
2755 -1091 requires proto 91
2756 -1096 requires proto 96
2758 static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2763 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2764 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2767 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2771 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2772 peer != UUID_JUST_CREATED)
2776 if (self != UUID_JUST_CREATED &&
2777 (peer == UUID_JUST_CREATED || peer == (u64)0))
2781 int rct, dc; /* roles at crash time */
2783 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2785 if (mdev->tconn->agreed_pro_version < 91)
2788 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2789 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2790 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2791 drbd_uuid_move_history(mdev);
2792 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2793 mdev->ldev->md.uuid[UI_BITMAP] = 0;
2795 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2796 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2799 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2806 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2808 if (mdev->tconn->agreed_pro_version < 91)
2811 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2812 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2813 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2815 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2816 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2817 mdev->p_uuid[UI_BITMAP] = 0UL;
2819 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2822 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2829 /* Common power [off|failure] */
2830 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2831 (mdev->p_uuid[UI_FLAGS] & 2);
2832 /* lowest bit is set when we were primary,
2833 * next bit (weight 2) is set when peer was primary */
2837 case 0: /* !self_pri && !peer_pri */ return 0;
2838 case 1: /* self_pri && !peer_pri */ return 1;
2839 case 2: /* !self_pri && peer_pri */ return -1;
2840 case 3: /* self_pri && peer_pri */
2841 dc = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
2847 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2852 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2854 if (mdev->tconn->agreed_pro_version < 96 ?
2855 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2856 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2857 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
2858 /* The last P_SYNC_UUID did not get though. Undo the last start of
2859 resync as sync source modifications of the peer's UUIDs. */
2861 if (mdev->tconn->agreed_pro_version < 91)
2864 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2865 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
2867 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
2868 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2875 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2876 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2877 peer = mdev->p_uuid[i] & ~((u64)1);
2883 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2884 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2889 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2891 if (mdev->tconn->agreed_pro_version < 96 ?
2892 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2893 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2894 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
2895 /* The last P_SYNC_UUID did not get though. Undo the last start of
2896 resync as sync source modifications of our UUIDs. */
2898 if (mdev->tconn->agreed_pro_version < 91)
2901 __drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2902 __drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2904 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
2905 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2906 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2914 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2915 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2916 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2922 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2923 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2924 if (self == peer && self != ((u64)0))
2928 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2929 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2930 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2931 peer = mdev->p_uuid[j] & ~((u64)1);
2940 /* drbd_sync_handshake() returns the new conn state on success, or
2941 CONN_MASK (-1) on failure.
2943 static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2944 enum drbd_disk_state peer_disk) __must_hold(local)
2946 enum drbd_conns rv = C_MASK;
2947 enum drbd_disk_state mydisk;
2948 struct net_conf *nc;
2949 int hg, rule_nr, rr_conflict, tentative;
2951 mydisk = mdev->state.disk;
2952 if (mydisk == D_NEGOTIATING)
2953 mydisk = mdev->new_state_tmp.disk;
2955 dev_info(DEV, "drbd_sync_handshake:\n");
2957 spin_lock_irq(&mdev->ldev->md.uuid_lock);
2958 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2959 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2960 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2962 hg = drbd_uuid_compare(mdev, &rule_nr);
2963 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
2965 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2968 dev_alert(DEV, "Unrelated data, aborting!\n");
2972 dev_alert(DEV, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000);
2976 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2977 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2978 int f = (hg == -100) || abs(hg) == 2;
2979 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2982 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2983 hg > 0 ? "source" : "target");
2987 drbd_khelper(mdev, "initial-split-brain");
2990 nc = rcu_dereference(mdev->tconn->net_conf);
2992 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
2993 int pcount = (mdev->state.role == R_PRIMARY)
2994 + (peer_role == R_PRIMARY);
2995 int forced = (hg == -100);
2999 hg = drbd_asb_recover_0p(mdev);
3002 hg = drbd_asb_recover_1p(mdev);
3005 hg = drbd_asb_recover_2p(mdev);
3008 if (abs(hg) < 100) {
3009 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3010 "automatically solved. Sync from %s node\n",
3011 pcount, (hg < 0) ? "peer" : "this");
3013 dev_warn(DEV, "Doing a full sync, since"
3014 " UUIDs where ambiguous.\n");
3021 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
3023 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
3027 dev_warn(DEV, "Split-Brain detected, manually solved. "
3028 "Sync from %s node\n",
3029 (hg < 0) ? "peer" : "this");
3031 rr_conflict = nc->rr_conflict;
3032 tentative = nc->tentative;
3036 /* FIXME this log message is not correct if we end up here
3037 * after an attempted attach on a diskless node.
3038 * We just refuse to attach -- well, we drop the "connection"
3039 * to that disk, in a way... */
3040 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
3041 drbd_khelper(mdev, "split-brain");
3045 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3046 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3050 if (hg < 0 && /* by intention we do not use mydisk here. */
3051 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
3052 switch (rr_conflict) {
3053 case ASB_CALL_HELPER:
3054 drbd_khelper(mdev, "pri-lost");
3056 case ASB_DISCONNECT:
3057 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3060 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3065 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
3067 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3069 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3070 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3071 abs(hg) >= 2 ? "full" : "bit-map based");
3076 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
3077 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3078 BM_LOCKED_SET_ALLOWED))
3082 if (hg > 0) { /* become sync source. */
3084 } else if (hg < 0) { /* become sync target */
3088 if (drbd_bm_total_weight(mdev)) {
3089 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3090 drbd_bm_total_weight(mdev));
3097 static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
3099 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
3100 if (peer == ASB_DISCARD_REMOTE)
3101 return ASB_DISCARD_LOCAL;
3103 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
3104 if (peer == ASB_DISCARD_LOCAL)
3105 return ASB_DISCARD_REMOTE;
3107 /* everything else is valid if they are equal on both sides. */
3111 static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
3113 struct p_protocol *p = pi->data;
3114 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3115 int p_proto, p_discard_my_data, p_two_primaries, cf;
3116 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3117 char integrity_alg[SHARED_SECRET_MAX] = "";
3118 struct crypto_hash *peer_integrity_tfm = NULL;
3119 void *int_dig_in = NULL, *int_dig_vv = NULL;
3121 p_proto = be32_to_cpu(p->protocol);
3122 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3123 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3124 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
3125 p_two_primaries = be32_to_cpu(p->two_primaries);
3126 cf = be32_to_cpu(p->conn_flags);
3127 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
3129 if (tconn->agreed_pro_version >= 87) {
3132 if (pi->size > sizeof(integrity_alg))
3134 err = drbd_recv_all(tconn, integrity_alg, pi->size);
3137 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3140 if (pi->cmd != P_PROTOCOL_UPDATE) {
3141 clear_bit(CONN_DRY_RUN, &tconn->flags);
3143 if (cf & CF_DRY_RUN)
3144 set_bit(CONN_DRY_RUN, &tconn->flags);
3147 nc = rcu_dereference(tconn->net_conf);
3149 if (p_proto != nc->wire_protocol) {
3150 conn_err(tconn, "incompatible %s settings\n", "protocol");
3151 goto disconnect_rcu_unlock;
3154 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
3155 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
3156 goto disconnect_rcu_unlock;
3159 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
3160 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
3161 goto disconnect_rcu_unlock;
3164 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
3165 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
3166 goto disconnect_rcu_unlock;
3169 if (p_discard_my_data && nc->discard_my_data) {
3170 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
3171 goto disconnect_rcu_unlock;
3174 if (p_two_primaries != nc->two_primaries) {
3175 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
3176 goto disconnect_rcu_unlock;
3179 if (strcmp(integrity_alg, nc->integrity_alg)) {
3180 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
3181 goto disconnect_rcu_unlock;
3187 if (integrity_alg[0]) {
3191 * We can only change the peer data integrity algorithm
3192 * here. Changing our own data integrity algorithm
3193 * requires that we send a P_PROTOCOL_UPDATE packet at
3194 * the same time; otherwise, the peer has no way to
3195 * tell between which packets the algorithm should
3199 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3200 if (!peer_integrity_tfm) {
3201 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3206 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3207 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3208 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3209 if (!(int_dig_in && int_dig_vv)) {
3210 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3215 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3216 if (!new_net_conf) {
3217 conn_err(tconn, "Allocation of new net_conf failed\n");
3221 mutex_lock(&tconn->data.mutex);
3222 mutex_lock(&tconn->conf_update);
3223 old_net_conf = tconn->net_conf;
3224 *new_net_conf = *old_net_conf;
3226 new_net_conf->wire_protocol = p_proto;
3227 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3228 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3229 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3230 new_net_conf->two_primaries = p_two_primaries;
3232 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3233 mutex_unlock(&tconn->conf_update);
3234 mutex_unlock(&tconn->data.mutex);
3236 crypto_free_hash(tconn->peer_integrity_tfm);
3237 kfree(tconn->int_dig_in);
3238 kfree(tconn->int_dig_vv);
3239 tconn->peer_integrity_tfm = peer_integrity_tfm;
3240 tconn->int_dig_in = int_dig_in;
3241 tconn->int_dig_vv = int_dig_vv;
3243 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3244 conn_info(tconn, "peer data-integrity-alg: %s\n",
3245 integrity_alg[0] ? integrity_alg : "(none)");
3248 kfree(old_net_conf);
3251 disconnect_rcu_unlock:
3254 crypto_free_hash(peer_integrity_tfm);
3257 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3262 * input: alg name, feature name
3263 * return: NULL (alg name was "")
3264 * ERR_PTR(error) if something goes wrong
3265 * or the crypto hash ptr, if it worked out ok. */
3266 struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3267 const char *alg, const char *name)
3269 struct crypto_hash *tfm;
3274 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3276 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3277 alg, name, PTR_ERR(tfm));
3283 static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
3285 void *buffer = tconn->data.rbuf;
3286 int size = pi->size;
3289 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3290 s = drbd_recv(tconn, buffer, s);
3304 * config_unknown_volume - device configuration command for unknown volume
3306 * When a device is added to an existing connection, the node on which the
3307 * device is added first will send configuration commands to its peer but the
3308 * peer will not know about the device yet. It will warn and ignore these
3309 * commands. Once the device is added on the second node, the second node will
3310 * send the same device configuration commands, but in the other direction.
3312 * (We can also end up here if drbd is misconfigured.)
3314 static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3316 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3317 cmdname(pi->cmd), pi->vnr);
3318 return ignore_remaining_packet(tconn, pi);
3321 static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3323 struct drbd_conf *mdev;
3324 struct p_rs_param_95 *p;
3325 unsigned int header_size, data_size, exp_max_sz;
3326 struct crypto_hash *verify_tfm = NULL;
3327 struct crypto_hash *csums_tfm = NULL;
3328 struct net_conf *old_net_conf, *new_net_conf = NULL;
3329 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
3330 const int apv = tconn->agreed_pro_version;
3331 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
3335 mdev = vnr_to_mdev(tconn, pi->vnr);
3337 return config_unknown_volume(tconn, pi);
3339 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3340 : apv == 88 ? sizeof(struct p_rs_param)
3342 : apv <= 94 ? sizeof(struct p_rs_param_89)
3343 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
3345 if (pi->size > exp_max_sz) {
3346 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
3347 pi->size, exp_max_sz);
3352 header_size = sizeof(struct p_rs_param);
3353 data_size = pi->size - header_size;
3354 } else if (apv <= 94) {
3355 header_size = sizeof(struct p_rs_param_89);
3356 data_size = pi->size - header_size;
3357 D_ASSERT(data_size == 0);
3359 header_size = sizeof(struct p_rs_param_95);
3360 data_size = pi->size - header_size;
3361 D_ASSERT(data_size == 0);
3364 /* initialize verify_alg and csums_alg */
3366 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3368 err = drbd_recv_all(mdev->tconn, p, header_size);
3372 mutex_lock(&mdev->tconn->conf_update);
3373 old_net_conf = mdev->tconn->net_conf;
3374 if (get_ldev(mdev)) {
3375 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3376 if (!new_disk_conf) {
3378 mutex_unlock(&mdev->tconn->conf_update);
3379 dev_err(DEV, "Allocation of new disk_conf failed\n");
3383 old_disk_conf = mdev->ldev->disk_conf;
3384 *new_disk_conf = *old_disk_conf;
3386 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
3391 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3392 dev_err(DEV, "verify-alg of wrong size, "
3393 "peer wants %u, accepting only up to %u byte\n",
3394 data_size, SHARED_SECRET_MAX);
3399 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
3402 /* we expect NUL terminated string */
3403 /* but just in case someone tries to be evil */
3404 D_ASSERT(p->verify_alg[data_size-1] == 0);
3405 p->verify_alg[data_size-1] = 0;
3407 } else /* apv >= 89 */ {
3408 /* we still expect NUL terminated strings */
3409 /* but just in case someone tries to be evil */
3410 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3411 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3412 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3413 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3416 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
3417 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3418 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
3419 old_net_conf->verify_alg, p->verify_alg);
3422 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3423 p->verify_alg, "verify-alg");
3424 if (IS_ERR(verify_tfm)) {
3430 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
3431 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3432 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
3433 old_net_conf->csums_alg, p->csums_alg);
3436 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3437 p->csums_alg, "csums-alg");
3438 if (IS_ERR(csums_tfm)) {
3444 if (apv > 94 && new_disk_conf) {
3445 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3446 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3447 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3448 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
3450 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
3451 if (fifo_size != mdev->rs_plan_s->size) {
3452 new_plan = fifo_alloc(fifo_size);
3454 dev_err(DEV, "kmalloc of fifo_buffer failed");
3461 if (verify_tfm || csums_tfm) {
3462 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3463 if (!new_net_conf) {
3464 dev_err(DEV, "Allocation of new net_conf failed\n");
3468 *new_net_conf = *old_net_conf;
3471 strcpy(new_net_conf->verify_alg, p->verify_alg);
3472 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3473 crypto_free_hash(mdev->tconn->verify_tfm);
3474 mdev->tconn->verify_tfm = verify_tfm;
3475 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3478 strcpy(new_net_conf->csums_alg, p->csums_alg);
3479 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3480 crypto_free_hash(mdev->tconn->csums_tfm);
3481 mdev->tconn->csums_tfm = csums_tfm;
3482 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3484 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3488 if (new_disk_conf) {
3489 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3494 old_plan = mdev->rs_plan_s;
3495 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3498 mutex_unlock(&mdev->tconn->conf_update);
3501 kfree(old_net_conf);
3502 kfree(old_disk_conf);
3508 if (new_disk_conf) {
3510 kfree(new_disk_conf);
3512 mutex_unlock(&mdev->tconn->conf_update);
3517 if (new_disk_conf) {
3519 kfree(new_disk_conf);
3521 mutex_unlock(&mdev->tconn->conf_update);
3522 /* just for completeness: actually not needed,
3523 * as this is not reached if csums_tfm was ok. */
3524 crypto_free_hash(csums_tfm);
3525 /* but free the verify_tfm again, if csums_tfm did not work out */
3526 crypto_free_hash(verify_tfm);
3527 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3531 /* warn if the arguments differ by more than 12.5% */
3532 static void warn_if_differ_considerably(struct drbd_conf *mdev,
3533 const char *s, sector_t a, sector_t b)
3536 if (a == 0 || b == 0)
3538 d = (a > b) ? (a - b) : (b - a);
3539 if (d > (a>>3) || d > (b>>3))
3540 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3541 (unsigned long long)a, (unsigned long long)b);
3544 static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
3546 struct drbd_conf *mdev;
3547 struct p_sizes *p = pi->data;
3548 enum determine_dev_size dd = unchanged;
3549 sector_t p_size, p_usize, my_usize;
3550 int ldsc = 0; /* local disk size changed */
3551 enum dds_flags ddsf;
3553 mdev = vnr_to_mdev(tconn, pi->vnr);
3555 return config_unknown_volume(tconn, pi);
3557 p_size = be64_to_cpu(p->d_size);
3558 p_usize = be64_to_cpu(p->u_size);
3560 /* just store the peer's disk size for now.
3561 * we still need to figure out whether we accept that. */
3562 mdev->p_size = p_size;
3564 if (get_ldev(mdev)) {
3566 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3569 warn_if_differ_considerably(mdev, "lower level device sizes",
3570 p_size, drbd_get_max_capacity(mdev->ldev));
3571 warn_if_differ_considerably(mdev, "user requested size",
3574 /* if this is the first connect, or an otherwise expected
3575 * param exchange, choose the minimum */
3576 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3577 p_usize = min_not_zero(my_usize, p_usize);
3579 /* Never shrink a device with usable data during connect.
3580 But allow online shrinking if we are connected. */
3581 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
3582 drbd_get_capacity(mdev->this_bdev) &&
3583 mdev->state.disk >= D_OUTDATED &&
3584 mdev->state.conn < C_CONNECTED) {
3585 dev_err(DEV, "The peer's disk size is too small!\n");
3586 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3591 if (my_usize != p_usize) {
3592 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3594 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3595 if (!new_disk_conf) {
3596 dev_err(DEV, "Allocation of new disk_conf failed\n");
3601 mutex_lock(&mdev->tconn->conf_update);
3602 old_disk_conf = mdev->ldev->disk_conf;
3603 *new_disk_conf = *old_disk_conf;
3604 new_disk_conf->disk_size = p_usize;
3606 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3607 mutex_unlock(&mdev->tconn->conf_update);
3609 kfree(old_disk_conf);
3611 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3612 (unsigned long)my_usize);
3618 ddsf = be16_to_cpu(p->dds_flags);
3619 if (get_ldev(mdev)) {
3620 dd = drbd_determine_dev_size(mdev, ddsf);
3622 if (dd == dev_size_error)
3626 /* I am diskless, need to accept the peer's size. */
3627 drbd_set_my_capacity(mdev, p_size);
3630 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3631 drbd_reconsider_max_bio_size(mdev);
3633 if (get_ldev(mdev)) {
3634 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3635 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3642 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3643 if (be64_to_cpu(p->c_size) !=
3644 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3645 /* we have different sizes, probably peer
3646 * needs to know my new size... */
3647 drbd_send_sizes(mdev, 0, ddsf);
3649 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3650 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3651 if (mdev->state.pdsk >= D_INCONSISTENT &&
3652 mdev->state.disk >= D_INCONSISTENT) {
3653 if (ddsf & DDSF_NO_RESYNC)
3654 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3656 resync_after_online_grow(mdev);
3658 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3665 static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
3667 struct drbd_conf *mdev;
3668 struct p_uuids *p = pi->data;
3670 int i, updated_uuids = 0;
3672 mdev = vnr_to_mdev(tconn, pi->vnr);
3674 return config_unknown_volume(tconn, pi);
3676 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3678 dev_err(DEV, "kmalloc of p_uuid failed\n");
3682 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3683 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3685 kfree(mdev->p_uuid);
3686 mdev->p_uuid = p_uuid;
3688 if (mdev->state.conn < C_CONNECTED &&
3689 mdev->state.disk < D_INCONSISTENT &&
3690 mdev->state.role == R_PRIMARY &&
3691 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3692 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3693 (unsigned long long)mdev->ed_uuid);
3694 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3698 if (get_ldev(mdev)) {
3699 int skip_initial_sync =
3700 mdev->state.conn == C_CONNECTED &&
3701 mdev->tconn->agreed_pro_version >= 90 &&
3702 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3703 (p_uuid[UI_FLAGS] & 8);
3704 if (skip_initial_sync) {
3705 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3706 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3707 "clear_n_write from receive_uuids",
3708 BM_LOCKED_TEST_ALLOWED);
3709 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3710 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3711 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3717 } else if (mdev->state.disk < D_INCONSISTENT &&
3718 mdev->state.role == R_PRIMARY) {
3719 /* I am a diskless primary, the peer just created a new current UUID
3721 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3724 /* Before we test for the disk state, we should wait until an eventually
3725 ongoing cluster wide state change is finished. That is important if
3726 we are primary and are detaching from our disk. We need to see the
3727 new disk state... */
3728 mutex_lock(mdev->state_mutex);
3729 mutex_unlock(mdev->state_mutex);
3730 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
3731 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3734 drbd_print_uuids(mdev, "receiver updated UUIDs to");
3740 * convert_state() - Converts the peer's view of the cluster state to our point of view
3741 * @ps: The state as seen by the peer.
3743 static union drbd_state convert_state(union drbd_state ps)
3745 union drbd_state ms;
3747 static enum drbd_conns c_tab[] = {
3748 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
3749 [C_CONNECTED] = C_CONNECTED,
3751 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3752 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3753 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3754 [C_VERIFY_S] = C_VERIFY_T,
3760 ms.conn = c_tab[ps.conn];
3765 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3770 static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
3772 struct drbd_conf *mdev;
3773 struct p_req_state *p = pi->data;
3774 union drbd_state mask, val;
3775 enum drbd_state_rv rv;
3777 mdev = vnr_to_mdev(tconn, pi->vnr);
3781 mask.i = be32_to_cpu(p->mask);
3782 val.i = be32_to_cpu(p->val);
3784 if (test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags) &&
3785 mutex_is_locked(mdev->state_mutex)) {
3786 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
3790 mask = convert_state(mask);
3791 val = convert_state(val);
3793 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3794 drbd_send_sr_reply(mdev, rv);
3801 static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
3803 struct p_req_state *p = pi->data;
3804 union drbd_state mask, val;
3805 enum drbd_state_rv rv;
3807 mask.i = be32_to_cpu(p->mask);
3808 val.i = be32_to_cpu(p->val);
3810 if (test_bit(RESOLVE_CONFLICTS, &tconn->flags) &&
3811 mutex_is_locked(&tconn->cstate_mutex)) {
3812 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
3816 mask = convert_state(mask);
3817 val = convert_state(val);
3819 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
3820 conn_send_sr_reply(tconn, rv);
3825 static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
3827 struct drbd_conf *mdev;
3828 struct p_state *p = pi->data;
3829 union drbd_state os, ns, peer_state;
3830 enum drbd_disk_state real_peer_disk;
3831 enum chg_state_flags cs_flags;
3834 mdev = vnr_to_mdev(tconn, pi->vnr);
3836 return config_unknown_volume(tconn, pi);
3838 peer_state.i = be32_to_cpu(p->state);
3840 real_peer_disk = peer_state.disk;
3841 if (peer_state.disk == D_NEGOTIATING) {
3842 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3843 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3846 spin_lock_irq(&mdev->tconn->req_lock);
3848 os = ns = drbd_read_state(mdev);
3849 spin_unlock_irq(&mdev->tconn->req_lock);
3851 /* If some other part of the code (asender thread, timeout)
3852 * already decided to close the connection again,
3853 * we must not "re-establish" it here. */
3854 if (os.conn <= C_TEAR_DOWN)
3857 /* If this is the "end of sync" confirmation, usually the peer disk
3858 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3859 * set) resync started in PausedSyncT, or if the timing of pause-/
3860 * unpause-sync events has been "just right", the peer disk may
3861 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3863 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3864 real_peer_disk == D_UP_TO_DATE &&
3865 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3866 /* If we are (becoming) SyncSource, but peer is still in sync
3867 * preparation, ignore its uptodate-ness to avoid flapping, it
3868 * will change to inconsistent once the peer reaches active
3870 * It may have changed syncer-paused flags, however, so we
3871 * cannot ignore this completely. */
3872 if (peer_state.conn > C_CONNECTED &&
3873 peer_state.conn < C_SYNC_SOURCE)
3874 real_peer_disk = D_INCONSISTENT;
3876 /* if peer_state changes to connected at the same time,
3877 * it explicitly notifies us that it finished resync.
3878 * Maybe we should finish it up, too? */
3879 else if (os.conn >= C_SYNC_SOURCE &&
3880 peer_state.conn == C_CONNECTED) {
3881 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3882 drbd_resync_finished(mdev);
3887 /* explicit verify finished notification, stop sector reached. */
3888 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
3889 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
3890 ov_out_of_sync_print(mdev);
3891 drbd_resync_finished(mdev);
3895 /* peer says his disk is inconsistent, while we think it is uptodate,
3896 * and this happens while the peer still thinks we have a sync going on,
3897 * but we think we are already done with the sync.
3898 * We ignore this to avoid flapping pdsk.
3899 * This should not happen, if the peer is a recent version of drbd. */
3900 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3901 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3902 real_peer_disk = D_UP_TO_DATE;
3904 if (ns.conn == C_WF_REPORT_PARAMS)
3905 ns.conn = C_CONNECTED;
3907 if (peer_state.conn == C_AHEAD)
3910 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3911 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3912 int cr; /* consider resync */
3914 /* if we established a new connection */
3915 cr = (os.conn < C_CONNECTED);
3916 /* if we had an established connection
3917 * and one of the nodes newly attaches a disk */
3918 cr |= (os.conn == C_CONNECTED &&
3919 (peer_state.disk == D_NEGOTIATING ||
3920 os.disk == D_NEGOTIATING));
3921 /* if we have both been inconsistent, and the peer has been
3922 * forced to be UpToDate with --overwrite-data */
3923 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3924 /* if we had been plain connected, and the admin requested to
3925 * start a sync by "invalidate" or "invalidate-remote" */
3926 cr |= (os.conn == C_CONNECTED &&
3927 (peer_state.conn >= C_STARTING_SYNC_S &&
3928 peer_state.conn <= C_WF_BITMAP_T));
3931 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
3934 if (ns.conn == C_MASK) {
3935 ns.conn = C_CONNECTED;
3936 if (mdev->state.disk == D_NEGOTIATING) {
3937 drbd_force_state(mdev, NS(disk, D_FAILED));
3938 } else if (peer_state.disk == D_NEGOTIATING) {
3939 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3940 peer_state.disk = D_DISKLESS;
3941 real_peer_disk = D_DISKLESS;
3943 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
3945 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
3946 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3952 spin_lock_irq(&mdev->tconn->req_lock);
3953 if (os.i != drbd_read_state(mdev).i)
3955 clear_bit(CONSIDER_RESYNC, &mdev->flags);
3956 ns.peer = peer_state.role;
3957 ns.pdsk = real_peer_disk;
3958 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
3959 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
3960 ns.disk = mdev->new_state_tmp.disk;
3961 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3962 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
3963 test_bit(NEW_CUR_UUID, &mdev->flags)) {
3964 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
3965 for temporal network outages! */
3966 spin_unlock_irq(&mdev->tconn->req_lock);
3967 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3968 tl_clear(mdev->tconn);
3969 drbd_uuid_new_current(mdev);
3970 clear_bit(NEW_CUR_UUID, &mdev->flags);
3971 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
3974 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
3975 ns = drbd_read_state(mdev);
3976 spin_unlock_irq(&mdev->tconn->req_lock);
3978 if (rv < SS_SUCCESS) {
3979 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
3983 if (os.conn > C_WF_REPORT_PARAMS) {
3984 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
3985 peer_state.disk != D_NEGOTIATING ) {
3986 /* we want resync, peer has not yet decided to sync... */
3987 /* Nowadays only used when forcing a node into primary role and
3988 setting its disk to UpToDate with that */
3989 drbd_send_uuids(mdev);
3990 drbd_send_current_state(mdev);
3994 clear_bit(DISCARD_MY_DATA, &mdev->flags);
3996 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
4001 static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
4003 struct drbd_conf *mdev;
4004 struct p_rs_uuid *p = pi->data;
4006 mdev = vnr_to_mdev(tconn, pi->vnr);
4010 wait_event(mdev->misc_wait,
4011 mdev->state.conn == C_WF_SYNC_UUID ||
4012 mdev->state.conn == C_BEHIND ||
4013 mdev->state.conn < C_CONNECTED ||
4014 mdev->state.disk < D_NEGOTIATING);
4016 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4018 /* Here the _drbd_uuid_ functions are right, current should
4019 _not_ be rotated into the history */
4020 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4021 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4022 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4024 drbd_print_uuids(mdev, "updated sync uuid");
4025 drbd_start_resync(mdev, C_SYNC_TARGET);
4029 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4035 * receive_bitmap_plain
4037 * Return 0 when done, 1 when another iteration is needed, and a negative error
4038 * code upon failure.
4041 receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
4042 unsigned long *p, struct bm_xfer_ctx *c)
4044 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4045 drbd_header_size(mdev->tconn);
4046 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
4047 c->bm_words - c->word_offset);
4048 unsigned int want = num_words * sizeof(*p);
4052 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
4057 err = drbd_recv_all(mdev->tconn, p, want);
4061 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
4063 c->word_offset += num_words;
4064 c->bit_offset = c->word_offset * BITS_PER_LONG;
4065 if (c->bit_offset > c->bm_bits)
4066 c->bit_offset = c->bm_bits;
4071 static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4073 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4076 static int dcbp_get_start(struct p_compressed_bm *p)
4078 return (p->encoding & 0x80) != 0;
4081 static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4083 return (p->encoding >> 4) & 0x7;
4089 * Return 0 when done, 1 when another iteration is needed, and a negative error
4090 * code upon failure.
4093 recv_bm_rle_bits(struct drbd_conf *mdev,
4094 struct p_compressed_bm *p,
4095 struct bm_xfer_ctx *c,
4098 struct bitstream bs;
4102 unsigned long s = c->bit_offset;
4104 int toggle = dcbp_get_start(p);
4108 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
4110 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4114 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4115 bits = vli_decode_bits(&rl, look_ahead);
4121 if (e >= c->bm_bits) {
4122 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
4125 _drbd_bm_set_bits(mdev, s, e);
4129 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4130 have, bits, look_ahead,
4131 (unsigned int)(bs.cur.b - p->code),
4132 (unsigned int)bs.buf_len);
4135 look_ahead >>= bits;
4138 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4141 look_ahead |= tmp << have;
4146 bm_xfer_ctx_bit_to_word_offset(c);
4148 return (s != c->bm_bits);
4154 * Return 0 when done, 1 when another iteration is needed, and a negative error
4155 * code upon failure.
4158 decode_bitmap_c(struct drbd_conf *mdev,
4159 struct p_compressed_bm *p,
4160 struct bm_xfer_ctx *c,
4163 if (dcbp_get_code(p) == RLE_VLI_Bits)
4164 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
4166 /* other variants had been implemented for evaluation,
4167 * but have been dropped as this one turned out to be "best"
4168 * during all our tests. */
4170 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
4171 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
4175 void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4176 const char *direction, struct bm_xfer_ctx *c)
4178 /* what would it take to transfer it "plaintext" */
4179 unsigned int header_size = drbd_header_size(mdev->tconn);
4180 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4181 unsigned int plain =
4182 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4183 c->bm_words * sizeof(unsigned long);
4184 unsigned int total = c->bytes[0] + c->bytes[1];
4187 /* total can not be zero. but just in case: */
4191 /* don't report if not compressed */
4195 /* total < plain. check for overflow, still */
4196 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4197 : (1000 * total / plain);
4203 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4204 "total %u; compression: %u.%u%%\n",
4206 c->bytes[1], c->packets[1],
4207 c->bytes[0], c->packets[0],
4208 total, r/10, r % 10);
4211 /* Since we are processing the bitfield from lower addresses to higher,
4212 it does not matter if the process it in 32 bit chunks or 64 bit
4213 chunks as long as it is little endian. (Understand it as byte stream,
4214 beginning with the lowest byte...) If we would use big endian
4215 we would need to process it from the highest address to the lowest,
4216 in order to be agnostic to the 32 vs 64 bits issue.
4218 returns 0 on failure, 1 if we successfully received it. */
4219 static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
4221 struct drbd_conf *mdev;
4222 struct bm_xfer_ctx c;
4225 mdev = vnr_to_mdev(tconn, pi->vnr);
4229 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4230 /* you are supposed to send additional out-of-sync information
4231 * if you actually set bits during this phase */
4233 c = (struct bm_xfer_ctx) {
4234 .bm_bits = drbd_bm_bits(mdev),
4235 .bm_words = drbd_bm_words(mdev),
4239 if (pi->cmd == P_BITMAP)
4240 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4241 else if (pi->cmd == P_COMPRESSED_BITMAP) {
4242 /* MAYBE: sanity check that we speak proto >= 90,
4243 * and the feature is enabled! */
4244 struct p_compressed_bm *p = pi->data;
4246 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
4247 dev_err(DEV, "ReportCBitmap packet too large\n");
4251 if (pi->size <= sizeof(*p)) {
4252 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
4256 err = drbd_recv_all(mdev->tconn, p, pi->size);
4259 err = decode_bitmap_c(mdev, p, &c, pi->size);
4261 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
4266 c.packets[pi->cmd == P_BITMAP]++;
4267 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
4274 err = drbd_recv_header(mdev->tconn, pi);
4279 INFO_bm_xfer_stats(mdev, "receive", &c);
4281 if (mdev->state.conn == C_WF_BITMAP_T) {
4282 enum drbd_state_rv rv;
4284 err = drbd_send_bitmap(mdev);
4287 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
4288 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4289 D_ASSERT(rv == SS_SUCCESS);
4290 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4291 /* admin may have requested C_DISCONNECTING,
4292 * other threads may have noticed network errors */
4293 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4294 drbd_conn_str(mdev->state.conn));
4299 drbd_bm_unlock(mdev);
4300 if (!err && mdev->state.conn == C_WF_BITMAP_S)
4301 drbd_start_resync(mdev, C_SYNC_SOURCE);
4305 static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
4307 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
4310 return ignore_remaining_packet(tconn, pi);
4313 static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
4315 /* Make sure we've acked all the TCP data associated
4316 * with the data requests being unplugged */
4317 drbd_tcp_quickack(tconn->data.socket);
4322 static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
4324 struct drbd_conf *mdev;
4325 struct p_block_desc *p = pi->data;
4327 mdev = vnr_to_mdev(tconn, pi->vnr);
4331 switch (mdev->state.conn) {
4332 case C_WF_SYNC_UUID:
4337 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4338 drbd_conn_str(mdev->state.conn));
4341 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4349 int (*fn)(struct drbd_tconn *, struct packet_info *);
4352 static struct data_cmd drbd_cmd_handler[] = {
4353 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4354 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4355 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4356 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
4357 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4358 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4359 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
4360 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4361 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4362 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4363 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
4364 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4365 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4366 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4367 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4368 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4369 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4370 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4371 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4372 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4373 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4374 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4375 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
4376 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
4379 static void drbdd(struct drbd_tconn *tconn)
4381 struct packet_info pi;
4382 size_t shs; /* sub header size */
4385 while (get_t_state(&tconn->receiver) == RUNNING) {
4386 struct data_cmd *cmd;
4388 drbd_thread_current_set_cpu(&tconn->receiver);
4389 if (drbd_recv_header(tconn, &pi))
4392 cmd = &drbd_cmd_handler[pi.cmd];
4393 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
4394 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4395 cmdname(pi.cmd), pi.cmd);
4399 shs = cmd->pkt_size;
4400 if (pi.size > shs && !cmd->expect_payload) {
4401 conn_err(tconn, "No payload expected %s l:%d\n",
4402 cmdname(pi.cmd), pi.size);
4407 err = drbd_recv_all_warn(tconn, pi.data, shs);
4413 err = cmd->fn(tconn, &pi);
4415 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4416 cmdname(pi.cmd), err, pi.size);
4423 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
4426 void conn_flush_workqueue(struct drbd_tconn *tconn)
4428 struct drbd_wq_barrier barr;
4430 barr.w.cb = w_prev_work_done;
4431 barr.w.tconn = tconn;
4432 init_completion(&barr.done);
4433 drbd_queue_work(&tconn->sender_work, &barr.w);
4434 wait_for_completion(&barr.done);
4437 static void conn_disconnect(struct drbd_tconn *tconn)
4439 struct drbd_conf *mdev;
4443 if (tconn->cstate == C_STANDALONE)
4446 /* We are about to start the cleanup after connection loss.
4447 * Make sure drbd_make_request knows about that.
4448 * Usually we should be in some network failure state already,
4449 * but just in case we are not, we fix it up here.
4451 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4453 /* asender does not clean up anything. it must not interfere, either */
4454 drbd_thread_stop(&tconn->asender);
4455 drbd_free_sock(tconn);
4458 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4459 kref_get(&mdev->kref);
4461 drbd_disconnected(mdev);
4462 kref_put(&mdev->kref, &drbd_minor_destroy);
4467 if (!list_empty(&tconn->current_epoch->list))
4468 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4469 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4470 atomic_set(&tconn->current_epoch->epoch_size, 0);
4471 tconn->send.seen_any_write_yet = false;
4473 conn_info(tconn, "Connection closed\n");
4475 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4476 conn_try_outdate_peer_async(tconn);
4478 spin_lock_irq(&tconn->req_lock);
4480 if (oc >= C_UNCONNECTED)
4481 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4483 spin_unlock_irq(&tconn->req_lock);
4485 if (oc == C_DISCONNECTING)
4486 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
4489 static int drbd_disconnected(struct drbd_conf *mdev)
4493 /* wait for current activity to cease. */
4494 spin_lock_irq(&mdev->tconn->req_lock);
4495 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4496 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4497 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
4498 spin_unlock_irq(&mdev->tconn->req_lock);
4500 /* We do not have data structures that would allow us to
4501 * get the rs_pending_cnt down to 0 again.
4502 * * On C_SYNC_TARGET we do not have any data structures describing
4503 * the pending RSDataRequest's we have sent.
4504 * * On C_SYNC_SOURCE there is no data structure that tracks
4505 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4506 * And no, it is not the sum of the reference counts in the
4507 * resync_LRU. The resync_LRU tracks the whole operation including
4508 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4510 drbd_rs_cancel_all(mdev);
4512 mdev->rs_failed = 0;
4513 atomic_set(&mdev->rs_pending_cnt, 0);
4514 wake_up(&mdev->misc_wait);
4516 del_timer_sync(&mdev->resync_timer);
4517 resync_timer_fn((unsigned long)mdev);
4519 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4520 * w_make_resync_request etc. which may still be on the worker queue
4521 * to be "canceled" */
4522 drbd_flush_workqueue(mdev);
4524 drbd_finish_peer_reqs(mdev);
4526 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4527 might have issued a work again. The one before drbd_finish_peer_reqs() is
4528 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4529 drbd_flush_workqueue(mdev);
4531 /* need to do it again, drbd_finish_peer_reqs() may have populated it
4532 * again via drbd_try_clear_on_disk_bm(). */
4533 drbd_rs_cancel_all(mdev);
4535 kfree(mdev->p_uuid);
4536 mdev->p_uuid = NULL;
4538 if (!drbd_suspended(mdev))
4539 tl_clear(mdev->tconn);
4543 /* serialize with bitmap writeout triggered by the state change,
4545 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4547 /* tcp_close and release of sendpage pages can be deferred. I don't
4548 * want to use SO_LINGER, because apparently it can be deferred for
4549 * more than 20 seconds (longest time I checked).
4551 * Actually we don't care for exactly when the network stack does its
4552 * put_page(), but release our reference on these pages right here.
4554 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
4556 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
4557 i = atomic_read(&mdev->pp_in_use_by_net);
4559 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
4560 i = atomic_read(&mdev->pp_in_use);
4562 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
4564 D_ASSERT(list_empty(&mdev->read_ee));
4565 D_ASSERT(list_empty(&mdev->active_ee));
4566 D_ASSERT(list_empty(&mdev->sync_ee));
4567 D_ASSERT(list_empty(&mdev->done_ee));
4573 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4574 * we can agree on is stored in agreed_pro_version.
4576 * feature flags and the reserved array should be enough room for future
4577 * enhancements of the handshake protocol, and possible plugins...
4579 * for now, they are expected to be zero, but ignored.
4581 static int drbd_send_features(struct drbd_tconn *tconn)
4583 struct drbd_socket *sock;
4584 struct p_connection_features *p;
4586 sock = &tconn->data;
4587 p = conn_prepare_command(tconn, sock);
4590 memset(p, 0, sizeof(*p));
4591 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4592 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
4593 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
4598 * 1 yes, we have a valid connection
4599 * 0 oops, did not work out, please try again
4600 * -1 peer talks different language,
4601 * no point in trying again, please go standalone.
4603 static int drbd_do_features(struct drbd_tconn *tconn)
4605 /* ASSERT current == tconn->receiver ... */
4606 struct p_connection_features *p;
4607 const int expect = sizeof(struct p_connection_features);
4608 struct packet_info pi;
4611 err = drbd_send_features(tconn);
4615 err = drbd_recv_header(tconn, &pi);
4619 if (pi.cmd != P_CONNECTION_FEATURES) {
4620 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
4621 cmdname(pi.cmd), pi.cmd);
4625 if (pi.size != expect) {
4626 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
4632 err = drbd_recv_all_warn(tconn, p, expect);
4636 p->protocol_min = be32_to_cpu(p->protocol_min);
4637 p->protocol_max = be32_to_cpu(p->protocol_max);
4638 if (p->protocol_max == 0)
4639 p->protocol_max = p->protocol_min;
4641 if (PRO_VERSION_MAX < p->protocol_min ||
4642 PRO_VERSION_MIN > p->protocol_max)
4645 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
4647 conn_info(tconn, "Handshake successful: "
4648 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
4653 conn_err(tconn, "incompatible DRBD dialects: "
4654 "I support %d-%d, peer supports %d-%d\n",
4655 PRO_VERSION_MIN, PRO_VERSION_MAX,
4656 p->protocol_min, p->protocol_max);
4660 #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
4661 static int drbd_do_auth(struct drbd_tconn *tconn)
4663 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4664 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
4668 #define CHALLENGE_LEN 64
4672 0 - failed, try again (network error),
4673 -1 - auth failed, don't try again.
4676 static int drbd_do_auth(struct drbd_tconn *tconn)
4678 struct drbd_socket *sock;
4679 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4680 struct scatterlist sg;
4681 char *response = NULL;
4682 char *right_response = NULL;
4683 char *peers_ch = NULL;
4684 unsigned int key_len;
4685 char secret[SHARED_SECRET_MAX]; /* 64 byte */
4686 unsigned int resp_size;
4687 struct hash_desc desc;
4688 struct packet_info pi;
4689 struct net_conf *nc;
4692 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4695 nc = rcu_dereference(tconn->net_conf);
4696 key_len = strlen(nc->shared_secret);
4697 memcpy(secret, nc->shared_secret, key_len);
4700 desc.tfm = tconn->cram_hmac_tfm;
4703 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
4705 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
4710 get_random_bytes(my_challenge, CHALLENGE_LEN);
4712 sock = &tconn->data;
4713 if (!conn_prepare_command(tconn, sock)) {
4717 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
4718 my_challenge, CHALLENGE_LEN);
4722 err = drbd_recv_header(tconn, &pi);
4728 if (pi.cmd != P_AUTH_CHALLENGE) {
4729 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
4730 cmdname(pi.cmd), pi.cmd);
4735 if (pi.size > CHALLENGE_LEN * 2) {
4736 conn_err(tconn, "expected AuthChallenge payload too big.\n");
4741 peers_ch = kmalloc(pi.size, GFP_NOIO);
4742 if (peers_ch == NULL) {
4743 conn_err(tconn, "kmalloc of peers_ch failed\n");
4748 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4754 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
4755 response = kmalloc(resp_size, GFP_NOIO);
4756 if (response == NULL) {
4757 conn_err(tconn, "kmalloc of response failed\n");
4762 sg_init_table(&sg, 1);
4763 sg_set_buf(&sg, peers_ch, pi.size);
4765 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4767 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
4772 if (!conn_prepare_command(tconn, sock)) {
4776 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
4777 response, resp_size);
4781 err = drbd_recv_header(tconn, &pi);
4787 if (pi.cmd != P_AUTH_RESPONSE) {
4788 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
4789 cmdname(pi.cmd), pi.cmd);
4794 if (pi.size != resp_size) {
4795 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
4800 err = drbd_recv_all_warn(tconn, response , resp_size);
4806 right_response = kmalloc(resp_size, GFP_NOIO);
4807 if (right_response == NULL) {
4808 conn_err(tconn, "kmalloc of right_response failed\n");
4813 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4815 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4817 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
4822 rv = !memcmp(response, right_response, resp_size);
4825 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4833 kfree(right_response);
4839 int drbdd_init(struct drbd_thread *thi)
4841 struct drbd_tconn *tconn = thi->tconn;
4844 conn_info(tconn, "receiver (re)started\n");
4847 h = conn_connect(tconn);
4849 conn_disconnect(tconn);
4850 schedule_timeout_interruptible(HZ);
4853 conn_warn(tconn, "Discarding network configuration.\n");
4854 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
4861 conn_disconnect(tconn);
4863 conn_info(tconn, "receiver terminated\n");
4867 /* ********* acknowledge sender ******** */
4869 static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
4871 struct p_req_state_reply *p = pi->data;
4872 int retcode = be32_to_cpu(p->retcode);
4874 if (retcode >= SS_SUCCESS) {
4875 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4877 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4878 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4879 drbd_set_st_err_str(retcode), retcode);
4881 wake_up(&tconn->ping_wait);
4886 static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
4888 struct drbd_conf *mdev;
4889 struct p_req_state_reply *p = pi->data;
4890 int retcode = be32_to_cpu(p->retcode);
4892 mdev = vnr_to_mdev(tconn, pi->vnr);
4896 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4897 D_ASSERT(tconn->agreed_pro_version < 100);
4898 return got_conn_RqSReply(tconn, pi);
4901 if (retcode >= SS_SUCCESS) {
4902 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4904 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4905 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4906 drbd_set_st_err_str(retcode), retcode);
4908 wake_up(&mdev->state_wait);
4913 static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
4915 return drbd_send_ping_ack(tconn);
4919 static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
4921 /* restore idle timeout */
4922 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4923 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4924 wake_up(&tconn->ping_wait);
4929 static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
4931 struct drbd_conf *mdev;
4932 struct p_block_ack *p = pi->data;
4933 sector_t sector = be64_to_cpu(p->sector);
4934 int blksize = be32_to_cpu(p->blksize);
4936 mdev = vnr_to_mdev(tconn, pi->vnr);
4940 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
4942 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4944 if (get_ldev(mdev)) {
4945 drbd_rs_complete_io(mdev, sector);
4946 drbd_set_in_sync(mdev, sector, blksize);
4947 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4948 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4951 dec_rs_pending(mdev);
4952 atomic_add(blksize >> 9, &mdev->rs_sect_in);
4958 validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4959 struct rb_root *root, const char *func,
4960 enum drbd_req_event what, bool missing_ok)
4962 struct drbd_request *req;
4963 struct bio_and_error m;
4965 spin_lock_irq(&mdev->tconn->req_lock);
4966 req = find_request(mdev, root, id, sector, missing_ok, func);
4967 if (unlikely(!req)) {
4968 spin_unlock_irq(&mdev->tconn->req_lock);
4971 __req_mod(req, what, &m);
4972 spin_unlock_irq(&mdev->tconn->req_lock);
4975 complete_master_bio(mdev, &m);
4979 static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
4981 struct drbd_conf *mdev;
4982 struct p_block_ack *p = pi->data;
4983 sector_t sector = be64_to_cpu(p->sector);
4984 int blksize = be32_to_cpu(p->blksize);
4985 enum drbd_req_event what;
4987 mdev = vnr_to_mdev(tconn, pi->vnr);
4991 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4993 if (p->block_id == ID_SYNCER) {
4994 drbd_set_in_sync(mdev, sector, blksize);
4995 dec_rs_pending(mdev);
4999 case P_RS_WRITE_ACK:
5000 what = WRITE_ACKED_BY_PEER_AND_SIS;
5003 what = WRITE_ACKED_BY_PEER;
5006 what = RECV_ACKED_BY_PEER;
5009 what = CONFLICT_RESOLVED;
5012 what = POSTPONE_WRITE;
5018 return validate_req_change_req_state(mdev, p->block_id, sector,
5019 &mdev->write_requests, __func__,
5023 static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
5025 struct drbd_conf *mdev;
5026 struct p_block_ack *p = pi->data;
5027 sector_t sector = be64_to_cpu(p->sector);
5028 int size = be32_to_cpu(p->blksize);
5031 mdev = vnr_to_mdev(tconn, pi->vnr);
5035 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5037 if (p->block_id == ID_SYNCER) {
5038 dec_rs_pending(mdev);
5039 drbd_rs_failed_io(mdev, sector, size);
5043 err = validate_req_change_req_state(mdev, p->block_id, sector,
5044 &mdev->write_requests, __func__,
5047 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5048 The master bio might already be completed, therefore the
5049 request is no longer in the collision hash. */
5050 /* In Protocol B we might already have got a P_RECV_ACK
5051 but then get a P_NEG_ACK afterwards. */
5052 drbd_set_out_of_sync(mdev, sector, size);
5057 static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
5059 struct drbd_conf *mdev;
5060 struct p_block_ack *p = pi->data;
5061 sector_t sector = be64_to_cpu(p->sector);
5063 mdev = vnr_to_mdev(tconn, pi->vnr);
5067 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5069 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
5070 (unsigned long long)sector, be32_to_cpu(p->blksize));
5072 return validate_req_change_req_state(mdev, p->block_id, sector,
5073 &mdev->read_requests, __func__,
5077 static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
5079 struct drbd_conf *mdev;
5082 struct p_block_ack *p = pi->data;
5084 mdev = vnr_to_mdev(tconn, pi->vnr);
5088 sector = be64_to_cpu(p->sector);
5089 size = be32_to_cpu(p->blksize);
5091 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5093 dec_rs_pending(mdev);
5095 if (get_ldev_if_state(mdev, D_FAILED)) {
5096 drbd_rs_complete_io(mdev, sector);
5098 case P_NEG_RS_DREPLY:
5099 drbd_rs_failed_io(mdev, sector, size);
5111 static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
5113 struct p_barrier_ack *p = pi->data;
5114 struct drbd_conf *mdev;
5117 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
5120 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5121 if (mdev->state.conn == C_AHEAD &&
5122 atomic_read(&mdev->ap_in_flight) == 0 &&
5123 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5124 mdev->start_resync_timer.expires = jiffies + HZ;
5125 add_timer(&mdev->start_resync_timer);
5133 static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
5135 struct drbd_conf *mdev;
5136 struct p_block_ack *p = pi->data;
5137 struct drbd_work *w;
5141 mdev = vnr_to_mdev(tconn, pi->vnr);
5145 sector = be64_to_cpu(p->sector);
5146 size = be32_to_cpu(p->blksize);
5148 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5150 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
5151 drbd_ov_out_of_sync_found(mdev, sector, size);
5153 ov_out_of_sync_print(mdev);
5155 if (!get_ldev(mdev))
5158 drbd_rs_complete_io(mdev, sector);
5159 dec_rs_pending(mdev);
5163 /* let's advance progress step marks only for every other megabyte */
5164 if ((mdev->ov_left & 0x200) == 0x200)
5165 drbd_advance_rs_marks(mdev, mdev->ov_left);
5167 if (mdev->ov_left == 0) {
5168 w = kmalloc(sizeof(*w), GFP_NOIO);
5170 w->cb = w_ov_finished;
5172 drbd_queue_work(&mdev->tconn->sender_work, w);
5174 dev_err(DEV, "kmalloc(w) failed.");
5175 ov_out_of_sync_print(mdev);
5176 drbd_resync_finished(mdev);
5183 static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
5188 static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
5190 struct drbd_conf *mdev;
5191 int vnr, not_empty = 0;
5194 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5195 flush_signals(current);
5198 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5199 kref_get(&mdev->kref);
5201 if (drbd_finish_peer_reqs(mdev)) {
5202 kref_put(&mdev->kref, &drbd_minor_destroy);
5205 kref_put(&mdev->kref, &drbd_minor_destroy);
5208 set_bit(SIGNAL_ASENDER, &tconn->flags);
5210 spin_lock_irq(&tconn->req_lock);
5211 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5212 not_empty = !list_empty(&mdev->done_ee);
5216 spin_unlock_irq(&tconn->req_lock);
5218 } while (not_empty);
5223 struct asender_cmd {
5225 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
5228 static struct asender_cmd asender_tbl[] = {
5229 [P_PING] = { 0, got_Ping },
5230 [P_PING_ACK] = { 0, got_PingAck },
5231 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5232 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5233 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5234 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
5235 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5236 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5237 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5238 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5239 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5240 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5241 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5242 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5243 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5244 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5245 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5248 int drbd_asender(struct drbd_thread *thi)
5250 struct drbd_tconn *tconn = thi->tconn;
5251 struct asender_cmd *cmd = NULL;
5252 struct packet_info pi;
5254 void *buf = tconn->meta.rbuf;
5256 unsigned int header_size = drbd_header_size(tconn);
5257 int expect = header_size;
5258 bool ping_timeout_active = false;
5259 struct net_conf *nc;
5260 int ping_timeo, tcp_cork, ping_int;
5262 current->policy = SCHED_RR; /* Make this a realtime task! */
5263 current->rt_priority = 2; /* more important than all other tasks */
5265 while (get_t_state(thi) == RUNNING) {
5266 drbd_thread_current_set_cpu(thi);
5269 nc = rcu_dereference(tconn->net_conf);
5270 ping_timeo = nc->ping_timeo;
5271 tcp_cork = nc->tcp_cork;
5272 ping_int = nc->ping_int;
5275 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
5276 if (drbd_send_ping(tconn)) {
5277 conn_err(tconn, "drbd_send_ping has failed\n");
5280 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5281 ping_timeout_active = true;
5284 /* TODO: conditionally cork; it may hurt latency if we cork without
5287 drbd_tcp_cork(tconn->meta.socket);
5288 if (tconn_finish_peer_reqs(tconn)) {
5289 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
5292 /* but unconditionally uncork unless disabled */
5294 drbd_tcp_uncork(tconn->meta.socket);
5296 /* short circuit, recv_msg would return EINTR anyways. */
5297 if (signal_pending(current))
5300 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5301 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5303 flush_signals(current);
5306 * -EINTR (on meta) we got a signal
5307 * -EAGAIN (on meta) rcvtimeo expired
5308 * -ECONNRESET other side closed the connection
5309 * -ERESTARTSYS (on data) we got a signal
5310 * rv < 0 other than above: unexpected error!
5311 * rv == expected: full header or command
5312 * rv < expected: "woken" by signal during receive
5313 * rv == 0 : "connection shut down by peer"
5315 if (likely(rv > 0)) {
5318 } else if (rv == 0) {
5319 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
5322 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
5325 t = wait_event_timeout(tconn->ping_wait,
5326 tconn->cstate < C_WF_REPORT_PARAMS,
5331 conn_err(tconn, "meta connection shut down by peer.\n");
5333 } else if (rv == -EAGAIN) {
5334 /* If the data socket received something meanwhile,
5335 * that is good enough: peer is still alive. */
5336 if (time_after(tconn->last_received,
5337 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
5339 if (ping_timeout_active) {
5340 conn_err(tconn, "PingAck did not arrive in time.\n");
5343 set_bit(SEND_PING, &tconn->flags);
5345 } else if (rv == -EINTR) {
5348 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
5352 if (received == expect && cmd == NULL) {
5353 if (decode_header(tconn, tconn->meta.rbuf, &pi))
5355 cmd = &asender_tbl[pi.cmd];
5356 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
5357 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5358 cmdname(pi.cmd), pi.cmd);
5361 expect = header_size + cmd->pkt_size;
5362 if (pi.size != expect - header_size) {
5363 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
5368 if (received == expect) {
5371 err = cmd->fn(tconn, &pi);
5373 conn_err(tconn, "%pf failed\n", cmd->fn);
5377 tconn->last_received = jiffies;
5379 if (cmd == &asender_tbl[P_PING_ACK]) {
5380 /* restore idle timeout */
5381 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5382 ping_timeout_active = false;
5385 buf = tconn->meta.rbuf;
5387 expect = header_size;
5394 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
5395 conn_md_sync(tconn);
5399 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
5401 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5403 conn_info(tconn, "asender terminated\n");