1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 drbd_receiver.c 4 5 This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 6 7 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH. 8 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>. 9 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 10 11 */ 12 13 14 #include <linux/module.h> 15 16 #include <linux/uaccess.h> 17 #include <net/sock.h> 18 19 #include <linux/drbd.h> 20 #include <linux/fs.h> 21 #include <linux/file.h> 22 #include <linux/in.h> 23 #include <linux/mm.h> 24 #include <linux/memcontrol.h> 25 #include <linux/mm_inline.h> 26 #include <linux/slab.h> 27 #include <uapi/linux/sched/types.h> 28 #include <linux/sched/signal.h> 29 #include <linux/pkt_sched.h> 30 #include <linux/unistd.h> 31 #include <linux/vmalloc.h> 32 #include <linux/random.h> 33 #include <linux/string.h> 34 #include <linux/scatterlist.h> 35 #include <linux/part_stat.h> 36 #include <linux/mempool.h> 37 #include "drbd_int.h" 38 #include "drbd_protocol.h" 39 #include "drbd_req.h" 40 #include "drbd_vli.h" 41 42 #define PRO_FEATURES (DRBD_FF_TRIM|DRBD_FF_THIN_RESYNC|DRBD_FF_WSAME|DRBD_FF_WZEROES) 43 44 struct packet_info { 45 enum drbd_packet cmd; 46 unsigned int size; 47 unsigned int vnr; 48 void *data; 49 }; 50 51 enum finish_epoch { 52 FE_STILL_LIVE, 53 FE_DESTROYED, 54 FE_RECYCLED, 55 }; 56 57 static int drbd_do_features(struct drbd_connection *connection); 58 static int drbd_do_auth(struct drbd_connection *connection); 59 static int drbd_disconnected(struct drbd_peer_device *); 60 static void conn_wait_active_ee_empty(struct drbd_connection *connection); 61 static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *, struct drbd_epoch *, enum epoch_event); 62 static int e_end_block(struct drbd_work *, int); 63 64 65 #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN) 66 67 static struct page *__drbd_alloc_pages(unsigned int number) 68 { 69 struct page *page = NULL; 70 struct page *tmp = NULL; 71 unsigned int i = 0; 72 73 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD 74 * "criss-cross" setup, that might cause write-out on some other DRBD, 75 * which in turn might block on the other node at this very place. */ 76 for (i = 0; i < number; i++) { 77 tmp = mempool_alloc(&drbd_buffer_page_pool, GFP_TRY); 78 if (!tmp) 79 goto fail; 80 set_page_private(tmp, (unsigned long)page); 81 page = tmp; 82 } 83 return page; 84 fail: 85 page_chain_for_each_safe(page, tmp) { 86 set_page_private(page, 0); 87 mempool_free(page, &drbd_buffer_page_pool); 88 } 89 return NULL; 90 } 91 92 /** 93 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled) 94 * @peer_device: DRBD device. 95 * @number: number of pages requested 96 * @retry: whether to retry, if not enough pages are available right now 97 * 98 * Tries to allocate number pages, first from our own page pool, then from 99 * the kernel. 100 * Possibly retry until DRBD frees sufficient pages somewhere else. 101 * 102 * If this allocation would exceed the max_buffers setting, we throttle 103 * allocation (schedule_timeout) to give the system some room to breathe. 104 * 105 * We do not use max-buffers as hard limit, because it could lead to 106 * congestion and further to a distributed deadlock during online-verify or 107 * (checksum based) resync, if the max-buffers, socket buffer sizes and 108 * resync-rate settings are mis-configured. 109 * 110 * Returns a page chain linked via page->private. 111 */ 112 struct page *drbd_alloc_pages(struct drbd_peer_device *peer_device, unsigned int number, 113 bool retry) 114 { 115 struct drbd_device *device = peer_device->device; 116 struct page *page; 117 struct net_conf *nc; 118 unsigned int mxb; 119 120 rcu_read_lock(); 121 nc = rcu_dereference(peer_device->connection->net_conf); 122 mxb = nc ? nc->max_buffers : 1000000; 123 rcu_read_unlock(); 124 125 if (atomic_read(&device->pp_in_use) >= mxb) 126 schedule_timeout_interruptible(HZ / 10); 127 page = __drbd_alloc_pages(number); 128 129 if (page) 130 atomic_add(number, &device->pp_in_use); 131 return page; 132 } 133 134 /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages. 135 * Is also used from inside an other spin_lock_irq(&resource->req_lock); 136 * Either links the page chain back to the global pool, 137 * or returns all pages to the system. */ 138 static void drbd_free_pages(struct drbd_device *device, struct page *page) 139 { 140 struct page *tmp; 141 int i = 0; 142 143 if (page == NULL) 144 return; 145 146 page_chain_for_each_safe(page, tmp) { 147 set_page_private(page, 0); 148 if (page_count(page) == 1) 149 mempool_free(page, &drbd_buffer_page_pool); 150 else 151 put_page(page); 152 i++; 153 } 154 i = atomic_sub_return(i, &device->pp_in_use); 155 if (i < 0) 156 drbd_warn(device, "ASSERTION FAILED: pp_in_use: %d < 0\n", i); 157 } 158 159 /* 160 You need to hold the req_lock: 161 _drbd_wait_ee_list_empty() 162 163 You must not have the req_lock: 164 drbd_free_peer_req() 165 drbd_alloc_peer_req() 166 drbd_free_peer_reqs() 167 drbd_ee_fix_bhs() 168 drbd_finish_peer_reqs() 169 drbd_clear_done_ee() 170 drbd_wait_ee_list_empty() 171 */ 172 173 /* normal: payload_size == request size (bi_size) 174 * w_same: payload_size == logical_block_size 175 * trim: payload_size == 0 */ 176 struct drbd_peer_request * 177 drbd_alloc_peer_req(struct drbd_peer_device *peer_device, u64 id, sector_t sector, 178 unsigned int request_size, unsigned int payload_size, gfp_t gfp_mask) __must_hold(local) 179 { 180 struct drbd_device *device = peer_device->device; 181 struct drbd_peer_request *peer_req; 182 struct page *page = NULL; 183 unsigned int nr_pages = PFN_UP(payload_size); 184 185 if (drbd_insert_fault(device, DRBD_FAULT_AL_EE)) 186 return NULL; 187 188 peer_req = mempool_alloc(&drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM); 189 if (!peer_req) { 190 if (!(gfp_mask & __GFP_NOWARN)) 191 drbd_err(device, "%s: allocation failed\n", __func__); 192 return NULL; 193 } 194 195 if (nr_pages) { 196 page = drbd_alloc_pages(peer_device, nr_pages, 197 gfpflags_allow_blocking(gfp_mask)); 198 if (!page) 199 goto fail; 200 if (!mempool_is_saturated(&drbd_buffer_page_pool)) 201 peer_req->flags |= EE_RELEASE_TO_MEMPOOL; 202 } 203 204 memset(peer_req, 0, sizeof(*peer_req)); 205 INIT_LIST_HEAD(&peer_req->w.list); 206 drbd_clear_interval(&peer_req->i); 207 peer_req->i.size = request_size; 208 peer_req->i.sector = sector; 209 peer_req->submit_jif = jiffies; 210 peer_req->peer_device = peer_device; 211 peer_req->pages = page; 212 /* 213 * The block_id is opaque to the receiver. It is not endianness 214 * converted, and sent back to the sender unchanged. 215 */ 216 peer_req->block_id = id; 217 218 return peer_req; 219 220 fail: 221 mempool_free(peer_req, &drbd_ee_mempool); 222 return NULL; 223 } 224 225 void drbd_free_peer_req(struct drbd_device *device, struct drbd_peer_request *peer_req) 226 { 227 might_sleep(); 228 if (peer_req->flags & EE_HAS_DIGEST) 229 kfree(peer_req->digest); 230 drbd_free_pages(device, peer_req->pages); 231 D_ASSERT(device, atomic_read(&peer_req->pending_bios) == 0); 232 D_ASSERT(device, drbd_interval_empty(&peer_req->i)); 233 if (!expect(device, !(peer_req->flags & EE_CALL_AL_COMPLETE_IO))) { 234 peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO; 235 drbd_al_complete_io(device, &peer_req->i); 236 } 237 mempool_free(peer_req, &drbd_ee_mempool); 238 } 239 240 int drbd_free_peer_reqs(struct drbd_device *device, struct list_head *list) 241 { 242 LIST_HEAD(work_list); 243 struct drbd_peer_request *peer_req, *t; 244 int count = 0; 245 246 spin_lock_irq(&device->resource->req_lock); 247 list_splice_init(list, &work_list); 248 spin_unlock_irq(&device->resource->req_lock); 249 250 list_for_each_entry_safe(peer_req, t, &work_list, w.list) { 251 drbd_free_peer_req(device, peer_req); 252 count++; 253 } 254 return count; 255 } 256 257 /* 258 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier. 259 */ 260 static int drbd_finish_peer_reqs(struct drbd_device *device) 261 { 262 LIST_HEAD(work_list); 263 struct drbd_peer_request *peer_req, *t; 264 int err = 0; 265 266 spin_lock_irq(&device->resource->req_lock); 267 list_splice_init(&device->done_ee, &work_list); 268 spin_unlock_irq(&device->resource->req_lock); 269 270 /* possible callbacks here: 271 * e_end_block, and e_end_resync_block, e_send_superseded. 272 * all ignore the last argument. 273 */ 274 list_for_each_entry_safe(peer_req, t, &work_list, w.list) { 275 int err2; 276 277 /* list_del not necessary, next/prev members not touched */ 278 err2 = peer_req->w.cb(&peer_req->w, !!err); 279 if (!err) 280 err = err2; 281 drbd_free_peer_req(device, peer_req); 282 } 283 wake_up(&device->ee_wait); 284 285 return err; 286 } 287 288 static void _drbd_wait_ee_list_empty(struct drbd_device *device, 289 struct list_head *head) 290 { 291 DEFINE_WAIT(wait); 292 293 /* avoids spin_lock/unlock 294 * and calling prepare_to_wait in the fast path */ 295 while (!list_empty(head)) { 296 prepare_to_wait(&device->ee_wait, &wait, TASK_UNINTERRUPTIBLE); 297 spin_unlock_irq(&device->resource->req_lock); 298 io_schedule(); 299 finish_wait(&device->ee_wait, &wait); 300 spin_lock_irq(&device->resource->req_lock); 301 } 302 } 303 304 static void drbd_wait_ee_list_empty(struct drbd_device *device, 305 struct list_head *head) 306 { 307 spin_lock_irq(&device->resource->req_lock); 308 _drbd_wait_ee_list_empty(device, head); 309 spin_unlock_irq(&device->resource->req_lock); 310 } 311 312 static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags) 313 { 314 struct kvec iov = { 315 .iov_base = buf, 316 .iov_len = size, 317 }; 318 struct msghdr msg = { 319 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL) 320 }; 321 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, size); 322 return sock_recvmsg(sock, &msg, msg.msg_flags); 323 } 324 325 static int drbd_recv(struct drbd_connection *connection, void *buf, size_t size) 326 { 327 int rv; 328 329 rv = drbd_recv_short(connection->data.socket, buf, size, 0); 330 331 if (rv < 0) { 332 if (rv == -ECONNRESET) 333 drbd_info(connection, "sock was reset by peer\n"); 334 else if (rv != -ERESTARTSYS) 335 drbd_err(connection, "sock_recvmsg returned %d\n", rv); 336 } else if (rv == 0) { 337 if (test_bit(DISCONNECT_SENT, &connection->flags)) { 338 long t; 339 rcu_read_lock(); 340 t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10; 341 rcu_read_unlock(); 342 343 t = wait_event_timeout(connection->ping_wait, connection->cstate < C_WF_REPORT_PARAMS, t); 344 345 if (t) 346 goto out; 347 } 348 drbd_info(connection, "sock was shut down by peer\n"); 349 } 350 351 if (rv != size) 352 conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD); 353 354 out: 355 return rv; 356 } 357 358 static int drbd_recv_all(struct drbd_connection *connection, void *buf, size_t size) 359 { 360 int err; 361 362 err = drbd_recv(connection, buf, size); 363 if (err != size) { 364 if (err >= 0) 365 err = -EIO; 366 } else 367 err = 0; 368 return err; 369 } 370 371 static int drbd_recv_all_warn(struct drbd_connection *connection, void *buf, size_t size) 372 { 373 int err; 374 375 err = drbd_recv_all(connection, buf, size); 376 if (err && !signal_pending(current)) 377 drbd_warn(connection, "short read (expected size %d)\n", (int)size); 378 return err; 379 } 380 381 /* quoting tcp(7): 382 * On individual connections, the socket buffer size must be set prior to the 383 * listen(2) or connect(2) calls in order to have it take effect. 384 * This is our wrapper to do so. 385 */ 386 static void drbd_setbufsize(struct socket *sock, unsigned int snd, 387 unsigned int rcv) 388 { 389 /* open coded SO_SNDBUF, SO_RCVBUF */ 390 if (snd) { 391 sock->sk->sk_sndbuf = snd; 392 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 393 } 394 if (rcv) { 395 sock->sk->sk_rcvbuf = rcv; 396 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 397 } 398 } 399 400 static struct socket *drbd_try_connect(struct drbd_connection *connection) 401 { 402 const char *what; 403 struct socket *sock; 404 struct sockaddr_in6 src_in6; 405 struct sockaddr_in6 peer_in6; 406 struct net_conf *nc; 407 int err, peer_addr_len, my_addr_len; 408 int sndbuf_size, rcvbuf_size, connect_int; 409 int disconnect_on_error = 1; 410 411 rcu_read_lock(); 412 nc = rcu_dereference(connection->net_conf); 413 if (!nc) { 414 rcu_read_unlock(); 415 return NULL; 416 } 417 sndbuf_size = nc->sndbuf_size; 418 rcvbuf_size = nc->rcvbuf_size; 419 connect_int = nc->connect_int; 420 rcu_read_unlock(); 421 422 my_addr_len = min_t(int, connection->my_addr_len, sizeof(src_in6)); 423 memcpy(&src_in6, &connection->my_addr, my_addr_len); 424 425 if (((struct sockaddr *)&connection->my_addr)->sa_family == AF_INET6) 426 src_in6.sin6_port = 0; 427 else 428 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */ 429 430 peer_addr_len = min_t(int, connection->peer_addr_len, sizeof(src_in6)); 431 memcpy(&peer_in6, &connection->peer_addr, peer_addr_len); 432 433 what = "sock_create_kern"; 434 err = sock_create_kern(&init_net, ((struct sockaddr *)&src_in6)->sa_family, 435 SOCK_STREAM, IPPROTO_TCP, &sock); 436 if (err < 0) { 437 sock = NULL; 438 goto out; 439 } 440 441 sock->sk->sk_rcvtimeo = 442 sock->sk->sk_sndtimeo = connect_int * HZ; 443 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size); 444 445 /* explicitly bind to the configured IP as source IP 446 * for the outgoing connections. 447 * This is needed for multihomed hosts and to be 448 * able to use lo: interfaces for drbd. 449 * Make sure to use 0 as port number, so linux selects 450 * a free one dynamically. 451 */ 452 what = "bind before connect"; 453 err = sock->ops->bind(sock, (struct sockaddr_unsized *) &src_in6, my_addr_len); 454 if (err < 0) 455 goto out; 456 457 /* connect may fail, peer not yet available. 458 * stay C_WF_CONNECTION, don't go Disconnecting! */ 459 disconnect_on_error = 0; 460 what = "connect"; 461 err = sock->ops->connect(sock, (struct sockaddr_unsized *) &peer_in6, peer_addr_len, 0); 462 463 out: 464 if (err < 0) { 465 if (sock) { 466 sock_release(sock); 467 sock = NULL; 468 } 469 switch (-err) { 470 /* timeout, busy, signal pending */ 471 case ETIMEDOUT: case EAGAIN: case EINPROGRESS: 472 case EINTR: case ERESTARTSYS: 473 /* peer not (yet) available, network problem */ 474 case ECONNREFUSED: case ENETUNREACH: 475 case EHOSTDOWN: case EHOSTUNREACH: 476 disconnect_on_error = 0; 477 break; 478 default: 479 drbd_err(connection, "%s failed, err = %d\n", what, err); 480 } 481 if (disconnect_on_error) 482 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 483 } 484 485 return sock; 486 } 487 488 struct accept_wait_data { 489 struct drbd_connection *connection; 490 struct socket *s_listen; 491 struct completion door_bell; 492 void (*original_sk_state_change)(struct sock *sk); 493 494 }; 495 496 static void drbd_incoming_connection(struct sock *sk) 497 { 498 struct accept_wait_data *ad = sk->sk_user_data; 499 void (*state_change)(struct sock *sk); 500 501 state_change = ad->original_sk_state_change; 502 if (sk->sk_state == TCP_ESTABLISHED) 503 complete(&ad->door_bell); 504 state_change(sk); 505 } 506 507 static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad) 508 { 509 int err, sndbuf_size, rcvbuf_size, my_addr_len; 510 struct sockaddr_in6 my_addr; 511 struct socket *s_listen; 512 struct net_conf *nc; 513 const char *what; 514 515 rcu_read_lock(); 516 nc = rcu_dereference(connection->net_conf); 517 if (!nc) { 518 rcu_read_unlock(); 519 return -EIO; 520 } 521 sndbuf_size = nc->sndbuf_size; 522 rcvbuf_size = nc->rcvbuf_size; 523 rcu_read_unlock(); 524 525 my_addr_len = min_t(int, connection->my_addr_len, sizeof(struct sockaddr_in6)); 526 memcpy(&my_addr, &connection->my_addr, my_addr_len); 527 528 what = "sock_create_kern"; 529 err = sock_create_kern(&init_net, ((struct sockaddr *)&my_addr)->sa_family, 530 SOCK_STREAM, IPPROTO_TCP, &s_listen); 531 if (err) { 532 s_listen = NULL; 533 goto out; 534 } 535 536 s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ 537 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size); 538 539 what = "bind before listen"; 540 err = s_listen->ops->bind(s_listen, (struct sockaddr_unsized *)&my_addr, my_addr_len); 541 if (err < 0) 542 goto out; 543 544 ad->s_listen = s_listen; 545 write_lock_bh(&s_listen->sk->sk_callback_lock); 546 ad->original_sk_state_change = s_listen->sk->sk_state_change; 547 s_listen->sk->sk_state_change = drbd_incoming_connection; 548 s_listen->sk->sk_user_data = ad; 549 write_unlock_bh(&s_listen->sk->sk_callback_lock); 550 551 what = "listen"; 552 err = s_listen->ops->listen(s_listen, 5); 553 if (err < 0) 554 goto out; 555 556 return 0; 557 out: 558 if (s_listen) 559 sock_release(s_listen); 560 if (err < 0) { 561 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { 562 drbd_err(connection, "%s failed, err = %d\n", what, err); 563 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 564 } 565 } 566 567 return -EIO; 568 } 569 570 static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad) 571 { 572 write_lock_bh(&sk->sk_callback_lock); 573 sk->sk_state_change = ad->original_sk_state_change; 574 sk->sk_user_data = NULL; 575 write_unlock_bh(&sk->sk_callback_lock); 576 } 577 578 static struct socket *drbd_wait_for_connect(struct drbd_connection *connection, struct accept_wait_data *ad) 579 { 580 int timeo, connect_int, err = 0; 581 struct socket *s_estab = NULL; 582 struct net_conf *nc; 583 584 rcu_read_lock(); 585 nc = rcu_dereference(connection->net_conf); 586 if (!nc) { 587 rcu_read_unlock(); 588 return NULL; 589 } 590 connect_int = nc->connect_int; 591 rcu_read_unlock(); 592 593 timeo = connect_int * HZ; 594 /* 28.5% random jitter */ 595 timeo += get_random_u32_below(2) ? timeo / 7 : -timeo / 7; 596 597 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo); 598 if (err <= 0) 599 return NULL; 600 601 err = kernel_accept(ad->s_listen, &s_estab, 0); 602 if (err < 0) { 603 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) { 604 drbd_err(connection, "accept failed, err = %d\n", err); 605 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 606 } 607 } 608 609 if (s_estab) 610 unregister_state_change(s_estab->sk, ad); 611 612 return s_estab; 613 } 614 615 static int decode_header(struct drbd_connection *, void *, struct packet_info *); 616 617 static int send_first_packet(struct drbd_connection *connection, struct drbd_socket *sock, 618 enum drbd_packet cmd) 619 { 620 if (!conn_prepare_command(connection, sock)) 621 return -EIO; 622 return conn_send_command(connection, sock, cmd, 0, NULL, 0); 623 } 624 625 static int receive_first_packet(struct drbd_connection *connection, struct socket *sock) 626 { 627 unsigned int header_size = drbd_header_size(connection); 628 struct packet_info pi; 629 struct net_conf *nc; 630 int err; 631 632 rcu_read_lock(); 633 nc = rcu_dereference(connection->net_conf); 634 if (!nc) { 635 rcu_read_unlock(); 636 return -EIO; 637 } 638 sock->sk->sk_rcvtimeo = nc->ping_timeo * 4 * HZ / 10; 639 rcu_read_unlock(); 640 641 err = drbd_recv_short(sock, connection->data.rbuf, header_size, 0); 642 if (err != header_size) { 643 if (err >= 0) 644 err = -EIO; 645 return err; 646 } 647 err = decode_header(connection, connection->data.rbuf, &pi); 648 if (err) 649 return err; 650 return pi.cmd; 651 } 652 653 /** 654 * drbd_socket_okay() - Free the socket if its connection is not okay 655 * @sock: pointer to the pointer to the socket. 656 */ 657 static bool drbd_socket_okay(struct socket **sock) 658 { 659 int rr; 660 char tb[4]; 661 662 if (!*sock) 663 return false; 664 665 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK); 666 667 if (rr > 0 || rr == -EAGAIN) { 668 return true; 669 } else { 670 sock_release(*sock); 671 *sock = NULL; 672 return false; 673 } 674 } 675 676 static bool connection_established(struct drbd_connection *connection, 677 struct socket **sock1, 678 struct socket **sock2) 679 { 680 struct net_conf *nc; 681 int timeout; 682 bool ok; 683 684 if (!*sock1 || !*sock2) 685 return false; 686 687 rcu_read_lock(); 688 nc = rcu_dereference(connection->net_conf); 689 timeout = (nc->sock_check_timeo ?: nc->ping_timeo) * HZ / 10; 690 rcu_read_unlock(); 691 schedule_timeout_interruptible(timeout); 692 693 ok = drbd_socket_okay(sock1); 694 ok = drbd_socket_okay(sock2) && ok; 695 696 return ok; 697 } 698 699 /* Gets called if a connection is established, or if a new minor gets created 700 in a connection */ 701 int drbd_connected(struct drbd_peer_device *peer_device) 702 { 703 struct drbd_device *device = peer_device->device; 704 int err; 705 706 atomic_set(&device->packet_seq, 0); 707 device->peer_seq = 0; 708 709 device->state_mutex = peer_device->connection->agreed_pro_version < 100 ? 710 &peer_device->connection->cstate_mutex : 711 &device->own_state_mutex; 712 713 err = drbd_send_sync_param(peer_device); 714 if (!err) 715 err = drbd_send_sizes(peer_device, 0, 0); 716 if (!err) 717 err = drbd_send_uuids(peer_device); 718 if (!err) 719 err = drbd_send_current_state(peer_device); 720 clear_bit(USE_DEGR_WFC_T, &device->flags); 721 clear_bit(RESIZE_PENDING, &device->flags); 722 atomic_set(&device->ap_in_flight, 0); 723 mod_timer(&device->request_timer, jiffies + HZ); /* just start it here. */ 724 return err; 725 } 726 727 /* 728 * return values: 729 * 1 yes, we have a valid connection 730 * 0 oops, did not work out, please try again 731 * -1 peer talks different language, 732 * no point in trying again, please go standalone. 733 * -2 We do not have a network config... 734 */ 735 static int conn_connect(struct drbd_connection *connection) 736 { 737 struct drbd_socket sock, msock; 738 struct drbd_peer_device *peer_device; 739 struct net_conf *nc; 740 int vnr, timeout, h; 741 bool discard_my_data, ok; 742 enum drbd_state_rv rv; 743 struct accept_wait_data ad = { 744 .connection = connection, 745 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell), 746 }; 747 748 clear_bit(DISCONNECT_SENT, &connection->flags); 749 if (conn_request_state(connection, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS) 750 return -2; 751 752 mutex_init(&sock.mutex); 753 sock.sbuf = connection->data.sbuf; 754 sock.rbuf = connection->data.rbuf; 755 sock.socket = NULL; 756 mutex_init(&msock.mutex); 757 msock.sbuf = connection->meta.sbuf; 758 msock.rbuf = connection->meta.rbuf; 759 msock.socket = NULL; 760 761 /* Assume that the peer only understands protocol 80 until we know better. */ 762 connection->agreed_pro_version = 80; 763 764 if (prepare_listen_socket(connection, &ad)) 765 return 0; 766 767 do { 768 struct socket *s; 769 770 s = drbd_try_connect(connection); 771 if (s) { 772 if (!sock.socket) { 773 sock.socket = s; 774 send_first_packet(connection, &sock, P_INITIAL_DATA); 775 } else if (!msock.socket) { 776 clear_bit(RESOLVE_CONFLICTS, &connection->flags); 777 msock.socket = s; 778 send_first_packet(connection, &msock, P_INITIAL_META); 779 } else { 780 drbd_err(connection, "Logic error in conn_connect()\n"); 781 goto out_release_sockets; 782 } 783 } 784 785 if (connection_established(connection, &sock.socket, &msock.socket)) 786 break; 787 788 retry: 789 s = drbd_wait_for_connect(connection, &ad); 790 if (s) { 791 int fp = receive_first_packet(connection, s); 792 drbd_socket_okay(&sock.socket); 793 drbd_socket_okay(&msock.socket); 794 switch (fp) { 795 case P_INITIAL_DATA: 796 if (sock.socket) { 797 drbd_warn(connection, "initial packet S crossed\n"); 798 sock_release(sock.socket); 799 sock.socket = s; 800 goto randomize; 801 } 802 sock.socket = s; 803 break; 804 case P_INITIAL_META: 805 set_bit(RESOLVE_CONFLICTS, &connection->flags); 806 if (msock.socket) { 807 drbd_warn(connection, "initial packet M crossed\n"); 808 sock_release(msock.socket); 809 msock.socket = s; 810 goto randomize; 811 } 812 msock.socket = s; 813 break; 814 default: 815 drbd_warn(connection, "Error receiving initial packet\n"); 816 sock_release(s); 817 randomize: 818 if (get_random_u32_below(2)) 819 goto retry; 820 } 821 } 822 823 if (connection->cstate <= C_DISCONNECTING) 824 goto out_release_sockets; 825 if (signal_pending(current)) { 826 flush_signals(current); 827 smp_rmb(); 828 if (get_t_state(&connection->receiver) == EXITING) 829 goto out_release_sockets; 830 } 831 832 ok = connection_established(connection, &sock.socket, &msock.socket); 833 } while (!ok); 834 835 if (ad.s_listen) 836 sock_release(ad.s_listen); 837 838 sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ 839 msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */ 840 841 sock.socket->sk->sk_allocation = GFP_NOIO; 842 msock.socket->sk->sk_allocation = GFP_NOIO; 843 844 sock.socket->sk->sk_use_task_frag = false; 845 msock.socket->sk->sk_use_task_frag = false; 846 847 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK; 848 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE; 849 850 /* NOT YET ... 851 * sock.socket->sk->sk_sndtimeo = connection->net_conf->timeout*HZ/10; 852 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; 853 * first set it to the P_CONNECTION_FEATURES timeout, 854 * which we set to 4x the configured ping_timeout. */ 855 rcu_read_lock(); 856 nc = rcu_dereference(connection->net_conf); 857 858 sock.socket->sk->sk_sndtimeo = 859 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10; 860 861 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ; 862 timeout = nc->timeout * HZ / 10; 863 discard_my_data = nc->discard_my_data; 864 rcu_read_unlock(); 865 866 msock.socket->sk->sk_sndtimeo = timeout; 867 868 /* we don't want delays. 869 * we use TCP_CORK where appropriate, though */ 870 tcp_sock_set_nodelay(sock.socket->sk); 871 tcp_sock_set_nodelay(msock.socket->sk); 872 873 connection->data.socket = sock.socket; 874 connection->meta.socket = msock.socket; 875 connection->last_received = jiffies; 876 877 h = drbd_do_features(connection); 878 if (h <= 0) 879 return h; 880 881 if (connection->cram_hmac_tfm) { 882 /* drbd_request_state(device, NS(conn, WFAuth)); */ 883 switch (drbd_do_auth(connection)) { 884 case -1: 885 drbd_err(connection, "Authentication of peer failed\n"); 886 return -1; 887 case 0: 888 drbd_err(connection, "Authentication of peer failed, trying again.\n"); 889 return 0; 890 } 891 } 892 893 connection->data.socket->sk->sk_sndtimeo = timeout; 894 connection->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; 895 896 if (drbd_send_protocol(connection) == -EOPNOTSUPP) 897 return -1; 898 899 /* Prevent a race between resync-handshake and 900 * being promoted to Primary. 901 * 902 * Grab and release the state mutex, so we know that any current 903 * drbd_set_role() is finished, and any incoming drbd_set_role 904 * will see the STATE_SENT flag, and wait for it to be cleared. 905 */ 906 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) 907 mutex_lock(peer_device->device->state_mutex); 908 909 /* avoid a race with conn_request_state( C_DISCONNECTING ) */ 910 spin_lock_irq(&connection->resource->req_lock); 911 set_bit(STATE_SENT, &connection->flags); 912 spin_unlock_irq(&connection->resource->req_lock); 913 914 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) 915 mutex_unlock(peer_device->device->state_mutex); 916 917 rcu_read_lock(); 918 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 919 struct drbd_device *device = peer_device->device; 920 kref_get(&device->kref); 921 rcu_read_unlock(); 922 923 if (discard_my_data) 924 set_bit(DISCARD_MY_DATA, &device->flags); 925 else 926 clear_bit(DISCARD_MY_DATA, &device->flags); 927 928 drbd_connected(peer_device); 929 kref_put(&device->kref, drbd_destroy_device); 930 rcu_read_lock(); 931 } 932 rcu_read_unlock(); 933 934 rv = conn_request_state(connection, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE); 935 if (rv < SS_SUCCESS || connection->cstate != C_WF_REPORT_PARAMS) { 936 clear_bit(STATE_SENT, &connection->flags); 937 return 0; 938 } 939 940 drbd_thread_start(&connection->ack_receiver); 941 /* opencoded create_singlethread_workqueue(), 942 * to be able to use format string arguments */ 943 connection->ack_sender = 944 alloc_ordered_workqueue("drbd_as_%s", WQ_MEM_RECLAIM, connection->resource->name); 945 if (!connection->ack_sender) { 946 drbd_err(connection, "Failed to create workqueue ack_sender\n"); 947 return 0; 948 } 949 950 mutex_lock(&connection->resource->conf_update); 951 /* The discard_my_data flag is a single-shot modifier to the next 952 * connection attempt, the handshake of which is now well underway. 953 * No need for rcu style copying of the whole struct 954 * just to clear a single value. */ 955 connection->net_conf->discard_my_data = 0; 956 mutex_unlock(&connection->resource->conf_update); 957 958 return h; 959 960 out_release_sockets: 961 if (ad.s_listen) 962 sock_release(ad.s_listen); 963 if (sock.socket) 964 sock_release(sock.socket); 965 if (msock.socket) 966 sock_release(msock.socket); 967 return -1; 968 } 969 970 static int decode_header(struct drbd_connection *connection, void *header, struct packet_info *pi) 971 { 972 unsigned int header_size = drbd_header_size(connection); 973 974 if (header_size == sizeof(struct p_header100) && 975 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) { 976 struct p_header100 *h = header; 977 if (h->pad != 0) { 978 drbd_err(connection, "Header padding is not zero\n"); 979 return -EINVAL; 980 } 981 pi->vnr = be16_to_cpu(h->volume); 982 pi->cmd = be16_to_cpu(h->command); 983 pi->size = be32_to_cpu(h->length); 984 } else if (header_size == sizeof(struct p_header95) && 985 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) { 986 struct p_header95 *h = header; 987 pi->cmd = be16_to_cpu(h->command); 988 pi->size = be32_to_cpu(h->length); 989 pi->vnr = 0; 990 } else if (header_size == sizeof(struct p_header80) && 991 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) { 992 struct p_header80 *h = header; 993 pi->cmd = be16_to_cpu(h->command); 994 pi->size = be16_to_cpu(h->length); 995 pi->vnr = 0; 996 } else { 997 drbd_err(connection, "Wrong magic value 0x%08x in protocol version %d\n", 998 be32_to_cpu(*(__be32 *)header), 999 connection->agreed_pro_version); 1000 return -EINVAL; 1001 } 1002 pi->data = header + header_size; 1003 return 0; 1004 } 1005 1006 static void drbd_unplug_all_devices(struct drbd_connection *connection) 1007 { 1008 if (current->plug == &connection->receiver_plug) { 1009 blk_finish_plug(&connection->receiver_plug); 1010 blk_start_plug(&connection->receiver_plug); 1011 } /* else: maybe just schedule() ?? */ 1012 } 1013 1014 static int drbd_recv_header(struct drbd_connection *connection, struct packet_info *pi) 1015 { 1016 void *buffer = connection->data.rbuf; 1017 int err; 1018 1019 err = drbd_recv_all_warn(connection, buffer, drbd_header_size(connection)); 1020 if (err) 1021 return err; 1022 1023 err = decode_header(connection, buffer, pi); 1024 connection->last_received = jiffies; 1025 1026 return err; 1027 } 1028 1029 static int drbd_recv_header_maybe_unplug(struct drbd_connection *connection, struct packet_info *pi) 1030 { 1031 void *buffer = connection->data.rbuf; 1032 unsigned int size = drbd_header_size(connection); 1033 int err; 1034 1035 err = drbd_recv_short(connection->data.socket, buffer, size, MSG_NOSIGNAL|MSG_DONTWAIT); 1036 if (err != size) { 1037 /* If we have nothing in the receive buffer now, to reduce 1038 * application latency, try to drain the backend queues as 1039 * quickly as possible, and let remote TCP know what we have 1040 * received so far. */ 1041 if (err == -EAGAIN) { 1042 tcp_sock_set_quickack(connection->data.socket->sk, 2); 1043 drbd_unplug_all_devices(connection); 1044 } 1045 if (err > 0) { 1046 buffer += err; 1047 size -= err; 1048 } 1049 err = drbd_recv_all_warn(connection, buffer, size); 1050 if (err) 1051 return err; 1052 } 1053 1054 err = decode_header(connection, connection->data.rbuf, pi); 1055 connection->last_received = jiffies; 1056 1057 return err; 1058 } 1059 /* This is blkdev_issue_flush, but asynchronous. 1060 * We want to submit to all component volumes in parallel, 1061 * then wait for all completions. 1062 */ 1063 struct issue_flush_context { 1064 atomic_t pending; 1065 int error; 1066 struct completion done; 1067 }; 1068 struct one_flush_context { 1069 struct drbd_device *device; 1070 struct issue_flush_context *ctx; 1071 }; 1072 1073 static void one_flush_endio(struct bio *bio) 1074 { 1075 struct one_flush_context *octx = bio->bi_private; 1076 struct drbd_device *device = octx->device; 1077 struct issue_flush_context *ctx = octx->ctx; 1078 1079 if (bio->bi_status) { 1080 ctx->error = blk_status_to_errno(bio->bi_status); 1081 drbd_info(device, "local disk FLUSH FAILED with status %d\n", bio->bi_status); 1082 } 1083 kfree(octx); 1084 bio_put(bio); 1085 1086 clear_bit(FLUSH_PENDING, &device->flags); 1087 put_ldev(device); 1088 kref_put(&device->kref, drbd_destroy_device); 1089 1090 if (atomic_dec_and_test(&ctx->pending)) 1091 complete(&ctx->done); 1092 } 1093 1094 static void submit_one_flush(struct drbd_device *device, struct issue_flush_context *ctx) 1095 { 1096 struct bio *bio = bio_alloc(device->ldev->backing_bdev, 0, 1097 REQ_OP_WRITE | REQ_PREFLUSH, GFP_NOIO); 1098 struct one_flush_context *octx = kmalloc_obj(*octx, GFP_NOIO); 1099 1100 if (!octx) { 1101 drbd_warn(device, "Could not allocate a octx, CANNOT ISSUE FLUSH\n"); 1102 /* FIXME: what else can I do now? disconnecting or detaching 1103 * really does not help to improve the state of the world, either. 1104 */ 1105 bio_put(bio); 1106 1107 ctx->error = -ENOMEM; 1108 put_ldev(device); 1109 kref_put(&device->kref, drbd_destroy_device); 1110 return; 1111 } 1112 1113 octx->device = device; 1114 octx->ctx = ctx; 1115 bio->bi_private = octx; 1116 bio->bi_end_io = one_flush_endio; 1117 1118 device->flush_jif = jiffies; 1119 set_bit(FLUSH_PENDING, &device->flags); 1120 atomic_inc(&ctx->pending); 1121 submit_bio(bio); 1122 } 1123 1124 static void drbd_flush(struct drbd_connection *connection) 1125 { 1126 if (connection->resource->write_ordering >= WO_BDEV_FLUSH) { 1127 struct drbd_peer_device *peer_device; 1128 struct issue_flush_context ctx; 1129 int vnr; 1130 1131 atomic_set(&ctx.pending, 1); 1132 ctx.error = 0; 1133 init_completion(&ctx.done); 1134 1135 rcu_read_lock(); 1136 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 1137 struct drbd_device *device = peer_device->device; 1138 1139 if (!get_ldev(device)) 1140 continue; 1141 kref_get(&device->kref); 1142 rcu_read_unlock(); 1143 1144 submit_one_flush(device, &ctx); 1145 1146 rcu_read_lock(); 1147 } 1148 rcu_read_unlock(); 1149 1150 /* Do we want to add a timeout, 1151 * if disk-timeout is set? */ 1152 if (!atomic_dec_and_test(&ctx.pending)) 1153 wait_for_completion(&ctx.done); 1154 1155 if (ctx.error) { 1156 /* would rather check on EOPNOTSUPP, but that is not reliable. 1157 * don't try again for ANY return value != 0 1158 * if (rv == -EOPNOTSUPP) */ 1159 /* Any error is already reported by bio_endio callback. */ 1160 drbd_bump_write_ordering(connection->resource, NULL, WO_DRAIN_IO); 1161 } 1162 } 1163 } 1164 1165 /** 1166 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it. 1167 * @connection: DRBD connection. 1168 * @epoch: Epoch object. 1169 * @ev: Epoch event. 1170 */ 1171 static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *connection, 1172 struct drbd_epoch *epoch, 1173 enum epoch_event ev) 1174 { 1175 int epoch_size; 1176 struct drbd_epoch *next_epoch; 1177 enum finish_epoch rv = FE_STILL_LIVE; 1178 1179 spin_lock(&connection->epoch_lock); 1180 do { 1181 next_epoch = NULL; 1182 1183 epoch_size = atomic_read(&epoch->epoch_size); 1184 1185 switch (ev & ~EV_CLEANUP) { 1186 case EV_PUT: 1187 atomic_dec(&epoch->active); 1188 break; 1189 case EV_GOT_BARRIER_NR: 1190 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags); 1191 break; 1192 case EV_BECAME_LAST: 1193 /* nothing to do*/ 1194 break; 1195 } 1196 1197 if (epoch_size != 0 && 1198 atomic_read(&epoch->active) == 0 && 1199 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) { 1200 if (!(ev & EV_CLEANUP)) { 1201 spin_unlock(&connection->epoch_lock); 1202 drbd_send_b_ack(epoch->connection, epoch->barrier_nr, epoch_size); 1203 spin_lock(&connection->epoch_lock); 1204 } 1205 #if 0 1206 /* FIXME: dec unacked on connection, once we have 1207 * something to count pending connection packets in. */ 1208 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) 1209 dec_unacked(epoch->connection); 1210 #endif 1211 1212 if (connection->current_epoch != epoch) { 1213 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list); 1214 list_del(&epoch->list); 1215 ev = EV_BECAME_LAST | (ev & EV_CLEANUP); 1216 connection->epochs--; 1217 kfree(epoch); 1218 1219 if (rv == FE_STILL_LIVE) 1220 rv = FE_DESTROYED; 1221 } else { 1222 epoch->flags = 0; 1223 atomic_set(&epoch->epoch_size, 0); 1224 /* atomic_set(&epoch->active, 0); is already zero */ 1225 if (rv == FE_STILL_LIVE) 1226 rv = FE_RECYCLED; 1227 } 1228 } 1229 1230 if (!next_epoch) 1231 break; 1232 1233 epoch = next_epoch; 1234 } while (1); 1235 1236 spin_unlock(&connection->epoch_lock); 1237 1238 return rv; 1239 } 1240 1241 static enum write_ordering_e 1242 max_allowed_wo(struct drbd_backing_dev *bdev, enum write_ordering_e wo) 1243 { 1244 struct disk_conf *dc; 1245 1246 dc = rcu_dereference(bdev->disk_conf); 1247 1248 if (wo == WO_BDEV_FLUSH && !dc->disk_flushes) 1249 wo = WO_DRAIN_IO; 1250 if (wo == WO_DRAIN_IO && !dc->disk_drain) 1251 wo = WO_NONE; 1252 1253 return wo; 1254 } 1255 1256 /* 1257 * drbd_bump_write_ordering() - Fall back to an other write ordering method 1258 * @wo: Write ordering method to try. 1259 */ 1260 void drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev, 1261 enum write_ordering_e wo) 1262 { 1263 struct drbd_device *device; 1264 enum write_ordering_e pwo; 1265 int vnr; 1266 static char *write_ordering_str[] = { 1267 [WO_NONE] = "none", 1268 [WO_DRAIN_IO] = "drain", 1269 [WO_BDEV_FLUSH] = "flush", 1270 }; 1271 1272 pwo = resource->write_ordering; 1273 if (wo != WO_BDEV_FLUSH) 1274 wo = min(pwo, wo); 1275 rcu_read_lock(); 1276 idr_for_each_entry(&resource->devices, device, vnr) { 1277 if (get_ldev(device)) { 1278 wo = max_allowed_wo(device->ldev, wo); 1279 if (device->ldev == bdev) 1280 bdev = NULL; 1281 put_ldev(device); 1282 } 1283 } 1284 1285 if (bdev) 1286 wo = max_allowed_wo(bdev, wo); 1287 1288 rcu_read_unlock(); 1289 1290 resource->write_ordering = wo; 1291 if (pwo != resource->write_ordering || wo == WO_BDEV_FLUSH) 1292 drbd_info(resource, "Method to ensure write ordering: %s\n", write_ordering_str[resource->write_ordering]); 1293 } 1294 1295 /* 1296 * Mapping "discard" to ZEROOUT with UNMAP does not work for us: 1297 * Drivers have to "announce" q->limits.max_write_zeroes_sectors, or it 1298 * will directly go to fallback mode, submitting normal writes, and 1299 * never even try to UNMAP. 1300 * 1301 * And dm-thin does not do this (yet), mostly because in general it has 1302 * to assume that "skip_block_zeroing" is set. See also: 1303 * https://www.mail-archive.com/dm-devel%40redhat.com/msg07965.html 1304 * https://www.redhat.com/archives/dm-devel/2018-January/msg00271.html 1305 * 1306 * We *may* ignore the discard-zeroes-data setting, if so configured. 1307 * 1308 * Assumption is that this "discard_zeroes_data=0" is only because the backend 1309 * may ignore partial unaligned discards. 1310 * 1311 * LVM/DM thin as of at least 1312 * LVM version: 2.02.115(2)-RHEL7 (2015-01-28) 1313 * Library version: 1.02.93-RHEL7 (2015-01-28) 1314 * Driver version: 4.29.0 1315 * still behaves this way. 1316 * 1317 * For unaligned (wrt. alignment and granularity) or too small discards, 1318 * we zero-out the initial (and/or) trailing unaligned partial chunks, 1319 * but discard all the aligned full chunks. 1320 * 1321 * At least for LVM/DM thin, with skip_block_zeroing=false, 1322 * the result is effectively "discard_zeroes_data=1". 1323 */ 1324 /* flags: EE_TRIM|EE_ZEROOUT */ 1325 int drbd_issue_discard_or_zero_out(struct drbd_device *device, sector_t start, unsigned int nr_sectors, int flags) 1326 { 1327 struct block_device *bdev = device->ldev->backing_bdev; 1328 sector_t tmp, nr; 1329 unsigned int max_discard_sectors, granularity; 1330 int alignment; 1331 int err = 0; 1332 1333 if ((flags & EE_ZEROOUT) || !(flags & EE_TRIM)) 1334 goto zero_out; 1335 1336 /* Zero-sector (unknown) and one-sector granularities are the same. */ 1337 granularity = max(bdev_discard_granularity(bdev) >> 9, 1U); 1338 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; 1339 1340 max_discard_sectors = min(bdev_max_discard_sectors(bdev), (1U << 22)); 1341 max_discard_sectors -= max_discard_sectors % granularity; 1342 if (unlikely(!max_discard_sectors)) 1343 goto zero_out; 1344 1345 if (nr_sectors < granularity) 1346 goto zero_out; 1347 1348 tmp = start; 1349 if (sector_div(tmp, granularity) != alignment) { 1350 if (nr_sectors < 2*granularity) 1351 goto zero_out; 1352 /* start + gran - (start + gran - align) % gran */ 1353 tmp = start + granularity - alignment; 1354 tmp = start + granularity - sector_div(tmp, granularity); 1355 1356 nr = tmp - start; 1357 /* don't flag BLKDEV_ZERO_NOUNMAP, we don't know how many 1358 * layers are below us, some may have smaller granularity */ 1359 err |= blkdev_issue_zeroout(bdev, start, nr, GFP_NOIO, 0); 1360 nr_sectors -= nr; 1361 start = tmp; 1362 } 1363 while (nr_sectors >= max_discard_sectors) { 1364 err |= blkdev_issue_discard(bdev, start, max_discard_sectors, 1365 GFP_NOIO); 1366 nr_sectors -= max_discard_sectors; 1367 start += max_discard_sectors; 1368 } 1369 if (nr_sectors) { 1370 /* max_discard_sectors is unsigned int (and a multiple of 1371 * granularity, we made sure of that above already); 1372 * nr is < max_discard_sectors; 1373 * I don't need sector_div here, even though nr is sector_t */ 1374 nr = nr_sectors; 1375 nr -= (unsigned int)nr % granularity; 1376 if (nr) { 1377 err |= blkdev_issue_discard(bdev, start, nr, GFP_NOIO); 1378 nr_sectors -= nr; 1379 start += nr; 1380 } 1381 } 1382 zero_out: 1383 if (nr_sectors) { 1384 err |= blkdev_issue_zeroout(bdev, start, nr_sectors, GFP_NOIO, 1385 (flags & EE_TRIM) ? 0 : BLKDEV_ZERO_NOUNMAP); 1386 } 1387 return err != 0; 1388 } 1389 1390 static bool can_do_reliable_discards(struct drbd_device *device) 1391 { 1392 struct disk_conf *dc; 1393 bool can_do; 1394 1395 if (!bdev_max_discard_sectors(device->ldev->backing_bdev)) 1396 return false; 1397 1398 rcu_read_lock(); 1399 dc = rcu_dereference(device->ldev->disk_conf); 1400 can_do = dc->discard_zeroes_if_aligned; 1401 rcu_read_unlock(); 1402 return can_do; 1403 } 1404 1405 static void drbd_issue_peer_discard_or_zero_out(struct drbd_device *device, struct drbd_peer_request *peer_req) 1406 { 1407 /* If the backend cannot discard, or does not guarantee 1408 * read-back zeroes in discarded ranges, we fall back to 1409 * zero-out. Unless configuration specifically requested 1410 * otherwise. */ 1411 if (!can_do_reliable_discards(device)) 1412 peer_req->flags |= EE_ZEROOUT; 1413 1414 if (drbd_issue_discard_or_zero_out(device, peer_req->i.sector, 1415 peer_req->i.size >> 9, peer_req->flags & (EE_ZEROOUT|EE_TRIM))) 1416 peer_req->flags |= EE_WAS_ERROR; 1417 drbd_endio_write_sec_final(peer_req); 1418 } 1419 1420 static int peer_request_fault_type(struct drbd_peer_request *peer_req) 1421 { 1422 if (peer_req_op(peer_req) == REQ_OP_READ) { 1423 return peer_req->flags & EE_APPLICATION ? 1424 DRBD_FAULT_DT_RD : DRBD_FAULT_RS_RD; 1425 } else { 1426 return peer_req->flags & EE_APPLICATION ? 1427 DRBD_FAULT_DT_WR : DRBD_FAULT_RS_WR; 1428 } 1429 } 1430 1431 /** 1432 * drbd_submit_peer_request() 1433 * @peer_req: peer request 1434 * 1435 * May spread the pages to multiple bios, 1436 * depending on bio_add_page restrictions. 1437 * 1438 * Returns 0 if all bios have been submitted, 1439 * -ENOMEM if we could not allocate enough bios, 1440 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a 1441 * single page to an empty bio (which should never happen and likely indicates 1442 * that the lower level IO stack is in some way broken). This has been observed 1443 * on certain Xen deployments. 1444 */ 1445 /* TODO allocate from our own bio_set. */ 1446 int drbd_submit_peer_request(struct drbd_peer_request *peer_req) 1447 { 1448 struct drbd_device *device = peer_req->peer_device->device; 1449 struct bio *bios = NULL; 1450 struct bio *bio; 1451 struct page *page = peer_req->pages; 1452 sector_t sector = peer_req->i.sector; 1453 unsigned int data_size = peer_req->i.size; 1454 unsigned int n_bios = 0; 1455 unsigned int nr_pages = PFN_UP(data_size); 1456 1457 /* TRIM/DISCARD: for now, always use the helper function 1458 * blkdev_issue_zeroout(..., discard=true). 1459 * It's synchronous, but it does the right thing wrt. bio splitting. 1460 * Correctness first, performance later. Next step is to code an 1461 * asynchronous variant of the same. 1462 */ 1463 if (peer_req->flags & (EE_TRIM | EE_ZEROOUT)) { 1464 /* wait for all pending IO completions, before we start 1465 * zeroing things out. */ 1466 conn_wait_active_ee_empty(peer_req->peer_device->connection); 1467 /* add it to the active list now, 1468 * so we can find it to present it in debugfs */ 1469 peer_req->submit_jif = jiffies; 1470 peer_req->flags |= EE_SUBMITTED; 1471 1472 /* If this was a resync request from receive_rs_deallocated(), 1473 * it is already on the sync_ee list */ 1474 if (list_empty(&peer_req->w.list)) { 1475 spin_lock_irq(&device->resource->req_lock); 1476 list_add_tail(&peer_req->w.list, &device->active_ee); 1477 spin_unlock_irq(&device->resource->req_lock); 1478 } 1479 1480 drbd_issue_peer_discard_or_zero_out(device, peer_req); 1481 return 0; 1482 } 1483 1484 /* In most cases, we will only need one bio. But in case the lower 1485 * level restrictions happen to be different at this offset on this 1486 * side than those of the sending peer, we may need to submit the 1487 * request in more than one bio. 1488 * 1489 * Plain bio_alloc is good enough here, this is no DRBD internally 1490 * generated bio, but a bio allocated on behalf of the peer. 1491 */ 1492 next_bio: 1493 /* _DISCARD, _WRITE_ZEROES handled above. 1494 * REQ_OP_FLUSH (empty flush) not expected, 1495 * should have been mapped to a "drbd protocol barrier". 1496 * REQ_OP_SECURE_ERASE: I don't see how we could ever support that. 1497 */ 1498 if (!(peer_req_op(peer_req) == REQ_OP_WRITE || 1499 peer_req_op(peer_req) == REQ_OP_READ)) { 1500 drbd_err(device, "Invalid bio op received: 0x%x\n", peer_req->opf); 1501 return -EINVAL; 1502 } 1503 1504 bio = bio_alloc(device->ldev->backing_bdev, nr_pages, peer_req->opf, GFP_NOIO); 1505 /* > peer_req->i.sector, unless this is the first bio */ 1506 bio->bi_iter.bi_sector = sector; 1507 bio->bi_private = peer_req; 1508 bio->bi_end_io = drbd_peer_request_endio; 1509 1510 bio->bi_next = bios; 1511 bios = bio; 1512 ++n_bios; 1513 1514 page_chain_for_each(page) { 1515 unsigned len = min_t(unsigned, data_size, PAGE_SIZE); 1516 if (!bio_add_page(bio, page, len, 0)) 1517 goto next_bio; 1518 data_size -= len; 1519 sector += len >> 9; 1520 --nr_pages; 1521 } 1522 D_ASSERT(device, data_size == 0); 1523 D_ASSERT(device, page == NULL); 1524 1525 atomic_set(&peer_req->pending_bios, n_bios); 1526 /* for debugfs: update timestamp, mark as submitted */ 1527 peer_req->submit_jif = jiffies; 1528 peer_req->flags |= EE_SUBMITTED; 1529 do { 1530 bio = bios; 1531 bios = bios->bi_next; 1532 bio->bi_next = NULL; 1533 1534 drbd_submit_bio_noacct(device, peer_request_fault_type(peer_req), bio); 1535 } while (bios); 1536 return 0; 1537 } 1538 1539 static void drbd_remove_epoch_entry_interval(struct drbd_device *device, 1540 struct drbd_peer_request *peer_req) 1541 { 1542 struct drbd_interval *i = &peer_req->i; 1543 1544 drbd_remove_interval(&device->write_requests, i); 1545 drbd_clear_interval(i); 1546 1547 /* Wake up any processes waiting for this peer request to complete. */ 1548 if (i->waiting) 1549 wake_up(&device->misc_wait); 1550 } 1551 1552 static void conn_wait_active_ee_empty(struct drbd_connection *connection) 1553 { 1554 struct drbd_peer_device *peer_device; 1555 int vnr; 1556 1557 rcu_read_lock(); 1558 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 1559 struct drbd_device *device = peer_device->device; 1560 1561 kref_get(&device->kref); 1562 rcu_read_unlock(); 1563 drbd_wait_ee_list_empty(device, &device->active_ee); 1564 kref_put(&device->kref, drbd_destroy_device); 1565 rcu_read_lock(); 1566 } 1567 rcu_read_unlock(); 1568 } 1569 1570 static int receive_Barrier(struct drbd_connection *connection, struct packet_info *pi) 1571 { 1572 int rv; 1573 struct p_barrier *p = pi->data; 1574 struct drbd_epoch *epoch; 1575 1576 /* FIXME these are unacked on connection, 1577 * not a specific (peer)device. 1578 */ 1579 connection->current_epoch->barrier_nr = p->barrier; 1580 connection->current_epoch->connection = connection; 1581 rv = drbd_may_finish_epoch(connection, connection->current_epoch, EV_GOT_BARRIER_NR); 1582 1583 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from 1584 * the activity log, which means it would not be resynced in case the 1585 * R_PRIMARY crashes now. 1586 * Therefore we must send the barrier_ack after the barrier request was 1587 * completed. */ 1588 switch (connection->resource->write_ordering) { 1589 case WO_NONE: 1590 if (rv == FE_RECYCLED) 1591 return 0; 1592 1593 /* receiver context, in the writeout path of the other node. 1594 * avoid potential distributed deadlock */ 1595 epoch = kmalloc_obj(struct drbd_epoch, GFP_NOIO); 1596 if (epoch) 1597 break; 1598 else 1599 drbd_warn(connection, "Allocation of an epoch failed, slowing down\n"); 1600 fallthrough; 1601 1602 case WO_BDEV_FLUSH: 1603 case WO_DRAIN_IO: 1604 conn_wait_active_ee_empty(connection); 1605 drbd_flush(connection); 1606 1607 if (atomic_read(&connection->current_epoch->epoch_size)) { 1608 epoch = kmalloc_obj(struct drbd_epoch, GFP_NOIO); 1609 if (epoch) 1610 break; 1611 } 1612 1613 return 0; 1614 default: 1615 drbd_err(connection, "Strangeness in connection->write_ordering %d\n", 1616 connection->resource->write_ordering); 1617 return -EIO; 1618 } 1619 1620 epoch->flags = 0; 1621 atomic_set(&epoch->epoch_size, 0); 1622 atomic_set(&epoch->active, 0); 1623 1624 spin_lock(&connection->epoch_lock); 1625 if (atomic_read(&connection->current_epoch->epoch_size)) { 1626 list_add(&epoch->list, &connection->current_epoch->list); 1627 connection->current_epoch = epoch; 1628 connection->epochs++; 1629 } else { 1630 /* The current_epoch got recycled while we allocated this one... */ 1631 kfree(epoch); 1632 } 1633 spin_unlock(&connection->epoch_lock); 1634 1635 return 0; 1636 } 1637 1638 /* quick wrapper in case payload size != request_size (write same) */ 1639 static void drbd_csum_ee_size(struct crypto_shash *h, 1640 struct drbd_peer_request *r, void *d, 1641 unsigned int payload_size) 1642 { 1643 unsigned int tmp = r->i.size; 1644 r->i.size = payload_size; 1645 drbd_csum_ee(h, r, d); 1646 r->i.size = tmp; 1647 } 1648 1649 /* used from receive_RSDataReply (recv_resync_read) 1650 * and from receive_Data. 1651 * data_size: actual payload ("data in") 1652 * for normal writes that is bi_size. 1653 * for discards, that is zero. 1654 * for write same, it is logical_block_size. 1655 * both trim and write same have the bi_size ("data len to be affected") 1656 * as extra argument in the packet header. 1657 */ 1658 static struct drbd_peer_request * 1659 read_in_block(struct drbd_peer_device *peer_device, u64 id, sector_t sector, 1660 struct packet_info *pi) __must_hold(local) 1661 { 1662 struct drbd_device *device = peer_device->device; 1663 const sector_t capacity = get_capacity(device->vdisk); 1664 struct drbd_peer_request *peer_req; 1665 struct page *page; 1666 int digest_size, err; 1667 unsigned int data_size = pi->size, ds; 1668 void *dig_in = peer_device->connection->int_dig_in; 1669 void *dig_vv = peer_device->connection->int_dig_vv; 1670 unsigned long *data; 1671 struct p_trim *trim = (pi->cmd == P_TRIM) ? pi->data : NULL; 1672 struct p_trim *zeroes = (pi->cmd == P_ZEROES) ? pi->data : NULL; 1673 1674 digest_size = 0; 1675 if (!trim && peer_device->connection->peer_integrity_tfm) { 1676 digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm); 1677 /* 1678 * FIXME: Receive the incoming digest into the receive buffer 1679 * here, together with its struct p_data? 1680 */ 1681 err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size); 1682 if (err) 1683 return NULL; 1684 data_size -= digest_size; 1685 } 1686 1687 /* assume request_size == data_size, but special case trim. */ 1688 ds = data_size; 1689 if (trim) { 1690 if (!expect(peer_device, data_size == 0)) 1691 return NULL; 1692 ds = be32_to_cpu(trim->size); 1693 } else if (zeroes) { 1694 if (!expect(peer_device, data_size == 0)) 1695 return NULL; 1696 ds = be32_to_cpu(zeroes->size); 1697 } 1698 1699 if (!expect(peer_device, IS_ALIGNED(ds, 512))) 1700 return NULL; 1701 if (trim || zeroes) { 1702 if (!expect(peer_device, ds <= (DRBD_MAX_BBIO_SECTORS << 9))) 1703 return NULL; 1704 } else if (!expect(peer_device, ds <= DRBD_MAX_BIO_SIZE)) 1705 return NULL; 1706 1707 /* even though we trust out peer, 1708 * we sometimes have to double check. */ 1709 if (sector + (ds>>9) > capacity) { 1710 drbd_err(device, "request from peer beyond end of local disk: " 1711 "capacity: %llus < sector: %llus + size: %u\n", 1712 (unsigned long long)capacity, 1713 (unsigned long long)sector, ds); 1714 return NULL; 1715 } 1716 1717 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD 1718 * "criss-cross" setup, that might cause write-out on some other DRBD, 1719 * which in turn might block on the other node at this very place. */ 1720 peer_req = drbd_alloc_peer_req(peer_device, id, sector, ds, data_size, GFP_NOIO); 1721 if (!peer_req) 1722 return NULL; 1723 1724 peer_req->flags |= EE_WRITE; 1725 if (trim) { 1726 peer_req->flags |= EE_TRIM; 1727 return peer_req; 1728 } 1729 if (zeroes) { 1730 peer_req->flags |= EE_ZEROOUT; 1731 return peer_req; 1732 } 1733 1734 /* receive payload size bytes into page chain */ 1735 ds = data_size; 1736 page = peer_req->pages; 1737 page_chain_for_each(page) { 1738 unsigned len = min_t(int, ds, PAGE_SIZE); 1739 data = kmap_local_page(page); 1740 err = drbd_recv_all_warn(peer_device->connection, data, len); 1741 if (drbd_insert_fault(device, DRBD_FAULT_RECEIVE)) { 1742 drbd_err(device, "Fault injection: Corrupting data on receive\n"); 1743 data[0] = data[0] ^ (unsigned long)-1; 1744 } 1745 kunmap_local(data); 1746 if (err) { 1747 drbd_free_peer_req(device, peer_req); 1748 return NULL; 1749 } 1750 ds -= len; 1751 } 1752 1753 if (digest_size) { 1754 drbd_csum_ee_size(peer_device->connection->peer_integrity_tfm, peer_req, dig_vv, data_size); 1755 if (memcmp(dig_in, dig_vv, digest_size)) { 1756 drbd_err(device, "Digest integrity check FAILED: %llus +%u\n", 1757 (unsigned long long)sector, data_size); 1758 drbd_free_peer_req(device, peer_req); 1759 return NULL; 1760 } 1761 } 1762 device->recv_cnt += data_size >> 9; 1763 return peer_req; 1764 } 1765 1766 /* drbd_drain_block() just takes a data block 1767 * out of the socket input buffer, and discards it. 1768 */ 1769 static int drbd_drain_block(struct drbd_peer_device *peer_device, int data_size) 1770 { 1771 struct page *page; 1772 int err = 0; 1773 void *data; 1774 1775 if (!data_size) 1776 return 0; 1777 1778 page = drbd_alloc_pages(peer_device, 1, 1); 1779 1780 data = kmap_local_page(page); 1781 while (data_size) { 1782 unsigned int len = min_t(int, data_size, PAGE_SIZE); 1783 1784 err = drbd_recv_all_warn(peer_device->connection, data, len); 1785 if (err) 1786 break; 1787 data_size -= len; 1788 } 1789 kunmap_local(data); 1790 drbd_free_pages(peer_device->device, page); 1791 return err; 1792 } 1793 1794 static int recv_dless_read(struct drbd_peer_device *peer_device, struct drbd_request *req, 1795 sector_t sector, int data_size) 1796 { 1797 struct bio_vec bvec; 1798 struct bvec_iter iter; 1799 struct bio *bio; 1800 int digest_size, err, expect; 1801 void *dig_in = peer_device->connection->int_dig_in; 1802 void *dig_vv = peer_device->connection->int_dig_vv; 1803 1804 digest_size = 0; 1805 if (peer_device->connection->peer_integrity_tfm) { 1806 digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm); 1807 err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size); 1808 if (err) 1809 return err; 1810 data_size -= digest_size; 1811 } 1812 1813 if (data_size < 0) { 1814 drbd_err(peer_device, "Invalid data reply size\n"); 1815 return -EIO; 1816 } 1817 1818 /* optimistically update recv_cnt. if receiving fails below, 1819 * we disconnect anyways, and counters will be reset. */ 1820 peer_device->device->recv_cnt += data_size>>9; 1821 1822 bio = req->master_bio; 1823 D_ASSERT(peer_device->device, sector == bio->bi_iter.bi_sector); 1824 1825 bio_for_each_segment(bvec, bio, iter) { 1826 void *mapped = bvec_kmap_local(&bvec); 1827 expect = min_t(int, data_size, bvec.bv_len); 1828 err = drbd_recv_all_warn(peer_device->connection, mapped, expect); 1829 kunmap_local(mapped); 1830 if (err) 1831 return err; 1832 data_size -= expect; 1833 } 1834 1835 if (digest_size) { 1836 drbd_csum_bio(peer_device->connection->peer_integrity_tfm, bio, dig_vv); 1837 if (memcmp(dig_in, dig_vv, digest_size)) { 1838 drbd_err(peer_device, "Digest integrity check FAILED. Broken NICs?\n"); 1839 return -EINVAL; 1840 } 1841 } 1842 1843 D_ASSERT(peer_device->device, data_size == 0); 1844 return 0; 1845 } 1846 1847 /* 1848 * e_end_resync_block() is called in ack_sender context via 1849 * drbd_finish_peer_reqs(). 1850 */ 1851 static int e_end_resync_block(struct drbd_work *w, int unused) 1852 { 1853 struct drbd_peer_request *peer_req = 1854 container_of(w, struct drbd_peer_request, w); 1855 struct drbd_peer_device *peer_device = peer_req->peer_device; 1856 struct drbd_device *device = peer_device->device; 1857 sector_t sector = peer_req->i.sector; 1858 int err; 1859 1860 D_ASSERT(device, drbd_interval_empty(&peer_req->i)); 1861 1862 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { 1863 drbd_set_in_sync(peer_device, sector, peer_req->i.size); 1864 err = drbd_send_ack(peer_device, P_RS_WRITE_ACK, peer_req); 1865 } else { 1866 /* Record failure to sync */ 1867 drbd_rs_failed_io(peer_device, sector, peer_req->i.size); 1868 1869 err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req); 1870 } 1871 dec_unacked(device); 1872 1873 return err; 1874 } 1875 1876 static int recv_resync_read(struct drbd_peer_device *peer_device, sector_t sector, 1877 struct packet_info *pi) __releases(local) 1878 { 1879 struct drbd_device *device = peer_device->device; 1880 struct drbd_peer_request *peer_req; 1881 1882 peer_req = read_in_block(peer_device, ID_SYNCER, sector, pi); 1883 if (!peer_req) 1884 goto fail; 1885 1886 dec_rs_pending(peer_device); 1887 1888 inc_unacked(device); 1889 /* corresponding dec_unacked() in e_end_resync_block() 1890 * respective _drbd_clear_done_ee */ 1891 1892 peer_req->w.cb = e_end_resync_block; 1893 peer_req->opf = REQ_OP_WRITE; 1894 peer_req->submit_jif = jiffies; 1895 1896 spin_lock_irq(&device->resource->req_lock); 1897 list_add_tail(&peer_req->w.list, &device->sync_ee); 1898 spin_unlock_irq(&device->resource->req_lock); 1899 1900 atomic_add(pi->size >> 9, &device->rs_sect_ev); 1901 if (drbd_submit_peer_request(peer_req) == 0) 1902 return 0; 1903 1904 /* don't care for the reason here */ 1905 drbd_err(device, "submit failed, triggering re-connect\n"); 1906 spin_lock_irq(&device->resource->req_lock); 1907 list_del(&peer_req->w.list); 1908 spin_unlock_irq(&device->resource->req_lock); 1909 1910 drbd_free_peer_req(device, peer_req); 1911 fail: 1912 put_ldev(device); 1913 return -EIO; 1914 } 1915 1916 static struct drbd_request * 1917 find_request(struct drbd_device *device, struct rb_root *root, u64 id, 1918 sector_t sector, bool missing_ok, const char *func) 1919 { 1920 struct drbd_request *req; 1921 1922 /* Request object according to our peer */ 1923 req = (struct drbd_request *)(unsigned long)id; 1924 if (drbd_contains_interval(root, sector, &req->i) && req->i.local) 1925 return req; 1926 if (!missing_ok) { 1927 drbd_err(device, "%s: failed to find request 0x%lx, sector %llus\n", func, 1928 (unsigned long)id, (unsigned long long)sector); 1929 } 1930 return NULL; 1931 } 1932 1933 static int receive_DataReply(struct drbd_connection *connection, struct packet_info *pi) 1934 { 1935 struct drbd_peer_device *peer_device; 1936 struct drbd_device *device; 1937 struct drbd_request *req; 1938 sector_t sector; 1939 int err; 1940 struct p_data *p = pi->data; 1941 1942 peer_device = conn_peer_device(connection, pi->vnr); 1943 if (!peer_device) 1944 return -EIO; 1945 device = peer_device->device; 1946 1947 sector = be64_to_cpu(p->sector); 1948 1949 spin_lock_irq(&device->resource->req_lock); 1950 req = find_request(device, &device->read_requests, p->block_id, sector, false, __func__); 1951 spin_unlock_irq(&device->resource->req_lock); 1952 if (unlikely(!req)) 1953 return -EIO; 1954 1955 err = recv_dless_read(peer_device, req, sector, pi->size); 1956 if (!err) 1957 req_mod(req, DATA_RECEIVED, peer_device); 1958 /* else: nothing. handled from drbd_disconnect... 1959 * I don't think we may complete this just yet 1960 * in case we are "on-disconnect: freeze" */ 1961 1962 return err; 1963 } 1964 1965 static int receive_RSDataReply(struct drbd_connection *connection, struct packet_info *pi) 1966 { 1967 struct drbd_peer_device *peer_device; 1968 struct drbd_device *device; 1969 sector_t sector; 1970 int err; 1971 struct p_data *p = pi->data; 1972 1973 peer_device = conn_peer_device(connection, pi->vnr); 1974 if (!peer_device) 1975 return -EIO; 1976 device = peer_device->device; 1977 1978 sector = be64_to_cpu(p->sector); 1979 D_ASSERT(device, p->block_id == ID_SYNCER); 1980 1981 if (get_ldev(device)) { 1982 /* data is submitted to disk within recv_resync_read. 1983 * corresponding put_ldev done below on error, 1984 * or in drbd_peer_request_endio. */ 1985 err = recv_resync_read(peer_device, sector, pi); 1986 } else { 1987 if (drbd_ratelimit()) 1988 drbd_err(device, "Can not write resync data to local disk.\n"); 1989 1990 err = drbd_drain_block(peer_device, pi->size); 1991 1992 drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size); 1993 } 1994 1995 atomic_add(pi->size >> 9, &device->rs_sect_in); 1996 1997 return err; 1998 } 1999 2000 static void restart_conflicting_writes(struct drbd_device *device, 2001 sector_t sector, int size) 2002 { 2003 struct drbd_interval *i; 2004 struct drbd_request *req; 2005 2006 drbd_for_each_overlap(i, &device->write_requests, sector, size) { 2007 if (!i->local) 2008 continue; 2009 req = container_of(i, struct drbd_request, i); 2010 if (req->rq_state & RQ_LOCAL_PENDING || 2011 !(req->rq_state & RQ_POSTPONED)) 2012 continue; 2013 /* as it is RQ_POSTPONED, this will cause it to 2014 * be queued on the retry workqueue. */ 2015 __req_mod(req, CONFLICT_RESOLVED, NULL, NULL); 2016 } 2017 } 2018 2019 /* 2020 * e_end_block() is called in ack_sender context via drbd_finish_peer_reqs(). 2021 */ 2022 static int e_end_block(struct drbd_work *w, int cancel) 2023 { 2024 struct drbd_peer_request *peer_req = 2025 container_of(w, struct drbd_peer_request, w); 2026 struct drbd_peer_device *peer_device = peer_req->peer_device; 2027 struct drbd_device *device = peer_device->device; 2028 sector_t sector = peer_req->i.sector; 2029 int err = 0, pcmd; 2030 2031 if (peer_req->flags & EE_SEND_WRITE_ACK) { 2032 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) { 2033 pcmd = (device->state.conn >= C_SYNC_SOURCE && 2034 device->state.conn <= C_PAUSED_SYNC_T && 2035 peer_req->flags & EE_MAY_SET_IN_SYNC) ? 2036 P_RS_WRITE_ACK : P_WRITE_ACK; 2037 err = drbd_send_ack(peer_device, pcmd, peer_req); 2038 if (pcmd == P_RS_WRITE_ACK) 2039 drbd_set_in_sync(peer_device, sector, peer_req->i.size); 2040 } else { 2041 err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req); 2042 /* we expect it to be marked out of sync anyways... 2043 * maybe assert this? */ 2044 } 2045 dec_unacked(device); 2046 } 2047 2048 /* we delete from the conflict detection hash _after_ we sent out the 2049 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */ 2050 if (peer_req->flags & EE_IN_INTERVAL_TREE) { 2051 spin_lock_irq(&device->resource->req_lock); 2052 D_ASSERT(device, !drbd_interval_empty(&peer_req->i)); 2053 drbd_remove_epoch_entry_interval(device, peer_req); 2054 if (peer_req->flags & EE_RESTART_REQUESTS) 2055 restart_conflicting_writes(device, sector, peer_req->i.size); 2056 spin_unlock_irq(&device->resource->req_lock); 2057 } else 2058 D_ASSERT(device, drbd_interval_empty(&peer_req->i)); 2059 2060 drbd_may_finish_epoch(peer_device->connection, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0)); 2061 2062 return err; 2063 } 2064 2065 static int e_send_ack(struct drbd_work *w, enum drbd_packet ack) 2066 { 2067 struct drbd_peer_request *peer_req = 2068 container_of(w, struct drbd_peer_request, w); 2069 struct drbd_peer_device *peer_device = peer_req->peer_device; 2070 int err; 2071 2072 err = drbd_send_ack(peer_device, ack, peer_req); 2073 dec_unacked(peer_device->device); 2074 2075 return err; 2076 } 2077 2078 static int e_send_superseded(struct drbd_work *w, int unused) 2079 { 2080 return e_send_ack(w, P_SUPERSEDED); 2081 } 2082 2083 static int e_send_retry_write(struct drbd_work *w, int unused) 2084 { 2085 struct drbd_peer_request *peer_req = 2086 container_of(w, struct drbd_peer_request, w); 2087 struct drbd_connection *connection = peer_req->peer_device->connection; 2088 2089 return e_send_ack(w, connection->agreed_pro_version >= 100 ? 2090 P_RETRY_WRITE : P_SUPERSEDED); 2091 } 2092 2093 static bool seq_greater(u32 a, u32 b) 2094 { 2095 /* 2096 * We assume 32-bit wrap-around here. 2097 * For 24-bit wrap-around, we would have to shift: 2098 * a <<= 8; b <<= 8; 2099 */ 2100 return (s32)a - (s32)b > 0; 2101 } 2102 2103 static u32 seq_max(u32 a, u32 b) 2104 { 2105 return seq_greater(a, b) ? a : b; 2106 } 2107 2108 static void update_peer_seq(struct drbd_peer_device *peer_device, unsigned int peer_seq) 2109 { 2110 struct drbd_device *device = peer_device->device; 2111 unsigned int newest_peer_seq; 2112 2113 if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) { 2114 spin_lock(&device->peer_seq_lock); 2115 newest_peer_seq = seq_max(device->peer_seq, peer_seq); 2116 device->peer_seq = newest_peer_seq; 2117 spin_unlock(&device->peer_seq_lock); 2118 /* wake up only if we actually changed device->peer_seq */ 2119 if (peer_seq == newest_peer_seq) 2120 wake_up(&device->seq_wait); 2121 } 2122 } 2123 2124 static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2) 2125 { 2126 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9))); 2127 } 2128 2129 /* maybe change sync_ee into interval trees as well? */ 2130 static bool overlapping_resync_write(struct drbd_device *device, struct drbd_peer_request *peer_req) 2131 { 2132 struct drbd_peer_request *rs_req; 2133 bool rv = false; 2134 2135 spin_lock_irq(&device->resource->req_lock); 2136 list_for_each_entry(rs_req, &device->sync_ee, w.list) { 2137 if (overlaps(peer_req->i.sector, peer_req->i.size, 2138 rs_req->i.sector, rs_req->i.size)) { 2139 rv = true; 2140 break; 2141 } 2142 } 2143 spin_unlock_irq(&device->resource->req_lock); 2144 2145 return rv; 2146 } 2147 2148 /* Called from receive_Data. 2149 * Synchronize packets on sock with packets on msock. 2150 * 2151 * This is here so even when a P_DATA packet traveling via sock overtook an Ack 2152 * packet traveling on msock, they are still processed in the order they have 2153 * been sent. 2154 * 2155 * Note: we don't care for Ack packets overtaking P_DATA packets. 2156 * 2157 * In case packet_seq is larger than device->peer_seq number, there are 2158 * outstanding packets on the msock. We wait for them to arrive. 2159 * In case we are the logically next packet, we update device->peer_seq 2160 * ourselves. Correctly handles 32bit wrap around. 2161 * 2162 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second, 2163 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds 2164 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have 2165 * 1<<9 == 512 seconds aka ages for the 32bit wrap around... 2166 * 2167 * returns 0 if we may process the packet, 2168 * -ERESTARTSYS if we were interrupted (by disconnect signal). */ 2169 static int wait_for_and_update_peer_seq(struct drbd_peer_device *peer_device, const u32 peer_seq) 2170 { 2171 struct drbd_device *device = peer_device->device; 2172 DEFINE_WAIT(wait); 2173 long timeout; 2174 int ret = 0, tp; 2175 2176 if (!test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) 2177 return 0; 2178 2179 spin_lock(&device->peer_seq_lock); 2180 for (;;) { 2181 if (!seq_greater(peer_seq - 1, device->peer_seq)) { 2182 device->peer_seq = seq_max(device->peer_seq, peer_seq); 2183 break; 2184 } 2185 2186 if (signal_pending(current)) { 2187 ret = -ERESTARTSYS; 2188 break; 2189 } 2190 2191 rcu_read_lock(); 2192 tp = rcu_dereference(peer_device->connection->net_conf)->two_primaries; 2193 rcu_read_unlock(); 2194 2195 if (!tp) 2196 break; 2197 2198 /* Only need to wait if two_primaries is enabled */ 2199 prepare_to_wait(&device->seq_wait, &wait, TASK_INTERRUPTIBLE); 2200 spin_unlock(&device->peer_seq_lock); 2201 rcu_read_lock(); 2202 timeout = rcu_dereference(peer_device->connection->net_conf)->ping_timeo*HZ/10; 2203 rcu_read_unlock(); 2204 timeout = schedule_timeout(timeout); 2205 spin_lock(&device->peer_seq_lock); 2206 if (!timeout) { 2207 ret = -ETIMEDOUT; 2208 drbd_err(device, "Timed out waiting for missing ack packets; disconnecting\n"); 2209 break; 2210 } 2211 } 2212 spin_unlock(&device->peer_seq_lock); 2213 finish_wait(&device->seq_wait, &wait); 2214 return ret; 2215 } 2216 2217 static enum req_op wire_flags_to_bio_op(u32 dpf) 2218 { 2219 if (dpf & DP_ZEROES) 2220 return REQ_OP_WRITE_ZEROES; 2221 if (dpf & DP_DISCARD) 2222 return REQ_OP_DISCARD; 2223 else 2224 return REQ_OP_WRITE; 2225 } 2226 2227 /* see also bio_flags_to_wire() */ 2228 static blk_opf_t wire_flags_to_bio(struct drbd_connection *connection, u32 dpf) 2229 { 2230 return wire_flags_to_bio_op(dpf) | 2231 (dpf & DP_RW_SYNC ? REQ_SYNC : 0) | 2232 (dpf & DP_FUA ? REQ_FUA : 0) | 2233 (dpf & DP_FLUSH ? REQ_PREFLUSH : 0); 2234 } 2235 2236 static void fail_postponed_requests(struct drbd_device *device, sector_t sector, 2237 unsigned int size) 2238 { 2239 struct drbd_peer_device *peer_device = first_peer_device(device); 2240 struct drbd_interval *i; 2241 2242 repeat: 2243 drbd_for_each_overlap(i, &device->write_requests, sector, size) { 2244 struct drbd_request *req; 2245 struct bio_and_error m; 2246 2247 if (!i->local) 2248 continue; 2249 req = container_of(i, struct drbd_request, i); 2250 if (!(req->rq_state & RQ_POSTPONED)) 2251 continue; 2252 req->rq_state &= ~RQ_POSTPONED; 2253 __req_mod(req, NEG_ACKED, peer_device, &m); 2254 spin_unlock_irq(&device->resource->req_lock); 2255 if (m.bio) 2256 complete_master_bio(device, &m); 2257 spin_lock_irq(&device->resource->req_lock); 2258 goto repeat; 2259 } 2260 } 2261 2262 static int handle_write_conflicts(struct drbd_device *device, 2263 struct drbd_peer_request *peer_req) 2264 { 2265 struct drbd_connection *connection = peer_req->peer_device->connection; 2266 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &connection->flags); 2267 sector_t sector = peer_req->i.sector; 2268 const unsigned int size = peer_req->i.size; 2269 struct drbd_interval *i; 2270 bool equal; 2271 int err; 2272 2273 /* 2274 * Inserting the peer request into the write_requests tree will prevent 2275 * new conflicting local requests from being added. 2276 */ 2277 drbd_insert_interval(&device->write_requests, &peer_req->i); 2278 2279 repeat: 2280 drbd_for_each_overlap(i, &device->write_requests, sector, size) { 2281 if (i == &peer_req->i) 2282 continue; 2283 if (i->completed) 2284 continue; 2285 2286 if (!i->local) { 2287 /* 2288 * Our peer has sent a conflicting remote request; this 2289 * should not happen in a two-node setup. Wait for the 2290 * earlier peer request to complete. 2291 */ 2292 err = drbd_wait_misc(device, i); 2293 if (err) 2294 goto out; 2295 goto repeat; 2296 } 2297 2298 equal = i->sector == sector && i->size == size; 2299 if (resolve_conflicts) { 2300 /* 2301 * If the peer request is fully contained within the 2302 * overlapping request, it can be considered overwritten 2303 * and thus superseded; otherwise, it will be retried 2304 * once all overlapping requests have completed. 2305 */ 2306 bool superseded = i->sector <= sector && i->sector + 2307 (i->size >> 9) >= sector + (size >> 9); 2308 2309 if (!equal) 2310 drbd_alert(device, "Concurrent writes detected: " 2311 "local=%llus +%u, remote=%llus +%u, " 2312 "assuming %s came first\n", 2313 (unsigned long long)i->sector, i->size, 2314 (unsigned long long)sector, size, 2315 superseded ? "local" : "remote"); 2316 2317 peer_req->w.cb = superseded ? e_send_superseded : 2318 e_send_retry_write; 2319 list_add_tail(&peer_req->w.list, &device->done_ee); 2320 /* put is in drbd_send_acks_wf() */ 2321 kref_get(&device->kref); 2322 if (!queue_work(connection->ack_sender, 2323 &peer_req->peer_device->send_acks_work)) 2324 kref_put(&device->kref, drbd_destroy_device); 2325 2326 err = -ENOENT; 2327 goto out; 2328 } else { 2329 struct drbd_request *req = 2330 container_of(i, struct drbd_request, i); 2331 2332 if (!equal) 2333 drbd_alert(device, "Concurrent writes detected: " 2334 "local=%llus +%u, remote=%llus +%u\n", 2335 (unsigned long long)i->sector, i->size, 2336 (unsigned long long)sector, size); 2337 2338 if (req->rq_state & RQ_LOCAL_PENDING || 2339 !(req->rq_state & RQ_POSTPONED)) { 2340 /* 2341 * Wait for the node with the discard flag to 2342 * decide if this request has been superseded 2343 * or needs to be retried. 2344 * Requests that have been superseded will 2345 * disappear from the write_requests tree. 2346 * 2347 * In addition, wait for the conflicting 2348 * request to finish locally before submitting 2349 * the conflicting peer request. 2350 */ 2351 err = drbd_wait_misc(device, &req->i); 2352 if (err) { 2353 _conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD); 2354 fail_postponed_requests(device, sector, size); 2355 goto out; 2356 } 2357 goto repeat; 2358 } 2359 /* 2360 * Remember to restart the conflicting requests after 2361 * the new peer request has completed. 2362 */ 2363 peer_req->flags |= EE_RESTART_REQUESTS; 2364 } 2365 } 2366 err = 0; 2367 2368 out: 2369 if (err) 2370 drbd_remove_epoch_entry_interval(device, peer_req); 2371 return err; 2372 } 2373 2374 /* mirrored write */ 2375 static int receive_Data(struct drbd_connection *connection, struct packet_info *pi) 2376 { 2377 struct drbd_peer_device *peer_device; 2378 struct drbd_device *device; 2379 struct net_conf *nc; 2380 sector_t sector; 2381 struct drbd_peer_request *peer_req; 2382 struct p_data *p = pi->data; 2383 u32 peer_seq = be32_to_cpu(p->seq_num); 2384 u32 dp_flags; 2385 int err, tp; 2386 2387 peer_device = conn_peer_device(connection, pi->vnr); 2388 if (!peer_device) 2389 return -EIO; 2390 device = peer_device->device; 2391 2392 if (!get_ldev(device)) { 2393 int err2; 2394 2395 err = wait_for_and_update_peer_seq(peer_device, peer_seq); 2396 drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size); 2397 atomic_inc(&connection->current_epoch->epoch_size); 2398 err2 = drbd_drain_block(peer_device, pi->size); 2399 if (!err) 2400 err = err2; 2401 return err; 2402 } 2403 2404 /* 2405 * Corresponding put_ldev done either below (on various errors), or in 2406 * drbd_peer_request_endio, if we successfully submit the data at the 2407 * end of this function. 2408 */ 2409 2410 sector = be64_to_cpu(p->sector); 2411 peer_req = read_in_block(peer_device, p->block_id, sector, pi); 2412 if (!peer_req) { 2413 put_ldev(device); 2414 return -EIO; 2415 } 2416 2417 peer_req->w.cb = e_end_block; 2418 peer_req->submit_jif = jiffies; 2419 peer_req->flags |= EE_APPLICATION; 2420 2421 dp_flags = be32_to_cpu(p->dp_flags); 2422 peer_req->opf = wire_flags_to_bio(connection, dp_flags); 2423 if (pi->cmd == P_TRIM) { 2424 D_ASSERT(peer_device, peer_req->i.size > 0); 2425 D_ASSERT(peer_device, peer_req_op(peer_req) == REQ_OP_DISCARD); 2426 D_ASSERT(peer_device, peer_req->pages == NULL); 2427 /* need to play safe: an older DRBD sender 2428 * may mean zero-out while sending P_TRIM. */ 2429 if (0 == (connection->agreed_features & DRBD_FF_WZEROES)) 2430 peer_req->flags |= EE_ZEROOUT; 2431 } else if (pi->cmd == P_ZEROES) { 2432 D_ASSERT(peer_device, peer_req->i.size > 0); 2433 D_ASSERT(peer_device, peer_req_op(peer_req) == REQ_OP_WRITE_ZEROES); 2434 D_ASSERT(peer_device, peer_req->pages == NULL); 2435 /* Do (not) pass down BLKDEV_ZERO_NOUNMAP? */ 2436 if (dp_flags & DP_DISCARD) 2437 peer_req->flags |= EE_TRIM; 2438 } else if (peer_req->pages == NULL) { 2439 D_ASSERT(device, peer_req->i.size == 0); 2440 D_ASSERT(device, dp_flags & DP_FLUSH); 2441 } 2442 2443 if (dp_flags & DP_MAY_SET_IN_SYNC) 2444 peer_req->flags |= EE_MAY_SET_IN_SYNC; 2445 2446 spin_lock(&connection->epoch_lock); 2447 peer_req->epoch = connection->current_epoch; 2448 atomic_inc(&peer_req->epoch->epoch_size); 2449 atomic_inc(&peer_req->epoch->active); 2450 spin_unlock(&connection->epoch_lock); 2451 2452 rcu_read_lock(); 2453 nc = rcu_dereference(peer_device->connection->net_conf); 2454 tp = nc->two_primaries; 2455 if (peer_device->connection->agreed_pro_version < 100) { 2456 switch (nc->wire_protocol) { 2457 case DRBD_PROT_C: 2458 dp_flags |= DP_SEND_WRITE_ACK; 2459 break; 2460 case DRBD_PROT_B: 2461 dp_flags |= DP_SEND_RECEIVE_ACK; 2462 break; 2463 } 2464 } 2465 rcu_read_unlock(); 2466 2467 if (dp_flags & DP_SEND_WRITE_ACK) { 2468 peer_req->flags |= EE_SEND_WRITE_ACK; 2469 inc_unacked(device); 2470 /* corresponding dec_unacked() in e_end_block() 2471 * respective _drbd_clear_done_ee */ 2472 } 2473 2474 if (dp_flags & DP_SEND_RECEIVE_ACK) { 2475 /* I really don't like it that the receiver thread 2476 * sends on the msock, but anyways */ 2477 drbd_send_ack(peer_device, P_RECV_ACK, peer_req); 2478 } 2479 2480 if (tp) { 2481 /* two primaries implies protocol C */ 2482 D_ASSERT(device, dp_flags & DP_SEND_WRITE_ACK); 2483 peer_req->flags |= EE_IN_INTERVAL_TREE; 2484 err = wait_for_and_update_peer_seq(peer_device, peer_seq); 2485 if (err) 2486 goto out_interrupted; 2487 spin_lock_irq(&device->resource->req_lock); 2488 err = handle_write_conflicts(device, peer_req); 2489 if (err) { 2490 spin_unlock_irq(&device->resource->req_lock); 2491 if (err == -ENOENT) { 2492 put_ldev(device); 2493 return 0; 2494 } 2495 goto out_interrupted; 2496 } 2497 } else { 2498 update_peer_seq(peer_device, peer_seq); 2499 spin_lock_irq(&device->resource->req_lock); 2500 } 2501 /* TRIM and is processed synchronously, 2502 * we wait for all pending requests, respectively wait for 2503 * active_ee to become empty in drbd_submit_peer_request(); 2504 * better not add ourselves here. */ 2505 if ((peer_req->flags & (EE_TRIM | EE_ZEROOUT)) == 0) 2506 list_add_tail(&peer_req->w.list, &device->active_ee); 2507 spin_unlock_irq(&device->resource->req_lock); 2508 2509 if (device->state.conn == C_SYNC_TARGET) 2510 wait_event(device->ee_wait, !overlapping_resync_write(device, peer_req)); 2511 2512 if (device->state.pdsk < D_INCONSISTENT) { 2513 /* In case we have the only disk of the cluster, */ 2514 drbd_set_out_of_sync(peer_device, peer_req->i.sector, peer_req->i.size); 2515 peer_req->flags &= ~EE_MAY_SET_IN_SYNC; 2516 drbd_al_begin_io(device, &peer_req->i); 2517 peer_req->flags |= EE_CALL_AL_COMPLETE_IO; 2518 } 2519 2520 err = drbd_submit_peer_request(peer_req); 2521 if (!err) 2522 return 0; 2523 2524 /* don't care for the reason here */ 2525 drbd_err(device, "submit failed, triggering re-connect\n"); 2526 spin_lock_irq(&device->resource->req_lock); 2527 list_del(&peer_req->w.list); 2528 drbd_remove_epoch_entry_interval(device, peer_req); 2529 spin_unlock_irq(&device->resource->req_lock); 2530 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO) { 2531 peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO; 2532 drbd_al_complete_io(device, &peer_req->i); 2533 } 2534 2535 out_interrupted: 2536 drbd_may_finish_epoch(connection, peer_req->epoch, EV_PUT | EV_CLEANUP); 2537 put_ldev(device); 2538 drbd_free_peer_req(device, peer_req); 2539 return err; 2540 } 2541 2542 /* We may throttle resync, if the lower device seems to be busy, 2543 * and current sync rate is above c_min_rate. 2544 * 2545 * To decide whether or not the lower device is busy, we use a scheme similar 2546 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant" 2547 * (more than 64 sectors) of activity we cannot account for with our own resync 2548 * activity, it obviously is "busy". 2549 * 2550 * The current sync rate used here uses only the most recent two step marks, 2551 * to have a short time average so we can react faster. 2552 */ 2553 bool drbd_rs_should_slow_down(struct drbd_peer_device *peer_device, sector_t sector, 2554 bool throttle_if_app_is_waiting) 2555 { 2556 struct drbd_device *device = peer_device->device; 2557 struct lc_element *tmp; 2558 bool throttle = drbd_rs_c_min_rate_throttle(device); 2559 2560 if (!throttle || throttle_if_app_is_waiting) 2561 return throttle; 2562 2563 spin_lock_irq(&device->al_lock); 2564 tmp = lc_find(device->resync, BM_SECT_TO_EXT(sector)); 2565 if (tmp) { 2566 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); 2567 if (test_bit(BME_PRIORITY, &bm_ext->flags)) 2568 throttle = false; 2569 /* Do not slow down if app IO is already waiting for this extent, 2570 * and our progress is necessary for application IO to complete. */ 2571 } 2572 spin_unlock_irq(&device->al_lock); 2573 2574 return throttle; 2575 } 2576 2577 bool drbd_rs_c_min_rate_throttle(struct drbd_device *device) 2578 { 2579 struct gendisk *disk = device->ldev->backing_bdev->bd_disk; 2580 unsigned long db, dt, dbdt; 2581 unsigned int c_min_rate; 2582 int curr_events; 2583 2584 rcu_read_lock(); 2585 c_min_rate = rcu_dereference(device->ldev->disk_conf)->c_min_rate; 2586 rcu_read_unlock(); 2587 2588 /* feature disabled? */ 2589 if (c_min_rate == 0) 2590 return false; 2591 2592 curr_events = (int)part_stat_read_accum(disk->part0, sectors) - 2593 atomic_read(&device->rs_sect_ev); 2594 2595 if (atomic_read(&device->ap_actlog_cnt) 2596 || curr_events - device->rs_last_events > 64) { 2597 unsigned long rs_left; 2598 int i; 2599 2600 device->rs_last_events = curr_events; 2601 2602 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP, 2603 * approx. */ 2604 i = (device->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS; 2605 2606 if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T) 2607 rs_left = device->ov_left; 2608 else 2609 rs_left = drbd_bm_total_weight(device) - device->rs_failed; 2610 2611 dt = ((long)jiffies - (long)device->rs_mark_time[i]) / HZ; 2612 if (!dt) 2613 dt++; 2614 db = device->rs_mark_left[i] - rs_left; 2615 dbdt = Bit2KB(db/dt); 2616 2617 if (dbdt > c_min_rate) 2618 return true; 2619 } 2620 return false; 2621 } 2622 2623 static int receive_DataRequest(struct drbd_connection *connection, struct packet_info *pi) 2624 { 2625 struct drbd_peer_device *peer_device; 2626 struct drbd_device *device; 2627 sector_t sector; 2628 sector_t capacity; 2629 struct drbd_peer_request *peer_req; 2630 struct digest_info *di = NULL; 2631 int size, verb; 2632 struct p_block_req *p = pi->data; 2633 2634 peer_device = conn_peer_device(connection, pi->vnr); 2635 if (!peer_device) 2636 return -EIO; 2637 device = peer_device->device; 2638 capacity = get_capacity(device->vdisk); 2639 2640 sector = be64_to_cpu(p->sector); 2641 size = be32_to_cpu(p->blksize); 2642 2643 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { 2644 drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, 2645 (unsigned long long)sector, size); 2646 return -EINVAL; 2647 } 2648 if (sector + (size>>9) > capacity) { 2649 drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__, 2650 (unsigned long long)sector, size); 2651 return -EINVAL; 2652 } 2653 2654 if (!get_ldev_if_state(device, D_UP_TO_DATE)) { 2655 verb = 1; 2656 switch (pi->cmd) { 2657 case P_DATA_REQUEST: 2658 drbd_send_ack_rp(peer_device, P_NEG_DREPLY, p); 2659 break; 2660 case P_RS_THIN_REQ: 2661 case P_RS_DATA_REQUEST: 2662 case P_CSUM_RS_REQUEST: 2663 case P_OV_REQUEST: 2664 drbd_send_ack_rp(peer_device, P_NEG_RS_DREPLY , p); 2665 break; 2666 case P_OV_REPLY: 2667 verb = 0; 2668 dec_rs_pending(peer_device); 2669 drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size, ID_IN_SYNC); 2670 break; 2671 default: 2672 BUG(); 2673 } 2674 if (verb && drbd_ratelimit()) 2675 drbd_err(device, "Can not satisfy peer's read request, " 2676 "no local data.\n"); 2677 2678 /* drain possibly payload */ 2679 return drbd_drain_block(peer_device, pi->size); 2680 } 2681 2682 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD 2683 * "criss-cross" setup, that might cause write-out on some other DRBD, 2684 * which in turn might block on the other node at this very place. */ 2685 peer_req = drbd_alloc_peer_req(peer_device, p->block_id, sector, size, 2686 size, GFP_NOIO); 2687 if (!peer_req) { 2688 put_ldev(device); 2689 return -ENOMEM; 2690 } 2691 peer_req->opf = REQ_OP_READ; 2692 2693 switch (pi->cmd) { 2694 case P_DATA_REQUEST: 2695 peer_req->w.cb = w_e_end_data_req; 2696 /* application IO, don't drbd_rs_begin_io */ 2697 peer_req->flags |= EE_APPLICATION; 2698 goto submit; 2699 2700 case P_RS_THIN_REQ: 2701 /* If at some point in the future we have a smart way to 2702 find out if this data block is completely deallocated, 2703 then we would do something smarter here than reading 2704 the block... */ 2705 peer_req->flags |= EE_RS_THIN_REQ; 2706 fallthrough; 2707 case P_RS_DATA_REQUEST: 2708 peer_req->w.cb = w_e_end_rsdata_req; 2709 /* used in the sector offset progress display */ 2710 device->bm_resync_fo = BM_SECT_TO_BIT(sector); 2711 break; 2712 2713 case P_OV_REPLY: 2714 case P_CSUM_RS_REQUEST: 2715 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO); 2716 if (!di) 2717 goto out_free_e; 2718 2719 di->digest_size = pi->size; 2720 di->digest = (((char *)di)+sizeof(struct digest_info)); 2721 2722 peer_req->digest = di; 2723 peer_req->flags |= EE_HAS_DIGEST; 2724 2725 if (drbd_recv_all(peer_device->connection, di->digest, pi->size)) 2726 goto out_free_e; 2727 2728 if (pi->cmd == P_CSUM_RS_REQUEST) { 2729 D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89); 2730 peer_req->w.cb = w_e_end_csum_rs_req; 2731 /* used in the sector offset progress display */ 2732 device->bm_resync_fo = BM_SECT_TO_BIT(sector); 2733 /* remember to report stats in drbd_resync_finished */ 2734 device->use_csums = true; 2735 } else if (pi->cmd == P_OV_REPLY) { 2736 /* track progress, we may need to throttle */ 2737 atomic_add(size >> 9, &device->rs_sect_in); 2738 peer_req->w.cb = w_e_end_ov_reply; 2739 dec_rs_pending(peer_device); 2740 /* drbd_rs_begin_io done when we sent this request, 2741 * but accounting still needs to be done. */ 2742 goto submit_for_resync; 2743 } 2744 break; 2745 2746 case P_OV_REQUEST: 2747 if (device->ov_start_sector == ~(sector_t)0 && 2748 peer_device->connection->agreed_pro_version >= 90) { 2749 unsigned long now = jiffies; 2750 int i; 2751 device->ov_start_sector = sector; 2752 device->ov_position = sector; 2753 device->ov_left = drbd_bm_bits(device) - BM_SECT_TO_BIT(sector); 2754 device->rs_total = device->ov_left; 2755 for (i = 0; i < DRBD_SYNC_MARKS; i++) { 2756 device->rs_mark_left[i] = device->ov_left; 2757 device->rs_mark_time[i] = now; 2758 } 2759 drbd_info(device, "Online Verify start sector: %llu\n", 2760 (unsigned long long)sector); 2761 } 2762 peer_req->w.cb = w_e_end_ov_req; 2763 break; 2764 2765 default: 2766 BUG(); 2767 } 2768 2769 /* Throttle, drbd_rs_begin_io and submit should become asynchronous 2770 * wrt the receiver, but it is not as straightforward as it may seem. 2771 * Various places in the resync start and stop logic assume resync 2772 * requests are processed in order, requeuing this on the worker thread 2773 * introduces a bunch of new code for synchronization between threads. 2774 * 2775 * Unlimited throttling before drbd_rs_begin_io may stall the resync 2776 * "forever", throttling after drbd_rs_begin_io will lock that extent 2777 * for application writes for the same time. For now, just throttle 2778 * here, where the rest of the code expects the receiver to sleep for 2779 * a while, anyways. 2780 */ 2781 2782 /* Throttle before drbd_rs_begin_io, as that locks out application IO; 2783 * this defers syncer requests for some time, before letting at least 2784 * on request through. The resync controller on the receiving side 2785 * will adapt to the incoming rate accordingly. 2786 * 2787 * We cannot throttle here if remote is Primary/SyncTarget: 2788 * we would also throttle its application reads. 2789 * In that case, throttling is done on the SyncTarget only. 2790 */ 2791 2792 /* Even though this may be a resync request, we do add to "read_ee"; 2793 * "sync_ee" is only used for resync WRITEs. 2794 * Add to list early, so debugfs can find this request 2795 * even if we have to sleep below. */ 2796 spin_lock_irq(&device->resource->req_lock); 2797 list_add_tail(&peer_req->w.list, &device->read_ee); 2798 spin_unlock_irq(&device->resource->req_lock); 2799 2800 update_receiver_timing_details(connection, drbd_rs_should_slow_down); 2801 if (device->state.peer != R_PRIMARY 2802 && drbd_rs_should_slow_down(peer_device, sector, false)) 2803 schedule_timeout_uninterruptible(HZ/10); 2804 update_receiver_timing_details(connection, drbd_rs_begin_io); 2805 if (drbd_rs_begin_io(device, sector)) 2806 goto out_free_e; 2807 2808 submit_for_resync: 2809 atomic_add(size >> 9, &device->rs_sect_ev); 2810 2811 submit: 2812 update_receiver_timing_details(connection, drbd_submit_peer_request); 2813 inc_unacked(device); 2814 if (drbd_submit_peer_request(peer_req) == 0) 2815 return 0; 2816 2817 /* don't care for the reason here */ 2818 drbd_err(device, "submit failed, triggering re-connect\n"); 2819 2820 out_free_e: 2821 spin_lock_irq(&device->resource->req_lock); 2822 list_del(&peer_req->w.list); 2823 spin_unlock_irq(&device->resource->req_lock); 2824 /* no drbd_rs_complete_io(), we are dropping the connection anyways */ 2825 2826 put_ldev(device); 2827 drbd_free_peer_req(device, peer_req); 2828 return -EIO; 2829 } 2830 2831 /* 2832 * drbd_asb_recover_0p - Recover after split-brain with no remaining primaries 2833 */ 2834 static int drbd_asb_recover_0p(struct drbd_peer_device *peer_device) __must_hold(local) 2835 { 2836 struct drbd_device *device = peer_device->device; 2837 int self, peer, rv = -100; 2838 unsigned long ch_self, ch_peer; 2839 enum drbd_after_sb_p after_sb_0p; 2840 2841 self = device->ldev->md.uuid[UI_BITMAP] & 1; 2842 peer = device->p_uuid[UI_BITMAP] & 1; 2843 2844 ch_peer = device->p_uuid[UI_SIZE]; 2845 ch_self = device->comm_bm_set; 2846 2847 rcu_read_lock(); 2848 after_sb_0p = rcu_dereference(peer_device->connection->net_conf)->after_sb_0p; 2849 rcu_read_unlock(); 2850 switch (after_sb_0p) { 2851 case ASB_CONSENSUS: 2852 case ASB_DISCARD_SECONDARY: 2853 case ASB_CALL_HELPER: 2854 case ASB_VIOLENTLY: 2855 drbd_err(device, "Configuration error.\n"); 2856 break; 2857 case ASB_DISCONNECT: 2858 break; 2859 case ASB_DISCARD_YOUNGER_PRI: 2860 if (self == 0 && peer == 1) { 2861 rv = -1; 2862 break; 2863 } 2864 if (self == 1 && peer == 0) { 2865 rv = 1; 2866 break; 2867 } 2868 fallthrough; /* to one of the other strategies */ 2869 case ASB_DISCARD_OLDER_PRI: 2870 if (self == 0 && peer == 1) { 2871 rv = 1; 2872 break; 2873 } 2874 if (self == 1 && peer == 0) { 2875 rv = -1; 2876 break; 2877 } 2878 /* Else fall through to one of the other strategies... */ 2879 drbd_warn(device, "Discard younger/older primary did not find a decision\n" 2880 "Using discard-least-changes instead\n"); 2881 fallthrough; 2882 case ASB_DISCARD_ZERO_CHG: 2883 if (ch_peer == 0 && ch_self == 0) { 2884 rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) 2885 ? -1 : 1; 2886 break; 2887 } else { 2888 if (ch_peer == 0) { rv = 1; break; } 2889 if (ch_self == 0) { rv = -1; break; } 2890 } 2891 if (after_sb_0p == ASB_DISCARD_ZERO_CHG) 2892 break; 2893 fallthrough; 2894 case ASB_DISCARD_LEAST_CHG: 2895 if (ch_self < ch_peer) 2896 rv = -1; 2897 else if (ch_self > ch_peer) 2898 rv = 1; 2899 else /* ( ch_self == ch_peer ) */ 2900 /* Well, then use something else. */ 2901 rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) 2902 ? -1 : 1; 2903 break; 2904 case ASB_DISCARD_LOCAL: 2905 rv = -1; 2906 break; 2907 case ASB_DISCARD_REMOTE: 2908 rv = 1; 2909 } 2910 2911 return rv; 2912 } 2913 2914 /* 2915 * drbd_asb_recover_1p - Recover after split-brain with one remaining primary 2916 */ 2917 static int drbd_asb_recover_1p(struct drbd_peer_device *peer_device) __must_hold(local) 2918 { 2919 struct drbd_device *device = peer_device->device; 2920 int hg, rv = -100; 2921 enum drbd_after_sb_p after_sb_1p; 2922 2923 rcu_read_lock(); 2924 after_sb_1p = rcu_dereference(peer_device->connection->net_conf)->after_sb_1p; 2925 rcu_read_unlock(); 2926 switch (after_sb_1p) { 2927 case ASB_DISCARD_YOUNGER_PRI: 2928 case ASB_DISCARD_OLDER_PRI: 2929 case ASB_DISCARD_LEAST_CHG: 2930 case ASB_DISCARD_LOCAL: 2931 case ASB_DISCARD_REMOTE: 2932 case ASB_DISCARD_ZERO_CHG: 2933 drbd_err(device, "Configuration error.\n"); 2934 break; 2935 case ASB_DISCONNECT: 2936 break; 2937 case ASB_CONSENSUS: 2938 hg = drbd_asb_recover_0p(peer_device); 2939 if (hg == -1 && device->state.role == R_SECONDARY) 2940 rv = hg; 2941 if (hg == 1 && device->state.role == R_PRIMARY) 2942 rv = hg; 2943 break; 2944 case ASB_VIOLENTLY: 2945 rv = drbd_asb_recover_0p(peer_device); 2946 break; 2947 case ASB_DISCARD_SECONDARY: 2948 return device->state.role == R_PRIMARY ? 1 : -1; 2949 case ASB_CALL_HELPER: 2950 hg = drbd_asb_recover_0p(peer_device); 2951 if (hg == -1 && device->state.role == R_PRIMARY) { 2952 enum drbd_state_rv rv2; 2953 2954 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, 2955 * we might be here in C_WF_REPORT_PARAMS which is transient. 2956 * we do not need to wait for the after state change work either. */ 2957 rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY)); 2958 if (rv2 != SS_SUCCESS) { 2959 drbd_khelper(device, "pri-lost-after-sb"); 2960 } else { 2961 drbd_warn(device, "Successfully gave up primary role.\n"); 2962 rv = hg; 2963 } 2964 } else 2965 rv = hg; 2966 } 2967 2968 return rv; 2969 } 2970 2971 /* 2972 * drbd_asb_recover_2p - Recover after split-brain with two remaining primaries 2973 */ 2974 static int drbd_asb_recover_2p(struct drbd_peer_device *peer_device) __must_hold(local) 2975 { 2976 struct drbd_device *device = peer_device->device; 2977 int hg, rv = -100; 2978 enum drbd_after_sb_p after_sb_2p; 2979 2980 rcu_read_lock(); 2981 after_sb_2p = rcu_dereference(peer_device->connection->net_conf)->after_sb_2p; 2982 rcu_read_unlock(); 2983 switch (after_sb_2p) { 2984 case ASB_DISCARD_YOUNGER_PRI: 2985 case ASB_DISCARD_OLDER_PRI: 2986 case ASB_DISCARD_LEAST_CHG: 2987 case ASB_DISCARD_LOCAL: 2988 case ASB_DISCARD_REMOTE: 2989 case ASB_CONSENSUS: 2990 case ASB_DISCARD_SECONDARY: 2991 case ASB_DISCARD_ZERO_CHG: 2992 drbd_err(device, "Configuration error.\n"); 2993 break; 2994 case ASB_VIOLENTLY: 2995 rv = drbd_asb_recover_0p(peer_device); 2996 break; 2997 case ASB_DISCONNECT: 2998 break; 2999 case ASB_CALL_HELPER: 3000 hg = drbd_asb_recover_0p(peer_device); 3001 if (hg == -1) { 3002 enum drbd_state_rv rv2; 3003 3004 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE, 3005 * we might be here in C_WF_REPORT_PARAMS which is transient. 3006 * we do not need to wait for the after state change work either. */ 3007 rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY)); 3008 if (rv2 != SS_SUCCESS) { 3009 drbd_khelper(device, "pri-lost-after-sb"); 3010 } else { 3011 drbd_warn(device, "Successfully gave up primary role.\n"); 3012 rv = hg; 3013 } 3014 } else 3015 rv = hg; 3016 } 3017 3018 return rv; 3019 } 3020 3021 static void drbd_uuid_dump(struct drbd_device *device, char *text, u64 *uuid, 3022 u64 bits, u64 flags) 3023 { 3024 if (!uuid) { 3025 drbd_info(device, "%s uuid info vanished while I was looking!\n", text); 3026 return; 3027 } 3028 drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n", 3029 text, 3030 (unsigned long long)uuid[UI_CURRENT], 3031 (unsigned long long)uuid[UI_BITMAP], 3032 (unsigned long long)uuid[UI_HISTORY_START], 3033 (unsigned long long)uuid[UI_HISTORY_END], 3034 (unsigned long long)bits, 3035 (unsigned long long)flags); 3036 } 3037 3038 /* 3039 100 after split brain try auto recover 3040 2 C_SYNC_SOURCE set BitMap 3041 1 C_SYNC_SOURCE use BitMap 3042 0 no Sync 3043 -1 C_SYNC_TARGET use BitMap 3044 -2 C_SYNC_TARGET set BitMap 3045 -100 after split brain, disconnect 3046 -1000 unrelated data 3047 -1091 requires proto 91 3048 -1096 requires proto 96 3049 */ 3050 3051 static int drbd_uuid_compare(struct drbd_peer_device *const peer_device, 3052 enum drbd_role const peer_role, int *rule_nr) __must_hold(local) 3053 { 3054 struct drbd_connection *const connection = peer_device->connection; 3055 struct drbd_device *device = peer_device->device; 3056 u64 self, peer; 3057 int i, j; 3058 3059 self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1); 3060 peer = device->p_uuid[UI_CURRENT] & ~((u64)1); 3061 3062 *rule_nr = 10; 3063 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED) 3064 return 0; 3065 3066 *rule_nr = 20; 3067 if ((self == UUID_JUST_CREATED || self == (u64)0) && 3068 peer != UUID_JUST_CREATED) 3069 return -2; 3070 3071 *rule_nr = 30; 3072 if (self != UUID_JUST_CREATED && 3073 (peer == UUID_JUST_CREATED || peer == (u64)0)) 3074 return 2; 3075 3076 if (self == peer) { 3077 int rct, dc; /* roles at crash time */ 3078 3079 if (device->p_uuid[UI_BITMAP] == (u64)0 && device->ldev->md.uuid[UI_BITMAP] != (u64)0) { 3080 3081 if (connection->agreed_pro_version < 91) 3082 return -1091; 3083 3084 if ((device->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) && 3085 (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) { 3086 drbd_info(device, "was SyncSource, missed the resync finished event, corrected myself:\n"); 3087 drbd_uuid_move_history(device); 3088 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP]; 3089 device->ldev->md.uuid[UI_BITMAP] = 0; 3090 3091 drbd_uuid_dump(device, "self", device->ldev->md.uuid, 3092 device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0); 3093 *rule_nr = 34; 3094 } else { 3095 drbd_info(device, "was SyncSource (peer failed to write sync_uuid)\n"); 3096 *rule_nr = 36; 3097 } 3098 3099 return 1; 3100 } 3101 3102 if (device->ldev->md.uuid[UI_BITMAP] == (u64)0 && device->p_uuid[UI_BITMAP] != (u64)0) { 3103 3104 if (connection->agreed_pro_version < 91) 3105 return -1091; 3106 3107 if ((device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_BITMAP] & ~((u64)1)) && 3108 (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1))) { 3109 drbd_info(device, "was SyncTarget, peer missed the resync finished event, corrected peer:\n"); 3110 3111 device->p_uuid[UI_HISTORY_START + 1] = device->p_uuid[UI_HISTORY_START]; 3112 device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_BITMAP]; 3113 device->p_uuid[UI_BITMAP] = 0UL; 3114 3115 drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); 3116 *rule_nr = 35; 3117 } else { 3118 drbd_info(device, "was SyncTarget (failed to write sync_uuid)\n"); 3119 *rule_nr = 37; 3120 } 3121 3122 return -1; 3123 } 3124 3125 /* Common power [off|failure] */ 3126 rct = (test_bit(CRASHED_PRIMARY, &device->flags) ? 1 : 0) + 3127 (device->p_uuid[UI_FLAGS] & 2); 3128 /* lowest bit is set when we were primary, 3129 * next bit (weight 2) is set when peer was primary */ 3130 *rule_nr = 40; 3131 3132 /* Neither has the "crashed primary" flag set, 3133 * only a replication link hickup. */ 3134 if (rct == 0) 3135 return 0; 3136 3137 /* Current UUID equal and no bitmap uuid; does not necessarily 3138 * mean this was a "simultaneous hard crash", maybe IO was 3139 * frozen, so no UUID-bump happened. 3140 * This is a protocol change, overload DRBD_FF_WSAME as flag 3141 * for "new-enough" peer DRBD version. */ 3142 if (device->state.role == R_PRIMARY || peer_role == R_PRIMARY) { 3143 *rule_nr = 41; 3144 if (!(connection->agreed_features & DRBD_FF_WSAME)) { 3145 drbd_warn(peer_device, "Equivalent unrotated UUIDs, but current primary present.\n"); 3146 return -(0x10000 | PRO_VERSION_MAX | (DRBD_FF_WSAME << 8)); 3147 } 3148 if (device->state.role == R_PRIMARY && peer_role == R_PRIMARY) { 3149 /* At least one has the "crashed primary" bit set, 3150 * both are primary now, but neither has rotated its UUIDs? 3151 * "Can not happen." */ 3152 drbd_err(peer_device, "Equivalent unrotated UUIDs, but both are primary. Can not resolve this.\n"); 3153 return -100; 3154 } 3155 if (device->state.role == R_PRIMARY) 3156 return 1; 3157 return -1; 3158 } 3159 3160 /* Both are secondary. 3161 * Really looks like recovery from simultaneous hard crash. 3162 * Check which had been primary before, and arbitrate. */ 3163 switch (rct) { 3164 case 0: /* !self_pri && !peer_pri */ return 0; /* already handled */ 3165 case 1: /* self_pri && !peer_pri */ return 1; 3166 case 2: /* !self_pri && peer_pri */ return -1; 3167 case 3: /* self_pri && peer_pri */ 3168 dc = test_bit(RESOLVE_CONFLICTS, &connection->flags); 3169 return dc ? -1 : 1; 3170 } 3171 } 3172 3173 *rule_nr = 50; 3174 peer = device->p_uuid[UI_BITMAP] & ~((u64)1); 3175 if (self == peer) 3176 return -1; 3177 3178 *rule_nr = 51; 3179 peer = device->p_uuid[UI_HISTORY_START] & ~((u64)1); 3180 if (self == peer) { 3181 if (connection->agreed_pro_version < 96 ? 3182 (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == 3183 (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) : 3184 peer + UUID_NEW_BM_OFFSET == (device->p_uuid[UI_BITMAP] & ~((u64)1))) { 3185 /* The last P_SYNC_UUID did not get though. Undo the last start of 3186 resync as sync source modifications of the peer's UUIDs. */ 3187 3188 if (connection->agreed_pro_version < 91) 3189 return -1091; 3190 3191 device->p_uuid[UI_BITMAP] = device->p_uuid[UI_HISTORY_START]; 3192 device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_HISTORY_START + 1]; 3193 3194 drbd_info(device, "Lost last syncUUID packet, corrected:\n"); 3195 drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); 3196 3197 return -1; 3198 } 3199 } 3200 3201 *rule_nr = 60; 3202 self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1); 3203 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { 3204 peer = device->p_uuid[i] & ~((u64)1); 3205 if (self == peer) 3206 return -2; 3207 } 3208 3209 *rule_nr = 70; 3210 self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1); 3211 peer = device->p_uuid[UI_CURRENT] & ~((u64)1); 3212 if (self == peer) 3213 return 1; 3214 3215 *rule_nr = 71; 3216 self = device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1); 3217 if (self == peer) { 3218 if (connection->agreed_pro_version < 96 ? 3219 (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == 3220 (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) : 3221 self + UUID_NEW_BM_OFFSET == (device->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) { 3222 /* The last P_SYNC_UUID did not get though. Undo the last start of 3223 resync as sync source modifications of our UUIDs. */ 3224 3225 if (connection->agreed_pro_version < 91) 3226 return -1091; 3227 3228 __drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_HISTORY_START]); 3229 __drbd_uuid_set(device, UI_HISTORY_START, device->ldev->md.uuid[UI_HISTORY_START + 1]); 3230 3231 drbd_info(device, "Last syncUUID did not get through, corrected:\n"); 3232 drbd_uuid_dump(device, "self", device->ldev->md.uuid, 3233 device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0); 3234 3235 return 1; 3236 } 3237 } 3238 3239 3240 *rule_nr = 80; 3241 peer = device->p_uuid[UI_CURRENT] & ~((u64)1); 3242 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { 3243 self = device->ldev->md.uuid[i] & ~((u64)1); 3244 if (self == peer) 3245 return 2; 3246 } 3247 3248 *rule_nr = 90; 3249 self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1); 3250 peer = device->p_uuid[UI_BITMAP] & ~((u64)1); 3251 if (self == peer && self != ((u64)0)) 3252 return 100; 3253 3254 *rule_nr = 100; 3255 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) { 3256 self = device->ldev->md.uuid[i] & ~((u64)1); 3257 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) { 3258 peer = device->p_uuid[j] & ~((u64)1); 3259 if (self == peer) 3260 return -100; 3261 } 3262 } 3263 3264 return -1000; 3265 } 3266 3267 /* drbd_sync_handshake() returns the new conn state on success, or 3268 CONN_MASK (-1) on failure. 3269 */ 3270 static enum drbd_conns drbd_sync_handshake(struct drbd_peer_device *peer_device, 3271 enum drbd_role peer_role, 3272 enum drbd_disk_state peer_disk) __must_hold(local) 3273 { 3274 struct drbd_device *device = peer_device->device; 3275 enum drbd_conns rv = C_MASK; 3276 enum drbd_disk_state mydisk; 3277 struct net_conf *nc; 3278 int hg, rule_nr, rr_conflict, tentative, always_asbp; 3279 3280 mydisk = device->state.disk; 3281 if (mydisk == D_NEGOTIATING) 3282 mydisk = device->new_state_tmp.disk; 3283 3284 drbd_info(device, "drbd_sync_handshake:\n"); 3285 3286 spin_lock_irq(&device->ldev->md.uuid_lock); 3287 drbd_uuid_dump(device, "self", device->ldev->md.uuid, device->comm_bm_set, 0); 3288 drbd_uuid_dump(device, "peer", device->p_uuid, 3289 device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]); 3290 3291 hg = drbd_uuid_compare(peer_device, peer_role, &rule_nr); 3292 spin_unlock_irq(&device->ldev->md.uuid_lock); 3293 3294 drbd_info(device, "uuid_compare()=%d by rule %d\n", hg, rule_nr); 3295 3296 if (hg == -1000) { 3297 drbd_alert(device, "Unrelated data, aborting!\n"); 3298 return C_MASK; 3299 } 3300 if (hg < -0x10000) { 3301 int proto, fflags; 3302 hg = -hg; 3303 proto = hg & 0xff; 3304 fflags = (hg >> 8) & 0xff; 3305 drbd_alert(device, "To resolve this both sides have to support at least protocol %d and feature flags 0x%x\n", 3306 proto, fflags); 3307 return C_MASK; 3308 } 3309 if (hg < -1000) { 3310 drbd_alert(device, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000); 3311 return C_MASK; 3312 } 3313 3314 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) || 3315 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) { 3316 int f = (hg == -100) || abs(hg) == 2; 3317 hg = mydisk > D_INCONSISTENT ? 1 : -1; 3318 if (f) 3319 hg = hg*2; 3320 drbd_info(device, "Becoming sync %s due to disk states.\n", 3321 hg > 0 ? "source" : "target"); 3322 } 3323 3324 if (abs(hg) == 100) 3325 drbd_khelper(device, "initial-split-brain"); 3326 3327 rcu_read_lock(); 3328 nc = rcu_dereference(peer_device->connection->net_conf); 3329 always_asbp = nc->always_asbp; 3330 rr_conflict = nc->rr_conflict; 3331 tentative = nc->tentative; 3332 rcu_read_unlock(); 3333 3334 if (hg == 100 || (hg == -100 && always_asbp)) { 3335 int pcount = (device->state.role == R_PRIMARY) 3336 + (peer_role == R_PRIMARY); 3337 int forced = (hg == -100); 3338 3339 switch (pcount) { 3340 case 0: 3341 hg = drbd_asb_recover_0p(peer_device); 3342 break; 3343 case 1: 3344 hg = drbd_asb_recover_1p(peer_device); 3345 break; 3346 case 2: 3347 hg = drbd_asb_recover_2p(peer_device); 3348 break; 3349 } 3350 if (abs(hg) < 100) { 3351 drbd_warn(device, "Split-Brain detected, %d primaries, " 3352 "automatically solved. Sync from %s node\n", 3353 pcount, (hg < 0) ? "peer" : "this"); 3354 if (forced) { 3355 drbd_warn(device, "Doing a full sync, since" 3356 " UUIDs where ambiguous.\n"); 3357 hg = hg*2; 3358 } 3359 } 3360 } 3361 3362 if (hg == -100) { 3363 if (test_bit(DISCARD_MY_DATA, &device->flags) && !(device->p_uuid[UI_FLAGS]&1)) 3364 hg = -1; 3365 if (!test_bit(DISCARD_MY_DATA, &device->flags) && (device->p_uuid[UI_FLAGS]&1)) 3366 hg = 1; 3367 3368 if (abs(hg) < 100) 3369 drbd_warn(device, "Split-Brain detected, manually solved. " 3370 "Sync from %s node\n", 3371 (hg < 0) ? "peer" : "this"); 3372 } 3373 3374 if (hg == -100) { 3375 /* FIXME this log message is not correct if we end up here 3376 * after an attempted attach on a diskless node. 3377 * We just refuse to attach -- well, we drop the "connection" 3378 * to that disk, in a way... */ 3379 drbd_alert(device, "Split-Brain detected but unresolved, dropping connection!\n"); 3380 drbd_khelper(device, "split-brain"); 3381 return C_MASK; 3382 } 3383 3384 if (hg > 0 && mydisk <= D_INCONSISTENT) { 3385 drbd_err(device, "I shall become SyncSource, but I am inconsistent!\n"); 3386 return C_MASK; 3387 } 3388 3389 if (hg < 0 && /* by intention we do not use mydisk here. */ 3390 device->state.role == R_PRIMARY && device->state.disk >= D_CONSISTENT) { 3391 switch (rr_conflict) { 3392 case ASB_CALL_HELPER: 3393 drbd_khelper(device, "pri-lost"); 3394 fallthrough; 3395 case ASB_DISCONNECT: 3396 drbd_err(device, "I shall become SyncTarget, but I am primary!\n"); 3397 return C_MASK; 3398 case ASB_VIOLENTLY: 3399 drbd_warn(device, "Becoming SyncTarget, violating the stable-data" 3400 "assumption\n"); 3401 } 3402 } 3403 3404 if (tentative || test_bit(CONN_DRY_RUN, &peer_device->connection->flags)) { 3405 if (hg == 0) 3406 drbd_info(device, "dry-run connect: No resync, would become Connected immediately.\n"); 3407 else 3408 drbd_info(device, "dry-run connect: Would become %s, doing a %s resync.", 3409 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET), 3410 abs(hg) >= 2 ? "full" : "bit-map based"); 3411 return C_MASK; 3412 } 3413 3414 if (abs(hg) >= 2) { 3415 drbd_info(device, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n"); 3416 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write, "set_n_write from sync_handshake", 3417 BM_LOCKED_SET_ALLOWED, NULL)) 3418 return C_MASK; 3419 } 3420 3421 if (hg > 0) { /* become sync source. */ 3422 rv = C_WF_BITMAP_S; 3423 } else if (hg < 0) { /* become sync target */ 3424 rv = C_WF_BITMAP_T; 3425 } else { 3426 rv = C_CONNECTED; 3427 if (drbd_bm_total_weight(device)) { 3428 drbd_info(device, "No resync, but %lu bits in bitmap!\n", 3429 drbd_bm_total_weight(device)); 3430 } 3431 } 3432 3433 return rv; 3434 } 3435 3436 static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer) 3437 { 3438 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */ 3439 if (peer == ASB_DISCARD_REMOTE) 3440 return ASB_DISCARD_LOCAL; 3441 3442 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */ 3443 if (peer == ASB_DISCARD_LOCAL) 3444 return ASB_DISCARD_REMOTE; 3445 3446 /* everything else is valid if they are equal on both sides. */ 3447 return peer; 3448 } 3449 3450 static int receive_protocol(struct drbd_connection *connection, struct packet_info *pi) 3451 { 3452 struct p_protocol *p = pi->data; 3453 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p; 3454 int p_proto, p_discard_my_data, p_two_primaries, cf; 3455 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL; 3456 char integrity_alg[SHARED_SECRET_MAX] = ""; 3457 struct crypto_shash *peer_integrity_tfm = NULL; 3458 void *int_dig_in = NULL, *int_dig_vv = NULL; 3459 3460 p_proto = be32_to_cpu(p->protocol); 3461 p_after_sb_0p = be32_to_cpu(p->after_sb_0p); 3462 p_after_sb_1p = be32_to_cpu(p->after_sb_1p); 3463 p_after_sb_2p = be32_to_cpu(p->after_sb_2p); 3464 p_two_primaries = be32_to_cpu(p->two_primaries); 3465 cf = be32_to_cpu(p->conn_flags); 3466 p_discard_my_data = cf & CF_DISCARD_MY_DATA; 3467 3468 if (connection->agreed_pro_version >= 87) { 3469 int err; 3470 3471 if (pi->size > sizeof(integrity_alg)) 3472 return -EIO; 3473 err = drbd_recv_all(connection, integrity_alg, pi->size); 3474 if (err) 3475 return err; 3476 integrity_alg[SHARED_SECRET_MAX - 1] = 0; 3477 } 3478 3479 if (pi->cmd != P_PROTOCOL_UPDATE) { 3480 clear_bit(CONN_DRY_RUN, &connection->flags); 3481 3482 if (cf & CF_DRY_RUN) 3483 set_bit(CONN_DRY_RUN, &connection->flags); 3484 3485 rcu_read_lock(); 3486 nc = rcu_dereference(connection->net_conf); 3487 3488 if (p_proto != nc->wire_protocol) { 3489 drbd_err(connection, "incompatible %s settings\n", "protocol"); 3490 goto disconnect_rcu_unlock; 3491 } 3492 3493 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) { 3494 drbd_err(connection, "incompatible %s settings\n", "after-sb-0pri"); 3495 goto disconnect_rcu_unlock; 3496 } 3497 3498 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) { 3499 drbd_err(connection, "incompatible %s settings\n", "after-sb-1pri"); 3500 goto disconnect_rcu_unlock; 3501 } 3502 3503 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) { 3504 drbd_err(connection, "incompatible %s settings\n", "after-sb-2pri"); 3505 goto disconnect_rcu_unlock; 3506 } 3507 3508 if (p_discard_my_data && nc->discard_my_data) { 3509 drbd_err(connection, "incompatible %s settings\n", "discard-my-data"); 3510 goto disconnect_rcu_unlock; 3511 } 3512 3513 if (p_two_primaries != nc->two_primaries) { 3514 drbd_err(connection, "incompatible %s settings\n", "allow-two-primaries"); 3515 goto disconnect_rcu_unlock; 3516 } 3517 3518 if (strcmp(integrity_alg, nc->integrity_alg)) { 3519 drbd_err(connection, "incompatible %s settings\n", "data-integrity-alg"); 3520 goto disconnect_rcu_unlock; 3521 } 3522 3523 rcu_read_unlock(); 3524 } 3525 3526 if (integrity_alg[0]) { 3527 int hash_size; 3528 3529 /* 3530 * We can only change the peer data integrity algorithm 3531 * here. Changing our own data integrity algorithm 3532 * requires that we send a P_PROTOCOL_UPDATE packet at 3533 * the same time; otherwise, the peer has no way to 3534 * tell between which packets the algorithm should 3535 * change. 3536 */ 3537 3538 peer_integrity_tfm = crypto_alloc_shash(integrity_alg, 0, 0); 3539 if (IS_ERR(peer_integrity_tfm)) { 3540 peer_integrity_tfm = NULL; 3541 drbd_err(connection, "peer data-integrity-alg %s not supported\n", 3542 integrity_alg); 3543 goto disconnect; 3544 } 3545 3546 hash_size = crypto_shash_digestsize(peer_integrity_tfm); 3547 int_dig_in = kmalloc(hash_size, GFP_KERNEL); 3548 int_dig_vv = kmalloc(hash_size, GFP_KERNEL); 3549 if (!(int_dig_in && int_dig_vv)) { 3550 drbd_err(connection, "Allocation of buffers for data integrity checking failed\n"); 3551 goto disconnect; 3552 } 3553 } 3554 3555 new_net_conf = kmalloc_obj(struct net_conf); 3556 if (!new_net_conf) 3557 goto disconnect; 3558 3559 mutex_lock(&connection->data.mutex); 3560 mutex_lock(&connection->resource->conf_update); 3561 old_net_conf = connection->net_conf; 3562 *new_net_conf = *old_net_conf; 3563 3564 new_net_conf->wire_protocol = p_proto; 3565 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p); 3566 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p); 3567 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p); 3568 new_net_conf->two_primaries = p_two_primaries; 3569 3570 rcu_assign_pointer(connection->net_conf, new_net_conf); 3571 mutex_unlock(&connection->resource->conf_update); 3572 mutex_unlock(&connection->data.mutex); 3573 3574 crypto_free_shash(connection->peer_integrity_tfm); 3575 kfree(connection->int_dig_in); 3576 kfree(connection->int_dig_vv); 3577 connection->peer_integrity_tfm = peer_integrity_tfm; 3578 connection->int_dig_in = int_dig_in; 3579 connection->int_dig_vv = int_dig_vv; 3580 3581 if (strcmp(old_net_conf->integrity_alg, integrity_alg)) 3582 drbd_info(connection, "peer data-integrity-alg: %s\n", 3583 integrity_alg[0] ? integrity_alg : "(none)"); 3584 3585 kvfree_rcu_mightsleep(old_net_conf); 3586 return 0; 3587 3588 disconnect_rcu_unlock: 3589 rcu_read_unlock(); 3590 disconnect: 3591 crypto_free_shash(peer_integrity_tfm); 3592 kfree(int_dig_in); 3593 kfree(int_dig_vv); 3594 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 3595 return -EIO; 3596 } 3597 3598 /* helper function 3599 * input: alg name, feature name 3600 * return: NULL (alg name was "") 3601 * ERR_PTR(error) if something goes wrong 3602 * or the crypto hash ptr, if it worked out ok. */ 3603 static struct crypto_shash *drbd_crypto_alloc_digest_safe( 3604 const struct drbd_device *device, 3605 const char *alg, const char *name) 3606 { 3607 struct crypto_shash *tfm; 3608 3609 if (!alg[0]) 3610 return NULL; 3611 3612 tfm = crypto_alloc_shash(alg, 0, 0); 3613 if (IS_ERR(tfm)) { 3614 drbd_err(device, "Can not allocate \"%s\" as %s (reason: %ld)\n", 3615 alg, name, PTR_ERR(tfm)); 3616 return tfm; 3617 } 3618 return tfm; 3619 } 3620 3621 static int ignore_remaining_packet(struct drbd_connection *connection, struct packet_info *pi) 3622 { 3623 void *buffer = connection->data.rbuf; 3624 int size = pi->size; 3625 3626 while (size) { 3627 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE); 3628 s = drbd_recv(connection, buffer, s); 3629 if (s <= 0) { 3630 if (s < 0) 3631 return s; 3632 break; 3633 } 3634 size -= s; 3635 } 3636 if (size) 3637 return -EIO; 3638 return 0; 3639 } 3640 3641 /* 3642 * config_unknown_volume - device configuration command for unknown volume 3643 * 3644 * When a device is added to an existing connection, the node on which the 3645 * device is added first will send configuration commands to its peer but the 3646 * peer will not know about the device yet. It will warn and ignore these 3647 * commands. Once the device is added on the second node, the second node will 3648 * send the same device configuration commands, but in the other direction. 3649 * 3650 * (We can also end up here if drbd is misconfigured.) 3651 */ 3652 static int config_unknown_volume(struct drbd_connection *connection, struct packet_info *pi) 3653 { 3654 drbd_warn(connection, "%s packet received for volume %u, which is not configured locally\n", 3655 cmdname(pi->cmd), pi->vnr); 3656 return ignore_remaining_packet(connection, pi); 3657 } 3658 3659 static int receive_SyncParam(struct drbd_connection *connection, struct packet_info *pi) 3660 { 3661 struct drbd_peer_device *peer_device; 3662 struct drbd_device *device; 3663 struct p_rs_param_95 *p; 3664 unsigned int header_size, data_size, exp_max_sz; 3665 struct crypto_shash *verify_tfm = NULL; 3666 struct crypto_shash *csums_tfm = NULL; 3667 struct net_conf *old_net_conf, *new_net_conf = NULL; 3668 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL; 3669 const int apv = connection->agreed_pro_version; 3670 struct fifo_buffer *old_plan = NULL, *new_plan = NULL; 3671 unsigned int fifo_size = 0; 3672 int err; 3673 3674 peer_device = conn_peer_device(connection, pi->vnr); 3675 if (!peer_device) 3676 return config_unknown_volume(connection, pi); 3677 device = peer_device->device; 3678 3679 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param) 3680 : apv == 88 ? sizeof(struct p_rs_param) 3681 + SHARED_SECRET_MAX 3682 : apv <= 94 ? sizeof(struct p_rs_param_89) 3683 : /* apv >= 95 */ sizeof(struct p_rs_param_95); 3684 3685 if (pi->size > exp_max_sz) { 3686 drbd_err(device, "SyncParam packet too long: received %u, expected <= %u bytes\n", 3687 pi->size, exp_max_sz); 3688 return -EIO; 3689 } 3690 3691 if (apv <= 88) { 3692 header_size = sizeof(struct p_rs_param); 3693 data_size = pi->size - header_size; 3694 } else if (apv <= 94) { 3695 header_size = sizeof(struct p_rs_param_89); 3696 data_size = pi->size - header_size; 3697 D_ASSERT(device, data_size == 0); 3698 } else { 3699 header_size = sizeof(struct p_rs_param_95); 3700 data_size = pi->size - header_size; 3701 D_ASSERT(device, data_size == 0); 3702 } 3703 3704 /* initialize verify_alg and csums_alg */ 3705 p = pi->data; 3706 BUILD_BUG_ON(sizeof(p->algs) != 2 * SHARED_SECRET_MAX); 3707 memset(&p->algs, 0, sizeof(p->algs)); 3708 3709 err = drbd_recv_all(peer_device->connection, p, header_size); 3710 if (err) 3711 return err; 3712 3713 mutex_lock(&connection->resource->conf_update); 3714 old_net_conf = peer_device->connection->net_conf; 3715 if (get_ldev(device)) { 3716 new_disk_conf = kzalloc_obj(struct disk_conf); 3717 if (!new_disk_conf) { 3718 put_ldev(device); 3719 mutex_unlock(&connection->resource->conf_update); 3720 drbd_err(device, "Allocation of new disk_conf failed\n"); 3721 return -ENOMEM; 3722 } 3723 3724 old_disk_conf = device->ldev->disk_conf; 3725 *new_disk_conf = *old_disk_conf; 3726 3727 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate); 3728 } 3729 3730 if (apv >= 88) { 3731 if (apv == 88) { 3732 if (data_size > SHARED_SECRET_MAX || data_size == 0) { 3733 drbd_err(device, "verify-alg of wrong size, " 3734 "peer wants %u, accepting only up to %u byte\n", 3735 data_size, SHARED_SECRET_MAX); 3736 goto reconnect; 3737 } 3738 3739 err = drbd_recv_all(peer_device->connection, p->verify_alg, data_size); 3740 if (err) 3741 goto reconnect; 3742 /* we expect NUL terminated string */ 3743 /* but just in case someone tries to be evil */ 3744 D_ASSERT(device, p->verify_alg[data_size-1] == 0); 3745 p->verify_alg[data_size-1] = 0; 3746 3747 } else /* apv >= 89 */ { 3748 /* we still expect NUL terminated strings */ 3749 /* but just in case someone tries to be evil */ 3750 D_ASSERT(device, p->verify_alg[SHARED_SECRET_MAX-1] == 0); 3751 D_ASSERT(device, p->csums_alg[SHARED_SECRET_MAX-1] == 0); 3752 p->verify_alg[SHARED_SECRET_MAX-1] = 0; 3753 p->csums_alg[SHARED_SECRET_MAX-1] = 0; 3754 } 3755 3756 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) { 3757 if (device->state.conn == C_WF_REPORT_PARAMS) { 3758 drbd_err(device, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n", 3759 old_net_conf->verify_alg, p->verify_alg); 3760 goto disconnect; 3761 } 3762 verify_tfm = drbd_crypto_alloc_digest_safe(device, 3763 p->verify_alg, "verify-alg"); 3764 if (IS_ERR(verify_tfm)) { 3765 verify_tfm = NULL; 3766 goto disconnect; 3767 } 3768 } 3769 3770 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) { 3771 if (device->state.conn == C_WF_REPORT_PARAMS) { 3772 drbd_err(device, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n", 3773 old_net_conf->csums_alg, p->csums_alg); 3774 goto disconnect; 3775 } 3776 csums_tfm = drbd_crypto_alloc_digest_safe(device, 3777 p->csums_alg, "csums-alg"); 3778 if (IS_ERR(csums_tfm)) { 3779 csums_tfm = NULL; 3780 goto disconnect; 3781 } 3782 } 3783 3784 if (apv > 94 && new_disk_conf) { 3785 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead); 3786 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target); 3787 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target); 3788 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate); 3789 3790 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ; 3791 if (fifo_size != device->rs_plan_s->size) { 3792 new_plan = fifo_alloc(fifo_size); 3793 if (!new_plan) { 3794 drbd_err(device, "kmalloc of fifo_buffer failed"); 3795 put_ldev(device); 3796 goto disconnect; 3797 } 3798 } 3799 } 3800 3801 if (verify_tfm || csums_tfm) { 3802 new_net_conf = kzalloc_obj(struct net_conf); 3803 if (!new_net_conf) 3804 goto disconnect; 3805 3806 *new_net_conf = *old_net_conf; 3807 3808 if (verify_tfm) { 3809 strscpy(new_net_conf->verify_alg, p->verify_alg); 3810 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1; 3811 crypto_free_shash(peer_device->connection->verify_tfm); 3812 peer_device->connection->verify_tfm = verify_tfm; 3813 drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg); 3814 } 3815 if (csums_tfm) { 3816 strscpy(new_net_conf->csums_alg, p->csums_alg); 3817 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1; 3818 crypto_free_shash(peer_device->connection->csums_tfm); 3819 peer_device->connection->csums_tfm = csums_tfm; 3820 drbd_info(device, "using csums-alg: \"%s\"\n", p->csums_alg); 3821 } 3822 rcu_assign_pointer(connection->net_conf, new_net_conf); 3823 } 3824 } 3825 3826 if (new_disk_conf) { 3827 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf); 3828 put_ldev(device); 3829 } 3830 3831 if (new_plan) { 3832 old_plan = device->rs_plan_s; 3833 rcu_assign_pointer(device->rs_plan_s, new_plan); 3834 } 3835 3836 mutex_unlock(&connection->resource->conf_update); 3837 synchronize_rcu(); 3838 if (new_net_conf) 3839 kfree(old_net_conf); 3840 kfree(old_disk_conf); 3841 kfree(old_plan); 3842 3843 return 0; 3844 3845 reconnect: 3846 if (new_disk_conf) { 3847 put_ldev(device); 3848 kfree(new_disk_conf); 3849 } 3850 mutex_unlock(&connection->resource->conf_update); 3851 return -EIO; 3852 3853 disconnect: 3854 kfree(new_plan); 3855 if (new_disk_conf) { 3856 put_ldev(device); 3857 kfree(new_disk_conf); 3858 } 3859 mutex_unlock(&connection->resource->conf_update); 3860 /* just for completeness: actually not needed, 3861 * as this is not reached if csums_tfm was ok. */ 3862 crypto_free_shash(csums_tfm); 3863 /* but free the verify_tfm again, if csums_tfm did not work out */ 3864 crypto_free_shash(verify_tfm); 3865 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 3866 return -EIO; 3867 } 3868 3869 /* warn if the arguments differ by more than 12.5% */ 3870 static void warn_if_differ_considerably(struct drbd_device *device, 3871 const char *s, sector_t a, sector_t b) 3872 { 3873 sector_t d; 3874 if (a == 0 || b == 0) 3875 return; 3876 d = (a > b) ? (a - b) : (b - a); 3877 if (d > (a>>3) || d > (b>>3)) 3878 drbd_warn(device, "Considerable difference in %s: %llus vs. %llus\n", s, 3879 (unsigned long long)a, (unsigned long long)b); 3880 } 3881 3882 static int receive_sizes(struct drbd_connection *connection, struct packet_info *pi) 3883 { 3884 struct drbd_peer_device *peer_device; 3885 struct drbd_device *device; 3886 struct p_sizes *p = pi->data; 3887 struct o_qlim *o = (connection->agreed_features & DRBD_FF_WSAME) ? p->qlim : NULL; 3888 enum determine_dev_size dd = DS_UNCHANGED; 3889 sector_t p_size, p_usize, p_csize, my_usize; 3890 sector_t new_size, cur_size; 3891 int ldsc = 0; /* local disk size changed */ 3892 enum dds_flags ddsf; 3893 3894 peer_device = conn_peer_device(connection, pi->vnr); 3895 if (!peer_device) 3896 return config_unknown_volume(connection, pi); 3897 device = peer_device->device; 3898 cur_size = get_capacity(device->vdisk); 3899 3900 p_size = be64_to_cpu(p->d_size); 3901 p_usize = be64_to_cpu(p->u_size); 3902 p_csize = be64_to_cpu(p->c_size); 3903 3904 /* just store the peer's disk size for now. 3905 * we still need to figure out whether we accept that. */ 3906 device->p_size = p_size; 3907 3908 if (get_ldev(device)) { 3909 rcu_read_lock(); 3910 my_usize = rcu_dereference(device->ldev->disk_conf)->disk_size; 3911 rcu_read_unlock(); 3912 3913 warn_if_differ_considerably(device, "lower level device sizes", 3914 p_size, drbd_get_max_capacity(device->ldev)); 3915 warn_if_differ_considerably(device, "user requested size", 3916 p_usize, my_usize); 3917 3918 /* if this is the first connect, or an otherwise expected 3919 * param exchange, choose the minimum */ 3920 if (device->state.conn == C_WF_REPORT_PARAMS) 3921 p_usize = min_not_zero(my_usize, p_usize); 3922 3923 /* Never shrink a device with usable data during connect, 3924 * or "attach" on the peer. 3925 * But allow online shrinking if we are connected. */ 3926 new_size = drbd_new_dev_size(device, device->ldev, p_usize, 0); 3927 if (new_size < cur_size && 3928 device->state.disk >= D_OUTDATED && 3929 (device->state.conn < C_CONNECTED || device->state.pdsk == D_DISKLESS)) { 3930 drbd_err(device, "The peer's disk size is too small! (%llu < %llu sectors)\n", 3931 (unsigned long long)new_size, (unsigned long long)cur_size); 3932 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 3933 put_ldev(device); 3934 return -EIO; 3935 } 3936 3937 if (my_usize != p_usize) { 3938 struct disk_conf *old_disk_conf, *new_disk_conf = NULL; 3939 3940 new_disk_conf = kzalloc_obj(struct disk_conf); 3941 if (!new_disk_conf) { 3942 put_ldev(device); 3943 return -ENOMEM; 3944 } 3945 3946 mutex_lock(&connection->resource->conf_update); 3947 old_disk_conf = device->ldev->disk_conf; 3948 *new_disk_conf = *old_disk_conf; 3949 new_disk_conf->disk_size = p_usize; 3950 3951 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf); 3952 mutex_unlock(&connection->resource->conf_update); 3953 kvfree_rcu_mightsleep(old_disk_conf); 3954 3955 drbd_info(device, "Peer sets u_size to %lu sectors (old: %lu)\n", 3956 (unsigned long)p_usize, (unsigned long)my_usize); 3957 } 3958 3959 put_ldev(device); 3960 } 3961 3962 device->peer_max_bio_size = be32_to_cpu(p->max_bio_size); 3963 /* Leave drbd_reconsider_queue_parameters() before drbd_determine_dev_size(). 3964 In case we cleared the QUEUE_FLAG_DISCARD from our queue in 3965 drbd_reconsider_queue_parameters(), we can be sure that after 3966 drbd_determine_dev_size() no REQ_DISCARDs are in the queue. */ 3967 3968 ddsf = be16_to_cpu(p->dds_flags); 3969 if (get_ldev(device)) { 3970 drbd_reconsider_queue_parameters(device, device->ldev, o); 3971 dd = drbd_determine_dev_size(device, ddsf, NULL); 3972 put_ldev(device); 3973 if (dd == DS_ERROR) 3974 return -EIO; 3975 drbd_md_sync(device); 3976 } else { 3977 /* 3978 * I am diskless, need to accept the peer's *current* size. 3979 * I must NOT accept the peers backing disk size, 3980 * it may have been larger than mine all along... 3981 * 3982 * At this point, the peer knows more about my disk, or at 3983 * least about what we last agreed upon, than myself. 3984 * So if his c_size is less than his d_size, the most likely 3985 * reason is that *my* d_size was smaller last time we checked. 3986 * 3987 * However, if he sends a zero current size, 3988 * take his (user-capped or) backing disk size anyways. 3989 * 3990 * Unless of course he does not have a disk himself. 3991 * In which case we ignore this completely. 3992 */ 3993 sector_t new_size = p_csize ?: p_usize ?: p_size; 3994 drbd_reconsider_queue_parameters(device, NULL, o); 3995 if (new_size == 0) { 3996 /* Ignore, peer does not know nothing. */ 3997 } else if (new_size == cur_size) { 3998 /* nothing to do */ 3999 } else if (cur_size != 0 && p_size == 0) { 4000 drbd_warn(device, "Ignored diskless peer device size (peer:%llu != me:%llu sectors)!\n", 4001 (unsigned long long)new_size, (unsigned long long)cur_size); 4002 } else if (new_size < cur_size && device->state.role == R_PRIMARY) { 4003 drbd_err(device, "The peer's device size is too small! (%llu < %llu sectors); demote me first!\n", 4004 (unsigned long long)new_size, (unsigned long long)cur_size); 4005 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 4006 return -EIO; 4007 } else { 4008 /* I believe the peer, if 4009 * - I don't have a current size myself 4010 * - we agree on the size anyways 4011 * - I do have a current size, am Secondary, 4012 * and he has the only disk 4013 * - I do have a current size, am Primary, 4014 * and he has the only disk, 4015 * which is larger than my current size 4016 */ 4017 drbd_set_my_capacity(device, new_size); 4018 } 4019 } 4020 4021 if (get_ldev(device)) { 4022 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev)) { 4023 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev); 4024 ldsc = 1; 4025 } 4026 4027 put_ldev(device); 4028 } 4029 4030 if (device->state.conn > C_WF_REPORT_PARAMS) { 4031 if (be64_to_cpu(p->c_size) != get_capacity(device->vdisk) || 4032 ldsc) { 4033 /* we have different sizes, probably peer 4034 * needs to know my new size... */ 4035 drbd_send_sizes(peer_device, 0, ddsf); 4036 } 4037 if (test_and_clear_bit(RESIZE_PENDING, &device->flags) || 4038 (dd == DS_GREW && device->state.conn == C_CONNECTED)) { 4039 if (device->state.pdsk >= D_INCONSISTENT && 4040 device->state.disk >= D_INCONSISTENT) { 4041 if (ddsf & DDSF_NO_RESYNC) 4042 drbd_info(device, "Resync of new storage suppressed with --assume-clean\n"); 4043 else 4044 resync_after_online_grow(device); 4045 } else 4046 set_bit(RESYNC_AFTER_NEG, &device->flags); 4047 } 4048 } 4049 4050 return 0; 4051 } 4052 4053 static int receive_uuids(struct drbd_connection *connection, struct packet_info *pi) 4054 { 4055 struct drbd_peer_device *peer_device; 4056 struct drbd_device *device; 4057 struct p_uuids *p = pi->data; 4058 u64 *p_uuid; 4059 int i, updated_uuids = 0; 4060 4061 peer_device = conn_peer_device(connection, pi->vnr); 4062 if (!peer_device) 4063 return config_unknown_volume(connection, pi); 4064 device = peer_device->device; 4065 4066 p_uuid = kmalloc_array(UI_EXTENDED_SIZE, sizeof(*p_uuid), GFP_NOIO); 4067 if (!p_uuid) 4068 return false; 4069 4070 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++) 4071 p_uuid[i] = be64_to_cpu(p->uuid[i]); 4072 4073 kfree(device->p_uuid); 4074 device->p_uuid = p_uuid; 4075 4076 if ((device->state.conn < C_CONNECTED || device->state.pdsk == D_DISKLESS) && 4077 device->state.disk < D_INCONSISTENT && 4078 device->state.role == R_PRIMARY && 4079 (device->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) { 4080 drbd_err(device, "Can only connect to data with current UUID=%016llX\n", 4081 (unsigned long long)device->ed_uuid); 4082 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 4083 return -EIO; 4084 } 4085 4086 if (get_ldev(device)) { 4087 int skip_initial_sync = 4088 device->state.conn == C_CONNECTED && 4089 peer_device->connection->agreed_pro_version >= 90 && 4090 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED && 4091 (p_uuid[UI_FLAGS] & 8); 4092 if (skip_initial_sync) { 4093 drbd_info(device, "Accepted new current UUID, preparing to skip initial sync\n"); 4094 drbd_bitmap_io(device, &drbd_bmio_clear_n_write, 4095 "clear_n_write from receive_uuids", 4096 BM_LOCKED_TEST_ALLOWED, NULL); 4097 _drbd_uuid_set(device, UI_CURRENT, p_uuid[UI_CURRENT]); 4098 _drbd_uuid_set(device, UI_BITMAP, 0); 4099 spin_lock_irq(&device->resource->req_lock); 4100 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE), 4101 CS_VERBOSE, NULL); 4102 spin_unlock_irq(&device->resource->req_lock); 4103 drbd_md_sync(device); 4104 updated_uuids = 1; 4105 } 4106 put_ldev(device); 4107 } else if (device->state.disk < D_INCONSISTENT && 4108 device->state.role == R_PRIMARY) { 4109 /* I am a diskless primary, the peer just created a new current UUID 4110 for me. */ 4111 updated_uuids = drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]); 4112 } 4113 4114 /* Before we test for the disk state, we should wait until an eventually 4115 ongoing cluster wide state change is finished. That is important if 4116 we are primary and are detaching from our disk. We need to see the 4117 new disk state... */ 4118 mutex_lock(device->state_mutex); 4119 mutex_unlock(device->state_mutex); 4120 if (device->state.conn >= C_CONNECTED && device->state.disk < D_INCONSISTENT) 4121 updated_uuids |= drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]); 4122 4123 if (updated_uuids) 4124 drbd_print_uuids(device, "receiver updated UUIDs to"); 4125 4126 return 0; 4127 } 4128 4129 /** 4130 * convert_state() - Converts the peer's view of the cluster state to our point of view 4131 * @ps: The state as seen by the peer. 4132 */ 4133 static union drbd_state convert_state(union drbd_state ps) 4134 { 4135 union drbd_state ms; 4136 4137 static enum drbd_conns c_tab[] = { 4138 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS, 4139 [C_CONNECTED] = C_CONNECTED, 4140 4141 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T, 4142 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S, 4143 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */ 4144 [C_VERIFY_S] = C_VERIFY_T, 4145 [C_MASK] = C_MASK, 4146 }; 4147 4148 ms.i = ps.i; 4149 4150 ms.conn = c_tab[ps.conn]; 4151 ms.peer = ps.role; 4152 ms.role = ps.peer; 4153 ms.pdsk = ps.disk; 4154 ms.disk = ps.pdsk; 4155 ms.peer_isp = (ps.aftr_isp | ps.user_isp); 4156 4157 return ms; 4158 } 4159 4160 static int receive_req_state(struct drbd_connection *connection, struct packet_info *pi) 4161 { 4162 struct drbd_peer_device *peer_device; 4163 struct drbd_device *device; 4164 struct p_req_state *p = pi->data; 4165 union drbd_state mask, val; 4166 enum drbd_state_rv rv; 4167 4168 peer_device = conn_peer_device(connection, pi->vnr); 4169 if (!peer_device) 4170 return -EIO; 4171 device = peer_device->device; 4172 4173 mask.i = be32_to_cpu(p->mask); 4174 val.i = be32_to_cpu(p->val); 4175 4176 if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) && 4177 mutex_is_locked(device->state_mutex)) { 4178 drbd_send_sr_reply(peer_device, SS_CONCURRENT_ST_CHG); 4179 return 0; 4180 } 4181 4182 mask = convert_state(mask); 4183 val = convert_state(val); 4184 4185 rv = drbd_change_state(device, CS_VERBOSE, mask, val); 4186 drbd_send_sr_reply(peer_device, rv); 4187 4188 drbd_md_sync(device); 4189 4190 return 0; 4191 } 4192 4193 static int receive_req_conn_state(struct drbd_connection *connection, struct packet_info *pi) 4194 { 4195 struct p_req_state *p = pi->data; 4196 union drbd_state mask, val; 4197 enum drbd_state_rv rv; 4198 4199 mask.i = be32_to_cpu(p->mask); 4200 val.i = be32_to_cpu(p->val); 4201 4202 if (test_bit(RESOLVE_CONFLICTS, &connection->flags) && 4203 mutex_is_locked(&connection->cstate_mutex)) { 4204 conn_send_sr_reply(connection, SS_CONCURRENT_ST_CHG); 4205 return 0; 4206 } 4207 4208 mask = convert_state(mask); 4209 val = convert_state(val); 4210 4211 rv = conn_request_state(connection, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL); 4212 conn_send_sr_reply(connection, rv); 4213 4214 return 0; 4215 } 4216 4217 static int receive_state(struct drbd_connection *connection, struct packet_info *pi) 4218 { 4219 struct drbd_peer_device *peer_device; 4220 struct drbd_device *device; 4221 struct p_state *p = pi->data; 4222 union drbd_state os, ns, peer_state; 4223 enum drbd_disk_state real_peer_disk; 4224 enum chg_state_flags cs_flags; 4225 int rv; 4226 4227 peer_device = conn_peer_device(connection, pi->vnr); 4228 if (!peer_device) 4229 return config_unknown_volume(connection, pi); 4230 device = peer_device->device; 4231 4232 peer_state.i = be32_to_cpu(p->state); 4233 4234 real_peer_disk = peer_state.disk; 4235 if (peer_state.disk == D_NEGOTIATING) { 4236 real_peer_disk = device->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT; 4237 drbd_info(device, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk)); 4238 } 4239 4240 spin_lock_irq(&device->resource->req_lock); 4241 retry: 4242 os = ns = drbd_read_state(device); 4243 spin_unlock_irq(&device->resource->req_lock); 4244 4245 /* If some other part of the code (ack_receiver thread, timeout) 4246 * already decided to close the connection again, 4247 * we must not "re-establish" it here. */ 4248 if (os.conn <= C_TEAR_DOWN) 4249 return -ECONNRESET; 4250 4251 /* If this is the "end of sync" confirmation, usually the peer disk 4252 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits 4253 * set) resync started in PausedSyncT, or if the timing of pause-/ 4254 * unpause-sync events has been "just right", the peer disk may 4255 * transition from D_CONSISTENT to D_UP_TO_DATE as well. 4256 */ 4257 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) && 4258 real_peer_disk == D_UP_TO_DATE && 4259 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) { 4260 /* If we are (becoming) SyncSource, but peer is still in sync 4261 * preparation, ignore its uptodate-ness to avoid flapping, it 4262 * will change to inconsistent once the peer reaches active 4263 * syncing states. 4264 * It may have changed syncer-paused flags, however, so we 4265 * cannot ignore this completely. */ 4266 if (peer_state.conn > C_CONNECTED && 4267 peer_state.conn < C_SYNC_SOURCE) 4268 real_peer_disk = D_INCONSISTENT; 4269 4270 /* if peer_state changes to connected at the same time, 4271 * it explicitly notifies us that it finished resync. 4272 * Maybe we should finish it up, too? */ 4273 else if (os.conn >= C_SYNC_SOURCE && 4274 peer_state.conn == C_CONNECTED) { 4275 if (drbd_bm_total_weight(device) <= device->rs_failed) 4276 drbd_resync_finished(peer_device); 4277 return 0; 4278 } 4279 } 4280 4281 /* explicit verify finished notification, stop sector reached. */ 4282 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE && 4283 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) { 4284 ov_out_of_sync_print(peer_device); 4285 drbd_resync_finished(peer_device); 4286 return 0; 4287 } 4288 4289 /* peer says his disk is inconsistent, while we think it is uptodate, 4290 * and this happens while the peer still thinks we have a sync going on, 4291 * but we think we are already done with the sync. 4292 * We ignore this to avoid flapping pdsk. 4293 * This should not happen, if the peer is a recent version of drbd. */ 4294 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT && 4295 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE) 4296 real_peer_disk = D_UP_TO_DATE; 4297 4298 if (ns.conn == C_WF_REPORT_PARAMS) 4299 ns.conn = C_CONNECTED; 4300 4301 if (peer_state.conn == C_AHEAD) 4302 ns.conn = C_BEHIND; 4303 4304 /* TODO: 4305 * if (primary and diskless and peer uuid != effective uuid) 4306 * abort attach on peer; 4307 * 4308 * If this node does not have good data, was already connected, but 4309 * the peer did a late attach only now, trying to "negotiate" with me, 4310 * AND I am currently Primary, possibly frozen, with some specific 4311 * "effective" uuid, this should never be reached, really, because 4312 * we first send the uuids, then the current state. 4313 * 4314 * In this scenario, we already dropped the connection hard 4315 * when we received the unsuitable uuids (receive_uuids(). 4316 * 4317 * Should we want to change this, that is: not drop the connection in 4318 * receive_uuids() already, then we would need to add a branch here 4319 * that aborts the attach of "unsuitable uuids" on the peer in case 4320 * this node is currently Diskless Primary. 4321 */ 4322 4323 if (device->p_uuid && peer_state.disk >= D_NEGOTIATING && 4324 get_ldev_if_state(device, D_NEGOTIATING)) { 4325 int cr; /* consider resync */ 4326 4327 /* if we established a new connection */ 4328 cr = (os.conn < C_CONNECTED); 4329 /* if we had an established connection 4330 * and one of the nodes newly attaches a disk */ 4331 cr |= (os.conn == C_CONNECTED && 4332 (peer_state.disk == D_NEGOTIATING || 4333 os.disk == D_NEGOTIATING)); 4334 /* if we have both been inconsistent, and the peer has been 4335 * forced to be UpToDate with --force */ 4336 cr |= test_bit(CONSIDER_RESYNC, &device->flags); 4337 /* if we had been plain connected, and the admin requested to 4338 * start a sync by "invalidate" or "invalidate-remote" */ 4339 cr |= (os.conn == C_CONNECTED && 4340 (peer_state.conn >= C_STARTING_SYNC_S && 4341 peer_state.conn <= C_WF_BITMAP_T)); 4342 4343 if (cr) 4344 ns.conn = drbd_sync_handshake(peer_device, peer_state.role, real_peer_disk); 4345 4346 put_ldev(device); 4347 if (ns.conn == C_MASK) { 4348 ns.conn = C_CONNECTED; 4349 if (device->state.disk == D_NEGOTIATING) { 4350 drbd_force_state(device, NS(disk, D_FAILED)); 4351 } else if (peer_state.disk == D_NEGOTIATING) { 4352 drbd_err(device, "Disk attach process on the peer node was aborted.\n"); 4353 peer_state.disk = D_DISKLESS; 4354 real_peer_disk = D_DISKLESS; 4355 } else { 4356 if (test_and_clear_bit(CONN_DRY_RUN, &peer_device->connection->flags)) 4357 return -EIO; 4358 D_ASSERT(device, os.conn == C_WF_REPORT_PARAMS); 4359 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 4360 return -EIO; 4361 } 4362 } 4363 } 4364 4365 spin_lock_irq(&device->resource->req_lock); 4366 if (os.i != drbd_read_state(device).i) 4367 goto retry; 4368 clear_bit(CONSIDER_RESYNC, &device->flags); 4369 ns.peer = peer_state.role; 4370 ns.pdsk = real_peer_disk; 4371 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp); 4372 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING) 4373 ns.disk = device->new_state_tmp.disk; 4374 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD); 4375 if (ns.pdsk == D_CONSISTENT && drbd_suspended(device) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED && 4376 test_bit(NEW_CUR_UUID, &device->flags)) { 4377 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this 4378 for temporal network outages! */ 4379 spin_unlock_irq(&device->resource->req_lock); 4380 drbd_err(device, "Aborting Connect, can not thaw IO with an only Consistent peer\n"); 4381 tl_clear(peer_device->connection); 4382 drbd_uuid_new_current(device); 4383 clear_bit(NEW_CUR_UUID, &device->flags); 4384 conn_request_state(peer_device->connection, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD); 4385 return -EIO; 4386 } 4387 rv = _drbd_set_state(device, ns, cs_flags, NULL); 4388 ns = drbd_read_state(device); 4389 spin_unlock_irq(&device->resource->req_lock); 4390 4391 if (rv < SS_SUCCESS) { 4392 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD); 4393 return -EIO; 4394 } 4395 4396 if (os.conn > C_WF_REPORT_PARAMS) { 4397 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED && 4398 peer_state.disk != D_NEGOTIATING ) { 4399 /* we want resync, peer has not yet decided to sync... */ 4400 /* Nowadays only used when forcing a node into primary role and 4401 setting its disk to UpToDate with that */ 4402 drbd_send_uuids(peer_device); 4403 drbd_send_current_state(peer_device); 4404 } 4405 } 4406 4407 clear_bit(DISCARD_MY_DATA, &device->flags); 4408 4409 drbd_md_sync(device); /* update connected indicator, la_size_sect, ... */ 4410 4411 return 0; 4412 } 4413 4414 static int receive_sync_uuid(struct drbd_connection *connection, struct packet_info *pi) 4415 { 4416 struct drbd_peer_device *peer_device; 4417 struct drbd_device *device; 4418 struct p_rs_uuid *p = pi->data; 4419 4420 peer_device = conn_peer_device(connection, pi->vnr); 4421 if (!peer_device) 4422 return -EIO; 4423 device = peer_device->device; 4424 4425 wait_event(device->misc_wait, 4426 device->state.conn == C_WF_SYNC_UUID || 4427 device->state.conn == C_BEHIND || 4428 device->state.conn < C_CONNECTED || 4429 device->state.disk < D_NEGOTIATING); 4430 4431 /* D_ASSERT(device, device->state.conn == C_WF_SYNC_UUID ); */ 4432 4433 /* Here the _drbd_uuid_ functions are right, current should 4434 _not_ be rotated into the history */ 4435 if (get_ldev_if_state(device, D_NEGOTIATING)) { 4436 _drbd_uuid_set(device, UI_CURRENT, be64_to_cpu(p->uuid)); 4437 _drbd_uuid_set(device, UI_BITMAP, 0UL); 4438 4439 drbd_print_uuids(device, "updated sync uuid"); 4440 drbd_start_resync(device, C_SYNC_TARGET); 4441 4442 put_ldev(device); 4443 } else 4444 drbd_err(device, "Ignoring SyncUUID packet!\n"); 4445 4446 return 0; 4447 } 4448 4449 /* 4450 * receive_bitmap_plain 4451 * 4452 * Return 0 when done, 1 when another iteration is needed, and a negative error 4453 * code upon failure. 4454 */ 4455 static int 4456 receive_bitmap_plain(struct drbd_peer_device *peer_device, unsigned int size, 4457 unsigned long *p, struct bm_xfer_ctx *c) 4458 { 4459 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - 4460 drbd_header_size(peer_device->connection); 4461 unsigned int num_words = min_t(size_t, data_size / sizeof(*p), 4462 c->bm_words - c->word_offset); 4463 unsigned int want = num_words * sizeof(*p); 4464 int err; 4465 4466 if (want != size) { 4467 drbd_err(peer_device, "%s:want (%u) != size (%u)\n", __func__, want, size); 4468 return -EIO; 4469 } 4470 if (want == 0) 4471 return 0; 4472 err = drbd_recv_all(peer_device->connection, p, want); 4473 if (err) 4474 return err; 4475 4476 drbd_bm_merge_lel(peer_device->device, c->word_offset, num_words, p); 4477 4478 c->word_offset += num_words; 4479 c->bit_offset = c->word_offset * BITS_PER_LONG; 4480 if (c->bit_offset > c->bm_bits) 4481 c->bit_offset = c->bm_bits; 4482 4483 return 1; 4484 } 4485 4486 static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p) 4487 { 4488 return (enum drbd_bitmap_code)(p->encoding & 0x0f); 4489 } 4490 4491 static int dcbp_get_start(struct p_compressed_bm *p) 4492 { 4493 return (p->encoding & 0x80) != 0; 4494 } 4495 4496 static int dcbp_get_pad_bits(struct p_compressed_bm *p) 4497 { 4498 return (p->encoding >> 4) & 0x7; 4499 } 4500 4501 /* 4502 * recv_bm_rle_bits 4503 * 4504 * Return 0 when done, 1 when another iteration is needed, and a negative error 4505 * code upon failure. 4506 */ 4507 static int 4508 recv_bm_rle_bits(struct drbd_peer_device *peer_device, 4509 struct p_compressed_bm *p, 4510 struct bm_xfer_ctx *c, 4511 unsigned int len) 4512 { 4513 struct bitstream bs; 4514 u64 look_ahead; 4515 u64 rl; 4516 u64 tmp; 4517 unsigned long s = c->bit_offset; 4518 unsigned long e; 4519 int toggle = dcbp_get_start(p); 4520 int have; 4521 int bits; 4522 4523 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p)); 4524 4525 bits = bitstream_get_bits(&bs, &look_ahead, 64); 4526 if (bits < 0) 4527 return -EIO; 4528 4529 for (have = bits; have > 0; s += rl, toggle = !toggle) { 4530 bits = vli_decode_bits(&rl, look_ahead); 4531 if (bits <= 0) 4532 return -EIO; 4533 4534 if (toggle) { 4535 e = s + rl -1; 4536 if (e >= c->bm_bits) { 4537 drbd_err(peer_device, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e); 4538 return -EIO; 4539 } 4540 _drbd_bm_set_bits(peer_device->device, s, e); 4541 } 4542 4543 if (have < bits) { 4544 drbd_err(peer_device, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n", 4545 have, bits, look_ahead, 4546 (unsigned int)(bs.cur.b - p->code), 4547 (unsigned int)bs.buf_len); 4548 return -EIO; 4549 } 4550 /* if we consumed all 64 bits, assign 0; >> 64 is "undefined"; */ 4551 if (likely(bits < 64)) 4552 look_ahead >>= bits; 4553 else 4554 look_ahead = 0; 4555 have -= bits; 4556 4557 bits = bitstream_get_bits(&bs, &tmp, 64 - have); 4558 if (bits < 0) 4559 return -EIO; 4560 look_ahead |= tmp << have; 4561 have += bits; 4562 } 4563 4564 c->bit_offset = s; 4565 bm_xfer_ctx_bit_to_word_offset(c); 4566 4567 return (s != c->bm_bits); 4568 } 4569 4570 /* 4571 * decode_bitmap_c 4572 * 4573 * Return 0 when done, 1 when another iteration is needed, and a negative error 4574 * code upon failure. 4575 */ 4576 static int 4577 decode_bitmap_c(struct drbd_peer_device *peer_device, 4578 struct p_compressed_bm *p, 4579 struct bm_xfer_ctx *c, 4580 unsigned int len) 4581 { 4582 if (dcbp_get_code(p) == RLE_VLI_Bits) 4583 return recv_bm_rle_bits(peer_device, p, c, len - sizeof(*p)); 4584 4585 /* other variants had been implemented for evaluation, 4586 * but have been dropped as this one turned out to be "best" 4587 * during all our tests. */ 4588 4589 drbd_err(peer_device, "receive_bitmap_c: unknown encoding %u\n", p->encoding); 4590 conn_request_state(peer_device->connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); 4591 return -EIO; 4592 } 4593 4594 void INFO_bm_xfer_stats(struct drbd_peer_device *peer_device, 4595 const char *direction, struct bm_xfer_ctx *c) 4596 { 4597 /* what would it take to transfer it "plaintext" */ 4598 unsigned int header_size = drbd_header_size(peer_device->connection); 4599 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size; 4600 unsigned int plain = 4601 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) + 4602 c->bm_words * sizeof(unsigned long); 4603 unsigned int total = c->bytes[0] + c->bytes[1]; 4604 unsigned int r; 4605 4606 /* total can not be zero. but just in case: */ 4607 if (total == 0) 4608 return; 4609 4610 /* don't report if not compressed */ 4611 if (total >= plain) 4612 return; 4613 4614 /* total < plain. check for overflow, still */ 4615 r = (total > UINT_MAX/1000) ? (total / (plain/1000)) 4616 : (1000 * total / plain); 4617 4618 if (r > 1000) 4619 r = 1000; 4620 4621 r = 1000 - r; 4622 drbd_info(peer_device, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), " 4623 "total %u; compression: %u.%u%%\n", 4624 direction, 4625 c->bytes[1], c->packets[1], 4626 c->bytes[0], c->packets[0], 4627 total, r/10, r % 10); 4628 } 4629 4630 /* Since we are processing the bitfield from lower addresses to higher, 4631 it does not matter if the process it in 32 bit chunks or 64 bit 4632 chunks as long as it is little endian. (Understand it as byte stream, 4633 beginning with the lowest byte...) If we would use big endian 4634 we would need to process it from the highest address to the lowest, 4635 in order to be agnostic to the 32 vs 64 bits issue. 4636 4637 returns 0 on failure, 1 if we successfully received it. */ 4638 static int receive_bitmap(struct drbd_connection *connection, struct packet_info *pi) 4639 { 4640 struct drbd_peer_device *peer_device; 4641 struct drbd_device *device; 4642 struct bm_xfer_ctx c; 4643 int err; 4644 4645 peer_device = conn_peer_device(connection, pi->vnr); 4646 if (!peer_device) 4647 return -EIO; 4648 device = peer_device->device; 4649 4650 drbd_bm_lock(device, "receive bitmap", BM_LOCKED_SET_ALLOWED); 4651 /* you are supposed to send additional out-of-sync information 4652 * if you actually set bits during this phase */ 4653 4654 c = (struct bm_xfer_ctx) { 4655 .bm_bits = drbd_bm_bits(device), 4656 .bm_words = drbd_bm_words(device), 4657 }; 4658 4659 for(;;) { 4660 if (pi->cmd == P_BITMAP) 4661 err = receive_bitmap_plain(peer_device, pi->size, pi->data, &c); 4662 else if (pi->cmd == P_COMPRESSED_BITMAP) { 4663 /* MAYBE: sanity check that we speak proto >= 90, 4664 * and the feature is enabled! */ 4665 struct p_compressed_bm *p = pi->data; 4666 4667 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(connection)) { 4668 drbd_err(device, "ReportCBitmap packet too large\n"); 4669 err = -EIO; 4670 goto out; 4671 } 4672 if (pi->size <= sizeof(*p)) { 4673 drbd_err(device, "ReportCBitmap packet too small (l:%u)\n", pi->size); 4674 err = -EIO; 4675 goto out; 4676 } 4677 err = drbd_recv_all(peer_device->connection, p, pi->size); 4678 if (err) 4679 goto out; 4680 err = decode_bitmap_c(peer_device, p, &c, pi->size); 4681 } else { 4682 drbd_warn(device, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd); 4683 err = -EIO; 4684 goto out; 4685 } 4686 4687 c.packets[pi->cmd == P_BITMAP]++; 4688 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(connection) + pi->size; 4689 4690 if (err <= 0) { 4691 if (err < 0) 4692 goto out; 4693 break; 4694 } 4695 err = drbd_recv_header(peer_device->connection, pi); 4696 if (err) 4697 goto out; 4698 } 4699 4700 INFO_bm_xfer_stats(peer_device, "receive", &c); 4701 4702 if (device->state.conn == C_WF_BITMAP_T) { 4703 enum drbd_state_rv rv; 4704 4705 err = drbd_send_bitmap(device, peer_device); 4706 if (err) 4707 goto out; 4708 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */ 4709 rv = _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE); 4710 D_ASSERT(device, rv == SS_SUCCESS); 4711 } else if (device->state.conn != C_WF_BITMAP_S) { 4712 /* admin may have requested C_DISCONNECTING, 4713 * other threads may have noticed network errors */ 4714 drbd_info(device, "unexpected cstate (%s) in receive_bitmap\n", 4715 drbd_conn_str(device->state.conn)); 4716 } 4717 err = 0; 4718 4719 out: 4720 drbd_bm_unlock(device); 4721 if (!err && device->state.conn == C_WF_BITMAP_S) 4722 drbd_start_resync(device, C_SYNC_SOURCE); 4723 return err; 4724 } 4725 4726 static int receive_skip(struct drbd_connection *connection, struct packet_info *pi) 4727 { 4728 drbd_warn(connection, "skipping unknown optional packet type %d, l: %d!\n", 4729 pi->cmd, pi->size); 4730 4731 return ignore_remaining_packet(connection, pi); 4732 } 4733 4734 static int receive_UnplugRemote(struct drbd_connection *connection, struct packet_info *pi) 4735 { 4736 /* Make sure we've acked all the TCP data associated 4737 * with the data requests being unplugged */ 4738 tcp_sock_set_quickack(connection->data.socket->sk, 2); 4739 return 0; 4740 } 4741 4742 static int receive_out_of_sync(struct drbd_connection *connection, struct packet_info *pi) 4743 { 4744 struct drbd_peer_device *peer_device; 4745 struct drbd_device *device; 4746 struct p_block_desc *p = pi->data; 4747 4748 peer_device = conn_peer_device(connection, pi->vnr); 4749 if (!peer_device) 4750 return -EIO; 4751 device = peer_device->device; 4752 4753 switch (device->state.conn) { 4754 case C_WF_SYNC_UUID: 4755 case C_WF_BITMAP_T: 4756 case C_BEHIND: 4757 break; 4758 default: 4759 drbd_err(device, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n", 4760 drbd_conn_str(device->state.conn)); 4761 } 4762 4763 drbd_set_out_of_sync(peer_device, be64_to_cpu(p->sector), be32_to_cpu(p->blksize)); 4764 4765 return 0; 4766 } 4767 4768 static int receive_rs_deallocated(struct drbd_connection *connection, struct packet_info *pi) 4769 { 4770 struct drbd_peer_device *peer_device; 4771 struct p_block_desc *p = pi->data; 4772 struct drbd_device *device; 4773 sector_t sector; 4774 int size, err = 0; 4775 4776 peer_device = conn_peer_device(connection, pi->vnr); 4777 if (!peer_device) 4778 return -EIO; 4779 device = peer_device->device; 4780 4781 sector = be64_to_cpu(p->sector); 4782 size = be32_to_cpu(p->blksize); 4783 4784 dec_rs_pending(peer_device); 4785 4786 if (get_ldev(device)) { 4787 struct drbd_peer_request *peer_req; 4788 4789 peer_req = drbd_alloc_peer_req(peer_device, ID_SYNCER, sector, 4790 size, 0, GFP_NOIO); 4791 if (!peer_req) { 4792 put_ldev(device); 4793 return -ENOMEM; 4794 } 4795 4796 peer_req->w.cb = e_end_resync_block; 4797 peer_req->opf = REQ_OP_DISCARD; 4798 peer_req->submit_jif = jiffies; 4799 peer_req->flags |= EE_TRIM; 4800 4801 spin_lock_irq(&device->resource->req_lock); 4802 list_add_tail(&peer_req->w.list, &device->sync_ee); 4803 spin_unlock_irq(&device->resource->req_lock); 4804 4805 atomic_add(pi->size >> 9, &device->rs_sect_ev); 4806 err = drbd_submit_peer_request(peer_req); 4807 4808 if (err) { 4809 spin_lock_irq(&device->resource->req_lock); 4810 list_del(&peer_req->w.list); 4811 spin_unlock_irq(&device->resource->req_lock); 4812 4813 drbd_free_peer_req(device, peer_req); 4814 put_ldev(device); 4815 err = 0; 4816 goto fail; 4817 } 4818 4819 inc_unacked(device); 4820 4821 /* No put_ldev() here. Gets called in drbd_endio_write_sec_final(), 4822 as well as drbd_rs_complete_io() */ 4823 } else { 4824 fail: 4825 drbd_rs_complete_io(device, sector); 4826 drbd_send_ack_ex(peer_device, P_NEG_ACK, sector, size, ID_SYNCER); 4827 } 4828 4829 atomic_add(size >> 9, &device->rs_sect_in); 4830 4831 return err; 4832 } 4833 4834 struct data_cmd { 4835 int expect_payload; 4836 unsigned int pkt_size; 4837 int (*fn)(struct drbd_connection *, struct packet_info *); 4838 }; 4839 4840 static struct data_cmd drbd_cmd_handler[] = { 4841 [P_DATA] = { 1, sizeof(struct p_data), receive_Data }, 4842 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply }, 4843 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } , 4844 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } , 4845 [P_BITMAP] = { 1, 0, receive_bitmap } , 4846 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } , 4847 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote }, 4848 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, 4849 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, 4850 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam }, 4851 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam }, 4852 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol }, 4853 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids }, 4854 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes }, 4855 [P_STATE] = { 0, sizeof(struct p_state), receive_state }, 4856 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state }, 4857 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid }, 4858 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest }, 4859 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest }, 4860 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest }, 4861 [P_RS_THIN_REQ] = { 0, sizeof(struct p_block_req), receive_DataRequest }, 4862 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip }, 4863 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync }, 4864 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state }, 4865 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol }, 4866 [P_TRIM] = { 0, sizeof(struct p_trim), receive_Data }, 4867 [P_ZEROES] = { 0, sizeof(struct p_trim), receive_Data }, 4868 [P_RS_DEALLOCATED] = { 0, sizeof(struct p_block_desc), receive_rs_deallocated }, 4869 }; 4870 4871 static void drbdd(struct drbd_connection *connection) 4872 { 4873 struct packet_info pi; 4874 size_t shs; /* sub header size */ 4875 int err; 4876 4877 while (get_t_state(&connection->receiver) == RUNNING) { 4878 struct data_cmd const *cmd; 4879 4880 drbd_thread_current_set_cpu(&connection->receiver); 4881 update_receiver_timing_details(connection, drbd_recv_header_maybe_unplug); 4882 if (drbd_recv_header_maybe_unplug(connection, &pi)) 4883 goto err_out; 4884 4885 cmd = &drbd_cmd_handler[pi.cmd]; 4886 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) { 4887 drbd_err(connection, "Unexpected data packet %s (0x%04x)", 4888 cmdname(pi.cmd), pi.cmd); 4889 goto err_out; 4890 } 4891 4892 shs = cmd->pkt_size; 4893 if (pi.cmd == P_SIZES && connection->agreed_features & DRBD_FF_WSAME) 4894 shs += sizeof(struct o_qlim); 4895 if (pi.size > shs && !cmd->expect_payload) { 4896 drbd_err(connection, "No payload expected %s l:%d\n", 4897 cmdname(pi.cmd), pi.size); 4898 goto err_out; 4899 } 4900 if (pi.size < shs) { 4901 drbd_err(connection, "%s: unexpected packet size, expected:%d received:%d\n", 4902 cmdname(pi.cmd), (int)shs, pi.size); 4903 goto err_out; 4904 } 4905 4906 if (shs) { 4907 update_receiver_timing_details(connection, drbd_recv_all_warn); 4908 err = drbd_recv_all_warn(connection, pi.data, shs); 4909 if (err) 4910 goto err_out; 4911 pi.size -= shs; 4912 } 4913 4914 update_receiver_timing_details(connection, cmd->fn); 4915 err = cmd->fn(connection, &pi); 4916 if (err) { 4917 drbd_err(connection, "error receiving %s, e: %d l: %d!\n", 4918 cmdname(pi.cmd), err, pi.size); 4919 goto err_out; 4920 } 4921 } 4922 return; 4923 4924 err_out: 4925 conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); 4926 } 4927 4928 static void conn_disconnect(struct drbd_connection *connection) 4929 { 4930 struct drbd_peer_device *peer_device; 4931 enum drbd_conns oc; 4932 int vnr; 4933 4934 if (connection->cstate == C_STANDALONE) 4935 return; 4936 4937 /* We are about to start the cleanup after connection loss. 4938 * Make sure drbd_make_request knows about that. 4939 * Usually we should be in some network failure state already, 4940 * but just in case we are not, we fix it up here. 4941 */ 4942 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); 4943 4944 /* ack_receiver does not clean up anything. it must not interfere, either */ 4945 drbd_thread_stop(&connection->ack_receiver); 4946 if (connection->ack_sender) { 4947 destroy_workqueue(connection->ack_sender); 4948 connection->ack_sender = NULL; 4949 } 4950 drbd_free_sock(connection); 4951 4952 rcu_read_lock(); 4953 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 4954 struct drbd_device *device = peer_device->device; 4955 kref_get(&device->kref); 4956 rcu_read_unlock(); 4957 drbd_disconnected(peer_device); 4958 kref_put(&device->kref, drbd_destroy_device); 4959 rcu_read_lock(); 4960 } 4961 rcu_read_unlock(); 4962 4963 if (!list_empty(&connection->current_epoch->list)) 4964 drbd_err(connection, "ASSERTION FAILED: connection->current_epoch->list not empty\n"); 4965 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */ 4966 atomic_set(&connection->current_epoch->epoch_size, 0); 4967 connection->send.seen_any_write_yet = false; 4968 4969 drbd_info(connection, "Connection closed\n"); 4970 4971 if (conn_highest_role(connection) == R_PRIMARY && conn_highest_pdsk(connection) >= D_UNKNOWN) 4972 conn_try_outdate_peer_async(connection); 4973 4974 spin_lock_irq(&connection->resource->req_lock); 4975 oc = connection->cstate; 4976 if (oc >= C_UNCONNECTED) 4977 _conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE); 4978 4979 spin_unlock_irq(&connection->resource->req_lock); 4980 4981 if (oc == C_DISCONNECTING) 4982 conn_request_state(connection, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD); 4983 } 4984 4985 static int drbd_disconnected(struct drbd_peer_device *peer_device) 4986 { 4987 struct drbd_device *device = peer_device->device; 4988 unsigned int i; 4989 4990 /* wait for current activity to cease. */ 4991 spin_lock_irq(&device->resource->req_lock); 4992 _drbd_wait_ee_list_empty(device, &device->active_ee); 4993 _drbd_wait_ee_list_empty(device, &device->sync_ee); 4994 _drbd_wait_ee_list_empty(device, &device->read_ee); 4995 spin_unlock_irq(&device->resource->req_lock); 4996 4997 /* We do not have data structures that would allow us to 4998 * get the rs_pending_cnt down to 0 again. 4999 * * On C_SYNC_TARGET we do not have any data structures describing 5000 * the pending RSDataRequest's we have sent. 5001 * * On C_SYNC_SOURCE there is no data structure that tracks 5002 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget. 5003 * And no, it is not the sum of the reference counts in the 5004 * resync_LRU. The resync_LRU tracks the whole operation including 5005 * the disk-IO, while the rs_pending_cnt only tracks the blocks 5006 * on the fly. */ 5007 drbd_rs_cancel_all(device); 5008 device->rs_total = 0; 5009 device->rs_failed = 0; 5010 atomic_set(&device->rs_pending_cnt, 0); 5011 wake_up(&device->misc_wait); 5012 5013 timer_delete_sync(&device->resync_timer); 5014 resync_timer_fn(&device->resync_timer); 5015 5016 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier, 5017 * w_make_resync_request etc. which may still be on the worker queue 5018 * to be "canceled" */ 5019 drbd_flush_workqueue(&peer_device->connection->sender_work); 5020 5021 drbd_finish_peer_reqs(device); 5022 5023 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs() 5024 might have issued a work again. The one before drbd_finish_peer_reqs() is 5025 necessary to reclain net_ee in drbd_finish_peer_reqs(). */ 5026 drbd_flush_workqueue(&peer_device->connection->sender_work); 5027 5028 /* need to do it again, drbd_finish_peer_reqs() may have populated it 5029 * again via drbd_try_clear_on_disk_bm(). */ 5030 drbd_rs_cancel_all(device); 5031 5032 kfree(device->p_uuid); 5033 device->p_uuid = NULL; 5034 5035 if (!drbd_suspended(device)) 5036 tl_clear(peer_device->connection); 5037 5038 drbd_md_sync(device); 5039 5040 if (get_ldev(device)) { 5041 drbd_bitmap_io(device, &drbd_bm_write_copy_pages, 5042 "write from disconnected", BM_LOCKED_CHANGE_ALLOWED, NULL); 5043 put_ldev(device); 5044 } 5045 5046 i = atomic_read(&device->pp_in_use_by_net); 5047 if (i) 5048 drbd_info(device, "pp_in_use_by_net = %d, expected 0\n", i); 5049 i = atomic_read(&device->pp_in_use); 5050 if (i) 5051 drbd_info(device, "pp_in_use = %d, expected 0\n", i); 5052 5053 D_ASSERT(device, list_empty(&device->read_ee)); 5054 D_ASSERT(device, list_empty(&device->active_ee)); 5055 D_ASSERT(device, list_empty(&device->sync_ee)); 5056 D_ASSERT(device, list_empty(&device->done_ee)); 5057 5058 return 0; 5059 } 5060 5061 /* 5062 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version 5063 * we can agree on is stored in agreed_pro_version. 5064 * 5065 * feature flags and the reserved array should be enough room for future 5066 * enhancements of the handshake protocol, and possible plugins... 5067 * 5068 * for now, they are expected to be zero, but ignored. 5069 */ 5070 static int drbd_send_features(struct drbd_connection *connection) 5071 { 5072 struct drbd_socket *sock; 5073 struct p_connection_features *p; 5074 5075 sock = &connection->data; 5076 p = conn_prepare_command(connection, sock); 5077 if (!p) 5078 return -EIO; 5079 memset(p, 0, sizeof(*p)); 5080 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN); 5081 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX); 5082 p->feature_flags = cpu_to_be32(PRO_FEATURES); 5083 return conn_send_command(connection, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0); 5084 } 5085 5086 /* 5087 * return values: 5088 * 1 yes, we have a valid connection 5089 * 0 oops, did not work out, please try again 5090 * -1 peer talks different language, 5091 * no point in trying again, please go standalone. 5092 */ 5093 static int drbd_do_features(struct drbd_connection *connection) 5094 { 5095 /* ASSERT current == connection->receiver ... */ 5096 struct p_connection_features *p; 5097 const int expect = sizeof(struct p_connection_features); 5098 struct packet_info pi; 5099 int err; 5100 5101 err = drbd_send_features(connection); 5102 if (err) 5103 return 0; 5104 5105 err = drbd_recv_header(connection, &pi); 5106 if (err) 5107 return 0; 5108 5109 if (pi.cmd != P_CONNECTION_FEATURES) { 5110 drbd_err(connection, "expected ConnectionFeatures packet, received: %s (0x%04x)\n", 5111 cmdname(pi.cmd), pi.cmd); 5112 return -1; 5113 } 5114 5115 if (pi.size != expect) { 5116 drbd_err(connection, "expected ConnectionFeatures length: %u, received: %u\n", 5117 expect, pi.size); 5118 return -1; 5119 } 5120 5121 p = pi.data; 5122 err = drbd_recv_all_warn(connection, p, expect); 5123 if (err) 5124 return 0; 5125 5126 p->protocol_min = be32_to_cpu(p->protocol_min); 5127 p->protocol_max = be32_to_cpu(p->protocol_max); 5128 if (p->protocol_max == 0) 5129 p->protocol_max = p->protocol_min; 5130 5131 if (PRO_VERSION_MAX < p->protocol_min || 5132 PRO_VERSION_MIN > p->protocol_max) 5133 goto incompat; 5134 5135 connection->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max); 5136 connection->agreed_features = PRO_FEATURES & be32_to_cpu(p->feature_flags); 5137 5138 drbd_info(connection, "Handshake successful: " 5139 "Agreed network protocol version %d\n", connection->agreed_pro_version); 5140 5141 drbd_info(connection, "Feature flags enabled on protocol level: 0x%x%s%s%s%s.\n", 5142 connection->agreed_features, 5143 connection->agreed_features & DRBD_FF_TRIM ? " TRIM" : "", 5144 connection->agreed_features & DRBD_FF_THIN_RESYNC ? " THIN_RESYNC" : "", 5145 connection->agreed_features & DRBD_FF_WSAME ? " WRITE_SAME" : "", 5146 connection->agreed_features & DRBD_FF_WZEROES ? " WRITE_ZEROES" : 5147 connection->agreed_features ? "" : " none"); 5148 5149 return 1; 5150 5151 incompat: 5152 drbd_err(connection, "incompatible DRBD dialects: " 5153 "I support %d-%d, peer supports %d-%d\n", 5154 PRO_VERSION_MIN, PRO_VERSION_MAX, 5155 p->protocol_min, p->protocol_max); 5156 return -1; 5157 } 5158 5159 #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE) 5160 static int drbd_do_auth(struct drbd_connection *connection) 5161 { 5162 drbd_err(connection, "This kernel was build without CONFIG_CRYPTO_HMAC.\n"); 5163 drbd_err(connection, "You need to disable 'cram-hmac-alg' in drbd.conf.\n"); 5164 return -1; 5165 } 5166 #else 5167 #define CHALLENGE_LEN 64 5168 5169 /* Return value: 5170 1 - auth succeeded, 5171 0 - failed, try again (network error), 5172 -1 - auth failed, don't try again. 5173 */ 5174 5175 static int drbd_do_auth(struct drbd_connection *connection) 5176 { 5177 struct drbd_socket *sock; 5178 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */ 5179 char *response = NULL; 5180 char *right_response = NULL; 5181 char *peers_ch = NULL; 5182 unsigned int key_len; 5183 char secret[SHARED_SECRET_MAX]; /* 64 byte */ 5184 unsigned int resp_size; 5185 struct shash_desc *desc; 5186 struct packet_info pi; 5187 struct net_conf *nc; 5188 int err, rv; 5189 5190 /* FIXME: Put the challenge/response into the preallocated socket buffer. */ 5191 5192 rcu_read_lock(); 5193 nc = rcu_dereference(connection->net_conf); 5194 key_len = strlen(nc->shared_secret); 5195 memcpy(secret, nc->shared_secret, key_len); 5196 rcu_read_unlock(); 5197 5198 desc = kmalloc(sizeof(struct shash_desc) + 5199 crypto_shash_descsize(connection->cram_hmac_tfm), 5200 GFP_KERNEL); 5201 if (!desc) { 5202 rv = -1; 5203 goto fail; 5204 } 5205 desc->tfm = connection->cram_hmac_tfm; 5206 5207 rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len); 5208 if (rv) { 5209 drbd_err(connection, "crypto_shash_setkey() failed with %d\n", rv); 5210 rv = -1; 5211 goto fail; 5212 } 5213 5214 get_random_bytes(my_challenge, CHALLENGE_LEN); 5215 5216 sock = &connection->data; 5217 if (!conn_prepare_command(connection, sock)) { 5218 rv = 0; 5219 goto fail; 5220 } 5221 rv = !conn_send_command(connection, sock, P_AUTH_CHALLENGE, 0, 5222 my_challenge, CHALLENGE_LEN); 5223 if (!rv) 5224 goto fail; 5225 5226 err = drbd_recv_header(connection, &pi); 5227 if (err) { 5228 rv = 0; 5229 goto fail; 5230 } 5231 5232 if (pi.cmd != P_AUTH_CHALLENGE) { 5233 drbd_err(connection, "expected AuthChallenge packet, received: %s (0x%04x)\n", 5234 cmdname(pi.cmd), pi.cmd); 5235 rv = -1; 5236 goto fail; 5237 } 5238 5239 if (pi.size > CHALLENGE_LEN * 2) { 5240 drbd_err(connection, "expected AuthChallenge payload too big.\n"); 5241 rv = -1; 5242 goto fail; 5243 } 5244 5245 if (pi.size < CHALLENGE_LEN) { 5246 drbd_err(connection, "AuthChallenge payload too small.\n"); 5247 rv = -1; 5248 goto fail; 5249 } 5250 5251 peers_ch = kmalloc(pi.size, GFP_NOIO); 5252 if (!peers_ch) { 5253 rv = -1; 5254 goto fail; 5255 } 5256 5257 err = drbd_recv_all_warn(connection, peers_ch, pi.size); 5258 if (err) { 5259 rv = 0; 5260 goto fail; 5261 } 5262 5263 if (!memcmp(my_challenge, peers_ch, CHALLENGE_LEN)) { 5264 drbd_err(connection, "Peer presented the same challenge!\n"); 5265 rv = -1; 5266 goto fail; 5267 } 5268 5269 resp_size = crypto_shash_digestsize(connection->cram_hmac_tfm); 5270 response = kmalloc(resp_size, GFP_NOIO); 5271 if (!response) { 5272 rv = -1; 5273 goto fail; 5274 } 5275 5276 rv = crypto_shash_digest(desc, peers_ch, pi.size, response); 5277 if (rv) { 5278 drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv); 5279 rv = -1; 5280 goto fail; 5281 } 5282 5283 if (!conn_prepare_command(connection, sock)) { 5284 rv = 0; 5285 goto fail; 5286 } 5287 rv = !conn_send_command(connection, sock, P_AUTH_RESPONSE, 0, 5288 response, resp_size); 5289 if (!rv) 5290 goto fail; 5291 5292 err = drbd_recv_header(connection, &pi); 5293 if (err) { 5294 rv = 0; 5295 goto fail; 5296 } 5297 5298 if (pi.cmd != P_AUTH_RESPONSE) { 5299 drbd_err(connection, "expected AuthResponse packet, received: %s (0x%04x)\n", 5300 cmdname(pi.cmd), pi.cmd); 5301 rv = 0; 5302 goto fail; 5303 } 5304 5305 if (pi.size != resp_size) { 5306 drbd_err(connection, "expected AuthResponse payload of wrong size\n"); 5307 rv = 0; 5308 goto fail; 5309 } 5310 5311 err = drbd_recv_all_warn(connection, response , resp_size); 5312 if (err) { 5313 rv = 0; 5314 goto fail; 5315 } 5316 5317 right_response = kmalloc(resp_size, GFP_NOIO); 5318 if (!right_response) { 5319 rv = -1; 5320 goto fail; 5321 } 5322 5323 rv = crypto_shash_digest(desc, my_challenge, CHALLENGE_LEN, 5324 right_response); 5325 if (rv) { 5326 drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv); 5327 rv = -1; 5328 goto fail; 5329 } 5330 5331 rv = !memcmp(response, right_response, resp_size); 5332 5333 if (rv) 5334 drbd_info(connection, "Peer authenticated using %d bytes HMAC\n", 5335 resp_size); 5336 else 5337 rv = -1; 5338 5339 fail: 5340 kfree(peers_ch); 5341 kfree(response); 5342 kfree(right_response); 5343 if (desc) { 5344 shash_desc_zero(desc); 5345 kfree(desc); 5346 } 5347 5348 return rv; 5349 } 5350 #endif 5351 5352 int drbd_receiver(struct drbd_thread *thi) 5353 { 5354 struct drbd_connection *connection = thi->connection; 5355 int h; 5356 5357 drbd_info(connection, "receiver (re)started\n"); 5358 5359 do { 5360 h = conn_connect(connection); 5361 if (h == 0) { 5362 conn_disconnect(connection); 5363 schedule_timeout_interruptible(HZ); 5364 } 5365 if (h == -1) { 5366 drbd_warn(connection, "Discarding network configuration.\n"); 5367 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 5368 } 5369 } while (h == 0); 5370 5371 if (h > 0) { 5372 blk_start_plug(&connection->receiver_plug); 5373 drbdd(connection); 5374 blk_finish_plug(&connection->receiver_plug); 5375 } 5376 5377 conn_disconnect(connection); 5378 5379 drbd_info(connection, "receiver terminated\n"); 5380 return 0; 5381 } 5382 5383 /* ********* acknowledge sender ******** */ 5384 5385 static int got_conn_RqSReply(struct drbd_connection *connection, struct packet_info *pi) 5386 { 5387 struct p_req_state_reply *p = pi->data; 5388 int retcode = be32_to_cpu(p->retcode); 5389 5390 if (retcode >= SS_SUCCESS) { 5391 set_bit(CONN_WD_ST_CHG_OKAY, &connection->flags); 5392 } else { 5393 set_bit(CONN_WD_ST_CHG_FAIL, &connection->flags); 5394 drbd_err(connection, "Requested state change failed by peer: %s (%d)\n", 5395 drbd_set_st_err_str(retcode), retcode); 5396 } 5397 wake_up(&connection->ping_wait); 5398 5399 return 0; 5400 } 5401 5402 static int got_RqSReply(struct drbd_connection *connection, struct packet_info *pi) 5403 { 5404 struct drbd_peer_device *peer_device; 5405 struct drbd_device *device; 5406 struct p_req_state_reply *p = pi->data; 5407 int retcode = be32_to_cpu(p->retcode); 5408 5409 peer_device = conn_peer_device(connection, pi->vnr); 5410 if (!peer_device) 5411 return -EIO; 5412 device = peer_device->device; 5413 5414 if (test_bit(CONN_WD_ST_CHG_REQ, &connection->flags)) { 5415 D_ASSERT(device, connection->agreed_pro_version < 100); 5416 return got_conn_RqSReply(connection, pi); 5417 } 5418 5419 if (retcode >= SS_SUCCESS) { 5420 set_bit(CL_ST_CHG_SUCCESS, &device->flags); 5421 } else { 5422 set_bit(CL_ST_CHG_FAIL, &device->flags); 5423 drbd_err(device, "Requested state change failed by peer: %s (%d)\n", 5424 drbd_set_st_err_str(retcode), retcode); 5425 } 5426 wake_up(&device->state_wait); 5427 5428 return 0; 5429 } 5430 5431 static int got_Ping(struct drbd_connection *connection, struct packet_info *pi) 5432 { 5433 return drbd_send_ping_ack(connection); 5434 5435 } 5436 5437 static int got_PingAck(struct drbd_connection *connection, struct packet_info *pi) 5438 { 5439 /* restore idle timeout */ 5440 connection->meta.socket->sk->sk_rcvtimeo = connection->net_conf->ping_int*HZ; 5441 if (!test_and_set_bit(GOT_PING_ACK, &connection->flags)) 5442 wake_up(&connection->ping_wait); 5443 5444 return 0; 5445 } 5446 5447 static int got_IsInSync(struct drbd_connection *connection, struct packet_info *pi) 5448 { 5449 struct drbd_peer_device *peer_device; 5450 struct drbd_device *device; 5451 struct p_block_ack *p = pi->data; 5452 sector_t sector = be64_to_cpu(p->sector); 5453 int blksize = be32_to_cpu(p->blksize); 5454 5455 peer_device = conn_peer_device(connection, pi->vnr); 5456 if (!peer_device) 5457 return -EIO; 5458 device = peer_device->device; 5459 5460 D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89); 5461 5462 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5463 5464 if (get_ldev(device)) { 5465 drbd_rs_complete_io(device, sector); 5466 drbd_set_in_sync(peer_device, sector, blksize); 5467 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */ 5468 device->rs_same_csum += (blksize >> BM_BLOCK_SHIFT); 5469 put_ldev(device); 5470 } 5471 dec_rs_pending(peer_device); 5472 atomic_add(blksize >> 9, &device->rs_sect_in); 5473 5474 return 0; 5475 } 5476 5477 static int 5478 validate_req_change_req_state(struct drbd_peer_device *peer_device, u64 id, sector_t sector, 5479 struct rb_root *root, const char *func, 5480 enum drbd_req_event what, bool missing_ok) 5481 { 5482 struct drbd_device *device = peer_device->device; 5483 struct drbd_request *req; 5484 struct bio_and_error m; 5485 5486 spin_lock_irq(&device->resource->req_lock); 5487 req = find_request(device, root, id, sector, missing_ok, func); 5488 if (unlikely(!req)) { 5489 spin_unlock_irq(&device->resource->req_lock); 5490 return -EIO; 5491 } 5492 __req_mod(req, what, peer_device, &m); 5493 spin_unlock_irq(&device->resource->req_lock); 5494 5495 if (m.bio) 5496 complete_master_bio(device, &m); 5497 return 0; 5498 } 5499 5500 static int got_BlockAck(struct drbd_connection *connection, struct packet_info *pi) 5501 { 5502 struct drbd_peer_device *peer_device; 5503 struct drbd_device *device; 5504 struct p_block_ack *p = pi->data; 5505 sector_t sector = be64_to_cpu(p->sector); 5506 int blksize = be32_to_cpu(p->blksize); 5507 enum drbd_req_event what; 5508 5509 peer_device = conn_peer_device(connection, pi->vnr); 5510 if (!peer_device) 5511 return -EIO; 5512 device = peer_device->device; 5513 5514 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5515 5516 if (p->block_id == ID_SYNCER) { 5517 drbd_set_in_sync(peer_device, sector, blksize); 5518 dec_rs_pending(peer_device); 5519 return 0; 5520 } 5521 switch (pi->cmd) { 5522 case P_RS_WRITE_ACK: 5523 what = WRITE_ACKED_BY_PEER_AND_SIS; 5524 break; 5525 case P_WRITE_ACK: 5526 what = WRITE_ACKED_BY_PEER; 5527 break; 5528 case P_RECV_ACK: 5529 what = RECV_ACKED_BY_PEER; 5530 break; 5531 case P_SUPERSEDED: 5532 what = CONFLICT_RESOLVED; 5533 break; 5534 case P_RETRY_WRITE: 5535 what = POSTPONE_WRITE; 5536 break; 5537 default: 5538 BUG(); 5539 } 5540 5541 return validate_req_change_req_state(peer_device, p->block_id, sector, 5542 &device->write_requests, __func__, 5543 what, false); 5544 } 5545 5546 static int got_NegAck(struct drbd_connection *connection, struct packet_info *pi) 5547 { 5548 struct drbd_peer_device *peer_device; 5549 struct drbd_device *device; 5550 struct p_block_ack *p = pi->data; 5551 sector_t sector = be64_to_cpu(p->sector); 5552 int size = be32_to_cpu(p->blksize); 5553 int err; 5554 5555 peer_device = conn_peer_device(connection, pi->vnr); 5556 if (!peer_device) 5557 return -EIO; 5558 device = peer_device->device; 5559 5560 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5561 5562 if (p->block_id == ID_SYNCER) { 5563 dec_rs_pending(peer_device); 5564 drbd_rs_failed_io(peer_device, sector, size); 5565 return 0; 5566 } 5567 5568 err = validate_req_change_req_state(peer_device, p->block_id, sector, 5569 &device->write_requests, __func__, 5570 NEG_ACKED, true); 5571 if (err) { 5572 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs. 5573 The master bio might already be completed, therefore the 5574 request is no longer in the collision hash. */ 5575 /* In Protocol B we might already have got a P_RECV_ACK 5576 but then get a P_NEG_ACK afterwards. */ 5577 drbd_set_out_of_sync(peer_device, sector, size); 5578 } 5579 return 0; 5580 } 5581 5582 static int got_NegDReply(struct drbd_connection *connection, struct packet_info *pi) 5583 { 5584 struct drbd_peer_device *peer_device; 5585 struct drbd_device *device; 5586 struct p_block_ack *p = pi->data; 5587 sector_t sector = be64_to_cpu(p->sector); 5588 5589 peer_device = conn_peer_device(connection, pi->vnr); 5590 if (!peer_device) 5591 return -EIO; 5592 device = peer_device->device; 5593 5594 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5595 5596 drbd_err(device, "Got NegDReply; Sector %llus, len %u.\n", 5597 (unsigned long long)sector, be32_to_cpu(p->blksize)); 5598 5599 return validate_req_change_req_state(peer_device, p->block_id, sector, 5600 &device->read_requests, __func__, 5601 NEG_ACKED, false); 5602 } 5603 5604 static int got_NegRSDReply(struct drbd_connection *connection, struct packet_info *pi) 5605 { 5606 struct drbd_peer_device *peer_device; 5607 struct drbd_device *device; 5608 sector_t sector; 5609 int size; 5610 struct p_block_ack *p = pi->data; 5611 5612 peer_device = conn_peer_device(connection, pi->vnr); 5613 if (!peer_device) 5614 return -EIO; 5615 device = peer_device->device; 5616 5617 sector = be64_to_cpu(p->sector); 5618 size = be32_to_cpu(p->blksize); 5619 5620 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5621 5622 dec_rs_pending(peer_device); 5623 5624 if (get_ldev_if_state(device, D_FAILED)) { 5625 drbd_rs_complete_io(device, sector); 5626 switch (pi->cmd) { 5627 case P_NEG_RS_DREPLY: 5628 drbd_rs_failed_io(peer_device, sector, size); 5629 break; 5630 case P_RS_CANCEL: 5631 break; 5632 default: 5633 BUG(); 5634 } 5635 put_ldev(device); 5636 } 5637 5638 return 0; 5639 } 5640 5641 static int got_BarrierAck(struct drbd_connection *connection, struct packet_info *pi) 5642 { 5643 struct p_barrier_ack *p = pi->data; 5644 struct drbd_peer_device *peer_device; 5645 int vnr; 5646 5647 tl_release(connection, p->barrier, be32_to_cpu(p->set_size)); 5648 5649 rcu_read_lock(); 5650 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 5651 struct drbd_device *device = peer_device->device; 5652 5653 if (device->state.conn == C_AHEAD && 5654 atomic_read(&device->ap_in_flight) == 0 && 5655 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &device->flags)) { 5656 device->start_resync_timer.expires = jiffies + HZ; 5657 add_timer(&device->start_resync_timer); 5658 } 5659 } 5660 rcu_read_unlock(); 5661 5662 return 0; 5663 } 5664 5665 static int got_OVResult(struct drbd_connection *connection, struct packet_info *pi) 5666 { 5667 struct drbd_peer_device *peer_device; 5668 struct drbd_device *device; 5669 struct p_block_ack *p = pi->data; 5670 struct drbd_device_work *dw; 5671 sector_t sector; 5672 int size; 5673 5674 peer_device = conn_peer_device(connection, pi->vnr); 5675 if (!peer_device) 5676 return -EIO; 5677 device = peer_device->device; 5678 5679 sector = be64_to_cpu(p->sector); 5680 size = be32_to_cpu(p->blksize); 5681 5682 update_peer_seq(peer_device, be32_to_cpu(p->seq_num)); 5683 5684 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC) 5685 drbd_ov_out_of_sync_found(peer_device, sector, size); 5686 else 5687 ov_out_of_sync_print(peer_device); 5688 5689 if (!get_ldev(device)) 5690 return 0; 5691 5692 drbd_rs_complete_io(device, sector); 5693 dec_rs_pending(peer_device); 5694 5695 --device->ov_left; 5696 5697 /* let's advance progress step marks only for every other megabyte */ 5698 if ((device->ov_left & 0x200) == 0x200) 5699 drbd_advance_rs_marks(peer_device, device->ov_left); 5700 5701 if (device->ov_left == 0) { 5702 dw = kmalloc_obj(*dw, GFP_NOIO); 5703 if (dw) { 5704 dw->w.cb = w_ov_finished; 5705 dw->device = device; 5706 drbd_queue_work(&peer_device->connection->sender_work, &dw->w); 5707 } else { 5708 drbd_err(device, "kmalloc(dw) failed."); 5709 ov_out_of_sync_print(peer_device); 5710 drbd_resync_finished(peer_device); 5711 } 5712 } 5713 put_ldev(device); 5714 return 0; 5715 } 5716 5717 static int got_skip(struct drbd_connection *connection, struct packet_info *pi) 5718 { 5719 return 0; 5720 } 5721 5722 struct meta_sock_cmd { 5723 size_t pkt_size; 5724 int (*fn)(struct drbd_connection *connection, struct packet_info *); 5725 }; 5726 5727 static void set_rcvtimeo(struct drbd_connection *connection, bool ping_timeout) 5728 { 5729 long t; 5730 struct net_conf *nc; 5731 5732 rcu_read_lock(); 5733 nc = rcu_dereference(connection->net_conf); 5734 t = ping_timeout ? nc->ping_timeo : nc->ping_int; 5735 rcu_read_unlock(); 5736 5737 t *= HZ; 5738 if (ping_timeout) 5739 t /= 10; 5740 5741 connection->meta.socket->sk->sk_rcvtimeo = t; 5742 } 5743 5744 static void set_ping_timeout(struct drbd_connection *connection) 5745 { 5746 set_rcvtimeo(connection, 1); 5747 } 5748 5749 static void set_idle_timeout(struct drbd_connection *connection) 5750 { 5751 set_rcvtimeo(connection, 0); 5752 } 5753 5754 static struct meta_sock_cmd ack_receiver_tbl[] = { 5755 [P_PING] = { 0, got_Ping }, 5756 [P_PING_ACK] = { 0, got_PingAck }, 5757 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, 5758 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, 5759 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck }, 5760 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck }, 5761 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck }, 5762 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply }, 5763 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply }, 5764 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult }, 5765 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck }, 5766 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply }, 5767 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync }, 5768 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip }, 5769 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply }, 5770 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply }, 5771 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck }, 5772 }; 5773 5774 int drbd_ack_receiver(struct drbd_thread *thi) 5775 { 5776 struct drbd_connection *connection = thi->connection; 5777 struct meta_sock_cmd *cmd = NULL; 5778 struct packet_info pi; 5779 unsigned long pre_recv_jif; 5780 int rv; 5781 void *buf = connection->meta.rbuf; 5782 int received = 0; 5783 unsigned int header_size = drbd_header_size(connection); 5784 int expect = header_size; 5785 bool ping_timeout_active = false; 5786 5787 sched_set_fifo_low(current); 5788 5789 while (get_t_state(thi) == RUNNING) { 5790 drbd_thread_current_set_cpu(thi); 5791 5792 if (test_and_clear_bit(SEND_PING, &connection->flags)) { 5793 if (drbd_send_ping(connection)) { 5794 drbd_err(connection, "drbd_send_ping has failed\n"); 5795 goto reconnect; 5796 } 5797 set_ping_timeout(connection); 5798 ping_timeout_active = true; 5799 } 5800 5801 pre_recv_jif = jiffies; 5802 rv = drbd_recv_short(connection->meta.socket, buf, expect-received, 0); 5803 5804 /* Note: 5805 * -EINTR (on meta) we got a signal 5806 * -EAGAIN (on meta) rcvtimeo expired 5807 * -ECONNRESET other side closed the connection 5808 * -ERESTARTSYS (on data) we got a signal 5809 * rv < 0 other than above: unexpected error! 5810 * rv == expected: full header or command 5811 * rv < expected: "woken" by signal during receive 5812 * rv == 0 : "connection shut down by peer" 5813 */ 5814 if (likely(rv > 0)) { 5815 received += rv; 5816 buf += rv; 5817 } else if (rv == 0) { 5818 if (test_bit(DISCONNECT_SENT, &connection->flags)) { 5819 long t; 5820 rcu_read_lock(); 5821 t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10; 5822 rcu_read_unlock(); 5823 5824 t = wait_event_timeout(connection->ping_wait, 5825 connection->cstate < C_WF_REPORT_PARAMS, 5826 t); 5827 if (t) 5828 break; 5829 } 5830 drbd_err(connection, "meta connection shut down by peer.\n"); 5831 goto reconnect; 5832 } else if (rv == -EAGAIN) { 5833 /* If the data socket received something meanwhile, 5834 * that is good enough: peer is still alive. */ 5835 if (time_after(connection->last_received, pre_recv_jif)) 5836 continue; 5837 if (ping_timeout_active) { 5838 drbd_err(connection, "PingAck did not arrive in time.\n"); 5839 goto reconnect; 5840 } 5841 set_bit(SEND_PING, &connection->flags); 5842 continue; 5843 } else if (rv == -EINTR) { 5844 /* maybe drbd_thread_stop(): the while condition will notice. 5845 * maybe woken for send_ping: we'll send a ping above, 5846 * and change the rcvtimeo */ 5847 flush_signals(current); 5848 continue; 5849 } else { 5850 drbd_err(connection, "sock_recvmsg returned %d\n", rv); 5851 goto reconnect; 5852 } 5853 5854 if (received == expect && cmd == NULL) { 5855 if (decode_header(connection, connection->meta.rbuf, &pi)) 5856 goto reconnect; 5857 cmd = &ack_receiver_tbl[pi.cmd]; 5858 if (pi.cmd >= ARRAY_SIZE(ack_receiver_tbl) || !cmd->fn) { 5859 drbd_err(connection, "Unexpected meta packet %s (0x%04x)\n", 5860 cmdname(pi.cmd), pi.cmd); 5861 goto disconnect; 5862 } 5863 expect = header_size + cmd->pkt_size; 5864 if (pi.size != expect - header_size) { 5865 drbd_err(connection, "Wrong packet size on meta (c: %d, l: %d)\n", 5866 pi.cmd, pi.size); 5867 goto reconnect; 5868 } 5869 } 5870 if (received == expect) { 5871 bool err; 5872 5873 err = cmd->fn(connection, &pi); 5874 if (err) { 5875 drbd_err(connection, "%ps failed\n", cmd->fn); 5876 goto reconnect; 5877 } 5878 5879 connection->last_received = jiffies; 5880 5881 if (cmd == &ack_receiver_tbl[P_PING_ACK]) { 5882 set_idle_timeout(connection); 5883 ping_timeout_active = false; 5884 } 5885 5886 buf = connection->meta.rbuf; 5887 received = 0; 5888 expect = header_size; 5889 cmd = NULL; 5890 } 5891 } 5892 5893 if (0) { 5894 reconnect: 5895 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); 5896 conn_md_sync(connection); 5897 } 5898 if (0) { 5899 disconnect: 5900 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD); 5901 } 5902 5903 drbd_info(connection, "ack_receiver terminated\n"); 5904 5905 return 0; 5906 } 5907 5908 void drbd_send_acks_wf(struct work_struct *ws) 5909 { 5910 struct drbd_peer_device *peer_device = 5911 container_of(ws, struct drbd_peer_device, send_acks_work); 5912 struct drbd_connection *connection = peer_device->connection; 5913 struct drbd_device *device = peer_device->device; 5914 struct net_conf *nc; 5915 int tcp_cork, err; 5916 5917 rcu_read_lock(); 5918 nc = rcu_dereference(connection->net_conf); 5919 tcp_cork = nc->tcp_cork; 5920 rcu_read_unlock(); 5921 5922 if (tcp_cork) 5923 tcp_sock_set_cork(connection->meta.socket->sk, true); 5924 5925 err = drbd_finish_peer_reqs(device); 5926 kref_put(&device->kref, drbd_destroy_device); 5927 /* get is in drbd_endio_write_sec_final(). That is necessary to keep the 5928 struct work_struct send_acks_work alive, which is in the peer_device object */ 5929 5930 if (err) { 5931 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD); 5932 return; 5933 } 5934 5935 if (tcp_cork) 5936 tcp_sock_set_cork(connection->meta.socket->sk, false); 5937 5938 return; 5939 } 5940