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