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/smp_lock.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>
49 #define __KERNEL_SYSCALLS__
50 #include <linux/unistd.h>
51 #include <linux/vmalloc.h>
53 #include <linux/drbd_limits.h>
55 #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
59 struct after_state_chg_work
{
63 enum chg_state_flags flags
;
64 struct completion
*done
;
67 int drbdd_init(struct drbd_thread
*);
68 int drbd_worker(struct drbd_thread
*);
69 int drbd_asender(struct drbd_thread
*);
72 static int drbd_open(struct block_device
*bdev
, fmode_t mode
);
73 static int drbd_release(struct gendisk
*gd
, fmode_t mode
);
74 static int w_after_state_ch(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
);
75 static void after_state_ch(struct drbd_conf
*mdev
, union drbd_state os
,
76 union drbd_state ns
, enum chg_state_flags flags
);
77 static int w_md_sync(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
);
78 static void md_sync_timer_fn(unsigned long data
);
79 static int w_bitmap_io(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
);
81 MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
82 "Lars Ellenberg <lars@linbit.com>");
83 MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION
);
84 MODULE_VERSION(REL_VERSION
);
85 MODULE_LICENSE("GPL");
86 MODULE_PARM_DESC(minor_count
, "Maximum number of drbd devices (1-255)");
87 MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR
);
89 #include <linux/moduleparam.h>
90 /* allow_open_on_secondary */
91 MODULE_PARM_DESC(allow_oos
, "DONT USE!");
92 /* thanks to these macros, if compiled into the kernel (not-module),
93 * this becomes the boot parameter drbd.minor_count */
94 module_param(minor_count
, uint
, 0444);
95 module_param(disable_sendpage
, bool, 0644);
96 module_param(allow_oos
, bool, 0);
97 module_param(cn_idx
, uint
, 0444);
98 module_param(proc_details
, int, 0644);
100 #ifdef CONFIG_DRBD_FAULT_INJECTION
103 static int fault_count
;
105 /* bitmap of enabled faults */
106 module_param(enable_faults
, int, 0664);
107 /* fault rate % value - applies to all enabled faults */
108 module_param(fault_rate
, int, 0664);
109 /* count of faults inserted */
110 module_param(fault_count
, int, 0664);
111 /* bitmap of devices to insert faults on */
112 module_param(fault_devs
, int, 0644);
115 /* module parameter, defined */
116 unsigned int minor_count
= 32;
117 int disable_sendpage
;
119 unsigned int cn_idx
= CN_IDX_DRBD
;
120 int proc_details
; /* Detail level in proc drbd*/
122 /* Module parameter for setting the user mode helper program
123 * to run. Default is /sbin/drbdadm */
124 char usermode_helper
[80] = "/sbin/drbdadm";
126 module_param_string(usermode_helper
, usermode_helper
, sizeof(usermode_helper
), 0644);
128 /* in 2.6.x, our device mapping and config info contains our virtual gendisks
129 * as member "struct gendisk *vdisk;"
131 struct drbd_conf
**minor_table
;
133 struct kmem_cache
*drbd_request_cache
;
134 struct kmem_cache
*drbd_ee_cache
; /* epoch entries */
135 struct kmem_cache
*drbd_bm_ext_cache
; /* bitmap extents */
136 struct kmem_cache
*drbd_al_ext_cache
; /* activity log extents */
137 mempool_t
*drbd_request_mempool
;
138 mempool_t
*drbd_ee_mempool
;
140 /* I do not use a standard mempool, because:
141 1) I want to hand out the pre-allocated objects first.
142 2) I want to be able to interrupt sleeping allocation with a signal.
143 Note: This is a single linked list, the next pointer is the private
144 member of struct page.
146 struct page
*drbd_pp_pool
;
147 spinlock_t drbd_pp_lock
;
149 wait_queue_head_t drbd_pp_wait
;
151 DEFINE_RATELIMIT_STATE(drbd_ratelimit_state
, 5 * HZ
, 5);
153 static const struct block_device_operations drbd_ops
= {
154 .owner
= THIS_MODULE
,
156 .release
= drbd_release
,
159 #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
162 /* When checking with sparse, and this is an inline function, sparse will
163 give tons of false positives. When this is a real functions sparse works.
165 int _get_ldev_if_state(struct drbd_conf
*mdev
, enum drbd_disk_state mins
)
169 atomic_inc(&mdev
->local_cnt
);
170 io_allowed
= (mdev
->state
.disk
>= mins
);
172 if (atomic_dec_and_test(&mdev
->local_cnt
))
173 wake_up(&mdev
->misc_wait
);
181 * DOC: The transfer log
183 * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
184 * mdev->newest_tle points to the head, mdev->oldest_tle points to the tail
185 * of the list. There is always at least one &struct drbd_tl_epoch object.
187 * Each &struct drbd_tl_epoch has a circular double linked list of requests
190 static int tl_init(struct drbd_conf
*mdev
)
192 struct drbd_tl_epoch
*b
;
194 /* during device minor initialization, we may well use GFP_KERNEL */
195 b
= kmalloc(sizeof(struct drbd_tl_epoch
), GFP_KERNEL
);
198 INIT_LIST_HEAD(&b
->requests
);
199 INIT_LIST_HEAD(&b
->w
.list
);
203 b
->w
.cb
= NULL
; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
205 mdev
->oldest_tle
= b
;
206 mdev
->newest_tle
= b
;
207 INIT_LIST_HEAD(&mdev
->out_of_sequence_requests
);
209 mdev
->tl_hash
= NULL
;
215 static void tl_cleanup(struct drbd_conf
*mdev
)
217 D_ASSERT(mdev
->oldest_tle
== mdev
->newest_tle
);
218 D_ASSERT(list_empty(&mdev
->out_of_sequence_requests
));
219 kfree(mdev
->oldest_tle
);
220 mdev
->oldest_tle
= NULL
;
221 kfree(mdev
->unused_spare_tle
);
222 mdev
->unused_spare_tle
= NULL
;
223 kfree(mdev
->tl_hash
);
224 mdev
->tl_hash
= NULL
;
229 * _tl_add_barrier() - Adds a barrier to the transfer log
230 * @mdev: DRBD device.
231 * @new: Barrier to be added before the current head of the TL.
233 * The caller must hold the req_lock.
235 void _tl_add_barrier(struct drbd_conf
*mdev
, struct drbd_tl_epoch
*new)
237 struct drbd_tl_epoch
*newest_before
;
239 INIT_LIST_HEAD(&new->requests
);
240 INIT_LIST_HEAD(&new->w
.list
);
241 new->w
.cb
= NULL
; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
245 newest_before
= mdev
->newest_tle
;
246 /* never send a barrier number == 0, because that is special-cased
247 * when using TCQ for our write ordering code */
248 new->br_number
= (newest_before
->br_number
+1) ?: 1;
249 if (mdev
->newest_tle
!= new) {
250 mdev
->newest_tle
->next
= new;
251 mdev
->newest_tle
= new;
256 * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
257 * @mdev: DRBD device.
258 * @barrier_nr: Expected identifier of the DRBD write barrier packet.
259 * @set_size: Expected number of requests before that barrier.
261 * In case the passed barrier_nr or set_size does not match the oldest
262 * &struct drbd_tl_epoch objects this function will cause a termination
265 void tl_release(struct drbd_conf
*mdev
, unsigned int barrier_nr
,
266 unsigned int set_size
)
268 struct drbd_tl_epoch
*b
, *nob
; /* next old barrier */
269 struct list_head
*le
, *tle
;
270 struct drbd_request
*r
;
272 spin_lock_irq(&mdev
->req_lock
);
274 b
= mdev
->oldest_tle
;
276 /* first some paranoia code */
278 dev_err(DEV
, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
282 if (b
->br_number
!= barrier_nr
) {
283 dev_err(DEV
, "BAD! BarrierAck #%u received, expected #%u!\n",
284 barrier_nr
, b
->br_number
);
287 if (b
->n_writes
!= set_size
) {
288 dev_err(DEV
, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
289 barrier_nr
, set_size
, b
->n_writes
);
293 /* Clean up list of requests processed during current epoch */
294 list_for_each_safe(le
, tle
, &b
->requests
) {
295 r
= list_entry(le
, struct drbd_request
, tl_requests
);
296 _req_mod(r
, barrier_acked
);
298 /* There could be requests on the list waiting for completion
299 of the write to the local disk. To avoid corruptions of
300 slab's data structures we have to remove the lists head.
302 Also there could have been a barrier ack out of sequence, overtaking
303 the write acks - which would be a bug and violating write ordering.
304 To not deadlock in case we lose connection while such requests are
305 still pending, we need some way to find them for the
306 _req_mode(connection_lost_while_pending).
308 These have been list_move'd to the out_of_sequence_requests list in
309 _req_mod(, barrier_acked) above.
311 list_del_init(&b
->requests
);
314 if (test_and_clear_bit(CREATE_BARRIER
, &mdev
->flags
)) {
315 _tl_add_barrier(mdev
, b
);
317 mdev
->oldest_tle
= nob
;
318 /* if nob == NULL b was the only barrier, and becomes the new
319 barrier. Therefore mdev->oldest_tle points already to b */
321 D_ASSERT(nob
!= NULL
);
322 mdev
->oldest_tle
= nob
;
326 spin_unlock_irq(&mdev
->req_lock
);
327 dec_ap_pending(mdev
);
332 spin_unlock_irq(&mdev
->req_lock
);
333 drbd_force_state(mdev
, NS(conn
, C_PROTOCOL_ERROR
));
337 * _tl_restart() - Walks the transfer log, and applies an action to all requests
338 * @mdev: DRBD device.
339 * @what: The action/event to perform with all request objects
341 * @what might be one of connection_lost_while_pending, resend, fail_frozen_disk_io,
342 * restart_frozen_disk_io.
344 static void _tl_restart(struct drbd_conf
*mdev
, enum drbd_req_event what
)
346 struct drbd_tl_epoch
*b
, *tmp
, **pn
;
347 struct list_head
*le
, *tle
, carry_reads
;
348 struct drbd_request
*req
;
349 int rv
, n_writes
, n_reads
;
351 b
= mdev
->oldest_tle
;
352 pn
= &mdev
->oldest_tle
;
356 INIT_LIST_HEAD(&carry_reads
);
357 list_for_each_safe(le
, tle
, &b
->requests
) {
358 req
= list_entry(le
, struct drbd_request
, tl_requests
);
359 rv
= _req_mod(req
, what
);
361 n_writes
+= (rv
& MR_WRITE
) >> MR_WRITE_SHIFT
;
362 n_reads
+= (rv
& MR_READ
) >> MR_READ_SHIFT
;
367 if (what
== resend
) {
368 b
->n_writes
= n_writes
;
369 if (b
->w
.cb
== NULL
) {
370 b
->w
.cb
= w_send_barrier
;
371 inc_ap_pending(mdev
);
372 set_bit(CREATE_BARRIER
, &mdev
->flags
);
375 drbd_queue_work(&mdev
->data
.work
, &b
->w
);
380 list_add(&carry_reads
, &b
->requests
);
381 /* there could still be requests on that ring list,
382 * in case local io is still pending */
383 list_del(&b
->requests
);
385 /* dec_ap_pending corresponding to queue_barrier.
386 * the newest barrier may not have been queued yet,
387 * in which case w.cb is still NULL. */
389 dec_ap_pending(mdev
);
391 if (b
== mdev
->newest_tle
) {
392 /* recycle, but reinit! */
393 D_ASSERT(tmp
== NULL
);
394 INIT_LIST_HEAD(&b
->requests
);
395 list_splice(&carry_reads
, &b
->requests
);
396 INIT_LIST_HEAD(&b
->w
.list
);
398 b
->br_number
= net_random();
408 list_splice(&carry_reads
, &b
->requests
);
414 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
415 * @mdev: DRBD device.
417 * This is called after the connection to the peer was lost. The storage covered
418 * by the requests on the transfer gets marked as our of sync. Called from the
419 * receiver thread and the worker thread.
421 void tl_clear(struct drbd_conf
*mdev
)
423 struct list_head
*le
, *tle
;
424 struct drbd_request
*r
;
426 spin_lock_irq(&mdev
->req_lock
);
428 _tl_restart(mdev
, connection_lost_while_pending
);
430 /* we expect this list to be empty. */
431 D_ASSERT(list_empty(&mdev
->out_of_sequence_requests
));
433 /* but just in case, clean it up anyways! */
434 list_for_each_safe(le
, tle
, &mdev
->out_of_sequence_requests
) {
435 r
= list_entry(le
, struct drbd_request
, tl_requests
);
436 /* It would be nice to complete outside of spinlock.
437 * But this is easier for now. */
438 _req_mod(r
, connection_lost_while_pending
);
441 /* ensure bit indicating barrier is required is clear */
442 clear_bit(CREATE_BARRIER
, &mdev
->flags
);
444 memset(mdev
->app_reads_hash
, 0, APP_R_HSIZE
*sizeof(void *));
446 spin_unlock_irq(&mdev
->req_lock
);
449 void tl_restart(struct drbd_conf
*mdev
, enum drbd_req_event what
)
451 spin_lock_irq(&mdev
->req_lock
);
452 _tl_restart(mdev
, what
);
453 spin_unlock_irq(&mdev
->req_lock
);
457 * cl_wide_st_chg() - TRUE if the state change is a cluster wide one
458 * @mdev: DRBD device.
459 * @os: old (current) state.
460 * @ns: new (wanted) state.
462 static int cl_wide_st_chg(struct drbd_conf
*mdev
,
463 union drbd_state os
, union drbd_state ns
)
465 return (os
.conn
>= C_CONNECTED
&& ns
.conn
>= C_CONNECTED
&&
466 ((os
.role
!= R_PRIMARY
&& ns
.role
== R_PRIMARY
) ||
467 (os
.conn
!= C_STARTING_SYNC_T
&& ns
.conn
== C_STARTING_SYNC_T
) ||
468 (os
.conn
!= C_STARTING_SYNC_S
&& ns
.conn
== C_STARTING_SYNC_S
) ||
469 (os
.disk
!= D_DISKLESS
&& ns
.disk
== D_DISKLESS
))) ||
470 (os
.conn
>= C_CONNECTED
&& ns
.conn
== C_DISCONNECTING
) ||
471 (os
.conn
== C_CONNECTED
&& ns
.conn
== C_VERIFY_S
);
474 int drbd_change_state(struct drbd_conf
*mdev
, enum chg_state_flags f
,
475 union drbd_state mask
, union drbd_state val
)
478 union drbd_state os
, ns
;
481 spin_lock_irqsave(&mdev
->req_lock
, flags
);
483 ns
.i
= (os
.i
& ~mask
.i
) | val
.i
;
484 rv
= _drbd_set_state(mdev
, ns
, f
, NULL
);
486 spin_unlock_irqrestore(&mdev
->req_lock
, flags
);
492 * drbd_force_state() - Impose a change which happens outside our control on our state
493 * @mdev: DRBD device.
494 * @mask: mask of state bits to change.
495 * @val: value of new state bits.
497 void drbd_force_state(struct drbd_conf
*mdev
,
498 union drbd_state mask
, union drbd_state val
)
500 drbd_change_state(mdev
, CS_HARD
, mask
, val
);
503 static int is_valid_state(struct drbd_conf
*mdev
, union drbd_state ns
);
504 static int is_valid_state_transition(struct drbd_conf
*,
505 union drbd_state
, union drbd_state
);
506 static union drbd_state
sanitize_state(struct drbd_conf
*mdev
, union drbd_state os
,
507 union drbd_state ns
, int *warn_sync_abort
);
508 int drbd_send_state_req(struct drbd_conf
*,
509 union drbd_state
, union drbd_state
);
511 static enum drbd_state_ret_codes
_req_st_cond(struct drbd_conf
*mdev
,
512 union drbd_state mask
, union drbd_state val
)
514 union drbd_state os
, ns
;
518 if (test_and_clear_bit(CL_ST_CHG_SUCCESS
, &mdev
->flags
))
519 return SS_CW_SUCCESS
;
521 if (test_and_clear_bit(CL_ST_CHG_FAIL
, &mdev
->flags
))
522 return SS_CW_FAILED_BY_PEER
;
525 spin_lock_irqsave(&mdev
->req_lock
, flags
);
527 ns
.i
= (os
.i
& ~mask
.i
) | val
.i
;
528 ns
= sanitize_state(mdev
, os
, ns
, NULL
);
530 if (!cl_wide_st_chg(mdev
, os
, ns
))
533 rv
= is_valid_state(mdev
, ns
);
534 if (rv
== SS_SUCCESS
) {
535 rv
= is_valid_state_transition(mdev
, ns
, os
);
536 if (rv
== SS_SUCCESS
)
537 rv
= 0; /* cont waiting, otherwise fail. */
540 spin_unlock_irqrestore(&mdev
->req_lock
, flags
);
546 * drbd_req_state() - Perform an eventually cluster wide state change
547 * @mdev: DRBD device.
548 * @mask: mask of state bits to change.
549 * @val: value of new state bits.
552 * Should not be called directly, use drbd_request_state() or
553 * _drbd_request_state().
555 static int drbd_req_state(struct drbd_conf
*mdev
,
556 union drbd_state mask
, union drbd_state val
,
557 enum chg_state_flags f
)
559 struct completion done
;
561 union drbd_state os
, ns
;
564 init_completion(&done
);
566 if (f
& CS_SERIALIZE
)
567 mutex_lock(&mdev
->state_mutex
);
569 spin_lock_irqsave(&mdev
->req_lock
, flags
);
571 ns
.i
= (os
.i
& ~mask
.i
) | val
.i
;
572 ns
= sanitize_state(mdev
, os
, ns
, NULL
);
574 if (cl_wide_st_chg(mdev
, os
, ns
)) {
575 rv
= is_valid_state(mdev
, ns
);
576 if (rv
== SS_SUCCESS
)
577 rv
= is_valid_state_transition(mdev
, ns
, os
);
578 spin_unlock_irqrestore(&mdev
->req_lock
, flags
);
580 if (rv
< SS_SUCCESS
) {
582 print_st_err(mdev
, os
, ns
, rv
);
586 drbd_state_lock(mdev
);
587 if (!drbd_send_state_req(mdev
, mask
, val
)) {
588 drbd_state_unlock(mdev
);
589 rv
= SS_CW_FAILED_BY_PEER
;
591 print_st_err(mdev
, os
, ns
, rv
);
595 wait_event(mdev
->state_wait
,
596 (rv
= _req_st_cond(mdev
, mask
, val
)));
598 if (rv
< SS_SUCCESS
) {
599 drbd_state_unlock(mdev
);
601 print_st_err(mdev
, os
, ns
, rv
);
604 spin_lock_irqsave(&mdev
->req_lock
, flags
);
606 ns
.i
= (os
.i
& ~mask
.i
) | val
.i
;
607 rv
= _drbd_set_state(mdev
, ns
, f
, &done
);
608 drbd_state_unlock(mdev
);
610 rv
= _drbd_set_state(mdev
, ns
, f
, &done
);
613 spin_unlock_irqrestore(&mdev
->req_lock
, flags
);
615 if (f
& CS_WAIT_COMPLETE
&& rv
== SS_SUCCESS
) {
616 D_ASSERT(current
!= mdev
->worker
.task
);
617 wait_for_completion(&done
);
621 if (f
& CS_SERIALIZE
)
622 mutex_unlock(&mdev
->state_mutex
);
628 * _drbd_request_state() - Request a state change (with flags)
629 * @mdev: DRBD device.
630 * @mask: mask of state bits to change.
631 * @val: value of new state bits.
634 * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
635 * flag, or when logging of failed state change requests is not desired.
637 int _drbd_request_state(struct drbd_conf
*mdev
, union drbd_state mask
,
638 union drbd_state val
, enum chg_state_flags f
)
642 wait_event(mdev
->state_wait
,
643 (rv
= drbd_req_state(mdev
, mask
, val
, f
)) != SS_IN_TRANSIENT_STATE
);
648 static void print_st(struct drbd_conf
*mdev
, char *name
, union drbd_state ns
)
650 dev_err(DEV
, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
652 drbd_conn_str(ns
.conn
),
653 drbd_role_str(ns
.role
),
654 drbd_role_str(ns
.peer
),
655 drbd_disk_str(ns
.disk
),
656 drbd_disk_str(ns
.pdsk
),
658 ns
.aftr_isp
? 'a' : '-',
659 ns
.peer_isp
? 'p' : '-',
660 ns
.user_isp
? 'u' : '-'
664 void print_st_err(struct drbd_conf
*mdev
,
665 union drbd_state os
, union drbd_state ns
, int err
)
667 if (err
== SS_IN_TRANSIENT_STATE
)
669 dev_err(DEV
, "State change failed: %s\n", drbd_set_st_err_str(err
));
670 print_st(mdev
, " state", os
);
671 print_st(mdev
, "wanted", ns
);
675 #define drbd_peer_str drbd_role_str
676 #define drbd_pdsk_str drbd_disk_str
678 #define drbd_susp_str(A) ((A) ? "1" : "0")
679 #define drbd_aftr_isp_str(A) ((A) ? "1" : "0")
680 #define drbd_peer_isp_str(A) ((A) ? "1" : "0")
681 #define drbd_user_isp_str(A) ((A) ? "1" : "0")
684 ({ if (ns.A != os.A) { \
685 pbp += sprintf(pbp, #A "( %s -> %s ) ", \
686 drbd_##A##_str(os.A), \
687 drbd_##A##_str(ns.A)); \
691 * is_valid_state() - Returns an SS_ error code if ns is not valid
692 * @mdev: DRBD device.
693 * @ns: State to consider.
695 static int is_valid_state(struct drbd_conf
*mdev
, union drbd_state ns
)
697 /* See drbd_state_sw_errors in drbd_strings.c */
699 enum drbd_fencing_p fp
;
703 if (get_ldev(mdev
)) {
704 fp
= mdev
->ldev
->dc
.fencing
;
708 if (get_net_conf(mdev
)) {
709 if (!mdev
->net_conf
->two_primaries
&&
710 ns
.role
== R_PRIMARY
&& ns
.peer
== R_PRIMARY
)
711 rv
= SS_TWO_PRIMARIES
;
716 /* already found a reason to abort */;
717 else if (ns
.role
== R_SECONDARY
&& mdev
->open_cnt
)
718 rv
= SS_DEVICE_IN_USE
;
720 else if (ns
.role
== R_PRIMARY
&& ns
.conn
< C_CONNECTED
&& ns
.disk
< D_UP_TO_DATE
)
721 rv
= SS_NO_UP_TO_DATE_DISK
;
723 else if (fp
>= FP_RESOURCE
&&
724 ns
.role
== R_PRIMARY
&& ns
.conn
< C_CONNECTED
&& ns
.pdsk
>= D_UNKNOWN
)
727 else if (ns
.role
== R_PRIMARY
&& ns
.disk
<= D_INCONSISTENT
&& ns
.pdsk
<= D_INCONSISTENT
)
728 rv
= SS_NO_UP_TO_DATE_DISK
;
730 else if (ns
.conn
> C_CONNECTED
&& ns
.disk
< D_INCONSISTENT
)
731 rv
= SS_NO_LOCAL_DISK
;
733 else if (ns
.conn
> C_CONNECTED
&& ns
.pdsk
< D_INCONSISTENT
)
734 rv
= SS_NO_REMOTE_DISK
;
736 else if (ns
.conn
> C_CONNECTED
&& ns
.disk
< D_UP_TO_DATE
&& ns
.pdsk
< D_UP_TO_DATE
)
737 rv
= SS_NO_UP_TO_DATE_DISK
;
739 else if ((ns
.conn
== C_CONNECTED
||
740 ns
.conn
== C_WF_BITMAP_S
||
741 ns
.conn
== C_SYNC_SOURCE
||
742 ns
.conn
== C_PAUSED_SYNC_S
) &&
743 ns
.disk
== D_OUTDATED
)
744 rv
= SS_CONNECTED_OUTDATES
;
746 else if ((ns
.conn
== C_VERIFY_S
|| ns
.conn
== C_VERIFY_T
) &&
747 (mdev
->sync_conf
.verify_alg
[0] == 0))
748 rv
= SS_NO_VERIFY_ALG
;
750 else if ((ns
.conn
== C_VERIFY_S
|| ns
.conn
== C_VERIFY_T
) &&
751 mdev
->agreed_pro_version
< 88)
752 rv
= SS_NOT_SUPPORTED
;
758 * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
759 * @mdev: DRBD device.
763 static int is_valid_state_transition(struct drbd_conf
*mdev
,
764 union drbd_state ns
, union drbd_state os
)
768 if ((ns
.conn
== C_STARTING_SYNC_T
|| ns
.conn
== C_STARTING_SYNC_S
) &&
769 os
.conn
> C_CONNECTED
)
770 rv
= SS_RESYNC_RUNNING
;
772 if (ns
.conn
== C_DISCONNECTING
&& os
.conn
== C_STANDALONE
)
773 rv
= SS_ALREADY_STANDALONE
;
775 if (ns
.disk
> D_ATTACHING
&& os
.disk
== D_DISKLESS
)
778 if (ns
.conn
== C_WF_CONNECTION
&& os
.conn
< C_UNCONNECTED
)
779 rv
= SS_NO_NET_CONFIG
;
781 if (ns
.disk
== D_OUTDATED
&& os
.disk
< D_OUTDATED
&& os
.disk
!= D_ATTACHING
)
782 rv
= SS_LOWER_THAN_OUTDATED
;
784 if (ns
.conn
== C_DISCONNECTING
&& os
.conn
== C_UNCONNECTED
)
785 rv
= SS_IN_TRANSIENT_STATE
;
787 if (ns
.conn
== os
.conn
&& ns
.conn
== C_WF_REPORT_PARAMS
)
788 rv
= SS_IN_TRANSIENT_STATE
;
790 if ((ns
.conn
== C_VERIFY_S
|| ns
.conn
== C_VERIFY_T
) && os
.conn
< C_CONNECTED
)
791 rv
= SS_NEED_CONNECTION
;
793 if ((ns
.conn
== C_VERIFY_S
|| ns
.conn
== C_VERIFY_T
) &&
794 ns
.conn
!= os
.conn
&& os
.conn
> C_CONNECTED
)
795 rv
= SS_RESYNC_RUNNING
;
797 if ((ns
.conn
== C_STARTING_SYNC_S
|| ns
.conn
== C_STARTING_SYNC_T
) &&
798 os
.conn
< C_CONNECTED
)
799 rv
= SS_NEED_CONNECTION
;
805 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
806 * @mdev: DRBD device.
811 * When we loose connection, we have to set the state of the peers disk (pdsk)
812 * to D_UNKNOWN. This rule and many more along those lines are in this function.
814 static union drbd_state
sanitize_state(struct drbd_conf
*mdev
, union drbd_state os
,
815 union drbd_state ns
, int *warn_sync_abort
)
817 enum drbd_fencing_p fp
;
820 if (get_ldev(mdev
)) {
821 fp
= mdev
->ldev
->dc
.fencing
;
825 /* Disallow Network errors to configure a device's network part */
826 if ((ns
.conn
>= C_TIMEOUT
&& ns
.conn
<= C_TEAR_DOWN
) &&
827 os
.conn
<= C_DISCONNECTING
)
830 /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow */
831 if (os
.conn
>= C_TIMEOUT
&& os
.conn
<= C_TEAR_DOWN
&&
832 ns
.conn
!= C_UNCONNECTED
&& ns
.conn
!= C_DISCONNECTING
)
835 /* After C_DISCONNECTING only C_STANDALONE may follow */
836 if (os
.conn
== C_DISCONNECTING
&& ns
.conn
!= C_STANDALONE
)
839 if (ns
.conn
< C_CONNECTED
) {
842 if (ns
.pdsk
> D_UNKNOWN
|| ns
.pdsk
< D_INCONSISTENT
)
846 /* Clear the aftr_isp when becoming unconfigured */
847 if (ns
.conn
== C_STANDALONE
&& ns
.disk
== D_DISKLESS
&& ns
.role
== R_SECONDARY
)
850 /* Abort resync if a disk fails/detaches */
851 if (os
.conn
> C_CONNECTED
&& ns
.conn
> C_CONNECTED
&&
852 (ns
.disk
<= D_FAILED
|| ns
.pdsk
<= D_FAILED
)) {
854 *warn_sync_abort
= 1;
855 ns
.conn
= C_CONNECTED
;
858 if (ns
.conn
>= C_CONNECTED
&&
859 ((ns
.disk
== D_CONSISTENT
|| ns
.disk
== D_OUTDATED
) ||
860 (ns
.disk
== D_NEGOTIATING
&& ns
.conn
== C_WF_BITMAP_T
))) {
863 case C_PAUSED_SYNC_T
:
864 ns
.disk
= D_OUTDATED
;
869 case C_PAUSED_SYNC_S
:
870 ns
.disk
= D_UP_TO_DATE
;
873 ns
.disk
= D_INCONSISTENT
;
874 dev_warn(DEV
, "Implicitly set disk state Inconsistent!\n");
877 if (os
.disk
== D_OUTDATED
&& ns
.disk
== D_UP_TO_DATE
)
878 dev_warn(DEV
, "Implicitly set disk from Outdated to UpToDate\n");
881 if (ns
.conn
>= C_CONNECTED
&&
882 (ns
.pdsk
== D_CONSISTENT
|| ns
.pdsk
== D_OUTDATED
)) {
886 case C_PAUSED_SYNC_T
:
888 ns
.pdsk
= D_UP_TO_DATE
;
891 case C_PAUSED_SYNC_S
:
892 /* remap any consistent state to D_OUTDATED,
893 * but disallow "upgrade" of not even consistent states.
896 (D_DISKLESS
< os
.pdsk
&& os
.pdsk
< D_OUTDATED
)
897 ? os
.pdsk
: D_OUTDATED
;
900 ns
.pdsk
= D_INCONSISTENT
;
901 dev_warn(DEV
, "Implicitly set pdsk Inconsistent!\n");
904 if (os
.pdsk
== D_OUTDATED
&& ns
.pdsk
== D_UP_TO_DATE
)
905 dev_warn(DEV
, "Implicitly set pdsk from Outdated to UpToDate\n");
908 /* Connection breaks down before we finished "Negotiating" */
909 if (ns
.conn
< C_CONNECTED
&& ns
.disk
== D_NEGOTIATING
&&
910 get_ldev_if_state(mdev
, D_NEGOTIATING
)) {
911 if (mdev
->ed_uuid
== mdev
->ldev
->md
.uuid
[UI_CURRENT
]) {
912 ns
.disk
= mdev
->new_state_tmp
.disk
;
913 ns
.pdsk
= mdev
->new_state_tmp
.pdsk
;
915 dev_alert(DEV
, "Connection lost while negotiating, no data!\n");
916 ns
.disk
= D_DISKLESS
;
922 if (fp
== FP_STONITH
&&
923 (ns
.role
== R_PRIMARY
&& ns
.conn
< C_CONNECTED
&& ns
.pdsk
> D_OUTDATED
) &&
924 !(os
.role
== R_PRIMARY
&& os
.conn
< C_CONNECTED
&& os
.pdsk
> D_OUTDATED
))
925 ns
.susp
= 1; /* Suspend IO while fence-peer handler runs (peer lost) */
927 if (mdev
->sync_conf
.on_no_data
== OND_SUSPEND_IO
&&
928 (ns
.role
== R_PRIMARY
&& ns
.disk
< D_UP_TO_DATE
&& ns
.pdsk
< D_UP_TO_DATE
) &&
929 !(os
.role
== R_PRIMARY
&& os
.disk
< D_UP_TO_DATE
&& os
.pdsk
< D_UP_TO_DATE
))
930 ns
.susp
= 1; /* Suspend IO while no data available (no accessible data available) */
932 if (ns
.aftr_isp
|| ns
.peer_isp
|| ns
.user_isp
) {
933 if (ns
.conn
== C_SYNC_SOURCE
)
934 ns
.conn
= C_PAUSED_SYNC_S
;
935 if (ns
.conn
== C_SYNC_TARGET
)
936 ns
.conn
= C_PAUSED_SYNC_T
;
938 if (ns
.conn
== C_PAUSED_SYNC_S
)
939 ns
.conn
= C_SYNC_SOURCE
;
940 if (ns
.conn
== C_PAUSED_SYNC_T
)
941 ns
.conn
= C_SYNC_TARGET
;
947 /* helper for __drbd_set_state */
948 static void set_ov_position(struct drbd_conf
*mdev
, enum drbd_conns cs
)
950 if (cs
== C_VERIFY_T
) {
951 /* starting online verify from an arbitrary position
952 * does not fit well into the existing protocol.
953 * on C_VERIFY_T, we initialize ov_left and friends
954 * implicitly in receive_DataRequest once the
955 * first P_OV_REQUEST is received */
956 mdev
->ov_start_sector
= ~(sector_t
)0;
958 unsigned long bit
= BM_SECT_TO_BIT(mdev
->ov_start_sector
);
959 if (bit
>= mdev
->rs_total
)
960 mdev
->ov_start_sector
=
961 BM_BIT_TO_SECT(mdev
->rs_total
- 1);
962 mdev
->ov_position
= mdev
->ov_start_sector
;
966 static void drbd_resume_al(struct drbd_conf
*mdev
)
968 if (test_and_clear_bit(AL_SUSPENDED
, &mdev
->flags
))
969 dev_info(DEV
, "Resumed AL updates\n");
973 * __drbd_set_state() - Set a new DRBD state
974 * @mdev: DRBD device.
977 * @done: Optional completion, that will get completed after the after_state_ch() finished
979 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
981 int __drbd_set_state(struct drbd_conf
*mdev
,
982 union drbd_state ns
, enum chg_state_flags flags
,
983 struct completion
*done
)
987 int warn_sync_abort
= 0;
988 struct after_state_chg_work
*ascw
;
992 ns
= sanitize_state(mdev
, os
, ns
, &warn_sync_abort
);
995 return SS_NOTHING_TO_DO
;
997 if (!(flags
& CS_HARD
)) {
998 /* pre-state-change checks ; only look at ns */
999 /* See drbd_state_sw_errors in drbd_strings.c */
1001 rv
= is_valid_state(mdev
, ns
);
1002 if (rv
< SS_SUCCESS
) {
1003 /* If the old state was illegal as well, then let
1006 if (is_valid_state(mdev
, os
) == rv
)
1007 rv
= is_valid_state_transition(mdev
, ns
, os
);
1009 rv
= is_valid_state_transition(mdev
, ns
, os
);
1012 if (rv
< SS_SUCCESS
) {
1013 if (flags
& CS_VERBOSE
)
1014 print_st_err(mdev
, os
, ns
, rv
);
1018 if (warn_sync_abort
)
1019 dev_warn(DEV
, "Resync aborted.\n");
1034 dev_info(DEV
, "%s\n", pb
);
1037 /* solve the race between becoming unconfigured,
1038 * worker doing the cleanup, and
1039 * admin reconfiguring us:
1040 * on (re)configure, first set CONFIG_PENDING,
1041 * then wait for a potentially exiting worker,
1042 * start the worker, and schedule one no_op.
1043 * then proceed with configuration.
1045 if (ns
.disk
== D_DISKLESS
&&
1046 ns
.conn
== C_STANDALONE
&&
1047 ns
.role
== R_SECONDARY
&&
1048 !test_and_set_bit(CONFIG_PENDING
, &mdev
->flags
))
1049 set_bit(DEVICE_DYING
, &mdev
->flags
);
1051 mdev
->state
.i
= ns
.i
;
1052 wake_up(&mdev
->misc_wait
);
1053 wake_up(&mdev
->state_wait
);
1055 /* aborted verify run. log the last position */
1056 if ((os
.conn
== C_VERIFY_S
|| os
.conn
== C_VERIFY_T
) &&
1057 ns
.conn
< C_CONNECTED
) {
1058 mdev
->ov_start_sector
=
1059 BM_BIT_TO_SECT(mdev
->rs_total
- mdev
->ov_left
);
1060 dev_info(DEV
, "Online Verify reached sector %llu\n",
1061 (unsigned long long)mdev
->ov_start_sector
);
1064 if ((os
.conn
== C_PAUSED_SYNC_T
|| os
.conn
== C_PAUSED_SYNC_S
) &&
1065 (ns
.conn
== C_SYNC_TARGET
|| ns
.conn
== C_SYNC_SOURCE
)) {
1066 dev_info(DEV
, "Syncer continues.\n");
1067 mdev
->rs_paused
+= (long)jiffies
1068 -(long)mdev
->rs_mark_time
[mdev
->rs_last_mark
];
1069 if (ns
.conn
== C_SYNC_TARGET
)
1070 mod_timer(&mdev
->resync_timer
, jiffies
);
1073 if ((os
.conn
== C_SYNC_TARGET
|| os
.conn
== C_SYNC_SOURCE
) &&
1074 (ns
.conn
== C_PAUSED_SYNC_T
|| ns
.conn
== C_PAUSED_SYNC_S
)) {
1075 dev_info(DEV
, "Resync suspended\n");
1076 mdev
->rs_mark_time
[mdev
->rs_last_mark
] = jiffies
;
1079 if (os
.conn
== C_CONNECTED
&&
1080 (ns
.conn
== C_VERIFY_S
|| ns
.conn
== C_VERIFY_T
)) {
1081 unsigned long now
= jiffies
;
1084 mdev
->ov_position
= 0;
1085 mdev
->rs_total
= drbd_bm_bits(mdev
);
1086 if (mdev
->agreed_pro_version
>= 90)
1087 set_ov_position(mdev
, ns
.conn
);
1089 mdev
->ov_start_sector
= 0;
1090 mdev
->ov_left
= mdev
->rs_total
1091 - BM_SECT_TO_BIT(mdev
->ov_position
);
1092 mdev
->rs_start
= now
;
1093 mdev
->rs_last_events
= 0;
1094 mdev
->rs_last_sect_ev
= 0;
1095 mdev
->ov_last_oos_size
= 0;
1096 mdev
->ov_last_oos_start
= 0;
1098 for (i
= 0; i
< DRBD_SYNC_MARKS
; i
++) {
1099 mdev
->rs_mark_left
[i
] = mdev
->rs_total
;
1100 mdev
->rs_mark_time
[i
] = now
;
1103 if (ns
.conn
== C_VERIFY_S
) {
1104 dev_info(DEV
, "Starting Online Verify from sector %llu\n",
1105 (unsigned long long)mdev
->ov_position
);
1106 mod_timer(&mdev
->resync_timer
, jiffies
);
1110 if (get_ldev(mdev
)) {
1111 u32 mdf
= mdev
->ldev
->md
.flags
& ~(MDF_CONSISTENT
|MDF_PRIMARY_IND
|
1112 MDF_CONNECTED_IND
|MDF_WAS_UP_TO_DATE
|
1113 MDF_PEER_OUT_DATED
|MDF_CRASHED_PRIMARY
);
1115 if (test_bit(CRASHED_PRIMARY
, &mdev
->flags
))
1116 mdf
|= MDF_CRASHED_PRIMARY
;
1117 if (mdev
->state
.role
== R_PRIMARY
||
1118 (mdev
->state
.pdsk
< D_INCONSISTENT
&& mdev
->state
.peer
== R_PRIMARY
))
1119 mdf
|= MDF_PRIMARY_IND
;
1120 if (mdev
->state
.conn
> C_WF_REPORT_PARAMS
)
1121 mdf
|= MDF_CONNECTED_IND
;
1122 if (mdev
->state
.disk
> D_INCONSISTENT
)
1123 mdf
|= MDF_CONSISTENT
;
1124 if (mdev
->state
.disk
> D_OUTDATED
)
1125 mdf
|= MDF_WAS_UP_TO_DATE
;
1126 if (mdev
->state
.pdsk
<= D_OUTDATED
&& mdev
->state
.pdsk
>= D_INCONSISTENT
)
1127 mdf
|= MDF_PEER_OUT_DATED
;
1128 if (mdf
!= mdev
->ldev
->md
.flags
) {
1129 mdev
->ldev
->md
.flags
= mdf
;
1130 drbd_md_mark_dirty(mdev
);
1132 if (os
.disk
< D_CONSISTENT
&& ns
.disk
>= D_CONSISTENT
)
1133 drbd_set_ed_uuid(mdev
, mdev
->ldev
->md
.uuid
[UI_CURRENT
]);
1137 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1138 if (os
.disk
== D_INCONSISTENT
&& os
.pdsk
== D_INCONSISTENT
&&
1139 os
.peer
== R_SECONDARY
&& ns
.peer
== R_PRIMARY
)
1140 set_bit(CONSIDER_RESYNC
, &mdev
->flags
);
1142 /* Receiver should clean up itself */
1143 if (os
.conn
!= C_DISCONNECTING
&& ns
.conn
== C_DISCONNECTING
)
1144 drbd_thread_stop_nowait(&mdev
->receiver
);
1146 /* Now the receiver finished cleaning up itself, it should die */
1147 if (os
.conn
!= C_STANDALONE
&& ns
.conn
== C_STANDALONE
)
1148 drbd_thread_stop_nowait(&mdev
->receiver
);
1150 /* Upon network failure, we need to restart the receiver. */
1151 if (os
.conn
> C_TEAR_DOWN
&&
1152 ns
.conn
<= C_TEAR_DOWN
&& ns
.conn
>= C_TIMEOUT
)
1153 drbd_thread_restart_nowait(&mdev
->receiver
);
1155 /* Resume AL writing if we get a connection */
1156 if (os
.conn
< C_CONNECTED
&& ns
.conn
>= C_CONNECTED
)
1157 drbd_resume_al(mdev
);
1159 ascw
= kmalloc(sizeof(*ascw
), GFP_ATOMIC
);
1163 ascw
->flags
= flags
;
1164 ascw
->w
.cb
= w_after_state_ch
;
1166 drbd_queue_work(&mdev
->data
.work
, &ascw
->w
);
1168 dev_warn(DEV
, "Could not kmalloc an ascw\n");
1174 static int w_after_state_ch(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
)
1176 struct after_state_chg_work
*ascw
=
1177 container_of(w
, struct after_state_chg_work
, w
);
1178 after_state_ch(mdev
, ascw
->os
, ascw
->ns
, ascw
->flags
);
1179 if (ascw
->flags
& CS_WAIT_COMPLETE
) {
1180 D_ASSERT(ascw
->done
!= NULL
);
1181 complete(ascw
->done
);
1188 static void abw_start_sync(struct drbd_conf
*mdev
, int rv
)
1191 dev_err(DEV
, "Writing the bitmap failed not starting resync.\n");
1192 _drbd_request_state(mdev
, NS(conn
, C_CONNECTED
), CS_VERBOSE
);
1196 switch (mdev
->state
.conn
) {
1197 case C_STARTING_SYNC_T
:
1198 _drbd_request_state(mdev
, NS(conn
, C_WF_SYNC_UUID
), CS_VERBOSE
);
1200 case C_STARTING_SYNC_S
:
1201 drbd_start_resync(mdev
, C_SYNC_SOURCE
);
1207 * after_state_ch() - Perform after state change actions that may sleep
1208 * @mdev: DRBD device.
1213 static void after_state_ch(struct drbd_conf
*mdev
, union drbd_state os
,
1214 union drbd_state ns
, enum chg_state_flags flags
)
1216 enum drbd_fencing_p fp
;
1217 enum drbd_req_event what
= nothing
;
1219 if (os
.conn
!= C_CONNECTED
&& ns
.conn
== C_CONNECTED
) {
1220 clear_bit(CRASHED_PRIMARY
, &mdev
->flags
);
1222 mdev
->p_uuid
[UI_FLAGS
] &= ~((u64
)2);
1226 if (get_ldev(mdev
)) {
1227 fp
= mdev
->ldev
->dc
.fencing
;
1231 /* Inform userspace about the change... */
1232 drbd_bcast_state(mdev
, ns
);
1234 if (!(os
.role
== R_PRIMARY
&& os
.disk
< D_UP_TO_DATE
&& os
.pdsk
< D_UP_TO_DATE
) &&
1235 (ns
.role
== R_PRIMARY
&& ns
.disk
< D_UP_TO_DATE
&& ns
.pdsk
< D_UP_TO_DATE
))
1236 drbd_khelper(mdev
, "pri-on-incon-degr");
1238 /* Here we have the actions that are performed after a
1239 state change. This function might sleep */
1241 if (os
.susp
&& ns
.susp
&& mdev
->sync_conf
.on_no_data
== OND_SUSPEND_IO
) {
1242 if (os
.conn
< C_CONNECTED
&& ns
.conn
>= C_CONNECTED
) {
1243 if (ns
.conn
== C_CONNECTED
)
1245 else /* ns.conn > C_CONNECTED */
1246 dev_err(DEV
, "Unexpected Resynd going on!\n");
1249 if (os
.disk
== D_ATTACHING
&& ns
.disk
> D_ATTACHING
)
1250 what
= restart_frozen_disk_io
;
1253 if (fp
== FP_STONITH
&& ns
.susp
) {
1254 /* case1: The outdate peer handler is successful: */
1255 if (os
.pdsk
> D_OUTDATED
&& ns
.pdsk
<= D_OUTDATED
) {
1257 if (test_bit(NEW_CUR_UUID
, &mdev
->flags
)) {
1258 drbd_uuid_new_current(mdev
);
1259 clear_bit(NEW_CUR_UUID
, &mdev
->flags
);
1262 spin_lock_irq(&mdev
->req_lock
);
1263 _drbd_set_state(_NS(mdev
, susp
, 0), CS_VERBOSE
, NULL
);
1264 spin_unlock_irq(&mdev
->req_lock
);
1266 /* case2: The connection was established again: */
1267 if (os
.conn
< C_CONNECTED
&& ns
.conn
>= C_CONNECTED
) {
1268 clear_bit(NEW_CUR_UUID
, &mdev
->flags
);
1273 if (what
!= nothing
) {
1274 spin_lock_irq(&mdev
->req_lock
);
1275 _tl_restart(mdev
, what
);
1276 _drbd_set_state(_NS(mdev
, susp
, 0), CS_VERBOSE
, NULL
);
1277 spin_unlock_irq(&mdev
->req_lock
);
1280 /* Do not change the order of the if above and the two below... */
1281 if (os
.pdsk
== D_DISKLESS
&& ns
.pdsk
> D_DISKLESS
) { /* attach on the peer */
1282 drbd_send_uuids(mdev
);
1283 drbd_send_state(mdev
);
1285 if (os
.conn
!= C_WF_BITMAP_S
&& ns
.conn
== C_WF_BITMAP_S
)
1286 drbd_queue_bitmap_io(mdev
, &drbd_send_bitmap
, NULL
, "send_bitmap (WFBitMapS)");
1288 /* Lost contact to peer's copy of the data */
1289 if ((os
.pdsk
>= D_INCONSISTENT
&&
1290 os
.pdsk
!= D_UNKNOWN
&&
1291 os
.pdsk
!= D_OUTDATED
)
1292 && (ns
.pdsk
< D_INCONSISTENT
||
1293 ns
.pdsk
== D_UNKNOWN
||
1294 ns
.pdsk
== D_OUTDATED
)) {
1295 if (get_ldev(mdev
)) {
1296 if ((ns
.role
== R_PRIMARY
|| ns
.peer
== R_PRIMARY
) &&
1297 mdev
->ldev
->md
.uuid
[UI_BITMAP
] == 0 && ns
.disk
>= D_UP_TO_DATE
) {
1298 if (mdev
->state
.susp
) {
1299 set_bit(NEW_CUR_UUID
, &mdev
->flags
);
1301 drbd_uuid_new_current(mdev
);
1302 drbd_send_uuids(mdev
);
1309 if (ns
.pdsk
< D_INCONSISTENT
&& get_ldev(mdev
)) {
1310 if (ns
.peer
== R_PRIMARY
&& mdev
->ldev
->md
.uuid
[UI_BITMAP
] == 0) {
1311 drbd_uuid_new_current(mdev
);
1312 drbd_send_uuids(mdev
);
1315 /* D_DISKLESS Peer becomes secondary */
1316 if (os
.peer
== R_PRIMARY
&& ns
.peer
== R_SECONDARY
)
1317 drbd_al_to_on_disk_bm(mdev
);
1321 /* Last part of the attaching process ... */
1322 if (ns
.conn
>= C_CONNECTED
&&
1323 os
.disk
== D_ATTACHING
&& ns
.disk
== D_NEGOTIATING
) {
1324 drbd_send_sizes(mdev
, 0, 0); /* to start sync... */
1325 drbd_send_uuids(mdev
);
1326 drbd_send_state(mdev
);
1329 /* We want to pause/continue resync, tell peer. */
1330 if (ns
.conn
>= C_CONNECTED
&&
1331 ((os
.aftr_isp
!= ns
.aftr_isp
) ||
1332 (os
.user_isp
!= ns
.user_isp
)))
1333 drbd_send_state(mdev
);
1335 /* In case one of the isp bits got set, suspend other devices. */
1336 if ((!os
.aftr_isp
&& !os
.peer_isp
&& !os
.user_isp
) &&
1337 (ns
.aftr_isp
|| ns
.peer_isp
|| ns
.user_isp
))
1338 suspend_other_sg(mdev
);
1340 /* Make sure the peer gets informed about eventual state
1341 changes (ISP bits) while we were in WFReportParams. */
1342 if (os
.conn
== C_WF_REPORT_PARAMS
&& ns
.conn
>= C_CONNECTED
)
1343 drbd_send_state(mdev
);
1345 /* We are in the progress to start a full sync... */
1346 if ((os
.conn
!= C_STARTING_SYNC_T
&& ns
.conn
== C_STARTING_SYNC_T
) ||
1347 (os
.conn
!= C_STARTING_SYNC_S
&& ns
.conn
== C_STARTING_SYNC_S
))
1348 drbd_queue_bitmap_io(mdev
, &drbd_bmio_set_n_write
, &abw_start_sync
, "set_n_write from StartingSync");
1350 /* We are invalidating our self... */
1351 if (os
.conn
< C_CONNECTED
&& ns
.conn
< C_CONNECTED
&&
1352 os
.disk
> D_INCONSISTENT
&& ns
.disk
== D_INCONSISTENT
)
1353 drbd_queue_bitmap_io(mdev
, &drbd_bmio_set_n_write
, NULL
, "set_n_write from invalidate");
1355 if (os
.disk
> D_FAILED
&& ns
.disk
== D_FAILED
) {
1356 enum drbd_io_error_p eh
;
1359 if (get_ldev_if_state(mdev
, D_FAILED
)) {
1360 eh
= mdev
->ldev
->dc
.on_io_error
;
1364 drbd_rs_cancel_all(mdev
);
1365 /* since get_ldev() only works as long as disk>=D_INCONSISTENT,
1366 and it is D_DISKLESS here, local_cnt can only go down, it can
1367 not increase... It will reach zero */
1368 wait_event(mdev
->misc_wait
, !atomic_read(&mdev
->local_cnt
));
1370 mdev
->rs_failed
= 0;
1371 atomic_set(&mdev
->rs_pending_cnt
, 0);
1373 spin_lock_irq(&mdev
->req_lock
);
1374 _drbd_set_state(_NS(mdev
, disk
, D_DISKLESS
), CS_HARD
, NULL
);
1375 spin_unlock_irq(&mdev
->req_lock
);
1377 if (eh
== EP_CALL_HELPER
)
1378 drbd_khelper(mdev
, "local-io-error");
1381 if (os
.disk
> D_DISKLESS
&& ns
.disk
== D_DISKLESS
) {
1383 if (os
.disk
== D_FAILED
) /* && ns.disk == D_DISKLESS*/ {
1384 if (drbd_send_state(mdev
))
1385 dev_warn(DEV
, "Notified peer that my disk is broken.\n");
1387 dev_err(DEV
, "Sending state in drbd_io_error() failed\n");
1390 wait_event(mdev
->misc_wait
, !atomic_read(&mdev
->local_cnt
));
1391 lc_destroy(mdev
->resync
);
1392 mdev
->resync
= NULL
;
1393 lc_destroy(mdev
->act_log
);
1394 mdev
->act_log
= NULL
;
1396 drbd_free_bc(mdev
->ldev
);
1397 mdev
->ldev
= NULL
;);
1399 if (mdev
->md_io_tmpp
)
1400 __free_page(mdev
->md_io_tmpp
);
1403 /* Disks got bigger while they were detached */
1404 if (ns
.disk
> D_NEGOTIATING
&& ns
.pdsk
> D_NEGOTIATING
&&
1405 test_and_clear_bit(RESYNC_AFTER_NEG
, &mdev
->flags
)) {
1406 if (ns
.conn
== C_CONNECTED
)
1407 resync_after_online_grow(mdev
);
1410 /* A resync finished or aborted, wake paused devices... */
1411 if ((os
.conn
> C_CONNECTED
&& ns
.conn
<= C_CONNECTED
) ||
1412 (os
.peer_isp
&& !ns
.peer_isp
) ||
1413 (os
.user_isp
&& !ns
.user_isp
))
1414 resume_next_sg(mdev
);
1416 /* free tl_hash if we Got thawed and are C_STANDALONE */
1417 if (ns
.conn
== C_STANDALONE
&& ns
.susp
== 0 && mdev
->tl_hash
)
1418 drbd_free_tl_hash(mdev
);
1420 /* Upon network connection, we need to start the receiver */
1421 if (os
.conn
== C_STANDALONE
&& ns
.conn
== C_UNCONNECTED
)
1422 drbd_thread_start(&mdev
->receiver
);
1424 /* Terminate worker thread if we are unconfigured - it will be
1425 restarted as needed... */
1426 if (ns
.disk
== D_DISKLESS
&&
1427 ns
.conn
== C_STANDALONE
&&
1428 ns
.role
== R_SECONDARY
) {
1429 if (os
.aftr_isp
!= ns
.aftr_isp
)
1430 resume_next_sg(mdev
);
1431 /* set in __drbd_set_state, unless CONFIG_PENDING was set */
1432 if (test_bit(DEVICE_DYING
, &mdev
->flags
))
1433 drbd_thread_stop_nowait(&mdev
->worker
);
1440 static int drbd_thread_setup(void *arg
)
1442 struct drbd_thread
*thi
= (struct drbd_thread
*) arg
;
1443 struct drbd_conf
*mdev
= thi
->mdev
;
1444 unsigned long flags
;
1448 retval
= thi
->function(thi
);
1450 spin_lock_irqsave(&thi
->t_lock
, flags
);
1452 /* if the receiver has been "Exiting", the last thing it did
1453 * was set the conn state to "StandAlone",
1454 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
1455 * and receiver thread will be "started".
1456 * drbd_thread_start needs to set "Restarting" in that case.
1457 * t_state check and assignment needs to be within the same spinlock,
1458 * so either thread_start sees Exiting, and can remap to Restarting,
1459 * or thread_start see None, and can proceed as normal.
1462 if (thi
->t_state
== Restarting
) {
1463 dev_info(DEV
, "Restarting %s\n", current
->comm
);
1464 thi
->t_state
= Running
;
1465 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1470 thi
->t_state
= None
;
1472 complete(&thi
->stop
);
1473 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1475 dev_info(DEV
, "Terminating %s\n", current
->comm
);
1477 /* Release mod reference taken when thread was started */
1478 module_put(THIS_MODULE
);
1482 static void drbd_thread_init(struct drbd_conf
*mdev
, struct drbd_thread
*thi
,
1483 int (*func
) (struct drbd_thread
*))
1485 spin_lock_init(&thi
->t_lock
);
1487 thi
->t_state
= None
;
1488 thi
->function
= func
;
1492 int drbd_thread_start(struct drbd_thread
*thi
)
1494 struct drbd_conf
*mdev
= thi
->mdev
;
1495 struct task_struct
*nt
;
1496 unsigned long flags
;
1499 thi
== &mdev
->receiver
? "receiver" :
1500 thi
== &mdev
->asender
? "asender" :
1501 thi
== &mdev
->worker
? "worker" : "NONSENSE";
1503 /* is used from state engine doing drbd_thread_stop_nowait,
1504 * while holding the req lock irqsave */
1505 spin_lock_irqsave(&thi
->t_lock
, flags
);
1507 switch (thi
->t_state
) {
1509 dev_info(DEV
, "Starting %s thread (from %s [%d])\n",
1510 me
, current
->comm
, current
->pid
);
1512 /* Get ref on module for thread - this is released when thread exits */
1513 if (!try_module_get(THIS_MODULE
)) {
1514 dev_err(DEV
, "Failed to get module reference in drbd_thread_start\n");
1515 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1519 init_completion(&thi
->stop
);
1520 D_ASSERT(thi
->task
== NULL
);
1521 thi
->reset_cpu_mask
= 1;
1522 thi
->t_state
= Running
;
1523 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1524 flush_signals(current
); /* otherw. may get -ERESTARTNOINTR */
1526 nt
= kthread_create(drbd_thread_setup
, (void *) thi
,
1527 "drbd%d_%s", mdev_to_minor(mdev
), me
);
1530 dev_err(DEV
, "Couldn't start thread\n");
1532 module_put(THIS_MODULE
);
1535 spin_lock_irqsave(&thi
->t_lock
, flags
);
1537 thi
->t_state
= Running
;
1538 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1539 wake_up_process(nt
);
1542 thi
->t_state
= Restarting
;
1543 dev_info(DEV
, "Restarting %s thread (from %s [%d])\n",
1544 me
, current
->comm
, current
->pid
);
1549 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1557 void _drbd_thread_stop(struct drbd_thread
*thi
, int restart
, int wait
)
1559 unsigned long flags
;
1561 enum drbd_thread_state ns
= restart
? Restarting
: Exiting
;
1563 /* may be called from state engine, holding the req lock irqsave */
1564 spin_lock_irqsave(&thi
->t_lock
, flags
);
1566 if (thi
->t_state
== None
) {
1567 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1569 drbd_thread_start(thi
);
1573 if (thi
->t_state
!= ns
) {
1574 if (thi
->task
== NULL
) {
1575 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1581 init_completion(&thi
->stop
);
1582 if (thi
->task
!= current
)
1583 force_sig(DRBD_SIGKILL
, thi
->task
);
1587 spin_unlock_irqrestore(&thi
->t_lock
, flags
);
1590 wait_for_completion(&thi
->stop
);
1595 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
1596 * @mdev: DRBD device.
1598 * Forces all threads of a device onto the same CPU. This is beneficial for
1599 * DRBD's performance. May be overwritten by user's configuration.
1601 void drbd_calc_cpu_mask(struct drbd_conf
*mdev
)
1605 /* user override. */
1606 if (cpumask_weight(mdev
->cpu_mask
))
1609 ord
= mdev_to_minor(mdev
) % cpumask_weight(cpu_online_mask
);
1610 for_each_online_cpu(cpu
) {
1612 cpumask_set_cpu(cpu
, mdev
->cpu_mask
);
1616 /* should not be reached */
1617 cpumask_setall(mdev
->cpu_mask
);
1621 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
1622 * @mdev: DRBD device.
1624 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
1627 void drbd_thread_current_set_cpu(struct drbd_conf
*mdev
)
1629 struct task_struct
*p
= current
;
1630 struct drbd_thread
*thi
=
1631 p
== mdev
->asender
.task
? &mdev
->asender
:
1632 p
== mdev
->receiver
.task
? &mdev
->receiver
:
1633 p
== mdev
->worker
.task
? &mdev
->worker
:
1637 if (!thi
->reset_cpu_mask
)
1639 thi
->reset_cpu_mask
= 0;
1640 set_cpus_allowed_ptr(p
, mdev
->cpu_mask
);
1644 /* the appropriate socket mutex must be held already */
1645 int _drbd_send_cmd(struct drbd_conf
*mdev
, struct socket
*sock
,
1646 enum drbd_packets cmd
, struct p_header80
*h
,
1647 size_t size
, unsigned msg_flags
)
1651 ERR_IF(!h
) return FALSE
;
1652 ERR_IF(!size
) return FALSE
;
1654 h
->magic
= BE_DRBD_MAGIC
;
1655 h
->command
= cpu_to_be16(cmd
);
1656 h
->length
= cpu_to_be16(size
-sizeof(struct p_header80
));
1658 sent
= drbd_send(mdev
, sock
, h
, size
, msg_flags
);
1660 ok
= (sent
== size
);
1662 dev_err(DEV
, "short sent %s size=%d sent=%d\n",
1663 cmdname(cmd
), (int)size
, sent
);
1667 /* don't pass the socket. we may only look at it
1668 * when we hold the appropriate socket mutex.
1670 int drbd_send_cmd(struct drbd_conf
*mdev
, int use_data_socket
,
1671 enum drbd_packets cmd
, struct p_header80
*h
, size_t size
)
1674 struct socket
*sock
;
1676 if (use_data_socket
) {
1677 mutex_lock(&mdev
->data
.mutex
);
1678 sock
= mdev
->data
.socket
;
1680 mutex_lock(&mdev
->meta
.mutex
);
1681 sock
= mdev
->meta
.socket
;
1684 /* drbd_disconnect() could have called drbd_free_sock()
1685 * while we were waiting in down()... */
1686 if (likely(sock
!= NULL
))
1687 ok
= _drbd_send_cmd(mdev
, sock
, cmd
, h
, size
, 0);
1689 if (use_data_socket
)
1690 mutex_unlock(&mdev
->data
.mutex
);
1692 mutex_unlock(&mdev
->meta
.mutex
);
1696 int drbd_send_cmd2(struct drbd_conf
*mdev
, enum drbd_packets cmd
, char *data
,
1699 struct p_header80 h
;
1702 h
.magic
= BE_DRBD_MAGIC
;
1703 h
.command
= cpu_to_be16(cmd
);
1704 h
.length
= cpu_to_be16(size
);
1706 if (!drbd_get_data_sock(mdev
))
1710 drbd_send(mdev
, mdev
->data
.socket
, &h
, sizeof(h
), 0));
1712 drbd_send(mdev
, mdev
->data
.socket
, data
, size
, 0));
1714 drbd_put_data_sock(mdev
);
1719 int drbd_send_sync_param(struct drbd_conf
*mdev
, struct syncer_conf
*sc
)
1721 struct p_rs_param_95
*p
;
1722 struct socket
*sock
;
1724 const int apv
= mdev
->agreed_pro_version
;
1726 size
= apv
<= 87 ? sizeof(struct p_rs_param
)
1727 : apv
== 88 ? sizeof(struct p_rs_param
)
1728 + strlen(mdev
->sync_conf
.verify_alg
) + 1
1729 : apv
<= 94 ? sizeof(struct p_rs_param_89
)
1730 : /* apv >= 95 */ sizeof(struct p_rs_param_95
);
1732 /* used from admin command context and receiver/worker context.
1733 * to avoid kmalloc, grab the socket right here,
1734 * then use the pre-allocated sbuf there */
1735 mutex_lock(&mdev
->data
.mutex
);
1736 sock
= mdev
->data
.socket
;
1738 if (likely(sock
!= NULL
)) {
1739 enum drbd_packets cmd
= apv
>= 89 ? P_SYNC_PARAM89
: P_SYNC_PARAM
;
1741 p
= &mdev
->data
.sbuf
.rs_param_95
;
1743 /* initialize verify_alg and csums_alg */
1744 memset(p
->verify_alg
, 0, 2 * SHARED_SECRET_MAX
);
1746 p
->rate
= cpu_to_be32(sc
->rate
);
1747 p
->c_plan_ahead
= cpu_to_be32(sc
->c_plan_ahead
);
1748 p
->c_delay_target
= cpu_to_be32(sc
->c_delay_target
);
1749 p
->c_fill_target
= cpu_to_be32(sc
->c_fill_target
);
1750 p
->c_max_rate
= cpu_to_be32(sc
->c_max_rate
);
1753 strcpy(p
->verify_alg
, mdev
->sync_conf
.verify_alg
);
1755 strcpy(p
->csums_alg
, mdev
->sync_conf
.csums_alg
);
1757 rv
= _drbd_send_cmd(mdev
, sock
, cmd
, &p
->head
, size
, 0);
1759 rv
= 0; /* not ok */
1761 mutex_unlock(&mdev
->data
.mutex
);
1766 int drbd_send_protocol(struct drbd_conf
*mdev
)
1768 struct p_protocol
*p
;
1771 size
= sizeof(struct p_protocol
);
1773 if (mdev
->agreed_pro_version
>= 87)
1774 size
+= strlen(mdev
->net_conf
->integrity_alg
) + 1;
1776 /* we must not recurse into our own queue,
1777 * as that is blocked during handshake */
1778 p
= kmalloc(size
, GFP_NOIO
);
1782 p
->protocol
= cpu_to_be32(mdev
->net_conf
->wire_protocol
);
1783 p
->after_sb_0p
= cpu_to_be32(mdev
->net_conf
->after_sb_0p
);
1784 p
->after_sb_1p
= cpu_to_be32(mdev
->net_conf
->after_sb_1p
);
1785 p
->after_sb_2p
= cpu_to_be32(mdev
->net_conf
->after_sb_2p
);
1786 p
->two_primaries
= cpu_to_be32(mdev
->net_conf
->two_primaries
);
1789 if (mdev
->net_conf
->want_lose
)
1791 if (mdev
->net_conf
->dry_run
) {
1792 if (mdev
->agreed_pro_version
>= 92)
1795 dev_err(DEV
, "--dry-run is not supported by peer");
1800 p
->conn_flags
= cpu_to_be32(cf
);
1802 if (mdev
->agreed_pro_version
>= 87)
1803 strcpy(p
->integrity_alg
, mdev
->net_conf
->integrity_alg
);
1805 rv
= drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_PROTOCOL
,
1806 (struct p_header80
*)p
, size
);
1811 int _drbd_send_uuids(struct drbd_conf
*mdev
, u64 uuid_flags
)
1816 if (!get_ldev_if_state(mdev
, D_NEGOTIATING
))
1819 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
1820 p
.uuid
[i
] = mdev
->ldev
? cpu_to_be64(mdev
->ldev
->md
.uuid
[i
]) : 0;
1822 mdev
->comm_bm_set
= drbd_bm_total_weight(mdev
);
1823 p
.uuid
[UI_SIZE
] = cpu_to_be64(mdev
->comm_bm_set
);
1824 uuid_flags
|= mdev
->net_conf
->want_lose
? 1 : 0;
1825 uuid_flags
|= test_bit(CRASHED_PRIMARY
, &mdev
->flags
) ? 2 : 0;
1826 uuid_flags
|= mdev
->new_state_tmp
.disk
== D_INCONSISTENT
? 4 : 0;
1827 p
.uuid
[UI_FLAGS
] = cpu_to_be64(uuid_flags
);
1831 return drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_UUIDS
,
1832 (struct p_header80
*)&p
, sizeof(p
));
1835 int drbd_send_uuids(struct drbd_conf
*mdev
)
1837 return _drbd_send_uuids(mdev
, 0);
1840 int drbd_send_uuids_skip_initial_sync(struct drbd_conf
*mdev
)
1842 return _drbd_send_uuids(mdev
, 8);
1846 int drbd_send_sync_uuid(struct drbd_conf
*mdev
, u64 val
)
1850 p
.uuid
= cpu_to_be64(val
);
1852 return drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_SYNC_UUID
,
1853 (struct p_header80
*)&p
, sizeof(p
));
1856 int drbd_send_sizes(struct drbd_conf
*mdev
, int trigger_reply
, enum dds_flags flags
)
1859 sector_t d_size
, u_size
;
1863 if (get_ldev_if_state(mdev
, D_NEGOTIATING
)) {
1864 D_ASSERT(mdev
->ldev
->backing_bdev
);
1865 d_size
= drbd_get_max_capacity(mdev
->ldev
);
1866 u_size
= mdev
->ldev
->dc
.disk_size
;
1867 q_order_type
= drbd_queue_order_type(mdev
);
1872 q_order_type
= QUEUE_ORDERED_NONE
;
1875 p
.d_size
= cpu_to_be64(d_size
);
1876 p
.u_size
= cpu_to_be64(u_size
);
1877 p
.c_size
= cpu_to_be64(trigger_reply
? 0 : drbd_get_capacity(mdev
->this_bdev
));
1878 p
.max_segment_size
= cpu_to_be32(queue_max_segment_size(mdev
->rq_queue
));
1879 p
.queue_order_type
= cpu_to_be16(q_order_type
);
1880 p
.dds_flags
= cpu_to_be16(flags
);
1882 ok
= drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_SIZES
,
1883 (struct p_header80
*)&p
, sizeof(p
));
1888 * drbd_send_state() - Sends the drbd state to the peer
1889 * @mdev: DRBD device.
1891 int drbd_send_state(struct drbd_conf
*mdev
)
1893 struct socket
*sock
;
1897 /* Grab state lock so we wont send state if we're in the middle
1898 * of a cluster wide state change on another thread */
1899 drbd_state_lock(mdev
);
1901 mutex_lock(&mdev
->data
.mutex
);
1903 p
.state
= cpu_to_be32(mdev
->state
.i
); /* Within the send mutex */
1904 sock
= mdev
->data
.socket
;
1906 if (likely(sock
!= NULL
)) {
1907 ok
= _drbd_send_cmd(mdev
, sock
, P_STATE
,
1908 (struct p_header80
*)&p
, sizeof(p
), 0);
1911 mutex_unlock(&mdev
->data
.mutex
);
1913 drbd_state_unlock(mdev
);
1917 int drbd_send_state_req(struct drbd_conf
*mdev
,
1918 union drbd_state mask
, union drbd_state val
)
1920 struct p_req_state p
;
1922 p
.mask
= cpu_to_be32(mask
.i
);
1923 p
.val
= cpu_to_be32(val
.i
);
1925 return drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_STATE_CHG_REQ
,
1926 (struct p_header80
*)&p
, sizeof(p
));
1929 int drbd_send_sr_reply(struct drbd_conf
*mdev
, int retcode
)
1931 struct p_req_state_reply p
;
1933 p
.retcode
= cpu_to_be32(retcode
);
1935 return drbd_send_cmd(mdev
, USE_META_SOCKET
, P_STATE_CHG_REPLY
,
1936 (struct p_header80
*)&p
, sizeof(p
));
1939 int fill_bitmap_rle_bits(struct drbd_conf
*mdev
,
1940 struct p_compressed_bm
*p
,
1941 struct bm_xfer_ctx
*c
)
1943 struct bitstream bs
;
1944 unsigned long plain_bits
;
1951 /* may we use this feature? */
1952 if ((mdev
->sync_conf
.use_rle
== 0) ||
1953 (mdev
->agreed_pro_version
< 90))
1956 if (c
->bit_offset
>= c
->bm_bits
)
1957 return 0; /* nothing to do. */
1959 /* use at most thus many bytes */
1960 bitstream_init(&bs
, p
->code
, BM_PACKET_VLI_BYTES_MAX
, 0);
1961 memset(p
->code
, 0, BM_PACKET_VLI_BYTES_MAX
);
1962 /* plain bits covered in this code string */
1965 /* p->encoding & 0x80 stores whether the first run length is set.
1966 * bit offset is implicit.
1967 * start with toggle == 2 to be able to tell the first iteration */
1970 /* see how much plain bits we can stuff into one packet
1971 * using RLE and VLI. */
1973 tmp
= (toggle
== 0) ? _drbd_bm_find_next_zero(mdev
, c
->bit_offset
)
1974 : _drbd_bm_find_next(mdev
, c
->bit_offset
);
1977 rl
= tmp
- c
->bit_offset
;
1979 if (toggle
== 2) { /* first iteration */
1981 /* the first checked bit was set,
1982 * store start value, */
1983 DCBP_set_start(p
, 1);
1984 /* but skip encoding of zero run length */
1988 DCBP_set_start(p
, 0);
1991 /* paranoia: catch zero runlength.
1992 * can only happen if bitmap is modified while we scan it. */
1994 dev_err(DEV
, "unexpected zero runlength while encoding bitmap "
1995 "t:%u bo:%lu\n", toggle
, c
->bit_offset
);
1999 bits
= vli_encode_bits(&bs
, rl
);
2000 if (bits
== -ENOBUFS
) /* buffer full */
2003 dev_err(DEV
, "error while encoding bitmap: %d\n", bits
);
2009 c
->bit_offset
= tmp
;
2010 } while (c
->bit_offset
< c
->bm_bits
);
2012 len
= bs
.cur
.b
- p
->code
+ !!bs
.cur
.bit
;
2014 if (plain_bits
< (len
<< 3)) {
2015 /* incompressible with this method.
2016 * we need to rewind both word and bit position. */
2017 c
->bit_offset
-= plain_bits
;
2018 bm_xfer_ctx_bit_to_word_offset(c
);
2019 c
->bit_offset
= c
->word_offset
* BITS_PER_LONG
;
2023 /* RLE + VLI was able to compress it just fine.
2024 * update c->word_offset. */
2025 bm_xfer_ctx_bit_to_word_offset(c
);
2027 /* store pad_bits */
2028 DCBP_set_pad_bits(p
, (8 - bs
.cur
.bit
) & 0x7);
2033 enum { OK
, FAILED
, DONE
}
2034 send_bitmap_rle_or_plain(struct drbd_conf
*mdev
,
2035 struct p_header80
*h
, struct bm_xfer_ctx
*c
)
2037 struct p_compressed_bm
*p
= (void*)h
;
2038 unsigned long num_words
;
2042 len
= fill_bitmap_rle_bits(mdev
, p
, c
);
2048 DCBP_set_code(p
, RLE_VLI_Bits
);
2049 ok
= _drbd_send_cmd(mdev
, mdev
->data
.socket
, P_COMPRESSED_BITMAP
, h
,
2050 sizeof(*p
) + len
, 0);
2053 c
->bytes
[0] += sizeof(*p
) + len
;
2055 if (c
->bit_offset
>= c
->bm_bits
)
2058 /* was not compressible.
2059 * send a buffer full of plain text bits instead. */
2060 num_words
= min_t(size_t, BM_PACKET_WORDS
, c
->bm_words
- c
->word_offset
);
2061 len
= num_words
* sizeof(long);
2063 drbd_bm_get_lel(mdev
, c
->word_offset
, num_words
, (unsigned long*)h
->payload
);
2064 ok
= _drbd_send_cmd(mdev
, mdev
->data
.socket
, P_BITMAP
,
2065 h
, sizeof(struct p_header80
) + len
, 0);
2066 c
->word_offset
+= num_words
;
2067 c
->bit_offset
= c
->word_offset
* BITS_PER_LONG
;
2070 c
->bytes
[1] += sizeof(struct p_header80
) + len
;
2072 if (c
->bit_offset
> c
->bm_bits
)
2073 c
->bit_offset
= c
->bm_bits
;
2075 ok
= ok
? ((len
== 0) ? DONE
: OK
) : FAILED
;
2078 INFO_bm_xfer_stats(mdev
, "send", c
);
2082 /* See the comment at receive_bitmap() */
2083 int _drbd_send_bitmap(struct drbd_conf
*mdev
)
2085 struct bm_xfer_ctx c
;
2086 struct p_header80
*p
;
2089 ERR_IF(!mdev
->bitmap
) return FALSE
;
2091 /* maybe we should use some per thread scratch page,
2092 * and allocate that during initial device creation? */
2093 p
= (struct p_header80
*) __get_free_page(GFP_NOIO
);
2095 dev_err(DEV
, "failed to allocate one page buffer in %s\n", __func__
);
2099 if (get_ldev(mdev
)) {
2100 if (drbd_md_test_flag(mdev
->ldev
, MDF_FULL_SYNC
)) {
2101 dev_info(DEV
, "Writing the whole bitmap, MDF_FullSync was set.\n");
2102 drbd_bm_set_all(mdev
);
2103 if (drbd_bm_write(mdev
)) {
2104 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
2105 * but otherwise process as per normal - need to tell other
2106 * side that a full resync is required! */
2107 dev_err(DEV
, "Failed to write bitmap to disk!\n");
2109 drbd_md_clear_flag(mdev
, MDF_FULL_SYNC
);
2116 c
= (struct bm_xfer_ctx
) {
2117 .bm_bits
= drbd_bm_bits(mdev
),
2118 .bm_words
= drbd_bm_words(mdev
),
2122 ret
= send_bitmap_rle_or_plain(mdev
, p
, &c
);
2123 } while (ret
== OK
);
2125 free_page((unsigned long) p
);
2126 return (ret
== DONE
);
2129 int drbd_send_bitmap(struct drbd_conf
*mdev
)
2133 if (!drbd_get_data_sock(mdev
))
2135 err
= !_drbd_send_bitmap(mdev
);
2136 drbd_put_data_sock(mdev
);
2140 int drbd_send_b_ack(struct drbd_conf
*mdev
, u32 barrier_nr
, u32 set_size
)
2143 struct p_barrier_ack p
;
2145 p
.barrier
= barrier_nr
;
2146 p
.set_size
= cpu_to_be32(set_size
);
2148 if (mdev
->state
.conn
< C_CONNECTED
)
2150 ok
= drbd_send_cmd(mdev
, USE_META_SOCKET
, P_BARRIER_ACK
,
2151 (struct p_header80
*)&p
, sizeof(p
));
2156 * _drbd_send_ack() - Sends an ack packet
2157 * @mdev: DRBD device.
2158 * @cmd: Packet command code.
2159 * @sector: sector, needs to be in big endian byte order
2160 * @blksize: size in byte, needs to be in big endian byte order
2161 * @block_id: Id, big endian byte order
2163 static int _drbd_send_ack(struct drbd_conf
*mdev
, enum drbd_packets cmd
,
2169 struct p_block_ack p
;
2172 p
.block_id
= block_id
;
2173 p
.blksize
= blksize
;
2174 p
.seq_num
= cpu_to_be32(atomic_add_return(1, &mdev
->packet_seq
));
2176 if (!mdev
->meta
.socket
|| mdev
->state
.conn
< C_CONNECTED
)
2178 ok
= drbd_send_cmd(mdev
, USE_META_SOCKET
, cmd
,
2179 (struct p_header80
*)&p
, sizeof(p
));
2183 int drbd_send_ack_dp(struct drbd_conf
*mdev
, enum drbd_packets cmd
,
2186 const int header_size
= sizeof(struct p_data
)
2187 - sizeof(struct p_header80
);
2188 int data_size
= ((struct p_header80
*)dp
)->length
- header_size
;
2190 return _drbd_send_ack(mdev
, cmd
, dp
->sector
, cpu_to_be32(data_size
),
2194 int drbd_send_ack_rp(struct drbd_conf
*mdev
, enum drbd_packets cmd
,
2195 struct p_block_req
*rp
)
2197 return _drbd_send_ack(mdev
, cmd
, rp
->sector
, rp
->blksize
, rp
->block_id
);
2201 * drbd_send_ack() - Sends an ack packet
2202 * @mdev: DRBD device.
2203 * @cmd: Packet command code.
2206 int drbd_send_ack(struct drbd_conf
*mdev
,
2207 enum drbd_packets cmd
, struct drbd_epoch_entry
*e
)
2209 return _drbd_send_ack(mdev
, cmd
,
2210 cpu_to_be64(e
->sector
),
2211 cpu_to_be32(e
->size
),
2215 /* This function misuses the block_id field to signal if the blocks
2216 * are is sync or not. */
2217 int drbd_send_ack_ex(struct drbd_conf
*mdev
, enum drbd_packets cmd
,
2218 sector_t sector
, int blksize
, u64 block_id
)
2220 return _drbd_send_ack(mdev
, cmd
,
2221 cpu_to_be64(sector
),
2222 cpu_to_be32(blksize
),
2223 cpu_to_be64(block_id
));
2226 int drbd_send_drequest(struct drbd_conf
*mdev
, int cmd
,
2227 sector_t sector
, int size
, u64 block_id
)
2230 struct p_block_req p
;
2232 p
.sector
= cpu_to_be64(sector
);
2233 p
.block_id
= block_id
;
2234 p
.blksize
= cpu_to_be32(size
);
2236 ok
= drbd_send_cmd(mdev
, USE_DATA_SOCKET
, cmd
,
2237 (struct p_header80
*)&p
, sizeof(p
));
2241 int drbd_send_drequest_csum(struct drbd_conf
*mdev
,
2242 sector_t sector
, int size
,
2243 void *digest
, int digest_size
,
2244 enum drbd_packets cmd
)
2247 struct p_block_req p
;
2249 p
.sector
= cpu_to_be64(sector
);
2250 p
.block_id
= BE_DRBD_MAGIC
+ 0xbeef;
2251 p
.blksize
= cpu_to_be32(size
);
2253 p
.head
.magic
= BE_DRBD_MAGIC
;
2254 p
.head
.command
= cpu_to_be16(cmd
);
2255 p
.head
.length
= cpu_to_be16(sizeof(p
) - sizeof(struct p_header80
) + digest_size
);
2257 mutex_lock(&mdev
->data
.mutex
);
2259 ok
= (sizeof(p
) == drbd_send(mdev
, mdev
->data
.socket
, &p
, sizeof(p
), 0));
2260 ok
= ok
&& (digest_size
== drbd_send(mdev
, mdev
->data
.socket
, digest
, digest_size
, 0));
2262 mutex_unlock(&mdev
->data
.mutex
);
2267 int drbd_send_ov_request(struct drbd_conf
*mdev
, sector_t sector
, int size
)
2270 struct p_block_req p
;
2272 p
.sector
= cpu_to_be64(sector
);
2273 p
.block_id
= BE_DRBD_MAGIC
+ 0xbabe;
2274 p
.blksize
= cpu_to_be32(size
);
2276 ok
= drbd_send_cmd(mdev
, USE_DATA_SOCKET
, P_OV_REQUEST
,
2277 (struct p_header80
*)&p
, sizeof(p
));
2281 /* called on sndtimeo
2282 * returns FALSE if we should retry,
2283 * TRUE if we think connection is dead
2285 static int we_should_drop_the_connection(struct drbd_conf
*mdev
, struct socket
*sock
)
2288 /* long elapsed = (long)(jiffies - mdev->last_received); */
2290 drop_it
= mdev
->meta
.socket
== sock
2291 || !mdev
->asender
.task
2292 || get_t_state(&mdev
->asender
) != Running
2293 || mdev
->state
.conn
< C_CONNECTED
;
2298 drop_it
= !--mdev
->ko_count
;
2300 dev_err(DEV
, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
2301 current
->comm
, current
->pid
, mdev
->ko_count
);
2305 return drop_it
; /* && (mdev->state == R_PRIMARY) */;
2308 /* The idea of sendpage seems to be to put some kind of reference
2309 * to the page into the skb, and to hand it over to the NIC. In
2310 * this process get_page() gets called.
2312 * As soon as the page was really sent over the network put_page()
2313 * gets called by some part of the network layer. [ NIC driver? ]
2315 * [ get_page() / put_page() increment/decrement the count. If count
2316 * reaches 0 the page will be freed. ]
2318 * This works nicely with pages from FSs.
2319 * But this means that in protocol A we might signal IO completion too early!
2321 * In order not to corrupt data during a resync we must make sure
2322 * that we do not reuse our own buffer pages (EEs) to early, therefore
2323 * we have the net_ee list.
2325 * XFS seems to have problems, still, it submits pages with page_count == 0!
2326 * As a workaround, we disable sendpage on pages
2327 * with page_count == 0 or PageSlab.
2329 static int _drbd_no_send_page(struct drbd_conf
*mdev
, struct page
*page
,
2330 int offset
, size_t size
, unsigned msg_flags
)
2332 int sent
= drbd_send(mdev
, mdev
->data
.socket
, kmap(page
) + offset
, size
, msg_flags
);
2335 mdev
->send_cnt
+= size
>>9;
2336 return sent
== size
;
2339 static int _drbd_send_page(struct drbd_conf
*mdev
, struct page
*page
,
2340 int offset
, size_t size
, unsigned msg_flags
)
2342 mm_segment_t oldfs
= get_fs();
2346 /* e.g. XFS meta- & log-data is in slab pages, which have a
2347 * page_count of 0 and/or have PageSlab() set.
2348 * we cannot use send_page for those, as that does get_page();
2349 * put_page(); and would cause either a VM_BUG directly, or
2350 * __page_cache_release a page that would actually still be referenced
2351 * by someone, leading to some obscure delayed Oops somewhere else. */
2352 if (disable_sendpage
|| (page_count(page
) < 1) || PageSlab(page
))
2353 return _drbd_no_send_page(mdev
, page
, offset
, size
, msg_flags
);
2355 msg_flags
|= MSG_NOSIGNAL
;
2356 drbd_update_congested(mdev
);
2359 sent
= mdev
->data
.socket
->ops
->sendpage(mdev
->data
.socket
, page
,
2362 if (sent
== -EAGAIN
) {
2363 if (we_should_drop_the_connection(mdev
,
2370 dev_warn(DEV
, "%s: size=%d len=%d sent=%d\n",
2371 __func__
, (int)size
, len
, sent
);
2376 } while (len
> 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
2378 clear_bit(NET_CONGESTED
, &mdev
->flags
);
2382 mdev
->send_cnt
+= size
>>9;
2386 static int _drbd_send_bio(struct drbd_conf
*mdev
, struct bio
*bio
)
2388 struct bio_vec
*bvec
;
2390 /* hint all but last page with MSG_MORE */
2391 __bio_for_each_segment(bvec
, bio
, i
, 0) {
2392 if (!_drbd_no_send_page(mdev
, bvec
->bv_page
,
2393 bvec
->bv_offset
, bvec
->bv_len
,
2394 i
== bio
->bi_vcnt
-1 ? 0 : MSG_MORE
))
2400 static int _drbd_send_zc_bio(struct drbd_conf
*mdev
, struct bio
*bio
)
2402 struct bio_vec
*bvec
;
2404 /* hint all but last page with MSG_MORE */
2405 __bio_for_each_segment(bvec
, bio
, i
, 0) {
2406 if (!_drbd_send_page(mdev
, bvec
->bv_page
,
2407 bvec
->bv_offset
, bvec
->bv_len
,
2408 i
== bio
->bi_vcnt
-1 ? 0 : MSG_MORE
))
2414 static int _drbd_send_zc_ee(struct drbd_conf
*mdev
, struct drbd_epoch_entry
*e
)
2416 struct page
*page
= e
->pages
;
2417 unsigned len
= e
->size
;
2418 /* hint all but last page with MSG_MORE */
2419 page_chain_for_each(page
) {
2420 unsigned l
= min_t(unsigned, len
, PAGE_SIZE
);
2421 if (!_drbd_send_page(mdev
, page
, 0, l
,
2422 page_chain_next(page
) ? MSG_MORE
: 0))
2429 static u32
bio_flags_to_wire(struct drbd_conf
*mdev
, unsigned long bi_rw
)
2431 if (mdev
->agreed_pro_version
>= 95)
2432 return (bi_rw
& REQ_SYNC
? DP_RW_SYNC
: 0) |
2433 (bi_rw
& REQ_UNPLUG
? DP_UNPLUG
: 0) |
2434 (bi_rw
& REQ_FUA
? DP_FUA
: 0) |
2435 (bi_rw
& REQ_FLUSH
? DP_FLUSH
: 0) |
2436 (bi_rw
& REQ_DISCARD
? DP_DISCARD
: 0);
2438 return bi_rw
& (REQ_SYNC
| REQ_UNPLUG
) ? DP_RW_SYNC
: 0;
2441 /* Used to send write requests
2442 * R_PRIMARY -> Peer (P_DATA)
2444 int drbd_send_dblock(struct drbd_conf
*mdev
, struct drbd_request
*req
)
2448 unsigned int dp_flags
= 0;
2452 if (!drbd_get_data_sock(mdev
))
2455 dgs
= (mdev
->agreed_pro_version
>= 87 && mdev
->integrity_w_tfm
) ?
2456 crypto_hash_digestsize(mdev
->integrity_w_tfm
) : 0;
2458 if (req
->size
<= DRBD_MAX_SIZE_H80_PACKET
) {
2459 p
.head
.h80
.magic
= BE_DRBD_MAGIC
;
2460 p
.head
.h80
.command
= cpu_to_be16(P_DATA
);
2462 cpu_to_be16(sizeof(p
) - sizeof(union p_header
) + dgs
+ req
->size
);
2464 p
.head
.h95
.magic
= BE_DRBD_MAGIC_BIG
;
2465 p
.head
.h95
.command
= cpu_to_be16(P_DATA
);
2467 cpu_to_be32(sizeof(p
) - sizeof(union p_header
) + dgs
+ req
->size
);
2470 p
.sector
= cpu_to_be64(req
->sector
);
2471 p
.block_id
= (unsigned long)req
;
2472 p
.seq_num
= cpu_to_be32(req
->seq_num
=
2473 atomic_add_return(1, &mdev
->packet_seq
));
2475 dp_flags
= bio_flags_to_wire(mdev
, req
->master_bio
->bi_rw
);
2477 if (mdev
->state
.conn
>= C_SYNC_SOURCE
&&
2478 mdev
->state
.conn
<= C_PAUSED_SYNC_T
)
2479 dp_flags
|= DP_MAY_SET_IN_SYNC
;
2481 p
.dp_flags
= cpu_to_be32(dp_flags
);
2482 set_bit(UNPLUG_REMOTE
, &mdev
->flags
);
2484 drbd_send(mdev
, mdev
->data
.socket
, &p
, sizeof(p
), dgs
? MSG_MORE
: 0));
2486 dgb
= mdev
->int_dig_out
;
2487 drbd_csum_bio(mdev
, mdev
->integrity_w_tfm
, req
->master_bio
, dgb
);
2488 ok
= drbd_send(mdev
, mdev
->data
.socket
, dgb
, dgs
, 0);
2491 if (mdev
->net_conf
->wire_protocol
== DRBD_PROT_A
)
2492 ok
= _drbd_send_bio(mdev
, req
->master_bio
);
2494 ok
= _drbd_send_zc_bio(mdev
, req
->master_bio
);
2497 drbd_put_data_sock(mdev
);
2502 /* answer packet, used to send data back for read requests:
2503 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
2504 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
2506 int drbd_send_block(struct drbd_conf
*mdev
, enum drbd_packets cmd
,
2507 struct drbd_epoch_entry
*e
)
2514 dgs
= (mdev
->agreed_pro_version
>= 87 && mdev
->integrity_w_tfm
) ?
2515 crypto_hash_digestsize(mdev
->integrity_w_tfm
) : 0;
2517 if (e
->size
<= DRBD_MAX_SIZE_H80_PACKET
) {
2518 p
.head
.h80
.magic
= BE_DRBD_MAGIC
;
2519 p
.head
.h80
.command
= cpu_to_be16(cmd
);
2521 cpu_to_be16(sizeof(p
) - sizeof(struct p_header80
) + dgs
+ e
->size
);
2523 p
.head
.h95
.magic
= BE_DRBD_MAGIC_BIG
;
2524 p
.head
.h95
.command
= cpu_to_be16(cmd
);
2526 cpu_to_be32(sizeof(p
) - sizeof(struct p_header80
) + dgs
+ e
->size
);
2529 p
.sector
= cpu_to_be64(e
->sector
);
2530 p
.block_id
= e
->block_id
;
2531 /* p.seq_num = 0; No sequence numbers here.. */
2533 /* Only called by our kernel thread.
2534 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
2535 * in response to admin command or module unload.
2537 if (!drbd_get_data_sock(mdev
))
2540 ok
= sizeof(p
) == drbd_send(mdev
, mdev
->data
.socket
, &p
, sizeof(p
), dgs
? MSG_MORE
: 0);
2542 dgb
= mdev
->int_dig_out
;
2543 drbd_csum_ee(mdev
, mdev
->integrity_w_tfm
, e
, dgb
);
2544 ok
= drbd_send(mdev
, mdev
->data
.socket
, dgb
, dgs
, 0);
2547 ok
= _drbd_send_zc_ee(mdev
, e
);
2549 drbd_put_data_sock(mdev
);
2555 drbd_send distinguishes two cases:
2557 Packets sent via the data socket "sock"
2558 and packets sent via the meta data socket "msock"
2561 -----------------+-------------------------+------------------------------
2562 timeout conf.timeout / 2 conf.timeout / 2
2563 timeout action send a ping via msock Abort communication
2564 and close all sockets
2568 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
2570 int drbd_send(struct drbd_conf
*mdev
, struct socket
*sock
,
2571 void *buf
, size_t size
, unsigned msg_flags
)
2580 /* THINK if (signal_pending) return ... ? */
2585 msg
.msg_name
= NULL
;
2586 msg
.msg_namelen
= 0;
2587 msg
.msg_control
= NULL
;
2588 msg
.msg_controllen
= 0;
2589 msg
.msg_flags
= msg_flags
| MSG_NOSIGNAL
;
2591 if (sock
== mdev
->data
.socket
) {
2592 mdev
->ko_count
= mdev
->net_conf
->ko_count
;
2593 drbd_update_congested(mdev
);
2597 * tcp_sendmsg does _not_ use its size parameter at all ?
2599 * -EAGAIN on timeout, -EINTR on signal.
2602 * do we need to block DRBD_SIG if sock == &meta.socket ??
2603 * otherwise wake_asender() might interrupt some send_*Ack !
2605 rv
= kernel_sendmsg(sock
, &msg
, &iov
, 1, size
);
2606 if (rv
== -EAGAIN
) {
2607 if (we_should_drop_the_connection(mdev
, sock
))
2614 flush_signals(current
);
2622 } while (sent
< size
);
2624 if (sock
== mdev
->data
.socket
)
2625 clear_bit(NET_CONGESTED
, &mdev
->flags
);
2628 if (rv
!= -EAGAIN
) {
2629 dev_err(DEV
, "%s_sendmsg returned %d\n",
2630 sock
== mdev
->meta
.socket
? "msock" : "sock",
2632 drbd_force_state(mdev
, NS(conn
, C_BROKEN_PIPE
));
2634 drbd_force_state(mdev
, NS(conn
, C_TIMEOUT
));
2640 static int drbd_open(struct block_device
*bdev
, fmode_t mode
)
2642 struct drbd_conf
*mdev
= bdev
->bd_disk
->private_data
;
2643 unsigned long flags
;
2647 spin_lock_irqsave(&mdev
->req_lock
, flags
);
2648 /* to have a stable mdev->state.role
2649 * and no race with updating open_cnt */
2651 if (mdev
->state
.role
!= R_PRIMARY
) {
2652 if (mode
& FMODE_WRITE
)
2654 else if (!allow_oos
)
2660 spin_unlock_irqrestore(&mdev
->req_lock
, flags
);
2666 static int drbd_release(struct gendisk
*gd
, fmode_t mode
)
2668 struct drbd_conf
*mdev
= gd
->private_data
;
2675 static void drbd_unplug_fn(struct request_queue
*q
)
2677 struct drbd_conf
*mdev
= q
->queuedata
;
2680 spin_lock_irq(q
->queue_lock
);
2682 spin_unlock_irq(q
->queue_lock
);
2684 /* only if connected */
2685 spin_lock_irq(&mdev
->req_lock
);
2686 if (mdev
->state
.pdsk
>= D_INCONSISTENT
&& mdev
->state
.conn
>= C_CONNECTED
) {
2687 D_ASSERT(mdev
->state
.role
== R_PRIMARY
);
2688 if (test_and_clear_bit(UNPLUG_REMOTE
, &mdev
->flags
)) {
2689 /* add to the data.work queue,
2690 * unless already queued.
2691 * XXX this might be a good addition to drbd_queue_work
2692 * anyways, to detect "double queuing" ... */
2693 if (list_empty(&mdev
->unplug_work
.list
))
2694 drbd_queue_work(&mdev
->data
.work
,
2695 &mdev
->unplug_work
);
2698 spin_unlock_irq(&mdev
->req_lock
);
2700 if (mdev
->state
.disk
>= D_INCONSISTENT
)
2704 static void drbd_set_defaults(struct drbd_conf
*mdev
)
2706 /* This way we get a compile error when sync_conf grows,
2707 and we forgot to initialize it here */
2708 mdev
->sync_conf
= (struct syncer_conf
) {
2709 /* .rate = */ DRBD_RATE_DEF
,
2710 /* .after = */ DRBD_AFTER_DEF
,
2711 /* .al_extents = */ DRBD_AL_EXTENTS_DEF
,
2712 /* .verify_alg = */ {}, 0,
2713 /* .cpu_mask = */ {}, 0,
2714 /* .csums_alg = */ {}, 0,
2716 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF
,
2717 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF
,
2718 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF
,
2719 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF
,
2720 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF
,
2721 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
2724 /* Have to use that way, because the layout differs between
2725 big endian and little endian */
2726 mdev
->state
= (union drbd_state
) {
2727 { .role
= R_SECONDARY
,
2729 .conn
= C_STANDALONE
,
2736 void drbd_init_set_defaults(struct drbd_conf
*mdev
)
2738 /* the memset(,0,) did most of this.
2739 * note: only assignments, no allocation in here */
2741 drbd_set_defaults(mdev
);
2743 /* for now, we do NOT yet support it,
2744 * even though we start some framework
2745 * to eventually support barriers */
2746 set_bit(NO_BARRIER_SUPP
, &mdev
->flags
);
2748 atomic_set(&mdev
->ap_bio_cnt
, 0);
2749 atomic_set(&mdev
->ap_pending_cnt
, 0);
2750 atomic_set(&mdev
->rs_pending_cnt
, 0);
2751 atomic_set(&mdev
->unacked_cnt
, 0);
2752 atomic_set(&mdev
->local_cnt
, 0);
2753 atomic_set(&mdev
->net_cnt
, 0);
2754 atomic_set(&mdev
->packet_seq
, 0);
2755 atomic_set(&mdev
->pp_in_use
, 0);
2756 atomic_set(&mdev
->rs_sect_in
, 0);
2757 atomic_set(&mdev
->rs_sect_ev
, 0);
2759 mutex_init(&mdev
->md_io_mutex
);
2760 mutex_init(&mdev
->data
.mutex
);
2761 mutex_init(&mdev
->meta
.mutex
);
2762 sema_init(&mdev
->data
.work
.s
, 0);
2763 sema_init(&mdev
->meta
.work
.s
, 0);
2764 mutex_init(&mdev
->state_mutex
);
2766 spin_lock_init(&mdev
->data
.work
.q_lock
);
2767 spin_lock_init(&mdev
->meta
.work
.q_lock
);
2769 spin_lock_init(&mdev
->al_lock
);
2770 spin_lock_init(&mdev
->req_lock
);
2771 spin_lock_init(&mdev
->peer_seq_lock
);
2772 spin_lock_init(&mdev
->epoch_lock
);
2774 INIT_LIST_HEAD(&mdev
->active_ee
);
2775 INIT_LIST_HEAD(&mdev
->sync_ee
);
2776 INIT_LIST_HEAD(&mdev
->done_ee
);
2777 INIT_LIST_HEAD(&mdev
->read_ee
);
2778 INIT_LIST_HEAD(&mdev
->net_ee
);
2779 INIT_LIST_HEAD(&mdev
->resync_reads
);
2780 INIT_LIST_HEAD(&mdev
->data
.work
.q
);
2781 INIT_LIST_HEAD(&mdev
->meta
.work
.q
);
2782 INIT_LIST_HEAD(&mdev
->resync_work
.list
);
2783 INIT_LIST_HEAD(&mdev
->unplug_work
.list
);
2784 INIT_LIST_HEAD(&mdev
->md_sync_work
.list
);
2785 INIT_LIST_HEAD(&mdev
->bm_io_work
.w
.list
);
2787 mdev
->resync_work
.cb
= w_resync_inactive
;
2788 mdev
->unplug_work
.cb
= w_send_write_hint
;
2789 mdev
->md_sync_work
.cb
= w_md_sync
;
2790 mdev
->bm_io_work
.w
.cb
= w_bitmap_io
;
2791 init_timer(&mdev
->resync_timer
);
2792 init_timer(&mdev
->md_sync_timer
);
2793 mdev
->resync_timer
.function
= resync_timer_fn
;
2794 mdev
->resync_timer
.data
= (unsigned long) mdev
;
2795 mdev
->md_sync_timer
.function
= md_sync_timer_fn
;
2796 mdev
->md_sync_timer
.data
= (unsigned long) mdev
;
2798 init_waitqueue_head(&mdev
->misc_wait
);
2799 init_waitqueue_head(&mdev
->state_wait
);
2800 init_waitqueue_head(&mdev
->net_cnt_wait
);
2801 init_waitqueue_head(&mdev
->ee_wait
);
2802 init_waitqueue_head(&mdev
->al_wait
);
2803 init_waitqueue_head(&mdev
->seq_wait
);
2805 drbd_thread_init(mdev
, &mdev
->receiver
, drbdd_init
);
2806 drbd_thread_init(mdev
, &mdev
->worker
, drbd_worker
);
2807 drbd_thread_init(mdev
, &mdev
->asender
, drbd_asender
);
2809 mdev
->agreed_pro_version
= PRO_VERSION_MAX
;
2810 mdev
->write_ordering
= WO_bio_barrier
;
2811 mdev
->resync_wenr
= LC_FREE
;
2814 void drbd_mdev_cleanup(struct drbd_conf
*mdev
)
2817 if (mdev
->receiver
.t_state
!= None
)
2818 dev_err(DEV
, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
2819 mdev
->receiver
.t_state
);
2821 /* no need to lock it, I'm the only thread alive */
2822 if (atomic_read(&mdev
->current_epoch
->epoch_size
) != 0)
2823 dev_err(DEV
, "epoch_size:%d\n", atomic_read(&mdev
->current_epoch
->epoch_size
));
2833 mdev
->rs_failed
= 0;
2834 mdev
->rs_last_events
= 0;
2835 mdev
->rs_last_sect_ev
= 0;
2836 for (i
= 0; i
< DRBD_SYNC_MARKS
; i
++) {
2837 mdev
->rs_mark_left
[i
] = 0;
2838 mdev
->rs_mark_time
[i
] = 0;
2840 D_ASSERT(mdev
->net_conf
== NULL
);
2842 drbd_set_my_capacity(mdev
, 0);
2844 /* maybe never allocated. */
2845 drbd_bm_resize(mdev
, 0, 1);
2846 drbd_bm_cleanup(mdev
);
2849 drbd_free_resources(mdev
);
2850 clear_bit(AL_SUSPENDED
, &mdev
->flags
);
2853 * currently we drbd_init_ee only on module load, so
2854 * we may do drbd_release_ee only on module unload!
2856 D_ASSERT(list_empty(&mdev
->active_ee
));
2857 D_ASSERT(list_empty(&mdev
->sync_ee
));
2858 D_ASSERT(list_empty(&mdev
->done_ee
));
2859 D_ASSERT(list_empty(&mdev
->read_ee
));
2860 D_ASSERT(list_empty(&mdev
->net_ee
));
2861 D_ASSERT(list_empty(&mdev
->resync_reads
));
2862 D_ASSERT(list_empty(&mdev
->data
.work
.q
));
2863 D_ASSERT(list_empty(&mdev
->meta
.work
.q
));
2864 D_ASSERT(list_empty(&mdev
->resync_work
.list
));
2865 D_ASSERT(list_empty(&mdev
->unplug_work
.list
));
2870 static void drbd_destroy_mempools(void)
2874 while (drbd_pp_pool
) {
2875 page
= drbd_pp_pool
;
2876 drbd_pp_pool
= (struct page
*)page_private(page
);
2881 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2883 if (drbd_ee_mempool
)
2884 mempool_destroy(drbd_ee_mempool
);
2885 if (drbd_request_mempool
)
2886 mempool_destroy(drbd_request_mempool
);
2888 kmem_cache_destroy(drbd_ee_cache
);
2889 if (drbd_request_cache
)
2890 kmem_cache_destroy(drbd_request_cache
);
2891 if (drbd_bm_ext_cache
)
2892 kmem_cache_destroy(drbd_bm_ext_cache
);
2893 if (drbd_al_ext_cache
)
2894 kmem_cache_destroy(drbd_al_ext_cache
);
2896 drbd_ee_mempool
= NULL
;
2897 drbd_request_mempool
= NULL
;
2898 drbd_ee_cache
= NULL
;
2899 drbd_request_cache
= NULL
;
2900 drbd_bm_ext_cache
= NULL
;
2901 drbd_al_ext_cache
= NULL
;
2906 static int drbd_create_mempools(void)
2909 const int number
= (DRBD_MAX_SEGMENT_SIZE
/PAGE_SIZE
) * minor_count
;
2912 /* prepare our caches and mempools */
2913 drbd_request_mempool
= NULL
;
2914 drbd_ee_cache
= NULL
;
2915 drbd_request_cache
= NULL
;
2916 drbd_bm_ext_cache
= NULL
;
2917 drbd_al_ext_cache
= NULL
;
2918 drbd_pp_pool
= NULL
;
2921 drbd_request_cache
= kmem_cache_create(
2922 "drbd_req", sizeof(struct drbd_request
), 0, 0, NULL
);
2923 if (drbd_request_cache
== NULL
)
2926 drbd_ee_cache
= kmem_cache_create(
2927 "drbd_ee", sizeof(struct drbd_epoch_entry
), 0, 0, NULL
);
2928 if (drbd_ee_cache
== NULL
)
2931 drbd_bm_ext_cache
= kmem_cache_create(
2932 "drbd_bm", sizeof(struct bm_extent
), 0, 0, NULL
);
2933 if (drbd_bm_ext_cache
== NULL
)
2936 drbd_al_ext_cache
= kmem_cache_create(
2937 "drbd_al", sizeof(struct lc_element
), 0, 0, NULL
);
2938 if (drbd_al_ext_cache
== NULL
)
2942 drbd_request_mempool
= mempool_create(number
,
2943 mempool_alloc_slab
, mempool_free_slab
, drbd_request_cache
);
2944 if (drbd_request_mempool
== NULL
)
2947 drbd_ee_mempool
= mempool_create(number
,
2948 mempool_alloc_slab
, mempool_free_slab
, drbd_ee_cache
);
2949 if (drbd_request_mempool
== NULL
)
2952 /* drbd's page pool */
2953 spin_lock_init(&drbd_pp_lock
);
2955 for (i
= 0; i
< number
; i
++) {
2956 page
= alloc_page(GFP_HIGHUSER
);
2959 set_page_private(page
, (unsigned long)drbd_pp_pool
);
2960 drbd_pp_pool
= page
;
2962 drbd_pp_vacant
= number
;
2967 drbd_destroy_mempools(); /* in case we allocated some */
2971 static int drbd_notify_sys(struct notifier_block
*this, unsigned long code
,
2974 /* just so we have it. you never know what interesting things we
2975 * might want to do here some day...
2981 static struct notifier_block drbd_notifier
= {
2982 .notifier_call
= drbd_notify_sys
,
2985 static void drbd_release_ee_lists(struct drbd_conf
*mdev
)
2989 rr
= drbd_release_ee(mdev
, &mdev
->active_ee
);
2991 dev_err(DEV
, "%d EEs in active list found!\n", rr
);
2993 rr
= drbd_release_ee(mdev
, &mdev
->sync_ee
);
2995 dev_err(DEV
, "%d EEs in sync list found!\n", rr
);
2997 rr
= drbd_release_ee(mdev
, &mdev
->read_ee
);
2999 dev_err(DEV
, "%d EEs in read list found!\n", rr
);
3001 rr
= drbd_release_ee(mdev
, &mdev
->done_ee
);
3003 dev_err(DEV
, "%d EEs in done list found!\n", rr
);
3005 rr
= drbd_release_ee(mdev
, &mdev
->net_ee
);
3007 dev_err(DEV
, "%d EEs in net list found!\n", rr
);
3010 /* caution. no locking.
3011 * currently only used from module cleanup code. */
3012 static void drbd_delete_device(unsigned int minor
)
3014 struct drbd_conf
*mdev
= minor_to_mdev(minor
);
3019 /* paranoia asserts */
3020 if (mdev
->open_cnt
!= 0)
3021 dev_err(DEV
, "open_cnt = %d in %s:%u", mdev
->open_cnt
,
3022 __FILE__
, __LINE__
);
3024 ERR_IF (!list_empty(&mdev
->data
.work
.q
)) {
3025 struct list_head
*lp
;
3026 list_for_each(lp
, &mdev
->data
.work
.q
) {
3027 dev_err(DEV
, "lp = %p\n", lp
);
3030 /* end paranoia asserts */
3032 del_gendisk(mdev
->vdisk
);
3034 /* cleanup stuff that may have been allocated during
3035 * device (re-)configuration or state changes */
3037 if (mdev
->this_bdev
)
3038 bdput(mdev
->this_bdev
);
3040 drbd_free_resources(mdev
);
3042 drbd_release_ee_lists(mdev
);
3044 /* should be free'd on disconnect? */
3045 kfree(mdev
->ee_hash
);
3047 mdev->ee_hash_s = 0;
3048 mdev->ee_hash = NULL;
3051 lc_destroy(mdev
->act_log
);
3052 lc_destroy(mdev
->resync
);
3054 kfree(mdev
->p_uuid
);
3055 /* mdev->p_uuid = NULL; */
3057 kfree(mdev
->int_dig_out
);
3058 kfree(mdev
->int_dig_in
);
3059 kfree(mdev
->int_dig_vv
);
3061 /* cleanup the rest that has been
3062 * allocated from drbd_new_device
3063 * and actually free the mdev itself */
3064 drbd_free_mdev(mdev
);
3067 static void drbd_cleanup(void)
3071 unregister_reboot_notifier(&drbd_notifier
);
3077 remove_proc_entry("drbd", NULL
);
3080 drbd_delete_device(i
);
3081 drbd_destroy_mempools();
3086 unregister_blkdev(DRBD_MAJOR
, "drbd");
3088 printk(KERN_INFO
"drbd: module cleanup done.\n");
3092 * drbd_congested() - Callback for pdflush
3093 * @congested_data: User data
3094 * @bdi_bits: Bits pdflush is currently interested in
3096 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
3098 static int drbd_congested(void *congested_data
, int bdi_bits
)
3100 struct drbd_conf
*mdev
= congested_data
;
3101 struct request_queue
*q
;
3105 if (!__inc_ap_bio_cond(mdev
)) {
3106 /* DRBD has frozen IO */
3112 if (get_ldev(mdev
)) {
3113 q
= bdev_get_queue(mdev
->ldev
->backing_bdev
);
3114 r
= bdi_congested(&q
->backing_dev_info
, bdi_bits
);
3120 if (bdi_bits
& (1 << BDI_async_congested
) && test_bit(NET_CONGESTED
, &mdev
->flags
)) {
3121 r
|= (1 << BDI_async_congested
);
3122 reason
= reason
== 'b' ? 'a' : 'n';
3126 mdev
->congestion_reason
= reason
;
3130 struct drbd_conf
*drbd_new_device(unsigned int minor
)
3132 struct drbd_conf
*mdev
;
3133 struct gendisk
*disk
;
3134 struct request_queue
*q
;
3136 /* GFP_KERNEL, we are outside of all write-out paths */
3137 mdev
= kzalloc(sizeof(struct drbd_conf
), GFP_KERNEL
);
3140 if (!zalloc_cpumask_var(&mdev
->cpu_mask
, GFP_KERNEL
))
3141 goto out_no_cpumask
;
3143 mdev
->minor
= minor
;
3145 drbd_init_set_defaults(mdev
);
3147 q
= blk_alloc_queue(GFP_KERNEL
);
3151 q
->queuedata
= mdev
;
3153 disk
= alloc_disk(1);
3158 set_disk_ro(disk
, TRUE
);
3161 disk
->major
= DRBD_MAJOR
;
3162 disk
->first_minor
= minor
;
3163 disk
->fops
= &drbd_ops
;
3164 sprintf(disk
->disk_name
, "drbd%d", minor
);
3165 disk
->private_data
= mdev
;
3167 mdev
->this_bdev
= bdget(MKDEV(DRBD_MAJOR
, minor
));
3168 /* we have no partitions. we contain only ourselves. */
3169 mdev
->this_bdev
->bd_contains
= mdev
->this_bdev
;
3171 q
->backing_dev_info
.congested_fn
= drbd_congested
;
3172 q
->backing_dev_info
.congested_data
= mdev
;
3174 blk_queue_make_request(q
, drbd_make_request_26
);
3175 blk_queue_max_segment_size(q
, DRBD_MAX_SEGMENT_SIZE
);
3176 blk_queue_bounce_limit(q
, BLK_BOUNCE_ANY
);
3177 blk_queue_merge_bvec(q
, drbd_merge_bvec
);
3178 q
->queue_lock
= &mdev
->req_lock
; /* needed since we use */
3179 /* plugging on a queue, that actually has no requests! */
3180 q
->unplug_fn
= drbd_unplug_fn
;
3182 mdev
->md_io_page
= alloc_page(GFP_KERNEL
);
3183 if (!mdev
->md_io_page
)
3184 goto out_no_io_page
;
3186 if (drbd_bm_init(mdev
))
3188 /* no need to lock access, we are still initializing this minor device. */
3192 mdev
->app_reads_hash
= kzalloc(APP_R_HSIZE
*sizeof(void *), GFP_KERNEL
);
3193 if (!mdev
->app_reads_hash
)
3194 goto out_no_app_reads
;
3196 mdev
->current_epoch
= kzalloc(sizeof(struct drbd_epoch
), GFP_KERNEL
);
3197 if (!mdev
->current_epoch
)
3200 INIT_LIST_HEAD(&mdev
->current_epoch
->list
);
3205 /* out_whatever_else:
3206 kfree(mdev->current_epoch); */
3208 kfree(mdev
->app_reads_hash
);
3212 drbd_bm_cleanup(mdev
);
3214 __free_page(mdev
->md_io_page
);
3218 blk_cleanup_queue(q
);
3220 free_cpumask_var(mdev
->cpu_mask
);
3226 /* counterpart of drbd_new_device.
3227 * last part of drbd_delete_device. */
3228 void drbd_free_mdev(struct drbd_conf
*mdev
)
3230 kfree(mdev
->current_epoch
);
3231 kfree(mdev
->app_reads_hash
);
3233 if (mdev
->bitmap
) /* should no longer be there. */
3234 drbd_bm_cleanup(mdev
);
3235 __free_page(mdev
->md_io_page
);
3236 put_disk(mdev
->vdisk
);
3237 blk_cleanup_queue(mdev
->rq_queue
);
3238 free_cpumask_var(mdev
->cpu_mask
);
3243 int __init
drbd_init(void)
3247 if (sizeof(struct p_handshake
) != 80) {
3249 "drbd: never change the size or layout "
3250 "of the HandShake packet.\n");
3254 if (1 > minor_count
|| minor_count
> 255) {
3256 "drbd: invalid minor_count (%d)\n", minor_count
);
3264 err
= drbd_nl_init();
3268 err
= register_blkdev(DRBD_MAJOR
, "drbd");
3271 "drbd: unable to register block device major %d\n",
3276 register_reboot_notifier(&drbd_notifier
);
3279 * allocate all necessary structs
3283 init_waitqueue_head(&drbd_pp_wait
);
3285 drbd_proc
= NULL
; /* play safe for drbd_cleanup */
3286 minor_table
= kzalloc(sizeof(struct drbd_conf
*)*minor_count
,
3291 err
= drbd_create_mempools();
3295 drbd_proc
= proc_create_data("drbd", S_IFREG
| S_IRUGO
, NULL
, &drbd_proc_fops
, NULL
);
3297 printk(KERN_ERR
"drbd: unable to register proc file\n");
3301 rwlock_init(&global_state_lock
);
3303 printk(KERN_INFO
"drbd: initialized. "
3304 "Version: " REL_VERSION
" (api:%d/proto:%d-%d)\n",
3305 API_VERSION
, PRO_VERSION_MIN
, PRO_VERSION_MAX
);
3306 printk(KERN_INFO
"drbd: %s\n", drbd_buildtag());
3307 printk(KERN_INFO
"drbd: registered as block device major %d\n",
3309 printk(KERN_INFO
"drbd: minor_table @ 0x%p\n", minor_table
);
3311 return 0; /* Success! */
3316 /* currently always the case */
3317 printk(KERN_ERR
"drbd: ran out of memory\n");
3319 printk(KERN_ERR
"drbd: initialization failure\n");
3323 void drbd_free_bc(struct drbd_backing_dev
*ldev
)
3328 bd_release(ldev
->backing_bdev
);
3329 bd_release(ldev
->md_bdev
);
3331 fput(ldev
->lo_file
);
3332 fput(ldev
->md_file
);
3337 void drbd_free_sock(struct drbd_conf
*mdev
)
3339 if (mdev
->data
.socket
) {
3340 mutex_lock(&mdev
->data
.mutex
);
3341 kernel_sock_shutdown(mdev
->data
.socket
, SHUT_RDWR
);
3342 sock_release(mdev
->data
.socket
);
3343 mdev
->data
.socket
= NULL
;
3344 mutex_unlock(&mdev
->data
.mutex
);
3346 if (mdev
->meta
.socket
) {
3347 mutex_lock(&mdev
->meta
.mutex
);
3348 kernel_sock_shutdown(mdev
->meta
.socket
, SHUT_RDWR
);
3349 sock_release(mdev
->meta
.socket
);
3350 mdev
->meta
.socket
= NULL
;
3351 mutex_unlock(&mdev
->meta
.mutex
);
3356 void drbd_free_resources(struct drbd_conf
*mdev
)
3358 crypto_free_hash(mdev
->csums_tfm
);
3359 mdev
->csums_tfm
= NULL
;
3360 crypto_free_hash(mdev
->verify_tfm
);
3361 mdev
->verify_tfm
= NULL
;
3362 crypto_free_hash(mdev
->cram_hmac_tfm
);
3363 mdev
->cram_hmac_tfm
= NULL
;
3364 crypto_free_hash(mdev
->integrity_w_tfm
);
3365 mdev
->integrity_w_tfm
= NULL
;
3366 crypto_free_hash(mdev
->integrity_r_tfm
);
3367 mdev
->integrity_r_tfm
= NULL
;
3369 drbd_free_sock(mdev
);
3372 drbd_free_bc(mdev
->ldev
);
3373 mdev
->ldev
= NULL
;);
3376 /* meta data management */
3378 struct meta_data_on_disk
{
3379 u64 la_size
; /* last agreed size. */
3380 u64 uuid
[UI_SIZE
]; /* UUIDs. */
3383 u32 flags
; /* MDF */
3386 u32 al_offset
; /* offset to this block */
3387 u32 al_nr_extents
; /* important for restoring the AL */
3388 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
3389 u32 bm_offset
; /* offset to the bitmap, from here */
3390 u32 bm_bytes_per_bit
; /* BM_BLOCK_SIZE */
3391 u32 reserved_u32
[4];
3396 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
3397 * @mdev: DRBD device.
3399 void drbd_md_sync(struct drbd_conf
*mdev
)
3401 struct meta_data_on_disk
*buffer
;
3405 del_timer(&mdev
->md_sync_timer
);
3406 /* timer may be rearmed by drbd_md_mark_dirty() now. */
3407 if (!test_and_clear_bit(MD_DIRTY
, &mdev
->flags
))
3410 /* We use here D_FAILED and not D_ATTACHING because we try to write
3411 * metadata even if we detach due to a disk failure! */
3412 if (!get_ldev_if_state(mdev
, D_FAILED
))
3415 mutex_lock(&mdev
->md_io_mutex
);
3416 buffer
= (struct meta_data_on_disk
*)page_address(mdev
->md_io_page
);
3417 memset(buffer
, 0, 512);
3419 buffer
->la_size
= cpu_to_be64(drbd_get_capacity(mdev
->this_bdev
));
3420 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
3421 buffer
->uuid
[i
] = cpu_to_be64(mdev
->ldev
->md
.uuid
[i
]);
3422 buffer
->flags
= cpu_to_be32(mdev
->ldev
->md
.flags
);
3423 buffer
->magic
= cpu_to_be32(DRBD_MD_MAGIC
);
3425 buffer
->md_size_sect
= cpu_to_be32(mdev
->ldev
->md
.md_size_sect
);
3426 buffer
->al_offset
= cpu_to_be32(mdev
->ldev
->md
.al_offset
);
3427 buffer
->al_nr_extents
= cpu_to_be32(mdev
->act_log
->nr_elements
);
3428 buffer
->bm_bytes_per_bit
= cpu_to_be32(BM_BLOCK_SIZE
);
3429 buffer
->device_uuid
= cpu_to_be64(mdev
->ldev
->md
.device_uuid
);
3431 buffer
->bm_offset
= cpu_to_be32(mdev
->ldev
->md
.bm_offset
);
3433 D_ASSERT(drbd_md_ss__(mdev
, mdev
->ldev
) == mdev
->ldev
->md
.md_offset
);
3434 sector
= mdev
->ldev
->md
.md_offset
;
3436 if (!drbd_md_sync_page_io(mdev
, mdev
->ldev
, sector
, WRITE
)) {
3437 /* this was a try anyways ... */
3438 dev_err(DEV
, "meta data update failed!\n");
3439 drbd_chk_io_error(mdev
, 1, TRUE
);
3442 /* Update mdev->ldev->md.la_size_sect,
3443 * since we updated it on metadata. */
3444 mdev
->ldev
->md
.la_size_sect
= drbd_get_capacity(mdev
->this_bdev
);
3446 mutex_unlock(&mdev
->md_io_mutex
);
3451 * drbd_md_read() - Reads in the meta data super block
3452 * @mdev: DRBD device.
3453 * @bdev: Device from which the meta data should be read in.
3455 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_codes in case
3456 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
3458 int drbd_md_read(struct drbd_conf
*mdev
, struct drbd_backing_dev
*bdev
)
3460 struct meta_data_on_disk
*buffer
;
3461 int i
, rv
= NO_ERROR
;
3463 if (!get_ldev_if_state(mdev
, D_ATTACHING
))
3464 return ERR_IO_MD_DISK
;
3466 mutex_lock(&mdev
->md_io_mutex
);
3467 buffer
= (struct meta_data_on_disk
*)page_address(mdev
->md_io_page
);
3469 if (!drbd_md_sync_page_io(mdev
, bdev
, bdev
->md
.md_offset
, READ
)) {
3470 /* NOTE: cant do normal error processing here as this is
3471 called BEFORE disk is attached */
3472 dev_err(DEV
, "Error while reading metadata.\n");
3473 rv
= ERR_IO_MD_DISK
;
3477 if (be32_to_cpu(buffer
->magic
) != DRBD_MD_MAGIC
) {
3478 dev_err(DEV
, "Error while reading metadata, magic not found.\n");
3479 rv
= ERR_MD_INVALID
;
3482 if (be32_to_cpu(buffer
->al_offset
) != bdev
->md
.al_offset
) {
3483 dev_err(DEV
, "unexpected al_offset: %d (expected %d)\n",
3484 be32_to_cpu(buffer
->al_offset
), bdev
->md
.al_offset
);
3485 rv
= ERR_MD_INVALID
;
3488 if (be32_to_cpu(buffer
->bm_offset
) != bdev
->md
.bm_offset
) {
3489 dev_err(DEV
, "unexpected bm_offset: %d (expected %d)\n",
3490 be32_to_cpu(buffer
->bm_offset
), bdev
->md
.bm_offset
);
3491 rv
= ERR_MD_INVALID
;
3494 if (be32_to_cpu(buffer
->md_size_sect
) != bdev
->md
.md_size_sect
) {
3495 dev_err(DEV
, "unexpected md_size: %u (expected %u)\n",
3496 be32_to_cpu(buffer
->md_size_sect
), bdev
->md
.md_size_sect
);
3497 rv
= ERR_MD_INVALID
;
3501 if (be32_to_cpu(buffer
->bm_bytes_per_bit
) != BM_BLOCK_SIZE
) {
3502 dev_err(DEV
, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3503 be32_to_cpu(buffer
->bm_bytes_per_bit
), BM_BLOCK_SIZE
);
3504 rv
= ERR_MD_INVALID
;
3508 bdev
->md
.la_size_sect
= be64_to_cpu(buffer
->la_size
);
3509 for (i
= UI_CURRENT
; i
< UI_SIZE
; i
++)
3510 bdev
->md
.uuid
[i
] = be64_to_cpu(buffer
->uuid
[i
]);
3511 bdev
->md
.flags
= be32_to_cpu(buffer
->flags
);
3512 mdev
->sync_conf
.al_extents
= be32_to_cpu(buffer
->al_nr_extents
);
3513 bdev
->md
.device_uuid
= be64_to_cpu(buffer
->device_uuid
);
3515 if (mdev
->sync_conf
.al_extents
< 7)
3516 mdev
->sync_conf
.al_extents
= 127;
3519 mutex_unlock(&mdev
->md_io_mutex
);
3526 * drbd_md_mark_dirty() - Mark meta data super block as dirty
3527 * @mdev: DRBD device.
3529 * Call this function if you change anything that should be written to
3530 * the meta-data super block. This function sets MD_DIRTY, and starts a
3531 * timer that ensures that within five seconds you have to call drbd_md_sync().
3533 #ifdef DRBD_DEBUG_MD_SYNC
3534 void drbd_md_mark_dirty_(struct drbd_conf
*mdev
, unsigned int line
, const char *func
)
3536 if (!test_and_set_bit(MD_DIRTY
, &mdev
->flags
)) {
3537 mod_timer(&mdev
->md_sync_timer
, jiffies
+ HZ
);
3538 mdev
->last_md_mark_dirty
.line
= line
;
3539 mdev
->last_md_mark_dirty
.func
= func
;
3543 void drbd_md_mark_dirty(struct drbd_conf
*mdev
)
3545 if (!test_and_set_bit(MD_DIRTY
, &mdev
->flags
))
3546 mod_timer(&mdev
->md_sync_timer
, jiffies
+ HZ
);
3550 static void drbd_uuid_move_history(struct drbd_conf
*mdev
) __must_hold(local
)
3554 for (i
= UI_HISTORY_START
; i
< UI_HISTORY_END
; i
++)
3555 mdev
->ldev
->md
.uuid
[i
+1] = mdev
->ldev
->md
.uuid
[i
];
3558 void _drbd_uuid_set(struct drbd_conf
*mdev
, int idx
, u64 val
) __must_hold(local
)
3560 if (idx
== UI_CURRENT
) {
3561 if (mdev
->state
.role
== R_PRIMARY
)
3566 drbd_set_ed_uuid(mdev
, val
);
3569 mdev
->ldev
->md
.uuid
[idx
] = val
;
3570 drbd_md_mark_dirty(mdev
);
3574 void drbd_uuid_set(struct drbd_conf
*mdev
, int idx
, u64 val
) __must_hold(local
)
3576 if (mdev
->ldev
->md
.uuid
[idx
]) {
3577 drbd_uuid_move_history(mdev
);
3578 mdev
->ldev
->md
.uuid
[UI_HISTORY_START
] = mdev
->ldev
->md
.uuid
[idx
];
3580 _drbd_uuid_set(mdev
, idx
, val
);
3584 * drbd_uuid_new_current() - Creates a new current UUID
3585 * @mdev: DRBD device.
3587 * Creates a new current UUID, and rotates the old current UUID into
3588 * the bitmap slot. Causes an incremental resync upon next connect.
3590 void drbd_uuid_new_current(struct drbd_conf
*mdev
) __must_hold(local
)
3594 dev_info(DEV
, "Creating new current UUID\n");
3595 D_ASSERT(mdev
->ldev
->md
.uuid
[UI_BITMAP
] == 0);
3596 mdev
->ldev
->md
.uuid
[UI_BITMAP
] = mdev
->ldev
->md
.uuid
[UI_CURRENT
];
3598 get_random_bytes(&val
, sizeof(u64
));
3599 _drbd_uuid_set(mdev
, UI_CURRENT
, val
);
3602 void drbd_uuid_set_bm(struct drbd_conf
*mdev
, u64 val
) __must_hold(local
)
3604 if (mdev
->ldev
->md
.uuid
[UI_BITMAP
] == 0 && val
== 0)
3608 drbd_uuid_move_history(mdev
);
3609 mdev
->ldev
->md
.uuid
[UI_HISTORY_START
] = mdev
->ldev
->md
.uuid
[UI_BITMAP
];
3610 mdev
->ldev
->md
.uuid
[UI_BITMAP
] = 0;
3612 if (mdev
->ldev
->md
.uuid
[UI_BITMAP
])
3613 dev_warn(DEV
, "bm UUID already set");
3615 mdev
->ldev
->md
.uuid
[UI_BITMAP
] = val
;
3616 mdev
->ldev
->md
.uuid
[UI_BITMAP
] &= ~((u64
)1);
3619 drbd_md_mark_dirty(mdev
);
3623 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3624 * @mdev: DRBD device.
3626 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3628 int drbd_bmio_set_n_write(struct drbd_conf
*mdev
)
3632 if (get_ldev_if_state(mdev
, D_ATTACHING
)) {
3633 drbd_md_set_flag(mdev
, MDF_FULL_SYNC
);
3635 drbd_bm_set_all(mdev
);
3637 rv
= drbd_bm_write(mdev
);
3640 drbd_md_clear_flag(mdev
, MDF_FULL_SYNC
);
3651 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3652 * @mdev: DRBD device.
3654 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3656 int drbd_bmio_clear_n_write(struct drbd_conf
*mdev
)
3660 drbd_resume_al(mdev
);
3661 if (get_ldev_if_state(mdev
, D_ATTACHING
)) {
3662 drbd_bm_clear_all(mdev
);
3663 rv
= drbd_bm_write(mdev
);
3670 static int w_bitmap_io(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
)
3672 struct bm_io_work
*work
= container_of(w
, struct bm_io_work
, w
);
3675 D_ASSERT(atomic_read(&mdev
->ap_bio_cnt
) == 0);
3677 drbd_bm_lock(mdev
, work
->why
);
3678 rv
= work
->io_fn(mdev
);
3679 drbd_bm_unlock(mdev
);
3681 clear_bit(BITMAP_IO
, &mdev
->flags
);
3682 wake_up(&mdev
->misc_wait
);
3685 work
->done(mdev
, rv
);
3687 clear_bit(BITMAP_IO_QUEUED
, &mdev
->flags
);
3694 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
3695 * @mdev: DRBD device.
3696 * @io_fn: IO callback to be called when bitmap IO is possible
3697 * @done: callback to be called after the bitmap IO was performed
3698 * @why: Descriptive text of the reason for doing the IO
3700 * While IO on the bitmap happens we freeze application IO thus we ensure
3701 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3702 * called from worker context. It MUST NOT be used while a previous such
3703 * work is still pending!
3705 void drbd_queue_bitmap_io(struct drbd_conf
*mdev
,
3706 int (*io_fn
)(struct drbd_conf
*),
3707 void (*done
)(struct drbd_conf
*, int),
3710 D_ASSERT(current
== mdev
->worker
.task
);
3712 D_ASSERT(!test_bit(BITMAP_IO_QUEUED
, &mdev
->flags
));
3713 D_ASSERT(!test_bit(BITMAP_IO
, &mdev
->flags
));
3714 D_ASSERT(list_empty(&mdev
->bm_io_work
.w
.list
));
3715 if (mdev
->bm_io_work
.why
)
3716 dev_err(DEV
, "FIXME going to queue '%s' but '%s' still pending?\n",
3717 why
, mdev
->bm_io_work
.why
);
3719 mdev
->bm_io_work
.io_fn
= io_fn
;
3720 mdev
->bm_io_work
.done
= done
;
3721 mdev
->bm_io_work
.why
= why
;
3723 set_bit(BITMAP_IO
, &mdev
->flags
);
3724 if (atomic_read(&mdev
->ap_bio_cnt
) == 0) {
3725 if (list_empty(&mdev
->bm_io_work
.w
.list
)) {
3726 set_bit(BITMAP_IO_QUEUED
, &mdev
->flags
);
3727 drbd_queue_work(&mdev
->data
.work
, &mdev
->bm_io_work
.w
);
3729 dev_err(DEV
, "FIXME avoided double queuing bm_io_work\n");
3734 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
3735 * @mdev: DRBD device.
3736 * @io_fn: IO callback to be called when bitmap IO is possible
3737 * @why: Descriptive text of the reason for doing the IO
3739 * freezes application IO while that the actual IO operations runs. This
3740 * functions MAY NOT be called from worker context.
3742 int drbd_bitmap_io(struct drbd_conf
*mdev
, int (*io_fn
)(struct drbd_conf
*), char *why
)
3746 D_ASSERT(current
!= mdev
->worker
.task
);
3748 drbd_suspend_io(mdev
);
3750 drbd_bm_lock(mdev
, why
);
3752 drbd_bm_unlock(mdev
);
3754 drbd_resume_io(mdev
);
3759 void drbd_md_set_flag(struct drbd_conf
*mdev
, int flag
) __must_hold(local
)
3761 if ((mdev
->ldev
->md
.flags
& flag
) != flag
) {
3762 drbd_md_mark_dirty(mdev
);
3763 mdev
->ldev
->md
.flags
|= flag
;
3767 void drbd_md_clear_flag(struct drbd_conf
*mdev
, int flag
) __must_hold(local
)
3769 if ((mdev
->ldev
->md
.flags
& flag
) != 0) {
3770 drbd_md_mark_dirty(mdev
);
3771 mdev
->ldev
->md
.flags
&= ~flag
;
3774 int drbd_md_test_flag(struct drbd_backing_dev
*bdev
, int flag
)
3776 return (bdev
->md
.flags
& flag
) != 0;
3779 static void md_sync_timer_fn(unsigned long data
)
3781 struct drbd_conf
*mdev
= (struct drbd_conf
*) data
;
3783 drbd_queue_work_front(&mdev
->data
.work
, &mdev
->md_sync_work
);
3786 static int w_md_sync(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
)
3788 dev_warn(DEV
, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
3790 dev_warn(DEV
, "last md_mark_dirty: %s:%u\n",
3791 mdev
->last_md_mark_dirty
.func
, mdev
->last_md_mark_dirty
.line
);
3797 #ifdef CONFIG_DRBD_FAULT_INJECTION
3798 /* Fault insertion support including random number generator shamelessly
3799 * stolen from kernel/rcutorture.c */
3800 struct fault_random_state
{
3801 unsigned long state
;
3802 unsigned long count
;
3805 #define FAULT_RANDOM_MULT 39916801 /* prime */
3806 #define FAULT_RANDOM_ADD 479001701 /* prime */
3807 #define FAULT_RANDOM_REFRESH 10000
3810 * Crude but fast random-number generator. Uses a linear congruential
3811 * generator, with occasional help from get_random_bytes().
3813 static unsigned long
3814 _drbd_fault_random(struct fault_random_state
*rsp
)
3818 if (!rsp
->count
--) {
3819 get_random_bytes(&refresh
, sizeof(refresh
));
3820 rsp
->state
+= refresh
;
3821 rsp
->count
= FAULT_RANDOM_REFRESH
;
3823 rsp
->state
= rsp
->state
* FAULT_RANDOM_MULT
+ FAULT_RANDOM_ADD
;
3824 return swahw32(rsp
->state
);
3828 _drbd_fault_str(unsigned int type
) {
3829 static char *_faults
[] = {
3830 [DRBD_FAULT_MD_WR
] = "Meta-data write",
3831 [DRBD_FAULT_MD_RD
] = "Meta-data read",
3832 [DRBD_FAULT_RS_WR
] = "Resync write",
3833 [DRBD_FAULT_RS_RD
] = "Resync read",
3834 [DRBD_FAULT_DT_WR
] = "Data write",
3835 [DRBD_FAULT_DT_RD
] = "Data read",
3836 [DRBD_FAULT_DT_RA
] = "Data read ahead",
3837 [DRBD_FAULT_BM_ALLOC
] = "BM allocation",
3838 [DRBD_FAULT_AL_EE
] = "EE allocation",
3839 [DRBD_FAULT_RECEIVE
] = "receive data corruption",
3842 return (type
< DRBD_FAULT_MAX
) ? _faults
[type
] : "**Unknown**";
3846 _drbd_insert_fault(struct drbd_conf
*mdev
, unsigned int type
)
3848 static struct fault_random_state rrs
= {0, 0};
3850 unsigned int ret
= (
3852 ((1 << mdev_to_minor(mdev
)) & fault_devs
) != 0) &&
3853 (((_drbd_fault_random(&rrs
) % 100) + 1) <= fault_rate
));
3858 if (__ratelimit(&drbd_ratelimit_state
))
3859 dev_warn(DEV
, "***Simulating %s failure\n",
3860 _drbd_fault_str(type
));
3867 const char *drbd_buildtag(void)
3869 /* DRBD built from external sources has here a reference to the
3870 git hash of the source code. */
3872 static char buildtag
[38] = "\0uilt-in";
3874 if (buildtag
[0] == 0) {
3875 #ifdef CONFIG_MODULES
3876 if (THIS_MODULE
!= NULL
)
3877 sprintf(buildtag
, "srcversion: %-24s", THIS_MODULE
->srcversion
);
3886 module_init(drbd_init
)
3887 module_exit(drbd_cleanup
)
3889 EXPORT_SYMBOL(drbd_conn_str
);
3890 EXPORT_SYMBOL(drbd_role_str
);
3891 EXPORT_SYMBOL(drbd_disk_str
);
3892 EXPORT_SYMBOL(drbd_set_st_err_str
);