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