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