drbd_worker.c revision 85719573dd716bc2ac3e098b44adfed884250bab
1b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/*
2b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd_worker.c
3b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
4b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd is free software; you can redistribute it and/or modify
11b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   it under the terms of the GNU General Public License as published by
12b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   the Free Software Foundation; either version 2, or (at your option)
13b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   any later version.
14b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
15b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd is distributed in the hope that it will be useful,
16b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   but WITHOUT ANY WARRANTY; without even the implied warranty of
17b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   GNU General Public License for more details.
19b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
20b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   You should have received a copy of the GNU General Public License
21b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   along with drbd; see the file COPYING.  If not, write to
22b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
24b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
25b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
26b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/module.h>
27b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/drbd.h>
28b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/sched.h>
29b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/smp_lock.h>
30b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/wait.h>
31b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm.h>
32b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/memcontrol.h>
33b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm_inline.h>
34b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/slab.h>
35b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/random.h>
36b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/string.h>
37b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/scatterlist.h>
38b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
39b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_int.h"
40b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_req.h"
41b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
42b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel);
43b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
44b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
45b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
46b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* defined here:
47b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd_md_io_complete
4845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg   drbd_endio_sec
49b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd_endio_pri
50b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
51b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * more endio handlers:
52b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   atodb_endio in drbd_actlog.c
53b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   drbd_bm_async_io_complete in drbd_bitmap.c
54b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
55b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * For all these callbacks, note the following:
56b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * The callbacks will be called in irq context by the IDE drivers,
57b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * and in Softirqs/Tasklets/BH context by the SCSI drivers.
58b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Try to get the locking right :)
59b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
60b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
61b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
62b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
63b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* About the global_state_lock
64b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Each state transition on an device holds a read lock. In case we have
65b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   to evaluate the sync after dependencies, we grab a write lock, because
66b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   we need stable states on all devices for that.  */
67b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerrwlock_t global_state_lock;
68b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
69b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* used for synchronous meta data and bitmap IO
70b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * submitted by drbd_md_sync_page_io()
71b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
72b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_md_io_complete(struct bio *bio, int error)
73b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
74b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_md_io *md_io;
75b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
76b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io = (struct drbd_md_io *)bio->bi_private;
77b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io->error = error;
78b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
79b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&md_io->event);
80b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
81b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
82b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* reads on behalf of the partner,
83b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * "submitted" by the receiver
84b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
8545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_endio_read_sec_final(struct drbd_epoch_entry *e) __releases(local)
86b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
87b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
8845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
89b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
90b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(e->block_id != ID_VACANT);
91b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
92b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irqsave(&mdev->req_lock, flags);
93b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->read_cnt += e->size >> 9;
94b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_del(&e->w.list);
95b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (list_empty(&mdev->read_ee))
96b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
9745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (test_bit(__EE_WAS_ERROR, &e->flags))
9845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		__drbd_chk_io_error(mdev, FALSE);
99b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irqrestore(&mdev->req_lock, flags);
100b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
101b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_queue_work(&mdev->data.work, &e->w);
102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergstatic int is_failed_barrier(int ee_flags)
10645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
10745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	return (ee_flags & (EE_IS_BARRIER|EE_WAS_ERROR|EE_RESUBMITTED))
10845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			== (EE_IS_BARRIER|EE_WAS_ERROR);
10945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
11045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* writes on behalf of the partner, or resync writes,
11245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver, final stage.  */
11345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergstatic void drbd_endio_write_sec_final(struct drbd_epoch_entry *e) __releases(local)
114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
11645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t e_sector;
118b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_wake;
119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int is_syncer_req;
120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_al_complete_io;
121b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* if this is a failed barrier request, disable use of barriers,
12345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	 * and schedule for resubmission */
12445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (is_failed_barrier(e->flags)) {
125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_bump_write_ordering(mdev, WO_bdev_flush);
126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_lock_irqsave(&mdev->req_lock, flags);
127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_del(&e->w.list);
128fc8ce1941d668c70e57a07f13f5a63e73e5dbff3Philipp Reisner		e->flags = (e->flags & ~EE_WAS_ERROR) | EE_RESUBMITTED;
129b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		e->w.cb = w_e_reissue;
130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* put_ldev actually happens below, once we come here again. */
131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		__release(local);
132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_unlock_irqrestore(&mdev->req_lock, flags);
133b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_queue_work(&mdev->data.work, &e->w);
134b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
135b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
137b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(e->block_id != ID_VACANT);
138b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after we moved e to done_ee,
140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * we may no longer access it,
141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * it may be freed/reused already!
142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (as soon as we release the req_lock) */
143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	e_sector = e->sector;
144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	do_al_complete_io = e->flags & EE_CALL_AL_COMPLETE_IO;
14545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	is_syncer_req = is_syncer_block_id(e->block_id);
146b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
14745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	spin_lock_irqsave(&mdev->req_lock, flags);
14845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	mdev->writ_cnt += e->size >> 9;
149b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_del(&e->w.list); /* has been on active_ee or sync_ee */
150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_add_tail(&e->w.list, &mdev->done_ee);
151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
152b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* No hlist_del_init(&e->colision) here, we did not send the Ack yet,
153b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * neither did we wake possibly waiting conflicting requests.
154b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * done from "drbd_process_done_ee" within the appropriate w.cb
155b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (e_end_block/e_end_resync_block) or from _drbd_clear_done_ee */
156b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
157b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	do_wake = is_syncer_req
158b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		? list_empty(&mdev->sync_ee)
159b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		: list_empty(&mdev->active_ee);
160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
16145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (test_bit(__EE_WAS_ERROR, &e->flags))
162b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		__drbd_chk_io_error(mdev, FALSE);
163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irqrestore(&mdev->req_lock, flags);
164b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
165b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (is_syncer_req)
166b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_rs_complete_io(mdev, e_sector);
167b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
168b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_wake)
169b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
170b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
171b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_al_complete_io)
172b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_al_complete_io(mdev, e_sector);
173b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
174b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	wake_asender(mdev);
175b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
17645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
177b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
17845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg/* writes on behalf of the partner, or resync writes,
17945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver.
18045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg */
18145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_endio_sec(struct bio *bio, int error)
18245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
18345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_epoch_entry *e = bio->bi_private;
18445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
18545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int uptodate = bio_flagged(bio, BIO_UPTODATE);
18645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int is_write = bio_data_dir(bio) == WRITE;
18745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
18845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
18945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		dev_warn(DEV, "%s: error=%d s=%llus\n",
19045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				is_write ? "write" : "read", error,
19145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				(unsigned long long)e->sector);
19245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!error && !uptodate) {
19345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
19445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				is_write ? "write" : "read",
19545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				(unsigned long long)e->sector);
19645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* strange behavior of some lower level drivers...
19745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * fail the request by clearing the uptodate flag,
19845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * but do not return any error?! */
19945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		error = -EIO;
20045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
20145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
20245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
20345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		set_bit(__EE_WAS_ERROR, &e->flags);
20445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
20545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	bio_put(bio); /* no need for the bio anymore */
20645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (atomic_dec_and_test(&e->pending_bios)) {
20745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		if (is_write)
20845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_endio_write_sec_final(e);
20945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		else
21045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_endio_read_sec_final(e);
21145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
213b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_endio_pri(struct bio *bio, int error)
217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags;
219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = bio->bi_private;
220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *mdev = req->mdev;
221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct bio_and_error m;
222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	enum drbd_req_event what;
223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int uptodate = bio_flagged(bio, BIO_UPTODATE);
224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!error && !uptodate) {
226b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_warn(DEV, "p %s: setting error to -EIO\n",
227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 bio_data_dir(bio) == WRITE ? "write" : "read");
228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* strange behavior of some lower level drivers...
229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * fail the request by clearing the uptodate flag,
230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but do not return any error?! */
231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		error = -EIO;
232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
233b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
234b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* to avoid recursion in __req_mod */
235b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(error)) {
236b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		what = (bio_data_dir(bio) == WRITE)
237b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			? write_completed_with_error
2385c3c7e64bbcb60db39d0d55c8fab39ef41d41c30Lars Ellenberg			: (bio_rw(bio) == READ)
239b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			  ? read_completed_with_error
240b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			  : read_ahead_completed_with_error;
241b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
242b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		what = completed_ok;
243b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
244b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	bio_put(req->private_bio);
245b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req->private_bio = ERR_PTR(error);
246b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
247b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irqsave(&mdev->req_lock, flags);
248b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	__req_mod(req, what, &m);
249b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irqrestore(&mdev->req_lock, flags);
250b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
251b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (m.bio)
252b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		complete_master_bio(mdev, &m);
253b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
254b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
255b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_read_retry_remote(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
256b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
257b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
258b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
259b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* We should not detach for read io-error,
260b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but try to WRITE the P_DATA_REPLY to the failed location,
261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * to give the disk the chance to relocate that block */
262b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irq(&mdev->req_lock);
264d255e5ff5fc6cc6c60dd014d1261448a7bbc8134Lars Ellenberg	if (cancel || mdev->state.pdsk != D_UP_TO_DATE) {
265d255e5ff5fc6cc6c60dd014d1261448a7bbc8134Lars Ellenberg		_req_mod(req, read_retry_remote_canceled);
266b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_unlock_irq(&mdev->req_lock);
267b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
268b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
269b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irq(&mdev->req_lock);
270b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
271b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return w_send_read_req(mdev, w, 0);
272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
273b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
274b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_resync_inactive(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
275b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
276b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ERR_IF(cancel) return 1;
277b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_err(DEV, "resync inactive, but callback triggered??\n");
278b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1; /* Simply ignore this! */
279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
28145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm, struct drbd_epoch_entry *e, void *digest)
28245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
28345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct hash_desc desc;
28445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct scatterlist sg;
28545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *page = e->pages;
28645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *tmp;
28745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	unsigned len;
28845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
28945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.tfm = tfm;
29045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.flags = 0;
29145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
29245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_init_table(&sg, 1);
29345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_init(&desc);
29445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
29545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	while ((tmp = page_chain_next(page))) {
29645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* all but the last page will be fully used */
29745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		sg_set_page(&sg, page, PAGE_SIZE, 0);
29845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		crypto_hash_update(&desc, &sg, sg.length);
29945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		page = tmp;
30045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
30145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* and now the last, possibly only partially used page */
30245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	len = e->size & (PAGE_SIZE - 1);
30345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
30445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_update(&desc, &sg, sg.length);
30545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_final(&desc, digest);
30645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
30745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
30845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
309b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
310b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct hash_desc desc;
311b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct scatterlist sg;
312b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct bio_vec *bvec;
313b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i;
314b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
315b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.tfm = tfm;
316b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.flags = 0;
317b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
318b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sg_init_table(&sg, 1);
319b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_init(&desc);
320b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
321b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	__bio_for_each_segment(bvec, bio, i, 0) {
322b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
323b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		crypto_hash_update(&desc, &sg, sg.length);
324b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
325b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_final(&desc, digest);
326b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
327b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_e_send_csum(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
330b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
331b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
334b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
335b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(e->block_id == DRBD_MAGIC + 0xbeef);
336b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
34245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest_size = crypto_hash_digestsize(mdev->csums_tfm);
344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
34645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_drequest_csum(mdev,
350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						     e->sector,
351b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						     e->size,
352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						     digest,
353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						     digest_size,
354b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						     P_CSUM_RS_REQUEST);
355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "kmalloc() of digest failed.\n");
358b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = 0;
359b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
361b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = 1;
362b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
363b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_free_ee(mdev, e);
364b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
366b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
367b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
368b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#define GFP_TRY	(__GFP_HIGHMEM | __GFP_NOWARN)
371b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
372b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
373b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
374b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e;
375b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
377b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* GFP_TRY, because if there is no memory available right now, this may
380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * be rescheduled for later. It is "only" background resync, after all. */
381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	e = drbd_alloc_ee(mdev, DRBD_MAGIC+0xbeef, sector, size, GFP_TRY);
38245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!e)
38345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		goto fail;
384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irq(&mdev->req_lock);
386b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_add(&e->w.list, &mdev->read_ee);
387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irq(&mdev->req_lock);
388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
389b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	e->w.cb = w_e_send_csum;
39045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (drbd_submit_ee(mdev, e, READ, DRBD_FAULT_RS_RD) == 0)
39145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		return 1;
392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
39345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	drbd_free_ee(mdev, e);
39445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergfail:
39545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	put_ldev(mdev);
39645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	return 2;
397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid resync_timer_fn(unsigned long data)
400b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
401b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags;
402b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *) data;
403b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int queue;
404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irqsave(&mdev->req_lock, flags);
406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (likely(!test_and_clear_bit(STOP_SYNC_TIMER, &mdev->flags))) {
408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		queue = 1;
409b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->state.conn == C_VERIFY_S)
410b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->resync_work.cb = w_make_ov_request;
411b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		else
412b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->resync_work.cb = w_make_resync_request;
413b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
414b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		queue = 0;
415b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->resync_work.cb = w_resync_inactive;
416b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
417b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
418b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irqrestore(&mdev->req_lock, flags);
419b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
420b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* harmless race: list_empty outside data.work.q_lock */
421b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (list_empty(&mdev->resync_work.list) && queue)
422b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_queue_work(&mdev->data.work, &mdev->resync_work);
423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
424b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
425778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_set(struct fifo_buffer *fb, int value)
426778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
427778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
428778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
429778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
430778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
431778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
432778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
433778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic int fifo_push(struct fifo_buffer *fb, int value)
434778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
435778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int ov;
436778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
437778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	ov = fb->values[fb->head_index];
438778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fb->values[fb->head_index++] = value;
439778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
440778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (fb->head_index >= fb->size)
441778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->head_index = 0;
442778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
443778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return ov;
444778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
445778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
446778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_add_val(struct fifo_buffer *fb, int value)
447778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
448778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
449778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
450778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
451778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
452778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
453778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
454778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerint drbd_rs_controller(struct drbd_conf *mdev)
455778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
456778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int sect_in;  /* Number of sectors that came in since the last turn */
457778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int want;     /* The number of sectors we want in the proxy */
458778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int req_sect; /* Number of sectors to request in this turn */
459778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int correction; /* Number of sectors more we need in the proxy*/
460778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int cps; /* correction per invocation of drbd_rs_controller() */
461778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int steps; /* Number of time steps to plan ahead */
462778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int curr_corr;
463778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int max_sect;
464778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
465778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
466778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight -= sect_in;
467778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
468778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_lock(&mdev->peer_seq_lock); /* get an atomic view on mdev->rs_plan_s */
469778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
470778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	steps = mdev->rs_plan_s.size; /* (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
471778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
472778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
473778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = ((mdev->sync_conf.rate * 2 * SLEEP_TIME) / HZ) * steps;
474778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else { /* normal path */
475778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = mdev->sync_conf.c_fill_target ? mdev->sync_conf.c_fill_target :
476778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner			sect_in * mdev->sync_conf.c_delay_target * HZ / (SLEEP_TIME * 10);
477778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
478778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
479778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	correction = want - mdev->rs_in_flight - mdev->rs_planed;
480778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
481778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* Plan ahead */
482778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	cps = correction / steps;
483778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fifo_add_val(&mdev->rs_plan_s, cps);
484778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed += cps * steps;
485778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
486778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* What we do in this step */
487778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	curr_corr = fifo_push(&mdev->rs_plan_s, 0);
488778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_unlock(&mdev->peer_seq_lock);
489778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed -= curr_corr;
490778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
491778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	req_sect = sect_in + curr_corr;
492778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect < 0)
493778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = 0;
494778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
495778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	max_sect = (mdev->sync_conf.c_max_rate * 2 * SLEEP_TIME) / HZ;
496778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect > max_sect)
497778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = max_sect;
498778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
499778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/*
500778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
501778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 sect_in, mdev->rs_in_flight, want, correction,
502778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 steps, cps, mdev->rs_planed, curr_corr, req_sect);
503778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	*/
504778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
505778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return req_sect;
506778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
507778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_make_resync_request(struct drbd_conf *mdev,
509b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		struct drbd_work *w, int cancel)
510b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
511b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long bit;
512b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
513b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
514bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg	int max_segment_size;
515d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner	int number, i, rollback_i, size, pe, mx;
516b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int align, queued, sndbuf;
517b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
519b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
521b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(mdev->state.conn < C_CONNECTED)) {
522b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Confused in w_make_resync_request()! cstate < Connected");
523b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
524b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
525b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
526b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->state.conn != C_SYNC_TARGET)
527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "%s in w_make_resync_request\n",
528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_conn_str(mdev->state.conn));
529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
530b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev)) {
531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Since we only need to access mdev->rsync a
532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
533b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   to continue resync with a broken disk makes no sense at
534b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   all */
535b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Disk broke down during resync!\n");
536b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->resync_work.cb = w_resync_inactive;
537b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
538b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
539b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
540bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg	/* starting with drbd 8.3.8, we can handle multi-bio EEs,
541bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg	 * if it should be necessary */
542bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg	max_segment_size = mdev->agreed_pro_version < 94 ?
543bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg		queue_max_segment_size(mdev->rq_queue) : DRBD_MAX_SEGMENT_SIZE;
544bb3d000cb99aa0924b78c1ae5f5943484527868aLars Ellenberg
545778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (mdev->rs_plan_s.size) { /* mdev->sync_conf.c_plan_ahead */
546778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
547778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
548778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else {
549778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		mdev->c_sync_rate = mdev->sync_conf.rate;
550778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		number = SLEEP_TIME * mdev->c_sync_rate  / ((BM_BLOCK_SIZE / 1024) * HZ);
551778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
552b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	pe = atomic_read(&mdev->rs_pending_cnt);
553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mutex_lock(&mdev->data.mutex);
555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->data.socket)
556b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mx = mdev->data.socket->sk->sk_rcvbuf / sizeof(struct p_block_req);
557b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mx = 1;
559b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mutex_unlock(&mdev->data.mutex);
560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* For resync rates >160MB/sec, allow more pending RS requests */
562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (number > mx)
563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mx = number;
564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Limit the number of pending RS requests to no more than the peer's receive buffer */
566b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if ((pe + number) > mx) {
567b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		number = mx - pe;
568b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
569b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
570b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
571b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Stop generating RS requests, when half of the send buffer is filled */
572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mutex_lock(&mdev->data.mutex);
573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->data.socket) {
574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			queued = mdev->data.socket->sk->sk_wmem_queued;
575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			sndbuf = mdev->data.socket->sk->sk_sndbuf;
576b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
577b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			queued = 1;
578b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			sndbuf = 0;
579b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mutex_unlock(&mdev->data.mutex);
581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (queued > sndbuf / 2)
582b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
583b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
584b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnernext_sector:
585b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
586b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		bit  = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
587b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
588b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (bit == -1UL) {
589b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = drbd_bm_bits(mdev);
590b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->resync_work.cb = w_resync_inactive;
591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			put_ldev(mdev);
592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
593b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
594b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector = BM_BIT_TO_SECT(bit);
596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (drbd_try_rs_begin_io(mdev, sector)) {
598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit;
599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
601b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->bm_resync_fo = bit + 1;
602b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
603b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_rs_complete_io(mdev, sector);
605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto next_sector;
606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
608b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#if DRBD_MAX_SEGMENT_SIZE > BM_BLOCK_SIZE
609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* try to find some adjacent bits.
610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we stop if we have already the maximum req size.
611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 *
612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * Additionally always align bigger requests, in order to
613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * be prepared for all stripe sizes of software RAIDs.
614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		align = 1;
616d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner		rollback_i = i;
617b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		for (;;) {
618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (size + BM_BLOCK_SIZE > max_segment_size)
619b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
621b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Be always aligned */
622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (sector & ((1<<(align+3))-1))
623b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
624b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
625b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* do not cross extent boundaries */
626b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
627b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* now, is it actually dirty, after all?
629b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * caution, drbd_bm_test_bit is tri-state for some
630b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * obscure reason; ( b == 0 ) would get the out-of-band
631b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * only accidentally right because of the "oddly sized"
632b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * adjustment below */
633b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (drbd_bm_test_bit(mdev, bit+1) != 1)
634b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
635b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			bit++;
636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size += BM_BLOCK_SIZE;
637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if ((BM_BLOCK_SIZE << align) <= size)
638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				align++;
639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++;
640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
641b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* if we merged some,
642b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * reset the offset to start the next drbd_bm_find_next from */
643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (size > BM_BLOCK_SIZE)
644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit + 1;
645b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#endif
646b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* adjust very last sectors, in case we are oddly sized */
648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->agreed_pro_version >= 89 && mdev->csums_tfm) {
651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			switch (read_for_csum(mdev, sector, size)) {
652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			case 0: /* Disk failure*/
653b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
654b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			case 2: /* Allocation failed */
656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_rs_complete_io(mdev, sector);
657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
658d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner				i = rollback_i;
659b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				goto requeue;
660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* case 1: everything ok */
661b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (!drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					       sector, size, ID_SYNCER)) {
666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
667b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dec_rs_pending(mdev);
668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
669b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
671b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
672b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* last syncer _request_ was sent,
676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but the P_RS_DATA_REPLY not yet received.  sync will end (and
677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * next sync group will resume), as soon as we receive the last
678b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * resync data block, and the last bit is cleared.
679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * until then resync "work" is "inactive" ...
680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->resync_work.cb = w_resync_inactive;
682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
683b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
685b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
687778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
688b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int number, i, size;
696b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
698b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
699b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(mdev->state.conn < C_CONNECTED)) {
703b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Confused in w_make_ov_request()! cstate < Connected");
704b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
705b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
706b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	number = SLEEP_TIME*mdev->sync_conf.rate / ((BM_BLOCK_SIZE/1024)*HZ);
708b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (atomic_read(&mdev->rs_pending_cnt) > number)
709b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto requeue;
710b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
711b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	number -= atomic_read(&mdev->rs_pending_cnt);
712b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
713b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector = mdev->ov_position;
714b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
715b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector >= capacity) {
716b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->resync_work.cb = w_resync_inactive;
717b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
720b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
721b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
722b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (drbd_try_rs_begin_io(mdev, sector)) {
723b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->ov_position = sector;
724b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
725b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
726b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
727b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
728b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
729b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
730b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		inc_rs_pending(mdev);
731b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!drbd_send_ov_request(mdev, sector, size)) {
732b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dec_rs_pending(mdev);
733b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
734b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector += BM_SECT_PER_BIT;
736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
737b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->ov_position = sector;
738b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
739b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
740b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
741b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
743b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
744b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
745b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_ov_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
746b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
747b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
748b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ov_oos_print(mdev);
749b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
750b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
751b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
752b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
753b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
754b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_resync_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
755b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
756b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
757b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
758b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
759b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
760b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
761b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
762b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
763b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_resync_finished(struct drbd_conf *mdev)
764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
765b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long db, dt, dbdt;
766b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long n_oos;
767b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state os, ns;
768b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w;
769b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	char *khelper_cmd = NULL;
770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
771b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Remove all elements from the resync LRU. Since future actions
772b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * might set bits in the (main) bitmap, then the entries in the
773b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * resync LRU would be wrong. */
774b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (drbd_rs_del_all(mdev)) {
775b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* In case this is not possible now, most probably because
776b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * there are P_RS_DATA_REPLY Packets lingering on the worker's
777b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * queue (or even the read operations for those packets
778b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * is not finished by now).   Retry in 100ms. */
779b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
780b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_kick_lo(mdev);
781b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		__set_current_state(TASK_INTERRUPTIBLE);
782b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		schedule_timeout(HZ / 10);
783b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
784b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (w) {
785b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb = w_resync_finished;
786b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_queue_work(&mdev->data.work, w);
787b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
788b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
789b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
790b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
791b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
792b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
793b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (dt <= 0)
794b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dt = 1;
795b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	db = mdev->rs_total;
796b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dbdt = Bit2KB(db/dt);
797b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused /= HZ;
798b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
799b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
800b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
801b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
802b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irq(&mdev->req_lock);
803b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	os = mdev->state;
804b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
805b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* This protects us against multiple calls (that can happen in the presence
806b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	   of application IO), and against connectivity loss just before we arrive here. */
807b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn <= C_CONNECTED)
808b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out_unlock;
809b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
810b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = os;
811b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = C_CONNECTED;
812b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
813b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
814b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	     (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) ?
815b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	     "Online verify " : "Resync",
816b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	     dt + mdev->rs_paused, mdev->rs_paused, dbdt);
817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
818b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	n_oos = drbd_bm_total_weight(mdev);
819b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
821b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (n_oos) {
822b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
823b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      n_oos, Bit2KB(1));
824b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "out-of-sync";
825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
826b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		D_ASSERT((n_oos - mdev->rs_failed) == 0);
828b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
829b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
830b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "after-resync-target";
831b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
832b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm && mdev->rs_total) {
833b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long s = mdev->rs_same_csum;
834b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long t = mdev->rs_total;
835b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const int ratio =
836b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(t == 0)     ? 0 :
837b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			(t < 100000) ? ((s*100)/t) : (s/(t/100));
838b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_info(DEV, "%u %% had equal check sums, eliminated: %luK; "
839b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "transferred %luK total %luK\n",
840b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     ratio,
841b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_same_csum),
842b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total - mdev->rs_same_csum),
843b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total));
844b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
845b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
846b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
847b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->rs_failed) {
848b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "            %lu failed blocks\n", mdev->rs_failed);
849b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
850b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
851b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_INCONSISTENT;
852b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_UP_TO_DATE;
853b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
854b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_UP_TO_DATE;
855b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_INCONSISTENT;
856b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
857b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
858b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_UP_TO_DATE;
859b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_UP_TO_DATE;
860b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
861b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
862b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->p_uuid) {
863b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				int i;
864b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
865b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					_drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
866b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
867b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				_drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
868b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			} else {
869b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
870b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
871b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
872b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
873b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_uuid_set_bm(mdev, 0UL);
874b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
875b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->p_uuid) {
876b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Now the two UUID sets are equal, update what we
877b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * know of the peer. */
878b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			int i;
879b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
880b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
881b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
882b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
883b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
884b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
885b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout_unlock:
886b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irq(&mdev->req_lock);
887b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
888b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
889b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_total  = 0;
890b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_failed = 0;
891b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused = 0;
892b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->ov_start_sector = 0;
893b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
894b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (test_and_clear_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags)) {
895b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_warn(DEV, "Writing the whole bitmap, due to failed kmalloc\n");
896b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_queue_bitmap_io(mdev, &drbd_bm_write, NULL, "write from resync_finished");
897b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
898b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
899b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (khelper_cmd)
900b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_khelper(mdev, khelper_cmd);
901b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
902b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
903b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
904b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
905b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* helper */
906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
907b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
90845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (drbd_ee_has_active_page(e)) {
909b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* This might happen if sendpage() has not finished */
910b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_lock_irq(&mdev->req_lock);
911b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_add_tail(&e->w.list, &mdev->net_ee);
912b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_unlock_irq(&mdev->req_lock);
913b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
914b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
916b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
917b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
918b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
919b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
920b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
921b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
922b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
923b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_data_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
924b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
925b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
926b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
927b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
928b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
929b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
930b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
931b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
932b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
933b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
93445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
935b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_block(mdev, P_DATA_REPLY, e);
936b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
937b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
938b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
939b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			    (unsigned long long)e->sector);
940b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
941b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_DREPLY, e);
942b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
943b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
944b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
945b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
946b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
947b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
948b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
949b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
950b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
951b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
952b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
953b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
954b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUESTRS
955b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
956b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
957b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
958b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
959b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_rsdata_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
960b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
961b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
962b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
963b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
964b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
965b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
966b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
967b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
968b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
969b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
970b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (get_ldev_if_state(mdev, D_FAILED)) {
971b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_rs_complete_io(mdev, e->sector);
972b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
973b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
974b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
97545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
976b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
977b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
978b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
979b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
980b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (__ratelimit(&drbd_ratelimit_state))
981b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "Not sending RSDataReply, "
982b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				    "partner DISKLESS!\n");
983b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = 1;
984b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
985b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
986b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
987b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
988b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			    (unsigned long long)e->sector);
989b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
990b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
991b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
992b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* update resync data with failure */
993b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_rs_failed_io(mdev, e->sector, e->size);
994b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
995b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
996b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
997b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
998b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
999b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1000b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
1001b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
1002b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1003b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1004b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1005b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_csum_rs_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1006b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1007b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1008b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1009b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1010b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest = NULL;
1011b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1012b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1013b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1014b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
1015b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1016b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1017b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1018b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1019b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_rs_complete_io(mdev, e->sector);
1020b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
102185719573dd716bc2ac3e098b44adfed884250babPhilipp Reisner	di = e->digest;
1022b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
102345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
1024b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* quick hack to try to avoid a race against reconfiguration.
1025b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * a real fix would be much more involved,
1026b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * introducing more locking mechanisms */
1027b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm) {
1028b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest_size = crypto_hash_digestsize(mdev->csums_tfm);
1029b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1030b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest = kmalloc(digest_size, GFP_NOIO);
1031b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1032b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
103345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
1034b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1035b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1036b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1037b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1038b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (eq) {
1039b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_set_in_sync(mdev, e->sector, e->size);
1040676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			/* rs_same_csums unit is BM_BLOCK_SIZE */
1041676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			mdev->rs_same_csum += e->size >> BM_BLOCK_SHIFT;
1042b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, e);
1043b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1044b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
1045b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			e->block_id = ID_SYNCER;
1046b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
1047b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1048b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1049b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1050b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1051b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1052b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1053b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1054b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1055b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1056b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(di);
1057b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1058b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
1059b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1060b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
1061b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block/ack() failed\n");
1062b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1063b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1064b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1065b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_ov_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1066b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1067b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1068b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1069b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1070b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1071b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1072b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
1073b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1074b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
107545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (unlikely((e->flags & EE_WAS_ERROR) != 0))
1076b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1077b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1078b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1079b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* FIXME if this allocation fails, online verify will not terminate! */
1080b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest = kmalloc(digest_size, GFP_NOIO);
1081b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (digest) {
108245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
1083b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		inc_rs_pending(mdev);
1084b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_drequest_csum(mdev, e->sector, e->size,
1085b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					     digest, digest_size, P_OV_REPLY);
1086b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!ok)
1087b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dec_rs_pending(mdev);
1088b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		kfree(digest);
1089b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1090b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1091b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
1092b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_free_ee(mdev, e);
1093b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1094b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1095b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1096b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1097b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1098b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1099b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_ov_oos_found(struct drbd_conf *mdev, sector_t sector, int size)
1100b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1101b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size += size>>9;
1103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_start = sector;
1105b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size = size>>9;
1106b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_set_out_of_sync(mdev, sector, size);
1108b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
1109b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_ov_reply(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1112b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1116b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1118b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
1121b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1122b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1124b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * the resync lru has been cleaned up already */
1127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_rs_complete_io(mdev, e->sector);
1128b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
112985719573dd716bc2ac3e098b44adfed884250babPhilipp Reisner	di = e->digest;
1130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
113145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
1132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1133b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
1134b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
113545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
1136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1137b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1138b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1145b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1146b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1147b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1149b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(di);
1150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!eq)
1152b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_ov_oos_found(mdev, e->sector, e->size);
1153b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
1154b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1155b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1156b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = drbd_send_ack_ex(mdev, P_OV_RESULT, e->sector, e->size,
1157b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1158b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1159b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_free_ee(mdev, e);
1160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1161b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (--mdev->ov_left == 0) {
1162b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_resync_finished(mdev);
1164b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1165b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1166b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1167b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1168b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1169b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_prev_work_done(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1170b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1171b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
1172b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&b->done);
1173b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
1174b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1175b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1176b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_barrier(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1177b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1178b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
1179b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct p_barrier *p = &mdev->data.sbuf.barrier;
1180b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1181b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1182b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* really avoid racing with tl_clear.  w.cb may have been referenced
1183b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * just before it was reassigned and re-queued, so double check that.
1184b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * actually, this race was harmless, since we only try to send the
1185b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * barrier packet here, and otherwise do nothing with the object.
1186b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but compare with the head of w_clear_epoch */
1187b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irq(&mdev->req_lock);
1188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		cancel = 1;
1190b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irq(&mdev->req_lock);
1191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1192b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1193b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1194b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!drbd_get_data_sock(mdev))
1195b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
1196b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	p->barrier = b->br_number;
1197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* inc_ap_pending was done where this was queued.
1198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * dec_ap_pending will be done in got_BarrierAck
1199b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * or (on connection loss) in w_clear_epoch.  */
1200b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BARRIER,
1201b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(struct p_header *)p, sizeof(*p), 0);
1202b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_put_data_sock(mdev);
1203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1207b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_write_hint(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1211b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return drbd_send_short_cmd(mdev, P_UNPLUG_REMOTE);
1212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1213b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_dblock(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1226b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		req_mod(req, send_canceled);
1227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = drbd_send_dblock(mdev, req);
1231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req_mod(req, ok ? handed_over_to_network : send_failed);
1232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1233b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1234b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1235b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1236b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1237b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1238b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1239b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1240b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1241b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1242b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_read_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1243b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1244b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1245b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1246b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1247b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1248b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		req_mod(req, send_canceled);
1249b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1250b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1251b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1252b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = drbd_send_drequest(mdev, P_DATA_REQUEST, req->sector, req->size,
1253b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(unsigned long)req);
1254b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1255b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!ok) {
1256b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ?? we set C_TIMEOUT or C_BROKEN_PIPE in drbd_send();
1257b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * so this is probably redundant */
1258b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->state.conn >= C_CONNECTED)
1259b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
1260b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req_mod(req, ok ? handed_over_to_network : send_failed);
1262b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1265b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1266265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisnerint w_restart_disk_io(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1267265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner{
1268265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1269265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1270265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	if (bio_data_dir(req->master_bio) == WRITE)
1271265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner		drbd_al_begin_io(mdev, req->sector);
1272265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	/* Calling drbd_al_begin_io() out of the worker might deadlocks
1273265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   theoretically. Practically it can not deadlock, since this is
1274265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   only used when unfreezing IOs. All the extents of the requests
1275265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   that made it into the TL are already active */
1276265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1277265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	drbd_req_make_private_bio(req, req->master_bio);
1278265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1279265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	generic_make_request(req->private_bio);
1280265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1281265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	return 1;
1282265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner}
1283265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_may_sync_now(struct drbd_conf *mdev)
1285b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1286b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev = mdev;
1287b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1288b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1289b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1290b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
1291b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ERR_IF(!odev) return 1;
1293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if ((odev->state.conn >= C_SYNC_SOURCE &&
1294b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     odev->state.conn <= C_PAUSED_SYNC_T) ||
1295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.aftr_isp || odev->state.peer_isp ||
1296b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.user_isp)
1297b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
1298b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1300b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1301b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1302b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_pause_after() - Pause resync on all devices that may not resync now
1303b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1304b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1305b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and after_state_ch).
1306b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1307b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_pause_after(struct drbd_conf *mdev)
1308b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1309b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1310b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1311b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1312b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1313b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1314b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1315b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1316b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1317b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1318b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!_drbd_may_sync_now(odev))
1319b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1320b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			       != SS_NOTHING_TO_DO);
1321b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1322b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1323b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1324b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1325b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1326b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1327b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_resume_next() - Resume resync on all devices that may resync now
1328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1330b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and worker).
1331b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_resume_next(struct drbd_conf *mdev)
1333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1334b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1335b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1336b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1342b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.aftr_isp) {
1344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (_drbd_may_sync_now(odev))
1345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1346b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner							CS_HARD, NULL)
1347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				       != SS_NOTHING_TO_DO) ;
1348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1351b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid resume_next_sg(struct drbd_conf *mdev)
1354b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_resume_next(mdev);
1357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1358b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1359b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid suspend_other_sg(struct drbd_conf *mdev)
1361b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1362b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1363b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_pause_after(mdev);
1364b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1366b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1367b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int sync_after_error(struct drbd_conf *mdev, int o_minor)
1368b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1371b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor == -1)
1372b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return NO_ERROR;
1373b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1374b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return ERR_SYNC_AFTER;
1375b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* check for loops */
1377b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	odev = minor_to_mdev(o_minor);
1378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev == mdev)
1380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return ERR_SYNC_AFTER_CYCLE;
1381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1382b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* dependency chain ends here, no cycles. */
1383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return NO_ERROR;
1385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1386b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* follow the dependency chain */
1387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1389b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1390b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1391b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_alter_sa(struct drbd_conf *mdev, int na)
1392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int changes;
1394b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int retcode;
1395b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1396b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	retcode = sync_after_error(mdev, na);
1398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (retcode == NO_ERROR) {
1399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->sync_conf.after = na;
1400b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		do {
1401b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes  = _drbd_pause_after(mdev);
1402b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes |= _drbd_resume_next(mdev);
1403b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} while (changes);
1404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return retcode;
1407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1409309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisnerstatic void ping_peer(struct drbd_conf *mdev)
1410309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner{
1411309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner	clear_bit(GOT_PING_ACK, &mdev->flags);
1412309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner	request_ping(mdev);
1413309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner	wait_event(mdev->misc_wait,
1414309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner		   test_bit(GOT_PING_ACK, &mdev->flags) || mdev->state.conn < C_CONNECTED);
1415309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner}
1416309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner
1417b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1418b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * drbd_start_resync() - Start the resync process
1419b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1420b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @side:	Either C_SYNC_SOURCE or C_SYNC_TARGET
1421b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1422b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * This function might bring you directly into one of the
1423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * C_PAUSED_SYNC_* states.
1424b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1425b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1426b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1427b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state ns;
1428b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int r;
1429b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1430b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->state.conn >= C_SYNC_SOURCE) {
1431b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Resync already running!\n");
1432b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1433b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1434b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1435b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* In case a previous resync run was aborted by an IO error/detach on the peer. */
1436b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_rs_cancel_all(mdev);
1437b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1438b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET) {
1439b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Since application IO was locked out during C_WF_BITMAP_T and
1440b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1441b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   we check that we might make the data inconsistent. */
1442b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = drbd_khelper(mdev, "before-resync-target");
1443b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = (r >> 8) & 0xff;
1444b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (r > 0) {
1445b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_info(DEV, "before-resync-target handler returned %d, "
1446b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "dropping connection.\n", r);
1447b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1448b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return;
1449b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1450b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1451b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1452b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_state_lock(mdev);
1453b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1454b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1455b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_state_unlock(mdev);
1456b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1457b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1458b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1459b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET) {
1460b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->bm_resync_fo = 0;
1461b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else /* side == C_SYNC_SOURCE */ {
1462b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		u64 uuid;
1463b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1464b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		get_random_bytes(&uuid, sizeof(u64));
1465b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_uuid_set(mdev, UI_BITMAP, uuid);
1466b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_send_sync_uuid(mdev, uuid);
1467b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1468b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
1469b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1470b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1471b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1472b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1473b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1474b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.aftr_isp = !_drbd_may_sync_now(mdev);
1475b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1476b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = side;
1477b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1478b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET)
1479b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_INCONSISTENT;
1480b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else /* side == C_SYNC_SOURCE */
1481b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_INCONSISTENT;
1482b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1483b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1484b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1485b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1486b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (ns.conn < C_CONNECTED)
1487b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = SS_UNKNOWN_ERROR;
1488b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1489b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1490b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_total     =
1491b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_mark_left = drbd_bm_total_weight(mdev);
1492b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_failed    = 0;
1493b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_paused    = 0;
1494b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_start     =
1495b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_mark_time = jiffies;
1496b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_same_csum = 0;
1497b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		_drbd_pause_after(mdev);
1498b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1499b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1500b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
1501b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1502b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1503b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1504b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     drbd_conn_str(ns.conn),
1505b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1506b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total);
1507b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->rs_total == 0) {
1509b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Peer still reachable? Beware of failing before-resync-target handlers! */
1510309d1608cce32903d67d47e7545e232c400b6aa0Philipp Reisner			ping_peer(mdev);
1511b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_resync_finished(mdev);
1512b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1513b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1514778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		atomic_set(&mdev->rs_sect_in, 0);
1515778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		mdev->rs_in_flight = 0;
1516778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		mdev->rs_planed = 0;
1517778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		spin_lock(&mdev->peer_seq_lock);
1518778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fifo_set(&mdev->rs_plan_s, 0);
1519778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		spin_unlock(&mdev->peer_seq_lock);
1520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ns.conn may already be != mdev->state.conn,
1521b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we may have been paused in between, or become paused until
1522b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * the timer triggers.
1523b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * No matter, that is handled in resync_timer_fn() */
1524b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (ns.conn == C_SYNC_TARGET)
1525b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mod_timer(&mdev->resync_timer, jiffies);
1526b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_md_sync(mdev);
1528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1529d0c3f60f3611ceac9b1e4fdffd1497337568e7cbPhilipp Reisner	drbd_state_unlock(mdev);
1530b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_worker(struct drbd_thread *thi)
1533b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1534b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *mdev = thi->mdev;
1535b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w = NULL;
1536b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	LIST_HEAD(work_list);
1537b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int intr = 0, i;
1538b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1539b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sprintf(current->comm, "drbd%d_worker", mdev_to_minor(mdev));
1540b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1541b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (get_t_state(thi) == Running) {
1542b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_thread_current_set_cpu(mdev);
1543b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1544b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (down_trylock(&mdev->data.work.s)) {
1545b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mutex_lock(&mdev->data.mutex);
1546b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->data.socket && !mdev->net_conf->no_cork)
1547b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_tcp_uncork(mdev->data.socket);
1548b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mutex_unlock(&mdev->data.mutex);
1549b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1550b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			intr = down_interruptible(&mdev->data.work.s);
1551b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1552b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mutex_lock(&mdev->data.mutex);
1553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->data.socket  && !mdev->net_conf->no_cork)
1554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_tcp_cork(mdev->data.socket);
1555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mutex_unlock(&mdev->data.mutex);
1556b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1557b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (intr) {
1559b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(intr == -EINTR);
1560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			flush_signals(current);
1561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ERR_IF (get_t_state(thi) == Running)
1562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				continue;
1563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1566b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (get_t_state(thi) != Running)
1567b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1568b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* With this break, we have done a down() but not consumed
1569b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   the entry from the list. The cleanup code takes care of
1570b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   this...   */
1571b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = NULL;
1573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_lock_irq(&mdev->data.work.q_lock);
1574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ERR_IF(list_empty(&mdev->data.work.q)) {
1575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* something terribly wrong in our logic.
1576b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * we were able to down() the semaphore,
1577b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * but the list is empty... doh.
1578b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1579b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * what is the best thing to do now?
1580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * try again from scratch, restarting the receiver,
1581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * asender, whatnot? could break even more ugly,
1582b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * e.g. when we are primary, but no good local data.
1583b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1584b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * I'll try to get away just starting over this loop.
1585b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 */
1586b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			spin_unlock_irq(&mdev->data.work.q_lock);
1587b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1588b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1589b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = list_entry(mdev->data.work.q.next, struct drbd_work, list);
1590b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_del_init(&w->list);
1591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_unlock_irq(&mdev->data.work.q_lock);
1592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1593b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!w->cb(mdev, w, mdev->state.conn < C_CONNECTED)) {
1594b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* dev_warn(DEV, "worker: a callback failed! \n"); */
1595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->state.conn >= C_CONNECTED)
1596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_force_state(mdev,
1597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						NS(conn, C_NETWORK_FAILURE));
1598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(test_bit(DEVICE_DYING, &mdev->flags));
1601b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(test_bit(CONFIG_PENDING, &mdev->flags));
1602b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1603b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_lock_irq(&mdev->data.work.q_lock);
1604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	i = 0;
1605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (!list_empty(&mdev->data.work.q)) {
1606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_splice_init(&mdev->data.work.q, &work_list);
1607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_unlock_irq(&mdev->data.work.q_lock);
1608b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		while (!list_empty(&work_list)) {
1610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w = list_entry(work_list.next, struct drbd_work, list);
1611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			list_del_init(&w->list);
1612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb(mdev, w, 1);
1613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++; /* dead debugging code */
1614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1616b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		spin_lock_irq(&mdev->data.work.q_lock);
1617b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sema_init(&mdev->data.work.s, 0);
1619b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* DANGEROUS race: if someone did queue his work within the spinlock,
1620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but up() ed outside the spinlock, we could get an up() on the
1621b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * semaphore without corresponding list entry.
1622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * So don't do that.
1623b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 */
1624b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	spin_unlock_irq(&mdev->data.work.q_lock);
1625b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1626b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
1627b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* _drbd_set_state only uses stop_nowait.
1628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * wait here for the Exiting receiver. */
1629b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_thread_stop(&mdev->receiver);
1630b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_mdev_cleanup(mdev);
1631b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1632b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "worker terminated\n");
1633b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1634b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	clear_bit(DEVICE_DYING, &mdev->flags);
1635b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	clear_bit(CONFIG_PENDING, &mdev->flags);
1636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	wake_up(&mdev->state_wait);
1637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 0;
1639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1640