1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 drbd.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 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev 12 from Logicworks, Inc. for making SDP replication support possible. 13 14 15 */ 16 17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18 19 #include <linux/module.h> 20 #include <linux/jiffies.h> 21 #include <linux/drbd.h> 22 #include <linux/uaccess.h> 23 #include <asm/types.h> 24 #include <net/sock.h> 25 #include <linux/ctype.h> 26 #include <linux/mutex.h> 27 #include <linux/fs.h> 28 #include <linux/file.h> 29 #include <linux/proc_fs.h> 30 #include <linux/init.h> 31 #include <linux/mm.h> 32 #include <linux/memcontrol.h> 33 #include <linux/mm_inline.h> 34 #include <linux/slab.h> 35 #include <linux/string.h> 36 #include <linux/random.h> 37 #include <linux/reboot.h> 38 #include <linux/notifier.h> 39 #include <linux/kthread.h> 40 #include <linux/workqueue.h> 41 #include <linux/unistd.h> 42 #include <linux/vmalloc.h> 43 #include <linux/sched/signal.h> 44 45 #include <linux/drbd_limits.h> 46 #include "drbd_int.h" 47 #include "drbd_protocol.h" 48 #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */ 49 #include "drbd_vli.h" 50 #include "drbd_debugfs.h" 51 52 static DEFINE_MUTEX(drbd_main_mutex); 53 static int drbd_open(struct gendisk *disk, blk_mode_t mode); 54 static void drbd_release(struct gendisk *gd); 55 static void md_sync_timer_fn(struct timer_list *t); 56 static int w_bitmap_io(struct drbd_work *w, int unused); 57 58 MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, " 59 "Lars Ellenberg <lars@linbit.com>"); 60 MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION); 61 MODULE_VERSION(REL_VERSION); 62 MODULE_LICENSE("GPL"); 63 MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices (" 64 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")"); 65 MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR); 66 67 #include <linux/moduleparam.h> 68 /* thanks to these macros, if compiled into the kernel (not-module), 69 * these become boot parameters (e.g., drbd.minor_count) */ 70 71 #ifdef CONFIG_DRBD_FAULT_INJECTION 72 int drbd_enable_faults; 73 int drbd_fault_rate; 74 static int drbd_fault_count; 75 static int drbd_fault_devs; 76 /* bitmap of enabled faults */ 77 module_param_named(enable_faults, drbd_enable_faults, int, 0664); 78 /* fault rate % value - applies to all enabled faults */ 79 module_param_named(fault_rate, drbd_fault_rate, int, 0664); 80 /* count of faults inserted */ 81 module_param_named(fault_count, drbd_fault_count, int, 0664); 82 /* bitmap of devices to insert faults on */ 83 module_param_named(fault_devs, drbd_fault_devs, int, 0644); 84 #endif 85 86 /* module parameters we can keep static */ 87 static bool drbd_allow_oos; /* allow_open_on_secondary */ 88 static bool drbd_disable_sendpage; 89 MODULE_PARM_DESC(allow_oos, "DONT USE!"); 90 module_param_named(allow_oos, drbd_allow_oos, bool, 0); 91 module_param_named(disable_sendpage, drbd_disable_sendpage, bool, 0644); 92 93 /* module parameters we share */ 94 int drbd_proc_details; /* Detail level in proc drbd*/ 95 module_param_named(proc_details, drbd_proc_details, int, 0644); 96 /* module parameters shared with defaults */ 97 unsigned int drbd_minor_count = DRBD_MINOR_COUNT_DEF; 98 /* Module parameter for setting the user mode helper program 99 * to run. Default is /sbin/drbdadm */ 100 char drbd_usermode_helper[80] = "/sbin/drbdadm"; 101 module_param_named(minor_count, drbd_minor_count, uint, 0444); 102 module_param_string(usermode_helper, drbd_usermode_helper, sizeof(drbd_usermode_helper), 0644); 103 104 /* in 2.6.x, our device mapping and config info contains our virtual gendisks 105 * as member "struct gendisk *vdisk;" 106 */ 107 struct idr drbd_devices; 108 struct list_head drbd_resources; 109 struct mutex resources_mutex; 110 111 struct kmem_cache *drbd_request_cache; 112 struct kmem_cache *drbd_ee_cache; /* peer requests */ 113 struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */ 114 struct kmem_cache *drbd_al_ext_cache; /* activity log extents */ 115 mempool_t drbd_request_mempool; 116 mempool_t drbd_ee_mempool; 117 mempool_t drbd_md_io_page_pool; 118 mempool_t drbd_buffer_page_pool; 119 struct bio_set drbd_md_io_bio_set; 120 struct bio_set drbd_io_bio_set; 121 122 DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5); 123 124 static const struct block_device_operations drbd_ops = { 125 .owner = THIS_MODULE, 126 .submit_bio = drbd_submit_bio, 127 .open = drbd_open, 128 .release = drbd_release, 129 }; 130 131 #ifdef __CHECKER__ 132 /* When checking with sparse, and this is an inline function, sparse will 133 give tons of false positives. When this is a real functions sparse works. 134 */ 135 int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins) 136 { 137 int io_allowed; 138 139 atomic_inc(&device->local_cnt); 140 io_allowed = (device->state.disk >= mins); 141 if (!io_allowed) { 142 if (atomic_dec_and_test(&device->local_cnt)) 143 wake_up(&device->misc_wait); 144 } 145 return io_allowed; 146 } 147 148 #endif 149 150 /** 151 * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch 152 * @connection: DRBD connection. 153 * @barrier_nr: Expected identifier of the DRBD write barrier packet. 154 * @set_size: Expected number of requests before that barrier. 155 * 156 * In case the passed barrier_nr or set_size does not match the oldest 157 * epoch of not yet barrier-acked requests, this function will cause a 158 * termination of the connection. 159 */ 160 void tl_release(struct drbd_connection *connection, unsigned int barrier_nr, 161 unsigned int set_size) 162 { 163 struct drbd_request *r; 164 struct drbd_request *req = NULL, *tmp = NULL; 165 int expect_epoch = 0; 166 int expect_size = 0; 167 168 spin_lock_irq(&connection->resource->req_lock); 169 170 /* find oldest not yet barrier-acked write request, 171 * count writes in its epoch. */ 172 list_for_each_entry(r, &connection->transfer_log, tl_requests) { 173 const unsigned s = r->rq_state; 174 if (!req) { 175 if (!(s & RQ_WRITE)) 176 continue; 177 if (!(s & RQ_NET_MASK)) 178 continue; 179 if (s & RQ_NET_DONE) 180 continue; 181 req = r; 182 expect_epoch = req->epoch; 183 expect_size ++; 184 } else { 185 if (r->epoch != expect_epoch) 186 break; 187 if (!(s & RQ_WRITE)) 188 continue; 189 /* if (s & RQ_DONE): not expected */ 190 /* if (!(s & RQ_NET_MASK)): not expected */ 191 expect_size++; 192 } 193 } 194 195 /* first some paranoia code */ 196 if (req == NULL) { 197 drbd_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n", 198 barrier_nr); 199 goto bail; 200 } 201 if (expect_epoch != barrier_nr) { 202 drbd_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n", 203 barrier_nr, expect_epoch); 204 goto bail; 205 } 206 207 if (expect_size != set_size) { 208 drbd_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n", 209 barrier_nr, set_size, expect_size); 210 goto bail; 211 } 212 213 /* Clean up list of requests processed during current epoch. */ 214 /* this extra list walk restart is paranoia, 215 * to catch requests being barrier-acked "unexpectedly". 216 * It usually should find the same req again, or some READ preceding it. */ 217 list_for_each_entry(req, &connection->transfer_log, tl_requests) 218 if (req->epoch == expect_epoch) { 219 tmp = req; 220 break; 221 } 222 req = list_prepare_entry(tmp, &connection->transfer_log, tl_requests); 223 list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) { 224 struct drbd_peer_device *peer_device; 225 if (req->epoch != expect_epoch) 226 break; 227 peer_device = conn_peer_device(connection, req->device->vnr); 228 _req_mod(req, BARRIER_ACKED, peer_device); 229 } 230 spin_unlock_irq(&connection->resource->req_lock); 231 232 return; 233 234 bail: 235 spin_unlock_irq(&connection->resource->req_lock); 236 conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD); 237 } 238 239 240 /** 241 * _tl_restart() - Walks the transfer log, and applies an action to all requests 242 * @connection: DRBD connection to operate on. 243 * @what: The action/event to perform with all request objects 244 * 245 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO, 246 * RESTART_FROZEN_DISK_IO. 247 */ 248 /* must hold resource->req_lock */ 249 void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what) 250 { 251 struct drbd_peer_device *peer_device; 252 struct drbd_request *req, *r; 253 254 list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) { 255 peer_device = conn_peer_device(connection, req->device->vnr); 256 _req_mod(req, what, peer_device); 257 } 258 } 259 260 void tl_restart(struct drbd_connection *connection, enum drbd_req_event what) 261 { 262 spin_lock_irq(&connection->resource->req_lock); 263 _tl_restart(connection, what); 264 spin_unlock_irq(&connection->resource->req_lock); 265 } 266 267 /** 268 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL 269 * @connection: DRBD connection. 270 * 271 * This is called after the connection to the peer was lost. The storage covered 272 * by the requests on the transfer gets marked as our of sync. Called from the 273 * receiver thread and the worker thread. 274 */ 275 void tl_clear(struct drbd_connection *connection) 276 { 277 tl_restart(connection, CONNECTION_LOST_WHILE_PENDING); 278 } 279 280 /** 281 * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL 282 * @device: DRBD device. 283 */ 284 void tl_abort_disk_io(struct drbd_device *device) 285 { 286 struct drbd_connection *connection = first_peer_device(device)->connection; 287 struct drbd_request *req, *r; 288 289 spin_lock_irq(&connection->resource->req_lock); 290 list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) { 291 if (!(req->rq_state & RQ_LOCAL_PENDING)) 292 continue; 293 if (req->device != device) 294 continue; 295 _req_mod(req, ABORT_DISK_IO, NULL); 296 } 297 spin_unlock_irq(&connection->resource->req_lock); 298 } 299 300 static int drbd_thread_setup(void *arg) 301 { 302 struct drbd_thread *thi = (struct drbd_thread *) arg; 303 struct drbd_resource *resource = thi->resource; 304 unsigned long flags; 305 int retval; 306 307 snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s", 308 thi->name[0], 309 resource->name); 310 311 allow_kernel_signal(DRBD_SIGKILL); 312 allow_kernel_signal(SIGXCPU); 313 restart: 314 retval = thi->function(thi); 315 316 spin_lock_irqsave(&thi->t_lock, flags); 317 318 /* if the receiver has been "EXITING", the last thing it did 319 * was set the conn state to "StandAlone", 320 * if now a re-connect request comes in, conn state goes C_UNCONNECTED, 321 * and receiver thread will be "started". 322 * drbd_thread_start needs to set "RESTARTING" in that case. 323 * t_state check and assignment needs to be within the same spinlock, 324 * so either thread_start sees EXITING, and can remap to RESTARTING, 325 * or thread_start see NONE, and can proceed as normal. 326 */ 327 328 if (thi->t_state == RESTARTING) { 329 drbd_info(resource, "Restarting %s thread\n", thi->name); 330 thi->t_state = RUNNING; 331 spin_unlock_irqrestore(&thi->t_lock, flags); 332 goto restart; 333 } 334 335 thi->task = NULL; 336 thi->t_state = NONE; 337 smp_mb(); 338 complete_all(&thi->stop); 339 spin_unlock_irqrestore(&thi->t_lock, flags); 340 341 drbd_info(resource, "Terminating %s\n", current->comm); 342 343 /* Release mod reference taken when thread was started */ 344 345 if (thi->connection) 346 kref_put(&thi->connection->kref, drbd_destroy_connection); 347 kref_put(&resource->kref, drbd_destroy_resource); 348 module_put(THIS_MODULE); 349 return retval; 350 } 351 352 static void drbd_thread_init(struct drbd_resource *resource, struct drbd_thread *thi, 353 int (*func) (struct drbd_thread *), const char *name) 354 { 355 spin_lock_init(&thi->t_lock); 356 thi->task = NULL; 357 thi->t_state = NONE; 358 thi->function = func; 359 thi->resource = resource; 360 thi->connection = NULL; 361 thi->name = name; 362 } 363 364 int drbd_thread_start(struct drbd_thread *thi) 365 { 366 struct drbd_resource *resource = thi->resource; 367 struct task_struct *nt; 368 unsigned long flags; 369 370 /* is used from state engine doing drbd_thread_stop_nowait, 371 * while holding the req lock irqsave */ 372 spin_lock_irqsave(&thi->t_lock, flags); 373 374 switch (thi->t_state) { 375 case NONE: 376 drbd_info(resource, "Starting %s thread (from %s [%d])\n", 377 thi->name, current->comm, current->pid); 378 379 /* Get ref on module for thread - this is released when thread exits */ 380 if (!try_module_get(THIS_MODULE)) { 381 drbd_err(resource, "Failed to get module reference in drbd_thread_start\n"); 382 spin_unlock_irqrestore(&thi->t_lock, flags); 383 return false; 384 } 385 386 kref_get(&resource->kref); 387 if (thi->connection) 388 kref_get(&thi->connection->kref); 389 390 init_completion(&thi->stop); 391 thi->reset_cpu_mask = 1; 392 thi->t_state = RUNNING; 393 spin_unlock_irqrestore(&thi->t_lock, flags); 394 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */ 395 396 nt = kthread_create(drbd_thread_setup, (void *) thi, 397 "drbd_%c_%s", thi->name[0], thi->resource->name); 398 399 if (IS_ERR(nt)) { 400 drbd_err(resource, "Couldn't start thread\n"); 401 402 if (thi->connection) 403 kref_put(&thi->connection->kref, drbd_destroy_connection); 404 kref_put(&resource->kref, drbd_destroy_resource); 405 module_put(THIS_MODULE); 406 return false; 407 } 408 spin_lock_irqsave(&thi->t_lock, flags); 409 thi->task = nt; 410 thi->t_state = RUNNING; 411 spin_unlock_irqrestore(&thi->t_lock, flags); 412 wake_up_process(nt); 413 break; 414 case EXITING: 415 thi->t_state = RESTARTING; 416 drbd_info(resource, "Restarting %s thread (from %s [%d])\n", 417 thi->name, current->comm, current->pid); 418 fallthrough; 419 case RUNNING: 420 case RESTARTING: 421 default: 422 spin_unlock_irqrestore(&thi->t_lock, flags); 423 break; 424 } 425 426 return true; 427 } 428 429 430 void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait) 431 { 432 unsigned long flags; 433 434 enum drbd_thread_state ns = restart ? RESTARTING : EXITING; 435 436 /* may be called from state engine, holding the req lock irqsave */ 437 spin_lock_irqsave(&thi->t_lock, flags); 438 439 if (thi->t_state == NONE) { 440 spin_unlock_irqrestore(&thi->t_lock, flags); 441 if (restart) 442 drbd_thread_start(thi); 443 return; 444 } 445 446 if (thi->t_state != ns) { 447 if (thi->task == NULL) { 448 spin_unlock_irqrestore(&thi->t_lock, flags); 449 return; 450 } 451 452 thi->t_state = ns; 453 smp_mb(); 454 init_completion(&thi->stop); 455 if (thi->task != current) 456 send_sig(DRBD_SIGKILL, thi->task, 1); 457 } 458 459 spin_unlock_irqrestore(&thi->t_lock, flags); 460 461 if (wait) 462 wait_for_completion(&thi->stop); 463 } 464 465 #ifdef CONFIG_SMP 466 /* 467 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs 468 * 469 * Forces all threads of a resource onto the same CPU. This is beneficial for 470 * DRBD's performance. May be overwritten by user's configuration. 471 */ 472 static void drbd_calc_cpu_mask(cpumask_var_t *cpu_mask) 473 { 474 unsigned int *resources_per_cpu, min_index = ~0; 475 476 resources_per_cpu = kcalloc(nr_cpu_ids, sizeof(*resources_per_cpu), 477 GFP_KERNEL); 478 if (resources_per_cpu) { 479 struct drbd_resource *resource; 480 unsigned int cpu, min = ~0; 481 482 rcu_read_lock(); 483 for_each_resource_rcu(resource, &drbd_resources) { 484 for_each_cpu(cpu, resource->cpu_mask) 485 resources_per_cpu[cpu]++; 486 } 487 rcu_read_unlock(); 488 for_each_online_cpu(cpu) { 489 if (resources_per_cpu[cpu] < min) { 490 min = resources_per_cpu[cpu]; 491 min_index = cpu; 492 } 493 } 494 kfree(resources_per_cpu); 495 } 496 if (min_index == ~0) { 497 cpumask_setall(*cpu_mask); 498 return; 499 } 500 cpumask_set_cpu(min_index, *cpu_mask); 501 } 502 503 /** 504 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread 505 * @thi: drbd_thread object 506 * 507 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die 508 * prematurely. 509 */ 510 void drbd_thread_current_set_cpu(struct drbd_thread *thi) 511 { 512 struct drbd_resource *resource = thi->resource; 513 struct task_struct *p = current; 514 515 if (!thi->reset_cpu_mask) 516 return; 517 thi->reset_cpu_mask = 0; 518 set_cpus_allowed_ptr(p, resource->cpu_mask); 519 } 520 #else 521 #define drbd_calc_cpu_mask(A) ({}) 522 #endif 523 524 /* 525 * drbd_header_size - size of a packet header 526 * 527 * The header size is a multiple of 8, so any payload following the header is 528 * word aligned on 64-bit architectures. (The bitmap send and receive code 529 * relies on this.) 530 */ 531 unsigned int drbd_header_size(struct drbd_connection *connection) 532 { 533 if (connection->agreed_pro_version >= 100) { 534 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8)); 535 return sizeof(struct p_header100); 536 } else { 537 BUILD_BUG_ON(sizeof(struct p_header80) != 538 sizeof(struct p_header95)); 539 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8)); 540 return sizeof(struct p_header80); 541 } 542 } 543 544 static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size) 545 { 546 h->magic = cpu_to_be32(DRBD_MAGIC); 547 h->command = cpu_to_be16(cmd); 548 h->length = cpu_to_be16(size); 549 return sizeof(struct p_header80); 550 } 551 552 static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size) 553 { 554 h->magic = cpu_to_be16(DRBD_MAGIC_BIG); 555 h->command = cpu_to_be16(cmd); 556 h->length = cpu_to_be32(size); 557 return sizeof(struct p_header95); 558 } 559 560 static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd, 561 int size, int vnr) 562 { 563 h->magic = cpu_to_be32(DRBD_MAGIC_100); 564 h->volume = cpu_to_be16(vnr); 565 h->command = cpu_to_be16(cmd); 566 h->length = cpu_to_be32(size); 567 h->pad = 0; 568 return sizeof(struct p_header100); 569 } 570 571 static unsigned int prepare_header(struct drbd_connection *connection, int vnr, 572 void *buffer, enum drbd_packet cmd, int size) 573 { 574 if (connection->agreed_pro_version >= 100) 575 return prepare_header100(buffer, cmd, size, vnr); 576 else if (connection->agreed_pro_version >= 95 && 577 size > DRBD_MAX_SIZE_H80_PACKET) 578 return prepare_header95(buffer, cmd, size); 579 else 580 return prepare_header80(buffer, cmd, size); 581 } 582 583 static void *__conn_prepare_command(struct drbd_connection *connection, 584 struct drbd_socket *sock) 585 { 586 if (!sock->socket) 587 return NULL; 588 return sock->sbuf + drbd_header_size(connection); 589 } 590 591 void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock) 592 { 593 void *p; 594 595 mutex_lock(&sock->mutex); 596 p = __conn_prepare_command(connection, sock); 597 if (!p) 598 mutex_unlock(&sock->mutex); 599 600 return p; 601 } 602 603 void *drbd_prepare_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock) 604 { 605 return conn_prepare_command(peer_device->connection, sock); 606 } 607 608 static int __send_command(struct drbd_connection *connection, int vnr, 609 struct drbd_socket *sock, enum drbd_packet cmd, 610 unsigned int header_size, void *data, 611 unsigned int size) 612 { 613 int msg_flags; 614 int err; 615 616 /* 617 * Called with @data == NULL and the size of the data blocks in @size 618 * for commands that send data blocks. For those commands, omit the 619 * MSG_MORE flag: this will increase the likelihood that data blocks 620 * which are page aligned on the sender will end up page aligned on the 621 * receiver. 622 */ 623 msg_flags = data ? MSG_MORE : 0; 624 625 header_size += prepare_header(connection, vnr, sock->sbuf, cmd, 626 header_size + size); 627 err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size, 628 msg_flags); 629 if (data && !err) 630 err = drbd_send_all(connection, sock->socket, data, size, 0); 631 /* DRBD protocol "pings" are latency critical. 632 * This is supposed to trigger tcp_push_pending_frames() */ 633 if (!err && (cmd == P_PING || cmd == P_PING_ACK)) 634 tcp_sock_set_nodelay(sock->socket->sk); 635 636 return err; 637 } 638 639 static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock, 640 enum drbd_packet cmd, unsigned int header_size, 641 void *data, unsigned int size) 642 { 643 return __send_command(connection, 0, sock, cmd, header_size, data, size); 644 } 645 646 int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock, 647 enum drbd_packet cmd, unsigned int header_size, 648 void *data, unsigned int size) 649 { 650 int err; 651 652 err = __conn_send_command(connection, sock, cmd, header_size, data, size); 653 mutex_unlock(&sock->mutex); 654 return err; 655 } 656 657 int drbd_send_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock, 658 enum drbd_packet cmd, unsigned int header_size, 659 void *data, unsigned int size) 660 { 661 int err; 662 663 err = __send_command(peer_device->connection, peer_device->device->vnr, 664 sock, cmd, header_size, data, size); 665 mutex_unlock(&sock->mutex); 666 return err; 667 } 668 669 int drbd_send_ping(struct drbd_connection *connection) 670 { 671 struct drbd_socket *sock; 672 673 sock = &connection->meta; 674 if (!conn_prepare_command(connection, sock)) 675 return -EIO; 676 return conn_send_command(connection, sock, P_PING, 0, NULL, 0); 677 } 678 679 int drbd_send_ping_ack(struct drbd_connection *connection) 680 { 681 struct drbd_socket *sock; 682 683 sock = &connection->meta; 684 if (!conn_prepare_command(connection, sock)) 685 return -EIO; 686 return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0); 687 } 688 689 int drbd_send_sync_param(struct drbd_peer_device *peer_device) 690 { 691 struct drbd_socket *sock; 692 struct p_rs_param_95 *p; 693 int size; 694 const int apv = peer_device->connection->agreed_pro_version; 695 enum drbd_packet cmd; 696 struct net_conf *nc; 697 struct disk_conf *dc; 698 699 sock = &peer_device->connection->data; 700 p = drbd_prepare_command(peer_device, sock); 701 if (!p) 702 return -EIO; 703 704 rcu_read_lock(); 705 nc = rcu_dereference(peer_device->connection->net_conf); 706 707 size = apv <= 87 ? sizeof(struct p_rs_param) 708 : apv == 88 ? sizeof(struct p_rs_param) 709 + strlen(nc->verify_alg) + 1 710 : apv <= 94 ? sizeof(struct p_rs_param_89) 711 : /* apv >= 95 */ sizeof(struct p_rs_param_95); 712 713 cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM; 714 715 /* initialize verify_alg and csums_alg */ 716 BUILD_BUG_ON(sizeof(p->algs) != 2 * SHARED_SECRET_MAX); 717 memset(&p->algs, 0, sizeof(p->algs)); 718 719 if (get_ldev(peer_device->device)) { 720 dc = rcu_dereference(peer_device->device->ldev->disk_conf); 721 p->resync_rate = cpu_to_be32(dc->resync_rate); 722 p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead); 723 p->c_delay_target = cpu_to_be32(dc->c_delay_target); 724 p->c_fill_target = cpu_to_be32(dc->c_fill_target); 725 p->c_max_rate = cpu_to_be32(dc->c_max_rate); 726 put_ldev(peer_device->device); 727 } else { 728 p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF); 729 p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF); 730 p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF); 731 p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF); 732 p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF); 733 } 734 735 if (apv >= 88) 736 strscpy(p->verify_alg, nc->verify_alg); 737 if (apv >= 89) 738 strscpy(p->csums_alg, nc->csums_alg); 739 rcu_read_unlock(); 740 741 return drbd_send_command(peer_device, sock, cmd, size, NULL, 0); 742 } 743 744 int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd) 745 { 746 struct drbd_socket *sock; 747 struct p_protocol *p; 748 struct net_conf *nc; 749 size_t integrity_alg_len; 750 int size, cf; 751 752 sock = &connection->data; 753 p = __conn_prepare_command(connection, sock); 754 if (!p) 755 return -EIO; 756 757 rcu_read_lock(); 758 nc = rcu_dereference(connection->net_conf); 759 760 if (nc->tentative && connection->agreed_pro_version < 92) { 761 rcu_read_unlock(); 762 drbd_err(connection, "--dry-run is not supported by peer"); 763 return -EOPNOTSUPP; 764 } 765 766 size = sizeof(*p); 767 if (connection->agreed_pro_version >= 87) { 768 integrity_alg_len = strlen(nc->integrity_alg) + 1; 769 size += integrity_alg_len; 770 } 771 772 p->protocol = cpu_to_be32(nc->wire_protocol); 773 p->after_sb_0p = cpu_to_be32(nc->after_sb_0p); 774 p->after_sb_1p = cpu_to_be32(nc->after_sb_1p); 775 p->after_sb_2p = cpu_to_be32(nc->after_sb_2p); 776 p->two_primaries = cpu_to_be32(nc->two_primaries); 777 cf = 0; 778 if (nc->discard_my_data) 779 cf |= CF_DISCARD_MY_DATA; 780 if (nc->tentative) 781 cf |= CF_DRY_RUN; 782 p->conn_flags = cpu_to_be32(cf); 783 784 if (connection->agreed_pro_version >= 87) 785 strscpy(p->integrity_alg, nc->integrity_alg, integrity_alg_len); 786 rcu_read_unlock(); 787 788 return __conn_send_command(connection, sock, cmd, size, NULL, 0); 789 } 790 791 int drbd_send_protocol(struct drbd_connection *connection) 792 { 793 int err; 794 795 mutex_lock(&connection->data.mutex); 796 err = __drbd_send_protocol(connection, P_PROTOCOL); 797 mutex_unlock(&connection->data.mutex); 798 799 return err; 800 } 801 802 static int _drbd_send_uuids(struct drbd_peer_device *peer_device, u64 uuid_flags) 803 { 804 struct drbd_device *device = peer_device->device; 805 struct drbd_socket *sock; 806 struct p_uuids *p; 807 int i; 808 809 if (!get_ldev_if_state(device, D_NEGOTIATING)) 810 return 0; 811 812 sock = &peer_device->connection->data; 813 p = drbd_prepare_command(peer_device, sock); 814 if (!p) { 815 put_ldev(device); 816 return -EIO; 817 } 818 spin_lock_irq(&device->ldev->md.uuid_lock); 819 for (i = UI_CURRENT; i < UI_SIZE; i++) 820 p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]); 821 spin_unlock_irq(&device->ldev->md.uuid_lock); 822 823 device->comm_bm_set = drbd_bm_total_weight(device); 824 p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set); 825 rcu_read_lock(); 826 uuid_flags |= rcu_dereference(peer_device->connection->net_conf)->discard_my_data ? 1 : 0; 827 rcu_read_unlock(); 828 uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0; 829 uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0; 830 p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags); 831 832 put_ldev(device); 833 return drbd_send_command(peer_device, sock, P_UUIDS, sizeof(*p), NULL, 0); 834 } 835 836 int drbd_send_uuids(struct drbd_peer_device *peer_device) 837 { 838 return _drbd_send_uuids(peer_device, 0); 839 } 840 841 int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *peer_device) 842 { 843 return _drbd_send_uuids(peer_device, 8); 844 } 845 846 void drbd_print_uuids(struct drbd_device *device, const char *text) 847 { 848 if (get_ldev_if_state(device, D_NEGOTIATING)) { 849 u64 *uuid = device->ldev->md.uuid; 850 drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX\n", 851 text, 852 (unsigned long long)uuid[UI_CURRENT], 853 (unsigned long long)uuid[UI_BITMAP], 854 (unsigned long long)uuid[UI_HISTORY_START], 855 (unsigned long long)uuid[UI_HISTORY_END]); 856 put_ldev(device); 857 } else { 858 drbd_info(device, "%s effective data uuid: %016llX\n", 859 text, 860 (unsigned long long)device->ed_uuid); 861 } 862 } 863 864 void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *peer_device) 865 { 866 struct drbd_device *device = peer_device->device; 867 struct drbd_socket *sock; 868 struct p_rs_uuid *p; 869 u64 uuid; 870 871 D_ASSERT(device, device->state.disk == D_UP_TO_DATE); 872 873 uuid = device->ldev->md.uuid[UI_BITMAP]; 874 if (uuid && uuid != UUID_JUST_CREATED) 875 uuid = uuid + UUID_NEW_BM_OFFSET; 876 else 877 uuid = get_random_u64(); 878 drbd_uuid_set(device, UI_BITMAP, uuid); 879 drbd_print_uuids(device, "updated sync UUID"); 880 drbd_md_sync(device); 881 882 sock = &peer_device->connection->data; 883 p = drbd_prepare_command(peer_device, sock); 884 if (p) { 885 p->uuid = cpu_to_be64(uuid); 886 drbd_send_command(peer_device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0); 887 } 888 } 889 890 int drbd_send_sizes(struct drbd_peer_device *peer_device, int trigger_reply, enum dds_flags flags) 891 { 892 struct drbd_device *device = peer_device->device; 893 struct drbd_socket *sock; 894 struct p_sizes *p; 895 sector_t d_size, u_size; 896 int q_order_type; 897 unsigned int max_bio_size; 898 unsigned int packet_size; 899 900 sock = &peer_device->connection->data; 901 p = drbd_prepare_command(peer_device, sock); 902 if (!p) 903 return -EIO; 904 905 packet_size = sizeof(*p); 906 if (peer_device->connection->agreed_features & DRBD_FF_WSAME) 907 packet_size += sizeof(p->qlim[0]); 908 909 memset(p, 0, packet_size); 910 if (get_ldev_if_state(device, D_NEGOTIATING)) { 911 struct block_device *bdev = device->ldev->backing_bdev; 912 struct request_queue *q = bdev_get_queue(bdev); 913 914 d_size = drbd_get_max_capacity(device->ldev); 915 rcu_read_lock(); 916 u_size = rcu_dereference(device->ldev->disk_conf)->disk_size; 917 rcu_read_unlock(); 918 q_order_type = drbd_queue_order_type(device); 919 max_bio_size = queue_max_hw_sectors(q) << 9; 920 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE); 921 p->qlim->physical_block_size = 922 cpu_to_be32(bdev_physical_block_size(bdev)); 923 p->qlim->logical_block_size = 924 cpu_to_be32(bdev_logical_block_size(bdev)); 925 p->qlim->alignment_offset = 926 cpu_to_be32(bdev_alignment_offset(bdev)); 927 p->qlim->io_min = cpu_to_be32(bdev_io_min(bdev)); 928 p->qlim->io_opt = cpu_to_be32(bdev_io_opt(bdev)); 929 p->qlim->discard_enabled = !!bdev_max_discard_sectors(bdev); 930 put_ldev(device); 931 } else { 932 struct request_queue *q = device->rq_queue; 933 934 p->qlim->physical_block_size = 935 cpu_to_be32(queue_physical_block_size(q)); 936 p->qlim->logical_block_size = 937 cpu_to_be32(queue_logical_block_size(q)); 938 p->qlim->alignment_offset = 0; 939 p->qlim->io_min = cpu_to_be32(queue_io_min(q)); 940 p->qlim->io_opt = cpu_to_be32(queue_io_opt(q)); 941 p->qlim->discard_enabled = 0; 942 943 d_size = 0; 944 u_size = 0; 945 q_order_type = QUEUE_ORDERED_NONE; 946 max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */ 947 } 948 949 if (peer_device->connection->agreed_pro_version <= 94) 950 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET); 951 else if (peer_device->connection->agreed_pro_version < 100) 952 max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95); 953 954 p->d_size = cpu_to_be64(d_size); 955 p->u_size = cpu_to_be64(u_size); 956 if (trigger_reply) 957 p->c_size = 0; 958 else 959 p->c_size = cpu_to_be64(get_capacity(device->vdisk)); 960 p->max_bio_size = cpu_to_be32(max_bio_size); 961 p->queue_order_type = cpu_to_be16(q_order_type); 962 p->dds_flags = cpu_to_be16(flags); 963 964 return drbd_send_command(peer_device, sock, P_SIZES, packet_size, NULL, 0); 965 } 966 967 /** 968 * drbd_send_current_state() - Sends the drbd state to the peer 969 * @peer_device: DRBD peer device. 970 */ 971 int drbd_send_current_state(struct drbd_peer_device *peer_device) 972 { 973 struct drbd_socket *sock; 974 struct p_state *p; 975 976 sock = &peer_device->connection->data; 977 p = drbd_prepare_command(peer_device, sock); 978 if (!p) 979 return -EIO; 980 p->state = cpu_to_be32(peer_device->device->state.i); /* Within the send mutex */ 981 return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0); 982 } 983 984 /** 985 * drbd_send_state() - After a state change, sends the new state to the peer 986 * @peer_device: DRBD peer device. 987 * @state: the state to send, not necessarily the current state. 988 * 989 * Each state change queues an "after_state_ch" work, which will eventually 990 * send the resulting new state to the peer. If more state changes happen 991 * between queuing and processing of the after_state_ch work, we still 992 * want to send each intermediary state in the order it occurred. 993 */ 994 int drbd_send_state(struct drbd_peer_device *peer_device, union drbd_state state) 995 { 996 struct drbd_socket *sock; 997 struct p_state *p; 998 999 sock = &peer_device->connection->data; 1000 p = drbd_prepare_command(peer_device, sock); 1001 if (!p) 1002 return -EIO; 1003 p->state = cpu_to_be32(state.i); /* Within the send mutex */ 1004 return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0); 1005 } 1006 1007 int drbd_send_state_req(struct drbd_peer_device *peer_device, union drbd_state mask, union drbd_state val) 1008 { 1009 struct drbd_socket *sock; 1010 struct p_req_state *p; 1011 1012 sock = &peer_device->connection->data; 1013 p = drbd_prepare_command(peer_device, sock); 1014 if (!p) 1015 return -EIO; 1016 p->mask = cpu_to_be32(mask.i); 1017 p->val = cpu_to_be32(val.i); 1018 return drbd_send_command(peer_device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0); 1019 } 1020 1021 int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val) 1022 { 1023 enum drbd_packet cmd; 1024 struct drbd_socket *sock; 1025 struct p_req_state *p; 1026 1027 cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ; 1028 sock = &connection->data; 1029 p = conn_prepare_command(connection, sock); 1030 if (!p) 1031 return -EIO; 1032 p->mask = cpu_to_be32(mask.i); 1033 p->val = cpu_to_be32(val.i); 1034 return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0); 1035 } 1036 1037 void drbd_send_sr_reply(struct drbd_peer_device *peer_device, enum drbd_state_rv retcode) 1038 { 1039 struct drbd_socket *sock; 1040 struct p_req_state_reply *p; 1041 1042 sock = &peer_device->connection->meta; 1043 p = drbd_prepare_command(peer_device, sock); 1044 if (p) { 1045 p->retcode = cpu_to_be32(retcode); 1046 drbd_send_command(peer_device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0); 1047 } 1048 } 1049 1050 void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode) 1051 { 1052 struct drbd_socket *sock; 1053 struct p_req_state_reply *p; 1054 enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY; 1055 1056 sock = &connection->meta; 1057 p = conn_prepare_command(connection, sock); 1058 if (p) { 1059 p->retcode = cpu_to_be32(retcode); 1060 conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0); 1061 } 1062 } 1063 1064 static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code) 1065 { 1066 BUG_ON(code & ~0xf); 1067 p->encoding = (p->encoding & ~0xf) | code; 1068 } 1069 1070 static void dcbp_set_start(struct p_compressed_bm *p, int set) 1071 { 1072 p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0); 1073 } 1074 1075 static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n) 1076 { 1077 BUG_ON(n & ~0x7); 1078 p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4); 1079 } 1080 1081 static int fill_bitmap_rle_bits(struct drbd_device *device, 1082 struct p_compressed_bm *p, 1083 unsigned int size, 1084 struct bm_xfer_ctx *c) 1085 { 1086 struct bitstream bs; 1087 unsigned long plain_bits; 1088 unsigned long tmp; 1089 unsigned long rl; 1090 unsigned len; 1091 unsigned toggle; 1092 int bits, use_rle; 1093 1094 /* may we use this feature? */ 1095 rcu_read_lock(); 1096 use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle; 1097 rcu_read_unlock(); 1098 if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90) 1099 return 0; 1100 1101 if (c->bit_offset >= c->bm_bits) 1102 return 0; /* nothing to do. */ 1103 1104 /* use at most thus many bytes */ 1105 bitstream_init(&bs, p->code, size, 0); 1106 memset(p->code, 0, size); 1107 /* plain bits covered in this code string */ 1108 plain_bits = 0; 1109 1110 /* p->encoding & 0x80 stores whether the first run length is set. 1111 * bit offset is implicit. 1112 * start with toggle == 2 to be able to tell the first iteration */ 1113 toggle = 2; 1114 1115 /* see how much plain bits we can stuff into one packet 1116 * using RLE and VLI. */ 1117 do { 1118 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset) 1119 : _drbd_bm_find_next(device, c->bit_offset); 1120 if (tmp == -1UL) 1121 tmp = c->bm_bits; 1122 rl = tmp - c->bit_offset; 1123 1124 if (toggle == 2) { /* first iteration */ 1125 if (rl == 0) { 1126 /* the first checked bit was set, 1127 * store start value, */ 1128 dcbp_set_start(p, 1); 1129 /* but skip encoding of zero run length */ 1130 toggle = !toggle; 1131 continue; 1132 } 1133 dcbp_set_start(p, 0); 1134 } 1135 1136 /* paranoia: catch zero runlength. 1137 * can only happen if bitmap is modified while we scan it. */ 1138 if (rl == 0) { 1139 drbd_err(device, "unexpected zero runlength while encoding bitmap " 1140 "t:%u bo:%lu\n", toggle, c->bit_offset); 1141 return -1; 1142 } 1143 1144 bits = vli_encode_bits(&bs, rl); 1145 if (bits == -ENOBUFS) /* buffer full */ 1146 break; 1147 if (bits <= 0) { 1148 drbd_err(device, "error while encoding bitmap: %d\n", bits); 1149 return 0; 1150 } 1151 1152 toggle = !toggle; 1153 plain_bits += rl; 1154 c->bit_offset = tmp; 1155 } while (c->bit_offset < c->bm_bits); 1156 1157 len = bs.cur.b - p->code + !!bs.cur.bit; 1158 1159 if (plain_bits < (len << 3)) { 1160 /* incompressible with this method. 1161 * we need to rewind both word and bit position. */ 1162 c->bit_offset -= plain_bits; 1163 bm_xfer_ctx_bit_to_word_offset(c); 1164 c->bit_offset = c->word_offset * BITS_PER_LONG; 1165 return 0; 1166 } 1167 1168 /* RLE + VLI was able to compress it just fine. 1169 * update c->word_offset. */ 1170 bm_xfer_ctx_bit_to_word_offset(c); 1171 1172 /* store pad_bits */ 1173 dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7); 1174 1175 return len; 1176 } 1177 1178 /* 1179 * send_bitmap_rle_or_plain 1180 * 1181 * Return 0 when done, 1 when another iteration is needed, and a negative error 1182 * code upon failure. 1183 */ 1184 static int 1185 send_bitmap_rle_or_plain(struct drbd_peer_device *peer_device, struct bm_xfer_ctx *c) 1186 { 1187 struct drbd_device *device = peer_device->device; 1188 struct drbd_socket *sock = &peer_device->connection->data; 1189 unsigned int header_size = drbd_header_size(peer_device->connection); 1190 struct p_compressed_bm *p = sock->sbuf + header_size; 1191 int len, err; 1192 1193 len = fill_bitmap_rle_bits(device, p, 1194 DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c); 1195 if (len < 0) 1196 return -EIO; 1197 1198 if (len) { 1199 dcbp_set_code(p, RLE_VLI_Bits); 1200 err = __send_command(peer_device->connection, device->vnr, sock, 1201 P_COMPRESSED_BITMAP, sizeof(*p) + len, 1202 NULL, 0); 1203 c->packets[0]++; 1204 c->bytes[0] += header_size + sizeof(*p) + len; 1205 1206 if (c->bit_offset >= c->bm_bits) 1207 len = 0; /* DONE */ 1208 } else { 1209 /* was not compressible. 1210 * send a buffer full of plain text bits instead. */ 1211 unsigned int data_size; 1212 unsigned long num_words; 1213 unsigned long *p = sock->sbuf + header_size; 1214 1215 data_size = DRBD_SOCKET_BUFFER_SIZE - header_size; 1216 num_words = min_t(size_t, data_size / sizeof(*p), 1217 c->bm_words - c->word_offset); 1218 len = num_words * sizeof(*p); 1219 if (len) 1220 drbd_bm_get_lel(device, c->word_offset, num_words, p); 1221 err = __send_command(peer_device->connection, device->vnr, sock, P_BITMAP, 1222 len, NULL, 0); 1223 c->word_offset += num_words; 1224 c->bit_offset = c->word_offset * BITS_PER_LONG; 1225 1226 c->packets[1]++; 1227 c->bytes[1] += header_size + len; 1228 1229 if (c->bit_offset > c->bm_bits) 1230 c->bit_offset = c->bm_bits; 1231 } 1232 if (!err) { 1233 if (len == 0) { 1234 INFO_bm_xfer_stats(peer_device, "send", c); 1235 return 0; 1236 } else 1237 return 1; 1238 } 1239 return -EIO; 1240 } 1241 1242 /* See the comment at receive_bitmap() */ 1243 static int _drbd_send_bitmap(struct drbd_device *device, 1244 struct drbd_peer_device *peer_device) 1245 { 1246 struct bm_xfer_ctx c; 1247 int err; 1248 1249 if (!expect(device, device->bitmap)) 1250 return false; 1251 1252 if (get_ldev(device)) { 1253 if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) { 1254 drbd_info(device, "Writing the whole bitmap, MDF_FullSync was set.\n"); 1255 drbd_bm_set_all(device); 1256 if (drbd_bm_write(device, peer_device)) { 1257 /* write_bm did fail! Leave full sync flag set in Meta P_DATA 1258 * but otherwise process as per normal - need to tell other 1259 * side that a full resync is required! */ 1260 drbd_err(device, "Failed to write bitmap to disk!\n"); 1261 } else { 1262 drbd_md_clear_flag(device, MDF_FULL_SYNC); 1263 drbd_md_sync(device); 1264 } 1265 } 1266 put_ldev(device); 1267 } 1268 1269 c = (struct bm_xfer_ctx) { 1270 .bm_bits = drbd_bm_bits(device), 1271 .bm_words = drbd_bm_words(device), 1272 }; 1273 1274 do { 1275 err = send_bitmap_rle_or_plain(peer_device, &c); 1276 } while (err > 0); 1277 1278 return err == 0; 1279 } 1280 1281 int drbd_send_bitmap(struct drbd_device *device, struct drbd_peer_device *peer_device) 1282 { 1283 struct drbd_socket *sock = &peer_device->connection->data; 1284 int err = -1; 1285 1286 mutex_lock(&sock->mutex); 1287 if (sock->socket) 1288 err = !_drbd_send_bitmap(device, peer_device); 1289 mutex_unlock(&sock->mutex); 1290 return err; 1291 } 1292 1293 void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size) 1294 { 1295 struct drbd_socket *sock; 1296 struct p_barrier_ack *p; 1297 1298 if (connection->cstate < C_WF_REPORT_PARAMS) 1299 return; 1300 1301 sock = &connection->meta; 1302 p = conn_prepare_command(connection, sock); 1303 if (!p) 1304 return; 1305 p->barrier = barrier_nr; 1306 p->set_size = cpu_to_be32(set_size); 1307 conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0); 1308 } 1309 1310 /** 1311 * _drbd_send_ack() - Sends an ack packet 1312 * @peer_device: DRBD peer device. 1313 * @cmd: Packet command code. 1314 * @sector: sector, needs to be in big endian byte order 1315 * @blksize: size in byte, needs to be in big endian byte order 1316 * @block_id: Id, big endian byte order 1317 */ 1318 static int _drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1319 u64 sector, u32 blksize, u64 block_id) 1320 { 1321 struct drbd_socket *sock; 1322 struct p_block_ack *p; 1323 1324 if (peer_device->device->state.conn < C_CONNECTED) 1325 return -EIO; 1326 1327 sock = &peer_device->connection->meta; 1328 p = drbd_prepare_command(peer_device, sock); 1329 if (!p) 1330 return -EIO; 1331 p->sector = sector; 1332 p->block_id = block_id; 1333 p->blksize = blksize; 1334 p->seq_num = cpu_to_be32(atomic_inc_return(&peer_device->device->packet_seq)); 1335 return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0); 1336 } 1337 1338 /* dp->sector and dp->block_id already/still in network byte order, 1339 * data_size is payload size according to dp->head, 1340 * and may need to be corrected for digest size. */ 1341 void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1342 struct p_data *dp, int data_size) 1343 { 1344 if (peer_device->connection->peer_integrity_tfm) 1345 data_size -= crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm); 1346 _drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size), 1347 dp->block_id); 1348 } 1349 1350 void drbd_send_ack_rp(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1351 struct p_block_req *rp) 1352 { 1353 _drbd_send_ack(peer_device, cmd, rp->sector, rp->blksize, rp->block_id); 1354 } 1355 1356 /** 1357 * drbd_send_ack() - Sends an ack packet 1358 * @peer_device: DRBD peer device 1359 * @cmd: packet command code 1360 * @peer_req: peer request 1361 */ 1362 int drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1363 struct drbd_peer_request *peer_req) 1364 { 1365 return _drbd_send_ack(peer_device, cmd, 1366 cpu_to_be64(peer_req->i.sector), 1367 cpu_to_be32(peer_req->i.size), 1368 peer_req->block_id); 1369 } 1370 1371 /* This function misuses the block_id field to signal if the blocks 1372 * are is sync or not. */ 1373 int drbd_send_ack_ex(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1374 sector_t sector, int blksize, u64 block_id) 1375 { 1376 return _drbd_send_ack(peer_device, cmd, 1377 cpu_to_be64(sector), 1378 cpu_to_be32(blksize), 1379 cpu_to_be64(block_id)); 1380 } 1381 1382 int drbd_send_rs_deallocated(struct drbd_peer_device *peer_device, 1383 struct drbd_peer_request *peer_req) 1384 { 1385 struct drbd_socket *sock; 1386 struct p_block_desc *p; 1387 1388 sock = &peer_device->connection->data; 1389 p = drbd_prepare_command(peer_device, sock); 1390 if (!p) 1391 return -EIO; 1392 p->sector = cpu_to_be64(peer_req->i.sector); 1393 p->blksize = cpu_to_be32(peer_req->i.size); 1394 p->pad = 0; 1395 return drbd_send_command(peer_device, sock, P_RS_DEALLOCATED, sizeof(*p), NULL, 0); 1396 } 1397 1398 int drbd_send_drequest(struct drbd_peer_device *peer_device, int cmd, 1399 sector_t sector, int size, u64 block_id) 1400 { 1401 struct drbd_socket *sock; 1402 struct p_block_req *p; 1403 1404 sock = &peer_device->connection->data; 1405 p = drbd_prepare_command(peer_device, sock); 1406 if (!p) 1407 return -EIO; 1408 p->sector = cpu_to_be64(sector); 1409 p->block_id = block_id; 1410 p->blksize = cpu_to_be32(size); 1411 return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0); 1412 } 1413 1414 int drbd_send_drequest_csum(struct drbd_peer_device *peer_device, sector_t sector, int size, 1415 void *digest, int digest_size, enum drbd_packet cmd) 1416 { 1417 struct drbd_socket *sock; 1418 struct p_block_req *p; 1419 1420 /* FIXME: Put the digest into the preallocated socket buffer. */ 1421 1422 sock = &peer_device->connection->data; 1423 p = drbd_prepare_command(peer_device, sock); 1424 if (!p) 1425 return -EIO; 1426 p->sector = cpu_to_be64(sector); 1427 p->block_id = ID_SYNCER /* unused */; 1428 p->blksize = cpu_to_be32(size); 1429 return drbd_send_command(peer_device, sock, cmd, sizeof(*p), digest, digest_size); 1430 } 1431 1432 int drbd_send_ov_request(struct drbd_peer_device *peer_device, sector_t sector, int size) 1433 { 1434 struct drbd_socket *sock; 1435 struct p_block_req *p; 1436 1437 sock = &peer_device->connection->data; 1438 p = drbd_prepare_command(peer_device, sock); 1439 if (!p) 1440 return -EIO; 1441 p->sector = cpu_to_be64(sector); 1442 p->block_id = ID_SYNCER /* unused */; 1443 p->blksize = cpu_to_be32(size); 1444 return drbd_send_command(peer_device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0); 1445 } 1446 1447 /* called on sndtimeo 1448 * returns false if we should retry, 1449 * true if we think connection is dead 1450 */ 1451 static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock) 1452 { 1453 int drop_it; 1454 /* long elapsed = (long)(jiffies - device->last_received); */ 1455 1456 drop_it = connection->meta.socket == sock 1457 || !connection->ack_receiver.task 1458 || get_t_state(&connection->ack_receiver) != RUNNING 1459 || connection->cstate < C_WF_REPORT_PARAMS; 1460 1461 if (drop_it) 1462 return true; 1463 1464 drop_it = !--connection->ko_count; 1465 if (!drop_it) { 1466 drbd_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n", 1467 current->comm, current->pid, connection->ko_count); 1468 request_ping(connection); 1469 } 1470 1471 return drop_it; /* && (device->state == R_PRIMARY) */; 1472 } 1473 1474 static void drbd_update_congested(struct drbd_connection *connection) 1475 { 1476 struct sock *sk = connection->data.socket->sk; 1477 if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5) 1478 set_bit(NET_CONGESTED, &connection->flags); 1479 } 1480 1481 /* The idea of sendpage seems to be to put some kind of reference 1482 * to the page into the skb, and to hand it over to the NIC. In 1483 * this process get_page() gets called. 1484 * 1485 * As soon as the page was really sent over the network put_page() 1486 * gets called by some part of the network layer. [ NIC driver? ] 1487 * 1488 * [ get_page() / put_page() increment/decrement the count. If count 1489 * reaches 0 the page will be freed. ] 1490 * 1491 * This works nicely with pages from FSs. 1492 * But this means that in protocol A we might signal IO completion too early! 1493 * 1494 * In order not to corrupt data during a resync we must make sure 1495 * that we do not reuse our own buffer pages (EEs) to early, therefore 1496 * we have the net_ee list. 1497 * 1498 * XFS seems to have problems, still, it submits pages with page_count == 0! 1499 * As a workaround, we disable sendpage on pages 1500 * with page_count == 0 or PageSlab. 1501 */ 1502 static int _drbd_no_send_page(struct drbd_peer_device *peer_device, struct page *page, 1503 int offset, size_t size, unsigned msg_flags) 1504 { 1505 struct socket *socket; 1506 void *addr; 1507 int err; 1508 1509 socket = peer_device->connection->data.socket; 1510 addr = kmap(page) + offset; 1511 err = drbd_send_all(peer_device->connection, socket, addr, size, msg_flags); 1512 kunmap(page); 1513 if (!err) 1514 peer_device->device->send_cnt += size >> 9; 1515 return err; 1516 } 1517 1518 static int _drbd_send_page(struct drbd_peer_device *peer_device, struct page *page, 1519 int offset, size_t size, unsigned msg_flags) 1520 { 1521 struct socket *socket = peer_device->connection->data.socket; 1522 struct msghdr msg = { .msg_flags = msg_flags, }; 1523 struct bio_vec bvec; 1524 int len = size; 1525 int err = -EIO; 1526 1527 /* e.g. XFS meta- & log-data is in slab pages, which have a 1528 * page_count of 0 and/or have PageSlab() set. 1529 * we cannot use send_page for those, as that does get_page(); 1530 * put_page(); and would cause either a VM_BUG directly, or 1531 * __page_cache_release a page that would actually still be referenced 1532 * by someone, leading to some obscure delayed Oops somewhere else. */ 1533 if (!drbd_disable_sendpage && sendpages_ok(page, len, offset)) 1534 msg.msg_flags |= MSG_NOSIGNAL | MSG_SPLICE_PAGES; 1535 1536 drbd_update_congested(peer_device->connection); 1537 do { 1538 int sent; 1539 1540 bvec_set_page(&bvec, page, len, offset); 1541 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1542 1543 sent = sock_sendmsg(socket, &msg); 1544 if (sent <= 0) { 1545 if (sent == -EAGAIN) { 1546 if (we_should_drop_the_connection(peer_device->connection, socket)) 1547 break; 1548 continue; 1549 } 1550 drbd_warn(peer_device->device, "%s: size=%d len=%d sent=%d\n", 1551 __func__, (int)size, len, sent); 1552 if (sent < 0) 1553 err = sent; 1554 break; 1555 } 1556 len -= sent; 1557 offset += sent; 1558 } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/); 1559 clear_bit(NET_CONGESTED, &peer_device->connection->flags); 1560 1561 if (len == 0) { 1562 err = 0; 1563 peer_device->device->send_cnt += size >> 9; 1564 } 1565 return err; 1566 } 1567 1568 static int _drbd_send_bio(struct drbd_peer_device *peer_device, struct bio *bio) 1569 { 1570 struct bio_vec bvec; 1571 struct bvec_iter iter; 1572 1573 /* hint all but last page with MSG_MORE */ 1574 bio_for_each_segment(bvec, bio, iter) { 1575 int err; 1576 1577 err = _drbd_no_send_page(peer_device, bvec.bv_page, 1578 bvec.bv_offset, bvec.bv_len, 1579 bio_iter_last(bvec, iter) 1580 ? 0 : MSG_MORE); 1581 if (err) 1582 return err; 1583 } 1584 return 0; 1585 } 1586 1587 static int _drbd_send_zc_bio(struct drbd_peer_device *peer_device, struct bio *bio) 1588 { 1589 struct bio_vec bvec; 1590 struct bvec_iter iter; 1591 1592 /* hint all but last page with MSG_MORE */ 1593 bio_for_each_segment(bvec, bio, iter) { 1594 int err; 1595 1596 err = _drbd_send_page(peer_device, bvec.bv_page, 1597 bvec.bv_offset, bvec.bv_len, 1598 bio_iter_last(bvec, iter) ? 0 : MSG_MORE); 1599 if (err) 1600 return err; 1601 } 1602 return 0; 1603 } 1604 1605 static int _drbd_send_zc_ee(struct drbd_peer_device *peer_device, 1606 struct drbd_peer_request *peer_req) 1607 { 1608 bool use_sendpage = !(peer_req->flags & EE_RELEASE_TO_MEMPOOL); 1609 struct page *page = peer_req->pages; 1610 unsigned len = peer_req->i.size; 1611 int err; 1612 1613 /* hint all but last page with MSG_MORE */ 1614 page_chain_for_each(page) { 1615 unsigned l = min_t(unsigned, len, PAGE_SIZE); 1616 1617 if (likely(use_sendpage)) 1618 err = _drbd_send_page(peer_device, page, 0, l, 1619 page_chain_next(page) ? MSG_MORE : 0); 1620 else 1621 err = _drbd_no_send_page(peer_device, page, 0, l, 1622 page_chain_next(page) ? MSG_MORE : 0); 1623 1624 if (err) 1625 return err; 1626 len -= l; 1627 } 1628 return 0; 1629 } 1630 1631 static u32 bio_flags_to_wire(struct drbd_connection *connection, 1632 struct bio *bio) 1633 { 1634 if (connection->agreed_pro_version >= 95) 1635 return (bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0) | 1636 (bio->bi_opf & REQ_FUA ? DP_FUA : 0) | 1637 (bio->bi_opf & REQ_PREFLUSH ? DP_FLUSH : 0) | 1638 (bio_op(bio) == REQ_OP_DISCARD ? DP_DISCARD : 0) | 1639 (bio_op(bio) == REQ_OP_WRITE_ZEROES ? 1640 ((connection->agreed_features & DRBD_FF_WZEROES) ? 1641 (DP_ZEROES |(!(bio->bi_opf & REQ_NOUNMAP) ? DP_DISCARD : 0)) 1642 : DP_DISCARD) 1643 : 0); 1644 else 1645 return bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0; 1646 } 1647 1648 /* Used to send write or TRIM aka REQ_OP_DISCARD requests 1649 * R_PRIMARY -> Peer (P_DATA, P_TRIM) 1650 */ 1651 int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *req) 1652 { 1653 struct drbd_device *device = peer_device->device; 1654 struct drbd_socket *sock; 1655 struct p_data *p; 1656 void *digest_out; 1657 unsigned int dp_flags = 0; 1658 int digest_size; 1659 int err; 1660 1661 sock = &peer_device->connection->data; 1662 p = drbd_prepare_command(peer_device, sock); 1663 digest_size = peer_device->connection->integrity_tfm ? 1664 crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0; 1665 1666 if (!p) 1667 return -EIO; 1668 p->sector = cpu_to_be64(req->i.sector); 1669 p->block_id = (unsigned long)req; 1670 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq)); 1671 dp_flags = bio_flags_to_wire(peer_device->connection, req->master_bio); 1672 if (device->state.conn >= C_SYNC_SOURCE && 1673 device->state.conn <= C_PAUSED_SYNC_T) 1674 dp_flags |= DP_MAY_SET_IN_SYNC; 1675 if (peer_device->connection->agreed_pro_version >= 100) { 1676 if (req->rq_state & RQ_EXP_RECEIVE_ACK) 1677 dp_flags |= DP_SEND_RECEIVE_ACK; 1678 /* During resync, request an explicit write ack, 1679 * even in protocol != C */ 1680 if (req->rq_state & RQ_EXP_WRITE_ACK 1681 || (dp_flags & DP_MAY_SET_IN_SYNC)) 1682 dp_flags |= DP_SEND_WRITE_ACK; 1683 } 1684 p->dp_flags = cpu_to_be32(dp_flags); 1685 1686 if (dp_flags & (DP_DISCARD|DP_ZEROES)) { 1687 enum drbd_packet cmd = (dp_flags & DP_ZEROES) ? P_ZEROES : P_TRIM; 1688 struct p_trim *t = (struct p_trim*)p; 1689 t->size = cpu_to_be32(req->i.size); 1690 err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*t), NULL, 0); 1691 goto out; 1692 } 1693 digest_out = p + 1; 1694 1695 /* our digest is still only over the payload. 1696 * TRIM does not carry any payload. */ 1697 if (digest_size) 1698 drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest_out); 1699 err = __send_command(peer_device->connection, device->vnr, sock, P_DATA, 1700 sizeof(*p) + digest_size, NULL, req->i.size); 1701 if (!err) { 1702 /* For protocol A, we have to memcpy the payload into 1703 * socket buffers, as we may complete right away 1704 * as soon as we handed it over to tcp, at which point the data 1705 * pages may become invalid. 1706 * 1707 * For data-integrity enabled, we copy it as well, so we can be 1708 * sure that even if the bio pages may still be modified, it 1709 * won't change the data on the wire, thus if the digest checks 1710 * out ok after sending on this side, but does not fit on the 1711 * receiving side, we sure have detected corruption elsewhere. 1712 */ 1713 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || digest_size) 1714 err = _drbd_send_bio(peer_device, req->master_bio); 1715 else 1716 err = _drbd_send_zc_bio(peer_device, req->master_bio); 1717 1718 /* double check digest, sometimes buffers have been modified in flight. */ 1719 if (digest_size > 0 && digest_size <= 64) { 1720 /* 64 byte, 512 bit, is the largest digest size 1721 * currently supported in kernel crypto. */ 1722 unsigned char digest[64]; 1723 drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest); 1724 if (memcmp(p + 1, digest, digest_size)) { 1725 drbd_warn(device, 1726 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n", 1727 (unsigned long long)req->i.sector, req->i.size); 1728 } 1729 } /* else if (digest_size > 64) { 1730 ... Be noisy about digest too large ... 1731 } */ 1732 } 1733 out: 1734 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */ 1735 1736 return err; 1737 } 1738 1739 /* answer packet, used to send data back for read requests: 1740 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY) 1741 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY) 1742 */ 1743 int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd, 1744 struct drbd_peer_request *peer_req) 1745 { 1746 struct drbd_device *device = peer_device->device; 1747 struct drbd_socket *sock; 1748 struct p_data *p; 1749 int err; 1750 int digest_size; 1751 1752 sock = &peer_device->connection->data; 1753 p = drbd_prepare_command(peer_device, sock); 1754 1755 digest_size = peer_device->connection->integrity_tfm ? 1756 crypto_shash_digestsize(peer_device->connection->integrity_tfm) : 0; 1757 1758 if (!p) 1759 return -EIO; 1760 p->sector = cpu_to_be64(peer_req->i.sector); 1761 p->block_id = peer_req->block_id; 1762 p->seq_num = 0; /* unused */ 1763 p->dp_flags = 0; 1764 if (digest_size) 1765 drbd_csum_ee(peer_device->connection->integrity_tfm, peer_req, p + 1); 1766 err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*p) + digest_size, NULL, peer_req->i.size); 1767 if (!err) 1768 err = _drbd_send_zc_ee(peer_device, peer_req); 1769 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */ 1770 1771 return err; 1772 } 1773 1774 int drbd_send_out_of_sync(struct drbd_peer_device *peer_device, struct drbd_request *req) 1775 { 1776 struct drbd_socket *sock; 1777 struct p_block_desc *p; 1778 1779 sock = &peer_device->connection->data; 1780 p = drbd_prepare_command(peer_device, sock); 1781 if (!p) 1782 return -EIO; 1783 p->sector = cpu_to_be64(req->i.sector); 1784 p->blksize = cpu_to_be32(req->i.size); 1785 return drbd_send_command(peer_device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0); 1786 } 1787 1788 /* 1789 drbd_send distinguishes two cases: 1790 1791 Packets sent via the data socket "sock" 1792 and packets sent via the meta data socket "msock" 1793 1794 sock msock 1795 -----------------+-------------------------+------------------------------ 1796 timeout conf.timeout / 2 conf.timeout / 2 1797 timeout action send a ping via msock Abort communication 1798 and close all sockets 1799 */ 1800 1801 /* 1802 * you must have down()ed the appropriate [m]sock_mutex elsewhere! 1803 */ 1804 int drbd_send(struct drbd_connection *connection, struct socket *sock, 1805 void *buf, size_t size, unsigned msg_flags) 1806 { 1807 struct kvec iov = {.iov_base = buf, .iov_len = size}; 1808 struct msghdr msg = {.msg_flags = msg_flags | MSG_NOSIGNAL}; 1809 int rv, sent = 0; 1810 1811 if (!sock) 1812 return -EBADR; 1813 1814 /* THINK if (signal_pending) return ... ? */ 1815 1816 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, size); 1817 1818 if (sock == connection->data.socket) { 1819 rcu_read_lock(); 1820 connection->ko_count = rcu_dereference(connection->net_conf)->ko_count; 1821 rcu_read_unlock(); 1822 drbd_update_congested(connection); 1823 } 1824 do { 1825 rv = sock_sendmsg(sock, &msg); 1826 if (rv == -EAGAIN) { 1827 if (we_should_drop_the_connection(connection, sock)) 1828 break; 1829 else 1830 continue; 1831 } 1832 if (rv == -EINTR) { 1833 flush_signals(current); 1834 rv = 0; 1835 } 1836 if (rv < 0) 1837 break; 1838 sent += rv; 1839 } while (sent < size); 1840 1841 if (sock == connection->data.socket) 1842 clear_bit(NET_CONGESTED, &connection->flags); 1843 1844 if (rv <= 0) { 1845 if (rv != -EAGAIN) { 1846 drbd_err(connection, "%s_sendmsg returned %d\n", 1847 sock == connection->meta.socket ? "msock" : "sock", 1848 rv); 1849 conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD); 1850 } else 1851 conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD); 1852 } 1853 1854 return sent; 1855 } 1856 1857 /* 1858 * drbd_send_all - Send an entire buffer 1859 * 1860 * Returns 0 upon success and a negative error value otherwise. 1861 */ 1862 int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer, 1863 size_t size, unsigned msg_flags) 1864 { 1865 int err; 1866 1867 err = drbd_send(connection, sock, buffer, size, msg_flags); 1868 if (err < 0) 1869 return err; 1870 if (err != size) 1871 return -EIO; 1872 return 0; 1873 } 1874 1875 static int drbd_open(struct gendisk *disk, blk_mode_t mode) 1876 { 1877 struct drbd_device *device = disk->private_data; 1878 unsigned long flags; 1879 int rv = 0; 1880 1881 mutex_lock(&drbd_main_mutex); 1882 spin_lock_irqsave(&device->resource->req_lock, flags); 1883 /* to have a stable device->state.role 1884 * and no race with updating open_cnt */ 1885 1886 if (device->state.role != R_PRIMARY) { 1887 if (mode & BLK_OPEN_WRITE) 1888 rv = -EROFS; 1889 else if (!drbd_allow_oos) 1890 rv = -EMEDIUMTYPE; 1891 } 1892 1893 if (!rv) 1894 device->open_cnt++; 1895 spin_unlock_irqrestore(&device->resource->req_lock, flags); 1896 mutex_unlock(&drbd_main_mutex); 1897 1898 return rv; 1899 } 1900 1901 static void drbd_release(struct gendisk *gd) 1902 { 1903 struct drbd_device *device = gd->private_data; 1904 1905 mutex_lock(&drbd_main_mutex); 1906 device->open_cnt--; 1907 mutex_unlock(&drbd_main_mutex); 1908 } 1909 1910 /* need to hold resource->req_lock */ 1911 void drbd_queue_unplug(struct drbd_device *device) 1912 { 1913 if (device->state.pdsk >= D_INCONSISTENT && device->state.conn >= C_CONNECTED) { 1914 D_ASSERT(device, device->state.role == R_PRIMARY); 1915 if (test_and_clear_bit(UNPLUG_REMOTE, &device->flags)) { 1916 drbd_queue_work_if_unqueued( 1917 &first_peer_device(device)->connection->sender_work, 1918 &device->unplug_work); 1919 } 1920 } 1921 } 1922 1923 static void drbd_set_defaults(struct drbd_device *device) 1924 { 1925 /* Beware! The actual layout differs 1926 * between big endian and little endian */ 1927 device->state = (union drbd_dev_state) { 1928 { .role = R_SECONDARY, 1929 .peer = R_UNKNOWN, 1930 .conn = C_STANDALONE, 1931 .disk = D_DISKLESS, 1932 .pdsk = D_UNKNOWN, 1933 } }; 1934 } 1935 1936 void drbd_init_set_defaults(struct drbd_device *device) 1937 { 1938 /* the memset(,0,) did most of this. 1939 * note: only assignments, no allocation in here */ 1940 1941 drbd_set_defaults(device); 1942 1943 atomic_set(&device->ap_bio_cnt, 0); 1944 atomic_set(&device->ap_actlog_cnt, 0); 1945 atomic_set(&device->ap_pending_cnt, 0); 1946 atomic_set(&device->rs_pending_cnt, 0); 1947 atomic_set(&device->unacked_cnt, 0); 1948 atomic_set(&device->local_cnt, 0); 1949 atomic_set(&device->pp_in_use_by_net, 0); 1950 atomic_set(&device->rs_sect_in, 0); 1951 atomic_set(&device->rs_sect_ev, 0); 1952 atomic_set(&device->ap_in_flight, 0); 1953 atomic_set(&device->md_io.in_use, 0); 1954 1955 mutex_init(&device->own_state_mutex); 1956 device->state_mutex = &device->own_state_mutex; 1957 1958 spin_lock_init(&device->al_lock); 1959 spin_lock_init(&device->peer_seq_lock); 1960 1961 INIT_LIST_HEAD(&device->active_ee); 1962 INIT_LIST_HEAD(&device->sync_ee); 1963 INIT_LIST_HEAD(&device->done_ee); 1964 INIT_LIST_HEAD(&device->read_ee); 1965 INIT_LIST_HEAD(&device->resync_reads); 1966 INIT_LIST_HEAD(&device->resync_work.list); 1967 INIT_LIST_HEAD(&device->unplug_work.list); 1968 INIT_LIST_HEAD(&device->bm_io_work.w.list); 1969 INIT_LIST_HEAD(&device->pending_master_completion[0]); 1970 INIT_LIST_HEAD(&device->pending_master_completion[1]); 1971 INIT_LIST_HEAD(&device->pending_completion[0]); 1972 INIT_LIST_HEAD(&device->pending_completion[1]); 1973 1974 device->resync_work.cb = w_resync_timer; 1975 device->unplug_work.cb = w_send_write_hint; 1976 device->bm_io_work.w.cb = w_bitmap_io; 1977 1978 timer_setup(&device->resync_timer, resync_timer_fn, 0); 1979 timer_setup(&device->md_sync_timer, md_sync_timer_fn, 0); 1980 timer_setup(&device->start_resync_timer, start_resync_timer_fn, 0); 1981 timer_setup(&device->request_timer, request_timer_fn, 0); 1982 1983 init_waitqueue_head(&device->misc_wait); 1984 init_waitqueue_head(&device->state_wait); 1985 init_waitqueue_head(&device->ee_wait); 1986 init_waitqueue_head(&device->al_wait); 1987 init_waitqueue_head(&device->seq_wait); 1988 1989 device->resync_wenr = LC_FREE; 1990 device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; 1991 device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE; 1992 } 1993 1994 void drbd_set_my_capacity(struct drbd_device *device, sector_t size) 1995 { 1996 char ppb[10]; 1997 1998 set_capacity_and_notify(device->vdisk, size); 1999 2000 drbd_info(device, "size = %s (%llu KB)\n", 2001 ppsize(ppb, size>>1), (unsigned long long)size>>1); 2002 } 2003 2004 void drbd_device_cleanup(struct drbd_device *device) 2005 { 2006 int i; 2007 if (first_peer_device(device)->connection->receiver.t_state != NONE) 2008 drbd_err(device, "ASSERT FAILED: receiver t_state == %d expected 0.\n", 2009 first_peer_device(device)->connection->receiver.t_state); 2010 2011 device->al_writ_cnt = 2012 device->bm_writ_cnt = 2013 device->read_cnt = 2014 device->recv_cnt = 2015 device->send_cnt = 2016 device->writ_cnt = 2017 device->p_size = 2018 device->rs_start = 2019 device->rs_total = 2020 device->rs_failed = 0; 2021 device->rs_last_events = 0; 2022 device->rs_last_sect_ev = 0; 2023 for (i = 0; i < DRBD_SYNC_MARKS; i++) { 2024 device->rs_mark_left[i] = 0; 2025 device->rs_mark_time[i] = 0; 2026 } 2027 D_ASSERT(device, first_peer_device(device)->connection->net_conf == NULL); 2028 2029 set_capacity_and_notify(device->vdisk, 0); 2030 if (device->bitmap) { 2031 /* maybe never allocated. */ 2032 drbd_bm_resize(device, 0, 1); 2033 drbd_bm_cleanup(device); 2034 } 2035 2036 drbd_backing_dev_free(device, device->ldev); 2037 device->ldev = NULL; 2038 2039 clear_bit(AL_SUSPENDED, &device->flags); 2040 2041 D_ASSERT(device, list_empty(&device->active_ee)); 2042 D_ASSERT(device, list_empty(&device->sync_ee)); 2043 D_ASSERT(device, list_empty(&device->done_ee)); 2044 D_ASSERT(device, list_empty(&device->read_ee)); 2045 D_ASSERT(device, list_empty(&device->resync_reads)); 2046 D_ASSERT(device, list_empty(&first_peer_device(device)->connection->sender_work.q)); 2047 D_ASSERT(device, list_empty(&device->resync_work.list)); 2048 D_ASSERT(device, list_empty(&device->unplug_work.list)); 2049 2050 drbd_set_defaults(device); 2051 } 2052 2053 2054 static void drbd_destroy_mempools(void) 2055 { 2056 /* D_ASSERT(device, atomic_read(&drbd_pp_vacant)==0); */ 2057 2058 bioset_exit(&drbd_io_bio_set); 2059 bioset_exit(&drbd_md_io_bio_set); 2060 mempool_exit(&drbd_buffer_page_pool); 2061 mempool_exit(&drbd_md_io_page_pool); 2062 mempool_exit(&drbd_ee_mempool); 2063 mempool_exit(&drbd_request_mempool); 2064 kmem_cache_destroy(drbd_ee_cache); 2065 kmem_cache_destroy(drbd_request_cache); 2066 kmem_cache_destroy(drbd_bm_ext_cache); 2067 kmem_cache_destroy(drbd_al_ext_cache); 2068 2069 drbd_ee_cache = NULL; 2070 drbd_request_cache = NULL; 2071 drbd_bm_ext_cache = NULL; 2072 drbd_al_ext_cache = NULL; 2073 2074 return; 2075 } 2076 2077 static int drbd_create_mempools(void) 2078 { 2079 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * drbd_minor_count; 2080 int ret; 2081 2082 /* caches */ 2083 drbd_request_cache = kmem_cache_create( 2084 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL); 2085 if (drbd_request_cache == NULL) 2086 goto Enomem; 2087 2088 drbd_ee_cache = kmem_cache_create( 2089 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL); 2090 if (drbd_ee_cache == NULL) 2091 goto Enomem; 2092 2093 drbd_bm_ext_cache = kmem_cache_create( 2094 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL); 2095 if (drbd_bm_ext_cache == NULL) 2096 goto Enomem; 2097 2098 drbd_al_ext_cache = kmem_cache_create( 2099 "drbd_al", sizeof(struct lc_element), 0, 0, NULL); 2100 if (drbd_al_ext_cache == NULL) 2101 goto Enomem; 2102 2103 /* mempools */ 2104 ret = bioset_init(&drbd_io_bio_set, BIO_POOL_SIZE, 0, 0); 2105 if (ret) 2106 goto Enomem; 2107 2108 ret = bioset_init(&drbd_md_io_bio_set, DRBD_MIN_POOL_PAGES, 0, 2109 BIOSET_NEED_BVECS); 2110 if (ret) 2111 goto Enomem; 2112 2113 ret = mempool_init_page_pool(&drbd_md_io_page_pool, DRBD_MIN_POOL_PAGES, 0); 2114 if (ret) 2115 goto Enomem; 2116 2117 ret = mempool_init_page_pool(&drbd_buffer_page_pool, number, 0); 2118 if (ret) 2119 goto Enomem; 2120 2121 ret = mempool_init_slab_pool(&drbd_request_mempool, number, 2122 drbd_request_cache); 2123 if (ret) 2124 goto Enomem; 2125 2126 ret = mempool_init_slab_pool(&drbd_ee_mempool, number, drbd_ee_cache); 2127 if (ret) 2128 goto Enomem; 2129 2130 return 0; 2131 2132 Enomem: 2133 drbd_destroy_mempools(); /* in case we allocated some */ 2134 return -ENOMEM; 2135 } 2136 2137 static void drbd_release_all_peer_reqs(struct drbd_device *device) 2138 { 2139 int rr; 2140 2141 rr = drbd_free_peer_reqs(device, &device->active_ee); 2142 if (rr) 2143 drbd_err(device, "%d EEs in active list found!\n", rr); 2144 2145 rr = drbd_free_peer_reqs(device, &device->sync_ee); 2146 if (rr) 2147 drbd_err(device, "%d EEs in sync list found!\n", rr); 2148 2149 rr = drbd_free_peer_reqs(device, &device->read_ee); 2150 if (rr) 2151 drbd_err(device, "%d EEs in read list found!\n", rr); 2152 2153 rr = drbd_free_peer_reqs(device, &device->done_ee); 2154 if (rr) 2155 drbd_err(device, "%d EEs in done list found!\n", rr); 2156 } 2157 2158 /* caution. no locking. */ 2159 void drbd_destroy_device(struct kref *kref) 2160 { 2161 struct drbd_device *device = container_of(kref, struct drbd_device, kref); 2162 struct drbd_resource *resource = device->resource; 2163 struct drbd_peer_device *peer_device, *tmp_peer_device; 2164 2165 timer_shutdown_sync(&device->request_timer); 2166 timer_shutdown_sync(&device->start_resync_timer); 2167 2168 /* paranoia asserts */ 2169 D_ASSERT(device, device->open_cnt == 0); 2170 /* end paranoia asserts */ 2171 2172 /* cleanup stuff that may have been allocated during 2173 * device (re-)configuration or state changes */ 2174 2175 drbd_backing_dev_free(device, device->ldev); 2176 device->ldev = NULL; 2177 2178 drbd_release_all_peer_reqs(device); 2179 2180 lc_destroy(device->act_log); 2181 lc_destroy(device->resync); 2182 2183 kfree(device->p_uuid); 2184 /* device->p_uuid = NULL; */ 2185 2186 if (device->bitmap) /* should no longer be there. */ 2187 drbd_bm_cleanup(device); 2188 __free_page(device->md_io.page); 2189 put_disk(device->vdisk); 2190 kfree(device->rs_plan_s); 2191 2192 /* not for_each_connection(connection, resource): 2193 * those may have been cleaned up and disassociated already. 2194 */ 2195 for_each_peer_device_safe(peer_device, tmp_peer_device, device) { 2196 kref_put(&peer_device->connection->kref, drbd_destroy_connection); 2197 kfree(peer_device); 2198 } 2199 if (device->submit.wq) 2200 destroy_workqueue(device->submit.wq); 2201 kfree(device); 2202 kref_put(&resource->kref, drbd_destroy_resource); 2203 } 2204 2205 /* One global retry thread, if we need to push back some bio and have it 2206 * reinserted through our make request function. 2207 */ 2208 static struct retry_worker { 2209 struct workqueue_struct *wq; 2210 struct work_struct worker; 2211 2212 spinlock_t lock; 2213 struct list_head writes; 2214 } retry; 2215 2216 static void do_retry(struct work_struct *ws) 2217 { 2218 struct retry_worker *retry = container_of(ws, struct retry_worker, worker); 2219 LIST_HEAD(writes); 2220 struct drbd_request *req, *tmp; 2221 2222 spin_lock_irq(&retry->lock); 2223 list_splice_init(&retry->writes, &writes); 2224 spin_unlock_irq(&retry->lock); 2225 2226 list_for_each_entry_safe(req, tmp, &writes, tl_requests) { 2227 struct drbd_device *device = req->device; 2228 struct bio *bio = req->master_bio; 2229 bool expected; 2230 2231 expected = 2232 expect(device, atomic_read(&req->completion_ref) == 0) && 2233 expect(device, req->rq_state & RQ_POSTPONED) && 2234 expect(device, (req->rq_state & RQ_LOCAL_PENDING) == 0 || 2235 (req->rq_state & RQ_LOCAL_ABORTED) != 0); 2236 2237 if (!expected) 2238 drbd_err(device, "req=%p completion_ref=%d rq_state=%x\n", 2239 req, atomic_read(&req->completion_ref), 2240 req->rq_state); 2241 2242 /* We still need to put one kref associated with the 2243 * "completion_ref" going zero in the code path that queued it 2244 * here. The request object may still be referenced by a 2245 * frozen local req->private_bio, in case we force-detached. 2246 */ 2247 kref_put(&req->kref, drbd_req_destroy); 2248 2249 /* A single suspended or otherwise blocking device may stall 2250 * all others as well. Fortunately, this code path is to 2251 * recover from a situation that "should not happen": 2252 * concurrent writes in multi-primary setup. 2253 * In a "normal" lifecycle, this workqueue is supposed to be 2254 * destroyed without ever doing anything. 2255 * If it turns out to be an issue anyways, we can do per 2256 * resource (replication group) or per device (minor) retry 2257 * workqueues instead. 2258 */ 2259 2260 /* We are not just doing submit_bio_noacct(), 2261 * as we want to keep the start_time information. */ 2262 inc_ap_bio(device); 2263 __drbd_make_request(device, bio); 2264 } 2265 } 2266 2267 /* called via drbd_req_put_completion_ref(), 2268 * holds resource->req_lock */ 2269 void drbd_restart_request(struct drbd_request *req) 2270 { 2271 unsigned long flags; 2272 spin_lock_irqsave(&retry.lock, flags); 2273 list_move_tail(&req->tl_requests, &retry.writes); 2274 spin_unlock_irqrestore(&retry.lock, flags); 2275 2276 /* Drop the extra reference that would otherwise 2277 * have been dropped by complete_master_bio. 2278 * do_retry() needs to grab a new one. */ 2279 dec_ap_bio(req->device); 2280 2281 queue_work(retry.wq, &retry.worker); 2282 } 2283 2284 void drbd_destroy_resource(struct kref *kref) 2285 { 2286 struct drbd_resource *resource = 2287 container_of(kref, struct drbd_resource, kref); 2288 2289 idr_destroy(&resource->devices); 2290 free_cpumask_var(resource->cpu_mask); 2291 kfree(resource->name); 2292 kfree(resource); 2293 } 2294 2295 void drbd_free_resource(struct drbd_resource *resource) 2296 { 2297 struct drbd_connection *connection, *tmp; 2298 2299 for_each_connection_safe(connection, tmp, resource) { 2300 list_del(&connection->connections); 2301 drbd_debugfs_connection_cleanup(connection); 2302 kref_put(&connection->kref, drbd_destroy_connection); 2303 } 2304 drbd_debugfs_resource_cleanup(resource); 2305 kref_put(&resource->kref, drbd_destroy_resource); 2306 } 2307 2308 static void drbd_cleanup(void) 2309 { 2310 unsigned int i; 2311 struct drbd_device *device; 2312 struct drbd_resource *resource, *tmp; 2313 2314 /* first remove proc, 2315 * drbdsetup uses it's presence to detect 2316 * whether DRBD is loaded. 2317 * If we would get stuck in proc removal, 2318 * but have netlink already deregistered, 2319 * some drbdsetup commands may wait forever 2320 * for an answer. 2321 */ 2322 if (drbd_proc) 2323 remove_proc_entry("drbd", NULL); 2324 2325 if (retry.wq) 2326 destroy_workqueue(retry.wq); 2327 2328 genl_unregister_family(&drbd_nl_family); 2329 2330 idr_for_each_entry(&drbd_devices, device, i) 2331 drbd_delete_device(device); 2332 2333 /* not _rcu since, no other updater anymore. Genl already unregistered */ 2334 for_each_resource_safe(resource, tmp, &drbd_resources) { 2335 list_del(&resource->resources); 2336 drbd_free_resource(resource); 2337 } 2338 2339 drbd_debugfs_cleanup(); 2340 2341 drbd_destroy_mempools(); 2342 unregister_blkdev(DRBD_MAJOR, "drbd"); 2343 2344 idr_destroy(&drbd_devices); 2345 2346 pr_info("module cleanup done.\n"); 2347 } 2348 2349 static void drbd_init_workqueue(struct drbd_work_queue* wq) 2350 { 2351 spin_lock_init(&wq->q_lock); 2352 INIT_LIST_HEAD(&wq->q); 2353 init_waitqueue_head(&wq->q_wait); 2354 } 2355 2356 struct completion_work { 2357 struct drbd_work w; 2358 struct completion done; 2359 }; 2360 2361 static int w_complete(struct drbd_work *w, int cancel) 2362 { 2363 struct completion_work *completion_work = 2364 container_of(w, struct completion_work, w); 2365 2366 complete(&completion_work->done); 2367 return 0; 2368 } 2369 2370 void drbd_flush_workqueue(struct drbd_work_queue *work_queue) 2371 { 2372 struct completion_work completion_work; 2373 2374 completion_work.w.cb = w_complete; 2375 init_completion(&completion_work.done); 2376 drbd_queue_work(work_queue, &completion_work.w); 2377 wait_for_completion(&completion_work.done); 2378 } 2379 2380 struct drbd_resource *drbd_find_resource(const char *name) 2381 { 2382 struct drbd_resource *resource; 2383 2384 if (!name || !name[0]) 2385 return NULL; 2386 2387 rcu_read_lock(); 2388 for_each_resource_rcu(resource, &drbd_resources) { 2389 if (!strcmp(resource->name, name)) { 2390 kref_get(&resource->kref); 2391 goto found; 2392 } 2393 } 2394 resource = NULL; 2395 found: 2396 rcu_read_unlock(); 2397 return resource; 2398 } 2399 2400 struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len, 2401 void *peer_addr, int peer_addr_len) 2402 { 2403 struct drbd_resource *resource; 2404 struct drbd_connection *connection; 2405 2406 rcu_read_lock(); 2407 for_each_resource_rcu(resource, &drbd_resources) { 2408 for_each_connection_rcu(connection, resource) { 2409 if (connection->my_addr_len == my_addr_len && 2410 connection->peer_addr_len == peer_addr_len && 2411 !memcmp(&connection->my_addr, my_addr, my_addr_len) && 2412 !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) { 2413 kref_get(&connection->kref); 2414 goto found; 2415 } 2416 } 2417 } 2418 connection = NULL; 2419 found: 2420 rcu_read_unlock(); 2421 return connection; 2422 } 2423 2424 static int drbd_alloc_socket(struct drbd_socket *socket) 2425 { 2426 socket->rbuf = (void *) __get_free_page(GFP_KERNEL); 2427 if (!socket->rbuf) 2428 return -ENOMEM; 2429 socket->sbuf = (void *) __get_free_page(GFP_KERNEL); 2430 if (!socket->sbuf) 2431 return -ENOMEM; 2432 return 0; 2433 } 2434 2435 static void drbd_free_socket(struct drbd_socket *socket) 2436 { 2437 free_page((unsigned long) socket->sbuf); 2438 free_page((unsigned long) socket->rbuf); 2439 } 2440 2441 void conn_free_crypto(struct drbd_connection *connection) 2442 { 2443 drbd_free_sock(connection); 2444 2445 crypto_free_shash(connection->csums_tfm); 2446 crypto_free_shash(connection->verify_tfm); 2447 crypto_free_shash(connection->cram_hmac_tfm); 2448 crypto_free_shash(connection->integrity_tfm); 2449 crypto_free_shash(connection->peer_integrity_tfm); 2450 kfree(connection->int_dig_in); 2451 kfree(connection->int_dig_vv); 2452 2453 connection->csums_tfm = NULL; 2454 connection->verify_tfm = NULL; 2455 connection->cram_hmac_tfm = NULL; 2456 connection->integrity_tfm = NULL; 2457 connection->peer_integrity_tfm = NULL; 2458 connection->int_dig_in = NULL; 2459 connection->int_dig_vv = NULL; 2460 } 2461 2462 int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts) 2463 { 2464 struct drbd_connection *connection; 2465 cpumask_var_t new_cpu_mask; 2466 int err; 2467 2468 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) 2469 return -ENOMEM; 2470 2471 /* silently ignore cpu mask on UP kernel */ 2472 if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) { 2473 err = bitmap_parse(res_opts->cpu_mask, DRBD_CPU_MASK_SIZE, 2474 cpumask_bits(new_cpu_mask), nr_cpu_ids); 2475 if (err == -EOVERFLOW) { 2476 /* So what. mask it out. */ 2477 cpumask_var_t tmp_cpu_mask; 2478 if (zalloc_cpumask_var(&tmp_cpu_mask, GFP_KERNEL)) { 2479 cpumask_setall(tmp_cpu_mask); 2480 cpumask_and(new_cpu_mask, new_cpu_mask, tmp_cpu_mask); 2481 drbd_warn(resource, "Overflow in bitmap_parse(%.12s%s), truncating to %u bits\n", 2482 res_opts->cpu_mask, 2483 strlen(res_opts->cpu_mask) > 12 ? "..." : "", 2484 nr_cpu_ids); 2485 free_cpumask_var(tmp_cpu_mask); 2486 err = 0; 2487 } 2488 } 2489 if (err) { 2490 drbd_warn(resource, "bitmap_parse() failed with %d\n", err); 2491 /* retcode = ERR_CPU_MASK_PARSE; */ 2492 goto fail; 2493 } 2494 } 2495 resource->res_opts = *res_opts; 2496 if (cpumask_empty(new_cpu_mask)) 2497 drbd_calc_cpu_mask(&new_cpu_mask); 2498 if (!cpumask_equal(resource->cpu_mask, new_cpu_mask)) { 2499 cpumask_copy(resource->cpu_mask, new_cpu_mask); 2500 for_each_connection_rcu(connection, resource) { 2501 connection->receiver.reset_cpu_mask = 1; 2502 connection->ack_receiver.reset_cpu_mask = 1; 2503 connection->worker.reset_cpu_mask = 1; 2504 } 2505 } 2506 err = 0; 2507 2508 fail: 2509 free_cpumask_var(new_cpu_mask); 2510 return err; 2511 2512 } 2513 2514 struct drbd_resource *drbd_create_resource(const char *name) 2515 { 2516 struct drbd_resource *resource; 2517 2518 resource = kzalloc_obj(struct drbd_resource); 2519 if (!resource) 2520 goto fail; 2521 resource->name = kstrdup(name, GFP_KERNEL); 2522 if (!resource->name) 2523 goto fail_free_resource; 2524 if (!zalloc_cpumask_var(&resource->cpu_mask, GFP_KERNEL)) 2525 goto fail_free_name; 2526 kref_init(&resource->kref); 2527 idr_init(&resource->devices); 2528 INIT_LIST_HEAD(&resource->connections); 2529 resource->write_ordering = WO_BDEV_FLUSH; 2530 list_add_tail_rcu(&resource->resources, &drbd_resources); 2531 mutex_init(&resource->conf_update); 2532 mutex_init(&resource->adm_mutex); 2533 spin_lock_init(&resource->req_lock); 2534 drbd_debugfs_resource_add(resource); 2535 return resource; 2536 2537 fail_free_name: 2538 kfree(resource->name); 2539 fail_free_resource: 2540 kfree(resource); 2541 fail: 2542 return NULL; 2543 } 2544 2545 /* caller must be under adm_mutex */ 2546 struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts) 2547 { 2548 struct drbd_resource *resource; 2549 struct drbd_connection *connection; 2550 2551 connection = kzalloc_obj(struct drbd_connection); 2552 if (!connection) 2553 return NULL; 2554 2555 if (drbd_alloc_socket(&connection->data)) 2556 goto fail; 2557 if (drbd_alloc_socket(&connection->meta)) 2558 goto fail; 2559 2560 connection->current_epoch = kzalloc_obj(struct drbd_epoch); 2561 if (!connection->current_epoch) 2562 goto fail; 2563 2564 INIT_LIST_HEAD(&connection->transfer_log); 2565 2566 INIT_LIST_HEAD(&connection->current_epoch->list); 2567 connection->epochs = 1; 2568 spin_lock_init(&connection->epoch_lock); 2569 2570 connection->send.seen_any_write_yet = false; 2571 connection->send.current_epoch_nr = 0; 2572 connection->send.current_epoch_writes = 0; 2573 2574 resource = drbd_create_resource(name); 2575 if (!resource) 2576 goto fail; 2577 2578 connection->cstate = C_STANDALONE; 2579 mutex_init(&connection->cstate_mutex); 2580 init_waitqueue_head(&connection->ping_wait); 2581 idr_init(&connection->peer_devices); 2582 2583 drbd_init_workqueue(&connection->sender_work); 2584 mutex_init(&connection->data.mutex); 2585 mutex_init(&connection->meta.mutex); 2586 2587 drbd_thread_init(resource, &connection->receiver, drbd_receiver, "receiver"); 2588 connection->receiver.connection = connection; 2589 drbd_thread_init(resource, &connection->worker, drbd_worker, "worker"); 2590 connection->worker.connection = connection; 2591 drbd_thread_init(resource, &connection->ack_receiver, drbd_ack_receiver, "ack_recv"); 2592 connection->ack_receiver.connection = connection; 2593 2594 kref_init(&connection->kref); 2595 2596 connection->resource = resource; 2597 2598 if (set_resource_options(resource, res_opts)) 2599 goto fail_resource; 2600 2601 kref_get(&resource->kref); 2602 list_add_tail_rcu(&connection->connections, &resource->connections); 2603 drbd_debugfs_connection_add(connection); 2604 return connection; 2605 2606 fail_resource: 2607 list_del(&resource->resources); 2608 drbd_free_resource(resource); 2609 fail: 2610 kfree(connection->current_epoch); 2611 drbd_free_socket(&connection->meta); 2612 drbd_free_socket(&connection->data); 2613 kfree(connection); 2614 return NULL; 2615 } 2616 2617 void drbd_destroy_connection(struct kref *kref) 2618 { 2619 struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref); 2620 struct drbd_resource *resource = connection->resource; 2621 2622 if (atomic_read(&connection->current_epoch->epoch_size) != 0) 2623 drbd_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size)); 2624 kfree(connection->current_epoch); 2625 2626 idr_destroy(&connection->peer_devices); 2627 2628 drbd_free_socket(&connection->meta); 2629 drbd_free_socket(&connection->data); 2630 kfree(connection->int_dig_in); 2631 kfree(connection->int_dig_vv); 2632 kfree(connection); 2633 kref_put(&resource->kref, drbd_destroy_resource); 2634 } 2635 2636 static int init_submitter(struct drbd_device *device) 2637 { 2638 /* opencoded create_singlethread_workqueue(), 2639 * to be able to say "drbd%d", ..., minor */ 2640 device->submit.wq = 2641 alloc_ordered_workqueue("drbd%u_submit", WQ_MEM_RECLAIM, device->minor); 2642 if (!device->submit.wq) 2643 return -ENOMEM; 2644 2645 INIT_WORK(&device->submit.worker, do_submit); 2646 INIT_LIST_HEAD(&device->submit.writes); 2647 return 0; 2648 } 2649 2650 enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor) 2651 { 2652 struct drbd_resource *resource = adm_ctx->resource; 2653 struct drbd_connection *connection, *n; 2654 struct drbd_device *device; 2655 struct drbd_peer_device *peer_device, *tmp_peer_device; 2656 struct gendisk *disk; 2657 int id; 2658 int vnr = adm_ctx->volume; 2659 enum drbd_ret_code err = ERR_NOMEM; 2660 struct queue_limits lim = { 2661 /* 2662 * Setting the max_hw_sectors to an odd value of 8kibyte here. 2663 * This triggers a max_bio_size message upon first attach or 2664 * connect. 2665 */ 2666 .max_hw_sectors = DRBD_MAX_BIO_SIZE_SAFE >> 8, 2667 }; 2668 2669 device = minor_to_device(minor); 2670 if (device) 2671 return ERR_MINOR_OR_VOLUME_EXISTS; 2672 2673 /* GFP_KERNEL, we are outside of all write-out paths */ 2674 device = kzalloc_obj(struct drbd_device); 2675 if (!device) 2676 return ERR_NOMEM; 2677 kref_init(&device->kref); 2678 2679 kref_get(&resource->kref); 2680 device->resource = resource; 2681 device->minor = minor; 2682 device->vnr = vnr; 2683 2684 drbd_init_set_defaults(device); 2685 2686 disk = blk_alloc_disk(&lim, NUMA_NO_NODE); 2687 if (IS_ERR(disk)) { 2688 err = PTR_ERR(disk); 2689 goto out_no_disk; 2690 } 2691 2692 device->vdisk = disk; 2693 device->rq_queue = disk->queue; 2694 2695 set_disk_ro(disk, true); 2696 2697 disk->major = DRBD_MAJOR; 2698 disk->first_minor = minor; 2699 disk->minors = 1; 2700 disk->fops = &drbd_ops; 2701 disk->flags |= GENHD_FL_NO_PART; 2702 sprintf(disk->disk_name, "drbd%d", minor); 2703 disk->private_data = device; 2704 2705 device->md_io.page = alloc_page(GFP_KERNEL); 2706 if (!device->md_io.page) 2707 goto out_no_io_page; 2708 2709 if (drbd_bm_init(device)) 2710 goto out_no_bitmap; 2711 device->read_requests = RB_ROOT; 2712 device->write_requests = RB_ROOT; 2713 2714 id = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL); 2715 if (id < 0) { 2716 if (id == -ENOSPC) 2717 err = ERR_MINOR_OR_VOLUME_EXISTS; 2718 goto out_no_minor_idr; 2719 } 2720 kref_get(&device->kref); 2721 2722 id = idr_alloc(&resource->devices, device, vnr, vnr + 1, GFP_KERNEL); 2723 if (id < 0) { 2724 if (id == -ENOSPC) 2725 err = ERR_MINOR_OR_VOLUME_EXISTS; 2726 goto out_idr_remove_minor; 2727 } 2728 kref_get(&device->kref); 2729 2730 INIT_LIST_HEAD(&device->peer_devices); 2731 INIT_LIST_HEAD(&device->pending_bitmap_io); 2732 for_each_connection(connection, resource) { 2733 peer_device = kzalloc_obj(struct drbd_peer_device); 2734 if (!peer_device) 2735 goto out_idr_remove_from_resource; 2736 peer_device->connection = connection; 2737 peer_device->device = device; 2738 2739 list_add(&peer_device->peer_devices, &device->peer_devices); 2740 kref_get(&device->kref); 2741 2742 id = idr_alloc(&connection->peer_devices, peer_device, vnr, vnr + 1, GFP_KERNEL); 2743 if (id < 0) { 2744 if (id == -ENOSPC) 2745 err = ERR_INVALID_REQUEST; 2746 goto out_idr_remove_from_resource; 2747 } 2748 kref_get(&connection->kref); 2749 INIT_WORK(&peer_device->send_acks_work, drbd_send_acks_wf); 2750 } 2751 2752 if (init_submitter(device)) { 2753 err = ERR_NOMEM; 2754 goto out_idr_remove_from_resource; 2755 } 2756 2757 err = add_disk(disk); 2758 if (err) 2759 goto out_destroy_workqueue; 2760 2761 /* inherit the connection state */ 2762 device->state.conn = first_connection(resource)->cstate; 2763 if (device->state.conn == C_WF_REPORT_PARAMS) { 2764 for_each_peer_device(peer_device, device) 2765 drbd_connected(peer_device); 2766 } 2767 /* move to create_peer_device() */ 2768 for_each_peer_device(peer_device, device) 2769 drbd_debugfs_peer_device_add(peer_device); 2770 drbd_debugfs_device_add(device); 2771 return NO_ERROR; 2772 2773 out_destroy_workqueue: 2774 destroy_workqueue(device->submit.wq); 2775 out_idr_remove_from_resource: 2776 for_each_connection_safe(connection, n, resource) { 2777 peer_device = idr_remove(&connection->peer_devices, vnr); 2778 if (peer_device) 2779 kref_put(&connection->kref, drbd_destroy_connection); 2780 } 2781 for_each_peer_device_safe(peer_device, tmp_peer_device, device) { 2782 list_del(&peer_device->peer_devices); 2783 kfree(peer_device); 2784 } 2785 idr_remove(&resource->devices, vnr); 2786 out_idr_remove_minor: 2787 idr_remove(&drbd_devices, minor); 2788 synchronize_rcu(); 2789 out_no_minor_idr: 2790 drbd_bm_cleanup(device); 2791 out_no_bitmap: 2792 __free_page(device->md_io.page); 2793 out_no_io_page: 2794 put_disk(disk); 2795 out_no_disk: 2796 kref_put(&resource->kref, drbd_destroy_resource); 2797 kfree(device); 2798 return err; 2799 } 2800 2801 void drbd_delete_device(struct drbd_device *device) 2802 { 2803 struct drbd_resource *resource = device->resource; 2804 struct drbd_connection *connection; 2805 struct drbd_peer_device *peer_device; 2806 2807 /* move to free_peer_device() */ 2808 for_each_peer_device(peer_device, device) 2809 drbd_debugfs_peer_device_cleanup(peer_device); 2810 drbd_debugfs_device_cleanup(device); 2811 for_each_connection(connection, resource) { 2812 idr_remove(&connection->peer_devices, device->vnr); 2813 kref_put(&device->kref, drbd_destroy_device); 2814 } 2815 idr_remove(&resource->devices, device->vnr); 2816 kref_put(&device->kref, drbd_destroy_device); 2817 idr_remove(&drbd_devices, device_to_minor(device)); 2818 kref_put(&device->kref, drbd_destroy_device); 2819 del_gendisk(device->vdisk); 2820 synchronize_rcu(); 2821 kref_put(&device->kref, drbd_destroy_device); 2822 } 2823 2824 static int __init drbd_init(void) 2825 { 2826 int err; 2827 2828 if (drbd_minor_count < DRBD_MINOR_COUNT_MIN || drbd_minor_count > DRBD_MINOR_COUNT_MAX) { 2829 pr_err("invalid minor_count (%d)\n", drbd_minor_count); 2830 #ifdef MODULE 2831 return -EINVAL; 2832 #else 2833 drbd_minor_count = DRBD_MINOR_COUNT_DEF; 2834 #endif 2835 } 2836 2837 err = register_blkdev(DRBD_MAJOR, "drbd"); 2838 if (err) { 2839 pr_err("unable to register block device major %d\n", 2840 DRBD_MAJOR); 2841 return err; 2842 } 2843 2844 drbd_proc = NULL; /* play safe for drbd_cleanup */ 2845 idr_init(&drbd_devices); 2846 2847 mutex_init(&resources_mutex); 2848 INIT_LIST_HEAD(&drbd_resources); 2849 2850 err = genl_register_family(&drbd_nl_family); 2851 if (err) { 2852 pr_err("unable to register generic netlink family\n"); 2853 goto fail; 2854 } 2855 2856 err = drbd_create_mempools(); 2857 if (err) 2858 goto fail; 2859 2860 err = -ENOMEM; 2861 drbd_proc = proc_create_single("drbd", S_IFREG | 0444 , NULL, drbd_seq_show); 2862 if (!drbd_proc) { 2863 pr_err("unable to register proc file\n"); 2864 goto fail; 2865 } 2866 2867 retry.wq = create_singlethread_workqueue("drbd-reissue"); 2868 if (!retry.wq) { 2869 pr_err("unable to create retry workqueue\n"); 2870 goto fail; 2871 } 2872 INIT_WORK(&retry.worker, do_retry); 2873 spin_lock_init(&retry.lock); 2874 INIT_LIST_HEAD(&retry.writes); 2875 2876 drbd_debugfs_init(); 2877 2878 pr_info("initialized. " 2879 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n", 2880 DRBD_FAMILY_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX); 2881 pr_info("%s\n", drbd_buildtag()); 2882 pr_info("registered as block device major %d\n", DRBD_MAJOR); 2883 return 0; /* Success! */ 2884 2885 fail: 2886 drbd_cleanup(); 2887 if (err == -ENOMEM) 2888 pr_err("ran out of memory\n"); 2889 else 2890 pr_err("initialization failure\n"); 2891 return err; 2892 } 2893 2894 static void drbd_free_one_sock(struct drbd_socket *ds) 2895 { 2896 struct socket *s; 2897 mutex_lock(&ds->mutex); 2898 s = ds->socket; 2899 ds->socket = NULL; 2900 mutex_unlock(&ds->mutex); 2901 if (s) { 2902 /* so debugfs does not need to mutex_lock() */ 2903 synchronize_rcu(); 2904 kernel_sock_shutdown(s, SHUT_RDWR); 2905 sock_release(s); 2906 } 2907 } 2908 2909 void drbd_free_sock(struct drbd_connection *connection) 2910 { 2911 if (connection->data.socket) 2912 drbd_free_one_sock(&connection->data); 2913 if (connection->meta.socket) 2914 drbd_free_one_sock(&connection->meta); 2915 } 2916 2917 /* meta data management */ 2918 2919 void conn_md_sync(struct drbd_connection *connection) 2920 { 2921 struct drbd_peer_device *peer_device; 2922 int vnr; 2923 2924 rcu_read_lock(); 2925 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) { 2926 struct drbd_device *device = peer_device->device; 2927 2928 kref_get(&device->kref); 2929 rcu_read_unlock(); 2930 drbd_md_sync(device); 2931 kref_put(&device->kref, drbd_destroy_device); 2932 rcu_read_lock(); 2933 } 2934 rcu_read_unlock(); 2935 } 2936 2937 /* aligned 4kByte */ 2938 struct meta_data_on_disk { 2939 u64 la_size_sect; /* last agreed size. */ 2940 u64 uuid[UI_SIZE]; /* UUIDs. */ 2941 u64 device_uuid; 2942 u64 reserved_u64_1; 2943 u32 flags; /* MDF */ 2944 u32 magic; 2945 u32 md_size_sect; 2946 u32 al_offset; /* offset to this block */ 2947 u32 al_nr_extents; /* important for restoring the AL (userspace) */ 2948 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */ 2949 u32 bm_offset; /* offset to the bitmap, from here */ 2950 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */ 2951 u32 la_peer_max_bio_size; /* last peer max_bio_size */ 2952 2953 /* see al_tr_number_to_on_disk_sector() */ 2954 u32 al_stripes; 2955 u32 al_stripe_size_4k; 2956 2957 u8 reserved_u8[4096 - (7*8 + 10*4)]; 2958 } __packed; 2959 2960 2961 2962 void drbd_md_write(struct drbd_device *device, void *b) 2963 { 2964 struct meta_data_on_disk *buffer = b; 2965 sector_t sector; 2966 int i; 2967 2968 memset(buffer, 0, sizeof(*buffer)); 2969 2970 buffer->la_size_sect = cpu_to_be64(get_capacity(device->vdisk)); 2971 for (i = UI_CURRENT; i < UI_SIZE; i++) 2972 buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]); 2973 buffer->flags = cpu_to_be32(device->ldev->md.flags); 2974 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN); 2975 2976 buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect); 2977 buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset); 2978 buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements); 2979 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE); 2980 buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid); 2981 2982 buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset); 2983 buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size); 2984 2985 buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes); 2986 buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k); 2987 2988 D_ASSERT(device, drbd_md_ss(device->ldev) == device->ldev->md.md_offset); 2989 sector = device->ldev->md.md_offset; 2990 2991 if (drbd_md_sync_page_io(device, device->ldev, sector, REQ_OP_WRITE)) { 2992 /* this was a try anyways ... */ 2993 drbd_err(device, "meta data update failed!\n"); 2994 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 2995 } 2996 } 2997 2998 /** 2999 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set 3000 * @device: DRBD device. 3001 */ 3002 void drbd_md_sync(struct drbd_device *device) 3003 { 3004 struct meta_data_on_disk *buffer; 3005 3006 /* Don't accidentally change the DRBD meta data layout. */ 3007 BUILD_BUG_ON(UI_SIZE != 4); 3008 BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096); 3009 3010 timer_delete(&device->md_sync_timer); 3011 /* timer may be rearmed by drbd_md_mark_dirty() now. */ 3012 if (!test_and_clear_bit(MD_DIRTY, &device->flags)) 3013 return; 3014 3015 /* We use here D_FAILED and not D_ATTACHING because we try to write 3016 * metadata even if we detach due to a disk failure! */ 3017 if (!get_ldev_if_state(device, D_FAILED)) 3018 return; 3019 3020 buffer = drbd_md_get_buffer(device, __func__); 3021 if (!buffer) 3022 goto out; 3023 3024 drbd_md_write(device, buffer); 3025 3026 /* Update device->ldev->md.la_size_sect, 3027 * since we updated it on metadata. */ 3028 device->ldev->md.la_size_sect = get_capacity(device->vdisk); 3029 3030 drbd_md_put_buffer(device); 3031 out: 3032 put_ldev(device); 3033 } 3034 3035 static int check_activity_log_stripe_size(struct drbd_device *device, 3036 struct meta_data_on_disk *on_disk, 3037 struct drbd_md *in_core) 3038 { 3039 u32 al_stripes = be32_to_cpu(on_disk->al_stripes); 3040 u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k); 3041 u64 al_size_4k; 3042 3043 /* both not set: default to old fixed size activity log */ 3044 if (al_stripes == 0 && al_stripe_size_4k == 0) { 3045 al_stripes = 1; 3046 al_stripe_size_4k = MD_32kB_SECT/8; 3047 } 3048 3049 /* some paranoia plausibility checks */ 3050 3051 /* we need both values to be set */ 3052 if (al_stripes == 0 || al_stripe_size_4k == 0) 3053 goto err; 3054 3055 al_size_4k = (u64)al_stripes * al_stripe_size_4k; 3056 3057 /* Upper limit of activity log area, to avoid potential overflow 3058 * problems in al_tr_number_to_on_disk_sector(). As right now, more 3059 * than 72 * 4k blocks total only increases the amount of history, 3060 * limiting this arbitrarily to 16 GB is not a real limitation ;-) */ 3061 if (al_size_4k > (16 * 1024 * 1024/4)) 3062 goto err; 3063 3064 /* Lower limit: we need at least 8 transaction slots (32kB) 3065 * to not break existing setups */ 3066 if (al_size_4k < MD_32kB_SECT/8) 3067 goto err; 3068 3069 in_core->al_stripe_size_4k = al_stripe_size_4k; 3070 in_core->al_stripes = al_stripes; 3071 in_core->al_size_4k = al_size_4k; 3072 3073 return 0; 3074 err: 3075 drbd_err(device, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n", 3076 al_stripes, al_stripe_size_4k); 3077 return -EINVAL; 3078 } 3079 3080 static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev) 3081 { 3082 sector_t capacity = drbd_get_capacity(bdev->md_bdev); 3083 struct drbd_md *in_core = &bdev->md; 3084 s32 on_disk_al_sect; 3085 s32 on_disk_bm_sect; 3086 3087 /* The on-disk size of the activity log, calculated from offsets, and 3088 * the size of the activity log calculated from the stripe settings, 3089 * should match. 3090 * Though we could relax this a bit: it is ok, if the striped activity log 3091 * fits in the available on-disk activity log size. 3092 * Right now, that would break how resize is implemented. 3093 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware 3094 * of possible unused padding space in the on disk layout. */ 3095 if (in_core->al_offset < 0) { 3096 if (in_core->bm_offset > in_core->al_offset) 3097 goto err; 3098 on_disk_al_sect = -in_core->al_offset; 3099 on_disk_bm_sect = in_core->al_offset - in_core->bm_offset; 3100 } else { 3101 if (in_core->al_offset != MD_4kB_SECT) 3102 goto err; 3103 if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT) 3104 goto err; 3105 3106 on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT; 3107 on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset; 3108 } 3109 3110 /* old fixed size meta data is exactly that: fixed. */ 3111 if (in_core->meta_dev_idx >= 0) { 3112 if (in_core->md_size_sect != MD_128MB_SECT 3113 || in_core->al_offset != MD_4kB_SECT 3114 || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT 3115 || in_core->al_stripes != 1 3116 || in_core->al_stripe_size_4k != MD_32kB_SECT/8) 3117 goto err; 3118 } 3119 3120 if (capacity < in_core->md_size_sect) 3121 goto err; 3122 if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev)) 3123 goto err; 3124 3125 /* should be aligned, and at least 32k */ 3126 if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT)) 3127 goto err; 3128 3129 /* should fit (for now: exactly) into the available on-disk space; 3130 * overflow prevention is in check_activity_log_stripe_size() above. */ 3131 if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT) 3132 goto err; 3133 3134 /* again, should be aligned */ 3135 if (in_core->bm_offset & 7) 3136 goto err; 3137 3138 /* FIXME check for device grow with flex external meta data? */ 3139 3140 /* can the available bitmap space cover the last agreed device size? */ 3141 if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512) 3142 goto err; 3143 3144 return 0; 3145 3146 err: 3147 drbd_err(device, "meta data offsets don't make sense: idx=%d " 3148 "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, " 3149 "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n", 3150 in_core->meta_dev_idx, 3151 in_core->al_stripes, in_core->al_stripe_size_4k, 3152 in_core->al_offset, in_core->bm_offset, in_core->md_size_sect, 3153 (unsigned long long)in_core->la_size_sect, 3154 (unsigned long long)capacity); 3155 3156 return -EINVAL; 3157 } 3158 3159 3160 /** 3161 * drbd_md_read() - Reads in the meta data super block 3162 * @device: DRBD device. 3163 * @bdev: Device from which the meta data should be read in. 3164 * 3165 * Return NO_ERROR on success, and an enum drbd_ret_code in case 3166 * something goes wrong. 3167 * 3168 * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS, 3169 * even before @bdev is assigned to @device->ldev. 3170 */ 3171 int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev) 3172 { 3173 struct meta_data_on_disk *buffer; 3174 u32 magic, flags; 3175 int i, rv = NO_ERROR; 3176 3177 if (device->state.disk != D_DISKLESS) 3178 return ERR_DISK_CONFIGURED; 3179 3180 buffer = drbd_md_get_buffer(device, __func__); 3181 if (!buffer) 3182 return ERR_NOMEM; 3183 3184 /* First, figure out where our meta data superblock is located, 3185 * and read it. */ 3186 bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx; 3187 bdev->md.md_offset = drbd_md_ss(bdev); 3188 /* Even for (flexible or indexed) external meta data, 3189 * initially restrict us to the 4k superblock for now. 3190 * Affects the paranoia out-of-range access check in drbd_md_sync_page_io(). */ 3191 bdev->md.md_size_sect = 8; 3192 3193 if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset, 3194 REQ_OP_READ)) { 3195 /* NOTE: can't do normal error processing here as this is 3196 called BEFORE disk is attached */ 3197 drbd_err(device, "Error while reading metadata.\n"); 3198 rv = ERR_IO_MD_DISK; 3199 goto err; 3200 } 3201 3202 magic = be32_to_cpu(buffer->magic); 3203 flags = be32_to_cpu(buffer->flags); 3204 if (magic == DRBD_MD_MAGIC_84_UNCLEAN || 3205 (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) { 3206 /* btw: that's Activity Log clean, not "all" clean. */ 3207 drbd_err(device, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n"); 3208 rv = ERR_MD_UNCLEAN; 3209 goto err; 3210 } 3211 3212 rv = ERR_MD_INVALID; 3213 if (magic != DRBD_MD_MAGIC_08) { 3214 if (magic == DRBD_MD_MAGIC_07) 3215 drbd_err(device, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n"); 3216 else 3217 drbd_err(device, "Meta data magic not found. Did you \"drbdadm create-md\"?\n"); 3218 goto err; 3219 } 3220 3221 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) { 3222 drbd_err(device, "unexpected bm_bytes_per_bit: %u (expected %u)\n", 3223 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE); 3224 goto err; 3225 } 3226 3227 3228 /* convert to in_core endian */ 3229 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect); 3230 for (i = UI_CURRENT; i < UI_SIZE; i++) 3231 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]); 3232 bdev->md.flags = be32_to_cpu(buffer->flags); 3233 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid); 3234 3235 bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect); 3236 bdev->md.al_offset = be32_to_cpu(buffer->al_offset); 3237 bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset); 3238 3239 if (check_activity_log_stripe_size(device, buffer, &bdev->md)) 3240 goto err; 3241 if (check_offsets_and_sizes(device, bdev)) 3242 goto err; 3243 3244 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) { 3245 drbd_err(device, "unexpected bm_offset: %d (expected %d)\n", 3246 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset); 3247 goto err; 3248 } 3249 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) { 3250 drbd_err(device, "unexpected md_size: %u (expected %u)\n", 3251 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect); 3252 goto err; 3253 } 3254 3255 rv = NO_ERROR; 3256 3257 spin_lock_irq(&device->resource->req_lock); 3258 if (device->state.conn < C_CONNECTED) { 3259 unsigned int peer; 3260 peer = be32_to_cpu(buffer->la_peer_max_bio_size); 3261 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE); 3262 device->peer_max_bio_size = peer; 3263 } 3264 spin_unlock_irq(&device->resource->req_lock); 3265 3266 err: 3267 drbd_md_put_buffer(device); 3268 3269 return rv; 3270 } 3271 3272 /** 3273 * drbd_md_mark_dirty() - Mark meta data super block as dirty 3274 * @device: DRBD device. 3275 * 3276 * Call this function if you change anything that should be written to 3277 * the meta-data super block. This function sets MD_DIRTY, and starts a 3278 * timer that ensures that within five seconds you have to call drbd_md_sync(). 3279 */ 3280 void drbd_md_mark_dirty(struct drbd_device *device) 3281 { 3282 if (!test_and_set_bit(MD_DIRTY, &device->flags)) 3283 mod_timer(&device->md_sync_timer, jiffies + 5*HZ); 3284 } 3285 3286 void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local) 3287 { 3288 int i; 3289 3290 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++) 3291 device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i]; 3292 } 3293 3294 void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) 3295 { 3296 if (idx == UI_CURRENT) { 3297 if (device->state.role == R_PRIMARY) 3298 val |= 1; 3299 else 3300 val &= ~((u64)1); 3301 3302 drbd_set_ed_uuid(device, val); 3303 } 3304 3305 device->ldev->md.uuid[idx] = val; 3306 drbd_md_mark_dirty(device); 3307 } 3308 3309 void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) 3310 { 3311 unsigned long flags; 3312 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); 3313 __drbd_uuid_set(device, idx, val); 3314 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); 3315 } 3316 3317 void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local) 3318 { 3319 unsigned long flags; 3320 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); 3321 if (device->ldev->md.uuid[idx]) { 3322 drbd_uuid_move_history(device); 3323 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx]; 3324 } 3325 __drbd_uuid_set(device, idx, val); 3326 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); 3327 } 3328 3329 /** 3330 * drbd_uuid_new_current() - Creates a new current UUID 3331 * @device: DRBD device. 3332 * 3333 * Creates a new current UUID, and rotates the old current UUID into 3334 * the bitmap slot. Causes an incremental resync upon next connect. 3335 */ 3336 void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local) 3337 { 3338 u64 val; 3339 unsigned long long bm_uuid; 3340 3341 val = get_random_u64(); 3342 3343 spin_lock_irq(&device->ldev->md.uuid_lock); 3344 bm_uuid = device->ldev->md.uuid[UI_BITMAP]; 3345 3346 if (bm_uuid) 3347 drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid); 3348 3349 device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT]; 3350 __drbd_uuid_set(device, UI_CURRENT, val); 3351 spin_unlock_irq(&device->ldev->md.uuid_lock); 3352 3353 drbd_print_uuids(device, "new current UUID"); 3354 /* get it to stable storage _now_ */ 3355 drbd_md_sync(device); 3356 } 3357 3358 void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local) 3359 { 3360 unsigned long flags; 3361 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags); 3362 if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0) { 3363 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); 3364 return; 3365 } 3366 3367 if (val == 0) { 3368 drbd_uuid_move_history(device); 3369 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP]; 3370 device->ldev->md.uuid[UI_BITMAP] = 0; 3371 } else { 3372 unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP]; 3373 if (bm_uuid) 3374 drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid); 3375 3376 device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1); 3377 } 3378 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags); 3379 3380 drbd_md_mark_dirty(device); 3381 } 3382 3383 /** 3384 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() 3385 * @device: DRBD device. 3386 * @peer_device: Peer DRBD device. 3387 * 3388 * Sets all bits in the bitmap and writes the whole bitmap to stable storage. 3389 */ 3390 int drbd_bmio_set_n_write(struct drbd_device *device, 3391 struct drbd_peer_device *peer_device) __must_hold(local) 3392 3393 { 3394 int rv = -EIO; 3395 3396 drbd_md_set_flag(device, MDF_FULL_SYNC); 3397 drbd_md_sync(device); 3398 drbd_bm_set_all(device); 3399 3400 rv = drbd_bm_write(device, peer_device); 3401 3402 if (!rv) { 3403 drbd_md_clear_flag(device, MDF_FULL_SYNC); 3404 drbd_md_sync(device); 3405 } 3406 3407 return rv; 3408 } 3409 3410 /** 3411 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io() 3412 * @device: DRBD device. 3413 * @peer_device: Peer DRBD device. 3414 * 3415 * Clears all bits in the bitmap and writes the whole bitmap to stable storage. 3416 */ 3417 int drbd_bmio_clear_n_write(struct drbd_device *device, 3418 struct drbd_peer_device *peer_device) __must_hold(local) 3419 3420 { 3421 drbd_resume_al(device); 3422 drbd_bm_clear_all(device); 3423 return drbd_bm_write(device, peer_device); 3424 } 3425 3426 static int w_bitmap_io(struct drbd_work *w, int unused) 3427 { 3428 struct drbd_device *device = 3429 container_of(w, struct drbd_device, bm_io_work.w); 3430 struct bm_io_work *work = &device->bm_io_work; 3431 int rv = -EIO; 3432 3433 if (work->flags != BM_LOCKED_CHANGE_ALLOWED) { 3434 int cnt = atomic_read(&device->ap_bio_cnt); 3435 if (cnt) 3436 drbd_err(device, "FIXME: ap_bio_cnt %d, expected 0; queued for '%s'\n", 3437 cnt, work->why); 3438 } 3439 3440 if (get_ldev(device)) { 3441 drbd_bm_lock(device, work->why, work->flags); 3442 rv = work->io_fn(device, work->peer_device); 3443 drbd_bm_unlock(device); 3444 put_ldev(device); 3445 } 3446 3447 clear_bit_unlock(BITMAP_IO, &device->flags); 3448 wake_up(&device->misc_wait); 3449 3450 if (work->done) 3451 work->done(device, rv); 3452 3453 clear_bit(BITMAP_IO_QUEUED, &device->flags); 3454 work->why = NULL; 3455 work->flags = 0; 3456 3457 return 0; 3458 } 3459 3460 /** 3461 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap 3462 * @device: DRBD device. 3463 * @io_fn: IO callback to be called when bitmap IO is possible 3464 * @done: callback to be called after the bitmap IO was performed 3465 * @why: Descriptive text of the reason for doing the IO 3466 * @flags: Bitmap flags 3467 * @peer_device: Peer DRBD device. 3468 * 3469 * While IO on the bitmap happens we freeze application IO thus we ensure 3470 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be 3471 * called from worker context. It MUST NOT be used while a previous such 3472 * work is still pending! 3473 * 3474 * Its worker function encloses the call of io_fn() by get_ldev() and 3475 * put_ldev(). 3476 */ 3477 void drbd_queue_bitmap_io(struct drbd_device *device, 3478 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *), 3479 void (*done)(struct drbd_device *, int), 3480 char *why, enum bm_flag flags, 3481 struct drbd_peer_device *peer_device) 3482 { 3483 D_ASSERT(device, current == peer_device->connection->worker.task); 3484 3485 D_ASSERT(device, !test_bit(BITMAP_IO_QUEUED, &device->flags)); 3486 D_ASSERT(device, !test_bit(BITMAP_IO, &device->flags)); 3487 D_ASSERT(device, list_empty(&device->bm_io_work.w.list)); 3488 if (device->bm_io_work.why) 3489 drbd_err(device, "FIXME going to queue '%s' but '%s' still pending?\n", 3490 why, device->bm_io_work.why); 3491 3492 device->bm_io_work.peer_device = peer_device; 3493 device->bm_io_work.io_fn = io_fn; 3494 device->bm_io_work.done = done; 3495 device->bm_io_work.why = why; 3496 device->bm_io_work.flags = flags; 3497 3498 spin_lock_irq(&device->resource->req_lock); 3499 set_bit(BITMAP_IO, &device->flags); 3500 /* don't wait for pending application IO if the caller indicates that 3501 * application IO does not conflict anyways. */ 3502 if (flags == BM_LOCKED_CHANGE_ALLOWED || atomic_read(&device->ap_bio_cnt) == 0) { 3503 if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags)) 3504 drbd_queue_work(&peer_device->connection->sender_work, 3505 &device->bm_io_work.w); 3506 } 3507 spin_unlock_irq(&device->resource->req_lock); 3508 } 3509 3510 /** 3511 * drbd_bitmap_io() - Does an IO operation on the whole bitmap 3512 * @device: DRBD device. 3513 * @io_fn: IO callback to be called when bitmap IO is possible 3514 * @why: Descriptive text of the reason for doing the IO 3515 * @flags: Bitmap flags 3516 * @peer_device: Peer DRBD device. 3517 * 3518 * freezes application IO while that the actual IO operations runs. This 3519 * functions MAY NOT be called from worker context. 3520 */ 3521 int drbd_bitmap_io(struct drbd_device *device, 3522 int (*io_fn)(struct drbd_device *, struct drbd_peer_device *), 3523 char *why, enum bm_flag flags, 3524 struct drbd_peer_device *peer_device) 3525 { 3526 /* Only suspend io, if some operation is supposed to be locked out */ 3527 const bool do_suspend_io = flags & (BM_DONT_CLEAR|BM_DONT_SET|BM_DONT_TEST); 3528 int rv; 3529 3530 D_ASSERT(device, current != first_peer_device(device)->connection->worker.task); 3531 3532 if (do_suspend_io) 3533 drbd_suspend_io(device); 3534 3535 drbd_bm_lock(device, why, flags); 3536 rv = io_fn(device, peer_device); 3537 drbd_bm_unlock(device); 3538 3539 if (do_suspend_io) 3540 drbd_resume_io(device); 3541 3542 return rv; 3543 } 3544 3545 void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local) 3546 { 3547 if ((device->ldev->md.flags & flag) != flag) { 3548 drbd_md_mark_dirty(device); 3549 device->ldev->md.flags |= flag; 3550 } 3551 } 3552 3553 void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local) 3554 { 3555 if ((device->ldev->md.flags & flag) != 0) { 3556 drbd_md_mark_dirty(device); 3557 device->ldev->md.flags &= ~flag; 3558 } 3559 } 3560 int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag) 3561 { 3562 return (bdev->md.flags & flag) != 0; 3563 } 3564 3565 static void md_sync_timer_fn(struct timer_list *t) 3566 { 3567 struct drbd_device *device = timer_container_of(device, t, 3568 md_sync_timer); 3569 drbd_device_post_work(device, MD_SYNC); 3570 } 3571 3572 const char *cmdname(enum drbd_packet cmd) 3573 { 3574 /* THINK may need to become several global tables 3575 * when we want to support more than 3576 * one PRO_VERSION */ 3577 static const char *cmdnames[] = { 3578 3579 [P_DATA] = "Data", 3580 [P_DATA_REPLY] = "DataReply", 3581 [P_RS_DATA_REPLY] = "RSDataReply", 3582 [P_BARRIER] = "Barrier", 3583 [P_BITMAP] = "ReportBitMap", 3584 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget", 3585 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource", 3586 [P_UNPLUG_REMOTE] = "UnplugRemote", 3587 [P_DATA_REQUEST] = "DataRequest", 3588 [P_RS_DATA_REQUEST] = "RSDataRequest", 3589 [P_SYNC_PARAM] = "SyncParam", 3590 [P_PROTOCOL] = "ReportProtocol", 3591 [P_UUIDS] = "ReportUUIDs", 3592 [P_SIZES] = "ReportSizes", 3593 [P_STATE] = "ReportState", 3594 [P_SYNC_UUID] = "ReportSyncUUID", 3595 [P_AUTH_CHALLENGE] = "AuthChallenge", 3596 [P_AUTH_RESPONSE] = "AuthResponse", 3597 [P_STATE_CHG_REQ] = "StateChgRequest", 3598 [P_PING] = "Ping", 3599 [P_PING_ACK] = "PingAck", 3600 [P_RECV_ACK] = "RecvAck", 3601 [P_WRITE_ACK] = "WriteAck", 3602 [P_RS_WRITE_ACK] = "RSWriteAck", 3603 [P_SUPERSEDED] = "Superseded", 3604 [P_NEG_ACK] = "NegAck", 3605 [P_NEG_DREPLY] = "NegDReply", 3606 [P_NEG_RS_DREPLY] = "NegRSDReply", 3607 [P_BARRIER_ACK] = "BarrierAck", 3608 [P_STATE_CHG_REPLY] = "StateChgReply", 3609 [P_OV_REQUEST] = "OVRequest", 3610 [P_OV_REPLY] = "OVReply", 3611 [P_OV_RESULT] = "OVResult", 3612 [P_CSUM_RS_REQUEST] = "CsumRSRequest", 3613 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync", 3614 [P_SYNC_PARAM89] = "SyncParam89", 3615 [P_COMPRESSED_BITMAP] = "CBitmap", 3616 [P_DELAY_PROBE] = "DelayProbe", 3617 [P_OUT_OF_SYNC] = "OutOfSync", 3618 [P_RS_CANCEL] = "RSCancel", 3619 [P_CONN_ST_CHG_REQ] = "conn_st_chg_req", 3620 [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply", 3621 [P_PROTOCOL_UPDATE] = "protocol_update", 3622 [P_TRIM] = "Trim", 3623 [P_RS_THIN_REQ] = "rs_thin_req", 3624 [P_RS_DEALLOCATED] = "rs_deallocated", 3625 [P_WSAME] = "WriteSame", 3626 [P_ZEROES] = "Zeroes", 3627 3628 /* enum drbd_packet, but not commands - obsoleted flags: 3629 * P_MAY_IGNORE 3630 * P_MAX_OPT_CMD 3631 */ 3632 }; 3633 3634 /* too big for the array: 0xfffX */ 3635 if (cmd == P_INITIAL_META) 3636 return "InitialMeta"; 3637 if (cmd == P_INITIAL_DATA) 3638 return "InitialData"; 3639 if (cmd == P_CONNECTION_FEATURES) 3640 return "ConnectionFeatures"; 3641 if (cmd >= ARRAY_SIZE(cmdnames)) 3642 return "Unknown"; 3643 return cmdnames[cmd]; 3644 } 3645 3646 /** 3647 * drbd_wait_misc - wait for a request to make progress 3648 * @device: device associated with the request 3649 * @i: the struct drbd_interval embedded in struct drbd_request or 3650 * struct drbd_peer_request 3651 */ 3652 int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i) 3653 { 3654 struct net_conf *nc; 3655 DEFINE_WAIT(wait); 3656 long timeout; 3657 3658 rcu_read_lock(); 3659 nc = rcu_dereference(first_peer_device(device)->connection->net_conf); 3660 if (!nc) { 3661 rcu_read_unlock(); 3662 return -ETIMEDOUT; 3663 } 3664 timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT; 3665 rcu_read_unlock(); 3666 3667 /* Indicate to wake up device->misc_wait on progress. */ 3668 i->waiting = true; 3669 prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE); 3670 spin_unlock_irq(&device->resource->req_lock); 3671 timeout = schedule_timeout(timeout); 3672 finish_wait(&device->misc_wait, &wait); 3673 spin_lock_irq(&device->resource->req_lock); 3674 if (!timeout || device->state.conn < C_CONNECTED) 3675 return -ETIMEDOUT; 3676 if (signal_pending(current)) 3677 return -ERESTARTSYS; 3678 return 0; 3679 } 3680 3681 void lock_all_resources(void) 3682 { 3683 struct drbd_resource *resource; 3684 int __maybe_unused i = 0; 3685 3686 mutex_lock(&resources_mutex); 3687 local_irq_disable(); 3688 for_each_resource(resource, &drbd_resources) 3689 spin_lock_nested(&resource->req_lock, i++); 3690 } 3691 3692 void unlock_all_resources(void) 3693 { 3694 struct drbd_resource *resource; 3695 3696 for_each_resource(resource, &drbd_resources) 3697 spin_unlock(&resource->req_lock); 3698 local_irq_enable(); 3699 mutex_unlock(&resources_mutex); 3700 } 3701 3702 #ifdef CONFIG_DRBD_FAULT_INJECTION 3703 /* Fault insertion support including random number generator shamelessly 3704 * stolen from kernel/rcutorture.c */ 3705 struct fault_random_state { 3706 unsigned long state; 3707 unsigned long count; 3708 }; 3709 3710 #define FAULT_RANDOM_MULT 39916801 /* prime */ 3711 #define FAULT_RANDOM_ADD 479001701 /* prime */ 3712 #define FAULT_RANDOM_REFRESH 10000 3713 3714 /* 3715 * Crude but fast random-number generator. Uses a linear congruential 3716 * generator, with occasional help from get_random_bytes(). 3717 */ 3718 static unsigned long 3719 _drbd_fault_random(struct fault_random_state *rsp) 3720 { 3721 long refresh; 3722 3723 if (!rsp->count--) { 3724 get_random_bytes(&refresh, sizeof(refresh)); 3725 rsp->state += refresh; 3726 rsp->count = FAULT_RANDOM_REFRESH; 3727 } 3728 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD; 3729 return swahw32(rsp->state); 3730 } 3731 3732 static char * 3733 _drbd_fault_str(unsigned int type) { 3734 static char *_faults[] = { 3735 [DRBD_FAULT_MD_WR] = "Meta-data write", 3736 [DRBD_FAULT_MD_RD] = "Meta-data read", 3737 [DRBD_FAULT_RS_WR] = "Resync write", 3738 [DRBD_FAULT_RS_RD] = "Resync read", 3739 [DRBD_FAULT_DT_WR] = "Data write", 3740 [DRBD_FAULT_DT_RD] = "Data read", 3741 [DRBD_FAULT_DT_RA] = "Data read ahead", 3742 [DRBD_FAULT_BM_ALLOC] = "BM allocation", 3743 [DRBD_FAULT_AL_EE] = "EE allocation", 3744 [DRBD_FAULT_RECEIVE] = "receive data corruption", 3745 }; 3746 3747 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**"; 3748 } 3749 3750 unsigned int 3751 _drbd_insert_fault(struct drbd_device *device, unsigned int type) 3752 { 3753 static struct fault_random_state rrs = {0, 0}; 3754 3755 unsigned int ret = ( 3756 (drbd_fault_devs == 0 || 3757 ((1 << device_to_minor(device)) & drbd_fault_devs) != 0) && 3758 (((_drbd_fault_random(&rrs) % 100) + 1) <= drbd_fault_rate)); 3759 3760 if (ret) { 3761 drbd_fault_count++; 3762 3763 if (drbd_ratelimit()) 3764 drbd_warn(device, "***Simulating %s failure\n", 3765 _drbd_fault_str(type)); 3766 } 3767 3768 return ret; 3769 } 3770 #endif 3771 3772 module_init(drbd_init) 3773 module_exit(drbd_cleanup) 3774 3775 EXPORT_SYMBOL(drbd_conn_str); 3776 EXPORT_SYMBOL(drbd_role_str); 3777 EXPORT_SYMBOL(drbd_disk_str); 3778 EXPORT_SYMBOL(drbd_set_st_err_str); 3779