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