4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
30 #include <linux/drbd.h>
31 #include <asm/uaccess.h>
32 #include <asm/types.h>
34 #include <linux/ctype.h>
35 #include <linux/mutex.h>
37 #include <linux/file.h>
38 #include <linux/proc_fs.h>
39 #include <linux/init.h>
41 #include <linux/memcontrol.h>
42 #include <linux/mm_inline.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/notifier.h>
47 #include <linux/kthread.h>
48 #include <linux/workqueue.h>
49 #define __KERNEL_SYSCALLS__
50 #include <linux/unistd.h>
51 #include <linux/vmalloc.h>
53 #include <linux/drbd_limits.h>
55 #include "drbd_protocol.h"
56 #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
60 static DEFINE_MUTEX(drbd_main_mutex
);
61 int drbd_worker(struct drbd_thread
*);
64 static int drbd_open(struct block_device
*bdev
, fmode_t mode
);
65 static void drbd_release(struct gendisk
*gd
, fmode_t mode
);
66 static int w_md_sync(struct drbd_work
*w
, int unused
);
67 static void md_sync_timer_fn(unsigned long data
);
68 static int w_bitmap_io(struct drbd_work
*w
, int unused
);
69 static int w_go_diskless(struct drbd_work
*w
, int unused
);
71 MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
72 "Lars Ellenberg <lars@linbit.com>");
73 MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION
);
74 MODULE_VERSION(REL_VERSION
);
75 MODULE_LICENSE("GPL");
76 MODULE_PARM_DESC(minor_count
, "Approximate number of drbd devices ("
77 __stringify(DRBD_MINOR_COUNT_MIN
) "-" __stringify(DRBD_MINOR_COUNT_MAX
) ")");
78 MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR
);
80 #include <linux/moduleparam.h>
81 /* allow_open_on_secondary */
82 MODULE_PARM_DESC(allow_oos
, "DONT USE!");
83 /* thanks to these macros, if compiled into the kernel (not-module),
84 * this becomes the boot parameter drbd.minor_count */
85 module_param(minor_count
, uint
, 0444);
86 module_param(disable_sendpage
, bool, 0644);
87 module_param(allow_oos
, bool, 0);
88 module_param(proc_details
, int, 0644);
90 #ifdef CONFIG_DRBD_FAULT_INJECTION
93 static int fault_count
;
95 /* bitmap of enabled faults */
96 module_param(enable_faults
, int, 0664);
97 /* fault rate % value - applies to all enabled faults */
98 module_param(fault_rate
, int, 0664);
99 /* count of faults inserted */
100 module_param(fault_count
, int, 0664);
101 /* bitmap of devices to insert faults on */
102 module_param(fault_devs
, int, 0644);
105 /* module parameter, defined */
106 unsigned int minor_count
= DRBD_MINOR_COUNT_DEF
;
107 bool disable_sendpage
;
109 int proc_details
; /* Detail level in proc drbd*/
111 /* Module parameter for setting the user mode helper program
112 * to run. Default is /sbin/drbdadm */
113 char usermode_helper
[80] = "/sbin/drbdadm";
115 module_param_string(usermode_helper
, usermode_helper
, sizeof(usermode_helper
), 0644);
117 /* in 2.6.x, our device mapping and config info contains our virtual gendisks
118 * as member "struct gendisk *vdisk;"
120 struct idr drbd_devices
;
121 struct list_head drbd_connections
; /* list of struct drbd_connection */
123 struct kmem_cache
*drbd_request_cache
;
124 struct kmem_cache
*drbd_ee_cache
; /* peer requests */
125 struct kmem_cache
*drbd_bm_ext_cache
; /* bitmap extents */
126 struct kmem_cache
*drbd_al_ext_cache
; /* activity log extents */
127 mempool_t
*drbd_request_mempool
;
128 mempool_t
*drbd_ee_mempool
;
129 mempool_t
*drbd_md_io_page_pool
;
130 struct bio_set
*drbd_md_io_bio_set
;
132 /* I do not use a standard mempool, because:
133 1) I want to hand out the pre-allocated objects first.
134 2) I want to be able to interrupt sleeping allocation with a signal.
135 Note: This is a single linked list, the next pointer is the private
136 member of struct page.
138 struct page
*drbd_pp_pool
;
139 spinlock_t drbd_pp_lock
;
141 wait_queue_head_t drbd_pp_wait
;
143 DEFINE_RATELIMIT_STATE(drbd_ratelimit_state
, 5 * HZ
, 5);
145 static const struct block_device_operations drbd_ops
= {
146 .owner
= THIS_MODULE
,
148 .release
= drbd_release
,
151 struct bio
*bio_alloc_drbd(gfp_t gfp_mask
)
155 if (!drbd_md_io_bio_set
)
156 return bio_alloc(gfp_mask
, 1);
158 bio
= bio_alloc_bioset(gfp_mask
, 1, drbd_md_io_bio_set
);
165 /* When checking with sparse, and this is an inline function, sparse will
166 give tons of false positives. When this is a real functions sparse works.
168 int _get_ldev_if_state(struct drbd_device
*device
, enum drbd_disk_state mins
)
172 atomic_inc(&device
->local_cnt
);
173 io_allowed
= (device
->state
.disk
>= mins
);
175 if (atomic_dec_and_test(&device
->local_cnt
))
176 wake_up(&device
->misc_wait
);
184 * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
185 * @connection: DRBD connection.
186 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
187 * @set_size: Expected number of requests before that barrier.
189 * In case the passed barrier_nr or set_size does not match the oldest
190 * epoch of not yet barrier-acked requests, this function will cause a
191 * termination of the connection.
193 void tl_release(struct drbd_connection
*connection
, unsigned int barrier_nr
,
194 unsigned int set_size
)
196 struct drbd_request
*r
;
197 struct drbd_request
*req
= NULL
;
198 int expect_epoch
= 0;
201 spin_lock_irq(&connection
->req_lock
);
203 /* find oldest not yet barrier-acked write request,
204 * count writes in its epoch. */
205 list_for_each_entry(r
, &connection
->transfer_log
, tl_requests
) {
206 const unsigned s
= r
->rq_state
;
210 if (!(s
& RQ_NET_MASK
))
215 expect_epoch
= req
->epoch
;
218 if (r
->epoch
!= expect_epoch
)
222 /* if (s & RQ_DONE): not expected */
223 /* if (!(s & RQ_NET_MASK)): not expected */
228 /* first some paranoia code */
230 conn_err(connection
, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
234 if (expect_epoch
!= barrier_nr
) {
235 conn_err(connection
, "BAD! BarrierAck #%u received, expected #%u!\n",
236 barrier_nr
, expect_epoch
);
240 if (expect_size
!= set_size
) {
241 conn_err(connection
, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
242 barrier_nr
, set_size
, expect_size
);
246 /* Clean up list of requests processed during current epoch. */
247 /* this extra list walk restart is paranoia,
248 * to catch requests being barrier-acked "unexpectedly".
249 * It usually should find the same req again, or some READ preceding it. */
250 list_for_each_entry(req
, &connection
->transfer_log
, tl_requests
)
251 if (req
->epoch
== expect_epoch
)
253 list_for_each_entry_safe_from(req
, r
, &connection
->transfer_log
, tl_requests
) {
254 if (req
->epoch
!= expect_epoch
)
256 _req_mod(req
, BARRIER_ACKED
);
258 spin_unlock_irq(&connection
->req_lock
);
263 spin_unlock_irq(&connection
->req_lock
);
264 conn_request_state(connection
, NS(conn
, C_PROTOCOL_ERROR
), CS_HARD
);
269 * _tl_restart() - Walks the transfer log, and applies an action to all requests
270 * @device: DRBD device.
271 * @what: The action/event to perform with all request objects
273 * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
274 * RESTART_FROZEN_DISK_IO.
276 /* must hold resource->req_lock */
277 void _tl_restart(struct drbd_connection
*connection
, enum drbd_req_event what
)
279 struct drbd_request
*req
, *r
;
281 list_for_each_entry_safe(req
, r
, &connection
->transfer_log
, tl_requests
)
285 void tl_restart(struct drbd_connection
*connection
, enum drbd_req_event what
)
287 spin_lock_irq(&connection
->req_lock
);
288 _tl_restart(connection
, what
);
289 spin_unlock_irq(&connection
->req_lock
);
293 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
294 * @device: DRBD device.
296 * This is called after the connection to the peer was lost. The storage covered
297 * by the requests on the transfer gets marked as our of sync. Called from the
298 * receiver thread and the worker thread.
300 void tl_clear(struct drbd_connection
*connection
)
302 tl_restart(connection
, CONNECTION_LOST_WHILE_PENDING
);
306 * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
307 * @device: DRBD device.
309 void tl_abort_disk_io(struct drbd_device
*device
)
311 struct drbd_connection
*connection
= first_peer_device(device
)->connection
;
312 struct drbd_request
*req
, *r
;
314 spin_lock_irq(&connection
->req_lock
);
315 list_for_each_entry_safe(req
, r
, &connection
->transfer_log
, tl_requests
) {
316 if (!(req
->rq_state
& RQ_LOCAL_PENDING
))
318 if (req
->w
.device
!= device
)
320 _req_mod(req
, ABORT_DISK_IO
);
322 spin_unlock_irq(&connection
->req_lock
);
325 static int drbd_thread_setup(void *arg
)
327 struct drbd_thread
*thi
= (struct drbd_thread
*) arg
;
328 struct drbd_connection
*connection
= thi
->connection
;
332 snprintf(current
->comm
, sizeof(current
->comm
), "drbd_%c_%s",
333 thi
->name
[0], thi
->connection
->name
);
336 retval
= thi
->function(thi
);
338 spin_lock_irqsave(&thi
->t_lock
, flags
);
340 /* if the receiver has been "EXITING", the last thing it did
341 * was set the conn state to "StandAlone",
342 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
343 * and receiver thread will be "started".
344 * drbd_thread_start needs to set "RESTARTING" in that case.
345 * t_state check and assignment needs to be within the same spinlock,
346 * so either thread_start sees EXITING, and can remap to RESTARTING,
347 * or thread_start see NONE, and can proceed as normal.
350 if (thi
->t_state
== RESTARTING
) {
351 conn_info(connection
, "Restarting %s thread\n", thi
->name
);
352 thi
->t_state
= RUNNING
;
353 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
360 complete_all(&thi
->stop
);
361 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
363 conn_info(connection
, "Terminating %s\n", current
->comm
);
365 /* Release mod reference taken when thread was started */
367 kref_put(&connection
->kref
, drbd_destroy_connection
);
368 module_put(THIS_MODULE
);
372 static void drbd_thread_init(struct drbd_connection
*connection
, struct drbd_thread
*thi
,
373 int (*func
) (struct drbd_thread
*), char *name
)
375 spin_lock_init(&thi
->t_lock
);
378 thi
->function
= func
;
379 thi
->connection
= connection
;
380 strncpy(thi
->name
, name
, ARRAY_SIZE(thi
->name
));
383 int drbd_thread_start(struct drbd_thread
*thi
)
385 struct drbd_connection
*connection
= thi
->connection
;
386 struct task_struct
*nt
;
389 /* is used from state engine doing drbd_thread_stop_nowait,
390 * while holding the req lock irqsave */
391 spin_lock_irqsave(&thi
->t_lock
, flags
);
393 switch (thi
->t_state
) {
395 conn_info(connection
, "Starting %s thread (from %s [%d])\n",
396 thi
->name
, current
->comm
, current
->pid
);
398 /* Get ref on module for thread - this is released when thread exits */
399 if (!try_module_get(THIS_MODULE
)) {
400 conn_err(connection
, "Failed to get module reference in drbd_thread_start\n");
401 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
405 kref_get(&thi
->connection
->kref
);
407 init_completion(&thi
->stop
);
408 thi
->reset_cpu_mask
= 1;
409 thi
->t_state
= RUNNING
;
410 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
411 flush_signals(current
); /* otherw. may get -ERESTARTNOINTR */
413 nt
= kthread_create(drbd_thread_setup
, (void *) thi
,
414 "drbd_%c_%s", thi
->name
[0], thi
->connection
->name
);
417 conn_err(connection
, "Couldn't start thread\n");
419 kref_put(&connection
->kref
, drbd_destroy_connection
);
420 module_put(THIS_MODULE
);
423 spin_lock_irqsave(&thi
->t_lock
, flags
);
425 thi
->t_state
= RUNNING
;
426 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
430 thi
->t_state
= RESTARTING
;
431 conn_info(connection
, "Restarting %s thread (from %s [%d])\n",
432 thi
->name
, current
->comm
, current
->pid
);
437 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
445 void _drbd_thread_stop(struct drbd_thread
*thi
, int restart
, int wait
)
449 enum drbd_thread_state ns
= restart
? RESTARTING
: EXITING
;
451 /* may be called from state engine, holding the req lock irqsave */
452 spin_lock_irqsave(&thi
->t_lock
, flags
);
454 if (thi
->t_state
== NONE
) {
455 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
457 drbd_thread_start(thi
);
461 if (thi
->t_state
!= ns
) {
462 if (thi
->task
== NULL
) {
463 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
469 init_completion(&thi
->stop
);
470 if (thi
->task
!= current
)
471 force_sig(DRBD_SIGKILL
, thi
->task
);
474 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
477 wait_for_completion(&thi
->stop
);
480 static struct drbd_thread
*drbd_task_to_thread(struct drbd_connection
*connection
, struct task_struct
*task
)
482 struct drbd_thread
*thi
=
483 task
== connection
->receiver
.task
? &connection
->receiver
:
484 task
== connection
->asender
.task
? &connection
->asender
:
485 task
== connection
->worker
.task
? &connection
->worker
: NULL
;
490 char *drbd_task_to_thread_name(struct drbd_connection
*connection
, struct task_struct
*task
)
492 struct drbd_thread
*thi
= drbd_task_to_thread(connection
, task
);
493 return thi
? thi
->name
: task
->comm
;
496 int conn_lowest_minor(struct drbd_connection
*connection
)
498 struct drbd_device
*device
;
502 device
= idr_get_next(&connection
->volumes
, &vnr
);
503 m
= device
? device_to_minor(device
) : -1;
511 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
512 * @device: DRBD device.
514 * Forces all threads of a device onto the same CPU. This is beneficial for
515 * DRBD's performance. May be overwritten by user's configuration.
517 void drbd_calc_cpu_mask(struct drbd_connection
*connection
)
522 if (cpumask_weight(connection
->cpu_mask
))
525 ord
= conn_lowest_minor(connection
) % cpumask_weight(cpu_online_mask
);
526 for_each_online_cpu(cpu
) {
528 cpumask_set_cpu(cpu
, connection
->cpu_mask
);
532 /* should not be reached */
533 cpumask_setall(connection
->cpu_mask
);
537 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
538 * @device: DRBD device.
539 * @thi: drbd_thread object
541 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
544 void drbd_thread_current_set_cpu(struct drbd_thread
*thi
)
546 struct task_struct
*p
= current
;
548 if (!thi
->reset_cpu_mask
)
550 thi
->reset_cpu_mask
= 0;
551 set_cpus_allowed_ptr(p
, thi
->connection
->cpu_mask
);
556 * drbd_header_size - size of a packet header
558 * The header size is a multiple of 8, so any payload following the header is
559 * word aligned on 64-bit architectures. (The bitmap send and receive code
562 unsigned int drbd_header_size(struct drbd_connection
*connection
)
564 if (connection
->agreed_pro_version
>= 100) {
565 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100
), 8));
566 return sizeof(struct p_header100
);
568 BUILD_BUG_ON(sizeof(struct p_header80
) !=
569 sizeof(struct p_header95
));
570 BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80
), 8));
571 return sizeof(struct p_header80
);
575 static unsigned int prepare_header80(struct p_header80
*h
, enum drbd_packet cmd
, int size
)
577 h
->magic
= cpu_to_be32(DRBD_MAGIC
);
578 h
->command
= cpu_to_be16(cmd
);
579 h
->length
= cpu_to_be16(size
);
580 return sizeof(struct p_header80
);
583 static unsigned int prepare_header95(struct p_header95
*h
, enum drbd_packet cmd
, int size
)
585 h
->magic
= cpu_to_be16(DRBD_MAGIC_BIG
);
586 h
->command
= cpu_to_be16(cmd
);
587 h
->length
= cpu_to_be32(size
);
588 return sizeof(struct p_header95
);
591 static unsigned int prepare_header100(struct p_header100
*h
, enum drbd_packet cmd
,
594 h
->magic
= cpu_to_be32(DRBD_MAGIC_100
);
595 h
->volume
= cpu_to_be16(vnr
);
596 h
->command
= cpu_to_be16(cmd
);
597 h
->length
= cpu_to_be32(size
);
599 return sizeof(struct p_header100
);
602 static unsigned int prepare_header(struct drbd_connection
*connection
, int vnr
,
603 void *buffer
, enum drbd_packet cmd
, int size
)
605 if (connection
->agreed_pro_version
>= 100)
606 return prepare_header100(buffer
, cmd
, size
, vnr
);
607 else if (connection
->agreed_pro_version
>= 95 &&
608 size
> DRBD_MAX_SIZE_H80_PACKET
)
609 return prepare_header95(buffer
, cmd
, size
);
611 return prepare_header80(buffer
, cmd
, size
);
614 static void *__conn_prepare_command(struct drbd_connection
*connection
,
615 struct drbd_socket
*sock
)
619 return sock
->sbuf
+ drbd_header_size(connection
);
622 void *conn_prepare_command(struct drbd_connection
*connection
, struct drbd_socket
*sock
)
626 mutex_lock(&sock
->mutex
);
627 p
= __conn_prepare_command(connection
, sock
);
629 mutex_unlock(&sock
->mutex
);
634 void *drbd_prepare_command(struct drbd_device
*device
, struct drbd_socket
*sock
)
636 return conn_prepare_command(first_peer_device(device
)->connection
, sock
);
639 static int __send_command(struct drbd_connection
*connection
, int vnr
,
640 struct drbd_socket
*sock
, enum drbd_packet cmd
,
641 unsigned int header_size
, void *data
,
648 * Called with @data == NULL and the size of the data blocks in @size
649 * for commands that send data blocks. For those commands, omit the
650 * MSG_MORE flag: this will increase the likelihood that data blocks
651 * which are page aligned on the sender will end up page aligned on the
654 msg_flags
= data
? MSG_MORE
: 0;
656 header_size
+= prepare_header(connection
, vnr
, sock
->sbuf
, cmd
,
658 err
= drbd_send_all(connection
, sock
->socket
, sock
->sbuf
, header_size
,
661 err
= drbd_send_all(connection
, sock
->socket
, data
, size
, 0);
665 static int __conn_send_command(struct drbd_connection
*connection
, struct drbd_socket
*sock
,
666 enum drbd_packet cmd
, unsigned int header_size
,
667 void *data
, unsigned int size
)
669 return __send_command(connection
, 0, sock
, cmd
, header_size
, data
, size
);
672 int conn_send_command(struct drbd_connection
*connection
, struct drbd_socket
*sock
,
673 enum drbd_packet cmd
, unsigned int header_size
,
674 void *data
, unsigned int size
)
678 err
= __conn_send_command(connection
, sock
, cmd
, header_size
, data
, size
);
679 mutex_unlock(&sock
->mutex
);
683 int drbd_send_command(struct drbd_device
*device
, struct drbd_socket
*sock
,
684 enum drbd_packet cmd
, unsigned int header_size
,
685 void *data
, unsigned int size
)
689 err
= __send_command(first_peer_device(device
)->connection
, device
->vnr
, sock
, cmd
, header_size
,
691 mutex_unlock(&sock
->mutex
);
695 int drbd_send_ping(struct drbd_connection
*connection
)
697 struct drbd_socket
*sock
;
699 sock
= &connection
->meta
;
700 if (!conn_prepare_command(connection
, sock
))
702 return conn_send_command(connection
, sock
, P_PING
, 0, NULL
, 0);
705 int drbd_send_ping_ack(struct drbd_connection
*connection
)
707 struct drbd_socket
*sock
;
709 sock
= &connection
->meta
;
710 if (!conn_prepare_command(connection
, sock
))
712 return conn_send_command(connection
, sock
, P_PING_ACK
, 0, NULL
, 0);
715 int drbd_send_sync_param(struct drbd_device
*device
)
717 struct drbd_socket
*sock
;
718 struct p_rs_param_95
*p
;
720 const int apv
= first_peer_device(device
)->connection
->agreed_pro_version
;
721 enum drbd_packet cmd
;
723 struct disk_conf
*dc
;
725 sock
= &first_peer_device(device
)->connection
->data
;
726 p
= drbd_prepare_command(device
, sock
);
731 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
733 size
= apv
<= 87 ? sizeof(struct p_rs_param
)
734 : apv
== 88 ? sizeof(struct p_rs_param
)
735 + strlen(nc
->verify_alg
) + 1
736 : apv
<= 94 ? sizeof(struct p_rs_param_89
)
737 : /* apv >= 95 */ sizeof(struct p_rs_param_95
);
739 cmd
= apv
>= 89 ? P_SYNC_PARAM89
: P_SYNC_PARAM
;
741 /* initialize verify_alg and csums_alg */
742 memset(p
->verify_alg
, 0, 2 * SHARED_SECRET_MAX
);
744 if (get_ldev(device
)) {
745 dc
= rcu_dereference(device
->ldev
->disk_conf
);
746 p
->resync_rate
= cpu_to_be32(dc
->resync_rate
);
747 p
->c_plan_ahead
= cpu_to_be32(dc
->c_plan_ahead
);
748 p
->c_delay_target
= cpu_to_be32(dc
->c_delay_target
);
749 p
->c_fill_target
= cpu_to_be32(dc
->c_fill_target
);
750 p
->c_max_rate
= cpu_to_be32(dc
->c_max_rate
);
753 p
->resync_rate
= cpu_to_be32(DRBD_RESYNC_RATE_DEF
);
754 p
->c_plan_ahead
= cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF
);
755 p
->c_delay_target
= cpu_to_be32(DRBD_C_DELAY_TARGET_DEF
);
756 p
->c_fill_target
= cpu_to_be32(DRBD_C_FILL_TARGET_DEF
);
757 p
->c_max_rate
= cpu_to_be32(DRBD_C_MAX_RATE_DEF
);
761 strcpy(p
->verify_alg
, nc
->verify_alg
);
763 strcpy(p
->csums_alg
, nc
->csums_alg
);
766 return drbd_send_command(device
, sock
, cmd
, size
, NULL
, 0);
769 int __drbd_send_protocol(struct drbd_connection
*connection
, enum drbd_packet cmd
)
771 struct drbd_socket
*sock
;
772 struct p_protocol
*p
;
776 sock
= &connection
->data
;
777 p
= __conn_prepare_command(connection
, sock
);
782 nc
= rcu_dereference(connection
->net_conf
);
784 if (nc
->tentative
&& connection
->agreed_pro_version
< 92) {
786 mutex_unlock(&sock
->mutex
);
787 conn_err(connection
, "--dry-run is not supported by peer");
792 if (connection
->agreed_pro_version
>= 87)
793 size
+= strlen(nc
->integrity_alg
) + 1;
795 p
->protocol
= cpu_to_be32(nc
->wire_protocol
);
796 p
->after_sb_0p
= cpu_to_be32(nc
->after_sb_0p
);
797 p
->after_sb_1p
= cpu_to_be32(nc
->after_sb_1p
);
798 p
->after_sb_2p
= cpu_to_be32(nc
->after_sb_2p
);
799 p
->two_primaries
= cpu_to_be32(nc
->two_primaries
);
801 if (nc
->discard_my_data
)
802 cf
|= CF_DISCARD_MY_DATA
;
805 p
->conn_flags
= cpu_to_be32(cf
);
807 if (connection
->agreed_pro_version
>= 87)
808 strcpy(p
->integrity_alg
, nc
->integrity_alg
);
811 return __conn_send_command(connection
, sock
, cmd
, size
, NULL
, 0);
814 int drbd_send_protocol(struct drbd_connection
*connection
)
818 mutex_lock(&connection
->data
.mutex
);
819 err
= __drbd_send_protocol(connection
, P_PROTOCOL
);
820 mutex_unlock(&connection
->data
.mutex
);
825 static int _drbd_send_uuids(struct drbd_device
*device
, u64 uuid_flags
)
827 struct drbd_socket
*sock
;
831 if (!get_ldev_if_state(device
, D_NEGOTIATING
))
834 sock
= &first_peer_device(device
)->connection
->data
;
835 p
= drbd_prepare_command(device
, sock
);
840 spin_lock_irq(&device
->ldev
->md
.uuid_lock
);
841 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
842 p
->uuid
[i
] = cpu_to_be64(device
->ldev
->md
.uuid
[i
]);
843 spin_unlock_irq(&device
->ldev
->md
.uuid_lock
);
845 device
->comm_bm_set
= drbd_bm_total_weight(device
);
846 p
->uuid
[UI_SIZE
] = cpu_to_be64(device
->comm_bm_set
);
848 uuid_flags
|= rcu_dereference(first_peer_device(device
)->connection
->net_conf
)->discard_my_data
? 1 : 0;
850 uuid_flags
|= test_bit(CRASHED_PRIMARY
, &device
->flags
) ? 2 : 0;
851 uuid_flags
|= device
->new_state_tmp
.disk
== D_INCONSISTENT
? 4 : 0;
852 p
->uuid
[UI_FLAGS
] = cpu_to_be64(uuid_flags
);
855 return drbd_send_command(device
, sock
, P_UUIDS
, sizeof(*p
), NULL
, 0);
858 int drbd_send_uuids(struct drbd_device
*device
)
860 return _drbd_send_uuids(device
, 0);
863 int drbd_send_uuids_skip_initial_sync(struct drbd_device
*device
)
865 return _drbd_send_uuids(device
, 8);
868 void drbd_print_uuids(struct drbd_device
*device
, const char *text
)
870 if (get_ldev_if_state(device
, D_NEGOTIATING
)) {
871 u64
*uuid
= device
->ldev
->md
.uuid
;
872 dev_info(DEV
, "%s %016llX:%016llX:%016llX:%016llX\n",
874 (unsigned long long)uuid
[UI_CURRENT
],
875 (unsigned long long)uuid
[UI_BITMAP
],
876 (unsigned long long)uuid
[UI_HISTORY_START
],
877 (unsigned long long)uuid
[UI_HISTORY_END
]);
880 dev_info(DEV
, "%s effective data uuid: %016llX\n",
882 (unsigned long long)device
->ed_uuid
);
886 void drbd_gen_and_send_sync_uuid(struct drbd_device
*device
)
888 struct drbd_socket
*sock
;
892 D_ASSERT(device
->state
.disk
== D_UP_TO_DATE
);
894 uuid
= device
->ldev
->md
.uuid
[UI_BITMAP
];
895 if (uuid
&& uuid
!= UUID_JUST_CREATED
)
896 uuid
= uuid
+ UUID_NEW_BM_OFFSET
;
898 get_random_bytes(&uuid
, sizeof(u64
));
899 drbd_uuid_set(device
, UI_BITMAP
, uuid
);
900 drbd_print_uuids(device
, "updated sync UUID");
901 drbd_md_sync(device
);
903 sock
= &first_peer_device(device
)->connection
->data
;
904 p
= drbd_prepare_command(device
, sock
);
906 p
->uuid
= cpu_to_be64(uuid
);
907 drbd_send_command(device
, sock
, P_SYNC_UUID
, sizeof(*p
), NULL
, 0);
911 int drbd_send_sizes(struct drbd_device
*device
, int trigger_reply
, enum dds_flags flags
)
913 struct drbd_socket
*sock
;
915 sector_t d_size
, u_size
;
917 unsigned int max_bio_size
;
919 if (get_ldev_if_state(device
, D_NEGOTIATING
)) {
920 D_ASSERT(device
->ldev
->backing_bdev
);
921 d_size
= drbd_get_max_capacity(device
->ldev
);
923 u_size
= rcu_dereference(device
->ldev
->disk_conf
)->disk_size
;
925 q_order_type
= drbd_queue_order_type(device
);
926 max_bio_size
= queue_max_hw_sectors(device
->ldev
->backing_bdev
->bd_disk
->queue
) << 9;
927 max_bio_size
= min(max_bio_size
, DRBD_MAX_BIO_SIZE
);
932 q_order_type
= QUEUE_ORDERED_NONE
;
933 max_bio_size
= DRBD_MAX_BIO_SIZE
; /* ... multiple BIOs per peer_request */
936 sock
= &first_peer_device(device
)->connection
->data
;
937 p
= drbd_prepare_command(device
, sock
);
941 if (first_peer_device(device
)->connection
->agreed_pro_version
<= 94)
942 max_bio_size
= min(max_bio_size
, DRBD_MAX_SIZE_H80_PACKET
);
943 else if (first_peer_device(device
)->connection
->agreed_pro_version
< 100)
944 max_bio_size
= min(max_bio_size
, DRBD_MAX_BIO_SIZE_P95
);
946 p
->d_size
= cpu_to_be64(d_size
);
947 p
->u_size
= cpu_to_be64(u_size
);
948 p
->c_size
= cpu_to_be64(trigger_reply
? 0 : drbd_get_capacity(device
->this_bdev
));
949 p
->max_bio_size
= cpu_to_be32(max_bio_size
);
950 p
->queue_order_type
= cpu_to_be16(q_order_type
);
951 p
->dds_flags
= cpu_to_be16(flags
);
952 return drbd_send_command(device
, sock
, P_SIZES
, sizeof(*p
), NULL
, 0);
956 * drbd_send_current_state() - Sends the drbd state to the peer
957 * @device: DRBD device.
959 int drbd_send_current_state(struct drbd_device
*device
)
961 struct drbd_socket
*sock
;
964 sock
= &first_peer_device(device
)->connection
->data
;
965 p
= drbd_prepare_command(device
, sock
);
968 p
->state
= cpu_to_be32(device
->state
.i
); /* Within the send mutex */
969 return drbd_send_command(device
, sock
, P_STATE
, sizeof(*p
), NULL
, 0);
973 * drbd_send_state() - After a state change, sends the new state to the peer
974 * @device: DRBD device.
975 * @state: the state to send, not necessarily the current state.
977 * Each state change queues an "after_state_ch" work, which will eventually
978 * send the resulting new state to the peer. If more state changes happen
979 * between queuing and processing of the after_state_ch work, we still
980 * want to send each intermediary state in the order it occurred.
982 int drbd_send_state(struct drbd_device
*device
, union drbd_state state
)
984 struct drbd_socket
*sock
;
987 sock
= &first_peer_device(device
)->connection
->data
;
988 p
= drbd_prepare_command(device
, sock
);
991 p
->state
= cpu_to_be32(state
.i
); /* Within the send mutex */
992 return drbd_send_command(device
, sock
, P_STATE
, sizeof(*p
), NULL
, 0);
995 int drbd_send_state_req(struct drbd_device
*device
, union drbd_state mask
, union drbd_state val
)
997 struct drbd_socket
*sock
;
998 struct p_req_state
*p
;
1000 sock
= &first_peer_device(device
)->connection
->data
;
1001 p
= drbd_prepare_command(device
, sock
);
1004 p
->mask
= cpu_to_be32(mask
.i
);
1005 p
->val
= cpu_to_be32(val
.i
);
1006 return drbd_send_command(device
, sock
, P_STATE_CHG_REQ
, sizeof(*p
), NULL
, 0);
1009 int conn_send_state_req(struct drbd_connection
*connection
, union drbd_state mask
, union drbd_state val
)
1011 enum drbd_packet cmd
;
1012 struct drbd_socket
*sock
;
1013 struct p_req_state
*p
;
1015 cmd
= connection
->agreed_pro_version
< 100 ? P_STATE_CHG_REQ
: P_CONN_ST_CHG_REQ
;
1016 sock
= &connection
->data
;
1017 p
= conn_prepare_command(connection
, sock
);
1020 p
->mask
= cpu_to_be32(mask
.i
);
1021 p
->val
= cpu_to_be32(val
.i
);
1022 return conn_send_command(connection
, sock
, cmd
, sizeof(*p
), NULL
, 0);
1025 void drbd_send_sr_reply(struct drbd_device
*device
, enum drbd_state_rv retcode
)
1027 struct drbd_socket
*sock
;
1028 struct p_req_state_reply
*p
;
1030 sock
= &first_peer_device(device
)->connection
->meta
;
1031 p
= drbd_prepare_command(device
, sock
);
1033 p
->retcode
= cpu_to_be32(retcode
);
1034 drbd_send_command(device
, sock
, P_STATE_CHG_REPLY
, sizeof(*p
), NULL
, 0);
1038 void conn_send_sr_reply(struct drbd_connection
*connection
, enum drbd_state_rv retcode
)
1040 struct drbd_socket
*sock
;
1041 struct p_req_state_reply
*p
;
1042 enum drbd_packet cmd
= connection
->agreed_pro_version
< 100 ? P_STATE_CHG_REPLY
: P_CONN_ST_CHG_REPLY
;
1044 sock
= &connection
->meta
;
1045 p
= conn_prepare_command(connection
, sock
);
1047 p
->retcode
= cpu_to_be32(retcode
);
1048 conn_send_command(connection
, sock
, cmd
, sizeof(*p
), NULL
, 0);
1052 static void dcbp_set_code(struct p_compressed_bm
*p
, enum drbd_bitmap_code code
)
1054 BUG_ON(code
& ~0xf);
1055 p
->encoding
= (p
->encoding
& ~0xf) | code
;
1058 static void dcbp_set_start(struct p_compressed_bm
*p
, int set
)
1060 p
->encoding
= (p
->encoding
& ~0x80) | (set
? 0x80 : 0);
1063 static void dcbp_set_pad_bits(struct p_compressed_bm
*p
, int n
)
1066 p
->encoding
= (p
->encoding
& (~0x7 << 4)) | (n
<< 4);
1069 static int fill_bitmap_rle_bits(struct drbd_device
*device
,
1070 struct p_compressed_bm
*p
,
1072 struct bm_xfer_ctx
*c
)
1074 struct bitstream bs
;
1075 unsigned long plain_bits
;
1082 /* may we use this feature? */
1084 use_rle
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
)->use_rle
;
1086 if (!use_rle
|| first_peer_device(device
)->connection
->agreed_pro_version
< 90)
1089 if (c
->bit_offset
>= c
->bm_bits
)
1090 return 0; /* nothing to do. */
1092 /* use at most thus many bytes */
1093 bitstream_init(&bs
, p
->code
, size
, 0);
1094 memset(p
->code
, 0, size
);
1095 /* plain bits covered in this code string */
1098 /* p->encoding & 0x80 stores whether the first run length is set.
1099 * bit offset is implicit.
1100 * start with toggle == 2 to be able to tell the first iteration */
1103 /* see how much plain bits we can stuff into one packet
1104 * using RLE and VLI. */
1106 tmp
= (toggle
== 0) ? _drbd_bm_find_next_zero(device
, c
->bit_offset
)
1107 : _drbd_bm_find_next(device
, c
->bit_offset
);
1110 rl
= tmp
- c
->bit_offset
;
1112 if (toggle
== 2) { /* first iteration */
1114 /* the first checked bit was set,
1115 * store start value, */
1116 dcbp_set_start(p
, 1);
1117 /* but skip encoding of zero run length */
1121 dcbp_set_start(p
, 0);
1124 /* paranoia: catch zero runlength.
1125 * can only happen if bitmap is modified while we scan it. */
1127 dev_err(DEV
, "unexpected zero runlength while encoding bitmap "
1128 "t:%u bo:%lu\n", toggle
, c
->bit_offset
);
1132 bits
= vli_encode_bits(&bs
, rl
);
1133 if (bits
== -ENOBUFS
) /* buffer full */
1136 dev_err(DEV
, "error while encoding bitmap: %d\n", bits
);
1142 c
->bit_offset
= tmp
;
1143 } while (c
->bit_offset
< c
->bm_bits
);
1145 len
= bs
.cur
.b
- p
->code
+ !!bs
.cur
.bit
;
1147 if (plain_bits
< (len
<< 3)) {
1148 /* incompressible with this method.
1149 * we need to rewind both word and bit position. */
1150 c
->bit_offset
-= plain_bits
;
1151 bm_xfer_ctx_bit_to_word_offset(c
);
1152 c
->bit_offset
= c
->word_offset
* BITS_PER_LONG
;
1156 /* RLE + VLI was able to compress it just fine.
1157 * update c->word_offset. */
1158 bm_xfer_ctx_bit_to_word_offset(c
);
1160 /* store pad_bits */
1161 dcbp_set_pad_bits(p
, (8 - bs
.cur
.bit
) & 0x7);
1167 * send_bitmap_rle_or_plain
1169 * Return 0 when done, 1 when another iteration is needed, and a negative error
1170 * code upon failure.
1173 send_bitmap_rle_or_plain(struct drbd_device
*device
, struct bm_xfer_ctx
*c
)
1175 struct drbd_socket
*sock
= &first_peer_device(device
)->connection
->data
;
1176 unsigned int header_size
= drbd_header_size(first_peer_device(device
)->connection
);
1177 struct p_compressed_bm
*p
= sock
->sbuf
+ header_size
;
1180 len
= fill_bitmap_rle_bits(device
, p
,
1181 DRBD_SOCKET_BUFFER_SIZE
- header_size
- sizeof(*p
), c
);
1186 dcbp_set_code(p
, RLE_VLI_Bits
);
1187 err
= __send_command(first_peer_device(device
)->connection
, device
->vnr
, sock
,
1188 P_COMPRESSED_BITMAP
, sizeof(*p
) + len
,
1191 c
->bytes
[0] += header_size
+ sizeof(*p
) + len
;
1193 if (c
->bit_offset
>= c
->bm_bits
)
1196 /* was not compressible.
1197 * send a buffer full of plain text bits instead. */
1198 unsigned int data_size
;
1199 unsigned long num_words
;
1200 unsigned long *p
= sock
->sbuf
+ header_size
;
1202 data_size
= DRBD_SOCKET_BUFFER_SIZE
- header_size
;
1203 num_words
= min_t(size_t, data_size
/ sizeof(*p
),
1204 c
->bm_words
- c
->word_offset
);
1205 len
= num_words
* sizeof(*p
);
1207 drbd_bm_get_lel(device
, c
->word_offset
, num_words
, p
);
1208 err
= __send_command(first_peer_device(device
)->connection
, device
->vnr
, sock
, P_BITMAP
, len
, NULL
, 0);
1209 c
->word_offset
+= num_words
;
1210 c
->bit_offset
= c
->word_offset
* BITS_PER_LONG
;
1213 c
->bytes
[1] += header_size
+ len
;
1215 if (c
->bit_offset
> c
->bm_bits
)
1216 c
->bit_offset
= c
->bm_bits
;
1220 INFO_bm_xfer_stats(device
, "send", c
);
1228 /* See the comment at receive_bitmap() */
1229 static int _drbd_send_bitmap(struct drbd_device
*device
)
1231 struct bm_xfer_ctx c
;
1234 if (!expect(device
->bitmap
))
1237 if (get_ldev(device
)) {
1238 if (drbd_md_test_flag(device
->ldev
, MDF_FULL_SYNC
)) {
1239 dev_info(DEV
, "Writing the whole bitmap, MDF_FullSync was set.\n");
1240 drbd_bm_set_all(device
);
1241 if (drbd_bm_write(device
)) {
1242 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
1243 * but otherwise process as per normal - need to tell other
1244 * side that a full resync is required! */
1245 dev_err(DEV
, "Failed to write bitmap to disk!\n");
1247 drbd_md_clear_flag(device
, MDF_FULL_SYNC
);
1248 drbd_md_sync(device
);
1254 c
= (struct bm_xfer_ctx
) {
1255 .bm_bits
= drbd_bm_bits(device
),
1256 .bm_words
= drbd_bm_words(device
),
1260 err
= send_bitmap_rle_or_plain(device
, &c
);
1266 int drbd_send_bitmap(struct drbd_device
*device
)
1268 struct drbd_socket
*sock
= &first_peer_device(device
)->connection
->data
;
1271 mutex_lock(&sock
->mutex
);
1273 err
= !_drbd_send_bitmap(device
);
1274 mutex_unlock(&sock
->mutex
);
1278 void drbd_send_b_ack(struct drbd_connection
*connection
, u32 barrier_nr
, u32 set_size
)
1280 struct drbd_socket
*sock
;
1281 struct p_barrier_ack
*p
;
1283 if (connection
->cstate
< C_WF_REPORT_PARAMS
)
1286 sock
= &connection
->meta
;
1287 p
= conn_prepare_command(connection
, sock
);
1290 p
->barrier
= barrier_nr
;
1291 p
->set_size
= cpu_to_be32(set_size
);
1292 conn_send_command(connection
, sock
, P_BARRIER_ACK
, sizeof(*p
), NULL
, 0);
1296 * _drbd_send_ack() - Sends an ack packet
1297 * @device: DRBD device.
1298 * @cmd: Packet command code.
1299 * @sector: sector, needs to be in big endian byte order
1300 * @blksize: size in byte, needs to be in big endian byte order
1301 * @block_id: Id, big endian byte order
1303 static int _drbd_send_ack(struct drbd_device
*device
, enum drbd_packet cmd
,
1304 u64 sector
, u32 blksize
, u64 block_id
)
1306 struct drbd_socket
*sock
;
1307 struct p_block_ack
*p
;
1309 if (device
->state
.conn
< C_CONNECTED
)
1312 sock
= &first_peer_device(device
)->connection
->meta
;
1313 p
= drbd_prepare_command(device
, sock
);
1317 p
->block_id
= block_id
;
1318 p
->blksize
= blksize
;
1319 p
->seq_num
= cpu_to_be32(atomic_inc_return(&device
->packet_seq
));
1320 return drbd_send_command(device
, sock
, cmd
, sizeof(*p
), NULL
, 0);
1323 /* dp->sector and dp->block_id already/still in network byte order,
1324 * data_size is payload size according to dp->head,
1325 * and may need to be corrected for digest size. */
1326 void drbd_send_ack_dp(struct drbd_device
*device
, enum drbd_packet cmd
,
1327 struct p_data
*dp
, int data_size
)
1329 if (first_peer_device(device
)->connection
->peer_integrity_tfm
)
1330 data_size
-= crypto_hash_digestsize(first_peer_device(device
)->connection
->peer_integrity_tfm
);
1331 _drbd_send_ack(device
, cmd
, dp
->sector
, cpu_to_be32(data_size
),
1335 void drbd_send_ack_rp(struct drbd_device
*device
, enum drbd_packet cmd
,
1336 struct p_block_req
*rp
)
1338 _drbd_send_ack(device
, cmd
, rp
->sector
, rp
->blksize
, rp
->block_id
);
1342 * drbd_send_ack() - Sends an ack packet
1343 * @device: DRBD device
1344 * @cmd: packet command code
1345 * @peer_req: peer request
1347 int drbd_send_ack(struct drbd_device
*device
, enum drbd_packet cmd
,
1348 struct drbd_peer_request
*peer_req
)
1350 return _drbd_send_ack(device
, cmd
,
1351 cpu_to_be64(peer_req
->i
.sector
),
1352 cpu_to_be32(peer_req
->i
.size
),
1353 peer_req
->block_id
);
1356 /* This function misuses the block_id field to signal if the blocks
1357 * are is sync or not. */
1358 int drbd_send_ack_ex(struct drbd_device
*device
, enum drbd_packet cmd
,
1359 sector_t sector
, int blksize
, u64 block_id
)
1361 return _drbd_send_ack(device
, cmd
,
1362 cpu_to_be64(sector
),
1363 cpu_to_be32(blksize
),
1364 cpu_to_be64(block_id
));
1367 int drbd_send_drequest(struct drbd_device
*device
, int cmd
,
1368 sector_t sector
, int size
, u64 block_id
)
1370 struct drbd_socket
*sock
;
1371 struct p_block_req
*p
;
1373 sock
= &first_peer_device(device
)->connection
->data
;
1374 p
= drbd_prepare_command(device
, sock
);
1377 p
->sector
= cpu_to_be64(sector
);
1378 p
->block_id
= block_id
;
1379 p
->blksize
= cpu_to_be32(size
);
1380 return drbd_send_command(device
, sock
, cmd
, sizeof(*p
), NULL
, 0);
1383 int drbd_send_drequest_csum(struct drbd_device
*device
, sector_t sector
, int size
,
1384 void *digest
, int digest_size
, enum drbd_packet cmd
)
1386 struct drbd_socket
*sock
;
1387 struct p_block_req
*p
;
1389 /* FIXME: Put the digest into the preallocated socket buffer. */
1391 sock
= &first_peer_device(device
)->connection
->data
;
1392 p
= drbd_prepare_command(device
, sock
);
1395 p
->sector
= cpu_to_be64(sector
);
1396 p
->block_id
= ID_SYNCER
/* unused */;
1397 p
->blksize
= cpu_to_be32(size
);
1398 return drbd_send_command(device
, sock
, cmd
, sizeof(*p
),
1399 digest
, digest_size
);
1402 int drbd_send_ov_request(struct drbd_device
*device
, sector_t sector
, int size
)
1404 struct drbd_socket
*sock
;
1405 struct p_block_req
*p
;
1407 sock
= &first_peer_device(device
)->connection
->data
;
1408 p
= drbd_prepare_command(device
, sock
);
1411 p
->sector
= cpu_to_be64(sector
);
1412 p
->block_id
= ID_SYNCER
/* unused */;
1413 p
->blksize
= cpu_to_be32(size
);
1414 return drbd_send_command(device
, sock
, P_OV_REQUEST
, sizeof(*p
), NULL
, 0);
1417 /* called on sndtimeo
1418 * returns false if we should retry,
1419 * true if we think connection is dead
1421 static int we_should_drop_the_connection(struct drbd_connection
*connection
, struct socket
*sock
)
1424 /* long elapsed = (long)(jiffies - device->last_received); */
1426 drop_it
= connection
->meta
.socket
== sock
1427 || !connection
->asender
.task
1428 || get_t_state(&connection
->asender
) != RUNNING
1429 || connection
->cstate
< C_WF_REPORT_PARAMS
;
1434 drop_it
= !--connection
->ko_count
;
1436 conn_err(connection
, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
1437 current
->comm
, current
->pid
, connection
->ko_count
);
1438 request_ping(connection
);
1441 return drop_it
; /* && (device->state == R_PRIMARY) */;
1444 static void drbd_update_congested(struct drbd_connection
*connection
)
1446 struct sock
*sk
= connection
->data
.socket
->sk
;
1447 if (sk
->sk_wmem_queued
> sk
->sk_sndbuf
* 4 / 5)
1448 set_bit(NET_CONGESTED
, &connection
->flags
);
1451 /* The idea of sendpage seems to be to put some kind of reference
1452 * to the page into the skb, and to hand it over to the NIC. In
1453 * this process get_page() gets called.
1455 * As soon as the page was really sent over the network put_page()
1456 * gets called by some part of the network layer. [ NIC driver? ]
1458 * [ get_page() / put_page() increment/decrement the count. If count
1459 * reaches 0 the page will be freed. ]
1461 * This works nicely with pages from FSs.
1462 * But this means that in protocol A we might signal IO completion too early!
1464 * In order not to corrupt data during a resync we must make sure
1465 * that we do not reuse our own buffer pages (EEs) to early, therefore
1466 * we have the net_ee list.
1468 * XFS seems to have problems, still, it submits pages with page_count == 0!
1469 * As a workaround, we disable sendpage on pages
1470 * with page_count == 0 or PageSlab.
1472 static int _drbd_no_send_page(struct drbd_device
*device
, struct page
*page
,
1473 int offset
, size_t size
, unsigned msg_flags
)
1475 struct socket
*socket
;
1479 socket
= first_peer_device(device
)->connection
->data
.socket
;
1480 addr
= kmap(page
) + offset
;
1481 err
= drbd_send_all(first_peer_device(device
)->connection
, socket
, addr
, size
, msg_flags
);
1484 device
->send_cnt
+= size
>> 9;
1488 static int _drbd_send_page(struct drbd_device
*device
, struct page
*page
,
1489 int offset
, size_t size
, unsigned msg_flags
)
1491 struct socket
*socket
= first_peer_device(device
)->connection
->data
.socket
;
1492 mm_segment_t oldfs
= get_fs();
1496 /* e.g. XFS meta- & log-data is in slab pages, which have a
1497 * page_count of 0 and/or have PageSlab() set.
1498 * we cannot use send_page for those, as that does get_page();
1499 * put_page(); and would cause either a VM_BUG directly, or
1500 * __page_cache_release a page that would actually still be referenced
1501 * by someone, leading to some obscure delayed Oops somewhere else. */
1502 if (disable_sendpage
|| (page_count(page
) < 1) || PageSlab(page
))
1503 return _drbd_no_send_page(device
, page
, offset
, size
, msg_flags
);
1505 msg_flags
|= MSG_NOSIGNAL
;
1506 drbd_update_congested(first_peer_device(device
)->connection
);
1511 sent
= socket
->ops
->sendpage(socket
, page
, offset
, len
, msg_flags
);
1513 if (sent
== -EAGAIN
) {
1514 if (we_should_drop_the_connection(first_peer_device(device
)->connection
, socket
))
1518 dev_warn(DEV
, "%s: size=%d len=%d sent=%d\n",
1519 __func__
, (int)size
, len
, sent
);
1526 } while (len
> 0 /* THINK && device->cstate >= C_CONNECTED*/);
1528 clear_bit(NET_CONGESTED
, &first_peer_device(device
)->connection
->flags
);
1532 device
->send_cnt
+= size
>> 9;
1537 static int _drbd_send_bio(struct drbd_device
*device
, struct bio
*bio
)
1539 struct bio_vec bvec
;
1540 struct bvec_iter iter
;
1542 /* hint all but last page with MSG_MORE */
1543 bio_for_each_segment(bvec
, bio
, iter
) {
1546 err
= _drbd_no_send_page(device
, bvec
.bv_page
,
1547 bvec
.bv_offset
, bvec
.bv_len
,
1548 bio_iter_last(bvec
, iter
)
1556 static int _drbd_send_zc_bio(struct drbd_device
*device
, struct bio
*bio
)
1558 struct bio_vec bvec
;
1559 struct bvec_iter iter
;
1561 /* hint all but last page with MSG_MORE */
1562 bio_for_each_segment(bvec
, bio
, iter
) {
1565 err
= _drbd_send_page(device
, bvec
.bv_page
,
1566 bvec
.bv_offset
, bvec
.bv_len
,
1567 bio_iter_last(bvec
, iter
) ? 0 : MSG_MORE
);
1574 static int _drbd_send_zc_ee(struct drbd_device
*device
,
1575 struct drbd_peer_request
*peer_req
)
1577 struct page
*page
= peer_req
->pages
;
1578 unsigned len
= peer_req
->i
.size
;
1581 /* hint all but last page with MSG_MORE */
1582 page_chain_for_each(page
) {
1583 unsigned l
= min_t(unsigned, len
, PAGE_SIZE
);
1585 err
= _drbd_send_page(device
, page
, 0, l
,
1586 page_chain_next(page
) ? MSG_MORE
: 0);
1594 static u32
bio_flags_to_wire(struct drbd_device
*device
, unsigned long bi_rw
)
1596 if (first_peer_device(device
)->connection
->agreed_pro_version
>= 95)
1597 return (bi_rw
& REQ_SYNC
? DP_RW_SYNC
: 0) |
1598 (bi_rw
& REQ_FUA
? DP_FUA
: 0) |
1599 (bi_rw
& REQ_FLUSH
? DP_FLUSH
: 0) |
1600 (bi_rw
& REQ_DISCARD
? DP_DISCARD
: 0);
1602 return bi_rw
& REQ_SYNC
? DP_RW_SYNC
: 0;
1605 /* Used to send write requests
1606 * R_PRIMARY -> Peer (P_DATA)
1608 int drbd_send_dblock(struct drbd_device
*device
, struct drbd_request
*req
)
1610 struct drbd_socket
*sock
;
1612 unsigned int dp_flags
= 0;
1616 sock
= &first_peer_device(device
)->connection
->data
;
1617 p
= drbd_prepare_command(device
, sock
);
1618 dgs
= first_peer_device(device
)->connection
->integrity_tfm
?
1619 crypto_hash_digestsize(first_peer_device(device
)->connection
->integrity_tfm
) : 0;
1623 p
->sector
= cpu_to_be64(req
->i
.sector
);
1624 p
->block_id
= (unsigned long)req
;
1625 p
->seq_num
= cpu_to_be32(atomic_inc_return(&device
->packet_seq
));
1626 dp_flags
= bio_flags_to_wire(device
, req
->master_bio
->bi_rw
);
1627 if (device
->state
.conn
>= C_SYNC_SOURCE
&&
1628 device
->state
.conn
<= C_PAUSED_SYNC_T
)
1629 dp_flags
|= DP_MAY_SET_IN_SYNC
;
1630 if (first_peer_device(device
)->connection
->agreed_pro_version
>= 100) {
1631 if (req
->rq_state
& RQ_EXP_RECEIVE_ACK
)
1632 dp_flags
|= DP_SEND_RECEIVE_ACK
;
1633 if (req
->rq_state
& RQ_EXP_WRITE_ACK
)
1634 dp_flags
|= DP_SEND_WRITE_ACK
;
1636 p
->dp_flags
= cpu_to_be32(dp_flags
);
1638 drbd_csum_bio(device
, first_peer_device(device
)->connection
->integrity_tfm
, req
->master_bio
, p
+ 1);
1639 err
= __send_command(first_peer_device(device
)->connection
, device
->vnr
, sock
, P_DATA
, sizeof(*p
) + dgs
, NULL
, req
->i
.size
);
1641 /* For protocol A, we have to memcpy the payload into
1642 * socket buffers, as we may complete right away
1643 * as soon as we handed it over to tcp, at which point the data
1644 * pages may become invalid.
1646 * For data-integrity enabled, we copy it as well, so we can be
1647 * sure that even if the bio pages may still be modified, it
1648 * won't change the data on the wire, thus if the digest checks
1649 * out ok after sending on this side, but does not fit on the
1650 * receiving side, we sure have detected corruption elsewhere.
1652 if (!(req
->rq_state
& (RQ_EXP_RECEIVE_ACK
| RQ_EXP_WRITE_ACK
)) || dgs
)
1653 err
= _drbd_send_bio(device
, req
->master_bio
);
1655 err
= _drbd_send_zc_bio(device
, req
->master_bio
);
1657 /* double check digest, sometimes buffers have been modified in flight. */
1658 if (dgs
> 0 && dgs
<= 64) {
1659 /* 64 byte, 512 bit, is the largest digest size
1660 * currently supported in kernel crypto. */
1661 unsigned char digest
[64];
1662 drbd_csum_bio(device
, first_peer_device(device
)->connection
->integrity_tfm
, req
->master_bio
, digest
);
1663 if (memcmp(p
+ 1, digest
, dgs
)) {
1665 "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
1666 (unsigned long long)req
->i
.sector
, req
->i
.size
);
1668 } /* else if (dgs > 64) {
1669 ... Be noisy about digest too large ...
1672 mutex_unlock(&sock
->mutex
); /* locked by drbd_prepare_command() */
1677 /* answer packet, used to send data back for read requests:
1678 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
1679 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
1681 int drbd_send_block(struct drbd_device
*device
, enum drbd_packet cmd
,
1682 struct drbd_peer_request
*peer_req
)
1684 struct drbd_socket
*sock
;
1689 sock
= &first_peer_device(device
)->connection
->data
;
1690 p
= drbd_prepare_command(device
, sock
);
1692 dgs
= first_peer_device(device
)->connection
->integrity_tfm
?
1693 crypto_hash_digestsize(first_peer_device(device
)->connection
->integrity_tfm
) : 0;
1697 p
->sector
= cpu_to_be64(peer_req
->i
.sector
);
1698 p
->block_id
= peer_req
->block_id
;
1699 p
->seq_num
= 0; /* unused */
1702 drbd_csum_ee(device
, first_peer_device(device
)->connection
->integrity_tfm
, peer_req
, p
+ 1);
1703 err
= __send_command(first_peer_device(device
)->connection
, device
->vnr
, sock
, cmd
, sizeof(*p
) + dgs
, NULL
, peer_req
->i
.size
);
1705 err
= _drbd_send_zc_ee(device
, peer_req
);
1706 mutex_unlock(&sock
->mutex
); /* locked by drbd_prepare_command() */
1711 int drbd_send_out_of_sync(struct drbd_device
*device
, struct drbd_request
*req
)
1713 struct drbd_socket
*sock
;
1714 struct p_block_desc
*p
;
1716 sock
= &first_peer_device(device
)->connection
->data
;
1717 p
= drbd_prepare_command(device
, sock
);
1720 p
->sector
= cpu_to_be64(req
->i
.sector
);
1721 p
->blksize
= cpu_to_be32(req
->i
.size
);
1722 return drbd_send_command(device
, sock
, P_OUT_OF_SYNC
, sizeof(*p
), NULL
, 0);
1726 drbd_send distinguishes two cases:
1728 Packets sent via the data socket "sock"
1729 and packets sent via the meta data socket "msock"
1732 -----------------+-------------------------+------------------------------
1733 timeout conf.timeout / 2 conf.timeout / 2
1734 timeout action send a ping via msock Abort communication
1735 and close all sockets
1739 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
1741 int drbd_send(struct drbd_connection
*connection
, struct socket
*sock
,
1742 void *buf
, size_t size
, unsigned msg_flags
)
1751 /* THINK if (signal_pending) return ... ? */
1756 msg
.msg_name
= NULL
;
1757 msg
.msg_namelen
= 0;
1758 msg
.msg_control
= NULL
;
1759 msg
.msg_controllen
= 0;
1760 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
1762 if (sock
== connection
->data
.socket
) {
1764 connection
->ko_count
= rcu_dereference(connection
->net_conf
)->ko_count
;
1766 drbd_update_congested(connection
);
1770 * tcp_sendmsg does _not_ use its size parameter at all ?
1772 * -EAGAIN on timeout, -EINTR on signal.
1775 * do we need to block DRBD_SIG if sock == &meta.socket ??
1776 * otherwise wake_asender() might interrupt some send_*Ack !
1778 rv
= kernel_sendmsg(sock
, &msg
, &iov
, 1, size
);
1779 if (rv
== -EAGAIN
) {
1780 if (we_should_drop_the_connection(connection
, sock
))
1786 flush_signals(current
);
1794 } while (sent
< size
);
1796 if (sock
== connection
->data
.socket
)
1797 clear_bit(NET_CONGESTED
, &connection
->flags
);
1800 if (rv
!= -EAGAIN
) {
1801 conn_err(connection
, "%s_sendmsg returned %d\n",
1802 sock
== connection
->meta
.socket
? "msock" : "sock",
1804 conn_request_state(connection
, NS(conn
, C_BROKEN_PIPE
), CS_HARD
);
1806 conn_request_state(connection
, NS(conn
, C_TIMEOUT
), CS_HARD
);
1813 * drbd_send_all - Send an entire buffer
1815 * Returns 0 upon success and a negative error value otherwise.
1817 int drbd_send_all(struct drbd_connection
*connection
, struct socket
*sock
, void *buffer
,
1818 size_t size
, unsigned msg_flags
)
1822 err
= drbd_send(connection
, sock
, buffer
, size
, msg_flags
);
1830 static int drbd_open(struct block_device
*bdev
, fmode_t mode
)
1832 struct drbd_device
*device
= bdev
->bd_disk
->private_data
;
1833 unsigned long flags
;
1836 mutex_lock(&drbd_main_mutex
);
1837 spin_lock_irqsave(&first_peer_device(device
)->connection
->req_lock
, flags
);
1838 /* to have a stable device->state.role
1839 * and no race with updating open_cnt */
1841 if (device
->state
.role
!= R_PRIMARY
) {
1842 if (mode
& FMODE_WRITE
)
1844 else if (!allow_oos
)
1850 spin_unlock_irqrestore(&first_peer_device(device
)->connection
->req_lock
, flags
);
1851 mutex_unlock(&drbd_main_mutex
);
1856 static void drbd_release(struct gendisk
*gd
, fmode_t mode
)
1858 struct drbd_device
*device
= gd
->private_data
;
1859 mutex_lock(&drbd_main_mutex
);
1861 mutex_unlock(&drbd_main_mutex
);
1864 static void drbd_set_defaults(struct drbd_device
*device
)
1866 /* Beware! The actual layout differs
1867 * between big endian and little endian */
1868 device
->state
= (union drbd_dev_state
) {
1869 { .role
= R_SECONDARY
,
1871 .conn
= C_STANDALONE
,
1877 void drbd_init_set_defaults(struct drbd_device
*device
)
1879 /* the memset(,0,) did most of this.
1880 * note: only assignments, no allocation in here */
1882 drbd_set_defaults(device
);
1884 atomic_set(&device
->ap_bio_cnt
, 0);
1885 atomic_set(&device
->ap_pending_cnt
, 0);
1886 atomic_set(&device
->rs_pending_cnt
, 0);
1887 atomic_set(&device
->unacked_cnt
, 0);
1888 atomic_set(&device
->local_cnt
, 0);
1889 atomic_set(&device
->pp_in_use_by_net
, 0);
1890 atomic_set(&device
->rs_sect_in
, 0);
1891 atomic_set(&device
->rs_sect_ev
, 0);
1892 atomic_set(&device
->ap_in_flight
, 0);
1893 atomic_set(&device
->md_io_in_use
, 0);
1895 mutex_init(&device
->own_state_mutex
);
1896 device
->state_mutex
= &device
->own_state_mutex
;
1898 spin_lock_init(&device
->al_lock
);
1899 spin_lock_init(&device
->peer_seq_lock
);
1901 INIT_LIST_HEAD(&device
->active_ee
);
1902 INIT_LIST_HEAD(&device
->sync_ee
);
1903 INIT_LIST_HEAD(&device
->done_ee
);
1904 INIT_LIST_HEAD(&device
->read_ee
);
1905 INIT_LIST_HEAD(&device
->net_ee
);
1906 INIT_LIST_HEAD(&device
->resync_reads
);
1907 INIT_LIST_HEAD(&device
->resync_work
.list
);
1908 INIT_LIST_HEAD(&device
->unplug_work
.list
);
1909 INIT_LIST_HEAD(&device
->go_diskless
.list
);
1910 INIT_LIST_HEAD(&device
->md_sync_work
.list
);
1911 INIT_LIST_HEAD(&device
->start_resync_work
.list
);
1912 INIT_LIST_HEAD(&device
->bm_io_work
.w
.list
);
1914 device
->resync_work
.cb
= w_resync_timer
;
1915 device
->unplug_work
.cb
= w_send_write_hint
;
1916 device
->go_diskless
.cb
= w_go_diskless
;
1917 device
->md_sync_work
.cb
= w_md_sync
;
1918 device
->bm_io_work
.w
.cb
= w_bitmap_io
;
1919 device
->start_resync_work
.cb
= w_start_resync
;
1921 device
->resync_work
.device
= device
;
1922 device
->unplug_work
.device
= device
;
1923 device
->go_diskless
.device
= device
;
1924 device
->md_sync_work
.device
= device
;
1925 device
->bm_io_work
.w
.device
= device
;
1926 device
->start_resync_work
.device
= device
;
1928 init_timer(&device
->resync_timer
);
1929 init_timer(&device
->md_sync_timer
);
1930 init_timer(&device
->start_resync_timer
);
1931 init_timer(&device
->request_timer
);
1932 device
->resync_timer
.function
= resync_timer_fn
;
1933 device
->resync_timer
.data
= (unsigned long) device
;
1934 device
->md_sync_timer
.function
= md_sync_timer_fn
;
1935 device
->md_sync_timer
.data
= (unsigned long) device
;
1936 device
->start_resync_timer
.function
= start_resync_timer_fn
;
1937 device
->start_resync_timer
.data
= (unsigned long) device
;
1938 device
->request_timer
.function
= request_timer_fn
;
1939 device
->request_timer
.data
= (unsigned long) device
;
1941 init_waitqueue_head(&device
->misc_wait
);
1942 init_waitqueue_head(&device
->state_wait
);
1943 init_waitqueue_head(&device
->ee_wait
);
1944 init_waitqueue_head(&device
->al_wait
);
1945 init_waitqueue_head(&device
->seq_wait
);
1947 device
->resync_wenr
= LC_FREE
;
1948 device
->peer_max_bio_size
= DRBD_MAX_BIO_SIZE_SAFE
;
1949 device
->local_max_bio_size
= DRBD_MAX_BIO_SIZE_SAFE
;
1952 void drbd_device_cleanup(struct drbd_device
*device
)
1955 if (first_peer_device(device
)->connection
->receiver
.t_state
!= NONE
)
1956 dev_err(DEV
, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
1957 first_peer_device(device
)->connection
->receiver
.t_state
);
1959 device
->al_writ_cnt
=
1960 device
->bm_writ_cnt
=
1968 device
->rs_failed
= 0;
1969 device
->rs_last_events
= 0;
1970 device
->rs_last_sect_ev
= 0;
1971 for (i
= 0; i
< DRBD_SYNC_MARKS
; i
++) {
1972 device
->rs_mark_left
[i
] = 0;
1973 device
->rs_mark_time
[i
] = 0;
1975 D_ASSERT(first_peer_device(device
)->connection
->net_conf
== NULL
);
1977 drbd_set_my_capacity(device
, 0);
1978 if (device
->bitmap
) {
1979 /* maybe never allocated. */
1980 drbd_bm_resize(device
, 0, 1);
1981 drbd_bm_cleanup(device
);
1984 drbd_free_bc(device
->ldev
);
1985 device
->ldev
= NULL
;
1987 clear_bit(AL_SUSPENDED
, &device
->flags
);
1989 D_ASSERT(list_empty(&device
->active_ee
));
1990 D_ASSERT(list_empty(&device
->sync_ee
));
1991 D_ASSERT(list_empty(&device
->done_ee
));
1992 D_ASSERT(list_empty(&device
->read_ee
));
1993 D_ASSERT(list_empty(&device
->net_ee
));
1994 D_ASSERT(list_empty(&device
->resync_reads
));
1995 D_ASSERT(list_empty(&first_peer_device(device
)->connection
->sender_work
.q
));
1996 D_ASSERT(list_empty(&device
->resync_work
.list
));
1997 D_ASSERT(list_empty(&device
->unplug_work
.list
));
1998 D_ASSERT(list_empty(&device
->go_diskless
.list
));
2000 drbd_set_defaults(device
);
2004 static void drbd_destroy_mempools(void)
2008 while (drbd_pp_pool
) {
2009 page
= drbd_pp_pool
;
2010 drbd_pp_pool
= (struct page
*)page_private(page
);
2015 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2017 if (drbd_md_io_bio_set
)
2018 bioset_free(drbd_md_io_bio_set
);
2019 if (drbd_md_io_page_pool
)
2020 mempool_destroy(drbd_md_io_page_pool
);
2021 if (drbd_ee_mempool
)
2022 mempool_destroy(drbd_ee_mempool
);
2023 if (drbd_request_mempool
)
2024 mempool_destroy(drbd_request_mempool
);
2026 kmem_cache_destroy(drbd_ee_cache
);
2027 if (drbd_request_cache
)
2028 kmem_cache_destroy(drbd_request_cache
);
2029 if (drbd_bm_ext_cache
)
2030 kmem_cache_destroy(drbd_bm_ext_cache
);
2031 if (drbd_al_ext_cache
)
2032 kmem_cache_destroy(drbd_al_ext_cache
);
2034 drbd_md_io_bio_set
= NULL
;
2035 drbd_md_io_page_pool
= NULL
;
2036 drbd_ee_mempool
= NULL
;
2037 drbd_request_mempool
= NULL
;
2038 drbd_ee_cache
= NULL
;
2039 drbd_request_cache
= NULL
;
2040 drbd_bm_ext_cache
= NULL
;
2041 drbd_al_ext_cache
= NULL
;
2046 static int drbd_create_mempools(void)
2049 const int number
= (DRBD_MAX_BIO_SIZE
/PAGE_SIZE
) * minor_count
;
2052 /* prepare our caches and mempools */
2053 drbd_request_mempool
= NULL
;
2054 drbd_ee_cache
= NULL
;
2055 drbd_request_cache
= NULL
;
2056 drbd_bm_ext_cache
= NULL
;
2057 drbd_al_ext_cache
= NULL
;
2058 drbd_pp_pool
= NULL
;
2059 drbd_md_io_page_pool
= NULL
;
2060 drbd_md_io_bio_set
= NULL
;
2063 drbd_request_cache
= kmem_cache_create(
2064 "drbd_req", sizeof(struct drbd_request
), 0, 0, NULL
);
2065 if (drbd_request_cache
== NULL
)
2068 drbd_ee_cache
= kmem_cache_create(
2069 "drbd_ee", sizeof(struct drbd_peer_request
), 0, 0, NULL
);
2070 if (drbd_ee_cache
== NULL
)
2073 drbd_bm_ext_cache
= kmem_cache_create(
2074 "drbd_bm", sizeof(struct bm_extent
), 0, 0, NULL
);
2075 if (drbd_bm_ext_cache
== NULL
)
2078 drbd_al_ext_cache
= kmem_cache_create(
2079 "drbd_al", sizeof(struct lc_element
), 0, 0, NULL
);
2080 if (drbd_al_ext_cache
== NULL
)
2084 drbd_md_io_bio_set
= bioset_create(DRBD_MIN_POOL_PAGES
, 0);
2085 if (drbd_md_io_bio_set
== NULL
)
2088 drbd_md_io_page_pool
= mempool_create_page_pool(DRBD_MIN_POOL_PAGES
, 0);
2089 if (drbd_md_io_page_pool
== NULL
)
2092 drbd_request_mempool
= mempool_create(number
,
2093 mempool_alloc_slab
, mempool_free_slab
, drbd_request_cache
);
2094 if (drbd_request_mempool
== NULL
)
2097 drbd_ee_mempool
= mempool_create(number
,
2098 mempool_alloc_slab
, mempool_free_slab
, drbd_ee_cache
);
2099 if (drbd_ee_mempool
== NULL
)
2102 /* drbd's page pool */
2103 spin_lock_init(&drbd_pp_lock
);
2105 for (i
= 0; i
< number
; i
++) {
2106 page
= alloc_page(GFP_HIGHUSER
);
2109 set_page_private(page
, (unsigned long)drbd_pp_pool
);
2110 drbd_pp_pool
= page
;
2112 drbd_pp_vacant
= number
;
2117 drbd_destroy_mempools(); /* in case we allocated some */
2121 static int drbd_notify_sys(struct notifier_block
*this, unsigned long code
,
2124 /* just so we have it. you never know what interesting things we
2125 * might want to do here some day...
2131 static struct notifier_block drbd_notifier
= {
2132 .notifier_call
= drbd_notify_sys
,
2135 static void drbd_release_all_peer_reqs(struct drbd_device
*device
)
2139 rr
= drbd_free_peer_reqs(device
, &device
->active_ee
);
2141 dev_err(DEV
, "%d EEs in active list found!\n", rr
);
2143 rr
= drbd_free_peer_reqs(device
, &device
->sync_ee
);
2145 dev_err(DEV
, "%d EEs in sync list found!\n", rr
);
2147 rr
= drbd_free_peer_reqs(device
, &device
->read_ee
);
2149 dev_err(DEV
, "%d EEs in read list found!\n", rr
);
2151 rr
= drbd_free_peer_reqs(device
, &device
->done_ee
);
2153 dev_err(DEV
, "%d EEs in done list found!\n", rr
);
2155 rr
= drbd_free_peer_reqs(device
, &device
->net_ee
);
2157 dev_err(DEV
, "%d EEs in net list found!\n", rr
);
2160 /* caution. no locking. */
2161 void drbd_destroy_device(struct kref
*kref
)
2163 struct drbd_device
*device
= container_of(kref
, struct drbd_device
, kref
);
2164 struct drbd_connection
*connection
= first_peer_device(device
)->connection
;
2166 del_timer_sync(&device
->request_timer
);
2168 /* paranoia asserts */
2169 D_ASSERT(device
->open_cnt
== 0);
2170 /* end paranoia asserts */
2172 /* cleanup stuff that may have been allocated during
2173 * device (re-)configuration or state changes */
2175 if (device
->this_bdev
)
2176 bdput(device
->this_bdev
);
2178 drbd_free_bc(device
->ldev
);
2179 device
->ldev
= NULL
;
2181 drbd_release_all_peer_reqs(device
);
2183 lc_destroy(device
->act_log
);
2184 lc_destroy(device
->resync
);
2186 kfree(device
->p_uuid
);
2187 /* device->p_uuid = NULL; */
2189 if (device
->bitmap
) /* should no longer be there. */
2190 drbd_bm_cleanup(device
);
2191 __free_page(device
->md_io_page
);
2192 put_disk(device
->vdisk
);
2193 blk_cleanup_queue(device
->rq_queue
);
2194 kfree(device
->rs_plan_s
);
2195 kfree(first_peer_device(device
));
2198 kref_put(&connection
->kref
, drbd_destroy_connection
);
2201 /* One global retry thread, if we need to push back some bio and have it
2202 * reinserted through our make request function.
2204 static struct retry_worker
{
2205 struct workqueue_struct
*wq
;
2206 struct work_struct worker
;
2209 struct list_head writes
;
2212 static void do_retry(struct work_struct
*ws
)
2214 struct retry_worker
*retry
= container_of(ws
, struct retry_worker
, worker
);
2216 struct drbd_request
*req
, *tmp
;
2218 spin_lock_irq(&retry
->lock
);
2219 list_splice_init(&retry
->writes
, &writes
);
2220 spin_unlock_irq(&retry
->lock
);
2222 list_for_each_entry_safe(req
, tmp
, &writes
, tl_requests
) {
2223 struct drbd_device
*device
= req
->w
.device
;
2224 struct bio
*bio
= req
->master_bio
;
2225 unsigned long start_time
= req
->start_time
;
2229 expect(atomic_read(&req
->completion_ref
) == 0) &&
2230 expect(req
->rq_state
& RQ_POSTPONED
) &&
2231 expect((req
->rq_state
& RQ_LOCAL_PENDING
) == 0 ||
2232 (req
->rq_state
& RQ_LOCAL_ABORTED
) != 0);
2235 dev_err(DEV
, "req=%p completion_ref=%d rq_state=%x\n",
2236 req
, atomic_read(&req
->completion_ref
),
2239 /* We still need to put one kref associated with the
2240 * "completion_ref" going zero in the code path that queued it
2241 * here. The request object may still be referenced by a
2242 * frozen local req->private_bio, in case we force-detached.
2244 kref_put(&req
->kref
, drbd_req_destroy
);
2246 /* A single suspended or otherwise blocking device may stall
2247 * all others as well. Fortunately, this code path is to
2248 * recover from a situation that "should not happen":
2249 * concurrent writes in multi-primary setup.
2250 * In a "normal" lifecycle, this workqueue is supposed to be
2251 * destroyed without ever doing anything.
2252 * If it turns out to be an issue anyways, we can do per
2253 * resource (replication group) or per device (minor) retry
2254 * workqueues instead.
2257 /* We are not just doing generic_make_request(),
2258 * as we want to keep the start_time information. */
2260 __drbd_make_request(device
, bio
, start_time
);
2264 void drbd_restart_request(struct drbd_request
*req
)
2266 unsigned long flags
;
2267 spin_lock_irqsave(&retry
.lock
, flags
);
2268 list_move_tail(&req
->tl_requests
, &retry
.writes
);
2269 spin_unlock_irqrestore(&retry
.lock
, flags
);
2271 /* Drop the extra reference that would otherwise
2272 * have been dropped by complete_master_bio.
2273 * do_retry() needs to grab a new one. */
2274 dec_ap_bio(req
->w
.device
);
2276 queue_work(retry
.wq
, &retry
.worker
);
2280 static void drbd_cleanup(void)
2283 struct drbd_device
*device
;
2284 struct drbd_connection
*connection
, *tmp
;
2286 unregister_reboot_notifier(&drbd_notifier
);
2288 /* first remove proc,
2289 * drbdsetup uses it's presence to detect
2290 * whether DRBD is loaded.
2291 * If we would get stuck in proc removal,
2292 * but have netlink already deregistered,
2293 * some drbdsetup commands may wait forever
2297 remove_proc_entry("drbd", NULL
);
2300 destroy_workqueue(retry
.wq
);
2302 drbd_genl_unregister();
2304 idr_for_each_entry(&drbd_devices
, device
, i
) {
2305 idr_remove(&drbd_devices
, device_to_minor(device
));
2306 idr_remove(&first_peer_device(device
)->connection
->volumes
, device
->vnr
);
2307 destroy_workqueue(device
->submit
.wq
);
2308 del_gendisk(device
->vdisk
);
2309 /* synchronize_rcu(); No other threads running at this point */
2310 kref_put(&device
->kref
, drbd_destroy_device
);
2313 /* not _rcu since, no other updater anymore. Genl already unregistered */
2314 list_for_each_entry_safe(connection
, tmp
, &drbd_connections
, connections
) {
2315 list_del(&connection
->connections
); /* not _rcu no proc, not other threads */
2316 /* synchronize_rcu(); */
2317 kref_put(&connection
->kref
, drbd_destroy_connection
);
2320 drbd_destroy_mempools();
2321 unregister_blkdev(DRBD_MAJOR
, "drbd");
2323 idr_destroy(&drbd_devices
);
2325 printk(KERN_INFO
"drbd: module cleanup done.\n");
2329 * drbd_congested() - Callback for the flusher thread
2330 * @congested_data: User data
2331 * @bdi_bits: Bits the BDI flusher thread is currently interested in
2333 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
2335 static int drbd_congested(void *congested_data
, int bdi_bits
)
2337 struct drbd_device
*device
= congested_data
;
2338 struct request_queue
*q
;
2342 if (!may_inc_ap_bio(device
)) {
2343 /* DRBD has frozen IO */
2349 if (test_bit(CALLBACK_PENDING
, &first_peer_device(device
)->connection
->flags
)) {
2350 r
|= (1 << BDI_async_congested
);
2351 /* Without good local data, we would need to read from remote,
2352 * and that would need the worker thread as well, which is
2353 * currently blocked waiting for that usermode helper to
2356 if (!get_ldev_if_state(device
, D_UP_TO_DATE
))
2357 r
|= (1 << BDI_sync_congested
);
2365 if (get_ldev(device
)) {
2366 q
= bdev_get_queue(device
->ldev
->backing_bdev
);
2367 r
= bdi_congested(&q
->backing_dev_info
, bdi_bits
);
2373 if (bdi_bits
& (1 << BDI_async_congested
) &&
2374 test_bit(NET_CONGESTED
, &first_peer_device(device
)->connection
->flags
)) {
2375 r
|= (1 << BDI_async_congested
);
2376 reason
= reason
== 'b' ? 'a' : 'n';
2380 device
->congestion_reason
= reason
;
2384 static void drbd_init_workqueue(struct drbd_work_queue
* wq
)
2386 spin_lock_init(&wq
->q_lock
);
2387 INIT_LIST_HEAD(&wq
->q
);
2388 init_waitqueue_head(&wq
->q_wait
);
2391 struct drbd_connection
*conn_get_by_name(const char *name
)
2393 struct drbd_connection
*connection
;
2395 if (!name
|| !name
[0])
2399 list_for_each_entry_rcu(connection
, &drbd_connections
, connections
) {
2400 if (!strcmp(connection
->name
, name
)) {
2401 kref_get(&connection
->kref
);
2411 struct drbd_connection
*conn_get_by_addrs(void *my_addr
, int my_addr_len
,
2412 void *peer_addr
, int peer_addr_len
)
2414 struct drbd_connection
*connection
;
2417 list_for_each_entry_rcu(connection
, &drbd_connections
, connections
) {
2418 if (connection
->my_addr_len
== my_addr_len
&&
2419 connection
->peer_addr_len
== peer_addr_len
&&
2420 !memcmp(&connection
->my_addr
, my_addr
, my_addr_len
) &&
2421 !memcmp(&connection
->peer_addr
, peer_addr
, peer_addr_len
)) {
2422 kref_get(&connection
->kref
);
2432 static int drbd_alloc_socket(struct drbd_socket
*socket
)
2434 socket
->rbuf
= (void *) __get_free_page(GFP_KERNEL
);
2437 socket
->sbuf
= (void *) __get_free_page(GFP_KERNEL
);
2443 static void drbd_free_socket(struct drbd_socket
*socket
)
2445 free_page((unsigned long) socket
->sbuf
);
2446 free_page((unsigned long) socket
->rbuf
);
2449 void conn_free_crypto(struct drbd_connection
*connection
)
2451 drbd_free_sock(connection
);
2453 crypto_free_hash(connection
->csums_tfm
);
2454 crypto_free_hash(connection
->verify_tfm
);
2455 crypto_free_hash(connection
->cram_hmac_tfm
);
2456 crypto_free_hash(connection
->integrity_tfm
);
2457 crypto_free_hash(connection
->peer_integrity_tfm
);
2458 kfree(connection
->int_dig_in
);
2459 kfree(connection
->int_dig_vv
);
2461 connection
->csums_tfm
= NULL
;
2462 connection
->verify_tfm
= NULL
;
2463 connection
->cram_hmac_tfm
= NULL
;
2464 connection
->integrity_tfm
= NULL
;
2465 connection
->peer_integrity_tfm
= NULL
;
2466 connection
->int_dig_in
= NULL
;
2467 connection
->int_dig_vv
= NULL
;
2470 int set_resource_options(struct drbd_connection
*connection
, struct res_opts
*res_opts
)
2472 cpumask_var_t new_cpu_mask
;
2475 if (!zalloc_cpumask_var(&new_cpu_mask
, GFP_KERNEL
))
2478 retcode = ERR_NOMEM;
2479 drbd_msg_put_info("unable to allocate cpumask");
2482 /* silently ignore cpu mask on UP kernel */
2483 if (nr_cpu_ids
> 1 && res_opts
->cpu_mask
[0] != 0) {
2484 /* FIXME: Get rid of constant 32 here */
2485 err
= bitmap_parse(res_opts
->cpu_mask
, 32,
2486 cpumask_bits(new_cpu_mask
), nr_cpu_ids
);
2488 conn_warn(connection
, "bitmap_parse() failed with %d\n", err
);
2489 /* retcode = ERR_CPU_MASK_PARSE; */
2493 connection
->res_opts
= *res_opts
;
2494 if (!cpumask_equal(connection
->cpu_mask
, new_cpu_mask
)) {
2495 cpumask_copy(connection
->cpu_mask
, new_cpu_mask
);
2496 drbd_calc_cpu_mask(connection
);
2497 connection
->receiver
.reset_cpu_mask
= 1;
2498 connection
->asender
.reset_cpu_mask
= 1;
2499 connection
->worker
.reset_cpu_mask
= 1;
2504 free_cpumask_var(new_cpu_mask
);
2509 /* caller must be under genl_lock() */
2510 struct drbd_connection
*conn_create(const char *name
, struct res_opts
*res_opts
)
2512 struct drbd_connection
*connection
;
2514 connection
= kzalloc(sizeof(struct drbd_connection
), GFP_KERNEL
);
2518 connection
->name
= kstrdup(name
, GFP_KERNEL
);
2519 if (!connection
->name
)
2522 if (drbd_alloc_socket(&connection
->data
))
2524 if (drbd_alloc_socket(&connection
->meta
))
2527 if (!zalloc_cpumask_var(&connection
->cpu_mask
, GFP_KERNEL
))
2530 if (set_resource_options(connection
, res_opts
))
2533 connection
->current_epoch
= kzalloc(sizeof(struct drbd_epoch
), GFP_KERNEL
);
2534 if (!connection
->current_epoch
)
2537 INIT_LIST_HEAD(&connection
->transfer_log
);
2539 INIT_LIST_HEAD(&connection
->current_epoch
->list
);
2540 connection
->epochs
= 1;
2541 spin_lock_init(&connection
->epoch_lock
);
2542 connection
->write_ordering
= WO_bdev_flush
;
2544 connection
->send
.seen_any_write_yet
= false;
2545 connection
->send
.current_epoch_nr
= 0;
2546 connection
->send
.current_epoch_writes
= 0;
2548 connection
->cstate
= C_STANDALONE
;
2549 mutex_init(&connection
->cstate_mutex
);
2550 spin_lock_init(&connection
->req_lock
);
2551 mutex_init(&connection
->conf_update
);
2552 init_waitqueue_head(&connection
->ping_wait
);
2553 idr_init(&connection
->volumes
);
2555 drbd_init_workqueue(&connection
->sender_work
);
2556 mutex_init(&connection
->data
.mutex
);
2557 mutex_init(&connection
->meta
.mutex
);
2559 drbd_thread_init(connection
, &connection
->receiver
, drbdd_init
, "receiver");
2560 drbd_thread_init(connection
, &connection
->worker
, drbd_worker
, "worker");
2561 drbd_thread_init(connection
, &connection
->asender
, drbd_asender
, "asender");
2563 kref_init(&connection
->kref
);
2564 list_add_tail_rcu(&connection
->connections
, &drbd_connections
);
2569 kfree(connection
->current_epoch
);
2570 free_cpumask_var(connection
->cpu_mask
);
2571 drbd_free_socket(&connection
->meta
);
2572 drbd_free_socket(&connection
->data
);
2573 kfree(connection
->name
);
2579 void drbd_destroy_connection(struct kref
*kref
)
2581 struct drbd_connection
*connection
= container_of(kref
, struct drbd_connection
, kref
);
2583 if (atomic_read(&connection
->current_epoch
->epoch_size
) != 0)
2584 conn_err(connection
, "epoch_size:%d\n", atomic_read(&connection
->current_epoch
->epoch_size
));
2585 kfree(connection
->current_epoch
);
2587 idr_destroy(&connection
->volumes
);
2589 free_cpumask_var(connection
->cpu_mask
);
2590 drbd_free_socket(&connection
->meta
);
2591 drbd_free_socket(&connection
->data
);
2592 kfree(connection
->name
);
2593 kfree(connection
->int_dig_in
);
2594 kfree(connection
->int_dig_vv
);
2598 static int init_submitter(struct drbd_device
*device
)
2600 /* opencoded create_singlethread_workqueue(),
2601 * to be able to say "drbd%d", ..., minor */
2602 device
->submit
.wq
= alloc_workqueue("drbd%u_submit",
2603 WQ_UNBOUND
| WQ_MEM_RECLAIM
, 1, device
->minor
);
2604 if (!device
->submit
.wq
)
2607 INIT_WORK(&device
->submit
.worker
, do_submit
);
2608 spin_lock_init(&device
->submit
.lock
);
2609 INIT_LIST_HEAD(&device
->submit
.writes
);
2613 enum drbd_ret_code
drbd_create_minor(struct drbd_connection
*connection
, unsigned int minor
, int vnr
)
2615 struct drbd_device
*device
;
2616 struct drbd_peer_device
*peer_device
;
2617 struct gendisk
*disk
;
2618 struct request_queue
*q
;
2620 int minor_got
= minor
;
2621 enum drbd_ret_code err
= ERR_NOMEM
;
2623 device
= minor_to_device(minor
);
2625 return ERR_MINOR_EXISTS
;
2627 /* GFP_KERNEL, we are outside of all write-out paths */
2628 device
= kzalloc(sizeof(struct drbd_device
), GFP_KERNEL
);
2631 peer_device
= kzalloc(sizeof(struct drbd_peer_device
), GFP_KERNEL
);
2633 goto out_no_peer_device
;
2635 INIT_LIST_HEAD(&device
->peer_devices
);
2636 list_add(&peer_device
->peer_devices
, &device
->peer_devices
);
2637 kref_get(&connection
->kref
);
2638 peer_device
->connection
= connection
;
2639 peer_device
->device
= device
;
2641 device
->minor
= minor
;
2644 drbd_init_set_defaults(device
);
2646 q
= blk_alloc_queue(GFP_KERNEL
);
2649 device
->rq_queue
= q
;
2650 q
->queuedata
= device
;
2652 disk
= alloc_disk(1);
2655 device
->vdisk
= disk
;
2657 set_disk_ro(disk
, true);
2660 disk
->major
= DRBD_MAJOR
;
2661 disk
->first_minor
= minor
;
2662 disk
->fops
= &drbd_ops
;
2663 sprintf(disk
->disk_name
, "drbd%d", minor
);
2664 disk
->private_data
= device
;
2666 device
->this_bdev
= bdget(MKDEV(DRBD_MAJOR
, minor
));
2667 /* we have no partitions. we contain only ourselves. */
2668 device
->this_bdev
->bd_contains
= device
->this_bdev
;
2670 q
->backing_dev_info
.congested_fn
= drbd_congested
;
2671 q
->backing_dev_info
.congested_data
= device
;
2673 blk_queue_make_request(q
, drbd_make_request
);
2674 blk_queue_flush(q
, REQ_FLUSH
| REQ_FUA
);
2675 /* Setting the max_hw_sectors to an odd value of 8kibyte here
2676 This triggers a max_bio_size message upon first attach or connect */
2677 blk_queue_max_hw_sectors(q
, DRBD_MAX_BIO_SIZE_SAFE
>> 8);
2678 blk_queue_bounce_limit(q
, BLK_BOUNCE_ANY
);
2679 blk_queue_merge_bvec(q
, drbd_merge_bvec
);
2680 q
->queue_lock
= &first_peer_device(device
)->connection
->req_lock
; /* needed since we use */
2682 device
->md_io_page
= alloc_page(GFP_KERNEL
);
2683 if (!device
->md_io_page
)
2684 goto out_no_io_page
;
2686 if (drbd_bm_init(device
))
2688 device
->read_requests
= RB_ROOT
;
2689 device
->write_requests
= RB_ROOT
;
2691 minor_got
= idr_alloc(&drbd_devices
, device
, minor
, minor
+ 1, GFP_KERNEL
);
2692 if (minor_got
< 0) {
2693 if (minor_got
== -ENOSPC
) {
2694 err
= ERR_MINOR_EXISTS
;
2695 drbd_msg_put_info("requested minor exists already");
2697 goto out_no_minor_idr
;
2700 vnr_got
= idr_alloc(&connection
->volumes
, device
, vnr
, vnr
+ 1, GFP_KERNEL
);
2702 if (vnr_got
== -ENOSPC
) {
2703 err
= ERR_INVALID_REQUEST
;
2704 drbd_msg_put_info("requested volume exists already");
2706 goto out_idr_remove_minor
;
2709 if (init_submitter(device
)) {
2711 drbd_msg_put_info("unable to create submit workqueue");
2712 goto out_idr_remove_vol
;
2716 kref_init(&device
->kref
); /* one ref for both idrs and the the add_disk */
2718 /* inherit the connection state */
2719 device
->state
.conn
= connection
->cstate
;
2720 if (device
->state
.conn
== C_WF_REPORT_PARAMS
)
2721 drbd_connected(device
);
2726 idr_remove(&connection
->volumes
, vnr_got
);
2727 out_idr_remove_minor
:
2728 idr_remove(&drbd_devices
, minor_got
);
2731 drbd_bm_cleanup(device
);
2733 __free_page(device
->md_io_page
);
2737 blk_cleanup_queue(q
);
2739 kref_put(&connection
->kref
, drbd_destroy_connection
);
2745 int __init
drbd_init(void)
2749 if (minor_count
< DRBD_MINOR_COUNT_MIN
|| minor_count
> DRBD_MINOR_COUNT_MAX
) {
2751 "drbd: invalid minor_count (%d)\n", minor_count
);
2755 minor_count
= DRBD_MINOR_COUNT_DEF
;
2759 err
= register_blkdev(DRBD_MAJOR
, "drbd");
2762 "drbd: unable to register block device major %d\n",
2767 register_reboot_notifier(&drbd_notifier
);
2770 * allocate all necessary structs
2772 init_waitqueue_head(&drbd_pp_wait
);
2774 drbd_proc
= NULL
; /* play safe for drbd_cleanup */
2775 idr_init(&drbd_devices
);
2777 rwlock_init(&global_state_lock
);
2778 INIT_LIST_HEAD(&drbd_connections
);
2780 err
= drbd_genl_register();
2782 printk(KERN_ERR
"drbd: unable to register generic netlink family\n");
2786 err
= drbd_create_mempools();
2791 drbd_proc
= proc_create_data("drbd", S_IFREG
| S_IRUGO
, NULL
, &drbd_proc_fops
, NULL
);
2793 printk(KERN_ERR
"drbd: unable to register proc file\n");
2797 retry
.wq
= create_singlethread_workqueue("drbd-reissue");
2799 printk(KERN_ERR
"drbd: unable to create retry workqueue\n");
2802 INIT_WORK(&retry
.worker
, do_retry
);
2803 spin_lock_init(&retry
.lock
);
2804 INIT_LIST_HEAD(&retry
.writes
);
2806 printk(KERN_INFO
"drbd: initialized. "
2807 "Version: " REL_VERSION
" (api:%d/proto:%d-%d)\n",
2808 API_VERSION
, PRO_VERSION_MIN
, PRO_VERSION_MAX
);
2809 printk(KERN_INFO
"drbd: %s\n", drbd_buildtag());
2810 printk(KERN_INFO
"drbd: registered as block device major %d\n",
2813 return 0; /* Success! */
2818 printk(KERN_ERR
"drbd: ran out of memory\n");
2820 printk(KERN_ERR
"drbd: initialization failure\n");
2824 void drbd_free_bc(struct drbd_backing_dev
*ldev
)
2829 blkdev_put(ldev
->backing_bdev
, FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
2830 blkdev_put(ldev
->md_bdev
, FMODE_READ
| FMODE_WRITE
| FMODE_EXCL
);
2832 kfree(ldev
->disk_conf
);
2836 void drbd_free_sock(struct drbd_connection
*connection
)
2838 if (connection
->data
.socket
) {
2839 mutex_lock(&connection
->data
.mutex
);
2840 kernel_sock_shutdown(connection
->data
.socket
, SHUT_RDWR
);
2841 sock_release(connection
->data
.socket
);
2842 connection
->data
.socket
= NULL
;
2843 mutex_unlock(&connection
->data
.mutex
);
2845 if (connection
->meta
.socket
) {
2846 mutex_lock(&connection
->meta
.mutex
);
2847 kernel_sock_shutdown(connection
->meta
.socket
, SHUT_RDWR
);
2848 sock_release(connection
->meta
.socket
);
2849 connection
->meta
.socket
= NULL
;
2850 mutex_unlock(&connection
->meta
.mutex
);
2854 /* meta data management */
2856 void conn_md_sync(struct drbd_connection
*connection
)
2858 struct drbd_device
*device
;
2862 idr_for_each_entry(&connection
->volumes
, device
, vnr
) {
2863 kref_get(&device
->kref
);
2865 drbd_md_sync(device
);
2866 kref_put(&device
->kref
, drbd_destroy_device
);
2872 /* aligned 4kByte */
2873 struct meta_data_on_disk
{
2874 u64 la_size_sect
; /* last agreed size. */
2875 u64 uuid
[UI_SIZE
]; /* UUIDs. */
2878 u32 flags
; /* MDF */
2881 u32 al_offset
; /* offset to this block */
2882 u32 al_nr_extents
; /* important for restoring the AL (userspace) */
2883 /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
2884 u32 bm_offset
; /* offset to the bitmap, from here */
2885 u32 bm_bytes_per_bit
; /* BM_BLOCK_SIZE */
2886 u32 la_peer_max_bio_size
; /* last peer max_bio_size */
2888 /* see al_tr_number_to_on_disk_sector() */
2890 u32 al_stripe_size_4k
;
2892 u8 reserved_u8
[4096 - (7*8 + 10*4)];
2897 void drbd_md_write(struct drbd_device
*device
, void *b
)
2899 struct meta_data_on_disk
*buffer
= b
;
2903 memset(buffer
, 0, sizeof(*buffer
));
2905 buffer
->la_size_sect
= cpu_to_be64(drbd_get_capacity(device
->this_bdev
));
2906 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
2907 buffer
->uuid
[i
] = cpu_to_be64(device
->ldev
->md
.uuid
[i
]);
2908 buffer
->flags
= cpu_to_be32(device
->ldev
->md
.flags
);
2909 buffer
->magic
= cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN
);
2911 buffer
->md_size_sect
= cpu_to_be32(device
->ldev
->md
.md_size_sect
);
2912 buffer
->al_offset
= cpu_to_be32(device
->ldev
->md
.al_offset
);
2913 buffer
->al_nr_extents
= cpu_to_be32(device
->act_log
->nr_elements
);
2914 buffer
->bm_bytes_per_bit
= cpu_to_be32(BM_BLOCK_SIZE
);
2915 buffer
->device_uuid
= cpu_to_be64(device
->ldev
->md
.device_uuid
);
2917 buffer
->bm_offset
= cpu_to_be32(device
->ldev
->md
.bm_offset
);
2918 buffer
->la_peer_max_bio_size
= cpu_to_be32(device
->peer_max_bio_size
);
2920 buffer
->al_stripes
= cpu_to_be32(device
->ldev
->md
.al_stripes
);
2921 buffer
->al_stripe_size_4k
= cpu_to_be32(device
->ldev
->md
.al_stripe_size_4k
);
2923 D_ASSERT(drbd_md_ss(device
->ldev
) == device
->ldev
->md
.md_offset
);
2924 sector
= device
->ldev
->md
.md_offset
;
2926 if (drbd_md_sync_page_io(device
, device
->ldev
, sector
, WRITE
)) {
2927 /* this was a try anyways ... */
2928 dev_err(DEV
, "meta data update failed!\n");
2929 drbd_chk_io_error(device
, 1, DRBD_META_IO_ERROR
);
2934 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
2935 * @device: DRBD device.
2937 void drbd_md_sync(struct drbd_device
*device
)
2939 struct meta_data_on_disk
*buffer
;
2941 /* Don't accidentally change the DRBD meta data layout. */
2942 BUILD_BUG_ON(UI_SIZE
!= 4);
2943 BUILD_BUG_ON(sizeof(struct meta_data_on_disk
) != 4096);
2945 del_timer(&device
->md_sync_timer
);
2946 /* timer may be rearmed by drbd_md_mark_dirty() now. */
2947 if (!test_and_clear_bit(MD_DIRTY
, &device
->flags
))
2950 /* We use here D_FAILED and not D_ATTACHING because we try to write
2951 * metadata even if we detach due to a disk failure! */
2952 if (!get_ldev_if_state(device
, D_FAILED
))
2955 buffer
= drbd_md_get_buffer(device
);
2959 drbd_md_write(device
, buffer
);
2961 /* Update device->ldev->md.la_size_sect,
2962 * since we updated it on metadata. */
2963 device
->ldev
->md
.la_size_sect
= drbd_get_capacity(device
->this_bdev
);
2965 drbd_md_put_buffer(device
);
2970 static int check_activity_log_stripe_size(struct drbd_device
*device
,
2971 struct meta_data_on_disk
*on_disk
,
2972 struct drbd_md
*in_core
)
2974 u32 al_stripes
= be32_to_cpu(on_disk
->al_stripes
);
2975 u32 al_stripe_size_4k
= be32_to_cpu(on_disk
->al_stripe_size_4k
);
2978 /* both not set: default to old fixed size activity log */
2979 if (al_stripes
== 0 && al_stripe_size_4k
== 0) {
2981 al_stripe_size_4k
= MD_32kB_SECT
/8;
2984 /* some paranoia plausibility checks */
2986 /* we need both values to be set */
2987 if (al_stripes
== 0 || al_stripe_size_4k
== 0)
2990 al_size_4k
= (u64
)al_stripes
* al_stripe_size_4k
;
2992 /* Upper limit of activity log area, to avoid potential overflow
2993 * problems in al_tr_number_to_on_disk_sector(). As right now, more
2994 * than 72 * 4k blocks total only increases the amount of history,
2995 * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
2996 if (al_size_4k
> (16 * 1024 * 1024/4))
2999 /* Lower limit: we need at least 8 transaction slots (32kB)
3000 * to not break existing setups */
3001 if (al_size_4k
< MD_32kB_SECT
/8)
3004 in_core
->al_stripe_size_4k
= al_stripe_size_4k
;
3005 in_core
->al_stripes
= al_stripes
;
3006 in_core
->al_size_4k
= al_size_4k
;
3010 dev_err(DEV
, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
3011 al_stripes
, al_stripe_size_4k
);
3015 static int check_offsets_and_sizes(struct drbd_device
*device
, struct drbd_backing_dev
*bdev
)
3017 sector_t capacity
= drbd_get_capacity(bdev
->md_bdev
);
3018 struct drbd_md
*in_core
= &bdev
->md
;
3019 s32 on_disk_al_sect
;
3020 s32 on_disk_bm_sect
;
3022 /* The on-disk size of the activity log, calculated from offsets, and
3023 * the size of the activity log calculated from the stripe settings,
3025 * Though we could relax this a bit: it is ok, if the striped activity log
3026 * fits in the available on-disk activity log size.
3027 * Right now, that would break how resize is implemented.
3028 * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
3029 * of possible unused padding space in the on disk layout. */
3030 if (in_core
->al_offset
< 0) {
3031 if (in_core
->bm_offset
> in_core
->al_offset
)
3033 on_disk_al_sect
= -in_core
->al_offset
;
3034 on_disk_bm_sect
= in_core
->al_offset
- in_core
->bm_offset
;
3036 if (in_core
->al_offset
!= MD_4kB_SECT
)
3038 if (in_core
->bm_offset
< in_core
->al_offset
+ in_core
->al_size_4k
* MD_4kB_SECT
)
3041 on_disk_al_sect
= in_core
->bm_offset
- MD_4kB_SECT
;
3042 on_disk_bm_sect
= in_core
->md_size_sect
- in_core
->bm_offset
;
3045 /* old fixed size meta data is exactly that: fixed. */
3046 if (in_core
->meta_dev_idx
>= 0) {
3047 if (in_core
->md_size_sect
!= MD_128MB_SECT
3048 || in_core
->al_offset
!= MD_4kB_SECT
3049 || in_core
->bm_offset
!= MD_4kB_SECT
+ MD_32kB_SECT
3050 || in_core
->al_stripes
!= 1
3051 || in_core
->al_stripe_size_4k
!= MD_32kB_SECT
/8)
3055 if (capacity
< in_core
->md_size_sect
)
3057 if (capacity
- in_core
->md_size_sect
< drbd_md_first_sector(bdev
))
3060 /* should be aligned, and at least 32k */
3061 if ((on_disk_al_sect
& 7) || (on_disk_al_sect
< MD_32kB_SECT
))
3064 /* should fit (for now: exactly) into the available on-disk space;
3065 * overflow prevention is in check_activity_log_stripe_size() above. */
3066 if (on_disk_al_sect
!= in_core
->al_size_4k
* MD_4kB_SECT
)
3069 /* again, should be aligned */
3070 if (in_core
->bm_offset
& 7)
3073 /* FIXME check for device grow with flex external meta data? */
3075 /* can the available bitmap space cover the last agreed device size? */
3076 if (on_disk_bm_sect
< (in_core
->la_size_sect
+7)/MD_4kB_SECT
/8/512)
3082 dev_err(DEV
, "meta data offsets don't make sense: idx=%d "
3083 "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
3084 "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
3085 in_core
->meta_dev_idx
,
3086 in_core
->al_stripes
, in_core
->al_stripe_size_4k
,
3087 in_core
->al_offset
, in_core
->bm_offset
, in_core
->md_size_sect
,
3088 (unsigned long long)in_core
->la_size_sect
,
3089 (unsigned long long)capacity
);
3096 * drbd_md_read() - Reads in the meta data super block
3097 * @device: DRBD device.
3098 * @bdev: Device from which the meta data should be read in.
3100 * Return NO_ERROR on success, and an enum drbd_ret_code in case
3101 * something goes wrong.
3103 * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
3104 * even before @bdev is assigned to @device->ldev.
3106 int drbd_md_read(struct drbd_device
*device
, struct drbd_backing_dev
*bdev
)
3108 struct meta_data_on_disk
*buffer
;
3110 int i
, rv
= NO_ERROR
;
3112 if (device
->state
.disk
!= D_DISKLESS
)
3113 return ERR_DISK_CONFIGURED
;
3115 buffer
= drbd_md_get_buffer(device
);
3119 /* First, figure out where our meta data superblock is located,
3121 bdev
->md
.meta_dev_idx
= bdev
->disk_conf
->meta_dev_idx
;
3122 bdev
->md
.md_offset
= drbd_md_ss(bdev
);
3124 if (drbd_md_sync_page_io(device
, bdev
, bdev
->md
.md_offset
, READ
)) {
3125 /* NOTE: can't do normal error processing here as this is
3126 called BEFORE disk is attached */
3127 dev_err(DEV
, "Error while reading metadata.\n");
3128 rv
= ERR_IO_MD_DISK
;
3132 magic
= be32_to_cpu(buffer
->magic
);
3133 flags
= be32_to_cpu(buffer
->flags
);
3134 if (magic
== DRBD_MD_MAGIC_84_UNCLEAN
||
3135 (magic
== DRBD_MD_MAGIC_08
&& !(flags
& MDF_AL_CLEAN
))) {
3136 /* btw: that's Activity Log clean, not "all" clean. */
3137 dev_err(DEV
, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
3138 rv
= ERR_MD_UNCLEAN
;
3142 rv
= ERR_MD_INVALID
;
3143 if (magic
!= DRBD_MD_MAGIC_08
) {
3144 if (magic
== DRBD_MD_MAGIC_07
)
3145 dev_err(DEV
, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
3147 dev_err(DEV
, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
3151 if (be32_to_cpu(buffer
->bm_bytes_per_bit
) != BM_BLOCK_SIZE
) {
3152 dev_err(DEV
, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3153 be32_to_cpu(buffer
->bm_bytes_per_bit
), BM_BLOCK_SIZE
);
3158 /* convert to in_core endian */
3159 bdev
->md
.la_size_sect
= be64_to_cpu(buffer
->la_size_sect
);
3160 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
3161 bdev
->md
.uuid
[i
] = be64_to_cpu(buffer
->uuid
[i
]);
3162 bdev
->md
.flags
= be32_to_cpu(buffer
->flags
);
3163 bdev
->md
.device_uuid
= be64_to_cpu(buffer
->device_uuid
);
3165 bdev
->md
.md_size_sect
= be32_to_cpu(buffer
->md_size_sect
);
3166 bdev
->md
.al_offset
= be32_to_cpu(buffer
->al_offset
);
3167 bdev
->md
.bm_offset
= be32_to_cpu(buffer
->bm_offset
);
3169 if (check_activity_log_stripe_size(device
, buffer
, &bdev
->md
))
3171 if (check_offsets_and_sizes(device
, bdev
))
3174 if (be32_to_cpu(buffer
->bm_offset
) != bdev
->md
.bm_offset
) {
3175 dev_err(DEV
, "unexpected bm_offset: %d (expected %d)\n",
3176 be32_to_cpu(buffer
->bm_offset
), bdev
->md
.bm_offset
);
3179 if (be32_to_cpu(buffer
->md_size_sect
) != bdev
->md
.md_size_sect
) {
3180 dev_err(DEV
, "unexpected md_size: %u (expected %u)\n",
3181 be32_to_cpu(buffer
->md_size_sect
), bdev
->md
.md_size_sect
);
3187 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
3188 if (device
->state
.conn
< C_CONNECTED
) {
3190 peer
= be32_to_cpu(buffer
->la_peer_max_bio_size
);
3191 peer
= max(peer
, DRBD_MAX_BIO_SIZE_SAFE
);
3192 device
->peer_max_bio_size
= peer
;
3194 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
3197 drbd_md_put_buffer(device
);
3203 * drbd_md_mark_dirty() - Mark meta data super block as dirty
3204 * @device: DRBD device.
3206 * Call this function if you change anything that should be written to
3207 * the meta-data super block. This function sets MD_DIRTY, and starts a
3208 * timer that ensures that within five seconds you have to call drbd_md_sync().
3211 void drbd_md_mark_dirty_(struct drbd_device
*device
, unsigned int line
, const char *func
)
3213 if (!test_and_set_bit(MD_DIRTY
, &device
->flags
)) {
3214 mod_timer(&device
->md_sync_timer
, jiffies
+ HZ
);
3215 device
->last_md_mark_dirty
.line
= line
;
3216 device
->last_md_mark_dirty
.func
= func
;
3220 void drbd_md_mark_dirty(struct drbd_device
*device
)
3222 if (!test_and_set_bit(MD_DIRTY
, &device
->flags
))
3223 mod_timer(&device
->md_sync_timer
, jiffies
+ 5*HZ
);
3227 void drbd_uuid_move_history(struct drbd_device
*device
) __must_hold(local
)
3231 for (i
= UI_HISTORY_START
; i
< UI_HISTORY_END
; i
++)
3232 device
->ldev
->md
.uuid
[i
+1] = device
->ldev
->md
.uuid
[i
];
3235 void __drbd_uuid_set(struct drbd_device
*device
, int idx
, u64 val
) __must_hold(local
)
3237 if (idx
== UI_CURRENT
) {
3238 if (device
->state
.role
== R_PRIMARY
)
3243 drbd_set_ed_uuid(device
, val
);
3246 device
->ldev
->md
.uuid
[idx
] = val
;
3247 drbd_md_mark_dirty(device
);
3250 void _drbd_uuid_set(struct drbd_device
*device
, int idx
, u64 val
) __must_hold(local
)
3252 unsigned long flags
;
3253 spin_lock_irqsave(&device
->ldev
->md
.uuid_lock
, flags
);
3254 __drbd_uuid_set(device
, idx
, val
);
3255 spin_unlock_irqrestore(&device
->ldev
->md
.uuid_lock
, flags
);
3258 void drbd_uuid_set(struct drbd_device
*device
, int idx
, u64 val
) __must_hold(local
)
3260 unsigned long flags
;
3261 spin_lock_irqsave(&device
->ldev
->md
.uuid_lock
, flags
);
3262 if (device
->ldev
->md
.uuid
[idx
]) {
3263 drbd_uuid_move_history(device
);
3264 device
->ldev
->md
.uuid
[UI_HISTORY_START
] = device
->ldev
->md
.uuid
[idx
];
3266 __drbd_uuid_set(device
, idx
, val
);
3267 spin_unlock_irqrestore(&device
->ldev
->md
.uuid_lock
, flags
);
3271 * drbd_uuid_new_current() - Creates a new current UUID
3272 * @device: DRBD device.
3274 * Creates a new current UUID, and rotates the old current UUID into
3275 * the bitmap slot. Causes an incremental resync upon next connect.
3277 void drbd_uuid_new_current(struct drbd_device
*device
) __must_hold(local
)
3280 unsigned long long bm_uuid
;
3282 get_random_bytes(&val
, sizeof(u64
));
3284 spin_lock_irq(&device
->ldev
->md
.uuid_lock
);
3285 bm_uuid
= device
->ldev
->md
.uuid
[UI_BITMAP
];
3288 dev_warn(DEV
, "bm UUID was already set: %llX\n", bm_uuid
);
3290 device
->ldev
->md
.uuid
[UI_BITMAP
] = device
->ldev
->md
.uuid
[UI_CURRENT
];
3291 __drbd_uuid_set(device
, UI_CURRENT
, val
);
3292 spin_unlock_irq(&device
->ldev
->md
.uuid_lock
);
3294 drbd_print_uuids(device
, "new current UUID");
3295 /* get it to stable storage _now_ */
3296 drbd_md_sync(device
);
3299 void drbd_uuid_set_bm(struct drbd_device
*device
, u64 val
) __must_hold(local
)
3301 unsigned long flags
;
3302 if (device
->ldev
->md
.uuid
[UI_BITMAP
] == 0 && val
== 0)
3305 spin_lock_irqsave(&device
->ldev
->md
.uuid_lock
, flags
);
3307 drbd_uuid_move_history(device
);
3308 device
->ldev
->md
.uuid
[UI_HISTORY_START
] = device
->ldev
->md
.uuid
[UI_BITMAP
];
3309 device
->ldev
->md
.uuid
[UI_BITMAP
] = 0;
3311 unsigned long long bm_uuid
= device
->ldev
->md
.uuid
[UI_BITMAP
];
3313 dev_warn(DEV
, "bm UUID was already set: %llX\n", bm_uuid
);
3315 device
->ldev
->md
.uuid
[UI_BITMAP
] = val
& ~((u64
)1);
3317 spin_unlock_irqrestore(&device
->ldev
->md
.uuid_lock
, flags
);
3319 drbd_md_mark_dirty(device
);
3323 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3324 * @device: DRBD device.
3326 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3328 int drbd_bmio_set_n_write(struct drbd_device
*device
)
3332 if (get_ldev_if_state(device
, D_ATTACHING
)) {
3333 drbd_md_set_flag(device
, MDF_FULL_SYNC
);
3334 drbd_md_sync(device
);
3335 drbd_bm_set_all(device
);
3337 rv
= drbd_bm_write(device
);
3340 drbd_md_clear_flag(device
, MDF_FULL_SYNC
);
3341 drbd_md_sync(device
);
3351 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3352 * @device: DRBD device.
3354 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3356 int drbd_bmio_clear_n_write(struct drbd_device
*device
)
3360 drbd_resume_al(device
);
3361 if (get_ldev_if_state(device
, D_ATTACHING
)) {
3362 drbd_bm_clear_all(device
);
3363 rv
= drbd_bm_write(device
);
3370 static int w_bitmap_io(struct drbd_work
*w
, int unused
)
3372 struct bm_io_work
*work
= container_of(w
, struct bm_io_work
, w
);
3373 struct drbd_device
*device
= w
->device
;
3376 D_ASSERT(atomic_read(&device
->ap_bio_cnt
) == 0);
3378 if (get_ldev(device
)) {
3379 drbd_bm_lock(device
, work
->why
, work
->flags
);
3380 rv
= work
->io_fn(device
);
3381 drbd_bm_unlock(device
);
3385 clear_bit_unlock(BITMAP_IO
, &device
->flags
);
3386 wake_up(&device
->misc_wait
);
3389 work
->done(device
, rv
);
3391 clear_bit(BITMAP_IO_QUEUED
, &device
->flags
);
3398 void drbd_ldev_destroy(struct drbd_device
*device
)
3400 lc_destroy(device
->resync
);
3401 device
->resync
= NULL
;
3402 lc_destroy(device
->act_log
);
3403 device
->act_log
= NULL
;
3405 drbd_free_bc(device
->ldev
);
3406 device
->ldev
= NULL
;);
3408 clear_bit(GO_DISKLESS
, &device
->flags
);
3411 static int w_go_diskless(struct drbd_work
*w
, int unused
)
3413 struct drbd_device
*device
= w
->device
;
3415 D_ASSERT(device
->state
.disk
== D_FAILED
);
3416 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
3417 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
3418 * the protected members anymore, though, so once put_ldev reaches zero
3419 * again, it will be safe to free them. */
3421 /* Try to write changed bitmap pages, read errors may have just
3422 * set some bits outside the area covered by the activity log.
3424 * If we have an IO error during the bitmap writeout,
3425 * we will want a full sync next time, just in case.
3426 * (Do we want a specific meta data flag for this?)
3428 * If that does not make it to stable storage either,
3429 * we cannot do anything about that anymore.
3431 * We still need to check if both bitmap and ldev are present, we may
3432 * end up here after a failed attach, before ldev was even assigned.
3434 if (device
->bitmap
&& device
->ldev
) {
3435 /* An interrupted resync or similar is allowed to recounts bits
3437 * Any modifications would not be expected anymore, though.
3439 if (drbd_bitmap_io_from_worker(device
, drbd_bm_write
,
3440 "detach", BM_LOCKED_TEST_ALLOWED
)) {
3441 if (test_bit(WAS_READ_ERROR
, &device
->flags
)) {
3442 drbd_md_set_flag(device
, MDF_FULL_SYNC
);
3443 drbd_md_sync(device
);
3448 drbd_force_state(device
, NS(disk
, D_DISKLESS
));
3453 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
3454 * @device: DRBD device.
3455 * @io_fn: IO callback to be called when bitmap IO is possible
3456 * @done: callback to be called after the bitmap IO was performed
3457 * @why: Descriptive text of the reason for doing the IO
3459 * While IO on the bitmap happens we freeze application IO thus we ensure
3460 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3461 * called from worker context. It MUST NOT be used while a previous such
3462 * work is still pending!
3464 void drbd_queue_bitmap_io(struct drbd_device
*device
,
3465 int (*io_fn
)(struct drbd_device
*),
3466 void (*done
)(struct drbd_device
*, int),
3467 char *why
, enum bm_flag flags
)
3469 D_ASSERT(current
== first_peer_device(device
)->connection
->worker
.task
);
3471 D_ASSERT(!test_bit(BITMAP_IO_QUEUED
, &device
->flags
));
3472 D_ASSERT(!test_bit(BITMAP_IO
, &device
->flags
));
3473 D_ASSERT(list_empty(&device
->bm_io_work
.w
.list
));
3474 if (device
->bm_io_work
.why
)
3475 dev_err(DEV
, "FIXME going to queue '%s' but '%s' still pending?\n",
3476 why
, device
->bm_io_work
.why
);
3478 device
->bm_io_work
.io_fn
= io_fn
;
3479 device
->bm_io_work
.done
= done
;
3480 device
->bm_io_work
.why
= why
;
3481 device
->bm_io_work
.flags
= flags
;
3483 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
3484 set_bit(BITMAP_IO
, &device
->flags
);
3485 if (atomic_read(&device
->ap_bio_cnt
) == 0) {
3486 if (!test_and_set_bit(BITMAP_IO_QUEUED
, &device
->flags
))
3487 drbd_queue_work(&first_peer_device(device
)->connection
->sender_work
, &device
->bm_io_work
.w
);
3489 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
3493 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
3494 * @device: DRBD device.
3495 * @io_fn: IO callback to be called when bitmap IO is possible
3496 * @why: Descriptive text of the reason for doing the IO
3498 * freezes application IO while that the actual IO operations runs. This
3499 * functions MAY NOT be called from worker context.
3501 int drbd_bitmap_io(struct drbd_device
*device
, int (*io_fn
)(struct drbd_device
*),
3502 char *why
, enum bm_flag flags
)
3506 D_ASSERT(current
!= first_peer_device(device
)->connection
->worker
.task
);
3508 if ((flags
& BM_LOCKED_SET_ALLOWED
) == 0)
3509 drbd_suspend_io(device
);
3511 drbd_bm_lock(device
, why
, flags
);
3513 drbd_bm_unlock(device
);
3515 if ((flags
& BM_LOCKED_SET_ALLOWED
) == 0)
3516 drbd_resume_io(device
);
3521 void drbd_md_set_flag(struct drbd_device
*device
, int flag
) __must_hold(local
)
3523 if ((device
->ldev
->md
.flags
& flag
) != flag
) {
3524 drbd_md_mark_dirty(device
);
3525 device
->ldev
->md
.flags
|= flag
;
3529 void drbd_md_clear_flag(struct drbd_device
*device
, int flag
) __must_hold(local
)
3531 if ((device
->ldev
->md
.flags
& flag
) != 0) {
3532 drbd_md_mark_dirty(device
);
3533 device
->ldev
->md
.flags
&= ~flag
;
3536 int drbd_md_test_flag(struct drbd_backing_dev
*bdev
, int flag
)
3538 return (bdev
->md
.flags
& flag
) != 0;
3541 static void md_sync_timer_fn(unsigned long data
)
3543 struct drbd_device
*device
= (struct drbd_device
*) data
;
3545 /* must not double-queue! */
3546 if (list_empty(&device
->md_sync_work
.list
))
3547 drbd_queue_work_front(&first_peer_device(device
)->connection
->sender_work
, &device
->md_sync_work
);
3550 static int w_md_sync(struct drbd_work
*w
, int unused
)
3552 struct drbd_device
*device
= w
->device
;
3554 dev_warn(DEV
, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
3556 dev_warn(DEV
, "last md_mark_dirty: %s:%u\n",
3557 device
->last_md_mark_dirty
.func
, device
->last_md_mark_dirty
.line
);
3559 drbd_md_sync(device
);
3563 const char *cmdname(enum drbd_packet cmd
)
3565 /* THINK may need to become several global tables
3566 * when we want to support more than
3567 * one PRO_VERSION */
3568 static const char *cmdnames
[] = {
3570 [P_DATA_REPLY
] = "DataReply",
3571 [P_RS_DATA_REPLY
] = "RSDataReply",
3572 [P_BARRIER
] = "Barrier",
3573 [P_BITMAP
] = "ReportBitMap",
3574 [P_BECOME_SYNC_TARGET
] = "BecomeSyncTarget",
3575 [P_BECOME_SYNC_SOURCE
] = "BecomeSyncSource",
3576 [P_UNPLUG_REMOTE
] = "UnplugRemote",
3577 [P_DATA_REQUEST
] = "DataRequest",
3578 [P_RS_DATA_REQUEST
] = "RSDataRequest",
3579 [P_SYNC_PARAM
] = "SyncParam",
3580 [P_SYNC_PARAM89
] = "SyncParam89",
3581 [P_PROTOCOL
] = "ReportProtocol",
3582 [P_UUIDS
] = "ReportUUIDs",
3583 [P_SIZES
] = "ReportSizes",
3584 [P_STATE
] = "ReportState",
3585 [P_SYNC_UUID
] = "ReportSyncUUID",
3586 [P_AUTH_CHALLENGE
] = "AuthChallenge",
3587 [P_AUTH_RESPONSE
] = "AuthResponse",
3589 [P_PING_ACK
] = "PingAck",
3590 [P_RECV_ACK
] = "RecvAck",
3591 [P_WRITE_ACK
] = "WriteAck",
3592 [P_RS_WRITE_ACK
] = "RSWriteAck",
3593 [P_SUPERSEDED
] = "Superseded",
3594 [P_NEG_ACK
] = "NegAck",
3595 [P_NEG_DREPLY
] = "NegDReply",
3596 [P_NEG_RS_DREPLY
] = "NegRSDReply",
3597 [P_BARRIER_ACK
] = "BarrierAck",
3598 [P_STATE_CHG_REQ
] = "StateChgRequest",
3599 [P_STATE_CHG_REPLY
] = "StateChgReply",
3600 [P_OV_REQUEST
] = "OVRequest",
3601 [P_OV_REPLY
] = "OVReply",
3602 [P_OV_RESULT
] = "OVResult",
3603 [P_CSUM_RS_REQUEST
] = "CsumRSRequest",
3604 [P_RS_IS_IN_SYNC
] = "CsumRSIsInSync",
3605 [P_COMPRESSED_BITMAP
] = "CBitmap",
3606 [P_DELAY_PROBE
] = "DelayProbe",
3607 [P_OUT_OF_SYNC
] = "OutOfSync",
3608 [P_RETRY_WRITE
] = "RetryWrite",
3609 [P_RS_CANCEL
] = "RSCancel",
3610 [P_CONN_ST_CHG_REQ
] = "conn_st_chg_req",
3611 [P_CONN_ST_CHG_REPLY
] = "conn_st_chg_reply",
3612 [P_RETRY_WRITE
] = "retry_write",
3613 [P_PROTOCOL_UPDATE
] = "protocol_update",
3615 /* enum drbd_packet, but not commands - obsoleted flags:
3621 /* too big for the array: 0xfffX */
3622 if (cmd
== P_INITIAL_META
)
3623 return "InitialMeta";
3624 if (cmd
== P_INITIAL_DATA
)
3625 return "InitialData";
3626 if (cmd
== P_CONNECTION_FEATURES
)
3627 return "ConnectionFeatures";
3628 if (cmd
>= ARRAY_SIZE(cmdnames
))
3630 return cmdnames
[cmd
];
3634 * drbd_wait_misc - wait for a request to make progress
3635 * @device: device associated with the request
3636 * @i: the struct drbd_interval embedded in struct drbd_request or
3637 * struct drbd_peer_request
3639 int drbd_wait_misc(struct drbd_device
*device
, struct drbd_interval
*i
)
3641 struct net_conf
*nc
;
3646 nc
= rcu_dereference(first_peer_device(device
)->connection
->net_conf
);
3651 timeout
= nc
->ko_count
? nc
->timeout
* HZ
/ 10 * nc
->ko_count
: MAX_SCHEDULE_TIMEOUT
;
3654 /* Indicate to wake up device->misc_wait on progress. */
3656 prepare_to_wait(&device
->misc_wait
, &wait
, TASK_INTERRUPTIBLE
);
3657 spin_unlock_irq(&first_peer_device(device
)->connection
->req_lock
);
3658 timeout
= schedule_timeout(timeout
);
3659 finish_wait(&device
->misc_wait
, &wait
);
3660 spin_lock_irq(&first_peer_device(device
)->connection
->req_lock
);
3661 if (!timeout
|| device
->state
.conn
< C_CONNECTED
)
3663 if (signal_pending(current
))
3664 return -ERESTARTSYS
;
3668 #ifdef CONFIG_DRBD_FAULT_INJECTION
3669 /* Fault insertion support including random number generator shamelessly
3670 * stolen from kernel/rcutorture.c */
3671 struct fault_random_state
{
3672 unsigned long state
;
3673 unsigned long count
;
3676 #define FAULT_RANDOM_MULT 39916801 /* prime */
3677 #define FAULT_RANDOM_ADD 479001701 /* prime */
3678 #define FAULT_RANDOM_REFRESH 10000
3681 * Crude but fast random-number generator. Uses a linear congruential
3682 * generator, with occasional help from get_random_bytes().
3684 static unsigned long
3685 _drbd_fault_random(struct fault_random_state
*rsp
)
3689 if (!rsp
->count
--) {
3690 get_random_bytes(&refresh
, sizeof(refresh
));
3691 rsp
->state
+= refresh
;
3692 rsp
->count
= FAULT_RANDOM_REFRESH
;
3694 rsp
->state
= rsp
->state
* FAULT_RANDOM_MULT
+ FAULT_RANDOM_ADD
;
3695 return swahw32(rsp
->state
);
3699 _drbd_fault_str(unsigned int type
) {
3700 static char *_faults
[] = {
3701 [DRBD_FAULT_MD_WR
] = "Meta-data write",
3702 [DRBD_FAULT_MD_RD
] = "Meta-data read",
3703 [DRBD_FAULT_RS_WR
] = "Resync write",
3704 [DRBD_FAULT_RS_RD
] = "Resync read",
3705 [DRBD_FAULT_DT_WR
] = "Data write",
3706 [DRBD_FAULT_DT_RD
] = "Data read",
3707 [DRBD_FAULT_DT_RA
] = "Data read ahead",
3708 [DRBD_FAULT_BM_ALLOC
] = "BM allocation",
3709 [DRBD_FAULT_AL_EE
] = "EE allocation",
3710 [DRBD_FAULT_RECEIVE
] = "receive data corruption",
3713 return (type
< DRBD_FAULT_MAX
) ? _faults
[type
] : "**Unknown**";
3717 _drbd_insert_fault(struct drbd_device
*device
, unsigned int type
)
3719 static struct fault_random_state rrs
= {0, 0};
3721 unsigned int ret
= (
3723 ((1 << device_to_minor(device
)) & fault_devs
) != 0) &&
3724 (((_drbd_fault_random(&rrs
) % 100) + 1) <= fault_rate
));
3729 if (__ratelimit(&drbd_ratelimit_state
))
3730 dev_warn(DEV
, "***Simulating %s failure\n",
3731 _drbd_fault_str(type
));
3738 const char *drbd_buildtag(void)
3740 /* DRBD built from external sources has here a reference to the
3741 git hash of the source code. */
3743 static char buildtag
[38] = "\0uilt-in";
3745 if (buildtag
[0] == 0) {
3747 sprintf(buildtag
, "srcversion: %-24s", THIS_MODULE
->srcversion
);
3756 module_init(drbd_init
)
3757 module_exit(drbd_cleanup
)
3759 EXPORT_SYMBOL(drbd_conn_str
);
3760 EXPORT_SYMBOL(drbd_role_str
);
3761 EXPORT_SYMBOL(drbd_disk_str
);
3762 EXPORT_SYMBOL(drbd_set_st_err_str
);