blob: 23878ffc43c8ac588ea4060e83d38724eff02a7d [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,
507 union drbd_state ns, int *warn_sync_abort);
508int 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,
815 union drbd_state ns, int *warn_sync_abort)
816{
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
830 /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow */
831 if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
832 ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING)
833 ns.conn = os.conn;
834
835 /* After C_DISCONNECTING only C_STANDALONE may follow */
836 if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
837 ns.conn = os.conn;
838
839 if (ns.conn < C_CONNECTED) {
840 ns.peer_isp = 0;
841 ns.peer = R_UNKNOWN;
842 if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
843 ns.pdsk = D_UNKNOWN;
844 }
845
846 /* Clear the aftr_isp when becoming unconfigured */
847 if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
848 ns.aftr_isp = 0;
849
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850 /* Abort resync if a disk fails/detaches */
851 if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
852 (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
853 if (warn_sync_abort)
854 *warn_sync_abort = 1;
855 ns.conn = C_CONNECTED;
856 }
857
858 if (ns.conn >= C_CONNECTED &&
859 ((ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED) ||
860 (ns.disk == D_NEGOTIATING && ns.conn == C_WF_BITMAP_T))) {
861 switch (ns.conn) {
862 case C_WF_BITMAP_T:
863 case C_PAUSED_SYNC_T:
864 ns.disk = D_OUTDATED;
865 break;
866 case C_CONNECTED:
867 case C_WF_BITMAP_S:
868 case C_SYNC_SOURCE:
869 case C_PAUSED_SYNC_S:
870 ns.disk = D_UP_TO_DATE;
871 break;
872 case C_SYNC_TARGET:
873 ns.disk = D_INCONSISTENT;
874 dev_warn(DEV, "Implicitly set disk state Inconsistent!\n");
875 break;
876 }
877 if (os.disk == D_OUTDATED && ns.disk == D_UP_TO_DATE)
878 dev_warn(DEV, "Implicitly set disk from Outdated to UpToDate\n");
879 }
880
881 if (ns.conn >= C_CONNECTED &&
882 (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)) {
883 switch (ns.conn) {
884 case C_CONNECTED:
885 case C_WF_BITMAP_T:
886 case C_PAUSED_SYNC_T:
887 case C_SYNC_TARGET:
888 ns.pdsk = D_UP_TO_DATE;
889 break;
890 case C_WF_BITMAP_S:
891 case C_PAUSED_SYNC_S:
Lars Ellenberge0f83012010-04-01 15:13:19 +0200892 /* remap any consistent state to D_OUTDATED,
893 * but disallow "upgrade" of not even consistent states.
894 */
895 ns.pdsk =
896 (D_DISKLESS < os.pdsk && os.pdsk < D_OUTDATED)
897 ? os.pdsk : D_OUTDATED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 break;
899 case C_SYNC_SOURCE:
900 ns.pdsk = D_INCONSISTENT;
901 dev_warn(DEV, "Implicitly set pdsk Inconsistent!\n");
902 break;
903 }
904 if (os.pdsk == D_OUTDATED && ns.pdsk == D_UP_TO_DATE)
905 dev_warn(DEV, "Implicitly set pdsk from Outdated to UpToDate\n");
906 }
907
908 /* Connection breaks down before we finished "Negotiating" */
909 if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
910 get_ldev_if_state(mdev, D_NEGOTIATING)) {
911 if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
912 ns.disk = mdev->new_state_tmp.disk;
913 ns.pdsk = mdev->new_state_tmp.pdsk;
914 } else {
915 dev_alert(DEV, "Connection lost while negotiating, no data!\n");
916 ns.disk = D_DISKLESS;
917 ns.pdsk = D_UNKNOWN;
918 }
919 put_ldev(mdev);
920 }
921
922 if (fp == FP_STONITH &&
Philipp Reisner0a492162009-10-21 13:08:29 +0200923 (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
924 !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
Philipp Reisner265be2d2010-05-31 10:14:17 +0200925 ns.susp = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
926
927 if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
928 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
929 !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
930 ns.susp = 1; /* Suspend IO while no data available (no accessible data available) */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931
932 if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
933 if (ns.conn == C_SYNC_SOURCE)
934 ns.conn = C_PAUSED_SYNC_S;
935 if (ns.conn == C_SYNC_TARGET)
936 ns.conn = C_PAUSED_SYNC_T;
937 } else {
938 if (ns.conn == C_PAUSED_SYNC_S)
939 ns.conn = C_SYNC_SOURCE;
940 if (ns.conn == C_PAUSED_SYNC_T)
941 ns.conn = C_SYNC_TARGET;
942 }
943
944 return ns;
945}
946
947/* helper for __drbd_set_state */
948static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
949{
950 if (cs == C_VERIFY_T) {
951 /* starting online verify from an arbitrary position
952 * does not fit well into the existing protocol.
953 * on C_VERIFY_T, we initialize ov_left and friends
954 * implicitly in receive_DataRequest once the
955 * first P_OV_REQUEST is received */
956 mdev->ov_start_sector = ~(sector_t)0;
957 } else {
958 unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
959 if (bit >= mdev->rs_total)
960 mdev->ov_start_sector =
961 BM_BIT_TO_SECT(mdev->rs_total - 1);
962 mdev->ov_position = mdev->ov_start_sector;
963 }
964}
965
Philipp Reisner07782862010-08-31 12:00:50 +0200966static void drbd_resume_al(struct drbd_conf *mdev)
967{
968 if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
969 dev_info(DEV, "Resumed AL updates\n");
970}
971
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972/**
973 * __drbd_set_state() - Set a new DRBD state
974 * @mdev: DRBD device.
975 * @ns: new state.
976 * @flags: Flags
977 * @done: Optional completion, that will get completed after the after_state_ch() finished
978 *
979 * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
980 */
981int __drbd_set_state(struct drbd_conf *mdev,
982 union drbd_state ns, enum chg_state_flags flags,
983 struct completion *done)
984{
985 union drbd_state os;
986 int rv = SS_SUCCESS;
987 int warn_sync_abort = 0;
988 struct after_state_chg_work *ascw;
989
990 os = mdev->state;
991
992 ns = sanitize_state(mdev, os, ns, &warn_sync_abort);
993
994 if (ns.i == os.i)
995 return SS_NOTHING_TO_DO;
996
997 if (!(flags & CS_HARD)) {
998 /* pre-state-change checks ; only look at ns */
999 /* See drbd_state_sw_errors in drbd_strings.c */
1000
1001 rv = is_valid_state(mdev, ns);
1002 if (rv < SS_SUCCESS) {
1003 /* If the old state was illegal as well, then let
1004 this happen...*/
1005
Philipp Reisner1616a252010-06-10 16:55:15 +02001006 if (is_valid_state(mdev, os) == rv)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007 rv = is_valid_state_transition(mdev, ns, os);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 } else
1009 rv = is_valid_state_transition(mdev, ns, os);
1010 }
1011
1012 if (rv < SS_SUCCESS) {
1013 if (flags & CS_VERBOSE)
1014 print_st_err(mdev, os, ns, rv);
1015 return rv;
1016 }
1017
1018 if (warn_sync_abort)
1019 dev_warn(DEV, "Resync aborted.\n");
1020
1021 {
1022 char *pbp, pb[300];
1023 pbp = pb;
1024 *pbp = 0;
1025 PSC(role);
1026 PSC(peer);
1027 PSC(conn);
1028 PSC(disk);
1029 PSC(pdsk);
1030 PSC(susp);
1031 PSC(aftr_isp);
1032 PSC(peer_isp);
1033 PSC(user_isp);
1034 dev_info(DEV, "%s\n", pb);
1035 }
1036
1037 /* solve the race between becoming unconfigured,
1038 * worker doing the cleanup, and
1039 * admin reconfiguring us:
1040 * on (re)configure, first set CONFIG_PENDING,
1041 * then wait for a potentially exiting worker,
1042 * start the worker, and schedule one no_op.
1043 * then proceed with configuration.
1044 */
1045 if (ns.disk == D_DISKLESS &&
1046 ns.conn == C_STANDALONE &&
1047 ns.role == R_SECONDARY &&
1048 !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
1049 set_bit(DEVICE_DYING, &mdev->flags);
1050
1051 mdev->state.i = ns.i;
1052 wake_up(&mdev->misc_wait);
1053 wake_up(&mdev->state_wait);
1054
1055 /* post-state-change actions */
1056 if (os.conn >= C_SYNC_SOURCE && ns.conn <= C_CONNECTED) {
1057 set_bit(STOP_SYNC_TIMER, &mdev->flags);
1058 mod_timer(&mdev->resync_timer, jiffies);
1059 }
1060
1061 /* aborted verify run. log the last position */
1062 if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
1063 ns.conn < C_CONNECTED) {
1064 mdev->ov_start_sector =
1065 BM_BIT_TO_SECT(mdev->rs_total - mdev->ov_left);
1066 dev_info(DEV, "Online Verify reached sector %llu\n",
1067 (unsigned long long)mdev->ov_start_sector);
1068 }
1069
1070 if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
1071 (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
1072 dev_info(DEV, "Syncer continues.\n");
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001073 mdev->rs_paused += (long)jiffies
1074 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 if (ns.conn == C_SYNC_TARGET) {
1076 if (!test_and_clear_bit(STOP_SYNC_TIMER, &mdev->flags))
1077 mod_timer(&mdev->resync_timer, jiffies);
1078 /* This if (!test_bit) is only needed for the case
1079 that a device that has ceased to used its timer,
1080 i.e. it is already in drbd_resync_finished() gets
1081 paused and resumed. */
1082 }
1083 }
1084
1085 if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
1086 (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
1087 dev_info(DEV, "Resync suspended\n");
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001088 mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089 if (ns.conn == C_PAUSED_SYNC_T)
1090 set_bit(STOP_SYNC_TIMER, &mdev->flags);
1091 }
1092
1093 if (os.conn == C_CONNECTED &&
1094 (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001095 unsigned long now = jiffies;
1096 int i;
1097
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098 mdev->ov_position = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001099 mdev->rs_total = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 if (mdev->agreed_pro_version >= 90)
1101 set_ov_position(mdev, ns.conn);
1102 else
1103 mdev->ov_start_sector = 0;
1104 mdev->ov_left = mdev->rs_total
1105 - BM_SECT_TO_BIT(mdev->ov_position);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001106 mdev->rs_start = now;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001107 mdev->rs_last_events = 0;
1108 mdev->rs_last_sect_ev = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109 mdev->ov_last_oos_size = 0;
1110 mdev->ov_last_oos_start = 0;
1111
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001112 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1113 mdev->rs_mark_left[i] = mdev->rs_total;
1114 mdev->rs_mark_time[i] = now;
1115 }
1116
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117 if (ns.conn == C_VERIFY_S) {
1118 dev_info(DEV, "Starting Online Verify from sector %llu\n",
1119 (unsigned long long)mdev->ov_position);
1120 mod_timer(&mdev->resync_timer, jiffies);
1121 }
1122 }
1123
1124 if (get_ldev(mdev)) {
1125 u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
1126 MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
1127 MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
1128
1129 if (test_bit(CRASHED_PRIMARY, &mdev->flags))
1130 mdf |= MDF_CRASHED_PRIMARY;
1131 if (mdev->state.role == R_PRIMARY ||
1132 (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
1133 mdf |= MDF_PRIMARY_IND;
1134 if (mdev->state.conn > C_WF_REPORT_PARAMS)
1135 mdf |= MDF_CONNECTED_IND;
1136 if (mdev->state.disk > D_INCONSISTENT)
1137 mdf |= MDF_CONSISTENT;
1138 if (mdev->state.disk > D_OUTDATED)
1139 mdf |= MDF_WAS_UP_TO_DATE;
1140 if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
1141 mdf |= MDF_PEER_OUT_DATED;
1142 if (mdf != mdev->ldev->md.flags) {
1143 mdev->ldev->md.flags = mdf;
1144 drbd_md_mark_dirty(mdev);
1145 }
1146 if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
1147 drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
1148 put_ldev(mdev);
1149 }
1150
1151 /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
1152 if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
1153 os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
1154 set_bit(CONSIDER_RESYNC, &mdev->flags);
1155
1156 /* Receiver should clean up itself */
1157 if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
1158 drbd_thread_stop_nowait(&mdev->receiver);
1159
1160 /* Now the receiver finished cleaning up itself, it should die */
1161 if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
1162 drbd_thread_stop_nowait(&mdev->receiver);
1163
1164 /* Upon network failure, we need to restart the receiver. */
1165 if (os.conn > C_TEAR_DOWN &&
1166 ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
1167 drbd_thread_restart_nowait(&mdev->receiver);
1168
Philipp Reisner07782862010-08-31 12:00:50 +02001169 /* Resume AL writing if we get a connection */
1170 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
1171 drbd_resume_al(mdev);
1172
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
1174 if (ascw) {
1175 ascw->os = os;
1176 ascw->ns = ns;
1177 ascw->flags = flags;
1178 ascw->w.cb = w_after_state_ch;
1179 ascw->done = done;
1180 drbd_queue_work(&mdev->data.work, &ascw->w);
1181 } else {
1182 dev_warn(DEV, "Could not kmalloc an ascw\n");
1183 }
1184
1185 return rv;
1186}
1187
1188static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1189{
1190 struct after_state_chg_work *ascw =
1191 container_of(w, struct after_state_chg_work, w);
1192 after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
1193 if (ascw->flags & CS_WAIT_COMPLETE) {
1194 D_ASSERT(ascw->done != NULL);
1195 complete(ascw->done);
1196 }
1197 kfree(ascw);
1198
1199 return 1;
1200}
1201
1202static void abw_start_sync(struct drbd_conf *mdev, int rv)
1203{
1204 if (rv) {
1205 dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
1206 _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
1207 return;
1208 }
1209
1210 switch (mdev->state.conn) {
1211 case C_STARTING_SYNC_T:
1212 _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
1213 break;
1214 case C_STARTING_SYNC_S:
1215 drbd_start_resync(mdev, C_SYNC_SOURCE);
1216 break;
1217 }
1218}
1219
1220/**
1221 * after_state_ch() - Perform after state change actions that may sleep
1222 * @mdev: DRBD device.
1223 * @os: old state.
1224 * @ns: new state.
1225 * @flags: Flags
1226 */
1227static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
1228 union drbd_state ns, enum chg_state_flags flags)
1229{
1230 enum drbd_fencing_p fp;
Philipp Reisner67098932010-06-24 16:24:25 +02001231 enum drbd_req_event what = nothing;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232
1233 if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
1234 clear_bit(CRASHED_PRIMARY, &mdev->flags);
1235 if (mdev->p_uuid)
1236 mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
1237 }
1238
1239 fp = FP_DONT_CARE;
1240 if (get_ldev(mdev)) {
1241 fp = mdev->ldev->dc.fencing;
1242 put_ldev(mdev);
1243 }
1244
1245 /* Inform userspace about the change... */
1246 drbd_bcast_state(mdev, ns);
1247
1248 if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
1249 (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
1250 drbd_khelper(mdev, "pri-on-incon-degr");
1251
1252 /* Here we have the actions that are performed after a
1253 state change. This function might sleep */
1254
Philipp Reisner265be2d2010-05-31 10:14:17 +02001255 if (os.susp && ns.susp && mdev->sync_conf.on_no_data == OND_SUSPEND_IO) {
1256 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
Philipp Reisner67098932010-06-24 16:24:25 +02001257 if (ns.conn == C_CONNECTED)
1258 what = resend;
1259 else /* ns.conn > C_CONNECTED */
Philipp Reisner265be2d2010-05-31 10:14:17 +02001260 dev_err(DEV, "Unexpected Resynd going on!\n");
1261 }
1262
Philipp Reisner67098932010-06-24 16:24:25 +02001263 if (os.disk == D_ATTACHING && ns.disk > D_ATTACHING)
1264 what = restart_frozen_disk_io;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001265 }
1266
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 if (fp == FP_STONITH && ns.susp) {
Philipp Reisner43a51822010-06-11 11:26:34 +02001268 /* case1: The outdate peer handler is successful: */
1269 if (os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 tl_clear(mdev);
Philipp Reisner43a51822010-06-11 11:26:34 +02001271 if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
1272 drbd_uuid_new_current(mdev);
1273 clear_bit(NEW_CUR_UUID, &mdev->flags);
1274 drbd_md_sync(mdev);
1275 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 spin_lock_irq(&mdev->req_lock);
1277 _drbd_set_state(_NS(mdev, susp, 0), CS_VERBOSE, NULL);
1278 spin_unlock_irq(&mdev->req_lock);
1279 }
Philipp Reisner43a51822010-06-11 11:26:34 +02001280 /* case2: The connection was established again: */
1281 if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
1282 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner67098932010-06-24 16:24:25 +02001283 what = resend;
Philipp Reisner43a51822010-06-11 11:26:34 +02001284 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 }
Philipp Reisner67098932010-06-24 16:24:25 +02001286
1287 if (what != nothing) {
1288 spin_lock_irq(&mdev->req_lock);
1289 _tl_restart(mdev, what);
1290 _drbd_set_state(_NS(mdev, susp, 0), CS_VERBOSE, NULL);
1291 spin_unlock_irq(&mdev->req_lock);
1292 }
1293
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294 /* Do not change the order of the if above and the two below... */
1295 if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
1296 drbd_send_uuids(mdev);
1297 drbd_send_state(mdev);
1298 }
1299 if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S)
1300 drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL, "send_bitmap (WFBitMapS)");
1301
1302 /* Lost contact to peer's copy of the data */
1303 if ((os.pdsk >= D_INCONSISTENT &&
1304 os.pdsk != D_UNKNOWN &&
1305 os.pdsk != D_OUTDATED)
1306 && (ns.pdsk < D_INCONSISTENT ||
1307 ns.pdsk == D_UNKNOWN ||
1308 ns.pdsk == D_OUTDATED)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001309 if (get_ldev(mdev)) {
1310 if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001311 mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
Philipp Reisner43a51822010-06-11 11:26:34 +02001312 if (mdev->state.susp) {
1313 set_bit(NEW_CUR_UUID, &mdev->flags);
1314 } else {
1315 drbd_uuid_new_current(mdev);
1316 drbd_send_uuids(mdev);
1317 }
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001318 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319 put_ldev(mdev);
1320 }
1321 }
1322
1323 if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
Philipp Reisner18a50fa2010-06-21 14:14:15 +02001324 if (ns.peer == R_PRIMARY && mdev->ldev->md.uuid[UI_BITMAP] == 0) {
Philipp Reisner2c8d1962010-05-25 14:32:03 +02001325 drbd_uuid_new_current(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02001326 drbd_send_uuids(mdev);
1327 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328
1329 /* D_DISKLESS Peer becomes secondary */
1330 if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
1331 drbd_al_to_on_disk_bm(mdev);
1332 put_ldev(mdev);
1333 }
1334
1335 /* Last part of the attaching process ... */
1336 if (ns.conn >= C_CONNECTED &&
1337 os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
Philipp Reisnere89b5912010-03-24 17:11:33 +01001338 drbd_send_sizes(mdev, 0, 0); /* to start sync... */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 drbd_send_uuids(mdev);
1340 drbd_send_state(mdev);
1341 }
1342
1343 /* We want to pause/continue resync, tell peer. */
1344 if (ns.conn >= C_CONNECTED &&
1345 ((os.aftr_isp != ns.aftr_isp) ||
1346 (os.user_isp != ns.user_isp)))
1347 drbd_send_state(mdev);
1348
1349 /* In case one of the isp bits got set, suspend other devices. */
1350 if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
1351 (ns.aftr_isp || ns.peer_isp || ns.user_isp))
1352 suspend_other_sg(mdev);
1353
1354 /* Make sure the peer gets informed about eventual state
1355 changes (ISP bits) while we were in WFReportParams. */
1356 if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
1357 drbd_send_state(mdev);
1358
1359 /* We are in the progress to start a full sync... */
1360 if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
1361 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
1362 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, &abw_start_sync, "set_n_write from StartingSync");
1363
1364 /* We are invalidating our self... */
1365 if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
1366 os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
1367 drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL, "set_n_write from invalidate");
1368
1369 if (os.disk > D_FAILED && ns.disk == D_FAILED) {
1370 enum drbd_io_error_p eh;
1371
1372 eh = EP_PASS_ON;
1373 if (get_ldev_if_state(mdev, D_FAILED)) {
1374 eh = mdev->ldev->dc.on_io_error;
1375 put_ldev(mdev);
1376 }
1377
1378 drbd_rs_cancel_all(mdev);
1379 /* since get_ldev() only works as long as disk>=D_INCONSISTENT,
1380 and it is D_DISKLESS here, local_cnt can only go down, it can
1381 not increase... It will reach zero */
1382 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
1383 mdev->rs_total = 0;
1384 mdev->rs_failed = 0;
1385 atomic_set(&mdev->rs_pending_cnt, 0);
1386
1387 spin_lock_irq(&mdev->req_lock);
1388 _drbd_set_state(_NS(mdev, disk, D_DISKLESS), CS_HARD, NULL);
1389 spin_unlock_irq(&mdev->req_lock);
1390
1391 if (eh == EP_CALL_HELPER)
1392 drbd_khelper(mdev, "local-io-error");
1393 }
1394
1395 if (os.disk > D_DISKLESS && ns.disk == D_DISKLESS) {
1396
1397 if (os.disk == D_FAILED) /* && ns.disk == D_DISKLESS*/ {
1398 if (drbd_send_state(mdev))
1399 dev_warn(DEV, "Notified peer that my disk is broken.\n");
1400 else
1401 dev_err(DEV, "Sending state in drbd_io_error() failed\n");
1402 }
1403
Philipp Reisner0a6dbf22009-12-28 16:58:38 +01001404 wait_event(mdev->misc_wait, !atomic_read(&mdev->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405 lc_destroy(mdev->resync);
1406 mdev->resync = NULL;
1407 lc_destroy(mdev->act_log);
1408 mdev->act_log = NULL;
1409 __no_warn(local,
1410 drbd_free_bc(mdev->ldev);
1411 mdev->ldev = NULL;);
1412
1413 if (mdev->md_io_tmpp)
1414 __free_page(mdev->md_io_tmpp);
1415 }
1416
1417 /* Disks got bigger while they were detached */
1418 if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
1419 test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
1420 if (ns.conn == C_CONNECTED)
1421 resync_after_online_grow(mdev);
1422 }
1423
1424 /* A resync finished or aborted, wake paused devices... */
1425 if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
1426 (os.peer_isp && !ns.peer_isp) ||
1427 (os.user_isp && !ns.user_isp))
1428 resume_next_sg(mdev);
1429
Philipp Reisnerf70b35112010-06-24 14:34:40 +02001430 /* free tl_hash if we Got thawed and are C_STANDALONE */
1431 if (ns.conn == C_STANDALONE && ns.susp == 0 && mdev->tl_hash)
1432 drbd_free_tl_hash(mdev);
1433
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434 /* Upon network connection, we need to start the receiver */
1435 if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
1436 drbd_thread_start(&mdev->receiver);
1437
1438 /* Terminate worker thread if we are unconfigured - it will be
1439 restarted as needed... */
1440 if (ns.disk == D_DISKLESS &&
1441 ns.conn == C_STANDALONE &&
1442 ns.role == R_SECONDARY) {
1443 if (os.aftr_isp != ns.aftr_isp)
1444 resume_next_sg(mdev);
1445 /* set in __drbd_set_state, unless CONFIG_PENDING was set */
1446 if (test_bit(DEVICE_DYING, &mdev->flags))
1447 drbd_thread_stop_nowait(&mdev->worker);
1448 }
1449
1450 drbd_md_sync(mdev);
1451}
1452
1453
1454static int drbd_thread_setup(void *arg)
1455{
1456 struct drbd_thread *thi = (struct drbd_thread *) arg;
1457 struct drbd_conf *mdev = thi->mdev;
1458 unsigned long flags;
1459 int retval;
1460
1461restart:
1462 retval = thi->function(thi);
1463
1464 spin_lock_irqsave(&thi->t_lock, flags);
1465
1466 /* if the receiver has been "Exiting", the last thing it did
1467 * was set the conn state to "StandAlone",
1468 * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
1469 * and receiver thread will be "started".
1470 * drbd_thread_start needs to set "Restarting" in that case.
1471 * t_state check and assignment needs to be within the same spinlock,
1472 * so either thread_start sees Exiting, and can remap to Restarting,
1473 * or thread_start see None, and can proceed as normal.
1474 */
1475
1476 if (thi->t_state == Restarting) {
1477 dev_info(DEV, "Restarting %s\n", current->comm);
1478 thi->t_state = Running;
1479 spin_unlock_irqrestore(&thi->t_lock, flags);
1480 goto restart;
1481 }
1482
1483 thi->task = NULL;
1484 thi->t_state = None;
1485 smp_mb();
1486 complete(&thi->stop);
1487 spin_unlock_irqrestore(&thi->t_lock, flags);
1488
1489 dev_info(DEV, "Terminating %s\n", current->comm);
1490
1491 /* Release mod reference taken when thread was started */
1492 module_put(THIS_MODULE);
1493 return retval;
1494}
1495
1496static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
1497 int (*func) (struct drbd_thread *))
1498{
1499 spin_lock_init(&thi->t_lock);
1500 thi->task = NULL;
1501 thi->t_state = None;
1502 thi->function = func;
1503 thi->mdev = mdev;
1504}
1505
1506int drbd_thread_start(struct drbd_thread *thi)
1507{
1508 struct drbd_conf *mdev = thi->mdev;
1509 struct task_struct *nt;
1510 unsigned long flags;
1511
1512 const char *me =
1513 thi == &mdev->receiver ? "receiver" :
1514 thi == &mdev->asender ? "asender" :
1515 thi == &mdev->worker ? "worker" : "NONSENSE";
1516
1517 /* is used from state engine doing drbd_thread_stop_nowait,
1518 * while holding the req lock irqsave */
1519 spin_lock_irqsave(&thi->t_lock, flags);
1520
1521 switch (thi->t_state) {
1522 case None:
1523 dev_info(DEV, "Starting %s thread (from %s [%d])\n",
1524 me, current->comm, current->pid);
1525
1526 /* Get ref on module for thread - this is released when thread exits */
1527 if (!try_module_get(THIS_MODULE)) {
1528 dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
1529 spin_unlock_irqrestore(&thi->t_lock, flags);
1530 return FALSE;
1531 }
1532
1533 init_completion(&thi->stop);
1534 D_ASSERT(thi->task == NULL);
1535 thi->reset_cpu_mask = 1;
1536 thi->t_state = Running;
1537 spin_unlock_irqrestore(&thi->t_lock, flags);
1538 flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
1539
1540 nt = kthread_create(drbd_thread_setup, (void *) thi,
1541 "drbd%d_%s", mdev_to_minor(mdev), me);
1542
1543 if (IS_ERR(nt)) {
1544 dev_err(DEV, "Couldn't start thread\n");
1545
1546 module_put(THIS_MODULE);
1547 return FALSE;
1548 }
1549 spin_lock_irqsave(&thi->t_lock, flags);
1550 thi->task = nt;
1551 thi->t_state = Running;
1552 spin_unlock_irqrestore(&thi->t_lock, flags);
1553 wake_up_process(nt);
1554 break;
1555 case Exiting:
1556 thi->t_state = Restarting;
1557 dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
1558 me, current->comm, current->pid);
1559 /* fall through */
1560 case Running:
1561 case Restarting:
1562 default:
1563 spin_unlock_irqrestore(&thi->t_lock, flags);
1564 break;
1565 }
1566
1567 return TRUE;
1568}
1569
1570
1571void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
1572{
1573 unsigned long flags;
1574
1575 enum drbd_thread_state ns = restart ? Restarting : Exiting;
1576
1577 /* may be called from state engine, holding the req lock irqsave */
1578 spin_lock_irqsave(&thi->t_lock, flags);
1579
1580 if (thi->t_state == None) {
1581 spin_unlock_irqrestore(&thi->t_lock, flags);
1582 if (restart)
1583 drbd_thread_start(thi);
1584 return;
1585 }
1586
1587 if (thi->t_state != ns) {
1588 if (thi->task == NULL) {
1589 spin_unlock_irqrestore(&thi->t_lock, flags);
1590 return;
1591 }
1592
1593 thi->t_state = ns;
1594 smp_mb();
1595 init_completion(&thi->stop);
1596 if (thi->task != current)
1597 force_sig(DRBD_SIGKILL, thi->task);
1598
1599 }
1600
1601 spin_unlock_irqrestore(&thi->t_lock, flags);
1602
1603 if (wait)
1604 wait_for_completion(&thi->stop);
1605}
1606
1607#ifdef CONFIG_SMP
1608/**
1609 * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
1610 * @mdev: DRBD device.
1611 *
1612 * Forces all threads of a device onto the same CPU. This is beneficial for
1613 * DRBD's performance. May be overwritten by user's configuration.
1614 */
1615void drbd_calc_cpu_mask(struct drbd_conf *mdev)
1616{
1617 int ord, cpu;
1618
1619 /* user override. */
1620 if (cpumask_weight(mdev->cpu_mask))
1621 return;
1622
1623 ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
1624 for_each_online_cpu(cpu) {
1625 if (ord-- == 0) {
1626 cpumask_set_cpu(cpu, mdev->cpu_mask);
1627 return;
1628 }
1629 }
1630 /* should not be reached */
1631 cpumask_setall(mdev->cpu_mask);
1632}
1633
1634/**
1635 * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
1636 * @mdev: DRBD device.
1637 *
1638 * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
1639 * prematurely.
1640 */
1641void drbd_thread_current_set_cpu(struct drbd_conf *mdev)
1642{
1643 struct task_struct *p = current;
1644 struct drbd_thread *thi =
1645 p == mdev->asender.task ? &mdev->asender :
1646 p == mdev->receiver.task ? &mdev->receiver :
1647 p == mdev->worker.task ? &mdev->worker :
1648 NULL;
1649 ERR_IF(thi == NULL)
1650 return;
1651 if (!thi->reset_cpu_mask)
1652 return;
1653 thi->reset_cpu_mask = 0;
1654 set_cpus_allowed_ptr(p, mdev->cpu_mask);
1655}
1656#endif
1657
1658/* the appropriate socket mutex must be held already */
1659int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001660 enum drbd_packets cmd, struct p_header80 *h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001661 size_t size, unsigned msg_flags)
1662{
1663 int sent, ok;
1664
1665 ERR_IF(!h) return FALSE;
1666 ERR_IF(!size) return FALSE;
1667
1668 h->magic = BE_DRBD_MAGIC;
1669 h->command = cpu_to_be16(cmd);
Philipp Reisner0b70a132010-08-20 13:36:10 +02001670 h->length = cpu_to_be16(size-sizeof(struct p_header80));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672 sent = drbd_send(mdev, sock, h, size, msg_flags);
1673
1674 ok = (sent == size);
1675 if (!ok)
1676 dev_err(DEV, "short sent %s size=%d sent=%d\n",
1677 cmdname(cmd), (int)size, sent);
1678 return ok;
1679}
1680
1681/* don't pass the socket. we may only look at it
1682 * when we hold the appropriate socket mutex.
1683 */
1684int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001685 enum drbd_packets cmd, struct p_header80 *h, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686{
1687 int ok = 0;
1688 struct socket *sock;
1689
1690 if (use_data_socket) {
1691 mutex_lock(&mdev->data.mutex);
1692 sock = mdev->data.socket;
1693 } else {
1694 mutex_lock(&mdev->meta.mutex);
1695 sock = mdev->meta.socket;
1696 }
1697
1698 /* drbd_disconnect() could have called drbd_free_sock()
1699 * while we were waiting in down()... */
1700 if (likely(sock != NULL))
1701 ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
1702
1703 if (use_data_socket)
1704 mutex_unlock(&mdev->data.mutex);
1705 else
1706 mutex_unlock(&mdev->meta.mutex);
1707 return ok;
1708}
1709
1710int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data,
1711 size_t size)
1712{
Philipp Reisner0b70a132010-08-20 13:36:10 +02001713 struct p_header80 h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714 int ok;
1715
1716 h.magic = BE_DRBD_MAGIC;
1717 h.command = cpu_to_be16(cmd);
1718 h.length = cpu_to_be16(size);
1719
1720 if (!drbd_get_data_sock(mdev))
1721 return 0;
1722
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723 ok = (sizeof(h) ==
1724 drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0));
1725 ok = ok && (size ==
1726 drbd_send(mdev, mdev->data.socket, data, size, 0));
1727
1728 drbd_put_data_sock(mdev);
1729
1730 return ok;
1731}
1732
1733int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
1734{
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001735 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 struct socket *sock;
1737 int size, rv;
1738 const int apv = mdev->agreed_pro_version;
1739
1740 size = apv <= 87 ? sizeof(struct p_rs_param)
1741 : apv == 88 ? sizeof(struct p_rs_param)
1742 + strlen(mdev->sync_conf.verify_alg) + 1
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001743 : apv <= 94 ? sizeof(struct p_rs_param_89)
1744 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745
1746 /* used from admin command context and receiver/worker context.
1747 * to avoid kmalloc, grab the socket right here,
1748 * then use the pre-allocated sbuf there */
1749 mutex_lock(&mdev->data.mutex);
1750 sock = mdev->data.socket;
1751
1752 if (likely(sock != NULL)) {
1753 enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
1754
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001755 p = &mdev->data.sbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001756
1757 /* initialize verify_alg and csums_alg */
1758 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
1759
1760 p->rate = cpu_to_be32(sc->rate);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02001761 p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
1762 p->c_delay_target = cpu_to_be32(sc->c_delay_target);
1763 p->c_fill_target = cpu_to_be32(sc->c_fill_target);
1764 p->c_max_rate = cpu_to_be32(sc->c_max_rate);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765
1766 if (apv >= 88)
1767 strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
1768 if (apv >= 89)
1769 strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
1770
1771 rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
1772 } else
1773 rv = 0; /* not ok */
1774
1775 mutex_unlock(&mdev->data.mutex);
1776
1777 return rv;
1778}
1779
1780int drbd_send_protocol(struct drbd_conf *mdev)
1781{
1782 struct p_protocol *p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001783 int size, cf, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784
1785 size = sizeof(struct p_protocol);
1786
1787 if (mdev->agreed_pro_version >= 87)
1788 size += strlen(mdev->net_conf->integrity_alg) + 1;
1789
1790 /* we must not recurse into our own queue,
1791 * as that is blocked during handshake */
1792 p = kmalloc(size, GFP_NOIO);
1793 if (p == NULL)
1794 return 0;
1795
1796 p->protocol = cpu_to_be32(mdev->net_conf->wire_protocol);
1797 p->after_sb_0p = cpu_to_be32(mdev->net_conf->after_sb_0p);
1798 p->after_sb_1p = cpu_to_be32(mdev->net_conf->after_sb_1p);
1799 p->after_sb_2p = cpu_to_be32(mdev->net_conf->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001800 p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
1801
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001802 cf = 0;
1803 if (mdev->net_conf->want_lose)
1804 cf |= CF_WANT_LOSE;
1805 if (mdev->net_conf->dry_run) {
1806 if (mdev->agreed_pro_version >= 92)
1807 cf |= CF_DRY_RUN;
1808 else {
1809 dev_err(DEV, "--dry-run is not supported by peer");
Dan Carpenter7ac314c2010-04-22 14:27:23 +02001810 kfree(p);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01001811 return 0;
1812 }
1813 }
1814 p->conn_flags = cpu_to_be32(cf);
1815
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 if (mdev->agreed_pro_version >= 87)
1817 strcpy(p->integrity_alg, mdev->net_conf->integrity_alg);
1818
1819 rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001820 (struct p_header80 *)p, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821 kfree(p);
1822 return rv;
1823}
1824
1825int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
1826{
1827 struct p_uuids p;
1828 int i;
1829
1830 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
1831 return 1;
1832
1833 for (i = UI_CURRENT; i < UI_SIZE; i++)
1834 p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
1835
1836 mdev->comm_bm_set = drbd_bm_total_weight(mdev);
1837 p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
1838 uuid_flags |= mdev->net_conf->want_lose ? 1 : 0;
1839 uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
1840 uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
1841 p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
1842
1843 put_ldev(mdev);
1844
1845 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001846 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847}
1848
1849int drbd_send_uuids(struct drbd_conf *mdev)
1850{
1851 return _drbd_send_uuids(mdev, 0);
1852}
1853
1854int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
1855{
1856 return _drbd_send_uuids(mdev, 8);
1857}
1858
1859
1860int drbd_send_sync_uuid(struct drbd_conf *mdev, u64 val)
1861{
1862 struct p_rs_uuid p;
1863
1864 p.uuid = cpu_to_be64(val);
1865
1866 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001867 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868}
1869
Philipp Reisnere89b5912010-03-24 17:11:33 +01001870int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001871{
1872 struct p_sizes p;
1873 sector_t d_size, u_size;
1874 int q_order_type;
1875 int ok;
1876
1877 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
1878 D_ASSERT(mdev->ldev->backing_bdev);
1879 d_size = drbd_get_max_capacity(mdev->ldev);
1880 u_size = mdev->ldev->dc.disk_size;
1881 q_order_type = drbd_queue_order_type(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882 put_ldev(mdev);
1883 } else {
1884 d_size = 0;
1885 u_size = 0;
1886 q_order_type = QUEUE_ORDERED_NONE;
1887 }
1888
1889 p.d_size = cpu_to_be64(d_size);
1890 p.u_size = cpu_to_be64(u_size);
1891 p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
1892 p.max_segment_size = cpu_to_be32(queue_max_segment_size(mdev->rq_queue));
Philipp Reisnere89b5912010-03-24 17:11:33 +01001893 p.queue_order_type = cpu_to_be16(q_order_type);
1894 p.dds_flags = cpu_to_be16(flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895
1896 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001897 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898 return ok;
1899}
1900
1901/**
1902 * drbd_send_state() - Sends the drbd state to the peer
1903 * @mdev: DRBD device.
1904 */
1905int drbd_send_state(struct drbd_conf *mdev)
1906{
1907 struct socket *sock;
1908 struct p_state p;
1909 int ok = 0;
1910
1911 /* Grab state lock so we wont send state if we're in the middle
1912 * of a cluster wide state change on another thread */
1913 drbd_state_lock(mdev);
1914
1915 mutex_lock(&mdev->data.mutex);
1916
1917 p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
1918 sock = mdev->data.socket;
1919
1920 if (likely(sock != NULL)) {
1921 ok = _drbd_send_cmd(mdev, sock, P_STATE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001922 (struct p_header80 *)&p, sizeof(p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001923 }
1924
1925 mutex_unlock(&mdev->data.mutex);
1926
1927 drbd_state_unlock(mdev);
1928 return ok;
1929}
1930
1931int drbd_send_state_req(struct drbd_conf *mdev,
1932 union drbd_state mask, union drbd_state val)
1933{
1934 struct p_req_state p;
1935
1936 p.mask = cpu_to_be32(mask.i);
1937 p.val = cpu_to_be32(val.i);
1938
1939 return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001940 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001941}
1942
1943int drbd_send_sr_reply(struct drbd_conf *mdev, int retcode)
1944{
1945 struct p_req_state_reply p;
1946
1947 p.retcode = cpu_to_be32(retcode);
1948
1949 return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001950 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951}
1952
1953int fill_bitmap_rle_bits(struct drbd_conf *mdev,
1954 struct p_compressed_bm *p,
1955 struct bm_xfer_ctx *c)
1956{
1957 struct bitstream bs;
1958 unsigned long plain_bits;
1959 unsigned long tmp;
1960 unsigned long rl;
1961 unsigned len;
1962 unsigned toggle;
1963 int bits;
1964
1965 /* may we use this feature? */
1966 if ((mdev->sync_conf.use_rle == 0) ||
1967 (mdev->agreed_pro_version < 90))
1968 return 0;
1969
1970 if (c->bit_offset >= c->bm_bits)
1971 return 0; /* nothing to do. */
1972
1973 /* use at most thus many bytes */
1974 bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
1975 memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
1976 /* plain bits covered in this code string */
1977 plain_bits = 0;
1978
1979 /* p->encoding & 0x80 stores whether the first run length is set.
1980 * bit offset is implicit.
1981 * start with toggle == 2 to be able to tell the first iteration */
1982 toggle = 2;
1983
1984 /* see how much plain bits we can stuff into one packet
1985 * using RLE and VLI. */
1986 do {
1987 tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
1988 : _drbd_bm_find_next(mdev, c->bit_offset);
1989 if (tmp == -1UL)
1990 tmp = c->bm_bits;
1991 rl = tmp - c->bit_offset;
1992
1993 if (toggle == 2) { /* first iteration */
1994 if (rl == 0) {
1995 /* the first checked bit was set,
1996 * store start value, */
1997 DCBP_set_start(p, 1);
1998 /* but skip encoding of zero run length */
1999 toggle = !toggle;
2000 continue;
2001 }
2002 DCBP_set_start(p, 0);
2003 }
2004
2005 /* paranoia: catch zero runlength.
2006 * can only happen if bitmap is modified while we scan it. */
2007 if (rl == 0) {
2008 dev_err(DEV, "unexpected zero runlength while encoding bitmap "
2009 "t:%u bo:%lu\n", toggle, c->bit_offset);
2010 return -1;
2011 }
2012
2013 bits = vli_encode_bits(&bs, rl);
2014 if (bits == -ENOBUFS) /* buffer full */
2015 break;
2016 if (bits <= 0) {
2017 dev_err(DEV, "error while encoding bitmap: %d\n", bits);
2018 return 0;
2019 }
2020
2021 toggle = !toggle;
2022 plain_bits += rl;
2023 c->bit_offset = tmp;
2024 } while (c->bit_offset < c->bm_bits);
2025
2026 len = bs.cur.b - p->code + !!bs.cur.bit;
2027
2028 if (plain_bits < (len << 3)) {
2029 /* incompressible with this method.
2030 * we need to rewind both word and bit position. */
2031 c->bit_offset -= plain_bits;
2032 bm_xfer_ctx_bit_to_word_offset(c);
2033 c->bit_offset = c->word_offset * BITS_PER_LONG;
2034 return 0;
2035 }
2036
2037 /* RLE + VLI was able to compress it just fine.
2038 * update c->word_offset. */
2039 bm_xfer_ctx_bit_to_word_offset(c);
2040
2041 /* store pad_bits */
2042 DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
2043
2044 return len;
2045}
2046
2047enum { OK, FAILED, DONE }
2048send_bitmap_rle_or_plain(struct drbd_conf *mdev,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002049 struct p_header80 *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002050{
2051 struct p_compressed_bm *p = (void*)h;
2052 unsigned long num_words;
2053 int len;
2054 int ok;
2055
2056 len = fill_bitmap_rle_bits(mdev, p, c);
2057
2058 if (len < 0)
2059 return FAILED;
2060
2061 if (len) {
2062 DCBP_set_code(p, RLE_VLI_Bits);
2063 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h,
2064 sizeof(*p) + len, 0);
2065
2066 c->packets[0]++;
2067 c->bytes[0] += sizeof(*p) + len;
2068
2069 if (c->bit_offset >= c->bm_bits)
2070 len = 0; /* DONE */
2071 } else {
2072 /* was not compressible.
2073 * send a buffer full of plain text bits instead. */
2074 num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
2075 len = num_words * sizeof(long);
2076 if (len)
2077 drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
2078 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002079 h, sizeof(struct p_header80) + len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002080 c->word_offset += num_words;
2081 c->bit_offset = c->word_offset * BITS_PER_LONG;
2082
2083 c->packets[1]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02002084 c->bytes[1] += sizeof(struct p_header80) + len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085
2086 if (c->bit_offset > c->bm_bits)
2087 c->bit_offset = c->bm_bits;
2088 }
2089 ok = ok ? ((len == 0) ? DONE : OK) : FAILED;
2090
2091 if (ok == DONE)
2092 INFO_bm_xfer_stats(mdev, "send", c);
2093 return ok;
2094}
2095
2096/* See the comment at receive_bitmap() */
2097int _drbd_send_bitmap(struct drbd_conf *mdev)
2098{
2099 struct bm_xfer_ctx c;
Philipp Reisner0b70a132010-08-20 13:36:10 +02002100 struct p_header80 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101 int ret;
2102
2103 ERR_IF(!mdev->bitmap) return FALSE;
2104
2105 /* maybe we should use some per thread scratch page,
2106 * and allocate that during initial device creation? */
Philipp Reisner0b70a132010-08-20 13:36:10 +02002107 p = (struct p_header80 *) __get_free_page(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108 if (!p) {
2109 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
2110 return FALSE;
2111 }
2112
2113 if (get_ldev(mdev)) {
2114 if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
2115 dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
2116 drbd_bm_set_all(mdev);
2117 if (drbd_bm_write(mdev)) {
2118 /* write_bm did fail! Leave full sync flag set in Meta P_DATA
2119 * but otherwise process as per normal - need to tell other
2120 * side that a full resync is required! */
2121 dev_err(DEV, "Failed to write bitmap to disk!\n");
2122 } else {
2123 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
2124 drbd_md_sync(mdev);
2125 }
2126 }
2127 put_ldev(mdev);
2128 }
2129
2130 c = (struct bm_xfer_ctx) {
2131 .bm_bits = drbd_bm_bits(mdev),
2132 .bm_words = drbd_bm_words(mdev),
2133 };
2134
2135 do {
2136 ret = send_bitmap_rle_or_plain(mdev, p, &c);
2137 } while (ret == OK);
2138
2139 free_page((unsigned long) p);
2140 return (ret == DONE);
2141}
2142
2143int drbd_send_bitmap(struct drbd_conf *mdev)
2144{
2145 int err;
2146
2147 if (!drbd_get_data_sock(mdev))
2148 return -1;
2149 err = !_drbd_send_bitmap(mdev);
2150 drbd_put_data_sock(mdev);
2151 return err;
2152}
2153
2154int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
2155{
2156 int ok;
2157 struct p_barrier_ack p;
2158
2159 p.barrier = barrier_nr;
2160 p.set_size = cpu_to_be32(set_size);
2161
2162 if (mdev->state.conn < C_CONNECTED)
2163 return FALSE;
2164 ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002165 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002166 return ok;
2167}
2168
2169/**
2170 * _drbd_send_ack() - Sends an ack packet
2171 * @mdev: DRBD device.
2172 * @cmd: Packet command code.
2173 * @sector: sector, needs to be in big endian byte order
2174 * @blksize: size in byte, needs to be in big endian byte order
2175 * @block_id: Id, big endian byte order
2176 */
2177static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
2178 u64 sector,
2179 u32 blksize,
2180 u64 block_id)
2181{
2182 int ok;
2183 struct p_block_ack p;
2184
2185 p.sector = sector;
2186 p.block_id = block_id;
2187 p.blksize = blksize;
2188 p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
2189
2190 if (!mdev->meta.socket || mdev->state.conn < C_CONNECTED)
2191 return FALSE;
2192 ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002193 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194 return ok;
2195}
2196
2197int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
2198 struct p_data *dp)
2199{
2200 const int header_size = sizeof(struct p_data)
Philipp Reisner0b70a132010-08-20 13:36:10 +02002201 - sizeof(struct p_header80);
2202 int data_size = ((struct p_header80 *)dp)->length - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002203
2204 return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
2205 dp->block_id);
2206}
2207
2208int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
2209 struct p_block_req *rp)
2210{
2211 return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
2212}
2213
2214/**
2215 * drbd_send_ack() - Sends an ack packet
2216 * @mdev: DRBD device.
2217 * @cmd: Packet command code.
2218 * @e: Epoch entry.
2219 */
2220int drbd_send_ack(struct drbd_conf *mdev,
2221 enum drbd_packets cmd, struct drbd_epoch_entry *e)
2222{
2223 return _drbd_send_ack(mdev, cmd,
2224 cpu_to_be64(e->sector),
2225 cpu_to_be32(e->size),
2226 e->block_id);
2227}
2228
2229/* This function misuses the block_id field to signal if the blocks
2230 * are is sync or not. */
2231int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
2232 sector_t sector, int blksize, u64 block_id)
2233{
2234 return _drbd_send_ack(mdev, cmd,
2235 cpu_to_be64(sector),
2236 cpu_to_be32(blksize),
2237 cpu_to_be64(block_id));
2238}
2239
2240int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
2241 sector_t sector, int size, u64 block_id)
2242{
2243 int ok;
2244 struct p_block_req p;
2245
2246 p.sector = cpu_to_be64(sector);
2247 p.block_id = block_id;
2248 p.blksize = cpu_to_be32(size);
2249
2250 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002251 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002252 return ok;
2253}
2254
2255int drbd_send_drequest_csum(struct drbd_conf *mdev,
2256 sector_t sector, int size,
2257 void *digest, int digest_size,
2258 enum drbd_packets cmd)
2259{
2260 int ok;
2261 struct p_block_req p;
2262
2263 p.sector = cpu_to_be64(sector);
2264 p.block_id = BE_DRBD_MAGIC + 0xbeef;
2265 p.blksize = cpu_to_be32(size);
2266
2267 p.head.magic = BE_DRBD_MAGIC;
2268 p.head.command = cpu_to_be16(cmd);
Philipp Reisner0b70a132010-08-20 13:36:10 +02002269 p.head.length = cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270
2271 mutex_lock(&mdev->data.mutex);
2272
2273 ok = (sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), 0));
2274 ok = ok && (digest_size == drbd_send(mdev, mdev->data.socket, digest, digest_size, 0));
2275
2276 mutex_unlock(&mdev->data.mutex);
2277
2278 return ok;
2279}
2280
2281int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
2282{
2283 int ok;
2284 struct p_block_req p;
2285
2286 p.sector = cpu_to_be64(sector);
2287 p.block_id = BE_DRBD_MAGIC + 0xbabe;
2288 p.blksize = cpu_to_be32(size);
2289
2290 ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST,
Philipp Reisner0b70a132010-08-20 13:36:10 +02002291 (struct p_header80 *)&p, sizeof(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292 return ok;
2293}
2294
2295/* called on sndtimeo
2296 * returns FALSE if we should retry,
2297 * TRUE if we think connection is dead
2298 */
2299static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *sock)
2300{
2301 int drop_it;
2302 /* long elapsed = (long)(jiffies - mdev->last_received); */
2303
2304 drop_it = mdev->meta.socket == sock
2305 || !mdev->asender.task
2306 || get_t_state(&mdev->asender) != Running
2307 || mdev->state.conn < C_CONNECTED;
2308
2309 if (drop_it)
2310 return TRUE;
2311
2312 drop_it = !--mdev->ko_count;
2313 if (!drop_it) {
2314 dev_err(DEV, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
2315 current->comm, current->pid, mdev->ko_count);
2316 request_ping(mdev);
2317 }
2318
2319 return drop_it; /* && (mdev->state == R_PRIMARY) */;
2320}
2321
2322/* The idea of sendpage seems to be to put some kind of reference
2323 * to the page into the skb, and to hand it over to the NIC. In
2324 * this process get_page() gets called.
2325 *
2326 * As soon as the page was really sent over the network put_page()
2327 * gets called by some part of the network layer. [ NIC driver? ]
2328 *
2329 * [ get_page() / put_page() increment/decrement the count. If count
2330 * reaches 0 the page will be freed. ]
2331 *
2332 * This works nicely with pages from FSs.
2333 * But this means that in protocol A we might signal IO completion too early!
2334 *
2335 * In order not to corrupt data during a resync we must make sure
2336 * that we do not reuse our own buffer pages (EEs) to early, therefore
2337 * we have the net_ee list.
2338 *
2339 * XFS seems to have problems, still, it submits pages with page_count == 0!
2340 * As a workaround, we disable sendpage on pages
2341 * with page_count == 0 or PageSlab.
2342 */
2343static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002344 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345{
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002346 int sent = drbd_send(mdev, mdev->data.socket, kmap(page) + offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002347 kunmap(page);
2348 if (sent == size)
2349 mdev->send_cnt += size>>9;
2350 return sent == size;
2351}
2352
2353static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002354 int offset, size_t size, unsigned msg_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002355{
2356 mm_segment_t oldfs = get_fs();
2357 int sent, ok;
2358 int len = size;
2359
2360 /* e.g. XFS meta- & log-data is in slab pages, which have a
2361 * page_count of 0 and/or have PageSlab() set.
2362 * we cannot use send_page for those, as that does get_page();
2363 * put_page(); and would cause either a VM_BUG directly, or
2364 * __page_cache_release a page that would actually still be referenced
2365 * by someone, leading to some obscure delayed Oops somewhere else. */
2366 if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002367 return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002369 msg_flags |= MSG_NOSIGNAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002370 drbd_update_congested(mdev);
2371 set_fs(KERNEL_DS);
2372 do {
2373 sent = mdev->data.socket->ops->sendpage(mdev->data.socket, page,
2374 offset, len,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002375 msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376 if (sent == -EAGAIN) {
2377 if (we_should_drop_the_connection(mdev,
2378 mdev->data.socket))
2379 break;
2380 else
2381 continue;
2382 }
2383 if (sent <= 0) {
2384 dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
2385 __func__, (int)size, len, sent);
2386 break;
2387 }
2388 len -= sent;
2389 offset += sent;
2390 } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
2391 set_fs(oldfs);
2392 clear_bit(NET_CONGESTED, &mdev->flags);
2393
2394 ok = (len == 0);
2395 if (likely(ok))
2396 mdev->send_cnt += size>>9;
2397 return ok;
2398}
2399
2400static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
2401{
2402 struct bio_vec *bvec;
2403 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002404 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002405 __bio_for_each_segment(bvec, bio, i, 0) {
2406 if (!_drbd_no_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002407 bvec->bv_offset, bvec->bv_len,
2408 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409 return 0;
2410 }
2411 return 1;
2412}
2413
2414static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
2415{
2416 struct bio_vec *bvec;
2417 int i;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002418 /* hint all but last page with MSG_MORE */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002419 __bio_for_each_segment(bvec, bio, i, 0) {
2420 if (!_drbd_send_page(mdev, bvec->bv_page,
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002421 bvec->bv_offset, bvec->bv_len,
2422 i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002423 return 0;
2424 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425 return 1;
2426}
2427
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002428static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
2429{
2430 struct page *page = e->pages;
2431 unsigned len = e->size;
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002432 /* hint all but last page with MSG_MORE */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002433 page_chain_for_each(page) {
2434 unsigned l = min_t(unsigned, len, PAGE_SIZE);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002435 if (!_drbd_send_page(mdev, page, 0, l,
2436 page_chain_next(page) ? MSG_MORE : 0))
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002437 return 0;
2438 len -= l;
2439 }
2440 return 1;
2441}
2442
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443/* Used to send write requests
2444 * R_PRIMARY -> Peer (P_DATA)
2445 */
2446int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
2447{
2448 int ok = 1;
2449 struct p_data p;
2450 unsigned int dp_flags = 0;
2451 void *dgb;
2452 int dgs;
2453
2454 if (!drbd_get_data_sock(mdev))
2455 return 0;
2456
2457 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
2458 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
2459
Philipp Reisnerd5373382010-08-23 15:18:33 +02002460 if (req->size <= DRBD_MAX_SIZE_H80_PACKET) {
Philipp Reisner0b70a132010-08-20 13:36:10 +02002461 p.head.h80.magic = BE_DRBD_MAGIC;
2462 p.head.h80.command = cpu_to_be16(P_DATA);
2463 p.head.h80.length =
2464 cpu_to_be16(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2465 } else {
2466 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2467 p.head.h95.command = cpu_to_be16(P_DATA);
2468 p.head.h95.length =
2469 cpu_to_be32(sizeof(p) - sizeof(union p_header) + dgs + req->size);
2470 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002471
2472 p.sector = cpu_to_be64(req->sector);
2473 p.block_id = (unsigned long)req;
2474 p.seq_num = cpu_to_be32(req->seq_num =
2475 atomic_add_return(1, &mdev->packet_seq));
2476 dp_flags = 0;
2477
2478 /* NOTE: no need to check if barriers supported here as we would
2479 * not pass the test in make_request_common in that case
2480 */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002481 if (req->master_bio->bi_rw & REQ_HARDBARRIER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002482 dev_err(DEV, "ASSERT FAILED would have set DP_HARDBARRIER\n");
2483 /* dp_flags |= DP_HARDBARRIER; */
2484 }
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002485 if (req->master_bio->bi_rw & REQ_SYNC)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002486 dp_flags |= DP_RW_SYNC;
2487 /* for now handle SYNCIO and UNPLUG
2488 * as if they still were one and the same flag */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02002489 if (req->master_bio->bi_rw & REQ_UNPLUG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490 dp_flags |= DP_RW_SYNC;
2491 if (mdev->state.conn >= C_SYNC_SOURCE &&
2492 mdev->state.conn <= C_PAUSED_SYNC_T)
2493 dp_flags |= DP_MAY_SET_IN_SYNC;
2494
2495 p.dp_flags = cpu_to_be32(dp_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002496 set_bit(UNPLUG_REMOTE, &mdev->flags);
2497 ok = (sizeof(p) ==
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002498 drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002499 if (ok && dgs) {
2500 dgb = mdev->int_dig_out;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002501 drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, dgb);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002502 ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002503 }
2504 if (ok) {
2505 if (mdev->net_conf->wire_protocol == DRBD_PROT_A)
2506 ok = _drbd_send_bio(mdev, req->master_bio);
2507 else
2508 ok = _drbd_send_zc_bio(mdev, req->master_bio);
2509 }
2510
2511 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02002512
Philipp Reisnerb411b362009-09-25 16:07:19 -07002513 return ok;
2514}
2515
2516/* answer packet, used to send data back for read requests:
2517 * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
2518 * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
2519 */
2520int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
2521 struct drbd_epoch_entry *e)
2522{
2523 int ok;
2524 struct p_data p;
2525 void *dgb;
2526 int dgs;
2527
2528 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
2529 crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
2530
Philipp Reisnerd5373382010-08-23 15:18:33 +02002531 if (e->size <= DRBD_MAX_SIZE_H80_PACKET) {
Philipp Reisner0b70a132010-08-20 13:36:10 +02002532 p.head.h80.magic = BE_DRBD_MAGIC;
2533 p.head.h80.command = cpu_to_be16(cmd);
2534 p.head.h80.length =
2535 cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2536 } else {
2537 p.head.h95.magic = BE_DRBD_MAGIC_BIG;
2538 p.head.h95.command = cpu_to_be16(cmd);
2539 p.head.h95.length =
2540 cpu_to_be32(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
2541 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002542
2543 p.sector = cpu_to_be64(e->sector);
2544 p.block_id = e->block_id;
2545 /* p.seq_num = 0; No sequence numbers here.. */
2546
2547 /* Only called by our kernel thread.
2548 * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
2549 * in response to admin command or module unload.
2550 */
2551 if (!drbd_get_data_sock(mdev))
2552 return 0;
2553
Philipp Reisner0b70a132010-08-20 13:36:10 +02002554 ok = sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555 if (ok && dgs) {
2556 dgb = mdev->int_dig_out;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002557 drbd_csum_ee(mdev, mdev->integrity_w_tfm, e, dgb);
Lars Ellenbergba11ad92010-05-25 16:26:16 +02002558 ok = drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559 }
2560 if (ok)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002561 ok = _drbd_send_zc_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002562
2563 drbd_put_data_sock(mdev);
Philipp Reisnerbd26bfc52010-05-04 12:33:58 +02002564
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565 return ok;
2566}
2567
2568/*
2569 drbd_send distinguishes two cases:
2570
2571 Packets sent via the data socket "sock"
2572 and packets sent via the meta data socket "msock"
2573
2574 sock msock
2575 -----------------+-------------------------+------------------------------
2576 timeout conf.timeout / 2 conf.timeout / 2
2577 timeout action send a ping via msock Abort communication
2578 and close all sockets
2579*/
2580
2581/*
2582 * you must have down()ed the appropriate [m]sock_mutex elsewhere!
2583 */
2584int drbd_send(struct drbd_conf *mdev, struct socket *sock,
2585 void *buf, size_t size, unsigned msg_flags)
2586{
2587 struct kvec iov;
2588 struct msghdr msg;
2589 int rv, sent = 0;
2590
2591 if (!sock)
2592 return -1000;
2593
2594 /* THINK if (signal_pending) return ... ? */
2595
2596 iov.iov_base = buf;
2597 iov.iov_len = size;
2598
2599 msg.msg_name = NULL;
2600 msg.msg_namelen = 0;
2601 msg.msg_control = NULL;
2602 msg.msg_controllen = 0;
2603 msg.msg_flags = msg_flags | MSG_NOSIGNAL;
2604
2605 if (sock == mdev->data.socket) {
2606 mdev->ko_count = mdev->net_conf->ko_count;
2607 drbd_update_congested(mdev);
2608 }
2609 do {
2610 /* STRANGE
2611 * tcp_sendmsg does _not_ use its size parameter at all ?
2612 *
2613 * -EAGAIN on timeout, -EINTR on signal.
2614 */
2615/* THINK
2616 * do we need to block DRBD_SIG if sock == &meta.socket ??
2617 * otherwise wake_asender() might interrupt some send_*Ack !
2618 */
2619 rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
2620 if (rv == -EAGAIN) {
2621 if (we_should_drop_the_connection(mdev, sock))
2622 break;
2623 else
2624 continue;
2625 }
2626 D_ASSERT(rv != 0);
2627 if (rv == -EINTR) {
2628 flush_signals(current);
2629 rv = 0;
2630 }
2631 if (rv < 0)
2632 break;
2633 sent += rv;
2634 iov.iov_base += rv;
2635 iov.iov_len -= rv;
2636 } while (sent < size);
2637
2638 if (sock == mdev->data.socket)
2639 clear_bit(NET_CONGESTED, &mdev->flags);
2640
2641 if (rv <= 0) {
2642 if (rv != -EAGAIN) {
2643 dev_err(DEV, "%s_sendmsg returned %d\n",
2644 sock == mdev->meta.socket ? "msock" : "sock",
2645 rv);
2646 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
2647 } else
2648 drbd_force_state(mdev, NS(conn, C_TIMEOUT));
2649 }
2650
2651 return sent;
2652}
2653
2654static int drbd_open(struct block_device *bdev, fmode_t mode)
2655{
2656 struct drbd_conf *mdev = bdev->bd_disk->private_data;
2657 unsigned long flags;
2658 int rv = 0;
2659
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002660 lock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002661 spin_lock_irqsave(&mdev->req_lock, flags);
2662 /* to have a stable mdev->state.role
2663 * and no race with updating open_cnt */
2664
2665 if (mdev->state.role != R_PRIMARY) {
2666 if (mode & FMODE_WRITE)
2667 rv = -EROFS;
2668 else if (!allow_oos)
2669 rv = -EMEDIUMTYPE;
2670 }
2671
2672 if (!rv)
2673 mdev->open_cnt++;
2674 spin_unlock_irqrestore(&mdev->req_lock, flags);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002675 unlock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002676
2677 return rv;
2678}
2679
2680static int drbd_release(struct gendisk *gd, fmode_t mode)
2681{
2682 struct drbd_conf *mdev = gd->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002683 lock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002684 mdev->open_cnt--;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002685 unlock_kernel();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002686 return 0;
2687}
2688
2689static void drbd_unplug_fn(struct request_queue *q)
2690{
2691 struct drbd_conf *mdev = q->queuedata;
2692
Philipp Reisnerb411b362009-09-25 16:07:19 -07002693 /* unplug FIRST */
2694 spin_lock_irq(q->queue_lock);
2695 blk_remove_plug(q);
2696 spin_unlock_irq(q->queue_lock);
2697
2698 /* only if connected */
2699 spin_lock_irq(&mdev->req_lock);
2700 if (mdev->state.pdsk >= D_INCONSISTENT && mdev->state.conn >= C_CONNECTED) {
2701 D_ASSERT(mdev->state.role == R_PRIMARY);
2702 if (test_and_clear_bit(UNPLUG_REMOTE, &mdev->flags)) {
2703 /* add to the data.work queue,
2704 * unless already queued.
2705 * XXX this might be a good addition to drbd_queue_work
2706 * anyways, to detect "double queuing" ... */
2707 if (list_empty(&mdev->unplug_work.list))
2708 drbd_queue_work(&mdev->data.work,
2709 &mdev->unplug_work);
2710 }
2711 }
2712 spin_unlock_irq(&mdev->req_lock);
2713
2714 if (mdev->state.disk >= D_INCONSISTENT)
2715 drbd_kick_lo(mdev);
2716}
2717
2718static void drbd_set_defaults(struct drbd_conf *mdev)
2719{
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002720 /* This way we get a compile error when sync_conf grows,
2721 and we forgot to initialize it here */
2722 mdev->sync_conf = (struct syncer_conf) {
2723 /* .rate = */ DRBD_RATE_DEF,
2724 /* .after = */ DRBD_AFTER_DEF,
2725 /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002726 /* .verify_alg = */ {}, 0,
2727 /* .cpu_mask = */ {}, 0,
2728 /* .csums_alg = */ {}, 0,
Philipp Reisnere7564142010-06-29 17:35:34 +02002729 /* .use_rle = */ 0,
Philipp Reisner9a31d712010-07-05 13:42:03 +02002730 /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
2731 /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
2732 /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
2733 /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002734 /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
2735 /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
Philipp Reisner85f4cc12010-06-29 17:35:34 +02002736 };
2737
2738 /* Have to use that way, because the layout differs between
2739 big endian and little endian */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740 mdev->state = (union drbd_state) {
2741 { .role = R_SECONDARY,
2742 .peer = R_UNKNOWN,
2743 .conn = C_STANDALONE,
2744 .disk = D_DISKLESS,
2745 .pdsk = D_UNKNOWN,
2746 .susp = 0
2747 } };
2748}
2749
2750void drbd_init_set_defaults(struct drbd_conf *mdev)
2751{
2752 /* the memset(,0,) did most of this.
2753 * note: only assignments, no allocation in here */
2754
2755 drbd_set_defaults(mdev);
2756
2757 /* for now, we do NOT yet support it,
2758 * even though we start some framework
2759 * to eventually support barriers */
2760 set_bit(NO_BARRIER_SUPP, &mdev->flags);
2761
2762 atomic_set(&mdev->ap_bio_cnt, 0);
2763 atomic_set(&mdev->ap_pending_cnt, 0);
2764 atomic_set(&mdev->rs_pending_cnt, 0);
2765 atomic_set(&mdev->unacked_cnt, 0);
2766 atomic_set(&mdev->local_cnt, 0);
2767 atomic_set(&mdev->net_cnt, 0);
2768 atomic_set(&mdev->packet_seq, 0);
2769 atomic_set(&mdev->pp_in_use, 0);
Philipp Reisner778f2712010-07-06 11:14:00 +02002770 atomic_set(&mdev->rs_sect_in, 0);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002771 atomic_set(&mdev->rs_sect_ev, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772
2773 mutex_init(&mdev->md_io_mutex);
2774 mutex_init(&mdev->data.mutex);
2775 mutex_init(&mdev->meta.mutex);
2776 sema_init(&mdev->data.work.s, 0);
2777 sema_init(&mdev->meta.work.s, 0);
2778 mutex_init(&mdev->state_mutex);
2779
2780 spin_lock_init(&mdev->data.work.q_lock);
2781 spin_lock_init(&mdev->meta.work.q_lock);
2782
2783 spin_lock_init(&mdev->al_lock);
2784 spin_lock_init(&mdev->req_lock);
2785 spin_lock_init(&mdev->peer_seq_lock);
2786 spin_lock_init(&mdev->epoch_lock);
2787
2788 INIT_LIST_HEAD(&mdev->active_ee);
2789 INIT_LIST_HEAD(&mdev->sync_ee);
2790 INIT_LIST_HEAD(&mdev->done_ee);
2791 INIT_LIST_HEAD(&mdev->read_ee);
2792 INIT_LIST_HEAD(&mdev->net_ee);
2793 INIT_LIST_HEAD(&mdev->resync_reads);
2794 INIT_LIST_HEAD(&mdev->data.work.q);
2795 INIT_LIST_HEAD(&mdev->meta.work.q);
2796 INIT_LIST_HEAD(&mdev->resync_work.list);
2797 INIT_LIST_HEAD(&mdev->unplug_work.list);
2798 INIT_LIST_HEAD(&mdev->md_sync_work.list);
2799 INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
Philipp Reisner0ced55a2010-04-30 15:26:20 +02002800
Philipp Reisnerb411b362009-09-25 16:07:19 -07002801 mdev->resync_work.cb = w_resync_inactive;
2802 mdev->unplug_work.cb = w_send_write_hint;
2803 mdev->md_sync_work.cb = w_md_sync;
2804 mdev->bm_io_work.w.cb = w_bitmap_io;
2805 init_timer(&mdev->resync_timer);
2806 init_timer(&mdev->md_sync_timer);
2807 mdev->resync_timer.function = resync_timer_fn;
2808 mdev->resync_timer.data = (unsigned long) mdev;
2809 mdev->md_sync_timer.function = md_sync_timer_fn;
2810 mdev->md_sync_timer.data = (unsigned long) mdev;
2811
2812 init_waitqueue_head(&mdev->misc_wait);
2813 init_waitqueue_head(&mdev->state_wait);
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02002814 init_waitqueue_head(&mdev->net_cnt_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002815 init_waitqueue_head(&mdev->ee_wait);
2816 init_waitqueue_head(&mdev->al_wait);
2817 init_waitqueue_head(&mdev->seq_wait);
2818
2819 drbd_thread_init(mdev, &mdev->receiver, drbdd_init);
2820 drbd_thread_init(mdev, &mdev->worker, drbd_worker);
2821 drbd_thread_init(mdev, &mdev->asender, drbd_asender);
2822
2823 mdev->agreed_pro_version = PRO_VERSION_MAX;
2824 mdev->write_ordering = WO_bio_barrier;
2825 mdev->resync_wenr = LC_FREE;
2826}
2827
2828void drbd_mdev_cleanup(struct drbd_conf *mdev)
2829{
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002830 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002831 if (mdev->receiver.t_state != None)
2832 dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
2833 mdev->receiver.t_state);
2834
2835 /* no need to lock it, I'm the only thread alive */
2836 if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
2837 dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
2838 mdev->al_writ_cnt =
2839 mdev->bm_writ_cnt =
2840 mdev->read_cnt =
2841 mdev->recv_cnt =
2842 mdev->send_cnt =
2843 mdev->writ_cnt =
2844 mdev->p_size =
2845 mdev->rs_start =
2846 mdev->rs_total =
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002847 mdev->rs_failed = 0;
2848 mdev->rs_last_events = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002849 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02002850 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2851 mdev->rs_mark_left[i] = 0;
2852 mdev->rs_mark_time[i] = 0;
2853 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002854 D_ASSERT(mdev->net_conf == NULL);
2855
2856 drbd_set_my_capacity(mdev, 0);
2857 if (mdev->bitmap) {
2858 /* maybe never allocated. */
Philipp Reisner02d9a942010-03-24 16:23:03 +01002859 drbd_bm_resize(mdev, 0, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002860 drbd_bm_cleanup(mdev);
2861 }
2862
2863 drbd_free_resources(mdev);
Philipp Reisner07782862010-08-31 12:00:50 +02002864 clear_bit(AL_SUSPENDED, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002865
2866 /*
2867 * currently we drbd_init_ee only on module load, so
2868 * we may do drbd_release_ee only on module unload!
2869 */
2870 D_ASSERT(list_empty(&mdev->active_ee));
2871 D_ASSERT(list_empty(&mdev->sync_ee));
2872 D_ASSERT(list_empty(&mdev->done_ee));
2873 D_ASSERT(list_empty(&mdev->read_ee));
2874 D_ASSERT(list_empty(&mdev->net_ee));
2875 D_ASSERT(list_empty(&mdev->resync_reads));
2876 D_ASSERT(list_empty(&mdev->data.work.q));
2877 D_ASSERT(list_empty(&mdev->meta.work.q));
2878 D_ASSERT(list_empty(&mdev->resync_work.list));
2879 D_ASSERT(list_empty(&mdev->unplug_work.list));
2880
2881}
2882
2883
2884static void drbd_destroy_mempools(void)
2885{
2886 struct page *page;
2887
2888 while (drbd_pp_pool) {
2889 page = drbd_pp_pool;
2890 drbd_pp_pool = (struct page *)page_private(page);
2891 __free_page(page);
2892 drbd_pp_vacant--;
2893 }
2894
2895 /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
2896
2897 if (drbd_ee_mempool)
2898 mempool_destroy(drbd_ee_mempool);
2899 if (drbd_request_mempool)
2900 mempool_destroy(drbd_request_mempool);
2901 if (drbd_ee_cache)
2902 kmem_cache_destroy(drbd_ee_cache);
2903 if (drbd_request_cache)
2904 kmem_cache_destroy(drbd_request_cache);
2905 if (drbd_bm_ext_cache)
2906 kmem_cache_destroy(drbd_bm_ext_cache);
2907 if (drbd_al_ext_cache)
2908 kmem_cache_destroy(drbd_al_ext_cache);
2909
2910 drbd_ee_mempool = NULL;
2911 drbd_request_mempool = NULL;
2912 drbd_ee_cache = NULL;
2913 drbd_request_cache = NULL;
2914 drbd_bm_ext_cache = NULL;
2915 drbd_al_ext_cache = NULL;
2916
2917 return;
2918}
2919
2920static int drbd_create_mempools(void)
2921{
2922 struct page *page;
2923 const int number = (DRBD_MAX_SEGMENT_SIZE/PAGE_SIZE) * minor_count;
2924 int i;
2925
2926 /* prepare our caches and mempools */
2927 drbd_request_mempool = NULL;
2928 drbd_ee_cache = NULL;
2929 drbd_request_cache = NULL;
2930 drbd_bm_ext_cache = NULL;
2931 drbd_al_ext_cache = NULL;
2932 drbd_pp_pool = NULL;
2933
2934 /* caches */
2935 drbd_request_cache = kmem_cache_create(
2936 "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
2937 if (drbd_request_cache == NULL)
2938 goto Enomem;
2939
2940 drbd_ee_cache = kmem_cache_create(
2941 "drbd_ee", sizeof(struct drbd_epoch_entry), 0, 0, NULL);
2942 if (drbd_ee_cache == NULL)
2943 goto Enomem;
2944
2945 drbd_bm_ext_cache = kmem_cache_create(
2946 "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
2947 if (drbd_bm_ext_cache == NULL)
2948 goto Enomem;
2949
2950 drbd_al_ext_cache = kmem_cache_create(
2951 "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
2952 if (drbd_al_ext_cache == NULL)
2953 goto Enomem;
2954
2955 /* mempools */
2956 drbd_request_mempool = mempool_create(number,
2957 mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
2958 if (drbd_request_mempool == NULL)
2959 goto Enomem;
2960
2961 drbd_ee_mempool = mempool_create(number,
2962 mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
2963 if (drbd_request_mempool == NULL)
2964 goto Enomem;
2965
2966 /* drbd's page pool */
2967 spin_lock_init(&drbd_pp_lock);
2968
2969 for (i = 0; i < number; i++) {
2970 page = alloc_page(GFP_HIGHUSER);
2971 if (!page)
2972 goto Enomem;
2973 set_page_private(page, (unsigned long)drbd_pp_pool);
2974 drbd_pp_pool = page;
2975 }
2976 drbd_pp_vacant = number;
2977
2978 return 0;
2979
2980Enomem:
2981 drbd_destroy_mempools(); /* in case we allocated some */
2982 return -ENOMEM;
2983}
2984
2985static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
2986 void *unused)
2987{
2988 /* just so we have it. you never know what interesting things we
2989 * might want to do here some day...
2990 */
2991
2992 return NOTIFY_DONE;
2993}
2994
2995static struct notifier_block drbd_notifier = {
2996 .notifier_call = drbd_notify_sys,
2997};
2998
2999static void drbd_release_ee_lists(struct drbd_conf *mdev)
3000{
3001 int rr;
3002
3003 rr = drbd_release_ee(mdev, &mdev->active_ee);
3004 if (rr)
3005 dev_err(DEV, "%d EEs in active list found!\n", rr);
3006
3007 rr = drbd_release_ee(mdev, &mdev->sync_ee);
3008 if (rr)
3009 dev_err(DEV, "%d EEs in sync list found!\n", rr);
3010
3011 rr = drbd_release_ee(mdev, &mdev->read_ee);
3012 if (rr)
3013 dev_err(DEV, "%d EEs in read list found!\n", rr);
3014
3015 rr = drbd_release_ee(mdev, &mdev->done_ee);
3016 if (rr)
3017 dev_err(DEV, "%d EEs in done list found!\n", rr);
3018
3019 rr = drbd_release_ee(mdev, &mdev->net_ee);
3020 if (rr)
3021 dev_err(DEV, "%d EEs in net list found!\n", rr);
3022}
3023
3024/* caution. no locking.
3025 * currently only used from module cleanup code. */
3026static void drbd_delete_device(unsigned int minor)
3027{
3028 struct drbd_conf *mdev = minor_to_mdev(minor);
3029
3030 if (!mdev)
3031 return;
3032
3033 /* paranoia asserts */
3034 if (mdev->open_cnt != 0)
3035 dev_err(DEV, "open_cnt = %d in %s:%u", mdev->open_cnt,
3036 __FILE__ , __LINE__);
3037
3038 ERR_IF (!list_empty(&mdev->data.work.q)) {
3039 struct list_head *lp;
3040 list_for_each(lp, &mdev->data.work.q) {
3041 dev_err(DEV, "lp = %p\n", lp);
3042 }
3043 };
3044 /* end paranoia asserts */
3045
3046 del_gendisk(mdev->vdisk);
3047
3048 /* cleanup stuff that may have been allocated during
3049 * device (re-)configuration or state changes */
3050
3051 if (mdev->this_bdev)
3052 bdput(mdev->this_bdev);
3053
3054 drbd_free_resources(mdev);
3055
3056 drbd_release_ee_lists(mdev);
3057
3058 /* should be free'd on disconnect? */
3059 kfree(mdev->ee_hash);
3060 /*
3061 mdev->ee_hash_s = 0;
3062 mdev->ee_hash = NULL;
3063 */
3064
3065 lc_destroy(mdev->act_log);
3066 lc_destroy(mdev->resync);
3067
3068 kfree(mdev->p_uuid);
3069 /* mdev->p_uuid = NULL; */
3070
3071 kfree(mdev->int_dig_out);
3072 kfree(mdev->int_dig_in);
3073 kfree(mdev->int_dig_vv);
3074
3075 /* cleanup the rest that has been
3076 * allocated from drbd_new_device
3077 * and actually free the mdev itself */
3078 drbd_free_mdev(mdev);
3079}
3080
3081static void drbd_cleanup(void)
3082{
3083 unsigned int i;
3084
3085 unregister_reboot_notifier(&drbd_notifier);
3086
3087 drbd_nl_cleanup();
3088
3089 if (minor_table) {
3090 if (drbd_proc)
3091 remove_proc_entry("drbd", NULL);
3092 i = minor_count;
3093 while (i--)
3094 drbd_delete_device(i);
3095 drbd_destroy_mempools();
3096 }
3097
3098 kfree(minor_table);
3099
3100 unregister_blkdev(DRBD_MAJOR, "drbd");
3101
3102 printk(KERN_INFO "drbd: module cleanup done.\n");
3103}
3104
3105/**
3106 * drbd_congested() - Callback for pdflush
3107 * @congested_data: User data
3108 * @bdi_bits: Bits pdflush is currently interested in
3109 *
3110 * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
3111 */
3112static int drbd_congested(void *congested_data, int bdi_bits)
3113{
3114 struct drbd_conf *mdev = congested_data;
3115 struct request_queue *q;
3116 char reason = '-';
3117 int r = 0;
3118
3119 if (!__inc_ap_bio_cond(mdev)) {
3120 /* DRBD has frozen IO */
3121 r = bdi_bits;
3122 reason = 'd';
3123 goto out;
3124 }
3125
3126 if (get_ldev(mdev)) {
3127 q = bdev_get_queue(mdev->ldev->backing_bdev);
3128 r = bdi_congested(&q->backing_dev_info, bdi_bits);
3129 put_ldev(mdev);
3130 if (r)
3131 reason = 'b';
3132 }
3133
3134 if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->flags)) {
3135 r |= (1 << BDI_async_congested);
3136 reason = reason == 'b' ? 'a' : 'n';
3137 }
3138
3139out:
3140 mdev->congestion_reason = reason;
3141 return r;
3142}
3143
3144struct drbd_conf *drbd_new_device(unsigned int minor)
3145{
3146 struct drbd_conf *mdev;
3147 struct gendisk *disk;
3148 struct request_queue *q;
3149
3150 /* GFP_KERNEL, we are outside of all write-out paths */
3151 mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
3152 if (!mdev)
3153 return NULL;
3154 if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL))
3155 goto out_no_cpumask;
3156
3157 mdev->minor = minor;
3158
3159 drbd_init_set_defaults(mdev);
3160
3161 q = blk_alloc_queue(GFP_KERNEL);
3162 if (!q)
3163 goto out_no_q;
3164 mdev->rq_queue = q;
3165 q->queuedata = mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003166
3167 disk = alloc_disk(1);
3168 if (!disk)
3169 goto out_no_disk;
3170 mdev->vdisk = disk;
3171
3172 set_disk_ro(disk, TRUE);
3173
3174 disk->queue = q;
3175 disk->major = DRBD_MAJOR;
3176 disk->first_minor = minor;
3177 disk->fops = &drbd_ops;
3178 sprintf(disk->disk_name, "drbd%d", minor);
3179 disk->private_data = mdev;
3180
3181 mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
3182 /* we have no partitions. we contain only ourselves. */
3183 mdev->this_bdev->bd_contains = mdev->this_bdev;
3184
3185 q->backing_dev_info.congested_fn = drbd_congested;
3186 q->backing_dev_info.congested_data = mdev;
3187
3188 blk_queue_make_request(q, drbd_make_request_26);
Lars Ellenberg98ec2862010-01-21 19:33:14 +01003189 blk_queue_max_segment_size(q, DRBD_MAX_SEGMENT_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003190 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
3191 blk_queue_merge_bvec(q, drbd_merge_bvec);
3192 q->queue_lock = &mdev->req_lock; /* needed since we use */
3193 /* plugging on a queue, that actually has no requests! */
3194 q->unplug_fn = drbd_unplug_fn;
3195
3196 mdev->md_io_page = alloc_page(GFP_KERNEL);
3197 if (!mdev->md_io_page)
3198 goto out_no_io_page;
3199
3200 if (drbd_bm_init(mdev))
3201 goto out_no_bitmap;
3202 /* no need to lock access, we are still initializing this minor device. */
3203 if (!tl_init(mdev))
3204 goto out_no_tl;
3205
3206 mdev->app_reads_hash = kzalloc(APP_R_HSIZE*sizeof(void *), GFP_KERNEL);
3207 if (!mdev->app_reads_hash)
3208 goto out_no_app_reads;
3209
3210 mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
3211 if (!mdev->current_epoch)
3212 goto out_no_epoch;
3213
3214 INIT_LIST_HEAD(&mdev->current_epoch->list);
3215 mdev->epochs = 1;
3216
3217 return mdev;
3218
3219/* out_whatever_else:
3220 kfree(mdev->current_epoch); */
3221out_no_epoch:
3222 kfree(mdev->app_reads_hash);
3223out_no_app_reads:
3224 tl_cleanup(mdev);
3225out_no_tl:
3226 drbd_bm_cleanup(mdev);
3227out_no_bitmap:
3228 __free_page(mdev->md_io_page);
3229out_no_io_page:
3230 put_disk(disk);
3231out_no_disk:
3232 blk_cleanup_queue(q);
3233out_no_q:
3234 free_cpumask_var(mdev->cpu_mask);
3235out_no_cpumask:
3236 kfree(mdev);
3237 return NULL;
3238}
3239
3240/* counterpart of drbd_new_device.
3241 * last part of drbd_delete_device. */
3242void drbd_free_mdev(struct drbd_conf *mdev)
3243{
3244 kfree(mdev->current_epoch);
3245 kfree(mdev->app_reads_hash);
3246 tl_cleanup(mdev);
3247 if (mdev->bitmap) /* should no longer be there. */
3248 drbd_bm_cleanup(mdev);
3249 __free_page(mdev->md_io_page);
3250 put_disk(mdev->vdisk);
3251 blk_cleanup_queue(mdev->rq_queue);
3252 free_cpumask_var(mdev->cpu_mask);
3253 kfree(mdev);
3254}
3255
3256
3257int __init drbd_init(void)
3258{
3259 int err;
3260
3261 if (sizeof(struct p_handshake) != 80) {
3262 printk(KERN_ERR
3263 "drbd: never change the size or layout "
3264 "of the HandShake packet.\n");
3265 return -EINVAL;
3266 }
3267
3268 if (1 > minor_count || minor_count > 255) {
3269 printk(KERN_ERR
3270 "drbd: invalid minor_count (%d)\n", minor_count);
3271#ifdef MODULE
3272 return -EINVAL;
3273#else
3274 minor_count = 8;
3275#endif
3276 }
3277
3278 err = drbd_nl_init();
3279 if (err)
3280 return err;
3281
3282 err = register_blkdev(DRBD_MAJOR, "drbd");
3283 if (err) {
3284 printk(KERN_ERR
3285 "drbd: unable to register block device major %d\n",
3286 DRBD_MAJOR);
3287 return err;
3288 }
3289
3290 register_reboot_notifier(&drbd_notifier);
3291
3292 /*
3293 * allocate all necessary structs
3294 */
3295 err = -ENOMEM;
3296
3297 init_waitqueue_head(&drbd_pp_wait);
3298
3299 drbd_proc = NULL; /* play safe for drbd_cleanup */
3300 minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
3301 GFP_KERNEL);
3302 if (!minor_table)
3303 goto Enomem;
3304
3305 err = drbd_create_mempools();
3306 if (err)
3307 goto Enomem;
3308
Lars Ellenberg8c484ee2010-03-11 16:47:58 +01003309 drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003310 if (!drbd_proc) {
3311 printk(KERN_ERR "drbd: unable to register proc file\n");
3312 goto Enomem;
3313 }
3314
3315 rwlock_init(&global_state_lock);
3316
3317 printk(KERN_INFO "drbd: initialized. "
3318 "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
3319 API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
3320 printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
3321 printk(KERN_INFO "drbd: registered as block device major %d\n",
3322 DRBD_MAJOR);
3323 printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
3324
3325 return 0; /* Success! */
3326
3327Enomem:
3328 drbd_cleanup();
3329 if (err == -ENOMEM)
3330 /* currently always the case */
3331 printk(KERN_ERR "drbd: ran out of memory\n");
3332 else
3333 printk(KERN_ERR "drbd: initialization failure\n");
3334 return err;
3335}
3336
3337void drbd_free_bc(struct drbd_backing_dev *ldev)
3338{
3339 if (ldev == NULL)
3340 return;
3341
3342 bd_release(ldev->backing_bdev);
3343 bd_release(ldev->md_bdev);
3344
3345 fput(ldev->lo_file);
3346 fput(ldev->md_file);
3347
3348 kfree(ldev);
3349}
3350
3351void drbd_free_sock(struct drbd_conf *mdev)
3352{
3353 if (mdev->data.socket) {
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003354 mutex_lock(&mdev->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003355 kernel_sock_shutdown(mdev->data.socket, SHUT_RDWR);
3356 sock_release(mdev->data.socket);
3357 mdev->data.socket = NULL;
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003358 mutex_unlock(&mdev->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003359 }
3360 if (mdev->meta.socket) {
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003361 mutex_lock(&mdev->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003362 kernel_sock_shutdown(mdev->meta.socket, SHUT_RDWR);
3363 sock_release(mdev->meta.socket);
3364 mdev->meta.socket = NULL;
Lars Ellenberg4589d7f2010-03-03 02:25:33 +01003365 mutex_unlock(&mdev->meta.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003366 }
3367}
3368
3369
3370void drbd_free_resources(struct drbd_conf *mdev)
3371{
3372 crypto_free_hash(mdev->csums_tfm);
3373 mdev->csums_tfm = NULL;
3374 crypto_free_hash(mdev->verify_tfm);
3375 mdev->verify_tfm = NULL;
3376 crypto_free_hash(mdev->cram_hmac_tfm);
3377 mdev->cram_hmac_tfm = NULL;
3378 crypto_free_hash(mdev->integrity_w_tfm);
3379 mdev->integrity_w_tfm = NULL;
3380 crypto_free_hash(mdev->integrity_r_tfm);
3381 mdev->integrity_r_tfm = NULL;
3382
3383 drbd_free_sock(mdev);
3384
3385 __no_warn(local,
3386 drbd_free_bc(mdev->ldev);
3387 mdev->ldev = NULL;);
3388}
3389
3390/* meta data management */
3391
3392struct meta_data_on_disk {
3393 u64 la_size; /* last agreed size. */
3394 u64 uuid[UI_SIZE]; /* UUIDs. */
3395 u64 device_uuid;
3396 u64 reserved_u64_1;
3397 u32 flags; /* MDF */
3398 u32 magic;
3399 u32 md_size_sect;
3400 u32 al_offset; /* offset to this block */
3401 u32 al_nr_extents; /* important for restoring the AL */
3402 /* `-- act_log->nr_elements <-- sync_conf.al_extents */
3403 u32 bm_offset; /* offset to the bitmap, from here */
3404 u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
3405 u32 reserved_u32[4];
3406
3407} __packed;
3408
3409/**
3410 * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
3411 * @mdev: DRBD device.
3412 */
3413void drbd_md_sync(struct drbd_conf *mdev)
3414{
3415 struct meta_data_on_disk *buffer;
3416 sector_t sector;
3417 int i;
3418
3419 if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
3420 return;
3421 del_timer(&mdev->md_sync_timer);
3422
3423 /* We use here D_FAILED and not D_ATTACHING because we try to write
3424 * metadata even if we detach due to a disk failure! */
3425 if (!get_ldev_if_state(mdev, D_FAILED))
3426 return;
3427
Philipp Reisnerb411b362009-09-25 16:07:19 -07003428 mutex_lock(&mdev->md_io_mutex);
3429 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
3430 memset(buffer, 0, 512);
3431
3432 buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
3433 for (i = UI_CURRENT; i < UI_SIZE; i++)
3434 buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
3435 buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
3436 buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
3437
3438 buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
3439 buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
3440 buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
3441 buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
3442 buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
3443
3444 buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
3445
3446 D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
3447 sector = mdev->ldev->md.md_offset;
3448
3449 if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
3450 clear_bit(MD_DIRTY, &mdev->flags);
3451 } else {
3452 /* this was a try anyways ... */
3453 dev_err(DEV, "meta data update failed!\n");
3454
3455 drbd_chk_io_error(mdev, 1, TRUE);
3456 }
3457
3458 /* Update mdev->ldev->md.la_size_sect,
3459 * since we updated it on metadata. */
3460 mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
3461
3462 mutex_unlock(&mdev->md_io_mutex);
3463 put_ldev(mdev);
3464}
3465
3466/**
3467 * drbd_md_read() - Reads in the meta data super block
3468 * @mdev: DRBD device.
3469 * @bdev: Device from which the meta data should be read in.
3470 *
3471 * Return 0 (NO_ERROR) on success, and an enum drbd_ret_codes in case
3472 * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
3473 */
3474int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
3475{
3476 struct meta_data_on_disk *buffer;
3477 int i, rv = NO_ERROR;
3478
3479 if (!get_ldev_if_state(mdev, D_ATTACHING))
3480 return ERR_IO_MD_DISK;
3481
Philipp Reisnerb411b362009-09-25 16:07:19 -07003482 mutex_lock(&mdev->md_io_mutex);
3483 buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
3484
3485 if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
3486 /* NOTE: cant do normal error processing here as this is
3487 called BEFORE disk is attached */
3488 dev_err(DEV, "Error while reading metadata.\n");
3489 rv = ERR_IO_MD_DISK;
3490 goto err;
3491 }
3492
3493 if (be32_to_cpu(buffer->magic) != DRBD_MD_MAGIC) {
3494 dev_err(DEV, "Error while reading metadata, magic not found.\n");
3495 rv = ERR_MD_INVALID;
3496 goto err;
3497 }
3498 if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
3499 dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
3500 be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
3501 rv = ERR_MD_INVALID;
3502 goto err;
3503 }
3504 if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
3505 dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
3506 be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
3507 rv = ERR_MD_INVALID;
3508 goto err;
3509 }
3510 if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
3511 dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
3512 be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
3513 rv = ERR_MD_INVALID;
3514 goto err;
3515 }
3516
3517 if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
3518 dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
3519 be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
3520 rv = ERR_MD_INVALID;
3521 goto err;
3522 }
3523
3524 bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
3525 for (i = UI_CURRENT; i < UI_SIZE; i++)
3526 bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
3527 bdev->md.flags = be32_to_cpu(buffer->flags);
3528 mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
3529 bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
3530
3531 if (mdev->sync_conf.al_extents < 7)
3532 mdev->sync_conf.al_extents = 127;
3533
3534 err:
3535 mutex_unlock(&mdev->md_io_mutex);
3536 put_ldev(mdev);
3537
3538 return rv;
3539}
3540
3541/**
3542 * drbd_md_mark_dirty() - Mark meta data super block as dirty
3543 * @mdev: DRBD device.
3544 *
3545 * Call this function if you change anything that should be written to
3546 * the meta-data super block. This function sets MD_DIRTY, and starts a
3547 * timer that ensures that within five seconds you have to call drbd_md_sync().
3548 */
3549void drbd_md_mark_dirty(struct drbd_conf *mdev)
3550{
3551 set_bit(MD_DIRTY, &mdev->flags);
3552 mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
3553}
3554
3555
3556static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
3557{
3558 int i;
3559
Jens Axboe6a0afdf2009-10-01 09:04:14 +02003560 for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003561 mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562}
3563
3564void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
3565{
3566 if (idx == UI_CURRENT) {
3567 if (mdev->state.role == R_PRIMARY)
3568 val |= 1;
3569 else
3570 val &= ~((u64)1);
3571
3572 drbd_set_ed_uuid(mdev, val);
3573 }
3574
3575 mdev->ldev->md.uuid[idx] = val;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 drbd_md_mark_dirty(mdev);
3577}
3578
3579
3580void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
3581{
3582 if (mdev->ldev->md.uuid[idx]) {
3583 drbd_uuid_move_history(mdev);
3584 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003585 }
3586 _drbd_uuid_set(mdev, idx, val);
3587}
3588
3589/**
3590 * drbd_uuid_new_current() - Creates a new current UUID
3591 * @mdev: DRBD device.
3592 *
3593 * Creates a new current UUID, and rotates the old current UUID into
3594 * the bitmap slot. Causes an incremental resync upon next connect.
3595 */
3596void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
3597{
3598 u64 val;
3599
3600 dev_info(DEV, "Creating new current UUID\n");
3601 D_ASSERT(mdev->ldev->md.uuid[UI_BITMAP] == 0);
3602 mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
Philipp Reisnerb411b362009-09-25 16:07:19 -07003603
3604 get_random_bytes(&val, sizeof(u64));
3605 _drbd_uuid_set(mdev, UI_CURRENT, val);
3606}
3607
3608void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
3609{
3610 if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
3611 return;
3612
3613 if (val == 0) {
3614 drbd_uuid_move_history(mdev);
3615 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
3616 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617 } else {
3618 if (mdev->ldev->md.uuid[UI_BITMAP])
3619 dev_warn(DEV, "bm UUID already set");
3620
3621 mdev->ldev->md.uuid[UI_BITMAP] = val;
3622 mdev->ldev->md.uuid[UI_BITMAP] &= ~((u64)1);
3623
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624 }
3625 drbd_md_mark_dirty(mdev);
3626}
3627
3628/**
3629 * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3630 * @mdev: DRBD device.
3631 *
3632 * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
3633 */
3634int drbd_bmio_set_n_write(struct drbd_conf *mdev)
3635{
3636 int rv = -EIO;
3637
3638 if (get_ldev_if_state(mdev, D_ATTACHING)) {
3639 drbd_md_set_flag(mdev, MDF_FULL_SYNC);
3640 drbd_md_sync(mdev);
3641 drbd_bm_set_all(mdev);
3642
3643 rv = drbd_bm_write(mdev);
3644
3645 if (!rv) {
3646 drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
3647 drbd_md_sync(mdev);
3648 }
3649
3650 put_ldev(mdev);
3651 }
3652
3653 return rv;
3654}
3655
3656/**
3657 * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
3658 * @mdev: DRBD device.
3659 *
3660 * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
3661 */
3662int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
3663{
3664 int rv = -EIO;
3665
Philipp Reisner07782862010-08-31 12:00:50 +02003666 drbd_resume_al(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003667 if (get_ldev_if_state(mdev, D_ATTACHING)) {
3668 drbd_bm_clear_all(mdev);
3669 rv = drbd_bm_write(mdev);
3670 put_ldev(mdev);
3671 }
3672
3673 return rv;
3674}
3675
3676static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
3677{
3678 struct bm_io_work *work = container_of(w, struct bm_io_work, w);
3679 int rv;
3680
3681 D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
3682
3683 drbd_bm_lock(mdev, work->why);
3684 rv = work->io_fn(mdev);
3685 drbd_bm_unlock(mdev);
3686
3687 clear_bit(BITMAP_IO, &mdev->flags);
3688 wake_up(&mdev->misc_wait);
3689
3690 if (work->done)
3691 work->done(mdev, rv);
3692
3693 clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
3694 work->why = NULL;
3695
3696 return 1;
3697}
3698
3699/**
3700 * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
3701 * @mdev: DRBD device.
3702 * @io_fn: IO callback to be called when bitmap IO is possible
3703 * @done: callback to be called after the bitmap IO was performed
3704 * @why: Descriptive text of the reason for doing the IO
3705 *
3706 * While IO on the bitmap happens we freeze application IO thus we ensure
3707 * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
3708 * called from worker context. It MUST NOT be used while a previous such
3709 * work is still pending!
3710 */
3711void drbd_queue_bitmap_io(struct drbd_conf *mdev,
3712 int (*io_fn)(struct drbd_conf *),
3713 void (*done)(struct drbd_conf *, int),
3714 char *why)
3715{
3716 D_ASSERT(current == mdev->worker.task);
3717
3718 D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
3719 D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
3720 D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
3721 if (mdev->bm_io_work.why)
3722 dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
3723 why, mdev->bm_io_work.why);
3724
3725 mdev->bm_io_work.io_fn = io_fn;
3726 mdev->bm_io_work.done = done;
3727 mdev->bm_io_work.why = why;
3728
3729 set_bit(BITMAP_IO, &mdev->flags);
3730 if (atomic_read(&mdev->ap_bio_cnt) == 0) {
3731 if (list_empty(&mdev->bm_io_work.w.list)) {
3732 set_bit(BITMAP_IO_QUEUED, &mdev->flags);
3733 drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
3734 } else
3735 dev_err(DEV, "FIXME avoided double queuing bm_io_work\n");
3736 }
3737}
3738
3739/**
3740 * drbd_bitmap_io() - Does an IO operation on the whole bitmap
3741 * @mdev: DRBD device.
3742 * @io_fn: IO callback to be called when bitmap IO is possible
3743 * @why: Descriptive text of the reason for doing the IO
3744 *
3745 * freezes application IO while that the actual IO operations runs. This
3746 * functions MAY NOT be called from worker context.
3747 */
3748int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *), char *why)
3749{
3750 int rv;
3751
3752 D_ASSERT(current != mdev->worker.task);
3753
3754 drbd_suspend_io(mdev);
3755
3756 drbd_bm_lock(mdev, why);
3757 rv = io_fn(mdev);
3758 drbd_bm_unlock(mdev);
3759
3760 drbd_resume_io(mdev);
3761
3762 return rv;
3763}
3764
3765void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3766{
3767 if ((mdev->ldev->md.flags & flag) != flag) {
3768 drbd_md_mark_dirty(mdev);
3769 mdev->ldev->md.flags |= flag;
3770 }
3771}
3772
3773void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
3774{
3775 if ((mdev->ldev->md.flags & flag) != 0) {
3776 drbd_md_mark_dirty(mdev);
3777 mdev->ldev->md.flags &= ~flag;
3778 }
3779}
3780int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
3781{
3782 return (bdev->md.flags & flag) != 0;
3783}
3784
3785static void md_sync_timer_fn(unsigned long data)
3786{
3787 struct drbd_conf *mdev = (struct drbd_conf *) data;
3788
3789 drbd_queue_work_front(&mdev->data.work, &mdev->md_sync_work);
3790}
3791
3792static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
3793{
3794 dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
3795 drbd_md_sync(mdev);
3796
3797 return 1;
3798}
3799
3800#ifdef CONFIG_DRBD_FAULT_INJECTION
3801/* Fault insertion support including random number generator shamelessly
3802 * stolen from kernel/rcutorture.c */
3803struct fault_random_state {
3804 unsigned long state;
3805 unsigned long count;
3806};
3807
3808#define FAULT_RANDOM_MULT 39916801 /* prime */
3809#define FAULT_RANDOM_ADD 479001701 /* prime */
3810#define FAULT_RANDOM_REFRESH 10000
3811
3812/*
3813 * Crude but fast random-number generator. Uses a linear congruential
3814 * generator, with occasional help from get_random_bytes().
3815 */
3816static unsigned long
3817_drbd_fault_random(struct fault_random_state *rsp)
3818{
3819 long refresh;
3820
Roel Kluin49829ea2009-12-15 22:55:44 +01003821 if (!rsp->count--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003822 get_random_bytes(&refresh, sizeof(refresh));
3823 rsp->state += refresh;
3824 rsp->count = FAULT_RANDOM_REFRESH;
3825 }
3826 rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
3827 return swahw32(rsp->state);
3828}
3829
3830static char *
3831_drbd_fault_str(unsigned int type) {
3832 static char *_faults[] = {
3833 [DRBD_FAULT_MD_WR] = "Meta-data write",
3834 [DRBD_FAULT_MD_RD] = "Meta-data read",
3835 [DRBD_FAULT_RS_WR] = "Resync write",
3836 [DRBD_FAULT_RS_RD] = "Resync read",
3837 [DRBD_FAULT_DT_WR] = "Data write",
3838 [DRBD_FAULT_DT_RD] = "Data read",
3839 [DRBD_FAULT_DT_RA] = "Data read ahead",
3840 [DRBD_FAULT_BM_ALLOC] = "BM allocation",
Philipp Reisner6b4388a2010-04-26 14:11:45 +02003841 [DRBD_FAULT_AL_EE] = "EE allocation",
3842 [DRBD_FAULT_RECEIVE] = "receive data corruption",
Philipp Reisnerb411b362009-09-25 16:07:19 -07003843 };
3844
3845 return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
3846}
3847
3848unsigned int
3849_drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
3850{
3851 static struct fault_random_state rrs = {0, 0};
3852
3853 unsigned int ret = (
3854 (fault_devs == 0 ||
3855 ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
3856 (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
3857
3858 if (ret) {
3859 fault_count++;
3860
Lars Ellenberg73835062010-05-27 11:51:56 +02003861 if (__ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003862 dev_warn(DEV, "***Simulating %s failure\n",
3863 _drbd_fault_str(type));
3864 }
3865
3866 return ret;
3867}
3868#endif
3869
3870const char *drbd_buildtag(void)
3871{
3872 /* DRBD built from external sources has here a reference to the
3873 git hash of the source code. */
3874
3875 static char buildtag[38] = "\0uilt-in";
3876
3877 if (buildtag[0] == 0) {
3878#ifdef CONFIG_MODULES
3879 if (THIS_MODULE != NULL)
3880 sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
3881 else
3882#endif
3883 buildtag[0] = 'b';
3884 }
3885
3886 return buildtag;
3887}
3888
3889module_init(drbd_init)
3890module_exit(drbd_cleanup)
3891
Philipp Reisnerb411b362009-09-25 16:07:19 -07003892EXPORT_SYMBOL(drbd_conn_str);
3893EXPORT_SYMBOL(drbd_role_str);
3894EXPORT_SYMBOL(drbd_disk_str);
3895EXPORT_SYMBOL(drbd_set_st_err_str);