drbd: Introduce "peer_device" object between "device" and "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{
a6b32bc3 311 struct drbd_connection *connection = first_peer_device(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{
a6b32bc3 636 return conn_prepare_command(first_peer_device(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
a6b32bc3 689 err = __send_command(first_peer_device(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;
a6b32bc3 720 const int apv = first_peer_device(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
a6b32bc3 725 sock = &first_peer_device(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();
a6b32bc3 731 nc = rcu_dereference(first_peer_device(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
a6b32bc3 834 sock = &first_peer_device(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();
a6b32bc3 848 uuid_flags |= rcu_dereference(first_peer_device(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
a6b32bc3 903 sock = &first_peer_device(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
a6b32bc3 936 sock = &first_peer_device(device)->connection->data;
b30ab791 937 p = drbd_prepare_command(device, sock);
9f5bdc33
AG
938 if (!p)
939 return -EIO;
6809384c 940
a6b32bc3 941 if (first_peer_device(device)->connection->agreed_pro_version <= 94)
98683650 942 max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
a6b32bc3 943 else if (first_peer_device(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
a6b32bc3 964 sock = &first_peer_device(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
a6b32bc3 987 sock = &first_peer_device(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
a6b32bc3 1000 sock = &first_peer_device(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
a6b32bc3 1030 sock = &first_peer_device(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();
a6b32bc3 1084 use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle;
44ed167d 1085 rcu_read_unlock();
a6b32bc3 1086 if (!use_rle || first_peer_device(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{
a6b32bc3
AG
1175 struct drbd_socket *sock = &first_peer_device(device)->connection->data;
1176 unsigned int header_size = drbd_header_size(first_peer_device(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);
a6b32bc3 1187 err = __send_command(first_peer_device(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);
a6b32bc3 1208 err = __send_command(first_peer_device(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{
a6b32bc3 1268 struct drbd_socket *sock = &first_peer_device(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
a6b32bc3 1312 sock = &first_peer_device(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{
a6b32bc3
AG
1329 if (first_peer_device(device)->connection->peer_integrity_tfm)
1330 data_size -= crypto_hash_digestsize(first_peer_device(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
a6b32bc3 1373 sock = &first_peer_device(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
a6b32bc3 1391 sock = &first_peer_device(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
a6b32bc3 1407 sock = &first_peer_device(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
a6b32bc3 1479 socket = first_peer_device(device)->connection->data.socket;
b987427b 1480 addr = kmap(page) + offset;
a6b32bc3 1481 err = drbd_send_all(first_peer_device(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{
a6b32bc3 1491 struct socket *socket = first_peer_device(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;
a6b32bc3 1506 drbd_update_congested(first_peer_device(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) {
a6b32bc3 1514 if (we_should_drop_the_connection(first_peer_device(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);
a6b32bc3 1528 clear_bit(NET_CONGESTED, &first_peer_device(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{
a6b32bc3 1596 if (first_peer_device(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
a6b32bc3 1616 sock = &first_peer_device(device)->connection->data;
b30ab791 1617 p = drbd_prepare_command(device, sock);
a6b32bc3
AG
1618 dgs = first_peer_device(device)->connection->integrity_tfm ?
1619 crypto_hash_digestsize(first_peer_device(device)->connection->integrity_tfm) : 0;
b411b363 1620
9f5bdc33
AG
1621 if (!p)
1622 return -EIO;
1623 p->sector = cpu_to_be64(req->i.sector);
1624 p->block_id = (unsigned long)req;
b30ab791
AG
1625 p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
1626 dp_flags = bio_flags_to_wire(device, req->master_bio->bi_rw);
1627 if (device->state.conn >= C_SYNC_SOURCE &&
1628 device->state.conn <= C_PAUSED_SYNC_T)
b411b363 1629 dp_flags |= DP_MAY_SET_IN_SYNC;
a6b32bc3 1630 if (first_peer_device(device)->connection->agreed_pro_version >= 100) {
303d1448
PR
1631 if (req->rq_state & RQ_EXP_RECEIVE_ACK)
1632 dp_flags |= DP_SEND_RECEIVE_ACK;
1633 if (req->rq_state & RQ_EXP_WRITE_ACK)
1634 dp_flags |= DP_SEND_WRITE_ACK;
1635 }
9f5bdc33
AG
1636 p->dp_flags = cpu_to_be32(dp_flags);
1637 if (dgs)
a6b32bc3
AG
1638 drbd_csum_bio(device, first_peer_device(device)->connection->integrity_tfm, req->master_bio, p + 1);
1639 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_DATA, sizeof(*p) + dgs, NULL, req->i.size);
6bdb9b0e 1640 if (!err) {
470be44a
LE
1641 /* For protocol A, we have to memcpy the payload into
1642 * socket buffers, as we may complete right away
1643 * as soon as we handed it over to tcp, at which point the data
1644 * pages may become invalid.
1645 *
1646 * For data-integrity enabled, we copy it as well, so we can be
1647 * sure that even if the bio pages may still be modified, it
1648 * won't change the data on the wire, thus if the digest checks
1649 * out ok after sending on this side, but does not fit on the
1650 * receiving side, we sure have detected corruption elsewhere.
1651 */
303d1448 1652 if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || dgs)
b30ab791 1653 err = _drbd_send_bio(device, req->master_bio);
b411b363 1654 else
b30ab791 1655 err = _drbd_send_zc_bio(device, req->master_bio);
470be44a
LE
1656
1657 /* double check digest, sometimes buffers have been modified in flight. */
1658 if (dgs > 0 && dgs <= 64) {
24c4830c 1659 /* 64 byte, 512 bit, is the largest digest size
470be44a
LE
1660 * currently supported in kernel crypto. */
1661 unsigned char digest[64];
a6b32bc3 1662 drbd_csum_bio(device, first_peer_device(device)->connection->integrity_tfm, req->master_bio, digest);
9f5bdc33 1663 if (memcmp(p + 1, digest, dgs)) {
470be44a
LE
1664 dev_warn(DEV,
1665 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
ace652ac 1666 (unsigned long long)req->i.sector, req->i.size);
470be44a
LE
1667 }
1668 } /* else if (dgs > 64) {
1669 ... Be noisy about digest too large ...
1670 } */
b411b363 1671 }
9f5bdc33 1672 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
b411b363 1673
6bdb9b0e 1674 return err;
b411b363
PR
1675}
1676
1677/* answer packet, used to send data back for read requests:
1678 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1679 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1680 */
b30ab791 1681int drbd_send_block(struct drbd_device *device, enum drbd_packet cmd,
db830c46 1682 struct drbd_peer_request *peer_req)
b411b363 1683{
9f5bdc33
AG
1684 struct drbd_socket *sock;
1685 struct p_data *p;
7b57b89d 1686 int err;
b411b363
PR
1687 int dgs;
1688
a6b32bc3 1689 sock = &first_peer_device(device)->connection->data;
b30ab791 1690 p = drbd_prepare_command(device, sock);
b411b363 1691
a6b32bc3
AG
1692 dgs = first_peer_device(device)->connection->integrity_tfm ?
1693 crypto_hash_digestsize(first_peer_device(device)->connection->integrity_tfm) : 0;
b411b363 1694
9f5bdc33
AG
1695 if (!p)
1696 return -EIO;
1697 p->sector = cpu_to_be64(peer_req->i.sector);
1698 p->block_id = peer_req->block_id;
1699 p->seq_num = 0; /* unused */
b17f33cb 1700 p->dp_flags = 0;
9f5bdc33 1701 if (dgs)
a6b32bc3
AG
1702 drbd_csum_ee(device, first_peer_device(device)->connection->integrity_tfm, peer_req, p + 1);
1703 err = __send_command(first_peer_device(device)->connection, device->vnr, sock, cmd, sizeof(*p) + dgs, NULL, peer_req->i.size);
7b57b89d 1704 if (!err)
b30ab791 1705 err = _drbd_send_zc_ee(device, peer_req);
9f5bdc33 1706 mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
bd26bfc5 1707
7b57b89d 1708 return err;
b411b363
PR
1709}
1710
b30ab791 1711int drbd_send_out_of_sync(struct drbd_device *device, struct drbd_request *req)
73a01a18 1712{
9f5bdc33
AG
1713 struct drbd_socket *sock;
1714 struct p_block_desc *p;
73a01a18 1715
a6b32bc3 1716 sock = &first_peer_device(device)->connection->data;
b30ab791 1717 p = drbd_prepare_command(device, sock);
9f5bdc33
AG
1718 if (!p)
1719 return -EIO;
1720 p->sector = cpu_to_be64(req->i.sector);
1721 p->blksize = cpu_to_be32(req->i.size);
b30ab791 1722 return drbd_send_command(device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
73a01a18
PR
1723}
1724
b411b363
PR
1725/*
1726 drbd_send distinguishes two cases:
1727
1728 Packets sent via the data socket "sock"
1729 and packets sent via the meta data socket "msock"
1730
1731 sock msock
1732 -----------------+-------------------------+------------------------------
1733 timeout conf.timeout / 2 conf.timeout / 2
1734 timeout action send a ping via msock Abort communication
1735 and close all sockets
1736*/
1737
1738/*
1739 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1740 */
bde89a9e 1741int drbd_send(struct drbd_connection *connection, struct socket *sock,
b411b363
PR
1742 void *buf, size_t size, unsigned msg_flags)
1743{
1744 struct kvec iov;
1745 struct msghdr msg;
1746 int rv, sent = 0;
1747
1748 if (!sock)
c0d42c8e 1749 return -EBADR;
b411b363
PR
1750
1751 /* THINK if (signal_pending) return ... ? */
1752
1753 iov.iov_base = buf;
1754 iov.iov_len = size;
1755
1756 msg.msg_name = NULL;
1757 msg.msg_namelen = 0;
1758 msg.msg_control = NULL;
1759 msg.msg_controllen = 0;
1760 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
1761
bde89a9e 1762 if (sock == connection->data.socket) {
44ed167d 1763 rcu_read_lock();
bde89a9e 1764 connection->ko_count = rcu_dereference(connection->net_conf)->ko_count;
44ed167d 1765 rcu_read_unlock();
bde89a9e 1766 drbd_update_congested(connection);
b411b363
PR
1767 }
1768 do {
1769 /* STRANGE
1770 * tcp_sendmsg does _not_ use its size parameter at all ?
1771 *
1772 * -EAGAIN on timeout, -EINTR on signal.
1773 */
1774/* THINK
1775 * do we need to block DRBD_SIG if sock == &meta.socket ??
1776 * otherwise wake_asender() might interrupt some send_*Ack !
1777 */
1778 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
1779 if (rv == -EAGAIN) {
bde89a9e 1780 if (we_should_drop_the_connection(connection, sock))
b411b363
PR
1781 break;
1782 else
1783 continue;
1784 }
b411b363
PR
1785 if (rv == -EINTR) {
1786 flush_signals(current);
1787 rv = 0;
1788 }
1789 if (rv < 0)
1790 break;
1791 sent += rv;
1792 iov.iov_base += rv;
1793 iov.iov_len -= rv;
1794 } while (sent < size);
1795
bde89a9e
AG
1796 if (sock == connection->data.socket)
1797 clear_bit(NET_CONGESTED, &connection->flags);
b411b363
PR
1798
1799 if (rv <= 0) {
1800 if (rv != -EAGAIN) {
bde89a9e
AG
1801 conn_err(connection, "%s_sendmsg returned %d\n",
1802 sock == connection->meta.socket ? "msock" : "sock",
bedbd2a5 1803 rv);
bde89a9e 1804 conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
b411b363 1805 } else
bde89a9e 1806 conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
b411b363
PR
1807 }
1808
1809 return sent;
1810}
1811
fb708e40
AG
1812/**
1813 * drbd_send_all - Send an entire buffer
1814 *
1815 * Returns 0 upon success and a negative error value otherwise.
1816 */
bde89a9e 1817int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer,
fb708e40
AG
1818 size_t size, unsigned msg_flags)
1819{
1820 int err;
1821
bde89a9e 1822 err = drbd_send(connection, sock, buffer, size, msg_flags);
fb708e40
AG
1823 if (err < 0)
1824 return err;
1825 if (err != size)
1826 return -EIO;
1827 return 0;
1828}
1829
b411b363
PR
1830static int drbd_open(struct block_device *bdev, fmode_t mode)
1831{
b30ab791 1832 struct drbd_device *device = bdev->bd_disk->private_data;
b411b363
PR
1833 unsigned long flags;
1834 int rv = 0;
1835
2a48fc0a 1836 mutex_lock(&drbd_main_mutex);
a6b32bc3 1837 spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
b30ab791 1838 /* to have a stable device->state.role
b411b363
PR
1839 * and no race with updating open_cnt */
1840
b30ab791 1841 if (device->state.role != R_PRIMARY) {
b411b363
PR
1842 if (mode & FMODE_WRITE)
1843 rv = -EROFS;
1844 else if (!allow_oos)
1845 rv = -EMEDIUMTYPE;
1846 }
1847
1848 if (!rv)
b30ab791 1849 device->open_cnt++;
a6b32bc3 1850 spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
2a48fc0a 1851 mutex_unlock(&drbd_main_mutex);
b411b363
PR
1852
1853 return rv;
1854}
1855
db2a144b 1856static void drbd_release(struct gendisk *gd, fmode_t mode)
b411b363 1857{
b30ab791 1858 struct drbd_device *device = gd->private_data;
2a48fc0a 1859 mutex_lock(&drbd_main_mutex);
b30ab791 1860 device->open_cnt--;
2a48fc0a 1861 mutex_unlock(&drbd_main_mutex);
b411b363
PR
1862}
1863
b30ab791 1864static void drbd_set_defaults(struct drbd_device *device)
b411b363 1865{
f399002e
LE
1866 /* Beware! The actual layout differs
1867 * between big endian and little endian */
b30ab791 1868 device->state = (union drbd_dev_state) {
b411b363
PR
1869 { .role = R_SECONDARY,
1870 .peer = R_UNKNOWN,
1871 .conn = C_STANDALONE,
1872 .disk = D_DISKLESS,
1873 .pdsk = D_UNKNOWN,
b411b363
PR
1874 } };
1875}
1876
b30ab791 1877void drbd_init_set_defaults(struct drbd_device *device)
b411b363
PR
1878{
1879 /* the memset(,0,) did most of this.
1880 * note: only assignments, no allocation in here */
1881
b30ab791
AG
1882 drbd_set_defaults(device);
1883
1884 atomic_set(&device->ap_bio_cnt, 0);
1885 atomic_set(&device->ap_pending_cnt, 0);
1886 atomic_set(&device->rs_pending_cnt, 0);
1887 atomic_set(&device->unacked_cnt, 0);
1888 atomic_set(&device->local_cnt, 0);
1889 atomic_set(&device->pp_in_use_by_net, 0);
1890 atomic_set(&device->rs_sect_in, 0);
1891 atomic_set(&device->rs_sect_ev, 0);
1892 atomic_set(&device->ap_in_flight, 0);
1893 atomic_set(&device->md_io_in_use, 0);
1894
1895 mutex_init(&device->own_state_mutex);
1896 device->state_mutex = &device->own_state_mutex;
1897
1898 spin_lock_init(&device->al_lock);
1899 spin_lock_init(&device->peer_seq_lock);
1900
1901 INIT_LIST_HEAD(&device->active_ee);
1902 INIT_LIST_HEAD(&device->sync_ee);
1903 INIT_LIST_HEAD(&device->done_ee);
1904 INIT_LIST_HEAD(&device->read_ee);
1905 INIT_LIST_HEAD(&device->net_ee);
1906 INIT_LIST_HEAD(&device->resync_reads);
1907 INIT_LIST_HEAD(&device->resync_work.list);
1908 INIT_LIST_HEAD(&device->unplug_work.list);
1909 INIT_LIST_HEAD(&device->go_diskless.list);
1910 INIT_LIST_HEAD(&device->md_sync_work.list);
1911 INIT_LIST_HEAD(&device->start_resync_work.list);
1912 INIT_LIST_HEAD(&device->bm_io_work.w.list);
1913
1914 device->resync_work.cb = w_resync_timer;
1915 device->unplug_work.cb = w_send_write_hint;
1916 device->go_diskless.cb = w_go_diskless;
1917 device->md_sync_work.cb = w_md_sync;
1918 device->bm_io_work.w.cb = w_bitmap_io;
1919 device->start_resync_work.cb = w_start_resync;
1920
1921 device->resync_work.device = device;
1922 device->unplug_work.device = device;
1923 device->go_diskless.device = device;
1924 device->md_sync_work.device = device;
1925 device->bm_io_work.w.device = device;
1926 device->start_resync_work.device = device;
1927
1928 init_timer(&device->resync_timer);
1929 init_timer(&device->md_sync_timer);
1930 init_timer(&device->start_resync_timer);
1931 init_timer(&device->request_timer);
1932 device->resync_timer.function = resync_timer_fn;
1933 device->resync_timer.data = (unsigned long) device;
1934 device->md_sync_timer.function = md_sync_timer_fn;
1935 device->md_sync_timer.data = (unsigned long) device;
1936 device->start_resync_timer.function = start_resync_timer_fn;
1937 device->start_resync_timer.data = (unsigned long) device;
1938 device->request_timer.function = request_timer_fn;
1939 device->request_timer.data = (unsigned long) device;
1940
1941 init_waitqueue_head(&device->misc_wait);
1942 init_waitqueue_head(&device->state_wait);
1943 init_waitqueue_head(&device->ee_wait);
1944 init_waitqueue_head(&device->al_wait);
1945 init_waitqueue_head(&device->seq_wait);
1946
1947 device->resync_wenr = LC_FREE;
1948 device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1949 device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
1950}
1951
1952void drbd_device_cleanup(struct drbd_device *device)
b411b363 1953{
1d7734a0 1954 int i;
a6b32bc3 1955 if (first_peer_device(device)->connection->receiver.t_state != NONE)
b411b363 1956 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
a6b32bc3 1957 first_peer_device(device)->connection->receiver.t_state);
b30ab791
AG
1958
1959 device->al_writ_cnt =
1960 device->bm_writ_cnt =
1961 device->read_cnt =
1962 device->recv_cnt =
1963 device->send_cnt =
1964 device->writ_cnt =
1965 device->p_size =
1966 device->rs_start =
1967 device->rs_total =
1968 device->rs_failed = 0;
1969 device->rs_last_events = 0;
1970 device->rs_last_sect_ev = 0;
1d7734a0 1971 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
b30ab791
AG
1972 device->rs_mark_left[i] = 0;
1973 device->rs_mark_time[i] = 0;
1d7734a0 1974 }
a6b32bc3 1975 D_ASSERT(first_peer_device(device)->connection->net_conf == NULL);
b411b363 1976
b30ab791
AG
1977 drbd_set_my_capacity(device, 0);
1978 if (device->bitmap) {
b411b363 1979 /* maybe never allocated. */
b30ab791
AG
1980 drbd_bm_resize(device, 0, 1);
1981 drbd_bm_cleanup(device);
b411b363
PR
1982 }
1983
b30ab791
AG
1984 drbd_free_bc(device->ldev);
1985 device->ldev = NULL;
1d041225 1986
b30ab791 1987 clear_bit(AL_SUSPENDED, &device->flags);
b411b363 1988
b30ab791
AG
1989 D_ASSERT(list_empty(&device->active_ee));
1990 D_ASSERT(list_empty(&device->sync_ee));
1991 D_ASSERT(list_empty(&device->done_ee));
1992 D_ASSERT(list_empty(&device->read_ee));
1993 D_ASSERT(list_empty(&device->net_ee));
1994 D_ASSERT(list_empty(&device->resync_reads));
a6b32bc3 1995 D_ASSERT(list_empty(&first_peer_device(device)->connection->sender_work.q));
b30ab791
AG
1996 D_ASSERT(list_empty(&device->resync_work.list));
1997 D_ASSERT(list_empty(&device->unplug_work.list));
1998 D_ASSERT(list_empty(&device->go_diskless.list));
2265b473 1999
b30ab791 2000 drbd_set_defaults(device);
b411b363
PR
2001}
2002
2003
2004static void drbd_destroy_mempools(void)
2005{
2006 struct page *page;
2007
2008 while (drbd_pp_pool) {
2009 page = drbd_pp_pool;
2010 drbd_pp_pool = (struct page *)page_private(page);
2011 __free_page(page);
2012 drbd_pp_vacant--;
2013 }
2014
2015 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2016
9476f39d
LE
2017 if (drbd_md_io_bio_set)
2018 bioset_free(drbd_md_io_bio_set);
4281808f
LE
2019 if (drbd_md_io_page_pool)
2020 mempool_destroy(drbd_md_io_page_pool);
b411b363
PR
2021 if (drbd_ee_mempool)
2022 mempool_destroy(drbd_ee_mempool);
2023 if (drbd_request_mempool)
2024 mempool_destroy(drbd_request_mempool);
2025 if (drbd_ee_cache)
2026 kmem_cache_destroy(drbd_ee_cache);
2027 if (drbd_request_cache)
2028 kmem_cache_destroy(drbd_request_cache);
2029 if (drbd_bm_ext_cache)
2030 kmem_cache_destroy(drbd_bm_ext_cache);
2031 if (drbd_al_ext_cache)
2032 kmem_cache_destroy(drbd_al_ext_cache);
2033
9476f39d 2034 drbd_md_io_bio_set = NULL;
4281808f 2035 drbd_md_io_page_pool = NULL;
b411b363
PR
2036 drbd_ee_mempool = NULL;
2037 drbd_request_mempool = NULL;
2038 drbd_ee_cache = NULL;
2039 drbd_request_cache = NULL;
2040 drbd_bm_ext_cache = NULL;
2041 drbd_al_ext_cache = NULL;
2042
2043 return;
2044}
2045
2046static int drbd_create_mempools(void)
2047{
2048 struct page *page;
1816a2b4 2049 const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
b411b363
PR
2050 int i;
2051
2052 /* prepare our caches and mempools */
2053 drbd_request_mempool = NULL;
2054 drbd_ee_cache = NULL;
2055 drbd_request_cache = NULL;
2056 drbd_bm_ext_cache = NULL;
2057 drbd_al_ext_cache = NULL;
2058 drbd_pp_pool = NULL;
4281808f 2059 drbd_md_io_page_pool = NULL;
9476f39d 2060 drbd_md_io_bio_set = NULL;
b411b363
PR
2061
2062 /* caches */
2063 drbd_request_cache = kmem_cache_create(
2064 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2065 if (drbd_request_cache == NULL)
2066 goto Enomem;
2067
2068 drbd_ee_cache = kmem_cache_create(
f6ffca9f 2069 "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
b411b363
PR
2070 if (drbd_ee_cache == NULL)
2071 goto Enomem;
2072
2073 drbd_bm_ext_cache = kmem_cache_create(
2074 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2075 if (drbd_bm_ext_cache == NULL)
2076 goto Enomem;
2077
2078 drbd_al_ext_cache = kmem_cache_create(
2079 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2080 if (drbd_al_ext_cache == NULL)
2081 goto Enomem;
2082
2083 /* mempools */
9476f39d
LE
2084 drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0);
2085 if (drbd_md_io_bio_set == NULL)
2086 goto Enomem;
9476f39d 2087
4281808f
LE
2088 drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
2089 if (drbd_md_io_page_pool == NULL)
2090 goto Enomem;
2091
b411b363
PR
2092 drbd_request_mempool = mempool_create(number,
2093 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2094 if (drbd_request_mempool == NULL)
2095 goto Enomem;
2096
2097 drbd_ee_mempool = mempool_create(number,
2098 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
2027ae1f 2099 if (drbd_ee_mempool == NULL)
b411b363
PR
2100 goto Enomem;
2101
2102 /* drbd's page pool */
2103 spin_lock_init(&drbd_pp_lock);
2104
2105 for (i = 0; i < number; i++) {
2106 page = alloc_page(GFP_HIGHUSER);
2107 if (!page)
2108 goto Enomem;
2109 set_page_private(page, (unsigned long)drbd_pp_pool);
2110 drbd_pp_pool = page;
2111 }
2112 drbd_pp_vacant = number;
2113
2114 return 0;
2115
2116Enomem:
2117 drbd_destroy_mempools(); /* in case we allocated some */
2118 return -ENOMEM;
2119}
2120
2121static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2122 void *unused)
2123{
2124 /* just so we have it. you never know what interesting things we
2125 * might want to do here some day...
2126 */
2127
2128 return NOTIFY_DONE;
2129}
2130
2131static struct notifier_block drbd_notifier = {
2132 .notifier_call = drbd_notify_sys,
2133};
2134
b30ab791 2135static void drbd_release_all_peer_reqs(struct drbd_device *device)
b411b363
PR
2136{
2137 int rr;
2138
b30ab791 2139 rr = drbd_free_peer_reqs(device, &device->active_ee);
b411b363
PR
2140 if (rr)
2141 dev_err(DEV, "%d EEs in active list found!\n", rr);
2142
b30ab791 2143 rr = drbd_free_peer_reqs(device, &device->sync_ee);
b411b363
PR
2144 if (rr)
2145 dev_err(DEV, "%d EEs in sync list found!\n", rr);
2146
b30ab791 2147 rr = drbd_free_peer_reqs(device, &device->read_ee);
b411b363
PR
2148 if (rr)
2149 dev_err(DEV, "%d EEs in read list found!\n", rr);
2150
b30ab791 2151 rr = drbd_free_peer_reqs(device, &device->done_ee);
b411b363
PR
2152 if (rr)
2153 dev_err(DEV, "%d EEs in done list found!\n", rr);
2154
b30ab791 2155 rr = drbd_free_peer_reqs(device, &device->net_ee);
b411b363
PR
2156 if (rr)
2157 dev_err(DEV, "%d EEs in net list found!\n", rr);
2158}
2159
774b3055 2160/* caution. no locking. */
81fa2e67 2161void drbd_minor_destroy(struct kref *kref)
b411b363 2162{
b30ab791 2163 struct drbd_device *device = container_of(kref, struct drbd_device, kref);
a6b32bc3 2164 struct drbd_connection *connection = first_peer_device(device)->connection;
b411b363 2165
b30ab791 2166 del_timer_sync(&device->request_timer);
dfa8bedb 2167
b411b363 2168 /* paranoia asserts */
b30ab791 2169 D_ASSERT(device->open_cnt == 0);
b411b363
PR
2170 /* end paranoia asserts */
2171
b411b363
PR
2172 /* cleanup stuff that may have been allocated during
2173 * device (re-)configuration or state changes */
2174
b30ab791
AG
2175 if (device->this_bdev)
2176 bdput(device->this_bdev);
b411b363 2177
b30ab791
AG
2178 drbd_free_bc(device->ldev);
2179 device->ldev = NULL;
b411b363 2180
b30ab791 2181 drbd_release_all_peer_reqs(device);
b411b363 2182
b30ab791
AG
2183 lc_destroy(device->act_log);
2184 lc_destroy(device->resync);
b411b363 2185
b30ab791
AG
2186 kfree(device->p_uuid);
2187 /* device->p_uuid = NULL; */
b411b363 2188
b30ab791
AG
2189 if (device->bitmap) /* should no longer be there. */
2190 drbd_bm_cleanup(device);
2191 __free_page(device->md_io_page);
2192 put_disk(device->vdisk);
2193 blk_cleanup_queue(device->rq_queue);
2194 kfree(device->rs_plan_s);
a6b32bc3 2195 kfree(first_peer_device(device));
b30ab791 2196 kfree(device);
9dc9fbb3 2197
bde89a9e 2198 kref_put(&connection->kref, &conn_destroy);
b411b363
PR
2199}
2200
2312f0b3
LE
2201/* One global retry thread, if we need to push back some bio and have it
2202 * reinserted through our make request function.
2203 */
2204static struct retry_worker {
2205 struct workqueue_struct *wq;
2206 struct work_struct worker;
2207
2208 spinlock_t lock;
2209 struct list_head writes;
2210} retry;
2211
2212static void do_retry(struct work_struct *ws)
2213{
2214 struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
2215 LIST_HEAD(writes);
2216 struct drbd_request *req, *tmp;
2217
2218 spin_lock_irq(&retry->lock);
2219 list_splice_init(&retry->writes, &writes);
2220 spin_unlock_irq(&retry->lock);
2221
2222 list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
b30ab791 2223 struct drbd_device *device = req->w.device;
2312f0b3
LE
2224 struct bio *bio = req->master_bio;
2225 unsigned long start_time = req->start_time;
9a278a79
LE
2226 bool expected;
2227
2228 expected =
2229 expect(atomic_read(&req->completion_ref) == 0) &&
2230 expect(req->rq_state & RQ_POSTPONED) &&
2231 expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
2232 (req->rq_state & RQ_LOCAL_ABORTED) != 0);
2233
2234 if (!expected)
2235 dev_err(DEV, "req=%p completion_ref=%d rq_state=%x\n",
2236 req, atomic_read(&req->completion_ref),
2237 req->rq_state);
2238
2239 /* We still need to put one kref associated with the
2240 * "completion_ref" going zero in the code path that queued it
2241 * here. The request object may still be referenced by a
2242 * frozen local req->private_bio, in case we force-detached.
2312f0b3 2243 */
9a278a79 2244 kref_put(&req->kref, drbd_req_destroy);
2312f0b3
LE
2245
2246 /* A single suspended or otherwise blocking device may stall
2247 * all others as well. Fortunately, this code path is to
2248 * recover from a situation that "should not happen":
2249 * concurrent writes in multi-primary setup.
2250 * In a "normal" lifecycle, this workqueue is supposed to be
2251 * destroyed without ever doing anything.
2252 * If it turns out to be an issue anyways, we can do per
2253 * resource (replication group) or per device (minor) retry
2254 * workqueues instead.
2255 */
2256
2257 /* We are not just doing generic_make_request(),
2258 * as we want to keep the start_time information. */
b30ab791
AG
2259 inc_ap_bio(device);
2260 __drbd_make_request(device, bio, start_time);
2312f0b3
LE
2261 }
2262}
2263
9d05e7c4 2264void drbd_restart_request(struct drbd_request *req)
2312f0b3
LE
2265{
2266 unsigned long flags;
2267 spin_lock_irqsave(&retry.lock, flags);
2268 list_move_tail(&req->tl_requests, &retry.writes);
2269 spin_unlock_irqrestore(&retry.lock, flags);
2270
2271 /* Drop the extra reference that would otherwise
2272 * have been dropped by complete_master_bio.
2273 * do_retry() needs to grab a new one. */
b30ab791 2274 dec_ap_bio(req->w.device);
b411b363 2275
2312f0b3 2276 queue_work(retry.wq, &retry.worker);
b411b363
PR
2277}
2278
2312f0b3 2279
b411b363
PR
2280static void drbd_cleanup(void)
2281{
2282 unsigned int i;
b30ab791 2283 struct drbd_device *device;
bde89a9e 2284 struct drbd_connection *connection, *tmp;
b411b363
PR
2285
2286 unregister_reboot_notifier(&drbd_notifier);
2287
17a93f30
LE
2288 /* first remove proc,
2289 * drbdsetup uses it's presence to detect
2290 * whether DRBD is loaded.
2291 * If we would get stuck in proc removal,
2292 * but have netlink already deregistered,
2293 * some drbdsetup commands may wait forever
2294 * for an answer.
2295 */
2296 if (drbd_proc)
2297 remove_proc_entry("drbd", NULL);
2298
2312f0b3
LE
2299 if (retry.wq)
2300 destroy_workqueue(retry.wq);
b411b363 2301
3b98c0c2 2302 drbd_genl_unregister();
b411b363 2303
b30ab791
AG
2304 idr_for_each_entry(&minors, device, i) {
2305 idr_remove(&minors, device_to_minor(device));
a6b32bc3 2306 idr_remove(&first_peer_device(device)->connection->volumes, device->vnr);
b30ab791
AG
2307 destroy_workqueue(device->submit.wq);
2308 del_gendisk(device->vdisk);
c141ebda 2309 /* synchronize_rcu(); No other threads running at this point */
b30ab791 2310 kref_put(&device->kref, &drbd_minor_destroy);
b411b363
PR
2311 }
2312
c141ebda 2313 /* not _rcu since, no other updater anymore. Genl already unregistered */
bde89a9e
AG
2314 list_for_each_entry_safe(connection, tmp, &drbd_connections, connections) {
2315 list_del(&connection->connections); /* not _rcu no proc, not other threads */
c141ebda 2316 /* synchronize_rcu(); */
bde89a9e 2317 kref_put(&connection->kref, &conn_destroy);
81fa2e67 2318 }
b411b363 2319
81a5d60e 2320 drbd_destroy_mempools();
b411b363
PR
2321 unregister_blkdev(DRBD_MAJOR, "drbd");
2322
81a5d60e
PR
2323 idr_destroy(&minors);
2324
b411b363
PR
2325 printk(KERN_INFO "drbd: module cleanup done.\n");
2326}
2327
2328/**
d97482ed 2329 * drbd_congested() - Callback for the flusher thread
b411b363 2330 * @congested_data: User data
d97482ed 2331 * @bdi_bits: Bits the BDI flusher thread is currently interested in
b411b363
PR
2332 *
2333 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2334 */
2335static int drbd_congested(void *congested_data, int bdi_bits)
2336{
b30ab791 2337 struct drbd_device *device = congested_data;
b411b363
PR
2338 struct request_queue *q;
2339 char reason = '-';
2340 int r = 0;
2341
b30ab791 2342 if (!may_inc_ap_bio(device)) {
b411b363
PR
2343 /* DRBD has frozen IO */
2344 r = bdi_bits;
2345 reason = 'd';
2346 goto out;
2347 }
2348
a6b32bc3 2349 if (test_bit(CALLBACK_PENDING, &first_peer_device(device)->connection->flags)) {
c2ba686f
LE
2350 r |= (1 << BDI_async_congested);
2351 /* Without good local data, we would need to read from remote,
2352 * and that would need the worker thread as well, which is
2353 * currently blocked waiting for that usermode helper to
2354 * finish.
2355 */
b30ab791 2356 if (!get_ldev_if_state(device, D_UP_TO_DATE))
c2ba686f
LE
2357 r |= (1 << BDI_sync_congested);
2358 else
b30ab791 2359 put_ldev(device);
c2ba686f
LE
2360 r &= bdi_bits;
2361 reason = 'c';
2362 goto out;
2363 }
2364
b30ab791
AG
2365 if (get_ldev(device)) {
2366 q = bdev_get_queue(device->ldev->backing_bdev);
b411b363 2367 r = bdi_congested(&q->backing_dev_info, bdi_bits);
b30ab791 2368 put_ldev(device);
b411b363
PR
2369 if (r)
2370 reason = 'b';
2371 }
2372
a6b32bc3
AG
2373 if (bdi_bits & (1 << BDI_async_congested) &&
2374 test_bit(NET_CONGESTED, &first_peer_device(device)->connection->flags)) {
b411b363
PR
2375 r |= (1 << BDI_async_congested);
2376 reason = reason == 'b' ? 'a' : 'n';
2377 }
2378
2379out:
b30ab791 2380 device->congestion_reason = reason;
b411b363
PR
2381 return r;
2382}
2383
6699b655
PR
2384static void drbd_init_workqueue(struct drbd_work_queue* wq)
2385{
6699b655
PR
2386 spin_lock_init(&wq->q_lock);
2387 INIT_LIST_HEAD(&wq->q);
8c0785a5 2388 init_waitqueue_head(&wq->q_wait);
6699b655
PR
2389}
2390
bde89a9e 2391struct drbd_connection *conn_get_by_name(const char *name)
1aba4d7f 2392{
bde89a9e 2393 struct drbd_connection *connection;
1aba4d7f 2394
3b98c0c2
LE
2395 if (!name || !name[0])
2396 return NULL;
2397
c141ebda 2398 rcu_read_lock();
bde89a9e
AG
2399 list_for_each_entry_rcu(connection, &drbd_connections, connections) {
2400 if (!strcmp(connection->name, name)) {
2401 kref_get(&connection->kref);
1aba4d7f 2402 goto found;
0ace9dfa 2403 }
1aba4d7f 2404 }
bde89a9e 2405 connection = NULL;
1aba4d7f 2406found:
c141ebda 2407 rcu_read_unlock();
bde89a9e 2408 return connection;
1aba4d7f
PR
2409}
2410
bde89a9e 2411struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
089c075d
AG
2412 void *peer_addr, int peer_addr_len)
2413{
bde89a9e 2414 struct drbd_connection *connection;
089c075d
AG
2415
2416 rcu_read_lock();
bde89a9e
AG
2417 list_for_each_entry_rcu(connection, &drbd_connections, connections) {
2418 if (connection->my_addr_len == my_addr_len &&
2419 connection->peer_addr_len == peer_addr_len &&
2420 !memcmp(&connection->my_addr, my_addr, my_addr_len) &&
2421 !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) {
2422 kref_get(&connection->kref);
089c075d
AG
2423 goto found;
2424 }
2425 }
bde89a9e 2426 connection = NULL;
089c075d
AG
2427found:
2428 rcu_read_unlock();
bde89a9e 2429 return connection;
089c075d
AG
2430}
2431
e6ef8a5c
AG
2432static int drbd_alloc_socket(struct drbd_socket *socket)
2433{
2434 socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
2435 if (!socket->rbuf)
2436 return -ENOMEM;
5a87d920
AG
2437 socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
2438 if (!socket->sbuf)
2439 return -ENOMEM;
e6ef8a5c
AG
2440 return 0;
2441}
2442
2443static void drbd_free_socket(struct drbd_socket *socket)
2444{
5a87d920 2445 free_page((unsigned long) socket->sbuf);
e6ef8a5c
AG
2446 free_page((unsigned long) socket->rbuf);
2447}
2448
bde89a9e 2449void conn_free_crypto(struct drbd_connection *connection)
91fd4dad 2450{
bde89a9e 2451 drbd_free_sock(connection);
1d041225 2452
bde89a9e
AG
2453 crypto_free_hash(connection->csums_tfm);
2454 crypto_free_hash(connection->verify_tfm);
2455 crypto_free_hash(connection->cram_hmac_tfm);
2456 crypto_free_hash(connection->integrity_tfm);
2457 crypto_free_hash(connection->peer_integrity_tfm);
2458 kfree(connection->int_dig_in);
2459 kfree(connection->int_dig_vv);
1d041225 2460
bde89a9e
AG
2461 connection->csums_tfm = NULL;
2462 connection->verify_tfm = NULL;
2463 connection->cram_hmac_tfm = NULL;
2464 connection->integrity_tfm = NULL;
2465 connection->peer_integrity_tfm = NULL;
2466 connection->int_dig_in = NULL;
2467 connection->int_dig_vv = NULL;
91fd4dad
PR
2468}
2469
bde89a9e 2470int set_resource_options(struct drbd_connection *connection, struct res_opts *res_opts)
afbbfa88
AG
2471{
2472 cpumask_var_t new_cpu_mask;
2473 int err;
2474
2475 if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
2476 return -ENOMEM;
2477 /*
2478 retcode = ERR_NOMEM;
2479 drbd_msg_put_info("unable to allocate cpumask");
2480 */
2481
2482 /* silently ignore cpu mask on UP kernel */
2483 if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
2484 /* FIXME: Get rid of constant 32 here */
c5b005ab
PR
2485 err = bitmap_parse(res_opts->cpu_mask, 32,
2486 cpumask_bits(new_cpu_mask), nr_cpu_ids);
afbbfa88 2487 if (err) {
bde89a9e 2488 conn_warn(connection, "bitmap_parse() failed with %d\n", err);
afbbfa88
AG
2489 /* retcode = ERR_CPU_MASK_PARSE; */
2490 goto fail;
2491 }
2492 }
bde89a9e
AG
2493 connection->res_opts = *res_opts;
2494 if (!cpumask_equal(connection->cpu_mask, new_cpu_mask)) {
2495 cpumask_copy(connection->cpu_mask, new_cpu_mask);
2496 drbd_calc_cpu_mask(connection);
2497 connection->receiver.reset_cpu_mask = 1;
2498 connection->asender.reset_cpu_mask = 1;
2499 connection->worker.reset_cpu_mask = 1;
afbbfa88
AG
2500 }
2501 err = 0;
2502
2503fail:
2504 free_cpumask_var(new_cpu_mask);
2505 return err;
2506
2507}
2508
ec0bddbc 2509/* caller must be under genl_lock() */
bde89a9e 2510struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts)
2111438b 2511{
bde89a9e 2512 struct drbd_connection *connection;
2111438b 2513
bde89a9e
AG
2514 connection = kzalloc(sizeof(struct drbd_connection), GFP_KERNEL);
2515 if (!connection)
2111438b
PR
2516 return NULL;
2517
bde89a9e
AG
2518 connection->name = kstrdup(name, GFP_KERNEL);
2519 if (!connection->name)
2111438b
PR
2520 goto fail;
2521
bde89a9e 2522 if (drbd_alloc_socket(&connection->data))
e6ef8a5c 2523 goto fail;
bde89a9e 2524 if (drbd_alloc_socket(&connection->meta))
e6ef8a5c
AG
2525 goto fail;
2526
bde89a9e 2527 if (!zalloc_cpumask_var(&connection->cpu_mask, GFP_KERNEL))
774b3055
PR
2528 goto fail;
2529
bde89a9e 2530 if (set_resource_options(connection, res_opts))
afbbfa88
AG
2531 goto fail;
2532
bde89a9e
AG
2533 connection->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
2534 if (!connection->current_epoch)
12038a3a 2535 goto fail;
b6dd1a89 2536
bde89a9e 2537 INIT_LIST_HEAD(&connection->transfer_log);
b6dd1a89 2538
bde89a9e
AG
2539 INIT_LIST_HEAD(&connection->current_epoch->list);
2540 connection->epochs = 1;
2541 spin_lock_init(&connection->epoch_lock);
2542 connection->write_ordering = WO_bdev_flush;
4b0007c0 2543
bde89a9e
AG
2544 connection->send.seen_any_write_yet = false;
2545 connection->send.current_epoch_nr = 0;
2546 connection->send.current_epoch_writes = 0;
b6dd1a89 2547
bde89a9e
AG
2548 connection->cstate = C_STANDALONE;
2549 mutex_init(&connection->cstate_mutex);
2550 spin_lock_init(&connection->req_lock);
2551 mutex_init(&connection->conf_update);
2552 init_waitqueue_head(&connection->ping_wait);
2553 idr_init(&connection->volumes);
b2fb6dbe 2554
bde89a9e
AG
2555 drbd_init_workqueue(&connection->sender_work);
2556 mutex_init(&connection->data.mutex);
2557 mutex_init(&connection->meta.mutex);
6699b655 2558
bde89a9e
AG
2559 drbd_thread_init(connection, &connection->receiver, drbdd_init, "receiver");
2560 drbd_thread_init(connection, &connection->worker, drbd_worker, "worker");
2561 drbd_thread_init(connection, &connection->asender, drbd_asender, "asender");
392c8801 2562
bde89a9e
AG
2563 kref_init(&connection->kref);
2564 list_add_tail_rcu(&connection->connections, &drbd_connections);
2111438b 2565
bde89a9e 2566 return connection;
2111438b
PR
2567
2568fail:
bde89a9e
AG
2569 kfree(connection->current_epoch);
2570 free_cpumask_var(connection->cpu_mask);
2571 drbd_free_socket(&connection->meta);
2572 drbd_free_socket(&connection->data);
2573 kfree(connection->name);
2574 kfree(connection);
2111438b
PR
2575
2576 return NULL;
2577}
2578
9dc9fbb3 2579void conn_destroy(struct kref *kref)
2111438b 2580{
bde89a9e 2581 struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref);
9dc9fbb3 2582
bde89a9e
AG
2583 if (atomic_read(&connection->current_epoch->epoch_size) != 0)
2584 conn_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size));
2585 kfree(connection->current_epoch);
12038a3a 2586
bde89a9e 2587 idr_destroy(&connection->volumes);
2111438b 2588
bde89a9e
AG
2589 free_cpumask_var(connection->cpu_mask);
2590 drbd_free_socket(&connection->meta);
2591 drbd_free_socket(&connection->data);
2592 kfree(connection->name);
2593 kfree(connection->int_dig_in);
2594 kfree(connection->int_dig_vv);
2595 kfree(connection);
2111438b
PR
2596}
2597
b30ab791 2598static int init_submitter(struct drbd_device *device)
113fef9e
LE
2599{
2600 /* opencoded create_singlethread_workqueue(),
2601 * to be able to say "drbd%d", ..., minor */
b30ab791
AG
2602 device->submit.wq = alloc_workqueue("drbd%u_submit",
2603 WQ_UNBOUND | WQ_MEM_RECLAIM, 1, device->minor);
2604 if (!device->submit.wq)
113fef9e
LE
2605 return -ENOMEM;
2606
b30ab791
AG
2607 INIT_WORK(&device->submit.worker, do_submit);
2608 spin_lock_init(&device->submit.lock);
2609 INIT_LIST_HEAD(&device->submit.writes);
113fef9e
LE
2610 return 0;
2611}
2612
a6b32bc3 2613enum drbd_ret_code drbd_create_minor(struct drbd_connection *connection, unsigned int minor, int vnr)
b411b363 2614{
b30ab791 2615 struct drbd_device *device;
a6b32bc3 2616 struct drbd_peer_device *peer_device;
b411b363
PR
2617 struct gendisk *disk;
2618 struct request_queue *q;
774b3055 2619 int vnr_got = vnr;
81a5d60e 2620 int minor_got = minor;
8432b314 2621 enum drbd_ret_code err = ERR_NOMEM;
774b3055 2622
b30ab791
AG
2623 device = minor_to_device(minor);
2624 if (device)
774b3055 2625 return ERR_MINOR_EXISTS;
b411b363
PR
2626
2627 /* GFP_KERNEL, we are outside of all write-out paths */
b30ab791
AG
2628 device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL);
2629 if (!device)
774b3055 2630 return ERR_NOMEM;
a6b32bc3
AG
2631 peer_device = kzalloc(sizeof(struct drbd_peer_device), GFP_KERNEL);
2632 if (!peer_device)
2633 goto out_no_peer_device;
774b3055 2634
a6b32bc3
AG
2635 INIT_LIST_HEAD(&device->peer_devices);
2636 list_add(&peer_device->peer_devices, &device->peer_devices);
bde89a9e 2637 kref_get(&connection->kref);
a6b32bc3
AG
2638 peer_device->connection = connection;
2639 peer_device->device = device;
b411b363 2640
b30ab791
AG
2641 device->minor = minor;
2642 device->vnr = vnr;
b411b363 2643
b30ab791 2644 drbd_init_set_defaults(device);
b411b363
PR
2645
2646 q = blk_alloc_queue(GFP_KERNEL);
2647 if (!q)
2648 goto out_no_q;
b30ab791
AG
2649 device->rq_queue = q;
2650 q->queuedata = device;
b411b363
PR
2651
2652 disk = alloc_disk(1);
2653 if (!disk)
2654 goto out_no_disk;
b30ab791 2655 device->vdisk = disk;
b411b363 2656
81e84650 2657 set_disk_ro(disk, true);
b411b363
PR
2658
2659 disk->queue = q;
2660 disk->major = DRBD_MAJOR;
2661 disk->first_minor = minor;
2662 disk->fops = &drbd_ops;
2663 sprintf(disk->disk_name, "drbd%d", minor);
b30ab791 2664 disk->private_data = device;
b411b363 2665
b30ab791 2666 device->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
b411b363 2667 /* we have no partitions. we contain only ourselves. */
b30ab791 2668 device->this_bdev->bd_contains = device->this_bdev;
b411b363
PR
2669
2670 q->backing_dev_info.congested_fn = drbd_congested;
b30ab791 2671 q->backing_dev_info.congested_data = device;
b411b363 2672
2f58dcfc 2673 blk_queue_make_request(q, drbd_make_request);
a73ff323 2674 blk_queue_flush(q, REQ_FLUSH | REQ_FUA);
99432fcc
PR
2675 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2676 This triggers a max_bio_size message upon first attach or connect */
2677 blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
b411b363
PR
2678 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
2679 blk_queue_merge_bvec(q, drbd_merge_bvec);
a6b32bc3 2680 q->queue_lock = &first_peer_device(device)->connection->req_lock; /* needed since we use */
b411b363 2681
b30ab791
AG
2682 device->md_io_page = alloc_page(GFP_KERNEL);
2683 if (!device->md_io_page)
b411b363
PR
2684 goto out_no_io_page;
2685
b30ab791 2686 if (drbd_bm_init(device))
b411b363 2687 goto out_no_bitmap;
b30ab791
AG
2688 device->read_requests = RB_ROOT;
2689 device->write_requests = RB_ROOT;
b411b363 2690
b30ab791 2691 minor_got = idr_alloc(&minors, device, minor, minor + 1, GFP_KERNEL);
56de2102
TH
2692 if (minor_got < 0) {
2693 if (minor_got == -ENOSPC) {
2694 err = ERR_MINOR_EXISTS;
2695 drbd_msg_put_info("requested minor exists already");
2696 }
8432b314 2697 goto out_no_minor_idr;
81a5d60e 2698 }
8432b314 2699
bde89a9e 2700 vnr_got = idr_alloc(&connection->volumes, device, vnr, vnr + 1, GFP_KERNEL);
56de2102
TH
2701 if (vnr_got < 0) {
2702 if (vnr_got == -ENOSPC) {
2703 err = ERR_INVALID_REQUEST;
2704 drbd_msg_put_info("requested volume exists already");
2705 }
8432b314 2706 goto out_idr_remove_minor;
8432b314 2707 }
56de2102 2708
b30ab791 2709 if (init_submitter(device)) {
113fef9e
LE
2710 err = ERR_NOMEM;
2711 drbd_msg_put_info("unable to create submit workqueue");
2712 goto out_idr_remove_vol;
2713 }
2714
774b3055 2715 add_disk(disk);
b30ab791 2716 kref_init(&device->kref); /* one ref for both idrs and the the add_disk */
774b3055 2717
2325eb66 2718 /* inherit the connection state */
bde89a9e 2719 device->state.conn = connection->cstate;
b30ab791
AG
2720 if (device->state.conn == C_WF_REPORT_PARAMS)
2721 drbd_connected(device);
2325eb66 2722
774b3055 2723 return NO_ERROR;
b411b363 2724
113fef9e 2725out_idr_remove_vol:
bde89a9e 2726 idr_remove(&connection->volumes, vnr_got);
8432b314
LE
2727out_idr_remove_minor:
2728 idr_remove(&minors, minor_got);
569083c0 2729 synchronize_rcu();
8432b314 2730out_no_minor_idr:
b30ab791 2731 drbd_bm_cleanup(device);
b411b363 2732out_no_bitmap:
b30ab791 2733 __free_page(device->md_io_page);
b411b363
PR
2734out_no_io_page:
2735 put_disk(disk);
2736out_no_disk:
2737 blk_cleanup_queue(q);
2738out_no_q:
bde89a9e 2739 kref_put(&connection->kref, &conn_destroy);
a6b32bc3
AG
2740out_no_peer_device:
2741 kfree(device);
8432b314 2742 return err;
b411b363
PR
2743}
2744
b411b363
PR
2745int __init drbd_init(void)
2746{
2747 int err;
2748
2b8a90b5 2749 if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
b411b363 2750 printk(KERN_ERR
81a5d60e 2751 "drbd: invalid minor_count (%d)\n", minor_count);
b411b363
PR
2752#ifdef MODULE
2753 return -EINVAL;
2754#else
46530e85 2755 minor_count = DRBD_MINOR_COUNT_DEF;
b411b363
PR
2756#endif
2757 }
2758
b411b363
PR
2759 err = register_blkdev(DRBD_MAJOR, "drbd");
2760 if (err) {
2761 printk(KERN_ERR
2762 "drbd: unable to register block device major %d\n",
2763 DRBD_MAJOR);
2764 return err;
2765 }
2766
2767 register_reboot_notifier(&drbd_notifier);
2768
2769 /*
2770 * allocate all necessary structs
2771 */
b411b363
PR
2772 init_waitqueue_head(&drbd_pp_wait);
2773
2774 drbd_proc = NULL; /* play safe for drbd_cleanup */
81a5d60e 2775 idr_init(&minors);
b411b363 2776
69babf05 2777 rwlock_init(&global_state_lock);
bde89a9e 2778 INIT_LIST_HEAD(&drbd_connections);
69babf05
LE
2779
2780 err = drbd_genl_register();
2781 if (err) {
2782 printk(KERN_ERR "drbd: unable to register generic netlink family\n");
2783 goto fail;
2784 }
2785
b411b363
PR
2786 err = drbd_create_mempools();
2787 if (err)
3b98c0c2 2788 goto fail;
b411b363 2789
6110d70b 2790 err = -ENOMEM;
8c484ee4 2791 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
b411b363
PR
2792 if (!drbd_proc) {
2793 printk(KERN_ERR "drbd: unable to register proc file\n");
3b98c0c2 2794 goto fail;
b411b363
PR
2795 }
2796
2312f0b3
LE
2797 retry.wq = create_singlethread_workqueue("drbd-reissue");
2798 if (!retry.wq) {
2799 printk(KERN_ERR "drbd: unable to create retry workqueue\n");
2800 goto fail;
2801 }
2802 INIT_WORK(&retry.worker, do_retry);
2803 spin_lock_init(&retry.lock);
2804 INIT_LIST_HEAD(&retry.writes);
b411b363
PR
2805
2806 printk(KERN_INFO "drbd: initialized. "
2807 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
2808 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
2809 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
2810 printk(KERN_INFO "drbd: registered as block device major %d\n",
2811 DRBD_MAJOR);
b411b363
PR
2812
2813 return 0; /* Success! */
2814
3b98c0c2 2815fail:
b411b363
PR
2816 drbd_cleanup();
2817 if (err == -ENOMEM)
b411b363
PR
2818 printk(KERN_ERR "drbd: ran out of memory\n");
2819 else
2820 printk(KERN_ERR "drbd: initialization failure\n");
2821 return err;
2822}
2823
2824void drbd_free_bc(struct drbd_backing_dev *ldev)
2825{
2826 if (ldev == NULL)
2827 return;
2828
e525fd89
TH
2829 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2830 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
b411b363 2831
94ad0a10 2832 kfree(ldev->disk_conf);
b411b363
PR
2833 kfree(ldev);
2834}
2835
bde89a9e 2836void drbd_free_sock(struct drbd_connection *connection)
b411b363 2837{
bde89a9e
AG
2838 if (connection->data.socket) {
2839 mutex_lock(&connection->data.mutex);
2840 kernel_sock_shutdown(connection->data.socket, SHUT_RDWR);
2841 sock_release(connection->data.socket);
2842 connection->data.socket = NULL;
2843 mutex_unlock(&connection->data.mutex);
b411b363 2844 }
bde89a9e
AG
2845 if (connection->meta.socket) {
2846 mutex_lock(&connection->meta.mutex);
2847 kernel_sock_shutdown(connection->meta.socket, SHUT_RDWR);
2848 sock_release(connection->meta.socket);
2849 connection->meta.socket = NULL;
2850 mutex_unlock(&connection->meta.mutex);
b411b363
PR
2851 }
2852}
2853
b411b363 2854/* meta data management */
b411b363 2855
bde89a9e 2856void conn_md_sync(struct drbd_connection *connection)
b411b363 2857{
b30ab791 2858 struct drbd_device *device;
19fffd7b 2859 int vnr;
b411b363 2860
19fffd7b 2861 rcu_read_lock();
bde89a9e 2862 idr_for_each_entry(&connection->volumes, device, vnr) {
b30ab791 2863 kref_get(&device->kref);
19fffd7b 2864 rcu_read_unlock();
b30ab791
AG
2865 drbd_md_sync(device);
2866 kref_put(&device->kref, &drbd_minor_destroy);
19fffd7b
PR
2867 rcu_read_lock();
2868 }
2869 rcu_read_unlock();
b411b363
PR
2870}
2871
ae8bf312 2872/* aligned 4kByte */
b411b363 2873struct meta_data_on_disk {
cccac985 2874 u64 la_size_sect; /* last agreed size. */
b411b363
PR
2875 u64 uuid[UI_SIZE]; /* UUIDs. */
2876 u64 device_uuid;
2877 u64 reserved_u64_1;
2878 u32 flags; /* MDF */
2879 u32 magic;
2880 u32 md_size_sect;
2881 u32 al_offset; /* offset to this block */
ae8bf312 2882 u32 al_nr_extents; /* important for restoring the AL (userspace) */
f399002e 2883 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
b411b363
PR
2884 u32 bm_offset; /* offset to the bitmap, from here */
2885 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
99432fcc 2886 u32 la_peer_max_bio_size; /* last peer max_bio_size */
b411b363 2887
3a4d4eb3
LE
2888 /* see al_tr_number_to_on_disk_sector() */
2889 u32 al_stripes;
2890 u32 al_stripe_size_4k;
2891
2892 u8 reserved_u8[4096 - (7*8 + 10*4)];
b411b363
PR
2893} __packed;
2894
d752b269
PR
2895
2896
b30ab791 2897void drbd_md_write(struct drbd_device *device, void *b)
b411b363 2898{
d752b269 2899 struct meta_data_on_disk *buffer = b;
b411b363
PR
2900 sector_t sector;
2901 int i;
2902
ae8bf312 2903 memset(buffer, 0, sizeof(*buffer));
b411b363 2904
b30ab791 2905 buffer->la_size_sect = cpu_to_be64(drbd_get_capacity(device->this_bdev));
b411b363 2906 for (i = UI_CURRENT; i < UI_SIZE; i++)
b30ab791
AG
2907 buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
2908 buffer->flags = cpu_to_be32(device->ldev->md.flags);
d5d7ebd4 2909 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
b411b363 2910
b30ab791
AG
2911 buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect);
2912 buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset);
2913 buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
b411b363 2914 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
b30ab791 2915 buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
b411b363 2916
b30ab791
AG
2917 buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
2918 buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
b411b363 2919
b30ab791
AG
2920 buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
2921 buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
3a4d4eb3 2922
b30ab791
AG
2923 D_ASSERT(drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
2924 sector = device->ldev->md.md_offset;
b411b363 2925
b30ab791 2926 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) {
b411b363
PR
2927 /* this was a try anyways ... */
2928 dev_err(DEV, "meta data update failed!\n");
b30ab791 2929 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
b411b363 2930 }
d752b269
PR
2931}
2932
2933/**
2934 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
b30ab791 2935 * @device: DRBD device.
d752b269 2936 */
b30ab791 2937void drbd_md_sync(struct drbd_device *device)
d752b269
PR
2938{
2939 struct meta_data_on_disk *buffer;
2940
2941 /* Don't accidentally change the DRBD meta data layout. */
2942 BUILD_BUG_ON(UI_SIZE != 4);
2943 BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
2944
b30ab791 2945 del_timer(&device->md_sync_timer);
d752b269 2946 /* timer may be rearmed by drbd_md_mark_dirty() now. */
b30ab791 2947 if (!test_and_clear_bit(MD_DIRTY, &device->flags))
d752b269
PR
2948 return;
2949
2950 /* We use here D_FAILED and not D_ATTACHING because we try to write
2951 * metadata even if we detach due to a disk failure! */
b30ab791 2952 if (!get_ldev_if_state(device, D_FAILED))
d752b269
PR
2953 return;
2954
b30ab791 2955 buffer = drbd_md_get_buffer(device);
d752b269
PR
2956 if (!buffer)
2957 goto out;
2958
b30ab791 2959 drbd_md_write(device, buffer);
b411b363 2960
b30ab791 2961 /* Update device->ldev->md.la_size_sect,
b411b363 2962 * since we updated it on metadata. */
b30ab791 2963 device->ldev->md.la_size_sect = drbd_get_capacity(device->this_bdev);
b411b363 2964
b30ab791 2965 drbd_md_put_buffer(device);
e1711731 2966out:
b30ab791 2967 put_ldev(device);
b411b363
PR
2968}
2969
b30ab791 2970static int check_activity_log_stripe_size(struct drbd_device *device,
3a4d4eb3
LE
2971 struct meta_data_on_disk *on_disk,
2972 struct drbd_md *in_core)
2973{
2974 u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
2975 u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
2976 u64 al_size_4k;
2977
2978 /* both not set: default to old fixed size activity log */
2979 if (al_stripes == 0 && al_stripe_size_4k == 0) {
2980 al_stripes = 1;
2981 al_stripe_size_4k = MD_32kB_SECT/8;
2982 }
2983
2984 /* some paranoia plausibility checks */
2985
2986 /* we need both values to be set */
2987 if (al_stripes == 0 || al_stripe_size_4k == 0)
2988 goto err;
2989
2990 al_size_4k = (u64)al_stripes * al_stripe_size_4k;
2991
2992 /* Upper limit of activity log area, to avoid potential overflow
2993 * problems in al_tr_number_to_on_disk_sector(). As right now, more
2994 * than 72 * 4k blocks total only increases the amount of history,
2995 * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
2996 if (al_size_4k > (16 * 1024 * 1024/4))
2997 goto err;
2998
2999 /* Lower limit: we need at least 8 transaction slots (32kB)
3000 * to not break existing setups */
3001 if (al_size_4k < MD_32kB_SECT/8)
3002 goto err;
3003
3004 in_core->al_stripe_size_4k = al_stripe_size_4k;
3005 in_core->al_stripes = al_stripes;
3006 in_core->al_size_4k = al_size_4k;
3007
3008 return 0;
3009err:
3010 dev_err(DEV, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
3011 al_stripes, al_stripe_size_4k);
3012 return -EINVAL;
3013}
3014
b30ab791 3015static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
c04ccaa6
LE
3016{
3017 sector_t capacity = drbd_get_capacity(bdev->md_bdev);
3018 struct drbd_md *in_core = &bdev->md;
3019 s32 on_disk_al_sect;
3020 s32 on_disk_bm_sect;
3021
3022 /* The on-disk size of the activity log, calculated from offsets, and
3023 * the size of the activity log calculated from the stripe settings,
3024 * should match.
3025 * Though we could relax this a bit: it is ok, if the striped activity log
3026 * fits in the available on-disk activity log size.
3027 * Right now, that would break how resize is implemented.
3028 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
3029 * of possible unused padding space in the on disk layout. */
3030 if (in_core->al_offset < 0) {
3031 if (in_core->bm_offset > in_core->al_offset)
3032 goto err;
3033 on_disk_al_sect = -in_core->al_offset;
3034 on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
3035 } else {
3036 if (in_core->al_offset != MD_4kB_SECT)
3037 goto err;
3038 if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
3039 goto err;
3040
3041 on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
3042 on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
3043 }
3044
3045 /* old fixed size meta data is exactly that: fixed. */
3046 if (in_core->meta_dev_idx >= 0) {
3047 if (in_core->md_size_sect != MD_128MB_SECT
3048 || in_core->al_offset != MD_4kB_SECT
3049 || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
3050 || in_core->al_stripes != 1
3051 || in_core->al_stripe_size_4k != MD_32kB_SECT/8)
3052 goto err;
3053 }
3054
3055 if (capacity < in_core->md_size_sect)
3056 goto err;
3057 if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
3058 goto err;
3059
3060 /* should be aligned, and at least 32k */
3061 if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
3062 goto err;
3063
3064 /* should fit (for now: exactly) into the available on-disk space;
3065 * overflow prevention is in check_activity_log_stripe_size() above. */
3066 if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
3067 goto err;
3068
3069 /* again, should be aligned */
3070 if (in_core->bm_offset & 7)
3071 goto err;
3072
3073 /* FIXME check for device grow with flex external meta data? */
3074
3075 /* can the available bitmap space cover the last agreed device size? */
3076 if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
3077 goto err;
3078
3079 return 0;
3080
3081err:
3082 dev_err(DEV, "meta data offsets don't make sense: idx=%d "
3083 "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
3084 "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
3085 in_core->meta_dev_idx,
3086 in_core->al_stripes, in_core->al_stripe_size_4k,
3087 in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
3088 (unsigned long long)in_core->la_size_sect,
3089 (unsigned long long)capacity);
3090
3091 return -EINVAL;
3092}
3093
3094
b411b363
PR
3095/**
3096 * drbd_md_read() - Reads in the meta data super block
b30ab791 3097 * @device: DRBD device.
b411b363
PR
3098 * @bdev: Device from which the meta data should be read in.
3099 *
3a4d4eb3 3100 * Return NO_ERROR on success, and an enum drbd_ret_code in case
d5d7ebd4 3101 * something goes wrong.
3a4d4eb3 3102 *
c04ccaa6 3103 * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
b30ab791 3104 * even before @bdev is assigned to @device->ldev.
b411b363 3105 */
b30ab791 3106int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
b411b363
PR
3107{
3108 struct meta_data_on_disk *buffer;
d5d7ebd4 3109 u32 magic, flags;
b411b363
PR
3110 int i, rv = NO_ERROR;
3111
b30ab791 3112 if (device->state.disk != D_DISKLESS)
c04ccaa6 3113 return ERR_DISK_CONFIGURED;
b411b363 3114
b30ab791 3115 buffer = drbd_md_get_buffer(device);
e1711731 3116 if (!buffer)
c04ccaa6 3117 return ERR_NOMEM;
b411b363 3118
c04ccaa6
LE
3119 /* First, figure out where our meta data superblock is located,
3120 * and read it. */
3a4d4eb3
LE
3121 bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
3122 bdev->md.md_offset = drbd_md_ss(bdev);
b411b363 3123
b30ab791 3124 if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset, READ)) {
25985edc 3125 /* NOTE: can't do normal error processing here as this is
b411b363
PR
3126 called BEFORE disk is attached */
3127 dev_err(DEV, "Error while reading metadata.\n");
3128 rv = ERR_IO_MD_DISK;
3129 goto err;
3130 }
3131
d5d7ebd4
LE
3132 magic = be32_to_cpu(buffer->magic);
3133 flags = be32_to_cpu(buffer->flags);
3134 if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
3135 (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
3136 /* btw: that's Activity Log clean, not "all" clean. */
3137 dev_err(DEV, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
3138 rv = ERR_MD_UNCLEAN;
3139 goto err;
3140 }
3a4d4eb3
LE
3141
3142 rv = ERR_MD_INVALID;
d5d7ebd4 3143 if (magic != DRBD_MD_MAGIC_08) {
43de7c85 3144 if (magic == DRBD_MD_MAGIC_07)
d5d7ebd4
LE
3145 dev_err(DEV, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
3146 else
3147 dev_err(DEV, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
b411b363
PR
3148 goto err;
3149 }
3a4d4eb3 3150
c04ccaa6
LE
3151 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
3152 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3153 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
b411b363
PR
3154 goto err;
3155 }
3a4d4eb3 3156
c04ccaa6
LE
3157
3158 /* convert to in_core endian */
3159 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
3160 for (i = UI_CURRENT; i < UI_SIZE; i++)
3161 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3162 bdev->md.flags = be32_to_cpu(buffer->flags);
3163 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3164
3165 bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
3166 bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
3167 bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
3168
b30ab791 3169 if (check_activity_log_stripe_size(device, buffer, &bdev->md))
b411b363 3170 goto err;
b30ab791 3171 if (check_offsets_and_sizes(device, bdev))
c04ccaa6
LE
3172 goto err;
3173
b411b363
PR
3174 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
3175 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
3176 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
b411b363
PR
3177 goto err;
3178 }
3179 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
3180 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
3181 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
b411b363
PR
3182 goto err;
3183 }
3184
3a4d4eb3 3185 rv = NO_ERROR;
b411b363 3186
a6b32bc3 3187 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 3188 if (device->state.conn < C_CONNECTED) {
db141b2f 3189 unsigned int peer;
99432fcc 3190 peer = be32_to_cpu(buffer->la_peer_max_bio_size);
db141b2f 3191 peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
b30ab791 3192 device->peer_max_bio_size = peer;
99432fcc 3193 }
a6b32bc3 3194 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
3195
3196 err:
b30ab791 3197 drbd_md_put_buffer(device);
b411b363
PR
3198
3199 return rv;
3200}
3201
3202/**
3203 * drbd_md_mark_dirty() - Mark meta data super block as dirty
b30ab791 3204 * @device: DRBD device.
b411b363
PR
3205 *
3206 * Call this function if you change anything that should be written to
3207 * the meta-data super block. This function sets MD_DIRTY, and starts a
3208 * timer that ensures that within five seconds you have to call drbd_md_sync().
3209 */
ca0e6098 3210#ifdef DEBUG
b30ab791 3211void drbd_md_mark_dirty_(struct drbd_device *device, unsigned int line, const char *func)
ee15b038 3212{
b30ab791
AG
3213 if (!test_and_set_bit(MD_DIRTY, &device->flags)) {
3214 mod_timer(&device->md_sync_timer, jiffies + HZ);
3215 device->last_md_mark_dirty.line = line;
3216 device->last_md_mark_dirty.func = func;
ee15b038
LE
3217 }
3218}
3219#else
b30ab791 3220void drbd_md_mark_dirty(struct drbd_device *device)
b411b363 3221{
b30ab791
AG
3222 if (!test_and_set_bit(MD_DIRTY, &device->flags))
3223 mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
b411b363 3224}
ee15b038 3225#endif
b411b363 3226
b30ab791 3227void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
b411b363
PR
3228{
3229 int i;
3230
62b0da3a 3231 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
b30ab791 3232 device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
b411b363
PR
3233}
3234
b30ab791 3235void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
b411b363
PR
3236{
3237 if (idx == UI_CURRENT) {
b30ab791 3238 if (device->state.role == R_PRIMARY)
b411b363
PR
3239 val |= 1;
3240 else
3241 val &= ~((u64)1);
3242
b30ab791 3243 drbd_set_ed_uuid(device, val);
b411b363
PR
3244 }
3245
b30ab791
AG
3246 device->ldev->md.uuid[idx] = val;
3247 drbd_md_mark_dirty(device);
b411b363
PR
3248}
3249
b30ab791 3250void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
9f2247bb
PR
3251{
3252 unsigned long flags;
b30ab791
AG
3253 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3254 __drbd_uuid_set(device, idx, val);
3255 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
9f2247bb 3256}
b411b363 3257
b30ab791 3258void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
b411b363 3259{
9f2247bb 3260 unsigned long flags;
b30ab791
AG
3261 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
3262 if (device->ldev->md.uuid[idx]) {
3263 drbd_uuid_move_history(device);
3264 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
b411b363 3265 }
b30ab791
AG
3266 __drbd_uuid_set(device, idx, val);
3267 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
b411b363
PR
3268}
3269
3270/**
3271 * drbd_uuid_new_current() - Creates a new current UUID
b30ab791 3272 * @device: DRBD device.
b411b363
PR
3273 *
3274 * Creates a new current UUID, and rotates the old current UUID into
3275 * the bitmap slot. Causes an incremental resync upon next connect.
3276 */
b30ab791 3277void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
b411b363
PR
3278{
3279 u64 val;
9f2247bb
PR
3280 unsigned long long bm_uuid;
3281
3282 get_random_bytes(&val, sizeof(u64));
3283
b30ab791
AG
3284 spin_lock_irq(&device->ldev->md.uuid_lock);
3285 bm_uuid = device->ldev->md.uuid[UI_BITMAP];
62b0da3a
LE
3286
3287 if (bm_uuid)
3288 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
b411b363 3289
b30ab791
AG
3290 device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
3291 __drbd_uuid_set(device, UI_CURRENT, val);
3292 spin_unlock_irq(&device->ldev->md.uuid_lock);
b411b363 3293
b30ab791 3294 drbd_print_uuids(device, "new current UUID");
aaa8e2b3 3295 /* get it to stable storage _now_ */
b30ab791 3296 drbd_md_sync(device);
b411b363
PR
3297}
3298
b30ab791 3299void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
b411b363 3300{
9f2247bb 3301 unsigned long flags;
b30ab791 3302 if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
b411b363
PR
3303 return;
3304
b30ab791 3305 spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
b411b363 3306 if (val == 0) {
b30ab791
AG
3307 drbd_uuid_move_history(device);
3308 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
3309 device->ldev->md.uuid[UI_BITMAP] = 0;
b411b363 3310 } else {
b30ab791 3311 unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
62b0da3a
LE
3312 if (bm_uuid)
3313 dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
b411b363 3314
b30ab791 3315 device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
b411b363 3316 }
b30ab791 3317 spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
9f2247bb 3318
b30ab791 3319 drbd_md_mark_dirty(device);
b411b363
PR
3320}
3321
3322/**
3323 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
b30ab791 3324 * @device: DRBD device.
b411b363
PR
3325 *
3326 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3327 */
b30ab791 3328int drbd_bmio_set_n_write(struct drbd_device *device)
b411b363
PR
3329{
3330 int rv = -EIO;
3331
b30ab791
AG
3332 if (get_ldev_if_state(device, D_ATTACHING)) {
3333 drbd_md_set_flag(device, MDF_FULL_SYNC);
3334 drbd_md_sync(device);
3335 drbd_bm_set_all(device);
b411b363 3336
b30ab791 3337 rv = drbd_bm_write(device);
b411b363
PR
3338
3339 if (!rv) {
b30ab791
AG
3340 drbd_md_clear_flag(device, MDF_FULL_SYNC);
3341 drbd_md_sync(device);
b411b363
PR
3342 }
3343
b30ab791 3344 put_ldev(device);
b411b363
PR
3345 }
3346
3347 return rv;
3348}
3349
3350/**
3351 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
b30ab791 3352 * @device: DRBD device.
b411b363
PR
3353 *
3354 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3355 */
b30ab791 3356int drbd_bmio_clear_n_write(struct drbd_device *device)
b411b363
PR
3357{
3358 int rv = -EIO;
3359
b30ab791
AG
3360 drbd_resume_al(device);
3361 if (get_ldev_if_state(device, D_ATTACHING)) {
3362 drbd_bm_clear_all(device);
3363 rv = drbd_bm_write(device);
3364 put_ldev(device);
b411b363
PR
3365 }
3366
3367 return rv;
3368}
3369
99920dc5 3370static int w_bitmap_io(struct drbd_work *w, int unused)
b411b363
PR
3371{
3372 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
b30ab791 3373 struct drbd_device *device = w->device;
02851e9f 3374 int rv = -EIO;
b411b363 3375
b30ab791 3376 D_ASSERT(atomic_read(&device->ap_bio_cnt) == 0);
b411b363 3377
b30ab791
AG
3378 if (get_ldev(device)) {
3379 drbd_bm_lock(device, work->why, work->flags);
3380 rv = work->io_fn(device);
3381 drbd_bm_unlock(device);
3382 put_ldev(device);
02851e9f 3383 }
b411b363 3384
b30ab791
AG
3385 clear_bit_unlock(BITMAP_IO, &device->flags);
3386 wake_up(&device->misc_wait);
b411b363
PR
3387
3388 if (work->done)
b30ab791 3389 work->done(device, rv);
b411b363 3390
b30ab791 3391 clear_bit(BITMAP_IO_QUEUED, &device->flags);
b411b363 3392 work->why = NULL;
20ceb2b2 3393 work->flags = 0;
b411b363 3394
99920dc5 3395 return 0;
b411b363
PR
3396}
3397
b30ab791 3398void drbd_ldev_destroy(struct drbd_device *device)
82f59cc6 3399{
b30ab791
AG
3400 lc_destroy(device->resync);
3401 device->resync = NULL;
3402 lc_destroy(device->act_log);
3403 device->act_log = NULL;
82f59cc6 3404 __no_warn(local,
b30ab791
AG
3405 drbd_free_bc(device->ldev);
3406 device->ldev = NULL;);
82f59cc6 3407
b30ab791 3408 clear_bit(GO_DISKLESS, &device->flags);
82f59cc6
LE
3409}
3410
99920dc5 3411static int w_go_diskless(struct drbd_work *w, int unused)
e9e6f3ec 3412{
b30ab791 3413 struct drbd_device *device = w->device;
00d56944 3414
b30ab791 3415 D_ASSERT(device->state.disk == D_FAILED);
9d282875
LE
3416 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
3417 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
82f59cc6
LE
3418 * the protected members anymore, though, so once put_ldev reaches zero
3419 * again, it will be safe to free them. */
a2a3c74f
LE
3420
3421 /* Try to write changed bitmap pages, read errors may have just
3422 * set some bits outside the area covered by the activity log.
3423 *
3424 * If we have an IO error during the bitmap writeout,
3425 * we will want a full sync next time, just in case.
3426 * (Do we want a specific meta data flag for this?)
3427 *
3428 * If that does not make it to stable storage either,
fd0017c1
PR
3429 * we cannot do anything about that anymore.
3430 *
3431 * We still need to check if both bitmap and ldev are present, we may
3432 * end up here after a failed attach, before ldev was even assigned.
3433 */
b30ab791 3434 if (device->bitmap && device->ldev) {
bb45185d
PR
3435 /* An interrupted resync or similar is allowed to recounts bits
3436 * while we detach.
3437 * Any modifications would not be expected anymore, though.
3438 */
b30ab791 3439 if (drbd_bitmap_io_from_worker(device, drbd_bm_write,
bb45185d 3440 "detach", BM_LOCKED_TEST_ALLOWED)) {
b30ab791
AG
3441 if (test_bit(WAS_READ_ERROR, &device->flags)) {
3442 drbd_md_set_flag(device, MDF_FULL_SYNC);
3443 drbd_md_sync(device);
a2a3c74f
LE
3444 }
3445 }
3446 }
3447
b30ab791 3448 drbd_force_state(device, NS(disk, D_DISKLESS));
99920dc5 3449 return 0;
e9e6f3ec
LE
3450}
3451
b411b363
PR
3452/**
3453 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
b30ab791 3454 * @device: DRBD device.
b411b363
PR
3455 * @io_fn: IO callback to be called when bitmap IO is possible
3456 * @done: callback to be called after the bitmap IO was performed
3457 * @why: Descriptive text of the reason for doing the IO
3458 *
3459 * While IO on the bitmap happens we freeze application IO thus we ensure
3460 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3461 * called from worker context. It MUST NOT be used while a previous such
3462 * work is still pending!
3463 */
b30ab791 3464void drbd_queue_bitmap_io(struct drbd_device *device,
54761697
AG
3465 int (*io_fn)(struct drbd_device *),
3466 void (*done)(struct drbd_device *, int),
20ceb2b2 3467 char *why, enum bm_flag flags)
b411b363 3468{
a6b32bc3 3469 D_ASSERT(current == first_peer_device(device)->connection->worker.task);
b411b363 3470
b30ab791
AG
3471 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &device->flags));
3472 D_ASSERT(!test_bit(BITMAP_IO, &device->flags));
3473 D_ASSERT(list_empty(&device->bm_io_work.w.list));
3474 if (device->bm_io_work.why)
b411b363 3475 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
b30ab791 3476 why, device->bm_io_work.why);
b411b363 3477
b30ab791
AG
3478 device->bm_io_work.io_fn = io_fn;
3479 device->bm_io_work.done = done;
3480 device->bm_io_work.why = why;
3481 device->bm_io_work.flags = flags;
b411b363 3482
a6b32bc3 3483 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791
AG
3484 set_bit(BITMAP_IO, &device->flags);
3485 if (atomic_read(&device->ap_bio_cnt) == 0) {
3486 if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
a6b32bc3 3487 drbd_queue_work(&first_peer_device(device)->connection->sender_work, &device->bm_io_work.w);
b411b363 3488 }
a6b32bc3 3489 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
b411b363
PR
3490}
3491
3492/**
3493 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
b30ab791 3494 * @device: DRBD device.
b411b363
PR
3495 * @io_fn: IO callback to be called when bitmap IO is possible
3496 * @why: Descriptive text of the reason for doing the IO
3497 *
3498 * freezes application IO while that the actual IO operations runs. This
3499 * functions MAY NOT be called from worker context.
3500 */
b30ab791 3501int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *),
20ceb2b2 3502 char *why, enum bm_flag flags)
b411b363
PR
3503{
3504 int rv;
3505
a6b32bc3 3506 D_ASSERT(current != first_peer_device(device)->connection->worker.task);
b411b363 3507
20ceb2b2 3508 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
b30ab791 3509 drbd_suspend_io(device);
b411b363 3510
b30ab791
AG
3511 drbd_bm_lock(device, why, flags);
3512 rv = io_fn(device);
3513 drbd_bm_unlock(device);
b411b363 3514
20ceb2b2 3515 if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
b30ab791 3516 drbd_resume_io(device);
b411b363
PR
3517
3518 return rv;
3519}
3520
b30ab791 3521void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
b411b363 3522{
b30ab791
AG
3523 if ((device->ldev->md.flags & flag) != flag) {
3524 drbd_md_mark_dirty(device);
3525 device->ldev->md.flags |= flag;
b411b363
PR
3526 }
3527}
3528
b30ab791 3529void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
b411b363 3530{
b30ab791
AG
3531 if ((device->ldev->md.flags & flag) != 0) {
3532 drbd_md_mark_dirty(device);
3533 device->ldev->md.flags &= ~flag;
b411b363
PR
3534 }
3535}
3536int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3537{
3538 return (bdev->md.flags & flag) != 0;
3539}
3540
3541static void md_sync_timer_fn(unsigned long data)
3542{
b30ab791 3543 struct drbd_device *device = (struct drbd_device *) data;
b411b363 3544
b792b655 3545 /* must not double-queue! */
b30ab791 3546 if (list_empty(&device->md_sync_work.list))
a6b32bc3 3547 drbd_queue_work_front(&first_peer_device(device)->connection->sender_work, &device->md_sync_work);
b411b363
PR
3548}
3549
99920dc5 3550static int w_md_sync(struct drbd_work *w, int unused)
b411b363 3551{
b30ab791 3552 struct drbd_device *device = w->device;
00d56944 3553
b411b363 3554 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
ee15b038
LE
3555#ifdef DEBUG
3556 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
b30ab791 3557 device->last_md_mark_dirty.func, device->last_md_mark_dirty.line);
ee15b038 3558#endif
b30ab791 3559 drbd_md_sync(device);
99920dc5 3560 return 0;
b411b363
PR
3561}
3562
d8763023 3563const char *cmdname(enum drbd_packet cmd)
f2ad9063
AG
3564{
3565 /* THINK may need to become several global tables
3566 * when we want to support more than
3567 * one PRO_VERSION */
3568 static const char *cmdnames[] = {
3569 [P_DATA] = "Data",
3570 [P_DATA_REPLY] = "DataReply",
3571 [P_RS_DATA_REPLY] = "RSDataReply",
3572 [P_BARRIER] = "Barrier",
3573 [P_BITMAP] = "ReportBitMap",
3574 [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
3575 [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
3576 [P_UNPLUG_REMOTE] = "UnplugRemote",
3577 [P_DATA_REQUEST] = "DataRequest",
3578 [P_RS_DATA_REQUEST] = "RSDataRequest",
3579 [P_SYNC_PARAM] = "SyncParam",
3580 [P_SYNC_PARAM89] = "SyncParam89",
3581 [P_PROTOCOL] = "ReportProtocol",
3582 [P_UUIDS] = "ReportUUIDs",
3583 [P_SIZES] = "ReportSizes",
3584 [P_STATE] = "ReportState",
3585 [P_SYNC_UUID] = "ReportSyncUUID",
3586 [P_AUTH_CHALLENGE] = "AuthChallenge",
3587 [P_AUTH_RESPONSE] = "AuthResponse",
3588 [P_PING] = "Ping",
3589 [P_PING_ACK] = "PingAck",
3590 [P_RECV_ACK] = "RecvAck",
3591 [P_WRITE_ACK] = "WriteAck",
3592 [P_RS_WRITE_ACK] = "RSWriteAck",
d4dabbe2 3593 [P_SUPERSEDED] = "Superseded",
f2ad9063
AG
3594 [P_NEG_ACK] = "NegAck",
3595 [P_NEG_DREPLY] = "NegDReply",
3596 [P_NEG_RS_DREPLY] = "NegRSDReply",
3597 [P_BARRIER_ACK] = "BarrierAck",
3598 [P_STATE_CHG_REQ] = "StateChgRequest",
3599 [P_STATE_CHG_REPLY] = "StateChgReply",
3600 [P_OV_REQUEST] = "OVRequest",
3601 [P_OV_REPLY] = "OVReply",
3602 [P_OV_RESULT] = "OVResult",
3603 [P_CSUM_RS_REQUEST] = "CsumRSRequest",
3604 [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
3605 [P_COMPRESSED_BITMAP] = "CBitmap",
3606 [P_DELAY_PROBE] = "DelayProbe",
3607 [P_OUT_OF_SYNC] = "OutOfSync",
7be8da07 3608 [P_RETRY_WRITE] = "RetryWrite",
ae25b336
LE
3609 [P_RS_CANCEL] = "RSCancel",
3610 [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
3611 [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
036b17ea
PR
3612 [P_RETRY_WRITE] = "retry_write",
3613 [P_PROTOCOL_UPDATE] = "protocol_update",
ae25b336
LE
3614
3615 /* enum drbd_packet, but not commands - obsoleted flags:
3616 * P_MAY_IGNORE
3617 * P_MAX_OPT_CMD
3618 */
f2ad9063
AG
3619 };
3620
ae25b336 3621 /* too big for the array: 0xfffX */
e5d6f33a
AG
3622 if (cmd == P_INITIAL_META)
3623 return "InitialMeta";
3624 if (cmd == P_INITIAL_DATA)
3625 return "InitialData";
6038178e
AG
3626 if (cmd == P_CONNECTION_FEATURES)
3627 return "ConnectionFeatures";
6e849ce8 3628 if (cmd >= ARRAY_SIZE(cmdnames))
f2ad9063
AG
3629 return "Unknown";
3630 return cmdnames[cmd];
3631}
3632
7be8da07
AG
3633/**
3634 * drbd_wait_misc - wait for a request to make progress
b30ab791 3635 * @device: device associated with the request
7be8da07
AG
3636 * @i: the struct drbd_interval embedded in struct drbd_request or
3637 * struct drbd_peer_request
3638 */
b30ab791 3639int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
7be8da07 3640{
44ed167d 3641 struct net_conf *nc;
7be8da07
AG
3642 DEFINE_WAIT(wait);
3643 long timeout;
3644
44ed167d 3645 rcu_read_lock();
a6b32bc3 3646 nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
44ed167d
PR
3647 if (!nc) {
3648 rcu_read_unlock();
7be8da07 3649 return -ETIMEDOUT;
44ed167d
PR
3650 }
3651 timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
3652 rcu_read_unlock();
7be8da07 3653
b30ab791 3654 /* Indicate to wake up device->misc_wait on progress. */
7be8da07 3655 i->waiting = true;
b30ab791 3656 prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
a6b32bc3 3657 spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
7be8da07 3658 timeout = schedule_timeout(timeout);
b30ab791 3659 finish_wait(&device->misc_wait, &wait);
a6b32bc3 3660 spin_lock_irq(&first_peer_device(device)->connection->req_lock);
b30ab791 3661 if (!timeout || device->state.conn < C_CONNECTED)
7be8da07
AG
3662 return -ETIMEDOUT;
3663 if (signal_pending(current))
3664 return -ERESTARTSYS;
3665 return 0;
b411b363
PR
3666}
3667
3668#ifdef CONFIG_DRBD_FAULT_INJECTION
3669/* Fault insertion support including random number generator shamelessly
3670 * stolen from kernel/rcutorture.c */
3671struct fault_random_state {
3672 unsigned long state;
3673 unsigned long count;
3674};
3675
3676#define FAULT_RANDOM_MULT 39916801 /* prime */
3677#define FAULT_RANDOM_ADD 479001701 /* prime */
3678#define FAULT_RANDOM_REFRESH 10000
3679
3680/*
3681 * Crude but fast random-number generator. Uses a linear congruential
3682 * generator, with occasional help from get_random_bytes().
3683 */
3684static unsigned long
3685_drbd_fault_random(struct fault_random_state *rsp)
3686{
3687 long refresh;
3688
49829ea7 3689 if (!rsp->count--) {
b411b363
PR
3690 get_random_bytes(&refresh, sizeof(refresh));
3691 rsp->state += refresh;
3692 rsp->count = FAULT_RANDOM_REFRESH;
3693 }
3694 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3695 return swahw32(rsp->state);
3696}
3697
3698static char *
3699_drbd_fault_str(unsigned int type) {
3700 static char *_faults[] = {
3701 [DRBD_FAULT_MD_WR] = "Meta-data write",
3702 [DRBD_FAULT_MD_RD] = "Meta-data read",
3703 [DRBD_FAULT_RS_WR] = "Resync write",
3704 [DRBD_FAULT_RS_RD] = "Resync read",
3705 [DRBD_FAULT_DT_WR] = "Data write",
3706 [DRBD_FAULT_DT_RD] = "Data read",
3707 [DRBD_FAULT_DT_RA] = "Data read ahead",
3708 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
6b4388ac
PR
3709 [DRBD_FAULT_AL_EE] = "EE allocation",
3710 [DRBD_FAULT_RECEIVE] = "receive data corruption",
b411b363
PR
3711 };
3712
3713 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3714}
3715
3716unsigned int
b30ab791 3717_drbd_insert_fault(struct drbd_device *device, unsigned int type)
b411b363
PR
3718{
3719 static struct fault_random_state rrs = {0, 0};
3720
3721 unsigned int ret = (
3722 (fault_devs == 0 ||
b30ab791 3723 ((1 << device_to_minor(device)) & fault_devs) != 0) &&
b411b363
PR
3724 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3725
3726 if (ret) {
3727 fault_count++;
3728
7383506c 3729 if (__ratelimit(&drbd_ratelimit_state))
b411b363
PR
3730 dev_warn(DEV, "***Simulating %s failure\n",
3731 _drbd_fault_str(type));
3732 }
3733
3734 return ret;
3735}
3736#endif
3737
3738const char *drbd_buildtag(void)
3739{
3740 /* DRBD built from external sources has here a reference to the
3741 git hash of the source code. */
3742
3743 static char buildtag[38] = "\0uilt-in";
3744
3745 if (buildtag[0] == 0) {
bc4854bc
CW
3746#ifdef MODULE
3747 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3748#else
3749 buildtag[0] = 'b';
b411b363 3750#endif
b411b363
PR
3751 }
3752
3753 return buildtag;
3754}
3755
3756module_init(drbd_init)
3757module_exit(drbd_cleanup)
3758
b411b363
PR
3759EXPORT_SYMBOL(drbd_conn_str);
3760EXPORT_SYMBOL(drbd_role_str);
3761EXPORT_SYMBOL(drbd_disk_str);
3762EXPORT_SYMBOL(drbd_set_st_err_str);
This page took 0.850121 seconds and 5 git commands to generate.