blob: 04c305d36f8e74632c3971c95764dd3c9edf3315 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
11 from Logicworks, Inc. for making SDP replication support possible.
12
13 drbd is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 drbd is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with drbd; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include <linux/module.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070030#include <linux/drbd.h>
31#include <asm/uaccess.h>
32#include <asm/types.h>
33#include <net/sock.h>
34#include <linux/ctype.h>
35#include <linux/smp_lock.h>
36#include <linux/fs.h>
37#include <linux/file.h>
38#include <linux/proc_fs.h>
39#include <linux/init.h>
40#include <linux/mm.h>
41#include <linux/memcontrol.h>
42#include <linux/mm_inline.h>
43#include <linux/slab.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
46#include <linux/notifier.h>
47#include <linux/kthread.h>
48
49#define __KERNEL_SYSCALLS__
50#include <linux/unistd.h>
51#include <linux/vmalloc.h>
52
53#include <linux/drbd_limits.h>
54#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070055#include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
56
57#include "drbd_vli.h"
58
59struct after_state_chg_work {
60 struct drbd_work w;
61 union drbd_state os;
62 union drbd_state ns;
63 enum chg_state_flags flags;
64 struct completion *done;
65};
66
67int drbdd_init(struct drbd_thread *);
68int drbd_worker(struct drbd_thread *);
69int drbd_asender(struct drbd_thread *);
70
71int drbd_init(void);
72static int drbd_open(struct block_device *bdev, fmode_t mode);
73static int drbd_release(struct gendisk *gd, fmode_t mode);
74static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused);
75static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
76 union drbd_state ns, enum chg_state_flags flags);
77static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
78static void md_sync_timer_fn(unsigned long data);
79static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
80
Philipp Reisnerb411b362009-09-25 16:07:19 -070081MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
82 "Lars Ellenberg <lars@linbit.com>");
83MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
84MODULE_VERSION(REL_VERSION);
85MODULE_LICENSE("GPL");
86MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices (1-255)");
87MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
88
89#include <linux/moduleparam.h>
90/* allow_open_on_secondary */
91MODULE_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 */
94module_param(minor_count, uint, 0444);
95module_param(disable_sendpage, bool, 0644);
96module_param(allow_oos, bool, 0);
97module_param(cn_idx, uint, 0444);
98module_param(proc_details, int, 0644);
99
100#ifdef CONFIG_DRBD_FAULT_INJECTION
101int enable_faults;
102int fault_rate;
103static int fault_count;
104int fault_devs;
105/* bitmap of enabled faults */
106module_param(enable_faults, int, 0664);
107/* fault rate % value - applies to all enabled faults */
108module_param(fault_rate, int, 0664);
109/* count of faults inserted */
110module_param(fault_count, int, 0664);
111/* bitmap of devices to insert faults on */
112module_param(fault_devs, int, 0644);
113#endif
114
115/* module parameter, defined */
116unsigned int minor_count = 32;
117int disable_sendpage;
118int allow_oos;
119unsigned int cn_idx = CN_IDX_DRBD;
120int proc_details; /* Detail level in proc drbd*/
121
122/* Module parameter for setting the user mode helper program
123 * to run. Default is /sbin/drbdadm */
124char usermode_helper[80] = "/sbin/drbdadm";
125
126module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
127
128/* in 2.6.x, our device mapping and config info contains our virtual gendisks
129 * as member "struct gendisk *vdisk;"
130 */
131struct drbd_conf **minor_table;
132
133struct kmem_cache *drbd_request_cache;
134struct kmem_cache *drbd_ee_cache; /* epoch entries */
135struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
136struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
137mempool_t *drbd_request_mempool;
138mempool_t *drbd_ee_mempool;
139
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.
145 */
146struct page *drbd_pp_pool;
147spinlock_t drbd_pp_lock;
148int drbd_pp_vacant;
149wait_queue_head_t drbd_pp_wait;
150
151DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
152
Emese Revfy7d4e9d02009-12-14 00:59:30 +0100153static const struct block_device_operations drbd_ops = {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154 .owner = THIS_MODULE,
155 .open = drbd_open,
156 .release = drbd_release,
157};
158
159#define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
160
161#ifdef __CHECKER__
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.
164 */
165int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
166{
167 int io_allowed;
168
169 atomic_inc(&mdev->local_cnt);
170 io_allowed = (mdev->state.disk >= mins);
171 if (!io_allowed) {
172 if (atomic_dec_and_test(&mdev->local_cnt))
173 wake_up(&mdev->misc_wait);
174 }
175 return io_allowed;
176}
177
178#endif
179
180/**
181 * DOC: The transfer log
182 *
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.
186 *
187 * Each &struct drbd_tl_epoch has a circular double linked list of requests
188 * attached.
189 */
190static int tl_init(struct drbd_conf *mdev)
191{
192 struct drbd_tl_epoch *b;
193
194 /* during device minor initialization, we may well use GFP_KERNEL */
195 b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
196 if (!b)
197 return 0;
198 INIT_LIST_HEAD(&b->requests);
199 INIT_LIST_HEAD(&b->w.list);
200 b->next = NULL;
201 b->br_number = 4711;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200202 b->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
204
205 mdev->oldest_tle = b;
206 mdev->newest_tle = b;
207 INIT_LIST_HEAD(&mdev->out_of_sequence_requests);
208
209 mdev->tl_hash = NULL;
210 mdev->tl_hash_s = 0;
211
212 return 1;
213}
214
215static void tl_cleanup(struct drbd_conf *mdev)
216{
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;
225 mdev->tl_hash_s = 0;
226}
227
228/**
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.
232 *
233 * The caller must hold the req_lock.
234 */
235void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
236{
237 struct drbd_tl_epoch *newest_before;
238
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 */
242 new->next = NULL;
Philipp Reisner7e602c02010-05-27 14:49:27 +0200243 new->n_writes = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700244
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;
252 }
253}
254
255/**
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.
260 *
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
263 * of the connection.
264 */
265void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
266 unsigned int set_size)
267{
268 struct drbd_tl_epoch *b, *nob; /* next old barrier */
269 struct list_head *le, *tle;
270 struct drbd_request *r;
271
272 spin_lock_irq(&mdev->req_lock);
273
274 b = mdev->oldest_tle;
275
276 /* first some paranoia code */
277 if (b == NULL) {
278 dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
279 barrier_nr);
280 goto bail;
281 }
282 if (b->br_number != barrier_nr) {
283 dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
284 barrier_nr, b->br_number);
285 goto bail;
286 }
Philipp Reisner7e602c02010-05-27 14:49:27 +0200287 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 goto bail;
291 }
292
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);
297 }
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.
301
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).
307
308 These have been list_move'd to the out_of_sequence_requests list in
309 _req_mod(, barrier_acked) above.
310 */
311 list_del_init(&b->requests);
312
313 nob = b->next;
314 if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
315 _tl_add_barrier(mdev, b);
316 if (nob)
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 */
320 } else {
321 D_ASSERT(nob != NULL);
322 mdev->oldest_tle = nob;
323 kfree(b);
324 }
325
326 spin_unlock_irq(&mdev->req_lock);
327 dec_ap_pending(mdev);
328
329 return;
330
331bail:
332 spin_unlock_irq(&mdev->req_lock);
333 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
334}
335
Philipp Reisner11b58e72010-05-12 17:08:26 +0200336/**
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
340 *
341 * @what might be one of connection_lost_while_pending, resend, fail_frozen_disk_io,
342 * restart_frozen_disk_io.
343 */
344static void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
345{
346 struct drbd_tl_epoch *b, *tmp, **pn;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200347 struct list_head *le, *tle, carry_reads;
Philipp Reisner11b58e72010-05-12 17:08:26 +0200348 struct drbd_request *req;
349 int rv, n_writes, n_reads;
350
351 b = mdev->oldest_tle;
352 pn = &mdev->oldest_tle;
353 while (b) {
354 n_writes = 0;
355 n_reads = 0;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200356 INIT_LIST_HEAD(&carry_reads);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200357 list_for_each_safe(le, tle, &b->requests) {
358 req = list_entry(le, struct drbd_request, tl_requests);
359 rv = _req_mod(req, what);
360
361 n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
362 n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
363 }
364 tmp = b->next;
365
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200366 if (n_writes) {
Philipp Reisner11b58e72010-05-12 17:08:26 +0200367 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);
373 }
374
375 drbd_queue_work(&mdev->data.work, &b->w);
376 }
377 pn = &b->next;
378 } else {
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200379 if (n_reads)
380 list_add(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200381 /* there could still be requests on that ring list,
382 * in case local io is still pending */
383 list_del(&b->requests);
384
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. */
388 if (b->w.cb != NULL)
389 dec_ap_pending(mdev);
390
391 if (b == mdev->newest_tle) {
392 /* recycle, but reinit! */
393 D_ASSERT(tmp == NULL);
394 INIT_LIST_HEAD(&b->requests);
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200395 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200396 INIT_LIST_HEAD(&b->w.list);
397 b->w.cb = NULL;
398 b->br_number = net_random();
399 b->n_writes = 0;
400
401 *pn = b;
402 break;
403 }
404 *pn = tmp;
405 kfree(b);
406 }
407 b = tmp;
Philipp Reisnerb9b98712010-06-22 11:26:48 +0200408 list_splice(&carry_reads, &b->requests);
Philipp Reisner11b58e72010-05-12 17:08:26 +0200409 }
410}
411
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412
413/**
414 * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
415 * @mdev: DRBD device.
416 *
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.
420 */
421void tl_clear(struct drbd_conf *mdev)
422{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423 struct list_head *le, *tle;
424 struct drbd_request *r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700425
426 spin_lock_irq(&mdev->req_lock);
427
Philipp Reisner11b58e72010-05-12 17:08:26 +0200428 _tl_restart(mdev, connection_lost_while_pending);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429
430 /* we expect this list to be empty. */
431 D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
432
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);
439 }
440
441 /* ensure bit indicating barrier is required is clear */
442 clear_bit(CREATE_BARRIER, &mdev->flags);
443
Philipp Reisner288f4222010-05-27 15:07:43 +0200444 memset(mdev->app_reads_hash, 0, APP_R_HSIZE*sizeof(void *));
445
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 spin_unlock_irq(&mdev->req_lock);
447}
448
Philipp Reisner11b58e72010-05-12 17:08:26 +0200449void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
450{
451 spin_lock_irq(&mdev->req_lock);
452 _tl_restart(mdev, what);
453 spin_unlock_irq(&mdev->req_lock);
454}
455
Philipp Reisnerb411b362009-09-25 16:07:19 -0700456/**
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.
461 */
462static int cl_wide_st_chg(struct drbd_conf *mdev,
463 union drbd_state os, union drbd_state ns)
464{
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);
472}
473
474int drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
475 union drbd_state mask, union drbd_state val)
476{
477 unsigned long flags;
478 union drbd_state os, ns;
479 int rv;
480
481 spin_lock_irqsave(&mdev->req_lock, flags);
482 os = mdev->state;
483 ns.i = (os.i & ~mask.i) | val.i;
484 rv = _drbd_set_state(mdev, ns, f, NULL);
485 ns = mdev->state;
486 spin_unlock_irqrestore(&mdev->req_lock, flags);
487
488 return rv;
489}
490
491/**
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.
496 */
497void drbd_force_state(struct drbd_conf *mdev,
498 union drbd_state mask, union drbd_state val)
499{
500 drbd_change_state(mdev, CS_HARD, mask, val);
501}
502
503static int is_valid_state(struct drbd_conf *mdev, union drbd_state ns);
504static int is_valid_state_transition(struct drbd_conf *,
505 union drbd_state, union drbd_state);
506static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
Lars Ellenberg02bc7172010-09-06 12:13:20 +0200507 union drbd_state ns, const char **warn_sync_abort);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508int drbd_send_state_req(struct drbd_conf *,
509 union drbd_state, union drbd_state);
510
511static enum drbd_state_ret_codes _req_st_cond(struct drbd_conf *mdev,
512 union drbd_state mask, union drbd_state val)
513{
514 union drbd_state os, ns;
515 unsigned long flags;
516 int rv;
517
518 if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &mdev->flags))
519 return SS_CW_SUCCESS;
520
521 if (test_and_clear_bit(CL_ST_CHG_FAIL, &mdev->flags))
522 return SS_CW_FAILED_BY_PEER;
523
524 rv = 0;
525 spin_lock_irqsave(&mdev->req_lock, flags);
526 os = mdev->state;
527 ns.i = (os.i & ~mask.i) | val.i;
528 ns = sanitize_state(mdev, os, ns, NULL);
529
530 if (!cl_wide_st_chg(mdev, os, ns))
531 rv = SS_CW_NO_NEED;
532 if (!rv) {
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. */
538 }
539 }
540 spin_unlock_irqrestore(&mdev->req_lock, flags);
541
542 return rv;
543}
544
545/**
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.
550 * @f: flags
551 *
552 * Should not be called directly, use drbd_request_state() or
553 * _drbd_request_state().
554 */
555static int drbd_req_state(struct drbd_conf *mdev,
556 union drbd_state mask, union drbd_state val,
557 enum chg_state_flags f)
558{
559 struct completion done;
560 unsigned long flags;
561 union drbd_state os, ns;
562 int rv;
563
564 init_completion(&done);
565
566 if (f & CS_SERIALIZE)
567 mutex_lock(&mdev->state_mutex);
568
569 spin_lock_irqsave(&mdev->req_lock, flags);
570 os = mdev->state;
571 ns.i = (os.i & ~mask.i) | val.i;
572 ns = sanitize_state(mdev, os, ns, NULL);
573
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);
579
580 if (rv < SS_SUCCESS) {
581 if (f & CS_VERBOSE)
582 print_st_err(mdev, os, ns, rv);
583 goto abort;
584 }
585
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;
590 if (f & CS_VERBOSE)
591 print_st_err(mdev, os, ns, rv);
592 goto abort;
593 }
594
595 wait_event(mdev->state_wait,
596 (rv = _req_st_cond(mdev, mask, val)));
597
598 if (rv < SS_SUCCESS) {
599 drbd_state_unlock(mdev);
600 if (f & CS_VERBOSE)
601 print_st_err(mdev, os, ns, rv);
602 goto abort;
603 }
604 spin_lock_irqsave(&mdev->req_lock, flags);
605 os = mdev->state;
606 ns.i = (os.i & ~mask.i) | val.i;
607 rv = _drbd_set_state(mdev, ns, f, &done);
608 drbd_state_unlock(mdev);
609 } else {
610 rv = _drbd_set_state(mdev, ns, f, &done);
611 }
612
613 spin_unlock_irqrestore(&mdev->req_lock, flags);
614
615 if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
616 D_ASSERT(current != mdev->worker.task);
617 wait_for_completion(&done);
618 }
619
620abort:
621 if (f & CS_SERIALIZE)
622 mutex_unlock(&mdev->state_mutex);
623
624 return rv;
625}
626
627/**
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.
632 * @f: flags
633 *
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.
636 */
637int _drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
638 union drbd_state val, enum chg_state_flags f)
639{
640 int rv;
641
642 wait_event(mdev->state_wait,
643 (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
644
645 return rv;
646}
647
648static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
649{
650 dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
651 name,
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),
657 ns.susp ? 's' : 'r',
658 ns.aftr_isp ? 'a' : '-',
659 ns.peer_isp ? 'p' : '-',
660 ns.user_isp ? 'u' : '-'
661 );
662}
663
664void print_st_err(struct drbd_conf *mdev,
665 union drbd_state os, union drbd_state ns, int err)
666{
667 if (err == SS_IN_TRANSIENT_STATE)
668 return;
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);
672}
673
674
675#define drbd_peer_str drbd_role_str
676#define drbd_pdsk_str drbd_disk_str
677
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")
682
683#define PSC(A) \
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)); \
688 } })
689
690/**
691 * is_valid_state() - Returns an SS_ error code if ns is not valid
692 * @mdev: DRBD device.
693 * @ns: State to consider.
694 */
695static int is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
696{
697 /* See drbd_state_sw_errors in drbd_strings.c */
698
699 enum drbd_fencing_p fp;
700 int rv = SS_SUCCESS;
701
702 fp = FP_DONT_CARE;
703 if (get_ldev(mdev)) {
704 fp = mdev->ldev->dc.fencing;
705 put_ldev(mdev);
706 }
707
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;
712 put_net_conf(mdev);
713 }
714
715 if (rv <= 0)
716 /* already found a reason to abort */;
717 else if (ns.role == R_SECONDARY && mdev->open_cnt)
718 rv = SS_DEVICE_IN_USE;
719
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;
722
723 else if (fp >= FP_RESOURCE &&
724 ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
725 rv = SS_PRIMARY_NOP;
726
727 else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
728 rv = SS_NO_UP_TO_DATE_DISK;
729
730 else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
731 rv = SS_NO_LOCAL_DISK;
732
733 else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
734 rv = SS_NO_REMOTE_DISK;
735
Lars Ellenberg8d4ce822010-04-01 16:59:32 +0200736 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;
738
Philipp Reisnerb411b362009-09-25 16:07:19 -0700739 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;
745
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;
749
750 else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
751 mdev->agreed_pro_version < 88)
752 rv = SS_NOT_SUPPORTED;
753
754 return rv;
755}
756
757/**
758 * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
759 * @mdev: DRBD device.
760 * @ns: new state.
761 * @os: old state.
762 */
763static int is_valid_state_transition(struct drbd_conf *mdev,
764 union drbd_state ns, union drbd_state os)
765{
766 int rv = SS_SUCCESS;
767
768 if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
769 os.conn > C_CONNECTED)
770 rv = SS_RESYNC_RUNNING;
771
772 if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
773 rv = SS_ALREADY_STANDALONE;
774
775 if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
776 rv = SS_IS_DISKLESS;
777
778 if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
779 rv = SS_NO_NET_CONFIG;
780
781 if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
782 rv = SS_LOWER_THAN_OUTDATED;
783
784 if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
785 rv = SS_IN_TRANSIENT_STATE;
786
787 if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
788 rv = SS_IN_TRANSIENT_STATE;
789
790 if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
791 rv = SS_NEED_CONNECTION;
792
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;
796
797 if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
798 os.conn < C_CONNECTED)
799 rv = SS_NEED_CONNECTION;
800
801 return rv;
802}
803
804/**
805 * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
806 * @mdev: DRBD device.
807 * @os: old state.
808 * @ns: new state.
809 * @warn_sync_abort:
810 *
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.
813 */
814static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
Lars Ellenberg02bc7172010-09-06 12:13:20 +0200815 union drbd_state ns, const char **warn_sync_abort)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700816{
817 enum drbd_fencing_p fp;
818
819 fp = FP_DONT_CARE;
820 if (get_ldev(mdev)) {
821 fp = mdev->ldev->dc.fencing;
822 put_ldev(mdev);
823 }
824
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)
828 ns.conn = os.conn;
829
Lars Ellenbergf2906e12010-07-21 17:04:32 +0200830 /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow.
831 * If you try to go into some Sync* state, that shall fail (elsewhere). */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700832 if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
Lars Ellenbergf2906e12010-07-21 17:04:32 +0200833 ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING && ns.conn <= C_TEAR_DOWN)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 ns.conn = os.conn;
835
836 /* After C_DISCONNECTING only C_STANDALONE may follow */
837 if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
838 ns.conn = os.conn;
839
840 if (ns.conn < C_CONNECTED) {
841 ns.peer_isp = 0;
842 ns.peer = R_UNKNOWN;
843 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
844 ns.pdsk = D_UNKNOWN;
845 }
846
847 /* Clear the aftr_isp when becoming unconfigured */
848 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
849 ns.aftr_isp = 0;
850
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 /* Abort resync if a disk fails/detaches */
852 if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
853 (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
854 if (warn_sync_abort)
Lars Ellenberg02bc7172010-09-06 12:13:20 +0200855 *warn_sync_abort =
856 os.conn == C_VERIFY_S || os.conn == C_VERIFY_T ?
857 "Online-verify" : "Resync";
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858 ns.conn = C_CONNECTED;
859 }
860
861 if (ns.conn >= C_CONNECTED &&
862 ((ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED) ||
863 (ns.disk == D_NEGOTIATING && ns.conn == C_WF_BITMAP_T))) {
864 switch (ns.conn) {
865 case C_WF_BITMAP_T:
866 case C_PAUSED_SYNC_T:
867 ns.disk = D_OUTDATED;
868 break;
869 case C_CONNECTED:
870 case C_WF_BITMAP_S:
871 case C_SYNC_SOURCE:
872 case C_PAUSED_SYNC_S:
873 ns.disk = D_UP_TO_DATE;
874 break;
875 case C_SYNC_TARGET:
876 ns.disk = D_INCONSISTENT;
877 dev_warn(DEV, "Implicitly set disk state Inconsistent!\n");
878 break;
879 }
880 if (os.disk == D_OUTDATED && ns.disk == D_UP_TO_DATE)
881 dev_warn(DEV, "Implicitly set disk from Outdated to UpToDate\n");
882 }
883
884 if (ns.conn >= C_CONNECTED &&
885 (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)) {
886 switch (ns.conn) {
887 case C_CONNECTED:
888 case C_WF_BITMAP_T:
889 case C_PAUSED_SYNC_T:
890 case C_SYNC_TARGET:
891 ns.pdsk = D_UP_TO_DATE;
892 break;
893 case C_WF_BITMAP_S:
894 case C_PAUSED_SYNC_S:
Lars Ellenberge0f83012010-04-01 15:13:19 +0200895 /* remap any consistent state to D_OUTDATED,
896 * but disallow "upgrade" of not even consistent states.
897 */
898 ns.pdsk =
899 (D_DISKLESS < os.pdsk && os.pdsk < D_OUTDATED)
900 ? os.pdsk : D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901 break;
902 case C_SYNC_SOURCE:
903 ns.pdsk = D_INCONSISTENT;
904 dev_warn(DEV, "Implicitly set pdsk Inconsistent!\n");
905 break;
906 }
907 if (os.pdsk == D_OUTDATED && ns.pdsk == D_UP_TO_DATE)
908 dev_warn(DEV, "Implicitly set pdsk from Outdated to UpToDate\n");
909 }
910
911 /* Connection breaks down before we finished "Negotiating" */
912 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
913 get_ldev_if_state(mdev, D_NEGOTIATING)) {
914 if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
915 ns.disk = mdev->new_state_tmp.disk;
916 ns.pdsk = mdev->new_state_tmp.pdsk;
917 } else {
918 dev_alert(DEV, "Connection lost while negotiating, no data!\n");
919 ns.disk = D_DISKLESS;
920 ns.pdsk = D_UNKNOWN;
921 }
922 put_ldev(mdev);
923 }
924
925 if (fp == FP_STONITH &&
Philipp Reisner0a492162009-10-21 13:08:29 +0200926 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
927 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200928 ns.susp = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
929
930 if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
931 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
932 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
933 ns.susp = 1; /* Suspend IO while no data available (no accessible data available) */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934
935 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
936 if (ns.conn == C_SYNC_SOURCE)
937 ns.conn = C_PAUSED_SYNC_S;
938 if (ns.conn == C_SYNC_TARGET)
939 ns.conn = C_PAUSED_SYNC_T;
940 } else {
941 if (ns.conn == C_PAUSED_SYNC_S)
942 ns.conn = C_SYNC_SOURCE;
943 if (ns.conn == C_PAUSED_SYNC_T)
944 ns.conn = C_SYNC_TARGET;
945 }
946
947 return ns;
948}
949
950/* helper for __drbd_set_state */
951static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
952{
953 if (cs == C_VERIFY_T) {
954 /* starting online verify from an arbitrary position
955 * does not fit well into the existing protocol.
956 * on C_VERIFY_T, we initialize ov_left and friends
957 * implicitly in receive_DataRequest once the
958 * first P_OV_REQUEST is received */
959 mdev->ov_start_sector = ~(sector_t)0;
960 } else {
961 unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
962 if (bit >= mdev->rs_total)
963 mdev->ov_start_sector =
964 BM_BIT_TO_SECT(mdev->rs_total - 1);
965 mdev->ov_position = mdev->ov_start_sector;
966 }
967}
968
Philipp Reisner07782862010-08-31 12:00:50 +0200969static void drbd_resume_al(struct drbd_conf *mdev)
970{
971 if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
972 dev_info(DEV, "Resumed AL updates\n");
973}
974
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975/**
976 * __drbd_set_state() - Set a new DRBD state
977 * @mdev: DRBD device.
978 * @ns: new state.
979 * @flags: Flags
980 * @done: Optional completion, that will get completed after the after_state_ch() finished
981 *
982 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
983 */
984int __drbd_set_state(struct drbd_conf *mdev,
985 union drbd_state ns, enum chg_state_flags flags,
986 struct completion *done)
987{
988 union drbd_state os;
989 int rv = SS_SUCCESS;
Lars Ellenberg02bc7172010-09-06 12:13:20 +0200990 const char *warn_sync_abort = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991 struct after_state_chg_work *ascw;
992
993 os = mdev->state;
994
995 ns = sanitize_state(mdev, os, ns, &warn_sync_abort);
996
997 if (ns.i == os.i)
998 return SS_NOTHING_TO_DO;
999
1000 if (!(flags & CS_HARD)) {
1001 /* pre-state-change checks ; only look at ns */
1002 /* See drbd_state_sw_errors in drbd_strings.c */
1003
1004 rv = is_valid_state(mdev, ns);
1005 if (rv < SS_SUCCESS) {
1006 /* If the old state was illegal as well, then let
1007 this happen...*/
1008
Philipp Reisner1616a252010-06-10 16:55:15 +02001009 if (is_valid_state(mdev, os) == rv)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010 rv = is_valid_state_transition(mdev, ns, os);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011 } else
1012 rv = is_valid_state_transition(mdev, ns, os);
1013 }
1014
1015 if (rv < SS_SUCCESS) {
1016 if (flags & CS_VERBOSE)
1017 print_st_err(mdev, os, ns, rv);
1018 return rv;
1019 }
1020
1021 if (warn_sync_abort)
Lars Ellenberg02bc7172010-09-06 12:13:20 +02001022 dev_warn(DEV, "%s aborted.\n", warn_sync_abort);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023
1024 {
1025 char *pbp, pb[300];
1026 pbp = pb;
1027 *pbp = 0;
1028 PSC(role);
1029 PSC(peer);
1030 PSC(conn);
1031 PSC(disk);
1032 PSC(pdsk);
1033 PSC(susp);
1034 PSC(aftr_isp);
1035 PSC(peer_isp);
1036 PSC(user_isp);
1037 dev_info(DEV, "%s\n", pb);
1038 }
1039
1040 /* solve the race between becoming unconfigured,
1041 * worker doing the cleanup, and
1042 * admin reconfiguring us:
1043 * on (re)configure, first set CONFIG_PENDING,
1044 * then wait for a potentially exiting worker,
1045 * start the worker, and schedule one no_op.
1046 * then proceed with configuration.
1047 */
1048 if (ns.disk == D_DISKLESS &&
1049 ns.conn == C_STANDALONE &&
1050 ns.role == R_SECONDARY &&
1051 !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
1052 set_bit(DEVICE_DYING, &mdev->flags);
1053
1054 mdev->state.i = ns.i;
1055 wake_up(&mdev->misc_wait);
1056 wake_up(&mdev->state_wait);
1057
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 /* aborted verify run. log the last position */
1059 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
1060 ns.conn < C_CONNECTED) {
1061 mdev->ov_start_sector =
1062 BM_BIT_TO_SECT(mdev->rs_total - mdev->ov_left);
1063 dev_info(DEV, "Online Verify reached sector %llu\n",
1064 (unsigned long long)mdev->ov_start_sector);
1065 }
1066
1067 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1068 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
1069 dev_info(DEV, "Syncer continues.\n");
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001070 mdev->rs_paused += (long)jiffies
1071 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
Philipp Reisner63106d32010-09-01 15:47:15 +02001072 if (ns.conn == C_SYNC_TARGET)
1073 mod_timer(&mdev->resync_timer, jiffies);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074 }
1075
1076 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1077 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1078 dev_info(DEV, "Resync suspended\n");
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001079 mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001080 }
1081
1082 if (os.conn == C_CONNECTED &&
1083 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001084 unsigned long now = jiffies;
1085 int i;
1086
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 mdev->ov_position = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001088 mdev->rs_total = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089 if (mdev->agreed_pro_version >= 90)
1090 set_ov_position(mdev, ns.conn);
1091 else
1092 mdev->ov_start_sector = 0;
1093 mdev->ov_left = mdev->rs_total
1094 - BM_SECT_TO_BIT(mdev->ov_position);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001095 mdev->rs_start = now;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001096 mdev->rs_last_events = 0;
1097 mdev->rs_last_sect_ev = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098 mdev->ov_last_oos_size = 0;
1099 mdev->ov_last_oos_start = 0;
1100
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001101 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1102 mdev->rs_mark_left[i] = mdev->rs_total;
1103 mdev->rs_mark_time[i] = now;
1104 }
1105
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 if (ns.conn == C_VERIFY_S) {
1107 dev_info(DEV, "Starting Online Verify from sector %llu\n",
1108 (unsigned long long)mdev->ov_position);
1109 mod_timer(&mdev->resync_timer, jiffies);
1110 }
1111 }
1112
1113 if (get_ldev(mdev)) {
1114 u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
1115 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1116 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1117
1118 if (test_bit(CRASHED_PRIMARY, &mdev->flags))
1119 mdf |= MDF_CRASHED_PRIMARY;
1120 if (mdev->state.role == R_PRIMARY ||
1121 (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
1122 mdf |= MDF_PRIMARY_IND;
1123 if (mdev->state.conn > C_WF_REPORT_PARAMS)
1124 mdf |= MDF_CONNECTED_IND;
1125 if (mdev->state.disk > D_INCONSISTENT)
1126 mdf |= MDF_CONSISTENT;
1127 if (mdev->state.disk > D_OUTDATED)
1128 mdf |= MDF_WAS_UP_TO_DATE;
1129 if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
1130 mdf |= MDF_PEER_OUT_DATED;
1131 if (mdf != mdev->ldev->md.flags) {
1132 mdev->ldev->md.flags = mdf;
1133 drbd_md_mark_dirty(mdev);
1134 }
1135 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
1136 drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
1137 put_ldev(mdev);
1138 }
1139
1140 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1141 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1142 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
1143 set_bit(CONSIDER_RESYNC, &mdev->flags);
1144
1145 /* Receiver should clean up itself */
1146 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
1147 drbd_thread_stop_nowait(&mdev->receiver);
1148
1149 /* Now the receiver finished cleaning up itself, it should die */
1150 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
1151 drbd_thread_stop_nowait(&mdev->receiver);
1152
1153 /* Upon network failure, we need to restart the receiver. */
1154 if (os.conn > C_TEAR_DOWN &&
1155 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
1156 drbd_thread_restart_nowait(&mdev->receiver);
1157
Philipp Reisner07782862010-08-31 12:00:50 +02001158 /* Resume AL writing if we get a connection */
1159 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
1160 drbd_resume_al(mdev);
1161
Philipp Reisnerb411b362009-09-25 16:07:19 -07001162 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1163 if (ascw) {
1164 ascw->os = os;
1165 ascw->ns = ns;
1166 ascw->flags = flags;
1167 ascw->w.cb = w_after_state_ch;
1168 ascw->done = done;
1169 drbd_queue_work(&mdev->data.work, &ascw->w);
1170 } else {
1171 dev_warn(DEV, "Could not kmalloc an ascw\n");
1172 }
1173
1174 return rv;
1175}
1176
1177static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1178{
1179 struct after_state_chg_work *ascw =
1180 container_of(w, struct after_state_chg_work, w);
1181 after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
1182 if (ascw->flags & CS_WAIT_COMPLETE) {
1183 D_ASSERT(ascw->done != NULL);
1184 complete(ascw->done);
1185 }
1186 kfree(ascw);
1187
1188 return 1;
1189}
1190
1191static void abw_start_sync(struct drbd_conf *mdev, int rv)
1192{
1193 if (rv) {
1194 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
1195 _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
1196 return;
1197 }
1198
1199 switch (mdev->state.conn) {
1200 case C_STARTING_SYNC_T:
1201 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
1202 break;
1203 case C_STARTING_SYNC_S:
1204 drbd_start_resync(mdev, C_SYNC_SOURCE);
1205 break;
1206 }
1207}
1208
1209/**
1210 * after_state_ch() - Perform after state change actions that may sleep
1211 * @mdev: DRBD device.
1212 * @os: old state.
1213 * @ns: new state.
1214 * @flags: Flags
1215 */
1216static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
1217 union drbd_state ns, enum chg_state_flags flags)
1218{
1219 enum drbd_fencing_p fp;
Philipp Reisner67098932010-06-24 16:24:25 +02001220 enum drbd_req_event what = nothing;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001221
1222 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
1223 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1224 if (mdev->p_uuid)
1225 mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
1226 }
1227
1228 fp = FP_DONT_CARE;
1229 if (get_ldev(mdev)) {
1230 fp = mdev->ldev->dc.fencing;
1231 put_ldev(mdev);
1232 }
1233
1234 /* Inform userspace about the change... */
1235 drbd_bcast_state(mdev, ns);
1236
1237 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1238 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
1239 drbd_khelper(mdev, "pri-on-incon-degr");
1240
1241 /* Here we have the actions that are performed after a
1242 state change. This function might sleep */
1243
Philipp Reisner265be2d2010-05-31 10:14:17 +02001244 if (os.susp && ns.susp && mdev->sync_conf.on_no_data == OND_SUSPEND_IO) {
1245 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Philipp Reisner67098932010-06-24 16:24:25 +02001246 if (ns.conn == C_CONNECTED)
1247 what = resend;
1248 else /* ns.conn > C_CONNECTED */
Philipp Reisner265be2d2010-05-31 10:14:17 +02001249 dev_err(DEV, "Unexpected Resynd going on!\n");
1250 }
1251
Philipp Reisner67098932010-06-24 16:24:25 +02001252 if (os.disk == D_ATTACHING && ns.disk > D_ATTACHING)
1253 what = restart_frozen_disk_io;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001254 }
1255
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 if (fp == FP_STONITH && ns.susp) {
Philipp Reisner43a51822010-06-11 11:26:34 +02001257 /* case1: The outdate peer handler is successful: */
1258 if (os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 tl_clear(mdev);
Philipp Reisner43a51822010-06-11 11:26:34 +02001260 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
1261 drbd_uuid_new_current(mdev);
1262 clear_bit(NEW_CUR_UUID, &mdev->flags);
1263 drbd_md_sync(mdev);
1264 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265 spin_lock_irq(&mdev->req_lock);
1266 _drbd_set_state(_NS(mdev, susp, 0), CS_VERBOSE, NULL);
1267 spin_unlock_irq(&mdev->req_lock);
1268 }
Philipp Reisner43a51822010-06-11 11:26:34 +02001269 /* case2: The connection was established again: */
1270 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
1271 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner67098932010-06-24 16:24:25 +02001272 what = resend;
Philipp Reisner43a51822010-06-11 11:26:34 +02001273 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 }
Philipp Reisner67098932010-06-24 16:24:25 +02001275
1276 if (what != nothing) {
1277 spin_lock_irq(&mdev->req_lock);
1278 _tl_restart(mdev, what);
1279 _drbd_set_state(_NS(mdev, susp, 0), CS_VERBOSE, NULL);
1280 spin_unlock_irq(&mdev->req_lock);
1281 }
1282
Philipp Reisnerb411b362009-09-25 16:07:19 -07001283 /* Do not change the order of the if above and the two below... */
1284 if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
1285 drbd_send_uuids(mdev);
1286 drbd_send_state(mdev);
1287 }
1288 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S)
1289 drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL, "send_bitmap (WFBitMapS)");
1290
1291 /* Lost contact to peer's copy of the data */
1292 if ((os.pdsk >= D_INCONSISTENT &&
1293 os.pdsk != D_UNKNOWN &&
1294 os.pdsk != D_OUTDATED)
1295 && (ns.pdsk < D_INCONSISTENT ||
1296 ns.pdsk == D_UNKNOWN ||
1297 ns.pdsk == D_OUTDATED)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 if (get_ldev(mdev)) {
1299 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001300 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
Philipp Reisner43a51822010-06-11 11:26:34 +02001301 if (mdev->state.susp) {
1302 set_bit(NEW_CUR_UUID, &mdev->flags);
1303 } else {
1304 drbd_uuid_new_current(mdev);
1305 drbd_send_uuids(mdev);
1306 }
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001307 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 put_ldev(mdev);
1309 }
1310 }
1311
1312 if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
Philipp Reisner18a50fa2010-06-21 14:14:15 +02001313 if (ns.peer == R_PRIMARY && mdev->ldev->md.uuid[UI_BITMAP] == 0) {
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001314 drbd_uuid_new_current(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02001315 drbd_send_uuids(mdev);
1316 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001317
1318 /* D_DISKLESS Peer becomes secondary */
1319 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1320 drbd_al_to_on_disk_bm(mdev);
1321 put_ldev(mdev);
1322 }
1323
1324 /* Last part of the attaching process ... */
1325 if (ns.conn >= C_CONNECTED &&
1326 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Philipp Reisnere89b5912010-03-24 17:11:33 +01001327 drbd_send_sizes(mdev, 0, 0); /* to start sync... */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328 drbd_send_uuids(mdev);
1329 drbd_send_state(mdev);
1330 }
1331
1332 /* We want to pause/continue resync, tell peer. */
1333 if (ns.conn >= C_CONNECTED &&
1334 ((os.aftr_isp != ns.aftr_isp) ||
1335 (os.user_isp != ns.user_isp)))
1336 drbd_send_state(mdev);
1337
1338 /* In case one of the isp bits got set, suspend other devices. */
1339 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1340 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1341 suspend_other_sg(mdev);
1342
1343 /* Make sure the peer gets informed about eventual state
1344 changes (ISP bits) while we were in WFReportParams. */
1345 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
1346 drbd_send_state(mdev);
1347
1348 /* We are in the progress to start a full sync... */
1349 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1350 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1351 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, &abw_start_sync, "set_n_write from StartingSync");
1352
1353 /* We are invalidating our self... */
1354 if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
1355 os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
1356 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL, "set_n_write from invalidate");
1357
1358 if (os.disk > D_FAILED && ns.disk == D_FAILED) {
1359 enum drbd_io_error_p eh;
1360
1361 eh = EP_PASS_ON;
1362 if (get_ldev_if_state(mdev, D_FAILED)) {
1363 eh = mdev->ldev->dc.on_io_error;
1364 put_ldev(mdev);
1365 }
1366
1367 drbd_rs_cancel_all(mdev);
1368 /* since get_ldev() only works as long as disk>=D_INCONSISTENT,
1369 and it is D_DISKLESS here, local_cnt can only go down, it can
1370 not increase... It will reach zero */
1371 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1372 mdev->rs_total = 0;
1373 mdev->rs_failed = 0;
1374 atomic_set(&mdev->rs_pending_cnt, 0);
1375
1376 spin_lock_irq(&mdev->req_lock);
1377 _drbd_set_state(_NS(mdev, disk, D_DISKLESS), CS_HARD, NULL);
1378 spin_unlock_irq(&mdev->req_lock);
1379
1380 if (eh == EP_CALL_HELPER)
1381 drbd_khelper(mdev, "local-io-error");
1382 }
1383
1384 if (os.disk > D_DISKLESS && ns.disk == D_DISKLESS) {
1385
1386 if (os.disk == D_FAILED) /* && ns.disk == D_DISKLESS*/ {
1387 if (drbd_send_state(mdev))
1388 dev_warn(DEV, "Notified peer that my disk is broken.\n");
1389 else
1390 dev_err(DEV, "Sending state in drbd_io_error() failed\n");
1391 }
1392
Philipp Reisner0a6dbf22009-12-28 16:58:38 +01001393 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 lc_destroy(mdev->resync);
1395 mdev->resync = NULL;
1396 lc_destroy(mdev->act_log);
1397 mdev->act_log = NULL;
1398 __no_warn(local,
1399 drbd_free_bc(mdev->ldev);
1400 mdev->ldev = NULL;);
1401
1402 if (mdev->md_io_tmpp)
1403 __free_page(mdev->md_io_tmpp);
1404 }
1405
1406 /* Disks got bigger while they were detached */
1407 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1408 test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
1409 if (ns.conn == C_CONNECTED)
1410 resync_after_online_grow(mdev);
1411 }
1412
1413 /* A resync finished or aborted, wake paused devices... */
1414 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1415 (os.peer_isp && !ns.peer_isp) ||
1416 (os.user_isp && !ns.user_isp))
1417 resume_next_sg(mdev);
1418
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001419 /* free tl_hash if we Got thawed and are C_STANDALONE */
1420 if (ns.conn == C_STANDALONE && ns.susp == 0 && mdev->tl_hash)
1421 drbd_free_tl_hash(mdev);
1422
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423 /* Upon network connection, we need to start the receiver */
1424 if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
1425 drbd_thread_start(&mdev->receiver);
1426
1427 /* Terminate worker thread if we are unconfigured - it will be
1428 restarted as needed... */
1429 if (ns.disk == D_DISKLESS &&
1430 ns.conn == C_STANDALONE &&
1431 ns.role == R_SECONDARY) {
1432 if (os.aftr_isp != ns.aftr_isp)
1433 resume_next_sg(mdev);
1434 /* set in __drbd_set_state, unless CONFIG_PENDING was set */
1435 if (test_bit(DEVICE_DYING, &mdev->flags))
1436 drbd_thread_stop_nowait(&mdev->worker);
1437 }
1438
1439 drbd_md_sync(mdev);
1440}
1441
1442
1443static int drbd_thread_setup(void *arg)
1444{
1445 struct drbd_thread *thi = (struct drbd_thread *) arg;
1446 struct drbd_conf *mdev = thi->mdev;
1447 unsigned long flags;
1448 int retval;
1449
1450restart:
1451 retval = thi->function(thi);
1452
1453 spin_lock_irqsave(&thi->t_lock, flags);
1454
1455 /* if the receiver has been "Exiting", the last thing it did
1456 * was set the conn state to "StandAlone",
1457 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
1458 * and receiver thread will be "started".
1459 * drbd_thread_start needs to set "Restarting" in that case.
1460 * t_state check and assignment needs to be within the same spinlock,
1461 * so either thread_start sees Exiting, and can remap to Restarting,
1462 * or thread_start see None, and can proceed as normal.
1463 */
1464
1465 if (thi->t_state == Restarting) {
1466 dev_info(DEV, "Restarting %s\n", current->comm);
1467 thi->t_state = Running;
1468 spin_unlock_irqrestore(&thi->t_lock, flags);
1469 goto restart;
1470 }
1471
1472 thi->task = NULL;
1473 thi->t_state = None;
1474 smp_mb();
1475 complete(&thi->stop);
1476 spin_unlock_irqrestore(&thi->t_lock, flags);
1477
1478 dev_info(DEV, "Terminating %s\n", current->comm);
1479
1480 /* Release mod reference taken when thread was started */
1481 module_put(THIS_MODULE);
1482 return retval;
1483}
1484
1485static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
1486 int (*func) (struct drbd_thread *))
1487{
1488 spin_lock_init(&thi->t_lock);
1489 thi->task = NULL;
1490 thi->t_state = None;
1491 thi->function = func;
1492 thi->mdev = mdev;
1493}
1494
1495int drbd_thread_start(struct drbd_thread *thi)
1496{
1497 struct drbd_conf *mdev = thi->mdev;
1498 struct task_struct *nt;
1499 unsigned long flags;
1500
1501 const char *me =
1502 thi == &mdev->receiver ? "receiver" :
1503 thi == &mdev->asender ? "asender" :
1504 thi == &mdev->worker ? "worker" : "NONSENSE";
1505
1506 /* is used from state engine doing drbd_thread_stop_nowait,
1507 * while holding the req lock irqsave */
1508 spin_lock_irqsave(&thi->t_lock, flags);
1509
1510 switch (thi->t_state) {
1511 case None:
1512 dev_info(DEV, "Starting %s thread (from %s [%d])\n",
1513 me, current->comm, current->pid);
1514
1515 /* Get ref on module for thread - this is released when thread exits */
1516 if (!try_module_get(THIS_MODULE)) {
1517 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
1518 spin_unlock_irqrestore(&thi->t_lock, flags);
1519 return FALSE;
1520 }
1521
1522 init_completion(&thi->stop);
1523 D_ASSERT(thi->task == NULL);
1524 thi->reset_cpu_mask = 1;
1525 thi->t_state = Running;
1526 spin_unlock_irqrestore(&thi->t_lock, flags);
1527 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
1528
1529 nt = kthread_create(drbd_thread_setup, (void *) thi,
1530 "drbd%d_%s", mdev_to_minor(mdev), me);
1531
1532 if (IS_ERR(nt)) {
1533 dev_err(DEV, "Couldn't start thread\n");
1534
1535 module_put(THIS_MODULE);
1536 return FALSE;
1537 }
1538 spin_lock_irqsave(&thi->t_lock, flags);
1539 thi->task = nt;
1540 thi->t_state = Running;
1541 spin_unlock_irqrestore(&thi->t_lock, flags);
1542 wake_up_process(nt);
1543 break;
1544 case Exiting:
1545 thi->t_state = Restarting;
1546 dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
1547 me, current->comm, current->pid);
1548 /* fall through */
1549 case Running:
1550 case Restarting:
1551 default:
1552 spin_unlock_irqrestore(&thi->t_lock, flags);
1553 break;
1554 }
1555
1556 return TRUE;
1557}
1558
1559
1560void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1561{
1562 unsigned long flags;
1563
1564 enum drbd_thread_state ns = restart ? Restarting : Exiting;
1565
1566 /* may be called from state engine, holding the req lock irqsave */
1567 spin_lock_irqsave(&thi->t_lock, flags);
1568
1569 if (thi->t_state == None) {
1570 spin_unlock_irqrestore(&thi->t_lock, flags);
1571 if (restart)
1572 drbd_thread_start(thi);
1573 return;
1574 }
1575
1576 if (thi->t_state != ns) {
1577 if (thi->task == NULL) {
1578 spin_unlock_irqrestore(&thi->t_lock, flags);
1579 return;
1580 }
1581
1582 thi->t_state = ns;
1583 smp_mb();
1584 init_completion(&thi->stop);
1585 if (thi->task != current)
1586 force_sig(DRBD_SIGKILL, thi->task);
1587
1588 }
1589
1590 spin_unlock_irqrestore(&thi->t_lock, flags);
1591
1592 if (wait)
1593 wait_for_completion(&thi->stop);
1594}
1595
1596#ifdef CONFIG_SMP
1597/**
1598 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
1599 * @mdev: DRBD device.
1600 *
1601 * Forces all threads of a device onto the same CPU. This is beneficial for
1602 * DRBD's performance. May be overwritten by user's configuration.
1603 */
1604void drbd_calc_cpu_mask(struct drbd_conf *mdev)
1605{
1606 int ord, cpu;
1607
1608 /* user override. */
1609 if (cpumask_weight(mdev->cpu_mask))
1610 return;
1611
1612 ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
1613 for_each_online_cpu(cpu) {
1614 if (ord-- == 0) {
1615 cpumask_set_cpu(cpu, mdev->cpu_mask);
1616 return;
1617 }
1618 }
1619 /* should not be reached */
1620 cpumask_setall(mdev->cpu_mask);
1621}
1622
1623/**
1624 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
1625 * @mdev: DRBD device.
1626 *
1627 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
1628 * prematurely.
1629 */
1630void drbd_thread_current_set_cpu(struct drbd_conf *mdev)
1631{
1632 struct task_struct *p = current;
1633 struct drbd_thread *thi =
1634 p == mdev->asender.task ? &mdev->asender :
1635 p == mdev->receiver.task ? &mdev->receiver :
1636 p == mdev->worker.task ? &mdev->worker :
1637 NULL;
1638 ERR_IF(thi == NULL)
1639 return;
1640 if (!thi->reset_cpu_mask)
1641 return;
1642 thi->reset_cpu_mask = 0;
1643 set_cpus_allowed_ptr(p, mdev->cpu_mask);
1644}
1645#endif
1646
1647/* the appropriate socket mutex must be held already */
1648int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001649 enum drbd_packets cmd, struct p_header80 *h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650 size_t size, unsigned msg_flags)
1651{
1652 int sent, ok;
1653
1654 ERR_IF(!h) return FALSE;
1655 ERR_IF(!size) return FALSE;
1656
1657 h->magic = BE_DRBD_MAGIC;
1658 h->command = cpu_to_be16(cmd);
Philipp Reisner0b70a132010-08-20 13:36:10 +02001659 h->length = cpu_to_be16(size-sizeof(struct p_header80));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660
Philipp Reisnerb411b362009-09-25 16:07:19 -07001661 sent = drbd_send(mdev, sock, h, size, msg_flags);
1662
1663 ok = (sent == size);
1664 if (!ok)
1665 dev_err(DEV, "short sent %s size=%d sent=%d\n",
1666 cmdname(cmd), (int)size, sent);
1667 return ok;
1668}
1669
1670/* don't pass the socket. we may only look at it
1671 * when we hold the appropriate socket mutex.
1672 */
1673int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001674 enum drbd_packets cmd, struct p_header80 *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675{
1676 int ok = 0;
1677 struct socket *sock;
1678
1679 if (use_data_socket) {
1680 mutex_lock(&mdev->data.mutex);
1681 sock = mdev->data.socket;
1682 } else {
1683 mutex_lock(&mdev->meta.mutex);
1684 sock = mdev->meta.socket;
1685 }
1686
1687 /* drbd_disconnect() could have called drbd_free_sock()
1688 * while we were waiting in down()... */
1689 if (likely(sock != NULL))
1690 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
1691
1692 if (use_data_socket)
1693 mutex_unlock(&mdev->data.mutex);
1694 else
1695 mutex_unlock(&mdev->meta.mutex);
1696 return ok;
1697}
1698
1699int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data,
1700 size_t size)
1701{
Philipp Reisner0b70a132010-08-20 13:36:10 +02001702 struct p_header80 h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703 int ok;
1704
1705 h.magic = BE_DRBD_MAGIC;
1706 h.command = cpu_to_be16(cmd);
1707 h.length = cpu_to_be16(size);
1708
1709 if (!drbd_get_data_sock(mdev))
1710 return 0;
1711
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712 ok = (sizeof(h) ==
1713 drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0));
1714 ok = ok && (size ==
1715 drbd_send(mdev, mdev->data.socket, data, size, 0));
1716
1717 drbd_put_data_sock(mdev);
1718
1719 return ok;
1720}
1721
1722int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
1723{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001724 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725 struct socket *sock;
1726 int size, rv;
1727 const int apv = mdev->agreed_pro_version;
1728
1729 size = apv <= 87 ? sizeof(struct p_rs_param)
1730 : apv == 88 ? sizeof(struct p_rs_param)
1731 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001732 : apv <= 94 ? sizeof(struct p_rs_param_89)
1733 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734
1735 /* used from admin command context and receiver/worker context.
1736 * to avoid kmalloc, grab the socket right here,
1737 * then use the pre-allocated sbuf there */
1738 mutex_lock(&mdev->data.mutex);
1739 sock = mdev->data.socket;
1740
1741 if (likely(sock != NULL)) {
1742 enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
1743
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001744 p = &mdev->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745
1746 /* initialize verify_alg and csums_alg */
1747 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
1748
1749 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001750 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
1751 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
1752 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
1753 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754
1755 if (apv >= 88)
1756 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
1757 if (apv >= 89)
1758 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
1759
1760 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
1761 } else
1762 rv = 0; /* not ok */
1763
1764 mutex_unlock(&mdev->data.mutex);
1765
1766 return rv;
1767}
1768
1769int drbd_send_protocol(struct drbd_conf *mdev)
1770{
1771 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001772 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773
1774 size = sizeof(struct p_protocol);
1775
1776 if (mdev->agreed_pro_version >= 87)
1777 size += strlen(mdev->net_conf->integrity_alg) + 1;
1778
1779 /* we must not recurse into our own queue,
1780 * as that is blocked during handshake */
1781 p = kmalloc(size, GFP_NOIO);
1782 if (p == NULL)
1783 return 0;
1784
1785 p->protocol = cpu_to_be32(mdev->net_conf->wire_protocol);
1786 p->after_sb_0p = cpu_to_be32(mdev->net_conf->after_sb_0p);
1787 p->after_sb_1p = cpu_to_be32(mdev->net_conf->after_sb_1p);
1788 p->after_sb_2p = cpu_to_be32(mdev->net_conf->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
1790
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001791 cf = 0;
1792 if (mdev->net_conf->want_lose)
1793 cf |= CF_WANT_LOSE;
1794 if (mdev->net_conf->dry_run) {
1795 if (mdev->agreed_pro_version >= 92)
1796 cf |= CF_DRY_RUN;
1797 else {
1798 dev_err(DEV, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +02001799 kfree(p);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001800 return 0;
1801 }
1802 }
1803 p->conn_flags = cpu_to_be32(cf);
1804
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805 if (mdev->agreed_pro_version >= 87)
1806 strcpy(p->integrity_alg, mdev->net_conf->integrity_alg);
1807
1808 rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001809 (struct p_header80 *)p, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810 kfree(p);
1811 return rv;
1812}
1813
1814int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
1815{
1816 struct p_uuids p;
1817 int i;
1818
1819 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
1820 return 1;
1821
1822 for (i = UI_CURRENT; i < UI_SIZE; i++)
1823 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
1824
1825 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
1826 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
1827 uuid_flags |= mdev->net_conf->want_lose ? 1 : 0;
1828 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
1829 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
1830 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
1831
1832 put_ldev(mdev);
1833
1834 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001835 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836}
1837
1838int drbd_send_uuids(struct drbd_conf *mdev)
1839{
1840 return _drbd_send_uuids(mdev, 0);
1841}
1842
1843int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
1844{
1845 return _drbd_send_uuids(mdev, 8);
1846}
1847
1848
1849int drbd_send_sync_uuid(struct drbd_conf *mdev, u64 val)
1850{
1851 struct p_rs_uuid p;
1852
1853 p.uuid = cpu_to_be64(val);
1854
1855 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001856 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857}
1858
Philipp Reisnere89b5912010-03-24 17:11:33 +01001859int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860{
1861 struct p_sizes p;
1862 sector_t d_size, u_size;
1863 int q_order_type;
1864 int ok;
1865
1866 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
1867 D_ASSERT(mdev->ldev->backing_bdev);
1868 d_size = drbd_get_max_capacity(mdev->ldev);
1869 u_size = mdev->ldev->dc.disk_size;
1870 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001871 put_ldev(mdev);
1872 } else {
1873 d_size = 0;
1874 u_size = 0;
1875 q_order_type = QUEUE_ORDERED_NONE;
1876 }
1877
1878 p.d_size = cpu_to_be64(d_size);
1879 p.u_size = cpu_to_be64(u_size);
1880 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
1881 p.max_segment_size = cpu_to_be32(queue_max_segment_size(mdev->rq_queue));
Philipp Reisnere89b5912010-03-24 17:11:33 +01001882 p.queue_order_type = cpu_to_be16(q_order_type);
1883 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884
1885 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001886 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887 return ok;
1888}
1889
1890/**
1891 * drbd_send_state() - Sends the drbd state to the peer
1892 * @mdev: DRBD device.
1893 */
1894int drbd_send_state(struct drbd_conf *mdev)
1895{
1896 struct socket *sock;
1897 struct p_state p;
1898 int ok = 0;
1899
1900 /* Grab state lock so we wont send state if we're in the middle
1901 * of a cluster wide state change on another thread */
1902 drbd_state_lock(mdev);
1903
1904 mutex_lock(&mdev->data.mutex);
1905
1906 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
1907 sock = mdev->data.socket;
1908
1909 if (likely(sock != NULL)) {
1910 ok = _drbd_send_cmd(mdev, sock, P_STATE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001911 (struct p_header80 *)&p, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001912 }
1913
1914 mutex_unlock(&mdev->data.mutex);
1915
1916 drbd_state_unlock(mdev);
1917 return ok;
1918}
1919
1920int drbd_send_state_req(struct drbd_conf *mdev,
1921 union drbd_state mask, union drbd_state val)
1922{
1923 struct p_req_state p;
1924
1925 p.mask = cpu_to_be32(mask.i);
1926 p.val = cpu_to_be32(val.i);
1927
1928 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001929 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001930}
1931
1932int drbd_send_sr_reply(struct drbd_conf *mdev, int retcode)
1933{
1934 struct p_req_state_reply p;
1935
1936 p.retcode = cpu_to_be32(retcode);
1937
1938 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001939 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001940}
1941
1942int fill_bitmap_rle_bits(struct drbd_conf *mdev,
1943 struct p_compressed_bm *p,
1944 struct bm_xfer_ctx *c)
1945{
1946 struct bitstream bs;
1947 unsigned long plain_bits;
1948 unsigned long tmp;
1949 unsigned long rl;
1950 unsigned len;
1951 unsigned toggle;
1952 int bits;
1953
1954 /* may we use this feature? */
1955 if ((mdev->sync_conf.use_rle == 0) ||
1956 (mdev->agreed_pro_version < 90))
1957 return 0;
1958
1959 if (c->bit_offset >= c->bm_bits)
1960 return 0; /* nothing to do. */
1961
1962 /* use at most thus many bytes */
1963 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1964 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1965 /* plain bits covered in this code string */
1966 plain_bits = 0;
1967
1968 /* p->encoding & 0x80 stores whether the first run length is set.
1969 * bit offset is implicit.
1970 * start with toggle == 2 to be able to tell the first iteration */
1971 toggle = 2;
1972
1973 /* see how much plain bits we can stuff into one packet
1974 * using RLE and VLI. */
1975 do {
1976 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1977 : _drbd_bm_find_next(mdev, c->bit_offset);
1978 if (tmp == -1UL)
1979 tmp = c->bm_bits;
1980 rl = tmp - c->bit_offset;
1981
1982 if (toggle == 2) { /* first iteration */
1983 if (rl == 0) {
1984 /* the first checked bit was set,
1985 * store start value, */
1986 DCBP_set_start(p, 1);
1987 /* but skip encoding of zero run length */
1988 toggle = !toggle;
1989 continue;
1990 }
1991 DCBP_set_start(p, 0);
1992 }
1993
1994 /* paranoia: catch zero runlength.
1995 * can only happen if bitmap is modified while we scan it. */
1996 if (rl == 0) {
1997 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
1998 "t:%u bo:%lu\n", toggle, c->bit_offset);
1999 return -1;
2000 }
2001
2002 bits = vli_encode_bits(&bs, rl);
2003 if (bits == -ENOBUFS) /* buffer full */
2004 break;
2005 if (bits <= 0) {
2006 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
2007 return 0;
2008 }
2009
2010 toggle = !toggle;
2011 plain_bits += rl;
2012 c->bit_offset = tmp;
2013 } while (c->bit_offset < c->bm_bits);
2014
2015 len = bs.cur.b - p->code + !!bs.cur.bit;
2016
2017 if (plain_bits < (len << 3)) {
2018 /* incompressible with this method.
2019 * we need to rewind both word and bit position. */
2020 c->bit_offset -= plain_bits;
2021 bm_xfer_ctx_bit_to_word_offset(c);
2022 c->bit_offset = c->word_offset * BITS_PER_LONG;
2023 return 0;
2024 }
2025
2026 /* RLE + VLI was able to compress it just fine.
2027 * update c->word_offset. */
2028 bm_xfer_ctx_bit_to_word_offset(c);
2029
2030 /* store pad_bits */
2031 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
2032
2033 return len;
2034}
2035
2036enum { OK, FAILED, DONE }
2037send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002038 struct p_header80 *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002039{
2040 struct p_compressed_bm *p = (void*)h;
2041 unsigned long num_words;
2042 int len;
2043 int ok;
2044
2045 len = fill_bitmap_rle_bits(mdev, p, c);
2046
2047 if (len < 0)
2048 return FAILED;
2049
2050 if (len) {
2051 DCBP_set_code(p, RLE_VLI_Bits);
2052 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h,
2053 sizeof(*p) + len, 0);
2054
2055 c->packets[0]++;
2056 c->bytes[0] += sizeof(*p) + len;
2057
2058 if (c->bit_offset >= c->bm_bits)
2059 len = 0; /* DONE */
2060 } else {
2061 /* was not compressible.
2062 * send a buffer full of plain text bits instead. */
2063 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
2064 len = num_words * sizeof(long);
2065 if (len)
2066 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
2067 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002068 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069 c->word_offset += num_words;
2070 c->bit_offset = c->word_offset * BITS_PER_LONG;
2071
2072 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02002073 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002074
2075 if (c->bit_offset > c->bm_bits)
2076 c->bit_offset = c->bm_bits;
2077 }
2078 ok = ok ? ((len == 0) ? DONE : OK) : FAILED;
2079
2080 if (ok == DONE)
2081 INFO_bm_xfer_stats(mdev, "send", c);
2082 return ok;
2083}
2084
2085/* See the comment at receive_bitmap() */
2086int _drbd_send_bitmap(struct drbd_conf *mdev)
2087{
2088 struct bm_xfer_ctx c;
Philipp Reisner0b70a132010-08-20 13:36:10 +02002089 struct p_header80 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002090 int ret;
2091
2092 ERR_IF(!mdev->bitmap) return FALSE;
2093
2094 /* maybe we should use some per thread scratch page,
2095 * and allocate that during initial device creation? */
Philipp Reisner0b70a132010-08-20 13:36:10 +02002096 p = (struct p_header80 *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002097 if (!p) {
2098 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
2099 return FALSE;
2100 }
2101
2102 if (get_ldev(mdev)) {
2103 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
2104 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
2105 drbd_bm_set_all(mdev);
2106 if (drbd_bm_write(mdev)) {
2107 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
2108 * but otherwise process as per normal - need to tell other
2109 * side that a full resync is required! */
2110 dev_err(DEV, "Failed to write bitmap to disk!\n");
2111 } else {
2112 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2113 drbd_md_sync(mdev);
2114 }
2115 }
2116 put_ldev(mdev);
2117 }
2118
2119 c = (struct bm_xfer_ctx) {
2120 .bm_bits = drbd_bm_bits(mdev),
2121 .bm_words = drbd_bm_words(mdev),
2122 };
2123
2124 do {
2125 ret = send_bitmap_rle_or_plain(mdev, p, &c);
2126 } while (ret == OK);
2127
2128 free_page((unsigned long) p);
2129 return (ret == DONE);
2130}
2131
2132int drbd_send_bitmap(struct drbd_conf *mdev)
2133{
2134 int err;
2135
2136 if (!drbd_get_data_sock(mdev))
2137 return -1;
2138 err = !_drbd_send_bitmap(mdev);
2139 drbd_put_data_sock(mdev);
2140 return err;
2141}
2142
2143int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
2144{
2145 int ok;
2146 struct p_barrier_ack p;
2147
2148 p.barrier = barrier_nr;
2149 p.set_size = cpu_to_be32(set_size);
2150
2151 if (mdev->state.conn < C_CONNECTED)
2152 return FALSE;
2153 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002154 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155 return ok;
2156}
2157
2158/**
2159 * _drbd_send_ack() - Sends an ack packet
2160 * @mdev: DRBD device.
2161 * @cmd: Packet command code.
2162 * @sector: sector, needs to be in big endian byte order
2163 * @blksize: size in byte, needs to be in big endian byte order
2164 * @block_id: Id, big endian byte order
2165 */
2166static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
2167 u64 sector,
2168 u32 blksize,
2169 u64 block_id)
2170{
2171 int ok;
2172 struct p_block_ack p;
2173
2174 p.sector = sector;
2175 p.block_id = block_id;
2176 p.blksize = blksize;
2177 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
2178
2179 if (!mdev->meta.socket || mdev->state.conn < C_CONNECTED)
2180 return FALSE;
2181 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002182 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183 return ok;
2184}
2185
2186int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
2187 struct p_data *dp)
2188{
2189 const int header_size = sizeof(struct p_data)
Philipp Reisner0b70a132010-08-20 13:36:10 +02002190 - sizeof(struct p_header80);
2191 int data_size = ((struct p_header80 *)dp)->length - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192
2193 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
2194 dp->block_id);
2195}
2196
2197int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
2198 struct p_block_req *rp)
2199{
2200 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
2201}
2202
2203/**
2204 * drbd_send_ack() - Sends an ack packet
2205 * @mdev: DRBD device.
2206 * @cmd: Packet command code.
2207 * @e: Epoch entry.
2208 */
2209int drbd_send_ack(struct drbd_conf *mdev,
2210 enum drbd_packets cmd, struct drbd_epoch_entry *e)
2211{
2212 return _drbd_send_ack(mdev, cmd,
2213 cpu_to_be64(e->sector),
2214 cpu_to_be32(e->size),
2215 e->block_id);
2216}
2217
2218/* This function misuses the block_id field to signal if the blocks
2219 * are is sync or not. */
2220int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
2221 sector_t sector, int blksize, u64 block_id)
2222{
2223 return _drbd_send_ack(mdev, cmd,
2224 cpu_to_be64(sector),
2225 cpu_to_be32(blksize),
2226 cpu_to_be64(block_id));
2227}
2228
2229int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
2230 sector_t sector, int size, u64 block_id)
2231{
2232 int ok;
2233 struct p_block_req p;
2234
2235 p.sector = cpu_to_be64(sector);
2236 p.block_id = block_id;
2237 p.blksize = cpu_to_be32(size);
2238
2239 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002240 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241 return ok;
2242}
2243
2244int drbd_send_drequest_csum(struct drbd_conf *mdev,
2245 sector_t sector, int size,
2246 void *digest, int digest_size,
2247 enum drbd_packets cmd)
2248{
2249 int ok;
2250 struct p_block_req p;
2251
2252 p.sector = cpu_to_be64(sector);
2253 p.block_id = BE_DRBD_MAGIC + 0xbeef;
2254 p.blksize = cpu_to_be32(size);
2255
2256 p.head.magic = BE_DRBD_MAGIC;
2257 p.head.command = cpu_to_be16(cmd);
Philipp Reisner0b70a132010-08-20 13:36:10 +02002258 p.head.length = cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259
2260 mutex_lock(&mdev->data.mutex);
2261
2262 ok = (sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), 0));
2263 ok = ok && (digest_size == drbd_send(mdev, mdev->data.socket, digest, digest_size, 0));
2264
2265 mutex_unlock(&mdev->data.mutex);
2266
2267 return ok;
2268}
2269
2270int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
2271{
2272 int ok;
2273 struct p_block_req p;
2274
2275 p.sector = cpu_to_be64(sector);
2276 p.block_id = BE_DRBD_MAGIC + 0xbabe;
2277 p.blksize = cpu_to_be32(size);
2278
2279 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002280 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281 return ok;
2282}
2283
2284/* called on sndtimeo
2285 * returns FALSE if we should retry,
2286 * TRUE if we think connection is dead
2287 */
2288static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *sock)
2289{
2290 int drop_it;
2291 /* long elapsed = (long)(jiffies - mdev->last_received); */
2292
2293 drop_it = mdev->meta.socket == sock
2294 || !mdev->asender.task
2295 || get_t_state(&mdev->asender) != Running
2296 || mdev->state.conn < C_CONNECTED;
2297
2298 if (drop_it)
2299 return TRUE;
2300
2301 drop_it = !--mdev->ko_count;
2302 if (!drop_it) {
2303 dev_err(DEV, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
2304 current->comm, current->pid, mdev->ko_count);
2305 request_ping(mdev);
2306 }
2307
2308 return drop_it; /* && (mdev->state == R_PRIMARY) */;
2309}
2310
2311/* The idea of sendpage seems to be to put some kind of reference
2312 * to the page into the skb, and to hand it over to the NIC. In
2313 * this process get_page() gets called.
2314 *
2315 * As soon as the page was really sent over the network put_page()
2316 * gets called by some part of the network layer. [ NIC driver? ]
2317 *
2318 * [ get_page() / put_page() increment/decrement the count. If count
2319 * reaches 0 the page will be freed. ]
2320 *
2321 * This works nicely with pages from FSs.
2322 * But this means that in protocol A we might signal IO completion too early!
2323 *
2324 * In order not to corrupt data during a resync we must make sure
2325 * that we do not reuse our own buffer pages (EEs) to early, therefore
2326 * we have the net_ee list.
2327 *
2328 * XFS seems to have problems, still, it submits pages with page_count == 0!
2329 * As a workaround, we disable sendpage on pages
2330 * with page_count == 0 or PageSlab.
2331 */
2332static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002333 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334{
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002335 int sent = drbd_send(mdev, mdev->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336 kunmap(page);
2337 if (sent == size)
2338 mdev->send_cnt += size>>9;
2339 return sent == size;
2340}
2341
2342static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002343 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002344{
2345 mm_segment_t oldfs = get_fs();
2346 int sent, ok;
2347 int len = size;
2348
2349 /* e.g. XFS meta- & log-data is in slab pages, which have a
2350 * page_count of 0 and/or have PageSlab() set.
2351 * we cannot use send_page for those, as that does get_page();
2352 * put_page(); and would cause either a VM_BUG directly, or
2353 * __page_cache_release a page that would actually still be referenced
2354 * by someone, leading to some obscure delayed Oops somewhere else. */
2355 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002356 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002358 msg_flags |= MSG_NOSIGNAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359 drbd_update_congested(mdev);
2360 set_fs(KERNEL_DS);
2361 do {
2362 sent = mdev->data.socket->ops->sendpage(mdev->data.socket, page,
2363 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002364 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365 if (sent == -EAGAIN) {
2366 if (we_should_drop_the_connection(mdev,
2367 mdev->data.socket))
2368 break;
2369 else
2370 continue;
2371 }
2372 if (sent <= 0) {
2373 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
2374 __func__, (int)size, len, sent);
2375 break;
2376 }
2377 len -= sent;
2378 offset += sent;
2379 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
2380 set_fs(oldfs);
2381 clear_bit(NET_CONGESTED, &mdev->flags);
2382
2383 ok = (len == 0);
2384 if (likely(ok))
2385 mdev->send_cnt += size>>9;
2386 return ok;
2387}
2388
2389static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
2390{
2391 struct bio_vec *bvec;
2392 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002393 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002394 __bio_for_each_segment(bvec, bio, i, 0) {
2395 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002396 bvec->bv_offset, bvec->bv_len,
2397 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398 return 0;
2399 }
2400 return 1;
2401}
2402
2403static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
2404{
2405 struct bio_vec *bvec;
2406 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002407 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002408 __bio_for_each_segment(bvec, bio, i, 0) {
2409 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002410 bvec->bv_offset, bvec->bv_len,
2411 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002412 return 0;
2413 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002414 return 1;
2415}
2416
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002417static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
2418{
2419 struct page *page = e->pages;
2420 unsigned len = e->size;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002421 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002422 page_chain_for_each(page) {
2423 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002424 if (!_drbd_send_page(mdev, page, 0, l,
2425 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002426 return 0;
2427 len -= l;
2428 }
2429 return 1;
2430}
2431
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002432static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
2433{
2434 if (mdev->agreed_pro_version >= 95)
2435 return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
2436 (bi_rw & REQ_UNPLUG ? DP_UNPLUG : 0) |
2437 (bi_rw & REQ_FUA ? DP_FUA : 0) |
2438 (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
2439 (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
2440 else
2441 return bi_rw & (REQ_SYNC | REQ_UNPLUG) ? DP_RW_SYNC : 0;
2442}
2443
Philipp Reisnerb411b362009-09-25 16:07:19 -07002444/* Used to send write requests
2445 * R_PRIMARY -> Peer (P_DATA)
2446 */
2447int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2448{
2449 int ok = 1;
2450 struct p_data p;
2451 unsigned int dp_flags = 0;
2452 void *dgb;
2453 int dgs;
2454
2455 if (!drbd_get_data_sock(mdev))
2456 return 0;
2457
2458 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
2459 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
2460
Philipp Reisnerd5373382010-08-23 15:18:33 +02002461 if (req->size <= DRBD_MAX_SIZE_H80_PACKET) {
Philipp Reisner0b70a132010-08-20 13:36:10 +02002462 p.head.h80.magic = BE_DRBD_MAGIC;
2463 p.head.h80.command = cpu_to_be16(P_DATA);
2464 p.head.h80.length =
2465 cpu_to_be16(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2466 } else {
2467 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2468 p.head.h95.command = cpu_to_be16(P_DATA);
2469 p.head.h95.length =
2470 cpu_to_be32(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2471 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002472
2473 p.sector = cpu_to_be64(req->sector);
2474 p.block_id = (unsigned long)req;
2475 p.seq_num = cpu_to_be32(req->seq_num =
2476 atomic_add_return(1, &mdev->packet_seq));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002478 dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
2479
Philipp Reisnerb411b362009-09-25 16:07:19 -07002480 if (mdev->state.conn >= C_SYNC_SOURCE &&
2481 mdev->state.conn <= C_PAUSED_SYNC_T)
2482 dp_flags |= DP_MAY_SET_IN_SYNC;
2483
2484 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485 set_bit(UNPLUG_REMOTE, &mdev->flags);
2486 ok = (sizeof(p) ==
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002487 drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488 if (ok && dgs) {
2489 dgb = mdev->int_dig_out;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002490 drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, dgb);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002491 ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002492 }
2493 if (ok) {
2494 if (mdev->net_conf->wire_protocol == DRBD_PROT_A)
2495 ok = _drbd_send_bio(mdev, req->master_bio);
2496 else
2497 ok = _drbd_send_zc_bio(mdev, req->master_bio);
2498 }
2499
2500 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02002501
Philipp Reisnerb411b362009-09-25 16:07:19 -07002502 return ok;
2503}
2504
2505/* answer packet, used to send data back for read requests:
2506 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
2507 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
2508 */
2509int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
2510 struct drbd_epoch_entry *e)
2511{
2512 int ok;
2513 struct p_data p;
2514 void *dgb;
2515 int dgs;
2516
2517 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
2518 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
2519
Philipp Reisnerd5373382010-08-23 15:18:33 +02002520 if (e->size <= DRBD_MAX_SIZE_H80_PACKET) {
Philipp Reisner0b70a132010-08-20 13:36:10 +02002521 p.head.h80.magic = BE_DRBD_MAGIC;
2522 p.head.h80.command = cpu_to_be16(cmd);
2523 p.head.h80.length =
2524 cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2525 } else {
2526 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2527 p.head.h95.command = cpu_to_be16(cmd);
2528 p.head.h95.length =
2529 cpu_to_be32(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2530 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002531
2532 p.sector = cpu_to_be64(e->sector);
2533 p.block_id = e->block_id;
2534 /* p.seq_num = 0; No sequence numbers here.. */
2535
2536 /* Only called by our kernel thread.
2537 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
2538 * in response to admin command or module unload.
2539 */
2540 if (!drbd_get_data_sock(mdev))
2541 return 0;
2542
Philipp Reisner0b70a132010-08-20 13:36:10 +02002543 ok = sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544 if (ok && dgs) {
2545 dgb = mdev->int_dig_out;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002546 drbd_csum_ee(mdev, mdev->integrity_w_tfm, e, dgb);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002547 ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002548 }
2549 if (ok)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002550 ok = _drbd_send_zc_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002551
2552 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02002553
Philipp Reisnerb411b362009-09-25 16:07:19 -07002554 return ok;
2555}
2556
2557/*
2558 drbd_send distinguishes two cases:
2559
2560 Packets sent via the data socket "sock"
2561 and packets sent via the meta data socket "msock"
2562
2563 sock msock
2564 -----------------+-------------------------+------------------------------
2565 timeout conf.timeout / 2 conf.timeout / 2
2566 timeout action send a ping via msock Abort communication
2567 and close all sockets
2568*/
2569
2570/*
2571 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
2572 */
2573int drbd_send(struct drbd_conf *mdev, struct socket *sock,
2574 void *buf, size_t size, unsigned msg_flags)
2575{
2576 struct kvec iov;
2577 struct msghdr msg;
2578 int rv, sent = 0;
2579
2580 if (!sock)
2581 return -1000;
2582
2583 /* THINK if (signal_pending) return ... ? */
2584
2585 iov.iov_base = buf;
2586 iov.iov_len = size;
2587
2588 msg.msg_name = NULL;
2589 msg.msg_namelen = 0;
2590 msg.msg_control = NULL;
2591 msg.msg_controllen = 0;
2592 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
2593
2594 if (sock == mdev->data.socket) {
2595 mdev->ko_count = mdev->net_conf->ko_count;
2596 drbd_update_congested(mdev);
2597 }
2598 do {
2599 /* STRANGE
2600 * tcp_sendmsg does _not_ use its size parameter at all ?
2601 *
2602 * -EAGAIN on timeout, -EINTR on signal.
2603 */
2604/* THINK
2605 * do we need to block DRBD_SIG if sock == &meta.socket ??
2606 * otherwise wake_asender() might interrupt some send_*Ack !
2607 */
2608 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
2609 if (rv == -EAGAIN) {
2610 if (we_should_drop_the_connection(mdev, sock))
2611 break;
2612 else
2613 continue;
2614 }
2615 D_ASSERT(rv != 0);
2616 if (rv == -EINTR) {
2617 flush_signals(current);
2618 rv = 0;
2619 }
2620 if (rv < 0)
2621 break;
2622 sent += rv;
2623 iov.iov_base += rv;
2624 iov.iov_len -= rv;
2625 } while (sent < size);
2626
2627 if (sock == mdev->data.socket)
2628 clear_bit(NET_CONGESTED, &mdev->flags);
2629
2630 if (rv <= 0) {
2631 if (rv != -EAGAIN) {
2632 dev_err(DEV, "%s_sendmsg returned %d\n",
2633 sock == mdev->meta.socket ? "msock" : "sock",
2634 rv);
2635 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
2636 } else
2637 drbd_force_state(mdev, NS(conn, C_TIMEOUT));
2638 }
2639
2640 return sent;
2641}
2642
2643static int drbd_open(struct block_device *bdev, fmode_t mode)
2644{
2645 struct drbd_conf *mdev = bdev->bd_disk->private_data;
2646 unsigned long flags;
2647 int rv = 0;
2648
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002649 lock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002650 spin_lock_irqsave(&mdev->req_lock, flags);
2651 /* to have a stable mdev->state.role
2652 * and no race with updating open_cnt */
2653
2654 if (mdev->state.role != R_PRIMARY) {
2655 if (mode & FMODE_WRITE)
2656 rv = -EROFS;
2657 else if (!allow_oos)
2658 rv = -EMEDIUMTYPE;
2659 }
2660
2661 if (!rv)
2662 mdev->open_cnt++;
2663 spin_unlock_irqrestore(&mdev->req_lock, flags);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002664 unlock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665
2666 return rv;
2667}
2668
2669static int drbd_release(struct gendisk *gd, fmode_t mode)
2670{
2671 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002672 lock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673 mdev->open_cnt--;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002674 unlock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002675 return 0;
2676}
2677
2678static void drbd_unplug_fn(struct request_queue *q)
2679{
2680 struct drbd_conf *mdev = q->queuedata;
2681
Philipp Reisnerb411b362009-09-25 16:07:19 -07002682 /* unplug FIRST */
2683 spin_lock_irq(q->queue_lock);
2684 blk_remove_plug(q);
2685 spin_unlock_irq(q->queue_lock);
2686
2687 /* only if connected */
2688 spin_lock_irq(&mdev->req_lock);
2689 if (mdev->state.pdsk >= D_INCONSISTENT && mdev->state.conn >= C_CONNECTED) {
2690 D_ASSERT(mdev->state.role == R_PRIMARY);
2691 if (test_and_clear_bit(UNPLUG_REMOTE, &mdev->flags)) {
2692 /* add to the data.work queue,
2693 * unless already queued.
2694 * XXX this might be a good addition to drbd_queue_work
2695 * anyways, to detect "double queuing" ... */
2696 if (list_empty(&mdev->unplug_work.list))
2697 drbd_queue_work(&mdev->data.work,
2698 &mdev->unplug_work);
2699 }
2700 }
2701 spin_unlock_irq(&mdev->req_lock);
2702
2703 if (mdev->state.disk >= D_INCONSISTENT)
2704 drbd_kick_lo(mdev);
2705}
2706
2707static void drbd_set_defaults(struct drbd_conf *mdev)
2708{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002709 /* This way we get a compile error when sync_conf grows,
2710 and we forgot to initialize it here */
2711 mdev->sync_conf = (struct syncer_conf) {
2712 /* .rate = */ DRBD_RATE_DEF,
2713 /* .after = */ DRBD_AFTER_DEF,
2714 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002715 /* .verify_alg = */ {}, 0,
2716 /* .cpu_mask = */ {}, 0,
2717 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02002718 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02002719 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
2720 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
2721 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
2722 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002723 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
2724 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002725 };
2726
2727 /* Have to use that way, because the layout differs between
2728 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002729 mdev->state = (union drbd_state) {
2730 { .role = R_SECONDARY,
2731 .peer = R_UNKNOWN,
2732 .conn = C_STANDALONE,
2733 .disk = D_DISKLESS,
2734 .pdsk = D_UNKNOWN,
2735 .susp = 0
2736 } };
2737}
2738
2739void drbd_init_set_defaults(struct drbd_conf *mdev)
2740{
2741 /* the memset(,0,) did most of this.
2742 * note: only assignments, no allocation in here */
2743
2744 drbd_set_defaults(mdev);
2745
2746 /* for now, we do NOT yet support it,
2747 * even though we start some framework
2748 * to eventually support barriers */
2749 set_bit(NO_BARRIER_SUPP, &mdev->flags);
2750
2751 atomic_set(&mdev->ap_bio_cnt, 0);
2752 atomic_set(&mdev->ap_pending_cnt, 0);
2753 atomic_set(&mdev->rs_pending_cnt, 0);
2754 atomic_set(&mdev->unacked_cnt, 0);
2755 atomic_set(&mdev->local_cnt, 0);
2756 atomic_set(&mdev->net_cnt, 0);
2757 atomic_set(&mdev->packet_seq, 0);
2758 atomic_set(&mdev->pp_in_use, 0);
Lars Ellenberg435f0742010-09-06 12:30:25 +02002759 atomic_set(&mdev->pp_in_use_by_net, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02002760 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002761 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002762
2763 mutex_init(&mdev->md_io_mutex);
2764 mutex_init(&mdev->data.mutex);
2765 mutex_init(&mdev->meta.mutex);
2766 sema_init(&mdev->data.work.s, 0);
2767 sema_init(&mdev->meta.work.s, 0);
2768 mutex_init(&mdev->state_mutex);
2769
2770 spin_lock_init(&mdev->data.work.q_lock);
2771 spin_lock_init(&mdev->meta.work.q_lock);
2772
2773 spin_lock_init(&mdev->al_lock);
2774 spin_lock_init(&mdev->req_lock);
2775 spin_lock_init(&mdev->peer_seq_lock);
2776 spin_lock_init(&mdev->epoch_lock);
2777
2778 INIT_LIST_HEAD(&mdev->active_ee);
2779 INIT_LIST_HEAD(&mdev->sync_ee);
2780 INIT_LIST_HEAD(&mdev->done_ee);
2781 INIT_LIST_HEAD(&mdev->read_ee);
2782 INIT_LIST_HEAD(&mdev->net_ee);
2783 INIT_LIST_HEAD(&mdev->resync_reads);
2784 INIT_LIST_HEAD(&mdev->data.work.q);
2785 INIT_LIST_HEAD(&mdev->meta.work.q);
2786 INIT_LIST_HEAD(&mdev->resync_work.list);
2787 INIT_LIST_HEAD(&mdev->unplug_work.list);
2788 INIT_LIST_HEAD(&mdev->md_sync_work.list);
2789 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02002790
Philipp Reisnerb411b362009-09-25 16:07:19 -07002791 mdev->resync_work.cb = w_resync_inactive;
2792 mdev->unplug_work.cb = w_send_write_hint;
2793 mdev->md_sync_work.cb = w_md_sync;
2794 mdev->bm_io_work.w.cb = w_bitmap_io;
2795 init_timer(&mdev->resync_timer);
2796 init_timer(&mdev->md_sync_timer);
2797 mdev->resync_timer.function = resync_timer_fn;
2798 mdev->resync_timer.data = (unsigned long) mdev;
2799 mdev->md_sync_timer.function = md_sync_timer_fn;
2800 mdev->md_sync_timer.data = (unsigned long) mdev;
2801
2802 init_waitqueue_head(&mdev->misc_wait);
2803 init_waitqueue_head(&mdev->state_wait);
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02002804 init_waitqueue_head(&mdev->net_cnt_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805 init_waitqueue_head(&mdev->ee_wait);
2806 init_waitqueue_head(&mdev->al_wait);
2807 init_waitqueue_head(&mdev->seq_wait);
2808
2809 drbd_thread_init(mdev, &mdev->receiver, drbdd_init);
2810 drbd_thread_init(mdev, &mdev->worker, drbd_worker);
2811 drbd_thread_init(mdev, &mdev->asender, drbd_asender);
2812
2813 mdev->agreed_pro_version = PRO_VERSION_MAX;
2814 mdev->write_ordering = WO_bio_barrier;
2815 mdev->resync_wenr = LC_FREE;
2816}
2817
2818void drbd_mdev_cleanup(struct drbd_conf *mdev)
2819{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002820 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002821 if (mdev->receiver.t_state != None)
2822 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
2823 mdev->receiver.t_state);
2824
2825 /* no need to lock it, I'm the only thread alive */
2826 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
2827 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
2828 mdev->al_writ_cnt =
2829 mdev->bm_writ_cnt =
2830 mdev->read_cnt =
2831 mdev->recv_cnt =
2832 mdev->send_cnt =
2833 mdev->writ_cnt =
2834 mdev->p_size =
2835 mdev->rs_start =
2836 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002837 mdev->rs_failed = 0;
2838 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002839 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002840 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2841 mdev->rs_mark_left[i] = 0;
2842 mdev->rs_mark_time[i] = 0;
2843 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002844 D_ASSERT(mdev->net_conf == NULL);
2845
2846 drbd_set_my_capacity(mdev, 0);
2847 if (mdev->bitmap) {
2848 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01002849 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002850 drbd_bm_cleanup(mdev);
2851 }
2852
2853 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02002854 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002855
2856 /*
2857 * currently we drbd_init_ee only on module load, so
2858 * we may do drbd_release_ee only on module unload!
2859 */
2860 D_ASSERT(list_empty(&mdev->active_ee));
2861 D_ASSERT(list_empty(&mdev->sync_ee));
2862 D_ASSERT(list_empty(&mdev->done_ee));
2863 D_ASSERT(list_empty(&mdev->read_ee));
2864 D_ASSERT(list_empty(&mdev->net_ee));
2865 D_ASSERT(list_empty(&mdev->resync_reads));
2866 D_ASSERT(list_empty(&mdev->data.work.q));
2867 D_ASSERT(list_empty(&mdev->meta.work.q));
2868 D_ASSERT(list_empty(&mdev->resync_work.list));
2869 D_ASSERT(list_empty(&mdev->unplug_work.list));
2870
2871}
2872
2873
2874static void drbd_destroy_mempools(void)
2875{
2876 struct page *page;
2877
2878 while (drbd_pp_pool) {
2879 page = drbd_pp_pool;
2880 drbd_pp_pool = (struct page *)page_private(page);
2881 __free_page(page);
2882 drbd_pp_vacant--;
2883 }
2884
2885 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2886
2887 if (drbd_ee_mempool)
2888 mempool_destroy(drbd_ee_mempool);
2889 if (drbd_request_mempool)
2890 mempool_destroy(drbd_request_mempool);
2891 if (drbd_ee_cache)
2892 kmem_cache_destroy(drbd_ee_cache);
2893 if (drbd_request_cache)
2894 kmem_cache_destroy(drbd_request_cache);
2895 if (drbd_bm_ext_cache)
2896 kmem_cache_destroy(drbd_bm_ext_cache);
2897 if (drbd_al_ext_cache)
2898 kmem_cache_destroy(drbd_al_ext_cache);
2899
2900 drbd_ee_mempool = NULL;
2901 drbd_request_mempool = NULL;
2902 drbd_ee_cache = NULL;
2903 drbd_request_cache = NULL;
2904 drbd_bm_ext_cache = NULL;
2905 drbd_al_ext_cache = NULL;
2906
2907 return;
2908}
2909
2910static int drbd_create_mempools(void)
2911{
2912 struct page *page;
2913 const int number = (DRBD_MAX_SEGMENT_SIZE/PAGE_SIZE) * minor_count;
2914 int i;
2915
2916 /* prepare our caches and mempools */
2917 drbd_request_mempool = NULL;
2918 drbd_ee_cache = NULL;
2919 drbd_request_cache = NULL;
2920 drbd_bm_ext_cache = NULL;
2921 drbd_al_ext_cache = NULL;
2922 drbd_pp_pool = NULL;
2923
2924 /* caches */
2925 drbd_request_cache = kmem_cache_create(
2926 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2927 if (drbd_request_cache == NULL)
2928 goto Enomem;
2929
2930 drbd_ee_cache = kmem_cache_create(
2931 "drbd_ee", sizeof(struct drbd_epoch_entry), 0, 0, NULL);
2932 if (drbd_ee_cache == NULL)
2933 goto Enomem;
2934
2935 drbd_bm_ext_cache = kmem_cache_create(
2936 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2937 if (drbd_bm_ext_cache == NULL)
2938 goto Enomem;
2939
2940 drbd_al_ext_cache = kmem_cache_create(
2941 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2942 if (drbd_al_ext_cache == NULL)
2943 goto Enomem;
2944
2945 /* mempools */
2946 drbd_request_mempool = mempool_create(number,
2947 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2948 if (drbd_request_mempool == NULL)
2949 goto Enomem;
2950
2951 drbd_ee_mempool = mempool_create(number,
2952 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
2953 if (drbd_request_mempool == NULL)
2954 goto Enomem;
2955
2956 /* drbd's page pool */
2957 spin_lock_init(&drbd_pp_lock);
2958
2959 for (i = 0; i < number; i++) {
2960 page = alloc_page(GFP_HIGHUSER);
2961 if (!page)
2962 goto Enomem;
2963 set_page_private(page, (unsigned long)drbd_pp_pool);
2964 drbd_pp_pool = page;
2965 }
2966 drbd_pp_vacant = number;
2967
2968 return 0;
2969
2970Enomem:
2971 drbd_destroy_mempools(); /* in case we allocated some */
2972 return -ENOMEM;
2973}
2974
2975static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2976 void *unused)
2977{
2978 /* just so we have it. you never know what interesting things we
2979 * might want to do here some day...
2980 */
2981
2982 return NOTIFY_DONE;
2983}
2984
2985static struct notifier_block drbd_notifier = {
2986 .notifier_call = drbd_notify_sys,
2987};
2988
2989static void drbd_release_ee_lists(struct drbd_conf *mdev)
2990{
2991 int rr;
2992
2993 rr = drbd_release_ee(mdev, &mdev->active_ee);
2994 if (rr)
2995 dev_err(DEV, "%d EEs in active list found!\n", rr);
2996
2997 rr = drbd_release_ee(mdev, &mdev->sync_ee);
2998 if (rr)
2999 dev_err(DEV, "%d EEs in sync list found!\n", rr);
3000
3001 rr = drbd_release_ee(mdev, &mdev->read_ee);
3002 if (rr)
3003 dev_err(DEV, "%d EEs in read list found!\n", rr);
3004
3005 rr = drbd_release_ee(mdev, &mdev->done_ee);
3006 if (rr)
3007 dev_err(DEV, "%d EEs in done list found!\n", rr);
3008
3009 rr = drbd_release_ee(mdev, &mdev->net_ee);
3010 if (rr)
3011 dev_err(DEV, "%d EEs in net list found!\n", rr);
3012}
3013
3014/* caution. no locking.
3015 * currently only used from module cleanup code. */
3016static void drbd_delete_device(unsigned int minor)
3017{
3018 struct drbd_conf *mdev = minor_to_mdev(minor);
3019
3020 if (!mdev)
3021 return;
3022
3023 /* paranoia asserts */
3024 if (mdev->open_cnt != 0)
3025 dev_err(DEV, "open_cnt = %d in %s:%u", mdev->open_cnt,
3026 __FILE__ , __LINE__);
3027
3028 ERR_IF (!list_empty(&mdev->data.work.q)) {
3029 struct list_head *lp;
3030 list_for_each(lp, &mdev->data.work.q) {
3031 dev_err(DEV, "lp = %p\n", lp);
3032 }
3033 };
3034 /* end paranoia asserts */
3035
3036 del_gendisk(mdev->vdisk);
3037
3038 /* cleanup stuff that may have been allocated during
3039 * device (re-)configuration or state changes */
3040
3041 if (mdev->this_bdev)
3042 bdput(mdev->this_bdev);
3043
3044 drbd_free_resources(mdev);
3045
3046 drbd_release_ee_lists(mdev);
3047
3048 /* should be free'd on disconnect? */
3049 kfree(mdev->ee_hash);
3050 /*
3051 mdev->ee_hash_s = 0;
3052 mdev->ee_hash = NULL;
3053 */
3054
3055 lc_destroy(mdev->act_log);
3056 lc_destroy(mdev->resync);
3057
3058 kfree(mdev->p_uuid);
3059 /* mdev->p_uuid = NULL; */
3060
3061 kfree(mdev->int_dig_out);
3062 kfree(mdev->int_dig_in);
3063 kfree(mdev->int_dig_vv);
3064
3065 /* cleanup the rest that has been
3066 * allocated from drbd_new_device
3067 * and actually free the mdev itself */
3068 drbd_free_mdev(mdev);
3069}
3070
3071static void drbd_cleanup(void)
3072{
3073 unsigned int i;
3074
3075 unregister_reboot_notifier(&drbd_notifier);
3076
3077 drbd_nl_cleanup();
3078
3079 if (minor_table) {
3080 if (drbd_proc)
3081 remove_proc_entry("drbd", NULL);
3082 i = minor_count;
3083 while (i--)
3084 drbd_delete_device(i);
3085 drbd_destroy_mempools();
3086 }
3087
3088 kfree(minor_table);
3089
3090 unregister_blkdev(DRBD_MAJOR, "drbd");
3091
3092 printk(KERN_INFO "drbd: module cleanup done.\n");
3093}
3094
3095/**
3096 * drbd_congested() - Callback for pdflush
3097 * @congested_data: User data
3098 * @bdi_bits: Bits pdflush is currently interested in
3099 *
3100 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
3101 */
3102static int drbd_congested(void *congested_data, int bdi_bits)
3103{
3104 struct drbd_conf *mdev = congested_data;
3105 struct request_queue *q;
3106 char reason = '-';
3107 int r = 0;
3108
3109 if (!__inc_ap_bio_cond(mdev)) {
3110 /* DRBD has frozen IO */
3111 r = bdi_bits;
3112 reason = 'd';
3113 goto out;
3114 }
3115
3116 if (get_ldev(mdev)) {
3117 q = bdev_get_queue(mdev->ldev->backing_bdev);
3118 r = bdi_congested(&q->backing_dev_info, bdi_bits);
3119 put_ldev(mdev);
3120 if (r)
3121 reason = 'b';
3122 }
3123
3124 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->flags)) {
3125 r |= (1 << BDI_async_congested);
3126 reason = reason == 'b' ? 'a' : 'n';
3127 }
3128
3129out:
3130 mdev->congestion_reason = reason;
3131 return r;
3132}
3133
3134struct drbd_conf *drbd_new_device(unsigned int minor)
3135{
3136 struct drbd_conf *mdev;
3137 struct gendisk *disk;
3138 struct request_queue *q;
3139
3140 /* GFP_KERNEL, we are outside of all write-out paths */
3141 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
3142 if (!mdev)
3143 return NULL;
3144 if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL))
3145 goto out_no_cpumask;
3146
3147 mdev->minor = minor;
3148
3149 drbd_init_set_defaults(mdev);
3150
3151 q = blk_alloc_queue(GFP_KERNEL);
3152 if (!q)
3153 goto out_no_q;
3154 mdev->rq_queue = q;
3155 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003156
3157 disk = alloc_disk(1);
3158 if (!disk)
3159 goto out_no_disk;
3160 mdev->vdisk = disk;
3161
3162 set_disk_ro(disk, TRUE);
3163
3164 disk->queue = q;
3165 disk->major = DRBD_MAJOR;
3166 disk->first_minor = minor;
3167 disk->fops = &drbd_ops;
3168 sprintf(disk->disk_name, "drbd%d", minor);
3169 disk->private_data = mdev;
3170
3171 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
3172 /* we have no partitions. we contain only ourselves. */
3173 mdev->this_bdev->bd_contains = mdev->this_bdev;
3174
3175 q->backing_dev_info.congested_fn = drbd_congested;
3176 q->backing_dev_info.congested_data = mdev;
3177
3178 blk_queue_make_request(q, drbd_make_request_26);
Lars Ellenberg98ec2862010-01-21 19:33:14 +01003179 blk_queue_max_segment_size(q, DRBD_MAX_SEGMENT_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003180 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
3181 blk_queue_merge_bvec(q, drbd_merge_bvec);
3182 q->queue_lock = &mdev->req_lock; /* needed since we use */
3183 /* plugging on a queue, that actually has no requests! */
3184 q->unplug_fn = drbd_unplug_fn;
3185
3186 mdev->md_io_page = alloc_page(GFP_KERNEL);
3187 if (!mdev->md_io_page)
3188 goto out_no_io_page;
3189
3190 if (drbd_bm_init(mdev))
3191 goto out_no_bitmap;
3192 /* no need to lock access, we are still initializing this minor device. */
3193 if (!tl_init(mdev))
3194 goto out_no_tl;
3195
3196 mdev->app_reads_hash = kzalloc(APP_R_HSIZE*sizeof(void *), GFP_KERNEL);
3197 if (!mdev->app_reads_hash)
3198 goto out_no_app_reads;
3199
3200 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
3201 if (!mdev->current_epoch)
3202 goto out_no_epoch;
3203
3204 INIT_LIST_HEAD(&mdev->current_epoch->list);
3205 mdev->epochs = 1;
3206
3207 return mdev;
3208
3209/* out_whatever_else:
3210 kfree(mdev->current_epoch); */
3211out_no_epoch:
3212 kfree(mdev->app_reads_hash);
3213out_no_app_reads:
3214 tl_cleanup(mdev);
3215out_no_tl:
3216 drbd_bm_cleanup(mdev);
3217out_no_bitmap:
3218 __free_page(mdev->md_io_page);
3219out_no_io_page:
3220 put_disk(disk);
3221out_no_disk:
3222 blk_cleanup_queue(q);
3223out_no_q:
3224 free_cpumask_var(mdev->cpu_mask);
3225out_no_cpumask:
3226 kfree(mdev);
3227 return NULL;
3228}
3229
3230/* counterpart of drbd_new_device.
3231 * last part of drbd_delete_device. */
3232void drbd_free_mdev(struct drbd_conf *mdev)
3233{
3234 kfree(mdev->current_epoch);
3235 kfree(mdev->app_reads_hash);
3236 tl_cleanup(mdev);
3237 if (mdev->bitmap) /* should no longer be there. */
3238 drbd_bm_cleanup(mdev);
3239 __free_page(mdev->md_io_page);
3240 put_disk(mdev->vdisk);
3241 blk_cleanup_queue(mdev->rq_queue);
3242 free_cpumask_var(mdev->cpu_mask);
3243 kfree(mdev);
3244}
3245
3246
3247int __init drbd_init(void)
3248{
3249 int err;
3250
3251 if (sizeof(struct p_handshake) != 80) {
3252 printk(KERN_ERR
3253 "drbd: never change the size or layout "
3254 "of the HandShake packet.\n");
3255 return -EINVAL;
3256 }
3257
3258 if (1 > minor_count || minor_count > 255) {
3259 printk(KERN_ERR
3260 "drbd: invalid minor_count (%d)\n", minor_count);
3261#ifdef MODULE
3262 return -EINVAL;
3263#else
3264 minor_count = 8;
3265#endif
3266 }
3267
3268 err = drbd_nl_init();
3269 if (err)
3270 return err;
3271
3272 err = register_blkdev(DRBD_MAJOR, "drbd");
3273 if (err) {
3274 printk(KERN_ERR
3275 "drbd: unable to register block device major %d\n",
3276 DRBD_MAJOR);
3277 return err;
3278 }
3279
3280 register_reboot_notifier(&drbd_notifier);
3281
3282 /*
3283 * allocate all necessary structs
3284 */
3285 err = -ENOMEM;
3286
3287 init_waitqueue_head(&drbd_pp_wait);
3288
3289 drbd_proc = NULL; /* play safe for drbd_cleanup */
3290 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
3291 GFP_KERNEL);
3292 if (!minor_table)
3293 goto Enomem;
3294
3295 err = drbd_create_mempools();
3296 if (err)
3297 goto Enomem;
3298
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01003299 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003300 if (!drbd_proc) {
3301 printk(KERN_ERR "drbd: unable to register proc file\n");
3302 goto Enomem;
3303 }
3304
3305 rwlock_init(&global_state_lock);
3306
3307 printk(KERN_INFO "drbd: initialized. "
3308 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
3309 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
3310 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
3311 printk(KERN_INFO "drbd: registered as block device major %d\n",
3312 DRBD_MAJOR);
3313 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
3314
3315 return 0; /* Success! */
3316
3317Enomem:
3318 drbd_cleanup();
3319 if (err == -ENOMEM)
3320 /* currently always the case */
3321 printk(KERN_ERR "drbd: ran out of memory\n");
3322 else
3323 printk(KERN_ERR "drbd: initialization failure\n");
3324 return err;
3325}
3326
3327void drbd_free_bc(struct drbd_backing_dev *ldev)
3328{
3329 if (ldev == NULL)
3330 return;
3331
3332 bd_release(ldev->backing_bdev);
3333 bd_release(ldev->md_bdev);
3334
3335 fput(ldev->lo_file);
3336 fput(ldev->md_file);
3337
3338 kfree(ldev);
3339}
3340
3341void drbd_free_sock(struct drbd_conf *mdev)
3342{
3343 if (mdev->data.socket) {
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003344 mutex_lock(&mdev->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 kernel_sock_shutdown(mdev->data.socket, SHUT_RDWR);
3346 sock_release(mdev->data.socket);
3347 mdev->data.socket = NULL;
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003348 mutex_unlock(&mdev->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003349 }
3350 if (mdev->meta.socket) {
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003351 mutex_lock(&mdev->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003352 kernel_sock_shutdown(mdev->meta.socket, SHUT_RDWR);
3353 sock_release(mdev->meta.socket);
3354 mdev->meta.socket = NULL;
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003355 mutex_unlock(&mdev->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003356 }
3357}
3358
3359
3360void drbd_free_resources(struct drbd_conf *mdev)
3361{
3362 crypto_free_hash(mdev->csums_tfm);
3363 mdev->csums_tfm = NULL;
3364 crypto_free_hash(mdev->verify_tfm);
3365 mdev->verify_tfm = NULL;
3366 crypto_free_hash(mdev->cram_hmac_tfm);
3367 mdev->cram_hmac_tfm = NULL;
3368 crypto_free_hash(mdev->integrity_w_tfm);
3369 mdev->integrity_w_tfm = NULL;
3370 crypto_free_hash(mdev->integrity_r_tfm);
3371 mdev->integrity_r_tfm = NULL;
3372
3373 drbd_free_sock(mdev);
3374
3375 __no_warn(local,
3376 drbd_free_bc(mdev->ldev);
3377 mdev->ldev = NULL;);
3378}
3379
3380/* meta data management */
3381
3382struct meta_data_on_disk {
3383 u64 la_size; /* last agreed size. */
3384 u64 uuid[UI_SIZE]; /* UUIDs. */
3385 u64 device_uuid;
3386 u64 reserved_u64_1;
3387 u32 flags; /* MDF */
3388 u32 magic;
3389 u32 md_size_sect;
3390 u32 al_offset; /* offset to this block */
3391 u32 al_nr_extents; /* important for restoring the AL */
3392 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
3393 u32 bm_offset; /* offset to the bitmap, from here */
3394 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
3395 u32 reserved_u32[4];
3396
3397} __packed;
3398
3399/**
3400 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
3401 * @mdev: DRBD device.
3402 */
3403void drbd_md_sync(struct drbd_conf *mdev)
3404{
3405 struct meta_data_on_disk *buffer;
3406 sector_t sector;
3407 int i;
3408
Lars Ellenbergee15b032010-09-03 10:00:09 +02003409 del_timer(&mdev->md_sync_timer);
3410 /* timer may be rearmed by drbd_md_mark_dirty() now. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003411 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
3412 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003413
3414 /* We use here D_FAILED and not D_ATTACHING because we try to write
3415 * metadata even if we detach due to a disk failure! */
3416 if (!get_ldev_if_state(mdev, D_FAILED))
3417 return;
3418
Philipp Reisnerb411b362009-09-25 16:07:19 -07003419 mutex_lock(&mdev->md_io_mutex);
3420 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
3421 memset(buffer, 0, 512);
3422
3423 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
3424 for (i = UI_CURRENT; i < UI_SIZE; i++)
3425 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
3426 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
3427 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
3428
3429 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
3430 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
3431 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
3432 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
3433 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
3434
3435 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
3436
3437 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
3438 sector = mdev->ldev->md.md_offset;
3439
Lars Ellenberg3f3a9b82010-09-01 15:12:12 +02003440 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441 /* this was a try anyways ... */
3442 dev_err(DEV, "meta data update failed!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003443 drbd_chk_io_error(mdev, 1, TRUE);
3444 }
3445
3446 /* Update mdev->ldev->md.la_size_sect,
3447 * since we updated it on metadata. */
3448 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
3449
3450 mutex_unlock(&mdev->md_io_mutex);
3451 put_ldev(mdev);
3452}
3453
3454/**
3455 * drbd_md_read() - Reads in the meta data super block
3456 * @mdev: DRBD device.
3457 * @bdev: Device from which the meta data should be read in.
3458 *
3459 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_codes in case
3460 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
3461 */
3462int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3463{
3464 struct meta_data_on_disk *buffer;
3465 int i, rv = NO_ERROR;
3466
3467 if (!get_ldev_if_state(mdev, D_ATTACHING))
3468 return ERR_IO_MD_DISK;
3469
Philipp Reisnerb411b362009-09-25 16:07:19 -07003470 mutex_lock(&mdev->md_io_mutex);
3471 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
3472
3473 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
3474 /* NOTE: cant do normal error processing here as this is
3475 called BEFORE disk is attached */
3476 dev_err(DEV, "Error while reading metadata.\n");
3477 rv = ERR_IO_MD_DISK;
3478 goto err;
3479 }
3480
3481 if (be32_to_cpu(buffer->magic) != DRBD_MD_MAGIC) {
3482 dev_err(DEV, "Error while reading metadata, magic not found.\n");
3483 rv = ERR_MD_INVALID;
3484 goto err;
3485 }
3486 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
3487 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
3488 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
3489 rv = ERR_MD_INVALID;
3490 goto err;
3491 }
3492 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
3493 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
3494 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
3495 rv = ERR_MD_INVALID;
3496 goto err;
3497 }
3498 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
3499 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
3500 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
3501 rv = ERR_MD_INVALID;
3502 goto err;
3503 }
3504
3505 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
3506 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3507 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
3508 rv = ERR_MD_INVALID;
3509 goto err;
3510 }
3511
3512 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
3513 for (i = UI_CURRENT; i < UI_SIZE; i++)
3514 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3515 bdev->md.flags = be32_to_cpu(buffer->flags);
3516 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
3517 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3518
3519 if (mdev->sync_conf.al_extents < 7)
3520 mdev->sync_conf.al_extents = 127;
3521
3522 err:
3523 mutex_unlock(&mdev->md_io_mutex);
3524 put_ldev(mdev);
3525
3526 return rv;
3527}
3528
3529/**
3530 * drbd_md_mark_dirty() - Mark meta data super block as dirty
3531 * @mdev: DRBD device.
3532 *
3533 * Call this function if you change anything that should be written to
3534 * the meta-data super block. This function sets MD_DIRTY, and starts a
3535 * timer that ensures that within five seconds you have to call drbd_md_sync().
3536 */
Lars Ellenbergee15b032010-09-03 10:00:09 +02003537#ifdef DRBD_DEBUG_MD_SYNC
3538void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
3539{
3540 if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
3541 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
3542 mdev->last_md_mark_dirty.line = line;
3543 mdev->last_md_mark_dirty.func = func;
3544 }
3545}
3546#else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547void drbd_md_mark_dirty(struct drbd_conf *mdev)
3548{
Lars Ellenbergee15b032010-09-03 10:00:09 +02003549 if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
3550 mod_timer(&mdev->md_sync_timer, jiffies + HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551}
Lars Ellenbergee15b032010-09-03 10:00:09 +02003552#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07003553
3554static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
3555{
3556 int i;
3557
Jens Axboe6a0afdf2009-10-01 09:04:14 +02003558 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560}
3561
3562void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
3563{
3564 if (idx == UI_CURRENT) {
3565 if (mdev->state.role == R_PRIMARY)
3566 val |= 1;
3567 else
3568 val &= ~((u64)1);
3569
3570 drbd_set_ed_uuid(mdev, val);
3571 }
3572
3573 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574 drbd_md_mark_dirty(mdev);
3575}
3576
3577
3578void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
3579{
3580 if (mdev->ldev->md.uuid[idx]) {
3581 drbd_uuid_move_history(mdev);
3582 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583 }
3584 _drbd_uuid_set(mdev, idx, val);
3585}
3586
3587/**
3588 * drbd_uuid_new_current() - Creates a new current UUID
3589 * @mdev: DRBD device.
3590 *
3591 * Creates a new current UUID, and rotates the old current UUID into
3592 * the bitmap slot. Causes an incremental resync upon next connect.
3593 */
3594void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
3595{
3596 u64 val;
3597
3598 dev_info(DEV, "Creating new current UUID\n");
3599 D_ASSERT(mdev->ldev->md.uuid[UI_BITMAP] == 0);
3600 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003601
3602 get_random_bytes(&val, sizeof(u64));
3603 _drbd_uuid_set(mdev, UI_CURRENT, val);
3604}
3605
3606void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
3607{
3608 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
3609 return;
3610
3611 if (val == 0) {
3612 drbd_uuid_move_history(mdev);
3613 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
3614 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003615 } else {
3616 if (mdev->ldev->md.uuid[UI_BITMAP])
3617 dev_warn(DEV, "bm UUID already set");
3618
3619 mdev->ldev->md.uuid[UI_BITMAP] = val;
3620 mdev->ldev->md.uuid[UI_BITMAP] &= ~((u64)1);
3621
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 }
3623 drbd_md_mark_dirty(mdev);
3624}
3625
3626/**
3627 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3628 * @mdev: DRBD device.
3629 *
3630 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3631 */
3632int drbd_bmio_set_n_write(struct drbd_conf *mdev)
3633{
3634 int rv = -EIO;
3635
3636 if (get_ldev_if_state(mdev, D_ATTACHING)) {
3637 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
3638 drbd_md_sync(mdev);
3639 drbd_bm_set_all(mdev);
3640
3641 rv = drbd_bm_write(mdev);
3642
3643 if (!rv) {
3644 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
3645 drbd_md_sync(mdev);
3646 }
3647
3648 put_ldev(mdev);
3649 }
3650
3651 return rv;
3652}
3653
3654/**
3655 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3656 * @mdev: DRBD device.
3657 *
3658 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3659 */
3660int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
3661{
3662 int rv = -EIO;
3663
Philipp Reisner07782862010-08-31 12:00:50 +02003664 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665 if (get_ldev_if_state(mdev, D_ATTACHING)) {
3666 drbd_bm_clear_all(mdev);
3667 rv = drbd_bm_write(mdev);
3668 put_ldev(mdev);
3669 }
3670
3671 return rv;
3672}
3673
3674static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
3675{
3676 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
3677 int rv;
3678
3679 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
3680
3681 drbd_bm_lock(mdev, work->why);
3682 rv = work->io_fn(mdev);
3683 drbd_bm_unlock(mdev);
3684
3685 clear_bit(BITMAP_IO, &mdev->flags);
3686 wake_up(&mdev->misc_wait);
3687
3688 if (work->done)
3689 work->done(mdev, rv);
3690
3691 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
3692 work->why = NULL;
3693
3694 return 1;
3695}
3696
3697/**
3698 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
3699 * @mdev: DRBD device.
3700 * @io_fn: IO callback to be called when bitmap IO is possible
3701 * @done: callback to be called after the bitmap IO was performed
3702 * @why: Descriptive text of the reason for doing the IO
3703 *
3704 * While IO on the bitmap happens we freeze application IO thus we ensure
3705 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3706 * called from worker context. It MUST NOT be used while a previous such
3707 * work is still pending!
3708 */
3709void drbd_queue_bitmap_io(struct drbd_conf *mdev,
3710 int (*io_fn)(struct drbd_conf *),
3711 void (*done)(struct drbd_conf *, int),
3712 char *why)
3713{
3714 D_ASSERT(current == mdev->worker.task);
3715
3716 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
3717 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
3718 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
3719 if (mdev->bm_io_work.why)
3720 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
3721 why, mdev->bm_io_work.why);
3722
3723 mdev->bm_io_work.io_fn = io_fn;
3724 mdev->bm_io_work.done = done;
3725 mdev->bm_io_work.why = why;
3726
3727 set_bit(BITMAP_IO, &mdev->flags);
3728 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
3729 if (list_empty(&mdev->bm_io_work.w.list)) {
3730 set_bit(BITMAP_IO_QUEUED, &mdev->flags);
3731 drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
3732 } else
3733 dev_err(DEV, "FIXME avoided double queuing bm_io_work\n");
3734 }
3735}
3736
3737/**
3738 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
3739 * @mdev: DRBD device.
3740 * @io_fn: IO callback to be called when bitmap IO is possible
3741 * @why: Descriptive text of the reason for doing the IO
3742 *
3743 * freezes application IO while that the actual IO operations runs. This
3744 * functions MAY NOT be called from worker context.
3745 */
3746int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *), char *why)
3747{
3748 int rv;
3749
3750 D_ASSERT(current != mdev->worker.task);
3751
3752 drbd_suspend_io(mdev);
3753
3754 drbd_bm_lock(mdev, why);
3755 rv = io_fn(mdev);
3756 drbd_bm_unlock(mdev);
3757
3758 drbd_resume_io(mdev);
3759
3760 return rv;
3761}
3762
3763void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3764{
3765 if ((mdev->ldev->md.flags & flag) != flag) {
3766 drbd_md_mark_dirty(mdev);
3767 mdev->ldev->md.flags |= flag;
3768 }
3769}
3770
3771void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3772{
3773 if ((mdev->ldev->md.flags & flag) != 0) {
3774 drbd_md_mark_dirty(mdev);
3775 mdev->ldev->md.flags &= ~flag;
3776 }
3777}
3778int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3779{
3780 return (bdev->md.flags & flag) != 0;
3781}
3782
3783static void md_sync_timer_fn(unsigned long data)
3784{
3785 struct drbd_conf *mdev = (struct drbd_conf *) data;
3786
3787 drbd_queue_work_front(&mdev->data.work, &mdev->md_sync_work);
3788}
3789
3790static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
3791{
3792 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
Lars Ellenbergee15b032010-09-03 10:00:09 +02003793#ifdef DEBUG
3794 dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
3795 mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
3796#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003798 return 1;
3799}
3800
3801#ifdef CONFIG_DRBD_FAULT_INJECTION
3802/* Fault insertion support including random number generator shamelessly
3803 * stolen from kernel/rcutorture.c */
3804struct fault_random_state {
3805 unsigned long state;
3806 unsigned long count;
3807};
3808
3809#define FAULT_RANDOM_MULT 39916801 /* prime */
3810#define FAULT_RANDOM_ADD 479001701 /* prime */
3811#define FAULT_RANDOM_REFRESH 10000
3812
3813/*
3814 * Crude but fast random-number generator. Uses a linear congruential
3815 * generator, with occasional help from get_random_bytes().
3816 */
3817static unsigned long
3818_drbd_fault_random(struct fault_random_state *rsp)
3819{
3820 long refresh;
3821
Roel Kluin49829ea2009-12-15 22:55:44 +01003822 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003823 get_random_bytes(&refresh, sizeof(refresh));
3824 rsp->state += refresh;
3825 rsp->count = FAULT_RANDOM_REFRESH;
3826 }
3827 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3828 return swahw32(rsp->state);
3829}
3830
3831static char *
3832_drbd_fault_str(unsigned int type) {
3833 static char *_faults[] = {
3834 [DRBD_FAULT_MD_WR] = "Meta-data write",
3835 [DRBD_FAULT_MD_RD] = "Meta-data read",
3836 [DRBD_FAULT_RS_WR] = "Resync write",
3837 [DRBD_FAULT_RS_RD] = "Resync read",
3838 [DRBD_FAULT_DT_WR] = "Data write",
3839 [DRBD_FAULT_DT_RD] = "Data read",
3840 [DRBD_FAULT_DT_RA] = "Data read ahead",
3841 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003842 [DRBD_FAULT_AL_EE] = "EE allocation",
3843 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003844 };
3845
3846 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3847}
3848
3849unsigned int
3850_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3851{
3852 static struct fault_random_state rrs = {0, 0};
3853
3854 unsigned int ret = (
3855 (fault_devs == 0 ||
3856 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3857 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3858
3859 if (ret) {
3860 fault_count++;
3861
Lars Ellenberg73835062010-05-27 11:51:56 +02003862 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003863 dev_warn(DEV, "***Simulating %s failure\n",
3864 _drbd_fault_str(type));
3865 }
3866
3867 return ret;
3868}
3869#endif
3870
3871const char *drbd_buildtag(void)
3872{
3873 /* DRBD built from external sources has here a reference to the
3874 git hash of the source code. */
3875
3876 static char buildtag[38] = "\0uilt-in";
3877
3878 if (buildtag[0] == 0) {
3879#ifdef CONFIG_MODULES
3880 if (THIS_MODULE != NULL)
3881 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3882 else
3883#endif
3884 buildtag[0] = 'b';
3885 }
3886
3887 return buildtag;
3888}
3889
3890module_init(drbd_init)
3891module_exit(drbd_cleanup)
3892
Philipp Reisnerb411b362009-09-25 16:07:19 -07003893EXPORT_SYMBOL(drbd_conn_str);
3894EXPORT_SYMBOL(drbd_role_str);
3895EXPORT_SYMBOL(drbd_disk_str);
3896EXPORT_SYMBOL(drbd_set_st_err_str);