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